gtkprintsettings.c 45 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
 * gtkprintsettings.c: Print Settings
 * 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 22
#include <string.h>
#include <stdlib.h>
23

24
#include <glib/gprintf.h>
25

26
#include "gtkprintsettings.h"
Matthias Clasen's avatar
Matthias Clasen committed
27
#include "gtkprintutils.h"
28 29
#include "gtktypebuiltins.h"
#include "gtkwidget.h"
30 31


32 33 34 35 36 37 38
/**
 * SECTION:gtkprintsettings
 * @Short_description: Stores print settings
 * @Title: GtkPrintSettings
 *
 * A GtkPrintSettings object represents the settings of a print dialog in
 * a system-independent way. The main use for this object is that once
39
 * you’ve printed you can get a settings object that represents the settings
40
 * the user chose, and the next time you print you can pass that object in so
41
 * that the user doesn’t have to re-set all his settings.
42 43 44 45 46 47 48 49 50
 *
 * Its also possible to enumerate the settings so that you can easily save
 * the settings for the next time your app runs, or even store them in a
 * document. The predefined keys try to use shared values as much as possible
 * so that moving such a document between systems still works.
 *
 * Printing support was added in GTK+ 2.10.
 */

51 52 53 54 55 56 57 58 59
typedef struct _GtkPrintSettingsClass GtkPrintSettingsClass;

#define GTK_IS_PRINT_SETTINGS_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PRINT_SETTINGS))
#define GTK_PRINT_SETTINGS_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PRINT_SETTINGS, GtkPrintSettingsClass))
#define GTK_PRINT_SETTINGS_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PRINT_SETTINGS, GtkPrintSettingsClass))

struct _GtkPrintSettings
{
  GObject parent_instance;
60

61 62 63 64 65 66 67 68
  GHashTable *hash;
};

struct _GtkPrintSettingsClass
{
  GObjectClass parent_class;
};

Matthias Clasen's avatar
Matthias Clasen committed
69 70
#define KEYFILE_GROUP_NAME "Print Settings"

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
G_DEFINE_TYPE (GtkPrintSettings, gtk_print_settings, G_TYPE_OBJECT)

static void
gtk_print_settings_finalize (GObject *object)
{
  GtkPrintSettings *settings = GTK_PRINT_SETTINGS (object);

  g_hash_table_destroy (settings->hash);

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

static void
gtk_print_settings_init (GtkPrintSettings *settings)
{
  settings->hash = g_hash_table_new_full (g_str_hash, g_str_equal,
					  g_free, g_free);
}

static void
gtk_print_settings_class_init (GtkPrintSettingsClass *class)
{
  GObjectClass *gobject_class = (GObjectClass *)class;

  gobject_class->finalize = gtk_print_settings_finalize;
}

/**
 * gtk_print_settings_new:
 * 
 * Creates a new #GtkPrintSettings object.
 *  
103
 * Returns: a new #GtkPrintSettings object
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
 *
 * Since: 2.10
 */
GtkPrintSettings *
gtk_print_settings_new (void)
{
  return g_object_new (GTK_TYPE_PRINT_SETTINGS, NULL);
}

static void
copy_hash_entry  (gpointer  key,
		  gpointer  value,
		  gpointer  user_data)
{
  GtkPrintSettings *settings = user_data;

  g_hash_table_insert (settings->hash, 
		       g_strdup (key), 
		       g_strdup (value));
}



/**
 * gtk_print_settings_copy:
 * @other: a #GtkPrintSettings
130
 *
131
 * Copies a #GtkPrintSettings object.
132
 *
133
 * Returns: (transfer full): a newly allocated copy of @other
134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
 *
 * Since: 2.10
 */
GtkPrintSettings *
gtk_print_settings_copy (GtkPrintSettings *other)
{
  GtkPrintSettings *settings;

  if (other == NULL)
    return NULL;
  
  g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (other), NULL);

  settings = gtk_print_settings_new ();

  g_hash_table_foreach (other->hash,
			copy_hash_entry,
			settings);

  return settings;
}

/**
 * gtk_print_settings_get:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Looks up the string value associated with @key.
 * 
163
 * Returns: the string value for @key
164 165 166
 * 
 * Since: 2.10
 */
167
const gchar *
168 169 170 171 172 173 174 175 176 177
gtk_print_settings_get (GtkPrintSettings *settings,
			const gchar      *key)
{
  return g_hash_table_lookup (settings->hash, key);
}

/**
 * gtk_print_settings_set:
 * @settings: a #GtkPrintSettings
 * @key: a key
178 179
 * @value: (allow-none): a string value, or %NULL
 *
180 181
 * Associates @value with @key.
 *
182
 * Since: 2.10
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
 */
void
gtk_print_settings_set (GtkPrintSettings *settings,
			const gchar      *key,
			const gchar      *value)
{
  if (value == NULL)
    gtk_print_settings_unset (settings, key);
  else
    g_hash_table_insert (settings->hash, 
			 g_strdup (key), 
			 g_strdup (value));
}

/**
 * gtk_print_settings_unset:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Removes any value associated with @key. 
 * This has the same effect as setting the value to %NULL.
 *
 * Since: 2.10 
 */
void
gtk_print_settings_unset (GtkPrintSettings *settings,
			  const gchar      *key)
{
  g_hash_table_remove (settings->hash, key);
}

/**
 * gtk_print_settings_has_key:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Returns %TRUE, if a value is associated with @key.
 * 
221
 * Returns: %TRUE, if @key has a value
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240
 *
 * Since: 2.10
 */
gboolean        
gtk_print_settings_has_key (GtkPrintSettings *settings,
			    const gchar      *key)
{
  return gtk_print_settings_get (settings, key) != NULL;
}


/**
 * gtk_print_settings_get_bool:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Returns the boolean represented by the value
 * that is associated with @key. 
 *
241
 * The string “true” represents %TRUE, any other 
242 243
 * string %FALSE.
 *
244
 * Returns: %TRUE, if @key maps to a true value.
245 246 247 248 249 250 251 252 253 254
 * 
 * Since: 2.10
 **/
gboolean
gtk_print_settings_get_bool (GtkPrintSettings *settings,
			     const gchar      *key)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, key);
255
  if (g_strcmp0 (val, "true") == 0)
256 257 258 259 260 261 262 263 264 265 266 267 268 269 270
    return TRUE;
  
  return FALSE;
}

/**
 * gtk_print_settings_get_bool_with_default:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @default_val: the default value
 * 
 * Returns the boolean represented by the value
 * that is associated with @key, or @default_val
 * if the value does not represent a boolean.
 *
271 272
 * The string “true” represents %TRUE, the string
 * “false” represents %FALSE.
273
 *
274
 * Returns: the boolean value associated with @key
275 276 277 278 279 280 281 282 283 284 285
 * 
 * Since: 2.10
 */
static gboolean
gtk_print_settings_get_bool_with_default (GtkPrintSettings *settings,
					  const gchar      *key,
					  gboolean          default_val)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, key);
286
  if (g_strcmp0 (val, "true") == 0)
287 288
    return TRUE;

289
  if (g_strcmp0 (val, "false") == 0)
290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327
    return FALSE;
  
  return default_val;
}

/**
 * gtk_print_settings_set_bool:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @value: a boolean
 * 
 * Sets @key to a boolean value.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_bool (GtkPrintSettings *settings,
			     const gchar      *key,
			     gboolean          value)
{
  if (value)
    gtk_print_settings_set (settings, key, "true");
  else
    gtk_print_settings_set (settings, key, "false");
}

/**
 * gtk_print_settings_get_double_with_default:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @def: the default value
 * 
 * Returns the floating point number represented by 
 * the value that is associated with @key, or @default_val
 * if the value does not represent a floating point number.
 *
 * Floating point numbers are parsed with g_ascii_strtod().
 *
328
 * Returns: the floating point number associated with @key
329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352
 * 
 * Since: 2.10
 */
gdouble
gtk_print_settings_get_double_with_default (GtkPrintSettings *settings,
					    const gchar      *key,
					    gdouble           def)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, key);
  if (val == NULL)
    return def;

  return g_ascii_strtod (val, NULL);
}

/**
 * gtk_print_settings_get_double:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Returns the double value associated with @key, or 0.
 * 
353
 * Returns: the double value of @key
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393
 *
 * Since: 2.10
 */
gdouble
gtk_print_settings_get_double (GtkPrintSettings *settings,
			       const gchar      *key)
{
  return gtk_print_settings_get_double_with_default (settings, key, 0.0);
}

/**
 * gtk_print_settings_set_double:
 * @settings: a #GtkPrintSettings
 * @key: a key 
 * @value: a double value
 * 
 * Sets @key to a double value.
 * 
 * Since: 2.10
 */
void
gtk_print_settings_set_double (GtkPrintSettings *settings,
			       const gchar      *key,
			       gdouble           value)
{
  gchar buf[G_ASCII_DTOSTR_BUF_SIZE];
  
  g_ascii_dtostr (buf, G_ASCII_DTOSTR_BUF_SIZE, value);
  gtk_print_settings_set (settings, key, buf);
}

/**
 * gtk_print_settings_get_length:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @unit: the unit of the return value
 * 
 * Returns the value associated with @key, interpreted
 * as a length. The returned value is converted to @units.
 * 
394
 * Returns: the length value of @key, converted to @unit
395 396 397 398 399 400 401 402 403
 *
 * Since: 2.10
 */
gdouble
gtk_print_settings_get_length (GtkPrintSettings *settings,
			       const gchar      *key,
			       GtkUnit           unit)
{
  gdouble length = gtk_print_settings_get_double (settings, key);
Matthias Clasen's avatar
Matthias Clasen committed
404
  return _gtk_print_convert_from_mm (length, unit);
405 406 407 408 409 410
}

/**
 * gtk_print_settings_set_length:
 * @settings: a #GtkPrintSettings
 * @key: a key
411
 * @value: a length
412 413 414 415 416 417 418 419 420
 * @unit: the unit of @length
 * 
 * Associates a length in units of @unit with @key.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_length (GtkPrintSettings *settings,
			       const gchar      *key,
421
			       gdouble           value, 
422 423 424
			       GtkUnit           unit)
{
  gtk_print_settings_set_double (settings, key,
425
				 _gtk_print_convert_to_mm (value, unit));
426 427 428 429 430 431 432 433 434 435 436
}

/**
 * gtk_print_settings_get_int_with_default:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @def: the default value
 * 
 * Returns the value of @key, interpreted as
 * an integer, or the default value.
 * 
437
 * Returns: the integer value of @key
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
 *
 * Since: 2.10
 */
gint
gtk_print_settings_get_int_with_default (GtkPrintSettings *settings,
					 const gchar      *key,
					 gint              def)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, key);
  if (val == NULL)
    return def;

  return atoi (val);
}

/**
 * gtk_print_settings_get_int:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * 
 * Returns the integer value of @key, or 0.
 * 
462
 * Returns: the integer value of @key 
463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
 *
 * Since: 2.10
 */
gint
gtk_print_settings_get_int (GtkPrintSettings *settings,
			    const gchar      *key)
{
  return gtk_print_settings_get_int_with_default (settings, key, 0);
}

/**
 * gtk_print_settings_set_int:
 * @settings: a #GtkPrintSettings
 * @key: a key
 * @value: an integer 
 * 
 * Sets @key to an integer value.
 *
 * Since: 2.10 
 */
void
gtk_print_settings_set_int (GtkPrintSettings *settings,
			    const gchar      *key,
			    gint              value)
{
  gchar buf[128];
  g_sprintf (buf, "%d", value);
  gtk_print_settings_set (settings, key, buf);
}

/**
 * gtk_print_settings_foreach:
 * @settings: a #GtkPrintSettings
496
 * @func: (scope call): the function to call
497
 * @user_data: user data for @func
498
 *
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517
 * Calls @func for each key-value pair of @settings.
 *
 * Since: 2.10
 */
void
gtk_print_settings_foreach (GtkPrintSettings    *settings,
			    GtkPrintSettingsFunc func,
			    gpointer             user_data)
{
  g_hash_table_foreach (settings->hash, (GHFunc)func, user_data);
}

/**
 * gtk_print_settings_get_printer:
 * @settings: a #GtkPrintSettings
 * 
 * Convenience function to obtain the value of 
 * %GTK_PRINT_SETTINGS_PRINTER.
 *
518
 * Returns: the printer name
519 520 521
 *
 * Since: 2.10
 */
522
const gchar *
523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
gtk_print_settings_get_printer (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_PRINTER);
}


/**
 * gtk_print_settings_set_printer:
 * @settings: a #GtkPrintSettings
 * @printer: the printer name
 * 
 * Convenience function to set %GTK_PRINT_SETTINGS_PRINTER
 * to @printer.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_printer (GtkPrintSettings *settings,
				const gchar      *printer)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PRINTER, printer);
}

/**
 * gtk_print_settings_get_orientation:
 * @settings: a #GtkPrintSettings
 * 
 * Get the value of %GTK_PRINT_SETTINGS_ORIENTATION, 
 * converted to a #GtkPageOrientation.
 * 
553
 * Returns: the orientation
554 555 556 557 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 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619
 *
 * Since: 2.10
 */
GtkPageOrientation
gtk_print_settings_get_orientation (GtkPrintSettings *settings)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_ORIENTATION);

  if (val == NULL || strcmp (val, "portrait") == 0)
    return GTK_PAGE_ORIENTATION_PORTRAIT;

  if (strcmp (val, "landscape") == 0)
    return GTK_PAGE_ORIENTATION_LANDSCAPE;
  
  if (strcmp (val, "reverse_portrait") == 0)
    return GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT;
  
  if (strcmp (val, "reverse_landscape") == 0)
    return GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE;
  
  return GTK_PAGE_ORIENTATION_PORTRAIT;
}

/**
 * gtk_print_settings_set_orientation:
 * @settings: a #GtkPrintSettings
 * @orientation: a page orientation
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_ORIENTATION.
 * 
 * Since: 2.10
 */
void
gtk_print_settings_set_orientation (GtkPrintSettings   *settings,
				    GtkPageOrientation  orientation)
{
  const gchar *val;

  switch (orientation)
    {
    case GTK_PAGE_ORIENTATION_LANDSCAPE:
      val = "landscape";
      break;
    default:
    case GTK_PAGE_ORIENTATION_PORTRAIT:
      val = "portrait";
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_LANDSCAPE:
      val = "reverse_landscape";
      break;
    case GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT:
      val = "reverse_portrait";
      break;
    }
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_ORIENTATION, val);
}

/**
 * gtk_print_settings_get_paper_size:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PAPER_FORMAT, 
 * converted to a #GtkPaperSize.
 * 
620
 * Returns: the paper size
621 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 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696
 *
 * Since: 2.10
 */
GtkPaperSize *     
gtk_print_settings_get_paper_size (GtkPrintSettings *settings)
{
  const gchar *val;
  const gchar *name;
  gdouble w, h;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_PAPER_FORMAT);
  if (val == NULL)
    return NULL;

  if (g_str_has_prefix (val, "custom-")) 
    {
      name = val + strlen ("custom-");
      w = gtk_print_settings_get_paper_width (settings, GTK_UNIT_MM);
      h = gtk_print_settings_get_paper_height (settings, GTK_UNIT_MM);
      return gtk_paper_size_new_custom (name, name, w, h, GTK_UNIT_MM);
    }

  return gtk_paper_size_new (val);
}

/**
 * gtk_print_settings_set_paper_size:
 * @settings: a #GtkPrintSettings
 * @paper_size: a paper size
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PAPER_FORMAT,
 * %GTK_PRINT_SETTINGS_PAPER_WIDTH and
 * %GTK_PRINT_SETTINGS_PAPER_HEIGHT.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_paper_size (GtkPrintSettings *settings,
				   GtkPaperSize     *paper_size)
{
  gchar *custom_name;

  if (paper_size == NULL) 
    {
      gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAPER_FORMAT, NULL);
      gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAPER_WIDTH, NULL);
      gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAPER_HEIGHT, NULL);
    }
  else if (gtk_paper_size_is_custom (paper_size)) 
    {
      custom_name = g_strdup_printf ("custom-%s", 
				     gtk_paper_size_get_name (paper_size));
      gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAPER_FORMAT, custom_name);
      g_free (custom_name);
      gtk_print_settings_set_paper_width (settings, 
					  gtk_paper_size_get_width (paper_size, 
								    GTK_UNIT_MM),
					  GTK_UNIT_MM);
      gtk_print_settings_set_paper_height (settings, 
					   gtk_paper_size_get_height (paper_size, 
								      GTK_UNIT_MM),
					   GTK_UNIT_MM);
    } 
  else
    gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAPER_FORMAT, 
			    gtk_paper_size_get_name (paper_size));
}

/**
 * gtk_print_settings_get_paper_width:
 * @settings: a #GtkPrintSettings
 * @unit: the unit for the return value
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PAPER_WIDTH,
 * converted to @unit. 
 * 
697
 * Returns: the paper width, in units of @unit
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 723 724 725 726 727 728 729 730 731 732 733
 *
 * Since: 2.10
 */
gdouble
gtk_print_settings_get_paper_width (GtkPrintSettings *settings,
				    GtkUnit           unit)
{
  return gtk_print_settings_get_length (settings, GTK_PRINT_SETTINGS_PAPER_WIDTH, unit);
}

/**
 * gtk_print_settings_set_paper_width:
 * @settings: a #GtkPrintSettings
 * @width: the paper width
 * @unit: the units of @width
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PAPER_WIDTH.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_paper_width (GtkPrintSettings *settings,
				    gdouble           width, 
				    GtkUnit           unit)
{
  gtk_print_settings_set_length (settings, GTK_PRINT_SETTINGS_PAPER_WIDTH, width, unit);
}

/**
 * gtk_print_settings_get_paper_height:
 * @settings: a #GtkPrintSettings
 * @unit: the unit for the return value
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PAPER_HEIGHT,
 * converted to @unit. 
 * 
734
 * Returns: the paper height, in units of @unit
735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766
 *
 * Since: 2.10
 */
gdouble
gtk_print_settings_get_paper_height (GtkPrintSettings *settings,
				     GtkUnit           unit)
{
  return gtk_print_settings_get_length (settings, 
					GTK_PRINT_SETTINGS_PAPER_HEIGHT,
					unit);
}

/**
 * gtk_print_settings_set_paper_height:
 * @settings: a #GtkPrintSettings
 * @height: the paper height
 * @unit: the units of @height
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PAPER_HEIGHT.
 *
 * Since: 2.10
 */
void
gtk_print_settings_set_paper_height (GtkPrintSettings *settings,
				     gdouble           height, 
				     GtkUnit           unit)
{
  gtk_print_settings_set_length (settings, 
				 GTK_PRINT_SETTINGS_PAPER_HEIGHT, 
				 height, unit);
}

Matthias Clasen's avatar
Matthias Clasen committed
767 768 769 770 771 772
/**
 * gtk_print_settings_get_use_color:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_USE_COLOR.
 * 
773
 * Returns: whether to use color
Matthias Clasen's avatar
Matthias Clasen committed
774 775 776
 *
 * Since: 2.10
 */
777 778 779 780 781 782 783 784
gboolean
gtk_print_settings_get_use_color (GtkPrintSettings *settings)
{
  return gtk_print_settings_get_bool_with_default (settings, 
						   GTK_PRINT_SETTINGS_USE_COLOR,
						   TRUE);
}

Matthias Clasen's avatar
Matthias Clasen committed
785 786 787 788 789 790 791 792 793
/**
 * gtk_print_settings_set_use_color:
 * @settings: a #GtkPrintSettings
 * @use_color: whether to use color
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_USE_COLOR.
 * 
 * Since: 2.10
 */
794 795 796 797 798 799 800 801 802
void
gtk_print_settings_set_use_color (GtkPrintSettings *settings,
				  gboolean          use_color)
{
  gtk_print_settings_set_bool (settings,
			       GTK_PRINT_SETTINGS_USE_COLOR, 
			       use_color);
}

Matthias Clasen's avatar
Matthias Clasen committed
803 804 805 806 807 808
/**
 * gtk_print_settings_get_collate:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_COLLATE.
 * 
809
 * Returns: whether to collate the printed pages
Matthias Clasen's avatar
Matthias Clasen committed
810 811 812
 *
 * Since: 2.10
 */
813 814 815
gboolean
gtk_print_settings_get_collate (GtkPrintSettings *settings)
{
816 817 818
  return gtk_print_settings_get_bool_with_default (settings,
                                                   GTK_PRINT_SETTINGS_COLLATE,
                                                   TRUE);
819 820
}

Matthias Clasen's avatar
Matthias Clasen committed
821 822 823 824 825 826 827 828 829
/**
 * gtk_print_settings_set_collate:
 * @settings: a #GtkPrintSettings
 * @collate: whether to collate the output
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_COLLATE.
 * 
 * Since: 2.10
 */
830 831 832 833 834 835 836 837 838
void
gtk_print_settings_set_collate (GtkPrintSettings *settings,
				gboolean          collate)
{
  gtk_print_settings_set_bool (settings,
			       GTK_PRINT_SETTINGS_COLLATE, 
			       collate);
}

Matthias Clasen's avatar
Matthias Clasen committed
839 840 841 842 843 844
/**
 * gtk_print_settings_get_reverse:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_REVERSE.
 * 
845
 * Returns: whether to reverse the order of the printed pages
Matthias Clasen's avatar
Matthias Clasen committed
846 847 848
 *
 * Since: 2.10
 */
849 850 851 852 853 854 855
gboolean
gtk_print_settings_get_reverse (GtkPrintSettings *settings)
{
  return gtk_print_settings_get_bool (settings, 
				      GTK_PRINT_SETTINGS_REVERSE);
}

Matthias Clasen's avatar
Matthias Clasen committed
856 857 858 859 860 861 862 863 864
/**
 * gtk_print_settings_set_reverse:
 * @settings: a #GtkPrintSettings
 * @reverse: whether to reverse the output
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_REVERSE.
 * 
 * Since: 2.10
 */
865 866 867 868 869 870 871 872 873
void
gtk_print_settings_set_reverse (GtkPrintSettings *settings,
				  gboolean        reverse)
{
  gtk_print_settings_set_bool (settings,
			       GTK_PRINT_SETTINGS_REVERSE, 
			       reverse);
}

Matthias Clasen's avatar
Matthias Clasen committed
874 875 876 877 878 879
/**
 * gtk_print_settings_get_duplex:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_DUPLEX.
 * 
880
 * Returns: whether to print the output in duplex.
Matthias Clasen's avatar
Matthias Clasen committed
881 882 883
 *
 * Since: 2.10
 */
884 885 886 887 888 889 890 891 892 893 894 895 896 897
GtkPrintDuplex
gtk_print_settings_get_duplex (GtkPrintSettings *settings)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_DUPLEX);

  if (val == NULL || (strcmp (val, "simplex") == 0))
    return GTK_PRINT_DUPLEX_SIMPLEX;

  if (strcmp (val, "horizontal") == 0)
    return GTK_PRINT_DUPLEX_HORIZONTAL;
  
  if (strcmp (val, "vertical") == 0)
Matthias Clasen's avatar
Matthias Clasen committed
898
    return GTK_PRINT_DUPLEX_VERTICAL;
899 900 901 902
  
  return GTK_PRINT_DUPLEX_SIMPLEX;
}

Matthias Clasen's avatar
Matthias Clasen committed
903 904 905 906 907 908 909 910 911
/**
 * gtk_print_settings_set_duplex:
 * @settings: a #GtkPrintSettings
 * @duplex: a #GtkPrintDuplex value
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_DUPLEX.
 * 
 * Since: 2.10
 */
912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934
void
gtk_print_settings_set_duplex (GtkPrintSettings *settings,
			       GtkPrintDuplex    duplex)
{
  const gchar *str;

  switch (duplex)
    {
    default:
    case GTK_PRINT_DUPLEX_SIMPLEX:
      str = "simplex";
      break;
    case GTK_PRINT_DUPLEX_HORIZONTAL:
      str = "horizontal";
      break;
    case GTK_PRINT_DUPLEX_VERTICAL:
      str = "vertical";
      break;
    }
  
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_DUPLEX, str);
}

Matthias Clasen's avatar
Matthias Clasen committed
935 936 937 938 939 940
/**
 * gtk_print_settings_get_quality:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_QUALITY.
 * 
941
 * Returns: the print quality
Matthias Clasen's avatar
Matthias Clasen committed
942 943 944
 *
 * Since: 2.10
 */
945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966
GtkPrintQuality
gtk_print_settings_get_quality (GtkPrintSettings *settings)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_QUALITY);

  if (val == NULL || (strcmp (val, "normal") == 0))
    return GTK_PRINT_QUALITY_NORMAL;

  if (strcmp (val, "high") == 0)
    return GTK_PRINT_QUALITY_HIGH;
  
  if (strcmp (val, "low") == 0)
    return GTK_PRINT_QUALITY_LOW;
  
  if (strcmp (val, "draft") == 0)
    return GTK_PRINT_QUALITY_DRAFT;
  
  return GTK_PRINT_QUALITY_NORMAL;
}

Matthias Clasen's avatar
Matthias Clasen committed
967 968 969
/**
 * gtk_print_settings_set_quality:
 * @settings: a #GtkPrintSettings
970
 * @quality: a #GtkPrintQuality value
Matthias Clasen's avatar
Matthias Clasen committed
971 972 973 974 975
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_QUALITY.
 * 
 * Since: 2.10
 */
976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001
void
gtk_print_settings_set_quality (GtkPrintSettings *settings,
				GtkPrintQuality   quality)
{
  const gchar *str;

  switch (quality)
    {
    default:
    case GTK_PRINT_QUALITY_NORMAL:
      str = "normal";
      break;
    case GTK_PRINT_QUALITY_HIGH:
      str = "high";
      break;
    case GTK_PRINT_QUALITY_LOW:
      str = "low";
      break;
    case GTK_PRINT_QUALITY_DRAFT:
      str = "draft";
      break;
    }
  
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_QUALITY, str);
}

Matthias Clasen's avatar
Matthias Clasen committed
1002 1003 1004 1005 1006 1007
/**
 * gtk_print_settings_get_page_set:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PAGE_SET.
 * 
1008
 * Returns: the set of pages to print
Matthias Clasen's avatar
Matthias Clasen committed
1009 1010 1011
 *
 * Since: 2.10
 */
1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030
GtkPageSet
gtk_print_settings_get_page_set (GtkPrintSettings *settings)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_PAGE_SET);

  if (val == NULL || (strcmp (val, "all") == 0))
    return GTK_PAGE_SET_ALL;

  if (strcmp (val, "even") == 0)
    return GTK_PAGE_SET_EVEN;
  
  if (strcmp (val, "odd") == 0)
    return GTK_PAGE_SET_ODD;
  
  return GTK_PAGE_SET_ALL;
}

Matthias Clasen's avatar
Matthias Clasen committed
1031 1032 1033 1034 1035 1036 1037 1038 1039
/**
 * gtk_print_settings_set_page_set:
 * @settings: a #GtkPrintSettings
 * @page_set: a #GtkPageSet value
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PAGE_SET.
 * 
 * Since: 2.10
 */
1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062
void
gtk_print_settings_set_page_set (GtkPrintSettings *settings,
				 GtkPageSet        page_set)
{
  const gchar *str;

  switch (page_set)
    {
    default:
    case GTK_PAGE_SET_ALL:
      str = "all";
      break;
    case GTK_PAGE_SET_EVEN:
      str = "even";
      break;
    case GTK_PAGE_SET_ODD:
      str = "odd";
      break;
    }
  
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAGE_SET, str);
}

1063 1064 1065 1066 1067 1068
/**
 * gtk_print_settings_get_number_up_layout:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT.
 * 
1069
 * Returns: layout of page in number-up mode
1070 1071 1072 1073 1074 1075 1076 1077
 *
 * Since: 2.14
 */
GtkNumberUpLayout
gtk_print_settings_get_number_up_layout (GtkPrintSettings *settings)
{
  GtkNumberUpLayout layout;
  GtkTextDirection  text_direction;
1078 1079
  GEnumClass       *enum_class;
  GEnumValue       *enum_value;
1080 1081
  const gchar      *val;

1082 1083
  g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM);

1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094
  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT);
  text_direction = gtk_widget_get_default_direction ();

  if (text_direction == GTK_TEXT_DIR_LTR)
    layout = GTK_NUMBER_UP_LAYOUT_LEFT_TO_RIGHT_TOP_TO_BOTTOM;
  else
    layout = GTK_NUMBER_UP_LAYOUT_RIGHT_TO_LEFT_TOP_TO_BOTTOM;

  if (val == NULL)
    return layout;

1095 1096 1097 1098 1099
  enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
  enum_value = g_enum_get_value_by_nick (enum_class, val);
  if (enum_value)
    layout = enum_value->value;
  g_type_class_unref (enum_class);
1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116

  return layout;
}

/**
 * gtk_print_settings_set_number_up_layout:
 * @settings: a #GtkPrintSettings
 * @number_up_layout: a #GtkNumberUpLayout value
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT.
 * 
 * Since: 2.14
 */
void
gtk_print_settings_set_number_up_layout (GtkPrintSettings  *settings,
					 GtkNumberUpLayout  number_up_layout)
{
1117 1118
  GEnumClass *enum_class;
  GEnumValue *enum_value;
1119

1120 1121 1122 1123 1124 1125 1126 1127
  g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));

  enum_class = g_type_class_ref (GTK_TYPE_NUMBER_UP_LAYOUT);
  enum_value = g_enum_get_value (enum_class, number_up_layout);
  g_return_if_fail (enum_value != NULL);

  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_NUMBER_UP_LAYOUT, enum_value->value_nick);
  g_type_class_unref (enum_class);
1128 1129
}

Matthias Clasen's avatar
Matthias Clasen committed
1130
/**
Alexander Larsson's avatar
Alexander Larsson committed
1131
 * gtk_print_settings_get_n_copies:
Matthias Clasen's avatar
Matthias Clasen committed
1132 1133
 * @settings: a #GtkPrintSettings
 * 
Alexander Larsson's avatar
Alexander Larsson committed
1134
 * Gets the value of %GTK_PRINT_SETTINGS_N_COPIES.
Matthias Clasen's avatar
Matthias Clasen committed
1135
 * 
1136
 * Returns: the number of copies to print
Matthias Clasen's avatar
Matthias Clasen committed
1137 1138 1139
 *
 * Since: 2.10
 */
1140
gint
Alexander Larsson's avatar
Alexander Larsson committed
1141
gtk_print_settings_get_n_copies (GtkPrintSettings *settings)
1142
{
Alexander Larsson's avatar
Alexander Larsson committed
1143
  return gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_N_COPIES, 1);
1144 1145
}

Matthias Clasen's avatar
Matthias Clasen committed
1146
/**
Alexander Larsson's avatar
Alexander Larsson committed
1147
 * gtk_print_settings_set_n_copies:
Matthias Clasen's avatar
Matthias Clasen committed
1148 1149 1150
 * @settings: a #GtkPrintSettings
 * @num_copies: the number of copies 
 * 
Alexander Larsson's avatar
Alexander Larsson committed
1151
 * Sets the value of %GTK_PRINT_SETTINGS_N_COPIES.
Matthias Clasen's avatar
Matthias Clasen committed
1152 1153 1154
 * 
 * Since: 2.10
 */
1155
void
Alexander Larsson's avatar
Alexander Larsson committed
1156 1157
gtk_print_settings_set_n_copies (GtkPrintSettings *settings,
				 gint              num_copies)
1158
{
Alexander Larsson's avatar
Alexander Larsson committed
1159
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_N_COPIES,
1160 1161 1162
			      num_copies);
}

Matthias Clasen's avatar
Matthias Clasen committed
1163 1164 1165 1166 1167 1168
/**
 * gtk_print_settings_get_number_up:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_NUMBER_UP.
 * 
1169
 * Returns: the number of pages per sheet
Matthias Clasen's avatar
Matthias Clasen committed
1170 1171 1172
 *
 * Since: 2.10
 */
1173 1174 1175
gint
gtk_print_settings_get_number_up (GtkPrintSettings *settings)
{
1176
  return gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_NUMBER_UP, 1);
1177 1178
}

Matthias Clasen's avatar
Matthias Clasen committed
1179 1180 1181
/**
 * gtk_print_settings_set_number_up:
 * @settings: a #GtkPrintSettings
1182
 * @number_up: the number of pages per sheet 
Matthias Clasen's avatar
Matthias Clasen committed
1183 1184 1185 1186 1187
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_NUMBER_UP.
 * 
 * Since: 2.10
 */
1188 1189 1190 1191 1192 1193 1194 1195
void
gtk_print_settings_set_number_up (GtkPrintSettings *settings,
				  gint              number_up)
{
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_NUMBER_UP,
				number_up);
}

Matthias Clasen's avatar
Matthias Clasen committed
1196 1197 1198 1199 1200 1201
/**
 * gtk_print_settings_get_resolution:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_RESOLUTION.
 * 
1202
 * Returns: the resolution in dpi
Matthias Clasen's avatar
Matthias Clasen committed
1203 1204 1205
 *
 * Since: 2.10
 */
1206 1207 1208
gint
gtk_print_settings_get_resolution (GtkPrintSettings *settings)
{
1209
  return gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION, 300);
1210 1211
}

Matthias Clasen's avatar
Matthias Clasen committed
1212 1213 1214 1215 1216
/**
 * gtk_print_settings_set_resolution:
 * @settings: a #GtkPrintSettings
 * @resolution: the resolution in dpi
 * 
1217 1218 1219
 * Sets the values of %GTK_PRINT_SETTINGS_RESOLUTION,
 * %GTK_PRINT_SETTINGS_RESOLUTION_X and 
 * %GTK_PRINT_SETTINGS_RESOLUTION_Y.
Matthias Clasen's avatar
Matthias Clasen committed
1220 1221 1222
 * 
 * Since: 2.10
 */
1223 1224 1225 1226 1227 1228
void
gtk_print_settings_set_resolution (GtkPrintSettings *settings,
				   gint              resolution)
{
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION,
			      resolution);
1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION_X,
			      resolution);
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y,
			      resolution);
}

/**
 * gtk_print_settings_get_resolution_x:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_RESOLUTION_X.
 * 
1241
 * Returns: the horizontal resolution in dpi
1242 1243 1244 1245 1246 1247
 *
 * Since: 2.16
 */
gint
gtk_print_settings_get_resolution_x (GtkPrintSettings *settings)
{
1248
  return gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_X, 300);
1249 1250 1251 1252 1253 1254 1255 1256
}

/**
 * gtk_print_settings_get_resolution_y:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_RESOLUTION_Y.
 * 
1257
 * Returns: the vertical resolution in dpi
1258 1259 1260 1261 1262 1263
 *
 * Since: 2.16
 */
gint
gtk_print_settings_get_resolution_y (GtkPrintSettings *settings)
{
1264
  return gtk_print_settings_get_int_with_default (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y, 300);
1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297
}

/**
 * gtk_print_settings_set_resolution_xy:
 * @settings: a #GtkPrintSettings
 * @resolution_x: the horizontal resolution in dpi
 * @resolution_y: the vertical resolution in dpi
 * 
 * Sets the values of %GTK_PRINT_SETTINGS_RESOLUTION,
 * %GTK_PRINT_SETTINGS_RESOLUTION_X and
 * %GTK_PRINT_SETTINGS_RESOLUTION_Y.
 * 
 * Since: 2.16
 */
void
gtk_print_settings_set_resolution_xy (GtkPrintSettings *settings,
				      gint              resolution_x,
				      gint              resolution_y)
{
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION_X,
			      resolution_x);
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION_Y,
			      resolution_y);
  gtk_print_settings_set_int (settings, GTK_PRINT_SETTINGS_RESOLUTION,
			      resolution_x);
}

/**
 * gtk_print_settings_get_printer_lpi:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PRINTER_LPI.
 * 
1298
 * Returns: the resolution in lpi (lines per inch)
1299 1300 1301 1302 1303 1304
 *
 * Since: 2.16
 */
gdouble
gtk_print_settings_get_printer_lpi (GtkPrintSettings *settings)
{
1305
  return gtk_print_settings_get_double_with_default (settings, GTK_PRINT_SETTINGS_PRINTER_LPI, 150.0);
1306 1307 1308 1309 1310
}

/**
 * gtk_print_settings_set_printer_lpi:
 * @settings: a #GtkPrintSettings
Matthias Clasen's avatar
Matthias Clasen committed
1311
 * @lpi: the resolution in lpi (lines per inch)
1312
 * 
Matthias Clasen's avatar
Matthias Clasen committed
1313
 * Sets the value of %GTK_PRINT_SETTINGS_PRINTER_LPI.
1314 1315 1316 1317 1318 1319 1320 1321 1322
 * 
 * Since: 2.16
 */
void
gtk_print_settings_set_printer_lpi (GtkPrintSettings *settings,
				    gdouble           lpi)
{
  gtk_print_settings_set_double (settings, GTK_PRINT_SETTINGS_PRINTER_LPI,
			         lpi);
1323 1324
}

Matthias Clasen's avatar
Matthias Clasen committed
1325 1326 1327 1328 1329 1330
/**
 * gtk_print_settings_get_scale:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_SCALE.
 * 
1331
 * Returns: the scale in percent
Matthias Clasen's avatar
Matthias Clasen committed
1332 1333 1334
 *
 * Since: 2.10
 */
1335 1336 1337 1338 1339 1340 1341 1342
gdouble
gtk_print_settings_get_scale (GtkPrintSettings *settings)
{
  return gtk_print_settings_get_double_with_default (settings,
						     GTK_PRINT_SETTINGS_SCALE,
						     100.0);
}

Matthias Clasen's avatar
Matthias Clasen committed
1343 1344 1345
/**
 * gtk_print_settings_set_scale:
 * @settings: a #GtkPrintSettings
1346
 * @scale: the scale in percent
Matthias Clasen's avatar
Matthias Clasen committed
1347 1348 1349 1350 1351
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_SCALE.
 * 
 * Since: 2.10
 */
1352 1353 1354 1355 1356 1357 1358 1359
void
gtk_print_settings_set_scale (GtkPrintSettings *settings,
			      gdouble           scale)
{
  gtk_print_settings_set_double (settings, GTK_PRINT_SETTINGS_SCALE,
				 scale);
}

Matthias Clasen's avatar
Matthias Clasen committed
1360 1361 1362 1363 1364 1365
/**
 * gtk_print_settings_get_print_pages:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_PRINT_PAGES.
 * 
1366
 * Returns: which pages to print
Matthias Clasen's avatar
Matthias Clasen committed
1367 1368 1369
 *
 * Since: 2.10
 */
1370 1371 1372 1373 1374 1375 1376 1377 1378 1379
GtkPrintPages
gtk_print_settings_get_print_pages (GtkPrintSettings *settings)
{
  const gchar *val;

  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_PRINT_PAGES);

  if (val == NULL || (strcmp (val, "all") == 0))
    return GTK_PRINT_PAGES_ALL;

1380 1381 1382
  if (strcmp (val, "selection") == 0)
    return GTK_PRINT_PAGES_SELECTION;

1383 1384 1385 1386 1387 1388 1389 1390 1391
  if (strcmp (val, "current") == 0)
    return GTK_PRINT_PAGES_CURRENT;
  
  if (strcmp (val, "ranges") == 0)
    return GTK_PRINT_PAGES_RANGES;
  
  return GTK_PRINT_PAGES_ALL;
}

Matthias Clasen's avatar
Matthias Clasen committed
1392 1393 1394
/**
 * gtk_print_settings_set_print_pages:
 * @settings: a #GtkPrintSettings
1395
 * @pages: a #GtkPrintPages value
Matthias Clasen's avatar
Matthias Clasen committed
1396 1397 1398 1399 1400
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PRINT_PAGES.
 * 
 * Since: 2.10
 */
1401 1402
void
gtk_print_settings_set_print_pages (GtkPrintSettings *settings,
1403
				    GtkPrintPages     pages)
1404 1405 1406
{
  const gchar *str;

1407
  switch (pages)
1408 1409 1410 1411 1412 1413 1414 1415
    {
    default:
    case GTK_PRINT_PAGES_ALL:
      str = "all";
      break;
    case GTK_PRINT_PAGES_CURRENT:
      str = "current";
      break;
1416 1417 1418
    case GTK_PRINT_PAGES_SELECTION:
      str = "selection";
      break;
1419 1420 1421 1422 1423 1424 1425
    case GTK_PRINT_PAGES_RANGES:
      str = "ranges";
      break;
    }
  
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PRINT_PAGES, str);
}
1426

Matthias Clasen's avatar
Matthias Clasen committed
1427 1428 1429
/**
 * gtk_print_settings_get_page_ranges:
 * @settings: a #GtkPrintSettings
1430
 * @num_ranges: (out): return location for the length of the returned array
1431
 *
Matthias Clasen's avatar
Matthias Clasen committed
1432
 * Gets the value of %GTK_PRINT_SETTINGS_PAGE_RANGES.
1433
 *
1434
 * Returns: (array length=num_ranges) (transfer full): an array
1435
 *     of #GtkPageRanges.  Use g_free() to free the array when
1436
 *     it is no longer needed.
Matthias Clasen's avatar
Matthias Clasen committed
1437 1438 1439
 *
 * Since: 2.10
 */
1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483
GtkPageRange *
gtk_print_settings_get_page_ranges (GtkPrintSettings *settings,
				    gint             *num_ranges)
{
  const gchar *val;
  gchar **range_strs;
  GtkPageRange *ranges;
  gint i, n;
  
  val = gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_PAGE_RANGES);

  if (val == NULL)
    {
      *num_ranges = 0;
      return NULL;
    }
  
  range_strs = g_strsplit (val, ",", 0);

  for (i = 0; range_strs[i] != NULL; i++)
    ;

  n = i;

  ranges = g_new0 (GtkPageRange, n);

  for (i = 0; i < n; i++)
    {
      gint start, end;
      gchar *str;

      start = (gint)strtol (range_strs[i], &str, 10);
      end = start;

      if (*str == '-')
	{
	  str++;
	  end = (gint)strtol (str, NULL, 10);
	}

      ranges[i].start = start;
      ranges[i].end = end;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1484 1485
  g_strfreev (range_strs);

1486 1487 1488 1489
  *num_ranges = n;
  return ranges;
}

Matthias Clasen's avatar
Matthias Clasen committed
1490 1491 1492
/**
 * gtk_print_settings_set_page_ranges:
 * @settings: a #GtkPrintSettings
1493
 * @page_ranges: (array length=num_ranges): an array of #GtkPageRanges
Matthias Clasen's avatar
Matthias Clasen committed
1494 1495 1496 1497 1498 1499
 * @num_ranges: the length of @page_ranges
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_PAGE_RANGES.
 * 
 * Since: 2.10
 */
1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528
void
gtk_print_settings_set_page_ranges  (GtkPrintSettings *settings,
				     GtkPageRange     *page_ranges,
				     gint              num_ranges)
{
  GString *s;
  gint i;
  
  s = g_string_new ("");

  for (i = 0; i < num_ranges; i++)
    {
      if (page_ranges[i].start == page_ranges[i].end)
	g_string_append_printf (s, "%d", page_ranges[i].start);
      else
	g_string_append_printf (s, "%d-%d",
				page_ranges[i].start,
				page_ranges[i].end);
      if (i < num_ranges - 1)
	g_string_append (s, ",");
    }

  
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_PAGE_RANGES, 
			  s->str);

  g_string_free (s, TRUE);
}

Matthias Clasen's avatar
Matthias Clasen committed
1529 1530 1531 1532 1533 1534
/**
 * gtk_print_settings_get_default_source:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_DEFAULT_SOURCE.
 * 
1535
 * Returns: the default source
Matthias Clasen's avatar
Matthias Clasen committed
1536 1537 1538
 *
 * Since: 2.10
 */
1539
const gchar *
1540 1541 1542 1543 1544
gtk_print_settings_get_default_source (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE);
}

Matthias Clasen's avatar
Matthias Clasen committed
1545 1546 1547 1548 1549 1550 1551 1552 1553
/**
 * gtk_print_settings_set_default_source:
 * @settings: a #GtkPrintSettings
 * @default_source: the default source
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_DEFAULT_SOURCE.
 * 
 * Since: 2.10
 */
1554 1555 1556 1557 1558 1559 1560
void
gtk_print_settings_set_default_source (GtkPrintSettings *settings,
				       const gchar      *default_source)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_DEFAULT_SOURCE, default_source);
}
     
Matthias Clasen's avatar
Matthias Clasen committed
1561 1562 1563 1564 1565 1566 1567 1568
/**
 * gtk_print_settings_get_media_type:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_MEDIA_TYPE.
 *
 * The set of media types is defined in PWG 5101.1-2002 PWG.
 * 
1569
 * Returns: the media type
Matthias Clasen's avatar
Matthias Clasen committed
1570 1571 1572
 *
 * Since: 2.10
 */
1573
const gchar *
1574 1575 1576 1577 1578
gtk_print_settings_get_media_type (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_MEDIA_TYPE);
}

Matthias Clasen's avatar
Matthias Clasen committed
1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589
/**
 * gtk_print_settings_set_media_type:
 * @settings: a #GtkPrintSettings
 * @media_type: the media type
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_MEDIA_TYPE.
 * 
 * The set of media types is defined in PWG 5101.1-2002 PWG.
 *
 * Since: 2.10
 */
1590 1591 1592 1593 1594 1595 1596
void
gtk_print_settings_set_media_type (GtkPrintSettings *settings,
				   const gchar      *media_type)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_MEDIA_TYPE, media_type);
}

Matthias Clasen's avatar
Matthias Clasen committed
1597 1598 1599 1600 1601 1602
/**
 * gtk_print_settings_get_dither:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_DITHER.
 * 
1603
 * Returns: the dithering that is used
Matthias Clasen's avatar
Matthias Clasen committed
1604 1605 1606
 *
 * Since: 2.10
 */
1607
const gchar *
1608 1609 1610 1611 1612
gtk_print_settings_get_dither (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_DITHER);
}

Matthias Clasen's avatar
Matthias Clasen committed
1613 1614 1615 1616 1617 1618 1619 1620 1621
/**
 * gtk_print_settings_set_dither:
 * @settings: a #GtkPrintSettings
 * @dither: the dithering that is used
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_DITHER.
 *
 * Since: 2.10
 */
1622 1623 1624 1625 1626 1627 1628
void
gtk_print_settings_set_dither (GtkPrintSettings *settings,
			       const gchar      *dither)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_DITHER, dither);
}
     
Matthias Clasen's avatar
Matthias Clasen committed
1629 1630 1631 1632 1633 1634
/**
 * gtk_print_settings_get_finishings:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_FINISHINGS.
 * 
1635
 * Returns: the finishings
Matthias Clasen's avatar
Matthias Clasen committed
1636 1637 1638
 *
 * Since: 2.10
 */
1639 1640 1641 1642 1643 1644
const gchar *
gtk_print_settings_get_finishings (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_FINISHINGS);
}

Matthias Clasen's avatar
Matthias Clasen committed
1645 1646 1647 1648 1649 1650 1651 1652 1653
/**
 * gtk_print_settings_set_finishings:
 * @settings: a #GtkPrintSettings
 * @finishings: the finishings
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_FINISHINGS.
 *
 * Since: 2.10
 */
1654 1655 1656 1657 1658 1659 1660
void
gtk_print_settings_set_finishings (GtkPrintSettings *settings,
				   const gchar      *finishings)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_FINISHINGS, finishings);
}
     
Matthias Clasen's avatar
Matthias Clasen committed
1661 1662 1663 1664 1665 1666
/**
 * gtk_print_settings_get_output_bin:
 * @settings: a #GtkPrintSettings
 * 
 * Gets the value of %GTK_PRINT_SETTINGS_OUTPUT_BIN.
 * 
1667
 * Returns: the output bin
Matthias Clasen's avatar
Matthias Clasen committed
1668 1669 1670
 *
 * Since: 2.10
 */
1671
const gchar *
1672 1673 1674 1675 1676
gtk_print_settings_get_output_bin (GtkPrintSettings *settings)
{
  return gtk_print_settings_get (settings, GTK_PRINT_SETTINGS_OUTPUT_BIN);
}

Matthias Clasen's avatar
Matthias Clasen committed
1677 1678 1679 1680 1681 1682 1683 1684 1685
/**
 * gtk_print_settings_set_output_bin:
 * @settings: a #GtkPrintSettings
 * @output_bin: the output bin
 * 
 * Sets the value of %GTK_PRINT_SETTINGS_OUTPUT_BIN.
 *
 * Since: 2.10
 */
1686 1687 1688 1689 1690 1691
void
gtk_print_settings_set_output_bin (GtkPrintSettings *settings,
				   const gchar      *output_bin)
{
  gtk_print_settings_set (settings, GTK_PRINT_SETTINGS_OUTPUT_BIN, output_bin);
}
Matthias Clasen's avatar
Matthias Clasen committed
1692

1693 1694 1695
/**
 * gtk_print_settings_load_file:
 * @settings: a #GtkPrintSettings
1696
 * @file_name: (type filename): the filename to read the settings from
1697
 * @error: (allow-none): return location for errors, or %NULL
1698
 *
1699 1700
 * Reads the print settings from @file_name. If the file could not be loaded
 * then error is set to either a #GFileError or #GKeyFileError.
1701 1702
 * See gtk_print_settings_to_file().
 *
1703
 * Returns: %TRUE on success
1704
 *
1705
 * Since: 2.14
1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 1716 1717 1718 1719 1720 1721 1722 1723 1724 1725 1726 1727 1728
 */
gboolean
gtk_print_settings_load_file (GtkPrintSettings *settings,
                              const gchar      *file_name,
                              GError          **error)
{
  gboolean retval = FALSE;
  GKeyFile *key_file;

  g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
  g_return_val_if_fail (file_name != NULL, FALSE);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file_name, 0, error) &&
      gtk_print_settings_load_key_file (settings, key_file, NULL, error))
    retval = TRUE;

  g_key_file_free (key_file);

  return retval;
}

Matthias Clasen's avatar
Matthias Clasen committed
1729 1730
/**
 * gtk_print_settings_new_from_file:
1731
 * @file_name: (type filename): the filename to read the settings from
1732
 * @error: (allow-none): return location for errors, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
1733
 * 
1734
 * Reads the print settings from @file_name. Returns a new #GtkPrintSettings
1735 1736 1737
 * object with the restored settings, or %NULL if an error occurred. If the
 * file could not be loaded then error is set to either a #GFileError or
 * #GKeyFileError.  See gtk_print_settings_to_file().
Matthias Clasen's avatar
Matthias Clasen committed
1738
 *
1739
 * Returns: the restored #GtkPrintSettings
Matthias Clasen's avatar
Matthias Clasen committed
1740 1741 1742 1743 1744 1745 1746
 * 
 * Since: 2.12
 */
GtkPrintSettings *
gtk_print_settings_new_from_file (const gchar  *file_name,
			          GError      **error)
{
1747
  GtkPrintSettings *settings = gtk_print_settings_new ();
Matthias Clasen's avatar
Matthias Clasen committed
1748

1749
  if (!gtk_print_settings_load_file (settings, file_name, error))
Matthias Clasen's avatar
Matthias Clasen committed
1750
    {
1751 1752
      g_object_unref (settings);
      settings = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
1753 1754 1755 1756 1757 1758
    }

  return settings;
}

/**
1759 1760
 * gtk_print_settings_load_key_file:
 * @settings: a #GtkPrintSettings
Matthias Clasen's avatar
Matthias Clasen committed
1761
 * @key_file: the #GKeyFile to retrieve the settings from
1762
 * @group_name: (allow-none): the name of the group to use, or %NULL to use the default
1763
 *     “Print Settings”
1764
 * @error: (allow-none): return location for errors, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
1765
 * 
1766 1767 1768
 * Reads the print settings from the group @group_name in @key_file. If the
 * file could not be loaded then error is set to either a #GFileError or
 * #GKeyFileError.
Matthias Clasen's avatar
Matthias Clasen committed
1769
 *
1770
 * Returns: %TRUE on success
Matthias Clasen's avatar
Matthias Clasen committed
1771
 * 
1772
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
1773
 */
1774 1775 1776 1777 1778
gboolean
gtk_print_settings_load_key_file (GtkPrintSettings *settings,
				  GKeyFile         *key_file,
				  const gchar      *group_name,
				  GError          **error)
Matthias Clasen's avatar
Matthias Clasen committed
1779 1780 1781 1782 1783
{
  gchar **keys;
  gsize n_keys, i;
  GError *err = NULL;

1784 1785
  g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
  g_return_val_if_fail (key_file != NULL, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796

  if (!group_name)
    group_name = KEYFILE_GROUP_NAME;

  keys = g_key_file_get_keys (key_file,
			      group_name,
			      &n_keys,
			      &err);
  if (err != NULL)
    {
      g_propagate_error (error, err);
1797
      return FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
1798
    }
1799
   
Matthias Clasen's avatar
Matthias Clasen committed
1800 1801 1802 1803 1804
  for (i = 0 ; i < n_keys; ++i)
    {
      gchar *value;

      value = g_key_file_get_string (key_file,
1805
				     group_name,
Matthias Clasen's avatar
Matthias Clasen committed
1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816
				     keys[i],
				     NULL);
      if (!value)
        continue;

      gtk_print_settings_set (settings, keys[i], value);
      g_free (value);
    }

  g_strfreev (keys);

1817 1818 1819 1820 1821 1822
  return TRUE;
}

/**
 * gtk_print_settings_new_from_key_file:
 * @key_file: the #GKeyFile to retrieve the settings from
1823
 * @group_name: (allow-none): the name of the group to use, or %NULL to use
1824
 *     the default “Print Settings”
1825
 * @error: (allow-none): return location for errors, or %NULL
1826
 *
1827 1828 1829 1830
 * Reads the print settings from the group @group_name in @key_file.  Returns a
 * new #GtkPrintSettings object with the restored settings, or %NULL if an
 * error occurred. If the file could not be loaded then error is set to either
 * a #GFileError or #GKeyFileError.
1831
 *
1832
 * Returns: the restored #GtkPrintSettings
1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849
 *
 * Since: 2.12
 */
GtkPrintSettings *
gtk_print_settings_new_from_key_file (GKeyFile     *key_file,
				      const gchar  *group_name,
				      GError      **error)
{
  GtkPrintSettings *settings = gtk_print_settings_new ();

  if (!gtk_print_settings_load_key_file (settings, key_file,
                                         group_name, error))
    {
      g_object_unref (settings);
      settings = NULL;
    }

Matthias Clasen's avatar
Matthias Clasen committed
1850 1851 1852 1853 1854 1855
  return settings;
}

/**
 * gtk_print_settings_to_file:
 * @settings: a #GtkPrintSettings
1856
 * @file_name: (type filename): the file to save to
1857
 * @error: (allow-none): return location for errors, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
1858
 * 
1859 1860 1861
 * This function saves the print settings from @settings to @file_name. If the
 * file could not be loaded then error is set to either a #GFileError or
 * #GKeyFileError.
Matthias Clasen's avatar
Matthias Clasen committed
1862
 * 
1863
 * Returns: %TRUE on success
Matthias Clasen's avatar
Matthias Clasen committed
1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917
 *
 * Since: 2.12
 */
gboolean
gtk_print_settings_to_file (GtkPrintSettings  *settings,
			    const gchar       *file_name,
			    GError           **error)
{
  GKeyFile *key_file;
  gboolean retval = FALSE;
  char *data = NULL;
  gsize len;
  GError *err = NULL;

  g_return_val_if_fail (GTK_IS_PRINT_SETTINGS (settings), FALSE);
  g_return_val_if_fail (file_name != NULL, FALSE);

  key_file = g_key_file_new ();
  gtk_print_settings_to_key_file (settings, key_file, NULL);

  data = g_key_file_to_data (key_file, &len, &err);
  if (!data)
    goto out;

  retval = g_file_set_contents (file_name, data, len, &err);

out:
  if (err != NULL)
    g_propagate_error (error, err);

  g_key_file_free (key_file);
  g_free (data);

  return retval;
}

typedef struct {
  GKeyFile *key_file;
  const gchar *group_name;
} SettingsData;

static void
add_value_to_key_file (const gchar  *key,
		       const gchar  *value,
		       SettingsData *data)
{
  g_key_file_set_string (data->key_file, data->group_name, key, value);
}

/**
 * gtk_print_settings_to_key_file:
 * @settings: a #GtkPrintSettings
 * @key_file: the #GKeyFile to save the print settings to
 * @group_name: the group to add the settings to in @key_file, or 
1918
 *     %NULL to use the default “Print Settings”
Matthias Clasen's avatar
Matthias Clasen committed
1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943
 *
 * This function adds the print settings from @settings to @key_file.
 * 
 * Since: 2.12
 */
void
gtk_print_settings_to_key_file (GtkPrintSettings  *settings,
			        GKeyFile          *key_file,
				const gchar       *group_name)
{
  SettingsData data;

  g_return_if_fail (GTK_IS_PRINT_SETTINGS (settings));
  g_return_if_fail (key_file != NULL);

  if (!group_name)
    group_name = KEYFILE_GROUP_NAME;

  data.key_file = key_file;
  data.group_name = group_name;

  gtk_print_settings_foreach (settings,
			      (GtkPrintSettingsFunc) add_value_to_key_file,
			      &data);
}