1 /* GTK - The GIMP Toolkit
2 * gtkpagesetup.c: Page Setup
3 * Copyright (C) 2006, Red Hat, Inc.
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.
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.
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.
23 #include "gtkpagesetup.h"
24 #include "gtkprintutils.h"
25 #include "gtkprintoperation.h" /* for GtkPrintError */
27 #include "gtktypebuiltins.h"
30 #define KEYFILE_GROUP_NAME "Page Setup"
32 typedef struct _GtkPageSetupClass GtkPageSetupClass;
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))
40 GObject parent_instance;
42 GtkPageOrientation orientation;
43 GtkPaperSize *paper_size;
44 /* These are stored in mm */
45 double top_margin, bottom_margin, left_margin, right_margin;
48 struct _GtkPageSetupClass
50 GObjectClass parent_class;
53 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
56 gtk_page_setup_finalize (GObject *object)
58 GtkPageSetup *setup = GTK_PAGE_SETUP (object);
60 gtk_paper_size_free (setup->paper_size);
62 G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
66 gtk_page_setup_init (GtkPageSetup *setup)
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);
77 gtk_page_setup_class_init (GtkPageSetupClass *class)
79 GObjectClass *gobject_class = (GObjectClass *)class;
81 gobject_class->finalize = gtk_page_setup_finalize;
87 * Creates a new #GtkPageSetup.
89 * Return value: a new #GtkPageSetup.
94 gtk_page_setup_new (void)
96 return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
100 * gtk_page_setup_copy:
101 * @other: the #GtkPageSetup to copy
103 * Copies a #GtkPageSetup.
105 * Return value: a copy of @other
110 gtk_page_setup_copy (GtkPageSetup *other)
114 copy = gtk_page_setup_new ();
115 copy->orientation = other->orientation;
116 gtk_paper_size_free (copy->paper_size);
117 copy->paper_size = gtk_paper_size_copy (other->paper_size);
118 copy->top_margin = other->top_margin;
119 copy->bottom_margin = other->bottom_margin;
120 copy->left_margin = other->left_margin;
121 copy->right_margin = other->right_margin;
127 * gtk_page_setup_get_orientation:
128 * @setup: a #GtkPageSetup
130 * Gets the page orientation of the #GtkPageSetup.
132 * Return value: the page orientation
137 gtk_page_setup_get_orientation (GtkPageSetup *setup)
139 return setup->orientation;
143 * gtk_page_setup_set_orientation:
144 * @setup: a #GtkPageSetup
145 * @orientation: a #GtkPageOrientation value
147 * Sets the page orientation of the #GtkPageSetup.
152 gtk_page_setup_set_orientation (GtkPageSetup *setup,
153 GtkPageOrientation orientation)
155 setup->orientation = orientation;
159 * gtk_page_setup_get_paper_size:
160 * @setup: a #GtkPageSetup
162 * Gets the paper size of the #GtkPageSetup.
164 * Return value: the paper size
169 gtk_page_setup_get_paper_size (GtkPageSetup *setup)
171 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);
173 return setup->paper_size;
177 * gtk_page_setup_set_paper_size:
178 * @setup: a #GtkPageSetup
179 * @size: a #GtkPaperSize
181 * Sets the paper size of the #GtkPageSetup without
182 * changing the margins. See
183 * gtk_page_setup_set_paper_size_and_default_margins().
188 gtk_page_setup_set_paper_size (GtkPageSetup *setup,
191 GtkPaperSize *old_size;
193 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
194 g_return_if_fail (size != NULL);
196 old_size = setup->paper_size;
198 setup->paper_size = gtk_paper_size_copy (size);
201 gtk_paper_size_free (old_size);
205 * gtk_page_setup_set_paper_size_and_default_margins:
206 * @setup: a #GtkPageSetup
207 * @size: a #GtkPaperSize
209 * Sets the paper size of the #GtkPageSetup and modifies
210 * the margins according to the new paper size.
215 gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
218 gtk_page_setup_set_paper_size (setup, size);
219 setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
220 setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
221 setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
222 setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
226 * gtk_page_setup_get_top_margin:
227 * @setup: a #GtkPageSetup
228 * @unit: the unit for the return value
230 * Gets the top margin in units of @unit.
232 * Return value: the top margin
237 gtk_page_setup_get_top_margin (GtkPageSetup *setup,
240 return _gtk_print_convert_from_mm (setup->top_margin, unit);
244 * gtk_page_setup_set_top_margin:
245 * @setup: a #GtkPageSetup
246 * @margin: the new top margin in units of @unit
247 * @unit: the units for @margin
249 * Sets the top margin of the #GtkPageSetup.
254 gtk_page_setup_set_top_margin (GtkPageSetup *setup,
258 setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
262 * gtk_page_setup_get_bottom_margin:
263 * @setup: a #GtkPageSetup
264 * @unit: the unit for the return value
266 * Gets the bottom margin in units of @unit.
268 * Return value: the bottom margin
273 gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
276 return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
280 * gtk_page_setup_set_bottom_margin:
281 * @setup: a #GtkPageSetup
282 * @margin: the new bottom margin in units of @unit
283 * @unit: the units for @margin
285 * Sets the bottom margin of the #GtkPageSetup.
290 gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
294 setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
298 * gtk_page_setup_get_left_margin:
299 * @setup: a #GtkPageSetup
300 * @unit: the unit for the return value
302 * Gets the left margin in units of @unit.
304 * Return value: the left margin
309 gtk_page_setup_get_left_margin (GtkPageSetup *setup,
312 return _gtk_print_convert_from_mm (setup->left_margin, unit);
316 * gtk_page_setup_set_left_margin:
317 * @setup: a #GtkPageSetup
318 * @margin: the new left margin in units of @unit
319 * @unit: the units for @margin
321 * Sets the left margin of the #GtkPageSetup.
326 gtk_page_setup_set_left_margin (GtkPageSetup *setup,
330 setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
334 * gtk_page_setup_get_right_margin:
335 * @setup: a #GtkPageSetup
336 * @unit: the unit for the return value
338 * Gets the right margin in units of @unit.
340 * Return value: the right margin
345 gtk_page_setup_get_right_margin (GtkPageSetup *setup,
348 return _gtk_print_convert_from_mm (setup->right_margin, unit);
352 * gtk_page_setup_set_right_margin:
353 * @setup: a #GtkPageSetup
354 * @margin: the new right margin in units of @unit
355 * @unit: the units for @margin
357 * Sets the right margin of the #GtkPageSetup.
362 gtk_page_setup_set_right_margin (GtkPageSetup *setup,
366 setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
370 * gtk_page_setup_get_paper_width:
371 * @setup: a #GtkPageSetup
372 * @unit: the unit for the return value
374 * Returns the paper width in units of @unit.
376 * Note that this function takes orientation, but
377 * not margins into consideration.
378 * See gtk_page_setup_get_page_width().
380 * Return value: the paper width.
385 gtk_page_setup_get_paper_width (GtkPageSetup *setup,
388 if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
389 setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
390 return gtk_paper_size_get_width (setup->paper_size, unit);
392 return gtk_paper_size_get_height (setup->paper_size, unit);
396 * gtk_page_setup_get_paper_height:
397 * @setup: a #GtkPageSetup
398 * @unit: the unit for the return value
400 * Returns the paper height in units of @unit.
402 * Note that this function takes orientation, but
403 * not margins into consideration.
404 * See gtk_page_setup_get_page_height().
406 * Return value: the paper height.
411 gtk_page_setup_get_paper_height (GtkPageSetup *setup,
414 if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
415 setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
416 return gtk_paper_size_get_height (setup->paper_size, unit);
418 return gtk_paper_size_get_width (setup->paper_size, unit);
422 * gtk_page_setup_get_page_width:
423 * @setup: a #GtkPageSetup
424 * @unit: the unit for the return value
426 * Returns the page width in units of @unit.
428 * Note that this function takes orientation and
429 * margins into consideration.
430 * See gtk_page_setup_get_paper_width().
432 * Return value: the page width.
437 gtk_page_setup_get_page_width (GtkPageSetup *setup,
442 width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
443 width -= setup->left_margin + setup->right_margin;
445 return _gtk_print_convert_from_mm (width, unit);
449 * gtk_page_setup_get_page_height:
450 * @setup: a #GtkPageSetup
451 * @unit: the unit for the return value
453 * Returns the page height in units of @unit.
455 * Note that this function takes orientation and
456 * margins into consideration.
457 * See gtk_page_setup_get_paper_height().
459 * Return value: the page height.
464 gtk_page_setup_get_page_height (GtkPageSetup *setup,
469 height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
470 height -= setup->top_margin + setup->bottom_margin;
472 return _gtk_print_convert_from_mm (height, unit);
476 * gtk_page_setup_load_file:
477 * @setup: a #GtkPageSetup
478 * @file_name: the filename to read the page setup from
479 * @error: return location for an error, or %NULL
481 * Reads the page setup from the file @file_name.
482 * See gtk_page_setup_to_file().
484 * Return value: %TRUE on success
489 gtk_page_setup_load_file (GtkPageSetup *setup,
490 const gchar *file_name,
493 gboolean retval = FALSE;
496 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
497 g_return_val_if_fail (file_name != NULL, FALSE);
499 key_file = g_key_file_new ();
501 if (g_key_file_load_from_file (key_file, file_name, 0, error) &&
502 gtk_page_setup_load_key_file (setup, key_file, NULL, error))
505 g_key_file_free (key_file);
511 * gtk_page_setup_new_from_file:
512 * @file_name: the filename to read the page setup from
513 * @error: return location for an error, or %NULL
515 * Reads the page setup from the file @file_name. Returns a
516 * new #GtkPageSetup object with the restored page setup,
517 * or %NULL if an error occurred. See gtk_page_setup_to_file().
519 * Return value: the restored #GtkPageSetup
524 gtk_page_setup_new_from_file (const gchar *file_name,
527 GtkPageSetup *setup = gtk_page_setup_new ();
529 if (!gtk_page_setup_load_file (setup, file_name, error))
531 g_object_unref (setup);
538 /* something like this should really be in gobject! */
540 string_to_enum (GType type,
541 const char *enum_string)
543 GEnumClass *enum_class;
544 const GEnumValue *value;
547 g_return_val_if_fail (enum_string != NULL, 0);
549 enum_class = g_type_class_ref (type);
550 value = g_enum_get_value_by_nick (enum_class, enum_string);
552 retval = value->value;
554 g_type_class_unref (enum_class);
560 * gtk_page_setup_load_key_file:
561 * @setup: a #GtkPageSetup
562 * @key_file: the #GKeyFile to retrieve the page_setup from
563 * @group_name: the name of the group in the key_file to read, or %NULL
564 * to use the default name "Page Setup"
565 * @error: return location for an error, or %NULL
567 * Reads the page setup from the group @group_name in the key file
570 * Return value: %TRUE on success
575 gtk_page_setup_load_key_file (GtkPageSetup *setup,
577 const gchar *group_name,
580 GtkPaperSize *paper_size;
581 gdouble top, bottom, left, right;
582 char *orientation = NULL, *freeme = NULL;
583 gboolean retval = FALSE;
586 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
587 g_return_val_if_fail (key_file != NULL, FALSE);
590 group_name = KEYFILE_GROUP_NAME;
592 if (!g_key_file_has_group (key_file, group_name))
594 g_set_error_literal (error,
596 GTK_PRINT_ERROR_INVALID_FILE,
597 _("Not a valid page setup file"));
601 #define GET_DOUBLE(kf, group, name, v) \
602 v = g_key_file_get_double (kf, group, name, &err); \
605 g_propagate_error (error, err);\
609 GET_DOUBLE (key_file, group_name, "MarginTop", top);
610 GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
611 GET_DOUBLE (key_file, group_name, "MarginLeft", left);
612 GET_DOUBLE (key_file, group_name, "MarginRight", right);
616 paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
619 g_propagate_error (error, err);
623 gtk_page_setup_set_paper_size (setup, paper_size);
624 gtk_paper_size_free (paper_size);
626 gtk_page_setup_set_top_margin (setup, top, GTK_UNIT_MM);
627 gtk_page_setup_set_bottom_margin (setup, bottom, GTK_UNIT_MM);
628 gtk_page_setup_set_left_margin (setup, left, GTK_UNIT_MM);
629 gtk_page_setup_set_right_margin (setup, right, GTK_UNIT_MM);
631 orientation = g_key_file_get_string (key_file, group_name,
632 "Orientation", NULL);
635 gtk_page_setup_set_orientation (setup,
636 string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
638 g_free (orientation);
649 * gtk_page_setup_new_from_key_file:
650 * @key_file: the #GKeyFile to retrieve the page_setup from
651 * @group_name: the name of the group in the key_file to read, or %NULL
652 * to use the default name "Page Setup"
653 * @error: return location for an error, or %NULL
655 * Reads the page setup from the group @group_name in the key file
656 * @key_file. Returns a new #GtkPageSetup object with the restored
657 * page setup, or %NULL if an error occurred.
659 * Return value: the restored #GtkPageSetup
664 gtk_page_setup_new_from_key_file (GKeyFile *key_file,
665 const gchar *group_name,
668 GtkPageSetup *setup = gtk_page_setup_new ();
670 if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
672 g_object_unref (setup);
680 * gtk_page_setup_to_file:
681 * @setup: a #GtkPageSetup
682 * @file_name: the file to save to
683 * @error: return location for errors, or %NULL
685 * This function saves the information from @setup to @file_name.
687 * Return value: %TRUE on success
692 gtk_page_setup_to_file (GtkPageSetup *setup,
693 const char *file_name,
697 gboolean retval = FALSE;
701 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
702 g_return_val_if_fail (file_name != NULL, FALSE);
704 key_file = g_key_file_new ();
705 gtk_page_setup_to_key_file (setup, key_file, NULL);
707 data = g_key_file_to_data (key_file, &len, error);
711 retval = g_file_set_contents (file_name, data, len, error);
714 g_key_file_free (key_file);
720 /* something like this should really be in gobject! */
722 enum_to_string (GType type,
725 GEnumClass *enum_class;
729 enum_class = g_type_class_ref (type);
731 value = g_enum_get_value (enum_class, enum_value);
733 retval = g_strdup (value->value_nick);
735 g_type_class_unref (enum_class);
741 * gtk_page_setup_to_key_file:
742 * @setup: a #GtkPageSetup
743 * @key_file: the #GKeyFile to save the page setup to
744 * @group_name: the group to add the settings to in @key_file,
745 * or %NULL to use the default name "Page Setup"
747 * This function adds the page setup from @setup to @key_file.
752 gtk_page_setup_to_key_file (GtkPageSetup *setup,
754 const gchar *group_name)
756 GtkPaperSize *paper_size;
759 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
760 g_return_if_fail (key_file != NULL);
763 group_name = KEYFILE_GROUP_NAME;
765 paper_size = gtk_page_setup_get_paper_size (setup);
766 g_assert (paper_size != NULL);
768 gtk_paper_size_to_key_file (paper_size, key_file, group_name);
770 g_key_file_set_double (key_file, group_name,
771 "MarginTop", gtk_page_setup_get_top_margin (setup, GTK_UNIT_MM));
772 g_key_file_set_double (key_file, group_name,
773 "MarginBottom", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM));
774 g_key_file_set_double (key_file, group_name,
775 "MarginLeft", gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM));
776 g_key_file_set_double (key_file, group_name,
777 "MarginRight", gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM));
779 orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
780 gtk_page_setup_get_orientation (setup));
781 g_key_file_set_string (key_file, group_name,
782 "Orientation", orientation);
783 g_free (orientation);
786 #define __GTK_PAGE_SETUP_C__
787 #include "gtkaliasdef.c"