blob: 4768e4d3a3d65ffce2285fb8631bbc593dc0ad98 [file] [log] [blame]
[email protected]ea5e81d92011-11-08 18:45:151// Copyright (c) 2011 The Chromium Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "printing/printing_context_no_system_dialog.h"
6
7#include <unicode/ulocdata.h>
8
9#include "base/logging.h"
10#include "base/values.h"
11#include "printing/metafile.h"
12#include "printing/print_job_constants.h"
13#include "printing/units.h"
14
15namespace printing {
16
17// static
18PrintingContext* PrintingContext::Create(const std::string& app_locale) {
19 return static_cast<PrintingContext*>(
20 new PrintingContextNoSystemDialog(app_locale));
21}
22
23PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(
24 const std::string& app_locale) : PrintingContext(app_locale) {
25}
26
27PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() {
28 ReleaseContext();
29}
30
31void PrintingContextNoSystemDialog::AskUserForSettings(
32 gfx::NativeView parent_view,
33 int max_pages,
34 bool has_selection,
[email protected]abe48112011-11-19 01:58:3835 const PrintSettingsCallback& callback) {
[email protected]ea5e81d92011-11-08 18:45:1536 // We don't want to bring up a dialog here. Ever. Just signal the callback.
[email protected]abe48112011-11-19 01:58:3837 callback.Run(OK);
[email protected]ea5e81d92011-11-08 18:45:1538}
39
40PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() {
41 DCHECK(!in_print_job_);
42
43 ResetSettings();
[email protected]4c9054b2013-11-04 18:34:2944 settings_.set_dpi(kDefaultPdfDpi);
45 gfx::Size physical_size = GetPdfPaperSizeDeviceUnits();
46 // Assume full page is printable for now.
47 gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height());
48 DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi);
49 settings_.SetPrinterPrintableArea(physical_size, printable_area, true);
50 return OK;
51}
52
53gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() {
[email protected]ea5e81d92011-11-08 18:45:1554 int32_t width = 0;
55 int32_t height = 0;
56 UErrorCode error = U_ZERO_ERROR;
57 ulocdata_getPaperSize(app_locale_.c_str(), &height, &width, &error);
[email protected]d1bee422012-08-07 23:44:3158 if (error > U_ZERO_ERROR) {
[email protected]ea5e81d92011-11-08 18:45:1559 // If the call failed, assume a paper size of 8.5 x 11 inches.
60 LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: "
61 << error;
[email protected]4c9054b2013-11-04 18:34:2962 width = static_cast<int>(
63 kLetterWidthInch * settings_.device_units_per_inch());
64 height = static_cast<int>(
65 kLetterHeightInch * settings_.device_units_per_inch());
[email protected]ea5e81d92011-11-08 18:45:1566 } else {
67 // ulocdata_getPaperSize returns the width and height in mm.
68 // Convert this to pixels based on the dpi.
[email protected]4c9054b2013-11-04 18:34:2969 float multiplier = 100 * settings_.device_units_per_inch();
70 multiplier /= kHundrethsMMPerInch;
71 width *= multiplier;
72 height *= multiplier;
[email protected]ea5e81d92011-11-08 18:45:1573 }
[email protected]4c9054b2013-11-04 18:34:2974 return gfx::Size(width, height);
[email protected]ea5e81d92011-11-08 18:45:1575}
76
77PrintingContext::Result PrintingContextNoSystemDialog::UpdatePrinterSettings(
[email protected]e5324b52013-10-29 03:16:3778 bool external_preview) {
[email protected]0c5e3022013-11-01 14:25:0179
80 if (settings_.dpi() == 0)
81 UseDefaultSettings();
82
[email protected]ea5e81d92011-11-08 18:45:1583 return OK;
84}
85
86PrintingContext::Result PrintingContextNoSystemDialog::InitWithSettings(
87 const PrintSettings& settings) {
88 DCHECK(!in_print_job_);
89
90 settings_ = settings;
91
92 return OK;
93}
94
95PrintingContext::Result PrintingContextNoSystemDialog::NewDocument(
[email protected]b5fa4ee2013-10-01 07:19:0796 const base::string16& document_name) {
[email protected]ea5e81d92011-11-08 18:45:1597 DCHECK(!in_print_job_);
98 in_print_job_ = true;
99
100 return OK;
101}
102
103PrintingContext::Result PrintingContextNoSystemDialog::NewPage() {
104 if (abort_printing_)
105 return CANCEL;
106 DCHECK(in_print_job_);
107
108 // Intentional No-op.
109
110 return OK;
111}
112
113PrintingContext::Result PrintingContextNoSystemDialog::PageDone() {
114 if (abort_printing_)
115 return CANCEL;
116 DCHECK(in_print_job_);
117
118 // Intentional No-op.
119
120 return OK;
121}
122
123PrintingContext::Result PrintingContextNoSystemDialog::DocumentDone() {
124 if (abort_printing_)
125 return CANCEL;
126 DCHECK(in_print_job_);
127
128 ResetSettings();
129 return OK;
130}
131
132void PrintingContextNoSystemDialog::Cancel() {
133 abort_printing_ = true;
134 in_print_job_ = false;
135}
136
137void PrintingContextNoSystemDialog::ReleaseContext() {
138 // Intentional No-op.
139}
140
141gfx::NativeDrawingContext PrintingContextNoSystemDialog::context() const {
142 // Intentional No-op.
143 return NULL;
144}
145
146} // namespace printing
147