gtk-High-level-Printing-API: GtkPrintOperation

Description Methods and Functions Hierarchy Implementations Interfaces Detailed Description Structures Convenient Construction Enums and Flags User Functions Signals Properties Author(s) References See Also

Description

High-level Printing API

Methods and Functions

gtkPrintOperationNew()
gtkPrintOperationSetAllowAsync(object, allow.async)
gtkPrintOperationGetError(object, .errwarn = TRUE)
gtkPrintOperationSetDefaultPageSetup(object, default.page.setup = NULL)
gtkPrintOperationGetDefaultPageSetup(object)
gtkPrintOperationSetPrintSettings(object, print.settings = NULL)
gtkPrintOperationGetPrintSettings(object)
gtkPrintOperationSetJobName(object, job.name)
gtkPrintOperationSetNPages(object, n.pages)
gtkPrintOperationGetNPagesToPrint(object)
gtkPrintOperationSetCurrentPage(object, current.page)
gtkPrintOperationSetUseFullPage(object, full.page)
gtkPrintOperationSetUnit(object, unit)
gtkPrintOperationSetExportFilename(object, filename)
gtkPrintOperationSetShowProgress(object, show.progress)
gtkPrintOperationSetTrackPrintStatus(object, track.status)
gtkPrintOperationSetCustomTabLabel(object, label)
gtkPrintOperationRun(object, action, parent = NULL, .errwarn = TRUE)
gtkPrintOperationCancel(object)
gtkPrintOperationDrawPageFinish(object)
gtkPrintOperationSetDeferDrawing(object)
gtkPrintOperationGetStatus(object)
gtkPrintOperationGetStatusString(object)
gtkPrintOperationIsFinished(object)
gtkPrintOperationSetSupportSelection(object, support.selection)
gtkPrintOperationGetSupportSelection(object)
gtkPrintOperationSetHasSelection(object, has.selection)
gtkPrintOperationGetHasSelection(object)
gtkPrintOperationSetEmbedPageSetup(object, embed)
gtkPrintOperationGetEmbedPageSetup(object)
gtkPrintRunPageSetupDialog(parent, page.setup = NULL, settings)
gtkPrintRunPageSetupDialogAsync(parent, page.setup, settings, done.cb, data)
gtkPrintOperationPreviewEndPreview(object)
gtkPrintOperationPreviewIsSelected(object, page.nr)
gtkPrintOperationPreviewRenderPage(object, page.nr)
gtkPrintOperation()

Hierarchy

1
2
3
4

Implementations

GtkPrintOperationPreview is implemented by GtkPrintOperation.

Interfaces

GtkPrintOperation implements GtkPrintOperationPreview.

Detailed Description

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

The high-level printing API

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
settings <- NULL
print_something <-
{
  op <- gtkPrintOperation()

  if (!is.null(settings)) 
    op$setPrintSettings(settings)

  gSignalConnect(op, "begin_print", begin_print)
  gSignalConnect(op, "draw_page", draw_page)

  res <- op$run("print-dialog", main_window)[[1]]

  if (res == "apply")
    settings <- op$getPrintSettings()
}

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 gtkPrintOperationPrintPreviewRenderPage(), gtkPrintOperationPreviewEndPreview and gtkPrintOperationPreviewIsSelected are useful when implementing a print preview.

Printing support was added in GTK+ 2.10.

Structures

GtkPrintOperation

undocumented

GtkPrintOperationPreview

undocumented

Convenient Construction

gtkPrintOperation is the equivalent of gtkPrintOperationNew.

Enums and Flags

GtkPrintStatus

The status gives a rough indication of the completion of a running print operation.

initial

The printing has not started yet; this status is set initially, and while the print dialog is shown.

preparing

This status is set while the begin-print signal is emitted and during pagination.

generating-data

This status is set while the pages are being rendered.

sending-data

The print job is being sent off to the printer.

pending

The print job has been sent to the printer, but is not printed for some reason, e.g. the printer may be stopped.

pending-issue

Some problem has occurred during printing, e.g. a paper jam.

printing

The printer is processing the print job.

finished

The printing has been completed successfully.

finished-aborted

The printing has been aborted.

GtkPrintOperationAction

The action parameter to gtkPrintOperationRun determines what action the print operation should perform.

print-dialog

Show the print dialog.

print

Start to print without showing the print dialog, based on the current print settings.

preview

Show the print preview.

export

Export to a file. This requires the export-filename property to be set.

GtkPrintOperationResult

A value of this type is returned by gtkPrintOperationRun.

error

An error has occured.

apply

The print settings should be stored.

cancel

The print operation has been canceled, the print settings should not be stored.

in-progress

The print operation is not complete yet. This value will only be returned when running asynchronously.

GtkPrintError

Error codes that identify various errors that can occur while using the GTK+ printing support.

general

An unspecified error occurred.

internal-error

An internal error occurred.

nomem

A memory allocation failed.

User Functions

GtkPageSetupDoneFunc(page.setup, data)

The type of function that is passed to gtkPrintRunPageSetupDialogAsync. This function will be called when the page setup dialog is dismissed, and also serves as destroy notify for data.

page.setup

the GtkPageSetup that has been

data

user data that has been passed to gtkPrintRunPageSetupDialogAsync.

Signals

begin-print(operation, context, user.data)

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 GtkPrintContext and paginate the document accordingly, and then set the number of pages with gtkPrintOperationSetNPages. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

context

the GtkPrintContext for the current operation

user.data

user data set when the signal handler was connected.

create-custom-widget(operation, user.data)

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 "custom-widget-apply" signal is emitted on the operation. Then you can read out any information you need from the widgets. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

user.data

user data set when the signal handler was connected.

Returns: [GObject] A custom widget that gets embedded in the print dialog, or NULL

custom-widget-apply(operation, widget, user.data)

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

operation

the GtkPrintOperation on which the signal was emitted

widget

the custom widget added in create-custom-widget

user.data

user data set when the signal handler was connected.

done(operation, result, user.data)

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 GTK_PRINT_OPERATION_RESULT_ERROR then you can call gtkPrintOperationGetError for more information.

If you enabled print status tracking then gtkPrintOperationIsFinished may still return FALSE after "done" was emitted. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

result

the result of the print operation

user.data

user data set when the signal handler was connected.

draw-page(operation, context, page.nr, user.data)

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

draw_page <- (operation, context, page_nr, user_data)
{
  cr <- context$getCairoContext()
  width <- context$getWidth()
  
  cr$rectangle(0, 0, width, HEADER_HEIGHT)
  
  cr$setSourceRgb(0.8, 0.8, 0.8)
  cr$fill()
  
  layout <- context$createPangoLayout()
  
  desc <- pangoFontDescriptionFromString("sans 14")
  layout$setFontDescription(desc)
  
  layout$setText("some text")
  layout$setWidth(width)
  layout$setAlignment(layout, "center")
     		      
  layout_height <- layout$getSize()$height
  text_height <- layout_height / PANGO_SCALE
  
  cr$moveTo(width / 2,  (HEADER_HEIGHT - text_height) / 2)
  pangoCairoShowLayout(cr, layout)
}

Use gtkPrintOperationSetUseFullPage and gtkPrintOperationSetUnit before starting the print operation to set up the transformation of the cairo context according to your needs. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

context

the GtkPrintContext for the current operation

page.nr

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

user.data

user data set when the signal handler was connected.

end-print(operation, context, user.data)

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

operation

the GtkPrintOperation on which the signal was emitted

context

the GtkPrintContext for the current operation

user.data

user data set when the signal handler was connected.

paginate(operation, context, user.data)

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 gtkPrintOperationSetNPages, 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. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

context

the GtkPrintContext for the current operation

user.data

user data set when the signal handler was connected.

Returns: [logical] TRUE if pagination is complete

preview(operation, preview, context, parent, user.data)

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

The custom preview implementation can use gtkPrintOperationPreviewIsSelected and gtkPrintOperationPreviewRenderPage to find pages which are selected for print and render them. The preview must be finished by calling gtkPrintOperationPreviewEndPreview (typically in response to the user clicking a close button). Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

preview

the GtkPrintPreviewOperation for the current operation

context

the GtkPrintContext that will be used

parent

the GtkWindow to use as window parent, or NULL. [ allow-none ]

user.data

user data set when the signal handler was connected.

Returns: [logical] TRUE if the listener wants to take over control of the preview

request-page-setup(operation, context, page.nr, setup, user.data)

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

operation

the GtkPrintOperation on which the signal was emitted

context

the GtkPrintContext for the current operation

page.nr

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

setup

the GtkPageSetup

user.data

user data set when the signal handler was connected.

status-changed(operation, user.data)

Emitted at between the various phases of the print operation. See GtkPrintStatus for the phases that are being discriminated. Use gtkPrintOperationGetStatus to find out the current status. Since 2.10

operation

the GtkPrintOperation on which the signal was emitted

user.data

user data set when the signal handler was connected.

update-custom-widget(operation, widget, setup, settings, user.data)

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

operation

the GtkPrintOperation on which the signal was emitted

widget

the custom widget added in create-custom-widget

setup

actual page setup

settings

actual print settings

user.data

user data set when the signal handler was connected.

got-page-size(preview, context, page.setup, user.data)

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

A handler for this signal should update the context according to page.setup and set up a suitable cairo context, using gtkPrintContextSetCairoContext.

preview

the object on which the signal is emitted

context

the current GtkPrintContext

page.setup

the GtkPageSetup for the current page

user.data

user data set when the signal handler was connected.

ready(preview, context, user.data)

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

A handler for this signal can be used for setup tasks.

preview

the object on which the signal is emitted

context

the current GtkPrintContext

user.data

user data set when the signal handler was connected.

Properties

allow-async [logical : Read / Write]

Determines whether the print operation may run asynchronously or not.

Some systems don't support asynchronous printing, but those that do will return GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and emit the "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 GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation. Default value: FALSE Since 2.10

current-page [integer : Read / Write]

The current page in the document.

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

Note that this only makes sense for pre-paginated documents. Allowed values: >= -1 Default value: -1 Since 2.10

custom-tab-label [character : * : Read / Write]

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

If this is NULL, GTK+ uses a default label. Default value: NULL Since 2.10

default-page-setup [GtkPageSetup : * : Read / Write]

The GtkPageSetup used by default.

This page setup will be used by gtkPrintOperationRun, but it can be overridden on a per-page basis by connecting to the "request-page-setup" signal. Since 2.10

embed-page-setup [logical : Read / Write]

If TRUE, page size combo box and orientation combo box are embedded into page setup page. Default value: FALSE Since 2.18

export-filename [character : * : Read / Write]

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. Default value: NULL Since 2.10

has-selection [logical : Read / Write]

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. Default value: FALSE Since 2.18

job-name [character : * : Read / Write]

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. Default value: "" Since 2.10

n-pages [integer : Read / Write]

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 "begin-print" signal hander.

Note that the page numbers passed to the "request-page-setup" and "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. Allowed values: >= -1 Default value: -1 Since 2.10

n-pages-to-print [integer : Read]

The number of pages that will be printed.

Note that this value is set during print preparation phase (GTK_PRINT_STATUS_PREPARING), so this value should never be get before the data generation phase (GTK_PRINT_STATUS_GENERATING_DATA). You can connect to the "status-changed" signal and call gtkPrintOperationGetNPagesToPrint when print status is GTK_PRINT_STATUS_GENERATING_DATA. This is typically used to track the progress of print operation. Allowed values: >= -1 Default value: -1 Since 2.18

print-settings [GtkPrintSettings : * : Read / Write]

The GtkPrintSettings used for initializing the dialog.

Setting this property is typically used to re-establish print settings from a previous print operation, see gtkPrintOperationRun. Since 2.10

show-progress [logical : Read / Write]

Determines whether to show a progress dialog during the print operation. Default value: FALSE Since 2.10

status [GtkPrintStatus : Read]

The status of the print operation. Default value: GTK_PRINT_STATUS_INITIAL Since 2.10

status-string [character : * : Read]

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.

See the "status" property for a status value that is suitable for programmatic use. Default value: "" Since 2.10

support-selection [logical : Read / Write]

If TRUE, the print operation will support print of selection. This allows the print dialog to show a "Selection" button. Default value: FALSE Since 2.18

track-print-status [logical : Read / Write]

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. Default value: FALSE Since 2.10

unit [GtkUnit : Read / Write]

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

use-full-page [logical : Read / Write]

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). Default value: FALSE Since 2.10

Author(s)

Derived by RGtkGen from GTK+ documentation

References

https://developer.gnome.org/gtk2/stable/gtk2-High-level-Printing-API.html

See Also

GtkPrintContext


RGtk2 documentation built on Oct. 14, 2021, 5:08 p.m.