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