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