Skip to content

Commit

Permalink
Print Preview: Implement basic settings.
Browse files Browse the repository at this point in the history
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
  • Loading branch information
thestig@chromium.org committed Mar 29, 2011
1 parent e555f20 commit c48bee2
Show file tree
Hide file tree
Showing 13 changed files with 178 additions and 10 deletions.
42 changes: 39 additions & 3 deletions chrome/browser/resources/print_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand Down Expand Up @@ -159,6 +161,24 @@ function parsePageRanges() {
return true;
}

/**
* 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.
*
Expand All @@ -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,
Expand Down Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -265,6 +299,7 @@ function createPDFPlugin() {

if ($('pdf-viewer')) {
$('pdf-viewer').reload();
$('pdf-viewer').grayscale(!isColor());
return;
}

Expand All @@ -283,6 +318,7 @@ function createPDFPlugin() {
$('no-plugin').classList.remove('hidden');
return;
}
pdfPlugin.grayscale(true);
pdfPlugin.onload('onPDFLoad()');
}

Expand Down
43 changes: 41 additions & 2 deletions chrome/browser/ui/webui/print_preview_handler.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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)) {
Expand Down Expand Up @@ -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() {
Expand Down Expand Up @@ -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) {
Expand All @@ -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;
}
}
15 changes: 15 additions & 0 deletions chrome/browser/ui/webui/print_preview_handler.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
};

Expand Down
17 changes: 16 additions & 1 deletion printing/page_setup.cc
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion printing/page_setup.h
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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_; }
Expand Down
15 changes: 15 additions & 0 deletions printing/print_job_constants.cc
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions printing/print_job_constants.h
Original file line number Diff line number Diff line change
@@ -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_
9 changes: 8 additions & 1 deletion printing/print_settings.cc
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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
5 changes: 4 additions & 1 deletion printing/print_settings.h
Original file line number Diff line number Diff line change
@@ -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.

Expand Down Expand Up @@ -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.
Expand Down
2 changes: 2 additions & 0 deletions printing/printing.gyp
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
8 changes: 7 additions & 1 deletion printing/printing_context_cairo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 6 additions & 0 deletions printing/printing_context_mac.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -91,6 +92,11 @@
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();
Expand Down
6 changes: 6 additions & 0 deletions printing/printing_context_win.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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.,

Expand Down

0 comments on commit c48bee2

Please sign in to comment.