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