]> Pileus Git - ~andy/gtk/blob - gtk/gtkprintunixdialog.c
Even/Odd Pages -> Even/Odd Sheets
[~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  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdio.h>
25 #include <math.h>
26
27 #include "gtkintl.h"
28 #include "gtkprivate.h"
29
30 #include "gtkspinbutton.h"
31 #include "gtkcellrendererpixbuf.h"
32 #include "gtkcellrenderertext.h"
33 #include "gtkstock.h"
34 #include "gtkimage.h"
35 #include "gtktreeselection.h"
36 #include "gtknotebook.h"
37 #include "gtkscrolledwindow.h"
38 #include "gtkcombobox.h"
39 #include "gtktogglebutton.h"
40 #include "gtkradiobutton.h"
41 #include "gtkdrawingarea.h"
42 #include "gtkvbox.h"
43 #include "gtktable.h"
44 #include "gtkframe.h"
45 #include "gtkalignment.h"
46 #include "gtklabel.h"
47
48 #include "gtkprintbackend.h"
49 #include "gtkprintunixdialog.h"
50 #include "gtkprinteroptionwidget.h"
51 #include "gtkalias.h"
52
53 #define EXAMPLE_PAGE_AREA_SIZE 140
54
55 #define GTK_PRINT_UNIX_DIALOG_GET_PRIVATE(o)  \
56    (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_UNIX_DIALOG, GtkPrintUnixDialogPrivate))
57
58 static void gtk_print_unix_dialog_destroy      (GtkPrintUnixDialog *dialog);
59 static void gtk_print_unix_dialog_finalize     (GObject            *object);
60 static void gtk_print_unix_dialog_set_property (GObject            *object,
61                                                 guint               prop_id,
62                                                 const GValue       *value,
63                                                 GParamSpec         *pspec);
64 static void gtk_print_unix_dialog_get_property (GObject            *object,
65                                                 guint               prop_id,
66                                                 GValue             *value,
67                                                 GParamSpec         *pspec);
68 static void populate_dialog                    (GtkPrintUnixDialog *dialog);
69 static void unschedule_idle_mark_conflicts     (GtkPrintUnixDialog *dialog);
70 static void selected_printer_changed           (GtkTreeSelection   *selection,
71                                                 GtkPrintUnixDialog *dialog);
72 static void clear_per_printer_ui               (GtkPrintUnixDialog *dialog);
73
74 enum {
75   PROP_0,
76   PROP_PAGE_SETUP,
77   PROP_CURRENT_PAGE,
78   PROP_PRINT_SETTINGS,
79   PROP_SELECTED_PRINTER
80 };
81
82 enum {
83   PRINTER_LIST_COL_ICON,
84   PRINTER_LIST_COL_NAME,
85   PRINTER_LIST_COL_STATE,
86   PRINTER_LIST_COL_JOBS,
87   PRINTER_LIST_COL_LOCATION,
88   PRINTER_LIST_COL_PRINTER_OBJ,
89   PRINTER_LIST_N_COLS
90 };
91
92 #define _EXTENTION_POINT_MAIN_PAGE_CUSTOM_INPUT "gtk-main-page-custom-input"
93
94 struct GtkPrintUnixDialogPrivate
95 {
96   GtkWidget *notebook;
97
98   GtkWidget *printer_treeview;
99   
100   GtkTreeModel *printer_list;
101   GtkTreeModelFilter *printer_list_filter;
102
103   GtkPageSetup *page_setup;
104
105   GtkWidget *all_pages_radio;
106   GtkWidget *current_page_radio;
107   GtkWidget *page_range_radio;
108   GtkWidget *page_range_entry;
109   
110   GtkWidget *copies_spin;
111   GtkWidget *collate_check;
112   GtkWidget *reverse_check;
113   GtkWidget *collate_image;
114   GtkWidget *page_layout_preview;
115   GtkWidget *scale_spin;
116   GtkWidget *page_set_combo;
117   GtkWidget *print_now_radio;
118   GtkWidget *print_at_radio;
119   GtkWidget *print_at_entry;
120   GtkWidget *print_hold_radio;
121   gboolean updating_print_at;
122   GtkPrinterOptionWidget *pages_per_sheet;
123   GtkPrinterOptionWidget *duplex;
124   GtkPrinterOptionWidget *paper_type;
125   GtkPrinterOptionWidget *paper_source;
126   GtkPrinterOptionWidget *output_tray;
127   GtkPrinterOptionWidget *job_prio;
128   GtkPrinterOptionWidget *billing_info;
129   GtkPrinterOptionWidget *cover_before;
130   GtkPrinterOptionWidget *cover_after;
131
132   GtkWidget *conflicts_widget;
133
134   GtkWidget *job_page;
135   GtkWidget *finishing_table;
136   GtkWidget *finishing_page;
137   GtkWidget *image_quality_table;
138   GtkWidget *image_quality_page;
139   GtkWidget *color_table;
140   GtkWidget *color_page;
141
142   GtkWidget *advanced_vbox;
143   GtkWidget *advanced_page;
144
145   GHashTable *extention_points;  
146
147   /* These are set initially on selected printer (either default printer, printer
148    * taken from set settings, or user-selected), but when any setting is changed
149    * by the user it is cleared */
150   GtkPrintSettings *initial_settings;
151   
152   /* This is the initial printer set by set_settings. We look for it in the
153    * added printers. We clear this whenever the user manually changes
154    * to another printer, when the user changes a setting or when we find
155    * this printer */
156   char *waiting_for_printer;
157   gboolean internal_printer_change;
158   
159   GList *print_backends;
160   
161   GtkPrinter *current_printer;
162   guint request_details_tag;
163   GtkPrinterOptionSet *options;
164   gulong options_changed_handler;
165   gulong mark_conflicts_id;
166
167   char *format_for_printer;
168   
169   gint current_page;
170 };
171
172 G_DEFINE_TYPE (GtkPrintUnixDialog, gtk_print_unix_dialog, GTK_TYPE_DIALOG);
173
174 /* XPM */
175 static const char *collate_xpm[] = {
176 "65 35 6 1",
177 "       c None",
178 ".      c #000000",
179 "+      c #020202",
180 "@      c #FFFFFF",
181 "#      c #010101",
182 "$      c #070707",
183 "           ..++++++++++++++++..              ..++++++++++++++++..",
184 "           ..++++++++++++++++..              ..++++++++++++++++..",
185 "           ..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..",
186 "           ..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..",
187 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
188 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
189 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
190 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
191 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
192 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
193 "..+++++++++##++++++$@@@@@@@@@..   ..+++++++++##++++++$@@@@@@@@@..",
194 "..+++++++++##+++++#+@@@@@@@@@..   ..+++++++++##+++++#+@@@@@@@@@..",
195 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
196 "..@@@@@@@@@@@@@@@@++@@@..@@@@..   ..@@@@@@@@@@@@@@@@++@@@..@@@@..",
197 "..@@@@@@@@@@@@@@@@++@@.@@.@@@..   ..@@@@@@@@@@@@@@@@++@@.@@.@@@..",
198 "..@@@@@@@@@@@@@@@@++@@@@@.@@@..   ..@@@@@@@@@@@@@@@@++@@@@@.@@@..",
199 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
200 "..@@@@@@@@@@@@@@@@++@@@.@@@@@..   ..@@@@@@@@@@@@@@@@++@@@.@@@@@..",
201 "..@@@@@@@@@@@@@@@@++@@.@@@@@@..   ..@@@@@@@@@@@@@@@@++@@.@@@@@@..",
202 "..@@@@@@@@@@@@@@@@++@@....@@@..   ..@@@@@@@@@@@@@@@@++@@....@@@..",
203 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
204 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
205 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
206 "..@@@@@@@@@@@.@@@@.............   ..@@@@@@@@@@@.@@@@.............",
207 "..@@@@@@@@@@..@@@@.............   ..@@@@@@@@@@..@@@@.............",
208 "..@@@@@@@@@@@.@@@@..              ..@@@@@@@@@@@.@@@@..           ",
209 "..@@@@@@@@@@@.@@@@..              ..@@@@@@@@@@@.@@@@..           ",
210 "..@@@@@@@@@@@.@@@@..              ..@@@@@@@@@@@.@@@@..           ",
211 "..@@@@@@@@@@@.@@@@..              ..@@@@@@@@@@@.@@@@..           ",
212 "..@@@@@@@@@@...@@@..              ..@@@@@@@@@@...@@@..           ",
213 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
214 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
215 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
216 "....................              ....................           ",
217 "....................              ....................           "};
218
219 /* XPM */
220 static const char *nocollate_xpm[] = {
221 "65 35 6 1",
222 "       c None",
223 ".      c #000000",
224 "+      c #FFFFFF",
225 "@      c #020202",
226 "#      c #010101",
227 "$      c #070707",
228 "           ....................              ....................",
229 "           ....................              ....................",
230 "           ..++++++++++++++++..              ..++++++++++++++++..",
231 "           ..++++++++++++++++..              ..++++++++++++++++..",
232 "           @@++++++++++++++++..              @@++++++++++++++++..",
233 "           @@++++++++++++++++..              @@++++++++++++++++..",
234 "           @@++++++++++++++++..              @@++++++++++++++++..",
235 "           @@++++++++++++++++..              @@++++++++++++++++..",
236 "           @@++++++++++++++++..              @@++++++++++++++++..",
237 "           @@++++++++++++++++..              @@++++++++++++++++..",
238 "..@@@@@@@@@##@@@@@@$+++++++++..   ..@@@@@@@@@##@@@@@@$+++++++++..",
239 "..@@@@@@@@@##@@@@@#@+++++++++..   ..@@@@@@@@@##@@@@@#@+++++++++..",
240 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
241 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@+++..++++..",
242 "..++++++++++++++++@@+++..++++..   ..++++++++++++++++@@++.++.+++..",
243 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@+++++.+++..",
244 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@++++.++++..",
245 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@+++.+++++..",
246 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@++.++++++..",
247 "..++++++++++++++++@@+++...+++..   ..++++++++++++++++@@++....+++..",
248 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
249 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
250 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
251 "..+++++++++++.++++.............   ..++++++++++..++++.............",
252 "..++++++++++..++++.............   ..+++++++++.++.+++.............",
253 "..+++++++++++.++++..              ..++++++++++++.+++..           ",
254 "..+++++++++++.++++..              ..+++++++++++.++++..           ",
255 "..+++++++++++.++++..              ..++++++++++.+++++..           ",
256 "..+++++++++++.++++..              ..+++++++++.++++++..           ",
257 "..++++++++++...+++..              ..+++++++++....+++..           ",
258 "..++++++++++++++++..              ..++++++++++++++++..           ",
259 "..++++++++++++++++..              ..++++++++++++++++..           ",
260 "..++++++++++++++++..              ..++++++++++++++++..           ",
261 "....................              ....................           ",
262 "....................              ....................           "};
263
264 /* XPM */
265 static const char *collate_reverse_xpm[] = {
266 "65 35 6 1",
267 "       c None",
268 ".      c #000000",
269 "+      c #020202",
270 "@      c #FFFFFF",
271 "#      c #010101",
272 "$      c #070707",
273 "           ..++++++++++++++++..              ..++++++++++++++++..",
274 "           ..++++++++++++++++..              ..++++++++++++++++..",
275 "           ..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..",
276 "           ..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..",
277 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
278 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
279 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
280 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
281 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
282 "           ++@@@@@@@@@@@@@@@@..              ++@@@@@@@@@@@@@@@@..",
283 "..+++++++++##++++++$@@@@@@@@@..   ..+++++++++##++++++$@@@@@@@@@..",
284 "..+++++++++##+++++#+@@@@@@@@@..   ..+++++++++##+++++#+@@@@@@@@@..",
285 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
286 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
287 "..@@@@@@@@@@@@@@@@++@@@..@@@@..   ..@@@@@@@@@@@@@@@@++@@@..@@@@..",
288 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
289 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
290 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
291 "..@@@@@@@@@@@@@@@@++@@@@.@@@@..   ..@@@@@@@@@@@@@@@@++@@@@.@@@@..",
292 "..@@@@@@@@@@@@@@@@++@@@...@@@..   ..@@@@@@@@@@@@@@@@++@@@...@@@..",
293 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
294 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
295 "..@@@@@@@@@@@@@@@@++@@@@@@@@@..   ..@@@@@@@@@@@@@@@@++@@@@@@@@@..",
296 "..@@@@@@@@@@..@@@@.............   ..@@@@@@@@@@..@@@@.............",
297 "..@@@@@@@@@.@@.@@@.............   ..@@@@@@@@@.@@.@@@.............",
298 "..@@@@@@@@@@@@.@@@..              ..@@@@@@@@@@@@.@@@..           ",
299 "..@@@@@@@@@@@.@@@@..              ..@@@@@@@@@@@.@@@@..           ",
300 "..@@@@@@@@@@.@@@@@..              ..@@@@@@@@@@.@@@@@..           ",
301 "..@@@@@@@@@.@@@@@@..              ..@@@@@@@@@.@@@@@@..           ",
302 "..@@@@@@@@@....@@@..              ..@@@@@@@@@....@@@..           ",
303 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
304 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
305 "..@@@@@@@@@@@@@@@@..              ..@@@@@@@@@@@@@@@@..           ",
306 "....................              ....................           ",
307 "....................              ....................           "};
308
309 /* XPM */
310 static const char *nocollate_reverse_xpm[] = {
311 "65 35 6 1",
312 "       c None",
313 ".      c #000000",
314 "+      c #FFFFFF",
315 "@      c #020202",
316 "#      c #010101",
317 "$      c #070707",
318 "           ....................              ....................",
319 "           ....................              ....................",
320 "           ..++++++++++++++++..              ..++++++++++++++++..",
321 "           ..++++++++++++++++..              ..++++++++++++++++..",
322 "           @@++++++++++++++++..              @@++++++++++++++++..",
323 "           @@++++++++++++++++..              @@++++++++++++++++..",
324 "           @@++++++++++++++++..              @@++++++++++++++++..",
325 "           @@++++++++++++++++..              @@++++++++++++++++..",
326 "           @@++++++++++++++++..              @@++++++++++++++++..",
327 "           @@++++++++++++++++..              @@++++++++++++++++..",
328 "..@@@@@@@@@##@@@@@@$+++++++++..   ..@@@@@@@@@##@@@@@@$+++++++++..",
329 "..@@@@@@@@@##@@@@@#@+++++++++..   ..@@@@@@@@@##@@@@@#@+++++++++..",
330 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
331 "..++++++++++++++++@@+++..++++..   ..++++++++++++++++@@++++.++++..",
332 "..++++++++++++++++@@++.++.+++..   ..++++++++++++++++@@+++..++++..",
333 "..++++++++++++++++@@+++++.+++..   ..++++++++++++++++@@++++.++++..",
334 "..++++++++++++++++@@++++.++++..   ..++++++++++++++++@@++++.++++..",
335 "..++++++++++++++++@@+++.+++++..   ..++++++++++++++++@@++++.++++..",
336 "..++++++++++++++++@@++.++++++..   ..++++++++++++++++@@++++.++++..",
337 "..++++++++++++++++@@++....+++..   ..++++++++++++++++@@+++...+++..",
338 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
339 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
340 "..++++++++++++++++@@+++++++++..   ..++++++++++++++++@@+++++++++..",
341 "..++++++++++..++++.............   ..+++++++++++.++++.............",
342 "..+++++++++.++.+++.............   ..++++++++++..++++.............",
343 "..++++++++++++.+++..              ..+++++++++++.++++..           ",
344 "..+++++++++++.++++..              ..+++++++++++.++++..           ",
345 "..++++++++++.+++++..              ..+++++++++++.++++..           ",
346 "..+++++++++.++++++..              ..+++++++++++.++++..           ",
347 "..+++++++++....+++..              ..++++++++++...+++..           ",
348 "..++++++++++++++++..              ..++++++++++++++++..           ",
349 "..++++++++++++++++..              ..++++++++++++++++..           ",
350 "..++++++++++++++++..              ..++++++++++++++++..           ",
351 "....................              ....................           ",
352 "....................              ....................           "};
353
354
355 static gboolean
356 is_default_printer (GtkPrintUnixDialog *dialog,
357                     GtkPrinter *printer)
358 {
359   if (dialog->priv->format_for_printer)
360     return strcmp (dialog->priv->format_for_printer,
361                    gtk_printer_get_name (printer)) == 0;
362  else
363    return gtk_printer_is_default (printer);
364 }
365
366 static void
367 gtk_print_unix_dialog_class_init (GtkPrintUnixDialogClass *class)
368 {
369   GObjectClass *object_class;
370   GtkWidgetClass *widget_class;
371
372   object_class = (GObjectClass *) class;
373   widget_class = (GtkWidgetClass *) class;
374
375   object_class->finalize = gtk_print_unix_dialog_finalize;
376   object_class->set_property = gtk_print_unix_dialog_set_property;
377   object_class->get_property = gtk_print_unix_dialog_get_property;
378
379   g_object_class_install_property (object_class,
380                                    PROP_PAGE_SETUP,
381                                    g_param_spec_object ("page-setup",
382                                                         P_("Page Setup"),
383                                                         P_("The GtkPageSetup to use"),
384                                                         GTK_TYPE_PAGE_SETUP,
385                                                         GTK_PARAM_READWRITE));
386
387   g_object_class_install_property (object_class,
388                                    PROP_CURRENT_PAGE,
389                                    g_param_spec_int ("current-page",
390                                                      P_("Current Page"),
391                                                      P_("The current page in the document"),
392                                                      -1,
393                                                      G_MAXINT,
394                                                      -1,
395                                                      GTK_PARAM_READWRITE));
396
397   g_object_class_install_property (object_class,
398                                    PROP_PRINT_SETTINGS,
399                                    g_param_spec_object ("print-settings",
400                                                         P_("Print Settings"),
401                                                         P_("The GtkPrintSettings used for initializing the dialog"),
402                                                         GTK_TYPE_PRINT_SETTINGS,
403                                                         GTK_PARAM_READWRITE));
404
405   g_object_class_install_property (object_class,
406                                    PROP_SELECTED_PRINTER,
407                                    g_param_spec_object ("selected-printer",
408                                                         P_("Selected Printer"),
409                                                         P_("The GtkPrinter which which is selected"),
410                                                         GTK_TYPE_PRINTER,
411                                                         GTK_PARAM_READABLE));
412   
413   g_type_class_add_private (class, sizeof (GtkPrintUnixDialogPrivate));  
414 }
415
416 static void
417 gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
418 {
419   dialog->priv = GTK_PRINT_UNIX_DIALOG_GET_PRIVATE (dialog); 
420   dialog->priv->print_backends = NULL;
421   dialog->priv->current_page = -1;
422
423   dialog->priv->extention_points = g_hash_table_new (g_str_hash,
424                                                      g_str_equal);
425
426   dialog->priv->page_setup = gtk_page_setup_new ();
427
428   populate_dialog (dialog);
429
430   g_signal_connect (dialog, 
431                     "destroy", 
432                     (GCallback) gtk_print_unix_dialog_destroy, 
433                     NULL);
434
435   gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
436                           GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
437                           GTK_STOCK_PRINT, GTK_RESPONSE_OK,
438                           NULL);
439
440   gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
441   gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
442 }
443
444 static void
445 gtk_print_unix_dialog_destroy (GtkPrintUnixDialog *dialog)
446 {
447   /* Make sure we don't destroy custom widgets owned by the backends */
448   clear_per_printer_ui (dialog);  
449 }
450
451 static void
452 gtk_print_unix_dialog_finalize (GObject *object)
453 {
454   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
455   
456   g_return_if_fail (object != NULL);
457
458   unschedule_idle_mark_conflicts (dialog);
459
460   if (dialog->priv->request_details_tag)
461     {
462       g_source_remove (dialog->priv->request_details_tag);
463       dialog->priv->request_details_tag = 0;
464     }
465   
466   if (dialog->priv->current_printer)
467     {
468       g_object_unref (dialog->priv->current_printer);
469       dialog->priv->current_printer = NULL;
470     }
471
472   if (dialog->priv->printer_list)
473     {
474       g_object_unref (dialog->priv->printer_list);
475       dialog->priv->printer_list = NULL;
476     }
477  
478   if (dialog->priv->printer_list_filter)
479     {
480       g_object_unref (dialog->priv->printer_list_filter);
481       dialog->priv->printer_list_filter = NULL;
482     }
483
484  
485   if (dialog->priv->options)
486     {
487       g_object_unref (dialog->priv->options);
488       dialog->priv->options = NULL;
489     }
490  
491   if (dialog->priv->extention_points)
492     {
493       g_hash_table_unref (dialog->priv->extention_points);
494       dialog->priv->extention_points = NULL;
495     }
496  
497   if (dialog->priv->page_setup)
498     {
499       g_object_unref (dialog->priv->page_setup);
500       dialog->priv->page_setup = NULL;
501     }
502
503   if (dialog->priv->initial_settings)
504     {
505       g_object_unref (dialog->priv->initial_settings);
506       dialog->priv->initial_settings = NULL;
507     }
508
509   g_free (dialog->priv->waiting_for_printer);
510   dialog->priv->waiting_for_printer = NULL;
511   
512   g_free (dialog->priv->format_for_printer);
513   dialog->priv->format_for_printer = NULL;
514   
515   if (G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize)
516     G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
517 }
518
519 static void
520 printer_removed_cb (GtkPrintBackend    *backend, 
521                     GtkPrinter         *printer, 
522                     GtkPrintUnixDialog *dialog)
523 {
524   GtkTreeIter *iter;
525   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
526   gtk_list_store_remove (GTK_LIST_STORE (dialog->priv->printer_list), iter);
527 }
528
529 static void
530 printer_status_cb (GtkPrintBackend    *backend, 
531                    GtkPrinter         *printer, 
532                    GtkPrintUnixDialog *dialog)
533 {
534   GtkTreeIter *iter;
535   iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
536
537   gtk_list_store_set (GTK_LIST_STORE (dialog->priv->printer_list), iter,
538                       PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
539                       PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
540                       PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
541                       PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
542                       -1);
543
544 }
545
546 static void
547 printer_added_cb (GtkPrintBackend    *backend, 
548                   GtkPrinter         *printer, 
549                   GtkPrintUnixDialog *dialog)
550 {
551   GtkTreeIter iter, filter_iter;
552   GtkTreeSelection *selection;
553
554   gtk_list_store_append (GTK_LIST_STORE (dialog->priv->printer_list), &iter);
555   
556   g_object_set_data_full (G_OBJECT (printer), 
557                          "gtk-print-tree-iter", 
558                           gtk_tree_iter_copy (&iter),
559                           (GDestroyNotify) gtk_tree_iter_free);
560
561   gtk_list_store_set (GTK_LIST_STORE (dialog->priv->printer_list), &iter,
562                       PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
563                       PRINTER_LIST_COL_NAME, gtk_printer_get_name (printer),
564                       PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
565                       PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
566                       PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
567                       PRINTER_LIST_COL_PRINTER_OBJ, printer,
568                       -1);
569
570   gtk_tree_model_filter_convert_child_iter_to_iter (dialog->priv->printer_list_filter,
571                                                     &filter_iter, &iter);
572   
573   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->priv->printer_treeview));
574   
575   if (dialog->priv->waiting_for_printer != NULL &&
576       strcmp (gtk_printer_get_name (printer),
577               dialog->priv->waiting_for_printer) == 0)
578     {
579       dialog->priv->internal_printer_change = TRUE;
580       gtk_tree_selection_select_iter (selection, &filter_iter);
581       dialog->priv->internal_printer_change = FALSE;
582       g_free (dialog->priv->waiting_for_printer);
583       dialog->priv->waiting_for_printer = NULL;
584     }
585   else if (is_default_printer (dialog, printer) &&
586            gtk_tree_selection_count_selected_rows (selection) == 0)
587     {
588       dialog->priv->internal_printer_change = TRUE;
589       gtk_tree_selection_select_iter (selection, &filter_iter);
590       dialog->priv->internal_printer_change = FALSE;
591     }
592 }
593
594 static void
595 printer_list_initialize (GtkPrintUnixDialog *dialog,
596                          GtkPrintBackend    *print_backend)
597 {
598   GList *list;
599   GList *node;
600
601   g_return_if_fail (print_backend != NULL);
602
603   g_signal_connect_object (print_backend, 
604                            "printer-added", 
605                            (GCallback) printer_added_cb, 
606                            G_OBJECT (dialog), 0);
607
608   g_signal_connect_object (print_backend, 
609                            "printer-removed", 
610                            (GCallback) printer_removed_cb, 
611                            G_OBJECT (dialog), 0);
612
613   g_signal_connect_object (print_backend, 
614                            "printer-status-changed", 
615                            (GCallback) printer_status_cb, 
616                            G_OBJECT (dialog), 0);
617
618   list = gtk_print_backend_get_printer_list (print_backend);
619
620   node = list;
621   while (node != NULL)
622     {
623       printer_added_cb (print_backend, node->data, dialog);
624       node = node->next;
625     }
626
627   g_list_free (list);
628 }
629
630 static void
631 load_print_backends (GtkPrintUnixDialog *dialog)
632 {
633   GList *node;
634
635   if (g_module_supported ())
636     dialog->priv->print_backends = gtk_print_backend_load_modules ();
637
638   for (node = dialog->priv->print_backends; node != NULL; node = node->next)
639     printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
640 }
641
642 static void
643 gtk_print_unix_dialog_set_property (GObject      *object,
644                                     guint         prop_id,
645                                     const GValue *value,
646                                     GParamSpec   *pspec)
647
648 {
649   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
650
651   switch (prop_id)
652     {
653     case PROP_PAGE_SETUP:
654       gtk_print_unix_dialog_set_page_setup (dialog, g_value_get_object (value));
655       break;
656     case PROP_CURRENT_PAGE:
657       gtk_print_unix_dialog_set_current_page (dialog, g_value_get_int (value));
658       break;
659     case PROP_PRINT_SETTINGS:
660       gtk_print_unix_dialog_set_settings (dialog, g_value_get_object (value));
661       break;
662     default:
663       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
664       break;
665     }
666 }
667
668 static void
669 gtk_print_unix_dialog_get_property (GObject    *object,
670                                     guint       prop_id,
671                                     GValue     *value,
672                                     GParamSpec *pspec)
673 {
674   GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
675
676   switch (prop_id)
677     {
678     case PROP_PAGE_SETUP:
679       g_value_set_object (value, dialog->priv->page_setup);
680       break;
681     case PROP_CURRENT_PAGE:
682       g_value_set_int (value, dialog->priv->current_page);
683       break;
684     case PROP_PRINT_SETTINGS:
685       g_value_set_object (value, gtk_print_unix_dialog_get_settings (dialog));
686       break;
687     case PROP_SELECTED_PRINTER:
688       g_value_set_object (value, dialog->priv->current_printer);
689       break;
690     default:
691       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
692       break;
693     }
694 }
695
696 static gboolean
697 is_printer_active (GtkTreeModel       *model,
698                    GtkTreeIter        *iter,
699                    GtkPrintUnixDialog *dialog)
700 {
701   gboolean result;
702   GtkPrinter *printer;
703
704   gtk_tree_model_get (model,
705                       iter,
706                       PRINTER_LIST_COL_PRINTER_OBJ,
707                       &printer,
708                       -1);
709   
710   if (printer == NULL)
711     return FALSE;
712
713   result = gtk_printer_is_active (printer);
714
715   g_object_unref (printer);
716
717   return result;
718 }
719
720 static gint
721 default_printer_list_sort_func (GtkTreeModel *model,
722                                 GtkTreeIter  *a,
723                                 GtkTreeIter  *b,
724                                 gpointer      user_data)
725 {
726   gchar *a_name;
727   gchar *b_name;
728   GtkPrinter *a_printer;
729   GtkPrinter *b_printer;
730   gint result;
731
732   gtk_tree_model_get (model, a, 
733                       PRINTER_LIST_COL_NAME, &a_name, 
734                       PRINTER_LIST_COL_PRINTER_OBJ, &a_printer,
735                       -1);
736   gtk_tree_model_get (model, b, 
737                       PRINTER_LIST_COL_NAME, &b_name,
738                       PRINTER_LIST_COL_PRINTER_OBJ, &b_printer,
739                       -1);
740
741   if (a_printer == NULL && b_printer == NULL)
742     result = 0;
743   else if (a_printer == NULL)
744    result = G_MAXINT;
745   else if (b_printer == NULL)
746    result = G_MININT;
747   else if (gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
748     result = 0;
749   else if (gtk_printer_is_virtual (a_printer) && !gtk_printer_is_virtual (b_printer))
750     result = G_MININT;
751   else if (!gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
752     result = G_MAXINT;
753   else if (a_name == NULL && b_name == NULL)
754     result = 0;
755   else if (a_name == NULL && b_name != NULL)
756     result = 1;
757   else if (a_name != NULL && b_name == NULL)
758     result = -1;
759   else
760     result = g_ascii_strcasecmp (a_name, b_name);
761
762   g_free (a_name);
763   g_free (b_name);
764   g_object_unref (a_printer);
765   g_object_unref (b_printer);
766
767   return result;
768 }
769
770
771 static void
772 create_printer_list_model (GtkPrintUnixDialog *dialog)
773 {
774   GtkListStore *model;
775   GtkTreeSortable *sort;
776
777   model = gtk_list_store_new (PRINTER_LIST_N_COLS,
778                               G_TYPE_STRING,
779                               G_TYPE_STRING, 
780                               G_TYPE_STRING, 
781                               G_TYPE_INT, 
782                               G_TYPE_STRING,
783                               G_TYPE_OBJECT);
784
785   dialog->priv->printer_list = (GtkTreeModel *)model;
786   dialog->priv->printer_list_filter = (GtkTreeModelFilter *) gtk_tree_model_filter_new ((GtkTreeModel *)model,
787                                                                                         NULL);
788
789   gtk_tree_model_filter_set_visible_func (dialog->priv->printer_list_filter,
790                                           (GtkTreeModelFilterVisibleFunc) is_printer_active,
791                                           dialog,
792                                           NULL);
793
794   sort = GTK_TREE_SORTABLE (model);
795   gtk_tree_sortable_set_default_sort_func (sort,
796                                            default_printer_list_sort_func,
797                                            NULL,
798                                            NULL);
799  
800   gtk_tree_sortable_set_sort_column_id (sort,
801                                         GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
802                                         GTK_SORT_ASCENDING);
803
804 }
805
806
807 static GtkWidget *
808 wrap_in_frame (const gchar *label, 
809                GtkWidget   *child)
810 {
811   GtkWidget *frame, *alignment, *label_widget;
812   char *bold_text;
813
814   label_widget = gtk_label_new ("");
815   gtk_widget_show (label_widget);
816   
817   bold_text = g_markup_printf_escaped ("<b>%s</b>", label);
818   gtk_label_set_markup (GTK_LABEL (label_widget), bold_text);
819   g_free (bold_text);
820   
821   frame = gtk_frame_new ("");
822   gtk_frame_set_label_widget (GTK_FRAME (frame), label_widget);
823   gtk_frame_set_shadow_type (GTK_FRAME (frame), GTK_SHADOW_NONE);
824   
825   alignment = gtk_alignment_new (0.5, 0.5, 1.0, 1.0);
826   gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
827                              12, 0, 12, 0);
828   gtk_container_add (GTK_CONTAINER (frame), alignment);
829
830   gtk_container_add (GTK_CONTAINER (alignment), child);
831
832   gtk_widget_show (frame);
833   gtk_widget_show (alignment);
834   
835   return frame;
836 }
837
838 static gboolean
839 setup_option (GtkPrintUnixDialog     *dialog,
840               const gchar            *option_name,
841               GtkPrinterOptionWidget *widget)
842 {
843   GtkPrinterOption *option;
844
845   option = gtk_printer_option_set_lookup (dialog->priv->options, option_name);
846   gtk_printer_option_widget_set_source (widget, option);
847
848   return option != NULL;
849 }
850
851 static void
852 add_option_to_extention_point (GtkPrinterOption *option,
853                                gpointer          user_data)
854 {
855   GHashTable *extention_points = (GHashTable *) user_data;
856
857   GtkWidget *widget;
858   GtkBox *extention_hbox;
859
860   extention_hbox = (GtkBox *) g_hash_table_lookup (extention_points, option->name);
861
862   if (extention_hbox)
863     {
864
865       widget = gtk_printer_option_widget_new (option);
866       gtk_widget_show (widget);
867    
868       if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
869         {
870           GtkWidget *label;
871
872           label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
873           gtk_widget_show (label);
874           gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
875
876           gtk_box_pack_start (extention_hbox, label, FALSE, FALSE, 6);
877           gtk_box_pack_start (extention_hbox, widget, FALSE, FALSE, 6);
878
879         }
880       else
881         gtk_box_pack_start (extention_hbox, widget, FALSE, FALSE, 6);
882     }
883   else
884     g_warning ("Extention point %s requested but not found.", option->name);
885 }
886
887 static void
888 add_option_to_table (GtkPrinterOption *option,
889                      gpointer          user_data)
890 {
891   GtkTable *table;
892   GtkWidget *label, *widget;
893   int row;
894
895   table = GTK_TABLE (user_data);
896   
897   if (g_str_has_prefix (option->name, "gtk-"))
898     return;
899   
900   widget = gtk_printer_option_widget_new (option);
901   gtk_widget_show (widget);
902
903   row = table->nrows;
904   gtk_table_resize (table, table->nrows + 1, table->ncols + 1);
905   
906   if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
907     {
908       label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
909       gtk_widget_show (label);
910
911       gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
912       
913       gtk_table_attach (table, label,
914                         0, 1, row - 1 , row,  GTK_FILL, 0, 0, 0);
915       
916       gtk_table_attach (table, widget,
917                         1, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
918     }
919   else
920     gtk_table_attach (table, widget,
921                       0, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
922 }
923
924
925 static void
926 setup_page_table (GtkPrinterOptionSet *options,
927                   const gchar         *group,
928                   GtkWidget           *table,
929                   GtkWidget           *page)
930 {
931   gtk_printer_option_set_foreach_in_group (options, group,
932                                            add_option_to_table,
933                                            table);
934   if (GTK_TABLE (table)->nrows == 1)
935     gtk_widget_hide (page);
936   else
937     gtk_widget_show (page);
938 }
939
940 static void
941 update_print_at_option (GtkPrintUnixDialog *dialog)
942 {
943   GtkPrinterOption *option;
944   
945   option = gtk_printer_option_set_lookup (dialog->priv->options, "gtk-print-time");
946
947   if (option == NULL)
948     return;
949   
950   if (dialog->priv->updating_print_at)
951     return;
952   
953   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->print_at_radio)))
954     gtk_printer_option_set (option, "at");
955   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->print_hold_radio)))
956     gtk_printer_option_set (option, "on-hold");
957   else
958     gtk_printer_option_set (option, "now");
959   
960   option = gtk_printer_option_set_lookup (dialog->priv->options, "gtk-print-time-text");
961   if (option != NULL)
962     {
963       const char *text = gtk_entry_get_text (GTK_ENTRY (dialog->priv->print_at_entry));
964       gtk_printer_option_set (option,text);
965     }
966 }
967
968
969 static gboolean
970 setup_print_at (GtkPrintUnixDialog *dialog)
971 {
972   GtkPrinterOption *option;
973   
974   option = gtk_printer_option_set_lookup (dialog->priv->options, "gtk-print-time");
975  
976   if (option == NULL)
977     {
978       gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->print_now_radio),
979                                     TRUE);
980       gtk_widget_set_sensitive (dialog->priv->print_at_radio, FALSE);
981       gtk_widget_set_sensitive (dialog->priv->print_at_entry, FALSE);
982       gtk_widget_set_sensitive (dialog->priv->print_hold_radio, FALSE);
983       gtk_entry_set_text (GTK_ENTRY (dialog->priv->print_at_entry), "");
984       return FALSE;
985     }
986
987   dialog->priv->updating_print_at = TRUE;
988   
989   if (gtk_printer_option_has_choice (option, "at"))
990     {
991       gtk_widget_set_sensitive (dialog->priv->print_at_radio, TRUE);
992       gtk_widget_set_sensitive (dialog->priv->print_at_entry, TRUE);
993     }
994   else
995     {
996       gtk_widget_set_sensitive (dialog->priv->print_at_radio, FALSE);
997       gtk_widget_set_sensitive (dialog->priv->print_at_entry, FALSE);
998     }
999   
1000   gtk_widget_set_sensitive (dialog->priv->print_hold_radio,
1001                             gtk_printer_option_has_choice (option, "on-hold"));
1002
1003   update_print_at_option (dialog);
1004
1005   if (strcmp (option->value, "at") == 0)
1006     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->print_at_radio),
1007                                   TRUE);
1008   else if (strcmp (option->value, "on-hold") == 0)
1009     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->print_hold_radio),
1010                                   TRUE);
1011   else
1012     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->print_now_radio),
1013                                   TRUE);
1014
1015   option = gtk_printer_option_set_lookup (dialog->priv->options, "gtk-print-time-text");
1016   if (option != NULL)
1017     gtk_entry_set_text (GTK_ENTRY (dialog->priv->print_at_entry),
1018                         option->value);
1019   
1020
1021   dialog->priv->updating_print_at = FALSE;
1022
1023   return TRUE;
1024 }
1025              
1026 static void
1027 update_dialog_from_settings (GtkPrintUnixDialog *dialog)
1028 {
1029   GList *groups, *l;
1030   char *group;
1031   GtkWidget *table, *frame;
1032   gboolean has_advanced, has_job;
1033  
1034   if (dialog->priv->current_printer == NULL)
1035     {
1036        clear_per_printer_ui (dialog);
1037        gtk_widget_hide (dialog->priv->job_page);
1038        gtk_widget_hide (dialog->priv->advanced_page);
1039        gtk_widget_hide (dialog->priv->image_quality_page);
1040        gtk_widget_hide (dialog->priv->finishing_page);
1041        gtk_widget_hide (dialog->priv->color_page);
1042        gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1043
1044        return;
1045     }
1046  
1047   setup_option (dialog, "gtk-n-up", dialog->priv->pages_per_sheet);
1048   setup_option (dialog, "gtk-duplex", dialog->priv->duplex);
1049   setup_option (dialog, "gtk-paper-type", dialog->priv->paper_type);
1050   setup_option (dialog, "gtk-paper-source", dialog->priv->paper_source);
1051   setup_option (dialog, "gtk-output-tray", dialog->priv->output_tray);
1052
1053   has_job = FALSE;
1054   has_job |= setup_option (dialog, "gtk-job-prio", dialog->priv->job_prio);
1055   has_job |= setup_option (dialog, "gtk-billing-info", dialog->priv->billing_info);
1056   has_job |= setup_option (dialog, "gtk-cover-before", dialog->priv->cover_before);
1057   has_job |= setup_option (dialog, "gtk-cover-after", dialog->priv->cover_after);
1058   has_job |= setup_print_at (dialog);
1059   
1060   if (has_job)
1061     gtk_widget_show (dialog->priv->job_page);
1062   else
1063     gtk_widget_hide (dialog->priv->job_page);
1064
1065   
1066   setup_page_table (dialog->priv->options,
1067                     "ImageQualityPage",
1068                     dialog->priv->image_quality_table,
1069                     dialog->priv->image_quality_page);
1070   
1071   setup_page_table (dialog->priv->options,
1072                     "FinishingPage",
1073                     dialog->priv->finishing_table,
1074                     dialog->priv->finishing_page);
1075
1076   setup_page_table (dialog->priv->options,
1077                     "ColorPage",
1078                     dialog->priv->color_table,
1079                     dialog->priv->color_page);
1080
1081   /* Put the rest of the groups in the advanced page */
1082   groups = gtk_printer_option_set_get_groups (dialog->priv->options);
1083
1084   has_advanced = FALSE;
1085   for (l = groups; l != NULL; l = l->next)
1086     {
1087       group = l->data;
1088
1089       if (group == NULL)
1090         continue;
1091       
1092       if (strcmp (group, "ImageQualityPage") == 0 ||
1093           strcmp (group, "ColorPage") == 0 ||
1094           strcmp (group, "FinishingPage") == 0)
1095         continue;
1096
1097       if (strcmp (group, "GtkPrintDialogExtention") == 0)
1098         {
1099           gtk_printer_option_set_foreach_in_group (dialog->priv->options,
1100                                                    group,
1101                                                    add_option_to_extention_point,
1102                                                    dialog->priv->extention_points);
1103           continue;
1104         }
1105
1106       table = gtk_table_new (1, 2, FALSE);
1107       gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1108       gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1109       
1110       gtk_printer_option_set_foreach_in_group (dialog->priv->options,
1111                                                group,
1112                                                add_option_to_table,
1113                                                table);
1114       if (GTK_TABLE (table)->nrows == 1)
1115         gtk_widget_destroy (table);
1116       else
1117         {
1118           has_advanced = TRUE;
1119           frame = wrap_in_frame (group, table);
1120           gtk_widget_show (table);
1121           gtk_widget_show (frame);
1122           
1123           gtk_box_pack_start (GTK_BOX (dialog->priv->advanced_vbox),
1124                               frame, FALSE, FALSE, 0);
1125         }
1126     }
1127
1128   if (has_advanced)
1129     gtk_widget_show (dialog->priv->advanced_page);
1130   else
1131     gtk_widget_hide (dialog->priv->advanced_page);
1132
1133   
1134   g_list_foreach (groups, (GFunc) g_free, NULL);
1135   g_list_free (groups);
1136 }
1137
1138 static void
1139 mark_conflicts (GtkPrintUnixDialog *dialog)
1140 {
1141   GtkPrinter *printer;
1142   gboolean have_conflict;
1143
1144   have_conflict = FALSE;
1145
1146   printer = dialog->priv->current_printer;
1147
1148   if (printer)
1149     {
1150
1151       g_signal_handler_block (dialog->priv->options,
1152                               dialog->priv->options_changed_handler);
1153       
1154       gtk_printer_option_set_clear_conflicts (dialog->priv->options);
1155       
1156       have_conflict = _gtk_printer_mark_conflicts (printer,
1157                                                    dialog->priv->options);
1158       
1159       g_signal_handler_unblock (dialog->priv->options,
1160                                 dialog->priv->options_changed_handler);
1161     }
1162
1163   if (have_conflict)
1164     gtk_widget_show (dialog->priv->conflicts_widget);
1165   else
1166     gtk_widget_hide (dialog->priv->conflicts_widget);
1167 }
1168
1169 static gboolean
1170 mark_conflicts_callback (gpointer data)
1171 {
1172   GtkPrintUnixDialog *dialog = data;
1173
1174   dialog->priv->mark_conflicts_id = 0;
1175
1176   mark_conflicts (dialog);
1177
1178   return FALSE;
1179 }
1180
1181 static void
1182 unschedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
1183 {
1184   if (dialog->priv->mark_conflicts_id != 0)
1185     {
1186       g_source_remove (dialog->priv->mark_conflicts_id);
1187       dialog->priv->mark_conflicts_id = 0;
1188     }
1189 }
1190
1191 static void
1192 schedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
1193 {
1194   if (dialog->priv->mark_conflicts_id != 0)
1195     return;
1196
1197   dialog->priv->mark_conflicts_id = g_idle_add (mark_conflicts_callback,
1198                                                 dialog);
1199 }
1200
1201 static void
1202 options_changed_cb (GtkPrintUnixDialog *dialog)
1203 {
1204   schedule_idle_mark_conflicts (dialog);
1205
1206   if (dialog->priv->initial_settings)
1207     {
1208       g_object_unref (dialog->priv->initial_settings);
1209       dialog->priv->initial_settings = NULL;
1210     }
1211
1212   g_free (dialog->priv->waiting_for_printer);
1213   dialog->priv->waiting_for_printer = NULL;
1214 }
1215
1216 static void
1217 remove_custom_widget (GtkWidget    *widget,
1218                       GtkContainer *container)
1219 {
1220   gtk_container_remove (container, widget);
1221 }
1222
1223 static void
1224 extention_point_clear_children (const gchar  *key,
1225                                 GtkContainer *container,
1226                                 gpointer      data)
1227 {
1228   gtk_container_foreach (container,
1229                          (GtkCallback)remove_custom_widget,
1230                          container);
1231 }
1232
1233 static void
1234 clear_per_printer_ui (GtkPrintUnixDialog *dialog)
1235 {
1236   gtk_container_foreach (GTK_CONTAINER (dialog->priv->finishing_table),
1237                          (GtkCallback)gtk_widget_destroy,
1238                          NULL);
1239   gtk_table_resize (GTK_TABLE (dialog->priv->finishing_table), 1, 2);
1240   gtk_container_foreach (GTK_CONTAINER (dialog->priv->image_quality_table),
1241                          (GtkCallback)gtk_widget_destroy,
1242                          NULL);
1243   gtk_table_resize (GTK_TABLE (dialog->priv->image_quality_table), 1, 2);
1244   gtk_container_foreach (GTK_CONTAINER (dialog->priv->color_table),
1245                          (GtkCallback)gtk_widget_destroy,
1246                          NULL);
1247   gtk_table_resize (GTK_TABLE (dialog->priv->color_table), 1, 2);
1248   gtk_container_foreach (GTK_CONTAINER (dialog->priv->advanced_vbox),
1249                          (GtkCallback)gtk_widget_destroy,
1250                          NULL);
1251   g_hash_table_foreach (dialog->priv->extention_points, 
1252                         (GHFunc) extention_point_clear_children, 
1253                         NULL);
1254 }
1255
1256 static void
1257 printer_details_acquired (GtkPrinter         *printer,
1258                           gboolean            success,
1259                           GtkPrintUnixDialog *dialog)
1260 {
1261   dialog->priv->request_details_tag = 0;
1262   
1263   if (success)
1264     {
1265       GtkTreeSelection *selection;
1266       selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->priv->printer_treeview));
1267       
1268       selected_printer_changed (selection, dialog);
1269     }
1270 }
1271
1272 static void
1273 selected_printer_changed (GtkTreeSelection   *selection,
1274                           GtkPrintUnixDialog *dialog)
1275 {
1276   GtkPrinter *printer;
1277   GtkTreeIter iter, filter_iter;
1278
1279   /* Whenever the user selects a printer we stop looking for
1280      the printer specified in the initial settings */
1281   if (dialog->priv->waiting_for_printer &&
1282       !dialog->priv->internal_printer_change)
1283     {
1284       g_free (dialog->priv->waiting_for_printer);
1285       dialog->priv->waiting_for_printer = NULL;
1286     }
1287   
1288   if (dialog->priv->request_details_tag)
1289     {
1290       g_source_remove (dialog->priv->request_details_tag);
1291       dialog->priv->request_details_tag = 0;
1292     }
1293   
1294   printer = NULL;
1295   if (gtk_tree_selection_get_selected (selection, NULL, &filter_iter))
1296     {
1297       gtk_tree_model_filter_convert_iter_to_child_iter (dialog->priv->printer_list_filter,
1298                                                         &iter,
1299                                                         &filter_iter);
1300
1301       gtk_tree_model_get (dialog->priv->printer_list, &iter,
1302                           PRINTER_LIST_COL_PRINTER_OBJ, &printer,
1303                           -1);
1304     }
1305   
1306   if (printer != NULL && !_gtk_printer_has_details (printer))
1307     {
1308       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1309       dialog->priv->request_details_tag =
1310         g_signal_connect (printer, "details-acquired",
1311                           G_CALLBACK (printer_details_acquired), dialog);
1312       _gtk_printer_request_details (printer);
1313       return;
1314     }
1315   
1316   if (printer == dialog->priv->current_printer)
1317     {
1318       if (printer)
1319         g_object_unref (printer);
1320       return;
1321     }
1322
1323   if (dialog->priv->options)
1324     {
1325       g_object_unref (dialog->priv->options);
1326       dialog->priv->options = NULL;  
1327
1328       clear_per_printer_ui (dialog);
1329     }
1330
1331   if (dialog->priv->current_printer)
1332     {
1333       g_object_unref (dialog->priv->current_printer);
1334     }
1335
1336   gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
1337   dialog->priv->current_printer = printer;
1338
1339   if (printer != NULL)
1340     {
1341       dialog->priv->options = _gtk_printer_get_options (printer, dialog->priv->initial_settings,
1342                                                         dialog->priv->page_setup);
1343   
1344       dialog->priv->options_changed_handler = 
1345         g_signal_connect_swapped (dialog->priv->options, "changed", G_CALLBACK (options_changed_cb), dialog);
1346     }
1347
1348   update_dialog_from_settings (dialog);
1349 }
1350
1351 static void
1352 update_collate_icon (GtkToggleButton    *toggle_button,
1353                      GtkPrintUnixDialog *dialog)
1354 {
1355   GdkPixbuf *pixbuf;
1356   gboolean collate, reverse;
1357   const char **xpm;
1358
1359   collate = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->collate_check));
1360   reverse = gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->reverse_check));
1361
1362   if (collate)
1363     {
1364       if (reverse)
1365         xpm = collate_reverse_xpm;
1366       else
1367         xpm = collate_xpm;
1368     }
1369   else
1370     {
1371       if (reverse)
1372         xpm = nocollate_reverse_xpm;
1373       else
1374         xpm = nocollate_xpm;
1375     }
1376   
1377   pixbuf = gdk_pixbuf_new_from_xpm_data (xpm);
1378   gtk_image_set_from_pixbuf (GTK_IMAGE (dialog->priv->collate_image), pixbuf);
1379   g_object_unref (pixbuf);
1380 }
1381
1382 static void
1383 create_main_page (GtkPrintUnixDialog *dialog)
1384 {
1385   GtkPrintUnixDialogPrivate *priv;
1386   GtkWidget *main_vbox, *label, *hbox;
1387   GtkWidget *scrolled, *treeview, *frame, *table;
1388   GtkWidget *entry, *spinbutton;
1389   GtkWidget *radio, *check, *image;
1390   GtkCellRenderer *renderer;
1391   GtkTreeViewColumn *column;
1392   GtkTreeSelection *selection;
1393   GtkWidget *custom_input;
1394   
1395   priv = dialog->priv;
1396
1397   main_vbox = gtk_vbox_new (FALSE, 6);
1398   gtk_widget_show (main_vbox);
1399
1400   scrolled = gtk_scrolled_window_new (NULL, NULL);
1401   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
1402                                   GTK_POLICY_AUTOMATIC,
1403                                   GTK_POLICY_AUTOMATIC);
1404   gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
1405                                        GTK_SHADOW_IN);
1406   gtk_widget_show (scrolled);
1407   gtk_box_pack_start (GTK_BOX (main_vbox), scrolled, TRUE, TRUE, 0);
1408
1409   treeview = gtk_tree_view_new_with_model ((GtkTreeModel *) priv->printer_list_filter);
1410   priv->printer_treeview = treeview;
1411   gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), TRUE);
1412   gtk_tree_view_set_search_column (GTK_TREE_VIEW (treeview), PRINTER_LIST_COL_NAME);
1413   gtk_tree_view_set_enable_search (GTK_TREE_VIEW (treeview), TRUE);
1414   selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
1415   gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
1416   g_signal_connect (selection, "changed", G_CALLBACK (selected_printer_changed), dialog);
1417  
1418   renderer = gtk_cell_renderer_pixbuf_new ();
1419   column = gtk_tree_view_column_new_with_attributes ("",
1420                                                      renderer,
1421                                                      "icon-name",
1422                                                      PRINTER_LIST_COL_ICON,
1423                                                      NULL);
1424   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1425
1426  
1427   renderer = gtk_cell_renderer_text_new ();
1428   column = gtk_tree_view_column_new_with_attributes (_("Printer"),
1429                                                      renderer,
1430                                                      "text",
1431                                                      PRINTER_LIST_COL_NAME,
1432                                                      NULL);
1433   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1434   
1435   renderer = gtk_cell_renderer_text_new ();
1436   column = gtk_tree_view_column_new_with_attributes (_("Location"),
1437                                                      renderer,
1438                                                      "text",
1439                                                      PRINTER_LIST_COL_LOCATION,
1440                                                      NULL);
1441   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1442
1443   renderer = gtk_cell_renderer_text_new ();
1444   g_object_set (renderer, "ellipsize", PANGO_ELLIPSIZE_END, NULL);
1445   column = gtk_tree_view_column_new_with_attributes (_("Status"),
1446                                                      renderer,
1447                                                      "text",
1448                                                      PRINTER_LIST_COL_STATE,
1449                                                      NULL);
1450   gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);
1451   
1452   gtk_widget_show (treeview);
1453   gtk_container_add (GTK_CONTAINER (scrolled), treeview);
1454
1455   custom_input = gtk_hbox_new (FALSE, 8);
1456   gtk_widget_show (custom_input);
1457   gtk_box_pack_start (GTK_BOX (main_vbox), custom_input, FALSE, FALSE, 0);
1458   g_hash_table_insert (dialog->priv->extention_points, 
1459                        _EXTENTION_POINT_MAIN_PAGE_CUSTOM_INPUT,
1460                        custom_input);
1461
1462   hbox = gtk_hbox_new (FALSE, 8);
1463   gtk_widget_show (hbox);
1464   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, FALSE, FALSE, 0);
1465
1466   table = gtk_table_new (3, 2, FALSE);
1467   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1468   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1469   frame = wrap_in_frame (_("Print Pages"), table);
1470   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 6);
1471   gtk_widget_show (table);
1472
1473   radio = gtk_radio_button_new_with_label (NULL, _("All"));
1474   priv->all_pages_radio = radio;
1475   gtk_widget_show (radio);
1476   gtk_table_attach (GTK_TABLE (table), radio,
1477                     0, 1, 0, 1,  GTK_FILL, 0,
1478                     0, 0);
1479   radio = gtk_radio_button_new_with_label (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
1480                                            _("Current"));
1481   if (dialog->priv->current_page == -1)
1482     gtk_widget_set_sensitive (radio, FALSE);    
1483   priv->current_page_radio = radio;
1484   gtk_widget_show (radio);
1485   gtk_table_attach (GTK_TABLE (table), radio,
1486                     0, 1, 1, 2,  GTK_FILL, 0,
1487                     0, 0);
1488   radio = gtk_radio_button_new_with_label (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
1489                                            _("Range: "));
1490   priv->page_range_radio = radio;
1491   gtk_widget_show (radio);
1492   gtk_table_attach (GTK_TABLE (table), radio,
1493                     0, 1, 2, 3,  GTK_FILL, 0,
1494                     0, 0);
1495   entry = gtk_entry_new ();
1496   priv->page_range_entry = entry;
1497   gtk_widget_show (entry);
1498   gtk_table_attach (GTK_TABLE (table), entry,
1499                     1, 2, 2, 3,  GTK_FILL, 0,
1500                     0, 0);
1501
1502   table = gtk_table_new (3, 2, FALSE);
1503   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1504   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1505   frame = wrap_in_frame (_("Copies"), table);
1506   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 6);
1507   gtk_widget_show (table);
1508
1509   label = gtk_label_new (_("Copies:"));
1510   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1511   gtk_widget_show (label);
1512   gtk_table_attach (GTK_TABLE (table), label,
1513                     0, 1, 0, 1,  GTK_FILL, 0,
1514                     0, 0);
1515   spinbutton = gtk_spin_button_new_with_range (1.0, 100.0, 1.0);
1516   priv->copies_spin = spinbutton;
1517   gtk_widget_show (spinbutton);
1518   gtk_table_attach (GTK_TABLE (table), spinbutton,
1519                     1, 2, 0, 1,  GTK_FILL, 0,
1520                     0, 0);
1521
1522   check = gtk_check_button_new_with_mnemonic (_("_Collate"));
1523   priv->collate_check = check;
1524   g_signal_connect (check, "toggled", G_CALLBACK (update_collate_icon), dialog);
1525   gtk_widget_show (check);
1526   gtk_table_attach (GTK_TABLE (table), check,
1527                     0, 1, 1, 2,  GTK_FILL, 0,
1528                     0, 0);
1529   check = gtk_check_button_new_with_mnemonic (_("_Reverse"));
1530   g_signal_connect (check, "toggled", G_CALLBACK (update_collate_icon), dialog);
1531   priv->reverse_check = check;
1532   gtk_widget_show (check);
1533   gtk_table_attach (GTK_TABLE (table), check,
1534                     0, 1, 2, 3,  GTK_FILL, 0,
1535                     0, 0);
1536
1537   image = gtk_image_new ();
1538   dialog->priv->collate_image = image;
1539   gtk_widget_show (image);
1540   gtk_table_attach (GTK_TABLE (table), image,
1541                     1, 2, 1, 3, GTK_FILL, 0,
1542                     0, 0);
1543
1544   update_collate_icon (NULL, dialog);
1545   
1546   label = gtk_label_new (_("General"));
1547   gtk_widget_show (label);
1548   
1549   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
1550                             main_vbox, label);
1551   
1552 }
1553
1554 static gboolean
1555 is_range_separator (gchar c)
1556 {
1557   return (c == ',' || c == ';' || c == ':');
1558 }
1559
1560 static GtkPageRange *
1561 dialog_get_page_ranges (GtkPrintUnixDialog *dialog,
1562                         gint               *n_ranges_out)
1563 {
1564   int i, n_ranges;
1565   const char *text, *p;
1566   char *next;
1567   GtkPageRange *ranges;
1568   int start, end;
1569   
1570   text = gtk_entry_get_text (GTK_ENTRY (dialog->priv->page_range_entry));
1571
1572   if (*text == 0)
1573     {
1574       *n_ranges_out = 0;
1575       return NULL;
1576     }
1577   
1578   n_ranges = 1;
1579   p = text;
1580   while (*p)
1581     {
1582       if (is_range_separator (*p))
1583         n_ranges++;
1584       p++;
1585     }
1586
1587   ranges = g_new0 (GtkPageRange, n_ranges);
1588   
1589   i = 0;
1590   p = text;
1591   while (*p)
1592     {
1593       start = (int)strtol (p, &next, 10);
1594       if (start < 1)
1595         start = 1;
1596       end = start;
1597
1598       if (next != p)
1599         {
1600           p = next;
1601
1602           if (*p == '-')
1603             {
1604               p++;
1605               end = (int)strtol (p, NULL, 10);
1606               if (end < start)
1607                 end = start;
1608             }
1609         }
1610
1611       ranges[i].start = start - 1;
1612       ranges[i].end = end - 1;
1613       i++;
1614
1615       /* Skip until end or separator */
1616       while (*p && !is_range_separator (*p))
1617         p++;
1618
1619       /* if not at end, skip separator */
1620       if (*p)
1621         p++;
1622     }
1623
1624   *n_ranges_out = i;
1625   
1626   return ranges;
1627 }
1628
1629 static void
1630 dialog_set_page_ranges (GtkPrintUnixDialog *dialog,
1631                         GtkPageRange       *ranges,
1632                         gint                n_ranges)
1633 {
1634   int i;
1635   GString *s = g_string_new ("");
1636
1637   for (i = 0; i < n_ranges; i++)
1638     {
1639       g_string_append_printf (s, "%d", ranges[i].start + 1);
1640       if (ranges[i].end > ranges[i].start)
1641         g_string_append_printf (s, "-%d", ranges[i].end + 1);
1642       
1643       if (i != n_ranges - 1)
1644         g_string_append (s, ",");
1645     }
1646
1647   gtk_entry_set_text (GTK_ENTRY (dialog->priv->page_range_entry),
1648                       s->str);
1649   
1650   g_string_free (s, TRUE);
1651 }
1652
1653
1654 static GtkPrintPages
1655 dialog_get_print_pages (GtkPrintUnixDialog *dialog)
1656 {
1657   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1658   
1659   if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->all_pages_radio)))
1660     return GTK_PRINT_PAGES_ALL;
1661   else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->current_page_radio)))
1662     return GTK_PRINT_PAGES_CURRENT;
1663   else
1664     return GTK_PRINT_PAGES_RANGES;
1665 }
1666
1667 static void
1668 dialog_set_print_pages (GtkPrintUnixDialog *dialog, GtkPrintPages pages)
1669 {
1670   GtkPrintUnixDialogPrivate *priv = dialog->priv;
1671
1672   if (pages == GTK_PRINT_PAGES_RANGES)
1673     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->page_range_radio), TRUE);
1674   else if (pages == GTK_PRINT_PAGES_CURRENT)
1675     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->current_page_radio), TRUE);
1676   else
1677     gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->all_pages_radio), TRUE);
1678 }
1679
1680 static gdouble
1681 dialog_get_scale (GtkPrintUnixDialog *dialog)
1682 {
1683   return gtk_spin_button_get_value (GTK_SPIN_BUTTON (dialog->priv->scale_spin));
1684 }
1685
1686 static void
1687 dialog_set_scale (GtkPrintUnixDialog *dialog, 
1688                   gdouble             val)
1689 {
1690   return gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->scale_spin),
1691                                     val);
1692 }
1693
1694 static GtkPageSet
1695 dialog_get_page_set (GtkPrintUnixDialog *dialog)
1696 {
1697   return (GtkPageSet)gtk_combo_box_get_active (GTK_COMBO_BOX (dialog->priv->page_set_combo));
1698 }
1699
1700 static void
1701 dialog_set_page_set (GtkPrintUnixDialog *dialog, 
1702                      GtkPageSet          val)
1703 {
1704   gtk_combo_box_set_active (GTK_COMBO_BOX (dialog->priv->page_set_combo),
1705                             (int)val);
1706 }
1707
1708 static gint
1709 dialog_get_n_copies (GtkPrintUnixDialog *dialog)
1710 {
1711   return gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON (dialog->priv->copies_spin));
1712 }
1713
1714 static void
1715 dialog_set_n_copies (GtkPrintUnixDialog *dialog, 
1716                      gint                n_copies)
1717 {
1718   gtk_spin_button_set_value (GTK_SPIN_BUTTON (dialog->priv->copies_spin),
1719                              n_copies);
1720 }
1721
1722 static gboolean
1723 dialog_get_collate (GtkPrintUnixDialog *dialog)
1724 {
1725   return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->collate_check));
1726 }
1727
1728 static void
1729 dialog_set_collate (GtkPrintUnixDialog *dialog, 
1730                     gboolean            collate)
1731 {
1732   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->collate_check),
1733                                 collate);
1734 }
1735
1736 static gboolean
1737 dialog_get_reverse (GtkPrintUnixDialog *dialog)
1738 {
1739   return gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (dialog->priv->reverse_check));
1740 }
1741
1742 static void
1743 dialog_set_reverse (GtkPrintUnixDialog *dialog, 
1744                     gboolean            reverse)
1745 {
1746   gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (dialog->priv->reverse_check),
1747                                 reverse);
1748 }
1749
1750 static gint 
1751 dialog_get_pages_per_sheet (GtkPrintUnixDialog *dialog)
1752 {
1753   const char *val;
1754   int num;
1755
1756   val = gtk_printer_option_widget_get_value (dialog->priv->pages_per_sheet);
1757
1758   num = 1;
1759   
1760   if (val)
1761     {
1762       num = atoi(val);
1763       if (num < 1)
1764         num = 1;
1765     }
1766   
1767   return num;
1768 }
1769
1770
1771 static gboolean
1772 draw_page_cb (GtkWidget          *widget,
1773               GdkEventExpose     *event,
1774               GtkPrintUnixDialog *dialog)
1775 {
1776   cairo_t *cr;
1777   double ratio;
1778   int w, h, tmp, shadow_offset;
1779   int pages_x, pages_y, i, x, y, layout_w, layout_h;
1780   double page_width, page_height;
1781   GtkPageOrientation orientation;
1782   gboolean landscape;
1783   PangoLayout *layout;
1784   PangoFontDescription *font;
1785   char *text;
1786   
1787   orientation = gtk_page_setup_get_orientation (dialog->priv->page_setup);
1788   landscape =
1789     (orientation == GTK_PAGE_ORIENTATION_LANDSCAPE) ||
1790     (orientation == GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE);
1791   
1792   cr = gdk_cairo_create (widget->window);
1793   
1794   ratio = 1.4142;
1795
1796   w = (EXAMPLE_PAGE_AREA_SIZE - 3) / ratio;
1797   h = w * ratio;
1798
1799   switch (dialog_get_pages_per_sheet (dialog))
1800     {
1801     default:
1802     case 1:
1803       pages_x = 1; pages_y = 1;
1804       break;
1805     case 2:
1806       landscape = !landscape;
1807       pages_x = 1; pages_y = 2;
1808       break;
1809     case 4:
1810       pages_x = 2; pages_y = 2;
1811       break;
1812     case 6:
1813       landscape = !landscape;
1814       pages_x = 2; pages_y = 3;
1815       break;
1816     case 9:
1817       pages_x = 3; pages_y = 3;
1818       break;
1819     case 16:
1820       pages_x = 4; pages_y = 4;
1821       break;
1822     }
1823
1824   if (landscape)
1825     {
1826       tmp = w;
1827       w = h;
1828       h = tmp;
1829
1830       tmp = pages_x;
1831       pages_x = pages_y;
1832       pages_y = tmp;
1833     }
1834   
1835   shadow_offset = 3;
1836   
1837   cairo_set_source_rgba (cr, 0.0, 0.0, 0.0, 0.5);
1838   cairo_rectangle (cr, shadow_offset + 1, shadow_offset + 1, w, h);
1839   cairo_fill (cr);
1840   
1841   cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
1842   cairo_rectangle (cr, 1, 1, w, h);
1843   cairo_fill (cr);
1844   cairo_set_line_width (cr, 1.0);
1845   cairo_rectangle (cr, 0.5, 0.5, w+1, h+1);
1846   
1847   cairo_set_source_rgb (cr, 0.0, 0.0, 0.0);
1848   cairo_stroke (cr);
1849
1850   i = 1;
1851
1852   page_width = (double)w / pages_x;
1853   page_height = (double)h / pages_y;
1854
1855   layout  = pango_cairo_create_layout (cr);
1856
1857   font = pango_font_description_new ();
1858   pango_font_description_set_family (font, "sans");
1859   pango_font_description_set_absolute_size (font, page_height * 0.4 * PANGO_SCALE);
1860   pango_layout_set_font_description (layout, font);
1861   pango_font_description_free (font);
1862
1863   pango_layout_set_width (layout, page_width * PANGO_SCALE);
1864   pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
1865   
1866   for (y = 0; y < pages_y; y++)
1867     {
1868       for (x = 0; x < pages_x; x++)
1869         {
1870           text = g_strdup_printf ("%d", i++);
1871           pango_layout_set_text (layout, text, -1);
1872           g_free (text);
1873           pango_layout_get_size (layout, &layout_w, &layout_h);
1874           cairo_save (cr);
1875           cairo_translate (cr,
1876                            x * page_width,
1877                            y * page_height + (page_height - layout_h / 1024.0) / 2
1878                            );
1879           
1880           pango_cairo_show_layout (cr, layout);
1881           cairo_restore (cr);
1882         }
1883     }
1884     
1885   return TRUE;
1886 }
1887
1888 static void
1889 redraw_page_layout_preview (GtkPrintUnixDialog *dialog)
1890 {
1891   if (dialog->priv->page_layout_preview)
1892     gtk_widget_queue_draw (dialog->priv->page_layout_preview);
1893 }
1894
1895 static void
1896 create_page_setup_page (GtkPrintUnixDialog *dialog)
1897 {
1898   GtkPrintUnixDialogPrivate *priv;
1899   GtkWidget *main_vbox, *label, *hbox, *hbox2;
1900   GtkWidget *frame, *table, *widget;
1901   GtkWidget *combo, *spinbutton, *draw;
1902   
1903   priv = dialog->priv;
1904
1905   main_vbox = gtk_vbox_new (FALSE, 8);
1906   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
1907   gtk_widget_show (main_vbox);
1908
1909   hbox = gtk_hbox_new (FALSE, 8);
1910   gtk_widget_show (hbox);
1911   gtk_box_pack_start (GTK_BOX (main_vbox), hbox, TRUE, TRUE, 0);
1912
1913   table = gtk_table_new (5, 2, FALSE);
1914   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1915   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1916   frame = wrap_in_frame (_("Layout"), table);
1917   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 6);
1918   gtk_widget_show (table);
1919
1920   label = gtk_label_new (_("Pages per sheet:"));
1921   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1922   gtk_widget_show (label);
1923   gtk_table_attach (GTK_TABLE (table), label,
1924                     0, 1, 0, 1,  GTK_FILL, 0,
1925                     0, 0);
1926
1927   widget = gtk_printer_option_widget_new (NULL);
1928   g_signal_connect_swapped (widget, "changed", G_CALLBACK (redraw_page_layout_preview), dialog);
1929   priv->pages_per_sheet = GTK_PRINTER_OPTION_WIDGET (widget);
1930   gtk_widget_show (widget);
1931   gtk_table_attach (GTK_TABLE (table), widget,
1932                     1, 2, 0, 1,  GTK_FILL, 0,
1933                     0, 0);
1934
1935   label = gtk_label_new (_("Two-sided:"));
1936   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1937   gtk_widget_show (label);
1938   gtk_table_attach (GTK_TABLE (table), label,
1939                     0, 1, 1, 2,  GTK_FILL, 0,
1940                     0, 0);
1941
1942   widget = gtk_printer_option_widget_new (NULL);
1943   priv->duplex = GTK_PRINTER_OPTION_WIDGET (widget);
1944   gtk_widget_show (widget);
1945   gtk_table_attach (GTK_TABLE (table), widget,
1946                     1, 2, 1, 2,  GTK_FILL, 0,
1947                     0, 0);
1948
1949   label = gtk_label_new (_("Only Print:"));
1950   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1951   gtk_widget_show (label);
1952   gtk_table_attach (GTK_TABLE (table), label,
1953                     0, 1, 2, 3,  GTK_FILL, 0,
1954                     0, 0);
1955
1956   combo = gtk_combo_box_new_text ();
1957   priv->page_set_combo = combo;
1958   gtk_widget_show (combo);
1959   gtk_table_attach (GTK_TABLE (table), combo,
1960                     1, 2, 2, 3,  GTK_FILL, 0,
1961                     0, 0);
1962   /* In enum order */
1963   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("All sheets"));  
1964   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Even sheets"));  
1965   gtk_combo_box_append_text (GTK_COMBO_BOX (combo), _("Odd sheets"));  
1966   gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
1967
1968   label = gtk_label_new (_("Scale:"));
1969   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1970   gtk_widget_show (label);
1971   gtk_table_attach (GTK_TABLE (table), label,
1972                     0, 1, 3, 4,  GTK_FILL, 0,
1973                     0, 0);
1974
1975   hbox2 = gtk_hbox_new (FALSE, 0);
1976   gtk_widget_show (hbox2);
1977   gtk_table_attach (GTK_TABLE (table), hbox2,
1978                     1, 2, 3, 4,  GTK_FILL, 0,
1979                     0, 0);
1980   
1981   spinbutton = gtk_spin_button_new_with_range (1.0, 1000.0, 1.0);
1982   priv->scale_spin = spinbutton;
1983   gtk_spin_button_set_digits (GTK_SPIN_BUTTON (spinbutton), 1);
1984   gtk_spin_button_set_value (GTK_SPIN_BUTTON (spinbutton), 100.0);
1985   gtk_widget_show (spinbutton);
1986   gtk_box_pack_start (GTK_BOX (hbox2), spinbutton, FALSE, FALSE, 0);
1987   label = gtk_label_new ("%");
1988   gtk_widget_show (label);
1989   gtk_box_pack_start (GTK_BOX (hbox2), label, FALSE, FALSE, 0);
1990
1991
1992   table = gtk_table_new (4, 2, FALSE);
1993   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1994   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1995   frame = wrap_in_frame (_("Paper"), table);
1996   gtk_box_pack_start (GTK_BOX (hbox), frame, TRUE, TRUE, 6);
1997   gtk_widget_show (table);
1998
1999   label = gtk_label_new (_("Paper Type:"));
2000   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2001   gtk_widget_show (label);
2002   gtk_table_attach (GTK_TABLE (table), label,
2003                     0, 1, 0, 1,  GTK_FILL, 0,
2004                     0, 0);
2005
2006   widget = gtk_printer_option_widget_new (NULL);
2007   priv->paper_type = GTK_PRINTER_OPTION_WIDGET (widget);
2008   gtk_widget_show (widget);
2009   gtk_table_attach (GTK_TABLE (table), widget,
2010                     1, 2, 0, 1,  GTK_FILL, 0,
2011                     0, 0);
2012
2013   label = gtk_label_new (_("Paper Source:"));
2014   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2015   gtk_widget_show (label);
2016   gtk_table_attach (GTK_TABLE (table), label,
2017                     0, 1, 1, 2,  GTK_FILL, 0,
2018                     0, 0);
2019
2020   widget = gtk_printer_option_widget_new (NULL);
2021   priv->paper_source = GTK_PRINTER_OPTION_WIDGET (widget);
2022   gtk_widget_show (widget);
2023   gtk_table_attach (GTK_TABLE (table), widget,
2024                     1, 2, 1, 2,  GTK_FILL, 0,
2025                     0, 0);
2026
2027   label = gtk_label_new (_("Output Tray:"));
2028   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2029   gtk_widget_show (label);
2030   gtk_table_attach (GTK_TABLE (table), label,
2031                     0, 1, 2, 3,  GTK_FILL, 0,
2032                     0, 0);
2033
2034   widget = gtk_printer_option_widget_new (NULL);
2035   priv->output_tray = GTK_PRINTER_OPTION_WIDGET (widget);
2036   gtk_widget_show (widget);
2037   gtk_table_attach (GTK_TABLE (table), widget,
2038                     1, 2, 2, 3,  GTK_FILL, 0,
2039                     0, 0);
2040
2041   hbox2 = gtk_hbox_new (FALSE, 0);
2042   gtk_widget_show (hbox2);
2043   gtk_box_pack_start (GTK_BOX (main_vbox), hbox2, TRUE, TRUE, 6);
2044
2045   draw = gtk_drawing_area_new ();
2046   dialog->priv->page_layout_preview = draw;
2047   gtk_widget_set_size_request (draw, 200, 200);
2048   g_signal_connect (draw, "expose_event", G_CALLBACK (draw_page_cb), dialog);
2049   gtk_widget_show (draw);
2050
2051   gtk_box_pack_start (GTK_BOX (hbox2), draw, TRUE, FALSE, 6);
2052   
2053   label = gtk_label_new (_("Page Setup"));
2054   gtk_widget_show (label);
2055   
2056   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
2057                             main_vbox, label);
2058   
2059 }
2060
2061 static void
2062 create_job_page (GtkPrintUnixDialog *dialog)
2063 {
2064   GtkPrintUnixDialogPrivate *priv;
2065   GtkWidget *main_table, *label;
2066   GtkWidget *frame, *table, *radio;
2067   GtkWidget *entry, *widget;
2068   
2069   priv = dialog->priv;
2070
2071   main_table = gtk_table_new (2, 2, FALSE);
2072   gtk_container_set_border_width (GTK_CONTAINER (main_table), 6);
2073
2074   table = gtk_table_new (2, 2, FALSE);
2075   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2076   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2077   frame = wrap_in_frame (_("Job Details"), table);
2078   gtk_table_attach (GTK_TABLE (main_table), frame,
2079                     0, 1, 0, 1,  0, 0,
2080                     0, 0);
2081   gtk_widget_show (table);
2082
2083   label = gtk_label_new (_("Priority:"));
2084   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2085   gtk_widget_show (label);
2086   gtk_table_attach (GTK_TABLE (table), label,
2087                     0, 1, 0, 1,  GTK_FILL, 0,
2088                     0, 0);
2089
2090   widget = gtk_printer_option_widget_new (NULL);
2091   priv->job_prio = GTK_PRINTER_OPTION_WIDGET (widget);
2092   gtk_widget_show (widget);
2093   gtk_table_attach (GTK_TABLE (table), widget,
2094                     1, 2, 0, 1,  GTK_FILL, 0,
2095                     0, 0);
2096
2097   label = gtk_label_new (_("Billing info:"));
2098   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2099   gtk_widget_show (label);
2100   gtk_table_attach (GTK_TABLE (table), label,
2101                     0, 1, 1, 2,  GTK_FILL, 0,
2102                     0, 0);
2103
2104   widget = gtk_printer_option_widget_new (NULL);
2105   priv->billing_info = GTK_PRINTER_OPTION_WIDGET (widget);
2106   gtk_widget_show (widget);
2107   gtk_table_attach (GTK_TABLE (table), widget,
2108                     1, 2, 1, 2,  GTK_FILL, 0,
2109                     0, 0);
2110
2111
2112   table = gtk_table_new (2, 2, FALSE);
2113   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2114   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2115   frame = wrap_in_frame (_("Print Document"), table);
2116   gtk_table_attach (GTK_TABLE (main_table), frame,
2117                     0, 1, 1, 2,  0, 0,
2118                     0, 0);
2119   gtk_widget_show (table);
2120
2121   radio = gtk_radio_button_new_with_label (NULL, _("Now"));
2122   priv->print_now_radio = radio;
2123   gtk_widget_show (radio);
2124   gtk_table_attach (GTK_TABLE (table), radio,
2125                     0, 1, 0, 1,  GTK_FILL, 0,
2126                     0, 0);
2127   radio = gtk_radio_button_new_with_label (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
2128                                            _("At:"));
2129   priv->print_at_radio = radio;
2130   gtk_widget_show (radio);
2131   gtk_table_attach (GTK_TABLE (table), radio,
2132                     0, 1, 1, 2,  GTK_FILL, 0,
2133                     0, 0);
2134   radio = gtk_radio_button_new_with_label (gtk_radio_button_get_group (GTK_RADIO_BUTTON (radio)),
2135                                            _("On Hold"));
2136   priv->print_hold_radio = radio;
2137   gtk_widget_show (radio);
2138   gtk_table_attach (GTK_TABLE (table), radio,
2139                     0, 1, 2, 3,  GTK_FILL, 0,
2140                     0, 0);
2141   entry = gtk_entry_new ();
2142   priv->print_at_entry = entry;
2143   gtk_widget_show (entry);
2144   gtk_table_attach (GTK_TABLE (table), entry,
2145                     1, 2, 1, 2,  GTK_FILL, 0,
2146                     0, 0);
2147
2148   g_signal_connect_swapped (priv->print_now_radio, "toggled",
2149                             G_CALLBACK (update_print_at_option), dialog);
2150   g_signal_connect_swapped (priv->print_at_radio, "toggled",
2151                             G_CALLBACK (update_print_at_option), dialog);
2152   g_signal_connect_swapped (priv->print_at_entry, "changed",
2153                             G_CALLBACK (update_print_at_option), dialog);
2154   g_signal_connect_swapped (priv->print_hold_radio, "toggled",
2155                             G_CALLBACK (update_print_at_option), dialog);
2156
2157   table = gtk_table_new (2, 2, FALSE);
2158   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2159   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2160   frame = wrap_in_frame (_("Add Cover Page"), table);
2161   gtk_table_attach (GTK_TABLE (main_table), frame,
2162                     1, 2, 0, 1,  0, 0,
2163                     0, 0);
2164   gtk_widget_show (table);
2165
2166   label = gtk_label_new (_("Before:"));
2167   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2168   gtk_widget_show (label);
2169   gtk_table_attach (GTK_TABLE (table), label,
2170                     0, 1, 0, 1,  GTK_FILL, 0,
2171                     0, 0);
2172
2173   widget = gtk_printer_option_widget_new (NULL);
2174   priv->cover_before = GTK_PRINTER_OPTION_WIDGET (widget);
2175   gtk_widget_show (widget);
2176   gtk_table_attach (GTK_TABLE (table), widget,
2177                     1, 2, 0, 1,  GTK_FILL, 0,
2178                     0, 0);
2179
2180   label = gtk_label_new (_("After:"));
2181   gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
2182   gtk_widget_show (label);
2183   gtk_table_attach (GTK_TABLE (table), label,
2184                     0, 1, 1, 2,  GTK_FILL, 0,
2185                     0, 0);
2186
2187   widget = gtk_printer_option_widget_new (NULL);
2188   priv->cover_after = GTK_PRINTER_OPTION_WIDGET (widget);
2189   gtk_widget_show (widget);
2190   gtk_table_attach (GTK_TABLE (table), widget,
2191                     1, 2, 1, 2,  GTK_FILL, 0,
2192                     0, 0);
2193
2194   label = gtk_label_new (_("Job"));
2195   gtk_widget_show (label);
2196
2197   priv->job_page = main_table;
2198   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
2199                             main_table, label);
2200 }
2201
2202 static void 
2203 create_optional_page (GtkPrintUnixDialog  *dialog,
2204                       const gchar         *text,
2205                       GtkWidget          **table_out,
2206                       GtkWidget          **page_out)
2207 {
2208   GtkPrintUnixDialogPrivate *priv;
2209   GtkWidget *table, *label, *scrolled;
2210   
2211   priv = dialog->priv;
2212
2213   scrolled = gtk_scrolled_window_new (NULL, NULL);
2214   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
2215                                   GTK_POLICY_NEVER,
2216                                   GTK_POLICY_AUTOMATIC);
2217   
2218   table = gtk_table_new (1, 2, FALSE);
2219   gtk_table_set_row_spacings (GTK_TABLE (table), 6);
2220   gtk_table_set_col_spacings (GTK_TABLE (table), 12);
2221   gtk_container_set_border_width (GTK_CONTAINER (table), 6);
2222   gtk_widget_show (table);
2223
2224   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled),
2225                                          table);
2226   gtk_viewport_set_shadow_type (GTK_VIEWPORT (GTK_BIN(scrolled)->child),
2227                                 GTK_SHADOW_NONE);
2228   
2229   label = gtk_label_new (text);
2230   gtk_widget_show (label);
2231   
2232   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
2233                             scrolled, label);
2234
2235   *table_out = table;
2236   *page_out = scrolled;
2237 }
2238
2239 static void
2240 create_advanced_page (GtkPrintUnixDialog *dialog)
2241 {
2242   GtkPrintUnixDialogPrivate *priv;
2243   GtkWidget *main_vbox, *label, *scrolled;
2244   
2245   priv = dialog->priv;
2246
2247   scrolled = gtk_scrolled_window_new (NULL, NULL);
2248   priv->advanced_page = scrolled;
2249   gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
2250                                   GTK_POLICY_NEVER,
2251                                   GTK_POLICY_AUTOMATIC);
2252
2253   main_vbox = gtk_vbox_new (FALSE, 8);
2254   gtk_container_set_border_width (GTK_CONTAINER (main_vbox), 6);
2255   gtk_widget_show (main_vbox);
2256
2257   gtk_scrolled_window_add_with_viewport (GTK_SCROLLED_WINDOW (scrolled),
2258                                          main_vbox);
2259   gtk_viewport_set_shadow_type (GTK_VIEWPORT (GTK_BIN(scrolled)->child),
2260                                 GTK_SHADOW_NONE);
2261   
2262   dialog->priv->advanced_vbox = main_vbox;
2263   
2264   label = gtk_label_new (_("Advanced"));
2265   gtk_widget_show (label);
2266   
2267   gtk_notebook_append_page (GTK_NOTEBOOK (priv->notebook),
2268                             scrolled, label);
2269 }
2270
2271
2272 static void
2273 populate_dialog (GtkPrintUnixDialog *dialog)
2274 {
2275   GtkPrintUnixDialogPrivate *priv;
2276   GtkWidget *hbox, *conflict_hbox, *image, *label;
2277   
2278   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
2279   
2280   priv = dialog->priv;
2281  
2282   create_printer_list_model (dialog);
2283
2284   priv->notebook = gtk_notebook_new ();
2285
2286   gtk_box_pack_start (GTK_BOX (GTK_DIALOG (dialog)->vbox), 
2287                       priv->notebook,
2288                       TRUE, TRUE, 10);
2289
2290   create_main_page (dialog);
2291   create_page_setup_page (dialog);
2292   create_job_page (dialog);
2293   create_optional_page (dialog, _("Image Quality"),
2294                         &dialog->priv->image_quality_table,
2295                         &dialog->priv->image_quality_page);
2296   create_optional_page (dialog, _("Color"),
2297                         &dialog->priv->color_table,
2298                         &dialog->priv->color_page);
2299   create_optional_page (dialog, _("Finishing"),
2300                         &dialog->priv->finishing_table,
2301                         &dialog->priv->finishing_page);
2302   create_advanced_page (dialog);
2303
2304   hbox = gtk_hbox_new (FALSE, 0);
2305   gtk_widget_show (hbox);
2306   gtk_box_pack_end (GTK_BOX (GTK_DIALOG (dialog)->vbox), hbox,
2307                     FALSE, TRUE, 0);
2308   
2309   conflict_hbox = gtk_hbox_new (FALSE, 0);
2310   image = gtk_image_new_from_stock (GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_MENU);
2311   gtk_widget_show (image);
2312   gtk_box_pack_start (GTK_BOX (conflict_hbox), image, FALSE, TRUE, 0);
2313   label = gtk_label_new (_("Some of the settings in the dialog conflict"));
2314   gtk_widget_show (label);
2315   gtk_box_pack_start (GTK_BOX (conflict_hbox), label, FALSE, TRUE, 0);
2316   dialog->priv->conflicts_widget = conflict_hbox;
2317
2318   gtk_box_pack_start (GTK_BOX (hbox), conflict_hbox,
2319                       FALSE, FALSE, 0);
2320
2321   /* Reparent the action area into the hbox. This is so we can have the
2322    * conflict warning on the same row, but not make the buttons the same
2323    * width as the warning (which the buttonbox does).
2324    */
2325   g_object_ref (GTK_DIALOG (dialog)->action_area);
2326   gtk_container_remove (GTK_CONTAINER (GTK_DIALOG (dialog)->vbox),
2327                         GTK_DIALOG (dialog)->action_area);
2328   gtk_box_pack_end (GTK_BOX (hbox), GTK_DIALOG (dialog)->action_area,
2329                     FALSE, FALSE, 0);
2330   g_object_unref (GTK_DIALOG (dialog)->action_area);
2331   
2332   gtk_widget_show (dialog->priv->notebook);
2333
2334   load_print_backends (dialog);
2335 }
2336
2337 /**
2338  * gtk_print_unix_dialog_new:
2339  * @title: Title of the dialog, or %NULL
2340  * @parent: Transient parent of the dialog, or %NULL
2341  *
2342  * Creates a new #GtkPrintUnixDialog.
2343  *
2344  * Return value: a new #GtkPrintUnixDialog
2345  *
2346  * Since: 2.10
2347  **/
2348 GtkWidget *
2349 gtk_print_unix_dialog_new (const gchar *title,
2350                            GtkWindow   *parent)
2351 {
2352   GtkWidget *result;
2353   const gchar *_title = _("Print");
2354
2355   if (title)
2356     _title = title;
2357   
2358   result = g_object_new (GTK_TYPE_PRINT_UNIX_DIALOG,
2359                          "title", _title,
2360                          "has-separator", FALSE,
2361                          NULL);
2362
2363   if (parent)
2364     gtk_window_set_transient_for (GTK_WINDOW (result), parent);
2365
2366   return result;
2367 }
2368
2369 GtkPrinter *
2370 gtk_print_unix_dialog_get_selected_printer (GtkPrintUnixDialog *dialog)
2371 {
2372   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
2373
2374   return dialog->priv->current_printer;
2375 }
2376
2377 void
2378 gtk_print_unix_dialog_set_page_setup (GtkPrintUnixDialog *dialog,
2379                                       GtkPageSetup       *page_setup)
2380 {
2381   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
2382   g_return_if_fail (GTK_IS_PAGE_SETUP (page_setup));
2383
2384   if (dialog->priv->page_setup != page_setup)
2385     {
2386       g_object_unref (dialog->priv->page_setup);
2387       dialog->priv->page_setup = g_object_ref (page_setup);
2388
2389       g_object_notify (G_OBJECT (dialog), "page-setup");
2390     }
2391 }
2392
2393 GtkPageSetup *
2394 gtk_print_unix_dialog_get_page_setup (GtkPrintUnixDialog *dialog)
2395 {
2396   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
2397
2398   return dialog->priv->page_setup;
2399 }
2400
2401 void
2402 gtk_print_unix_dialog_set_current_page (GtkPrintUnixDialog *dialog,
2403                                         gint                current_page)
2404 {
2405   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
2406
2407   if (dialog->priv->current_page != current_page)
2408     {
2409       dialog->priv->current_page = current_page;
2410
2411       if (dialog->priv->current_page_radio)
2412         gtk_widget_set_sensitive (dialog->priv->current_page_radio, current_page != -1);
2413
2414       g_object_notify (G_OBJECT (dialog), "current-page");
2415     }
2416 }
2417
2418 gint
2419 gtk_print_unix_dialog_get_current_page (GtkPrintUnixDialog *dialog)
2420 {
2421   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), -1);
2422
2423   return dialog->priv->current_page;
2424 }
2425
2426 static gboolean
2427 set_active_printer (GtkPrintUnixDialog *dialog,
2428                     const gchar        *printer_name)
2429 {
2430   GtkTreeModel *model;
2431   GtkTreeIter iter, filter_iter;
2432   GtkTreeSelection *selection;
2433   GtkPrinter *printer;
2434
2435   model = GTK_TREE_MODEL (dialog->priv->printer_list);
2436
2437   if (gtk_tree_model_get_iter_first (model, &iter))
2438     {
2439       do
2440         {
2441           gtk_tree_model_get (GTK_TREE_MODEL (dialog->priv->printer_list), &iter,
2442                               PRINTER_LIST_COL_PRINTER_OBJ, &printer, -1);
2443           if (printer == NULL)
2444             continue;
2445           
2446           if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
2447             {
2448               gtk_tree_model_filter_convert_child_iter_to_iter (dialog->priv->printer_list_filter,
2449                                                                 &filter_iter, &iter);
2450               
2451               selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (dialog->priv->printer_treeview));
2452               dialog->priv->internal_printer_change = TRUE;
2453               gtk_tree_selection_select_iter (selection, &filter_iter);
2454               dialog->priv->internal_printer_change = FALSE;
2455               g_free (dialog->priv->waiting_for_printer);
2456               dialog->priv->waiting_for_printer = NULL;
2457               
2458               g_object_unref (printer);
2459               return TRUE;
2460             }
2461               
2462           g_object_unref (printer);
2463           
2464         } while (gtk_tree_model_iter_next (model, &iter));
2465     }
2466   
2467   return FALSE;
2468 }
2469
2470 void
2471 gtk_print_unix_dialog_set_settings (GtkPrintUnixDialog *dialog,
2472                                     GtkPrintSettings   *settings)
2473 {
2474   const char *printer;
2475   GtkPageRange *ranges;
2476   int num_ranges;
2477   
2478   g_return_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog));
2479   g_return_if_fail (settings == NULL || GTK_IS_PRINT_SETTINGS (settings));
2480
2481   if (settings != NULL)
2482     {
2483       dialog_set_collate (dialog, gtk_print_settings_get_collate (settings));
2484       dialog_set_reverse (dialog, gtk_print_settings_get_reverse (settings));
2485       dialog_set_n_copies (dialog, gtk_print_settings_get_num_copies (settings));
2486       dialog_set_scale (dialog, gtk_print_settings_get_scale (settings));
2487       dialog_set_page_set (dialog, gtk_print_settings_get_page_set (settings));
2488       dialog_set_print_pages (dialog, gtk_print_settings_get_print_pages (settings));
2489       ranges = gtk_print_settings_get_page_ranges (settings, &num_ranges);
2490       if (ranges)
2491         dialog_set_page_ranges (dialog, ranges, num_ranges);
2492
2493       dialog->priv->format_for_printer =
2494         g_strdup (gtk_print_settings_get (settings, "format-for-printer"));
2495     }
2496
2497   if (dialog->priv->initial_settings)
2498     g_object_unref (dialog->priv->initial_settings);
2499
2500   dialog->priv->initial_settings = settings;
2501
2502   g_free (dialog->priv->waiting_for_printer);
2503   dialog->priv->waiting_for_printer = NULL;
2504   
2505   if (settings)
2506     {
2507       g_object_ref (settings);
2508
2509       printer = gtk_print_settings_get_printer (settings);
2510       
2511       if (printer && !set_active_printer (dialog, printer))
2512         dialog->priv->waiting_for_printer = g_strdup (printer); 
2513     }
2514
2515   g_object_notify (G_OBJECT (dialog), "print-settings");
2516 }
2517
2518 GtkPrintSettings *
2519 gtk_print_unix_dialog_get_settings (GtkPrintUnixDialog *dialog)
2520 {
2521   GtkPrintSettings *settings;
2522   GtkPrintPages print_pages;
2523   GtkPageRange *ranges;
2524   int n_ranges;
2525
2526   g_return_val_if_fail (GTK_IS_PRINT_UNIX_DIALOG (dialog), NULL);
2527
2528   settings = gtk_print_settings_new ();
2529
2530   if (dialog->priv->current_printer)
2531     gtk_print_settings_set_printer (settings,
2532                                     gtk_printer_get_name (dialog->priv->current_printer));
2533   else
2534     gtk_print_settings_set_printer (settings, "default");
2535   
2536   gtk_print_settings_set (settings, "format-for-printer",
2537                           dialog->priv->format_for_printer);
2538
2539   
2540   gtk_print_settings_set_collate (settings,
2541                                   dialog_get_collate (dialog));
2542   
2543   gtk_print_settings_set_reverse (settings,
2544                                   dialog_get_reverse (dialog));
2545   
2546   gtk_print_settings_set_num_copies (settings,
2547                                      dialog_get_n_copies (dialog));
2548
2549   gtk_print_settings_set_scale (settings,
2550                                 dialog_get_scale (dialog));
2551   
2552   gtk_print_settings_set_page_set (settings,
2553                                    dialog_get_page_set (dialog));
2554   
2555   print_pages = dialog_get_print_pages (dialog);
2556   gtk_print_settings_set_print_pages (settings, print_pages);
2557
2558   ranges = dialog_get_page_ranges (dialog, &n_ranges);
2559   if (ranges)
2560     {
2561       gtk_print_settings_set_page_ranges  (settings, ranges, n_ranges);
2562       g_free (ranges);
2563     }
2564
2565   /* TODO: print when. How to handle? */
2566
2567   if (dialog->priv->current_printer)
2568     _gtk_printer_get_settings_from_options (dialog->priv->current_printer,
2569                                             dialog->priv->options,
2570                                             settings);
2571   
2572   return settings;
2573 }
2574
2575
2576 #define __GTK_PRINT_UNIX_DIALOG_C__
2577 #include "gtkaliasdef.c"