| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "chrome/browser/printing/print_settings.h" | |
| 6 | |
| 7 #include "base/atomic_sequence_num.h" | |
| 8 #include "base/logging.h" | |
| 9 #include "chrome/common/render_messages.h" | |
| 10 #include "printing/units.h" | |
| 11 | |
| 12 namespace printing { | |
| 13 | |
| 14 // Global SequenceNumber used for generating unique cookie values. | |
| 15 static base::AtomicSequenceNumber cookie_seq(base::LINKER_INITIALIZED); | |
| 16 | |
| 17 PrintSettings::PrintSettings() | |
| 18 : min_shrink(1.25), | |
| 19 max_shrink(2.0), | |
| 20 desired_dpi(72), | |
| 21 selection_only(false), | |
| 22 dpi_(0), | |
| 23 landscape_(false) { | |
| 24 } | |
| 25 | |
| 26 void PrintSettings::Clear() { | |
| 27 ranges.clear(); | |
| 28 min_shrink = 1.25; | |
| 29 max_shrink = 2.; | |
| 30 desired_dpi = 72; | |
| 31 selection_only = false; | |
| 32 printer_name_.clear(); | |
| 33 device_name_.clear(); | |
| 34 page_setup_pixels_.Clear(); | |
| 35 dpi_ = 0; | |
| 36 landscape_ = false; | |
| 37 } | |
| 38 | |
| 39 #ifdef WIN32 | |
| 40 void PrintSettings::Init(HDC hdc, | |
| 41 const DEVMODE& dev_mode, | |
| 42 const PageRanges& new_ranges, | |
| 43 const std::wstring& new_device_name, | |
| 44 bool print_selection_only) { | |
| 45 DCHECK(hdc); | |
| 46 printer_name_ = dev_mode.dmDeviceName; | |
| 47 device_name_ = new_device_name; | |
| 48 ranges = new_ranges; | |
| 49 landscape_ = dev_mode.dmOrientation == DMORIENT_LANDSCAPE; | |
| 50 selection_only = print_selection_only; | |
| 51 | |
| 52 dpi_ = GetDeviceCaps(hdc, LOGPIXELSX); | |
| 53 // No printer device is known to advertise different dpi in X and Y axis; even | |
| 54 // the fax device using the 200x100 dpi setting. It's ought to break so many | |
| 55 // applications that it's not even needed to care about. WebKit doesn't | |
| 56 // support different dpi settings in X and Y axis. | |
| 57 DCHECK_EQ(dpi_, GetDeviceCaps(hdc, LOGPIXELSY)); | |
| 58 | |
| 59 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0); | |
| 60 DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0); | |
| 61 | |
| 62 // Initialize page_setup_pixels_. | |
| 63 gfx::Size physical_size_pixels(GetDeviceCaps(hdc, PHYSICALWIDTH), | |
| 64 GetDeviceCaps(hdc, PHYSICALHEIGHT)); | |
| 65 gfx::Rect printable_area_pixels(GetDeviceCaps(hdc, PHYSICALOFFSETX), | |
| 66 GetDeviceCaps(hdc, PHYSICALOFFSETY), | |
| 67 GetDeviceCaps(hdc, HORZRES), | |
| 68 GetDeviceCaps(hdc, VERTRES)); | |
| 69 | |
| 70 SetPrinterPrintableArea(physical_size_pixels, printable_area_pixels); | |
| 71 } | |
| 72 #endif | |
| 73 | |
| 74 void PrintSettings::SetPrinterPrintableArea( | |
| 75 gfx::Size const& physical_size_pixels, | |
| 76 gfx::Rect const& printable_area_pixels) { | |
| 77 | |
| 78 int margin_printer_units = ConvertUnit(500, kHundrethsMMPerInch, dpi_); | |
| 79 | |
| 80 // Start by setting the user configuration | |
| 81 // Hard-code text_height = 0.5cm = ~1/5 of inch | |
| 82 page_setup_pixels_.Init(physical_size_pixels, | |
| 83 printable_area_pixels, | |
| 84 margin_printer_units); | |
| 85 | |
| 86 // Now apply user configured settings. | |
| 87 PageMargins margins; | |
| 88 margins.header = margin_printer_units; | |
| 89 margins.footer = margin_printer_units; | |
| 90 margins.left = margin_printer_units; | |
| 91 margins.top = margin_printer_units; | |
| 92 margins.right = margin_printer_units; | |
| 93 margins.bottom = margin_printer_units; | |
| 94 page_setup_pixels_.SetRequestedMargins(margins); | |
| 95 } | |
| 96 | |
| 97 void PrintSettings::RenderParams(ViewMsg_Print_Params* params) const { | |
| 98 DCHECK(params); | |
| 99 params->printable_size.SetSize(page_setup_pixels_.content_area().width(), | |
| 100 page_setup_pixels_.content_area().height()); | |
| 101 params->dpi = dpi_; | |
| 102 // Currently hardcoded at 1.25. See PrintSettings' constructor. | |
| 103 params->min_shrink = min_shrink; | |
| 104 // Currently hardcoded at 2.0. See PrintSettings' constructor. | |
| 105 params->max_shrink = max_shrink; | |
| 106 // Currently hardcoded at 72dpi. See PrintSettings' constructor. | |
| 107 params->desired_dpi = desired_dpi; | |
| 108 // Always use an invalid cookie. | |
| 109 params->document_cookie = 0; | |
| 110 params->selection_only = selection_only; | |
| 111 } | |
| 112 | |
| 113 bool PrintSettings::Equals(const PrintSettings& rhs) const { | |
| 114 // Do not test the display device name (printer_name_) for equality since it | |
| 115 // may sometimes be chopped off at 30 chars. As long as device_name is the | |
| 116 // same, that's fine. | |
| 117 return ranges == rhs.ranges && | |
| 118 min_shrink == rhs.min_shrink && | |
| 119 max_shrink == rhs.max_shrink && | |
| 120 desired_dpi == rhs.desired_dpi && | |
| 121 overlays.Equals(rhs.overlays) && | |
| 122 device_name_ == rhs.device_name_ && | |
| 123 page_setup_pixels_.Equals(rhs.page_setup_pixels_) && | |
| 124 dpi_ == rhs.dpi_ && | |
| 125 landscape_ == rhs.landscape_; | |
| 126 } | |
| 127 | |
| 128 int PrintSettings::NewCookie() { | |
| 129 // A cookie of 0 is used to mark a document as unassigned, count from 1. | |
| 130 return cookie_seq.GetNext() + 1; | |
| 131 } | |
| 132 | |
| 133 } // namespace printing | |
| OLD | NEW |