class Gtk::PrintOperation

Overview

Gtk::PrintOperation is the high-level, portable printing API.

It looks a bit different than other GTK dialogs such as the Gtk::FileChooser, since some platforms don’t expose enough infrastructure to implement a good print dialog. On such platforms, Gtk::PrintOperation 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 Gtk::PrintOperation 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 Gtk::PrintOperation, 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 Gtk::PrintSettings *settings = NULL;

static void
do_print (void)
{
  Gtk::PrintOperation *print;
  Gtk::PrintOperationResult 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 Gtk::PrintOperation 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

Constructor methods inherited from module Gtk::PrintOperationPreview

cast(obj : GObject::Object) : self cast

Class methods inherited from module Gtk::PrintOperationPreview

cast?(obj : GObject::Object) : self | Nil cast?, g_type : UInt64 g_type

Instance methods inherited from class GObject::Object

==(other : self) ==, 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) | Nil data, finalize finalize, freeze_notify : Nil freeze_notify, getv(names : Enumerable(String), values : Enumerable(_)) : Nil getv, hash(hasher) hash, 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) | Nil qdata, ref_count : UInt32 ref_count, run_dispose : Nil run_dispose, set_data(key : String, data : Pointer(Void) | Nil) : Nil set_data, set_property(property_name : String, value : _) : Nil set_property, steal_data(key : String) : Pointer(Void) | Nil steal_data, steal_qdata(quark : UInt32) : Pointer(Void) | Nil 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, 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

cast?(obj : GObject::Object) : self | Nil cast?, compat_control(what : UInt64, data : Pointer(Void) | Nil) : 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

Macros inherited from class GObject::Object

previous_vfunc(*args) previous_vfunc, previous_vfunc!(*args) previous_vfunc!, signal(signature) signal

Constructor Detail

def self.new : self #

Creates a new Gtk::PrintOperation.


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

[View source]

Class Method Detail

def self.g_type : UInt64 #

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


[View source]

Instance Method Detail

def ==(other : self) #
Description copied from class Reference

Returns true if this reference is the same as other. Invokes same?.


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.


[View source]
def allow_async? : Bool #

[View source]
def begin_print_signal #

[View source]
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.


[View source]
def create_custom_widget_signal #

[View source]
def current_page : Int32 #

[View source]
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.


[View source]
def custom_tab_label : String #

[View source]
def custom_tab_label=(value : String) : String #

[View source]
def custom_tab_label=(label : String | Nil) : Nil #

Sets the label for the tab holding custom widgets.


[View source]
def custom_widget_apply_signal #

[View source]
def default_page_setup : Gtk::PageSetup #

Returns the default page setup.


[View source]
def default_page_setup=(default_page_setup : Gtk::PageSetup | Nil) : 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.


[View source]
def done_signal #

[View source]
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.


[View source]
def draw_page_signal #

[View source]
def embed_page_setup : Bool #

Gets whether page setup selection combos are embedded


[View source]
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 Gtk::PrintOperation.


[View source]
def embed_page_setup? : Bool #

[View source]
def end_print_signal #

[View source]
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.


[View source]
def export_filename : String #

[View source]
def export_filename=(filename : String) : Nil #

Sets up the Gtk::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.


[View source]
def has_selection : Bool #

Gets whether there is a selection.


[View source]
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.


[View source]
def has_selection? : Bool #

[View source]
def hash(hasher) #
Description copied from class Reference

See Object#hash(hasher)


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.


[View source]
def job_name : String #

[View source]
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.


[View source]
def n_pages : Int32 #

[View source]
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.


[View source]
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.


[View source]
def paginate_signal #

[View source]
def preview_signal #

[View source]
def print_settings : Gtk::PrintSettings | Nil #

Returns the current print settings.

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


[View source]
def print_settings=(print_settings : Gtk::PrintSettings | Nil) : 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.


[View source]
def request_page_setup_signal #

[View source]

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


[View source]
def set_defer_drawing : Nil #

Sets up the Gtk::PrintOperation 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.


[View source]
def show_progress=(show_progress : Bool) : Nil #

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


[View source]
def show_progress? : Bool #

[View source]
def status : Gtk::PrintStatus #

Returns the status of the print operation.

Also see Gtk::PrintOperation#status_string.


[View source]
def status_changed_signal #

[View source]
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 Gtk::Statusbar.

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


[View source]
def support_selection : Bool #

Gets whether the application supports print of selection


[View source]
def support_selection=(support_selection : Bool) : Nil #

Sets whether selection is supported by Gtk::PrintOperation.


[View source]
def support_selection? : Bool #

[View source]
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.


[View source]
def track_print_status? : Bool #

[View source]
def unit : Gtk::Unit #

[View source]
def unit=(unit : Gtk::Unit) : Nil #

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


[View source]
def update_custom_widget_signal #

[View source]
def use_full_page=(full_page : Bool) : Nil #

If full_page is true, the transformation for the cairo context obtained from Gtk::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).


[View source]
def use_full_page? : Bool #

[View source]