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"
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 "base/win/metro.h"
19 #include "printing/backend/print_backend.h"
20 #include "printing/backend/printing_info_win.h"
21 #include "printing/backend/win_helper.h"
22 #include "printing/print_job_constants.h"
23 #include "printing/print_settings_initializer_win.h"
24 #include "printing/printed_document.h"
25 #include "printing/printing_utils.h"
26 #include "printing/units.h"
27 #include "skia/ext/platform_device.h"
28 #include "win8/util/win8_util.h"
31 #include "ui/aura/remote_root_window_host_win.h"
32 #include "ui/aura/root_window.h"
33 #include "ui/aura/window.h"
40 HWND
GetRootWindow(gfx::NativeView view
) {
44 window
= view
->GetDispatcher()->host()->GetAcceleratedWidget();
46 if (view
&& IsWindow(view
)) {
47 window
= GetAncestor(view
, GA_ROOTOWNER
);
51 // TODO(maruel): bug 1214347 Get the right browser window instead.
52 return GetDesktopWindow();
57 } // anonymous namespace
61 class PrintingContextWin::CallbackHandler
: public IPrintDialogCallback
,
62 public IObjectWithSite
{
64 CallbackHandler(PrintingContextWin
& owner
, HWND owner_hwnd
)
66 owner_hwnd_(owner_hwnd
),
75 IUnknown
* ToIUnknown() {
76 return static_cast<IUnknown
*>(static_cast<IPrintDialogCallback
*>(this));
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);
93 // No real ref counting.
94 virtual ULONG WINAPI
AddRef() {
97 virtual ULONG WINAPI
Release() {
101 // IPrintDialogCallback methods
102 virtual HRESULT WINAPI
InitDone() {
106 virtual HRESULT WINAPI
SelectionChange() {
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.
116 virtual HRESULT WINAPI
HandleMessage(HWND dialog
,
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
);
133 virtual HRESULT WINAPI
SetSite(IUnknown
* site
) {
136 services_
->Release();
138 // The dialog box is destroying, PrintJob::Worker don't need the handle
140 owner_
.dialog_box_
= NULL
;
142 DCHECK(services_
== NULL
);
143 HRESULT hr
= site
->QueryInterface(IID_IPrintDialogServices
,
144 reinterpret_cast<void**>(&services_
));
145 DCHECK(SUCCEEDED(hr
));
150 virtual HRESULT WINAPI
GetSite(REFIID riid
, void** site
) {
155 PrintingContextWin
& owner_
;
157 IPrintDialogServices
* services_
;
159 DISALLOW_COPY_AND_ASSIGN(CallbackHandler
);
163 PrintingContext
* PrintingContext::Create(const std::string
& app_locale
) {
164 return static_cast<PrintingContext
*>(new PrintingContextWin(app_locale
));
167 PrintingContextWin::PrintingContextWin(const std::string
& app_locale
)
168 : PrintingContext(app_locale
),
171 print_dialog_func_(&PrintDlgEx
) {
174 PrintingContextWin::~PrintingContextWin() {
178 void PrintingContextWin::AskUserForSettings(
179 gfx::NativeView view
, int max_pages
, bool has_selection
,
180 const PrintSettingsCallback
& callback
) {
181 DCHECK(!in_print_job_
);
182 // TODO(scottmg): Possibly this has to move into the threaded runner too?
183 if (win8::IsSingleWindowMetroMode()) {
184 // The system dialog can not be opened while running in Metro.
185 // But we can programatically launch the Metro print device charm though.
186 HMODULE metro_module
= base::win::GetMetroModule();
187 if (metro_module
!= NULL
) {
188 typedef void (*MetroShowPrintUI
)();
189 MetroShowPrintUI metro_show_print_ui
=
190 reinterpret_cast<MetroShowPrintUI
>(
191 ::GetProcAddress(metro_module
, "MetroShowPrintUI"));
192 if (metro_show_print_ui
) {
193 // TODO(mad): Remove this once we can send user metrics from the metro
194 // driver. crbug.com/142330
195 UMA_HISTOGRAM_ENUMERATION("Metro.Print", 1, 2);
196 metro_show_print_ui();
199 return callback
.Run(CANCEL
);
201 dialog_box_dismissed_
= false;
203 HWND window
= GetRootWindow(view
);
206 // Show the OS-dependent dialog box.
208 // - OK, the settings are reset and reinitialized with the new settings. OK is
210 // - Apply then Cancel, the settings are reset and reinitialized with the new
211 // settings. CANCEL is returned.
212 // - Cancel, the settings are not changed, the previous setting, if it was
213 // initialized before, are kept. CANCEL is returned.
214 // On failure, the settings are reset and FAILED is returned.
215 PRINTDLGEX
* dialog_options
=
216 reinterpret_cast<PRINTDLGEX
*>(malloc(sizeof(PRINTDLGEX
)));
217 ZeroMemory(dialog_options
, sizeof(PRINTDLGEX
));
218 dialog_options
->lStructSize
= sizeof(PRINTDLGEX
);
219 dialog_options
->hwndOwner
= window
;
220 // Disable options we don't support currently.
221 // TODO(maruel): Reuse the previously loaded settings!
222 dialog_options
->Flags
= PD_RETURNDC
| PD_USEDEVMODECOPIESANDCOLLATE
|
223 PD_NOCURRENTPAGE
| PD_HIDEPRINTTOFILE
;
225 dialog_options
->Flags
|= PD_NOSELECTION
;
227 const size_t max_page_ranges
= 32;
228 PRINTPAGERANGE
* ranges
= new PRINTPAGERANGE
[max_page_ranges
];
229 dialog_options
->lpPageRanges
= ranges
;
230 dialog_options
->nStartPage
= START_PAGE_GENERAL
;
232 // Default initialize to print all the pages.
233 memset(ranges
, 0, sizeof(ranges
));
234 ranges
[0].nFromPage
= 1;
235 ranges
[0].nToPage
= max_pages
;
236 dialog_options
->nPageRanges
= 1;
237 dialog_options
->nMaxPageRanges
= max_page_ranges
;
238 dialog_options
->nMinPage
= 1;
239 dialog_options
->nMaxPage
= max_pages
;
241 // No need to bother, we don't know how many pages are available.
242 dialog_options
->Flags
|= PD_NOPAGENUMS
;
245 callback_
= callback
;
246 print_settings_dialog_
= new ui::PrintSettingsDialogWin(this);
247 print_settings_dialog_
->GetPrintSettings(
248 print_dialog_func_
, window
, dialog_options
);
251 PrintingContext::Result
PrintingContextWin::UseDefaultSettings() {
252 DCHECK(!in_print_job_
);
254 PRINTDLG dialog_options
= { sizeof(PRINTDLG
) };
255 dialog_options
.Flags
= PD_RETURNDC
| PD_RETURNDEFAULT
;
256 if (PrintDlg(&dialog_options
))
257 return ParseDialogResult(dialog_options
);
259 // No default printer configured, do we have any printers at all?
260 DWORD bytes_needed
= 0;
261 DWORD count_returned
= 0;
262 (void)::EnumPrinters(PRINTER_ENUM_LOCAL
|PRINTER_ENUM_CONNECTIONS
,
263 NULL
, 2, NULL
, 0, &bytes_needed
, &count_returned
);
265 DCHECK(bytes_needed
>= count_returned
* sizeof(PRINTER_INFO_2
));
266 scoped_ptr
<BYTE
[]> printer_info_buffer(new BYTE
[bytes_needed
]);
267 BOOL ret
= ::EnumPrinters(PRINTER_ENUM_LOCAL
|PRINTER_ENUM_CONNECTIONS
,
268 NULL
, 2, printer_info_buffer
.get(),
269 bytes_needed
, &bytes_needed
,
271 if (ret
&& count_returned
) { // have printers
272 // Open the first successfully found printer.
273 for (DWORD count
= 0; count
< count_returned
; ++count
) {
274 PRINTER_INFO_2
* info_2
= reinterpret_cast<PRINTER_INFO_2
*>(
275 printer_info_buffer
.get() + count
* sizeof(PRINTER_INFO_2
));
276 std::wstring printer_name
= info_2
->pPrinterName
;
277 if (info_2
->pDevMode
== NULL
|| printer_name
.length() == 0)
279 if (!AllocateContext(printer_name
, info_2
->pDevMode
, &context_
))
281 if (InitializeSettings(*info_2
->pDevMode
, printer_name
,
296 gfx::Size
PrintingContextWin::GetPdfPaperSizeDeviceUnits() {
297 // Default fallback to Letter size.
298 gfx::SizeF
paper_size(kLetterWidthInch
, kLetterHeightInch
);
300 // Get settings from locale. Paper type buffer length is at most 4.
301 const int paper_type_buffer_len
= 4;
302 wchar_t paper_type_buffer
[paper_type_buffer_len
] = {0};
303 GetLocaleInfo(LOCALE_USER_DEFAULT
, LOCALE_IPAPERSIZE
, paper_type_buffer
,
304 paper_type_buffer_len
);
305 if (wcslen(paper_type_buffer
)) { // The call succeeded.
306 int paper_code
= _wtoi(paper_type_buffer
);
307 switch (paper_code
) {
309 paper_size
.SetSize(kLegalWidthInch
, kLegalHeightInch
);
312 paper_size
.SetSize(kA4WidthInch
, kA4HeightInch
);
315 paper_size
.SetSize(kA3WidthInch
, kA3HeightInch
);
317 default: // DMPAPER_LETTER is used for default fallback.
322 paper_size
.width() * settings_
.device_units_per_inch(),
323 paper_size
.height() * settings_
.device_units_per_inch());
326 PrintingContext::Result
PrintingContextWin::UpdatePrinterSettings(
327 bool external_preview
) {
328 DCHECK(!in_print_job_
);
329 DCHECK(!external_preview
) << "Not implemented";
331 ScopedPrinterHandle printer
;
332 LPWSTR device_name_wide
=
333 const_cast<wchar_t*>(settings_
.device_name().c_str());
334 if (!printer
.OpenPrinter(device_name_wide
))
337 // Make printer changes local to Chrome.
338 // See MSDN documentation regarding DocumentProperties.
339 scoped_ptr
<uint8
[]> buffer
;
340 DEVMODE
* dev_mode
= NULL
;
341 LONG buffer_size
= DocumentProperties(NULL
, printer
, device_name_wide
,
343 if (buffer_size
> 0) {
344 buffer
.reset(new uint8
[buffer_size
]);
345 memset(buffer
.get(), 0, buffer_size
);
346 if (DocumentProperties(NULL
, printer
, device_name_wide
,
347 reinterpret_cast<PDEVMODE
>(buffer
.get()), NULL
,
348 DM_OUT_BUFFER
) == IDOK
) {
349 dev_mode
= reinterpret_cast<PDEVMODE
>(buffer
.get());
352 if (dev_mode
== NULL
) {
357 if (settings_
.color() == GRAY
)
358 dev_mode
->dmColor
= DMCOLOR_MONOCHROME
;
360 dev_mode
->dmColor
= DMCOLOR_COLOR
;
362 dev_mode
->dmCopies
= std::max(settings_
.copies(), 1);
363 if (dev_mode
->dmCopies
> 1) { // do not change collate unless multiple copies
364 dev_mode
->dmCollate
= settings_
.collate() ? DMCOLLATE_TRUE
:
367 switch (settings_
.duplex_mode()) {
369 dev_mode
->dmDuplex
= DMDUP_VERTICAL
;
372 dev_mode
->dmDuplex
= DMDUP_HORIZONTAL
;
375 dev_mode
->dmDuplex
= DMDUP_SIMPLEX
;
377 default: // UNKNOWN_DUPLEX_MODE
380 dev_mode
->dmOrientation
= settings_
.landscape() ? DMORIENT_LANDSCAPE
:
383 // Update data using DocumentProperties.
384 if (DocumentProperties(NULL
, printer
, device_name_wide
, dev_mode
, dev_mode
,
385 DM_IN_BUFFER
| DM_OUT_BUFFER
) != IDOK
) {
389 // Set printer then refresh printer settings.
390 if (!AllocateContext(settings_
.device_name(), dev_mode
, &context_
)) {
393 PrintSettingsInitializerWin::InitPrintSettings(context_
, *dev_mode
,
398 PrintingContext::Result
PrintingContextWin::InitWithSettings(
399 const PrintSettings
& settings
) {
400 DCHECK(!in_print_job_
);
402 settings_
= settings
;
404 // TODO(maruel): settings_.ToDEVMODE()
405 ScopedPrinterHandle printer
;
406 if (!printer
.OpenPrinter(settings_
.device_name().c_str())) {
412 if (!GetPrinterSettings(printer
, settings_
.device_name()))
420 PrintingContext::Result
PrintingContextWin::NewDocument(
421 const base::string16
& document_name
) {
422 DCHECK(!in_print_job_
);
426 // Set the flag used by the AbortPrintJob dialog procedure.
427 abort_printing_
= false;
429 in_print_job_
= true;
431 // Register the application's AbortProc function with GDI.
432 if (SP_ERROR
== SetAbortProc(context_
, &AbortProc
))
435 DCHECK(SimplifyDocumentTitle(document_name
) == document_name
);
436 DOCINFO di
= { sizeof(DOCINFO
) };
437 const std::wstring
& document_name_wide
= base::UTF16ToWide(document_name
);
438 di
.lpszDocName
= document_name_wide
.c_str();
440 // Is there a debug dump directory specified? If so, force to print to a file.
441 base::FilePath debug_dump_path
= PrintedDocument::debug_dump_path();
442 if (!debug_dump_path
.empty()) {
443 // Create a filename.
444 std::wstring filename
;
445 Time
now(Time::Now());
446 filename
= base::TimeFormatShortDateNumeric(now
);
448 filename
+= base::TimeFormatTimeOfDay(now
);
450 filename
+= base::UTF16ToWide(document_name
);
452 filename
+= L
"buffer.prn";
453 file_util::ReplaceIllegalCharactersInPath(&filename
, '_');
454 debug_dump_path
.Append(filename
);
455 di
.lpszOutput
= debug_dump_path
.value().c_str();
458 // No message loop running in unit tests.
459 DCHECK(!base::MessageLoop::current() ||
460 !base::MessageLoop::current()->NestableTasksAllowed());
462 // Begin a print job by calling the StartDoc function.
463 // NOTE: StartDoc() starts a message loop. That causes a lot of problems with
464 // IPC. Make sure recursive task processing is disabled.
465 if (StartDoc(context_
, &di
) <= 0)
471 PrintingContext::Result
PrintingContextWin::NewPage() {
475 DCHECK(in_print_job_
);
477 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
483 PrintingContext::Result
PrintingContextWin::PageDone() {
486 DCHECK(in_print_job_
);
488 // Intentional No-op. NativeMetafile::SafePlayback takes care of calling
494 PrintingContext::Result
PrintingContextWin::DocumentDone() {
497 DCHECK(in_print_job_
);
500 // Inform the driver that document has ended.
501 if (EndDoc(context_
) <= 0)
508 void PrintingContextWin::Cancel() {
509 abort_printing_
= true;
510 in_print_job_
= false;
514 DestroyWindow(dialog_box_
);
515 dialog_box_dismissed_
= true;
519 void PrintingContextWin::ReleaseContext() {
526 gfx::NativeDrawingContext
PrintingContextWin::context() const {
530 void PrintingContextWin::PrintSettingsConfirmed(PRINTDLGEX
* dialog_options
) {
531 // TODO(maruel): Support PD_PRINTTOFILE.
532 callback_
.Run(ParseDialogResultEx(*dialog_options
));
533 delete [] dialog_options
->lpPageRanges
;
534 free(dialog_options
);
537 void PrintingContextWin::PrintSettingsCancelled(PRINTDLGEX
* dialog_options
) {
539 callback_
.Run(FAILED
);
540 delete [] dialog_options
->lpPageRanges
;
541 free(dialog_options
);
545 BOOL
PrintingContextWin::AbortProc(HDC hdc
, int nCode
) {
547 // TODO(maruel): Need a way to find the right instance to set. Should
548 // leverage PrintJobManager here?
549 // abort_printing_ = true;
554 bool PrintingContextWin::InitializeSettings(const DEVMODE
& dev_mode
,
555 const std::wstring
& new_device_name
,
556 const PRINTPAGERANGE
* ranges
,
558 bool selection_only
) {
559 skia::InitializeDC(context_
);
560 DCHECK(GetDeviceCaps(context_
, CLIPCAPS
));
561 DCHECK(GetDeviceCaps(context_
, RASTERCAPS
) & RC_STRETCHDIB
);
562 DCHECK(GetDeviceCaps(context_
, RASTERCAPS
) & RC_BITMAP64
);
563 // Some printers don't advertise these.
564 // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING);
565 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA);
566 // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
568 // StretchDIBits() support is needed for printing.
569 if (!(GetDeviceCaps(context_
, RASTERCAPS
) & RC_STRETCHDIB
) ||
570 !(GetDeviceCaps(context_
, RASTERCAPS
) & RC_BITMAP64
)) {
576 DCHECK(!in_print_job_
);
578 PageRanges ranges_vector
;
579 if (!selection_only
) {
580 // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector.
581 ranges_vector
.reserve(number_ranges
);
582 for (int i
= 0; i
< number_ranges
; ++i
) {
584 // Transfer from 1-based to 0-based.
585 range
.from
= ranges
[i
].nFromPage
- 1;
586 range
.to
= ranges
[i
].nToPage
- 1;
587 ranges_vector
.push_back(range
);
591 settings_
.set_ranges(ranges_vector
);
592 settings_
.set_device_name(new_device_name
);
593 settings_
.set_selection_only(selection_only
);
594 PrintSettingsInitializerWin::InitPrintSettings(context_
, dev_mode
,
600 bool PrintingContextWin::GetPrinterSettings(HANDLE printer
,
601 const std::wstring
& device_name
) {
602 DCHECK(!in_print_job_
);
604 UserDefaultDevMode user_settings
;
606 if (!user_settings
.Init(printer
) ||
607 !AllocateContext(device_name
, user_settings
.get(), &context_
)) {
612 return InitializeSettings(*user_settings
.get(), device_name
, NULL
, 0, false);
616 bool PrintingContextWin::AllocateContext(const std::wstring
& device_name
,
617 const DEVMODE
* dev_mode
,
618 gfx::NativeDrawingContext
* context
) {
619 *context
= CreateDC(L
"WINSPOOL", device_name
.c_str(), NULL
, dev_mode
);
621 return *context
!= NULL
;
624 PrintingContext::Result
PrintingContextWin::ParseDialogResultEx(
625 const PRINTDLGEX
& dialog_options
) {
626 // If the user clicked OK or Apply then Cancel, but not only Cancel.
627 if (dialog_options
.dwResultAction
!= PD_RESULT_CANCEL
) {
631 DEVMODE
* dev_mode
= NULL
;
632 if (dialog_options
.hDevMode
) {
634 reinterpret_cast<DEVMODE
*>(GlobalLock(dialog_options
.hDevMode
));
638 std::wstring device_name
;
639 if (dialog_options
.hDevNames
) {
640 DEVNAMES
* dev_names
=
641 reinterpret_cast<DEVNAMES
*>(GlobalLock(dialog_options
.hDevNames
));
644 device_name
= reinterpret_cast<const wchar_t*>(dev_names
) +
645 dev_names
->wDeviceOffset
;
646 GlobalUnlock(dialog_options
.hDevNames
);
650 bool success
= false;
651 if (dev_mode
&& !device_name
.empty()) {
652 context_
= dialog_options
.hDC
;
653 PRINTPAGERANGE
* page_ranges
= NULL
;
654 DWORD num_page_ranges
= 0;
655 bool print_selection_only
= false;
656 if (dialog_options
.Flags
& PD_PAGENUMS
) {
657 page_ranges
= dialog_options
.lpPageRanges
;
658 num_page_ranges
= dialog_options
.nPageRanges
;
660 if (dialog_options
.Flags
& PD_SELECTION
) {
661 print_selection_only
= true;
663 success
= InitializeSettings(*dev_mode
,
667 print_selection_only
);
670 if (!success
&& dialog_options
.hDC
) {
671 DeleteDC(dialog_options
.hDC
);
676 GlobalUnlock(dialog_options
.hDevMode
);
679 if (dialog_options
.hDC
) {
680 DeleteDC(dialog_options
.hDC
);
684 if (dialog_options
.hDevMode
!= NULL
)
685 GlobalFree(dialog_options
.hDevMode
);
686 if (dialog_options
.hDevNames
!= NULL
)
687 GlobalFree(dialog_options
.hDevNames
);
689 switch (dialog_options
.dwResultAction
) {
690 case PD_RESULT_PRINT
:
691 return context_
? OK
: FAILED
;
692 case PD_RESULT_APPLY
:
693 return context_
? CANCEL
: FAILED
;
694 case PD_RESULT_CANCEL
:
701 PrintingContext::Result
PrintingContextWin::ParseDialogResult(
702 const PRINTDLG
& dialog_options
) {
703 // If the user clicked OK or Apply then Cancel, but not only Cancel.
707 DEVMODE
* dev_mode
= NULL
;
708 if (dialog_options
.hDevMode
) {
710 reinterpret_cast<DEVMODE
*>(GlobalLock(dialog_options
.hDevMode
));
714 std::wstring device_name
;
715 if (dialog_options
.hDevNames
) {
716 DEVNAMES
* dev_names
=
717 reinterpret_cast<DEVNAMES
*>(GlobalLock(dialog_options
.hDevNames
));
721 reinterpret_cast<const wchar_t*>(
722 reinterpret_cast<const wchar_t*>(dev_names
) +
723 dev_names
->wDeviceOffset
);
724 GlobalUnlock(dialog_options
.hDevNames
);
728 bool success
= false;
729 if (dev_mode
&& !device_name
.empty()) {
730 context_
= dialog_options
.hDC
;
731 success
= InitializeSettings(*dev_mode
, device_name
, NULL
, 0, false);
734 if (!success
&& dialog_options
.hDC
) {
735 DeleteDC(dialog_options
.hDC
);
740 GlobalUnlock(dialog_options
.hDevMode
);
743 if (dialog_options
.hDevMode
!= NULL
)
744 GlobalFree(dialog_options
.hDevMode
);
745 if (dialog_options
.hDevNames
!= NULL
)
746 GlobalFree(dialog_options
.hDevNames
);
748 return context_
? OK
: FAILED
;
751 } // namespace printing