| OLD | NEW |
| (Empty) |
| 1 // 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_cairo.h" | |
| 6 | |
| 7 // TODO(abodenha@chromium.org) The number of #ifdefs here has gotten too | |
| 8 // large. Refactor this code into separate files for Linux and Chrome OS. | |
| 9 | |
| 10 #include "base/logging.h" | |
| 11 #include "base/values.h" | |
| 12 #include "printing/metafile.h" | |
| 13 #include "printing/print_job_constants.h" | |
| 14 #include "printing/units.h" | |
| 15 | |
| 16 #if defined(OS_CHROMEOS) | |
| 17 #include <unicode/ulocdata.h> | |
| 18 #include "printing/print_settings_initializer_gtk.h" | |
| 19 #else | |
| 20 #include <gtk/gtk.h> | |
| 21 #include <gtk/gtkprintunixdialog.h> | |
| 22 #include "printing/print_dialog_gtk_interface.h" | |
| 23 #endif // defined(OS_CHROMEOS) | |
| 24 | |
| 25 #if !defined(OS_CHROMEOS) | |
| 26 namespace { | |
| 27 // Function pointer for creating print dialogs. |callback| is only used when | |
| 28 // |show_dialog| is true. | |
| 29 static printing::PrintDialogGtkInterface* (*create_dialog_func_)( | |
| 30 printing::PrintingContextCairo* context) = NULL; | |
| 31 } // namespace | |
| 32 #endif // !defined(OS_CHROMEOS) | |
| 33 | |
| 34 namespace printing { | |
| 35 | |
| 36 // static | |
| 37 PrintingContext* PrintingContext::Create(const std::string& app_locale) { | |
| 38 return static_cast<PrintingContext*>(new PrintingContextCairo(app_locale)); | |
| 39 } | |
| 40 | |
| 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 | |
| 48 } | |
| 49 | |
| 50 PrintingContextCairo::~PrintingContextCairo() { | |
| 51 ReleaseContext(); | |
| 52 | |
| 53 #if !defined(OS_CHROMEOS) | |
| 54 if (print_dialog_) | |
| 55 print_dialog_->ReleaseDialog(); | |
| 56 #endif | |
| 57 } | |
| 58 | |
| 59 #if !defined(OS_CHROMEOS) | |
| 60 // static | |
| 61 void PrintingContextCairo::SetCreatePrintDialogFunction( | |
| 62 PrintDialogGtkInterface* (*create_dialog_func)( | |
| 63 PrintingContextCairo* context)) { | |
| 64 DCHECK(create_dialog_func); | |
| 65 DCHECK(!create_dialog_func_); | |
| 66 create_dialog_func_ = create_dialog_func; | |
| 67 } | |
| 68 | |
| 69 void PrintingContextCairo::PrintDocument(const Metafile* metafile) { | |
| 70 DCHECK(print_dialog_); | |
| 71 DCHECK(metafile); | |
| 72 print_dialog_->PrintDocument(metafile, document_name_); | |
| 73 } | |
| 74 #endif // !defined(OS_CHROMEOS) | |
| 75 | |
| 76 void PrintingContextCairo::AskUserForSettings( | |
| 77 gfx::NativeView parent_view, | |
| 78 int max_pages, | |
| 79 bool has_selection, | |
| 80 PrintSettingsCallback* callback) { | |
| 81 #if defined(OS_CHROMEOS) | |
| 82 callback->Run(OK); | |
| 83 #else | |
| 84 print_dialog_->ShowDialog(callback); | |
| 85 #endif // defined(OS_CHROMEOS) | |
| 86 } | |
| 87 | |
| 88 PrintingContext::Result PrintingContextCairo::UseDefaultSettings() { | |
| 89 DCHECK(!in_print_job_); | |
| 90 | |
| 91 ResetSettings(); | |
| 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 } | |
| 118 | |
| 119 physical_size_device_units.SetSize(width, height); | |
| 120 printable_area_device_units.SetRect( | |
| 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); | |
| 127 | |
| 128 settings_.set_dpi(dpi); | |
| 129 settings_.SetPrinterPrintableArea(physical_size_device_units, | |
| 130 printable_area_device_units, | |
| 131 dpi); | |
| 132 #else | |
| 133 if (!print_dialog_) { | |
| 134 print_dialog_ = create_dialog_func_(this); | |
| 135 print_dialog_->AddRefToDialog(); | |
| 136 } | |
| 137 print_dialog_->UseDefaultSettings(); | |
| 138 #endif // defined(OS_CHROMEOS) | |
| 139 | |
| 140 return OK; | |
| 141 } | |
| 142 | |
| 143 PrintingContext::Result PrintingContextCairo::UpdatePrinterSettings( | |
| 144 const DictionaryValue& job_settings, const PageRanges& ranges) { | |
| 145 #if defined(OS_CHROMEOS) | |
| 146 bool landscape = false; | |
| 147 | |
| 148 if (!job_settings.GetBoolean(kSettingLandscape, &landscape)) | |
| 149 return OnError(); | |
| 150 | |
| 151 if (settings_.dpi() == 0) | |
| 152 UseDefaultSettings(); | |
| 153 | |
| 154 settings_.SetOrientation(landscape); | |
| 155 settings_.ranges = ranges; | |
| 156 | |
| 157 return OK; | |
| 158 #else | |
| 159 DCHECK(!in_print_job_); | |
| 160 | |
| 161 if (!print_dialog_) { | |
| 162 print_dialog_ = create_dialog_func_(this); | |
| 163 print_dialog_->AddRefToDialog(); | |
| 164 } | |
| 165 | |
| 166 if (!print_dialog_->UpdateSettings(job_settings, ranges, &settings_)) | |
| 167 return OnError(); | |
| 168 | |
| 169 return OK; | |
| 170 #endif | |
| 171 } | |
| 172 | |
| 173 PrintingContext::Result PrintingContextCairo::InitWithSettings( | |
| 174 const PrintSettings& settings) { | |
| 175 DCHECK(!in_print_job_); | |
| 176 | |
| 177 settings_ = settings; | |
| 178 | |
| 179 return OK; | |
| 180 } | |
| 181 | |
| 182 PrintingContext::Result PrintingContextCairo::NewDocument( | |
| 183 const string16& document_name) { | |
| 184 DCHECK(!in_print_job_); | |
| 185 in_print_job_ = true; | |
| 186 | |
| 187 #if !defined(OS_CHROMEOS) | |
| 188 document_name_ = document_name; | |
| 189 #endif // !defined(OS_CHROMEOS) | |
| 190 | |
| 191 return OK; | |
| 192 } | |
| 193 | |
| 194 PrintingContext::Result PrintingContextCairo::NewPage() { | |
| 195 if (abort_printing_) | |
| 196 return CANCEL; | |
| 197 DCHECK(in_print_job_); | |
| 198 | |
| 199 // Intentional No-op. | |
| 200 | |
| 201 return OK; | |
| 202 } | |
| 203 | |
| 204 PrintingContext::Result PrintingContextCairo::PageDone() { | |
| 205 if (abort_printing_) | |
| 206 return CANCEL; | |
| 207 DCHECK(in_print_job_); | |
| 208 | |
| 209 // Intentional No-op. | |
| 210 | |
| 211 return OK; | |
| 212 } | |
| 213 | |
| 214 PrintingContext::Result PrintingContextCairo::DocumentDone() { | |
| 215 if (abort_printing_) | |
| 216 return CANCEL; | |
| 217 DCHECK(in_print_job_); | |
| 218 | |
| 219 ResetSettings(); | |
| 220 return OK; | |
| 221 } | |
| 222 | |
| 223 void PrintingContextCairo::Cancel() { | |
| 224 abort_printing_ = true; | |
| 225 in_print_job_ = false; | |
| 226 } | |
| 227 | |
| 228 void PrintingContextCairo::ReleaseContext() { | |
| 229 // Intentional No-op. | |
| 230 } | |
| 231 | |
| 232 gfx::NativeDrawingContext PrintingContextCairo::context() const { | |
| 233 // Intentional No-op. | |
| 234 return NULL; | |
| 235 } | |
| 236 | |
| 237 } // namespace printing | |
| OLD | NEW |