gtk.PrintOperation — a high-level printing API (new in PyGTK 2.10)
class gtk.PrintOperation(gobject.GObject, gtk.PrintOperationPreview): |
|
gobject.GObject Signal Prototypes
gtk.PrintOperationPreview Signal Prototypes
"begin-print | def callback(operation, context, user_param1, ...) |
"create-custom-widget | def callback(operation, user_param1, ...) |
"custom-widget-apply | def callback(operation, widget, user_param1, ...) |
"done | def callback(operation, result, user_param1, ...) |
"draw-page | def callback(operation, context, page_nr, user_param1, ...) |
"end-print | def callback(operation, context, user_param1, ...) |
"paginate | def callback(printoperation, context, user_param1, ...) |
"preview | def callback(operation, preview, context, parent, user_param1, ...) |
"request-page-setup | def callback(operation, context, page_nr, setup, user_param1, ...) |
"status-changed | def callback(operation, printoperation, user_param1, ...) |
gtk.PrintOperation is the high-level, portable printing API. It looks a bit different than other GTK+ dialogs such as the gtk.FileChooser, since some platforms don't expose enough infrastructure to implement a good print dialog. On such platforms, gtk.PrintOperation uses the native print dialog. On platforms which do not provide a native print dialog, GTK+ uses its own, see gtk.unixprint.PrintUnixDialog.
The typical way to use the high-level printing API is to create a gtk.PrintOperation object with the gtk.PrintOperation constructor when the user selects to print. Then you set some properties on it, e.g. the page size, any gtk.PrintSettings from previous print operations, the number of pages, the current page, etc.
Then you start the print operation by calling the gtk.PrintOperation.run() method. It will then show a dialog, let the user select a printer and options. When the user finished the dialog various signals will be emitted on the gtk.PrintOperation, the main one being "draw-page", which you are supposed to catch and render the page on the provided gtk.PrintContext using Cairo.
Example 3. The high-level printing API
settings = None def do_print(): print = gtk.PrintOperation() if settings != None: print.set_print_settings(settings) print.connect("begin_print", begin_print) print.connect("draw_page", draw_page) res = print.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, main_window) if res == gtk.PRINT_OPERATION_RESULT_APPLY: settings = print.get_print_settings() |
Printing support was added in GTK+ 2.10.
|
Returns : | a new gtk.PrintOperation |
This constructor is available in PyGTK 2.10 and above.
Creates a new gtk.PrintOperation.
|
default_page_setup : | a gtk.PageSetup, or None |
This method is available in PyGTK 2.10 and above.
The set_default_page_setup() method makes default_page_setup the default page setup.
This page setup will be used by the gtk.PrintOperation.run() method, but it can be overridden on a per-page basis by connecting to the "request-page-setup" signal.
|
Returns : | the default page setup |
This method is available in PyGTK 2.10 and above.
The get_default_page_setup() method returns the default page setup, see the gtk.PrintOperation.set_default_page_setup() method.
|
print_settings : | gtk.PrintSettings, or None |
This method is available in PyGTK 2.10 and above.
The set_print_settings() method sets the print settings. This is typically used to re-establish print settings from a previous print operation, see the gtk.PrintOperation.run() method.
|
Returns : | the current print settings. |
This method is available in PyGTK 2.10 and above.
The get_print_settings() method returns the current print settings.
Note that the return value is None until either the gtk.PrintOperation.set_print_settings() or gtk.PrintOperation.run() methods have been called.
|
job_name : | a string that identifies the print job |
This method is available in PyGTK 2.10 and above.
The set_job_name() method sets the name of the print job. The name is used to identify the job (e.g. in monitoring applications like eggcups).
If you don't set a job name, GTK+ picks a default one by numbering successive print jobs.
|
n_pages : | the number of pages |
This method is available in PyGTK 2.10 and above.
The set_n_pages() >method sets the number of pages in the document.
This must be set to a positive number before the rendering starts. It may be set in a "begin-print" signal hander.
Note that the page numbers passed to the "request-page-setup" and "draw-page" signals are 0-based, i.e. if the user chooses to print all pages, the last "draw-page" signal will be for page n_pages - 1.
|
current_page : | the current page, 0-based |
This method is available in PyGTK 2.10 and above.
The set_current_page() method sets the current page.
If this is called before gtk.PrintOperation.run(), the user will be able to select to print only the current page.
Note that this only makes sense for pre-paginated documents.
|
full_page : | TRUE to set up the gtk.PrintContext for the full page |
This method is available in PyGTK 2.10 and above.
The set_use_full_page() method sets the "full-page" property to the value of full_page. If full_page is TRUE, the transformation for the cairo context obtained from gtk.PrintContext puts the origin at the top left corner of the page (which may not be the top left corner of the sheet, depending on page orientation and the number of pages per sheet). Otherwise, the origin is at the top left corner of the imageable area (i.e. inside the margins).
|
unit : | the unit to use |
This method is available in PyGTK 2.10 and above.
The set_unit() method sets up the transformation for the cairo context such distances are measured in units of unit.
|
filename : | the filename for the exported file |
This method is available in PyGTK 2.10 and above.
The set_export_filename() method sets up the gtk.PrintOperation to generate a file instead of showing the print dialog. The intended use of this method is for implementing "Export to PDF" actions. Currently, PDF is the only supported format.
"Print to PDF" support is independent of this and is done by letting the user pick the "Print to PDF" item from the list of printers in the print dialog.
|
track_status : | if TRUE track status after printing |
This method is available in PyGTK 2.10 and above.
The set_track_print_status() method sets the "track-print-status" to the value of track_status. If track_status is TRUE, the print operation will try to continue report on the status of the print job in the printer queues and printer. This can allow your application to show things like "out of paper" issues, and when the print job actually reaches the printer.
This method is often implemented using some form of polling, so it should not be enabled unless needed.
|
show_progress : | if TRUE show a progress dialog |
This method is available in PyGTK 2.10 and above.
The set_show_progress() method sets the "show-progress" property to the value of show_progress. If show_progress is TRUE, the print operation will show a progress dialog during the print operation.
|
allow_async : | if TRUE allow asynchronous operation |
This method is available in PyGTK 2.10 and above.
The set_allow_async() method sets the "allow-async" to the value of allow_async. If allow_async is TRUE the gtk.PrintOperation.run() may return before the print operation is completed. Note that some platforms may not allow asynchronous operation.
|
label : |
This method is available in PyGTK 2.10 and above.
The set_custom_tab_label() method sets the label for the tab holding custom widgets.
|
action : | the action to start - one of the GTK Print Operation Action Constants |
parent : | Transient parent of the dialog, or None |
Returns : | the result of the print operation - one of the GTK Print Operation Result Constants. A return value of gtk.PRINT_OPERATION_RESULT_APPLY indicates that the printing was completed successfully. In this case, it is a good idea to obtain the used print settings with the gtk.PrintOperation.get_print_settings() method and store them for reuse with the next print operation. A value of gtk.PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running asynchronously, and will emit the "done" signal when done. |
This method is available in PyGTK 2.10 and above.
The run() method runs the print operation, by first letting the user modify print settings in the print dialog, and then print the document.
Normally that this method does not return until the rendering of all pages is complete. You can connect to the "status-changed" signal to obtain some information about the progress of the print operation. Furthermore, it may use a recursive mainloop to show the print dialog.
If you call the gtk.PrintOperation.set_allow_async() method or set the "allow-async" property the operation will run asyncronously if this is supported on the platform. The "done" signal will be emitted with the operation results when the operation is done (i.e. when the dialog is canceled, or when the print succeeds or fails).
if settings != None: print.set_print_settings( settings) if page_setup != None: print.set_default_page_setup(page_setup) print.connect( "begin-print", begin_print, data) print.connect("draw-page", draw_page, data) res = print.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, parent) if res == gtkprint_OPERATION_RESULT_ERROR: error_dialog = gtk.MessageDialog(parent, gtk.DIALOG_DESTROY_WITH_PARENT, gtk.MESSAGE_ERROR, gtk.BUTTONS_CLOSE, "Error printing file:\n") error_dialog.connect("response", lambda w,id: w.destroy()) error_dialog.show() elif res == gtk.PRINT_OPERATION_RESULT_APPLY: settings = print.get_print_settings() |
|
Returns : | the error message or None |
This method is available in PyGTK 2.10 and above.
The get_error() method returns the error message or None. Call this when the result of a print operation is gtk.PRINT_OPERATION_RESULT_ERROR, either as returned by the gtk.PrintOperation.run() method, or in the "done" signal handler.
|
Returns : | the status of the print operation |
This method is available in PyGTK 2.10 and above.
The get_status() method returns the status of the print operation as one of the GTK Print Status Constants. Also see the gtk.PrintOperation.get_status_string() method.
|
Returns : | a string representation of the status of the print operation |
This method is available in PyGTK 2.10 and above.
The get_status_string() method returns a string representation of the status of the print operation. The string is translated and suitable for displaying the print status e.g. in a gtk.Statusbar.
Use the gtk.PrintOperation.get_status() method to obtain a status value that is suitable for programmatic use.
|
Returns : | TRUE, if the print operation is finished. |
This method is available in PyGTK 2.10 and above.
The method returns TRUE if the print operation is completed. This is a convenience method to find out if the print operation is finished, either successfully (gtk.PRINT_STATUS_FINISHED) or unsuccessfully (gtk.PRINT_STATUS_FINISHED_ABORTED).
When you enable print status tracking, the print operation can be in a non-finished state even after done has been called, as the operation status then tracks the print job status on the printer.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
context : | the gtk.PrintContext for the current operation |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "begin-print" signal is emitted after the user has finished changing print settings in the dialog, before the actual rendering starts.
A typical use for this signal is to use the parameters from the gtk.PrintContext and paginate the document accordingly, and then set the number of pages with gtk.PrintOperation.set_n_pages().
|
operation : | the gtk.PrintOperation on which the signal was emitted |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
Returns : | A custom widget that gets embedded in the print dialog, or None |
This signal is available in GTK+ 2.10 and above.
The "create-custom-widget" signal is emitted when displaying the print dialog. If you return a widget in a handler for this signal it will be added to a custom tab in the print dialog. You typically return a container widget with multiple widgets in it.
The print dialog owns the returned widget, and its lifetime isn't controlled by the app. However, the widget is guaranteed to stay around until the "custom-widget-apply" signal is emitted on the operation. Then you can read out any information you need from the widgets.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
widget : | the custom widget added in create-custom-widget |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "custom-widget-apply" signal is emitted right before "begin-print" if you added a custom widget in the "create-custom-widget" handler. When you get this signal you should read the information from the custom widgets, as the widgets are not guaraneed to be around at a later time.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
result : | the result of the print operation |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "done" signal is emitted when the print operation run has finished doing everything required for printing. result (one of the GTK Print Operation Result Constants) gives you information about what happened during the run. If result is gtk.PRINT_OPERATION_RESULT_ERROR then you can call the gtk.PrintOperation.get_error() method for more information.
If you enabled print status tracking then the gtk.PrintOperation.is_finished() method may still return FALSE after this was emitted.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
context : | the gtk.PrintContext for the current operation |
page_nr : | the number of the currently printed page |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "draw-page" signal is emitted for every page that is printed. The signal handler must render the page_nr's page onto the cairo context obtained from context using gtk.PrintContext.get_cairo_context().
def draw_page(operation, context, page_nr, user_data): cr = context.get_cairo_context() width = context.get_width() cr.rectangle(0, 0, width, HEADER_HEIGHT) cr.set_source_rgb(0.8, 0.8, 0.8); cr.fill() layout = context.create_pango_layout() desc = pango.FontDescription("sans 14") layout.set_font_description(desc) layout.set_text("some text") layout.set_width(width) layout.set_alignment(pango.ALIGN_CENTER) x,layout_height = layout.get_size() text_height = layout_height / pango.SCALE cr.move_to(width / 2, (HEADER_HEIGHT - text_height) / 2) cr.show_layout(layout) |
Use the gtk.PrintOperation.set_use_full_page() and gtk.PrintOperation.set_unit() methods before starting the print operation to set up the transformation of the cairo context according to your needs.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
context : | the gtk.PrintContext for the current operation |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "end-print" signal is emitted after all pages have been rendered. A handler for this signal can clean up any resources that have been allocated in the "begin-print" handler.
|
printoperation : | the object which received the signal. |
context : | the gtk.PrintContext for the current operation |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
Returns : |
This signal is available in GTK+ 2.10 and above.
The "paginate" signal is emitted after the "begin-print" signal, but before the actual rendering starts. It keeps getting emitted until it returns FALSE.
This signal is intended to be used for paginating the document in small chunks, to avoid blocking the user interface for a long time. The signal handler should update the number of pages using the gtk.PrintOperation.set_n_pages() method, and return TRUE if the document has been completely paginated.
If you don't need to do pagination in chunks, you can simply do it all in the "begin-print" handler, and set the number of pages from there.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
preview : | the GtkPrintPreviewOperation for the current operation |
context : | the gtk.PrintContext that will be used |
parent : | the gtk.Window to use as window parent, or None |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
Returns : | TRUE if the listener wants to take over control of the preview |
This signal is available in GTK+ 2.10 and above.
The "preview" signal is emitted when a preview is requested from the native dialog. If you handle this you must set the cairo context on the printing context.
If you don't override this, a default implementation using an external viewer will be used.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
context : | the gtk.PrintContext for the current operation |
page_nr : | the number of the currently printed page |
setup : | the gtk.PageSetup |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "request-page-setup" signal is emitted once for every page that is printed, to give the application a chance to modify the page setup. Any changes done to setup will be in force only for printing this page.
|
operation : | the gtk.PrintOperation on which the signal was emitted |
printoperation : | the object which received the signal. |
user_param1 : | the first user parameter (if any) specified with the connect() method |
... : | additional user parameters (if any) |
This signal is available in GTK+ 2.10 and above.
The "status-changed" signal is emitted at between the various phases of the print operation. See the GTK Print Status Constants for the phases that are being discriminated. Use the gtk.PrintOperation.get_status() method to find out the current status.