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);
83 GtkPrintOperationPrivate *priv = print_operation->priv;
85 if (priv->free_platform_data &&
88 priv->free_platform_data (priv->platform_data);
89 priv->free_platform_data = NULL;
92 if (priv->default_page_setup)
93 g_object_unref (priv->default_page_setup);
95 if (priv->print_settings)
96 g_object_unref (priv->print_settings);
98 g_free (priv->pdf_target);
99 g_free (priv->job_name);
101 G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
105 gtk_print_operation_init (GtkPrintOperation *operation)
107 GtkPrintOperationPrivate *priv;
110 priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
112 priv->status = GTK_PRINT_STATUS_INITIAL;
113 priv->status_string = g_strdup ("");
114 priv->default_page_setup = NULL;
115 priv->print_settings = NULL;
116 priv->nr_of_pages = -1;
117 priv->current_page = -1;
118 priv->use_full_page = FALSE;
119 priv->show_dialog = TRUE;
120 priv->pdf_target = NULL;
122 priv->unit = GTK_UNIT_PIXEL;
124 appname = g_get_application_name ();
125 priv->job_name = g_strdup_printf ("%s job #%d", appname, ++job_nr);
129 gtk_print_operation_set_property (GObject *object,
134 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
138 case PROP_DEFAULT_PAGE_SETUP:
139 gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
141 case PROP_PRINT_SETTINGS:
142 gtk_print_operation_set_print_settings (op, g_value_get_object (value));
145 gtk_print_operation_set_job_name (op, g_value_get_string (value));
147 case PROP_NR_OF_PAGES:
148 gtk_print_operation_set_nr_of_pages (op, g_value_get_int (value));
150 case PROP_CURRENT_PAGE:
151 gtk_print_operation_set_current_page (op, g_value_get_int (value));
153 case PROP_USE_FULL_PAGE:
154 gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
157 gtk_print_operation_set_unit (op, g_value_get_enum (value));
159 case PROP_SHOW_DIALOG:
160 gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
162 case PROP_PDF_TARGET:
163 gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
166 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
172 gtk_print_operation_get_property (GObject *object,
177 GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
178 GtkPrintOperationPrivate *priv = op->priv;
182 case PROP_DEFAULT_PAGE_SETUP:
183 g_value_set_object (value, priv->default_page_setup);
185 case PROP_PRINT_SETTINGS:
186 g_value_set_object (value, priv->print_settings);
189 g_value_set_string (value, priv->job_name);
191 case PROP_NR_OF_PAGES:
192 g_value_set_int (value, priv->nr_of_pages);
194 case PROP_CURRENT_PAGE:
195 g_value_set_int (value, priv->current_page);
197 case PROP_USE_FULL_PAGE:
198 g_value_set_boolean (value, priv->use_full_page);
201 g_value_set_enum (value, priv->unit);
203 case PROP_SHOW_DIALOG:
204 g_value_set_boolean (value, priv->show_dialog);
206 case PROP_PDF_TARGET:
207 g_value_set_string (value, priv->pdf_target);
210 g_value_set_enum (value, priv->status);
212 case PROP_STATUS_STRING:
213 g_value_set_string (value, priv->status_string);
216 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
222 gtk_print_operation_class_init (GtkPrintOperationClass *class)
224 GObjectClass *gobject_class = (GObjectClass *)class;
226 gobject_class->set_property = gtk_print_operation_set_property;
227 gobject_class->get_property = gtk_print_operation_get_property;
228 gobject_class->finalize = gtk_print_operation_finalize;
230 g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
233 * GtkPrintOperation::begin-print:
234 * @operation: the #GtkPrintOperation on which the signal was emitted
235 * @context: the #GtkPrintContext for the current operation
237 * Gets emitted after the user has finished changing print settings
238 * in the dialog, before the actual rendering starts.
240 * A typical use for this signal is to use the parameters from the
241 * #GtkPrintContext and paginate the document accordingly, and then
242 * set the number of pages with gtk_print_operation_set_nr_of_pages().
246 signals[BEGIN_PRINT] =
247 g_signal_new ("begin_print",
248 G_TYPE_FROM_CLASS (gobject_class),
250 G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
252 g_cclosure_marshal_VOID__OBJECT,
253 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
256 * GtkPrintOperation::request-page-setup:
257 * @operation: the #GtkPrintOperation on which the signal was emitted
258 * @context: the #GtkPrintContext for the current operation
259 * @page_nr: the number of the currently printed page
260 * @setup: the #GtkPageSetup
262 * Gets emitted once for every page that is printed, to give
263 * the application a chance to modify the page setup. Any changes
264 * done to @setup will be in force only for printing this page.
268 signals[REQUEST_PAGE_SETUP] =
269 g_signal_new ("request_page_setup",
270 G_TYPE_FROM_CLASS (gobject_class),
272 G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
274 _gtk_marshal_VOID__OBJECT_INT_OBJECT,
276 GTK_TYPE_PRINT_CONTEXT,
278 GTK_TYPE_PAGE_SETUP);
281 * GtkPrintOperation::draw-page:
282 * @operation: the #GtkPrintOperation on which the signal was emitted
283 * @context: the #GtkPrintContext for the current operation
284 * @page_nr: the number of the currently printed page
286 * Gets emitted for every page that is printed. The signal handler
287 * must render the @page_nr's page onto the cairo context obtained
288 * from @context using gtk_print_context_get_cairo().
290 * <informalexample><programlisting>
291 * FIXME: need an example here
292 * </programlisting></informalexample>
294 * Use gtk_print_operation_set_use_full_page() and
295 * gtk_print_operation_set_unit() before starting the print operation
296 * to set up the transformation of the cairo context according to your
302 g_signal_new ("draw_page",
303 G_TYPE_FROM_CLASS (gobject_class),
305 G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
307 _gtk_marshal_VOID__OBJECT_INT,
309 GTK_TYPE_PRINT_CONTEXT,
313 * GtkPrintOperation::end-print:
314 * @operation: the #GtkPrintOperation on which the signal was emitted
315 * @context: the #GtkPrintContext for the current operation
317 * Gets emitted after all pages have been rendered.
318 * A handler for this signal can clean up any resources that have
319 * been allocated in the ::begin-print handler.
324 g_signal_new ("end_print",
325 G_TYPE_FROM_CLASS (gobject_class),
327 G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
329 g_cclosure_marshal_VOID__OBJECT,
330 G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
333 * GtkPrintOperation::status-changed:
334 * @operation: the #GtkPrintOperation on which the signal was emitted
336 * Gets emitted at between the various phases of the print operation.
337 * See #GtkPrintStatus for the phases that are being discriminated.
338 * Use gtk_print_operation_get_status() to find out the current
343 signals[STATUS_CHANGED] =
344 g_signal_new ("status-changed",
345 G_TYPE_FROM_CLASS (class),
347 G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
349 g_cclosure_marshal_VOID__VOID,
353 * GtkPrintOperation:default-page-setup:
355 * The #GtkPageSetup used by default.
357 * This page setup will be used by gtk_print_operation_run(),
358 * but it can be overridden on a per-page basis by connecting
359 * to the ::request-page-setup signal.
363 g_object_class_install_property (gobject_class,
364 PROP_DEFAULT_PAGE_SETUP,
365 g_param_spec_object ("default-page-setup",
366 P_("Default Page Setup"),
367 P_("The GtkPageSetup used by default"),
369 GTK_PARAM_READWRITE));
372 * GtkPrintOperation:print-settings:
374 * The #GtkPrintSettings used for initializing the dialog.
376 * Setting this property is typically used to re-establish print
377 * settings from a previous print operation, see gtk_print_operation_run().
381 g_object_class_install_property (gobject_class,
383 g_param_spec_object ("print-settings",
384 P_("Print Settings"),
385 P_("The GtkPrintSettings used for initializing the dialog"),
386 GTK_TYPE_PRINT_SETTINGS,
387 GTK_PARAM_READWRITE));
390 * GtkPrintOperation:job-name:
392 * A string used to identify the job (e.g. in monitoring applications like eggcups).
394 * If you don't set a job name, GTK+ picks a default one by numbering successive
399 g_object_class_install_property (gobject_class,
401 g_param_spec_string ("job-name",
403 P_("A string used for identifying the print job."),
405 GTK_PARAM_READWRITE));
408 * GtkPrintOperation:number-of-pages:
410 * The number of pages in the document.
412 * This <emphasis>must</emphasis> be set to a positive number
413 * before the rendering starts. It may be set in a ::begin-print signal hander.
415 * Note that the page numbers passed to the ::request-page-setup and ::draw-page
416 * signals are 0-based, i.e. if the user chooses to print all pages, the last
417 * ::draw-page signal will be for page @n_pages - 1.
421 g_object_class_install_property (gobject_class,
423 g_param_spec_int ("number-of-pages",
424 P_("Number of Pages"),
425 P_("The number of pages in the document."),
429 GTK_PARAM_READWRITE));
432 * GtkPrintOperation:current-page:
434 * The current page in the document.
436 * If this is set before gtk_print_operation_run(),
437 * the user will be able to select to print only the current page.
439 * Note that this only makes sense for pre-paginated documents.
443 g_object_class_install_property (gobject_class,
445 g_param_spec_int ("current-page",
447 P_("The current page in the document."),
451 GTK_PARAM_READWRITE));
454 * GtkPrintOperation:use-full-page:
456 * If %TRUE, the transformation for the cairo context obtained from
457 * #GtkPrintContext puts the origin at the top left corner of the page
458 * (which may not be the top left corner of the sheet, depending on page
459 * orientation and the number of pages per sheet). Otherwise, the origin
460 * is at the top left corner of the imageable area (i.e. inside the margins).
464 g_object_class_install_property (gobject_class,
466 g_param_spec_boolean ("use-full-page",
468 P_("%TRUE if the the origin of the context should be at the corner of the page and not the corner of the imageable area"),
470 GTK_PARAM_READWRITE));
474 * GtkPrintOperation:unit:
476 * The transformation for the cairo context obtained from
477 * #GtkPrintContext is set up in such a way that distances are measured
482 g_object_class_install_property (gobject_class,
484 g_param_spec_enum ("unit",
486 P_("The unit in which distances can be measured in the context"),
489 GTK_PARAM_READWRITE));
493 * GtkPrintOperation:show-dialog:
495 * Determines whether calling gtk_print_operation_run() will present
496 * a print dialog to the user, or just print to the default printer.
500 g_object_class_install_property (gobject_class,
502 g_param_spec_boolean ("show-dialog",
504 P_("%TRUE if gtk_print_operation_run() should show the print dialog."),
506 GTK_PARAM_READWRITE));
509 * GtkPrintOperation:pdf-target:
511 * The name of a PDF file to generate instead of showing the print dialog.
513 * The indended use of this property is for implementing "Export to PDF"
516 * "Print to PDF" support is independent of this and is done
517 * by letting the user pick the "Print to PDF" item from the list
518 * of printers in the print dialog.
522 g_object_class_install_property (gobject_class,
524 g_param_spec_string ("pdf-target",
525 P_("PDF target filename"),
528 GTK_PARAM_READWRITE));
531 * GtkPrintOperation:status:
533 * The status of the print operation.
537 g_object_class_install_property (gobject_class,
539 g_param_spec_enum ("status",
541 P_("The status of the print operation"),
542 GTK_TYPE_PRINT_STATUS,
543 GTK_PRINT_STATUS_INITIAL,
544 GTK_PARAM_READABLE));
547 * GtkPrintOperation:status-string:
549 * A string representation of the status of the print operation.
550 * The string is translated and suitable for displaying the print
551 * status e.g. in a #GtkStatusbar.
553 * See the ::status property for a status value that is suitable
554 * for programmatic use.
558 g_object_class_install_property (gobject_class,
560 g_param_spec_string ("status-string",
562 P_("A human-readable description of the status"),
564 GTK_PARAM_READABLE));
570 * gtk_print_operation_new:
572 * Creates a new #GtkPrintOperation.
574 * Returns: a new #GtkPrintOperation
579 gtk_print_operation_new (void)
581 GtkPrintOperation *print_operation;
583 print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
585 return print_operation;
589 * gtk_print_operation_set_default_page_setup:
590 * @op: a #GtkPrintOperation
591 * @default_page_setup: a #GtkPageSetup, or %NULL
593 * Makes @default_page_setup the default page setup for @op.
595 * This page setup will be used by gtk_print_operation_run(),
596 * but it can be overridden on a per-page basis by connecting
597 * to the ::request-page-setup signal.
602 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
603 GtkPageSetup *default_page_setup)
605 GtkPrintOperationPrivate *priv;
607 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
608 g_return_if_fail (default_page_setup == NULL ||
609 GTK_IS_PAGE_SETUP (default_page_setup));
613 if (default_page_setup != priv->default_page_setup)
615 if (default_page_setup)
616 g_object_ref (default_page_setup);
618 if (priv->default_page_setup)
619 g_object_unref (priv->default_page_setup);
621 priv->default_page_setup = default_page_setup;
623 g_object_notify (G_OBJECT (op), "default-page-setup");
628 * gtk_print_operation_get_default_page_setup:
629 * @op: a #GtkPrintOperation
631 * Returns the default page setup, see
632 * gtk_print_operation_set_default_page_setup().
634 * Returns: the default page setup
639 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
641 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
643 return op->priv->default_page_setup;
648 * gtk_print_operation_set_print_settings:
649 * @op: a #GtkPrintOperation
650 * @print_settings: #GtkPrintSettings, or %NULL
652 * Sets the print settings for @op. This is typically used to
653 * re-establish print settings from a previous print operation,
654 * see gtk_print_operation_run().
659 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
660 GtkPrintSettings *print_settings)
662 GtkPrintOperationPrivate *priv;
664 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
665 g_return_if_fail (print_settings == NULL ||
666 GTK_IS_PRINT_SETTINGS (print_settings));
670 if (print_settings != priv->print_settings)
673 g_object_ref (print_settings);
675 if (priv->print_settings)
676 g_object_unref (priv->print_settings);
678 priv->print_settings = print_settings;
680 g_object_notify (G_OBJECT (op), "print-settings");
685 * gtk_print_operation_get_print_settings:
686 * @op: a #GtkPrintOperation
688 * Returns the current print settings.
690 * Note that the return value is %NULL until either
691 * gtk_print_operation_set_print_settings() or
692 * gtk_print_operation_run() have been called.
694 * Return value: the current print settings of @op.
699 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
701 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
703 return op->priv->print_settings;
707 * gtk_print_operation_set_job_name:
708 * @op: a #GtkPrintOperation
709 * @job_name: a string that identifies the print job
711 * Sets the name of the print job. The name is used to identify
712 * the job (e.g. in monitoring applications like eggcups).
714 * If you don't set a job name, GTK+ picks a default one by
715 * numbering successive print jobs.
720 gtk_print_operation_set_job_name (GtkPrintOperation *op,
721 const gchar *job_name)
723 GtkPrintOperationPrivate *priv;
725 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
726 g_return_if_fail (g_utf8_validate (job_name, -1, NULL));
730 g_free (priv->job_name);
731 priv->job_name = g_strdup (job_name);
733 g_object_notify (G_OBJECT (op), "job-name");
737 * gtk_print_operation_set_nr_of_pages:
738 * @op: a #GtkPrintOperation
739 * @n_pages: the number of pages
741 * Sets the number of pages in the document.
743 * This <emphasis>must</emphasis> be set to a positive number
744 * before the rendering starts. It may be set in a ::begin-print
747 * Note that the page numbers passed to the ::request-page-setup
748 * and ::draw-page signals are 0-based, i.e. if the user chooses
749 * to print all pages, the last ::draw-page signal will be
750 * for page @n_pages - 1.
755 gtk_print_operation_set_nr_of_pages (GtkPrintOperation *op,
758 GtkPrintOperationPrivate *priv;
760 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
761 g_return_if_fail (n_pages > 0);
764 g_return_if_fail (priv->current_page == -1 ||
765 priv->current_page < n_pages);
767 if (priv->nr_of_pages != n_pages)
769 priv->nr_of_pages = n_pages;
771 g_object_notify (G_OBJECT (op), "number-of-pages");
776 * gtk_print_operation_set_current_page:
777 * @op: a #GtkPrintOperation
778 * @current_page: the current page, 0-based
780 * Sets the current page.
782 * If this is called before gtk_print_operation_run(),
783 * the user will be able to select to print only the current page.
785 * Note that this only makes sense for pre-paginated documents.
790 gtk_print_operation_set_current_page (GtkPrintOperation *op,
793 GtkPrintOperationPrivate *priv;
795 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
796 g_return_if_fail (current_page >= 0);
799 g_return_if_fail (priv->nr_of_pages == -1 ||
800 current_page < priv->nr_of_pages);
802 if (priv->current_page != current_page)
804 priv->current_page = current_page;
806 g_object_notify (G_OBJECT (op), "current-page");
811 * gtk_print_operation_set_use_full_page:
812 * @op: a #GtkPrintOperation
813 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
815 * If @full_page is %TRUE, the transformation for the cairo context
816 * obtained from #GtkPrintContext puts the origin at the top left
817 * corner of the page (which may not be the top left corner of the
818 * sheet, depending on page orientation and the number of pages per
819 * sheet). Otherwise, the origin is at the top left corner of the
820 * imageable area (i.e. inside the margins).
825 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
828 GtkPrintOperationPrivate *priv;
830 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
832 full_page = full_page != FALSE;
836 if (priv->use_full_page != full_page)
838 priv->use_full_page = full_page;
840 g_object_notify (G_OBJECT (op), "use-full-page");
845 * gtk_print_operation_set_unit:
846 * @op: a #GtkPrintOperation
847 * @unit: the unit to use
849 * Sets up the transformation for the cairo context obtained from
850 * #GtkPrintContext in such a way that distances are measured in
856 gtk_print_operation_set_unit (GtkPrintOperation *op,
859 GtkPrintOperationPrivate *priv;
861 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
865 if (priv->unit != unit)
869 g_object_notify (G_OBJECT (op), "unit");
874 _gtk_print_operation_set_status (GtkPrintOperation *op,
875 GtkPrintStatus status,
878 GtkPrintOperationPrivate *priv = op->priv;
879 static const gchar *status_strs[] = {
880 /* translators, strip the prefix up to and including the first | */
881 N_("print operation status|Initial state"),
882 /* translators, strip the prefix up to and including the first | */
883 N_("print operation status|Preparing to print"),
884 /* translators, strip the prefix up to and including the first | */
885 N_("print operation status|Generating data"),
886 /* translators, strip the prefix up to and including the first | */
887 N_("print operation status|Sending data"),
888 /* translators, strip the prefix up to and including the first | */
889 N_("print operation status|Waiting"),
890 /* translators, strip the prefix up to and including the first | */
891 N_("print operation status|Blocking on issue"),
892 /* translators, strip the prefix up to and including the first | */
893 N_("print operation status|Printing"),
894 /* translators, strip the prefix up to and including the first | */
895 N_("print operation status|Finished"),
896 /* translators, strip the prefix up to and including the first | */
897 N_("print operation status|Finished with error")
900 if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
901 status = GTK_PRINT_STATUS_FINISHED_ABORTED;
904 string = g_strip_context (status_strs[status],
905 gettext (status_strs[status]));
907 if (priv->status == status &&
908 strcmp (string, priv->status_string) == 0)
911 g_free (priv->status_string);
912 priv->status_string = g_strdup (string);
913 priv->status = status;
915 g_object_notify (G_OBJECT (op), "status");
916 g_object_notify (G_OBJECT (op), "status-string");
918 g_signal_emit (op, signals[STATUS_CHANGED], 0);
923 * gtk_print_operation_get_status:
924 * @op: a #GtkPrintOperation
926 * Returns the status of the print operation.
927 * Also see gtk_print_operation_get_status_string().
929 * Return value: the status of the print operation
934 gtk_print_operation_get_status (GtkPrintOperation *op)
936 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
937 GTK_PRINT_STATUS_FINISHED_ABORTED);
939 return op->priv->status;
943 * gtk_print_operation_get_status_string:
944 * @op: a #GtkPrintOperation
946 * Returns a string representation of the status of the
947 * print operation. The string is translated and suitable
948 * for displaying the print status e.g. in a #GtkStatusbar.
950 * Use gtk_print_operation_get_status() to obtain a status
951 * value that is suitable for programmatic use.
953 * Return value: a string representation of the status
954 * of the print operation
958 G_CONST_RETURN gchar *
959 gtk_print_operation_get_status_string (GtkPrintOperation *op)
961 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
963 return op->priv->status_string;
967 * gtk_print_operation_is_finished:
968 * @op: a #GtkPrintOperation
970 * A convenience function to find out if the print operation
971 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
972 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
974 * Return value: %TRUE, if the print operation is finished.
979 gtk_print_operation_is_finished (GtkPrintOperation *op)
981 GtkPrintOperationPrivate *priv;
983 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
987 priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
988 priv->status == GTK_PRINT_STATUS_FINISHED;
993 * gtk_print_operation_set_show_dialog:
994 * @op: a #GtkPrintOperation
995 * @show_dialog: %TRUE to show the print dialog
997 * Sets whether calling gtk_print_operation_run() will present
998 * a print dialog to the user, or just print to the default printer.
1003 gtk_print_operation_set_show_dialog (GtkPrintOperation *op,
1004 gboolean show_dialog)
1006 GtkPrintOperationPrivate *priv;
1008 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1012 show_dialog = show_dialog != FALSE;
1014 if (priv->show_dialog != show_dialog)
1016 priv->show_dialog = show_dialog;
1018 g_object_notify (G_OBJECT (op), "show-dialog");
1023 * gtk_print_operation_set_pdf_target:
1024 * @op: a #GtkPrintOperation
1025 * @filename: the filename for the PDF file
1027 * Sets up the #GtkPrintOperation to generate a PDF file instead
1028 * of showing the print dialog. The indended use of this function
1029 * is for implementing "Export to PDF" actions.
1031 * "Print to PDF" support is independent of this and is done
1032 * by letting the user pick the "Print to PDF" item from the list
1033 * of printers in the print dialog.
1038 gtk_print_operation_set_pdf_target (GtkPrintOperation *op,
1039 const gchar *filename)
1041 GtkPrintOperationPrivate *priv;
1043 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1047 g_free (priv->pdf_target);
1048 priv->pdf_target = g_strdup (filename);
1050 g_object_notify (G_OBJECT (op), "pdf-target");
1053 /* Creates the initial page setup used for printing unless the
1054 * app overrides this on a per-page basis using request_page_setup.
1056 * Data is taken from, in order, if existing:
1058 * PrintSettings returned from the print dialog
1059 * (initial dialog values are set from default_page_setup
1060 * if unset in app specified print_settings)
1061 * default_page_setup
1062 * per-locale default setup
1064 static GtkPageSetup *
1065 create_page_setup (GtkPrintOperation *op)
1067 GtkPrintOperationPrivate *priv = op->priv;
1068 GtkPageSetup *page_setup;
1069 GtkPrintSettings *settings;
1071 if (priv->default_page_setup)
1072 page_setup = gtk_page_setup_copy (priv->default_page_setup);
1074 page_setup = gtk_page_setup_new ();
1076 settings = priv->print_settings;
1079 GtkPaperSize *paper_size;
1081 if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
1082 gtk_page_setup_set_orientation (page_setup,
1083 gtk_print_settings_get_orientation (settings));
1086 paper_size = gtk_print_settings_get_paper_size (settings);
1089 gtk_page_setup_set_paper_size (page_setup, paper_size);
1090 gtk_paper_size_free (paper_size);
1093 /* TODO: Margins? */
1100 pdf_start_page (GtkPrintOperation *op,
1101 GtkPrintContext *print_context,
1102 GtkPageSetup *page_setup)
1104 GtkPaperSize *paper_size;
1107 paper_size = gtk_page_setup_get_paper_size (page_setup);
1109 w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1110 h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1112 cairo_pdf_surface_set_size (op->priv->surface, w, h);
1116 pdf_end_page (GtkPrintOperation *op,
1117 GtkPrintContext *print_context)
1121 cr = gtk_print_context_get_cairo (print_context);
1122 cairo_show_page (cr);
1126 pdf_end_run (GtkPrintOperation *op,
1129 GtkPrintOperationPrivate *priv = op->priv;
1131 cairo_surface_destroy (priv->surface);
1132 priv->surface = NULL;
1135 static GtkPrintOperationResult
1136 run_pdf (GtkPrintOperation *op,
1141 GtkPrintOperationPrivate *priv = op->priv;
1142 GtkPageSetup *page_setup;
1143 double width, height;
1144 /* This will be overwritten later by the non-default size, but
1145 we need to pass some size: */
1147 page_setup = create_page_setup (op);
1148 width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
1149 height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
1150 g_object_unref (page_setup);
1152 priv->surface = cairo_pdf_surface_create (priv->pdf_target,
1154 cairo_pdf_surface_set_dpi (priv->surface, 300, 300);
1159 priv->print_pages = GTK_PRINT_PAGES_ALL;
1160 priv->page_ranges = NULL;
1161 priv->num_page_ranges = 0;
1163 priv->manual_num_copies = 1;
1164 priv->manual_collation = FALSE;
1165 priv->manual_reverse = FALSE;
1166 priv->manual_page_set = GTK_PAGE_SET_ALL;
1167 priv->manual_scale = 1.0;
1168 priv->manual_orientation = TRUE;
1172 priv->start_page = pdf_start_page;
1173 priv->end_page = pdf_end_page;
1174 priv->end_run = pdf_end_run;
1176 return GTK_PRINT_OPERATION_RESULT_APPLY;
1180 print_pages (GtkPrintOperation *op,
1183 GtkPrintOperationPrivate *priv = op->priv;
1185 GtkPageSetup *initial_page_setup, *page_setup;
1186 GtkPrintContext *print_context;
1188 int uncollated_copies, collated_copies;
1190 GtkPageRange *ranges;
1191 GtkPageRange one_range;
1194 if (priv->manual_collation)
1196 uncollated_copies = priv->manual_num_copies;
1197 collated_copies = 1;
1201 uncollated_copies = 1;
1202 collated_copies = priv->manual_num_copies;
1205 print_context = _gtk_print_context_new (op);
1207 initial_page_setup = create_page_setup (op);
1208 _gtk_print_context_set_page_setup (print_context, initial_page_setup);
1210 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
1211 g_signal_emit (op, signals[BEGIN_PRINT], 0, print_context);
1213 g_return_if_fail (priv->nr_of_pages > 0);
1215 if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
1217 ranges = priv->page_ranges;
1218 num_ranges = priv->num_page_ranges;
1220 else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
1221 priv->current_page != -1)
1223 ranges = &one_range;
1225 ranges[0].start = priv->current_page;
1226 ranges[0].end = priv->current_page;
1230 ranges = &one_range;
1232 ranges[0].start = 0;
1233 ranges[0].end = priv->nr_of_pages - 1;
1236 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_GENERATING_DATA, NULL);
1238 for (i = 0; i < uncollated_copies; i++)
1240 for (range = 0; range < num_ranges; range ++)
1242 int start, end, inc;
1243 int low = ranges[range].start;
1244 int high = ranges[range].end;
1246 if (priv->manual_reverse)
1258 for (page = start; page != end; page += inc)
1260 if ((priv->manual_page_set == GTK_PAGE_SET_EVEN && page % 2 == 0) ||
1261 (priv->manual_page_set == GTK_PAGE_SET_ODD && page % 2 == 1))
1264 for (j = 0; j < collated_copies; j++)
1266 page_setup = gtk_page_setup_copy (initial_page_setup);
1267 g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0, print_context, page, page_setup);
1269 _gtk_print_context_set_page_setup (print_context, page_setup);
1270 priv->start_page (op, print_context, page_setup);
1272 cr = gtk_print_context_get_cairo (print_context);
1275 if (priv->manual_scale != 100.0)
1278 priv->manual_scale);
1280 if (priv->manual_orientation)
1281 _gtk_print_context_rotate_according_to_orientation (print_context);
1283 if (!priv->use_full_page)
1284 _gtk_print_context_translate_into_margin (print_context);
1286 g_signal_emit (op, signals[DRAW_PAGE], 0,
1287 print_context, page);
1289 priv->end_page (op, print_context);
1293 g_object_unref (page_setup);
1295 /* Iterate the mainloop so that we redraw windows */
1296 while (gtk_events_pending ())
1297 gtk_main_iteration ();
1303 g_signal_emit (op, signals[END_PRINT], 0, print_context);
1305 g_object_unref (print_context);
1306 g_object_unref (initial_page_setup);
1308 cairo_surface_finish (priv->surface);
1309 priv->end_run (op, wait);
1313 * gtk_print_operation_run:
1314 * @op: a #GtkPrintOperation
1315 * @parent: Transient parent of the dialog, or %NULL
1316 * @error: Return location for errors, or %NULL
1318 * Runs the print operation, by first letting the user modify
1319 * print settings in the print dialog, and then print the
1322 * Note that this function does not return until the rendering of all
1323 * pages is complete. You can connect to the ::status-changed signal on
1324 * @op to obtain some information about the progress of the print operation.
1325 * Furthermore, it may use a recursive mainloop to show the print dialog.
1326 * See gtk_print_operation_run_async() if this is a problem.
1328 * <informalexample><programlisting>
1329 * FIXME: need an example here
1330 * </programlisting></informalexample>
1332 * Return value: the result of the print operation. A return value of
1333 * %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was
1334 * completed successfully. In this case, it is a good idea to obtain
1335 * the used print settings with gtk_print_operation_get_print_settings()
1336 * and store them for reuse with the next print operation.
1340 GtkPrintOperationResult
1341 gtk_print_operation_run (GtkPrintOperation *op,
1345 GtkPrintOperationPrivate *priv;
1346 GtkPrintOperationResult result;
1349 g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op),
1350 GTK_PRINT_OPERATION_RESULT_ERROR);
1354 if (priv->pdf_target != NULL)
1355 result = run_pdf (op, parent, &do_print, error);
1357 result = _gtk_print_operation_platform_backend_run_dialog (op,
1362 print_pages (op, TRUE);
1364 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1370 * gtk_print_operation_run_async:
1371 * @op: a #GtkPrintOperation
1372 * @parent: Transient parent of the dialog, or %NULL
1374 * Runs the print operation, by first letting the user modify
1375 * print settings in the print dialog, and then print the
1378 * In contrast to gtk_print_operation_run(), this function returns after
1379 * showing the print dialog on platforms that support this, and handles
1380 * the printing by connecting a signal handler to the ::response signal
1383 * If you use this function, it is recommended that you store the modified
1384 * #GtkPrintSettings in a ::begin-print or ::end-print signal handler.
1389 gtk_print_operation_run_async (GtkPrintOperation *op,
1392 GtkPrintOperationPrivate *priv;
1395 g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1399 if (priv->pdf_target != NULL)
1401 run_pdf (op, parent, &do_print, NULL);
1403 print_pages (op, FALSE);
1405 _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1408 _gtk_print_operation_platform_backend_run_dialog_async (op,
1414 #define __GTK_PRINT_OPERATION_C__
1415 #include "gtkaliasdef.c"