Introduce ProfileInvalidationProvider wrapper for InvalidationService
[chromium-blink-merge.git] / printing / printing_context_win.cc
blobf68482d3a61016d2cd7f4a62bba3d323e287ca0c
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "printing/printing_context_win.h"
7 #include <winspool.h>
9 #include <algorithm>
11 #include "base/i18n/file_util_icu.h"
12 #include "base/i18n/time_formatting.h"
13 #include "base/message_loop/message_loop.h"
14 #include "base/metrics/histogram.h"
15 #include "base/strings/utf_string_conversions.h"
16 #include "base/time/time.h"
17 #include "base/values.h"
18 #include "printing/backend/print_backend.h"
19 #include "printing/backend/printing_info_win.h"
20 #include "printing/backend/win_helper.h"
21 #include "printing/print_job_constants.h"
22 #include "printing/print_settings_initializer_win.h"
23 #include "printing/printed_document.h"
24 #include "printing/printing_utils.h"
25 #include "printing/units.h"
26 #include "skia/ext/platform_device.h"
28 #if defined(USE_AURA)
29 #include "ui/aura/remote_window_tree_host_win.h"
30 #include "ui/aura/window.h"
31 #include "ui/aura/window_event_dispatcher.h"
32 #endif
34 using base::Time;
36 namespace {
38 HWND GetRootWindow(gfx::NativeView view) {
39 HWND window = NULL;
40 #if defined(USE_AURA)
41 if (view)
42 window = view->GetHost()->GetAcceleratedWidget();
43 #else
44 if (view && IsWindow(view)) {
45 window = GetAncestor(view, GA_ROOTOWNER);
47 #endif
48 if (!window) {
49 // TODO(maruel): bug 1214347 Get the right browser window instead.
50 return GetDesktopWindow();
52 return window;
55 } // anonymous namespace
57 namespace printing {
59 class PrintingContextWin::CallbackHandler : public IPrintDialogCallback,
60 public IObjectWithSite {
61 public:
62 CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd)
63 : owner_(owner),
64 owner_hwnd_(owner_hwnd),
65 services_(NULL) {
68 ~CallbackHandler() {
69 if (services_)
70 services_->Release();
73 IUnknown* ToIUnknown() {
74 return static_cast<IUnknown*>(static_cast<IPrintDialogCallback*>(this));
77 // IUnknown
78 virtual HRESULT WINAPI QueryInterface(REFIID riid, void**object) {
79 if (riid == IID_IUnknown) {
80 *object = ToIUnknown();
81 } else if (riid == IID_IPrintDialogCallback) {
82 *object = static_cast<IPrintDialogCallback*>(this);
83 } else if (riid == IID_IObjectWithSite) {
84 *object = static_cast<IObjectWithSite*>(this);
85 } else {
86 return E_NOINTERFACE;
88 return S_OK;
91 // No real ref counting.
92 virtual ULONG WINAPI AddRef() {
93 return 1;
95 virtual ULONG WINAPI Release() {
96 return 1;
99 // IPrintDialogCallback methods
100 virtual HRESULT WINAPI InitDone() {
101 return S_OK;
104 virtual HRESULT WINAPI SelectionChange() {
105 if (services_) {
106 // TODO(maruel): Get the devmode for the new printer with
107 // services_->GetCurrentDevMode(&devmode, &size), send that information
108 // back to our client and continue. The client needs to recalculate the
109 // number of rendered pages and send back this information here.
111 return S_OK;
114 virtual HRESULT WINAPI HandleMessage(HWND dialog,
115 UINT message,
116 WPARAM wparam,
117 LPARAM lparam,
118 LRESULT* result) {
119 // Cheap way to retrieve the window handle.
120 if (!owner_.dialog_box_) {
121 // The handle we receive is the one of the groupbox in the General tab. We
122 // need to get the grand-father to get the dialog box handle.
123 owner_.dialog_box_ = GetAncestor(dialog, GA_ROOT);
124 // Trick to enable the owner window. This can cause issues with navigation
125 // events so it may have to be disabled if we don't fix the side-effects.
126 EnableWindow(owner_hwnd_, TRUE);
128 return S_FALSE;
131 virtual HRESULT WINAPI SetSite(IUnknown* site) {
132 if (!site) {
133 DCHECK(services_);
134 services_->Release();
135 services_ = NULL;
136 // The dialog box is destroying, PrintJob::Worker don't need the handle
137 // anymore.
138 owner_.dialog_box_ = NULL;
139 } else {
140 DCHECK(services_ == NULL);
141 HRESULT hr = site->QueryInterface(IID_IPrintDialogServices,
142 reinterpret_cast<void**>(&services_));
143 DCHECK(SUCCEEDED(hr));
145 return S_OK;
148 virtual HRESULT WINAPI GetSite(REFIID riid, void** site) {
149 return E_NOTIMPL;
152 private:
153 PrintingContextWin& owner_;
154 HWND owner_hwnd_;
155 IPrintDialogServices* services_;
157 DISALLOW_COPY_AND_ASSIGN(CallbackHandler);
160 // static
161 PrintingContext* PrintingContext::Create(const std::string& app_locale) {
162 return static_cast<PrintingContext*>(new PrintingContextWin(app_locale));
165 PrintingContextWin::PrintingContextWin(const std::string& app_locale)
166 : PrintingContext(app_locale), context_(NULL), dialog_box_(NULL) {}
168 PrintingContextWin::~PrintingContextWin() {
169 ReleaseContext();
172 void PrintingContextWin::AskUserForSettings(
173 gfx::NativeView view, int max_pages, bool has_selection,
174 const PrintSettingsCallback& callback) {
175 DCHECK(!in_print_job_);
176 dialog_box_dismissed_ = false;
178 HWND window = GetRootWindow(view);
179 DCHECK(window);
181 // Show the OS-dependent dialog box.
182 // If the user press
183 // - OK, the settings are reset and reinitialized with the new settings. OK is
184 // returned.
185 // - Apply then Cancel, the settings are reset and reinitialized with the new
186 // settings. CANCEL is returned.
187 // - Cancel, the settings are not changed, the previous setting, if it was
188 // initialized before, are kept. CANCEL is returned.
189 // On failure, the settings are reset and FAILED is returned.
190 PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) };
191 dialog_options.hwndOwner = window;
192 // Disable options we don't support currently.
193 // TODO(maruel): Reuse the previously loaded settings!
194 dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
195 PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE;
196 if (!has_selection)
197 dialog_options.Flags |= PD_NOSELECTION;
199 PRINTPAGERANGE ranges[32];
200 dialog_options.nStartPage = START_PAGE_GENERAL;
201 if (max_pages) {
202 // Default initialize to print all the pages.
203 memset(ranges, 0, sizeof(ranges));
204 ranges[0].nFromPage = 1;
205 ranges[0].nToPage = max_pages;
206 dialog_options.nPageRanges = 1;
207 dialog_options.nMaxPageRanges = arraysize(ranges);
208 dialog_options.nMinPage = 1;
209 dialog_options.nMaxPage = max_pages;
210 dialog_options.lpPageRanges = ranges;
211 } else {
212 // No need to bother, we don't know how many pages are available.
213 dialog_options.Flags |= PD_NOPAGENUMS;
216 if (ShowPrintDialog(&dialog_options) != S_OK) {
217 ResetSettings();
218 callback.Run(FAILED);
221 // TODO(maruel): Support PD_PRINTTOFILE.
222 callback.Run(ParseDialogResultEx(dialog_options));
225 PrintingContext::Result PrintingContextWin::UseDefaultSettings() {
226 DCHECK(!in_print_job_);
228 PRINTDLG dialog_options = { sizeof(PRINTDLG) };
229 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT;
230 if (PrintDlg(&dialog_options))
231 return ParseDialogResult(dialog_options);
233 // No default printer configured, do we have any printers at all?
234 DWORD bytes_needed = 0;
235 DWORD count_returned = 0;
236 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
237 NULL, 2, NULL, 0, &bytes_needed, &count_returned);
238 if (bytes_needed) {
239 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2));
240 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]);
241 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS,
242 NULL, 2, printer_info_buffer.get(),
243 bytes_needed, &bytes_needed,
244 &count_returned);
245 if (ret && count_returned) { // have printers
246 // Open the first successfully found printer.
247 const PRINTER_INFO_2* info_2 =
248 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get());
249 const PRINTER_INFO_2* info_2_end = info_2 + count_returned;
250 for (; info_2 < info_2_end; ++info_2) {
251 ScopedPrinterHandle printer;
252 if (!printer.OpenPrinter(info_2->pPrinterName))
253 continue;
254 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode =
255 CreateDevMode(printer, NULL);
256 if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(),
257 &context_)) {
258 continue;
260 if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0,
261 false)) {
262 return OK;
264 ReleaseContext();
266 if (context_)
267 return OK;
271 ResetSettings();
272 return FAILED;
275 gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() {
276 // Default fallback to Letter size.
277 gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch);
279 // Get settings from locale. Paper type buffer length is at most 4.
280 const int paper_type_buffer_len = 4;
281 wchar_t paper_type_buffer[paper_type_buffer_len] = {0};
282 GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer,
283 paper_type_buffer_len);
284 if (wcslen(paper_type_buffer)) { // The call succeeded.
285 int paper_code = _wtoi(paper_type_buffer);
286 switch (paper_code) {
287 case DMPAPER_LEGAL:
288 paper_size.SetSize(kLegalWidthInch, kLegalHeightInch);
289 break;
290 case DMPAPER_A4:
291 paper_size.SetSize(kA4WidthInch, kA4HeightInch);
292 break;
293 case DMPAPER_A3:
294 paper_size.SetSize(kA3WidthInch, kA3HeightInch);
295 break;
296 default: // DMPAPER_LETTER is used for default fallback.
297 break;
300 return gfx::Size(
301 paper_size.width() * settings_.device_units_per_inch(),
302 paper_size.height() * settings_.device_units_per_inch());
305 PrintingContext::Result PrintingContextWin::UpdatePrinterSettings(
306 bool external_preview) {
307 DCHECK(!in_print_job_);
308 DCHECK(!external_preview) << "Not implemented";
310 ScopedPrinterHandle printer;
311 if (!printer.OpenPrinter(settings_.device_name().c_str()))
312 return OnError();
314 // Make printer changes local to Chrome.
315 // See MSDN documentation regarding DocumentProperties.
316 scoped_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode =
317 CreateDevModeWithColor(printer, settings_.device_name(),
318 settings_.color() != GRAY);
319 if (!scoped_dev_mode)
320 return OnError();
323 DEVMODE* dev_mode = scoped_dev_mode.get();
324 dev_mode->dmCopies = std::max(settings_.copies(), 1);
325 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies
326 dev_mode->dmFields |= DM_COPIES;
327 dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE :
328 DMCOLLATE_FALSE;
331 switch (settings_.duplex_mode()) {
332 case LONG_EDGE:
333 dev_mode->dmFields |= DM_DUPLEX;
334 dev_mode->dmDuplex = DMDUP_VERTICAL;
335 break;
336 case SHORT_EDGE:
337 dev_mode->dmFields |= DM_DUPLEX;
338 dev_mode->dmDuplex = DMDUP_HORIZONTAL;
339 break;
340 case SIMPLEX:
341 dev_mode->dmFields |= DM_DUPLEX;
342 dev_mode->dmDuplex = DMDUP_SIMPLEX;
343 break;
344 default: // UNKNOWN_DUPLEX_MODE
345 break;
348 dev_mode->dmFields |= DM_ORIENTATION;
349 dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE :
350 DMORIENT_PORTRAIT;
353 // Update data using DocumentProperties.
354 scoped_dev_mode = CreateDevMode(printer, scoped_dev_mode.get());
355 if (!scoped_dev_mode)
356 return OnError();
358 // Set printer then refresh printer settings.
359 if (!AllocateContext(settings_.device_name(), scoped_dev_mode.get(),
360 &context_)) {
361 return OnError();
363 PrintSettingsInitializerWin::InitPrintSettings(context_,
364 *scoped_dev_mode.get(),
365 &settings_);
366 return OK;
369 PrintingContext::Result PrintingContextWin::InitWithSettings(
370 const PrintSettings& settings) {
371 DCHECK(!in_print_job_);
373 settings_ = settings;
375 // TODO(maruel): settings_.ToDEVMODE()
376 ScopedPrinterHandle printer;
377 if (!printer.OpenPrinter(settings_.device_name().c_str())) {
378 return FAILED;
381 Result status = OK;
383 if (!GetPrinterSettings(printer, settings_.device_name()))
384 status = FAILED;
386 if (status != OK)
387 ResetSettings();
388 return status;
391 PrintingContext::Result PrintingContextWin::NewDocument(
392 const base::string16& document_name) {
393 DCHECK(!in_print_job_);
394 if (!context_)
395 return OnError();
397 // Set the flag used by the AbortPrintJob dialog procedure.
398 abort_printing_ = false;
400 in_print_job_ = true;
402 // Register the application's AbortProc function with GDI.
403 if (SP_ERROR == SetAbortProc(context_, &AbortProc))
404 return OnError();
406 DCHECK(SimplifyDocumentTitle(document_name) == document_name);
407 DOCINFO di = { sizeof(DOCINFO) };
408 di.lpszDocName = document_name.c_str();
410 // Is there a debug dump directory specified? If so, force to print to a file.
411 base::FilePath debug_dump_path = PrintedDocument::debug_dump_path();
412 if (!debug_dump_path.empty()) {
413 // Create a filename.
414 std::wstring filename;
415 Time now(Time::Now());
416 filename = base::TimeFormatShortDateNumeric(now);
417 filename += L"_";
418 filename += base::TimeFormatTimeOfDay(now);
419 filename += L"_";
420 filename += document_name;
421 filename += L"_";
422 filename += L"buffer.prn";
423 file_util::ReplaceIllegalCharactersInPath(&filename, '_');
424 debug_dump_path = debug_dump_path.Append(filename);
425 di.lpszOutput = debug_dump_path.value().c_str();
428 // No message loop running in unit tests.
429 DCHECK(!base::MessageLoop::current() ||
430 !base::MessageLoop::current()->NestableTasksAllowed());
432 // Begin a print job by calling the StartDoc function.
433 // NOTE: StartDoc() starts a message loop. That causes a lot of problems with
434 // IPC. Make sure recursive task processing is disabled.
435 if (StartDoc(context_, &di) <= 0)
436 return OnError();
438 return OK;
441 PrintingContext::Result PrintingContextWin::NewPage() {
442 if (abort_printing_)
443 return CANCEL;
444 DCHECK(context_);
445 DCHECK(in_print_job_);
447 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
448 // ::StartPage().
450 return OK;
453 PrintingContext::Result PrintingContextWin::PageDone() {
454 if (abort_printing_)
455 return CANCEL;
456 DCHECK(in_print_job_);
458 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
459 // ::EndPage().
461 return OK;
464 PrintingContext::Result PrintingContextWin::DocumentDone() {
465 if (abort_printing_)
466 return CANCEL;
467 DCHECK(in_print_job_);
468 DCHECK(context_);
470 // Inform the driver that document has ended.
471 if (EndDoc(context_) <= 0)
472 return OnError();
474 ResetSettings();
475 return OK;
478 void PrintingContextWin::Cancel() {
479 abort_printing_ = true;
480 in_print_job_ = false;
481 if (context_)
482 CancelDC(context_);
483 if (dialog_box_) {
484 DestroyWindow(dialog_box_);
485 dialog_box_dismissed_ = true;
489 void PrintingContextWin::ReleaseContext() {
490 if (context_) {
491 DeleteDC(context_);
492 context_ = NULL;
496 gfx::NativeDrawingContext PrintingContextWin::context() const {
497 return context_;
500 // static
501 BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) {
502 if (nCode) {
503 // TODO(maruel): Need a way to find the right instance to set. Should
504 // leverage PrintJobManager here?
505 // abort_printing_ = true;
507 return true;
510 bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode,
511 const std::wstring& new_device_name,
512 const PRINTPAGERANGE* ranges,
513 int number_ranges,
514 bool selection_only) {
515 skia::InitializeDC(context_);
516 DCHECK(GetDeviceCaps(context_, CLIPCAPS));
517 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB);
518 DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64);
519 // Some printers don't advertise these.
520 // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING);
521 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA);
522 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
524 // StretchDIBits() support is needed for printing.
525 if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) ||
526 !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) {
527 NOTREACHED();
528 ResetSettings();
529 return false;
532 DCHECK(!in_print_job_);
533 DCHECK(context_);
534 PageRanges ranges_vector;
535 if (!selection_only) {
536 // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector.
537 ranges_vector.reserve(number_ranges);
538 for (int i = 0; i < number_ranges; ++i) {
539 PageRange range;
540 // Transfer from 1-based to 0-based.
541 range.from = ranges[i].nFromPage - 1;
542 range.to = ranges[i].nToPage - 1;
543 ranges_vector.push_back(range);
547 settings_.set_ranges(ranges_vector);
548 settings_.set_device_name(new_device_name);
549 settings_.set_selection_only(selection_only);
550 PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode,
551 &settings_);
553 return true;
556 bool PrintingContextWin::GetPrinterSettings(HANDLE printer,
557 const std::wstring& device_name) {
558 DCHECK(!in_print_job_);
560 scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode =
561 CreateDevMode(printer, NULL);
563 if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) {
564 ResetSettings();
565 return false;
568 return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false);
571 // static
572 bool PrintingContextWin::AllocateContext(const std::wstring& device_name,
573 const DEVMODE* dev_mode,
574 gfx::NativeDrawingContext* context) {
575 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode);
576 DCHECK(*context);
577 return *context != NULL;
580 PrintingContext::Result PrintingContextWin::ParseDialogResultEx(
581 const PRINTDLGEX& dialog_options) {
582 // If the user clicked OK or Apply then Cancel, but not only Cancel.
583 if (dialog_options.dwResultAction != PD_RESULT_CANCEL) {
584 // Start fresh.
585 ResetSettings();
587 DEVMODE* dev_mode = NULL;
588 if (dialog_options.hDevMode) {
589 dev_mode =
590 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
591 DCHECK(dev_mode);
594 std::wstring device_name;
595 if (dialog_options.hDevNames) {
596 DEVNAMES* dev_names =
597 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
598 DCHECK(dev_names);
599 if (dev_names) {
600 device_name = reinterpret_cast<const wchar_t*>(dev_names) +
601 dev_names->wDeviceOffset;
602 GlobalUnlock(dialog_options.hDevNames);
606 bool success = false;
607 if (dev_mode && !device_name.empty()) {
608 context_ = dialog_options.hDC;
609 PRINTPAGERANGE* page_ranges = NULL;
610 DWORD num_page_ranges = 0;
611 bool print_selection_only = false;
612 if (dialog_options.Flags & PD_PAGENUMS) {
613 page_ranges = dialog_options.lpPageRanges;
614 num_page_ranges = dialog_options.nPageRanges;
616 if (dialog_options.Flags & PD_SELECTION) {
617 print_selection_only = true;
619 success = InitializeSettings(*dev_mode,
620 device_name,
621 page_ranges,
622 num_page_ranges,
623 print_selection_only);
626 if (!success && dialog_options.hDC) {
627 DeleteDC(dialog_options.hDC);
628 context_ = NULL;
631 if (dev_mode) {
632 GlobalUnlock(dialog_options.hDevMode);
634 } else {
635 if (dialog_options.hDC) {
636 DeleteDC(dialog_options.hDC);
640 if (dialog_options.hDevMode != NULL)
641 GlobalFree(dialog_options.hDevMode);
642 if (dialog_options.hDevNames != NULL)
643 GlobalFree(dialog_options.hDevNames);
645 switch (dialog_options.dwResultAction) {
646 case PD_RESULT_PRINT:
647 return context_ ? OK : FAILED;
648 case PD_RESULT_APPLY:
649 return context_ ? CANCEL : FAILED;
650 case PD_RESULT_CANCEL:
651 return CANCEL;
652 default:
653 return FAILED;
657 HRESULT PrintingContextWin::ShowPrintDialog(PRINTDLGEX* options) {
658 // Note that this cannot use ui::BaseShellDialog as the print dialog is
659 // system modal: opening it from a background thread can cause Windows to
660 // get the wrong Z-order which will make the print dialog appear behind the
661 // browser frame (but still being modal) so neither the browser frame nor
662 // the print dialog will get any input. See http://crbug.com/342697
663 // http://crbug.com/180997 for details.
664 base::MessageLoop::ScopedNestableTaskAllower allow(
665 base::MessageLoop::current());
667 return PrintDlgEx(options);
670 PrintingContext::Result PrintingContextWin::ParseDialogResult(
671 const PRINTDLG& dialog_options) {
672 // If the user clicked OK or Apply then Cancel, but not only Cancel.
673 // Start fresh.
674 ResetSettings();
676 DEVMODE* dev_mode = NULL;
677 if (dialog_options.hDevMode) {
678 dev_mode =
679 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
680 DCHECK(dev_mode);
683 std::wstring device_name;
684 if (dialog_options.hDevNames) {
685 DEVNAMES* dev_names =
686 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
687 DCHECK(dev_names);
688 if (dev_names) {
689 device_name =
690 reinterpret_cast<const wchar_t*>(
691 reinterpret_cast<const wchar_t*>(dev_names) +
692 dev_names->wDeviceOffset);
693 GlobalUnlock(dialog_options.hDevNames);
697 bool success = false;
698 if (dev_mode && !device_name.empty()) {
699 context_ = dialog_options.hDC;
700 success = InitializeSettings(*dev_mode, device_name, NULL, 0, false);
703 if (!success && dialog_options.hDC) {
704 DeleteDC(dialog_options.hDC);
705 context_ = NULL;
708 if (dev_mode) {
709 GlobalUnlock(dialog_options.hDevMode);
712 if (dialog_options.hDevMode != NULL)
713 GlobalFree(dialog_options.hDevMode);
714 if (dialog_options.hDevNames != NULL)
715 GlobalFree(dialog_options.hDevNames);
717 return context_ ? OK : FAILED;
720 } // namespace printing