blob: d0205ea4a36ae1e6af330b3da46ff4d5763e3d44 [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]dcccb942009-02-01 18:23:0013#include "base/message_loop.h"
[email protected]e9c7c372012-08-13 16:36:0414#include "base/metrics/histogram.h"
[email protected]dcccb942009-02-01 18:23:0015#include "base/time.h"
[email protected]d9d42992010-09-13 19:39:1916#include "base/utf_string_conversions.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]e5636a52011-09-28 21:44:4225#include "printing/units.h"
[email protected]62f2e802011-05-26 14:28:3526#include "skia/ext/platform_device.h"
[email protected]bd04f2492012-12-06 00:40:3127#include "win8/util/win8_util.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]e1acf6f2008-10-27 20:43:3329using base::Time;
30
[email protected]bd5baaf2011-04-21 20:48:3331namespace {
32
[email protected]e5636a52011-09-28 21:44:4233// Constants for setting default PDF settings.
34const int kPDFDpi = 300; // 300 dpi
35// LETTER: 8.5 x 11 inches
36const int kPDFLetterWidth = 8.5 * kPDFDpi;
37const int kPDFLetterHeight = 11 * kPDFDpi;
38// LEGAL: 8.5 x 14 inches
39const int kPDFLegalWidth = 8.5 * kPDFDpi;
40const int kPDFLegalHeight = 14 * kPDFDpi;
41// A4: 8.27 x 11.69 inches
42const int kPDFA4Width = 8.27 * kPDFDpi;
43const int kPDFA4Height = 11.69 * kPDFDpi;
44// A3: 11.69 x 16.54 inches
45const int kPDFA3Width = 11.69 * kPDFDpi;
46const int kPDFA3Height = 16.54 * kPDFDpi;
47
[email protected]bd5baaf2011-04-21 20:48:3348} // anonymous namespace
49
initial.commit09911bf2008-07-26 23:55:2950namespace printing {
51
[email protected]51e8d9352010-10-06 22:21:1752class PrintingContextWin::CallbackHandler : public IPrintDialogCallback,
53 public IObjectWithSite {
initial.commit09911bf2008-07-26 23:55:2954 public:
[email protected]51e8d9352010-10-06 22:21:1755 CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd)
initial.commit09911bf2008-07-26 23:55:2956 : 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]51e8d9352010-10-06 22:21:17146 PrintingContextWin& owner_;
initial.commit09911bf2008-07-26 23:55:29147 HWND owner_hwnd_;
148 IPrintDialogServices* services_;
149
[email protected]5930cb62009-12-08 02:04:22150 DISALLOW_COPY_AND_ASSIGN(CallbackHandler);
initial.commit09911bf2008-07-26 23:55:29151};
152
[email protected]51e8d9352010-10-06 22:21:17153// static
[email protected]ee5f36e42010-12-03 22:40:37154PrintingContext* PrintingContext::Create(const std::string& app_locale) {
155 return static_cast<PrintingContext*>(new PrintingContextWin(app_locale));
[email protected]51e8d9352010-10-06 22:21:17156}
157
[email protected]ee5f36e42010-12-03 22:40:37158PrintingContextWin::PrintingContextWin(const std::string& app_locale)
159 : PrintingContext(app_locale),
[email protected]51e8d9352010-10-06 22:21:17160 context_(NULL),
initial.commit09911bf2008-07-26 23:55:29161 dialog_box_(NULL),
[email protected]d8254622010-08-13 19:15:46162 print_dialog_func_(&PrintDlgEx) {
initial.commit09911bf2008-07-26 23:55:29163}
164
[email protected]51e8d9352010-10-06 22:21:17165PrintingContextWin::~PrintingContextWin() {
166 ReleaseContext();
initial.commit09911bf2008-07-26 23:55:29167}
168
[email protected]abe48112011-11-19 01:58:38169void PrintingContextWin::AskUserForSettings(
170 gfx::NativeView view, int max_pages, bool has_selection,
171 const PrintSettingsCallback& callback) {
[email protected]94fbaa42011-09-07 20:09:54172#if !defined(USE_AURA)
initial.commit09911bf2008-07-26 23:55:29173 DCHECK(!in_print_job_);
[email protected]06186a3c2012-07-12 18:38:30174
[email protected]bd04f2492012-12-06 00:40:31175 if (win8::IsSingleWindowMetroMode()) {
[email protected]06186a3c2012-07-12 18:38:30176 // 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]e9c7c372012-08-13 16:36:04184 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]06186a3c2012-07-12 18:38:30188 metro_show_print_ui();
[email protected]e9c7c372012-08-13 16:36:04189 }
[email protected]06186a3c2012-07-12 18:38:30190 }
191 return callback.Run(CANCEL);
192 }
initial.commit09911bf2008-07-26 23:55:29193 dialog_box_dismissed_ = false;
[email protected]fc7904622010-05-12 19:26:40194
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.commit09911bf2008-07-26 23:55:29204 // 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]c8ad40c2009-06-08 17:05:21215 // Disable options we don't support currently.
initial.commit09911bf2008-07-26 23:55:29216 // TODO(maruel): Reuse the previously loaded settings!
217 dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
[email protected]c8ad40c2009-06-08 17:05:21218 PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE;
219 if (!has_selection)
220 dialog_options.Flags |= PD_NOSELECTION;
221
initial.commit09911bf2008-07-26 23:55:29222 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]3a0e4a32009-06-09 19:07:05231 dialog_options.nMinPage = 1;
initial.commit09911bf2008-07-26 23:55:29232 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]128bec3c2011-01-20 22:57:35239 if ((*print_dialog_func_)(&dialog_options) != S_OK) {
240 ResetSettings();
[email protected]abe48112011-11-19 01:58:38241 callback.Run(FAILED);
initial.commit09911bf2008-07-26 23:55:29242 }
[email protected]128bec3c2011-01-20 22:57:35243
initial.commit09911bf2008-07-26 23:55:29244 // TODO(maruel): Support PD_PRINTTOFILE.
[email protected]abe48112011-11-19 01:58:38245 callback.Run(ParseDialogResultEx(dialog_options));
[email protected]94fbaa42011-09-07 20:09:54246#endif
initial.commit09911bf2008-07-26 23:55:29247}
248
[email protected]51e8d9352010-10-06 22:21:17249PrintingContext::Result PrintingContextWin::UseDefaultSettings() {
initial.commit09911bf2008-07-26 23:55:29250 DCHECK(!in_print_job_);
251
252 PRINTDLG dialog_options = { sizeof(PRINTDLG) };
253 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
[email protected]ff3ccc22011-04-18 21:35:48254 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]fb3a97c2013-04-11 04:48:01264 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
[email protected]ff3ccc22011-04-18 21:35:48265 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]a5e0ef572011-04-21 18:38:54271 for (DWORD count = 0; count < count_returned; ++count) {
272 PRINTER_INFO_2* info_2 = reinterpret_cast<PRINTER_INFO_2*>(
[email protected]ff3ccc22011-04-18 21:35:48273 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]a5e0ef572011-04-21 18:38:54280 NULL, 0, false)) {
[email protected]ff3ccc22011-04-18 21:35:48281 break;
[email protected]ff3ccc22011-04-18 21:35:48282 }
[email protected]a5e0ef572011-04-21 18:38:54283 ReleaseContext();
[email protected]ff3ccc22011-04-18 21:35:48284 }
285 if (context_)
286 return OK;
287 }
initial.commit09911bf2008-07-26 23:55:29288 }
[email protected]ff3ccc22011-04-18 21:35:48289
290 ResetSettings();
291 return FAILED;
initial.commit09911bf2008-07-26 23:55:29292}
293
[email protected]55b23a02011-08-17 23:09:36294PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
[email protected]bd5baaf2011-04-21 20:48:33295 const DictionaryValue& job_settings,
296 const PageRanges& ranges) {
[email protected]7868ecab2011-03-05 00:12:53297 DCHECK(!in_print_job_);
298
[email protected]19f09a62011-05-04 23:37:47299 bool collate;
[email protected]f3256b0d82011-09-04 23:36:29300 int color;
[email protected]19f09a62011-05-04 23:37:47301 bool landscape;
302 bool print_to_pdf;
[email protected]737cfb22011-10-04 18:06:29303 bool is_cloud_dialog;
[email protected]19f09a62011-05-04 23:37:47304 int copies;
305 int duplex_mode;
[email protected]9f47bbf2011-05-06 19:03:17306 string16 device_name;
[email protected]19f09a62011-05-04 23:37:47307
308 if (!job_settings.GetBoolean(kSettingLandscape, &landscape) ||
309 !job_settings.GetBoolean(kSettingCollate, &collate) ||
[email protected]f3256b0d82011-09-04 23:36:29310 !job_settings.GetInteger(kSettingColor, &color) ||
[email protected]19f09a62011-05-04 23:37:47311 !job_settings.GetBoolean(kSettingPrintToPDF, &print_to_pdf) ||
[email protected]bd5baaf2011-04-21 20:48:33312 !job_settings.GetInteger(kSettingDuplexMode, &duplex_mode) ||
[email protected]19f09a62011-05-04 23:37:47313 !job_settings.GetInteger(kSettingCopies, &copies) ||
[email protected]737cfb22011-10-04 18:06:29314 !job_settings.GetString(kSettingDeviceName, &device_name) ||
315 !job_settings.GetBoolean(kSettingCloudPrintDialog, &is_cloud_dialog)) {
[email protected]c48bee22011-03-29 02:36:26316 return OnError();
[email protected]bd5baaf2011-04-21 20:48:33317 }
[email protected]37a401bf2011-03-31 16:12:36318
[email protected]d91db112011-10-18 20:58:51319 bool print_to_cloud = job_settings.HasKey(kSettingCloudPrintId);
[email protected]71dfff72011-07-23 01:27:27320
[email protected]737cfb22011-10-04 18:06:29321 if (print_to_pdf || print_to_cloud || is_cloud_dialog) {
[email protected]e5636a52011-09-28 21:44:42322 // 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]19f09a62011-05-04 23:37:47351 settings_.SetOrientation(landscape);
[email protected]30aaa81a2011-05-03 21:08:39352 settings_.ranges = ranges;
353 return OK;
354 }
355
[email protected]55e8e9f2012-03-02 22:50:54356 ScopedPrinterHandle printer;
[email protected]9f47bbf2011-05-06 19:03:17357 LPWSTR device_name_wide = const_cast<wchar_t*>(device_name.c_str());
[email protected]55e8e9f2012-03-02 22:50:54358 if (!OpenPrinter(device_name_wide, printer.Receive(), NULL))
[email protected]bd5baaf2011-04-21 20:48:33359 return OnError();
[email protected]afbdbf112011-03-28 22:09:37360
[email protected]30aaa81a2011-05-03 21:08:39361 // Make printer changes local to Chrome.
362 // See MSDN documentation regarding DocumentProperties.
[email protected]fb3a97c2013-04-11 04:48:01363 scoped_ptr<uint8[]> buffer;
[email protected]bd5baaf2011-04-21 20:48:33364 DEVMODE* dev_mode = NULL;
[email protected]9f47bbf2011-05-06 19:03:17365 LONG buffer_size = DocumentProperties(NULL, printer, device_name_wide,
[email protected]30aaa81a2011-05-03 21:08:39366 NULL, NULL, 0);
[email protected]bcf63422011-06-14 18:57:39367 if (buffer_size > 0) {
[email protected]30aaa81a2011-05-03 21:08:39368 buffer.reset(new uint8[buffer_size]);
369 memset(buffer.get(), 0, buffer_size);
[email protected]9f47bbf2011-05-06 19:03:17370 if (DocumentProperties(NULL, printer, device_name_wide,
[email protected]30aaa81a2011-05-03 21:08:39371 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]bd5baaf2011-04-21 20:48:33378 return OnError();
379 }
[email protected]7868ecab2011-03-05 00:12:53380
[email protected]d91db112011-10-18 20:58:51381 if (color == GRAY)
[email protected]f3256b0d82011-09-04 23:36:29382 dev_mode->dmColor = DMCOLOR_MONOCHROME;
383 else
384 dev_mode->dmColor = DMCOLOR_COLOR;
385
[email protected]bd5baaf2011-04-21 20:48:33386 dev_mode->dmCopies = std::max(copies, 1);
387 if (dev_mode->dmCopies > 1) // do not change collate unless multiple copies
[email protected]19f09a62011-05-04 23:37:47388 dev_mode->dmCollate = collate ? DMCOLLATE_TRUE : DMCOLLATE_FALSE;
[email protected]bd5baaf2011-04-21 20:48:33389 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]dc8373d2011-09-08 21:32:35396 case SIMPLEX:
[email protected]bd5baaf2011-04-21 20:48:33397 dev_mode->dmDuplex = DMDUP_SIMPLEX;
398 break;
[email protected]dc8373d2011-09-08 21:32:35399 default: // UNKNOWN_DUPLEX_MODE
400 break;
[email protected]bd5baaf2011-04-21 20:48:33401 }
[email protected]19f09a62011-05-04 23:37:47402 dev_mode->dmOrientation = landscape ? DMORIENT_LANDSCAPE : DMORIENT_PORTRAIT;
[email protected]bd5baaf2011-04-21 20:48:33403
[email protected]30aaa81a2011-05-03 21:08:39404 // Update data using DocumentProperties.
[email protected]9f47bbf2011-05-06 19:03:17405 if (DocumentProperties(NULL, printer, device_name_wide, dev_mode, dev_mode,
[email protected]30aaa81a2011-05-03 21:08:39406 DM_IN_BUFFER | DM_OUT_BUFFER) != IDOK) {
[email protected]30aaa81a2011-05-03 21:08:39407 return OnError();
408 }
409
[email protected]bd5baaf2011-04-21 20:48:33410 // Set printer then refresh printer settings.
[email protected]9f47bbf2011-05-06 19:03:17411 if (!AllocateContext(device_name, dev_mode, &context_)) {
[email protected]bd5baaf2011-04-21 20:48:33412 return OnError();
413 }
414 PrintSettingsInitializerWin::InitPrintSettings(context_, *dev_mode,
[email protected]9f47bbf2011-05-06 19:03:17415 ranges, device_name,
[email protected]bd5baaf2011-04-21 20:48:33416 false, &settings_);
[email protected]afbdbf112011-03-28 22:09:37417 return OK;
[email protected]7868ecab2011-03-05 00:12:53418}
419
[email protected]51e8d9352010-10-06 22:21:17420PrintingContext::Result PrintingContextWin::InitWithSettings(
initial.commit09911bf2008-07-26 23:55:29421 const PrintSettings& settings) {
422 DCHECK(!in_print_job_);
[email protected]4993f342010-10-26 17:57:52423
initial.commit09911bf2008-07-26 23:55:29424 settings_ = settings;
[email protected]4993f342010-10-26 17:57:52425
426 // TODO(maruel): settings_.ToDEVMODE()
[email protected]55e8e9f2012-03-02 22:50:54427 ScopedPrinterHandle printer;
initial.commit09911bf2008-07-26 23:55:29428 if (!OpenPrinter(const_cast<wchar_t*>(settings_.device_name().c_str()),
[email protected]55e8e9f2012-03-02 22:50:54429 printer.Receive(), NULL))
initial.commit09911bf2008-07-26 23:55:29430 return FAILED;
431
432 Result status = OK;
433
434 if (!GetPrinterSettings(printer, settings_.device_name()))
435 status = FAILED;
436
initial.commit09911bf2008-07-26 23:55:29437 if (status != OK)
438 ResetSettings();
439 return status;
440}
441
[email protected]51e8d9352010-10-06 22:21:17442PrintingContext::Result PrintingContextWin::NewDocument(
[email protected]d9d42992010-09-13 19:39:19443 const string16& document_name) {
initial.commit09911bf2008-07-26 23:55:29444 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21445 if (!context_)
[email protected]c8ad40c2009-06-08 17:05:21446 return OnError();
initial.commit09911bf2008-07-26 23:55:29447
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]b75dca82009-10-13 18:46:21454 if (SP_ERROR == SetAbortProc(context_, &AbortProc))
[email protected]c8ad40c2009-06-08 17:05:21455 return OnError();
initial.commit09911bf2008-07-26 23:55:29456
[email protected]09083eb2012-09-20 05:58:43457 DCHECK(PrintBackend::SimplifyDocumentTitle(document_name) == document_name);
initial.commit09911bf2008-07-26 23:55:29458 DOCINFO di = { sizeof(DOCINFO) };
[email protected]ea6f72a97c2010-09-28 00:34:47459 const std::wstring& document_name_wide = UTF16ToWide(document_name);
460 di.lpszDocName = document_name_wide.c_str();
initial.commit09911bf2008-07-26 23:55:29461
462 // Is there a debug dump directory specified? If so, force to print to a file.
[email protected]79f63882013-02-10 05:15:45463 base::FilePath debug_dump_path = PrintedDocument::debug_dump_path();
initial.commit09911bf2008-07-26 23:55:29464 if (!debug_dump_path.empty()) {
465 // Create a filename.
466 std::wstring filename;
467 Time now(Time::Now());
[email protected]5cca3a52008-08-19 22:35:29468 filename = base::TimeFormatShortDateNumeric(now);
initial.commit09911bf2008-07-26 23:55:29469 filename += L"_";
[email protected]5cca3a52008-08-19 22:35:29470 filename += base::TimeFormatTimeOfDay(now);
initial.commit09911bf2008-07-26 23:55:29471 filename += L"_";
[email protected]d9d42992010-09-13 19:39:19472 filename += UTF16ToWide(document_name);
initial.commit09911bf2008-07-26 23:55:29473 filename += L"_";
474 filename += L"buffer.prn";
[email protected]de2943352009-10-22 23:06:12475 file_util::ReplaceIllegalCharactersInPath(&filename, '_');
[email protected]d9d42992010-09-13 19:39:19476 debug_dump_path.Append(filename);
477 di.lpszOutput = debug_dump_path.value().c_str();
initial.commit09911bf2008-07-26 23:55:29478 }
479
[email protected]daee4972009-07-09 14:28:24480 // No message loop running in unit tests.
[email protected]f58ef2b2013-05-06 22:43:57481 DCHECK(!base::MessageLoop::current() ||
482 !base::MessageLoop::current()->NestableTasksAllowed());
[email protected]daee4972009-07-09 14:28:24483
initial.commit09911bf2008-07-26 23:55:29484 // 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]b75dca82009-10-13 18:46:21487 if (StartDoc(context_, &di) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21488 return OnError();
initial.commit09911bf2008-07-26 23:55:29489
initial.commit09911bf2008-07-26 23:55:29490 return OK;
491}
492
[email protected]51e8d9352010-10-06 22:21:17493PrintingContext::Result PrintingContextWin::NewPage() {
initial.commit09911bf2008-07-26 23:55:29494 if (abort_printing_)
495 return CANCEL;
[email protected]3b52c982010-09-27 20:40:36496 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29497 DCHECK(in_print_job_);
498
[email protected]b10c54d2011-03-16 18:22:35499 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
500 // ::StartPage().
initial.commit09911bf2008-07-26 23:55:29501
initial.commit09911bf2008-07-26 23:55:29502 return OK;
503}
504
[email protected]51e8d9352010-10-06 22:21:17505PrintingContext::Result PrintingContextWin::PageDone() {
initial.commit09911bf2008-07-26 23:55:29506 if (abort_printing_)
507 return CANCEL;
508 DCHECK(in_print_job_);
509
[email protected]b10c54d2011-03-16 18:22:35510 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
511 // ::EndPage().
512
initial.commit09911bf2008-07-26 23:55:29513 return OK;
514}
515
[email protected]51e8d9352010-10-06 22:21:17516PrintingContext::Result PrintingContextWin::DocumentDone() {
initial.commit09911bf2008-07-26 23:55:29517 if (abort_printing_)
518 return CANCEL;
519 DCHECK(in_print_job_);
[email protected]60745412010-09-27 23:46:07520 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29521
522 // Inform the driver that document has ended.
[email protected]b75dca82009-10-13 18:46:21523 if (EndDoc(context_) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21524 return OnError();
initial.commit09911bf2008-07-26 23:55:29525
526 ResetSettings();
527 return OK;
528}
529
[email protected]51e8d9352010-10-06 22:21:17530void PrintingContextWin::Cancel() {
initial.commit09911bf2008-07-26 23:55:29531 abort_printing_ = true;
532 in_print_job_ = false;
[email protected]b75dca82009-10-13 18:46:21533 if (context_)
534 CancelDC(context_);
initial.commit09911bf2008-07-26 23:55:29535 if (dialog_box_) {
536 DestroyWindow(dialog_box_);
537 dialog_box_dismissed_ = true;
538 }
539}
540
[email protected]51e8d9352010-10-06 22:21:17541void PrintingContextWin::ReleaseContext() {
542 if (context_) {
543 DeleteDC(context_);
544 context_ = NULL;
545 }
546}
547
548gfx::NativeDrawingContext PrintingContextWin::context() const {
549 return context_;
initial.commit09911bf2008-07-26 23:55:29550}
551
552// static
[email protected]51e8d9352010-10-06 22:21:17553BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
initial.commit09911bf2008-07-26 23:55:29554 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]51e8d9352010-10-06 22:21:17562bool 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]62f2e802011-05-26 14:28:35567 skia::InitializeDC(context_);
[email protected]b75dca82009-10-13 18:46:21568 DCHECK(GetDeviceCaps(context_, CLIPCAPS));
569 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB);
570 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64);
initial.commit09911bf2008-07-26 23:55:29571 // Some printers don't advertise these.
[email protected]b75dca82009-10-13 18:46:21572 // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING);
573 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA);
574 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
initial.commit09911bf2008-07-26 23:55:29575
576 // StretchDIBits() support is needed for printing.
[email protected]b75dca82009-10-13 18:46:21577 if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) ||
578 !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) {
initial.commit09911bf2008-07-26 23:55:29579 NOTREACHED();
580 ResetSettings();
581 return false;
582 }
583
584 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21585 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29586 PageRanges ranges_vector;
[email protected]82270452009-06-19 15:58:01587 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.commit09911bf2008-07-26 23:55:29597 }
[email protected]4993f342010-10-26 17:57:52598
599 PrintSettingsInitializerWin::InitPrintSettings(context_,
600 dev_mode,
601 ranges_vector,
602 new_device_name,
603 selection_only,
604 &settings_);
605
initial.commit09911bf2008-07-26 23:55:29606 return true;
607}
608
[email protected]51e8d9352010-10-06 22:21:17609bool PrintingContextWin::GetPrinterSettings(HANDLE printer,
610 const std::wstring& device_name) {
initial.commit09911bf2008-07-26 23:55:29611 DCHECK(!in_print_job_);
initial.commit09911bf2008-07-26 23:55:29612
[email protected]d8d17eb2012-12-12 07:25:41613 UserDefaultDevMode user_settings;
614
615 if (!user_settings.Init(printer) ||
616 !AllocateContext(device_name, user_settings.get(), &context_)) {
617 ResetSettings();
618 return false;
initial.commit09911bf2008-07-26 23:55:29619 }
620
[email protected]d8d17eb2012-12-12 07:25:41621 return InitializeSettings(*user_settings.get(), device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29622}
623
[email protected]d8254622010-08-13 19:15:46624// static
[email protected]9f47bbf2011-05-06 19:03:17625bool PrintingContextWin::AllocateContext(const std::wstring& device_name,
[email protected]51e8d9352010-10-06 22:21:17626 const DEVMODE* dev_mode,
627 gfx::NativeDrawingContext* context) {
[email protected]9f47bbf2011-05-06 19:03:17628 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode);
[email protected]d8254622010-08-13 19:15:46629 DCHECK(*context);
630 return *context != NULL;
initial.commit09911bf2008-07-26 23:55:29631}
632
[email protected]51e8d9352010-10-06 22:21:17633PrintingContext::Result PrintingContextWin::ParseDialogResultEx(
initial.commit09911bf2008-07-26 23:55:29634 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]b75dca82009-10-13 18:46:21663 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21664 PRINTPAGERANGE* page_ranges = NULL;
665 DWORD num_page_ranges = 0;
666 bool print_selection_only = false;
initial.commit09911bf2008-07-26 23:55:29667 if (dialog_options.Flags & PD_PAGENUMS) {
[email protected]c8ad40c2009-06-08 17:05:21668 page_ranges = dialog_options.lpPageRanges;
669 num_page_ranges = dialog_options.nPageRanges;
initial.commit09911bf2008-07-26 23:55:29670 }
[email protected]c8ad40c2009-06-08 17:05:21671 if (dialog_options.Flags & PD_SELECTION) {
672 print_selection_only = true;
673 }
674 success = InitializeSettings(*dev_mode,
675 device_name,
[email protected]d8254622010-08-13 19:15:46676 page_ranges,
677 num_page_ranges,
[email protected]c8ad40c2009-06-08 17:05:21678 print_selection_only);
initial.commit09911bf2008-07-26 23:55:29679 }
680
681 if (!success && dialog_options.hDC) {
682 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21683 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29684 }
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]b75dca82009-10-13 18:46:21702 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29703 case PD_RESULT_APPLY:
[email protected]b75dca82009-10-13 18:46:21704 return context_ ? CANCEL : FAILED;
initial.commit09911bf2008-07-26 23:55:29705 case PD_RESULT_CANCEL:
706 return CANCEL;
707 default:
708 return FAILED;
709 }
710}
711
[email protected]51e8d9352010-10-06 22:21:17712PrintingContext::Result PrintingContextWin::ParseDialogResult(
initial.commit09911bf2008-07-26 23:55:29713 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]b75dca82009-10-13 18:46:21741 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21742 success = InitializeSettings(*dev_mode, device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29743 }
744
745 if (!success && dialog_options.hDC) {
746 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21747 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29748 }
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]b75dca82009-10-13 18:46:21759 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29760}
761
762} // namespace printing