]> Pileus Git - ~andy/gtk/blob - gtk/gtkpapersize.c
c5abd1404fd85302f776a686ab8f434f00ed26ba
[~andy/gtk] / gtk / gtkpapersize.c
1 /* GTK - The GTK+ Toolkit
2  * gtkpapersize.c: Paper Size
3  * Copyright (C) 2006, Red Hat, Inc.
4  * Copyright © 2006, 2007 Christian Persch
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the
18  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21
22 #include "config.h"
23 #include <string.h>
24 #include <stdlib.h>
25 #include <locale.h>
26 #if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
27 #include <langinfo.h>
28 #endif
29
30 #include "gtkpapersize.h"
31 #include "gtkprintutils.h"
32 #include "gtkprintoperation.h"  /* for GtkPrintError */
33 #include "gtkintl.h"
34 #include "gtkalias.h"
35
36 #include "paper_names_offsets.c"
37
38 struct _GtkPaperSize
39 {
40   const PaperInfo *info;
41
42   /* If these are not set we fall back to info */
43   gchar *name;
44   gchar *display_name;
45   gchar *ppd_name;
46   
47   gdouble width, height; /* Stored in mm */
48   gboolean is_custom;
49 };
50
51 GType
52 gtk_paper_size_get_type (void)
53 {
54   static GType our_type = 0;
55   
56   if (our_type == 0)
57     our_type = g_boxed_type_register_static (I_("GtkPaperSize"),
58                                              (GBoxedCopyFunc)gtk_paper_size_copy,
59                                              (GBoxedFreeFunc)gtk_paper_size_free);
60   return our_type;
61 }
62
63 static const PaperInfo *
64 lookup_paper_info (const gchar *name)
65 {
66   int lower = 0;
67   int upper = G_N_ELEMENTS (standard_names_offsets) - 1;
68   int mid;
69   int cmp;
70
71   do 
72     {
73        mid = (lower + upper) / 2;
74        cmp = strcmp (name, paper_names + standard_names_offsets[mid].name);
75        if (cmp < 0)
76          upper = mid - 1;
77        else if (cmp > 0)
78          lower = mid + 1;
79        else
80          return &standard_names_offsets[mid];
81     }
82   while (lower <= upper);
83
84   return NULL;
85 }
86
87 static gboolean
88 parse_media_size (const gchar *size,
89                   gdouble     *width_mm, 
90                   gdouble     *height_mm)
91 {
92   const char *p;
93   char *e;
94   double short_dim, long_dim;
95
96   p = size;
97   
98   short_dim = g_ascii_strtod (p, &e);
99
100   if (p == e || *e != 'x')
101     return FALSE;
102
103   p = e + 1; /* Skip x */
104
105   long_dim = g_ascii_strtod (p, &e);
106
107   if (p == e)
108     return FALSE;
109
110   p = e;
111
112   if (strcmp (p, "in") == 0)
113     {
114       short_dim = short_dim * MM_PER_INCH;
115       long_dim = long_dim * MM_PER_INCH;
116     }
117   else if (strcmp (p, "mm") != 0)
118     return FALSE;
119
120   if (width_mm)
121     *width_mm = short_dim;
122   if (height_mm)
123     *height_mm = long_dim;
124   
125   return TRUE;  
126 }
127
128 static gboolean
129 parse_full_media_size_name (const gchar  *full_name,
130                             gchar       **name,
131                             gdouble      *width_mm, 
132                             gdouble      *height_mm)
133 {
134   const char *p;
135   const char *end_of_name;
136   
137   /* From the spec:
138    media-size-self-describing-name =
139         ( class-in "_" size-name "_" short-dim "x" long-dim "in" ) |
140         ( class-mm "_" size-name "_" short-dim "x" long-dim "mm" )
141    class-in = "custom" | "na" | "asme" | "roc" | "oe"
142    class-mm = "custom" | "iso" | "jis" | "jpn" | "prc" | "om"
143    size-name = ( lowalpha | digit ) *( lowalpha | digit | "-" )
144    short-dim = dim
145    long-dim = dim
146    dim = integer-part [fraction-part] | "0" fraction-part
147    integer-part = non-zero-digit *digit
148    fraction-part = "." *digit non-zero-digit
149    lowalpha = "a" | "b" | "c" | "d" | "e" | "f" | "g" | "h" | "i" |
150               "j" | "k" | "l" | "m" | "n" | "o" | "p" | "q" | "r" |
151               "s" | "t" | "u" | "v" | "w" | "x" | "y" | "z"
152    non-zero-digit = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
153    digit    = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9"
154  */
155
156   p = strchr (full_name, '_');
157   if (p == NULL)
158     return FALSE;
159
160   p++; /* Skip _ */
161   
162   p = strchr (p, '_');
163   if (p == NULL)
164     return FALSE;
165
166   end_of_name = p;
167
168   p++; /* Skip _ */
169
170   if (!parse_media_size (p, width_mm, height_mm))
171     return FALSE;
172
173   if (name)
174     *name = g_strndup (full_name, end_of_name - full_name);
175   
176   return TRUE;  
177 }
178
179 static GtkPaperSize *
180 gtk_paper_size_new_from_info (const PaperInfo *info)
181 {
182   GtkPaperSize *size;
183   
184   size = g_slice_new0 (GtkPaperSize);
185   size->info = info;
186   size->width = info->width;
187   size->height = info->height;
188   
189   return size;
190 }
191
192 /**
193  * gtk_paper_size_new:
194  * @name: a paper size name, or %NULL
195  * 
196  * 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  * paper name. 
199  *
200  * If @name is %NULL, the default paper size is returned,
201  * see gtk_paper_size_get_default().
202  * 
203  * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
204  * to free it
205  *
206  * Since: 2.10
207  */
208 GtkPaperSize *
209 gtk_paper_size_new (const gchar *name)
210 {
211   GtkPaperSize *size;
212   char *short_name;
213   double width, height;
214   const PaperInfo *info;
215
216   if (name == NULL)
217     name = gtk_paper_size_get_default ();
218   
219   if (parse_full_media_size_name (name, &short_name, &width, &height))
220     {
221       size = g_slice_new0 (GtkPaperSize);
222
223       size->width = width;
224       size->height = height;
225       size->name = short_name;
226       size->display_name = g_strdup (short_name);
227       if (strncmp (short_name, "custom", 6) == 0)
228         size->is_custom = TRUE;
229     }
230   else
231     {
232       info = lookup_paper_info (name);
233       if (info != NULL)
234         size = gtk_paper_size_new_from_info (info);
235       else
236         {
237           g_warning ("Unknown paper size %s\n", name);
238           size = g_slice_new0 (GtkPaperSize);
239           size->name = g_strdup (name);
240           size->display_name = g_strdup (name);
241           /* Default to A4 size */
242           size->width = 210;
243           size->height = 297;
244         }
245     }
246   
247   return size;
248 }
249
250 /**
251  * gtk_paper_size_new_from_ppd:
252  * @ppd_name: a PPD paper name
253  * @ppd_display_name: the corresponding human-readable name
254  * @width: the paper width, in points
255  * @height: the paper height in points
256  * 
257  * Creates a new #GtkPaperSize object by using 
258  * PPD information. 
259  * 
260  * If @ppd_name is not a recognized PPD paper name, 
261  * @ppd_display_name, @width and @height are used to 
262  * construct a custom #GtkPaperSize object.
263  *
264  * Return value: a new #GtkPaperSize, use gtk_paper_size_free()
265  * to free it
266  *
267  * Since: 2.10
268  */
269 GtkPaperSize *
270 gtk_paper_size_new_from_ppd (const gchar *ppd_name,
271                              const gchar *ppd_display_name,
272                              gdouble      width,
273                              gdouble      height)
274 {
275   char *name;
276   const char *lookup_ppd_name;
277   char *freeme;
278   GtkPaperSize *size;
279   int i;
280
281   lookup_ppd_name = ppd_name;
282   
283   freeme = NULL;
284   /* Strip out Traverse suffix in matching. */
285   if (g_str_has_suffix (ppd_name, ".Transverse"))
286     {
287       lookup_ppd_name = freeme =
288         g_strndup (ppd_name, strlen (ppd_name) - strlen (".Transverse"));
289     }
290   
291   for (i = 0; i < G_N_ELEMENTS(standard_names_offsets); i++)
292     {
293       if (standard_names_offsets[i].ppd_name != -1 &&
294           strcmp (paper_names + standard_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
295         {
296           size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
297           goto out;
298         }
299     }
300   
301   for (i = 0; i < G_N_ELEMENTS(extra_ppd_names_offsets); i++)
302     {
303       if (strcmp (paper_names + extra_ppd_names_offsets[i].ppd_name, lookup_ppd_name) == 0)
304         {
305           size = gtk_paper_size_new (paper_names + extra_ppd_names_offsets[i].standard_name);
306           goto out;
307         }
308     }
309
310   name = g_strconcat ("ppd_", ppd_name, NULL);
311   size = gtk_paper_size_new_custom (name, ppd_display_name, width, height, GTK_UNIT_POINTS);
312   g_free (name);
313
314  out:
315
316   if (size->info == NULL ||
317       size->info->ppd_name == -1 ||
318       strcmp (paper_names + size->info->ppd_name, ppd_name) != 0)
319     size->ppd_name = g_strdup (ppd_name);
320   
321   g_free (freeme);
322   
323   return size;
324 }
325
326 /**
327  * gtk_paper_size_new_custom:
328  * @name: the paper name 
329  * @display_name: the human-readable name
330  * @width: the paper width, in units of @unit
331  * @height: the paper height, in units of @unit
332  * @unit: the unit for @width and @height
333  * 
334  * Creates a new #GtkPaperSize object with the
335  * given parameters.
336  * 
337  * Return value: a new #GtkPaperSize object, use gtk_paper_size_free()
338  * to free it
339  *
340  * Since: 2.10
341  */
342 GtkPaperSize *
343 gtk_paper_size_new_custom (const gchar *name, 
344                            const gchar *display_name,
345                            gdouble      width, 
346                            gdouble      height, 
347                            GtkUnit      unit)
348 {
349   GtkPaperSize *size;
350   g_return_val_if_fail (name != NULL, NULL);
351   g_return_val_if_fail (unit != GTK_UNIT_PIXEL, NULL);
352
353   size = g_slice_new0 (GtkPaperSize);
354   
355   size->name = g_strdup (name);
356   size->display_name = g_strdup (display_name);
357   size->is_custom = TRUE;
358   
359   size->width = _gtk_print_convert_to_mm (width, unit);
360   size->height = _gtk_print_convert_to_mm (height, unit);
361   
362   return size;
363 }
364
365 /**
366  * gtk_paper_size_copy:
367  * @other: a #GtkPaperSize
368  * 
369  * Copies an existing #GtkPaperSize.
370  * 
371  * Return value: a copy of @other
372  *
373  * Since: 2.10
374  */
375 GtkPaperSize *
376 gtk_paper_size_copy (GtkPaperSize *other)
377 {
378   GtkPaperSize *size;
379
380   size = g_slice_new0 (GtkPaperSize);
381
382   size->info = other->info;
383   if (other->name)
384     size->name = g_strdup (other->name);
385   if (other->display_name)
386     size->display_name = g_strdup (other->display_name);
387   if (other->ppd_name)
388     size->ppd_name = g_strdup (other->ppd_name);
389   
390   size->width = other->width;
391   size->height = other->height;
392   size->is_custom = other->is_custom;
393
394   return size;
395 }
396
397 /**
398  * gtk_paper_size_free:
399  * @size: a #GtkPaperSize
400  * 
401  * Free the given #GtkPaperSize object.
402  *
403  * Since: 2.10
404  */
405 void
406 gtk_paper_size_free (GtkPaperSize *size)
407 {
408   g_free (size->name);
409   g_free (size->display_name);
410   g_free (size->ppd_name);
411
412   g_slice_free (GtkPaperSize, size);
413 }
414
415 /**
416  * gtk_paper_size_is_equal:
417  * @size1: a #GtkPaperSize object
418  * @size2: another #GtkPaperSize object
419  * 
420  * Compares two #GtkPaperSize objects.
421  * 
422  * Return value: %TRUE, if @size1 and @size2 
423  * represent the same paper size
424  *
425  * Since: 2.10
426  */
427 gboolean
428 gtk_paper_size_is_equal (GtkPaperSize *size1,
429                          GtkPaperSize *size2)
430 {
431   if (size1->info != NULL && size2->info != NULL)
432     return size1->info == size2->info;
433   
434   return strcmp (gtk_paper_size_get_name (size1),
435                  gtk_paper_size_get_name (size2)) == 0;
436 }
437
438 GList * _gtk_load_custom_papers (void);
439
440 /**
441  * gtk_paper_size_get_paper_sizes:
442  * @include_custom: whether to include custom paper sizes
443  *     as defined in the page setup dialog
444  *
445  * Creates a list of known paper sizes.
446  * 
447  * Return value: a newly allocated list of newly 
448  *    allocated #GtkPaperSize objects
449  *
450  * Since: 2.12
451  */
452 GList *
453 gtk_paper_size_get_paper_sizes (gboolean include_custom)
454 {
455   GList *list = NULL;
456   guint i;
457 #ifdef G_OS_UNIX                /* _gtk_load_custom_papers() only on Unix so far  */
458   if (include_custom)
459     {
460       GList *page_setups, *l;
461
462       page_setups = _gtk_load_custom_papers ();
463       for (l = page_setups; l != NULL; l = l->next)
464         {
465           GtkPageSetup *setup = (GtkPageSetup *) l->data;
466           GtkPaperSize *size;
467
468           size = gtk_page_setup_get_paper_size (setup);
469           list = g_list_prepend (list, gtk_paper_size_copy (size));
470         }
471
472       g_list_foreach (page_setups, (GFunc) g_object_unref, NULL);
473       g_list_free (page_setups);
474     }
475 #endif
476   for (i = 0; i < G_N_ELEMENTS (standard_names_offsets); ++i)
477     {
478        GtkPaperSize *size;
479
480        size = gtk_paper_size_new_from_info (&standard_names_offsets[i]);
481        list = g_list_prepend (list, size);
482     }
483
484   return g_list_reverse (list);
485 }
486
487
488 /**
489  * gtk_paper_size_get_name:
490  * @size: a #GtkPaperSize object
491  * 
492  * Gets the name of the #GtkPaperSize.
493  * 
494  * Return value: the name of @size
495  *
496  * Since: 2.10
497  */
498 G_CONST_RETURN gchar *
499 gtk_paper_size_get_name (GtkPaperSize *size)
500 {
501   if (size->name)
502     return size->name;
503   g_assert (size->info != NULL);
504   return paper_names + size->info->name;
505 }
506
507 /**
508  * gtk_paper_size_get_display_name:
509  * @size: a #GtkPaperSize object
510  * 
511  * Gets the human-readable name of the #GtkPaperSize.
512  * 
513  * Return value: the human-readable name of @size
514  *
515  * Since: 2.10
516  */
517 G_CONST_RETURN gchar *
518 gtk_paper_size_get_display_name (GtkPaperSize *size)
519 {
520   const gchar *display_name;
521
522   if (size->display_name)
523     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  * gtk_paper_size_get_ppd_name:
533  * @size: a #GtkPaperSize object
534  * 
535  * Gets the PPD name of the #GtkPaperSize, which
536  * may be %NULL.
537  * 
538  * Return value: the PPD name of @size
539  *
540  * Since: 2.10
541  */
542 G_CONST_RETURN gchar *
543 gtk_paper_size_get_ppd_name (GtkPaperSize *size)
544 {
545   if (size->ppd_name)
546     return size->ppd_name;
547   if (size->info)
548     return paper_names + size->info->ppd_name;
549   return NULL;
550 }
551
552 /**
553  * gtk_paper_size_get_width:
554  * @size: a #GtkPaperSize object
555  * @unit: the unit for the return value
556  * 
557  * Gets the paper width of the #GtkPaperSize, in 
558  * units of @unit.
559  * 
560  * Return value: the paper width 
561  *
562  * Since: 2.10
563  */
564 gdouble
565 gtk_paper_size_get_width (GtkPaperSize *size, 
566                           GtkUnit       unit)
567 {
568   return _gtk_print_convert_from_mm (size->width, unit);
569 }
570
571 /**
572  * gtk_paper_size_get_height:
573  * @size: a #GtkPaperSize object
574  * @unit: the unit for the return value
575  * 
576  * Gets the paper height of the #GtkPaperSize, in 
577  * units of @unit.
578  * 
579  * Return value: the paper height 
580  *
581  * Since: 2.10
582  */
583 gdouble
584 gtk_paper_size_get_height (GtkPaperSize *size, 
585                            GtkUnit       unit)
586 {
587   return _gtk_print_convert_from_mm (size->height, unit);
588 }
589
590 /**
591  * gtk_paper_size_is_custom:
592  * @size: a #GtkPaperSize object
593  * 
594  * Returns %TRUE if @size is not a standard paper size.
595  * 
596  * Return value: whether @size is a custom paper size.
597  **/
598 gboolean
599 gtk_paper_size_is_custom (GtkPaperSize *size)
600 {
601   return size->is_custom;
602 }
603
604 /**
605  * gtk_paper_size_set_size:
606  * @size: a custom #GtkPaperSize object
607  * @width: the new width in units of @unit
608  * @height: the new height in units of @unit
609  * @unit: the unit for @width and @height
610  * 
611  * Changes the dimensions of a @size to @width x @height.
612  *
613  * Since: 2.10
614  */
615 void
616 gtk_paper_size_set_size (GtkPaperSize *size, 
617                          gdouble       width, 
618                          gdouble       height, 
619                          GtkUnit       unit)
620 {
621   g_return_if_fail (size != NULL);
622   g_return_if_fail (size->is_custom);
623
624   size->width = _gtk_print_convert_to_mm (width, unit);
625   size->height = _gtk_print_convert_to_mm (height, unit);
626 }
627
628 #define NL_PAPER_GET(x)         \
629   ((union { char *string; unsigned int word; })nl_langinfo(x)).word
630
631 /**
632  * gtk_paper_size_get_default:
633  *
634  * Returns the name of the default paper size, which 
635  * depends on the current locale.  
636  * 
637  * Return value: the name of the default paper size. The string
638  * is owned by GTK+ and should not be modified.
639  * 
640  * Since: 2.10
641  */
642 G_CONST_RETURN gchar *
643 gtk_paper_size_get_default (void)
644 {
645   char *locale, *freeme = NULL;
646   const char *paper_size;
647
648 #if defined(HAVE__NL_PAPER_HEIGHT) && defined(HAVE__NL_PAPER_WIDTH)
649   {
650     int width = NL_PAPER_GET (_NL_PAPER_WIDTH);
651     int height = NL_PAPER_GET (_NL_PAPER_HEIGHT);
652     
653     if (width == 210 && height == 297)
654       return GTK_PAPER_NAME_A4;
655     
656     if (width == 216 && height == 279)
657       return GTK_PAPER_NAME_LETTER;
658   }
659 #endif
660
661 #ifdef G_OS_WIN32
662   freeme = locale = g_win32_getlocale ();
663 #elif defined(LC_PAPER)
664   locale = setlocale(LC_PAPER, NULL);
665 #else
666   locale = setlocale(LC_MESSAGES, NULL);
667 #endif
668
669   if (!locale)
670     return GTK_PAPER_NAME_A4;
671
672   if (g_str_has_prefix (locale, "en_CA") ||
673       g_str_has_prefix (locale, "en_US") ||
674       g_str_has_prefix (locale, "es_PR") ||
675       g_str_has_prefix (locale, "es_US"))
676     paper_size = GTK_PAPER_NAME_LETTER;
677   else
678     paper_size = GTK_PAPER_NAME_A4;
679
680   g_free (freeme);
681   return paper_size;
682 }
683
684 /* These get the default margins used for the paper size. Its
685  * larger than most printers margins, so that it will be within
686  * the imageble area on any printer.
687  *
688  * I've taken the actual values used from the OSX page setup dialog.
689  * I'm not sure exactly where they got these values for, but might
690  * correspond to this (from ghostscript docs):
691  * 
692  * All DeskJets have 0.5 inches (1.27cm) of unprintable bottom margin,
693  * due to the mechanical arrangement used to grab the paper. Side margins
694  * are approximately 0.25 inches (0.64cm) for U.S. letter paper, and 0.15
695  * inches (0.38cm) for A4.
696  */
697
698 /**
699  * gtk_paper_size_get_default_top_margin:
700  * @size: a #GtkPaperSize object
701  * @unit: the unit for the return value
702  * 
703  * Gets the default top margin for the #GtkPaperSize.
704  * 
705  * Return value: the default top margin
706  *
707  * Since: 2.10
708  */
709 gdouble
710 gtk_paper_size_get_default_top_margin (GtkPaperSize *size, 
711                                        GtkUnit       unit)
712 {
713   gdouble margin;
714
715   margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
716   return _gtk_print_convert_from_mm (margin, unit);
717 }
718
719 /**
720  * gtk_paper_size_get_default_bottom_margin:
721  * @size: a #GtkPaperSize object
722  * @unit: the unit for the return value
723  * 
724  * Gets the default bottom margin for the #GtkPaperSize.
725  * 
726  * Return value: the default bottom margin
727  *
728  * Since: 2.10
729  */
730 gdouble
731 gtk_paper_size_get_default_bottom_margin (GtkPaperSize *size, 
732                                           GtkUnit       unit)
733 {
734   gdouble margin;
735   const gchar *name;
736
737   margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
738
739   name = gtk_paper_size_get_name (size);
740   if (strcmp (name, "na_letter") == 0 ||
741       strcmp (name, "na_legal") == 0 ||
742       strcmp (name, "iso_a4") == 0)
743     margin = _gtk_print_convert_to_mm (0.56, GTK_UNIT_INCH);
744   
745   return _gtk_print_convert_from_mm (margin, unit);
746 }
747
748 /**
749  * gtk_paper_size_get_default_left_margin:
750  * @size: a #GtkPaperSize object
751  * @unit: the unit for the return value
752  * 
753  * Gets the default left margin for the #GtkPaperSize.
754  * 
755  * Return value: the default left margin
756  *
757  * Since: 2.10
758  */
759 gdouble
760 gtk_paper_size_get_default_left_margin (GtkPaperSize *size, 
761                                         GtkUnit       unit)
762 {
763   gdouble margin;
764
765   margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
766   return _gtk_print_convert_from_mm (margin, unit);
767 }
768
769 /**
770  * gtk_paper_size_get_default_right_margin:
771  * @size: a #GtkPaperSize object
772  * @unit: the unit for the return value
773  * 
774  * Gets the default right margin for the #GtkPaperSize.
775  * 
776  * Return value: the default right margin
777  *
778  * Since: 2.10
779  */
780 gdouble
781 gtk_paper_size_get_default_right_margin (GtkPaperSize *size, 
782                                          GtkUnit       unit)
783 {
784   gdouble margin;
785
786   margin = _gtk_print_convert_to_mm (0.25, GTK_UNIT_INCH);
787   return _gtk_print_convert_from_mm (margin, unit);
788 }
789
790 /**
791  * gtk_paper_size_new_from_key_file:
792  * @key_file: the #GKeyFile to retrieve the papersize from
793  * @group_name: the name ofthe group in the key file to read,
794  *     or %NULL to read the first group
795  * @error: return location for an error, or %NULL
796  *
797  * Reads a paper size from the group @group_name in the key file
798  * @key_file. 
799  *
800  * Returns: a new #GtkPaperSize object with the restored
801  *          paper size, or %NULL if an error occurred.
802  *
803  * Since: 2.12
804  */
805 GtkPaperSize *
806 gtk_paper_size_new_from_key_file (GKeyFile    *key_file,
807                                   const gchar *group_name,
808                                   GError     **error)
809 {
810   GtkPaperSize *paper_size = NULL;
811   char *name = NULL, *ppd_name = NULL, *display_name = NULL, *freeme = NULL;
812   gdouble width, height;
813   gboolean retval = TRUE;
814   GError *err = NULL;
815
816   g_return_val_if_fail (key_file != NULL, NULL);
817
818   if (!group_name)
819     group_name = freeme = g_key_file_get_start_group (key_file);
820   if (!group_name || !g_key_file_has_group (key_file, group_name))
821     {
822       g_set_error_literal (error,
823                            GTK_PRINT_ERROR,
824                            GTK_PRINT_ERROR_INVALID_FILE,
825                            _("Not a valid page setup file"));
826       retval = FALSE;
827       goto out;
828     }
829
830 #define GET_DOUBLE(kf, group, name, v) \
831   v = g_key_file_get_double (kf, group, name, &err); \
832   if (err != NULL) \
833     {\
834       g_propagate_error (error, err);\
835       retval = FALSE;\
836       goto out;\
837     }
838
839   GET_DOUBLE (key_file, group_name, "Width", width);
840   GET_DOUBLE (key_file, group_name, "Height", height);
841
842 #undef GET_DOUBLE
843
844   name = g_key_file_get_string (key_file, group_name,
845                                 "Name", NULL);
846   ppd_name = g_key_file_get_string (key_file, group_name,
847                                     "PPDName", NULL);
848   display_name = g_key_file_get_string (key_file, group_name,
849                                         "DisplayName", NULL);
850   /* Fallback for old ~/.gtk-custom-paper entries */
851   if (!display_name)
852     display_name = g_strdup (name);
853
854   if (ppd_name != NULL)
855     paper_size = gtk_paper_size_new_from_ppd (ppd_name, display_name,
856                                               width, height);
857   else if (name != NULL)
858     paper_size = gtk_paper_size_new_custom (name, display_name,
859                                             width, height, GTK_UNIT_MM);
860   else
861     {
862       g_set_error_literal (error,
863                            GTK_PRINT_ERROR,
864                            GTK_PRINT_ERROR_INVALID_FILE,
865                            _("Not a valid page setup file"));
866       retval = FALSE;
867       goto out;
868     }
869   g_assert (paper_size != NULL);
870
871 out:
872   g_free (ppd_name);
873   g_free (name);
874   g_free (display_name);
875   g_free (freeme);
876
877   return paper_size;
878 }
879
880 /**
881  * gtk_paper_size_to_key_file:
882  * @size: a #GtkPaperSize
883  * @key_file: the #GKeyFile to save the paper size to
884  * @group_name: the group to add the settings to in @key_file
885  *
886  * This function adds the paper size from @size to @key_file.
887  *
888  * Since: 2.12
889  */
890 void
891 gtk_paper_size_to_key_file (GtkPaperSize *size,
892                             GKeyFile     *key_file,
893                             const gchar  *group_name)
894 {
895   const char *name, *ppd_name, *display_name;
896
897   g_return_if_fail (size != NULL);
898   g_return_if_fail (key_file != NULL);
899
900   name = gtk_paper_size_get_name (size);
901   display_name = gtk_paper_size_get_display_name (size);
902   ppd_name = gtk_paper_size_get_ppd_name (size);
903
904   if (ppd_name != NULL) 
905     g_key_file_set_string (key_file, group_name,
906                            "PPDName", ppd_name);
907   else
908     g_key_file_set_string (key_file, group_name,
909                            "Name", name);
910
911   if (display_name) 
912     g_key_file_set_string (key_file, group_name,
913                            "DisplayName", display_name);
914
915   g_key_file_set_double (key_file, group_name,
916                          "Width", gtk_paper_size_get_width (size, GTK_UNIT_MM));
917   g_key_file_set_double (key_file, group_name,
918                          "Height", gtk_paper_size_get_height (size, GTK_UNIT_MM));
919 }
920
921
922 #define __GTK_PAPER_SIZE_C__
923 #include "gtkaliasdef.c"