Skip to content

Commit

Permalink
Printing: Refactor PrintSettings::Init into a PrintSettingsFactory class
Browse files Browse the repository at this point in the history
per-platform.

BUG=none
TEST=none

Review URL: http://codereview.chromium.org/4040001

git-svn-id: svn://svn.chromium.org/chrome/trunk/src@63907 0039d316-1c4b-4281-b951-d872f2087c98
  • Loading branch information
jhawkins@chromium.org committed Oct 26, 2010
1 parent caf706f commit 4993f34
Show file tree
Hide file tree
Showing 15 changed files with 341 additions and 193 deletions.
4 changes: 2 additions & 2 deletions chrome/browser/printing/print_job_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ class TestPrintNotifObserv : public NotificationObserver {
#define MAYBE_SimplePrint SimplePrint
#endif
TEST(PrintJobTest, MAYBE_SimplePrint) {
// Test the multithreaded nature of PrintJob to make sure we can use it with
// known livetime.
// Test the multi-threaded nature of PrintJob to make sure we can use it with
// known lifetime.

// This message loop is actually never run.
MessageLoop current;
Expand Down
146 changes: 0 additions & 146 deletions printing/print_settings.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,7 @@

#include "printing/print_settings.h"

// TODO(jhawkins): Move platform-specific implementations to their own files.
#if defined(USE_X11)
#include <gtk/gtk.h>
#include <gtk/gtkprinter.h>
#include "printing/native_metafile.h"
#endif // defined(USE_X11)

#include "base/atomic_sequence_num.h"
#include "base/logging.h"
#include "base/string_piece.h"
#include "base/sys_string_conversions.h"
#include "base/utf_string_conversions.h"
#include "printing/units.h"

namespace printing {
Expand Down Expand Up @@ -49,141 +38,6 @@ void PrintSettings::Clear() {
landscape_ = false;
}

#if defined(OS_WIN)
void PrintSettings::Init(HDC hdc,
const DEVMODE& dev_mode,
const PageRanges& new_ranges,
const std::wstring& new_device_name,
bool print_selection_only) {
DCHECK(hdc);
printer_name_ = dev_mode.dmDeviceName;
device_name_ = new_device_name;
ranges = new_ranges;
landscape_ = dev_mode.dmOrientation == DMORIENT_LANDSCAPE;
selection_only = print_selection_only;

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_device_units_.
gfx::Size physical_size_device_units(GetDeviceCaps(hdc, PHYSICALWIDTH),
GetDeviceCaps(hdc, PHYSICALHEIGHT));
gfx::Rect printable_area_device_units(GetDeviceCaps(hdc, PHYSICALOFFSETX),
GetDeviceCaps(hdc, PHYSICALOFFSETY),
GetDeviceCaps(hdc, HORZRES),
GetDeviceCaps(hdc, VERTRES));

SetPrinterPrintableArea(physical_size_device_units,
printable_area_device_units,
dpi_);
}
#elif defined(OS_MACOSX)
void PrintSettings::Init(PMPrinter printer, PMPageFormat page_format,
const PageRanges& new_ranges,
bool print_selection_only) {
printer_name_ = base::SysCFStringRefToWide(PMPrinterGetName(printer));
device_name_ = base::SysCFStringRefToWide(PMPrinterGetID(printer));
ranges = new_ranges;
PMOrientation orientation = kPMPortrait;
PMGetOrientation(page_format, &orientation);
landscape_ = orientation == kPMLandscape;
selection_only = print_selection_only;

UInt32 resolution_count = 0;
PMResolution best_resolution = { 72.0, 72.0 };
OSStatus status = PMPrinterGetPrinterResolutionCount(printer,
&resolution_count);
if (status == noErr) {
// Resolution indexes are 1-based.
for (uint32 i = 1; i <= resolution_count; ++i) {
PMResolution resolution;
PMPrinterGetIndexedPrinterResolution(printer, i, &resolution);
if (resolution.hRes > best_resolution.hRes)
best_resolution = resolution;
}
}
dpi_ = best_resolution.hRes;
// See comment in the Windows code above.
DCHECK_EQ(dpi_, best_resolution.vRes);

// Get printable area and paper rects (in points)
PMRect page_rect, paper_rect;
PMGetAdjustedPageRect(page_format, &page_rect);
PMGetAdjustedPaperRect(page_format, &paper_rect);
// Device units are in points. Units per inch is 72.
gfx::Size physical_size_device_units(
(paper_rect.right - paper_rect.left),
(paper_rect.bottom - paper_rect.top));
gfx::Rect printable_area_device_units(
(page_rect.left - paper_rect.left),
(page_rect.top - paper_rect.top),
(page_rect.right - page_rect.left),
(page_rect.bottom - page_rect.top));

SetPrinterPrintableArea(physical_size_device_units,
printable_area_device_units,
72);
}
#elif defined(USE_X11)
void PrintSettings::Init(GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_only) {
// TODO(jhawkins): |printer_name_| and |device_name_| should be string16.
base::StringPiece name(
reinterpret_cast<const char*>(gtk_print_settings_get_printer(settings)));
printer_name_ = UTF8ToWide(name);
device_name_ = printer_name_;
ranges = new_ranges;
selection_only = print_selection_only;

GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
landscape_ = orientation == GTK_PAGE_ORIENTATION_LANDSCAPE;

gfx::Size physical_size_device_units;
gfx::Rect printable_area_device_units;
dpi_ = gtk_print_settings_get_resolution(settings);
if (dpi_) {
// Initialize page_setup_device_units_.
physical_size_device_units.SetSize(
gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi_);
printable_area_device_units.SetRect(
gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi_,
gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi_);
} else {
// Use dummy values if we cannot get valid values.
// TODO(jhawkins) Remove this hack when the Linux printing refactoring
// finishes.
dpi_ = kPixelsPerInch;
double page_width_in_pixel = 8.5 * dpi_;
double page_height_in_pixel = 11.0 * dpi_;
physical_size_device_units.SetSize(
static_cast<int>(page_width_in_pixel),
static_cast<int>(page_height_in_pixel));
printable_area_device_units.SetRect(
static_cast<int>(
NativeMetafile::kLeftMarginInInch * printing::kPixelsPerInch),
static_cast<int>(
NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch),
page_width_in_pixel,
page_height_in_pixel);
}
SetPrinterPrintableArea(physical_size_device_units,
printable_area_device_units,
dpi_);
}
#endif

void PrintSettings::SetPrinterPrintableArea(
gfx::Size const& physical_size_device_units,
gfx::Rect const& printable_area_device_units,
Expand Down
38 changes: 5 additions & 33 deletions printing/print_settings.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,6 @@
#include "printing/page_range.h"
#include "printing/page_setup.h"

#if defined(OS_MACOSX)
#import <ApplicationServices/ApplicationServices.h>
#endif

#if defined(OS_WIN)
typedef struct HDC__* HDC;
typedef struct _devicemodeW DEVMODE;
#elif defined(USE_X11)
typedef struct _GtkPrintSettings GtkPrintSettings;
typedef struct _GtkPageSetup GtkPageSetup;
#endif

namespace printing {

// OS-independent print settings.
Expand All @@ -33,27 +21,6 @@ class PrintSettings {
// Reinitialize the settings to the default values.
void Clear();

#if defined(OS_WIN)
// Reads the settings from the selected device context. Calculates derived
// values like printable_area_.
void Init(HDC hdc,
const DEVMODE& dev_mode,
const PageRanges& new_ranges,
const std::wstring& new_device_name,
bool selection_only);
#elif defined(OS_MACOSX)
// Reads the settings from the given PMPrinter and PMPageFormat.
void Init(PMPrinter printer, PMPageFormat page_format,
const PageRanges& new_ranges, bool print_selection_only);
#elif defined(USE_X11)
// Initializes the settings from the given GtkPrintSettings and GtkPageSetup.
// TODO(jhawkins): This method is a mess across the platforms. Refactor.
void Init(GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_onl);
#endif

// Set printer printable area in in device units.
void SetPrinterPrintableArea(gfx::Size const& physical_size_device_units,
gfx::Rect const& printable_area_device_units,
Expand All @@ -64,11 +31,16 @@ class PrintSettings {
// output.
bool Equals(const PrintSettings& rhs) const;

void set_landscape(bool landscape) { landscape_ = landscape; }
void set_printer_name(const std::wstring& printer_name) {
printer_name_ = printer_name;
}
const std::wstring& printer_name() const { return printer_name_; }
void set_device_name(const std::wstring& device_name) {
device_name_ = device_name;
}
const std::wstring& device_name() const { return device_name_; }
void set_dpi(int dpi) { dpi_ = dpi; }
int dpi() const { return dpi_; }
const PageSetup& page_setup_device_units() const {
return page_setup_device_units_;
Expand Down
78 changes: 78 additions & 0 deletions printing/print_settings_initializer_gtk.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "printing/print_settings_initializer_gtk.h"

#include <gtk/gtk.h>
#include <gtk/gtkprinter.h>

#include "base/string_piece.h"
#include "base/utf_string_conversions.h"
#include "printing/native_metafile.h"
#include "printing/print_settings.h"
#include "printing/units.h"

namespace printing {

// static
void PrintSettingsInitializerGtk::InitPrintSettings(
GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_only,
PrintSettings* print_settings) {
DCHECK(settings);
DCHECK(page_setup);
DCHECK(print_settings);

// TODO(jhawkins): |printer_name_| and |device_name_| should be string16.
base::StringPiece name(
reinterpret_cast<const char*>(gtk_print_settings_get_printer(settings)));
print_settings->set_printer_name(UTF8ToWide(name));
print_settings->set_device_name(print_settings->printer_name());
print_settings->ranges = new_ranges;
print_settings->selection_only = print_selection_only;

GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
print_settings->set_landscape(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);

gfx::Size physical_size_device_units;
gfx::Rect printable_area_device_units;
int dpi = gtk_print_settings_get_resolution(settings);
if (dpi) {
// Initialize page_setup_device_units_.
physical_size_device_units.SetSize(
gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi,
gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi);
printable_area_device_units.SetRect(
gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi,
gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi,
gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi,
gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi);
} else {
// Use dummy values if we cannot get valid values.
// TODO(jhawkins) Remove this hack when the Linux printing refactoring
// finishes.
dpi = kPixelsPerInch;
double page_width_in_pixel = 8.5 * dpi;
double page_height_in_pixel = 11.0 * dpi;
physical_size_device_units.SetSize(
static_cast<int>(page_width_in_pixel),
static_cast<int>(page_height_in_pixel));
printable_area_device_units.SetRect(
static_cast<int>(
NativeMetafile::kLeftMarginInInch * printing::kPixelsPerInch),
static_cast<int>(
NativeMetafile::kTopMarginInInch * printing::kPixelsPerInch),
page_width_in_pixel,
page_height_in_pixel);
}

print_settings->set_dpi(dpi);
print_settings->SetPrinterPrintableArea(physical_size_device_units,
printable_area_device_units,
dpi);
}

} // namespace printing
33 changes: 33 additions & 0 deletions printing/print_settings_initializer_gtk.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef PRINTING_PRINT_SETTINGS_INITIALIZER_GTK_H_
#define PRINTING_PRINT_SETTINGS_INITIALIZER_GTK_H_

#include "base/logging.h"
#include "printing/page_range.h"

typedef struct _GtkPrintSettings GtkPrintSettings;
typedef struct _GtkPageSetup GtkPageSetup;

namespace printing {

class PrintSettings;

// Initializes a PrintSettings object from the provided Gtk printer objects.
class PrintSettingsInitializerGtk {
public:
static void InitPrintSettings(GtkPrintSettings* settings,
GtkPageSetup* page_setup,
const PageRanges& new_ranges,
bool print_selection_only,
PrintSettings* print_settings);

private:
DISALLOW_IMPLICIT_CONSTRUCTORS(PrintSettingsInitializerGtk);
};

} // namespace printing

#endif // PRINTING_PRINT_SETTINGS_INITIALIZER_GTK_H_
Loading

0 comments on commit 4993f34

Please sign in to comment.