class Gtk::PrintOperation

Overview

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 Gtk#PrintUnixDialog.

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

Then you start the print operation by calling 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 Gtk#PrintContext using Cairo.

The high-level printing API

WARNING ⚠️ The following code is in 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 Gtk::PrintOperationPreview#render_page, Gtk::PrintOperationPreview#end_preview and Gtk::PrintOperationPreview#is_selected? are useful when implementing a print preview.

Included Modules

Defined in:

lib/gi-crystal/src/auto/gtk-4.0/print_operation.cr

Constructors

Class Method Summary

Instance Method Summary

Instance methods inherited from module Gtk::PrintOperationPreview

end_preview : Nil end_preview, got_page_size_signal got_page_size_signal, is_selected(page_nr : Int32) : Bool is_selected, ready_signal ready_signal, render_page(page_nr : Int32) : Nil render_page, to_unsafe to_unsafe

Class methods inherited from module Gtk::PrintOperationPreview

g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

bind_property(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags) : GObject::Binding bind_property, bind_property_full(source_property : String, target : GObject::Object, target_property : String, flags : GObject::BindingFlags, transform_to : GObject::Closure, transform_from : GObject::Closure) : GObject::Binding bind_property_full, data(key : String) : Pointer(Void)? data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, notify(property_name : String) : Nil notify, notify_by_pspec(pspec : GObject::ParamSpec) : Nil notify_by_pspec, notify_signal notify_signal, property(property_name : String, value : _) : Nil property, qdata(quark : UInt32) : Pointer(Void)? qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void)?) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void)? steal_data, steal_qdata(quark : UInt32) : Pointer(Void)? steal_qdata, thaw_notify : Nil thaw_notify, to_unsafe : Pointer(Void) to_unsafe, watch_closure(closure : GObject::Closure) : Nil watch_closure

Constructor methods inherited from class GObject::Object

cast(obj : GObject::Object) : self cast, cast?(obj : GObject::Object) : self? cast?, new(pointer : Pointer(Void), transfer : GICrystal::Transfer)
new
new
, newv(object_type : UInt64, parameters : Enumerable(GObject::Parameter)) : self newv

Class methods inherited from class GObject::Object

compat_control(what : UInt64, data : Pointer(Void)?) : UInt64 compat_control, g_type : UInt64 g_type, interface_find_property(g_iface : GObject::TypeInterface, property_name : String) : GObject::ParamSpec interface_find_property, interface_list_properties(g_iface : GObject::TypeInterface) : Enumerable(GObject::ParamSpec) interface_list_properties

Constructor Detail

def self.new : self #

Creates a new GtkPrintOperation.


def self.new(*, allow_async : Bool? = nil, current_page : Int32? = nil, custom_tab_label : String? = nil, default_page_setup : Gtk::PageSetup? = nil, embed_page_setup : Bool? = nil, export_filename : String? = nil, has_selection : Bool? = nil, job_name : String? = nil, n_pages : Int32? = nil, n_pages_to_print : Int32? = nil, print_settings : Gtk::PrintSettings? = nil, show_progress : Bool? = nil, status : Gtk::PrintStatus? = nil, status_string : String? = nil, support_selection : Bool? = nil, track_print_status : Bool? = nil, unit : Gtk::Unit? = nil, use_full_page : Bool? = nil) #

Class Method Detail

def self.g_type : UInt64 #

Returns the type id (GType) registered in GLib type system.


Instance Method Detail

def allow_async=(allow_async : Bool) : Nil #

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

Note that some platforms may not allow asynchronous operation.


def allow_async? : Bool #

def begin_print_signal #

def cancel : Nil #

Cancels a running print operation.

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


def create_custom_widget_signal #

def current_page : Int32 #

def current_page=(current_page : Int32) : Nil #

Sets the current page.

If this is called before 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.


def custom_tab_label : String #

def custom_tab_label=(value : String) : String #

def custom_tab_label=(label : String?) : Nil #

Sets the label for the tab holding custom widgets.


def custom_widget_apply_signal #

def default_page_setup : Gtk::PageSetup #

Returns the default page setup.


def default_page_setup=(default_page_setup : Gtk::PageSetup?) : Nil #

Makes @default_page_setup the default page setup for @op.

This page setup will be used by Gtk::PrintOperation#run, but it can be overridden on a per-page basis by connecting to the [signal@Gtk.PrintOperation::request-page-setup] signal.


def done_signal #

def draw_page_finish : Nil #

Signal that drawing of particular page is complete.

It is called after completion of page drawing (e.g. drawing in another thread). If Gtk::PrintOperation#defer_drawing= was called before, then this function has to be called by application. Otherwise it is called by GTK itself.


def draw_page_signal #

def embed_page_setup : Bool #

Gets whether page setup selection combos are embedded


def embed_page_setup=(embed : Bool) : Nil #

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

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


def embed_page_setup? : Bool #

def end_print_signal #

def error : Nil #

Call this when the result of a print operation is %GTK_PRINT_OPERATION_RESULT_ERROR.

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

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


def export_filename : String #

def export_filename=(filename : String) : Nil #

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.


def has_selection : Bool #

Gets whether there is a selection.


def has_selection=(has_selection : Bool) : Nil #

Sets whether there is a selection to print.

Application has to set number of pages to which the selection will draw by Gtk::PrintOperation#n_pages= in a handler for the [signal@Gtk.PrintOperation::begin-print] signal.


def has_selection? : Bool #

def is_finished : Bool #

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.


def job_name : String #

def job_name=(job_name : String) : Nil #

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.


def n_pages : Int32 #

def n_pages=(n_pages : Int32) : Nil #

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.


def n_pages_to_print : Int32 #

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.


def paginate_signal #

def preview_signal #

def print_settings : Gtk::PrintSettings? #

Returns the current print settings.

Note that the return value is %NULL until either Gtk::PrintOperation#print_settings= or Gtk::PrintOperation#run have been called.


def print_settings=(print_settings : Gtk::PrintSettings?) : Nil #

Sets the print settings for @op.

This is typically used to re-establish print settings from a previous print operation, see Gtk::PrintOperation#run.


def request_page_setup_signal #

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

WARNING ⚠️ 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 gtk_print_operation_run() can only be called once on a given GtkPrintOperation.


def set_defer_drawing : Nil #

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.


def show_progress=(show_progress : Bool) : Nil #

If @show_progress is %TRUE, the print operation will show a progress dialog during the print operation.


def show_progress? : Bool #

def status : Gtk::PrintStatus #

Returns the status of the print operation.

Also see Gtk::PrintOperation#status_string.


def status_changed_signal #

def status_string : 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.

Use Gtk::PrintOperation#status to obtain a status value that is suitable for programmatic use.


def support_selection : Bool #

Gets whether the application supports print of selection


def support_selection=(support_selection : Bool) : Nil #

Sets whether selection is supported by GtkPrintOperation.


def support_selection? : Bool #

def track_print_status=(track_status : Bool) : Nil #

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.


def track_print_status? : Bool #

def unit : Gtk::Unit #

def unit=(unit : Gtk::Unit) : Nil #

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


def update_custom_widget_signal #

def use_full_page=(full_page : Bool) : Nil #

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


def use_full_page? : Bool #