vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 1 | // Copyright 2014 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. |
| 4 | |
| 5 | #include "printing/printing_context_system_dialog_win.h" |
| 6 | |
Lei Zhang | 9fae89c | 2018-09-26 03:29:48 | [diff] [blame] | 7 | #include <utility> |
| 8 | |
vitalybuka | a58ec6a | 2015-02-23 18:30:49 | [diff] [blame] | 9 | #include "base/auto_reset.h" |
Gabriel Charette | d258c51 | 2018-04-20 00:50:45 | [diff] [blame] | 10 | #include "base/message_loop/message_loop_current.h" |
Lei Zhang | 9fae89c | 2018-09-26 03:29:48 | [diff] [blame] | 11 | #include "base/stl_util.h" |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 12 | #include "printing/backend/win_helper.h" |
| 13 | #include "printing/print_settings_initializer_win.h" |
tomhudson | 828dddb | 2015-12-04 14:34:16 | [diff] [blame] | 14 | #include "skia/ext/skia_utils_win.h" |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 15 | |
| 16 | namespace printing { |
| 17 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 18 | PrintingContextSystemDialogWin::PrintingContextSystemDialogWin( |
| 19 | Delegate* delegate) |
| 20 | : PrintingContextWin(delegate) {} |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 21 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 22 | PrintingContextSystemDialogWin::~PrintingContextSystemDialogWin() {} |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 23 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 24 | void PrintingContextSystemDialogWin::AskUserForSettings( |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 25 | int max_pages, |
| 26 | bool has_selection, |
dgn | 4c172eea | 2014-12-15 21:11:23 | [diff] [blame] | 27 | bool is_scripted, |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 28 | PrintSettingsCallback callback) { |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 29 | DCHECK(!in_print_job_); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 30 | |
| 31 | HWND window = GetRootWindow(delegate_->GetParentView()); |
| 32 | DCHECK(window); |
| 33 | |
| 34 | // Show the OS-dependent dialog box. |
| 35 | // If the user press |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 36 | // - OK, the settings are reset and reinitialized with the new settings. OK is |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 37 | // returned. |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 38 | // - Apply then Cancel, the settings are reset and reinitialized with the new |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 39 | // settings. CANCEL is returned. |
| 40 | // - Cancel, the settings are not changed, the previous setting, if it was |
| 41 | // initialized before, are kept. CANCEL is returned. |
| 42 | // On failure, the settings are reset and FAILED is returned. |
| 43 | PRINTDLGEX dialog_options = {sizeof(PRINTDLGEX)}; |
| 44 | dialog_options.hwndOwner = window; |
| 45 | // Disable options we don't support currently. |
| 46 | // TODO(maruel): Reuse the previously loaded settings! |
| 47 | dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | |
| 48 | PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE; |
| 49 | if (!has_selection) |
| 50 | dialog_options.Flags |= PD_NOSELECTION; |
| 51 | |
| 52 | PRINTPAGERANGE ranges[32]; |
| 53 | dialog_options.nStartPage = START_PAGE_GENERAL; |
| 54 | if (max_pages) { |
| 55 | // Default initialize to print all the pages. |
| 56 | memset(ranges, 0, sizeof(ranges)); |
| 57 | ranges[0].nFromPage = 1; |
| 58 | ranges[0].nToPage = max_pages; |
| 59 | dialog_options.nPageRanges = 1; |
Lei Zhang | 9fae89c | 2018-09-26 03:29:48 | [diff] [blame] | 60 | dialog_options.nMaxPageRanges = base::size(ranges); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 61 | dialog_options.nMinPage = 1; |
| 62 | dialog_options.nMaxPage = max_pages; |
| 63 | dialog_options.lpPageRanges = ranges; |
| 64 | } else { |
| 65 | // No need to bother, we don't know how many pages are available. |
| 66 | dialog_options.Flags |= PD_NOPAGENUMS; |
| 67 | } |
| 68 | |
| 69 | if (ShowPrintDialog(&dialog_options) != S_OK) { |
Vladislav Kuzkokov | 56e6bbbd | 2019-08-21 18:48:18 | [diff] [blame] | 70 | ResetSettings(); |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 71 | std::move(callback).Run(FAILED); |
vitalybuka | 442258d | 2015-04-11 01:46:42 | [diff] [blame] | 72 | return; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | // TODO(maruel): Support PD_PRINTTOFILE. |
Vladislav Kuzkokov | 48ceab2 | 2018-02-14 16:29:28 | [diff] [blame] | 76 | std::move(callback).Run(ParseDialogResultEx(dialog_options)); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 77 | } |
| 78 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 79 | HRESULT PrintingContextSystemDialogWin::ShowPrintDialog(PRINTDLGEX* options) { |
vitalybuka | a58ec6a | 2015-02-23 18:30:49 | [diff] [blame] | 80 | // Runs always on the UI thread. |
| 81 | static bool is_dialog_shown = false; |
| 82 | if (is_dialog_shown) |
| 83 | return E_FAIL; |
| 84 | // Block opening dialog from nested task. It crashes PrintDlgEx. |
| 85 | base::AutoReset<bool> auto_reset(&is_dialog_shown, true); |
| 86 | |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 87 | // Note that this cannot use ui::BaseShellDialog as the print dialog is |
| 88 | // system modal: opening it from a background thread can cause Windows to |
| 89 | // get the wrong Z-order which will make the print dialog appear behind the |
| 90 | // browser frame (but still being modal) so neither the browser frame nor |
| 91 | // the print dialog will get any input. See http://crbug.com/342697 |
| 92 | // http://crbug.com/180997 for details. |
Gabriel Charette | d258c51 | 2018-04-20 00:50:45 | [diff] [blame] | 93 | base::MessageLoopCurrent::ScopedNestableTaskAllower allow; |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 94 | |
| 95 | return PrintDlgEx(options); |
| 96 | } |
| 97 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 98 | bool PrintingContextSystemDialogWin::InitializeSettingsWithRanges( |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 99 | const DEVMODE& dev_mode, |
| 100 | const std::wstring& new_device_name, |
| 101 | const PRINTPAGERANGE* ranges, |
| 102 | int number_ranges, |
| 103 | bool selection_only) { |
| 104 | DCHECK(GetDeviceCaps(context(), CLIPCAPS)); |
| 105 | DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_STRETCHDIB); |
| 106 | DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_BITMAP64); |
| 107 | // Some printers don't advertise these. |
| 108 | // DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_SCALING); |
| 109 | // DCHECK(GetDeviceCaps(context(), SHADEBLENDCAPS) & SB_CONST_ALPHA); |
| 110 | // DCHECK(GetDeviceCaps(context(), SHADEBLENDCAPS) & SB_PIXEL_ALPHA); |
| 111 | |
| 112 | // StretchDIBits() support is needed for printing. |
| 113 | if (!(GetDeviceCaps(context(), RASTERCAPS) & RC_STRETCHDIB) || |
| 114 | !(GetDeviceCaps(context(), RASTERCAPS) & RC_BITMAP64)) { |
| 115 | NOTREACHED(); |
Vladislav Kuzkokov | 56e6bbbd | 2019-08-21 18:48:18 | [diff] [blame] | 116 | ResetSettings(); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 117 | return false; |
| 118 | } |
| 119 | |
| 120 | DCHECK(!in_print_job_); |
| 121 | DCHECK(context()); |
| 122 | PageRanges ranges_vector; |
| 123 | if (!selection_only) { |
| 124 | // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector. |
| 125 | ranges_vector.reserve(number_ranges); |
| 126 | for (int i = 0; i < number_ranges; ++i) { |
| 127 | PageRange range; |
| 128 | // Transfer from 1-based to 0-based. |
| 129 | range.from = ranges[i].nFromPage - 1; |
| 130 | range.to = ranges[i].nToPage - 1; |
| 131 | ranges_vector.push_back(range); |
| 132 | } |
| 133 | } |
| 134 | |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 135 | settings_->set_ranges(ranges_vector); |
| 136 | settings_->set_device_name(new_device_name); |
| 137 | settings_->set_selection_only(selection_only); |
Lei Zhang | 01a1d3c0 | 2019-05-21 04:59:10 | [diff] [blame] | 138 | PrintSettingsInitializerWin::InitPrintSettings(context(), dev_mode, |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 139 | settings_.get()); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 140 | |
| 141 | return true; |
| 142 | } |
| 143 | |
thestig | e85e6b6 | 2016-08-25 00:00:06 | [diff] [blame] | 144 | PrintingContext::Result PrintingContextSystemDialogWin::ParseDialogResultEx( |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 145 | const PRINTDLGEX& dialog_options) { |
| 146 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 147 | if (dialog_options.dwResultAction != PD_RESULT_CANCEL) { |
Lei Zhang | 3764d52b | 2018-07-24 22:31:11 | [diff] [blame] | 148 | // Start fresh, but preserve is_modifiable and GDI print setting. |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 149 | bool is_modifiable = settings_->is_modifiable(); |
| 150 | bool print_text_with_gdi = settings_->print_text_with_gdi(); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 151 | ResetSettings(); |
Vladislav Kuzkokov | 1999822 | 2019-08-12 14:26:09 | [diff] [blame] | 152 | settings_->set_is_modifiable(is_modifiable); |
| 153 | settings_->set_print_text_with_gdi(print_text_with_gdi); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 154 | |
| 155 | DEVMODE* dev_mode = NULL; |
| 156 | if (dialog_options.hDevMode) { |
| 157 | dev_mode = |
| 158 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 159 | DCHECK(dev_mode); |
| 160 | } |
| 161 | |
| 162 | std::wstring device_name; |
| 163 | if (dialog_options.hDevNames) { |
| 164 | DEVNAMES* dev_names = |
| 165 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 166 | DCHECK(dev_names); |
| 167 | if (dev_names) { |
| 168 | device_name = reinterpret_cast<const wchar_t*>(dev_names) + |
| 169 | dev_names->wDeviceOffset; |
| 170 | GlobalUnlock(dialog_options.hDevNames); |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | bool success = false; |
| 175 | if (dev_mode && !device_name.empty()) { |
| 176 | set_context(dialog_options.hDC); |
| 177 | PRINTPAGERANGE* page_ranges = NULL; |
| 178 | DWORD num_page_ranges = 0; |
| 179 | bool print_selection_only = false; |
| 180 | if (dialog_options.Flags & PD_PAGENUMS) { |
| 181 | page_ranges = dialog_options.lpPageRanges; |
| 182 | num_page_ranges = dialog_options.nPageRanges; |
| 183 | } |
| 184 | if (dialog_options.Flags & PD_SELECTION) { |
| 185 | print_selection_only = true; |
| 186 | } |
mgiuca | 8ca5918 | 2015-07-08 02:10:21 | [diff] [blame] | 187 | success = |
| 188 | InitializeSettingsWithRanges(*dev_mode, device_name, page_ranges, |
| 189 | num_page_ranges, print_selection_only); |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | if (!success && dialog_options.hDC) { |
| 193 | DeleteDC(dialog_options.hDC); |
| 194 | set_context(NULL); |
| 195 | } |
| 196 | |
| 197 | if (dev_mode) { |
| 198 | GlobalUnlock(dialog_options.hDevMode); |
| 199 | } |
| 200 | } else { |
| 201 | if (dialog_options.hDC) { |
| 202 | DeleteDC(dialog_options.hDC); |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | if (dialog_options.hDevMode != NULL) |
| 207 | GlobalFree(dialog_options.hDevMode); |
| 208 | if (dialog_options.hDevNames != NULL) |
| 209 | GlobalFree(dialog_options.hDevNames); |
| 210 | |
| 211 | switch (dialog_options.dwResultAction) { |
| 212 | case PD_RESULT_PRINT: |
| 213 | return context() ? OK : FAILED; |
| 214 | case PD_RESULT_APPLY: |
| 215 | return context() ? CANCEL : FAILED; |
| 216 | case PD_RESULT_CANCEL: |
| 217 | return CANCEL; |
| 218 | default: |
| 219 | return FAILED; |
| 220 | } |
| 221 | } |
| 222 | |
vitalybuka | f9d0c0c | 2014-09-09 19:53:33 | [diff] [blame] | 223 | } // namespace printing |