]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintunixdialog.c
gtk/gtkprintunixdialog.c: Use accessor functions to access GtkWidget
[~andy/gtk] / gtk / gtkprintunixdialog.c
1 /* GtkPrintUnixDialog
2  * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
3  * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
4  * Copyright © 2006, 2007 Christian Persch
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23 #include <stdlib.h>
24 #include <string.h>
25 #include <ctype.h>
26 #include <stdio.h>
27 #include <math.h>
28
29 #include "gtkintl.h"
30 #include "gtkprivate.h"
31
32 #include "gtkspinbutton.h"
33 #include "gtkcellrendererpixbuf.h"
34 #include "gtkcellrenderertext.h"
35 #include "gtkstock.h"
36 #include "gtkiconfactory.h"
37 #include "gtkimage.h"
38 #include "gtktreeselection.h"
39 #include "gtknotebook.h"
40 #include "gtkscrolledwindow.h"
41 #include "gtkcombobox.h"
42 #include "gtktogglebutton.h"
43 #include "gtkradiobutton.h"
44 #include "gtkdrawingarea.h"
45 #include "gtkvbox.h"
46 #include "gtktable.h"
47 #include "gtkframe.h"
48 #include "gtkalignment.h"
49 #include "gtklabel.h"
50 #include "gtkeventbox.h"
51 #include "gtkbuildable.h"
52
53 #include "gtkcustompaperunixdialog.h"
54 #include "gtkprintbackend.h"
55 #include "gtkprinter-private.h"
56 #include "gtkprintunixdialog.h"
57 #include "gtkprinteroptionwidget.h"
58 #include "gtkprintutils.h"
59
60 #include "gtkmessagedialog.h"
61 #include "gtkbutton.h"
62
63 #define EXAMPLE_PAGE_AREA_SIZE 140
64 #define RULER_DISTANCE 7.5
65 #define RULER_RADIUS 2
66
67
68 static void     gtk_print_unix_dialog_destroy      (GtkPrintUnixDialog *dialog);
69 static void     gtk_print_unix_dialog_finalize     (GObject            *object);
70 static void     gtk_print_unix_dialog_set_property (GObject            *object,
71                                                     guint               prop_id,
72                                                     const GValue       *value,
73                                                     GParamSpec         *pspec);
74 static void     gtk_print_unix_dialog_get_property (GObject            *object,
75                                                     guint               prop_id,
76                                                     GValue             *value,
77                                                     GParamSpec         *pspec);
78 static void     gtk_print_unix_dialog_style_set    (GtkWidget          *widget,
79                                                     GtkStyle           *previous_style);
80 static void     populate_dialog                    (GtkPrintUnixDialog *dialog);
81 static void     unschedule_idle_mark_conflicts     (GtkPrintUnixDialog *dialog);
82 static void     selected_printer_changed           (GtkTreeSelection   *selection,
83                                                     GtkPrintUnixDialog *dialog);
84 static void     clear_per_printer_ui               (GtkPrintUnixDialog *dialog);
85 static void     printer_added_cb                   (GtkPrintBackend    *backend,
86                                                     GtkPrinter         *printer,
87                                                     GtkPrintUnixDialog *dialog);
88 static void     printer_removed_cb                 (GtkPrintBackend    *backend,
89                                                     GtkPrinter         *printer,
90                                                     GtkPrintUnixDialog *dialog);
91 static void     printer_status_cb                  (GtkPrintBackend    *backend,
92                                                     GtkPrinter         *printer,
93                                                     GtkPrintUnixDialog *dialog);
94 static void     update_collate_icon                (GtkToggleButton    *toggle_button,
95                                                     GtkPrintUnixDialog *dialog);
96 static gboolean dialog_get_collate                 (GtkPrintUnixDialog *dialog);
97 static gboolean dialog_get_reverse                 (GtkPrintUnixDialog *dialog);
98 static gint     dialog_get_n_copies                (GtkPrintUnixDialog *dialog);
99
100 static void     set_cell_sensitivity_func          (GtkTreeViewColumn *tree_column,
101                                                     GtkCellRenderer   *cell,
102                                                     GtkTreeModel      *model,
103                                                     GtkTreeIter       *iter,
104                                                     gpointer           data);
105 static gboolean set_active_printer                 (GtkPrintUnixDialog *dialog,
106                                                     const gchar        *printer_name);
107 static void redraw_page_layout_preview             (GtkPrintUnixDialog *dialog);
108
109 /* GtkBuildable */
110 static void gtk_print_unix_dialog_buildable_init                    (GtkBuildableIface *iface);
111 static GObject *gtk_print_unix_dialog_buildable_get_internal_child  (GtkBuildable *buildable,
112                                                                      GtkBuilder   *builder,
113                                                                      const gchar  *childname);
114
115 static const gchar const common_paper_sizes[][16] = {
116   "na_letter",
117   "na_legal",
118   "iso_a4",
119   "iso_a5",
120   "roc_16k",
121   "iso_b5",
122   "jis_b5",
123   "na_number-10",
124   "iso_dl",
125   "jpn_chou3",
126   "na_ledger",
127   "iso_a3",
128 };
129
130 enum {
131   PAGE_SETUP_LIST_COL_PAGE_SETUP,
132   PAGE_SETUP_LIST_COL_IS_SEPARATOR,
133   PAGE_SETUP_LIST_N_COLS
134 };
135
136 enum {
137   PROP_0,
138   PROP_PAGE_SETUP,
139   PROP_CURRENT_PAGE,
140   PROP_PRINT_SETTINGS,
141   PROP_SELECTED_PRINTER,
142   PROP_MANUAL_CAPABILITIES,
143   PROP_SUPPORT_SELECTION,
144   PROP_HAS_SELECTION,
145   PROP_EMBED_PAGE_SETUP
146 };
147
148 enum {
149   PRINTER_LIST_COL_ICON,
150   PRINTER_LIST_COL_NAME,
151   PRINTER_LIST_COL_STATE,
152   PRINTER_LIST_COL_JOBS,
153   PRINTER_LIST_COL_LOCATION,
154   PRINTER_LIST_COL_PRINTER_OBJ,
155   PRINTER_LIST_N_COLS
156 };
157
158 struct GtkPrintUnixDialogPrivate
159 {
160   GtkWidget *notebook;
161
162   GtkWidget *printer_treeview;
163
164   GtkPrintCapabilities manual_capabilities;
165   GtkPrintCapabilities printer_capabilities;
166
167   GtkTreeModel *printer_list;
168   GtkTreeModelFilter *printer_list_filter;
169
170   GtkPageSetup *page_setup;
171   gboolean page_setup_set;
172   gboolean embed_page_setup;
173   GtkListStore *page_setup_list;
174   GtkListStore *custom_paper_list;
175
176   gboolean support_selection;
177   gboolean has_selection;
178
179   GtkWidget *all_pages_radio;
180   GtkWidget *current_page_radio;
181   GtkWidget *selection_radio;
182   GtkWidget *range_table;
183   GtkWidget *page_range_radio;
184   GtkWidget *page_range_entry;
185
186   GtkWidget *copies_spin;
187   GtkWidget *collate_check;
188   GtkWidget *reverse_check;
189   GtkWidget *collate_image;
190   GtkWidget *page_layout_preview;
191   GtkWidget *scale_spin;
192   GtkWidget *page_set_combo;
193   GtkWidget *print_now_radio;
194   GtkWidget *print_at_radio;
195   GtkWidget *print_at_entry;
196   GtkWidget *print_hold_radio;
197   GtkWidget *preview_button;
198   GtkWidget *paper_size_combo;
199   GtkWidget *paper_size_combo_label;
200   GtkWidget *orientation_combo;
201   GtkWidget *orientation_combo_label;
202   gboolean internal_page_setup_change;
203   gboolean updating_print_at;
204   GtkPrinterOptionWidget *pages_per_sheet;
205   GtkPrinterOptionWidget *duplex;
206   GtkPrinterOptionWidget *paper_type;
207   GtkPrinterOptionWidget *paper_source;
208   GtkPrinterOptionWidget *output_tray;
209   GtkPrinterOptionWidget *job_prio;
210   GtkPrinterOptionWidget *billing_info;
211   GtkPrinterOptionWidget *cover_before;
212   GtkPrinterOptionWidget *cover_after;
213   GtkPrinterOptionWidget *number_up_layout;
214
215   GtkWidget *conflicts_widget;
216
217   GtkWidget *job_page;
218   GtkWidget *finishing_table;
219   GtkWidget *finishing_page;
220   GtkWidget *image_quality_table;
221   GtkWidget *image_quality_page;
222   GtkWidget *color_table;
223   GtkWidget *color_page;
224
225   GtkWidget *advanced_vbox;
226   GtkWidget *advanced_page;
227
228   GtkWidget *extension_point;
229
230   /* These are set initially on selected printer (either default printer,
231    * printer taken from set settings, or user-selected), but when any
232    * setting is changed by the user it is cleared.
233    */
234   GtkPrintSettings *initial_settings;
235
236   GtkPrinterOption *number_up_layout_n_option;
237   GtkPrinterOption *number_up_layout_2_option;
238
239   /* This is the initial printer set by set_settings. We look for it in
240    * the added printers. We clear this whenever the user manually changes
241    * to another printer, when the user changes a setting or when we find
242    * this printer.
243    */
244   char *waiting_for_printer;
245   gboolean internal_printer_change;
246
247   GList *print_backends;
248
249   GtkPrinter *current_printer;
250   GtkPrinter *request_details_printer;
251   guint request_details_tag;
252   GtkPrinterOptionSet *options;
253   gulong options_changed_handler;
254   gulong mark_conflicts_id;
255
256   gchar *format_for_printer;
257
258   gint current_page;
259 };
260
261 G_DEFINE_TYPE_WITH_CODE (GtkPrintUnixDialog, gtk_print_unix_dialog, GTK_TYPE_DIALOG,
262                          G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
263                                                 gtk_print_unix_dialog_buildable_init))
264
265 static GtkBuildableIface *parent_buildable_iface;
266
267 static gboolean
268 is_default_printer (GtkPrintUnixDialog *dialog,
269                     GtkPrinter         *printer)
270 {
271   GtkPrintUnixDialogPrivate *priv = dialog->priv;
272
273   if (priv->format_for_printer)
274     return strcmp (priv->format_for_printer,
275                    gtk_printer_get_name (printer)) == 0;
276  else
277    return gtk_printer_is_default (printer);
278 }
279
280 static void
281 gtk_print_unix_dialog_class_init (GtkPrintUnixDialogClass *class)
282 {
283   GObjectClass *object_class;
284   GtkWidgetClass *widget_class;
285
286   object_class = (GObjectClass *) class;
287   widget_class = (GtkWidgetClass *) class;
288
289   object_class->finalize = gtk_print_unix_dialog_finalize;
290   object_class->set_property = gtk_print_unix_dialog_set_property;
291   object_class->get_property = gtk_print_unix_dialog_get_property;
292
293   widget_class->style_set = gtk_print_unix_dialog_style_set;
294
295   g_object_class_install_property (object_class,
296                                    PROP_PAGE_SETUP,
297                                    g_param_spec_object ("page-setup",
298                                                         P_("Page Setup"),
299                                                         P_("The GtkPageSetup to use"),
300                                                         GTK_TYPE_PAGE_SETUP,
301                                                         GTK_PARAM_READWRITE));
302
303   g_object_class_install_property (object_class,
304                                    PROP_CURRENT_PAGE,
305                                    g_param_spec_int ("current-page",
306                                                      P_("Current Page"),
307                                                      P_("The current page in the document"),
308                                                      -1,
309                                                      G_MAXINT,
310                                                      -1,
311                                                      GTK_PARAM_READWRITE));
312
313   g_object_class_install_property (object_class,
314                                    PROP_PRINT_SETTINGS,
315                                    g_param_spec_object ("print-settings",
316                                                         P_("Print Settings"),
317                                                         P_("The GtkPrintSettings used for initializing the dialog"),
318                                                         GTK_TYPE_PRINT_SETTINGS,
319                                                         GTK_PARAM_READWRITE));
320
321   g_object_class_install_property (object_class,
322                                    PROP_SELECTED_PRINTER,
323                                    g_param_spec_object ("selected-printer",
324                                                         P_("Selected Printer"),
325                                                         P_("The GtkPrinter which is selected"),
326                                                         GTK_TYPE_PRINTER,
327                                                         GTK_PARAM_READABLE));
328
329   g_object_class_install_property (object_class,
330                                    PROP_MANUAL_CAPABILITIES,
331                                    g_param_spec_flags ("manual-capabilities",
332                                                        P_("Manual Capabilities"),
333                                                        P_("Capabilities the application can handle"),
334                                                        GTK_TYPE_PRINT_CAPABILITIES,
335                                                        0,
336                                                        GTK_PARAM_READWRITE));
337
338   g_object_class_install_property (object_class,
339                                    PROP_SUPPORT_SELECTION,
340                                    g_param_spec_boolean ("support-selection",
341                                                          P_("Support Selection"),
342                                                          P_("Whether the dialog supports selection"),
343                                                          FALSE,
344                                                          GTK_PARAM_READWRITE));
345
346   g_object_class_install_property (object_class,
347                                    PROP_HAS_SELECTION,
348                                    g_param_spec_boolean ("has-selection",
349                                                          P_("Has Selection"),
350                                                          P_("Whether the application has a selection"),
351                                                          FALSE,
352                                                          GTK_PARAM_READWRITE));
353
354    g_object_class_install_property (object_class,
355                                    PROP_EMBED_PAGE_SETUP,
356                                    g_param_spec_boolean ("embed-page-setup",
357                                                          P_("Embed Page Setup"),
358                                                          P_("TRUE if page setup combos are embedded in GtkPrintUnixDialog"),
359                                                          FALSE,
360                                                          GTK_PARAM_READWRITE));
361
362   g_type_class_add_private (class, sizeof (GtkPrintUnixDialogPrivate));
363 }
364
365 /* Returns a toplevel GtkWindow, or NULL if none */
366 static GtkWindow *
367 get_toplevel (GtkWidget *widget)
368 {
369   GtkWidget *toplevel = NULL;
370
371   toplevel = gtk_widget_get_toplevel (widget);
372   if (!gtk_widget_is_toplevel (toplevel))
373     return NULL;
374   else
375     return GTK_WINDOW (toplevel);
376 }
377
378 static void
379 set_busy_cursor (GtkPrintUnixDialog *dialog,
380                  gboolean            busy)
381 {
382   GtkWidget *widget;
383   GtkWindow *toplevel;
384   GdkDisplay *display;
385   GdkCursor *cursor;
386
387   toplevel = get_toplevel (GTK_WIDGET (dialog));
388   widget = GTK_WIDGET (toplevel);
389
390   if (!toplevel || !gtk_widget_get_realized (widget))
391     return;
392
393   display = gtk_widget_get_display (widget);
394
395   if (busy)
396     cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
397   else
398     cursor = NULL;
399
400   gdk_window_set_cursor (gtk_widget_get_window (widget),
401                          cursor);
402   gdk_display_flush (display);
403
404   if (cursor)
405     gdk_cursor_unref (cursor);
406 }
407
408 static void
409 add_custom_button_to_dialog (GtkDialog   *dialog,
410                              const gchar *mnemonic_label,
411                              const gchar *stock_id,
412                              gint         response_id)
413 {
414   GtkWidget *button = NULL;
415
416   button = gtk_button_new_with_mnemonic (mnemonic_label);
417   gtk_widget_set_can_default (button, TRUE);
418   gtk_button_set_image (GTK_BUTTON (button),
419                         gtk_image_new_from_stock (stock_id,
420                                                   GTK_ICON_SIZE_BUTTON));
421   gtk_widget_show (button);
422
423   gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
424 }
425
426 /* This function handles error messages before printing.
427  */
428 static gboolean
429 error_dialogs (GtkPrintUnixDialog *print_dialog,
430                gint                print_dialog_response_id,
431                gpointer            data)
432 {
433   GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
434   GtkPrinterOption          *option = NULL;
435   GtkPrinter                *printer = NULL;
436   GtkWindow                 *toplevel = NULL;
437   GtkWidget                 *dialog = NULL;
438   GFile                     *file = NULL;
439   gchar                     *basename = NULL;
440   gchar                     *dirname = NULL;
441   int                        response;
442
443   if (print_dialog != NULL && print_dialog_response_id == GTK_RESPONSE_OK)
444     {
445       printer = gtk_print_unix_dialog_get_selected_printer (print_dialog);
446
447       if (printer != NULL)
448         {
449           if (priv->request_details_tag || !gtk_printer_is_accepting_jobs (printer))
450             {
451               g_signal_stop_emission_by_name (print_dialog, "response");
452               return TRUE;
453             }
454
455           /* Shows overwrite confirmation dialog in the case of printing to file which
456            * already exists. */
457           if (gtk_printer_is_virtual (printer))
458             {
459               option = gtk_printer_option_set_lookup (priv->options,
460                                                       "gtk-main-page-custom-input");
461
462               if (option != NULL &&
463                   option->type == GTK_PRINTER_OPTION_TYPE_FILESAVE)
464                 {
465                   file = g_file_new_for_uri (option->value);
466
467                   if (file != NULL &&
468                       g_file_query_exists (file, NULL))
469                     {
470                       toplevel = get_toplevel (GTK_WIDGET (print_dialog));
471
472                       basename = g_file_get_basename (file);
473                       dirname = g_file_get_parse_name (g_file_get_parent (file));
474
475                       dialog = gtk_message_dialog_new (toplevel,
476                                                        GTK_DIALOG_MODAL |
477                                                        GTK_DIALOG_DESTROY_WITH_PARENT,
478                                                        GTK_MESSAGE_QUESTION,
479                                                        GTK_BUTTONS_NONE,
480                                                        _("A file named \"%s\" already exists.  Do you want to replace it?"),
481                                                        basename);
482
483                       gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
484                                                                 _("The file already exists in \"%s\".  Replacing it will "
485                                                                 "overwrite its contents."),
486                                                                 dirname);
487
488                       gtk_dialog_add_button (GTK_DIALOG (dialog),
489                                              GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
490                       add_custom_button_to_dialog (GTK_DIALOG (dialog),
491                                                    _("_Replace"),
492                                                    GTK_STOCK_PRINT,
493                                                    GTK_RESPONSE_ACCEPT);
494                       gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
495                                                                GTK_RESPONSE_ACCEPT,
496                                                                GTK_RESPONSE_CANCEL,
497                                                                -1);
498                       gtk_dialog_set_default_response (GTK_DIALOG (dialog),
499                                                        GTK_RESPONSE_ACCEPT);
500
501                       if (toplevel->group)
502                         gtk_window_group_add_window (toplevel->group,
503                                                      GTK_WINDOW (dialog));
504
505                       response = gtk_dialog_run (GTK_DIALOG (dialog));
506
507                       gtk_widget_destroy (dialog);
508
509                       g_free (dirname);
510                       g_free (basename);
511
512                       if (response != GTK_RESPONSE_ACCEPT)
513                         {
514                           g_signal_stop_emission_by_name (print_dialog, "response");
515                           g_object_unref (file);
516                           return TRUE;
517                         }
518                     }
519
520                   g_object_unref (file);
521                 }
522             }
523         }
524     }
525   return FALSE;
526 }
527
528 static void
529 gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
530 {
531   GtkPrintUnixDialogPrivate *priv;
532
533   dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
534                                               GTK_TYPE_PRINT_UNIX_DIALOG,
535                                               GtkPrintUnixDialogPrivate);
536   priv = dialog->priv;
537
538   priv->print_backends = NULL;
539   priv->current_page = -1;
540   priv->number_up_layout_n_option = NULL;
541   priv->number_up_layout_2_option = NULL;
542
543   priv->page_setup = gtk_page_setup_new ();
544   priv->page_setup_set = FALSE;
545   priv->embed_page_setup = FALSE;
546   priv->internal_page_setup_change = FALSE;
547
548   priv->support_selection = FALSE;
549   priv->has_selection = FALSE;
550
551   g_signal_connect (dialog,
552                     "destroy",
553                     (GCallback) gtk_print_unix_dialog_destroy,
554                     NULL);
555
556   g_signal_connect (dialog,
557                     "response",
558                     (GCallback) error_dialogs,
559                     NULL);
560
561   g_signal_connect (dialog,
562                     "notify::page-setup",
563                     (GCallback) redraw_page_layout_preview,
564                     NULL);
565
566   priv->preview_button = gtk_button_new_from_stock (GTK_STOCK_PRINT_PREVIEW);
567   gtk_widget_show (priv->preview_button);
568
569   gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
570                                 priv->preview_button,
571                                 GTK_RESPONSE_APPLY);
572   gtk_dialog_add_buttons (GTK_DIALOG (dialog),
573                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
574                           GTK_STOCK_PRINT, GTK_RESPONSE_OK,
575                           NULL);
576   gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
577                                            GTK_RESPONSE_APPLY,
578                                            GTK_RESPONSE_OK,
579                                            GTK_RESPONSE_CANCEL,
580                                            -1);
581
582   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
583   gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
584
585   priv->page_setup_list = gtk_list_store_new (PAGE_SETUP_LIST_N_COLS,
586                                               G_TYPE_OBJECT,
587                                               G_TYPE_BOOLEAN);
588
589   priv->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
590   _gtk_print_load_custom_papers (priv->custom_paper_list);
591
592   populate_dialog (dialog);
593 }
594
595 static void
596 gtk_print_unix_dialog_destroy (GtkPrintUnixDialog *dialog)
597 {
598   /* Make sure we don't destroy custom widgets owned by the backends */
599   clear_per_printer_ui (dialog);
600 }
601
602 static void
603 disconnect_printer_details_request (GtkPrintUnixDialog *dialog, gboolean details_failed)
604 {
605   GtkPrintUnixDialogPrivate *priv = dialog->priv;
606
607   if (priv->request_details_tag)
608     {
609       g_signal_handler_disconnect (priv->request_details_printer,
610                                    priv->request_details_tag);
611       priv->request_details_tag = 0;
612       set_busy_cursor (dialog, FALSE);
613       if (details_failed)
614         gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
615                             g_object_get_data (G_OBJECT (priv->request_details_printer),
616                                                "gtk-print-tree-iter"),
617                             PRINTER_LIST_COL_STATE,
618                              _("Getting printer information failed"),
619                             -1);
620       else
621         gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
622                             g_object_get_data (G_OBJECT (priv->request_details_printer),
623                                                "gtk-print-tree-iter"),
624                             PRINTER_LIST_COL_STATE,
625                             gtk_printer_get_state_message (priv->request_details_printer),
626                             -1);
627       g_object_unref (priv->request_details_printer);
628       priv->request_details_printer = NULL;
629     }
630 }
631
632 static void
633 gtk_print_unix_dialog_finalize (GObject *object)
634 {
635   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
636   GtkPrintUnixDialogPrivate *priv = dialog->priv;
637   GtkPrintBackend *backend;
638   GList *node;
639
640   unschedule_idle_mark_conflicts (dialog);
641   disconnect_printer_details_request (dialog, FALSE);
642
643   if (priv->current_printer)
644     {
645       g_object_unref (priv->current_printer);
646       priv->current_printer = NULL;
647     }
648
649   if (priv->printer_list)
650     {
651       g_object_unref (priv->printer_list);
652       priv->printer_list = NULL;
653     }
654
655   if (priv->custom_paper_list)
656     {
657       g_object_unref (priv->custom_paper_list);
658       priv->custom_paper_list = NULL;
659     }
660
661   if (priv->printer_list_filter)
662     {
663       g_object_unref (priv->printer_list_filter);
664       priv->printer_list_filter = NULL;
665     }
666
667   if (priv->options)
668     {
669       g_object_unref (priv->options);
670       priv->options = NULL;
671     }
672
673   if (priv->number_up_layout_2_option)
674     {
675       priv->number_up_layout_2_option->choices[0] = NULL;
676       priv->number_up_layout_2_option->choices[1] = NULL;
677       g_free (priv->number_up_layout_2_option->choices_display[0]);
678       g_free (priv->number_up_layout_2_option->choices_display[1]);
679       priv->number_up_layout_2_option->choices_display[0] = NULL;
680       priv->number_up_layout_2_option->choices_display[1] = NULL;
681       g_object_unref (priv->number_up_layout_2_option);
682       priv->number_up_layout_2_option = NULL;
683     }
684
685   if (priv->number_up_layout_n_option)
686     {
687       g_object_unref (priv->number_up_layout_n_option);
688       priv->number_up_layout_n_option = NULL;
689     }
690
691  if (priv->page_setup)
692     {
693       g_object_unref (priv->page_setup);
694       priv->page_setup = NULL;
695     }
696
697   if (priv->initial_settings)
698     {
699       g_object_unref (priv->initial_settings);
700       priv->initial_settings = NULL;
701     }
702
703   g_free (priv->waiting_for_printer);
704   priv->waiting_for_printer = NULL;
705
706   g_free (priv->format_for_printer);
707   priv->format_for_printer = NULL;
708
709   for (node = priv->print_backends; node != NULL; node = node->next)
710     {
711       backend = GTK_PRINT_BACKEND (node->data);
712
713       g_signal_handlers_disconnect_by_func (backend, printer_added_cb, dialog);
714       g_signal_handlers_disconnect_by_func (backend, printer_removed_cb, dialog);
715       g_signal_handlers_disconnect_by_func (backend, printer_status_cb, dialog);
716
717       gtk_print_backend_destroy (backend);
718       g_object_unref (backend);
719     }
720
721   g_list_free (priv->print_backends);
722   priv->print_backends = NULL;
723
724   if (priv->page_setup_list)
725     {
726       g_object_unref (priv->page_setup_list);
727       priv->page_setup_list = NULL;
728     }
729
730   G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
731 }
732
733 static void
734 printer_removed_cb (GtkPrintBackend    *backend,
735                     GtkPrinter         *printer,
736                     GtkPrintUnixDialog *dialog)
737 {
738   GtkPrintUnixDialogPrivate *priv = dialog->priv;
739   GtkTreeIter *iter;
740
741   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
742   gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
743 }
744
745 static void
746 gtk_print_unix_dialog_buildable_init (GtkBuildableIface *iface)
747 {
748   parent_buildable_iface = g_type_interface_peek_parent (iface);
749
750   iface->get_internal_child = gtk_print_unix_dialog_buildable_get_internal_child;
751 }
752
753 static GObject *
754 gtk_print_unix_dialog_buildable_get_internal_child (GtkBuildable *buildable,
755                                                     GtkBuilder   *builder,
756                                                     const gchar  *childname)
757 {
758   if (strcmp (childname, "notebook") == 0)
759     return G_OBJECT (GTK_PRINT_UNIX_DIALOG (buildable)->priv->notebook);
760
761   return parent_buildable_iface->get_internal_child (buildable, builder, childname);
762 }
763
764 /* This function controls "sensitive" property of GtkCellRenderer based on pause
765  * state of printers. */
766 void set_cell_sensitivity_func (GtkTreeViewColumn *tree_column,
767                                 GtkCellRenderer   *cell,
768                                 GtkTreeModel      *tree_model,
769                                 GtkTreeIter       *iter,
770                                 gpointer           data)
771 {
772   GtkPrinter *printer;
773
774   gtk_tree_model_get (tree_model, iter, PRINTER_LIST_COL_PRINTER_OBJ, &printer, -1);
775
776   if (printer != NULL && !gtk_printer_is_accepting_jobs (printer))
777     g_object_set (cell,
778                   "sensitive", FALSE,
779                   NULL);
780   else
781     g_object_set (cell,
782                   "sensitive", TRUE,
783                   NULL);
784 }
785
786 static void
787 printer_status_cb (GtkPrintBackend    *backend,
788                    GtkPrinter         *printer,
789                    GtkPrintUnixDialog *dialog)
790 {
791   GtkPrintUnixDialogPrivate *priv = dialog->priv;
792   GtkTreeIter *iter;
793   GtkTreeSelection *selection;
794
795   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
796
797   gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), iter,
798                       PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
799                       PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
800                       PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
801                       PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
802                       -1);
803
804   /* When the pause state change then we need to update sensitive property
805    * of GTK_RESPONSE_OK button inside of selected_printer_changed function. */
806   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
807   selected_printer_changed (selection, dialog);
808
809   if (gtk_print_backend_printer_list_is_done (backend) &&
810       gtk_printer_is_default (printer) &&
811       (gtk_tree_selection_count_selected_rows (selection) == 0))
812     set_active_printer (dialog, gtk_printer_get_name (printer));
813 }
814
815 static void
816 printer_added_cb (GtkPrintBackend    *backend,
817                   GtkPrinter         *printer,
818                   GtkPrintUnixDialog *dialog)
819 {
820   GtkPrintUnixDialogPrivate *priv = dialog->priv;
821   GtkTreeIter iter, filter_iter;
822   GtkTreeSelection *selection;
823   GtkTreePath *path;
824
825   gtk_list_store_append (GTK_LIST_STORE (priv->printer_list), &iter);
826
827   g_object_set_data_full (G_OBJECT (printer),
828                          "gtk-print-tree-iter",
829                           gtk_tree_iter_copy (&iter),
830                           (GDestroyNotify) gtk_tree_iter_free);
831
832   gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), &iter,
833                       PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
834                       PRINTER_LIST_COL_NAME, gtk_printer_get_name (printer),
835                       PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
836                       PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
837                       PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
838                       PRINTER_LIST_COL_PRINTER_OBJ, printer,
839                       -1);
840
841   gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
842                                                     &filter_iter, &iter);
843   path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->printer_list_filter), &filter_iter);
844
845   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
846
847   if (priv->waiting_for_printer != NULL &&
848       strcmp (gtk_printer_get_name (printer),
849               priv->waiting_for_printer) == 0)
850     {
851       priv->internal_printer_change = TRUE;
852       gtk_tree_selection_select_iter (selection, &filter_iter);
853       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
854                                     path, NULL, TRUE, 0.5, 0.0);
855       priv->internal_printer_change = FALSE;
856       g_free (priv->waiting_for_printer);
857       priv->waiting_for_printer = NULL;
858     }
859   else if (is_default_printer (dialog, printer) &&
860            gtk_tree_selection_count_selected_rows (selection) == 0)
861     {
862       priv->internal_printer_change = TRUE;
863       gtk_tree_selection_select_iter (selection, &filter_iter);
864       gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
865                                     path, NULL, TRUE, 0.5, 0.0);
866       priv->internal_printer_change = FALSE;
867     }
868
869   gtk_tree_path_free (path);
870 }
871
872 static void
873 printer_list_initialize (GtkPrintUnixDialog *dialog,
874                          GtkPrintBackend    *print_backend)
875 {
876   GList *list;
877   GList *node;
878
879   g_return_if_fail (print_backend != NULL);
880
881   g_signal_connect_object (print_backend,
882                            "printer-added",
883                            (GCallback) printer_added_cb,
884                            G_OBJECT (dialog), 0);
885
886   g_signal_connect_object (print_backend,
887                            "printer-removed",
888                            (GCallback) printer_removed_cb,
889                            G_OBJECT (dialog), 0);
890
891   g_signal_connect_object (print_backend,
892                            "printer-status-changed",
893                            (GCallback) printer_status_cb,
894                            G_OBJECT (dialog), 0);
895
896   list = gtk_print_backend_get_printer_list (print_backend);
897
898   node = list;
899   while (node != NULL)
900     {
901       printer_added_cb (print_backend, node->data, dialog);
902       node = node->next;
903     }
904
905   g_list_free (list);
906 }
907
908 static void
909 load_print_backends (GtkPrintUnixDialog *dialog)
910 {
911   GtkPrintUnixDialogPrivate *priv = dialog->priv;
912   GList *node;
913
914   if (g_module_supported ())
915     priv->print_backends = gtk_print_backend_load_modules ();
916
917   for (node = priv->print_backends; node != NULL; node = node->next)
918     {
919       GtkPrintBackend *backend = node->data;
920       printer_list_initialize (dialog, backend);
921     }
922 }
923
924 static void
925 gtk_print_unix_dialog_set_property (GObject      *object,
926                                     guint         prop_id,
927                                     const GValue *value,
928                                     GParamSpec   *pspec)
929
930 {
931   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
932
933   switch (prop_id)
934     {
935     case PROP_PAGE_SETUP:
936       gtk_print_unix_dialog_set_page_setup (dialog, g_value_get_object (value));
937       break;
938     case PROP_CURRENT_PAGE:
939       gtk_print_unix_dialog_set_current_page (dialog, g_value_get_int (value));
940       break;
941     case PROP_PRINT_SETTINGS:
942       gtk_print_unix_dialog_set_settings (dialog, g_value_get_object (value));
943       break;
944     case PROP_MANUAL_CAPABILITIES:
945       gtk_print_unix_dialog_set_manual_capabilities (dialog, g_value_get_flags (value));
946       break;
947     case PROP_SUPPORT_SELECTION:
948       gtk_print_unix_dialog_set_support_selection (dialog, g_value_get_boolean (value));
949       break;
950     case PROP_HAS_SELECTION:
951       gtk_print_unix_dialog_set_has_selection (dialog, g_value_get_boolean (value));
952       break;
953     case PROP_EMBED_PAGE_SETUP:
954       gtk_print_unix_dialog_set_embed_page_setup (dialog, g_value_get_boolean (value));
955       break;
956     default:
957       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
958       break;
959     }
960 }
961
962 static void
963 gtk_print_unix_dialog_get_property (GObject    *object,
964                                     guint       prop_id,
965                                     GValue     *value,
966                                     GParamSpec *pspec)
967 {
968   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
969   GtkPrintUnixDialogPrivate *priv = dialog->priv;
970
971   switch (prop_id)
972     {
973     case PROP_PAGE_SETUP:
974       g_value_set_object (value, priv->page_setup);
975       break;
976     case PROP_CURRENT_PAGE:
977       g_value_set_int (value, priv->current_page);
978       break;
979     case PROP_PRINT_SETTINGS:
980       g_value_take_object (value, gtk_print_unix_dialog_get_settings (dialog));
981       break;
982     case PROP_SELECTED_PRINTER:
983       g_value_set_object (value, priv->current_printer);
984       break;
985     case PROP_MANUAL_CAPABILITIES:
986       g_value_set_flags (value, priv->manual_capabilities);
987       break;
988     case PROP_SUPPORT_SELECTION:
989       g_value_set_boolean (value, priv->support_selection);
990       break;
991     case PROP_HAS_SELECTION:
992       g_value_set_boolean (value, priv->has_selection);
993       break;
994     case PROP_EMBED_PAGE_SETUP:
995       g_value_set_boolean (value, priv->embed_page_setup);
996       break;
997     default:
998       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
999       break;
1000     }
1001 }
1002
1003 static gboolean
1004 is_printer_active (GtkTreeModel       *model,
1005                    GtkTreeIter        *iter,
1006                    GtkPrintUnixDialog *dialog)
1007 {
1008   gboolean result;
1009   GtkPrinter *printer;
1010   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1011
1012   gtk_tree_model_get (model,
1013                       iter,
1014                       PRINTER_LIST_COL_PRINTER_OBJ,
1015                       &printer,
1016                       -1);
1017
1018   if (printer == NULL)
1019     return FALSE;
1020
1021   result = gtk_printer_is_active (printer);
1022
1023   if (result &&
1024       priv->manual_capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF |
1025                                    GTK_PRINT_CAPABILITY_GENERATE_PS))
1026     {
1027        /* Check that the printer can handle at least one of the data
1028         * formats that the application supports.
1029         */
1030        result = ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF) &&
1031                  gtk_printer_accepts_pdf (printer)) ||
1032                 ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS) &&
1033                  gtk_printer_accepts_ps (printer));
1034     }
1035
1036   g_object_unref (printer);
1037
1038   return result;
1039 }
1040
1041 static gint
1042 default_printer_list_sort_func (GtkTreeModel *model,
1043                                 GtkTreeIter  *a,
1044                                 GtkTreeIter  *b,
1045                                 gpointer      user_data)
1046 {
1047   gchar *a_name;
1048   gchar *b_name;
1049   GtkPrinter *a_printer;
1050   GtkPrinter *b_printer;
1051   gint result;
1052
1053   gtk_tree_model_get (model, a,
1054                       PRINTER_LIST_COL_NAME, &a_name,
1055                       PRINTER_LIST_COL_PRINTER_OBJ, &a_printer,
1056                       -1);
1057   gtk_tree_model_get (model, b,
1058                       PRINTER_LIST_COL_NAME, &b_name,
1059                       PRINTER_LIST_COL_PRINTER_OBJ, &b_printer,
1060                       -1);
1061
1062   if (a_printer == NULL && b_printer == NULL)
1063     result = 0;
1064   else if (a_printer == NULL)
1065    result = G_MAXINT;
1066   else if (b_printer == NULL)
1067    result = G_MININT;
1068   else if (gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
1069     result = 0;
1070   else if (gtk_printer_is_virtual (a_printer) && !gtk_printer_is_virtual (b_printer))
1071     result = G_MININT;
1072   else if (!gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
1073     result = G_MAXINT;
1074   else if (a_name == NULL && b_name == NULL)
1075     result = 0;
1076   else if (a_name == NULL && b_name != NULL)
1077     result = 1;
1078   else if (a_name != NULL && b_name == NULL)
1079     result = -1;
1080   else
1081     result = g_ascii_strcasecmp (a_name, b_name);
1082
1083   g_free (a_name);
1084   g_free (b_name);
1085   g_object_unref (a_printer);
1086   g_object_unref (b_printer);
1087
1088   return result;
1089 }
1090
1091 static void
1092 create_printer_list_model (GtkPrintUnixDialog *dialog)
1093 {
1094   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1095   GtkListStore *model;
1096   GtkTreeSortable *sort;
1097
1098   model = gtk_list_store_new (PRINTER_LIST_N_COLS,
1099                               G_TYPE_STRING,
1100                               G_TYPE_STRING,
1101                               G_TYPE_STRING,
1102                               G_TYPE_INT,
1103                               G_TYPE_STRING,
1104                               G_TYPE_OBJECT);
1105
1106   priv->printer_list = (GtkTreeModel *)model;
1107   priv->printer_list_filter = (GtkTreeModelFilter *) gtk_tree_model_filter_new ((GtkTreeModel *)model,
1108                                                                                         NULL);
1109
1110   gtk_tree_model_filter_set_visible_func (priv->printer_list_filter,
1111                                           (GtkTreeModelFilterVisibleFunc) is_printer_active,
1112                                           dialog,
1113                                           NULL);
1114
1115   sort = GTK_TREE_SORTABLE (model);
1116   gtk_tree_sortable_set_default_sort_func (sort,
1117                                            default_printer_list_sort_func,
1118                                            NULL,
1119                                            NULL);
1120
1121   gtk_tree_sortable_set_sort_column_id (sort,
1122                                         GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
1123                                         GTK_SORT_ASCENDING);
1124
1125 }
1126
1127
1128 static GtkWidget *
1129 wrap_in_frame (const gchar *label,
1130                GtkWidget   *child)
1131 {
1132   GtkWidget *frame, *alignment, *label_widget;
1133   gchar *bold_text;
1134
1135   label_widget = gtk_label_new (NULL);
1136   gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
1137   gtk_widget_show (label_widget);
1138
1139   bold_text = g_markup_printf_escaped ("<b>%s</b>", label);
1140   gtk_label_set_markup (GTK_LABEL (label_widget), bold_text);
1141   g_free (bold_text);
1142
1143   frame = gtk_vbox_new (FALSE, 6);
1144   gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
1145
1146   alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
1147   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
1148                              0, 0, 12, 0);
1149   gtk_box_pack_start (GTK_BOX (frame), alignment, FALSE, FALSE, 0);
1150
1151   gtk_container_add (GTK_CONTAINER (alignment), child);
1152
1153   gtk_widget_show (frame);
1154   gtk_widget_show (alignment);
1155
1156   return frame;
1157 }
1158
1159 static gboolean
1160 setup_option (GtkPrintUnixDialog     *dialog,
1161               const gchar            *option_name,
1162               GtkPrinterOptionWidget *widget)
1163 {
1164   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1165   GtkPrinterOption *option;
1166
1167   option = gtk_printer_option_set_lookup (priv->options, option_name);
1168   gtk_printer_option_widget_set_source (widget, option);
1169
1170   return option != NULL;
1171 }
1172
1173 static void
1174 add_option_to_extension_point (GtkPrinterOption *option,
1175                                gpointer          data)
1176 {
1177   GtkWidget *extension_point = data;
1178   GtkWidget *widget;
1179
1180   widget = gtk_printer_option_widget_new (option);
1181   gtk_widget_show (widget);
1182
1183   if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
1184     {
1185       GtkWidget *label, *hbox;
1186
1187       label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
1188       gtk_widget_show (label);
1189       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1190       gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
1191
1192       hbox = gtk_hbox_new (FALSE, 12);
1193       gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
1194       gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
1195       gtk_widget_show (hbox);
1196
1197       gtk_box_pack_start (GTK_BOX (extension_point), hbox, FALSE, FALSE, 0);
1198     }
1199   else
1200     gtk_box_pack_start (GTK_BOX (extension_point), widget, FALSE, FALSE, 0);
1201 }
1202
1203 static void
1204 add_option_to_table (GtkPrinterOption *option,
1205                      gpointer          user_data)
1206 {
1207   GtkTable *table;
1208   GtkWidget *label, *widget;
1209   guint row;
1210
1211   table = GTK_TABLE (user_data);
1212
1213   if (g_str_has_prefix (option->name, "gtk-"))
1214     return;
1215
1216   widget = gtk_printer_option_widget_new (option);
1217   gtk_widget_show (widget);
1218
1219   gtk_table_get_size (table, &row, NULL);
1220   gtk_table_resize (table, row + 1, 2);
1221
1222   if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
1223     {
1224       label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
1225       gtk_widget_show (label);
1226
1227       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1228       gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
1229
1230       gtk_table_attach (table, label,
1231                         0, 1, row - 1 , row,  GTK_FILL, 0, 0, 0);
1232
1233       gtk_table_attach (table, widget,
1234                         1, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
1235     }
1236   else
1237     gtk_table_attach (table, widget,
1238                       0, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
1239 }
1240
1241 static void
1242 setup_page_table (GtkPrinterOptionSet *options,
1243                   const gchar         *group,
1244                   GtkWidget           *table,
1245                   GtkWidget           *page)
1246 {
1247   guint nrows;
1248
1249   gtk_printer_option_set_foreach_in_group (options, group,
1250                                            add_option_to_table,
1251                                            table);
1252
1253   gtk_table_get_size (GTK_TABLE (table), &nrows, NULL);
1254   if (nrows == 1)
1255     gtk_widget_hide (page);
1256   else
1257     gtk_widget_show (page);
1258 }
1259
1260 static void
1261 update_print_at_option (GtkPrintUnixDialog *dialog)
1262 {
1263   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1264   GtkPrinterOption *option;
1265
1266   option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1267
1268   if (option == NULL)
1269     return;
1270
1271   if (priv->updating_print_at)
1272     return;
1273
1274   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_at_radio)))
1275     gtk_printer_option_set (option, "at");
1276   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio)))
1277     gtk_printer_option_set (option, "on-hold");
1278   else
1279     gtk_printer_option_set (option, "now");
1280
1281   option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1282   if (option != NULL)
1283     {
1284       const char *text = gtk_entry_get_text (GTK_ENTRY (priv->print_at_entry));
1285       gtk_printer_option_set (option, text);
1286     }
1287 }
1288
1289
1290 static gboolean
1291 setup_print_at (GtkPrintUnixDialog *dialog)
1292 {
1293   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1294   GtkPrinterOption *option;
1295
1296   option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1297
1298   if (option == NULL)
1299     {
1300       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
1301                                     TRUE);
1302       gtk_widget_set_sensitive (priv->print_at_radio, FALSE);
1303       gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
1304       gtk_widget_set_sensitive (priv->print_hold_radio, FALSE);
1305       gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry), "");
1306       return FALSE;
1307     }
1308
1309   priv->updating_print_at = TRUE;
1310
1311   gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
1312   gtk_widget_set_sensitive (priv->print_at_radio,
1313                             gtk_printer_option_has_choice (option, "at"));
1314
1315   gtk_widget_set_sensitive (priv->print_hold_radio,
1316                             gtk_printer_option_has_choice (option, "on-hold"));
1317
1318   update_print_at_option (dialog);
1319
1320   if (strcmp (option->value, "at") == 0)
1321     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_at_radio),
1322                                   TRUE);
1323   else if (strcmp (option->value, "on-hold") == 0)
1324     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio),
1325                                   TRUE);
1326   else
1327     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
1328                                   TRUE);
1329
1330   option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1331   if (option != NULL)
1332     gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry), option->value);
1333
1334   priv->updating_print_at = FALSE;
1335
1336   return TRUE;
1337 }
1338
1339 static void
1340 update_dialog_from_settings (GtkPrintUnixDialog *dialog)
1341 {
1342   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1343   GList *groups, *l;
1344   gchar *group;
1345   GtkWidget *table, *frame;
1346   gboolean has_advanced, has_job;
1347   guint nrows;
1348
1349   if (priv->current_printer == NULL)
1350     {
1351        clear_per_printer_ui (dialog);
1352        gtk_widget_hide (priv->job_page);
1353        gtk_widget_hide (priv->advanced_page);
1354        gtk_widget_hide (priv->image_quality_page);
1355        gtk_widget_hide (priv->finishing_page);
1356        gtk_widget_hide (priv->color_page);
1357        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1358
1359        return;
1360     }
1361
1362   setup_option (dialog, "gtk-n-up", priv->pages_per_sheet);
1363   setup_option (dialog, "gtk-n-up-layout", priv->number_up_layout);
1364   setup_option (dialog, "gtk-duplex", priv->duplex);
1365   setup_option (dialog, "gtk-paper-type", priv->paper_type);
1366   setup_option (dialog, "gtk-paper-source", priv->paper_source);
1367   setup_option (dialog, "gtk-output-tray", priv->output_tray);
1368
1369   has_job = FALSE;
1370   has_job |= setup_option (dialog, "gtk-job-prio", priv->job_prio);
1371   has_job |= setup_option (dialog, "gtk-billing-info", priv->billing_info);
1372   has_job |= setup_option (dialog, "gtk-cover-before", priv->cover_before);
1373   has_job |= setup_option (dialog, "gtk-cover-after", priv->cover_after);
1374   has_job |= setup_print_at (dialog);
1375
1376   if (has_job)
1377     gtk_widget_show (priv->job_page);
1378   else
1379     gtk_widget_hide (priv->job_page);
1380
1381   setup_page_table (priv->options,
1382                     "ImageQualityPage",
1383                     priv->image_quality_table,
1384                     priv->image_quality_page);
1385
1386   setup_page_table (priv->options,
1387                     "FinishingPage",
1388                     priv->finishing_table,
1389                     priv->finishing_page);
1390
1391   setup_page_table (priv->options,
1392                     "ColorPage",
1393                     priv->color_table,
1394                     priv->color_page);
1395
1396   gtk_printer_option_set_foreach_in_group (priv->options,
1397                                            "GtkPrintDialogExtension",
1398                                            add_option_to_extension_point,
1399                                            priv->extension_point);
1400
1401   /* Put the rest of the groups in the advanced page */
1402   groups = gtk_printer_option_set_get_groups (priv->options);
1403
1404   has_advanced = FALSE;
1405   for (l = groups; l != NULL; l = l->next)
1406     {
1407       group = l->data;
1408
1409       if (group == NULL)
1410         continue;
1411
1412       if (strcmp (group, "ImageQualityPage") == 0 ||
1413           strcmp (group, "ColorPage") == 0 ||
1414           strcmp (group, "FinishingPage") == 0 ||
1415           strcmp (group, "GtkPrintDialogExtension") == 0)
1416         continue;
1417
1418       table = gtk_table_new (1, 2, FALSE);
1419       gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1420       gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1421
1422       gtk_printer_option_set_foreach_in_group (priv->options,
1423                                                group,
1424                                                add_option_to_table,
1425                                                table);
1426
1427       gtk_table_get_size (GTK_TABLE (table), &nrows, NULL);
1428       if (nrows == 1)
1429         gtk_widget_destroy (table);
1430       else
1431         {
1432           has_advanced = TRUE;
1433           frame = wrap_in_frame (group, table);
1434           gtk_widget_show (table);
1435           gtk_widget_show (frame);
1436
1437           gtk_box_pack_start (GTK_BOX (priv->advanced_vbox),
1438                               frame, FALSE, FALSE, 0);
1439         }
1440     }
1441
1442   if (has_advanced)
1443     gtk_widget_show (priv->advanced_page);
1444   else
1445     gtk_widget_hide (priv->advanced_page);
1446
1447   g_list_foreach (groups, (GFunc) g_free, NULL);
1448   g_list_free (groups);
1449 }
1450
1451 static void
1452 update_dialog_from_capabilities (GtkPrintUnixDialog *dialog)
1453 {
1454   GtkPrintCapabilities caps;
1455   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1456   gboolean can_collate;
1457   const gchar *copies;
1458
1459   copies = gtk_entry_get_text (GTK_ENTRY (priv->copies_spin));
1460   can_collate = (*copies != '\0' && atoi (copies) > 1);
1461
1462   caps = priv->manual_capabilities | priv->printer_capabilities;
1463
1464   gtk_widget_set_sensitive (priv->page_set_combo,
1465                             caps & GTK_PRINT_CAPABILITY_PAGE_SET);
1466   gtk_widget_set_sensitive (priv->copies_spin,
1467                             caps & GTK_PRINT_CAPABILITY_COPIES);
1468   gtk_widget_set_sensitive (priv->collate_check,
1469                             can_collate &&
1470                             (caps & GTK_PRINT_CAPABILITY_COLLATE));
1471   gtk_widget_set_sensitive (priv->reverse_check,
1472                             caps & GTK_PRINT_CAPABILITY_REVERSE);
1473   gtk_widget_set_sensitive (priv->scale_spin,
1474                             caps & GTK_PRINT_CAPABILITY_SCALE);
1475   gtk_widget_set_sensitive (GTK_WIDGET (priv->pages_per_sheet),
1476                             caps & GTK_PRINT_CAPABILITY_NUMBER_UP);
1477
1478   if (caps & GTK_PRINT_CAPABILITY_PREVIEW)
1479     gtk_widget_show (priv->preview_button);
1480   else
1481     gtk_widget_hide (priv->preview_button);
1482
1483   update_collate_icon (NULL, dialog);
1484
1485   gtk_tree_model_filter_refilter (priv->printer_list_filter);
1486 }
1487
1488 static gboolean
1489 page_setup_is_equal (GtkPageSetup *a, 
1490                      GtkPageSetup *b)
1491 {
1492   return
1493     gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
1494                              gtk_page_setup_get_paper_size (b)) &&
1495     gtk_page_setup_get_top_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_top_margin (b, GTK_UNIT_MM) &&
1496     gtk_page_setup_get_bottom_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_bottom_margin (b, GTK_UNIT_MM) &&
1497     gtk_page_setup_get_left_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_left_margin (b, GTK_UNIT_MM) &&
1498     gtk_page_setup_get_right_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_right_margin (b, GTK_UNIT_MM);
1499 }
1500
1501 static gboolean
1502 page_setup_is_same_size (GtkPageSetup *a,
1503                          GtkPageSetup *b)
1504 {
1505   return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
1506                                   gtk_page_setup_get_paper_size (b));
1507 }
1508
1509 static gboolean
1510 set_paper_size (GtkPrintUnixDialog *dialog,
1511                 GtkPageSetup       *page_setup,
1512                 gboolean            size_only,
1513                 gboolean            add_item)
1514 {
1515   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1516   GtkTreeModel *model;
1517   GtkTreeIter iter;
1518   GtkPageSetup *list_page_setup;
1519
1520   if (!priv->internal_page_setup_change)
1521     return TRUE;
1522
1523   if (page_setup == NULL)
1524     return FALSE;
1525
1526   model = GTK_TREE_MODEL (priv->page_setup_list);
1527
1528   if (gtk_tree_model_get_iter_first (model, &iter))
1529     {
1530       do
1531         {
1532           gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
1533                               PAGE_SETUP_LIST_COL_PAGE_SETUP, &list_page_setup, -1);
1534           if (list_page_setup == NULL)
1535             continue;
1536           
1537           if ((size_only && page_setup_is_same_size (page_setup, list_page_setup)) ||
1538               (!size_only && page_setup_is_equal (page_setup, list_page_setup)))
1539             {
1540               gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
1541                                              &iter);
1542               gtk_combo_box_set_active (GTK_COMBO_BOX (priv->orientation_combo),
1543                                         gtk_page_setup_get_orientation (page_setup));
1544               g_object_unref (list_page_setup);
1545               return TRUE;
1546             }
1547               
1548           g_object_unref (list_page_setup);
1549           
1550         } while (gtk_tree_model_iter_next (model, &iter));
1551     }
1552
1553   if (add_item)
1554     {
1555       gtk_list_store_append (priv->page_setup_list, &iter);
1556       gtk_list_store_set (priv->page_setup_list, &iter,
1557                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
1558                           -1);
1559       gtk_list_store_append (priv->page_setup_list, &iter);
1560       gtk_list_store_set (priv->page_setup_list, &iter,
1561                           PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
1562                           -1);
1563       gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
1564                                      &iter);
1565       gtk_combo_box_set_active (GTK_COMBO_BOX (priv->orientation_combo),
1566                                 gtk_page_setup_get_orientation (page_setup));
1567       return TRUE;
1568     }
1569
1570   return FALSE;
1571 }
1572
1573 static void
1574 fill_custom_paper_sizes (GtkPrintUnixDialog *dialog)
1575 {
1576   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1577   GtkTreeIter iter, paper_iter;
1578   GtkTreeModel *model;
1579
1580   model = GTK_TREE_MODEL (priv->custom_paper_list);
1581   if (gtk_tree_model_get_iter_first (model, &iter))
1582     {
1583       gtk_list_store_append (priv->page_setup_list, &paper_iter);
1584       gtk_list_store_set (priv->page_setup_list, &paper_iter,
1585                           PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
1586                           -1);
1587       do
1588         {
1589           GtkPageSetup *page_setup;
1590           gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
1591
1592           gtk_list_store_append (priv->page_setup_list, &paper_iter);
1593           gtk_list_store_set (priv->page_setup_list, &paper_iter,
1594                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
1595                               -1);
1596
1597           g_object_unref (page_setup);
1598         } while (gtk_tree_model_iter_next (model, &iter));
1599     }
1600   
1601   gtk_list_store_append (priv->page_setup_list, &paper_iter);
1602   gtk_list_store_set (priv->page_setup_list, &paper_iter,
1603                       PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
1604                       -1);
1605   gtk_list_store_append (priv->page_setup_list, &paper_iter);
1606   gtk_list_store_set (priv->page_setup_list, &paper_iter,
1607                       PAGE_SETUP_LIST_COL_PAGE_SETUP, NULL,
1608                       -1);
1609 }
1610
1611 static void
1612 fill_paper_sizes (GtkPrintUnixDialog *dialog,
1613                   GtkPrinter         *printer)
1614 {
1615   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1616   GList *list, *l;
1617   GtkPageSetup *page_setup;
1618   GtkPaperSize *paper_size;
1619   GtkTreeIter iter;
1620   gint i;
1621
1622   gtk_list_store_clear (priv->page_setup_list);
1623
1624   if (printer == NULL || (list = gtk_printer_list_papers (printer)) == NULL)
1625     {
1626       for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
1627         {
1628           page_setup = gtk_page_setup_new ();
1629           paper_size = gtk_paper_size_new (common_paper_sizes[i]);
1630           gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
1631           gtk_paper_size_free (paper_size);
1632
1633           gtk_list_store_append (priv->page_setup_list, &iter);
1634           gtk_list_store_set (priv->page_setup_list, &iter,
1635                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
1636                               -1);
1637           g_object_unref (page_setup);
1638         }
1639     }
1640   else
1641     {
1642       for (l = list; l != NULL; l = l->next)
1643         {
1644           page_setup = l->data;
1645           gtk_list_store_append (priv->page_setup_list, &iter);
1646           gtk_list_store_set (priv->page_setup_list, &iter,
1647                               PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
1648                               -1);
1649           g_object_unref (page_setup);
1650         }
1651       g_list_free (list);
1652     }
1653
1654   fill_custom_paper_sizes (dialog);
1655 }
1656
1657 static void
1658 update_paper_sizes (GtkPrintUnixDialog *dialog)
1659 {
1660   GtkPageSetup *current_page_setup = NULL;
1661   GtkPrinter   *printer;
1662
1663   printer = gtk_print_unix_dialog_get_selected_printer (dialog);
1664
1665   fill_paper_sizes (dialog, printer);
1666
1667   current_page_setup = gtk_page_setup_copy (gtk_print_unix_dialog_get_page_setup (dialog));
1668
1669   if (current_page_setup)
1670     {
1671       if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
1672         set_paper_size (dialog, current_page_setup, TRUE, TRUE);
1673
1674       g_object_unref (current_page_setup);
1675     }
1676 }
1677
1678 static void
1679 mark_conflicts (GtkPrintUnixDialog *dialog)
1680 {
1681   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1682   GtkPrinter *printer;
1683   gboolean have_conflict;
1684
1685   have_conflict = FALSE;
1686
1687   printer = priv->current_printer;
1688
1689   if (printer)
1690     {
1691
1692       g_signal_handler_block (priv->options,
1693                               priv->options_changed_handler);
1694
1695       gtk_printer_option_set_clear_conflicts (priv->options);
1696
1697       have_conflict = _gtk_printer_mark_conflicts (printer,
1698                                                    priv->options);
1699
1700       g_signal_handler_unblock (priv->options,
1701                                 priv->options_changed_handler);
1702     }
1703
1704   if (have_conflict)
1705     gtk_widget_show (priv->conflicts_widget);
1706   else
1707     gtk_widget_hide (priv->conflicts_widget);
1708 }
1709
1710 static gboolean
1711 mark_conflicts_callback (gpointer data)
1712 {
1713   GtkPrintUnixDialog *dialog = data;
1714   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1715
1716   priv->mark_conflicts_id = 0;
1717
1718   mark_conflicts (dialog);
1719
1720   return FALSE;
1721 }
1722
1723 static void
1724 unschedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
1725 {
1726   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1727
1728   if (priv->mark_conflicts_id != 0)
1729     {
1730       g_source_remove (priv->mark_conflicts_id);
1731       priv->mark_conflicts_id = 0;
1732     }
1733 }
1734
1735 static void
1736 schedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
1737 {
1738   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1739
1740   if (priv->mark_conflicts_id != 0)
1741     return;
1742
1743   priv->mark_conflicts_id = gdk_threads_add_idle (mark_conflicts_callback,
1744                                         dialog);
1745 }
1746
1747 static void
1748 options_changed_cb (GtkPrintUnixDialog *dialog)
1749 {
1750   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1751
1752   schedule_idle_mark_conflicts (dialog);
1753
1754   g_free (priv->waiting_for_printer);
1755   priv->waiting_for_printer = NULL;
1756 }
1757
1758 static void
1759 remove_custom_widget (GtkWidget    *widget,
1760                       GtkContainer *container)
1761 {
1762   gtk_container_remove (container, widget);
1763 }
1764
1765 static void
1766 extension_point_clear_children (GtkContainer *container)
1767 {
1768   gtk_container_foreach (container,
1769                          (GtkCallback)remove_custom_widget,
1770                          container);
1771 }
1772
1773 static void
1774 clear_per_printer_ui (GtkPrintUnixDialog *dialog)
1775 {
1776   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1777
1778   gtk_container_foreach (GTK_CONTAINER (priv->finishing_table),
1779                          (GtkCallback)gtk_widget_destroy,
1780                          NULL);
1781   gtk_table_resize (GTK_TABLE (priv->finishing_table), 1, 2);
1782   gtk_container_foreach (GTK_CONTAINER (priv->image_quality_table),
1783                          (GtkCallback)gtk_widget_destroy,
1784                          NULL);
1785   gtk_table_resize (GTK_TABLE (priv->image_quality_table), 1, 2);
1786   gtk_container_foreach (GTK_CONTAINER (priv->color_table),
1787                          (GtkCallback)gtk_widget_destroy,
1788                          NULL);
1789   gtk_table_resize (GTK_TABLE (priv->color_table), 1, 2);
1790   gtk_container_foreach (GTK_CONTAINER (priv->advanced_vbox),
1791                          (GtkCallback)gtk_widget_destroy,
1792                          NULL);
1793   extension_point_clear_children (GTK_CONTAINER (priv->extension_point));
1794 }
1795
1796 static void
1797 printer_details_acquired (GtkPrinter         *printer,
1798                           gboolean            success,
1799                           GtkPrintUnixDialog *dialog)
1800 {
1801   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1802
1803   disconnect_printer_details_request (dialog, !success);
1804
1805   if (success)
1806     {
1807       GtkTreeSelection *selection;
1808       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
1809
1810       selected_printer_changed (selection, dialog);
1811     }
1812 }
1813
1814 static void
1815 selected_printer_changed (GtkTreeSelection   *selection,
1816                           GtkPrintUnixDialog *dialog)
1817 {
1818   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1819   GtkPrinter *printer;
1820   GtkTreeIter iter, filter_iter;
1821
1822   /* Whenever the user selects a printer we stop looking for
1823      the printer specified in the initial settings */
1824   if (priv->waiting_for_printer &&
1825       !priv->internal_printer_change)
1826     {
1827       g_free (priv->waiting_for_printer);
1828       priv->waiting_for_printer = NULL;
1829     }
1830
1831   disconnect_printer_details_request (dialog, FALSE);
1832
1833   printer = NULL;
1834   if (gtk_tree_selection_get_selected (selection, NULL, &filter_iter))
1835     {
1836       gtk_tree_model_filter_convert_iter_to_child_iter (priv->printer_list_filter,
1837                                                         &iter,
1838                                                         &filter_iter);
1839
1840       gtk_tree_model_get (priv->printer_list, &iter,
1841                           PRINTER_LIST_COL_PRINTER_OBJ, &printer,
1842                           -1);
1843     }
1844
1845   /* sets GTK_RESPONSE_OK button sensitivity depending on whether the printer
1846    * accepts/rejects jobs
1847    */
1848   if (printer != NULL)
1849     {
1850       if (!gtk_printer_is_accepting_jobs (printer))
1851         {
1852           gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1853         }
1854       else
1855         {
1856           if (priv->current_printer == printer && gtk_printer_has_details (printer))
1857             gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
1858         }
1859     }
1860
1861   if (printer != NULL && !gtk_printer_has_details (printer))
1862     {
1863       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1864       priv->request_details_tag =
1865         g_signal_connect (printer, "details-acquired",
1866                           G_CALLBACK (printer_details_acquired), dialog);
1867       /* take the reference */
1868       priv->request_details_printer = printer;
1869       gtk_printer_request_details (printer);
1870       set_busy_cursor (dialog, TRUE);
1871       gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
1872                           g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter"),
1873                           PRINTER_LIST_COL_STATE, _("Getting printer information..."),
1874                           -1);
1875       return;
1876     }
1877
1878   if (printer == priv->current_printer)
1879     {
1880       if (printer)
1881         g_object_unref (printer);
1882       return;
1883     }
1884
1885   if (priv->options)
1886     {
1887       g_object_unref (priv->options);
1888       priv->options = NULL;
1889
1890       clear_per_printer_ui (dialog);
1891     }
1892
1893   if (priv->current_printer)
1894     {
1895       g_object_unref (priv->current_printer);
1896     }
1897
1898   priv->printer_capabilities = 0;
1899
1900   if (gtk_printer_is_accepting_jobs (printer))
1901     gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
1902   priv->current_printer = printer;
1903
1904   if (printer != NULL)
1905     {
1906       if (!priv->page_setup_set)
1907         {
1908           /* if no explicit page setup has been set, use the printer default */
1909           GtkPageSetup *page_setup;
1910
1911           page_setup = gtk_printer_get_default_page_size (printer);
1912
1913           if (!page_setup)
1914             page_setup = gtk_page_setup_new ();
1915
1916           if (page_setup && priv->page_setup)
1917             gtk_page_setup_set_orientation (page_setup, gtk_page_setup_get_orientation (priv->page_setup));
1918
1919           g_object_unref (priv->page_setup);
1920           priv->page_setup = page_setup;
1921         }
1922
1923       priv->printer_capabilities = gtk_printer_get_capabilities (printer);
1924       priv->options = _gtk_printer_get_options (printer,
1925                                                 priv->initial_settings,
1926                                                 priv->page_setup,
1927                                                 priv->manual_capabilities);
1928
1929       priv->options_changed_handler =
1930         g_signal_connect_swapped (priv->options, "changed", G_CALLBACK (options_changed_cb), dialog);
1931     }
1932
1933   update_dialog_from_settings (dialog);
1934   update_dialog_from_capabilities (dialog);
1935
1936   priv->internal_page_setup_change = TRUE;
1937   update_paper_sizes (dialog);
1938   priv->internal_page_setup_change = FALSE;
1939
1940   g_object_notify ( G_OBJECT(dialog), "selected-printer");
1941 }
1942
1943 static void
1944 update_collate_icon (GtkToggleButton    *toggle_button,
1945                      GtkPrintUnixDialog *dialog)
1946 {
1947   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1948
1949   gtk_widget_queue_draw (priv->collate_image);
1950 }
1951
1952 static void
1953 paint_page (GtkWidget *widget,
1954             cairo_t   *cr,
1955             gfloat     scale,
1956             gint       x_offset,
1957             gint       y_offset,
1958             gchar     *text,
1959             gint       text_x)
1960 {
1961   GtkStyle *style;
1962   gint x, y, width, height;
1963   gint text_y, linewidth;
1964
1965   x = x_offset * scale;
1966   y = y_offset * scale;
1967   width = 20 * scale;
1968   height = 26 * scale;
1969
1970   linewidth = 2;
1971   text_y = 21;
1972
1973   style = gtk_widget_get_style (widget);
1974
1975   gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_NORMAL]);
1976   cairo_rectangle (cr, x, y, width, height);
1977   cairo_fill (cr);
1978
1979   gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_NORMAL]);
1980   cairo_set_line_width (cr, linewidth);
1981   cairo_rectangle (cr, x + linewidth/2.0, y + linewidth/2.0, width - linewidth, height - linewidth);
1982   cairo_stroke (cr);
1983
1984   cairo_select_font_face (cr, "Sans",
1985                           CAIRO_FONT_SLANT_NORMAL,
1986                           CAIRO_FONT_WEIGHT_NORMAL);
1987   cairo_set_font_size (cr, (gint)(9 * scale));
1988   cairo_move_to (cr, x + (gint)(text_x * scale), y + (gint)(text_y * scale));
1989   cairo_show_text (cr, text);
1990 }
1991
1992 static gboolean
1993 draw_collate_cb (GtkWidget          *widget,
1994                  GdkEventExpose     *event,
1995                  GtkPrintUnixDialog *dialog)
1996 {
1997   GtkAllocation allocation;
1998   GtkSettings *settings;
1999   cairo_t *cr;
2000   gint size;
2001   gfloat scale;
2002   gboolean collate, reverse, rtl;
2003   gint copies;
2004   gint text_x;
2005
2006   collate = dialog_get_collate (dialog);
2007   reverse = dialog_get_reverse (dialog);
2008   copies = dialog_get_n_copies (dialog);
2009
2010   rtl = (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL);
2011
2012   settings = gtk_widget_get_settings (widget);
2013   gtk_icon_size_lookup_for_settings (settings,
2014                                      GTK_ICON_SIZE_DIALOG,
2015                                      &size,
2016                                      NULL);
2017   scale = size / 48.0;
2018   text_x = rtl ? 4 : 11;
2019
2020   cr = gdk_cairo_create (gtk_widget_get_window (widget));
2021
2022   gtk_widget_get_allocation (widget, &allocation);
2023   cairo_translate (cr, allocation.x, allocation.y);
2024   gtk_widget_set_allocation (widget, &allocation);
2025
2026   if (copies == 1)
2027     {
2028       paint_page (widget, cr, scale, rtl ? 40: 15, 5, reverse ? "1" : "2", text_x);
2029       paint_page (widget, cr, scale, rtl ? 50: 5, 15, reverse ? "2" : "1", text_x);
2030     }
2031   else
2032     {
2033       paint_page (widget, cr, scale, rtl ? 40: 15, 5, collate == reverse ? "1" : "2", text_x);
2034       paint_page (widget, cr, scale, rtl ? 50: 5, 15, reverse ? "2" : "1", text_x);
2035
2036       paint_page (widget, cr, scale, rtl ? 5 : 50, 5, reverse ? "1" : "2", text_x);
2037       paint_page (widget, cr, scale, rtl ? 15 : 40, 15, collate == reverse ? "2" : "1", text_x);
2038     }
2039
2040   cairo_destroy (cr);
2041
2042   return TRUE;
2043 }
2044
2045 static void
2046 gtk_print_unix_dialog_style_set (GtkWidget *widget,
2047                                  GtkStyle  *previous_style)
2048 {
2049   GTK_WIDGET_CLASS (gtk_print_unix_dialog_parent_class)->style_set (widget, previous_style);
2050
2051   if (gtk_widget_has_screen (widget))
2052     {
2053       GtkPrintUnixDialog *dialog = (GtkPrintUnixDialog *)widget;
2054       GtkPrintUnixDialogPrivate *priv = dialog->priv;
2055       GtkSettings *settings;
2056       gint size;
2057       gfloat scale;
2058
2059       settings = gtk_widget_get_settings (widget);
2060       gtk_icon_size_lookup_for_settings (settings,
2061                                          GTK_ICON_SIZE_DIALOG,
2062                                          &size,
2063                                          NULL);
2064       scale = size / 48.0;
2065
2066       gtk_widget_set_size_request (priv->collate_image,
2067                                    (50 + 20) * scale,
2068                                    (15 + 26) * scale);
2069     }
2070 }
2071
2072 static void
2073 update_entry_sensitivity (GtkWidget *button,
2074                           GtkWidget *range)
2075 {
2076   gboolean active;
2077
2078   active = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button));
2079
2080   gtk_widget_set_sensitive (range, active);
2081
2082   if (active)
2083     gtk_widget_grab_focus (range);
2084 }
2085
2086 static void
2087 emit_ok_response (GtkTreeView       *tree_view,
2088                   GtkTreePath       *path,
2089                   GtkTreeViewColumn *column,
2090                   gpointer          *user_data)
2091 {
2092   GtkPrintUnixDialog *print_dialog;
2093
2094   print_dialog = (GtkPrintUnixDialog *) user_data;
2095
2096   gtk_dialog_response (GTK_DIALOG (print_dialog), GTK_RESPONSE_OK);
2097 }
2098
2099 static void
2100 create_main_page (GtkPrintUnixDialog *dialog)
2101 {
2102   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2103   GtkWidget *main_vbox, *label, *vbox, *hbox;
2104   GtkWidget *scrolled, *treeview, *frame, *table;
2105   GtkWidget *entry, *spinbutton;
2106   GtkWidget *radio, *check, *image;
2107   GtkCellRenderer *renderer;
2108   GtkTreeViewColumn *column;
2109   GtkTreeSelection *selection;
2110   GtkWidget *custom_input;
2111   const gchar *range_tooltip;
2112
2113   main_vbox = gtk_vbox_new (FALSE, 18);
2114   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
2115   gtk_widget_show (main_vbox);
2116
2117   vbox = gtk_vbox_new (FALSE, 6);
2118   gtk_box_pack_start (GTK_BOX (main_vbox), vbox, TRUE, TRUE, 0);
2119   gtk_widget_show (vbox);
2120
2121   scrolled = gtk_scrolled_window_new (NULL, NULL);
2122   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
2123                                   GTK_POLICY_AUTOMATIC,
2124                                   GTK_POLICY_AUTOMATIC);
2125   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
2126                                        GTK_SHADOW_IN);
2127   gtk_widget_show (scrolled);
2128   gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 0);
2129
2130   treeview = gtk_tree_view_new_with_model ((GtkTreeModel *) priv->printer_list_filter);
2131   priv->printer_treeview = treeview;
2132   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);
2133   gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview), PRINTER_LIST_COL_NAME);
2134   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (treeview), TRUE);
2135   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
2136   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
2137   g_signal_connect (selection, "changed", G_CALLBACK (selected_printer_changed), dialog);
2138
2139   renderer = gtk_cell_renderer_pixbuf_new ();
2140   column = gtk_tree_view_column_new_with_attributes ("",
2141                                                      renderer,
2142                                                      "icon-name",
2143                                                      PRINTER_LIST_COL_ICON,
2144                                                      NULL);
2145   gtk_tree_view_column_set_cell_data_func (column, renderer, set_cell_sensitivity_func, NULL, NULL);
2146   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
2147
2148   renderer = gtk_cell_renderer_text_new ();
2149   column = gtk_tree_view_column_new_with_attributes (_("Printer"),
2150                                                      renderer,
2151                                                      "text",
2152                                                      PRINTER_LIST_COL_NAME,
2153                                                      NULL);
2154   gtk_tree_view_column_set_cell_data_func (column, renderer, set_cell_sensitivity_func, NULL, NULL);
2155   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
2156
2157   renderer = gtk_cell_renderer_text_new ();
2158   /* Translators: this is the header for the location column in the print dialog */
2159   column = gtk_tree_view_column_new_with_attributes (_("Location"),
2160                                                      renderer,
2161                                                      "text",
2162                                                      PRINTER_LIST_COL_LOCATION,
2163                                                      NULL);
2164   gtk_tree_view_column_set_cell_data_func (column, renderer, set_cell_sensitivity_func, NULL, NULL);
2165   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
2166
2167   renderer = gtk_cell_renderer_text_new ();
2168   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
2169   /* Translators: this is the header for the printer status column in the print dialog */
2170   column = gtk_tree_view_column_new_with_attributes (_("Status"),
2171                                                      renderer,
2172                                                      "text",
2173                                                      PRINTER_LIST_COL_STATE,
2174                                                      NULL);
2175   gtk_tree_view_column_set_cell_data_func (column, renderer, set_cell_sensitivity_func, NULL, NULL);
2176   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
2177
2178   g_signal_connect (GTK_TREE_VIEW (treeview), "row-activated", G_CALLBACK (emit_ok_response), dialog);
2179
2180   gtk_widget_show (treeview);
2181   gtk_container_add (GTK_CONTAINER (scrolled), treeview);
2182
2183   custom_input = gtk_hbox_new (FALSE, 18);
2184   gtk_widget_show (custom_input);
2185   gtk_box_pack_start (GTK_BOX (vbox), custom_input, FALSE, FALSE, 0);
2186   priv->extension_point = custom_input;
2187
2188   hbox = gtk_hbox_new (FALSE, 18);
2189   gtk_widget_show (hbox);
2190   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
2191
2192   table = gtk_table_new (4, 2, FALSE);
2193   priv->range_table = table;
2194   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2195   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2196   frame = wrap_in_frame (_("Range"), table);
2197   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
2198   gtk_widget_show (table);
2199
2200   radio = gtk_radio_button_new_with_mnemonic (NULL, _("_All Pages"));
2201   priv->all_pages_radio = radio;
2202   gtk_widget_show (radio);
2203   gtk_table_attach (GTK_TABLE (table), radio,
2204                     0, 2, 0, 1,  GTK_FILL, 0,
2205                     0, 0);
2206   radio = gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
2207                                               _("C_urrent Page"));
2208   if (priv->current_page == -1)
2209     gtk_widget_set_sensitive (radio, FALSE);
2210   priv->current_page_radio = radio;
2211   gtk_widget_show (radio);
2212   gtk_table_attach (GTK_TABLE (table), radio,
2213                     0, 2, 1, 2,  GTK_FILL, 0,
2214                     0, 0);
2215
2216   radio = gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
2217                                               _("Se_lection"));
2218
2219   gtk_widget_set_sensitive (radio, priv->has_selection);
2220   priv->selection_radio = radio;
2221   gtk_table_attach (GTK_TABLE (table), radio,
2222                     0, 2, 2, 3,  GTK_FILL, 0,
2223                     0, 0);
2224   gtk_table_set_row_spacing (GTK_TABLE (table), 2, 0);
2225
2226   radio = gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)), _("Pag_es:"));
2227   range_tooltip = _("Specify one or more page ranges,\n e.g. 1-3,7,11");
2228   gtk_widget_set_tooltip_text (radio, range_tooltip);
2229
2230   priv->page_range_radio = radio;
2231   gtk_widget_show (radio);
2232   gtk_table_attach (GTK_TABLE (table), radio,
2233                     0, 1, 3, 4,  GTK_FILL, 0,
2234                     0, 0);
2235   entry = gtk_entry_new ();
2236   gtk_widget_set_tooltip_text (entry, range_tooltip);
2237   atk_object_set_name (gtk_widget_get_accessible (entry), _("Pages"));
2238   atk_object_set_description (gtk_widget_get_accessible (entry), range_tooltip);
2239   priv->page_range_entry = entry;
2240   gtk_widget_show (entry);
2241   gtk_table_attach (GTK_TABLE (table), entry,
2242                     1, 2, 3, 4,  GTK_FILL, 0,
2243                     0, 0);
2244   g_signal_connect (radio, "toggled", G_CALLBACK (update_entry_sensitivity), entry);
2245   update_entry_sensitivity (radio, entry);
2246
2247   table = gtk_table_new (3, 2, FALSE);
2248   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2249   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2250   frame = wrap_in_frame (_("Copies"), table);
2251   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
2252   gtk_widget_show (table);
2253
2254   /* FIXME chpe: too much space between Copies and spinbutton, put those 2 in a hbox and make it span 2 columns */
2255   label = gtk_label_new_with_mnemonic (_("Copie_s:"));
2256   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2257   gtk_widget_show (label);
2258   gtk_table_attach (GTK_TABLE (table), label,
2259                     0, 1, 0, 1,  GTK_FILL, 0,
2260                     0, 0);
2261   spinbutton = gtk_spin_button_new_with_range (1.0, 100.0, 1.0);
2262   priv->copies_spin = spinbutton;
2263   gtk_widget_show (spinbutton);
2264   gtk_table_attach (GTK_TABLE (table), spinbutton,
2265                     1, 2, 0, 1,  GTK_FILL, 0,
2266                     0, 0);
2267   gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);
2268   g_signal_connect_swapped (spinbutton, "value-changed",
2269                             G_CALLBACK (update_dialog_from_capabilities), dialog);
2270   g_signal_connect_swapped (spinbutton, "changed",
2271                             G_CALLBACK (update_dialog_from_capabilities), dialog);
2272
2273   check = gtk_check_button_new_with_mnemonic (_("C_ollate"));
2274   priv->collate_check = check;
2275   g_signal_connect (check, "toggled", G_CALLBACK (update_collate_icon), dialog);
2276   gtk_widget_show (check);
2277   gtk_table_attach (GTK_TABLE (table), check,
2278                     0, 1, 1, 2,  GTK_FILL, 0,
2279                     0, 0);
2280
2281   check = gtk_check_button_new_with_mnemonic (_("_Reverse"));
2282   g_signal_connect (check, "toggled", G_CALLBACK (update_collate_icon), dialog);
2283   priv->reverse_check = check;
2284   gtk_widget_show (check);
2285   gtk_table_attach (GTK_TABLE (table), check,
2286                     0, 1, 2, 3,  GTK_FILL, 0,
2287                     0, 0);
2288
2289   image = gtk_drawing_area_new ();
2290   gtk_widget_set_has_window (image, FALSE);
2291
2292   priv->collate_image = image;
2293   gtk_widget_show (image);
2294   gtk_widget_set_size_request (image, 70, 90);
2295   gtk_table_attach (GTK_TABLE (table), image,
2296                     1, 2, 1, 3, GTK_FILL, 0,
2297                     0, 0);
2298   g_signal_connect (image, "expose-event",
2299                     G_CALLBACK (draw_collate_cb), dialog);
2300
2301   label = gtk_label_new (_("General"));
2302   gtk_widget_show (label);
2303
2304   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook), main_vbox, label);
2305 }
2306
2307 static gboolean
2308 is_range_separator (gchar c)
2309 {
2310   return (c == ',' || c == ';' || c == ':');
2311 }
2312
2313 static GtkPageRange *
2314 dialog_get_page_ranges (GtkPrintUnixDialog *dialog,
2315                         gint               *n_ranges_out)
2316 {
2317   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2318   gint i, n_ranges;
2319   const gchar *text, *p;
2320   gchar *next;
2321   GtkPageRange *ranges;
2322   gint start, end;
2323
2324   text = gtk_entry_get_text (GTK_ENTRY (priv->page_range_entry));
2325
2326   if (*text == 0)
2327     {
2328       *n_ranges_out = 0;
2329       return NULL;
2330     }
2331
2332   n_ranges = 1;
2333   p = text;
2334   while (*p)
2335     {
2336       if (is_range_separator (*p))
2337         n_ranges++;
2338       p++;
2339     }
2340
2341   ranges = g_new0 (GtkPageRange, n_ranges);
2342
2343   i = 0;
2344   p = text;
2345   while (*p)
2346     {
2347       while (isspace (*p)) p++;
2348
2349       if (*p == '-')
2350         {
2351           /* a half-open range like -2 */
2352           start = 1;
2353         }
2354       else
2355         {
2356           start = (int)strtol (p, &next, 10);
2357           if (start < 1)
2358             start = 1;
2359           p = next;
2360         }
2361
2362       end = start;
2363
2364       while (isspace (*p)) p++;
2365
2366       if (*p == '-')
2367         {
2368           p++;
2369           end = (int)strtol (p, &next, 10);
2370           if (next == p) /* a half-open range like 2- */
2371             end = 0;
2372           else if (end < start)
2373             end = start;
2374         }
2375
2376       ranges[i].start = start - 1;
2377       ranges[i].end = end - 1;
2378       i++;
2379
2380       /* Skip until end or separator */
2381       while (*p && !is_range_separator (*p))
2382         p++;
2383
2384       /* if not at end, skip separator */
2385       if (*p)
2386         p++;
2387     }
2388
2389   *n_ranges_out = i;
2390
2391   return ranges;
2392 }
2393
2394 static void
2395 dialog_set_page_ranges (GtkPrintUnixDialog *dialog,
2396                         GtkPageRange       *ranges,
2397                         gint                n_ranges)
2398 {
2399   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2400   gint i;
2401   GString *s = g_string_new (NULL);
2402
2403   for (i = 0; i < n_ranges; i++)
2404     {
2405       g_string_append_printf (s, "%d", ranges[i].start + 1);
2406       if (ranges[i].end > ranges[i].start)
2407         g_string_append_printf (s, "-%d", ranges[i].end + 1);
2408       else if (ranges[i].end == -1)
2409         g_string_append (s, "-");
2410
2411       if (i != n_ranges - 1)
2412         g_string_append (s, ",");
2413     }
2414
2415   gtk_entry_set_text (GTK_ENTRY (priv->page_range_entry), s->str);
2416
2417   g_string_free (s, TRUE);
2418 }
2419
2420 static GtkPrintPages
2421 dialog_get_print_pages (GtkPrintUnixDialog *dialog)
2422 {
2423   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2424
2425   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->all_pages_radio)))
2426     return GTK_PRINT_PAGES_ALL;
2427   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->current_page_radio)))
2428     return GTK_PRINT_PAGES_CURRENT;
2429   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->selection_radio)))
2430     return GTK_PRINT_PAGES_SELECTION;
2431   else
2432     return GTK_PRINT_PAGES_RANGES;
2433 }
2434
2435 static void
2436 dialog_set_print_pages (GtkPrintUnixDialog *dialog,
2437                         GtkPrintPages       pages)
2438 {
2439   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2440
2441   if (pages == GTK_PRINT_PAGES_RANGES)
2442     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->page_range_radio), TRUE);
2443   else if (pages == GTK_PRINT_PAGES_CURRENT)
2444     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->current_page_radio), TRUE);
2445   else if (pages == GTK_PRINT_PAGES_SELECTION)
2446     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->selection_radio), TRUE);
2447   else
2448     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->all_pages_radio), TRUE);
2449 }
2450
2451 static gdouble
2452 dialog_get_scale (GtkPrintUnixDialog *dialog)
2453 {
2454   if (gtk_widget_is_sensitive (dialog->priv->scale_spin))
2455     return gtk_spin_button_get_value (GTK_SPIN_BUTTON (dialog->priv->scale_spin));
2456   else
2457     return 100.0;
2458 }
2459
2460 static void
2461 dialog_set_scale (GtkPrintUnixDialog *dialog,
2462                   gdouble             val)
2463 {
2464   gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->scale_spin), val);
2465 }
2466
2467 static GtkPageSet
2468 dialog_get_page_set (GtkPrintUnixDialog *dialog)
2469 {
2470   if (gtk_widget_is_sensitive (dialog->priv->page_set_combo))
2471     return (GtkPageSet)gtk_combo_box_get_active (GTK_COMBO_BOX (dialog->priv->page_set_combo));
2472   else
2473     return GTK_PAGE_SET_ALL;
2474 }
2475
2476 static void
2477 dialog_set_page_set (GtkPrintUnixDialog *dialog,
2478                      GtkPageSet          val)
2479 {
2480   gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->priv->page_set_combo),
2481                             (int)val);
2482 }
2483
2484 static gint
2485 dialog_get_n_copies (GtkPrintUnixDialog *dialog)
2486 {
2487   if (gtk_widget_is_sensitive (dialog->priv->copies_spin))
2488     return gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (dialog->priv->copies_spin));
2489   return 1;
2490 }
2491
2492 static void
2493 dialog_set_n_copies (GtkPrintUnixDialog *dialog,
2494                      gint                n_copies)
2495 {
2496   gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->copies_spin),
2497                              n_copies);
2498 }
2499
2500 static gboolean
2501 dialog_get_collate (GtkPrintUnixDialog *dialog)
2502 {
2503   if (gtk_widget_is_sensitive (dialog->priv->collate_check))
2504     return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->collate_check));
2505   return FALSE;
2506 }
2507
2508 static void
2509 dialog_set_collate (GtkPrintUnixDialog *dialog,
2510                     gboolean            collate)
2511 {
2512   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->collate_check),
2513                                 collate);
2514 }
2515
2516 static gboolean
2517 dialog_get_reverse (GtkPrintUnixDialog *dialog)
2518 {
2519   if (gtk_widget_is_sensitive (dialog->priv->reverse_check))
2520     return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->reverse_check));
2521   return FALSE;
2522 }
2523
2524 static void
2525 dialog_set_reverse (GtkPrintUnixDialog *dialog,
2526                     gboolean            reverse)
2527 {
2528   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->reverse_check),
2529                                 reverse);
2530 }
2531
2532 static gint
2533 dialog_get_pages_per_sheet (GtkPrintUnixDialog *dialog)
2534 {
2535   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2536   const gchar *val;
2537   gint num;
2538
2539   val = gtk_printer_option_widget_get_value (priv->pages_per_sheet);
2540
2541   num = 1;
2542
2543   if (val)
2544     {
2545       num = atoi(val);
2546       if (num < 1)
2547         num = 1;
2548     }
2549
2550   return num;
2551 }
2552
2553 static GtkNumberUpLayout
2554 dialog_get_number_up_layout (GtkPrintUnixDialog *dialog)
2555 {
2556   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2557   GtkPrintCapabilities       caps;
2558   GtkNumberUpLayout          layout;
2559   const gchar               *val;
2560   GEnumClass                *enum_class;
2561   GEnumValue                *enum_value;
2562
2563   val = gtk_printer_option_widget_get_value (priv->number_up_layout);
2564
2565   caps = priv->manual_capabilities | priv->printer_capabilities;
2566
2567   if ((caps & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT) == 0)
2568     return GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2569
2570   if (gtk_widget_get_direction (GTK_WIDGET (dialog)) == GTK_TEXT_DIR_LTR)
2571     layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
2572   else
2573     layout = GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM;
2574
2575   if (val == NULL)
2576     return layout;
2577
2578   enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
2579   enum_value = g_enum_get_value_by_nick (enum_class, val);
2580   if (enum_value)
2581     layout = enum_value->value;
2582   g_type_class_unref (enum_class);
2583
2584   return layout;
2585 }
2586
2587 static gboolean
2588 draw_page_cb (GtkWidget          *widget,
2589               GdkEventExpose     *event,
2590               GtkPrintUnixDialog *dialog)
2591 {
2592   GtkPrintUnixDialogPrivate *priv = dialog->priv;
2593   GtkAllocation allocation;
2594   GtkStyle *style;
2595   cairo_t *cr;
2596   gdouble ratio;
2597   gint w, h, tmp, shadow_offset;
2598   gint pages_x, pages_y, i, x, y, layout_w, layout_h;
2599   gdouble page_width, page_height;
2600   GtkPageOrientation orientation;
2601   gboolean landscape;
2602   PangoLayout *layout;
2603   PangoFontDescription *font;
2604   gchar *text;
2605   GdkColor *color;
2606   GtkNumberUpLayout number_up_layout;
2607   gint start_x, end_x, start_y, end_y;
2608   gint dx, dy;
2609   gboolean horizontal;
2610   GtkPageSetup *page_setup;
2611   gdouble paper_width, paper_height;
2612   gdouble pos_x, pos_y;
2613   gint pages_per_sheet;
2614   gboolean ltr = TRUE;
2615
2616   orientation = gtk_page_setup_get_orientation (priv->page_setup);
2617   landscape =
2618     (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE) ||
2619     (orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE);
2620
2621   number_up_layout = dialog_get_number_up_layout (dialog);
2622
2623   cr = gdk_cairo_create (gtk_widget_get_window (widget));
2624
2625   cairo_save (cr);
2626
2627   page_setup = gtk_print_unix_dialog_get_page_setup (dialog);  
2628
2629   if (page_setup != NULL)
2630     {
2631       if (!landscape)
2632         {
2633           paper_width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
2634           paper_height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
2635         }
2636       else
2637         {
2638           paper_width = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM);
2639           paper_height = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM);
2640         }
2641
2642       if (paper_width < paper_height)
2643         {
2644           h = EXAMPLE_PAGE_AREA_SIZE - 3;
2645           w = (paper_height != 0) ? h * paper_width / paper_height : 0;
2646         }
2647       else
2648         {
2649           w = EXAMPLE_PAGE_AREA_SIZE - 3;
2650           h = (paper_width != 0) ? w * paper_height / paper_width : 0;
2651         }
2652
2653       if (paper_width == 0)
2654         w = 0;
2655
2656       if (paper_height == 0)
2657         h = 0;
2658     }
2659   else
2660     {
2661       ratio = G_SQRT2;
2662       w = (EXAMPLE_PAGE_AREA_SIZE - 3) / ratio;
2663       h = EXAMPLE_PAGE_AREA_SIZE - 3;
2664     }
2665
2666   pages_per_sheet = dialog_get_pages_per_sheet (dialog);
2667   switch (pages_per_sheet)
2668     {
2669     default:
2670     case 1:
2671       pages_x = 1; pages_y = 1;
2672       break;
2673     case 2:
2674       landscape = !landscape;
2675       pages_x = 1; pages_y = 2;
2676       break;
2677     case 4:
2678       pages_x = 2; pages_y = 2;
2679       break;
2680     case 6:
2681       landscape = !landscape;
2682       pages_x = 2; pages_y = 3;
2683       break;
2684     case 9:
2685       pages_x = 3; pages_y = 3;
2686       break;
2687     case 16:
2688       pages_x = 4; pages_y = 4;
2689       break;
2690     }
2691
2692   if (landscape)
2693     {
2694       tmp = w;
2695       w = h;
2696       h = tmp;
2697
2698       tmp = pages_x;
2699       pages_x = pages_y;
2700       pages_y = tmp;
2701     }
2702
2703   style = gtk_widget_get_style (widget);
2704
2705   gtk_widget_get_allocation (widget, &allocation);
2706   pos_x = allocation.x + (allocation.width - w) / 2;
2707   pos_y = allocation.y + (allocation.height - h) / 2 - 10;
2708   cairo_translate (cr, pos_x, pos_y);
2709
2710   shadow_offset = 3;
2711
2712   color = &style->text[GTK_STATE_NORMAL];
2713   cairo_set_source_rgba (cr, color->red / 65535., color->green / 65535., color->blue / 65535, 0.5);
2714   cairo_rectangle (cr, shadow_offset + 1, shadow_offset + 1, w, h);
2715   cairo_fill (cr);
2716
2717   gdk_cairo_set_source_color (cr, &style->base[GTK_STATE_NORMAL]);
2718   cairo_rectangle (cr, 1, 1, w, h);
2719   cairo_fill (cr);
2720   cairo_set_line_width (cr, 1.0);
2721   cairo_rectangle (cr, 0.5, 0.5, w + 1, h + 1);
2722
2723   gdk_cairo_set_source_color (cr, &style->text[GTK_STATE_NORMAL]);
2724   cairo_stroke (cr);
2725
2726   i = 1;
2727
2728   page_width = (double)w / pages_x;
2729   page_height = (double)h / pages_y;
2730
2731   layout  = pango_cairo_create_layout (cr);
2732
2733   font = pango_font_description_new ();
2734   pango_font_description_set_family (font, "sans");
2735
2736   if (page_height > 0)
2737     pango_font_description_set_absolute_size (font, page_height * 0.4 * PANGO_SCALE);
2738   else
2739     pango_font_description_set_absolute_size (font, 1);
2740
2741   pango_layout_set_font_description (layout, font);
2742   pango_font_description_free (font);
2743
2744   pango_layout_set_width (layout, page_width * PANGO_SCALE);
2745   pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2746
2747   switch (number_up_layout)
2748     {
2749       default:
2750       case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
2751         start_x = 0;
2752         end_x = pages_x - 1;
2753         start_y = 0;
2754         end_y = pages_y - 1;
2755         dx = 1;
2756         dy = 1;
2757         horizontal = TRUE;
2758         break;
2759       case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
2760         start_x = 0;
2761         end_x = pages_x - 1;
2762         start_y = pages_y - 1;
2763         end_y = 0;
2764         dx = 1;
2765         dy = - 1;
2766         horizontal = TRUE;
2767         break;
2768       case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
2769         start_x = pages_x - 1;
2770         end_x = 0;
2771         start_y = 0;
2772         end_y = pages_y - 1;
2773         dx = - 1;
2774         dy = 1;
2775         horizontal = TRUE;
2776         break;
2777       case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
2778         start_x = pages_x - 1;
2779         end_x = 0;
2780         start_y = pages_y - 1;
2781         end_y = 0;
2782         dx = - 1;
2783         dy = - 1;
2784         horizontal = TRUE;
2785         break;
2786       case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
2787         start_x = 0;
2788         end_x = pages_x - 1;
2789         start_y = 0;
2790         end_y = pages_y - 1;
2791         dx = 1;
2792         dy = 1;
2793         horizontal = FALSE;
2794         break;
2795       case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
2796         start_x = pages_x - 1;
2797         end_x = 0;
2798         start_y = 0;
2799         end_y = pages_y - 1;
2800         dx = - 1;
2801         dy = 1;
2802         horizontal = FALSE;
2803         break;
2804       case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
2805         start_x = 0;
2806         end_x = pages_x - 1;
2807         start_y = pages_y - 1;
2808         end_y = 0;
2809         dx = 1;
2810         dy = - 1;
2811         horizontal = FALSE;
2812         break;
2813       case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
2814         start_x = pages_x - 1;
2815         end_x = 0;
2816         start_y = pages_y - 1;
2817         end_y = 0;
2818         dx = - 1;
2819         dy = - 1;
2820         horizontal = FALSE;
2821         break;
2822     }
2823
2824   if (horizontal)
2825     for (y = start_y; y != end_y + dy; y += dy)
2826       {
2827         for (x = start_x; x != end_x + dx; x += dx)
2828           {
2829             text = g_strdup_printf ("%d", i++);
2830             pango_layout_set_text (layout, text, -1);
2831             g_free (text);
2832             pango_layout_get_size (layout, &layout_w, &layout_h);
2833             cairo_save (cr);
2834             cairo_translate (cr,
2835                              x * page_width,
2836                              y * page_height + (page_height - layout_h / 1024.0) / 2);
2837
2838             pango_cairo_show_layout (cr, layout);
2839             cairo_restore (cr);
2840           }
2841       }
2842   else
2843     for (x = start_x; x != end_x + dx; x += dx)
2844       {
2845         for (y = start_y; y != end_y + dy; y += dy)
2846           {
2847             text = g_strdup_printf ("%d", i++);
2848             pango_layout_set_text (layout, text, -1);
2849             g_free (text);
2850             pango_layout_get_size (layout, &layout_w, &layout_h);
2851             cairo_save (cr);
2852             cairo_translate (cr,
2853                              x * page_width,
2854                              y * page_height + (page_height - layout_h / 1024.0) / 2);
2855
2856             pango_cairo_show_layout (cr, layout);
2857             cairo_restore (cr);
2858           }
2859       }
2860
2861   g_object_unref (layout);
2862
2863   if (page_setup != NULL)
2864     {
2865       pos_x += 1;
2866       pos_y += 1;
2867
2868       if (pages_per_sheet == 2 || pages_per_sheet == 6)
2869         {
2870           paper_width = gtk_page_setup_get_paper_height (page_setup, _gtk_print_get_default_user_units ());
2871           paper_height = gtk_page_setup_get_paper_width (page_setup, _gtk_print_get_default_user_units ());
2872         }
2873       else
2874         {
2875           paper_width = gtk_page_setup_get_paper_width (page_setup, _gtk_print_get_default_user_units ());
2876           paper_height = gtk_page_setup_get_paper_height (page_setup, _gtk_print_get_default_user_units ());
2877         }
2878
2879       cairo_restore (cr);
2880       cairo_save (cr);
2881
2882       layout  = pango_cairo_create_layout (cr);
2883
2884       font = pango_font_description_new ();
2885       pango_font_description_set_family (font, "sans");
2886
2887       PangoContext *pango_c = NULL;
2888       PangoFontDescription *pango_f = NULL;
2889       gint font_size = 12 * PANGO_SCALE;
2890
2891       pango_c = gtk_widget_get_pango_context (widget);
2892       if (pango_c != NULL)
2893         {
2894           pango_f = pango_context_get_font_description (pango_c);
2895           if (pango_f != NULL)
2896             font_size = pango_font_description_get_size (pango_f);
2897         }
2898
2899       pango_font_description_set_size (font, font_size);
2900       pango_layout_set_font_description (layout, font);
2901       pango_font_description_free (font);
2902
2903       pango_layout_set_width (layout, -1);
2904       pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
2905
2906       if (_gtk_print_get_default_user_units () == GTK_UNIT_MM)
2907         text = g_strdup_printf ("%.1f mm", paper_height);
2908       else
2909         text = g_strdup_printf ("%.2f inch", paper_height);
2910
2911       pango_layout_set_text (layout, text, -1);
2912       g_free (text);
2913       pango_layout_get_size (layout, &layout_w, &layout_h);
2914
2915       ltr = gtk_widget_get_direction (GTK_WIDGET (dialog)) == GTK_TEXT_DIR_LTR;
2916
2917       gtk_widget_get_allocation (widget, &allocation);
2918       if (ltr)
2919         cairo_translate (cr, pos_x - layout_w / PANGO_SCALE - 2 * RULER_DISTANCE,
2920                              allocation.y + (allocation.height - layout_h / PANGO_SCALE) / 2);
2921       else
2922         cairo_translate (cr, pos_x + w + shadow_offset + 2 * RULER_DISTANCE,
2923                              allocation.y + (allocation.height - layout_h / PANGO_SCALE) / 2);
2924
2925       pango_cairo_show_layout (cr, layout);
2926
2927       cairo_restore (cr);
2928       cairo_save (cr);
2929
2930       if (_gtk_print_get_default_user_units () == GTK_UNIT_MM)
2931         text = g_strdup_printf ("%.1f mm", paper_width);
2932       else
2933         text = g_strdup_printf ("%.2f inch", paper_width);
2934
2935       pango_layout_set_text (layout, text, -1);
2936       g_free (text);
2937       pango_layout_get_size (layout, &layout_w, &layout_h);
2938
2939       cairo_translate (cr, allocation.x + (allocation.width - layout_w / PANGO_SCALE) / 2,
2940                            pos_y + h + shadow_offset + 2 * RULER_DISTANCE);
2941
2942       pango_cairo_show_layout (cr, layout);
2943
2944       g_object_unref (layout);
2945
2946       cairo_restore (cr);
2947
2948       cairo_set_line_width (cr, 1);
2949
2950       if (ltr)
2951         {
2952           cairo_move_to (cr, pos_x - RULER_DISTANCE, pos_y);
2953           cairo_line_to (cr, pos_x - RULER_DISTANCE, pos_y + h);
2954           cairo_stroke (cr);
2955
2956           cairo_move_to (cr, pos_x - RULER_DISTANCE - RULER_RADIUS, pos_y - 0.5);
2957           cairo_line_to (cr, pos_x - RULER_DISTANCE + RULER_RADIUS, pos_y - 0.5);
2958           cairo_stroke (cr);
2959
2960           cairo_move_to (cr, pos_x - RULER_DISTANCE - RULER_RADIUS, pos_y + h + 0.5);
2961           cairo_line_to (cr, pos_x - RULER_DISTANCE + RULER_RADIUS, pos_y + h + 0.5);
2962           cairo_stroke (cr);
2963         }
2964       else
2965         {
2966           cairo_move_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE, pos_y);
2967           cairo_line_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE, pos_y + h);
2968           cairo_stroke (cr);
2969
2970           cairo_move_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE - RULER_RADIUS, pos_y - 0.5);
2971           cairo_line_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE + RULER_RADIUS, pos_y - 0.5);
2972           cairo_stroke (cr);
2973
2974           cairo_move_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE - RULER_RADIUS, pos_y + h + 0.5);
2975           cairo_line_to (cr, pos_x + w + shadow_offset + RULER_DISTANCE + RULER_RADIUS, pos_y + h + 0.5);
2976           cairo_stroke (cr);
2977         }
2978
2979       cairo_move_to (cr, pos_x, pos_y + h + shadow_offset + RULER_DISTANCE);
2980       cairo_line_to (cr, pos_x + w, pos_y + h + shadow_offset + RULER_DISTANCE);
2981       cairo_stroke (cr);
2982
2983       cairo_move_to (cr, pos_x - 0.5, pos_y + h + shadow_offset + RULER_DISTANCE - RULER_RADIUS);
2984       cairo_line_to (cr, pos_x - 0.5, pos_y + h + shadow_offset + RULER_DISTANCE + RULER_RADIUS);
2985       cairo_stroke (cr);
2986
2987       cairo_move_to (cr, pos_x + w + 0.5, pos_y + h + shadow_offset + RULER_DISTANCE - RULER_RADIUS);
2988       cairo_line_to (cr, pos_x + w + 0.5, pos_y + h + shadow_offset + RULER_DISTANCE + RULER_RADIUS);
2989       cairo_stroke (cr);
2990     }
2991
2992   cairo_destroy (cr);
2993
2994   return TRUE;
2995 }
2996
2997 static void
2998 redraw_page_layout_preview (GtkPrintUnixDialog *dialog)
2999 {
3000   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3001
3002   if (priv->page_layout_preview)
3003     gtk_widget_queue_draw (priv->page_layout_preview);
3004 }
3005
3006 static void
3007 update_number_up_layout (GtkPrintUnixDialog *dialog)
3008 {
3009   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3010   GtkPrintCapabilities       caps;
3011   GtkPrinterOptionSet       *set;
3012   GtkNumberUpLayout          layout;
3013   GtkPrinterOption          *option;
3014   GtkPrinterOption          *old_option;
3015   GtkPageOrientation         page_orientation;
3016
3017   set = priv->options;
3018
3019   caps = priv->manual_capabilities | priv->printer_capabilities;
3020
3021   if (caps & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT)
3022     {
3023       if (priv->number_up_layout_n_option == NULL)
3024         {
3025           priv->number_up_layout_n_option = gtk_printer_option_set_lookup (set, "gtk-n-up-layout");
3026           if (priv->number_up_layout_n_option == NULL)
3027             {
3028               char *n_up_layout[] = { "lrtb", "lrbt", "rltb", "rlbt", "tblr", "tbrl", "btlr", "btrl" };
3029                /* Translators: These strings name the possible arrangements of
3030                 * multiple pages on a sheet when printing (same as in gtkprintbackendcups.c)
3031                 */
3032               char *n_up_layout_display[] = { N_("Left to right, top to bottom"), N_("Left to right, bottom to top"),
3033                                               N_("Right to left, top to bottom"), N_("Right to left, bottom to top"),
3034                                               N_("Top to bottom, left to right"), N_("Top to bottom, right to left"),
3035                                               N_("Bottom to top, left to right"), N_("Bottom to top, right to left") };
3036               int i;
3037
3038               priv->number_up_layout_n_option = gtk_printer_option_new ("gtk-n-up-layout",
3039                                                                         _("Page Ordering"),
3040                                                                         GTK_PRINTER_OPTION_TYPE_PICKONE);
3041               gtk_printer_option_allocate_choices (priv->number_up_layout_n_option, 8);
3042
3043               for (i = 0; i < G_N_ELEMENTS (n_up_layout_display); i++)
3044                 {
3045                   priv->number_up_layout_n_option->choices[i] = g_strdup (n_up_layout[i]);
3046                   priv->number_up_layout_n_option->choices_display[i] = g_strdup (_(n_up_layout_display[i]));
3047                 }
3048             }
3049           g_object_ref (priv->number_up_layout_n_option);
3050
3051           priv->number_up_layout_2_option = gtk_printer_option_new ("gtk-n-up-layout",
3052                                                                     _("Page Ordering"),
3053                                                                     GTK_PRINTER_OPTION_TYPE_PICKONE);
3054           gtk_printer_option_allocate_choices (priv->number_up_layout_2_option, 2);
3055         }
3056
3057       page_orientation = gtk_page_setup_get_orientation (priv->page_setup);
3058       if (page_orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
3059           page_orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
3060         {
3061           if (! (priv->number_up_layout_2_option->choices[0] == priv->number_up_layout_n_option->choices[0] &&
3062                  priv->number_up_layout_2_option->choices[1] == priv->number_up_layout_n_option->choices[2]))
3063             {
3064               g_free (priv->number_up_layout_2_option->choices_display[0]);
3065               g_free (priv->number_up_layout_2_option->choices_display[1]);
3066               priv->number_up_layout_2_option->choices[0] = priv->number_up_layout_n_option->choices[0];
3067               priv->number_up_layout_2_option->choices[1] = priv->number_up_layout_n_option->choices[2];
3068               priv->number_up_layout_2_option->choices_display[0] = g_strdup ( _("Left to right"));
3069               priv->number_up_layout_2_option->choices_display[1] = g_strdup ( _("Right to left"));
3070             }
3071         }
3072       else
3073         {
3074           if (! (priv->number_up_layout_2_option->choices[0] == priv->number_up_layout_n_option->choices[0] &&
3075                  priv->number_up_layout_2_option->choices[1] == priv->number_up_layout_n_option->choices[1]))
3076             {
3077               g_free (priv->number_up_layout_2_option->choices_display[0]);
3078               g_free (priv->number_up_layout_2_option->choices_display[1]);
3079               priv->number_up_layout_2_option->choices[0] = priv->number_up_layout_n_option->choices[0];
3080               priv->number_up_layout_2_option->choices[1] = priv->number_up_layout_n_option->choices[1];
3081               priv->number_up_layout_2_option->choices_display[0] = g_strdup ( _("Top to bottom"));
3082               priv->number_up_layout_2_option->choices_display[1] = g_strdup ( _("Bottom to top"));
3083             }
3084         }
3085
3086       layout = dialog_get_number_up_layout (dialog);
3087
3088       old_option = gtk_printer_option_set_lookup (set, "gtk-n-up-layout");
3089       if (old_option != NULL)
3090         gtk_printer_option_set_remove (set, old_option);
3091
3092       if (dialog_get_pages_per_sheet (dialog) != 1)
3093         {
3094           GEnumClass *enum_class;
3095           GEnumValue *enum_value;
3096           enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
3097
3098           if (dialog_get_pages_per_sheet (dialog) == 2)
3099             {
3100               option = priv->number_up_layout_2_option;
3101
3102               switch (layout)
3103                 {
3104                 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM:
3105                 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_LEFT_TO_RIGHT:
3106                   enum_value = g_enum_get_value (enum_class, GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM);
3107                   break;
3108
3109                 case GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP:
3110                 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_LEFT_TO_RIGHT:
3111                   enum_value = g_enum_get_value (enum_class, GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_BOTTOM_TO_TOP);
3112                   break;
3113
3114                 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM:
3115                 case GTK_NUMBER_UP_LAYOUT_TOP_TO_BOTTOM_RIGHT_TO_LEFT:
3116                   enum_value = g_enum_get_value (enum_class, GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM);
3117                   break;
3118
3119                 case GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP:
3120                 case GTK_NUMBER_UP_LAYOUT_BOTTOM_TO_TOP_RIGHT_TO_LEFT:
3121                   enum_value = g_enum_get_value (enum_class, GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_BOTTOM_TO_TOP);
3122                   break;
3123
3124                 default:
3125                   g_assert_not_reached();
3126                   enum_value = NULL;
3127                 }
3128             }
3129           else
3130             {
3131               option = priv->number_up_layout_n_option;
3132
3133               enum_value = g_enum_get_value (enum_class, layout);
3134             }
3135
3136           g_assert (enum_value != NULL);
3137           gtk_printer_option_set (option, enum_value->value_nick);
3138           g_type_class_unref (enum_class);
3139
3140           gtk_printer_option_set_add (set, option);
3141         }
3142     }
3143
3144   setup_option (dialog, "gtk-n-up-layout", priv->number_up_layout);
3145
3146   if (priv->number_up_layout != NULL)
3147     gtk_widget_set_sensitive (GTK_WIDGET (priv->number_up_layout),
3148                               (caps & GTK_PRINT_CAPABILITY_NUMBER_UP_LAYOUT) &&
3149                               (dialog_get_pages_per_sheet (dialog) > 1));
3150 }
3151
3152 static void
3153 custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
3154                                  gint       response_id,
3155                                  gpointer   user_data)
3156 {
3157   GtkPrintUnixDialog        *print_dialog = GTK_PRINT_UNIX_DIALOG (user_data);
3158   GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
3159   GtkTreeModel              *model;
3160   GtkTreeIter                iter;
3161
3162   _gtk_print_load_custom_papers (priv->custom_paper_list);
3163
3164   priv->internal_page_setup_change = TRUE;
3165   update_paper_sizes (print_dialog);
3166   priv->internal_page_setup_change = FALSE;
3167
3168   if (priv->page_setup_set)
3169     {
3170       model = GTK_TREE_MODEL (priv->custom_paper_list);
3171       if (gtk_tree_model_get_iter_first (model, &iter))
3172         {
3173           do
3174             {
3175               GtkPageSetup *page_setup;
3176               gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
3177
3178               if (page_setup &&
3179                   g_strcmp0 (gtk_paper_size_get_display_name (gtk_page_setup_get_paper_size (page_setup)),
3180                              gtk_paper_size_get_display_name (gtk_page_setup_get_paper_size (priv->page_setup))) == 0)
3181                 gtk_print_unix_dialog_set_page_setup (print_dialog, page_setup);
3182
3183               g_object_unref (page_setup);
3184             } while (gtk_tree_model_iter_next (model, &iter));
3185         }
3186     }
3187
3188   gtk_widget_destroy (GTK_WIDGET (custom_paper_dialog));
3189 }
3190
3191 static void
3192 orientation_changed (GtkComboBox        *combo_box,
3193                      GtkPrintUnixDialog *dialog)
3194 {
3195   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3196   GtkPageOrientation         orientation;
3197   GtkPageSetup              *page_setup;
3198
3199   if (priv->internal_page_setup_change)
3200     return;
3201
3202   orientation = (GtkPageOrientation) gtk_combo_box_get_active (GTK_COMBO_BOX (priv->orientation_combo));
3203
3204   if (priv->page_setup)
3205     {
3206       page_setup = gtk_page_setup_copy (priv->page_setup);
3207       if (page_setup)
3208         gtk_page_setup_set_orientation (page_setup, orientation);
3209
3210       gtk_print_unix_dialog_set_page_setup (dialog, page_setup);
3211     }
3212
3213   redraw_page_layout_preview (dialog);
3214 }
3215
3216 static void
3217 paper_size_changed (GtkComboBox        *combo_box,
3218                     GtkPrintUnixDialog *dialog)
3219 {
3220   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3221   GtkTreeIter iter;
3222   GtkPageSetup *page_setup, *last_page_setup;
3223   GtkPageOrientation orientation;
3224
3225   if (priv->internal_page_setup_change)
3226     return;
3227
3228   if (gtk_combo_box_get_active_iter (combo_box, &iter))
3229     {
3230       gtk_tree_model_get (gtk_combo_box_get_model (combo_box),
3231                           &iter, PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
3232
3233       if (page_setup == NULL)
3234         {
3235           GtkWidget *custom_paper_dialog;
3236
3237           /* Change from "manage" menu item to last value */
3238           if (priv->page_setup)
3239             last_page_setup = g_object_ref (priv->page_setup);
3240           else
3241             last_page_setup = gtk_page_setup_new (); /* "good" default */
3242
3243           if (!set_paper_size (dialog, last_page_setup, FALSE, FALSE))
3244             set_paper_size (dialog, last_page_setup, TRUE, TRUE);
3245           g_object_unref (last_page_setup);
3246
3247           /* And show the custom paper dialog */
3248           custom_paper_dialog = _gtk_custom_paper_unix_dialog_new (GTK_WINDOW (dialog), _("Manage Custom Sizes"));
3249           g_signal_connect (custom_paper_dialog, "response", G_CALLBACK (custom_paper_dialog_response_cb), dialog);
3250           gtk_window_present (GTK_WINDOW (custom_paper_dialog));
3251
3252           return;
3253         }
3254
3255       if (priv->page_setup)
3256         orientation = gtk_page_setup_get_orientation (priv->page_setup);
3257       else
3258         orientation = GTK_PAGE_ORIENTATION_PORTRAIT;
3259
3260       gtk_page_setup_set_orientation (page_setup, orientation);
3261       gtk_print_unix_dialog_set_page_setup (dialog, page_setup);
3262
3263       g_object_unref (page_setup);
3264     }
3265
3266   redraw_page_layout_preview (dialog);
3267 }
3268
3269 static gboolean
3270 paper_size_row_is_separator (GtkTreeModel *model,
3271                              GtkTreeIter  *iter,
3272                              gpointer      data)
3273 {
3274   gboolean separator;
3275
3276   gtk_tree_model_get (model, iter, PAGE_SETUP_LIST_COL_IS_SEPARATOR, &separator, -1);
3277   return separator;
3278 }
3279
3280 static void
3281 page_name_func (GtkCellLayout   *cell_layout,
3282                 GtkCellRenderer *cell,
3283                 GtkTreeModel    *tree_model,
3284                 GtkTreeIter     *iter,
3285                 gpointer         data)
3286 {
3287   GtkPageSetup *page_setup;
3288   GtkPaperSize *paper_size;
3289
3290   gtk_tree_model_get (tree_model, iter,
3291                       PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
3292   if (page_setup)
3293     {
3294       paper_size = gtk_page_setup_get_paper_size (page_setup);
3295       g_object_set (cell, "text",  gtk_paper_size_get_display_name (paper_size), NULL);
3296       g_object_unref (page_setup);
3297     }
3298   else
3299     g_object_set (cell, "text",  _("Manage Custom Sizes..."), NULL);
3300 }
3301
3302 static void
3303 create_page_setup_page (GtkPrintUnixDialog *dialog)
3304 {
3305   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3306   GtkWidget *main_vbox, *label, *hbox, *hbox2;
3307   GtkWidget *frame, *table, *widget;
3308   GtkWidget *combo, *spinbutton, *draw;
3309   GtkCellRenderer *cell;
3310
3311   main_vbox = gtk_vbox_new (FALSE, 18);
3312   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
3313   gtk_widget_show (main_vbox);
3314
3315   hbox = gtk_hbox_new (FALSE, 18);
3316   gtk_widget_show (hbox);
3317   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
3318
3319   table = gtk_table_new (5, 2, FALSE);
3320   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3321   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3322   frame = wrap_in_frame (_("Layout"), table);
3323   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 0);
3324   gtk_widget_show (table);
3325
3326   label = gtk_label_new_with_mnemonic (_("T_wo-sided:"));
3327   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3328   gtk_widget_show (label);
3329   gtk_table_attach (GTK_TABLE (table), label,
3330                     0, 1, 0, 1,  GTK_FILL, 0,
3331                     0, 0);
3332
3333   widget = gtk_printer_option_widget_new (NULL);
3334   priv->duplex = GTK_PRINTER_OPTION_WIDGET (widget);
3335   gtk_widget_show (widget);
3336   gtk_table_attach (GTK_TABLE (table), widget,
3337                     1, 2, 0, 1,  GTK_FILL, 0,
3338                     0, 0);
3339   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3340
3341   label = gtk_label_new_with_mnemonic (_("Pages per _side:"));
3342   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3343   gtk_widget_show (label);
3344   gtk_table_attach (GTK_TABLE (table), label,
3345                     0, 1, 1, 2,  GTK_FILL, 0,
3346                     0, 0);
3347
3348   widget = gtk_printer_option_widget_new (NULL);
3349   g_signal_connect_swapped (widget, "changed", G_CALLBACK (redraw_page_layout_preview), dialog);
3350   g_signal_connect_swapped (widget, "changed", G_CALLBACK (update_number_up_layout), dialog);
3351   priv->pages_per_sheet = GTK_PRINTER_OPTION_WIDGET (widget);
3352   gtk_widget_show (widget);
3353   gtk_table_attach (GTK_TABLE (table), widget,
3354                     1, 2, 1, 2,  GTK_FILL, 0,
3355                     0, 0);
3356   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3357
3358   label = gtk_label_new_with_mnemonic (_("Page or_dering:"));
3359   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3360   gtk_widget_show (label);
3361   gtk_table_attach (GTK_TABLE (table), label,
3362                     0, 1, 2, 3,  GTK_FILL, 0,
3363                     0, 0);
3364
3365   widget = gtk_printer_option_widget_new (NULL);
3366   g_signal_connect_swapped (widget, "changed", G_CALLBACK (redraw_page_layout_preview), dialog);
3367   priv->number_up_layout = GTK_PRINTER_OPTION_WIDGET (widget);
3368   gtk_widget_show (widget);
3369   gtk_table_attach (GTK_TABLE (table), widget,
3370                     1, 2, 2, 3,  GTK_FILL, 0,
3371                     0, 0);
3372   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3373
3374   label = gtk_label_new_with_mnemonic (_("_Only print:"));
3375   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3376   gtk_widget_show (label);
3377   gtk_table_attach (GTK_TABLE (table), label,
3378                     0, 1, 3, 4,  GTK_FILL, 0,
3379                     0, 0);
3380
3381   combo = gtk_combo_box_new_text ();
3382   priv->page_set_combo = combo;
3383   gtk_widget_show (combo);
3384   gtk_table_attach (GTK_TABLE (table), combo,
3385                     1, 2, 3, 4,  GTK_FILL, 0,
3386                     0, 0);
3387   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
3388   /* In enum order */
3389   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("All sheets"));
3390   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Even sheets"));
3391   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Odd sheets"));
3392   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
3393
3394   label = gtk_label_new_with_mnemonic (_("Sc_ale:"));
3395   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3396   gtk_widget_show (label);
3397   gtk_table_attach (GTK_TABLE (table), label,
3398                     0, 1, 4, 5,  GTK_FILL, 0,
3399                     0, 0);
3400
3401   hbox2 = gtk_hbox_new (FALSE, 6);
3402   gtk_widget_show (hbox2);
3403   gtk_table_attach (GTK_TABLE (table), hbox2,
3404                     1, 2, 4, 5,  GTK_FILL, 0,
3405                     0, 0);
3406
3407   spinbutton = gtk_spin_button_new_with_range (1.0, 1000.0, 1.0);
3408   priv->scale_spin = spinbutton;
3409   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), 1);
3410   gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), 100.0);
3411   gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
3412   gtk_label_set_mnemonic_widget (GTK_LABEL (label), spinbutton);
3413   gtk_widget_show (spinbutton);
3414   label = gtk_label_new ("%"); /* FIXMEchpe does there exist any language where % needs to be translated? */
3415   gtk_widget_show (label);
3416   gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
3417
3418   table = gtk_table_new (4, 2, FALSE);
3419   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3420   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3421   frame = wrap_in_frame (_("Paper"), table);
3422   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 6);
3423   gtk_widget_show (table);
3424
3425   label = gtk_label_new_with_mnemonic (_("Paper _type:"));
3426   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3427   gtk_widget_show (label);
3428   gtk_table_attach (GTK_TABLE (table), label,
3429                     0, 1, 0, 1,  GTK_FILL, 0,
3430                     0, 0);
3431
3432   widget = gtk_printer_option_widget_new (NULL);
3433   priv->paper_type = GTK_PRINTER_OPTION_WIDGET (widget);
3434   gtk_widget_show (widget);
3435   gtk_table_attach (GTK_TABLE (table), widget,
3436                     1, 2, 0, 1,  GTK_FILL, 0,
3437                     0, 0);
3438   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3439
3440   label = gtk_label_new_with_mnemonic (_("Paper _source:"));
3441   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3442   gtk_widget_show (label);
3443   gtk_table_attach (GTK_TABLE (table), label,
3444                     0, 1, 1, 2,  GTK_FILL, 0,
3445                     0, 0);
3446
3447   widget = gtk_printer_option_widget_new (NULL);
3448   priv->paper_source = GTK_PRINTER_OPTION_WIDGET (widget);
3449   gtk_widget_show (widget);
3450   gtk_table_attach (GTK_TABLE (table), widget,
3451                     1, 2, 1, 2,  GTK_FILL, 0,
3452                     0, 0);
3453   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3454
3455   label = gtk_label_new_with_mnemonic (_("Output t_ray:"));
3456   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3457   gtk_widget_show (label);
3458   gtk_table_attach (GTK_TABLE (table), label,
3459                     0, 1, 2, 3,  GTK_FILL, 0,
3460                     0, 0);
3461
3462   widget = gtk_printer_option_widget_new (NULL);
3463   priv->output_tray = GTK_PRINTER_OPTION_WIDGET (widget);
3464   gtk_widget_show (widget);
3465   gtk_table_attach (GTK_TABLE (table), widget,
3466                     1, 2, 2, 3,  GTK_FILL, 0,
3467                     0, 0);
3468   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3469
3470
3471   label = gtk_label_new_with_mnemonic (_("_Paper size:"));
3472   priv->paper_size_combo_label = GTK_WIDGET (label);
3473   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3474   gtk_widget_show (label);
3475   gtk_table_attach (GTK_TABLE (table), label,
3476                     0, 1, 3, 4,  GTK_FILL, 0,
3477                     0, 0);
3478
3479   combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->page_setup_list));
3480   priv->paper_size_combo = GTK_WIDGET (combo);
3481   gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo), 
3482                                         paper_size_row_is_separator, NULL, NULL);
3483   cell = gtk_cell_renderer_text_new ();
3484   gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
3485   gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), cell,
3486                                       page_name_func, NULL, NULL);
3487   gtk_table_attach (GTK_TABLE (table), combo,
3488                     1, 2, 3, 4,  GTK_FILL, 0,
3489                     0, 0);
3490   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
3491   gtk_widget_set_sensitive (combo, FALSE);
3492   gtk_widget_show (combo);
3493
3494
3495   label = gtk_label_new_with_mnemonic (_("Or_ientation:"));
3496   priv->orientation_combo_label = GTK_WIDGET (label);
3497   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3498   gtk_widget_show (label);
3499   gtk_table_attach (GTK_TABLE (table), label,
3500                     0, 1, 4, 5,
3501                     GTK_FILL, 0, 0, 0);
3502
3503   combo = gtk_combo_box_new_text ();
3504   priv->orientation_combo = GTK_WIDGET (combo);
3505   gtk_table_attach (GTK_TABLE (table), combo,
3506                     1, 2, 4, 5,  GTK_FILL, 0,
3507                     0, 0);
3508   gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
3509   /* In enum order */
3510   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Portrait"));
3511   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Landscape"));
3512   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Reverse portrait"));
3513   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Reverse landscape"));
3514   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
3515   gtk_widget_set_sensitive (combo, FALSE);
3516   gtk_widget_show (combo);
3517
3518
3519   /* Add the page layout preview */
3520   hbox2 = gtk_hbox_new (FALSE, 0);
3521   gtk_widget_show (hbox2);
3522   gtk_box_pack_start (GTK_BOX (main_vbox), hbox2, TRUE, TRUE, 0);
3523
3524   draw = gtk_drawing_area_new ();
3525   gtk_widget_set_has_window (draw, FALSE);
3526   priv->page_layout_preview = draw;
3527   gtk_widget_set_size_request (draw, 350, 200);
3528   g_signal_connect (draw, "expose-event", G_CALLBACK (draw_page_cb), dialog);
3529   gtk_widget_show (draw);
3530
3531   gtk_box_pack_start (GTK_BOX (hbox2), draw, TRUE, FALSE, 0);
3532
3533   label = gtk_label_new (_("Page Setup"));
3534   gtk_widget_show (label);
3535
3536   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
3537                             main_vbox, label);
3538 }
3539
3540 static void
3541 create_job_page (GtkPrintUnixDialog *dialog)
3542 {
3543   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3544   GtkWidget *main_table, *label;
3545   GtkWidget *frame, *table, *radio;
3546   GtkWidget *entry, *widget;
3547   const gchar *at_tooltip;
3548   const gchar *on_hold_tooltip;
3549
3550   main_table = gtk_table_new (2, 2, FALSE);
3551   gtk_container_set_border_width (GTK_CONTAINER (main_table), 12);
3552   gtk_table_set_row_spacings (GTK_TABLE (main_table), 18);
3553   gtk_table_set_col_spacings (GTK_TABLE (main_table), 18);
3554
3555   table = gtk_table_new (2, 2, FALSE);
3556   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3557   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3558   frame = wrap_in_frame (_("Job Details"), table);
3559   gtk_table_attach (GTK_TABLE (main_table), frame,
3560                     0, 1, 0, 1,  GTK_FILL, 0,
3561                     0, 0);
3562   gtk_widget_show (table);
3563
3564   label = gtk_label_new_with_mnemonic (_("Pri_ority:"));
3565   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3566   gtk_widget_show (label);
3567   gtk_table_attach (GTK_TABLE (table), label,
3568                     0, 1, 0, 1,  GTK_FILL, 0,
3569                     0, 0);
3570
3571   widget = gtk_printer_option_widget_new (NULL);
3572   priv->job_prio = GTK_PRINTER_OPTION_WIDGET (widget);
3573   gtk_widget_show (widget);
3574   gtk_table_attach (GTK_TABLE (table), widget,
3575                     1, 2, 0, 1,  GTK_FILL, 0,
3576                     0, 0);
3577   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3578
3579   label = gtk_label_new_with_mnemonic (_("_Billing info:"));
3580   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3581   gtk_widget_show (label);
3582   gtk_table_attach (GTK_TABLE (table), label,
3583                     0, 1, 1, 2,  GTK_FILL, 0,
3584                     0, 0);
3585
3586   widget = gtk_printer_option_widget_new (NULL);
3587   priv->billing_info = GTK_PRINTER_OPTION_WIDGET (widget);
3588   gtk_widget_show (widget);
3589   gtk_table_attach (GTK_TABLE (table), widget,
3590                     1, 2, 1, 2,  GTK_FILL, 0,
3591                     0, 0);
3592   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3593
3594   table = gtk_table_new (2, 2, FALSE);
3595   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3596   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3597   frame = wrap_in_frame (_("Print Document"), table);
3598   gtk_table_attach (GTK_TABLE (main_table), frame,
3599                     0, 1, 1, 2,  GTK_FILL, 0,
3600                     0, 0);
3601   gtk_widget_show (table);
3602
3603   /* Translators: this is one of the choices for the print at option
3604    * in the print dialog
3605    */
3606   radio = gtk_radio_button_new_with_mnemonic (NULL, _("_Now"));
3607   priv->print_now_radio = radio;
3608   gtk_widget_show (radio);
3609   gtk_table_attach (GTK_TABLE (table), radio,
3610                     0, 2, 0, 1,  GTK_FILL, 0,
3611                     0, 0);
3612   /* Translators: this is one of the choices for the print at option
3613    * in the print dialog. It also serves as the label for an entry that
3614    * allows the user to enter a time.
3615    */
3616   radio = gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
3617                                               _("A_t:"));
3618
3619   /* Translators: Ability to parse the am/pm format depends on actual locale.
3620    * You can remove the am/pm values below for your locale if they are not
3621    * supported.
3622    */
3623   at_tooltip = _("Specify the time of print,\n e.g. 15:30, 2:35 pm, 14:15:20, 11:46:30 am, 4 pm");
3624   gtk_widget_set_tooltip_text (radio, at_tooltip);
3625   priv->print_at_radio = radio;
3626   gtk_widget_show (radio);
3627   gtk_table_attach (GTK_TABLE (table), radio,
3628                     0, 1, 1, 2,  GTK_FILL, 0,
3629                     0, 0);
3630
3631   entry = gtk_entry_new ();
3632   gtk_widget_set_tooltip_text (entry, at_tooltip);
3633   atk_object_set_name (gtk_widget_get_accessible (entry), _("Time of print"));
3634   atk_object_set_description (gtk_widget_get_accessible (entry), at_tooltip);
3635   priv->print_at_entry = entry;
3636   gtk_widget_show (entry);
3637   gtk_table_attach (GTK_TABLE (table), entry,
3638                     1, 2, 1, 2,  GTK_FILL, 0,
3639                     0, 0);
3640
3641   g_signal_connect (radio, "toggled", G_CALLBACK (update_entry_sensitivity), entry);
3642   update_entry_sensitivity (radio, entry);
3643
3644   /* Translators: this is one of the choices for the print at option
3645    * in the print dialog. It means that the print job will not be
3646    * printed until it explicitly gets 'released'.
3647    */
3648   radio = gtk_radio_button_new_with_mnemonic (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
3649                                               _("On _hold"));
3650   on_hold_tooltip = _("Hold the job until it is explicitly released");
3651   gtk_widget_set_tooltip_text (radio, on_hold_tooltip);
3652   priv->print_hold_radio = radio;
3653   gtk_widget_show (radio);
3654   gtk_table_attach (GTK_TABLE (table), radio,
3655                     0, 2, 2, 3,  GTK_FILL, 0,
3656                     0, 0);
3657
3658   g_signal_connect_swapped (priv->print_now_radio, "toggled",
3659                             G_CALLBACK (update_print_at_option), dialog);
3660   g_signal_connect_swapped (priv->print_at_radio, "toggled",
3661                             G_CALLBACK (update_print_at_option), dialog);
3662   g_signal_connect_swapped (priv->print_at_entry, "changed",
3663                             G_CALLBACK (update_print_at_option), dialog);
3664   g_signal_connect_swapped (priv->print_hold_radio, "toggled",
3665                             G_CALLBACK (update_print_at_option), dialog);
3666
3667   table = gtk_table_new (2, 2, FALSE);
3668   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3669   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3670   frame = wrap_in_frame (_("Add Cover Page"), table);
3671   gtk_table_attach (GTK_TABLE (main_table), frame,
3672                     1, 2, 0, 1,  GTK_FILL, 0,
3673                     0, 0);
3674   gtk_widget_show (table);
3675
3676   /* Translators, this is the label used for the option in the print
3677    * dialog that controls the front cover page.
3678    */
3679   label = gtk_label_new_with_mnemonic (_("Be_fore:"));
3680   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3681   gtk_widget_show (label);
3682   gtk_table_attach (GTK_TABLE (table), label,
3683                     0, 1, 0, 1,  GTK_FILL, 0,
3684                     0, 0);
3685
3686   widget = gtk_printer_option_widget_new (NULL);
3687   priv->cover_before = GTK_PRINTER_OPTION_WIDGET (widget);
3688   gtk_widget_show (widget);
3689   gtk_table_attach (GTK_TABLE (table), widget,
3690                     1, 2, 0, 1,  GTK_FILL, 0,
3691                     0, 0);
3692   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3693
3694   /* Translators, this is the label used for the option in the print
3695    * dialog that controls the back cover page.
3696    */
3697   label = gtk_label_new_with_mnemonic (_("_After:"));
3698   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
3699   gtk_widget_show (label);
3700   gtk_table_attach (GTK_TABLE (table), label,
3701                     0, 1, 1, 2,  GTK_FILL, 0,
3702                     0, 0);
3703
3704   widget = gtk_printer_option_widget_new (NULL);
3705   priv->cover_after = GTK_PRINTER_OPTION_WIDGET (widget);
3706   gtk_widget_show (widget);
3707   gtk_table_attach (GTK_TABLE (table), widget,
3708                     1, 2, 1, 2,  GTK_FILL, 0,
3709                     0, 0);
3710   gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
3711
3712   /* Translators: this is the tab label for the notebook tab containing
3713    * job-specific options in the print dialog
3714    */
3715   label = gtk_label_new (_("Job"));
3716   gtk_widget_show (label);
3717
3718   priv->job_page = main_table;
3719   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
3720                             main_table, label);
3721 }
3722
3723 static void
3724 create_optional_page (GtkPrintUnixDialog  *dialog,
3725                       const gchar         *text,
3726                       GtkWidget          **table_out,
3727                       GtkWidget          **page_out)
3728 {
3729   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3730   GtkWidget *table, *label, *scrolled;
3731
3732   scrolled = gtk_scrolled_window_new (NULL, NULL);
3733   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
3734                                   GTK_POLICY_NEVER,
3735                                   GTK_POLICY_AUTOMATIC);
3736
3737   table = gtk_table_new (1, 2, FALSE);
3738   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
3739   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
3740   gtk_container_set_border_width (GTK_CONTAINER (table), 12);
3741   gtk_widget_show (table);
3742
3743   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled),
3744                                          table);
3745   gtk_viewport_set_shadow_type (GTK_VIEWPORT (gtk_bin_get_child (GTK_BIN (scrolled))),
3746                                 GTK_SHADOW_NONE);
3747
3748   label = gtk_label_new (text);
3749   gtk_widget_show (label);
3750
3751   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
3752                             scrolled, label);
3753
3754   *table_out = table;
3755   *page_out = scrolled;
3756 }
3757
3758 static void
3759 create_advanced_page (GtkPrintUnixDialog *dialog)
3760 {
3761   GtkPrintUnixDialogPrivate *priv = dialog->priv;
3762   GtkWidget *main_vbox, *label, *scrolled;
3763
3764   scrolled = gtk_scrolled_window_new (NULL, NULL);
3765   priv->advanced_page = scrolled;
3766   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
3767                                   GTK_POLICY_NEVER,
3768                                   GTK_POLICY_AUTOMATIC);
3769
3770   main_vbox = gtk_vbox_new (FALSE, 18);
3771   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 12);
3772   gtk_widget_show (main_vbox);
3773
3774   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled),
3775                                          main_vbox);
3776   gtk_viewport_set_shadow_type (GTK_VIEWPORT (gtk_bin_get_child (GTK_BIN (scrolled))),
3777                                 GTK_SHADOW_NONE);
3778
3779   priv->advanced_vbox = main_vbox;
3780
3781   label = gtk_label_new (_("Advanced"));
3782   gtk_widget_show (label);
3783
3784   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
3785                             scrolled, label);
3786 }
3787
3788 static void
3789 populate_dialog (GtkPrintUnixDialog *print_dialog)
3790 {
3791   GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
3792   GtkDialog *dialog = GTK_DIALOG (print_dialog);
3793   GtkWidget *vbox, *conflict_hbox, *image, *label;
3794   GtkWidget *action_area, *content_area;
3795
3796   content_area = gtk_dialog_get_content_area (dialog);
3797   action_area = gtk_dialog_get_action_area (dialog);
3798
3799   gtk_dialog_set_has_separator (dialog, FALSE);
3800   gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
3801   gtk_box_set_spacing (GTK_BOX (content_area), 2); /* 2 * 5 + 2 = 12 */
3802   gtk_container_set_border_width (GTK_CONTAINER (action_area), 5);
3803   gtk_box_set_spacing (GTK_BOX (action_area), 6);
3804
3805   vbox = gtk_vbox_new (FALSE, 6);
3806   gtk_container_set_border_width (GTK_CONTAINER (vbox), 5);
3807   gtk_box_pack_start (GTK_BOX (content_area), vbox, TRUE, TRUE, 0);
3808   gtk_widget_show (vbox);
3809
3810   priv->notebook = gtk_notebook_new ();
3811   gtk_box_pack_start (GTK_BOX (vbox), priv->notebook, TRUE, TRUE, 0);
3812   gtk_widget_show (priv->notebook);
3813
3814   create_printer_list_model (print_dialog);
3815
3816   create_main_page (print_dialog);
3817   create_page_setup_page (print_dialog);
3818   create_job_page (print_dialog);
3819   /* Translators: this will appear as tab label in print dialog. */
3820   create_optional_page (print_dialog, _("Image Quality"),
3821                         &priv->image_quality_table,
3822                         &priv->image_quality_page);
3823   /* Translators: this will appear as tab label in print dialog. */
3824   create_optional_page (print_dialog, _("Color"),
3825                         &priv->color_table,
3826                         &priv->color_page);
3827   /* Translators: this will appear as tab label in print dialog. */
3828   /* It's a typographical term, as in "Binding and finishing" */
3829   create_optional_page (print_dialog, _("Finishing"),
3830                         &priv->finishing_table,
3831                         &priv->finishing_page);
3832   create_advanced_page (print_dialog);
3833
3834   priv->conflicts_widget = conflict_hbox = gtk_hbox_new (FALSE, 12);
3835   gtk_box_pack_end (GTK_BOX (vbox), conflict_hbox, FALSE, FALSE, 0);
3836   image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU);
3837   gtk_widget_show (image);
3838   gtk_box_pack_start (GTK_BOX (conflict_hbox), image, FALSE, TRUE, 0);
3839   label = gtk_label_new (_("Some of the settings in the dialog conflict"));
3840   gtk_widget_show (label);
3841   gtk_box_pack_start (GTK_BOX (conflict_hbox), label, FALSE, TRUE, 0);
3842
3843   load_print_backends (print_dialog);
3844 }
3845
3846 /**
3847  * gtk_print_unix_dialog_new:
3848  * @title: (allow-none): Title of the dialog, or %NULL
3849  * @parent: (allow-none): Transient parent of the dialog, or %NULL
3850  *
3851  * Creates a new #GtkPrintUnixDialog.
3852  *
3853  * Return value: a new #GtkPrintUnixDialog
3854  *
3855  * Since: 2.10
3856  **/
3857 GtkWidget *
3858 gtk_print_unix_dialog_new (const gchar *title,
3859                            GtkWindow   *parent)
3860 {
3861   GtkWidget *result;
3862   const gchar *_title = _("Print");
3863
3864   if (title)
3865     _title = title;
3866
3867   result = g_object_new (GTK_TYPE_PRINT_UNIX_DIALOG,
3868                          "transient-for", parent,
3869                          "title", _title,
3870                          "has-separator", FALSE,
3871                          NULL);
3872
3873   return result;
3874 }
3875
3876 /**
3877  * gtk_print_unix_dialog_get_selected_printer:
3878  * @dialog: a #GtkPrintUnixDialog
3879  *
3880  * Gets the currently selected printer.
3881  *
3882  * Returns: the currently selected printer
3883  *
3884  * Since: 2.10
3885  */
3886 GtkPrinter *
3887 gtk_print_unix_dialog_get_selected_printer (GtkPrintUnixDialog *dialog)
3888 {
3889   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
3890
3891   return dialog->priv->current_printer;
3892 }
3893
3894 /**
3895  * gtk_print_unix_dialog_set_page_setup:
3896  * @dialog: a #GtkPrintUnixDialog
3897  * @page_setup: a #GtkPageSetup
3898  *
3899  * Sets the page setup of the #GtkPrintUnixDialog.
3900  *
3901  * Since: 2.10
3902  */
3903 void
3904 gtk_print_unix_dialog_set_page_setup (GtkPrintUnixDialog *dialog,
3905                                       GtkPageSetup       *page_setup)
3906 {
3907   GtkPrintUnixDialogPrivate *priv;
3908
3909   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
3910   g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
3911
3912   priv = dialog->priv;
3913
3914   if (priv->page_setup != page_setup)
3915     {
3916       g_object_unref (priv->page_setup);
3917       priv->page_setup = g_object_ref (page_setup);
3918
3919       priv->page_setup_set = TRUE;
3920
3921       g_object_notify (G_OBJECT (dialog), "page-setup");
3922     }
3923 }
3924
3925 /**
3926  * gtk_print_unix_dialog_get_page_setup:
3927  * @dialog: a #GtkPrintUnixDialog
3928  *
3929  * Gets the page setup that is used by the #GtkPrintUnixDialog.
3930  *
3931  * Returns: the page setup of @dialog.
3932  *
3933  * Since: 2.10
3934  */
3935 GtkPageSetup *
3936 gtk_print_unix_dialog_get_page_setup (GtkPrintUnixDialog *dialog)
3937 {
3938   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
3939
3940   return dialog->priv->page_setup;
3941 }
3942
3943 /**
3944  * gtk_print_unix_dialog_get_page_setup_set:
3945  * @dialog: a #GtkPrintUnixDialog
3946  *
3947  * Gets the page setup that is used by the #GtkPrintUnixDialog.
3948  *
3949  * Returns: whether a page setup was set by user.
3950  *
3951  * Since: 2.18
3952  */
3953 gboolean
3954 gtk_print_unix_dialog_get_page_setup_set (GtkPrintUnixDialog *dialog)
3955 {
3956   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), FALSE);
3957
3958   return dialog->priv->page_setup_set;
3959 }
3960
3961 /**
3962  * gtk_print_unix_dialog_set_current_page:
3963  * @dialog: a #GtkPrintUnixDialog
3964  * @current_page: the current page number.
3965  *
3966  * Sets the current page number. If @current_page is not -1, this enables
3967  * the current page choice for the range of pages to print.
3968  *
3969  * Since: 2.10
3970  */
3971 void
3972 gtk_print_unix_dialog_set_current_page (GtkPrintUnixDialog *dialog,
3973                                         gint                current_page)
3974 {
3975   GtkPrintUnixDialogPrivate *priv;
3976
3977   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
3978
3979   priv = dialog->priv;
3980
3981   if (priv->current_page != current_page)
3982     {
3983       priv->current_page = current_page;
3984
3985       if (priv->current_page_radio)
3986         gtk_widget_set_sensitive (priv->current_page_radio, current_page != -1);
3987
3988       g_object_notify (G_OBJECT (dialog), "current-page");
3989     }
3990 }
3991
3992 /**
3993  * gtk_print_unix_dialog_get_current_page:
3994  * @dialog: a #GtkPrintUnixDialog
3995  *
3996  * Gets the current page of the #GtkPrintDialog.
3997  *
3998  * Returns: the current page of @dialog
3999  *
4000  * Since: 2.10
4001  */
4002 gint
4003 gtk_print_unix_dialog_get_current_page (GtkPrintUnixDialog *dialog)
4004 {
4005   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), -1);
4006
4007   return dialog->priv->current_page;
4008 }
4009
4010 static gboolean
4011 set_active_printer (GtkPrintUnixDialog *dialog,
4012                     const gchar        *printer_name)
4013 {
4014   GtkPrintUnixDialogPrivate *priv = dialog->priv;
4015   GtkTreeModel *model;
4016   GtkTreeIter iter, filter_iter;
4017   GtkTreeSelection *selection;
4018   GtkPrinter *printer;
4019
4020   model = GTK_TREE_MODEL (priv->printer_list);
4021
4022   if (gtk_tree_model_get_iter_first (model, &iter))
4023     {
4024       do
4025         {
4026           gtk_tree_model_get (GTK_TREE_MODEL (priv->printer_list), &iter,
4027                               PRINTER_LIST_COL_PRINTER_OBJ, &printer, -1);
4028           if (printer == NULL)
4029             continue;
4030
4031           if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
4032             {
4033               gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
4034                                                                 &filter_iter, &iter);
4035
4036               selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
4037               priv->internal_printer_change = TRUE;
4038               gtk_tree_selection_select_iter (selection, &filter_iter);
4039               priv->internal_printer_change = FALSE;
4040               g_free (priv->waiting_for_printer);
4041               priv->waiting_for_printer = NULL;
4042
4043               g_object_unref (printer);
4044               return TRUE;
4045             }
4046
4047           g_object_unref (printer);
4048
4049         } while (gtk_tree_model_iter_next (model, &iter));
4050     }
4051
4052   return FALSE;
4053 }
4054
4055 /**
4056  * gtk_print_unix_dialog_set_settings:
4057  * @dialog: a #GtkPrintUnixDialog
4058  * @settings: (allow-none): a #GtkPrintSettings, or %NULL
4059  *
4060  * Sets the #GtkPrintSettings for the #GtkPrintUnixDialog. Typically,
4061  * this is used to restore saved print settings from a previous print
4062  * operation before the print dialog is shown.
4063  *
4064  * Since: 2.10
4065  **/
4066 void
4067 gtk_print_unix_dialog_set_settings (GtkPrintUnixDialog *dialog,
4068                                     GtkPrintSettings   *settings)
4069 {
4070   GtkPrintUnixDialogPrivate *priv;
4071   const gchar *printer;
4072   GtkPageRange *ranges;
4073   gint num_ranges;
4074
4075   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
4076   g_return_if_fail (settings == NULL || GTK_IS_PRINT_SETTINGS (settings));
4077
4078   priv = dialog->priv;
4079
4080   if (settings != NULL)
4081     {
4082       dialog_set_collate (dialog, gtk_print_settings_get_collate (settings));
4083       dialog_set_reverse (dialog, gtk_print_settings_get_reverse (settings));
4084       dialog_set_n_copies (dialog, gtk_print_settings_get_n_copies (settings));
4085       dialog_set_scale (dialog, gtk_print_settings_get_scale (settings));
4086       dialog_set_page_set (dialog, gtk_print_settings_get_page_set (settings));
4087       dialog_set_print_pages (dialog, gtk_print_settings_get_print_pages (settings));
4088       ranges = gtk_print_settings_get_page_ranges (settings, &num_ranges);
4089       if (ranges)
4090         {
4091           dialog_set_page_ranges (dialog, ranges, num_ranges);
4092           g_free (ranges);
4093         }
4094
4095       priv->format_for_printer =
4096         g_strdup (gtk_print_settings_get (settings, "format-for-printer"));
4097     }
4098
4099   if (priv->initial_settings)
4100     g_object_unref (priv->initial_settings);
4101
4102   priv->initial_settings = settings;
4103
4104   g_free (priv->waiting_for_printer);
4105   priv->waiting_for_printer = NULL;
4106
4107   if (settings)
4108     {
4109       g_object_ref (settings);
4110
4111       printer = gtk_print_settings_get_printer (settings);
4112
4113       if (printer && !set_active_printer (dialog, printer))
4114         priv->waiting_for_printer = g_strdup (printer);
4115     }
4116
4117   g_object_notify (G_OBJECT (dialog), "print-settings");
4118 }
4119
4120 /**
4121  * gtk_print_unix_dialog_get_settings:
4122  * @dialog: a #GtkPrintUnixDialog
4123  *
4124  * Gets a new #GtkPrintSettings object that represents the
4125  * current values in the print dialog. Note that this creates a
4126  * <emphasis>new object</emphasis>, and you need to unref it
4127  * if don't want to keep it.
4128  *
4129  * Returns: a new #GtkPrintSettings object with the values from @dialog
4130  *
4131  * Since: 2.10
4132  */
4133 GtkPrintSettings *
4134 gtk_print_unix_dialog_get_settings (GtkPrintUnixDialog *dialog)
4135 {
4136   GtkPrintUnixDialogPrivate *priv;
4137   GtkPrintSettings *settings;
4138   GtkPrintPages print_pages;
4139   GtkPageRange *ranges;
4140   gint n_ranges;
4141
4142   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
4143
4144   priv = dialog->priv;
4145   settings = gtk_print_settings_new ();
4146
4147   if (priv->current_printer)
4148     gtk_print_settings_set_printer (settings,
4149                                     gtk_printer_get_name (priv->current_printer));
4150   else
4151     gtk_print_settings_set_printer (settings, "default");
4152
4153   gtk_print_settings_set (settings, "format-for-printer",
4154                           priv->format_for_printer);
4155
4156   gtk_print_settings_set_collate (settings,
4157                                   dialog_get_collate (dialog));
4158
4159   gtk_print_settings_set_reverse (settings,
4160                                   dialog_get_reverse (dialog));
4161
4162   gtk_print_settings_set_n_copies (settings,
4163                                    dialog_get_n_copies (dialog));
4164
4165   gtk_print_settings_set_scale (settings,
4166                                 dialog_get_scale (dialog));
4167
4168   gtk_print_settings_set_page_set (settings,
4169                                    dialog_get_page_set (dialog));
4170
4171   print_pages = dialog_get_print_pages (dialog);
4172   gtk_print_settings_set_print_pages (settings, print_pages);
4173
4174   ranges = dialog_get_page_ranges (dialog, &n_ranges);
4175   if (ranges)
4176     {
4177       gtk_print_settings_set_page_ranges  (settings, ranges, n_ranges);
4178       g_free (ranges);
4179     }
4180
4181   /* TODO: print when. How to handle? */
4182
4183   if (priv->current_printer)
4184     _gtk_printer_get_settings_from_options (priv->current_printer,
4185                                             priv->options,
4186                                             settings);
4187
4188   return settings;
4189 }
4190
4191 /**
4192  * gtk_print_unix_dialog_add_custom_tab:
4193  * @dialog: a #GtkPrintUnixDialog
4194  * @child: the widget to put in the custom tab
4195  * @tab_label: the widget to use as tab label
4196  *
4197  * Adds a custom tab to the print dialog.
4198  *
4199  * Since: 2.10
4200  */
4201 void
4202 gtk_print_unix_dialog_add_custom_tab (GtkPrintUnixDialog *dialog,
4203                                       GtkWidget          *child,
4204                                       GtkWidget          *tab_label)
4205 {
4206   gtk_notebook_insert_page (GTK_NOTEBOOK (dialog->priv->notebook),
4207                             child, tab_label, 2);
4208   gtk_widget_show (child);
4209   gtk_widget_show (tab_label);
4210 }
4211
4212 /**
4213  * gtk_print_unix_dialog_set_manual_capabilities:
4214  * @dialog: a #GtkPrintUnixDialog
4215  * @capabilities: the printing capabilities of your application
4216  *
4217  * This lets you specify the printing capabilities your application
4218  * supports. For instance, if you can handle scaling the output then
4219  * you pass #GTK_PRINT_CAPABILITY_SCALE. If you don't pass that, then
4220  * the dialog will only let you select the scale if the printing
4221  * system automatically handles scaling.
4222  *
4223  * Since: 2.10
4224  */
4225 void
4226 gtk_print_unix_dialog_set_manual_capabilities (GtkPrintUnixDialog   *dialog,
4227                                                GtkPrintCapabilities  capabilities)
4228 {
4229   GtkPrintUnixDialogPrivate *priv = dialog->priv;
4230
4231   if (priv->manual_capabilities != capabilities)
4232     {
4233       priv->manual_capabilities = capabilities;
4234       update_dialog_from_capabilities (dialog);
4235
4236       if (priv->current_printer)
4237         {
4238           GtkTreeSelection *selection;
4239
4240           selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
4241
4242           g_object_unref (priv->current_printer);
4243           priv->current_printer = NULL;
4244           priv->internal_printer_change = TRUE;
4245           selected_printer_changed (selection, dialog);
4246           priv->internal_printer_change = FALSE;
4247        }
4248
4249       g_object_notify (G_OBJECT (dialog), "manual-capabilities");
4250     }
4251 }
4252
4253 /**
4254  * gtk_print_unix_dialog_get_manual_capabilities:
4255  * @dialog: a #GtkPrintUnixDialog
4256  *
4257  * Gets the value of #GtkPrintUnixDialog::manual-capabilities property.
4258  *
4259  * Returns: the printing capabilities
4260  *
4261  * Since: 2.18
4262  */
4263 GtkPrintCapabilities
4264 gtk_print_unix_dialog_get_manual_capabilities (GtkPrintUnixDialog *dialog)
4265 {
4266   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), FALSE);
4267
4268   return dialog->priv->manual_capabilities;
4269 }
4270
4271 /**
4272  * gtk_print_unix_dialog_set_support_selection:
4273  * @dialog: a #GtkPrintUnixDialog
4274  * @support_selection: %TRUE to allow print selection
4275  *
4276  * Sets whether the print dialog allows user to print a selection.
4277  *
4278  * Since: 2.18
4279  */
4280 void
4281 gtk_print_unix_dialog_set_support_selection (GtkPrintUnixDialog *dialog,
4282                                              gboolean            support_selection)
4283 {
4284   GtkPrintUnixDialogPrivate *priv;
4285
4286   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
4287
4288   priv = dialog->priv;
4289
4290   support_selection = support_selection != FALSE;
4291   if (priv->support_selection != support_selection)
4292     {
4293       priv->support_selection = support_selection;
4294
4295       if (priv->selection_radio)
4296         {
4297           if (support_selection)
4298             {
4299               gtk_widget_set_sensitive (priv->selection_radio, priv->has_selection);
4300               gtk_table_set_row_spacing (GTK_TABLE (priv->range_table),
4301                                          2,
4302                                          gtk_table_get_default_row_spacing (GTK_TABLE (priv->range_table)));
4303               gtk_widget_show (priv->selection_radio);
4304             }
4305           else
4306             {
4307               gtk_widget_set_sensitive (priv->selection_radio, FALSE);
4308               gtk_table_set_row_spacing (GTK_TABLE (priv->range_table), 2, 0);
4309               gtk_widget_hide (priv->selection_radio);
4310             }
4311         }
4312
4313       g_object_notify (G_OBJECT (dialog), "support-selection");
4314     }
4315 }
4316
4317 /**
4318  * gtk_print_unix_dialog_get_support_selection:
4319  * @dialog: a #GtkPrintUnixDialog
4320  *
4321  * Gets the value of #GtkPrintUnixDialog::support-selection property.
4322  *
4323  * Returns: whether the application supports print of selection
4324  *
4325  * Since: 2.18
4326  */
4327 gboolean
4328 gtk_print_unix_dialog_get_support_selection (GtkPrintUnixDialog *dialog)
4329 {
4330   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), FALSE);
4331
4332   return dialog->priv->support_selection;
4333 }
4334
4335 /**
4336  * gtk_print_unix_dialog_set_has_selection:
4337  * @dialog: a #GtkPrintUnixDialog
4338  * @has_selection: %TRUE indicates that a selection exists
4339  *
4340  * Sets whether a selection exists.
4341  *
4342  * Since: 2.18
4343  */
4344 void
4345 gtk_print_unix_dialog_set_has_selection (GtkPrintUnixDialog *dialog,
4346                                          gboolean            has_selection)
4347 {
4348   GtkPrintUnixDialogPrivate *priv;
4349
4350   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
4351
4352   priv = dialog->priv;
4353
4354   has_selection = has_selection != FALSE;
4355   if (priv->has_selection != has_selection)
4356     {
4357       priv->has_selection = has_selection;
4358
4359       if (priv->selection_radio)
4360         {
4361           if (priv->support_selection)
4362             gtk_widget_set_sensitive (priv->selection_radio, has_selection);
4363           else
4364             gtk_widget_set_sensitive (priv->selection_radio, FALSE);
4365         }
4366
4367       g_object_notify (G_OBJECT (dialog), "has-selection");
4368     }
4369 }
4370
4371 /**
4372  * gtk_print_unix_dialog_get_has_selection:
4373  * @dialog: a #GtkPrintUnixDialog
4374  *
4375  * Gets the value of #GtkPrintUnixDialog::has-selection property.
4376  *
4377  * Returns: whether there is a selection
4378  *
4379  * Since: 2.18
4380  */
4381 gboolean
4382 gtk_print_unix_dialog_get_has_selection (GtkPrintUnixDialog *dialog)
4383 {
4384   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), FALSE);
4385
4386   return dialog->priv->has_selection;
4387 }
4388
4389 /**
4390  * gtk_print_unix_dialog_set_embed_page_setup
4391  * @dialog: a #GtkPrintUnixDialog
4392  * @embed: embed page setup selection
4393  *
4394  * Embed page size combo box and orientation combo box into page setup page.
4395  *
4396  * Since: 2.18
4397  */
4398 void
4399 gtk_print_unix_dialog_set_embed_page_setup (GtkPrintUnixDialog *dialog,
4400                                             gboolean            embed)
4401 {
4402   GtkPrintUnixDialogPrivate *priv;
4403
4404   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
4405
4406   priv = dialog->priv;
4407
4408   embed = embed != FALSE;
4409   if (priv->embed_page_setup != embed)
4410     {
4411       priv->embed_page_setup = embed;
4412
4413       gtk_widget_set_sensitive (priv->paper_size_combo, priv->embed_page_setup);
4414       gtk_widget_set_sensitive (priv->orientation_combo, priv->embed_page_setup);
4415
4416       if (priv->embed_page_setup)
4417         {
4418           if (priv->paper_size_combo != NULL)
4419             g_signal_connect (priv->paper_size_combo, "changed", G_CALLBACK (paper_size_changed), dialog);
4420
4421           if (priv->orientation_combo)
4422             g_signal_connect (priv->orientation_combo, "changed", G_CALLBACK (orientation_changed), dialog);
4423         }
4424       else
4425         {
4426           if (priv->paper_size_combo != NULL)
4427             g_signal_handlers_disconnect_by_func (priv->paper_size_combo, G_CALLBACK (paper_size_changed), dialog);
4428
4429           if (priv->orientation_combo)
4430             g_signal_handlers_disconnect_by_func (priv->orientation_combo, G_CALLBACK (orientation_changed), dialog);
4431         }
4432
4433       priv->internal_page_setup_change = TRUE;
4434       update_paper_sizes (dialog);
4435       priv->internal_page_setup_change = FALSE;
4436     }
4437 }
4438
4439 /**
4440  * gtk_print_unix_dialog_get_embed_page_setup:
4441  * @dialog: a #GtkPrintUnixDialog
4442  *
4443  * Gets the value of #GtkPrintUnixDialog::embed-page-setup property.
4444  *
4445  * Returns: whether there is a selection
4446  *
4447  * Since: 2.18
4448  */
4449 gboolean
4450 gtk_print_unix_dialog_get_embed_page_setup (GtkPrintUnixDialog *dialog)
4451 {
4452   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), FALSE);
4453
4454   return dialog->priv->embed_page_setup;
4455 }