blob: b89f1a4e469ac89ca10b34942a95f8fd35a21010 [file] [log] [blame]
vitalybukaf9d0c0c2014-09-09 19:53:331// 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
vitalybukaa58ec6a2015-02-23 18:30:497#include "base/auto_reset.h"
vitalybukaf9d0c0c2014-09-09 19:53:338#include "base/message_loop/message_loop.h"
9#include "printing/backend/win_helper.h"
10#include "printing/print_settings_initializer_win.h"
11#include "skia/ext/platform_device.h"
12
13namespace printing {
14
15PrintingContextSytemDialogWin::PrintingContextSytemDialogWin(Delegate* delegate)
16 : PrintingContextWin(delegate), dialog_box_(NULL) {
17}
18
19PrintingContextSytemDialogWin::~PrintingContextSytemDialogWin() {
20}
21
22void PrintingContextSytemDialogWin::AskUserForSettings(
23 int max_pages,
24 bool has_selection,
dgn4c172eea2014-12-15 21:11:2325 bool is_scripted,
vitalybukaf9d0c0c2014-09-09 19:53:3326 const PrintSettingsCallback& callback) {
27 DCHECK(!in_print_job_);
28 dialog_box_dismissed_ = false;
29
30 HWND window = GetRootWindow(delegate_->GetParentView());
31 DCHECK(window);
32
33 // Show the OS-dependent dialog box.
34 // If the user press
35 // - OK, the settings are reset and reinitialized with the new settings. OK
36 // is
37 // returned.
38 // - Apply then Cancel, the settings are reset and reinitialized with the
39 // new
40 // settings. CANCEL is returned.
41 // - Cancel, the settings are not changed, the previous setting, if it was
42 // initialized before, are kept. CANCEL is returned.
43 // On failure, the settings are reset and FAILED is returned.
44 PRINTDLGEX dialog_options = {sizeof(PRINTDLGEX)};
45 dialog_options.hwndOwner = window;
46 // Disable options we don't support currently.
47 // TODO(maruel): Reuse the previously loaded settings!
48 dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE |
49 PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE;
50 if (!has_selection)
51 dialog_options.Flags |= PD_NOSELECTION;
52
53 PRINTPAGERANGE ranges[32];
54 dialog_options.nStartPage = START_PAGE_GENERAL;
55 if (max_pages) {
56 // Default initialize to print all the pages.
57 memset(ranges, 0, sizeof(ranges));
58 ranges[0].nFromPage = 1;
59 ranges[0].nToPage = max_pages;
60 dialog_options.nPageRanges = 1;
61 dialog_options.nMaxPageRanges = arraysize(ranges);
62 dialog_options.nMinPage = 1;
63 dialog_options.nMaxPage = max_pages;
64 dialog_options.lpPageRanges = ranges;
65 } else {
66 // No need to bother, we don't know how many pages are available.
67 dialog_options.Flags |= PD_NOPAGENUMS;
68 }
69
70 if (ShowPrintDialog(&dialog_options) != S_OK) {
71 ResetSettings();
72 callback.Run(FAILED);
73 }
74
75 // TODO(maruel): Support PD_PRINTTOFILE.
76 callback.Run(ParseDialogResultEx(dialog_options));
77}
78
79void PrintingContextSytemDialogWin::Cancel() {
80 PrintingContextWin::Cancel();
81 if (dialog_box_) {
82 DestroyWindow(dialog_box_);
83 dialog_box_dismissed_ = true;
84 }
85}
86
87HRESULT PrintingContextSytemDialogWin::ShowPrintDialog(PRINTDLGEX* options) {
vitalybukaa58ec6a2015-02-23 18:30:4988 // Runs always on the UI thread.
89 static bool is_dialog_shown = false;
90 if (is_dialog_shown)
91 return E_FAIL;
92 // Block opening dialog from nested task. It crashes PrintDlgEx.
93 base::AutoReset<bool> auto_reset(&is_dialog_shown, true);
94
vitalybukaf9d0c0c2014-09-09 19:53:3395 // Note that this cannot use ui::BaseShellDialog as the print dialog is
96 // system modal: opening it from a background thread can cause Windows to
97 // get the wrong Z-order which will make the print dialog appear behind the
98 // browser frame (but still being modal) so neither the browser frame nor
99 // the print dialog will get any input. See http://crbug.com/342697
100 // http://crbug.com/180997 for details.
101 base::MessageLoop::ScopedNestableTaskAllower allow(
102 base::MessageLoop::current());
103
104 return PrintDlgEx(options);
105}
106
107bool PrintingContextSytemDialogWin::InitializeSettings(
108 const DEVMODE& dev_mode,
109 const std::wstring& new_device_name,
110 const PRINTPAGERANGE* ranges,
111 int number_ranges,
112 bool selection_only) {
113 DCHECK(GetDeviceCaps(context(), CLIPCAPS));
114 DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_STRETCHDIB);
115 DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_BITMAP64);
116 // Some printers don't advertise these.
117 // DCHECK(GetDeviceCaps(context(), RASTERCAPS) & RC_SCALING);
118 // DCHECK(GetDeviceCaps(context(), SHADEBLENDCAPS) & SB_CONST_ALPHA);
119 // DCHECK(GetDeviceCaps(context(), SHADEBLENDCAPS) & SB_PIXEL_ALPHA);
120
121 // StretchDIBits() support is needed for printing.
122 if (!(GetDeviceCaps(context(), RASTERCAPS) & RC_STRETCHDIB) ||
123 !(GetDeviceCaps(context(), RASTERCAPS) & RC_BITMAP64)) {
124 NOTREACHED();
125 ResetSettings();
126 return false;
127 }
128
129 DCHECK(!in_print_job_);
130 DCHECK(context());
131 PageRanges ranges_vector;
132 if (!selection_only) {
133 // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector.
134 ranges_vector.reserve(number_ranges);
135 for (int i = 0; i < number_ranges; ++i) {
136 PageRange range;
137 // Transfer from 1-based to 0-based.
138 range.from = ranges[i].nFromPage - 1;
139 range.to = ranges[i].nToPage - 1;
140 ranges_vector.push_back(range);
141 }
142 }
143
144 settings_.set_ranges(ranges_vector);
145 settings_.set_device_name(new_device_name);
146 settings_.set_selection_only(selection_only);
147 PrintSettingsInitializerWin::InitPrintSettings(
148 context(), dev_mode, &settings_);
149
150 return true;
151}
152
153PrintingContext::Result PrintingContextSytemDialogWin::ParseDialogResultEx(
154 const PRINTDLGEX& dialog_options) {
155 // If the user clicked OK or Apply then Cancel, but not only Cancel.
156 if (dialog_options.dwResultAction != PD_RESULT_CANCEL) {
157 // Start fresh.
158 ResetSettings();
159
160 DEVMODE* dev_mode = NULL;
161 if (dialog_options.hDevMode) {
162 dev_mode =
163 reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
164 DCHECK(dev_mode);
165 }
166
167 std::wstring device_name;
168 if (dialog_options.hDevNames) {
169 DEVNAMES* dev_names =
170 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
171 DCHECK(dev_names);
172 if (dev_names) {
173 device_name = reinterpret_cast<const wchar_t*>(dev_names) +
174 dev_names->wDeviceOffset;
175 GlobalUnlock(dialog_options.hDevNames);
176 }
177 }
178
179 bool success = false;
180 if (dev_mode && !device_name.empty()) {
181 set_context(dialog_options.hDC);
182 PRINTPAGERANGE* page_ranges = NULL;
183 DWORD num_page_ranges = 0;
184 bool print_selection_only = false;
185 if (dialog_options.Flags & PD_PAGENUMS) {
186 page_ranges = dialog_options.lpPageRanges;
187 num_page_ranges = dialog_options.nPageRanges;
188 }
189 if (dialog_options.Flags & PD_SELECTION) {
190 print_selection_only = true;
191 }
192 success = InitializeSettings(*dev_mode,
193 device_name,
194 page_ranges,
195 num_page_ranges,
196 print_selection_only);
197 }
198
199 if (!success && dialog_options.hDC) {
200 DeleteDC(dialog_options.hDC);
201 set_context(NULL);
202 }
203
204 if (dev_mode) {
205 GlobalUnlock(dialog_options.hDevMode);
206 }
207 } else {
208 if (dialog_options.hDC) {
209 DeleteDC(dialog_options.hDC);
210 }
211 }
212
213 if (dialog_options.hDevMode != NULL)
214 GlobalFree(dialog_options.hDevMode);
215 if (dialog_options.hDevNames != NULL)
216 GlobalFree(dialog_options.hDevNames);
217
218 switch (dialog_options.dwResultAction) {
219 case PD_RESULT_PRINT:
220 return context() ? OK : FAILED;
221 case PD_RESULT_APPLY:
222 return context() ? CANCEL : FAILED;
223 case PD_RESULT_CANCEL:
224 return CANCEL;
225 default:
226 return FAILED;
227 }
228}
229
230PrintingContext::Result PrintingContextSytemDialogWin::ParseDialogResult(
231 const PRINTDLG& dialog_options) {
232 // If the user clicked OK or Apply then Cancel, but not only Cancel.
233 // Start fresh.
234 ResetSettings();
235
236 DEVMODE* dev_mode = NULL;
237 if (dialog_options.hDevMode) {
238 dev_mode = reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode));
239 DCHECK(dev_mode);
240 }
241
242 std::wstring device_name;
243 if (dialog_options.hDevNames) {
244 DEVNAMES* dev_names =
245 reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames));
246 DCHECK(dev_names);
247 if (dev_names) {
248 device_name = reinterpret_cast<const wchar_t*>(
249 reinterpret_cast<const wchar_t*>(dev_names) +
250 dev_names->wDeviceOffset);
251 GlobalUnlock(dialog_options.hDevNames);
252 }
253 }
254
255 bool success = false;
256 if (dev_mode && !device_name.empty()) {
257 set_context(dialog_options.hDC);
258 success = InitializeSettings(*dev_mode, device_name, NULL, 0, false);
259 }
260
261 if (!success && dialog_options.hDC) {
262 DeleteDC(dialog_options.hDC);
263 set_context(NULL);
264 }
265
266 if (dev_mode) {
267 GlobalUnlock(dialog_options.hDevMode);
268 }
269
270 if (dialog_options.hDevMode != NULL)
271 GlobalFree(dialog_options.hDevMode);
272 if (dialog_options.hDevNames != NULL)
273 GlobalFree(dialog_options.hDevNames);
274
275 return context() ? OK : FAILED;
276}
277
278} // namespace printing