Skip to content

Commit

Permalink
PrintPreview: Set number of copies and collate in print ticket.
Browse files Browse the repository at this point in the history
BUG=none
TEST=Enable print preview on mac. Print preview a webpage. Change the number of copies and collate setting values. Print the preview data.

Review URL: http://codereview.chromium.org/6780001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@80499 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
kmadhusu@chromium.org committed Apr 5, 2011
1 parent d76bbf0 commit c97e5e8
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 29 deletions.
14 changes: 12 additions & 2 deletions chrome/browser/resources/print_preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,16 @@ function isColor() {
return ($('color').options[$('color').selectedIndex].value == '1');
}

/**
* Checks whether the preview collate setting value is set or not.
*
* @return {boolean} true if collate setting is enabled and checked.
*/
function isCollated() {
var collateField = $('collate');
return (!collateField.disabled && collateField.checked);
}

/**
* Creates a JSON string based on the values in the printer settings.
*
Expand All @@ -191,8 +201,8 @@ function getSettingsJSON() {
printerName = $('printer-list').options[selectedPrinter].textContent;
var printAll = $('all-pages').checked;
var twoSided = $('two-sided').checked;
var copies = $('copies').value;
var collate = $('collate').checked;
var copies = parseInt($('copies').value, 10);
var collate = isCollated();
var landscape = isLandscape();
var color = isColor();
var printToPDF = (printerName == localStrings.getString('printToPDF'));
Expand Down
6 changes: 6 additions & 0 deletions printing/print_job_constants.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,15 @@

namespace printing {

// Print job setting 'collate'.
const char kSettingCollate[] = "collate";

// Print out color: true for color, false for grayscale.
const char kSettingColor[] = "color";

// Number of copies.
const char kSettingCopies[] = "copies";

// Page orientation: true for landscape, false for portrait.
const char kSettingLandscape[] = "landscape";

Expand Down
2 changes: 2 additions & 0 deletions printing/print_job_constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@

namespace printing {

extern const char kSettingCollate[];
extern const char kSettingColor[];
extern const char kSettingCopies[];
extern const char kSettingLandscape[];
extern const char kSettingPrintToPDF[];
extern const char kSettingPrinterName[];
Expand Down
14 changes: 0 additions & 14 deletions printing/printing_context.cc
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include "printing/printing_context.h"

#include "base/values.h"
#include "printing/print_job_constants.h"

namespace printing {

Expand All @@ -29,19 +28,6 @@ void PrintingContext::ResetSettings() {
abort_printing_ = false;
}

bool PrintingContext::GetSettingsFromDict(const DictionaryValue& settings,
bool* landscape,
std::string* printerName) {
bool ret = true;
if (landscape)
ret &= settings.GetBoolean(kSettingLandscape, landscape);

if (printerName)
ret &= settings.GetString(kSettingPrinterName, printerName);

return ret;
}

PrintingContext::Result PrintingContext::OnError() {
ResetSettings();
return abort_printing_ ? CANCEL : FAILED;
Expand Down
7 changes: 0 additions & 7 deletions printing/printing_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,13 +104,6 @@ class PrintingContext {
// Reinitializes the settings for object reuse.
void ResetSettings();

// Extracts print job settings from |settings|. Out parameters can be NULL.
// Returns true if all non-NULL out parameters are successfully extracted
// from |settings| else returns false.
bool GetSettingsFromDict(const DictionaryValue& settings,
bool* landscape,
std::string* printerName);

// Does bookkeeping when an error occurs.
PrintingContext::Result OnError();

Expand Down
3 changes: 2 additions & 1 deletion printing/printing_context_cairo.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "base/logging.h"
#include "base/values.h"
#include "printing/print_job_constants.h"
#include "printing/print_settings_initializer_gtk.h"
#include "printing/units.h"

Expand Down Expand Up @@ -153,7 +154,7 @@ PrintingContext::Result PrintingContextCairo::UpdatePrintSettings(
DCHECK(!in_print_job_);

bool landscape;
if (!GetSettingsFromDict(job_settings, &landscape, NULL))
if (!job_settings.GetBoolean(kSettingLandscape, &landscape))
return OnError();

settings_.SetOrientation(landscape);
Expand Down
8 changes: 8 additions & 0 deletions printing/printing_context_mac.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,14 @@ class PrintingContextMac : public PrintingContext {
// Returns true if the printer was set else returns false.
bool SetPrinter(const std::string& printer_name);

// Sets |copies| in PMPrintSettings.
// Returns true if the number of copies is set.
bool SetCopiesInPrintSettings(int copies);

// Sets |collate| in PMPrintSettings.
// Returns true if |collate| is set.
bool SetCollateInPrintSettings(bool collate);

// The native print info object.
scoped_nsobject<NSPrintInfo> print_info_;

Expand Down
35 changes: 31 additions & 4 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 @@ -85,22 +86,33 @@
const DictionaryValue& job_settings, const PageRanges& ranges) {
DCHECK(!in_print_job_);

// TODO (kmadhusu): Update other print job settings such as number of copies,
// collate, etc.,

ResetSettings();
print_info_.reset([[NSPrintInfo sharedPrintInfo] copy]);

bool landscape;
std::string printer_name;
if (!GetSettingsFromDict(job_settings, &landscape, &printer_name))
int copies;
bool collate;
if (!job_settings.GetBoolean(kSettingLandscape, &landscape) ||
!job_settings.GetString(kSettingPrinterName, &printer_name) ||
!job_settings.GetInteger(kSettingCopies, &copies) ||
!job_settings.GetBoolean(kSettingCollate, &collate)) {
return OnError();
}

settings_.SetOrientation(landscape);

if (!SetPrinter(printer_name))
return OnError();

if (!SetCopiesInPrintSettings(copies))
return OnError();

if (!SetCollateInPrintSettings(collate))
return OnError();

[print_info_.get() updateFromPMPrintSettings];

InitPrintSettingsFromPrintInfo(ranges);
return OK;
}
Expand Down Expand Up @@ -132,6 +144,21 @@
return true;
}

bool PrintingContextMac::SetCopiesInPrintSettings(int copies) {
if (copies < 1)
return false;

PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
return PMSetCopies(pmPrintSettings, copies, false) == noErr;
}

bool PrintingContextMac::SetCollateInPrintSettings(bool collate) {
PMPrintSettings pmPrintSettings =
static_cast<PMPrintSettings>([print_info_.get() PMPrintSettings]);
return PMSetCollate(pmPrintSettings, collate) == noErr;
}

void PrintingContextMac::ParsePrintInfo(NSPrintInfo* print_info) {
ResetSettings();
print_info_.reset([print_info retain]);
Expand Down
3 changes: 2 additions & 1 deletion 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 @@ -214,7 +215,7 @@ PrintingContext::Result PrintingContextWin::UpdatePrintSettings(
DCHECK(!in_print_job_);

bool landscape;
if (!GetSettingsFromDict(job_settings, &landscape, NULL))
if (!job_settings.GetBoolean(kSettingLandscape, &landscape))
return OnError();

settings_.SetOrientation(landscape);
Expand Down

0 comments on commit c97e5e8

Please sign in to comment.