[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 5 | #include "printing/printing_context_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <winspool.h> |
| 8 | |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 11 | #include "base/i18n/file_util_icu.h" |
[email protected] | 260d157 | 2009-10-09 18:50:35 | [diff] [blame] | 12 | #include "base/i18n/time_formatting.h" |
[email protected] | dcccb94 | 2009-02-01 18:23:00 | [diff] [blame] | 13 | #include "base/message_loop.h" |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | dcccb94 | 2009-02-01 18:23:00 | [diff] [blame] | 15 | #include "base/time.h" |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 16 | #include "base/utf_string_conversions.h" |
[email protected] | 89f5aa8c | 2011-03-21 20:58:44 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 18 | #include "base/win/metro.h" |
[email protected] | 09083eb | 2012-09-20 05:58:43 | [diff] [blame] | 19 | #include "printing/backend/print_backend.h" |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 20 | #include "printing/backend/printing_info_win.h" |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 21 | #include "printing/backend/win_helper.h" |
[email protected] | c97e5e8 | 2011-04-05 18:50:23 | [diff] [blame] | 22 | #include "printing/print_job_constants.h" |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 23 | #include "printing/print_settings_initializer_win.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 24 | #include "printing/printed_document.h" |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 25 | #include "printing/units.h" |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 26 | #include "skia/ext/platform_device.h" |
[email protected] | bd04f249 | 2012-12-06 00:40:31 | [diff] [blame] | 27 | #include "win8/util/win8_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 29 | using base::Time; |
| 30 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 31 | namespace { |
| 32 | |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 33 | // Constants for setting default PDF settings. |
| 34 | const int kPDFDpi = 300; // 300 dpi |
| 35 | // LETTER: 8.5 x 11 inches |
| 36 | const int kPDFLetterWidth = 8.5 * kPDFDpi; |
| 37 | const int kPDFLetterHeight = 11 * kPDFDpi; |
| 38 | // LEGAL: 8.5 x 14 inches |
| 39 | const int kPDFLegalWidth = 8.5 * kPDFDpi; |
| 40 | const int kPDFLegalHeight = 14 * kPDFDpi; |
| 41 | // A4: 8.27 x 11.69 inches |
| 42 | const int kPDFA4Width = 8.27 * kPDFDpi; |
| 43 | const int kPDFA4Height = 11.69 * kPDFDpi; |
| 44 | // A3: 11.69 x 16.54 inches |
| 45 | const int kPDFA3Width = 11.69 * kPDFDpi; |
| 46 | const int kPDFA3Height = 16.54 * kPDFDpi; |
| 47 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 48 | } // anonymous namespace |
| 49 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | namespace printing { |
| 51 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 52 | class PrintingContextWin::CallbackHandler : public IPrintDialogCallback, |
| 53 | public IObjectWithSite { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | public: |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 55 | CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | : owner_(owner), |
| 57 | owner_hwnd_(owner_hwnd), |
| 58 | services_(NULL) { |
| 59 | } |
| 60 | |
| 61 | ~CallbackHandler() { |
| 62 | if (services_) |
| 63 | services_->Release(); |
| 64 | } |
| 65 | |
| 66 | IUnknown* ToIUnknown() { |
| 67 | return static_cast<IUnknown*>(static_cast<IPrintDialogCallback*>(this)); |
| 68 | } |
| 69 | |
| 70 | // IUnknown |
| 71 | virtual HRESULT WINAPI QueryInterface(REFIID riid, void**object) { |
| 72 | if (riid == IID_IUnknown) { |
| 73 | *object = ToIUnknown(); |
| 74 | } else if (riid == IID_IPrintDialogCallback) { |
| 75 | *object = static_cast<IPrintDialogCallback*>(this); |
| 76 | } else if (riid == IID_IObjectWithSite) { |
| 77 | *object = static_cast<IObjectWithSite*>(this); |
| 78 | } else { |
| 79 | return E_NOINTERFACE; |
| 80 | } |
| 81 | return S_OK; |
| 82 | } |
| 83 | |
| 84 | // No real ref counting. |
| 85 | virtual ULONG WINAPI AddRef() { |
| 86 | return 1; |
| 87 | } |
| 88 | virtual ULONG WINAPI Release() { |
| 89 | return 1; |
| 90 | } |
| 91 | |
| 92 | // IPrintDialogCallback methods |
| 93 | virtual HRESULT WINAPI InitDone() { |
| 94 | return S_OK; |
| 95 | } |
| 96 | |
| 97 | virtual HRESULT WINAPI SelectionChange() { |
| 98 | if (services_) { |
| 99 | // TODO(maruel): Get the devmode for the new printer with |
| 100 | // services_->GetCurrentDevMode(&devmode, &size), send that information |
| 101 | // back to our client and continue. The client needs to recalculate the |
| 102 | // number of rendered pages and send back this information here. |
| 103 | } |
| 104 | return S_OK; |
| 105 | } |
| 106 | |
| 107 | virtual HRESULT WINAPI HandleMessage(HWND dialog, |
| 108 | UINT message, |
| 109 | WPARAM wparam, |
| 110 | LPARAM lparam, |
| 111 | LRESULT* result) { |
| 112 | // Cheap way to retrieve the window handle. |
| 113 | if (!owner_.dialog_box_) { |
| 114 | // The handle we receive is the one of the groupbox in the General tab. We |
| 115 | // need to get the grand-father to get the dialog box handle. |
| 116 | owner_.dialog_box_ = GetAncestor(dialog, GA_ROOT); |
| 117 | // Trick to enable the owner window. This can cause issues with navigation |
| 118 | // events so it may have to be disabled if we don't fix the side-effects. |
| 119 | EnableWindow(owner_hwnd_, TRUE); |
| 120 | } |
| 121 | return S_FALSE; |
| 122 | } |
| 123 | |
| 124 | virtual HRESULT WINAPI SetSite(IUnknown* site) { |
| 125 | if (!site) { |
| 126 | DCHECK(services_); |
| 127 | services_->Release(); |
| 128 | services_ = NULL; |
| 129 | // The dialog box is destroying, PrintJob::Worker don't need the handle |
| 130 | // anymore. |
| 131 | owner_.dialog_box_ = NULL; |
| 132 | } else { |
| 133 | DCHECK(services_ == NULL); |
| 134 | HRESULT hr = site->QueryInterface(IID_IPrintDialogServices, |
| 135 | reinterpret_cast<void**>(&services_)); |
| 136 | DCHECK(SUCCEEDED(hr)); |
| 137 | } |
| 138 | return S_OK; |
| 139 | } |
| 140 | |
| 141 | virtual HRESULT WINAPI GetSite(REFIID riid, void** site) { |
| 142 | return E_NOTIMPL; |
| 143 | } |
| 144 | |
| 145 | private: |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 146 | PrintingContextWin& owner_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 147 | HWND owner_hwnd_; |
| 148 | IPrintDialogServices* services_; |
| 149 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 150 | DISALLOW_COPY_AND_ASSIGN(CallbackHandler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 151 | }; |
| 152 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 153 | // static |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 154 | PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 155 | return static_cast<PrintingContext*>(new PrintingContextWin(app_locale)); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 156 | } |
| 157 | |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 158 | PrintingContextWin::PrintingContextWin(const std::string& app_locale) |
| 159 | : PrintingContext(app_locale), |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 160 | context_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | dialog_box_(NULL), |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 162 | print_dialog_func_(&PrintDlgEx) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 163 | } |
| 164 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 165 | PrintingContextWin::~PrintingContextWin() { |
| 166 | ReleaseContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 167 | } |
| 168 | |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 169 | void PrintingContextWin::AskUserForSettings( |
| 170 | gfx::NativeView view, int max_pages, bool has_selection, |
| 171 | const PrintSettingsCallback& callback) { |
[email protected] | 94fbaa4 | 2011-09-07 20:09:54 | [diff] [blame] | 172 | #if !defined(USE_AURA) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 173 | DCHECK(!in_print_job_); |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 174 | |
[email protected] | bd04f249 | 2012-12-06 00:40:31 | [diff] [blame] | 175 | if (win8::IsSingleWindowMetroMode()) { |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 176 | // The system dialog can not be opened while running in Metro. |
| 177 | // But we can programatically launch the Metro print device charm though. |
| 178 | HMODULE metro_module = base::win::GetMetroModule(); |
| 179 | if (metro_module != NULL) { |
| 180 | typedef void (*MetroShowPrintUI)(); |
| 181 | MetroShowPrintUI metro_show_print_ui = |
| 182 | reinterpret_cast<MetroShowPrintUI>( |
| 183 | ::GetProcAddress(metro_module, "MetroShowPrintUI")); |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 184 | if (metro_show_print_ui) { |
| 185 | // TODO(mad): Remove this once we can send user metrics from the metro |
| 186 | // driver. crbug.com/142330 |
| 187 | UMA_HISTOGRAM_ENUMERATION("Metro.Print", 1, 2); |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 188 | metro_show_print_ui(); |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 189 | } |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 190 | } |
| 191 | return callback.Run(CANCEL); |
| 192 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 193 | dialog_box_dismissed_ = false; |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 194 | |
| 195 | HWND window; |
| 196 | if (!view || !IsWindow(view)) { |
| 197 | // TODO(maruel): bug 1214347 Get the right browser window instead. |
| 198 | window = GetDesktopWindow(); |
| 199 | } else { |
| 200 | window = GetAncestor(view, GA_ROOTOWNER); |
| 201 | } |
| 202 | DCHECK(window); |
| 203 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | // Show the OS-dependent dialog box. |
| 205 | // If the user press |
| 206 | // - OK, the settings are reset and reinitialized with the new settings. OK is |
| 207 | // returned. |
| 208 | // - Apply then Cancel, the settings are reset and reinitialized with the new |
| 209 | // settings. CANCEL is returned. |
| 210 | // - Cancel, the settings are not changed, the previous setting, if it was |
| 211 | // initialized before, are kept. CANCEL is returned. |
| 212 | // On failure, the settings are reset and FAILED is returned. |
| 213 | PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) }; |
| 214 | dialog_options.hwndOwner = window; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 215 | // Disable options we don't support currently. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 216 | // TODO(maruel): Reuse the previously loaded settings! |
| 217 | dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 218 | PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE; |
| 219 | if (!has_selection) |
| 220 | dialog_options.Flags |= PD_NOSELECTION; |
| 221 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | PRINTPAGERANGE ranges[32]; |
| 223 | dialog_options.nStartPage = START_PAGE_GENERAL; |
| 224 | if (max_pages) { |
| 225 | // Default initialize to print all the pages. |
| 226 | memset(ranges, 0, sizeof(ranges)); |
| 227 | ranges[0].nFromPage = 1; |
| 228 | ranges[0].nToPage = max_pages; |
| 229 | dialog_options.nPageRanges = 1; |
| 230 | dialog_options.nMaxPageRanges = arraysize(ranges); |
[email protected] | 3a0e4a3 | 2009-06-09 19:07:05 | [diff] [blame] | 231 | dialog_options.nMinPage = 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 232 | dialog_options.nMaxPage = max_pages; |
| 233 | dialog_options.lpPageRanges = ranges; |
| 234 | } else { |
| 235 | // No need to bother, we don't know how many pages are available. |
| 236 | dialog_options.Flags |= PD_NOPAGENUMS; |
| 237 | } |
| 238 | |
[email protected] | 128bec3c | 2011-01-20 22:57:35 | [diff] [blame] | 239 | if ((*print_dialog_func_)(&dialog_options) != S_OK) { |
| 240 | ResetSettings(); |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 241 | callback.Run(FAILED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 242 | } |
[email protected] | 128bec3c | 2011-01-20 22:57:35 | [diff] [blame] | 243 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 244 | // TODO(maruel): Support PD_PRINTTOFILE. |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 245 | callback.Run(ParseDialogResultEx(dialog_options)); |
[email protected] | 94fbaa4 | 2011-09-07 20:09:54 | [diff] [blame] | 246 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 247 | } |
| 248 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 249 | PrintingContext::Result PrintingContextWin::UseDefaultSettings() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 250 | DCHECK(!in_print_job_); |
| 251 | |
| 252 | PRINTDLG dialog_options = { sizeof(PRINTDLG) }; |
| 253 | dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 254 | if (PrintDlg(&dialog_options)) |
| 255 | return ParseDialogResult(dialog_options); |
| 256 | |
| 257 | // No default printer configured, do we have any printers at all? |
| 258 | DWORD bytes_needed = 0; |
| 259 | DWORD count_returned = 0; |
| 260 | (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 261 | NULL, 2, NULL, 0, &bytes_needed, &count_returned); |
| 262 | if (bytes_needed) { |
| 263 | DCHECK(bytes_needed >= count_returned * sizeof(PRINTER_INFO_2)); |
[email protected] | fb3a97c | 2013-04-11 04:48:01 | [diff] [blame] | 264 | scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 265 | BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 266 | NULL, 2, printer_info_buffer.get(), |
| 267 | bytes_needed, &bytes_needed, |
| 268 | &count_returned); |
| 269 | if (ret && count_returned) { // have printers |
| 270 | // Open the first successfully found printer. |
[email protected] | a5e0ef57 | 2011-04-21 18:38:54 | [diff] [blame] | 271 | for (DWORD count = 0; count < count_returned; ++count) { |
| 272 | PRINTER_INFO_2* info_2 = reinterpret_cast<PRINTER_INFO_2*>( |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 273 | printer_info_buffer.get() + count * sizeof(PRINTER_INFO_2)); |
| 274 | std::wstring printer_name = info_2->pPrinterName; |
| 275 | if (info_2->pDevMode == NULL || printer_name.length() == 0) |
| 276 | continue; |
| 277 | if (!AllocateContext(printer_name, info_2->pDevMode, &context_)) |
| 278 | break; |
| 279 | if (InitializeSettings(*info_2->pDevMode, printer_name, |
[email protected] | a5e0ef57 | 2011-04-21 18:38:54 | [diff] [blame] | 280 | NULL, 0, false)) { |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 281 | break; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 282 | } |
[email protected] | a5e0ef57 | 2011-04-21 18:38:54 | [diff] [blame] | 283 | ReleaseContext(); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 284 | } |
| 285 | if (context_) |
| 286 | return OK; |
| 287 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 288 | } |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 289 | |
| 290 | ResetSettings(); |
| 291 | return FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 292 | } |
| 293 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 294 | PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 295 | const DictionaryValue& job_settings, |
| 296 | const PageRanges& ranges) { |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 297 | DCHECK(!in_print_job_); |
| 298 | |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 299 | bool collate; |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 300 | int color; |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 301 | bool landscape; |
| 302 | bool print_to_pdf; |
[email protected] | 737cfb2 | 2011-10-04 18:06:29 | [diff] [blame] | 303 | bool is_cloud_dialog; |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 304 | int copies; |
| 305 | int duplex_mode; |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 306 | string16 device_name; |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 307 | |
| 308 | if (!job_settings.GetBoolean(kSettingLandscape, &landscape) || |
| 309 | !job_settings.GetBoolean(kSettingCollate, &collate) || |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 310 | !job_settings.GetInteger(kSettingColor, &color) || |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 311 | !job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) || |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 312 | !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) || |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 313 | !job_settings.GetInteger(kSettingCopies, &copies) || |
[email protected] | 737cfb2 | 2011-10-04 18:06:29 | [diff] [blame] | 314 | !job_settings.GetString(kSettingDeviceName, &device_name) || |
| 315 | !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) { |
[email protected] | c48bee2 | 2011-03-29 02:36:26 | [diff] [blame] | 316 | return OnError(); |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 317 | } |
[email protected] | 37a401bf | 2011-03-31 16:12:36 | [diff] [blame] | 318 | |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 319 | bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId); |
[email protected] | 71dfff7 | 2011-07-23 01:27:27 | [diff] [blame] | 320 | |
[email protected] | 737cfb2 | 2011-10-04 18:06:29 | [diff] [blame] | 321 | if (print_to_pdf || print_to_cloud || is_cloud_dialog) { |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 322 | // Default fallback to Letter size. |
| 323 | gfx::Size paper_size; |
| 324 | gfx::Rect paper_rect; |
| 325 | paper_size.SetSize(kPDFLetterWidth, kPDFLetterHeight); |
| 326 | |
| 327 | // Get settings from locale. Paper type buffer length is at most 4. |
| 328 | const int paper_type_buffer_len = 4; |
| 329 | wchar_t paper_type_buffer[paper_type_buffer_len] = {0}; |
| 330 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer, |
| 331 | paper_type_buffer_len); |
| 332 | if (wcslen(paper_type_buffer)) { // The call succeeded. |
| 333 | int paper_code = _wtoi(paper_type_buffer); |
| 334 | switch (paper_code) { |
| 335 | case DMPAPER_LEGAL: |
| 336 | paper_size.SetSize(kPDFLegalWidth, kPDFLegalHeight); |
| 337 | break; |
| 338 | case DMPAPER_A4: |
| 339 | paper_size.SetSize(kPDFA4Width, kPDFA4Height); |
| 340 | break; |
| 341 | case DMPAPER_A3: |
| 342 | paper_size.SetSize(kPDFA3Width, kPDFA3Height); |
| 343 | break; |
| 344 | default: // DMPAPER_LETTER is used for default fallback. |
| 345 | break; |
| 346 | } |
| 347 | } |
| 348 | paper_rect.SetRect(0, 0, paper_size.width(), paper_size.height()); |
| 349 | settings_.SetPrinterPrintableArea(paper_size, paper_rect, kPDFDpi); |
| 350 | settings_.set_dpi(kPDFDpi); |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 351 | settings_.SetOrientation(landscape); |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 352 | settings_.ranges = ranges; |
| 353 | return OK; |
| 354 | } |
| 355 | |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 356 | ScopedPrinterHandle printer; |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 357 | LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str()); |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 358 | if (!OpenPrinter(device_name_wide, printer.Receive(), NULL)) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 359 | return OnError(); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 360 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 361 | // Make printer changes local to Chrome. |
| 362 | // See MSDN documentation regarding DocumentProperties. |
[email protected] | fb3a97c | 2013-04-11 04:48:01 | [diff] [blame] | 363 | scoped_ptr<uint8[]> buffer; |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 364 | DEVMODE* dev_mode = NULL; |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 365 | LONG buffer_size = DocumentProperties(NULL, printer, device_name_wide, |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 366 | NULL, NULL, 0); |
[email protected] | bcf6342 | 2011-06-14 18:57:39 | [diff] [blame] | 367 | if (buffer_size > 0) { |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 368 | buffer.reset(new uint8[buffer_size]); |
| 369 | memset(buffer.get(), 0, buffer_size); |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 370 | if (DocumentProperties(NULL, printer, device_name_wide, |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 371 | reinterpret_cast<PDEVMODE>(buffer.get()), NULL, |
| 372 | DM_OUT_BUFFER) == IDOK) { |
| 373 | dev_mode = reinterpret_cast<PDEVMODE>(buffer.get()); |
| 374 | } |
| 375 | } |
| 376 | if (dev_mode == NULL) { |
| 377 | buffer.reset(); |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 378 | return OnError(); |
| 379 | } |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 380 | |
[email protected] | d91db11 | 2011-10-18 20:58:51 | [diff] [blame] | 381 | if (color == GRAY) |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 382 | dev_mode->dmColor = DMCOLOR_MONOCHROME; |
| 383 | else |
| 384 | dev_mode->dmColor = DMCOLOR_COLOR; |
| 385 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 386 | dev_mode->dmCopies = std::max(copies, 1); |
| 387 | if (dev_mode->dmCopies > 1) // do not change collate unless multiple copies |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 388 | dev_mode->dmCollate = collate ? DMCOLLATE_TRUE : DMCOLLATE_FALSE; |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 389 | switch (duplex_mode) { |
| 390 | case LONG_EDGE: |
| 391 | dev_mode->dmDuplex = DMDUP_VERTICAL; |
| 392 | break; |
| 393 | case SHORT_EDGE: |
| 394 | dev_mode->dmDuplex = DMDUP_HORIZONTAL; |
| 395 | break; |
[email protected] | dc8373d | 2011-09-08 21:32:35 | [diff] [blame] | 396 | case SIMPLEX: |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 397 | dev_mode->dmDuplex = DMDUP_SIMPLEX; |
| 398 | break; |
[email protected] | dc8373d | 2011-09-08 21:32:35 | [diff] [blame] | 399 | default: // UNKNOWN_DUPLEX_MODE |
| 400 | break; |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 401 | } |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 402 | dev_mode->dmOrientation = landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT; |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 403 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 404 | // Update data using DocumentProperties. |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 405 | if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode, |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 406 | DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) { |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 407 | return OnError(); |
| 408 | } |
| 409 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 410 | // Set printer then refresh printer settings. |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 411 | if (!AllocateContext(device_name, dev_mode, &context_)) { |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 412 | return OnError(); |
| 413 | } |
| 414 | PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode, |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 415 | ranges, device_name, |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 416 | false, &settings_); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 417 | return OK; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 418 | } |
| 419 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 420 | PrintingContext::Result PrintingContextWin::InitWithSettings( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 421 | const PrintSettings& settings) { |
| 422 | DCHECK(!in_print_job_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 423 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | settings_ = settings; |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 425 | |
| 426 | // TODO(maruel): settings_.ToDEVMODE() |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 427 | ScopedPrinterHandle printer; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()), |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 429 | printer.Receive(), NULL)) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | return FAILED; |
| 431 | |
| 432 | Result status = OK; |
| 433 | |
| 434 | if (!GetPrinterSettings(printer, settings_.device_name())) |
| 435 | status = FAILED; |
| 436 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | if (status != OK) |
| 438 | ResetSettings(); |
| 439 | return status; |
| 440 | } |
| 441 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 442 | PrintingContext::Result PrintingContextWin::NewDocument( |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 443 | const string16& document_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 444 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 445 | if (!context_) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 446 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 447 | |
| 448 | // Set the flag used by the AbortPrintJob dialog procedure. |
| 449 | abort_printing_ = false; |
| 450 | |
| 451 | in_print_job_ = true; |
| 452 | |
| 453 | // Register the application's AbortProc function with GDI. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 454 | if (SP_ERROR == SetAbortProc(context_, &AbortProc)) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 455 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 456 | |
[email protected] | 09083eb | 2012-09-20 05:58:43 | [diff] [blame] | 457 | DCHECK(PrintBackend::SimplifyDocumentTitle(document_name) == document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | DOCINFO di = { sizeof(DOCINFO) }; |
[email protected] | ea6f72a97c | 2010-09-28 00:34:47 | [diff] [blame] | 459 | const std::wstring& document_name_wide = UTF16ToWide(document_name); |
| 460 | di.lpszDocName = document_name_wide.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | |
| 462 | // Is there a debug dump directory specified? If so, force to print to a file. |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 463 | base::FilePath debug_dump_path = PrintedDocument::debug_dump_path(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 464 | if (!debug_dump_path.empty()) { |
| 465 | // Create a filename. |
| 466 | std::wstring filename; |
| 467 | Time now(Time::Now()); |
[email protected] | 5cca3a5 | 2008-08-19 22:35:29 | [diff] [blame] | 468 | filename = base::TimeFormatShortDateNumeric(now); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | filename += L"_"; |
[email protected] | 5cca3a5 | 2008-08-19 22:35:29 | [diff] [blame] | 470 | filename += base::TimeFormatTimeOfDay(now); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | filename += L"_"; |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 472 | filename += UTF16ToWide(document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 473 | filename += L"_"; |
| 474 | filename += L"buffer.prn"; |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 475 | file_util::ReplaceIllegalCharactersInPath(&filename, '_'); |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 476 | debug_dump_path.Append(filename); |
| 477 | di.lpszOutput = debug_dump_path.value().c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | } |
| 479 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 480 | // No message loop running in unit tests. |
[email protected] | f58ef2b | 2013-05-06 22:43:57 | [diff] [blame^] | 481 | DCHECK(!base::MessageLoop::current() || |
| 482 | !base::MessageLoop::current()->NestableTasksAllowed()); |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 483 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 484 | // Begin a print job by calling the StartDoc function. |
| 485 | // NOTE: StartDoc() starts a message loop. That causes a lot of problems with |
| 486 | // IPC. Make sure recursive task processing is disabled. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 487 | if (StartDoc(context_, &di) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 488 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 489 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 490 | return OK; |
| 491 | } |
| 492 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 493 | PrintingContext::Result PrintingContextWin::NewPage() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | if (abort_printing_) |
| 495 | return CANCEL; |
[email protected] | 3b52c98 | 2010-09-27 20:40:36 | [diff] [blame] | 496 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 497 | DCHECK(in_print_job_); |
| 498 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 499 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 500 | // ::StartPage(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 501 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | return OK; |
| 503 | } |
| 504 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 505 | PrintingContext::Result PrintingContextWin::PageDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | if (abort_printing_) |
| 507 | return CANCEL; |
| 508 | DCHECK(in_print_job_); |
| 509 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 510 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 511 | // ::EndPage(). |
| 512 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 513 | return OK; |
| 514 | } |
| 515 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 516 | PrintingContext::Result PrintingContextWin::DocumentDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 517 | if (abort_printing_) |
| 518 | return CANCEL; |
| 519 | DCHECK(in_print_job_); |
[email protected] | 6074541 | 2010-09-27 23:46:07 | [diff] [blame] | 520 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 521 | |
| 522 | // Inform the driver that document has ended. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 523 | if (EndDoc(context_) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 524 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 525 | |
| 526 | ResetSettings(); |
| 527 | return OK; |
| 528 | } |
| 529 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 530 | void PrintingContextWin::Cancel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | abort_printing_ = true; |
| 532 | in_print_job_ = false; |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 533 | if (context_) |
| 534 | CancelDC(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | if (dialog_box_) { |
| 536 | DestroyWindow(dialog_box_); |
| 537 | dialog_box_dismissed_ = true; |
| 538 | } |
| 539 | } |
| 540 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 541 | void PrintingContextWin::ReleaseContext() { |
| 542 | if (context_) { |
| 543 | DeleteDC(context_); |
| 544 | context_ = NULL; |
| 545 | } |
| 546 | } |
| 547 | |
| 548 | gfx::NativeDrawingContext PrintingContextWin::context() const { |
| 549 | return context_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 550 | } |
| 551 | |
| 552 | // static |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 553 | BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 554 | if (nCode) { |
| 555 | // TODO(maruel): Need a way to find the right instance to set. Should |
| 556 | // leverage PrintJobManager here? |
| 557 | // abort_printing_ = true; |
| 558 | } |
| 559 | return true; |
| 560 | } |
| 561 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 562 | bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode, |
| 563 | const std::wstring& new_device_name, |
| 564 | const PRINTPAGERANGE* ranges, |
| 565 | int number_ranges, |
| 566 | bool selection_only) { |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 567 | skia::InitializeDC(context_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 568 | DCHECK(GetDeviceCaps(context_, CLIPCAPS)); |
| 569 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB); |
| 570 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 571 | // Some printers don't advertise these. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 572 | // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING); |
| 573 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA); |
| 574 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 575 | |
| 576 | // StretchDIBits() support is needed for printing. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 577 | if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) || |
| 578 | !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 579 | NOTREACHED(); |
| 580 | ResetSettings(); |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 585 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 586 | PageRanges ranges_vector; |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 587 | if (!selection_only) { |
| 588 | // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector. |
| 589 | ranges_vector.reserve(number_ranges); |
| 590 | for (int i = 0; i < number_ranges; ++i) { |
| 591 | PageRange range; |
| 592 | // Transfer from 1-based to 0-based. |
| 593 | range.from = ranges[i].nFromPage - 1; |
| 594 | range.to = ranges[i].nToPage - 1; |
| 595 | ranges_vector.push_back(range); |
| 596 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 597 | } |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 598 | |
| 599 | PrintSettingsInitializerWin::InitPrintSettings(context_, |
| 600 | dev_mode, |
| 601 | ranges_vector, |
| 602 | new_device_name, |
| 603 | selection_only, |
| 604 | &settings_); |
| 605 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | return true; |
| 607 | } |
| 608 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 609 | bool PrintingContextWin::GetPrinterSettings(HANDLE printer, |
| 610 | const std::wstring& device_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 611 | DCHECK(!in_print_job_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 612 | |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 613 | UserDefaultDevMode user_settings; |
| 614 | |
| 615 | if (!user_settings.Init(printer) || |
| 616 | !AllocateContext(device_name, user_settings.get(), &context_)) { |
| 617 | ResetSettings(); |
| 618 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 619 | } |
| 620 | |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 621 | return InitializeSettings(*user_settings.get(), device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 622 | } |
| 623 | |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 624 | // static |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 625 | bool PrintingContextWin::AllocateContext(const std::wstring& device_name, |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 626 | const DEVMODE* dev_mode, |
| 627 | gfx::NativeDrawingContext* context) { |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 628 | *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 629 | DCHECK(*context); |
| 630 | return *context != NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 631 | } |
| 632 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 633 | PrintingContext::Result PrintingContextWin::ParseDialogResultEx( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 634 | const PRINTDLGEX& dialog_options) { |
| 635 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 636 | if (dialog_options.dwResultAction != PD_RESULT_CANCEL) { |
| 637 | // Start fresh. |
| 638 | ResetSettings(); |
| 639 | |
| 640 | DEVMODE* dev_mode = NULL; |
| 641 | if (dialog_options.hDevMode) { |
| 642 | dev_mode = |
| 643 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 644 | DCHECK(dev_mode); |
| 645 | } |
| 646 | |
| 647 | std::wstring device_name; |
| 648 | if (dialog_options.hDevNames) { |
| 649 | DEVNAMES* dev_names = |
| 650 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 651 | DCHECK(dev_names); |
| 652 | if (dev_names) { |
| 653 | device_name = |
| 654 | reinterpret_cast<const wchar_t*>( |
| 655 | reinterpret_cast<const wchar_t*>(dev_names) + |
| 656 | dev_names->wDeviceOffset); |
| 657 | GlobalUnlock(dialog_options.hDevNames); |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | bool success = false; |
| 662 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 663 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 664 | PRINTPAGERANGE* page_ranges = NULL; |
| 665 | DWORD num_page_ranges = 0; |
| 666 | bool print_selection_only = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 667 | if (dialog_options.Flags & PD_PAGENUMS) { |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 668 | page_ranges = dialog_options.lpPageRanges; |
| 669 | num_page_ranges = dialog_options.nPageRanges; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 670 | } |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 671 | if (dialog_options.Flags & PD_SELECTION) { |
| 672 | print_selection_only = true; |
| 673 | } |
| 674 | success = InitializeSettings(*dev_mode, |
| 675 | device_name, |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 676 | page_ranges, |
| 677 | num_page_ranges, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 678 | print_selection_only); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 679 | } |
| 680 | |
| 681 | if (!success && dialog_options.hDC) { |
| 682 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 683 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | if (dev_mode) { |
| 687 | GlobalUnlock(dialog_options.hDevMode); |
| 688 | } |
| 689 | } else { |
| 690 | if (dialog_options.hDC) { |
| 691 | DeleteDC(dialog_options.hDC); |
| 692 | } |
| 693 | } |
| 694 | |
| 695 | if (dialog_options.hDevMode != NULL) |
| 696 | GlobalFree(dialog_options.hDevMode); |
| 697 | if (dialog_options.hDevNames != NULL) |
| 698 | GlobalFree(dialog_options.hDevNames); |
| 699 | |
| 700 | switch (dialog_options.dwResultAction) { |
| 701 | case PD_RESULT_PRINT: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 702 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 703 | case PD_RESULT_APPLY: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 704 | return context_ ? CANCEL : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 705 | case PD_RESULT_CANCEL: |
| 706 | return CANCEL; |
| 707 | default: |
| 708 | return FAILED; |
| 709 | } |
| 710 | } |
| 711 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 712 | PrintingContext::Result PrintingContextWin::ParseDialogResult( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 713 | const PRINTDLG& dialog_options) { |
| 714 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 715 | // Start fresh. |
| 716 | ResetSettings(); |
| 717 | |
| 718 | DEVMODE* dev_mode = NULL; |
| 719 | if (dialog_options.hDevMode) { |
| 720 | dev_mode = |
| 721 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 722 | DCHECK(dev_mode); |
| 723 | } |
| 724 | |
| 725 | std::wstring device_name; |
| 726 | if (dialog_options.hDevNames) { |
| 727 | DEVNAMES* dev_names = |
| 728 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 729 | DCHECK(dev_names); |
| 730 | if (dev_names) { |
| 731 | device_name = |
| 732 | reinterpret_cast<const wchar_t*>( |
| 733 | reinterpret_cast<const wchar_t*>(dev_names) + |
| 734 | dev_names->wDeviceOffset); |
| 735 | GlobalUnlock(dialog_options.hDevNames); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | bool success = false; |
| 740 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 741 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 742 | success = InitializeSettings(*dev_mode, device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 743 | } |
| 744 | |
| 745 | if (!success && dialog_options.hDC) { |
| 746 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 747 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 748 | } |
| 749 | |
| 750 | if (dev_mode) { |
| 751 | GlobalUnlock(dialog_options.hDevMode); |
| 752 | } |
| 753 | |
| 754 | if (dialog_options.hDevMode != NULL) |
| 755 | GlobalFree(dialog_options.hDevMode); |
| 756 | if (dialog_options.hDevNames != NULL) |
| 757 | GlobalFree(dialog_options.hDevNames); |
| 758 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 759 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | } // namespace printing |