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

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: 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 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') | no next file with comments »
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..b8b01413cfcb5e8c4032ce81b75aac4c9132b129 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,53 @@ 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) {
+ 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) {
+ GtkPaperSize* first_fuzzy_match = NULL;
+ 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, false)) {
+ return gtk_paper_size;
+ }
+ if (!first_fuzzy_match && PaperSizeMatch(gtk_paper_size, media, true)) {
+ first_fuzzy_match = gtk_paper_size;
+ }
+ }
+ return first_fuzzy_match;
+}
+
class StickyPrintSettingGtk {
public:
StickyPrintSettingGtk() : last_used_settings_(gtk_print_settings_new()) {
@@ -224,6 +273,40 @@ 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);
+ 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') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698