| 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_gtk.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> |
| (...skipping 54 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 std::abs(paper_size_microns.height() - media.size_microns.height())); | 65 std::abs(paper_size_microns.height() - media.size_microns.height())); |
| 66 if (fuzzy_match) { | 66 if (fuzzy_match) { |
| 67 return diff <= kPaperSizeTresholdMicrons; | 67 return diff <= kPaperSizeTresholdMicrons; |
| 68 } | 68 } |
| 69 return !media.vendor_id.empty() && | 69 return !media.vendor_id.empty() && |
| 70 media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) && | 70 media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) && |
| 71 diff <= kPaperSizeTresholdMicrons; | 71 diff <= kPaperSizeTresholdMicrons; |
| 72 } | 72 } |
| 73 | 73 |
| 74 // Looks up a paper size matching (in terms of PaperSizeMatch) the user selected | 74 // Looks up a paper size matching (in terms of PaperSizeMatch) the user selected |
| 75 // media in the paper size list reported by GTK. Returns NULL if there's no | 75 // media in the paper size list reported by GTK. Returns nullptr if there's no |
| 76 // match found. | 76 // match found. |
| 77 GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes, | 77 GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes, |
| 78 const PrintSettings::RequestedMedia& media) { | 78 const PrintSettings::RequestedMedia& media) { |
| 79 GtkPaperSize* first_fuzzy_match = NULL; | 79 GtkPaperSize* first_fuzzy_match = nullptr; |
| 80 for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) { | 80 for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) { |
| 81 GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data); | 81 GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data); |
| 82 if (PaperSizeMatch(gtk_paper_size, media, false)) { | 82 if (PaperSizeMatch(gtk_paper_size, media, false)) { |
| 83 return gtk_paper_size; | 83 return gtk_paper_size; |
| 84 } | 84 } |
| 85 if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) { | 85 if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) { |
| 86 first_fuzzy_match = gtk_paper_size; | 86 first_fuzzy_match = gtk_paper_size; |
| 87 } | 87 } |
| 88 } | 88 } |
| 89 return first_fuzzy_match; | 89 return first_fuzzy_match; |
| (...skipping 19 matching lines...) Expand all Loading... |
| 109 | 109 |
| 110 DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk); | 110 DISALLOW_COPY_AND_ASSIGN(StickyPrintSettingGtk); |
| 111 }; | 111 }; |
| 112 | 112 |
| 113 base::LazyInstance<StickyPrintSettingGtk>::Leaky g_last_used_settings = | 113 base::LazyInstance<StickyPrintSettingGtk>::Leaky g_last_used_settings = |
| 114 LAZY_INSTANCE_INITIALIZER; | 114 LAZY_INSTANCE_INITIALIZER; |
| 115 | 115 |
| 116 // Helper class to track GTK printers. | 116 // Helper class to track GTK printers. |
| 117 class GtkPrinterList { | 117 class GtkPrinterList { |
| 118 public: | 118 public: |
| 119 GtkPrinterList() : default_printer_(NULL) { | 119 GtkPrinterList() : default_printer_(nullptr) { |
| 120 gtk_enumerate_printers(SetPrinter, this, NULL, TRUE); | 120 gtk_enumerate_printers(SetPrinter, this, nullptr, TRUE); |
| 121 } | 121 } |
| 122 | 122 |
| 123 ~GtkPrinterList() { | 123 ~GtkPrinterList() { |
| 124 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); | 124 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); |
| 125 it < printers_.end(); ++it) { | 125 it < printers_.end(); ++it) { |
| 126 g_object_unref(*it); | 126 g_object_unref(*it); |
| 127 } | 127 } |
| 128 } | 128 } |
| 129 | 129 |
| 130 // Can return NULL if there's no default printer. E.g. Printer on a laptop | 130 // Can return nullptr if there's no default printer. E.g. Printer on a laptop |
| 131 // is "home_printer", but the laptop is at work. | 131 // is "home_printer", but the laptop is at work. |
| 132 GtkPrinter* default_printer() { return default_printer_; } | 132 GtkPrinter* default_printer() { return default_printer_; } |
| 133 | 133 |
| 134 // Can return NULL if the printer cannot be found due to: | 134 // Can return nullptr if the printer cannot be found due to: |
| 135 // - Printer list out of sync with printer dialog UI. | 135 // - Printer list out of sync with printer dialog UI. |
| 136 // - Querying for non-existant printers like 'Print to PDF'. | 136 // - Querying for non-existant printers like 'Print to PDF'. |
| 137 GtkPrinter* GetPrinterWithName(const std::string& name) { | 137 GtkPrinter* GetPrinterWithName(const std::string& name) { |
| 138 if (name.empty()) | 138 if (name.empty()) |
| 139 return NULL; | 139 return nullptr; |
| 140 | 140 |
| 141 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); | 141 for (std::vector<GtkPrinter*>::iterator it = printers_.begin(); |
| 142 it < printers_.end(); ++it) { | 142 it < printers_.end(); ++it) { |
| 143 if (gtk_printer_get_name(*it) == name) { | 143 if (gtk_printer_get_name(*it) == name) { |
| 144 return *it; | 144 return *it; |
| 145 } | 145 } |
| 146 } | 146 } |
| 147 | 147 |
| 148 return NULL; | 148 return nullptr; |
| 149 } | 149 } |
| 150 | 150 |
| 151 private: | 151 private: |
| 152 // Callback function used by gtk_enumerate_printers() to get all printer. | 152 // Callback function used by gtk_enumerate_printers() to get all printer. |
| 153 static gboolean SetPrinter(GtkPrinter* printer, gpointer data) { | 153 static gboolean SetPrinter(GtkPrinter* printer, gpointer data) { |
| 154 GtkPrinterList* printer_list = reinterpret_cast<GtkPrinterList*>(data); | 154 GtkPrinterList* printer_list = reinterpret_cast<GtkPrinterList*>(data); |
| 155 if (gtk_printer_is_default(printer)) | 155 if (gtk_printer_is_default(printer)) |
| 156 printer_list->default_printer_ = printer; | 156 printer_list->default_printer_ = printer; |
| 157 | 157 |
| 158 g_object_ref(printer); | 158 g_object_ref(printer); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 169 | 169 |
| 170 // static | 170 // static |
| 171 printing::PrintDialogGtkInterface* PrintDialogGtk2::CreatePrintDialog( | 171 printing::PrintDialogGtkInterface* PrintDialogGtk2::CreatePrintDialog( |
| 172 PrintingContextLinux* context) { | 172 PrintingContextLinux* context) { |
| 173 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 173 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 174 return new PrintDialogGtk2(context); | 174 return new PrintDialogGtk2(context); |
| 175 } | 175 } |
| 176 | 176 |
| 177 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux* context) | 177 PrintDialogGtk2::PrintDialogGtk2(PrintingContextLinux* context) |
| 178 : context_(context), | 178 : context_(context), |
| 179 dialog_(NULL), | 179 dialog_(nullptr), |
| 180 gtk_settings_(NULL), | 180 gtk_settings_(nullptr), |
| 181 page_setup_(NULL), | 181 page_setup_(nullptr), |
| 182 printer_(NULL) {} | 182 printer_(nullptr) {} |
| 183 | 183 |
| 184 PrintDialogGtk2::~PrintDialogGtk2() { | 184 PrintDialogGtk2::~PrintDialogGtk2() { |
| 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 185 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 186 | 186 |
| 187 if (dialog_) { | 187 if (dialog_) { |
| 188 aura::Window* parent = libgtkui::GetAuraTransientParent(dialog_); | 188 aura::Window* parent = libgtkui::GetAuraTransientParent(dialog_); |
| 189 if (parent) { | 189 if (parent) { |
| 190 parent->RemoveObserver(this); | 190 parent->RemoveObserver(this); |
| 191 libgtkui::ClearAuraTransientParent(dialog_); | 191 libgtkui::ClearAuraTransientParent(dialog_); |
| 192 } | 192 } |
| 193 gtk_widget_destroy(dialog_); | 193 gtk_widget_destroy(dialog_); |
| 194 dialog_ = NULL; | 194 dialog_ = nullptr; |
| 195 } | 195 } |
| 196 if (gtk_settings_) { | 196 if (gtk_settings_) { |
| 197 g_object_unref(gtk_settings_); | 197 g_object_unref(gtk_settings_); |
| 198 gtk_settings_ = NULL; | 198 gtk_settings_ = nullptr; |
| 199 } | 199 } |
| 200 if (page_setup_) { | 200 if (page_setup_) { |
| 201 g_object_unref(page_setup_); | 201 g_object_unref(page_setup_); |
| 202 page_setup_ = NULL; | 202 page_setup_ = nullptr; |
| 203 } | 203 } |
| 204 if (printer_) { | 204 if (printer_) { |
| 205 g_object_unref(printer_); | 205 g_object_unref(printer_); |
| 206 printer_ = NULL; | 206 printer_ = nullptr; |
| 207 } | 207 } |
| 208 } | 208 } |
| 209 | 209 |
| 210 void PrintDialogGtk2::UseDefaultSettings() { | 210 void PrintDialogGtk2::UseDefaultSettings() { |
| 211 DCHECK(!page_setup_); | 211 DCHECK(!page_setup_); |
| 212 DCHECK(!printer_); | 212 DCHECK(!printer_); |
| 213 | 213 |
| 214 // |gtk_settings_| is a new copy. | 214 // |gtk_settings_| is a new copy. |
| 215 gtk_settings_ = | 215 gtk_settings_ = |
| 216 gtk_print_settings_copy(g_last_used_settings.Get().settings()); | 216 gtk_print_settings_copy(g_last_used_settings.Get().settings()); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 243 | 243 |
| 244 #if defined(USE_CUPS) | 244 #if defined(USE_CUPS) |
| 245 std::string color_value; | 245 std::string color_value; |
| 246 std::string color_setting_name; | 246 std::string color_setting_name; |
| 247 printing::GetColorModelForMode(settings->color(), &color_setting_name, | 247 printing::GetColorModelForMode(settings->color(), &color_setting_name, |
| 248 &color_value); | 248 &color_value); |
| 249 gtk_print_settings_set(gtk_settings_, color_setting_name.c_str(), | 249 gtk_print_settings_set(gtk_settings_, color_setting_name.c_str(), |
| 250 color_value.c_str()); | 250 color_value.c_str()); |
| 251 | 251 |
| 252 if (settings->duplex_mode() != printing::UNKNOWN_DUPLEX_MODE) { | 252 if (settings->duplex_mode() != printing::UNKNOWN_DUPLEX_MODE) { |
| 253 const char* cups_duplex_mode = NULL; | 253 const char* cups_duplex_mode = nullptr; |
| 254 switch (settings->duplex_mode()) { | 254 switch (settings->duplex_mode()) { |
| 255 case printing::LONG_EDGE: | 255 case printing::LONG_EDGE: |
| 256 cups_duplex_mode = kDuplexNoTumble; | 256 cups_duplex_mode = kDuplexNoTumble; |
| 257 break; | 257 break; |
| 258 case printing::SHORT_EDGE: | 258 case printing::SHORT_EDGE: |
| 259 cups_duplex_mode = kDuplexTumble; | 259 cups_duplex_mode = kDuplexTumble; |
| 260 break; | 260 break; |
| 261 case printing::SIMPLEX: | 261 case printing::SIMPLEX: |
| 262 cups_duplex_mode = kDuplexNone; | 262 cups_duplex_mode = kDuplexNone; |
| 263 break; | 263 break; |
| (...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 295 requested_media.size_microns.height() / kMicronsInMm, | 295 requested_media.size_microns.height() / kMicronsInMm, |
| 296 GTK_UNIT_MM); | 296 GTK_UNIT_MM); |
| 297 gtk_page_setup_set_paper_size(page_setup_, custom_size); | 297 gtk_page_setup_set_paper_size(page_setup_, custom_size); |
| 298 gtk_paper_size_free(custom_size); | 298 gtk_paper_size_free(custom_size); |
| 299 } | 299 } |
| 300 #if GTK_CHECK_VERSION(2, 28, 0) | 300 #if GTK_CHECK_VERSION(2, 28, 0) |
| 301 g_list_free_full(gtk_paper_sizes, | 301 g_list_free_full(gtk_paper_sizes, |
| 302 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free)); | 302 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free)); |
| 303 #else | 303 #else |
| 304 g_list_foreach(gtk_paper_sizes, | 304 g_list_foreach(gtk_paper_sizes, |
| 305 reinterpret_cast<GFunc>(gtk_paper_size_free), NULL); | 305 reinterpret_cast<GFunc>(gtk_paper_size_free), nullptr); |
| 306 g_list_free(gtk_paper_sizes); | 306 g_list_free(gtk_paper_sizes); |
| 307 #endif | 307 #endif |
| 308 } | 308 } |
| 309 } else { | 309 } else { |
| 310 VLOG(1) << "Using default paper size"; | 310 VLOG(1) << "Using default paper size"; |
| 311 } | 311 } |
| 312 } | 312 } |
| 313 | 313 |
| 314 gtk_print_settings_set_orientation( | 314 gtk_print_settings_set_orientation( |
| 315 gtk_settings_, settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE | 315 gtk_settings_, settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE |
| 316 : GTK_PAGE_ORIENTATION_PORTRAIT); | 316 : GTK_PAGE_ORIENTATION_PORTRAIT); |
| 317 | 317 |
| 318 InitPrintSettings(settings); | 318 InitPrintSettings(settings); |
| 319 return true; | 319 return true; |
| 320 } | 320 } |
| 321 | 321 |
| 322 void PrintDialogGtk2::ShowDialog( | 322 void PrintDialogGtk2::ShowDialog( |
| 323 gfx::NativeView parent_view, | 323 gfx::NativeView parent_view, |
| 324 bool has_selection, | 324 bool has_selection, |
| 325 const PrintingContextLinux::PrintSettingsCallback& callback) { | 325 const PrintingContextLinux::PrintSettingsCallback& callback) { |
| 326 callback_ = callback; | 326 callback_ = callback; |
| 327 DCHECK(!callback_.is_null()); | 327 DCHECK(!callback_.is_null()); |
| 328 | 328 |
| 329 dialog_ = gtk_print_unix_dialog_new(NULL, NULL); | 329 dialog_ = gtk_print_unix_dialog_new(nullptr, nullptr); |
| 330 libgtkui::SetGtkTransientForAura(dialog_, parent_view); | 330 libgtkui::SetGtkTransientForAura(dialog_, parent_view); |
| 331 if (parent_view) | 331 if (parent_view) |
| 332 parent_view->AddObserver(this); | 332 parent_view->AddObserver(this); |
| 333 g_signal_connect(dialog_, "delete-event", | 333 g_signal_connect(dialog_, "delete-event", |
| 334 G_CALLBACK(gtk_widget_hide_on_delete), NULL); | 334 G_CALLBACK(gtk_widget_hide_on_delete), nullptr); |
| 335 | 335 |
| 336 // Handle the case when the existing |gtk_settings_| has "selection" selected | 336 // Handle the case when the existing |gtk_settings_| has "selection" selected |
| 337 // as the page range, but |has_selection| is false. | 337 // as the page range, but |has_selection| is false. |
| 338 if (!has_selection) { | 338 if (!has_selection) { |
| 339 GtkPrintPages range = gtk_print_settings_get_print_pages(gtk_settings_); | 339 GtkPrintPages range = gtk_print_settings_get_print_pages(gtk_settings_); |
| 340 if (range == GTK_PRINT_PAGES_SELECTION) | 340 if (range == GTK_PRINT_PAGES_SELECTION) |
| 341 gtk_print_settings_set_print_pages(gtk_settings_, GTK_PRINT_PAGES_ALL); | 341 gtk_print_settings_set_print_pages(gtk_settings_, GTK_PRINT_PAGES_ALL); |
| 342 } | 342 } |
| 343 | 343 |
| 344 // Set modal so user cannot focus the same tab and press print again. | 344 // Set modal so user cannot focus the same tab and press print again. |
| (...skipping 152 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 497 #else | 497 #else |
| 498 const GError* error | 498 const GError* error |
| 499 #endif | 499 #endif |
| 500 ) { | 500 ) { |
| 501 static_cast<PrintDialogGtk2*>(user_data)->OnJobCompleted(print_job, error); | 501 static_cast<PrintDialogGtk2*>(user_data)->OnJobCompleted(print_job, error); |
| 502 } | 502 } |
| 503 void PrintDialogGtk2::SendDocumentToPrinter( | 503 void PrintDialogGtk2::SendDocumentToPrinter( |
| 504 const base::string16& document_name) { | 504 const base::string16& document_name) { |
| 505 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 505 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 506 | 506 |
| 507 // If |printer_| is NULL then somehow the GTK printer list changed out under | 507 // If |printer_| is nullptr then somehow the GTK printer list changed out |
| 508 // us. In which case, just bail out. | 508 // under us. In which case, just bail out. |
| 509 if (!printer_) { | 509 if (!printer_) { |
| 510 // Matches AddRef() in PrintDocument(); | 510 // Matches AddRef() in PrintDocument(); |
| 511 Release(); | 511 Release(); |
| 512 return; | 512 return; |
| 513 } | 513 } |
| 514 | 514 |
| 515 // Save the settings for next time. | 515 // Save the settings for next time. |
| 516 g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_); | 516 g_last_used_settings.Get().SetLastUsedSettings(gtk_settings_); |
| 517 | 517 |
| 518 GtkPrintJob* print_job = | 518 GtkPrintJob* print_job = |
| 519 gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_, | 519 gtk_print_job_new(base::UTF16ToUTF8(document_name).c_str(), printer_, |
| 520 gtk_settings_, page_setup_); | 520 gtk_settings_, page_setup_); |
| 521 gtk_print_job_set_source_file(print_job, path_to_pdf_.value().c_str(), NULL); | 521 gtk_print_job_set_source_file(print_job, path_to_pdf_.value().c_str(), |
| 522 gtk_print_job_send(print_job, OnJobCompletedThunk, this, NULL); | 522 nullptr); |
| 523 gtk_print_job_send(print_job, OnJobCompletedThunk, this, nullptr); |
| 523 } | 524 } |
| 524 | 525 |
| 525 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob* print_job, | 526 void PrintDialogGtk2::OnJobCompleted(GtkPrintJob* print_job, |
| 526 const GError* error) { | 527 const GError* error) { |
| 527 if (error) | 528 if (error) |
| 528 LOG(ERROR) << "Printing failed: " << error->message; | 529 LOG(ERROR) << "Printing failed: " << error->message; |
| 529 if (print_job) | 530 if (print_job) |
| 530 g_object_unref(print_job); | 531 g_object_unref(print_job); |
| 531 BrowserThread::PostTask( | 532 BrowserThread::PostTask( |
| 532 BrowserThread::FILE, FROM_HERE, | 533 BrowserThread::FILE, FROM_HERE, |
| (...skipping 10 matching lines...) Expand all Loading... |
| 543 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { | 544 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { |
| 544 DCHECK_EQ(libgtkui::GetAuraTransientParent(dialog_), window); | 545 DCHECK_EQ(libgtkui::GetAuraTransientParent(dialog_), window); |
| 545 | 546 |
| 546 libgtkui::ClearAuraTransientParent(dialog_); | 547 libgtkui::ClearAuraTransientParent(dialog_); |
| 547 window->RemoveObserver(this); | 548 window->RemoveObserver(this); |
| 548 if (!callback_.is_null()) { | 549 if (!callback_.is_null()) { |
| 549 callback_.Run(PrintingContextLinux::CANCEL); | 550 callback_.Run(PrintingContextLinux::CANCEL); |
| 550 callback_.Reset(); | 551 callback_.Reset(); |
| 551 } | 552 } |
| 552 } | 553 } |
| OLD | NEW |