blob: b76821bdc93c2ea82c942d25481caa7231181dec [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]c86d4722009-06-23 17:59:5577 int margin_printer_units = ConvertUnit(500, kHundrethsMMPerInch, dpi_);
78
[email protected]4ecd07452009-03-31 14:34:4379 // Start by setting the user configuration
initial.commit09911bf2008-07-26 23:55:2980 // Hard-code text_height = 0.5cm = ~1/5 of inch
[email protected]4ae30d082009-02-20 17:55:5581 page_setup_pixels_.Init(physical_size_pixels,
82 printable_area_pixels,
[email protected]c86d4722009-06-23 17:59:5583 margin_printer_units);
initial.commit09911bf2008-07-26 23:55:2984
[email protected]4ecd07452009-03-31 14:34:4385 // Now apply user configured settings.
86 PageMargins margins;
[email protected]c86d4722009-06-23 17:59:5587 margins.header = margin_printer_units;
88 margins.footer = margin_printer_units;
89 margins.left = margin_printer_units;
90 margins.top = margin_printer_units;
91 margins.right = margin_printer_units;
92 margins.bottom = margin_printer_units;
[email protected]4ecd07452009-03-31 14:34:4393 page_setup_pixels_.SetRequestedMargins(margins);
initial.commit09911bf2008-07-26 23:55:2994}
95
initial.commit09911bf2008-07-26 23:55:2996bool PrintSettings::Equals(const PrintSettings& rhs) const {
97 // Do not test the display device name (printer_name_) for equality since it
98 // may sometimes be chopped off at 30 chars. As long as device_name is the
99 // same, that's fine.
100 return ranges == rhs.ranges &&
101 min_shrink == rhs.min_shrink &&
102 max_shrink == rhs.max_shrink &&
103 desired_dpi == rhs.desired_dpi &&
104 overlays.Equals(rhs.overlays) &&
105 device_name_ == rhs.device_name_ &&
106 page_setup_pixels_.Equals(rhs.page_setup_pixels_) &&
initial.commit09911bf2008-07-26 23:55:29107 dpi_ == rhs.dpi_ &&
108 landscape_ == rhs.landscape_;
109}
110
111int PrintSettings::NewCookie() {
[email protected]13c9eec2008-08-06 13:42:44112 // A cookie of 0 is used to mark a document as unassigned, count from 1.
113 return cookie_seq.GetNext() + 1;
initial.commit09911bf2008-07-26 23:55:29114}
115
116} // namespace printing