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