gtkprintunixdialog.c 147 KB
Newer Older
1 2 3
/* GtkPrintUnixDialog
 * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
 * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
4
 * Copyright © 2006, 2007 Christian Persch
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include <stdlib.h>
#include <string.h>
25
#include <ctype.h>
26 27 28 29 30 31 32 33 34 35
#include <stdio.h>
#include <math.h>

#include "gtkintl.h"
#include "gtkprivate.h"

#include "gtkspinbutton.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkstock.h"
36
#include "gtkiconfactory.h"
37 38 39 40 41 42 43 44 45 46 47 48 49
#include "gtkimage.h"
#include "gtktreeselection.h"
#include "gtknotebook.h"
#include "gtkscrolledwindow.h"
#include "gtkcombobox.h"
#include "gtktogglebutton.h"
#include "gtkradiobutton.h"
#include "gtkdrawingarea.h"
#include "gtkvbox.h"
#include "gtktable.h"
#include "gtkframe.h"
#include "gtkalignment.h"
#include "gtklabel.h"
50
#include "gtkeventbox.h"
51
#include "gtkbuildable.h"
52

53
#include "gtkcustompaperunixdialog.h"
54
#include "gtkprintbackend.h"
55
#include "gtkprinter-private.h"
56 57
#include "gtkprintunixdialog.h"
#include "gtkprinteroptionwidget.h"
58
#include "gtkprintutils.h"
59

60 61 62
#include "gtkmessagedialog.h"
#include "gtkbutton.h"

63
#define EXAMPLE_PAGE_AREA_SIZE 140
64 65 66
#define RULER_DISTANCE 7.5
#define RULER_RADIUS 2

67

68 69 70
static void     gtk_print_unix_dialog_destroy      (GtkPrintUnixDialog *dialog);
static void     gtk_print_unix_dialog_finalize     (GObject            *object);
static void     gtk_print_unix_dialog_set_property (GObject            *object,
Matthias Clasen's avatar
Matthias Clasen committed
71 72 73
                                                    guint               prop_id,
                                                    const GValue       *value,
                                                    GParamSpec         *pspec);
74
static void     gtk_print_unix_dialog_get_property (GObject            *object,
Matthias Clasen's avatar
Matthias Clasen committed
75 76 77
                                                    guint               prop_id,
                                                    GValue             *value,
                                                    GParamSpec         *pspec);
78
static void     gtk_print_unix_dialog_style_set    (GtkWidget          *widget,
Matthias Clasen's avatar
Matthias Clasen committed
79
                                                    GtkStyle           *previous_style);
80 81 82
static void     populate_dialog                    (GtkPrintUnixDialog *dialog);
static void     unschedule_idle_mark_conflicts     (GtkPrintUnixDialog *dialog);
static void     selected_printer_changed           (GtkTreeSelection   *selection,
Matthias Clasen's avatar
Matthias Clasen committed
83
                                                    GtkPrintUnixDialog *dialog);
84 85
static void     clear_per_printer_ui               (GtkPrintUnixDialog *dialog);
static void     printer_added_cb                   (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
86 87
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
88
static void     printer_removed_cb                 (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
89 90
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
91
static void     printer_status_cb                  (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
92 93
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
94
static void     update_collate_icon                (GtkToggleButton    *toggle_button,
Matthias Clasen's avatar
Matthias Clasen committed
95
                                                    GtkPrintUnixDialog *dialog);
96 97
static gboolean dialog_get_collate                 (GtkPrintUnixDialog *dialog);
static gboolean dialog_get_reverse                 (GtkPrintUnixDialog *dialog);
98
static gint     dialog_get_n_copies                (GtkPrintUnixDialog *dialog);
99

100
static void     set_cell_sensitivity_func          (GtkTreeViewColumn *tree_column,
Matthias Clasen's avatar
Matthias Clasen committed
101 102 103 104
                                                    GtkCellRenderer   *cell,
                                                    GtkTreeModel      *model,
                                                    GtkTreeIter       *iter,
                                                    gpointer           data);
105
static gboolean set_active_printer                 (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
106
                                                    const gchar        *printer_name);
107
static void redraw_page_layout_preview             (GtkPrintUnixDialog *dialog);
108

109 110 111 112 113 114
/* GtkBuildable */
static void gtk_print_unix_dialog_buildable_init                    (GtkBuildableIface *iface);
static GObject *gtk_print_unix_dialog_buildable_get_internal_child  (GtkBuildable *buildable,
                                                                     GtkBuilder   *builder,
                                                                     const gchar  *childname);

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
static const gchar const common_paper_sizes[][16] = {
  "na_letter",
  "na_legal",
  "iso_a4",
  "iso_a5",
  "roc_16k",
  "iso_b5",
  "jis_b5",
  "na_number-10",
  "iso_dl",
  "jpn_chou3",
  "na_ledger",
  "iso_a3",
};

enum {
  PAGE_SETUP_LIST_COL_PAGE_SETUP,
  PAGE_SETUP_LIST_COL_IS_SEPARATOR,
  PAGE_SETUP_LIST_N_COLS
};

136 137 138 139 140
enum {
  PROP_0,
  PROP_PAGE_SETUP,
  PROP_CURRENT_PAGE,
  PROP_PRINT_SETTINGS,
141 142 143
  PROP_SELECTED_PRINTER,
  PROP_MANUAL_CAPABILITIES,
  PROP_SUPPORT_SELECTION,
144 145
  PROP_HAS_SELECTION,
  PROP_EMBED_PAGE_SETUP
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
};

enum {
  PRINTER_LIST_COL_ICON,
  PRINTER_LIST_COL_NAME,
  PRINTER_LIST_COL_STATE,
  PRINTER_LIST_COL_JOBS,
  PRINTER_LIST_COL_LOCATION,
  PRINTER_LIST_COL_PRINTER_OBJ,
  PRINTER_LIST_N_COLS
};

struct GtkPrintUnixDialogPrivate
{
  GtkWidget *notebook;

  GtkWidget *printer_treeview;
163 164 165

  GtkPrintCapabilities manual_capabilities;
  GtkPrintCapabilities printer_capabilities;
Matthias Clasen's avatar
Matthias Clasen committed
166

167 168 169 170
  GtkTreeModel *printer_list;
  GtkTreeModelFilter *printer_list_filter;

  GtkPageSetup *page_setup;
171
  gboolean page_setup_set;
172 173 174
  gboolean embed_page_setup;
  GtkListStore *page_setup_list;
  GtkListStore *custom_paper_list;
175

176 177 178
  gboolean support_selection;
  gboolean has_selection;

179 180
  GtkWidget *all_pages_radio;
  GtkWidget *current_page_radio;
181 182
  GtkWidget *selection_radio;
  GtkWidget *range_table;
183 184
  GtkWidget *page_range_radio;
  GtkWidget *page_range_entry;
Matthias Clasen's avatar
Matthias Clasen committed
185

186 187 188 189 190 191 192 193 194 195 196
  GtkWidget *copies_spin;
  GtkWidget *collate_check;
  GtkWidget *reverse_check;
  GtkWidget *collate_image;
  GtkWidget *page_layout_preview;
  GtkWidget *scale_spin;
  GtkWidget *page_set_combo;
  GtkWidget *print_now_radio;
  GtkWidget *print_at_radio;
  GtkWidget *print_at_entry;
  GtkWidget *print_hold_radio;
197
  GtkWidget *preview_button;
198 199 200 201 202
  GtkWidget *paper_size_combo;
  GtkWidget *paper_size_combo_label;
  GtkWidget *orientation_combo;
  GtkWidget *orientation_combo_label;
  gboolean internal_page_setup_change;
203 204 205 206 207 208 209 210 211 212
  gboolean updating_print_at;
  GtkPrinterOptionWidget *pages_per_sheet;
  GtkPrinterOptionWidget *duplex;
  GtkPrinterOptionWidget *paper_type;
  GtkPrinterOptionWidget *paper_source;
  GtkPrinterOptionWidget *output_tray;
  GtkPrinterOptionWidget *job_prio;
  GtkPrinterOptionWidget *billing_info;
  GtkPrinterOptionWidget *cover_before;
  GtkPrinterOptionWidget *cover_after;
213
  GtkPrinterOptionWidget *number_up_layout;
214 215 216 217 218 219 220 221 222 223 224 225 226 227

  GtkWidget *conflicts_widget;

  GtkWidget *job_page;
  GtkWidget *finishing_table;
  GtkWidget *finishing_page;
  GtkWidget *image_quality_table;
  GtkWidget *image_quality_page;
  GtkWidget *color_table;
  GtkWidget *color_page;

  GtkWidget *advanced_vbox;
  GtkWidget *advanced_page;

Matthias Clasen's avatar
~  
Matthias Clasen committed
228
  GtkWidget *extension_point;
229

Matthias Clasen's avatar
Matthias Clasen committed
230 231 232
  /* These are set initially on selected printer (either default printer,
   * printer taken from set settings, or user-selected), but when any
   * setting is changed by the user it is cleared.
Matthias Clasen's avatar
Matthias Clasen committed
233
   */
234
  GtkPrintSettings *initial_settings;
235 236 237

  GtkPrinterOption *number_up_layout_n_option;
  GtkPrinterOption *number_up_layout_2_option;
Matthias Clasen's avatar
Matthias Clasen committed
238 239 240

  /* This is the initial printer set by set_settings. We look for it in
   * the added printers. We clear this whenever the user manually changes
241
   * to another printer, when the user changes a setting or when we find
Matthias Clasen's avatar
Matthias Clasen committed
242 243
   * this printer.
   */
244 245
  char *waiting_for_printer;
  gboolean internal_printer_change;
Matthias Clasen's avatar
Matthias Clasen committed
246

247
  GList *print_backends;
Matthias Clasen's avatar
Matthias Clasen committed
248

249
  GtkPrinter *current_printer;
250
  GtkPrinter *request_details_printer;
251 252 253 254 255
  guint request_details_tag;
  GtkPrinterOptionSet *options;
  gulong options_changed_handler;
  gulong mark_conflicts_id;

Matthias Clasen's avatar
Matthias Clasen committed
256
  gchar *format_for_printer;
Matthias Clasen's avatar
Matthias Clasen committed
257

258 259 260
  gint current_page;
};

261 262 263 264 265
G_DEFINE_TYPE_WITH_CODE (GtkPrintUnixDialog, gtk_print_unix_dialog, GTK_TYPE_DIALOG,
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_print_unix_dialog_buildable_init))

static GtkBuildableIface *parent_buildable_iface;
266 267 268

static gboolean
is_default_printer (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
269
                    GtkPrinter         *printer)
270
{
271 272 273 274
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->format_for_printer)
    return strcmp (priv->format_for_printer,
Matthias Clasen's avatar
Matthias Clasen committed
275
                   gtk_printer_get_name (printer)) == 0;
276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292
 else
   return gtk_printer_is_default (printer);
}

static void
gtk_print_unix_dialog_class_init (GtkPrintUnixDialogClass *class)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;

  object_class = (GObjectClass *) class;
  widget_class = (GtkWidgetClass *) class;

  object_class->finalize = gtk_print_unix_dialog_finalize;
  object_class->set_property = gtk_print_unix_dialog_set_property;
  object_class->get_property = gtk_print_unix_dialog_get_property;

293 294
  widget_class->style_set = gtk_print_unix_dialog_style_set;

295
  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
296 297 298 299 300 301
                                   PROP_PAGE_SETUP,
                                   g_param_spec_object ("page-setup",
                                                        P_("Page Setup"),
                                                        P_("The GtkPageSetup to use"),
                                                        GTK_TYPE_PAGE_SETUP,
                                                        GTK_PARAM_READWRITE));
302 303

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
304 305 306 307 308 309 310 311
                                   PROP_CURRENT_PAGE,
                                   g_param_spec_int ("current-page",
                                                     P_("Current Page"),
                                                     P_("The current page in the document"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
                                                     GTK_PARAM_READWRITE));
312 313

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
314 315 316 317 318 319
                                   PROP_PRINT_SETTINGS,
                                   g_param_spec_object ("print-settings",
                                                        P_("Print Settings"),
                                                        P_("The GtkPrintSettings used for initializing the dialog"),
                                                        GTK_TYPE_PRINT_SETTINGS,
                                                        GTK_PARAM_READWRITE));
320 321

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
322 323 324 325 326 327 328
                                   PROP_SELECTED_PRINTER,
                                   g_param_spec_object ("selected-printer",
                                                        P_("Selected Printer"),
                                                        P_("The GtkPrinter which is selected"),
                                                        GTK_TYPE_PRINTER,
                                                        GTK_PARAM_READABLE));

329
  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
330 331
                                   PROP_MANUAL_CAPABILITIES,
                                   g_param_spec_flags ("manual-capabilities",
332
                                                       P_("Manual Capabilities"),
Matthias Clasen's avatar
Matthias Clasen committed
333 334 335 336
                                                       P_("Capabilities the application can handle"),
                                                       GTK_TYPE_PRINT_CAPABILITIES,
                                                       0,
                                                       GTK_PARAM_READWRITE));
337 338

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
339 340 341 342 343 344
                                   PROP_SUPPORT_SELECTION,
                                   g_param_spec_boolean ("support-selection",
                                                         P_("Support Selection"),
                                                         P_("Whether the dialog supports selection"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
345 346

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
347 348 349 350 351 352 353
                                   PROP_HAS_SELECTION,
                                   g_param_spec_boolean ("has-selection",
                                                         P_("Has Selection"),
                                                         P_("Whether the application has a selection"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));

354 355 356 357 358 359 360 361
   g_object_class_install_property (object_class,
 				   PROP_EMBED_PAGE_SETUP,
 				   g_param_spec_boolean ("embed-page-setup",
 							 P_("Embed Page Setup"),
 							 P_("TRUE if page setup combos are embedded in GtkPrintUnixDialog"),
 							 FALSE,
 							 GTK_PARAM_READWRITE));

Matthias Clasen's avatar
Matthias Clasen committed
362
  g_type_class_add_private (class, sizeof (GtkPrintUnixDialogPrivate));
363 364
}

365 366 367 368 369 370 371
/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel = NULL;

  toplevel = gtk_widget_get_toplevel (widget);
372
  if (!gtk_widget_is_toplevel (toplevel))
373 374 375 376 377
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

378 379 380 381
static void
set_busy_cursor (GtkPrintUnixDialog *dialog,
		 gboolean            busy)
{
382
  GtkWidget *widget;
383 384 385 386 387
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (dialog));
388 389 390
  widget = GTK_WIDGET (toplevel);

  if (!toplevel || !gtk_widget_get_realized (widget))
391 392
    return;

393
  display = gtk_widget_get_display (widget);
394 395 396 397 398 399

  if (busy)
    cursor = gdk_cursor_new_for_display (display, GDK_WATCH);
  else
    cursor = NULL;

400 401
  gdk_window_set_cursor (gtk_widget_get_window (widget),
                         cursor);
402 403 404 405 406 407
  gdk_display_flush (display);

  if (cursor)
    gdk_cursor_unref (cursor);
}

408 409 410 411 412 413 414 415 416
static void
add_custom_button_to_dialog (GtkDialog   *dialog,
                             const gchar *mnemonic_label,
                             const gchar *stock_id,
                             gint         response_id)
{
  GtkWidget *button = NULL;

  button = gtk_button_new_with_mnemonic (mnemonic_label);
417
  gtk_widget_set_can_default (button, TRUE);
418
  gtk_button_set_image (GTK_BUTTON (button),
419 420
                        gtk_image_new_from_stock (stock_id,
                                                  GTK_ICON_SIZE_BUTTON));
421 422 423 424 425
  gtk_widget_show (button);

  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), button, response_id);
}

426
/* This function handles error messages before printing.
427 428
 */
static gboolean
429 430 431
error_dialogs (GtkPrintUnixDialog *print_dialog,
               gint                print_dialog_response_id,
               gpointer            data)
432 433 434 435 436 437
{
  GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
  GtkPrinterOption          *option = NULL;
  GtkPrinter                *printer = NULL;
  GtkWindow                 *toplevel = NULL;
  GtkWidget                 *dialog = NULL;
438
  GFile                     *file = NULL;
439 440 441 442 443 444 445 446
  gchar                     *basename = NULL;
  gchar                     *dirname = NULL;
  int                        response;

  if (print_dialog != NULL && print_dialog_response_id == GTK_RESPONSE_OK)
    {
      printer = gtk_print_unix_dialog_get_selected_printer (print_dialog);

447
      if (printer != NULL)
448
        {
449 450 451 452 453
          if (priv->request_details_tag || !gtk_printer_is_accepting_jobs (printer))
            {
              g_signal_stop_emission_by_name (print_dialog, "response");
              return TRUE;
            }
454

455 456 457
          /* Shows overwrite confirmation dialog in the case of printing to file which
           * already exists. */
          if (gtk_printer_is_virtual (printer))
458
            {
459 460
              option = gtk_printer_option_set_lookup (priv->options,
                                                      "gtk-main-page-custom-input");
461

462 463
              if (option != NULL &&
                  option->type == GTK_PRINTER_OPTION_TYPE_FILESAVE)
464
                {
465
                  file = g_file_new_for_uri (option->value);
466

467 468
                  if (file != NULL &&
                      g_file_query_exists (file, NULL))
469
                    {
470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500
                      toplevel = get_toplevel (GTK_WIDGET (print_dialog));

                      basename = g_file_get_basename (file);
                      dirname = g_file_get_parse_name (g_file_get_parent (file));

                      dialog = gtk_message_dialog_new (toplevel,
                                                       GTK_DIALOG_MODAL |
                                                       GTK_DIALOG_DESTROY_WITH_PARENT,
                                                       GTK_MESSAGE_QUESTION,
                                                       GTK_BUTTONS_NONE,
                                                       _("A file named \"%s\" already exists.  Do you want to replace it?"),
                                                       basename);

                      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
                                                                _("The file already exists in \"%s\".  Replacing it will "
                                                                "overwrite its contents."),
                                                                dirname);

                      gtk_dialog_add_button (GTK_DIALOG (dialog),
                                             GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
                      add_custom_button_to_dialog (GTK_DIALOG (dialog),
                                                   _("_Replace"),
                                                   GTK_STOCK_PRINT,
                                                   GTK_RESPONSE_ACCEPT);
                      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                                               GTK_RESPONSE_ACCEPT,
                                                               GTK_RESPONSE_CANCEL,
                                                               -1);
                      gtk_dialog_set_default_response (GTK_DIALOG (dialog),
                                                       GTK_RESPONSE_ACCEPT);

501 502
                      if (gtk_window_has_group (toplevel))
                        gtk_window_group_add_window (gtk_window_get_group (toplevel),
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
                                                     GTK_WINDOW (dialog));

                      response = gtk_dialog_run (GTK_DIALOG (dialog));

                      gtk_widget_destroy (dialog);

                      g_free (dirname);
                      g_free (basename);

                      if (response != GTK_RESPONSE_ACCEPT)
                        {
                          g_signal_stop_emission_by_name (print_dialog, "response");
                          g_object_unref (file);
                          return TRUE;
                        }
518 519
                    }

520 521
                  g_object_unref (file);
                }
522 523 524 525 526 527
            }
        }
    }
  return FALSE;
}

528 529 530
static void
gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
{
531 532 533 534 535 536
  GtkPrintUnixDialogPrivate *priv;

  dialog->priv = G_TYPE_INSTANCE_GET_PRIVATE (dialog,
                                              GTK_TYPE_PRINT_UNIX_DIALOG,
                                              GtkPrintUnixDialogPrivate);
  priv = dialog->priv;
537

538 539
  priv->print_backends = NULL;
  priv->current_page = -1;
540 541
  priv->number_up_layout_n_option = NULL;
  priv->number_up_layout_2_option = NULL;
542

543
  priv->page_setup = gtk_page_setup_new ();
544
  priv->page_setup_set = FALSE;
545 546
  priv->embed_page_setup = FALSE;
  priv->internal_page_setup_change = FALSE;
547

548 549 550
  priv->support_selection = FALSE;
  priv->has_selection = FALSE;

Matthias Clasen's avatar
Matthias Clasen committed
551 552 553 554
  g_signal_connect (dialog,
                    "destroy",
                    (GCallback) gtk_print_unix_dialog_destroy,
                    NULL);
555

556 557
  g_signal_connect (dialog,
                    "response",
558
                    (GCallback) error_dialogs,
559 560
                    NULL);

561 562 563 564 565
  g_signal_connect (dialog,
                    "notify::page-setup",
                    (GCallback) redraw_page_layout_preview,
                    NULL);

566 567
  priv->preview_button = gtk_button_new_from_stock (GTK_STOCK_PRINT_PREVIEW);
  gtk_widget_show (priv->preview_button);
Matthias Clasen's avatar
Matthias Clasen committed
568 569 570

  gtk_dialog_add_action_widget (GTK_DIALOG (dialog),
                                priv->preview_button,
571
                                GTK_RESPONSE_APPLY);
572
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
Matthias Clasen's avatar
Matthias Clasen committed
573 574
                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                          GTK_STOCK_PRINT, GTK_RESPONSE_OK,
575
                          NULL);
576
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
Matthias Clasen's avatar
Matthias Clasen committed
577 578 579 580
                                           GTK_RESPONSE_APPLY,
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
581 582 583

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
Alexander Larsson's avatar
Alexander Larsson committed
584

585 586 587 588 589 590 591
  priv->page_setup_list = gtk_list_store_new (PAGE_SETUP_LIST_N_COLS,
                                              G_TYPE_OBJECT,
                                              G_TYPE_BOOLEAN);

  priv->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
  _gtk_print_load_custom_papers (priv->custom_paper_list);

Matthias Clasen's avatar
Matthias Clasen committed
592
  populate_dialog (dialog);
Alexander Larsson's avatar
Alexander Larsson committed
593 594
}

595 596 597 598
static void
gtk_print_unix_dialog_destroy (GtkPrintUnixDialog *dialog)
{
  /* Make sure we don't destroy custom widgets owned by the backends */
Matthias Clasen's avatar
Matthias Clasen committed
599
  clear_per_printer_ui (dialog);
600 601
}

602
static void
603
disconnect_printer_details_request (GtkPrintUnixDialog *dialog, gboolean details_failed)
604 605 606 607 608 609 610 611
{
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->request_details_tag)
    {
      g_signal_handler_disconnect (priv->request_details_printer,
                                   priv->request_details_tag);
      priv->request_details_tag = 0;
612
      set_busy_cursor (dialog, FALSE);
613 614 615 616 617 618 619 620 621 622 623 624 625 626
      if (details_failed)
        gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
                            g_object_get_data (G_OBJECT (priv->request_details_printer),
                                               "gtk-print-tree-iter"),
                            PRINTER_LIST_COL_STATE,
                             _("Getting printer information failed"),
                            -1);
      else
        gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
                            g_object_get_data (G_OBJECT (priv->request_details_printer),
                                               "gtk-print-tree-iter"),
                            PRINTER_LIST_COL_STATE,
                            gtk_printer_get_state_message (priv->request_details_printer),
                            -1);
627 628 629 630 631
      g_object_unref (priv->request_details_printer);
      priv->request_details_printer = NULL;
    }
}

632 633 634 635
static void
gtk_print_unix_dialog_finalize (GObject *object)
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
636
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
637 638
  GtkPrintBackend *backend;
  GList *node;
639 640

  unschedule_idle_mark_conflicts (dialog);
641
  disconnect_printer_details_request (dialog, FALSE);
642

643
  if (priv->current_printer)
644
    {
645 646
      g_object_unref (priv->current_printer);
      priv->current_printer = NULL;
647 648
    }

649
  if (priv->printer_list)
650
    {
651 652
      g_object_unref (priv->printer_list);
      priv->printer_list = NULL;
653
    }
Matthias Clasen's avatar
Matthias Clasen committed
654

655 656 657 658 659 660
  if (priv->custom_paper_list)
    {
      g_object_unref (priv->custom_paper_list);
      priv->custom_paper_list = NULL;
    }

661
  if (priv->printer_list_filter)
662
    {
663 664
      g_object_unref (priv->printer_list_filter);
      priv->printer_list_filter = NULL;
665 666
    }

667
  if (priv->options)
668
    {
669 670
      g_object_unref (priv->options);
      priv->options = NULL;
671
    }
Matthias Clasen's avatar
Matthias Clasen committed
672

673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690
  if (priv->number_up_layout_2_option)
    {
      priv->number_up_layout_2_option->choices[0] = NULL;
      priv->number_up_layout_2_option->choices[1] = NULL;
      g_free (priv->number_up_layout_2_option->choices_display[0]);
      g_free (priv->number_up_layout_2_option->choices_display[1]);
      priv->number_up_layout_2_option->choices_display[0] = NULL;
      priv->number_up_layout_2_option->choices_display[1] = NULL;
      g_object_unref (priv->number_up_layout_2_option);
      priv->number_up_layout_2_option = NULL;
    }

  if (priv->number_up_layout_n_option)
    {
      g_object_unref (priv->number_up_layout_n_option);
      priv->number_up_layout_n_option = NULL;
    }

Matthias Clasen's avatar
~  
Matthias Clasen committed
691
 if (priv->page_setup)
692
    {
693 694
      g_object_unref (priv->page_setup);
      priv->page_setup = NULL;
695 696
    }

697
  if (priv->initial_settings)
698
    {
699 700
      g_object_unref (priv->initial_settings);
      priv->initial_settings = NULL;
701 702
    }

703 704
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
705

706 707 708
  g_free (priv->format_for_printer);
  priv->format_for_printer = NULL;

Alexander Larsson's avatar
Alexander Larsson committed
709 710 711 712 713 714 715 716 717 718 719
  for (node = priv->print_backends; node != NULL; node = node->next)
    {
      backend = GTK_PRINT_BACKEND (node->data);

      g_signal_handlers_disconnect_by_func (backend, printer_added_cb, dialog);
      g_signal_handlers_disconnect_by_func (backend, printer_removed_cb, dialog);
      g_signal_handlers_disconnect_by_func (backend, printer_status_cb, dialog);

      gtk_print_backend_destroy (backend);
      g_object_unref (backend);
    }
Matthias Clasen's avatar
Matthias Clasen committed
720

Alexander Larsson's avatar
Alexander Larsson committed
721 722
  g_list_free (priv->print_backends);
  priv->print_backends = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
723

724 725 726 727 728 729
  if (priv->page_setup_list)
    {
      g_object_unref (priv->page_setup_list);
      priv->page_setup_list = NULL;
    }

730
  G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
731 732 733
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
734 735 736
printer_removed_cb (GtkPrintBackend    *backend,
                    GtkPrinter         *printer,
                    GtkPrintUnixDialog *dialog)
737
{
738
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
739
  GtkTreeIter *iter;
740

741
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
742
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
743 744
}

745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
static void
gtk_print_unix_dialog_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);

  iface->get_internal_child = gtk_print_unix_dialog_buildable_get_internal_child;
}

static GObject *
gtk_print_unix_dialog_buildable_get_internal_child (GtkBuildable *buildable,
                                                    GtkBuilder   *builder,
                                                    const gchar  *childname)
{
  if (strcmp (childname, "notebook") == 0)
    return G_OBJECT (GTK_PRINT_UNIX_DIALOG (buildable)->priv->notebook);

  return parent_buildable_iface->get_internal_child (buildable, builder, childname);
}

764 765 766 767 768 769 770 771 772
/* This function controls "sensitive" property of GtkCellRenderer based on pause
 * state of printers. */
void set_cell_sensitivity_func (GtkTreeViewColumn *tree_column,
                                GtkCellRenderer   *cell,
                                GtkTreeModel      *tree_model,
                                GtkTreeIter       *iter,
                                gpointer           data)
{
  GtkPrinter *printer;
Matthias Clasen's avatar
Matthias Clasen committed
773

774 775 776 777 778 779 780 781 782 783 784 785
  gtk_tree_model_get (tree_model, iter, PRINTER_LIST_COL_PRINTER_OBJ, &printer, -1);

  if (printer != NULL && !gtk_printer_is_accepting_jobs (printer))
    g_object_set (cell,
                  "sensitive", FALSE,
                  NULL);
  else
    g_object_set (cell,
                  "sensitive", TRUE,
                  NULL);
}

786
static void
Matthias Clasen's avatar
Matthias Clasen committed
787 788 789
printer_status_cb (GtkPrintBackend    *backend,
                   GtkPrinter         *printer,
                   GtkPrintUnixDialog *dialog)
790
{
791
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
792
  GtkTreeIter *iter;
793
  GtkTreeSelection *selection;
794

795 796
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");

797
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), iter,
798 799 800 801 802 803
                      PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
                      PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
                      PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
                      PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
                      -1);

804 805 806 807
  /* When the pause state change then we need to update sensitive property
   * of GTK_RESPONSE_OK button inside of selected_printer_changed function. */
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
  selected_printer_changed (selection, dialog);
808 809 810 811 812

  if (gtk_print_backend_printer_list_is_done (backend) &&
      gtk_printer_is_default (printer) &&
      (gtk_tree_selection_count_selected_rows (selection) == 0))
    set_active_printer (dialog, gtk_printer_get_name (printer));
813 814 815
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
816 817 818
printer_added_cb (GtkPrintBackend    *backend,
                  GtkPrinter         *printer,
                  GtkPrintUnixDialog *dialog)
819
{
820
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
821 822
  GtkTreeIter iter, filter_iter;
  GtkTreeSelection *selection;
823
  GtkTreePath *path;
824

825
  gtk_list_store_append (GTK_LIST_STORE (priv->printer_list), &iter);
Matthias Clasen's avatar
Matthias Clasen committed
826 827 828

  g_object_set_data_full (G_OBJECT (printer),
                         "gtk-print-tree-iter",
829 830 831
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);

832
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), &iter,
833 834 835 836 837 838 839 840
                      PRINTER_LIST_COL_ICON, gtk_printer_get_icon_name (printer),
                      PRINTER_LIST_COL_NAME, gtk_printer_get_name (printer),
                      PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
                      PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
                      PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
                      PRINTER_LIST_COL_PRINTER_OBJ, printer,
                      -1);

841
  gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
Matthias Clasen's avatar
Matthias Clasen committed
842
                                                    &filter_iter, &iter);
843 844
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->printer_list_filter), &filter_iter);

845
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
Matthias Clasen's avatar
Matthias Clasen committed
846

847
  if (priv->waiting_for_printer != NULL &&
848
      strcmp (gtk_printer_get_name (printer),
Matthias Clasen's avatar
Matthias Clasen committed
849
              priv->waiting_for_printer) == 0)
850
    {
851
      priv->internal_printer_change = TRUE;
852
      gtk_tree_selection_select_iter (selection, &filter_iter);
853 854
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
855 856 857
      priv->internal_printer_change = FALSE;
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
858 859
    }
  else if (is_default_printer (dialog, printer) &&
Matthias Clasen's avatar
Matthias Clasen committed
860
           gtk_tree_selection_count_selected_rows (selection) == 0)
861
    {
862
      priv->internal_printer_change = TRUE;
863
      gtk_tree_selection_select_iter (selection, &filter_iter);
864 865
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
866
      priv->internal_printer_change = FALSE;
867
    }
868 869

  gtk_tree_path_free (path);
870 871 872 873
}

static void
printer_list_initialize (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
874
                         GtkPrintBackend    *print_backend)
875 876 877 878 879 880
{
  GList *list;
  GList *node;

  g_return_if_fail (print_backend != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
881 882 883 884
  g_signal_connect_object (print_backend,
                           "printer-added",
                           (GCallback) printer_added_cb,
                           G_OBJECT (dialog), 0);
885

Matthias Clasen's avatar
Matthias Clasen committed
886 887 888 889
  g_signal_connect_object (print_backend,
                           "printer-removed",
                           (GCallback) printer_removed_cb,
                           G_OBJECT (dialog), 0);
890

Matthias Clasen's avatar
Matthias Clasen committed
891 892 893 894
  g_signal_connect_object (print_backend,
                           "printer-status-changed",
                           (GCallback) printer_status_cb,
                           G_OBJECT (dialog), 0);
895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910

  list = gtk_print_backend_get_printer_list (print_backend);

  node = list;
  while (node != NULL)
    {
      printer_added_cb (print_backend, node->data, dialog);
      node = node->next;
    }

  g_list_free (list);
}

static void
load_print_backends (GtkPrintUnixDialog *dialog)
{
911
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
912 913 914
  GList *node;

  if (g_module_supported ())
915
    priv->print_backends = gtk_print_backend_load_modules ();
916

917
  for (node = priv->print_backends; node != NULL; node = node->next)
918 919 920 921
    {
      GtkPrintBackend *backend = node->data;
      printer_list_initialize (dialog, backend);
    }
922 923 924 925
}

static void
gtk_print_unix_dialog_set_property (GObject      *object,
Matthias Clasen's avatar
Matthias Clasen committed
926 927 928
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
      gtk_print_unix_dialog_set_page_setup (dialog, g_value_get_object (value));
      break;
    case PROP_CURRENT_PAGE:
      gtk_print_unix_dialog_set_current_page (dialog, g_value_get_int (value));
      break;
    case PROP_PRINT_SETTINGS:
      gtk_print_unix_dialog_set_settings (dialog, g_value_get_object (value));
      break;
944 945 946 947 948 949 950 951 952
    case PROP_MANUAL_CAPABILITIES:
      gtk_print_unix_dialog_set_manual_capabilities (dialog, g_value_get_flags (value));
      break;
    case PROP_SUPPORT_SELECTION:
      gtk_print_unix_dialog_set_support_selection (dialog, g_value_get_boolean (value));
      break;
    case PROP_HAS_SELECTION:
      gtk_print_unix_dialog_set_has_selection (dialog, g_value_get_boolean (value));
      break;
953 954 955
    case PROP_EMBED_PAGE_SETUP:
      gtk_print_unix_dialog_set_embed_page_setup (dialog, g_value_get_boolean (value));
      break;
956 957 958 959 960 961 962 963
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_print_unix_dialog_get_property (GObject    *object,
Matthias Clasen's avatar
Matthias Clasen committed
964 965 966
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
967 968
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
969
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
970 971 972 973

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
974
      g_value_set_object (value, priv->page_setup);
975 976
      break;
    case PROP_CURRENT_PAGE:
977
      g_value_set_int (value, priv->current_page);
978 979
      break;
    case PROP_PRINT_SETTINGS:
980
      g_value_take_object (value, gtk_print_unix_dialog_get_settings (dialog));
981 982
      break;
    case PROP_SELECTED_PRINTER:
983
      g_value_set_object (value, priv->current_printer);
984
      break;
985 986 987 988 989 990 991 992 993
    case PROP_MANUAL_CAPABILITIES:
      g_value_set_flags (value, priv->manual_capabilities);
      break;
    case PROP_SUPPORT_SELECTION:
      g_value_set_boolean (value, priv->support_selection);
      break;
    case PROP_HAS_SELECTION:
      g_value_set_boolean (value, priv->has_selection);
      break;
994 995 996
    case PROP_EMBED_PAGE_SETUP:
      g_value_set_boolean (value, priv->embed_page_setup);
      break;
997 998 999 1000 1001 1002 1003 1004 1005
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static gboolean
is_printer_active (GtkTreeModel       *model,
                   GtkTreeIter        *iter,
Matthias Clasen's avatar
Matthias Clasen committed
1006
                   GtkPrintUnixDialog *dialog)
1007 1008 1009
{
  gboolean result;
  GtkPrinter *printer;
1010
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1011 1012

  gtk_tree_model_get (model,
Matthias Clasen's avatar
Matthias Clasen committed
1013 1014
                      iter,
                      PRINTER_LIST_COL_PRINTER_OBJ,
1015
                      &printer,
Matthias Clasen's avatar
Matthias Clasen committed
1016 1017
                      -1);

1018 1019 1020 1021
  if (printer == NULL)
    return FALSE;

  result = gtk_printer_is_active (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1022 1023

  if (result &&
1024
      priv->manual_capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF |
Matthias Clasen's avatar
Matthias Clasen committed
1025
                                   GTK_PRINT_CAPABILITY_GENERATE_PS))
1026
    {
Matthias Clasen's avatar
Matthias Clasen committed
1027 1028
       /* Check that the printer can handle at least one of the data
        * formats that the application supports.
1029 1030
        */
       result = ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF) &&
Matthias Clasen's avatar
Matthias Clasen committed
1031 1032 1033
                 gtk_printer_accepts_pdf (printer)) ||
                ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS) &&
                 gtk_printer_accepts_ps (printer));
1034
    }
Matthias Clasen's avatar
Matthias Clasen committed
1035

1036
  g_object_unref (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1037

1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052
  return result;
}

static gint
default_printer_list_sort_func (GtkTreeModel *model,
                                GtkTreeIter  *a,
                                GtkTreeIter  *b,
                                gpointer      user_data)
{
  gchar *a_name;
  gchar *b_name;
  GtkPrinter *a_printer;
  GtkPrinter *b_printer;
  gint result;

Matthias Clasen's avatar
Matthias Clasen committed
1053 1054 1055 1056 1057
  gtk_tree_model_get (model, a,
                      PRINTER_LIST_COL_NAME, &a_name,
                      PRINTER_LIST_COL_PRINTER_OBJ, &a_printer,
                      -1);
  gtk_tree_model_get (model, b,
1058
                      PRINTER_LIST_COL_NAME, &b_name,
Matthias Clasen's avatar
Matthias Clasen committed
1059 1060
                      PRINTER_LIST_COL_PRINTER_OBJ, &b_printer,
                      -1);
1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093

  if (a_printer == NULL && b_printer == NULL)
    result = 0;
  else if (a_printer == NULL)
   result = G_MAXINT;
  else if (b_printer == NULL)
   result = G_MININT;
  else if (gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
    result = 0;
  else if (gtk_printer_is_virtual (a_printer) && !gtk_printer_is_virtual (b_printer))
    result = G_MININT;
  else if (!gtk_printer_is_virtual (a_printer) && gtk_printer_is_virtual (b_printer))
    result = G_MAXINT;
  else if (a_name == NULL && b_name == NULL)
    result = 0;
  else if (a_name == NULL && b_name != NULL)
    result = 1;
  else if (a_name != NULL && b_name == NULL)
    result = -1;
  else
    result = g_ascii_strcasecmp (a_name, b_name);

  g_free (a_name);
  g_free (b_name);
  g_object_unref (a_printer);
  g_object_unref (b_printer);

  return result;
}

static void
create_printer_list_model (GtkPrintUnixDialog *dialog)
{
1094
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1095 1096 1097 1098 1099
  GtkListStore *model;
  GtkTreeSortable *sort;

  model = gtk_list_store_new (PRINTER_LIST_N_COLS,
                              G_TYPE_STRING,
Matthias Clasen's avatar
Matthias Clasen committed
1100 1101 1102
                              G_TYPE_STRING,
                              G_TYPE_STRING,
                              G_TYPE_INT,
1103 1104 1105
                              G_TYPE_STRING,
                              G_TYPE_OBJECT);

1106 1107
  priv->printer_list = (GtkTreeModel *)model;
  priv->printer_list_filter = (GtkTreeModelFilter *) gtk_tree_model_filter_new ((GtkTreeModel *)model,
Matthias Clasen's avatar
Matthias Clasen committed
1108
                                                                                        NULL);
1109

1110
  gtk_tree_model_filter_set_visible_func (priv->printer_list_filter,
Matthias Clasen's avatar
Matthias Clasen committed
1111 1112 1113
                                          (GtkTreeModelFilterVisibleFunc) is_printer_active,
                                          dialog,
                                          NULL);
1114 1115 1116

  sort = GTK_TREE_SORTABLE (model);
  gtk_tree_sortable_set_default_sort_func (sort,
Matthias Clasen's avatar
Matthias Clasen committed
1117 1118 1119 1120
                                           default_printer_list_sort_func,
                                           NULL,
                                           NULL);

1121
  gtk_tree_sortable_set_sort_column_id (sort,
Matthias Clasen's avatar
Matthias Clasen committed
1122 1123
                                        GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
                                        GTK_SORT_ASCENDING);
1124 1125 1126 1127 1128

}


static GtkWidget *
Matthias Clasen's avatar
Matthias Clasen committed
1129
wrap_in_frame (const gchar *label,
1130 1131 1132
               GtkWidget   *child)
{
  GtkWidget *frame, *alignment, *label_widget;
Matthias Clasen's avatar
Matthias Clasen committed
1133
  gchar *bold_text;
1134

1135
  label_widget = gtk_label_new (NULL);
1136
  gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
1137
  gtk_widget_show (label_widget);
Matthias Clasen's avatar
Matthias Clasen committed
1138

1139 1140 1141
  bold_text = g_markup_printf_escaped ("<b>%s</b>", label);
  gtk_label_set_markup (GTK_LABEL (label_widget), bold_text);
  g_free (bold_text);
Matthias Clasen's avatar
Matthias Clasen committed
1142

1143 1144
  frame = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
1145

1146
  alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
1147
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
Matthias Clasen's avatar
Matthias Clasen committed
1148
                             0, 0, 12, 0);
1149
  gtk_box_pack_start (GTK_BOX (frame), alignment, FALSE, FALSE, 0);
1150 1151 1152 1153 1154

  gtk_container_add (GTK_CONTAINER (alignment), child);

  gtk_widget_show (frame);
  gtk_widget_show (alignment);
Matthias Clasen's avatar
Matthias Clasen committed
1155

1156 1157 1158 1159 1160
  return frame;
}

static gboolean
setup_option (GtkPrintUnixDialog     *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
1161 1162
              const gchar            *option_name,
              GtkPrinterOptionWidget *widget)
1163
{
1164
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1165 1166
  GtkPrinterOption *option;

1167
  option = gtk_printer_option_set_lookup (priv->options, option_name);
1168 1169 1170 1171 1172 1173
  gtk_printer_option_widget_set_source (widget, option);

  return option != NULL;
}

static void
1174
add_option_to_extension_point (GtkPrinterOption *option,
Matthias Clasen's avatar
Matthias Clasen committed
1175
                               gpointer          data)
1176
{
Matthias Clasen's avatar
~  
Matthias Clasen committed
1177 1178
  GtkWidget *extension_point = data;
  GtkWidget *widget;
1179

Matthias Clasen's avatar
~  
Matthias Clasen committed
1180 1181
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);
Matthias Clasen's avatar
Matthias Clasen committed
1182

Matthias Clasen's avatar
~  
Matthias Clasen committed
1183 1184 1185
  if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
    {
      GtkWidget *label, *hbox;
Matthias Clasen's avatar
Matthias Clasen committed
1186

Matthias Clasen's avatar
~  
Matthias Clasen committed
1187 1188 1189 1190
      label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
      gtk_widget_show (label);
      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
Matthias Clasen's avatar
Matthias Clasen committed
1191

Matthias Clasen's avatar
~  
Matthias Clasen committed
1192 1193 1194 1195
      hbox = gtk_hbox_new (FALSE, 12);
      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
      gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
      gtk_widget_show (hbox);
Matthias Clasen's avatar
Matthias Clasen committed
1196

Matthias Clasen's avatar
~  
Matthias Clasen committed
1197
      gtk_box_pack_start (GTK_BOX (extension_point), hbox, FALSE, FALSE, 0);
1198 1199
    }
  else
Matthias Clasen's avatar
~  
Matthias Clasen committed
1200
    gtk_box_pack_start (GTK_BOX (extension_point), widget, FALSE, FALSE, 0);
1201 1202 1203 1204
}

static void
add_option_to_table (GtkPrinterOption *option,
Matthias Clasen's avatar
Matthias Clasen committed
1205
                     gpointer          user_data)
1206 1207 1208
{
  GtkTable *table;
  GtkWidget *label, *widget;
1209
  guint row;
1210 1211

  table = GTK_TABLE (user_data);
Matthias Clasen's avatar
Matthias Clasen committed
1212

1213 1214
  if (g_str_has_prefix (option->name, "gtk-"))
    return;
Matthias Clasen's avatar
Matthias Clasen committed
1215

1216 1217 1218
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);

1219 1220
  gtk_table_get_size (table, &row, NULL);
  gtk_table_resize (table, row + 1, 2);
Matthias Clasen's avatar
Matthias Clasen committed
1221

1222 1223 1224 1225 1226 1227
  if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
    {
      label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
      gtk_widget_show (label);

      gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1228 1229
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);

1230
      gtk_table_attach (table, label,
Matthias Clasen's avatar
Matthias Clasen committed
1231 1232
                        0, 1, row - 1 , row,  GTK_FILL, 0, 0, 0);

1233
      gtk_table_attach (table, widget,
Matthias Clasen's avatar
Matthias Clasen committed
1234
                        1, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
1235 1236 1237
    }
  else
    gtk_table_attach (table, widget,
Matthias Clasen's avatar
Matthias Clasen committed
1238
                      0, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
1239 1240 1241 1242
}

static void
setup_page_table (GtkPrinterOptionSet *options,
Matthias Clasen's avatar
Matthias Clasen committed
1243 1244 1245
                  const gchar         *group,
                  GtkWidget           *table,
                  GtkWidget           *page)
1246
{
1247 1248
  guint nrows;

1249
  gtk_printer_option_set_foreach_in_group (options, group,
Matthias Clasen's avatar
Matthias Clasen committed
1250 1251
                                           add_option_to_table,
                                           table);
1252 1253 1254

  gtk_table_get_size (GTK_TABLE (table), &nrows, NULL);
  if (nrows == 1)
1255 1256 1257 1258 1259 1260 1261 1262
    gtk_widget_hide (page);
  else
    gtk_widget_show (page);
}

static void
update_print_at_option (GtkPrintUnixDialog *dialog)
{
1263
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1264
  GtkPrinterOption *option;
Matthias Clasen's avatar
Matthias Clasen committed
1265

1266
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1267 1268 1269

  if (option == NULL)
    return;
Matthias Clasen's avatar
Matthias Clasen committed
1270

1271
  if (priv->updating_print_at)
1272
    return;
1273

1274
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_at_radio)))
1275
    gtk_printer_option_set (option, "at");
1276
  else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio)))
1277 1278 1279
    gtk_printer_option_set (option, "on-hold");
  else
    gtk_printer_option_set (option, "now");
1280

1281
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1282 1283
  if (option != NULL)
    {
1284
      const char *text = gtk_entry_get_text (GTK_ENTRY (priv->print_at_entry));
1285
      gtk_printer_option_set (option, text);
1286 1287 1288 1289 1290 1291 1292
    }
}


static gboolean
setup_print_at (GtkPrintUnixDialog *dialog)
{
1293
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1294
  GtkPrinterOption *option;
1295

1296
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1297

1298 1299
  if (option == NULL)
    {
1300
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
Matthias Clasen's avatar
Matthias Clasen committed
1301
                                    TRUE);
1302 1303 1304 1305
      gtk_widget_set_sensitive (priv->print_at_radio, FALSE);
      gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
      gtk_widget_set_sensitive (priv->print_hold_radio, FALSE);
      gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry), "");
1306 1307 1308
      return FALSE;
    }

1309
  priv->updating_print_at = TRUE;
1310 1311 1312 1313 1314

  gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
  gtk_widget_set_sensitive (priv->print_at_radio,
                            gtk_printer_option_has_choice (option, "at"));

1315
  gtk_widget_set_sensitive (priv->print_hold_radio,
Matthias Clasen's avatar
Matthias Clasen committed
1316
                            gtk_printer_option_has_choice (option, "on-hold"));
1317 1318 1319 1320

  update_print_at_option (dialog);

  if (strcmp (option->value, "at") == 0)
1321
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_at_radio),
Matthias Clasen's avatar
Matthias Clasen committed
1322
                                  TRUE);
1323
  else if (strcmp (option->value, "on-hold") == 0)
1324
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio),
Matthias Clasen's avatar
Matthias Clasen committed
1325
                                  TRUE);
1326
  else
1327
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
Matthias Clasen's avatar
Matthias Clasen committed
1328
                                  TRUE);
1329

1330
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1331
  if (option != NULL)
1332 1333
    gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry), option->value);

1334
  priv->updating_print_at = FALSE;
1335 1336 1337

  return TRUE;
}
1338

1339 1340 1341
static void
update_dialog_from_settings (GtkPrintUnixDialog *dialog)
{
1342
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1343
  GList *groups, *l;
Matthias Clasen's avatar
Matthias Clasen committed
1344
  gchar *group;
1345 1346
  GtkWidget *table, *frame;
  gboolean has_advanced, has_job;
1347
  guint nrows;
Matthias Clasen's avatar
Matthias Clasen committed
1348

1349
  if (priv->current_printer == NULL)
1350 1351
    {
       clear_per_printer_ui (dialog);
Christian Persch's avatar