gtkprintunixdialog.c 132 KB
Newer Older
1 2 3
/* GtkPrintUnixDialog
 * Copyright (C) 2006 John (J5) Palmieri  <johnp@redhat.com>
 * Copyright (C) 2006 Alexander Larsson <alexl@redhat.com>
4
 * Copyright © 2006, 2007 Christian Persch
5 6 7 8 9 10 11 12 13 14 15 16
 *
 * 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
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20
 */

#include "config.h"
21

22 23
#include <stdlib.h>
#include <string.h>
24
#include <ctype.h>
25 26 27
#include <stdio.h>
#include <math.h>

28 29 30 31 32 33 34
#include "gtkprintunixdialog.h"

#include "gtkcustompaperunixdialog.h"
#include "gtkprintbackend.h"
#include "gtkprinter-private.h"
#include "gtkprinteroptionwidget.h"
#include "gtkprintutils.h"
35 36 37 38 39 40 41 42 43 44 45 46

#include "gtkspinbutton.h"
#include "gtkcellrendererpixbuf.h"
#include "gtkcellrenderertext.h"
#include "gtkimage.h"
#include "gtktreeselection.h"
#include "gtknotebook.h"
#include "gtkscrolledwindow.h"
#include "gtkcombobox.h"
#include "gtktogglebutton.h"
#include "gtkradiobutton.h"
#include "gtkdrawingarea.h"
47 48
#include "gtkbox.h"
#include "gtkgrid.h"
49 50
#include "gtkframe.h"
#include "gtklabel.h"
51
#include "gtkeventbox.h"
52
#include "gtkbuildable.h"
53 54
#include "gtkmessagedialog.h"
#include "gtkbutton.h"
55 56 57
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtktypebuiltins.h"
58
#include "gtkdialogprivate.h"
59
#include "gtkstylecontextprivate.h"
60
#include "gtkwidgetprivate.h"
61

62 63 64 65 66 67 68 69

/**
 * SECTION:gtkprintunixdialog
 * @Short_description: A print dialog
 * @Title: GtkPrintUnixDialog
 * @See_also: #GtkPageSetupUnixDialog, #GtkPrinter, #GtkPrintJob
 *
 * GtkPrintUnixDialog implements a print dialog for platforms
70
 * which don’t provide a native print dialog, like Unix. It can
71 72
 * be used very much like any other GTK+ dialog, at the cost of
 * the portability offered by the
73
 * [high-level printing API][gtk3-High-level-Printing-API]
74 75 76 77 78 79 80
 *
 * In order to print something with #GtkPrintUnixDialog, you need
 * to use gtk_print_unix_dialog_get_selected_printer() to obtain
 * a #GtkPrinter object and use it to construct a #GtkPrintJob using
 * gtk_print_job_new().
 *
 * #GtkPrintUnixDialog uses the following response values:
81 82 83
 * - %GTK_RESPONSE_OK: for the “Print” button
 * - %GTK_RESPONSE_APPLY: for the “Preview” button
 * - %GTK_RESPONSE_CANCEL: for the “Cancel” button
84 85 86
 *
 * Printing support was added in GTK+ 2.10.
 *
87
 * # GtkPrintUnixDialog as GtkBuildable
88
 *
89
 * The GtkPrintUnixDialog implementation of the GtkBuildable interface exposes its
90
 * @notebook internal children with the name “notebook”.
91
 *
92
 * An example of a #GtkPrintUnixDialog UI definition fragment:
93
 * |[
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
 * <object class="GtkPrintUnixDialog" id="dialog1">
 *   <child internal-child="notebook">
 *     <object class="GtkNotebook" id="notebook">
 *       <child>
 *         <object class="GtkLabel" id="tabcontent">
 *         <property name="label">Content on notebook tab</property>
 *         </object>
 *       </child>
 *       <child type="tab">
 *         <object class="GtkLabel" id="tablabel">
 *           <property name="label">Tab label</property>
 *         </object>
 *         <packing>
 *           <property name="tab_expand">False</property>
 *           <property name="tab_fill">False</property>
 *         </packing>
 *       </child>
 *     </object>
 *   </child>
 * </object>
114
 * ]|
115 116 117 118
 *
 * # CSS nodes
 *
 * GtkPrintUnixDialog has a single CSS node with name printdialog.
119 120 121
 */


122
#define EXAMPLE_PAGE_AREA_SIZE 110
123 124 125
#define RULER_DISTANCE 7.5
#define RULER_RADIUS 2

126

127
static void     gtk_print_unix_dialog_constructed  (GObject            *object);
128
static void     gtk_print_unix_dialog_destroy      (GtkWidget          *widget);
129 130
static void     gtk_print_unix_dialog_finalize     (GObject            *object);
static void     gtk_print_unix_dialog_set_property (GObject            *object,
Matthias Clasen's avatar
Matthias Clasen committed
131 132 133
                                                    guint               prop_id,
                                                    const GValue       *value,
                                                    GParamSpec         *pspec);
134
static void     gtk_print_unix_dialog_get_property (GObject            *object,
Matthias Clasen's avatar
Matthias Clasen committed
135 136 137
                                                    guint               prop_id,
                                                    GValue             *value,
                                                    GParamSpec         *pspec);
138
static void     gtk_print_unix_dialog_style_updated (GtkWidget          *widget);
139 140
static void     unschedule_idle_mark_conflicts     (GtkPrintUnixDialog *dialog);
static void     selected_printer_changed           (GtkTreeSelection   *selection,
Matthias Clasen's avatar
Matthias Clasen committed
141
                                                    GtkPrintUnixDialog *dialog);
142 143
static void     clear_per_printer_ui               (GtkPrintUnixDialog *dialog);
static void     printer_added_cb                   (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
144 145
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
146
static void     printer_removed_cb                 (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
147 148
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
149
static void     printer_status_cb                  (GtkPrintBackend    *backend,
Matthias Clasen's avatar
Matthias Clasen committed
150 151
                                                    GtkPrinter         *printer,
                                                    GtkPrintUnixDialog *dialog);
152
static void     update_collate_icon                (GtkToggleButton    *toggle_button,
Matthias Clasen's avatar
Matthias Clasen committed
153
                                                    GtkPrintUnixDialog *dialog);
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
static gboolean error_dialogs                      (GtkPrintUnixDialog *print_dialog,
						    gint                print_dialog_response_id,
						    gpointer            data);
static void     emit_ok_response                   (GtkTreeView        *tree_view,
						    GtkTreePath        *path,
						    GtkTreeViewColumn  *column,
						    gpointer           *user_data);
static void     update_page_range_entry_sensitivity(GtkWidget          *button,
						    GtkPrintUnixDialog *dialog);
static void     update_print_at_entry_sensitivity  (GtkWidget          *button,
						    GtkPrintUnixDialog *dialog);
static void     update_print_at_option             (GtkPrintUnixDialog *dialog);
static void     update_dialog_from_capabilities    (GtkPrintUnixDialog *dialog);
static gboolean draw_collate_cb                    (GtkWidget          *widget,
						    cairo_t            *cr,
						    GtkPrintUnixDialog *dialog);
static gboolean is_printer_active                  (GtkTreeModel        *model,
						    GtkTreeIter         *iter,
						    GtkPrintUnixDialog  *dialog);
static gint     default_printer_list_sort_func     (GtkTreeModel        *model,
						    GtkTreeIter         *a,
						    GtkTreeIter         *b,
						    gpointer             user_data);
static gboolean paper_size_row_is_separator        (GtkTreeModel        *model,
						    GtkTreeIter         *iter,
						    gpointer             data);
static void     page_name_func                     (GtkCellLayout       *cell_layout,
						    GtkCellRenderer     *cell,
						    GtkTreeModel        *tree_model,
						    GtkTreeIter         *iter,
						    gpointer             data);
static void     update_number_up_layout            (GtkPrintUnixDialog  *dialog);
static gboolean draw_page_cb                       (GtkWidget           *widget,
						    cairo_t             *cr,
						    GtkPrintUnixDialog  *dialog);


191 192
static gboolean dialog_get_collate                 (GtkPrintUnixDialog *dialog);
static gboolean dialog_get_reverse                 (GtkPrintUnixDialog *dialog);
193
static gint     dialog_get_n_copies                (GtkPrintUnixDialog *dialog);
194

195
static void     set_cell_sensitivity_func          (GtkTreeViewColumn *tree_column,
Matthias Clasen's avatar
Matthias Clasen committed
196 197 198 199
                                                    GtkCellRenderer   *cell,
                                                    GtkTreeModel      *model,
                                                    GtkTreeIter       *iter,
                                                    gpointer           data);
200
static gboolean set_active_printer                 (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
201
                                                    const gchar        *printer_name);
202
static void redraw_page_layout_preview             (GtkPrintUnixDialog *dialog);
203
static void load_print_backends                    (GtkPrintUnixDialog *dialog);
204 205 206 207 208
static gboolean printer_compare                    (GtkTreeModel       *model,
                                                    gint                column,
                                                    const gchar        *key,
                                                    GtkTreeIter        *iter,
                                                    gpointer            search_data);
209

210 211 212 213 214 215
/* GtkBuildable */
static void gtk_print_unix_dialog_buildable_init                    (GtkBuildableIface *iface);
static GObject *gtk_print_unix_dialog_buildable_get_internal_child  (GtkBuildable *buildable,
                                                                     GtkBuilder   *builder,
                                                                     const gchar  *childname);

216
static const gchar common_paper_sizes[][16] = {
217 218 219 220 221 222 223 224 225 226 227 228 229 230
  "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",
};

231
/* Keep in line with liststore defined in gtkprintunixdialog.ui */
232 233 234 235 236 237
enum {
  PAGE_SETUP_LIST_COL_PAGE_SETUP,
  PAGE_SETUP_LIST_COL_IS_SEPARATOR,
  PAGE_SETUP_LIST_N_COLS
};

238 239 240 241 242 243 244 245 246 247 248
/* Keep in line with liststore defined in gtkprintunixdialog.ui */
enum {
  PRINTER_LIST_COL_ICON,
  PRINTER_LIST_COL_NAME,
  PRINTER_LIST_COL_STATE,
  PRINTER_LIST_COL_JOBS,
  PRINTER_LIST_COL_LOCATION,
  PRINTER_LIST_COL_PRINTER_OBJ,
  PRINTER_LIST_N_COLS
};

249 250 251 252 253
enum {
  PROP_0,
  PROP_PAGE_SETUP,
  PROP_CURRENT_PAGE,
  PROP_PRINT_SETTINGS,
254 255 256
  PROP_SELECTED_PRINTER,
  PROP_MANUAL_CAPABILITIES,
  PROP_SUPPORT_SELECTION,
257 258
  PROP_HAS_SELECTION,
  PROP_EMBED_PAGE_SETUP
259 260 261 262 263 264 265
};

struct GtkPrintUnixDialogPrivate
{
  GtkWidget *notebook;

  GtkWidget *printer_treeview;
266 267 268 269 270 271 272 273
  GtkTreeViewColumn *printer_icon_column;
  GtkTreeViewColumn *printer_name_column;
  GtkTreeViewColumn *printer_location_column;
  GtkTreeViewColumn *printer_status_column;
  GtkCellRenderer *printer_icon_renderer;
  GtkCellRenderer *printer_name_renderer;
  GtkCellRenderer *printer_location_renderer;
  GtkCellRenderer *printer_status_renderer;
274 275 276

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

278 279 280 281
  GtkTreeModel *printer_list;
  GtkTreeModelFilter *printer_list_filter;

  GtkPageSetup *page_setup;
282
  gboolean page_setup_set;
283 284 285
  gboolean embed_page_setup;
  GtkListStore *page_setup_list;
  GtkListStore *custom_paper_list;
286

287 288 289
  gboolean support_selection;
  gboolean has_selection;

290 291
  GtkWidget *all_pages_radio;
  GtkWidget *current_page_radio;
292 293
  GtkWidget *selection_radio;
  GtkWidget *range_table;
294 295
  GtkWidget *page_range_radio;
  GtkWidget *page_range_entry;
Matthias Clasen's avatar
Matthias Clasen committed
296

297 298 299 300 301 302 303 304 305 306 307
  GtkWidget *copies_spin;
  GtkWidget *collate_check;
  GtkWidget *reverse_check;
  GtkWidget *collate_image;
  GtkWidget *page_layout_preview;
  GtkWidget *scale_spin;
  GtkWidget *page_set_combo;
  GtkWidget *print_now_radio;
  GtkWidget *print_at_radio;
  GtkWidget *print_at_entry;
  GtkWidget *print_hold_radio;
308 309
  GtkWidget *paper_size_combo;
  GtkWidget *paper_size_combo_label;
310
  GtkCellRenderer *paper_size_renderer;
311 312 313
  GtkWidget *orientation_combo;
  GtkWidget *orientation_combo_label;
  gboolean internal_page_setup_change;
314 315 316 317 318 319 320 321 322 323
  gboolean updating_print_at;
  GtkPrinterOptionWidget *pages_per_sheet;
  GtkPrinterOptionWidget *duplex;
  GtkPrinterOptionWidget *paper_type;
  GtkPrinterOptionWidget *paper_source;
  GtkPrinterOptionWidget *output_tray;
  GtkPrinterOptionWidget *job_prio;
  GtkPrinterOptionWidget *billing_info;
  GtkPrinterOptionWidget *cover_before;
  GtkPrinterOptionWidget *cover_after;
324
  GtkPrinterOptionWidget *number_up_layout;
325 326 327 328 329 330 331 332 333 334 335 336 337 338

  GtkWidget *conflicts_widget;

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

  GtkWidget *advanced_vbox;
  GtkWidget *advanced_page;

Matthias Clasen's avatar
~  
Matthias Clasen committed
339
  GtkWidget *extension_point;
340

Matthias Clasen's avatar
Matthias Clasen committed
341 342 343
  /* These are set initially on selected printer (either default printer,
   * printer taken from set settings, or user-selected), but when any
   * setting is changed by the user it is cleared.
Matthias Clasen's avatar
Matthias Clasen committed
344
   */
345
  GtkPrintSettings *initial_settings;
346 347 348

  GtkPrinterOption *number_up_layout_n_option;
  GtkPrinterOption *number_up_layout_2_option;
Matthias Clasen's avatar
Matthias Clasen committed
349 350 351

  /* This is the initial printer set by set_settings. We look for it in
   * the added printers. We clear this whenever the user manually changes
352
   * to another printer, when the user changes a setting or when we find
Matthias Clasen's avatar
Matthias Clasen committed
353 354
   * this printer.
   */
355 356
  char *waiting_for_printer;
  gboolean internal_printer_change;
Matthias Clasen's avatar
Matthias Clasen committed
357

358
  GList *print_backends;
Matthias Clasen's avatar
Matthias Clasen committed
359

360
  GtkPrinter *current_printer;
361
  GtkPrinter *request_details_printer;
362
  gulong request_details_tag;
363 364 365 366
  GtkPrinterOptionSet *options;
  gulong options_changed_handler;
  gulong mark_conflicts_id;

Matthias Clasen's avatar
Matthias Clasen committed
367
  gchar *format_for_printer;
Matthias Clasen's avatar
Matthias Clasen committed
368

369 370 371
  gint current_page;
};

372
G_DEFINE_TYPE_WITH_CODE (GtkPrintUnixDialog, gtk_print_unix_dialog, GTK_TYPE_DIALOG,
373
                         G_ADD_PRIVATE (GtkPrintUnixDialog)
374 375 376 377
                         G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
                                                gtk_print_unix_dialog_buildable_init))

static GtkBuildableIface *parent_buildable_iface;
378 379 380

static gboolean
is_default_printer (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
381
                    GtkPrinter         *printer)
382
{
383 384 385 386
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->format_for_printer)
    return strcmp (priv->format_for_printer,
Matthias Clasen's avatar
Matthias Clasen committed
387
                   gtk_printer_get_name (printer)) == 0;
388 389 390 391 392 393 394 395 396 397 398 399 400
 else
   return gtk_printer_is_default (printer);
}

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

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

401
  object_class->constructed = gtk_print_unix_dialog_constructed;
402 403 404 405
  object_class->finalize = gtk_print_unix_dialog_finalize;
  object_class->set_property = gtk_print_unix_dialog_set_property;
  object_class->get_property = gtk_print_unix_dialog_get_property;

406
  widget_class->style_updated = gtk_print_unix_dialog_style_updated;
407
  widget_class->destroy = gtk_print_unix_dialog_destroy;
408

409
  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
410 411 412 413 414 415
                                   PROP_PAGE_SETUP,
                                   g_param_spec_object ("page-setup",
                                                        P_("Page Setup"),
                                                        P_("The GtkPageSetup to use"),
                                                        GTK_TYPE_PAGE_SETUP,
                                                        GTK_PARAM_READWRITE));
416 417

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
418 419 420 421 422 423 424 425
                                   PROP_CURRENT_PAGE,
                                   g_param_spec_int ("current-page",
                                                     P_("Current Page"),
                                                     P_("The current page in the document"),
                                                     -1,
                                                     G_MAXINT,
                                                     -1,
                                                     GTK_PARAM_READWRITE));
426 427

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
428 429 430 431 432 433
                                   PROP_PRINT_SETTINGS,
                                   g_param_spec_object ("print-settings",
                                                        P_("Print Settings"),
                                                        P_("The GtkPrintSettings used for initializing the dialog"),
                                                        GTK_TYPE_PRINT_SETTINGS,
                                                        GTK_PARAM_READWRITE));
434 435

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
436 437 438 439 440 441 442
                                   PROP_SELECTED_PRINTER,
                                   g_param_spec_object ("selected-printer",
                                                        P_("Selected Printer"),
                                                        P_("The GtkPrinter which is selected"),
                                                        GTK_TYPE_PRINTER,
                                                        GTK_PARAM_READABLE));

443
  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
444 445
                                   PROP_MANUAL_CAPABILITIES,
                                   g_param_spec_flags ("manual-capabilities",
446
                                                       P_("Manual Capabilities"),
Matthias Clasen's avatar
Matthias Clasen committed
447 448 449 450
                                                       P_("Capabilities the application can handle"),
                                                       GTK_TYPE_PRINT_CAPABILITIES,
                                                       0,
                                                       GTK_PARAM_READWRITE));
451 452

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
453 454 455 456 457 458
                                   PROP_SUPPORT_SELECTION,
                                   g_param_spec_boolean ("support-selection",
                                                         P_("Support Selection"),
                                                         P_("Whether the dialog supports selection"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
459 460

  g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
461 462 463 464 465 466 467
                                   PROP_HAS_SELECTION,
                                   g_param_spec_boolean ("has-selection",
                                                         P_("Has Selection"),
                                                         P_("Whether the application has a selection"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));

468
   g_object_class_install_property (object_class,
Matthias Clasen's avatar
Matthias Clasen committed
469 470 471 472 473 474
                                   PROP_EMBED_PAGE_SETUP,
                                   g_param_spec_boolean ("embed-page-setup",
                                                         P_("Embed Page Setup"),
                                                         P_("TRUE if page setup combos are embedded in GtkPrintUnixDialog"),
                                                         FALSE,
                                                         GTK_PARAM_READWRITE));
475

476 477 478
  /* Bind class to template
   */
  gtk_widget_class_set_template_from_resource (widget_class,
479
					       "/org/gtk/libgtk/ui/gtkprintunixdialog.ui");
480 481

  /* GtkTreeView / GtkTreeModel */
482 483 484 485 486 487 488 489 490 491 492 493 494
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_treeview);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_list_filter);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, page_setup_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, custom_paper_list);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_icon_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_name_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_location_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_status_column);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_icon_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_name_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_location_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, printer_status_renderer);
495 496

  /* General Widgetry */
497 498 499 500 501 502 503 504 505 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
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, notebook);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, all_pages_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, all_pages_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, current_page_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, selection_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, range_table);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, page_range_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, page_range_entry);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, copies_spin);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, collate_check);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, reverse_check);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, collate_image);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, page_layout_preview);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, scale_spin);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, page_set_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, print_now_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, print_at_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, print_at_entry);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, print_hold_radio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, paper_size_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, paper_size_combo_label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, paper_size_renderer);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, orientation_combo);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, orientation_combo_label);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, conflicts_widget);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, job_page);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, finishing_table);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, finishing_page);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, image_quality_table);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, image_quality_page);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, color_table);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, color_page);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, advanced_vbox);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, advanced_page);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, extension_point);
532 533

  /* GtkPrinterOptionWidgets... */
534 535 536 537 538 539 540 541 542 543
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, pages_per_sheet);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, duplex);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, paper_type);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, paper_source);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, output_tray);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, job_prio);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, billing_info);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, cover_before);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, cover_after);
  gtk_widget_class_bind_template_child_private (widget_class, GtkPrintUnixDialog, number_up_layout);
544 545

  /* Callbacks handled in the UI */
546 547 548 549 550 551 552 553 554 555 556 557 558 559
  gtk_widget_class_bind_template_callback (widget_class, redraw_page_layout_preview);
  gtk_widget_class_bind_template_callback (widget_class, error_dialogs);
  gtk_widget_class_bind_template_callback (widget_class, emit_ok_response);
  gtk_widget_class_bind_template_callback (widget_class, selected_printer_changed);
  gtk_widget_class_bind_template_callback (widget_class, update_page_range_entry_sensitivity);
  gtk_widget_class_bind_template_callback (widget_class, update_print_at_entry_sensitivity);
  gtk_widget_class_bind_template_callback (widget_class, update_print_at_option);
  gtk_widget_class_bind_template_callback (widget_class, update_dialog_from_capabilities);
  gtk_widget_class_bind_template_callback (widget_class, update_collate_icon);
  gtk_widget_class_bind_template_callback (widget_class, draw_collate_cb);
  gtk_widget_class_bind_template_callback (widget_class, redraw_page_layout_preview);
  gtk_widget_class_bind_template_callback (widget_class, update_number_up_layout);
  gtk_widget_class_bind_template_callback (widget_class, redraw_page_layout_preview);
  gtk_widget_class_bind_template_callback (widget_class, draw_page_cb);
560 561

  gtk_widget_class_set_css_name (widget_class, "printdialog");
562 563
}

564 565 566 567 568 569 570
/* Returns a toplevel GtkWindow, or NULL if none */
static GtkWindow *
get_toplevel (GtkWidget *widget)
{
  GtkWidget *toplevel = NULL;

  toplevel = gtk_widget_get_toplevel (widget);
571
  if (!gtk_widget_is_toplevel (toplevel))
572 573 574 575 576
    return NULL;
  else
    return GTK_WINDOW (toplevel);
}

577 578
static void
set_busy_cursor (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
579
                 gboolean            busy)
580
{
581
  GtkWidget *widget;
582 583 584 585 586
  GtkWindow *toplevel;
  GdkDisplay *display;
  GdkCursor *cursor;

  toplevel = get_toplevel (GTK_WIDGET (dialog));
587 588 589
  widget = GTK_WIDGET (toplevel);

  if (!toplevel || !gtk_widget_get_realized (widget))
590 591
    return;

592
  display = gtk_widget_get_display (widget);
593 594

  if (busy)
595
    cursor = gdk_cursor_new_from_name (display, "progress");
596 597 598
  else
    cursor = NULL;

Matthias Clasen's avatar
Matthias Clasen committed
599
  gdk_window_set_cursor (gtk_widget_get_window (widget), cursor);
600 601 602
  gdk_display_flush (display);

  if (cursor)
603
    g_object_unref (cursor);
604 605
}

606
/* This function handles error messages before printing.
607 608
 */
static gboolean
609 610 611
error_dialogs (GtkPrintUnixDialog *print_dialog,
               gint                print_dialog_response_id,
               gpointer            data)
612 613 614 615 616 617
{
  GtkPrintUnixDialogPrivate *priv = print_dialog->priv;
  GtkPrinterOption          *option = NULL;
  GtkPrinter                *printer = NULL;
  GtkWindow                 *toplevel = NULL;
  GtkWidget                 *dialog = NULL;
618
  GFile                     *file = NULL;
619 620 621 622 623 624 625 626
  gchar                     *basename = NULL;
  gchar                     *dirname = NULL;
  int                        response;

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

627
      if (printer != NULL)
628
        {
629 630 631 632 633
          if (priv->request_details_tag || !gtk_printer_is_accepting_jobs (printer))
            {
              g_signal_stop_emission_by_name (print_dialog, "response");
              return TRUE;
            }
634

Matthias Clasen's avatar
Matthias Clasen committed
635 636 637
          /* Shows overwrite confirmation dialog in the case of printing
           * to file which already exists.
           */
638
          if (gtk_printer_is_virtual (printer))
639
            {
640 641
              option = gtk_printer_option_set_lookup (priv->options,
                                                      "gtk-main-page-custom-input");
642

643 644
              if (option != NULL &&
                  option->type == GTK_PRINTER_OPTION_TYPE_FILESAVE)
645
                {
646
                  file = g_file_new_for_uri (option->value);
647

648
                  if (g_file_query_exists (file, NULL))
649
                    {
650 651
                      GFile *parent;

652 653 654
                      toplevel = get_toplevel (GTK_WIDGET (print_dialog));

                      basename = g_file_get_basename (file);
655 656 657
                      parent = g_file_get_parent (file);
                      dirname = g_file_get_parse_name (parent);
                      g_object_unref (parent);
658 659 660 661 662 663

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

                      gtk_message_dialog_format_secondary_text (GTK_MESSAGE_DIALOG (dialog),
668
                                                                _("The file already exists in “%s”.  Replacing it will "
669 670 671 672
                                                                "overwrite its contents."),
                                                                dirname);

                      gtk_dialog_add_button (GTK_DIALOG (dialog),
673 674 675 676 677
                                             _("_Cancel"),
                                             GTK_RESPONSE_CANCEL);
                      gtk_dialog_add_button (GTK_DIALOG (dialog),
                                             _("_Replace"),
                                             GTK_RESPONSE_ACCEPT);
678
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
679 680 681 682
                      gtk_dialog_set_alternative_button_order (GTK_DIALOG (dialog),
                                                               GTK_RESPONSE_ACCEPT,
                                                               GTK_RESPONSE_CANCEL,
                                                               -1);
683
G_GNUC_END_IGNORE_DEPRECATIONS
684 685 686
                      gtk_dialog_set_default_response (GTK_DIALOG (dialog),
                                                       GTK_RESPONSE_ACCEPT);

687 688
                      if (gtk_window_has_group (toplevel))
                        gtk_window_group_add_window (gtk_window_get_group (toplevel),
689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
                                                     GTK_WINDOW (dialog));

                      response = gtk_dialog_run (GTK_DIALOG (dialog));

                      gtk_widget_destroy (dialog);

                      g_free (dirname);
                      g_free (basename);

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

706 707
                  g_object_unref (file);
                }
708 709 710 711 712 713
            }
        }
    }
  return FALSE;
}

714 715 716
static void
gtk_print_unix_dialog_init (GtkPrintUnixDialog *dialog)
{
717
  GtkPrintUnixDialogPrivate *priv;
718
  GtkTreeSortable *sort;
719
  GtkWidget *widget;
720

721
  dialog->priv = gtk_print_unix_dialog_get_instance_private (dialog);
722
  priv = dialog->priv;
723

724 725
  priv->print_backends = NULL;
  priv->current_page = -1;
726 727
  priv->number_up_layout_n_option = NULL;
  priv->number_up_layout_2_option = NULL;
728

729
  priv->page_setup = gtk_page_setup_new ();
730
  priv->page_setup_set = FALSE;
731 732
  priv->embed_page_setup = FALSE;
  priv->internal_page_setup_change = FALSE;
733

734 735 736
  priv->support_selection = FALSE;
  priv->has_selection = FALSE;

737 738 739 740 741
  g_type_ensure (GTK_TYPE_PRINTER);
  g_type_ensure (GTK_TYPE_PRINTER_OPTION);
  g_type_ensure (GTK_TYPE_PRINTER_OPTION_SET);
  g_type_ensure (GTK_TYPE_PRINTER_OPTION_WIDGET);

742
  gtk_widget_init_template (GTK_WIDGET (dialog));
743 744 745 746 747 748 749
  gtk_dialog_set_use_header_bar_from_setting (GTK_DIALOG (dialog));
  gtk_dialog_add_buttons (GTK_DIALOG (dialog),
                          _("Pre_view"), GTK_RESPONSE_APPLY,
                          _("_Cancel"), GTK_RESPONSE_CANCEL,
                          _("_Print"), GTK_RESPONSE_OK,
                          NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
750 751
  widget = gtk_dialog_get_widget_for_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  gtk_widget_set_sensitive (widget, FALSE);
752

753
  /* Treeview auxiliary functions need to be setup here */
754 755 756 757
  gtk_tree_model_filter_set_visible_func (priv->printer_list_filter,
                                          (GtkTreeModelFilterVisibleFunc) is_printer_active,
                                          dialog,
                                          NULL);
758

759 760 761 762 763
  sort = GTK_TREE_SORTABLE (priv->printer_list);
  gtk_tree_sortable_set_default_sort_func (sort,
                                           default_printer_list_sort_func,
                                           NULL,
                                           NULL);
764

765 766 767
  gtk_tree_sortable_set_sort_column_id (sort,
                                        GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID,
                                        GTK_SORT_ASCENDING);
Matthias Clasen's avatar
Matthias Clasen committed
768

769 770 771
  gtk_tree_view_set_search_equal_func (GTK_TREE_VIEW (priv->printer_treeview),
                                       printer_compare, NULL, NULL);

772 773 774
  gtk_tree_view_column_set_cell_data_func (priv->printer_icon_column,
					   priv->printer_icon_renderer,
					   set_cell_sensitivity_func, NULL, NULL);
775

776 777 778
  gtk_tree_view_column_set_cell_data_func (priv->printer_name_column,
					   priv->printer_name_renderer,
					   set_cell_sensitivity_func, NULL, NULL);
Alexander Larsson's avatar
Alexander Larsson committed
779

780 781 782
  gtk_tree_view_column_set_cell_data_func (priv->printer_location_column,
					   priv->printer_location_renderer,
					   set_cell_sensitivity_func, NULL, NULL);
783

784 785 786 787 788 789 790 791 792 793 794 795 796 797
  gtk_tree_view_column_set_cell_data_func (priv->printer_status_column,
					   priv->printer_status_renderer,
					   set_cell_sensitivity_func, NULL, NULL);


  /* Paper size combo auxilary funcs */
  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_renderer,
                                      page_name_func, NULL, NULL);

  /* Preview drawing area has no window */
  gtk_widget_set_has_window (priv->page_layout_preview, FALSE);
798

799 800 801 802 803
  /* Load backends */
  load_print_backends (dialog);

  /* Load custom papers */
  _gtk_print_load_custom_papers (priv->custom_paper_list);
804 805

  gtk_css_node_set_name (gtk_widget_get_css_node (priv->collate_image), I_("paper"));
806
  gtk_css_node_set_name (gtk_widget_get_css_node (priv->page_layout_preview), I_("paper"));
Alexander Larsson's avatar
Alexander Larsson committed
807 808
}

809 810
static void
gtk_print_unix_dialog_constructed (GObject *object)
811 812 813
{
  gboolean use_header;

814
  G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->constructed (object);
815 816 817 818 819 820 821 822 823 824 825 826 827

  g_object_get (object, "use-header-bar", &use_header, NULL);
  if (use_header)
    {
       /* Reorder the preview button */
       GtkWidget *button, *parent;
       button = gtk_dialog_get_widget_for_response (GTK_DIALOG (object), GTK_RESPONSE_APPLY);
       g_object_ref (button);
       parent = gtk_widget_get_parent (button);
       gtk_container_remove (GTK_CONTAINER (parent), button); 
       gtk_header_bar_pack_end (GTK_HEADER_BAR (parent), button);
       g_object_unref (button);
    }
828 829

  update_dialog_from_capabilities (GTK_PRINT_UNIX_DIALOG (object));
830 831
}

832
static void
833
gtk_print_unix_dialog_destroy (GtkWidget *widget)
834
{
835 836
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (widget);

837
  /* Make sure we don't destroy custom widgets owned by the backends */
Matthias Clasen's avatar
Matthias Clasen committed
838
  clear_per_printer_ui (dialog);
839 840

  GTK_WIDGET_CLASS (gtk_print_unix_dialog_parent_class)->destroy (widget);
841 842
}

843
static void
Matthias Clasen's avatar
Matthias Clasen committed
844 845
disconnect_printer_details_request (GtkPrintUnixDialog *dialog,
                                    gboolean            details_failed)
846 847 848 849 850 851 852 853
{
  GtkPrintUnixDialogPrivate *priv = dialog->priv;

  if (priv->request_details_tag)
    {
      g_signal_handler_disconnect (priv->request_details_printer,
                                   priv->request_details_tag);
      priv->request_details_tag = 0;
854
      set_busy_cursor (dialog, FALSE);
855 856 857 858 859 860 861 862 863 864 865 866 867 868
      if (details_failed)
        gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
                            g_object_get_data (G_OBJECT (priv->request_details_printer),
                                               "gtk-print-tree-iter"),
                            PRINTER_LIST_COL_STATE,
                             _("Getting printer information failed"),
                            -1);
      else
        gtk_list_store_set (GTK_LIST_STORE (priv->printer_list),
                            g_object_get_data (G_OBJECT (priv->request_details_printer),
                                               "gtk-print-tree-iter"),
                            PRINTER_LIST_COL_STATE,
                            gtk_printer_get_state_message (priv->request_details_printer),
                            -1);
869 870 871 872 873
      g_object_unref (priv->request_details_printer);
      priv->request_details_printer = NULL;
    }
}

874 875 876 877
static void
gtk_print_unix_dialog_finalize (GObject *object)
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
878
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
Alexander Larsson's avatar
Alexander Larsson committed
879 880
  GtkPrintBackend *backend;
  GList *node;
881 882

  unschedule_idle_mark_conflicts (dialog);
883
  disconnect_printer_details_request (dialog, FALSE);
884

Matthias Clasen's avatar
Matthias Clasen committed
885 886
  g_clear_object (&priv->current_printer);
  g_clear_object (&priv->options);
Matthias Clasen's avatar
Matthias Clasen committed
887

888 889 890 891 892 893 894 895 896 897 898 899
  if (priv->number_up_layout_2_option)
    {
      priv->number_up_layout_2_option->choices[0] = NULL;
      priv->number_up_layout_2_option->choices[1] = NULL;
      g_free (priv->number_up_layout_2_option->choices_display[0]);
      g_free (priv->number_up_layout_2_option->choices_display[1]);
      priv->number_up_layout_2_option->choices_display[0] = NULL;
      priv->number_up_layout_2_option->choices_display[1] = NULL;
      g_object_unref (priv->number_up_layout_2_option);
      priv->number_up_layout_2_option = NULL;
    }

Matthias Clasen's avatar
Matthias Clasen committed
900 901 902 903 904
  g_clear_object (&priv->number_up_layout_n_option);
  g_clear_object (&priv->page_setup);
  g_clear_object (&priv->initial_settings);
  g_clear_pointer (&priv->waiting_for_printer, (GDestroyNotify)g_free);
  g_clear_pointer (&priv->format_for_printer, (GDestroyNotify)g_free);
905

Alexander Larsson's avatar
Alexander Larsson committed
906 907 908 909 910 911 912 913 914 915 916
  for (node = priv->print_backends; node != NULL; node = node->next)
    {
      backend = GTK_PRINT_BACKEND (node->data);

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

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

Alexander Larsson's avatar
Alexander Larsson committed
918 919
  g_list_free (priv->print_backends);
  priv->print_backends = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
920

Matthias Clasen's avatar
Matthias Clasen committed
921
  g_clear_object (&priv->page_setup_list);
922

923
  G_OBJECT_CLASS (gtk_print_unix_dialog_parent_class)->finalize (object);
924 925 926
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
927 928 929
printer_removed_cb (GtkPrintBackend    *backend,
                    GtkPrinter         *printer,
                    GtkPrintUnixDialog *dialog)
930
{
931
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
932
  GtkTreeIter *iter;
933

934
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");
935
  gtk_list_store_remove (GTK_LIST_STORE (priv->printer_list), iter);
936 937
}

938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956
static void
gtk_print_unix_dialog_buildable_init (GtkBuildableIface *iface)
{
  parent_buildable_iface = g_type_interface_peek_parent (iface);

  iface->get_internal_child = gtk_print_unix_dialog_buildable_get_internal_child;
}

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

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

Matthias Clasen's avatar
Matthias Clasen committed
957 958 959
/* This function controls "sensitive" property of GtkCellRenderer
 * based on pause state of printers.
 */
960 961 962 963 964 965 966
void set_cell_sensitivity_func (GtkTreeViewColumn *tree_column,
                                GtkCellRenderer   *cell,
                                GtkTreeModel      *tree_model,
                                GtkTreeIter       *iter,
                                gpointer           data)
{
  GtkPrinter *printer;
Matthias Clasen's avatar
Matthias Clasen committed
967

Matthias Clasen's avatar
Matthias Clasen committed
968 969 970
  gtk_tree_model_get (tree_model, iter,
                      PRINTER_LIST_COL_PRINTER_OBJ, &printer,
                      -1);
971 972

  if (printer != NULL && !gtk_printer_is_accepting_jobs (printer))
973
    g_object_set (cell, "sensitive", FALSE, NULL);
974
  else
975 976
    g_object_set (cell, "sensitive", TRUE, NULL);

977
  g_clear_object (&printer);
978 979
}

980
static void
Matthias Clasen's avatar
Matthias Clasen committed
981 982 983
printer_status_cb (GtkPrintBackend    *backend,
                   GtkPrinter         *printer,
                   GtkPrintUnixDialog *dialog)
984
{
985
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
986
  GtkTreeIter *iter;
987
  GtkTreeSelection *selection;
988
  GIcon *icon;
989

990 991
  iter = g_object_get_data (G_OBJECT (printer), "gtk-print-tree-iter");

992 993
  icon = g_themed_icon_new ("printer");
  g_themed_icon_prepend_name (G_THEMED_ICON (icon), gtk_printer_get_icon_name (printer));
994
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), iter,
995
                      PRINTER_LIST_COL_ICON, icon,
996 997 998 999
                      PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
                      PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
                      PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
                      -1);
1000
  g_object_unref (icon);
1001

1002
  /* When the pause state change then we need to update sensitive property
Matthias Clasen's avatar
Matthias Clasen committed
1003 1004
   * of GTK_RESPONSE_OK button inside of selected_printer_changed function.
   */
1005
  selection = gtk_tree_view_get_selection (GTK_TREE_VIEW (priv->printer_treeview));
1006
  priv->internal_printer_change = TRUE;
1007
  selected_printer_changed (selection, dialog);
1008
  priv->internal_printer_change = FALSE;
1009 1010 1011 1012 1013

  if (gtk_print_backend_printer_list_is_done (backend) &&
      gtk_printer_is_default (printer) &&
      (gtk_tree_selection_count_selected_rows (selection) == 0))
    set_active_printer (dialog, gtk_printer_get_name (printer));
1014 1015 1016
}

static void
Matthias Clasen's avatar
Matthias Clasen committed
1017 1018 1019
printer_added_cb (GtkPrintBackend    *backend,
                  GtkPrinter         *printer,
                  GtkPrintUnixDialog *dialog)
1020
{
1021
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1022 1023
  GtkTreeIter iter, filter_iter;
  GtkTreeSelection *selection;
1024
  GtkTreePath *path;
1025
  GIcon *icon;
1026

1027
  gtk_list_store_append (GTK_LIST_STORE (priv->printer_list), &iter);
Matthias Clasen's avatar
Matthias Clasen committed
1028 1029 1030

  g_object_set_data_full (G_OBJECT (printer),
                         "gtk-print-tree-iter",
1031 1032 1033
                          gtk_tree_iter_copy (&iter),
                          (GDestroyNotify) gtk_tree_iter_free);

1034 1035
  icon = g_themed_icon_new ("printer");
  g_themed_icon_prepend_name (G_THEMED_ICON (icon), gtk_printer_get_icon_name (printer));
1036
  gtk_list_store_set (GTK_LIST_STORE (priv->printer_list), &iter,
1037
                      PRINTER_LIST_COL_ICON, icon,
1038 1039 1040 1041 1042 1043
                      PRINTER_LIST_COL_NAME, gtk_printer_get_name (printer),
                      PRINTER_LIST_COL_STATE, gtk_printer_get_state_message (printer),
                      PRINTER_LIST_COL_JOBS, gtk_printer_get_job_count (printer),
                      PRINTER_LIST_COL_LOCATION, gtk_printer_get_location (printer),
                      PRINTER_LIST_COL_PRINTER_OBJ, printer,
                      -1);
1044
  g_object_unref (icon);
1045

1046
  gtk_tree_model_filter_convert_child_iter_to_iter (priv->printer_list_filter,
Matthias Clasen's avatar
Matthias Clasen committed
1047
                                                    &filter_iter, &iter);
1048 1049
  path = gtk_tree_model_get_path (GTK_TREE_MODEL (priv->printer_list_filter), &filter_iter);

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

1052
  if (priv->waiting_for_printer != NULL &&
Matthias Clasen's avatar
Matthias Clasen committed
1053
      strcmp (gtk_printer_get_name (printer), priv->waiting_for_printer) == 0)
1054
    {
1055
      priv->internal_printer_change = TRUE;
1056
      gtk_tree_selection_select_iter (selection, &filter_iter);
1057 1058
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
1059 1060 1061
      priv->internal_printer_change = FALSE;
      g_free (priv->waiting_for_printer);
      priv->waiting_for_printer = NULL;
1062 1063
    }
  else if (is_default_printer (dialog, printer) &&
Matthias Clasen's avatar
Matthias Clasen committed
1064
           gtk_tree_selection_count_selected_rows (selection) == 0)
1065
    {
1066
      priv->internal_printer_change = TRUE;
1067
      gtk_tree_selection_select_iter (selection, &filter_iter);
1068 1069
      gtk_tree_view_scroll_to_cell (GTK_TREE_VIEW (priv->printer_treeview),
                                    path, NULL, TRUE, 0.5, 0.0);
1070
      priv->internal_printer_change = FALSE;
1071
    }
1072 1073

  gtk_tree_path_free (path);
1074 1075 1076 1077
}

static void
printer_list_initialize (GtkPrintUnixDialog *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
1078
                         GtkPrintBackend    *print_backend)
1079 1080 1081 1082 1083 1084
{
  GList *list;
  GList *node;

  g_return_if_fail (print_backend != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
1085 1086
  g_signal_connect_object (print_backend, "printer-added",
                           (GCallback) printer_added_cb, G_OBJECT (dialog), 0);
1087

Matthias Clasen's avatar
Matthias Clasen committed
1088 1089
  g_signal_connect_object (print_backend, "printer-removed",
                           (GCallback) printer_removed_cb, G_OBJECT (dialog), 0);
1090

Matthias Clasen's avatar
Matthias Clasen committed
1091 1092
  g_signal_connect_object (print_backend, "printer-status-changed",
                           (GCallback) printer_status_cb, G_OBJECT (dialog), 0);
1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108

  list = gtk_print_backend_get_printer_list (print_backend);

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

  g_list_free (list);
}

static void
load_print_backends (GtkPrintUnixDialog *dialog)
{
1109
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1110 1111 1112
  GList *node;

  if (g_module_supported ())
1113
    priv->print_backends = gtk_print_backend_load_modules ();
1114

1115
  for (node = priv->print_backends; node != NULL; node = node->next)
1116 1117 1118 1119
    {
      GtkPrintBackend *backend = node->data;
      printer_list_initialize (dialog, backend);
    }
1120 1121 1122 1123
}

static void
gtk_print_unix_dialog_set_property (GObject      *object,
Matthias Clasen's avatar
Matthias Clasen committed
1124 1125 1126
                                    guint         prop_id,
                                    const GValue *value,
                                    GParamSpec   *pspec)
1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141

{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
      gtk_print_unix_dialog_set_page_setup (dialog, g_value_get_object (value));
      break;
    case PROP_CURRENT_PAGE:
      gtk_print_unix_dialog_set_current_page (dialog, g_value_get_int (value));
      break;
    case PROP_PRINT_SETTINGS:
      gtk_print_unix_dialog_set_settings (dialog, g_value_get_object (value));
      break;
1142 1143 1144 1145 1146 1147 1148 1149 1150
    case PROP_MANUAL_CAPABILITIES:
      gtk_print_unix_dialog_set_manual_capabilities (dialog, g_value_get_flags (value));
      break;
    case PROP_SUPPORT_SELECTION:
      gtk_print_unix_dialog_set_support_selection (dialog, g_value_get_boolean (value));
      break;
    case PROP_HAS_SELECTION:
      gtk_print_unix_dialog_set_has_selection (dialog, g_value_get_boolean (value));
      break;
1151 1152 1153
    case PROP_EMBED_PAGE_SETUP:
      gtk_print_unix_dialog_set_embed_page_setup (dialog, g_value_get_boolean (value));
      break;
1154 1155 1156 1157 1158 1159 1160 1161
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_print_unix_dialog_get_property (GObject    *object,
Matthias Clasen's avatar
Matthias Clasen committed
1162 1163 1164
                                    guint       prop_id,
                                    GValue     *value,
                                    GParamSpec *pspec)
1165 1166
{
  GtkPrintUnixDialog *dialog = GTK_PRINT_UNIX_DIALOG (object);
1167
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1168 1169 1170 1171

  switch (prop_id)
    {
    case PROP_PAGE_SETUP:
1172
      g_value_set_object (value, priv->page_setup);
1173 1174
      break;
    case PROP_CURRENT_PAGE:
1175
      g_value_set_int (value, priv->current_page);
1176 1177
      break;
    case PROP_PRINT_SETTINGS:
1178
      g_value_take_object (value, gtk_print_unix_dialog_get_settings (dialog));
1179 1180
      break;
    case PROP_SELECTED_PRINTER:
1181
      g_value_set_object (value, priv->current_printer);
1182
      break;
1183 1184 1185 1186 1187 1188 1189 1190 1191
    case PROP_MANUAL_CAPABILITIES:
      g_value_set_flags (value, priv->manual_capabilities);
      break;
    case PROP_SUPPORT_SELECTION:
      g_value_set_boolean (value, priv->support_selection);
      break;
    case PROP_HAS_SELECTION:
      g_value_set_boolean (value, priv->has_selection);
      break;
1192 1193 1194
    case PROP_EMBED_PAGE_SETUP:
      g_value_set_boolean (value, priv->embed_page_setup);
      break;
1195 1196 1197 1198 1199 1200 1201 1202 1203
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static gboolean
is_printer_active (GtkTreeModel       *model,
                   GtkTreeIter        *iter,
Matthias Clasen's avatar
Matthias Clasen committed
1204
                   GtkPrintUnixDialog *dialog)
1205 1206 1207
{
  gboolean result;
  GtkPrinter *printer;
1208
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1209

1210 1211
  gtk_tree_model_get (model, iter,
                      PRINTER_LIST_COL_PRINTER_OBJ, &printer,
Matthias Clasen's avatar
Matthias Clasen committed
1212 1213
                      -1);

1214 1215 1216 1217
  if (printer == NULL)
    return FALSE;

  result = gtk_printer_is_active (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1218 1219

  if (result &&
1220
      priv->manual_capabilities & (GTK_PRINT_CAPABILITY_GENERATE_PDF |
Matthias Clasen's avatar
Matthias Clasen committed
1221
                                   GTK_PRINT_CAPABILITY_GENERATE_PS))
1222
    {
Matthias Clasen's avatar
Matthias Clasen committed
1223 1224
       /* Check that the printer can handle at least one of the data
        * formats that the application supports.
1225 1226
        */
       result = ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PDF) &&
Matthias Clasen's avatar
Matthias Clasen committed
1227 1228 1229
                 gtk_printer_accepts_pdf (printer)) ||
                ((priv->manual_capabilities & GTK_PRINT_CAPABILITY_GENERATE_PS) &&
                 gtk_printer_accepts_ps (printer));
1230
    }
Matthias Clasen's avatar
Matthias Clasen committed
1231

1232
  g_object_unref (printer);
Matthias Clasen's avatar
Matthias Clasen committed
1233

1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248
  return result;
}

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

Matthias Clasen's avatar
Matthias Clasen committed
1249 1250 1251 1252 1253
  gtk_tree_model_get (model, a,
                      PRINTER_LIST_COL_NAME, &a_name,
                      PRINTER_LIST_COL_PRINTER_OBJ, &a_printer,
                      -1);
  gtk_tree_model_get (model, b,
1254
                      PRINTER_LIST_COL_NAME, &b_name,
Matthias Clasen's avatar
Matthias Clasen committed
1255 1256
                      PRINTER_LIST_COL_PRINTER_OBJ, &b_printer,
                      -1);
1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280

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

  g_free (a_name);
  g_free (b_name);
1281 1282 1283 1284
  if (a_printer)
    g_object_unref (a_printer);
  if (b_printer)
    g_object_unref (b_printer);
1285 1286 1287 1288 1289

  return result;
}

static GtkWidget *
Matthias Clasen's avatar
Matthias Clasen committed
1290
wrap_in_frame (const gchar *label,
1291 1292
               GtkWidget   *child)
{
1293
  GtkWidget *frame, *label_widget;
Matthias Clasen's avatar
Matthias Clasen committed
1294
  gchar *bold_text;
1295

1296
  label_widget = gtk_label_new (NULL);
1297 1298
  gtk_widget_set_halign (label_widget, GTK_ALIGN_START);
  gtk_widget_set_valign (label_widget, GTK_ALIGN_CENTER);
1299
  gtk_widget_show (label_widget);
Matthias Clasen's avatar
Matthias Clasen committed
1300

1301 1302 1303
  bold_text = g_markup_printf_escaped ("<b>%s</b>", label);
  gtk_label_set_markup (GTK_LABEL (label_widget), bold_text);
  g_free (bold_text);
Matthias Clasen's avatar
Matthias Clasen committed
1304

1305
  frame = gtk_box_new (GTK_ORIENTATION_VERTICAL, 6);
1306
  gtk_box_pack_start (GTK_BOX (frame), label_widget, FALSE, FALSE, 0);
Matthias Clasen's avatar
Matthias Clasen committed
1307

1308
  gtk_widget_set_margin_start (child, 12);
1309 1310
  gtk_widget_set_halign (child, GTK_ALIGN_FILL);
  gtk_widget_set_valign (child, GTK_ALIGN_FILL);
1311

1312
  gtk_box_pack_start (GTK_BOX (frame), child, FALSE, FALSE, 0);
1313 1314

  gtk_widget_show (frame);
Matthias Clasen's avatar
Matthias Clasen committed
1315

1316 1317 1318 1319 1320
  return frame;
}

static gboolean
setup_option (GtkPrintUnixDialog     *dialog,
Matthias Clasen's avatar
Matthias Clasen committed
1321 1322
              const gchar            *option_name,
              GtkPrinterOptionWidget *widget)
1323
{
1324
  GtkPrintUnixDialogPrivate *priv = dialog->priv;
1325 1326
  GtkPrinterOption *option;

1327
  option = gtk_printer_option_set_lookup (priv->options, option_name);
1328 1329 1330 1331 1332 1333
  gtk_printer_option_widget_set_source (widget, option);

  return option != NULL;
}

static void
1334
add_option_to_extension_point (GtkPrinterOption *option,
Matthias Clasen's avatar
Matthias Clasen committed
1335
                               gpointer          data)
1336
{
Matthias Clasen's avatar
~  
Matthias Clasen committed
1337 1338
  GtkWidget *extension_point = data;
  GtkWidget *widget;
1339

Matthias Clasen's avatar
~  
Matthias Clasen committed
1340 1341
  widget = gtk_printer_option_widget_new (option);
  gtk_widget_show (widget);
Matthias Clasen's avatar
Matthias Clasen committed
1342

Matthias Clasen's avatar
~  
Matthias Clasen committed
1343 1344 1345
  if (gtk_printer_option_widget_has_external_label (GTK_PRINTER_OPTION_WIDGET (widget)))
    {
      GtkWidget *label, *hbox;
Matthias Clasen's avatar
Matthias Clasen committed
1346

1347 1348
      gtk_widget_set_valign (widget, GTK_ALIGN_BASELINE);

Matthias Clasen's avatar
~  
Matthias Clasen committed
1349 1350
      label = gtk_printer_option_widget_get_external_label (GTK_PRINTER_OPTION_WIDGET (widget));
      gtk_widget_show (label);
1351
      gtk_widget_set_halign (label, GTK_ALIGN_START);
1352
      gtk_widget_set_valign (label, GTK_ALIGN_BASELINE);
Matthias Clasen's avatar
~  
Matthias Clasen committed
1353
      gtk_label_set_mnemonic_widget (GTK_LABEL (label), widget);
Matthias Clasen's avatar
Matthias Clasen committed
1354

1355
      hbox = gtk_box_new (GTK_ORIENTATION_HORIZONTAL, 12);
1356
      gtk_widget_set_valign (hbox, GTK_ALIGN_BASELINE);
Matthias Clasen's avatar
~  
Matthias Clasen committed
1357 1358 1359
      gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
      gtk_box_pack_start (GTK_BOX (hbox), widget, FALSE, FALSE, 0);
      gtk_widget_show (hbox);
Matthias Clasen's avatar
Matthias Clasen committed
1360

1361
      gtk_box_pack_start (GTK_BOX (extension_point), hbox, TRUE, TRUE, 0);
1362 1363
    }
  else
1364
    gtk_box_pack_start (GTK_BOX (extension_point), widget, TRUE, TRUE, 0);
1365 1366
}

1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398
static gint
grid_rows (GtkGrid *table)
{
  gint t0, t1, t, h;
  GList *children, *c;

  children = gtk_container_get_children (GTK_CONTAINER (table));
  t0 = t1 = 0;
  for (c = children; c; c = c->next)
    {
      gtk_container_child_get (GTK_CONTAINER (table), c->data,
                               "top-attach", &t,
                               "height", &h,
                               NULL);
      if (c == children)
        {
          t0 = t;
          t1 = t + h;
        }
      else
        {
          if (t < t0)
            t0 = t;
          if (t + h > t1)
            t1 = t + h;
        }
    }
  g_list_free (children);

  return t1 - t0;
}

1399 1400
static void
add_option_to_table (GtkPrinterOption *option,
Matthias Clasen's avatar
Matthias Clasen committed
1401
                     gpointer          user_data)
1402
{
1403
  GtkGrid *table;
1404
  GtkWidget *label, *widget;
1405
  guint row;
1406

1407
  table = GTK_GRID (user_data);
Matthias Clasen's avatar
Matthias Clasen committed
1408

Alexander Larsson's avatar