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 g_free (priv->export_filename);
124 g_free (priv->job_name);
125 g_free (priv->custom_tab_label);
127 if (priv->print_pages_idle_id > 0)
128 g_source_remove (priv->print_pages_idle_id);
130 if (priv->show_progress_timeout_id > 0)
131 g_source_remove (priv->show_progress_timeout_id);
134 g_error_free (priv->error);
136 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
140 gtk_print_operation_init (GtkPrintOperation *operation)
142 GtkPrintOperationPrivate *priv;
145 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
147 priv->status = GTK_PRINT_STATUS_INITIAL;
148 priv->status_string = g_strdup ("");
149 priv->default_page_setup = NULL;
150 priv->print_settings = NULL;
151 priv->nr_of_pages = -1;
152 priv->current_page = -1;
153 priv->use_full_page = FALSE;
154 priv->show_progress = FALSE;
155 priv->export_filename = NULL;
156 priv->track_print_status = FALSE;
157 priv->is_sync = FALSE;
160 priv->unit = GTK_UNIT_PIXEL;
162 appname = g_get_application_name ();
163 priv->job_name = g_strdup_printf ("%s job #%d", appname, ++job_nr);
167 preview_iface_render_page (GtkPrintOperationPreview *preview,
171 GtkPrintOperation *op;
173 op = GTK_PRINT_OPERATION (preview);
174 common_render_page (op, page_nr);
178 preview_iface_end_preview (GtkPrintOperationPreview *preview)
180 GtkPrintOperation *op;
182 op = GTK_PRINT_OPERATION (preview);
184 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
187 g_main_loop_quit (op->priv->rloop);
189 op->priv->end_run (op, op->priv->is_sync, TRUE);
191 g_signal_emit (op, signals[DONE], 0,
192 GTK_PRINT_OPERATION_RESULT_APPLY);
196 preview_iface_is_selected (GtkPrintOperationPreview *preview,
199 GtkPrintOperation *op;
200 GtkPrintOperationPrivate *priv;
203 op = GTK_PRINT_OPERATION (preview);
206 switch (priv->print_pages)
208 case GTK_PRINT_PAGES_ALL:
209 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
210 case GTK_PRINT_PAGES_CURRENT:
211 return page_nr == priv->current_page;
212 case GTK_PRINT_PAGES_RANGES:
213 for (i = 0; i < priv->num_page_ranges; i++)
215 if (page_nr >= priv->page_ranges[i].start &&
216 page_nr <= priv->page_ranges[i].end)
225 preview_iface_init (GtkPrintOperationPreviewIface *iface)
227 iface->render_page = preview_iface_render_page;
228 iface->end_preview = preview_iface_end_preview;
229 iface->is_selected = preview_iface_is_selected;
233 preview_start_page (GtkPrintOperation *op,
234 GtkPrintContext *print_context,
235 GtkPageSetup *page_setup)
237 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
241 preview_end_page (GtkPrintOperation *op,
242 GtkPrintContext *print_context)
247 preview_end_run (GtkPrintOperation *op,
251 g_free (op->priv->page_ranges);
252 op->priv->page_ranges = NULL;
254 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
259 gtk_print_operation_set_property (GObject *object,
264 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
268 case PROP_DEFAULT_PAGE_SETUP:
269 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
271 case PROP_PRINT_SETTINGS:
272 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
275 gtk_print_operation_set_job_name (op, g_value_get_string (value));
278 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
280 case PROP_CURRENT_PAGE:
281 gtk_print_operation_set_current_page (op, g_value_get_int (value));
283 case PROP_USE_FULL_PAGE:
284 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
286 case PROP_TRACK_PRINT_STATUS:
287 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
290 gtk_print_operation_set_unit (op, g_value_get_enum (value));
292 case PROP_ALLOW_ASYNC:
293 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
295 case PROP_SHOW_PROGRESS:
296 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
298 case PROP_EXPORT_FILENAME:
299 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
301 case PROP_CUSTOM_TAB_LABEL:
302 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
305 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
311 gtk_print_operation_get_property (GObject *object,
316 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
317 GtkPrintOperationPrivate *priv = op->priv;
321 case PROP_DEFAULT_PAGE_SETUP:
322 g_value_set_object (value, priv->default_page_setup);
324 case PROP_PRINT_SETTINGS:
325 g_value_set_object (value, priv->print_settings);
328 g_value_set_string (value, priv->job_name);
331 g_value_set_int (value, priv->nr_of_pages);
333 case PROP_CURRENT_PAGE:
334 g_value_set_int (value, priv->current_page);
336 case PROP_USE_FULL_PAGE:
337 g_value_set_boolean (value, priv->use_full_page);
339 case PROP_TRACK_PRINT_STATUS:
340 g_value_set_boolean (value, priv->track_print_status);
343 g_value_set_enum (value, priv->unit);
345 case PROP_ALLOW_ASYNC:
346 g_value_set_boolean (value, priv->allow_async);
348 case PROP_SHOW_PROGRESS:
349 g_value_set_boolean (value, priv->show_progress);
351 case PROP_EXPORT_FILENAME:
352 g_value_set_string (value, priv->export_filename);
355 g_value_set_enum (value, priv->status);
357 case PROP_STATUS_STRING:
358 g_value_set_string (value, priv->status_string);
360 case PROP_CUSTOM_TAB_LABEL:
361 g_value_set_string (value, priv->custom_tab_label);
364 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
371 GtkPrintOperationPreview *preview;
372 GtkPrintContext *print_context;
374 cairo_surface_t *surface;
381 preview_print_idle_done (gpointer data)
383 GtkPrintOperation *op;
384 PreviewOp *pop = (PreviewOp *) data;
386 GDK_THREADS_ENTER ();
388 op = GTK_PRINT_OPERATION (pop->preview);
390 cairo_surface_finish (pop->surface);
391 /* Surface is destroyed in launch_preview */
392 _gtk_print_operation_platform_backend_launch_preview (op,
397 g_free (pop->filename);
399 gtk_print_operation_preview_end_preview (pop->preview);
402 GDK_THREADS_LEAVE ();
406 preview_print_idle (gpointer data)
409 GtkPrintOperation *op;
410 gboolean retval = TRUE;
413 GDK_THREADS_ENTER ();
415 pop = (PreviewOp *) data;
416 op = GTK_PRINT_OPERATION (pop->preview);
418 gtk_print_operation_preview_render_page (pop->preview, pop->page_nr);
420 cr = gtk_print_context_get_cairo_context (pop->print_context);
421 _gtk_print_operation_platform_backend_preview_end_page (op, pop->surface, cr);
423 /* TODO: print out sheets not pages and follow ranges */
425 if (op->priv->nr_of_pages <= pop->page_nr)
428 GDK_THREADS_LEAVE ();
434 preview_got_page_size (GtkPrintOperationPreview *preview,
435 GtkPrintContext *context,
436 GtkPageSetup *page_setup,
439 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
442 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
444 cr = gtk_print_context_get_cairo_context (pop->print_context);
445 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
450 preview_ready (GtkPrintOperationPreview *preview,
451 GtkPrintContext *context,
455 pop->print_context = context;
457 g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
460 preview_print_idle_done);
465 gtk_print_operation_preview_handler (GtkPrintOperation *op,
466 GtkPrintOperationPreview *preview,
467 GtkPrintContext *context,
470 gdouble dpi_x, dpi_y;
472 GtkPageSetup *page_setup;
475 pop = g_new0 (PreviewOp, 1);
476 pop->filename = NULL;
477 pop->preview = preview;
478 pop->parent = parent;
480 page_setup = gtk_print_context_get_page_setup (context);
483 _gtk_print_operation_platform_backend_create_preview_surface (op,
488 cr = cairo_create (pop->surface);
489 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
493 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
494 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
500 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
506 custom_widget_accumulator (GSignalInvocationHint *ihint,
508 const GValue *handler_return,
511 gboolean continue_emission;
514 widget = g_value_get_object (handler_return);
516 g_value_set_object (return_accu, widget);
517 continue_emission = (widget == NULL);
519 return continue_emission;
523 gtk_print_operation_class_init (GtkPrintOperationClass *class)
525 GObjectClass *gobject_class = (GObjectClass *)class;
527 gobject_class->set_property = gtk_print_operation_set_property;
528 gobject_class->get_property = gtk_print_operation_get_property;
529 gobject_class->finalize = gtk_print_operation_finalize;
531 class->preview = gtk_print_operation_preview_handler;
532 class->create_custom_widget = gtk_print_operation_create_custom_widget;
534 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
537 * GtkPrintOperation::done:
538 * @operation: the #GtkPrintOperation on which the signal was emitted
539 * @result: the result of the print operation
541 * Emitted when the print operation run has finished doing
542 * everything required for printing. @result gives you information
543 * about what happened during the run. If @result is
544 * %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
545 * gtk_print_operation_get_error() for more information.
547 * If you enabled print status tracking then
548 * gtk_print_operation_is_finished() may still return %FALSE
549 * after this was emitted.
554 g_signal_new (I_("done"),
555 G_TYPE_FROM_CLASS (gobject_class),
557 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
559 g_cclosure_marshal_VOID__ENUM,
560 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
563 * GtkPrintOperation::begin-print:
564 * @operation: the #GtkPrintOperation on which the signal was emitted
565 * @context: the #GtkPrintContext for the current operation
567 * Emitted after the user has finished changing print settings
568 * in the dialog, before the actual rendering starts.
570 * A typical use for this signal is to use the parameters from the
571 * #GtkPrintContext and paginate the document accordingly, and then
572 * set the number of pages with gtk_print_operation_set_n_pages().
576 signals[BEGIN_PRINT] =
577 g_signal_new (I_("begin-print"),
578 G_TYPE_FROM_CLASS (gobject_class),
580 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
582 g_cclosure_marshal_VOID__OBJECT,
583 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
586 * GtkPrintOperation::paginate:
587 * @operation: the #GtkPrintOperation on which the signal was emitted
588 * @context: the #GtkPrintContext for the current operation
590 * Emitted after the begin-print signal, but before the actual
591 * rendering starts. It keeps getting emitted until it returns %FALSE.
593 * This signal is intended to be used for paginating the document
594 * in small chunks, to avoid blocking the user interface for a long
595 * time. The signal handler should update the number of pages using
596 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
597 * has been completely paginated.
599 * If you don't need to do pagination in chunks, you can simply do
600 * it all in the begin-print handler, and set the number of pages
603 * Return value: %TRUE if pagination is complete
608 g_signal_new (I_("paginate"),
609 G_TYPE_FROM_CLASS (gobject_class),
611 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
612 _gtk_boolean_handled_accumulator, NULL,
613 _gtk_marshal_BOOLEAN__OBJECT,
614 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
618 * GtkPrintOperation::request-page-setup:
619 * @operation: the #GtkPrintOperation on which the signal was emitted
620 * @context: the #GtkPrintContext for the current operation
621 * @page_nr: the number of the currently printed page
622 * @setup: the #GtkPageSetup
624 * Emitted once for every page that is printed, to give
625 * the application a chance to modify the page setup. Any changes
626 * done to @setup will be in force only for printing this page.
630 signals[REQUEST_PAGE_SETUP] =
631 g_signal_new (I_("request-page-setup"),
632 G_TYPE_FROM_CLASS (gobject_class),
634 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
636 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
638 GTK_TYPE_PRINT_CONTEXT,
640 GTK_TYPE_PAGE_SETUP);
643 * GtkPrintOperation::draw-page:
644 * @operation: the #GtkPrintOperation on which the signal was emitted
645 * @context: the #GtkPrintContext for the current operation
646 * @page_nr: the number of the currently printed page
648 * Emitted for every page that is printed. The signal handler
649 * must render the @page_nr's page onto the cairo context obtained
650 * from @context using gtk_print_context_get_cairo_context().
652 * <informalexample><programlisting>
654 * draw_page (GtkPrintOperation *operation,
655 * GtkPrintContext *context,
657 * gpointer user_data)
660 * PangoLayout *layout;
661 * gdouble width, text_height;
662 * gint layout_height;
663 * PangoFontDescription *desc;
665 * cr = gtk_print_context_get_cairo_context (context);
666 * width = gtk_print_context_get_width (context);
668 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
670 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
673 * layout = gtk_print_context_create_pango_layout (context);
675 * desc = pango_font_description_from_string ("sans 14");
676 * pango_layout_set_font_description (layout, desc);
677 * pango_font_description_free (desc);
679 * pango_layout_set_text (layout, "some text", -1);
680 * pango_layout_set_width (layout, width);
681 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
683 * pango_layout_get_size (layout, NULL, &layout_height);
684 * text_height = (gdouble)layout_height / PANGO_SCALE;
686 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
687 * pango_cairo_show_layout (cr, layout);
689 * g_object_unref (layout);
691 * </programlisting></informalexample>
693 * Use gtk_print_operation_set_use_full_page() and
694 * gtk_print_operation_set_unit() before starting the print operation
695 * to set up the transformation of the cairo context according to your
701 g_signal_new (I_("draw-page"),
702 G_TYPE_FROM_CLASS (gobject_class),
704 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
706 _gtk_marshal_VOID__OBJECT_INT,
708 GTK_TYPE_PRINT_CONTEXT,
712 * GtkPrintOperation::end-print:
713 * @operation: the #GtkPrintOperation on which the signal was emitted
714 * @context: the #GtkPrintContext for the current operation
716 * Emitted after all pages have been rendered.
717 * A handler for this signal can clean up any resources that have
718 * been allocated in the ::begin-print handler.
723 g_signal_new (I_("end-print"),
724 G_TYPE_FROM_CLASS (gobject_class),
726 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
728 g_cclosure_marshal_VOID__OBJECT,
729 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
732 * GtkPrintOperation::status-changed:
733 * @operation: the #GtkPrintOperation on which the signal was emitted
735 * Emitted at between the various phases of the print operation.
736 * See #GtkPrintStatus for the phases that are being discriminated.
737 * Use gtk_print_operation_get_status() to find out the current
742 signals[STATUS_CHANGED] =
743 g_signal_new (I_("status-changed"),
744 G_TYPE_FROM_CLASS (class),
746 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
748 g_cclosure_marshal_VOID__VOID,
753 * GtkPrintOperation::create-custom-widget:
754 * @operation: the #GtkPrintOperation on which the signal was emitted
756 * Emitted when displaying the print dialog. If you return a
757 * widget in a handler for this signal it will be added to a custom
758 * tab in the print dialog. You typically return a container widget
759 * with multiple widgets in it.
761 * The print dialog owns the returned widget, and its lifetime
762 * isn't controlled by the app. However, the widget is guaranteed
763 * to stay around until the custom-widget-apply signal is emitted
764 * on the operation. Then you can read out any information you need
767 * Returns: A custom widget that gets embedded in the print dialog,
772 signals[CREATE_CUSTOM_WIDGET] =
773 g_signal_new (I_("create-custom-widget"),
774 G_TYPE_FROM_CLASS (class),
776 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
777 custom_widget_accumulator, NULL,
778 _gtk_marshal_OBJECT__VOID,
782 * GtkPrintOperation::custom-widget-apply:
783 * @operation: the #GtkPrintOperation on which the signal was emitted
784 * @widget: the custom widget added in create-custom-widget
786 * Emitted right before begin-print if you added
787 * a custom widget in the create-custom-widget handler. When you get
788 * this signal you should read the information from the custom widgets,
789 * as the widgets are not guaraneed to be around at a later time.
793 signals[CUSTOM_WIDGET_APPLY] =
794 g_signal_new (I_("custom-widget-apply"),
795 G_TYPE_FROM_CLASS (class),
797 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
799 g_cclosure_marshal_VOID__OBJECT,
800 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
803 * GtkPrintOperation::preview:
804 * @operation: the #GtkPrintOperation on which the signal was emitted
805 * @preview: the #GtkPrintPreviewOperation for the current operation
806 * @context: the #GtkPrintContext that will be used
807 * @parent: the #GtkWindow to use as window parent, or %NULL
809 * Gets emitted when a preview is requested from the native dialog.
810 * If you handle this you must set the cairo context on the printing context.
812 * If you don't override this a default implementation using an external
813 * viewer will be used.
815 * Returns: %TRUE if the listener wants to take over control of the preview
820 g_signal_new (I_("preview"),
821 G_TYPE_FROM_CLASS (gobject_class),
823 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
824 _gtk_boolean_handled_accumulator, NULL,
825 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
827 GTK_TYPE_PRINT_OPERATION_PREVIEW,
828 GTK_TYPE_PRINT_CONTEXT,
833 * GtkPrintOperation:default-page-setup:
835 * The #GtkPageSetup used by default.
837 * This page setup will be used by gtk_print_operation_run(),
838 * but it can be overridden on a per-page basis by connecting
839 * to the ::request-page-setup signal.
843 g_object_class_install_property (gobject_class,
844 PROP_DEFAULT_PAGE_SETUP,
845 g_param_spec_object ("default-page-setup",
846 P_("Default Page Setup"),
847 P_("The GtkPageSetup used by default"),
849 GTK_PARAM_READWRITE));
852 * GtkPrintOperation:print-settings:
854 * The #GtkPrintSettings used for initializing the dialog.
856 * Setting this property is typically used to re-establish
857 * print settings from a previous print operation, see
858 * gtk_print_operation_run().
862 g_object_class_install_property (gobject_class,
864 g_param_spec_object ("print-settings",
865 P_("Print Settings"),
866 P_("The GtkPrintSettings used for initializing the dialog"),
867 GTK_TYPE_PRINT_SETTINGS,
868 GTK_PARAM_READWRITE));
871 * GtkPrintOperation:job-name:
873 * A string used to identify the job (e.g. in monitoring
874 * applications like eggcups).
876 * If you don't set a job name, GTK+ picks a default one
877 * by numbering successive print jobs.
881 g_object_class_install_property (gobject_class,
883 g_param_spec_string ("job-name",
885 P_("A string used for identifying the print job."),
887 GTK_PARAM_READWRITE));
890 * GtkPrintOperation:n-pages:
892 * The number of pages in the document.
894 * This <emphasis>must</emphasis> be set to a positive number
895 * before the rendering starts. It may be set in a ::begin-print
898 * Note that the page numbers passed to the ::request-page-setup
899 * and ::draw-page signals are 0-based, i.e. if the user chooses
900 * to print all pages, the last ::draw-page signal will be for
905 g_object_class_install_property (gobject_class,
907 g_param_spec_int ("n-pages",
908 P_("Number of Pages"),
909 P_("The number of pages in the document."),
913 GTK_PARAM_READWRITE));
916 * GtkPrintOperation:current-page:
918 * The current page in the document.
920 * If this is set before gtk_print_operation_run(),
921 * the user will be able to select to print only the current page.
923 * Note that this only makes sense for pre-paginated documents.
927 g_object_class_install_property (gobject_class,
929 g_param_spec_int ("current-page",
931 P_("The current page in the document."),
935 GTK_PARAM_READWRITE));
938 * GtkPrintOperation:use-full-page:
940 * If %TRUE, the transformation for the cairo context obtained
941 * from #GtkPrintContext puts the origin at the top left corner
942 * of the page (which may not be the top left corner of the sheet,
943 * depending on page orientation and the number of pages per sheet).
944 * Otherwise, the origin is at the top left corner of the imageable
945 * area (i.e. inside the margins).
949 g_object_class_install_property (gobject_class,
951 g_param_spec_boolean ("use-full-page",
953 P_("TRUE if the the origin of the context should be at the corner of the page and not the corner of the imageable area"),
955 GTK_PARAM_READWRITE));
959 * GtkPrintOperation:track-print-status:
961 * If %TRUE, the print operation will try to continue report on
962 * the status of the print job in the printer queues and printer.
963 * This can allow your application to show things like "out of paper"
964 * issues, and when the print job actually reaches the printer.
965 * However, this is often implemented using polling, and should
966 * not be enabled unless needed.
970 g_object_class_install_property (gobject_class,
971 PROP_TRACK_PRINT_STATUS,
972 g_param_spec_boolean ("track-print-status",
973 P_("Track Print Status"),
974 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."),
976 GTK_PARAM_READWRITE));
980 * GtkPrintOperation:unit:
982 * The transformation for the cairo context obtained from
983 * #GtkPrintContext is set up in such a way that distances
984 * are measured in units of @unit.
988 g_object_class_install_property (gobject_class,
990 g_param_spec_enum ("unit",
992 P_("The unit in which distances can be measured in the context"),
995 GTK_PARAM_READWRITE));
999 * GtkPrintOperation:show-progress:
1001 * Determines whether to show a progress dialog during the
1006 g_object_class_install_property (gobject_class,
1008 g_param_spec_boolean ("show-progress",
1010 P_("TRUE if a progress dialog is shown while printing."),
1012 GTK_PARAM_READWRITE));
1015 * GtkPrintOperation:allow-async:
1017 * Determines whether the print operation may run asynchronously or not.
1018 * Some systems don't support asynchronous printing, but those that do
1019 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1020 * emit the done signal when the operation is actually done.
1024 g_object_class_install_property (gobject_class,
1026 g_param_spec_boolean ("allow-async",
1028 P_("TRUE if print process may run asynchronous."),
1030 GTK_PARAM_READWRITE));
1033 * GtkPrintOperation:export-filename:
1035 * The name of a file file to generate instead of showing
1036 * the print dialog. Currently, PDF is the only supported
1039 * The intended use of this property is for implementing
1040 * "Export to PDF" actions.
1042 * "Print to PDF" support is independent of this and is done
1043 * by letting the user pick the "Print to PDF" item from the
1044 * list of printers in the print dialog.
1048 g_object_class_install_property (gobject_class,
1049 PROP_EXPORT_FILENAME,
1050 g_param_spec_string ("export-filename",
1051 P_("Export filename"),
1052 P_("Export filename"),
1054 GTK_PARAM_READWRITE));
1057 * GtkPrintOperation:status:
1059 * The status of the print operation.
1063 g_object_class_install_property (gobject_class,
1065 g_param_spec_enum ("status",
1067 P_("The status of the print operation"),
1068 GTK_TYPE_PRINT_STATUS,
1069 GTK_PRINT_STATUS_INITIAL,
1070 GTK_PARAM_READABLE));
1073 * GtkPrintOperation:status-string:
1075 * A string representation of the status of the print operation.
1076 * The string is translated and suitable for displaying the print
1077 * status e.g. in a #GtkStatusbar.
1079 * See the ::status property for a status value that is suitable
1080 * for programmatic use.
1084 g_object_class_install_property (gobject_class,
1086 g_param_spec_string ("status-string",
1087 P_("Status String"),
1088 P_("A human-readable description of the status"),
1090 GTK_PARAM_READABLE));
1094 * GtkPrintOperation:custom-tab-label:
1096 * Used as the label of the tab containing custom widgets.
1097 * Note that this property may be ignored on some platforms.
1099 * If this is %NULL, GTK+ uses a default label.
1103 g_object_class_install_property (gobject_class,
1104 PROP_CUSTOM_TAB_LABEL,
1105 g_param_spec_string ("custom-tab-label",
1106 P_("Custom tab label"),
1107 P_("Label for the tab containing custom widgets."),
1109 GTK_PARAM_READWRITE));
1114 * gtk_print_operation_new:
1116 * Creates a new #GtkPrintOperation.
1118 * Returns: a new #GtkPrintOperation
1123 gtk_print_operation_new (void)
1125 GtkPrintOperation *print_operation;
1127 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1129 return print_operation;
1133 * gtk_print_operation_set_default_page_setup:
1134 * @op: a #GtkPrintOperation
1135 * @default_page_setup: a #GtkPageSetup, or %NULL
1137 * Makes @default_page_setup the default page setup for @op.
1139 * This page setup will be used by gtk_print_operation_run(),
1140 * but it can be overridden on a per-page basis by connecting
1141 * to the ::request-page-setup signal.
1146 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1147 GtkPageSetup *default_page_setup)
1149 GtkPrintOperationPrivate *priv;
1151 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1152 g_return_if_fail (default_page_setup == NULL ||
1153 GTK_IS_PAGE_SETUP (default_page_setup));
1157 if (default_page_setup != priv->default_page_setup)
1159 if (default_page_setup)
1160 g_object_ref (default_page_setup);
1162 if (priv->default_page_setup)
1163 g_object_unref (priv->default_page_setup);
1165 priv->default_page_setup = default_page_setup;
1167 g_object_notify (G_OBJECT (op), "default-page-setup");
1172 * gtk_print_operation_get_default_page_setup:
1173 * @op: a #GtkPrintOperation
1175 * Returns the default page setup, see
1176 * gtk_print_operation_set_default_page_setup().
1178 * Returns: the default page setup
1183 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1185 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1187 return op->priv->default_page_setup;
1192 * gtk_print_operation_set_print_settings:
1193 * @op: a #GtkPrintOperation
1194 * @print_settings: #GtkPrintSettings, or %NULL
1196 * Sets the print settings for @op. This is typically used to
1197 * re-establish print settings from a previous print operation,
1198 * see gtk_print_operation_run().
1203 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1204 GtkPrintSettings *print_settings)
1206 GtkPrintOperationPrivate *priv;
1208 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1209 g_return_if_fail (print_settings == NULL ||
1210 GTK_IS_PRINT_SETTINGS (print_settings));
1214 if (print_settings != priv->print_settings)
1217 g_object_ref (print_settings);
1219 if (priv->print_settings)
1220 g_object_unref (priv->print_settings);
1222 priv->print_settings = print_settings;
1224 g_object_notify (G_OBJECT (op), "print-settings");
1229 * gtk_print_operation_get_print_settings:
1230 * @op: a #GtkPrintOperation
1232 * Returns the current print settings.
1234 * Note that the return value is %NULL until either
1235 * gtk_print_operation_set_print_settings() or
1236 * gtk_print_operation_run() have been called.
1238 * Return value: the current print settings of @op.
1243 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1245 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1247 return op->priv->print_settings;
1251 * gtk_print_operation_set_job_name:
1252 * @op: a #GtkPrintOperation
1253 * @job_name: a string that identifies the print job
1255 * Sets the name of the print job. The name is used to identify
1256 * the job (e.g. in monitoring applications like eggcups).
1258 * If you don't set a job name, GTK+ picks a default one by
1259 * numbering successive print jobs.
1264 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1265 const gchar *job_name)
1267 GtkPrintOperationPrivate *priv;
1269 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1270 g_return_if_fail (g_utf8_validate (job_name, -1, NULL));
1274 g_free (priv->job_name);
1275 priv->job_name = g_strdup (job_name);
1277 g_object_notify (G_OBJECT (op), "job-name");
1281 * gtk_print_operation_set_n_pages:
1282 * @op: a #GtkPrintOperation
1283 * @n_pages: the number of pages
1285 * Sets the number of pages in the document.
1287 * This <emphasis>must</emphasis> be set to a positive number
1288 * before the rendering starts. It may be set in a ::begin-print
1291 * Note that the page numbers passed to the ::request-page-setup
1292 * and ::draw-page signals are 0-based, i.e. if the user chooses
1293 * to print all pages, the last ::draw-page signal will be
1294 * for page @n_pages - 1.
1299 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1302 GtkPrintOperationPrivate *priv;
1304 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1305 g_return_if_fail (n_pages > 0);
1308 g_return_if_fail (priv->current_page == -1 ||
1309 priv->current_page < n_pages);
1311 if (priv->nr_of_pages != n_pages)
1313 priv->nr_of_pages = n_pages;
1315 g_object_notify (G_OBJECT (op), "n-pages");
1320 * gtk_print_operation_set_current_page:
1321 * @op: a #GtkPrintOperation
1322 * @current_page: the current page, 0-based
1324 * Sets the current page.
1326 * If this is called before gtk_print_operation_run(),
1327 * the user will be able to select to print only the current page.
1329 * Note that this only makes sense for pre-paginated documents.
1334 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1337 GtkPrintOperationPrivate *priv;
1339 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1340 g_return_if_fail (current_page >= 0);
1343 g_return_if_fail (priv->nr_of_pages == -1 ||
1344 current_page < priv->nr_of_pages);
1346 if (priv->current_page != current_page)
1348 priv->current_page = current_page;
1350 g_object_notify (G_OBJECT (op), "current-page");
1355 * gtk_print_operation_set_use_full_page:
1356 * @op: a #GtkPrintOperation
1357 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1359 * If @full_page is %TRUE, the transformation for the cairo context
1360 * obtained from #GtkPrintContext puts the origin at the top left
1361 * corner of the page (which may not be the top left corner of the
1362 * sheet, depending on page orientation and the number of pages per
1363 * sheet). Otherwise, the origin is at the top left corner of the
1364 * imageable area (i.e. inside the margins).
1369 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1372 GtkPrintOperationPrivate *priv;
1374 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1376 full_page = full_page != FALSE;
1380 if (priv->use_full_page != full_page)
1382 priv->use_full_page = full_page;
1384 g_object_notify (G_OBJECT (op), "use-full-page");
1389 * gtk_print_operation_set_unit:
1390 * @op: a #GtkPrintOperation
1391 * @unit: the unit to use
1393 * Sets up the transformation for the cairo context obtained from
1394 * #GtkPrintContext in such a way that distances are measured in
1400 gtk_print_operation_set_unit (GtkPrintOperation *op,
1403 GtkPrintOperationPrivate *priv;
1405 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1409 if (priv->unit != unit)
1413 g_object_notify (G_OBJECT (op), "unit");
1418 * gtk_print_operation_set_track_print_status:
1419 * @op: a #GtkPrintOperation
1420 * @track_status: %TRUE to track status after printing
1422 * If track_status is %TRUE, the print operation will try to continue report
1423 * on the status of the print job in the printer queues and printer. This
1424 * can allow your application to show things like "out of paper" issues,
1425 * and when the print job actually reaches the printer.
1427 * This function is often implemented using some form of polling, so it should
1428 * not be enabled unless needed.
1433 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1434 gboolean track_status)
1436 GtkPrintOperationPrivate *priv;
1438 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1442 if (priv->track_print_status != track_status)
1444 priv->track_print_status = track_status;
1446 g_object_notify (G_OBJECT (op), "track-print-status");
1451 _gtk_print_operation_set_status (GtkPrintOperation *op,
1452 GtkPrintStatus status,
1453 const gchar *string)
1455 GtkPrintOperationPrivate *priv = op->priv;
1456 static const gchar *status_strs[] = {
1457 /* translators, strip the prefix up to and including the first | */
1458 N_("print operation status|Initial state"),
1459 /* translators, strip the prefix up to and including the first | */
1460 N_("print operation status|Preparing to print"),
1461 /* translators, strip the prefix up to and including the first | */
1462 N_("print operation status|Generating data"),
1463 /* translators, strip the prefix up to and including the first | */
1464 N_("print operation status|Sending data"),
1465 /* translators, strip the prefix up to and including the first | */
1466 N_("print operation status|Waiting"),
1467 /* translators, strip the prefix up to and including the first | */
1468 N_("print operation status|Blocking on issue"),
1469 /* translators, strip the prefix up to and including the first | */
1470 N_("print operation status|Printing"),
1471 /* translators, strip the prefix up to and including the first | */
1472 N_("print operation status|Finished"),
1473 /* translators, strip the prefix up to and including the first | */
1474 N_("print operation status|Finished with error")
1477 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1478 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1481 string = g_strip_context (status_strs[status],
1482 gettext (status_strs[status]));
1484 if (priv->status == status &&
1485 strcmp (string, priv->status_string) == 0)
1488 g_free (priv->status_string);
1489 priv->status_string = g_strdup (string);
1490 priv->status = status;
1492 g_object_notify (G_OBJECT (op), "status");
1493 g_object_notify (G_OBJECT (op), "status-string");
1495 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1500 * gtk_print_operation_get_status:
1501 * @op: a #GtkPrintOperation
1503 * Returns the status of the print operation.
1504 * Also see gtk_print_operation_get_status_string().
1506 * Return value: the status of the print operation
1511 gtk_print_operation_get_status (GtkPrintOperation *op)
1513 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1514 GTK_PRINT_STATUS_FINISHED_ABORTED);
1516 return op->priv->status;
1520 * gtk_print_operation_get_status_string:
1521 * @op: a #GtkPrintOperation
1523 * Returns a string representation of the status of the
1524 * print operation. The string is translated and suitable
1525 * for displaying the print status e.g. in a #GtkStatusbar.
1527 * Use gtk_print_operation_get_status() to obtain a status
1528 * value that is suitable for programmatic use.
1530 * Return value: a string representation of the status
1531 * of the print operation
1535 G_CONST_RETURN gchar *
1536 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1538 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1540 return op->priv->status_string;
1544 * gtk_print_operation_is_finished:
1545 * @op: a #GtkPrintOperation
1547 * A convenience function to find out if the print operation
1548 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1549 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1551 * Note: when you enable print status tracking the print operation
1552 * can be in a non-finished state even after done has been called, as
1553 * the operation status then tracks the print job status on the printer.
1555 * Return value: %TRUE, if the print operation is finished.
1560 gtk_print_operation_is_finished (GtkPrintOperation *op)
1562 GtkPrintOperationPrivate *priv;
1564 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1568 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1569 priv->status == GTK_PRINT_STATUS_FINISHED;
1573 * gtk_print_operation_set_show_progress:
1574 * @op: a #GtkPrintOperation
1575 * @show_progress: %TRUE to show a progress dialog
1577 * If @show_progress is %TRUE, the print operation will show a
1578 * progress dialog during the print operation.
1583 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1584 gboolean show_progress)
1586 GtkPrintOperationPrivate *priv;
1588 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1592 show_progress = show_progress != FALSE;
1594 if (priv->show_progress != show_progress)
1596 priv->show_progress = show_progress;
1598 g_object_notify (G_OBJECT (op), "show-progress");
1603 * gtk_print_operation_set_allow_async:
1604 * @op: a #GtkPrintOperation
1605 * @allow_async: %TRUE to allow asynchronous operation
1607 * Sets whether the gtk_print_operation_run() may return
1608 * before the print operation is completed. Note that
1609 * some platforms may not allow asynchronous operation.
1614 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1615 gboolean allow_async)
1617 GtkPrintOperationPrivate *priv;
1619 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1623 allow_async = allow_async != FALSE;
1625 if (priv->allow_async != allow_async)
1627 priv->allow_async = allow_async;
1629 g_object_notify (G_OBJECT (op), "allow-async");
1635 * gtk_print_operation_set_custom_tab_label:
1636 * @op: a #GtkPrintOperation
1637 * @label: the label to use, or %NULL to use the default label
1639 * Sets the label for the tab holding custom widgets.
1644 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1647 GtkPrintOperationPrivate *priv;
1649 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1653 g_free (priv->custom_tab_label);
1654 priv->custom_tab_label = g_strdup (label);
1656 g_object_notify (G_OBJECT (op), "custom-tab-label");
1661 * gtk_print_operation_set_export_filename:
1662 * @op: a #GtkPrintOperation
1663 * @filename: the filename for the exported file
1665 * Sets up the #GtkPrintOperation to generate a file instead
1666 * of showing the print dialog. The indended use of this function
1667 * is for implementing "Export to PDF" actions. Currently, PDF
1668 * is the only supported format.
1670 * "Print to PDF" support is independent of this and is done
1671 * by letting the user pick the "Print to PDF" item from the list
1672 * of printers in the print dialog.
1677 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1678 const gchar *filename)
1680 GtkPrintOperationPrivate *priv;
1682 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1686 g_free (priv->export_filename);
1687 priv->export_filename = g_strdup (filename);
1689 g_object_notify (G_OBJECT (op), "export_filename");
1692 /* Creates the initial page setup used for printing unless the
1693 * app overrides this on a per-page basis using request_page_setup.
1695 * Data is taken from, in order, if existing:
1697 * PrintSettings returned from the print dialog
1698 * (initial dialog values are set from default_page_setup
1699 * if unset in app specified print_settings)
1700 * default_page_setup
1701 * per-locale default setup
1703 static GtkPageSetup *
1704 create_page_setup (GtkPrintOperation *op)
1706 GtkPrintOperationPrivate *priv = op->priv;
1707 GtkPageSetup *page_setup;
1708 GtkPrintSettings *settings;
1710 if (priv->default_page_setup)
1711 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1713 page_setup = gtk_page_setup_new ();
1715 settings = priv->print_settings;
1718 GtkPaperSize *paper_size;
1720 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1721 gtk_page_setup_set_orientation (page_setup,
1722 gtk_print_settings_get_orientation (settings));
1725 paper_size = gtk_print_settings_get_paper_size (settings);
1728 gtk_page_setup_set_paper_size (page_setup, paper_size);
1729 gtk_paper_size_free (paper_size);
1732 /* TODO: Margins? */
1739 pdf_start_page (GtkPrintOperation *op,
1740 GtkPrintContext *print_context,
1741 GtkPageSetup *page_setup)
1743 GtkPaperSize *paper_size;
1744 cairo_surface_t *surface = op->priv->platform_data;
1747 paper_size = gtk_page_setup_get_paper_size (page_setup);
1749 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1750 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1752 cairo_pdf_surface_set_size (surface, w, h);
1756 pdf_end_page (GtkPrintOperation *op,
1757 GtkPrintContext *print_context)
1761 cr = gtk_print_context_get_cairo_context (print_context);
1762 cairo_show_page (cr);
1766 pdf_end_run (GtkPrintOperation *op,
1770 GtkPrintOperationPrivate *priv = op->priv;
1771 cairo_surface_t *surface = priv->platform_data;
1773 cairo_surface_finish (surface);
1774 cairo_surface_destroy (surface);
1777 static GtkPrintOperationResult
1778 run_pdf (GtkPrintOperation *op,
1782 GtkPrintOperationPrivate *priv = op->priv;
1783 GtkPageSetup *page_setup;
1784 cairo_surface_t *surface;
1786 gdouble width, height;
1788 priv->print_context = _gtk_print_context_new (op);
1790 page_setup = create_page_setup (op);
1791 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
1793 /* This will be overwritten later by the non-default size, but
1794 we need to pass some size: */
1795 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1796 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1797 g_object_unref (page_setup);
1799 surface = cairo_pdf_surface_create (priv->export_filename,
1801 cairo_surface_set_fallback_resolution (surface, 300, 300);
1803 priv->platform_data = surface;
1804 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
1806 cr = cairo_create (surface);
1807 gtk_print_context_set_cairo_context (op->priv->print_context,
1812 priv->print_pages = GTK_PRINT_PAGES_ALL;
1813 priv->page_ranges = NULL;
1814 priv->num_page_ranges = 0;
1816 priv->manual_num_copies = 1;
1817 priv->manual_collation = FALSE;
1818 priv->manual_reverse = FALSE;
1819 priv->manual_page_set = GTK_PAGE_SET_ALL;
1820 priv->manual_scale = 1.0;
1821 priv->manual_orientation = TRUE;
1825 priv->start_page = pdf_start_page;
1826 priv->end_page = pdf_end_page;
1827 priv->end_run = pdf_end_run;
1829 return GTK_PRINT_OPERATION_RESULT_APPLY;
1834 GtkPrintOperation *op;
1835 gint uncollated_copies;
1836 gint collated_copies;
1837 gint uncollated, collated, total;
1839 gint range, num_ranges;
1840 GtkPageRange *ranges;
1841 GtkPageRange one_range;
1843 gint page, start, end, inc;
1845 GtkWidget *progress;
1847 gboolean initialized;
1848 gboolean is_preview;
1852 find_range (PrintPagesData *data)
1854 GtkPageRange *range;
1856 range = &data->ranges[data->range];
1860 data->start = range->end;
1861 data->end = range->start - 1;
1865 data->start = range->start;
1866 data->end = range->end + 1;
1871 increment_page_sequence (PrintPagesData *data)
1873 GtkPrintOperationPrivate *priv = data->op->priv;
1876 data->page += data->inc;
1877 if (data->page == data->end)
1879 data->range += data->inc;
1880 if (data->range == -1 || data->range == data->num_ranges)
1883 if (data->uncollated == data->uncollated_copies)
1886 data->range = data->inc < 0 ? data->num_ranges - 1 : 0;
1889 data->page = data->start;
1892 while ((priv->manual_page_set == GTK_PAGE_SET_EVEN && data->page % 2 == 0) ||
1893 (priv->manual_page_set == GTK_PAGE_SET_ODD && data->page % 2 == 1));
1899 print_pages_idle_done (gpointer user_data)
1901 PrintPagesData *data;
1902 GtkPrintOperationPrivate *priv;
1904 GDK_THREADS_ENTER ();
1906 data = (PrintPagesData*)user_data;
1907 priv = data->op->priv;
1909 priv->print_pages_idle_id = 0;
1911 if (priv->show_progress_timeout_id > 0)
1913 g_source_remove (priv->show_progress_timeout_id);
1914 priv->show_progress_timeout_id = 0;
1918 gtk_widget_destroy (data->progress);
1920 if (priv->rloop && !data->is_preview)
1921 g_main_loop_quit (priv->rloop);
1923 if (!data->is_preview)
1924 g_signal_emit (data->op, signals[DONE], 0,
1926 GTK_PRINT_OPERATION_RESULT_CANCEL :
1927 GTK_PRINT_OPERATION_RESULT_APPLY);
1929 g_object_unref (data->op);
1933 GDK_THREADS_LEAVE ();
1937 update_progress (PrintPagesData *data)
1939 GtkPrintOperationPrivate *priv;
1942 priv = data->op->priv;
1946 if (priv->status == GTK_PRINT_STATUS_PREPARING)
1948 if (priv->nr_of_pages > 0)
1949 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages);
1951 text = g_strdup (_("Preparing"));
1953 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
1954 text = g_strdup_printf (_("Printing %d"), data->total);
1958 g_object_set (data->progress, "text", text, NULL);
1965 common_render_page (GtkPrintOperation *op,
1968 GtkPrintOperationPrivate *priv = op->priv;
1969 GtkPageSetup *page_setup;
1970 GtkPrintContext *print_context;
1973 print_context = priv->print_context;
1975 page_setup = create_page_setup (op);
1977 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
1978 print_context, page_nr, page_setup);
1980 _gtk_print_context_set_page_setup (print_context, page_setup);
1982 priv->start_page (op, print_context, page_setup);
1984 cr = gtk_print_context_get_cairo_context (print_context);
1987 if (priv->manual_scale != 1.0)
1990 priv->manual_scale);
1992 if (priv->manual_orientation)
1993 _gtk_print_context_rotate_according_to_orientation (print_context);
1995 if (!priv->use_full_page)
1996 _gtk_print_context_translate_into_margin (print_context);
1998 g_signal_emit (op, signals[DRAW_PAGE], 0,
1999 print_context, page_nr);
2001 priv->end_page (op, print_context);
2005 g_object_unref (page_setup);
2009 print_pages_idle (gpointer user_data)
2011 PrintPagesData *data;
2012 GtkPrintOperationPrivate *priv;
2013 GtkPageSetup *page_setup;
2014 gboolean done = FALSE;
2016 GDK_THREADS_ENTER ();
2018 data = (PrintPagesData*)user_data;
2019 priv = data->op->priv;
2021 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2023 if (!data->initialized)
2025 data->initialized = TRUE;
2026 page_setup = create_page_setup (data->op);
2027 _gtk_print_context_set_page_setup (priv->print_context,
2029 g_object_unref (page_setup);
2031 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2033 if (priv->manual_collation)
2035 data->uncollated_copies = priv->manual_num_copies;
2036 data->collated_copies = 1;
2040 data->uncollated_copies = 1;
2041 data->collated_copies = priv->manual_num_copies;
2047 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2049 gboolean paginated = FALSE;
2051 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2056 /* FIXME handle this better */
2057 if (priv->nr_of_pages == 0)
2058 g_warning ("no pages to print");
2060 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2062 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2064 data->ranges = priv->page_ranges;
2065 data->num_ranges = priv->num_page_ranges;
2067 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2068 priv->current_page != -1)
2070 data->ranges = &data->one_range;
2071 data->num_ranges = 1;
2072 data->ranges[0].start = priv->current_page;
2073 data->ranges[0].end = priv->current_page;
2077 data->ranges = &data->one_range;
2078 data->num_ranges = 1;
2079 data->ranges[0].start = 0;
2080 data->ranges[0].end = priv->nr_of_pages - 1;
2083 if (priv->manual_reverse)
2085 data->range = data->num_ranges - 1;
2095 /* go back one page, since we preincrement below */
2096 data->page = data->start - data->inc;
2097 data->collated = data->collated_copies - 1;
2099 _gtk_print_operation_set_status (data->op,
2100 GTK_PRINT_STATUS_GENERATING_DATA,
2108 if (data->collated == data->collated_copies)
2111 if (!increment_page_sequence (data))
2119 if (data->is_preview)
2123 g_object_ref (data->op);
2125 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2129 common_render_page (data->op, data->page);
2133 if (priv->cancelled)
2135 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2140 if (done && !data->is_preview)
2142 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2143 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2146 update_progress (data);
2148 GDK_THREADS_LEAVE ();
2154 handle_progress_response (GtkWidget *dialog,
2158 GtkPrintOperation *op = (GtkPrintOperation *)data;
2160 gtk_widget_hide (dialog);
2161 gtk_print_operation_cancel (op);
2165 show_progress_timeout (PrintPagesData *data)
2167 GDK_THREADS_ENTER ();
2169 gtk_window_present (GTK_WINDOW (data->progress));
2171 data->op->priv->show_progress_timeout_id = 0;
2173 GDK_THREADS_LEAVE ();
2179 print_pages (GtkPrintOperation *op,
2182 GtkPrintOperationResult result)
2184 GtkPrintOperationPrivate *priv = op->priv;
2185 PrintPagesData *data;
2189 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2190 g_signal_emit (op, signals[DONE], 0, result);
2194 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2196 data = g_new0 (PrintPagesData, 1);
2197 data->op = g_object_ref (op);
2198 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2200 if (priv->show_progress)
2202 GtkWidget *progress;
2204 progress = gtk_message_dialog_new (parent, 0,
2208 g_signal_connect (progress, "response",
2209 G_CALLBACK (handle_progress_response), op);
2211 priv->show_progress_timeout_id =
2212 g_timeout_add (SHOW_PROGRESS_TIME,
2213 (GSourceFunc)show_progress_timeout,
2216 data->progress = progress;
2219 if (data->is_preview)
2223 g_signal_emit_by_name (op, "preview",
2224 GTK_PRINT_OPERATION_PREVIEW (op),
2225 priv->print_context,
2230 gtk_print_context_get_cairo_context (priv->print_context) == NULL)
2232 /* Programmer error */
2233 g_error ("You must set a cairo context on the print context");
2236 priv->start_page = preview_start_page;
2237 priv->end_page = preview_end_page;
2238 priv->end_run = preview_end_run;
2240 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
2241 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
2242 &priv->num_page_ranges);
2243 priv->manual_num_copies = 1;
2244 priv->manual_collation = FALSE;
2245 priv->manual_reverse = FALSE;
2246 priv->manual_page_set = GTK_PAGE_SET_ALL;
2247 priv->manual_scale = 1.0;
2248 priv->manual_orientation = TRUE;
2251 priv->print_pages_idle_id = g_idle_add_full (G_PRIORITY_DEFAULT_IDLE,
2254 print_pages_idle_done);
2256 /* Recursive main loop to make sure we don't exit on sync operations */
2259 priv->rloop = g_main_loop_new (NULL, FALSE);
2261 GDK_THREADS_LEAVE ();
2262 g_main_loop_run (priv->rloop);
2263 GDK_THREADS_ENTER ();
2265 g_main_loop_unref (priv->rloop);
2271 * gtk_print_operation_get_error:
2272 * @op: a #GtkPrintOperation
2273 * @error: return location for the error
2275 * Call this when the result of a print operation is
2276 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
2277 * gtk_print_operation_run(), or in the ::done signal handler.
2278 * The returned #GError will contain more details on what went wrong.
2283 gtk_print_operation_get_error (GtkPrintOperation *op,
2286 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2288 g_propagate_error (error, op->priv->error);
2290 op->priv->error = NULL;
2295 * gtk_print_operation_run:
2296 * @op: a #GtkPrintOperation
2297 * @action: the action to start
2298 * @parent: Transient parent of the dialog, or %NULL
2299 * @error: Return location for errors, or %NULL
2301 * Runs the print operation, by first letting the user modify
2302 * print settings in the print dialog, and then print the
2305 * Normally that this function does not return until the rendering of all
2306 * pages is complete. You can connect to the ::status-changed signal on
2307 * @op to obtain some information about the progress of the print operation.
2308 * Furthermore, it may use a recursive mainloop to show the print dialog.
2310 * If you call gtk_print_operation_set_allow_async() or set the allow-async
2311 * property the operation will run asyncronously if this is supported on the
2312 * platform. The ::done signal will be emitted with the operation results when
2313 * the operation is done (i.e. when the dialog is canceled, or when the print
2314 * succeeds or fails).
2316 * <informalexample><programlisting>
2317 * if (settings != NULL)
2318 * gtk_print_operation_set_print_settings (print, settings);
2320 * if (page_setup != NULL)
2321 * gtk_print_operation_set_default_page_setup (print, page_setup);
2323 * g_signal_connect (print, "begin-print",
2324 * G_CALLBACK (begin_print), &data);
2325 * g_signal_connect (print, "draw-page",
2326 * G_CALLBACK (draw_page), &data);
2328 * res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG, parent, &error);
2330 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
2332 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
2333 * GTK_DIALOG_DESTROY_WITH_PARENT,
2334 * GTK_MESSAGE_ERROR,
2335 * GTK_BUTTONS_CLOSE,
2336 * "Error printing file:\n%s",
2338 * g_signal_connect (error_dialog, "response",
2339 * G_CALLBACK (gtk_widget_destroy), NULL);
2340 * gtk_widget_show (error_dialog);
2341 * g_error_free (error);
2343 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
2345 * if (settings != NULL)
2346 * g_object_unref (settings);
2347 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
2349 * </programlisting></informalexample>
2351 * Return value: the result of the print operation. A return value of
2352 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
2353 * completed successfully. In this case, it is a good idea to obtain
2354 * the used print settings with gtk_print_operation_get_print_settings()
2355 * and store them for reuse with the next print operation. A value of
2356 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
2357 * asynchronously, and will emit the ::done signal when done.
2361 GtkPrintOperationResult
2362 gtk_print_operation_run (GtkPrintOperation *op,
2363 GtkPrintOperationAction action,
2367 GtkPrintOperationPrivate *priv;
2368 GtkPrintOperationResult result;
2369 GtkPageSetup *page_setup;
2372 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
2373 GTK_PRINT_OPERATION_RESULT_ERROR);
2379 priv->action = action;
2381 if (priv->print_settings == NULL)
2382 priv->print_settings = gtk_print_settings_new ();
2384 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
2386 priv->is_sync = TRUE;
2387 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
2388 result = run_pdf (op, parent, &do_print);
2390 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
2392 priv->is_sync = !priv->allow_async;
2393 priv->print_context = _gtk_print_context_new (op);
2394 page_setup = create_page_setup (op);
2395 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2396 g_object_unref (page_setup);
2398 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2401 else if (priv->allow_async)
2403 priv->is_sync = FALSE;
2404 _gtk_print_operation_platform_backend_run_dialog_async (op,
2405 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2408 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
2413 priv->is_sync = TRUE;
2414 result = _gtk_print_operation_platform_backend_run_dialog (op,
2415 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
2420 if (result != GTK_PRINT_OPERATION_RESULT_IN_PROGRESS)
2421 print_pages (op, parent, do_print, result);
2423 if (priv->error && error)
2424 *error = g_error_copy (priv->error);
2430 * gtk_print_operation_cancel:
2431 * @op: a #GtkPrintOperation
2433 * Cancels a running print operation. This function may
2434 * be called from a begin-print, paginate or draw-page
2435 * signal handler to stop the currently running print
2441 gtk_print_operation_cancel (GtkPrintOperation *op)
2443 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2445 op->priv->cancelled = TRUE;
2450 #define __GTK_PRINT_OPERATION_C__
2451 #include "gtkaliasdef.c"