OLD | NEW |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/ui/libgtkui/print_dialog_gtk2.h" | 5 #include "chrome/browser/ui/libgtkui/print_dialog_gtk.h" |
6 | 6 |
7 #include <gtk/gtkunixprint.h> | 7 #include <gtk/gtkunixprint.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 #include <cmath> | 10 #include <cmath> |
11 #include <string> | 11 #include <string> |
12 #include <vector> | 12 #include <vector> |
13 | 13 |
14 #include "base/bind.h" | 14 #include "base/bind.h" |
15 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
16 #include "base/files/file_util_proxy.h" | 16 #include "base/files/file_util_proxy.h" |
17 #include "base/lazy_instance.h" | 17 #include "base/lazy_instance.h" |
18 #include "base/logging.h" | 18 #include "base/logging.h" |
19 #include "base/macros.h" | 19 #include "base/macros.h" |
20 #include "base/strings/utf_string_conversions.h" | 20 #include "base/strings/utf_string_conversions.h" |
21 #include "base/values.h" | 21 #include "base/values.h" |
22 #include "chrome/browser/ui/libgtkui/gtk2_util.h" | 22 #include "chrome/browser/ui/libgtkui/gtk_util.h" |
23 #include "chrome/browser/ui/libgtkui/printing_gtk2_util.h" | 23 #include "chrome/browser/ui/libgtkui/printing_gtk_util.h" |
24 #include "printing/metafile.h" | 24 #include "printing/metafile.h" |
25 #include "printing/print_job_constants.h" | 25 #include "printing/print_job_constants.h" |
26 #include "printing/print_settings.h" | 26 #include "printing/print_settings.h" |
27 #include "ui/aura/window.h" | 27 #include "ui/aura/window.h" |
28 #include "ui/events/platform/x11/x11_event_source.h" | 28 #include "ui/events/platform/x11/x11_event_source.h" |
29 | 29 |
30 using content::BrowserThread; | 30 using content::BrowserThread; |
31 using printing::PageRanges; | 31 using printing::PageRanges; |
32 using printing::PrintSettings; | 32 using printing::PrintSettings; |
33 | 33 |
(...skipping 15 matching lines...) Expand all Loading... |
49 // 1mm difference). In the exact mode, looks for the paper with the same PPD | 49 // 1mm difference). In the exact mode, looks for the paper with the same PPD |
50 // name and "close enough" size. | 50 // name and "close enough" size. |
51 bool PaperSizeMatch(GtkPaperSize* gtk_paper_size, | 51 bool PaperSizeMatch(GtkPaperSize* gtk_paper_size, |
52 const PrintSettings::RequestedMedia& media, | 52 const PrintSettings::RequestedMedia& media, |
53 bool fuzzy_match) { | 53 bool fuzzy_match) { |
54 if (!gtk_paper_size) { | 54 if (!gtk_paper_size) { |
55 return false; | 55 return false; |
56 } | 56 } |
57 gfx::Size paper_size_microns( | 57 gfx::Size paper_size_microns( |
58 static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) * | 58 static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) * |
59 kMicronsInMm + 0.5), | 59 kMicronsInMm + |
| 60 0.5), |
60 static_cast<int>(gtk_paper_size_get_height(gtk_paper_size, GTK_UNIT_MM) * | 61 static_cast<int>(gtk_paper_size_get_height(gtk_paper_size, GTK_UNIT_MM) * |
61 kMicronsInMm + 0.5)); | 62 kMicronsInMm + |
| 63 0.5)); |
62 int diff = std::max( | 64 int diff = std::max( |
63 std::abs(paper_size_microns.width() - media.size_microns.width()), | 65 std::abs(paper_size_microns.width() - media.size_microns.width()), |
64 std::abs(paper_size_microns.height() - media.size_microns.height())); | 66 std::abs(paper_size_microns.height() - media.size_microns.height())); |
65 if (fuzzy_match) { | 67 if (fuzzy_match) { |
66 return diff <= kPaperSizeTresholdMicrons; | 68 return diff <= kPaperSizeTresholdMicrons; |
67 } | 69 } |
68 return !media.vendor_id.empty() && | 70 return !media.vendor_id.empty() && |
69 media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) && | 71 media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) && |
70 diff <= kPaperSizeTresholdMicrons; | 72 diff <= kPaperSizeTresholdMicrons; |
71 } | 73 } |
(...skipping 11 matching lines...) Expand all Loading... |
83 } | 85 } |
84 if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) { | 86 if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) { |
85 first_fuzzy_match = gtk_paper_size; | 87 first_fuzzy_match = gtk_paper_size; |
86 } | 88 } |
87 } | 89 } |
88 return first_fuzzy_match; | 90 return first_fuzzy_match; |
89 } | 91 } |
90 | 92 |
91 class StickyPrintSettingGtk { | 93 class StickyPrintSettingGtk { |
92 public: | 94 public: |
93 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) { | 95 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {} |
94 } | |
95 ~StickyPrintSettingGtk() { | 96 ~StickyPrintSettingGtk() { |
96 NOTREACHED(); // Intended to be used with a Leaky LazyInstance. | 97 NOTREACHED(); // Intended to be used with a Leaky LazyInstance. |
97 } | 98 } |
98 | 99 |
99 GtkPrintSettings* settings() { | 100 GtkPrintSettings* settings() { return last_used_settings_; } |
100 return last_used_settings_; | |
101 } | |
102 | 101 |
103 void SetLastUsedSettings(GtkPrintSettings* settings) { | 102 void SetLastUsedSettings(GtkPrintSettings* settings) { |
104 DCHECK(last_used_settings_); | 103 DCHECK(last_used_settings_); |
105 g_object_unref(last_used_settings_); | 104 g_object_unref(last_used_settings_); |
106 last_used_settings_ = gtk_print_settings_copy(settings); | 105 last_used_settings_ = gtk_print_settings_copy(settings); |
107 } | 106 } |
108 | 107 |
109 private: | 108 private: |
110 GtkPrintSettings* last_used_settings_; | 109 GtkPrintSettings* last_used_settings_; |
111 | 110 |
(...skipping 12 matching lines...) Expand all Loading... |
124 | 123 |
125 ~GtkPrinterList() { | 124 ~GtkPrinterList() { |
126 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); | 125 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); |
127 it < printers_.end(); ++it) { | 126 it < printers_.end(); ++it) { |
128 g_object_unref(*it); | 127 g_object_unref(*it); |
129 } | 128 } |
130 } | 129 } |
131 | 130 |
132 // Can return NULL if there's no default printer. E.g. Printer on a laptop | 131 // Can return NULL if there's no default printer. E.g. Printer on a laptop |
133 // is "home_printer", but the laptop is at work. | 132 // is "home_printer", but the laptop is at work. |
134 GtkPrinter* default_printer() { | 133 GtkPrinter* default_printer() { return default_printer_; } |
135 return default_printer_; | |
136 } | |
137 | 134 |
138 // Can return NULL if the printer cannot be found due to: | 135 // Can return NULL if the printer cannot be found due to: |
139 // - Printer list out of sync with printer dialog UI. | 136 // - Printer list out of sync with printer dialog UI. |
140 // - Querying for non-existant printers like 'Print to PDF'. | 137 // - Querying for non-existant printers like 'Print to PDF'. |
141 GtkPrinter* GetPrinterWithName(const std::string& name) { | 138 GtkPrinter* GetPrinterWithName(const std::string& name) { |
142 if (name.empty()) | 139 if (name.empty()) |
143 return NULL; | 140 return NULL; |
144 | 141 |
145 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); | 142 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); |
146 it < printers_.end(); ++it) { | 143 it < printers_.end(); ++it) { |
(...skipping 29 matching lines...) Expand all Loading... |
176 PrintingContextLinux* context) { | 173 PrintingContextLinux* context) { |
177 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 174 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
178 return new PrintDialogGtk2(context); | 175 return new PrintDialogGtk2(context); |
179 } | 176 } |
180 | 177 |
181 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux* context) | 178 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux* context) |
182 : context_(context), | 179 : context_(context), |
183 dialog_(NULL), | 180 dialog_(NULL), |
184 gtk_settings_(NULL), | 181 gtk_settings_(NULL), |
185 page_setup_(NULL), | 182 page_setup_(NULL), |
186 printer_(NULL) { | 183 printer_(NULL) {} |
187 } | |
188 | 184 |
189 PrintDialogGtk2::~PrintDialogGtk2() { | 185 PrintDialogGtk2::~PrintDialogGtk2() { |
190 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 186 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
191 | 187 |
192 if (dialog_) { | 188 if (dialog_) { |
193 aura::Window* parent = libgtkui::GetAuraTransientParent(dialog_); | 189 aura::Window* parent = libgtkui::GetAuraTransientParent(dialog_); |
194 if (parent) { | 190 if (parent) { |
195 parent->RemoveObserver(this); | 191 parent->RemoveObserver(this); |
196 libgtkui::ClearAuraTransientParent(dialog_); | 192 libgtkui::ClearAuraTransientParent(dialog_); |
197 } | 193 } |
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
295 VLOG(1) << "Using custom paper size"; | 291 VLOG(1) << "Using custom paper size"; |
296 GtkPaperSize* custom_size = gtk_paper_size_new_custom( | 292 GtkPaperSize* custom_size = gtk_paper_size_new_custom( |
297 requested_media.vendor_id.c_str(), | 293 requested_media.vendor_id.c_str(), |
298 requested_media.vendor_id.c_str(), | 294 requested_media.vendor_id.c_str(), |
299 requested_media.size_microns.width() / kMicronsInMm, | 295 requested_media.size_microns.width() / kMicronsInMm, |
300 requested_media.size_microns.height() / kMicronsInMm, | 296 requested_media.size_microns.height() / kMicronsInMm, |
301 GTK_UNIT_MM); | 297 GTK_UNIT_MM); |
302 gtk_page_setup_set_paper_size(page_setup_, custom_size); | 298 gtk_page_setup_set_paper_size(page_setup_, custom_size); |
303 gtk_paper_size_free(custom_size); | 299 gtk_paper_size_free(custom_size); |
304 } | 300 } |
305 #if GTK_CHECK_VERSION(2,28,0) | 301 #if GTK_CHECK_VERSION(2, 28, 0) |
306 g_list_free_full(gtk_paper_sizes, | 302 g_list_free_full(gtk_paper_sizes, |
307 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free)); | 303 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free)); |
308 #else | 304 #else |
309 g_list_foreach(gtk_paper_sizes, | 305 g_list_foreach(gtk_paper_sizes, |
310 reinterpret_cast<GFunc>(gtk_paper_size_free), NULL); | 306 reinterpret_cast<GFunc>(gtk_paper_size_free), NULL); |
311 g_list_free(gtk_paper_sizes); | 307 g_list_free(gtk_paper_sizes); |
312 #endif | 308 #endif |
313 } | 309 } |
314 } else { | 310 } else { |
315 VLOG(1) << "Using default paper size"; | 311 VLOG(1) << "Using default paper size"; |
316 } | 312 } |
317 } | 313 } |
318 | 314 |
319 gtk_print_settings_set_orientation( | 315 gtk_print_settings_set_orientation( |
320 gtk_settings_, | 316 gtk_settings_, settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE |
321 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE : | 317 : GTK_PAGE_ORIENTATION_PORTRAIT); |
322 GTK_PAGE_ORIENTATION_PORTRAIT); | |
323 | 318 |
324 InitPrintSettings(settings); | 319 InitPrintSettings(settings); |
325 return true; | 320 return true; |
326 } | 321 } |
327 | 322 |
328 void PrintDialogGtk2::ShowDialog( | 323 void PrintDialogGtk2::ShowDialog( |
329 gfx::NativeView parent_view, | 324 gfx::NativeView parent_view, |
330 bool has_selection, | 325 bool has_selection, |
331 const PrintingContextLinux::PrintSettingsCallback& callback) { | 326 const PrintingContextLinux::PrintSettingsCallback& callback) { |
332 callback_ = callback; | 327 callback_ = callback; |
(...skipping 67 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 | 395 |
401 if (!success) { | 396 if (!success) { |
402 LOG(ERROR) << "Saving metafile failed"; | 397 LOG(ERROR) << "Saving metafile failed"; |
403 // Matches AddRef() above. | 398 // Matches AddRef() above. |
404 Release(); | 399 Release(); |
405 return; | 400 return; |
406 } | 401 } |
407 | 402 |
408 // No errors, continue printing. | 403 // No errors, continue printing. |
409 BrowserThread::PostTask( | 404 BrowserThread::PostTask( |
410 BrowserThread::UI, | 405 BrowserThread::UI, FROM_HERE, |
411 FROM_HERE, | |
412 base::Bind(&PrintDialogGtk2::SendDocumentToPrinter, this, document_name)); | 406 base::Bind(&PrintDialogGtk2::SendDocumentToPrinter, this, document_name)); |
413 } | 407 } |
414 | 408 |
415 void PrintDialogGtk2::AddRefToDialog() { | 409 void PrintDialogGtk2::AddRefToDialog() { |
416 AddRef(); | 410 AddRef(); |
417 } | 411 } |
418 | 412 |
419 void PrintDialogGtk2::ReleaseDialog() { | 413 void PrintDialogGtk2::ReleaseDialog() { |
420 Release(); | 414 Release(); |
421 } | 415 } |
422 | 416 |
423 void PrintDialogGtk2::OnResponse(GtkWidget* dialog, int response_id) { | 417 void PrintDialogGtk2::OnResponse(GtkWidget* dialog, int response_id) { |
424 int num_matched_handlers = g_signal_handlers_disconnect_by_func( | 418 int num_matched_handlers = g_signal_handlers_disconnect_by_func( |
425 dialog_, reinterpret_cast<gpointer>(&OnResponseThunk), this); | 419 dialog_, reinterpret_cast<gpointer>(&OnResponseThunk), this); |
426 CHECK_EQ(1, num_matched_handlers); | 420 CHECK_EQ(1, num_matched_handlers); |
427 | 421 |
428 gtk_widget_hide(dialog_); | 422 gtk_widget_hide(dialog_); |
429 | 423 |
430 switch (response_id) { | 424 switch (response_id) { |
431 case GTK_RESPONSE_OK: { | 425 case GTK_RESPONSE_OK: { |
432 if (gtk_settings_) | 426 if (gtk_settings_) |
433 g_object_unref(gtk_settings_); | 427 g_object_unref(gtk_settings_); |
434 gtk_settings_ = gtk_print_unix_dialog_get_settings( | 428 gtk_settings_ = |
435 GTK_PRINT_UNIX_DIALOG(dialog_)); | 429 gtk_print_unix_dialog_get_settings(GTK_PRINT_UNIX_DIALOG(dialog_)); |
436 | 430 |
437 if (printer_) | 431 if (printer_) |
438 g_object_unref(printer_); | 432 g_object_unref(printer_); |
439 printer_ = gtk_print_unix_dialog_get_selected_printer( | 433 printer_ = gtk_print_unix_dialog_get_selected_printer( |
440 GTK_PRINT_UNIX_DIALOG(dialog_)); | 434 GTK_PRINT_UNIX_DIALOG(dialog_)); |
441 g_object_ref(printer_); | 435 g_object_ref(printer_); |
442 | 436 |
443 if (page_setup_) | 437 if (page_setup_) |
444 g_object_unref(page_setup_); | 438 g_object_unref(page_setup_); |
445 page_setup_ = gtk_print_unix_dialog_get_page_setup( | 439 page_setup_ = |
446 GTK_PRINT_UNIX_DIALOG(dialog_)); | 440 gtk_print_unix_dialog_get_page_setup(GTK_PRINT_UNIX_DIALOG(dialog_)); |
447 g_object_ref(page_setup_); | 441 g_object_ref(page_setup_); |
448 | 442 |
449 // Handle page ranges. | 443 // Handle page ranges. |
450 PageRanges ranges_vector; | 444 PageRanges ranges_vector; |
451 gint num_ranges; | 445 gint num_ranges; |
452 bool print_selection_only = false; | 446 bool print_selection_only = false; |
453 switch (gtk_print_settings_get_print_pages(gtk_settings_)) { | 447 switch (gtk_print_settings_get_print_pages(gtk_settings_)) { |
454 case GTK_PRINT_PAGES_RANGES: { | 448 case GTK_PRINT_PAGES_RANGES: { |
455 GtkPageRange* gtk_range = | 449 GtkPageRange* gtk_range = |
456 gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges); | 450 gtk_print_settings_get_page_ranges(gtk_settings_, &num_ranges); |
(...skipping 29 matching lines...) Expand all Loading... |
486 callback_.Reset(); | 480 callback_.Reset(); |
487 return; | 481 return; |
488 } | 482 } |
489 case GTK_RESPONSE_DELETE_EVENT: // Fall through. | 483 case GTK_RESPONSE_DELETE_EVENT: // Fall through. |
490 case GTK_RESPONSE_CANCEL: { | 484 case GTK_RESPONSE_CANCEL: { |
491 callback_.Run(PrintingContextLinux::CANCEL); | 485 callback_.Run(PrintingContextLinux::CANCEL); |
492 callback_.Reset(); | 486 callback_.Reset(); |
493 return; | 487 return; |
494 } | 488 } |
495 case GTK_RESPONSE_APPLY: | 489 case GTK_RESPONSE_APPLY: |
496 default: { | 490 default: { NOTREACHED(); } |
497 NOTREACHED(); | |
498 } | |
499 } | 491 } |
500 } | 492 } |
501 | 493 |
502 | |
503 | |
504 static void OnJobCompletedThunk(GtkPrintJob* print_job, | 494 static void OnJobCompletedThunk(GtkPrintJob* print_job, |
505 gpointer user_data, | 495 gpointer user_data, |
506 #if GTK_MAJOR_VERSION == 2 | 496 #if GTK_MAJOR_VERSION == 2 |
507 GError* error | 497 GError* error |
508 #else | 498 #else |
509 const GError* error | 499 const GError* error |
510 #endif | 500 #endif |
511 ) { | 501 ) { |
512 static_cast<PrintDialogGtk2*>(user_data)->OnJobCompleted(print_job, error); | 502 static_cast<PrintDialogGtk2*>(user_data)->OnJobCompleted(print_job, error); |
513 } | 503 } |
514 void PrintDialogGtk2::SendDocumentToPrinter( | 504 void PrintDialogGtk2::SendDocumentToPrinter( |
515 const base::string16& document_name) { | 505 const base::string16& document_name) { |
516 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 506 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
517 | 507 |
518 // If |printer_| is NULL then somehow the GTK printer list changed out under | 508 // If |printer_| is NULL then somehow the GTK printer list changed out under |
519 // us. In which case, just bail out. | 509 // us. In which case, just bail out. |
520 if (!printer_) { | 510 if (!printer_) { |
521 // Matches AddRef() in PrintDocument(); | 511 // Matches AddRef() in PrintDocument(); |
522 Release(); | 512 Release(); |
523 return; | 513 return; |
524 } | 514 } |
525 | 515 |
526 // Save the settings for next time. | 516 // Save the settings for next time. |
527 g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_); | 517 g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_); |
528 | 518 |
529 GtkPrintJob* print_job = gtk_print_job_new( | 519 GtkPrintJob* print_job = |
530 base::UTF16ToUTF8(document_name).c_str(), | 520 gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_, |
531 printer_, | 521 gtk_settings_, page_setup_); |
532 gtk_settings_, | |
533 page_setup_); | |
534 gtk_print_job_set_source_file(print_job, path_to_pdf_.value().c_str(), NULL); | 522 gtk_print_job_set_source_file(print_job, path_to_pdf_.value().c_str(), NULL); |
535 gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL); | 523 gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL); |
536 } | 524 } |
537 | 525 |
538 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob* print_job, | 526 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob* print_job, |
539 const GError* error) { | 527 const GError* error) { |
540 if (error) | 528 if (error) |
541 LOG(ERROR) << "Printing failed: " << error->message; | 529 LOG(ERROR) << "Printing failed: " << error->message; |
542 if (print_job) | 530 if (print_job) |
543 g_object_unref(print_job); | 531 g_object_unref(print_job); |
(...skipping 12 matching lines...) Expand all Loading... |
556 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { | 544 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { |
557 DCHECK_EQ(libgtkui::GetAuraTransientParent(dialog_), window); | 545 DCHECK_EQ(libgtkui::GetAuraTransientParent(dialog_), window); |
558 | 546 |
559 libgtkui::ClearAuraTransientParent(dialog_); | 547 libgtkui::ClearAuraTransientParent(dialog_); |
560 window->RemoveObserver(this); | 548 window->RemoveObserver(this); |
561 if (!callback_.is_null()) { | 549 if (!callback_.is_null()) { |
562 callback_.Run(PrintingContextLinux::CANCEL); | 550 callback_.Run(PrintingContextLinux::CANCEL); |
563 callback_.Reset(); | 551 callback_.Reset(); |
564 } | 552 } |
565 } | 553 } |
OLD | NEW |