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,
72 PROP_CUSTOM_TAB_LABEL,
73 PROP_EMBED_PAGE_SETUP,
75 PROP_SUPPORT_SELECTION,
79 static guint signals[LAST_SIGNAL] = { 0 };
80 static int job_nr = 0;
82 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
83 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
84 static void common_render_page (GtkPrintOperation *op,
88 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
89 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
93 * gtk_print_error_quark:
95 * Registers an error quark for #GtkPrintOperation if necessary.
97 * Return value: The error quark used for #GtkPrintOperation errors.
102 gtk_print_error_quark (void)
104 static GQuark quark = 0;
106 quark = g_quark_from_static_string ("gtk-print-error-quark");
111 gtk_print_operation_finalize (GObject *object)
113 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
114 GtkPrintOperationPrivate *priv = print_operation->priv;
116 if (priv->free_platform_data &&
119 priv->free_platform_data (priv->platform_data);
120 priv->free_platform_data = NULL;
123 if (priv->default_page_setup)
124 g_object_unref (priv->default_page_setup);
126 if (priv->print_settings)
127 g_object_unref (priv->print_settings);
129 if (priv->print_context)
130 g_object_unref (priv->print_context);
132 g_free (priv->export_filename);
133 g_free (priv->job_name);
134 g_free (priv->custom_tab_label);
135 g_free (priv->status_string);
137 if (priv->print_pages_idle_id > 0)
138 g_source_remove (priv->print_pages_idle_id);
140 if (priv->show_progress_timeout_id > 0)
141 g_source_remove (priv->show_progress_timeout_id);
144 g_error_free (priv->error);
146 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
150 gtk_print_operation_init (GtkPrintOperation *operation)
152 GtkPrintOperationPrivate *priv;
155 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
157 priv->status = GTK_PRINT_STATUS_INITIAL;
158 priv->status_string = g_strdup ("");
159 priv->default_page_setup = NULL;
160 priv->print_settings = NULL;
161 priv->nr_of_pages = -1;
162 priv->nr_of_pages_to_print = -1;
163 priv->page_position = -1;
164 priv->current_page = -1;
165 priv->use_full_page = FALSE;
166 priv->show_progress = FALSE;
167 priv->export_filename = NULL;
168 priv->track_print_status = FALSE;
169 priv->is_sync = FALSE;
170 priv->support_selection = FALSE;
171 priv->has_selection = FALSE;
172 priv->embed_page_setup = FALSE;
174 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
177 priv->unit = GTK_UNIT_PIXEL;
179 appname = g_get_application_name ();
182 /* translators: this string is the default job title for print
183 * jobs. %s gets replaced by the application name, %d gets replaced
186 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
190 preview_iface_render_page (GtkPrintOperationPreview *preview,
194 GtkPrintOperation *op;
196 op = GTK_PRINT_OPERATION (preview);
197 common_render_page (op, page_nr);
201 preview_iface_end_preview (GtkPrintOperationPreview *preview)
203 GtkPrintOperation *op;
205 op = GTK_PRINT_OPERATION (preview);
207 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
210 g_main_loop_quit (op->priv->rloop);
212 if (op->priv->end_run)
213 op->priv->end_run (op, op->priv->is_sync, TRUE);
215 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
217 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
221 preview_iface_is_selected (GtkPrintOperationPreview *preview,
224 GtkPrintOperation *op;
225 GtkPrintOperationPrivate *priv;
228 op = GTK_PRINT_OPERATION (preview);
231 switch (priv->print_pages)
233 case GTK_PRINT_PAGES_SELECTION:
234 case GTK_PRINT_PAGES_ALL:
235 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
236 case GTK_PRINT_PAGES_CURRENT:
237 return page_nr == priv->current_page;
238 case GTK_PRINT_PAGES_RANGES:
239 for (i = 0; i < priv->num_page_ranges; i++)
241 if (page_nr >= priv->page_ranges[i].start &&
242 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
251 preview_iface_init (GtkPrintOperationPreviewIface *iface)
253 iface->render_page = preview_iface_render_page;
254 iface->end_preview = preview_iface_end_preview;
255 iface->is_selected = preview_iface_is_selected;
259 preview_start_page (GtkPrintOperation *op,
260 GtkPrintContext *print_context,
261 GtkPageSetup *page_setup)
263 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
267 preview_end_page (GtkPrintOperation *op,
268 GtkPrintContext *print_context)
273 preview_end_run (GtkPrintOperation *op,
277 g_free (op->priv->page_ranges);
278 op->priv->page_ranges = NULL;
283 gtk_print_operation_set_property (GObject *object,
288 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
292 case PROP_DEFAULT_PAGE_SETUP:
293 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
295 case PROP_PRINT_SETTINGS:
296 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
299 gtk_print_operation_set_job_name (op, g_value_get_string (value));
302 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
304 case PROP_CURRENT_PAGE:
305 gtk_print_operation_set_current_page (op, g_value_get_int (value));
307 case PROP_USE_FULL_PAGE:
308 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
310 case PROP_TRACK_PRINT_STATUS:
311 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
314 gtk_print_operation_set_unit (op, g_value_get_enum (value));
316 case PROP_ALLOW_ASYNC:
317 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
319 case PROP_SHOW_PROGRESS:
320 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
322 case PROP_EXPORT_FILENAME:
323 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
325 case PROP_CUSTOM_TAB_LABEL:
326 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
328 case PROP_EMBED_PAGE_SETUP:
329 gtk_print_operation_set_embed_page_setup (op, g_value_get_boolean (value));
331 case PROP_HAS_SELECTION:
332 gtk_print_operation_set_has_selection (op, g_value_get_boolean (value));
334 case PROP_SUPPORT_SELECTION:
335 gtk_print_operation_set_support_selection (op, g_value_get_boolean (value));
338 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
344 gtk_print_operation_get_property (GObject *object,
349 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
350 GtkPrintOperationPrivate *priv = op->priv;
354 case PROP_DEFAULT_PAGE_SETUP:
355 g_value_set_object (value, priv->default_page_setup);
357 case PROP_PRINT_SETTINGS:
358 g_value_set_object (value, priv->print_settings);
361 g_value_set_string (value, priv->job_name);
364 g_value_set_int (value, priv->nr_of_pages);
366 case PROP_CURRENT_PAGE:
367 g_value_set_int (value, priv->current_page);
369 case PROP_USE_FULL_PAGE:
370 g_value_set_boolean (value, priv->use_full_page);
372 case PROP_TRACK_PRINT_STATUS:
373 g_value_set_boolean (value, priv->track_print_status);
376 g_value_set_enum (value, priv->unit);
378 case PROP_ALLOW_ASYNC:
379 g_value_set_boolean (value, priv->allow_async);
381 case PROP_SHOW_PROGRESS:
382 g_value_set_boolean (value, priv->show_progress);
384 case PROP_EXPORT_FILENAME:
385 g_value_set_string (value, priv->export_filename);
388 g_value_set_enum (value, priv->status);
390 case PROP_STATUS_STRING:
391 g_value_set_string (value, priv->status_string);
393 case PROP_CUSTOM_TAB_LABEL:
394 g_value_set_string (value, priv->custom_tab_label);
396 case PROP_EMBED_PAGE_SETUP:
397 g_value_set_boolean (value, priv->embed_page_setup);
399 case PROP_HAS_SELECTION:
400 g_value_set_boolean (value, priv->has_selection);
402 case PROP_SUPPORT_SELECTION:
403 g_value_set_boolean (value, priv->support_selection);
405 case PROP_N_PAGES_TO_PRINT:
406 g_value_set_int (value, priv->nr_of_pages_to_print);
409 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
416 GtkPrintOperationPreview *preview;
417 GtkPrintContext *print_context;
419 cairo_surface_t *surface;
426 preview_print_idle_done (gpointer data)
428 GtkPrintOperation *op;
429 PreviewOp *pop = (PreviewOp *) data;
431 op = GTK_PRINT_OPERATION (pop->preview);
433 cairo_surface_finish (pop->surface);
434 /* Surface is destroyed in launch_preview */
435 _gtk_print_operation_platform_backend_launch_preview (op,
440 g_free (pop->filename);
442 gtk_print_operation_preview_end_preview (pop->preview);
449 preview_print_idle (gpointer data)
452 GtkPrintOperation *op;
453 gboolean retval = TRUE;
455 GtkPrintOperationPrivate *priv;
457 pop = (PreviewOp *) data;
458 op = GTK_PRINT_OPERATION (pop->preview);
462 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
464 /* TODO: print out sheets not pages and follow ranges */
465 if (pop->page_nr >= op->priv->nr_of_pages)
468 if (pop->page_nr > 0)
470 cr = gtk_print_context_get_cairo_context (pop->print_context);
471 _gtk_print_operation_platform_backend_preview_end_page (op, pop->surface, cr);
476 gtk_print_operation_preview_render_page (pop->preview, pop->page_nr);
485 preview_got_page_size (GtkPrintOperationPreview *preview,
486 GtkPrintContext *context,
487 GtkPageSetup *page_setup,
490 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
493 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
495 cr = gtk_print_context_get_cairo_context (pop->print_context);
496 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
501 preview_ready (GtkPrintOperationPreview *preview,
502 GtkPrintContext *context,
506 pop->print_context = context;
508 g_object_ref (preview);
510 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
513 preview_print_idle_done);
518 gtk_print_operation_preview_handler (GtkPrintOperation *op,
519 GtkPrintOperationPreview *preview,
520 GtkPrintContext *context,
523 gdouble dpi_x, dpi_y;
525 GtkPageSetup *page_setup;
528 pop = g_new0 (PreviewOp, 1);
529 pop->filename = NULL;
530 pop->preview = preview;
531 pop->parent = parent;
533 page_setup = gtk_print_context_get_page_setup (context);
536 _gtk_print_operation_platform_backend_create_preview_surface (op,
541 if (pop->surface == NULL)
547 cr = cairo_create (pop->surface);
548 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
552 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
553 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
559 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
565 gtk_print_operation_done (GtkPrintOperation *operation,
566 GtkPrintOperationResult result)
568 GtkPrintOperationPrivate *priv = operation->priv;
570 if (priv->print_context)
572 g_object_unref (priv->print_context);
573 priv->print_context = NULL;
578 custom_widget_accumulator (GSignalInvocationHint *ihint,
580 const GValue *handler_return,
583 gboolean continue_emission;
586 widget = g_value_get_object (handler_return);
588 g_value_set_object (return_accu, widget);
589 continue_emission = (widget == NULL);
591 return continue_emission;
595 gtk_print_operation_class_init (GtkPrintOperationClass *class)
597 GObjectClass *gobject_class = (GObjectClass *)class;
599 gobject_class->set_property = gtk_print_operation_set_property;
600 gobject_class->get_property = gtk_print_operation_get_property;
601 gobject_class->finalize = gtk_print_operation_finalize;
603 class->preview = gtk_print_operation_preview_handler;
604 class->create_custom_widget = gtk_print_operation_create_custom_widget;
605 class->done = gtk_print_operation_done;
607 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
610 * GtkPrintOperation::done:
611 * @operation: the #GtkPrintOperation on which the signal was emitted
612 * @result: the result of the print operation
614 * Emitted when the print operation run has finished doing
615 * everything required for printing.
617 * @result gives you information about what happened during the run.
618 * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
619 * gtk_print_operation_get_error() for more information.
621 * If you enabled print status tracking then
622 * gtk_print_operation_is_finished() may still return %FALSE
623 * after #GtkPrintOperation::done was emitted.
628 g_signal_new (I_("done"),
629 G_TYPE_FROM_CLASS (gobject_class),
631 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
633 g_cclosure_marshal_VOID__ENUM,
634 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
637 * GtkPrintOperation::begin-print:
638 * @operation: the #GtkPrintOperation on which the signal was emitted
639 * @context: the #GtkPrintContext for the current operation
641 * Emitted after the user has finished changing print settings
642 * in the dialog, before the actual rendering starts.
644 * A typical use for ::begin-print is to use the parameters from the
645 * #GtkPrintContext and paginate the document accordingly, and then
646 * set the number of pages with gtk_print_operation_set_n_pages().
650 signals[BEGIN_PRINT] =
651 g_signal_new (I_("begin-print"),
652 G_TYPE_FROM_CLASS (gobject_class),
654 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
656 g_cclosure_marshal_VOID__OBJECT,
657 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
660 * GtkPrintOperation::paginate:
661 * @operation: the #GtkPrintOperation on which the signal was emitted
662 * @context: the #GtkPrintContext for the current operation
664 * Emitted after the #GtkPrintOperation::begin-print signal, but before
665 * the actual rendering starts. It keeps getting emitted until a connected
666 * signal handler returns %TRUE.
668 * The ::paginate signal is intended to be used for paginating a document
669 * in small chunks, to avoid blocking the user interface for a long
670 * time. The signal handler should update the number of pages using
671 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
672 * has been completely paginated.
674 * If you don't need to do pagination in chunks, you can simply do
675 * it all in the ::begin-print handler, and set the number of pages
678 * Return value: %TRUE if pagination is complete
683 g_signal_new (I_("paginate"),
684 G_TYPE_FROM_CLASS (gobject_class),
686 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
687 _gtk_boolean_handled_accumulator, NULL,
688 _gtk_marshal_BOOLEAN__OBJECT,
689 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
693 * GtkPrintOperation::request-page-setup:
694 * @operation: the #GtkPrintOperation on which the signal was emitted
695 * @context: the #GtkPrintContext for the current operation
696 * @page_nr: the number of the currently printed page (0-based)
697 * @setup: the #GtkPageSetup
699 * Emitted once for every page that is printed, to give
700 * the application a chance to modify the page setup. Any changes
701 * done to @setup will be in force only for printing this page.
705 signals[REQUEST_PAGE_SETUP] =
706 g_signal_new (I_("request-page-setup"),
707 G_TYPE_FROM_CLASS (gobject_class),
709 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
711 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
713 GTK_TYPE_PRINT_CONTEXT,
715 GTK_TYPE_PAGE_SETUP);
718 * GtkPrintOperation::draw-page:
719 * @operation: the #GtkPrintOperation on which the signal was emitted
720 * @context: the #GtkPrintContext for the current operation
721 * @page_nr: the number of the currently printed page (0-based)
723 * Emitted for every page that is printed. The signal handler
724 * must render the @page_nr's page onto the cairo context obtained
725 * from @context using gtk_print_context_get_cairo_context().
728 * draw_page (GtkPrintOperation *operation,
729 * GtkPrintContext *context,
731 * gpointer user_data)
734 * PangoLayout *layout;
735 * gdouble width, text_height;
736 * gint layout_height;
737 * PangoFontDescription *desc;
739 * cr = gtk_print_context_get_cairo_context (context);
740 * width = gtk_print_context_get_width (context);
742 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
744 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
747 * layout = gtk_print_context_create_pango_layout (context);
749 * desc = pango_font_description_from_string ("sans 14");
750 * pango_layout_set_font_description (layout, desc);
751 * pango_font_description_free (desc);
753 * pango_layout_set_text (layout, "some text", -1);
754 * pango_layout_set_width (layout, width * PANGO_SCALE);
755 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
757 * pango_layout_get_size (layout, NULL, &layout_height);
758 * text_height = (gdouble)layout_height / PANGO_SCALE;
760 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
761 * pango_cairo_show_layout (cr, layout);
763 * g_object_unref (layout);
767 * Use gtk_print_operation_set_use_full_page() and
768 * gtk_print_operation_set_unit() before starting the print operation
769 * to set up the transformation of the cairo context according to your
775 g_signal_new (I_("draw-page"),
776 G_TYPE_FROM_CLASS (gobject_class),
778 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
780 _gtk_marshal_VOID__OBJECT_INT,
782 GTK_TYPE_PRINT_CONTEXT,
786 * GtkPrintOperation::end-print:
787 * @operation: the #GtkPrintOperation on which the signal was emitted
788 * @context: the #GtkPrintContext for the current operation
790 * Emitted after all pages have been rendered.
791 * A handler for this signal can clean up any resources that have
792 * been allocated in the #GtkPrintOperation::begin-print handler.
797 g_signal_new (I_("end-print"),
798 G_TYPE_FROM_CLASS (gobject_class),
800 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
802 g_cclosure_marshal_VOID__OBJECT,
803 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
806 * GtkPrintOperation::status-changed:
807 * @operation: the #GtkPrintOperation on which the signal was emitted
809 * Emitted at between the various phases of the print operation.
810 * See #GtkPrintStatus for the phases that are being discriminated.
811 * Use gtk_print_operation_get_status() to find out the current
816 signals[STATUS_CHANGED] =
817 g_signal_new (I_("status-changed"),
818 G_TYPE_FROM_CLASS (class),
820 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
822 g_cclosure_marshal_VOID__VOID,
827 * GtkPrintOperation::create-custom-widget:
828 * @operation: the #GtkPrintOperation on which the signal was emitted
830 * Emitted when displaying the print dialog. If you return a
831 * widget in a handler for this signal it will be added to a custom
832 * tab in the print dialog. You typically return a container widget
833 * with multiple widgets in it.
835 * The print dialog owns the returned widget, and its lifetime is not
836 * controlled by the application. However, the widget is guaranteed
837 * to stay around until the #GtkPrintOperation::custom-widget-apply
838 * signal is emitted on the operation. Then you can read out any
839 * information you need from the widgets.
841 * Returns: A custom widget that gets embedded in the print dialog,
846 signals[CREATE_CUSTOM_WIDGET] =
847 g_signal_new (I_("create-custom-widget"),
848 G_TYPE_FROM_CLASS (class),
850 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
851 custom_widget_accumulator, NULL,
852 _gtk_marshal_OBJECT__VOID,
856 * GtkPrintOperation::update-custom-widget:
857 * @operation: the #GtkPrintOperation on which the signal was emitted
858 * @widget: the custom widget added in create-custom-widget
859 * @setup: actual page setup
860 * @settings: actual print settings
862 * Emitted after change of selected printer. The actual page setup and
863 * print settings are passed to the custom widget, which can actualize
864 * itself according to this change.
868 signals[UPDATE_CUSTOM_WIDGET] =
869 g_signal_new (I_("update-custom-widget"),
870 G_TYPE_FROM_CLASS (class),
872 G_STRUCT_OFFSET (GtkPrintOperationClass, update_custom_widget),
874 _gtk_marshal_VOID__OBJECT_OBJECT_OBJECT,
875 G_TYPE_NONE, 3, GTK_TYPE_WIDGET, GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);
878 * GtkPrintOperation::custom-widget-apply:
879 * @operation: the #GtkPrintOperation on which the signal was emitted
880 * @widget: the custom widget added in create-custom-widget
882 * Emitted right before #GtkPrintOperation::begin-print if you added
883 * a custom widget in the #GtkPrintOperation::create-custom-widget handler.
884 * When you get this signal you should read the information from the
885 * custom widgets, as the widgets are not guaraneed to be around at a
890 signals[CUSTOM_WIDGET_APPLY] =
891 g_signal_new (I_("custom-widget-apply"),
892 G_TYPE_FROM_CLASS (class),
894 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
896 g_cclosure_marshal_VOID__OBJECT,
897 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
900 * GtkPrintOperation::preview:
901 * @operation: the #GtkPrintOperation on which the signal was emitted
902 * @preview: the #GtkPrintPreviewOperation for the current operation
903 * @context: the #GtkPrintContext that will be used
904 * @parent: the #GtkWindow to use as window parent, or %NULL
906 * Gets emitted when a preview is requested from the native dialog.
908 * The default handler for this signal uses an external viewer
909 * application to preview.
911 * To implement a custom print preview, an application must return
912 * %TRUE from its handler for this signal. In order to use the
913 * provided @context for the preview implementation, it must be
914 * given a suitable cairo context with gtk_print_context_set_cairo_context().
916 * The custom preview implementation can use
917 * gtk_print_operation_preview_is_selected() and
918 * gtk_print_operation_preview_render_page() to find pages which
919 * are selected for print and render them. The preview must be
920 * finished by calling gtk_print_operation_preview_end_preview()
921 * (typically in response to the user clicking a close button).
923 * Returns: %TRUE if the listener wants to take over control of the preview
928 g_signal_new (I_("preview"),
929 G_TYPE_FROM_CLASS (gobject_class),
931 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
932 _gtk_boolean_handled_accumulator, NULL,
933 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
935 GTK_TYPE_PRINT_OPERATION_PREVIEW,
936 GTK_TYPE_PRINT_CONTEXT,
941 * GtkPrintOperation:default-page-setup:
943 * The #GtkPageSetup used by default.
945 * This page setup will be used by gtk_print_operation_run(),
946 * but it can be overridden on a per-page basis by connecting
947 * to the #GtkPrintOperation::request-page-setup signal.
951 g_object_class_install_property (gobject_class,
952 PROP_DEFAULT_PAGE_SETUP,
953 g_param_spec_object ("default-page-setup",
954 P_("Default Page Setup"),
955 P_("The GtkPageSetup used by default"),
957 GTK_PARAM_READWRITE));
960 * GtkPrintOperation:print-settings:
962 * The #GtkPrintSettings used for initializing the dialog.
964 * Setting this property is typically used to re-establish
965 * print settings from a previous print operation, see
966 * gtk_print_operation_run().
970 g_object_class_install_property (gobject_class,
972 g_param_spec_object ("print-settings",
973 P_("Print Settings"),
974 P_("The GtkPrintSettings used for initializing the dialog"),
975 GTK_TYPE_PRINT_SETTINGS,
976 GTK_PARAM_READWRITE));
979 * GtkPrintOperation:job-name:
981 * A string used to identify the job (e.g. in monitoring
982 * applications like eggcups).
984 * If you don't set a job name, GTK+ picks a default one
985 * by numbering successive print jobs.
989 g_object_class_install_property (gobject_class,
991 g_param_spec_string ("job-name",
993 P_("A string used for identifying the print job."),
995 GTK_PARAM_READWRITE));
998 * GtkPrintOperation:n-pages:
1000 * The number of pages in the document.
1002 * This <emphasis>must</emphasis> be set to a positive number
1003 * before the rendering starts. It may be set in a
1004 * #GtkPrintOperation::begin-print signal hander.
1006 * Note that the page numbers passed to the
1007 * #GtkPrintOperation::request-page-setup and
1008 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1009 * the user chooses to print all pages, the last ::draw-page signal
1010 * will be for page @n_pages - 1.
1014 g_object_class_install_property (gobject_class,
1016 g_param_spec_int ("n-pages",
1017 P_("Number of Pages"),
1018 P_("The number of pages in the document."),
1022 GTK_PARAM_READWRITE));
1025 * GtkPrintOperation:current-page:
1027 * The current page in the document.
1029 * If this is set before gtk_print_operation_run(),
1030 * the user will be able to select to print only the current page.
1032 * Note that this only makes sense for pre-paginated documents.
1036 g_object_class_install_property (gobject_class,
1038 g_param_spec_int ("current-page",
1040 P_("The current page in the document"),
1044 GTK_PARAM_READWRITE));
1047 * GtkPrintOperation:use-full-page:
1049 * If %TRUE, the transformation for the cairo context obtained
1050 * from #GtkPrintContext puts the origin at the top left corner
1051 * of the page (which may not be the top left corner of the sheet,
1052 * depending on page orientation and the number of pages per sheet).
1053 * Otherwise, the origin is at the top left corner of the imageable
1054 * area (i.e. inside the margins).
1058 g_object_class_install_property (gobject_class,
1060 g_param_spec_boolean ("use-full-page",
1061 P_("Use full page"),
1062 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
1064 GTK_PARAM_READWRITE));
1068 * GtkPrintOperation:track-print-status:
1070 * If %TRUE, the print operation will try to continue report on
1071 * the status of the print job in the printer queues and printer.
1072 * This can allow your application to show things like "out of paper"
1073 * issues, and when the print job actually reaches the printer.
1074 * However, this is often implemented using polling, and should
1075 * not be enabled unless needed.
1079 g_object_class_install_property (gobject_class,
1080 PROP_TRACK_PRINT_STATUS,
1081 g_param_spec_boolean ("track-print-status",
1082 P_("Track Print Status"),
1083 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."),
1085 GTK_PARAM_READWRITE));
1089 * GtkPrintOperation:unit:
1091 * The transformation for the cairo context obtained from
1092 * #GtkPrintContext is set up in such a way that distances
1093 * are measured in units of @unit.
1097 g_object_class_install_property (gobject_class,
1099 g_param_spec_enum ("unit",
1101 P_("The unit in which distances can be measured in the context"),
1104 GTK_PARAM_READWRITE));
1108 * GtkPrintOperation:show-progress:
1110 * Determines whether to show a progress dialog during the
1115 g_object_class_install_property (gobject_class,
1117 g_param_spec_boolean ("show-progress",
1119 P_("TRUE if a progress dialog is shown while printing."),
1121 GTK_PARAM_READWRITE));
1124 * GtkPrintOperation:allow-async:
1126 * Determines whether the print operation may run asynchronously or not.
1128 * Some systems don't support asynchronous printing, but those that do
1129 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1130 * emit the #GtkPrintOperation::done signal when the operation is actually
1133 * The Windows port does not support asynchronous operation at all (this
1134 * is unlikely to change). On other platforms, all actions except for
1135 * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
1139 g_object_class_install_property (gobject_class,
1141 g_param_spec_boolean ("allow-async",
1143 P_("TRUE if print process may run asynchronous."),
1145 GTK_PARAM_READWRITE));
1148 * GtkPrintOperation:export-filename:
1150 * The name of a file to generate instead of showing the print dialog.
1151 * Currently, PDF is the only supported format.
1153 * The intended use of this property is for implementing
1154 * "Export to PDF" actions.
1156 * "Print to PDF" support is independent of this and is done
1157 * by letting the user pick the "Print to PDF" item from the
1158 * list of printers in the print dialog.
1162 g_object_class_install_property (gobject_class,
1163 PROP_EXPORT_FILENAME,
1164 g_param_spec_string ("export-filename",
1165 P_("Export filename"),
1166 P_("Export filename"),
1168 GTK_PARAM_READWRITE));
1171 * GtkPrintOperation:status:
1173 * The status of the print operation.
1177 g_object_class_install_property (gobject_class,
1179 g_param_spec_enum ("status",
1181 P_("The status of the print operation"),
1182 GTK_TYPE_PRINT_STATUS,
1183 GTK_PRINT_STATUS_INITIAL,
1184 GTK_PARAM_READABLE));
1187 * GtkPrintOperation:status-string:
1189 * A string representation of the status of the print operation.
1190 * The string is translated and suitable for displaying the print
1191 * status e.g. in a #GtkStatusbar.
1193 * See the #GtkPrintOperation:status property for a status value that
1194 * is suitable for programmatic use.
1198 g_object_class_install_property (gobject_class,
1200 g_param_spec_string ("status-string",
1201 P_("Status String"),
1202 P_("A human-readable description of the status"),
1204 GTK_PARAM_READABLE));
1208 * GtkPrintOperation:custom-tab-label:
1210 * Used as the label of the tab containing custom widgets.
1211 * Note that this property may be ignored on some platforms.
1213 * If this is %NULL, GTK+ uses a default label.
1217 g_object_class_install_property (gobject_class,
1218 PROP_CUSTOM_TAB_LABEL,
1219 g_param_spec_string ("custom-tab-label",
1220 P_("Custom tab label"),
1221 P_("Label for the tab containing custom widgets."),
1223 GTK_PARAM_READWRITE));
1226 * GtkPrintOperation:support-selection:
1228 * If %TRUE, the print operation will support print of selection.
1229 * This allows the print dialog to show a "Selection" button.
1233 g_object_class_install_property (gobject_class,
1234 PROP_SUPPORT_SELECTION,
1235 g_param_spec_boolean ("support-selection",
1236 P_("Support Selection"),
1237 P_("TRUE if the print operation will support print of selection."),
1239 GTK_PARAM_READWRITE));
1242 * GtkPrintOperation:has-selection:
1244 * Determines whether there is a selection in your application.
1245 * This can allow your application to print the selection.
1246 * This is typically used to make a "Selection" button sensitive.
1250 g_object_class_install_property (gobject_class,
1252 g_param_spec_boolean ("has-selection",
1253 P_("Has Selection"),
1254 P_("TRUE if a selecion exists."),
1256 GTK_PARAM_READWRITE));
1260 * GtkPrintOperation:embed-page-setup:
1262 * If %TRUE, page size combo box and orientation combo box are embedded into page setup page.
1266 g_object_class_install_property (gobject_class,
1267 PROP_EMBED_PAGE_SETUP,
1268 g_param_spec_boolean ("embed-page-setup",
1269 P_("Embed Page Setup"),
1270 P_("TRUE if page setup combos are embedded in GtkPrintDialog"),
1272 GTK_PARAM_READWRITE));
1274 * GtkPrintOperation:n-pages-to-print:
1276 * The number of pages that will be printed.
1278 * Note that this value is set during print preparation phase
1279 * (%GTK_PRINT_STATUS_PREPARING), so this value should never be
1280 * get before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
1281 * You can connect to the #GtkPrintOperation::status-changed signal
1282 * and call gtk_print_operation_get_n_pages_to_print() when
1283 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
1284 * This is typically used to track the progress of print operation.
1288 g_object_class_install_property (gobject_class,
1289 PROP_N_PAGES_TO_PRINT,
1290 g_param_spec_int ("n-pages-to-print",
1291 P_("Number of Pages To Print"),
1292 P_("The number of pages that will be printed."),
1296 GTK_PARAM_READABLE));
1300 * gtk_print_operation_new:
1302 * Creates a new #GtkPrintOperation.
1304 * Returns: a new #GtkPrintOperation
1309 gtk_print_operation_new (void)
1311 GtkPrintOperation *print_operation;
1313 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1315 return print_operation;
1319 * gtk_print_operation_set_default_page_setup:
1320 * @op: a #GtkPrintOperation
1321 * @default_page_setup: a #GtkPageSetup, or %NULL
1323 * Makes @default_page_setup the default page setup for @op.
1325 * This page setup will be used by gtk_print_operation_run(),
1326 * but it can be overridden on a per-page basis by connecting
1327 * to the #GtkPrintOperation::request-page-setup signal.
1332 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1333 GtkPageSetup *default_page_setup)
1335 GtkPrintOperationPrivate *priv;
1337 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1338 g_return_if_fail (default_page_setup == NULL ||
1339 GTK_IS_PAGE_SETUP (default_page_setup));
1343 if (default_page_setup != priv->default_page_setup)
1345 if (default_page_setup)
1346 g_object_ref (default_page_setup);
1348 if (priv->default_page_setup)
1349 g_object_unref (priv->default_page_setup);
1351 priv->default_page_setup = default_page_setup;
1353 g_object_notify (G_OBJECT (op), "default-page-setup");
1358 * gtk_print_operation_get_default_page_setup:
1359 * @op: a #GtkPrintOperation
1361 * Returns the default page setup, see
1362 * gtk_print_operation_set_default_page_setup().
1364 * Returns: the default page setup
1369 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1371 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1373 return op->priv->default_page_setup;
1378 * gtk_print_operation_set_print_settings:
1379 * @op: a #GtkPrintOperation
1380 * @print_settings: #GtkPrintSettings, or %NULL
1382 * Sets the print settings for @op. This is typically used to
1383 * re-establish print settings from a previous print operation,
1384 * see gtk_print_operation_run().
1389 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1390 GtkPrintSettings *print_settings)
1392 GtkPrintOperationPrivate *priv;
1394 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1395 g_return_if_fail (print_settings == NULL ||
1396 GTK_IS_PRINT_SETTINGS (print_settings));
1400 if (print_settings != priv->print_settings)
1403 g_object_ref (print_settings);
1405 if (priv->print_settings)
1406 g_object_unref (priv->print_settings);
1408 priv->print_settings = print_settings;
1410 g_object_notify (G_OBJECT (op), "print-settings");
1415 * gtk_print_operation_get_print_settings:
1416 * @op: a #GtkPrintOperation
1418 * Returns the current print settings.
1420 * Note that the return value is %NULL until either
1421 * gtk_print_operation_set_print_settings() or
1422 * gtk_print_operation_run() have been called.
1424 * Return value: the current print settings of @op.
1429 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1431 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1433 return op->priv->print_settings;
1437 * gtk_print_operation_set_job_name:
1438 * @op: a #GtkPrintOperation
1439 * @job_name: a string that identifies the print job
1441 * Sets the name of the print job. The name is used to identify
1442 * the job (e.g. in monitoring applications like eggcups).
1444 * If you don't set a job name, GTK+ picks a default one by
1445 * numbering successive print jobs.
1450 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1451 const gchar *job_name)
1453 GtkPrintOperationPrivate *priv;
1455 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1456 g_return_if_fail (job_name != NULL);
1460 g_free (priv->job_name);
1461 priv->job_name = g_strdup (job_name);
1463 g_object_notify (G_OBJECT (op), "job-name");
1467 * gtk_print_operation_set_n_pages:
1468 * @op: a #GtkPrintOperation
1469 * @n_pages: the number of pages
1471 * Sets the number of pages in the document.
1473 * This <emphasis>must</emphasis> be set to a positive number
1474 * before the rendering starts. It may be set in a
1475 * #GtkPrintOperation::begin-print signal hander.
1477 * Note that the page numbers passed to the
1478 * #GtkPrintOperation::request-page-setup
1479 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1480 * the user chooses to print all pages, the last ::draw-page signal
1481 * will be for page @n_pages - 1.
1486 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1489 GtkPrintOperationPrivate *priv;
1491 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1492 g_return_if_fail (n_pages > 0);
1495 g_return_if_fail (priv->current_page == -1 ||
1496 priv->current_page < n_pages);
1498 if (priv->nr_of_pages != n_pages)
1500 priv->nr_of_pages = n_pages;
1502 g_object_notify (G_OBJECT (op), "n-pages");
1507 * gtk_print_operation_set_current_page:
1508 * @op: a #GtkPrintOperation
1509 * @current_page: the current page, 0-based
1511 * Sets the current page.
1513 * If this is called before gtk_print_operation_run(),
1514 * the user will be able to select to print only the current page.
1516 * Note that this only makes sense for pre-paginated documents.
1521 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1524 GtkPrintOperationPrivate *priv;
1526 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1527 g_return_if_fail (current_page >= 0);
1530 g_return_if_fail (priv->nr_of_pages == -1 ||
1531 current_page < priv->nr_of_pages);
1533 if (priv->current_page != current_page)
1535 priv->current_page = current_page;
1537 g_object_notify (G_OBJECT (op), "current-page");
1542 * gtk_print_operation_set_use_full_page:
1543 * @op: a #GtkPrintOperation
1544 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1546 * If @full_page is %TRUE, the transformation for the cairo context
1547 * obtained from #GtkPrintContext puts the origin at the top left
1548 * corner of the page (which may not be the top left corner of the
1549 * sheet, depending on page orientation and the number of pages per
1550 * sheet). Otherwise, the origin is at the top left corner of the
1551 * imageable area (i.e. inside the margins).
1556 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1559 GtkPrintOperationPrivate *priv;
1561 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1563 full_page = full_page != FALSE;
1567 if (priv->use_full_page != full_page)
1569 priv->use_full_page = full_page;
1571 g_object_notify (G_OBJECT (op), "use-full-page");
1576 * gtk_print_operation_set_unit:
1577 * @op: a #GtkPrintOperation
1578 * @unit: the unit to use
1580 * Sets up the transformation for the cairo context obtained from
1581 * #GtkPrintContext in such a way that distances are measured in
1587 gtk_print_operation_set_unit (GtkPrintOperation *op,
1590 GtkPrintOperationPrivate *priv;
1592 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1596 if (priv->unit != unit)
1600 g_object_notify (G_OBJECT (op), "unit");
1605 * gtk_print_operation_set_track_print_status:
1606 * @op: a #GtkPrintOperation
1607 * @track_status: %TRUE to track status after printing
1609 * If track_status is %TRUE, the print operation will try to continue report
1610 * on the status of the print job in the printer queues and printer. This
1611 * can allow your application to show things like "out of paper" issues,
1612 * and when the print job actually reaches the printer.
1614 * This function is often implemented using some form of polling, so it should
1615 * not be enabled unless needed.
1620 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1621 gboolean track_status)
1623 GtkPrintOperationPrivate *priv;
1625 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1629 if (priv->track_print_status != track_status)
1631 priv->track_print_status = track_status;
1633 g_object_notify (G_OBJECT (op), "track-print-status");
1638 _gtk_print_operation_set_status (GtkPrintOperation *op,
1639 GtkPrintStatus status,
1640 const gchar *string)
1642 GtkPrintOperationPrivate *priv = op->priv;
1643 static const gchar *status_strs[] = {
1644 NC_("print operation status", "Initial state"),
1645 NC_("print operation status", "Preparing to print"),
1646 NC_("print operation status", "Generating data"),
1647 NC_("print operation status", "Sending data"),
1648 NC_("print operation status", "Waiting"),
1649 NC_("print operation status", "Blocking on issue"),
1650 NC_("print operation status", "Printing"),
1651 NC_("print operation status", "Finished"),
1652 NC_("print operation status", "Finished with error")
1655 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1656 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1659 string = g_dpgettext2 (GETTEXT_PACKAGE, "print operation status", status_strs[status]);
1661 if (priv->status == status &&
1662 strcmp (string, priv->status_string) == 0)
1665 g_free (priv->status_string);
1666 priv->status_string = g_strdup (string);
1667 priv->status = status;
1669 g_object_notify (G_OBJECT (op), "status");
1670 g_object_notify (G_OBJECT (op), "status-string");
1672 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1677 * gtk_print_operation_get_status:
1678 * @op: a #GtkPrintOperation
1680 * Returns the status of the print operation.
1681 * Also see gtk_print_operation_get_status_string().
1683 * Return value: the status of the print operation
1688 gtk_print_operation_get_status (GtkPrintOperation *op)
1690 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1691 GTK_PRINT_STATUS_FINISHED_ABORTED);
1693 return op->priv->status;
1697 * gtk_print_operation_get_status_string:
1698 * @op: a #GtkPrintOperation
1700 * Returns a string representation of the status of the
1701 * print operation. The string is translated and suitable
1702 * for displaying the print status e.g. in a #GtkStatusbar.
1704 * Use gtk_print_operation_get_status() to obtain a status
1705 * value that is suitable for programmatic use.
1707 * Return value: a string representation of the status
1708 * of the print operation
1712 G_CONST_RETURN gchar *
1713 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1715 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1717 return op->priv->status_string;
1721 * gtk_print_operation_is_finished:
1722 * @op: a #GtkPrintOperation
1724 * A convenience function to find out if the print operation
1725 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1726 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1728 * Note: when you enable print status tracking the print operation
1729 * can be in a non-finished state even after done has been called, as
1730 * the operation status then tracks the print job status on the printer.
1732 * Return value: %TRUE, if the print operation is finished.
1737 gtk_print_operation_is_finished (GtkPrintOperation *op)
1739 GtkPrintOperationPrivate *priv;
1741 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1745 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1746 priv->status == GTK_PRINT_STATUS_FINISHED;
1750 * gtk_print_operation_set_show_progress:
1751 * @op: a #GtkPrintOperation
1752 * @show_progress: %TRUE to show a progress dialog
1754 * If @show_progress is %TRUE, the print operation will show a
1755 * progress dialog during the print operation.
1760 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1761 gboolean show_progress)
1763 GtkPrintOperationPrivate *priv;
1765 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1769 show_progress = show_progress != FALSE;
1771 if (priv->show_progress != show_progress)
1773 priv->show_progress = show_progress;
1775 g_object_notify (G_OBJECT (op), "show-progress");
1780 * gtk_print_operation_set_allow_async:
1781 * @op: a #GtkPrintOperation
1782 * @allow_async: %TRUE to allow asynchronous operation
1784 * Sets whether the gtk_print_operation_run() may return
1785 * before the print operation is completed. Note that
1786 * some platforms may not allow asynchronous operation.
1791 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1792 gboolean allow_async)
1794 GtkPrintOperationPrivate *priv;
1796 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1800 allow_async = allow_async != FALSE;
1802 if (priv->allow_async != allow_async)
1804 priv->allow_async = allow_async;
1806 g_object_notify (G_OBJECT (op), "allow-async");
1812 * gtk_print_operation_set_custom_tab_label:
1813 * @op: a #GtkPrintOperation
1814 * @label: the label to use, or %NULL to use the default label
1816 * Sets the label for the tab holding custom widgets.
1821 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1824 GtkPrintOperationPrivate *priv;
1826 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1830 g_free (priv->custom_tab_label);
1831 priv->custom_tab_label = g_strdup (label);
1833 g_object_notify (G_OBJECT (op), "custom-tab-label");
1838 * gtk_print_operation_set_export_filename:
1839 * @op: a #GtkPrintOperation
1840 * @filename: the filename for the exported file
1842 * Sets up the #GtkPrintOperation to generate a file instead
1843 * of showing the print dialog. The indended use of this function
1844 * is for implementing "Export to PDF" actions. Currently, PDF
1845 * is the only supported format.
1847 * "Print to PDF" support is independent of this and is done
1848 * by letting the user pick the "Print to PDF" item from the list
1849 * of printers in the print dialog.
1854 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1855 const gchar *filename)
1857 GtkPrintOperationPrivate *priv;
1859 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1863 g_free (priv->export_filename);
1864 priv->export_filename = g_strdup (filename);
1866 g_object_notify (G_OBJECT (op), "export-filename");
1869 /* Creates the initial page setup used for printing unless the
1870 * app overrides this on a per-page basis using request_page_setup.
1872 * Data is taken from, in order, if existing:
1874 * PrintSettings returned from the print dialog
1875 * (initial dialog values are set from default_page_setup
1876 * if unset in app specified print_settings)
1877 * default_page_setup
1878 * per-locale default setup
1880 static GtkPageSetup *
1881 create_page_setup (GtkPrintOperation *op)
1883 GtkPrintOperationPrivate *priv = op->priv;
1884 GtkPageSetup *page_setup;
1885 GtkPrintSettings *settings;
1887 if (priv->default_page_setup)
1888 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1890 page_setup = gtk_page_setup_new ();
1892 settings = priv->print_settings;
1895 GtkPaperSize *paper_size;
1897 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1898 gtk_page_setup_set_orientation (page_setup,
1899 gtk_print_settings_get_orientation (settings));
1902 paper_size = gtk_print_settings_get_paper_size (settings);
1905 gtk_page_setup_set_paper_size (page_setup, paper_size);
1906 gtk_paper_size_free (paper_size);
1909 /* TODO: Margins? */
1916 pdf_start_page (GtkPrintOperation *op,
1917 GtkPrintContext *print_context,
1918 GtkPageSetup *page_setup)
1920 GtkPaperSize *paper_size;
1921 cairo_surface_t *surface = op->priv->platform_data;
1924 paper_size = gtk_page_setup_get_paper_size (page_setup);
1926 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1927 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1929 cairo_pdf_surface_set_size (surface, w, h);
1933 pdf_end_page (GtkPrintOperation *op,
1934 GtkPrintContext *print_context)
1938 cr = gtk_print_context_get_cairo_context (print_context);
1940 if ((op->priv->manual_number_up < 2) ||
1941 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
1942 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
1943 cairo_show_page (cr);
1947 pdf_end_run (GtkPrintOperation *op,
1951 GtkPrintOperationPrivate *priv = op->priv;
1952 cairo_surface_t *surface = priv->platform_data;
1954 cairo_surface_finish (surface);
1955 cairo_surface_destroy (surface);
1957 priv->platform_data = NULL;
1958 priv->free_platform_data = NULL;
1961 static GtkPrintOperationResult
1962 run_pdf (GtkPrintOperation *op,
1966 GtkPrintOperationPrivate *priv = op->priv;
1967 GtkPageSetup *page_setup;
1968 cairo_surface_t *surface;
1970 gdouble width, height;
1972 priv->print_context = _gtk_print_context_new (op);
1974 page_setup = create_page_setup (op);
1975 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
1977 /* This will be overwritten later by the non-default size, but
1978 we need to pass some size: */
1979 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1980 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1981 g_object_unref (page_setup);
1983 surface = cairo_pdf_surface_create (priv->export_filename,
1985 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
1987 g_set_error_literal (&priv->error,
1989 GTK_PRINT_ERROR_GENERAL,
1990 cairo_status_to_string (cairo_surface_status (surface)));
1992 return GTK_PRINT_OPERATION_RESULT_ERROR;
1995 /* this would crash on a nil surface */
1996 cairo_surface_set_fallback_resolution (surface, 300, 300);
1998 priv->platform_data = surface;
1999 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
2001 cr = cairo_create (surface);
2002 gtk_print_context_set_cairo_context (op->priv->print_context,
2007 priv->print_pages = GTK_PRINT_PAGES_ALL;
2008 priv->page_ranges = NULL;
2009 priv->num_page_ranges = 0;
2011 priv->manual_num_copies = 1;
2012 priv->manual_collation = FALSE;
2013 priv->manual_reverse = FALSE;
2014 priv->manual_page_set = GTK_PAGE_SET_ALL;
2015 priv->manual_scale = 1.0;
2016 priv->manual_orientation = TRUE;
2017 priv->manual_number_up = 1;
2018 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2022 priv->start_page = pdf_start_page;
2023 priv->end_page = pdf_end_page;
2024 priv->end_run = pdf_end_run;
2026 return GTK_PRINT_OPERATION_RESULT_APPLY;
2031 GtkPrintOperation *op;
2032 gint uncollated_copies;
2033 gint collated_copies;
2034 gint uncollated, collated, total;
2036 gint range, num_ranges;
2037 GtkPageRange *ranges;
2038 GtkPageRange one_range;
2042 gint first_position, last_position;
2047 GtkWidget *progress;
2049 gboolean initialized;
2050 gboolean is_preview;
2055 clamp_page_ranges (PrintPagesData *data)
2057 GtkPrintOperationPrivate *priv;
2058 gint num_of_correct_ranges;
2061 priv = data->op->priv;
2063 num_of_correct_ranges = 0;
2065 for (i = 0; i < data->num_ranges; i++)
2066 if ((data->ranges[i].start >= 0) &&
2067 (data->ranges[i].start < priv->nr_of_pages) &&
2068 (data->ranges[i].end >= 0) &&
2069 (data->ranges[i].end < priv->nr_of_pages))
2071 data->ranges[num_of_correct_ranges] = data->ranges[i];
2072 num_of_correct_ranges++;
2074 else if ((data->ranges[i].start >= 0) &&
2075 (data->ranges[i].start < priv->nr_of_pages) &&
2076 (data->ranges[i].end >= priv->nr_of_pages))
2078 data->ranges[i].end = priv->nr_of_pages - 1;
2079 data->ranges[num_of_correct_ranges] = data->ranges[i];
2080 num_of_correct_ranges++;
2082 else if ((data->ranges[i].end >= 0) &&
2083 (data->ranges[i].end < priv->nr_of_pages) &&
2084 (data->ranges[i].start < 0))
2086 data->ranges[i].start = 0;
2087 data->ranges[num_of_correct_ranges] = data->ranges[i];
2088 num_of_correct_ranges++;
2091 data->num_ranges = num_of_correct_ranges;
2095 increment_page_sequence (PrintPagesData *data)
2097 GtkPrintOperationPrivate *priv = data->op->priv;
2100 if (data->total == -1)
2106 /* check whether we reached last position */
2107 if (priv->page_position == data->last_position &&
2108 !(data->collated_copies > 1 && data->collated < (data->collated_copies - 1)))
2110 if (data->uncollated_copies > 1 && data->uncollated < (data->uncollated_copies - 1))
2112 priv->page_position = data->first_position;
2113 data->sheet = data->first_sheet;
2124 if (priv->manual_reverse)
2129 /* changing sheet */
2130 if ((priv->page_position + 1) % priv->manual_number_up == 0 ||
2131 priv->page_position == data->last_position ||
2132 priv->page_position == priv->nr_of_pages_to_print - 1)
2134 /* check whether to print the same sheet again */
2135 if (data->collated_copies > 1)
2137 if (data->collated < (data->collated_copies - 1))
2141 priv->page_position = data->sheet * priv->manual_number_up;
2143 if (priv->page_position < 0 ||
2144 priv->page_position >= priv->nr_of_pages_to_print ||
2146 data->sheet >= data->num_of_sheets)
2152 data->page = data->pages[priv->page_position];
2160 if (priv->manual_page_set == GTK_PAGE_SET_ODD ||
2161 priv->manual_page_set == GTK_PAGE_SET_EVEN)
2162 data->sheet += 2 * inc;
2166 priv->page_position = data->sheet * priv->manual_number_up;
2169 priv->page_position += 1;
2173 if (priv->page_position < 0 ||
2174 priv->page_position >= priv->nr_of_pages_to_print ||
2176 data->sheet >= data->num_of_sheets)
2182 data->page = data->pages[priv->page_position];
2188 print_pages_idle_done (gpointer user_data)
2190 PrintPagesData *data;
2191 GtkPrintOperationPrivate *priv;
2193 data = (PrintPagesData*)user_data;
2194 priv = data->op->priv;
2196 priv->print_pages_idle_id = 0;
2198 if (priv->show_progress_timeout_id > 0)
2200 g_source_remove (priv->show_progress_timeout_id);
2201 priv->show_progress_timeout_id = 0;
2205 gtk_widget_destroy (data->progress);
2207 if (priv->rloop && !data->is_preview)
2208 g_main_loop_quit (priv->rloop);
2210 if (!data->is_preview)
2211 g_signal_emit (data->op, signals[DONE], 0,
2213 GTK_PRINT_OPERATION_RESULT_CANCEL :
2214 GTK_PRINT_OPERATION_RESULT_APPLY);
2216 g_object_unref (data->op);
2217 g_free (data->pages);
2222 update_progress (PrintPagesData *data)
2224 GtkPrintOperationPrivate *priv;
2227 priv = data->op->priv;
2231 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2233 if (priv->nr_of_pages_to_print > 0)
2234 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages_to_print);
2236 text = g_strdup (_("Preparing"));
2238 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2239 text = g_strdup_printf (_("Printing %d"), data->total);
2243 g_object_set (data->progress, "text", text, NULL);
2250 * gtk_print_operation_set_defer_drawing:
2251 * @op: a #GtkPrintOperation
2253 * Sets up the #GtkPrintOperation to wait for calling of
2254 * gtk_print_operation_draw_page_finish() from application. It can
2255 * be used for drawing page in another thread.
2257 * This function must be called in the callback of "draw-page" signal.
2262 gtk_print_operation_set_defer_drawing (GtkPrintOperation *op)
2264 GtkPrintOperationPrivate *priv = op->priv;
2266 g_return_if_fail (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING);
2268 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DEFERRED_DRAWING;
2272 * gtk_print_operation_set_embed_page_setup:
2273 * @op: a #GtkPrintOperation
2274 * @embed: %TRUE to embed page setup selection in the #GtkPrintDialog
2276 * Embed page size combo box and orientation combo box into page setup page.
2277 * Selected page setup is stored as default page setup in #GtkPrintOperation.
2282 gtk_print_operation_set_embed_page_setup (GtkPrintOperation *op,
2285 GtkPrintOperationPrivate *priv;
2287 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2291 embed = embed != FALSE;
2292 if (priv->embed_page_setup != embed)
2294 priv->embed_page_setup = embed;
2295 g_object_notify (G_OBJECT (op), "embed-page-setup");
2300 * gtk_print_operation_get_embed_page_setup:
2301 * @op: a #GtkPrintOperation
2303 * Gets the value of #GtkPrintOperation::embed-page-setup property.
2305 * Returns: whether page setup selection combos are embedded
2310 gtk_print_operation_get_embed_page_setup (GtkPrintOperation *op)
2312 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
2314 return op->priv->embed_page_setup;
2318 * gtk_print_operation_draw_page_finish:
2319 * @op: a #GtkPrintOperation
2321 * Signalize that drawing of particular page is complete.
2323 * It is called after completion of page drawing (e.g. drawing in another
2325 * If gtk_print_operation_set_defer_drawing() was called before, then this function
2326 * has to be called by application. In another case it is called by the library
2332 gtk_print_operation_draw_page_finish (GtkPrintOperation *op)
2334 GtkPrintOperationPrivate *priv = op->priv;
2335 GtkPageSetup *page_setup;
2336 GtkPrintContext *print_context;
2339 print_context = priv->print_context;
2340 page_setup = gtk_print_context_get_page_setup (print_context);
2342 cr = gtk_print_context_get_cairo_context (print_context);
2344 priv->end_page (op, print_context);
2348 g_object_unref (page_setup);
2350 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
2354 common_render_page (GtkPrintOperation *op,
2357 GtkPrintOperationPrivate *priv = op->priv;
2358 GtkPageSetup *page_setup;
2359 GtkPrintContext *print_context;
2362 print_context = priv->print_context;
2364 page_setup = create_page_setup (op);
2366 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2367 print_context, page_nr, page_setup);
2369 _gtk_print_context_set_page_setup (print_context, page_setup);
2371 priv->start_page (op, print_context, page_setup);
2373 cr = gtk_print_context_get_cairo_context (print_context);
2376 if (priv->manual_scale != 1.0 && priv->manual_number_up <= 1)
2379 priv->manual_scale);
2381 if (priv->manual_orientation)
2382 _gtk_print_context_rotate_according_to_orientation (print_context);
2384 if (priv->manual_number_up > 1)
2386 GtkPageOrientation orientation;
2387 GtkPageSetup *page_setup;
2388 gdouble paper_width, paper_height;
2389 gdouble page_width, page_height;
2390 gdouble context_width, context_height;
2391 gdouble bottom_margin, top_margin, left_margin, right_margin;
2392 gdouble x_step, y_step;
2393 gdouble x_scale, y_scale, scale;
2394 gdouble horizontal_offset = 0.0, vertical_offset = 0.0;
2395 gint columns, rows, x, y, tmp_length;
2397 page_setup = gtk_print_context_get_page_setup (print_context);
2398 orientation = gtk_page_setup_get_orientation (page_setup);
2400 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
2401 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
2402 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
2403 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
2405 paper_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2406 paper_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2408 context_width = gtk_print_context_get_width (print_context);
2409 context_height = gtk_print_context_get_height (print_context);
2411 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2412 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2414 page_width = paper_width - (left_margin + right_margin);
2415 page_height = paper_height - (top_margin + bottom_margin);
2419 page_width = paper_width - (top_margin + bottom_margin);
2420 page_height = paper_height - (left_margin + right_margin);
2423 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2424 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2425 cairo_translate (cr, left_margin, top_margin);
2427 cairo_translate (cr, top_margin, left_margin);
2429 switch (priv->manual_number_up)
2457 if (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE ||
2458 orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE)
2460 tmp_length = columns;
2465 switch (priv->manual_number_up_layout)
2467 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
2468 x = priv->page_position % columns;
2469 y = (priv->page_position / columns) % rows;
2471 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
2472 x = priv->page_position % columns;
2473 y = rows - 1 - (priv->page_position / columns) % rows;
2475 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
2476 x = columns - 1 - priv->page_position % columns;
2477 y = (priv->page_position / columns) % rows;
2479 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
2480 x = columns - 1 - priv->page_position % columns;
2481 y = rows - 1 - (priv->page_position / columns) % rows;
2483 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
2484 x = (priv->page_position / rows) % columns;
2485 y = priv->page_position % rows;
2487 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
2488 x = columns - 1 - (priv->page_position / rows) % columns;
2489 y = priv->page_position % rows;
2491 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
2492 x = (priv->page_position / rows) % columns;
2493 y = rows - 1 - priv->page_position % rows;
2495 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
2496 x = columns - 1 - (priv->page_position / rows) % columns;
2497 y = rows - 1 - priv->page_position % rows;
2501 if (priv->manual_number_up == 4 || priv->manual_number_up == 9 || priv->manual_number_up == 16)
2503 x_scale = page_width / (columns * paper_width);
2504 y_scale = page_height / (rows * paper_height);
2506 scale = x_scale < y_scale ? x_scale : y_scale;
2508 x_step = paper_width * (x_scale / scale);
2509 y_step = paper_height * (y_scale / scale);
2511 if ((left_margin + right_margin) > 0)
2513 horizontal_offset = left_margin * (x_step - context_width) / (left_margin + right_margin);
2514 vertical_offset = top_margin * (y_step - context_height) / (top_margin + bottom_margin);
2518 horizontal_offset = (x_step - context_width) / 2.0;
2519 vertical_offset = (y_step - context_height) / 2.0;
2522 cairo_scale (cr, scale, scale);
2524 cairo_translate (cr,
2525 x * x_step + horizontal_offset,
2526 y * y_step + vertical_offset);
2528 if (priv->manual_scale != 1.0)
2529 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2532 if (priv->manual_number_up == 2 || priv->manual_number_up == 6)
2534 x_scale = page_height / (columns * paper_width);
2535 y_scale = page_width / (rows * paper_height);
2537 scale = x_scale < y_scale ? x_scale : y_scale;
2539 horizontal_offset = (paper_width * (x_scale / scale) - paper_width) / 2.0 * columns;
2540 vertical_offset = (paper_height * (y_scale / scale) - paper_height) / 2.0 * rows;
2542 if (!priv->use_full_page)
2544 horizontal_offset -= right_margin;
2545 vertical_offset += top_margin;
2548 cairo_scale (cr, scale, scale);
2550 cairo_translate (cr,
2551 y * paper_height + vertical_offset,
2552 (columns - x) * paper_width + horizontal_offset);
2554 if (priv->manual_scale != 1.0)
2555 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2557 cairo_rotate (cr, - G_PI / 2);
2561 if (!priv->use_full_page)
2562 _gtk_print_context_translate_into_margin (print_context);
2564 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DRAWING;
2566 g_signal_emit (op, signals[DRAW_PAGE], 0,
2567 print_context, page_nr);
2569 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING)
2570 gtk_print_operation_draw_page_finish (op);
2574 prepare_data (PrintPagesData *data)
2576 GtkPrintOperationPrivate *priv;
2577 GtkPageSetup *page_setup;
2580 priv = data->op->priv;
2582 if (!data->initialized)
2584 data->initialized = TRUE;
2585 page_setup = create_page_setup (data->op);
2586 _gtk_print_context_set_page_setup (priv->print_context,
2588 g_object_unref (page_setup);
2590 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2592 if (priv->manual_collation)
2594 data->uncollated_copies = priv->manual_num_copies;
2595 data->collated_copies = 1;
2599 data->uncollated_copies = 1;
2600 data->collated_copies = priv->manual_num_copies;
2606 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2608 gboolean paginated = FALSE;
2610 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2615 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2617 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2619 if (priv->page_ranges == NULL)
2621 g_warning ("no pages to print");
2622 priv->cancelled = TRUE;
2625 data->ranges = priv->page_ranges;
2626 data->num_ranges = priv->num_page_ranges;
2627 for (i = 0; i < data->num_ranges; i++)
2628 if (data->ranges[i].end == -1 ||
2629 data->ranges[i].end >= priv->nr_of_pages)
2630 data->ranges[i].end = priv->nr_of_pages - 1;
2632 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2633 priv->current_page != -1)
2635 data->ranges = &data->one_range;
2636 data->num_ranges = 1;
2637 data->ranges[0].start = priv->current_page;
2638 data->ranges[0].end = priv->current_page;
2642 data->ranges = &data->one_range;
2643 data->num_ranges = 1;
2644 data->ranges[0].start = 0;
2645 data->ranges[0].end = priv->nr_of_pages - 1;
2648 clamp_page_ranges (data);
2650 if (data->num_ranges < 1)
2652 priv->cancelled = TRUE;
2656 priv->nr_of_pages_to_print = 0;
2657 for (i = 0; i < data->num_ranges; i++)
2658 priv->nr_of_pages_to_print += data->ranges[i].end - data->ranges[i].start + 1;
2660 data->pages = g_new (gint, priv->nr_of_pages_to_print);
2662 for (i = 0; i < data->num_ranges; i++)
2663 for (j = data->ranges[i].start; j <= data->ranges[i].end; j++)
2665 data->pages[counter] = j;
2671 data->uncollated = 0;
2673 if (priv->nr_of_pages_to_print % priv->manual_number_up == 0)
2674 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up;
2676 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up + 1;
2678 if (priv->manual_reverse)
2680 /* data->sheet is 0-based */
2681 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2682 data->sheet = (data->num_of_sheets - 1) - (data->num_of_sheets - 1) % 2;
2683 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2684 data->sheet = (data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2);
2686 data->sheet = data->num_of_sheets - 1;
2690 /* data->sheet is 0-based */
2691 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2693 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2695 if (data->num_of_sheets > 1)
2704 priv->page_position = data->sheet * priv->manual_number_up;
2706 if (priv->page_position < 0 || priv->page_position >= priv->nr_of_pages_to_print)
2708 priv->cancelled = TRUE;
2712 data->page = data->pages[priv->page_position];
2713 data->first_position = priv->page_position;
2714 data->first_sheet = data->sheet;
2716 if (priv->manual_reverse)
2718 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2719 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2720 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2721 data->last_position = MIN (2 * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2723 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2727 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2728 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);
2729 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2730 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);
2732 data->last_position = priv->nr_of_pages_to_print - 1;
2736 _gtk_print_operation_set_status (data->op,
2737 GTK_PRINT_STATUS_GENERATING_DATA,
2742 print_pages_idle (gpointer user_data)
2744 PrintPagesData *data;
2745 GtkPrintOperationPrivate *priv;
2746 gboolean done = FALSE;
2748 data = (PrintPagesData*)user_data;
2749 priv = data->op->priv;
2751 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
2753 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2755 prepare_data (data);
2759 if (data->is_preview && !priv->cancelled)
2763 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2767 increment_page_sequence (data);
2770 common_render_page (data->op, data->page);
2772 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
2776 if (priv->cancelled)
2778 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2780 data->is_preview = FALSE;
2784 if (done && !data->is_preview)
2786 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2787 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2790 update_progress (data);
2797 handle_progress_response (GtkWidget *dialog,
2801 GtkPrintOperation *op = (GtkPrintOperation *)data;
2803 gtk_widget_hide (dialog);
2804 gtk_print_operation_cancel (op);
2808 show_progress_timeout (PrintPagesData *data)
2810 gtk_window_present (GTK_WINDOW (data->progress));
2812 data->op->priv->show_progress_timeout_id = 0;
2818 print_pages (GtkPrintOperation *op,
2821 GtkPrintOperationResult result)
2823 GtkPrintOperationPrivate *priv = op->priv;
2824 PrintPagesData *data;
2828 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2829 g_signal_emit (op, signals[DONE], 0, result);
2833 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2835 data = g_new0 (PrintPagesData, 1);
2836 data->op = g_object_ref (op);
2837 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2839 if (priv->show_progress)
2841 GtkWidget *progress;
2843 progress = gtk_message_dialog_new (parent, 0,
2847 g_signal_connect (progress, "response",
2848 G_CALLBACK (handle_progress_response), op);
2850 priv->show_progress_timeout_id =
2851 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2852 (GSourceFunc)show_progress_timeout,
2855 data->progress = progress;
2858 if (data->is_preview)
2862 g_signal_emit_by_name (op, "preview",
2863 GTK_PRINT_OPERATION_PREVIEW (op),
2864 priv->print_context,
2870 GtkWidget *error_dialog;
2872 error_dialog = gtk_message_dialog_new (parent,
2873 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
2876 _("Error creating print preview"));
2878 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
2879 _("The most probable reason is that a temporary file could not be created."));
2881 if (parent && parent->group)
2882 gtk_window_group_add_window (parent->group, GTK_WINDOW (error_dialog));
2884 g_signal_connect (error_dialog, "response",
2885 G_CALLBACK (gtk_widget_destroy), NULL);
2887 gtk_widget_show (error_dialog);
2889 print_pages_idle_done (data);
2894 if (gtk_print_context_get_cairo_context (priv->print_context) == NULL)
2896 /* Programmer error */
2897 g_error ("You must set a cairo context on the print context");
2900 priv->start_page = preview_start_page;
2901 priv->end_page = preview_end_page;
2902 priv->end_run = preview_end_run;
2904 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
2905 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
2906 &priv->num_page_ranges);
2907 priv->manual_num_copies = 1;
2908 priv->manual_collation = FALSE;
2909 priv->manual_reverse = FALSE;
2910 priv->manual_page_set = GTK_PAGE_SET_ALL;
2911 priv->manual_scale = 1.0;
2912 priv->manual_orientation = TRUE;
2913 priv->manual_number_up = 1;
2914 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2917 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
2920 print_pages_idle_done);
2922 /* Recursive main loop to make sure we don't exit on sync operations */
2925 priv->rloop = g_main_loop_new (NULL, FALSE);
2928 GDK_THREADS_LEAVE ();
2929 g_main_loop_run (priv->rloop);
2930 GDK_THREADS_ENTER ();
2932 g_main_loop_unref (priv->rloop);
2934 g_object_unref (op);
2939 * gtk_print_operation_get_error:
2940 * @op: a #GtkPrintOperation
2941 * @error: return location for the error
2943 * Call this when the result of a print operation is
2944 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
2945 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
2946 * handler. The returned #GError will contain more details on what went wrong.
2951 gtk_print_operation_get_error (GtkPrintOperation *op,
2954 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2956 g_propagate_error (error, op->priv->error);
2958 op->priv->error = NULL;
2963 * gtk_print_operation_run:
2964 * @op: a #GtkPrintOperation
2965 * @action: the action to start
2966 * @parent: Transient parent of the dialog, or %NULL
2967 * @error: Return location for errors, or %NULL
2969 * Runs the print operation, by first letting the user modify
2970 * print settings in the print dialog, and then print the document.
2972 * Normally that this function does not return until the rendering of all
2973 * pages is complete. You can connect to the
2974 * #GtkPrintOperation::status-changed signal on @op to obtain some
2975 * information about the progress of the print operation.
2976 * Furthermore, it may use a recursive mainloop to show the print dialog.
2978 * If you call gtk_print_operation_set_allow_async() or set the
2979 * #GtkPrintOperation:allow-async property the operation will run
2980 * asynchronously if this is supported on the platform. The
2981 * #GtkPrintOperation::done signal will be emitted with the result of the
2982 * operation when the it is done (i.e. when the dialog is canceled, or when
2983 * the print succeeds or fails).
2985 * if (settings != NULL)
2986 * gtk_print_operation_set_print_settings (print, settings);
2988 * if (page_setup != NULL)
2989 * gtk_print_operation_set_default_page_setup (print, page_setup);
2991 * g_signal_connect (print, "begin-print",
2992 * G_CALLBACK (begin_print), &data);
2993 * g_signal_connect (print, "draw-page",
2994 * G_CALLBACK (draw_page), &data);
2996 * res = gtk_print_operation_run (print,
2997 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3001 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
3003 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
3004 * GTK_DIALOG_DESTROY_WITH_PARENT,
3005 * GTK_MESSAGE_ERROR,
3006 * GTK_BUTTONS_CLOSE,
3007 * "Error printing file:\n%s",
3009 * g_signal_connect (error_dialog, "response",
3010 * G_CALLBACK (gtk_widget_destroy), NULL);
3011 * gtk_widget_show (error_dialog);
3012 * g_error_free (error);
3014 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
3016 * if (settings != NULL)
3017 * g_object_unref (settings);
3018 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
3022 * Note that gtk_print_operation_run() can only be called once on a
3023 * given #GtkPrintOperation.
3025 * Return value: the result of the print operation. A return value of
3026 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
3027 * completed successfully. In this case, it is a good idea to obtain
3028 * the used print settings with gtk_print_operation_get_print_settings()
3029 * and store them for reuse with the next print operation. A value of
3030 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
3031 * asynchronously, and will emit the #GtkPrintOperation::done signal when
3036 GtkPrintOperationResult
3037 gtk_print_operation_run (GtkPrintOperation *op,
3038 GtkPrintOperationAction action,
3042 GtkPrintOperationPrivate *priv;
3043 GtkPrintOperationResult result;
3044 GtkPageSetup *page_setup;
3046 gboolean run_print_pages;
3048 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
3049 GTK_PRINT_OPERATION_RESULT_ERROR);
3050 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
3051 GTK_PRINT_OPERATION_RESULT_ERROR);
3054 run_print_pages = TRUE;
3057 priv->action = action;
3059 if (priv->print_settings == NULL)
3060 priv->print_settings = gtk_print_settings_new ();
3062 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
3064 /* note: if you implement async EXPORT, update the docs
3065 * docs for the allow-async property.
3067 priv->is_sync = TRUE;
3068 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
3069 result = run_pdf (op, parent, &do_print);
3071 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
3073 priv->is_sync = !priv->allow_async;
3074 priv->print_context = _gtk_print_context_new (op);
3075 page_setup = create_page_setup (op);
3076 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
3077 g_object_unref (page_setup);
3079 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3082 else if (priv->allow_async)
3084 priv->is_sync = FALSE;
3085 _gtk_print_operation_platform_backend_run_dialog_async (op,
3086 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3089 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3090 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
3095 priv->is_sync = TRUE;
3096 result = _gtk_print_operation_platform_backend_run_dialog (op,
3097 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3102 if (run_print_pages)
3103 print_pages (op, parent, do_print, result);
3105 if (priv->error && error)
3106 *error = g_error_copy (priv->error);
3112 * gtk_print_operation_cancel:
3113 * @op: a #GtkPrintOperation
3115 * Cancels a running print operation. This function may
3116 * be called from a #GtkPrintOperation::begin-print,
3117 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
3118 * signal handler to stop the currently running print
3124 gtk_print_operation_cancel (GtkPrintOperation *op)
3126 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3128 op->priv->cancelled = TRUE;
3132 * gtk_print_operation_set_support_selection:
3133 * @op: a #GtkPrintOperation
3134 * @support_selection: %TRUE to support selection
3136 * Sets whether selection is supported by #GtkPrintOperation.
3141 gtk_print_operation_set_support_selection (GtkPrintOperation *op,
3142 gboolean support_selection)
3144 GtkPrintOperationPrivate *priv;
3146 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3150 support_selection = support_selection != FALSE;
3151 if (priv->support_selection != support_selection)
3153 priv->support_selection = support_selection;
3154 g_object_notify (G_OBJECT (op), "support-selection");
3159 * gtk_print_operation_get_support_selection:
3160 * @op: a #GtkPrintOperation
3162 * Gets the value of #GtkPrintOperation::support-selection property.
3164 * Returns: whether the application supports print of selection
3169 gtk_print_operation_get_support_selection (GtkPrintOperation *op)
3171 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3173 return op->priv->support_selection;
3177 * gtk_print_operation_set_has_selection:
3178 * @op: a #GtkPrintOperation
3179 * @has_selection: %TRUE indicates that a selection exists
3181 * Sets whether there is a selection to print.
3183 * Application has to set number of pages to which the selection
3184 * will draw by gtk_print_operation_set_n_pages() in a callback of
3185 * #GtkPrintOperation::begin-print.
3190 gtk_print_operation_set_has_selection (GtkPrintOperation *op,
3191 gboolean has_selection)
3193 GtkPrintOperationPrivate *priv;
3195 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3199 has_selection = has_selection != FALSE;
3200 if (priv->has_selection != has_selection)
3202 priv->has_selection = has_selection;
3203 g_object_notify (G_OBJECT (op), "has-selection");
3208 * gtk_print_operation_get_has_selection:
3209 * @op: a #GtkPrintOperation
3211 * Gets the value of #GtkPrintOperation::has-selection property.
3213 * Returns: whether there is a selection
3218 gtk_print_operation_get_has_selection (GtkPrintOperation *op)
3220 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3222 return op->priv->has_selection;
3226 * gtk_print_operation_get_n_pages_to_print:
3227 * @op: a #GtkPrintOperation
3229 * Returns the number of pages that will be printed.
3231 * Note that this value is set during print preparation phase
3232 * (%GTK_PRINT_STATUS_PREPARING), so this function should never be
3233 * called before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
3234 * You can connect to the #GtkPrintOperation::status-changed signal
3235 * and call gtk_print_operation_get_n_pages_to_print() when
3236 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
3237 * This is typically used to track the progress of print operation.
3239 * Returns: the number of pages that will be printed
3244 gtk_print_operation_get_n_pages_to_print (GtkPrintOperation *op)
3246 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), -1);
3248 return op->priv->nr_of_pages_to_print;
3251 #define __GTK_PRINT_OPERATION_C__
3252 #include "gtkaliasdef.c"