c++-gtk-utils
file_print_manager.h
Go to the documentation of this file.
1 /* Copyright (C) 2006 to 2010 and 2013 Chris Vine
2 
3 The library comprised in this file or of which this file is part is
4 distributed by Chris Vine under the GNU Lesser General Public
5 License as follows:
6 
7  This library is free software; you can redistribute it and/or
8  modify it under the terms of the GNU Lesser General Public License
9  as published by the Free Software Foundation; either version 2.1 of
10  the License, or (at your option) any later version.
11 
12  This library is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  Lesser General Public License, version 2.1, for more details.
16 
17  You should have received a copy of the GNU Lesser General Public
18  License, version 2.1, along with this library (see the file LGPL.TXT
19  which came with this source code package in the c++-gtk-utils
20  sub-directory); if not, write to the Free Software Foundation, Inc.,
21  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 
23 */
24 
25 #ifndef CGU_FILE_PRINTMANAGER_H
26 #define CGU_FILE_PRINTMANAGER_H
27 
28 #include <string>
29 
31 
32 #ifdef CGU_USE_GTK
33 #include <gtk/gtk.h>
34 #include <gdk/gdk.h>
35 #if GTK_CHECK_VERSION(2,14,0)
36 #include <gtk/gtkunixprint.h>
37 #else
38 #include <gtk/gtkprintunixdialog.h>
39 #include <gtk/gtkprintjob.h>
40 #include <gtk/gtkprinter.h>
41 #endif
42 #endif
43 
44 #include <c++-gtk-utils/window.h>
45 #include <c++-gtk-utils/notifier.h>
46 #include <c++-gtk-utils/mutex.h>
49 #include <c++-gtk-utils/emitter.h>
50 
51 
52 namespace Cgu {
53 
54 #if defined(DOXYGEN_PARSING) || defined(CGU_USE_GTK)
55 
56 /**
57  * @class FilePrintDialog file_print_manager.h c++-gtk-utils/file_print_manager.h
58  * @brief A print dialog class for FilePrintManager.
59  * @sa FilePrintManager
60  *
61  * This class is not compiled into the library if the library is built
62  * with the \--without-gtk configuration option.
63  */
64 
65 class FilePrintDialog: public WinBase {
66 protected:
67 /**
68  * Closes the dialog, cleans up and emits the rejected Emitter object.
69  * It will not throw, even if a method connected to the rejected
70  * Emitter object throws (the exception is caught and reported in
71  * order to prevent it trying to propagate through the GTK+ event
72  * system).
73  */
74  virtual void on_delete_event();
75 public:
76 #ifndef DOXYGEN_PARSING
77  // this helper class avoids exposing GObject callbacks with C
78  // linkage to the global namespace
79  class CB;
80  friend class CB;
81 #endif
82 
83 /**
84  * The accepted Emitter object emits if the user choses OK in the
85  * print dialog. Emission of accepted by this class will not throw,
86  * even if a method connected to the accepted Emitter object throws
87  * (the exception is caught and reported in order to prevent it trying
88  * to propagate through the GTK+ event system).
89  */
91 
92 /**
93  * The rejected Emitter object emits if the user choses Cancel in the
94  * print dialog. Emission of rejected by this class will not throw,
95  * even if a method connected to the rejected Emitter object throws
96  * (the exception is caught and reported in order to prevent it trying
97  * to propagate through the GTK+ event system).
98  */
100 
101 /**
102  * Gets the currently selected printer from the print dialog. It will
103  * not throw. It must be called in the thread in which the main GTK+
104  * event loop runs.
105  * @return The currently selected printer in the print dialog. The
106  * return value is owned by GTK+ - do not unreference it.
107  */
108  GtkPrinter* get_printer() const;
109 
110 /**
111  * Gets the print settings from the print dialog. It will not throw.
112  * It must be called in the thread in which the main GTK+ event loop
113  * runs.
114  * @return The currently selected printer in the print dialog.
115  * Ownership is taken of the return value, by GobjHandle.
116  */
118 
119 /**
120  * Gets the printer page set-up from the print dialog. It will not
121  * throw. It must be called in the thread in which the main GTK+
122  * event loop runs.
123  * @return The printer page set-up in the print dialog. The return
124  * value is owned by GTK+ - do not unreference it.
125  */
126  GtkPageSetup* get_page_setup() const;
127 
128 /**
129  * The constructor will not throw. A FIlePrintDialog object must be
130  * created in the thread in which the main GTK+ event loop runs.
131  * @param parent The parent of the print dialog (NULL may be passed).
132  * @param print_settings The print settings from a previous print job
133  * (if any, NULL may be passed if none).
134  * @param caption Window caption (optional, NULL may be passed).
135  * @param window_icon A pixbuf which will comprise the window icon
136  * (optional, NULL may be passed).
137  */
138  FilePrintDialog(GtkWindow* parent, GtkPrintSettings* print_settings = 0,
139  const char* caption = 0, GdkPixbuf* window_icon = 0);
140 
141  /* we inherit glib memory slice functions from WinBase */
142 };
143 
144 
145 /**
146  * @class FilePrintManager file_print_manager.h c++-gtk-utils/file_print_manager.h
147  * @brief A class to print a file using the GTK+ print system.
148  *
149  * The FilePrintManager class prints a file printable by the print
150  * system (eg lpr or CUPS) using the GTK+ printer interface. All
151  * print systems on Unix-like systems will print Postscript (PS)
152  * files. Some may also print PDF files. To obtain a
153  * FilePrintManager object, call FilePrintManager::create_manager().
154  * FilePrintManager::set_filename() passes the name of the file to be
155  * printed. To print the file, call FilePrintManager::print(). If
156  * FilePrintManager::print() returns true, the FilePrintManager class
157  * will unlink() (ie delete) the file to be printed when it has been
158  * finished with if true is passed as the second argument of
159  * FilePrintManager::set_filename(), even if printing failed.
160  *
161  * Once FilePrintManager::print() has been called, the
162  * FilePrintManager class owns a reference to itself and so manages
163  * its own lifetime - so once that method has been called it doesn't
164  * matter if the IntrusivePtr object returned by
165  * FilePrintManager::create_manager() goes out of scope (however, once
166  * FilePrintManager::print() has been called, the FilePrintManager
167  * object will not be deleted until both printing has completed or
168  * failed AND the IntrusivePtr object returned by
169  * FilePrintManager::create_manager() has gone out of scope or has
170  * been reset()).
171  *
172  * If the print dialog which is displayed on calling
173  * FilePrintManager::print() is to display a window icon, then pass
174  * FilePrintManager::create_manager() a GobjHandle<GdkPixbuf> icon as
175  * the second argument.
176  *
177  * Normally, a user would probably only want to use any one
178  * FilePrintManager object to print a single print job (it has been
179  * designed with that in mind). Nevertheless, if a reference to the
180  * object is kept alive via an active IntrusivePtr object, it can be
181  * used to print more than one print job. However, if that is done it
182  * is not possible to call FilePrintManager::print() or
183  * FilePrintManager::set_filename() until the previous print job (if
184  * any) has been dispatched to the GTK+ print system (or cancelled).
185  * If the FilePrintManager object is not ready because it is in the
186  * middle of handling an earlier print job, then the call to
187  * FilePrintManager::print() or FilePrintManager::set_filename() will
188  * return false; if however the new print job was successfully started
189  * or file name successfully set, they will return true. It is not
190  * necessary to check the return value of these methods for the first
191  * print job despatched by any one FilePrintManager object.
192  *
193  * For printing plain text (say, from a GtkTextBuffer class, or from a
194  * plain text file), see the TextPrintManager class.
195  *
196  * This class is not compiled into the library if the library is built
197  * with the \--without-gtk configuration option.
198  */
199 
201  Thread::Mutex mutex;
202  GtkWindow* parent_p;
203  std::string caption;
204  GobjHandle<GdkPixbuf> window_icon_h;
205  bool manage;
206  std::string filename;
207  FilePrintDialog* dialog_p;
208  Notifier print_notifier;
209  bool ready;
210 
211  static GobjHandle<GtkPrintSettings> print_settings_h;
212 
213  void show_dialog();
214  void print_file();
215  void print_cancel();
216  void clean_up();
217  // private constructor
218  FilePrintManager() {}
219  // and this class may not be copied
221  FilePrintManager& operator=(const FilePrintManager&);
222 public:
223 #ifndef DOXYGEN_PARSING
224  // this helper class avoids exposing GObject callbacks with C
225  // linkage to the global namespace
226  class CB;
227  friend class CB;
228 #endif
229 
230  // TODO at a suitable ABI break, change the pixbuf to pass by constant
231  // reference to GobjHandle<GdkPixbuf> in the following method
232 /**
233  * Creates a new FilePrintManager object. No GTK+/GDK functions are
234  * called by this method, and it is thread safe provided that, if it
235  * is called in a thread other than the one in which the GTK+ event
236  * loop runs, Cgu::Notifier::init() has previously been called in the
237  * GTK+ event loop thread.
238  * @param parent The parent of the print dialog which will be created
239  * by the FilePrintManager object (optional, NULL may be passed).
240  * @param caption Window caption of the print dialog which will be
241  * created by the FilePrintManager object (optional, an empty string
242  * may be passed).
243  * @param window_icon A pixbuf which will comprise the window icon of
244  * the print dialog which will be created by the FilePrintManager
245  * object (optional, an empty GobjHandle object may be passed).
246  * @exception std::bad_alloc This method might throw std::bad_alloc if
247  * memory is exhausted and the system throws in that case.
248  * @exception Cgu::Thread::MutexError This method might throw
249  * Cgu::Thread::MutexError if initialisation of the contained mutex
250  * fails. (It is often not worth checking for this, as it means
251  * either memory is exhausted or pthread has run out of other
252  * resources to create new mutexes.)
253  * @exception Cgu::PipeError This method might throw Cgu::PipeError if
254  * the contained Notifier object is the first Notifier object in the
255  * program to be constructed and Cgu::Notifier::init() has not
256  * previously been called.
257  */
258  static Cgu::IntrusivePtr<Cgu::FilePrintManager> create_manager(GtkWindow* parent = 0,
259  const std::string& caption = "",
261 
262 /**
263  * Sets the filename of the file to be printed. This method is
264  * thread-safe and may be called in any thread. No GTK+/GDK functions
265  * are called.
266  * @param filename The filename of the file to be printed.
267  * @param manage_file If this argument is set to true, then if print()
268  * returns true, after printing succeeds or fails the file being
269  * printed will be deleted.
270  * @return Returns true, unless the same FilePrintManager object is
271  * being used to print more than one print job and when this method is
272  * called it is already printing another print job (in which case it
273  * returns false).
274  * @exception std::bad_alloc This method might throw std::bad_alloc if
275  * memory is exhausted and the system throws in that case.
276  */
277  bool set_filename(const char* filename, bool manage_file = false);
278 
279 /**
280  * Prints the file set with set_filename(). This method is
281  * thread-safe and may be called in any thread (it hands off its work
282  * to the main program thread via a Cgu::Notifier object), unless the
283  * program by which it is called calls GTK+ directly in more than one
284  * thread and thus employs gdk_threads_enter()/gdk_threads_leave()
285  * (rather than, say, Cgu::Notifier or Cgu::Callback::post()), in
286  * which case it must be called in the main GUI thread only and
287  * surrounded by gdk_threads_enter()/gdk_threads_leave() if called
288  * otherwise than in a GTK+ signal handler. (The best approach
289  * however is for a program only to address GTK+/GDK in the main
290  * program thread, for which purpose this library provides various
291  * functions and classes for inter-thread communication, such as
292  * Cgu::Notifier and Cgu::Callback::post().)
293  * @return Returns true, unless the same FilePrintManager object is
294  * being used to print more than one print job and when this method is
295  * called it is already printing another print job (in which case it
296  * returns false).
297  * @exception std::bad_alloc This method might throw std::bad_alloc if
298  * memory is exhausted and the system throws in that case, but only if
299  * it is called in the main program thread. Otherwise it will not
300  * throw.
301  */
302  bool print();
303 
304 /**
305  * The destructor will not throw. It is thread safe (the
306  * FilePrintManager object may be destroyed in any thread), and does
307  * not call any GTK+/GDK functions.
308  */
310 
311 #ifdef CGU_USE_GLIB_MEMORY_SLICES_NO_COMPAT
313 #endif
314 };
315 
316 #endif // CGU_USE_GTK
317 
318 } // namespace Cgu
319 
320 #endif // CGU_FILE_PRINTMANAGER_H
Cgu::FilePrintDialog::accepted
Emitter accepted
Definition: file_print_manager.h:90
Cgu::WinBase
This is a class for managing the lifetime of top level widgets.
Definition: window.h:225
Cgu
Definition: application.h:45
Cgu::GobjHandle< GtkPrintSettings >
Cgu::IntrusivePtr
This is a smart pointer for managing objects allocated on freestore which maintain their own referenc...
Definition: intrusive_ptr.h:106
Cgu::FilePrintManager
A class to print a file using the GTK+ print system.
Definition: file_print_manager.h:200
intrusive_ptr.h
Cgu::FilePrintManager::create_manager
static Cgu::IntrusivePtr< Cgu::FilePrintManager > create_manager(GtkWindow *parent=0, const std::string &caption="", GobjHandle< GdkPixbuf > window_icon=GobjHandle< GdkPixbuf >(0))
Cgu::Notifier
Provides thread-safe signalling between a worker thread and the main program thread.
Definition: notifier.h:181
gobj_handle.h
Cgu::FilePrintDialog
A print dialog class for FilePrintManager.
Definition: file_print_manager.h:65
Cgu::FilePrintManager::print
bool print()
Cgu::FilePrintDialog::get_settings
GobjHandle< GtkPrintSettings > get_settings() const
notifier.h
This file provides a Notifier class to provide thread-safe signalling between a worker thread and the...
CGU_GLIB_MEMORY_SLICES_FUNCS
#define CGU_GLIB_MEMORY_SLICES_FUNCS
Definition: cgu_config.h:84
Cgu::IntrusiveLockCounter
This is a counter class providing the ref() and unref() functions required by IntrusivePtr,...
Definition: intrusive_ptr.h:317
Cgu::FilePrintDialog::rejected
Emitter rejected
Definition: file_print_manager.h:99
Cgu::FilePrintManager::set_filename
bool set_filename(const char *filename, bool manage_file=false)
mutex.h
Provides wrapper classes for pthread mutexes and condition variables, and scoped locking classes for ...
window.h
Cgu::FilePrintDialog::FilePrintDialog
FilePrintDialog(GtkWindow *parent, GtkPrintSettings *print_settings=0, const char *caption=0, GdkPixbuf *window_icon=0)
Cgu::EmitterArg< void >
Cgu::FilePrintDialog::get_printer
GtkPrinter * get_printer() const
Cgu::FilePrintDialog::get_page_setup
GtkPageSetup * get_page_setup() const
emitter.h
This file provides a thread-safe signal/slot mechanism, with automatic disconnection.
Cgu::Thread::Mutex
A wrapper class for pthread mutexes.
Definition: mutex.h:109
Cgu::FilePrintManager::~FilePrintManager
~FilePrintManager()
cgu_config.h
Cgu::FilePrintDialog::on_delete_event
virtual void on_delete_event()