blob: 78dd47fd0692b7c518aea328ebf2ae1b8f6ea999 [file] [log] [blame]
[email protected]55e8e9f2012-03-02 22:50:541// Copyright (c) 2012 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]51e8d9352010-10-06 22:21:175#include "printing/printing_context_win.h"
initial.commit09911bf2008-07-26 23:55:296
7#include <winspool.h>
8
[email protected]19f09a62011-05-04 23:37:479#include <algorithm>
10
[email protected]d0767cb542009-10-08 17:38:3011#include "base/i18n/file_util_icu.h"
[email protected]260d1572009-10-09 18:50:3512#include "base/i18n/time_formatting.h"
[email protected]a76295972013-07-18 00:42:3213#include "base/message_loop/message_loop.h"
[email protected]e9c7c372012-08-13 16:36:0414#include "base/metrics/histogram.h"
[email protected]906265872013-06-07 22:40:4515#include "base/strings/utf_string_conversions.h"
[email protected]5d7eb862013-06-28 15:21:2416#include "base/time/time.h"
[email protected]89f5aa8c2011-03-21 20:58:4417#include "base/values.h"
[email protected]06186a3c2012-07-12 18:38:3018#include "base/win/metro.h"
[email protected]09083eb2012-09-20 05:58:4319#include "printing/backend/print_backend.h"
[email protected]d8d17eb2012-12-12 07:25:4120#include "printing/backend/printing_info_win.h"
[email protected]55e8e9f2012-03-02 22:50:5421#include "printing/backend/win_helper.h"
[email protected]c97e5e82011-04-05 18:50:2322#include "printing/print_job_constants.h"
[email protected]4993f342010-10-26 17:57:5223#include "printing/print_settings_initializer_win.h"
[email protected]8ff1d422009-07-07 21:31:3924#include "printing/printed_document.h"
[email protected]ec91bfa2013-11-09 17:24:2725#include "printing/printing_utils.h"
[email protected]e5636a52011-09-28 21:44:4226#include "printing/units.h"
[email protected]62f2e802011-05-26 14:28:3527#include "skia/ext/platform_device.h"
[email protected]bd04f2492012-12-06 00:40:3128#include "win8/util/win8_util.h"
initial.commit09911bf2008-07-26 23:55:2929
[email protected]6b7bfd22013-05-28 23:55:1530#if defined(USE_AURA)
[email protected]e8cf7332014-02-14 18:54:5831#include "ui/aura/remote_window_tree_host_win.h"
[email protected]6b7bfd22013-05-28 23:55:1532#include "ui/aura/window.h"
[email protected]fcc51c952014-02-21 21:31:2633#include "ui/aura/window_event_dispatcher.h"
[email protected]6b7bfd22013-05-28 23:55:1534#endif
35
[email protected]e1acf6f2008-10-27 20:43:3336using base::Time;
37
[email protected]bd5baaf2011-04-21 20:48:3338namespace {
39
[email protected]6b7bfd22013-05-28 23:55:1540HWND GetRootWindow(gfx::NativeView view) {
41 HWND window = NULL;
42#if defined(USE_AURA)
43 if (view)
[email protected]2374d1812014-03-04 03:42:2744 window = view->GetHost()->GetAcceleratedWidget();
[email protected]6b7bfd22013-05-28 23:55:1545#else
46 if (view && IsWindow(view)) {
47 window = GetAncestor(view, GA_ROOTOWNER);
48 }
49#endif
50 if (!window) {
51 // TODO(maruel): bug 1214347 Get the right browser window instead.
52 return GetDesktopWindow();
53 }
54 return window;
55}
56
[email protected]bd5baaf2011-04-21 20:48:3357} // anonymous namespace
58
initial.commit09911bf2008-07-26 23:55:2959namespace printing {
60
[email protected]51e8d9352010-10-06 22:21:1761class PrintingContextWin::CallbackHandler : public IPrintDialogCallback,
62 public IObjectWithSite {
initial.commit09911bf2008-07-26 23:55:2963 public:
[email protected]51e8d9352010-10-06 22:21:1764 CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd)
initial.commit09911bf2008-07-26 23:55:2965 : owner_(owner),
66 owner_hwnd_(owner_hwnd),
67 services_(NULL) {
68 }
69
70 ~CallbackHandler() {
71 if (services_)
72 services_->Release();
73 }
74
75 IUnknown* ToIUnknown() {
76 return static_cast<IUnknown*>(static_cast<IPrintDialogCallback*>(this));
77 }
78
79 // IUnknown
80 virtual HRESULT WINAPI QueryInterface(REFIID riid, void**object) {
81 if (riid == IID_IUnknown) {
82 *object = ToIUnknown();
83 } else if (riid == IID_IPrintDialogCallback) {
84 *object = static_cast<IPrintDialogCallback*>(this);
85 } else if (riid == IID_IObjectWithSite) {
86 *object = static_cast<IObjectWithSite*>(this);
87 } else {
88 return E_NOINTERFACE;
89 }
90 return S_OK;
91 }
92
93 // No real ref counting.
94 virtual ULONG WINAPI AddRef() {
95 return 1;
96 }
97 virtual ULONG WINAPI Release() {
98 return 1;
99 }
100
101 // IPrintDialogCallback methods
102 virtual HRESULT WINAPI InitDone() {
103 return S_OK;
104 }
105
106 virtual HRESULT WINAPI SelectionChange() {
107 if (services_) {
108 // TODO(maruel): Get the devmode for the new printer with
109 // services_->GetCurrentDevMode(&devmode, &size), send that information
110 // back to our client and continue. The client needs to recalculate the
111 // number of rendered pages and send back this information here.
112 }
113 return S_OK;
114 }
115
116 virtual HRESULT WINAPI HandleMessage(HWND dialog,
117 UINT message,
118 WPARAM wparam,
119 LPARAM lparam,
120 LRESULT* result) {
121 // Cheap way to retrieve the window handle.
122 if (!owner_.dialog_box_) {
123 // The handle we receive is the one of the groupbox in the General tab. We
124 // need to get the grand-father to get the dialog box handle.
125 owner_.dialog_box_ = GetAncestor(dialog, GA_ROOT);
126 // Trick to enable the owner window. This can cause issues with navigation
127 // events so it may have to be disabled if we don't fix the side-effects.
128 EnableWindow(owner_hwnd_, TRUE);
129 }
130 return S_FALSE;
131 }
132
133 virtual HRESULT WINAPI SetSite(IUnknown* site) {
134 if (!site) {
135 DCHECK(services_);
136 services_->Release();
137 services_ = NULL;
138 // The dialog box is destroying, PrintJob::Worker don't need the handle
139 // anymore.
140 owner_.dialog_box_ = NULL;
141 } else {
142 DCHECK(services_ == NULL);
143 HRESULT hr = site->QueryInterface(IID_IPrintDialogServices,
144 reinterpret_cast<void**>(&services_));
145 DCHECK(SUCCEEDED(hr));
146 }
147 return S_OK;
148 }
149
150 virtual HRESULT WINAPI GetSite(REFIID riid, void** site) {
151 return E_NOTIMPL;
152 }
153
154 private:
[email protected]51e8d9352010-10-06 22:21:17155 PrintingContextWin& owner_;
initial.commit09911bf2008-07-26 23:55:29156 HWND owner_hwnd_;
157 IPrintDialogServices* services_;
158
[email protected]5930cb62009-12-08 02:04:22159 DISALLOW_COPY_AND_ASSIGN(CallbackHandler);
initial.commit09911bf2008-07-26 23:55:29160};
161
[email protected]51e8d9352010-10-06 22:21:17162// static
[email protected]ee5f36e42010-12-03 22:40:37163PrintingContext* PrintingContext::Create(const std::string& app_locale) {
164 return static_cast<PrintingContext*>(new PrintingContextWin(app_locale));
[email protected]51e8d9352010-10-06 22:21:17165}
166
[email protected]ee5f36e42010-12-03 22:40:37167PrintingContextWin::PrintingContextWin(const std::string& app_locale)
168 : PrintingContext(app_locale),
[email protected]51e8d9352010-10-06 22:21:17169 context_(NULL),
initial.commit09911bf2008-07-26 23:55:29170 dialog_box_(NULL),
[email protected]d8254622010-08-13 19:15:46171 print_dialog_func_(&PrintDlgEx) {
initial.commit09911bf2008-07-26 23:55:29172}
173
[email protected]51e8d9352010-10-06 22:21:17174PrintingContextWin::~PrintingContextWin() {
175 ReleaseContext();
initial.commit09911bf2008-07-26 23:55:29176}
177
[email protected]abe48112011-11-19 01:58:38178void PrintingContextWin::AskUserForSettings(
179 gfx::NativeView view, int max_pages, bool has_selection,
180 const PrintSettingsCallback& callback) {
initial.commit09911bf2008-07-26 23:55:29181 DCHECK(!in_print_job_);
[email protected]bd04f2492012-12-06 00:40:31182 if (win8::IsSingleWindowMetroMode()) {
[email protected]06186a3c2012-07-12 18:38:30183 // The system dialog can not be opened while running in Metro.
184 // But we can programatically launch the Metro print device charm though.
185 HMODULE metro_module = base::win::GetMetroModule();
186 if (metro_module != NULL) {
187 typedef void (*MetroShowPrintUI)();
188 MetroShowPrintUI metro_show_print_ui =
189 reinterpret_cast<MetroShowPrintUI>(
190 ::GetProcAddress(metro_module, "MetroShowPrintUI"));
[email protected]e9c7c372012-08-13 16:36:04191 if (metro_show_print_ui) {
192 // TODO(mad): Remove this once we can send user metrics from the metro
193 // driver. crbug.com/142330
194 UMA_HISTOGRAM_ENUMERATION("Metro.Print", 1, 2);
[email protected]06186a3c2012-07-12 18:38:30195 metro_show_print_ui();
[email protected]e9c7c372012-08-13 16:36:04196 }
[email protected]06186a3c2012-07-12 18:38:30197 }
198 return callback.Run(CANCEL);
199 }
initial.commit09911bf2008-07-26 23:55:29200 dialog_box_dismissed_ = false;
[email protected]fc7904622010-05-12 19:26:40201
[email protected]6b7bfd22013-05-28 23:55:15202 HWND window = GetRootWindow(view);
[email protected]fc7904622010-05-12 19:26:40203 DCHECK(window);
204
initial.commit09911bf2008-07-26 23:55:29205 // Show the OS-dependent dialog box.
206 // If the user press
207 // - OK, the settings are reset and reinitialized with the new settings. OK is
208 // returned.
209 // - Apply then Cancel, the settings are reset and reinitialized with the new
210 // settings. CANCEL is returned.
211 // - Cancel, the settings are not changed, the previous setting, if it was
212 // initialized before, are kept. CANCEL is returned.
213 // On failure, the settings are reset and FAILED is returned.
[email protected]f487ac22014-02-13 17:04:31214 PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) };
215 dialog_options.hwndOwner = window;
[email protected]c8ad40c2009-06-08 17:05:21216 // Disable options we don't support currently.
initial.commit09911bf2008-07-26 23:55:29217 // TODO(maruel): Reuse the previously loaded settings!
[email protected]f487ac22014-02-13 17:04:31218 dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
219 PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE;
[email protected]c8ad40c2009-06-08 17:05:21220 if (!has_selection)
[email protected]f487ac22014-02-13 17:04:31221 dialog_options.Flags |= PD_NOSELECTION;
[email protected]c8ad40c2009-06-08 17:05:21222
[email protected]f487ac22014-02-13 17:04:31223 PRINTPAGERANGE ranges[32];
224 dialog_options.nStartPage = START_PAGE_GENERAL;
initial.commit09911bf2008-07-26 23:55:29225 if (max_pages) {
226 // Default initialize to print all the pages.
227 memset(ranges, 0, sizeof(ranges));
228 ranges[0].nFromPage = 1;
229 ranges[0].nToPage = max_pages;
[email protected]f487ac22014-02-13 17:04:31230 dialog_options.nPageRanges = 1;
231 dialog_options.nMaxPageRanges = arraysize(ranges);
232 dialog_options.nMinPage = 1;
233 dialog_options.nMaxPage = max_pages;
234 dialog_options.lpPageRanges = ranges;
initial.commit09911bf2008-07-26 23:55:29235 } else {
236 // No need to bother, we don't know how many pages are available.
[email protected]f487ac22014-02-13 17:04:31237 dialog_options.Flags |= PD_NOPAGENUMS;
initial.commit09911bf2008-07-26 23:55:29238 }
239
[email protected]368d362d02014-02-14 18:36:17240 // Note that this cannot use ui::BaseShellDialog as the print dialog is
241 // system modal: opening it from a background thread can cause Windows to
242 // get the wrong Z-order which will make the print dialog appear behind the
243 // browser frame (but still being modal) so neither the browser frame nor
244 // the print dialog will get any input. See http://crbug.com/342697
245 // http://crbug.com/180997 for details.
246 base::MessageLoop::ScopedNestableTaskAllower allow(
247 base::MessageLoop::current());
[email protected]f487ac22014-02-13 17:04:31248 HRESULT hr = (*print_dialog_func_)(&dialog_options);
249 if (hr != S_OK) {
250 ResetSettings();
251 callback.Run(FAILED);
252 }
253
254 // TODO(maruel): Support PD_PRINTTOFILE.
255 callback.Run(ParseDialogResultEx(dialog_options));
initial.commit09911bf2008-07-26 23:55:29256}
257
[email protected]51e8d9352010-10-06 22:21:17258PrintingContext::Result PrintingContextWin::UseDefaultSettings() {
initial.commit09911bf2008-07-26 23:55:29259 DCHECK(!in_print_job_);
260
261 PRINTDLG dialog_options = { sizeof(PRINTDLG) };
262 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
[email protected]ff3ccc22011-04-18 21:35:48263 if (PrintDlg(&dialog_options))
264 return ParseDialogResult(dialog_options);
265
266 // No default printer configured, do we have any printers at all?
267 DWORD bytes_needed = 0;
268 DWORD count_returned = 0;
269 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
270 NULL, 2, NULL, 0, &bytes_needed, &count_returned);
271 if (bytes_needed) {
[email protected]1b248882014-02-25 04:39:14272 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
[email protected]fb3a97c2013-04-11 04:48:01273 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
[email protected]ff3ccc22011-04-18 21:35:48274 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
275 NULL, 2, printer_info_buffer.get(),
276 bytes_needed, &bytes_needed,
277 &count_returned);
278 if (ret && count_returned) { // have printers
279 // Open the first successfully found printer.
[email protected]1b248882014-02-25 04:39:14280 const PRINTER_INFO_2* info_2 =
281 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get());
282 const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
283 for (; info_2 < info_2_end; ++info_2) {
284 ScopedPrinterHandle printer;
285 if (!printer.OpenPrinter(info_2->pPrinterName))
[email protected]ff3ccc22011-04-18 21:35:48286 continue;
[email protected]1b248882014-02-25 04:39:14287 scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL);
288 if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(),
289 &context_)) {
290 continue;
291 }
292 if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0,
293 false)) {
294 return OK;
[email protected]ff3ccc22011-04-18 21:35:48295 }
[email protected]a5e0ef572011-04-21 18:38:54296 ReleaseContext();
[email protected]ff3ccc22011-04-18 21:35:48297 }
298 if (context_)
299 return OK;
300 }
initial.commit09911bf2008-07-26 23:55:29301 }
[email protected]ff3ccc22011-04-18 21:35:48302
303 ResetSettings();
304 return FAILED;
initial.commit09911bf2008-07-26 23:55:29305}
306
[email protected]4c9054b2013-11-04 18:34:29307gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() {
308 // Default fallback to Letter size.
309 gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch);
310
311 // Get settings from locale. Paper type buffer length is at most 4.
312 const int paper_type_buffer_len = 4;
313 wchar_t paper_type_buffer[paper_type_buffer_len] = {0};
314 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer,
315 paper_type_buffer_len);
316 if (wcslen(paper_type_buffer)) { // The call succeeded.
317 int paper_code = _wtoi(paper_type_buffer);
318 switch (paper_code) {
319 case DMPAPER_LEGAL:
320 paper_size.SetSize(kLegalWidthInch, kLegalHeightInch);
321 break;
322 case DMPAPER_A4:
323 paper_size.SetSize(kA4WidthInch, kA4HeightInch);
324 break;
325 case DMPAPER_A3:
326 paper_size.SetSize(kA3WidthInch, kA3HeightInch);
327 break;
328 default: // DMPAPER_LETTER is used for default fallback.
329 break;
330 }
331 }
332 return gfx::Size(
333 paper_size.width() * settings_.device_units_per_inch(),
334 paper_size.height() * settings_.device_units_per_inch());
335}
336
[email protected]55b23a02011-08-17 23:09:36337PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
[email protected]e5324b52013-10-29 03:16:37338 bool external_preview) {
[email protected]7868ecab2011-03-05 00:12:53339 DCHECK(!in_print_job_);
[email protected]e5324b52013-10-29 03:16:37340 DCHECK(!external_preview) << "Not implemented";
[email protected]7868ecab2011-03-05 00:12:53341
[email protected]55e8e9f2012-03-02 22:50:54342 ScopedPrinterHandle printer;
[email protected]1b248882014-02-25 04:39:14343 if (!printer.OpenPrinter(settings_.device_name().c_str()))
[email protected]bd5baaf2011-04-21 20:48:33344 return OnError();
[email protected]afbdbf112011-03-28 22:09:37345
[email protected]30aaa81a2011-05-03 21:08:39346 // Make printer changes local to Chrome.
347 // See MSDN documentation regarding DocumentProperties.
[email protected]b3b6b6f2014-02-14 20:19:54348 scoped_ptr<DEVMODE[]> scoped_dev_mode =
[email protected]1b248882014-02-25 04:39:14349 CreateDevModeWithColor(printer, settings_.device_name(),
[email protected]b3b6b6f2014-02-14 20:19:54350 settings_.color() != GRAY);
351 if (!scoped_dev_mode)
[email protected]bd5baaf2011-04-21 20:48:33352 return OnError();
[email protected]7868ecab2011-03-05 00:12:53353
[email protected]b3b6b6f2014-02-14 20:19:54354 {
355 DEVMODE* dev_mode = scoped_dev_mode.get();
356 dev_mode->dmCopies = std::max(settings_.copies(), 1);
357 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies
358 dev_mode->dmFields |= DM_COPIES;
359 dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE :
360 DMCOLLATE_FALSE;
361 }
[email protected]f3256b0d82011-09-04 23:36:29362
[email protected]b3b6b6f2014-02-14 20:19:54363 switch (settings_.duplex_mode()) {
364 case LONG_EDGE:
365 dev_mode->dmFields |= DM_DUPLEX;
366 dev_mode->dmDuplex = DMDUP_VERTICAL;
367 break;
368 case SHORT_EDGE:
369 dev_mode->dmFields |= DM_DUPLEX;
370 dev_mode->dmDuplex = DMDUP_HORIZONTAL;
371 break;
372 case SIMPLEX:
373 dev_mode->dmFields |= DM_DUPLEX;
374 dev_mode->dmDuplex = DMDUP_SIMPLEX;
375 break;
376 default: // UNKNOWN_DUPLEX_MODE
377 break;
378 }
379
380 dev_mode->dmFields |= DM_ORIENTATION;
381 dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE :
382 DMORIENT_PORTRAIT;
[email protected]e5324b52013-10-29 03:16:37383 }
[email protected]bd5baaf2011-04-21 20:48:33384
[email protected]30aaa81a2011-05-03 21:08:39385 // Update data using DocumentProperties.
[email protected]b3b6b6f2014-02-14 20:19:54386 scoped_dev_mode = CreateDevMode(printer, scoped_dev_mode.get());
387 if (!scoped_dev_mode)
[email protected]30aaa81a2011-05-03 21:08:39388 return OnError();
[email protected]30aaa81a2011-05-03 21:08:39389
[email protected]bd5baaf2011-04-21 20:48:33390 // Set printer then refresh printer settings.
[email protected]b3b6b6f2014-02-14 20:19:54391 if (!AllocateContext(settings_.device_name(), scoped_dev_mode.get(),
392 &context_)) {
[email protected]bd5baaf2011-04-21 20:48:33393 return OnError();
394 }
[email protected]b3b6b6f2014-02-14 20:19:54395 PrintSettingsInitializerWin::InitPrintSettings(context_,
396 *scoped_dev_mode.get(),
[email protected]e5324b52013-10-29 03:16:37397 &settings_);
[email protected]afbdbf112011-03-28 22:09:37398 return OK;
[email protected]7868ecab2011-03-05 00:12:53399}
400
[email protected]51e8d9352010-10-06 22:21:17401PrintingContext::Result PrintingContextWin::InitWithSettings(
initial.commit09911bf2008-07-26 23:55:29402 const PrintSettings& settings) {
403 DCHECK(!in_print_job_);
[email protected]4993f342010-10-26 17:57:52404
initial.commit09911bf2008-07-26 23:55:29405 settings_ = settings;
[email protected]4993f342010-10-26 17:57:52406
407 // TODO(maruel): settings_.ToDEVMODE()
[email protected]55e8e9f2012-03-02 22:50:54408 ScopedPrinterHandle printer;
[email protected]37d638842014-02-21 21:54:32409 if (!printer.OpenPrinter(settings_.device_name().c_str())) {
initial.commit09911bf2008-07-26 23:55:29410 return FAILED;
[email protected]263c3512013-06-11 19:11:40411 }
initial.commit09911bf2008-07-26 23:55:29412
413 Result status = OK;
414
415 if (!GetPrinterSettings(printer, settings_.device_name()))
416 status = FAILED;
417
initial.commit09911bf2008-07-26 23:55:29418 if (status != OK)
419 ResetSettings();
420 return status;
421}
422
[email protected]51e8d9352010-10-06 22:21:17423PrintingContext::Result PrintingContextWin::NewDocument(
[email protected]b5fa4ee2013-10-01 07:19:07424 const base::string16& document_name) {
initial.commit09911bf2008-07-26 23:55:29425 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21426 if (!context_)
[email protected]c8ad40c2009-06-08 17:05:21427 return OnError();
initial.commit09911bf2008-07-26 23:55:29428
429 // Set the flag used by the AbortPrintJob dialog procedure.
430 abort_printing_ = false;
431
432 in_print_job_ = true;
433
434 // Register the application's AbortProc function with GDI.
[email protected]b75dca82009-10-13 18:46:21435 if (SP_ERROR == SetAbortProc(context_, &AbortProc))
[email protected]c8ad40c2009-06-08 17:05:21436 return OnError();
initial.commit09911bf2008-07-26 23:55:29437
[email protected]ec91bfa2013-11-09 17:24:27438 DCHECK(SimplifyDocumentTitle(document_name) == document_name);
initial.commit09911bf2008-07-26 23:55:29439 DOCINFO di = { sizeof(DOCINFO) };
[email protected]6c3bf032013-12-25 19:37:03440 const std::wstring& document_name_wide = base::UTF16ToWide(document_name);
[email protected]ea6f72a97c2010-09-28 00:34:47441 di.lpszDocName = document_name_wide.c_str();
initial.commit09911bf2008-07-26 23:55:29442
443 // Is there a debug dump directory specified? If so, force to print to a file.
[email protected]79f63882013-02-10 05:15:45444 base::FilePath debug_dump_path = PrintedDocument::debug_dump_path();
initial.commit09911bf2008-07-26 23:55:29445 if (!debug_dump_path.empty()) {
446 // Create a filename.
447 std::wstring filename;
448 Time now(Time::Now());
[email protected]5cca3a52008-08-19 22:35:29449 filename = base::TimeFormatShortDateNumeric(now);
initial.commit09911bf2008-07-26 23:55:29450 filename += L"_";
[email protected]5cca3a52008-08-19 22:35:29451 filename += base::TimeFormatTimeOfDay(now);
initial.commit09911bf2008-07-26 23:55:29452 filename += L"_";
[email protected]6c3bf032013-12-25 19:37:03453 filename += base::UTF16ToWide(document_name);
initial.commit09911bf2008-07-26 23:55:29454 filename += L"_";
455 filename += L"buffer.prn";
[email protected]de2943352009-10-22 23:06:12456 file_util::ReplaceIllegalCharactersInPath(&filename, '_');
[email protected]d9d42992010-09-13 19:39:19457 debug_dump_path.Append(filename);
458 di.lpszOutput = debug_dump_path.value().c_str();
initial.commit09911bf2008-07-26 23:55:29459 }
460
[email protected]daee4972009-07-09 14:28:24461 // No message loop running in unit tests.
[email protected]f58ef2b2013-05-06 22:43:57462 DCHECK(!base::MessageLoop::current() ||
463 !base::MessageLoop::current()->NestableTasksAllowed());
[email protected]daee4972009-07-09 14:28:24464
initial.commit09911bf2008-07-26 23:55:29465 // Begin a print job by calling the StartDoc function.
466 // NOTE: StartDoc() starts a message loop. That causes a lot of problems with
467 // IPC. Make sure recursive task processing is disabled.
[email protected]b75dca82009-10-13 18:46:21468 if (StartDoc(context_, &di) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21469 return OnError();
initial.commit09911bf2008-07-26 23:55:29470
initial.commit09911bf2008-07-26 23:55:29471 return OK;
472}
473
[email protected]51e8d9352010-10-06 22:21:17474PrintingContext::Result PrintingContextWin::NewPage() {
initial.commit09911bf2008-07-26 23:55:29475 if (abort_printing_)
476 return CANCEL;
[email protected]3b52c982010-09-27 20:40:36477 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29478 DCHECK(in_print_job_);
479
[email protected]b10c54d2011-03-16 18:22:35480 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
481 // ::StartPage().
initial.commit09911bf2008-07-26 23:55:29482
initial.commit09911bf2008-07-26 23:55:29483 return OK;
484}
485
[email protected]51e8d9352010-10-06 22:21:17486PrintingContext::Result PrintingContextWin::PageDone() {
initial.commit09911bf2008-07-26 23:55:29487 if (abort_printing_)
488 return CANCEL;
489 DCHECK(in_print_job_);
490
[email protected]b10c54d2011-03-16 18:22:35491 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
492 // ::EndPage().
493
initial.commit09911bf2008-07-26 23:55:29494 return OK;
495}
496
[email protected]51e8d9352010-10-06 22:21:17497PrintingContext::Result PrintingContextWin::DocumentDone() {
initial.commit09911bf2008-07-26 23:55:29498 if (abort_printing_)
499 return CANCEL;
500 DCHECK(in_print_job_);
[email protected]60745412010-09-27 23:46:07501 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29502
503 // Inform the driver that document has ended.
[email protected]b75dca82009-10-13 18:46:21504 if (EndDoc(context_) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21505 return OnError();
initial.commit09911bf2008-07-26 23:55:29506
507 ResetSettings();
508 return OK;
509}
510
[email protected]51e8d9352010-10-06 22:21:17511void PrintingContextWin::Cancel() {
initial.commit09911bf2008-07-26 23:55:29512 abort_printing_ = true;
513 in_print_job_ = false;
[email protected]b75dca82009-10-13 18:46:21514 if (context_)
515 CancelDC(context_);
initial.commit09911bf2008-07-26 23:55:29516 if (dialog_box_) {
517 DestroyWindow(dialog_box_);
518 dialog_box_dismissed_ = true;
519 }
520}
521
[email protected]51e8d9352010-10-06 22:21:17522void PrintingContextWin::ReleaseContext() {
523 if (context_) {
524 DeleteDC(context_);
525 context_ = NULL;
526 }
527}
528
529gfx::NativeDrawingContext PrintingContextWin::context() const {
530 return context_;
initial.commit09911bf2008-07-26 23:55:29531}
532
533// static
[email protected]51e8d9352010-10-06 22:21:17534BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
initial.commit09911bf2008-07-26 23:55:29535 if (nCode) {
536 // TODO(maruel): Need a way to find the right instance to set. Should
537 // leverage PrintJobManager here?
538 // abort_printing_ = true;
539 }
540 return true;
541}
542
[email protected]51e8d9352010-10-06 22:21:17543bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode,
544 const std::wstring& new_device_name,
545 const PRINTPAGERANGE* ranges,
546 int number_ranges,
547 bool selection_only) {
[email protected]62f2e802011-05-26 14:28:35548 skia::InitializeDC(context_);
[email protected]b75dca82009-10-13 18:46:21549 DCHECK(GetDeviceCaps(context_, CLIPCAPS));
550 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB);
551 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64);
initial.commit09911bf2008-07-26 23:55:29552 // Some printers don't advertise these.
[email protected]b75dca82009-10-13 18:46:21553 // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING);
554 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA);
555 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
initial.commit09911bf2008-07-26 23:55:29556
557 // StretchDIBits() support is needed for printing.
[email protected]b75dca82009-10-13 18:46:21558 if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) ||
559 !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) {
initial.commit09911bf2008-07-26 23:55:29560 NOTREACHED();
561 ResetSettings();
562 return false;
563 }
564
565 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21566 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29567 PageRanges ranges_vector;
[email protected]82270452009-06-19 15:58:01568 if (!selection_only) {
569 // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector.
570 ranges_vector.reserve(number_ranges);
571 for (int i = 0; i < number_ranges; ++i) {
572 PageRange range;
573 // Transfer from 1-based to 0-based.
574 range.from = ranges[i].nFromPage - 1;
575 range.to = ranges[i].nToPage - 1;
576 ranges_vector.push_back(range);
577 }
initial.commit09911bf2008-07-26 23:55:29578 }
[email protected]4993f342010-10-26 17:57:52579
[email protected]e5324b52013-10-29 03:16:37580 settings_.set_ranges(ranges_vector);
581 settings_.set_device_name(new_device_name);
582 settings_.set_selection_only(selection_only);
583 PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode,
[email protected]4993f342010-10-26 17:57:52584 &settings_);
585
initial.commit09911bf2008-07-26 23:55:29586 return true;
587}
588
[email protected]51e8d9352010-10-06 22:21:17589bool PrintingContextWin::GetPrinterSettings(HANDLE printer,
590 const std::wstring& device_name) {
initial.commit09911bf2008-07-26 23:55:29591 DCHECK(!in_print_job_);
initial.commit09911bf2008-07-26 23:55:29592
[email protected]1b248882014-02-25 04:39:14593 scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL);
[email protected]d8d17eb2012-12-12 07:25:41594
[email protected]1b248882014-02-25 04:39:14595 if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) {
[email protected]d8d17eb2012-12-12 07:25:41596 ResetSettings();
597 return false;
initial.commit09911bf2008-07-26 23:55:29598 }
599
[email protected]1b248882014-02-25 04:39:14600 return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29601}
602
[email protected]d8254622010-08-13 19:15:46603// static
[email protected]9f47bbf2011-05-06 19:03:17604bool PrintingContextWin::AllocateContext(const std::wstring& device_name,
[email protected]51e8d9352010-10-06 22:21:17605 const DEVMODE* dev_mode,
606 gfx::NativeDrawingContext* context) {
[email protected]9f47bbf2011-05-06 19:03:17607 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode);
[email protected]d8254622010-08-13 19:15:46608 DCHECK(*context);
609 return *context != NULL;
initial.commit09911bf2008-07-26 23:55:29610}
611
[email protected]51e8d9352010-10-06 22:21:17612PrintingContext::Result PrintingContextWin::ParseDialogResultEx(
initial.commit09911bf2008-07-26 23:55:29613 const PRINTDLGEX& dialog_options) {
614 // If the user clicked OK or Apply then Cancel, but not only Cancel.
615 if (dialog_options.dwResultAction != PD_RESULT_CANCEL) {
616 // Start fresh.
617 ResetSettings();
618
619 DEVMODE* dev_mode = NULL;
620 if (dialog_options.hDevMode) {
621 dev_mode =
622 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
623 DCHECK(dev_mode);
624 }
625
626 std::wstring device_name;
627 if (dialog_options.hDevNames) {
628 DEVNAMES* dev_names =
629 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
630 DCHECK(dev_names);
631 if (dev_names) {
[email protected]e5dc0f52013-11-02 13:49:28632 device_name = reinterpret_cast<const wchar_t*>(dev_names) +
633 dev_names->wDeviceOffset;
initial.commit09911bf2008-07-26 23:55:29634 GlobalUnlock(dialog_options.hDevNames);
635 }
636 }
637
638 bool success = false;
639 if (dev_mode && !device_name.empty()) {
[email protected]b75dca82009-10-13 18:46:21640 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21641 PRINTPAGERANGE* page_ranges = NULL;
642 DWORD num_page_ranges = 0;
643 bool print_selection_only = false;
initial.commit09911bf2008-07-26 23:55:29644 if (dialog_options.Flags & PD_PAGENUMS) {
[email protected]c8ad40c2009-06-08 17:05:21645 page_ranges = dialog_options.lpPageRanges;
646 num_page_ranges = dialog_options.nPageRanges;
initial.commit09911bf2008-07-26 23:55:29647 }
[email protected]c8ad40c2009-06-08 17:05:21648 if (dialog_options.Flags & PD_SELECTION) {
649 print_selection_only = true;
650 }
651 success = InitializeSettings(*dev_mode,
652 device_name,
[email protected]d8254622010-08-13 19:15:46653 page_ranges,
654 num_page_ranges,
[email protected]c8ad40c2009-06-08 17:05:21655 print_selection_only);
initial.commit09911bf2008-07-26 23:55:29656 }
657
658 if (!success && dialog_options.hDC) {
659 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21660 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29661 }
662
663 if (dev_mode) {
664 GlobalUnlock(dialog_options.hDevMode);
665 }
666 } else {
667 if (dialog_options.hDC) {
668 DeleteDC(dialog_options.hDC);
669 }
670 }
671
672 if (dialog_options.hDevMode != NULL)
673 GlobalFree(dialog_options.hDevMode);
674 if (dialog_options.hDevNames != NULL)
675 GlobalFree(dialog_options.hDevNames);
676
677 switch (dialog_options.dwResultAction) {
678 case PD_RESULT_PRINT:
[email protected]b75dca82009-10-13 18:46:21679 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29680 case PD_RESULT_APPLY:
[email protected]b75dca82009-10-13 18:46:21681 return context_ ? CANCEL : FAILED;
initial.commit09911bf2008-07-26 23:55:29682 case PD_RESULT_CANCEL:
683 return CANCEL;
684 default:
685 return FAILED;
686 }
687}
688
[email protected]51e8d9352010-10-06 22:21:17689PrintingContext::Result PrintingContextWin::ParseDialogResult(
initial.commit09911bf2008-07-26 23:55:29690 const PRINTDLG& dialog_options) {
691 // If the user clicked OK or Apply then Cancel, but not only Cancel.
692 // Start fresh.
693 ResetSettings();
694
695 DEVMODE* dev_mode = NULL;
696 if (dialog_options.hDevMode) {
697 dev_mode =
698 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
699 DCHECK(dev_mode);
700 }
701
702 std::wstring device_name;
703 if (dialog_options.hDevNames) {
704 DEVNAMES* dev_names =
705 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
706 DCHECK(dev_names);
707 if (dev_names) {
708 device_name =
709 reinterpret_cast<const wchar_t*>(
710 reinterpret_cast<const wchar_t*>(dev_names) +
711 dev_names->wDeviceOffset);
712 GlobalUnlock(dialog_options.hDevNames);
713 }
714 }
715
716 bool success = false;
717 if (dev_mode && !device_name.empty()) {
[email protected]b75dca82009-10-13 18:46:21718 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21719 success = InitializeSettings(*dev_mode, device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29720 }
721
722 if (!success && dialog_options.hDC) {
723 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21724 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29725 }
726
727 if (dev_mode) {
728 GlobalUnlock(dialog_options.hDevMode);
729 }
730
731 if (dialog_options.hDevMode != NULL)
732 GlobalFree(dialog_options.hDevMode);
733 if (dialog_options.hDevNames != NULL)
734 GlobalFree(dialog_options.hDevNames);
735
[email protected]b75dca82009-10-13 18:46:21736 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29737}
738
739} // namespace printing