gtkpagesetupunixdialog.c 57.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 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
/* GtkPageSetupUnixDialog 
 * 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 <string.h>
#include <locale.h>
#include <langinfo.h>

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

#include "gtkliststore.h"
#include "gtkstock.h"
#include "gtktreeviewcolumn.h"
#include "gtktreeselection.h"
#include "gtktreemodel.h"
#include "gtkbutton.h"
#include "gtkscrolledwindow.h"
#include "gtkvbox.h"
#include "gtkhbox.h"
#include "gtkframe.h"
#include "gtkeventbox.h"
#include "gtkcombobox.h"
#include "gtktogglebutton.h"
#include "gtkradiobutton.h"
#include "gtklabel.h"
#include "gtktable.h"
#include "gtktooltips.h"
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"
#include "gtkalignment.h"
#include "gtkspinbutton.h"
50 51
#include "gtkbbox.h"
#include "gtkhbbox.h"
52 53 54 55 56

#include "gtkpagesetupunixdialog.h"
#include "gtkprintbackend.h"
#include "gtkprinter-private.h"
#include "gtkpapersize.h"
Matthias Clasen's avatar
Matthias Clasen committed
57
#include "gtkprintutils.h"
58 59 60 61 62 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 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
#include "gtkalias.h"

#define CUSTOM_PAPER_FILENAME ".gtk-custom-papers"


struct GtkPageSetupUnixDialogPrivate
{
  GtkListStore *printer_list;
  GtkListStore *page_setup_list;
  GtkListStore *custom_paper_list;
  
  GList *print_backends;

  GtkWidget *printer_combo;
  GtkWidget *paper_size_combo;
  GtkWidget *paper_size_label;
  GtkWidget *paper_size_eventbox;
  GtkTooltips *tooltips;

  GtkWidget *portrait_radio;
  GtkWidget *landscape_radio;
  GtkWidget *reverse_landscape_radio;

  guint request_details_tag;
  
  GtkPrintSettings *print_settings;

  /* Save last setup so we can re-set it after selecting manage custom sizes */
  GtkPageSetup *last_setup;

  char *waiting_for_printer;
};

enum {
  PRINTER_LIST_COL_NAME,
  PRINTER_LIST_COL_PRINTER,
  PRINTER_LIST_N_COLS
};

enum {
  PAGE_SETUP_LIST_COL_PAGE_SETUP,
  PAGE_SETUP_LIST_COL_IS_SEPARATOR,
  PAGE_SETUP_LIST_N_COLS
};

G_DEFINE_TYPE (GtkPageSetupUnixDialog, gtk_page_setup_unix_dialog, GTK_TYPE_DIALOG);

#define GTK_PAGE_SETUP_UNIX_DIALOG_GET_PRIVATE(o)  \
   (G_TYPE_INSTANCE_GET_PRIVATE ((o), GTK_TYPE_PAGE_SETUP_UNIX_DIALOG, GtkPageSetupUnixDialogPrivate))

static void gtk_page_setup_unix_dialog_finalize  (GObject                *object);
static void populate_dialog                      (GtkPageSetupUnixDialog *dialog);
static void fill_paper_sizes_from_printer        (GtkPageSetupUnixDialog *dialog,
						  GtkPrinter             *printer);
static void run_custom_paper_dialog              (GtkPageSetupUnixDialog *dialog);
Alexander Larsson's avatar
Alexander Larsson committed
113 114 115 116 117 118 119 120 121 122 123
static void printer_added_cb                     (GtkPrintBackend        *backend,
						  GtkPrinter             *printer,
						  GtkPageSetupUnixDialog *dialog);
static void printer_removed_cb                   (GtkPrintBackend        *backend,
						  GtkPrinter             *printer,
						  GtkPageSetupUnixDialog *dialog);
static void printer_status_cb                    (GtkPrintBackend        *backend,
						  GtkPrinter             *printer,
						  GtkPageSetupUnixDialog *dialog);


124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 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 335 336 337 338 339 340 341 342 343

static const char * const common_paper_sizes[] = {
  "na_letter",
  "na_legal",
  "iso_a4",
  "iso_a5",
  "roc_16k",
  "iso_b5",
  "jis_b5",
  "na_number-10",
  "iso_dl",
  "jpn_chou3",
  "na_ledger",
  "iso_a3",
};

static GtkUnit
get_default_user_units (void)
{
  /* Translate to the default units to use for presenting
   * lengths to the user. Translate to default:inch if you
   * want inches, otherwise translate to default:mm.
   * Do *not* translate it to "predefinito:mm", if it
   * it isn't default:mm or default:inch it will not work 
   */
  char *e = _("default:mm");
  
#ifdef HAVE__NL_MEASUREMENT_MEASUREMENT
  char *imperial = NULL;
  
  imperial = nl_langinfo(_NL_MEASUREMENT_MEASUREMENT);
  if (imperial && imperial[0] == 2 )
    return GTK_UNIT_INCH;  /* imperial */
  if (imperial && imperial[0] == 1 )
    return GTK_UNIT_MM;  /* metric */
#endif
  
  if (strcmp (e, "default:inch")==0)
    return GTK_UNIT_INCH;
  else if (strcmp (e, "default:mm"))
    g_warning ("Whoever translated default:mm did so wrongly.\n");
  return GTK_UNIT_MM;
}

static char *
custom_paper_get_filename (void)
{
  char *filename;

  filename = g_build_filename (g_get_home_dir (), 
			       CUSTOM_PAPER_FILENAME, NULL);
  g_assert (filename != NULL);
  return filename;
}

static gboolean
scan_double (char **text, double *val, gboolean last)
{
  char *p, *e;

  p = *text;
  
  *val = g_ascii_strtod (p, &e);
  if (p == e)
    return FALSE;

  p = e;
  if (!last)
    {
      while (g_ascii_isspace(*p))
	p++;
      if (*p++ != ',')
	return FALSE;
    }
  *text = p;
  return TRUE;
}

static void
load_custom_papers (GtkListStore *store)
{
  char *filename;
  gchar *contents;
  
  filename = custom_paper_get_filename ();

  if (g_file_get_contents (filename, &contents, NULL, NULL))
    {
      gchar **lines = g_strsplit (contents, "\n", -1);
      double w, h, top, bottom, left, right;
      GtkPaperSize *paper_size;
      GtkPageSetup *page_setup;
      char *name, *p;
      GtkTreeIter iter;
      int i;

      for (i = 0; lines[i]; i++)
	{
	  name = lines[i];
	  p = strchr(lines[i], ':');
	  if (p == NULL)
	    continue;
	  *p++ = 0;

	  while (g_ascii_isspace(*p))
	    p++;
	  
	  if (!scan_double (&p, &w, FALSE))
	    continue;
	  if (!scan_double (&p, &h, FALSE))
	    continue;
	  if (!scan_double (&p, &top, FALSE))
	    continue;
	  if (!scan_double (&p, &bottom, FALSE))
	    continue;
	  if (!scan_double (&p, &left, FALSE))
	    continue;
	  if (!scan_double (&p, &right, TRUE))
	    continue;

	  page_setup = gtk_page_setup_new ();
	  paper_size = gtk_paper_size_new_custom (name, name, w, h, GTK_UNIT_MM);
	  gtk_page_setup_set_paper_size (page_setup, paper_size);
	  gtk_paper_size_free (paper_size);

	  gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM);
	  gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
	  gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
	  gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);

	  gtk_list_store_append (store, &iter);
	  gtk_list_store_set (store, &iter,
			      0, page_setup, 
			      -1);

	  g_object_unref (page_setup);
	}
      
      g_free (contents);
      g_strfreev (lines);
    }
  g_free (filename);
}

static void
save_custom_papers (GtkListStore *store)
{
  GtkTreeModel *model = GTK_TREE_MODEL (store);
  GtkTreeIter iter;
  GString *string;
  char *filename;

  string = g_string_new ("");
  
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
	{
	  GtkPaperSize *paper_size;
	  GtkPageSetup *page_setup;
	  char buffer[G_ASCII_DTOSTR_BUF_SIZE];
	  
	  gtk_tree_model_get (model, &iter, 0, &page_setup, -1);

	  paper_size = gtk_page_setup_get_paper_size (page_setup);
	  g_string_append_printf (string, "%s: ", gtk_paper_size_get_name (paper_size));
	  
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
			  gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);
	  g_string_append (string, ", ");
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
			  gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);
	  g_string_append (string, ", ");
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
			  gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);
	  g_string_append (string, ", ");
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE, 
			  gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);
	  g_string_append (string, ", ");
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
			  gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);
	  g_string_append (string, ", ");
	  g_ascii_dtostr (buffer, G_ASCII_DTOSTR_BUF_SIZE,
			  gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
	  g_string_append (string, buffer);

	  g_string_append (string, "\n");
	  
	} while (gtk_tree_model_iter_next (model, &iter));
    }

  filename = custom_paper_get_filename ();
  g_file_set_contents (filename, string->str, -1, NULL);
  g_free (filename);
  
  g_string_free (string, TRUE);
}

static void
gtk_page_setup_unix_dialog_class_init (GtkPageSetupUnixDialogClass *class)
{
  GObjectClass *object_class;
  GtkWidgetClass *widget_class;

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

  object_class->finalize = gtk_page_setup_unix_dialog_finalize;

  g_type_class_add_private (class, sizeof (GtkPageSetupUnixDialogPrivate));  
}

static void
gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
{
344
  GtkPageSetupUnixDialogPrivate *priv;
345 346
  GtkTreeIter iter;

347 348 349
  priv = dialog->priv = GTK_PAGE_SETUP_UNIX_DIALOG_GET_PRIVATE (dialog);

  priv->print_backends = NULL;
350

351
  priv->printer_list = gtk_list_store_new (PRINTER_LIST_N_COLS,
352 353 354
						   G_TYPE_STRING, 
						   G_TYPE_OBJECT);

355 356
  gtk_list_store_append (priv->printer_list, &iter);
  gtk_list_store_set (priv->printer_list, &iter,
357 358 359 360
                      PRINTER_LIST_COL_NAME, _("<b>Any Printer</b>\nFor portable documents"),
                      PRINTER_LIST_COL_PRINTER, NULL,
                      -1);
  
361
  priv->page_setup_list = gtk_list_store_new (PAGE_SETUP_LIST_N_COLS,
362 363 364
						      G_TYPE_OBJECT,
						      G_TYPE_BOOLEAN);

365 366
  priv->custom_paper_list = gtk_list_store_new (1, G_TYPE_OBJECT);
  load_custom_papers (priv->custom_paper_list);
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381

  populate_dialog (dialog);
  
  gtk_dialog_add_buttons (GTK_DIALOG (dialog), 
                          GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
                          GTK_STOCK_APPLY, GTK_RESPONSE_OK,
                          NULL);

  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
}

static void
gtk_page_setup_unix_dialog_finalize (GObject *object)
{
  GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (object);
382
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
383 384 385
  GtkPrintBackend *backend;
  GList *node;
  
386
  if (priv->request_details_tag)
387
    {
388 389
      g_source_remove (priv->request_details_tag);
      priv->request_details_tag = 0;
390 391
    }
  
392
  if (priv->printer_list)
393
    {
394 395
      g_object_unref (priv->printer_list);
      priv->printer_list = NULL;
396 397
    }

398
  if (priv->page_setup_list)
399
    {
400 401
      g_object_unref (priv->page_setup_list);
      priv->page_setup_list = NULL;
402 403
    }

404
  if (priv->print_settings)
405
    {
406 407
      g_object_unref (priv->print_settings);
      priv->print_settings = NULL;
408 409
    }

410 411 412
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;

Alexander Larsson's avatar
Alexander Larsson committed
413 414 415 416 417 418 419 420 421 422 423 424 425 426 427
  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;

428
  G_OBJECT_CLASS (gtk_page_setup_unix_dialog_parent_class)->finalize (object);
429 430 431 432 433 434 435
}

static void
printer_added_cb (GtkPrintBackend *backend, 
		  GtkPrinter *printer, 
		  GtkPageSetupUnixDialog *dialog)
{
436
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
437 438 439 440 441 442 443 444 445 446 447 448 449 450
  GtkTreeIter iter;
  char *str;
  const char *location;;

  if (gtk_printer_is_virtual (printer))
    return;

  location = gtk_printer_get_location (printer);
  if (location == NULL)
    location = "";
  str = g_strdup_printf ("<b>%s</b>\n%s",
			 gtk_printer_get_name (printer),
			 location);
  
451 452
  gtk_list_store_append (priv->printer_list, &iter);
  gtk_list_store_set (priv->printer_list, &iter,
453 454 455 456 457 458 459 460 461 462 463
                      PRINTER_LIST_COL_NAME, str,
                      PRINTER_LIST_COL_PRINTER, printer,
                      -1);

  g_object_set_data_full (G_OBJECT (printer), 
			  "gtk-print-tree-iter", 
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);
  
  g_free (str);

464 465
  if (priv->waiting_for_printer != NULL &&
      strcmp (priv->waiting_for_printer,
466 467
	      gtk_printer_get_name (printer)) == 0)
    {
468
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
469
				     &iter);
470
      priv->waiting_for_printer = NULL;
471 472 473 474 475 476 477 478
    }
}

static void
printer_removed_cb (GtkPrintBackend *backend, 
		    GtkPrinter *printer, 
		    GtkPageSetupUnixDialog *dialog)
{
479
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
480
  GtkTreeIter *iter;
481

482
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
483
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
484 485 486 487 488 489 490 491
}


static void
printer_status_cb (GtkPrintBackend *backend, 
                   GtkPrinter *printer, 
		   GtkPageSetupUnixDialog *dialog)
{
492
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
493 494 495 496 497 498 499 500 501 502 503 504
  GtkTreeIter *iter;
  char *str;
  const char *location;;
  
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");

  location = gtk_printer_get_location (printer);
  if (location == NULL)
    location = "";
  str = g_strdup_printf ("<b>%s</b>\n%s",
			 gtk_printer_get_name (printer),
			 location);
505
  gtk_list_store_set (priv->printer_list, iter,
506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548
                      PRINTER_LIST_COL_NAME, str,
                      -1);
}

static void
printer_list_initialize (GtkPageSetupUnixDialog *dialog,
			 GtkPrintBackend *print_backend)
{
  GList *list, *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 (GtkPageSetupUnixDialog *dialog)
{
549
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
550 551 552
  GList *node;

  if (g_module_supported ())
553
    priv->print_backends = gtk_print_backend_load_modules ();
554

555
  for (node = priv->print_backends; node != NULL; node = node->next)
556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572
    printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
}

static gboolean
paper_size_row_is_separator (GtkTreeModel *model,
			     GtkTreeIter  *iter,
			     gpointer      data)
{
  gboolean separator;

  gtk_tree_model_get (model, iter, PAGE_SETUP_LIST_COL_IS_SEPARATOR, &separator, -1);
  return separator;
}

static GtkPageSetup *
get_current_page_setup (GtkPageSetupUnixDialog *dialog)
{
573
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
574 575 576 577 578 579
  GtkPageSetup *current_page_setup;
  GtkComboBox *combo_box;
  GtkTreeIter iter;

  current_page_setup = NULL;
  
580
  combo_box = GTK_COMBO_BOX (priv->paper_size_combo);
581
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
582
    gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
			PAGE_SETUP_LIST_COL_PAGE_SETUP, &current_page_setup, -1);

  if (current_page_setup)
    return current_page_setup;

  /* No selected page size, return the default one.
   * This is used to set the first page setup when the dialog is created
   * as there is no selection on the first printer_changed.
   */ 
  return gtk_page_setup_new ();
}

static gboolean
page_setup_is_equal (GtkPageSetup *a, GtkPageSetup *b)
{
  return
    gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
			     gtk_page_setup_get_paper_size (b)) &&
    gtk_page_setup_get_top_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_top_margin (b, GTK_UNIT_MM) &&
    gtk_page_setup_get_bottom_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_bottom_margin (b, GTK_UNIT_MM) &&
    gtk_page_setup_get_left_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_left_margin (b, GTK_UNIT_MM) &&
    gtk_page_setup_get_right_margin (a, GTK_UNIT_MM) == gtk_page_setup_get_right_margin (b, GTK_UNIT_MM);
}

static gboolean
page_setup_is_same_size (GtkPageSetup *a, GtkPageSetup *b)
{
  return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
				  gtk_page_setup_get_paper_size (b));
}

static gboolean
set_paper_size (GtkPageSetupUnixDialog *dialog,
		GtkPageSetup *page_setup,
		gboolean size_only,
		gboolean add_item)
{
620
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
621 622 623 624
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPageSetup *list_page_setup;

625
  model = GTK_TREE_MODEL (priv->page_setup_list);
626 627 628 629 630

  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
	{
631
	  gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
632 633 634 635 636 637 638
			      PAGE_SETUP_LIST_COL_PAGE_SETUP, &list_page_setup, -1);
	  if (list_page_setup == NULL)
	    continue;
	  
	  if ((size_only && page_setup_is_same_size (page_setup, list_page_setup)) ||
	      (!size_only && page_setup_is_equal (page_setup, list_page_setup)))
	    {
639
	      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
640 641 642 643 644 645 646 647 648 649 650 651
					     &iter);
	      g_object_unref (list_page_setup);
	      return TRUE;
	    }
	      
	  g_object_unref (list_page_setup);
	  
	} while (gtk_tree_model_iter_next (model, &iter));
    }

  if (add_item)
    {
652 653
      gtk_list_store_append (priv->page_setup_list, &iter);
      gtk_list_store_set (priv->page_setup_list, &iter,
654 655
			  PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
			  -1);
656 657
      gtk_list_store_append (priv->page_setup_list, &iter);
      gtk_list_store_set (priv->page_setup_list, &iter,
658 659
			  PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
			  -1);
660
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
661 662 663 664 665 666 667 668 669 670
				     &iter);
      return TRUE;
    }

  return FALSE;
}

static void
fill_custom_paper_sizes (GtkPageSetupUnixDialog *dialog)
{
671
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
672 673 674
  GtkTreeIter iter, paper_iter;
  GtkTreeModel *model;

675
  model = GTK_TREE_MODEL (priv->custom_paper_list);
676 677
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
678 679
      gtk_list_store_append (priv->page_setup_list, &paper_iter);
      gtk_list_store_set (priv->page_setup_list, &paper_iter,
680 681 682 683 684 685 686
			  PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
			  -1);
      do
	{
	  GtkPageSetup *page_setup;
	  gtk_tree_model_get (model, &iter, 0, &page_setup, -1);

687 688
	  gtk_list_store_append (priv->page_setup_list, &paper_iter);
	  gtk_list_store_set (priv->page_setup_list, &paper_iter,
689 690 691 692 693 694 695
			      PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
			      -1);

	  g_object_unref (page_setup);
	} while (gtk_tree_model_iter_next (model, &iter));
    }
  
696 697
  gtk_list_store_append (priv->page_setup_list, &paper_iter);
  gtk_list_store_set (priv->page_setup_list, &paper_iter,
698 699
                      PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
                      -1);
700 701
  gtk_list_store_append (priv->page_setup_list, &paper_iter);
  gtk_list_store_set (priv->page_setup_list, &paper_iter,
702 703 704 705 706 707 708 709
                      PAGE_SETUP_LIST_COL_PAGE_SETUP, NULL,
                      -1);
}

static void
fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
			       GtkPrinter *printer)
{
710
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
711 712 713 714 715 716 717 718
  GList *list, *l;
  GtkPageSetup *current_page_setup, *page_setup;
  GtkPaperSize *paper_size;
  GtkTreeIter iter;
  int i;

  current_page_setup = get_current_page_setup (dialog);
  
719
  gtk_list_store_clear (priv->page_setup_list);
720 721 722 723 724 725 726 727 728 729

  if (printer == NULL)
    {
      for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
	{
	  page_setup = gtk_page_setup_new ();
	  paper_size = gtk_paper_size_new (common_paper_sizes[i]);
	  gtk_page_setup_set_paper_size_and_default_margins (page_setup, paper_size);
	  gtk_paper_size_free (paper_size);
	  
730 731
	  gtk_list_store_append (priv->page_setup_list, &iter);
	  gtk_list_store_set (priv->page_setup_list, &iter,
732 733 734 735 736 737 738 739 740 741 742 743 744
			      PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
			      -1);
	  g_object_unref (page_setup);
	}
    }
  else
    {
      list = _gtk_printer_list_papers (printer);
      /* TODO: We should really sort this list so interesting size
	 are at the top */
      for (l = list; l != NULL; l = l->next)
	{
	  page_setup = l->data;
745 746
	  gtk_list_store_append (priv->page_setup_list, &iter);
	  gtk_list_store_set (priv->page_setup_list, &iter,
747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767
			      PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
			      -1);
	  g_object_unref (page_setup);
	}
      g_list_free (list);
    }

  fill_custom_paper_sizes (dialog);
  
  if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
    set_paper_size (dialog, current_page_setup, TRUE, TRUE);
  
  if (current_page_setup)
    g_object_unref (current_page_setup);
}

static void
printer_changed_finished_callback (GtkPrinter *printer,
				   gboolean success,
				   GtkPageSetupUnixDialog *dialog)
{
768 769 770
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

  priv->request_details_tag = 0;
771 772 773 774 775 776 777 778 779 780
  
  if (success)
    fill_paper_sizes_from_printer (dialog, printer);

}

static void
printer_changed_callback (GtkComboBox *combo_box,
			  GtkPageSetupUnixDialog *dialog)
{
781
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
782 783 784 785 786
  GtkPrinter *printer;
  GtkTreeIter iter;

  /* If we're waiting for a specific printer but the user changed
     to another printer, cancel that wait. */
787
  if (priv->waiting_for_printer)
788
    {
789 790
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
791 792
    }
  
793
  if (priv->request_details_tag)
794
    {
795 796
      g_source_remove (priv->request_details_tag);
      priv->request_details_tag = 0;
797 798 799 800 801 802 803 804 805 806 807
    }
  
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
      gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
			  PRINTER_LIST_COL_PRINTER, &printer, -1);

      if (printer == NULL || _gtk_printer_has_details (printer))
	fill_paper_sizes_from_printer (dialog, printer);
      else
	{
808
	  priv->request_details_tag =
809 810 811 812 813 814 815 816 817
	    g_signal_connect (printer, "details-acquired",
			      G_CALLBACK (printer_changed_finished_callback), dialog);
	  _gtk_printer_request_details (printer);

	}

      if (printer)
	g_object_unref (printer);

818
      if (priv->print_settings)
819 820 821 822 823 824
	{
	  const char *name = NULL;

	  if (printer)
	    name = gtk_printer_get_name (printer);
	  
825
	  gtk_print_settings_set (priv->print_settings,
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866
				  "format-for-printer", name);
	}
    }
}

/* We do this munging because we don't want to show zero digits
   after the decimal point, and not to many such digits if they
   are nonzero. I wish printf let you specify max precision for %f... */
static char *
double_to_string (double d, GtkUnit unit)
{
  char *val, *p;
  struct lconv *locale_data;
  const char *decimal_point;
  int decimal_point_len;

  locale_data = localeconv ();
  decimal_point = locale_data->decimal_point;
  decimal_point_len = strlen (decimal_point);
  
  /* Max two decimal digits for inch, max one for mm */
  if (unit == GTK_UNIT_INCH)
    val = g_strdup_printf ("%.2f", d);
  else
    val = g_strdup_printf ("%.1f", d);

  if (strstr (val, decimal_point))
    {
      p = val + strlen (val) - 1;
      while (*p == '0')
        p--;
      if (p - val + 1 >= decimal_point_len &&
	  strncmp (p - (decimal_point_len - 1), decimal_point, decimal_point_len) == 0)
        p -= decimal_point_len;
      p[1] = '\0';
    }

  return val;
}

static void
867 868
paper_size_changed (GtkComboBox *combo_box,
		    GtkPageSetupUnixDialog *dialog)
869
{
870
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
871 872 873 874 875 876 877 878
  GtkTreeIter iter;
  GtkPageSetup *page_setup, *last_page_setup;
  GtkUnit unit;
  char *str, *w, *h;
  char *top, *bottom, *left, *right;
  GtkLabel *label;
  const char *unit_str;

879
  label = GTK_LABEL (priv->paper_size_label);
880 881 882 883 884 885 886 887 888 889 890 891
   
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
      gtk_tree_model_get (gtk_combo_box_get_model (combo_box),
			  &iter, PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);

      if (page_setup == NULL)
	{
	  run_custom_paper_dialog (dialog);

	  /* Save current last_setup as it is changed by updating the list */
	  last_page_setup = NULL;
892 893
	  if (priv->last_setup)
	    last_page_setup = g_object_ref (priv->last_setup);
894 895

	  /* Update printer page list */
896
	  printer_changed_callback (GTK_COMBO_BOX (priv->printer_combo), dialog);
897 898 899 900 901 902 903 904 905 906

	  /* Change from "manage" menu item to last value */
	  if (last_page_setup == NULL)
	    last_page_setup = gtk_page_setup_new (); /* "good" default */
	  set_paper_size (dialog, last_page_setup, FALSE, TRUE);
	  g_object_unref (last_page_setup);
	  
	  return;
	}

907 908
      if (priv->last_setup)
	g_object_unref (priv->last_setup);
909

910
      priv->last_setup = g_object_ref (page_setup);
911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950
      
      unit = get_default_user_units ();

      if (unit == GTK_UNIT_MM)
	unit_str = _("mm");
      else
	unit_str = _("inch");
	

      w = double_to_string (gtk_page_setup_get_paper_width (page_setup, unit),
			    unit);
      h = double_to_string (gtk_page_setup_get_paper_height (page_setup, unit),
			    unit);
      str = g_strdup_printf ("%s x %s %s", w, h, unit_str);
      g_free (w);
      g_free (h);
      
      gtk_label_set_text (label, str);
      g_free (str);

      top = double_to_string (gtk_page_setup_get_top_margin (page_setup, unit), unit);
      bottom = double_to_string (gtk_page_setup_get_bottom_margin (page_setup, unit), unit);
      left = double_to_string (gtk_page_setup_get_left_margin (page_setup, unit), unit);
      right = double_to_string (gtk_page_setup_get_right_margin (page_setup, unit), unit);

      str = g_strdup_printf (_("Margins:\n"
			       " Left: %s %s\n"
			       " Right: %s %s\n"
			       " Top: %s %s\n"
			       " Bottom: %s %s"
			       ),
			     left, unit_str,
			     right, unit_str,
			     top, unit_str,
			     bottom, unit_str);
      g_free (top);
      g_free (bottom);
      g_free (left);
      g_free (right);
      
951 952
      gtk_tooltips_set_tip (GTK_TOOLTIPS (priv->tooltips),
			    priv->paper_size_eventbox, str, NULL);
953 954 955 956 957 958 959
      g_free (str);
      
      g_object_unref (page_setup);
    }
  else
    {
      gtk_label_set_text (label, "");
960 961 962 963 964
      gtk_tooltips_set_tip (GTK_TOOLTIPS (priv->tooltips),
			    priv->paper_size_eventbox, NULL, NULL);
      if (priv->last_setup)
	g_object_unref (priv->last_setup);
      priv->last_setup = NULL;
965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991
    }
}

static void
page_name_func (GtkCellLayout   *cell_layout,
		GtkCellRenderer *cell,
		GtkTreeModel    *tree_model,
		GtkTreeIter     *iter,
		gpointer         data)
{
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;
  
  gtk_tree_model_get (tree_model, iter,
		      PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
  if (page_setup)
    {
      paper_size = gtk_page_setup_get_paper_size (page_setup);
      g_object_set (cell, "text",  gtk_paper_size_get_display_name (paper_size), NULL);
      g_object_unref (page_setup);
    }
  else
    g_object_set (cell, "text",  _("Manage Custom Sizes..."), NULL);
      
}

static void
992
populate_dialog (GtkPageSetupUnixDialog *ps_dialog)
993
{
994 995
  GtkPageSetupUnixDialogPrivate *priv = ps_dialog->priv;
  GtkDialog *dialog = GTK_DIALOG (ps_dialog);
996 997 998
  GtkWidget *table, *label, *combo, *radio_button, *ebox, *image;
  GtkCellRenderer *cell;

999 1000 1001 1002 1003 1004 1005 1006
  gtk_window_set_resizable (GTK_WINDOW (dialog), FALSE);

  gtk_dialog_set_has_separator (dialog, FALSE);
  gtk_container_set_border_width (GTK_CONTAINER (dialog), 5);
  gtk_box_set_spacing (GTK_BOX (dialog->vbox), 2); /* 2 * 5 + 2 = 12 */
  gtk_container_set_border_width (GTK_CONTAINER (dialog->action_area), 5);
  gtk_box_set_spacing (GTK_BOX (dialog->action_area), 6);

1007
  table = gtk_table_new (4, 4, FALSE);
1008
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1009
  gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1010 1011
  gtk_container_set_border_width (GTK_CONTAINER (table), 5);
  gtk_box_pack_start (GTK_BOX (dialog->vbox), table, TRUE, TRUE, 0);
1012 1013 1014 1015 1016 1017 1018 1019
  gtk_widget_show (table);

  label = gtk_label_new_with_mnemonic (_("_Format for:"));
  gtk_table_attach (GTK_TABLE (table), label,
		    0, 1, 0, 1,
		    GTK_FILL, 0, 0, 0);
  gtk_widget_show (label);

1020 1021
  combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->printer_list));
  priv->printer_combo = combo;
1022

1023 1024 1025 1026 1027 1028 1029 1030 1031 1032
  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
  gtk_cell_layout_set_attributes (GTK_CELL_LAYOUT (combo), cell,
                                  "markup", PRINTER_LIST_COL_NAME,
                                  NULL);

  gtk_table_attach (GTK_TABLE (table), combo,
		    1, 4, 0, 1,
		    GTK_FILL | GTK_EXPAND, 0, 0, 0);
  gtk_widget_show (combo);
1033
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
1034 1035 1036 1037 1038 1039 1040

  label = gtk_label_new_with_mnemonic (_("_Paper size:"));
  gtk_table_attach (GTK_TABLE (table), label,
		    0, 1, 1, 2,
		    GTK_FILL, 0, 0, 0);
  gtk_widget_show (label);

1041 1042
  combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->page_setup_list));
  priv->paper_size_combo = combo;
1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
  gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (combo), 
					paper_size_row_is_separator, NULL, NULL);
  
  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), cell,
				      page_name_func, NULL, NULL);

  gtk_table_attach (GTK_TABLE (table), combo,
		    1, 4, 1, 2,
		    GTK_FILL | GTK_EXPAND, 0, 0, 0);
  gtk_widget_show (combo);
1055
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), combo);
1056 1057

  ebox = gtk_event_box_new ();
1058
  priv->paper_size_eventbox = ebox;
1059 1060 1061 1062 1063 1064
  gtk_event_box_set_visible_window (GTK_EVENT_BOX (ebox), FALSE);
  gtk_table_attach (GTK_TABLE (table), ebox,
		    1, 4, 2, 3,
		    GTK_FILL, 0, 0, 0);
  gtk_widget_show (ebox);
  
1065
  label = gtk_label_new ("");
1066
  priv->paper_size_label = label;
1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_container_add (GTK_CONTAINER (ebox), label);
  gtk_widget_show (label);

  label = gtk_label_new_with_mnemonic (_("_Orientation:"));
  gtk_table_attach (GTK_TABLE (table), label,
		    0, 1, 3, 4,
		    GTK_FILL, 0, 0, 0);
  gtk_widget_show (label);

  radio_button = gtk_radio_button_new (NULL);
  image = gtk_image_new_from_stock (GTK_STOCK_ORIENTATION_PORTRAIT,
				    GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_widget_show (image);
  gtk_container_add (GTK_CONTAINER (radio_button), image);
1082
  priv->portrait_radio = radio_button;
1083 1084 1085 1086
  gtk_table_attach (GTK_TABLE (table), radio_button,
		    1, 2, 3, 4,
		    0, 0, 0, 0);
  gtk_widget_show (radio_button);
1087
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), radio_button);
1088 1089 1090 1091 1092 1093

  radio_button = gtk_radio_button_new (gtk_radio_button_get_group (GTK_RADIO_BUTTON(radio_button)));
  image = gtk_image_new_from_stock (GTK_STOCK_ORIENTATION_LANDSCAPE,
				    GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_widget_show (image);
  gtk_container_add (GTK_CONTAINER (radio_button), image);
1094
  priv->landscape_radio = radio_button;
1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106
  gtk_table_attach (GTK_TABLE (table), radio_button,
		    2, 3, 3, 4,
		    0, 0, 0, 0);
  gtk_widget_show (radio_button);

  gtk_table_set_row_spacing (GTK_TABLE (table), 3, 0);
  
  radio_button = gtk_radio_button_new (gtk_radio_button_get_group (GTK_RADIO_BUTTON(radio_button)));
  image = gtk_image_new_from_stock (GTK_STOCK_ORIENTATION_REVERSE_LANDSCAPE,
				    GTK_ICON_SIZE_LARGE_TOOLBAR);
  gtk_widget_show (image);
  gtk_container_add (GTK_CONTAINER (radio_button), image);
1107
  priv->reverse_landscape_radio = radio_button;
1108 1109 1110 1111 1112
  gtk_table_attach (GTK_TABLE (table), radio_button,
		    3, 4, 3, 4,
		    0, 0, 0, 0);
  gtk_widget_show (radio_button);

1113
  priv->tooltips = gtk_tooltips_new ();
1114

1115 1116
  g_signal_connect (priv->paper_size_combo, "changed", G_CALLBACK (paper_size_changed), ps_dialog);
  g_signal_connect (priv->printer_combo, "changed", G_CALLBACK (printer_changed_callback), ps_dialog);
1117
  gtk_combo_box_set_active (GTK_COMBO_BOX (priv->printer_combo), 0);
1118

1119
  load_print_backends (ps_dialog);
1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
}

GtkWidget *
gtk_page_setup_unix_dialog_new (const gchar *title,
				GtkWindow *parent)
{
  GtkWidget *result;

  if (title == NULL)
    title = _("Page Setup");
  
  result = g_object_new (GTK_TYPE_PAGE_SETUP_UNIX_DIALOG,
                         "title", title,
                         NULL);

  if (parent)
    gtk_window_set_transient_for (GTK_WINDOW (result), parent);

  return result;
}

static GtkPageOrientation
get_orientation (GtkPageSetupUnixDialog *dialog)
{
1144 1145 1146
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->portrait_radio)))
1147
    return GTK_PAGE_ORIENTATION_PORTRAIT;
1148
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->landscape_radio)))
1149
    return GTK_PAGE_ORIENTATION_LANDSCAPE;
1150
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio)))
1151 1152 1153 1154 1155 1156 1157
    return GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE;
  return GTK_PAGE_ORIENTATION_PORTRAIT;
}

static void
set_orientation (GtkPageSetupUnixDialog *dialog, GtkPageOrientation orientation)
{
1158 1159
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

1160 1161 1162 1163
  switch (orientation)
    {
    case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
    case GTK_PAGE_ORIENTATION_PORTRAIT:
1164
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->portrait_radio), TRUE);
1165 1166
      break;
    case GTK_PAGE_ORIENTATION_LANDSCAPE:
1167
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->landscape_radio), TRUE);
1168 1169
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
1170
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio), TRUE);
1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201
      break;
    }
}

void
gtk_page_setup_unix_dialog_set_page_setup (GtkPageSetupUnixDialog *dialog,
					   GtkPageSetup           *page_setup)
{
  if (page_setup)
    {
      set_paper_size (dialog, page_setup, FALSE, TRUE);
      set_orientation (dialog, gtk_page_setup_get_orientation (page_setup));
    }
}

GtkPageSetup *
gtk_page_setup_unix_dialog_get_page_setup (GtkPageSetupUnixDialog *dialog)
{
  GtkPageSetup *page_setup;
  
  page_setup = get_current_page_setup (dialog);

  gtk_page_setup_set_orientation (page_setup, get_orientation (dialog));

  return page_setup;
}

static gboolean
set_active_printer (GtkPageSetupUnixDialog *dialog,
		    const char *printer_name)
{
1202
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1203 1204 1205 1206
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPrinter *printer;

1207
  model = GTK_TREE_MODEL (priv->printer_list);
1208 1209 1210 1211 1212

  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
	{
1213
	  gtk_tree_model_get (GTK_TREE_MODEL (priv->printer_list), &iter,
1214 1215 1216 1217 1218 1219
			      PRINTER_LIST_COL_PRINTER, &printer, -1);
	  if (printer == NULL)
	    continue;
	  
	  if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
	    {
1220
	      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237
					     &iter);
	      g_object_unref (printer);
	      return TRUE;
	    }
	      
	  g_object_unref (printer);
	  
	} while (gtk_tree_model_iter_next (model, &iter));
    }
  
  return FALSE;
}

void
gtk_page_setup_unix_dialog_set_print_settings (GtkPageSetupUnixDialog *dialog,
					       GtkPrintSettings       *print_settings)
{
1238
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
1239 1240
  const char *format_for_printer;

1241 1242
  if (priv->print_settings == print_settings) return;

1243 1244
  if (priv->print_settings)
    g_object_unref (priv->print_settings);
1245

1246
  priv->print_settings = print_settings;
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257

  if (print_settings)
    {
      g_object_ref (print_settings);

      format_for_printer = gtk_print_settings_get (print_settings, "format-for-printer");

      /* Set printer if in list, otherwise set when that printer
	 is added */
      if (format_for_printer &&
	  !set_active_printer (dialog, format_for_printer))
1258
	priv->waiting_for_printer = g_strdup (format_for_printer); 
1259 1260 1261 1262 1263 1264
    }
}

GtkPrintSettings *
gtk_page_setup_unix_dialog_get_print_settings (GtkPageSetupUnixDialog *dialog)
{
1265 1266 1267
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

  return priv->print_settings;
1268 1269 1270
}

static GtkWidget *
1271 1272
wrap_in_frame (const gchar *label, 
               GtkWidget   *child)
1273 1274 1275 1276 1277
{
  GtkWidget *frame, *alignment, *label_widget;
  char *bold_text;

  label_widget = gtk_label_new ("");
1278
  gtk_misc_set_alignment (GTK_MISC (label_widget), 0.0, 0.5);
1279 1280 1281 1282 1283 1284
  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);
  
1285 1286
  frame = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
1287
  
1288
  alignment = gtk_alignment_new (0.0, 0.0, 1.0, 1.0);
1289
  gtk_alignment_set_padding (GTK_ALIGNMENT (alignment),
1290 1291
			     0, 0, 12, 0);
  gtk_box_pack_start (GTK_BOX (frame), alignment, FALSE, FALSE, 0);
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310

  gtk_container_add (GTK_CONTAINER (alignment), child);

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

typedef struct {
  GtkUnit display_unit;
  GtkWidget *spin_button;
} UnitWidget;

typedef struct {
  GtkPageSetupUnixDialog *dialog;
  GtkWidget *treeview;
  GtkTreeViewColumn *text_column;
  gboolean non_user_change;
1311
  GtkWidget *values_box;
1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324
  GtkWidget *printer_combo;
  GtkWidget *width_widget;
  GtkWidget *height_widget;
  GtkWidget *top_widget;
  GtkWidget *bottom_widget;
  GtkWidget *left_widget;
  GtkWidget *right_widget;
  guint request_details_tag;
} CustomPaperDialog;

static void unit_widget_changed (CustomPaperDialog *data);

static GtkWidget *
1325
new_unit_widget (CustomPaperDialog *dialog, GtkUnit unit, GtkWidget *mnemonic_label)
1326 1327 1328 1329 1330 1331 1332
{
  GtkWidget *hbox, *button, *label;
  UnitWidget *data;

  data = g_new0 (UnitWidget, 1);
  data->display_unit = unit;
  
1333
  hbox = gtk_hbox_new (FALSE, 6);
1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349

  button = gtk_spin_button_new_with_range (0.0, 9999.0, 1);
  if (unit == GTK_UNIT_INCH)
    gtk_spin_button_set_digits (GTK_SPIN_BUTTON (button), 2);
  else
    gtk_spin_button_set_digits (GTK_SPIN_BUTTON (button), 1);

  gtk_box_pack_start (GTK_BOX (hbox), button, TRUE, TRUE, 0);
  gtk_widget_show (button);

  data->spin_button = button;

  g_signal_connect_swapped (button, "value_changed",
			    G_CALLBACK (unit_widget_changed), dialog);
  
  if (unit == GTK_UNIT_INCH)
1350
    label = gtk_label_new (_("inch"));
1351
  else
1352
    label = gtk_label_new (_("mm"));
1353 1354 1355

  gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
  gtk_widget_show (label);
1356
  gtk_label_set_mnemonic_widget (GTK_LABEL (mnemonic_label), button);
1357 1358 1359 1360 1361 1362 1363 1364 1365 1366

  g_object_set_data_full (G_OBJECT (hbox), "unit-data", data, g_free);
  
  return hbox;
}

static double
unit_widget_get (GtkWidget *unit_widget)
{
  UnitWidget *data = g_object_get_data (G_OBJECT (unit_widget), "unit-data");
Matthias Clasen's avatar
Matthias Clasen committed
1367
  return _gtk_print_convert_to_mm (gtk_spin_button_get_value (GTK_SPIN_BUTTON (data->spin_button)),
1368 1369 1370 1371 1372 1373 1374 1375
		data->display_unit);
}

static void
unit_widget_set (GtkWidget *unit_widget, double val)
{
  UnitWidget *data = g_object_get_data (G_OBJECT (unit_widget), "unit-data");
  gtk_spin_button_set_value (GTK_SPIN_BUTTON (data->spin_button),
Matthias Clasen's avatar
Matthias Clasen committed
1376
			     _gtk_print_convert_from_mm (val, data->display_unit));
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446

}

static void
custom_paper_printer_data_func (GtkCellLayout   *cell_layout,
				GtkCellRenderer *cell,
				GtkTreeModel    *tree_model,
				GtkTreeIter     *iter,
				gpointer         data)
{
  GtkPrinter *printer;
  gtk_tree_model_get (tree_model, iter,
		      PRINTER_LIST_COL_PRINTER, &printer, -1);

  if (printer)
    g_object_set (cell, "text",  gtk_printer_get_name (printer), NULL);
  else
    g_object_set (cell, "text",  _("Margins from Printer..."), NULL);
  
  if (printer)
    g_object_unref (printer);
}

static void
update_combo_sensitivity_from_printers (CustomPaperDialog *data)
{
  GtkTreeIter iter;
  gboolean sensitive;
  GtkTreeSelection *selection;
  GtkTreeModel *model;

  sensitive = FALSE;
  model = GTK_TREE_MODEL (data->dialog->priv->printer_list);
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->treeview));
  if (gtk_tree_model_get_iter_first (model, &iter) &&
      gtk_tree_model_iter_next (model, &iter) &&
      gtk_tree_selection_get_selected (selection, NULL, &iter))
    sensitive = TRUE;

  gtk_widget_set_sensitive (data->printer_combo, sensitive);
}

static void
update_custom_widgets_from_list (CustomPaperDialog *data)
{
  GtkTreeSelection *selection;
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPageSetup *page_setup;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW (data->treeview));
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->treeview));

  data->non_user_change = TRUE;
  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
      
      unit_widget_set (data->width_widget,
		       gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM));
      unit_widget_set (data->height_widget, 
		       gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM));
      unit_widget_set (data->top_widget,
		       gtk_page_setup_get_top_margin (page_setup, GTK_UNIT_MM));
      unit_widget_set (data->bottom_widget, 
		       gtk_page_setup_get_bottom_margin (page_setup, GTK_UNIT_MM));
      unit_widget_set (data->left_widget,
		       gtk_page_setup_get_left_margin (page_setup, GTK_UNIT_MM));
      unit_widget_set (data->right_widget,
		       gtk_page_setup_get_right_margin (page_setup, GTK_UNIT_MM));
1447 1448

      gtk_widget_set_sensitive (data->values_box, TRUE);
1449 1450 1451
    }
  else
    {
1452
      gtk_widget_set_sensitive (data->values_box, FALSE);
1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610
    }

  update_combo_sensitivity_from_printers (data);
  data->non_user_change = FALSE;
}

static void
selected_custom_paper_changed (GtkTreeSelection *selection,
			       CustomPaperDialog *data)
{
  update_custom_widgets_from_list (data);
}

static void
unit_widget_changed (CustomPaperDialog *data)
{
  double w, h, top, bottom, left, right;
  GtkTreeSelection *selection;
  GtkTreeIter iter;
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;

  if (data->non_user_change)
    return;
  
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->treeview));

  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      gtk_tree_model_get (GTK_TREE_MODEL (data->dialog->priv->custom_paper_list), &iter, 0, &page_setup, -1);

      w = unit_widget_get (data->width_widget);
      h = unit_widget_get (data->height_widget);

      paper_size = gtk_page_setup_get_paper_size (page_setup);
      gtk_paper_size_set_size (paper_size, w, h, GTK_UNIT_MM);
      
      top = unit_widget_get (data->top_widget);
      bottom = unit_widget_get (data->bottom_widget);
      left = unit_widget_get (data->left_widget);
      right = unit_widget_get (data->right_widget);

      gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM);
      gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
      gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
      gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);
      
      g_object_unref (page_setup);
    }
}

static gboolean
custom_paper_name_used (CustomPaperDialog *data, const char *name)
{
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;
  
  model = gtk_tree_view_get_model (GTK_TREE_VIEW (data->treeview));
	  
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
	{
	  gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
	  paper_size = gtk_page_setup_get_paper_size (page_setup);
	  if (strcmp (name,
		      gtk_paper_size_get_name (paper_size)) == 0)
	    {
	      g_object_unref (page_setup);
	      return TRUE;
	    }
	  g_object_unref (page_setup);
	} while (gtk_tree_model_iter_next (model, &iter));
    }
  
  return FALSE;
}

static void
add_custom_paper (CustomPaperDialog *data)
{
  GtkListStore *store;
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;
  GtkTreeSelection *selection;
  GtkTreePath *path;
  GtkTreeIter iter;
  char *name;
  int i;
  
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->treeview));
  store = data->dialog->priv->custom_paper_list;

  i = 1;
  name = NULL;
  do
    {
      g_free (name);
      name = g_strdup_printf (_("Custom Size %d"), i);
      i++;
    } while (custom_paper_name_used (data, name));

  page_setup = gtk_page_setup_new ();
  paper_size = gtk_paper_size_new_custom (name, name,
					  gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM),
					  gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM),
					  GTK_UNIT_MM);
  gtk_page_setup_set_paper_size (page_setup, paper_size);
  gtk_paper_size_free (paper_size);
  
  gtk_list_store_append (store, &iter);
  gtk_list_store_set (store, &iter, 0, page_setup, -1);
  g_object_unref (page_setup);

  gtk_tree_selection_select_iter (selection, &iter);
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
  gtk_widget_grab_focus (data->treeview);
  gtk_tree_view_set_cursor (GTK_TREE_VIEW (data->treeview), path,
			    data->text_column, TRUE);
  gtk_tree_path_free (path);
}

static void
remove_custom_paper (CustomPaperDialog *data)
{
  GtkTreeSelection *selection;
  GtkTreeIter iter;
  GtkListStore *store;
  
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (data->treeview));
  store = data->dialog->priv->custom_paper_list;

  if (gtk_tree_selection_get_selected (selection, NULL, &iter))
    {
      GtkTreePath *path = gtk_tree_model_get_path (GTK_TREE_MODEL (store), &iter);
      gtk_list_store_remove (store, &iter);
      
      if (gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path))
	gtk_tree_selection_select_iter (selection, &iter);
      else if (gtk_tree_path_prev (path) && gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path))
	gtk_tree_selection_select_iter (selection, &iter);
      
      gtk_tree_path_free (path);
    }
}

static void
set_margins_from_printer (CustomPaperDialog *data,
			  GtkPrinter *printer)
{
  double top, bottom, left, right;

  top = bottom = left = right = 0;
  _gtk_printer_get_hard_margins (printer, &top, &bottom, &left, &right);
  
  data->non_user_change = TRUE;
Matthias Clasen's avatar
Matthias Clasen committed
1611 1612 1613 1614
  unit_widget_set (data->top_widget, _gtk_print_convert_to_mm (top, GTK_UNIT_POINTS));
  unit_widget_set (data->bottom_widget, _gtk_print_convert_to_mm (bottom, GTK_UNIT_POINTS));
  unit_widget_set (data->left_widget, _gtk_print_convert_to_mm (left, GTK_UNIT_POINTS));
  unit_widget_set (data->right_widget, _gtk_print_convert_to_mm (right, GTK_UNIT_POINTS));
1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 1688 1689 1690 1691 1692 1693 1694 1695 1696 1697 1698 1699 1700 1701 1702 1703 1704 1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728 1729 1730 1731 1732 1733 1734 1735
  data->non_user_change = FALSE;

  /* Only send one change */
  unit_widget_changed (data);
}

static void
get_margins_finished_callback (GtkPrinter *printer,
			       gboolean success,
			       CustomPaperDialog *data)
{
  data->request_details_tag = 0;
  
  if (success)
    set_margins_from_printer (data, printer);

  gtk_combo_box_set_active (GTK_COMBO_BOX (data->printer_combo), 0);
}

static void
margins_from_printer_changed (CustomPaperDialog *data)
{
  GtkTreeIter iter;
  GtkComboBox *combo;
  GtkPrinter *printer;

  combo = GTK_COMBO_BOX (data->printer_combo);

  if (data->request_details_tag)
    {
      g_source_remove (data->request_details_tag);
      data->request_details_tag = 0;
    }
  
  if (gtk_combo_box_get_active_iter  (combo, &iter))
    {
      gtk_tree_model_get (gtk_combo_box_get_model (combo), &iter,
			  PRINTER_LIST_COL_PRINTER, &printer, -1);

      if (printer)
	{
	  if (_gtk_printer_has_details (printer))
	    {
	      set_margins_from_printer (data, printer);
	      gtk_combo_box_set_active (combo, 0);
	    }
	  else
	    {
	      data->request_details_tag =
		g_signal_connect (printer, "details-acquired",
				  G_CALLBACK (get_margins_finished_callback), data);
	      _gtk_printer_request_details (printer);
	    }

	  g_object_unref (printer);
	}
    }
}


static void
custom_paper_dialog_free (gpointer p)
{
  CustomPaperDialog *data = p;
  if (data->request_details_tag)
    {
      g_source_remove (data->request_details_tag);
      data->request_details_tag = 0;
    }
  
  g_free (data);
}

static void
custom_size_name_edited (GtkCellRenderer   *cell,
			 gchar             *path_string,
			 gchar             *new_text,
			 CustomPaperDialog *data)
{
  GtkTreePath *path;
  GtkTreeIter iter;
  GtkListStore *store;
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;

  store = data->dialog->priv->custom_paper_list;
  path = gtk_tree_path_new_from_string (path_string);
  gtk_tree_model_get_iter (GTK_TREE_MODEL (store), &iter, path);
  gtk_tree_model_get (GTK_TREE_MODEL (store), &iter, 0, &page_setup, -1);
  gtk_tree_path_free (path);

  paper_size = gtk_paper_size_new_custom (new_text, new_text,
					  gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_MM),
					  gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_MM),
					  GTK_UNIT_MM);
  gtk_page_setup_set_paper_size (page_setup, paper_size);
  gtk_paper_size_free (paper_size);
  
  g_object_unref (page_setup);
}

static void
custom_name_func (GtkTreeViewColumn *tree_column,
		  GtkCellRenderer   *cell,
		  GtkTreeModel      *tree_model,
		  GtkTreeIter       *iter,
		  gpointer           data)
{
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;

  gtk_tree_model_get (tree_model, iter, 0, &page_setup, -1);
  if (page_setup)
    {
      paper_size = gtk_page_setup_get_paper_size (page_setup);
      g_object_set (cell, "text",  gtk_paper_size_get_display_name (paper_size), NULL);
      g_object_unref (page_setup);
    }
}

static void
1736
run_custom_paper_dialog (GtkPageSetupUnixDialog *ps_dialog)
1737
{
1738
  GtkPageSetupUnixDialogPrivate *priv = ps_dialog->priv;
1739 1740
  GtkWidget *custom_dialog, *image, *table, *label, *widget, *frame, *combo;
  GtkWidget *hbox, *vbox, *treeview, *scrolled, *button_box, *button;
1741
  GtkDialog *ddialog;
1742 1743 1744 1745 1746 1747 1748 1749 1750
  GtkCellRenderer *cell;
  GtkTreeViewColumn *column;
  GtkTreeIter iter;
  GtkTreeSelection *selection;
  CustomPaperDialog *data;
  GtkUnit user_units;
  gulong printer_tag1, printer_tag2;
  
  custom_dialog = gtk_dialog_new_with_buttons (_("Manage Custom Sizes"),
1751
					       GTK_WINDOW (ps_dialog),
1752 1753 1754 1755
					       GTK_DIALOG_MODAL | GTK_DIALOG_DESTROY_WITH_PARENT | GTK_DIALOG_NO_SEPARATOR,
					       GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
					       NULL);

1756 1757
  ddialog = GTK_DIALOG (custom_dialog);

1758
  data = g_new0 (CustomPaperDialog, 1);
1759
  data->dialog = ps_dialog;
1760 1761
  g_object_set_data_full (G_OBJECT (custom_dialog), "custom-dialog", data, custom_paper_dialog_free);

1762 1763 1764 1765 1766 1767 1768 1769 1770
  gtk_dialog_set_has_separator (ddialog, FALSE);
  gtk_container_set_border_width (GTK_CONTAINER (ddialog), 5);
  gtk_box_set_spacing (GTK_BOX (ddialog->vbox), 2); /* 2 * 5 + 2 = 12 */
  gtk_container_set_border_width (GTK_CONTAINER (ddialog->action_area), 5);
  gtk_box_set_spacing (GTK_BOX (ddialog->action_area), 6);

  hbox = gtk_hbox_new (FALSE, 18);
  gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
  gtk_box_pack_start (GTK_BOX (ddialog->vbox), hbox, TRUE, TRUE, 0);
1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785
  gtk_widget_show (hbox);

  vbox = gtk_vbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);
  
  scrolled = gtk_scrolled_window_new (NULL, NULL);
  gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled),
				  GTK_POLICY_AUTOMATIC,
				  GTK_POLICY_AUTOMATIC);
  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled),
				       GTK_SHADOW_IN);
  gtk_box_pack_start (GTK_BOX (vbox), scrolled, TRUE, TRUE, 0);
  gtk_widget_show (scrolled);

1786
  treeview = gtk_tree_view_new_with_model (GTK_TREE_MODEL (priv->custom_paper_list));
1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832
  data->treeview = treeview;
  gtk_tree_view_set_headers_visible (GTK_TREE_VIEW (treeview), FALSE);
  gtk_widget_set_size_request (treeview, 140, -1);
  
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (treeview));
  gtk_tree_selection_set_mode (selection, GTK_SELECTION_BROWSE);
  g_signal_connect (selection, "changed", G_CALLBACK (selected_custom_paper_changed), data);

  cell = gtk_cell_renderer_text_new ();
  g_object_set (cell, "editable", TRUE, NULL);
  g_signal_connect (cell, "edited", 
		    G_CALLBACK (custom_size_name_edited), data);
  data->text_column = column =
    gtk_tree_view_column_new_with_attributes ("paper", cell,
					      NULL);
  gtk_tree_view_column_set_cell_data_func  (column, cell, custom_name_func, NULL, NULL);
  
  gtk_tree_view_append_column (GTK_TREE_VIEW (treeview), column);

  gtk_container_add (GTK_CONTAINER (scrolled), treeview);
  gtk_widget_show (treeview);

  button_box = gtk_hbox_new (FALSE, 6);
  gtk_box_pack_start (GTK_BOX (vbox), button_box, FALSE, FALSE, 0);
  gtk_widget_show (button_box);

  button = gtk_button_new ();
  image = gtk_image_new_from_stock (GTK_STOCK_ADD, GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image);
  gtk_container_add (GTK_CONTAINER (button), image);
  gtk_box_pack_start (GTK_BOX (button_box), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  g_signal_connect_swapped (button, "clicked", G_CALLBACK (add_custom_paper), data);
  
  button = gtk_button_new ();
  image = gtk_image_new_from_stock (GTK_STOCK_REMOVE, GTK_ICON_SIZE_BUTTON);
  gtk_widget_show (image);
  gtk_container_add (GTK_CONTAINER (button), image);
  gtk_box_pack_start (GTK_BOX (button_box), button, FALSE, FALSE, 0);
  gtk_widget_show (button);

  g_signal_connect_swapped (button, "clicked", G_CALLBACK (remove_custom_paper), data);

  user_units = get_default_user_units ();
  
1833 1834
  vbox = gtk_vbox_new (FALSE, 18);
  data->values_box = vbox;
1835 1836 1837 1838 1839 1840 1841
  gtk_box_pack_start (GTK_BOX (hbox), vbox, TRUE, TRUE, 0);
  gtk_widget_show (vbox);
  
  table = gtk_table_new (2, 2, FALSE);
  
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
  gtk_table_set_col_spacings (GTK_TABLE (table), 12);
1842 1843 1844

  label = gtk_label_new_with_mnemonic (_("_Width:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1845 1846
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (table), label,
1847
		    0, 1, 0, 1, GTK_FILL, 0, 0, 0);
1848
  
1849
  widget = new_unit_widget (data, user_units, label);
1850 1851
  data->width_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1852
		    1, 2, 0, 1, GTK_FILL, 0, 0, 0);
1853
  gtk_widget_show (widget);
1854 1855 1856

  label = gtk_label_new_with_mnemonic (_("_Height:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1857 1858
  gtk_widget_show (label);
  gtk_table_attach (GTK_TABLE (table), label,
1859
		    0, 1, 1, 2, GTK_FILL, 0, 0, 0);
1860
  
1861
  widget = new_unit_widget (data, user_units, label);
1862 1863
  data->height_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1864
		    1, 2, 1, 2, GTK_FILL, 0, 0, 0);
1865 1866 1867 1868 1869 1870 1871 1872
  gtk_widget_show (widget);

  frame = wrap_in_frame (_("Paper Size"), table);
  gtk_widget_show (table);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);
  

1873
  table = gtk_table_new (5, 2, FALSE);
1874
  gtk_table_set_row_spacings (GTK_TABLE (table), 6);
1875 1876 1877 1878 1879 1880 1881
  gtk_table_set_col_spacings (GTK_TABLE (table), 12);

  label = gtk_label_new_with_mnemonic (_("_Top:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label,
		    0, 1, 0, 1, GTK_FILL, 0, 0, 0);
  gtk_widget_show (label);
1882

1883
  widget = new_unit_widget (data, user_units, label);
1884 1885
  data->top_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1886
		    1, 2, 0, 1, GTK_FILL, 0, 0, 0);
1887
  gtk_widget_show (widget);
1888 1889 1890
  
  label = gtk_label_new_with_mnemonic (_("_Bottom:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1891
  gtk_table_attach (GTK_TABLE (table), label,
1892
		    0, 1 , 1, 2, GTK_FILL, 0, 0, 0);
1893
  gtk_widget_show (label);
1894 1895

  widget = new_unit_widget (data, user_units, label);
1896 1897
  data->bottom_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1898
		    1, 2, 1, 2, GTK_FILL, 0, 0, 0);
1899 1900
  gtk_widget_show (widget);

1901 1902
  label = gtk_label_new_with_mnemonic (_("_Left:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1903
  gtk_table_attach (GTK_TABLE (table), label,
1904
		    0, 1, 2, 3, GTK_FILL, 0, 0, 0);
1905
  gtk_widget_show (label);
1906 1907
  
  widget = new_unit_widget (data, user_units, label);
1908 1909
  data->left_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1910
		    1, 2, 2, 3, GTK_FILL, 0, 0, 0);
1911 1912
  gtk_widget_show (widget);

1913 1914
  label = gtk_label_new_with_mnemonic (_("_Right:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
1915
  gtk_table_attach (GTK_TABLE (table), label,
1916
		    0, 1, 3, 4, GTK_FILL, 0, 0, 0);
1917
  gtk_widget_show (label);
1918 1919

  widget = new_unit_widget (data, user_units, label);
1920 1921
  data->right_widget = widget;
  gtk_table_attach (GTK_TABLE (table), widget,
1922
		    1, 2, 3, 4, GTK_FILL, 0, 0, 0);
1923 1924 1925 1926
  gtk_widget_show (widget);

  hbox = gtk_hbox_new (FALSE, 0);
  gtk_table_attach (GTK_TABLE (table), hbox,
1927
		    0, 2, 4, 5, GTK_FILL | GTK_EXPAND, 0, 0, 0);
1928 1929
  gtk_widget_show (hbox);
  
1930
  combo = gtk_combo_box_new_with_model (GTK_TREE_MODEL (priv->printer_list));
1931 1932 1933
  data->printer_combo = combo;

  printer_tag1 =
1934
    g_signal_connect_swapped (priv->printer_list, "row_inserted",
1935 1936
			      G_CALLBACK (update_combo_sensitivity_from_printers), data);
  printer_tag2 =
1937
    g_signal_connect_swapped (priv->printer_list, "row_deleted",
1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961
			      G_CALLBACK (update_combo_sensitivity_from_printers), data);
  update_combo_sensitivity_from_printers (data);
  
  cell = gtk_cell_renderer_text_new ();
  gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (combo), cell, TRUE);
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (combo), cell,
				      custom_paper_printer_data_func,
				      NULL, NULL);

  gtk_combo_box_set_active (GTK_COMBO_BOX (combo), 0);
  gtk_box_pack_start (GTK_BOX (hbox), combo, FALSE, FALSE, 0);
  gtk_widget_show (combo);

  g_signal_connect_swapped (combo, "changed",
			    G_CALLBACK (margins_from_printer_changed), data);
  
  frame = wrap_in_frame (_("Paper Margins"), table);
  gtk_widget_show (table);
  gtk_box_pack_start (GTK_BOX (vbox), frame, FALSE, FALSE, 0);
  gtk_widget_show (frame);

  update_custom_widgets_from_list (data);

  /* If no custom sizes, add one */
1962
  if (!gtk_tree_model_get_iter_first (GTK_TREE_MODEL (priv->custom_paper_list),
1963 1964 1965 1966 1967 1968
				      &iter))
    {
      /* Need to realize treeview so we can start the rename */
      gtk_widget_realize (treeview);
      add_custom_paper (data);
    }
1969

1970 1971 1972
  gtk_dialog_run (GTK_DIALOG (custom_dialog));
  gtk_widget_destroy (custom_dialog);

Christian Persch's avatar