diff options
author | initial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-26 23:55:29 +0000 |
---|---|---|
committer | initial.commit <initial.commit@0039d316-1c4b-4281-b951-d872f2087c98> | 2008-07-26 23:55:29 +0000 |
commit | 09911bf300f1a419907a9412154760efd0b7abc3 (patch) | |
tree | f131325fb4e2ad12c6d3504ab75b16dd92facfed /chrome/browser/printing/print_settings.cc | |
parent | 586acc5fe142f498261f52c66862fa417c3d52d2 (diff) | |
download | chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.zip chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.gz chromium_src-09911bf300f1a419907a9412154760efd0b7abc3.tar.bz2 |
Add chrome to the repository.
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@15 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/printing/print_settings.cc')
-rw-r--r-- | chrome/browser/printing/print_settings.cc | 195 |
1 files changed, 195 insertions, 0 deletions
diff --git a/chrome/browser/printing/print_settings.cc b/chrome/browser/printing/print_settings.cc new file mode 100644 index 0000000..308e306 --- /dev/null +++ b/chrome/browser/printing/print_settings.cc @@ -0,0 +1,195 @@ +// Copyright 2008, Google Inc. +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are +// met: +// +// * Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above +// copyright notice, this list of conditions and the following disclaimer +// in the documentation and/or other materials provided with the +// distribution. +// * Neither the name of Google Inc. nor the names of its +// contributors may be used to endorse or promote products derived from +// this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include "chrome/browser/printing/print_settings.h" + +#include "base/logging.h" +#include "chrome/browser/printing/units.h" +#include "chrome/common/render_messages.h" + +namespace printing { + +int PrintSettings::s_cookie_; + +PrintSettings::PrintSettings() + : min_shrink(1.25), + max_shrink(2.0), + desired_dpi(72), + dpi_(0), + landscape_(false) { +} + +void PrintSettings::Clear() { + ranges.clear(); + min_shrink = 1.25; + max_shrink = 2.; + desired_dpi = 72; + printer_name_.clear(); + device_name_.clear(); + page_setup_cmm_.Clear(); + page_setup_pixels_.Clear(); + dpi_ = 0; + landscape_ = false; +} + +#ifdef WIN32 +void PrintSettings::Init(HDC hdc, + const DEVMODE& dev_mode, + const PageRanges& new_ranges, + const std::wstring& new_device_name) { + DCHECK(hdc); + printer_name_ = dev_mode.dmDeviceName; + device_name_ = new_device_name; + ranges = new_ranges; + landscape_ = dev_mode.dmOrientation == DMORIENT_LANDSCAPE; + + int old_dpi = dpi_; + dpi_ = GetDeviceCaps(hdc, LOGPIXELSX); + // No printer device is known to advertise different dpi in X and Y axis; even + // the fax device using the 200x100 dpi setting. It's ought to break so many + // applications that it's not even needed to care about. WebKit doesn't + // support different dpi settings in X and Y axis. + DCHECK_EQ(dpi_, GetDeviceCaps(hdc, LOGPIXELSY)); + + DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORX), 0); + DCHECK_EQ(GetDeviceCaps(hdc, SCALINGFACTORY), 0); + + // Initialize page_setup_pixels_. + gfx::Size physical_size_pixels(GetDeviceCaps(hdc, PHYSICALWIDTH), + GetDeviceCaps(hdc, PHYSICALHEIGHT)); + gfx::Rect printable_area_pixels(GetDeviceCaps(hdc, PHYSICALOFFSETX), + GetDeviceCaps(hdc, PHYSICALOFFSETY), + GetDeviceCaps(hdc, HORZRES), + GetDeviceCaps(hdc, VERTRES)); + // Hard-code text_height = 0.5cm = ~1/5 of inch + page_setup_pixels_.Init(physical_size_pixels, printable_area_pixels, + ConvertUnit(500, kHundrethsMMPerInch, dpi_)); + + // Initialize page_setup_cmm_. + // In theory, we should be using HORZSIZE and VERTSIZE but their value is + // so wrong it's useless. So read the values in dpi unit and convert them back + // in 0.01 mm. + gfx::Size physical_size_cmm( + ConvertUnit(physical_size_pixels.width(), dpi_, kHundrethsMMPerInch), + ConvertUnit(physical_size_pixels.height(), dpi_, kHundrethsMMPerInch)); + gfx::Rect printable_area_cmm( + ConvertUnit(printable_area_pixels.x(), dpi_, kHundrethsMMPerInch), + ConvertUnit(printable_area_pixels.y(), dpi_, kHundrethsMMPerInch), + ConvertUnit(printable_area_pixels.width(), dpi_, kHundrethsMMPerInch), + ConvertUnit(printable_area_pixels.bottom(), dpi_, kHundrethsMMPerInch)); + + static const int kRoundingTolerance = 5; + // Some printers may advertise a slightly larger printable area than the + // physical area. This is mostly due to integer calculation and rounding. + if (physical_size_cmm.height() > printable_area_cmm.bottom() && + physical_size_cmm.height() <= (printable_area_cmm.bottom() + + kRoundingTolerance)) { + physical_size_cmm.set_height(printable_area_cmm.bottom()); + } + if (physical_size_cmm.width() > printable_area_cmm.right() && + physical_size_cmm.width() <= (printable_area_cmm.right() + + kRoundingTolerance)) { + physical_size_cmm.set_width(printable_area_cmm.right()); + } + page_setup_cmm_.Init(physical_size_cmm, printable_area_cmm, 500); +} +#endif + +void PrintSettings::UpdateMarginsMetric(const PageMargins& new_margins) { + // Apply the new margins in 0.01 mm unit. + page_setup_cmm_.SetRequestedMargins(new_margins); + + // Converts the margins in dpi unit and apply those too. + PageMargins pixels_margins; + pixels_margins.header = ConvertUnit(new_margins.header, kHundrethsMMPerInch, + dpi_); + pixels_margins.footer = ConvertUnit(new_margins.footer, kHundrethsMMPerInch, + dpi_); + pixels_margins.left = ConvertUnit(new_margins.left, kHundrethsMMPerInch, + dpi_); + pixels_margins.top = ConvertUnit(new_margins.top, kHundrethsMMPerInch, dpi_); + pixels_margins.right = ConvertUnit(new_margins.right, kHundrethsMMPerInch, + dpi_); + pixels_margins.bottom = ConvertUnit(new_margins.bottom, kHundrethsMMPerInch, + dpi_); + page_setup_pixels_.SetRequestedMargins(pixels_margins); +} + +void PrintSettings::UpdateMarginsMilliInch(const PageMargins& new_margins) { + // Convert margins from thousandth inches to cmm (0.01mm). + PageMargins cmm_margins; + cmm_margins.header = + ConvertMilliInchToHundredThousanthMeter(new_margins.header); + cmm_margins.footer = + ConvertMilliInchToHundredThousanthMeter(new_margins.footer); + cmm_margins.left = ConvertMilliInchToHundredThousanthMeter(new_margins.left); + cmm_margins.top = ConvertMilliInchToHundredThousanthMeter(new_margins.top); + cmm_margins.right = + ConvertMilliInchToHundredThousanthMeter(new_margins.right); + cmm_margins.bottom = + ConvertMilliInchToHundredThousanthMeter(new_margins.bottom); + UpdateMarginsMetric(cmm_margins); +} + +void PrintSettings::RenderParams(ViewMsg_Print_Params* params) const { + DCHECK(params); + params->printable_size.SetSize(page_setup_pixels_.content_area().width(), + page_setup_pixels_.content_area().height()); + params->dpi = dpi_; + // Currently hardcoded at 1.25. See PrintSettings' constructor. + params->min_shrink = min_shrink; + // Currently hardcoded at 2.0. See PrintSettings' constructor. + params->max_shrink = max_shrink; + // Currently hardcoded at 72dpi. See PrintSettings' constructor. + params->desired_dpi = desired_dpi; + // Always use an invalid cookie. + params->document_cookie = 0; +} + +bool PrintSettings::Equals(const PrintSettings& rhs) const { + // Do not test the display device name (printer_name_) for equality since it + // may sometimes be chopped off at 30 chars. As long as device_name is the + // same, that's fine. + return ranges == rhs.ranges && + min_shrink == rhs.min_shrink && + max_shrink == rhs.max_shrink && + desired_dpi == rhs.desired_dpi && + overlays.Equals(rhs.overlays) && + device_name_ == rhs.device_name_ && + page_setup_pixels_.Equals(rhs.page_setup_pixels_) && + page_setup_cmm_.Equals(rhs.page_setup_cmm_) && + dpi_ == rhs.dpi_ && + landscape_ == rhs.landscape_; +} + +int PrintSettings::NewCookie() { + return base::AtomicIncrement(&s_cookie_); +} + +} // namespace printing |