Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(44)

Side by Side Diff: chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc

Issue 334763002: Add paper size reporting for CUPS printers (to let user to select one in the Print Preview). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Add unit test for PPD page size extraction, log error on PPD file opening, address other cl comment… Created 6 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | chrome/common/chrome_utility_messages.h » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/libgtk2ui/print_dialog_gtk2.h" 5 #include "chrome/browser/ui/libgtk2ui/print_dialog_gtk2.h"
6 6
7 #include <gtk/gtkunixprint.h> 7 #include <gtk/gtkunixprint.h>
8 8
9 #include <algorithm>
10 #include <cmath>
9 #include <string> 11 #include <string>
10 #include <vector> 12 #include <vector>
11 13
12 #include "base/bind.h" 14 #include "base/bind.h"
13 #include "base/file_util.h" 15 #include "base/file_util.h"
14 #include "base/files/file_util_proxy.h" 16 #include "base/files/file_util_proxy.h"
15 #include "base/lazy_instance.h" 17 #include "base/lazy_instance.h"
16 #include "base/logging.h" 18 #include "base/logging.h"
17 #include "base/message_loop/message_loop_proxy.h" 19 #include "base/message_loop/message_loop_proxy.h"
18 #include "base/strings/utf_string_conversions.h" 20 #include "base/strings/utf_string_conversions.h"
(...skipping 10 matching lines...) Expand all
29 using printing::PrintSettings; 31 using printing::PrintSettings;
30 32
31 namespace { 33 namespace {
32 34
33 // CUPS Duplex attribute and values. 35 // CUPS Duplex attribute and values.
34 const char kCUPSDuplex[] = "cups-Duplex"; 36 const char kCUPSDuplex[] = "cups-Duplex";
35 const char kDuplexNone[] = "None"; 37 const char kDuplexNone[] = "None";
36 const char kDuplexTumble[] = "DuplexTumble"; 38 const char kDuplexTumble[] = "DuplexTumble";
37 const char kDuplexNoTumble[] = "DuplexNoTumble"; 39 const char kDuplexNoTumble[] = "DuplexNoTumble";
38 40
41 int kPaperSizeTresholdMicrons = 100;
42 int kMicronsInMm = 1000;
43
44 // Checks whether gtk_paper_size can be used to represent user selected media.
45 // In fuzzy match mode checks that paper sizes are "close enough" (less than
46 // 1mm difference). In the exact mode, looks for the paper with the same PPD
47 // name and "close enough" size.
48 bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
49 const PrintSettings::RequestedMedia& media,
50 bool fuzzy_match) {
51 if (!gtk_paper_size) {
52 return false;
53 }
54 gfx::Size paper_size_microns(
55 static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) *
56 kMicronsInMm + 0.5),
57 static_cast<int>(gtk_paper_size_get_height(gtk_paper_size, GTK_UNIT_MM) *
58 kMicronsInMm + 0.5));
59 int diff = std::max(
60 std::abs(paper_size_microns.width() - media.size_microns.width()),
61 std::abs(paper_size_microns.height() - media.size_microns.height()));
62 if (fuzzy_match) {
63 return diff <= kPaperSizeTresholdMicrons;
64 }
65 return !media.vendor_id.empty() &&
66 media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) &&
67 diff <= kPaperSizeTresholdMicrons;
68 }
69
70 // Looks up a paper size matching (in terms of PaperSizeMatch) the user selected
71 // media in the paper size list reported by GTK. Returns NULL if there's no
72 // match found.
73 GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes,
74 const PrintSettings::RequestedMedia& media) {
75 GtkPaperSize* first_fuzzy_match = NULL;
76 for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) {
77 GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data);
78 if (PaperSizeMatch(gtk_paper_size, media, false)) {
79 return gtk_paper_size;
80 }
81 if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) {
82 first_fuzzy_match = gtk_paper_size;
83 }
84 }
85 return first_fuzzy_match;
86 }
87
39 class StickyPrintSettingGtk { 88 class StickyPrintSettingGtk {
40 public: 89 public:
41 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) { 90 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {
42 } 91 }
43 ~StickyPrintSettingGtk() { 92 ~StickyPrintSettingGtk() {
44 NOTREACHED(); // Intended to be used with a Leaky LazyInstance. 93 NOTREACHED(); // Intended to be used with a Leaky LazyInstance.
45 } 94 }
46 95
47 GtkPrintSettings* settings() { 96 GtkPrintSettings* settings() {
48 return last_used_settings_; 97 return last_used_settings_;
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
217 default: // UNKNOWN_DUPLEX_MODE 266 default: // UNKNOWN_DUPLEX_MODE
218 NOTREACHED(); 267 NOTREACHED();
219 break; 268 break;
220 } 269 }
221 gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode); 270 gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode);
222 } 271 }
223 #endif 272 #endif
224 if (!page_setup_) 273 if (!page_setup_)
225 page_setup_ = gtk_page_setup_new(); 274 page_setup_ = gtk_page_setup_new();
226 275
276 if (page_setup_ && !settings->requested_media().IsDefault()) {
277 const PrintSettings::RequestedMedia& requested_media =
278 settings->requested_media();
279 GtkPaperSize* gtk_current_paper_size =
280 gtk_page_setup_get_paper_size(page_setup_);
281 if (!PaperSizeMatch(gtk_current_paper_size, requested_media,
282 true /*fuzzy_match*/)) {
283 GList* gtk_paper_sizes =
284 gtk_paper_size_get_paper_sizes(false /*include_custom*/);
285 if (gtk_paper_sizes) {
286 GtkPaperSize* matching_gtk_paper_size =
287 FindPaperSizeMatch(gtk_paper_sizes, requested_media);
288 if (matching_gtk_paper_size) {
289 VLOG(1) << "Using listed paper size";
290 gtk_page_setup_set_paper_size(page_setup_, matching_gtk_paper_size);
291 } else {
292 VLOG(1) << "Using custom paper size";
293 GtkPaperSize* custom_size = gtk_paper_size_new_custom(
294 requested_media.vendor_id.c_str(),
295 requested_media.vendor_id.c_str(),
296 requested_media.size_microns.width() / kMicronsInMm,
297 requested_media.size_microns.height() / kMicronsInMm,
298 GTK_UNIT_MM);
299 gtk_page_setup_set_paper_size(page_setup_, custom_size);
300 gtk_paper_size_free(custom_size);
301 }
302 g_list_free_full(gtk_paper_sizes,
303 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free));
304 }
305 } else {
306 VLOG(1) << "Using default paper size";
307 }
308 }
309
227 gtk_print_settings_set_orientation( 310 gtk_print_settings_set_orientation(
228 gtk_settings_, 311 gtk_settings_,
229 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE : 312 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE :
230 GTK_PAGE_ORIENTATION_PORTRAIT); 313 GTK_PAGE_ORIENTATION_PORTRAIT);
231 314
232 InitPrintSettings(settings); 315 InitPrintSettings(settings);
233 return true; 316 return true;
234 } 317 }
235 318
236 void PrintDialogGtk2::ShowDialog( 319 void PrintDialogGtk2::ShowDialog(
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after
442 context_->InitWithSettings(*settings); 525 context_->InitWithSettings(*settings);
443 } 526 }
444 527
445 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { 528 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) {
446 DCHECK_EQ(libgtk2ui::GetAuraTransientParent(dialog_), window); 529 DCHECK_EQ(libgtk2ui::GetAuraTransientParent(dialog_), window);
447 530
448 libgtk2ui::ClearAuraTransientParent(dialog_); 531 libgtk2ui::ClearAuraTransientParent(dialog_);
449 window->RemoveObserver(this); 532 window->RemoveObserver(this);
450 Release(); 533 Release();
451 } 534 }
OLDNEW
« no previous file with comments | « no previous file | chrome/common/chrome_utility_messages.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698