diff options
author | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-29 02:36:26 +0000 |
---|---|---|
committer | thestig@chromium.org <thestig@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2011-03-29 02:36:26 +0000 |
commit | c48bee274f6c6869430bdda4ce771c8cdabf736f (patch) | |
tree | 7ce89dceab0e9dd2d2a7f7426a429f68201a8782 | |
parent | e555f205eb8931da7fdf0f4cb4a01a24c3e2bf8a (diff) | |
download | chromium_src-c48bee274f6c6869430bdda4ce771c8cdabf736f.zip chromium_src-c48bee274f6c6869430bdda4ce771c8cdabf736f.tar.gz chromium_src-c48bee274f6c6869430bdda4ce771c8cdabf736f.tar.bz2 |
Print Preview: Implement basic settings.
BUG=57902
TEST=Run with --enable-print-preview, make sure orientation and color settings work.
Review URL: http://codereview.chromium.org/6736014
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@79655 0039d316-1c4b-4281-b951-d872f2087c98
-rw-r--r-- | chrome/browser/resources/print_preview.js | 42 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_handler.cc | 43 | ||||
-rw-r--r-- | chrome/browser/ui/webui/print_preview_handler.h | 15 | ||||
-rw-r--r-- | printing/page_setup.cc | 17 | ||||
-rw-r--r-- | printing/page_setup.h | 5 | ||||
-rw-r--r-- | printing/print_job_constants.cc | 15 | ||||
-rw-r--r-- | printing/print_job_constants.h | 15 | ||||
-rw-r--r-- | printing/print_settings.cc | 9 | ||||
-rw-r--r-- | printing/print_settings.h | 5 | ||||
-rw-r--r-- | printing/printing.gyp | 2 | ||||
-rw-r--r-- | printing/printing_context_cairo.cc | 8 | ||||
-rw-r--r-- | printing/printing_context_mac.mm | 6 | ||||
-rw-r--r-- | printing/printing_context_win.cc | 6 |
13 files changed, 178 insertions, 10 deletions
diff --git a/chrome/browser/resources/print_preview.js b/chrome/browser/resources/print_preview.js index edb6653..f37b482 100644 --- a/chrome/browser/resources/print_preview.js +++ b/chrome/browser/resources/print_preview.js @@ -27,6 +27,8 @@ function load() { $('print-pages').addEventListener('click', validatePageRangeInfo); $('copies').addEventListener('input', validateNumberOfCopies); $('copies').addEventListener('blur', handleCopiesFieldBlur); + $('layout').onchange = getPreview; + $('color').onchange = getPreview; updateCollateCheckboxState(); chrome.send('getPrinters'); @@ -160,6 +162,24 @@ function parsePageRanges() { } /** + * Checks whether the preview layout setting is set to 'landscape' or not. + * + * @return {boolean} true if layout is 'landscape'. + */ +function isLandscape() { + return ($('layout').options[$('layout').selectedIndex].value == '1'); +} + +/** + * Checks whether the preview color setting is set to 'color' or not. + * + * @return {boolean} true if color is 'color'. + */ +function isColor() { + return ($('color').options[$('color').selectedIndex].value == '1'); +} + +/** * Creates a JSON string based on the values in the printer settings. * * @return {string} JSON string with print job settings. @@ -173,8 +193,8 @@ function getSettingsJSON() { var twoSided = $('two-sided').checked; var copies = $('copies').value; var collate = $('collate').checked; - var landscape = ($('layout').options[$('layout').selectedIndex].value == '1'); - var color = ($('color').options[$('color').selectedIndex].value == '1'); + var landscape = isLandscape(); + var color = isColor(); return JSON.stringify({'printerName': printerName, 'pageRange': pageRangesInfo, @@ -222,8 +242,22 @@ function setPrinters(printers) { getPreview(); } +/** + * Sets the color mode for the PDF plugin. + * @param {boolean} color is true if the PDF plugin should display in color. + */ +function setColor(color) { + if (!hasPDFPlugin) { + return; + } + $('pdf-viewer').grayscale(!color); +} + function onPDFLoad() { - $('pdf-viewer').fitToHeight(); + if (isLandscape()) + $('pdf-viewer').fitToWidth(); + else + $('pdf-viewer').fitToHeight(); } /** @@ -265,6 +299,7 @@ function createPDFPlugin() { if ($('pdf-viewer')) { $('pdf-viewer').reload(); + $('pdf-viewer').grayscale(!isColor()); return; } @@ -283,6 +318,7 @@ function createPDFPlugin() { $('no-plugin').classList.remove('hidden'); return; } + pdfPlugin.grayscale(true); pdfPlugin.onload('onPDFLoad()'); } diff --git a/chrome/browser/ui/webui/print_preview_handler.cc b/chrome/browser/ui/webui/print_preview_handler.cc index 45f316b..b828070 100644 --- a/chrome/browser/ui/webui/print_preview_handler.cc +++ b/chrome/browser/ui/webui/print_preview_handler.cc @@ -15,9 +15,13 @@ #include "content/browser/renderer_host/render_view_host.h" #include "content/browser/tab_contents/tab_contents.h" #include "printing/backend/print_backend.h" +#include "printing/print_job_constants.h" namespace { +const bool kColorDefaultValue = false; +const bool kLandscapeDefaultValue = false; + TabContents* GetInitiatorTab(TabContents* preview_tab) { printing::PrintPreviewTabController* tab_controller = printing::PrintPreviewTabController::GetInstance(); @@ -26,6 +30,8 @@ TabContents* GetInitiatorTab(TabContents* preview_tab) { return tab_controller->GetInitiatorTab(preview_tab); } +// Get the print job settings dictionary from |args|. The caller takes +// ownership of the returned DictionaryValue. Returns NULL on failure. DictionaryValue* GetSettingsDictionary(const ListValue* args) { std::string json_str; if (!args->GetString(0, &json_str)) { @@ -100,7 +106,10 @@ class EnumeratePrintersTaskProxy }; PrintPreviewHandler::PrintPreviewHandler() - : print_backend_(printing::PrintBackend::CreateInstance(NULL)) { + : print_backend_(printing::PrintBackend::CreateInstance(NULL)), + need_to_generate_preview_(true), + color_(kColorDefaultValue), + landscape_(kLandscapeDefaultValue) { } PrintPreviewHandler::~PrintPreviewHandler() { @@ -131,7 +140,17 @@ void PrintPreviewHandler::HandleGetPreview(const ListValue* args) { scoped_ptr<DictionaryValue> settings(GetSettingsDictionary(args)); if (!settings.get()) return; - initiator_tab->render_view_host()->PrintPreview(*settings); + + // Handle settings that do not require print preview regeneration. + ProcessColorSetting(*settings); + + // Handle settings that require print preview regeneration. + ProcessLandscapeSetting(*settings); + + if (need_to_generate_preview_) { + initiator_tab->render_view_host()->PrintPreview(*settings); + need_to_generate_preview_ = false; + } } void PrintPreviewHandler::HandlePrint(const ListValue* args) { @@ -150,3 +169,23 @@ void PrintPreviewHandler::HandlePrint(const ListValue* args) { void PrintPreviewHandler::SendPrinterList(const ListValue& printers) { web_ui_->CallJavascriptFunction("setPrinters", printers); } + +void PrintPreviewHandler::ProcessColorSetting(const DictionaryValue& settings) { + bool color = kColorDefaultValue; + settings.GetBoolean(printing::kSettingColor, &color); + if (color != color_) { + color_ = color; + FundamentalValue color_value(color_); + web_ui_->CallJavascriptFunction("setColor", color_value); + } +} + +void PrintPreviewHandler::ProcessLandscapeSetting( + const DictionaryValue& settings) { + bool landscape = kLandscapeDefaultValue; + settings.GetBoolean(printing::kSettingLandscape, &landscape); + if (landscape != landscape_) { + landscape_ = landscape; + need_to_generate_preview_ = true; + } +} diff --git a/chrome/browser/ui/webui/print_preview_handler.h b/chrome/browser/ui/webui/print_preview_handler.h index 9f11707..108234e 100644 --- a/chrome/browser/ui/webui/print_preview_handler.h +++ b/chrome/browser/ui/webui/print_preview_handler.h @@ -43,9 +43,24 @@ class PrintPreviewHandler : public WebUIMessageHandler, // Send the list of printers to the Web UI. void SendPrinterList(const ListValue& printers); + // Helper function to process the color setting in the dictionary. + void ProcessColorSetting(const DictionaryValue& settings); + + // Helper function to process the landscape setting in the dictionary. + void ProcessLandscapeSetting(const DictionaryValue& settings); + // Pointer to current print system. scoped_refptr<printing::PrintBackend> print_backend_; + // Set to true if we need to generate a new print preview. + bool need_to_generate_preview_; + + // Set to true if the preview should be in color. + bool color_; + + // Set to true if the preview should be landscape. + bool landscape_; + DISALLOW_COPY_AND_ASSIGN(PrintPreviewHandler); }; diff --git a/printing/page_setup.cc b/printing/page_setup.cc index 6d4ef1d..9ac3e520 100644 --- a/printing/page_setup.cc +++ b/printing/page_setup.cc @@ -1,9 +1,11 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #include "printing/page_setup.h" +#include <algorithm> + #include "base/logging.h" namespace printing { @@ -125,4 +127,17 @@ void PageSetup::SetRequestedMargins(const PageMargins& requested_margins) { Init(physical_size_, printable_area_, text_height_); } +void PageSetup::FlipOrientation() { + if (physical_size_.width() && physical_size_.height()) { + gfx::Size new_size(physical_size_.height(), physical_size_.width()); + int new_y = physical_size_.width() - + (printable_area_.width() + printable_area_.x()); + gfx::Rect new_printable_area(printable_area_.y(), + new_y, + printable_area_.height(), + printable_area_.width()); + Init(new_size, new_printable_area, text_height_); + } +} + } // namespace printing diff --git a/printing/page_setup.h b/printing/page_setup.h index d96980f..0ca1f789 100644 --- a/printing/page_setup.h +++ b/printing/page_setup.h @@ -1,4 +1,4 @@ -// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -47,6 +47,9 @@ class PageSetup { void SetRequestedMargins(const PageMargins& requested_margins); + // Flips the orientation of the page and recalculates all page areas. + void FlipOrientation(); + const gfx::Size& physical_size() const { return physical_size_; } const gfx::Rect& overlay_area() const { return overlay_area_; } const gfx::Rect& content_area() const { return content_area_; } diff --git a/printing/print_job_constants.cc b/printing/print_job_constants.cc new file mode 100644 index 0000000..f4eb542 --- /dev/null +++ b/printing/print_job_constants.cc @@ -0,0 +1,15 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "printing/print_job_constants.h" + +namespace printing { + +// Print out color: true for color, false for grayscale. +const char kSettingColor[] = "color"; + +// Page orientation: true for landscape, false for portrait. +const char kSettingLandscape[] = "landscape"; + +} // namespace printing diff --git a/printing/print_job_constants.h b/printing/print_job_constants.h new file mode 100644 index 0000000..ce224af --- /dev/null +++ b/printing/print_job_constants.h @@ -0,0 +1,15 @@ +// Copyright (c) 2011 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef PRINTING_PRINT_JOB_CONSTANTS_H_ +#define PRINTING_PRINT_JOB_CONSTANTS_H_ + +namespace printing { + +extern const char kSettingColor[]; +extern const char kSettingLandscape[]; + +} // namespace printing + +#endif // PRINTING_PRINT_JOB_CONSTANTS_H_ diff --git a/printing/print_settings.cc b/printing/print_settings.cc index 3f0f842..92f3560 100644 --- a/printing/print_settings.cc +++ b/printing/print_settings.cc @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -94,4 +94,11 @@ int PrintSettings::NewCookie() { return cookie_seq.GetNext() + 1; } +void PrintSettings::SetOrientation(bool landscape) { + if (landscape_ != landscape) { + landscape_ = landscape; + page_setup_device_units_.FlipOrientation(); + } +} + } // namespace printing diff --git a/printing/print_settings.h b/printing/print_settings.h index 2ec5462..fbd37f4 100644 --- a/printing/print_settings.h +++ b/printing/print_settings.h @@ -1,4 +1,4 @@ -// Copyright (c) 2010 The Chromium Authors. All rights reserved. +// Copyright (c) 2011 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. @@ -94,6 +94,9 @@ class PrintSettings { // correctly associated with its corresponding PrintedDocument. static int NewCookie(); + // Updates the orientation and flip the page if needed. + void SetOrientation(bool landscape); + private: ////////////////////////////////////////////////////////////////////////////// // Settings that can't be changed without side-effects. diff --git a/printing/printing.gyp b/printing/printing.gyp index 0e504c4..94c90a6 100644 --- a/printing/printing.gyp +++ b/printing/printing.gyp @@ -68,6 +68,8 @@ 'printing_context_mac.h', 'printing_context_win.cc', 'printing_context_win.h', + 'print_job_constants.cc', + 'print_job_constants.h', 'print_settings.cc', 'print_settings.h', 'print_settings_initializer_gtk.cc', diff --git a/printing/printing_context_cairo.cc b/printing/printing_context_cairo.cc index 5687fff..c721e3d 100644 --- a/printing/printing_context_cairo.cc +++ b/printing/printing_context_cairo.cc @@ -6,8 +6,9 @@ #include "base/logging.h" #include "base/values.h" -#include "printing/units.h" +#include "printing/print_job_constants.h" #include "printing/print_settings_initializer_gtk.h" +#include "printing/units.h" #if defined(OS_CHROMEOS) #include <unicode/ulocdata.h> @@ -152,6 +153,11 @@ PrintingContext::Result PrintingContextCairo::UpdatePrintSettings( const DictionaryValue& job_settings, const PageRanges& ranges) { DCHECK(!in_print_job_); + bool landscape; + if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) + return OnError(); + settings_.SetOrientation(landscape); + settings_.ranges = ranges; // TODO(kmadhusu): Update other print settings such as number of copies, diff --git a/printing/printing_context_mac.mm b/printing/printing_context_mac.mm index 47ccb72..65e707f 100644 --- a/printing/printing_context_mac.mm +++ b/printing/printing_context_mac.mm @@ -11,6 +11,7 @@ #include "base/mac/scoped_cftyperef.h" #include "base/sys_string_conversions.h" #include "base/values.h" +#include "printing/print_job_constants.h" #include "printing/print_settings_initializer_mac.h" namespace printing { @@ -91,6 +92,11 @@ PrintingContext::Result PrintingContextMac::UpdatePrintSettings( ResetSettings(); print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]); + bool landscape; + if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) + return OnError(); + settings_.SetOrientation(landscape); + std::string printer_name; if (!job_settings.GetString("printerName", &printer_name)) return OnError(); diff --git a/printing/printing_context_win.cc b/printing/printing_context_win.cc index 1b62c44..d410f31 100644 --- a/printing/printing_context_win.cc +++ b/printing/printing_context_win.cc @@ -12,6 +12,7 @@ #include "base/time.h" #include "base/utf_string_conversions.h" #include "base/values.h" +#include "printing/print_job_constants.h" #include "printing/print_settings_initializer_win.h" #include "printing/printed_document.h" #include "skia/ext/platform_device_win.h" @@ -213,6 +214,11 @@ PrintingContext::Result PrintingContextWin::UpdatePrintSettings( const DictionaryValue& job_settings, const PageRanges& ranges) { DCHECK(!in_print_job_); + bool landscape; + if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) + return OnError(); + settings_.SetOrientation(landscape); + // TODO(kmadhusu): Update other print settings such as number of copies, // collate, duplex printing, job title, etc., |