[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 | |
[email protected] | 19f09a6 | 2011-05-04 23:37:47 | [diff] [blame] | 7 | #include <algorithm> |
| 8 | |
[email protected] | ad3b0501 | 2014-06-16 23:02:15 | [diff] [blame] | 9 | #include "base/strings/string_number_conversions.h" |
[email protected] | 90626587 | 2013-06-07 22:40:45 | [diff] [blame] | 10 | #include "base/strings/utf_string_conversions.h" |
[email protected] | 09083eb | 2012-09-20 05:58:43 | [diff] [blame] | 11 | #include "printing/backend/print_backend.h" |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 12 | #include "printing/backend/win_helper.h" |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 13 | #include "printing/print_settings_initializer_win.h" |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 14 | #include "printing/printed_document.h" |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 15 | #include "printing/printing_utils.h" |
[email protected] | e5636a5 | 2011-09-28 21:44:42 | [diff] [blame] | 16 | #include "printing/units.h" |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 17 | #include "skia/ext/platform_device.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 18 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 19 | #if defined(USE_AURA) |
[email protected] | e8cf733 | 2014-02-14 18:54:58 | [diff] [blame] | 20 | #include "ui/aura/remote_window_tree_host_win.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 21 | #include "ui/aura/window.h" |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 22 | #endif |
| 23 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 24 | namespace { |
| 25 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 26 | HWND GetRootWindow(gfx::NativeView view) { |
| 27 | HWND window = NULL; |
| 28 | #if defined(USE_AURA) |
| 29 | if (view) |
[email protected] | 2374d181 | 2014-03-04 03:42:27 | [diff] [blame] | 30 | window = view->GetHost()->GetAcceleratedWidget(); |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 31 | #else |
| 32 | if (view && IsWindow(view)) { |
| 33 | window = GetAncestor(view, GA_ROOTOWNER); |
| 34 | } |
| 35 | #endif |
| 36 | if (!window) { |
| 37 | // TODO(maruel): bug 1214347 Get the right browser window instead. |
| 38 | return GetDesktopWindow(); |
| 39 | } |
| 40 | return window; |
| 41 | } |
| 42 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 43 | } // anonymous namespace |
| 44 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 45 | namespace printing { |
| 46 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 47 | // static |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 48 | PrintingContext* PrintingContext::Create(const std::string& app_locale) { |
| 49 | return static_cast<PrintingContext*>(new PrintingContextWin(app_locale)); |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 50 | } |
| 51 | |
[email protected] | ee5f36e4 | 2010-12-03 22:40:37 | [diff] [blame] | 52 | PrintingContextWin::PrintingContextWin(const std::string& app_locale) |
[email protected] | 97a8063 | 2014-03-27 17:50:39 | [diff] [blame] | 53 | : PrintingContext(app_locale), context_(NULL), dialog_box_(NULL) {} |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 54 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 55 | PrintingContextWin::~PrintingContextWin() { |
| 56 | ReleaseContext(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 57 | } |
| 58 | |
[email protected] | abe4811 | 2011-11-19 01:58:38 | [diff] [blame] | 59 | void PrintingContextWin::AskUserForSettings( |
| 60 | gfx::NativeView view, int max_pages, bool has_selection, |
| 61 | const PrintSettingsCallback& callback) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 62 | DCHECK(!in_print_job_); |
| 63 | dialog_box_dismissed_ = false; |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 64 | |
[email protected] | 6b7bfd2 | 2013-05-28 23:55:15 | [diff] [blame] | 65 | HWND window = GetRootWindow(view); |
[email protected] | fc790462 | 2010-05-12 19:26:40 | [diff] [blame] | 66 | DCHECK(window); |
| 67 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 68 | // Show the OS-dependent dialog box. |
| 69 | // If the user press |
| 70 | // - OK, the settings are reset and reinitialized with the new settings. OK is |
| 71 | // returned. |
| 72 | // - Apply then Cancel, the settings are reset and reinitialized with the new |
| 73 | // settings. CANCEL is returned. |
| 74 | // - Cancel, the settings are not changed, the previous setting, if it was |
| 75 | // initialized before, are kept. CANCEL is returned. |
| 76 | // On failure, the settings are reset and FAILED is returned. |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 77 | PRINTDLGEX dialog_options = { sizeof(PRINTDLGEX) }; |
| 78 | dialog_options.hwndOwner = window; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 79 | // Disable options we don't support currently. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 80 | // TODO(maruel): Reuse the previously loaded settings! |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 81 | dialog_options.Flags = PD_RETURNDC | PD_USEDEVMODECOPIESANDCOLLATE | |
| 82 | PD_NOCURRENTPAGE | PD_HIDEPRINTTOFILE; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 83 | if (!has_selection) |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 84 | dialog_options.Flags |= PD_NOSELECTION; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 85 | |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 86 | PRINTPAGERANGE ranges[32]; |
| 87 | dialog_options.nStartPage = START_PAGE_GENERAL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 88 | if (max_pages) { |
| 89 | // Default initialize to print all the pages. |
| 90 | memset(ranges, 0, sizeof(ranges)); |
| 91 | ranges[0].nFromPage = 1; |
| 92 | ranges[0].nToPage = max_pages; |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 93 | dialog_options.nPageRanges = 1; |
| 94 | dialog_options.nMaxPageRanges = arraysize(ranges); |
| 95 | dialog_options.nMinPage = 1; |
| 96 | dialog_options.nMaxPage = max_pages; |
| 97 | dialog_options.lpPageRanges = ranges; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 98 | } else { |
| 99 | // No need to bother, we don't know how many pages are available. |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 100 | dialog_options.Flags |= PD_NOPAGENUMS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | } |
| 102 | |
[email protected] | 97a8063 | 2014-03-27 17:50:39 | [diff] [blame] | 103 | if (ShowPrintDialog(&dialog_options) != S_OK) { |
[email protected] | f487ac2 | 2014-02-13 17:04:31 | [diff] [blame] | 104 | ResetSettings(); |
| 105 | callback.Run(FAILED); |
| 106 | } |
| 107 | |
| 108 | // TODO(maruel): Support PD_PRINTTOFILE. |
| 109 | callback.Run(ParseDialogResultEx(dialog_options)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 110 | } |
| 111 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 112 | PrintingContext::Result PrintingContextWin::UseDefaultSettings() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 113 | DCHECK(!in_print_job_); |
| 114 | |
| 115 | PRINTDLG dialog_options = { sizeof(PRINTDLG) }; |
| 116 | dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 117 | if (PrintDlg(&dialog_options)) |
| 118 | return ParseDialogResult(dialog_options); |
| 119 | |
| 120 | // No default printer configured, do we have any printers at all? |
| 121 | DWORD bytes_needed = 0; |
| 122 | DWORD count_returned = 0; |
| 123 | (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 124 | NULL, 2, NULL, 0, &bytes_needed, &count_returned); |
| 125 | if (bytes_needed) { |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 126 | DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2)); |
[email protected] | fb3a97c | 2013-04-11 04:48:01 | [diff] [blame] | 127 | scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 128 | BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
| 129 | NULL, 2, printer_info_buffer.get(), |
| 130 | bytes_needed, &bytes_needed, |
| 131 | &count_returned); |
| 132 | if (ret && count_returned) { // have printers |
| 133 | // Open the first successfully found printer. |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 134 | const PRINTER_INFO_2* info_2 = |
| 135 | reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
| 136 | const PRINTER_INFO_2* info_2_end = info_2 + count_returned; |
| 137 | for (; info_2 < info_2_end; ++info_2) { |
| 138 | ScopedPrinterHandle printer; |
| 139 | if (!printer.OpenPrinter(info_2->pPrinterName)) |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 140 | continue; |
[email protected] | 2b364be | 2014-03-11 00:49:49 | [diff] [blame] | 141 | scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
| 142 | CreateDevMode(printer, NULL); |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 143 | if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(), |
| 144 | &context_)) { |
| 145 | continue; |
| 146 | } |
| 147 | if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0, |
| 148 | false)) { |
| 149 | return OK; |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 150 | } |
[email protected] | a5e0ef57 | 2011-04-21 18:38:54 | [diff] [blame] | 151 | ReleaseContext(); |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 152 | } |
| 153 | if (context_) |
| 154 | return OK; |
| 155 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 156 | } |
[email protected] | ff3ccc2 | 2011-04-18 21:35:48 | [diff] [blame] | 157 | |
| 158 | ResetSettings(); |
| 159 | return FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 160 | } |
| 161 | |
[email protected] | 4c9054b | 2013-11-04 18:34:29 | [diff] [blame] | 162 | gfx::Size PrintingContextWin::GetPdfPaperSizeDeviceUnits() { |
| 163 | // Default fallback to Letter size. |
| 164 | gfx::SizeF paper_size(kLetterWidthInch, kLetterHeightInch); |
| 165 | |
| 166 | // Get settings from locale. Paper type buffer length is at most 4. |
| 167 | const int paper_type_buffer_len = 4; |
| 168 | wchar_t paper_type_buffer[paper_type_buffer_len] = {0}; |
| 169 | GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IPAPERSIZE, paper_type_buffer, |
| 170 | paper_type_buffer_len); |
| 171 | if (wcslen(paper_type_buffer)) { // The call succeeded. |
| 172 | int paper_code = _wtoi(paper_type_buffer); |
| 173 | switch (paper_code) { |
| 174 | case DMPAPER_LEGAL: |
| 175 | paper_size.SetSize(kLegalWidthInch, kLegalHeightInch); |
| 176 | break; |
| 177 | case DMPAPER_A4: |
| 178 | paper_size.SetSize(kA4WidthInch, kA4HeightInch); |
| 179 | break; |
| 180 | case DMPAPER_A3: |
| 181 | paper_size.SetSize(kA3WidthInch, kA3HeightInch); |
| 182 | break; |
| 183 | default: // DMPAPER_LETTER is used for default fallback. |
| 184 | break; |
| 185 | } |
| 186 | } |
| 187 | return gfx::Size( |
| 188 | paper_size.width() * settings_.device_units_per_inch(), |
| 189 | paper_size.height() * settings_.device_units_per_inch()); |
| 190 | } |
| 191 | |
[email protected] | 55b23a0 | 2011-08-17 23:09:36 | [diff] [blame] | 192 | PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 193 | bool external_preview) { |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 194 | DCHECK(!in_print_job_); |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 195 | DCHECK(!external_preview) << "Not implemented"; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 196 | |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 197 | ScopedPrinterHandle printer; |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 198 | if (!printer.OpenPrinter(settings_.device_name().c_str())) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 199 | return OnError(); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 200 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 201 | // Make printer changes local to Chrome. |
| 202 | // See MSDN documentation regarding DocumentProperties. |
[email protected] | 2b364be | 2014-03-11 00:49:49 | [diff] [blame] | 203 | scoped_ptr<DEVMODE, base::FreeDeleter> scoped_dev_mode = |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 204 | CreateDevModeWithColor(printer, settings_.device_name(), |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 205 | settings_.color() != GRAY); |
| 206 | if (!scoped_dev_mode) |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 207 | return OnError(); |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 208 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 209 | { |
| 210 | DEVMODE* dev_mode = scoped_dev_mode.get(); |
| 211 | dev_mode->dmCopies = std::max(settings_.copies(), 1); |
[email protected] | 299e3d00 | 2014-06-03 08:46:49 | [diff] [blame] | 212 | if (dev_mode->dmCopies > 1) { // do not change unless multiple copies |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 213 | dev_mode->dmFields |= DM_COPIES; |
| 214 | dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : |
| 215 | DMCOLLATE_FALSE; |
| 216 | } |
[email protected] | f3256b0d8 | 2011-09-04 23:36:29 | [diff] [blame] | 217 | |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 218 | switch (settings_.duplex_mode()) { |
| 219 | case LONG_EDGE: |
| 220 | dev_mode->dmFields |= DM_DUPLEX; |
| 221 | dev_mode->dmDuplex = DMDUP_VERTICAL; |
| 222 | break; |
| 223 | case SHORT_EDGE: |
| 224 | dev_mode->dmFields |= DM_DUPLEX; |
| 225 | dev_mode->dmDuplex = DMDUP_HORIZONTAL; |
| 226 | break; |
| 227 | case SIMPLEX: |
| 228 | dev_mode->dmFields |= DM_DUPLEX; |
| 229 | dev_mode->dmDuplex = DMDUP_SIMPLEX; |
| 230 | break; |
| 231 | default: // UNKNOWN_DUPLEX_MODE |
| 232 | break; |
| 233 | } |
| 234 | |
| 235 | dev_mode->dmFields |= DM_ORIENTATION; |
| 236 | dev_mode->dmOrientation = settings_.landscape() ? DMORIENT_LANDSCAPE : |
| 237 | DMORIENT_PORTRAIT; |
[email protected] | ad3b0501 | 2014-06-16 23:02:15 | [diff] [blame] | 238 | |
| 239 | const PrintSettings::RequestedMedia& requested_media = |
| 240 | settings_.requested_media(); |
| 241 | static const int kFromUm = 100; // Windows uses 0.1mm. |
| 242 | int width = requested_media.size_microns.width() / kFromUm; |
| 243 | int height = requested_media.size_microns.height() / kFromUm; |
| 244 | unsigned id = 0; |
| 245 | if (base::StringToUint(requested_media.vendor_id, &id) && id) { |
| 246 | dev_mode->dmFields |= DM_PAPERSIZE; |
| 247 | dev_mode->dmPaperSize = static_cast<short>(id); |
| 248 | } else if (width > 0 && height > 0) { |
| 249 | dev_mode->dmFields |= DM_PAPERWIDTH; |
| 250 | dev_mode->dmPaperWidth = width; |
| 251 | dev_mode->dmFields |= DM_PAPERLENGTH; |
| 252 | dev_mode->dmPaperLength = height; |
| 253 | } |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 254 | } |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 255 | |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 256 | // Update data using DocumentProperties. |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 257 | scoped_dev_mode = CreateDevMode(printer, scoped_dev_mode.get()); |
| 258 | if (!scoped_dev_mode) |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 259 | return OnError(); |
[email protected] | 30aaa81a | 2011-05-03 21:08:39 | [diff] [blame] | 260 | |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 261 | // Set printer then refresh printer settings. |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 262 | if (!AllocateContext(settings_.device_name(), scoped_dev_mode.get(), |
| 263 | &context_)) { |
[email protected] | bd5baaf | 2011-04-21 20:48:33 | [diff] [blame] | 264 | return OnError(); |
| 265 | } |
[email protected] | b3b6b6f | 2014-02-14 20:19:54 | [diff] [blame] | 266 | PrintSettingsInitializerWin::InitPrintSettings(context_, |
| 267 | *scoped_dev_mode.get(), |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 268 | &settings_); |
[email protected] | afbdbf11 | 2011-03-28 22:09:37 | [diff] [blame] | 269 | return OK; |
[email protected] | 7868ecab | 2011-03-05 00:12:53 | [diff] [blame] | 270 | } |
| 271 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 272 | PrintingContext::Result PrintingContextWin::InitWithSettings( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 273 | const PrintSettings& settings) { |
| 274 | DCHECK(!in_print_job_); |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 275 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 276 | settings_ = settings; |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 277 | |
| 278 | // TODO(maruel): settings_.ToDEVMODE() |
[email protected] | 55e8e9f | 2012-03-02 22:50:54 | [diff] [blame] | 279 | ScopedPrinterHandle printer; |
[email protected] | 37d63884 | 2014-02-21 21:54:32 | [diff] [blame] | 280 | if (!printer.OpenPrinter(settings_.device_name().c_str())) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 281 | return FAILED; |
[email protected] | 263c351 | 2013-06-11 19:11:40 | [diff] [blame] | 282 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 283 | |
| 284 | Result status = OK; |
| 285 | |
| 286 | if (!GetPrinterSettings(printer, settings_.device_name())) |
| 287 | status = FAILED; |
| 288 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 289 | if (status != OK) |
| 290 | ResetSettings(); |
| 291 | return status; |
| 292 | } |
| 293 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 294 | PrintingContext::Result PrintingContextWin::NewDocument( |
[email protected] | b5fa4ee | 2013-10-01 07:19:07 | [diff] [blame] | 295 | const base::string16& document_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 296 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 297 | if (!context_) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 298 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 299 | |
| 300 | // Set the flag used by the AbortPrintJob dialog procedure. |
| 301 | abort_printing_ = false; |
| 302 | |
| 303 | in_print_job_ = true; |
| 304 | |
| 305 | // Register the application's AbortProc function with GDI. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 306 | if (SP_ERROR == SetAbortProc(context_, &AbortProc)) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 307 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 308 | |
[email protected] | ec91bfa | 2013-11-09 17:24:27 | [diff] [blame] | 309 | DCHECK(SimplifyDocumentTitle(document_name) == document_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 310 | DOCINFO di = { sizeof(DOCINFO) }; |
[email protected] | 299e3d00 | 2014-06-03 08:46:49 | [diff] [blame] | 311 | di.lpszDocName = document_name.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 312 | |
| 313 | // Is there a debug dump directory specified? If so, force to print to a file. |
[email protected] | c95198b | 2014-06-12 16:56:55 | [diff] [blame] | 314 | base::string16 debug_dump_path = |
| 315 | PrintedDocument::CreateDebugDumpPath(document_name, |
| 316 | FILE_PATH_LITERAL(".prn")).value(); |
| 317 | if (!debug_dump_path.empty()) |
| 318 | di.lpszOutput = debug_dump_path.c_str(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 319 | |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 320 | // No message loop running in unit tests. |
[email protected] | f58ef2b | 2013-05-06 22:43:57 | [diff] [blame] | 321 | DCHECK(!base::MessageLoop::current() || |
| 322 | !base::MessageLoop::current()->NestableTasksAllowed()); |
[email protected] | daee497 | 2009-07-09 14:28:24 | [diff] [blame] | 323 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 324 | // Begin a print job by calling the StartDoc function. |
| 325 | // NOTE: StartDoc() starts a message loop. That causes a lot of problems with |
| 326 | // IPC. Make sure recursive task processing is disabled. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 327 | if (StartDoc(context_, &di) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 328 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 329 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | return OK; |
| 331 | } |
| 332 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 333 | PrintingContext::Result PrintingContextWin::NewPage() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 334 | if (abort_printing_) |
| 335 | return CANCEL; |
[email protected] | 3b52c98 | 2010-09-27 20:40:36 | [diff] [blame] | 336 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 337 | DCHECK(in_print_job_); |
| 338 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 339 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 340 | // ::StartPage(). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 341 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | return OK; |
| 343 | } |
| 344 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 345 | PrintingContext::Result PrintingContextWin::PageDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 346 | if (abort_printing_) |
| 347 | return CANCEL; |
| 348 | DCHECK(in_print_job_); |
| 349 | |
[email protected] | b10c54d | 2011-03-16 18:22:35 | [diff] [blame] | 350 | // Intentional No-op. NativeMetafile::SafePlayback takes care of calling |
| 351 | // ::EndPage(). |
| 352 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | return OK; |
| 354 | } |
| 355 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 356 | PrintingContext::Result PrintingContextWin::DocumentDone() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 357 | if (abort_printing_) |
| 358 | return CANCEL; |
| 359 | DCHECK(in_print_job_); |
[email protected] | 6074541 | 2010-09-27 23:46:07 | [diff] [blame] | 360 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 361 | |
| 362 | // Inform the driver that document has ended. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 363 | if (EndDoc(context_) <= 0) |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 364 | return OnError(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 365 | |
| 366 | ResetSettings(); |
| 367 | return OK; |
| 368 | } |
| 369 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 370 | void PrintingContextWin::Cancel() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 371 | abort_printing_ = true; |
| 372 | in_print_job_ = false; |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 373 | if (context_) |
| 374 | CancelDC(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 375 | if (dialog_box_) { |
| 376 | DestroyWindow(dialog_box_); |
| 377 | dialog_box_dismissed_ = true; |
| 378 | } |
| 379 | } |
| 380 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 381 | void PrintingContextWin::ReleaseContext() { |
| 382 | if (context_) { |
| 383 | DeleteDC(context_); |
| 384 | context_ = NULL; |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | gfx::NativeDrawingContext PrintingContextWin::context() const { |
| 389 | return context_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 390 | } |
| 391 | |
| 392 | // static |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 393 | BOOL PrintingContextWin::AbortProc(HDC hdc, int nCode) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 394 | if (nCode) { |
| 395 | // TODO(maruel): Need a way to find the right instance to set. Should |
| 396 | // leverage PrintJobManager here? |
| 397 | // abort_printing_ = true; |
| 398 | } |
| 399 | return true; |
| 400 | } |
| 401 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 402 | bool PrintingContextWin::InitializeSettings(const DEVMODE& dev_mode, |
| 403 | const std::wstring& new_device_name, |
| 404 | const PRINTPAGERANGE* ranges, |
| 405 | int number_ranges, |
| 406 | bool selection_only) { |
[email protected] | 62f2e80 | 2011-05-26 14:28:35 | [diff] [blame] | 407 | skia::InitializeDC(context_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 408 | DCHECK(GetDeviceCaps(context_, CLIPCAPS)); |
| 409 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB); |
| 410 | DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 411 | // Some printers don't advertise these. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 412 | // DCHECK(GetDeviceCaps(context_, RASTERCAPS) & RC_SCALING); |
| 413 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_CONST_ALPHA); |
| 414 | // DCHECK(GetDeviceCaps(context_, SHADEBLENDCAPS) & SB_PIXEL_ALPHA); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 415 | |
| 416 | // StretchDIBits() support is needed for printing. |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 417 | if (!(GetDeviceCaps(context_, RASTERCAPS) & RC_STRETCHDIB) || |
| 418 | !(GetDeviceCaps(context_, RASTERCAPS) & RC_BITMAP64)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 419 | NOTREACHED(); |
| 420 | ResetSettings(); |
| 421 | return false; |
| 422 | } |
| 423 | |
| 424 | DCHECK(!in_print_job_); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 425 | DCHECK(context_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | PageRanges ranges_vector; |
[email protected] | 8227045 | 2009-06-19 15:58:01 | [diff] [blame] | 427 | if (!selection_only) { |
| 428 | // Convert the PRINTPAGERANGE array to a PrintSettings::PageRanges vector. |
| 429 | ranges_vector.reserve(number_ranges); |
| 430 | for (int i = 0; i < number_ranges; ++i) { |
| 431 | PageRange range; |
| 432 | // Transfer from 1-based to 0-based. |
| 433 | range.from = ranges[i].nFromPage - 1; |
| 434 | range.to = ranges[i].nToPage - 1; |
| 435 | ranges_vector.push_back(range); |
| 436 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 437 | } |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 438 | |
[email protected] | e5324b5 | 2013-10-29 03:16:37 | [diff] [blame] | 439 | settings_.set_ranges(ranges_vector); |
| 440 | settings_.set_device_name(new_device_name); |
| 441 | settings_.set_selection_only(selection_only); |
| 442 | PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode, |
[email protected] | 4993f34 | 2010-10-26 17:57:52 | [diff] [blame] | 443 | &settings_); |
| 444 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 445 | return true; |
| 446 | } |
| 447 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 448 | bool PrintingContextWin::GetPrinterSettings(HANDLE printer, |
| 449 | const std::wstring& device_name) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 450 | DCHECK(!in_print_job_); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 451 | |
[email protected] | 2b364be | 2014-03-11 00:49:49 | [diff] [blame] | 452 | scoped_ptr<DEVMODE, base::FreeDeleter> dev_mode = |
| 453 | CreateDevMode(printer, NULL); |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 454 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 455 | if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) { |
[email protected] | d8d17eb | 2012-12-12 07:25:41 | [diff] [blame] | 456 | ResetSettings(); |
| 457 | return false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 458 | } |
| 459 | |
[email protected] | 1b24888 | 2014-02-25 04:39:14 | [diff] [blame] | 460 | return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 461 | } |
| 462 | |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 463 | // static |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 464 | bool PrintingContextWin::AllocateContext(const std::wstring& device_name, |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 465 | const DEVMODE* dev_mode, |
| 466 | gfx::NativeDrawingContext* context) { |
[email protected] | 9f47bbf | 2011-05-06 19:03:17 | [diff] [blame] | 467 | *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 468 | DCHECK(*context); |
| 469 | return *context != NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 470 | } |
| 471 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 472 | PrintingContext::Result PrintingContextWin::ParseDialogResultEx( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 473 | const PRINTDLGEX& dialog_options) { |
| 474 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 475 | if (dialog_options.dwResultAction != PD_RESULT_CANCEL) { |
| 476 | // Start fresh. |
| 477 | ResetSettings(); |
| 478 | |
| 479 | DEVMODE* dev_mode = NULL; |
| 480 | if (dialog_options.hDevMode) { |
| 481 | dev_mode = |
| 482 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 483 | DCHECK(dev_mode); |
| 484 | } |
| 485 | |
| 486 | std::wstring device_name; |
| 487 | if (dialog_options.hDevNames) { |
| 488 | DEVNAMES* dev_names = |
| 489 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 490 | DCHECK(dev_names); |
| 491 | if (dev_names) { |
[email protected] | e5dc0f5 | 2013-11-02 13:49:28 | [diff] [blame] | 492 | device_name = reinterpret_cast<const wchar_t*>(dev_names) + |
| 493 | dev_names->wDeviceOffset; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 494 | GlobalUnlock(dialog_options.hDevNames); |
| 495 | } |
| 496 | } |
| 497 | |
| 498 | bool success = false; |
| 499 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 500 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 501 | PRINTPAGERANGE* page_ranges = NULL; |
| 502 | DWORD num_page_ranges = 0; |
| 503 | bool print_selection_only = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 504 | if (dialog_options.Flags & PD_PAGENUMS) { |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 505 | page_ranges = dialog_options.lpPageRanges; |
| 506 | num_page_ranges = dialog_options.nPageRanges; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | } |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 508 | if (dialog_options.Flags & PD_SELECTION) { |
| 509 | print_selection_only = true; |
| 510 | } |
| 511 | success = InitializeSettings(*dev_mode, |
| 512 | device_name, |
[email protected] | d825462 | 2010-08-13 19:15:46 | [diff] [blame] | 513 | page_ranges, |
| 514 | num_page_ranges, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 515 | print_selection_only); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 516 | } |
| 517 | |
| 518 | if (!success && dialog_options.hDC) { |
| 519 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 520 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | if (dev_mode) { |
| 524 | GlobalUnlock(dialog_options.hDevMode); |
| 525 | } |
| 526 | } else { |
| 527 | if (dialog_options.hDC) { |
| 528 | DeleteDC(dialog_options.hDC); |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | if (dialog_options.hDevMode != NULL) |
| 533 | GlobalFree(dialog_options.hDevMode); |
| 534 | if (dialog_options.hDevNames != NULL) |
| 535 | GlobalFree(dialog_options.hDevNames); |
| 536 | |
| 537 | switch (dialog_options.dwResultAction) { |
| 538 | case PD_RESULT_PRINT: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 539 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | case PD_RESULT_APPLY: |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 541 | return context_ ? CANCEL : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 542 | case PD_RESULT_CANCEL: |
| 543 | return CANCEL; |
| 544 | default: |
| 545 | return FAILED; |
| 546 | } |
| 547 | } |
| 548 | |
[email protected] | 97a8063 | 2014-03-27 17:50:39 | [diff] [blame] | 549 | HRESULT PrintingContextWin::ShowPrintDialog(PRINTDLGEX* options) { |
| 550 | // Note that this cannot use ui::BaseShellDialog as the print dialog is |
| 551 | // system modal: opening it from a background thread can cause Windows to |
| 552 | // get the wrong Z-order which will make the print dialog appear behind the |
| 553 | // browser frame (but still being modal) so neither the browser frame nor |
| 554 | // the print dialog will get any input. See http://crbug.com/342697 |
| 555 | // http://crbug.com/180997 for details. |
| 556 | base::MessageLoop::ScopedNestableTaskAllower allow( |
| 557 | base::MessageLoop::current()); |
| 558 | |
| 559 | return PrintDlgEx(options); |
| 560 | } |
| 561 | |
[email protected] | 51e8d935 | 2010-10-06 22:21:17 | [diff] [blame] | 562 | PrintingContext::Result PrintingContextWin::ParseDialogResult( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 563 | const PRINTDLG& dialog_options) { |
| 564 | // If the user clicked OK or Apply then Cancel, but not only Cancel. |
| 565 | // Start fresh. |
| 566 | ResetSettings(); |
| 567 | |
| 568 | DEVMODE* dev_mode = NULL; |
| 569 | if (dialog_options.hDevMode) { |
| 570 | dev_mode = |
| 571 | reinterpret_cast<DEVMODE*>(GlobalLock(dialog_options.hDevMode)); |
| 572 | DCHECK(dev_mode); |
| 573 | } |
| 574 | |
| 575 | std::wstring device_name; |
| 576 | if (dialog_options.hDevNames) { |
| 577 | DEVNAMES* dev_names = |
| 578 | reinterpret_cast<DEVNAMES*>(GlobalLock(dialog_options.hDevNames)); |
| 579 | DCHECK(dev_names); |
| 580 | if (dev_names) { |
| 581 | device_name = |
| 582 | reinterpret_cast<const wchar_t*>( |
| 583 | reinterpret_cast<const wchar_t*>(dev_names) + |
| 584 | dev_names->wDeviceOffset); |
| 585 | GlobalUnlock(dialog_options.hDevNames); |
| 586 | } |
| 587 | } |
| 588 | |
| 589 | bool success = false; |
| 590 | if (dev_mode && !device_name.empty()) { |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 591 | context_ = dialog_options.hDC; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 592 | success = InitializeSettings(*dev_mode, device_name, NULL, 0, false); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | if (!success && dialog_options.hDC) { |
| 596 | DeleteDC(dialog_options.hDC); |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 597 | context_ = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | if (dev_mode) { |
| 601 | GlobalUnlock(dialog_options.hDevMode); |
| 602 | } |
| 603 | |
| 604 | if (dialog_options.hDevMode != NULL) |
| 605 | GlobalFree(dialog_options.hDevMode); |
| 606 | if (dialog_options.hDevNames != NULL) |
| 607 | GlobalFree(dialog_options.hDevNames); |
| 608 | |
[email protected] | b75dca8 | 2009-10-13 18:46:21 | [diff] [blame] | 609 | return context_ ? OK : FAILED; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | } // namespace printing |