]> Pileus Git - ~andy/gtk/blob - gtk/gtkpagesetupunixdialog.c
Move documentation to inline comments: GtkPageSetupUnixDialog
[~andy/gtk] / gtk / gtkpagesetupunixdialog.c
1 /* GtkPageSetupUnixDialog 
2  * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
3  * Copyright © 2006, 2007, 2008 Christian Persch
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
22 #include "config.h"
23 #include <string.h>
24 #include <locale.h>
25
26 #include "gtkintl.h"
27 #include "gtkprivate.h"
28
29 #include "gtkliststore.h"
30 #include "gtkstock.h"
31 #include "gtktreeviewcolumn.h"
32 #include "gtktreeselection.h"
33 #include "gtktreemodel.h"
34 #include "gtkbutton.h"
35 #include "gtkscrolledwindow.h"
36 #include "gtkvbox.h"
37 #include "gtkhbox.h"
38 #include "gtkframe.h"
39 #include "gtkeventbox.h"
40 #include "gtkcombobox.h"
41 #include "gtktogglebutton.h"
42 #include "gtkradiobutton.h"
43 #include "gtklabel.h"
44 #include "gtktable.h"
45 #include "gtkcelllayout.h"
46 #include "gtkcellrenderertext.h"
47 #include "gtkalignment.h"
48 #include "gtkspinbutton.h"
49 #include "gtkbbox.h"
50 #include "gtkhbbox.h"
51
52 #include "gtkpagesetupunixdialog.h"
53 #include "gtkcustompaperunixdialog.h"
54 #include "gtkprintbackend.h"
55 #include "gtkpapersize.h"
56 #include "gtkprintutils.h"
57 #include "gtkalias.h"
58
59
60 /**
61  * SECTION:gtkpagesetupunixdialog
62  * @Short_description: A page setup dialog
63  * @Title: GtkPageSetupUnixDialog
64  *
65  * #GtkPageSetupUnixDialog implements a page setup dialog for platforms
66  * which don't provide a native page setup dialog, like Unix. It can
67  * be used very much like any other GTK+ dialog, at the cost of
68  * the portability offered by the <link
69  * linkend="gtk-High-level-Printing-API">high-level printing API</link>
70  *
71  * Printing support was added in GTK+ 2.10.
72  */
73
74
75 struct GtkPageSetupUnixDialogPrivate
76 {
77   GtkListStore *printer_list;
78   GtkListStore *page_setup_list;
79   GtkListStore *custom_paper_list;
80   
81   GList *print_backends;
82
83   GtkWidget *printer_combo;
84   GtkWidget *paper_size_combo;
85   GtkWidget *paper_size_label;
86
87   GtkWidget *portrait_radio;
88   GtkWidget *reverse_portrait_radio;
89   GtkWidget *landscape_radio;
90   GtkWidget *reverse_landscape_radio;
91
92   guint request_details_tag;
93   GtkPrinter *request_details_printer;
94   
95   GtkPrintSettings *print_settings;
96
97   /* Save last setup so we can re-set it after selecting manage custom sizes */
98   GtkPageSetup *last_setup;
99
100   gchar *waiting_for_printer;
101 };
102
103 enum {
104   PRINTER_LIST_COL_NAME,
105   PRINTER_LIST_COL_PRINTER,
106   PRINTER_LIST_N_COLS
107 };
108
109 enum {
110   PAGE_SETUP_LIST_COL_PAGE_SETUP,
111   PAGE_SETUP_LIST_COL_IS_SEPARATOR,
112   PAGE_SETUP_LIST_N_COLS
113 };
114
115 G_DEFINE_TYPE (GtkPageSetupUnixDialog, gtk_page_setup_unix_dialog, GTK_TYPE_DIALOG)
116
117 #define GTK_PAGE_SETUP_UNIX_DIALOG_GET_PRIVATE(o)  \
118    (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PAGE_SETUP_UNIX_DIALOG, GtkPageSetupUnixDialogPrivate))
119
120 static void gtk_page_setup_unix_dialog_finalize  (GObject                *object);
121 static void populate_dialog                      (GtkPageSetupUnixDialog *dialog);
122 static void fill_paper_sizes_from_printer        (GtkPageSetupUnixDialog *dialog,
123                                                   GtkPrinter             *printer);
124 static void printer_added_cb                     (GtkPrintBackend        *backend,
125                                                   GtkPrinter             *printer,
126                                                   GtkPageSetupUnixDialog *dialog);
127 static void printer_removed_cb                   (GtkPrintBackend        *backend,
128                                                   GtkPrinter             *printer,
129                                                   GtkPageSetupUnixDialog *dialog);
130 static void printer_status_cb                    (GtkPrintBackend        *backend,
131                                                   GtkPrinter             *printer,
132                                                   GtkPageSetupUnixDialog *dialog);
133
134
135
136 static const gchar const common_paper_sizes[][16] = {
137   "na_letter",
138   "na_legal",
139   "iso_a4",
140   "iso_a5",
141   "roc_16k",
142   "iso_b5",
143   "jis_b5",
144   "na_number-10",
145   "iso_dl",
146   "jpn_chou3",
147   "na_ledger",
148   "iso_a3",
149 };
150
151
152 static void
153 gtk_page_setup_unix_dialog_class_init (GtkPageSetupUnixDialogClass *class)
154 {
155   GObjectClass *object_class;
156   GtkWidgetClass *widget_class;
157
158   object_class = (GObjectClass *) class;
159   widget_class = (GtkWidgetClass *) class;
160
161   object_class->finalize = gtk_page_setup_unix_dialog_finalize;
162
163   g_type_class_add_private (class, sizeof (GtkPageSetupUnixDialogPrivate));
164 }
165
166 static void
167 gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
168 {
169   GtkPageSetupUnixDialogPrivate *priv;
170   GtkTreeIter iter;
171   gchar *tmp;
172
173   priv = dialog->priv = GTK_PAGE_SETUP_UNIX_DIALOG_GET_PRIVATE (dialog);
174
175   priv->print_backends = NULL;
176
177   priv->printer_list = gtk_list_store_new (PRINTER_LIST_N_COLS,
178                                                    G_TYPE_STRING,
179                                                    G_TYPE_OBJECT);
180
181   gtk_list_store_append (priv->printer_list, &iter);
182   tmp = g_strdup_printf ("<b>%s</b>\n%s", _("Any Printer"), _("For portable documents"));
183   gtk_list_store_set (priv->printer_list, &iter,
184                       PRINTER_LIST_COL_NAME, tmp,
185                       PRINTER_LIST_COL_PRINTER, NULL,
186                       -1);
187   g_free (tmp);
188
189   priv->page_setup_list = gtk_list_store_new (PAGE_SETUP_LIST_N_COLS,
190                                                       G_TYPE_OBJECT,
191                                                       G_TYPE_BOOLEAN);
192
193   priv->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
194   _gtk_print_load_custom_papers (priv->custom_paper_list);
195
196   populate_dialog (dialog);
197   
198   gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
199                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
200                           GTK_STOCK_APPLY, GTK_RESPONSE_OK,
201                           NULL);
202   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
203                                            GTK_RESPONSE_OK,
204                                            GTK_RESPONSE_CANCEL,
205                                            -1);
206
207   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
208 }
209
210 static void
211 gtk_page_setup_unix_dialog_finalize (GObject *object)
212 {
213   GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (object);
214   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
215   GtkPrintBackend *backend;
216   GList *node;
217   
218   if (priv->request_details_tag)
219     {
220       g_signal_handler_disconnect (priv->request_details_printer,
221                                    priv->request_details_tag);
222       g_object_unref (priv->request_details_printer);
223       priv->request_details_printer = NULL;
224       priv->request_details_tag = 0;
225     }
226   
227   if (priv->printer_list)
228     {
229       g_object_unref (priv->printer_list);
230       priv->printer_list = NULL;
231     }
232
233   if (priv->page_setup_list)
234     {
235       g_object_unref (priv->page_setup_list);
236       priv->page_setup_list = NULL;
237     }
238
239   if (priv->custom_paper_list)
240     {
241       g_object_unref (priv->custom_paper_list);
242       priv->custom_paper_list = NULL;
243     }
244
245   if (priv->print_settings)
246     {
247       g_object_unref (priv->print_settings);
248       priv->print_settings = NULL;
249     }
250
251   g_free (priv->waiting_for_printer);
252   priv->waiting_for_printer = NULL;
253
254   for (node = priv->print_backends; node != NULL; node = node->next)
255     {
256       backend = GTK_PRINT_BACKEND (node->data);
257
258       g_signal_handlers_disconnect_by_func (backend, printer_added_cb, dialog);
259       g_signal_handlers_disconnect_by_func (backend, printer_removed_cb, dialog);
260       g_signal_handlers_disconnect_by_func (backend, printer_status_cb, dialog);
261
262       gtk_print_backend_destroy (backend);
263       g_object_unref (backend);
264     }
265   
266   g_list_free (priv->print_backends);
267   priv->print_backends = NULL;
268
269   G_OBJECT_CLASS (gtk_page_setup_unix_dialog_parent_class)->finalize (object);
270 }
271
272 static void
273 printer_added_cb (GtkPrintBackend        *backend, 
274                   GtkPrinter             *printer, 
275                   GtkPageSetupUnixDialog *dialog)
276 {
277   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
278   GtkTreeIter iter;
279   gchar *str;
280   const gchar *location;
281
282   if (gtk_printer_is_virtual (printer))
283     return;
284
285   location = gtk_printer_get_location (printer);
286   if (location == NULL)
287     location = "";
288   str = g_strdup_printf ("<b>%s</b>\n%s",
289                          gtk_printer_get_name (printer),
290                          location);
291
292   gtk_list_store_append (priv->printer_list, &iter);
293   gtk_list_store_set (priv->printer_list, &iter,
294                       PRINTER_LIST_COL_NAME, str,
295                       PRINTER_LIST_COL_PRINTER, printer,
296                       -1);
297
298   g_object_set_data_full (G_OBJECT (printer),
299                           "gtk-print-tree-iter",
300                           gtk_tree_iter_copy (&iter),
301                           (GDestroyNotify) gtk_tree_iter_free);
302   g_free (str);
303
304   if (priv->waiting_for_printer != NULL &&
305       strcmp (priv->waiting_for_printer,
306               gtk_printer_get_name (printer)) == 0)
307     {
308       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
309                                      &iter);
310       priv->waiting_for_printer = NULL;
311     }
312 }
313
314 static void
315 printer_removed_cb (GtkPrintBackend        *backend, 
316                     GtkPrinter             *printer, 
317                     GtkPageSetupUnixDialog *dialog)
318 {
319   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
320   GtkTreeIter *iter;
321
322   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
323   gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
324 }
325
326
327 static void
328 printer_status_cb (GtkPrintBackend        *backend, 
329                    GtkPrinter             *printer, 
330                    GtkPageSetupUnixDialog *dialog)
331 {
332   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
333   GtkTreeIter *iter;
334   gchar *str;
335   const gchar *location;
336   
337   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
338
339   location = gtk_printer_get_location (printer);
340   if (location == NULL)
341     location = "";
342   str = g_strdup_printf ("<b>%s</b>\n%s",
343                          gtk_printer_get_name (printer),
344                          location);
345   gtk_list_store_set (priv->printer_list, iter,
346                       PRINTER_LIST_COL_NAME, str,
347                       -1);
348   g_free (str);
349 }
350
351 static void
352 printer_list_initialize (GtkPageSetupUnixDialog *dialog,
353                          GtkPrintBackend        *print_backend)
354 {
355   GList *list, *node;
356   
357   g_return_if_fail (print_backend != NULL);
358
359   g_signal_connect_object (print_backend, 
360                            "printer-added", 
361                            (GCallback) printer_added_cb, 
362                            G_OBJECT (dialog), 0);
363
364   g_signal_connect_object (print_backend, 
365                            "printer-removed", 
366                            (GCallback) printer_removed_cb, 
367                            G_OBJECT (dialog), 0);
368
369   g_signal_connect_object (print_backend, 
370                            "printer-status-changed", 
371                            (GCallback) printer_status_cb, 
372                            G_OBJECT (dialog), 0);
373
374   list = gtk_print_backend_get_printer_list (print_backend);
375
376   node = list;
377   while (node != NULL)
378     {
379       printer_added_cb (print_backend, node->data, dialog);
380       node = node->next;
381     }
382
383   g_list_free (list);
384   
385 }
386
387 static void
388 load_print_backends (GtkPageSetupUnixDialog *dialog)
389 {
390   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
391   GList *node;
392
393   if (g_module_supported ())
394     priv->print_backends = gtk_print_backend_load_modules ();
395
396   for (node = priv->print_backends; node != NULL; node = node->next)
397     printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
398 }
399
400 static gboolean
401 paper_size_row_is_separator (GtkTreeModel *model,
402                              GtkTreeIter  *iter,
403                              gpointer      data)
404 {
405   gboolean separator;
406
407   gtk_tree_model_get (model, iter, PAGE_SETUP_LIST_COL_IS_SEPARATOR, &separator, -1);
408   return separator;
409 }
410
411 static GtkPageSetup *
412 get_current_page_setup (GtkPageSetupUnixDialog *dialog)
413 {
414   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
415   GtkPageSetup *current_page_setup;
416   GtkComboBox *combo_box;
417   GtkTreeIter iter;
418
419   current_page_setup = NULL;
420   
421   combo_box = GTK_COMBO_BOX (priv->paper_size_combo);
422   if (gtk_combo_box_get_active_iter (combo_box, &iter))
423     gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
424                         PAGE_SETUP_LIST_COL_PAGE_SETUP, &current_page_setup, -1);
425
426   if (current_page_setup)
427     return current_page_setup;
428
429   /* No selected page size, return the default one.
430    * This is used to set the first page setup when the dialog is created
431    * as there is no selection on the first printer_changed.
432    */ 
433   return gtk_page_setup_new ();
434 }
435
436 static gboolean
437 page_setup_is_equal (GtkPageSetup *a, 
438                      GtkPageSetup *b)
439 {
440   return
441     gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
442                              gtk_page_setup_get_paper_size (b)) &&
443     gtk_page_setup_get_top_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_top_margin (b, GTK_UNIT_MM) &&
444     gtk_page_setup_get_bottom_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_bottom_margin (b, GTK_UNIT_MM) &&
445     gtk_page_setup_get_left_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_left_margin (b, GTK_UNIT_MM) &&
446     gtk_page_setup_get_right_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_right_margin (b, GTK_UNIT_MM);
447 }
448
449 static gboolean
450 page_setup_is_same_size (GtkPageSetup *a,
451                          GtkPageSetup *b)
452 {
453   return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
454                                   gtk_page_setup_get_paper_size (b));
455 }
456
457 static gboolean
458 set_paper_size (GtkPageSetupUnixDialog *dialog,
459                 GtkPageSetup           *page_setup,
460                 gboolean                size_only,
461                 gboolean                add_item)
462 {
463   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
464   GtkTreeModel *model;
465   GtkTreeIter iter;
466   GtkPageSetup *list_page_setup;
467
468   model = GTK_TREE_MODEL (priv->page_setup_list);
469
470   if (gtk_tree_model_get_iter_first (model, &iter))
471     {
472       do
473         {
474           gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
475                               PAGE_SETUP_LIST_COL_PAGE_SETUP, &list_page_setup, -1);
476           if (list_page_setup == NULL)
477             continue;
478           
479           if ((size_only && page_setup_is_same_size (page_setup, list_page_setup)) ||
480               (!size_only && page_setup_is_equal (page_setup, list_page_setup)))
481             {
482               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
483                                              &iter);
484               g_object_unref (list_page_setup);
485               return TRUE;
486             }
487               
488           g_object_unref (list_page_setup);
489           
490         } while (gtk_tree_model_iter_next (model, &iter));
491     }
492
493   if (add_item)
494     {
495       gtk_list_store_append (priv->page_setup_list, &iter);
496       gtk_list_store_set (priv->page_setup_list, &iter,
497                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
498                           -1);
499       gtk_list_store_append (priv->page_setup_list, &iter);
500       gtk_list_store_set (priv->page_setup_list, &iter,
501                           PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
502                           -1);
503       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
504                                      &iter);
505       return TRUE;
506     }
507
508   return FALSE;
509 }
510
511 static void
512 fill_custom_paper_sizes (GtkPageSetupUnixDialog *dialog)
513 {
514   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
515   GtkTreeIter iter, paper_iter;
516   GtkTreeModel *model;
517
518   model = GTK_TREE_MODEL (priv->custom_paper_list);
519   if (gtk_tree_model_get_iter_first (model, &iter))
520     {
521       gtk_list_store_append (priv->page_setup_list, &paper_iter);
522       gtk_list_store_set (priv->page_setup_list, &paper_iter,
523                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
524                           -1);
525       do
526         {
527           GtkPageSetup *page_setup;
528           gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
529
530           gtk_list_store_append (priv->page_setup_list, &paper_iter);
531           gtk_list_store_set (priv->page_setup_list, &paper_iter,
532                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
533                               -1);
534
535           g_object_unref (page_setup);
536         } while (gtk_tree_model_iter_next (model, &iter));
537     }
538   
539   gtk_list_store_append (priv->page_setup_list, &paper_iter);
540   gtk_list_store_set (priv->page_setup_list, &paper_iter,
541                       PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
542                       -1);
543   gtk_list_store_append (priv->page_setup_list, &paper_iter);
544   gtk_list_store_set (priv->page_setup_list, &paper_iter,
545                       PAGE_SETUP_LIST_COL_PAGE_SETUP, NULL,
546                       -1);
547 }
548
549 static void
550 fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
551                                GtkPrinter             *printer)
552 {
553   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
554   GList *list, *l;
555   GtkPageSetup *current_page_setup, *page_setup;
556   GtkPaperSize *paper_size;
557   GtkTreeIter iter;
558   gint i;
559
560   gtk_list_store_clear (priv->page_setup_list);
561
562   if (printer == NULL)
563     {
564       for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
565         {
566           page_setup = gtk_page_setup_new ();
567           paper_size = gtk_paper_size_new (common_paper_sizes[i]);
568           gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
569           gtk_paper_size_free (paper_size);
570           
571           gtk_list_store_append (priv->page_setup_list, &iter);
572           gtk_list_store_set (priv->page_setup_list, &iter,
573                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
574                               -1);
575           g_object_unref (page_setup);
576         }
577     }
578   else
579     {
580       list = gtk_printer_list_papers (printer);
581       /* TODO: We should really sort this list so interesting size
582          are at the top */
583       for (l = list; l != NULL; l = l->next)
584         {
585           page_setup = l->data;
586           gtk_list_store_append (priv->page_setup_list, &iter);
587           gtk_list_store_set (priv->page_setup_list, &iter,
588                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
589                               -1);
590           g_object_unref (page_setup);
591         }
592       g_list_free (list);
593     }
594
595   fill_custom_paper_sizes (dialog);
596   
597   current_page_setup = NULL;
598
599   /* When selecting a different printer, select its default paper size */
600   if (printer != NULL)
601     current_page_setup = gtk_printer_get_default_page_size (printer);
602
603   if (current_page_setup == NULL)
604     current_page_setup = get_current_page_setup (dialog);
605
606   if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
607     set_paper_size (dialog, current_page_setup, TRUE, TRUE);
608   
609   if (current_page_setup)
610     g_object_unref (current_page_setup);
611 }
612
613 static void
614 printer_changed_finished_callback (GtkPrinter             *printer,
615                                    gboolean                success,
616                                    GtkPageSetupUnixDialog *dialog)
617 {
618   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
619
620   g_signal_handler_disconnect (priv->request_details_printer,
621                                priv->request_details_tag);
622   g_object_unref (priv->request_details_printer);
623   priv->request_details_tag = 0;
624   priv->request_details_printer = NULL;
625   
626   if (success)
627     fill_paper_sizes_from_printer (dialog, printer);
628
629 }
630
631 static void
632 printer_changed_callback (GtkComboBox            *combo_box,
633                           GtkPageSetupUnixDialog *dialog)
634 {
635   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
636   GtkPrinter *printer;
637   GtkTreeIter iter;
638
639   /* If we're waiting for a specific printer but the user changed
640    * to another printer, cancel that wait. 
641    */
642   if (priv->waiting_for_printer)
643     {
644       g_free (priv->waiting_for_printer);
645       priv->waiting_for_printer = NULL;
646     }
647   
648   if (priv->request_details_tag)
649     {
650       g_signal_handler_disconnect (priv->request_details_printer,
651                                    priv->request_details_tag);
652       g_object_unref (priv->request_details_printer);
653       priv->request_details_printer = NULL;
654       priv->request_details_tag = 0;
655     }
656   
657   if (gtk_combo_box_get_active_iter (combo_box, &iter))
658     {
659       gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
660                           PRINTER_LIST_COL_PRINTER, &printer, -1);
661
662       if (printer == NULL || gtk_printer_has_details (printer))
663         fill_paper_sizes_from_printer (dialog, printer);
664       else
665         {
666           priv->request_details_printer = g_object_ref (printer);
667           priv->request_details_tag =
668             g_signal_connect (printer, "details-acquired",
669                               G_CALLBACK (printer_changed_finished_callback), dialog);
670           gtk_printer_request_details (printer);
671
672         }
673
674       if (printer)
675         g_object_unref (printer);
676
677       if (priv->print_settings)
678         {
679           const char *name = NULL;
680
681           if (printer)
682             name = gtk_printer_get_name (printer);
683           
684           gtk_print_settings_set (priv->print_settings,
685                                   "format-for-printer", name);
686         }
687     }
688 }
689
690 /* We do this munging because we don't want to show zero digits
691    after the decimal point, and not to many such digits if they
692    are nonzero. I wish printf let you specify max precision for %f... */
693 static gchar *
694 double_to_string (gdouble d, 
695                   GtkUnit unit)
696 {
697   gchar *val, *p;
698   struct lconv *locale_data;
699   const gchar *decimal_point;
700   gint decimal_point_len;
701
702   locale_data = localeconv ();
703   decimal_point = locale_data->decimal_point;
704   decimal_point_len = strlen (decimal_point);
705   
706   /* Max two decimal digits for inch, max one for mm */
707   if (unit == GTK_UNIT_INCH)
708     val = g_strdup_printf ("%.2f", d);
709   else
710     val = g_strdup_printf ("%.1f", d);
711
712   if (strstr (val, decimal_point))
713     {
714       p = val + strlen (val) - 1;
715       while (*p == '0')
716         p--;
717       if (p - val + 1 >= decimal_point_len &&
718           strncmp (p - (decimal_point_len - 1), decimal_point, decimal_point_len) == 0)
719         p -= decimal_point_len;
720       p[1] = '\0';
721     }
722
723   return val;
724 }
725
726
727 static void
728 custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
729                                  gint       response_id,
730                                  gpointer   user_data)
731 {
732   GtkPageSetupUnixDialog *page_setup_dialog = GTK_PAGE_SETUP_UNIX_DIALOG (user_data);
733   GtkPageSetupUnixDialogPrivate *priv = page_setup_dialog->priv;
734
735   _gtk_print_load_custom_papers (priv->custom_paper_list);
736
737   /* Update printer page list */
738   printer_changed_callback (GTK_COMBO_BOX (priv->printer_combo), page_setup_dialog);
739
740   gtk_widget_destroy (GTK_WIDGET (custom_paper_dialog));
741 }
742
743 static void
744 paper_size_changed (GtkComboBox            *combo_box,
745                     GtkPageSetupUnixDialog *dialog)
746 {
747   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
748   GtkTreeIter iter;
749   GtkPageSetup *page_setup, *last_page_setup;
750   GtkUnit unit;
751   gchar *str, *w, *h;
752   gchar *top, *bottom, *left, *right;
753   GtkLabel *label;
754   const gchar *unit_str;
755
756   label = GTK_LABEL (priv->paper_size_label);
757    
758   if (gtk_combo_box_get_active_iter (combo_box, &iter))
759     {
760       gtk_tree_model_get (gtk_combo_box_get_model (combo_box),
761                           &iter, PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
762
763       if (page_setup == NULL)
764         {
765           GtkWidget *custom_paper_dialog;
766
767           /* Change from "manage" menu item to last value */
768           if (priv->last_setup)
769             last_page_setup = g_object_ref (priv->last_setup);
770           else
771             last_page_setup = gtk_page_setup_new (); /* "good" default */
772           set_paper_size (dialog, last_page_setup, FALSE, TRUE);
773           g_object_unref (last_page_setup);
774
775           /* And show the custom paper dialog */
776           custom_paper_dialog = _gtk_custom_paper_unix_dialog_new (GTK_WINDOW (dialog), NULL);
777           g_signal_connect (custom_paper_dialog, "response", G_CALLBACK (custom_paper_dialog_response_cb), dialog);
778           gtk_window_present (GTK_WINDOW (custom_paper_dialog));
779
780           return;
781         }
782
783       if (priv->last_setup)
784         g_object_unref (priv->last_setup);
785
786       priv->last_setup = g_object_ref (page_setup);
787       
788       unit = _gtk_print_get_default_user_units ();
789
790       if (unit == GTK_UNIT_MM)
791         unit_str = _("mm");
792       else
793         unit_str = _("inch");
794         
795
796       w = double_to_string (gtk_page_setup_get_paper_width (page_setup, unit),
797                             unit);
798       h = double_to_string (gtk_page_setup_get_paper_height (page_setup, unit),
799                             unit);
800       str = g_strdup_printf ("%s x %s %s", w, h, unit_str);
801       g_free (w);
802       g_free (h);
803       
804       gtk_label_set_text (label, str);
805       g_free (str);
806
807       top = double_to_string (gtk_page_setup_get_top_margin (page_setup, unit), unit);
808       bottom = double_to_string (gtk_page_setup_get_bottom_margin (page_setup, unit), unit);
809       left = double_to_string (gtk_page_setup_get_left_margin (page_setup, unit), unit);
810       right = double_to_string (gtk_page_setup_get_right_margin (page_setup, unit), unit);
811
812       str = g_strdup_printf (_("Margins:\n"
813                                " Left: %s %s\n"
814                                " Right: %s %s\n"
815                                " Top: %s %s\n"
816                                " Bottom: %s %s"
817                                ),
818                              left, unit_str,
819                              right, unit_str,
820                              top, unit_str,
821                              bottom, unit_str);
822       g_free (top);
823       g_free (bottom);
824       g_free (left);
825       g_free (right);
826       
827       gtk_widget_set_tooltip_text (priv->paper_size_label, str);
828       g_free (str);
829       
830       g_object_unref (page_setup);
831     }
832   else
833     {
834       gtk_label_set_text (label, "");
835       gtk_widget_set_tooltip_text (priv->paper_size_label, NULL);
836       if (priv->last_setup)
837         g_object_unref (priv->last_setup);
838       priv->last_setup = NULL;
839     }
840 }
841
842 static void
843 page_name_func (GtkCellLayout   *cell_layout,
844                 GtkCellRenderer *cell,
845                 GtkTreeModel    *tree_model,
846                 GtkTreeIter     *iter,
847                 gpointer         data)
848 {
849   GtkPageSetup *page_setup;
850   GtkPaperSize *paper_size;
851   
852   gtk_tree_model_get (tree_model, iter,
853                       PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
854   if (page_setup)
855     {
856       paper_size = gtk_page_setup_get_paper_size (page_setup);
857       g_object_set (cell, "text",  gtk_paper_size_get_display_name (paper_size), NULL);
858       g_object_unref (page_setup);
859     }
860   else
861     g_object_set (cell, "text",  _("Manage Custom Sizes..."), NULL);
862       
863 }
864
865 static GtkWidget *
866 create_radio_button (GSList      *group,
867                      const gchar *stock_id)
868 {
869   GtkWidget *radio_button, *image, *label, *hbox;
870   GtkStockItem item;
871
872   radio_button = gtk_radio_button_new (group);
873   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_LARGE_TOOLBAR);
874   gtk_stock_lookup (stock_id, &item);
875   label = gtk_label_new (item.label);
876   hbox = gtk_hbox_new (0, 6);
877   gtk_container_add (GTK_CONTAINER (radio_button), hbox);
878   gtk_container_add (GTK_CONTAINER (hbox), image);
879   gtk_container_add (GTK_CONTAINER (hbox), label);
880
881   gtk_widget_show_all (radio_button);
882
883   return radio_button;
884 }
885
886 static void
887 populate_dialog (GtkPageSetupUnixDialog *ps_dialog)
888 {
889   GtkPageSetupUnixDialogPrivate *priv = ps_dialog->priv;
890   GtkDialog *dialog = GTK_DIALOG (ps_dialog);
891   GtkWidget *table, *label, *combo, *radio_button;
892   GtkCellRenderer *cell;
893
894   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
895
896   gtk_dialog_set_has_separator (dialog, FALSE);
897   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
898   gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
899   gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
900   gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);
901
902   table = gtk_table_new (5, 4, FALSE);
903   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
904   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
905   gtk_container_set_border_width (GTK_CONTAINER (table), 5);
906   gtk_box_pack_start (GTK_BOX (dialog->vbox), table, TRUE, TRUE, 0);
907   gtk_widget_show (table);
908
909   label = gtk_label_new_with_mnemonic (_("_Format for:"));
910   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
911   gtk_table_attach (GTK_TABLE (table), label,
912                     0, 1, 0, 1,
913                     GTK_FILL, 0, 0, 0);
914   gtk_widget_show (label);
915
916   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->printer_list));
917   priv->printer_combo = combo;
918
919   cell = gtk_cell_renderer_text_new ();
920   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
921   gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
922                                   "markup", PRINTER_LIST_COL_NAME,
923                                   NULL);
924
925   gtk_table_attach (GTK_TABLE (table), combo,
926                     1, 4, 0, 1,
927                     GTK_FILL | GTK_EXPAND, 0, 0, 0);
928   gtk_widget_show (combo);
929   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
930
931   label = gtk_label_new_with_mnemonic (_("_Paper size:"));
932   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
933   gtk_table_attach (GTK_TABLE (table), label,
934                     0, 1, 1, 2,
935                     GTK_FILL, 0, 0, 0);
936   gtk_widget_show (label);
937
938   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->page_setup_list));
939   priv->paper_size_combo = combo;
940   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo), 
941                                         paper_size_row_is_separator, NULL, NULL);
942   
943   cell = gtk_cell_renderer_text_new ();
944   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
945   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), cell,
946                                       page_name_func, NULL, NULL);
947
948   gtk_table_attach (GTK_TABLE (table), combo,
949                     1, 4, 1, 2,
950                     GTK_FILL | GTK_EXPAND, 0, 0, 0);
951   gtk_widget_show (combo);
952   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
953
954   label = gtk_label_new (NULL);
955   priv->paper_size_label = label;
956   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
957   gtk_table_attach (GTK_TABLE (table), label,
958                     1, 4, 2, 3,
959                     GTK_FILL, 0, 0, 0);
960   gtk_widget_show (label);
961
962   label = gtk_label_new_with_mnemonic (_("_Orientation:"));
963   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
964   gtk_table_attach (GTK_TABLE (table), label,
965                     0, 1, 3, 4,
966                     GTK_FILL, 0, 0, 0);
967   gtk_widget_show (label);
968
969   radio_button = create_radio_button (NULL, GTK_STOCK_ORIENTATION_PORTRAIT);
970   priv->portrait_radio = radio_button;
971   gtk_table_attach (GTK_TABLE (table), radio_button,
972                     1, 2, 3, 4,
973                     GTK_EXPAND|GTK_FILL, 0, 0, 0);
974   gtk_label_set_mnemonic_widget (GTK_LABEL (label), radio_button);
975
976   radio_button = create_radio_button (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button)),
977                                       GTK_STOCK_ORIENTATION_REVERSE_PORTRAIT);
978   priv->reverse_portrait_radio = radio_button;
979   gtk_table_attach (GTK_TABLE (table), radio_button,
980                     2, 3, 3, 4,
981                     GTK_EXPAND|GTK_FILL, 0, 0, 0);
982
983   radio_button = create_radio_button (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button)),
984                                       GTK_STOCK_ORIENTATION_LANDSCAPE);
985   priv->landscape_radio = radio_button;
986   gtk_table_attach (GTK_TABLE (table), radio_button,
987                     1, 2, 4, 5,
988                     GTK_EXPAND|GTK_FILL, 0, 0, 0);
989   gtk_widget_show (radio_button);
990
991   gtk_table_set_row_spacing (GTK_TABLE (table), 3, 0);
992   
993   radio_button = create_radio_button (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio_button)),
994                                       GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE);
995   priv->reverse_landscape_radio = radio_button;
996   gtk_table_attach (GTK_TABLE (table), radio_button,
997                     2, 3, 4, 5,
998                     GTK_EXPAND|GTK_FILL, 0, 0, 0);
999
1000
1001   g_signal_connect (priv->paper_size_combo, "changed", G_CALLBACK (paper_size_changed), ps_dialog);
1002   g_signal_connect (priv->printer_combo, "changed", G_CALLBACK (printer_changed_callback), ps_dialog);
1003   gtk_combo_box_set_active (GTK_COMBO_BOX (priv->printer_combo), 0);
1004
1005   load_print_backends (ps_dialog);
1006 }
1007
1008 /**
1009  * gtk_page_setup_unix_dialog_new:
1010  * @title: (allow-none): the title of the dialog, or %NULL
1011  * @parent: (allow-none): transient parent of the dialog, or %NULL
1012  *
1013  * Creates a new page setup dialog.
1014  *
1015  * Returns: the new #GtkPageSetupUnixDialog
1016  *
1017  * Since: 2.10
1018  */
1019 GtkWidget *
1020 gtk_page_setup_unix_dialog_new (const gchar *title,
1021                                 GtkWindow   *parent)
1022 {
1023   GtkWidget *result;
1024
1025   if (title == NULL)
1026     title = _("Page Setup");
1027   
1028   result = g_object_new (GTK_TYPE_PAGE_SETUP_UNIX_DIALOG,
1029                          "title", title,
1030                          NULL);
1031
1032   if (parent)
1033     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
1034
1035   return result;
1036 }
1037
1038 static GtkPageOrientation
1039 get_orientation (GtkPageSetupUnixDialog *dialog)
1040 {
1041   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1042
1043   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->portrait_radio)))
1044     return GTK_PAGE_ORIENTATION_PORTRAIT;
1045   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->landscape_radio)))
1046     return GTK_PAGE_ORIENTATION_LANDSCAPE;
1047   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio)))
1048     return GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE;
1049   return GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT;
1050 }
1051
1052 static void
1053 set_orientation (GtkPageSetupUnixDialog *dialog, 
1054                  GtkPageOrientation      orientation)
1055 {
1056   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1057
1058   switch (orientation)
1059     {
1060     case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
1061       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->reverse_portrait_radio), TRUE);
1062       break;
1063     case GTK_PAGE_ORIENTATION_PORTRAIT:
1064       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->portrait_radio), TRUE);
1065       break;
1066     case GTK_PAGE_ORIENTATION_LANDSCAPE:
1067       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->landscape_radio), TRUE);
1068       break;
1069     case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
1070       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio), TRUE);
1071       break;
1072     }
1073 }
1074
1075 /**
1076  * gtk_page_setup_unix_dialog_set_page_setup:
1077  * @dialog: a #GtkPageSetupUnixDialog
1078  * @page_setup: a #GtkPageSetup
1079  * 
1080  * Sets the #GtkPageSetup from which the page setup
1081  * dialog takes its values.
1082  *
1083  * Since: 2.10
1084  **/
1085 void
1086 gtk_page_setup_unix_dialog_set_page_setup (GtkPageSetupUnixDialog *dialog,
1087                                            GtkPageSetup           *page_setup)
1088 {
1089   if (page_setup)
1090     {
1091       set_paper_size (dialog, page_setup, FALSE, TRUE);
1092       set_orientation (dialog, gtk_page_setup_get_orientation (page_setup));
1093     }
1094 }
1095
1096 /**
1097  * gtk_page_setup_unix_dialog_get_page_setup:
1098  * @dialog: a #GtkPageSetupUnixDialog
1099  * 
1100  * Gets the currently selected page setup from the dialog. 
1101  * 
1102  * Returns: the current page setup 
1103  *
1104  * Since: 2.10
1105  **/
1106 GtkPageSetup *
1107 gtk_page_setup_unix_dialog_get_page_setup (GtkPageSetupUnixDialog *dialog)
1108 {
1109   GtkPageSetup *page_setup;
1110   
1111   page_setup = get_current_page_setup (dialog);
1112
1113   gtk_page_setup_set_orientation (page_setup, get_orientation (dialog));
1114
1115   return page_setup;
1116 }
1117
1118 static gboolean
1119 set_active_printer (GtkPageSetupUnixDialog *dialog,
1120                     const gchar            *printer_name)
1121 {
1122   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1123   GtkTreeModel *model;
1124   GtkTreeIter iter;
1125   GtkPrinter *printer;
1126
1127   model = GTK_TREE_MODEL (priv->printer_list);
1128
1129   if (gtk_tree_model_get_iter_first (model, &iter))
1130     {
1131       do
1132         {
1133           gtk_tree_model_get (GTK_TREE_MODEL (priv->printer_list), &iter,
1134                               PRINTER_LIST_COL_PRINTER, &printer, -1);
1135           if (printer == NULL)
1136             continue;
1137           
1138           if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
1139             {
1140               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
1141                                              &iter);
1142               g_object_unref (printer);
1143               return TRUE;
1144             }
1145               
1146           g_object_unref (printer);
1147           
1148         } while (gtk_tree_model_iter_next (model, &iter));
1149     }
1150   
1151   return FALSE;
1152 }
1153
1154 /**
1155  * gtk_page_setup_unix_dialog_set_print_settings:
1156  * @dialog: a #GtkPageSetupUnixDialog
1157  * @print_settings: a #GtkPrintSettings
1158  * 
1159  * Sets the #GtkPrintSettings from which the page setup dialog
1160  * takes its values.
1161  * 
1162  * Since: 2.10
1163  **/
1164 void
1165 gtk_page_setup_unix_dialog_set_print_settings (GtkPageSetupUnixDialog *dialog,
1166                                                GtkPrintSettings       *print_settings)
1167 {
1168   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1169   const gchar *format_for_printer;
1170
1171   if (priv->print_settings == print_settings) return;
1172
1173   if (priv->print_settings)
1174     g_object_unref (priv->print_settings);
1175
1176   priv->print_settings = print_settings;
1177
1178   if (print_settings)
1179     {
1180       g_object_ref (print_settings);
1181
1182       format_for_printer = gtk_print_settings_get (print_settings, "format-for-printer");
1183
1184       /* Set printer if in list, otherwise set when 
1185        * that printer is added 
1186        */
1187       if (format_for_printer &&
1188           !set_active_printer (dialog, format_for_printer))
1189         priv->waiting_for_printer = g_strdup (format_for_printer); 
1190     }
1191 }
1192
1193 /**
1194  * gtk_page_setup_unix_dialog_get_print_settings:
1195  * @dialog: a #GtkPageSetupUnixDialog
1196  * 
1197  * Gets the current print settings from the dialog.
1198  * 
1199  * Returns: the current print settings
1200  *
1201  * Since: 2.10
1202  **/
1203 GtkPrintSettings *
1204 gtk_page_setup_unix_dialog_get_print_settings (GtkPageSetupUnixDialog *dialog)
1205 {
1206   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1207
1208   return priv->print_settings;
1209 }
1210
1211 #define __GTK_PAGE_SETUP_UNIX_DIALOG_C__
1212 #include "gtkaliasdef.c"