Class PrintOperation

All Implemented Interfaces:
PointerInterface

public class PrintOperation extends Object
`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 [class@Gtk.PrintUnixDialog].

The typical way to use the high-level printing API is to create
a `GtkPrintOperation` object with [ctor@Gtk.PrintOperation.new]
when the user selects to print. Then you set some properties on it,
e.g. the page size, any [class@Gtk.PrintSettings] from previous print
operations, the number of pages, the current page, etc.

Then you start the print operation by calling [method@Gtk.PrintOperation.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
[signal@Gtk.PrintOperation::draw-page], which you are supposed to handle
and render the page on the provided [class@Gtk.PrintContext] using Cairo.

# The high-level printing API

```c
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
[method@Gtk.PrintOperationPreview.render_page],
[method@Gtk.PrintOperationPreview.end_preview] and
[method@Gtk.PrintOperationPreview.is_selected]
are useful when implementing a print preview.

https://docs.gtk.org/gtk4/class.PrintOperation.html

  • Field Details

  • Constructor Details

    • PrintOperation

      public PrintOperation(PointerContainer pointer)
    • PrintOperation

      public PrintOperation()
      Creates a new `GtkPrintOperation`.
  • Method Details

    • getClassHandler

      public static ClassHandler getClassHandler()
    • cancel

      public void cancel()
      Cancels a running print operation.

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

      public void drawPageFinish()
      Signal that drawing of particular page is complete.

      It is called after completion of page drawing (e.g. drawing
      in another thread). If [method@Gtk.PrintOperation.set_defer_drawing]
      was called before, then this function has to be called by application.
      Otherwise it is called by GTK itself.
    • getDefaultPageSetup

      public PageSetup getDefaultPageSetup()
      Returns the default page setup.
      Returns:
      the default page setup
    • getEmbedPageSetup

      public boolean getEmbedPageSetup()
      Gets whether page setup selection combos are embedded
      Returns:
      whether page setup selection combos are embedded
    • getError

      public void getError() throws AllocationError
      Call this when the result of a print operation is
      %GTK_PRINT_OPERATION_RESULT_ERROR.

      It can be called either after [method@Gtk.PrintOperation.run]
      returns, or in the [signal@Gtk.PrintOperation::done] signal
      handler.

      The returned `GError` will contain more details on what went wrong.
      Throws:
      AllocationError
    • getHasSelection

      public boolean getHasSelection()
      Gets whether there is a selection.
      Returns:
      whether there is a selection
    • getNPagesToPrint

      public int getNPagesToPrint()
      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 [signal@Gtk.PrintOperation::status-changed]
      signal and call gtk_print_operation_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.
      Returns:
      the number of pages that will be printed
    • getPrintSettings

      public PrintSettings getPrintSettings()
      Returns the current print settings.

      Note that the return value is %NULL until either
      [method@Gtk.PrintOperation.set_print_settings] or
      [method@Gtk.PrintOperation.run] have been called.
      Returns:
      the current print settings of @op.
    • getStatus

      public int getStatus()
      Returns the status of the print operation.

      Also see [method@Gtk.PrintOperation.get_status_string].
      Returns:
      the status of the print operation
    • getStatusString

      public Str getStatusString()
      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 [method@Gtk.PrintOperation.get_status] to obtain
      a status value that is suitable for programmatic use.
      Returns:
      a string representation of the status of the print operation
    • getSupportSelection

      public boolean getSupportSelection()
      Gets whether the application supports print of selection
      Returns:
      whether the application supports print of selection
    • isFinished

      public boolean isFinished()
      A convenience function to find out if the print operation
      is finished.

      a print operation is finished if its status is either
      %GTK_PRINT_STATUS_FINISHED or %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.
    • run

      public int run(int action, @Nullable Window parent) throws AllocationError
      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@Gtk.PrintOperation::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 set the [Gtk.PrintOperation:allow-async] property, the operation
      will run asynchronously if this is supported on the platform. The
      [signal@Gtk.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).

      ```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 gtk_print_operation_run() can only be called once on a
      given `GtkPrintOperation`.
      Parameters:
      action - the action to start
      parent - Transient parent of the dialog
      Returns:
      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 [method@Gtk.PrintOperation.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 [signal@Gtk.PrintOperation::done] signal when done.
      Throws:
      AllocationError
    • setAllowAsync

      public void setAllowAsync(boolean allow_async)
      Sets whether gtk_print_operation_run() may return
      before the print operation is completed.

      Note that some platforms may not allow asynchronous
      operation.
      Parameters:
      allow_async - %TRUE to allow asynchronous operation
    • setCurrentPage

      public void setCurrentPage(int current_page)
      Sets the current page.

      If this is called before [method@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.
      Parameters:
      current_page - the current page, 0-based
    • setCustomTabLabel

      public void setCustomTabLabel(@Nullable Str label)
      Sets the label for the tab holding custom widgets.
      Parameters:
      label - the label to use, or %NULL to use the default label
    • setCustomTabLabel

      public void setCustomTabLabel(String label)
      Sets the label for the tab holding custom widgets.
      Parameters:
      label - the label to use, or %NULL to use the default label
    • setDefaultPageSetup

      public void setDefaultPageSetup(@Nullable PageSetup default_page_setup)
      Makes @default_page_setup the default page setup for @op.

      This page setup will be used by [method@Gtk.PrintOperation.run],
      but it can be overridden on a per-page basis by connecting
      to the [signal@Gtk.PrintOperation::request-page-setup] signal.
      Parameters:
      default_page_setup - a `GtkPageSetup`
    • setDeferDrawing

      public void setDeferDrawing()
      Sets up the `GtkPrintOperation` to wait for calling of
      [method@Gtk.PrintOperation.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@Gtk.PrintOperation::draw-page] signal.
    • setEmbedPageSetup

      public void setEmbedPageSetup(boolean 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`.
      Parameters:
      embed - %TRUE to embed page setup selection in the `GtkPrintUnixDialog`
    • setExportFilename

      public void setExportFilename(@Nonnull Str filename)
      Sets up the `GtkPrintOperation` 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.
      Parameters:
      filename - the filename for the exported file
    • setExportFilename

      public void setExportFilename(String filename)
      Sets up the `GtkPrintOperation` 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.
      Parameters:
      filename - the filename for the exported file
    • setHasSelection

      public void setHasSelection(boolean has_selection)
      Sets whether there is a selection to print.

      Application has to set number of pages to which the selection
      will draw by [method@Gtk.PrintOperation.set_n_pages] in a handler
      for the [signal@Gtk.PrintOperation::begin-print] signal.
      Parameters:
      has_selection - %TRUE indicates that a selection exists
    • setJobName

      public void setJobName(@Nonnull Str 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.
      Parameters:
      job_name - a string that identifies the print job
    • setJobName

      public void setJobName(String 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.
      Parameters:
      job_name - a string that identifies the print job
    • setNPages

      public void setNPages(int 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 [signal@Gtk.PrintOperation::begin-print]
      signal handler.

      Note that the page numbers passed to the
      [signal@Gtk.PrintOperation::request-page-setup]
      and [signal@Gtk.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.
      Parameters:
      n_pages - the number of pages
    • setPrintSettings

      public void setPrintSettings(@Nullable PrintSettings print_settings)
      Sets the print settings for @op.

      This is typically used to re-establish print settings
      from a previous print operation, see [method@Gtk.PrintOperation.run].
      Parameters:
      print_settings - `GtkPrintSettings`
    • setShowProgress

      public void setShowProgress(boolean show_progress)
      If @show_progress is %TRUE, the print operation will show
      a progress dialog during the print operation.
      Parameters:
      show_progress - %TRUE to show a progress dialog
    • setSupportSelection

      public void setSupportSelection(boolean support_selection)
      Sets whether selection is supported by `GtkPrintOperation`.
      Parameters:
      support_selection - %TRUE to support selection
    • setTrackPrintStatus

      public void setTrackPrintStatus(boolean 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.
      Parameters:
      track_status - %TRUE to track status after printing
    • setUnit

      public void setUnit(int unit)
      Sets up the transformation for the cairo context obtained from
      `GtkPrintContext` in such a way that distances are measured in
      units of @unit.
      Parameters:
      unit - the unit to use
    • setUseFullPage

      public void setUseFullPage(boolean 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.

      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).
      Parameters:
      full_page - %TRUE to set up the `GtkPrintContext` for the full page
    • onBeginPrint

      public SignalHandler onBeginPrint(PrintOperation.OnBeginPrint signal)
      Connect to signal "begin-print".
      See PrintOperation.OnBeginPrint.onBeginPrint(ch.bailu.gtk.gtk.PrintContext) for signal description.
      Field SIGNAL_ON_BEGIN_PRINT contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onCreateCustomWidget

      public SignalHandler onCreateCustomWidget(PrintOperation.OnCreateCustomWidget signal)
      Connect to signal "create-custom-widget".
      See PrintOperation.OnCreateCustomWidget.onCreateCustomWidget() for signal description.
      Field SIGNAL_ON_CREATE_CUSTOM_WIDGET contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onCustomWidgetApply

      public SignalHandler onCustomWidgetApply(PrintOperation.OnCustomWidgetApply signal)
      Connect to signal "custom-widget-apply".
      See PrintOperation.OnCustomWidgetApply.onCustomWidgetApply(ch.bailu.gtk.gtk.Widget) for signal description.
      Field SIGNAL_ON_CUSTOM_WIDGET_APPLY contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onDone

      public SignalHandler onDone(PrintOperation.OnDone signal)
      Connect to signal "done".
      See PrintOperation.OnDone.onDone(int) for signal description.
      Field SIGNAL_ON_DONE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onDrawPage

      public SignalHandler onDrawPage(PrintOperation.OnDrawPage signal)
      Connect to signal "draw-page".
      See PrintOperation.OnDrawPage.onDrawPage(ch.bailu.gtk.gtk.PrintContext, int) for signal description.
      Field SIGNAL_ON_DRAW_PAGE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onEndPrint

      public SignalHandler onEndPrint(PrintOperation.OnEndPrint signal)
      Connect to signal "end-print".
      See PrintOperation.OnEndPrint.onEndPrint(ch.bailu.gtk.gtk.PrintContext) for signal description.
      Field SIGNAL_ON_END_PRINT contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onPaginate

      public SignalHandler onPaginate(PrintOperation.OnPaginate signal)
      Connect to signal "paginate".
      See PrintOperation.OnPaginate.onPaginate(ch.bailu.gtk.gtk.PrintContext) for signal description.
      Field SIGNAL_ON_PAGINATE contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onPreview

      public SignalHandler onPreview(PrintOperation.OnPreview signal)
      Connect to signal "preview".
      See PrintOperation.OnPreview.onPreview(ch.bailu.gtk.gtk.PrintOperationPreview, ch.bailu.gtk.gtk.PrintContext, ch.bailu.gtk.gtk.Window) for signal description.
      Field SIGNAL_ON_PREVIEW contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onRequestPageSetup

      public SignalHandler onRequestPageSetup(PrintOperation.OnRequestPageSetup signal)
      Connect to signal "request-page-setup".
      See PrintOperation.OnRequestPageSetup.onRequestPageSetup(ch.bailu.gtk.gtk.PrintContext, int, ch.bailu.gtk.gtk.PageSetup) for signal description.
      Field SIGNAL_ON_REQUEST_PAGE_SETUP contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onStatusChanged

      public SignalHandler onStatusChanged(PrintOperation.OnStatusChanged signal)
      Connect to signal "status-changed".
      See PrintOperation.OnStatusChanged.onStatusChanged() for signal description.
      Field SIGNAL_ON_STATUS_CHANGED contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • onUpdateCustomWidget

      public SignalHandler onUpdateCustomWidget(PrintOperation.OnUpdateCustomWidget signal)
      Connect to signal "update-custom-widget".
      See PrintOperation.OnUpdateCustomWidget.onUpdateCustomWidget(ch.bailu.gtk.gtk.Widget, ch.bailu.gtk.gtk.PageSetup, ch.bailu.gtk.gtk.PrintSettings) for signal description.
      Field SIGNAL_ON_UPDATE_CUSTOM_WIDGET contains original signal name and can be used as resource reference.
      Parameters:
      signal - callback function (lambda).
      Returns:
      SignalHandler. Can be used to disconnect signal and to release callback function.
    • asPrintOperationPreview

      public PrintOperationPreview asPrintOperationPreview()
      Implements interface PrintOperationPreview. Call this to get access to interface functions.
      Returns:
      PrintOperationPreview
    • getTypeID

      public static long getTypeID()
    • getParentTypeID

      public static long getParentTypeID()
    • getTypeSize

      public static TypeSystem.TypeSize getTypeSize()
    • getParentTypeSize

      public static TypeSystem.TypeSize getParentTypeSize()
    • getInstanceSize

      public static int getInstanceSize()