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.
23 #include "gtkprintoperation-private.h"
24 #include "gtkmarshalers.h"
25 #include <cairo-pdf.h>
27 #include "gtkprivate.h"
30 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
43 PROP_DEFAULT_PAGE_SETUP,
56 static guint signals[LAST_SIGNAL] = { 0 };
57 static int job_nr = 0;
59 G_DEFINE_TYPE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT)
62 * gtk_print_error_quark:
64 * Registers an error quark for #GtkPrintOperation if necessary.
66 * Return value: The error quark used for #GtkPrintOperation errors.
71 gtk_print_error_quark (void)
73 static GQuark quark = 0;
75 quark = g_quark_from_static_string ("gtk-print-error-quark");
80 gtk_print_operation_finalize (GObject *object)
82 GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
84 if (print_operation->priv->free_platform_data &&
85 print_operation->priv->platform_data)
87 print_operation->priv->free_platform_data (print_operation->priv->platform_data);
88 print_operation->priv->free_platform_data = NULL;
91 if (print_operation->priv->default_page_setup)
92 g_object_unref (print_operation->priv->default_page_setup);
94 if (print_operation->priv->print_settings)
95 g_object_unref (print_operation->priv->print_settings);
97 g_free (print_operation->priv->pdf_target);
98 g_free (print_operation->priv->job_name);
100 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
104 gtk_print_operation_init (GtkPrintOperation *operation)
108 operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
110 operation->priv->status = GTK_PRINT_STATUS_INITIAL;
111 operation->priv->status_string = g_strdup ("");
112 operation->priv->default_page_setup = NULL;
113 operation->priv->print_settings = NULL;
114 operation->priv->nr_of_pages = -1;
115 operation->priv->current_page = -1;
116 operation->priv->use_full_page = FALSE;
117 operation->priv->show_dialog = TRUE;
118 operation->priv->pdf_target = NULL;
120 operation->priv->unit = GTK_UNIT_PIXEL;
122 appname = g_get_application_name ();
123 operation->priv->job_name = g_strdup_printf ("%s job #%d",
128 gtk_print_operation_set_property (GObject *object,
133 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
137 case PROP_DEFAULT_PAGE_SETUP:
138 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
140 case PROP_PRINT_SETTINGS:
141 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
144 gtk_print_operation_set_job_name (op, g_value_get_string (value));
146 case PROP_NR_OF_PAGES:
147 gtk_print_operation_set_nr_of_pages (op, g_value_get_int (value));
149 case PROP_CURRENT_PAGE:
150 gtk_print_operation_set_current_page (op, g_value_get_int (value));
152 case PROP_USE_FULL_PAGE:
153 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
156 gtk_print_operation_set_unit (op, g_value_get_enum (value));
158 case PROP_SHOW_DIALOG:
159 gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
161 case PROP_PDF_TARGET:
162 gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
165 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
171 gtk_print_operation_get_property (GObject *object,
176 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
180 case PROP_DEFAULT_PAGE_SETUP:
181 g_value_set_object (value, op->priv->default_page_setup);
183 case PROP_PRINT_SETTINGS:
184 g_value_set_object (value, op->priv->print_settings);
187 g_value_set_string (value, op->priv->job_name);
189 case PROP_NR_OF_PAGES:
190 g_value_set_int (value, op->priv->nr_of_pages);
192 case PROP_CURRENT_PAGE:
193 g_value_set_int (value, op->priv->current_page);
195 case PROP_USE_FULL_PAGE:
196 g_value_set_boolean (value, op->priv->use_full_page);
199 g_value_set_enum (value, op->priv->unit);
201 case PROP_SHOW_DIALOG:
202 g_value_set_boolean (value, op->priv->show_dialog);
204 case PROP_PDF_TARGET:
205 g_value_set_string (value, op->priv->pdf_target);
208 g_value_set_enum (value, op->priv->status);
210 case PROP_STATUS_STRING:
211 g_value_set_string (value, op->priv->status_string);
214 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
220 gtk_print_operation_class_init (GtkPrintOperationClass *class)
222 GObjectClass *gobject_class = (GObjectClass *)class;
224 gobject_class->set_property = gtk_print_operation_set_property;
225 gobject_class->get_property = gtk_print_operation_get_property;
226 gobject_class->finalize = gtk_print_operation_finalize;
228 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
231 * GtkPrintOperation::begin-print:
232 * @operation: the #GtkPrintOperation on which the signal was emitted
233 * @context: the #GtkPrintContext for the current operation
235 * Gets emitted after the user has finished changing print settings
236 * in the dialog, before the actual rendering starts.
238 * A typical use for this signal is to use the parameters from the
239 * #GtkPrintContext and paginate the document accordingly, and then
240 * set the number of pages with gtk_print_operation_set_nr_of_pages().
244 signals[BEGIN_PRINT] =
245 g_signal_new ("begin_print",
246 G_TYPE_FROM_CLASS (gobject_class),
248 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
250 g_cclosure_marshal_VOID__OBJECT,
251 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
254 * GtkPrintOperation::request-page-setup:
255 * @operation: the #GtkPrintOperation on which the signal was emitted
256 * @context: the #GtkPrintContext for the current operation
257 * @page_nr: the number of the currently printed page
258 * @setup: the #GtkPageSetup
260 * Gets emitted once for every page that is printed, to give
261 * the application a chance to modify the page setup. Any changes
262 * done to @setup will be in force only for printing this page.
266 signals[REQUEST_PAGE_SETUP] =
267 g_signal_new ("request_page_setup",
268 G_TYPE_FROM_CLASS (gobject_class),
270 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
272 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
274 GTK_TYPE_PRINT_CONTEXT,
276 GTK_TYPE_PAGE_SETUP);
279 * GtkPrintOperation::draw-page:
280 * @operation: the #GtkPrintOperation on which the signal was emitted
281 * @context: the #GtkPrintContext for the current operation
282 * @page_nr: the number of the currently printed page
284 * Gets emitted for every page that is printed. The signal handler
285 * must render the @page_nr's page onto the cairo context obtained
286 * from @context using gtk_print_context_get_cairo().
288 * <informalexample><programlisting>
289 * FIXME: need an example here
290 * </programlisting></informalexample>
295 g_signal_new ("draw_page",
296 G_TYPE_FROM_CLASS (gobject_class),
298 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
300 _gtk_marshal_VOID__OBJECT_INT,
302 GTK_TYPE_PRINT_CONTEXT,
306 * GtkPrintOperation::end-print:
307 * @operation: the #GtkPrintOperation on which the signal was emitted
308 * @context: the #GtkPrintContext for the current operation
310 * Gets emitted after all pages have been rendered.
311 * A handler for this signal can clean up any resources that have
312 * been allocated in the ::begin-print handler.
317 g_signal_new ("end_print",
318 G_TYPE_FROM_CLASS (gobject_class),
320 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
322 g_cclosure_marshal_VOID__OBJECT,
323 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
326 * GtkPrintOperation::status-changed:
327 * @operation: the #GtkPrintOperation on which the signal was emitted
329 * Gets emitted at between the various phases of the print operation.
330 * See #GtkPrintStatus for the phases that are being discriminated.
331 * Use gtk_print_operation_get_status() to find out the current
336 signals[STATUS_CHANGED] =
337 g_signal_new ("status-changed",
338 G_TYPE_FROM_CLASS (class),
340 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
342 g_cclosure_marshal_VOID__VOID,
345 g_object_class_install_property (gobject_class,
346 PROP_DEFAULT_PAGE_SETUP,
347 g_param_spec_object ("default-page-setup",
348 P_("Default Page Setup"),
349 P_("The GtkPageSetup used by default"),
351 GTK_PARAM_READWRITE));
353 g_object_class_install_property (gobject_class,
355 g_param_spec_object ("print-settings",
356 P_("Print Settings"),
357 P_("The GtkPrintSettings used for initializing the dialog"),
358 GTK_TYPE_PRINT_SETTINGS,
359 GTK_PARAM_READWRITE));
361 g_object_class_install_property (gobject_class,
363 g_param_spec_string ("job-name",
365 P_("A string used for identifying the print job."),
367 GTK_PARAM_READWRITE));
369 g_object_class_install_property (gobject_class,
371 g_param_spec_int ("number-of-pages",
372 P_("Number of Pages"),
373 P_("The number of pages in the document."),
377 GTK_PARAM_READWRITE));
379 g_object_class_install_property (gobject_class,
381 g_param_spec_int ("current-page",
383 P_("The current page in the document."),
387 GTK_PARAM_READWRITE));
389 g_object_class_install_property (gobject_class,
391 g_param_spec_boolean ("use-full-page",
395 GTK_PARAM_READWRITE));
398 g_object_class_install_property (gobject_class,
400 g_param_spec_enum ("unit",
405 GTK_PARAM_READWRITE));
408 g_object_class_install_property (gobject_class,
410 g_param_spec_boolean ("show-dialog",
412 P_("%TRUE if gtk_print_operation_run() should show the print dialog."),
414 GTK_PARAM_READWRITE));
416 g_object_class_install_property (gobject_class,
418 g_param_spec_string ("pdf-target",
419 P_("PDF target filename"),
422 GTK_PARAM_READWRITE));
424 g_object_class_install_property (gobject_class,
426 g_param_spec_enum ("status",
428 P_("The status of the print operation"),
429 GTK_TYPE_PRINT_STATUS,
430 GTK_PRINT_STATUS_INITIAL,
431 GTK_PARAM_READABLE));
433 g_object_class_install_property (gobject_class,
435 g_param_spec_string ("status-string",
437 P_("A human-readable description of the status"),
439 GTK_PARAM_READABLE));
445 * gtk_print_operation_new:
447 * Creates a new #GtkPrintOperation.
449 * Returns: a new #GtkPrintOperation
454 gtk_print_operation_new (void)
456 GtkPrintOperation *print_operation;
458 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
460 return print_operation;
464 * gtk_print_operation_set_default_page_setup:
465 * @op: a #GtkPrintOperation
466 * @default_page_setup: a #GtkPageSetup, or %NULL
468 * Makes @default_page_setup the default page setup for @op.
470 * This page setup will be used by gtk_print_operation_run(),
471 * but it can be overridden on a per-page basis by connecting
472 * to the ::request-page-setup signal.
477 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
478 GtkPageSetup *default_page_setup)
480 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
481 g_return_if_fail (default_page_setup == NULL ||
482 GTK_IS_PAGE_SETUP (default_page_setup));
484 if (default_page_setup != op->priv->default_page_setup)
486 if (default_page_setup)
487 g_object_ref (default_page_setup);
489 if (op->priv->default_page_setup)
490 g_object_unref (op->priv->default_page_setup);
492 op->priv->default_page_setup = default_page_setup;
494 g_object_notify (G_OBJECT (op), "default-page-setup");
499 * gtk_print_operation_get_default_page_setup:
500 * @op: a #GtkPrintOperation
502 * Returns the default page setup, see
503 * gtk_print_operation_set_default_page_setup().
505 * Returns: the default page setup
510 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
512 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
514 return op->priv->default_page_setup;
519 * gtk_print_operation_set_print_settings:
520 * @op: a #GtkPrintOperation
521 * @print_settings: #GtkPrintSettings, or %NULL
523 * Sets the print settings for @op. This is typically used to
524 * re-establish print settings from a previous print operation,
525 * see gtk_print_operation_run().
530 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
531 GtkPrintSettings *print_settings)
533 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
534 g_return_if_fail (print_settings == NULL ||
535 GTK_IS_PRINT_SETTINGS (print_settings));
537 if (print_settings != op->priv->print_settings)
540 g_object_ref (print_settings);
542 if (op->priv->print_settings)
543 g_object_unref (op->priv->print_settings);
545 op->priv->print_settings = print_settings;
547 g_object_notify (G_OBJECT (op), "print-settings");
552 * gtk_print_operation_get_print_settings:
553 * @op: a #GtkPrintOperation
555 * Returns the current print settings.
557 * Note that the return value is %NULL until either
558 * gtk_print_operation_set_print_settings() or
559 * gtk_print_operation_run() have been called.
561 * Return value: the current print settings of @op.
566 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
568 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
570 return op->priv->print_settings;
574 * gtk_print_operation_set_job_name:
575 * @op: a #GtkPrintOperation
576 * @job_name: a string that identifies the print job
578 * Sets the name of the print job. The name is used to identify
579 * the job (e.g. in monitoring applications like eggcups).
581 * If you don't set a job name, GTK+ picks a default one by
582 * numbering successive print jobs.
587 gtk_print_operation_set_job_name (GtkPrintOperation *op,
588 const gchar *job_name)
590 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
591 g_return_if_fail (g_utf8_validate (job_name, -1, NULL));
593 g_free (op->priv->job_name);
594 op->priv->job_name = g_strdup (job_name);
596 g_object_notify (G_OBJECT (op), "job-name");
600 * gtk_print_operation_set_nr_of_pages:
601 * @op: a #GtkPrintOperation
602 * @n_pages: the number of pages
604 * Sets the number of pages in the document.
606 * This <emphasis>must</emphasis> be set to a positive number
607 * before the print dialog is shown. It may be set in a
608 * ::begin-print signal hander.
610 * Note that the page numbers passed to the ::request-page-setup
611 * and ::draw-page signals are 0-based, i.e. if the user chooses
612 * to print all pages, the last ::draw-page signal will be
613 * for page @n_pages - 1.
618 gtk_print_operation_set_nr_of_pages (GtkPrintOperation *op,
621 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
622 g_return_if_fail (n_pages > 0);
623 g_return_if_fail (op->priv->current_page == -1 ||
624 op->priv->current_page < n_pages);
626 if (op->priv->nr_of_pages != n_pages)
628 op->priv->nr_of_pages = n_pages;
630 g_object_notify (G_OBJECT (op), "number-of-pages");
635 * gtk_print_operation_set_current_page:
636 * @op: a #GtkPrintOperation
637 * @current_page: the current page, 0-based
639 * Sets the current page.
640 * If this is called before gtk_print_operation_run(),
641 * the user will be able to select to print only the current page.
643 * Note that this only makes sense for pre-paginated documents.
648 gtk_print_operation_set_current_page (GtkPrintOperation *op,
651 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
652 g_return_if_fail (current_page >= 0);
653 g_return_if_fail (op->priv->nr_of_pages == -1 ||
654 current_page < op->priv->nr_of_pages);
656 if (op->priv->current_page != current_page)
658 op->priv->current_page = current_page;
660 g_object_notify (G_OBJECT (op), "current-page");
665 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
668 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
670 full_page = full_page != FALSE;
672 if (op->priv->use_full_page != full_page)
674 op->priv->use_full_page = full_page;
676 g_object_notify (G_OBJECT (op), "use-full-page");
681 gtk_print_operation_set_unit (GtkPrintOperation *op,
684 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
686 if (op->priv->unit != unit)
688 op->priv->unit = unit;
690 g_object_notify (G_OBJECT (op), "unit");
695 _gtk_print_operation_set_status (GtkPrintOperation *op,
696 GtkPrintStatus status,
699 const gchar *status_strs[] = {
700 /* translators, strip the prefix up to and including the first | */
701 N_("print operation status|Initial state"),
702 /* translators, strip the prefix up to and including the first | */
703 N_("print operation status|Preparing to print"),
704 /* translators, strip the prefix up to and including the first | */
705 N_("print operation status|Generating data"),
706 /* translators, strip the prefix up to and including the first | */
707 N_("print operation status|Sending data"),
708 /* translators, strip the prefix up to and including the first | */
709 N_("print operation status|Waiting"),
710 /* translators, strip the prefix up to and including the first | */
711 N_("print operation status|Blocking on issue"),
712 /* translators, strip the prefix up to and including the first | */
713 N_("print operation status|Printing"),
714 /* translators, strip the prefix up to and including the first | */
715 N_("print operation status|Finished"),
716 /* translators, strip the prefix up to and including the first | */
717 N_("print operation status|Finished with error")
720 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
721 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
724 string = g_strip_context (status_strs[status],
725 gettext (status_strs[status]));
727 if (op->priv->status == status &&
728 strcmp (string, op->priv->status_string) == 0)
731 g_free (op->priv->status_string);
732 op->priv->status_string = g_strdup (string);
733 op->priv->status = status;
735 g_object_notify (G_OBJECT (op), "status");
736 g_object_notify (G_OBJECT (op), "status-string");
738 g_signal_emit (op, signals[STATUS_CHANGED], 0);
743 * gtk_print_operation_get_status:
744 * @op: a #GtkPrintOperation
746 * Returns the status of the print operation.
747 * Also see gtk_print_operation_get_status_string().
749 * Return value: the status of the print operation
754 gtk_print_operation_get_status (GtkPrintOperation *op)
756 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
757 GTK_PRINT_STATUS_FINISHED_ABORTED);
759 return op->priv->status;
763 * gtk_print_operation_get_status_string:
764 * @op: a #GtkPrintOperation
766 * Returns a string representation of the status of the
767 * print operation. The string is translated and suitable
768 * for displaying the print status e.g. in a #GtkStatusbar.
770 * Use gtk_print_operation_get_status() to obtain a status
771 * value that is suitable for programmatic use.
773 * Return value: a string representation of the status
774 * of the print operation
778 G_CONST_RETURN gchar *
779 gtk_print_operation_get_status_string (GtkPrintOperation *op)
781 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
783 return op->priv->status_string;
787 * gtk_print_operation_is_finished:
788 * @op: a #GtkPrintOperation
790 * A convenience function to find out if the print operation
791 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
792 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
794 * Return value: %TRUE, if the print operation is finished.
799 gtk_print_operation_is_finished (GtkPrintOperation *op)
801 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
804 op->priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
805 op->priv->status == GTK_PRINT_STATUS_FINISHED;
810 * gtk_print_operation_set_show_dialog:
811 * @op: a #GtkPrintOperation
812 * @show_dialog: %TRUE to show the print dialog
814 * Sets whether calling gtk_print_operation_run() will present
815 * a print dialog to the user, or just print to the default printer.
820 gtk_print_operation_set_show_dialog (GtkPrintOperation *op,
821 gboolean show_dialog)
823 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
825 show_dialog = show_dialog != FALSE;
827 if (op->priv->show_dialog != show_dialog)
829 op->priv->show_dialog = show_dialog;
831 g_object_notify (G_OBJECT (op), "show-dialog");
836 gtk_print_operation_set_pdf_target (GtkPrintOperation *op,
837 const gchar * filename)
839 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
841 g_free (op->priv->pdf_target);
842 op->priv->pdf_target = g_strdup (filename);
844 g_object_notify (G_OBJECT (op), "pdf-target");
847 /* Creates the initial page setup used for printing unless the
848 * app overrides this on a per-page basis using request_page_setup.
850 * Data is taken from, in order, if existing:
852 * PrintSettings returned from the print dialog
853 * (initial dialog values are set from default_page_setup
854 * if unset in app specified print_settings)
856 * per-locale default setup
858 static GtkPageSetup *
859 create_page_setup (GtkPrintOperation *op)
861 GtkPageSetup *page_setup;
862 GtkPrintSettings *settings;
864 if (op->priv->default_page_setup)
865 page_setup = gtk_page_setup_copy (op->priv->default_page_setup);
867 page_setup = gtk_page_setup_new ();
869 settings = op->priv->print_settings;
872 GtkPaperSize *paper_size;
874 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
875 gtk_page_setup_set_orientation (page_setup,
876 gtk_print_settings_get_orientation (settings));
879 paper_size = gtk_print_settings_get_paper_size (settings);
882 gtk_page_setup_set_paper_size (page_setup, paper_size);
883 gtk_paper_size_free (paper_size);
893 pdf_start_page (GtkPrintOperation *op,
894 GtkPrintContext *print_context,
895 GtkPageSetup *page_setup)
897 /* TODO: Set up page size, not supported in cairo yet */
901 pdf_end_page (GtkPrintOperation *op,
902 GtkPrintContext *print_context)
906 cr = gtk_print_context_get_cairo (print_context);
907 cairo_show_page (cr);
911 pdf_end_run (GtkPrintOperation *op)
913 cairo_surface_destroy (op->priv->surface);
914 op->priv->surface = NULL;
917 static GtkPrintOperationResult
918 run_pdf (GtkPrintOperation *op,
923 GtkPageSetup *page_setup;
924 double width, height;
925 /* This will be overwritten later by the non-default size, but
926 we need to pass some size: */
928 page_setup = create_page_setup (op);
929 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
930 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
931 g_object_unref (page_setup);
933 op->priv->surface = cairo_pdf_surface_create (op->priv->pdf_target,
935 /* TODO: DPI from settings object? */
936 cairo_pdf_surface_set_dpi (op->priv->surface, 300, 300);
938 op->priv->dpi_x = 72;
939 op->priv->dpi_y = 72;
941 op->priv->manual_num_copies = 1;
942 op->priv->manual_collation = FALSE;
946 op->priv->start_page = pdf_start_page;
947 op->priv->end_page = pdf_end_page;
948 op->priv->end_run = pdf_end_run;
950 return GTK_PRINT_OPERATION_RESULT_APPLY;
953 static GtkPrintOperationResult
954 run_print_dialog (GtkPrintOperation *op,
959 if (op->priv->pdf_target != NULL)
960 return run_pdf (op, parent, do_print, error);
964 * set print settings on dialog
965 * run dialog, if show_dialog set
966 * extract print settings from dialog
967 * create cairo surface and data for print job
968 * return correct result val
970 return _gtk_print_operation_platform_backend_run_dialog (op,
977 * gtk_print_operation_run:
978 * @op: a #GtkPrintOperation
979 * @parent: Transient parent of the dialog, or %NULL
980 * @error: Return location for errors, or %NULL
982 * Runs the print operation, by first letting the user modify
983 * print settings in the print dialog, and then print the
986 * Note that this function does not return until the rendering
987 * of all pages is complete. You can connect to the ::status-changed
988 * signal on @op to obtain some information about the progress
989 * of the print operation.
991 * <informalexample><programlisting>
992 * FIXME: need an example here
993 * </programlisting></informalexample>
995 * Return value: the result of the print operation. A return value
996 * of %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing
997 * was completed successfully. In this case, it is a good idea
998 * to obtain the used print settings with
999 * gtk_print_operation_get_print_settings() and store them for
1000 * reuse with the next print operation.
1004 GtkPrintOperationResult
1005 gtk_print_operation_run (GtkPrintOperation *op,
1010 GtkPageSetup *initial_page_setup, *page_setup;
1011 GtkPrintContext *print_context;
1014 int uncollated_copies, collated_copies;
1016 GtkPageRange *ranges;
1017 GtkPageRange one_range;
1019 GtkPrintOperationResult result;
1021 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1022 GTK_PRINT_OPERATION_RESULT_ERROR);
1024 result = run_print_dialog (op, parent, &do_print, error);
1027 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1031 if (op->priv->manual_collation)
1033 uncollated_copies = op->priv->manual_num_copies;
1034 collated_copies = 1;
1038 uncollated_copies = 1;
1039 collated_copies = op->priv->manual_num_copies;
1042 print_context = _gtk_print_context_new (op);
1044 initial_page_setup = create_page_setup (op);
1045 _gtk_print_context_set_page_setup (print_context, initial_page_setup);
1047 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
1048 g_signal_emit (op, signals[BEGIN_PRINT], 0, print_context);
1050 g_return_val_if_fail (op->priv->nr_of_pages > 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1052 if (op->priv->print_pages == GTK_PRINT_PAGES_RANGES)
1054 ranges = op->priv->page_ranges;
1055 num_ranges = op->priv->num_page_ranges;
1057 else if (op->priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
1058 op->priv->current_page != -1)
1060 ranges = &one_range;
1062 ranges[0].start = op->priv->current_page;
1063 ranges[0].end = op->priv->current_page;
1067 ranges = &one_range;
1069 ranges[0].start = 0;
1070 ranges[0].end = op->priv->nr_of_pages - 1;
1073 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_GENERATING_DATA, NULL);
1075 for (i = 0; i < uncollated_copies; i++)
1077 for (range = 0; range < num_ranges; range ++)
1079 int start, end, inc;
1080 int low = ranges[range].start;
1081 int high = ranges[range].end;
1083 if (op->priv->manual_reverse)
1095 for (page = start; page != end; page += inc)
1097 if ((op->priv->manual_page_set == GTK_PAGE_SET_EVEN && page % 2 == 0) ||
1098 (op->priv->manual_page_set == GTK_PAGE_SET_ODD && page % 2 == 1))
1101 for (j = 0; j < collated_copies; j++)
1103 page_setup = gtk_page_setup_copy (initial_page_setup);
1104 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0, print_context, page, page_setup);
1106 _gtk_print_context_set_page_setup (print_context, page_setup);
1107 op->priv->start_page (op, print_context, page_setup);
1109 cr = gtk_print_context_get_cairo (print_context);
1112 if (op->priv->manual_scale != 100.0)
1114 op->priv->manual_scale,
1115 op->priv->manual_scale);
1117 if (op->priv->manual_orientation)
1118 _gtk_print_context_rotate_according_to_orientation (print_context);
1120 if (!op->priv->use_full_page)
1121 _gtk_print_context_translate_into_margin (print_context);
1123 g_signal_emit (op, signals[DRAW_PAGE], 0,
1124 print_context, page);
1126 op->priv->end_page (op, print_context);
1130 g_object_unref (page_setup);
1132 /* Iterate the mainloop so that we redraw windows */
1133 while (gtk_events_pending ())
1134 gtk_main_iteration ();
1140 g_signal_emit (op, signals[END_PRINT], 0, print_context);
1142 g_object_unref (print_context);
1143 g_object_unref (initial_page_setup);
1145 cairo_surface_finish (op->priv->surface);
1146 op->priv->end_run (op);
1148 return GTK_PRINT_OPERATION_RESULT_APPLY;
1152 #define __GTK_PRINT_OPERATION_C__
1153 #include "gtkaliasdef.c"