gtkpapersize.c 22.1 KB
Newer Older
1 2 3
/* GTK - The GIMP Toolkit
 * gtkpapersize.c: Paper Size
 * Copyright (C) 2006, Red Hat, Inc.
4
 * Copyright © 2006, 2007 Christian Persch
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
 *
 * 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 <stdlib.h>
#include <locale.h>
#if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
#include <langinfo.h>
#endif

#include "gtkpapersize.h"
Matthias Clasen's avatar
Matthias Clasen committed
31
#include "gtkprintutils.h"
Matthias Clasen's avatar
Matthias Clasen committed
32
#include "gtkprintoperation.h"  /* for GtkPrintError */
33
#include "gtkintl.h"
34 35 36 37 38 39 40 41 42
#include "gtkalias.h"

#include "paper_names_offsets.c"

struct _GtkPaperSize
{
  const PaperInfo *info;

  /* If these are not set we fall back to info */
43 44 45
  gchar *name;
  gchar *display_name;
  gchar *ppd_name;
46
  
47
  gdouble width, height; /* Stored in mm */
48 49 50 51 52 53 54 55 56
  gboolean is_custom;
};

GType
gtk_paper_size_get_type (void)
{
  static GType our_type = 0;
  
  if (our_type == 0)
57
    our_type = g_boxed_type_register_static (I_("GtkPaperSize"),
58 59 60 61 62
					     (GBoxedCopyFunc)gtk_paper_size_copy,
					     (GBoxedFreeFunc)gtk_paper_size_free);
  return our_type;
}

63 64
static const PaperInfo *
lookup_paper_info (const gchar *name)
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
{
  int lower = 0;
  int upper = G_N_ELEMENTS (standard_names_offsets) - 1;
  int mid;
  int cmp;

  do 
    {
       mid = (lower + upper) / 2;
       cmp = strcmp (name, paper_names + standard_names_offsets[mid].name);
       if (cmp < 0)
         upper = mid - 1;
       else if (cmp > 0)
         lower = mid + 1;
       else
	 return &standard_names_offsets[mid];
    }
  while (lower <= upper);

  return NULL;
}

static gboolean
88 89 90
parse_media_size (const gchar *size,
		  gdouble     *width_mm, 
		  gdouble     *height_mm)
91 92 93 94 95 96 97 98 99 100 101 102 103 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
{
  const char *p;
  char *e;
  double short_dim, long_dim;

  p = size;
  
  short_dim = g_ascii_strtod (p, &e);

  if (p == e || *e != 'x')
    return FALSE;

  p = e + 1; /* Skip x */

  long_dim = g_ascii_strtod (p, &e);

  if (p == e)
    return FALSE;

  p = e;

  if (strcmp (p, "in") == 0)
    {
      short_dim = short_dim * MM_PER_INCH;
      long_dim = long_dim * MM_PER_INCH;
    }
  else if (strcmp (p, "mm") != 0)
    return FALSE;

  if (width_mm)
    *width_mm = short_dim;
  if (height_mm)
    *height_mm = long_dim;
  
  return TRUE;  
}

static gboolean
129 130 131 132
parse_full_media_size_name (const gchar  *full_name,
			    gchar       **name,
			    gdouble      *width_mm, 
			    gdouble      *height_mm)
133 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 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
{
  const char *p;
  const char *end_of_name;
  
  /* From the spec:
   media-size-self-describing-name =
        ( class-in "_" size-name "_" short-dim "x" long-dim "in" ) |
        ( class-mm "_" size-name "_" short-dim "x" long-dim "mm" )
   class-in = "custom" | "na" | "asme" | "roc" | "oe"
   class-mm = "custom" | "iso" | "jis" | "jpn" | "prc" | "om"
   size-name = ( lowalpha | digit ) *( lowalpha | digit | "-" )
   short-dim = dim
   long-dim = dim
   dim = integer-part [fraction-part] | "0" fraction-part
   integer-part = non-zero-digit *digit
   fraction-part = "." *digit non-zero-digit
   lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
              "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
              "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
   non-zero-digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
   digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
 */

  p = strchr (full_name, '_');
  if (p == NULL)
    return FALSE;

  p++; /* Skip _ */
  
  p = strchr (p, '_');
  if (p == NULL)
    return FALSE;

  end_of_name = p;

  p++; /* Skip _ */

  if (!parse_media_size (p, width_mm, height_mm))
    return FALSE;

  if (name)
    *name = g_strndup (full_name, end_of_name - full_name);
  
  return TRUE;  
}

static GtkPaperSize *
gtk_paper_size_new_from_info (const PaperInfo *info)
{
  GtkPaperSize *size;
  
184
  size = g_slice_new0 (GtkPaperSize);
185 186 187 188 189 190 191
  size->info = info;
  size->width = info->width;
  size->height = info->height;
  
  return size;
}

192 193 194 195 196
/**
 * gtk_paper_size_new:
 * @name: a paper size name, or %NULL
 * 
 * Creates a new #GtkPaperSize object by parsing a 
197
 * <ulink url="ftp://ftp.pwg.org/pub/pwg/candidates/cs-pwgmsn10-20020226-5101.1.pdf">PWG 5101.1-2002</ulink>
198 199 200 201 202 203 204 205 206 207
 * paper name. 
 *
 * If @name is %NULL, the default paper size is returned,
 * see gtk_paper_size_get_default().
 * 
 * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
 * to free it
 *
 * Since: 2.10
 */
208
GtkPaperSize *
209
gtk_paper_size_new (const gchar *name)
210 211 212 213
{
  GtkPaperSize *size;
  char *short_name;
  double width, height;
214
  const PaperInfo *info;
215 216 217 218 219 220

  if (name == NULL)
    name = gtk_paper_size_get_default ();
  
  if (parse_full_media_size_name (name, &short_name, &width, &height))
    {
221
      size = g_slice_new0 (GtkPaperSize);
222 223 224 225 226

      size->width = width;
      size->height = height;
      size->name = short_name;
      size->display_name = g_strdup (short_name);
227 228
      if (strncmp (short_name, "custom", 6) == 0)
        size->is_custom = TRUE;
229 230 231 232 233 234 235 236 237
    }
  else
    {
      info = lookup_paper_info (name);
      if (info != NULL)
	size = gtk_paper_size_new_from_info (info);
      else
	{
	  g_warning ("Unknown paper size %s\n", name);
238
	  size = g_slice_new0 (GtkPaperSize);
239 240 241 242 243 244 245 246 247 248 249
	  size->name = g_strdup (name);
	  size->display_name = g_strdup (name);
	  /* Default to A4 size */
	  size->width = 210;
	  size->height = 297;
	}
    }
  
  return size;
}

250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
/**
 * gtk_paper_size_new_from_ppd:
 * @ppd_name: a PPD paper name
 * @ppd_display_name: the corresponding human-readable name
 * @width: the paper width, in points
 * @height: the paper height in points
 * 
 * Creates a new #GtkPaperSize object by using 
 * PPD information. 
 * 
 * If @ppd_name is not a recognized PPD paper name, 
 * @ppd_display_name, @width and @height are used to 
 * construct a custom #GtkPaperSize object.
 *
 * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
 * to free it
 *
 * Since: 2.10
 */
269
GtkPaperSize *
270 271 272 273
gtk_paper_size_new_from_ppd (const gchar *ppd_name,
			     const gchar *ppd_display_name,
			     gdouble      width,
			     gdouble      height)
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
{
  char *name;
  const char *lookup_ppd_name;
  char *freeme;
  GtkPaperSize *size;
  int i;

  lookup_ppd_name = ppd_name;
  
  freeme = NULL;
  /* Strip out Traverse suffix in matching. */
  if (g_str_has_suffix (ppd_name, ".Transverse"))
    {
      lookup_ppd_name = freeme =
	g_strndup (ppd_name, strlen (ppd_name) - strlen (".Transverse"));
    }
  
  for (i = 0; i < G_N_ELEMENTS(standard_names_offsets); i++)
    {
      if (standard_names_offsets[i].ppd_name != -1 &&
	  strcmp (paper_names + standard_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
	{
	  size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
	  goto out;
	}
    }
  
  for (i = 0; i < G_N_ELEMENTS(extra_ppd_names_offsets); i++)
    {
      if (strcmp (paper_names + extra_ppd_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
	{
	  size = gtk_paper_size_new (paper_names + extra_ppd_names_offsets[i].standard_name);
	  goto out;
	}
    }

310
  name = g_strconcat ("ppd_", ppd_name, NULL);
311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  size = gtk_paper_size_new_custom (name, ppd_display_name, width, height, GTK_UNIT_POINTS);
  g_free (name);

 out:

  if (size->info == NULL ||
      size->info->ppd_name == -1 ||
      strcmp (paper_names + size->info->ppd_name, ppd_name) != 0)
    size->ppd_name = g_strdup (ppd_name);
  
  g_free (freeme);
  
  return size;
}

326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
/**
 * gtk_paper_size_new_custom:
 * @name: the paper name 
 * @display_name: the human-readable name
 * @width: the paper width, in units of @unit
 * @height: the paper height, in units of @unit
 * @unit: the unit for @width and @height
 * 
 * Creates a new #GtkPaperSize object with the
 * given parameters.
 * 
 * Return value: a new #GtkPaperSize object, use gtk_paper_size_free()
 * to free it
 *
 * Since: 2.10
 */
342
GtkPaperSize *
343 344 345 346 347
gtk_paper_size_new_custom (const gchar *name, 
			   const gchar *display_name,
			   gdouble      width, 
			   gdouble      height, 
			   GtkUnit      unit)
348 349 350 351 352
{
  GtkPaperSize *size;
  g_return_val_if_fail (name != NULL, NULL);
  g_return_val_if_fail (unit != GTK_UNIT_PIXEL, NULL);

353
  size = g_slice_new0 (GtkPaperSize);
354 355 356 357 358
  
  size->name = g_strdup (name);
  size->display_name = g_strdup (display_name);
  size->is_custom = TRUE;
  
Matthias Clasen's avatar
Matthias Clasen committed
359 360
  size->width = _gtk_print_convert_to_mm (width, unit);
  size->height = _gtk_print_convert_to_mm (height, unit);
361 362 363 364
  
  return size;
}

365 366 367 368 369 370 371 372 373 374
/**
 * gtk_paper_size_copy:
 * @other: a #GtkPaperSize
 * 
 * Copies an existing #GtkPaperSize.
 * 
 * Return value: a copy of @other
 *
 * Since: 2.10
 */
375 376 377 378 379
GtkPaperSize *
gtk_paper_size_copy (GtkPaperSize *other)
{
  GtkPaperSize *size;

380
  size = g_slice_new0 (GtkPaperSize);
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396

  size->info = other->info;
  if (other->name)
    size->name = g_strdup (other->name);
  if (other->display_name)
    size->display_name = g_strdup (other->display_name);
  if (other->ppd_name)
    size->ppd_name = g_strdup (other->ppd_name);
  
  size->width = other->width;
  size->height = other->height;
  size->is_custom = other->is_custom;

  return size;
}

397 398 399 400 401 402 403 404
/**
 * gtk_paper_size_free:
 * @size: a #GtkPaperSize
 * 
 * Free the given #GtkPaperSize object.
 *
 * Since: 2.10
 */
405 406 407 408 409 410
void
gtk_paper_size_free (GtkPaperSize *size)
{
  g_free (size->name);
  g_free (size->display_name);
  g_free (size->ppd_name);
411 412

  g_slice_free (GtkPaperSize, size);
413 414
}

415 416 417
/**
 * gtk_paper_size_is_equal:
 * @size1: a #GtkPaperSize object
418
 * @size2: another #GtkPaperSize object
419 420 421 422 423 424 425 426
 * 
 * Compares two #GtkPaperSize objects.
 * 
 * Return value: %TRUE, if @size1 and @size2 
 * represent the same paper size
 *
 * Since: 2.10
 */
427 428 429 430 431 432 433 434 435 436
gboolean
gtk_paper_size_is_equal (GtkPaperSize *size1,
			 GtkPaperSize *size2)
{
  if (size1->info != NULL && size2->info != NULL)
    return size1->info == size2->info;
  
  return strcmp (gtk_paper_size_get_name (size1),
		 gtk_paper_size_get_name (size2)) == 0;
}
437

438 439
GList * _gtk_load_custom_papers (void);

440
/**
441 442 443
 * gtk_paper_size_get_paper_sizes:
 * @include_custom: whether to include custom paper sizes
 *     as defined in the page setup dialog
444
 *
445
 * Creates a list of known paper sizes.
446 447 448 449 450 451 452
 * 
 * Return value: a newly allocated list of newly 
 *    allocated #GtkPaperSize objects
 *
 * Since: 2.12
 */
GList *
453
gtk_paper_size_get_paper_sizes (gboolean include_custom)
454 455 456
{
  GList *list = NULL;
  guint i;
457
#ifdef G_OS_UNIX		/* _gtk_load_custom_papers() only on Unix so far  */
458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
  if (include_custom)
    {
      GList *page_setups, *l;

      page_setups = _gtk_load_custom_papers ();
      for (l = page_setups; l != NULL; l = l->next)
        {
          GtkPageSetup *setup = (GtkPageSetup *) l->data;
          GtkPaperSize *size;

          size = gtk_page_setup_get_paper_size (setup);
          list = g_list_prepend (list, gtk_paper_size_copy (size));
        }

      g_list_foreach (page_setups, (GFunc) g_object_unref, NULL);
      g_list_free (page_setups);
    }
475
#endif
476 477 478 479 480 481 482 483
  for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); ++i)
    {
       GtkPaperSize *size;

       size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
       list = g_list_prepend (list, size);
    }

484
  return g_list_reverse (list);
485 486
}

487

488 489 490 491 492 493 494 495 496 497 498
/**
 * gtk_paper_size_get_name:
 * @size: a #GtkPaperSize object
 * 
 * Gets the name of the #GtkPaperSize.
 * 
 * Return value: the name of @size
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
499 500 501 502 503 504 505 506
gtk_paper_size_get_name (GtkPaperSize *size)
{
  if (size->name)
    return size->name;
  g_assert (size->info != NULL);
  return paper_names + size->info->name;
}

507 508 509 510 511 512 513 514 515 516 517
/**
 * gtk_paper_size_get_display_name:
 * @size: a #GtkPaperSize object
 * 
 * Gets the human-readable name of the #GtkPaperSize.
 * 
 * Return value: the human-readable name of @size
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
518 519
gtk_paper_size_get_display_name (GtkPaperSize *size)
{
520 521
  const gchar *display_name;

522 523
  if (size->display_name)
    return size->display_name;
524

525
  g_assert (size->info != NULL);
526 527

  display_name = paper_names + size->info->display_name;
528
  return g_strip_context (display_name, _(display_name));
529 530
}

531 532 533 534 535 536 537 538 539 540 541 542
/**
 * gtk_paper_size_get_ppd_name:
 * @size: a #GtkPaperSize object
 * 
 * Gets the PPD name of the #GtkPaperSize, which
 * may be %NULL.
 * 
 * Return value: the PPD name of @size
 *
 * Since: 2.10
 */
G_CONST_RETURN gchar *
543 544 545 546 547 548 549 550 551
gtk_paper_size_get_ppd_name (GtkPaperSize *size)
{
  if (size->ppd_name)
    return size->ppd_name;
  if (size->info)
    return paper_names + size->info->ppd_name;
  return NULL;
}

552 553 554 555 556 557 558 559 560 561 562 563 564 565 566
/**
 * gtk_paper_size_get_width:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the paper width of the #GtkPaperSize, in 
 * units of @unit.
 * 
 * Return value: the paper width 
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_width (GtkPaperSize *size, 
			  GtkUnit       unit)
567
{
Matthias Clasen's avatar
Matthias Clasen committed
568
  return _gtk_print_convert_from_mm (size->width, unit);
569
}
570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585

/**
 * gtk_paper_size_get_height:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the paper height of the #GtkPaperSize, in 
 * units of @unit.
 * 
 * Return value: the paper height 
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_height (GtkPaperSize *size, 
			   GtkUnit       unit)
586
{
Matthias Clasen's avatar
Matthias Clasen committed
587
  return _gtk_print_convert_from_mm (size->height, unit);
588 589
}

590 591 592 593 594 595 596 597
/**
 * gtk_paper_size_is_custom:
 * @size: a #GtkPaperSize object
 * 
 * Returns %TRUE if @size is not a standard paper size.
 * 
 * Return value: whether @size is a custom paper size.
 **/
598 599 600 601 602 603
gboolean
gtk_paper_size_is_custom (GtkPaperSize *size)
{
  return size->is_custom;
}

604 605 606 607 608 609 610 611 612 613 614
/**
 * gtk_paper_size_set_size:
 * @size: a custom #GtkPaperSize object
 * @width: the new width in units of @unit
 * @height: the new height in units of @unit
 * @unit: the unit for @width and @height
 * 
 * Changes the dimensions of a @size to @width x @height.
 *
 * Since: 2.10
 */
615
void
616 617 618 619
gtk_paper_size_set_size (GtkPaperSize *size, 
			 gdouble       width, 
			 gdouble       height, 
			 GtkUnit       unit)
620 621 622 623
{
  g_return_if_fail (size != NULL);
  g_return_if_fail (size->is_custom);

Matthias Clasen's avatar
Matthias Clasen committed
624 625
  size->width = _gtk_print_convert_to_mm (width, unit);
  size->height = _gtk_print_convert_to_mm (height, unit);
626 627 628 629 630
}

#define NL_PAPER_GET(x)         \
  ((union { char *string; unsigned int word; })nl_langinfo(x)).word

631 632 633 634 635 636 637 638 639 640 641 642
/**
 * gtk_paper_size_get_default:
 *
 * Returns the name of the default paper size, which 
 * depends on the current locale.  
 * 
 * Return value: the name of the default paper size. The string
 * is owned by GTK+ and should not be modified.
 * 
 * Since: 2.10
 */
G_CONST_RETURN gchar *
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 697
gtk_paper_size_get_default (void)
{
  char *locale, *freeme = NULL;
  const char *paper_size;

#if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
  {
    int width = NL_PAPER_GET (_NL_PAPER_WIDTH);
    int height = NL_PAPER_GET (_NL_PAPER_HEIGHT);
    
    if (width == 210 && height == 297)
      return GTK_PAPER_NAME_A4;
    
    if (width == 216 && height == 279)
      return GTK_PAPER_NAME_LETTER;
  }
#endif

#ifdef G_OS_WIN32
  freeme = locale = g_win32_getlocale ();
#elif defined(LC_PAPER)
  locale = setlocale(LC_PAPER, NULL);
#else
  locale = setlocale(LC_MESSAGES, NULL);
#endif

  if (!locale)
    return GTK_PAPER_NAME_A4;

  if (g_str_has_prefix (locale, "en_CA") ||
      g_str_has_prefix (locale, "en_US") ||
      g_str_has_prefix (locale, "es_PR") ||
      g_str_has_prefix (locale, "es_US"))
    paper_size = GTK_PAPER_NAME_LETTER;
  else
    paper_size = GTK_PAPER_NAME_A4;

  g_free (freeme);
  return paper_size;
}

/* These get the default margins used for the paper size. Its
 * larger than most printers margins, so that it will be within
 * the imageble area on any printer.
 *
 * I've taken the actual values used from the OSX page setup dialog.
 * I'm not sure exactly where they got these values for, but might
 * correspond to this (from ghostscript docs):
 * 
 * All DeskJets have 0.5 inches (1.27cm) of unprintable bottom margin,
 * due to the mechanical arrangement used to grab the paper. Side margins
 * are approximately 0.25 inches (0.64cm) for U.S. letter paper, and 0.15
 * inches (0.38cm) for A4.
 */

698 699 700 701 702 703 704 705 706 707 708 709 710 711
/**
 * gtk_paper_size_get_default_top_margin:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the default top margin for the #GtkPaperSize.
 * 
 * Return value: the default top margin
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_default_top_margin (GtkPaperSize *size, 
				       GtkUnit       unit)
712
{
713
  gdouble margin;
714

Matthias Clasen's avatar
Matthias Clasen committed
715 716
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
717 718
}

719 720 721 722 723 724 725 726 727 728 729 730 731 732
/**
 * gtk_paper_size_get_default_bottom_margin:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the default bottom margin for the #GtkPaperSize.
 * 
 * Return value: the default bottom margin
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_default_bottom_margin (GtkPaperSize *size, 
					  GtkUnit       unit)
733
{
734 735
  gdouble margin;
  const gchar *name;
736

Matthias Clasen's avatar
Matthias Clasen committed
737
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
738 739 740 741 742

  name = gtk_paper_size_get_name (size);
  if (strcmp (name, "na_letter") == 0 ||
      strcmp (name, "na_legal") == 0 ||
      strcmp (name, "iso_a4") == 0)
Matthias Clasen's avatar
Matthias Clasen committed
743
    margin = _gtk_print_convert_to_mm (0.56, GTK_UNIT_INCH);
744
  
Matthias Clasen's avatar
Matthias Clasen committed
745
  return _gtk_print_convert_from_mm (margin, unit);
746 747
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761
/**
 * gtk_paper_size_get_default_left_margin:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the default left margin for the #GtkPaperSize.
 * 
 * Return value: the default left margin
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_default_left_margin (GtkPaperSize *size, 
					GtkUnit       unit)
762
{
763
  gdouble margin;
764

Matthias Clasen's avatar
Matthias Clasen committed
765 766
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
767 768
}

769 770 771 772 773 774 775 776 777 778 779 780 781 782
/**
 * gtk_paper_size_get_default_right_margin:
 * @size: a #GtkPaperSize object
 * @unit: the unit for the return value
 * 
 * Gets the default right margin for the #GtkPaperSize.
 * 
 * Return value: the default right margin
 *
 * Since: 2.10
 */
gdouble
gtk_paper_size_get_default_right_margin (GtkPaperSize *size, 
					 GtkUnit       unit)
783
{
784
  gdouble margin;
785

Matthias Clasen's avatar
Matthias Clasen committed
786 787
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
788 789
}

Matthias Clasen's avatar
Matthias Clasen committed
790 791 792 793 794 795 796 797
/**
 * gtk_paper_size_new_from_key_file:
 * @key_file: the #GKeyFile to retrieve the papersize from
 * @group_name: the name ofthe group in the key file to read,
 *     or %NULL to read the first group
 * @error: return location for an error, or %NULL
 *
 * Reads a paper size from the group @group_name in the key file
Matthias Clasen's avatar
Matthias Clasen committed
798 799 800 801
 * @key_file. 
 *
 * Returns: a new #GtkPaperSize object with the restored
 *          paper size, or %NULL if an error occurred.
Matthias Clasen's avatar
Matthias Clasen committed
802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
 *
 * Since: 2.12
 */
GtkPaperSize *
gtk_paper_size_new_from_key_file (GKeyFile    *key_file,
				  const gchar *group_name,
				  GError     **error)
{
  GtkPaperSize *paper_size = NULL;
  char *name = NULL, *ppd_name = NULL, *display_name = NULL, *freeme = NULL;
  gdouble width, height;
  gboolean retval = TRUE;
  GError *err = NULL;

  g_return_val_if_fail (key_file != NULL, NULL);

  if (!group_name)
    group_name = freeme = g_key_file_get_start_group (key_file);
  if (!group_name || !g_key_file_has_group (key_file, group_name))
    {
822 823 824 825
      g_set_error_literal (error,
                           GTK_PRINT_ERROR,
                           GTK_PRINT_ERROR_INVALID_FILE,
                           _("Not a valid page setup file"));
Matthias Clasen's avatar
Matthias Clasen committed
826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861
      retval = FALSE;
      goto out;
    }

#define GET_DOUBLE(kf, group, name, v) \
  v = g_key_file_get_double (kf, group, name, &err); \
  if (err != NULL) \
    {\
      g_propagate_error (error, err);\
      retval = FALSE;\
      goto out;\
    }

  GET_DOUBLE (key_file, group_name, "Width", width);
  GET_DOUBLE (key_file, group_name, "Height", height);

#undef GET_DOUBLE

  name = g_key_file_get_string (key_file, group_name,
				"Name", NULL);
  ppd_name = g_key_file_get_string (key_file, group_name,
				    "PPDName", NULL);
  display_name = g_key_file_get_string (key_file, group_name,
					"DisplayName", NULL);
  /* Fallback for old ~/.gtk-custom-paper entries */
  if (!display_name)
    display_name = g_strdup (name);

  if (ppd_name != NULL)
    paper_size = gtk_paper_size_new_from_ppd (ppd_name, display_name,
			                      width, height);
  else if (name != NULL)
    paper_size = gtk_paper_size_new_custom (name, display_name,
					    width, height, GTK_UNIT_MM);
  else
    {
862 863 864 865
      g_set_error_literal (error,
                           GTK_PRINT_ERROR,
                           GTK_PRINT_ERROR_INVALID_FILE,
                           _("Not a valid page setup file"));
Matthias Clasen's avatar
Matthias Clasen committed
866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881
      retval = FALSE;
      goto out;
    }
  g_assert (paper_size != NULL);

out:
  g_free (ppd_name);
  g_free (name);
  g_free (display_name);
  g_free (freeme);

  return paper_size;
}

/**
 * gtk_paper_size_to_key_file:
Matthias Clasen's avatar
Matthias Clasen committed
882
 * @size: a #GtkPaperSize
Matthias Clasen's avatar
Matthias Clasen committed
883 884 885
 * @key_file: the #GKeyFile to save the paper size to
 * @group_name: the group to add the settings to in @key_file
 *
Matthias Clasen's avatar
Matthias Clasen committed
886
 * This function adds the paper size from @size to @key_file.
Matthias Clasen's avatar
Matthias Clasen committed
887 888 889 890
 *
 * Since: 2.12
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
891
gtk_paper_size_to_key_file (GtkPaperSize *size,
Matthias Clasen's avatar
Matthias Clasen committed
892 893 894 895 896
			    GKeyFile     *key_file,
			    const gchar  *group_name)
{
  const char *name, *ppd_name, *display_name;

Matthias Clasen's avatar
Matthias Clasen committed
897
  g_return_if_fail (size != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
898 899
  g_return_if_fail (key_file != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
900 901 902
  name = gtk_paper_size_get_name (size);
  display_name = gtk_paper_size_get_display_name (size);
  ppd_name = gtk_paper_size_get_ppd_name (size);
Matthias Clasen's avatar
Matthias Clasen committed
903 904 905 906 907 908 909 910 911 912 913 914 915

  if (ppd_name != NULL) 
    g_key_file_set_string (key_file, group_name,
			   "PPDName", ppd_name);
  else
    g_key_file_set_string (key_file, group_name,
			   "Name", name);

  if (display_name) 
    g_key_file_set_string (key_file, group_name,
			   "DisplayName", display_name);

  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
916
			 "Width", gtk_paper_size_get_width (size, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
917
  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
918
			 "Height", gtk_paper_size_get_height (size, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
919 920
}

921 922 923

#define __GTK_PAPER_SIZE_C__
#include "gtkaliasdef.c"