Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 | |
Torne (Richard Coles) | 7d4cd47 | 2013-06-19 11:58:07 +0100 | [diff] [blame] | 10 | #include "base/strings/string16.h" |
Torne (Richard Coles) | 868fa2f | 2013-06-11 10:57:03 +0100 | [diff] [blame] | 11 | #include "base/strings/utf_string_conversions.h" |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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, |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 21 | PrintSettings* print_settings) { |
| 22 | DCHECK(settings); |
| 23 | DCHECK(page_setup); |
| 24 | DCHECK(print_settings); |
| 25 | |
Torne (Richard Coles) | 4e180b6 | 2013-10-18 15:46:22 +0100 | [diff] [blame] | 26 | base::string16 name(base::UTF8ToUTF16(static_cast<const char*>( |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 27 | gtk_print_settings_get_printer(settings)))); |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 28 | print_settings->set_device_name(name); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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; |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 46 | double page_width_in_pixel = kLetterWidthInch * dpi; |
| 47 | double page_height_in_pixel = kLetterHeightInch * dpi; |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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); |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 68 | // Set before SetPrinterPrintableArea to make it flip area if necessary. |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 69 | print_settings->SetOrientation(orientation == GTK_PAGE_ORIENTATION_LANDSCAPE); |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 70 | DCHECK_EQ(print_settings->device_units_per_inch(), dpi); |
Torne (Richard Coles) | 1e9bf3e | 2013-10-31 11:16:26 +0000 | [diff] [blame] | 71 | print_settings->SetPrinterPrintableArea(physical_size_device_units, |
| 72 | printable_area_device_units, |
Torne (Richard Coles) | 0f1bc08 | 2013-11-06 12:27:47 +0000 | [diff] [blame] | 73 | true); |
Torne (Richard Coles) | 5821806 | 2012-11-14 11:43:16 +0000 | [diff] [blame] | 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 |