1 /* GTK - The GIMP Toolkit
2 * gtkprintoperation.c: Print Operation
3 * Copyright (C) 2006, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
26 #include <cairo-pdf.h>
28 #include "gtkprintoperation-private.h"
29 #include "gtkmarshalers.h"
31 #include "gtkprivate.h"
32 #include "gtkmessagedialog.h"
33 #include "gtktypebuiltins.h"
36 * SECTION:gtkprintoperation
37 * @Title: GtkPrintOperation
38 * @Short_description: High-level Printing API
39 * @See_also: #GtkPrintContext, #GtkPrintUnixDialog
41 * GtkPrintOperation is the high-level, portable printing API.
42 * It looks a bit different than other GTK+ dialogs such as the
43 * #GtkFileChooser, since some platforms don't expose enough
44 * infrastructure to implement a good print dialog. On such
45 * platforms, GtkPrintOperation uses the native print dialog.
46 * On platforms which do not provide a native print dialog, GTK+
47 * uses its own, see #GtkPrintUnixDialog.
49 * The typical way to use the high-level printing API is to create
50 * a GtkPrintOperation object with gtk_print_operation_new() when
51 * the user selects to print. Then you set some properties on it,
52 * e.g. the page size, any #GtkPrintSettings from previous print
53 * operations, the number of pages, the current page, etc.
55 * Then you start the print operation by calling gtk_print_operation_run().
56 * It will then show a dialog, let the user select a printer and
57 * options. When the user finished the dialog various signals will
58 * be emitted on the #GtkPrintOperation, the main one being
59 * #GtkPrintOperation::draw-page, which you are supposed to catch
60 * and render the page on the provided #GtkPrintContext using Cairo.
63 * <title>The high-level printing API</title>
65 * static GtkPrintSettings *settings = NULL;
70 * GtkPrintOperation *print;
71 * GtkPrintOperationResult res;
73 * print = gtk_print_operation_new ();
75 * if (settings != NULL)
76 * gtk_print_operation_set_print_settings (print, settings);
78 * g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), NULL);
79 * g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
81 * res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
82 * GTK_WINDOW (main_window), NULL);
84 * if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
86 * if (settings != NULL)
87 * g_object_unref (settings);
88 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
91 * g_object_unref (print);
96 * By default GtkPrintOperation uses an external application to do
97 * print preview. To implement a custom print preview, an application
98 * must connect to the preview signal. The functions
99 * gtk_print_operation_preview_render_page(),
100 * gtk_print_operation_preview_end_preview() and
101 * gtk_print_operation_preview_is_selected()
102 * are useful when implementing a print preview.
105 #define SHOW_PROGRESS_TIME 1200
117 CREATE_CUSTOM_WIDGET,
120 UPDATE_CUSTOM_WIDGET,
127 PROP_DEFAULT_PAGE_SETUP,
133 PROP_TRACK_PRINT_STATUS,
137 PROP_EXPORT_FILENAME,
140 PROP_CUSTOM_TAB_LABEL,
141 PROP_EMBED_PAGE_SETUP,
143 PROP_SUPPORT_SELECTION,
144 PROP_N_PAGES_TO_PRINT
147 static guint signals[LAST_SIGNAL] = { 0 };
148 static int job_nr = 0;
149 typedef struct _PrintPagesData PrintPagesData;
151 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
152 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
153 static void common_render_page (GtkPrintOperation *op,
155 static void increment_page_sequence (PrintPagesData *data);
156 static void prepare_data (PrintPagesData *data);
157 static void clamp_page_ranges (PrintPagesData *data);
160 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
161 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
165 * gtk_print_error_quark:
167 * Registers an error quark for #GtkPrintOperation if necessary.
169 * Return value: The error quark used for #GtkPrintOperation errors.
174 gtk_print_error_quark (void)
176 static GQuark quark = 0;
178 quark = g_quark_from_static_string ("gtk-print-error-quark");
183 gtk_print_operation_finalize (GObject *object)
185 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
186 GtkPrintOperationPrivate *priv = print_operation->priv;
188 if (priv->free_platform_data &&
191 priv->free_platform_data (priv->platform_data);
192 priv->free_platform_data = NULL;
195 if (priv->default_page_setup)
196 g_object_unref (priv->default_page_setup);
198 if (priv->print_settings)
199 g_object_unref (priv->print_settings);
201 if (priv->print_context)
202 g_object_unref (priv->print_context);
204 g_free (priv->export_filename);
205 g_free (priv->job_name);
206 g_free (priv->custom_tab_label);
207 g_free (priv->status_string);
209 if (priv->print_pages_idle_id > 0)
210 g_source_remove (priv->print_pages_idle_id);
212 if (priv->show_progress_timeout_id > 0)
213 g_source_remove (priv->show_progress_timeout_id);
216 g_error_free (priv->error);
218 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
222 gtk_print_operation_init (GtkPrintOperation *operation)
224 GtkPrintOperationPrivate *priv;
227 priv = operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
228 GTK_TYPE_PRINT_OPERATION,
229 GtkPrintOperationPrivate);
231 priv->status = GTK_PRINT_STATUS_INITIAL;
232 priv->status_string = g_strdup ("");
233 priv->default_page_setup = NULL;
234 priv->print_settings = NULL;
235 priv->nr_of_pages = -1;
236 priv->nr_of_pages_to_print = -1;
237 priv->page_position = -1;
238 priv->current_page = -1;
239 priv->use_full_page = FALSE;
240 priv->show_progress = FALSE;
241 priv->export_filename = NULL;
242 priv->track_print_status = FALSE;
243 priv->is_sync = FALSE;
244 priv->support_selection = FALSE;
245 priv->has_selection = FALSE;
246 priv->embed_page_setup = FALSE;
248 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
251 priv->unit = GTK_UNIT_NONE;
253 appname = g_get_application_name ();
256 /* translators: this string is the default job title for print
257 * jobs. %s gets replaced by the application name, %d gets replaced
260 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
264 preview_iface_render_page (GtkPrintOperationPreview *preview,
268 GtkPrintOperation *op;
270 op = GTK_PRINT_OPERATION (preview);
271 common_render_page (op, page_nr);
275 preview_iface_end_preview (GtkPrintOperationPreview *preview)
277 GtkPrintOperation *op;
278 GtkPrintOperationResult result;
280 op = GTK_PRINT_OPERATION (preview);
282 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
285 g_main_loop_quit (op->priv->rloop);
287 if (op->priv->end_run)
288 op->priv->end_run (op, op->priv->is_sync, TRUE);
290 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
293 result = GTK_PRINT_OPERATION_RESULT_ERROR;
294 else if (op->priv->cancelled)
295 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
297 result = GTK_PRINT_OPERATION_RESULT_APPLY;
299 g_signal_emit (op, signals[DONE], 0, result);
303 preview_iface_is_selected (GtkPrintOperationPreview *preview,
306 GtkPrintOperation *op;
307 GtkPrintOperationPrivate *priv;
310 op = GTK_PRINT_OPERATION (preview);
313 switch (priv->print_pages)
315 case GTK_PRINT_PAGES_SELECTION:
316 case GTK_PRINT_PAGES_ALL:
317 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
318 case GTK_PRINT_PAGES_CURRENT:
319 return page_nr == priv->current_page;
320 case GTK_PRINT_PAGES_RANGES:
321 for (i = 0; i < priv->num_page_ranges; i++)
323 if (page_nr >= priv->page_ranges[i].start &&
324 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
333 preview_iface_init (GtkPrintOperationPreviewIface *iface)
335 iface->render_page = preview_iface_render_page;
336 iface->end_preview = preview_iface_end_preview;
337 iface->is_selected = preview_iface_is_selected;
341 preview_start_page (GtkPrintOperation *op,
342 GtkPrintContext *print_context,
343 GtkPageSetup *page_setup)
345 if ((op->priv->manual_number_up < 2) ||
346 (op->priv->page_position % op->priv->manual_number_up == 0))
347 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
351 preview_end_page (GtkPrintOperation *op,
352 GtkPrintContext *print_context)
356 cr = gtk_print_context_get_cairo_context (print_context);
358 if ((op->priv->manual_number_up < 2) ||
359 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
360 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
361 cairo_show_page (cr);
365 preview_end_run (GtkPrintOperation *op,
369 g_free (op->priv->page_ranges);
370 op->priv->page_ranges = NULL;
375 gtk_print_operation_set_property (GObject *object,
380 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
384 case PROP_DEFAULT_PAGE_SETUP:
385 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
387 case PROP_PRINT_SETTINGS:
388 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
391 gtk_print_operation_set_job_name (op, g_value_get_string (value));
394 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
396 case PROP_CURRENT_PAGE:
397 gtk_print_operation_set_current_page (op, g_value_get_int (value));
399 case PROP_USE_FULL_PAGE:
400 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
402 case PROP_TRACK_PRINT_STATUS:
403 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
406 gtk_print_operation_set_unit (op, g_value_get_enum (value));
408 case PROP_ALLOW_ASYNC:
409 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
411 case PROP_SHOW_PROGRESS:
412 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
414 case PROP_EXPORT_FILENAME:
415 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
417 case PROP_CUSTOM_TAB_LABEL:
418 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
420 case PROP_EMBED_PAGE_SETUP:
421 gtk_print_operation_set_embed_page_setup (op, g_value_get_boolean (value));
423 case PROP_HAS_SELECTION:
424 gtk_print_operation_set_has_selection (op, g_value_get_boolean (value));
426 case PROP_SUPPORT_SELECTION:
427 gtk_print_operation_set_support_selection (op, g_value_get_boolean (value));
430 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
436 gtk_print_operation_get_property (GObject *object,
441 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
442 GtkPrintOperationPrivate *priv = op->priv;
446 case PROP_DEFAULT_PAGE_SETUP:
447 g_value_set_object (value, priv->default_page_setup);
449 case PROP_PRINT_SETTINGS:
450 g_value_set_object (value, priv->print_settings);
453 g_value_set_string (value, priv->job_name);
456 g_value_set_int (value, priv->nr_of_pages);
458 case PROP_CURRENT_PAGE:
459 g_value_set_int (value, priv->current_page);
461 case PROP_USE_FULL_PAGE:
462 g_value_set_boolean (value, priv->use_full_page);
464 case PROP_TRACK_PRINT_STATUS:
465 g_value_set_boolean (value, priv->track_print_status);
468 g_value_set_enum (value, priv->unit);
470 case PROP_ALLOW_ASYNC:
471 g_value_set_boolean (value, priv->allow_async);
473 case PROP_SHOW_PROGRESS:
474 g_value_set_boolean (value, priv->show_progress);
476 case PROP_EXPORT_FILENAME:
477 g_value_set_string (value, priv->export_filename);
480 g_value_set_enum (value, priv->status);
482 case PROP_STATUS_STRING:
483 g_value_set_string (value, priv->status_string);
485 case PROP_CUSTOM_TAB_LABEL:
486 g_value_set_string (value, priv->custom_tab_label);
488 case PROP_EMBED_PAGE_SETUP:
489 g_value_set_boolean (value, priv->embed_page_setup);
491 case PROP_HAS_SELECTION:
492 g_value_set_boolean (value, priv->has_selection);
494 case PROP_SUPPORT_SELECTION:
495 g_value_set_boolean (value, priv->support_selection);
497 case PROP_N_PAGES_TO_PRINT:
498 g_value_set_int (value, priv->nr_of_pages_to_print);
501 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
506 struct _PrintPagesData
508 GtkPrintOperation *op;
509 gint uncollated_copies;
510 gint collated_copies;
511 gint uncollated, collated, total;
513 gint range, num_ranges;
514 GtkPageRange *ranges;
515 GtkPageRange one_range;
519 gint first_position, last_position;
526 gboolean initialized;
533 GtkPrintOperationPreview *preview;
534 GtkPrintContext *print_context;
536 cairo_surface_t *surface;
539 PrintPagesData *pages_data;
543 preview_print_idle_done (gpointer data)
545 GtkPrintOperation *op;
546 PreviewOp *pop = (PreviewOp *) data;
548 op = GTK_PRINT_OPERATION (pop->preview);
550 cairo_surface_finish (pop->surface);
552 if (op->priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED)
554 cairo_surface_destroy (pop->surface);
558 /* Surface is destroyed in launch_preview */
559 _gtk_print_operation_platform_backend_launch_preview (op,
565 g_free (pop->filename);
567 gtk_print_operation_preview_end_preview (pop->preview);
569 g_object_unref (pop->pages_data->op);
570 g_free (pop->pages_data->pages);
571 g_free (pop->pages_data);
578 preview_print_idle (gpointer data)
581 GtkPrintOperation *op;
582 GtkPrintOperationPrivate *priv;
583 gboolean done = FALSE;
585 pop = (PreviewOp *) data;
586 op = GTK_PRINT_OPERATION (pop->preview);
589 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
594 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
596 else if (!pop->pages_data->initialized)
598 pop->pages_data->initialized = TRUE;
599 prepare_data (pop->pages_data);
603 increment_page_sequence (pop->pages_data);
605 if (!pop->pages_data->done)
606 gtk_print_operation_preview_render_page (pop->preview, pop->pages_data->page);
608 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
616 preview_got_page_size (GtkPrintOperationPreview *preview,
617 GtkPrintContext *context,
618 GtkPageSetup *page_setup,
621 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
624 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
626 cr = gtk_print_context_get_cairo_context (pop->print_context);
627 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
632 preview_ready (GtkPrintOperationPreview *preview,
633 GtkPrintContext *context,
636 pop->print_context = context;
638 g_object_ref (preview);
640 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
643 preview_print_idle_done);
648 gtk_print_operation_preview_handler (GtkPrintOperation *op,
649 GtkPrintOperationPreview *preview,
650 GtkPrintContext *context,
653 gdouble dpi_x, dpi_y;
655 GtkPageSetup *page_setup;
658 pop = g_new0 (PreviewOp, 1);
659 pop->filename = NULL;
660 pop->preview = preview;
661 pop->parent = parent;
662 pop->pages_data = g_new0 (PrintPagesData, 1);
663 pop->pages_data->op = g_object_ref (GTK_PRINT_OPERATION (preview));
664 pop->pages_data->is_preview = TRUE;
666 page_setup = gtk_print_context_get_page_setup (context);
669 _gtk_print_operation_platform_backend_create_preview_surface (op,
674 if (pop->surface == NULL)
680 cr = cairo_create (pop->surface);
681 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
685 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
686 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
692 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
698 gtk_print_operation_done (GtkPrintOperation *operation,
699 GtkPrintOperationResult result)
701 GtkPrintOperationPrivate *priv = operation->priv;
703 if (priv->print_context)
705 g_object_unref (priv->print_context);
706 priv->print_context = NULL;
711 custom_widget_accumulator (GSignalInvocationHint *ihint,
713 const GValue *handler_return,
716 gboolean continue_emission;
719 widget = g_value_get_object (handler_return);
721 g_value_set_object (return_accu, widget);
722 continue_emission = (widget == NULL);
724 return continue_emission;
728 gtk_print_operation_class_init (GtkPrintOperationClass *class)
730 GObjectClass *gobject_class = (GObjectClass *)class;
732 gobject_class->set_property = gtk_print_operation_set_property;
733 gobject_class->get_property = gtk_print_operation_get_property;
734 gobject_class->finalize = gtk_print_operation_finalize;
736 class->preview = gtk_print_operation_preview_handler;
737 class->create_custom_widget = gtk_print_operation_create_custom_widget;
738 class->done = gtk_print_operation_done;
740 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
743 * GtkPrintOperation::done:
744 * @operation: the #GtkPrintOperation on which the signal was emitted
745 * @result: the result of the print operation
747 * Emitted when the print operation run has finished doing
748 * everything required for printing.
750 * @result gives you information about what happened during the run.
751 * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
752 * gtk_print_operation_get_error() for more information.
754 * If you enabled print status tracking then
755 * gtk_print_operation_is_finished() may still return %FALSE
756 * after #GtkPrintOperation::done was emitted.
761 g_signal_new (I_("done"),
762 G_TYPE_FROM_CLASS (gobject_class),
764 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
766 g_cclosure_marshal_VOID__ENUM,
767 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
770 * GtkPrintOperation::begin-print:
771 * @operation: the #GtkPrintOperation on which the signal was emitted
772 * @context: the #GtkPrintContext for the current operation
774 * Emitted after the user has finished changing print settings
775 * in the dialog, before the actual rendering starts.
777 * A typical use for ::begin-print is to use the parameters from the
778 * #GtkPrintContext and paginate the document accordingly, and then
779 * set the number of pages with gtk_print_operation_set_n_pages().
783 signals[BEGIN_PRINT] =
784 g_signal_new (I_("begin-print"),
785 G_TYPE_FROM_CLASS (gobject_class),
787 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
789 g_cclosure_marshal_VOID__OBJECT,
790 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
793 * GtkPrintOperation::paginate:
794 * @operation: the #GtkPrintOperation on which the signal was emitted
795 * @context: the #GtkPrintContext for the current operation
797 * Emitted after the #GtkPrintOperation::begin-print signal, but before
798 * the actual rendering starts. It keeps getting emitted until a connected
799 * signal handler returns %TRUE.
801 * The ::paginate signal is intended to be used for paginating a document
802 * in small chunks, to avoid blocking the user interface for a long
803 * time. The signal handler should update the number of pages using
804 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
805 * has been completely paginated.
807 * If you don't need to do pagination in chunks, you can simply do
808 * it all in the ::begin-print handler, and set the number of pages
811 * Return value: %TRUE if pagination is complete
816 g_signal_new (I_("paginate"),
817 G_TYPE_FROM_CLASS (gobject_class),
819 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
820 _gtk_boolean_handled_accumulator, NULL,
821 _gtk_marshal_BOOLEAN__OBJECT,
822 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
826 * GtkPrintOperation::request-page-setup:
827 * @operation: the #GtkPrintOperation on which the signal was emitted
828 * @context: the #GtkPrintContext for the current operation
829 * @page_nr: the number of the currently printed page (0-based)
830 * @setup: the #GtkPageSetup
832 * Emitted once for every page that is printed, to give
833 * the application a chance to modify the page setup. Any changes
834 * done to @setup will be in force only for printing this page.
838 signals[REQUEST_PAGE_SETUP] =
839 g_signal_new (I_("request-page-setup"),
840 G_TYPE_FROM_CLASS (gobject_class),
842 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
844 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
846 GTK_TYPE_PRINT_CONTEXT,
848 GTK_TYPE_PAGE_SETUP);
851 * GtkPrintOperation::draw-page:
852 * @operation: the #GtkPrintOperation on which the signal was emitted
853 * @context: the #GtkPrintContext for the current operation
854 * @page_nr: the number of the currently printed page (0-based)
856 * Emitted for every page that is printed. The signal handler
857 * must render the @page_nr's page onto the cairo context obtained
858 * from @context using gtk_print_context_get_cairo_context().
861 * draw_page (GtkPrintOperation *operation,
862 * GtkPrintContext *context,
864 * gpointer user_data)
867 * PangoLayout *layout;
868 * gdouble width, text_height;
869 * gint layout_height;
870 * PangoFontDescription *desc;
872 * cr = gtk_print_context_get_cairo_context (context);
873 * width = gtk_print_context_get_width (context);
875 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
877 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
880 * layout = gtk_print_context_create_pango_layout (context);
882 * desc = pango_font_description_from_string ("sans 14");
883 * pango_layout_set_font_description (layout, desc);
884 * pango_font_description_free (desc);
886 * pango_layout_set_text (layout, "some text", -1);
887 * pango_layout_set_width (layout, width * PANGO_SCALE);
888 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
890 * pango_layout_get_size (layout, NULL, &layout_height);
891 * text_height = (gdouble)layout_height / PANGO_SCALE;
893 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
894 * pango_cairo_show_layout (cr, layout);
896 * g_object_unref (layout);
900 * Use gtk_print_operation_set_use_full_page() and
901 * gtk_print_operation_set_unit() before starting the print operation
902 * to set up the transformation of the cairo context according to your
908 g_signal_new (I_("draw-page"),
909 G_TYPE_FROM_CLASS (gobject_class),
911 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
913 _gtk_marshal_VOID__OBJECT_INT,
915 GTK_TYPE_PRINT_CONTEXT,
919 * GtkPrintOperation::end-print:
920 * @operation: the #GtkPrintOperation on which the signal was emitted
921 * @context: the #GtkPrintContext for the current operation
923 * Emitted after all pages have been rendered.
924 * A handler for this signal can clean up any resources that have
925 * been allocated in the #GtkPrintOperation::begin-print handler.
930 g_signal_new (I_("end-print"),
931 G_TYPE_FROM_CLASS (gobject_class),
933 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
935 g_cclosure_marshal_VOID__OBJECT,
936 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
939 * GtkPrintOperation::status-changed:
940 * @operation: the #GtkPrintOperation on which the signal was emitted
942 * Emitted at between the various phases of the print operation.
943 * See #GtkPrintStatus for the phases that are being discriminated.
944 * Use gtk_print_operation_get_status() to find out the current
949 signals[STATUS_CHANGED] =
950 g_signal_new (I_("status-changed"),
951 G_TYPE_FROM_CLASS (class),
953 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
955 g_cclosure_marshal_VOID__VOID,
960 * GtkPrintOperation::create-custom-widget:
961 * @operation: the #GtkPrintOperation on which the signal was emitted
963 * Emitted when displaying the print dialog. If you return a
964 * widget in a handler for this signal it will be added to a custom
965 * tab in the print dialog. You typically return a container widget
966 * with multiple widgets in it.
968 * The print dialog owns the returned widget, and its lifetime is not
969 * controlled by the application. However, the widget is guaranteed
970 * to stay around until the #GtkPrintOperation::custom-widget-apply
971 * signal is emitted on the operation. Then you can read out any
972 * information you need from the widgets.
974 * Returns: (transfer none): A custom widget that gets embedded in
975 * the print dialog, or %NULL
979 signals[CREATE_CUSTOM_WIDGET] =
980 g_signal_new (I_("create-custom-widget"),
981 G_TYPE_FROM_CLASS (class),
983 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
984 custom_widget_accumulator, NULL,
985 _gtk_marshal_OBJECT__VOID,
989 * GtkPrintOperation::update-custom-widget:
990 * @operation: the #GtkPrintOperation on which the signal was emitted
991 * @widget: the custom widget added in create-custom-widget
992 * @setup: actual page setup
993 * @settings: actual print settings
995 * Emitted after change of selected printer. The actual page setup and
996 * print settings are passed to the custom widget, which can actualize
997 * itself according to this change.
1001 signals[UPDATE_CUSTOM_WIDGET] =
1002 g_signal_new (I_("update-custom-widget"),
1003 G_TYPE_FROM_CLASS (class),
1005 G_STRUCT_OFFSET (GtkPrintOperationClass, update_custom_widget),
1007 _gtk_marshal_VOID__OBJECT_OBJECT_OBJECT,
1008 G_TYPE_NONE, 3, GTK_TYPE_WIDGET, GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);
1011 * GtkPrintOperation::custom-widget-apply:
1012 * @operation: the #GtkPrintOperation on which the signal was emitted
1013 * @widget: the custom widget added in create-custom-widget
1015 * Emitted right before #GtkPrintOperation::begin-print if you added
1016 * a custom widget in the #GtkPrintOperation::create-custom-widget handler.
1017 * When you get this signal you should read the information from the
1018 * custom widgets, as the widgets are not guaraneed to be around at a
1023 signals[CUSTOM_WIDGET_APPLY] =
1024 g_signal_new (I_("custom-widget-apply"),
1025 G_TYPE_FROM_CLASS (class),
1027 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
1029 g_cclosure_marshal_VOID__OBJECT,
1030 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
1033 * GtkPrintOperation::preview:
1034 * @operation: the #GtkPrintOperation on which the signal was emitted
1035 * @preview: the #GtkPrintOperationPreview for the current operation
1036 * @context: the #GtkPrintContext that will be used
1037 * @parent: (allow-none): the #GtkWindow to use as window parent, or %NULL
1039 * Gets emitted when a preview is requested from the native dialog.
1041 * The default handler for this signal uses an external viewer
1042 * application to preview.
1044 * To implement a custom print preview, an application must return
1045 * %TRUE from its handler for this signal. In order to use the
1046 * provided @context for the preview implementation, it must be
1047 * given a suitable cairo context with gtk_print_context_set_cairo_context().
1049 * The custom preview implementation can use
1050 * gtk_print_operation_preview_is_selected() and
1051 * gtk_print_operation_preview_render_page() to find pages which
1052 * are selected for print and render them. The preview must be
1053 * finished by calling gtk_print_operation_preview_end_preview()
1054 * (typically in response to the user clicking a close button).
1056 * Returns: %TRUE if the listener wants to take over control of the preview
1061 g_signal_new (I_("preview"),
1062 G_TYPE_FROM_CLASS (gobject_class),
1064 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
1065 _gtk_boolean_handled_accumulator, NULL,
1066 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
1068 GTK_TYPE_PRINT_OPERATION_PREVIEW,
1069 GTK_TYPE_PRINT_CONTEXT,
1074 * GtkPrintOperation:default-page-setup:
1076 * The #GtkPageSetup used by default.
1078 * This page setup will be used by gtk_print_operation_run(),
1079 * but it can be overridden on a per-page basis by connecting
1080 * to the #GtkPrintOperation::request-page-setup signal.
1084 g_object_class_install_property (gobject_class,
1085 PROP_DEFAULT_PAGE_SETUP,
1086 g_param_spec_object ("default-page-setup",
1087 P_("Default Page Setup"),
1088 P_("The GtkPageSetup used by default"),
1089 GTK_TYPE_PAGE_SETUP,
1090 GTK_PARAM_READWRITE));
1093 * GtkPrintOperation:print-settings:
1095 * The #GtkPrintSettings used for initializing the dialog.
1097 * Setting this property is typically used to re-establish
1098 * print settings from a previous print operation, see
1099 * gtk_print_operation_run().
1103 g_object_class_install_property (gobject_class,
1104 PROP_PRINT_SETTINGS,
1105 g_param_spec_object ("print-settings",
1106 P_("Print Settings"),
1107 P_("The GtkPrintSettings used for initializing the dialog"),
1108 GTK_TYPE_PRINT_SETTINGS,
1109 GTK_PARAM_READWRITE));
1112 * GtkPrintOperation:job-name:
1114 * A string used to identify the job (e.g. in monitoring
1115 * applications like eggcups).
1117 * If you don't set a job name, GTK+ picks a default one
1118 * by numbering successive print jobs.
1122 g_object_class_install_property (gobject_class,
1124 g_param_spec_string ("job-name",
1126 P_("A string used for identifying the print job."),
1128 GTK_PARAM_READWRITE));
1131 * GtkPrintOperation:n-pages:
1133 * The number of pages in the document.
1135 * This <emphasis>must</emphasis> be set to a positive number
1136 * before the rendering starts. It may be set in a
1137 * #GtkPrintOperation::begin-print signal hander.
1139 * Note that the page numbers passed to the
1140 * #GtkPrintOperation::request-page-setup and
1141 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1142 * the user chooses to print all pages, the last ::draw-page signal
1143 * will be for page @n_pages - 1.
1147 g_object_class_install_property (gobject_class,
1149 g_param_spec_int ("n-pages",
1150 P_("Number of Pages"),
1151 P_("The number of pages in the document."),
1155 GTK_PARAM_READWRITE));
1158 * GtkPrintOperation:current-page:
1160 * The current page in the document.
1162 * If this is set before gtk_print_operation_run(),
1163 * the user will be able to select to print only the current page.
1165 * Note that this only makes sense for pre-paginated documents.
1169 g_object_class_install_property (gobject_class,
1171 g_param_spec_int ("current-page",
1173 P_("The current page in the document"),
1177 GTK_PARAM_READWRITE));
1180 * GtkPrintOperation:use-full-page:
1182 * If %TRUE, the transformation for the cairo context obtained
1183 * from #GtkPrintContext puts the origin at the top left corner
1184 * of the page (which may not be the top left corner of the sheet,
1185 * depending on page orientation and the number of pages per sheet).
1186 * Otherwise, the origin is at the top left corner of the imageable
1187 * area (i.e. inside the margins).
1191 g_object_class_install_property (gobject_class,
1193 g_param_spec_boolean ("use-full-page",
1194 P_("Use full page"),
1195 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
1197 GTK_PARAM_READWRITE));
1201 * GtkPrintOperation:track-print-status:
1203 * If %TRUE, the print operation will try to continue report on
1204 * the status of the print job in the printer queues and printer.
1205 * This can allow your application to show things like "out of paper"
1206 * issues, and when the print job actually reaches the printer.
1207 * However, this is often implemented using polling, and should
1208 * not be enabled unless needed.
1212 g_object_class_install_property (gobject_class,
1213 PROP_TRACK_PRINT_STATUS,
1214 g_param_spec_boolean ("track-print-status",
1215 P_("Track Print Status"),
1216 P_("TRUE if the print operation will continue to report on the print job status after the print data has been sent to the printer or print server."),
1218 GTK_PARAM_READWRITE));
1222 * GtkPrintOperation:unit:
1224 * The transformation for the cairo context obtained from
1225 * #GtkPrintContext is set up in such a way that distances
1226 * are measured in units of @unit.
1230 g_object_class_install_property (gobject_class,
1232 g_param_spec_enum ("unit",
1234 P_("The unit in which distances can be measured in the context"),
1237 GTK_PARAM_READWRITE));
1241 * GtkPrintOperation:show-progress:
1243 * Determines whether to show a progress dialog during the
1248 g_object_class_install_property (gobject_class,
1250 g_param_spec_boolean ("show-progress",
1252 P_("TRUE if a progress dialog is shown while printing."),
1254 GTK_PARAM_READWRITE));
1257 * GtkPrintOperation:allow-async:
1259 * Determines whether the print operation may run asynchronously or not.
1261 * Some systems don't support asynchronous printing, but those that do
1262 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1263 * emit the #GtkPrintOperation::done signal when the operation is actually
1266 * The Windows port does not support asynchronous operation at all (this
1267 * is unlikely to change). On other platforms, all actions except for
1268 * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
1272 g_object_class_install_property (gobject_class,
1274 g_param_spec_boolean ("allow-async",
1276 P_("TRUE if print process may run asynchronous."),
1278 GTK_PARAM_READWRITE));
1281 * GtkPrintOperation:export-filename:
1283 * The name of a file to generate instead of showing the print dialog.
1284 * Currently, PDF is the only supported format.
1286 * The intended use of this property is for implementing
1287 * "Export to PDF" actions.
1289 * "Print to PDF" support is independent of this and is done
1290 * by letting the user pick the "Print to PDF" item from the
1291 * list of printers in the print dialog.
1295 g_object_class_install_property (gobject_class,
1296 PROP_EXPORT_FILENAME,
1297 g_param_spec_string ("export-filename",
1298 P_("Export filename"),
1299 P_("Export filename"),
1301 GTK_PARAM_READWRITE));
1304 * GtkPrintOperation:status:
1306 * The status of the print operation.
1310 g_object_class_install_property (gobject_class,
1312 g_param_spec_enum ("status",
1314 P_("The status of the print operation"),
1315 GTK_TYPE_PRINT_STATUS,
1316 GTK_PRINT_STATUS_INITIAL,
1317 GTK_PARAM_READABLE));
1320 * GtkPrintOperation:status-string:
1322 * A string representation of the status of the print operation.
1323 * The string is translated and suitable for displaying the print
1324 * status e.g. in a #GtkStatusbar.
1326 * See the #GtkPrintOperation:status property for a status value that
1327 * is suitable for programmatic use.
1331 g_object_class_install_property (gobject_class,
1333 g_param_spec_string ("status-string",
1334 P_("Status String"),
1335 P_("A human-readable description of the status"),
1337 GTK_PARAM_READABLE));
1341 * GtkPrintOperation:custom-tab-label:
1343 * Used as the label of the tab containing custom widgets.
1344 * Note that this property may be ignored on some platforms.
1346 * If this is %NULL, GTK+ uses a default label.
1350 g_object_class_install_property (gobject_class,
1351 PROP_CUSTOM_TAB_LABEL,
1352 g_param_spec_string ("custom-tab-label",
1353 P_("Custom tab label"),
1354 P_("Label for the tab containing custom widgets."),
1356 GTK_PARAM_READWRITE));
1359 * GtkPrintOperation:support-selection:
1361 * If %TRUE, the print operation will support print of selection.
1362 * This allows the print dialog to show a "Selection" button.
1366 g_object_class_install_property (gobject_class,
1367 PROP_SUPPORT_SELECTION,
1368 g_param_spec_boolean ("support-selection",
1369 P_("Support Selection"),
1370 P_("TRUE if the print operation will support print of selection."),
1372 GTK_PARAM_READWRITE));
1375 * GtkPrintOperation:has-selection:
1377 * Determines whether there is a selection in your application.
1378 * This can allow your application to print the selection.
1379 * This is typically used to make a "Selection" button sensitive.
1383 g_object_class_install_property (gobject_class,
1385 g_param_spec_boolean ("has-selection",
1386 P_("Has Selection"),
1387 P_("TRUE if a selection exists."),
1389 GTK_PARAM_READWRITE));
1393 * GtkPrintOperation:embed-page-setup:
1395 * If %TRUE, page size combo box and orientation combo box are embedded into page setup page.
1399 g_object_class_install_property (gobject_class,
1400 PROP_EMBED_PAGE_SETUP,
1401 g_param_spec_boolean ("embed-page-setup",
1402 P_("Embed Page Setup"),
1403 P_("TRUE if page setup combos are embedded in GtkPrintUnixDialog"),
1405 GTK_PARAM_READWRITE));
1407 * GtkPrintOperation:n-pages-to-print:
1409 * The number of pages that will be printed.
1411 * Note that this value is set during print preparation phase
1412 * (%GTK_PRINT_STATUS_PREPARING), so this value should never be
1413 * get before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
1414 * You can connect to the #GtkPrintOperation::status-changed signal
1415 * and call gtk_print_operation_get_n_pages_to_print() when
1416 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
1417 * This is typically used to track the progress of print operation.
1421 g_object_class_install_property (gobject_class,
1422 PROP_N_PAGES_TO_PRINT,
1423 g_param_spec_int ("n-pages-to-print",
1424 P_("Number of Pages To Print"),
1425 P_("The number of pages that will be printed."),
1429 GTK_PARAM_READABLE));
1433 * gtk_print_operation_new:
1435 * Creates a new #GtkPrintOperation.
1437 * Returns: a new #GtkPrintOperation
1442 gtk_print_operation_new (void)
1444 GtkPrintOperation *print_operation;
1446 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1448 return print_operation;
1452 * gtk_print_operation_set_default_page_setup:
1453 * @op: a #GtkPrintOperation
1454 * @default_page_setup: (allow-none): a #GtkPageSetup, or %NULL
1456 * Makes @default_page_setup the default page setup for @op.
1458 * This page setup will be used by gtk_print_operation_run(),
1459 * but it can be overridden on a per-page basis by connecting
1460 * to the #GtkPrintOperation::request-page-setup signal.
1465 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1466 GtkPageSetup *default_page_setup)
1468 GtkPrintOperationPrivate *priv;
1470 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1471 g_return_if_fail (default_page_setup == NULL ||
1472 GTK_IS_PAGE_SETUP (default_page_setup));
1476 if (default_page_setup != priv->default_page_setup)
1478 if (default_page_setup)
1479 g_object_ref (default_page_setup);
1481 if (priv->default_page_setup)
1482 g_object_unref (priv->default_page_setup);
1484 priv->default_page_setup = default_page_setup;
1486 g_object_notify (G_OBJECT (op), "default-page-setup");
1491 * gtk_print_operation_get_default_page_setup:
1492 * @op: a #GtkPrintOperation
1494 * Returns the default page setup, see
1495 * gtk_print_operation_set_default_page_setup().
1497 * Returns: (transfer none): the default page setup
1502 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1504 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1506 return op->priv->default_page_setup;
1511 * gtk_print_operation_set_print_settings:
1512 * @op: a #GtkPrintOperation
1513 * @print_settings: (allow-none): #GtkPrintSettings
1515 * Sets the print settings for @op. This is typically used to
1516 * re-establish print settings from a previous print operation,
1517 * see gtk_print_operation_run().
1522 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1523 GtkPrintSettings *print_settings)
1525 GtkPrintOperationPrivate *priv;
1527 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1528 g_return_if_fail (print_settings == NULL ||
1529 GTK_IS_PRINT_SETTINGS (print_settings));
1533 if (print_settings != priv->print_settings)
1536 g_object_ref (print_settings);
1538 if (priv->print_settings)
1539 g_object_unref (priv->print_settings);
1541 priv->print_settings = print_settings;
1543 g_object_notify (G_OBJECT (op), "print-settings");
1548 * gtk_print_operation_get_print_settings:
1549 * @op: a #GtkPrintOperation
1551 * Returns the current print settings.
1553 * Note that the return value is %NULL until either
1554 * gtk_print_operation_set_print_settings() or
1555 * gtk_print_operation_run() have been called.
1557 * Return value: (transfer none): the current print settings of @op.
1562 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1564 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1566 return op->priv->print_settings;
1570 * gtk_print_operation_set_job_name:
1571 * @op: a #GtkPrintOperation
1572 * @job_name: a string that identifies the print job
1574 * Sets the name of the print job. The name is used to identify
1575 * the job (e.g. in monitoring applications like eggcups).
1577 * If you don't set a job name, GTK+ picks a default one by
1578 * numbering successive print jobs.
1583 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1584 const gchar *job_name)
1586 GtkPrintOperationPrivate *priv;
1588 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1589 g_return_if_fail (job_name != NULL);
1593 g_free (priv->job_name);
1594 priv->job_name = g_strdup (job_name);
1596 g_object_notify (G_OBJECT (op), "job-name");
1600 * gtk_print_operation_set_n_pages:
1601 * @op: a #GtkPrintOperation
1602 * @n_pages: the number of pages
1604 * Sets the number of pages in the document.
1606 * This <emphasis>must</emphasis> be set to a positive number
1607 * before the rendering starts. It may be set in a
1608 * #GtkPrintOperation::begin-print signal hander.
1610 * Note that the page numbers passed to the
1611 * #GtkPrintOperation::request-page-setup
1612 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1613 * the user chooses to print all pages, the last ::draw-page signal
1614 * will be for page @n_pages - 1.
1619 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1622 GtkPrintOperationPrivate *priv;
1624 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1625 g_return_if_fail (n_pages > 0);
1628 g_return_if_fail (priv->current_page == -1 ||
1629 priv->current_page < n_pages);
1631 if (priv->nr_of_pages != n_pages)
1633 priv->nr_of_pages = n_pages;
1635 g_object_notify (G_OBJECT (op), "n-pages");
1640 * gtk_print_operation_set_current_page:
1641 * @op: a #GtkPrintOperation
1642 * @current_page: the current page, 0-based
1644 * Sets the current page.
1646 * If this is called before gtk_print_operation_run(),
1647 * the user will be able to select to print only the current page.
1649 * Note that this only makes sense for pre-paginated documents.
1654 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1657 GtkPrintOperationPrivate *priv;
1659 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1660 g_return_if_fail (current_page >= 0);
1663 g_return_if_fail (priv->nr_of_pages == -1 ||
1664 current_page < priv->nr_of_pages);
1666 if (priv->current_page != current_page)
1668 priv->current_page = current_page;
1670 g_object_notify (G_OBJECT (op), "current-page");
1675 * gtk_print_operation_set_use_full_page:
1676 * @op: a #GtkPrintOperation
1677 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1679 * If @full_page is %TRUE, the transformation for the cairo context
1680 * obtained from #GtkPrintContext puts the origin at the top left
1681 * corner of the page (which may not be the top left corner of the
1682 * sheet, depending on page orientation and the number of pages per
1683 * sheet). Otherwise, the origin is at the top left corner of the
1684 * imageable area (i.e. inside the margins).
1689 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1692 GtkPrintOperationPrivate *priv;
1694 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1696 full_page = full_page != FALSE;
1700 if (priv->use_full_page != full_page)
1702 priv->use_full_page = full_page;
1704 g_object_notify (G_OBJECT (op), "use-full-page");
1709 * gtk_print_operation_set_unit:
1710 * @op: a #GtkPrintOperation
1711 * @unit: the unit to use
1713 * Sets up the transformation for the cairo context obtained from
1714 * #GtkPrintContext in such a way that distances are measured in
1720 gtk_print_operation_set_unit (GtkPrintOperation *op,
1723 GtkPrintOperationPrivate *priv;
1725 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1729 if (priv->unit != unit)
1733 g_object_notify (G_OBJECT (op), "unit");
1738 * gtk_print_operation_set_track_print_status:
1739 * @op: a #GtkPrintOperation
1740 * @track_status: %TRUE to track status after printing
1742 * If track_status is %TRUE, the print operation will try to continue report
1743 * on the status of the print job in the printer queues and printer. This
1744 * can allow your application to show things like "out of paper" issues,
1745 * and when the print job actually reaches the printer.
1747 * This function is often implemented using some form of polling, so it should
1748 * not be enabled unless needed.
1753 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1754 gboolean track_status)
1756 GtkPrintOperationPrivate *priv;
1758 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1762 if (priv->track_print_status != track_status)
1764 priv->track_print_status = track_status;
1766 g_object_notify (G_OBJECT (op), "track-print-status");
1771 _gtk_print_operation_set_status (GtkPrintOperation *op,
1772 GtkPrintStatus status,
1773 const gchar *string)
1775 GtkPrintOperationPrivate *priv = op->priv;
1776 static const gchar *status_strs[] = {
1777 NC_("print operation status", "Initial state"),
1778 NC_("print operation status", "Preparing to print"),
1779 NC_("print operation status", "Generating data"),
1780 NC_("print operation status", "Sending data"),
1781 NC_("print operation status", "Waiting"),
1782 NC_("print operation status", "Blocking on issue"),
1783 NC_("print operation status", "Printing"),
1784 NC_("print operation status", "Finished"),
1785 NC_("print operation status", "Finished with error")
1788 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1789 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1792 string = g_dpgettext2 (GETTEXT_PACKAGE, "print operation status", status_strs[status]);
1794 if (priv->status == status &&
1795 strcmp (string, priv->status_string) == 0)
1798 g_free (priv->status_string);
1799 priv->status_string = g_strdup (string);
1800 priv->status = status;
1802 g_object_notify (G_OBJECT (op), "status");
1803 g_object_notify (G_OBJECT (op), "status-string");
1805 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1810 * gtk_print_operation_get_status:
1811 * @op: a #GtkPrintOperation
1813 * Returns the status of the print operation.
1814 * Also see gtk_print_operation_get_status_string().
1816 * Return value: the status of the print operation
1821 gtk_print_operation_get_status (GtkPrintOperation *op)
1823 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1824 GTK_PRINT_STATUS_FINISHED_ABORTED);
1826 return op->priv->status;
1830 * gtk_print_operation_get_status_string:
1831 * @op: a #GtkPrintOperation
1833 * Returns a string representation of the status of the
1834 * print operation. The string is translated and suitable
1835 * for displaying the print status e.g. in a #GtkStatusbar.
1837 * Use gtk_print_operation_get_status() to obtain a status
1838 * value that is suitable for programmatic use.
1840 * Return value: a string representation of the status
1841 * of the print operation
1846 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1848 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1850 return op->priv->status_string;
1854 * gtk_print_operation_is_finished:
1855 * @op: a #GtkPrintOperation
1857 * A convenience function to find out if the print operation
1858 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1859 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1861 * Note: when you enable print status tracking the print operation
1862 * can be in a non-finished state even after done has been called, as
1863 * the operation status then tracks the print job status on the printer.
1865 * Return value: %TRUE, if the print operation is finished.
1870 gtk_print_operation_is_finished (GtkPrintOperation *op)
1872 GtkPrintOperationPrivate *priv;
1874 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1878 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1879 priv->status == GTK_PRINT_STATUS_FINISHED;
1883 * gtk_print_operation_set_show_progress:
1884 * @op: a #GtkPrintOperation
1885 * @show_progress: %TRUE to show a progress dialog
1887 * If @show_progress is %TRUE, the print operation will show a
1888 * progress dialog during the print operation.
1893 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1894 gboolean show_progress)
1896 GtkPrintOperationPrivate *priv;
1898 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1902 show_progress = show_progress != FALSE;
1904 if (priv->show_progress != show_progress)
1906 priv->show_progress = show_progress;
1908 g_object_notify (G_OBJECT (op), "show-progress");
1913 * gtk_print_operation_set_allow_async:
1914 * @op: a #GtkPrintOperation
1915 * @allow_async: %TRUE to allow asynchronous operation
1917 * Sets whether the gtk_print_operation_run() may return
1918 * before the print operation is completed. Note that
1919 * some platforms may not allow asynchronous operation.
1924 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1925 gboolean allow_async)
1927 GtkPrintOperationPrivate *priv;
1929 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1933 allow_async = allow_async != FALSE;
1935 if (priv->allow_async != allow_async)
1937 priv->allow_async = allow_async;
1939 g_object_notify (G_OBJECT (op), "allow-async");
1945 * gtk_print_operation_set_custom_tab_label:
1946 * @op: a #GtkPrintOperation
1947 * @label: (allow-none): the label to use, or %NULL to use the default label
1949 * Sets the label for the tab holding custom widgets.
1954 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1957 GtkPrintOperationPrivate *priv;
1959 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1963 g_free (priv->custom_tab_label);
1964 priv->custom_tab_label = g_strdup (label);
1966 g_object_notify (G_OBJECT (op), "custom-tab-label");
1971 * gtk_print_operation_set_export_filename:
1972 * @op: a #GtkPrintOperation
1973 * @filename: (type filename): the filename for the exported file
1975 * Sets up the #GtkPrintOperation to generate a file instead
1976 * of showing the print dialog. The indended use of this function
1977 * is for implementing "Export to PDF" actions. Currently, PDF
1978 * is the only supported format.
1980 * "Print to PDF" support is independent of this and is done
1981 * by letting the user pick the "Print to PDF" item from the list
1982 * of printers in the print dialog.
1987 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1988 const gchar *filename)
1990 GtkPrintOperationPrivate *priv;
1992 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1996 g_free (priv->export_filename);
1997 priv->export_filename = g_strdup (filename);
1999 g_object_notify (G_OBJECT (op), "export-filename");
2002 /* Creates the initial page setup used for printing unless the
2003 * app overrides this on a per-page basis using request_page_setup.
2005 * Data is taken from, in order, if existing:
2007 * PrintSettings returned from the print dialog
2008 * (initial dialog values are set from default_page_setup
2009 * if unset in app specified print_settings)
2010 * default_page_setup
2011 * per-locale default setup
2013 static GtkPageSetup *
2014 create_page_setup (GtkPrintOperation *op)
2016 GtkPrintOperationPrivate *priv = op->priv;
2017 GtkPageSetup *page_setup;
2018 GtkPrintSettings *settings;
2020 if (priv->default_page_setup)
2021 page_setup = gtk_page_setup_copy (priv->default_page_setup);
2023 page_setup = gtk_page_setup_new ();
2025 settings = priv->print_settings;
2028 GtkPaperSize *paper_size;
2030 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
2031 gtk_page_setup_set_orientation (page_setup,
2032 gtk_print_settings_get_orientation (settings));
2035 paper_size = gtk_print_settings_get_paper_size (settings);
2038 gtk_page_setup_set_paper_size (page_setup, paper_size);
2039 gtk_paper_size_free (paper_size);
2042 /* TODO: Margins? */
2049 pdf_start_page (GtkPrintOperation *op,
2050 GtkPrintContext *print_context,
2051 GtkPageSetup *page_setup)
2053 cairo_surface_t *surface = op->priv->platform_data;
2056 w = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2057 h = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2059 cairo_pdf_surface_set_size (surface, w, h);
2063 pdf_end_page (GtkPrintOperation *op,
2064 GtkPrintContext *print_context)
2068 cr = gtk_print_context_get_cairo_context (print_context);
2070 if ((op->priv->manual_number_up < 2) ||
2071 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
2072 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
2073 cairo_show_page (cr);
2077 pdf_end_run (GtkPrintOperation *op,
2081 GtkPrintOperationPrivate *priv = op->priv;
2082 cairo_surface_t *surface = priv->platform_data;
2084 cairo_surface_finish (surface);
2085 cairo_surface_destroy (surface);
2087 priv->platform_data = NULL;
2088 priv->free_platform_data = NULL;
2091 static GtkPrintOperationResult
2092 run_pdf (GtkPrintOperation *op,
2096 GtkPrintOperationPrivate *priv = op->priv;
2097 GtkPageSetup *page_setup;
2098 cairo_surface_t *surface;
2100 gdouble width, height;
2102 priv->print_context = _gtk_print_context_new (op);
2104 page_setup = create_page_setup (op);
2105 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2107 /* This will be overwritten later by the non-default size, but
2108 we need to pass some size: */
2109 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2110 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2111 g_object_unref (page_setup);
2113 surface = cairo_pdf_surface_create (priv->export_filename,
2115 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
2117 g_set_error_literal (&priv->error,
2119 GTK_PRINT_ERROR_GENERAL,
2120 cairo_status_to_string (cairo_surface_status (surface)));
2122 return GTK_PRINT_OPERATION_RESULT_ERROR;
2125 /* this would crash on a nil surface */
2126 cairo_surface_set_fallback_resolution (surface, 300, 300);
2128 priv->platform_data = surface;
2129 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
2131 cr = cairo_create (surface);
2132 gtk_print_context_set_cairo_context (op->priv->print_context,
2137 priv->print_pages = GTK_PRINT_PAGES_ALL;
2138 priv->page_ranges = NULL;
2139 priv->num_page_ranges = 0;
2141 priv->manual_num_copies = 1;
2142 priv->manual_collation = FALSE;
2143 priv->manual_reverse = FALSE;
2144 priv->manual_page_set = GTK_PAGE_SET_ALL;
2145 priv->manual_scale = 1.0;
2146 priv->manual_orientation = FALSE;
2147 priv->manual_number_up = 1;
2148 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2152 priv->start_page = pdf_start_page;
2153 priv->end_page = pdf_end_page;
2154 priv->end_run = pdf_end_run;
2156 return GTK_PRINT_OPERATION_RESULT_APPLY;
2161 clamp_page_ranges (PrintPagesData *data)
2163 GtkPrintOperationPrivate *priv;
2164 gint num_of_correct_ranges;
2167 priv = data->op->priv;
2169 num_of_correct_ranges = 0;
2171 for (i = 0; i < data->num_ranges; i++)
2172 if ((data->ranges[i].start >= 0) &&
2173 (data->ranges[i].start < priv->nr_of_pages) &&
2174 (data->ranges[i].end >= 0) &&
2175 (data->ranges[i].end < priv->nr_of_pages))
2177 data->ranges[num_of_correct_ranges] = data->ranges[i];
2178 num_of_correct_ranges++;
2180 else if ((data->ranges[i].start >= 0) &&
2181 (data->ranges[i].start < priv->nr_of_pages) &&
2182 (data->ranges[i].end >= priv->nr_of_pages))
2184 data->ranges[i].end = priv->nr_of_pages - 1;
2185 data->ranges[num_of_correct_ranges] = data->ranges[i];
2186 num_of_correct_ranges++;
2188 else if ((data->ranges[i].end >= 0) &&
2189 (data->ranges[i].end < priv->nr_of_pages) &&
2190 (data->ranges[i].start < 0))
2192 data->ranges[i].start = 0;
2193 data->ranges[num_of_correct_ranges] = data->ranges[i];
2194 num_of_correct_ranges++;
2197 data->num_ranges = num_of_correct_ranges;
2201 increment_page_sequence (PrintPagesData *data)
2203 GtkPrintOperationPrivate *priv = data->op->priv;
2206 if (data->total == -1)
2212 /* check whether we reached last position */
2213 if (priv->page_position == data->last_position &&
2214 !(data->collated_copies > 1 && data->collated < (data->collated_copies - 1)))
2216 if (data->uncollated_copies > 1 && data->uncollated < (data->uncollated_copies - 1))
2218 priv->page_position = data->first_position;
2219 data->sheet = data->first_sheet;
2230 if (priv->manual_reverse)
2235 /* changing sheet */
2236 if (priv->manual_number_up < 2 ||
2237 (priv->page_position + 1) % priv->manual_number_up == 0 ||
2238 priv->page_position == data->last_position ||
2239 priv->page_position == priv->nr_of_pages_to_print - 1)
2241 /* check whether to print the same sheet again */
2242 if (data->collated_copies > 1)
2244 if (data->collated < (data->collated_copies - 1))
2248 priv->page_position = data->sheet * priv->manual_number_up;
2250 if (priv->page_position < 0 ||
2251 priv->page_position >= priv->nr_of_pages_to_print ||
2253 data->sheet >= data->num_of_sheets)
2259 data->page = data->pages[priv->page_position];
2267 if (priv->manual_page_set == GTK_PAGE_SET_ODD ||
2268 priv->manual_page_set == GTK_PAGE_SET_EVEN)
2269 data->sheet += 2 * inc;
2273 priv->page_position = data->sheet * priv->manual_number_up;
2276 priv->page_position += 1;
2280 if (priv->page_position < 0 ||
2281 priv->page_position >= priv->nr_of_pages_to_print ||
2283 data->sheet >= data->num_of_sheets)
2289 data->page = data->pages[priv->page_position];
2295 print_pages_idle_done (gpointer user_data)
2297 PrintPagesData *data;
2298 GtkPrintOperationPrivate *priv;
2300 data = (PrintPagesData*)user_data;
2301 priv = data->op->priv;
2303 priv->print_pages_idle_id = 0;
2305 if (priv->show_progress_timeout_id > 0)
2307 g_source_remove (priv->show_progress_timeout_id);
2308 priv->show_progress_timeout_id = 0;
2312 gtk_widget_destroy (data->progress);
2314 if (priv->rloop && !data->is_preview)
2315 g_main_loop_quit (priv->rloop);
2317 if (!data->is_preview)
2319 GtkPrintOperationResult result;
2322 result = GTK_PRINT_OPERATION_RESULT_ERROR;
2323 else if (priv->cancelled)
2324 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
2326 result = GTK_PRINT_OPERATION_RESULT_APPLY;
2328 g_signal_emit (data->op, signals[DONE], 0, result);
2331 g_object_unref (data->op);
2332 g_free (data->pages);
2337 update_progress (PrintPagesData *data)
2339 GtkPrintOperationPrivate *priv;
2342 priv = data->op->priv;
2346 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2348 if (priv->nr_of_pages_to_print > 0)
2349 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages_to_print);
2351 text = g_strdup (_("Preparing"));
2353 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2354 text = g_strdup_printf (_("Printing %d"), data->total);
2358 g_object_set (data->progress, "text", text, NULL);
2365 * gtk_print_operation_set_defer_drawing:
2366 * @op: a #GtkPrintOperation
2368 * Sets up the #GtkPrintOperation to wait for calling of
2369 * gtk_print_operation_draw_page_finish() from application. It can
2370 * be used for drawing page in another thread.
2372 * This function must be called in the callback of "draw-page" signal.
2377 gtk_print_operation_set_defer_drawing (GtkPrintOperation *op)
2379 GtkPrintOperationPrivate *priv = op->priv;
2381 g_return_if_fail (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING);
2383 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DEFERRED_DRAWING;
2387 * gtk_print_operation_set_embed_page_setup:
2388 * @op: a #GtkPrintOperation
2389 * @embed: %TRUE to embed page setup selection in the #GtkPrintUnixDialog
2391 * Embed page size combo box and orientation combo box into page setup page.
2392 * Selected page setup is stored as default page setup in #GtkPrintOperation.
2397 gtk_print_operation_set_embed_page_setup (GtkPrintOperation *op,
2400 GtkPrintOperationPrivate *priv;
2402 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2406 embed = embed != FALSE;
2407 if (priv->embed_page_setup != embed)
2409 priv->embed_page_setup = embed;
2410 g_object_notify (G_OBJECT (op), "embed-page-setup");
2415 * gtk_print_operation_get_embed_page_setup:
2416 * @op: a #GtkPrintOperation
2418 * Gets the value of #GtkPrintOperation:embed-page-setup property.
2420 * Returns: whether page setup selection combos are embedded
2425 gtk_print_operation_get_embed_page_setup (GtkPrintOperation *op)
2427 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
2429 return op->priv->embed_page_setup;
2433 * gtk_print_operation_draw_page_finish:
2434 * @op: a #GtkPrintOperation
2436 * Signalize that drawing of particular page is complete.
2438 * It is called after completion of page drawing (e.g. drawing in another
2440 * If gtk_print_operation_set_defer_drawing() was called before, then this function
2441 * has to be called by application. In another case it is called by the library
2447 gtk_print_operation_draw_page_finish (GtkPrintOperation *op)
2449 GtkPrintOperationPrivate *priv = op->priv;
2450 GtkPageSetup *page_setup;
2451 GtkPrintContext *print_context;
2454 print_context = priv->print_context;
2455 page_setup = gtk_print_context_get_page_setup (print_context);
2457 cr = gtk_print_context_get_cairo_context (print_context);
2459 priv->end_page (op, print_context);
2463 g_object_unref (page_setup);
2465 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
2469 common_render_page (GtkPrintOperation *op,
2472 GtkPrintOperationPrivate *priv = op->priv;
2473 GtkPageSetup *page_setup;
2474 GtkPrintContext *print_context;
2477 print_context = priv->print_context;
2479 page_setup = create_page_setup (op);
2481 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2482 print_context, page_nr, page_setup);
2484 _gtk_print_context_set_page_setup (print_context, page_setup);
2486 priv->start_page (op, print_context, page_setup);
2488 cr = gtk_print_context_get_cairo_context (print_context);
2491 if (priv->manual_scale != 1.0 && priv->manual_number_up <= 1)
2494 priv->manual_scale);
2496 if (priv->manual_orientation)
2497 _gtk_print_context_rotate_according_to_orientation (print_context);
2499 _gtk_print_context_reverse_according_to_orientation (print_context);
2501 if (priv->manual_number_up > 1)
2503 GtkPageOrientation orientation;
2504 GtkPageSetup *page_setup;
2505 gdouble paper_width, paper_height;
2506 gdouble page_width, page_height;
2507 gdouble context_width, context_height;
2508 gdouble bottom_margin, top_margin, left_margin, right_margin;
2509 gdouble x_step, y_step;
2510 gdouble x_scale, y_scale, scale;
2511 gdouble horizontal_offset = 0.0, vertical_offset = 0.0;
2512 gint columns, rows, x, y, tmp_length;
2514 page_setup = gtk_print_context_get_page_setup (print_context);
2515 orientation = gtk_page_setup_get_orientation (page_setup);
2517 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
2518 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
2519 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
2520 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
2522 paper_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2523 paper_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2525 context_width = gtk_print_context_get_width (print_context);
2526 context_height = gtk_print_context_get_height (print_context);
2528 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2529 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2531 page_width = paper_width - (left_margin + right_margin);
2532 page_height = paper_height - (top_margin + bottom_margin);
2536 page_width = paper_width - (top_margin + bottom_margin);
2537 page_height = paper_height - (left_margin + right_margin);
2540 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2541 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2542 cairo_translate (cr, left_margin, top_margin);
2544 cairo_translate (cr, top_margin, left_margin);
2546 switch (priv->manual_number_up)
2574 if (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE ||
2575 orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE)
2577 tmp_length = columns;
2582 switch (priv->manual_number_up_layout)
2584 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
2585 x = priv->page_position % columns;
2586 y = (priv->page_position / columns) % rows;
2588 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
2589 x = priv->page_position % columns;
2590 y = rows - 1 - (priv->page_position / columns) % rows;
2592 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
2593 x = columns - 1 - priv->page_position % columns;
2594 y = (priv->page_position / columns) % rows;
2596 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
2597 x = columns - 1 - priv->page_position % columns;
2598 y = rows - 1 - (priv->page_position / columns) % rows;
2600 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
2601 x = (priv->page_position / rows) % columns;
2602 y = priv->page_position % rows;
2604 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
2605 x = columns - 1 - (priv->page_position / rows) % columns;
2606 y = priv->page_position % rows;
2608 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
2609 x = (priv->page_position / rows) % columns;
2610 y = rows - 1 - priv->page_position % rows;
2612 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
2613 x = columns - 1 - (priv->page_position / rows) % columns;
2614 y = rows - 1 - priv->page_position % rows;
2617 g_assert_not_reached();
2622 if (priv->manual_number_up == 4 || priv->manual_number_up == 9 || priv->manual_number_up == 16)
2624 x_scale = page_width / (columns * paper_width);
2625 y_scale = page_height / (rows * paper_height);
2627 scale = x_scale < y_scale ? x_scale : y_scale;
2629 x_step = paper_width * (x_scale / scale);
2630 y_step = paper_height * (y_scale / scale);
2632 if ((left_margin + right_margin) > 0)
2634 horizontal_offset = left_margin * (x_step - context_width) / (left_margin + right_margin);
2635 vertical_offset = top_margin * (y_step - context_height) / (top_margin + bottom_margin);
2639 horizontal_offset = (x_step - context_width) / 2.0;
2640 vertical_offset = (y_step - context_height) / 2.0;
2643 cairo_scale (cr, scale, scale);
2645 cairo_translate (cr,
2646 x * x_step + horizontal_offset,
2647 y * y_step + vertical_offset);
2649 if (priv->manual_scale != 1.0)
2650 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2653 if (priv->manual_number_up == 2 || priv->manual_number_up == 6)
2655 x_scale = page_height / (columns * paper_width);
2656 y_scale = page_width / (rows * paper_height);
2658 scale = x_scale < y_scale ? x_scale : y_scale;
2660 horizontal_offset = (paper_width * (x_scale / scale) - paper_width) / 2.0 * columns;
2661 vertical_offset = (paper_height * (y_scale / scale) - paper_height) / 2.0 * rows;
2663 if (!priv->use_full_page)
2665 horizontal_offset -= right_margin;
2666 vertical_offset += top_margin;
2669 cairo_scale (cr, scale, scale);
2671 cairo_translate (cr,
2672 y * paper_height + vertical_offset,
2673 (columns - x) * paper_width + horizontal_offset);
2675 if (priv->manual_scale != 1.0)
2676 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2678 cairo_rotate (cr, - G_PI / 2);
2682 if (!priv->use_full_page)
2683 _gtk_print_context_translate_into_margin (print_context);
2685 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DRAWING;
2687 g_signal_emit (op, signals[DRAW_PAGE], 0,
2688 print_context, page_nr);
2690 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING)
2691 gtk_print_operation_draw_page_finish (op);
2695 prepare_data (PrintPagesData *data)
2697 GtkPrintOperationPrivate *priv;
2698 GtkPageSetup *page_setup;
2701 priv = data->op->priv;
2703 if (priv->manual_collation)
2705 data->uncollated_copies = priv->manual_num_copies;
2706 data->collated_copies = 1;
2710 data->uncollated_copies = 1;
2711 data->collated_copies = priv->manual_num_copies;
2714 if (!data->initialized)
2716 data->initialized = TRUE;
2717 page_setup = create_page_setup (data->op);
2718 _gtk_print_context_set_page_setup (priv->print_context,
2720 g_object_unref (page_setup);
2722 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2727 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2729 gboolean paginated = FALSE;
2731 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2736 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2738 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2740 if (priv->page_ranges == NULL)
2742 g_warning ("no pages to print");
2743 priv->cancelled = TRUE;
2746 data->ranges = priv->page_ranges;
2747 data->num_ranges = priv->num_page_ranges;
2748 for (i = 0; i < data->num_ranges; i++)
2749 if (data->ranges[i].end == -1 ||
2750 data->ranges[i].end >= priv->nr_of_pages)
2751 data->ranges[i].end = priv->nr_of_pages - 1;
2753 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2754 priv->current_page != -1)
2756 data->ranges = &data->one_range;
2757 data->num_ranges = 1;
2758 data->ranges[0].start = priv->current_page;
2759 data->ranges[0].end = priv->current_page;
2763 data->ranges = &data->one_range;
2764 data->num_ranges = 1;
2765 data->ranges[0].start = 0;
2766 data->ranges[0].end = priv->nr_of_pages - 1;
2769 clamp_page_ranges (data);
2771 if (data->num_ranges < 1)
2773 priv->cancelled = TRUE;
2777 priv->nr_of_pages_to_print = 0;
2778 for (i = 0; i < data->num_ranges; i++)
2779 priv->nr_of_pages_to_print += data->ranges[i].end - data->ranges[i].start + 1;
2781 data->pages = g_new (gint, priv->nr_of_pages_to_print);
2783 for (i = 0; i < data->num_ranges; i++)
2784 for (j = data->ranges[i].start; j <= data->ranges[i].end; j++)
2786 data->pages[counter] = j;
2792 data->uncollated = 0;
2794 if (priv->manual_number_up > 1)
2796 if (priv->nr_of_pages_to_print % priv->manual_number_up == 0)
2797 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up;
2799 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up + 1;
2802 data->num_of_sheets = priv->nr_of_pages_to_print;
2804 if (priv->manual_reverse)
2806 /* data->sheet is 0-based */
2807 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2808 data->sheet = (data->num_of_sheets - 1) - (data->num_of_sheets - 1) % 2;
2809 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2810 data->sheet = (data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2);
2812 data->sheet = data->num_of_sheets - 1;
2816 /* data->sheet is 0-based */
2817 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2819 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2821 if (data->num_of_sheets > 1)
2830 priv->page_position = data->sheet * priv->manual_number_up;
2832 if (priv->page_position < 0 || priv->page_position >= priv->nr_of_pages_to_print)
2834 priv->cancelled = TRUE;
2838 data->page = data->pages[priv->page_position];
2839 data->first_position = priv->page_position;
2840 data->first_sheet = data->sheet;
2842 if (priv->manual_reverse)
2844 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2845 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2846 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2847 data->last_position = MIN (2 * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2849 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2853 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2854 data->last_position = MIN (((data->num_of_sheets - 1) - ((data->num_of_sheets - 1) % 2)) * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2855 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2856 data->last_position = MIN (((data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2)) * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2858 data->last_position = priv->nr_of_pages_to_print - 1;
2862 _gtk_print_operation_set_status (data->op,
2863 GTK_PRINT_STATUS_GENERATING_DATA,
2868 print_pages_idle (gpointer user_data)
2870 PrintPagesData *data;
2871 GtkPrintOperationPrivate *priv;
2872 gboolean done = FALSE;
2874 data = (PrintPagesData*)user_data;
2875 priv = data->op->priv;
2877 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
2879 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2881 prepare_data (data);
2885 if (data->is_preview && !priv->cancelled)
2889 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2893 increment_page_sequence (data);
2896 common_render_page (data->op, data->page);
2898 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
2902 if (priv->cancelled)
2904 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2906 data->is_preview = FALSE;
2910 if (done && !data->is_preview)
2912 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2913 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2916 update_progress (data);
2923 handle_progress_response (GtkWidget *dialog,
2927 GtkPrintOperation *op = (GtkPrintOperation *)data;
2929 gtk_widget_hide (dialog);
2930 gtk_print_operation_cancel (op);
2934 show_progress_timeout (PrintPagesData *data)
2936 gtk_window_present (GTK_WINDOW (data->progress));
2938 data->op->priv->show_progress_timeout_id = 0;
2944 print_pages (GtkPrintOperation *op,
2947 GtkPrintOperationResult result)
2949 GtkPrintOperationPrivate *priv = op->priv;
2950 PrintPagesData *data;
2954 GtkPrintOperationResult tmp_result;
2956 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2959 tmp_result = GTK_PRINT_OPERATION_RESULT_ERROR;
2960 else if (priv->cancelled)
2961 tmp_result = GTK_PRINT_OPERATION_RESULT_CANCEL;
2963 tmp_result = result;
2965 g_signal_emit (op, signals[DONE], 0, tmp_result);
2970 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2972 data = g_new0 (PrintPagesData, 1);
2973 data->op = g_object_ref (op);
2974 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2976 if (priv->show_progress)
2978 GtkWidget *progress;
2980 progress = gtk_message_dialog_new (parent, 0,
2984 g_signal_connect (progress, "response",
2985 G_CALLBACK (handle_progress_response), op);
2987 priv->show_progress_timeout_id =
2988 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2989 (GSourceFunc)show_progress_timeout,
2992 data->progress = progress;
2995 if (data->is_preview)
2999 g_signal_emit_by_name (op, "preview",
3000 GTK_PRINT_OPERATION_PREVIEW (op),
3001 priv->print_context,
3007 GtkWidget *error_dialog;
3009 error_dialog = gtk_message_dialog_new (parent,
3010 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
3013 _("Error creating print preview"));
3015 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
3016 _("The most probable reason is that a temporary file could not be created."));
3018 if (parent && gtk_window_has_group (parent))
3019 gtk_window_group_add_window (gtk_window_get_group (parent),
3020 GTK_WINDOW (error_dialog));
3022 g_signal_connect (error_dialog, "response",
3023 G_CALLBACK (gtk_widget_destroy), NULL);
3025 gtk_widget_show (error_dialog);
3027 print_pages_idle_done (data);
3032 if (gtk_print_context_get_cairo_context (priv->print_context) == NULL)
3034 /* Programmer error */
3035 g_error ("You must set a cairo context on the print context");
3038 priv->start_page = preview_start_page;
3039 priv->end_page = preview_end_page;
3040 priv->end_run = preview_end_run;
3042 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
3043 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
3044 &priv->num_page_ranges);
3045 priv->manual_num_copies = 1;
3046 priv->manual_collation = FALSE;
3047 priv->manual_reverse = gtk_print_settings_get_reverse (priv->print_settings);
3048 priv->manual_page_set = gtk_print_settings_get_page_set (priv->print_settings);
3049 priv->manual_scale = gtk_print_settings_get_scale (priv->print_settings) / 100.0;
3050 priv->manual_orientation = FALSE;
3051 priv->manual_number_up = gtk_print_settings_get_number_up (priv->print_settings);
3052 priv->manual_number_up_layout = gtk_print_settings_get_number_up_layout (priv->print_settings);
3055 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
3058 print_pages_idle_done);
3060 /* Recursive main loop to make sure we don't exit on sync operations */
3063 priv->rloop = g_main_loop_new (NULL, FALSE);
3066 gdk_threads_leave ();
3067 g_main_loop_run (priv->rloop);
3068 gdk_threads_enter ();
3070 g_main_loop_unref (priv->rloop);
3072 g_object_unref (op);
3077 * gtk_print_operation_get_error:
3078 * @op: a #GtkPrintOperation
3079 * @error: return location for the error
3081 * Call this when the result of a print operation is
3082 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
3083 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
3084 * handler. The returned #GError will contain more details on what went wrong.
3089 gtk_print_operation_get_error (GtkPrintOperation *op,
3092 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3094 g_propagate_error (error, op->priv->error);
3096 op->priv->error = NULL;
3101 * gtk_print_operation_run:
3102 * @op: a #GtkPrintOperation
3103 * @action: the action to start
3104 * @parent: (allow-none): Transient parent of the dialog
3105 * @error: (allow-none): Return location for errors, or %NULL
3107 * Runs the print operation, by first letting the user modify
3108 * print settings in the print dialog, and then print the document.
3110 * Normally that this function does not return until the rendering of all
3111 * pages is complete. You can connect to the
3112 * #GtkPrintOperation::status-changed signal on @op to obtain some
3113 * information about the progress of the print operation.
3114 * Furthermore, it may use a recursive mainloop to show the print dialog.
3116 * If you call gtk_print_operation_set_allow_async() or set the
3117 * #GtkPrintOperation:allow-async property the operation will run
3118 * asynchronously if this is supported on the platform. The
3119 * #GtkPrintOperation::done signal will be emitted with the result of the
3120 * operation when the it is done (i.e. when the dialog is canceled, or when
3121 * the print succeeds or fails).
3123 * if (settings != NULL)
3124 * gtk_print_operation_set_print_settings (print, settings);
3126 * if (page_setup != NULL)
3127 * gtk_print_operation_set_default_page_setup (print, page_setup);
3129 * g_signal_connect (print, "begin-print",
3130 * G_CALLBACK (begin_print), &data);
3131 * g_signal_connect (print, "draw-page",
3132 * G_CALLBACK (draw_page), &data);
3134 * res = gtk_print_operation_run (print,
3135 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3139 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
3141 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
3142 * GTK_DIALOG_DESTROY_WITH_PARENT,
3143 * GTK_MESSAGE_ERROR,
3144 * GTK_BUTTONS_CLOSE,
3145 * "Error printing file:\n%s",
3147 * g_signal_connect (error_dialog, "response",
3148 * G_CALLBACK (gtk_widget_destroy), NULL);
3149 * gtk_widget_show (error_dialog);
3150 * g_error_free (error);
3152 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
3154 * if (settings != NULL)
3155 * g_object_unref (settings);
3156 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
3160 * Note that gtk_print_operation_run() can only be called once on a
3161 * given #GtkPrintOperation.
3163 * Return value: the result of the print operation. A return value of
3164 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
3165 * completed successfully. In this case, it is a good idea to obtain
3166 * the used print settings with gtk_print_operation_get_print_settings()
3167 * and store them for reuse with the next print operation. A value of
3168 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
3169 * asynchronously, and will emit the #GtkPrintOperation::done signal when
3174 GtkPrintOperationResult
3175 gtk_print_operation_run (GtkPrintOperation *op,
3176 GtkPrintOperationAction action,
3180 GtkPrintOperationPrivate *priv;
3181 GtkPrintOperationResult result;
3182 GtkPageSetup *page_setup;
3184 gboolean run_print_pages;
3186 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
3187 GTK_PRINT_OPERATION_RESULT_ERROR);
3188 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
3189 GTK_PRINT_OPERATION_RESULT_ERROR);
3192 run_print_pages = TRUE;
3195 priv->action = action;
3197 if (priv->print_settings == NULL)
3198 priv->print_settings = gtk_print_settings_new ();
3200 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
3202 /* note: if you implement async EXPORT, update the docs
3203 * docs for the allow-async property.
3205 priv->is_sync = TRUE;
3206 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
3207 result = run_pdf (op, parent, &do_print);
3209 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
3211 priv->is_sync = !priv->allow_async;
3212 priv->print_context = _gtk_print_context_new (op);
3213 page_setup = create_page_setup (op);
3214 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
3215 g_object_unref (page_setup);
3217 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3220 else if (priv->allow_async)
3222 priv->is_sync = FALSE;
3223 _gtk_print_operation_platform_backend_run_dialog_async (op,
3224 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3227 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3228 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
3233 priv->is_sync = TRUE;
3234 result = _gtk_print_operation_platform_backend_run_dialog (op,
3235 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3240 if (run_print_pages)
3241 print_pages (op, parent, do_print, result);
3243 if (priv->error && error)
3245 *error = g_error_copy (priv->error);
3246 result = GTK_PRINT_OPERATION_RESULT_ERROR;
3248 else if (priv->cancelled)
3249 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
3255 * gtk_print_operation_cancel:
3256 * @op: a #GtkPrintOperation
3258 * Cancels a running print operation. This function may
3259 * be called from a #GtkPrintOperation::begin-print,
3260 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
3261 * signal handler to stop the currently running print
3267 gtk_print_operation_cancel (GtkPrintOperation *op)
3269 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3271 op->priv->cancelled = TRUE;
3275 * gtk_print_operation_set_support_selection:
3276 * @op: a #GtkPrintOperation
3277 * @support_selection: %TRUE to support selection
3279 * Sets whether selection is supported by #GtkPrintOperation.
3284 gtk_print_operation_set_support_selection (GtkPrintOperation *op,
3285 gboolean support_selection)
3287 GtkPrintOperationPrivate *priv;
3289 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3293 support_selection = support_selection != FALSE;
3294 if (priv->support_selection != support_selection)
3296 priv->support_selection = support_selection;
3297 g_object_notify (G_OBJECT (op), "support-selection");
3302 * gtk_print_operation_get_support_selection:
3303 * @op: a #GtkPrintOperation
3305 * Gets the value of #GtkPrintOperation:support-selection property.
3307 * Returns: whether the application supports print of selection
3312 gtk_print_operation_get_support_selection (GtkPrintOperation *op)
3314 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3316 return op->priv->support_selection;
3320 * gtk_print_operation_set_has_selection:
3321 * @op: a #GtkPrintOperation
3322 * @has_selection: %TRUE indicates that a selection exists
3324 * Sets whether there is a selection to print.
3326 * Application has to set number of pages to which the selection
3327 * will draw by gtk_print_operation_set_n_pages() in a callback of
3328 * #GtkPrintOperation::begin-print.
3333 gtk_print_operation_set_has_selection (GtkPrintOperation *op,
3334 gboolean has_selection)
3336 GtkPrintOperationPrivate *priv;
3338 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3342 has_selection = has_selection != FALSE;
3343 if (priv->has_selection != has_selection)
3345 priv->has_selection = has_selection;
3346 g_object_notify (G_OBJECT (op), "has-selection");
3351 * gtk_print_operation_get_has_selection:
3352 * @op: a #GtkPrintOperation
3354 * Gets the value of #GtkPrintOperation:has-selection property.
3356 * Returns: whether there is a selection
3361 gtk_print_operation_get_has_selection (GtkPrintOperation *op)
3363 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3365 return op->priv->has_selection;
3369 * gtk_print_operation_get_n_pages_to_print:
3370 * @op: a #GtkPrintOperation
3372 * Returns the number of pages that will be printed.
3374 * Note that this value is set during print preparation phase
3375 * (%GTK_PRINT_STATUS_PREPARING), so this function should never be
3376 * called before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
3377 * You can connect to the #GtkPrintOperation::status-changed signal
3378 * and call gtk_print_operation_get_n_pages_to_print() when
3379 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
3380 * This is typically used to track the progress of print operation.
3382 * Returns: the number of pages that will be printed
3387 gtk_print_operation_get_n_pages_to_print (GtkPrintOperation *op)
3389 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), -1);
3391 return op->priv->nr_of_pages_to_print;