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