gtkprintoperation.c 38.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
/* GTK - The GIMP Toolkit
 * 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
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"
#include "string.h"
#include "gtkprintoperation-private.h"
#include "gtkmarshalers.h"
#include <cairo-pdf.h>
#include "gtkintl.h"
#include "gtkprivate.h"
#include "gtkalias.h"

#define GTK_PRINT_OPERATION_GET_PRIVATE(obj)(G_TYPE_INSTANCE_GET_PRIVATE ((obj), GTK_TYPE_PRINT_OPERATION, GtkPrintOperationPrivate))

enum {
  BEGIN_PRINT,
  REQUEST_PAGE_SETUP,
  DRAW_PAGE,
  END_PRINT,
  STATUS_CHANGED,
  LAST_SIGNAL
};

enum {
  PROP_0,
  PROP_DEFAULT_PAGE_SETUP,
  PROP_PRINT_SETTINGS,
  PROP_JOB_NAME,
Alexander Larsson's avatar
Alexander Larsson committed
46
  PROP_N_PAGES,
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
  PROP_CURRENT_PAGE,
  PROP_USE_FULL_PAGE,
  PROP_UNIT,
  PROP_SHOW_DIALOG,
  PROP_PDF_TARGET,
  PROP_STATUS,
  PROP_STATUS_STRING
};

static guint signals[LAST_SIGNAL] = { 0 };
static int job_nr = 0;

G_DEFINE_TYPE (GtkPrintOperation, gtk_print_operation, G_TYPE_OBJECT)

/**
 * 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);
83
  GtkPrintOperationPrivate *priv = print_operation->priv;
84

85 86
  if (priv->free_platform_data &&
      priv->platform_data)
87
    {
88 89
      priv->free_platform_data (priv->platform_data);
      priv->free_platform_data = NULL;
90 91
    }

92 93
  if (priv->default_page_setup)
    g_object_unref (priv->default_page_setup);
94
  
95 96
  if (priv->print_settings)
    g_object_unref (priv->print_settings);
97
  
98 99
  g_free (priv->pdf_target);
  g_free (priv->job_name);
100 101 102 103 104 105 106

  G_OBJECT_CLASS (gtk_print_operation_parent_class)->finalize (object);
}

static void
gtk_print_operation_init (GtkPrintOperation *operation)
{
107
  GtkPrintOperationPrivate *priv;
108 109
  const char *appname;

110
  priv = operation->priv = GTK_PRINT_OPERATION_GET_PRIVATE (operation);
111

112 113 114 115 116 117 118 119 120
  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;
  priv->current_page = -1;
  priv->use_full_page = FALSE;
  priv->show_dialog = TRUE;
  priv->pdf_target = NULL;
121

122
  priv->unit = GTK_UNIT_PIXEL;
123 124

  appname = g_get_application_name ();
125
  priv->job_name = g_strdup_printf ("%s job #%d", appname, ++job_nr);
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
}

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
147 148
    case PROP_N_PAGES:
      gtk_print_operation_set_n_pages (op, g_value_get_int (value));
149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
      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;
    case PROP_UNIT:
      gtk_print_operation_set_unit (op, g_value_get_enum (value));
      break;
    case PROP_SHOW_DIALOG:
      gtk_print_operation_set_show_dialog (op, g_value_get_boolean (value));
      break;
    case PROP_PDF_TARGET:
      gtk_print_operation_set_pdf_target (op, g_value_get_string (value));
      break;
    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);
178 179
  GtkPrintOperationPrivate *priv = op->priv;

180 181 182
  switch (prop_id)
    {
    case PROP_DEFAULT_PAGE_SETUP:
183
      g_value_set_object (value, priv->default_page_setup);
184 185
      break;
    case PROP_PRINT_SETTINGS:
186
      g_value_set_object (value, priv->print_settings);
187 188
      break;
    case PROP_JOB_NAME:
189
      g_value_set_string (value, priv->job_name);
190
      break;
Alexander Larsson's avatar
Alexander Larsson committed
191
    case PROP_N_PAGES:
192
      g_value_set_int (value, priv->nr_of_pages);
193 194
      break;
    case PROP_CURRENT_PAGE:
195
      g_value_set_int (value, priv->current_page);
196 197
      break;      
    case PROP_USE_FULL_PAGE:
198
      g_value_set_boolean (value, priv->use_full_page);
199 200
      break;
    case PROP_UNIT:
201
      g_value_set_enum (value, priv->unit);
202 203
      break;
    case PROP_SHOW_DIALOG:
204
      g_value_set_boolean (value, priv->show_dialog);
205 206
      break;
    case PROP_PDF_TARGET:
207
      g_value_set_string (value, priv->pdf_target);
208 209
      break;
    case PROP_STATUS:
210
      g_value_set_enum (value, priv->status);
211 212
      break;
    case PROP_STATUS_STRING:
213
      g_value_set_string (value, priv->status_string);
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
      break;
    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
    }
}

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;
  
  g_type_class_add_private (gobject_class, sizeof (GtkPrintOperationPrivate));

  /**
   * GtkPrintOperation::begin-print:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
   *
   * Gets emitted after the user has finished changing print settings
   * in the dialog, before the actual rendering starts. 
   *
   * A typical use for this signal is to use the parameters from the
   * #GtkPrintContext and paginate the document accordingly, and then
Alexander Larsson's avatar
Alexander Larsson committed
242
   * set the number of pages with gtk_print_operation_set_n_pages().
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
   *
   * Since: 2.10
   */
  signals[BEGIN_PRINT] =
    g_signal_new ("begin_print",
		  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);

  /**
   * GtkPrintOperation::request-page-setup:
   * @operation: the #GtkPrintOperation on which the signal was emitted
   * @context: the #GtkPrintContext for the current operation
   * @page_nr: the number of the currently printed page
   * @setup: the #GtkPageSetup 
   * 
   * Gets emitted once for every page that is printed, to give
   * 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] =
    g_signal_new ("request_page_setup",
		  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
   * @page_nr: the number of the currently printed page
   *
   * Gets emitted for every page that is printed. The signal handler
   * must render the @page_nr's page onto the cairo context obtained
   * from @context using gtk_print_context_get_cairo().
Matthias Clasen's avatar
Matthias Clasen committed
289
   *
290 291 292 293
   * <informalexample><programlisting>
   *  FIXME: need an example here
   * </programlisting></informalexample>
   *
Matthias Clasen's avatar
Matthias Clasen committed
294 295 296 297 298
   * 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.
   * 
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351
   * Since: 2.10
   */
  signals[DRAW_PAGE] =
    g_signal_new ("draw_page",
		  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
   *
   * Gets emitted after all pages have been rendered. 
   * A handler for this signal can clean up any resources that have
   * been allocated in the ::begin-print handler.
   * 
   * Since: 2.10
   */
  signals[END_PRINT] =
    g_signal_new ("end_print",
		  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
   *
   * Gets emitted at between the various phases of the print operation.
   * 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] =
   g_signal_new ("status-changed",
                 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
352 353 354 355 356 357 358 359 360 361 362
  /**
   * 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
   * to the ::request-page-setup signal.
   *
   * Since: 2.10
   */
363 364 365 366 367 368 369 370
  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
371 372 373 374 375 376 377 378 379 380
  /**
   * GtkPrintOperation:print-settings:
   *
   * The #GtkPrintSettings used for initializing the dialog.
   *
   * Setting this property is typically used to re-establish print 
   * settings from a previous print operation, see gtk_print_operation_run().
   *
   * Since: 2.10
   */
381 382 383 384 385 386 387
  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
388 389 390 391 392 393 394 395 396 397 398
  
  /**
   * GtkPrintOperation:job-name:
   *
   * A string 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
   */
399 400 401 402 403 404 405
  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
406 407

  /**
Alexander Larsson's avatar
Alexander Larsson committed
408
   * GtkPrintOperation:n-pages:
Matthias Clasen's avatar
Matthias Clasen committed
409 410 411 412 413 414 415 416 417 418 419 420
   *
   * The number of pages in the document. 
   *
   * This <emphasis>must</emphasis> be set to a positive number
   * before the rendering starts. It may be set in a ::begin-print signal hander.
   *
   * Note that the page numbers passed to the ::request-page-setup and ::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.
   *
   * Since: 2.10
   */
421
  g_object_class_install_property (gobject_class,
Alexander Larsson's avatar
Alexander Larsson committed
422 423
				   PROP_N_PAGES,
				   g_param_spec_int ("n-pages",
424 425 426 427 428 429
						     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
430 431 432 433 434 435 436 437 438 439 440 441 442

  /**
   * 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
   */
443 444 445 446 447 448 449 450 451 452
  g_object_class_install_property (gobject_class,
				   PROP_CURRENT_PAGE,
				   g_param_spec_int ("current-page",
						     P_("Current Page"),
						     P_("The current page in the document."),
						     -1,
						     G_MAXINT,
						     -1,
						     GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
453 454 455 456 457 458 459 460 461 462 463
  /**
   * GtkPrintOperation:use-full-page:
   *
   * 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).
   * 
   * Since: 2.10
   */
464 465 466 467
  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
468
							 P_("%TRUE if the the origin of the context should be at the corner of the page and not the corner of the imageable area"),
469 470 471 472
							 FALSE,
							 GTK_PARAM_READWRITE));
  

Matthias Clasen's avatar
Matthias Clasen committed
473 474 475 476 477 478 479 480 481
  /**
   * GtkPrintOperation:unit:
   *
   * The transformation for the cairo context obtained from
   * #GtkPrintContext is set up in such a way that distances are measured 
   * in units of @unit.
   *
   * Since: 2.10
   */
482 483 484 485
  g_object_class_install_property (gobject_class,
				   PROP_UNIT,
				   g_param_spec_enum ("unit",
						      P_("Unit"),
Matthias Clasen's avatar
Matthias Clasen committed
486
						      P_("The unit in which distances can be measured in the context"),
487 488 489 490 491
						      GTK_TYPE_UNIT,
						      GTK_UNIT_PIXEL,
						      GTK_PARAM_READWRITE));
  

Matthias Clasen's avatar
Matthias Clasen committed
492 493 494 495 496 497 498 499
  /**
   * GtkPrintOperation:show-dialog:
   *
   * Determines whether calling gtk_print_operation_run() will present
   * a print dialog to the user, or just print to the default printer.
   *
   * Since: 2.10
   */
500 501 502 503 504 505 506 507
  g_object_class_install_property (gobject_class,
				   PROP_SHOW_DIALOG,
				   g_param_spec_boolean ("show-dialog",
							 P_("Show Dialog"),
							 P_("%TRUE if gtk_print_operation_run() should show the print dialog."),
							 TRUE,
							 GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
508 509 510 511 512 513 514 515 516 517 518 519 520 521
  /**
   * GtkPrintOperation:pdf-target:
   *
   * The name of a PDF file to generate instead of showing the print dialog. 
   *
   * The indended use of this property is for implementing "Export to PDF" 
   * actions.
   *
   * "Print to PDF" support is independent of this and is done
   * by letting the user pick the "Print to PDF" item from the list
   * of printers in the print dialog.
   *
   * Since: 2.10
   */
522 523 524 525 526 527 528 529
  g_object_class_install_property (gobject_class,
				   PROP_JOB_NAME,
				   g_param_spec_string ("pdf-target",
							P_("PDF target filename"),
							P_(""),
							NULL,
							GTK_PARAM_READWRITE));
  
Matthias Clasen's avatar
Matthias Clasen committed
530 531 532 533 534 535 536
  /**
   * GtkPrintOperation:status:
   *
   * The status of the print operation.
   * 
   * Since: 2.10
   */
537 538 539 540 541 542 543 544 545
  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
546 547 548 549 550 551 552 553 554 555 556 557
  /**
   * 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.
   *
   * See the ::status property for a status value that is suitable 
   * for programmatic use. 
   *
   * Since: 2.10
   */
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604
  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));
  

}

/**
 * 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
 * @default_page_setup: a #GtkPageSetup, or %NULL
 * 
 * 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
 * to the ::request-page-setup signal.
 *
 * Since: 2.10
 **/
void
gtk_print_operation_set_default_page_setup (GtkPrintOperation *op,
					    GtkPageSetup      *default_page_setup)
{
605 606
  GtkPrintOperationPrivate *priv;

607 608 609 610
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (default_page_setup == NULL || 
                    GTK_IS_PAGE_SETUP (default_page_setup));

611 612 613
  priv = op->priv;

  if (default_page_setup != priv->default_page_setup)
614 615 616 617
    {
      if (default_page_setup)
	g_object_ref (default_page_setup);
      
618 619
      if (priv->default_page_setup)
	g_object_unref (priv->default_page_setup);
620
      
621
      priv->default_page_setup = default_page_setup;
622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661
     
      g_object_notify (G_OBJECT (op), "default-page-setup");
    }
}

/**
 * gtk_print_operation_get_default_page_setup:
 * @op: a #GtkPrintOperation
 *
 * Returns the default page setup, see 
 * gtk_print_operation_set_default_page_setup().
 *
 * Returns: the default page setup 
 *
 * 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
 * @print_settings: #GtkPrintSettings, or %NULL
 * 
 * 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)
{
662 663
  GtkPrintOperationPrivate *priv;

664 665 666 667
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (print_settings == NULL || 
                    GTK_IS_PRINT_SETTINGS (print_settings));

668 669 670
  priv = op->priv;

  if (print_settings != priv->print_settings)
671 672 673 674
    {
      if (print_settings)
        g_object_ref (print_settings);

675 676
      if (priv->print_settings)
        g_object_unref (priv->print_settings);
677
  
678
      priv->print_settings = print_settings;
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722

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

/**
 * gtk_print_operation_get_print_settings:
 * @op: a #GtkPrintOperation
 * 
 * Returns the current print settings. 
 *
 * Note that the return value is %NULL until either 
 * gtk_print_operation_set_print_settings() or 
 * gtk_print_operation_run() have been called.
 * 
 * Return value: the current print settings of @op.
 * 
 * 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)
{
723 724
  GtkPrintOperationPrivate *priv;

725 726 727
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (g_utf8_validate (job_name, -1, NULL));

728 729 730 731
  priv = op->priv;

  g_free (priv->job_name);
  priv->job_name = g_strdup (job_name);
732 733 734 735 736

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

/**
Alexander Larsson's avatar
Alexander Larsson committed
737
 * gtk_print_operation_set_n_pages:
738 739 740 741 742 743
 * @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
744 745
 * before the rendering starts. It may be set in a ::begin-print 
 * signal hander.
746 747 748 749 750 751 752 753 754
 *
 * Note that the page numbers passed to the ::request-page-setup 
 * and ::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.
 *
 * Since: 2.10
 **/
void
Alexander Larsson's avatar
Alexander Larsson committed
755 756
gtk_print_operation_set_n_pages (GtkPrintOperation *op,
				 gint               n_pages)
757
{
758 759
  GtkPrintOperationPrivate *priv;

760 761 762
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (n_pages > 0);

763 764 765 766 767
  priv = op->priv;
  g_return_if_fail (priv->current_page == -1 || 
		    priv->current_page < n_pages);

  if (priv->nr_of_pages != n_pages)
768
    {
769
      priv->nr_of_pages = n_pages;
770 771 772 773 774 775 776 777 778 779 780

      g_object_notify (G_OBJECT (op), "number-of-pages");
    }
}

/**
 * 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
781
 *
782 783 784 785 786 787 788 789 790 791 792
 * 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)
{
793 794
  GtkPrintOperationPrivate *priv;

795 796
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
  g_return_if_fail (current_page >= 0);
797 798 799 800 801 802

  priv = op->priv;
  g_return_if_fail (priv->nr_of_pages == -1 || 
		    current_page < priv->nr_of_pages);

  if (priv->current_page != current_page)
803
    {
804
      priv->current_page = current_page;
805 806 807 808 809

      g_object_notify (G_OBJECT (op), "current-page");
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
810 811 812 813 814 815 816 817 818 819 820 821 822 823
/**
 * gtk_print_operation_set_use_full_page:
 * @op: a #GtkPrintOperation
 * @full_page: %TRUE to set up the #GtkPrintContext for the full page
 * 
 * If @full_page is %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).
 * 
 * Since: 2.10 
 */
824 825 826 827
void
gtk_print_operation_set_use_full_page (GtkPrintOperation *op,
				       gboolean           full_page)
{
828 829
  GtkPrintOperationPrivate *priv;

830 831 832
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));

  full_page = full_page != FALSE;
833 834 835 836
 
  priv = op->priv;
	
  if (priv->use_full_page != full_page)
837
    {
838
      priv->use_full_page = full_page;
839 840 841 842 843
   
      g_object_notify (G_OBJECT (op), "use-full-page");
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
844 845 846 847 848 849 850 851 852 853 854
/**
 * gtk_print_operation_set_unit:
 * @op: a #GtkPrintOperation
 * @unit: the unit to use
 * 
 * Sets up the transformation for the cairo context obtained from
 * #GtkPrintContext in such a way that distances are measured in 
 * units of @unit.
 *
 * Since: 2.10
 */
855 856 857 858
void
gtk_print_operation_set_unit (GtkPrintOperation *op,
			      GtkUnit            unit)
{
859 860
  GtkPrintOperationPrivate *priv;

861 862
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));

863 864 865
  priv = op->priv;

  if (priv->unit != unit)
866
    {
867
      priv->unit = unit;
868 869 870 871 872 873 874 875 876 877

      g_object_notify (G_OBJECT (op), "unit");
    }
}

void
_gtk_print_operation_set_status (GtkPrintOperation *op,
				 GtkPrintStatus     status,
				 const gchar       *string)
{
878 879
  GtkPrintOperationPrivate *priv = op->priv;
  static const gchar *status_strs[] = {
880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Initial state"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Preparing to print"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Generating data"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Sending data"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Waiting"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Blocking on issue"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Printing"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Finished"),
    /* translators, strip the prefix up to and including the first | */
    N_("print operation status|Finished with error")
  };

  if (status < 0 || status > GTK_PRINT_STATUS_FINISHED_ABORTED)
    status = GTK_PRINT_STATUS_FINISHED_ABORTED;

  if (string == NULL)
    string = g_strip_context (status_strs[status],
			      gettext (status_strs[status]));
  
907 908
  if (priv->status == status &&
      strcmp (string, priv->status_string) == 0)
909 910
    return;
  
911 912 913
  g_free (priv->status_string);
  priv->status_string = g_strdup (string);
  priv->status = status;
914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980

  g_object_notify (G_OBJECT (op), "status");
  g_object_notify (G_OBJECT (op), "status-string");

  g_signal_emit (op, signals[STATUS_CHANGED], 0);
}


/**
 * gtk_print_operation_get_status:
 * @op: a #GtkPrintOperation
 * 
 * Returns the status of the print operation. 
 * Also see gtk_print_operation_get_status_string().
 * 
 * Return value: the status of the print operation
 *
 * Since: 2.10
 **/
GtkPrintStatus
gtk_print_operation_get_status (GtkPrintOperation *op)
{
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
                        GTK_PRINT_STATUS_FINISHED_ABORTED);

  return op->priv->status;
}

/**
 * gtk_print_operation_get_status_string:
 * @op: a #GtkPrintOperation
 * 
 * Returns 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.
 *
 * Use gtk_print_operation_get_status() to obtain a status
 * value that is suitable for programmatic use. 
 * 
 * Return value: a string representation of the status
 *    of the print operation
 *
 * Since: 2.10
 **/
G_CONST_RETURN gchar *
gtk_print_operation_get_status_string (GtkPrintOperation *op)
{
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), "");

  return op->priv->status_string;
}

/**
 * gtk_print_operation_is_finished:
 * @op: a #GtkPrintOperation
 * 
 * A convenience function to find out if the print operation
 * is finished, either successfully (%GTK_PRINT_STATUS_FINISHED)
 * or unsuccessfully (%GTK_PRINT_STATUS_FINISHED_ABORTED).
 * 
 * Return value: %TRUE, if the print operation is finished.
 *
 * Since: 2.10
 **/
gboolean
gtk_print_operation_is_finished (GtkPrintOperation *op)
{
981 982
  GtkPrintOperationPrivate *priv;

983 984
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), TRUE);

985
  priv = op->priv;
986
  return
987 988
    priv->status == GTK_PRINT_STATUS_FINISHED_ABORTED ||
    priv->status == GTK_PRINT_STATUS_FINISHED;
989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005
}


/**
 * gtk_print_operation_set_show_dialog:
 * @op: a #GtkPrintOperation
 * @show_dialog: %TRUE to show the print dialog
 * 
 * Sets whether calling gtk_print_operation_run() will present
 * a print dialog to the user, or just print to the default printer.
 *
 * Since: 2.10
 */
void
gtk_print_operation_set_show_dialog (GtkPrintOperation *op,
				     gboolean           show_dialog)
{
1006 1007
  GtkPrintOperationPrivate *priv;

1008
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));
1009 1010 1011

  priv = op->priv;

1012 1013
  show_dialog = show_dialog != FALSE;

1014
  if (priv->show_dialog != show_dialog)
1015
    {
1016
      priv->show_dialog = show_dialog;
1017 1018 1019 1020 1021

      g_object_notify (G_OBJECT (op), "show-dialog");
    }
}

Matthias Clasen's avatar
Matthias Clasen committed
1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036
/**
 * gtk_print_operation_set_pdf_target:
 * @op: a #GtkPrintOperation
 * @filename: the filename for the PDF file
 * 
 * Sets up the #GtkPrintOperation to generate a PDF file instead
 * of showing the print dialog. The indended use of this function
 * is for implementing "Export to PDF" actions.
 *
 * "Print to PDF" support is independent of this and is done
 * by letting the user pick the "Print to PDF" item from the list
 * of printers in the print dialog.
 *
 * Since: 2.10
 */
1037 1038
void
gtk_print_operation_set_pdf_target (GtkPrintOperation *op,
Matthias Clasen's avatar
Matthias Clasen committed
1039
				    const gchar       *filename)
1040
{
1041 1042
  GtkPrintOperationPrivate *priv;

1043 1044
  g_return_if_fail (GTK_IS_PRINT_OPERATION (op));

1045 1046 1047 1048
  priv = op->priv;

  g_free (priv->pdf_target);
  priv->pdf_target = g_strdup (filename);
1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066

  g_object_notify (G_OBJECT (op), "pdf-target");
}

/* Creates the initial page setup used for printing unless the
 * app overrides this on a per-page basis using request_page_setup.
 *
 * Data is taken from, in order, if existing:
 *
 * PrintSettings returned from the print dialog
 *  (initial dialog values are set from default_page_setup
 *   if unset in app specified print_settings)
 * default_page_setup
 * per-locale default setup
 */
static GtkPageSetup *
create_page_setup (GtkPrintOperation *op)
{
1067
  GtkPrintOperationPrivate *priv = op->priv;
1068 1069 1070
  GtkPageSetup *page_setup;
  GtkPrintSettings *settings;
  
1071 1072
  if (priv->default_page_setup)
    page_setup = gtk_page_setup_copy (priv->default_page_setup);
1073 1074 1075
  else
    page_setup = gtk_page_setup_new ();

1076
  settings = priv->print_settings;
1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103
  if (settings)
    {
      GtkPaperSize *paper_size;
      
      if (gtk_print_settings_has_key (settings, GTK_PRINT_SETTINGS_ORIENTATION))
	gtk_page_setup_set_orientation (page_setup,
					gtk_print_settings_get_orientation (settings));


      paper_size = gtk_print_settings_get_paper_size (settings);
      if (paper_size)
	{
	  gtk_page_setup_set_paper_size (page_setup, paper_size);
	  gtk_paper_size_free (paper_size);
	}

      /* TODO: Margins? */
    }
  
  return page_setup;
}

static void 
pdf_start_page (GtkPrintOperation *op,
		GtkPrintContext   *print_context,
		GtkPageSetup      *page_setup)
{
1104 1105 1106 1107 1108 1109 1110 1111 1112
  GtkPaperSize *paper_size;
  double w, h;

  paper_size = gtk_page_setup_get_paper_size (page_setup);

  w = gtk_paper_size_get_width (paper_size, GTK_UNIT_POINTS);
  h = gtk_paper_size_get_height (paper_size, GTK_UNIT_POINTS);
  
  cairo_pdf_surface_set_size (op->priv->surface, w, h);
1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125
}

static void
pdf_end_page (GtkPrintOperation *op,
	      GtkPrintContext   *print_context)
{
  cairo_t *cr;

  cr = gtk_print_context_get_cairo (print_context);
  cairo_show_page (cr);
}

static void
Alexander Larsson's avatar
Alexander Larsson committed
1126 1127
pdf_end_run (GtkPrintOperation *op,
	     gboolean wait)
1128
{
1129 1130 1131 1132
  GtkPrintOperationPrivate *priv = op->priv;

  cairo_surface_destroy (priv->surface);
  priv->surface = NULL;
1133 1134 1135 1136 1137 1138 1139 1140
}

static GtkPrintOperationResult
run_pdf (GtkPrintOperation  *op,
	 GtkWindow          *parent,
	 gboolean           *do_print,
	 GError            **error)
{
1141
  GtkPrintOperationPrivate *priv = op->priv;
1142 1143 1144 1145 1146 1147 1148 1149 1150 1151
  GtkPageSetup *page_setup;
  double width, height;
  /* This will be overwritten later by the non-default size, but
     we need to pass some size: */
  
  page_setup = create_page_setup (op);
  width = gtk_page_setup_get_paper_width (page_setup, GTK_UNIT_POINTS);
  height = gtk_page_setup_get_paper_height (page_setup, GTK_UNIT_POINTS);
  g_object_unref (page_setup);
  
1152 1153 1154
  priv->surface = cairo_pdf_surface_create (priv->pdf_target,
					    width, height);
  cairo_pdf_surface_set_dpi (priv->surface, 300, 300);
1155
  
1156 1157
  priv->dpi_x = 72;
  priv->dpi_y = 72;
1158

1159 1160 1161 1162
  priv->print_pages = GTK_PRINT_PAGES_ALL;
  priv->page_ranges = NULL;
  priv->num_page_ranges = 0;

1163 1164
  priv->manual_num_copies = 1;
  priv->manual_collation = FALSE;
1165 1166 1167 1168
  priv->manual_reverse = FALSE;
  priv->manual_page_set = GTK_PAGE_SET_ALL;
  priv->manual_scale = 1.0;
  priv->manual_orientation = TRUE;
1169 1170 1171
  
  *do_print = TRUE;
  
1172 1173 1174
  priv->start_page = pdf_start_page;
  priv->end_page = pdf_end_page;
  priv->end_run = pdf_end_run;
1175 1176 1177 1178
  
  return GTK_PRINT_OPERATION_RESULT_APPLY; 
}

1179
static void
Alexander Larsson's avatar
Alexander Larsson committed
1180 1181
print_pages (GtkPrintOperation *op,
	     gboolean wait)
1182
{
1183
  GtkPrintOperationPrivate *priv = op->priv;
1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
  int page, range;
  GtkPageSetup *initial_page_setup, *page_setup;
  GtkPrintContext *print_context;
  cairo_t *cr;
  int uncollated_copies, collated_copies;
  int i, j;
  GtkPageRange *ranges;
  GtkPageRange one_range;
  int num_ranges;

1194
  if (priv->manual_collation)
1195
    {
1196
      uncollated_copies = priv->manual_num_copies;
1197 1198 1199 1200 1201
      collated_copies = 1;
    }
  else
    {
      uncollated_copies = 1;
1202
      collated_copies = priv->manual_num_copies;
1203 1204 1205 1206 1207 1208 1209 1210 1211 1212
    }

  print_context = _gtk_print_context_new (op);

  initial_page_setup = create_page_setup (op);
  _gtk_print_context_set_page_setup (print_context, initial_page_setup);

  _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_PREPARING, NULL);
  g_signal_emit (op, signals[BEGIN_PRINT], 0, print_context);
  
1213
  g_return_if_fail (priv->nr_of_pages > 0);
1214

1215
  if (priv->print_pages == GTK_PRINT_PAGES_RANGES)
1216
    {
1217 1218
      ranges = priv->page_ranges;
      num_ranges = priv->num_page_ranges;
1219
    }
1220 1221
  else if (priv->print_pages == GTK_PRINT_PAGES_CURRENT &&
	   priv->current_page != -1)
1222 1223 1224
    {
      ranges = &one_range;
      num_ranges = 1;
1225 1226
      ranges[0].start = priv->current_page;
      ranges[0].end = priv->current_page;
1227 1228 1229 1230 1231 1232
    }
  else
    {
      ranges = &one_range;
      num_ranges = 1;
      ranges[0].start = 0;
1233
      ranges[0].end = priv->nr_of_pages - 1;
1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245
    }
  
  _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_GENERATING_DATA, NULL);

  for (i = 0; i < uncollated_copies; i++)
    {
      for (range = 0; range < num_ranges; range ++)
	{
	  int start, end, inc;
	  int low = ranges[range].start;
	  int high = ranges[range].end;
	  
1246
	  if (priv->manual_reverse)
1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259
	    {
	      start = high;
	      end = low - 1;
	      inc = -1;
	    }
	  else
	    {
	      start = low;
	      end = high + 1;
	      inc = 1;
	    }
	  for (page = start; page != end; page += inc)
	    {
1260 1261
	      if ((priv->manual_page_set == GTK_PAGE_SET_EVEN && page % 2 == 0) ||
		  (priv->manual_page_set == GTK_PAGE_SET_ODD && page % 2 == 1))
1262 1263 1264 1265 1266 1267 1268 1269
		continue;
	      
	      for (j = 0; j < collated_copies; j++)
		{
		  page_setup = gtk_page_setup_copy (initial_page_setup);
		  g_signal_emit (op, signals[REQUEST_PAGE_SETUP], 0, print_context, page, page_setup);
		  
		  _gtk_print_context_set_page_setup (print_context, page_setup);
1270
		  priv->start_page (op, print_context, page_setup);
1271 1272 1273 1274
		  
		  cr = gtk_print_context_get_cairo (print_context);

		  cairo_save (cr);
1275
		  if (priv->manual_scale != 100.0)
1276
		    cairo_scale (cr,
1277 1278
				 priv->manual_scale,
				 priv->manual_scale);
1279
		  
1280
		  if (priv->manual_orientation)
1281 1282
		    _gtk_print_context_rotate_according_to_orientation (print_context);

1283
		  if (!priv->use_full_page)
1284 1285 1286 1287 1288
		    _gtk_print_context_translate_into_margin (print_context);
	  
		  g_signal_emit (op, signals[DRAW_PAGE], 0, 
				 print_context, page);
		  
1289
		  priv->end_page (op, print_context);
1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307
		  
		  cairo_restore (cr);
		  
		  g_object_unref (page_setup);

		  /* Iterate the mainloop so that we redraw windows */
		  while (gtk_events_pending ())
		    gtk_main_iteration ();
		}
	    }
	}
    }
  
  g_signal_emit (op, signals[END_PRINT], 0, print_context);

  g_object_unref (print_context);
  g_object_unref (initial_page_setup);

1308
  cairo_surface_finish (priv->surface);
Alexander Larsson's avatar
Alexander Larsson committed
1309
  priv->end_run (op, wait);
1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344
}

/**
 * gtk_print_operation_run:
 * @op: a #GtkPrintOperation
 * @parent: Transient parent of the dialog, or %NULL
 * @error: Return location for errors, or %NULL
 * 
 * Runs the print operation, by first letting the user modify
 * print settings in the print dialog, and then print the
 * document.
 *
 * Note that this function does not return until the rendering of all 
 * pages is complete. You can connect to the ::status-changed signal on
 * @op to obtain some information about the progress of the print operation. 
 * Furthermore, it may use a recursive mainloop to show the print dialog.
 * See gtk_print_operation_run_async() if this is a problem.
 * 
 * <informalexample><programlisting>
 *  FIXME: need an example here
 * </programlisting></informalexample>
 *
 * Return value: the result of the print operation. A return value of 
 *   %GTK_PRINT_OPERATION_RESULT_APPLY indicates that the printing was 
 *   completed successfully. In this case, it is a good idea to obtain 
 *   the used print settings with gtk_print_operation_get_print_settings() 
 *   and store them for reuse with the next print operation.
 *
 * Since: 2.10
 **/
GtkPrintOperationResult
gtk_print_operation_run (GtkPrintOperation  *op,
			 GtkWindow          *parent,
			 GError            **error)
{
1345
  GtkPrintOperationPrivate *priv;
1346 1347 1348 1349 1350 1351
  GtkPrintOperationResult result;
  gboolean do_print;
  
  g_return_val_if_fail (GTK_IS_PRINT_OPERATION (op), 
                        GTK_PRINT_OPERATION_RESULT_ERROR);

1352 1353 1354
  priv = op->priv;

  if (priv->pdf_target != NULL)
1355 1356 1357 1358 1359 1360 1361
    result = run_pdf (op, parent, &do_print, error);
  else
    result = _gtk_print_operation_platform_backend_run_dialog (op, 
							       parent,
							       &do_print,
							       error);
  if (do_print)
Alexander Larsson's avatar
Alexander Larsson committed
1362
    print_pages (op, TRUE);
1363 1364 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
  else 
    _gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);

  return result;
}

/**
 * gtk_print_operation_run_async:
 * @op: a #GtkPrintOperation
 * @parent: Transient parent of the dialog, or %NULL
 * 
 * Runs the print operation, by first letting the user modify
 * print settings in the print dialog, and then print the
 * document.
 *
 * In contrast to gtk_print_operation_run(), this function returns after 
 * showing the print dialog on platforms that support this, and handles 
 * the printing by connecting a signal handler to the ::response signal 
 * of the dialog. 
 * 
 * If you use this function, it is recommended that you store the modified
 * #GtkPrintSettings in a ::begin-print or ::end-print signal handler.
 * 
 * Since: 2.10
 **/
void
gtk_print_operation_run_async (GtkPrintOperation *op,
			       GtkWindow         *parent)
{
1392
  GtkPrintOperationPrivate *priv;
1393 1394 1395 1396
  gboolean do_print;

  g_return_if_fail (GTK_IS_PRINT_OPERATION (op)); 

1397 1398 1399
  priv = op->priv;

  if (priv->pdf_target != NULL)
1400 1401 1402
    {
      run_pdf (op, parent, &do_print, NULL);
      if (do_print)
Alexander Larsson's avatar
Alexander Larsson committed
1403
	print_pages (op, FALSE);
1404 1405 1406 1407 1408 1409 1410
      else 
	_gtk_print_operation_set_status (op, GTK_PRINT_STATUS_FINISHED_ABORTED, NULL);
    }
  else
    _gtk_print_operation_platform_backend_run_dialog_async (op, 
							    parent,
							    print_pages);