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

Unified 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: 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | chrome/common/chrome_utility_messages.h » ('j') | printing/backend/cups_helper.cc » ('J')
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc
diff --git a/chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc b/chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc
index 971eecc633679eae293f341f88029a9e1dafad3a..8219a1bca370b9201a2a41800971badabbdd9804 100644
--- a/chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc
+++ b/chrome/browser/ui/libgtk2ui/print_dialog_gtk2.cc
@@ -6,6 +6,8 @@
#include <gtk/gtkunixprint.h>
+#include <algorithm>
+#include <cmath>
#include <string>
#include <vector>
@@ -36,6 +38,50 @@ const char kDuplexNone[] = "None";
const char kDuplexTumble[] = "DuplexTumble";
const char kDuplexNoTumble[] = "DuplexNoTumble";
+int kPaperSizeTresholdMicrons = 100;
+int kMicronsInMm = 1000;
+
+// Checks whether gtk_paper_size can be used to represent user selected media.
+// In fuzzy match mode checks that paper sizes are "close enough" (less than
+// 1mm difference). In the exact mode, looks for the paper with the same PPD
+// name and "close enough" size.
+bool PaperSizeMatch(GtkPaperSize* gtk_paper_size,
+ const PrintSettings::RequestedMedia& media,
+ 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
+ if (!gtk_paper_size) {
+ return false;
+ }
+ gfx::Size paper_size_microns(
+ static_cast<int>(gtk_paper_size_get_width(gtk_paper_size, GTK_UNIT_MM) *
+ kMicronsInMm + 0.5),
+ static_cast<int>(gtk_paper_size_get_height(gtk_paper_size, GTK_UNIT_MM) *
+ kMicronsInMm + 0.5));
+ int diff = std::max(
+ std::abs(paper_size_microns.width() - media.size_microns.width()),
+ std::abs(paper_size_microns.height() - media.size_microns.height()));
+ if (fuzzy_match) {
+ return diff <= kPaperSizeTresholdMicrons;
+ }
+ return !media.vendor_id.empty() &&
+ media.vendor_id == gtk_paper_size_get_ppd_name(gtk_paper_size) &&
+ diff <= kPaperSizeTresholdMicrons;
+}
+
+// Looks up a paper size matching (in terms of PaperSizeMatch) the user selected
+// media in the paper size list reported by GTK. Returns NULL if there's no
+// match found.
+GtkPaperSize* FindPaperSizeMatch(GList* gtk_paper_sizes,
+ const PrintSettings::RequestedMedia& media,
+ bool fuzzy_match) {
+ for (GList* p = gtk_paper_sizes; p && p->data; p = g_list_next(p)) {
+ GtkPaperSize* gtk_paper_size = static_cast<GtkPaperSize*>(p->data);
+ 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.
+ return gtk_paper_size;
+ }
+ }
+ return NULL;
+}
+
class StickyPrintSettingGtk {
public:
StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {
@@ -224,6 +270,44 @@ bool PrintDialogGtk2::UpdateSettings(printing::PrintSettings* settings) {
if (!page_setup_)
page_setup_ = gtk_page_setup_new();
+ if (page_setup_ && !settings->requested_media().IsDefault()) {
+ const PrintSettings::RequestedMedia& requested_media =
+ settings->requested_media();
+ GtkPaperSize* gtk_current_paper_size =
+ gtk_page_setup_get_paper_size(page_setup_);
+ if (!PaperSizeMatch(gtk_current_paper_size, requested_media,
+ true /*fuzzy_match*/)) {
+ GList* gtk_paper_sizes =
+ gtk_paper_size_get_paper_sizes(false /*include_custom*/);
+ if (gtk_paper_sizes) {
+ GtkPaperSize* matching_gtk_paper_size = FindPaperSizeMatch(
+ gtk_paper_sizes, requested_media, false /*fuzzy_match*/);
+ if (!matching_gtk_paper_size) {
+ matching_gtk_paper_size = FindPaperSizeMatch(
+ gtk_paper_sizes, requested_media, true /*fuzzy_match*/);
+ }
+ if (matching_gtk_paper_size) {
+ VLOG(1) << "Using listed paper size";
+ gtk_page_setup_set_paper_size(page_setup_, matching_gtk_paper_size);
+ } else {
+ VLOG(1) << "Using custom paper size";
+ GtkPaperSize* custom_size = gtk_paper_size_new_custom(
+ requested_media.vendor_id.c_str(),
+ requested_media.vendor_id.c_str(),
+ requested_media.size_microns.width() / kMicronsInMm,
+ requested_media.size_microns.height() / kMicronsInMm,
+ GTK_UNIT_MM);
+ gtk_page_setup_set_paper_size(page_setup_, custom_size);
+ gtk_paper_size_free(custom_size);
+ }
+ g_list_free_full(gtk_paper_sizes,
+ reinterpret_cast<GDestroyNotify>(gtk_paper_size_free));
+ }
+ } else {
+ VLOG(1) << "Using default paper size";
+ }
+ }
+
gtk_print_settings_set_orientation(
gtk_settings_,
settings->landscape() ? GTK_PAGE_ORIENTATION_LANDSCAPE :
« no previous file with comments | « no previous file | chrome/common/chrome_utility_messages.h » ('j') | printing/backend/cups_helper.cc » ('J')

Powered by Google App Engine
This is Rietveld 408576698