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