1 // Copyright 2014 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.
5 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.h"
8 #include <gtk/gtkunixprint.h>
10 #include "base/strings/string16.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "printing/print_settings.h"
13 #include "printing/printing_context_linux.h"
14 #include "printing/units.h"
18 const double kTopMarginInInch
= 0.25;
19 const double kBottomMarginInInch
= 0.56;
20 const double kLeftMarginInInch
= 0.25;
21 const double kRightMarginInInch
= 0.25;
25 gfx::Size
GetPdfPaperSizeDeviceUnitsGtk(
26 printing::PrintingContextLinux
* context
) {
27 GtkPageSetup
* page_setup
= gtk_page_setup_new();
29 gfx::SizeF
paper_size(
30 gtk_page_setup_get_paper_width(page_setup
, GTK_UNIT_INCH
),
31 gtk_page_setup_get_paper_height(page_setup
, GTK_UNIT_INCH
));
33 g_object_unref(page_setup
);
35 const printing::PrintSettings
& settings
= context
->settings();
38 paper_size
.width() * settings
.device_units_per_inch(),
39 paper_size
.height() * settings
.device_units_per_inch());
42 void InitPrintSettingsGtk(GtkPrintSettings
* settings
,
43 GtkPageSetup
* page_setup
,
44 printing::PrintSettings
* print_settings
) {
47 DCHECK(print_settings
);
49 base::string16
name(base::UTF8ToUTF16(static_cast<const char*>(
50 gtk_print_settings_get_printer(settings
))));
51 print_settings
->set_device_name(name
);
53 gfx::Size physical_size_device_units
;
54 gfx::Rect printable_area_device_units
;
55 int dpi
= gtk_print_settings_get_resolution(settings
);
57 // Initialize page_setup_device_units_.
58 physical_size_device_units
.SetSize(
59 gtk_page_setup_get_paper_width(page_setup
, GTK_UNIT_INCH
) * dpi
,
60 gtk_page_setup_get_paper_height(page_setup
, GTK_UNIT_INCH
) * dpi
);
61 printable_area_device_units
.SetRect(
62 gtk_page_setup_get_left_margin(page_setup
, GTK_UNIT_INCH
) * dpi
,
63 gtk_page_setup_get_top_margin(page_setup
, GTK_UNIT_INCH
) * dpi
,
64 gtk_page_setup_get_page_width(page_setup
, GTK_UNIT_INCH
) * dpi
,
65 gtk_page_setup_get_page_height(page_setup
, GTK_UNIT_INCH
) * dpi
);
67 // Use default values if we cannot get valid values from the print dialog.
68 dpi
= printing::kPixelsPerInch
;
69 double page_width_in_pixel
= printing::kLetterWidthInch
* dpi
;
70 double page_height_in_pixel
= printing::kLetterHeightInch
* dpi
;
71 physical_size_device_units
.SetSize(
72 static_cast<int>(page_width_in_pixel
),
73 static_cast<int>(page_height_in_pixel
));
74 printable_area_device_units
.SetRect(
75 static_cast<int>(kLeftMarginInInch
* dpi
),
76 static_cast<int>(kTopMarginInInch
* dpi
),
77 page_width_in_pixel
- (kLeftMarginInInch
+ kRightMarginInInch
) * dpi
,
78 page_height_in_pixel
- (kTopMarginInInch
+ kBottomMarginInInch
) * dpi
);
81 print_settings
->set_dpi(dpi
);
83 // Note: With the normal GTK print dialog, when the user selects the landscape
84 // orientation, all that does is change the paper size. Which seems to be
85 // enough to render the right output and send it to the printer.
86 // The orientation value stays as portrait and does not actually affect
88 // Thus this is only useful in print preview mode, where we manually set the
89 // orientation and change the paper size ourselves.
90 GtkPageOrientation orientation
= gtk_print_settings_get_orientation(settings
);
91 // Set before SetPrinterPrintableArea to make it flip area if necessary.
92 print_settings
->SetOrientation(orientation
== GTK_PAGE_ORIENTATION_LANDSCAPE
);
93 DCHECK_EQ(print_settings
->device_units_per_inch(), dpi
);
94 print_settings
->SetPrinterPrintableArea(physical_size_device_units
,
95 printable_area_device_units
,