Class PrintOperation
- All Implemented Interfaces:
Proxy
,PrintOperationPreview
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 PrintUnixDialog
.
The typical way to use the high-level printing API is to create
a GtkPrintOperation
object with PrintOperation()
when the user selects to print. Then you set some properties on it,
e.g. the page size, any PrintSettings
from previous print
operations, the number of pages, the current page, etc.
Then you start the print operation by calling run(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
.
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
Gtk.PrintOperation::draw-page
, which you are supposed to handle
and render the page on the provided PrintContext
using Cairo.
The high-level printing API
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
PrintOperationPreview.renderPage(int)
,
PrintOperationPreview.endPreview()
and
PrintOperationPreview.isSelected(int)
are useful when implementing a print preview.
-
Nested Class Summary
Modifier and TypeClassDescriptionstatic interface
Functional interface declaration of theBeginPrintCallback
callback.static class
PrintOperation.Builder<B extends PrintOperation.Builder<B>>
Inner class implementing a builder pattern to construct a GObject with properties.static interface
Functional interface declaration of theCreateCustomWidgetCallback
callback.static interface
Functional interface declaration of theCustomWidgetApplyCallback
callback.static interface
Functional interface declaration of theDoneCallback
callback.static interface
Functional interface declaration of theDrawPageCallback
callback.static interface
Functional interface declaration of theEndPrintCallback
callback.static interface
Functional interface declaration of thePaginateCallback
callback.static interface
Functional interface declaration of thePreviewCallback
callback.static class
static interface
Functional interface declaration of theRequestPageSetupCallback
callback.static interface
Functional interface declaration of theStatusChangedCallback
callback.static interface
Functional interface declaration of theUpdateCustomWidgetCallback
callback.Nested classes/interfaces inherited from class org.gnome.gobject.GObject
GObject.NotifyCallback, GObject.ObjectClass
Nested classes/interfaces inherited from interface org.gnome.gtk.PrintOperationPreview
PrintOperationPreview.GotPageSizeCallback, PrintOperationPreview.PrintOperationPreviewIface, PrintOperationPreview.PrintOperationPreviewImpl, PrintOperationPreview.ReadyCallback
-
Constructor Summary
ConstructorDescriptionCreates a newGtkPrintOperation
.PrintOperation
(MemorySegment address) Create a PrintOperation proxy instance for the provided memory address. -
Method Summary
Modifier and TypeMethodDescriptionprotected PrintOperation
asParent()
Returns this instance as if it were its parent type.protected void
beginPrint
(PrintContext context) Signal emitted after the user has finished changing print settings in the dialog, before the actual rendering starts.static PrintOperation.Builder
<? extends PrintOperation.Builder> builder()
APrintOperation.Builder
object constructs aPrintOperation
with the specified properties.void
cancel()
Cancels a running print operation.protected Widget
Signal emitted when displaying the print dialog.protected void
customWidgetApply
(Widget widget) Signal emitted right before “begin-print” if you added a custom widget in the “create-custom-widget” handler.protected void
done
(PrintOperationResult result) Signal emitted when the print operation run has finished doing everything required for printing.protected void
drawPage
(PrintContext context, int pageNr) Signal emitted for every page that is printed.void
Signal that drawing of particular page is complete.void
emitBeginPrint
(PrintContext context) Emits the "begin-print" signal.Emits the "create-custom-widget" signal.void
emitCustomWidgetApply
(Widget widget) Emits the "custom-widget-apply" signal.void
emitDone
(PrintOperationResult result) Emits the "done" signal.void
emitDrawPage
(PrintContext context, int pageNr) Emits the "draw-page" signal.void
emitEndPrint
(PrintContext context) Emits the "end-print" signal.boolean
emitPaginate
(PrintContext context) Emits the "paginate" signal.boolean
emitPreview
(PrintOperationPreview preview, PrintContext context, @Nullable Window parent) Emits the "preview" signal.void
emitRequestPageSetup
(PrintContext context, int pageNr, PageSetup setup) Emits the "request-page-setup" signal.void
Emits the "status-changed" signal.void
emitUpdateCustomWidget
(Widget widget, PageSetup setup, PrintSettings settings) Emits the "update-custom-widget" signal.protected void
endPrint
(PrintContext context) Signal emitted after all pages have been rendered.Returns the default page setup.boolean
Gets whether page setup selection combos are embeddedvoid
getError()
Call this when the result of a print operation isPrintOperationResult.ERROR
.boolean
Gets whether there is a selection.static MemoryLayout
The memory layout of the native struct.int
Returns the number of pages that will be printed.Returns the current print settings.Returns the status of the print operation.Returns a string representation of the status of the print operation.boolean
Gets whether the application supports print of selectionstatic Type
getType()
Get the GType of the PrintOperation classboolean
A convenience function to find out if the print operation is finished.Emitted after the user has finished changing print settings in the dialog, before the actual rendering starts.Emitted when displaying the print dialog.Emitted right before ::begin-print if you added a custom widget in the ::create-custom-widget handler.onDone
(PrintOperation.DoneCallback handler) Emitted when the print operation run has finished doing everything required for printing.Emitted for every page that is printed.Emitted after all pages have been rendered.Emitted after the ::begin-print signal, but before the actual rendering starts.onPreview
(PrintOperation.PreviewCallback handler) Gets emitted when a preview is requested from the native dialog.Emitted once for every page that is printed.Emitted at between the various phases of the print operation.Emitted after change of selected printer.protected boolean
paginate
(PrintContext context) Signal emitted after the “begin-print” signal, but before the actual rendering starts.protected boolean
preview
(PrintOperationPreview preview, PrintContext context, Window parent) Signal emitted when a preview is requested from the native dialog.protected void
requestPageSetup
(PrintContext context, int pageNr, PageSetup setup) Emitted once for every page that is printed, to give the application a chance to modify the page setup.run
(PrintOperationAction action, @Nullable Window parent) Runs the print operation.void
setAllowAsync
(boolean allowAsync) Sets whether gtk_print_operation_run() may return before the print operation is completed.void
setCurrentPage
(int currentPage) Sets the current page.void
setCustomTabLabel
(@Nullable String label) Sets the label for the tab holding custom widgets.void
setDefaultPageSetup
(@Nullable PageSetup defaultPageSetup) MakesdefaultPageSetup
the default page setup for this PrintOperation.void
Sets up theGtkPrintOperation
to wait for calling ofPrintOperation#drawPageFinish 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::drawPage
signal.void
setEmbedPageSetup
(boolean embed) Embed page size combo box and orientation combo box into page setup page.void
setExportFilename
(String filename) Sets up theGtkPrintOperation
to generate a file instead of showing the print dialog.void
setHasSelection
(boolean hasSelection) Sets whether there is a selection to print.void
setJobName
(String jobName) Sets the name of the print job.void
setNPages
(int nPages) Sets the number of pages in the document.void
setPrintSettings
(@Nullable PrintSettings printSettings) Sets the print settings for this PrintOperation.void
setShowProgress
(boolean showProgress) IfshowProgress
istrue
, the print operation will show a progress dialog during the print operation.void
setSupportSelection
(boolean supportSelection) Sets whether selection is supported byGtkPrintOperation
.void
setTrackPrintStatus
(boolean trackStatus) If track_status istrue
, the print operation will try to continue report on the status of the print job in the printer queues and printer.void
Sets up the transformation for the cairo context obtained fromGtkPrintContext
in such a way that distances are measured in units ofunit
.void
setUseFullPage
(boolean fullPage) IffullPage
istrue
, the transformation for the cairo context obtained fromGtkPrintContext
puts the origin at the top left corner of the page.protected void
Emitted at between the various phases of the print operation.protected void
updateCustomWidget
(Widget widget, PageSetup setup, PrintSettings settings) Emitted after change of selected printer.Methods inherited from class org.gnome.gobject.GObject
addToggleRef, addWeakPointer, bindProperty, bindProperty, bindProperty, bindPropertyFull, bindPropertyFull, bindPropertyWithClosures, bindPropertyWithClosures, compatControl, connect, connect, connect, constructed, disconnect, dispatchPropertiesChanged, dispose, dupData, dupQdata, emit, emitNotify, finalize_, forceFloating, freezeNotify, get, getData, getProperty, getProperty, getProperty, getQdata, getv, interfaceFindProperty, interfaceInstallProperty, interfaceListProperties, isFloating, newInstance, newInstance, newInstance, newInstance, newv, notify_, notify_, notifyByPspec, onNotify, ref, refSink, removeToggleRef, removeWeakPointer, replaceData, replaceQdata, runDispose, set, setData, setDataFull, setProperty, setProperty, setProperty, setQdata, setQdataFull, setv, stealData, stealQdata, takeRef, thawNotify, unref, watchClosure, weakRef, weakUnref, withProperties
Methods inherited from class org.gnome.gobject.TypeInstance
callParent, callParent, getPrivate, readGClass, writeGClass
Methods inherited from class io.github.jwharm.javagi.base.ProxyInstance
equals, handle, hashCode
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, toString, wait, wait, wait
Methods inherited from interface org.gnome.gtk.PrintOperationPreview
emitGotPageSize, emitReady, endPreview, isSelected, onGotPageSize, onReady, renderPage
-
Constructor Details
-
PrintOperation
Create a PrintOperation proxy instance for the provided memory address.- Parameters:
address
- the memory address of the native object
-
PrintOperation
public PrintOperation()Creates a newGtkPrintOperation
.
-
-
Method Details
-
getType
-
getMemoryLayout
The memory layout of the native struct.- Returns:
- the memory layout
-
asParent
Returns this instance as if it were its parent type. This is mostly synonymous to the Javasuper
keyword, but will set the native typeclass function pointers to the parent type. When overriding a native virtual method in Java, "chaining up" withsuper.methodName()
doesn't work, because it invokes the overridden function pointer again. To chain up, callasParent().methodName()
. This will call the native function pointer of this virtual method in the typeclass of the parent type. -
cancel
public void cancel()Cancels a running print operation.This function may be called from a
Gtk.PrintOperation::begin-print
,Gtk.PrintOperation::paginate
orGtk.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
setDeferDrawing()
was called before, then this function has to be called by application. Otherwise it is called by GTK itself. -
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
Call this when the result of a print operation isPrintOperationResult.ERROR
.It can be called either after
run(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
returns, or in theGtk.PrintOperation::done
signal handler.The returned
GError
will contain more details on what went wrong.- Throws:
GErrorException
- seeGError
-
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 (
PrintStatus.PREPARING
), so this function should never be called before the data generation phase (PrintStatus.GENERATING_DATA
). You can connect to theGtk.PrintOperation::status-changed
signal and call gtk_print_operation_get_n_pages_to_print() when print status isPrintStatus.GENERATING_DATA
.This is typically used to track the progress of print operation.
- Returns:
- the number of pages that will be printed
-
getPrintSettings
Returns the current print settings.Note that the return value is
null
until eithersetPrintSettings(org.gnome.gtk.PrintSettings)
orrun(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
have been called.- Returns:
- the current print settings of this PrintOperation.
-
getStatus
Returns the status of the print operation.Also see
getStatusString()
.- Returns:
- the status of the print operation
-
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
getStatus()
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
PrintStatus.FINISHED
orPrintStatus.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 PrintOperationResult run(PrintOperationAction action, @Nullable @Nullable Window parent) throws GErrorException Runs the print operation.Normally that this function does not return until the rendering of all pages is complete. You can connect to the
Gtk.PrintOperation::status-changed
signal on this PrintOperation 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).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_window_present (GTK_WINDOW (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 startparent
- 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 withgetPrintSettings()
and store them for reuse with the next print operation. A value ofPrintOperationResult.IN_PROGRESS
means the operation is running asynchronously, and will emit theGtk.PrintOperation::done
signal when done. - Throws:
GErrorException
- seeGError
-
setAllowAsync
public void setAllowAsync(boolean allowAsync) Sets whether gtk_print_operation_run() may return before the print operation is completed.Note that some platforms may not allow asynchronous operation.
- Parameters:
allowAsync
-true
to allow asynchronous operation
-
setCurrentPage
public void setCurrentPage(int currentPage) Sets the current page.If this is called before
run(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
, the user will be able to select to print only the current page.Note that this only makes sense for pre-paginated documents.
- Parameters:
currentPage
- the current page, 0-based
-
setCustomTabLabel
Sets the label for the tab holding custom widgets.- Parameters:
label
- the label to use, ornull
to use the default label
-
setDefaultPageSetup
MakesdefaultPageSetup
the default page setup for this PrintOperation.This page setup will be used by
run(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
, but it can be overridden on a per-page basis by connecting to theGtk.PrintOperation::request-page-setup
signal.- Parameters:
defaultPageSetup
- aGtkPageSetup
-
setDeferDrawing
public void setDeferDrawing()Sets up theGtkPrintOperation
to wait for calling ofPrintOperation#drawPageFinish 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::drawPage
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 theGtkPrintUnixDialog
-
setExportFilename
Sets up theGtkPrintOperation
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 hasSelection) Sets whether there is a selection to print.Application has to set number of pages to which the selection will draw by
setNPages(int)
in a handler for theGtk.PrintOperation::begin-print
signal.- Parameters:
hasSelection
-true
indicates that a selection exists
-
setJobName
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:
jobName
- a string that identifies the print job
-
setNPages
public void setNPages(int nPages) 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
Gtk.PrintOperation::begin-print
signal handler.Note that the page numbers passed to the
Gtk.PrintOperation::request-page-setup
andGtk.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 pagenPages
- 1.- Parameters:
nPages
- the number of pages
-
setPrintSettings
Sets the print settings for this PrintOperation.This is typically used to re-establish print settings from a previous print operation, see
run(org.gnome.gtk.PrintOperationAction, org.gnome.gtk.Window)
.- Parameters:
printSettings
-GtkPrintSettings
-
setShowProgress
public void setShowProgress(boolean showProgress) IfshowProgress
istrue
, the print operation will show a progress dialog during the print operation.- Parameters:
showProgress
-true
to show a progress dialog
-
setSupportSelection
public void setSupportSelection(boolean supportSelection) Sets whether selection is supported byGtkPrintOperation
.- Parameters:
supportSelection
-true
to support selection
-
setTrackPrintStatus
public void setTrackPrintStatus(boolean trackStatus) If track_status istrue
, 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:
trackStatus
-true
to track status after printing
-
setUnit
Sets up the transformation for the cairo context obtained fromGtkPrintContext
in such a way that distances are measured in units ofunit
.- Parameters:
unit
- the unit to use
-
setUseFullPage
public void setUseFullPage(boolean fullPage) IffullPage
istrue
, the transformation for the cairo context obtained fromGtkPrintContext
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:
fullPage
-true
to set up theGtkPrintContext
for the full page
-
beginPrint
Signal emitted after the user has finished changing print settings in the dialog, before the actual rendering starts. -
createCustomWidget
Signal emitted when displaying the print dialog. -
customWidgetApply
Signal emitted right before “begin-print” if you added a custom widget in the “create-custom-widget” handler. -
done
Signal emitted when the print operation run has finished doing everything required for printing. -
drawPage
Signal emitted for every page that is printed. -
endPrint
Signal emitted after all pages have been rendered. -
paginate
Signal emitted after the “begin-print” signal, but before the actual rendering starts. -
preview
Signal emitted when a preview is requested from the native dialog. -
requestPageSetup
Emitted once for every page that is printed, to give the application a chance to modify the page setup. -
statusChanged
protected void statusChanged()Emitted at between the various phases of the print operation. -
updateCustomWidget
Emitted after change of selected printer. -
onBeginPrint
public SignalConnection<PrintOperation.BeginPrintCallback> onBeginPrint(PrintOperation.BeginPrintCallback handler) 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 withsetNPages(int)
.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitBeginPrint
Emits the "begin-print" signal. SeeonBeginPrint(org.gnome.gtk.PrintOperation.BeginPrintCallback)
. -
onCreateCustomWidget
public SignalConnection<PrintOperation.CreateCustomWidgetCallback> onCreateCustomWidget(PrintOperation.CreateCustomWidgetCallback handler) 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
Gtk.PrintOperation::custom-widget-apply
signal is emitted on the operation. Then you can read out any information you need from the widgets.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitCreateCustomWidget
Emits the "create-custom-widget" signal. SeeonCreateCustomWidget(org.gnome.gtk.PrintOperation.CreateCustomWidgetCallback)
. -
onCustomWidgetApply
public SignalConnection<PrintOperation.CustomWidgetApplyCallback> onCustomWidgetApply(PrintOperation.CustomWidgetApplyCallback handler) 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.
- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitCustomWidgetApply
Emits the "custom-widget-apply" signal. SeeonCustomWidgetApply(org.gnome.gtk.PrintOperation.CustomWidgetApplyCallback)
. -
onDone
Emitted when the print operation run has finished doing everything required for printing.result
gives you information about what happened during the run. Ifresult
isPrintOperationResult.ERROR
then you can callgetError()
for more information.If you enabled print status tracking then
isFinished()
may still returnfalse
after the ::done signal was emitted.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDone
Emits the "done" signal. SeeonDone(org.gnome.gtk.PrintOperation.DoneCallback)
. -
onDrawPage
public SignalConnection<PrintOperation.DrawPageCallback> onDrawPage(PrintOperation.DrawPageCallback handler) Emitted for every page that is printed.The signal handler must render the
pageNr
's page onto the cairo context obtained fromcontext
usingPrintContext.getCairoContext()
.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
setUseFullPage(boolean)
andsetUnit(org.gnome.gtk.Unit)
before starting the print operation to set up the transformation of the cairo context according to your needs.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitDrawPage
Emits the "draw-page" signal. SeeonDrawPage(org.gnome.gtk.PrintOperation.DrawPageCallback)
. -
onEndPrint
public SignalConnection<PrintOperation.EndPrintCallback> onEndPrint(PrintOperation.EndPrintCallback handler) Emitted after all pages have been rendered.A handler for this signal can clean up any resources that have been allocated in the
Gtk.PrintOperation::begin-print
handler.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitEndPrint
Emits the "end-print" signal. SeeonEndPrint(org.gnome.gtk.PrintOperation.EndPrintCallback)
. -
onPaginate
public SignalConnection<PrintOperation.PaginateCallback> onPaginate(PrintOperation.PaginateCallback handler) 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
setNPages(int)
, and returntrue
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.
- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitPaginate
Emits the "paginate" signal. SeeonPaginate(org.gnome.gtk.PrintOperation.PaginateCallback)
. -
onPreview
public SignalConnection<PrintOperation.PreviewCallback> onPreview(PrintOperation.PreviewCallback handler) 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 providedcontext
for the preview implementation, it must be given a suitable cairo context withPrintContext.setCairoContext(org.freedesktop.cairo.Context, double, double)
.The custom preview implementation can use
PrintOperationPreview.isSelected(int)
andPrintOperationPreview.renderPage(int)
to find pages which are selected for print and render them. The preview must be finished by callingPrintOperationPreview.endPreview()
(typically in response to the user clicking a close button).- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitPreview
public boolean emitPreview(PrintOperationPreview preview, PrintContext context, @Nullable @Nullable Window parent) Emits the "preview" signal. SeeonPreview(org.gnome.gtk.PrintOperation.PreviewCallback)
. -
onRequestPageSetup
public SignalConnection<PrintOperation.RequestPageSetupCallback> onRequestPageSetup(PrintOperation.RequestPageSetupCallback handler) 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.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitRequestPageSetup
Emits the "request-page-setup" signal. SeeonRequestPageSetup(org.gnome.gtk.PrintOperation.RequestPageSetupCallback)
. -
onStatusChanged
public SignalConnection<PrintOperation.StatusChangedCallback> onStatusChanged(PrintOperation.StatusChangedCallback handler) Emitted at between the various phases of the print operation.See
Gtk.PrintStatus
for the phases that are being discriminated. UsegetStatus()
to find out the current status.- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitStatusChanged
public void emitStatusChanged()Emits the "status-changed" signal. SeeonStatusChanged(org.gnome.gtk.PrintOperation.StatusChangedCallback)
. -
onUpdateCustomWidget
public SignalConnection<PrintOperation.UpdateCustomWidgetCallback> onUpdateCustomWidget(PrintOperation.UpdateCustomWidgetCallback handler) 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.
- Parameters:
handler
- the signal handler- Returns:
- a signal handler ID to keep track of the signal connection
- See Also:
-
emitUpdateCustomWidget
Emits the "update-custom-widget" signal. SeeonUpdateCustomWidget(org.gnome.gtk.PrintOperation.UpdateCustomWidgetCallback)
. -
builder
APrintOperation.Builder
object constructs aPrintOperation
with the specified properties. Use the variousset...()
methods to set properties, and finish construction withPrintOperation.Builder.build()
.
-