blob: b0a964448cd65ec752cab0015e25712f838f8de9 [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
[email protected]19f09a62011-05-04 23:37:477#include <algorithm>
8
[email protected]ad3b05012014-06-16 23:02:159#include "base/strings/string_number_conversions.h"
[email protected]906265872013-06-07 22:40:4510#include "base/strings/utf_string_conversions.h"
[email protected]09083eb2012-09-20 05:58:4311#include "printing/backend/print_backend.h"
[email protected]55e8e9f2012-03-02 22:50:5412#include "printing/backend/win_helper.h"
[email protected]4993f342010-10-26 17:57:5213#include "printing/print_settings_initializer_win.h"
[email protected]8ff1d422009-07-07 21:31:3914#include "printing/printed_document.h"
[email protected]ec91bfa2013-11-09 17:24:2715#include "printing/printing_utils.h"
[email protected]e5636a52011-09-28 21:44:4216#include "printing/units.h"
[email protected]62f2e802011-05-26 14:28:3517#include "skia/ext/platform_device.h"
initial.commit09911bf2008-07-26 23:55:2918
[email protected]6b7bfd22013-05-28 23:55:1519#if defined(USE_AURA)
[email protected]e8cf7332014-02-14 18:54:5820#include "ui/aura/remote_window_tree_host_win.h"
[email protected]6b7bfd22013-05-28 23:55:1521#include "ui/aura/window.h"
[email protected]6b7bfd22013-05-28 23:55:1522#endif
23
[email protected]bd5baaf2011-04-21 20:48:3324namespace {
25
[email protected]6b7bfd22013-05-28 23:55:1526HWND GetRootWindow(gfx::NativeView view) {
27 HWND window = NULL;
28#if defined(USE_AURA)
29 if (view)
[email protected]2374d1812014-03-04 03:42:2730 window = view->GetHost()->GetAcceleratedWidget();
[email protected]6b7bfd22013-05-28 23:55:1531#else
32 if (view && IsWindow(view)) {
33 window = GetAncestor(view, GA_ROOTOWNER);
34 }
35#endif
36 if (!window) {
37 // TODO(maruel): bug 1214347 Get the right browser window instead.
38 return GetDesktopWindow();
39 }
40 return window;
41}
42
[email protected]bd5baaf2011-04-21 20:48:3343} // anonymous namespace
44
initial.commit09911bf2008-07-26 23:55:2945namespace printing {
46
[email protected]51e8d9352010-10-06 22:21:1747// static
[email protected]ee5f36e42010-12-03 22:40:3748PrintingContext* PrintingContext::Create(const std::string& app_locale) {
49 return static_cast<PrintingContext*>(new PrintingContextWin(app_locale));
[email protected]51e8d9352010-10-06 22:21:1750}
51
[email protected]ee5f36e42010-12-03 22:40:3752PrintingContextWin::PrintingContextWin(const std::string& app_locale)
[email protected]97a80632014-03-27 17:50:3953 : PrintingContext(app_locale), context_(NULL), dialog_box_(NULL) {}
initial.commit09911bf2008-07-26 23:55:2954
[email protected]51e8d9352010-10-06 22:21:1755PrintingContextWin::~PrintingContextWin() {
56 ReleaseContext();
initial.commit09911bf2008-07-26 23:55:2957}
58
[email protected]abe48112011-11-19 01:58:3859void PrintingContextWin::AskUserForSettings(
60 gfx::NativeView view, int max_pages, bool has_selection,
61 const PrintSettingsCallback& callback) {
initial.commit09911bf2008-07-26 23:55:2962 DCHECK(!in_print_job_);
63 dialog_box_dismissed_ = false;
[email protected]fc7904622010-05-12 19:26:4064
[email protected]6b7bfd22013-05-28 23:55:1565 HWND window = GetRootWindow(view);
[email protected]fc7904622010-05-12 19:26:4066 DCHECK(window);
67
initial.commit09911bf2008-07-26 23:55:2968 // Show the OS-dependent dialog box.
69 // If the user press
70 // - OK, the settings are reset and reinitialized with the new settings. OK is
71 // returned.
72 // - Apply then Cancel, the settings are reset and reinitialized with the new
73 // settings. CANCEL is returned.
74 // - Cancel, the settings are not changed, the previous setting, if it was
75 // initialized before, are kept. CANCEL is returned.
76 // On failure, the settings are reset and FAILED is returned.
[email protected]f487ac22014-02-13 17:04:3177 PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) };
78 dialog_options.hwndOwner = window;
[email protected]c8ad40c2009-06-08 17:05:2179 // Disable options we don't support currently.
initial.commit09911bf2008-07-26 23:55:2980 // TODO(maruel): Reuse the previously loaded settings!
[email protected]f487ac22014-02-13 17:04:3181 dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
82 PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE;
[email protected]c8ad40c2009-06-08 17:05:2183 if (!has_selection)
[email protected]f487ac22014-02-13 17:04:3184 dialog_options.Flags |= PD_NOSELECTION;
[email protected]c8ad40c2009-06-08 17:05:2185
[email protected]f487ac22014-02-13 17:04:3186 PRINTPAGERANGE ranges[32];
87 dialog_options.nStartPage = START_PAGE_GENERAL;
initial.commit09911bf2008-07-26 23:55:2988 if (max_pages) {
89 // Default initialize to print all the pages.
90 memset(ranges, 0, sizeof(ranges));
91 ranges[0].nFromPage = 1;
92 ranges[0].nToPage = max_pages;
[email protected]f487ac22014-02-13 17:04:3193 dialog_options.nPageRanges = 1;
94 dialog_options.nMaxPageRanges = arraysize(ranges);
95 dialog_options.nMinPage = 1;
96 dialog_options.nMaxPage = max_pages;
97 dialog_options.lpPageRanges = ranges;
initial.commit09911bf2008-07-26 23:55:2998 } else {
99 // No need to bother, we don't know how many pages are available.
[email protected]f487ac22014-02-13 17:04:31100 dialog_options.Flags |= PD_NOPAGENUMS;
initial.commit09911bf2008-07-26 23:55:29101 }
102
[email protected]97a80632014-03-27 17:50:39103 if (ShowPrintDialog(&dialog_options) != S_OK) {
[email protected]f487ac22014-02-13 17:04:31104 ResetSettings();
105 callback.Run(FAILED);
106 }
107
108 // TODO(maruel): Support PD_PRINTTOFILE.
109 callback.Run(ParseDialogResultEx(dialog_options));
initial.commit09911bf2008-07-26 23:55:29110}
111
[email protected]51e8d9352010-10-06 22:21:17112PrintingContext::Result PrintingContextWin::UseDefaultSettings() {
initial.commit09911bf2008-07-26 23:55:29113 DCHECK(!in_print_job_);
114
115 PRINTDLG dialog_options = { sizeof(PRINTDLG) };
116 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
[email protected]ff3ccc22011-04-18 21:35:48117 if (PrintDlg(&dialog_options))
118 return ParseDialogResult(dialog_options);
119
120 // No default printer configured, do we have any printers at all?
121 DWORD bytes_needed = 0;
122 DWORD count_returned = 0;
123 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
124 NULL, 2, NULL, 0, &bytes_needed, &count_returned);
125 if (bytes_needed) {
[email protected]1b248882014-02-25 04:39:14126 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
[email protected]fb3a97c2013-04-11 04:48:01127 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
[email protected]ff3ccc22011-04-18 21:35:48128 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
129 NULL, 2, printer_info_buffer.get(),
130 bytes_needed, &bytes_needed,
131 &count_returned);
132 if (ret && count_returned) { // have printers
133 // Open the first successfully found printer.
[email protected]1b248882014-02-25 04:39:14134 const PRINTER_INFO_2* info_2 =
135 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get());
136 const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
137 for (; info_2 < info_2_end; ++info_2) {
138 ScopedPrinterHandle printer;
139 if (!printer.OpenPrinter(info_2->pPrinterName))
[email protected]ff3ccc22011-04-18 21:35:48140 continue;
[email protected]2b364be2014-03-11 00:49:49141 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode =
142 CreateDevMode(printer, NULL);
[email protected]1b248882014-02-25 04:39:14143 if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(),
144 &context_)) {
145 continue;
146 }
147 if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0,
148 false)) {
149 return OK;
[email protected]ff3ccc22011-04-18 21:35:48150 }
[email protected]a5e0ef572011-04-21 18:38:54151 ReleaseContext();
[email protected]ff3ccc22011-04-18 21:35:48152 }
153 if (context_)
154 return OK;
155 }
initial.commit09911bf2008-07-26 23:55:29156 }
[email protected]ff3ccc22011-04-18 21:35:48157
158 ResetSettings();
159 return FAILED;
initial.commit09911bf2008-07-26 23:55:29160}
161
[email protected]4c9054b2013-11-04 18:34:29162gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() {
163 // Default fallback to Letter size.
164 gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch);
165
166 // Get settings from locale. Paper type buffer length is at most 4.
167 const int paper_type_buffer_len = 4;
168 wchar_t paper_type_buffer[paper_type_buffer_len] = {0};
169 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer,
170 paper_type_buffer_len);
171 if (wcslen(paper_type_buffer)) { // The call succeeded.
172 int paper_code = _wtoi(paper_type_buffer);
173 switch (paper_code) {
174 case DMPAPER_LEGAL:
175 paper_size.SetSize(kLegalWidthInch, kLegalHeightInch);
176 break;
177 case DMPAPER_A4:
178 paper_size.SetSize(kA4WidthInch, kA4HeightInch);
179 break;
180 case DMPAPER_A3:
181 paper_size.SetSize(kA3WidthInch, kA3HeightInch);
182 break;
183 default: // DMPAPER_LETTER is used for default fallback.
184 break;
185 }
186 }
187 return gfx::Size(
188 paper_size.width() * settings_.device_units_per_inch(),
189 paper_size.height() * settings_.device_units_per_inch());
190}
191
[email protected]55b23a02011-08-17 23:09:36192PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
[email protected]e5324b52013-10-29 03:16:37193 bool external_preview) {
[email protected]7868ecab2011-03-05 00:12:53194 DCHECK(!in_print_job_);
[email protected]e5324b52013-10-29 03:16:37195 DCHECK(!external_preview) << "Not implemented";
[email protected]7868ecab2011-03-05 00:12:53196
[email protected]55e8e9f2012-03-02 22:50:54197 ScopedPrinterHandle printer;
[email protected]1b248882014-02-25 04:39:14198 if (!printer.OpenPrinter(settings_.device_name().c_str()))
[email protected]bd5baaf2011-04-21 20:48:33199 return OnError();
[email protected]afbdbf112011-03-28 22:09:37200
[email protected]30aaa81a2011-05-03 21:08:39201 // Make printer changes local to Chrome.
202 // See MSDN documentation regarding DocumentProperties.
[email protected]2b364be2014-03-11 00:49:49203 scoped_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode =
[email protected]1b248882014-02-25 04:39:14204 CreateDevModeWithColor(printer, settings_.device_name(),
[email protected]b3b6b6f2014-02-14 20:19:54205 settings_.color() != GRAY);
206 if (!scoped_dev_mode)
[email protected]bd5baaf2011-04-21 20:48:33207 return OnError();
[email protected]7868ecab2011-03-05 00:12:53208
[email protected]b3b6b6f2014-02-14 20:19:54209 {
210 DEVMODE* dev_mode = scoped_dev_mode.get();
211 dev_mode->dmCopies = std::max(settings_.copies(), 1);
[email protected]299e3d002014-06-03 08:46:49212 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies
[email protected]b3b6b6f2014-02-14 20:19:54213 dev_mode->dmFields |= DM_COPIES;
214 dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE :
215 DMCOLLATE_FALSE;
216 }
[email protected]f3256b0d82011-09-04 23:36:29217
[email protected]b3b6b6f2014-02-14 20:19:54218 switch (settings_.duplex_mode()) {
219 case LONG_EDGE:
220 dev_mode->dmFields |= DM_DUPLEX;
221 dev_mode->dmDuplex = DMDUP_VERTICAL;
222 break;
223 case SHORT_EDGE:
224 dev_mode->dmFields |= DM_DUPLEX;
225 dev_mode->dmDuplex = DMDUP_HORIZONTAL;
226 break;
227 case SIMPLEX:
228 dev_mode->dmFields |= DM_DUPLEX;
229 dev_mode->dmDuplex = DMDUP_SIMPLEX;
230 break;
231 default: // UNKNOWN_DUPLEX_MODE
232 break;
233 }
234
235 dev_mode->dmFields |= DM_ORIENTATION;
236 dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE :
237 DMORIENT_PORTRAIT;
[email protected]ad3b05012014-06-16 23:02:15238
239 const PrintSettings::RequestedMedia& requested_media =
240 settings_.requested_media();
241 static const int kFromUm = 100; // Windows uses 0.1mm.
242 int width = requested_media.size_microns.width() / kFromUm;
243 int height = requested_media.size_microns.height() / kFromUm;
244 unsigned id = 0;
245 if (base::StringToUint(requested_media.vendor_id, &id) && id) {
246 dev_mode->dmFields |= DM_PAPERSIZE;
247 dev_mode->dmPaperSize = static_cast<short>(id);
248 } else if (width > 0 && height > 0) {
249 dev_mode->dmFields |= DM_PAPERWIDTH;
250 dev_mode->dmPaperWidth = width;
251 dev_mode->dmFields |= DM_PAPERLENGTH;
252 dev_mode->dmPaperLength = height;
253 }
[email protected]e5324b52013-10-29 03:16:37254 }
[email protected]bd5baaf2011-04-21 20:48:33255
[email protected]30aaa81a2011-05-03 21:08:39256 // Update data using DocumentProperties.
[email protected]b3b6b6f2014-02-14 20:19:54257 scoped_dev_mode = CreateDevMode(printer, scoped_dev_mode.get());
258 if (!scoped_dev_mode)
[email protected]30aaa81a2011-05-03 21:08:39259 return OnError();
[email protected]30aaa81a2011-05-03 21:08:39260
[email protected]bd5baaf2011-04-21 20:48:33261 // Set printer then refresh printer settings.
[email protected]b3b6b6f2014-02-14 20:19:54262 if (!AllocateContext(settings_.device_name(), scoped_dev_mode.get(),
263 &context_)) {
[email protected]bd5baaf2011-04-21 20:48:33264 return OnError();
265 }
[email protected]b3b6b6f2014-02-14 20:19:54266 PrintSettingsInitializerWin::InitPrintSettings(context_,
267 *scoped_dev_mode.get(),
[email protected]e5324b52013-10-29 03:16:37268 &settings_);
[email protected]afbdbf112011-03-28 22:09:37269 return OK;
[email protected]7868ecab2011-03-05 00:12:53270}
271
[email protected]51e8d9352010-10-06 22:21:17272PrintingContext::Result PrintingContextWin::InitWithSettings(
initial.commit09911bf2008-07-26 23:55:29273 const PrintSettings& settings) {
274 DCHECK(!in_print_job_);
[email protected]4993f342010-10-26 17:57:52275
initial.commit09911bf2008-07-26 23:55:29276 settings_ = settings;
[email protected]4993f342010-10-26 17:57:52277
278 // TODO(maruel): settings_.ToDEVMODE()
[email protected]55e8e9f2012-03-02 22:50:54279 ScopedPrinterHandle printer;
[email protected]37d638842014-02-21 21:54:32280 if (!printer.OpenPrinter(settings_.device_name().c_str())) {
initial.commit09911bf2008-07-26 23:55:29281 return FAILED;
[email protected]263c3512013-06-11 19:11:40282 }
initial.commit09911bf2008-07-26 23:55:29283
284 Result status = OK;
285
286 if (!GetPrinterSettings(printer, settings_.device_name()))
287 status = FAILED;
288
initial.commit09911bf2008-07-26 23:55:29289 if (status != OK)
290 ResetSettings();
291 return status;
292}
293
[email protected]51e8d9352010-10-06 22:21:17294PrintingContext::Result PrintingContextWin::NewDocument(
[email protected]b5fa4ee2013-10-01 07:19:07295 const base::string16& document_name) {
initial.commit09911bf2008-07-26 23:55:29296 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21297 if (!context_)
[email protected]c8ad40c2009-06-08 17:05:21298 return OnError();
initial.commit09911bf2008-07-26 23:55:29299
300 // Set the flag used by the AbortPrintJob dialog procedure.
301 abort_printing_ = false;
302
303 in_print_job_ = true;
304
305 // Register the application's AbortProc function with GDI.
[email protected]b75dca82009-10-13 18:46:21306 if (SP_ERROR == SetAbortProc(context_, &AbortProc))
[email protected]c8ad40c2009-06-08 17:05:21307 return OnError();
initial.commit09911bf2008-07-26 23:55:29308
[email protected]ec91bfa2013-11-09 17:24:27309 DCHECK(SimplifyDocumentTitle(document_name) == document_name);
initial.commit09911bf2008-07-26 23:55:29310 DOCINFO di = { sizeof(DOCINFO) };
[email protected]299e3d002014-06-03 08:46:49311 di.lpszDocName = document_name.c_str();
initial.commit09911bf2008-07-26 23:55:29312
313 // Is there a debug dump directory specified? If so, force to print to a file.
[email protected]c95198b2014-06-12 16:56:55314 base::string16 debug_dump_path =
315 PrintedDocument::CreateDebugDumpPath(document_name,
316 FILE_PATH_LITERAL(".prn")).value();
317 if (!debug_dump_path.empty())
318 di.lpszOutput = debug_dump_path.c_str();
initial.commit09911bf2008-07-26 23:55:29319
[email protected]daee4972009-07-09 14:28:24320 // No message loop running in unit tests.
[email protected]f58ef2b2013-05-06 22:43:57321 DCHECK(!base::MessageLoop::current() ||
322 !base::MessageLoop::current()->NestableTasksAllowed());
[email protected]daee4972009-07-09 14:28:24323
initial.commit09911bf2008-07-26 23:55:29324 // Begin a print job by calling the StartDoc function.
325 // NOTE: StartDoc() starts a message loop. That causes a lot of problems with
326 // IPC. Make sure recursive task processing is disabled.
[email protected]b75dca82009-10-13 18:46:21327 if (StartDoc(context_, &di) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21328 return OnError();
initial.commit09911bf2008-07-26 23:55:29329
initial.commit09911bf2008-07-26 23:55:29330 return OK;
331}
332
[email protected]51e8d9352010-10-06 22:21:17333PrintingContext::Result PrintingContextWin::NewPage() {
initial.commit09911bf2008-07-26 23:55:29334 if (abort_printing_)
335 return CANCEL;
[email protected]3b52c982010-09-27 20:40:36336 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29337 DCHECK(in_print_job_);
338
[email protected]b10c54d2011-03-16 18:22:35339 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
340 // ::StartPage().
initial.commit09911bf2008-07-26 23:55:29341
initial.commit09911bf2008-07-26 23:55:29342 return OK;
343}
344
[email protected]51e8d9352010-10-06 22:21:17345PrintingContext::Result PrintingContextWin::PageDone() {
initial.commit09911bf2008-07-26 23:55:29346 if (abort_printing_)
347 return CANCEL;
348 DCHECK(in_print_job_);
349
[email protected]b10c54d2011-03-16 18:22:35350 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
351 // ::EndPage().
352
initial.commit09911bf2008-07-26 23:55:29353 return OK;
354}
355
[email protected]51e8d9352010-10-06 22:21:17356PrintingContext::Result PrintingContextWin::DocumentDone() {
initial.commit09911bf2008-07-26 23:55:29357 if (abort_printing_)
358 return CANCEL;
359 DCHECK(in_print_job_);
[email protected]60745412010-09-27 23:46:07360 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29361
362 // Inform the driver that document has ended.
[email protected]b75dca82009-10-13 18:46:21363 if (EndDoc(context_) <= 0)
[email protected]c8ad40c2009-06-08 17:05:21364 return OnError();
initial.commit09911bf2008-07-26 23:55:29365
366 ResetSettings();
367 return OK;
368}
369
[email protected]51e8d9352010-10-06 22:21:17370void PrintingContextWin::Cancel() {
initial.commit09911bf2008-07-26 23:55:29371 abort_printing_ = true;
372 in_print_job_ = false;
[email protected]b75dca82009-10-13 18:46:21373 if (context_)
374 CancelDC(context_);
initial.commit09911bf2008-07-26 23:55:29375 if (dialog_box_) {
376 DestroyWindow(dialog_box_);
377 dialog_box_dismissed_ = true;
378 }
379}
380
[email protected]51e8d9352010-10-06 22:21:17381void PrintingContextWin::ReleaseContext() {
382 if (context_) {
383 DeleteDC(context_);
384 context_ = NULL;
385 }
386}
387
388gfx::NativeDrawingContext PrintingContextWin::context() const {
389 return context_;
initial.commit09911bf2008-07-26 23:55:29390}
391
392// static
[email protected]51e8d9352010-10-06 22:21:17393BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
initial.commit09911bf2008-07-26 23:55:29394 if (nCode) {
395 // TODO(maruel): Need a way to find the right instance to set. Should
396 // leverage PrintJobManager here?
397 // abort_printing_ = true;
398 }
399 return true;
400}
401
[email protected]51e8d9352010-10-06 22:21:17402bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode,
403 const std::wstring& new_device_name,
404 const PRINTPAGERANGE* ranges,
405 int number_ranges,
406 bool selection_only) {
[email protected]62f2e802011-05-26 14:28:35407 skia::InitializeDC(context_);
[email protected]b75dca82009-10-13 18:46:21408 DCHECK(GetDeviceCaps(context_, CLIPCAPS));
409 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB);
410 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64);
initial.commit09911bf2008-07-26 23:55:29411 // Some printers don't advertise these.
[email protected]b75dca82009-10-13 18:46:21412 // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING);
413 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA);
414 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
initial.commit09911bf2008-07-26 23:55:29415
416 // StretchDIBits() support is needed for printing.
[email protected]b75dca82009-10-13 18:46:21417 if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) ||
418 !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) {
initial.commit09911bf2008-07-26 23:55:29419 NOTREACHED();
420 ResetSettings();
421 return false;
422 }
423
424 DCHECK(!in_print_job_);
[email protected]b75dca82009-10-13 18:46:21425 DCHECK(context_);
initial.commit09911bf2008-07-26 23:55:29426 PageRanges ranges_vector;
[email protected]82270452009-06-19 15:58:01427 if (!selection_only) {
428 // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector.
429 ranges_vector.reserve(number_ranges);
430 for (int i = 0; i < number_ranges; ++i) {
431 PageRange range;
432 // Transfer from 1-based to 0-based.
433 range.from = ranges[i].nFromPage - 1;
434 range.to = ranges[i].nToPage - 1;
435 ranges_vector.push_back(range);
436 }
initial.commit09911bf2008-07-26 23:55:29437 }
[email protected]4993f342010-10-26 17:57:52438
[email protected]e5324b52013-10-29 03:16:37439 settings_.set_ranges(ranges_vector);
440 settings_.set_device_name(new_device_name);
441 settings_.set_selection_only(selection_only);
442 PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode,
[email protected]4993f342010-10-26 17:57:52443 &settings_);
444
initial.commit09911bf2008-07-26 23:55:29445 return true;
446}
447
[email protected]51e8d9352010-10-06 22:21:17448bool PrintingContextWin::GetPrinterSettings(HANDLE printer,
449 const std::wstring& device_name) {
initial.commit09911bf2008-07-26 23:55:29450 DCHECK(!in_print_job_);
initial.commit09911bf2008-07-26 23:55:29451
[email protected]2b364be2014-03-11 00:49:49452 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode =
453 CreateDevMode(printer, NULL);
[email protected]d8d17eb2012-12-12 07:25:41454
[email protected]1b248882014-02-25 04:39:14455 if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) {
[email protected]d8d17eb2012-12-12 07:25:41456 ResetSettings();
457 return false;
initial.commit09911bf2008-07-26 23:55:29458 }
459
[email protected]1b248882014-02-25 04:39:14460 return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29461}
462
[email protected]d8254622010-08-13 19:15:46463// static
[email protected]9f47bbf2011-05-06 19:03:17464bool PrintingContextWin::AllocateContext(const std::wstring& device_name,
[email protected]51e8d9352010-10-06 22:21:17465 const DEVMODE* dev_mode,
466 gfx::NativeDrawingContext* context) {
[email protected]9f47bbf2011-05-06 19:03:17467 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode);
[email protected]d8254622010-08-13 19:15:46468 DCHECK(*context);
469 return *context != NULL;
initial.commit09911bf2008-07-26 23:55:29470}
471
[email protected]51e8d9352010-10-06 22:21:17472PrintingContext::Result PrintingContextWin::ParseDialogResultEx(
initial.commit09911bf2008-07-26 23:55:29473 const PRINTDLGEX& dialog_options) {
474 // If the user clicked OK or Apply then Cancel, but not only Cancel.
475 if (dialog_options.dwResultAction != PD_RESULT_CANCEL) {
476 // Start fresh.
477 ResetSettings();
478
479 DEVMODE* dev_mode = NULL;
480 if (dialog_options.hDevMode) {
481 dev_mode =
482 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
483 DCHECK(dev_mode);
484 }
485
486 std::wstring device_name;
487 if (dialog_options.hDevNames) {
488 DEVNAMES* dev_names =
489 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
490 DCHECK(dev_names);
491 if (dev_names) {
[email protected]e5dc0f52013-11-02 13:49:28492 device_name = reinterpret_cast<const wchar_t*>(dev_names) +
493 dev_names->wDeviceOffset;
initial.commit09911bf2008-07-26 23:55:29494 GlobalUnlock(dialog_options.hDevNames);
495 }
496 }
497
498 bool success = false;
499 if (dev_mode && !device_name.empty()) {
[email protected]b75dca82009-10-13 18:46:21500 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21501 PRINTPAGERANGE* page_ranges = NULL;
502 DWORD num_page_ranges = 0;
503 bool print_selection_only = false;
initial.commit09911bf2008-07-26 23:55:29504 if (dialog_options.Flags & PD_PAGENUMS) {
[email protected]c8ad40c2009-06-08 17:05:21505 page_ranges = dialog_options.lpPageRanges;
506 num_page_ranges = dialog_options.nPageRanges;
initial.commit09911bf2008-07-26 23:55:29507 }
[email protected]c8ad40c2009-06-08 17:05:21508 if (dialog_options.Flags & PD_SELECTION) {
509 print_selection_only = true;
510 }
511 success = InitializeSettings(*dev_mode,
512 device_name,
[email protected]d8254622010-08-13 19:15:46513 page_ranges,
514 num_page_ranges,
[email protected]c8ad40c2009-06-08 17:05:21515 print_selection_only);
initial.commit09911bf2008-07-26 23:55:29516 }
517
518 if (!success && dialog_options.hDC) {
519 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21520 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29521 }
522
523 if (dev_mode) {
524 GlobalUnlock(dialog_options.hDevMode);
525 }
526 } else {
527 if (dialog_options.hDC) {
528 DeleteDC(dialog_options.hDC);
529 }
530 }
531
532 if (dialog_options.hDevMode != NULL)
533 GlobalFree(dialog_options.hDevMode);
534 if (dialog_options.hDevNames != NULL)
535 GlobalFree(dialog_options.hDevNames);
536
537 switch (dialog_options.dwResultAction) {
538 case PD_RESULT_PRINT:
[email protected]b75dca82009-10-13 18:46:21539 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29540 case PD_RESULT_APPLY:
[email protected]b75dca82009-10-13 18:46:21541 return context_ ? CANCEL : FAILED;
initial.commit09911bf2008-07-26 23:55:29542 case PD_RESULT_CANCEL:
543 return CANCEL;
544 default:
545 return FAILED;
546 }
547}
548
[email protected]97a80632014-03-27 17:50:39549HRESULT PrintingContextWin::ShowPrintDialog(PRINTDLGEX* options) {
550 // Note that this cannot use ui::BaseShellDialog as the print dialog is
551 // system modal: opening it from a background thread can cause Windows to
552 // get the wrong Z-order which will make the print dialog appear behind the
553 // browser frame (but still being modal) so neither the browser frame nor
554 // the print dialog will get any input. See http://crbug.com/342697
555 // http://crbug.com/180997 for details.
556 base::MessageLoop::ScopedNestableTaskAllower allow(
557 base::MessageLoop::current());
558
559 return PrintDlgEx(options);
560}
561
[email protected]51e8d9352010-10-06 22:21:17562PrintingContext::Result PrintingContextWin::ParseDialogResult(
initial.commit09911bf2008-07-26 23:55:29563 const PRINTDLG& dialog_options) {
564 // If the user clicked OK or Apply then Cancel, but not only Cancel.
565 // Start fresh.
566 ResetSettings();
567
568 DEVMODE* dev_mode = NULL;
569 if (dialog_options.hDevMode) {
570 dev_mode =
571 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
572 DCHECK(dev_mode);
573 }
574
575 std::wstring device_name;
576 if (dialog_options.hDevNames) {
577 DEVNAMES* dev_names =
578 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
579 DCHECK(dev_names);
580 if (dev_names) {
581 device_name =
582 reinterpret_cast<const wchar_t*>(
583 reinterpret_cast<const wchar_t*>(dev_names) +
584 dev_names->wDeviceOffset);
585 GlobalUnlock(dialog_options.hDevNames);
586 }
587 }
588
589 bool success = false;
590 if (dev_mode && !device_name.empty()) {
[email protected]b75dca82009-10-13 18:46:21591 context_ = dialog_options.hDC;
[email protected]c8ad40c2009-06-08 17:05:21592 success = InitializeSettings(*dev_mode, device_name, NULL, 0, false);
initial.commit09911bf2008-07-26 23:55:29593 }
594
595 if (!success && dialog_options.hDC) {
596 DeleteDC(dialog_options.hDC);
[email protected]b75dca82009-10-13 18:46:21597 context_ = NULL;
initial.commit09911bf2008-07-26 23:55:29598 }
599
600 if (dev_mode) {
601 GlobalUnlock(dialog_options.hDevMode);
602 }
603
604 if (dialog_options.hDevMode != NULL)
605 GlobalFree(dialog_options.hDevMode);
606 if (dialog_options.hDevNames != NULL)
607 GlobalFree(dialog_options.hDevNames);
608
[email protected]b75dca82009-10-13 18:46:21609 return context_ ? OK : FAILED;
initial.commit09911bf2008-07-26 23:55:29610}
611
612} // namespace printing