High-level Printing API
GtkPrintOperation is the high-level, portable printing API. It looks a bit
different than other GTK+ dialogs such as the <gtk-file-chooser>
, since
some platforms don't expose enough infrastructure to implement a good print
dialog. On such platforms, GtkPrintOperation uses the native print dialog. On
platforms which do not provide a native print dialog, GTK+ uses its own, see
<gtk-print-unix-dialog>
.
The typical way to use the high-level printing API is to create a
<gtk-print-operation>
object with gtk-print-operation-new
when the
user selects to print. Then you set some properties on it, e.g. the page size,
any <gtk-print-settings>
from previous print operations, the number of
pages, the current page, etc.
Then you start the print operation by calling gtk-print-operation-run
. 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-print-operation>
, the main one being ::draw-page, which you are
supposed to catch and render the page on the provided <gtk-print-context>
using Cairo.
static GtkPrintSettings *settings = NULL; static void do_print (void) { GtkPrintOperation *print; GtkPrintOperationResult res; print = gtk_print_operation_new (); if (settings != NULL) gtk_print_operation_set_print_settings (print, settings); g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), NULL); g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL); res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, GTK_WINDOW (main_window), NULL); if (res == GTK_PRINT_OPERATION_RESULT_APPLY) { if (settings != NULL) g_object_unref (settings); settings = g_object_ref (gtk_print_operation_get_print_settings (print)); } g_object_unref (print); }
By default GtkPrintOperation uses an external application to do print preview.
To implement a custom print preview, an application must connect to the preview
signal. The functions gtk-print-operation-print-preview-render-page
,
gtk-print-operation-preview-end-preview
and
gtk-print-operation-preview-is-selected
are useful when implementing a
print preview.
Printing support was added in GTK+ 2.10.
Derives from
<gtk-print-operation-preview>
,<gobject>
.This class defines the following slots:
default-page-setup
- The GtkPageSetup used by default
print-settings
- The GtkPrintSettings used for initializing the dialog
job-name
- A string used for identifying the print job.
n-pages
- The number of pages in the document.
current-page
- The current page in the document
use-full-page
- TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area
track-print-status
- TRUE if the print operation will continue to report on the print job status after the print data has been sent to the printer or print server.
unit
- The unit in which distances can be measured in the context
show-progress
- TRUE if a progress dialog is shown while printing.
allow-async
- TRUE if print process may run asynchronous.
export-filename
- Export filename
status
- The status of the print operation
status-string
- A human-readable description of the status
custom-tab-label
- Label for the tab containing custom widgets.
<gtk-print-operation-result>
)Emitted when the print operation run has finished doing everything required for printing. result gives you information about what happened during the run. If result is ‘GTK_PRINT_OPERATION_RESULT_ERROR’ then you can call
gtk-print-operation-get-error
for more information.If you enabled print status tracking then
gtk-print-operation-is-finished
may still return ‘#f
’ after this was emitted.Since 2.10
<gtk-print-context>
)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-print-context>
and paginate the document accordingly, and then set the number of pages withgtk-print-operation-set-n-pages
.Since 2.10
<gtk-print-context>
) ⇒ <gboolean>
Emitted after the begin-print signal, but before the actual rendering starts. It keeps getting emitted until it returns ‘
#f
’.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
gtk-print-operation-set-n-pages
, and return ‘#t
’ 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.
Since 2.10
<gtk-print-context>
) (arg1 <gint>
) (arg2 <gtk-page-setup>
)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.
Since 2.10
<gtk-print-context>
) (arg1 <gint>
)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-print-context-get-cairo-context
.static void draw_page (GtkPrintOperation *operation, GtkPrintContext *context, gint page_nr, gpointer user_data) { cairo_t *cr; PangoLayout *layout; gdouble width, text_height; gint layout_height; PangoFontDescription *desc; cr = gtk_print_context_get_cairo_context (context); width = gtk_print_context_get_width (context); cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT); cairo_set_source_rgb (cr, 0.8, 0.8, 0.8); cairo_fill (cr); layout = gtk_print_context_create_pango_layout (context); desc = pango_font_description_from_string ("sans 14"); pango_layout_set_font_description (layout, desc); pango_font_description_free (desc); pango_layout_set_text (layout, "some text", -1); pango_layout_set_width (layout, width); pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER); pango_layout_get_size (layout, NULL, &layout_height); text_height = (gdouble)layout_height / PANGO_SCALE; cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2); pango_cairo_show_layout (cr, layout); g_object_unref (layout); }Use
gtk-print-operation-set-use-full-page
andgtk-print-operation-set-unit
before starting the print operation to set up the transformation of the cairo context according to your needs.Since 2.10
<gtk-print-context>
)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.
Since 2.10
Emitted at between the various phases of the print operation. See
<gtk-print-status>
for the phases that are being discriminated. Usegtk-print-operation-get-status
to find out the current status.Since 2.10
<gobject>
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.
Since 2.10
<gtk-widget>
)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.
Since 2.10
<gtk-print-operation-preview>
) (arg1 <gtk-print-context>
) (arg2 <gtk-window>
) ⇒ <gboolean>
Gets emitted when a preview is requested from the native dialog.
The default handler for this signal uses an external viewer application to preview.
To implement a custom print preview, an application must return ‘
#t
’ from its handler for this signal. In order to use the provided context for the preview implementation, it must be given a suitable cairo context withgtk-print-context-set-cairo-context
.The custom preview implementation can use
gtk-print-operation-preview-is-selected
andgtk-print-operation-preview-render-page
to find pages which are selected for print and render them. The preview must be finished by callinggtk-print-operation-preview-end-preview
(typically in response to the user clicking a close button).Since 2.10
Derives from
<ginterface>
.This class defines no direct slots.
<gtk-print-context>
) (arg1 <gtk-page-setup>
)undocumented
<gtk-print-operation>
)Creates a new
<gtk-print-operation>
.
- ret
- a new
<gtk-print-operation>
Since 2.10
<gtk-print-operation>
) (allow_async bool
)Sets whether the
gtk-print-operation-run
may return before the print operation is completed. Note that some platforms may not allow asynchronous operation.
- op
- a
<gtk-print-operation>
- allow-async
- ‘
#t
’ to allow asynchronous operationSince 2.10
<gtk-print-operation>
)Call this when the result of a print operation is ‘GTK_PRINT_OPERATION_RESULT_ERROR’, either as returned by
gtk-print-operation-run
, or in the ::done signal handler. The returned<g-error>
will contain more details on what went wrong.
- op
- a
<gtk-print-operation>
- error
- return location for the error
Since 2.10
<gtk-print-operation>
) (job_name mchars
)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.
- op
- a
<gtk-print-operation>
- job-name
- a string that identifies the print job
Since 2.10
<gtk-print-operation>
) (n_pages int
)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.
- op
- a
<gtk-print-operation>
- n-pages
- the number of pages
Since 2.10
<gtk-print-operation>
) (unit <gtk-unit>
)Sets up the transformation for the cairo context obtained from
<gtk-print-context>
in such a way that distances are measured in units of unit.
- op
- a
<gtk-print-operation>
- unit
- the unit to use
Since 2.10
<gtk-print-operation>
) (action <gtk-print-operation-action>
) (parent <gtk-window>
) ⇒ (ret <gtk-print-operation-result>
)Runs the print operation, by first letting the user modify print settings in the print dialog, and then print the document.
Normally that this function does not return until the rendering of all pages is complete. You can connect to the ::status-changed signal on op 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
gtk-print-operation-set-allow-async
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 != NULL) gtk_print_operation_set_print_settings (print, settings); if (page_setup != NULL) gtk_print_operation_set_default_page_setup (print, page_setup); g_signal_connect (print, "begin-print", G_CALLBACK (begin_print), &data); g_signal_connect (print, "draw-page", G_CALLBACK (draw_page), &data); res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, parent, &error); if (res == GTK_PRINT_OPERATION_RESULT_ERROR) { error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent), GTK_DIALOG_DESTROY_WITH_PARENT, GTK_MESSAGE_ERROR, GTK_BUTTONS_CLOSE, "Error printing file:\n%s", error->message); g_signal_connect (error_dialog, "response", G_CALLBACK (gtk_widget_destroy), NULL); gtk_widget_show (error_dialog); g_error_free (error); } else if (res == GTK_PRINT_OPERATION_RESULT_APPLY) { if (settings != NULL) g_object_unref (settings); settings = g_object_ref (gtk_print_operation_get_print_settings (print)); }Note that
gtk-print-operation-run
can only be called once on a given<gtk-print-operation>
.
- op
- a
<gtk-print-operation>
- action
- the action to start
- parent
- Transient parent of the dialog, or ‘
#f
’- error
- Return location for errors, or ‘
#f
’- ret
- the result of the print operation. 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
gtk-print-operation-get-print-settings
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.Since 2.10
<gtk-print-operation>
)Cancels a running print operation. This function may be called from a begin-print, paginate or draw-page signal handler to stop the currently running print operation.
- op
- a
<gtk-print-operation>
Since 2.10
<gtk-print-operation>
) ⇒ (ret <gtk-print-status>
)Returns the status of the print operation. Also see
gtk-print-operation-get-status-string
.
- op
- a
<gtk-print-operation>
- ret
- the status of the print operation
Since 2.10
<gtk-print-operation>
) ⇒ (ret bool
)A convenience function to find out if the print operation is finished, either successfully (‘GTK_PRINT_STATUS_FINISHED’) or unsuccessfully (‘GTK_PRINT_STATUS_FINISHED_ABORTED’).
Note: 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.
- op
- a
<gtk-print-operation>
- ret
- ‘
#t
’, if the print operation is finished.Since 2.10
<gtk-print-operation>
)Call this when the result of a print operation is ‘GTK_PRINT_OPERATION_RESULT_ERROR’, either as returned by
gtk-print-operation-run
, or in the ::done signal handler. The returned<g-error>
will contain more details on what went wrong.
- op
- a
<gtk-print-operation>
- error
- return location for the error
Since 2.10
<gtk-window>
) (page_setup <gtk-page-setup>
) (settings <gtk-print-settings>
) ⇒ (ret <gtk-page-setup>
)Runs a page setup dialog, letting the user modify the values from page-setup. If the user cancels the dialog, the returned
<gtk-page-setup>
is identical to the passed in page-setup, otherwise it contains the modifications done in the dialog.Note that this function may use a recursive mainloop to show the page setup dialog. See
gtk-print-run-page-setup-dialog-async
if this is a problem.
- parent
- transient parent, or ‘
#f
’- page-setup
- an existing
<gtk-page-setup>
, or ‘#f
’- settings
- a
<gtk-print-settings>
- ret
- a new
<gtk-page-setup>
Since 2.10