[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 1 | // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 5 | #include "printing/printing_context_win.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
| 7 | #include <winspool.h> |
| 8 | |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 9 | #include <algorithm> |
| 10 | |
[email protected] | d0767cb54 | 2009-10-08 17:38:30 | [diff] [blame] | 11 | #include "base/i18n/file_util_icu.h" |
[email protected] | 260d157 | 2009-10-09 18:50:35 | [diff] [blame] | 12 | #include "base/i18n/time_formatting.h" |
[email protected] | a7629597 | 2013-07-18 00:42:32 | [diff] [blame] | 13 | #include "base/message_loop/message_loop.h" |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 14 | #include "base/metrics/histogram.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 15 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 5d7eb86 | 2013-06-28 15:21:24 | [diff] [blame] | 16 | #include "base/time/time.h" |
[email protected] | 89f5aa8c | 2011-03-21 20:58:44 | [diff] [blame] | 17 | #include "base/values.h" |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 18 | #include "base/win/metro.h" |
[email protected] | 09083eb | 2012-09-20 05:58:43 | [diff] [blame] | 19 | #include "printing/backend/print_backend.h" |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 20 | #include "printing/backend/printing_info_win.h" |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 21 | #include "printing/backend/win_helper.h" |
[email protected] | c97e5e8 | 2011-04-05 18:50:23 | [diff] [blame] | 22 | #include "printing/print_job_constants.h" |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 23 | #include "printing/print_settings_initializer_win.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 24 | #include "printing/printed_document.h" |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 25 | #include "printing/printing_utils.h" |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 26 | #include "printing/units.h" |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 27 | #include "skia/ext/platform_device.h" |
[email protected] | bd04f249 | 2012-12-06 00:40:31 | [diff] [blame] | 28 | #include "win8/util/win8_util.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 30 | #if defined(USE_AURA) |
[email protected] | e8cf733 | 2014-02-14 18:54:58 | [diff] [blame] | 31 | #include "ui/aura/remote_window_tree_host_win.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 32 | #include "ui/aura/window.h" |
[email protected] | fcc51c95 | 2014-02-21 21:31:26 | [diff] [blame] | 33 | #include "ui/aura/window_event_dispatcher.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 34 | #endif |
| 35 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 36 | using base::Time; |
| 37 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 38 | namespace { |
| 39 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 40 | HWND GetRootWindow(gfx::NativeView view) { |
| 41 | HWND window = NULL; |
| 42 | #if defined(USE_AURA) |
| 43 | if (view) |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame^] | 44 | window = view->GetHost()->GetAcceleratedWidget(); |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 45 | #else |
| 46 | if (view && IsWindow(view)) { |
| 47 | window = GetAncestor(view, GA_ROOTOWNER); |
| 48 | } |
| 49 | #endif |
| 50 | if (!window) { |
| 51 | // TODO(maruel): bug 1214347 Get the right browser window instead. |
| 52 | return GetDesktopWindow(); |
| 53 | } |
| 54 | return window; |
| 55 | } |
| 56 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 57 | } // anonymous namespace |
| 58 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 59 | namespace printing { |
| 60 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 61 | class PrintingContextWin::CallbackHandler : public IPrintDialogCallback, |
| 62 | public IObjectWithSite { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 63 | public: |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 64 | CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 65 | : owner_(owner), |
| 66 | owner_hwnd_(owner_hwnd), |
| 67 | services_(NULL) { |
| 68 | } |
| 69 | |
| 70 | ~CallbackHandler() { |
| 71 | if (services_) |
| 72 | services_->Release(); |
| 73 | } |
| 74 | |
| 75 | IUnknown* ToIUnknown() { |
| 76 | return static_cast<IUnknown*>(static_cast<IPrintDialogCallback*>(this)); |
| 77 | } |
| 78 | |
| 79 | // IUnknown |
| 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); |
| 87 | } else { |
| 88 | return E_NOINTERFACE; |
| 89 | } |
| 90 | return S_OK; |
| 91 | } |
| 92 | |
| 93 | // No real ref counting. |
| 94 | virtual ULONG WINAPI AddRef() { |
| 95 | return 1; |
| 96 | } |
| 97 | virtual ULONG WINAPI Release() { |
| 98 | return 1; |
| 99 | } |
| 100 | |
| 101 | // IPrintDialogCallback methods |
| 102 | virtual HRESULT WINAPI InitDone() { |
| 103 | return S_OK; |
| 104 | } |
| 105 | |
| 106 | virtual HRESULT WINAPI SelectionChange() { |
| 107 | if (services_) { |
| 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. |
| 112 | } |
| 113 | return S_OK; |
| 114 | } |
| 115 | |
| 116 | virtual HRESULT WINAPI HandleMessage(HWND dialog, |
| 117 | UINT message, |
| 118 | WPARAM wparam, |
| 119 | LPARAM lparam, |
| 120 | LRESULT* result) { |
| 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); |
| 129 | } |
| 130 | return S_FALSE; |
| 131 | } |
| 132 | |
| 133 | virtual HRESULT WINAPI SetSite(IUnknown* site) { |
| 134 | if (!site) { |
| 135 | DCHECK(services_); |
| 136 | services_->Release(); |
| 137 | services_ = NULL; |
| 138 | // The dialog box is destroying, PrintJob::Worker don't need the handle |
| 139 | // anymore. |
| 140 | owner_.dialog_box_ = NULL; |
| 141 | } else { |
| 142 | DCHECK(services_ == NULL); |
| 143 | HRESULT hr = site->QueryInterface(IID_IPrintDialogServices, |
| 144 | reinterpret_cast<void**>(&services_)); |
| 145 | DCHECK(SUCCEEDED(hr)); |
| 146 | } |
| 147 | return S_OK; |
| 148 | } |
| 149 | |
| 150 | virtual HRESULT WINAPI GetSite(REFIID riid, void** site) { |
| 151 | return E_NOTIMPL; |
| 152 | } |
| 153 | |
| 154 | private: |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 155 | PrintingContextWin& owner_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | HWND owner_hwnd_; |
| 157 | IPrintDialogServices* services_; |
| 158 | |
[email protected] | 5930cb6 | 2009-12-08 02:04:22 | [diff] [blame] | 159 | DISALLOW_COPY_AND_ASSIGN(CallbackHandler); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | }; |
| 161 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 162 | // static |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 163 | PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 164 | return static_cast<PrintingContext*>(new PrintingContextWin(app_locale)); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 165 | } |
| 166 | |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 167 | PrintingContextWin::PrintingContextWin(const std::string& app_locale) |
| 168 | : PrintingContext(app_locale), |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 169 | context_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | dialog_box_(NULL), |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 171 | print_dialog_func_(&PrintDlgEx) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | } |
| 173 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 174 | PrintingContextWin::~PrintingContextWin() { |
| 175 | ReleaseContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 176 | } |
| 177 | |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 178 | void PrintingContextWin::AskUserForSettings( |
| 179 | gfx::NativeView view, int max_pages, bool has_selection, |
| 180 | const PrintSettingsCallback& callback) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 181 | DCHECK(!in_print_job_); |
[email protected] | bd04f249 | 2012-12-06 00:40:31 | [diff] [blame] | 182 | if (win8::IsSingleWindowMetroMode()) { |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 183 | // The system dialog can not be opened while running in Metro. |
| 184 | // But we can programatically launch the Metro print device charm though. |
| 185 | HMODULE metro_module = base::win::GetMetroModule(); |
| 186 | if (metro_module != NULL) { |
| 187 | typedef void (*MetroShowPrintUI)(); |
| 188 | MetroShowPrintUI metro_show_print_ui = |
| 189 | reinterpret_cast<MetroShowPrintUI>( |
| 190 | ::GetProcAddress(metro_module, "MetroShowPrintUI")); |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 191 | if (metro_show_print_ui) { |
| 192 | // TODO(mad): Remove this once we can send user metrics from the metro |
| 193 | // driver. crbug.com/142330 |
| 194 | UMA_HISTOGRAM_ENUMERATION("Metro.Print", 1, 2); |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 195 | metro_show_print_ui(); |
[email protected] | e9c7c37 | 2012-08-13 16:36:04 | [diff] [blame] | 196 | } |
[email protected] | 06186a3c | 2012-07-12 18:38:30 | [diff] [blame] | 197 | } |
| 198 | return callback.Run(CANCEL); |
| 199 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 200 | dialog_box_dismissed_ = false; |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 201 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 202 | HWND window = GetRootWindow(view); |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 203 | DCHECK(window); |
| 204 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 205 | // Show the OS-dependent dialog box. |
| 206 | // If the user press |
| 207 | // - OK, the settings are reset and reinitialized with the new settings. OK is |
| 208 | // returned. |
| 209 | // - Apply then Cancel, the settings are reset and reinitialized with the new |
| 210 | // settings. CANCEL is returned. |
| 211 | // - Cancel, the settings are not changed, the previous setting, if it was |
| 212 | // initialized before, are kept. CANCEL is returned. |
| 213 | // On failure, the settings are reset and FAILED is returned. |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 214 | PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) }; |
| 215 | dialog_options.hwndOwner = window; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 216 | // Disable options we don't support currently. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 217 | // TODO(maruel): Reuse the previously loaded settings! |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 218 | dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | |
| 219 | PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 220 | if (!has_selection) |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 221 | dialog_options.Flags |= PD_NOSELECTION; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 222 | |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 223 | PRINTPAGERANGE ranges[32]; |
| 224 | dialog_options.nStartPage = START_PAGE_GENERAL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 225 | if (max_pages) { |
| 226 | // Default initialize to print all the pages. |
| 227 | memset(ranges, 0, sizeof(ranges)); |
| 228 | ranges[0].nFromPage = 1; |
| 229 | ranges[0].nToPage = max_pages; |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 230 | dialog_options.nPageRanges = 1; |
| 231 | dialog_options.nMaxPageRanges = arraysize(ranges); |
| 232 | dialog_options.nMinPage = 1; |
| 233 | dialog_options.nMaxPage = max_pages; |
| 234 | dialog_options.lpPageRanges = ranges; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | } else { |
| 236 | // No need to bother, we don't know how many pages are available. |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 237 | dialog_options.Flags |= PD_NOPAGENUMS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 238 | } |
| 239 | |
[email protected] | 368d362d0 | 2014-02-14 18:36:17 | [diff] [blame] | 240 | // Note that this cannot use ui::BaseShellDialog as the print dialog is |
| 241 | // system modal: opening it from a background thread can cause Windows to |
| 242 | // get the wrong Z-order which will make the print dialog appear behind the |
| 243 | // browser frame (but still being modal) so neither the browser frame nor |
| 244 | // the print dialog will get any input. See http://crbug.com/342697 |
| 245 | // http://crbug.com/180997 for details. |
| 246 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 247 | base::MessageLoop::current()); |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 248 | HRESULT hr = (*print_dialog_func_)(&dialog_options); |
| 249 | if (hr != S_OK) { |
| 250 | ResetSettings(); |
| 251 | callback.Run(FAILED); |
| 252 | } |
| 253 | |
| 254 | // TODO(maruel): Support PD_PRINTTOFILE. |
| 255 | callback.Run(ParseDialogResultEx(dialog_options)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 256 | } |
| 257 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 258 | PrintingContext::Result PrintingContextWin::UseDefaultSettings() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 259 | DCHECK(!in_print_job_); |
| 260 | |
| 261 | PRINTDLG dialog_options = { sizeof(PRINTDLG) }; |
| 262 | dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 263 | if (PrintDlg(&dialog_options)) |
| 264 | return ParseDialogResult(dialog_options); |
| 265 | |
| 266 | // No default printer configured, do we have any printers at all? |
| 267 | DWORD bytes_needed = 0; |
| 268 | DWORD count_returned = 0; |
| 269 | (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 270 | NULL, 2, NULL, 0, &bytes_needed, &count_returned); |
| 271 | if (bytes_needed) { |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 272 | DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2)); |
[email protected] | fb3a97c | 2013-04-11 04:48:01 | [diff] [blame] | 273 | scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 274 | BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 275 | NULL, 2, printer_info_buffer.get(), |
| 276 | bytes_needed, &bytes_needed, |
| 277 | &count_returned); |
| 278 | if (ret && count_returned) { // have printers |
| 279 | // Open the first successfully found printer. |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 280 | const PRINTER_INFO_2* info_2 = |
| 281 | reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
| 282 | const PRINTER_INFO_2* info_2_end = info_2 + count_returned; |
| 283 | for (; info_2 < info_2_end; ++info_2) { |
| 284 | ScopedPrinterHandle printer; |
| 285 | if (!printer.OpenPrinter(info_2->pPrinterName)) |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 286 | continue; |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 287 | scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL); |
| 288 | if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(), |
| 289 | &context_)) { |
| 290 | continue; |
| 291 | } |
| 292 | if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0, |
| 293 | false)) { |
| 294 | return OK; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 295 | } |
[email protected] | a5e0ef57 | 2011-04-21 18:38:54 | [diff] [blame] | 296 | ReleaseContext(); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 297 | } |
| 298 | if (context_) |
| 299 | return OK; |
| 300 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 301 | } |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 302 | |
| 303 | ResetSettings(); |
| 304 | return FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 305 | } |
| 306 | |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 307 | gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() { |
| 308 | // Default fallback to Letter size. |
| 309 | gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch); |
| 310 | |
| 311 | // Get settings from locale. Paper type buffer length is at most 4. |
| 312 | const int paper_type_buffer_len = 4; |
| 313 | wchar_t paper_type_buffer[paper_type_buffer_len] = {0}; |
| 314 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer, |
| 315 | paper_type_buffer_len); |
| 316 | if (wcslen(paper_type_buffer)) { // The call succeeded. |
| 317 | int paper_code = _wtoi(paper_type_buffer); |
| 318 | switch (paper_code) { |
| 319 | case DMPAPER_LEGAL: |
| 320 | paper_size.SetSize(kLegalWidthInch, kLegalHeightInch); |
| 321 | break; |
| 322 | case DMPAPER_A4: |
| 323 | paper_size.SetSize(kA4WidthInch, kA4HeightInch); |
| 324 | break; |
| 325 | case DMPAPER_A3: |
| 326 | paper_size.SetSize(kA3WidthInch, kA3HeightInch); |
| 327 | break; |
| 328 | default: // DMPAPER_LETTER is used for default fallback. |
| 329 | break; |
| 330 | } |
| 331 | } |
| 332 | return gfx::Size( |
| 333 | paper_size.width() * settings_.device_units_per_inch(), |
| 334 | paper_size.height() * settings_.device_units_per_inch()); |
| 335 | } |
| 336 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 337 | PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 338 | bool external_preview) { |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 339 | DCHECK(!in_print_job_); |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 340 | DCHECK(!external_preview) << "Not implemented"; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 341 | |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 342 | ScopedPrinterHandle printer; |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 343 | if (!printer.OpenPrinter(settings_.device_name().c_str())) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 344 | return OnError(); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 345 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 346 | // Make printer changes local to Chrome. |
| 347 | // See MSDN documentation regarding DocumentProperties. |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 348 | scoped_ptr<DEVMODE[]> scoped_dev_mode = |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 349 | CreateDevModeWithColor(printer, settings_.device_name(), |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 350 | settings_.color() != GRAY); |
| 351 | if (!scoped_dev_mode) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 352 | return OnError(); |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 353 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 354 | { |
| 355 | DEVMODE* dev_mode = scoped_dev_mode.get(); |
| 356 | dev_mode->dmCopies = std::max(settings_.copies(), 1); |
| 357 | if (dev_mode->dmCopies > 1) { // do not change unless multiple copies |
| 358 | dev_mode->dmFields |= DM_COPIES; |
| 359 | dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : |
| 360 | DMCOLLATE_FALSE; |
| 361 | } |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 362 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 363 | switch (settings_.duplex_mode()) { |
| 364 | case LONG_EDGE: |
| 365 | dev_mode->dmFields |= DM_DUPLEX; |
| 366 | dev_mode->dmDuplex = DMDUP_VERTICAL; |
| 367 | break; |
| 368 | case SHORT_EDGE: |
| 369 | dev_mode->dmFields |= DM_DUPLEX; |
| 370 | dev_mode->dmDuplex = DMDUP_HORIZONTAL; |
| 371 | break; |
| 372 | case SIMPLEX: |
| 373 | dev_mode->dmFields |= DM_DUPLEX; |
| 374 | dev_mode->dmDuplex = DMDUP_SIMPLEX; |
| 375 | break; |
| 376 | default: // UNKNOWN_DUPLEX_MODE |
| 377 | break; |
| 378 | } |
| 379 | |
| 380 | dev_mode->dmFields |= DM_ORIENTATION; |
| 381 | dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE : |
| 382 | DMORIENT_PORTRAIT; |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 383 | } |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 384 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 385 | // Update data using DocumentProperties. |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 386 | scoped_dev_mode = CreateDevMode(printer, scoped_dev_mode.get()); |
| 387 | if (!scoped_dev_mode) |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 388 | return OnError(); |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 389 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 390 | // Set printer then refresh printer settings. |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 391 | if (!AllocateContext(settings_.device_name(), scoped_dev_mode.get(), |
| 392 | &context_)) { |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 393 | return OnError(); |
| 394 | } |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 395 | PrintSettingsInitializerWin::InitPrintSettings(context_, |
| 396 | *scoped_dev_mode.get(), |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 397 | &settings_); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 398 | return OK; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 399 | } |
| 400 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 401 | PrintingContext::Result PrintingContextWin::InitWithSettings( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 402 | const PrintSettings& settings) { |
| 403 | DCHECK(!in_print_job_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 404 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 405 | settings_ = settings; |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 406 | |
| 407 | // TODO(maruel): settings_.ToDEVMODE() |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 408 | ScopedPrinterHandle printer; |
[email protected] | 37d63884 | 2014-02-21 21:54:32 | [diff] [blame] | 409 | if (!printer.OpenPrinter(settings_.device_name().c_str())) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 410 | return FAILED; |
[email protected] | 263c351 | 2013-06-11 19:11:40 | [diff] [blame] | 411 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 412 | |
| 413 | Result status = OK; |
| 414 | |
| 415 | if (!GetPrinterSettings(printer, settings_.device_name())) |
| 416 | status = FAILED; |
| 417 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 418 | if (status != OK) |
| 419 | ResetSettings(); |
| 420 | return status; |
| 421 | } |
| 422 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 423 | PrintingContext::Result PrintingContextWin::NewDocument( |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 424 | const base::string16& document_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 425 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 426 | if (!context_) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 427 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 428 | |
| 429 | // Set the flag used by the AbortPrintJob dialog procedure. |
| 430 | abort_printing_ = false; |
| 431 | |
| 432 | in_print_job_ = true; |
| 433 | |
| 434 | // Register the application's AbortProc function with GDI. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 435 | if (SP_ERROR == SetAbortProc(context_, &AbortProc)) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 436 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 438 | DCHECK(SimplifyDocumentTitle(document_name) == document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 439 | DOCINFO di = { sizeof(DOCINFO) }; |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 440 | const std::wstring& document_name_wide = base::UTF16ToWide(document_name); |
[email protected] | ea6f72a97c | 2010-09-28 00:34:47 | [diff] [blame] | 441 | di.lpszDocName = document_name_wide.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 442 | |
| 443 | // Is there a debug dump directory specified? If so, force to print to a file. |
[email protected] | 79f6388 | 2013-02-10 05:15:45 | [diff] [blame] | 444 | base::FilePath debug_dump_path = PrintedDocument::debug_dump_path(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | if (!debug_dump_path.empty()) { |
| 446 | // Create a filename. |
| 447 | std::wstring filename; |
| 448 | Time now(Time::Now()); |
[email protected] | 5cca3a5 | 2008-08-19 22:35:29 | [diff] [blame] | 449 | filename = base::TimeFormatShortDateNumeric(now); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | filename += L"_"; |
[email protected] | 5cca3a5 | 2008-08-19 22:35:29 | [diff] [blame] | 451 | filename += base::TimeFormatTimeOfDay(now); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 452 | filename += L"_"; |
[email protected] | 6c3bf03 | 2013-12-25 19:37:03 | [diff] [blame] | 453 | filename += base::UTF16ToWide(document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 454 | filename += L"_"; |
| 455 | filename += L"buffer.prn"; |
[email protected] | de294335 | 2009-10-22 23:06:12 | [diff] [blame] | 456 | file_util::ReplaceIllegalCharactersInPath(&filename, '_'); |
[email protected] | d9d4299 | 2010-09-13 19:39:19 | [diff] [blame] | 457 | debug_dump_path.Append(filename); |
| 458 | di.lpszOutput = debug_dump_path.value().c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 459 | } |
| 460 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 461 | // No message loop running in unit tests. |
[email protected] | f58ef2b | 2013-05-06 22:43:57 | [diff] [blame] | 462 | DCHECK(!base::MessageLoop::current() || |
| 463 | !base::MessageLoop::current()->NestableTasksAllowed()); |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 464 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 465 | // Begin a print job by calling the StartDoc function. |
| 466 | // NOTE: StartDoc() starts a message loop. That causes a lot of problems with |
| 467 | // IPC. Make sure recursive task processing is disabled. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 468 | if (StartDoc(context_, &di) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 469 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 471 | return OK; |
| 472 | } |
| 473 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 474 | PrintingContext::Result PrintingContextWin::NewPage() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | if (abort_printing_) |
| 476 | return CANCEL; |
[email protected] | 3b52c98 | 2010-09-27 20:40:36 | [diff] [blame] | 477 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 478 | DCHECK(in_print_job_); |
| 479 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 480 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 481 | // ::StartPage(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 482 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | return OK; |
| 484 | } |
| 485 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 486 | PrintingContext::Result PrintingContextWin::PageDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | if (abort_printing_) |
| 488 | return CANCEL; |
| 489 | DCHECK(in_print_job_); |
| 490 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 491 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 492 | // ::EndPage(). |
| 493 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | return OK; |
| 495 | } |
| 496 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 497 | PrintingContext::Result PrintingContextWin::DocumentDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 498 | if (abort_printing_) |
| 499 | return CANCEL; |
| 500 | DCHECK(in_print_job_); |
[email protected] | 6074541 | 2010-09-27 23:46:07 | [diff] [blame] | 501 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | |
| 503 | // Inform the driver that document has ended. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 504 | if (EndDoc(context_) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 505 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | |
| 507 | ResetSettings(); |
| 508 | return OK; |
| 509 | } |
| 510 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 511 | void PrintingContextWin::Cancel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 512 | abort_printing_ = true; |
| 513 | in_print_job_ = false; |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 514 | if (context_) |
| 515 | CancelDC(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | if (dialog_box_) { |
| 517 | DestroyWindow(dialog_box_); |
| 518 | dialog_box_dismissed_ = true; |
| 519 | } |
| 520 | } |
| 521 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 522 | void PrintingContextWin::ReleaseContext() { |
| 523 | if (context_) { |
| 524 | DeleteDC(context_); |
| 525 | context_ = NULL; |
| 526 | } |
| 527 | } |
| 528 | |
| 529 | gfx::NativeDrawingContext PrintingContextWin::context() const { |
| 530 | return context_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | // static |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 534 | BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 535 | if (nCode) { |
| 536 | // TODO(maruel): Need a way to find the right instance to set. Should |
| 537 | // leverage PrintJobManager here? |
| 538 | // abort_printing_ = true; |
| 539 | } |
| 540 | return true; |
| 541 | } |
| 542 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 543 | bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode, |
| 544 | const std::wstring& new_device_name, |
| 545 | const PRINTPAGERANGE* ranges, |
| 546 | int number_ranges, |
| 547 | bool selection_only) { |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 548 | skia::InitializeDC(context_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 549 | DCHECK(GetDeviceCaps(context_, CLIPCAPS)); |
| 550 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB); |
| 551 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 552 | // Some printers don't advertise these. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 553 | // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING); |
| 554 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA); |
| 555 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 556 | |
| 557 | // StretchDIBits() support is needed for printing. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 558 | if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) || |
| 559 | !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 560 | NOTREACHED(); |
| 561 | ResetSettings(); |
| 562 | return false; |
| 563 | } |
| 564 | |
| 565 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 566 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 567 | PageRanges ranges_vector; |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 568 | if (!selection_only) { |
| 569 | // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector. |
| 570 | ranges_vector.reserve(number_ranges); |
| 571 | for (int i = 0; i < number_ranges; ++i) { |
| 572 | PageRange range; |
| 573 | // Transfer from 1-based to 0-based. |
| 574 | range.from = ranges[i].nFromPage - 1; |
| 575 | range.to = ranges[i].nToPage - 1; |
| 576 | ranges_vector.push_back(range); |
| 577 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 578 | } |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 579 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 580 | settings_.set_ranges(ranges_vector); |
| 581 | settings_.set_device_name(new_device_name); |
| 582 | settings_.set_selection_only(selection_only); |
| 583 | PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode, |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 584 | &settings_); |
| 585 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 589 | bool PrintingContextWin::GetPrinterSettings(HANDLE printer, |
| 590 | const std::wstring& device_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 591 | DCHECK(!in_print_job_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 592 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 593 | scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL); |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 594 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 595 | if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) { |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 596 | ResetSettings(); |
| 597 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | } |
| 599 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 600 | return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 601 | } |
| 602 | |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 603 | // static |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 604 | bool PrintingContextWin::AllocateContext(const std::wstring& device_name, |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 605 | const DEVMODE* dev_mode, |
| 606 | gfx::NativeDrawingContext* context) { |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 607 | *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 608 | DCHECK(*context); |
| 609 | return *context != NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 610 | } |
| 611 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 612 | PrintingContext::Result PrintingContextWin::ParseDialogResultEx( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 613 | const PRINTDLGEX& dialog_options) { |
| 614 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 615 | if (dialog_options.dwResultAction != PD_RESULT_CANCEL) { |
| 616 | // Start fresh. |
| 617 | ResetSettings(); |
| 618 | |
| 619 | DEVMODE* dev_mode = NULL; |
| 620 | if (dialog_options.hDevMode) { |
| 621 | dev_mode = |
| 622 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 623 | DCHECK(dev_mode); |
| 624 | } |
| 625 | |
| 626 | std::wstring device_name; |
| 627 | if (dialog_options.hDevNames) { |
| 628 | DEVNAMES* dev_names = |
| 629 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 630 | DCHECK(dev_names); |
| 631 | if (dev_names) { |
[email protected] | e5dc0f5 | 2013-11-02 13:49:28 | [diff] [blame] | 632 | device_name = reinterpret_cast<const wchar_t*>(dev_names) + |
| 633 | dev_names->wDeviceOffset; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 634 | GlobalUnlock(dialog_options.hDevNames); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | bool success = false; |
| 639 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 640 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 641 | PRINTPAGERANGE* page_ranges = NULL; |
| 642 | DWORD num_page_ranges = 0; |
| 643 | bool print_selection_only = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 644 | if (dialog_options.Flags & PD_PAGENUMS) { |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 645 | page_ranges = dialog_options.lpPageRanges; |
| 646 | num_page_ranges = dialog_options.nPageRanges; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 647 | } |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 648 | if (dialog_options.Flags & PD_SELECTION) { |
| 649 | print_selection_only = true; |
| 650 | } |
| 651 | success = InitializeSettings(*dev_mode, |
| 652 | device_name, |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 653 | page_ranges, |
| 654 | num_page_ranges, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 655 | print_selection_only); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 656 | } |
| 657 | |
| 658 | if (!success && dialog_options.hDC) { |
| 659 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 660 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 661 | } |
| 662 | |
| 663 | if (dev_mode) { |
| 664 | GlobalUnlock(dialog_options.hDevMode); |
| 665 | } |
| 666 | } else { |
| 667 | if (dialog_options.hDC) { |
| 668 | DeleteDC(dialog_options.hDC); |
| 669 | } |
| 670 | } |
| 671 | |
| 672 | if (dialog_options.hDevMode != NULL) |
| 673 | GlobalFree(dialog_options.hDevMode); |
| 674 | if (dialog_options.hDevNames != NULL) |
| 675 | GlobalFree(dialog_options.hDevNames); |
| 676 | |
| 677 | switch (dialog_options.dwResultAction) { |
| 678 | case PD_RESULT_PRINT: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 679 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 680 | case PD_RESULT_APPLY: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 681 | return context_ ? CANCEL : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 682 | case PD_RESULT_CANCEL: |
| 683 | return CANCEL; |
| 684 | default: |
| 685 | return FAILED; |
| 686 | } |
| 687 | } |
| 688 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 689 | PrintingContext::Result PrintingContextWin::ParseDialogResult( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 690 | const PRINTDLG& dialog_options) { |
| 691 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 692 | // Start fresh. |
| 693 | ResetSettings(); |
| 694 | |
| 695 | DEVMODE* dev_mode = NULL; |
| 696 | if (dialog_options.hDevMode) { |
| 697 | dev_mode = |
| 698 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 699 | DCHECK(dev_mode); |
| 700 | } |
| 701 | |
| 702 | std::wstring device_name; |
| 703 | if (dialog_options.hDevNames) { |
| 704 | DEVNAMES* dev_names = |
| 705 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 706 | DCHECK(dev_names); |
| 707 | if (dev_names) { |
| 708 | device_name = |
| 709 | reinterpret_cast<const wchar_t*>( |
| 710 | reinterpret_cast<const wchar_t*>(dev_names) + |
| 711 | dev_names->wDeviceOffset); |
| 712 | GlobalUnlock(dialog_options.hDevNames); |
| 713 | } |
| 714 | } |
| 715 | |
| 716 | bool success = false; |
| 717 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 718 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 719 | success = InitializeSettings(*dev_mode, device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | if (!success && dialog_options.hDC) { |
| 723 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 724 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 725 | } |
| 726 | |
| 727 | if (dev_mode) { |
| 728 | GlobalUnlock(dialog_options.hDevMode); |
| 729 | } |
| 730 | |
| 731 | if (dialog_options.hDevMode != NULL) |
| 732 | GlobalFree(dialog_options.hDevMode); |
| 733 | if (dialog_options.hDevNames != NULL) |
| 734 | GlobalFree(dialog_options.hDevNames); |
| 735 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 736 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 737 | } |
| 738 | |
| 739 | } // namespace printing |