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, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include "gtkprintoperation-private.h"
28 #include "gtkmarshalers.h"
29 #include <cairo-pdf.h>
31 #include "gtkprivate.h"
32 #include "gtkmessagedialog.h"
35 #define SHOW_PROGRESS_TIME 1200
37 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
57 PROP_DEFAULT_PAGE_SETUP,
63 PROP_TRACK_PRINT_STATUS,
73 static guint signals[LAST_SIGNAL] = { 0 };
74 static int job_nr = 0;
76 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
77 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
78 static void common_render_page (GtkPrintOperation *op,
82 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
83 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
87 * gtk_print_error_quark:
89 * Registers an error quark for #GtkPrintOperation if necessary.
91 * Return value: The error quark used for #GtkPrintOperation errors.
96 gtk_print_error_quark (void)
98 static GQuark quark = 0;
100 quark = g_quark_from_static_string ("gtk-print-error-quark");
105 gtk_print_operation_finalize (GObject *object)
107 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
108 GtkPrintOperationPrivate *priv = print_operation->priv;
110 if (priv->free_platform_data &&
113 priv->free_platform_data (priv->platform_data);
114 priv->free_platform_data = NULL;
117 if (priv->default_page_setup)
118 g_object_unref (priv->default_page_setup);
120 if (priv->print_settings)
121 g_object_unref (priv->print_settings);
123 if (priv->print_context)
124 g_object_unref (priv->print_context);
126 g_free (priv->export_filename);
127 g_free (priv->job_name);
128 g_free (priv->custom_tab_label);
129 g_free (priv->status_string);
131 if (priv->print_pages_idle_id > 0)
132 g_source_remove (priv->print_pages_idle_id);
134 if (priv->show_progress_timeout_id > 0)
135 g_source_remove (priv->show_progress_timeout_id);
138 g_error_free (priv->error);
140 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
144 gtk_print_operation_init (GtkPrintOperation *operation)
146 GtkPrintOperationPrivate *priv;
149 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
151 priv->status = GTK_PRINT_STATUS_INITIAL;
152 priv->status_string = g_strdup ("");
153 priv->default_page_setup = NULL;
154 priv->print_settings = NULL;
155 priv->nr_of_pages = -1;
156 priv->current_page = -1;
157 priv->use_full_page = FALSE;
158 priv->show_progress = FALSE;
159 priv->export_filename = NULL;
160 priv->track_print_status = FALSE;
161 priv->is_sync = FALSE;
164 priv->unit = GTK_UNIT_PIXEL;
166 appname = g_get_application_name ();
169 /* translators: this string is the default job title for print
170 * jobs. %s gets replaced by the application name, %d gets replaced
173 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
177 preview_iface_render_page (GtkPrintOperationPreview *preview,
181 GtkPrintOperation *op;
183 op = GTK_PRINT_OPERATION (preview);
184 common_render_page (op, page_nr);
188 preview_iface_end_preview (GtkPrintOperationPreview *preview)
190 GtkPrintOperation *op;
192 op = GTK_PRINT_OPERATION (preview);
194 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
197 g_main_loop_quit (op->priv->rloop);
199 if (op->priv->end_run)
200 op->priv->end_run (op, op->priv->is_sync, TRUE);
202 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
204 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
208 preview_iface_is_selected (GtkPrintOperationPreview *preview,
211 GtkPrintOperation *op;
212 GtkPrintOperationPrivate *priv;
215 op = GTK_PRINT_OPERATION (preview);
218 switch (priv->print_pages)
220 case GTK_PRINT_PAGES_ALL:
221 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
222 case GTK_PRINT_PAGES_CURRENT:
223 return page_nr == priv->current_page;
224 case GTK_PRINT_PAGES_RANGES:
225 for (i = 0; i < priv->num_page_ranges; i++)
227 if (page_nr >= priv->page_ranges[i].start &&
228 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
237 preview_iface_init (GtkPrintOperationPreviewIface *iface)
239 iface->render_page = preview_iface_render_page;
240 iface->end_preview = preview_iface_end_preview;
241 iface->is_selected = preview_iface_is_selected;
245 preview_start_page (GtkPrintOperation *op,
246 GtkPrintContext *print_context,
247 GtkPageSetup *page_setup)
249 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
253 preview_end_page (GtkPrintOperation *op,
254 GtkPrintContext *print_context)
259 preview_end_run (GtkPrintOperation *op,
263 g_free (op->priv->page_ranges);
264 op->priv->page_ranges = NULL;
269 gtk_print_operation_set_property (GObject *object,
274 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
278 case PROP_DEFAULT_PAGE_SETUP:
279 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
281 case PROP_PRINT_SETTINGS:
282 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
285 gtk_print_operation_set_job_name (op, g_value_get_string (value));
288 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
290 case PROP_CURRENT_PAGE:
291 gtk_print_operation_set_current_page (op, g_value_get_int (value));
293 case PROP_USE_FULL_PAGE:
294 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
296 case PROP_TRACK_PRINT_STATUS:
297 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
300 gtk_print_operation_set_unit (op, g_value_get_enum (value));
302 case PROP_ALLOW_ASYNC:
303 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
305 case PROP_SHOW_PROGRESS:
306 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
308 case PROP_EXPORT_FILENAME:
309 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
311 case PROP_CUSTOM_TAB_LABEL:
312 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 gtk_print_operation_get_property (GObject *object,
326 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
327 GtkPrintOperationPrivate *priv = op->priv;
331 case PROP_DEFAULT_PAGE_SETUP:
332 g_value_set_object (value, priv->default_page_setup);
334 case PROP_PRINT_SETTINGS:
335 g_value_set_object (value, priv->print_settings);
338 g_value_set_string (value, priv->job_name);
341 g_value_set_int (value, priv->nr_of_pages);
343 case PROP_CURRENT_PAGE:
344 g_value_set_int (value, priv->current_page);
346 case PROP_USE_FULL_PAGE:
347 g_value_set_boolean (value, priv->use_full_page);
349 case PROP_TRACK_PRINT_STATUS:
350 g_value_set_boolean (value, priv->track_print_status);
353 g_value_set_enum (value, priv->unit);
355 case PROP_ALLOW_ASYNC:
356 g_value_set_boolean (value, priv->allow_async);
358 case PROP_SHOW_PROGRESS:
359 g_value_set_boolean (value, priv->show_progress);
361 case PROP_EXPORT_FILENAME:
362 g_value_set_string (value, priv->export_filename);
365 g_value_set_enum (value, priv->status);
367 case PROP_STATUS_STRING:
368 g_value_set_string (value, priv->status_string);
370 case PROP_CUSTOM_TAB_LABEL:
371 g_value_set_string (value, priv->custom_tab_label);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 GtkPrintOperationPreview *preview;
382 GtkPrintContext *print_context;
384 cairo_surface_t *surface;
391 preview_print_idle_done (gpointer data)
393 GtkPrintOperation *op;
394 PreviewOp *pop = (PreviewOp *) data;
396 op = GTK_PRINT_OPERATION (pop->preview);
398 cairo_surface_finish (pop->surface);
399 /* Surface is destroyed in launch_preview */
400 _gtk_print_operation_platform_backend_launch_preview (op,
405 g_free (pop->filename);
407 gtk_print_operation_preview_end_preview (pop->preview);
414 preview_print_idle (gpointer data)
417 GtkPrintOperation *op;
418 gboolean retval = TRUE;
421 pop = (PreviewOp *) data;
422 op = GTK_PRINT_OPERATION (pop->preview);
424 gtk_print_operation_preview_render_page (pop->preview, pop->page_nr);
426 cr = gtk_print_context_get_cairo_context (pop->print_context);
427 _gtk_print_operation_platform_backend_preview_end_page (op, pop->surface, cr);
429 /* TODO: print out sheets not pages and follow ranges */
431 if (op->priv->nr_of_pages <= pop->page_nr)
438 preview_got_page_size (GtkPrintOperationPreview *preview,
439 GtkPrintContext *context,
440 GtkPageSetup *page_setup,
443 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
446 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
448 cr = gtk_print_context_get_cairo_context (pop->print_context);
449 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
454 preview_ready (GtkPrintOperationPreview *preview,
455 GtkPrintContext *context,
459 pop->print_context = context;
461 g_object_ref (preview);
463 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
466 preview_print_idle_done);
471 gtk_print_operation_preview_handler (GtkPrintOperation *op,
472 GtkPrintOperationPreview *preview,
473 GtkPrintContext *context,
476 gdouble dpi_x, dpi_y;
478 GtkPageSetup *page_setup;
481 pop = g_new0 (PreviewOp, 1);
482 pop->filename = NULL;
483 pop->preview = preview;
484 pop->parent = parent;
486 page_setup = gtk_print_context_get_page_setup (context);
489 _gtk_print_operation_platform_backend_create_preview_surface (op,
494 if (pop->surface == NULL)
500 cr = cairo_create (pop->surface);
501 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
505 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
506 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
512 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
518 gtk_print_operation_done (GtkPrintOperation *operation,
519 GtkPrintOperationResult result)
521 GtkPrintOperationPrivate *priv = operation->priv;
523 if (priv->print_context)
525 g_object_unref (priv->print_context);
526 priv->print_context = NULL;
531 custom_widget_accumulator (GSignalInvocationHint *ihint,
533 const GValue *handler_return,
536 gboolean continue_emission;
539 widget = g_value_get_object (handler_return);
541 g_value_set_object (return_accu, widget);
542 continue_emission = (widget == NULL);
544 return continue_emission;
548 gtk_print_operation_class_init (GtkPrintOperationClass *class)
550 GObjectClass *gobject_class = (GObjectClass *)class;
552 gobject_class->set_property = gtk_print_operation_set_property;
553 gobject_class->get_property = gtk_print_operation_get_property;
554 gobject_class->finalize = gtk_print_operation_finalize;
556 class->preview = gtk_print_operation_preview_handler;
557 class->create_custom_widget = gtk_print_operation_create_custom_widget;
558 class->done = gtk_print_operation_done;
560 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
563 * GtkPrintOperation::done:
564 * @operation: the #GtkPrintOperation on which the signal was emitted
565 * @result: the result of the print operation
567 * Emitted when the print operation run has finished doing
568 * everything required for printing.
570 * @result gives you information about what happened during the run.
571 * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
572 * gtk_print_operation_get_error() for more information.
574 * If you enabled print status tracking then
575 * gtk_print_operation_is_finished() may still return %FALSE
576 * after #GtkPrintOperation::done was emitted.
581 g_signal_new (I_("done"),
582 G_TYPE_FROM_CLASS (gobject_class),
584 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
586 g_cclosure_marshal_VOID__ENUM,
587 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
590 * GtkPrintOperation::begin-print:
591 * @operation: the #GtkPrintOperation on which the signal was emitted
592 * @context: the #GtkPrintContext for the current operation
594 * Emitted after the user has finished changing print settings
595 * in the dialog, before the actual rendering starts.
597 * A typical use for ::begin-print is to use the parameters from the
598 * #GtkPrintContext and paginate the document accordingly, and then
599 * set the number of pages with gtk_print_operation_set_n_pages().
603 signals[BEGIN_PRINT] =
604 g_signal_new (I_("begin-print"),
605 G_TYPE_FROM_CLASS (gobject_class),
607 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
609 g_cclosure_marshal_VOID__OBJECT,
610 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
613 * GtkPrintOperation::paginate:
614 * @operation: the #GtkPrintOperation on which the signal was emitted
615 * @context: the #GtkPrintContext for the current operation
617 * Emitted after the #GtkPrintOperation::begin-print signal, but before
618 * the actual rendering starts. It keeps getting emitted until a connected
619 * signal handler returns %TRUE.
621 * The ::paginate signal is intended to be used for paginating a document
622 * in small chunks, to avoid blocking the user interface for a long
623 * time. The signal handler should update the number of pages using
624 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
625 * has been completely paginated.
627 * If you don't need to do pagination in chunks, you can simply do
628 * it all in the ::begin-print handler, and set the number of pages
631 * Return value: %TRUE if pagination is complete
636 g_signal_new (I_("paginate"),
637 G_TYPE_FROM_CLASS (gobject_class),
639 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
640 _gtk_boolean_handled_accumulator, NULL,
641 _gtk_marshal_BOOLEAN__OBJECT,
642 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
646 * GtkPrintOperation::request-page-setup:
647 * @operation: the #GtkPrintOperation on which the signal was emitted
648 * @context: the #GtkPrintContext for the current operation
649 * @page_nr: the number of the currently printed page
650 * @setup: the #GtkPageSetup
652 * Emitted once for every page that is printed, to give
653 * the application a chance to modify the page setup. Any changes
654 * done to @setup will be in force only for printing this page.
658 signals[REQUEST_PAGE_SETUP] =
659 g_signal_new (I_("request-page-setup"),
660 G_TYPE_FROM_CLASS (gobject_class),
662 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
664 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
666 GTK_TYPE_PRINT_CONTEXT,
668 GTK_TYPE_PAGE_SETUP);
671 * GtkPrintOperation::draw-page:
672 * @operation: the #GtkPrintOperation on which the signal was emitted
673 * @context: the #GtkPrintContext for the current operation
674 * @page_nr: the number of the currently printed page
676 * Emitted for every page that is printed. The signal handler
677 * must render the @page_nr's page onto the cairo context obtained
678 * from @context using gtk_print_context_get_cairo_context().
681 * draw_page (GtkPrintOperation *operation,
682 * GtkPrintContext *context,
684 * gpointer user_data)
687 * PangoLayout *layout;
688 * gdouble width, text_height;
689 * gint layout_height;
690 * PangoFontDescription *desc;
692 * cr = gtk_print_context_get_cairo_context (context);
693 * width = gtk_print_context_get_width (context);
695 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
697 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
700 * layout = gtk_print_context_create_pango_layout (context);
702 * desc = pango_font_description_from_string ("sans 14");
703 * pango_layout_set_font_description (layout, desc);
704 * pango_font_description_free (desc);
706 * pango_layout_set_text (layout, "some text", -1);
707 * pango_layout_set_width (layout, width * PANGO_SCALE);
708 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
710 * pango_layout_get_size (layout, NULL, &layout_height);
711 * text_height = (gdouble)layout_height / PANGO_SCALE;
713 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
714 * pango_cairo_show_layout (cr, layout);
716 * g_object_unref (layout);
720 * Use gtk_print_operation_set_use_full_page() and
721 * gtk_print_operation_set_unit() before starting the print operation
722 * to set up the transformation of the cairo context according to your
728 g_signal_new (I_("draw-page"),
729 G_TYPE_FROM_CLASS (gobject_class),
731 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
733 _gtk_marshal_VOID__OBJECT_INT,
735 GTK_TYPE_PRINT_CONTEXT,
739 * GtkPrintOperation::end-print:
740 * @operation: the #GtkPrintOperation on which the signal was emitted
741 * @context: the #GtkPrintContext for the current operation
743 * Emitted after all pages have been rendered.
744 * A handler for this signal can clean up any resources that have
745 * been allocated in the #GtkPrintOperation::begin-print handler.
750 g_signal_new (I_("end-print"),
751 G_TYPE_FROM_CLASS (gobject_class),
753 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
755 g_cclosure_marshal_VOID__OBJECT,
756 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
759 * GtkPrintOperation::status-changed:
760 * @operation: the #GtkPrintOperation on which the signal was emitted
762 * Emitted at between the various phases of the print operation.
763 * See #GtkPrintStatus for the phases that are being discriminated.
764 * Use gtk_print_operation_get_status() to find out the current
769 signals[STATUS_CHANGED] =
770 g_signal_new (I_("status-changed"),
771 G_TYPE_FROM_CLASS (class),
773 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
775 g_cclosure_marshal_VOID__VOID,
780 * GtkPrintOperation::create-custom-widget:
781 * @operation: the #GtkPrintOperation on which the signal was emitted
783 * Emitted when displaying the print dialog. If you return a
784 * widget in a handler for this signal it will be added to a custom
785 * tab in the print dialog. You typically return a container widget
786 * with multiple widgets in it.
788 * The print dialog owns the returned widget, and its lifetime is not
789 * controlled by the application. However, the widget is guaranteed
790 * to stay around until the #GtkPrintOperation::custom-widget-apply
791 * signal is emitted on the operation. Then you can read out any
792 * information you need from the widgets.
794 * Returns: A custom widget that gets embedded in the print dialog,
799 signals[CREATE_CUSTOM_WIDGET] =
800 g_signal_new (I_("create-custom-widget"),
801 G_TYPE_FROM_CLASS (class),
803 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
804 custom_widget_accumulator, NULL,
805 _gtk_marshal_OBJECT__VOID,
809 * GtkPrintOperation::custom-widget-apply:
810 * @operation: the #GtkPrintOperation on which the signal was emitted
811 * @widget: the custom widget added in create-custom-widget
813 * Emitted right before #GtkPrintOperation::begin-print if you added
814 * a custom widget in the #GtkPrintOperation:;create-custom-widget handler.
815 * When you get this signal you should read the information from the
816 * custom widgets, as the widgets are not guaraneed to be around at a
821 signals[CUSTOM_WIDGET_APPLY] =
822 g_signal_new (I_("custom-widget-apply"),
823 G_TYPE_FROM_CLASS (class),
825 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
827 g_cclosure_marshal_VOID__OBJECT,
828 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
831 * GtkPrintOperation::preview:
832 * @operation: the #GtkPrintOperation on which the signal was emitted
833 * @preview: the #GtkPrintPreviewOperation for the current operation
834 * @context: the #GtkPrintContext that will be used
835 * @parent: the #GtkWindow to use as window parent, or %NULL
837 * Gets emitted when a preview is requested from the native dialog.
839 * The default handler for this signal uses an external viewer
840 * application to preview.
842 * To implement a custom print preview, an application must return
843 * %TRUE from its handler for this signal. In order to use the
844 * provided @context for the preview implementation, it must be
845 * given a suitable cairo context with gtk_print_context_set_cairo_context().
847 * The custom preview implementation can use
848 * gtk_print_operation_preview_is_selected() and
849 * gtk_print_operation_preview_render_page() to find pages which
850 * are selected for print and render them. The preview must be
851 * finished by calling gtk_print_operation_preview_end_preview()
852 * (typically in response to the user clicking a close button).
854 * Returns: %TRUE if the listener wants to take over control of the preview
859 g_signal_new (I_("preview"),
860 G_TYPE_FROM_CLASS (gobject_class),
862 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
863 _gtk_boolean_handled_accumulator, NULL,
864 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
866 GTK_TYPE_PRINT_OPERATION_PREVIEW,
867 GTK_TYPE_PRINT_CONTEXT,
872 * GtkPrintOperation:default-page-setup:
874 * The #GtkPageSetup used by default.
876 * This page setup will be used by gtk_print_operation_run(),
877 * but it can be overridden on a per-page basis by connecting
878 * to the #GtkPrintOperation::request-page-setup signal.
882 g_object_class_install_property (gobject_class,
883 PROP_DEFAULT_PAGE_SETUP,
884 g_param_spec_object ("default-page-setup",
885 P_("Default Page Setup"),
886 P_("The GtkPageSetup used by default"),
888 GTK_PARAM_READWRITE));
891 * GtkPrintOperation:print-settings:
893 * The #GtkPrintSettings used for initializing the dialog.
895 * Setting this property is typically used to re-establish
896 * print settings from a previous print operation, see
897 * gtk_print_operation_run().
901 g_object_class_install_property (gobject_class,
903 g_param_spec_object ("print-settings",
904 P_("Print Settings"),
905 P_("The GtkPrintSettings used for initializing the dialog"),
906 GTK_TYPE_PRINT_SETTINGS,
907 GTK_PARAM_READWRITE));
910 * GtkPrintOperation:job-name:
912 * A string used to identify the job (e.g. in monitoring
913 * applications like eggcups).
915 * If you don't set a job name, GTK+ picks a default one
916 * by numbering successive print jobs.
920 g_object_class_install_property (gobject_class,
922 g_param_spec_string ("job-name",
924 P_("A string used for identifying the print job."),
926 GTK_PARAM_READWRITE));
929 * GtkPrintOperation:n-pages:
931 * The number of pages in the document.
933 * This <emphasis>must</emphasis> be set to a positive number
934 * before the rendering starts. It may be set in a
935 * #GtkPrintOperation::begin-print signal hander.
937 * Note that the page numbers passed to the
938 * #GtkPrintOperation::request-page-setup and
939 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
940 * the user chooses to print all pages, the last ::draw-page signal
941 * will be for page @n_pages - 1.
945 g_object_class_install_property (gobject_class,
947 g_param_spec_int ("n-pages",
948 P_("Number of Pages"),
949 P_("The number of pages in the document."),
953 GTK_PARAM_READWRITE));
956 * GtkPrintOperation:current-page:
958 * The current page in the document.
960 * If this is set before gtk_print_operation_run(),
961 * the user will be able to select to print only the current page.
963 * Note that this only makes sense for pre-paginated documents.
967 g_object_class_install_property (gobject_class,
969 g_param_spec_int ("current-page",
971 P_("The current page in the document"),
975 GTK_PARAM_READWRITE));
978 * GtkPrintOperation:use-full-page:
980 * If %TRUE, the transformation for the cairo context obtained
981 * from #GtkPrintContext puts the origin at the top left corner
982 * of the page (which may not be the top left corner of the sheet,
983 * depending on page orientation and the number of pages per sheet).
984 * Otherwise, the origin is at the top left corner of the imageable
985 * area (i.e. inside the margins).
989 g_object_class_install_property (gobject_class,
991 g_param_spec_boolean ("use-full-page",
993 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
995 GTK_PARAM_READWRITE));
999 * GtkPrintOperation:track-print-status:
1001 * If %TRUE, the print operation will try to continue report on
1002 * the status of the print job in the printer queues and printer.
1003 * This can allow your application to show things like "out of paper"
1004 * issues, and when the print job actually reaches the printer.
1005 * However, this is often implemented using polling, and should
1006 * not be enabled unless needed.
1010 g_object_class_install_property (gobject_class,
1011 PROP_TRACK_PRINT_STATUS,
1012 g_param_spec_boolean ("track-print-status",
1013 P_("Track Print Status"),
1014 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."),
1016 GTK_PARAM_READWRITE));
1020 * GtkPrintOperation:unit:
1022 * The transformation for the cairo context obtained from
1023 * #GtkPrintContext is set up in such a way that distances
1024 * are measured in units of @unit.
1028 g_object_class_install_property (gobject_class,
1030 g_param_spec_enum ("unit",
1032 P_("The unit in which distances can be measured in the context"),
1035 GTK_PARAM_READWRITE));
1039 * GtkPrintOperation:show-progress:
1041 * Determines whether to show a progress dialog during the
1046 g_object_class_install_property (gobject_class,
1048 g_param_spec_boolean ("show-progress",
1050 P_("TRUE if a progress dialog is shown while printing."),
1052 GTK_PARAM_READWRITE));
1055 * GtkPrintOperation:allow-async:
1057 * Determines whether the print operation may run asynchronously or not.
1059 * Some systems don't support asynchronous printing, but those that do
1060 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1061 * emit the #GtkPrintOperation::done signal when the operation is actually
1064 * The Windows port does not support asynchronous operation at all (this
1065 * is unlikely to change). On other platforms, all actions except for
1066 * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
1070 g_object_class_install_property (gobject_class,
1072 g_param_spec_boolean ("allow-async",
1074 P_("TRUE if print process may run asynchronous."),
1076 GTK_PARAM_READWRITE));
1079 * GtkPrintOperation:export-filename:
1081 * The name of a file to generate instead of showing the print dialog.
1082 * Currently, PDF is the only supported format.
1084 * The intended use of this property is for implementing
1085 * "Export to PDF" actions.
1087 * "Print to PDF" support is independent of this and is done
1088 * by letting the user pick the "Print to PDF" item from the
1089 * list of printers in the print dialog.
1093 g_object_class_install_property (gobject_class,
1094 PROP_EXPORT_FILENAME,
1095 g_param_spec_string ("export-filename",
1096 P_("Export filename"),
1097 P_("Export filename"),
1099 GTK_PARAM_READWRITE));
1102 * GtkPrintOperation:status:
1104 * The status of the print operation.
1108 g_object_class_install_property (gobject_class,
1110 g_param_spec_enum ("status",
1112 P_("The status of the print operation"),
1113 GTK_TYPE_PRINT_STATUS,
1114 GTK_PRINT_STATUS_INITIAL,
1115 GTK_PARAM_READABLE));
1118 * GtkPrintOperation:status-string:
1120 * A string representation of the status of the print operation.
1121 * The string is translated and suitable for displaying the print
1122 * status e.g. in a #GtkStatusbar.
1124 * See the #GtkPrintOperation:status property for a status value that
1125 * is suitable for programmatic use.
1129 g_object_class_install_property (gobject_class,
1131 g_param_spec_string ("status-string",
1132 P_("Status String"),
1133 P_("A human-readable description of the status"),
1135 GTK_PARAM_READABLE));
1139 * GtkPrintOperation:custom-tab-label:
1141 * Used as the label of the tab containing custom widgets.
1142 * Note that this property may be ignored on some platforms.
1144 * If this is %NULL, GTK+ uses a default label.
1148 g_object_class_install_property (gobject_class,
1149 PROP_CUSTOM_TAB_LABEL,
1150 g_param_spec_string ("custom-tab-label",
1151 P_("Custom tab label"),
1152 P_("Label for the tab containing custom widgets."),
1154 GTK_PARAM_READWRITE));
1159 * gtk_print_operation_new:
1161 * Creates a new #GtkPrintOperation.
1163 * Returns: a new #GtkPrintOperation
1168 gtk_print_operation_new (void)
1170 GtkPrintOperation *print_operation;
1172 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1174 return print_operation;
1178 * gtk_print_operation_set_default_page_setup:
1179 * @op: a #GtkPrintOperation
1180 * @default_page_setup: a #GtkPageSetup, or %NULL
1182 * Makes @default_page_setup the default page setup for @op.
1184 * This page setup will be used by gtk_print_operation_run(),
1185 * but it can be overridden on a per-page basis by connecting
1186 * to the #GtkPrintOperation::request-page-setup signal.
1191 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1192 GtkPageSetup *default_page_setup)
1194 GtkPrintOperationPrivate *priv;
1196 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1197 g_return_if_fail (default_page_setup == NULL ||
1198 GTK_IS_PAGE_SETUP (default_page_setup));
1202 if (default_page_setup != priv->default_page_setup)
1204 if (default_page_setup)
1205 g_object_ref (default_page_setup);
1207 if (priv->default_page_setup)
1208 g_object_unref (priv->default_page_setup);
1210 priv->default_page_setup = default_page_setup;
1212 g_object_notify (G_OBJECT (op), "default-page-setup");
1217 * gtk_print_operation_get_default_page_setup:
1218 * @op: a #GtkPrintOperation
1220 * Returns the default page setup, see
1221 * gtk_print_operation_set_default_page_setup().
1223 * Returns: the default page setup
1228 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1230 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1232 return op->priv->default_page_setup;
1237 * gtk_print_operation_set_print_settings:
1238 * @op: a #GtkPrintOperation
1239 * @print_settings: #GtkPrintSettings, or %NULL
1241 * Sets the print settings for @op. This is typically used to
1242 * re-establish print settings from a previous print operation,
1243 * see gtk_print_operation_run().
1248 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1249 GtkPrintSettings *print_settings)
1251 GtkPrintOperationPrivate *priv;
1253 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1254 g_return_if_fail (print_settings == NULL ||
1255 GTK_IS_PRINT_SETTINGS (print_settings));
1259 if (print_settings != priv->print_settings)
1262 g_object_ref (print_settings);
1264 if (priv->print_settings)
1265 g_object_unref (priv->print_settings);
1267 priv->print_settings = print_settings;
1269 g_object_notify (G_OBJECT (op), "print-settings");
1274 * gtk_print_operation_get_print_settings:
1275 * @op: a #GtkPrintOperation
1277 * Returns the current print settings.
1279 * Note that the return value is %NULL until either
1280 * gtk_print_operation_set_print_settings() or
1281 * gtk_print_operation_run() have been called.
1283 * Return value: the current print settings of @op.
1288 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1290 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1292 return op->priv->print_settings;
1296 * gtk_print_operation_set_job_name:
1297 * @op: a #GtkPrintOperation
1298 * @job_name: a string that identifies the print job
1300 * Sets the name of the print job. The name is used to identify
1301 * the job (e.g. in monitoring applications like eggcups).
1303 * If you don't set a job name, GTK+ picks a default one by
1304 * numbering successive print jobs.
1309 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1310 const gchar *job_name)
1312 GtkPrintOperationPrivate *priv;
1314 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1315 g_return_if_fail (job_name != NULL);
1319 g_free (priv->job_name);
1320 priv->job_name = g_strdup (job_name);
1322 g_object_notify (G_OBJECT (op), "job-name");
1326 * gtk_print_operation_set_n_pages:
1327 * @op: a #GtkPrintOperation
1328 * @n_pages: the number of pages
1330 * Sets the number of pages in the document.
1332 * This <emphasis>must</emphasis> be set to a positive number
1333 * before the rendering starts. It may be set in a
1334 * #GtkPrintOperation::begin-print signal hander.
1336 * Note that the page numbers passed to the
1337 * #GtkPrintOperation::request-page-setup
1338 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1339 * the user chooses to print all pages, the last ::draw-page signal
1340 * will be for page @n_pages - 1.
1345 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1348 GtkPrintOperationPrivate *priv;
1350 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1351 g_return_if_fail (n_pages > 0);
1354 g_return_if_fail (priv->current_page == -1 ||
1355 priv->current_page < n_pages);
1357 if (priv->nr_of_pages != n_pages)
1359 priv->nr_of_pages = n_pages;
1361 g_object_notify (G_OBJECT (op), "n-pages");
1366 * gtk_print_operation_set_current_page:
1367 * @op: a #GtkPrintOperation
1368 * @current_page: the current page, 0-based
1370 * Sets the current page.
1372 * If this is called before gtk_print_operation_run(),
1373 * the user will be able to select to print only the current page.
1375 * Note that this only makes sense for pre-paginated documents.
1380 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1383 GtkPrintOperationPrivate *priv;
1385 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1386 g_return_if_fail (current_page >= 0);
1389 g_return_if_fail (priv->nr_of_pages == -1 ||
1390 current_page < priv->nr_of_pages);
1392 if (priv->current_page != current_page)
1394 priv->current_page = current_page;
1396 g_object_notify (G_OBJECT (op), "current-page");
1401 * gtk_print_operation_set_use_full_page:
1402 * @op: a #GtkPrintOperation
1403 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1405 * If @full_page is %TRUE, the transformation for the cairo context
1406 * obtained from #GtkPrintContext puts the origin at the top left
1407 * corner of the page (which may not be the top left corner of the
1408 * sheet, depending on page orientation and the number of pages per
1409 * sheet). Otherwise, the origin is at the top left corner of the
1410 * imageable area (i.e. inside the margins).
1415 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1418 GtkPrintOperationPrivate *priv;
1420 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1422 full_page = full_page != FALSE;
1426 if (priv->use_full_page != full_page)
1428 priv->use_full_page = full_page;
1430 g_object_notify (G_OBJECT (op), "use-full-page");
1435 * gtk_print_operation_set_unit:
1436 * @op: a #GtkPrintOperation
1437 * @unit: the unit to use
1439 * Sets up the transformation for the cairo context obtained from
1440 * #GtkPrintContext in such a way that distances are measured in
1446 gtk_print_operation_set_unit (GtkPrintOperation *op,
1449 GtkPrintOperationPrivate *priv;
1451 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1455 if (priv->unit != unit)
1459 g_object_notify (G_OBJECT (op), "unit");
1464 * gtk_print_operation_set_track_print_status:
1465 * @op: a #GtkPrintOperation
1466 * @track_status: %TRUE to track status after printing
1468 * If track_status is %TRUE, the print operation will try to continue report
1469 * on the status of the print job in the printer queues and printer. This
1470 * can allow your application to show things like "out of paper" issues,
1471 * and when the print job actually reaches the printer.
1473 * This function is often implemented using some form of polling, so it should
1474 * not be enabled unless needed.
1479 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1480 gboolean track_status)
1482 GtkPrintOperationPrivate *priv;
1484 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1488 if (priv->track_print_status != track_status)
1490 priv->track_print_status = track_status;
1492 g_object_notify (G_OBJECT (op), "track-print-status");
1497 _gtk_print_operation_set_status (GtkPrintOperation *op,
1498 GtkPrintStatus status,
1499 const gchar *string)
1501 GtkPrintOperationPrivate *priv = op->priv;
1502 static const gchar *status_strs[] = {
1503 /* translators, strip the prefix up to and including the first | */
1504 N_("print operation status|Initial state"),
1505 /* translators, strip the prefix up to and including the first | */
1506 N_("print operation status|Preparing to print"),
1507 /* translators, strip the prefix up to and including the first | */
1508 N_("print operation status|Generating data"),
1509 /* translators, strip the prefix up to and including the first | */
1510 N_("print operation status|Sending data"),
1511 /* translators, strip the prefix up to and including the first | */
1512 N_("print operation status|Waiting"),
1513 /* translators, strip the prefix up to and including the first | */
1514 N_("print operation status|Blocking on issue"),
1515 /* translators, strip the prefix up to and including the first | */
1516 N_("print operation status|Printing"),
1517 /* translators, strip the prefix up to and including the first | */
1518 N_("print operation status|Finished"),
1519 /* translators, strip the prefix up to and including the first | */
1520 N_("print operation status|Finished with error")
1523 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1524 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1527 string = g_strip_context (status_strs[status], _(status_strs[status]));
1529 if (priv->status == status &&
1530 strcmp (string, priv->status_string) == 0)
1533 g_free (priv->status_string);
1534 priv->status_string = g_strdup (string);
1535 priv->status = status;
1537 g_object_notify (G_OBJECT (op), "status");
1538 g_object_notify (G_OBJECT (op), "status-string");
1540 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1545 * gtk_print_operation_get_status:
1546 * @op: a #GtkPrintOperation
1548 * Returns the status of the print operation.
1549 * Also see gtk_print_operation_get_status_string().
1551 * Return value: the status of the print operation
1556 gtk_print_operation_get_status (GtkPrintOperation *op)
1558 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1559 GTK_PRINT_STATUS_FINISHED_ABORTED);
1561 return op->priv->status;
1565 * gtk_print_operation_get_status_string:
1566 * @op: a #GtkPrintOperation
1568 * Returns a string representation of the status of the
1569 * print operation. The string is translated and suitable
1570 * for displaying the print status e.g. in a #GtkStatusbar.
1572 * Use gtk_print_operation_get_status() to obtain a status
1573 * value that is suitable for programmatic use.
1575 * Return value: a string representation of the status
1576 * of the print operation
1580 G_CONST_RETURN gchar *
1581 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1583 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1585 return op->priv->status_string;
1589 * gtk_print_operation_is_finished:
1590 * @op: a #GtkPrintOperation
1592 * A convenience function to find out if the print operation
1593 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1594 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1596 * Note: when you enable print status tracking the print operation
1597 * can be in a non-finished state even after done has been called, as
1598 * the operation status then tracks the print job status on the printer.
1600 * Return value: %TRUE, if the print operation is finished.
1605 gtk_print_operation_is_finished (GtkPrintOperation *op)
1607 GtkPrintOperationPrivate *priv;
1609 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1613 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1614 priv->status == GTK_PRINT_STATUS_FINISHED;
1618 * gtk_print_operation_set_show_progress:
1619 * @op: a #GtkPrintOperation
1620 * @show_progress: %TRUE to show a progress dialog
1622 * If @show_progress is %TRUE, the print operation will show a
1623 * progress dialog during the print operation.
1628 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1629 gboolean show_progress)
1631 GtkPrintOperationPrivate *priv;
1633 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1637 show_progress = show_progress != FALSE;
1639 if (priv->show_progress != show_progress)
1641 priv->show_progress = show_progress;
1643 g_object_notify (G_OBJECT (op), "show-progress");
1648 * gtk_print_operation_set_allow_async:
1649 * @op: a #GtkPrintOperation
1650 * @allow_async: %TRUE to allow asynchronous operation
1652 * Sets whether the gtk_print_operation_run() may return
1653 * before the print operation is completed. Note that
1654 * some platforms may not allow asynchronous operation.
1659 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1660 gboolean allow_async)
1662 GtkPrintOperationPrivate *priv;
1664 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1668 allow_async = allow_async != FALSE;
1670 if (priv->allow_async != allow_async)
1672 priv->allow_async = allow_async;
1674 g_object_notify (G_OBJECT (op), "allow-async");
1680 * gtk_print_operation_set_custom_tab_label:
1681 * @op: a #GtkPrintOperation
1682 * @label: the label to use, or %NULL to use the default label
1684 * Sets the label for the tab holding custom widgets.
1689 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1692 GtkPrintOperationPrivate *priv;
1694 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1698 g_free (priv->custom_tab_label);
1699 priv->custom_tab_label = g_strdup (label);
1701 g_object_notify (G_OBJECT (op), "custom-tab-label");
1706 * gtk_print_operation_set_export_filename:
1707 * @op: a #GtkPrintOperation
1708 * @filename: the filename for the exported file
1710 * Sets up the #GtkPrintOperation to generate a file instead
1711 * of showing the print dialog. The indended use of this function
1712 * is for implementing "Export to PDF" actions. Currently, PDF
1713 * is the only supported format.
1715 * "Print to PDF" support is independent of this and is done
1716 * by letting the user pick the "Print to PDF" item from the list
1717 * of printers in the print dialog.
1722 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1723 const gchar *filename)
1725 GtkPrintOperationPrivate *priv;
1727 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1731 g_free (priv->export_filename);
1732 priv->export_filename = g_strdup (filename);
1734 g_object_notify (G_OBJECT (op), "export-filename");
1737 /* Creates the initial page setup used for printing unless the
1738 * app overrides this on a per-page basis using request_page_setup.
1740 * Data is taken from, in order, if existing:
1742 * PrintSettings returned from the print dialog
1743 * (initial dialog values are set from default_page_setup
1744 * if unset in app specified print_settings)
1745 * default_page_setup
1746 * per-locale default setup
1748 static GtkPageSetup *
1749 create_page_setup (GtkPrintOperation *op)
1751 GtkPrintOperationPrivate *priv = op->priv;
1752 GtkPageSetup *page_setup;
1753 GtkPrintSettings *settings;
1755 if (priv->default_page_setup)
1756 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1758 page_setup = gtk_page_setup_new ();
1760 settings = priv->print_settings;
1763 GtkPaperSize *paper_size;
1765 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1766 gtk_page_setup_set_orientation (page_setup,
1767 gtk_print_settings_get_orientation (settings));
1770 paper_size = gtk_print_settings_get_paper_size (settings);
1773 gtk_page_setup_set_paper_size (page_setup, paper_size);
1774 gtk_paper_size_free (paper_size);
1777 /* TODO: Margins? */
1784 pdf_start_page (GtkPrintOperation *op,
1785 GtkPrintContext *print_context,
1786 GtkPageSetup *page_setup)
1788 GtkPaperSize *paper_size;
1789 cairo_surface_t *surface = op->priv->platform_data;
1792 paper_size = gtk_page_setup_get_paper_size (page_setup);
1794 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1795 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1797 cairo_pdf_surface_set_size (surface, w, h);
1801 pdf_end_page (GtkPrintOperation *op,
1802 GtkPrintContext *print_context)
1806 cr = gtk_print_context_get_cairo_context (print_context);
1807 cairo_show_page (cr);
1811 pdf_end_run (GtkPrintOperation *op,
1815 GtkPrintOperationPrivate *priv = op->priv;
1816 cairo_surface_t *surface = priv->platform_data;
1818 cairo_surface_finish (surface);
1819 cairo_surface_destroy (surface);
1821 priv->platform_data = NULL;
1822 priv->free_platform_data = NULL;
1825 static GtkPrintOperationResult
1826 run_pdf (GtkPrintOperation *op,
1830 GtkPrintOperationPrivate *priv = op->priv;
1831 GtkPageSetup *page_setup;
1832 cairo_surface_t *surface;
1834 gdouble width, height;
1836 priv->print_context = _gtk_print_context_new (op);
1838 page_setup = create_page_setup (op);
1839 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
1841 /* This will be overwritten later by the non-default size, but
1842 we need to pass some size: */
1843 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1844 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1845 g_object_unref (page_setup);
1847 surface = cairo_pdf_surface_create (priv->export_filename,
1849 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
1851 g_set_error_literal (&priv->error,
1853 GTK_PRINT_ERROR_GENERAL,
1854 cairo_status_to_string (cairo_surface_status (surface)));
1856 return GTK_PRINT_OPERATION_RESULT_ERROR;
1859 /* this would crash on a nil surface */
1860 cairo_surface_set_fallback_resolution (surface, 300, 300);
1862 priv->platform_data = surface;
1863 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
1865 cr = cairo_create (surface);
1866 gtk_print_context_set_cairo_context (op->priv->print_context,
1871 priv->print_pages = GTK_PRINT_PAGES_ALL;
1872 priv->page_ranges = NULL;
1873 priv->num_page_ranges = 0;
1875 priv->manual_num_copies = 1;
1876 priv->manual_collation = FALSE;
1877 priv->manual_reverse = FALSE;
1878 priv->manual_page_set = GTK_PAGE_SET_ALL;
1879 priv->manual_scale = 1.0;
1880 priv->manual_orientation = TRUE;
1884 priv->start_page = pdf_start_page;
1885 priv->end_page = pdf_end_page;
1886 priv->end_run = pdf_end_run;
1888 return GTK_PRINT_OPERATION_RESULT_APPLY;
1893 GtkPrintOperation *op;
1894 gint uncollated_copies;
1895 gint collated_copies;
1896 gint uncollated, collated, total;
1898 gint range, num_ranges;
1899 GtkPageRange *ranges;
1900 GtkPageRange one_range;
1902 gint page, start, end, inc;
1904 GtkWidget *progress;
1906 gboolean initialized;
1907 gboolean is_preview;
1911 find_range (PrintPagesData *data)
1913 GtkPageRange *range;
1915 range = &data->ranges[data->range];
1919 data->start = range->end;
1920 data->end = range->start - 1;
1924 data->start = range->start;
1925 data->end = range->end + 1;
1930 clamp_page_ranges (PrintPagesData *data)
1932 GtkPrintOperationPrivate *priv;
1933 gint num_of_correct_ranges;
1936 priv = data->op->priv;
1938 num_of_correct_ranges = 0;
1940 for (i = 0; i < data->num_ranges; i++)
1941 if ((data->ranges[i].start >= 0) &&
1942 (data->ranges[i].start < priv->nr_of_pages) &&
1943 (data->ranges[i].end >= 0) &&
1944 (data->ranges[i].end < priv->nr_of_pages))
1946 data->ranges[num_of_correct_ranges] = data->ranges[i];
1947 num_of_correct_ranges++;
1949 else if ((data->ranges[i].start >= 0) &&
1950 (data->ranges[i].start < priv->nr_of_pages) &&
1951 (data->ranges[i].end >= priv->nr_of_pages))
1953 data->ranges[i].end = priv->nr_of_pages - 1;
1954 data->ranges[num_of_correct_ranges] = data->ranges[i];
1955 num_of_correct_ranges++;
1957 else if ((data->ranges[i].end >= 0) &&
1958 (data->ranges[i].end < priv->nr_of_pages) &&
1959 (data->ranges[i].start < 0))
1961 data->ranges[i].start = 0;
1962 data->ranges[num_of_correct_ranges] = data->ranges[i];
1963 num_of_correct_ranges++;
1966 data->num_ranges = num_of_correct_ranges;
1970 increment_page_sequence (PrintPagesData *data)
1972 GtkPrintOperationPrivate *priv = data->op->priv;
1975 data->page += data->inc;
1976 if (data->page == data->end)
1978 data->range += data->inc;
1979 if (data->range == -1 || data->range == data->num_ranges)
1982 if (data->uncollated == data->uncollated_copies)
1985 data->range = data->inc < 0 ? data->num_ranges - 1 : 0;
1988 data->page = data->start;
1991 while ((priv->manual_page_set == GTK_PAGE_SET_EVEN && data->page % 2 == 0) ||
1992 (priv->manual_page_set == GTK_PAGE_SET_ODD && data->page % 2 == 1));
1998 print_pages_idle_done (gpointer user_data)
2000 PrintPagesData *data;
2001 GtkPrintOperationPrivate *priv;
2003 data = (PrintPagesData*)user_data;
2004 priv = data->op->priv;
2006 priv->print_pages_idle_id = 0;
2008 if (priv->show_progress_timeout_id > 0)
2010 g_source_remove (priv->show_progress_timeout_id);
2011 priv->show_progress_timeout_id = 0;
2015 gtk_widget_destroy (data->progress);
2017 if (priv->rloop && !data->is_preview)
2018 g_main_loop_quit (priv->rloop);
2020 if (!data->is_preview)
2021 g_signal_emit (data->op, signals[DONE], 0,
2023 GTK_PRINT_OPERATION_RESULT_CANCEL :
2024 GTK_PRINT_OPERATION_RESULT_APPLY);
2026 g_object_unref (data->op);
2031 update_progress (PrintPagesData *data)
2033 GtkPrintOperationPrivate *priv;
2036 priv = data->op->priv;
2040 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2042 if (priv->nr_of_pages > 0)
2043 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages);
2045 text = g_strdup (_("Preparing"));
2047 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2048 text = g_strdup_printf (_("Printing %d"), data->total);
2052 g_object_set (data->progress, "text", text, NULL);
2059 common_render_page (GtkPrintOperation *op,
2062 GtkPrintOperationPrivate *priv = op->priv;
2063 GtkPageSetup *page_setup;
2064 GtkPrintContext *print_context;
2067 print_context = priv->print_context;
2069 page_setup = create_page_setup (op);
2071 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2072 print_context, page_nr, page_setup);
2074 _gtk_print_context_set_page_setup (print_context, page_setup);
2076 priv->start_page (op, print_context, page_setup);
2078 cr = gtk_print_context_get_cairo_context (print_context);
2081 if (priv->manual_scale != 1.0)
2084 priv->manual_scale);
2086 if (priv->manual_orientation)
2087 _gtk_print_context_rotate_according_to_orientation (print_context);
2089 if (!priv->use_full_page)
2090 _gtk_print_context_translate_into_margin (print_context);
2092 g_signal_emit (op, signals[DRAW_PAGE], 0,
2093 print_context, page_nr);
2095 priv->end_page (op, print_context);
2099 g_object_unref (page_setup);
2103 print_pages_idle (gpointer user_data)
2105 PrintPagesData *data;
2106 GtkPrintOperationPrivate *priv;
2107 GtkPageSetup *page_setup;
2108 gboolean done = FALSE;
2111 data = (PrintPagesData*)user_data;
2112 priv = data->op->priv;
2114 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2116 if (!data->initialized)
2118 data->initialized = TRUE;
2119 page_setup = create_page_setup (data->op);
2120 _gtk_print_context_set_page_setup (priv->print_context,
2122 g_object_unref (page_setup);
2124 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2126 if (priv->manual_collation)
2128 data->uncollated_copies = priv->manual_num_copies;
2129 data->collated_copies = 1;
2133 data->uncollated_copies = 1;
2134 data->collated_copies = priv->manual_num_copies;
2140 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2142 gboolean paginated = FALSE;
2144 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2149 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2151 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2153 if (priv->page_ranges == NULL)
2155 g_warning ("no pages to print");
2156 priv->cancelled = TRUE;
2159 data->ranges = priv->page_ranges;
2160 data->num_ranges = priv->num_page_ranges;
2161 for (i = 0; i < data->num_ranges; i++)
2162 if (data->ranges[i].end == -1 ||
2163 data->ranges[i].end >= priv->nr_of_pages)
2164 data->ranges[i].end = priv->nr_of_pages - 1;
2166 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2167 priv->current_page != -1)
2169 data->ranges = &data->one_range;
2170 data->num_ranges = 1;
2171 data->ranges[0].start = priv->current_page;
2172 data->ranges[0].end = priv->current_page;
2176 data->ranges = &data->one_range;
2177 data->num_ranges = 1;
2178 data->ranges[0].start = 0;
2179 data->ranges[0].end = priv->nr_of_pages - 1;
2182 clamp_page_ranges (data);
2184 if (priv->manual_reverse)
2186 data->range = data->num_ranges - 1;
2196 /* go back one page, since we preincrement below */
2197 data->page = data->start - data->inc;
2198 data->collated = data->collated_copies - 1;
2200 _gtk_print_operation_set_status (data->op,
2201 GTK_PRINT_STATUS_GENERATING_DATA,
2209 if (data->collated == data->collated_copies)
2212 if (!increment_page_sequence (data))
2220 if (data->is_preview && !priv->cancelled)
2224 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2228 common_render_page (data->op, data->page);
2232 if (priv->cancelled)
2234 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2236 data->is_preview = FALSE;
2240 if (done && !data->is_preview)
2242 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2243 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2246 update_progress (data);
2252 handle_progress_response (GtkWidget *dialog,
2256 GtkPrintOperation *op = (GtkPrintOperation *)data;
2258 gtk_widget_hide (dialog);
2259 gtk_print_operation_cancel (op);
2263 show_progress_timeout (PrintPagesData *data)
2265 gtk_window_present (GTK_WINDOW (data->progress));
2267 data->op->priv->show_progress_timeout_id = 0;
2273 print_pages (GtkPrintOperation *op,
2276 GtkPrintOperationResult result)
2278 GtkPrintOperationPrivate *priv = op->priv;
2279 PrintPagesData *data;
2283 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2284 g_signal_emit (op, signals[DONE], 0, result);
2288 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2290 data = g_new0 (PrintPagesData, 1);
2291 data->op = g_object_ref (op);
2292 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2294 if (priv->show_progress)
2296 GtkWidget *progress;
2298 progress = gtk_message_dialog_new (parent, 0,
2302 g_signal_connect (progress, "response",
2303 G_CALLBACK (handle_progress_response), op);
2305 priv->show_progress_timeout_id =
2306 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2307 (GSourceFunc)show_progress_timeout,
2310 data->progress = progress;
2313 if (data->is_preview)
2317 g_signal_emit_by_name (op, "preview",
2318 GTK_PRINT_OPERATION_PREVIEW (op),
2319 priv->print_context,
2325 GtkWidget *error_dialog;
2327 error_dialog = gtk_message_dialog_new (parent,
2328 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
2331 _("Error creating print preview"));
2333 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
2334 _("The most probable reason is that a temporary file could not be created."));
2336 if (parent && parent->group)
2337 gtk_window_group_add_window (parent->group, GTK_WINDOW (error_dialog));
2339 g_signal_connect (error_dialog, "response",
2340 G_CALLBACK (gtk_widget_destroy), NULL);
2342 gtk_widget_show (error_dialog);
2344 print_pages_idle_done (data);
2349 if (gtk_print_context_get_cairo_context (priv->print_context) == NULL)
2351 /* Programmer error */
2352 g_error ("You must set a cairo context on the print context");
2355 priv->start_page = preview_start_page;
2356 priv->end_page = preview_end_page;
2357 priv->end_run = preview_end_run;
2359 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
2360 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
2361 &priv->num_page_ranges);
2362 priv->manual_num_copies = 1;
2363 priv->manual_collation = FALSE;
2364 priv->manual_reverse = FALSE;
2365 priv->manual_page_set = GTK_PAGE_SET_ALL;
2366 priv->manual_scale = 1.0;
2367 priv->manual_orientation = TRUE;
2370 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
2373 print_pages_idle_done);
2375 /* Recursive main loop to make sure we don't exit on sync operations */
2378 priv->rloop = g_main_loop_new (NULL, FALSE);
2381 GDK_THREADS_LEAVE ();
2382 g_main_loop_run (priv->rloop);
2383 GDK_THREADS_ENTER ();
2385 g_main_loop_unref (priv->rloop);
2387 g_object_unref (op);
2392 * gtk_print_operation_get_error:
2393 * @op: a #GtkPrintOperation
2394 * @error: return location for the error
2396 * Call this when the result of a print operation is
2397 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
2398 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
2399 * handler. The returned #GError will contain more details on what went wrong.
2404 gtk_print_operation_get_error (GtkPrintOperation *op,
2407 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2409 g_propagate_error (error, op->priv->error);
2411 op->priv->error = NULL;
2416 * gtk_print_operation_run:
2417 * @op: a #GtkPrintOperation
2418 * @action: the action to start
2419 * @parent: Transient parent of the dialog, or %NULL
2420 * @error: Return location for errors, or %NULL
2422 * Runs the print operation, by first letting the user modify
2423 * print settings in the print dialog, and then print the document.
2425 * Normally that this function does not return until the rendering of all
2426 * pages is complete. You can connect to the
2427 * #GtkPrintOperation::status-changed signal on @op to obtain some
2428 * information about the progress of the print operation.
2429 * Furthermore, it may use a recursive mainloop to show the print dialog.
2431 * If you call gtk_print_operation_set_allow_async() or set the
2432 * #GtkPrintOperation:allow-async property the operation will run
2433 * asynchronously if this is supported on the platform. The
2434 * #GtkPrintOperation::done signal will be emitted with the result of the
2435 * operation when the it is done (i.e. when the dialog is canceled, or when
2436 * the print succeeds or fails).
2438 * if (settings != NULL)
2439 * gtk_print_operation_set_print_settings (print, settings);
2441 * if (page_setup != NULL)
2442 * gtk_print_operation_set_default_page_setup (print, page_setup);
2444 * g_signal_connect (print, "begin-print",
2445 * G_CALLBACK (begin_print), &data);
2446 * g_signal_connect (print, "draw-page",
2447 * G_CALLBACK (draw_page), &data);
2449 * res = gtk_print_operation_run (print,
2450 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2454 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
2456 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
2457 * GTK_DIALOG_DESTROY_WITH_PARENT,
2458 * GTK_MESSAGE_ERROR,
2459 * GTK_BUTTONS_CLOSE,
2460 * "Error printing file:\n%s",
2462 * g_signal_connect (error_dialog, "response",
2463 * G_CALLBACK (gtk_widget_destroy), NULL);
2464 * gtk_widget_show (error_dialog);
2465 * g_error_free (error);
2467 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
2469 * if (settings != NULL)
2470 * g_object_unref (settings);
2471 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
2475 * Note that gtk_print_operation_run() can only be called once on a
2476 * given #GtkPrintOperation.
2478 * Return value: the result of the print operation. A return value of
2479 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
2480 * completed successfully. In this case, it is a good idea to obtain
2481 * the used print settings with gtk_print_operation_get_print_settings()
2482 * and store them for reuse with the next print operation. A value of
2483 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
2484 * asynchronously, and will emit the #GtkPrintOperation::done signal when
2489 GtkPrintOperationResult
2490 gtk_print_operation_run (GtkPrintOperation *op,
2491 GtkPrintOperationAction action,
2495 GtkPrintOperationPrivate *priv;
2496 GtkPrintOperationResult result;
2497 GtkPageSetup *page_setup;
2499 gboolean run_print_pages;
2501 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
2502 GTK_PRINT_OPERATION_RESULT_ERROR);
2503 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
2504 GTK_PRINT_OPERATION_RESULT_ERROR);
2507 run_print_pages = TRUE;
2510 priv->action = action;
2512 if (priv->print_settings == NULL)
2513 priv->print_settings = gtk_print_settings_new ();
2515 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
2517 /* note: if you implement async EXPORT, update the docs
2518 * docs for the allow-async property.
2520 priv->is_sync = TRUE;
2521 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
2522 result = run_pdf (op, parent, &do_print);
2524 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
2526 priv->is_sync = !priv->allow_async;
2527 priv->print_context = _gtk_print_context_new (op);
2528 page_setup = create_page_setup (op);
2529 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2530 g_object_unref (page_setup);
2532 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2535 else if (priv->allow_async)
2537 priv->is_sync = FALSE;
2538 _gtk_print_operation_platform_backend_run_dialog_async (op,
2539 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2542 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2543 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
2548 priv->is_sync = TRUE;
2549 result = _gtk_print_operation_platform_backend_run_dialog (op,
2550 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2555 if (run_print_pages)
2556 print_pages (op, parent, do_print, result);
2558 if (priv->error && error)
2559 *error = g_error_copy (priv->error);
2565 * gtk_print_operation_cancel:
2566 * @op: a #GtkPrintOperation
2568 * Cancels a running print operation. This function may
2569 * be called from a #GtkPrintOperation::begin-print,
2570 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
2571 * signal handler to stop the currently running print
2577 gtk_print_operation_cancel (GtkPrintOperation *op)
2579 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2581 op->priv->cancelled = TRUE;
2586 #define __GTK_PRINT_OPERATION_C__
2587 #include "gtkaliasdef.c"