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"
29 #define KEYFILE_GROUP_NAME "Page Setup"
31 typedef struct _GtkPageSetupClass GtkPageSetupClass;
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))
39 GObject parent_instance;
41 GtkPageOrientation orientation;
42 GtkPaperSize *paper_size;
43 /* These are stored in mm */
44 double top_margin, bottom_margin, left_margin, right_margin;
47 struct _GtkPageSetupClass
49 GObjectClass parent_class;
52 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
55 gtk_page_setup_finalize (GObject *object)
57 GtkPageSetup *setup = GTK_PAGE_SETUP (object);
59 gtk_paper_size_free (setup->paper_size);
61 G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
65 gtk_page_setup_init (GtkPageSetup *setup)
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);
76 gtk_page_setup_class_init (GtkPageSetupClass *class)
78 GObjectClass *gobject_class = (GObjectClass *)class;
80 gobject_class->finalize = gtk_page_setup_finalize;
86 * Creates a new #GtkPageSetup.
88 * Return value: a new #GtkPageSetup.
93 gtk_page_setup_new (void)
95 return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
99 * gtk_page_setup_copy:
100 * @other: the #GtkPageSetup to copy
102 * Copies a #GtkPageSetup.
104 * Return value: a copy of @other
109 gtk_page_setup_copy (GtkPageSetup *other)
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;
126 * gtk_page_setup_get_orientation:
127 * @setup: a #GtkPageSetup
129 * Gets the page orientation of the #GtkPageSetup.
131 * Return value: the page orientation
136 gtk_page_setup_get_orientation (GtkPageSetup *setup)
138 return setup->orientation;
142 * gtk_page_setup_set_orientation:
143 * @setup: a #GtkPageSetup
144 * @orientation: a #GtkPageOrientation value
146 * Sets the page orientation of the #GtkPageSetup.
151 gtk_page_setup_set_orientation (GtkPageSetup *setup,
152 GtkPageOrientation orientation)
154 setup->orientation = orientation;
158 * gtk_page_setup_get_paper_size:
159 * @setup: a #GtkPageSetup
161 * Gets the paper size of the #GtkPageSetup.
163 * Return value: the paper size
168 gtk_page_setup_get_paper_size (GtkPageSetup *setup)
170 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);
172 return setup->paper_size;
176 * gtk_page_setup_set_paper_size:
177 * @setup: a #GtkPageSetup
178 * @size: a #GtkPaperSize
180 * Sets the paper size of the #GtkPageSetup without
181 * changing the margins. See
182 * gtk_page_setup_set_paper_size_and_default_margins().
187 gtk_page_setup_set_paper_size (GtkPageSetup *setup,
190 GtkPaperSize *old_size;
192 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
193 g_return_if_fail (size != NULL);
195 old_size = setup->paper_size;
197 setup->paper_size = gtk_paper_size_copy (size);
200 gtk_paper_size_free (old_size);
204 * gtk_page_setup_set_paper_size_and_default_margins:
205 * @setup: a #GtkPageSetup
206 * @size: a #GtkPaperSize
208 * Sets the paper size of the #GtkPageSetup and modifies
209 * the margins according to the new paper size.
214 gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
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);
225 * gtk_page_setup_get_top_margin:
226 * @setup: a #GtkPageSetup
227 * @unit: the unit for the return value
229 * Gets the top margin in units of @unit.
231 * Return value: the top margin
236 gtk_page_setup_get_top_margin (GtkPageSetup *setup,
239 return _gtk_print_convert_from_mm (setup->top_margin, unit);
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
248 * Sets the top margin of the #GtkPageSetup.
253 gtk_page_setup_set_top_margin (GtkPageSetup *setup,
257 setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
261 * gtk_page_setup_get_bottom_margin:
262 * @setup: a #GtkPageSetup
263 * @unit: the unit for the return value
265 * Gets the bottom margin in units of @unit.
267 * Return value: the bottom margin
272 gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
275 return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
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
284 * Sets the bottom margin of the #GtkPageSetup.
289 gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
293 setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
297 * gtk_page_setup_get_left_margin:
298 * @setup: a #GtkPageSetup
299 * @unit: the unit for the return value
301 * Gets the left margin in units of @unit.
303 * Return value: the left margin
308 gtk_page_setup_get_left_margin (GtkPageSetup *setup,
311 return _gtk_print_convert_from_mm (setup->left_margin, unit);
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
320 * Sets the left margin of the #GtkPageSetup.
325 gtk_page_setup_set_left_margin (GtkPageSetup *setup,
329 setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
333 * gtk_page_setup_get_right_margin:
334 * @setup: a #GtkPageSetup
335 * @unit: the unit for the return value
337 * Gets the right margin in units of @unit.
339 * Return value: the right margin
344 gtk_page_setup_get_right_margin (GtkPageSetup *setup,
347 return _gtk_print_convert_from_mm (setup->right_margin, unit);
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
356 * Sets the right margin of the #GtkPageSetup.
361 gtk_page_setup_set_right_margin (GtkPageSetup *setup,
365 setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
369 * gtk_page_setup_get_paper_width:
370 * @setup: a #GtkPageSetup
371 * @unit: the unit for the return value
373 * Returns the paper width in units of @unit.
375 * Note that this function takes orientation, but
376 * not margins into consideration.
377 * See gtk_page_setup_get_page_width().
379 * Return value: the paper width.
384 gtk_page_setup_get_paper_width (GtkPageSetup *setup,
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);
391 return gtk_paper_size_get_height (setup->paper_size, unit);
395 * gtk_page_setup_get_paper_height:
396 * @setup: a #GtkPageSetup
397 * @unit: the unit for the return value
399 * Returns the paper height in units of @unit.
401 * Note that this function takes orientation, but
402 * not margins into consideration.
403 * See gtk_page_setup_get_page_height().
405 * Return value: the paper height.
410 gtk_page_setup_get_paper_height (GtkPageSetup *setup,
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);
417 return gtk_paper_size_get_width (setup->paper_size, unit);
421 * gtk_page_setup_get_page_width:
422 * @setup: a #GtkPageSetup
423 * @unit: the unit for the return value
425 * Returns the page width in units of @unit.
427 * Note that this function takes orientation and
428 * margins into consideration.
429 * See gtk_page_setup_get_paper_width().
431 * Return value: the page width.
436 gtk_page_setup_get_page_width (GtkPageSetup *setup,
441 width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
442 width -= setup->left_margin + setup->right_margin;
444 return _gtk_print_convert_from_mm (width, unit);
448 * gtk_page_setup_get_page_height:
449 * @setup: a #GtkPageSetup
450 * @unit: the unit for the return value
452 * Returns the page height in units of @unit.
454 * Note that this function takes orientation and
455 * margins into consideration.
456 * See gtk_page_setup_get_paper_height().
458 * Return value: the page height.
463 gtk_page_setup_get_page_height (GtkPageSetup *setup,
468 height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
469 height -= setup->top_margin + setup->bottom_margin;
471 return _gtk_print_convert_from_mm (height, unit);
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
480 * Reads the page setup from the file @file_name.
481 * See gtk_page_setup_to_file().
483 * Return value: %TRUE on success
488 gtk_page_setup_load_file (GtkPageSetup *setup,
489 const gchar *file_name,
492 gboolean retval = FALSE;
495 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
496 g_return_val_if_fail (file_name != NULL, FALSE);
498 key_file = g_key_file_new ();
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))
504 g_key_file_free (key_file);
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
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().
518 * Return value: the restored #GtkPageSetup
523 gtk_page_setup_new_from_file (const gchar *file_name,
526 GtkPageSetup *setup = gtk_page_setup_new ();
528 if (!gtk_page_setup_load_file (setup, file_name, error))
530 g_object_unref (setup);
537 /* something like this should really be in gobject! */
539 string_to_enum (GType type,
540 const char *enum_string)
542 GEnumClass *enum_class;
543 const GEnumValue *value;
546 g_return_val_if_fail (enum_string != NULL, 0);
548 enum_class = g_type_class_ref (type);
549 value = g_enum_get_value_by_nick (enum_class, enum_string);
551 retval = value->value;
553 g_type_class_unref (enum_class);
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
566 * Reads the page setup from the group @group_name in the key file
569 * Return value: %TRUE on success
574 gtk_page_setup_load_key_file (GtkPageSetup *setup,
576 const gchar *group_name,
579 GtkPaperSize *paper_size;
580 gdouble top, bottom, left, right;
581 char *orientation = NULL, *freeme = NULL;
582 gboolean retval = FALSE;
585 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
586 g_return_val_if_fail (key_file != NULL, FALSE);
589 group_name = KEYFILE_GROUP_NAME;
591 if (!g_key_file_has_group (key_file, group_name))
593 g_set_error_literal (error,
595 GTK_PRINT_ERROR_INVALID_FILE,
596 _("Not a valid page setup file"));
600 #define GET_DOUBLE(kf, group, name, v) \
601 v = g_key_file_get_double (kf, group, name, &err); \
604 g_propagate_error (error, err);\
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);
615 paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
618 g_propagate_error (error, err);
622 gtk_page_setup_set_paper_size (setup, paper_size);
623 gtk_paper_size_free (paper_size);
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);
630 orientation = g_key_file_get_string (key_file, group_name,
631 "Orientation", NULL);
634 gtk_page_setup_set_orientation (setup,
635 string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
637 g_free (orientation);
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
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.
658 * Return value: the restored #GtkPageSetup
663 gtk_page_setup_new_from_key_file (GKeyFile *key_file,
664 const gchar *group_name,
667 GtkPageSetup *setup = gtk_page_setup_new ();
669 if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
671 g_object_unref (setup);
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
684 * This function saves the information from @setup to @file_name.
686 * Return value: %TRUE on success
691 gtk_page_setup_to_file (GtkPageSetup *setup,
692 const char *file_name,
696 gboolean retval = FALSE;
700 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
701 g_return_val_if_fail (file_name != NULL, FALSE);
703 key_file = g_key_file_new ();
704 gtk_page_setup_to_key_file (setup, key_file, NULL);
706 data = g_key_file_to_data (key_file, &len, error);
710 retval = g_file_set_contents (file_name, data, len, error);
713 g_key_file_free (key_file);
719 /* something like this should really be in gobject! */
721 enum_to_string (GType type,
724 GEnumClass *enum_class;
728 enum_class = g_type_class_ref (type);
730 value = g_enum_get_value (enum_class, enum_value);
732 retval = g_strdup (value->value_nick);
734 g_type_class_unref (enum_class);
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"
746 * This function adds the page setup from @setup to @key_file.
751 gtk_page_setup_to_key_file (GtkPageSetup *setup,
753 const gchar *group_name)
755 GtkPaperSize *paper_size;
758 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
759 g_return_if_fail (key_file != NULL);
762 group_name = KEYFILE_GROUP_NAME;
764 paper_size = gtk_page_setup_get_paper_size (setup);
765 g_assert (paper_size != NULL);
767 gtk_paper_size_to_key_file (paper_size, key_file, group_name);
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));
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);