blob: 53cb9bbee380e3440a0f60e2efd80216a9c5b257 [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,
Torne (Richard Coles)58218062012-11-14 11:43:16 +000021 PrintSettings* print_settings) {
22 DCHECK(settings);
23 DCHECK(page_setup);
24 DCHECK(print_settings);
25
Torne (Richard Coles)4e180b62013-10-18 15:46:22 +010026 base::string16 name(base::UTF8ToUTF16(static_cast<const char*>(
Torne (Richard Coles)58218062012-11-14 11:43:16 +000027 gtk_print_settings_get_printer(settings))));
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000028 print_settings->set_device_name(name);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000029
30 gfx::Size physical_size_device_units;
31 gfx::Rect printable_area_device_units;
32 int dpi = gtk_print_settings_get_resolution(settings);
33 if (dpi) {
34 // Initialize page_setup_device_units_.
35 physical_size_device_units.SetSize(
36 gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH) * dpi,
37 gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH) * dpi);
38 printable_area_device_units.SetRect(
39 gtk_page_setup_get_left_margin(page_setup, GTK_UNIT_INCH) * dpi,
40 gtk_page_setup_get_top_margin(page_setup, GTK_UNIT_INCH) * dpi,
41 gtk_page_setup_get_page_width(page_setup, GTK_UNIT_INCH) * dpi,
42 gtk_page_setup_get_page_height(page_setup, GTK_UNIT_INCH) * dpi);
43 } else {
44 // Use default values if we cannot get valid values from the print dialog.
45 dpi = kPixelsPerInch;
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000046 double page_width_in_pixel = kLetterWidthInch * dpi;
47 double page_height_in_pixel = kLetterHeightInch * dpi;
Torne (Richard Coles)58218062012-11-14 11:43:16 +000048 physical_size_device_units.SetSize(
49 static_cast<int>(page_width_in_pixel),
50 static_cast<int>(page_height_in_pixel));
51 printable_area_device_units.SetRect(
52 static_cast<int>(kLeftMarginInInch * dpi),
53 static_cast<int>(kTopMarginInInch * dpi),
54 page_width_in_pixel - (kLeftMarginInInch + kRightMarginInInch) * dpi,
55 page_height_in_pixel - (kTopMarginInInch + kBottomMarginInInch) * dpi);
56 }
57
58 print_settings->set_dpi(dpi);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000059
60 // Note: With the normal GTK print dialog, when the user selects the landscape
61 // orientation, all that does is change the paper size. Which seems to be
62 // enough to render the right output and send it to the printer.
63 // The orientation value stays as portrait and does not actually affect
64 // printing.
65 // Thus this is only useful in print preview mode, where we manually set the
66 // orientation and change the paper size ourselves.
67 GtkPageOrientation orientation = gtk_print_settings_get_orientation(settings);
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000068 // Set before SetPrinterPrintableArea to make it flip area if necessary.
Torne (Richard Coles)58218062012-11-14 11:43:16 +000069 print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000070 DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
Torne (Richard Coles)1e9bf3e2013-10-31 11:16:26 +000071 print_settings->SetPrinterPrintableArea(physical_size_device_units,
72 printable_area_device_units,
Torne (Richard Coles)0f1bc082013-11-06 12:27:47 +000073 true);
Torne (Richard Coles)58218062012-11-14 11:43:16 +000074}
75
76const double PrintSettingsInitializerGtk::kTopMarginInInch = 0.25;
77const double PrintSettingsInitializerGtk::kBottomMarginInInch = 0.56;
78const double PrintSettingsInitializerGtk::kLeftMarginInInch = 0.25;
79const double PrintSettingsInitializerGtk::kRightMarginInInch = 0.25;
80
81} // namespace printing