| 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 |
| 11 #include "base/i18n/file_util_icu.h" | 11 #include "base/i18n/file_util_icu.h" |
| 12 #include "base/i18n/time_formatting.h" | 12 #include "base/i18n/time_formatting.h" |
| 13 #include "base/message_loop.h" | 13 #include "base/message_loop.h" |
| 14 #include "base/metrics/histogram.h" | 14 #include "base/metrics/histogram.h" |
| 15 #include "base/time.h" | 15 #include "base/time.h" |
| 16 #include "base/utf_string_conversions.h" | 16 #include "base/utf_string_conversions.h" |
| 17 #include "base/values.h" | 17 #include "base/values.h" |
| 18 #include "base/win/metro.h" | 18 #include "base/win/metro.h" |
| 19 #include "printing/backend/print_backend.h" | 19 #include "printing/backend/print_backend.h" |
| 20 #include "printing/backend/win_helper.h" | 20 #include "printing/backend/win_helper.h" |
| 21 #include "printing/backend/printing_info_win.h" |
| 21 #include "printing/print_job_constants.h" | 22 #include "printing/print_job_constants.h" |
| 22 #include "printing/print_settings_initializer_win.h" | 23 #include "printing/print_settings_initializer_win.h" |
| 23 #include "printing/printed_document.h" | 24 #include "printing/printed_document.h" |
| 24 #include "printing/units.h" | 25 #include "printing/units.h" |
| 25 #include "skia/ext/platform_device.h" | 26 #include "skia/ext/platform_device.h" |
| 26 #include "win8/util/win8_util.h" | 27 #include "win8/util/win8_util.h" |
| 27 | 28 |
| 28 using base::Time; | 29 using base::Time; |
| 29 | 30 |
| 30 namespace { | 31 namespace { |
| 31 | 32 |
| 32 // Constants for setting default PDF settings. | 33 // Constants for setting default PDF settings. |
| 33 const int kPDFDpi = 300; // 300 dpi | 34 const int kPDFDpi = 300; // 300 dpi |
| 34 // LETTER: 8.5 x 11 inches | 35 // LETTER: 8.5 x 11 inches |
| 35 const int kPDFLetterWidth = 8.5 * kPDFDpi; | 36 const int kPDFLetterWidth = 8.5 * kPDFDpi; |
| 36 const int kPDFLetterHeight = 11 * kPDFDpi; | 37 const int kPDFLetterHeight = 11 * kPDFDpi; |
| 37 // LEGAL: 8.5 x 14 inches | 38 // LEGAL: 8.5 x 14 inches |
| 38 const int kPDFLegalWidth = 8.5 * kPDFDpi; | 39 const int kPDFLegalWidth = 8.5 * kPDFDpi; |
| 39 const int kPDFLegalHeight = 14 * kPDFDpi; | 40 const int kPDFLegalHeight = 14 * kPDFDpi; |
| 40 // A4: 8.27 x 11.69 inches | 41 // A4: 8.27 x 11.69 inches |
| 41 const int kPDFA4Width = 8.27 * kPDFDpi; | 42 const int kPDFA4Width = 8.27 * kPDFDpi; |
| 42 const int kPDFA4Height = 11.69 * kPDFDpi; | 43 const int kPDFA4Height = 11.69 * kPDFDpi; |
| 43 // A3: 11.69 x 16.54 inches | 44 // A3: 11.69 x 16.54 inches |
| 44 const int kPDFA3Width = 11.69 * kPDFDpi; | 45 const int kPDFA3Width = 11.69 * kPDFDpi; |
| 45 const int kPDFA3Height = 16.54 * kPDFDpi; | 46 const int kPDFA3Height = 16.54 * kPDFDpi; |
| 46 | 47 |
| 47 // Retrieves the printer's PRINTER_INFO_* structure. | |
| 48 // Output |level| can be 9 (user-default), 8 (admin-default), or 2 | |
| 49 // (printer-default). | |
| 50 // |devmode| is a pointer points to the start of DEVMODE structure in | |
| 51 // |buffer|. | |
| 52 bool GetPrinterInfo(HANDLE printer, | |
| 53 const std::wstring &device_name, | |
| 54 int* level, | |
| 55 scoped_array<uint8>* buffer, | |
| 56 DEVMODE** dev_mode) { | |
| 57 DCHECK(buffer); | |
| 58 | |
| 59 // A PRINTER_INFO_9 structure specifying the per-user default printer | |
| 60 // settings. | |
| 61 printing::PrintingContextWin::GetPrinterHelper(printer, 9, buffer); | |
| 62 if (buffer->get()) { | |
| 63 PRINTER_INFO_9* info_9 = reinterpret_cast<PRINTER_INFO_9*>(buffer->get()); | |
| 64 if (info_9->pDevMode != NULL) { | |
| 65 *level = 9; | |
| 66 *dev_mode = info_9->pDevMode; | |
| 67 return true; | |
| 68 } | |
| 69 buffer->reset(); | |
| 70 } | |
| 71 | |
| 72 // A PRINTER_INFO_8 structure specifying the global default printer settings. | |
| 73 printing::PrintingContextWin::GetPrinterHelper(printer, 8, buffer); | |
| 74 if (buffer->get()) { | |
| 75 PRINTER_INFO_8* info_8 = reinterpret_cast<PRINTER_INFO_8*>(buffer->get()); | |
| 76 if (info_8->pDevMode != NULL) { | |
| 77 *level = 8; | |
| 78 *dev_mode = info_8->pDevMode; | |
| 79 return true; | |
| 80 } | |
| 81 buffer->reset(); | |
| 82 } | |
| 83 | |
| 84 // A PRINTER_INFO_2 structure specifying the driver's default printer | |
| 85 // settings. | |
| 86 printing::PrintingContextWin::GetPrinterHelper(printer, 2, buffer); | |
| 87 if (buffer->get()) { | |
| 88 PRINTER_INFO_2* info_2 = reinterpret_cast<PRINTER_INFO_2*>(buffer->get()); | |
| 89 if (info_2->pDevMode != NULL) { | |
| 90 *level = 2; | |
| 91 *dev_mode = info_2->pDevMode; | |
| 92 return true; | |
| 93 } | |
| 94 buffer->reset(); | |
| 95 } | |
| 96 | |
| 97 return false; | |
| 98 } | |
| 99 | |
| 100 } // anonymous namespace | 48 } // anonymous namespace |
| 101 | 49 |
| 102 namespace printing { | 50 namespace printing { |
| 103 | 51 |
| 104 class PrintingContextWin::CallbackHandler : public IPrintDialogCallback, | 52 class PrintingContextWin::CallbackHandler : public IPrintDialogCallback, |
| 105 public IObjectWithSite { | 53 public IObjectWithSite { |
| 106 public: | 54 public: |
| 107 CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd) | 55 CallbackHandler(PrintingContextWin& owner, HWND owner_hwnd) |
| 108 : owner_(owner), | 56 : owner_(owner), |
| 109 owner_hwnd_(owner_hwnd), | 57 owner_hwnd_(owner_hwnd), |
| (...skipping 544 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 654 new_device_name, | 602 new_device_name, |
| 655 selection_only, | 603 selection_only, |
| 656 &settings_); | 604 &settings_); |
| 657 | 605 |
| 658 return true; | 606 return true; |
| 659 } | 607 } |
| 660 | 608 |
| 661 bool PrintingContextWin::GetPrinterSettings(HANDLE printer, | 609 bool PrintingContextWin::GetPrinterSettings(HANDLE printer, |
| 662 const std::wstring& device_name) { | 610 const std::wstring& device_name) { |
| 663 DCHECK(!in_print_job_); | 611 DCHECK(!in_print_job_); |
| 664 scoped_array<uint8> buffer; | 612 |
| 665 int level = 0; | 613 DevMode dev_mode; |
| 666 DEVMODE* dev_mode = NULL; | |
| 667 | 614 |
| 668 if (GetPrinterInfo(printer, device_name, &level, &buffer, &dev_mode) && | 615 if (dev_mode.Init(printer) && |
| 669 AllocateContext(device_name, dev_mode, &context_)) { | 616 AllocateContext(device_name, dev_mode.get(), &context_)) { |
| 670 return InitializeSettings(*dev_mode, device_name, NULL, 0, false); | 617 return InitializeSettings(*dev_mode.get(), device_name, NULL, 0, false); |
| 671 } | 618 } |
| 672 | 619 |
| 673 buffer.reset(); | |
| 674 ResetSettings(); | 620 ResetSettings(); |
| 675 return false; | 621 return false; |
| 676 } | 622 } |
| 677 | 623 |
| 678 // static | 624 // static |
| 679 bool PrintingContextWin::AllocateContext(const std::wstring& device_name, | 625 bool PrintingContextWin::AllocateContext(const std::wstring& device_name, |
| 680 const DEVMODE* dev_mode, | 626 const DEVMODE* dev_mode, |
| 681 gfx::NativeDrawingContext* context) { | 627 gfx::NativeDrawingContext* context) { |
| 682 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); | 628 *context = CreateDC(L"WINSPOOL", device_name.c_str(), NULL, dev_mode); |
| 683 DCHECK(*context); | 629 DCHECK(*context); |
| (...skipping 122 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 806 } | 752 } |
| 807 | 753 |
| 808 if (dialog_options.hDevMode != NULL) | 754 if (dialog_options.hDevMode != NULL) |
| 809 GlobalFree(dialog_options.hDevMode); | 755 GlobalFree(dialog_options.hDevMode); |
| 810 if (dialog_options.hDevNames != NULL) | 756 if (dialog_options.hDevNames != NULL) |
| 811 GlobalFree(dialog_options.hDevNames); | 757 GlobalFree(dialog_options.hDevNames); |
| 812 | 758 |
| 813 return context_ ? OK : FAILED; | 759 return context_ ? OK : FAILED; |
| 814 } | 760 } |
| 815 | 761 |
| 816 // static | |
| 817 void PrintingContextWin::GetPrinterHelper(HANDLE printer, int level, | |
| 818 scoped_array<uint8>* buffer) { | |
| 819 DWORD buf_size = 0; | |
| 820 GetPrinter(printer, level, NULL, 0, &buf_size); | |
| 821 if (buf_size) { | |
| 822 buffer->reset(new uint8[buf_size]); | |
| 823 memset(buffer->get(), 0, buf_size); | |
| 824 if (!GetPrinter(printer, level, buffer->get(), buf_size, &buf_size)) { | |
| 825 buffer->reset(); | |
| 826 } | |
| 827 } | |
| 828 } | |
| 829 | |
| 830 } // namespace printing | 762 } // namespace printing |
| OLD | NEW |