- add sources.
[platform/framework/web/crosswalk.git] / src / printing / print_settings_initializer_gtk.cc
1 // 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
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "printing/print_settings.h"
13 #include "printing/units.h"
14
15 namespace printing {
16
17 // static
18 void PrintSettingsInitializerGtk::InitPrintSettings(
19     GtkPrintSettings* settings,
20     GtkPageSetup* page_setup,
21     PrintSettings* print_settings) {
22   DCHECK(settings);
23   DCHECK(page_setup);
24   DCHECK(print_settings);
25
26   base::string16 name(base::UTF8ToUTF16(static_cast<const char*>(
27       gtk_print_settings_get_printer(settings))));
28   print_settings->set_device_name(name);
29
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;
46     double page_width_in_pixel = kLetterWidthInch * dpi;
47     double page_height_in_pixel = kLetterHeightInch * dpi;
48     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);
59
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);
68   // Set before SetPrinterPrintableArea to make it flip area if necessary.
69   print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE);
70   DCHECK_EQ(print_settings->device_units_per_inch(), dpi);
71   print_settings->SetPrinterPrintableArea(physical_size_device_units,
72                                           printable_area_device_units,
73                                           true);
74 }
75
76 const double PrintSettingsInitializerGtk::kTopMarginInInch = 0.25;
77 const double PrintSettingsInitializerGtk::kBottomMarginInInch = 0.56;
78 const double PrintSettingsInitializerGtk::kLeftMarginInInch = 0.25;
79 const double PrintSettingsInitializerGtk::kRightMarginInInch = 0.25;
80
81 }  // namespace printing