Home | History | Annotate | Download | only in libgtk2ui
      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.
      4 
      5 #include "chrome/browser/ui/libgtk2ui/printing_gtk2_util.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/printing_context_linux.h"
     14 #include "printing/units.h"
     15 
     16 namespace {
     17 
     18 const double kTopMarginInInch = 0.25;
     19 const double kBottomMarginInInch = 0.56;
     20 const double kLeftMarginInInch = 0.25;
     21 const double kRightMarginInInch = 0.25;
     22 
     23 }  // namespace
     24 
     25 gfx::Size GetPdfPaperSizeDeviceUnitsGtk(
     26     printing::PrintingContextLinux* context) {
     27   GtkPageSetup* page_setup = gtk_page_setup_new();
     28 
     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));
     32 
     33   g_object_unref(page_setup);
     34 
     35   const printing::PrintSettings& settings = context->settings();
     36 
     37   return gfx::Size(
     38       paper_size.width() * settings.device_units_per_inch(),
     39       paper_size.height() * settings.device_units_per_inch());
     40 }
     41 
     42 void InitPrintSettingsGtk(GtkPrintSettings* settings,
     43                           GtkPageSetup* page_setup,
     44                           printing::PrintSettings* print_settings) {
     45   DCHECK(settings);
     46   DCHECK(page_setup);
     47   DCHECK(print_settings);
     48 
     49   base::string16 name(base::UTF8ToUTF16(static_cast<const char*>(
     50       gtk_print_settings_get_printer(settings))));
     51   print_settings->set_device_name(name);
     52 
     53   gfx::Size physical_size_device_units;
     54   gfx::Rect printable_area_device_units;
     55   int dpi = gtk_print_settings_get_resolution(settings);
     56   if (dpi) {
     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);
     66   } else {
     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);
     79   }
     80 
     81   print_settings->set_dpi(dpi);
     82 
     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
     87   // printing.
     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,
     96                                           true);
     97 }
     98