gtkprintoperation.c 98.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15
 * gtkprintoperation.c: Print Operation
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * 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
16
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
17 18 19
 */

#include "config.h"
20 21

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

#include <cairo-pdf.h>

28 29 30 31
#include "gtkprintoperation-private.h"
#include "gtkmarshalers.h"
#include "gtkintl.h"
#include "gtkprivate.h"
32
#include "gtkmessagedialog.h"
33
#include "gtktypebuiltins.h"
34

35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
/**
 * SECTION:gtkprintoperation
 * @Title: GtkPrintOperation
 * @Short_description: High-level Printing API
 * @See_also: #GtkPrintContext, #GtkPrintUnixDialog
 *
 * GtkPrintOperation is the high-level, portable printing API.
 * It looks a bit different than other GTK+ dialogs such as the
 * #GtkFileChooser, since some platforms don't expose enough
 * infrastructure to implement a good print dialog. On such
 * platforms, GtkPrintOperation uses the native print dialog.
 * On platforms which do not provide a native print dialog, GTK+
 * uses its own, see #GtkPrintUnixDialog.
 *
 * The typical way to use the high-level printing API is to create
 * a GtkPrintOperation object with gtk_print_operation_new() when
 * the user selects to print. Then you set some properties on it,
 * e.g. the page size, any #GtkPrintSettings from previous print
 * operations, the number of pages, the current page, etc.
 *
 * Then you start the print operation by calling gtk_print_operation_run().
 * It will then show a dialog, let the user select a printer and
 * options. When the user finished the dialog various signals will
 * be emitted on the #GtkPrintOperation, the main one being
 * #GtkPrintOperation::draw-page, which you are supposed to catch
 * and render the page on the provided #GtkPrintContext using Cairo.
 *
 * <example>
 * <title>The high-level printing API</title>
 * <programlisting>
 * static GtkPrintSettings *settings = NULL;
 *
 * static void
 * do_print (void)
 * {
 *   GtkPrintOperation *print;
 *   GtkPrintOperationResult res;
 *
 *   print = gtk_print_operation_new ();
 *
 *   if (settings != NULL)
 *     gtk_print_operation_set_print_settings (print, settings);
 *
 *   g_signal_connect (print, "begin_print", G_CALLBACK (begin_print), NULL);
 *   g_signal_connect (print, "draw_page", G_CALLBACK (draw_page), NULL);
 *
 *   res = gtk_print_operation_run (print, GTK_PRINT_OPERATION_ACTION_PRINT_DIALOG,
 *                                  GTK_WINDOW (main_window), NULL);
 *
 *   if (res == GTK_PRINT_OPERATION_RESULT_APPLY)
 *     {
 *       if (settings != NULL)
 *         g_object_unref (settings);
 *       settings = g_object_ref (gtk_print_operation_get_print_settings (print));
 *     }
 *
 *   g_object_unref (print);
 * }
 * </programlisting>
 * </example>
 *
 * By default GtkPrintOperation uses an external application to do
 * print preview. To implement a custom print preview, an application
 * must connect to the preview signal. The functions
99
 * gtk_print_operation_preview_render_page(),
100 101 102 103 104
 * gtk_print_operation_preview_end_preview() and
 * gtk_print_operation_preview_is_selected()
 * are useful when implementing a print preview.
 */

105 106
#define SHOW_PROGRESS_TIME 1200

107

108
enum
109
{
110
  DONE,
111
  BEGIN_PRINT,
112
  PAGINATE,
113 114 115 116
  REQUEST_PAGE_SETUP,
  DRAW_PAGE,
  END_PRINT,
  STATUS_CHANGED,
117 118
  CREATE_CUSTOM_WIDGET,
  CUSTOM_WIDGET_APPLY,
119
  PREVIEW,
120
  UPDATE_CUSTOM_WIDGET,
121 122 123
  LAST_SIGNAL
};

124 125
enum 
{
126 127 128 129
  PROP_0,
  PROP_DEFAULT_PAGE_SETUP,
  PROP_PRINT_SETTINGS,
  PROP_JOB_NAME,
Alexander Larsson's avatar
Alexander Larsson committed
130
  PROP_N_PAGES,
131 132
  PROP_CURRENT_PAGE,
  PROP_USE_FULL_PAGE,
133
  PROP_TRACK_PRINT_STATUS,
134
  PROP_UNIT,
135
  PROP_SHOW_PROGRESS,
136
  PROP_ALLOW_ASYNC,
137
  PROP_EXPORT_FILENAME,
138
  PROP_STATUS,
Matthias Clasen's avatar
Matthias Clasen committed
139
  PROP_STATUS_STRING,
140
  PROP_CUSTOM_TAB_LABEL,
141 142
  PROP_EMBED_PAGE_SETUP,
  PROP_HAS_SELECTION,
143 144
  PROP_SUPPORT_SELECTION,
  PROP_N_PAGES_TO_PRINT
145 146 147 148
};

static guint signals[LAST_SIGNAL] = { 0 };
static int job_nr = 0;
149
typedef struct _PrintPagesData PrintPagesData;
150

151 152 153 154 155 156 157
static void          preview_iface_init      (GtkPrintOperationPreviewIface *iface);
static GtkPageSetup *create_page_setup       (GtkPrintOperation             *op);
static void          common_render_page      (GtkPrintOperation             *op,
					      gint                           page_nr);
static void          increment_page_sequence (PrintPagesData *data);
static void          prepare_data            (PrintPagesData *data);
static void          clamp_page_ranges       (PrintPagesData *data);
158 159 160 161 162


G_DEFINE_TYPE_WITH_CODE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT,
			 G_IMPLEMENT_INTERFACE (GTK_TYPE_PRINT_OPERATION_PREVIEW,
						preview_iface_init))
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185

/**
 * gtk_print_error_quark:
 *
 * Registers an error quark for #GtkPrintOperation if necessary.
 * 
 * Return value: The error quark used for #GtkPrintOperation errors.
 *
 * Since: 2.10
 **/
GQuark     
gtk_print_error_quark (void)
{
  static GQuark quark = 0;
  if (quark == 0)
    quark = g_quark_from_static_string ("gtk-print-error-quark");
  return quark;
}
     
static void
gtk_print_operation_finalize (GObject *object)
{
  GtkPrintOperation *print_operation = GTK_PRINT_OPERATION (object);
186
  GtkPrintOperationPrivate *priv = print_operation->priv;
187

188 189
  if (priv->free_platform_data &&
      priv->platform_data)
190
    {
191 192
      priv->free_platform_data (priv->platform_data);
      priv->free_platform_data = NULL;
193 194
    }

195 196
  if (priv->default_page_setup)
    g_object_unref (priv->default_page_setup);
197
  
198 199
  if (priv->print_settings)
    g_object_unref (priv->print_settings);
200
  
201 202 203
  if (priv->print_context)
    g_object_unref (priv->print_context);

204
  g_free (priv->export_filename);
205
  g_free (priv->job_name);
Matthias Clasen's avatar
Matthias Clasen committed
206
  g_free (priv->custom_tab_label);
Matthias Clasen's avatar
Matthias Clasen committed
207
  g_free (priv->status_string);
208

209 210 211
  if (priv->print_pages_idle_id > 0)
    g_source_remove (priv->print_pages_idle_id);

212 213 214
  if (priv->show_progress_timeout_id > 0)
    g_source_remove (priv->show_progress_timeout_id);

215 216 217
  if (priv->error)
    g_error_free (priv->error);
  
218 219 220 221 222 223
  G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
}

static void
gtk_print_operation_init (GtkPrintOperation *operation)
{
224
  GtkPrintOperationPrivate *priv;
225 226
  const char *appname;

227 228 229
  priv = operation->priv = G_TYPE_INSTANCE_GET_PRIVATE (operation,
                                                        GTK_TYPE_PRINT_OPERATION,
                                                        GtkPrintOperationPrivate);
230

231 232 233 234 235
  priv->status = GTK_PRINT_STATUS_INITIAL;
  priv->status_string = g_strdup ("");
  priv->default_page_setup = NULL;
  priv->print_settings = NULL;
  priv->nr_of_pages = -1;
236 237
  priv->nr_of_pages_to_print = -1;
  priv->page_position = -1;
238 239
  priv->current_page = -1;
  priv->use_full_page = FALSE;
240
  priv->show_progress = FALSE;
241
  priv->export_filename = NULL;
242
  priv->track_print_status = FALSE;
243
  priv->is_sync = FALSE;
244 245
  priv->support_selection = FALSE;
  priv->has_selection = FALSE;
246
  priv->embed_page_setup = FALSE;
247

248 249
  priv->page_drawing_state = GTK_PAGE_DRAWING_STATE_READY;

250
  priv->rloop = NULL;
251
  priv->unit = GTK_UNIT_NONE;
252 253

  appname = g_get_application_name ();
254 255
  if (appname == NULL)
    appname = "";
256 257 258 259 260
  /* translators: this string is the default job title for print
   * jobs. %s gets replaced by the application name, %d gets replaced
   * by the job number.
   */
  priv->job_name = g_strdup_printf (_("%s job #%d"), appname, ++job_nr);
261 262
}

263 264
static void
preview_iface_render_page (GtkPrintOperationPreview *preview,
265
			   gint                      page_nr)
266
{
267

268 269 270 271 272 273 274 275 276 277
  GtkPrintOperation *op;

  op = GTK_PRINT_OPERATION (preview);
  common_render_page (op, page_nr);
}

static void
preview_iface_end_preview (GtkPrintOperationPreview *preview)
{
  GtkPrintOperation *op;
278
  GtkPrintOperationResult result;
279 280 281 282 283 284 285
  
  op = GTK_PRINT_OPERATION (preview);

  g_signal_emit (op, signals[END_PRINT], 0, op->priv->print_context);

  if (op->priv->rloop)
    g_main_loop_quit (op->priv->rloop);
286 287 288
  
  if (op->priv->end_run)
    op->priv->end_run (op, op->priv->is_sync, TRUE);
289
  
290 291
  _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED, NULL);

292 293 294 295 296 297 298 299
  if (op->priv->error)
    result = GTK_PRINT_OPERATION_RESULT_ERROR;
  else if (op->priv->cancelled)
    result = GTK_PRINT_OPERATION_RESULT_CANCEL;
  else
    result = GTK_PRINT_OPERATION_RESULT_APPLY;

  g_signal_emit (op, signals[DONE], 0, result);
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314
}

static gboolean
preview_iface_is_selected (GtkPrintOperationPreview *preview,
			   gint                      page_nr)
{
  GtkPrintOperation *op;
  GtkPrintOperationPrivate *priv;
  int i;
  
  op = GTK_PRINT_OPERATION (preview);
  priv = op->priv;
  
  switch (priv->print_pages)
    {
315
    case GTK_PRINT_PAGES_SELECTION:
316 317 318 319 320 321 322 323
    case GTK_PRINT_PAGES_ALL:
      return (page_nr >= 0) && (page_nr < priv->nr_of_pages);
    case GTK_PRINT_PAGES_CURRENT:
      return page_nr == priv->current_page;
    case GTK_PRINT_PAGES_RANGES:
      for (i = 0; i < priv->num_page_ranges; i++)
	{
	  if (page_nr >= priv->page_ranges[i].start &&
324
	      (page_nr <= priv->page_ranges[i].end || priv->page_ranges[i].end == -1))
325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344
	    return TRUE;
	}
      return FALSE;
    }
  return FALSE;
}

static void
preview_iface_init (GtkPrintOperationPreviewIface *iface)
{
  iface->render_page = preview_iface_render_page;
  iface->end_preview = preview_iface_end_preview;
  iface->is_selected = preview_iface_is_selected;
}

static void
preview_start_page (GtkPrintOperation *op,
		    GtkPrintContext   *print_context,
		    GtkPageSetup      *page_setup)
{
345 346 347
  if ((op->priv->manual_number_up < 2) ||
      (op->priv->page_position % op->priv->manual_number_up == 0))
    g_signal_emit_by_name (op, "got-page-size", print_context, page_setup);
348 349 350 351 352 353
}

static void
preview_end_page (GtkPrintOperation *op,
		  GtkPrintContext   *print_context)
{
354 355 356 357 358 359 360 361
  cairo_t *cr;

  cr = gtk_print_context_get_cairo_context (print_context);

  if ((op->priv->manual_number_up < 2) ||
      ((op->priv->page_position + 1) % op->priv->manual_number_up == 0) ||
      (op->priv->page_position == op->priv->nr_of_pages_to_print - 1))
    cairo_show_page (cr);
362 363 364 365 366 367 368 369 370 371 372 373
}

static void
preview_end_run (GtkPrintOperation *op,
		 gboolean           wait,
		 gboolean           cancelled)
{
  g_free (op->priv->page_ranges);
  op->priv->page_ranges = NULL;
}


374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
static void
gtk_print_operation_set_property (GObject      *object,
				  guint         prop_id,
				  const GValue *value,
				  GParamSpec   *pspec)
{
  GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
  
  switch (prop_id)
    {
    case PROP_DEFAULT_PAGE_SETUP:
      gtk_print_operation_set_default_page_setup (op, g_value_get_object (value));
      break;
    case PROP_PRINT_SETTINGS:
      gtk_print_operation_set_print_settings (op, g_value_get_object (value));
      break;
    case PROP_JOB_NAME:
      gtk_print_operation_set_job_name (op, g_value_get_string (value));
      break;
Alexander Larsson's avatar
Alexander Larsson committed
393 394
    case PROP_N_PAGES:
      gtk_print_operation_set_n_pages (op, g_value_get_int (value));
395 396 397 398 399 400 401
      break;
    case PROP_CURRENT_PAGE:
      gtk_print_operation_set_current_page (op, g_value_get_int (value));
      break;
    case PROP_USE_FULL_PAGE:
      gtk_print_operation_set_use_full_page (op, g_value_get_boolean (value));
      break;
402 403 404
    case PROP_TRACK_PRINT_STATUS:
      gtk_print_operation_set_track_print_status (op, g_value_get_boolean (value));
      break;
405 406 407
    case PROP_UNIT:
      gtk_print_operation_set_unit (op, g_value_get_enum (value));
      break;
408 409
    case PROP_ALLOW_ASYNC:
      gtk_print_operation_set_allow_async (op, g_value_get_boolean (value));
410
      break;
411 412 413
    case PROP_SHOW_PROGRESS:
      gtk_print_operation_set_show_progress (op, g_value_get_boolean (value));
      break;
414 415
    case PROP_EXPORT_FILENAME:
      gtk_print_operation_set_export_filename (op, g_value_get_string (value));
416
      break;
Matthias Clasen's avatar
Matthias Clasen committed
417 418 419
    case PROP_CUSTOM_TAB_LABEL:
      gtk_print_operation_set_custom_tab_label (op, g_value_get_string (value));
      break;
420 421 422
    case PROP_EMBED_PAGE_SETUP:
      gtk_print_operation_set_embed_page_setup (op, g_value_get_boolean (value));
      break;
423 424 425 426 427 428
    case PROP_HAS_SELECTION:
      gtk_print_operation_set_has_selection (op, g_value_get_boolean (value));
      break;
    case PROP_SUPPORT_SELECTION:
      gtk_print_operation_set_support_selection (op, g_value_get_boolean (value));
      break;
429 430 431 432 433 434 435 436 437 438 439 440 441
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

static void
gtk_print_operation_get_property (GObject    *object,
				  guint       prop_id,
				  GValue     *value,
				  GParamSpec *pspec)
{
  GtkPrintOperation *op = GTK_PRINT_OPERATION (object);
442 443
  GtkPrintOperationPrivate *priv = op->priv;

444 445 446
  switch (prop_id)
    {
    case PROP_DEFAULT_PAGE_SETUP:
447
      g_value_set_object (value, priv->default_page_setup);
448 449
      break;
    case PROP_PRINT_SETTINGS:
450
      g_value_set_object (value, priv->print_settings);
451 452
      break;
    case PROP_JOB_NAME:
453
      g_value_set_string (value, priv->job_name);
454
      break;
Alexander Larsson's avatar
Alexander Larsson committed
455
    case PROP_N_PAGES:
456
      g_value_set_int (value, priv->nr_of_pages);
457 458
      break;
    case PROP_CURRENT_PAGE:
459
      g_value_set_int (value, priv->current_page);
460 461
      break;      
    case PROP_USE_FULL_PAGE:
462
      g_value_set_boolean (value, priv->use_full_page);
463
      break;
464 465 466
    case PROP_TRACK_PRINT_STATUS:
      g_value_set_boolean (value, priv->track_print_status);
      break;
467
    case PROP_UNIT:
468
      g_value_set_enum (value, priv->unit);
469
      break;
470 471
    case PROP_ALLOW_ASYNC:
      g_value_set_boolean (value, priv->allow_async);
472
      break;
473 474 475
    case PROP_SHOW_PROGRESS:
      g_value_set_boolean (value, priv->show_progress);
      break;
476 477
    case PROP_EXPORT_FILENAME:
      g_value_set_string (value, priv->export_filename);
478 479
      break;
    case PROP_STATUS:
480
      g_value_set_enum (value, priv->status);
481 482
      break;
    case PROP_STATUS_STRING:
483
      g_value_set_string (value, priv->status_string);
484
      break;
Matthias Clasen's avatar
Matthias Clasen committed
485 486 487
    case PROP_CUSTOM_TAB_LABEL:
      g_value_set_string (value, priv->custom_tab_label);
      break;
488 489 490
    case PROP_EMBED_PAGE_SETUP:
      g_value_set_boolean (value, priv->embed_page_setup);
      break;
491 492 493 494 495 496
    case PROP_HAS_SELECTION:
      g_value_set_boolean (value, priv->has_selection);
      break;
    case PROP_SUPPORT_SELECTION:
      g_value_set_boolean (value, priv->support_selection);
      break;
497 498 499
    case PROP_N_PAGES_TO_PRINT:
      g_value_set_int (value, priv->nr_of_pages_to_print);
      break;
500 501 502 503 504 505
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

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
struct _PrintPagesData
{
  GtkPrintOperation *op;
  gint uncollated_copies;
  gint collated_copies;
  gint uncollated, collated, total;

  gint range, num_ranges;
  GtkPageRange *ranges;
  GtkPageRange one_range;

  gint page;
  gint sheet;
  gint first_position, last_position;
  gint first_sheet;
  gint num_of_sheets;
  gint *pages;

  GtkWidget *progress;
 
  gboolean initialized;
  gboolean is_preview;
  gboolean done;
};

531 532 533 534 535 536 537 538
typedef struct
{
  GtkPrintOperationPreview *preview;
  GtkPrintContext *print_context;
  GtkWindow *parent;
  cairo_surface_t *surface;
  gchar *filename;
  gboolean wait;
539
  PrintPagesData *pages_data;
540 541 542 543 544 545 546 547 548 549
} PreviewOp;

static void
preview_print_idle_done (gpointer data)
{
  GtkPrintOperation *op;
  PreviewOp *pop = (PreviewOp *) data;

  op = GTK_PRINT_OPERATION (pop->preview);

550
  cairo_surface_finish (pop->surface);
551 552 553 554 555 556 557 558 559 560 561 562 563

  if (op->priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED)
    {
      cairo_surface_destroy (pop->surface);
    }
  else
    {
      /* Surface is destroyed in launch_preview */
      _gtk_print_operation_platform_backend_launch_preview (op,
							    pop->surface,
							    pop->parent,
							    pop->filename);
    }
564 565 566 567

  g_free (pop->filename);

  gtk_print_operation_preview_end_preview (pop->preview);
568

569 570 571 572
  g_object_unref (pop->pages_data->op);
  g_free (pop->pages_data->pages);
  g_free (pop->pages_data);

573
  g_object_unref (op);
574 575 576 577 578 579 580 581
  g_free (pop);
}

static gboolean
preview_print_idle (gpointer data)
{
  PreviewOp *pop;
  GtkPrintOperation *op;
582
  GtkPrintOperationPrivate *priv; 
583
  gboolean done = FALSE;
584 585 586

  pop = (PreviewOp *) data;
  op = GTK_PRINT_OPERATION (pop->preview);
587
  priv = op->priv;
588

589 590
  if (priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY)
    {
591 592 593 594 595 596
      if (priv->cancelled)
	{
	  done = TRUE;
          _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
	}
      else if (!pop->pages_data->initialized)
597
        {
598 599
          pop->pages_data->initialized = TRUE;
          prepare_data (pop->pages_data);
600
        }
601
      else
602
        {
603 604 605 606 607 608
          increment_page_sequence (pop->pages_data);

          if (!pop->pages_data->done)
            gtk_print_operation_preview_render_page (pop->preview, pop->pages_data->page);
          else
            done = priv->page_drawing_state == GTK_PAGE_DRAWING_STATE_READY;
609 610
        }
    }
611

612
  return !done;
613 614 615 616 617 618
}

static void
preview_got_page_size (GtkPrintOperationPreview *preview, 
		       GtkPrintContext          *context,
		       GtkPageSetup             *page_setup,
619
		       PreviewOp                *pop)
620 621
{
  GtkPrintOperation *op = GTK_PRINT_OPERATION (preview);
622
  cairo_t *cr;
623 624

  _gtk_print_operation_platform_backend_resize_preview_surface (op, page_setup, pop->surface);
625 626

  cr = gtk_print_context_get_cairo_context (pop->print_context);
627
  _gtk_print_operation_platform_backend_preview_start_page (op, pop->surface, cr);
628

629 630 631 632
}

static void
preview_ready (GtkPrintOperationPreview *preview,
633 634
               GtkPrintContext          *context,
	       PreviewOp                *pop)
635 636 637
{
  pop->print_context = context;

638 639
  g_object_ref (preview);
      
640 641 642 643
  gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE + 10,
	                     preview_print_idle,
		             pop,
		             preview_print_idle_done);
644 645
}

646

647
static gboolean
648
gtk_print_operation_preview_handler (GtkPrintOperation        *op,
649 650 651 652 653 654 655 656 657 658
                                     GtkPrintOperationPreview *preview, 
				     GtkPrintContext          *context,
				     GtkWindow                *parent)
{
  gdouble dpi_x, dpi_y;
  PreviewOp *pop;
  GtkPageSetup *page_setup;
  cairo_t *cr;

  pop = g_new0 (PreviewOp, 1);
659
  pop->filename = NULL;
660 661
  pop->preview = preview;
  pop->parent = parent;
662 663 664
  pop->pages_data = g_new0 (PrintPagesData, 1);
  pop->pages_data->op = g_object_ref (GTK_PRINT_OPERATION (preview));
  pop->pages_data->is_preview = TRUE;
665 666 667 668 669 670 671

  page_setup = gtk_print_context_get_page_setup (context);

  pop->surface =
    _gtk_print_operation_platform_backend_create_preview_surface (op,
								  page_setup,
								  &dpi_x, &dpi_y,
672
								  &pop->filename);
673

674 675 676 677 678 679
  if (pop->surface == NULL)
    {
      g_free (pop);
      return FALSE;
    }

680 681 682 683 684 685 686 687 688 689 690
  cr = cairo_create (pop->surface);
  gtk_print_context_set_cairo_context (op->priv->print_context, cr,
				       dpi_x, dpi_y);
  cairo_destroy (cr);

  g_signal_connect (preview, "ready", (GCallback) preview_ready, pop);
  g_signal_connect (preview, "got-page-size", (GCallback) preview_got_page_size, pop);
  
  return TRUE;
}

691 692 693 694 695 696
static GtkWidget *
gtk_print_operation_create_custom_widget (GtkPrintOperation *operation)
{
  return NULL;
}

697
static void
698 699
gtk_print_operation_done (GtkPrintOperation       *operation,
                          GtkPrintOperationResult  result)
700 701 702 703 704 705 706 707 708 709
{
  GtkPrintOperationPrivate *priv = operation->priv;

  if (priv->print_context)
    {
      g_object_unref (priv->print_context);
      priv->print_context = NULL;
    } 
}

710 711 712 713 714 715 716 717 718
static gboolean
custom_widget_accumulator (GSignalInvocationHint *ihint,
			   GValue                *return_accu,
			   const GValue          *handler_return,
			   gpointer               dummy)
{
  gboolean continue_emission;
  GtkWidget *widget;
  
719
  widget = g_value_get_object (handler_return);
720
  if (widget != NULL)
721
    g_value_set_object (return_accu, widget);
722 723 724 725 726
  continue_emission = (widget == NULL);
  
  return continue_emission;
}

727 728 729 730 731 732 733 734
static void
gtk_print_operation_class_init (GtkPrintOperationClass *class)
{
  GObjectClass *gobject_class = (GObjectClass *)class;

  gobject_class->set_property = gtk_print_operation_set_property;
  gobject_class->get_property = gtk_print_operation_get_property;
  gobject_class->finalize = gtk_print_operation_finalize;
735 736
 
  class->preview = gtk_print_operation_preview_handler; 
737
  class->create_custom_widget = gtk_print_operation_create_custom_widget;
738
  class->done = gtk_print_operation_done;
739 740 741
  
  g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));

742 743 744 745 746
  /**
   * GtkPrintOperation::done:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @result: the result of the print operation
   *
747
   * Emitted when the print operation run has finished doing
Matthias Clasen's avatar
Matthias Clasen committed
748 749 750 751
   * everything required for printing. 
   *
   * @result gives you information about what happened during the run. 
   * If @result is %GTK_PRINT_OPERATION_RESULT_ERROR then you can call
752 753
   * gtk_print_operation_get_error() for more information.
   *
754 755
   * If you enabled print status tracking then 
   * gtk_print_operation_is_finished() may still return %FALSE 
Matthias Clasen's avatar
Matthias Clasen committed
756
   * after #GtkPrintOperation::done was emitted.
757 758 759 760 761 762 763 764 765
   *
   * Since: 2.10
   */
  signals[DONE] =
    g_signal_new (I_("done"),
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, done),
		  NULL, NULL,
766 767
		  g_cclosure_marshal_VOID__ENUM,
		  G_TYPE_NONE, 1, GTK_TYPE_PRINT_OPERATION_RESULT);
768

769 770 771 772 773
  /**
   * GtkPrintOperation::begin-print:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
   *
774
   * Emitted after the user has finished changing print settings
775 776
   * in the dialog, before the actual rendering starts. 
   *
Matthias Clasen's avatar
Matthias Clasen committed
777
   * A typical use for ::begin-print is to use the parameters from the
778
   * #GtkPrintContext and paginate the document accordingly, and then
Alexander Larsson's avatar
Alexander Larsson committed
779
   * set the number of pages with gtk_print_operation_set_n_pages().
780 781 782 783
   *
   * Since: 2.10
   */
  signals[BEGIN_PRINT] =
Matthias Clasen's avatar
Matthias Clasen committed
784
    g_signal_new (I_("begin-print"),
785 786 787 788 789 790 791
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, begin_print),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);

792
   /**
793
   * GtkPrintOperation::paginate:
794 795 796
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
   *
Matthias Clasen's avatar
Matthias Clasen committed
797
   * Emitted after the #GtkPrintOperation::begin-print signal, but before 
Matthias Clasen's avatar
Matthias Clasen committed
798 799
   * the actual rendering starts. It keeps getting emitted until a connected 
   * signal handler returns %TRUE.
800
   *
Matthias Clasen's avatar
Matthias Clasen committed
801
   * The ::paginate signal is intended to be used for paginating a document
802 803 804 805 806 807
   * in small chunks, to avoid blocking the user interface for a long
   * time. The signal handler should update the number of pages using
   * gtk_print_operation_set_n_pages(), and return %TRUE if the document
   * has been completely paginated.
   *
   * If you don't need to do pagination in chunks, you can simply do
Matthias Clasen's avatar
Matthias Clasen committed
808
   * it all in the ::begin-print handler, and set the number of pages
809 810
   * from there.
   *
811 812
   * Return value: %TRUE if pagination is complete
   *
813 814 815 816 817 818 819 820 821 822 823 824
   * Since: 2.10
   */
  signals[PAGINATE] =
    g_signal_new (I_("paginate"),
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, paginate),
		  _gtk_boolean_handled_accumulator, NULL,
		  _gtk_marshal_BOOLEAN__OBJECT,
		  G_TYPE_BOOLEAN, 1, GTK_TYPE_PRINT_CONTEXT);


825 826 827 828
  /**
   * GtkPrintOperation::request-page-setup:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
829
   * @page_nr: the number of the currently printed page (0-based)
830 831
   * @setup: the #GtkPageSetup 
   * 
832
   * Emitted once for every page that is printed, to give
833 834 835 836 837 838
   * the application a chance to modify the page setup. Any changes 
   * done to @setup will be in force only for printing this page.
   *
   * Since: 2.10
   */
  signals[REQUEST_PAGE_SETUP] =
Matthias Clasen's avatar
Matthias Clasen committed
839
    g_signal_new (I_("request-page-setup"),
840 841 842 843 844 845 846 847 848 849 850 851 852 853
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, request_page_setup),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT_INT_OBJECT,
		  G_TYPE_NONE, 3,
		  GTK_TYPE_PRINT_CONTEXT,
		  G_TYPE_INT,
		  GTK_TYPE_PAGE_SETUP);

  /**
   * GtkPrintOperation::draw-page:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
854
   * @page_nr: the number of the currently printed page (0-based)
855
   *
856
   * Emitted for every page that is printed. The signal handler
857
   * must render the @page_nr's page onto the cairo context obtained
858
   * from @context using gtk_print_context_get_cairo_context().
Matthias Clasen's avatar
Matthias Clasen committed
859
   * |[
Matthias Clasen's avatar
Matthias Clasen committed
860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886
   * static void
   * draw_page (GtkPrintOperation *operation,
   *            GtkPrintContext   *context,
   *            gint               page_nr,
   *            gpointer           user_data)
   * {
   *   cairo_t *cr;
   *   PangoLayout *layout;
   *   gdouble width, text_height;
   *   gint layout_height;
   *   PangoFontDescription *desc;
   *   
   *   cr = gtk_print_context_get_cairo_context (context);
   *   width = gtk_print_context_get_width (context);
   *   
   *   cairo_rectangle (cr, 0, 0, width, HEADER_HEIGHT);
   *   
   *   cairo_set_source_rgb (cr, 0.8, 0.8, 0.8);
   *   cairo_fill (cr);
   *   
   *   layout = gtk_print_context_create_pango_layout (context);
   *   
   *   desc = pango_font_description_from_string ("sans 14");
   *   pango_layout_set_font_description (layout, desc);
   *   pango_font_description_free (desc);
   *   
   *   pango_layout_set_text (layout, "some text", -1);
887
   *   pango_layout_set_width (layout, width * PANGO_SCALE);
Matthias Clasen's avatar
Matthias Clasen committed
888
   *   pango_layout_set_alignment (layout, PANGO_ALIGN_CENTER);
Matthias Clasen's avatar
Matthias Clasen committed
889
   *      		      
Matthias Clasen's avatar
Matthias Clasen committed
890
   *   pango_layout_get_size (layout, NULL, &layout_height);
Matthias Clasen's avatar
Matthias Clasen committed
891 892 893 894
   *   text_height = (gdouble)layout_height / PANGO_SCALE;
   *   
   *   cairo_move_to (cr, width / 2,  (HEADER_HEIGHT - text_height) / 2);
   *   pango_cairo_show_layout (cr, layout);
Matthias Clasen's avatar
Matthias Clasen committed
895
   *   
Matthias Clasen's avatar
Matthias Clasen committed
896 897
   *   g_object_unref (layout);
   * }
Matthias Clasen's avatar
Matthias Clasen committed
898
   * ]|
899
   *
Matthias Clasen's avatar
Matthias Clasen committed
900 901 902 903 904
   * Use gtk_print_operation_set_use_full_page() and 
   * gtk_print_operation_set_unit() before starting the print operation
   * to set up the transformation of the cairo context according to your
   * needs.
   * 
905 906 907
   * Since: 2.10
   */
  signals[DRAW_PAGE] =
Matthias Clasen's avatar
Matthias Clasen committed
908
    g_signal_new (I_("draw-page"),
909 910 911 912 913 914 915 916 917 918 919 920 921 922
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, draw_page),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT_INT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_PRINT_CONTEXT,
		  G_TYPE_INT);

  /**
   * GtkPrintOperation::end-print:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
   *
923
   * Emitted after all pages have been rendered. 
924
   * A handler for this signal can clean up any resources that have
Matthias Clasen's avatar
Matthias Clasen committed
925
   * been allocated in the #GtkPrintOperation::begin-print handler.
926 927 928 929
   * 
   * Since: 2.10
   */
  signals[END_PRINT] =
Matthias Clasen's avatar
Matthias Clasen committed
930
    g_signal_new (I_("end-print"),
931 932 933 934 935 936 937 938 939 940 941
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, end_print),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GTK_TYPE_PRINT_CONTEXT);

  /**
   * GtkPrintOperation::status-changed:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   *
942
   * Emitted at between the various phases of the print operation.
943 944 945 946 947 948 949
   * See #GtkPrintStatus for the phases that are being discriminated.
   * Use gtk_print_operation_get_status() to find out the current
   * status.
   *
   * Since: 2.10
   */
  signals[STATUS_CHANGED] =
Matthias Clasen's avatar
Matthias Clasen committed
950
    g_signal_new (I_("status-changed"),
951 952 953 954 955 956 957 958 959
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, status_changed),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__VOID,
		  G_TYPE_NONE, 0);


  /**
Matthias Clasen's avatar
Matthias Clasen committed
960
   * GtkPrintOperation::create-custom-widget:
961 962
   * @operation: the #GtkPrintOperation on which the signal was emitted
   *
963
   * Emitted when displaying the print dialog. If you return a
964 965 966 967
   * widget in a handler for this signal it will be added to a custom
   * tab in the print dialog. You typically return a container widget
   * with multiple widgets in it.
   *
Matthias Clasen's avatar
Matthias Clasen committed
968 969
   * The print dialog owns the returned widget, and its lifetime is not 
   * controlled by the application. However, the widget is guaranteed 
Matthias Clasen's avatar
Matthias Clasen committed
970 971 972
   * to stay around until the #GtkPrintOperation::custom-widget-apply 
   * signal is emitted on the operation. Then you can read out any 
   * information you need from the widgets.
973
   *
974 975
   * Returns: (transfer none): A custom widget that gets embedded in
   *          the print dialog, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
976
   *
977 978 979 980 981 982 983 984
   * Since: 2.10
   */
  signals[CREATE_CUSTOM_WIDGET] =
    g_signal_new (I_("create-custom-widget"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, create_custom_widget),
		  custom_widget_accumulator, NULL,
985 986
		  _gtk_marshal_OBJECT__VOID,
		  G_TYPE_OBJECT, 0);
987

988 989 990 991 992 993 994
  /**
   * GtkPrintOperation::update-custom-widget:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @widget: the custom widget added in create-custom-widget
   * @setup: actual page setup
   * @settings: actual print settings
   *
995
   * Emitted after change of selected printer. The actual page setup and
996
   * print settings are passed to the custom widget, which can actualize
997
   * itself according to this change.
998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009
   *
   * Since: 2.18
   */
  signals[UPDATE_CUSTOM_WIDGET] =
    g_signal_new (I_("update-custom-widget"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, update_custom_widget),
		  NULL, NULL,
		  _gtk_marshal_VOID__OBJECT_OBJECT_OBJECT,
		  G_TYPE_NONE, 3, GTK_TYPE_WIDGET, GTK_TYPE_PAGE_SETUP, GTK_TYPE_PRINT_SETTINGS);

1010 1011 1012 1013 1014
  /**
   * GtkPrintOperation::custom-widget-apply:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @widget: the custom widget added in create-custom-widget
   *
Matthias Clasen's avatar
Matthias Clasen committed
1015
   * Emitted right before #GtkPrintOperation::begin-print if you added
1016
   * a custom widget in the #GtkPrintOperation::create-custom-widget handler. 
Matthias Clasen's avatar
Matthias Clasen committed
1017 1018 1019
   * When you get this signal you should read the information from the 
   * custom widgets, as the widgets are not guaraneed to be around at a 
   * later time.
1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
   *
   * Since: 2.10
   */
  signals[CUSTOM_WIDGET_APPLY] =
    g_signal_new (I_("custom-widget-apply"),
		  G_TYPE_FROM_CLASS (class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, custom_widget_apply),
		  NULL, NULL,
		  g_cclosure_marshal_VOID__OBJECT,
		  G_TYPE_NONE, 1, GTK_TYPE_WIDGET);
1031

1032 1033 1034
   /**
   * GtkPrintOperation::preview:
   * @operation: the #GtkPrintOperation on which the signal was emitted
1035
   * @preview: the #GtkPrintOperationPreview for the current operation
1036
   * @context: the #GtkPrintContext that will be used
1037
   * @parent: (allow-none): the #GtkWindow to use as window parent, or %NULL
1038 1039 1040
   *
   * Gets emitted when a preview is requested from the native dialog.
   *
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054
   * The default handler for this signal uses an external viewer 
   * application to preview.
   *
   * To implement a custom print preview, an application must return
   * %TRUE from its handler for this signal. In order to use the
   * provided @context for the preview implementation, it must be
   * given a suitable cairo context with gtk_print_context_set_cairo_context().
   * 
   * The custom preview implementation can use 
   * gtk_print_operation_preview_is_selected() and 
   * gtk_print_operation_preview_render_page() to find pages which
   * are selected for print and render them. The preview must be
   * finished by calling gtk_print_operation_preview_end_preview()
   * (typically in response to the user clicking a close button).
1055
   *
1056
   * Returns: %TRUE if the listener wants to take over control of the preview
1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072
   * 
   * Since: 2.10
   */
  signals[PREVIEW] =
    g_signal_new (I_("preview"),
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GtkPrintOperationClass, preview),
		  _gtk_boolean_handled_accumulator, NULL,
		  _gtk_marshal_BOOLEAN__OBJECT_OBJECT_OBJECT,
		  G_TYPE_BOOLEAN, 3,
		  GTK_TYPE_PRINT_OPERATION_PREVIEW,
		  GTK_TYPE_PRINT_CONTEXT,
		  GTK_TYPE_WINDOW);

  
Matthias Clasen's avatar
Matthias Clasen committed
1073 1074 1075 1076 1077 1078 1079
  /**
   * GtkPrintOperation:default-page-setup:
   *
   * The #GtkPageSetup used by default.
   * 
   * This page setup will be used by gtk_print_operation_run(),
   * but it can be overridden on a per-page basis by connecting
Matthias Clasen's avatar
Matthias Clasen committed
1080
   * to the #GtkPrintOperation::request-page-setup signal.
Matthias Clasen's avatar
Matthias Clasen committed
1081 1082 1083
   *
   * Since: 2.10
   */
1084 1085 1086 1087 1088 1089 1090 1091
  g_object_class_install_property (gobject_class,
				   PROP_DEFAULT_PAGE_SETUP,
				   g_param_spec_object ("default-page-setup",
							P_("Default Page Setup"),
							P_("The GtkPageSetup used by default"),
							GTK_TYPE_PAGE_SETUP,
							GTK_PARAM_READWRITE));

Matthias Clasen's avatar
Matthias Clasen committed
1092 1093 1094 1095 1096
  /**
   * GtkPrintOperation:print-settings:
   *
   * The #GtkPrintSettings used for initializing the dialog.
   *
Matthias Clasen's avatar
Matthias Clasen committed
1097 1098 1099
   * Setting this property is typically used to re-establish 
   * print settings from a previous print operation, see 
   * gtk_print_operation_run().
Matthias Clasen's avatar
Matthias Clasen committed
1100 1101 1102
   *
   * Since: 2.10
   */
1103 1104 1105 1106 1107 1108 1109
  g_object_class_install_property (gobject_class,
				   PROP_PRINT_SETTINGS,
				   g_param_spec_object ("print-settings",
							P_("Print Settings"),
							P_("The GtkPrintSettings used for initializing the dialog"),
							GTK_TYPE_PRINT_SETTINGS,
							GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1110 1111 1112 1113
  
  /**
   * GtkPrintOperation:job-name:
   *
Matthias Clasen's avatar
Matthias Clasen committed
1114 1115
   * A string used to identify the job (e.g. in monitoring 
   * applications like eggcups). 
Matthias Clasen's avatar
Matthias Clasen committed
1116
   * 
Matthias Clasen's avatar
Matthias Clasen committed
1117 1118
   * If you don't set a job name, GTK+ picks a default one 
   * by numbering successive print jobs.
Matthias Clasen's avatar
Matthias Clasen committed
1119 1120 1121
   *
   * Since: 2.10
   */
1122 1123 1124 1125 1126 1127 1128
  g_object_class_install_property (gobject_class,
				   PROP_JOB_NAME,
				   g_param_spec_string ("job-name",
							P_("Job Name"),
							P_("A string used for identifying the print job."),
							"",
							GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1129 1130

  /**
Alexander Larsson's avatar
Alexander Larsson committed
1131
   * GtkPrintOperation:n-pages:
Matthias Clasen's avatar
Matthias Clasen committed
1132 1133 1134 1135
   *
   * The number of pages in the document. 
   *
   * This <emphasis>must</emphasis> be set to a positive number
Matthias Clasen's avatar
Matthias Clasen committed
1136 1137
   * before the rendering starts. It may be set in a 
   * #GtkPrintOperation::begin-print signal hander.
Matthias Clasen's avatar
Matthias Clasen committed
1138
   *
Matthias Clasen's avatar
Matthias Clasen committed
1139 1140 1141 1142 1143
   * Note that the page numbers passed to the 
   * #GtkPrintOperation::request-page-setup and 
   * #GtkPrintOperation::draw-page signals are 0-based, i.e. if 
   * the user chooses to print all pages, the last ::draw-page signal 
   * will be for page @n_pages - 1.
Matthias Clasen's avatar
Matthias Clasen committed
1144 1145 1146
   *
   * Since: 2.10
   */
1147
  g_object_class_install_property (gobject_class,
Alexander Larsson's avatar
Alexander Larsson committed
1148 1149
				   PROP_N_PAGES,
				   g_param_spec_int ("n-pages",
1150 1151 1152 1153 1154 1155
						     P_("Number of Pages"),
						     P_("The number of pages in the document."),
						     -1,
						     G_MAXINT,
						     -1,
						     GTK_PARAM_READWRITE));
Matthias Clasen's avatar
Matthias Clasen committed
1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168

  /**
   * GtkPrintOperation:current-page:
   *
   * The current page in the document.
   *
   * If this is set before gtk_print_operation_run(), 
   * the user will be able to select to print only the current page.
   *
   * Note that this only makes sense for pre-paginated documents.
   *
   * Since: 2.10
   */
1169 1170 1171 1172
  g_object_class_install_property (gobject_class,
				   PROP_CURRENT_PAGE,
				   g_param_spec_int ("current-page",
						     P_("Current Page"),
1173
						     P_("The current page in the document"),
1174 1175 1176 1177 1178
						     -1,
						     G_MAXINT,
						     -1,
						     GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
1179 1180 1181
  /**
   * GtkPrintOperation:use-full-page:
   *
Matthias Clasen's avatar
Matthias Clasen committed
1182 1183 1184 1185 1186 1187
   * If %TRUE, the transformation for the cairo context obtained 
   * from #GtkPrintContext puts the origin at the top left corner 
   * of the page (which may not be the top left corner of the sheet, 
   * depending on page orientation and the number of pages per sheet). 
   * Otherwise, the origin is at the top left corner of the imageable 
   * area (i.e. inside the margins).
Matthias Clasen's avatar
Matthias Clasen committed
1188 1189 1190
   * 
   * Since: 2.10
   */
1191 1192 1193 1194
  g_object_class_install_property (gobject_class,
				   PROP_USE_FULL_PAGE,
				   g_param_spec_boolean ("use-full-page",
							 P_("Use full page"),
Matthias Clasen's avatar
Matthias Clasen committed
1195
							 P_("TRUE if the origin of the context should be at the corner of the page and not the corner of the imageable area"),
1196 1197 1198 1199
							 FALSE,
							 GTK_PARAM_READWRITE));
  

1200 1201 1202
  /**
   * GtkPrintOperation:track-print-status:
   *
Matthias Clasen's avatar
Matthias Clasen committed
1203 1204 1205 1206 1207 1208
   * If %TRUE, the print operation will try to continue report on 
   * the status of the print job in the printer queues and printer. 
   * This can allow your application to show things like "out of paper" 
   * issues, and when the print job actually reaches the printer. 
   * However, this is often implemented using polling, and should 
   * not be enabled unless needed.
1209 1210 1211 1212
   * 
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
Matthias Clasen's avatar
Matthias Clasen committed
1213
				   PROP_TRACK_PRINT_STATUS,
1214 1215
				   g_param_spec_boolean ("track-print-status",
							 P_("Track Print Status"),
Matthias Clasen's avatar
Matthias Clasen committed
1216
							 P_("TRUE if the print operation will continue to report on the print job status after the print data has been sent to the printer or print server."),
1217 1218 1219 1220
							 FALSE,
							 GTK_PARAM_READWRITE));
  

Matthias Clasen's avatar
Matthias Clasen committed
1221 1222 1223 1224
  /**
   * GtkPrintOperation:unit:
   *
   * The transformation for the cairo context obtained from
Matthias Clasen's avatar
Matthias Clasen committed
1225 1226
   * #GtkPrintContext is set up in such a way that distances 
   * are measured in units of @unit.
Matthias Clasen's avatar
Matthias Clasen committed
1227 1228 1229
   *
   * Since: 2.10
   */
1230 1231 1232 1233
  g_object_class_install_property (gobject_class,
				   PROP_UNIT,
				   g_param_spec_enum ("unit",
						      P_("Unit"),
Matthias Clasen's avatar
Matthias Clasen committed
1234
						      P_("The unit in which distances can be measured in the context"),
1235
						      GTK_TYPE_UNIT,
1236
						      GTK_UNIT_NONE,
1237 1238
						      GTK_PARAM_READWRITE));
  
1239
  
Matthias Clasen's avatar
Matthias Clasen committed
1240
  /**
1241
   * GtkPrintOperation:show-progress:
Matthias Clasen's avatar
Matthias Clasen committed
1242
   *
1243 1244
   * Determines whether to show a progress dialog during the 
   * print operation.
Matthias Clasen's avatar
Matthias Clasen committed
1245 1246 1247
   *
   * Since: 2.10
   */
1248
  g_object_class_install_property (gobject_class,
1249 1250
				   PROP_SHOW_PROGRESS,
				   g_param_spec_boolean ("show-progress",
1251
							 P_("Show Dialog"),
1252 1253
							 P_("TRUE if a progress dialog is shown while printing."),
							 FALSE,
1254
							 GTK_PARAM_READWRITE));
1255

1256
  /**
1257
   * GtkPrintOperation:allow-async:
1258
   *
1259
   * Determines whether the print operation may run asynchronously or not.
Matthias Clasen's avatar
Matthias Clasen committed
1260
   *
1261
   * Some systems don't support asynchronous printing, but those that do
Matthias Clasen's avatar
Matthias Clasen committed
1262
   * will return %GTK_PRINT_OPERATION_RESULT_IN_PROGRESS as the status, and
Matthias Clasen's avatar
Matthias Clasen committed
1263 1264
   * emit the #GtkPrintOperation::done signal when the operation is actually 
   * done.
1265
   *
Matthias Clasen's avatar
Matthias Clasen committed
1266 1267 1268
   * The Windows port does not support asynchronous operation at all (this 
   * is unlikely to change). On other platforms, all actions except for 
   * %GTK_PRINT_OPERATION_ACTION_EXPORT support asynchronous operation.
Matthias Clasen's avatar
Matthias Clasen committed
1269
   *
1270 1271 1272
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
1273 1274 1275 1276
				   PROP_ALLOW_ASYNC,
				   g_param_spec_boolean ("allow-async",
							 P_("Allow Async"),
							 P_("TRUE if print process may run asynchronous."),
1277 1278 1279
							 FALSE,
							 GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
1280
  /**
Matthias Clasen's avatar
Matthias Clasen committed
1281
   * GtkPrintOperation:export-filename:
Matthias Clasen's avatar
Matthias Clasen committed
1282
   *
Matthias Clasen's avatar
Matthias Clasen committed
1283 1284
   * The name of a file to generate instead of showing the print dialog. 
   * Currently, PDF is the only supported format.
Matthias Clasen's avatar
Matthias Clasen committed
1285
   *
Matthias Clasen's avatar
Matthias Clasen committed
1286 1287
   * The intended use of this property is for implementing 
   * "Export to PDF" actions.
Matthias Clasen's avatar
Matthias Clasen committed
1288 1289
   *
   * "Print to PDF" support is independent of this and is done
Matthias Clasen's avatar
Matthias Clasen committed
1290 1291
   * by letting the user pick the "Print to PDF" item from the 
   * list of printers in the print dialog.
Matthias Clasen's avatar
Matthias Clasen committed
1292 1293 1294
   *
   * Since: 2.10
   */
1295
  g_object_class_install_property (gobject_class,
Matthias Clasen's avatar
Matthias Clasen committed
1296 1297 1298 1299
				   PROP_EXPORT_FILENAME,
				   g_param_spec_string ("export-filename",
							P_("Export filename"),
							P_("Export filename"),
1300 1301 1302
							NULL,
							GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
1303 1304 1305 1306 1307 1308 1309
  /**
   * GtkPrintOperation:status:
   *
   * The status of the print operation.
   * 
   * Since: 2.10
   */
1310 1311 1312 1313 1314 1315 1316 1317 1318
  g_object_class_install_property (gobject_class,
				   PROP_STATUS,
				   g_param_spec_enum ("status",
						      P_("Status"),
						      P_("The status of the print operation"),
						      GTK_TYPE_PRINT_STATUS,
						      GTK_PRINT_STATUS_INITIAL,
						      GTK_PARAM_READABLE));
  
Matthias Clasen's avatar
Matthias Clasen committed
1319 1320 1321 1322 1323 1324 1325
  /**
   * GtkPrintOperation:status-string:
   *
   * A string representation of the status of the print operation. 
   * The string is translated and suitable for displaying the print 
   * status e.g. in a #GtkStatusbar.
   *
Matthias Clasen's avatar
Matthias Clasen committed
1326 1327
   * See the #GtkPrintOperation:status property for a status value that 
   * is suitable for programmatic use. 
Matthias Clasen's avatar
Matthias Clasen committed
1328 1329 1330
   *
   * Since: 2.10
   */
1331 1332 1333 1334 1335 1336 1337 1338 1339
  g_object_class_install_property (gobject_class,
				   PROP_STATUS_STRING,
				   g_param_spec_string ("status-string",
							P_("Status String"),
							P_("A human-readable description of the status"),
							"",
							GTK_PARAM_READABLE));
  

Matthias Clasen's avatar
Matthias Clasen committed
1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357
  /**
   * GtkPrintOperation:custom-tab-label:
   *
   * Used as the label of the tab containing custom widgets.
   * Note that this property may be ignored on some platforms.
   * 
   * If this is %NULL, GTK+ uses a default label.
   *
   * Since: 2.10
   */
  g_object_class_install_property (gobject_class,
				   PROP_CUSTOM_TAB_LABEL,
				   g_param_spec_string ("custom-tab-label",
							P_("Custom tab label"),
							P_("Label for the tab containing custom widgets."),
							NULL,
							GTK_PARAM_READWRITE));

1358 1359 1360 1361 1362 1363 1364 1365 1366
  /**
   * GtkPrintOperation:support-selection:
   *
   * If %TRUE, the print operation will support print of selection.
   * This allows the print dialog to show a "Selection" button.
   * 
   * Since: 2.18
   */
  g_object_class_install_property (gobject_class,
1367
				   PROP_SUPPORT_SELECTION,
1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383
				   g_param_spec_boolean ("support-selection",
							 P_("Support Selection"),
							 P_("TRUE if the print operation will support print of selection."),
							 FALSE,
							 GTK_PARAM_READWRITE));

  /**
   * GtkPrintOperation:has-selection:
   *
   * Determines whether there is a selection in your application.
   * This can allow your application to print the selection.
   * This is typically used to make a "Selection" button sensitive.
   * 
   * Since: 2.18
   */
  g_object_class_install_property (gobject_class,
1384
				   PROP_HAS_SELECTION,
1385 1386
				   g_param_spec_boolean ("has-selection",
							 P_("Has Selection"),
1387
							 P_("TRUE if a selection exists."),
1388 1389 1390
							 FALSE,
							 GTK_PARAM_READWRITE));

1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402

  /**
   * GtkPrintOperation:embed-page-setup:
   *
   * If %TRUE, page size combo box and orientation combo box are embedded into page setup page.
   * 
   * Since: 2.18
   */
  g_object_class_install_property (gobject_class,
				   PROP_EMBED_PAGE_SETUP,
				   g_param_spec_boolean ("embed-page-setup",
							 P_("Embed Page Setup"),
1403
							 P_("TRUE if page setup combos are embedded in GtkPrintUnixDialog"),
1404 1405
							 FALSE,
							 GTK_PARAM_READWRITE));
1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429
  /**
   * GtkPrintOperation:n-pages-to-print:
   *
   * The number of pages that will be printed.
   *
   * Note that this value is set during print preparation phase
   * (%GTK_PRINT_STATUS_PREPARING), so this value should never be
   * get before the data generation phase (%GTK_PRINT_STATUS_GENERATING_DATA).
   * You can connect to the #GtkPrintOperation::status-changed signal
   * and call gtk_print_operation_get_n_pages_to_print() when
   * print status is %GTK_PRINT_STATUS_GENERATING_DATA.
   * This is typically used to track the progress of print operation.
   *
   * Since: 2.18
   */
  g_object_class_install_property (gobject_class,
				   PROP_N_PAGES_TO_PRINT,
				   g_param_spec_int ("n-pages-to-print",
						     P_("Number of Pages To Print"),
						     P_("The number of pages that will be printed."),
						     -1,
						     G_MAXINT,
						     -1,
						     GTK_PARAM_READABLE));
1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453
}

/**
 * gtk_print_operation_new:
 *
 * Creates a new #GtkPrintOperation. 
 *
 * Returns: a new #GtkPrintOperation
 *
 * Since: 2.10
 */
GtkPrintOperation *
gtk_print_operation_new (void)
{
  GtkPrintOperation *print_operation;

  print_operation = g_object_new (GTK_TYPE_PRINT_OPERATION, NULL);
  
  return print_operation;
}

/**
 * gtk_print_operation_set_default_page_setup:
 * @op: a #GtkPrintOperation
1454 1455
 * @default_page_setup: (allow-none): a #GtkPageSetup, or %NULL
 *
1456 1457 1458 1459
 * Makes @default_page_setup the default page setup for @op.
 *
 * This page setup will be used by gtk_print_operation_run(),
 * but it can be overridden on a per-page basis by connecting
Matthias Clasen's avatar
Matthias Clasen committed
1460
 * to the #GtkPrintOperation::request-page-setup signal.
1461 1462 1463 1464 1465 1466 1467
 *
 * Since: 2.10
 **/
void
gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
					    GtkPageSetup      *default_page_setup)
{
1468 1469
  GtkPrintOperationPrivate *priv;

1470 1471 1472 1473
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (default_page_setup == NULL || 
                    GTK_IS_PAGE_SETUP (default_page_setup));

1474 1475 1476
  priv = op->priv;

  if (default_page_setup != priv->default_page_setup)
1477 1478 1479 1480
    {
      if (default_page_setup)
	g_object_ref (default_page_setup);
      
1481 1482
      if (priv->default_page_setup)
	g_object_unref (priv->default_page_setup);
1483
      
1484
      priv->default_page_setup = default_page_setup;
1485 1486 1487 1488 1489 1490 1491 1492 1493
     
      g_object_notify (G_OBJECT (op), "default-page-setup");
    }
}

/**
 * gtk_print_operation_get_default_page_setup:
 * @op: a #GtkPrintOperation
 *
1494
 * Returns the default page setup, see
1495 1496
 * gtk_print_operation_set_default_page_setup().
 *
1497
 * Returns: (transfer none): the default page setup
1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512
 *
 * Since: 2.10
 */
GtkPageSetup *
gtk_print_operation_get_default_page_setup (GtkPrintOperation *op)
{
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);

  return op->priv->default_page_setup;
}


/**
 * gtk_print_operation_set_print_settings:
 * @op: a #GtkPrintOperation
1513 1514
 * @print_settings: (allow-none): #GtkPrintSettings
 *
1515 1516 1517 1518 1519 1520 1521 1522 1523 1524
 * Sets the print settings for @op. This is typically used to
 * re-establish print settings from a previous print operation,
 * see gtk_print_operation_run().
 *
 * Since: 2.10
 **/
void
gtk_print_operation_set_print_settings (GtkPrintOperation *op,
					GtkPrintSettings  *print_settings)
{
1525 1526
  GtkPrintOperationPrivate *priv;

1527 1528 1529 1530
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (print_settings == NULL || 
                    GTK_IS_PRINT_SETTINGS (print_settings));

1531 1532 1533
  priv = op->priv;

  if (print_settings != priv->print_settings)
1534 1535 1536 1537
    {
      if (print_settings)
        g_object_ref (print_settings);

1538 1539
      if (priv->print_settings)
        g_object_unref (priv->print_settings);
1540
  
1541
      priv->print_settings = print_settings;
1542 1543 1544 1545 1546 1547 1548 1549 1550

      g_object_notify (G_OBJECT (op), "print-settings");
    }
}

/**
 * gtk_print_operation_get_print_settings:
 * @op: a #GtkPrintOperation
 *
1551 1552 1553 1554
 * Returns the current print settings.
 *
 * Note that the return value is %NULL until either
 * gtk_print_operation_set_print_settings() or
1555
 * gtk_print_operation_run() have been called.
1556 1557 1558
 *
 * Return value: (transfer none): the current print settings of @op.
 *
1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585
 * Since: 2.10
 **/
GtkPrintSettings *
gtk_print_operation_get_print_settings (GtkPrintOperation *op)
{
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), NULL);

  return op->priv->print_settings;
}

/**
 * gtk_print_operation_set_job_name:
 * @op: a #GtkPrintOperation
 * @job_name: a string that identifies the print job
 * 
 * Sets the name of the print job. The name is used to identify 
 * the job (e.g. in monitoring applications like eggcups). 
 * 
 * If you don't set a job name, GTK+ picks a default one by 
 * numbering successive print jobs.
 *
 * Since: 2.10
 **/
void
gtk_print_operation_set_job_name (GtkPrintOperation *op,
				  const gchar       *job_name)
{
1586 1587
  GtkPrintOperationPrivate *priv;

1588
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1589
  g_return_if_fail (job_name != NULL);
1590

1591 1592 1593 1594
  priv = op->priv;

  g_free (priv->job_name);
  priv->job_name = g_strdup (job_name);
1595 1596 1597 1598 1599

  g_object_notify (G_OBJECT (op), "job-name");
}

/**
Alexander Larsson's avatar
Alexander Larsson committed
1600
 * gtk_print_operation_set_n_pages:
1601 1602 1603 1604 1605 1606
 * @op: a #GtkPrintOperation
 * @n_pages: the number of pages
 * 
 * Sets the number of pages in the document. 
 *
 * This <emphasis>must</emphasis> be set to a positive number
Matthias Clasen's avatar
Matthias Clasen committed
1607 1608
 * before the rendering starts. It may be set in a 
 * #GtkPrintOperation::begin-print signal hander.
1609
 *
Matthias Clasen's avatar
Matthias Clasen committed
1610 1611 1612 1613 1614
 * Note that the page numbers passed to the 
 * #GtkPrintOperation::request-page-setup 
 * and #GtkPrintOperation::draw-page signals are 0-based, i.e. if 
 * the user chooses to print all pages, the last ::draw-page signal 
 * will be for page @n_pages - 1.
1615 1616 1617 1618
 *
 * Since: 2.10
 **/
void
Alexander Larsson's avatar
Alexander Larsson committed
1619 1620
gtk_print_operation_set_n_pages (GtkPrintOperation *op,
				 gint               n_pages)
1621
{
1622 1623
  GtkPrintOperationPrivate *priv;

1624 1625 1626
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (n_pages > 0);

1627 1628 1629 1630 1631
  priv = op->priv;
  g_return_if_fail (priv->current_page == -1 || 
		    priv->current_page < n_pages);

  if (priv->nr_of_pages != n_pages)
1632
    {
1633
      priv->nr_of_pages = n_pages;
1634

1635
      g_object_notify (G_OBJECT (op), "n-pages");
1636 1637 1638 1639 1640 1641 1642 1643 1644
    }
}

/**
 * gtk_print_operation_set_current_page:
 * @op: a #GtkPrintOperation
 * @current_page: the current page, 0-based
 *
 * Sets the current page.
Matthias Clasen's avatar
Matthias Clasen committed
1645
 *
1646 1647 1648 1649 1650 1651 1652 1653 1654 1655 1656
 * If this is called before gtk_print_operation_run(), 
 * the user will be able to select to print only the current page.
 *
 * Note that this only makes sense for pre-paginated documents.
 * 
 * Since: 2.10
 **/
void
gtk_print_operation_set_current_page (GtkPrintOperation *op,
				      gint               current_page)
{
Christian Persch's avatar