gtkprintunixdialog.c 88 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 "gtkprintbackend.h"
54
#include "gtkprinter-private.h"
55 56 57 58
#include "gtkprintunixdialog.h"
#include "gtkprinteroptionwidget.h"
#include "gtkalias.h"

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

62 63 64 65 66
#define EXAMPLE_PAGE_AREA_SIZE 140

#define GTK_PRINT_UNIX_DIALOG_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PRINT_UNIX_DIALOG, GtkPrintUnixDialogPrivate))

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

99 100 101 102 103 104
/* 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);

105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
enum {
  PROP_0,
  PROP_PAGE_SETUP,
  PROP_CURRENT_PAGE,
  PROP_PRINT_SETTINGS,
  PROP_SELECTED_PRINTER
};

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;
128 129 130

  GtkPrintCapabilities manual_capabilities;
  GtkPrintCapabilities printer_capabilities;
131 132 133 134 135
  
  GtkTreeModel *printer_list;
  GtkTreeModelFilter *printer_list_filter;

  GtkPageSetup *page_setup;
136
  gboolean page_setup_set;
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153

  GtkWidget *all_pages_radio;
  GtkWidget *current_page_radio;
  GtkWidget *page_range_radio;
  GtkWidget *page_range_entry;
  
  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;
154
  GtkWidget *preview_button;
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178
  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;

  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
179
  GtkWidget *extension_point;
180

Matthias Clasen's avatar
Matthias Clasen committed
181 182 183 184
  /* 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.
   */
185 186 187 188 189
  GtkPrintSettings *initial_settings;
  
  /* 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
   * to another printer, when the user changes a setting or when we find
Matthias Clasen's avatar
Matthias Clasen committed
190 191
   * this printer.
   */
192 193 194 195 196 197
  char *waiting_for_printer;
  gboolean internal_printer_change;
  
  GList *print_backends;
  
  GtkPrinter *current_printer;
198
  GtkPrinter *request_details_printer;
199 200 201 202 203
  guint request_details_tag;
  GtkPrinterOptionSet *options;
  gulong options_changed_handler;
  gulong mark_conflicts_id;

Matthias Clasen's avatar
Matthias Clasen committed
204
  gchar *format_for_printer;
205 206 207 208
  
  gint current_page;
};

209 210 211 212 213
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;
214 215 216

static gboolean
is_default_printer (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
217
		    GtkPrinter         *printer)
218
{
219 220 221 222
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->format_for_printer)
    return strcmp (priv->format_for_printer,
223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
		   gtk_printer_get_name (printer)) == 0;
 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;

241 242
  widget_class->style_set = gtk_print_unix_dialog_style_set;

243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272
  g_object_class_install_property (object_class,
				   PROP_PAGE_SETUP,
				   g_param_spec_object ("page-setup",
							P_("Page Setup"),
							P_("The GtkPageSetup to use"),
							GTK_TYPE_PAGE_SETUP,
							GTK_PARAM_READWRITE));

  g_object_class_install_property (object_class,
				   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));

  g_object_class_install_property (object_class,
				   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));

  g_object_class_install_property (object_class,
				   PROP_SELECTED_PRINTER,
				   g_param_spec_object ("selected-printer",
							P_("Selected Printer"),
273
							P_("The GtkPrinter which is selected"),
274 275 276
							GTK_TYPE_PRINTER,
							GTK_PARAM_READABLE));
  
Alexander Larsson's avatar
Alexander Larsson committed
277
  
278 279 280
  g_type_class_add_private (class, sizeof (GtkPrintUnixDialogPrivate));  
}

281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel = NULL;

  toplevel = gtk_widget_get_toplevel (widget);
  if (!GTK_WIDGET_TOPLEVEL (toplevel))
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

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);
  GTK_WIDGET_SET_FLAGS (button, GTK_CAN_DEFAULT);
  gtk_button_set_image (GTK_BUTTON (button),
305 306
                        gtk_image_new_from_stock (stock_id,
                                                  GTK_ICON_SIZE_BUTTON));
307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334
  gtk_widget_show (button);

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

/* Presents an overwrite confirmation dialog ("print to file" backend).
 */
static gboolean
overwrite_confirmation_dialog (GtkPrintUnixDialog *print_dialog,
                               gint                print_dialog_response_id,
                               gpointer            data)
{
  GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
  GtkPrinterOption          *option = NULL;
  GtkPrinter                *printer = NULL;
  GtkWindow                 *toplevel = NULL;
  GtkWidget                 *dialog = NULL;
  gchar                     *filename = NULL;
  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);

      if (printer != NULL && gtk_printer_is_virtual (printer))
        {
335 336
          option = gtk_printer_option_set_lookup (priv->options,
                                                  "gtk-main-page-custom-input");
337

338 339
          if (option != NULL &&
              option->type == GTK_PRINTER_OPTION_TYPE_FILESAVE)
340 341 342
            {
              filename = g_filename_from_uri (option->value, NULL, NULL);

343 344
              if (filename != NULL &&
                  g_file_test (filename, G_FILE_TEST_EXISTS))
345 346 347 348 349 350 351
                {
                  toplevel = get_toplevel (GTK_WIDGET (print_dialog));

                  basename = g_path_get_basename (filename);
                  dirname = g_path_get_dirname (filename);

                  dialog = gtk_message_dialog_new (toplevel,
352 353
                                                   GTK_DIALOG_MODAL |
                                                   GTK_DIALOG_DESTROY_WITH_PARENT,
354 355 356 357 358 359 360 361 362 363
                                                   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);

364 365 366 367 368 369
                  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);
370 371 372 373
                  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                                           GTK_RESPONSE_ACCEPT,
                                                           GTK_RESPONSE_CANCEL,
                                                           -1);
374 375
                  gtk_dialog_set_default_response (GTK_DIALOG (dialog),
                                                   GTK_RESPONSE_ACCEPT);
376 377

                  if (toplevel->group)
378 379
                    gtk_window_group_add_window (toplevel->group,
                                                 GTK_WINDOW (dialog));
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401

                  response = gtk_dialog_run (GTK_DIALOG (dialog));

                  gtk_widget_destroy (dialog);

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

                  g_free (dirname);
                  g_free (basename);
                }

              g_free (filename);
            }
        }
    }
  return FALSE;
}

402 403 404
static void
gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
{
405
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
406

407 408 409
  priv = dialog->priv = GTK_PRINT_UNIX_DIALOG_GET_PRIVATE (dialog); 
  priv->print_backends = NULL;
  priv->current_page = -1;
410

411
  priv->page_setup = gtk_page_setup_new ();
412
  priv->page_setup_set = FALSE;
413 414 415 416 417 418

  g_signal_connect (dialog, 
                    "destroy", 
		    (GCallback) gtk_print_unix_dialog_destroy, 
		    NULL);

419 420 421 422 423
  g_signal_connect (dialog,
                    "response",
                    (GCallback) overwrite_confirmation_dialog,
                    NULL);

424 425 426 427 428 429
  priv->preview_button = gtk_button_new_from_stock (GTK_STOCK_PRINT_PREVIEW);
  gtk_widget_show (priv->preview_button);
   
  gtk_dialog_add_action_widget (GTK_DIALOG (dialog), 
                                priv->preview_button, 
                                GTK_RESPONSE_APPLY);
430
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
431 432 433
			  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			  GTK_STOCK_PRINT, GTK_RESPONSE_OK,
                          NULL);
434 435 436 437 438
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
					   GTK_RESPONSE_APPLY,
					   GTK_RESPONSE_OK,
					   GTK_RESPONSE_CANCEL,
					   -1);
439 440 441

  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
442

443
  populate_dialog (dialog);  
Alexander Larsson's avatar
Alexander Larsson committed
444 445
}

446 447 448 449 450 451 452
static void
gtk_print_unix_dialog_destroy (GtkPrintUnixDialog *dialog)
{
  /* Make sure we don't destroy custom widgets owned by the backends */
  clear_per_printer_ui (dialog);  
}

453 454 455 456 457 458 459 460 461 462 463 464 465 466 467
static void
disconnect_printer_details_request (GtkPrintUnixDialog *dialog)
{
  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;
      g_object_unref (priv->request_details_printer);
      priv->request_details_printer = NULL;
    }
}

468 469 470 471
static void
gtk_print_unix_dialog_finalize (GObject *object)
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
472
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
473 474
  GtkPrintBackend *backend;
  GList *node;
475 476

  unschedule_idle_mark_conflicts (dialog);
477
  disconnect_printer_details_request (dialog);
478

479
  if (priv->current_printer)
480
    {
481 482
      g_object_unref (priv->current_printer);
      priv->current_printer = NULL;
483 484
    }

485
  if (priv->printer_list)
486
    {
487 488
      g_object_unref (priv->printer_list);
      priv->printer_list = NULL;
489 490
    }
 
491
  if (priv->printer_list_filter)
492
    {
493 494
      g_object_unref (priv->printer_list_filter);
      priv->printer_list_filter = NULL;
495 496 497
    }

 
498
  if (priv->options)
499
    {
500 501
      g_object_unref (priv->options);
      priv->options = NULL;
502 503
    }
 
Matthias Clasen's avatar
~  
Matthias Clasen committed
504
 if (priv->page_setup)
505
    {
506 507
      g_object_unref (priv->page_setup);
      priv->page_setup = NULL;
508 509
    }

510
  if (priv->initial_settings)
511
    {
512 513
      g_object_unref (priv->initial_settings);
      priv->initial_settings = NULL;
514 515
    }

516 517
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;
518
  
519 520 521
  g_free (priv->format_for_printer);
  priv->format_for_printer = NULL;

Alexander Larsson's avatar
Alexander Larsson committed
522 523 524 525 526 527 528 529 530 531 532 533 534 535 536
  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);
    }
  
  g_list_free (priv->print_backends);
  priv->print_backends = NULL;
  
537
  G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
538 539 540 541 542 543 544
}

static void
printer_removed_cb (GtkPrintBackend    *backend, 
                    GtkPrinter         *printer, 
		    GtkPrintUnixDialog *dialog)
{
545
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
546
  GtkTreeIter *iter;
547

548
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
549
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
550 551
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570
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);
}

571 572 573 574 575
static void
printer_status_cb (GtkPrintBackend    *backend, 
		   GtkPrinter         *printer, 
		   GtkPrintUnixDialog *dialog)
{
576
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
577
  GtkTreeIter *iter;
578

579 580
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");

581
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), iter,
582 583 584 585 586 587 588 589 590 591 592 593 594
                      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);

}

static void
printer_added_cb (GtkPrintBackend    *backend, 
                  GtkPrinter         *printer, 
		  GtkPrintUnixDialog *dialog)
{
595
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
596 597
  GtkTreeIter iter, filter_iter;
  GtkTreeSelection *selection;
598
  GtkTreePath *path;
599

600
  gtk_list_store_append (GTK_LIST_STORE (priv->printer_list), &iter);
601 602 603 604 605 606
  
  g_object_set_data_full (G_OBJECT (printer), 
                         "gtk-print-tree-iter", 
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);

607
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), &iter,
608 609 610 611 612 613 614 615
                      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);

616
  gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
617
						    &filter_iter, &iter);
618 619
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->printer_list_filter), &filter_iter);

620
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
621
  
622
  if (priv->waiting_for_printer != NULL &&
623
      strcmp (gtk_printer_get_name (printer),
624
	      priv->waiting_for_printer) == 0)
625
    {
626
      priv->internal_printer_change = TRUE;
627
      gtk_tree_selection_select_iter (selection, &filter_iter);
628 629
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
630 631 632
      priv->internal_printer_change = FALSE;
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
633 634 635 636
    }
  else if (is_default_printer (dialog, printer) &&
	   gtk_tree_selection_count_selected_rows (selection) == 0)
    {
637
      priv->internal_printer_change = TRUE;
638
      gtk_tree_selection_select_iter (selection, &filter_iter);
639 640
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
641
      priv->internal_printer_change = FALSE;
642
    }
643 644

  gtk_tree_path_free (path);
645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685
}

static void
printer_list_initialize (GtkPrintUnixDialog *dialog,
			 GtkPrintBackend    *print_backend)
{
  GList *list;
  GList *node;

  g_return_if_fail (print_backend != NULL);

  g_signal_connect_object (print_backend, 
			   "printer-added", 
			   (GCallback) printer_added_cb, 
			   G_OBJECT (dialog), 0);

  g_signal_connect_object (print_backend, 
			   "printer-removed", 
			   (GCallback) printer_removed_cb, 
			   G_OBJECT (dialog), 0);

  g_signal_connect_object (print_backend, 
			   "printer-status-changed", 
			   (GCallback) printer_status_cb, 
			   G_OBJECT (dialog), 0);

  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)
{
686
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
687 688 689
  GList *node;

  if (g_module_supported ())
690
    priv->print_backends = gtk_print_backend_load_modules ();
691

692
  for (node = priv->print_backends; node != NULL; node = node->next)
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728
    printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
}

static void
gtk_print_unix_dialog_set_property (GObject      *object,
				    guint         prop_id,
				    const GValue *value,
				    GParamSpec   *pspec)

{
  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;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_print_unix_dialog_get_property (GObject    *object,
				    guint       prop_id,
				    GValue     *value,
				    GParamSpec *pspec)
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
729
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
730 731 732 733

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
734
      g_value_set_object (value, priv->page_setup);
735 736
      break;
    case PROP_CURRENT_PAGE:
737
      g_value_set_int (value, priv->current_page);
738 739
      break;
    case PROP_PRINT_SETTINGS:
740
      g_value_take_object (value, gtk_print_unix_dialog_get_settings (dialog));
741 742
      break;
    case PROP_SELECTED_PRINTER:
743
      g_value_set_object (value, priv->current_printer);
744 745 746 747 748 749 750 751 752 753 754 755 756 757
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static gboolean
is_printer_active (GtkTreeModel       *model,
                   GtkTreeIter        *iter,
		   GtkPrintUnixDialog *dialog)
{
  gboolean result;
  GtkPrinter *printer;
758
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
759 760 761 762 763 764 765 766 767 768 769

  gtk_tree_model_get (model,
		      iter,
		      PRINTER_LIST_COL_PRINTER_OBJ,
                      &printer,
		      -1);
  
  if (printer == NULL)
    return FALSE;

  result = gtk_printer_is_active (printer);
770
  
771 772 773
  if (result && 
      priv->manual_capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF |
				   GTK_PRINT_CAPABILITY_GENERATE_PS))
774
    {
775 776 777 778 779 780 781
       /* Check that the printer can handle at least one of the data 
	* formats that the application supports.
        */
       result = ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF) &&
		 gtk_printer_accepts_pdf (printer)) ||
		((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS) &&
		 gtk_printer_accepts_ps (printer));
782
    }
783
  
784
  g_object_unref (printer);
785
  
786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842
  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;

  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, 
                      PRINTER_LIST_COL_NAME, &b_name,
		      PRINTER_LIST_COL_PRINTER_OBJ, &b_printer,
		      -1);

  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)
{
843
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
844 845 846 847 848 849 850 851 852 853 854
  GtkListStore *model;
  GtkTreeSortable *sort;

  model = gtk_list_store_new (PRINTER_LIST_N_COLS,
                              G_TYPE_STRING,
                              G_TYPE_STRING, 
                              G_TYPE_STRING, 
                              G_TYPE_INT, 
                              G_TYPE_STRING,
                              G_TYPE_OBJECT);

855 856
  priv->printer_list = (GtkTreeModel *)model;
  priv->printer_list_filter = (GtkTreeModelFilter *) gtk_tree_model_filter_new ((GtkTreeModel *)model,
857 858
											NULL);

859
  gtk_tree_model_filter_set_visible_func (priv->printer_list_filter,
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
					  (GtkTreeModelFilterVisibleFunc) is_printer_active,
					  dialog,
					  NULL);

  sort = GTK_TREE_SORTABLE (model);
  gtk_tree_sortable_set_default_sort_func (sort,
					   default_printer_list_sort_func,
					   NULL,
					   NULL);
 
  gtk_tree_sortable_set_sort_column_id (sort,
					GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
					GTK_SORT_ASCENDING);

}


static GtkWidget *
wrap_in_frame (const gchar *label, 
               GtkWidget   *child)
{
  GtkWidget *frame, *alignment, *label_widget;
Matthias Clasen's avatar
Matthias Clasen committed
882
  gchar *bold_text;
883

884
  label_widget = gtk_label_new (NULL);
885
  gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
886 887 888 889 890 891
  gtk_widget_show (label_widget);
  
  bold_text = g_markup_printf_escaped ("<b>%s</b>", label);
  gtk_label_set_markup (GTK_LABEL (label_widget), bold_text);
  g_free (bold_text);
  
892 893
  frame = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
894
  
895
  alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
896
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
897 898
			     0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (frame), alignment, FALSE, FALSE, 0);
899 900 901 902 903 904 905 906 907 908 909 910 911 912

  gtk_container_add (GTK_CONTAINER (alignment), child);

  gtk_widget_show (frame);
  gtk_widget_show (alignment);
  
  return frame;
}

static gboolean
setup_option (GtkPrintUnixDialog     *dialog,
	      const gchar            *option_name,
	      GtkPrinterOptionWidget *widget)
{
913
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
914 915
  GtkPrinterOption *option;

916
  option = gtk_printer_option_set_lookup (priv->options, option_name);
917 918 919 920 921 922
  gtk_printer_option_widget_set_source (widget, option);

  return option != NULL;
}

static void
923
add_option_to_extension_point (GtkPrinterOption *option,
Matthias Clasen's avatar
~  
Matthias Clasen committed
924
		               gpointer          data)
925
{
Matthias Clasen's avatar
~  
Matthias Clasen committed
926 927
  GtkWidget *extension_point = data;
  GtkWidget *widget;
928

Matthias Clasen's avatar
~  
Matthias Clasen committed
929 930
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);
931
   
Matthias Clasen's avatar
~  
Matthias Clasen committed
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946
  if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
    {
      GtkWidget *label, *hbox;
      
      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);
      
      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);
      
      gtk_box_pack_start (GTK_BOX (extension_point), hbox, FALSE, FALSE, 0);
947 948
    }
  else
Matthias Clasen's avatar
~  
Matthias Clasen committed
949
    gtk_box_pack_start (GTK_BOX (extension_point), widget, FALSE, FALSE, 0);
950 951 952 953 954 955 956 957
}

static void
add_option_to_table (GtkPrinterOption *option,
		     gpointer          user_data)
{
  GtkTable *table;
  GtkWidget *label, *widget;
Matthias Clasen's avatar
Matthias Clasen committed
958
  gint row;
959 960 961 962 963 964 965 966 967 968

  table = GTK_TABLE (user_data);
  
  if (g_str_has_prefix (option->name, "gtk-"))
    return;
  
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);

  row = table->nrows;
969
  gtk_table_resize (table, table->nrows + 1, 2);
970 971 972 973 974 975 976
  
  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);
977 978
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);

979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008
      gtk_table_attach (table, label,
			0, 1, row - 1 , row,  GTK_FILL, 0, 0, 0);
      
      gtk_table_attach (table, widget,
			1, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
    }
  else
    gtk_table_attach (table, widget,
		      0, 2, row - 1, row,  GTK_FILL, 0, 0, 0);
}


static void
setup_page_table (GtkPrinterOptionSet *options,
		  const gchar         *group,
		  GtkWidget           *table,
		  GtkWidget           *page)
{
  gtk_printer_option_set_foreach_in_group (options, group,
					   add_option_to_table,
					   table);
  if (GTK_TABLE (table)->nrows == 1)
    gtk_widget_hide (page);
  else
    gtk_widget_show (page);
}

static void
update_print_at_option (GtkPrintUnixDialog *dialog)
{
1009
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1010 1011
  GtkPrinterOption *option;
  
1012
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1013 1014 1015 1016

  if (option == NULL)
    return;
  
1017
  if (priv->updating_print_at)
1018 1019
    return;
  
1020
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_at_radio)))
1021
    gtk_printer_option_set (option, "at");
1022
  else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio)))
1023 1024 1025 1026
    gtk_printer_option_set (option, "on-hold");
  else
    gtk_printer_option_set (option, "now");
  
1027
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1028 1029
  if (option != NULL)
    {
1030
      const char *text = gtk_entry_get_text (GTK_ENTRY (priv->print_at_entry));
1031 1032 1033 1034 1035 1036 1037 1038
      gtk_printer_option_set (option,text);
    }
}


static gboolean
setup_print_at (GtkPrintUnixDialog *dialog)
{
1039
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1040 1041
  GtkPrinterOption *option;
  
1042
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
1043 1044 1045
 
  if (option == NULL)
    {
1046
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
1047
				    TRUE);
1048 1049 1050 1051
      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), "");
1052 1053 1054
      return FALSE;
    }

1055
  priv->updating_print_at = TRUE;
1056 1057 1058
  
  if (gtk_printer_option_has_choice (option, "at"))
    {
1059 1060
      gtk_widget_set_sensitive (priv->print_at_radio, TRUE);
      gtk_widget_set_sensitive (priv->print_at_entry, TRUE);
1061 1062 1063
    }
  else
    {
1064 1065
      gtk_widget_set_sensitive (priv->print_at_radio, FALSE);
      gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
1066 1067
    }
  
1068
  gtk_widget_set_sensitive (priv->print_hold_radio,
1069 1070 1071 1072 1073
			    gtk_printer_option_has_choice (option, "on-hold"));

  update_print_at_option (dialog);

  if (strcmp (option->value, "at") == 0)
1074
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_at_radio),
1075 1076
				  TRUE);
  else if (strcmp (option->value, "on-hold") == 0)
1077
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio),
1078 1079
				  TRUE);
  else
1080
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
1081 1082
				  TRUE);

1083
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
1084
  if (option != NULL)
1085
    gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry),
1086 1087 1088
			option->value);
  

1089
  priv->updating_print_at = FALSE;
1090 1091 1092 1093 1094 1095 1096

  return TRUE;
}
	     
static void
update_dialog_from_settings (GtkPrintUnixDialog *dialog)
{
1097
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1098
  GList *groups, *l;
Matthias Clasen's avatar
Matthias Clasen committed
1099
  gchar *group;
1100 1101 1102
  GtkWidget *table, *frame;
  gboolean has_advanced, has_job;
 
1103
  if (priv->current_printer == NULL)
1104 1105
    {
       clear_per_printer_ui (dialog);
1106 1107 1108 1109 1110
       gtk_widget_hide (priv->job_page);
       gtk_widget_hide (priv->advanced_page);
       gtk_widget_hide (priv->image_quality_page);
       gtk_widget_hide (priv->finishing_page);
       gtk_widget_hide (priv->color_page);
1111 1112 1113 1114 1115
       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);

       return;
    }
 
1116 1117 1118 1119 1120
  setup_option (dialog, "gtk-n-up", priv->pages_per_sheet);
  setup_option (dialog, "gtk-duplex", priv->duplex);
  setup_option (dialog, "gtk-paper-type", priv->paper_type);
  setup_option (dialog, "gtk-paper-source", priv->paper_source);
  setup_option (dialog, "gtk-output-tray", priv->output_tray);
1121 1122

  has_job = FALSE;
1123 1124 1125 1126
  has_job |= setup_option (dialog, "gtk-job-prio", priv->job_prio);
  has_job |= setup_option (dialog, "gtk-billing-info", priv->billing_info);
  has_job |= setup_option (dialog, "gtk-cover-before", priv->cover_before);
  has_job |= setup_option (dialog, "gtk-cover-after", priv->cover_after);
1127 1128 1129
  has_job |= setup_print_at (dialog);
  
  if (has_job)
1130
    gtk_widget_show (priv->job_page);
1131
  else
1132
    gtk_widget_hide (priv->job_page);
1133 1134

  
1135
  setup_page_table (priv->options,
1136
		    "ImageQualityPage",
1137 1138
		    priv->image_quality_table,
		    priv->image_quality_page);
1139
  
1140
  setup_page_table (priv->options,
1141
		    "FinishingPage",
1142 1143
		    priv->finishing_table,
		    priv->finishing_page);
1144

1145
  setup_page_table (priv->options,
1146
		    "ColorPage",
1147 1148
		    priv->color_table,
		    priv->color_page);
1149

Matthias Clasen's avatar
~  
Matthias Clasen committed
1150 1151 1152 1153 1154
  gtk_printer_option_set_foreach_in_group (priv->options,
					   "GtkPrintDialogExtension",
					   add_option_to_extension_point,
					   priv->extension_point);

1155
  /* Put the rest of the groups in the advanced page */
1156
  groups = gtk_printer_option_set_get_groups (priv->options);
1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167

  has_advanced = FALSE;
  for (l = groups; l != NULL; l = l->next)
    {
      group = l->data;

      if (group == NULL)
	continue;
      
      if (strcmp (group, "ImageQualityPage") == 0 ||
	  strcmp (group, "ColorPage") == 0 ||
Matthias Clasen's avatar
~  
Matthias Clasen committed
1168 1169
	  strcmp (group, "FinishingPage") == 0 ||
	  strcmp (group, "GtkPrintDialogExtension") == 0)
1170 1171 1172 1173 1174 1175
	continue;

      table = gtk_table_new (1, 2, FALSE);
      gtk_table_set_row_spacings (GTK_TABLE (table), 6);
      gtk_table_set_col_spacings (GTK_TABLE (table), 12);
      
1176
      gtk_printer_option_set_foreach_in_group (priv->options,
1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188
					       group,
					       add_option_to_table,
					       table);
      if (GTK_TABLE (table)->nrows == 1)
	gtk_widget_destroy (table);
      else
	{
	  has_advanced = TRUE;
	  frame = wrap_in_frame (group, table);
	  gtk_widget_show (table);
	  gtk_widget_show (frame);
	  
1189
	  gtk_box_pack_start (GTK_BOX (priv->advanced_vbox),
1190 1191 1192 1193 1194
			      frame, FALSE, FALSE, 0);
	}
    }

  if (has_advanced)
1195
    gtk_widget_show (priv->advanced_page);
1196
  else
1197
    gtk_widget_hide (priv->advanced_page);
1198 1199 1200 1201 1202 1203

  
  g_list_foreach (groups, (GFunc) g_free, NULL);
  g_list_free (groups);
}

1204 1205 1206 1207 1208
static void
update_dialog_from_capabilities (GtkPrintUnixDialog *dialog)
{
  GtkPrintCapabilities caps;
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1209
  gboolean can_collate;
Matthias Clasen's avatar
Matthias Clasen committed
1210
  const gchar *copies;
1211

Matthias Clasen's avatar
Matthias Clasen committed
1212 1213
  copies = gtk_entry_get_text (GTK_ENTRY (priv->copies_spin));
  can_collate = (*copies != '\0' && atoi (copies) > 1);
1214 1215 1216 1217 1218 1219 1220 1221

  caps = priv->manual_capabilities | priv->printer_capabilities;

  gtk_widget_set_sensitive (priv->page_set_combo,
			    caps & GTK_PRINT_CAPABILITY_PAGE_SET);
  gtk_widget_set_sensitive (priv->copies_spin,
			    caps & GTK_PRINT_CAPABILITY_COPIES);
  gtk_widget_set_sensitive (priv->collate_check,
1222 1223
			    can_collate && 
			    (caps & GTK_PRINT_CAPABILITY_COLLATE));
1224 1225 1226
  gtk_widget_set_sensitive (priv->reverse_check,
			    caps & GTK_PRINT_CAPABILITY_REVERSE);
  gtk_widget_set_sensitive (priv->scale_spin,
1227
			    caps & GTK_PRINT_CAPABILITY_SCALE);
1228 1229
  gtk_widget_set_sensitive (GTK_WIDGET (priv->pages_per_sheet),
			    caps & GTK_PRINT_CAPABILITY_NUMBER_UP);
1230

1231 1232 1233 1234 1235
  if (caps & GTK_PRINT_CAPABILITY_PREVIEW)
    gtk_widget_show (priv->preview_button);
  else
    gtk_widget_hide (priv->preview_button);

1236
  update_collate_icon (NULL, dialog);
1237 1238

  gtk_tree_model_filter_refilter (priv->printer_list_filter);
1239 1240
}

1241 1242 1243
static void
mark_conflicts (GtkPrintUnixDialog *dialog)
{
1244
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1245 1246 1247 1248 1249
  GtkPrinter *printer;
  gboolean have_conflict;

  have_conflict = FALSE;

1250
  printer = priv->current_printer;
1251 1252 1253 1254

  if (printer)
    {

1255 1256
      g_signal_handler_block (priv->options,
			      priv->options_changed_handler);
1257
      
1258
      gtk_printer_option_set_clear_conflicts (priv->options);
1259 1260
      
      have_conflict = _gtk_printer_mark_conflicts (printer,
1261
						   priv->options);
1262
      
1263 1264
      g_signal_handler_unblock (priv->options,
				priv->options_changed_handler);
1265 1266 1267
    }

  if (have_conflict)
1268
    gtk_widget_show (priv->conflicts_widget);
1269
  else
1270
    gtk_widget_hide (priv->conflicts_widget);
1271 1272 1273 1274 1275 1276
}

static gboolean
mark_conflicts_callback (gpointer data)
{
  GtkPrintUnixDialog *dialog = data;
1277
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1278

1279
  priv->mark_conflicts_id = 0;
1280 1281 1282 1283 1284 1285 1286 1287 1288

  mark_conflicts (dialog);

  return FALSE;
}

static void
unschedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
{
1289 1290 1291
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->mark_conflicts_id != 0)
1292
    {
1293 1294
      g_source_remove (priv->mark_conflicts_id);
      priv->mark_conflicts_id = 0;
1295 1296 1297 1298 1299 1300
    }
}

static void
schedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
{
1301 1302 1303
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->mark_conflicts_id != 0)
1304 1305
    return;

1306
  priv->mark_conflicts_id = gdk_threads_add_idle (mark_conflicts_callback,
Matthias Clasen's avatar
Matthias Clasen committed
1307
					dialog);
1308 1309 1310 1311 1312
}

static void
options_changed_cb (GtkPrintUnixDialog *dialog)
{
1313 1314
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

1315 1316
  schedule_idle_mark_conflicts (dialog);

1317
  if (priv->initial_settings)
1318
    {
1319 1320
      g_object_unref (priv->initial_settings);
      priv->initial_settings = NULL;
1321 1322
    }

1323 1324
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;
1325 1326 1327 1328 1329 1330 1331 1332 1333 1334
}

static void
remove_custom_widget (GtkWidget    *widget,
                      GtkContainer *container)
{
  gtk_container_remove (container, widget);
}

static void
Matthias Clasen's avatar
~  
Matthias Clasen committed
1335
extension_point_clear_children (GtkContainer *container)
1336 1337 1338 1339 1340 1341 1342 1343 1344
{
  gtk_container_foreach (container,
                         (GtkCallback)remove_custom_widget,
                         container);
}

static void
clear_per_printer_ui (GtkPrintUnixDialog *dialog)
{
1345 1346 1347
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  gtk_container_foreach (GTK_CONTAINER (priv->finishing_table),
1348 1349
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1350 1351
  gtk_table_resize (GTK_TABLE (priv->finishing_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->image_quality_table),
1352 1353
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1354 1355
  gtk_table_resize (GTK_TABLE (priv->image_quality_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->color_table),
1356 1357
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1358 1359
  gtk_table_resize (GTK_TABLE (priv->color_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->advanced_vbox),
1360 1361
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1362
  extension_point_clear_children (GTK_CONTAINER (priv->extension_point));
1363 1364 1365 1366 1367 1368 1369
}

static void
printer_details_acquired (GtkPrinter         *printer,
			  gboolean            success,
			  GtkPrintUnixDialog *dialog)
{
1370 1371
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

1372
  disconnect_printer_details_request (dialog);
1373 1374 1375 1376
  
  if (success)
    {
      GtkTreeSelection *selection;
1377
      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
1378 1379 1380 1381 1382 1383 1384 1385 1386
      
      selected_printer_changed (selection, dialog);
    }
}

static void
selected_printer_changed (GtkTreeSelection   *selection,
			  GtkPrintUnixDialog *dialog)
{
1387
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1388 1389 1390 1391 1392
  GtkPrinter *printer;
  GtkTreeIter iter, filter_iter;

  /* Whenever the user selects a printer we stop looking for
     the printer specified in the initial settings */
1393 1394
  if (priv->waiting_for_printer &&
      !priv->internal_printer_change)
1395
    {
1396 1397
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
1398
    }
1399 1400 1401

  disconnect_printer_details_request (dialog);

1402 1403 1404
  printer = NULL;
  if (gtk_tree_selection_get_selected (selection, NULL, &filter_iter))
    {
1405
      gtk_tree_model_filter_convert_iter_to_child_iter (priv->printer_list_filter,
1406 1407 1408
							&iter,
							&filter_iter);

1409
      gtk_tree_model_get (priv->printer_list, &iter,
1410 1411 1412 1413
  			  PRINTER_LIST_COL_PRINTER_OBJ, &printer,
			  -1);
    }
  
1414
  if (printer != NULL && !gtk_printer_has_details (printer))
1415 1416
    {
      gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1417
      priv->request_details_tag =
1418 1419
	g_signal_connect (printer, "details-acquired",
			  G_CALLBACK (printer_details_acquired), dialog);
1420 1421
      /* take the reference */
      priv->request_details_printer = printer;
1422
      gtk_printer_request_details (printer);
1423 1424 1425
      return;
    }
  
1426
  if (printer == priv->current_printer)
1427 1428 1429 1430 1431 1432
    {
      if (printer)
	g_object_unref (printer);
      return;
    }

1433
  if (priv->options)
1434
    {
1435 1436
      g_object_unref (priv->options);
      priv->options = NULL;  
1437 1438 1439 1440

      clear_per_printer_ui (dialog);
    }

1441
  if (priv->current_printer)
1442
    {
1443
      g_object_unref (priv->current_printer);
1444 1445
    }

1446 1447
  priv->printer_capabilities = 0;
  
1448
  gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
1449
  priv->current_printer = printer;
1450 1451 1452

  if (printer != NULL)
    {
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466
      if (!priv->page_setup_set)
	{
	  /* if no explicit page setup has been set, use the printer default */	  
  	  GtkPageSetup *page_setup;

	  page_setup = gtk_printer_get_default_page_size (printer);

	  if (!page_setup)
	    page_setup = gtk_page_setup_new ();

	  g_object_unref (priv->page_setup);
	  priv->page_setup = page_setup;
	}

1467
      priv->printer_capabilities = gtk_printer_get_capabilities (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1468 1469 1470 1471
      priv->options = _gtk_printer_get_options (printer, 
						priv->initial_settings,
						priv->page_setup,
						priv->manual_capabilities);
1472
  
1473 1474
      priv->options_changed_handler = 
        g_signal_connect_swapped (priv->options, "changed", G_CALLBACK (options_changed_cb), dialog);
1475 1476 1477
    }

  update_dialog_from_settings (dialog);
1478
  update_dialog_from_capabilities (dialog);
1479
}
1480

1481 1482 1483 1484
static void
update_collate_icon (GtkToggleButton    *toggle_button,
		     GtkPrintUnixDialog *dialog)
{
1485
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1486 1487 1488 1489 1490 1491 1492 1493 1494 1495

  gtk_widget_queue_draw (priv->collate_image);
}

static void
paint_page (GtkWidget *widget,
	    cairo_t   *cr, 
	    gfloat     scale,
	    gint       x_offset, 
	    gint       y_offset,
1496 1497
	    gchar     *text,
	    gint       text_x)
1498 1499
{
  gint x, y, width, height;
1500
  gint text_y, linewidth;
1501 1502 1503 1504 1505 1506

  x = x_offset * scale;
  y = y_offset * scale;
  width = 20 * scale;
  height = 26 * scale;

1507 1508 1509
  linewidth = 2;
  text_y = 21;

1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522
  gdk_cairo_set_source_color (cr, &widget->style->base[GTK_STATE_NORMAL]);
  cairo_rectangle (cr, x, y, width, height);
  cairo_fill (cr);

  gdk_cairo_set_source_color (cr, &widget->style->text[GTK_STATE_NORMAL]);
  cairo_set_line_width (cr, linewidth);
  cairo_rectangle (cr, x + linewidth/2.0, y + linewidth/2.0, width - linewidth, height - linewidth);
  cairo_stroke (cr);

  cairo_select_font_face (cr, "Sans", 
			  CAIRO_FONT_SLANT_NORMAL,
			  CAIRO_FONT_WEIGHT_NORMAL);
  cairo_set_font_size (cr, (gint)(9 * scale));
1523
  cairo_move_to (cr, x + (gint)(text_x * scale), y + (gint)(text_y * scale));
1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536
  cairo_show_text (cr, text);
}


static gboolean
draw_collate_cb (GtkWidget	    *widget,
		 GdkEventExpose     *event,
		 GtkPrintUnixDialog *dialog)
{
  GtkSettings *settings;
  cairo_t *cr;
  gint size;
  gfloat scale;
1537
  gboolean collate, reverse, rtl;
1538
  gint copies;
1539
  gint text_x;
1540

1541 1542
  collate = dialog_get_collate (dialog);
  reverse = dialog_get_reverse (dialog);
1543 1544
  copies = dialog_get_n_copies (dialog);

1545
  rtl = (gtk_widget_get_direction (GTK_WIDGET (widget)) == GTK_TEXT_DIR_RTL);
1546

1547 1548 1549 1550 1551 1552
  settings = gtk_widget_get_settings (widget);
  gtk_icon_size_lookup_for_settings (settings,
				     GTK_ICON_SIZE_DIALOG,
				     &size,
				     NULL);
  scale = size / 48.0;
1553
  text_x = rtl ? 4 : 11;
1554 1555 1556

  cr = gdk_cairo_create (widget->window);

1557 1558
  cairo_translate (cr, widget->allocation.x, widget->allocation.y);

1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571
  if (copies == 1)
    {
      paint_page (widget, cr, scale, rtl ? 40: 15, 5, reverse ? "1" : "2", text_x);
      paint_page (widget, cr, scale, rtl ? 50: 5, 15, reverse ? "2" : "1", text_x);
    }
  else
    {
      paint_page (widget, cr, scale, rtl ? 40: 15, 5, collate == reverse ? "1" : "2", text_x);
      paint_page (widget, cr, scale, rtl ? 50: 5, 15, reverse ? "2" : "1", text_x);

      paint_page (widget, cr, scale, rtl ? 5 : 50, 5, reverse ? "1" : "2", text_x);
      paint_page (widget, cr, scale, rtl ? 15 : 40, 15, collate == reverse ? "2" : "1", text_x);
    }
1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585

  cairo_destroy (cr);

  return TRUE;
}

static void
gtk_print_unix_dialog_style_set (GtkWidget *widget,
				 GtkStyle  *previous_style)
{
  if (GTK_WIDGET_CLASS (gtk_print_unix_dialog_parent_class)->style_set)
    GTK_WIDGET_CLASS (gtk_print_unix_dialog_parent_class)->style_set (widget, previous_style);

  if (gtk_widget_has_screen (widget))