1 /* GTK - The GIMP Toolkit
2 * gtkprintoperation.c: Print Operation
3 * Copyright (C) 2006, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
27 #include "gtkprintoperation-private.h"
28 #include "gtkmarshalers.h"
29 #include <cairo-pdf.h>
31 #include "gtkprivate.h"
32 #include "gtkmessagedialog.h"
35 #define SHOW_PROGRESS_TIME 1200
37 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
57 PROP_DEFAULT_PAGE_SETUP,
63 PROP_TRACK_PRINT_STATUS,
73 static guint signals[LAST_SIGNAL] = { 0 };
74 static int job_nr = 0;
76 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
77 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
78 static void common_render_page (GtkPrintOperation *op,
82 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
83 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
87 * gtk_print_error_quark:
89 * Registers an error quark for #GtkPrintOperation if necessary.
91 * Return value: The error quark used for #GtkPrintOperation errors.
96 gtk_print_error_quark (void)
98 static GQuark quark = 0;
100 quark = g_quark_from_static_string ("gtk-print-error-quark");
105 gtk_print_operation_finalize (GObject *object)
107 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
108 GtkPrintOperationPrivate *priv = print_operation->priv;
110 if (priv->free_platform_data &&
113 priv->free_platform_data (priv->platform_data);
114 priv->free_platform_data = NULL;
117 if (priv->default_page_setup)
118 g_object_unref (priv->default_page_setup);
120 if (priv->print_settings)
121 g_object_unref (priv->print_settings);
123 if (priv->print_context)
124 g_object_unref (priv->print_context);
126 g_free (priv->export_filename);
127 g_free (priv->job_name);
128 g_free (priv->custom_tab_label);
129 g_free (priv->status_string);
131 if (priv->print_pages_idle_id > 0)
132 g_source_remove (priv->print_pages_idle_id);
134 if (priv->show_progress_timeout_id > 0)
135 g_source_remove (priv->show_progress_timeout_id);
138 g_error_free (priv->error);
140 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
144 gtk_print_operation_init (GtkPrintOperation *operation)
146 GtkPrintOperationPrivate *priv;
149 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
151 priv->status = GTK_PRINT_STATUS_INITIAL;
152 priv->status_string = g_strdup ("");
153 priv->default_page_setup = NULL;
154 priv->print_settings = NULL;
155 priv->nr_of_pages = -1;
156 priv->current_page = -1;
157 priv->use_full_page = FALSE;
158 priv->show_progress = FALSE;
159 priv->export_filename = NULL;
160 priv->track_print_status = FALSE;
161 priv->is_sync = FALSE;
164 priv->unit = GTK_UNIT_PIXEL;
166 appname = g_get_application_name ();
169 /* translators: this string is the default job title for print
170 * jobs. %s gets replaced by the application name, %d gets replaced
173 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
177 preview_iface_render_page (GtkPrintOperationPreview *preview,
181 GtkPrintOperation *op;
183 op = GTK_PRINT_OPERATION (preview);
184 common_render_page (op, page_nr);
188 preview_iface_end_preview (GtkPrintOperationPreview *preview)
190 GtkPrintOperation *op;
192 op = GTK_PRINT_OPERATION (preview);
194 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
197 g_main_loop_quit (op->priv->rloop);
199 if (op->priv->end_run)
200 op->priv->end_run (op, op->priv->is_sync, TRUE);
202 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
204 g_signal_emit (op, signals[DONE], 0, GTK_PRINT_OPERATION_RESULT_APPLY);
208 preview_iface_is_selected (GtkPrintOperationPreview *preview,
211 GtkPrintOperation *op;
212 GtkPrintOperationPrivate *priv;
215 op = GTK_PRINT_OPERATION (preview);
218 switch (priv->print_pages)
220 case GTK_PRINT_PAGES_ALL:
221 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
222 case GTK_PRINT_PAGES_CURRENT:
223 return page_nr == priv->current_page;
224 case GTK_PRINT_PAGES_RANGES:
225 for (i = 0; i < priv->num_page_ranges; i++)
227 if (page_nr >= priv->page_ranges[i].start &&
228 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
237 preview_iface_init (GtkPrintOperationPreviewIface *iface)
239 iface->render_page = preview_iface_render_page;
240 iface->end_preview = preview_iface_end_preview;
241 iface->is_selected = preview_iface_is_selected;
245 preview_start_page (GtkPrintOperation *op,
246 GtkPrintContext *print_context,
247 GtkPageSetup *page_setup)
249 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
253 preview_end_page (GtkPrintOperation *op,
254 GtkPrintContext *print_context)
259 preview_end_run (GtkPrintOperation *op,
263 g_free (op->priv->page_ranges);
264 op->priv->page_ranges = NULL;
269 gtk_print_operation_set_property (GObject *object,
274 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
278 case PROP_DEFAULT_PAGE_SETUP:
279 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
281 case PROP_PRINT_SETTINGS:
282 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
285 gtk_print_operation_set_job_name (op, g_value_get_string (value));
288 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
290 case PROP_CURRENT_PAGE:
291 gtk_print_operation_set_current_page (op, g_value_get_int (value));
293 case PROP_USE_FULL_PAGE:
294 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
296 case PROP_TRACK_PRINT_STATUS:
297 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
300 gtk_print_operation_set_unit (op, g_value_get_enum (value));
302 case PROP_ALLOW_ASYNC:
303 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
305 case PROP_SHOW_PROGRESS:
306 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
308 case PROP_EXPORT_FILENAME:
309 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
311 case PROP_CUSTOM_TAB_LABEL:
312 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
315 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
321 gtk_print_operation_get_property (GObject *object,
326 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
327 GtkPrintOperationPrivate *priv = op->priv;
331 case PROP_DEFAULT_PAGE_SETUP:
332 g_value_set_object (value, priv->default_page_setup);
334 case PROP_PRINT_SETTINGS:
335 g_value_set_object (value, priv->print_settings);
338 g_value_set_string (value, priv->job_name);
341 g_value_set_int (value, priv->nr_of_pages);
343 case PROP_CURRENT_PAGE:
344 g_value_set_int (value, priv->current_page);
346 case PROP_USE_FULL_PAGE:
347 g_value_set_boolean (value, priv->use_full_page);
349 case PROP_TRACK_PRINT_STATUS:
350 g_value_set_boolean (value, priv->track_print_status);
353 g_value_set_enum (value, priv->unit);
355 case PROP_ALLOW_ASYNC:
356 g_value_set_boolean (value, priv->allow_async);
358 case PROP_SHOW_PROGRESS:
359 g_value_set_boolean (value, priv->show_progress);
361 case PROP_EXPORT_FILENAME:
362 g_value_set_string (value, priv->export_filename);
365 g_value_set_enum (value, priv->status);
367 case PROP_STATUS_STRING:
368 g_value_set_string (value, priv->status_string);
370 case PROP_CUSTOM_TAB_LABEL:
371 g_value_set_string (value, priv->custom_tab_label);
374 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
381 GtkPrintOperationPreview *preview;
382 GtkPrintContext *print_context;
384 cairo_surface_t *surface;
391 preview_print_idle_done (gpointer data)
393 GtkPrintOperation *op;
394 PreviewOp *pop = (PreviewOp *) data;
396 op = GTK_PRINT_OPERATION (pop->preview);
398 cairo_surface_finish (pop->surface);
399 /* Surface is destroyed in launch_preview */
400 _gtk_print_operation_platform_backend_launch_preview (op,
405 g_free (pop->filename);
407 gtk_print_operation_preview_end_preview (pop->preview);
414 preview_print_idle (gpointer data)
417 GtkPrintOperation *op;
418 gboolean retval = TRUE;
421 pop = (PreviewOp *) data;
422 op = GTK_PRINT_OPERATION (pop->preview);
424 gtk_print_operation_preview_render_page (pop->preview, pop->page_nr);
426 cr = gtk_print_context_get_cairo_context (pop->print_context);
427 _gtk_print_operation_platform_backend_preview_end_page (op, pop->surface, cr);
429 /* TODO: print out sheets not pages and follow ranges */
431 if (op->priv->nr_of_pages <= pop->page_nr)
438 preview_got_page_size (GtkPrintOperationPreview *preview,
439 GtkPrintContext *context,
440 GtkPageSetup *page_setup,
443 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
446 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
448 cr = gtk_print_context_get_cairo_context (pop->print_context);
449 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
454 preview_ready (GtkPrintOperationPreview *preview,
455 GtkPrintContext *context,
459 pop->print_context = context;
461 g_object_ref (preview);
463 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
466 preview_print_idle_done);
471 gtk_print_operation_preview_handler (GtkPrintOperation *op,
472 GtkPrintOperationPreview *preview,
473 GtkPrintContext *context,
476 gdouble dpi_x, dpi_y;
478 GtkPageSetup *page_setup;
481 pop = g_new0 (PreviewOp, 1);
482 pop->filename = NULL;
483 pop->preview = preview;
484 pop->parent = parent;
486 page_setup = gtk_print_context_get_page_setup (context);
489 _gtk_print_operation_platform_backend_create_preview_surface (op,
494 cr = cairo_create (pop->surface);
495 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
499 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
500 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
506 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
512 gtk_print_operation_done (GtkPrintOperation *operation,
513 GtkPrintOperationResult result)
515 GtkPrintOperationPrivate *priv = operation->priv;
517 if (priv->print_context)
519 g_object_unref (priv->print_context);
520 priv->print_context = NULL;
525 custom_widget_accumulator (GSignalInvocationHint *ihint,
527 const GValue *handler_return,
530 gboolean continue_emission;
533 widget = g_value_get_object (handler_return);
535 g_value_set_object (return_accu, widget);
536 continue_emission = (widget == NULL);
538 return continue_emission;
542 gtk_print_operation_class_init (GtkPrintOperationClass *class)
544 GObjectClass *gobject_class = (GObjectClass *)class;
546 gobject_class->set_property = gtk_print_operation_set_property;
547 gobject_class->get_property = gtk_print_operation_get_property;
548 gobject_class->finalize = gtk_print_operation_finalize;
550 class->preview = gtk_print_operation_preview_handler;
551 class->create_custom_widget = gtk_print_operation_create_custom_widget;
552 class->done = gtk_print_operation_done;
554 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
557 * GtkPrintOperation::done:
558 * @operation: the #GtkPrintOperation on which the signal was emitted
559 * @result: the result of the print operation
561 * Emitted when the print operation run has finished doing
562 * everything required for printing. @result gives you information
563 * about what happened during the run. If @result is
564 * %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
565 * gtk_print_operation_get_error() for more information.
567 * If you enabled print status tracking then
568 * gtk_print_operation_is_finished() may still return %FALSE
569 * after #GtkPrintOperation::done was emitted.
574 g_signal_new (I_("done"),
575 G_TYPE_FROM_CLASS (gobject_class),
577 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
579 g_cclosure_marshal_VOID__ENUM,
580 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
583 * GtkPrintOperation::begin-print:
584 * @operation: the #GtkPrintOperation on which the signal was emitted
585 * @context: the #GtkPrintContext for the current operation
587 * Emitted after the user has finished changing print settings
588 * in the dialog, before the actual rendering starts.
590 * A typical use for ::begin-print is to use the parameters from the
591 * #GtkPrintContext and paginate the document accordingly, and then
592 * set the number of pages with gtk_print_operation_set_n_pages().
596 signals[BEGIN_PRINT] =
597 g_signal_new (I_("begin-print"),
598 G_TYPE_FROM_CLASS (gobject_class),
600 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
602 g_cclosure_marshal_VOID__OBJECT,
603 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
606 * GtkPrintOperation::paginate:
607 * @operation: the #GtkPrintOperation on which the signal was emitted
608 * @context: the #GtkPrintContext for the current operation
610 * Emitted after the #GtkPrintOperation::begin-print signal, but before
611 * the actual rendering starts. It keeps getting emitted until it
614 * The ::paginate signal is intended to be used for paginating the document
615 * in small chunks, to avoid blocking the user interface for a long
616 * time. The signal handler should update the number of pages using
617 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
618 * has been completely paginated.
620 * If you don't need to do pagination in chunks, you can simply do
621 * it all in the ::begin-print handler, and set the number of pages
624 * Return value: %TRUE if pagination is complete
629 g_signal_new (I_("paginate"),
630 G_TYPE_FROM_CLASS (gobject_class),
632 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
633 _gtk_boolean_handled_accumulator, NULL,
634 _gtk_marshal_BOOLEAN__OBJECT,
635 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
639 * GtkPrintOperation::request-page-setup:
640 * @operation: the #GtkPrintOperation on which the signal was emitted
641 * @context: the #GtkPrintContext for the current operation
642 * @page_nr: the number of the currently printed page
643 * @setup: the #GtkPageSetup
645 * Emitted once for every page that is printed, to give
646 * the application a chance to modify the page setup. Any changes
647 * done to @setup will be in force only for printing this page.
651 signals[REQUEST_PAGE_SETUP] =
652 g_signal_new (I_("request-page-setup"),
653 G_TYPE_FROM_CLASS (gobject_class),
655 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
657 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
659 GTK_TYPE_PRINT_CONTEXT,
661 GTK_TYPE_PAGE_SETUP);
664 * GtkPrintOperation::draw-page:
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
669 * Emitted for every page that is printed. The signal handler
670 * must render the @page_nr's page onto the cairo context obtained
671 * from @context using gtk_print_context_get_cairo_context().
674 * draw_page (GtkPrintOperation *operation,
675 * GtkPrintContext *context,
677 * gpointer user_data)
680 * PangoLayout *layout;
681 * gdouble width, text_height;
682 * gint layout_height;
683 * PangoFontDescription *desc;
685 * cr = gtk_print_context_get_cairo_context (context);
686 * width = gtk_print_context_get_width (context);
688 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
690 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
693 * layout = gtk_print_context_create_pango_layout (context);
695 * desc = pango_font_description_from_string ("sans 14");
696 * pango_layout_set_font_description (layout, desc);
697 * pango_font_description_free (desc);
699 * pango_layout_set_text (layout, "some text", -1);
700 * pango_layout_set_width (layout, width * PANGO_SCALE);
701 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
703 * pango_layout_get_size (layout, NULL, &layout_height);
704 * text_height = (gdouble)layout_height / PANGO_SCALE;
706 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
707 * pango_cairo_show_layout (cr, layout);
709 * g_object_unref (layout);
713 * Use gtk_print_operation_set_use_full_page() and
714 * gtk_print_operation_set_unit() before starting the print operation
715 * to set up the transformation of the cairo context according to your
721 g_signal_new (I_("draw-page"),
722 G_TYPE_FROM_CLASS (gobject_class),
724 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
726 _gtk_marshal_VOID__OBJECT_INT,
728 GTK_TYPE_PRINT_CONTEXT,
732 * GtkPrintOperation::end-print:
733 * @operation: the #GtkPrintOperation on which the signal was emitted
734 * @context: the #GtkPrintContext for the current operation
736 * Emitted after all pages have been rendered.
737 * A handler for this signal can clean up any resources that have
738 * been allocated in the #GtkPrintOperation::begin-print handler.
743 g_signal_new (I_("end-print"),
744 G_TYPE_FROM_CLASS (gobject_class),
746 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
748 g_cclosure_marshal_VOID__OBJECT,
749 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
752 * GtkPrintOperation::status-changed:
753 * @operation: the #GtkPrintOperation on which the signal was emitted
755 * Emitted at between the various phases of the print operation.
756 * See #GtkPrintStatus for the phases that are being discriminated.
757 * Use gtk_print_operation_get_status() to find out the current
762 signals[STATUS_CHANGED] =
763 g_signal_new (I_("status-changed"),
764 G_TYPE_FROM_CLASS (class),
766 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
768 g_cclosure_marshal_VOID__VOID,
773 * GtkPrintOperation::create-custom-widget:
774 * @operation: the #GtkPrintOperation on which the signal was emitted
776 * Emitted when displaying the print dialog. If you return a
777 * widget in a handler for this signal it will be added to a custom
778 * tab in the print dialog. You typically return a container widget
779 * with multiple widgets in it.
781 * The print dialog owns the returned widget, and its lifetime
782 * isn't controlled by the app. However, the widget is guaranteed
783 * to stay around until the #GtkPrintOperation::custom-widget-apply
784 * signal is emitted on the operation. Then you can read out any
785 * information you need from the widgets.
787 * Returns: A custom widget that gets embedded in the print dialog,
792 signals[CREATE_CUSTOM_WIDGET] =
793 g_signal_new (I_("create-custom-widget"),
794 G_TYPE_FROM_CLASS (class),
796 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
797 custom_widget_accumulator, NULL,
798 _gtk_marshal_OBJECT__VOID,
802 * GtkPrintOperation::custom-widget-apply:
803 * @operation: the #GtkPrintOperation on which the signal was emitted
804 * @widget: the custom widget added in create-custom-widget
806 * Emitted right before #GtkPrintOperation::begin-print if you added
807 * a custom widget in the #GtkPrintOperation:;create-custom-widget handler.
808 * When you get this signal you should read the information from the
809 * custom widgets, as the widgets are not guaraneed to be around at a
814 signals[CUSTOM_WIDGET_APPLY] =
815 g_signal_new (I_("custom-widget-apply"),
816 G_TYPE_FROM_CLASS (class),
818 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
820 g_cclosure_marshal_VOID__OBJECT,
821 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
824 * GtkPrintOperation::preview:
825 * @operation: the #GtkPrintOperation on which the signal was emitted
826 * @preview: the #GtkPrintPreviewOperation for the current operation
827 * @context: the #GtkPrintContext that will be used
828 * @parent: the #GtkWindow to use as window parent, or %NULL
830 * Gets emitted when a preview is requested from the native dialog.
832 * The default handler for this signal uses an external viewer
833 * application to preview.
835 * To implement a custom print preview, an application must return
836 * %TRUE from its handler for this signal. In order to use the
837 * provided @context for the preview implementation, it must be
838 * given a suitable cairo context with gtk_print_context_set_cairo_context().
840 * The custom preview implementation can use
841 * gtk_print_operation_preview_is_selected() and
842 * gtk_print_operation_preview_render_page() to find pages which
843 * are selected for print and render them. The preview must be
844 * finished by calling gtk_print_operation_preview_end_preview()
845 * (typically in response to the user clicking a close button).
847 * Returns: %TRUE if the listener wants to take over control of the preview
852 g_signal_new (I_("preview"),
853 G_TYPE_FROM_CLASS (gobject_class),
855 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
856 _gtk_boolean_handled_accumulator, NULL,
857 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
859 GTK_TYPE_PRINT_OPERATION_PREVIEW,
860 GTK_TYPE_PRINT_CONTEXT,
865 * GtkPrintOperation:default-page-setup:
867 * The #GtkPageSetup used by default.
869 * This page setup will be used by gtk_print_operation_run(),
870 * but it can be overridden on a per-page basis by connecting
871 * to the #GtkPrintOperation::request-page-setup signal.
875 g_object_class_install_property (gobject_class,
876 PROP_DEFAULT_PAGE_SETUP,
877 g_param_spec_object ("default-page-setup",
878 P_("Default Page Setup"),
879 P_("The GtkPageSetup used by default"),
881 GTK_PARAM_READWRITE));
884 * GtkPrintOperation:print-settings:
886 * The #GtkPrintSettings used for initializing the dialog.
888 * Setting this property is typically used to re-establish
889 * print settings from a previous print operation, see
890 * gtk_print_operation_run().
894 g_object_class_install_property (gobject_class,
896 g_param_spec_object ("print-settings",
897 P_("Print Settings"),
898 P_("The GtkPrintSettings used for initializing the dialog"),
899 GTK_TYPE_PRINT_SETTINGS,
900 GTK_PARAM_READWRITE));
903 * GtkPrintOperation:job-name:
905 * A string used to identify the job (e.g. in monitoring
906 * applications like eggcups).
908 * If you don't set a job name, GTK+ picks a default one
909 * by numbering successive print jobs.
913 g_object_class_install_property (gobject_class,
915 g_param_spec_string ("job-name",
917 P_("A string used for identifying the print job."),
919 GTK_PARAM_READWRITE));
922 * GtkPrintOperation:n-pages:
924 * The number of pages in the document.
926 * This <emphasis>must</emphasis> be set to a positive number
927 * before the rendering starts. It may be set in a
928 * #GtkPrintOperation::begin-print signal hander.
930 * Note that the page numbers passed to the
931 * #GtkPrintOperation::request-page-setup and
932 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
933 * the user chooses to print all pages, the last ::draw-page signal
934 * will be for page @n_pages - 1.
938 g_object_class_install_property (gobject_class,
940 g_param_spec_int ("n-pages",
941 P_("Number of Pages"),
942 P_("The number of pages in the document."),
946 GTK_PARAM_READWRITE));
949 * GtkPrintOperation:current-page:
951 * The current page in the document.
953 * If this is set before gtk_print_operation_run(),
954 * the user will be able to select to print only the current page.
956 * Note that this only makes sense for pre-paginated documents.
960 g_object_class_install_property (gobject_class,
962 g_param_spec_int ("current-page",
964 P_("The current page in the document"),
968 GTK_PARAM_READWRITE));
971 * GtkPrintOperation:use-full-page:
973 * If %TRUE, the transformation for the cairo context obtained
974 * from #GtkPrintContext puts the origin at the top left corner
975 * of the page (which may not be the top left corner of the sheet,
976 * depending on page orientation and the number of pages per sheet).
977 * Otherwise, the origin is at the top left corner of the imageable
978 * area (i.e. inside the margins).
982 g_object_class_install_property (gobject_class,
984 g_param_spec_boolean ("use-full-page",
986 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
988 GTK_PARAM_READWRITE));
992 * GtkPrintOperation:track-print-status:
994 * If %TRUE, the print operation will try to continue report on
995 * the status of the print job in the printer queues and printer.
996 * This can allow your application to show things like "out of paper"
997 * issues, and when the print job actually reaches the printer.
998 * However, this is often implemented using polling, and should
999 * not be enabled unless needed.
1003 g_object_class_install_property (gobject_class,
1004 PROP_TRACK_PRINT_STATUS,
1005 g_param_spec_boolean ("track-print-status",
1006 P_("Track Print Status"),
1007 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."),
1009 GTK_PARAM_READWRITE));
1013 * GtkPrintOperation:unit:
1015 * The transformation for the cairo context obtained from
1016 * #GtkPrintContext is set up in such a way that distances
1017 * are measured in units of @unit.
1021 g_object_class_install_property (gobject_class,
1023 g_param_spec_enum ("unit",
1025 P_("The unit in which distances can be measured in the context"),
1028 GTK_PARAM_READWRITE));
1032 * GtkPrintOperation:show-progress:
1034 * Determines whether to show a progress dialog during the
1039 g_object_class_install_property (gobject_class,
1041 g_param_spec_boolean ("show-progress",
1043 P_("TRUE if a progress dialog is shown while printing."),
1045 GTK_PARAM_READWRITE));
1048 * GtkPrintOperation:allow-async:
1050 * Determines whether the print operation may run asynchronously or not.
1052 * Some systems don't support asynchronous printing, but those that do
1053 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1054 * emit the done signal when the operation is actually done.
1056 * The Windows port does not support asynchronous operation
1057 * at all (this is unlikely to change). On other platforms, all actions
1058 * except for %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous
1063 g_object_class_install_property (gobject_class,
1065 g_param_spec_boolean ("allow-async",
1067 P_("TRUE if print process may run asynchronous."),
1069 GTK_PARAM_READWRITE));
1072 * GtkPrintOperation:export-filename:
1074 * The name of a file file to generate instead of showing
1075 * the print dialog. Currently, PDF is the only supported
1078 * The intended use of this property is for implementing
1079 * "Export to PDF" actions.
1081 * "Print to PDF" support is independent of this and is done
1082 * by letting the user pick the "Print to PDF" item from the
1083 * list of printers in the print dialog.
1087 g_object_class_install_property (gobject_class,
1088 PROP_EXPORT_FILENAME,
1089 g_param_spec_string ("export-filename",
1090 P_("Export filename"),
1091 P_("Export filename"),
1093 GTK_PARAM_READWRITE));
1096 * GtkPrintOperation:status:
1098 * The status of the print operation.
1102 g_object_class_install_property (gobject_class,
1104 g_param_spec_enum ("status",
1106 P_("The status of the print operation"),
1107 GTK_TYPE_PRINT_STATUS,
1108 GTK_PRINT_STATUS_INITIAL,
1109 GTK_PARAM_READABLE));
1112 * GtkPrintOperation:status-string:
1114 * A string representation of the status of the print operation.
1115 * The string is translated and suitable for displaying the print
1116 * status e.g. in a #GtkStatusbar.
1118 * See the ::status property for a status value that is suitable
1119 * for programmatic use.
1123 g_object_class_install_property (gobject_class,
1125 g_param_spec_string ("status-string",
1126 P_("Status String"),
1127 P_("A human-readable description of the status"),
1129 GTK_PARAM_READABLE));
1133 * GtkPrintOperation:custom-tab-label:
1135 * Used as the label of the tab containing custom widgets.
1136 * Note that this property may be ignored on some platforms.
1138 * If this is %NULL, GTK+ uses a default label.
1142 g_object_class_install_property (gobject_class,
1143 PROP_CUSTOM_TAB_LABEL,
1144 g_param_spec_string ("custom-tab-label",
1145 P_("Custom tab label"),
1146 P_("Label for the tab containing custom widgets."),
1148 GTK_PARAM_READWRITE));
1153 * gtk_print_operation_new:
1155 * Creates a new #GtkPrintOperation.
1157 * Returns: a new #GtkPrintOperation
1162 gtk_print_operation_new (void)
1164 GtkPrintOperation *print_operation;
1166 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1168 return print_operation;
1172 * gtk_print_operation_set_default_page_setup:
1173 * @op: a #GtkPrintOperation
1174 * @default_page_setup: a #GtkPageSetup, or %NULL
1176 * Makes @default_page_setup the default page setup for @op.
1178 * This page setup will be used by gtk_print_operation_run(),
1179 * but it can be overridden on a per-page basis by connecting
1180 * to the #GtkPrintOperation::request-page-setup signal.
1185 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1186 GtkPageSetup *default_page_setup)
1188 GtkPrintOperationPrivate *priv;
1190 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1191 g_return_if_fail (default_page_setup == NULL ||
1192 GTK_IS_PAGE_SETUP (default_page_setup));
1196 if (default_page_setup != priv->default_page_setup)
1198 if (default_page_setup)
1199 g_object_ref (default_page_setup);
1201 if (priv->default_page_setup)
1202 g_object_unref (priv->default_page_setup);
1204 priv->default_page_setup = default_page_setup;
1206 g_object_notify (G_OBJECT (op), "default-page-setup");
1211 * gtk_print_operation_get_default_page_setup:
1212 * @op: a #GtkPrintOperation
1214 * Returns the default page setup, see
1215 * gtk_print_operation_set_default_page_setup().
1217 * Returns: the default page setup
1222 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1224 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1226 return op->priv->default_page_setup;
1231 * gtk_print_operation_set_print_settings:
1232 * @op: a #GtkPrintOperation
1233 * @print_settings: #GtkPrintSettings, or %NULL
1235 * Sets the print settings for @op. This is typically used to
1236 * re-establish print settings from a previous print operation,
1237 * see gtk_print_operation_run().
1242 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1243 GtkPrintSettings *print_settings)
1245 GtkPrintOperationPrivate *priv;
1247 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1248 g_return_if_fail (print_settings == NULL ||
1249 GTK_IS_PRINT_SETTINGS (print_settings));
1253 if (print_settings != priv->print_settings)
1256 g_object_ref (print_settings);
1258 if (priv->print_settings)
1259 g_object_unref (priv->print_settings);
1261 priv->print_settings = print_settings;
1263 g_object_notify (G_OBJECT (op), "print-settings");
1268 * gtk_print_operation_get_print_settings:
1269 * @op: a #GtkPrintOperation
1271 * Returns the current print settings.
1273 * Note that the return value is %NULL until either
1274 * gtk_print_operation_set_print_settings() or
1275 * gtk_print_operation_run() have been called.
1277 * Return value: the current print settings of @op.
1282 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1284 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1286 return op->priv->print_settings;
1290 * gtk_print_operation_set_job_name:
1291 * @op: a #GtkPrintOperation
1292 * @job_name: a string that identifies the print job
1294 * Sets the name of the print job. The name is used to identify
1295 * the job (e.g. in monitoring applications like eggcups).
1297 * If you don't set a job name, GTK+ picks a default one by
1298 * numbering successive print jobs.
1303 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1304 const gchar *job_name)
1306 GtkPrintOperationPrivate *priv;
1308 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1309 g_return_if_fail (job_name != NULL);
1313 g_free (priv->job_name);
1314 priv->job_name = g_strdup (job_name);
1316 g_object_notify (G_OBJECT (op), "job-name");
1320 * gtk_print_operation_set_n_pages:
1321 * @op: a #GtkPrintOperation
1322 * @n_pages: the number of pages
1324 * Sets the number of pages in the document.
1326 * This <emphasis>must</emphasis> be set to a positive number
1327 * before the rendering starts. It may be set in a
1328 * #GtkPrintOperation::begin-print signal hander.
1330 * Note that the page numbers passed to the
1331 * #GtkPrintOperation::request-page-setup
1332 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1333 * the user chooses to print all pages, the last ::draw-page signal
1334 * will be for page @n_pages - 1.
1339 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1342 GtkPrintOperationPrivate *priv;
1344 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1345 g_return_if_fail (n_pages > 0);
1348 g_return_if_fail (priv->current_page == -1 ||
1349 priv->current_page < n_pages);
1351 if (priv->nr_of_pages != n_pages)
1353 priv->nr_of_pages = n_pages;
1355 g_object_notify (G_OBJECT (op), "n-pages");
1360 * gtk_print_operation_set_current_page:
1361 * @op: a #GtkPrintOperation
1362 * @current_page: the current page, 0-based
1364 * Sets the current page.
1366 * If this is called before gtk_print_operation_run(),
1367 * the user will be able to select to print only the current page.
1369 * Note that this only makes sense for pre-paginated documents.
1374 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1377 GtkPrintOperationPrivate *priv;
1379 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1380 g_return_if_fail (current_page >= 0);
1383 g_return_if_fail (priv->nr_of_pages == -1 ||
1384 current_page < priv->nr_of_pages);
1386 if (priv->current_page != current_page)
1388 priv->current_page = current_page;
1390 g_object_notify (G_OBJECT (op), "current-page");
1395 * gtk_print_operation_set_use_full_page:
1396 * @op: a #GtkPrintOperation
1397 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1399 * If @full_page is %TRUE, the transformation for the cairo context
1400 * obtained from #GtkPrintContext puts the origin at the top left
1401 * corner of the page (which may not be the top left corner of the
1402 * sheet, depending on page orientation and the number of pages per
1403 * sheet). Otherwise, the origin is at the top left corner of the
1404 * imageable area (i.e. inside the margins).
1409 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1412 GtkPrintOperationPrivate *priv;
1414 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1416 full_page = full_page != FALSE;
1420 if (priv->use_full_page != full_page)
1422 priv->use_full_page = full_page;
1424 g_object_notify (G_OBJECT (op), "use-full-page");
1429 * gtk_print_operation_set_unit:
1430 * @op: a #GtkPrintOperation
1431 * @unit: the unit to use
1433 * Sets up the transformation for the cairo context obtained from
1434 * #GtkPrintContext in such a way that distances are measured in
1440 gtk_print_operation_set_unit (GtkPrintOperation *op,
1443 GtkPrintOperationPrivate *priv;
1445 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1449 if (priv->unit != unit)
1453 g_object_notify (G_OBJECT (op), "unit");
1458 * gtk_print_operation_set_track_print_status:
1459 * @op: a #GtkPrintOperation
1460 * @track_status: %TRUE to track status after printing
1462 * If track_status is %TRUE, the print operation will try to continue report
1463 * on the status of the print job in the printer queues and printer. This
1464 * can allow your application to show things like "out of paper" issues,
1465 * and when the print job actually reaches the printer.
1467 * This function is often implemented using some form of polling, so it should
1468 * not be enabled unless needed.
1473 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1474 gboolean track_status)
1476 GtkPrintOperationPrivate *priv;
1478 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1482 if (priv->track_print_status != track_status)
1484 priv->track_print_status = track_status;
1486 g_object_notify (G_OBJECT (op), "track-print-status");
1491 _gtk_print_operation_set_status (GtkPrintOperation *op,
1492 GtkPrintStatus status,
1493 const gchar *string)
1495 GtkPrintOperationPrivate *priv = op->priv;
1496 static const gchar *status_strs[] = {
1497 /* translators, strip the prefix up to and including the first | */
1498 N_("print operation status|Initial state"),
1499 /* translators, strip the prefix up to and including the first | */
1500 N_("print operation status|Preparing to print"),
1501 /* translators, strip the prefix up to and including the first | */
1502 N_("print operation status|Generating data"),
1503 /* translators, strip the prefix up to and including the first | */
1504 N_("print operation status|Sending data"),
1505 /* translators, strip the prefix up to and including the first | */
1506 N_("print operation status|Waiting"),
1507 /* translators, strip the prefix up to and including the first | */
1508 N_("print operation status|Blocking on issue"),
1509 /* translators, strip the prefix up to and including the first | */
1510 N_("print operation status|Printing"),
1511 /* translators, strip the prefix up to and including the first | */
1512 N_("print operation status|Finished"),
1513 /* translators, strip the prefix up to and including the first | */
1514 N_("print operation status|Finished with error")
1517 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1518 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1521 string = g_strip_context (status_strs[status], _(status_strs[status]));
1523 if (priv->status == status &&
1524 strcmp (string, priv->status_string) == 0)
1527 g_free (priv->status_string);
1528 priv->status_string = g_strdup (string);
1529 priv->status = status;
1531 g_object_notify (G_OBJECT (op), "status");
1532 g_object_notify (G_OBJECT (op), "status-string");
1534 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1539 * gtk_print_operation_get_status:
1540 * @op: a #GtkPrintOperation
1542 * Returns the status of the print operation.
1543 * Also see gtk_print_operation_get_status_string().
1545 * Return value: the status of the print operation
1550 gtk_print_operation_get_status (GtkPrintOperation *op)
1552 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1553 GTK_PRINT_STATUS_FINISHED_ABORTED);
1555 return op->priv->status;
1559 * gtk_print_operation_get_status_string:
1560 * @op: a #GtkPrintOperation
1562 * Returns a string representation of the status of the
1563 * print operation. The string is translated and suitable
1564 * for displaying the print status e.g. in a #GtkStatusbar.
1566 * Use gtk_print_operation_get_status() to obtain a status
1567 * value that is suitable for programmatic use.
1569 * Return value: a string representation of the status
1570 * of the print operation
1574 G_CONST_RETURN gchar *
1575 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1577 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1579 return op->priv->status_string;
1583 * gtk_print_operation_is_finished:
1584 * @op: a #GtkPrintOperation
1586 * A convenience function to find out if the print operation
1587 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1588 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1590 * Note: when you enable print status tracking the print operation
1591 * can be in a non-finished state even after done has been called, as
1592 * the operation status then tracks the print job status on the printer.
1594 * Return value: %TRUE, if the print operation is finished.
1599 gtk_print_operation_is_finished (GtkPrintOperation *op)
1601 GtkPrintOperationPrivate *priv;
1603 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1607 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1608 priv->status == GTK_PRINT_STATUS_FINISHED;
1612 * gtk_print_operation_set_show_progress:
1613 * @op: a #GtkPrintOperation
1614 * @show_progress: %TRUE to show a progress dialog
1616 * If @show_progress is %TRUE, the print operation will show a
1617 * progress dialog during the print operation.
1622 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1623 gboolean show_progress)
1625 GtkPrintOperationPrivate *priv;
1627 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1631 show_progress = show_progress != FALSE;
1633 if (priv->show_progress != show_progress)
1635 priv->show_progress = show_progress;
1637 g_object_notify (G_OBJECT (op), "show-progress");
1642 * gtk_print_operation_set_allow_async:
1643 * @op: a #GtkPrintOperation
1644 * @allow_async: %TRUE to allow asynchronous operation
1646 * Sets whether the gtk_print_operation_run() may return
1647 * before the print operation is completed. Note that
1648 * some platforms may not allow asynchronous operation.
1653 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1654 gboolean allow_async)
1656 GtkPrintOperationPrivate *priv;
1658 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1662 allow_async = allow_async != FALSE;
1664 if (priv->allow_async != allow_async)
1666 priv->allow_async = allow_async;
1668 g_object_notify (G_OBJECT (op), "allow-async");
1674 * gtk_print_operation_set_custom_tab_label:
1675 * @op: a #GtkPrintOperation
1676 * @label: the label to use, or %NULL to use the default label
1678 * Sets the label for the tab holding custom widgets.
1683 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1686 GtkPrintOperationPrivate *priv;
1688 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1692 g_free (priv->custom_tab_label);
1693 priv->custom_tab_label = g_strdup (label);
1695 g_object_notify (G_OBJECT (op), "custom-tab-label");
1700 * gtk_print_operation_set_export_filename:
1701 * @op: a #GtkPrintOperation
1702 * @filename: the filename for the exported file
1704 * Sets up the #GtkPrintOperation to generate a file instead
1705 * of showing the print dialog. The indended use of this function
1706 * is for implementing "Export to PDF" actions. Currently, PDF
1707 * is the only supported format.
1709 * "Print to PDF" support is independent of this and is done
1710 * by letting the user pick the "Print to PDF" item from the list
1711 * of printers in the print dialog.
1716 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1717 const gchar *filename)
1719 GtkPrintOperationPrivate *priv;
1721 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1725 g_free (priv->export_filename);
1726 priv->export_filename = g_strdup (filename);
1728 g_object_notify (G_OBJECT (op), "export-filename");
1731 /* Creates the initial page setup used for printing unless the
1732 * app overrides this on a per-page basis using request_page_setup.
1734 * Data is taken from, in order, if existing:
1736 * PrintSettings returned from the print dialog
1737 * (initial dialog values are set from default_page_setup
1738 * if unset in app specified print_settings)
1739 * default_page_setup
1740 * per-locale default setup
1742 static GtkPageSetup *
1743 create_page_setup (GtkPrintOperation *op)
1745 GtkPrintOperationPrivate *priv = op->priv;
1746 GtkPageSetup *page_setup;
1747 GtkPrintSettings *settings;
1749 if (priv->default_page_setup)
1750 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1752 page_setup = gtk_page_setup_new ();
1754 settings = priv->print_settings;
1757 GtkPaperSize *paper_size;
1759 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1760 gtk_page_setup_set_orientation (page_setup,
1761 gtk_print_settings_get_orientation (settings));
1764 paper_size = gtk_print_settings_get_paper_size (settings);
1767 gtk_page_setup_set_paper_size (page_setup, paper_size);
1768 gtk_paper_size_free (paper_size);
1771 /* TODO: Margins? */
1778 pdf_start_page (GtkPrintOperation *op,
1779 GtkPrintContext *print_context,
1780 GtkPageSetup *page_setup)
1782 GtkPaperSize *paper_size;
1783 cairo_surface_t *surface = op->priv->platform_data;
1786 paper_size = gtk_page_setup_get_paper_size (page_setup);
1788 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1789 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1791 cairo_pdf_surface_set_size (surface, w, h);
1795 pdf_end_page (GtkPrintOperation *op,
1796 GtkPrintContext *print_context)
1800 cr = gtk_print_context_get_cairo_context (print_context);
1801 cairo_show_page (cr);
1805 pdf_end_run (GtkPrintOperation *op,
1809 GtkPrintOperationPrivate *priv = op->priv;
1810 cairo_surface_t *surface = priv->platform_data;
1812 cairo_surface_finish (surface);
1813 cairo_surface_destroy (surface);
1815 priv->platform_data = NULL;
1816 priv->free_platform_data = NULL;
1819 static GtkPrintOperationResult
1820 run_pdf (GtkPrintOperation *op,
1824 GtkPrintOperationPrivate *priv = op->priv;
1825 GtkPageSetup *page_setup;
1826 cairo_surface_t *surface;
1828 gdouble width, height;
1830 priv->print_context = _gtk_print_context_new (op);
1832 page_setup = create_page_setup (op);
1833 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
1835 /* This will be overwritten later by the non-default size, but
1836 we need to pass some size: */
1837 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1838 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1839 g_object_unref (page_setup);
1841 surface = cairo_pdf_surface_create (priv->export_filename,
1843 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
1845 g_set_error (&priv->error,
1847 GTK_PRINT_ERROR_GENERAL,
1848 cairo_status_to_string (cairo_surface_status (surface)));
1850 return GTK_PRINT_OPERATION_RESULT_ERROR;
1853 /* this would crash on a nil surface */
1854 cairo_surface_set_fallback_resolution (surface, 300, 300);
1856 priv->platform_data = surface;
1857 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
1859 cr = cairo_create (surface);
1860 gtk_print_context_set_cairo_context (op->priv->print_context,
1865 priv->print_pages = GTK_PRINT_PAGES_ALL;
1866 priv->page_ranges = NULL;
1867 priv->num_page_ranges = 0;
1869 priv->manual_num_copies = 1;
1870 priv->manual_collation = FALSE;
1871 priv->manual_reverse = FALSE;
1872 priv->manual_page_set = GTK_PAGE_SET_ALL;
1873 priv->manual_scale = 1.0;
1874 priv->manual_orientation = TRUE;
1878 priv->start_page = pdf_start_page;
1879 priv->end_page = pdf_end_page;
1880 priv->end_run = pdf_end_run;
1882 return GTK_PRINT_OPERATION_RESULT_APPLY;
1887 GtkPrintOperation *op;
1888 gint uncollated_copies;
1889 gint collated_copies;
1890 gint uncollated, collated, total;
1892 gint range, num_ranges;
1893 GtkPageRange *ranges;
1894 GtkPageRange one_range;
1896 gint page, start, end, inc;
1898 GtkWidget *progress;
1900 gboolean initialized;
1901 gboolean is_preview;
1905 find_range (PrintPagesData *data)
1907 GtkPageRange *range;
1909 range = &data->ranges[data->range];
1913 data->start = range->end;
1914 data->end = range->start - 1;
1918 data->start = range->start;
1919 data->end = range->end + 1;
1924 increment_page_sequence (PrintPagesData *data)
1926 GtkPrintOperationPrivate *priv = data->op->priv;
1929 data->page += data->inc;
1930 if (data->page == data->end)
1932 data->range += data->inc;
1933 if (data->range == -1 || data->range == data->num_ranges)
1936 if (data->uncollated == data->uncollated_copies)
1939 data->range = data->inc < 0 ? data->num_ranges - 1 : 0;
1942 data->page = data->start;
1945 while ((priv->manual_page_set == GTK_PAGE_SET_EVEN && data->page % 2 == 0) ||
1946 (priv->manual_page_set == GTK_PAGE_SET_ODD && data->page % 2 == 1));
1952 print_pages_idle_done (gpointer user_data)
1954 PrintPagesData *data;
1955 GtkPrintOperationPrivate *priv;
1957 data = (PrintPagesData*)user_data;
1958 priv = data->op->priv;
1960 priv->print_pages_idle_id = 0;
1962 if (priv->show_progress_timeout_id > 0)
1964 g_source_remove (priv->show_progress_timeout_id);
1965 priv->show_progress_timeout_id = 0;
1969 gtk_widget_destroy (data->progress);
1971 if (priv->rloop && !data->is_preview)
1972 g_main_loop_quit (priv->rloop);
1974 if (!data->is_preview)
1975 g_signal_emit (data->op, signals[DONE], 0,
1977 GTK_PRINT_OPERATION_RESULT_CANCEL :
1978 GTK_PRINT_OPERATION_RESULT_APPLY);
1980 g_object_unref (data->op);
1985 update_progress (PrintPagesData *data)
1987 GtkPrintOperationPrivate *priv;
1990 priv = data->op->priv;
1994 if (priv->status == GTK_PRINT_STATUS_PREPARING)
1996 if (priv->nr_of_pages > 0)
1997 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages);
1999 text = g_strdup (_("Preparing"));
2001 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2002 text = g_strdup_printf (_("Printing %d"), data->total);
2006 g_object_set (data->progress, "text", text, NULL);
2013 common_render_page (GtkPrintOperation *op,
2016 GtkPrintOperationPrivate *priv = op->priv;
2017 GtkPageSetup *page_setup;
2018 GtkPrintContext *print_context;
2021 print_context = priv->print_context;
2023 page_setup = create_page_setup (op);
2025 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2026 print_context, page_nr, page_setup);
2028 _gtk_print_context_set_page_setup (print_context, page_setup);
2030 priv->start_page (op, print_context, page_setup);
2032 cr = gtk_print_context_get_cairo_context (print_context);
2035 if (priv->manual_scale != 1.0)
2038 priv->manual_scale);
2040 if (priv->manual_orientation)
2041 _gtk_print_context_rotate_according_to_orientation (print_context);
2043 if (!priv->use_full_page)
2044 _gtk_print_context_translate_into_margin (print_context);
2046 g_signal_emit (op, signals[DRAW_PAGE], 0,
2047 print_context, page_nr);
2049 priv->end_page (op, print_context);
2053 g_object_unref (page_setup);
2057 print_pages_idle (gpointer user_data)
2059 PrintPagesData *data;
2060 GtkPrintOperationPrivate *priv;
2061 GtkPageSetup *page_setup;
2062 gboolean done = FALSE;
2065 data = (PrintPagesData*)user_data;
2066 priv = data->op->priv;
2068 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2070 if (!data->initialized)
2072 data->initialized = TRUE;
2073 page_setup = create_page_setup (data->op);
2074 _gtk_print_context_set_page_setup (priv->print_context,
2076 g_object_unref (page_setup);
2078 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2080 if (priv->manual_collation)
2082 data->uncollated_copies = priv->manual_num_copies;
2083 data->collated_copies = 1;
2087 data->uncollated_copies = 1;
2088 data->collated_copies = priv->manual_num_copies;
2094 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2096 gboolean paginated = FALSE;
2098 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2103 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2105 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2107 if (priv->page_ranges == NULL)
2109 g_warning ("no pages to print");
2110 priv->cancelled = TRUE;
2113 data->ranges = priv->page_ranges;
2114 data->num_ranges = priv->num_page_ranges;
2115 for (i = 0; i < data->num_ranges; i++)
2116 if (data->ranges[i].end == -1 ||
2117 data->ranges[i].end >= priv->nr_of_pages)
2118 data->ranges[i].end = priv->nr_of_pages - 1;
2120 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2121 priv->current_page != -1)
2123 data->ranges = &data->one_range;
2124 data->num_ranges = 1;
2125 data->ranges[0].start = priv->current_page;
2126 data->ranges[0].end = priv->current_page;
2130 data->ranges = &data->one_range;
2131 data->num_ranges = 1;
2132 data->ranges[0].start = 0;
2133 data->ranges[0].end = priv->nr_of_pages - 1;
2136 if (priv->manual_reverse)
2138 data->range = data->num_ranges - 1;
2148 /* go back one page, since we preincrement below */
2149 data->page = data->start - data->inc;
2150 data->collated = data->collated_copies - 1;
2152 _gtk_print_operation_set_status (data->op,
2153 GTK_PRINT_STATUS_GENERATING_DATA,
2161 if (data->collated == data->collated_copies)
2164 if (!increment_page_sequence (data))
2172 if (data->is_preview && !priv->cancelled)
2176 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2180 common_render_page (data->op, data->page);
2184 if (priv->cancelled)
2186 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2188 data->is_preview = FALSE;
2192 if (done && !data->is_preview)
2194 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2195 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2198 update_progress (data);
2204 handle_progress_response (GtkWidget *dialog,
2208 GtkPrintOperation *op = (GtkPrintOperation *)data;
2210 gtk_widget_hide (dialog);
2211 gtk_print_operation_cancel (op);
2215 show_progress_timeout (PrintPagesData *data)
2217 gtk_window_present (GTK_WINDOW (data->progress));
2219 data->op->priv->show_progress_timeout_id = 0;
2225 print_pages (GtkPrintOperation *op,
2228 GtkPrintOperationResult result)
2230 GtkPrintOperationPrivate *priv = op->priv;
2231 PrintPagesData *data;
2235 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2236 g_signal_emit (op, signals[DONE], 0, result);
2240 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2242 data = g_new0 (PrintPagesData, 1);
2243 data->op = g_object_ref (op);
2244 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2246 if (priv->show_progress)
2248 GtkWidget *progress;
2250 progress = gtk_message_dialog_new (parent, 0,
2254 g_signal_connect (progress, "response",
2255 G_CALLBACK (handle_progress_response), op);
2257 priv->show_progress_timeout_id =
2258 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2259 (GSourceFunc)show_progress_timeout,
2262 data->progress = progress;
2265 if (data->is_preview)
2269 g_signal_emit_by_name (op, "preview",
2270 GTK_PRINT_OPERATION_PREVIEW (op),
2271 priv->print_context,
2276 gtk_print_context_get_cairo_context (priv->print_context) == NULL)
2278 /* Programmer error */
2279 g_error ("You must set a cairo context on the print context");
2282 priv->start_page = preview_start_page;
2283 priv->end_page = preview_end_page;
2284 priv->end_run = preview_end_run;
2286 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
2287 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
2288 &priv->num_page_ranges);
2289 priv->manual_num_copies = 1;
2290 priv->manual_collation = FALSE;
2291 priv->manual_reverse = FALSE;
2292 priv->manual_page_set = GTK_PAGE_SET_ALL;
2293 priv->manual_scale = 1.0;
2294 priv->manual_orientation = TRUE;
2297 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
2300 print_pages_idle_done);
2302 /* Recursive main loop to make sure we don't exit on sync operations */
2305 priv->rloop = g_main_loop_new (NULL, FALSE);
2308 GDK_THREADS_LEAVE ();
2309 g_main_loop_run (priv->rloop);
2310 GDK_THREADS_ENTER ();
2312 g_main_loop_unref (priv->rloop);
2314 g_object_unref (op);
2319 * gtk_print_operation_get_error:
2320 * @op: a #GtkPrintOperation
2321 * @error: return location for the error
2323 * Call this when the result of a print operation is
2324 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
2325 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
2326 * handler. The returned #GError will contain more details on what went wrong.
2331 gtk_print_operation_get_error (GtkPrintOperation *op,
2334 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2336 g_propagate_error (error, op->priv->error);
2338 op->priv->error = NULL;
2343 * gtk_print_operation_run:
2344 * @op: a #GtkPrintOperation
2345 * @action: the action to start
2346 * @parent: Transient parent of the dialog, or %NULL
2347 * @error: Return location for errors, or %NULL
2349 * Runs the print operation, by first letting the user modify
2350 * print settings in the print dialog, and then print the document.
2352 * Normally that this function does not return until the rendering of all
2353 * pages is complete. You can connect to the
2354 * #GtkPrintOperation::status-changed signal on @op to obtain some
2355 * information about the progress of the print operation.
2356 * Furthermore, it may use a recursive mainloop to show the print dialog.
2358 * If you call gtk_print_operation_set_allow_async() or set the allow-async
2359 * property the operation will run asyncronously if this is supported on the
2360 * platform. The #GtkPrintOperation::done signal will be emitted with the
2361 * operation results when the operation is done (i.e. when the dialog is
2362 * canceled, or when the print succeeds or fails).
2364 * if (settings != NULL)
2365 * gtk_print_operation_set_print_settings (print, settings);
2367 * if (page_setup != NULL)
2368 * gtk_print_operation_set_default_page_setup (print, page_setup);
2370 * g_signal_connect (print, "begin-print",
2371 * G_CALLBACK (begin_print), &data);
2372 * g_signal_connect (print, "draw-page",
2373 * G_CALLBACK (draw_page), &data);
2375 * res = gtk_print_operation_run (print,
2376 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2380 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
2382 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
2383 * GTK_DIALOG_DESTROY_WITH_PARENT,
2384 * GTK_MESSAGE_ERROR,
2385 * GTK_BUTTONS_CLOSE,
2386 * "Error printing file:\n%s",
2388 * g_signal_connect (error_dialog, "response",
2389 * G_CALLBACK (gtk_widget_destroy), NULL);
2390 * gtk_widget_show (error_dialog);
2391 * g_error_free (error);
2393 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
2395 * if (settings != NULL)
2396 * g_object_unref (settings);
2397 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
2401 * Note that gtk_print_operation_run() can only be called once on a
2402 * given #GtkPrintOperation.
2404 * Return value: the result of the print operation. A return value of
2405 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
2406 * completed successfully. In this case, it is a good idea to obtain
2407 * the used print settings with gtk_print_operation_get_print_settings()
2408 * and store them for reuse with the next print operation. A value of
2409 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
2410 * asynchronously, and will emit the ::done signal when done.
2414 GtkPrintOperationResult
2415 gtk_print_operation_run (GtkPrintOperation *op,
2416 GtkPrintOperationAction action,
2420 GtkPrintOperationPrivate *priv;
2421 GtkPrintOperationResult result;
2422 GtkPageSetup *page_setup;
2424 gboolean run_print_pages;
2426 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
2427 GTK_PRINT_OPERATION_RESULT_ERROR);
2428 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
2429 GTK_PRINT_OPERATION_RESULT_ERROR);
2432 run_print_pages = TRUE;
2435 priv->action = action;
2437 if (priv->print_settings == NULL)
2438 priv->print_settings = gtk_print_settings_new ();
2440 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
2442 /* note: if you implement async EXPORT, update the docs
2443 * docs for the allow-async property.
2445 priv->is_sync = TRUE;
2446 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
2447 result = run_pdf (op, parent, &do_print);
2449 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
2451 priv->is_sync = !priv->allow_async;
2452 priv->print_context = _gtk_print_context_new (op);
2453 page_setup = create_page_setup (op);
2454 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2455 g_object_unref (page_setup);
2457 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2460 else if (priv->allow_async)
2462 priv->is_sync = FALSE;
2463 _gtk_print_operation_platform_backend_run_dialog_async (op,
2464 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2467 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2468 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
2473 priv->is_sync = TRUE;
2474 result = _gtk_print_operation_platform_backend_run_dialog (op,
2475 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2480 if (run_print_pages)
2481 print_pages (op, parent, do_print, result);
2483 if (priv->error && error)
2484 *error = g_error_copy (priv->error);
2490 * gtk_print_operation_cancel:
2491 * @op: a #GtkPrintOperation
2493 * Cancels a running print operation. This function may
2494 * be called from a #GtkPrintOperation::begin-print,
2495 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
2496 * signal handler to stop the currently running print
2502 gtk_print_operation_cancel (GtkPrintOperation *op)
2504 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2506 op->priv->cancelled = TRUE;
2511 #define __GTK_PRINT_OPERATION_C__
2512 #include "gtkaliasdef.c"