blob: 7ae10ddaad06a6f15e326d087809ee3a801b4813 [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
Vitaly Bukabd7c9812014-08-26 08:57:5418scoped_ptr<PrintingContext> PrintingContext::Create(Delegate* delegate) {
19 return make_scoped_ptr<PrintingContext>(
20 new PrintingContextNoSystemDialog(delegate));
[email protected]ea5e81d92011-11-08 18:45:1521}
22
Vitaly Bukabd7c9812014-08-26 08:57:5423PrintingContextNoSystemDialog::PrintingContextNoSystemDialog(Delegate* delegate)
24 : PrintingContext(delegate) {
[email protected]ea5e81d92011-11-08 18:45:1525}
26
27PrintingContextNoSystemDialog::~PrintingContextNoSystemDialog() {
28 ReleaseContext();
29}
30
31void PrintingContextNoSystemDialog::AskUserForSettings(
[email protected]ea5e81d92011-11-08 18:45:1532 int max_pages,
33 bool has_selection,
[email protected]abe48112011-11-19 01:58:3834 const PrintSettingsCallback& callback) {
[email protected]ea5e81d92011-11-08 18:45:1535 // We don't want to bring up a dialog here. Ever. Just signal the callback.
[email protected]abe48112011-11-19 01:58:3836 callback.Run(OK);
[email protected]ea5e81d92011-11-08 18:45:1537}
38
39PrintingContext::Result PrintingContextNoSystemDialog::UseDefaultSettings() {
40 DCHECK(!in_print_job_);
41
42 ResetSettings();
[email protected]4c9054b2013-11-04 18:34:2943 settings_.set_dpi(kDefaultPdfDpi);
44 gfx::Size physical_size = GetPdfPaperSizeDeviceUnits();
45 // Assume full page is printable for now.
46 gfx::Rect printable_area(0, 0, physical_size.width(), physical_size.height());
47 DCHECK_EQ(settings_.device_units_per_inch(), kDefaultPdfDpi);
48 settings_.SetPrinterPrintableArea(physical_size, printable_area, true);
49 return OK;
50}
51
52gfx::Size PrintingContextNoSystemDialog::GetPdfPaperSizeDeviceUnits() {
[email protected]ea5e81d92011-11-08 18:45:1553 int32_t width = 0;
54 int32_t height = 0;
55 UErrorCode error = U_ZERO_ERROR;
Vitaly Bukabd7c9812014-08-26 08:57:5456 ulocdata_getPaperSize(
57 delegate_->GetAppLocale().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(
vitalybuka92ab8ce2014-08-26 23:41:4578 bool external_preview,
79 bool show_system_dialog) {
80 DCHECK(!show_system_dialog);
[email protected]0c5e3022013-11-01 14:25:0181
82 if (settings_.dpi() == 0)
83 UseDefaultSettings();
84
[email protected]ea5e81d92011-11-08 18:45:1585 return OK;
86}
87
88PrintingContext::Result PrintingContextNoSystemDialog::InitWithSettings(
89 const PrintSettings& settings) {
90 DCHECK(!in_print_job_);
91
92 settings_ = settings;
93
94 return OK;
95}
96
97PrintingContext::Result PrintingContextNoSystemDialog::NewDocument(
[email protected]b5fa4ee2013-10-01 07:19:0798 const base::string16& document_name) {
[email protected]ea5e81d92011-11-08 18:45:1599 DCHECK(!in_print_job_);
100 in_print_job_ = true;
101
102 return OK;
103}
104
105PrintingContext::Result PrintingContextNoSystemDialog::NewPage() {
106 if (abort_printing_)
107 return CANCEL;
108 DCHECK(in_print_job_);
109
110 // Intentional No-op.
111
112 return OK;
113}
114
115PrintingContext::Result PrintingContextNoSystemDialog::PageDone() {
116 if (abort_printing_)
117 return CANCEL;
118 DCHECK(in_print_job_);
119
120 // Intentional No-op.
121
122 return OK;
123}
124
125PrintingContext::Result PrintingContextNoSystemDialog::DocumentDone() {
126 if (abort_printing_)
127 return CANCEL;
128 DCHECK(in_print_job_);
129
130 ResetSettings();
131 return OK;
132}
133
134void PrintingContextNoSystemDialog::Cancel() {
135 abort_printing_ = true;
136 in_print_job_ = false;
137}
138
139void PrintingContextNoSystemDialog::ReleaseContext() {
140 // Intentional No-op.
141}
142
143gfx::NativeDrawingContext PrintingContextNoSystemDialog::context() const {
144 // Intentional No-op.
145 return NULL;
146}
147
148} // namespace printing
149