1 /* GTK - The GIMP Toolkit
2 * gtkprintoperation.c: Print Operation
3 * Copyright (C) 2006, Red Hat, Inc.
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the
17 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 * Boston, MA 02111-1307, USA.
28 #include <cairo-pdf.h>
30 #include "gtkprintoperation-private.h"
31 #include "gtkmarshalers.h"
33 #include "gtkprivate.h"
34 #include "gtkmainprivate.h"
35 #include "gtkmessagedialog.h"
36 #include "gtktypebuiltins.h"
39 * SECTION:gtkprintoperation
40 * @Title: GtkPrintOperation
41 * @Short_description: High-level Printing API
42 * @See_also: #GtkPrintContext, #GtkPrintUnixDialog
44 * GtkPrintOperation is the high-level, portable printing API.
45 * It looks a bit different than other GTK+ dialogs such as the
46 * #GtkFileChooser, since some platforms don't expose enough
47 * infrastructure to implement a good print dialog. On such
48 * platforms, GtkPrintOperation uses the native print dialog.
49 * On platforms which do not provide a native print dialog, GTK+
50 * uses its own, see #GtkPrintUnixDialog.
52 * The typical way to use the high-level printing API is to create
53 * a GtkPrintOperation object with gtk_print_operation_new() when
54 * the user selects to print. Then you set some properties on it,
55 * e.g. the page size, any #GtkPrintSettings from previous print
56 * operations, the number of pages, the current page, etc.
58 * Then you start the print operation by calling gtk_print_operation_run().
59 * It will then show a dialog, let the user select a printer and
60 * options. When the user finished the dialog various signals will
61 * be emitted on the #GtkPrintOperation, the main one being
62 * #GtkPrintOperation::draw-page, which you are supposed to catch
63 * and render the page on the provided #GtkPrintContext using Cairo.
66 * <title>The high-level printing API</title>
68 * static GtkPrintSettings *settings = NULL;
73 * GtkPrintOperation *print;
74 * GtkPrintOperationResult res;
76 * print = gtk_print_operation_new ();
78 * if (settings != NULL)
79 * gtk_print_operation_set_print_settings (print, settings);
81 * g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), NULL);
82 * g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
84 * res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
85 * GTK_WINDOW (main_window), NULL);
87 * if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
89 * if (settings != NULL)
90 * g_object_unref (settings);
91 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
94 * g_object_unref (print);
99 * By default GtkPrintOperation uses an external application to do
100 * print preview. To implement a custom print preview, an application
101 * must connect to the preview signal. The functions
102 * gtk_print_operation_print_preview_render_page(),
103 * gtk_print_operation_preview_end_preview() and
104 * gtk_print_operation_preview_is_selected()
105 * are useful when implementing a print preview.
108 #define SHOW_PROGRESS_TIME 1200
120 CREATE_CUSTOM_WIDGET,
123 UPDATE_CUSTOM_WIDGET,
130 PROP_DEFAULT_PAGE_SETUP,
136 PROP_TRACK_PRINT_STATUS,
140 PROP_EXPORT_FILENAME,
143 PROP_CUSTOM_TAB_LABEL,
144 PROP_EMBED_PAGE_SETUP,
146 PROP_SUPPORT_SELECTION,
147 PROP_N_PAGES_TO_PRINT
150 static guint signals[LAST_SIGNAL] = { 0 };
151 static int job_nr = 0;
152 typedef struct _PrintPagesData PrintPagesData;
154 static void preview_iface_init (GtkPrintOperationPreviewIface *iface);
155 static GtkPageSetup *create_page_setup (GtkPrintOperation *op);
156 static void common_render_page (GtkPrintOperation *op,
158 static void increment_page_sequence (PrintPagesData *data);
159 static void prepare_data (PrintPagesData *data);
160 static void clamp_page_ranges (PrintPagesData *data);
163 G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
164 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
168 * gtk_print_error_quark:
170 * Registers an error quark for #GtkPrintOperation if necessary.
172 * Return value: The error quark used for #GtkPrintOperation errors.
177 gtk_print_error_quark (void)
179 static GQuark quark = 0;
181 quark = g_quark_from_static_string ("gtk-print-error-quark");
186 gtk_print_operation_finalize (GObject *object)
188 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
189 GtkPrintOperationPrivate *priv = print_operation->priv;
191 if (priv->free_platform_data &&
194 priv->free_platform_data (priv->platform_data);
195 priv->free_platform_data = NULL;
198 if (priv->default_page_setup)
199 g_object_unref (priv->default_page_setup);
201 if (priv->print_settings)
202 g_object_unref (priv->print_settings);
204 if (priv->print_context)
205 g_object_unref (priv->print_context);
207 g_free (priv->export_filename);
208 g_free (priv->job_name);
209 g_free (priv->custom_tab_label);
210 g_free (priv->status_string);
212 if (priv->print_pages_idle_id > 0)
213 g_source_remove (priv->print_pages_idle_id);
215 if (priv->show_progress_timeout_id > 0)
216 g_source_remove (priv->show_progress_timeout_id);
219 g_error_free (priv->error);
221 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
225 gtk_print_operation_init (GtkPrintOperation *operation)
227 GtkPrintOperationPrivate *priv;
230 priv = operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
231 GTK_TYPE_PRINT_OPERATION,
232 GtkPrintOperationPrivate);
234 priv->status = GTK_PRINT_STATUS_INITIAL;
235 priv->status_string = g_strdup ("");
236 priv->default_page_setup = NULL;
237 priv->print_settings = NULL;
238 priv->nr_of_pages = -1;
239 priv->nr_of_pages_to_print = -1;
240 priv->page_position = -1;
241 priv->current_page = -1;
242 priv->use_full_page = FALSE;
243 priv->show_progress = FALSE;
244 priv->export_filename = NULL;
245 priv->track_print_status = FALSE;
246 priv->is_sync = FALSE;
247 priv->support_selection = FALSE;
248 priv->has_selection = FALSE;
249 priv->embed_page_setup = FALSE;
251 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
254 priv->unit = GTK_UNIT_PIXEL;
256 appname = g_get_application_name ();
259 /* translators: this string is the default job title for print
260 * jobs. %s gets replaced by the application name, %d gets replaced
263 priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
267 preview_iface_render_page (GtkPrintOperationPreview *preview,
271 GtkPrintOperation *op;
273 op = GTK_PRINT_OPERATION (preview);
274 common_render_page (op, page_nr);
278 preview_iface_end_preview (GtkPrintOperationPreview *preview)
280 GtkPrintOperation *op;
281 GtkPrintOperationResult result;
283 op = GTK_PRINT_OPERATION (preview);
285 g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);
288 g_main_loop_quit (op->priv->rloop);
290 if (op->priv->end_run)
291 op->priv->end_run (op, op->priv->is_sync, TRUE);
293 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);
296 result = GTK_PRINT_OPERATION_RESULT_ERROR;
297 else if (op->priv->cancelled)
298 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
300 result = GTK_PRINT_OPERATION_RESULT_APPLY;
302 g_signal_emit (op, signals[DONE], 0, result);
306 preview_iface_is_selected (GtkPrintOperationPreview *preview,
309 GtkPrintOperation *op;
310 GtkPrintOperationPrivate *priv;
313 op = GTK_PRINT_OPERATION (preview);
316 switch (priv->print_pages)
318 case GTK_PRINT_PAGES_SELECTION:
319 case GTK_PRINT_PAGES_ALL:
320 return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
321 case GTK_PRINT_PAGES_CURRENT:
322 return page_nr == priv->current_page;
323 case GTK_PRINT_PAGES_RANGES:
324 for (i = 0; i < priv->num_page_ranges; i++)
326 if (page_nr >= priv->page_ranges[i].start &&
327 (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
336 preview_iface_init (GtkPrintOperationPreviewIface *iface)
338 iface->render_page = preview_iface_render_page;
339 iface->end_preview = preview_iface_end_preview;
340 iface->is_selected = preview_iface_is_selected;
344 preview_start_page (GtkPrintOperation *op,
345 GtkPrintContext *print_context,
346 GtkPageSetup *page_setup)
348 if ((op->priv->manual_number_up < 2) ||
349 (op->priv->page_position % op->priv->manual_number_up == 0))
350 g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
354 preview_end_page (GtkPrintOperation *op,
355 GtkPrintContext *print_context)
359 cr = gtk_print_context_get_cairo_context (print_context);
361 if ((op->priv->manual_number_up < 2) ||
362 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
363 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
364 cairo_show_page (cr);
368 preview_end_run (GtkPrintOperation *op,
372 g_free (op->priv->page_ranges);
373 op->priv->page_ranges = NULL;
378 gtk_print_operation_set_property (GObject *object,
383 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
387 case PROP_DEFAULT_PAGE_SETUP:
388 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
390 case PROP_PRINT_SETTINGS:
391 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
394 gtk_print_operation_set_job_name (op, g_value_get_string (value));
397 gtk_print_operation_set_n_pages (op, g_value_get_int (value));
399 case PROP_CURRENT_PAGE:
400 gtk_print_operation_set_current_page (op, g_value_get_int (value));
402 case PROP_USE_FULL_PAGE:
403 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
405 case PROP_TRACK_PRINT_STATUS:
406 gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
409 gtk_print_operation_set_unit (op, g_value_get_enum (value));
411 case PROP_ALLOW_ASYNC:
412 gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
414 case PROP_SHOW_PROGRESS:
415 gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
417 case PROP_EXPORT_FILENAME:
418 gtk_print_operation_set_export_filename (op, g_value_get_string (value));
420 case PROP_CUSTOM_TAB_LABEL:
421 gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
423 case PROP_EMBED_PAGE_SETUP:
424 gtk_print_operation_set_embed_page_setup (op, g_value_get_boolean (value));
426 case PROP_HAS_SELECTION:
427 gtk_print_operation_set_has_selection (op, g_value_get_boolean (value));
429 case PROP_SUPPORT_SELECTION:
430 gtk_print_operation_set_support_selection (op, g_value_get_boolean (value));
433 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
439 gtk_print_operation_get_property (GObject *object,
444 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
445 GtkPrintOperationPrivate *priv = op->priv;
449 case PROP_DEFAULT_PAGE_SETUP:
450 g_value_set_object (value, priv->default_page_setup);
452 case PROP_PRINT_SETTINGS:
453 g_value_set_object (value, priv->print_settings);
456 g_value_set_string (value, priv->job_name);
459 g_value_set_int (value, priv->nr_of_pages);
461 case PROP_CURRENT_PAGE:
462 g_value_set_int (value, priv->current_page);
464 case PROP_USE_FULL_PAGE:
465 g_value_set_boolean (value, priv->use_full_page);
467 case PROP_TRACK_PRINT_STATUS:
468 g_value_set_boolean (value, priv->track_print_status);
471 g_value_set_enum (value, priv->unit);
473 case PROP_ALLOW_ASYNC:
474 g_value_set_boolean (value, priv->allow_async);
476 case PROP_SHOW_PROGRESS:
477 g_value_set_boolean (value, priv->show_progress);
479 case PROP_EXPORT_FILENAME:
480 g_value_set_string (value, priv->export_filename);
483 g_value_set_enum (value, priv->status);
485 case PROP_STATUS_STRING:
486 g_value_set_string (value, priv->status_string);
488 case PROP_CUSTOM_TAB_LABEL:
489 g_value_set_string (value, priv->custom_tab_label);
491 case PROP_EMBED_PAGE_SETUP:
492 g_value_set_boolean (value, priv->embed_page_setup);
494 case PROP_HAS_SELECTION:
495 g_value_set_boolean (value, priv->has_selection);
497 case PROP_SUPPORT_SELECTION:
498 g_value_set_boolean (value, priv->support_selection);
500 case PROP_N_PAGES_TO_PRINT:
501 g_value_set_int (value, priv->nr_of_pages_to_print);
504 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
509 struct _PrintPagesData
511 GtkPrintOperation *op;
512 gint uncollated_copies;
513 gint collated_copies;
514 gint uncollated, collated, total;
516 gint range, num_ranges;
517 GtkPageRange *ranges;
518 GtkPageRange one_range;
522 gint first_position, last_position;
529 gboolean initialized;
536 GtkPrintOperationPreview *preview;
537 GtkPrintContext *print_context;
539 cairo_surface_t *surface;
542 PrintPagesData *pages_data;
546 preview_print_idle_done (gpointer data)
548 GtkPrintOperation *op;
549 PreviewOp *pop = (PreviewOp *) data;
551 op = GTK_PRINT_OPERATION (pop->preview);
553 cairo_surface_finish (pop->surface);
554 /* Surface is destroyed in launch_preview */
555 _gtk_print_operation_platform_backend_launch_preview (op,
560 g_free (pop->filename);
562 gtk_print_operation_preview_end_preview (pop->preview);
564 g_object_unref (pop->pages_data->op);
565 g_free (pop->pages_data->pages);
566 g_free (pop->pages_data);
573 preview_print_idle (gpointer data)
576 GtkPrintOperation *op;
577 GtkPrintOperationPrivate *priv;
578 gboolean done = FALSE;
580 pop = (PreviewOp *) data;
581 op = GTK_PRINT_OPERATION (pop->preview);
585 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
587 if (!pop->pages_data->initialized)
589 pop->pages_data->initialized = TRUE;
590 prepare_data (pop->pages_data);
594 increment_page_sequence (pop->pages_data);
596 if (!pop->pages_data->done)
597 gtk_print_operation_preview_render_page (pop->preview, pop->pages_data->page);
599 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
607 preview_got_page_size (GtkPrintOperationPreview *preview,
608 GtkPrintContext *context,
609 GtkPageSetup *page_setup,
612 GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
615 _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
617 cr = gtk_print_context_get_cairo_context (pop->print_context);
618 _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
623 preview_ready (GtkPrintOperationPreview *preview,
624 GtkPrintContext *context,
627 pop->print_context = context;
629 g_object_ref (preview);
631 gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
634 preview_print_idle_done);
639 gtk_print_operation_preview_handler (GtkPrintOperation *op,
640 GtkPrintOperationPreview *preview,
641 GtkPrintContext *context,
644 gdouble dpi_x, dpi_y;
646 GtkPageSetup *page_setup;
649 pop = g_new0 (PreviewOp, 1);
650 pop->filename = NULL;
651 pop->preview = preview;
652 pop->parent = parent;
653 pop->pages_data = g_new0 (PrintPagesData, 1);
654 pop->pages_data->op = g_object_ref (GTK_PRINT_OPERATION (preview));
655 pop->pages_data->is_preview = TRUE;
657 page_setup = gtk_print_context_get_page_setup (context);
660 _gtk_print_operation_platform_backend_create_preview_surface (op,
665 if (pop->surface == NULL)
671 cr = cairo_create (pop->surface);
672 gtk_print_context_set_cairo_context (op->priv->print_context, cr,
676 g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
677 g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
683 gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
689 gtk_print_operation_done (GtkPrintOperation *operation,
690 GtkPrintOperationResult result)
692 GtkPrintOperationPrivate *priv = operation->priv;
694 if (priv->print_context)
696 g_object_unref (priv->print_context);
697 priv->print_context = NULL;
702 custom_widget_accumulator (GSignalInvocationHint *ihint,
704 const GValue *handler_return,
707 gboolean continue_emission;
710 widget = g_value_get_object (handler_return);
712 g_value_set_object (return_accu, widget);
713 continue_emission = (widget == NULL);
715 return continue_emission;
719 gtk_print_operation_class_init (GtkPrintOperationClass *class)
721 GObjectClass *gobject_class = (GObjectClass *)class;
723 gobject_class->set_property = gtk_print_operation_set_property;
724 gobject_class->get_property = gtk_print_operation_get_property;
725 gobject_class->finalize = gtk_print_operation_finalize;
727 class->preview = gtk_print_operation_preview_handler;
728 class->create_custom_widget = gtk_print_operation_create_custom_widget;
729 class->done = gtk_print_operation_done;
731 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
734 * GtkPrintOperation::done:
735 * @operation: the #GtkPrintOperation on which the signal was emitted
736 * @result: the result of the print operation
738 * Emitted when the print operation run has finished doing
739 * everything required for printing.
741 * @result gives you information about what happened during the run.
742 * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
743 * gtk_print_operation_get_error() for more information.
745 * If you enabled print status tracking then
746 * gtk_print_operation_is_finished() may still return %FALSE
747 * after #GtkPrintOperation::done was emitted.
752 g_signal_new (I_("done"),
753 G_TYPE_FROM_CLASS (gobject_class),
755 G_STRUCT_OFFSET (GtkPrintOperationClass, done),
757 g_cclosure_marshal_VOID__ENUM,
758 G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
761 * GtkPrintOperation::begin-print:
762 * @operation: the #GtkPrintOperation on which the signal was emitted
763 * @context: the #GtkPrintContext for the current operation
765 * Emitted after the user has finished changing print settings
766 * in the dialog, before the actual rendering starts.
768 * A typical use for ::begin-print is to use the parameters from the
769 * #GtkPrintContext and paginate the document accordingly, and then
770 * set the number of pages with gtk_print_operation_set_n_pages().
774 signals[BEGIN_PRINT] =
775 g_signal_new (I_("begin-print"),
776 G_TYPE_FROM_CLASS (gobject_class),
778 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
780 g_cclosure_marshal_VOID__OBJECT,
781 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
784 * GtkPrintOperation::paginate:
785 * @operation: the #GtkPrintOperation on which the signal was emitted
786 * @context: the #GtkPrintContext for the current operation
788 * Emitted after the #GtkPrintOperation::begin-print signal, but before
789 * the actual rendering starts. It keeps getting emitted until a connected
790 * signal handler returns %TRUE.
792 * The ::paginate signal is intended to be used for paginating a document
793 * in small chunks, to avoid blocking the user interface for a long
794 * time. The signal handler should update the number of pages using
795 * gtk_print_operation_set_n_pages(), and return %TRUE if the document
796 * has been completely paginated.
798 * If you don't need to do pagination in chunks, you can simply do
799 * it all in the ::begin-print handler, and set the number of pages
802 * Return value: %TRUE if pagination is complete
807 g_signal_new (I_("paginate"),
808 G_TYPE_FROM_CLASS (gobject_class),
810 G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
811 _gtk_boolean_handled_accumulator, NULL,
812 _gtk_marshal_BOOLEAN__OBJECT,
813 G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);
817 * GtkPrintOperation::request-page-setup:
818 * @operation: the #GtkPrintOperation on which the signal was emitted
819 * @context: the #GtkPrintContext for the current operation
820 * @page_nr: the number of the currently printed page (0-based)
821 * @setup: the #GtkPageSetup
823 * Emitted once for every page that is printed, to give
824 * the application a chance to modify the page setup. Any changes
825 * done to @setup will be in force only for printing this page.
829 signals[REQUEST_PAGE_SETUP] =
830 g_signal_new (I_("request-page-setup"),
831 G_TYPE_FROM_CLASS (gobject_class),
833 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
835 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
837 GTK_TYPE_PRINT_CONTEXT,
839 GTK_TYPE_PAGE_SETUP);
842 * GtkPrintOperation::draw-page:
843 * @operation: the #GtkPrintOperation on which the signal was emitted
844 * @context: the #GtkPrintContext for the current operation
845 * @page_nr: the number of the currently printed page (0-based)
847 * Emitted for every page that is printed. The signal handler
848 * must render the @page_nr's page onto the cairo context obtained
849 * from @context using gtk_print_context_get_cairo_context().
852 * draw_page (GtkPrintOperation *operation,
853 * GtkPrintContext *context,
855 * gpointer user_data)
858 * PangoLayout *layout;
859 * gdouble width, text_height;
860 * gint layout_height;
861 * PangoFontDescription *desc;
863 * cr = gtk_print_context_get_cairo_context (context);
864 * width = gtk_print_context_get_width (context);
866 * cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
868 * cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
871 * layout = gtk_print_context_create_pango_layout (context);
873 * desc = pango_font_description_from_string ("sans 14");
874 * pango_layout_set_font_description (layout, desc);
875 * pango_font_description_free (desc);
877 * pango_layout_set_text (layout, "some text", -1);
878 * pango_layout_set_width (layout, width * PANGO_SCALE);
879 * pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
881 * pango_layout_get_size (layout, NULL, &layout_height);
882 * text_height = (gdouble)layout_height / PANGO_SCALE;
884 * cairo_move_to (cr, width / 2, (HEADER_HEIGHT - text_height) / 2);
885 * pango_cairo_show_layout (cr, layout);
887 * g_object_unref (layout);
891 * Use gtk_print_operation_set_use_full_page() and
892 * gtk_print_operation_set_unit() before starting the print operation
893 * to set up the transformation of the cairo context according to your
899 g_signal_new (I_("draw-page"),
900 G_TYPE_FROM_CLASS (gobject_class),
902 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
904 _gtk_marshal_VOID__OBJECT_INT,
906 GTK_TYPE_PRINT_CONTEXT,
910 * GtkPrintOperation::end-print:
911 * @operation: the #GtkPrintOperation on which the signal was emitted
912 * @context: the #GtkPrintContext for the current operation
914 * Emitted after all pages have been rendered.
915 * A handler for this signal can clean up any resources that have
916 * been allocated in the #GtkPrintOperation::begin-print handler.
921 g_signal_new (I_("end-print"),
922 G_TYPE_FROM_CLASS (gobject_class),
924 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
926 g_cclosure_marshal_VOID__OBJECT,
927 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
930 * GtkPrintOperation::status-changed:
931 * @operation: the #GtkPrintOperation on which the signal was emitted
933 * Emitted at between the various phases of the print operation.
934 * See #GtkPrintStatus for the phases that are being discriminated.
935 * Use gtk_print_operation_get_status() to find out the current
940 signals[STATUS_CHANGED] =
941 g_signal_new (I_("status-changed"),
942 G_TYPE_FROM_CLASS (class),
944 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
946 g_cclosure_marshal_VOID__VOID,
951 * GtkPrintOperation::create-custom-widget:
952 * @operation: the #GtkPrintOperation on which the signal was emitted
954 * Emitted when displaying the print dialog. If you return a
955 * widget in a handler for this signal it will be added to a custom
956 * tab in the print dialog. You typically return a container widget
957 * with multiple widgets in it.
959 * The print dialog owns the returned widget, and its lifetime is not
960 * controlled by the application. However, the widget is guaranteed
961 * to stay around until the #GtkPrintOperation::custom-widget-apply
962 * signal is emitted on the operation. Then you can read out any
963 * information you need from the widgets.
965 * Returns: (transfer none): A custom widget that gets embedded in
966 * the print dialog, or %NULL
970 signals[CREATE_CUSTOM_WIDGET] =
971 g_signal_new (I_("create-custom-widget"),
972 G_TYPE_FROM_CLASS (class),
974 G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
975 custom_widget_accumulator, NULL,
976 _gtk_marshal_OBJECT__VOID,
980 * GtkPrintOperation::update-custom-widget:
981 * @operation: the #GtkPrintOperation on which the signal was emitted
982 * @widget: the custom widget added in create-custom-widget
983 * @setup: actual page setup
984 * @settings: actual print settings
986 * Emitted after change of selected printer. The actual page setup and
987 * print settings are passed to the custom widget, which can actualize
988 * itself according to this change.
992 signals[UPDATE_CUSTOM_WIDGET] =
993 g_signal_new (I_("update-custom-widget"),
994 G_TYPE_FROM_CLASS (class),
996 G_STRUCT_OFFSET (GtkPrintOperationClass, update_custom_widget),
998 _gtk_marshal_VOID__OBJECT_OBJECT_OBJECT,
999 G_TYPE_NONE, 3, GTK_TYPE_WIDGET, GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);
1002 * GtkPrintOperation::custom-widget-apply:
1003 * @operation: the #GtkPrintOperation on which the signal was emitted
1004 * @widget: the custom widget added in create-custom-widget
1006 * Emitted right before #GtkPrintOperation::begin-print if you added
1007 * a custom widget in the #GtkPrintOperation::create-custom-widget handler.
1008 * When you get this signal you should read the information from the
1009 * custom widgets, as the widgets are not guaraneed to be around at a
1014 signals[CUSTOM_WIDGET_APPLY] =
1015 g_signal_new (I_("custom-widget-apply"),
1016 G_TYPE_FROM_CLASS (class),
1018 G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
1020 g_cclosure_marshal_VOID__OBJECT,
1021 G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
1024 * GtkPrintOperation::preview:
1025 * @operation: the #GtkPrintOperation on which the signal was emitted
1026 * @preview: the #GtkPrintPreviewOperation for the current operation
1027 * @context: the #GtkPrintContext that will be used
1028 * @parent: (allow-none): the #GtkWindow to use as window parent, or %NULL
1030 * Gets emitted when a preview is requested from the native dialog.
1032 * The default handler for this signal uses an external viewer
1033 * application to preview.
1035 * To implement a custom print preview, an application must return
1036 * %TRUE from its handler for this signal. In order to use the
1037 * provided @context for the preview implementation, it must be
1038 * given a suitable cairo context with gtk_print_context_set_cairo_context().
1040 * The custom preview implementation can use
1041 * gtk_print_operation_preview_is_selected() and
1042 * gtk_print_operation_preview_render_page() to find pages which
1043 * are selected for print and render them. The preview must be
1044 * finished by calling gtk_print_operation_preview_end_preview()
1045 * (typically in response to the user clicking a close button).
1047 * Returns: %TRUE if the listener wants to take over control of the preview
1052 g_signal_new (I_("preview"),
1053 G_TYPE_FROM_CLASS (gobject_class),
1055 G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
1056 _gtk_boolean_handled_accumulator, NULL,
1057 _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
1059 GTK_TYPE_PRINT_OPERATION_PREVIEW,
1060 GTK_TYPE_PRINT_CONTEXT,
1065 * GtkPrintOperation:default-page-setup:
1067 * The #GtkPageSetup used by default.
1069 * This page setup will be used by gtk_print_operation_run(),
1070 * but it can be overridden on a per-page basis by connecting
1071 * to the #GtkPrintOperation::request-page-setup signal.
1075 g_object_class_install_property (gobject_class,
1076 PROP_DEFAULT_PAGE_SETUP,
1077 g_param_spec_object ("default-page-setup",
1078 P_("Default Page Setup"),
1079 P_("The GtkPageSetup used by default"),
1080 GTK_TYPE_PAGE_SETUP,
1081 GTK_PARAM_READWRITE));
1084 * GtkPrintOperation:print-settings:
1086 * The #GtkPrintSettings used for initializing the dialog.
1088 * Setting this property is typically used to re-establish
1089 * print settings from a previous print operation, see
1090 * gtk_print_operation_run().
1094 g_object_class_install_property (gobject_class,
1095 PROP_PRINT_SETTINGS,
1096 g_param_spec_object ("print-settings",
1097 P_("Print Settings"),
1098 P_("The GtkPrintSettings used for initializing the dialog"),
1099 GTK_TYPE_PRINT_SETTINGS,
1100 GTK_PARAM_READWRITE));
1103 * GtkPrintOperation:job-name:
1105 * A string used to identify the job (e.g. in monitoring
1106 * applications like eggcups).
1108 * If you don't set a job name, GTK+ picks a default one
1109 * by numbering successive print jobs.
1113 g_object_class_install_property (gobject_class,
1115 g_param_spec_string ("job-name",
1117 P_("A string used for identifying the print job."),
1119 GTK_PARAM_READWRITE));
1122 * GtkPrintOperation:n-pages:
1124 * The number of pages in the document.
1126 * This <emphasis>must</emphasis> be set to a positive number
1127 * before the rendering starts. It may be set in a
1128 * #GtkPrintOperation::begin-print signal hander.
1130 * Note that the page numbers passed to the
1131 * #GtkPrintOperation::request-page-setup and
1132 * #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1133 * the user chooses to print all pages, the last ::draw-page signal
1134 * will be for page @n_pages - 1.
1138 g_object_class_install_property (gobject_class,
1140 g_param_spec_int ("n-pages",
1141 P_("Number of Pages"),
1142 P_("The number of pages in the document."),
1146 GTK_PARAM_READWRITE));
1149 * GtkPrintOperation:current-page:
1151 * The current page in the document.
1153 * If this is set before gtk_print_operation_run(),
1154 * the user will be able to select to print only the current page.
1156 * Note that this only makes sense for pre-paginated documents.
1160 g_object_class_install_property (gobject_class,
1162 g_param_spec_int ("current-page",
1164 P_("The current page in the document"),
1168 GTK_PARAM_READWRITE));
1171 * GtkPrintOperation:use-full-page:
1173 * If %TRUE, the transformation for the cairo context obtained
1174 * from #GtkPrintContext puts the origin at the top left corner
1175 * of the page (which may not be the top left corner of the sheet,
1176 * depending on page orientation and the number of pages per sheet).
1177 * Otherwise, the origin is at the top left corner of the imageable
1178 * area (i.e. inside the margins).
1182 g_object_class_install_property (gobject_class,
1184 g_param_spec_boolean ("use-full-page",
1185 P_("Use full page"),
1186 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
1188 GTK_PARAM_READWRITE));
1192 * GtkPrintOperation:track-print-status:
1194 * If %TRUE, the print operation will try to continue report on
1195 * the status of the print job in the printer queues and printer.
1196 * This can allow your application to show things like "out of paper"
1197 * issues, and when the print job actually reaches the printer.
1198 * However, this is often implemented using polling, and should
1199 * not be enabled unless needed.
1203 g_object_class_install_property (gobject_class,
1204 PROP_TRACK_PRINT_STATUS,
1205 g_param_spec_boolean ("track-print-status",
1206 P_("Track Print Status"),
1207 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."),
1209 GTK_PARAM_READWRITE));
1213 * GtkPrintOperation:unit:
1215 * The transformation for the cairo context obtained from
1216 * #GtkPrintContext is set up in such a way that distances
1217 * are measured in units of @unit.
1221 g_object_class_install_property (gobject_class,
1223 g_param_spec_enum ("unit",
1225 P_("The unit in which distances can be measured in the context"),
1228 GTK_PARAM_READWRITE));
1232 * GtkPrintOperation:show-progress:
1234 * Determines whether to show a progress dialog during the
1239 g_object_class_install_property (gobject_class,
1241 g_param_spec_boolean ("show-progress",
1243 P_("TRUE if a progress dialog is shown while printing."),
1245 GTK_PARAM_READWRITE));
1248 * GtkPrintOperation:allow-async:
1250 * Determines whether the print operation may run asynchronously or not.
1252 * Some systems don't support asynchronous printing, but those that do
1253 * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
1254 * emit the #GtkPrintOperation::done signal when the operation is actually
1257 * The Windows port does not support asynchronous operation at all (this
1258 * is unlikely to change). On other platforms, all actions except for
1259 * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
1263 g_object_class_install_property (gobject_class,
1265 g_param_spec_boolean ("allow-async",
1267 P_("TRUE if print process may run asynchronous."),
1269 GTK_PARAM_READWRITE));
1272 * GtkPrintOperation:export-filename:
1274 * The name of a file to generate instead of showing the print dialog.
1275 * Currently, PDF is the only supported format.
1277 * The intended use of this property is for implementing
1278 * "Export to PDF" actions.
1280 * "Print to PDF" support is independent of this and is done
1281 * by letting the user pick the "Print to PDF" item from the
1282 * list of printers in the print dialog.
1286 g_object_class_install_property (gobject_class,
1287 PROP_EXPORT_FILENAME,
1288 g_param_spec_string ("export-filename",
1289 P_("Export filename"),
1290 P_("Export filename"),
1292 GTK_PARAM_READWRITE));
1295 * GtkPrintOperation:status:
1297 * The status of the print operation.
1301 g_object_class_install_property (gobject_class,
1303 g_param_spec_enum ("status",
1305 P_("The status of the print operation"),
1306 GTK_TYPE_PRINT_STATUS,
1307 GTK_PRINT_STATUS_INITIAL,
1308 GTK_PARAM_READABLE));
1311 * GtkPrintOperation:status-string:
1313 * A string representation of the status of the print operation.
1314 * The string is translated and suitable for displaying the print
1315 * status e.g. in a #GtkStatusbar.
1317 * See the #GtkPrintOperation:status property for a status value that
1318 * is suitable for programmatic use.
1322 g_object_class_install_property (gobject_class,
1324 g_param_spec_string ("status-string",
1325 P_("Status String"),
1326 P_("A human-readable description of the status"),
1328 GTK_PARAM_READABLE));
1332 * GtkPrintOperation:custom-tab-label:
1334 * Used as the label of the tab containing custom widgets.
1335 * Note that this property may be ignored on some platforms.
1337 * If this is %NULL, GTK+ uses a default label.
1341 g_object_class_install_property (gobject_class,
1342 PROP_CUSTOM_TAB_LABEL,
1343 g_param_spec_string ("custom-tab-label",
1344 P_("Custom tab label"),
1345 P_("Label for the tab containing custom widgets."),
1347 GTK_PARAM_READWRITE));
1350 * GtkPrintOperation:support-selection:
1352 * If %TRUE, the print operation will support print of selection.
1353 * This allows the print dialog to show a "Selection" button.
1357 g_object_class_install_property (gobject_class,
1358 PROP_SUPPORT_SELECTION,
1359 g_param_spec_boolean ("support-selection",
1360 P_("Support Selection"),
1361 P_("TRUE if the print operation will support print of selection."),
1363 GTK_PARAM_READWRITE));
1366 * GtkPrintOperation:has-selection:
1368 * Determines whether there is a selection in your application.
1369 * This can allow your application to print the selection.
1370 * This is typically used to make a "Selection" button sensitive.
1374 g_object_class_install_property (gobject_class,
1376 g_param_spec_boolean ("has-selection",
1377 P_("Has Selection"),
1378 P_("TRUE if a selection exists."),
1380 GTK_PARAM_READWRITE));
1384 * GtkPrintOperation:embed-page-setup:
1386 * If %TRUE, page size combo box and orientation combo box are embedded into page setup page.
1390 g_object_class_install_property (gobject_class,
1391 PROP_EMBED_PAGE_SETUP,
1392 g_param_spec_boolean ("embed-page-setup",
1393 P_("Embed Page Setup"),
1394 P_("TRUE if page setup combos are embedded in GtkPrintDialog"),
1396 GTK_PARAM_READWRITE));
1398 * GtkPrintOperation:n-pages-to-print:
1400 * The number of pages that will be printed.
1402 * Note that this value is set during print preparation phase
1403 * (%GTK_PRINT_STATUS_PREPARING), so this value should never be
1404 * get before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
1405 * You can connect to the #GtkPrintOperation::status-changed signal
1406 * and call gtk_print_operation_get_n_pages_to_print() when
1407 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
1408 * This is typically used to track the progress of print operation.
1412 g_object_class_install_property (gobject_class,
1413 PROP_N_PAGES_TO_PRINT,
1414 g_param_spec_int ("n-pages-to-print",
1415 P_("Number of Pages To Print"),
1416 P_("The number of pages that will be printed."),
1420 GTK_PARAM_READABLE));
1424 * gtk_print_operation_new:
1426 * Creates a new #GtkPrintOperation.
1428 * Returns: a new #GtkPrintOperation
1433 gtk_print_operation_new (void)
1435 GtkPrintOperation *print_operation;
1437 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
1439 return print_operation;
1443 * gtk_print_operation_set_default_page_setup:
1444 * @op: a #GtkPrintOperation
1445 * @default_page_setup: (allow-none): a #GtkPageSetup, or %NULL
1447 * Makes @default_page_setup the default page setup for @op.
1449 * This page setup will be used by gtk_print_operation_run(),
1450 * but it can be overridden on a per-page basis by connecting
1451 * to the #GtkPrintOperation::request-page-setup signal.
1456 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
1457 GtkPageSetup *default_page_setup)
1459 GtkPrintOperationPrivate *priv;
1461 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1462 g_return_if_fail (default_page_setup == NULL ||
1463 GTK_IS_PAGE_SETUP (default_page_setup));
1467 if (default_page_setup != priv->default_page_setup)
1469 if (default_page_setup)
1470 g_object_ref (default_page_setup);
1472 if (priv->default_page_setup)
1473 g_object_unref (priv->default_page_setup);
1475 priv->default_page_setup = default_page_setup;
1477 g_object_notify (G_OBJECT (op), "default-page-setup");
1482 * gtk_print_operation_get_default_page_setup:
1483 * @op: a #GtkPrintOperation
1485 * Returns the default page setup, see
1486 * gtk_print_operation_set_default_page_setup().
1488 * Returns: (transfer none): the default page setup
1493 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
1495 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1497 return op->priv->default_page_setup;
1502 * gtk_print_operation_set_print_settings:
1503 * @op: a #GtkPrintOperation
1504 * @print_settings: (allow-none): #GtkPrintSettings
1506 * Sets the print settings for @op. This is typically used to
1507 * re-establish print settings from a previous print operation,
1508 * see gtk_print_operation_run().
1513 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
1514 GtkPrintSettings *print_settings)
1516 GtkPrintOperationPrivate *priv;
1518 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1519 g_return_if_fail (print_settings == NULL ||
1520 GTK_IS_PRINT_SETTINGS (print_settings));
1524 if (print_settings != priv->print_settings)
1527 g_object_ref (print_settings);
1529 if (priv->print_settings)
1530 g_object_unref (priv->print_settings);
1532 priv->print_settings = print_settings;
1534 g_object_notify (G_OBJECT (op), "print-settings");
1539 * gtk_print_operation_get_print_settings:
1540 * @op: a #GtkPrintOperation
1542 * Returns the current print settings.
1544 * Note that the return value is %NULL until either
1545 * gtk_print_operation_set_print_settings() or
1546 * gtk_print_operation_run() have been called.
1548 * Return value: (transfer none): the current print settings of @op.
1553 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
1555 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
1557 return op->priv->print_settings;
1561 * gtk_print_operation_set_job_name:
1562 * @op: a #GtkPrintOperation
1563 * @job_name: a string that identifies the print job
1565 * Sets the name of the print job. The name is used to identify
1566 * the job (e.g. in monitoring applications like eggcups).
1568 * If you don't set a job name, GTK+ picks a default one by
1569 * numbering successive print jobs.
1574 gtk_print_operation_set_job_name (GtkPrintOperation *op,
1575 const gchar *job_name)
1577 GtkPrintOperationPrivate *priv;
1579 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1580 g_return_if_fail (job_name != NULL);
1584 g_free (priv->job_name);
1585 priv->job_name = g_strdup (job_name);
1587 g_object_notify (G_OBJECT (op), "job-name");
1591 * gtk_print_operation_set_n_pages:
1592 * @op: a #GtkPrintOperation
1593 * @n_pages: the number of pages
1595 * Sets the number of pages in the document.
1597 * This <emphasis>must</emphasis> be set to a positive number
1598 * before the rendering starts. It may be set in a
1599 * #GtkPrintOperation::begin-print signal hander.
1601 * Note that the page numbers passed to the
1602 * #GtkPrintOperation::request-page-setup
1603 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if
1604 * the user chooses to print all pages, the last ::draw-page signal
1605 * will be for page @n_pages - 1.
1610 gtk_print_operation_set_n_pages (GtkPrintOperation *op,
1613 GtkPrintOperationPrivate *priv;
1615 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1616 g_return_if_fail (n_pages > 0);
1619 g_return_if_fail (priv->current_page == -1 ||
1620 priv->current_page < n_pages);
1622 if (priv->nr_of_pages != n_pages)
1624 priv->nr_of_pages = n_pages;
1626 g_object_notify (G_OBJECT (op), "n-pages");
1631 * gtk_print_operation_set_current_page:
1632 * @op: a #GtkPrintOperation
1633 * @current_page: the current page, 0-based
1635 * Sets the current page.
1637 * If this is called before gtk_print_operation_run(),
1638 * the user will be able to select to print only the current page.
1640 * Note that this only makes sense for pre-paginated documents.
1645 gtk_print_operation_set_current_page (GtkPrintOperation *op,
1648 GtkPrintOperationPrivate *priv;
1650 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1651 g_return_if_fail (current_page >= 0);
1654 g_return_if_fail (priv->nr_of_pages == -1 ||
1655 current_page < priv->nr_of_pages);
1657 if (priv->current_page != current_page)
1659 priv->current_page = current_page;
1661 g_object_notify (G_OBJECT (op), "current-page");
1666 * gtk_print_operation_set_use_full_page:
1667 * @op: a #GtkPrintOperation
1668 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
1670 * If @full_page is %TRUE, the transformation for the cairo context
1671 * obtained from #GtkPrintContext puts the origin at the top left
1672 * corner of the page (which may not be the top left corner of the
1673 * sheet, depending on page orientation and the number of pages per
1674 * sheet). Otherwise, the origin is at the top left corner of the
1675 * imageable area (i.e. inside the margins).
1680 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
1683 GtkPrintOperationPrivate *priv;
1685 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1687 full_page = full_page != FALSE;
1691 if (priv->use_full_page != full_page)
1693 priv->use_full_page = full_page;
1695 g_object_notify (G_OBJECT (op), "use-full-page");
1700 * gtk_print_operation_set_unit:
1701 * @op: a #GtkPrintOperation
1702 * @unit: the unit to use
1704 * Sets up the transformation for the cairo context obtained from
1705 * #GtkPrintContext in such a way that distances are measured in
1711 gtk_print_operation_set_unit (GtkPrintOperation *op,
1714 GtkPrintOperationPrivate *priv;
1716 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1720 if (priv->unit != unit)
1724 g_object_notify (G_OBJECT (op), "unit");
1729 * gtk_print_operation_set_track_print_status:
1730 * @op: a #GtkPrintOperation
1731 * @track_status: %TRUE to track status after printing
1733 * If track_status is %TRUE, the print operation will try to continue report
1734 * on the status of the print job in the printer queues and printer. This
1735 * can allow your application to show things like "out of paper" issues,
1736 * and when the print job actually reaches the printer.
1738 * This function is often implemented using some form of polling, so it should
1739 * not be enabled unless needed.
1744 gtk_print_operation_set_track_print_status (GtkPrintOperation *op,
1745 gboolean track_status)
1747 GtkPrintOperationPrivate *priv;
1749 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1753 if (priv->track_print_status != track_status)
1755 priv->track_print_status = track_status;
1757 g_object_notify (G_OBJECT (op), "track-print-status");
1762 _gtk_print_operation_set_status (GtkPrintOperation *op,
1763 GtkPrintStatus status,
1764 const gchar *string)
1766 GtkPrintOperationPrivate *priv = op->priv;
1767 static const gchar *status_strs[] = {
1768 NC_("print operation status", "Initial state"),
1769 NC_("print operation status", "Preparing to print"),
1770 NC_("print operation status", "Generating data"),
1771 NC_("print operation status", "Sending data"),
1772 NC_("print operation status", "Waiting"),
1773 NC_("print operation status", "Blocking on issue"),
1774 NC_("print operation status", "Printing"),
1775 NC_("print operation status", "Finished"),
1776 NC_("print operation status", "Finished with error")
1779 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
1780 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
1783 string = g_dpgettext2 (GETTEXT_PACKAGE, "print operation status", status_strs[status]);
1785 if (priv->status == status &&
1786 strcmp (string, priv->status_string) == 0)
1789 g_free (priv->status_string);
1790 priv->status_string = g_strdup (string);
1791 priv->status = status;
1793 g_object_notify (G_OBJECT (op), "status");
1794 g_object_notify (G_OBJECT (op), "status-string");
1796 g_signal_emit (op, signals[STATUS_CHANGED], 0);
1801 * gtk_print_operation_get_status:
1802 * @op: a #GtkPrintOperation
1804 * Returns the status of the print operation.
1805 * Also see gtk_print_operation_get_status_string().
1807 * Return value: the status of the print operation
1812 gtk_print_operation_get_status (GtkPrintOperation *op)
1814 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1815 GTK_PRINT_STATUS_FINISHED_ABORTED);
1817 return op->priv->status;
1821 * gtk_print_operation_get_status_string:
1822 * @op: a #GtkPrintOperation
1824 * Returns a string representation of the status of the
1825 * print operation. The string is translated and suitable
1826 * for displaying the print status e.g. in a #GtkStatusbar.
1828 * Use gtk_print_operation_get_status() to obtain a status
1829 * value that is suitable for programmatic use.
1831 * Return value: a string representation of the status
1832 * of the print operation
1837 gtk_print_operation_get_status_string (GtkPrintOperation *op)
1839 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
1841 return op->priv->status_string;
1845 * gtk_print_operation_is_finished:
1846 * @op: a #GtkPrintOperation
1848 * A convenience function to find out if the print operation
1849 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
1850 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
1852 * Note: when you enable print status tracking the print operation
1853 * can be in a non-finished state even after done has been called, as
1854 * the operation status then tracks the print job status on the printer.
1856 * Return value: %TRUE, if the print operation is finished.
1861 gtk_print_operation_is_finished (GtkPrintOperation *op)
1863 GtkPrintOperationPrivate *priv;
1865 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
1869 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
1870 priv->status == GTK_PRINT_STATUS_FINISHED;
1874 * gtk_print_operation_set_show_progress:
1875 * @op: a #GtkPrintOperation
1876 * @show_progress: %TRUE to show a progress dialog
1878 * If @show_progress is %TRUE, the print operation will show a
1879 * progress dialog during the print operation.
1884 gtk_print_operation_set_show_progress (GtkPrintOperation *op,
1885 gboolean show_progress)
1887 GtkPrintOperationPrivate *priv;
1889 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1893 show_progress = show_progress != FALSE;
1895 if (priv->show_progress != show_progress)
1897 priv->show_progress = show_progress;
1899 g_object_notify (G_OBJECT (op), "show-progress");
1904 * gtk_print_operation_set_allow_async:
1905 * @op: a #GtkPrintOperation
1906 * @allow_async: %TRUE to allow asynchronous operation
1908 * Sets whether the gtk_print_operation_run() may return
1909 * before the print operation is completed. Note that
1910 * some platforms may not allow asynchronous operation.
1915 gtk_print_operation_set_allow_async (GtkPrintOperation *op,
1916 gboolean allow_async)
1918 GtkPrintOperationPrivate *priv;
1920 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1924 allow_async = allow_async != FALSE;
1926 if (priv->allow_async != allow_async)
1928 priv->allow_async = allow_async;
1930 g_object_notify (G_OBJECT (op), "allow-async");
1936 * gtk_print_operation_set_custom_tab_label:
1937 * @op: a #GtkPrintOperation
1938 * @label: (allow-none): the label to use, or %NULL to use the default label
1940 * Sets the label for the tab holding custom widgets.
1945 gtk_print_operation_set_custom_tab_label (GtkPrintOperation *op,
1948 GtkPrintOperationPrivate *priv;
1950 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1954 g_free (priv->custom_tab_label);
1955 priv->custom_tab_label = g_strdup (label);
1957 g_object_notify (G_OBJECT (op), "custom-tab-label");
1962 * gtk_print_operation_set_export_filename:
1963 * @op: a #GtkPrintOperation
1964 * @filename: (type filename): the filename for the exported file
1966 * Sets up the #GtkPrintOperation to generate a file instead
1967 * of showing the print dialog. The indended use of this function
1968 * is for implementing "Export to PDF" actions. Currently, PDF
1969 * is the only supported format.
1971 * "Print to PDF" support is independent of this and is done
1972 * by letting the user pick the "Print to PDF" item from the list
1973 * of printers in the print dialog.
1978 gtk_print_operation_set_export_filename (GtkPrintOperation *op,
1979 const gchar *filename)
1981 GtkPrintOperationPrivate *priv;
1983 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1987 g_free (priv->export_filename);
1988 priv->export_filename = g_strdup (filename);
1990 g_object_notify (G_OBJECT (op), "export-filename");
1993 /* Creates the initial page setup used for printing unless the
1994 * app overrides this on a per-page basis using request_page_setup.
1996 * Data is taken from, in order, if existing:
1998 * PrintSettings returned from the print dialog
1999 * (initial dialog values are set from default_page_setup
2000 * if unset in app specified print_settings)
2001 * default_page_setup
2002 * per-locale default setup
2004 static GtkPageSetup *
2005 create_page_setup (GtkPrintOperation *op)
2007 GtkPrintOperationPrivate *priv = op->priv;
2008 GtkPageSetup *page_setup;
2009 GtkPrintSettings *settings;
2011 if (priv->default_page_setup)
2012 page_setup = gtk_page_setup_copy (priv->default_page_setup);
2014 page_setup = gtk_page_setup_new ();
2016 settings = priv->print_settings;
2019 GtkPaperSize *paper_size;
2021 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
2022 gtk_page_setup_set_orientation (page_setup,
2023 gtk_print_settings_get_orientation (settings));
2026 paper_size = gtk_print_settings_get_paper_size (settings);
2029 gtk_page_setup_set_paper_size (page_setup, paper_size);
2030 gtk_paper_size_free (paper_size);
2033 /* TODO: Margins? */
2040 pdf_start_page (GtkPrintOperation *op,
2041 GtkPrintContext *print_context,
2042 GtkPageSetup *page_setup)
2044 GtkPaperSize *paper_size;
2045 cairo_surface_t *surface = op->priv->platform_data;
2048 paper_size = gtk_page_setup_get_paper_size (page_setup);
2050 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
2051 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
2053 cairo_pdf_surface_set_size (surface, w, h);
2057 pdf_end_page (GtkPrintOperation *op,
2058 GtkPrintContext *print_context)
2062 cr = gtk_print_context_get_cairo_context (print_context);
2064 if ((op->priv->manual_number_up < 2) ||
2065 ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
2066 (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
2067 cairo_show_page (cr);
2071 pdf_end_run (GtkPrintOperation *op,
2075 GtkPrintOperationPrivate *priv = op->priv;
2076 cairo_surface_t *surface = priv->platform_data;
2078 cairo_surface_finish (surface);
2079 cairo_surface_destroy (surface);
2081 priv->platform_data = NULL;
2082 priv->free_platform_data = NULL;
2085 static GtkPrintOperationResult
2086 run_pdf (GtkPrintOperation *op,
2090 GtkPrintOperationPrivate *priv = op->priv;
2091 GtkPageSetup *page_setup;
2092 cairo_surface_t *surface;
2094 gdouble width, height;
2096 priv->print_context = _gtk_print_context_new (op);
2098 page_setup = create_page_setup (op);
2099 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
2101 /* This will be overwritten later by the non-default size, but
2102 we need to pass some size: */
2103 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2104 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2105 g_object_unref (page_setup);
2107 surface = cairo_pdf_surface_create (priv->export_filename,
2109 if (cairo_surface_status (surface) != CAIRO_STATUS_SUCCESS)
2111 g_set_error_literal (&priv->error,
2113 GTK_PRINT_ERROR_GENERAL,
2114 cairo_status_to_string (cairo_surface_status (surface)));
2116 return GTK_PRINT_OPERATION_RESULT_ERROR;
2119 /* this would crash on a nil surface */
2120 cairo_surface_set_fallback_resolution (surface, 300, 300);
2122 priv->platform_data = surface;
2123 priv->free_platform_data = (GDestroyNotify) cairo_surface_destroy;
2125 cr = cairo_create (surface);
2126 gtk_print_context_set_cairo_context (op->priv->print_context,
2131 priv->print_pages = GTK_PRINT_PAGES_ALL;
2132 priv->page_ranges = NULL;
2133 priv->num_page_ranges = 0;
2135 priv->manual_num_copies = 1;
2136 priv->manual_collation = FALSE;
2137 priv->manual_reverse = FALSE;
2138 priv->manual_page_set = GTK_PAGE_SET_ALL;
2139 priv->manual_scale = 1.0;
2140 priv->manual_orientation = TRUE;
2141 priv->manual_number_up = 1;
2142 priv->manual_number_up_layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2146 priv->start_page = pdf_start_page;
2147 priv->end_page = pdf_end_page;
2148 priv->end_run = pdf_end_run;
2150 return GTK_PRINT_OPERATION_RESULT_APPLY;
2155 clamp_page_ranges (PrintPagesData *data)
2157 GtkPrintOperationPrivate *priv;
2158 gint num_of_correct_ranges;
2161 priv = data->op->priv;
2163 num_of_correct_ranges = 0;
2165 for (i = 0; i < data->num_ranges; i++)
2166 if ((data->ranges[i].start >= 0) &&
2167 (data->ranges[i].start < priv->nr_of_pages) &&
2168 (data->ranges[i].end >= 0) &&
2169 (data->ranges[i].end < priv->nr_of_pages))
2171 data->ranges[num_of_correct_ranges] = data->ranges[i];
2172 num_of_correct_ranges++;
2174 else if ((data->ranges[i].start >= 0) &&
2175 (data->ranges[i].start < priv->nr_of_pages) &&
2176 (data->ranges[i].end >= priv->nr_of_pages))
2178 data->ranges[i].end = priv->nr_of_pages - 1;
2179 data->ranges[num_of_correct_ranges] = data->ranges[i];
2180 num_of_correct_ranges++;
2182 else if ((data->ranges[i].end >= 0) &&
2183 (data->ranges[i].end < priv->nr_of_pages) &&
2184 (data->ranges[i].start < 0))
2186 data->ranges[i].start = 0;
2187 data->ranges[num_of_correct_ranges] = data->ranges[i];
2188 num_of_correct_ranges++;
2191 data->num_ranges = num_of_correct_ranges;
2195 increment_page_sequence (PrintPagesData *data)
2197 GtkPrintOperationPrivate *priv = data->op->priv;
2200 if (data->total == -1)
2206 /* check whether we reached last position */
2207 if (priv->page_position == data->last_position &&
2208 !(data->collated_copies > 1 && data->collated < (data->collated_copies - 1)))
2210 if (data->uncollated_copies > 1 && data->uncollated < (data->uncollated_copies - 1))
2212 priv->page_position = data->first_position;
2213 data->sheet = data->first_sheet;
2224 if (priv->manual_reverse)
2229 /* changing sheet */
2230 if (priv->manual_number_up < 2 ||
2231 (priv->page_position + 1) % priv->manual_number_up == 0 ||
2232 priv->page_position == data->last_position ||
2233 priv->page_position == priv->nr_of_pages_to_print - 1)
2235 /* check whether to print the same sheet again */
2236 if (data->collated_copies > 1)
2238 if (data->collated < (data->collated_copies - 1))
2242 priv->page_position = data->sheet * priv->manual_number_up;
2244 if (priv->page_position < 0 ||
2245 priv->page_position >= priv->nr_of_pages_to_print ||
2247 data->sheet >= data->num_of_sheets)
2253 data->page = data->pages[priv->page_position];
2261 if (priv->manual_page_set == GTK_PAGE_SET_ODD ||
2262 priv->manual_page_set == GTK_PAGE_SET_EVEN)
2263 data->sheet += 2 * inc;
2267 priv->page_position = data->sheet * priv->manual_number_up;
2270 priv->page_position += 1;
2274 if (priv->page_position < 0 ||
2275 priv->page_position >= priv->nr_of_pages_to_print ||
2277 data->sheet >= data->num_of_sheets)
2283 data->page = data->pages[priv->page_position];
2289 print_pages_idle_done (gpointer user_data)
2291 PrintPagesData *data;
2292 GtkPrintOperationPrivate *priv;
2294 data = (PrintPagesData*)user_data;
2295 priv = data->op->priv;
2297 priv->print_pages_idle_id = 0;
2299 if (priv->show_progress_timeout_id > 0)
2301 g_source_remove (priv->show_progress_timeout_id);
2302 priv->show_progress_timeout_id = 0;
2306 gtk_widget_destroy (data->progress);
2308 if (priv->rloop && !data->is_preview)
2309 g_main_loop_quit (priv->rloop);
2311 if (!data->is_preview)
2313 GtkPrintOperationResult result;
2316 result = GTK_PRINT_OPERATION_RESULT_ERROR;
2317 else if (priv->cancelled)
2318 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
2320 result = GTK_PRINT_OPERATION_RESULT_APPLY;
2322 g_signal_emit (data->op, signals[DONE], 0, result);
2325 g_object_unref (data->op);
2326 g_free (data->pages);
2331 update_progress (PrintPagesData *data)
2333 GtkPrintOperationPrivate *priv;
2336 priv = data->op->priv;
2340 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2342 if (priv->nr_of_pages_to_print > 0)
2343 text = g_strdup_printf (_("Preparing %d"), priv->nr_of_pages_to_print);
2345 text = g_strdup (_("Preparing"));
2347 else if (priv->status == GTK_PRINT_STATUS_GENERATING_DATA)
2348 text = g_strdup_printf (_("Printing %d"), data->total);
2352 g_object_set (data->progress, "text", text, NULL);
2359 * gtk_print_operation_set_defer_drawing:
2360 * @op: a #GtkPrintOperation
2362 * Sets up the #GtkPrintOperation to wait for calling of
2363 * gtk_print_operation_draw_page_finish() from application. It can
2364 * be used for drawing page in another thread.
2366 * This function must be called in the callback of "draw-page" signal.
2371 gtk_print_operation_set_defer_drawing (GtkPrintOperation *op)
2373 GtkPrintOperationPrivate *priv = op->priv;
2375 g_return_if_fail (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING);
2377 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DEFERRED_DRAWING;
2381 * gtk_print_operation_set_embed_page_setup:
2382 * @op: a #GtkPrintOperation
2383 * @embed: %TRUE to embed page setup selection in the #GtkPrintDialog
2385 * Embed page size combo box and orientation combo box into page setup page.
2386 * Selected page setup is stored as default page setup in #GtkPrintOperation.
2391 gtk_print_operation_set_embed_page_setup (GtkPrintOperation *op,
2394 GtkPrintOperationPrivate *priv;
2396 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
2400 embed = embed != FALSE;
2401 if (priv->embed_page_setup != embed)
2403 priv->embed_page_setup = embed;
2404 g_object_notify (G_OBJECT (op), "embed-page-setup");
2409 * gtk_print_operation_get_embed_page_setup:
2410 * @op: a #GtkPrintOperation
2412 * Gets the value of #GtkPrintOperation::embed-page-setup property.
2414 * Returns: whether page setup selection combos are embedded
2419 gtk_print_operation_get_embed_page_setup (GtkPrintOperation *op)
2421 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
2423 return op->priv->embed_page_setup;
2427 * gtk_print_operation_draw_page_finish:
2428 * @op: a #GtkPrintOperation
2430 * Signalize that drawing of particular page is complete.
2432 * It is called after completion of page drawing (e.g. drawing in another
2434 * If gtk_print_operation_set_defer_drawing() was called before, then this function
2435 * has to be called by application. In another case it is called by the library
2441 gtk_print_operation_draw_page_finish (GtkPrintOperation *op)
2443 GtkPrintOperationPrivate *priv = op->priv;
2444 GtkPageSetup *page_setup;
2445 GtkPrintContext *print_context;
2448 print_context = priv->print_context;
2449 page_setup = gtk_print_context_get_page_setup (print_context);
2451 cr = gtk_print_context_get_cairo_context (print_context);
2453 priv->end_page (op, print_context);
2457 g_object_unref (page_setup);
2459 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;
2463 common_render_page (GtkPrintOperation *op,
2466 GtkPrintOperationPrivate *priv = op->priv;
2467 GtkPageSetup *page_setup;
2468 GtkPrintContext *print_context;
2471 print_context = priv->print_context;
2473 page_setup = create_page_setup (op);
2475 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0,
2476 print_context, page_nr, page_setup);
2478 _gtk_print_context_set_page_setup (print_context, page_setup);
2480 priv->start_page (op, print_context, page_setup);
2482 cr = gtk_print_context_get_cairo_context (print_context);
2485 if (priv->manual_scale != 1.0 && priv->manual_number_up <= 1)
2488 priv->manual_scale);
2490 if (priv->manual_orientation)
2491 _gtk_print_context_rotate_according_to_orientation (print_context);
2493 if (priv->manual_number_up > 1)
2495 GtkPageOrientation orientation;
2496 GtkPageSetup *page_setup;
2497 gdouble paper_width, paper_height;
2498 gdouble page_width, page_height;
2499 gdouble context_width, context_height;
2500 gdouble bottom_margin, top_margin, left_margin, right_margin;
2501 gdouble x_step, y_step;
2502 gdouble x_scale, y_scale, scale;
2503 gdouble horizontal_offset = 0.0, vertical_offset = 0.0;
2504 gint columns, rows, x, y, tmp_length;
2506 page_setup = gtk_print_context_get_page_setup (print_context);
2507 orientation = gtk_page_setup_get_orientation (page_setup);
2509 top_margin = gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_POINTS);
2510 bottom_margin = gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_POINTS);
2511 left_margin = gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_POINTS);
2512 right_margin = gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_POINTS);
2514 paper_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
2515 paper_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
2517 context_width = gtk_print_context_get_width (print_context);
2518 context_height = gtk_print_context_get_height (print_context);
2520 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2521 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2523 page_width = paper_width - (left_margin + right_margin);
2524 page_height = paper_height - (top_margin + bottom_margin);
2528 page_width = paper_width - (top_margin + bottom_margin);
2529 page_height = paper_height - (left_margin + right_margin);
2532 if (orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
2533 orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
2534 cairo_translate (cr, left_margin, top_margin);
2536 cairo_translate (cr, top_margin, left_margin);
2538 switch (priv->manual_number_up)
2566 if (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE ||
2567 orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE)
2569 tmp_length = columns;
2574 switch (priv->manual_number_up_layout)
2576 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
2577 x = priv->page_position % columns;
2578 y = (priv->page_position / columns) % rows;
2580 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
2581 x = priv->page_position % columns;
2582 y = rows - 1 - (priv->page_position / columns) % rows;
2584 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
2585 x = columns - 1 - priv->page_position % columns;
2586 y = (priv->page_position / columns) % rows;
2588 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
2589 x = columns - 1 - priv->page_position % columns;
2590 y = rows - 1 - (priv->page_position / columns) % rows;
2592 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
2593 x = (priv->page_position / rows) % columns;
2594 y = priv->page_position % rows;
2596 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
2597 x = columns - 1 - (priv->page_position / rows) % columns;
2598 y = priv->page_position % rows;
2600 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
2601 x = (priv->page_position / rows) % columns;
2602 y = rows - 1 - priv->page_position % rows;
2604 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
2605 x = columns - 1 - (priv->page_position / rows) % columns;
2606 y = rows - 1 - priv->page_position % rows;
2609 g_assert_not_reached();
2614 if (priv->manual_number_up == 4 || priv->manual_number_up == 9 || priv->manual_number_up == 16)
2616 x_scale = page_width / (columns * paper_width);
2617 y_scale = page_height / (rows * paper_height);
2619 scale = x_scale < y_scale ? x_scale : y_scale;
2621 x_step = paper_width * (x_scale / scale);
2622 y_step = paper_height * (y_scale / scale);
2624 if ((left_margin + right_margin) > 0)
2626 horizontal_offset = left_margin * (x_step - context_width) / (left_margin + right_margin);
2627 vertical_offset = top_margin * (y_step - context_height) / (top_margin + bottom_margin);
2631 horizontal_offset = (x_step - context_width) / 2.0;
2632 vertical_offset = (y_step - context_height) / 2.0;
2635 cairo_scale (cr, scale, scale);
2637 cairo_translate (cr,
2638 x * x_step + horizontal_offset,
2639 y * y_step + vertical_offset);
2641 if (priv->manual_scale != 1.0)
2642 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2645 if (priv->manual_number_up == 2 || priv->manual_number_up == 6)
2647 x_scale = page_height / (columns * paper_width);
2648 y_scale = page_width / (rows * paper_height);
2650 scale = x_scale < y_scale ? x_scale : y_scale;
2652 horizontal_offset = (paper_width * (x_scale / scale) - paper_width) / 2.0 * columns;
2653 vertical_offset = (paper_height * (y_scale / scale) - paper_height) / 2.0 * rows;
2655 if (!priv->use_full_page)
2657 horizontal_offset -= right_margin;
2658 vertical_offset += top_margin;
2661 cairo_scale (cr, scale, scale);
2663 cairo_translate (cr,
2664 y * paper_height + vertical_offset,
2665 (columns - x) * paper_width + horizontal_offset);
2667 if (priv->manual_scale != 1.0)
2668 cairo_scale (cr, priv->manual_scale, priv->manual_scale);
2670 cairo_rotate (cr, - G_PI / 2);
2674 if (!priv->use_full_page)
2675 _gtk_print_context_translate_into_margin (print_context);
2677 priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_DRAWING;
2679 g_signal_emit (op, signals[DRAW_PAGE], 0,
2680 print_context, page_nr);
2682 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_DRAWING)
2683 gtk_print_operation_draw_page_finish (op);
2687 prepare_data (PrintPagesData *data)
2689 GtkPrintOperationPrivate *priv;
2690 GtkPageSetup *page_setup;
2693 priv = data->op->priv;
2695 if (priv->manual_collation)
2697 data->uncollated_copies = priv->manual_num_copies;
2698 data->collated_copies = 1;
2702 data->uncollated_copies = 1;
2703 data->collated_copies = priv->manual_num_copies;
2706 if (!data->initialized)
2708 data->initialized = TRUE;
2709 page_setup = create_page_setup (data->op);
2710 _gtk_print_context_set_page_setup (priv->print_context,
2712 g_object_unref (page_setup);
2714 g_signal_emit (data->op, signals[BEGIN_PRINT], 0, priv->print_context);
2719 if (g_signal_has_handler_pending (data->op, signals[PAGINATE], 0, FALSE))
2721 gboolean paginated = FALSE;
2723 g_signal_emit (data->op, signals[PAGINATE], 0, priv->print_context, &paginated);
2728 /* Initialize parts of PrintPagesData that depend on nr_of_pages
2730 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
2732 if (priv->page_ranges == NULL)
2734 g_warning ("no pages to print");
2735 priv->cancelled = TRUE;
2738 data->ranges = priv->page_ranges;
2739 data->num_ranges = priv->num_page_ranges;
2740 for (i = 0; i < data->num_ranges; i++)
2741 if (data->ranges[i].end == -1 ||
2742 data->ranges[i].end >= priv->nr_of_pages)
2743 data->ranges[i].end = priv->nr_of_pages - 1;
2745 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
2746 priv->current_page != -1)
2748 data->ranges = &data->one_range;
2749 data->num_ranges = 1;
2750 data->ranges[0].start = priv->current_page;
2751 data->ranges[0].end = priv->current_page;
2755 data->ranges = &data->one_range;
2756 data->num_ranges = 1;
2757 data->ranges[0].start = 0;
2758 data->ranges[0].end = priv->nr_of_pages - 1;
2761 clamp_page_ranges (data);
2763 if (data->num_ranges < 1)
2765 priv->cancelled = TRUE;
2769 priv->nr_of_pages_to_print = 0;
2770 for (i = 0; i < data->num_ranges; i++)
2771 priv->nr_of_pages_to_print += data->ranges[i].end - data->ranges[i].start + 1;
2773 data->pages = g_new (gint, priv->nr_of_pages_to_print);
2775 for (i = 0; i < data->num_ranges; i++)
2776 for (j = data->ranges[i].start; j <= data->ranges[i].end; j++)
2778 data->pages[counter] = j;
2784 data->uncollated = 0;
2786 if (priv->manual_number_up > 1)
2788 if (priv->nr_of_pages_to_print % priv->manual_number_up == 0)
2789 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up;
2791 data->num_of_sheets = priv->nr_of_pages_to_print / priv->manual_number_up + 1;
2794 data->num_of_sheets = priv->nr_of_pages_to_print;
2796 if (priv->manual_reverse)
2798 /* data->sheet is 0-based */
2799 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2800 data->sheet = (data->num_of_sheets - 1) - (data->num_of_sheets - 1) % 2;
2801 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2802 data->sheet = (data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2);
2804 data->sheet = data->num_of_sheets - 1;
2808 /* data->sheet is 0-based */
2809 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2811 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2813 if (data->num_of_sheets > 1)
2822 priv->page_position = data->sheet * priv->manual_number_up;
2824 if (priv->page_position < 0 || priv->page_position >= priv->nr_of_pages_to_print)
2826 priv->cancelled = TRUE;
2830 data->page = data->pages[priv->page_position];
2831 data->first_position = priv->page_position;
2832 data->first_sheet = data->sheet;
2834 if (priv->manual_reverse)
2836 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2837 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2838 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2839 data->last_position = MIN (2 * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2841 data->last_position = MIN (priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2845 if (priv->manual_page_set == GTK_PAGE_SET_ODD)
2846 data->last_position = MIN (((data->num_of_sheets - 1) - ((data->num_of_sheets - 1) % 2)) * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2847 else if (priv->manual_page_set == GTK_PAGE_SET_EVEN)
2848 data->last_position = MIN (((data->num_of_sheets - 1) - (1 - (data->num_of_sheets - 1) % 2)) * priv->manual_number_up - 1, priv->nr_of_pages_to_print - 1);
2850 data->last_position = priv->nr_of_pages_to_print - 1;
2854 _gtk_print_operation_set_status (data->op,
2855 GTK_PRINT_STATUS_GENERATING_DATA,
2860 print_pages_idle (gpointer user_data)
2862 PrintPagesData *data;
2863 GtkPrintOperationPrivate *priv;
2864 gboolean done = FALSE;
2866 data = (PrintPagesData*)user_data;
2867 priv = data->op->priv;
2869 if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
2871 if (priv->status == GTK_PRINT_STATUS_PREPARING)
2873 prepare_data (data);
2877 if (data->is_preview && !priv->cancelled)
2881 g_signal_emit_by_name (data->op, "ready", priv->print_context);
2885 increment_page_sequence (data);
2888 common_render_page (data->op, data->page);
2890 done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
2894 if (priv->cancelled)
2896 _gtk_print_operation_set_status (data->op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2898 data->is_preview = FALSE;
2902 if (done && !data->is_preview)
2904 g_signal_emit (data->op, signals[END_PRINT], 0, priv->print_context);
2905 priv->end_run (data->op, priv->is_sync, priv->cancelled);
2908 update_progress (data);
2915 handle_progress_response (GtkWidget *dialog,
2919 GtkPrintOperation *op = (GtkPrintOperation *)data;
2921 gtk_widget_hide (dialog);
2922 gtk_print_operation_cancel (op);
2926 show_progress_timeout (PrintPagesData *data)
2928 gtk_window_present (GTK_WINDOW (data->progress));
2930 data->op->priv->show_progress_timeout_id = 0;
2936 print_pages (GtkPrintOperation *op,
2939 GtkPrintOperationResult result)
2941 GtkPrintOperationPrivate *priv = op->priv;
2942 PrintPagesData *data;
2946 GtkPrintOperationResult tmp_result;
2948 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
2951 tmp_result = GTK_PRINT_OPERATION_RESULT_ERROR;
2952 else if (priv->cancelled)
2953 tmp_result = GTK_PRINT_OPERATION_RESULT_CANCEL;
2955 tmp_result = result;
2957 g_signal_emit (op, signals[DONE], 0, tmp_result);
2962 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
2964 data = g_new0 (PrintPagesData, 1);
2965 data->op = g_object_ref (op);
2966 data->is_preview = (priv->action == GTK_PRINT_OPERATION_ACTION_PREVIEW);
2968 if (priv->show_progress)
2970 GtkWidget *progress;
2972 progress = gtk_message_dialog_new (parent, 0,
2976 g_signal_connect (progress, "response",
2977 G_CALLBACK (handle_progress_response), op);
2979 priv->show_progress_timeout_id =
2980 gdk_threads_add_timeout (SHOW_PROGRESS_TIME,
2981 (GSourceFunc)show_progress_timeout,
2984 data->progress = progress;
2987 if (data->is_preview)
2991 g_signal_emit_by_name (op, "preview",
2992 GTK_PRINT_OPERATION_PREVIEW (op),
2993 priv->print_context,
2999 GtkWidget *error_dialog;
3001 error_dialog = gtk_message_dialog_new (parent,
3002 GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT,
3005 _("Error creating print preview"));
3007 gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (error_dialog),
3008 _("The most probable reason is that a temporary file could not be created."));
3010 if (parent && gtk_window_has_group (parent))
3011 gtk_window_group_add_window (gtk_window_get_group (parent),
3012 GTK_WINDOW (error_dialog));
3014 g_signal_connect (error_dialog, "response",
3015 G_CALLBACK (gtk_widget_destroy), NULL);
3017 gtk_widget_show (error_dialog);
3019 print_pages_idle_done (data);
3024 if (gtk_print_context_get_cairo_context (priv->print_context) == NULL)
3026 /* Programmer error */
3027 g_error ("You must set a cairo context on the print context");
3030 priv->start_page = preview_start_page;
3031 priv->end_page = preview_end_page;
3032 priv->end_run = preview_end_run;
3034 priv->print_pages = gtk_print_settings_get_print_pages (priv->print_settings);
3035 priv->page_ranges = gtk_print_settings_get_page_ranges (priv->print_settings,
3036 &priv->num_page_ranges);
3037 priv->manual_num_copies = 1;
3038 priv->manual_collation = FALSE;
3039 priv->manual_reverse = gtk_print_settings_get_reverse (priv->print_settings);
3040 priv->manual_page_set = gtk_print_settings_get_page_set (priv->print_settings);
3041 priv->manual_scale = gtk_print_settings_get_scale (priv->print_settings) / 100.0;
3042 priv->manual_orientation = TRUE;
3043 priv->manual_number_up = gtk_print_settings_get_number_up (priv->print_settings);
3044 priv->manual_number_up_layout = gtk_print_settings_get_number_up_layout (priv->print_settings);
3047 priv->print_pages_idle_id = gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
3050 print_pages_idle_done);
3052 /* Recursive main loop to make sure we don't exit on sync operations */
3055 priv->rloop = g_main_loop_new (NULL, FALSE);
3058 GDK_THREADS_LEAVE ();
3059 g_main_loop_run (priv->rloop);
3060 GDK_THREADS_ENTER ();
3062 g_main_loop_unref (priv->rloop);
3064 g_object_unref (op);
3069 * gtk_print_operation_get_error:
3070 * @op: a #GtkPrintOperation
3071 * @error: return location for the error
3073 * Call this when the result of a print operation is
3074 * %GTK_PRINT_OPERATION_RESULT_ERROR, either as returned by
3075 * gtk_print_operation_run(), or in the #GtkPrintOperation::done signal
3076 * handler. The returned #GError will contain more details on what went wrong.
3081 gtk_print_operation_get_error (GtkPrintOperation *op,
3084 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3086 g_propagate_error (error, op->priv->error);
3088 op->priv->error = NULL;
3093 * gtk_print_operation_run:
3094 * @op: a #GtkPrintOperation
3095 * @action: the action to start
3096 * @parent: (allow-none): Transient parent of the dialog
3097 * @error: (allow-none): Return location for errors, or %NULL
3099 * Runs the print operation, by first letting the user modify
3100 * print settings in the print dialog, and then print the document.
3102 * Normally that this function does not return until the rendering of all
3103 * pages is complete. You can connect to the
3104 * #GtkPrintOperation::status-changed signal on @op to obtain some
3105 * information about the progress of the print operation.
3106 * Furthermore, it may use a recursive mainloop to show the print dialog.
3108 * If you call gtk_print_operation_set_allow_async() or set the
3109 * #GtkPrintOperation:allow-async property the operation will run
3110 * asynchronously if this is supported on the platform. The
3111 * #GtkPrintOperation::done signal will be emitted with the result of the
3112 * operation when the it is done (i.e. when the dialog is canceled, or when
3113 * the print succeeds or fails).
3115 * if (settings != NULL)
3116 * gtk_print_operation_set_print_settings (print, settings);
3118 * if (page_setup != NULL)
3119 * gtk_print_operation_set_default_page_setup (print, page_setup);
3121 * g_signal_connect (print, "begin-print",
3122 * G_CALLBACK (begin_print), &data);
3123 * g_signal_connect (print, "draw-page",
3124 * G_CALLBACK (draw_page), &data);
3126 * res = gtk_print_operation_run (print,
3127 * GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3131 * if (res == GTK_PRINT_OPERATION_RESULT_ERROR)
3133 * error_dialog = gtk_message_dialog_new (GTK_WINDOW (parent),
3134 * GTK_DIALOG_DESTROY_WITH_PARENT,
3135 * GTK_MESSAGE_ERROR,
3136 * GTK_BUTTONS_CLOSE,
3137 * "Error printing file:\n%s",
3139 * g_signal_connect (error_dialog, "response",
3140 * G_CALLBACK (gtk_widget_destroy), NULL);
3141 * gtk_widget_show (error_dialog);
3142 * g_error_free (error);
3144 * else if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
3146 * if (settings != NULL)
3147 * g_object_unref (settings);
3148 * settings = g_object_ref (gtk_print_operation_get_print_settings (print));
3152 * Note that gtk_print_operation_run() can only be called once on a
3153 * given #GtkPrintOperation.
3155 * Return value: the result of the print operation. A return value of
3156 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
3157 * completed successfully. In this case, it is a good idea to obtain
3158 * the used print settings with gtk_print_operation_get_print_settings()
3159 * and store them for reuse with the next print operation. A value of
3160 * %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS means the operation is running
3161 * asynchronously, and will emit the #GtkPrintOperation::done signal when
3166 GtkPrintOperationResult
3167 gtk_print_operation_run (GtkPrintOperation *op,
3168 GtkPrintOperationAction action,
3172 GtkPrintOperationPrivate *priv;
3173 GtkPrintOperationResult result;
3174 GtkPageSetup *page_setup;
3176 gboolean run_print_pages;
3178 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
3179 GTK_PRINT_OPERATION_RESULT_ERROR);
3180 g_return_val_if_fail (op->priv->status == GTK_PRINT_STATUS_INITIAL,
3181 GTK_PRINT_OPERATION_RESULT_ERROR);
3184 run_print_pages = TRUE;
3187 priv->action = action;
3189 if (priv->print_settings == NULL)
3190 priv->print_settings = gtk_print_settings_new ();
3192 if (action == GTK_PRINT_OPERATION_ACTION_EXPORT)
3194 /* note: if you implement async EXPORT, update the docs
3195 * docs for the allow-async property.
3197 priv->is_sync = TRUE;
3198 g_return_val_if_fail (priv->export_filename != NULL, GTK_PRINT_OPERATION_RESULT_ERROR);
3199 result = run_pdf (op, parent, &do_print);
3201 else if (action == GTK_PRINT_OPERATION_ACTION_PREVIEW)
3203 priv->is_sync = !priv->allow_async;
3204 priv->print_context = _gtk_print_context_new (op);
3205 page_setup = create_page_setup (op);
3206 _gtk_print_context_set_page_setup (priv->print_context, page_setup);
3207 g_object_unref (page_setup);
3209 result = priv->is_sync ? GTK_PRINT_OPERATION_RESULT_APPLY : GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3212 else if (priv->allow_async)
3214 priv->is_sync = FALSE;
3215 _gtk_print_operation_platform_backend_run_dialog_async (op,
3216 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3219 result = GTK_PRINT_OPERATION_RESULT_IN_PROGRESS;
3220 run_print_pages = FALSE; /* print_pages is called asynchronously from dialog */
3225 priv->is_sync = TRUE;
3226 result = _gtk_print_operation_platform_backend_run_dialog (op,
3227 action == GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
3232 if (run_print_pages)
3233 print_pages (op, parent, do_print, result);
3235 if (priv->error && error)
3237 *error = g_error_copy (priv->error);
3238 result = GTK_PRINT_OPERATION_RESULT_ERROR;
3240 else if (priv->cancelled)
3241 result = GTK_PRINT_OPERATION_RESULT_CANCEL;
3247 * gtk_print_operation_cancel:
3248 * @op: a #GtkPrintOperation
3250 * Cancels a running print operation. This function may
3251 * be called from a #GtkPrintOperation::begin-print,
3252 * #GtkPrintOperation::paginate or #GtkPrintOperation::draw-page
3253 * signal handler to stop the currently running print
3259 gtk_print_operation_cancel (GtkPrintOperation *op)
3261 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3263 op->priv->cancelled = TRUE;
3267 * gtk_print_operation_set_support_selection:
3268 * @op: a #GtkPrintOperation
3269 * @support_selection: %TRUE to support selection
3271 * Sets whether selection is supported by #GtkPrintOperation.
3276 gtk_print_operation_set_support_selection (GtkPrintOperation *op,
3277 gboolean support_selection)
3279 GtkPrintOperationPrivate *priv;
3281 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3285 support_selection = support_selection != FALSE;
3286 if (priv->support_selection != support_selection)
3288 priv->support_selection = support_selection;
3289 g_object_notify (G_OBJECT (op), "support-selection");
3294 * gtk_print_operation_get_support_selection:
3295 * @op: a #GtkPrintOperation
3297 * Gets the value of #GtkPrintOperation::support-selection property.
3299 * Returns: whether the application supports print of selection
3304 gtk_print_operation_get_support_selection (GtkPrintOperation *op)
3306 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3308 return op->priv->support_selection;
3312 * gtk_print_operation_set_has_selection:
3313 * @op: a #GtkPrintOperation
3314 * @has_selection: %TRUE indicates that a selection exists
3316 * Sets whether there is a selection to print.
3318 * Application has to set number of pages to which the selection
3319 * will draw by gtk_print_operation_set_n_pages() in a callback of
3320 * #GtkPrintOperation::begin-print.
3325 gtk_print_operation_set_has_selection (GtkPrintOperation *op,
3326 gboolean has_selection)
3328 GtkPrintOperationPrivate *priv;
3330 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
3334 has_selection = has_selection != FALSE;
3335 if (priv->has_selection != has_selection)
3337 priv->has_selection = has_selection;
3338 g_object_notify (G_OBJECT (op), "has-selection");
3343 * gtk_print_operation_get_has_selection:
3344 * @op: a #GtkPrintOperation
3346 * Gets the value of #GtkPrintOperation::has-selection property.
3348 * Returns: whether there is a selection
3353 gtk_print_operation_get_has_selection (GtkPrintOperation *op)
3355 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), FALSE);
3357 return op->priv->has_selection;
3361 * gtk_print_operation_get_n_pages_to_print:
3362 * @op: a #GtkPrintOperation
3364 * Returns the number of pages that will be printed.
3366 * Note that this value is set during print preparation phase
3367 * (%GTK_PRINT_STATUS_PREPARING), so this function should never be
3368 * called before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
3369 * You can connect to the #GtkPrintOperation::status-changed signal
3370 * and call gtk_print_operation_get_n_pages_to_print() when
3371 * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
3372 * This is typically used to track the progress of print operation.
3374 * Returns: the number of pages that will be printed
3379 gtk_print_operation_get_n_pages_to_print (GtkPrintOperation *op)
3381 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), -1);
3383 return op->priv->nr_of_pages_to_print;