blob: 92316a6dab5f8d9e985775e676511229c5533e52 [file] [log] [blame]
[email protected]5ad76172011-03-02 19:25:171// Copyright (c) 2011 The Chromium Authors. All rights reserved.
[email protected]4993f342010-10-26 17:57:522// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "printing/print_settings_initializer_gtk.h"
6
7#include <gtk/gtk.h>
[email protected]a47aa892011-11-22 03:12:318#include <gtk/gtkunixprint.h>
[email protected]4993f342010-10-26 17:57:529
[email protected]a0f59002011-09-20 03:59:0510#include "base/string16.h"
[email protected]4993f342010-10-26 17:57:5211#include "base/utf_string_conversions.h"
[email protected]4993f342010-10-26 17:57:5212#include "printing/print_settings.h"
13#include "printing/units.h"
14
15namespace printing {
16
17// static
18void PrintSettingsInitializerGtk::InitPrintSettings(
19 GtkPrintSettings* settings,
20 GtkPageSetup* page_setup,
21 const PageRanges& new_ranges,
22 bool print_selection_only,
23 PrintSettings* print_settings) {
24 DCHECK(settings);
25 DCHECK(page_setup);
26 DCHECK(print_settings);
27
[email protected]a0f59002011-09-20 03:59:0528 string16 name(UTF8ToUTF16(static_cast<const char*>(
29 gtk_print_settings_get_printer(settings))));
30 print_settings->set_printer_name(name);
[email protected]4993f342010-10-26 17:57:5231 print_settings->set_device_name(print_settings->printer_name());
32 print_settings->ranges = new_ranges;
33 print_settings->selection_only = print_selection_only;
34
[email protected]4993f342010-10-26 17:57:5235 gfx::Size physical_size_device_units;
36 gfx::Rect printable_area_device_units;
37 int dpi = gtk_print_settings_get_resolution(settings);
38 if (dpi) {
39 // Initialize page_setup_device_units_.
40 physical_size_device_units.SetSize(
41 gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi,
42 gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi);
43 printable_area_device_units.SetRect(
44 gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi,
45 gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi,
46 gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi,
47 gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi);
48 } else {
[email protected]2896dcc2011-03-15 19:54:1049 // Use default values if we cannot get valid values from the print dialog.
[email protected]4993f342010-10-26 17:57:5250 dpi = kPixelsPerInch;
51 double page_width_in_pixel = 8.5 * dpi;
52 double page_height_in_pixel = 11.0 * dpi;
53 physical_size_device_units.SetSize(
[email protected]5ad76172011-03-02 19:25:1754 static_cast<int>(page_width_in_pixel),
55 static_cast<int>(page_height_in_pixel));
[email protected]4993f342010-10-26 17:57:5256 printable_area_device_units.SetRect(
[email protected]2896dcc2011-03-15 19:54:1057 static_cast<int>(kLeftMarginInInch * dpi),
58 static_cast<int>(kTopMarginInInch * dpi),
59 page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi,
60 page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi);
[email protected]4993f342010-10-26 17:57:5261 }
62
63 print_settings->set_dpi(dpi);
64 print_settings->SetPrinterPrintableArea(physical_size_device_units,
65 printable_area_device_units,
66 dpi);
[email protected]2ed86fdf2011-04-19 20:57:0367
68 // Note: With the normal GTK print dialog, when the user selects the landscape
69 // orientation, all that does is change the paper size. Which seems to be
70 // enough to render the right output and send it to the printer.
71 // The orientation value stays as portrait and does not actually affect
72 // printing.
73 // Thus this is only useful in print preview mode, where we manually set the
74 // orientation and change the paper size ourselves.
75 GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
76 print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);
[email protected]4993f342010-10-26 17:57:5277}
78
[email protected]2896dcc2011-03-15 19:54:1079const double PrintSettingsInitializerGtk::kTopMarginInInch = 0.25;
80const double PrintSettingsInitializerGtk::kBottomMarginInInch = 0.56;
81const double PrintSettingsInitializerGtk::kLeftMarginInInch = 0.25;
82const double PrintSettingsInitializerGtk::kRightMarginInInch = 0.25;
83
[email protected]4993f342010-10-26 17:57:5284} // namespace printing