gtkprintunixdialog.c 80.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/* GtkPrintUnixDialog
 * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
 * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
 *
 * 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>
Yevgen Muntyan's avatar
Yevgen Muntyan committed
24
#include <ctype.h>
25 26 27 28 29 30 31 32 33 34
#include <stdio.h>
#include <math.h>

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

#include "gtkspinbutton.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkstock.h"
35
#include "gtkiconfactory.h"
36 37 38 39 40 41 42 43 44 45 46 47 48
#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"
49 50
#include "gtktooltips.h"
#include "gtkeventbox.h"
51 52

#include "gtkprintbackend.h"
53
#include "gtkprinter-private.h"
54 55 56 57 58 59 60 61 62
#include "gtkprintunixdialog.h"
#include "gtkprinteroptionwidget.h"
#include "gtkalias.h"

#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))

63 64 65 66 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
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);
93
static gint     dialog_get_n_copies                (GtkPrintUnixDialog *dialog);
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117

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;
118 119 120

  GtkPrintCapabilities manual_capabilities;
  GtkPrintCapabilities printer_capabilities;
121 122 123 124 125 126
  
  GtkTreeModel *printer_list;
  GtkTreeModelFilter *printer_list_filter;

  GtkPageSetup *page_setup;

127
  GtkTooltips *tooltips;
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
  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;
Matthias Clasen's avatar
Matthias Clasen committed
144
  GtkWidget *preview_button;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
  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
169
  GtkWidget *extension_point;
170

Matthias Clasen's avatar
Matthias Clasen committed
171 172 173 174
  /* 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.
   */
175 176 177 178 179
  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
180 181
   * this printer.
   */
182 183 184 185 186 187 188 189 190 191 192
  char *waiting_for_printer;
  gboolean internal_printer_change;
  
  GList *print_backends;
  
  GtkPrinter *current_printer;
  guint request_details_tag;
  GtkPrinterOptionSet *options;
  gulong options_changed_handler;
  gulong mark_conflicts_id;

Matthias Clasen's avatar
Matthias Clasen committed
193
  gchar *format_for_printer;
194 195 196 197
  
  gint current_page;
};

Matthias Clasen's avatar
Matthias Clasen committed
198
G_DEFINE_TYPE (GtkPrintUnixDialog, gtk_print_unix_dialog, GTK_TYPE_DIALOG)
199 200 201

static gboolean
is_default_printer (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
202
		    GtkPrinter         *printer)
203
{
204 205 206 207
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->format_for_printer)
    return strcmp (priv->format_for_printer,
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
		   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;

226 227
  widget_class->style_set = gtk_print_unix_dialog_style_set;

228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
  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"),
258
							P_("The GtkPrinter which is selected"),
259 260 261
							GTK_TYPE_PRINTER,
							GTK_PARAM_READABLE));
  
Alexander Larsson's avatar
Alexander Larsson committed
262
  
263 264 265 266 267 268
  g_type_class_add_private (class, sizeof (GtkPrintUnixDialogPrivate));  
}

static void
gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
{
269
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
270

271 272 273
  priv = dialog->priv = GTK_PRINT_UNIX_DIALOG_GET_PRIVATE (dialog); 
  priv->print_backends = NULL;
  priv->current_page = -1;
274

275 276 277
  priv->tooltips = gtk_tooltips_new ();
  g_object_ref_sink (priv->tooltips);

278
  priv->page_setup = gtk_page_setup_new ();
279 280 281 282 283 284

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

Matthias Clasen's avatar
Matthias Clasen committed
285 286 287 288 289 290
  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);
291
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
292 293 294 295 296 297
			  GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
			  GTK_STOCK_PRINT, GTK_RESPONSE_OK,
                          NULL);

  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
298

299
  populate_dialog (dialog);  
Alexander Larsson's avatar
Alexander Larsson committed
300 301
}

302 303 304 305 306 307 308 309 310 311 312
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);  
}

static void
gtk_print_unix_dialog_finalize (GObject *object)
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
313
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
314 315
  GtkPrintBackend *backend;
  GList *node;
316 317 318

  unschedule_idle_mark_conflicts (dialog);

319 320 321 322 323 324
  if (priv->tooltips)
    {
      g_object_unref (priv->tooltips);
      priv->tooltips = NULL;
    }

325
  if (priv->request_details_tag)
326
    {
327 328
      g_source_remove (priv->request_details_tag);
      priv->request_details_tag = 0;
329 330
    }
  
331
  if (priv->current_printer)
332
    {
333 334
      g_object_unref (priv->current_printer);
      priv->current_printer = NULL;
335 336
    }

337
  if (priv->printer_list)
338
    {
339 340
      g_object_unref (priv->printer_list);
      priv->printer_list = NULL;
341 342
    }
 
343
  if (priv->printer_list_filter)
344
    {
345 346
      g_object_unref (priv->printer_list_filter);
      priv->printer_list_filter = NULL;
347 348 349
    }

 
350
  if (priv->options)
351
    {
352 353
      g_object_unref (priv->options);
      priv->options = NULL;
354 355
    }
 
Matthias Clasen's avatar
~  
Matthias Clasen committed
356
 if (priv->page_setup)
357
    {
358 359
      g_object_unref (priv->page_setup);
      priv->page_setup = NULL;
360 361
    }

362
  if (priv->initial_settings)
363
    {
364 365
      g_object_unref (priv->initial_settings);
      priv->initial_settings = NULL;
366 367
    }

368 369
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;
370
  
371 372 373
  g_free (priv->format_for_printer);
  priv->format_for_printer = NULL;

Alexander Larsson's avatar
Alexander Larsson committed
374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
  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;
  
389
  G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
390 391 392 393 394 395 396
}

static void
printer_removed_cb (GtkPrintBackend    *backend, 
                    GtkPrinter         *printer, 
		    GtkPrintUnixDialog *dialog)
{
397
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
398
  GtkTreeIter *iter;
399

400
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
401
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
402 403 404 405 406 407 408
}

static void
printer_status_cb (GtkPrintBackend    *backend, 
		   GtkPrinter         *printer, 
		   GtkPrintUnixDialog *dialog)
{
409
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
410
  GtkTreeIter *iter;
411

412 413
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");

414
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), iter,
415 416 417 418 419 420 421 422 423 424 425 426 427
                      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)
{
428
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
429 430
  GtkTreeIter iter, filter_iter;
  GtkTreeSelection *selection;
431
  GtkTreePath *path;
432

433
  gtk_list_store_append (GTK_LIST_STORE (priv->printer_list), &iter);
434 435 436 437 438 439
  
  g_object_set_data_full (G_OBJECT (printer), 
                         "gtk-print-tree-iter", 
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);

440
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), &iter,
441 442 443 444 445 446 447 448
                      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);

449
  gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
450
						    &filter_iter, &iter);
451 452
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->printer_list_filter), &filter_iter);

453
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
454
  
455
  if (priv->waiting_for_printer != NULL &&
456
      strcmp (gtk_printer_get_name (printer),
457
	      priv->waiting_for_printer) == 0)
458
    {
459
      priv->internal_printer_change = TRUE;
460
      gtk_tree_selection_select_iter (selection, &filter_iter);
461 462
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
463 464 465
      priv->internal_printer_change = FALSE;
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
466 467 468 469
    }
  else if (is_default_printer (dialog, printer) &&
	   gtk_tree_selection_count_selected_rows (selection) == 0)
    {
470
      priv->internal_printer_change = TRUE;
471
      gtk_tree_selection_select_iter (selection, &filter_iter);
472 473
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
474
      priv->internal_printer_change = FALSE;
475
    }
476 477

  gtk_tree_path_free (path);
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518
}

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)
{
519
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
520 521 522
  GList *node;

  if (g_module_supported ())
523
    priv->print_backends = gtk_print_backend_load_modules ();
524

525
  for (node = priv->print_backends; node != NULL; node = node->next)
526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561
    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);
562
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
563 564 565 566

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
567
      g_value_set_object (value, priv->page_setup);
568 569
      break;
    case PROP_CURRENT_PAGE:
570
      g_value_set_int (value, priv->current_page);
571 572 573 574 575
      break;
    case PROP_PRINT_SETTINGS:
      g_value_set_object (value, gtk_print_unix_dialog_get_settings (dialog));
      break;
    case PROP_SELECTED_PRINTER:
576
      g_value_set_object (value, priv->current_printer);
577 578 579 580 581 582 583 584 585 586 587 588 589 590
      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;
591
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
592 593 594 595 596 597 598 599 600 601 602

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

  result = gtk_printer_is_active (printer);
603
  
604 605 606
  if (result && 
      priv->manual_capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF |
				   GTK_PRINT_CAPABILITY_GENERATE_PS))
607
    {
608 609 610 611 612 613 614
       /* 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));
615
    }
616
  
617
  g_object_unref (printer);
618
  
619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 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
  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)
{
676
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
677 678 679 680 681 682 683 684 685 686 687
  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);

688 689
  priv->printer_list = (GtkTreeModel *)model;
  priv->printer_list_filter = (GtkTreeModelFilter *) gtk_tree_model_filter_new ((GtkTreeModel *)model,
690 691
											NULL);

692
  gtk_tree_model_filter_set_visible_func (priv->printer_list_filter,
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714
					  (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
715
  gchar *bold_text;
716 717

  label_widget = gtk_label_new ("");
718
  gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
719 720 721 722 723 724
  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);
  
725 726
  frame = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
727
  
728
  alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
729
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
730 731
			     0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (frame), alignment, FALSE, FALSE, 0);
732 733 734 735 736 737 738 739 740 741 742 743 744 745

  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)
{
746
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
747 748
  GtkPrinterOption *option;

749
  option = gtk_printer_option_set_lookup (priv->options, option_name);
750 751 752 753 754 755
  gtk_printer_option_widget_set_source (widget, option);

  return option != NULL;
}

static void
756
add_option_to_extension_point (GtkPrinterOption *option,
Matthias Clasen's avatar
~  
Matthias Clasen committed
757
		               gpointer          data)
758
{
Matthias Clasen's avatar
~  
Matthias Clasen committed
759 760
  GtkWidget *extension_point = data;
  GtkWidget *widget;
761

Matthias Clasen's avatar
~  
Matthias Clasen committed
762 763
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);
764
   
Matthias Clasen's avatar
~  
Matthias Clasen committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778 779
  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);
780 781
    }
  else
Matthias Clasen's avatar
~  
Matthias Clasen committed
782
    gtk_box_pack_start (GTK_BOX (extension_point), widget, FALSE, FALSE, 0);
783 784 785 786 787 788 789 790
}

static void
add_option_to_table (GtkPrinterOption *option,
		     gpointer          user_data)
{
  GtkTable *table;
  GtkWidget *label, *widget;
Matthias Clasen's avatar
Matthias Clasen committed
791
  gint row;
792 793 794 795 796 797 798 799 800 801

  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;
802
  gtk_table_resize (table, table->nrows + 1, 2);
803 804 805 806 807 808 809
  
  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);
810 811
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);

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
      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)
{
842
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
843 844
  GtkPrinterOption *option;
  
845
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
846 847 848 849

  if (option == NULL)
    return;
  
850
  if (priv->updating_print_at)
851 852
    return;
  
853
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_at_radio)))
854
    gtk_printer_option_set (option, "at");
855
  else if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio)))
856 857 858 859
    gtk_printer_option_set (option, "on-hold");
  else
    gtk_printer_option_set (option, "now");
  
860
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
861 862
  if (option != NULL)
    {
863
      const char *text = gtk_entry_get_text (GTK_ENTRY (priv->print_at_entry));
864 865 866 867 868 869 870 871
      gtk_printer_option_set (option,text);
    }
}


static gboolean
setup_print_at (GtkPrintUnixDialog *dialog)
{
872
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
873 874
  GtkPrinterOption *option;
  
875
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time");
876 877 878
 
  if (option == NULL)
    {
879
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
880
				    TRUE);
881 882 883 884
      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), "");
885 886 887
      return FALSE;
    }

888
  priv->updating_print_at = TRUE;
889 890 891
  
  if (gtk_printer_option_has_choice (option, "at"))
    {
892 893
      gtk_widget_set_sensitive (priv->print_at_radio, TRUE);
      gtk_widget_set_sensitive (priv->print_at_entry, TRUE);
894 895 896
    }
  else
    {
897 898
      gtk_widget_set_sensitive (priv->print_at_radio, FALSE);
      gtk_widget_set_sensitive (priv->print_at_entry, FALSE);
899 900
    }
  
901
  gtk_widget_set_sensitive (priv->print_hold_radio,
902 903 904 905 906
			    gtk_printer_option_has_choice (option, "on-hold"));

  update_print_at_option (dialog);

  if (strcmp (option->value, "at") == 0)
907
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_at_radio),
908 909
				  TRUE);
  else if (strcmp (option->value, "on-hold") == 0)
910
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_hold_radio),
911 912
				  TRUE);
  else
913
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->print_now_radio),
914 915
				  TRUE);

916
  option = gtk_printer_option_set_lookup (priv->options, "gtk-print-time-text");
917
  if (option != NULL)
918
    gtk_entry_set_text (GTK_ENTRY (priv->print_at_entry),
919 920 921
			option->value);
  

922
  priv->updating_print_at = FALSE;
923 924 925 926 927 928 929

  return TRUE;
}
	     
static void
update_dialog_from_settings (GtkPrintUnixDialog *dialog)
{
930
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
931
  GList *groups, *l;
Matthias Clasen's avatar
Matthias Clasen committed
932
  gchar *group;
933 934 935
  GtkWidget *table, *frame;
  gboolean has_advanced, has_job;
 
936
  if (priv->current_printer == NULL)
937 938
    {
       clear_per_printer_ui (dialog);
939 940 941 942 943
       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);
944 945 946 947 948
       gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);

       return;
    }
 
949 950 951 952 953
  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);
954 955

  has_job = FALSE;
956 957 958 959
  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);
960 961 962
  has_job |= setup_print_at (dialog);
  
  if (has_job)
963
    gtk_widget_show (priv->job_page);
964
  else
965
    gtk_widget_hide (priv->job_page);
966 967

  
968
  setup_page_table (priv->options,
969
		    "ImageQualityPage",
970 971
		    priv->image_quality_table,
		    priv->image_quality_page);
972
  
973
  setup_page_table (priv->options,
974
		    "FinishingPage",
975 976
		    priv->finishing_table,
		    priv->finishing_page);
977

978
  setup_page_table (priv->options,
979
		    "ColorPage",
980 981
		    priv->color_table,
		    priv->color_page);
982

Matthias Clasen's avatar
~  
Matthias Clasen committed
983 984 985 986 987
  gtk_printer_option_set_foreach_in_group (priv->options,
					   "GtkPrintDialogExtension",
					   add_option_to_extension_point,
					   priv->extension_point);

988
  /* Put the rest of the groups in the advanced page */
989
  groups = gtk_printer_option_set_get_groups (priv->options);
990 991 992 993 994 995 996 997 998 999 1000

  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
1001 1002
	  strcmp (group, "FinishingPage") == 0 ||
	  strcmp (group, "GtkPrintDialogExtension") == 0)
1003 1004 1005 1006 1007 1008
	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);
      
1009
      gtk_printer_option_set_foreach_in_group (priv->options,
1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021
					       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);
	  
1022
	  gtk_box_pack_start (GTK_BOX (priv->advanced_vbox),
1023 1024 1025 1026 1027
			      frame, FALSE, FALSE, 0);
	}
    }

  if (has_advanced)
1028
    gtk_widget_show (priv->advanced_page);
1029
  else
1030
    gtk_widget_hide (priv->advanced_page);
1031 1032 1033 1034 1035 1036

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

1037 1038 1039 1040 1041
static void
update_dialog_from_capabilities (GtkPrintUnixDialog *dialog)
{
  GtkPrintCapabilities caps;
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1042
  gboolean can_collate;
Matthias Clasen's avatar
Matthias Clasen committed
1043
  const gchar *copies;
1044

Matthias Clasen's avatar
Matthias Clasen committed
1045 1046
  copies = gtk_entry_get_text (GTK_ENTRY (priv->copies_spin));
  can_collate = (*copies != '\0' && atoi (copies) > 1);
1047 1048 1049 1050 1051 1052 1053 1054

  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,
1055 1056
			    can_collate && 
			    (caps & GTK_PRINT_CAPABILITY_COLLATE));
1057 1058 1059
  gtk_widget_set_sensitive (priv->reverse_check,
			    caps & GTK_PRINT_CAPABILITY_REVERSE);
  gtk_widget_set_sensitive (priv->scale_spin,
1060
			    caps & GTK_PRINT_CAPABILITY_SCALE);
1061 1062
  gtk_widget_set_sensitive (GTK_WIDGET (priv->pages_per_sheet),
			    caps & GTK_PRINT_CAPABILITY_NUMBER_UP);
1063

Matthias Clasen's avatar
Matthias Clasen committed
1064 1065 1066 1067 1068
  if (caps & GTK_PRINT_CAPABILITY_PREVIEW)
    gtk_widget_show (priv->preview_button);
  else
    gtk_widget_hide (priv->preview_button);

1069
  update_collate_icon (NULL, dialog);
1070 1071

  gtk_tree_model_filter_refilter (priv->printer_list_filter);
1072 1073
}

1074 1075 1076
static void
mark_conflicts (GtkPrintUnixDialog *dialog)
{
1077
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1078 1079 1080 1081 1082
  GtkPrinter *printer;
  gboolean have_conflict;

  have_conflict = FALSE;

1083
  printer = priv->current_printer;
1084 1085 1086 1087

  if (printer)
    {

1088 1089
      g_signal_handler_block (priv->options,
			      priv->options_changed_handler);
1090
      
1091
      gtk_printer_option_set_clear_conflicts (priv->options);
1092 1093
      
      have_conflict = _gtk_printer_mark_conflicts (printer,
1094
						   priv->options);
1095
      
1096 1097
      g_signal_handler_unblock (priv->options,
				priv->options_changed_handler);
1098 1099 1100
    }

  if (have_conflict)
1101
    gtk_widget_show (priv->conflicts_widget);
1102
  else
1103
    gtk_widget_hide (priv->conflicts_widget);
1104 1105 1106 1107 1108 1109
}

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

1112
  priv->mark_conflicts_id = 0;
1113 1114 1115 1116 1117 1118 1119 1120 1121

  mark_conflicts (dialog);

  return FALSE;
}

static void
unschedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
{
1122 1123 1124
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->mark_conflicts_id != 0)
1125
    {
1126 1127
      g_source_remove (priv->mark_conflicts_id);
      priv->mark_conflicts_id = 0;
1128 1129 1130 1131 1132 1133
    }
}

static void
schedule_idle_mark_conflicts (GtkPrintUnixDialog *dialog)
{
1134 1135 1136
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->mark_conflicts_id != 0)
1137 1138
    return;

1139
  priv->mark_conflicts_id = gdk_threads_add_idle (mark_conflicts_callback,
Matthias Clasen's avatar
Matthias Clasen committed
1140
					dialog);
1141 1142 1143 1144 1145
}

static void
options_changed_cb (GtkPrintUnixDialog *dialog)
{
1146 1147
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

1148 1149
  schedule_idle_mark_conflicts (dialog);

1150
  if (priv->initial_settings)
1151
    {
1152 1153
      g_object_unref (priv->initial_settings);
      priv->initial_settings = NULL;
1154 1155
    }

1156 1157
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;
1158 1159 1160 1161 1162 1163 1164 1165 1166 1167
}

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

static void
Matthias Clasen's avatar
~  
Matthias Clasen committed
1168
extension_point_clear_children (GtkContainer *container)
1169 1170 1171 1172 1173 1174 1175 1176 1177
{
  gtk_container_foreach (container,
                         (GtkCallback)remove_custom_widget,
                         container);
}

static void
clear_per_printer_ui (GtkPrintUnixDialog *dialog)
{
1178 1179 1180
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  gtk_container_foreach (GTK_CONTAINER (priv->finishing_table),
1181 1182
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1183 1184
  gtk_table_resize (GTK_TABLE (priv->finishing_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->image_quality_table),
1185 1186
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1187 1188
  gtk_table_resize (GTK_TABLE (priv->image_quality_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->color_table),
1189 1190
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1191 1192
  gtk_table_resize (GTK_TABLE (priv->color_table), 1, 2);
  gtk_container_foreach (GTK_CONTAINER (priv->advanced_vbox),
1193 1194
			 (GtkCallback)gtk_widget_destroy,
			 NULL);
1195
  extension_point_clear_children (GTK_CONTAINER (priv->extension_point));
1196 1197 1198 1199 1200 1201 1202
}

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

  priv->request_details_tag = 0;
1206 1207 1208 1209
  
  if (success)
    {
      GtkTreeSelection *selection;
1210
      selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
1211 1212 1213 1214 1215 1216 1217 1218 1219
      
      selected_printer_changed (selection, dialog);
    }
}

static void
selected_printer_changed (GtkTreeSelection   *selection,
			  GtkPrintUnixDialog *dialog)
{
1220
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1221 1222 1223 1224 1225
  GtkPrinter *printer;
  GtkTreeIter iter, filter_iter;

  /* Whenever the user selects a printer we stop looking for
     the printer specified in the initial settings */
1226 1227
  if (priv->waiting_for_printer &&
      !priv->internal_printer_change)
1228
    {
1229 1230
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
1231 1232
    }
  
1233
  if (priv->request_details_tag)
1234
    {
1235 1236
      g_source_remove (priv->request_details_tag);
      priv->request_details_tag = 0;
1237 1238 1239 1240 1241
    }
  
  printer = NULL;
  if (gtk_tree_selection_get_selected (selection, NULL, &filter_iter))
    {
1242
      gtk_tree_model_filter_convert_iter_to_child_iter (priv->printer_list_filter,
1243 1244 1245
							&iter,
							&filter_iter);

1246
      gtk_tree_model_get (priv->printer_list, &iter,
1247 1248 1249 1250
  			  PRINTER_LIST_COL_PRINTER_OBJ, &printer,
			  -1);
    }
  
1251
  if (printer != NULL && !gtk_printer_has_details (printer))
1252 1253
    {
      gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, FALSE);
1254
      priv->request_details_tag =
1255 1256
	g_signal_connect (printer, "details-acquired",
			  G_CALLBACK (printer_details_acquired), dialog);
1257
      gtk_printer_request_details (printer);
Alexander Larsson's avatar
Alexander Larsson committed
1258
      g_object_unref (printer);
1259 1260 1261
      return;
    }
  
1262
  if (printer == priv->current_printer)
1263 1264 1265 1266 1267 1268
    {
      if (printer)
	g_object_unref (printer);
      return;
    }

1269
  if (priv->options)
1270
    {
1271 1272
      g_object_unref (priv->options);
      priv->options = NULL;  
1273 1274 1275 1276

      clear_per_printer_ui (dialog);
    }

1277
  if (priv->current_printer)
1278
    {
1279
      g_object_unref (priv->current_printer);
1280 1281
    }

1282 1283
  priv->printer_capabilities = 0;
  
1284
  gtk_dialog_set_response_sensitive (GTK_DIALOG (dialog), GTK_RESPONSE_OK, TRUE);
1285
  priv->current_printer = printer;
1286 1287 1288

  if (printer != NULL)
    {
1289
      priv->printer_capabilities = gtk_printer_get_capabilities (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1290 1291 1292 1293
      priv->options = _gtk_printer_get_options (printer, 
						priv->initial_settings,
						priv->page_setup,
						priv->manual_capabilities);
1294
  
1295 1296
      priv->options_changed_handler = 
        g_signal_connect_swapped (priv->options, "changed", G_CALLBACK (options_changed_cb), dialog);
1297 1298 1299
    }

  update_dialog_from_settings (dialog);
1300
  update_dialog_from_capabilities (dialog);
1301
}
1302

1303 1304 1305 1306
static void
update_collate_icon (GtkToggleButton    *toggle_button,
		     GtkPrintUnixDialog *dialog)
{
1307
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1308 1309 1310 1311 1312 1313 1314 1315 1316 1317

  gtk_widget_queue_draw (priv->collate_image);
}

static void
paint_page (GtkWidget *widget,
	    cairo_t   *cr, 
	    gfloat     scale,
	    gint       x_offset, 
	    gint       y_offset,
1318 1319
	    gchar     *text,
	    gint       text_x)
1320 1321
{
  gint x, y, width, height;
1322
  gint text_y, linewidth;
1323 1324 1325 1326 1327 1328

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

1329 1330 1331
  linewidth = 2;
  text_y = 21;

1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
  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));
1345
  cairo_move_to (cr, x + (gint)(text_x * scale), y + (gint)(text_y * scale));
1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358
  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;
1359
  gboolean collate, reverse, rtl;
1360
  gint copies;
1361
  gint text_x;