gtk.PrintOperation — a high-level printing API (new in PyGTK 2.10)
class gtk.PrintOperation( |
|
gtk.PrintOperationPreview Signal Prototypes
def callback( | |
def callback( | |
def callback( | |
"done" | def callback( |
def callback( | |
def callback( | |
"paginate" | def callback( |
"preview" | def callback( |
def callback( | |
def callback( |
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 gtkunixprint.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_op = gtk.PrintOperation() if settings != None: print_op.set_print_settings(settings) print_op.connect("begin_print", begin_print) print_op.connect("draw_page", draw_page) res = print_op.run(gtk.PRINT_OPERATION_ACTION_PRINT_DIALOG, main_window) if res == gtk.PRINT_OPERATION_RESULT_APPLY: settings = print_op.get_print_settings()
Printing support was added in GTK+ 2.10.
gtk.PrintOperation()
Returns : | a new gtk.PrintOperation |
This constructor is available in PyGTK 2.10 and above.
Creates a new gtk.PrintOperation
.
def set_default_page_setup(default_page_setup
=None)
| 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.
def get_default_page_setup()
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.
def set_print_settings(print_settings
=None)
| 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.
def get_print_settings()
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.
def set_job_name(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.
def set_n_pages(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.
def set_current_page(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.
def set_use_full_page(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).
def set_unit(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
.
def set_export_filename(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.
def set_track_print_status(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.
def set_show_progress(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.
def set_allow_async(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.
def set_custom_tab_label(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.
def run(action
, parent
=None)
| the action to start - one of the GTK Print Operation Action Constants |
| 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()
def get_error()
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.
def get_status()
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.
def get_status_string()
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.
def is_finished()
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.
def cancel()
This method is available in PyGTK 2.10 and above.
The cancel
() method cancels a running
print operation. This method may be called from a "begin-print",
"paginate" or "draw-page" signal handler to stop the currently running
print operation.
def set_support_selection(support_selection
)
| True to support selection. |
This method is available in PyGTK 2.22 and above.
The set_support_selection
() method sets whether "print selection" is supported by the print operation.
def get_support_selection()
Returns : | whether the application supports print of selection. |
This method is available in PyGTK 2.22 and above.
The get_support_selection
() method gets whether "print selection" is supported by the print operation.
def set_has_selection(has_selection
)
| True indicates that a selection exists. |
This method is available in PyGTK 2.22 and above.
The set_has_selection
() method sets whether there is a selection to print.
def get_has_selection()
Returns : | True when a selection exists. |
This method is available in PyGTK 2.22 and above.
The get_has_selection
() method gets whether there is a selection to print.
def set_embed_page_setup(embed
)
| True to embed page setup selection in the GtkPrintDialog. |
This method is available in PyGTK 2.22 and above.
The set_embed_page_setup
() method embeds the page size combo box and orientation combo box
into the page setup page. Selected page setup is stored as default page setup in the print operation.
def get_embed_page_setup()
Returns : | True when page setup selection combos are embedded. |
This method is available in PyGTK 2.22 and above.
The get_embed_page_setup
() method XXX.
def get_n_pages_to_print()
Returns : | the number of pages that will be printed. |
This method is available in PyGTK 2.22 and above.
The get_n_pages_to_print
() method returns the number of pages that will be printed.
Note that this value is set during print preparation phase (gtk.PRINT_STATUS_PREPARING)
, so this
function should never be called before the data generation phase (gtk.PRINT_STATUS_GENERATING_DATA
). You
can connect to the "status-changed" signal and call get_n_pages_to_print
() when print status is
gtk.PRINT_STATUS_GENERATING_DATA
. This is typically used to track the progress of print operation.
def draw_page_finish()
This method is available in PyGTK 2.16 and above.
The draw_page_finish
() method signals that the drawing of a particular page is complete.
It is called after completion of page drawing (e.g. drawing in another thread). If
set_defer_drawing
()
was called before, then this function has to be called by application. In another case it is called by the library itself.
def set_defer_drawing()
This method is available in PyGTK 2.16 and above.
The set_defer_drawing
() method sets up the print operation to wait for calling of
draw_page_finish
()
from application. It can be used for drawing page in another thread.
This function must be called in the callback of "draw-page" signal. .
def callback(operation
, context
, user_param1
, ...
)
| the gtk.PrintOperation
on which the signal was emitted |
| the gtk.PrintContext
for the current operation |
| the first user parameter (if any) specified
with the connect () |
| 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()
.
def callback(operation
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, widget
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the custom widget added in create-custom-widget |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, result
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the result of the print operation |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, context
, page_nr
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the gtk.PrintContext for the current operation |
| the number of the currently printed page |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, context
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the gtk.PrintContext for the current operation |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(printoperation
, context
, user_param1
, ...
)
| the object which received the signal. |
| the gtk.PrintContext
for the current operation |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, preview
, context
, parent
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the GtkPrintPreviewOperation for the current operation |
| the gtk.PrintContext that will be used |
| the gtk.Window to use as window parent, or None |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, context
, page_nr
, setup
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the gtk.PrintContext for the current operation |
| the number of the currently printed page |
| the gtk.PageSetup |
| the first user parameter (if any) specified
with the connect () |
| 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.
def callback(operation
, printoperation
, user_param1
, ...
)
| the gtk.PrintOperation on which the signal was emitted |
| the object which received the signal. |
| the first user parameter (if any) specified
with the connect () |
| 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.