]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintoperation.c
Merge the gtk-printing branch. For more detailed ChangeLog entries, see
[~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
84   if (print_operation->priv->free_platform_data &&
85       print_operation->priv->platform_data)
86     {
87       print_operation->priv->free_platform_data (print_operation->priv->platform_data);
88       print_operation->priv->free_platform_data = NULL;
89     }
90
91   if (print_operation->priv->default_page_setup)
92     g_object_unref (print_operation->priv->default_page_setup);
93   
94   if (print_operation->priv->print_settings)
95     g_object_unref (print_operation->priv->print_settings);
96   
97   g_free (print_operation->priv->pdf_target);
98   g_free (print_operation->priv->job_name);
99
100   G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
101 }
102
103 static void
104 gtk_print_operation_init (GtkPrintOperation *operation)
105 {
106   const char *appname;
107
108   operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
109
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;
119
120   operation->priv->unit = GTK_UNIT_PIXEL;
121
122   appname = g_get_application_name ();
123   operation->priv->job_name = g_strdup_printf ("%s job #%d",
124                                                appname, ++job_nr);
125 }
126
127 static void
128 gtk_print_operation_set_property (GObject      *object,
129                                   guint         prop_id,
130                                   const GValue *value,
131                                   GParamSpec   *pspec)
132 {
133   GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
134   
135   switch (prop_id)
136     {
137     case PROP_DEFAULT_PAGE_SETUP:
138       gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
139       break;
140     case PROP_PRINT_SETTINGS:
141       gtk_print_operation_set_print_settings (op, g_value_get_object (value));
142       break;
143     case PROP_JOB_NAME:
144       gtk_print_operation_set_job_name (op, g_value_get_string (value));
145       break;
146     case PROP_NR_OF_PAGES:
147       gtk_print_operation_set_nr_of_pages (op, g_value_get_int (value));
148       break;
149     case PROP_CURRENT_PAGE:
150       gtk_print_operation_set_current_page (op, g_value_get_int (value));
151       break;
152     case PROP_USE_FULL_PAGE:
153       gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
154       break;
155     case PROP_UNIT:
156       gtk_print_operation_set_unit (op, g_value_get_enum (value));
157       break;
158     case PROP_SHOW_DIALOG:
159       gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
160       break;
161     case PROP_PDF_TARGET:
162       gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
163       break;
164     default:
165       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
166       break;
167     }
168 }
169
170 static void
171 gtk_print_operation_get_property (GObject    *object,
172                                   guint       prop_id,
173                                   GValue     *value,
174                                   GParamSpec *pspec)
175 {
176   GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
177   
178   switch (prop_id)
179     {
180     case PROP_DEFAULT_PAGE_SETUP:
181       g_value_set_object (value, op->priv->default_page_setup);
182       break;
183     case PROP_PRINT_SETTINGS:
184       g_value_set_object (value, op->priv->print_settings);
185       break;
186     case PROP_JOB_NAME:
187       g_value_set_string (value, op->priv->job_name);
188       break;
189     case PROP_NR_OF_PAGES:
190       g_value_set_int (value, op->priv->nr_of_pages);
191       break;
192     case PROP_CURRENT_PAGE:
193       g_value_set_int (value, op->priv->current_page);
194       break;      
195     case PROP_USE_FULL_PAGE:
196       g_value_set_boolean (value, op->priv->use_full_page);
197       break;
198     case PROP_UNIT:
199       g_value_set_enum (value, op->priv->unit);
200       break;
201     case PROP_SHOW_DIALOG:
202       g_value_set_boolean (value, op->priv->show_dialog);
203       break;
204     case PROP_PDF_TARGET:
205       g_value_set_string (value, op->priv->pdf_target);
206       break;
207     case PROP_STATUS:
208       g_value_set_enum (value, op->priv->status);
209       break;
210     case PROP_STATUS_STRING:
211       g_value_set_string (value, op->priv->status_string);
212       break;
213     default:
214       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
215       break;
216     }
217 }
218
219 static void
220 gtk_print_operation_class_init (GtkPrintOperationClass *class)
221 {
222   GObjectClass *gobject_class = (GObjectClass *)class;
223
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;
227   
228   g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));
229
230   /**
231    * GtkPrintOperation::begin-print:
232    * @operation: the #GtkPrintOperation on which the signal was emitted
233    * @context: the #GtkPrintContext for the current operation
234    *
235    * Gets emitted after the user has finished changing print settings
236    * in the dialog, before the actual rendering starts. 
237    *
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().
241    *
242    * Since: 2.10
243    */
244   signals[BEGIN_PRINT] =
245     g_signal_new ("begin_print",
246                   G_TYPE_FROM_CLASS (gobject_class),
247                   G_SIGNAL_RUN_LAST,
248                   G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
249                   NULL, NULL,
250                   g_cclosure_marshal_VOID__OBJECT,
251                   G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
252
253   /**
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 
259    * 
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.
263    *
264    * Since: 2.10
265    */
266   signals[REQUEST_PAGE_SETUP] =
267     g_signal_new ("request_page_setup",
268                   G_TYPE_FROM_CLASS (gobject_class),
269                   G_SIGNAL_RUN_LAST,
270                   G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
271                   NULL, NULL,
272                   _gtk_marshal_VOID__OBJECT_INT_OBJECT,
273                   G_TYPE_NONE, 3,
274                   GTK_TYPE_PRINT_CONTEXT,
275                   G_TYPE_INT,
276                   GTK_TYPE_PAGE_SETUP);
277
278   /**
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
283    *
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().
287    * 
288    * <informalexample><programlisting>
289    *  FIXME: need an example here
290    * </programlisting></informalexample>
291    *
292    * Since: 2.10
293    */
294   signals[DRAW_PAGE] =
295     g_signal_new ("draw_page",
296                   G_TYPE_FROM_CLASS (gobject_class),
297                   G_SIGNAL_RUN_LAST,
298                   G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
299                   NULL, NULL,
300                   _gtk_marshal_VOID__OBJECT_INT,
301                   G_TYPE_NONE, 2,
302                   GTK_TYPE_PRINT_CONTEXT,
303                   G_TYPE_INT);
304
305   /**
306    * GtkPrintOperation::end-print:
307    * @operation: the #GtkPrintOperation on which the signal was emitted
308    * @context: the #GtkPrintContext for the current operation
309    *
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.
313    * 
314    * Since: 2.10
315    */
316   signals[END_PRINT] =
317     g_signal_new ("end_print",
318                   G_TYPE_FROM_CLASS (gobject_class),
319                   G_SIGNAL_RUN_LAST,
320                   G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
321                   NULL, NULL,
322                   g_cclosure_marshal_VOID__OBJECT,
323                   G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);
324
325   /**
326    * GtkPrintOperation::status-changed:
327    * @operation: the #GtkPrintOperation on which the signal was emitted
328    *
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
332    * status.
333    *
334    * Since: 2.10
335    */
336   signals[STATUS_CHANGED] =
337    g_signal_new ("status-changed",
338                  G_TYPE_FROM_CLASS (class),
339                  G_SIGNAL_RUN_LAST,
340                  G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
341                  NULL, NULL,
342                  g_cclosure_marshal_VOID__VOID,
343                  G_TYPE_NONE, 0);
344
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"),
350                                                         GTK_TYPE_PAGE_SETUP,
351                                                         GTK_PARAM_READWRITE));
352
353   g_object_class_install_property (gobject_class,
354                                    PROP_PRINT_SETTINGS,
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));
360
361   g_object_class_install_property (gobject_class,
362                                    PROP_JOB_NAME,
363                                    g_param_spec_string ("job-name",
364                                                         P_("Job Name"),
365                                                         P_("A string used for identifying the print job."),
366                                                         "",
367                                                         GTK_PARAM_READWRITE));
368   
369   g_object_class_install_property (gobject_class,
370                                    PROP_NR_OF_PAGES,
371                                    g_param_spec_int ("number-of-pages",
372                                                      P_("Number of Pages"),
373                                                      P_("The number of pages in the document."),
374                                                      -1,
375                                                      G_MAXINT,
376                                                      -1,
377                                                      GTK_PARAM_READWRITE));
378   
379   g_object_class_install_property (gobject_class,
380                                    PROP_CURRENT_PAGE,
381                                    g_param_spec_int ("current-page",
382                                                      P_("Current Page"),
383                                                      P_("The current page in the document."),
384                                                      -1,
385                                                      G_MAXINT,
386                                                      -1,
387                                                      GTK_PARAM_READWRITE));
388   
389   g_object_class_install_property (gobject_class,
390                                    PROP_USE_FULL_PAGE,
391                                    g_param_spec_boolean ("use-full-page",
392                                                          P_("Use full page"),
393                                                          P_(""),
394                                                          FALSE,
395                                                          GTK_PARAM_READWRITE));
396   
397
398   g_object_class_install_property (gobject_class,
399                                    PROP_UNIT,
400                                    g_param_spec_enum ("unit",
401                                                       P_("Unit"),
402                                                       P_(""),
403                                                       GTK_TYPE_UNIT,
404                                                       GTK_UNIT_PIXEL,
405                                                       GTK_PARAM_READWRITE));
406   
407
408   g_object_class_install_property (gobject_class,
409                                    PROP_SHOW_DIALOG,
410                                    g_param_spec_boolean ("show-dialog",
411                                                          P_("Show Dialog"),
412                                                          P_("%TRUE if gtk_print_operation_run() should show the print dialog."),
413                                                          TRUE,
414                                                          GTK_PARAM_READWRITE));
415   
416   g_object_class_install_property (gobject_class,
417                                    PROP_JOB_NAME,
418                                    g_param_spec_string ("pdf-target",
419                                                         P_("PDF target filename"),
420                                                         P_(""),
421                                                         NULL,
422                                                         GTK_PARAM_READWRITE));
423   
424   g_object_class_install_property (gobject_class,
425                                    PROP_STATUS,
426                                    g_param_spec_enum ("status",
427                                                       P_("Status"),
428                                                       P_("The status of the print operation"),
429                                                       GTK_TYPE_PRINT_STATUS,
430                                                       GTK_PRINT_STATUS_INITIAL,
431                                                       GTK_PARAM_READABLE));
432   
433   g_object_class_install_property (gobject_class,
434                                    PROP_STATUS_STRING,
435                                    g_param_spec_string ("status-string",
436                                                         P_("Status String"),
437                                                         P_("A human-readable description of the status"),
438                                                         "",
439                                                         GTK_PARAM_READABLE));
440   
441
442 }
443
444 /**
445  * gtk_print_operation_new:
446  *
447  * Creates a new #GtkPrintOperation. 
448  *
449  * Returns: a new #GtkPrintOperation
450  *
451  * Since: 2.10
452  */
453 GtkPrintOperation *
454 gtk_print_operation_new (void)
455 {
456   GtkPrintOperation *print_operation;
457
458   print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
459   
460   return print_operation;
461 }
462
463 /**
464  * gtk_print_operation_set_default_page_setup:
465  * @op: a #GtkPrintOperation
466  * @default_page_setup: a #GtkPageSetup, or %NULL
467  * 
468  * Makes @default_page_setup the default page setup for @op.
469  *
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.
473  *
474  * Since: 2.10
475  **/
476 void
477 gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
478                                             GtkPageSetup      *default_page_setup)
479 {
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));
483
484   if (default_page_setup != op->priv->default_page_setup)
485     {
486       if (default_page_setup)
487         g_object_ref (default_page_setup);
488       
489       if (op->priv->default_page_setup)
490         g_object_unref (op->priv->default_page_setup);
491       
492       op->priv->default_page_setup = default_page_setup;
493      
494       g_object_notify (G_OBJECT (op), "default-page-setup");
495     }
496 }
497
498 /**
499  * gtk_print_operation_get_default_page_setup:
500  * @op: a #GtkPrintOperation
501  *
502  * Returns the default page setup, see 
503  * gtk_print_operation_set_default_page_setup().
504  *
505  * Returns: the default page setup 
506  *
507  * Since: 2.10
508  */
509 GtkPageSetup *
510 gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
511 {
512   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
513
514   return op->priv->default_page_setup;
515 }
516
517
518 /**
519  * gtk_print_operation_set_print_settings:
520  * @op: a #GtkPrintOperation
521  * @print_settings: #GtkPrintSettings, or %NULL
522  * 
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().
526  *
527  * Since: 2.10
528  **/
529 void
530 gtk_print_operation_set_print_settings (GtkPrintOperation *op,
531                                         GtkPrintSettings  *print_settings)
532 {
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));
536
537   if (print_settings != op->priv->print_settings)
538     {
539       if (print_settings)
540         g_object_ref (print_settings);
541
542       if (op->priv->print_settings)
543         g_object_unref (op->priv->print_settings);
544   
545       op->priv->print_settings = print_settings;
546
547       g_object_notify (G_OBJECT (op), "print-settings");
548     }
549 }
550
551 /**
552  * gtk_print_operation_get_print_settings:
553  * @op: a #GtkPrintOperation
554  * 
555  * Returns the current print settings. 
556  *
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.
560  * 
561  * Return value: the current print settings of @op.
562  * 
563  * Since: 2.10
564  **/
565 GtkPrintSettings *
566 gtk_print_operation_get_print_settings (GtkPrintOperation *op)
567 {
568   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);
569
570   return op->priv->print_settings;
571 }
572
573 /**
574  * gtk_print_operation_set_job_name:
575  * @op: a #GtkPrintOperation
576  * @job_name: a string that identifies the print job
577  * 
578  * Sets the name of the print job. The name is used to identify 
579  * the job (e.g. in monitoring applications like eggcups). 
580  * 
581  * If you don't set a job name, GTK+ picks a default one by 
582  * numbering successive print jobs.
583  *
584  * Since: 2.10
585  **/
586 void
587 gtk_print_operation_set_job_name (GtkPrintOperation *op,
588                                   const gchar       *job_name)
589 {
590   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
591   g_return_if_fail (g_utf8_validate (job_name, -1, NULL));
592
593   g_free (op->priv->job_name);
594   op->priv->job_name = g_strdup (job_name);
595
596   g_object_notify (G_OBJECT (op), "job-name");
597 }
598
599 /**
600  * gtk_print_operation_set_nr_of_pages:
601  * @op: a #GtkPrintOperation
602  * @n_pages: the number of pages
603  * 
604  * Sets the number of pages in the document. 
605  *
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.
609  *
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.
614  *
615  * Since: 2.10
616  **/
617 void
618 gtk_print_operation_set_nr_of_pages (GtkPrintOperation *op,
619                                      gint               n_pages)
620 {
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);
625
626   if (op->priv->nr_of_pages != n_pages)
627     {
628       op->priv->nr_of_pages = n_pages;
629
630       g_object_notify (G_OBJECT (op), "number-of-pages");
631     }
632 }
633
634 /**
635  * gtk_print_operation_set_current_page:
636  * @op: a #GtkPrintOperation
637  * @current_page: the current page, 0-based
638  *
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.
642  *
643  * Note that this only makes sense for pre-paginated documents.
644  * 
645  * Since: 2.10
646  **/
647 void
648 gtk_print_operation_set_current_page (GtkPrintOperation *op,
649                                       gint               current_page)
650 {
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);
655  
656   if (op->priv->current_page != current_page)
657     {
658       op->priv->current_page = current_page;
659
660       g_object_notify (G_OBJECT (op), "current-page");
661     }
662 }
663
664 void
665 gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
666                                        gboolean           full_page)
667 {
668   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
669
670   full_page = full_page != FALSE;
671   
672   if (op->priv->use_full_page != full_page)
673     {
674       op->priv->use_full_page = full_page;
675    
676       g_object_notify (G_OBJECT (op), "use-full-page");
677     }
678 }
679
680 void
681 gtk_print_operation_set_unit (GtkPrintOperation *op,
682                               GtkUnit            unit)
683 {
684   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
685
686   if (op->priv->unit != unit)
687     {
688       op->priv->unit = unit;
689
690       g_object_notify (G_OBJECT (op), "unit");
691     }
692 }
693
694 void
695 _gtk_print_operation_set_status (GtkPrintOperation *op,
696                                  GtkPrintStatus     status,
697                                  const gchar       *string)
698 {
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")
718   };
719
720   if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
721     status = GTK_PRINT_STATUS_FINISHED_ABORTED;
722
723   if (string == NULL)
724     string = g_strip_context (status_strs[status],
725                               gettext (status_strs[status]));
726   
727   if (op->priv->status == status &&
728       strcmp (string, op->priv->status_string) == 0)
729     return;
730   
731   g_free (op->priv->status_string);
732   op->priv->status_string = g_strdup (string);
733   op->priv->status = status;
734
735   g_object_notify (G_OBJECT (op), "status");
736   g_object_notify (G_OBJECT (op), "status-string");
737
738   g_signal_emit (op, signals[STATUS_CHANGED], 0);
739 }
740
741
742 /**
743  * gtk_print_operation_get_status:
744  * @op: a #GtkPrintOperation
745  * 
746  * Returns the status of the print operation. 
747  * Also see gtk_print_operation_get_status_string().
748  * 
749  * Return value: the status of the print operation
750  *
751  * Since: 2.10
752  **/
753 GtkPrintStatus
754 gtk_print_operation_get_status (GtkPrintOperation *op)
755 {
756   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
757                         GTK_PRINT_STATUS_FINISHED_ABORTED);
758
759   return op->priv->status;
760 }
761
762 /**
763  * gtk_print_operation_get_status_string:
764  * @op: a #GtkPrintOperation
765  * 
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.
769  *
770  * Use gtk_print_operation_get_status() to obtain a status
771  * value that is suitable for programmatic use. 
772  * 
773  * Return value: a string representation of the status
774  *    of the print operation
775  *
776  * Since: 2.10
777  **/
778 G_CONST_RETURN gchar *
779 gtk_print_operation_get_status_string (GtkPrintOperation *op)
780 {
781   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");
782
783   return op->priv->status_string;
784 }
785
786 /**
787  * gtk_print_operation_is_finished:
788  * @op: a #GtkPrintOperation
789  * 
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).
793  * 
794  * Return value: %TRUE, if the print operation is finished.
795  *
796  * Since: 2.10
797  **/
798 gboolean
799 gtk_print_operation_is_finished (GtkPrintOperation *op)
800 {
801   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);
802
803   return
804     op->priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
805     op->priv->status == GTK_PRINT_STATUS_FINISHED;
806 }
807
808
809 /**
810  * gtk_print_operation_set_show_dialog:
811  * @op: a #GtkPrintOperation
812  * @show_dialog: %TRUE to show the print dialog
813  * 
814  * Sets whether calling gtk_print_operation_run() will present
815  * a print dialog to the user, or just print to the default printer.
816  *
817  * Since: 2.10
818  */
819 void
820 gtk_print_operation_set_show_dialog (GtkPrintOperation *op,
821                                      gboolean           show_dialog)
822 {
823   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
824   
825   show_dialog = show_dialog != FALSE;
826
827   if (op->priv->show_dialog != show_dialog)
828     {
829       op->priv->show_dialog = show_dialog;
830
831       g_object_notify (G_OBJECT (op), "show-dialog");
832     }
833 }
834
835 void
836 gtk_print_operation_set_pdf_target (GtkPrintOperation *op,
837                                     const gchar *      filename)
838 {
839   g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
840
841   g_free (op->priv->pdf_target);
842   op->priv->pdf_target = g_strdup (filename);
843
844   g_object_notify (G_OBJECT (op), "pdf-target");
845 }
846
847 /* Creates the initial page setup used for printing unless the
848  * app overrides this on a per-page basis using request_page_setup.
849  *
850  * Data is taken from, in order, if existing:
851  *
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)
855  * default_page_setup
856  * per-locale default setup
857  */
858 static GtkPageSetup *
859 create_page_setup (GtkPrintOperation *op)
860 {
861   GtkPageSetup *page_setup;
862   GtkPrintSettings *settings;
863   
864   if (op->priv->default_page_setup)
865     page_setup = gtk_page_setup_copy (op->priv->default_page_setup);
866   else
867     page_setup = gtk_page_setup_new ();
868
869   settings = op->priv->print_settings;
870   if (settings)
871     {
872       GtkPaperSize *paper_size;
873       
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));
877
878
879       paper_size = gtk_print_settings_get_paper_size (settings);
880       if (paper_size)
881         {
882           gtk_page_setup_set_paper_size (page_setup, paper_size);
883           gtk_paper_size_free (paper_size);
884         }
885
886       /* TODO: Margins? */
887     }
888   
889   return page_setup;
890 }
891
892 static void 
893 pdf_start_page (GtkPrintOperation *op,
894                 GtkPrintContext   *print_context,
895                 GtkPageSetup      *page_setup)
896 {
897   /* TODO: Set up page size, not supported in cairo yet */
898 }
899
900 static void
901 pdf_end_page (GtkPrintOperation *op,
902               GtkPrintContext   *print_context)
903 {
904   cairo_t *cr;
905
906   cr = gtk_print_context_get_cairo (print_context);
907   cairo_show_page (cr);
908 }
909
910 static void
911 pdf_end_run (GtkPrintOperation *op)
912 {
913   cairo_surface_destroy (op->priv->surface);
914   op->priv->surface = NULL;
915 }
916
917 static GtkPrintOperationResult
918 run_pdf (GtkPrintOperation  *op,
919          GtkWindow          *parent,
920          gboolean           *do_print,
921          GError            **error)
922 {
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: */
927   
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);
932   
933   op->priv->surface = cairo_pdf_surface_create (op->priv->pdf_target,
934                                                 width, height);
935   /* TODO: DPI from settings object? */
936   cairo_pdf_surface_set_dpi (op->priv->surface, 300, 300);
937   
938   op->priv->dpi_x = 72;
939   op->priv->dpi_y = 72;
940
941   op->priv->manual_num_copies = 1;
942   op->priv->manual_collation = FALSE;
943   
944   *do_print = TRUE;
945   
946   op->priv->start_page = pdf_start_page;
947   op->priv->end_page = pdf_end_page;
948   op->priv->end_run = pdf_end_run;
949   
950   return GTK_PRINT_OPERATION_RESULT_APPLY; 
951 }
952
953 static GtkPrintOperationResult
954 run_print_dialog (GtkPrintOperation  *op,
955                   GtkWindow          *parent,
956                   gboolean           *do_print,
957                   GError            **error)
958 {
959   if (op->priv->pdf_target != NULL)
960     return run_pdf (op, parent, do_print, error);
961
962   /* This does:
963    * Open print dialog 
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
969    */
970   return _gtk_print_operation_platform_backend_run_dialog (op, 
971                                                            parent,
972                                                            do_print,
973                                                            error);
974 }
975
976 /**
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
981  * 
982  * Runs the print operation, by first letting the user modify
983  * print settings in the print dialog, and then print the
984  * document.
985  *
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.
990  * 
991  * <informalexample><programlisting>
992  *  FIXME: need an example here
993  * </programlisting></informalexample>
994  *
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.
1001  *
1002  * Since: 2.10
1003  **/
1004 GtkPrintOperationResult
1005 gtk_print_operation_run (GtkPrintOperation  *op,
1006                          GtkWindow          *parent,
1007                          GError            **error)
1008 {
1009   int page, range;
1010   GtkPageSetup *initial_page_setup, *page_setup;
1011   GtkPrintContext *print_context;
1012   cairo_t *cr;
1013   gboolean do_print;
1014   int uncollated_copies, collated_copies;
1015   int i, j;
1016   GtkPageRange *ranges;
1017   GtkPageRange one_range;
1018   int num_ranges;
1019   GtkPrintOperationResult result;
1020   
1021   g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
1022                         GTK_PRINT_OPERATION_RESULT_ERROR);
1023
1024   result = run_print_dialog (op, parent, &do_print, error);
1025   if (!do_print)
1026     {
1027       _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
1028       return result;
1029     }
1030   
1031   if (op->priv->manual_collation)
1032     {
1033       uncollated_copies = op->priv->manual_num_copies;
1034       collated_copies = 1;
1035     }
1036   else
1037     {
1038       uncollated_copies = 1;
1039       collated_copies = op->priv->manual_num_copies;
1040     }
1041
1042   print_context = _gtk_print_context_new (op);
1043
1044   initial_page_setup = create_page_setup (op);
1045   _gtk_print_context_set_page_setup (print_context, initial_page_setup);
1046
1047   _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
1048   g_signal_emit (op, signals[BEGIN_PRINT], 0, print_context);
1049   
1050   g_return_val_if_fail (op->priv->nr_of_pages > 0, GTK_PRINT_OPERATION_RESULT_ERROR);
1051
1052   if (op->priv->print_pages == GTK_PRINT_PAGES_RANGES)
1053     {
1054       ranges = op->priv->page_ranges;
1055       num_ranges = op->priv->num_page_ranges;
1056     }
1057   else if (op->priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
1058            op->priv->current_page != -1)
1059     {
1060       ranges = &one_range;
1061       num_ranges = 1;
1062       ranges[0].start = op->priv->current_page;
1063       ranges[0].end = op->priv->current_page;
1064     }
1065   else
1066     {
1067       ranges = &one_range;
1068       num_ranges = 1;
1069       ranges[0].start = 0;
1070       ranges[0].end = op->priv->nr_of_pages - 1;
1071     }
1072   
1073   _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_GENERATING_DATA, NULL);
1074
1075   for (i = 0; i < uncollated_copies; i++)
1076     {
1077       for (range = 0; range < num_ranges; range ++)
1078         {
1079           int start, end, inc;
1080           int low = ranges[range].start;
1081           int high = ranges[range].end;
1082           
1083           if (op->priv->manual_reverse)
1084             {
1085               start = high;
1086               end = low - 1;
1087               inc = -1;
1088             }
1089           else
1090             {
1091               start = low;
1092               end = high + 1;
1093               inc = 1;
1094             }
1095           for (page = start; page != end; page += inc)
1096             {
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))
1099                 continue;
1100               
1101               for (j = 0; j < collated_copies; j++)
1102                 {
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);
1105                   
1106                   _gtk_print_context_set_page_setup (print_context, page_setup);
1107                   op->priv->start_page (op, print_context, page_setup);
1108                   
1109                   cr = gtk_print_context_get_cairo (print_context);
1110
1111                   cairo_save (cr);
1112                   if (op->priv->manual_scale != 100.0)
1113                     cairo_scale (cr,
1114                                  op->priv->manual_scale,
1115                                  op->priv->manual_scale);
1116                   
1117                   if (op->priv->manual_orientation)
1118                     _gtk_print_context_rotate_according_to_orientation (print_context);
1119
1120                   if (!op->priv->use_full_page)
1121                     _gtk_print_context_translate_into_margin (print_context);
1122           
1123                   g_signal_emit (op, signals[DRAW_PAGE], 0, 
1124                                  print_context, page);
1125                   
1126                   op->priv->end_page (op, print_context);
1127                   
1128                   cairo_restore (cr);
1129                   
1130                   g_object_unref (page_setup);
1131
1132                   /* Iterate the mainloop so that we redraw windows */
1133                   while (gtk_events_pending ())
1134                     gtk_main_iteration ();
1135                 }
1136             }
1137         }
1138     }
1139   
1140   g_signal_emit (op, signals[END_PRINT], 0, print_context);
1141
1142   g_object_unref (print_context);
1143   g_object_unref (initial_page_setup);
1144
1145   cairo_surface_finish (op->priv->surface);
1146   op->priv->end_run (op);
1147
1148   return GTK_PRINT_OPERATION_RESULT_APPLY;
1149 }
1150
1151
1152 #define __GTK_PRINT_OPERATION_C__
1153 #include "gtkaliasdef.c"