gtkpagesetupunixdialog.c 34.3 KB
Newer Older
1
/* GtkPageSetupUnixDialog
2
 * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
3
 * Copyright © 2006, 2007, 2008 Christian Persch
4 5 6 7 8 9 10 11
 *
 * 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
12
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 14 15
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19 20 21 22
 */


#include "config.h"
#include <string.h>
#include <locale.h>
23

24 25 26 27 28 29 30 31 32 33 34 35 36
#include "gtkintl.h"
#include "gtkprivate.h"

#include "gtkliststore.h"
#include "gtktreeviewcolumn.h"
#include "gtktreeselection.h"
#include "gtktreemodel.h"
#include "gtkbutton.h"
#include "gtkscrolledwindow.h"
#include "gtkcombobox.h"
#include "gtktogglebutton.h"
#include "gtkradiobutton.h"
#include "gtklabel.h"
37
#include "gtkgrid.h"
38 39 40 41
#include "gtkcelllayout.h"
#include "gtkcellrenderertext.h"

#include "gtkpagesetupunixdialog.h"
42
#include "gtkcustompaperunixdialog.h"
43 44
#include "gtkprintbackend.h"
#include "gtkpapersize.h"
Matthias Clasen's avatar
Matthias Clasen committed
45
#include "gtkprintutils.h"
46
#include "gtkdialogprivate.h"
47

48 49 50 51 52 53
/**
 * SECTION:gtkpagesetupunixdialog
 * @Short_description: A page setup dialog
 * @Title: GtkPageSetupUnixDialog
 *
 * #GtkPageSetupUnixDialog implements a page setup dialog for platforms
54
 * which don’t provide a native page setup dialog, like Unix. It can
55
 * be used very much like any other GTK+ dialog, at the cost of
56 57
 * the portability offered by the
 * [high-level printing API][gtk3-High-level-Printing-API]
58 59 60 61 62
 *
 * Printing support was added in GTK+ 2.10.
 */


63
struct _GtkPageSetupUnixDialogPrivate
64 65 66 67
{
  GtkListStore *printer_list;
  GtkListStore *page_setup_list;
  GtkListStore *custom_paper_list;
68

69 70 71 72 73
  GList *print_backends;

  GtkWidget *printer_combo;
  GtkWidget *paper_size_combo;
  GtkWidget *paper_size_label;
74
  GtkCellRenderer *paper_size_cell;
75 76

  GtkWidget *portrait_radio;
77
  GtkWidget *reverse_portrait_radio;
78 79 80
  GtkWidget *landscape_radio;
  GtkWidget *reverse_landscape_radio;

81
  gulong request_details_tag;
82
  GtkPrinter *request_details_printer;
83

84 85 86 87 88
  GtkPrintSettings *print_settings;

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

Matthias Clasen's avatar
Matthias Clasen committed
89
  gchar *waiting_for_printer;
90 91
};

92
/* Keep these in line with GtkListStores defined in gtkpagesetupunixprintdialog.ui */
93 94 95 96 97 98 99 100 101 102 103 104
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
};

105
G_DEFINE_TYPE_WITH_PRIVATE (GtkPageSetupUnixDialog, gtk_page_setup_unix_dialog, GTK_TYPE_DIALOG)
106 107 108

static void gtk_page_setup_unix_dialog_finalize  (GObject                *object);
static void fill_paper_sizes_from_printer        (GtkPageSetupUnixDialog *dialog,
109
                                                  GtkPrinter             *printer);
Alexander Larsson's avatar
Alexander Larsson committed
110
static void printer_added_cb                     (GtkPrintBackend        *backend,
111 112
                                                  GtkPrinter             *printer,
                                                  GtkPageSetupUnixDialog *dialog);
Alexander Larsson's avatar
Alexander Larsson committed
113
static void printer_removed_cb                   (GtkPrintBackend        *backend,
114 115
                                                  GtkPrinter             *printer,
                                                  GtkPageSetupUnixDialog *dialog);
Alexander Larsson's avatar
Alexander Larsson committed
116
static void printer_status_cb                    (GtkPrintBackend        *backend,
117 118
                                                  GtkPrinter             *printer,
                                                  GtkPageSetupUnixDialog *dialog);
119 120 121 122 123 124 125 126 127 128 129 130 131
static void printer_changed_callback             (GtkComboBox            *combo_box,
						  GtkPageSetupUnixDialog *dialog);
static void paper_size_changed                   (GtkComboBox            *combo_box,
						  GtkPageSetupUnixDialog *dialog);
static void page_name_func                       (GtkCellLayout          *cell_layout,
						  GtkCellRenderer        *cell,
						  GtkTreeModel           *tree_model,
						  GtkTreeIter            *iter,
						  gpointer                data);
static void load_print_backends                  (GtkPageSetupUnixDialog *dialog);
static gboolean paper_size_row_is_separator      (GtkTreeModel           *model,
						  GtkTreeIter            *iter,
						  gpointer                data);
Alexander Larsson's avatar
Alexander Larsson committed
132

133

134
static const gchar common_paper_sizes[][16] = {
135 136 137 138 139 140 141 142 143 144 145
  "na_letter",
  "na_legal",
  "iso_a4",
  "iso_a5",
  "roc_16k",
  "iso_b5",
  "jis_b5",
  "na_number-10",
  "iso_dl",
  "jpn_chou3",
  "na_ledger",
146
  "iso_a3"
147 148 149 150 151 152 153
};


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

156 157
  object_class = G_OBJECT_CLASS (class);
  widget_class = GTK_WIDGET_CLASS (class);
158 159 160

  object_class->finalize = gtk_page_setup_unix_dialog_finalize;

161 162 163
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
164
					       "/org/gtk/libgtk/ui/gtkpagesetupunixdialog.ui");
165

166 167 168 169 170 171 172 173 174 175 176
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, printer_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, page_setup_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, custom_paper_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, printer_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, paper_size_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, paper_size_label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, paper_size_cell);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, portrait_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, reverse_portrait_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, landscape_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPageSetupUnixDialog, reverse_landscape_radio);
177 178 179

  gtk_widget_class_bind_template_callback (widget_class, printer_changed_callback);
  gtk_widget_class_bind_template_callback (widget_class, paper_size_changed);
180 181 182 183 184
}

static void
gtk_page_setup_unix_dialog_init (GtkPageSetupUnixDialog *dialog)
{
185
  GtkPageSetupUnixDialogPrivate *priv;
186
  GtkTreeIter iter;
187
  gchar *tmp;
188

189
  priv = dialog->priv = gtk_page_setup_unix_dialog_get_instance_private (dialog);
190 191

  priv->print_backends = NULL;
192

193
  gtk_widget_init_template (GTK_WIDGET (dialog));
194 195 196 197 198 199 200 201 202 203 204 205 206
  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                          _("_Cancel"), GTK_RESPONSE_CANCEL,
                          _("_Apply"), GTK_RESPONSE_OK,
                          NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);

G_GNUC_BEGIN_IGNORE_DEPRECATIONS
  gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                           GTK_RESPONSE_OK,
                                           GTK_RESPONSE_CANCEL,
                                           -1);
G_GNUC_END_IGNORE_DEPRECATIONS
207

208
  /* Do this in code, we want the translatable strings without the markup */
209
  gtk_list_store_append (priv->printer_list, &iter);
210
  tmp = g_strdup_printf ("<b>%s</b>\n%s", _("Any Printer"), _("For portable documents"));
211
  gtk_list_store_set (priv->printer_list, &iter,
212
                      PRINTER_LIST_COL_NAME, tmp,
213 214
                      PRINTER_LIST_COL_PRINTER, NULL,
                      -1);
215 216
  g_free (tmp);

217 218
  /* After adding the above row, set it active */
  gtk_combo_box_set_active (GTK_COMBO_BOX (priv->printer_combo), 0);
219

220 221 222 223 224 225
  /* Setup cell data func and separator func in code */
  gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (priv->paper_size_combo),
					paper_size_row_is_separator, NULL, NULL);
  gtk_cell_layout_set_cell_data_func (GTK_CELL_LAYOUT (priv->paper_size_combo),
				      priv->paper_size_cell,
                                      page_name_func, NULL, NULL);
226

227 228 229
  /* Load data */
  _gtk_print_load_custom_papers (priv->custom_paper_list);
  load_print_backends (dialog);
230 231 232 233 234 235
}

static void
gtk_page_setup_unix_dialog_finalize (GObject *object)
{
  GtkPageSetupUnixDialog *dialog = GTK_PAGE_SETUP_UNIX_DIALOG (object);
236
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
237 238
  GtkPrintBackend *backend;
  GList *node;
239

240
  if (priv->request_details_tag)
241
    {
242
      g_signal_handler_disconnect (priv->request_details_printer,
243
                                   priv->request_details_tag);
244 245
      g_object_unref (priv->request_details_printer);
      priv->request_details_printer = NULL;
246
      priv->request_details_tag = 0;
247
    }
248

249
  if (priv->printer_list)
250
    {
251 252
      g_object_unref (priv->printer_list);
      priv->printer_list = NULL;
253 254
    }

255
  if (priv->page_setup_list)
256
    {
257 258
      g_object_unref (priv->page_setup_list);
      priv->page_setup_list = NULL;
259 260
    }

261 262 263 264 265 266
  if (priv->custom_paper_list)
    {
      g_object_unref (priv->custom_paper_list);
      priv->custom_paper_list = NULL;
    }

267
  if (priv->print_settings)
268
    {
269 270
      g_object_unref (priv->print_settings);
      priv->print_settings = NULL;
271 272
    }

273 274 275
  g_free (priv->waiting_for_printer);
  priv->waiting_for_printer = NULL;

Alexander Larsson's avatar
Alexander Larsson committed
276 277 278 279 280 281 282 283 284 285 286
  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);
    }
287

Alexander Larsson's avatar
Alexander Larsson committed
288 289 290
  g_list_free (priv->print_backends);
  priv->print_backends = NULL;

291
  G_OBJECT_CLASS (gtk_page_setup_unix_dialog_parent_class)->finalize (object);
292 293 294
}

static void
295 296 297
printer_added_cb (GtkPrintBackend        *backend,
                  GtkPrinter             *printer,
                  GtkPageSetupUnixDialog *dialog)
298
{
299
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
300
  GtkTreeIter iter;
Matthias Clasen's avatar
Matthias Clasen committed
301
  gchar *str;
302
  const gchar *location;
303 304 305 306 307 308 309 310

  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",
311 312
                         gtk_printer_get_name (printer),
                         location);
313

314 315
  gtk_list_store_append (priv->printer_list, &iter);
  gtk_list_store_set (priv->printer_list, &iter,
316 317 318 319
                      PRINTER_LIST_COL_NAME, str,
                      PRINTER_LIST_COL_PRINTER, printer,
                      -1);

320
  g_object_set_data_full (G_OBJECT (printer),
321
                          "gtk-print-tree-iter",
322 323 324 325
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);
  g_free (str);

326 327
  if (priv->waiting_for_printer != NULL &&
      strcmp (priv->waiting_for_printer,
328
              gtk_printer_get_name (printer)) == 0)
329
    {
330
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
331
                                     &iter);
332
      priv->waiting_for_printer = NULL;
333 334 335 336
    }
}

static void
337 338 339
printer_removed_cb (GtkPrintBackend        *backend,
                    GtkPrinter             *printer,
                    GtkPageSetupUnixDialog *dialog)
340
{
341
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
342
  GtkTreeIter *iter;
343

344
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
345
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
346 347 348 349
}


static void
350 351 352
printer_status_cb (GtkPrintBackend        *backend,
                   GtkPrinter             *printer,
                   GtkPageSetupUnixDialog *dialog)
353
{
354
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
355
  GtkTreeIter *iter;
Matthias Clasen's avatar
Matthias Clasen committed
356
  gchar *str;
357
  const gchar *location;
358

359 360 361 362 363 364
  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",
365 366
                         gtk_printer_get_name (printer),
                         location);
367
  gtk_list_store_set (priv->printer_list, iter,
368 369
                      PRINTER_LIST_COL_NAME, str,
                      -1);
370
  g_free (str);
371 372 373 374
}

static void
printer_list_initialize (GtkPageSetupUnixDialog *dialog,
375
                         GtkPrintBackend        *print_backend)
376 377
{
  GList *list, *node;
378

379 380
  g_return_if_fail (print_backend != NULL);

381 382 383 384
  g_signal_connect_object (print_backend,
                           "printer-added",
                           (GCallback) printer_added_cb,
                           G_OBJECT (dialog), 0);
385

386 387 388 389
  g_signal_connect_object (print_backend,
                           "printer-removed",
                           (GCallback) printer_removed_cb,
                           G_OBJECT (dialog), 0);
390

391 392 393 394
  g_signal_connect_object (print_backend,
                           "printer-status-changed",
                           (GCallback) printer_status_cb,
                           G_OBJECT (dialog), 0);
395 396 397 398 399 400 401 402 403 404 405

  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);
406

407 408 409 410 411
}

static void
load_print_backends (GtkPageSetupUnixDialog *dialog)
{
412
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
413 414 415
  GList *node;

  if (g_module_supported ())
416
    priv->print_backends = gtk_print_backend_load_modules ();
417

418
  for (node = priv->print_backends; node != NULL; node = node->next)
419 420 421 422 423
    printer_list_initialize (dialog, GTK_PRINT_BACKEND (node->data));
}

static gboolean
paper_size_row_is_separator (GtkTreeModel *model,
424 425
                             GtkTreeIter  *iter,
                             gpointer      data)
426 427 428 429 430 431 432 433 434 435
{
  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)
{
436
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
437 438 439 440 441
  GtkPageSetup *current_page_setup;
  GtkComboBox *combo_box;
  GtkTreeIter iter;

  current_page_setup = NULL;
442

443
  combo_box = GTK_COMBO_BOX (priv->paper_size_combo);
444
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
445
    gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
446
                        PAGE_SETUP_LIST_COL_PAGE_SETUP, &current_page_setup, -1);
447 448 449 450 451 452 453

  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.
454
   */
455 456 457 458
  return gtk_page_setup_new ();
}

static gboolean
459 460
page_setup_is_equal (GtkPageSetup *a,
                     GtkPageSetup *b)
461 462 463
{
  return
    gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
464
                             gtk_page_setup_get_paper_size (b)) &&
465 466 467 468 469 470 471
    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
Matthias Clasen's avatar
Matthias Clasen committed
472
page_setup_is_same_size (GtkPageSetup *a,
473
                         GtkPageSetup *b)
474 475
{
  return gtk_paper_size_is_equal (gtk_page_setup_get_paper_size (a),
476
                                  gtk_page_setup_get_paper_size (b));
477 478 479 480
}

static gboolean
set_paper_size (GtkPageSetupUnixDialog *dialog,
481 482 483
                GtkPageSetup           *page_setup,
                gboolean                size_only,
                gboolean                add_item)
484
{
485
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
486 487 488 489
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPageSetup *list_page_setup;

490
  model = GTK_TREE_MODEL (priv->page_setup_list);
491 492 493 494

  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512
        {
          gtk_tree_model_get (GTK_TREE_MODEL (priv->page_setup_list), &iter,
                              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)))
            {
              gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
                                             &iter);
              g_object_unref (list_page_setup);
              return TRUE;
            }

          g_object_unref (list_page_setup);

        } while (gtk_tree_model_iter_next (model, &iter));
513 514 515 516
    }

  if (add_item)
    {
517 518
      gtk_list_store_append (priv->page_setup_list, &iter);
      gtk_list_store_set (priv->page_setup_list, &iter,
519 520
                          PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
                          -1);
521 522
      gtk_list_store_append (priv->page_setup_list, &iter);
      gtk_list_store_set (priv->page_setup_list, &iter,
523 524
                          PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
                          -1);
525
      gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->paper_size_combo),
526
                                     &iter);
527 528 529 530 531 532 533 534 535
      return TRUE;
    }

  return FALSE;
}

static void
fill_custom_paper_sizes (GtkPageSetupUnixDialog *dialog)
{
536
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
537 538 539
  GtkTreeIter iter, paper_iter;
  GtkTreeModel *model;

540
  model = GTK_TREE_MODEL (priv->custom_paper_list);
541 542
  if (gtk_tree_model_get_iter_first (model, &iter))
    {
543 544
      gtk_list_store_append (priv->page_setup_list, &paper_iter);
      gtk_list_store_set (priv->page_setup_list, &paper_iter,
545 546
                          PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
                          -1);
547
      do
548 549 550
        {
          GtkPageSetup *page_setup;
          gtk_tree_model_get (model, &iter, 0, &page_setup, -1);
551

552 553 554 555
          gtk_list_store_append (priv->page_setup_list, &paper_iter);
          gtk_list_store_set (priv->page_setup_list, &paper_iter,
                              PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
                              -1);
556

557 558
          g_object_unref (page_setup);
        } while (gtk_tree_model_iter_next (model, &iter));
559
    }
560

561 562
  gtk_list_store_append (priv->page_setup_list, &paper_iter);
  gtk_list_store_set (priv->page_setup_list, &paper_iter,
563 564
                      PAGE_SETUP_LIST_COL_IS_SEPARATOR, TRUE,
                      -1);
565 566
  gtk_list_store_append (priv->page_setup_list, &paper_iter);
  gtk_list_store_set (priv->page_setup_list, &paper_iter,
567 568 569 570 571 572
                      PAGE_SETUP_LIST_COL_PAGE_SETUP, NULL,
                      -1);
}

static void
fill_paper_sizes_from_printer (GtkPageSetupUnixDialog *dialog,
573
                               GtkPrinter             *printer)
574
{
575
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
576 577 578 579
  GList *list, *l;
  GtkPageSetup *current_page_setup, *page_setup;
  GtkPaperSize *paper_size;
  GtkTreeIter iter;
Matthias Clasen's avatar
Matthias Clasen committed
580
  gint i;
581

582
  gtk_list_store_clear (priv->page_setup_list);
583 584 585 586

  if (printer == NULL)
    {
      for (i = 0; i < G_N_ELEMENTS (common_paper_sizes); i++)
587 588 589 590 591 592 593 594 595 596 597 598
        {
          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);

          gtk_list_store_append (priv->page_setup_list, &iter);
          gtk_list_store_set (priv->page_setup_list, &iter,
                              PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
                              -1);
          g_object_unref (page_setup);
        }
599 600 601
    }
  else
    {
602
      list = gtk_printer_list_papers (printer);
603
      /* TODO: We should really sort this list so interesting size
604
         are at the top */
605
      for (l = list; l != NULL; l = l->next)
606 607 608 609 610 611 612 613
        {
          page_setup = l->data;
          gtk_list_store_append (priv->page_setup_list, &iter);
          gtk_list_store_set (priv->page_setup_list, &iter,
                              PAGE_SETUP_LIST_COL_PAGE_SETUP, page_setup,
                              -1);
          g_object_unref (page_setup);
        }
614 615 616 617
      g_list_free (list);
    }

  fill_custom_paper_sizes (dialog);
618

619 620 621 622 623 624 625 626 627
  current_page_setup = NULL;

  /* When selecting a different printer, select its default paper size */
  if (printer != NULL)
    current_page_setup = gtk_printer_get_default_page_size (printer);

  if (current_page_setup == NULL)
    current_page_setup = get_current_page_setup (dialog);

628 629
  if (!set_paper_size (dialog, current_page_setup, FALSE, FALSE))
    set_paper_size (dialog, current_page_setup, TRUE, TRUE);
630

631 632 633 634 635
  if (current_page_setup)
    g_object_unref (current_page_setup);
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
636
printer_changed_finished_callback (GtkPrinter             *printer,
637 638
                                   gboolean                success,
                                   GtkPageSetupUnixDialog *dialog)
639
{
640 641
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

642
  g_signal_handler_disconnect (priv->request_details_printer,
643
                               priv->request_details_tag);
644
  g_object_unref (priv->request_details_printer);
645
  priv->request_details_tag = 0;
646
  priv->request_details_printer = NULL;
647

648 649 650 651 652 653
  if (success)
    fill_paper_sizes_from_printer (dialog, printer);

}

static void
Matthias Clasen's avatar
Matthias Clasen committed
654
printer_changed_callback (GtkComboBox            *combo_box,
655
                          GtkPageSetupUnixDialog *dialog)
656
{
657
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
658 659 660 661
  GtkPrinter *printer;
  GtkTreeIter iter;

  /* If we're waiting for a specific printer but the user changed
662
   * to another printer, cancel that wait.
Matthias Clasen's avatar
Matthias Clasen committed
663
   */
664
  g_clear_pointer (&priv->waiting_for_printer, g_free);
665

666
  if (priv->request_details_tag)
667
    {
668
      g_signal_handler_disconnect (priv->request_details_printer,
669
                                   priv->request_details_tag);
670 671
      g_object_unref (priv->request_details_printer);
      priv->request_details_printer = NULL;
672
      priv->request_details_tag = 0;
673
    }
674

675 676 677
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
      gtk_tree_model_get (gtk_combo_box_get_model (combo_box), &iter,
678
                          PRINTER_LIST_COL_PRINTER, &printer, -1);
679

680
      if (printer == NULL || gtk_printer_has_details (printer))
681
        fill_paper_sizes_from_printer (dialog, printer);
682
      else
683 684 685 686 687 688
        {
          priv->request_details_printer = g_object_ref (printer);
          priv->request_details_tag =
            g_signal_connect (printer, "details-acquired",
                              G_CALLBACK (printer_changed_finished_callback), dialog);
          gtk_printer_request_details (printer);
689

690
        }
691 692

      if (printer)
693
        g_object_unref (printer);
694

695
      if (priv->print_settings)
696 697 698 699 700 701 702 703 704
        {
          const char *name = NULL;

          if (printer)
            name = gtk_printer_get_name (printer);

          gtk_print_settings_set (priv->print_settings,
                                  "format-for-printer", name);
        }
705 706 707 708 709 710
    }
}

/* 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... */
Matthias Clasen's avatar
Matthias Clasen committed
711
static gchar *
712 713
double_to_string (gdouble d,
                  GtkUnit unit)
714
{
Matthias Clasen's avatar
Matthias Clasen committed
715
  gchar *val, *p;
716
  struct lconv *locale_data;
Matthias Clasen's avatar
Matthias Clasen committed
717 718
  const gchar *decimal_point;
  gint decimal_point_len;
719 720 721 722

  locale_data = localeconv ();
  decimal_point = locale_data->decimal_point;
  decimal_point_len = strlen (decimal_point);
723

724 725 726 727 728 729 730 731 732 733 734 735
  /* 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 &&
736
          strncmp (p - (decimal_point_len - 1), decimal_point, decimal_point_len) == 0)
737 738 739 740 741 742 743
        p -= decimal_point_len;
      p[1] = '\0';
    }

  return val;
}

744 745 746

static void
custom_paper_dialog_response_cb (GtkDialog *custom_paper_dialog,
747 748
                                 gint       response_id,
                                 gpointer   user_data)
749 750 751 752 753 754 755 756 757 758 759 760
{
  GtkPageSetupUnixDialog *page_setup_dialog = GTK_PAGE_SETUP_UNIX_DIALOG (user_data);
  GtkPageSetupUnixDialogPrivate *priv = page_setup_dialog->priv;

  _gtk_print_load_custom_papers (priv->custom_paper_list);

  /* Update printer page list */
  printer_changed_callback (GTK_COMBO_BOX (priv->printer_combo), page_setup_dialog);

  gtk_widget_destroy (GTK_WIDGET (custom_paper_dialog));
}

761
static void
Matthias Clasen's avatar
Matthias Clasen committed
762
paper_size_changed (GtkComboBox            *combo_box,
763
                    GtkPageSetupUnixDialog *dialog)
764
{
765
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
766 767 768
  GtkTreeIter iter;
  GtkPageSetup *page_setup, *last_page_setup;
  GtkUnit unit;
Matthias Clasen's avatar
Matthias Clasen committed
769 770
  gchar *str, *w, *h;
  gchar *top, *bottom, *left, *right;
771
  GtkLabel *label;
Matthias Clasen's avatar
Matthias Clasen committed
772
  const gchar *unit_str;
773

774
  label = GTK_LABEL (priv->paper_size_label);
775

776 777 778
  if (gtk_combo_box_get_active_iter (combo_box, &iter))
    {
      gtk_tree_model_get (gtk_combo_box_get_model (combo_box),
779
                          &iter, PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
780 781

      if (page_setup == NULL)
782
        {
783 784
          GtkWidget *custom_paper_dialog;

785 786 787 788
          /* Change from "manage" menu item to last value */
          if (priv->last_setup)
            last_page_setup = g_object_ref (priv->last_setup);
          else
789 790
            last_page_setup = gtk_page_setup_new (); /* "good" default */
          set_paper_size (dialog, last_page_setup, FALSE, TRUE);
791 792 793
          g_object_unref (last_page_setup);

          /* And show the custom paper dialog */
794 795 796
          custom_paper_dialog = _gtk_custom_paper_unix_dialog_new (GTK_WINDOW (dialog), NULL);
          g_signal_connect (custom_paper_dialog, "response", G_CALLBACK (custom_paper_dialog_response_cb), dialog);
          gtk_window_present (GTK_WINDOW (custom_paper_dialog));
797 798

          return;
799
        }
800

801
      if (priv->last_setup)
802
        g_object_unref (priv->last_setup);
803

804
      priv->last_setup = g_object_ref (page_setup);
805

806
      unit = _gtk_print_get_default_user_units ();
807 808

      if (unit == GTK_UNIT_MM)
809
        unit_str = _("mm");
810
      else
811
        unit_str = _("inch");
812 813

      w = double_to_string (gtk_page_setup_get_paper_width (page_setup, unit),
814
                            unit);
815
      h = double_to_string (gtk_page_setup_get_paper_height (page_setup, unit),
816
                            unit);
817
      str = g_strdup_printf ("%s × %s %s", w, h, unit_str);
818 819
      g_free (w);
      g_free (h);
820

821 822 823 824 825 826 827 828 829
      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"
830 831 832 833 834 835 836 837 838
                               " 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);
839 840 841 842
      g_free (top);
      g_free (bottom);
      g_free (left);
      g_free (right);
843

844
      gtk_widget_set_tooltip_text (priv->paper_size_label, str);
845
      g_free (str);
846

847 848 849 850 851
      g_object_unref (page_setup);
    }
  else
    {
      gtk_label_set_text (label, "");
852
      gtk_widget_set_tooltip_text (priv->paper_size_label, NULL);
853
      if (priv->last_setup)
854
        g_object_unref (priv->last_setup);
855
      priv->last_setup = NULL;
856 857 858 859 860
    }
}

static void
page_name_func (GtkCellLayout   *cell_layout,
861 862 863 864
                GtkCellRenderer *cell,
                GtkTreeModel    *tree_model,
                GtkTreeIter     *iter,
                gpointer         data)
865 866 867
{
  GtkPageSetup *page_setup;
  GtkPaperSize *paper_size;
868

869
  gtk_tree_model_get (tree_model, iter,
870
                      PAGE_SETUP_LIST_COL_PAGE_SETUP, &page_setup, -1);
871 872 873 874 875 876 877
  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
878
    g_object_set (cell, "text",  _("Manage Custom Sizes…"), NULL);
879

880 881
}

Matthias Clasen's avatar
Matthias Clasen committed
882 883
/**
 * gtk_page_setup_unix_dialog_new:
884 885
 * @title: (allow-none): the title of the dialog, or %NULL
 * @parent: (allow-none): transient parent of the dialog, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
886 887 888 889 890 891 892
 *
 * Creates a new page setup dialog.
 *
 * Returns: the new #GtkPageSetupUnixDialog
 *
 * Since: 2.10
 */
893 894
GtkWidget *
gtk_page_setup_unix_dialog_new (const gchar *title,
895
                                GtkWindow   *parent)
896 897 898 899 900
{
  GtkWidget *result;

  if (title == NULL)
    title = _("Page Setup");
901

902 903 904 905 906 907 908 909 910 911 912 913 914
  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)
{
915 916 917
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->portrait_radio)))
918
    return GTK_PAGE_ORIENTATION_PORTRAIT;
919
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->landscape_radio)))
920
    return GTK_PAGE_ORIENTATION_LANDSCAPE;
921
  if (gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio)))
922
    return GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE;
923
  return GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT;
924 925 926
}

static void
927 928
set_orientation (GtkPageSetupUnixDialog *dialog,
                 GtkPageOrientation      orientation)
929
{
930 931
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

932 933 934
  switch (orientation)
    {
    case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
935 936
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->reverse_portrait_radio), TRUE);
      break;
937
    case GTK_PAGE_ORIENTATION_PORTRAIT:
938
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->portrait_radio), TRUE);
939 940
      break;
    case GTK_PAGE_ORIENTATION_LANDSCAPE:
941
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->landscape_radio), TRUE);
942 943
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
944
      gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (priv->reverse_landscape_radio), TRUE);
945 946 947 948
      break;
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
949 950 951 952
/**
 * gtk_page_setup_unix_dialog_set_page_setup:
 * @dialog: a #GtkPageSetupUnixDialog
 * @page_setup: a #GtkPageSetup
953
 *
Matthias Clasen's avatar
Matthias Clasen committed
954 955 956 957 958
 * Sets the #GtkPageSetup from which the page setup
 * dialog takes its values.
 *
 * Since: 2.10
 **/
959 960
void
gtk_page_setup_unix_dialog_set_page_setup (GtkPageSetupUnixDialog *dialog,
961
                                           GtkPageSetup           *page_setup)
962 963 964 965 966 967 968 969
{
  if (page_setup)
    {
      set_paper_size (dialog, page_setup, FALSE, TRUE);
      set_orientation (dialog, gtk_page_setup_get_orientation (page_setup));
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
970 971 972
/**
 * gtk_page_setup_unix_dialog_get_page_setup:
 * @dialog: a #GtkPageSetupUnixDialog
973 974 975 976
 *
 * Gets the currently selected page setup from the dialog.
 *
 * Returns: (transfer none): the current page setup
Matthias Clasen's avatar
Matthias Clasen committed
977 978 979
 *
 * Since: 2.10
 **/
980 981 982 983
GtkPageSetup *
gtk_page_setup_unix_dialog_get_page_setup (GtkPageSetupUnixDialog *dialog)
{
  GtkPageSetup *page_setup;
984

985 986 987 988 989 990 991 992 993
  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,
994
                    const gchar            *printer_name)
995
{
996
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
997 998 999 1000
  GtkTreeModel *model;
  GtkTreeIter iter;
  GtkPrinter *printer;

1001
  model = GTK_TREE_MODEL (priv->printer_list);
1002 1003 1004 1005

  if (gtk_tree_model_get_iter_first (model, &iter))
    {
      do
1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022
        {
          gtk_tree_model_get (GTK_TREE_MODEL (priv->printer_list), &iter,
                              PRINTER_LIST_COL_PRINTER, &printer, -1);
          if (printer == NULL)
            continue;

          if (strcmp (gtk_printer_get_name (printer), printer_name) == 0)
            {
              gtk_combo_box_set_active_iter (GTK_COMBO_BOX (priv->printer_combo),
                                             &iter);
              g_object_unref (printer);
              return TRUE;
            }

          g_object_unref (printer);

        } while (gtk_tree_model_iter_next (model, &iter));
1023
    }
1024

1025 1026 1027
  return FALSE;
}

Matthias Clasen's avatar
Matthias Clasen committed
1028 1029 1030 1031
/**
 * gtk_page_setup_unix_dialog_set_print_settings:
 * @dialog: a #GtkPageSetupUnixDialog
 * @print_settings: a #GtkPrintSettings
1032
 *
Matthias Clasen's avatar
Matthias Clasen committed
1033 1034
 * Sets the #GtkPrintSettings from which the page setup dialog
 * takes its values.
1035
 *
Matthias Clasen's avatar
Matthias Clasen committed
1036 1037
 * Since: 2.10
 **/
1038 1039
void
gtk_page_setup_unix_dialog_set_print_settings (GtkPageSetupUnixDialog *dialog,
1040
                                               GtkPrintSettings       *print_settings)
1041
{
1042
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;
Matthias Clasen's avatar
Matthias Clasen committed
1043
  const gchar *format_for_printer;
1044

1045 1046
  if (priv->print_settings == print_settings) return;

1047 1048
  if (priv->print_settings)
    g_object_unref (priv->print_settings);
1049

1050
  priv->print_settings = print_settings;
1051 1052 1053 1054 1055 1056 1057

  if (print_settings)
    {
      g_object_ref (print_settings);

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

1058 1059
      /* Set printer if in list, otherwise set when
       * that printer is added
Matthias Clasen's avatar
Matthias Clasen committed
1060
       */
1061
      if (format_for_printer &&
1062 1063
          !set_active_printer (dialog, format_for_printer))
        priv->waiting_for_printer = g_strdup (format_for_printer);
1064 1065 1066
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
1067 1068 1069
/**
 * gtk_page_setup_unix_dialog_get_print_settings:
 * @dialog: a #GtkPageSetupUnixDialog
1070
 *
Matthias Clasen's avatar
Matthias Clasen committed
1071
 * Gets the current print settings from the dialog.
1072
 *
1073
 * Returns: (transfer none): the current print settings
Matthias Clasen's avatar
Matthias Clasen committed
1074 1075 1076
 *
 * Since: 2.10
 **/
1077 1078 1079
GtkPrintSettings *
gtk_page_setup_unix_dialog_get_print_settings (GtkPageSetupUnixDialog *dialog)
{
1080 1081 1082
  GtkPageSetupUnixDialogPrivate *priv = dialog->priv;

  return priv->print_settings;
1083
}