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