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