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.
28 #include "gtkprintoperation-private.h"
29 #include "gtkmarshalers.h"
30 #include <cairo-pdf.h>
32 #include "gtkprivate.h"
33 #include "gtkmessagedialog.h"
36 #define SHOW_PROGRESS_TIME 1200
38 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
59 PROP_DEFAULT_PAGE_SETUP,
65 PROP_TRACK_PRINT_STATUS,
75 static guint signals[LAST_SIGNAL] = { 0 };
76 static int job_nr = 0;
78 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
79 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
80 static void common_render_page (GtkPrintOperation *op,
84 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
85 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
89 * gtk_print_error_quark:
91 * Registers an error quark for #GtkPrintOperation if necessary.
93 * Return value: The error quark used for #GtkPrintOperation errors.
98 gtk_print_error_quark (void)
100 static GQuark quark = 0;
102 quark = g_quark_from_static_string ("gtk-print-error-quark");
107 gtk_print_operation_finalize (GObject *object)
109 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
110 GtkPrintOperationPrivate *priv = print_operation->priv;
112 if (priv->free_platform_data &&
115 priv->free_platform_data (priv->platform_data);
116 priv->free_platform_data = NULL;
119 if (priv->default_page_setup)
120 g_object_unref (priv->default_page_setup);
122 if (priv->print_settings)
123 g_object_unref (priv->print_settings);
125 if (priv->print_context)
126 g_object_unref (priv->print_context);
128 g_free (priv->export_filename);
129 g_free (priv->job_name);
130 g_free (priv->custom_tab_label);
131 g_free (priv->status_string);
133 if (priv->print_pages_idle_id > 0)
134 g_source_remove (priv->print_pages_idle_id);
136 if (priv->show_progress_timeout_id > 0)
137 g_source_remove (priv->show_progress_timeout_id);
140 g_error_free (priv->error);
142 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
146 gtk_print_operation_init (GtkPrintOperation *operation)
148 GtkPrintOperationPrivate *priv;
151 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
153 priv->status = GTK_PRINT_STATUS_INITIAL;
154 priv->status_string = g_strdup ("");
155 priv->default_page_setup = NULL;
156 priv->print_settings = NULL;
157 priv->nr_of_pages = -1;
158 priv->nr_of_pages_to_print = -1;
159 priv->page_position = -1;
160 priv->current_page = -1;
161 priv->use_full_page = FALSE;
162 priv->show_progress = FALSE;
163 priv->export_filename = NULL;
164 priv->track_print_status = FALSE;
165 priv->is_sync = FALSE;
167 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
170 priv->unit = GTK_UNIT_PIXEL;
172 appname = g_get_application_name ();
175 /* translators: this string is the default job title for print
176 * jobs. %s gets replaced by the application name, %d gets replaced
179 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
183 preview_iface_render_page (GtkPrintOperationPreview *preview,
187 GtkPrintOperation *op;
189 op = GTK_PRINT_OPERATION (preview);
190 common_render_page (op, page_nr);
194 preview_iface_end_preview (GtkPrintOperationPreview *preview)
196 GtkPrintOperation *op;
198 op = GTK_PRINT_OPERATION (preview);
200 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
203 g_main_loop_quit (op->priv->rloop);
205 if (op->priv->end_run)
206 op->priv->end_run (op, op->priv->is_sync, TRUE);
208 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
210 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
214 preview_iface_is_selected (GtkPrintOperationPreview *preview,
217 GtkPrintOperation *op;
218 GtkPrintOperationPrivate *priv;
221 op = GTK_PRINT_OPERATION (preview);
224 switch (priv->print_pages)
226 case GTK_PRINT_PAGES_ALL:
227 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
228 case GTK_PRINT_PAGES_CURRENT:
229 return page_nr == priv->current_page;
230 case GTK_PRINT_PAGES_RANGES:
231 for (i = 0; i < priv->num_page_ranges; i++)
233 if (page_nr >= priv->page_ranges[i].start &&
234 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
243 preview_iface_init (GtkPrintOperationPreviewIface *iface)
245 iface->render_page = preview_iface_render_page;
246 iface->end_preview = preview_iface_end_preview;
247 iface->is_selected = preview_iface_is_selected;
251 preview_start_page (GtkPrintOperation *op,
252 GtkPrintContext *print_context,
253 GtkPageSetup *page_setup)
255 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
259 preview_end_page (GtkPrintOperation *op,
260 GtkPrintContext *print_context)
265 preview_end_run (GtkPrintOperation *op,
269 g_free (op->priv->page_ranges);
270 op->priv->page_ranges = NULL;
275 gtk_print_operation_set_property (GObject *object,
280 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
284 case PROP_DEFAULT_PAGE_SETUP:
285 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
287 case PROP_PRINT_SETTINGS:
288 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
291 gtk_print_operation_set_job_name (op, g_value_get_string (value));
294 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
296 case PROP_CURRENT_PAGE:
297 gtk_print_operation_set_current_page (op, g_value_get_int (value));
299 case PROP_USE_FULL_PAGE:
300 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
302 case PROP_TRACK_PRINT_STATUS:
303 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
306 gtk_print_operation_set_unit (op, g_value_get_enum (value));
308 case PROP_ALLOW_ASYNC:
309 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
311 case PROP_SHOW_PROGRESS:
312 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
314 case PROP_EXPORT_FILENAME:
315 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
317 case PROP_CUSTOM_TAB_LABEL:
318 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
321 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
327 gtk_print_operation_get_property (GObject *object,
332 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
333 GtkPrintOperationPrivate *priv = op->priv;
337 case PROP_DEFAULT_PAGE_SETUP:
338 g_value_set_object (value, priv->default_page_setup);
340 case PROP_PRINT_SETTINGS:
341 g_value_set_object (value, priv->print_settings);
344 g_value_set_string (value, priv->job_name);
347 g_value_set_int (value, priv->nr_of_pages);
349 case PROP_CURRENT_PAGE:
350 g_value_set_int (value, priv->current_page);
352 case PROP_USE_FULL_PAGE:
353 g_value_set_boolean (value, priv->use_full_page);
355 case PROP_TRACK_PRINT_STATUS:
356 g_value_set_boolean (value, priv->track_print_status);
359 g_value_set_enum (value, priv->unit);
361 case PROP_ALLOW_ASYNC:
362 g_value_set_boolean (value, priv->allow_async);
364 case PROP_SHOW_PROGRESS:
365 g_value_set_boolean (value, priv->show_progress);
367 case PROP_EXPORT_FILENAME:
368 g_value_set_string (value, priv->export_filename);
371 g_value_set_enum (value, priv->status);
373 case PROP_STATUS_STRING:
374 g_value_set_string (value, priv->status_string);
376 case PROP_CUSTOM_TAB_LABEL:
377 g_value_set_string (value, priv->custom_tab_label);
380 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
387 GtkPrintOperationPreview *preview;
388 GtkPrintContext *print_context;
390 cairo_surface_t *surface;
397 preview_print_idle_done (gpointer data)
399 GtkPrintOperation *op;
400 PreviewOp *pop = (PreviewOp *) data;
402 op = GTK_PRINT_OPERATION (pop->preview);
404 cairo_surface_finish (pop->surface);
405 /* Surface is destroyed in launch_preview */
406 _gtk_print_operation_platform_backend_launch_preview (op,
411 g_free (pop->filename);
413 gtk_print_operation_preview_end_preview (pop->preview);
420 preview_print_idle (gpointer data)
423 GtkPrintOperation *op;
424 gboolean retval = TRUE;
426 GtkPrintOperationPrivate *priv;
428 pop = (PreviewOp *) data;
429 op = GTK_PRINT_OPERATION (pop->preview);
433 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
435 /* TODO: print out sheets not pages and follow ranges */
436 if (pop->page_nr >= op->priv->nr_of_pages)
439 if (pop->page_nr > 0)
441 cr = gtk_print_context_get_cairo_context (pop->print_context);
442 _gtk_print_operation_platform_backend_preview_end_page (op, pop->surface, cr);
447 gtk_print_operation_preview_render_page (pop->preview, pop->page_nr);
456 preview_got_page_size (GtkPrintOperationPreview *preview,
457 GtkPrintContext *context,
458 GtkPageSetup *page_setup,
461 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
464 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
466 cr = gtk_print_context_get_cairo_context (pop->print_context);
467 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
472 preview_ready (GtkPrintOperationPreview *preview,
473 GtkPrintContext *context,
477 pop->print_context = context;
479 g_object_ref (preview);
481 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
484 preview_print_idle_done);
489 gtk_print_operation_preview_handler (GtkPrintOperation *op,
490 GtkPrintOperationPreview *preview,
491 GtkPrintContext *context,
494 gdouble dpi_x, dpi_y;
496 GtkPageSetup *page_setup;
499 pop = g_new0 (PreviewOp, 1);
500 pop->filename = NULL;
501 pop->preview = preview;
502 pop->parent = parent;
504 page_setup = gtk_print_context_get_page_setup (context);
507 _gtk_print_operation_platform_backend_create_preview_surface (op,
512 if (pop->surface == NULL)
518 cr = cairo_create (pop->surface);
519 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
523 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
524 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
530 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
536 gtk_print_operation_done (GtkPrintOperation *operation,
537 GtkPrintOperationResult result)
539 GtkPrintOperationPrivate *priv = operation->priv;
541 if (priv->print_context)
543 g_object_unref (priv->print_context);
544 priv->print_context = NULL;
549 custom_widget_accumulator (GSignalInvocationHint *ihint,
551 const GValue *handler_return,
554 gboolean continue_emission;
557 widget = g_value_get_object (handler_return);
559 g_value_set_object (return_accu, widget);
560 continue_emission = (widget == NULL);
562 return continue_emission;
566 gtk_print_operation_class_init (GtkPrintOperationClass *class)
568 GObjectClass *gobject_class = (GObjectClass *)class;
570 gobject_class->set_property = gtk_print_operation_set_property;
571 gobject_class->get_property = gtk_print_operation_get_property;
572 gobject_class->finalize = gtk_print_operation_finalize;
574 class->preview = gtk_print_operation_preview_handler;
575 class->create_custom_widget = gtk_print_operation_create_custom_widget;
576 class->done = gtk_print_operation_done;
578 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
581 * GtkPrintOperation::done:
582 * @operation: the #GtkPrintOperation on which the signal was emitted
583 * @result: the result of the print operation
585 * Emitted when the print operation run has finished doing
586 * everything required for printing.
588 * @result gives you information about what happened during the run.
589 * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
590 * gtk_print_operation_get_error() for more information.
592 * If you enabled print status tracking then
593 * gtk_print_operation_is_finished() may still return %FALSE
594 * after #GtkPrintOperation::done was emitted.
599 g_signal_new (I_("done"),
600 G_TYPE_FROM_CLASS (gobject_class),
602 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
604 g_cclosure_marshal_VOID__ENUM,
605 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
608 * GtkPrintOperation::begin-print:
609 * @operation: the #GtkPrintOperation on which the signal was emitted
610 * @context: the #GtkPrintContext for the current operation
612 * Emitted after the user has finished changing print settings
613 * in the dialog, before the actual rendering starts.
615 * A typical use for ::begin-print is to use the parameters from the
616 * #GtkPrintContext and paginate the document accordingly, and then
617 * set the number of pages with gtk_print_operation_set_n_pages().
621 signals[BEGIN_PRINT] =
622 g_signal_new (I_("begin-print"),
623 G_TYPE_FROM_CLASS (gobject_class),
625 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
627 g_cclosure_marshal_VOID__OBJECT,
628 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
631 * GtkPrintOperation::paginate:
632 * @operation: the #GtkPrintOperation on which the signal was emitted
633 * @context: the #GtkPrintContext for the current operation
635 * Emitted after the #GtkPrintOperation::begin-print signal, but before
636 * the actual rendering starts. It keeps getting emitted until a connected
637 * signal handler returns %TRUE.
639 * The ::paginate signal is intended to be used for paginating a document
640 * in small chunks, to avoid blocking the user interface for a long
641 * time. The signal handler should update the number of pages using
642 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
643 * has been completely paginated.
645 * If you don't need to do pagination in chunks, you can simply do
646 * it all in the ::begin-print handler, and set the number of pages
649 * Return value: %TRUE if pagination is complete
654 g_signal_new (I_("paginate"),
655 G_TYPE_FROM_CLASS (gobject_class),
657 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
658 _gtk_boolean_handled_accumulator, NULL,
659 _gtk_marshal_BOOLEAN__OBJECT,
660 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
664 * GtkPrintOperation::request-page-setup:
665 * @operation: the #GtkPrintOperation on which the signal was emitted
666 * @context: the #GtkPrintContext for the current operation
667 * @page_nr: the number of the currently printed page (0-based)
668 * @setup: the #GtkPageSetup
670 * Emitted once for every page that is printed, to give
671 * the application a chance to modify the page setup. Any changes
672 * done to @setup will be in force only for printing this page.
676 signals[REQUEST_PAGE_SETUP] =
677 g_signal_new (I_("request-page-setup"),
678 G_TYPE_FROM_CLASS (gobject_class),
680 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
682 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
684 GTK_TYPE_PRINT_CONTEXT,
686 GTK_TYPE_PAGE_SETUP);
689 * GtkPrintOperation::draw-page:
690 * @operation: the #GtkPrintOperation on which the signal was emitted
691 * @context: the #GtkPrintContext for the current operation
692 * @page_nr: the number of the currently printed page (0-based)
694 * Emitted for every page that is printed. The signal handler
695 * must render the @page_nr's page onto the cairo context obtained
696 * from @context using gtk_print_context_get_cairo_context().
699 * draw_page (GtkPrintOperation *operation,
700 * GtkPrintContext *context,
702 * gpointer user_data)
705 * PangoLayout *layout;
706 * gdouble width, text_height;
707 * gint layout_height;
708 * PangoFontDescription *desc;
710 * cr = gtk_print_context_get_cairo_context (context);
711 * width = gtk_print_context_get_width (context);
713 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
715 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
718 * layout = gtk_print_context_create_pango_layout (context);
720 * desc = pango_font_description_from_string ("sans 14");
721 * pango_layout_set_font_description (layout, desc);
722 * pango_font_description_free (desc);
724 * pango_layout_set_text (layout, "some text", -1);
725 * pango_layout_set_width (layout, width * PANGO_SCALE);
726 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
728 * pango_layout_get_size (layout, NULL, &layout_height);
729 * text_height = (gdouble)layout_height / PANGO_SCALE;
731 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
732 * pango_cairo_show_layout (cr, layout);
734 * g_object_unref (layout);
738 * Use gtk_print_operation_set_use_full_page() and
739 * gtk_print_operation_set_unit() before starting the print operation
740 * to set up the transformation of the cairo context according to your
746 g_signal_new (I_("draw-page"),
747 G_TYPE_FROM_CLASS (gobject_class),
749 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
751 _gtk_marshal_VOID__OBJECT_INT,
753 GTK_TYPE_PRINT_CONTEXT,
757 * GtkPrintOperation::end-print:
758 * @operation: the #GtkPrintOperation on which the signal was emitted
759 * @context: the #GtkPrintContext for the current operation
761 * Emitted after all pages have been rendered.
762 * A handler for this signal can clean up any resources that have
763 * been allocated in the #GtkPrintOperation::begin-print handler.
768 g_signal_new (I_("end-print"),
769 G_TYPE_FROM_CLASS (gobject_class),
771 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
773 g_cclosure_marshal_VOID__OBJECT,
774 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
777 * GtkPrintOperation::status-changed:
778 * @operation: the #GtkPrintOperation on which the signal was emitted
780 * Emitted at between the various phases of the print operation.
781 * See #GtkPrintStatus for the phases that are being discriminated.
782 * Use gtk_print_operation_get_status() to find out the current
787 signals[STATUS_CHANGED] =
788 g_signal_new (I_("status-changed"),
789 G_TYPE_FROM_CLASS (class),
791 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
793 g_cclosure_marshal_VOID__VOID,
798 * GtkPrintOperation::create-custom-widget:
799 * @operation: the #GtkPrintOperation on which the signal was emitted
801 * Emitted when displaying the print dialog. If you return a
802 * widget in a handler for this signal it will be added to a custom
803 * tab in the print dialog. You typically return a container widget
804 * with multiple widgets in it.
806 * The print dialog owns the returned widget, and its lifetime is not
807 * controlled by the application. However, the widget is guaranteed
808 * to stay around until the #GtkPrintOperation::custom-widget-apply
809 * signal is emitted on the operation. Then you can read out any
810 * information you need from the widgets.
812 * Returns: A custom widget that gets embedded in the print dialog,
817 signals[CREATE_CUSTOM_WIDGET] =
818 g_signal_new (I_("create-custom-widget"),
819 G_TYPE_FROM_CLASS (class),
821 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
822 custom_widget_accumulator, NULL,
823 _gtk_marshal_OBJECT__VOID,
827 * GtkPrintOperation::update-custom-widget:
828 * @operation: the #GtkPrintOperation on which the signal was emitted
829 * @widget: the custom widget added in create-custom-widget
830 * @setup: actual page setup
831 * @settings: actual print settings
833 * Emmited after change of selected printer. The actual page setup and
834 * print settings are passed to the custom widget, which can actualize
835 * itself according to this change.
839 signals[UPDATE_CUSTOM_WIDGET] =
840 g_signal_new (I_("update-custom-widget"),
841 G_TYPE_FROM_CLASS (class),
843 G_STRUCT_OFFSET (GtkPrintOperationClass, update_custom_widget),
845 _gtk_marshal_VOID__OBJECT_OBJECT_OBJECT,
846 G_TYPE_NONE, 3, GTK_TYPE_WIDGET, GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);
849 * GtkPrintOperation::custom-widget-apply:
850 * @operation: the #GtkPrintOperation on which the signal was emitted
851 * @widget: the custom widget added in create-custom-widget
853 * Emitted right before #GtkPrintOperation::begin-print if you added
854 * a custom widget in the #GtkPrintOperation::create-custom-widget handler.
855 * When you get this signal you should read the information from the
856 * custom widgets, as the widgets are not guaraneed to be around at a
861 signals[CUSTOM_WIDGET_APPLY] =
862 g_signal_new (I_("custom-widget-apply"),
863 G_TYPE_FROM_CLASS (class),
865 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
867 g_cclosure_marshal_VOID__OBJECT,
868 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
871 * GtkPrintOperation::preview:
872 * @operation: the #GtkPrintOperation on which the signal was emitted
873 * @preview: the #GtkPrintPreviewOperation for the current operation
874 * @context: the #GtkPrintContext that will be used
875 * @parent: the #GtkWindow to use as window parent, or %NULL
877 * Gets emitted when a preview is requested from the native dialog.
879 * The default handler for this signal uses an external viewer
880 * application to preview.
882 * To implement a custom print preview, an application must return
883 * %TRUE from its handler for this signal. In order to use the
884 * provided @context for the preview implementation, it must be
885 * given a suitable cairo context with gtk_print_context_set_cairo_context().
887 * The custom preview implementation can use
888 * gtk_print_operation_preview_is_selected() and
889 * gtk_print_operation_preview_render_page() to find pages which
890 * are selected for print and render them. The preview must be
891 * finished by calling gtk_print_operation_preview_end_preview()
892 * (typically in response to the user clicking a close button).
894 * Returns: %TRUE if the listener wants to take over control of the preview
899 g_signal_new (I_("preview"),
900 G_TYPE_FROM_CLASS (gobject_class),
902 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
903 _gtk_boolean_handled_accumulator, NULL,
904 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
906 GTK_TYPE_PRINT_OPERATION_PREVIEW,
907 GTK_TYPE_PRINT_CONTEXT,
912 * GtkPrintOperation:default-page-setup:
914 * The #GtkPageSetup used by default.
916 * This page setup will be used by gtk_print_operation_run(),
917 * but it can be overridden on a per-page basis by connecting
918 * to the #GtkPrintOperation::request-page-setup signal.
922 g_object_class_install_property (gobject_class,
923 PROP_DEFAULT_PAGE_SETUP,
924 g_param_spec_object ("default-page-setup",
925 P_("Default Page Setup"),
926 P_("The GtkPageSetup used by default"),
928 GTK_PARAM_READWRITE));
931 * GtkPrintOperation:print-settings:
933 * The #GtkPrintSettings used for initializing the dialog.
935 * Setting this property is typically used to re-establish
936 * print settings from a previous print operation, see
937 * gtk_print_operation_run().
941 g_object_class_install_property (gobject_class,
943 g_param_spec_object ("print-settings",
944 P_("Print Settings"),
945 P_("The GtkPrintSettings used for initializing the dialog"),
946 GTK_TYPE_PRINT_SETTINGS,
947 GTK_PARAM_READWRITE));
950 * GtkPrintOperation:job-name:
952 * A string used to identify the job (e.g. in monitoring
953 * applications like eggcups).
955 * If you don't set a job name, GTK+ picks a default one
956 * by numbering successive print jobs.
960 g_object_class_install_property (gobject_class,
962 g_param_spec_string ("job-name",
964 P_("A string used for identifying the print job."),
966 GTK_PARAM_READWRITE));
969 * GtkPrintOperation:n-pages:
971 * The number of pages in the document.
973 * This <emphasis>must</emphasis> be set to a positive number
974 * before the rendering starts. It may be set in a
975 * #GtkPrintOperation::begin-print signal hander.
977 * Note that the page numbers passed to the
978 * #GtkPrintOperation::request-page-setup and
979 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
980 * the user chooses to print all pages, the last ::draw-page signal
981 * will be for page @n_pages - 1.
985 g_object_class_install_property (gobject_class,
987 g_param_spec_int ("n-pages",
988 P_("Number of Pages"),
989 P_("The number of pages in the document."),
993 GTK_PARAM_READWRITE));
996 * GtkPrintOperation:current-page:
998 * The current page in the document.
1000 * If this is set before gtk_print_operation_run(),
1001 * the user will be able to select to print only the current page.
1003 * Note that this only makes sense for pre-paginated documents.
1007 g_object_class_install_property (gobject_class,
1009 g_param_spec_int ("current-page",
1011 P_("The current page in the document"),
1015 GTK_PARAM_READWRITE));
1018 * GtkPrintOperation:use-full-page:
1020 * If %TRUE, the transformation for the cairo context obtained
1021 * from #GtkPrintContext puts the origin at the top left corner
1022 * of the page (which may not be the top left corner of the sheet,
1023 * depending on page orientation and the number of pages per sheet).
1024 * Otherwise, the origin is at the top left corner of the imageable
1025 * area (i.e. inside the margins).
1029 g_object_class_install_property (gobject_class,
1031 g_param_spec_boolean ("use-full-page",
1032 P_("Use full page"),
1033 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
1035 GTK_PARAM_READWRITE));
1039 * GtkPrintOperation:track-print-status:
1041 * If %TRUE, the print operation will try to continue report on
1042 * the status of the print job in the printer queues and printer.
1043 * This can allow your application to show things like "out of paper"
1044 * issues, and when the print job actually reaches the printer.
1045 * However, this is often implemented using polling, and should
1046 * not be enabled unless needed.
1050 g_object_class_install_property (gobject_class,
1051 PROP_TRACK_PRINT_STATUS,
1052 g_param_spec_boolean ("track-print-status",
1053 P_("Track Print Status"),
1054 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."),
1056 GTK_PARAM_READWRITE));
1060 * GtkPrintOperation:unit:
1062 * The transformation for the cairo context obtained from
1063 * #GtkPrintContext is set up in such a way that distances
1064 * are measured in units of @unit.
1068 g_object_class_install_property (gobject_class,
1070 g_param_spec_enum ("unit",
1072 P_("The unit in which distances can be measured in the context"),
1075 GTK_PARAM_READWRITE));
1079 * GtkPrintOperation:show-progress:
1081 * Determines whether to show a progress dialog during the
1086 g_object_class_install_property (gobject_class,
1088 g_param_spec_boolean ("show-progress",
1090 P_("TRUE if a progress dialog is shown while printing."),
1092 GTK_PARAM_READWRITE));
1095 * GtkPrintOperation:allow-async:
1097 * Determines whether the print operation may run asynchronously or not.
1099 * Some systems don't support asynchronous printing, but those that do
1100 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1101 * emit the #GtkPrintOperation::done signal when the operation is actually
1104 * The Windows port does not support asynchronous operation at all (this
1105 * is unlikely to change). On other platforms, all actions except for
1106 * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
1110 g_object_class_install_property (gobject_class,
1112 g_param_spec_boolean ("allow-async",
1114 P_("TRUE if print process may run asynchronous."),
1116 GTK_PARAM_READWRITE));
1119 * GtkPrintOperation:export-filename:
1121 * The name of a file to generate instead of showing the print dialog.
1122 * Currently, PDF is the only supported format.
1124 * The intended use of this property is for implementing
1125 * "Export to PDF" actions.
1127 * "Print to PDF" support is independent of this and is done
1128 * by letting the user pick the "Print to PDF" item from the
1129 * list of printers in the print dialog.
1133 g_object_class_install_property (gobject_class,
1134 PROP_EXPORT_FILENAME,
1135 g_param_spec_string ("export-filename",
1136 P_("Export filename"),
1137 P_("Export filename"),
1139 GTK_PARAM_READWRITE));
1142 * GtkPrintOperation:status:
1144 * The status of the print operation.
1148 g_object_class_install_property (gobject_class,
1150 g_param_spec_enum ("status",
1152 P_("The status of the print operation"),
1153 GTK_TYPE_PRINT_STATUS,
1154 GTK_PRINT_STATUS_INITIAL,
1155 GTK_PARAM_READABLE));
1158 * GtkPrintOperation:status-string:
1160 * A string representation of the status of the print operation.
1161 * The string is translated and suitable for displaying the print
1162 * status e.g. in a #GtkStatusbar.
1164 * See the #GtkPrintOperation:status property for a status value that
1165 * is suitable for programmatic use.
1169 g_object_class_install_property (gobject_class,
1171 g_param_spec_string ("status-string",
1172 P_("Status String"),
1173 P_("A human-readable description of the status"),
1175 GTK_PARAM_READABLE));
1179 * GtkPrintOperation:custom-tab-label:
1181 * Used as the label of the tab containing custom widgets.
1182 * Note that this property may be ignored on some platforms.
1184 * If this is %NULL, GTK+ uses a default label.
1188 g_object_class_install_property (gobject_class,
1189 PROP_CUSTOM_TAB_LABEL,
1190 g_param_spec_string ("custom-tab-label",
1191 P_("Custom tab label"),
1192 P_("Label for the tab containing custom widgets."),
1194 GTK_PARAM_READWRITE));
1199 * gtk_print_operation_new:
1201 * Creates a new #GtkPrintOperation.
1203 * Returns: a new #GtkPrintOperation
1208 gtk_print_operation_new (void)
1210 GtkPrintOperation *print_operation;
1212 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1214 return print_operation;
1218 * gtk_print_operation_set_default_page_setup:
1219 * @op: a #GtkPrintOperation
1220 * @default_page_setup: a #GtkPageSetup, or %NULL
1222 * Makes @default_page_setup the default page setup for @op.
1224 * This page setup will be used by gtk_print_operation_run(),
1225 * but it can be overridden on a per-page basis by connecting
1226 * to the #GtkPrintOperation::request-page-setup signal.
1231 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1232 GtkPageSetup *default_page_setup)
1234 GtkPrintOperationPrivate *priv;
1236 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1237 g_return_if_fail (default_page_setup == NULL ||
1238 GTK_IS_PAGE_SETUP (default_page_setup));
1242 if (default_page_setup != priv->default_page_setup)
1244 if (default_page_setup)
1245 g_object_ref (default_page_setup);
1247 if (priv->default_page_setup)
1248 g_object_unref (priv->default_page_setup);
1250 priv->default_page_setup = default_page_setup;
1252 g_object_notify (G_OBJECT (op), "default-page-setup");
1257 * gtk_print_operation_get_default_page_setup:
1258 * @op: a #GtkPrintOperation
1260 * Returns the default page setup, see
1261 * gtk_print_operation_set_default_page_setup().
1263 * Returns: the default page setup
1268 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1270 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1272 return op->priv->default_page_setup;
1277 * gtk_print_operation_set_print_settings:
1278 * @op: a #GtkPrintOperation
1279 * @print_settings: #GtkPrintSettings, or %NULL
1281 * Sets the print settings for @op. This is typically used to
1282 * re-establish print settings from a previous print operation,
1283 * see gtk_print_operation_run().
1288 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1289 GtkPrintSettings *print_settings)
1291 GtkPrintOperationPrivate *priv;
1293 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1294 g_return_if_fail (print_settings == NULL ||
1295 GTK_IS_PRINT_SETTINGS (print_settings));
1299 if (print_settings != priv->print_settings)
1302 g_object_ref (print_settings);
1304 if (priv->print_settings)
1305 g_object_unref (priv->print_settings);
1307 priv->print_settings = print_settings;
1309 g_object_notify (G_OBJECT (op), "print-settings");
1314 * gtk_print_operation_get_print_settings:
1315 * @op: a #GtkPrintOperation
1317 * Returns the current print settings.
1319 * Note that the return value is %NULL until either
1320 * gtk_print_operation_set_print_settings() or
1321 * gtk_print_operation_run() have been called.
1323 * Return value: the current print settings of @op.
1328 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1330 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1332 return op->priv->print_settings;
1336 * gtk_print_operation_set_job_name:
1337 * @op: a #GtkPrintOperation
1338 * @job_name: a string that identifies the print job
1340 * Sets the name of the print job. The name is used to identify
1341 * the job (e.g. in monitoring applications like eggcups).
1343 * If you don't set a job name, GTK+ picks a default one by
1344 * numbering successive print jobs.
1349 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1350 const gchar *job_name)
1352 GtkPrintOperationPrivate *priv;
1354 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1355 g_return_if_fail (job_name != NULL);
1359 g_free (priv->job_name);
1360 priv->job_name = g_strdup (job_name);
1362 g_object_notify (G_OBJECT (op), "job-name");
1366 * gtk_print_operation_set_n_pages:
1367 * @op: a #GtkPrintOperation
1368 * @n_pages: the number of pages
1370 * Sets the number of pages in the document.
1372 * This <emphasis>must</emphasis> be set to a positive number
1373 * before the rendering starts. It may be set in a
1374 * #GtkPrintOperation::begin-print signal hander.
1376 * Note that the page numbers passed to the
1377 * #GtkPrintOperation::request-page-setup
1378 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1379 * the user chooses to print all pages, the last ::draw-page signal
1380 * will be for page @n_pages - 1.
1385 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1388 GtkPrintOperationPrivate *priv;
1390 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1391 g_return_if_fail (n_pages > 0);
1394 g_return_if_fail (priv->current_page == -1 ||
1395 priv->current_page < n_pages);
1397 if (priv->nr_of_pages != n_pages)
1399 priv->nr_of_pages = n_pages;
1401 g_object_notify (G_OBJECT (op), "n-pages");
1406 * gtk_print_operation_set_current_page:
1407 * @op: a #GtkPrintOperation
1408 * @current_page: the current page, 0-based
1410 * Sets the current page.
1412 * If this is called before gtk_print_operation_run(),
1413 * the user will be able to select to print only the current page.
1415 * Note that this only makes sense for pre-paginated documents.
1420 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1423 GtkPrintOperationPrivate *priv;
1425 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1426 g_return_if_fail (current_page >= 0);
1429 g_return_if_fail (priv->nr_of_pages == -1 ||
1430 current_page < priv->nr_of_pages);
1432 if (priv->current_page != current_page)
1434 priv->current_page = current_page;
1436 g_object_notify (G_OBJECT (op), "current-page");
1441 * gtk_print_operation_set_use_full_page:
1442 * @op: a #GtkPrintOperation
1443 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1445 * If @full_page is %TRUE, the transformation for the cairo context
1446 * obtained from #GtkPrintContext puts the origin at the top left
1447 * corner of the page (which may not be the top left corner of the
1448 * sheet, depending on page orientation and the number of pages per
1449 * sheet). Otherwise, the origin is at the top left corner of the
1450 * imageable area (i.e. inside the margins).
1455 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1458 GtkPrintOperationPrivate *priv;
1460 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1462 full_page = full_page != FALSE;
1466 if (priv->use_full_page != full_page)
1468 priv->use_full_page = full_page;
1470 g_object_notify (G_OBJECT (op), "use-full-page");
1475 * gtk_print_operation_set_unit:
1476 * @op: a #GtkPrintOperation
1477 * @unit: the unit to use
1479 * Sets up the transformation for the cairo context obtained from
1480 * #GtkPrintContext in such a way that distances are measured in
1486 gtk_print_operation_set_unit (GtkPrintOperation *op,
1489 GtkPrintOperationPrivate *priv;
1491 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1495 if (priv->unit != unit)
1499 g_object_notify (G_OBJECT (op), "unit");
1504 * gtk_print_operation_set_track_print_status:
1505 * @op: a #GtkPrintOperation
1506 * @track_status: %TRUE to track status after printing
1508 * If track_status is %TRUE, the print operation will try to continue report
1509 * on the status of the print job in the printer queues and printer. This
1510 * can allow your application to show things like "out of paper" issues,
1511 * and when the print job actually reaches the printer.
1513 * This function is often implemented using some form of polling, so it should
1514 * not be enabled unless needed.
1519 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1520 gboolean track_status)
1522 GtkPrintOperationPrivate *priv;
1524 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1528 if (priv->track_print_status != track_status)
1530 priv->track_print_status = track_status;
1532 g_object_notify (G_OBJECT (op), "track-print-status");
1537 _gtk_print_operation_set_status (GtkPrintOperation *op,
1538 GtkPrintStatus status,
1539 const gchar *string)
1541 GtkPrintOperationPrivate *priv = op->priv;
1542 static const gchar *status_strs[] = {
1543 NC_("print operation status", "Initial state"),
1544 NC_("print operation status", "Preparing to print"),
1545 NC_("print operation status", "Generating data"),
1546 NC_("print operation status", "Sending data"),
1547 NC_("print operation status", "Waiting"),
1548 NC_("print operation status", "Blocking on issue"),
1549 NC_("print operation status", "Printing"),
1550 NC_("print operation status", "Finished"),
1551 NC_("print operation status", "Finished with error")
1554 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1555 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1558 string = g_dpgettext2 (GETTEXT_PACKAGE, "print operation status", status_strs[status]);
1560 if (priv->status == status &&
1561 strcmp (string, priv->status_string) == 0)
1564 g_free (priv->status_string);
1565 priv->status_string = g_strdup (string);
1566 priv->status = status;
1568 g_object_notify (G_OBJECT (op), "status");
1569 g_object_notify (G_OBJECT (op), "status-string");
1571 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1576 * gtk_print_operation_get_status:
1577 * @op: a #GtkPrintOperation
1579 * Returns the status of the print operation.
1580 * Also see gtk_print_operation_get_status_string().
1582 * Return value: the status of the print operation
1587 gtk_print_operation_get_status (GtkPrintOperation *op)
1589 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1590 GTK_PRINT_STATUS_FINISHED_ABORTED);
1592 return op->priv->status;
1596 * gtk_print_operation_get_status_string:
1597 * @op: a #GtkPrintOperation
1599 * Returns a string representation of the status of the
1600 * print operation. The string is translated and suitable
1601 * for displaying the print status e.g. in a #GtkStatusbar.
1603 * Use gtk_print_operation_get_status() to obtain a status
1604 * value that is suitable for programmatic use.
1606 * Return value: a string representation of the status
1607 * of the print operation
1611 G_CONST_RETURN gchar *
1612 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1614 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1616 return op->priv->status_string;
1620 * gtk_print_operation_is_finished:
1621 * @op: a #GtkPrintOperation
1623 * A convenience function to find out if the print operation
1624 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1625 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1627 * Note: when you enable print status tracking the print operation
1628 * can be in a non-finished state even after done has been called, as
1629 * the operation status then tracks the print job status on the printer.
1631 * Return value: %TRUE, if the print operation is finished.
1636 gtk_print_operation_is_finished (GtkPrintOperation *op)
1638 GtkPrintOperationPrivate *priv;
1640 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1644 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1645 priv->status == GTK_PRINT_STATUS_FINISHED;
1649 * gtk_print_operation_set_show_progress:
1650 * @op: a #GtkPrintOperation
1651 * @show_progress: %TRUE to show a progress dialog
1653 * If @show_progress is %TRUE, the print operation will show a
1654 * progress dialog during the print operation.
1659 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1660 gboolean show_progress)
1662 GtkPrintOperationPrivate *priv;
1664 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1668 show_progress = show_progress != FALSE;
1670 if (priv->show_progress != show_progress)
1672 priv->show_progress = show_progress;
1674 g_object_notify (G_OBJECT (op), "show-progress");
1679 * gtk_print_operation_set_allow_async:
1680 * @op: a #GtkPrintOperation
1681 * @allow_async: %TRUE to allow asynchronous operation
1683 * Sets whether the gtk_print_operation_run() may return
1684 * before the print operation is completed. Note that
1685 * some platforms may not allow asynchronous operation.
1690 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1691 gboolean allow_async)
1693 GtkPrintOperationPrivate *priv;
1695 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1699 allow_async = allow_async != FALSE;
1701 if (priv->allow_async != allow_async)
1703 priv->allow_async = allow_async;
1705 g_object_notify (G_OBJECT (op), "allow-async");
1711 * gtk_print_operation_set_custom_tab_label:
1712 * @op: a #GtkPrintOperation
1713 * @label: the label to use, or %NULL to use the default label
1715 * Sets the label for the tab holding custom widgets.
1720 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1723 GtkPrintOperationPrivate *priv;
1725 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1729 g_free (priv->custom_tab_label);
1730 priv->custom_tab_label = g_strdup (label);
1732 g_object_notify (G_OBJECT (op), "custom-tab-label");
1737 * gtk_print_operation_set_export_filename:
1738 * @op: a #GtkPrintOperation
1739 * @filename: the filename for the exported file
1741 * Sets up the #GtkPrintOperation to generate a file instead
1742 * of showing the print dialog. The indended use of this function
1743 * is for implementing "Export to PDF" actions. Currently, PDF
1744 * is the only supported format.
1746 * "Print to PDF" support is independent of this and is done
1747 * by letting the user pick the "Print to PDF" item from the list
1748 * of printers in the print dialog.
1753 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1754 const gchar *filename)
1756 GtkPrintOperationPrivate *priv;
1758 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1762 g_free (priv->export_filename);
1763 priv->export_filename = g_strdup (filename);
1765 g_object_notify (G_OBJECT (op), "export-filename");
1768 /* Creates the initial page setup used for printing unless the
1769 * app overrides this on a per-page basis using request_page_setup.
1771 * Data is taken from, in order, if existing:
1773 * PrintSettings returned from the print dialog
1774 * (initial dialog values are set from default_page_setup
1775 * if unset in app specified print_settings)
1776 * default_page_setup
1777 * per-locale default setup
1779 static GtkPageSetup *
1780 create_page_setup (GtkPrintOperation *op)
1782 GtkPrintOperationPrivate *priv = op->priv;
1783 GtkPageSetup *page_setup;
1784 GtkPrintSettings *settings;
1786 if (priv->default_page_setup)
1787 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1789 page_setup = gtk_page_setup_new ();
1791 settings = priv->print_settings;
1794 GtkPaperSize *paper_size;
1796 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1797 gtk_page_setup_set_orientation (page_setup,
1798 gtk_print_settings_get_orientation (settings));
1801 paper_size = gtk_print_settings_get_paper_size (settings);
1804 gtk_page_setup_set_paper_size (page_setup, paper_size);
1805 gtk_paper_size_free (paper_size);
1808 /* TODO: Margins? */
1815 pdf_start_page (GtkPrintOperation *op,
1816 GtkPrintContext *print_context,
1817 GtkPageSetup *page_setup)
1819 GtkPaperSize *paper_size;
1820 cairo_surface_t *surface = op->priv->platform_data;
1823 paper_size = gtk_page_setup_get_paper_size (page_setup);
1825 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1826 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1828 cairo_pdf_surface_set_size (surface, w, h);
1832 pdf_end_page (GtkPrintOperation *op,
1833 GtkPrintContext *print_context)
1837 cr = gtk_print_context_get_cairo_context (print_context);
1839 if ((op->priv->manual_number_up < 2) ||
1840 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
1841 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
1842 cairo_show_page (cr);
1846 pdf_end_run (GtkPrintOperation *op,
1850 GtkPrintOperationPrivate *priv = op->priv;
1851 cairo_surface_t *surface = priv->platform_data;
1853 cairo_surface_finish (surface);
1854 cairo_surface_destroy (surface);
1856 priv->platform_data = NULL;
1857 priv->free_platform_data = NULL;
1860 static GtkPrintOperationResult
1861 run_pdf (GtkPrintOperation *op,
1865 GtkPrintOperationPrivate *priv = op->priv;
1866 GtkPageSetup *page_setup;
1867 cairo_surface_t *surface;
1869 gdouble width, height;
1871 priv->print_context = _gtk_print_context_new (op);
1873 page_setup = create_page_setup (op);
1874 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
1876 /* This will be overwritten later by the non-default size, but
1877 we need to pass some size: */
1878 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1879 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1880 g_object_unref (page_setup);
1882 surface = cairo_pdf_surface_create (priv->export_filename,
1884 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
1886 g_set_error_literal (&priv->error,
1888 GTK_PRINT_ERROR_GENERAL,
1889 cairo_status_to_string (cairo_surface_status (surface)));
1891 return GTK_PRINT_OPERATION_RESULT_ERROR;
1894 /* this would crash on a nil surface */
1895 cairo_surface_set_fallback_resolution (surface, 300, 300);
1897 priv->platform_data = surface;
1898 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
1900 cr = cairo_create (surface);
1901 gtk_print_context_set_cairo_context (op->priv->print_context,
1906 priv->print_pages = GTK_PRINT_PAGES_ALL;
1907 priv->page_ranges = NULL;
1908 priv->num_page_ranges = 0;
1910 priv->manual_num_copies = 1;
1911 priv->manual_collation = FALSE;
1912 priv->manual_reverse = FALSE;
1913 priv->manual_page_set = GTK_PAGE_SET_ALL;
1914 priv->manual_scale = 1.0;
1915 priv->manual_orientation = TRUE;
1916 priv->manual_number_up = 1;
1917 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
1921 priv->start_page = pdf_start_page;
1922 priv->end_page = pdf_end_page;
1923 priv->end_run = pdf_end_run;
1925 return GTK_PRINT_OPERATION_RESULT_APPLY;
1930 GtkPrintOperation *op;
1931 gint uncollated_copies;
1932 gint collated_copies;
1933 gint uncollated, collated, total;
1935 gint range, num_ranges;
1936 GtkPageRange *ranges;
1937 GtkPageRange one_range;
1941 gint first_position, last_position;
1946 GtkWidget *progress;
1948 gboolean initialized;
1949 gboolean is_preview;
1954 clamp_page_ranges (PrintPagesData *data)
1956 GtkPrintOperationPrivate *priv;
1957 gint num_of_correct_ranges;
1960 priv = data->op->priv;
1962 num_of_correct_ranges = 0;
1964 for (i = 0; i < data->num_ranges; i++)
1965 if ((data->ranges[i].start >= 0) &&
1966 (data->ranges[i].start < priv->nr_of_pages) &&
1967 (data->ranges[i].end >= 0) &&
1968 (data->ranges[i].end < priv->nr_of_pages))
1970 data->ranges[num_of_correct_ranges] = data->ranges[i];
1971 num_of_correct_ranges++;
1973 else if ((data->ranges[i].start >= 0) &&
1974 (data->ranges[i].start < priv->nr_of_pages) &&
1975 (data->ranges[i].end >= priv->nr_of_pages))
1977 data->ranges[i].end = priv->nr_of_pages - 1;
1978 data->ranges[num_of_correct_ranges] = data->ranges[i];
1979 num_of_correct_ranges++;
1981 else if ((data->ranges[i].end >= 0) &&
1982 (data->ranges[i].end < priv->nr_of_pages) &&
1983 (data->ranges[i].start < 0))
1985 data->ranges[i].start = 0;
1986 data->ranges[num_of_correct_ranges] = data->ranges[i];
1987 num_of_correct_ranges++;
1990 data->num_ranges = num_of_correct_ranges;
1994 increment_page_sequence (PrintPagesData *data)
1996 GtkPrintOperationPrivate *priv = data->op->priv;
1999 /* check whether we reached last position */
2000 if (priv->page_position == data->last_position &&
2001 !(data->collated_copies > 1 && data->collated < (data->collated_copies - 1)))
2003 if (data->uncollated_copies > 1 && data->uncollated < (data->uncollated_copies - 1))
2005 priv->page_position = data->first_position;
2006 data->sheet = data->first_sheet;
2017 if (priv->manual_reverse)
2022 /* changing sheet */
2023 if ((priv->page_position + 1) % priv->manual_number_up == 0 ||
2024 priv->page_position == data->last_position ||
2025 priv->page_position == priv->nr_of_pages_to_print - 1)
2027 /* check whether to print the same sheet again */
2028 if (data->collated_copies > 1)
2030 if (data->collated < (data->collated_copies - 1))
2034 priv->page_position = data->sheet * priv->manual_number_up;
2036 if (priv->page_position < 0 ||
2037 priv->page_position >= priv->nr_of_pages_to_print ||
2039 data->sheet >= data->num_of_sheets)
2045 data->page = data->pages[priv->page_position];
2053 if (priv->manual_page_set == GTK_PAGE_SET_ODD ||
2054 priv->manual_page_set == GTK_PAGE_SET_EVEN)
2055 data->sheet += 2 * inc;
2059 priv->page_position = data->sheet * priv->manual_number_up;
2062 priv->page_position += 1;
2066 if (priv->page_position < 0 ||
2067 priv->page_position >= priv->nr_of_pages_to_print ||
2069 data->sheet >= data->num_of_sheets)
2075 data->page = data->pages[priv->page_position];
2081 print_pages_idle_done (gpointer user_data)
2083 PrintPagesData *data;
2084 GtkPrintOperationPrivate *priv;
2086 data = (PrintPagesData*)user_data;
2087 priv = data->op->priv;
2089 priv->print_pages_idle_id = 0;
2091 if (priv->show_progress_timeout_id > 0)
2093 g_source_remove (priv->show_progress_timeout_id);
2094 priv->show_progress_timeout_id = 0;
2098 gtk_widget_destroy (data->progress);
2100 if (priv->rloop && !data->is_preview)
2101 g_main_loop_quit (priv->rloop);
2103 if (!data->is_preview)
2104 g_signal_emit (data->op, signals[DONE], 0,
2106 GTK_PRINT_OPERATION_RESULT_CANCEL :
2107 GTK_PRINT_OPERATION_RESULT_APPLY);
2109 g_object_unref (data->op);
2110 g_free (data->pages);
2115 update_progress (PrintPagesData *data)
2117 GtkPrintOperationPrivate *priv;
2120 priv = data->op->priv;
2124 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2126 if (priv->nr_of_pages_to_print > 0)
2127 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages_to_print);
2129 text = g_strdup (_("Preparing"));
2131 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2132 text = g_strdup_printf (_("Printing %d"), data->total - 1);
2136 g_object_set (data->progress, "text", text, NULL);
2143 * gtk_print_operation_set_defer_drawing:
2144 * @op: a #GtkPrintOperation
2146 * Sets up the #GtkPrintOperation to wait for calling of
2147 * gtk_print_operation_draw_page_finish() from application. It can
2148 * be used for drawing page in another thread.
2150 * This function must be called in the callback of "draw-page" signal.
2155 gtk_print_operation_set_defer_drawing (GtkPrintOperation *op)
2157 GtkPrintOperationPrivate *priv = op->priv;
2159 g_return_if_fail (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING);
2161 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DEFERRED_DRAWING;
2165 * gtk_print_operation_draw_page_finish:
2166 * @op: a #GtkPrintOperation
2168 * Signalize that drawing of particular page is complete.
2170 * It is called after completion of page drawing (e.g. drawing in another
2172 * If gtk_print_operation_set_defer_drawing() was called before, then this function
2173 * has to be called by application. In another case it is called by the library
2179 gtk_print_operation_draw_page_finish (GtkPrintOperation *op)
2181 GtkPrintOperationPrivate *priv = op->priv;
2182 GtkPageSetup *page_setup;
2183 GtkPrintContext *print_context;
2186 print_context = priv->print_context;
2187 page_setup = gtk_print_context_get_page_setup (print_context);
2189 cr = gtk_print_context_get_cairo_context (print_context);
2191 priv->end_page (op, print_context);
2195 g_object_unref (page_setup);
2197 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
2201 common_render_page (GtkPrintOperation *op,
2204 GtkPrintOperationPrivate *priv = op->priv;
2205 GtkPageSetup *page_setup;
2206 GtkPrintContext *print_context;
2209 print_context = priv->print_context;
2211 page_setup = create_page_setup (op);
2213 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2214 print_context, page_nr, page_setup);
2216 _gtk_print_context_set_page_setup (print_context, page_setup);
2218 priv->start_page (op, print_context, page_setup);
2220 cr = gtk_print_context_get_cairo_context (print_context);
2223 if (priv->manual_scale != 1.0 && priv->manual_number_up <= 1)
2226 priv->manual_scale);
2228 if (priv->manual_orientation)
2229 _gtk_print_context_rotate_according_to_orientation (print_context);
2231 if (priv->manual_number_up > 1)
2233 GtkPageOrientation orientation;
2234 GtkPageSetup *page_setup;
2235 gdouble paper_width, paper_height;
2236 gdouble page_width, page_height;
2237 gdouble context_width, context_height;
2238 gdouble bottom_margin, top_margin, left_margin, right_margin;
2239 gdouble x_step, y_step;
2240 gdouble x_scale, y_scale, scale;
2241 gdouble horizontal_offset = 0.0, vertical_offset = 0.0;
2242 gint columns, rows, x, y, tmp_length;
2244 page_setup = gtk_print_context_get_page_setup (print_context);
2245 orientation = gtk_page_setup_get_orientation (page_setup);
2247 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
2248 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
2249 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
2250 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
2252 paper_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2253 paper_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2255 context_width = gtk_print_context_get_width (print_context);
2256 context_height = gtk_print_context_get_height (print_context);
2258 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2259 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2261 page_width = paper_width - (left_margin + right_margin);
2262 page_height = paper_height - (top_margin + bottom_margin);
2266 page_width = paper_width - (top_margin + bottom_margin);
2267 page_height = paper_height - (left_margin + right_margin);
2270 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2271 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2272 cairo_translate (cr, left_margin, top_margin);
2274 cairo_translate (cr, top_margin, left_margin);
2276 switch (priv->manual_number_up)
2304 if (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE ||
2305 orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE)
2307 tmp_length = columns;
2312 switch (priv->manual_number_up_layout)
2314 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
2315 x = priv->page_position % columns;
2316 y = (priv->page_position / columns) % rows;
2318 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
2319 x = priv->page_position % columns;
2320 y = rows - 1 - (priv->page_position / columns) % rows;
2322 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
2323 x = columns - 1 - priv->page_position % columns;
2324 y = (priv->page_position / columns) % rows;
2326 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
2327 x = columns - 1 - priv->page_position % columns;
2328 y = rows - 1 - (priv->page_position / columns) % rows;
2330 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
2331 x = (priv->page_position / rows) % columns;
2332 y = priv->page_position % rows;
2334 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
2335 x = columns - 1 - (priv->page_position / rows) % columns;
2336 y = priv->page_position % rows;
2338 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
2339 x = (priv->page_position / rows) % columns;
2340 y = rows - 1 - priv->page_position % rows;
2342 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
2343 x = columns - 1 - (priv->page_position / rows) % columns;
2344 y = rows - 1 - priv->page_position % rows;
2348 if (priv->manual_number_up == 4 || priv->manual_number_up == 9 || priv->manual_number_up == 16)
2350 x_scale = page_width / (columns * paper_width);
2351 y_scale = page_height / (rows * paper_height);
2353 scale = x_scale < y_scale ? x_scale : y_scale;
2355 x_step = paper_width * (x_scale / scale);
2356 y_step = paper_height * (y_scale / scale);
2358 if ((left_margin + right_margin) > 0)
2360 horizontal_offset = left_margin * (x_step - context_width) / (left_margin + right_margin);
2361 vertical_offset = top_margin * (y_step - context_height) / (top_margin + bottom_margin);
2365 horizontal_offset = (x_step - context_width) / 2.0;
2366 vertical_offset = (y_step - context_height) / 2.0;
2369 cairo_scale (cr, scale, scale);
2371 cairo_translate (cr,
2372 x * x_step + horizontal_offset,
2373 y * y_step + vertical_offset);
2375 if (priv->manual_scale != 1.0)
2376 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2379 if (priv->manual_number_up == 2 || priv->manual_number_up == 6)
2381 x_scale = page_height / (columns * paper_width);
2382 y_scale = page_width / (rows * paper_height);
2384 scale = x_scale < y_scale ? x_scale : y_scale;
2386 horizontal_offset = (paper_width * (x_scale / scale) - paper_width) / 2.0 * columns;
2387 vertical_offset = (paper_height * (y_scale / scale) - paper_height) / 2.0 * rows;
2389 if (!priv->use_full_page)
2391 horizontal_offset -= right_margin;
2392 vertical_offset += top_margin;
2395 cairo_scale (cr, scale, scale);
2397 cairo_translate (cr,
2398 y * paper_height + vertical_offset,
2399 (columns - x) * paper_width + horizontal_offset);
2401 if (priv->manual_scale != 1.0)
2402 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2404 cairo_rotate (cr, - M_PI / 2);
2408 if (!priv->use_full_page)
2409 _gtk_print_context_translate_into_margin (print_context);
2411 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DRAWING;
2413 g_signal_emit (op, signals[DRAW_PAGE], 0,
2414 print_context, page_nr);
2416 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING)
2417 gtk_print_operation_draw_page_finish (op);
2421 prepare_data (PrintPagesData *data)
2423 GtkPrintOperationPrivate *priv;
2424 GtkPageSetup *page_setup;
2427 priv = data->op->priv;
2429 if (!data->initialized)
2431 data->initialized = TRUE;
2432 page_setup = create_page_setup (data->op);
2433 _gtk_print_context_set_page_setup (priv->print_context,
2435 g_object_unref (page_setup);
2437 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2439 if (priv->manual_collation)
2441 data->uncollated_copies = priv->manual_num_copies;
2442 data->collated_copies = 1;
2446 data->uncollated_copies = 1;
2447 data->collated_copies = priv->manual_num_copies;
2453 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2455 gboolean paginated = FALSE;
2457 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2462 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2464 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2466 if (priv->page_ranges == NULL)
2468 g_warning ("no pages to print");
2469 priv->cancelled = TRUE;
2472 data->ranges = priv->page_ranges;
2473 data->num_ranges = priv->num_page_ranges;
2474 for (i = 0; i < data->num_ranges; i++)
2475 if (data->ranges[i].end == -1 ||
2476 data->ranges[i].end >= priv->nr_of_pages)
2477 data->ranges[i].end = priv->nr_of_pages - 1;
2479 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2480 priv->current_page != -1)
2482 data->ranges = &data->one_range;
2483 data->num_ranges = 1;
2484 data->ranges[0].start = priv->current_page;
2485 data->ranges[0].end = priv->current_page;
2489 data->ranges = &data->one_range;
2490 data->num_ranges = 1;
2491 data->ranges[0].start = 0;
2492 data->ranges[0].end = priv->nr_of_pages - 1;
2495 clamp_page_ranges (data);
2497 if (data->num_ranges < 1)
2499 priv->cancelled = TRUE;
2503 priv->nr_of_pages_to_print = 0;
2504 for (i = 0; i < data->num_ranges; i++)
2505 priv->nr_of_pages_to_print += data->ranges[i].end - data->ranges[i].start + 1;
2507 data->pages = g_new (gint, priv->nr_of_pages_to_print);
2509 for (i = 0; i < data->num_ranges; i++)
2510 for (j = data->ranges[i].start; j <= data->ranges[i].end; j++)
2512 data->pages[counter] = j;
2518 data->uncollated = 0;
2520 if (priv->nr_of_pages_to_print % priv->manual_number_up == 0)
2521 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up;
2523 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up + 1;
2525 if (priv->manual_reverse)
2527 /* data->sheet is 0-based */
2528 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2529 data->sheet = (data->num_of_sheets - 1) - (data->num_of_sheets - 1) % 2;
2530 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2531 data->sheet = (data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2);
2533 data->sheet = data->num_of_sheets - 1;
2537 /* data->sheet is 0-based */
2538 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2540 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2542 if (data->num_of_sheets > 1)
2551 priv->page_position = data->sheet * priv->manual_number_up;
2553 if (priv->page_position < 0 || priv->page_position >= priv->nr_of_pages_to_print)
2555 priv->cancelled = TRUE;
2559 data->page = data->pages[priv->page_position];
2560 data->first_position = priv->page_position;
2561 data->first_sheet = data->sheet;
2563 if (priv->manual_reverse)
2565 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2566 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2567 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2568 data->last_position = MIN (2 * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2570 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2574 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2575 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);
2576 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2577 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);
2579 data->last_position = priv->nr_of_pages_to_print - 1;
2583 _gtk_print_operation_set_status (data->op,
2584 GTK_PRINT_STATUS_GENERATING_DATA,
2589 print_pages_idle (gpointer user_data)
2591 PrintPagesData *data;
2592 GtkPrintOperationPrivate *priv;
2593 gboolean done = FALSE;
2595 data = (PrintPagesData*)user_data;
2596 priv = data->op->priv;
2598 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
2600 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2602 prepare_data (data);
2606 if (data->is_preview && !priv->cancelled)
2610 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2616 common_render_page (data->op, data->page);
2617 increment_page_sequence (data);
2620 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
2624 if (priv->cancelled)
2626 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2628 data->is_preview = FALSE;
2632 if (done && !data->is_preview)
2634 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2635 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2638 update_progress (data);
2645 handle_progress_response (GtkWidget *dialog,
2649 GtkPrintOperation *op = (GtkPrintOperation *)data;
2651 gtk_widget_hide (dialog);
2652 gtk_print_operation_cancel (op);
2656 show_progress_timeout (PrintPagesData *data)
2658 gtk_window_present (GTK_WINDOW (data->progress));
2660 data->op->priv->show_progress_timeout_id = 0;
2666 print_pages (GtkPrintOperation *op,
2669 GtkPrintOperationResult result)
2671 GtkPrintOperationPrivate *priv = op->priv;
2672 PrintPagesData *data;
2676 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2677 g_signal_emit (op, signals[DONE], 0, result);
2681 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2683 data = g_new0 (PrintPagesData, 1);
2684 data->op = g_object_ref (op);
2685 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2687 if (priv->show_progress)
2689 GtkWidget *progress;
2691 progress = gtk_message_dialog_new (parent, 0,
2695 g_signal_connect (progress, "response",
2696 G_CALLBACK (handle_progress_response), op);
2698 priv->show_progress_timeout_id =
2699 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2700 (GSourceFunc)show_progress_timeout,
2703 data->progress = progress;
2706 if (data->is_preview)
2710 g_signal_emit_by_name (op, "preview",
2711 GTK_PRINT_OPERATION_PREVIEW (op),
2712 priv->print_context,
2718 GtkWidget *error_dialog;
2720 error_dialog = gtk_message_dialog_new (parent,
2721 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
2724 _("Error creating print preview"));
2726 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
2727 _("The most probable reason is that a temporary file could not be created."));
2729 if (parent && parent->group)
2730 gtk_window_group_add_window (parent->group, GTK_WINDOW (error_dialog));
2732 g_signal_connect (error_dialog, "response",
2733 G_CALLBACK (gtk_widget_destroy), NULL);
2735 gtk_widget_show (error_dialog);
2737 print_pages_idle_done (data);
2742 if (gtk_print_context_get_cairo_context (priv->print_context) == NULL)
2744 /* Programmer error */
2745 g_error ("You must set a cairo context on the print context");
2748 priv->start_page = preview_start_page;
2749 priv->end_page = preview_end_page;
2750 priv->end_run = preview_end_run;
2752 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
2753 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
2754 &priv->num_page_ranges);
2755 priv->manual_num_copies = 1;
2756 priv->manual_collation = FALSE;
2757 priv->manual_reverse = FALSE;
2758 priv->manual_page_set = GTK_PAGE_SET_ALL;
2759 priv->manual_scale = 1.0;
2760 priv->manual_orientation = TRUE;
2761 priv->manual_number_up = 1;
2762 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2765 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
2768 print_pages_idle_done);
2770 /* Recursive main loop to make sure we don't exit on sync operations */
2773 priv->rloop = g_main_loop_new (NULL, FALSE);
2776 GDK_THREADS_LEAVE ();
2777 g_main_loop_run (priv->rloop);
2778 GDK_THREADS_ENTER ();
2780 g_main_loop_unref (priv->rloop);
2782 g_object_unref (op);
2787 * gtk_print_operation_get_error:
2788 * @op: a #GtkPrintOperation
2789 * @error: return location for the error
2791 * Call this when the result of a print operation is
2792 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
2793 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
2794 * handler. The returned #GError will contain more details on what went wrong.
2799 gtk_print_operation_get_error (GtkPrintOperation *op,
2802 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2804 g_propagate_error (error, op->priv->error);
2806 op->priv->error = NULL;
2811 * gtk_print_operation_run:
2812 * @op: a #GtkPrintOperation
2813 * @action: the action to start
2814 * @parent: Transient parent of the dialog, or %NULL
2815 * @error: Return location for errors, or %NULL
2817 * Runs the print operation, by first letting the user modify
2818 * print settings in the print dialog, and then print the document.
2820 * Normally that this function does not return until the rendering of all
2821 * pages is complete. You can connect to the
2822 * #GtkPrintOperation::status-changed signal on @op to obtain some
2823 * information about the progress of the print operation.
2824 * Furthermore, it may use a recursive mainloop to show the print dialog.
2826 * If you call gtk_print_operation_set_allow_async() or set the
2827 * #GtkPrintOperation:allow-async property the operation will run
2828 * asynchronously if this is supported on the platform. The
2829 * #GtkPrintOperation::done signal will be emitted with the result of the
2830 * operation when the it is done (i.e. when the dialog is canceled, or when
2831 * the print succeeds or fails).
2833 * if (settings != NULL)
2834 * gtk_print_operation_set_print_settings (print, settings);
2836 * if (page_setup != NULL)
2837 * gtk_print_operation_set_default_page_setup (print, page_setup);
2839 * g_signal_connect (print, "begin-print",
2840 * G_CALLBACK (begin_print), &data);
2841 * g_signal_connect (print, "draw-page",
2842 * G_CALLBACK (draw_page), &data);
2844 * res = gtk_print_operation_run (print,
2845 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2849 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
2851 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
2852 * GTK_DIALOG_DESTROY_WITH_PARENT,
2853 * GTK_MESSAGE_ERROR,
2854 * GTK_BUTTONS_CLOSE,
2855 * "Error printing file:\n%s",
2857 * g_signal_connect (error_dialog, "response",
2858 * G_CALLBACK (gtk_widget_destroy), NULL);
2859 * gtk_widget_show (error_dialog);
2860 * g_error_free (error);
2862 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
2864 * if (settings != NULL)
2865 * g_object_unref (settings);
2866 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
2870 * Note that gtk_print_operation_run() can only be called once on a
2871 * given #GtkPrintOperation.
2873 * Return value: the result of the print operation. A return value of
2874 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
2875 * completed successfully. In this case, it is a good idea to obtain
2876 * the used print settings with gtk_print_operation_get_print_settings()
2877 * and store them for reuse with the next print operation. A value of
2878 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
2879 * asynchronously, and will emit the #GtkPrintOperation::done signal when
2884 GtkPrintOperationResult
2885 gtk_print_operation_run (GtkPrintOperation *op,
2886 GtkPrintOperationAction action,
2890 GtkPrintOperationPrivate *priv;
2891 GtkPrintOperationResult result;
2892 GtkPageSetup *page_setup;
2894 gboolean run_print_pages;
2896 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
2897 GTK_PRINT_OPERATION_RESULT_ERROR);
2898 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
2899 GTK_PRINT_OPERATION_RESULT_ERROR);
2902 run_print_pages = TRUE;
2905 priv->action = action;
2907 if (priv->print_settings == NULL)
2908 priv->print_settings = gtk_print_settings_new ();
2910 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
2912 /* note: if you implement async EXPORT, update the docs
2913 * docs for the allow-async property.
2915 priv->is_sync = TRUE;
2916 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
2917 result = run_pdf (op, parent, &do_print);
2919 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
2921 priv->is_sync = !priv->allow_async;
2922 priv->print_context = _gtk_print_context_new (op);
2923 page_setup = create_page_setup (op);
2924 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2925 g_object_unref (page_setup);
2927 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2930 else if (priv->allow_async)
2932 priv->is_sync = FALSE;
2933 _gtk_print_operation_platform_backend_run_dialog_async (op,
2934 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2937 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2938 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
2943 priv->is_sync = TRUE;
2944 result = _gtk_print_operation_platform_backend_run_dialog (op,
2945 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2950 if (run_print_pages)
2951 print_pages (op, parent, do_print, result);
2953 if (priv->error && error)
2954 *error = g_error_copy (priv->error);
2960 * gtk_print_operation_cancel:
2961 * @op: a #GtkPrintOperation
2963 * Cancels a running print operation. This function may
2964 * be called from a #GtkPrintOperation::begin-print,
2965 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
2966 * signal handler to stop the currently running print
2972 gtk_print_operation_cancel (GtkPrintOperation *op)
2974 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2976 op->priv->cancelled = TRUE;
2981 #define __GTK_PRINT_OPERATION_C__
2982 #include "gtkaliasdef.c"