blob: 4df0483ea09dd259ea30e8231c7c8be67881f4ad [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
[email protected]8ff1d422009-07-07 21:31:395#include "printing/print_settings.h"
initial.commit09911bf2008-07-26 23:55:296
[email protected]d30e8e642008-08-06 12:05:247#include "base/atomic_sequence_num.h"
initial.commit09911bf2008-07-26 23:55:298#include "base/logging.h"
[email protected]4ae30d082009-02-20 17:55:559#include "printing/units.h"
initial.commit09911bf2008-07-26 23:55:2910
11namespace printing {
12
[email protected]d30e8e642008-08-06 12:05:2413// Global SequenceNumber used for generating unique cookie values.
[email protected]a10a16b2008-09-02 13:11:4614static base::AtomicSequenceNumber cookie_seq(base::LINKER_INITIALIZED);
initial.commit09911bf2008-07-26 23:55:2915
16PrintSettings::PrintSettings()
17 : min_shrink(1.25),
18 max_shrink(2.0),
19 desired_dpi(72),
[email protected]c8ad40c2009-06-08 17:05:2120 selection_only(false),
initial.commit09911bf2008-07-26 23:55:2921 dpi_(0),
22 landscape_(false) {
23}
24
25void PrintSettings::Clear() {
26 ranges.clear();
27 min_shrink = 1.25;
28 max_shrink = 2.;
29 desired_dpi = 72;
[email protected]c8ad40c2009-06-08 17:05:2130 selection_only = false;
initial.commit09911bf2008-07-26 23:55:2931 printer_name_.clear();
32 device_name_.clear();
initial.commit09911bf2008-07-26 23:55:2933 page_setup_pixels_.Clear();
34 dpi_ = 0;
35 landscape_ = false;
36}
37
38#ifdef WIN32
39void PrintSettings::Init(HDC hdc,
40 const DEVMODE& dev_mode,
41 const PageRanges& new_ranges,
[email protected]c8ad40c2009-06-08 17:05:2142 const std::wstring& new_device_name,
43 bool print_selection_only) {
initial.commit09911bf2008-07-26 23:55:2944 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]c8ad40c2009-06-08 17:05:2149 selection_only = print_selection_only;
initial.commit09911bf2008-07-26 23:55:2950
initial.commit09911bf2008-07-26 23:55:2951 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]4ecd07452009-03-31 14:34:4368
69 SetPrinterPrintableArea(physical_size_pixels, printable_area_pixels);
70}
71#endif
72
73void PrintSettings::SetPrinterPrintableArea(
74 gfx::Size const& physical_size_pixels,
75 gfx::Rect const& printable_area_pixels) {
76
[email protected]be3b19a2009-07-13 14:58:1877 // Hard-code text_height = 0.5cm = ~1/5 of inch.
78 int header_footer_text_height = ConvertUnit(500, kHundrethsMMPerInch, dpi_);
[email protected]c86d4722009-06-23 17:59:5579
[email protected]4ecd07452009-03-31 14:34:4380 // Start by setting the user configuration
[email protected]4ae30d082009-02-20 17:55:5581 page_setup_pixels_.Init(physical_size_pixels,
82 printable_area_pixels,
[email protected]be3b19a2009-07-13 14:58:1883 header_footer_text_height);
initial.commit09911bf2008-07-26 23:55:2984
[email protected]be3b19a2009-07-13 14:58:1885 // 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]4ecd07452009-03-31 14:34:4391 PageMargins margins;
[email protected]be3b19a2009-07-13 14:58:1892 margins.header = header_footer_text_height;
93 margins.footer = header_footer_text_height;
[email protected]c86d4722009-06-23 17:59:5594 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]4ecd07452009-03-31 14:34:4398 page_setup_pixels_.SetRequestedMargins(margins);
initial.commit09911bf2008-07-26 23:55:2999}
100
initial.commit09911bf2008-07-26 23:55:29101bool 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.commit09911bf2008-07-26 23:55:29112 dpi_ == rhs.dpi_ &&
113 landscape_ == rhs.landscape_;
114}
115
116int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44117 // A cookie of 0 is used to mark a document as unassigned, count from 1.
118 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29119}
120
121} // namespace printing