license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
[email protected] | 8ff1d42 | 2009-07-07 21:31:39 | [diff] [blame] | 5 | #include "printing/print_settings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 6 | |
[email protected] | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 7 | #include "base/atomic_sequence_num.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 8 | #include "base/logging.h" |
[email protected] | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 9 | #include "printing/units.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | |
| 11 | namespace printing { |
| 12 | |
[email protected] | d30e8e64 | 2008-08-06 12:05:24 | [diff] [blame] | 13 | // Global SequenceNumber used for generating unique cookie values. |
[email protected] | a10a16b | 2008-09-02 13:11:46 | [diff] [blame] | 14 | static base::AtomicSequenceNumber cookie_seq(base::LINKER_INITIALIZED); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | |
| 16 | PrintSettings::PrintSettings() |
| 17 | : min_shrink(1.25), |
| 18 | max_shrink(2.0), |
| 19 | desired_dpi(72), |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 20 | selection_only(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | dpi_(0), |
| 22 | landscape_(false) { |
| 23 | } |
| 24 | |
| 25 | void PrintSettings::Clear() { |
| 26 | ranges.clear(); |
| 27 | min_shrink = 1.25; |
| 28 | max_shrink = 2.; |
| 29 | desired_dpi = 72; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 30 | selection_only = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 31 | printer_name_.clear(); |
| 32 | device_name_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 33 | page_setup_pixels_.Clear(); |
| 34 | dpi_ = 0; |
| 35 | landscape_ = false; |
| 36 | } |
| 37 | |
| 38 | #ifdef WIN32 |
| 39 | void PrintSettings::Init(HDC hdc, |
| 40 | const DEVMODE& dev_mode, |
| 41 | const PageRanges& new_ranges, |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 42 | const std::wstring& new_device_name, |
| 43 | bool print_selection_only) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | DCHECK(hdc); |
| 45 | printer_name_ = dev_mode.dmDeviceName; |
| 46 | device_name_ = new_device_name; |
| 47 | ranges = new_ranges; |
| 48 | landscape_ = dev_mode.dmOrientation == DMORIENT_LANDSCAPE; |
[email protected] | c8ad40c | 2009-06-08 17:05:21 | [diff] [blame] | 49 | selection_only = print_selection_only; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 50 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 51 | dpi_ = GetDeviceCaps(hdc, LOGPIXELSX); |
| 52 | // No printer device is known to advertise different dpi in X and Y axis; even |
| 53 | // the fax device using the 200x100 dpi setting. It's ought to break so many |
| 54 | // applications that it's not even needed to care about. WebKit doesn't |
| 55 | // support different dpi settings in X and Y axis. |
| 56 | DCHECK_EQ(dpi_, GetDeviceCaps(hdc, LOGPIXELSY)); |
| 57 | |
| 58 | DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0); |
| 59 | DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0); |
| 60 | |
| 61 | // Initialize page_setup_pixels_. |
| 62 | gfx::Size physical_size_pixels(GetDeviceCaps(hdc, PHYSICALWIDTH), |
| 63 | GetDeviceCaps(hdc, PHYSICALHEIGHT)); |
| 64 | gfx::Rect printable_area_pixels(GetDeviceCaps(hdc, PHYSICALOFFSETX), |
| 65 | GetDeviceCaps(hdc, PHYSICALOFFSETY), |
| 66 | GetDeviceCaps(hdc, HORZRES), |
| 67 | GetDeviceCaps(hdc, VERTRES)); |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 68 | |
| 69 | SetPrinterPrintableArea(physical_size_pixels, printable_area_pixels); |
| 70 | } |
| 71 | #endif |
| 72 | |
| 73 | void PrintSettings::SetPrinterPrintableArea( |
| 74 | gfx::Size const& physical_size_pixels, |
| 75 | gfx::Rect const& printable_area_pixels) { |
| 76 | |
[email protected] | be3b19a | 2009-07-13 14:58:18 | [diff] [blame] | 77 | // Hard-code text_height = 0.5cm = ~1/5 of inch. |
| 78 | int header_footer_text_height = ConvertUnit(500, kHundrethsMMPerInch, dpi_); |
[email protected] | c86d472 | 2009-06-23 17:59:55 | [diff] [blame] | 79 | |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 80 | // Start by setting the user configuration |
[email protected] | 4ae30d08 | 2009-02-20 17:55:55 | [diff] [blame] | 81 | page_setup_pixels_.Init(physical_size_pixels, |
| 82 | printable_area_pixels, |
[email protected] | be3b19a | 2009-07-13 14:58:18 | [diff] [blame] | 83 | header_footer_text_height); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 84 | |
[email protected] | be3b19a | 2009-07-13 14:58:18 | [diff] [blame] | 85 | // Default margins 1.0cm = ~2/5 of an inch. |
| 86 | int margin_printer_units = ConvertUnit(1000, kHundrethsMMPerInch, dpi_); |
| 87 | |
| 88 | // Apply default margins (not user configurable just yet). |
| 89 | // Since the font height is half the margin we put the header and footers at |
| 90 | // the font height from the margins. |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 91 | PageMargins margins; |
[email protected] | be3b19a | 2009-07-13 14:58:18 | [diff] [blame] | 92 | margins.header = header_footer_text_height; |
| 93 | margins.footer = header_footer_text_height; |
[email protected] | c86d472 | 2009-06-23 17:59:55 | [diff] [blame] | 94 | margins.left = margin_printer_units; |
| 95 | margins.top = margin_printer_units; |
| 96 | margins.right = margin_printer_units; |
| 97 | margins.bottom = margin_printer_units; |
[email protected] | 4ecd0745 | 2009-03-31 14:34:43 | [diff] [blame] | 98 | page_setup_pixels_.SetRequestedMargins(margins); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | } |
| 100 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 101 | bool PrintSettings::Equals(const PrintSettings& rhs) const { |
| 102 | // Do not test the display device name (printer_name_) for equality since it |
| 103 | // may sometimes be chopped off at 30 chars. As long as device_name is the |
| 104 | // same, that's fine. |
| 105 | return ranges == rhs.ranges && |
| 106 | min_shrink == rhs.min_shrink && |
| 107 | max_shrink == rhs.max_shrink && |
| 108 | desired_dpi == rhs.desired_dpi && |
| 109 | overlays.Equals(rhs.overlays) && |
| 110 | device_name_ == rhs.device_name_ && |
| 111 | page_setup_pixels_.Equals(rhs.page_setup_pixels_) && |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 112 | dpi_ == rhs.dpi_ && |
| 113 | landscape_ == rhs.landscape_; |
| 114 | } |
| 115 | |
| 116 | int PrintSettings::NewCookie() { |
[email protected] | 13c9eec | 2008-08-06 13:42:44 | [diff] [blame] | 117 | // A cookie of 0 is used to mark a document as unassigned, count from 1. |
| 118 | return cookie_seq.GetNext() + 1; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | } // namespace printing |