Home | History | Annotate | Download | only in printing
      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 #ifndef CHROME_BROWSER_PRINTING_PRINT_DIALOG_GTK_H_
      6 #define CHROME_BROWSER_PRINTING_PRINT_DIALOG_GTK_H_
      7 #pragma once
      8 
      9 #include <gtk/gtk.h>
     10 #include <gtk/gtkprintunixdialog.h>
     11 
     12 #include "base/file_path.h"
     13 #include "base/memory/ref_counted.h"
     14 #include "base/memory/scoped_ptr.h"
     15 #include "content/browser/browser_thread.h"
     16 #include "printing/print_dialog_gtk_interface.h"
     17 #include "printing/printing_context_cairo.h"
     18 #include "ui/base/gtk/gtk_signal.h"
     19 
     20 namespace base {
     21 class WaitableEvent;
     22 }
     23 
     24 namespace printing {
     25 class Metafile;
     26 }
     27 
     28 using printing::PrintingContextCairo;
     29 
     30 // Needs to be freed on the UI thread to clean up its GTK members variables.
     31 class PrintDialogGtk
     32     : public printing::PrintDialogGtkInterface,
     33       public base::RefCountedThreadSafe<PrintDialogGtk,
     34                                         BrowserThread::DeleteOnUIThread> {
     35  public:
     36   // Creates and returns a print dialog.
     37   static printing::PrintDialogGtkInterface* CreatePrintDialog(
     38       PrintingContextCairo* context);
     39 
     40   // printing::PrintDialogGtkInterface implementation.
     41   virtual void UseDefaultSettings();
     42   virtual bool UpdateSettings(const DictionaryValue& settings,
     43                               const printing::PageRanges& ranges);
     44   virtual void ShowDialog(
     45       PrintingContextCairo::PrintSettingsCallback* callback);
     46   virtual void PrintDocument(const printing::Metafile* metafile,
     47                              const string16& document_name);
     48   virtual void AddRefToDialog();
     49   virtual void ReleaseDialog();
     50 
     51  private:
     52   friend struct BrowserThread::DeleteOnThread<BrowserThread::UI>;
     53   friend class DeleteTask<PrintDialogGtk>;
     54 
     55   explicit PrintDialogGtk(PrintingContextCairo* context);
     56   ~PrintDialogGtk();
     57 
     58   // Handles dialog response.
     59   CHROMEGTK_CALLBACK_1(PrintDialogGtk, void, OnResponse, int);
     60 
     61   // Saves data in |metafile| to disk for document named |document_name|.
     62   void SaveDocumentToDisk(const printing::Metafile* metafile,
     63                           const string16& document_name);
     64 
     65   // Prints document named |document_name|.
     66   void SendDocumentToPrinter(const string16& document_name);
     67 
     68   // Handles print job response.
     69   static void OnJobCompletedThunk(GtkPrintJob* print_job,
     70                                   gpointer user_data,
     71                                   GError* error);
     72   void OnJobCompleted(GtkPrintJob* print_job, GError* error);
     73 
     74   // Helper function for initializing |context_|'s PrintSettings with a given
     75   // set of |page_ranges|.
     76   void InitPrintSettings(const printing::PageRanges& page_ranges);
     77 
     78   // Printing dialog callback.
     79   PrintingContextCairo::PrintSettingsCallback* callback_;
     80   PrintingContextCairo* context_;
     81 
     82   // Print dialog settings. PrintDialogGtk owns |dialog_| and holds references
     83   // to the other objects.
     84   GtkWidget* dialog_;
     85   GtkPrintSettings* gtk_settings_;
     86   GtkPageSetup* page_setup_;
     87   GtkPrinter* printer_;
     88 
     89   // Event to signal when save document finishes.
     90   scoped_ptr<base::WaitableEvent> save_document_event_;
     91 
     92   FilePath path_to_pdf_;
     93 
     94   DISALLOW_COPY_AND_ASSIGN(PrintDialogGtk);
     95 };
     96 
     97 #endif  // CHROME_BROWSER_PRINTING_PRINT_DIALOG_GTK_H_
     98