blob: d4f21ed1067a112aa079bae1d5972efc8d6d489e [file] [log] [blame]
Torne (Richard Coles)58218062012-11-14 11:43:16 +00001// Copyright (c) 2011 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.
4
5#include "printing/print_settings_initializer_gtk.h"
6
7#include <gtk/gtk.h>
8#include <gtk/gtkunixprint.h>
9
Torne (Richard Coles)7d4cd472013-06-19 11:58:07 +010010#include "base/strings/string16.h"
Torne (Richard Coles)868fa2f2013-06-11 10:57:03 +010011#include "base/strings/utf_string_conversions.h"
Torne (Richard Coles)58218062012-11-14 11:43:16 +000012#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
28 string16 name(UTF8ToUTF16(static_cast<const char*>(
29 gtk_print_settings_get_printer(settings))));
30 print_settings->set_printer_name(name);
31 print_settings->set_device_name(print_settings->printer_name());
32 print_settings->ranges = new_ranges;
33 print_settings->selection_only = print_selection_only;
34
35 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 {
49 // Use default values if we cannot get valid values from the print dialog.
50 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(
54 static_cast<int>(page_width_in_pixel),
55 static_cast<int>(page_height_in_pixel));
56 printable_area_device_units.SetRect(
57 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);
61 }
62
63 print_settings->set_dpi(dpi);
64 print_settings->SetPrinterPrintableArea(physical_size_device_units,
65 printable_area_device_units,
66 dpi);
67
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);
77}
78
79const double PrintSettingsInitializerGtk::kTopMarginInInch = 0.25;
80const double PrintSettingsInitializerGtk::kBottomMarginInInch = 0.56;
81const double PrintSettingsInitializerGtk::kLeftMarginInInch = 0.25;
82const double PrintSettingsInitializerGtk::kRightMarginInInch = 0.25;
83
84} // namespace printing