OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "printing/printing_context_win.h" | 5 #include "printing/printing_context_win.h" |
6 | 6 |
7 #include <winspool.h> | 7 #include <winspool.h> |
8 | 8 |
9 #include <algorithm> | 9 #include <algorithm> |
10 | 10 |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT; | 262 dialog_options.Flags = PD_RETURNDC | PD_RETURNDEFAULT; |
263 if (PrintDlg(&dialog_options)) | 263 if (PrintDlg(&dialog_options)) |
264 return ParseDialogResult(dialog_options); | 264 return ParseDialogResult(dialog_options); |
265 | 265 |
266 // No default printer configured, do we have any printers at all? | 266 // No default printer configured, do we have any printers at all? |
267 DWORD bytes_needed = 0; | 267 DWORD bytes_needed = 0; |
268 DWORD count_returned = 0; | 268 DWORD count_returned = 0; |
269 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, | 269 (void)::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
270 NULL, 2, NULL, 0, &bytes_needed, &count_returned); | 270 NULL, 2, NULL, 0, &bytes_needed, &count_returned); |
271 if (bytes_needed) { | 271 if (bytes_needed) { |
272 DCHECK(bytes_needed >= count_returned * sizeof(PRINTER_INFO_2)); | 272 DCHECK_GE(bytes_needed, count_returned * sizeof(PRINTER_INFO_2)); |
273 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); | 273 scoped_ptr<BYTE[]> printer_info_buffer(new BYTE[bytes_needed]); |
274 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, | 274 BOOL ret = ::EnumPrinters(PRINTER_ENUM_LOCAL|PRINTER_ENUM_CONNECTIONS, |
275 NULL, 2, printer_info_buffer.get(), | 275 NULL, 2, printer_info_buffer.get(), |
276 bytes_needed, &bytes_needed, | 276 bytes_needed, &bytes_needed, |
277 &count_returned); | 277 &count_returned); |
278 if (ret && count_returned) { // have printers | 278 if (ret && count_returned) { // have printers |
279 // Open the first successfully found printer. | 279 // Open the first successfully found printer. |
280 for (DWORD count = 0; count < count_returned; ++count) { | 280 const PRINTER_INFO_2* info_2 = |
281 PRINTER_INFO_2* info_2 = reinterpret_cast<PRINTER_INFO_2*>( | 281 reinterpret_cast<PRINTER_INFO_2*>(printer_info_buffer.get()); |
282 printer_info_buffer.get() + count * sizeof(PRINTER_INFO_2)); | 282 const PRINTER_INFO_2* info_2_end = info_2 + count_returned; |
283 std::wstring printer_name = info_2->pPrinterName; | 283 for (; info_2 < info_2_end; ++info_2) { |
284 if (info_2->pDevMode == NULL || printer_name.length() == 0) | 284 ScopedPrinterHandle printer; |
| 285 if (!printer.OpenPrinter(info_2->pPrinterName)) |
285 continue; | 286 continue; |
286 if (!AllocateContext(printer_name, info_2->pDevMode, &context_)) | 287 scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL); |
287 break; | 288 if (!dev_mode || !AllocateContext(info_2->pPrinterName, dev_mode.get(), |
288 if (InitializeSettings(*info_2->pDevMode, printer_name, | 289 &context_)) { |
289 NULL, 0, false)) { | 290 continue; |
290 break; | 291 } |
| 292 if (InitializeSettings(*dev_mode.get(), info_2->pPrinterName, NULL, 0, |
| 293 false)) { |
| 294 return OK; |
291 } | 295 } |
292 ReleaseContext(); | 296 ReleaseContext(); |
293 } | 297 } |
294 if (context_) | 298 if (context_) |
295 return OK; | 299 return OK; |
296 } | 300 } |
297 } | 301 } |
298 | 302 |
299 ResetSettings(); | 303 ResetSettings(); |
300 return FAILED; | 304 return FAILED; |
(...skipping 28 matching lines...) Expand all Loading... |
329 paper_size.width() * settings_.device_units_per_inch(), | 333 paper_size.width() * settings_.device_units_per_inch(), |
330 paper_size.height() * settings_.device_units_per_inch()); | 334 paper_size.height() * settings_.device_units_per_inch()); |
331 } | 335 } |
332 | 336 |
333 PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( | 337 PrintingContext::Result PrintingContextWin::UpdatePrinterSettings( |
334 bool external_preview) { | 338 bool external_preview) { |
335 DCHECK(!in_print_job_); | 339 DCHECK(!in_print_job_); |
336 DCHECK(!external_preview) << "Not implemented"; | 340 DCHECK(!external_preview) << "Not implemented"; |
337 | 341 |
338 ScopedPrinterHandle printer; | 342 ScopedPrinterHandle printer; |
339 LPWSTR device_name_wide = | 343 if (!printer.OpenPrinter(settings_.device_name().c_str())) |
340 const_cast<wchar_t*>(settings_.device_name().c_str()); | |
341 if (!printer.OpenPrinter(device_name_wide)) | |
342 return OnError(); | 344 return OnError(); |
343 | 345 |
344 // Make printer changes local to Chrome. | 346 // Make printer changes local to Chrome. |
345 // See MSDN documentation regarding DocumentProperties. | 347 // See MSDN documentation regarding DocumentProperties. |
346 scoped_ptr<DEVMODE[]> scoped_dev_mode = | 348 scoped_ptr<DEVMODE[]> scoped_dev_mode = |
347 CreateDevModeWithColor(printer, device_name_wide, | 349 CreateDevModeWithColor(printer, settings_.device_name(), |
348 settings_.color() != GRAY); | 350 settings_.color() != GRAY); |
349 if (!scoped_dev_mode) | 351 if (!scoped_dev_mode) |
350 return OnError(); | 352 return OnError(); |
351 | 353 |
352 { | 354 { |
353 DEVMODE* dev_mode = scoped_dev_mode.get(); | 355 DEVMODE* dev_mode = scoped_dev_mode.get(); |
354 dev_mode->dmCopies = std::max(settings_.copies(), 1); | 356 dev_mode->dmCopies = std::max(settings_.copies(), 1); |
355 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies | 357 if (dev_mode->dmCopies > 1) { // do not change unless multiple copies |
356 dev_mode->dmFields |= DM_COPIES; | 358 dev_mode->dmFields |= DM_COPIES; |
357 dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : | 359 dev_mode->dmCollate = settings_.collate() ? DMCOLLATE_TRUE : |
(...skipping 223 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
581 PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode, | 583 PrintSettingsInitializerWin::InitPrintSettings(context_, dev_mode, |
582 &settings_); | 584 &settings_); |
583 | 585 |
584 return true; | 586 return true; |
585 } | 587 } |
586 | 588 |
587 bool PrintingContextWin::GetPrinterSettings(HANDLE printer, | 589 bool PrintingContextWin::GetPrinterSettings(HANDLE printer, |
588 const std::wstring& device_name) { | 590 const std::wstring& device_name) { |
589 DCHECK(!in_print_job_); | 591 DCHECK(!in_print_job_); |
590 | 592 |
591 UserDefaultDevMode user_settings; | 593 scoped_ptr<DEVMODE[]> dev_mode = CreateDevMode(printer, NULL); |
592 | 594 |
593 if (!user_settings.Init(printer) || | 595 if (!dev_mode || !AllocateContext(device_name, dev_mode.get(), &context_)) { |
594 !AllocateContext(device_name, user_settings.get(), &context_)) { | |
595 ResetSettings(); | 596 ResetSettings(); |
596 return false; | 597 return false; |
597 } | 598 } |
598 | 599 |
599 return InitializeSettings(*user_settings.get(), device_name, NULL, 0, false); | 600 return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false); |
600 } | 601 } |
601 | 602 |
602 // static | 603 // static |
603 bool PrintingContextWin::AllocateContext(const std::wstring& device_name, | 604 bool PrintingContextWin::AllocateContext(const std::wstring& device_name, |
604 const DEVMODE* dev_mode, | 605 const DEVMODE* dev_mode, |
605 gfx::NativeDrawingContext* context) { | 606 gfx::NativeDrawingContext* context) { |
606 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); | 607 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
607 DCHECK(*context); | 608 DCHECK(*context); |
608 return *context != NULL; | 609 return *context != NULL; |
609 } | 610 } |
(...skipping 119 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
729 | 730 |
730 if (dialog_options.hDevMode != NULL) | 731 if (dialog_options.hDevMode != NULL) |
731 GlobalFree(dialog_options.hDevMode); | 732 GlobalFree(dialog_options.hDevMode); |
732 if (dialog_options.hDevNames != NULL) | 733 if (dialog_options.hDevNames != NULL) |
733 GlobalFree(dialog_options.hDevNames); | 734 GlobalFree(dialog_options.hDevNames); |
734 | 735 |
735 return context_ ? OK : FAILED; | 736 return context_ ? OK : FAILED; |
736 } | 737 } |
737 | 738 |
738 } // namespace printing | 739 } // namespace printing |
OLD | NEW |