Home | History | Annotate | Download | only in printing
      1 // Copyright (c) 2012 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/printing_context_gtk.h"
      6 
      7 #include <gtk/gtk.h>
      8 #include <gtk/gtkunixprint.h>
      9 
     10 #include "base/logging.h"
     11 #include "base/values.h"
     12 #include "printing/metafile.h"
     13 #include "printing/print_dialog_gtk_interface.h"
     14 #include "printing/print_job_constants.h"
     15 #include "printing/units.h"
     16 
     17 namespace {
     18 
     19 // Function pointer for creating print dialogs. |callback| is only used when
     20 // |show_dialog| is true.
     21 printing::PrintDialogGtkInterface* (*create_dialog_func_)(
     22     printing::PrintingContextGtk* context) = NULL;
     23 
     24 }  // namespace
     25 
     26 namespace printing {
     27 
     28 // static
     29 PrintingContext* PrintingContext::Create(const std::string& app_locale) {
     30   return static_cast<PrintingContext*>(new PrintingContextGtk(app_locale));
     31 }
     32 
     33 PrintingContextGtk::PrintingContextGtk(const std::string& app_locale)
     34     : PrintingContext(app_locale),
     35       print_dialog_(NULL) {
     36 }
     37 
     38 PrintingContextGtk::~PrintingContextGtk() {
     39   ReleaseContext();
     40 
     41   if (print_dialog_)
     42     print_dialog_->ReleaseDialog();
     43 }
     44 
     45 // static
     46 void PrintingContextGtk::SetCreatePrintDialogFunction(
     47     PrintDialogGtkInterface* (*create_dialog_func)(
     48         PrintingContextGtk* context)) {
     49   DCHECK(create_dialog_func);
     50   DCHECK(!create_dialog_func_);
     51   create_dialog_func_ = create_dialog_func;
     52 }
     53 
     54 void PrintingContextGtk::PrintDocument(const Metafile* metafile) {
     55   DCHECK(print_dialog_);
     56   DCHECK(metafile);
     57   print_dialog_->PrintDocument(metafile, document_name_);
     58 }
     59 
     60 void PrintingContextGtk::AskUserForSettings(
     61     gfx::NativeView parent_view,
     62     int max_pages,
     63     bool has_selection,
     64     const PrintSettingsCallback& callback) {
     65   print_dialog_->ShowDialog(parent_view, has_selection, callback);
     66 }
     67 
     68 PrintingContext::Result PrintingContextGtk::UseDefaultSettings() {
     69   DCHECK(!in_print_job_);
     70 
     71   ResetSettings();
     72   if (!print_dialog_) {
     73     print_dialog_ = create_dialog_func_(this);
     74     print_dialog_->AddRefToDialog();
     75   }
     76   print_dialog_->UseDefaultSettings();
     77 
     78   return OK;
     79 }
     80 
     81 gfx::Size PrintingContextGtk::GetPdfPaperSizeDeviceUnits() {
     82   GtkPageSetup* page_setup = gtk_page_setup_new();
     83 
     84   gfx::SizeF paper_size(
     85       gtk_page_setup_get_paper_width(page_setup, GTK_UNIT_INCH),
     86       gtk_page_setup_get_paper_height(page_setup, GTK_UNIT_INCH));
     87 
     88   g_object_unref(page_setup);
     89 
     90   return gfx::Size(
     91       paper_size.width() * settings_.device_units_per_inch(),
     92       paper_size.height() * settings_.device_units_per_inch());
     93 }
     94 
     95 PrintingContext::Result PrintingContextGtk::UpdatePrinterSettings(
     96     bool external_preview) {
     97   DCHECK(!in_print_job_);
     98   DCHECK(!external_preview) << "Not implemented";
     99 
    100   if (!print_dialog_) {
    101     print_dialog_ = create_dialog_func_(this);
    102     print_dialog_->AddRefToDialog();
    103   }
    104 
    105   if (!print_dialog_->UpdateSettings(&settings_))
    106     return OnError();
    107 
    108   return OK;
    109 }
    110 
    111 PrintingContext::Result PrintingContextGtk::InitWithSettings(
    112     const PrintSettings& settings) {
    113   DCHECK(!in_print_job_);
    114 
    115   settings_ = settings;
    116 
    117   return OK;
    118 }
    119 
    120 PrintingContext::Result PrintingContextGtk::NewDocument(
    121     const base::string16& document_name) {
    122   DCHECK(!in_print_job_);
    123   in_print_job_ = true;
    124 
    125   document_name_ = document_name;
    126 
    127   return OK;
    128 }
    129 
    130 PrintingContext::Result PrintingContextGtk::NewPage() {
    131   if (abort_printing_)
    132     return CANCEL;
    133   DCHECK(in_print_job_);
    134 
    135   // Intentional No-op.
    136 
    137   return OK;
    138 }
    139 
    140 PrintingContext::Result PrintingContextGtk::PageDone() {
    141   if (abort_printing_)
    142     return CANCEL;
    143   DCHECK(in_print_job_);
    144 
    145   // Intentional No-op.
    146 
    147   return OK;
    148 }
    149 
    150 PrintingContext::Result PrintingContextGtk::DocumentDone() {
    151   if (abort_printing_)
    152     return CANCEL;
    153   DCHECK(in_print_job_);
    154 
    155   ResetSettings();
    156   return OK;
    157 }
    158 
    159 void PrintingContextGtk::Cancel() {
    160   abort_printing_ = true;
    161   in_print_job_ = false;
    162 }
    163 
    164 void PrintingContextGtk::ReleaseContext() {
    165   // Intentional No-op.
    166 }
    167 
    168 gfx::NativeDrawingContext PrintingContextGtk::context() const {
    169   // Intentional No-op.
    170   return NULL;
    171 }
    172 
    173 }  // namespace printing
    174 
    175