Description
GtkPrintOperation is the high-level, portable printing API.
It looks a bit different than other GTK+ dialogs such as the
GtkFileChooser, 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 GtkPrintUnixDialog.
The typical way to use the high-level printing API is to create
a GtkPrintOperation 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 GtkPrintSettings 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 GtkPrintOperation, the main one being
“draw-page”, which you are supposed to catch
and render the page on the provided GtkPrintContext using Cairo.
Functions
gtk_print_operation_set_allow_async ()
void
gtk_print_operation_set_allow_async (GtkPrintOperation *op,
gboolean allow_async);
Sets whether the gtk_print_operation_run() may return
before the print operation is completed. Note that
some platforms may not allow asynchronous operation.
Since: 2.10
gtk_print_operation_set_default_page_setup ()
void
gtk_print_operation_set_default_page_setup
(GtkPrintOperation *op,
GtkPageSetup *default_page_setup);
Makes default_page_setup
the default page setup for op
.
This page setup will be used by gtk_print_operation_run(),
but it can be overridden on a per-page basis by connecting
to the “request-page-setup” signal.
Since: 2.10
gtk_print_operation_set_print_settings ()
void
gtk_print_operation_set_print_settings
(GtkPrintOperation *op,
GtkPrintSettings *print_settings);
Sets the print settings for op
. This is typically used to
re-establish print settings from a previous print operation,
see gtk_print_operation_run().
Since: 2.10
gtk_print_operation_set_job_name ()
void
gtk_print_operation_set_job_name (GtkPrintOperation *op,
const gchar *job_name);
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.
Since: 2.10
gtk_print_operation_set_n_pages ()
void
gtk_print_operation_set_n_pages (GtkPrintOperation *op,
gint n_pages);
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.
Since: 2.10
gtk_print_operation_set_current_page ()
void
gtk_print_operation_set_current_page (GtkPrintOperation *op,
gint current_page);
Sets the current page.
If this is called before gtk_print_operation_run(),
the user will be able to select to print only the current page.
Note that this only makes sense for pre-paginated documents.
Since: 2.10
gtk_print_operation_set_use_full_page ()
void
gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
gboolean full_page);
If full_page
is TRUE, the transformation for the cairo context
obtained from GtkPrintContext 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).
Since: 2.10
gtk_print_operation_set_unit ()
void
gtk_print_operation_set_unit (GtkPrintOperation *op,
GtkUnit unit);
Sets up the transformation for the cairo context obtained from
GtkPrintContext in such a way that distances are measured in
units of unit
.
Since: 2.10
gtk_print_operation_set_export_filename ()
void
gtk_print_operation_set_export_filename
(GtkPrintOperation *op,
const gchar *filename);
Sets up the GtkPrintOperation to generate a file instead
of showing the print dialog. The indended use of this function
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.
Since: 2.10
gtk_print_operation_set_show_progress ()
void
gtk_print_operation_set_show_progress (GtkPrintOperation *op,
gboolean show_progress);
If show_progress
is TRUE, the print operation will show a
progress dialog during the print operation.
Since: 2.10
gtk_print_operation_set_track_print_status ()
void
gtk_print_operation_set_track_print_status
(GtkPrintOperation *op,
gboolean 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 function is often implemented using some form of polling, so it should
not be enabled unless needed.
Since: 2.10
gtk_print_operation_set_custom_tab_label ()
void
gtk_print_operation_set_custom_tab_label
(GtkPrintOperation *op,
const gchar *label);
Sets the label for the tab holding custom widgets.
Since: 2.10
gtk_print_operation_run ()
GtkPrintOperationResult
gtk_print_operation_run (GtkPrintOperation *op,
GtkPrintOperationAction action,
GtkWindow *parent,
GError **error);
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
asynchronously if this is supported on the platform. The
“done” signal will be emitted with the result of the
operation when the it is done (i.e. when the dialog is canceled, or when
the print succeeds or fails).
Note that gtk_print_operation_run() can only be called once on a
given GtkPrintOperation.
Since: 2.10
gtk_print_operation_cancel ()
void
gtk_print_operation_cancel (GtkPrintOperation *op);
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.
Since: 2.10
gtk_print_operation_draw_page_finish ()
void
gtk_print_operation_draw_page_finish (GtkPrintOperation *op);
Signalize that drawing of particular page is complete.
It is called after completion of page drawing (e.g. drawing in another
thread).
If gtk_print_operation_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.
Since: 2.16
gtk_print_operation_get_status_string ()
const gchar *
gtk_print_operation_get_status_string (GtkPrintOperation *op);
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 GtkStatusbar.
Use gtk_print_operation_get_status() to obtain a status
value that is suitable for programmatic use.
Returns
a string representation of the status
of the print operation
Since: 2.10
gtk_print_operation_is_finished ()
gboolean
gtk_print_operation_is_finished (GtkPrintOperation *op);
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.
Returns
TRUE, if the print operation is finished.
Since: 2.10
gtk_print_operation_set_support_selection ()
void
gtk_print_operation_set_support_selection
(GtkPrintOperation *op,
gboolean support_selection);
Sets whether selection is supported by GtkPrintOperation.
Since: 2.18
gtk_print_operation_get_support_selection ()
gboolean
gtk_print_operation_get_support_selection
(GtkPrintOperation *op);
Gets the value of “support-selection” property.
Returns
whether the application supports print of selection
Since: 2.18
gtk_print_operation_get_has_selection ()
gboolean
gtk_print_operation_get_has_selection (GtkPrintOperation *op);
Gets the value of “has-selection” property.
Returns
whether there is a selection
Since: 2.18
gtk_print_operation_set_embed_page_setup ()
void
gtk_print_operation_set_embed_page_setup
(GtkPrintOperation *op,
gboolean embed);
Embed page size combo box and orientation combo box into page setup page.
Selected page setup is stored as default page setup in GtkPrintOperation.
Since: 2.18
gtk_print_operation_get_embed_page_setup ()
gboolean
gtk_print_operation_get_embed_page_setup
(GtkPrintOperation *op);
Gets the value of “embed-page-setup” property.
Returns
whether page setup selection combos are embedded
Since: 2.18
gtk_print_run_page_setup_dialog ()
GtkPageSetup *
gtk_print_run_page_setup_dialog (GtkWindow *parent,
GtkPageSetup *page_setup,
GtkPrintSettings *settings);
Runs a page setup dialog, letting the user modify the values from
page_setup
. If the user cancels the dialog, the returned GtkPageSetup
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.
Since: 2.10
gtk_print_run_page_setup_dialog_async ()
void
gtk_print_run_page_setup_dialog_async (GtkWindow *parent,
GtkPageSetup *page_setup,
GtkPrintSettings *settings,
GtkPageSetupDoneFunc done_cb,
gpointer data);
Runs a page setup dialog, letting the user modify the values from page_setup
.
In contrast to gtk_print_run_page_setup_dialog(), this function returns after
showing the page setup dialog on platforms that support this, and calls done_cb
from a signal handler for the ::response signal of the dialog.
Since: 2.10
gtk_print_operation_preview_end_preview ()
void
gtk_print_operation_preview_end_preview
(GtkPrintOperationPreview *preview);
Ends a preview.
This function must be called to finish a custom print preview.
Since: 2.10
gtk_print_operation_preview_is_selected ()
gboolean
gtk_print_operation_preview_is_selected
(GtkPrintOperationPreview *preview,
gint page_nr);
Returns whether the given page is included in the set of pages that
have been selected for printing.
Returns
TRUE if the page has been selected for printing
Since: 2.10
gtk_print_operation_preview_render_page ()
void
gtk_print_operation_preview_render_page
(GtkPrintOperationPreview *preview,
gint page_nr);
Renders a page to the preview, using the print context that
was passed to the “preview” handler together
with preview
.
A custom iprint preview should use this function in its ::expose
handler to render the currently selected page.
Note that this function requires a suitable cairo context to
be associated with the print context.
Since: 2.10
Property Details
The “allow-async” property
“allow-async” gboolean
Determines whether the print operation may run asynchronously or not.
Some systems don't support asynchronous printing, but those that do
will return GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
emit the “done” signal when the operation is actually
done.
The Windows port does not support asynchronous operation at all (this
is unlikely to change). On other platforms, all actions except for
GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.10
The “current-page” property
“current-page” gint
The current page in the document.
If this is set before gtk_print_operation_run(),
the user will be able to select to print only the current page.
Note that this only makes sense for pre-paginated documents.
Owner: GtkPrintOperation
Flags: Read / Write
Allowed values: >= -1
Default value: -1
Since: 2.10
The “custom-tab-label” property
“custom-tab-label” gchar *
Used as the label of the tab containing custom widgets.
Note that this property may be ignored on some platforms.
If this is NULL, GTK+ uses a default label.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: NULL
Since: 2.10
The “embed-page-setup” property
“embed-page-setup” gboolean
If TRUE, page size combo box and orientation combo box are embedded into page setup page.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.18
The “export-filename” property
“export-filename” gchar *
The name of a file to generate instead of showing the print dialog.
Currently, PDF is the only supported format.
The intended use of this property is for implementing
“Export to PDF” actions.
“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.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: NULL
Since: 2.10
The “has-selection” property
“has-selection” gboolean
Determines whether there is a selection in your application.
This can allow your application to print the selection.
This is typically used to make a "Selection" button sensitive.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.18
The “job-name” property
“job-name” gchar *
A string 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.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: ""
Since: 2.10
The “n-pages” property
“n-pages” gint
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.
Owner: GtkPrintOperation
Flags: Read / Write
Allowed values: >= -1
Default value: -1
Since: 2.10
The “print-settings” property
“print-settings” GtkPrintSettings *
The GtkPrintSettings used for initializing the dialog.
Setting this property is typically used to re-establish
print settings from a previous print operation, see
gtk_print_operation_run().
Owner: GtkPrintOperation
Flags: Read / Write
Since: 2.10
The “show-progress” property
“show-progress” gboolean
Determines whether to show a progress dialog during the
print operation.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.10
The “status” property
“status” GtkPrintStatus
The status of the print operation.
Owner: GtkPrintOperation
Flags: Read
Default value: GTK_PRINT_STATUS_INITIAL
Since: 2.10
The “status-string” property
“status-string” gchar *
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 GtkStatusbar.
See the “status” property for a status value that
is suitable for programmatic use.
Owner: GtkPrintOperation
Flags: Read
Default value: ""
Since: 2.10
The “support-selection” property
“support-selection” gboolean
If TRUE, the print operation will support print of selection.
This allows the print dialog to show a "Selection" button.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.18
The “track-print-status” property
“track-print-status” gboolean
If 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.
However, this is often implemented using polling, and should
not be enabled unless needed.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.10
The “unit” property
“unit” GtkUnit
The transformation for the cairo context obtained from
GtkPrintContext is set up in such a way that distances
are measured in units of unit
.
Owner: GtkPrintOperation
Flags: Read / Write
Default value: GTK_UNIT_NONE
Since: 2.10
The “use-full-page” property
“use-full-page” gboolean
If TRUE, the transformation for the cairo context obtained
from GtkPrintContext 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).
Owner: GtkPrintOperation
Flags: Read / Write
Default value: FALSE
Since: 2.10
Signal Details
The “create-custom-widget” signal
GObject*
user_function (GtkPrintOperation *operation,
gpointer user_data)
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 is not
controlled by the application. 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.
Returns
A custom widget that gets embedded in
the print dialog, or NULL.
[transfer none]
Flags: Run Last
Since: 2.10
The “custom-widget-apply” signal
void
user_function (GtkPrintOperation *operation,
GtkWidget *widget,
gpointer user_data)
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.
Flags: Run Last
Since: 2.10
The “paginate” signal
gboolean
user_function (GtkPrintOperation *operation,
GtkPrintContext *context,
gpointer user_data)
Emitted after the “begin-print” signal, but before
the actual rendering starts. It keeps getting emitted until a connected
signal handler returns TRUE.
The ::paginate signal is intended to be used for paginating a 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 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.
Returns
TRUE if pagination is complete
Flags: Run Last
Since: 2.10
The “request-page-setup” signal
void
user_function (GtkPrintOperation *operation,
GtkPrintContext *context,
gint page_nr,
GtkPageSetup *setup,
gpointer user_data)
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.
Flags: Run Last
Since: 2.10
The “update-custom-widget” signal
void
user_function (GtkPrintOperation *operation,
GtkWidget *widget,
GtkPageSetup *setup,
GtkPrintSettings *settings,
gpointer user_data)
Emitted after change of selected printer. The actual page setup and
print settings are passed to the custom widget, which can actualize
itself according to this change.
Flags: Run Last
Since: 2.18
The “ready” signal
void
user_function (GtkPrintOperationPreview *preview,
GtkPrintContext *context,
gpointer user_data)
The ::ready signal gets emitted once per preview operation,
before the first page is rendered.
A handler for this signal can be used for setup tasks.
Flags: Run Last