Class

GtkPrintOperation

Description [src]

class Gtk.PrintOperation : GObject.Object {
  parent_instance: GObject
}

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 GtkPrintOperation::draw-page, which you are supposed to catch and render the page on the provided GtkPrintContext using Cairo.

The high-level printing API

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_preview_render_page(), gtk_print_operation_preview_end_preview() and gtk_print_operation_preview_is_selected() are useful when implementing a print preview.

Hierarchy

hierarchy this GtkPrintOperation implements_0 GtkPrintOperationPreview this--implements_0 ancestor_0 GObject ancestor_0--this

Ancestors

Constructors

gtk_print_operation_new

Creates a new GtkPrintOperation.

since: 2.10

Instance methods

gtk_print_operation_cancel

Cancels a running print operation. This function may be called from a GtkPrintOperation::begin-print, GtkPrintOperation::paginate or GtkPrintOperation::draw-page signal handler to stop the currently running print operation.

since: 2.10

gtk_print_operation_draw_page_finish

Signalize that drawing of particular page is complete.

since: 2.16

gtk_print_operation_get_default_page_setup

Returns the default page setup, see gtk_print_operation_set_default_page_setup().

since: 2.10

gtk_print_operation_get_embed_page_setup

Gets the value of GtkPrintOperation:embed-page-setup property.

since: 2.18

gtk_print_operation_get_error

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 GtkPrintOperation::done signal handler. The returned GError will contain more details on what went wrong.

since: 2.10

gtk_print_operation_get_has_selection

Gets the value of GtkPrintOperation:has-selection property.

since: 2.18

gtk_print_operation_get_n_pages_to_print

Returns the number of pages that will be printed.

since: 2.18

gtk_print_operation_get_print_settings

Returns the current print settings.

since: 2.10

gtk_print_operation_get_status

Returns the status of the print operation. Also see gtk_print_operation_get_status_string().

since: 2.10

gtk_print_operation_get_status_string

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.

since: 2.10

gtk_print_operation_get_support_selection

Gets the value of GtkPrintOperation:support-selection property.

since: 2.18

gtk_print_operation_is_finished

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).

since: 2.10

gtk_print_operation_run

Runs the print operation, by first letting the user modify print settings in the print dialog, and then print the document.

since: 2.10

gtk_print_operation_set_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_current_page

Sets the current page.

since: 2.10

gtk_print_operation_set_custom_tab_label

Sets the label for the tab holding custom widgets.

since: 2.10

gtk_print_operation_set_default_page_setup

Makes default_page_setup the default page setup for op.

since: 2.10

gtk_print_operation_set_defer_drawing

Sets up the GtkPrintOperation to wait for calling of gtk_print_operation_draw_page_finish() from application. It can be used for drawing page in another thread.

since: 2.16

gtk_print_operation_set_embed_page_setup

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_set_export_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.

since: 2.10

gtk_print_operation_set_has_selection

Sets whether there is a selection to print.

since: 2.18

gtk_print_operation_set_job_name

Sets the name of the print job. The name is used to identify the job (e.g. in monitoring applications like eggcups).

since: 2.10

gtk_print_operation_set_n_pages

Sets the number of pages in the document.

since: 2.10

gtk_print_operation_set_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_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_support_selection

Sets whether selection is supported by GtkPrintOperation.

since: 2.18

gtk_print_operation_set_track_print_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.

since: 2.10

gtk_print_operation_set_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_use_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

Methods inherited from GObject (43)

Please see GObject for a full list of methods.

Methods inherited from GtkPrintOperationPreview (3)
gtk_print_operation_preview_end_preview

Ends a preview.

since: 2.10

gtk_print_operation_preview_is_selected

Returns whether the given page is included in the set of pages that have been selected for printing.

since: 2.10

gtk_print_operation_preview_render_page

Renders a page to the preview, using the print context that was passed to the GtkPrintOperation::preview handler together with preview.

since: 2.10

Properties

Gtk.PrintOperation:allow-async

Determines whether the print operation may run asynchronously or not.

since: 2.10

Gtk.PrintOperation:current-page

The current page in the document.

since: 2.10

Gtk.PrintOperation:custom-tab-label

Used as the label of the tab containing custom widgets. Note that this property may be ignored on some platforms.

since: 2.10

Gtk.PrintOperation:default-page-setup

The GtkPageSetup used by default.

since: 2.10

Gtk.PrintOperation:embed-page-setup

If TRUE, page size combo box and orientation combo box are embedded into page setup page.

since: 2.18

Gtk.PrintOperation:export-filename

The name of a file to generate instead of showing the print dialog. Currently, PDF is the only supported format.

since: 2.10

Gtk.PrintOperation:has-selection

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.

since: 2.18

Gtk.PrintOperation:job-name

A string used to identify the job (e.g. in monitoring applications like eggcups).

since: 2.10

Gtk.PrintOperation:n-pages

The number of pages in the document.

since: 2.10

Gtk.PrintOperation:n-pages-to-print

The number of pages that will be printed.

since: 2.18

Gtk.PrintOperation:print-settings

The GtkPrintSettings used for initializing the dialog.

since: 2.10

Gtk.PrintOperation:show-progress

Determines whether to show a progress dialog during the print operation.

since: 2.10

Gtk.PrintOperation:status

The status of the print operation.

since: 2.10

Gtk.PrintOperation:status-string

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.

since: 2.10

Gtk.PrintOperation:support-selection

If TRUE, the print operation will support print of selection. This allows the print dialog to show a “Selection” button.

since: 2.18

Gtk.PrintOperation:track-print-status

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.

since: 2.10

Gtk.PrintOperation:unit

The transformation for the cairo context obtained from GtkPrintContext is set up in such a way that distances are measured in units of unit.

since: 2.10

Gtk.PrintOperation:use-full-page

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).

since: 2.10

Signals

Gtk.PrintOperation::begin-print

Emitted after the user has finished changing print settings in the dialog, before the actual rendering starts.

since: 2.10

Gtk.PrintOperation::create-custom-widget

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.

since: 2.10

Gtk.PrintOperation::custom-widget-apply

Emitted right before GtkPrintOperation::begin-print if you added a custom widget in the GtkPrintOperation::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.PrintOperation::done

Emitted when the print operation run has finished doing everything required for printing.

since: 2.10

Gtk.PrintOperation::draw-page

Emitted for every page that is printed. The signal handler must render the page_nrs 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_SCALE);
  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);
}

since: 2.10

Gtk.PrintOperation::end-print

Emitted after all pages have been rendered. A handler for this signal can clean up any resources that have been allocated in the GtkPrintOperation::begin-print handler.

since: 2.10

Gtk.PrintOperation::paginate

Emitted after the GtkPrintOperation::begin-print signal, but before the actual rendering starts. It keeps getting emitted until a connected signal handler returns TRUE.

since: 2.10

Gtk.PrintOperation::preview

Gets emitted when a preview is requested from the native dialog.

since: 2.10

Gtk.PrintOperation::request-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.PrintOperation::status-changed

Emitted at between the various phases of the print operation. See GtkPrintStatus for the phases that are being discriminated. Use gtk_print_operation_get_status() to find out the current status.

since: 2.10

Gtk.PrintOperation::update-custom-widget

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.

since: 2.18

Signals inherited from GObject (1)
GObject::notify

The notify signal is emitted on an object when one of its properties has its value set through g_object_set_property(), g_object_set(), et al.

Signals inherited from GtkPrintOperationPreview (2)
GtkPrintOperationPreview::got-page-size

The ::got-page-size signal is emitted once for each page that gets rendered to the preview.

GtkPrintOperationPreview::ready

The ::ready signal gets emitted once per preview operation, before the first page is rendered.

Class structure

struct GtkPrintOperationClass {
  GObjectClass parent_class;
  void (* done) (
    GtkPrintOperation* operation,
    GtkPrintOperationResult result
  );
  void (* begin_print) (
    GtkPrintOperation* operation,
    GtkPrintContext* context
  );
  gboolean (* paginate) (
    GtkPrintOperation* operation,
    GtkPrintContext* context
  );
  void (* request_page_setup) (
    GtkPrintOperation* operation,
    GtkPrintContext* context,
    gint page_nr,
    GtkPageSetup* setup
  );
  void (* draw_page) (
    GtkPrintOperation* operation,
    GtkPrintContext* context,
    gint page_nr
  );
  void (* end_print) (
    GtkPrintOperation* operation,
    GtkPrintContext* context
  );
  void (* status_changed) (
    GtkPrintOperation* operation
  );
  GtkWidget* (* create_custom_widget) (
    GtkPrintOperation* operation
  );
  void (* custom_widget_apply) (
    GtkPrintOperation* operation,
    GtkWidget* widget
  );
  gboolean (* preview) (
    GtkPrintOperation* operation,
    GtkPrintOperationPreview* preview,
    GtkPrintContext* context,
    GtkWindow* parent
  );
  void (* update_custom_widget) (
    GtkPrintOperation* operation,
    GtkWidget* widget,
    GtkPageSetup* setup,
    GtkPrintSettings* settings
  );
  void (* _gtk_reserved1) (
void
  );
  void (* _gtk_reserved2) (
void
  );
  void (* _gtk_reserved3) (
void
  );
  void (* _gtk_reserved4) (
void
  );
  void (* _gtk_reserved5) (
void
  );
  void (* _gtk_reserved6) (
void
  );
  void (* _gtk_reserved7) (
void
  );
  void (* _gtk_reserved8) (
void
  );
  
}
No description available.
Class members
parent_class: GObjectClass

The parent class.

done: void (* done) ( GtkPrintOperation* operation, GtkPrintOperationResult result )
No description available.
begin_print: void (* begin_print) ( GtkPrintOperation* operation, GtkPrintContext* context )
No description available.
paginate: gboolean (* paginate) ( GtkPrintOperation* operation, GtkPrintContext* context )
No description available.
request_page_setup: void (* request_page_setup) ( GtkPrintOperation* operation, GtkPrintContext* context, gint page_nr, GtkPageSetup* setup )
No description available.
draw_page: void (* draw_page) ( GtkPrintOperation* operation, GtkPrintContext* context, gint page_nr )
No description available.
end_print: void (* end_print) ( GtkPrintOperation* operation, GtkPrintContext* context )
No description available.
status_changed: void (* status_changed) ( GtkPrintOperation* operation )
No description available.
create_custom_widget: GtkWidget* (* create_custom_widget) ( GtkPrintOperation* operation )
No description available.
custom_widget_apply: void (* custom_widget_apply) ( GtkPrintOperation* operation, GtkWidget* widget )
No description available.
preview: gboolean (* preview) ( GtkPrintOperation* operation, GtkPrintOperationPreview* preview, GtkPrintContext* context, GtkWindow* parent )
No description available.
update_custom_widget: void (* update_custom_widget) ( GtkPrintOperation* operation, GtkWidget* widget, GtkPageSetup* setup, GtkPrintSettings* settings )
No description available.
_gtk_reserved1: void (* _gtk_reserved1) ( void )
No description available.
_gtk_reserved2: void (* _gtk_reserved2) ( void )
No description available.
_gtk_reserved3: void (* _gtk_reserved3) ( void )
No description available.
_gtk_reserved4: void (* _gtk_reserved4) ( void )
No description available.
_gtk_reserved5: void (* _gtk_reserved5) ( void )
No description available.
_gtk_reserved6: void (* _gtk_reserved6) ( void )
No description available.
_gtk_reserved7: void (* _gtk_reserved7) ( void )
No description available.
_gtk_reserved8: void (* _gtk_reserved8) ( void )
No description available.

Virtual methods

Gtk.PrintOperationClass.begin_print
No description available.

Gtk.PrintOperationClass.done
No description available.

Gtk.PrintOperationClass.draw_page
No description available.

Gtk.PrintOperationClass.end_print
No description available.

Gtk.PrintOperationClass.paginate
No description available.

Gtk.PrintOperationClass.preview
No description available.

Gtk.PrintOperationClass.request_page_setup
No description available.

Gtk.PrintOperationClass.status_changed
No description available.