gtkpapersize.c 28.1 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3
 * 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
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
Javier Jardón's avatar
Javier Jardón committed
17
 * License along with this library. If not, see <http://www.gnu.org/licenses/>.
18 19 20 21 22 23 24 25 26
 */

#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
27
#include <math.h>
28 29

#include "gtkpapersize.h"
Matthias Clasen's avatar
Matthias Clasen committed
30
#include "gtkprintutils.h"
Matthias Clasen's avatar
Matthias Clasen committed
31
#include "gtkprintoperation.h"  /* for GtkPrintError */
32
#include "gtkintl.h"
33

34 35
/* _gtk_load_custom_papers() only on Unix so far  */
#ifdef G_OS_UNIX
36 37 38
#include "gtkcustompaperunixdialog.h"
#endif

39 40
#include "paper_names_offsets.c"

41 42 43 44 45 46 47 48

/**
 * SECTION:gtkpapersize
 * @Short_description: Support for named paper sizes
 * @Title: GtkPaperSize
 * @See_also:#GtkPageSetup
 *
 * GtkPaperSize handles paper sizes. It uses the standard called
49
 * [PWG 5101.1-2002 PWG: Standard for Media Standardized Names](http://www.pwg.org/standards.html)
50 51 52 53 54 55
 * to name the paper sizes (and to get the data for the page sizes).
 * In addition to standard paper sizes, GtkPaperSize allows to
 * construct custom paper sizes with arbitrary dimensions.
 *
 * The #GtkPaperSize object stores not only the dimensions (width
 * and height) of a paper size and its name, it also provides
56
 * default [print margins][print-margins].
57 58 59 60 61
 *
 * Printing support has been added in GTK+ 2.10.
 */


62 63 64 65 66
struct _GtkPaperSize
{
  const PaperInfo *info;

  /* If these are not set we fall back to info */
67 68 69
  gchar *name;
  gchar *display_name;
  gchar *ppd_name;
70

71
  gdouble width, height; /* Stored in mm */
72
  gboolean is_custom;
73
  gboolean is_ipp;
74 75
};

76 77 78
G_DEFINE_BOXED_TYPE (GtkPaperSize, gtk_paper_size,
                     gtk_paper_size_copy,
                     gtk_paper_size_free)
79

80 81
static const PaperInfo *
lookup_paper_info (const gchar *name)
82 83 84 85 86 87
{
  int lower = 0;
  int upper = G_N_ELEMENTS (standard_names_offsets) - 1;
  int mid;
  int cmp;

88
  do
89 90 91 92 93 94 95 96
    {
       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
97
         return &standard_names_offsets[mid];
98 99 100 101 102 103 104
    }
  while (lower <= upper);

  return NULL;
}

static gboolean
105
parse_media_size (const gchar *size,
106 107
                  gdouble     *width_mm,
                  gdouble     *height_mm)
108 109 110 111 112 113
{
  const char *p;
  char *e;
  double short_dim, long_dim;

  p = size;
114

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  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;
141 142

  return TRUE;
143 144 145
}

static gboolean
146
parse_full_media_size_name (const gchar  *full_name,
147 148 149
                            gchar       **name,
                            gdouble      *width_mm,
                            gdouble      *height_mm)
150 151 152
{
  const char *p;
  const char *end_of_name;
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
  /* 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 _ */
178

179 180 181 182 183 184 185 186 187 188 189 190 191
  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);
192 193

  return TRUE;
194 195 196 197 198 199
}

static GtkPaperSize *
gtk_paper_size_new_from_info (const PaperInfo *info)
{
  GtkPaperSize *size;
200

201
  size = g_slice_new0 (GtkPaperSize);
202 203 204
  size->info = info;
  size->width = info->width;
  size->height = info->height;
205

206 207 208
  return size;
}

209 210
/**
 * gtk_paper_size_new:
211 212 213
 * @name: (allow-none): a paper size name, or %NULL
 *
 * Creates a new #GtkPaperSize object by parsing a
214
 * [PWG 5101.1-2002](ftp://ftp.pwg.org/pub/pwg/candidates/cs-pwgmsn10-20020226-5101.1.pdf)
215
 * paper name.
216 217 218
 *
 * If @name is %NULL, the default paper size is returned,
 * see gtk_paper_size_get_default().
219
 *
220
 * Returns: a new #GtkPaperSize, use gtk_paper_size_free()
221 222 223 224
 * to free it
 *
 * Since: 2.10
 */
225
GtkPaperSize *
226
gtk_paper_size_new (const gchar *name)
227 228 229 230
{
  GtkPaperSize *size;
  char *short_name;
  double width, height;
231
  const PaperInfo *info;
232 233 234

  if (name == NULL)
    name = gtk_paper_size_get_default ();
235

236 237
  if (parse_full_media_size_name (name, &short_name, &width, &height))
    {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
      info = lookup_paper_info (short_name);
      if (info != NULL && info->width == width && info->height == height)
        {
          size = gtk_paper_size_new_from_info (info);
          g_free (short_name);
        }
      else
        {
          size = g_slice_new0 (GtkPaperSize);

          size->width = width;
          size->height = height;
          size->name = short_name;
          size->display_name = g_strdup (short_name);
          if (strncmp (short_name, "custom", 6) == 0)
            size->is_custom = TRUE;
        }
255 256 257 258 259
    }
  else
    {
      info = lookup_paper_info (name);
      if (info != NULL)
260
        size = gtk_paper_size_new_from_info (info);
261
      else
262 263 264 265 266 267 268 269 270
        {
          g_warning ("Unknown paper size %s\n", name);
          size = g_slice_new0 (GtkPaperSize);
          size->name = g_strdup (name);
          size->display_name = g_strdup (name);
          /* Default to A4 size */
          size->width = 210;
          size->height = 297;
        }
271
    }
272

273 274 275
  return size;
}

276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297
static gchar *
improve_displayname (const gchar *name)
{
  gchar *p, *p1, *p2, *s;

  p = strrchr (name, 'x');
  if (p && p != name &&
      g_ascii_isdigit (*(p - 1)) &&
      g_ascii_isdigit (*(p + 1)))
    {
      p1 = g_strndup (name, p - name);
      p2 = g_strdup (p + 1);
      s = g_strconcat (p1, "×", p2, NULL);
      g_free (p1);
      g_free (p2);
    }
  else
    s = g_strdup (name);

  return s;
}

298 299 300 301 302 303
/**
 * 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
304 305 306 307 308 309
 *
 * 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
310 311
 * construct a custom #GtkPaperSize object.
 *
312
 * Returns: a new #GtkPaperSize, use gtk_paper_size_free()
313 314 315 316
 * to free it
 *
 * Since: 2.10
 */
317
GtkPaperSize *
318
gtk_paper_size_new_from_ppd (const gchar *ppd_name,
319 320 321
                             const gchar *ppd_display_name,
                             gdouble      width,
                             gdouble      height)
322 323 324 325 326 327
{
  char *name;
  const char *lookup_ppd_name;
  char *freeme;
  GtkPaperSize *size;
  int i;
328
  char *display_name;
329 330

  lookup_ppd_name = ppd_name;
331

332 333 334 335 336
  freeme = NULL;
  /* Strip out Traverse suffix in matching. */
  if (g_str_has_suffix (ppd_name, ".Transverse"))
    {
      lookup_ppd_name = freeme =
337
        g_strndup (ppd_name, strlen (ppd_name) - strlen (".Transverse"));
338
    }
339

340
  for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); i++)
341 342
    {
      if (standard_names_offsets[i].ppd_name != -1 &&
343 344 345 346 347
          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;
        }
348
    }
349

350
  for (i = 0; i < G_N_ELEMENTS (extra_ppd_names_offsets); i++)
351 352
    {
      if (strcmp (paper_names + extra_ppd_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
353 354 355 356
        {
          size = gtk_paper_size_new (paper_names + extra_ppd_names_offsets[i].standard_name);
          goto out;
        }
357 358
    }

359
  name = g_strconcat ("ppd_", ppd_name, NULL);
360 361 362
  display_name = improve_displayname (ppd_display_name);
  size = gtk_paper_size_new_custom (name, display_name, width, height, GTK_UNIT_POINTS);
  g_free (display_name);
363 364 365 366 367 368 369 370
  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);
371

372
  g_free (freeme);
373

374 375 376
  return size;
}

377 378 379 380 381
/* Tolerance of paper size in points according to PostScript Language Reference */
#define PAPER_SIZE_TOLERANCE 5

/**
 * gtk_paper_size_new_from_ipp:
382
 * @ipp_name: an IPP paper name
383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445
 * @width: the paper width, in points
 * @height: the paper height in points
 *
 * Creates a new #GtkPaperSize object by using
 * IPP information.
 *
 * If @ipp_name is not a recognized paper name,
 * @width and @height are used to
 * construct a custom #GtkPaperSize object.
 *
 * Returns: a new #GtkPaperSize, use gtk_paper_size_free()
 * to free it
 *
 * Since: 3.16
 */
GtkPaperSize *
gtk_paper_size_new_from_ipp (const gchar *ipp_name,
                             gdouble      width,
                             gdouble      height)
{
  GtkPaperSize *size;
  const gchar  *name = NULL;
  gboolean      found = FALSE;
  float         x_dimension;
  float         y_dimension;
  gchar        *display_name = NULL;
  gint          i;

  /* Find paper size according to its name */
  for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); i++)
    {
      if (standard_names_offsets[i].name != -1)
        name = paper_names + standard_names_offsets[i].name;
      if (name != NULL &&
          /* Given paper size name is equal to a name
             from the standard paper size names list. */
          ((g_strcmp0 (ipp_name, name) == 0) ||
           /* Given paper size name is prefixed by a name
              from the standard paper size names list +
              it consists of size in its name (e.g. iso_a4_210x297mm). */
            (g_str_has_prefix (ipp_name, name) &&
             strlen (ipp_name) > strlen (name) + 2 &&
             ipp_name[strlen (ipp_name)] == '_' &&
             g_ascii_isdigit (ipp_name[strlen (ipp_name) + 1]) &&
             (g_str_has_suffix (ipp_name, "mm") ||
              g_str_has_suffix (ipp_name, "in")))))
        {
          display_name = g_strdup (g_dpgettext2 (GETTEXT_PACKAGE,
                                                 "paper size",
                                                 paper_names + standard_names_offsets[i].display_name));
          found = TRUE;
          break;
        }
    }

  /* Find paper size according to its size */
  if (display_name == NULL)
    {
      for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); i++)
        {
          x_dimension = _gtk_print_convert_from_mm (standard_names_offsets[i].width, GTK_UNIT_POINTS);
          y_dimension = _gtk_print_convert_from_mm (standard_names_offsets[i].height, GTK_UNIT_POINTS);

446 447
          if (fabs (x_dimension - width) <= PAPER_SIZE_TOLERANCE &&
              fabs (y_dimension - height) <= PAPER_SIZE_TOLERANCE)
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470
            {
              display_name = g_strdup (g_dpgettext2 (GETTEXT_PACKAGE,
                                                     "paper size",
                                                     paper_names + standard_names_offsets[i].display_name));
              found = TRUE;
              break;
            }
        }
    }

  /* Fallback to name of the paper size as given in "ipp_name" parameter */
  if (display_name == NULL)
    display_name = g_strdup (ipp_name);

  size = gtk_paper_size_new_custom (ipp_name, display_name, width, height, GTK_UNIT_POINTS);
  size->is_custom = !found;
  size->is_ipp = found;

  g_free (display_name);

  return size;
}

471 472
/**
 * gtk_paper_size_new_custom:
473
 * @name: the paper name
474 475 476
 * @display_name: the human-readable name
 * @width: the paper width, in units of @unit
 * @height: the paper height, in units of @unit
477
 * @unit: the unit for @width and @height. not %GTK_UNIT_NONE.
478
 *
479 480
 * Creates a new #GtkPaperSize object with the
 * given parameters.
481
 *
482
 * Returns: a new #GtkPaperSize object, use gtk_paper_size_free()
483 484 485 486
 * to free it
 *
 * Since: 2.10
 */
487
GtkPaperSize *
488 489 490 491 492
gtk_paper_size_new_custom (const gchar *name,
                           const gchar *display_name,
                           gdouble      width,
                           gdouble      height,
                           GtkUnit      unit)
493 494 495
{
  GtkPaperSize *size;
  g_return_val_if_fail (name != NULL, NULL);
496
  g_return_val_if_fail (unit != GTK_UNIT_NONE, NULL);
497

498
  size = g_slice_new0 (GtkPaperSize);
499

500 501 502
  size->name = g_strdup (name);
  size->display_name = g_strdup (display_name);
  size->is_custom = TRUE;
503

Matthias Clasen's avatar
Matthias Clasen committed
504 505
  size->width = _gtk_print_convert_to_mm (width, unit);
  size->height = _gtk_print_convert_to_mm (height, unit);
506

507 508 509
  return size;
}

510 511 512
/**
 * gtk_paper_size_copy:
 * @other: a #GtkPaperSize
513
 *
514
 * Copies an existing #GtkPaperSize.
515
 *
516
 * Returns: a copy of @other
517 518 519
 *
 * Since: 2.10
 */
520 521 522 523 524
GtkPaperSize *
gtk_paper_size_copy (GtkPaperSize *other)
{
  GtkPaperSize *size;

525
  size = g_slice_new0 (GtkPaperSize);
526 527 528 529 530 531 532 533

  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);
534

535 536 537
  size->width = other->width;
  size->height = other->height;
  size->is_custom = other->is_custom;
538
  size->is_ipp = other->is_ipp;
539 540 541 542

  return size;
}

543 544 545
/**
 * gtk_paper_size_free:
 * @size: a #GtkPaperSize
546
 *
547 548 549 550
 * Free the given #GtkPaperSize object.
 *
 * Since: 2.10
 */
551 552 553 554 555 556
void
gtk_paper_size_free (GtkPaperSize *size)
{
  g_free (size->name);
  g_free (size->display_name);
  g_free (size->ppd_name);
557 558

  g_slice_free (GtkPaperSize, size);
559 560
}

561 562 563
/**
 * gtk_paper_size_is_equal:
 * @size1: a #GtkPaperSize object
564
 * @size2: another #GtkPaperSize object
565
 *
566
 * Compares two #GtkPaperSize objects.
567
 *
568
 * Returns: %TRUE, if @size1 and @size2
569 570 571 572
 * represent the same paper size
 *
 * Since: 2.10
 */
573 574
gboolean
gtk_paper_size_is_equal (GtkPaperSize *size1,
575
                         GtkPaperSize *size2)
576 577 578
{
  if (size1->info != NULL && size2->info != NULL)
    return size1->info == size2->info;
579

580
  return strcmp (gtk_paper_size_get_name (size1),
581
                 gtk_paper_size_get_name (size2)) == 0;
582
}
583

584
/**
585 586 587
 * gtk_paper_size_get_paper_sizes:
 * @include_custom: whether to include custom paper sizes
 *     as defined in the page setup dialog
588
 *
589
 * Creates a list of known paper sizes.
590
 *
591
 * Returns:  (element-type GtkPaperSize) (transfer full): a newly allocated list of newly
592 593 594 595 596
 *    allocated #GtkPaperSize objects
 *
 * Since: 2.12
 */
GList *
597
gtk_paper_size_get_paper_sizes (gboolean include_custom)
598 599 600
{
  GList *list = NULL;
  guint i;
601 602
/* _gtk_load_custom_papers() only on Unix so far  */
#ifdef G_OS_UNIX
603 604 605 606 607 608 609 610 611 612 613 614 615 616
  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));
        }

617
      g_list_free_full (page_setups, g_object_unref);
618
    }
619
#endif
620 621 622 623 624 625 626 627
  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);
    }

628
  return g_list_reverse (list);
629 630
}

631

632 633 634
/**
 * gtk_paper_size_get_name:
 * @size: a #GtkPaperSize object
635
 *
636
 * Gets the name of the #GtkPaperSize.
637
 *
638
 * Returns: the name of @size
639 640 641
 *
 * Since: 2.10
 */
642
const gchar *
643 644 645 646 647 648 649 650
gtk_paper_size_get_name (GtkPaperSize *size)
{
  if (size->name)
    return size->name;
  g_assert (size->info != NULL);
  return paper_names + size->info->name;
}

651 652 653
/**
 * gtk_paper_size_get_display_name:
 * @size: a #GtkPaperSize object
654
 *
655
 * Gets the human-readable name of the #GtkPaperSize.
656
 *
657
 * Returns: the human-readable name of @size
658 659 660
 *
 * Since: 2.10
 */
661
const gchar *
662 663
gtk_paper_size_get_display_name (GtkPaperSize *size)
{
664 665
  const gchar *display_name;

666 667
  if (size->display_name)
    return size->display_name;
668

669
  g_assert (size->info != NULL);
670 671

  display_name = paper_names + size->info->display_name;
672
  return g_dpgettext2 (GETTEXT_PACKAGE, "paper size", display_name);
673 674
}

675 676 677
/**
 * gtk_paper_size_get_ppd_name:
 * @size: a #GtkPaperSize object
678
 *
679 680
 * Gets the PPD name of the #GtkPaperSize, which
 * may be %NULL.
681
 *
682
 * Returns: the PPD name of @size
683 684 685
 *
 * Since: 2.10
 */
686
const gchar *
687 688 689 690 691 692 693 694 695
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;
}

696 697 698
/**
 * gtk_paper_size_get_width:
 * @size: a #GtkPaperSize object
699
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
700 701
 *
 * Gets the paper width of the #GtkPaperSize, in
702
 * units of @unit.
703
 *
704
 * Returns: the paper width
705 706 707 708
 *
 * Since: 2.10
 */
gdouble
709 710
gtk_paper_size_get_width (GtkPaperSize *size,
                          GtkUnit       unit)
711
{
Matthias Clasen's avatar
Matthias Clasen committed
712
  return _gtk_print_convert_from_mm (size->width, unit);
713
}
714 715 716 717

/**
 * gtk_paper_size_get_height:
 * @size: a #GtkPaperSize object
718
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
719 720
 *
 * Gets the paper height of the #GtkPaperSize, in
721
 * units of @unit.
722
 *
723
 * Returns: the paper height
724 725 726 727
 *
 * Since: 2.10
 */
gdouble
728 729
gtk_paper_size_get_height (GtkPaperSize *size,
                           GtkUnit       unit)
730
{
Matthias Clasen's avatar
Matthias Clasen committed
731
  return _gtk_print_convert_from_mm (size->height, unit);
732 733
}

734 735 736
/**
 * gtk_paper_size_is_custom:
 * @size: a #GtkPaperSize object
737
 *
738
 * Returns %TRUE if @size is not a standard paper size.
739
 *
740
 * Returns: whether @size is a custom paper size.
741
 **/
742 743 744 745 746 747
gboolean
gtk_paper_size_is_custom (GtkPaperSize *size)
{
  return size->is_custom;
}

748 749 750 751 752 753 754 755 756 757 758 759 760 761
/**
 * gtk_paper_size_is_ipp:
 * @size: a #GtkPaperSize object
 *
 * Returns %TRUE if @size is an IPP standard paper size.
 *
 * Returns: whether @size is not an IPP custom paper size.
 **/
gboolean
gtk_paper_size_is_ipp (GtkPaperSize *size)
{
  return size->is_ipp;
}

762 763 764 765 766 767
/**
 * 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
768
 *
769 770 771 772
 * Changes the dimensions of a @size to @width x @height.
 *
 * Since: 2.10
 */
773
void
774 775 776 777
gtk_paper_size_set_size (GtkPaperSize *size,
                         gdouble       width,
                         gdouble       height,
                         GtkUnit       unit)
778 779 780 781
{
  g_return_if_fail (size != NULL);
  g_return_if_fail (size->is_custom);

Matthias Clasen's avatar
Matthias Clasen committed
782 783
  size->width = _gtk_print_convert_to_mm (width, unit);
  size->height = _gtk_print_convert_to_mm (height, unit);
784 785 786 787 788
}

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

789 790 791
/**
 * gtk_paper_size_get_default:
 *
792 793 794
 * Returns the name of the default paper size, which
 * depends on the current locale.
 *
795
 * Returns: the name of the default paper size. The string
796
 * is owned by GTK+ and should not be modified.
797
 *
798 799
 * Since: 2.10
 */
800
const gchar *
801 802 803 804 805 806 807 808 809
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);
810

811 812
    if (width == 210 && height == 297)
      return GTK_PAPER_NAME_A4;
813

814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829
    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;

830 831 832 833 834
  /* CLDR 1.8.1
   * http://unicode.org/repos/cldr-tmp/trunk/diff/supplemental/territory_language_information.html
   */
  if (g_regex_match_simple("[^_.@]{2,3}_(BZ|CA|CL|CO|CR|GT|MX|NI|PA|PH|PR|SV|US|VE)",
                           locale, G_REGEX_ANCHORED, G_REGEX_MATCH_ANCHORED))
835 836 837 838 839 840 841 842 843 844 845 846
    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.
 *
847
 * I’ve taken the actual values used from the OSX page setup dialog.
848
 * I’m not sure exactly where they got these values for, but might
849
 * correspond to this (from ghostscript docs):
850
 *
851 852 853 854 855 856
 * 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.
 */

857 858 859
/**
 * gtk_paper_size_get_default_top_margin:
 * @size: a #GtkPaperSize object
860
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
861
 *
862
 * Gets the default top margin for the #GtkPaperSize.
863
 *
864
 * Returns: the default top margin
865 866 867 868
 *
 * Since: 2.10
 */
gdouble
869 870
gtk_paper_size_get_default_top_margin (GtkPaperSize *size,
                                       GtkUnit       unit)
871
{
872
  gdouble margin;
873

Matthias Clasen's avatar
Matthias Clasen committed
874 875
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
876 877
}

878 879 880
/**
 * gtk_paper_size_get_default_bottom_margin:
 * @size: a #GtkPaperSize object
881
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
882
 *
883
 * Gets the default bottom margin for the #GtkPaperSize.
884
 *
885
 * Returns: the default bottom margin
886 887 888 889
 *
 * Since: 2.10
 */
gdouble
890 891
gtk_paper_size_get_default_bottom_margin (GtkPaperSize *size,
                                          GtkUnit       unit)
892
{
893 894
  gdouble margin;
  const gchar *name;
895

Matthias Clasen's avatar
Matthias Clasen committed
896
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
897 898 899 900 901

  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
902
    margin = _gtk_print_convert_to_mm (0.56, GTK_UNIT_INCH);
903

Matthias Clasen's avatar
Matthias Clasen committed
904
  return _gtk_print_convert_from_mm (margin, unit);
905 906
}

907 908 909
/**
 * gtk_paper_size_get_default_left_margin:
 * @size: a #GtkPaperSize object
910
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
911
 *
912
 * Gets the default left margin for the #GtkPaperSize.
913
 *
914
 * Returns: the default left margin
915 916 917 918
 *
 * Since: 2.10
 */
gdouble
919 920
gtk_paper_size_get_default_left_margin (GtkPaperSize *size,
                                        GtkUnit       unit)
921
{
922
  gdouble margin;
923

Matthias Clasen's avatar
Matthias Clasen committed
924 925
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
926 927
}

928 929 930
/**
 * gtk_paper_size_get_default_right_margin:
 * @size: a #GtkPaperSize object
931
 * @unit: the unit for the return value, not %GTK_UNIT_NONE
932
 *
933
 * Gets the default right margin for the #GtkPaperSize.
934
 *
935
 * Returns: the default right margin
936 937 938 939
 *
 * Since: 2.10
 */
gdouble
940 941
gtk_paper_size_get_default_right_margin (GtkPaperSize *size,
                                         GtkUnit       unit)
942
{
943
  gdouble margin;
944

Matthias Clasen's avatar
Matthias Clasen committed
945 946
  margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
  return _gtk_print_convert_from_mm (margin, unit);
947 948
}

Matthias Clasen's avatar
Matthias Clasen committed
949 950 951 952 953
/**
 * 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
954
 * @error: (allow-none): return location for an error, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
955 956
 *
 * Reads a paper size from the group @group_name in the key file
957
 * @key_file.
Matthias Clasen's avatar
Matthias Clasen committed
958 959
 *
 * Returns: a new #GtkPaperSize object with the restored
960
 *     paper size, or %NULL if an error occurred
Matthias Clasen's avatar
Matthias Clasen committed
961 962 963 964
 *
 * Since: 2.12
 */
GtkPaperSize *
965 966 967
gtk_paper_size_new_from_key_file (GKeyFile     *key_file,
                                  const gchar  *group_name,
                                  GError      **error)
Matthias Clasen's avatar
Matthias Clasen committed
968 969
{
  GtkPaperSize *paper_size = NULL;
970 971 972 973
  gchar *name = NULL;
  gchar *ppd_name = NULL;
  gchar *display_name = NULL;
  gchar *freeme = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
974 975 976 977 978 979 980 981 982
  gdouble width, height;
  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))
    {
983 984 985 986
      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
987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003
      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);\
      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,
1004
                                "Name", NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1005
  ppd_name = g_key_file_get_string (key_file, group_name,
1006
                                    "PPDName", NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1007
  display_name = g_key_file_get_string (key_file, group_name,
1008
                                        "DisplayName", NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1009 1010 1011 1012 1013
  /* Fallback for old ~/.gtk-custom-paper entries */
  if (!display_name)
    display_name = g_strdup (name);

  if (ppd_name != NULL)
1014 1015 1016 1017
    paper_size = gtk_paper_size_new_from_ppd (ppd_name,
                                              display_name,
                                              _gtk_print_convert_from_mm (width, GTK_UNIT_POINTS),
                                              _gtk_print_convert_from_mm (height, GTK_UNIT_POINTS));
Matthias Clasen's avatar
Matthias Clasen committed
1018 1019
  else if (name != NULL)
    paper_size = gtk_paper_size_new_custom (name, display_name,
1020
                                            width, height, GTK_UNIT_MM);
Matthias Clasen's avatar
Matthias Clasen committed
1021 1022
  else
    {
1023 1024 1025 1026
      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
1027 1028
      goto out;
    }
1029

Matthias Clasen's avatar
Matthias Clasen committed
1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
  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
1043
 * @size: a #GtkPaperSize
Matthias Clasen's avatar
Matthias Clasen committed
1044 1045 1046
 * @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
1047
 * This function adds the paper size from @size to @key_file.
Matthias Clasen's avatar
Matthias Clasen committed
1048 1049 1050 1051
 *
 * Since: 2.12
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
1052
gtk_paper_size_to_key_file (GtkPaperSize *size,
1053 1054
                            GKeyFile     *key_file,
                            const gchar  *group_name)
Matthias Clasen's avatar
Matthias Clasen committed
1055 1056 1057
{
  const char *name, *ppd_name, *display_name;

Matthias Clasen's avatar
Matthias Clasen committed
1058
  g_return_if_fail (size != NULL);
Matthias Clasen's avatar
Matthias Clasen committed
1059 1060
  g_return_if_fail (key_file != NULL);

Matthias Clasen's avatar
Matthias Clasen committed
1061 1062 1063
  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
1064

1065
  if (ppd_name != NULL)
Matthias Clasen's avatar
Matthias Clasen committed
1066
    g_key_file_set_string (key_file, group_name,
1067
                           "PPDName", ppd_name);
Matthias Clasen's avatar
Matthias Clasen committed
1068 1069
  else
    g_key_file_set_string (key_file, group_name,
1070
                           "Name", name);
Matthias Clasen's avatar
Matthias Clasen committed
1071

1072
  if (display_name)
Matthias Clasen's avatar
Matthias Clasen committed
1073
    g_key_file_set_string (key_file, group_name,
1074
                           "DisplayName", display_name);
Matthias Clasen's avatar
Matthias Clasen committed
1075 1076

  g_key_file_set_double (key_file, group_name,
1077
                         "Width", gtk_paper_size_get_width (size, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
1078
  g_key_file_set_double (key_file, group_name,
1079
                         "Height", gtk_paper_size_get_height (size, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
1080
}