]> Pileus Git - ~andy/gtk/blob - gtk/gtkpagesetup.c
a69e0821c56890522e510b74fed6bccaeb7717ac
[~andy/gtk] / gtk / gtkpagesetup.c
1 /* GTK - The GIMP Toolkit
2  * gtkpagesetup.c: Page Setup
3  * Copyright (C) 2006, Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18  * Boston, MA 02111-1307, USA.
19  */
20
21 #include "config.h"
22
23 #include "gtkpagesetup.h"
24 #include "gtkprintutils.h"
25 #include "gtkprintoperation.h" /* for GtkPrintError */
26 #include "gtkintl.h"
27 #include "gtktypebuiltins.h"
28
29 #define KEYFILE_GROUP_NAME "Page Setup"
30
31 typedef struct _GtkPageSetupClass GtkPageSetupClass;
32
33 #define GTK_IS_PAGE_SETUP_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PAGE_SETUP))
34 #define GTK_PAGE_SETUP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
35 #define GTK_PAGE_SETUP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
36
37 struct _GtkPageSetup
38 {
39   GObject parent_instance;
40
41   GtkPageOrientation orientation;
42   GtkPaperSize *paper_size;
43   /* These are stored in mm */
44   double top_margin, bottom_margin, left_margin, right_margin;
45 };
46
47 struct _GtkPageSetupClass
48 {
49   GObjectClass parent_class;
50 };
51
52 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
53
54 static void
55 gtk_page_setup_finalize (GObject *object)
56 {
57   GtkPageSetup *setup = GTK_PAGE_SETUP (object);
58   
59   gtk_paper_size_free (setup->paper_size);
60   
61   G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
62 }
63
64 static void
65 gtk_page_setup_init (GtkPageSetup *setup)
66 {
67   setup->paper_size = gtk_paper_size_new (NULL);
68   setup->orientation = GTK_PAGE_ORIENTATION_PORTRAIT;
69   setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
70   setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
71   setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
72   setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
73 }
74
75 static void
76 gtk_page_setup_class_init (GtkPageSetupClass *class)
77 {
78   GObjectClass *gobject_class = (GObjectClass *)class;
79
80   gobject_class->finalize = gtk_page_setup_finalize;
81 }
82
83 /**
84  * gtk_page_setup_new:
85  *
86  * Creates a new #GtkPageSetup. 
87  * 
88  * Return value: a new #GtkPageSetup.
89  *
90  * Since: 2.10
91  */
92 GtkPageSetup *
93 gtk_page_setup_new (void)
94 {
95   return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
96 }
97
98 /**
99  * gtk_page_setup_copy:
100  * @other: the #GtkPageSetup to copy
101  * 
102  * Copies a #GtkPageSetup.
103  * 
104  * Return value: a copy of @other
105  *
106  * Since: 2.10
107  */
108 GtkPageSetup *
109 gtk_page_setup_copy (GtkPageSetup *other)
110 {
111   GtkPageSetup *copy;
112
113   copy = gtk_page_setup_new ();
114   copy->orientation = other->orientation;
115   gtk_paper_size_free (copy->paper_size);
116   copy->paper_size = gtk_paper_size_copy (other->paper_size);
117   copy->top_margin = other->top_margin;
118   copy->bottom_margin = other->bottom_margin;
119   copy->left_margin = other->left_margin;
120   copy->right_margin = other->right_margin;
121
122   return copy;
123 }
124
125 /**
126  * gtk_page_setup_get_orientation:
127  * @setup: a #GtkPageSetup
128  * 
129  * Gets the page orientation of the #GtkPageSetup.
130  * 
131  * Return value: the page orientation
132  *
133  * Since: 2.10
134  */
135 GtkPageOrientation
136 gtk_page_setup_get_orientation (GtkPageSetup *setup)
137 {
138   return setup->orientation;
139 }
140
141 /**
142  * gtk_page_setup_set_orientation:
143  * @setup: a #GtkPageSetup
144  * @orientation: a #GtkPageOrientation value
145  * 
146  * Sets the page orientation of the #GtkPageSetup.
147  *
148  * Since: 2.10
149  */
150 void
151 gtk_page_setup_set_orientation (GtkPageSetup       *setup,
152                                 GtkPageOrientation  orientation)
153 {
154   setup->orientation = orientation;
155 }
156
157 /**
158  * gtk_page_setup_get_paper_size:
159  * @setup: a #GtkPageSetup
160  * 
161  * Gets the paper size of the #GtkPageSetup.
162  * 
163  * Return value: the paper size
164  *
165  * Since: 2.10
166  */
167 GtkPaperSize *
168 gtk_page_setup_get_paper_size (GtkPageSetup *setup)
169 {
170   g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);
171
172   return setup->paper_size;
173 }
174
175 /**
176  * gtk_page_setup_set_paper_size:
177  * @setup: a #GtkPageSetup
178  * @size: a #GtkPaperSize 
179  * 
180  * Sets the paper size of the #GtkPageSetup without
181  * changing the margins. See 
182  * gtk_page_setup_set_paper_size_and_default_margins().
183  *
184  * Since: 2.10
185  */
186 void
187 gtk_page_setup_set_paper_size (GtkPageSetup *setup,
188                                GtkPaperSize *size)
189 {
190   GtkPaperSize *old_size;
191
192   g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
193   g_return_if_fail (size != NULL);
194
195   old_size = setup->paper_size;
196
197   setup->paper_size = gtk_paper_size_copy (size);
198
199   if (old_size)
200     gtk_paper_size_free (old_size);
201 }
202
203 /**
204  * gtk_page_setup_set_paper_size_and_default_margins:
205  * @setup: a #GtkPageSetup
206  * @size: a #GtkPaperSize 
207  * 
208  * Sets the paper size of the #GtkPageSetup and modifies
209  * the margins according to the new paper size.
210  *
211  * Since: 2.10
212  */
213 void
214 gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
215                                                    GtkPaperSize *size)
216 {
217   gtk_page_setup_set_paper_size (setup, size);
218   setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
219   setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
220   setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
221   setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
222 }
223
224 /**
225  * gtk_page_setup_get_top_margin:
226  * @setup: a #GtkPageSetup
227  * @unit: the unit for the return value
228  * 
229  * Gets the top margin in units of @unit.
230  * 
231  * Return value: the top margin
232  *
233  * Since: 2.10
234  */
235 gdouble
236 gtk_page_setup_get_top_margin (GtkPageSetup *setup,
237                                GtkUnit       unit)
238 {
239   return _gtk_print_convert_from_mm (setup->top_margin, unit);
240 }
241
242 /**
243  * gtk_page_setup_set_top_margin:
244  * @setup: a #GtkPageSetup
245  * @margin: the new top margin in units of @unit
246  * @unit: the units for @margin
247  * 
248  * Sets the top margin of the #GtkPageSetup.
249  *
250  * Since: 2.10
251  */
252 void
253 gtk_page_setup_set_top_margin (GtkPageSetup *setup,
254                                gdouble       margin,
255                                GtkUnit       unit)
256 {
257   setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
258 }
259
260 /**
261  * gtk_page_setup_get_bottom_margin:
262  * @setup: a #GtkPageSetup
263  * @unit: the unit for the return value
264  * 
265  * Gets the bottom margin in units of @unit.
266  * 
267  * Return value: the bottom margin
268  *
269  * Since: 2.10
270  */
271 gdouble
272 gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
273                                   GtkUnit       unit)
274 {
275   return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
276 }
277
278 /**
279  * gtk_page_setup_set_bottom_margin:
280  * @setup: a #GtkPageSetup
281  * @margin: the new bottom margin in units of @unit
282  * @unit: the units for @margin
283  * 
284  * Sets the bottom margin of the #GtkPageSetup.
285  *
286  * Since: 2.10
287  */
288 void
289 gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
290                                   gdouble       margin,
291                                   GtkUnit       unit)
292 {
293   setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
294 }
295
296 /**
297  * gtk_page_setup_get_left_margin:
298  * @setup: a #GtkPageSetup
299  * @unit: the unit for the return value
300  * 
301  * Gets the left margin in units of @unit.
302  * 
303  * Return value: the left margin
304  *
305  * Since: 2.10
306  */
307 gdouble
308 gtk_page_setup_get_left_margin (GtkPageSetup *setup,
309                                 GtkUnit       unit)
310 {
311   return _gtk_print_convert_from_mm (setup->left_margin, unit);
312 }
313
314 /**
315  * gtk_page_setup_set_left_margin:
316  * @setup: a #GtkPageSetup
317  * @margin: the new left margin in units of @unit
318  * @unit: the units for @margin
319  * 
320  * Sets the left margin of the #GtkPageSetup.
321  *
322  * Since: 2.10
323  */
324 void
325 gtk_page_setup_set_left_margin (GtkPageSetup *setup,
326                                 gdouble       margin,
327                                 GtkUnit       unit)
328 {
329   setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
330 }
331
332 /**
333  * gtk_page_setup_get_right_margin:
334  * @setup: a #GtkPageSetup
335  * @unit: the unit for the return value
336  * 
337  * Gets the right margin in units of @unit.
338  * 
339  * Return value: the right margin
340  *
341  * Since: 2.10
342  */
343 gdouble
344 gtk_page_setup_get_right_margin (GtkPageSetup *setup,
345                                  GtkUnit       unit)
346 {
347   return _gtk_print_convert_from_mm (setup->right_margin, unit);
348 }
349
350 /**
351  * gtk_page_setup_set_right_margin:
352  * @setup: a #GtkPageSetup
353  * @margin: the new right margin in units of @unit
354  * @unit: the units for @margin
355  * 
356  * Sets the right margin of the #GtkPageSetup.
357  *
358  * Since: 2.10
359  */
360 void
361 gtk_page_setup_set_right_margin (GtkPageSetup *setup,
362                                  gdouble       margin,
363                                  GtkUnit       unit)
364 {
365   setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
366 }
367
368 /**
369  * gtk_page_setup_get_paper_width:
370  * @setup: a #GtkPageSetup
371  * @unit: the unit for the return value
372  * 
373  * Returns the paper width in units of @unit.
374  * 
375  * Note that this function takes orientation, but 
376  * not margins into consideration. 
377  * See gtk_page_setup_get_page_width().
378  *
379  * Return value: the paper width.
380  *
381  * Since: 2.10
382  */
383 gdouble
384 gtk_page_setup_get_paper_width (GtkPageSetup *setup,
385                                 GtkUnit       unit)
386 {
387   if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
388       setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
389     return gtk_paper_size_get_width (setup->paper_size, unit);
390   else
391     return gtk_paper_size_get_height (setup->paper_size, unit);
392 }
393
394 /**
395  * gtk_page_setup_get_paper_height:
396  * @setup: a #GtkPageSetup
397  * @unit: the unit for the return value
398  * 
399  * Returns the paper height in units of @unit.
400  * 
401  * Note that this function takes orientation, but 
402  * not margins into consideration.
403  * See gtk_page_setup_get_page_height().
404  *
405  * Return value: the paper height.
406  *
407  * Since: 2.10
408  */
409 gdouble
410 gtk_page_setup_get_paper_height (GtkPageSetup *setup,
411                                  GtkUnit       unit)
412 {
413   if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
414       setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
415     return gtk_paper_size_get_height (setup->paper_size, unit);
416   else
417     return gtk_paper_size_get_width (setup->paper_size, unit);
418 }
419
420 /**
421  * gtk_page_setup_get_page_width:
422  * @setup: a #GtkPageSetup
423  * @unit: the unit for the return value
424  * 
425  * Returns the page width in units of @unit.
426  * 
427  * Note that this function takes orientation and
428  * margins into consideration. 
429  * See gtk_page_setup_get_paper_width().
430  *
431  * Return value: the page width.
432  *
433  * Since: 2.10
434  */
435 gdouble
436 gtk_page_setup_get_page_width (GtkPageSetup *setup,
437                                GtkUnit       unit)
438 {
439   gdouble width;
440   
441   width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
442   width -= setup->left_margin + setup->right_margin;
443   
444   return _gtk_print_convert_from_mm (width, unit);
445 }
446
447 /**
448  * gtk_page_setup_get_page_height:
449  * @setup: a #GtkPageSetup
450  * @unit: the unit for the return value
451  * 
452  * Returns the page height in units of @unit.
453  * 
454  * Note that this function takes orientation and
455  * margins into consideration. 
456  * See gtk_page_setup_get_paper_height().
457  *
458  * Return value: the page height.
459  *
460  * Since: 2.10
461  */
462 gdouble
463 gtk_page_setup_get_page_height (GtkPageSetup *setup,
464                                 GtkUnit       unit)
465 {
466   gdouble height;
467   
468   height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
469   height -= setup->top_margin + setup->bottom_margin;
470   
471   return _gtk_print_convert_from_mm (height, unit);
472 }
473
474 /**
475  * gtk_page_setup_load_file:
476  * @setup: a #GtkPageSetup
477  * @file_name: the filename to read the page setup from
478  * @error: (allow-none): return location for an error, or %NULL
479  *
480  * Reads the page setup from the file @file_name.
481  * See gtk_page_setup_to_file().
482  *
483  * Return value: %TRUE on success
484  *
485  * Since: 2.14
486  */
487 gboolean
488 gtk_page_setup_load_file (GtkPageSetup *setup,
489                           const gchar  *file_name,
490                           GError      **error)
491 {
492   gboolean retval = FALSE;
493   GKeyFile *key_file;
494
495   g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
496   g_return_val_if_fail (file_name != NULL, FALSE);
497
498   key_file = g_key_file_new ();
499
500   if (g_key_file_load_from_file (key_file, file_name, 0, error) &&
501       gtk_page_setup_load_key_file (setup, key_file, NULL, error))
502     retval = TRUE;
503
504   g_key_file_free (key_file);
505
506   return retval;
507 }
508
509 /**
510  * gtk_page_setup_new_from_file:
511  * @file_name: the filename to read the page setup from
512  * @error: (allow-none): return location for an error, or %NULL
513  * 
514  * Reads the page setup from the file @file_name. Returns a 
515  * new #GtkPageSetup object with the restored page setup, 
516  * or %NULL if an error occurred. See gtk_page_setup_to_file().
517  *
518  * Return value: the restored #GtkPageSetup
519  * 
520  * Since: 2.12
521  */
522 GtkPageSetup *
523 gtk_page_setup_new_from_file (const gchar  *file_name,
524                               GError      **error)
525 {
526   GtkPageSetup *setup = gtk_page_setup_new ();
527
528   if (!gtk_page_setup_load_file (setup, file_name, error))
529     {
530       g_object_unref (setup);
531       setup = NULL;
532     }
533
534   return setup;
535 }
536
537 /* something like this should really be in gobject! */
538 static guint
539 string_to_enum (GType type,
540                 const char *enum_string)
541 {
542   GEnumClass *enum_class;
543   const GEnumValue *value;
544   guint retval = 0;
545
546   g_return_val_if_fail (enum_string != NULL, 0);
547
548   enum_class = g_type_class_ref (type);
549   value = g_enum_get_value_by_nick (enum_class, enum_string);
550   if (value)
551     retval = value->value;
552
553   g_type_class_unref (enum_class);
554
555   return retval;
556 }
557
558 /**
559  * gtk_page_setup_load_key_file:
560  * @setup: a #GtkPageSetup
561  * @key_file: the #GKeyFile to retrieve the page_setup from
562  * @group_name: (allow-none): the name of the group in the key_file to read, or %NULL
563  *              to use the default name "Page Setup"
564  * @error: (allow-none): return location for an error, or %NULL
565  * 
566  * Reads the page setup from the group @group_name in the key file
567  * @key_file.
568  * 
569  * Return value: %TRUE on success
570  *
571  * Since: 2.14
572  */
573 gboolean
574 gtk_page_setup_load_key_file (GtkPageSetup *setup,
575                               GKeyFile     *key_file,
576                               const gchar  *group_name,
577                               GError      **error)
578 {
579   GtkPaperSize *paper_size;
580   gdouble top, bottom, left, right;
581   char *orientation = NULL, *freeme = NULL;
582   gboolean retval = FALSE;
583   GError *err = NULL;
584
585   g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
586   g_return_val_if_fail (key_file != NULL, FALSE);
587
588   if (!group_name)
589     group_name = KEYFILE_GROUP_NAME;
590
591   if (!g_key_file_has_group (key_file, group_name))
592     {
593       g_set_error_literal (error,
594                            GTK_PRINT_ERROR,
595                            GTK_PRINT_ERROR_INVALID_FILE,
596                            _("Not a valid page setup file"));
597       goto out;
598     }
599
600 #define GET_DOUBLE(kf, group, name, v) \
601   v = g_key_file_get_double (kf, group, name, &err); \
602   if (err != NULL) \
603     { \
604       g_propagate_error (error, err);\
605       goto out;\
606     }
607
608   GET_DOUBLE (key_file, group_name, "MarginTop", top);
609   GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
610   GET_DOUBLE (key_file, group_name, "MarginLeft", left);
611   GET_DOUBLE (key_file, group_name, "MarginRight", right);
612
613 #undef GET_DOUBLE
614
615   paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
616   if (!paper_size)
617     {
618       g_propagate_error (error, err);
619       goto out;
620     }
621
622   gtk_page_setup_set_paper_size (setup, paper_size);
623   gtk_paper_size_free (paper_size);
624
625   gtk_page_setup_set_top_margin (setup, top, GTK_UNIT_MM);
626   gtk_page_setup_set_bottom_margin (setup, bottom, GTK_UNIT_MM);
627   gtk_page_setup_set_left_margin (setup, left, GTK_UNIT_MM);
628   gtk_page_setup_set_right_margin (setup, right, GTK_UNIT_MM);
629
630   orientation = g_key_file_get_string (key_file, group_name,
631                                        "Orientation", NULL);
632   if (orientation)
633     {
634       gtk_page_setup_set_orientation (setup,
635                                       string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
636                                                       orientation));
637       g_free (orientation);
638     }
639
640   retval = TRUE;
641
642 out:
643   g_free (freeme);
644   return retval;
645 }
646
647 /**
648  * gtk_page_setup_new_from_key_file:
649  * @key_file: the #GKeyFile to retrieve the page_setup from
650  * @group_name: (allow-none): the name of the group in the key_file to read, or %NULL
651  *              to use the default name "Page Setup"
652  * @error: (allow-none): return location for an error, or %NULL
653  *
654  * Reads the page setup from the group @group_name in the key file
655  * @key_file. Returns a new #GtkPageSetup object with the restored
656  * page setup, or %NULL if an error occurred.
657  *
658  * Return value: the restored #GtkPageSetup
659  *
660  * Since: 2.12
661  */
662 GtkPageSetup *
663 gtk_page_setup_new_from_key_file (GKeyFile     *key_file,
664                                   const gchar  *group_name,
665                                   GError      **error)
666 {
667   GtkPageSetup *setup = gtk_page_setup_new ();
668
669   if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
670     {
671       g_object_unref (setup);
672       setup = NULL;
673     }
674
675   return setup;
676 }
677
678 /**
679  * gtk_page_setup_to_file:
680  * @setup: a #GtkPageSetup
681  * @file_name: the file to save to
682  * @error: (allow-none): return location for errors, or %NULL
683  * 
684  * This function saves the information from @setup to @file_name.
685  * 
686  * Return value: %TRUE on success
687  *
688  * Since: 2.12
689  */
690 gboolean
691 gtk_page_setup_to_file (GtkPageSetup  *setup,
692                         const char    *file_name,
693                         GError       **error)
694 {
695   GKeyFile *key_file;
696   gboolean retval = FALSE;
697   char *data = NULL;
698   gsize len;
699
700   g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
701   g_return_val_if_fail (file_name != NULL, FALSE);
702
703   key_file = g_key_file_new ();
704   gtk_page_setup_to_key_file (setup, key_file, NULL);
705
706   data = g_key_file_to_data (key_file, &len, error);
707   if (!data)
708     goto out;
709
710   retval = g_file_set_contents (file_name, data, len, error);
711
712 out:
713   g_key_file_free (key_file);
714   g_free (data);
715
716   return retval;
717 }
718
719 /* something like this should really be in gobject! */
720 static char *
721 enum_to_string (GType type,
722                 guint enum_value)
723 {
724   GEnumClass *enum_class;
725   GEnumValue *value;
726   char *retval = NULL;
727
728   enum_class = g_type_class_ref (type);
729
730   value = g_enum_get_value (enum_class, enum_value);
731   if (value)
732     retval = g_strdup (value->value_nick);
733
734   g_type_class_unref (enum_class);
735
736   return retval;
737 }
738
739 /**
740  * gtk_page_setup_to_key_file:
741  * @setup: a #GtkPageSetup
742  * @key_file: the #GKeyFile to save the page setup to
743  * @group_name: the group to add the settings to in @key_file, 
744  *      or %NULL to use the default name "Page Setup"
745  * 
746  * This function adds the page setup from @setup to @key_file.
747  * 
748  * Since: 2.12
749  */
750 void
751 gtk_page_setup_to_key_file (GtkPageSetup *setup,
752                             GKeyFile     *key_file,
753                             const gchar  *group_name)
754 {
755   GtkPaperSize *paper_size;
756   char *orientation;
757
758   g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
759   g_return_if_fail (key_file != NULL);
760
761   if (!group_name)
762     group_name = KEYFILE_GROUP_NAME;
763
764   paper_size = gtk_page_setup_get_paper_size (setup);
765   g_assert (paper_size != NULL);
766
767   gtk_paper_size_to_key_file (paper_size, key_file, group_name);
768
769   g_key_file_set_double (key_file, group_name,
770                          "MarginTop", gtk_page_setup_get_top_margin (setup, GTK_UNIT_MM));
771   g_key_file_set_double (key_file, group_name,
772                          "MarginBottom", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM));
773   g_key_file_set_double (key_file, group_name,
774                          "MarginLeft", gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM));
775   g_key_file_set_double (key_file, group_name,
776                          "MarginRight", gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM));
777
778   orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
779                                 gtk_page_setup_get_orientation (setup));
780   g_key_file_set_string (key_file, group_name,
781                          "Orientation", orientation);
782   g_free (orientation);
783 }