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/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 Loading... | |
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) { | |
Vitaly Buka (NO REVIEWS)
2014/06/12 19:32:51
fuzzy_match -> size_only_match?
Aleksey Shlyapnikov
2014/06/12 21:21:12
I think fuzzy_match conveys the purpose better.
Vitaly Buka (NO REVIEWS)
2014/06/12 21:34:07
fuzzy is good when you can't explain logic.
Here i
Aleksey Shlyapnikov
2014/06/12 22:56:33
Here it also means "ignore PPD name".
Vitaly Buka (NO REVIEWS)
2014/06/12 23:06:04
Thanks.
On 2014/06/12 22:56:33, Aleksey Shlyapnik
| |
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 bool fuzzy_match) { | |
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, fuzzy_match)) { | |
Vitaly Buka (NO REVIEWS)
2014/06/12 19:32:51
No need for arg bool fuzzy_match
GtkPaperSize* fu
Aleksey Shlyapnikov
2014/06/12 21:21:12
It changes the semantics. My idea is to traverse t
Vitaly Buka (NO REVIEWS)
2014/06/12 21:34:08
Semantic exactly the same.
It just remember fuzzy
Aleksey Shlyapnikov
2014/06/12 22:56:33
Addressed your concern.
| |
79 return gtk_paper_size; | |
80 } | |
81 } | |
82 return NULL; | |
83 } | |
84 | |
39 class StickyPrintSettingGtk { | 85 class StickyPrintSettingGtk { |
40 public: | 86 public: |
41 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) { | 87 StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) { |
42 } | 88 } |
43 ~StickyPrintSettingGtk() { | 89 ~StickyPrintSettingGtk() { |
44 NOTREACHED(); // Intended to be used with a Leaky LazyInstance. | 90 NOTREACHED(); // Intended to be used with a Leaky LazyInstance. |
45 } | 91 } |
46 | 92 |
47 GtkPrintSettings* settings() { | 93 GtkPrintSettings* settings() { |
48 return last_used_settings_; | 94 return last_used_settings_; |
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
217 default: // UNKNOWN_DUPLEX_MODE | 263 default: // UNKNOWN_DUPLEX_MODE |
218 NOTREACHED(); | 264 NOTREACHED(); |
219 break; | 265 break; |
220 } | 266 } |
221 gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode); | 267 gtk_print_settings_set(gtk_settings_, kCUPSDuplex, cups_duplex_mode); |
222 } | 268 } |
223 #endif | 269 #endif |
224 if (!page_setup_) | 270 if (!page_setup_) |
225 page_setup_ = gtk_page_setup_new(); | 271 page_setup_ = gtk_page_setup_new(); |
226 | 272 |
273 if (page_setup_ && !settings->requested_media().IsDefault()) { | |
274 const PrintSettings::RequestedMedia& requested_media = | |
275 settings->requested_media(); | |
276 GtkPaperSize* gtk_current_paper_size = | |
277 gtk_page_setup_get_paper_size(page_setup_); | |
278 if (!PaperSizeMatch(gtk_current_paper_size, requested_media, | |
279 true /*fuzzy_match*/)) { | |
280 GList* gtk_paper_sizes = | |
281 gtk_paper_size_get_paper_sizes(false /*include_custom*/); | |
282 if (gtk_paper_sizes) { | |
283 GtkPaperSize* matching_gtk_paper_size = FindPaperSizeMatch( | |
284 gtk_paper_sizes, requested_media, false /*fuzzy_match*/); | |
285 if (!matching_gtk_paper_size) { | |
286 matching_gtk_paper_size = FindPaperSizeMatch( | |
287 gtk_paper_sizes, requested_media, true /*fuzzy_match*/); | |
288 } | |
289 if (matching_gtk_paper_size) { | |
290 VLOG(1) << "Using listed paper size"; | |
291 gtk_page_setup_set_paper_size(page_setup_, matching_gtk_paper_size); | |
292 } else { | |
293 VLOG(1) << "Using custom paper size"; | |
294 GtkPaperSize* custom_size = gtk_paper_size_new_custom( | |
295 requested_media.vendor_id.c_str(), | |
296 requested_media.vendor_id.c_str(), | |
297 requested_media.size_microns.width() / kMicronsInMm, | |
298 requested_media.size_microns.height() / kMicronsInMm, | |
299 GTK_UNIT_MM); | |
300 gtk_page_setup_set_paper_size(page_setup_, custom_size); | |
301 gtk_paper_size_free(custom_size); | |
302 } | |
303 g_list_free_full(gtk_paper_sizes, | |
304 reinterpret_cast<GDestroyNotify>(gtk_paper_size_free)); | |
305 } | |
306 } else { | |
307 VLOG(1) << "Using default paper size"; | |
308 } | |
309 } | |
310 | |
227 gtk_print_settings_set_orientation( | 311 gtk_print_settings_set_orientation( |
228 gtk_settings_, | 312 gtk_settings_, |
229 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE : | 313 settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE : |
230 GTK_PAGE_ORIENTATION_PORTRAIT); | 314 GTK_PAGE_ORIENTATION_PORTRAIT); |
231 | 315 |
232 InitPrintSettings(settings); | 316 InitPrintSettings(settings); |
233 return true; | 317 return true; |
234 } | 318 } |
235 | 319 |
236 void PrintDialogGtk2::ShowDialog( | 320 void PrintDialogGtk2::ShowDialog( |
(...skipping 205 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
442 context_->InitWithSettings(*settings); | 526 context_->InitWithSettings(*settings); |
443 } | 527 } |
444 | 528 |
445 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { | 529 void PrintDialogGtk2::OnWindowDestroying(aura::Window* window) { |
446 DCHECK_EQ(libgtk2ui::GetAuraTransientParent(dialog_), window); | 530 DCHECK_EQ(libgtk2ui::GetAuraTransientParent(dialog_), window); |
447 | 531 |
448 libgtk2ui::ClearAuraTransientParent(dialog_); | 532 libgtk2ui::ClearAuraTransientParent(dialog_); |
449 window->RemoveObserver(this); | 533 window->RemoveObserver(this); |
450 Release(); | 534 Release(); |
451 } | 535 } |
OLD | NEW |