blob: 81d8d92cbae3ffcd8f2c4eb3dc22edb29c7a3081 [file] [log] [blame]
[email protected]5cc4c422011-02-19 00:09:221// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]8ff1d422009-07-07 21:31:392// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
[email protected]51e8d9352010-10-06 22:21:175#include "printing/printing_context_cairo.h"
[email protected]8ff1d422009-07-07 21:31:396
[email protected]71dfff72011-07-23 01:27:277// TODO([email protected]) The number of #ifdefs here has gotten too
8// large. Refactor this code into separate files for Linux and Chrome OS.
9
[email protected]5cc4c422011-02-19 00:09:2210#include "base/logging.h"
[email protected]89f5aa8c2011-03-21 20:58:4411#include "base/values.h"
[email protected]7d7489902011-04-11 21:54:0612#include "printing/metafile.h"
[email protected]c97e5e82011-04-05 18:50:2313#include "printing/print_job_constants.h"
[email protected]c48bee22011-03-29 02:36:2614#include "printing/units.h"
[email protected]5cc4c422011-02-19 00:09:2215
16#if defined(OS_CHROMEOS)
17#include <unicode/ulocdata.h>
[email protected]2ed86fdf2011-04-19 20:57:0318#include "printing/print_settings_initializer_gtk.h"
[email protected]5cc4c422011-02-19 00:09:2219#else
[email protected]b7191422010-09-21 19:18:0520#include <gtk/gtk.h>
21#include <gtk/gtkprintunixdialog.h>
[email protected]499ededf2011-04-11 05:42:3922#include "printing/print_dialog_gtk_interface.h"
[email protected]5cc4c422011-02-19 00:09:2223#endif // defined(OS_CHROMEOS)
24
25#if !defined(OS_CHROMEOS)
26namespace {
[email protected]eaa389e2011-04-11 04:58:2027 // Function pointer for creating print dialogs. |callback| is only used when
28 // |show_dialog| is true.
29 static printing::PrintDialogGtkInterface* (*create_dialog_func_)(
[email protected]5cc4c422011-02-19 00:09:2230 printing::PrintingContextCairo* context) = NULL;
[email protected]5cc4c422011-02-19 00:09:2231} // namespace
32#endif // !defined(OS_CHROMEOS)
[email protected]8ff1d422009-07-07 21:31:3933
34namespace printing {
35
[email protected]51e8d9352010-10-06 22:21:1736// static
[email protected]eaa389e2011-04-11 04:58:2037PrintingContext* PrintingContext::Create(const std::string& app_locale) {
[email protected]ee5f36e42010-12-03 22:40:3738 return static_cast<PrintingContext*>(new PrintingContextCairo(app_locale));
[email protected]8ff1d422009-07-07 21:31:3939}
40
[email protected]5cc4c422011-02-19 00:09:2241PrintingContextCairo::PrintingContextCairo(const std::string& app_locale)
42#if defined(OS_CHROMEOS)
43 : PrintingContext(app_locale) {
44#else
45 : PrintingContext(app_locale),
46 print_dialog_(NULL) {
47#endif
[email protected]8ff1d422009-07-07 21:31:3948}
49
[email protected]51e8d9352010-10-06 22:21:1750PrintingContextCairo::~PrintingContextCairo() {
51 ReleaseContext();
[email protected]eaa389e2011-04-11 04:58:2052
[email protected]499ededf2011-04-11 05:42:3953#if !defined(OS_CHROMEOS)
[email protected]eaa389e2011-04-11 04:58:2054 if (print_dialog_)
55 print_dialog_->ReleaseDialog();
[email protected]499ededf2011-04-11 05:42:3956#endif
[email protected]51e8d9352010-10-06 22:21:1757}
58
[email protected]5cc4c422011-02-19 00:09:2259#if !defined(OS_CHROMEOS)
60// static
[email protected]eaa389e2011-04-11 04:58:2061void PrintingContextCairo::SetCreatePrintDialogFunction(
62 PrintDialogGtkInterface* (*create_dialog_func)(
63 PrintingContextCairo* context)) {
[email protected]5cc4c422011-02-19 00:09:2264 DCHECK(create_dialog_func);
[email protected]5cc4c422011-02-19 00:09:2265 DCHECK(!create_dialog_func_);
[email protected]5cc4c422011-02-19 00:09:2266 create_dialog_func_ = create_dialog_func;
[email protected]5cc4c422011-02-19 00:09:2267}
68
[email protected]7d7489902011-04-11 21:54:0669void PrintingContextCairo::PrintDocument(const Metafile* metafile) {
[email protected]5cc4c422011-02-19 00:09:2270 DCHECK(print_dialog_);
71 DCHECK(metafile);
[email protected]eaa389e2011-04-11 04:58:2072 print_dialog_->PrintDocument(metafile, document_name_);
[email protected]5cc4c422011-02-19 00:09:2273}
74#endif // !defined(OS_CHROMEOS)
75
[email protected]51e8d9352010-10-06 22:21:1776void PrintingContextCairo::AskUserForSettings(
[email protected]fc7904622010-05-12 19:26:4077 gfx::NativeView parent_view,
[email protected]11f2b272010-02-15 04:41:1578 int max_pages,
[email protected]b7191422010-09-21 19:18:0579 bool has_selection,
80 PrintSettingsCallback* callback) {
[email protected]5cc4c422011-02-19 00:09:2281#if defined(OS_CHROMEOS)
[email protected]b7191422010-09-21 19:18:0582 callback->Run(OK);
[email protected]5cc4c422011-02-19 00:09:2283#else
[email protected]eaa389e2011-04-11 04:58:2084 print_dialog_->ShowDialog(callback);
[email protected]5cc4c422011-02-19 00:09:2285#endif // defined(OS_CHROMEOS)
[email protected]11f2b272010-02-15 04:41:1586}
[email protected]8ff1d422009-07-07 21:31:3987
[email protected]51e8d9352010-10-06 22:21:1788PrintingContext::Result PrintingContextCairo::UseDefaultSettings() {
[email protected]8ff1d422009-07-07 21:31:3989 DCHECK(!in_print_job_);
90
[email protected]b7191422010-09-21 19:18:0591 ResetSettings();
[email protected]ee5f36e42010-12-03 22:40:3792#if defined(OS_CHROMEOS)
93 // For Chrome OS use default values based on the app locale rather than rely
94 // on GTK. Note that relying on the app locale does not work well if it is
95 // different from the system locale, e.g. a user using Chinese ChromeOS in the
96 // US. Eventually we need to get the defaults from the printer.
97 // TODO(sanjeevr): We need a better feedback loop between the cloud print
98 // dialog and this code.
99 int dpi = 300;
100 gfx::Size physical_size_device_units;
101 gfx::Rect printable_area_device_units;
102 int32_t width = 0;
103 int32_t height = 0;
104 UErrorCode error = U_ZERO_ERROR;
105 ulocdata_getPaperSize(app_locale_.c_str(), &height, &width, &error);
106 if (error != U_ZERO_ERROR) {
107 // If the call failed, assume a paper size of 8.5 x 11 inches.
108 LOG(WARNING) << "ulocdata_getPaperSize failed, using 8.5 x 11, error: "
109 << error;
110 width = static_cast<int>(8.5 * dpi);
111 height = static_cast<int>(11 * dpi);
112 } else {
113 // ulocdata_getPaperSize returns the width and height in mm.
114 // Convert this to pixels based on the dpi.
115 width = static_cast<int>(ConvertUnitDouble(width, 25.4, 1.0) * dpi);
116 height = static_cast<int>(ConvertUnitDouble(height, 25.4, 1.0) * dpi);
117 }
[email protected]8ff1d422009-07-07 21:31:39118
[email protected]ee5f36e42010-12-03 22:40:37119 physical_size_device_units.SetSize(width, height);
120 printable_area_device_units.SetRect(
[email protected]2896dcc2011-03-15 19:54:10121 static_cast<int>(PrintSettingsInitializerGtk::kLeftMarginInInch * dpi),
122 static_cast<int>(PrintSettingsInitializerGtk::kTopMarginInInch * dpi),
123 width - (PrintSettingsInitializerGtk::kLeftMarginInInch +
124 PrintSettingsInitializerGtk::kRightMarginInInch) * dpi,
125 height - (PrintSettingsInitializerGtk::kTopMarginInInch +
126 PrintSettingsInitializerGtk::kBottomMarginInInch) * dpi);
[email protected]ee5f36e42010-12-03 22:40:37127
128 settings_.set_dpi(dpi);
129 settings_.SetPrinterPrintableArea(physical_size_device_units,
130 printable_area_device_units,
131 dpi);
[email protected]2ed86fdf2011-04-19 20:57:03132#else
133 if (!print_dialog_) {
134 print_dialog_ = create_dialog_func_(this);
135 print_dialog_->AddRefToDialog();
136 }
137 print_dialog_->UseDefaultSettings();
[email protected]ee5f36e42010-12-03 22:40:37138#endif // defined(OS_CHROMEOS)
[email protected]b7191422010-09-21 19:18:05139
140 return OK;
[email protected]8ff1d422009-07-07 21:31:39141}
142
[email protected]55b23a02011-08-17 23:09:36143PrintingContext::Result PrintingContextCairo::UpdatePrinterSettings(
[email protected]c82d3f212011-03-22 01:18:30144 const DictionaryValue& job_settings, const PageRanges& ranges) {
[email protected]2ed86fdf2011-04-19 20:57:03145#if defined(OS_CHROMEOS)
[email protected]71dfff72011-07-23 01:27:27146 bool landscape = false;
147
148 if (!job_settings.GetBoolean(kSettingLandscape, &landscape))
149 return OnError();
150
[email protected]ee09defb2011-09-27 20:33:45151 if (settings_.dpi() == 0)
152 UseDefaultSettings();
153
[email protected]71dfff72011-07-23 01:27:27154 settings_.SetOrientation(landscape);
155 settings_.ranges = ranges;
156
[email protected]2ed86fdf2011-04-19 20:57:03157 return OK;
158#else
[email protected]7868ecab2011-03-05 00:12:53159 DCHECK(!in_print_job_);
160
[email protected]c7a900192011-09-22 05:31:00161 if (!print_dialog_) {
162 print_dialog_ = create_dialog_func_(this);
163 print_dialog_->AddRefToDialog();
164 }
165
[email protected]1c23b4e82011-10-15 22:30:48166 if (!print_dialog_->UpdateSettings(job_settings, ranges, &settings_))
[email protected]c48bee22011-03-29 02:36:26167 return OnError();
[email protected]c48bee22011-03-29 02:36:26168
[email protected]f242acf2011-03-25 19:46:21169 return OK;
[email protected]2ed86fdf2011-04-19 20:57:03170#endif
[email protected]7868ecab2011-03-05 00:12:53171}
172
[email protected]51e8d9352010-10-06 22:21:17173PrintingContext::Result PrintingContextCairo::InitWithSettings(
[email protected]8ff1d422009-07-07 21:31:39174 const PrintSettings& settings) {
175 DCHECK(!in_print_job_);
[email protected]4993f342010-10-26 17:57:52176
[email protected]8ff1d422009-07-07 21:31:39177 settings_ = settings;
178
[email protected]5cc4c422011-02-19 00:09:22179 return OK;
[email protected]8ff1d422009-07-07 21:31:39180}
181
[email protected]51e8d9352010-10-06 22:21:17182PrintingContext::Result PrintingContextCairo::NewDocument(
[email protected]d9d42992010-09-13 19:39:19183 const string16& document_name) {
[email protected]8ff1d422009-07-07 21:31:39184 DCHECK(!in_print_job_);
[email protected]5cc4c422011-02-19 00:09:22185 in_print_job_ = true;
[email protected]8ff1d422009-07-07 21:31:39186
[email protected]5cc4c422011-02-19 00:09:22187#if !defined(OS_CHROMEOS)
188 document_name_ = document_name;
189#endif // !defined(OS_CHROMEOS)
[email protected]8ff1d422009-07-07 21:31:39190
[email protected]5cc4c422011-02-19 00:09:22191 return OK;
[email protected]8ff1d422009-07-07 21:31:39192}
193
[email protected]51e8d9352010-10-06 22:21:17194PrintingContext::Result PrintingContextCairo::NewPage() {
[email protected]8ff1d422009-07-07 21:31:39195 if (abort_printing_)
196 return CANCEL;
197 DCHECK(in_print_job_);
198
[email protected]5cc4c422011-02-19 00:09:22199 // Intentional No-op.
[email protected]8ff1d422009-07-07 21:31:39200
[email protected]5cc4c422011-02-19 00:09:22201 return OK;
[email protected]8ff1d422009-07-07 21:31:39202}
203
[email protected]51e8d9352010-10-06 22:21:17204PrintingContext::Result PrintingContextCairo::PageDone() {
[email protected]8ff1d422009-07-07 21:31:39205 if (abort_printing_)
206 return CANCEL;
207 DCHECK(in_print_job_);
208
[email protected]5cc4c422011-02-19 00:09:22209 // Intentional No-op.
[email protected]8ff1d422009-07-07 21:31:39210
[email protected]5cc4c422011-02-19 00:09:22211 return OK;
[email protected]8ff1d422009-07-07 21:31:39212}
213
[email protected]51e8d9352010-10-06 22:21:17214PrintingContext::Result PrintingContextCairo::DocumentDone() {
[email protected]8ff1d422009-07-07 21:31:39215 if (abort_printing_)
216 return CANCEL;
217 DCHECK(in_print_job_);
218
[email protected]8ff1d422009-07-07 21:31:39219 ResetSettings();
[email protected]5cc4c422011-02-19 00:09:22220 return OK;
[email protected]8ff1d422009-07-07 21:31:39221}
222
[email protected]51e8d9352010-10-06 22:21:17223void PrintingContextCairo::Cancel() {
[email protected]8ff1d422009-07-07 21:31:39224 abort_printing_ = true;
225 in_print_job_ = false;
[email protected]8ff1d422009-07-07 21:31:39226}
227
[email protected]51e8d9352010-10-06 22:21:17228void PrintingContextCairo::ReleaseContext() {
[email protected]5cc4c422011-02-19 00:09:22229 // Intentional No-op.
[email protected]51e8d9352010-10-06 22:21:17230}
231
232gfx::NativeDrawingContext PrintingContextCairo::context() const {
[email protected]5cc4c422011-02-19 00:09:22233 // Intentional No-op.
[email protected]51e8d9352010-10-06 22:21:17234 return NULL;
[email protected]8ff1d422009-07-07 21:31:39235}
236
237} // namespace printing