diff options
author | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-29 03:16:37 +0000 |
---|---|---|
committer | vitalybuka@chromium.org <vitalybuka@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-10-29 03:16:37 +0000 |
commit | e5324b55e4204451d02d9323a6ea013955b82bc0 (patch) | |
tree | 369de1fdbf16aee58036f9a0a95c27bb9a0aa1e8 /printing | |
parent | b7898896f26bc424e1e11d7533f866c2872a8c4f (diff) | |
download | chromium_src-e5324b55e4204451d02d9323a6ea013955b82bc0.zip chromium_src-e5324b55e4204451d02d9323a6ea013955b82bc0.tar.gz chromium_src-e5324b55e4204451d02d9323a6ea013955b82bc0.tar.bz2 |
De-duplicate job_settings parsing code.
Proper set/get methods in PrintSettings.
Objective of this re-factoring is to simplify processing of
"print_to_pdf || is_cloud_dialog || print_to_cloud" case.
Now all platforms for this case do almost the same by in different way: calculate and set printing area.
In the next CL I'll move that code into common PrintingContext::UpdatePrintSettings and will ask platform specific implementation for desired page size only.
BUG=305731
Review URL: https://codereview.chromium.org/47823002
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@231501 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'printing')
26 files changed, 304 insertions, 307 deletions
diff --git a/printing/page_number.cc b/printing/page_number.cc index 8e92870..db3861d 100644 --- a/printing/page_number.cc +++ b/printing/page_number.cc @@ -31,7 +31,7 @@ void PageNumber::operator=(const PageNumber& other) { void PageNumber::Init(const PrintSettings& settings, int document_page_count) { DCHECK(document_page_count); - ranges_ = settings.ranges.empty() ? NULL : &settings.ranges; + ranges_ = settings.ranges().empty() ? NULL : &settings.ranges(); document_page_count_ = document_page_count; if (ranges_) { page_range_index_ = 0; diff --git a/printing/print_dialog_gtk_interface.h b/printing/print_dialog_gtk_interface.h index b361837..3b3fcdd 100644 --- a/printing/print_dialog_gtk_interface.h +++ b/printing/print_dialog_gtk_interface.h @@ -22,12 +22,12 @@ class PrintDialogGtkInterface { // Tell the dialog to use the default print setting. virtual void UseDefaultSettings() = 0; - // Update the dialog to use |job_settings| and |ranges|, where |job_settings| - // is a dictionary of settings with possible keys from - // printing/print_job_constants.h. Only used when printing without the system - // print dialog. E.g. for Print Preview. Returns false on error. - virtual bool UpdateSettings(const base::DictionaryValue& job_settings, - const PageRanges& ranges, + // Updates the dialog to use |settings|. Only used when printing without the + // system print dialog. E.g. for Print Preview. Returns false on error. + // |target_is_pdf| is true if implementation needs to generate PDF without + // actual printing. In this case implementation may avoid setup of native + // print system. Ex. "save to pdf" or Cloud Print. + virtual bool UpdateSettings(bool target_is_pdf, PrintSettings* settings) = 0; // Shows the dialog and handles the response with |callback|. Only used when diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc index f659aed..b2fefa8 100644 --- a/printing/print_job_constants.cc +++ b/printing/print_job_constants.cc @@ -129,9 +129,8 @@ const char kSettingShouldPrintSelectionOnly[] = "shouldPrintSelectionOnly"; const int FIRST_PAGE_INDEX = 0; const int COMPLETE_PREVIEW_DOCUMENT_INDEX = -1; -#if defined(OS_MACOSX) +// Whether to show PDF in view provided by OS. Implemented for MacOS only. const char kSettingOpenPDFInPreview[] = "OpenPDFInPreview"; -#endif #if defined (USE_CUPS) const char kBlack[] = "Black"; diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h index c038eb0..526f8ee 100644 --- a/printing/print_job_constants.h +++ b/printing/print_job_constants.h @@ -53,10 +53,7 @@ PRINTING_EXPORT extern const char kSettingShouldPrintSelectionOnly[]; PRINTING_EXPORT extern const int FIRST_PAGE_INDEX; PRINTING_EXPORT extern const int COMPLETE_PREVIEW_DOCUMENT_INDEX; - -#if defined(OS_MACOSX) PRINTING_EXPORT extern const char kSettingOpenPDFInPreview[]; -#endif // defined(OS_MACOSX) #if defined (USE_CUPS) // Printer color models diff --git a/printing/print_settings.cc b/printing/print_settings.cc index 1835b85..9e2a251 100644 --- a/printing/print_settings.cc +++ b/printing/print_settings.cc @@ -110,52 +110,49 @@ bool IsColorModelSelected(int color_mode) { // Global SequenceNumber used for generating unique cookie values. static base::StaticAtomicSequenceNumber cookie_seq; -PrintSettings::PrintSettings() - : min_shrink(1.25), - max_shrink(2.0), - desired_dpi(72), - selection_only(false), - margin_type(DEFAULT_MARGINS), - display_header_footer(false), - should_print_backgrounds(false), - dpi_(0), - landscape_(false), - supports_alpha_blend_(true) { +PrintSettings::PrintSettings() { + Clear(); } PrintSettings::~PrintSettings() { } void PrintSettings::Clear() { - ranges.clear(); - min_shrink = 1.25; - max_shrink = 2.; - desired_dpi = 72; - selection_only = false; - title = base::string16(); - url = base::string16(); - display_header_footer = false; + ranges_.clear(); + margin_type_ = DEFAULT_MARGINS; + min_shrink_ = 1.25; + max_shrink_ = 2.; + desired_dpi_ = 72; + selection_only_ = false; + title_ = base::string16(); + url_ = base::string16(); + display_header_footer_ = false; device_name_.clear(); page_setup_device_units_.Clear(); dpi_ = 0; landscape_ = false; supports_alpha_blend_ = true; - should_print_backgrounds = false; + should_print_backgrounds_ = false; + collate_ = false; + color_ = UNKNOWN_COLOR_MODEL; + copies_ = 0; + duplex_mode_ = UNKNOWN_DUPLEX_MODE; } void PrintSettings::SetPrinterPrintableArea( gfx::Size const& physical_size_device_units, gfx::Rect const& printable_area_device_units, - int units_per_inch) { + int units_per_inch, + bool landscape_needs_flip) { int header_footer_text_height = 0; - if (display_header_footer) { + if (display_header_footer_) { // Hard-code text_height = 0.5cm = ~1/5 of inch. header_footer_text_height = ConvertUnit(kSettingHeaderFooterInterstice, kPointsPerInch, units_per_inch); } PageMargins margins; - switch (margin_type) { + switch (margin_type_) { case DEFAULT_MARGINS: { // Default margins 1.0cm = ~2/5 of an inch. int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, @@ -204,7 +201,7 @@ void PrintSettings::SetPrinterPrintableArea( } } - if (margin_type == DEFAULT_MARGINS || margin_type == PRINTABLE_AREA_MARGINS) + if (margin_type_ == DEFAULT_MARGINS || margin_type_ == PRINTABLE_AREA_MARGINS) page_setup_device_units_.SetRequestedMargins(margins); else page_setup_device_units_.ForceRequestedMargins(margins); @@ -212,27 +209,14 @@ void PrintSettings::SetPrinterPrintableArea( page_setup_device_units_.Init(physical_size_device_units, printable_area_device_units, header_footer_text_height); + if (landscape_ && landscape_needs_flip) + page_setup_device_units_.FlipOrientation(); } void PrintSettings::SetCustomMargins( const PageMargins& requested_margins_in_points) { requested_custom_margins_in_points_ = requested_margins_in_points; - margin_type = CUSTOM_MARGINS; -} - -bool PrintSettings::Equals(const PrintSettings& rhs) const { - // Do not test the display device name (printer_name_) for equality since it - // may sometimes be chopped off at 30 chars. As long as device_name is the - // same, that's fine. - return ranges == rhs.ranges && - min_shrink == rhs.min_shrink && - max_shrink == rhs.max_shrink && - desired_dpi == rhs.desired_dpi && - device_name_ == rhs.device_name_ && - page_setup_device_units_.Equals(rhs.page_setup_device_units_) && - dpi_ == rhs.dpi_ && - landscape_ == rhs.landscape_ && - should_print_backgrounds == rhs.should_print_backgrounds; + margin_type_ = CUSTOM_MARGINS; } int PrintSettings::NewCookie() { diff --git a/printing/print_settings.h b/printing/print_settings.h index 7cff3bd..b80d354 100644 --- a/printing/print_settings.h +++ b/printing/print_settings.h @@ -35,32 +35,38 @@ class PRINTING_EXPORT PrintSettings { // Reinitialize the settings to the default values. void Clear(); - // Set printer printable area in in device units. - void SetPrinterPrintableArea(gfx::Size const& physical_size_device_units, - gfx::Rect const& printable_area_device_units, - int units_per_inch); - void SetCustomMargins(const PageMargins& requested_margins_in_points); + void set_margin_type(MarginType margin_type) { margin_type_ = margin_type; } + MarginType margin_type() const { return margin_type_; } - // Equality operator. - // NOTE: printer_name is NOT tested for equality since it doesn't affect the - // output. - bool Equals(const PrintSettings& rhs) const; + // Updates the orientation and flip the page if needed. + void SetOrientation(bool landscape); + bool landscape() const { return landscape_; } + + // Set printer printable area in in device units. + // Some platforms already provide flipped area. Set |landscape_needs_flip| + // to false on those platforms to avoid double flipping. + void SetPrinterPrintableArea(const gfx::Size& physical_size_device_units, + const gfx::Rect& printable_area_device_units, + int units_per_inch, + bool landscape_needs_flip); + const PageSetup& page_setup_device_units() const { + return page_setup_device_units_; + } - void set_landscape(bool landscape) { landscape_ = landscape; } void set_device_name(const base::string16& device_name) { device_name_ = device_name; } const base::string16& device_name() const { return device_name_; } + void set_dpi(int dpi) { dpi_ = dpi; } int dpi() const { return dpi_; } + void set_supports_alpha_blend(bool supports_alpha_blend) { supports_alpha_blend_ = supports_alpha_blend; } bool supports_alpha_blend() const { return supports_alpha_blend_; } - const PageSetup& page_setup_device_units() const { - return page_setup_device_units_; - } + int device_units_per_inch() const { #if defined(OS_MACOSX) return 72; @@ -69,54 +75,102 @@ class PRINTING_EXPORT PrintSettings { #endif // defined(OS_MACOSX) } + void set_ranges(const PageRanges& ranges) { ranges_ = ranges; }; + const PageRanges& ranges() const { return ranges_; }; + + void set_selection_only(bool selection_only) { + selection_only_ = selection_only; + } + bool selection_only() const { return selection_only_; } + + void set_should_print_backgrounds(bool should_print_backgrounds) { + should_print_backgrounds_ = should_print_backgrounds; + } + bool should_print_backgrounds() const { return should_print_backgrounds_; } + + void set_display_header_footer(bool display_header_footer) { + display_header_footer_ = display_header_footer; + } + bool display_header_footer() const { return display_header_footer_; } + + void set_title(const base::string16& title) { title_ = title; } + const base::string16& title() const { return title_; } + + void set_url(const base::string16& url) { url_ = url; } + const base::string16& url() const { return url_; } + + void set_collate(bool collate) { collate_ = collate; } + bool collate() const { return collate_; } + + void set_color(ColorModel color) { color_ = color; } + ColorModel color() const { return color_; } + + void set_copies(int copies) { copies_ = copies; } + int copies() const { return copies_; } + + void set_duplex_mode(DuplexMode duplex_mode) { duplex_mode_ = duplex_mode; } + DuplexMode duplex_mode() const { return duplex_mode_; } + + int desired_dpi() const { return desired_dpi_; } + + double max_shrink() const { return max_shrink_; } + + double min_shrink() const { return min_shrink_; } + + // Cookie generator. It is used to initialize PrintedDocument with its + // associated PrintSettings, to be sure that each generated PrintedPage is + // correctly associated with its corresponding PrintedDocument. + static int NewCookie(); + + private: // Multi-page printing. Each PageRange describes a from-to page combination. // This permits printing selected pages only. - PageRanges ranges; + PageRanges ranges_; // By imaging to a width a little wider than the available pixels, thin pages // will be scaled down a little, matching the way they print in IE and Camino. // This lets them use fewer sheets than they would otherwise, which is // presumably why other browsers do this. Wide pages will be scaled down more // than this. - double min_shrink; + double min_shrink_; // This number determines how small we are willing to reduce the page content // in order to accommodate the widest line. If the page would have to be // reduced smaller to make the widest line fit, we just clip instead (this // behavior matches MacIE and Mozilla, at least) - double max_shrink; + double max_shrink_; // Desired visible dots per inch rendering for output. Printing should be // scaled to ScreenDpi/dpix*desired_dpi. - int desired_dpi; + int desired_dpi_; // Indicates if the user only wants to print the current selection. - bool selection_only; + bool selection_only_; // Indicates what kind of margins should be applied to the printable area. - MarginType margin_type; - - // Cookie generator. It is used to initialize PrintedDocument with its - // associated PrintSettings, to be sure that each generated PrintedPage is - // correctly associated with its corresponding PrintedDocument. - static int NewCookie(); - - // Updates the orientation and flip the page if needed. - void SetOrientation(bool landscape); + MarginType margin_type_; // Strings to be printed as headers and footers if requested by the user. - base::string16 title; - base::string16 url; + base::string16 title_; + base::string16 url_; // True if the user wants headers and footers to be displayed. - bool display_header_footer; + bool display_header_footer_; // True if the user wants to print CSS backgrounds. - bool should_print_backgrounds; + bool should_print_backgrounds_; - private: - ////////////////////////////////////////////////////////////////////////////// - // Settings that can't be changed without side-effects. + // True if the user wants to print with collate. + bool collate_; + + // True if the user wants to print with collate. + ColorModel color_; + + // Number of copies user wants to print. + int copies_; + + // Duplex type user wants to use. + DuplexMode duplex_mode_; // Printer device name as opened by the OS. base::string16 device_name_; diff --git a/printing/print_settings_initializer.cc b/printing/print_settings_initializer.cc index 67afc10..565fc78 100644 --- a/printing/print_settings_initializer.cc +++ b/printing/print_settings_initializer.cc @@ -12,39 +12,95 @@ #include "base/strings/utf_string_conversions.h" #include "base/time/time.h" #include "base/values.h" +#include "printing/page_size_margins.h" #include "printing/print_job_constants.h" #include "printing/print_settings.h" #include "printing/units.h" -#include "ui/base/resource/resource_bundle.h" -#include "ui/gfx/font_list.h" -#include "ui/gfx/text_elider.h" -#include "url/gurl.h" namespace printing { -void PrintSettingsInitializer::InitHeaderFooterStrings( - const base::DictionaryValue& job_settings, - PrintSettings* print_settings) { +bool PrintSettingsInitializer::InitSettings( + const base::DictionaryValue& job_settings, + const PageRanges& ranges, + PrintSettings* settings) { + bool display_header_footer = false; if (!job_settings.GetBoolean(kSettingHeaderFooterEnabled, - &print_settings->display_header_footer)) { - NOTREACHED(); + &display_header_footer)) { + return false; } - if (!print_settings->display_header_footer) - return; - - base::string16 title; - base::string16 url; - if (!job_settings.GetString(kSettingHeaderFooterTitle, &title) || - !job_settings.GetString(kSettingHeaderFooterURL, &url)) { - NOTREACHED(); + settings->set_display_header_footer(display_header_footer); + + if (settings->display_header_footer()) { + base::string16 title; + base::string16 url; + if (!job_settings.GetString(kSettingHeaderFooterTitle, &title) || + !job_settings.GetString(kSettingHeaderFooterURL, &url)) { + return false; + } + settings->set_title(title); + settings->set_url(url); + } + + bool backgrounds = false; + bool selection_only = false; + if (!job_settings.GetBoolean(kSettingShouldPrintBackgrounds, &backgrounds) || + !job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, + &selection_only)) { + return false; + } + settings->set_should_print_backgrounds(backgrounds); + settings->set_selection_only(selection_only); + + int margin_type = DEFAULT_MARGINS; + if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || + (margin_type != DEFAULT_MARGINS && + margin_type != NO_MARGINS && + margin_type != CUSTOM_MARGINS && + margin_type != PRINTABLE_AREA_MARGINS)) { + margin_type = DEFAULT_MARGINS; + } + settings->set_margin_type(static_cast<MarginType>(margin_type)); + + if (margin_type == CUSTOM_MARGINS) { + PageSizeMargins page_size_margins; + GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); + + PageMargins margins_in_points; + margins_in_points.Clear(); + margins_in_points.top = page_size_margins.margin_top; + margins_in_points.bottom = page_size_margins.margin_bottom; + margins_in_points.left = page_size_margins.margin_left; + margins_in_points.right = page_size_margins.margin_right; + + settings->SetCustomMargins(margins_in_points); } - print_settings->title = title; - const gfx::FontList& default_fonts = - ui::ResourceBundle::GetSharedInstance().GetFontList( - ui::ResourceBundle::BaseFont); - print_settings->url = gfx::ElideUrl(GURL(url), default_fonts, 0, - std::string()); + settings->set_ranges(ranges); + + int color = 0; + bool landscape = false; + int duplex_mode = 0; + base::string16 device_name; + bool collate = false; + int copies = 1; + + if (!job_settings.GetBoolean(kSettingCollate, &collate) || + !job_settings.GetInteger(kSettingCopies, &copies) || + !job_settings.GetInteger(kSettingColor, &color) || + !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || + !job_settings.GetBoolean(kSettingLandscape, &landscape) || + !job_settings.GetString(kSettingDeviceName, &device_name)) { + return false; + } + + settings->set_collate(collate); + settings->set_copies(copies); + settings->SetOrientation(landscape); + settings->set_device_name(device_name); + settings->set_duplex_mode(static_cast<DuplexMode>(duplex_mode)); + settings->set_color(static_cast<ColorModel>(color)); + + return true; } } // namespace printing diff --git a/printing/print_settings_initializer.h b/printing/print_settings_initializer.h index 4897be8..a1093c5 100644 --- a/printing/print_settings_initializer.h +++ b/printing/print_settings_initializer.h @@ -7,6 +7,7 @@ #include "base/basictypes.h" #include "base/logging.h" +#include "printing/page_range.h" #include "printing/printing_export.h" namespace base { @@ -21,8 +22,9 @@ class PrintSettings; // provided |job_settings|. class PRINTING_EXPORT PrintSettingsInitializer { public: - static void InitHeaderFooterStrings( + static bool InitSettings( const base::DictionaryValue& job_settings, + const PageRanges& ranges, PrintSettings* print_settings); private: diff --git a/printing/print_settings_initializer_gtk.cc b/printing/print_settings_initializer_gtk.cc index c492f5a..793fdb8 100644 --- a/printing/print_settings_initializer_gtk.cc +++ b/printing/print_settings_initializer_gtk.cc @@ -18,8 +18,6 @@ namespace printing { void PrintSettingsInitializerGtk::InitPrintSettings( GtkPrintSettings* settings, GtkPageSetup* page_setup, - const PageRanges& new_ranges, - bool print_selection_only, PrintSettings* print_settings) { DCHECK(settings); DCHECK(page_setup); @@ -28,8 +26,6 @@ void PrintSettingsInitializerGtk::InitPrintSettings( base::string16 name(base::UTF8ToUTF16(static_cast<const char*>( gtk_print_settings_get_printer(settings)))); print_settings->set_device_name(name); - print_settings->ranges = new_ranges; - print_settings->selection_only = print_selection_only; gfx::Size physical_size_device_units; gfx::Rect printable_area_device_units; @@ -60,9 +56,6 @@ void PrintSettingsInitializerGtk::InitPrintSettings( } print_settings->set_dpi(dpi); - print_settings->SetPrinterPrintableArea(physical_size_device_units, - printable_area_device_units, - dpi); // Note: With the normal GTK print dialog, when the user selects the landscape // orientation, all that does is change the paper size. Which seems to be @@ -72,7 +65,12 @@ void PrintSettingsInitializerGtk::InitPrintSettings( // Thus this is only useful in print preview mode, where we manually set the // orientation and change the paper size ourselves. GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings); + // Set before SetPrinterPrintableArea to make it flip area if necessary. print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE); + + print_settings->SetPrinterPrintableArea(physical_size_device_units, + printable_area_device_units, + dpi, true); } const double PrintSettingsInitializerGtk::kTopMarginInInch = 0.25; diff --git a/printing/print_settings_initializer_gtk.h b/printing/print_settings_initializer_gtk.h index 5807500..828c34d 100644 --- a/printing/print_settings_initializer_gtk.h +++ b/printing/print_settings_initializer_gtk.h @@ -20,8 +20,6 @@ class PRINTING_EXPORT PrintSettingsInitializerGtk { public: static void InitPrintSettings(GtkPrintSettings* settings, GtkPageSetup* page_setup, - const PageRanges& new_ranges, - bool print_selection_only, PrintSettings* print_settings); // The default margins, in points. These values are based on 72 dpi, diff --git a/printing/print_settings_initializer_mac.cc b/printing/print_settings_initializer_mac.cc index e6ba4ea..9333ec9 100644 --- a/printing/print_settings_initializer_mac.cc +++ b/printing/print_settings_initializer_mac.cc @@ -13,19 +13,13 @@ namespace printing { void PrintSettingsInitializerMac::InitPrintSettings( PMPrinter printer, PMPageFormat page_format, - const PageRanges& new_ranges, - bool print_selection_only, PrintSettings* print_settings) { - DCHECK(print_settings); - print_settings->set_device_name( base::SysCFStringRefToUTF16(PMPrinterGetID(printer))); - print_settings->ranges = new_ranges; PMOrientation orientation = kPMPortrait; PMGetOrientation(page_format, &orientation); - print_settings->set_landscape(orientation == kPMLandscape); - print_settings->selection_only = print_selection_only; + print_settings->SetOrientation(orientation == kPMLandscape); UInt32 resolution_count = 0; PMResolution best_resolution = { 72.0, 72.0 }; @@ -62,7 +56,7 @@ void PrintSettingsInitializerMac::InitPrintSettings( print_settings->SetPrinterPrintableArea(physical_size_device_units, printable_area_device_units, - 72); + 72, false); } } // namespace printing diff --git a/printing/print_settings_initializer_mac.h b/printing/print_settings_initializer_mac.h index 2b79251..bbdd0b1 100644 --- a/printing/print_settings_initializer_mac.h +++ b/printing/print_settings_initializer_mac.h @@ -19,8 +19,6 @@ class PRINTING_EXPORT PrintSettingsInitializerMac { public: static void InitPrintSettings(PMPrinter printer, PMPageFormat page_format, - const PageRanges& new_ranges, - bool print_selection_only, PrintSettings* print_settings); private: diff --git a/printing/print_settings_initializer_win.cc b/printing/print_settings_initializer_win.cc index f97fd22..ef46f8d 100644 --- a/printing/print_settings_initializer_win.cc +++ b/printing/print_settings_initializer_win.cc @@ -14,17 +14,10 @@ namespace printing { void PrintSettingsInitializerWin::InitPrintSettings( HDC hdc, const DEVMODE& dev_mode, - const PageRanges& new_ranges, - const std::wstring& new_device_name, - bool print_selection_only, PrintSettings* print_settings) { DCHECK(hdc); DCHECK(print_settings); - - print_settings->set_device_name(new_device_name); - print_settings->ranges = const_cast<PageRanges&>(new_ranges); - print_settings->set_landscape(dev_mode.dmOrientation == DMORIENT_LANDSCAPE); - print_settings->selection_only = print_selection_only; + print_settings->SetOrientation(dev_mode.dmOrientation == DMORIENT_LANDSCAPE); int dpi = GetDeviceCaps(hdc, LOGPIXELSX); print_settings->set_dpi(dpi); @@ -58,7 +51,7 @@ void PrintSettingsInitializerWin::InitPrintSettings( print_settings->SetPrinterPrintableArea(physical_size_device_units, printable_area_device_units, - dpi); + dpi, false); } } // namespace printing diff --git a/printing/print_settings_initializer_win.h b/printing/print_settings_initializer_win.h index 720abae..fcbacc2 100644 --- a/printing/print_settings_initializer_win.h +++ b/printing/print_settings_initializer_win.h @@ -22,9 +22,6 @@ class PRINTING_EXPORT PrintSettingsInitializerWin { public: static void InitPrintSettings(HDC hdc, const DEVMODE& dev_mode, - const PageRanges& new_ranges, - const std::wstring& new_device_name, - bool print_selection_only, PrintSettings* print_settings); private: diff --git a/printing/printed_document.cc b/printing/printed_document.cc index e2cd74a..1f08ac1 100644 --- a/printing/printed_document.cc +++ b/printing/printed_document.cc @@ -51,10 +51,10 @@ PrintedDocument::PrintedDocument(const PrintSettings& settings, immutable_(settings, source, cookie) { // Records the expected page count if a range is setup. - if (!settings.ranges.empty()) { + if (!settings.ranges().empty()) { // If there is a range, set the number of page - for (unsigned i = 0; i < settings.ranges.size(); ++i) { - const PageRange& range = settings.ranges[i]; + for (unsigned i = 0; i < settings.ranges().size(); ++i) { + const PageRange& range = settings.ranges()[i]; mutable_.expected_page_count_ += range.to - range.from + 1; } } @@ -153,7 +153,7 @@ void PrintedDocument::set_page_count(int max_page) { base::AutoLock lock(lock_); DCHECK_EQ(0, mutable_.page_count_); mutable_.page_count_ = max_page; - if (immutable_.settings_.ranges.empty()) { + if (immutable_.settings_.ranges().empty()) { mutable_.expected_page_count_ = max_page; } else { // If there is a range, don't bother since expected_page_count_ is already diff --git a/printing/printing_context.cc b/printing/printing_context.cc index 98ea11c..edbcaa4 100644 --- a/printing/printing_context.cc +++ b/printing/printing_context.cc @@ -24,7 +24,7 @@ PrintingContext::~PrintingContext() { void PrintingContext::set_margin_type(MarginType type) { DCHECK(type != CUSTOM_MARGINS); - settings_.margin_type = type; + settings_.set_margin_type(type); } void PrintingContext::ResetSettings() { @@ -47,41 +47,31 @@ PrintingContext::Result PrintingContext::UpdatePrintSettings( const PageRanges& ranges) { ResetSettings(); - job_settings.GetBoolean(kSettingHeaderFooterEnabled, - &settings_.display_header_footer); + if (settings_.dpi() == 0) + UseDefaultSettings(); - int margin_type = DEFAULT_MARGINS; - if (!job_settings.GetInteger(kSettingMarginsType, &margin_type) || - (margin_type != DEFAULT_MARGINS && - margin_type != NO_MARGINS && - margin_type != CUSTOM_MARGINS && - margin_type != PRINTABLE_AREA_MARGINS)) { - margin_type = DEFAULT_MARGINS; + if (!PrintSettingsInitializer::InitSettings(job_settings, ranges, + &settings_)) { + NOTREACHED(); + return OnError(); } - settings_.margin_type = static_cast<MarginType>(margin_type); - if (margin_type == CUSTOM_MARGINS) { - printing::PageSizeMargins page_size_margins; - GetCustomMarginsFromJobSettings(job_settings, &page_size_margins); + bool print_to_pdf = false; + bool is_cloud_dialog = false; - PageMargins margins_in_points; - margins_in_points.Clear(); - margins_in_points.top = page_size_margins.margin_top; - margins_in_points.bottom = page_size_margins.margin_bottom; - margins_in_points.left = page_size_margins.margin_left; - margins_in_points.right = page_size_margins.margin_right; - - settings_.SetCustomMargins(margins_in_points); + if (!job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || + !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { + NOTREACHED(); + return OnError(); } - PrintingContext::Result result = UpdatePrinterSettings(job_settings, ranges); - PrintSettingsInitializer::InitHeaderFooterStrings(job_settings, &settings_); + bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); + bool open_in_external_preview = + job_settings.HasKey(kSettingOpenPDFInPreview); - job_settings.GetBoolean(kSettingShouldPrintBackgrounds, - &settings_.should_print_backgrounds); - job_settings.GetBoolean(kSettingShouldPrintSelectionOnly, - &settings_.selection_only); - return result; + return UpdatePrinterSettings( + print_to_pdf || is_cloud_dialog || print_to_cloud, + open_in_external_preview); } } // namespace printing diff --git a/printing/printing_context.h b/printing/printing_context.h index f4ba7ad7..7fa90d2 100644 --- a/printing/printing_context.h +++ b/printing/printing_context.h @@ -51,11 +51,14 @@ class PRINTING_EXPORT PrintingContext { // default device settings. virtual Result UseDefaultSettings() = 0; - // Updates printer related settings. |job_settings| contains all print job - // settings information. |ranges| has the new page range settings. - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) = 0; + // Updates printer settings. + // |target_is_pdf| is true if implementation needs to generate PDF without + // actual printing. In this case implementation may avoid setup of native + // print system. Ex. "save to pdf" or Cloud Print. + // |external_preview| is true if pdf is going to be opened in external + // preview. Used by MacOS only now to open Preview.app. + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) = 0; // Updates Print Settings. |job_settings| contains all print job // settings information. |ranges| has the new page range settings. diff --git a/printing/printing_context_android.h b/printing/printing_context_android.h index ff0d222..67d68eb 100644 --- a/printing/printing_context_android.h +++ b/printing/printing_context_android.h @@ -37,9 +37,8 @@ class PRINTING_EXPORT PrintingContextAndroid : public PrintingContext { bool has_selection, const PrintSettingsCallback& callback) OVERRIDE; virtual Result UseDefaultSettings() OVERRIDE; - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) OVERRIDE; + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) OVERRIDE; virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE; virtual Result NewDocument(const base::string16& document_name) OVERRIDE; virtual Result NewPage() OVERRIDE; diff --git a/printing/printing_context_gtk.cc b/printing/printing_context_gtk.cc index 39d0ef5..6cf0015 100644 --- a/printing/printing_context_gtk.cc +++ b/printing/printing_context_gtk.cc @@ -79,15 +79,17 @@ PrintingContext::Result PrintingContextGtk::UseDefaultSettings() { } PrintingContext::Result PrintingContextGtk::UpdatePrinterSettings( - const DictionaryValue& job_settings, const PageRanges& ranges) { + bool target_is_pdf, + bool external_preview) { DCHECK(!in_print_job_); + DCHECK(!external_preview) << "Not implemented"; if (!print_dialog_) { print_dialog_ = create_dialog_func_(this); print_dialog_->AddRefToDialog(); } - if (!print_dialog_->UpdateSettings(job_settings, ranges, &settings_)) + if (!print_dialog_->UpdateSettings(target_is_pdf, &settings_)) return OnError(); return OK; diff --git a/printing/printing_context_gtk.h b/printing/printing_context_gtk.h index 3b4e831b..1977893 100644 --- a/printing/printing_context_gtk.h +++ b/printing/printing_context_gtk.h @@ -38,9 +38,8 @@ class PRINTING_EXPORT PrintingContextGtk : public PrintingContext { bool has_selection, const PrintSettingsCallback& callback) OVERRIDE; virtual Result UseDefaultSettings() OVERRIDE; - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) OVERRIDE; + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) OVERRIDE; virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE; virtual Result NewDocument(const base::string16& document_name) OVERRIDE; virtual Result NewPage() OVERRIDE; diff --git a/printing/printing_context_mac.h b/printing/printing_context_mac.h index 8dc70ba..8e7e510 100644 --- a/printing/printing_context_mac.h +++ b/printing/printing_context_mac.h @@ -31,9 +31,8 @@ class PRINTING_EXPORT PrintingContextMac : public PrintingContext { bool has_selection, const PrintSettingsCallback& callback) OVERRIDE; virtual Result UseDefaultSettings() OVERRIDE; - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) OVERRIDE; + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) OVERRIDE; virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE; virtual Result NewDocument(const base::string16& document_name) OVERRIDE; virtual Result NewPage() OVERRIDE; @@ -48,8 +47,8 @@ class PRINTING_EXPORT PrintingContextMac : public PrintingContext { // after changes to |print_info_| in order for the changes to take effect in // printing. // This function ignores the page range information specified in the print - // info object and use |ranges| instead. - void InitPrintSettingsFromPrintInfo(const PageRanges& ranges); + // info object and use |settings_.ranges| instead. + void InitPrintSettingsFromPrintInfo(); // Returns the set of page ranges constructed from |print_info_|. PageRanges GetPageRangesFromPrintInfo(); diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm index bce03c8..9ed31dd 100644 --- a/printing/printing_context_mac.mm +++ b/printing/printing_context_mac.mm @@ -15,6 +15,7 @@ #include "base/mac/scoped_nsautorelease_pool.h" #include "base/mac/scoped_nsexception_enabler.h" #include "base/strings/sys_string_conversions.h" +#include "base/strings/utf_string_conversions.h" #include "base/values.h" #include "printing/print_settings_initializer_mac.h" @@ -97,7 +98,8 @@ void PrintingContextMac::AskUserForSettings( NSInteger selection = [panel runModalWithPrintInfo:printInfo]; if (selection == NSOKButton) { print_info_.reset([[panel printInfo] retain]); - InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); + settings_.set_ranges(GetPageRangesFromPrintInfo()); + InitPrintSettingsFromPrintInfo(); callback.Run(OK); } else { callback.Run(CANCEL); @@ -108,74 +110,40 @@ PrintingContext::Result PrintingContextMac::UseDefaultSettings() { DCHECK(!in_print_job_); print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); - InitPrintSettingsFromPrintInfo(GetPageRangesFromPrintInfo()); + settings_.set_ranges(GetPageRangesFromPrintInfo()); + InitPrintSettingsFromPrintInfo(); return OK; } PrintingContext::Result PrintingContextMac::UpdatePrinterSettings( - const DictionaryValue& job_settings, const PageRanges& ranges) { + bool target_is_pdf, + bool external_preview) { DCHECK(!in_print_job_); // NOTE: Reset |print_info_| with a copy of |sharedPrintInfo| so as to start // with a clean slate. print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); - bool collate; - int color; - bool landscape; - bool print_to_pdf; - bool is_cloud_dialog; - int copies; - int duplex_mode; - std::string device_name; - - if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || - !job_settings.GetBoolean(kSettingCollate, &collate) || - !job_settings.GetInteger(kSettingColor, &color) || - !job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || - !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || - !job_settings.GetInteger(kSettingCopies, &copies) || - !job_settings.GetString(kSettingDeviceName, &device_name) || - !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { - return OnError(); - } - - bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); - bool open_pdf_in_preview = job_settings.HasKey(kSettingOpenPDFInPreview); - - if (!print_to_pdf && !print_to_cloud && !is_cloud_dialog) { - if (!SetPrinter(device_name)) - return OnError(); - - if (!SetCopiesInPrintSettings(copies)) - return OnError(); - - if (!SetCollateInPrintSettings(collate)) - return OnError(); - - if (!SetDuplexModeInPrintSettings( - static_cast<DuplexMode>(duplex_mode))) { + if (!target_is_pdf) { + if (!SetPrinter(UTF16ToUTF8(settings_.device_name())) || + !SetCopiesInPrintSettings(settings_.copies()) || + !SetCollateInPrintSettings(settings_.collate()) || + !SetDuplexModeInPrintSettings(settings_.duplex_mode()) || + !SetOutputColor(settings_.color())) { return OnError(); } - - if (!SetOutputColor(color)) - return OnError(); - } - if (open_pdf_in_preview) { - if (!SetPrintPreviewJob()) - return OnError(); } - if (!UpdatePageFormatWithPaperInfo()) - return OnError(); - - if (!SetOrientationIsLandscape(landscape)) + if ((external_preview && !SetPrintPreviewJob()) || + !UpdatePageFormatWithPaperInfo() || + !SetOrientationIsLandscape(settings_.landscape())) { return OnError(); + } [print_info_.get() updateFromPMPrintSettings]; - InitPrintSettingsFromPrintInfo(ranges); + InitPrintSettingsFromPrintInfo(); return OK; } @@ -189,16 +157,16 @@ bool PrintingContextMac::SetPrintPreviewJob() { NULL, NULL) == noErr; } -void PrintingContextMac::InitPrintSettingsFromPrintInfo( - const PageRanges& ranges) { +void PrintingContextMac::InitPrintSettingsFromPrintInfo() { PMPrintSession print_session = static_cast<PMPrintSession>([print_info_.get() PMPrintSession]); PMPageFormat page_format = static_cast<PMPageFormat>([print_info_.get() PMPageFormat]); PMPrinter printer; PMSessionGetCurrentPrinter(print_session, &printer); + settings_.set_selection_only(false); PrintSettingsInitializerMac::InitPrintSettings( - printer, page_format, ranges, false, &settings_); + printer, page_format, &settings_); } bool PrintingContextMac::SetPrinter(const std::string& device_name) { diff --git a/printing/printing_context_no_system_dialog.cc b/printing/printing_context_no_system_dialog.cc index f02ef32..7fd2cee 100644 --- a/printing/printing_context_no_system_dialog.cc +++ b/printing/printing_context_no_system_dialog.cc @@ -73,24 +73,14 @@ PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() { settings_.set_dpi(dpi); settings_.SetPrinterPrintableArea(physical_size_device_units, printable_area_device_units, - dpi); + dpi, true); return OK; } PrintingContext::Result PrintingContextNoSystemDialog::UpdatePrinterSettings( - const base::DictionaryValue& job_settings, const PageRanges& ranges) { - bool landscape = false; - - if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) - return OnError(); - - if (settings_.dpi() == 0) - UseDefaultSettings(); - - settings_.SetOrientation(landscape); - settings_.ranges = ranges; - + bool target_is_pdf, + bool external_preview) { return OK; } diff --git a/printing/printing_context_no_system_dialog.h b/printing/printing_context_no_system_dialog.h index 505f3186..f45fb46 100644 --- a/printing/printing_context_no_system_dialog.h +++ b/printing/printing_context_no_system_dialog.h @@ -27,9 +27,8 @@ class PRINTING_EXPORT PrintingContextNoSystemDialog : public PrintingContext { bool has_selection, const PrintSettingsCallback& callback) OVERRIDE; virtual Result UseDefaultSettings() OVERRIDE; - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) OVERRIDE; + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) OVERRIDE; virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE; virtual Result NewDocument(const base::string16& document_name) OVERRIDE; virtual Result NewPage() OVERRIDE; diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index e967b4b..7a4a526 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -308,33 +308,12 @@ PrintingContext::Result PrintingContextWin::UseDefaultSettings() { } PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( - const DictionaryValue& job_settings, - const PageRanges& ranges) { + bool target_is_pdf, + bool external_preview) { DCHECK(!in_print_job_); + DCHECK(!external_preview) << "Not implemented"; - bool collate; - int color; - bool landscape; - bool print_to_pdf; - bool is_cloud_dialog; - int copies; - int duplex_mode; - base::string16 device_name; - - if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || - !job_settings.GetBoolean(kSettingCollate, &collate) || - !job_settings.GetInteger(kSettingColor, &color) || - !job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || - !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || - !job_settings.GetInteger(kSettingCopies, &copies) || - !job_settings.GetString(kSettingDeviceName, &device_name) || - !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { - return OnError(); - } - - bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); - - if (print_to_pdf || print_to_cloud || is_cloud_dialog) { + if (target_is_pdf) { // Default fallback to Letter size. gfx::Size paper_size; gfx::Rect paper_rect; @@ -362,15 +341,14 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( } } paper_rect.SetRect(0, 0, paper_size.width(), paper_size.height()); - settings_.SetPrinterPrintableArea(paper_size, paper_rect, kPDFDpi); + settings_.SetPrinterPrintableArea(paper_size, paper_rect, kPDFDpi, true); settings_.set_dpi(kPDFDpi); - settings_.SetOrientation(landscape); - settings_.ranges = ranges; return OK; } ScopedPrinterHandle printer; - LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); + LPWSTR device_name_wide = + const_cast<wchar_t*>(settings_.device_name().c_str()); if (!printer.OpenPrinter(device_name_wide)) return OnError(); @@ -394,15 +372,17 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( return OnError(); } - if (color == GRAY) + if (settings_.color() == GRAY) dev_mode->dmColor = DMCOLOR_MONOCHROME; else dev_mode->dmColor = DMCOLOR_COLOR; - dev_mode->dmCopies = std::max(copies, 1); - if (dev_mode->dmCopies > 1) // do not change collate unless multiple copies - dev_mode->dmCollate = collate ? DMCOLLATE_TRUE : DMCOLLATE_FALSE; - switch (duplex_mode) { + dev_mode->dmCopies = std::max(settings_.copies(), 1); + if (dev_mode->dmCopies > 1) { // do not change collate unless multiple copies + dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : + DMCOLLATE_FALSE; + } + switch (settings_.duplex_mode()) { case LONG_EDGE: dev_mode->dmDuplex = DMDUP_VERTICAL; break; @@ -415,7 +395,8 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( default: // UNKNOWN_DUPLEX_MODE break; } - dev_mode->dmOrientation = landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT; + dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE : + DMORIENT_PORTRAIT; // Update data using DocumentProperties. if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, @@ -424,12 +405,11 @@ PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( } // Set printer then refresh printer settings. - if (!AllocateContext(device_name, dev_mode, &context_)) { + if (!AllocateContext(settings_.device_name(), dev_mode, &context_)) { return OnError(); } PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, - ranges, device_name, - false, &settings_); + &settings_); return OK; } @@ -626,11 +606,10 @@ bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode, } } - PrintSettingsInitializerWin::InitPrintSettings(context_, - dev_mode, - ranges_vector, - new_device_name, - selection_only, + settings_.set_ranges(ranges_vector); + settings_.set_device_name(new_device_name); + settings_.set_selection_only(selection_only); + PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode, &settings_); return true; diff --git a/printing/printing_context_win.h b/printing/printing_context_win.h index 06226b2..37e5b04 100644 --- a/printing/printing_context_win.h +++ b/printing/printing_context_win.h @@ -32,9 +32,8 @@ class PRINTING_EXPORT PrintingContextWin bool has_selection, const PrintSettingsCallback& callback) OVERRIDE; virtual Result UseDefaultSettings() OVERRIDE; - virtual Result UpdatePrinterSettings( - const base::DictionaryValue& job_settings, - const PageRanges& ranges) OVERRIDE; + virtual Result UpdatePrinterSettings(bool target_is_pdf, + bool external_preview) OVERRIDE; virtual Result InitWithSettings(const PrintSettings& settings) OVERRIDE; virtual Result NewDocument(const base::string16& document_name) OVERRIDE; virtual Result NewPage() OVERRIDE; |