]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintoperation.c
Implement per-page paper sizes.
[~andy/gtk] / gtk / gtkprintoperation.c
1 /* GTK - The GIMP Toolkit
2  * gtkprintoperation.c: Print Operation
3  * Copyright (C) 2006, Red Hat, Inc.
4  *
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.
9  *
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.
14  *
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.
19  */
20
21 #include "config.h"
22 #include "string.h"
23 #include "gtkprintoperation-private.h"
24 #include "gtkmarshalers.h"
25 #include <cairo-pdf.h>
26 #include "gtkintl.h"
27 #include "gtkprivate.h"
28 #include "gtkalias.h"
29
30 #define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))
31
32 enum {
33   BEGIN_PRINT,
34   REQUEST_PAGE_SETUP,
35   DRAW_PAGE,
36   END_PRINT,
37   STATUS_CHANGED,
38   LAST_SIGNAL
39 };
40
41 enum {
42   PROP_0,
43   PROP_DEFAULT_PAGE_SETUP,
44   PROP_PRINT_SETTINGS,
45   PROP_JOB_NAME,
46   PROP_NR_OF_PAGES,
47   PROP_CURRENT_PAGE,
48   PROP_USE_FULL_PAGE,
49   PROP_UNIT,
50   PROP_SHOW_DIALOG,
51   PROP_PDF_TARGET,
52   PROP_STATUS,
53   PROP_STATUS_STRING
54 };
55
56 static guint signals[LAST_SIGNAL] = { 0 };
57 static int job_nr = 0;
58
59 G_DEFINE_TYPE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT)
60
61 /**
62  * gtk_print_error_quark:
63  *
64  * Registers an error quark for #GtkPrintOperation if necessary.
65  * 
66  * Return value: The error quark used for #GtkPrintOperation errors.
67  *
68  * Since: 2.10
69  **/
70 GQuark     
71 gtk_print_error_quark (void)
72 {
73   static GQuark quark = 0;
74   if (quark == 0)
75     quark = g_quark_from_static_string ("gtk-print-error-quark");
76   return quark;
77 }
78      
79 static void
80 gtk_print_operation_finalize (GObject *object)
81 {
82   GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
83   GtkPrintOperationPrivate *priv = print_operation->priv;
84
85   if (priv->free_platform_data &&
86       priv->platform_data)
87     {
88       priv->free_platform_data (priv->platform_data);
89       priv->free_platform_data = NULL;
90     }
91
92   if (priv->default_page_setup)
93     g_object_unref (priv->default_page_setup);
94   
95   if (priv->print_settings)
96     g_object_unref (priv->print_settings);
97   
98   g_free (priv->pdf_target);
99   g_free (priv->job_name);
100
101   G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
102 }
103
104 static void
105 gtk_print_operation_init (GtkPrintOperation *operation)
106 {
107   GtkPrintOperationPrivate *priv;
108   const char *appname;
109
110   priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
111
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;
121
122   priv->unit = GTK_UNIT_PIXEL;
123
124   appname = g_get_application_name ();
125   priv->job_name = g_strdup_printf ("%s job #%d", appname, ++job_nr);
126 }
127
128 static void
129 gtk_print_operation_set_property (GObject      *object,
130                                   guint         prop_id,
131                                   const GValue *value,
132                                   GParamSpec   *pspec)
133 {
134   GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
135   
136   switch (prop_id)
137     {
138     case PROP_DEFAULT_PAGE_SETUP:
139       gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
140       break;
141     case PROP_PRINT_SETTINGS:
142       gtk_print_operation_set_print_settings (op, g_value_get_object (value));
143       break;
144     case PROP_JOB_NAME:
145       gtk_print_operation_set_job_name (op, g_value_get_string (value));
146       break;
147     case PROP_NR_OF_PAGES:
148       gtk_print_operation_set_nr_of_pages (op, g_value_get_int (value));
149       break;
150     case PROP_CURRENT_PAGE:
151       gtk_print_operation_set_current_page (op, g_value_get_int (value));
152       break;
153     case PROP_USE_FULL_PAGE:
154       gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
155       break;
156     case PROP_UNIT:
157       gtk_print_operation_set_unit (op, g_value_get_enum (value));
158       break;
159     case PROP_SHOW_DIALOG:
160       gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
161       break;
162     case PROP_PDF_TARGET:
163       gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
164       break;
165     default:
166       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
167       break;
168     }
169 }
170
171 static void
172 gtk_print_operation_get_property (GObject    *object,
173                                   guint       prop_id,
174                                   GValue     *value,
175                                   GParamSpec *pspec)
176 {
177   GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
178   GtkPrintOperationPrivate *priv = op->priv;
179
180   switch (prop_id)
181     {
182     case PROP_DEFAULT_PAGE_SETUP:
183       g_value_set_object (value, priv->default_page_setup);
184       break;
185     case PROP_PRINT_SETTINGS:
186       g_value_set_object (value, priv->print_settings);
187       break;
188     case PROP_JOB_NAME:
189       g_value_set_string (value, priv->job_name);
190       break;
191     case PROP_NR_OF_PAGES:
192       g_value_set_int (value, priv->nr_of_pages);
193       break;
194     case PROP_CURRENT_PAGE:
195       g_value_set_int (value, priv->current_page);
196       break;      
197     case PROP_USE_FULL_PAGE:
198       g_value_set_boolean (value, priv->use_full_page);
199       break;
200     case PROP_UNIT:
201       g_value_set_enum (value, priv->unit);
202       break;
203     case PROP_SHOW_DIALOG:
204       g_value_set_boolean (value, priv->show_dialog);
205       break;
206     case PROP_PDF_TARGET:
207       g_value_set_string (value, priv->pdf_target);
208       break;
209     case PROP_STATUS:
210       g_value_set_enum (value, priv->status);
211       break;
212     case PROP_STATUS_STRING:
213       g_value_set_string (value, priv->status_string);
214       break;
215     default:
216       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
217       break;
218     }
219 }
220
221 static void
222 gtk_print_operation_class_init (GtkPrintOperationClass *class)
223 {
224   GObjectClass *gobject_class = (GObjectClass *)class;
225
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;
229   
230   g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
231
232   /**
233    * GtkPrintOperation::begin-print:
234    * @operation: the #GtkPrintOperation on which the signal was emitted
235    * @context: the #GtkPrintContext for the current operation
236    *
237    * Gets emitted after the user has finished changing print settings
238    * in the dialog, before the actual rendering starts. 
239    *
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().
243    *
244    * Since: 2.10
245    */
246   signals[BEGIN_PRINT] =
247     g_signal_new ("begin_print",
248                   G_TYPE_FROM_CLASS (gobject_class),
249                   G_SIGNAL_RUN_LAST,
250                   G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
251                   NULL, NULL,
252                   g_cclosure_marshal_VOID__OBJECT,
253                   G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
254
255   /**
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 
261    * 
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.
265    *
266    * Since: 2.10
267    */
268   signals[REQUEST_PAGE_SETUP] =
269     g_signal_new ("request_page_setup",
270                   G_TYPE_FROM_CLASS (gobject_class),
271                   G_SIGNAL_RUN_LAST,
272                   G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
273                   NULL, NULL,
274                   _gtk_marshal_VOID__OBJECT_INT_OBJECT,
275                   G_TYPE_NONE, 3,
276                   GTK_TYPE_PRINT_CONTEXT,
277                   G_TYPE_INT,
278                   GTK_TYPE_PAGE_SETUP);
279
280   /**
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
285    *
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().
289    *
290    * <informalexample><programlisting>
291    *  FIXME: need an example here
292    * </programlisting></informalexample>
293    *
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
297    * needs.
298    * 
299    * Since: 2.10
300    */
301   signals[DRAW_PAGE] =
302     g_signal_new ("draw_page",
303                   G_TYPE_FROM_CLASS (gobject_class),
304                   G_SIGNAL_RUN_LAST,
305                   G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
306                   NULL, NULL,
307                   _gtk_marshal_VOID__OBJECT_INT,
308                   G_TYPE_NONE, 2,
309                   GTK_TYPE_PRINT_CONTEXT,
310                   G_TYPE_INT);
311
312   /**
313    * GtkPrintOperation::end-print:
314    * @operation: the #GtkPrintOperation on which the signal was emitted
315    * @context: the #GtkPrintContext for the current operation
316    *
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.
320    * 
321    * Since: 2.10
322    */
323   signals[END_PRINT] =
324     g_signal_new ("end_print",
325                   G_TYPE_FROM_CLASS (gobject_class),
326                   G_SIGNAL_RUN_LAST,
327                   G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
328                   NULL, NULL,
329                   g_cclosure_marshal_VOID__OBJECT,
330                   G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
331
332   /**
333    * GtkPrintOperation::status-changed:
334    * @operation: the #GtkPrintOperation on which the signal was emitted
335    *
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
339    * status.
340    *
341    * Since: 2.10
342    */
343   signals[STATUS_CHANGED] =
344    g_signal_new ("status-changed",
345                  G_TYPE_FROM_CLASS (class),
346                  G_SIGNAL_RUN_LAST,
347                  G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
348                  NULL, NULL,
349                  g_cclosure_marshal_VOID__VOID,
350                  G_TYPE_NONE, 0);
351
352   /**
353    * GtkPrintOperation:default-page-setup:
354    *
355    * The #GtkPageSetup used by default.
356    * 
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.
360    *
361    * Since: 2.10
362    */
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"),
368                                                         GTK_TYPE_PAGE_SETUP,
369                                                         GTK_PARAM_READWRITE));
370
371   /**
372    * GtkPrintOperation:print-settings:
373    *
374    * The #GtkPrintSettings used for initializing the dialog.
375    *
376    * Setting this property is typically used to re-establish print 
377    * settings from a previous print operation, see gtk_print_operation_run().
378    *
379    * Since: 2.10
380    */
381   g_object_class_install_property (gobject_class,
382                                    PROP_PRINT_SETTINGS,
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));
388   
389   /**
390    * GtkPrintOperation:job-name:
391    *
392    * A string used to identify the job (e.g. in monitoring applications like eggcups). 
393    * 
394    * If you don't set a job name, GTK+ picks a default one by numbering successive 
395    * print jobs.
396    *
397    * Since: 2.10
398    */
399   g_object_class_install_property (gobject_class,
400                                    PROP_JOB_NAME,
401                                    g_param_spec_string ("job-name",
402                                                         P_("Job Name"),
403                                                         P_("A string used for identifying the print job."),
404                                                         "",
405                                                         GTK_PARAM_READWRITE));
406
407   /**
408    * GtkPrintOperation:number-of-pages:
409    *
410    * The number of pages in the document. 
411    *
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.
414    *
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.
418    *
419    * Since: 2.10
420    */
421   g_object_class_install_property (gobject_class,
422                                    PROP_NR_OF_PAGES,
423                                    g_param_spec_int ("number-of-pages",
424                                                      P_("Number of Pages"),
425                                                      P_("The number of pages in the document."),
426                                                      -1,
427                                                      G_MAXINT,
428                                                      -1,
429                                                      GTK_PARAM_READWRITE));
430
431   /**
432    * GtkPrintOperation:current-page:
433    *
434    * The current page in the document.
435    *
436    * If this is set before gtk_print_operation_run(), 
437    * the user will be able to select to print only the current page.
438    *
439    * Note that this only makes sense for pre-paginated documents.
440    *
441    * Since: 2.10
442    */
443   g_object_class_install_property (gobject_class,
444                                    PROP_CURRENT_PAGE,
445                                    g_param_spec_int ("current-page",
446                                                      P_("Current Page"),
447                                                      P_("The current page in the document."),
448                                                      -1,
449                                                      G_MAXINT,
450                                                      -1,
451                                                      GTK_PARAM_READWRITE));
452   
453   /**
454    * GtkPrintOperation:use-full-page:
455    *
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).
461    * 
462    * Since: 2.10
463    */
464   g_object_class_install_property (gobject_class,
465                                    PROP_USE_FULL_PAGE,
466                                    g_param_spec_boolean ("use-full-page",
467                                                          P_("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"),
469                                                          FALSE,
470                                                          GTK_PARAM_READWRITE));
471   
472
473   /**
474    * GtkPrintOperation:unit:
475    *
476    * The transformation for the cairo context obtained from
477    * #GtkPrintContext is set up in such a way that distances are measured 
478    * in units of @unit.
479    *
480    * Since: 2.10
481    */
482   g_object_class_install_property (gobject_class,
483                                    PROP_UNIT,
484                                    g_param_spec_enum ("unit",
485                                                       P_("Unit"),
486                                                       P_("The unit in which distances can be measured in the context"),
487                                                       GTK_TYPE_UNIT,
488                                                       GTK_UNIT_PIXEL,
489                                                       GTK_PARAM_READWRITE));
490   
491
492   /**
493    * GtkPrintOperation:show-dialog:
494    *
495    * Determines whether calling gtk_print_operation_run() will present
496    * a print dialog to the user, or just print to the default printer.
497    *
498    * Since: 2.10
499    */
500   g_object_class_install_property (gobject_class,
501                                    PROP_SHOW_DIALOG,
502                                    g_param_spec_boolean ("show-dialog",
503                                                          P_("Show Dialog"),
504                                                          P_("%TRUE if gtk_print_operation_run() should show the print dialog."),
505                                                          TRUE,
506                                                          GTK_PARAM_READWRITE));
507   
508   /**
509    * GtkPrintOperation:pdf-target:
510    *
511    * The name of a PDF file to generate instead of showing the print dialog. 
512    *
513    * The indended use of this property is for implementing "Export to PDF" 
514    * actions.
515    *
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.
519    *
520    * Since: 2.10
521    */
522   g_object_class_install_property (gobject_class,
523                                    PROP_JOB_NAME,
524                                    g_param_spec_string ("pdf-target",
525                                                         P_("PDF target filename"),
526                                                         P_(""),
527                                                         NULL,
528                                                         GTK_PARAM_READWRITE));
529   
530   /**
531    * GtkPrintOperation:status:
532    *
533    * The status of the print operation.
534    * 
535    * Since: 2.10
536    */
537   g_object_class_install_property (gobject_class,
538                                    PROP_STATUS,
539                                    g_param_spec_enum ("status",
540                                                       P_("Status"),
541                                                       P_("The status of the print operation"),
542                                                       GTK_TYPE_PRINT_STATUS,
543                                                       GTK_PRINT_STATUS_INITIAL,
544                                                       GTK_PARAM_READABLE));
545   
546   /**
547    * GtkPrintOperation:status-string:
548    *
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.
552    *
553    * See the ::status property for a status value that is suitable 
554    * for programmatic use. 
555    *
556    * Since: 2.10
557    */
558   g_object_class_install_property (gobject_class,
559                                    PROP_STATUS_STRING,
560                                    g_param_spec_string ("status-string",
561                                                         P_("Status String"),
562                                                         P_("A human-readable description of the status"),
563                                                         "",
564                                                         GTK_PARAM_READABLE));
565   
566
567 }
568
569 /**
570  * gtk_print_operation_new:
571  *
572  * Creates a new #GtkPrintOperation. 
573  *
574  * Returns: a new #GtkPrintOperation
575  *
576  * Since: 2.10
577  */
578 GtkPrintOperation *
579 gtk_print_operation_new (void)
580 {
581   GtkPrintOperation *print_operation;
582
583   print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
584   
585   return print_operation;
586 }
587
588 /**
589  * gtk_print_operation_set_default_page_setup:
590  * @op: a #GtkPrintOperation
591  * @default_page_setup: a #GtkPageSetup, or %NULL
592  * 
593  * Makes @default_page_setup the default page setup for @op.
594  *
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.
598  *
599  * Since: 2.10
600  **/
601 void
602 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
603                                             GtkPageSetup      *default_page_setup)
604 {
605   GtkPrintOperationPrivate *priv;
606
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));
610
611   priv = op->priv;
612
613   if (default_page_setup != priv->default_page_setup)
614     {
615       if (default_page_setup)
616         g_object_ref (default_page_setup);
617       
618       if (priv->default_page_setup)
619         g_object_unref (priv->default_page_setup);
620       
621       priv->default_page_setup = default_page_setup;
622      
623       g_object_notify (G_OBJECT (op), "default-page-setup");
624     }
625 }
626
627 /**
628  * gtk_print_operation_get_default_page_setup:
629  * @op: a #GtkPrintOperation
630  *
631  * Returns the default page setup, see 
632  * gtk_print_operation_set_default_page_setup().
633  *
634  * Returns: the default page setup 
635  *
636  * Since: 2.10
637  */
638 GtkPageSetup *
639 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
640 {
641   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
642
643   return op->priv->default_page_setup;
644 }
645
646
647 /**
648  * gtk_print_operation_set_print_settings:
649  * @op: a #GtkPrintOperation
650  * @print_settings: #GtkPrintSettings, or %NULL
651  * 
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().
655  *
656  * Since: 2.10
657  **/
658 void
659 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
660                                         GtkPrintSettings  *print_settings)
661 {
662   GtkPrintOperationPrivate *priv;
663
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));
667
668   priv = op->priv;
669
670   if (print_settings != priv->print_settings)
671     {
672       if (print_settings)
673         g_object_ref (print_settings);
674
675       if (priv->print_settings)
676         g_object_unref (priv->print_settings);
677   
678       priv->print_settings = print_settings;
679
680       g_object_notify (G_OBJECT (op), "print-settings");
681     }
682 }
683
684 /**
685  * gtk_print_operation_get_print_settings:
686  * @op: a #GtkPrintOperation
687  * 
688  * Returns the current print settings. 
689  *
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.
693  * 
694  * Return value: the current print settings of @op.
695  * 
696  * Since: 2.10
697  **/
698 GtkPrintSettings *
699 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
700 {
701   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
702
703   return op->priv->print_settings;
704 }
705
706 /**
707  * gtk_print_operation_set_job_name:
708  * @op: a #GtkPrintOperation
709  * @job_name: a string that identifies the print job
710  * 
711  * Sets the name of the print job. The name is used to identify 
712  * the job (e.g. in monitoring applications like eggcups). 
713  * 
714  * If you don't set a job name, GTK+ picks a default one by 
715  * numbering successive print jobs.
716  *
717  * Since: 2.10
718  **/
719 void
720 gtk_print_operation_set_job_name (GtkPrintOperation *op,
721                                   const gchar       *job_name)
722 {
723   GtkPrintOperationPrivate *priv;
724
725   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
726   g_return_if_fail (g_utf8_validate (job_name, -1, NULL));
727
728   priv = op->priv;
729
730   g_free (priv->job_name);
731   priv->job_name = g_strdup (job_name);
732
733   g_object_notify (G_OBJECT (op), "job-name");
734 }
735
736 /**
737  * gtk_print_operation_set_nr_of_pages:
738  * @op: a #GtkPrintOperation
739  * @n_pages: the number of pages
740  * 
741  * Sets the number of pages in the document. 
742  *
743  * This <emphasis>must</emphasis> be set to a positive number
744  * before the rendering starts. It may be set in a ::begin-print 
745  * signal hander.
746  *
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.
751  *
752  * Since: 2.10
753  **/
754 void
755 gtk_print_operation_set_nr_of_pages (GtkPrintOperation *op,
756                                      gint               n_pages)
757 {
758   GtkPrintOperationPrivate *priv;
759
760   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
761   g_return_if_fail (n_pages > 0);
762
763   priv = op->priv;
764   g_return_if_fail (priv->current_page == -1 || 
765                     priv->current_page < n_pages);
766
767   if (priv->nr_of_pages != n_pages)
768     {
769       priv->nr_of_pages = n_pages;
770
771       g_object_notify (G_OBJECT (op), "number-of-pages");
772     }
773 }
774
775 /**
776  * gtk_print_operation_set_current_page:
777  * @op: a #GtkPrintOperation
778  * @current_page: the current page, 0-based
779  *
780  * Sets the current page.
781  *
782  * If this is called before gtk_print_operation_run(), 
783  * the user will be able to select to print only the current page.
784  *
785  * Note that this only makes sense for pre-paginated documents.
786  * 
787  * Since: 2.10
788  **/
789 void
790 gtk_print_operation_set_current_page (GtkPrintOperation *op,
791                                       gint               current_page)
792 {
793   GtkPrintOperationPrivate *priv;
794
795   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
796   g_return_if_fail (current_page >= 0);
797
798   priv = op->priv;
799   g_return_if_fail (priv->nr_of_pages == -1 || 
800                     current_page < priv->nr_of_pages);
801
802   if (priv->current_page != current_page)
803     {
804       priv->current_page = current_page;
805
806       g_object_notify (G_OBJECT (op), "current-page");
807     }
808 }
809
810 /**
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
814  * 
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).
821  * 
822  * Since: 2.10 
823  */
824 void
825 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
826                                        gboolean           full_page)
827 {
828   GtkPrintOperationPrivate *priv;
829
830   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
831
832   full_page = full_page != FALSE;
833  
834   priv = op->priv;
835         
836   if (priv->use_full_page != full_page)
837     {
838       priv->use_full_page = full_page;
839    
840       g_object_notify (G_OBJECT (op), "use-full-page");
841     }
842 }
843
844 /**
845  * gtk_print_operation_set_unit:
846  * @op: a #GtkPrintOperation
847  * @unit: the unit to use
848  * 
849  * Sets up the transformation for the cairo context obtained from
850  * #GtkPrintContext in such a way that distances are measured in 
851  * units of @unit.
852  *
853  * Since: 2.10
854  */
855 void
856 gtk_print_operation_set_unit (GtkPrintOperation *op,
857                               GtkUnit            unit)
858 {
859   GtkPrintOperationPrivate *priv;
860
861   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
862
863   priv = op->priv;
864
865   if (priv->unit != unit)
866     {
867       priv->unit = unit;
868
869       g_object_notify (G_OBJECT (op), "unit");
870     }
871 }
872
873 void
874 _gtk_print_operation_set_status (GtkPrintOperation *op,
875                                  GtkPrintStatus     status,
876                                  const gchar       *string)
877 {
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")
898   };
899
900   if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
901     status = GTK_PRINT_STATUS_FINISHED_ABORTED;
902
903   if (string == NULL)
904     string = g_strip_context (status_strs[status],
905                               gettext (status_strs[status]));
906   
907   if (priv->status == status &&
908       strcmp (string, priv->status_string) == 0)
909     return;
910   
911   g_free (priv->status_string);
912   priv->status_string = g_strdup (string);
913   priv->status = status;
914
915   g_object_notify (G_OBJECT (op), "status");
916   g_object_notify (G_OBJECT (op), "status-string");
917
918   g_signal_emit (op, signals[STATUS_CHANGED], 0);
919 }
920
921
922 /**
923  * gtk_print_operation_get_status:
924  * @op: a #GtkPrintOperation
925  * 
926  * Returns the status of the print operation. 
927  * Also see gtk_print_operation_get_status_string().
928  * 
929  * Return value: the status of the print operation
930  *
931  * Since: 2.10
932  **/
933 GtkPrintStatus
934 gtk_print_operation_get_status (GtkPrintOperation *op)
935 {
936   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
937                         GTK_PRINT_STATUS_FINISHED_ABORTED);
938
939   return op->priv->status;
940 }
941
942 /**
943  * gtk_print_operation_get_status_string:
944  * @op: a #GtkPrintOperation
945  * 
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.
949  *
950  * Use gtk_print_operation_get_status() to obtain a status
951  * value that is suitable for programmatic use. 
952  * 
953  * Return value: a string representation of the status
954  *    of the print operation
955  *
956  * Since: 2.10
957  **/
958 G_CONST_RETURN gchar *
959 gtk_print_operation_get_status_string (GtkPrintOperation *op)
960 {
961   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
962
963   return op->priv->status_string;
964 }
965
966 /**
967  * gtk_print_operation_is_finished:
968  * @op: a #GtkPrintOperation
969  * 
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).
973  * 
974  * Return value: %TRUE, if the print operation is finished.
975  *
976  * Since: 2.10
977  **/
978 gboolean
979 gtk_print_operation_is_finished (GtkPrintOperation *op)
980 {
981   GtkPrintOperationPrivate *priv;
982
983   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
984
985   priv = op->priv;
986   return
987     priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
988     priv->status == GTK_PRINT_STATUS_FINISHED;
989 }
990
991
992 /**
993  * gtk_print_operation_set_show_dialog:
994  * @op: a #GtkPrintOperation
995  * @show_dialog: %TRUE to show the print dialog
996  * 
997  * Sets whether calling gtk_print_operation_run() will present
998  * a print dialog to the user, or just print to the default printer.
999  *
1000  * Since: 2.10
1001  */
1002 void
1003 gtk_print_operation_set_show_dialog (GtkPrintOperation *op,
1004                                      gboolean           show_dialog)
1005 {
1006   GtkPrintOperationPrivate *priv;
1007
1008   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1009
1010   priv = op->priv;
1011
1012   show_dialog = show_dialog != FALSE;
1013
1014   if (priv->show_dialog != show_dialog)
1015     {
1016       priv->show_dialog = show_dialog;
1017
1018       g_object_notify (G_OBJECT (op), "show-dialog");
1019     }
1020 }
1021
1022 /**
1023  * gtk_print_operation_set_pdf_target:
1024  * @op: a #GtkPrintOperation
1025  * @filename: the filename for the PDF file
1026  * 
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.
1030  *
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.
1034  *
1035  * Since: 2.10
1036  */
1037 void
1038 gtk_print_operation_set_pdf_target (GtkPrintOperation *op,
1039                                     const gchar       *filename)
1040 {
1041   GtkPrintOperationPrivate *priv;
1042
1043   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1044
1045   priv = op->priv;
1046
1047   g_free (priv->pdf_target);
1048   priv->pdf_target = g_strdup (filename);
1049
1050   g_object_notify (G_OBJECT (op), "pdf-target");
1051 }
1052
1053 /* Creates the initial page setup used for printing unless the
1054  * app overrides this on a per-page basis using request_page_setup.
1055  *
1056  * Data is taken from, in order, if existing:
1057  *
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
1063  */
1064 static GtkPageSetup *
1065 create_page_setup (GtkPrintOperation *op)
1066 {
1067   GtkPrintOperationPrivate *priv = op->priv;
1068   GtkPageSetup *page_setup;
1069   GtkPrintSettings *settings;
1070   
1071   if (priv->default_page_setup)
1072     page_setup = gtk_page_setup_copy (priv->default_page_setup);
1073   else
1074     page_setup = gtk_page_setup_new ();
1075
1076   settings = priv->print_settings;
1077   if (settings)
1078     {
1079       GtkPaperSize *paper_size;
1080       
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));
1084
1085
1086       paper_size = gtk_print_settings_get_paper_size (settings);
1087       if (paper_size)
1088         {
1089           gtk_page_setup_set_paper_size (page_setup, paper_size);
1090           gtk_paper_size_free (paper_size);
1091         }
1092
1093       /* TODO: Margins? */
1094     }
1095   
1096   return page_setup;
1097 }
1098
1099 static void 
1100 pdf_start_page (GtkPrintOperation *op,
1101                 GtkPrintContext   *print_context,
1102                 GtkPageSetup      *page_setup)
1103 {
1104   GtkPaperSize *paper_size;
1105   double w, h;
1106
1107   paper_size = gtk_page_setup_get_paper_size (page_setup);
1108
1109   w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
1110   h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
1111   
1112   cairo_pdf_surface_set_size (op->priv->surface, w, h);
1113 }
1114
1115 static void
1116 pdf_end_page (GtkPrintOperation *op,
1117               GtkPrintContext   *print_context)
1118 {
1119   cairo_t *cr;
1120
1121   cr = gtk_print_context_get_cairo (print_context);
1122   cairo_show_page (cr);
1123 }
1124
1125 static void
1126 pdf_end_run (GtkPrintOperation *op,
1127              gboolean wait)
1128 {
1129   GtkPrintOperationPrivate *priv = op->priv;
1130
1131   cairo_surface_destroy (priv->surface);
1132   priv->surface = NULL;
1133 }
1134
1135 static GtkPrintOperationResult
1136 run_pdf (GtkPrintOperation  *op,
1137          GtkWindow          *parent,
1138          gboolean           *do_print,
1139          GError            **error)
1140 {
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: */
1146   
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);
1151   
1152   priv->surface = cairo_pdf_surface_create (priv->pdf_target,
1153                                             width, height);
1154   cairo_pdf_surface_set_dpi (priv->surface, 300, 300);
1155   
1156   priv->dpi_x = 72;
1157   priv->dpi_y = 72;
1158
1159   priv->print_pages = GTK_PRINT_PAGES_ALL;
1160   priv->page_ranges = NULL;
1161   priv->num_page_ranges = 0;
1162
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;
1169   
1170   *do_print = TRUE;
1171   
1172   priv->start_page = pdf_start_page;
1173   priv->end_page = pdf_end_page;
1174   priv->end_run = pdf_end_run;
1175   
1176   return GTK_PRINT_OPERATION_RESULT_APPLY; 
1177 }
1178
1179 static void
1180 print_pages (GtkPrintOperation *op,
1181              gboolean wait)
1182 {
1183   GtkPrintOperationPrivate *priv = op->priv;
1184   int page, range;
1185   GtkPageSetup *initial_page_setup, *page_setup;
1186   GtkPrintContext *print_context;
1187   cairo_t *cr;
1188   int uncollated_copies, collated_copies;
1189   int i, j;
1190   GtkPageRange *ranges;
1191   GtkPageRange one_range;
1192   int num_ranges;
1193
1194   if (priv->manual_collation)
1195     {
1196       uncollated_copies = priv->manual_num_copies;
1197       collated_copies = 1;
1198     }
1199   else
1200     {
1201       uncollated_copies = 1;
1202       collated_copies = priv->manual_num_copies;
1203     }
1204
1205   print_context = _gtk_print_context_new (op);
1206
1207   initial_page_setup = create_page_setup (op);
1208   _gtk_print_context_set_page_setup (print_context, initial_page_setup);
1209
1210   _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
1211   g_signal_emit (op, signals[BEGIN_PRINT], 0, print_context);
1212   
1213   g_return_if_fail (priv->nr_of_pages > 0);
1214
1215   if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
1216     {
1217       ranges = priv->page_ranges;
1218       num_ranges = priv->num_page_ranges;
1219     }
1220   else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
1221            priv->current_page != -1)
1222     {
1223       ranges = &one_range;
1224       num_ranges = 1;
1225       ranges[0].start = priv->current_page;
1226       ranges[0].end = priv->current_page;
1227     }
1228   else
1229     {
1230       ranges = &one_range;
1231       num_ranges = 1;
1232       ranges[0].start = 0;
1233       ranges[0].end = priv->nr_of_pages - 1;
1234     }
1235   
1236   _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_GENERATING_DATA, NULL);
1237
1238   for (i = 0; i < uncollated_copies; i++)
1239     {
1240       for (range = 0; range < num_ranges; range ++)
1241         {
1242           int start, end, inc;
1243           int low = ranges[range].start;
1244           int high = ranges[range].end;
1245           
1246           if (priv->manual_reverse)
1247             {
1248               start = high;
1249               end = low - 1;
1250               inc = -1;
1251             }
1252           else
1253             {
1254               start = low;
1255               end = high + 1;
1256               inc = 1;
1257             }
1258           for (page = start; page != end; page += inc)
1259             {
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))
1262                 continue;
1263               
1264               for (j = 0; j < collated_copies; j++)
1265                 {
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);
1268                   
1269                   _gtk_print_context_set_page_setup (print_context, page_setup);
1270                   priv->start_page (op, print_context, page_setup);
1271                   
1272                   cr = gtk_print_context_get_cairo (print_context);
1273
1274                   cairo_save (cr);
1275                   if (priv->manual_scale != 100.0)
1276                     cairo_scale (cr,
1277                                  priv->manual_scale,
1278                                  priv->manual_scale);
1279                   
1280                   if (priv->manual_orientation)
1281                     _gtk_print_context_rotate_according_to_orientation (print_context);
1282
1283                   if (!priv->use_full_page)
1284                     _gtk_print_context_translate_into_margin (print_context);
1285           
1286                   g_signal_emit (op, signals[DRAW_PAGE], 0, 
1287                                  print_context, page);
1288                   
1289                   priv->end_page (op, print_context);
1290                   
1291                   cairo_restore (cr);
1292                   
1293                   g_object_unref (page_setup);
1294
1295                   /* Iterate the mainloop so that we redraw windows */
1296                   while (gtk_events_pending ())
1297                     gtk_main_iteration ();
1298                 }
1299             }
1300         }
1301     }
1302   
1303   g_signal_emit (op, signals[END_PRINT], 0, print_context);
1304
1305   g_object_unref (print_context);
1306   g_object_unref (initial_page_setup);
1307
1308   cairo_surface_finish (priv->surface);
1309   priv->end_run (op, wait);
1310 }
1311
1312 /**
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
1317  * 
1318  * Runs the print operation, by first letting the user modify
1319  * print settings in the print dialog, and then print the
1320  * document.
1321  *
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.
1327  * 
1328  * <informalexample><programlisting>
1329  *  FIXME: need an example here
1330  * </programlisting></informalexample>
1331  *
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.
1337  *
1338  * Since: 2.10
1339  **/
1340 GtkPrintOperationResult
1341 gtk_print_operation_run (GtkPrintOperation  *op,
1342                          GtkWindow          *parent,
1343                          GError            **error)
1344 {
1345   GtkPrintOperationPrivate *priv;
1346   GtkPrintOperationResult result;
1347   gboolean do_print;
1348   
1349   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
1350                         GTK_PRINT_OPERATION_RESULT_ERROR);
1351
1352   priv = op->priv;
1353
1354   if (priv->pdf_target != NULL)
1355     result = run_pdf (op, parent, &do_print, error);
1356   else
1357     result = _gtk_print_operation_platform_backend_run_dialog (op, 
1358                                                                parent,
1359                                                                &do_print,
1360                                                                error);
1361   if (do_print)
1362     print_pages (op, TRUE);
1363   else 
1364     _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1365
1366   return result;
1367 }
1368
1369 /**
1370  * gtk_print_operation_run_async:
1371  * @op: a #GtkPrintOperation
1372  * @parent: Transient parent of the dialog, or %NULL
1373  * 
1374  * Runs the print operation, by first letting the user modify
1375  * print settings in the print dialog, and then print the
1376  * document.
1377  *
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 
1381  * of the dialog. 
1382  * 
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.
1385  * 
1386  * Since: 2.10
1387  **/
1388 void
1389 gtk_print_operation_run_async (GtkPrintOperation *op,
1390                                GtkWindow         *parent)
1391 {
1392   GtkPrintOperationPrivate *priv;
1393   gboolean do_print;
1394
1395   g_return_if_fail (GTK_IS_PRINT_OPERATION (op)); 
1396
1397   priv = op->priv;
1398
1399   if (priv->pdf_target != NULL)
1400     {
1401       run_pdf (op, parent, &do_print, NULL);
1402       if (do_print)
1403         print_pages (op, FALSE);
1404       else 
1405         _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1406     }
1407   else
1408     _gtk_print_operation_platform_backend_run_dialog_async (op, 
1409                                                             parent,
1410                                                             print_pages);
1411 }
1412
1413
1414 #define __GTK_PRINT_OPERATION_C__
1415 #include "gtkaliasdef.c"