Trait gtk4::prelude::PrintOperationExt[][src]

pub trait PrintOperationExt: 'static {
Show 68 methods fn cancel(&self);
fn draw_page_finish(&self);
fn default_page_setup(&self) -> Option<PageSetup>;
fn embeds_page_setup(&self) -> bool;
fn error(&self) -> Result<(), Error>;
fn has_selection(&self) -> bool;
fn n_pages_to_print(&self) -> i32;
fn print_settings(&self) -> Option<PrintSettings>;
fn status(&self) -> PrintStatus;
fn status_string(&self) -> Option<GString>;
fn supports_selection(&self) -> bool;
fn is_finished(&self) -> bool;
fn run<P: IsA<Window>>(
        &self,
        action: PrintOperationAction,
        parent: Option<&P>
    ) -> Result<PrintOperationResult, Error>;
fn set_allow_async(&self, allow_async: bool);
fn set_current_page(&self, current_page: i32);
fn set_custom_tab_label(&self, label: Option<&str>);
fn set_default_page_setup(&self, default_page_setup: Option<&PageSetup>);
fn set_defer_drawing(&self);
fn set_embed_page_setup(&self, embed: bool);
fn set_export_filename<P: AsRef<Path>>(&self, filename: P);
fn set_has_selection(&self, has_selection: bool);
fn set_job_name(&self, job_name: &str);
fn set_n_pages(&self, n_pages: i32);
fn set_print_settings(&self, print_settings: Option<&PrintSettings>);
fn set_show_progress(&self, show_progress: bool);
fn set_support_selection(&self, support_selection: bool);
fn set_track_print_status(&self, track_status: bool);
fn set_unit(&self, unit: Unit);
fn set_use_full_page(&self, full_page: bool);
fn allows_async(&self) -> bool;
fn current_page(&self) -> i32;
fn custom_tab_label(&self) -> Option<GString>;
fn export_filename(&self) -> Option<GString>;
fn job_name(&self) -> Option<GString>;
fn n_pages(&self) -> i32;
fn shows_progress(&self) -> bool;
fn tracks_print_status(&self) -> bool;
fn unit(&self) -> Unit;
fn uses_full_page(&self) -> bool;
fn connect_begin_print<F: Fn(&Self, &PrintContext) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_create_custom_widget<F: Fn(&Self) -> Option<Object> + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_custom_widget_apply<F: Fn(&Self, &Widget) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_done<F: Fn(&Self, PrintOperationResult) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_draw_page<F: Fn(&Self, &PrintContext, i32) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_end_print<F: Fn(&Self, &PrintContext) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_paginate<F: Fn(&Self, &PrintContext) -> bool + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_preview<F: Fn(&Self, &PrintOperationPreview, &PrintContext, Option<&Window>) -> bool + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_request_page_setup<F: Fn(&Self, &PrintContext, i32, &PageSetup) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_status_changed<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_update_custom_widget<F: Fn(&Self, &Widget, &PageSetup, &PrintSettings) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_allow_async_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_current_page_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_custom_tab_label_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_default_page_setup_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_embed_page_setup_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_export_filename_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_has_selection_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_job_name_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_n_pages_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_n_pages_to_print_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_print_settings_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_show_progress_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_status_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_status_string_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_support_selection_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_track_print_status_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_unit_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
fn connect_use_full_page_notify<F: Fn(&Self) + 'static>(
        &self,
        f: F
    ) -> SignalHandlerId;
}
Expand description

Trait containing all PrintOperation methods.

Implementors

PrintOperation

Required methods

Cancels a running print operation.

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

Signal that drawing of 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. Otherwise it is called by GTK itself.

Returns the default page setup.

Returns

the default page setup

Gets whether page setup selection combos are embedded

Returns

whether page setup selection combos are embedded

Call this when the result of a print operation is PrintOperationResult::Error.

It can be called either after run() returns, or in the signal::PrintOperation::done signal handler.

The returned GError will contain more details on what went wrong.

Gets whether there is a selection.

Returns

whether there is a selection

Returns the number of pages that will be printed.

Note that this value is set during print preparation phase (PrintStatus::Preparing), so this function should never be called before the data generation phase (PrintStatus::GeneratingData). You can connect to the signal::PrintOperation::status-changed signal and call n_pages_to_print() when print status is PrintStatus::GeneratingData.

This is typically used to track the progress of print operation.

Returns

the number of pages that will be printed

Returns the current print settings.

Note that the return value is None until either set_print_settings() or run() have been called.

Returns

the current print settings of self.

Returns the status of the print operation.

Also see status_string().

Returns

the status of the print operation

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

Use status() to obtain a status value that is suitable for programmatic use.

Returns

a string representation of the status of the print operation

Gets whether the application supports print of selection

Returns

whether the application supports print of selection

A convenience function to find out if the print operation is finished.

a print operation is finished if its status is either PrintStatus::Finished or PrintStatus::FinishedAborted.

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.

Runs the print operation.

Normally that this function does not return until the rendering of all pages is complete. You can connect to the signal::PrintOperation::status-changed signal on self 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 set the [Gtk.PrintOperation:allow-async] property, the operation will run asynchronously if this is supported on the platform. The signal::PrintOperation::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).

⚠️ The following code is in c ⚠️

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_window_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 run() can only be called once on a given PrintOperation.

action

the action to start

parent

Transient parent of the dialog

Returns

the result of the print operation. A return value of PrintOperationResult::Apply indicates that the printing was completed successfully. In this case, it is a good idea to obtain the used print settings with print_settings() and store them for reuse with the next print operation. A value of PrintOperationResult::InProgress means the operation is running asynchronously, and will emit the signal::PrintOperation::done signal when done.

Sets whether run() may return before the print operation is completed.

Note that some platforms may not allow asynchronous operation.

allow_async

true to allow asynchronous operation

Sets the current page.

If this is called before run(), the user will be able to select to print only the current page.

Note that this only makes sense for pre-paginated documents.

current_page

the current page, 0-based

Sets the label for the tab holding custom widgets.

label

the label to use, or None to use the default label

Makes default_page_setup the default page setup for self.

This page setup will be used by run(), but it can be overridden on a per-page basis by connecting to the signal::PrintOperation::request-page-setup signal.

default_page_setup

a PageSetup

Sets up the PrintOperation to wait for calling of draw_page_finish() from application.

This can be used for drawing page in another thread.

This function must be called in the callback of the signal::PrintOperation::draw-page signal.

Embed page size combo box and orientation combo box into page setup page.

Selected page setup is stored as default page setup in PrintOperation.

embed

true to embed page setup selection in the PrintUnixDialog

Sets up the PrintOperation to generate a file instead of showing the print dialog.

The intended 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.

filename

the filename for the exported file

Sets whether there is a selection to print.

Application has to set number of pages to which the selection will draw by set_n_pages() in a handler for the signal::PrintOperation::begin-print signal.

has_selection

true indicates that a selection exists

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.

job_name

a string that identifies the print job

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 signal::PrintOperation::begin-print signal handler.

Note that the page numbers passed to the signal::PrintOperation::request-page-setup and signal::PrintOperation::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.

n_pages

the number of pages

Sets the print settings for self.

This is typically used to re-establish print settings from a previous print operation, see run().

PrintSettings

If show_progress is true, the print operation will show a progress dialog during the print operation.

show_progress

true to show a progress dialog

Sets whether selection is supported by PrintOperation.

support_selection

true to support selection

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.

track_status

true to track status after printing

Sets up the transformation for the cairo context obtained from PrintContext in such a way that distances are measured in units of unit.

unit

the unit to use

If full_page is true, the transformation for the cairo context obtained from PrintContext puts the origin at the top left corner of the page.

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

full_page

true to set up the PrintContext for the full page

Determines whether the print operation may run asynchronously or not.

Some systems don’t support asynchronous printing, but those that do will return PrintOperationResult::InProgress as the status, and emit the signal::PrintOperation::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 PrintOperationAction::Export support asynchronous operation.

The current page in the document.

If this is set before run(), the user will be able to select to print only the current page.

Note that this only makes sense for pre-paginated documents.

Used as the label of the tab containing custom widgets.

Note that this property may be ignored on some platforms.

If this is None, GTK uses a default label.

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.

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.

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 signal::PrintOperation::begin-print signal handler.

Note that the page numbers passed to the signal::PrintOperation::request-page-setup and signal::PrintOperation::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.

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

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.

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

If true, the transformation for the cairo context obtained from PrintContext puts the origin at the top left corner of the page.

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

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

A typical use for ::begin-print is to use the parameters from the PrintContext and paginate the document accordingly, and then set the number of pages with set_n_pages().

context

the PrintContext for the current operation

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 signal::PrintOperation::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

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 guaranteed to be around at a later time.

widget

the custom widget added in ::create-custom-widget

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 PrintOperationResult::Error then you can call error() for more information.

If you enabled print status tracking then is_finished() may still return false after the ::done signal was emitted.

result

the result of the print operation

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 PrintContext::cairo_context().

⚠️ The following code is in c ⚠️

static void
draw_page (GtkPrintOperation *operation,
           GtkPrintContext   *context,
           int                page_nr,
           gpointer           user_data)
{
  cairo_t *cr;
  PangoLayout *layout;
  double width, text_height;
  int 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 = (double)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 set_use_full_page() and set_unit() before starting the print operation to set up the transformation of the cairo context according to your needs.

context

the PrintContext for the current operation

page_nr

the number of the currently printed page (0-based)

Emitted after all pages have been rendered.

A handler for this signal can clean up any resources that have been allocated in the signal::PrintOperation::begin-print handler.

context

the PrintContext for the current operation

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

context

the PrintContext for the current operation

Returns

true if pagination is complete

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 true 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 with PrintContext::set_cairo_context().

The custom preview implementation can use PrintOperationPreviewExt::is_selected() and PrintOperationPreviewExt::render_page() to find pages which are selected for print and render them. The preview must be finished by calling PrintOperationPreviewExt::end_preview() (typically in response to the user clicking a close button).

preview

the PrintOperationPreview for the current operation

context

the PrintContext that will be used

parent

the Window to use as window parent

Returns

true if the listener wants to take over control of the preview

Emitted once for every page that is printed.

This gives the application a chance to modify the page setup. Any changes done to setup will be in force only for printing this page.

context

the PrintContext for the current operation

page_nr

the number of the currently printed page (0-based)

setup

the PageSetup

Emitted at between the various phases of the print operation.

See PrintStatus for the phases that are being discriminated. Use status() to find out the current status.

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.

widget

the custom widget added in ::create-custom-widget

setup

actual page setup

settings

actual print settings

Implementors