[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 1 | // Copyright (c) 2011 The Chromium Authors. All rights reserved. |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 5 | #include "printing/printing_context_cairo.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 6 | |
[email protected] | 71dfff7 | 2011-07-23 01:27:27 | [diff] [blame] | 7 | // 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] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 10 | #include "base/logging.h" |
[email protected] | 89f5aa8c | 2011-03-21 20:58:44 | [diff] [blame] | 11 | #include "base/values.h" |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 12 | #include "printing/metafile.h" |
[email protected] | c97e5e8 | 2011-04-05 18:50:23 | [diff] [blame] | 13 | #include "printing/print_job_constants.h" |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 14 | #include "printing/units.h" |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 15 | |
| 16 | #if defined(OS_CHROMEOS) |
| 17 | #include <unicode/ulocdata.h> |
[email protected] | 2ed86fdf | 2011-04-19 20:57:03 | [diff] [blame] | 18 | #include "printing/print_settings_initializer_gtk.h" |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 19 | #else |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 20 | #include <gtk/gtk.h> |
| 21 | #include <gtk/gtkprintunixdialog.h> |
[email protected] | 499ededf | 2011-04-11 05:42:39 | [diff] [blame] | 22 | #include "printing/print_dialog_gtk_interface.h" |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 23 | #endif // defined(OS_CHROMEOS) |
| 24 | |
| 25 | #if !defined(OS_CHROMEOS) |
| 26 | namespace { |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 27 | // 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] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 30 | printing::PrintingContextCairo* context) = NULL; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 31 | } // namespace |
| 32 | #endif // !defined(OS_CHROMEOS) |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 33 | |
| 34 | namespace printing { |
| 35 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 36 | // static |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 37 | PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 38 | return static_cast<PrintingContext*>(new PrintingContextCairo(app_locale)); |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 39 | } |
| 40 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 41 | PrintingContextCairo::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] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 48 | } |
| 49 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 50 | PrintingContextCairo::~PrintingContextCairo() { |
| 51 | ReleaseContext(); |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 52 | |
[email protected] | 499ededf | 2011-04-11 05:42:39 | [diff] [blame] | 53 | #if !defined(OS_CHROMEOS) |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 54 | if (print_dialog_) |
| 55 | print_dialog_->ReleaseDialog(); |
[email protected] | 499ededf | 2011-04-11 05:42:39 | [diff] [blame] | 56 | #endif |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 59 | #if !defined(OS_CHROMEOS) |
| 60 | // static |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 61 | void PrintingContextCairo::SetCreatePrintDialogFunction( |
| 62 | PrintDialogGtkInterface* (*create_dialog_func)( |
| 63 | PrintingContextCairo* context)) { |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 64 | DCHECK(create_dialog_func); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 65 | DCHECK(!create_dialog_func_); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 66 | create_dialog_func_ = create_dialog_func; |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 67 | } |
| 68 | |
[email protected] | 7d748990 | 2011-04-11 21:54:06 | [diff] [blame] | 69 | void PrintingContextCairo::PrintDocument(const Metafile* metafile) { |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 70 | DCHECK(print_dialog_); |
| 71 | DCHECK(metafile); |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 72 | print_dialog_->PrintDocument(metafile, document_name_); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 73 | } |
| 74 | #endif // !defined(OS_CHROMEOS) |
| 75 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 76 | void PrintingContextCairo::AskUserForSettings( |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 77 | gfx::NativeView parent_view, |
[email protected] | 11f2b27 | 2010-02-15 04:41:15 | [diff] [blame] | 78 | int max_pages, |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 79 | bool has_selection, |
| 80 | PrintSettingsCallback* callback) { |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 81 | #if defined(OS_CHROMEOS) |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 82 | callback->Run(OK); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 83 | #else |
[email protected] | eaa389e | 2011-04-11 04:58:20 | [diff] [blame] | 84 | print_dialog_->ShowDialog(callback); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 85 | #endif // defined(OS_CHROMEOS) |
[email protected] | 11f2b27 | 2010-02-15 04:41:15 | [diff] [blame] | 86 | } |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 87 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 88 | PrintingContext::Result PrintingContextCairo::UseDefaultSettings() { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 89 | DCHECK(!in_print_job_); |
| 90 | |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 91 | ResetSettings(); |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 92 | #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] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 118 | |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 119 | physical_size_device_units.SetSize(width, height); |
| 120 | printable_area_device_units.SetRect( |
[email protected] | 2896dcc | 2011-03-15 19:54:10 | [diff] [blame] | 121 | 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] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 127 | |
| 128 | settings_.set_dpi(dpi); |
| 129 | settings_.SetPrinterPrintableArea(physical_size_device_units, |
| 130 | printable_area_device_units, |
| 131 | dpi); |
[email protected] | 2ed86fdf | 2011-04-19 20:57:03 | [diff] [blame] | 132 | #else |
| 133 | if (!print_dialog_) { |
| 134 | print_dialog_ = create_dialog_func_(this); |
| 135 | print_dialog_->AddRefToDialog(); |
| 136 | } |
| 137 | print_dialog_->UseDefaultSettings(); |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 138 | #endif // defined(OS_CHROMEOS) |
[email protected] | b719142 | 2010-09-21 19:18:05 | [diff] [blame] | 139 | |
| 140 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 141 | } |
| 142 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 143 | PrintingContext::Result PrintingContextCairo::UpdatePrinterSettings( |
[email protected] | c82d3f21 | 2011-03-22 01:18:30 | [diff] [blame] | 144 | const DictionaryValue& job_settings, const PageRanges& ranges) { |
[email protected] | 2ed86fdf | 2011-04-19 20:57:03 | [diff] [blame] | 145 | #if defined(OS_CHROMEOS) |
[email protected] | 71dfff7 | 2011-07-23 01:27:27 | [diff] [blame] | 146 | bool landscape = false; |
| 147 | |
| 148 | if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) |
| 149 | return OnError(); |
| 150 | |
[email protected] | ee09defb | 2011-09-27 20:33:45 | [diff] [blame] | 151 | if (settings_.dpi() == 0) |
| 152 | UseDefaultSettings(); |
| 153 | |
[email protected] | 71dfff7 | 2011-07-23 01:27:27 | [diff] [blame] | 154 | settings_.SetOrientation(landscape); |
| 155 | settings_.ranges = ranges; |
| 156 | |
[email protected] | 2ed86fdf | 2011-04-19 20:57:03 | [diff] [blame] | 157 | return OK; |
| 158 | #else |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 159 | DCHECK(!in_print_job_); |
| 160 | |
[email protected] | c7a90019 | 2011-09-22 05:31:00 | [diff] [blame] | 161 | if (!print_dialog_) { |
| 162 | print_dialog_ = create_dialog_func_(this); |
| 163 | print_dialog_->AddRefToDialog(); |
| 164 | } |
| 165 | |
[email protected] | 1c23b4e8 | 2011-10-15 22:30:48 | [diff] [blame] | 166 | if (!print_dialog_->UpdateSettings(job_settings, ranges, &settings_)) |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 167 | return OnError(); |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 168 | |
[email protected] | f242acf | 2011-03-25 19:46:21 | [diff] [blame] | 169 | return OK; |
[email protected] | 2ed86fdf | 2011-04-19 20:57:03 | [diff] [blame] | 170 | #endif |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 171 | } |
| 172 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 173 | PrintingContext::Result PrintingContextCairo::InitWithSettings( |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 174 | const PrintSettings& settings) { |
| 175 | DCHECK(!in_print_job_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 176 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 177 | settings_ = settings; |
| 178 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 179 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 180 | } |
| 181 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 182 | PrintingContext::Result PrintingContextCairo::NewDocument( |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 183 | const string16& document_name) { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 184 | DCHECK(!in_print_job_); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 185 | in_print_job_ = true; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 186 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 187 | #if !defined(OS_CHROMEOS) |
| 188 | document_name_ = document_name; |
| 189 | #endif // !defined(OS_CHROMEOS) |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 190 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 191 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 192 | } |
| 193 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 194 | PrintingContext::Result PrintingContextCairo::NewPage() { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 195 | if (abort_printing_) |
| 196 | return CANCEL; |
| 197 | DCHECK(in_print_job_); |
| 198 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 199 | // Intentional No-op. |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 200 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 201 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 202 | } |
| 203 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 204 | PrintingContext::Result PrintingContextCairo::PageDone() { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 205 | if (abort_printing_) |
| 206 | return CANCEL; |
| 207 | DCHECK(in_print_job_); |
| 208 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 209 | // Intentional No-op. |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 210 | |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 211 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 212 | } |
| 213 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 214 | PrintingContext::Result PrintingContextCairo::DocumentDone() { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 215 | if (abort_printing_) |
| 216 | return CANCEL; |
| 217 | DCHECK(in_print_job_); |
| 218 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 219 | ResetSettings(); |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 220 | return OK; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 221 | } |
| 222 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 223 | void PrintingContextCairo::Cancel() { |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 224 | abort_printing_ = true; |
| 225 | in_print_job_ = false; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 226 | } |
| 227 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 228 | void PrintingContextCairo::ReleaseContext() { |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 229 | // Intentional No-op. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 230 | } |
| 231 | |
| 232 | gfx::NativeDrawingContext PrintingContextCairo::context() const { |
[email protected] | 5cc4c42 | 2011-02-19 00:09:22 | [diff] [blame] | 233 | // Intentional No-op. |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 234 | return NULL; |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 235 | } |
| 236 | |
| 237 | } // namespace printing |