]> Pileus Git - ~andy/gtk/blob - gtk/gtkpagesetupunixdialog.c
gtk: remove "gboolean homogeneous" from gtk_box_new()
[~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
58 /**
59  * SECTION:gtkpagesetupunixdialog
60  * @Short_description: A page setup dialog
61  * @Title: GtkPageSetupUnixDialog
62  *
63  * #GtkPageSetupUnixDialog implements a page setup dialog for platforms
64  * which don't provide a native page setup dialog, like Unix. It can
65  * be used very much like any other GTK+ dialog, at the cost of
66  * the portability offered by the <link
67  * linkend="gtk-High-level-Printing-API">high-level printing API</link>
68  *
69  * Printing support was added in GTK+ 2.10.
70  */
71
72
73 struct _GtkPageSetupUnixDialogPrivate
74 {
75   GtkListStore *printer_list;
76   GtkListStore *page_setup_list;
77   GtkListStore *custom_paper_list;
78   
79   GList *print_backends;
80
81   GtkWidget *printer_combo;
82   GtkWidget *paper_size_combo;
83   GtkWidget *paper_size_label;
84
85   GtkWidget *portrait_radio;
86   GtkWidget *reverse_portrait_radio;
87   GtkWidget *landscape_radio;
88   GtkWidget *reverse_landscape_radio;
89
90   gulong request_details_tag;
91   GtkPrinter *request_details_printer;
92   
93   GtkPrintSettings *print_settings;
94
95   /* Save last setup so we can re-set it after selecting manage custom sizes */
96   GtkPageSetup *last_setup;
97
98   gchar *waiting_for_printer;
99 };
100
101 enum {
102   PRINTER_LIST_COL_NAME,
103   PRINTER_LIST_COL_PRINTER,
104   PRINTER_LIST_N_COLS
105 };
106
107 enum {
108   PAGE_SETUP_LIST_COL_PAGE_SETUP,
109   PAGE_SETUP_LIST_COL_IS_SEPARATOR,
110   PAGE_SETUP_LIST_N_COLS
111 };
112
113 G_DEFINE_TYPE (GtkPageSetupUnixDialog, gtk_page_setup_unix_dialog, GTK_TYPE_DIALOG)
114
115 static void gtk_page_setup_unix_dialog_finalize  (GObject                *object);
116 static void populate_dialog                      (GtkPageSetupUnixDialog *dialog);
117 static void fill_paper_sizes_from_printer        (GtkPageSetupUnixDialog *dialog,
118                                                   GtkPrinter             *printer);
119 static void printer_added_cb                     (GtkPrintBackend        *backend,
120                                                   GtkPrinter             *printer,
121                                                   GtkPageSetupUnixDialog *dialog);
122 static void printer_removed_cb                   (GtkPrintBackend        *backend,
123                                                   GtkPrinter             *printer,
124                                                   GtkPageSetupUnixDialog *dialog);
125 static void printer_status_cb                    (GtkPrintBackend        *backend,
126                                                   GtkPrinter             *printer,
127                                                   GtkPageSetupUnixDialog *dialog);
128
129
130
131 static const gchar const common_paper_sizes[][16] = {
132   "na_letter",
133   "na_legal",
134   "iso_a4",
135   "iso_a5",
136   "roc_16k",
137   "iso_b5",
138   "jis_b5",
139   "na_number-10",
140   "iso_dl",
141   "jpn_chou3",
142   "na_ledger",
143   "iso_a3",
144 };
145
146
147 static void
148 gtk_page_setup_unix_dialog_class_init (GtkPageSetupUnixDialogClass *class)
149 {
150   GObjectClass *object_class;
151   GtkWidgetClass *widget_class;
152
153   object_class = (GObjectClass *) class;
154   widget_class = (GtkWidgetClass *) class;
155
156   object_class->finalize = gtk_page_setup_unix_dialog_finalize;
157
158   g_type_class_add_private (class, sizeof (GtkPageSetupUnixDialogPrivate));
159 }
160
161 static void
162 gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
163 {
164   GtkPageSetupUnixDialogPrivate *priv;
165   GtkTreeIter iter;
166   gchar *tmp;
167
168   priv = dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
169                                                      GTK_TYPE_PAGE_SETUP_UNIX_DIALOG,
170                                                      GtkPageSetupUnixDialogPrivate);
171
172   priv->print_backends = NULL;
173
174   priv->printer_list = gtk_list_store_new (PRINTER_LIST_N_COLS,
175                                                    G_TYPE_STRING,
176                                                    G_TYPE_OBJECT);
177
178   gtk_list_store_append (priv->printer_list, &iter);
179   tmp = g_strdup_printf ("<b>%s</b>\n%s", _("Any Printer"), _("For portable documents"));
180   gtk_list_store_set (priv->printer_list, &iter,
181                       PRINTER_LIST_COL_NAME, tmp,
182                       PRINTER_LIST_COL_PRINTER, NULL,
183                       -1);
184   g_free (tmp);
185
186   priv->page_setup_list = gtk_list_store_new (PAGE_SETUP_LIST_N_COLS,
187                                                       G_TYPE_OBJECT,
188                                                       G_TYPE_BOOLEAN);
189
190   priv->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
191   _gtk_print_load_custom_papers (priv->custom_paper_list);
192
193   populate_dialog (dialog);
194   
195   gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
196                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
197                           GTK_STOCK_APPLY, GTK_RESPONSE_OK,
198                           NULL);
199   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
200                                            GTK_RESPONSE_OK,
201                                            GTK_RESPONSE_CANCEL,
202                                            -1);
203
204   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
205 }
206
207 static void
208 gtk_page_setup_unix_dialog_finalize (GObject *object)
209 {
210   GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (object);
211   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
212   GtkPrintBackend *backend;
213   GList *node;
214   
215   if (priv->request_details_tag)
216     {
217       g_signal_handler_disconnect (priv->request_details_printer,
218                                    priv->request_details_tag);
219       g_object_unref (priv->request_details_printer);
220       priv->request_details_printer = NULL;
221       priv->request_details_tag = 0;
222     }
223   
224   if (priv->printer_list)
225     {
226       g_object_unref (priv->printer_list);
227       priv->printer_list = NULL;
228     }
229
230   if (priv->page_setup_list)
231     {
232       g_object_unref (priv->page_setup_list);
233       priv->page_setup_list = NULL;
234     }
235
236   if (priv->custom_paper_list)
237     {
238       g_object_unref (priv->custom_paper_list);
239       priv->custom_paper_list = NULL;
240     }
241
242   if (priv->print_settings)
243     {
244       g_object_unref (priv->print_settings);
245       priv->print_settings = NULL;
246     }
247
248   g_free (priv->waiting_for_printer);
249   priv->waiting_for_printer = NULL;
250
251   for (node = priv->print_backends; node != NULL; node = node->next)
252     {
253       backend = GTK_PRINT_BACKEND (node->data);
254
255       g_signal_handlers_disconnect_by_func (backend, printer_added_cb, dialog);
256       g_signal_handlers_disconnect_by_func (backend, printer_removed_cb, dialog);
257       g_signal_handlers_disconnect_by_func (backend, printer_status_cb, dialog);
258
259       gtk_print_backend_destroy (backend);
260       g_object_unref (backend);
261     }
262   
263   g_list_free (priv->print_backends);
264   priv->print_backends = NULL;
265
266   G_OBJECT_CLASS (gtk_page_setup_unix_dialog_parent_class)->finalize (object);
267 }
268
269 static void
270 printer_added_cb (GtkPrintBackend        *backend, 
271                   GtkPrinter             *printer, 
272                   GtkPageSetupUnixDialog *dialog)
273 {
274   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
275   GtkTreeIter iter;
276   gchar *str;
277   const gchar *location;
278
279   if (gtk_printer_is_virtual (printer))
280     return;
281
282   location = gtk_printer_get_location (printer);
283   if (location == NULL)
284     location = "";
285   str = g_strdup_printf ("<b>%s</b>\n%s",
286                          gtk_printer_get_name (printer),
287                          location);
288
289   gtk_list_store_append (priv->printer_list, &iter);
290   gtk_list_store_set (priv->printer_list, &iter,
291                       PRINTER_LIST_COL_NAME, str,
292                       PRINTER_LIST_COL_PRINTER, printer,
293                       -1);
294
295   g_object_set_data_full (G_OBJECT (printer),
296                           "gtk-print-tree-iter",
297                           gtk_tree_iter_copy (&iter),
298                           (GDestroyNotify) gtk_tree_iter_free);
299   g_free (str);
300
301   if (priv->waiting_for_printer != NULL &&
302       strcmp (priv->waiting_for_printer,
303               gtk_printer_get_name (printer)) == 0)
304     {
305       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
306                                      &iter);
307       priv->waiting_for_printer = NULL;
308     }
309 }
310
311 static void
312 printer_removed_cb (GtkPrintBackend        *backend, 
313                     GtkPrinter             *printer, 
314                     GtkPageSetupUnixDialog *dialog)
315 {
316   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
317   GtkTreeIter *iter;
318
319   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
320   gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
321 }
322
323
324 static void
325 printer_status_cb (GtkPrintBackend        *backend, 
326                    GtkPrinter             *printer, 
327                    GtkPageSetupUnixDialog *dialog)
328 {
329   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
330   GtkTreeIter *iter;
331   gchar *str;
332   const gchar *location;
333   
334   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
335
336   location = gtk_printer_get_location (printer);
337   if (location == NULL)
338     location = "";
339   str = g_strdup_printf ("<b>%s</b>\n%s",
340                          gtk_printer_get_name (printer),
341                          location);
342   gtk_list_store_set (priv->printer_list, iter,
343                       PRINTER_LIST_COL_NAME, str,
344                       -1);
345   g_free (str);
346 }
347
348 static void
349 printer_list_initialize (GtkPageSetupUnixDialog *dialog,
350                          GtkPrintBackend        *print_backend)
351 {
352   GList *list, *node;
353   
354   g_return_if_fail (print_backend != NULL);
355
356   g_signal_connect_object (print_backend, 
357                            "printer-added", 
358                            (GCallback) printer_added_cb, 
359                            G_OBJECT (dialog), 0);
360
361   g_signal_connect_object (print_backend, 
362                            "printer-removed", 
363                            (GCallback) printer_removed_cb, 
364                            G_OBJECT (dialog), 0);
365
366   g_signal_connect_object (print_backend, 
367                            "printer-status-changed", 
368                            (GCallback) printer_status_cb, 
369                            G_OBJECT (dialog), 0);
370
371   list = gtk_print_backend_get_printer_list (print_backend);
372
373   node = list;
374   while (node != NULL)
375     {
376       printer_added_cb (print_backend, node->data, dialog);
377       node = node->next;
378     }
379
380   g_list_free (list);
381   
382 }
383
384 static void
385 load_print_backends (GtkPageSetupUnixDialog *dialog)
386 {
387   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
388   GList *node;
389
390   if (g_module_supported ())
391     priv->print_backends = gtk_print_backend_load_modules ();
392
393   for (node = priv->print_backends; node != NULL; node = node->next)
394     printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
395 }
396
397 static gboolean
398 paper_size_row_is_separator (GtkTreeModel *model,
399                              GtkTreeIter  *iter,
400                              gpointer      data)
401 {
402   gboolean separator;
403
404   gtk_tree_model_get (model, iter, PAGE_SETUP_LIST_COL_IS_SEPARATOR, &separator, -1);
405   return separator;
406 }
407
408 static GtkPageSetup *
409 get_current_page_setup (GtkPageSetupUnixDialog *dialog)
410 {
411   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
412   GtkPageSetup *current_page_setup;
413   GtkComboBox *combo_box;
414   GtkTreeIter iter;
415
416   current_page_setup = NULL;
417   
418   combo_box = GTK_COMBO_BOX (priv->paper_size_combo);
419   if (gtk_combo_box_get_active_iter (combo_box, &iter))
420     gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
421                         PAGE_SETUP_LIST_COL_PAGE_SETUP, &current_page_setup, -1);
422
423   if (current_page_setup)
424     return current_page_setup;
425
426   /* No selected page size, return the default one.
427    * This is used to set the first page setup when the dialog is created
428    * as there is no selection on the first printer_changed.
429    */ 
430   return gtk_page_setup_new ();
431 }
432
433 static gboolean
434 page_setup_is_equal (GtkPageSetup *a, 
435                      GtkPageSetup *b)
436 {
437   return
438     gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
439                              gtk_page_setup_get_paper_size (b)) &&
440     gtk_page_setup_get_top_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_top_margin (b, GTK_UNIT_MM) &&
441     gtk_page_setup_get_bottom_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_bottom_margin (b, GTK_UNIT_MM) &&
442     gtk_page_setup_get_left_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_left_margin (b, GTK_UNIT_MM) &&
443     gtk_page_setup_get_right_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_right_margin (b, GTK_UNIT_MM);
444 }
445
446 static gboolean
447 page_setup_is_same_size (GtkPageSetup *a,
448                          GtkPageSetup *b)
449 {
450   return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
451                                   gtk_page_setup_get_paper_size (b));
452 }
453
454 static gboolean
455 set_paper_size (GtkPageSetupUnixDialog *dialog,
456                 GtkPageSetup           *page_setup,
457                 gboolean                size_only,
458                 gboolean                add_item)
459 {
460   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
461   GtkTreeModel *model;
462   GtkTreeIter iter;
463   GtkPageSetup *list_page_setup;
464
465   model = GTK_TREE_MODEL (priv->page_setup_list);
466
467   if (gtk_tree_model_get_iter_first (model, &iter))
468     {
469       do
470         {
471           gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
472                               PAGE_SETUP_LIST_COL_PAGE_SETUP, &list_page_setup, -1);
473           if (list_page_setup == NULL)
474             continue;
475           
476           if ((size_only && page_setup_is_same_size (page_setup, list_page_setup)) ||
477               (!size_only && page_setup_is_equal (page_setup, list_page_setup)))
478             {
479               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
480                                              &iter);
481               g_object_unref (list_page_setup);
482               return TRUE;
483             }
484               
485           g_object_unref (list_page_setup);
486           
487         } while (gtk_tree_model_iter_next (model, &iter));
488     }
489
490   if (add_item)
491     {
492       gtk_list_store_append (priv->page_setup_list, &iter);
493       gtk_list_store_set (priv->page_setup_list, &iter,
494                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
495                           -1);
496       gtk_list_store_append (priv->page_setup_list, &iter);
497       gtk_list_store_set (priv->page_setup_list, &iter,
498                           PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
499                           -1);
500       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
501                                      &iter);
502       return TRUE;
503     }
504
505   return FALSE;
506 }
507
508 static void
509 fill_custom_paper_sizes (GtkPageSetupUnixDialog *dialog)
510 {
511   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
512   GtkTreeIter iter, paper_iter;
513   GtkTreeModel *model;
514
515   model = GTK_TREE_MODEL (priv->custom_paper_list);
516   if (gtk_tree_model_get_iter_first (model, &iter))
517     {
518       gtk_list_store_append (priv->page_setup_list, &paper_iter);
519       gtk_list_store_set (priv->page_setup_list, &paper_iter,
520                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
521                           -1);
522       do
523         {
524           GtkPageSetup *page_setup;
525           gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
526
527           gtk_list_store_append (priv->page_setup_list, &paper_iter);
528           gtk_list_store_set (priv->page_setup_list, &paper_iter,
529                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
530                               -1);
531
532           g_object_unref (page_setup);
533         } while (gtk_tree_model_iter_next (model, &iter));
534     }
535   
536   gtk_list_store_append (priv->page_setup_list, &paper_iter);
537   gtk_list_store_set (priv->page_setup_list, &paper_iter,
538                       PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
539                       -1);
540   gtk_list_store_append (priv->page_setup_list, &paper_iter);
541   gtk_list_store_set (priv->page_setup_list, &paper_iter,
542                       PAGE_SETUP_LIST_COL_PAGE_SETUP, NULL,
543                       -1);
544 }
545
546 static void
547 fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
548                                GtkPrinter             *printer)
549 {
550   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
551   GList *list, *l;
552   GtkPageSetup *current_page_setup, *page_setup;
553   GtkPaperSize *paper_size;
554   GtkTreeIter iter;
555   gint i;
556
557   gtk_list_store_clear (priv->page_setup_list);
558
559   if (printer == NULL)
560     {
561       for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
562         {
563           page_setup = gtk_page_setup_new ();
564           paper_size = gtk_paper_size_new (common_paper_sizes[i]);
565           gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
566           gtk_paper_size_free (paper_size);
567           
568           gtk_list_store_append (priv->page_setup_list, &iter);
569           gtk_list_store_set (priv->page_setup_list, &iter,
570                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
571                               -1);
572           g_object_unref (page_setup);
573         }
574     }
575   else
576     {
577       list = gtk_printer_list_papers (printer);
578       /* TODO: We should really sort this list so interesting size
579          are at the top */
580       for (l = list; l != NULL; l = l->next)
581         {
582           page_setup = l->data;
583           gtk_list_store_append (priv->page_setup_list, &iter);
584           gtk_list_store_set (priv->page_setup_list, &iter,
585                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
586                               -1);
587           g_object_unref (page_setup);
588         }
589       g_list_free (list);
590     }
591
592   fill_custom_paper_sizes (dialog);
593   
594   current_page_setup = NULL;
595
596   /* When selecting a different printer, select its default paper size */
597   if (printer != NULL)
598     current_page_setup = gtk_printer_get_default_page_size (printer);
599
600   if (current_page_setup == NULL)
601     current_page_setup = get_current_page_setup (dialog);
602
603   if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
604     set_paper_size (dialog, current_page_setup, TRUE, TRUE);
605   
606   if (current_page_setup)
607     g_object_unref (current_page_setup);
608 }
609
610 static void
611 printer_changed_finished_callback (GtkPrinter             *printer,
612                                    gboolean                success,
613                                    GtkPageSetupUnixDialog *dialog)
614 {
615   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
616
617   g_signal_handler_disconnect (priv->request_details_printer,
618                                priv->request_details_tag);
619   g_object_unref (priv->request_details_printer);
620   priv->request_details_tag = 0;
621   priv->request_details_printer = NULL;
622   
623   if (success)
624     fill_paper_sizes_from_printer (dialog, printer);
625
626 }
627
628 static void
629 printer_changed_callback (GtkComboBox            *combo_box,
630                           GtkPageSetupUnixDialog *dialog)
631 {
632   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
633   GtkPrinter *printer;
634   GtkTreeIter iter;
635
636   /* If we're waiting for a specific printer but the user changed
637    * to another printer, cancel that wait. 
638    */
639   if (priv->waiting_for_printer)
640     {
641       g_free (priv->waiting_for_printer);
642       priv->waiting_for_printer = NULL;
643     }
644   
645   if (priv->request_details_tag)
646     {
647       g_signal_handler_disconnect (priv->request_details_printer,
648                                    priv->request_details_tag);
649       g_object_unref (priv->request_details_printer);
650       priv->request_details_printer = NULL;
651       priv->request_details_tag = 0;
652     }
653   
654   if (gtk_combo_box_get_active_iter (combo_box, &iter))
655     {
656       gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
657                           PRINTER_LIST_COL_PRINTER, &printer, -1);
658
659       if (printer == NULL || gtk_printer_has_details (printer))
660         fill_paper_sizes_from_printer (dialog, printer);
661       else
662         {
663           priv->request_details_printer = g_object_ref (printer);
664           priv->request_details_tag =
665             g_signal_connect (printer, "details-acquired",
666                               G_CALLBACK (printer_changed_finished_callback), dialog);
667           gtk_printer_request_details (printer);
668
669         }
670
671       if (printer)
672         g_object_unref (printer);
673
674       if (priv->print_settings)
675         {
676           const char *name = NULL;
677
678           if (printer)
679             name = gtk_printer_get_name (printer);
680           
681           gtk_print_settings_set (priv->print_settings,
682                                   "format-for-printer", name);
683         }
684     }
685 }
686
687 /* We do this munging because we don't want to show zero digits
688    after the decimal point, and not to many such digits if they
689    are nonzero. I wish printf let you specify max precision for %f... */
690 static gchar *
691 double_to_string (gdouble d, 
692                   GtkUnit unit)
693 {
694   gchar *val, *p;
695   struct lconv *locale_data;
696   const gchar *decimal_point;
697   gint decimal_point_len;
698
699   locale_data = localeconv ();
700   decimal_point = locale_data->decimal_point;
701   decimal_point_len = strlen (decimal_point);
702   
703   /* Max two decimal digits for inch, max one for mm */
704   if (unit == GTK_UNIT_INCH)
705     val = g_strdup_printf ("%.2f", d);
706   else
707     val = g_strdup_printf ("%.1f", d);
708
709   if (strstr (val, decimal_point))
710     {
711       p = val + strlen (val) - 1;
712       while (*p == '0')
713         p--;
714       if (p - val + 1 >= decimal_point_len &&
715           strncmp (p - (decimal_point_len - 1), decimal_point, decimal_point_len) == 0)
716         p -= decimal_point_len;
717       p[1] = '\0';
718     }
719
720   return val;
721 }
722
723
724 static void
725 custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
726                                  gint       response_id,
727                                  gpointer   user_data)
728 {
729   GtkPageSetupUnixDialog *page_setup_dialog = GTK_PAGE_SETUP_UNIX_DIALOG (user_data);
730   GtkPageSetupUnixDialogPrivate *priv = page_setup_dialog->priv;
731
732   _gtk_print_load_custom_papers (priv->custom_paper_list);
733
734   /* Update printer page list */
735   printer_changed_callback (GTK_COMBO_BOX (priv->printer_combo), page_setup_dialog);
736
737   gtk_widget_destroy (GTK_WIDGET (custom_paper_dialog));
738 }
739
740 static void
741 paper_size_changed (GtkComboBox            *combo_box,
742                     GtkPageSetupUnixDialog *dialog)
743 {
744   GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
745   GtkTreeIter iter;
746   GtkPageSetup *page_setup, *last_page_setup;
747   GtkUnit unit;
748   gchar *str, *w, *h;
749   gchar *top, *bottom, *left, *right;
750   GtkLabel *label;
751   const gchar *unit_str;
752
753   label = GTK_LABEL (priv->paper_size_label);
754    
755   if (gtk_combo_box_get_active_iter (combo_box, &iter))
756     {
757       gtk_tree_model_get (gtk_combo_box_get_model (combo_box),
758                           &iter, PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
759
760       if (page_setup == NULL)
761         {
762           GtkWidget *custom_paper_dialog;
763
764           /* Change from "manage" menu item to last value */
765           if (priv->last_setup)
766             last_page_setup = g_object_ref (priv->last_setup);
767           else
768             last_page_setup = gtk_page_setup_new (); /* "good" default */
769           set_paper_size (dialog, last_page_setup, FALSE, TRUE);
770           g_object_unref (last_page_setup);
771
772           /* And show the custom paper dialog */
773           custom_paper_dialog = _gtk_custom_paper_unix_dialog_new (GTK_WINDOW (dialog), NULL);
774           g_signal_connect (custom_paper_dialog, "response", G_CALLBACK (custom_paper_dialog_response_cb), dialog);
775           gtk_window_present (GTK_WINDOW (custom_paper_dialog));
776
777           return;
778         }
779
780       if (priv->last_setup)
781         g_object_unref (priv->last_setup);
782
783       priv->last_setup = g_object_ref (page_setup);
784       
785       unit = _gtk_print_get_default_user_units ();
786
787       if (unit == GTK_UNIT_MM)
788         unit_str = _("mm");
789       else
790         unit_str = _("inch");
791         
792
793       w = double_to_string (gtk_page_setup_get_paper_width (page_setup, unit),
794                             unit);
795       h = double_to_string (gtk_page_setup_get_paper_height (page_setup, unit),
796                             unit);
797       str = g_strdup_printf ("%s x %s %s", w, h, unit_str);
798       g_free (w);
799       g_free (h);
800       
801       gtk_label_set_text (label, str);
802       g_free (str);
803
804       top = double_to_string (gtk_page_setup_get_top_margin (page_setup, unit), unit);
805       bottom = double_to_string (gtk_page_setup_get_bottom_margin (page_setup, unit), unit);
806       left = double_to_string (gtk_page_setup_get_left_margin (page_setup, unit), unit);
807       right = double_to_string (gtk_page_setup_get_right_margin (page_setup, unit), unit);
808
809       str = g_strdup_printf (_("Margins:\n"
810                                " Left: %s %s\n"
811                                " Right: %s %s\n"
812                                " Top: %s %s\n"
813                                " Bottom: %s %s"
814                                ),
815                              left, unit_str,
816                              right, unit_str,
817                              top, unit_str,
818                              bottom, unit_str);
819       g_free (top);
820       g_free (bottom);
821       g_free (left);
822       g_free (right);
823       
824       gtk_widget_set_tooltip_text (priv->paper_size_label, str);
825       g_free (str);
826       
827       g_object_unref (page_setup);
828     }
829   else
830     {
831       gtk_label_set_text (label, "");
832       gtk_widget_set_tooltip_text (priv->paper_size_label, NULL);
833       if (priv->last_setup)
834         g_object_unref (priv->last_setup);
835       priv->last_setup = NULL;
836     }
837 }
838
839 static void
840 page_name_func (GtkCellLayout   *cell_layout,
841                 GtkCellRenderer *cell,
842                 GtkTreeModel    *tree_model,
843                 GtkTreeIter     *iter,
844                 gpointer         data)
845 {
846   GtkPageSetup *page_setup;
847   GtkPaperSize *paper_size;
848   
849   gtk_tree_model_get (tree_model, iter,
850                       PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
851   if (page_setup)
852     {
853       paper_size = gtk_page_setup_get_paper_size (page_setup);
854       g_object_set (cell, "text",  gtk_paper_size_get_display_name (paper_size), NULL);
855       g_object_unref (page_setup);
856     }
857   else
858     g_object_set (cell, "text",  _("Manage Custom Sizes..."), NULL);
859       
860 }
861
862 static GtkWidget *
863 create_radio_button (GSList      *group,
864                      const gchar *stock_id)
865 {
866   GtkWidget *radio_button, *image, *label, *hbox;
867   GtkStockItem item;
868
869   radio_button = gtk_radio_button_new (group);
870   image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_LARGE_TOOLBAR);
871   gtk_stock_lookup (stock_id, &item);
872   label = gtk_label_new (item.label);
873   hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 6);
874   gtk_container_add (GTK_CONTAINER (radio_button), hbox);
875   gtk_container_add (GTK_CONTAINER (hbox), image);
876   gtk_container_add (GTK_CONTAINER (hbox), label);
877
878   gtk_widget_show_all (radio_button);
879
880   return radio_button;
881 }
882
883 static void
884 populate_dialog (GtkPageSetupUnixDialog *ps_dialog)
885 {
886   GtkPageSetupUnixDialogPrivate *priv = ps_dialog->priv;
887   GtkDialog *dialog = GTK_DIALOG (ps_dialog);
888   GtkWidget *table, *label, *combo, *radio_button;
889   GtkWidget *action_area, *content_area;
890   GtkCellRenderer *cell;
891
892   gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);
893
894   content_area = gtk_dialog_get_content_area (dialog);
895   action_area = gtk_dialog_get_action_area (dialog);
896
897   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
898   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
899   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
900   gtk_box_set_spacing (GTK_BOX (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 (content_area), 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 }