]> Pileus Git - ~andy/gtk/blob - gtk/gtkpagesetup.c
Translation updated by Ivar Smolin.
[~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 #include "gtkalias.h"
29
30 #define KEYFILE_GROUP_NAME "Page Setup"
31
32 typedef struct _GtkPageSetupClass GtkPageSetupClass;
33
34 #define GTK_IS_PAGE_SETUP_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PAGE_SETUP))
35 #define GTK_PAGE_SETUP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
36 #define GTK_PAGE_SETUP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
37
38 struct _GtkPageSetup
39 {
40   GObject parent_instance;
41
42   GtkPageOrientation orientation;
43   GtkPaperSize *paper_size;
44   /* These are stored in mm */
45   double top_margin, bottom_margin, left_margin, right_margin;
46 };
47
48 struct _GtkPageSetupClass
49 {
50   GObjectClass parent_class;
51 };
52
53 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
54
55 static void
56 gtk_page_setup_finalize (GObject *object)
57 {
58   GtkPageSetup *setup = GTK_PAGE_SETUP (object);
59   
60   gtk_paper_size_free (setup->paper_size);
61   
62   G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
63 }
64
65 static void
66 gtk_page_setup_init (GtkPageSetup *setup)
67 {
68   setup->paper_size = gtk_paper_size_new (NULL);
69   setup->orientation = GTK_PAGE_ORIENTATION_PORTRAIT;
70   setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
71   setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
72   setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
73   setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
74 }
75
76 static void
77 gtk_page_setup_class_init (GtkPageSetupClass *class)
78 {
79   GObjectClass *gobject_class = (GObjectClass *)class;
80
81   gobject_class->finalize = gtk_page_setup_finalize;
82 }
83
84 /**
85  * gtk_page_setup_new:
86  *
87  * Creates a new #GtkPageSetup. 
88  * 
89  * Return value: a new #GtkPageSetup.
90  *
91  * Since: 2.10
92  */
93 GtkPageSetup *
94 gtk_page_setup_new (void)
95 {
96   return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
97 }
98
99 /**
100  * gtk_page_setup_copy:
101  * @other: the #GtkPageSetup to copy
102  * 
103  * Copies a #GtkPageSetup.
104  * 
105  * Return value: a copy of @other
106  *
107  * Since: 2.10
108  */
109 GtkPageSetup *
110 gtk_page_setup_copy (GtkPageSetup *other)
111 {
112   GtkPageSetup *copy;
113
114   copy = gtk_page_setup_new ();
115   copy->orientation = other->orientation;
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 /**
476  * gtk_page_setup_new_from_file:
477  * @file_name: the filename to read the page setup from
478  * @error: return location for an error, or %NULL
479  * 
480  * Reads the page setup from the file @file_name. Returns a 
481  * new #GtkPageSetup object with the restored page setup, 
482  * or %NULL if an error occurred. See gtk_page_setup_to_file().
483  *
484  * Return value: the restored #GtkPageSetup
485  * 
486  * Since: 2.12
487  */
488 GtkPageSetup *
489 gtk_page_setup_new_from_file (const gchar  *file_name,
490                               GError      **error)
491 {
492   GtkPageSetup *page_setup;
493   GKeyFile *key_file;
494   GError *err = NULL;
495
496   g_return_val_if_fail (file_name != NULL, NULL);
497
498   key_file = g_key_file_new ();
499   if (!g_key_file_load_from_file (key_file, file_name, 0, &err))
500     {
501       g_key_file_free (key_file);
502       g_propagate_error (error, err);
503       return NULL;
504     }
505
506   page_setup = gtk_page_setup_new_from_key_file (key_file, NULL, error);
507   g_key_file_free (key_file);
508
509   return page_setup;
510 }
511
512 /* something like this should really be in gobject! */
513 static guint
514 string_to_enum (GType type,
515                 const char *enum_string)
516 {
517   GEnumClass *enum_class;
518   const GEnumValue *value;
519   guint retval = 0;
520
521   g_return_val_if_fail (enum_string != NULL, 0);
522
523   enum_class = g_type_class_ref (type);
524   value = g_enum_get_value_by_nick (enum_class, enum_string);
525   if (value)
526     retval = value->value;
527
528   g_type_class_unref (enum_class);
529
530   return retval;
531 }
532
533 /**
534  * gtk_page_setup_new_from_key_file:
535  * @key_file: the #GKeyFile to retrieve the page_setup from
536  * @group_name: the name of the group in the key_file to read, or %NULL
537  *              to use the default name "Page Setup"
538  * @error: return location for an error, or %NULL
539  * 
540  * Reads the page setup from the group @group_name in the key file
541  * @key_file. Returns a new #GtkPageSetup object with the restored
542  * page setup, or %NULL if an error occurred.
543  *
544  * Return value: the restored #GtkPageSetup
545  * 
546  * Since: 2.12
547  */
548 GtkPageSetup *
549 gtk_page_setup_new_from_key_file (GKeyFile     *key_file,
550                                   const gchar  *group_name,
551                                   GError      **error)
552 {
553   GtkPageSetup *page_setup = NULL;
554   GtkPaperSize *paper_size;
555   gdouble top, bottom, left, right;
556   char *orientation = NULL, *freeme = NULL;
557   gboolean retval = TRUE;
558   GError *err = NULL;
559
560   g_return_val_if_fail (key_file != NULL, NULL);
561
562   if (!group_name)
563     group_name = KEYFILE_GROUP_NAME;
564
565   if (!g_key_file_has_group (key_file, group_name))
566     {
567       g_set_error (error,
568                    GTK_PRINT_ERROR,
569                    GTK_PRINT_ERROR_INVALID_FILE,
570                    _("Not a valid page setup file"));
571       retval = FALSE;
572       goto out;
573     }
574
575 #define GET_DOUBLE(kf, group, name, v) \
576   v = g_key_file_get_double (kf, group, name, &err); \
577   if (err != NULL) \
578     { \
579       g_propagate_error (error, err);\
580       retval = FALSE;\
581       goto out;\
582     }
583
584   GET_DOUBLE (key_file, group_name, "MarginTop", top);
585   GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
586   GET_DOUBLE (key_file, group_name, "MarginLeft", left);
587   GET_DOUBLE (key_file, group_name, "MarginRight", right);
588
589 #undef GET_DOUBLE
590
591   paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
592   if (!paper_size)
593     {
594       g_propagate_error (error, err);
595       goto out;
596     }
597
598   page_setup = gtk_page_setup_new ();
599   gtk_page_setup_set_paper_size (page_setup, paper_size);
600   gtk_paper_size_free (paper_size);
601
602   gtk_page_setup_set_top_margin (page_setup, top, GTK_UNIT_MM);
603   gtk_page_setup_set_bottom_margin (page_setup, bottom, GTK_UNIT_MM);
604   gtk_page_setup_set_left_margin (page_setup, left, GTK_UNIT_MM);
605   gtk_page_setup_set_right_margin (page_setup, right, GTK_UNIT_MM);
606
607   orientation = g_key_file_get_string (key_file, group_name,
608                                        "Orientation", NULL);
609   if (orientation)
610     {
611       gtk_page_setup_set_orientation (page_setup,
612                                       string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
613                                                       orientation));
614       g_free (orientation);
615     }
616
617 out:
618   g_free (freeme);
619
620   return page_setup;
621 }
622
623 /**
624  * gtk_page_setup_to_file:
625  * @setup: a #GtkPageSetup
626  * @file_name: the file to save to
627  * @error: return location for errors, or %NULL
628  * 
629  * This function saves the information from @setup to @file_name.
630  * 
631  * Return value: %TRUE on success
632  *
633  * Since: 2.12
634  */
635 gboolean
636 gtk_page_setup_to_file (GtkPageSetup  *setup,
637                         const char    *file_name,
638                         GError       **error)
639 {
640   GKeyFile *key_file;
641   gboolean retval = FALSE;
642   char *data = NULL;
643   gsize len;
644
645   g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
646   g_return_val_if_fail (file_name != NULL, FALSE);
647
648   key_file = g_key_file_new ();
649   gtk_page_setup_to_key_file (setup, key_file, NULL);
650
651   data = g_key_file_to_data (key_file, &len, error);
652   if (!data)
653     goto out;
654
655   retval = g_file_set_contents (file_name, data, len, error);
656
657 out:
658   g_key_file_free (key_file);
659   g_free (data);
660
661   return retval;
662 }
663
664 /* something like this should really be in gobject! */
665 static char *
666 enum_to_string (GType type,
667                 guint enum_value)
668 {
669   GEnumClass *enum_class;
670   GEnumValue *value;
671   char *retval = NULL;
672
673   enum_class = g_type_class_ref (type);
674
675   value = g_enum_get_value (enum_class, enum_value);
676   if (value)
677     retval = g_strdup (value->value_nick);
678
679   g_type_class_unref (enum_class);
680
681   return retval;
682 }
683
684 /**
685  * gtk_page_setup_to_key_file:
686  * @setup: a #GtkPageSetup
687  * @key_file: the #GKeyFile to save the page setup to
688  * @group_name: the group to add the settings to in @key_file, 
689  *      or %NULL to use the default name "Page Setup"
690  * 
691  * This function adds the page setup from @setup to @key_file.
692  * 
693  * Since: 2.12
694  */
695 void
696 gtk_page_setup_to_key_file (GtkPageSetup *setup,
697                             GKeyFile     *key_file,
698                             const gchar  *group_name)
699 {
700   GtkPaperSize *paper_size;
701   char *orientation;
702
703   g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
704   g_return_if_fail (key_file != NULL);
705
706   if (!group_name)
707     group_name = KEYFILE_GROUP_NAME;
708
709   paper_size = gtk_page_setup_get_paper_size (setup);
710   g_assert (paper_size != NULL);
711
712   gtk_paper_size_to_key_file (paper_size, key_file, group_name);
713
714   g_key_file_set_double (key_file, group_name,
715                          "MarginTop", gtk_page_setup_get_top_margin (setup, GTK_UNIT_MM));
716   g_key_file_set_double (key_file, group_name,
717                          "MarginBottom", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM));
718   g_key_file_set_double (key_file, group_name,
719                          "MarginLeft", gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM));
720   g_key_file_set_double (key_file, group_name,
721                          "MarginRight", gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM));
722
723   orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
724                                 gtk_page_setup_get_orientation (setup));
725   g_key_file_set_string (key_file, group_name,
726                          "Orientation", orientation);
727   g_free (orientation);
728 }
729
730 #define __GTK_PAGE_SETUP_C__
731 #include "gtkaliasdef.c"