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, see <http://www.gnu.org/licenses/>.
21 #include "gtkpagesetup.h"
22 #include "gtkprintutils.h"
23 #include "gtkprintoperation.h" /* for GtkPrintError */
25 #include "gtktypebuiltins.h"
28 * SECTION:gtkpagesetup
29 * @Short_description: Stores page setup information
30 * @Title: GtkPageSetup
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.
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
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.
52 * <title>A page setup dialog</title>
54 * static GtkPrintSettings *settings = NULL;
55 * static GtkPageSetup *page_setup = NULL;
58 * do_page_setup (void)
60 * GtkPageSetup *new_page_setup;
62 * if (settings == NULL)
63 * settings = gtk_print_settings_new (<!-- -->);
65 * new_page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (main_window),
66 * page_setup, settings);
69 * g_object_unref (page_setup);
71 * page_setup = new_page_setup;
76 * Printing support was added in GTK+ 2.10.
79 #define KEYFILE_GROUP_NAME "Page Setup"
81 typedef struct _GtkPageSetupClass GtkPageSetupClass;
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))
89 GObject parent_instance;
91 GtkPageOrientation orientation;
92 GtkPaperSize *paper_size;
93 /* These are stored in mm */
94 double top_margin, bottom_margin, left_margin, right_margin;
97 struct _GtkPageSetupClass
99 GObjectClass parent_class;
102 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
105 gtk_page_setup_finalize (GObject *object)
107 GtkPageSetup *setup = GTK_PAGE_SETUP (object);
109 gtk_paper_size_free (setup->paper_size);
111 G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
115 gtk_page_setup_init (GtkPageSetup *setup)
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);
126 gtk_page_setup_class_init (GtkPageSetupClass *class)
128 GObjectClass *gobject_class = (GObjectClass *)class;
130 gobject_class->finalize = gtk_page_setup_finalize;
134 * gtk_page_setup_new:
136 * Creates a new #GtkPageSetup.
138 * Return value: a new #GtkPageSetup.
143 gtk_page_setup_new (void)
145 return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
149 * gtk_page_setup_copy:
150 * @other: the #GtkPageSetup to copy
152 * Copies a #GtkPageSetup.
154 * Return value: (transfer full): a copy of @other
159 gtk_page_setup_copy (GtkPageSetup *other)
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;
176 * gtk_page_setup_get_orientation:
177 * @setup: a #GtkPageSetup
179 * Gets the page orientation of the #GtkPageSetup.
181 * Return value: the page orientation
186 gtk_page_setup_get_orientation (GtkPageSetup *setup)
188 return setup->orientation;
192 * gtk_page_setup_set_orientation:
193 * @setup: a #GtkPageSetup
194 * @orientation: a #GtkPageOrientation value
196 * Sets the page orientation of the #GtkPageSetup.
201 gtk_page_setup_set_orientation (GtkPageSetup *setup,
202 GtkPageOrientation orientation)
204 setup->orientation = orientation;
208 * gtk_page_setup_get_paper_size:
209 * @setup: a #GtkPageSetup
211 * Gets the paper size of the #GtkPageSetup.
213 * Return value: the paper size
218 gtk_page_setup_get_paper_size (GtkPageSetup *setup)
220 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);
222 return setup->paper_size;
226 * gtk_page_setup_set_paper_size:
227 * @setup: a #GtkPageSetup
228 * @size: a #GtkPaperSize
230 * Sets the paper size of the #GtkPageSetup without
231 * changing the margins. See
232 * gtk_page_setup_set_paper_size_and_default_margins().
237 gtk_page_setup_set_paper_size (GtkPageSetup *setup,
240 GtkPaperSize *old_size;
242 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
243 g_return_if_fail (size != NULL);
245 old_size = setup->paper_size;
247 setup->paper_size = gtk_paper_size_copy (size);
250 gtk_paper_size_free (old_size);
254 * gtk_page_setup_set_paper_size_and_default_margins:
255 * @setup: a #GtkPageSetup
256 * @size: a #GtkPaperSize
258 * Sets the paper size of the #GtkPageSetup and modifies
259 * the margins according to the new paper size.
264 gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
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);
275 * gtk_page_setup_get_top_margin:
276 * @setup: a #GtkPageSetup
277 * @unit: the unit for the return value
279 * Gets the top margin in units of @unit.
281 * Return value: the top margin
286 gtk_page_setup_get_top_margin (GtkPageSetup *setup,
289 return _gtk_print_convert_from_mm (setup->top_margin, unit);
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
298 * Sets the top margin of the #GtkPageSetup.
303 gtk_page_setup_set_top_margin (GtkPageSetup *setup,
307 setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
311 * gtk_page_setup_get_bottom_margin:
312 * @setup: a #GtkPageSetup
313 * @unit: the unit for the return value
315 * Gets the bottom margin in units of @unit.
317 * Return value: the bottom margin
322 gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
325 return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
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
334 * Sets the bottom margin of the #GtkPageSetup.
339 gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
343 setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
347 * gtk_page_setup_get_left_margin:
348 * @setup: a #GtkPageSetup
349 * @unit: the unit for the return value
351 * Gets the left margin in units of @unit.
353 * Return value: the left margin
358 gtk_page_setup_get_left_margin (GtkPageSetup *setup,
361 return _gtk_print_convert_from_mm (setup->left_margin, unit);
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
370 * Sets the left margin of the #GtkPageSetup.
375 gtk_page_setup_set_left_margin (GtkPageSetup *setup,
379 setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
383 * gtk_page_setup_get_right_margin:
384 * @setup: a #GtkPageSetup
385 * @unit: the unit for the return value
387 * Gets the right margin in units of @unit.
389 * Return value: the right margin
394 gtk_page_setup_get_right_margin (GtkPageSetup *setup,
397 return _gtk_print_convert_from_mm (setup->right_margin, unit);
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
406 * Sets the right margin of the #GtkPageSetup.
411 gtk_page_setup_set_right_margin (GtkPageSetup *setup,
415 setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
419 * gtk_page_setup_get_paper_width:
420 * @setup: a #GtkPageSetup
421 * @unit: the unit for the return value
423 * Returns the paper width in units of @unit.
425 * Note that this function takes orientation, but
426 * not margins into consideration.
427 * See gtk_page_setup_get_page_width().
429 * Return value: the paper width.
434 gtk_page_setup_get_paper_width (GtkPageSetup *setup,
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);
441 return gtk_paper_size_get_height (setup->paper_size, unit);
445 * gtk_page_setup_get_paper_height:
446 * @setup: a #GtkPageSetup
447 * @unit: the unit for the return value
449 * Returns the paper height in units of @unit.
451 * Note that this function takes orientation, but
452 * not margins into consideration.
453 * See gtk_page_setup_get_page_height().
455 * Return value: the paper height.
460 gtk_page_setup_get_paper_height (GtkPageSetup *setup,
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);
467 return gtk_paper_size_get_width (setup->paper_size, unit);
471 * gtk_page_setup_get_page_width:
472 * @setup: a #GtkPageSetup
473 * @unit: the unit for the return value
475 * Returns the page width in units of @unit.
477 * Note that this function takes orientation and
478 * margins into consideration.
479 * See gtk_page_setup_get_paper_width().
481 * Return value: the page width.
486 gtk_page_setup_get_page_width (GtkPageSetup *setup,
491 width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
492 width -= setup->left_margin + setup->right_margin;
494 return _gtk_print_convert_from_mm (width, unit);
498 * gtk_page_setup_get_page_height:
499 * @setup: a #GtkPageSetup
500 * @unit: the unit for the return value
502 * Returns the page height in units of @unit.
504 * Note that this function takes orientation and
505 * margins into consideration.
506 * See gtk_page_setup_get_paper_height().
508 * Return value: the page height.
513 gtk_page_setup_get_page_height (GtkPageSetup *setup,
518 height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
519 height -= setup->top_margin + setup->bottom_margin;
521 return _gtk_print_convert_from_mm (height, unit);
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
530 * Reads the page setup from the file @file_name.
531 * See gtk_page_setup_to_file().
533 * Return value: %TRUE on success
538 gtk_page_setup_load_file (GtkPageSetup *setup,
539 const gchar *file_name,
542 gboolean retval = FALSE;
545 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
546 g_return_val_if_fail (file_name != NULL, FALSE);
548 key_file = g_key_file_new ();
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))
554 g_key_file_free (key_file);
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
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().
568 * Return value: the restored #GtkPageSetup
573 gtk_page_setup_new_from_file (const gchar *file_name,
576 GtkPageSetup *setup = gtk_page_setup_new ();
578 if (!gtk_page_setup_load_file (setup, file_name, error))
580 g_object_unref (setup);
587 /* something like this should really be in gobject! */
589 string_to_enum (GType type,
590 const char *enum_string)
592 GEnumClass *enum_class;
593 const GEnumValue *value;
596 g_return_val_if_fail (enum_string != NULL, 0);
598 enum_class = g_type_class_ref (type);
599 value = g_enum_get_value_by_nick (enum_class, enum_string);
601 retval = value->value;
603 g_type_class_unref (enum_class);
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
616 * Reads the page setup from the group @group_name in the key file
619 * Return value: %TRUE on success
624 gtk_page_setup_load_key_file (GtkPageSetup *setup,
626 const gchar *group_name,
629 GtkPaperSize *paper_size;
630 gdouble top, bottom, left, right;
631 char *orientation = NULL, *freeme = NULL;
632 gboolean retval = FALSE;
635 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
636 g_return_val_if_fail (key_file != NULL, FALSE);
639 group_name = KEYFILE_GROUP_NAME;
641 if (!g_key_file_has_group (key_file, group_name))
643 g_set_error_literal (error,
645 GTK_PRINT_ERROR_INVALID_FILE,
646 _("Not a valid page setup file"));
650 #define GET_DOUBLE(kf, group, name, v) \
651 v = g_key_file_get_double (kf, group, name, &err); \
654 g_propagate_error (error, err);\
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);
665 paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
668 g_propagate_error (error, err);
672 gtk_page_setup_set_paper_size (setup, paper_size);
673 gtk_paper_size_free (paper_size);
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);
680 orientation = g_key_file_get_string (key_file, group_name,
681 "Orientation", NULL);
684 gtk_page_setup_set_orientation (setup,
685 string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
687 g_free (orientation);
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
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.
708 * Return value: the restored #GtkPageSetup
713 gtk_page_setup_new_from_key_file (GKeyFile *key_file,
714 const gchar *group_name,
717 GtkPageSetup *setup = gtk_page_setup_new ();
719 if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
721 g_object_unref (setup);
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
734 * This function saves the information from @setup to @file_name.
736 * Return value: %TRUE on success
741 gtk_page_setup_to_file (GtkPageSetup *setup,
742 const char *file_name,
746 gboolean retval = FALSE;
750 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
751 g_return_val_if_fail (file_name != NULL, FALSE);
753 key_file = g_key_file_new ();
754 gtk_page_setup_to_key_file (setup, key_file, NULL);
756 data = g_key_file_to_data (key_file, &len, error);
760 retval = g_file_set_contents (file_name, data, len, error);
763 g_key_file_free (key_file);
769 /* something like this should really be in gobject! */
771 enum_to_string (GType type,
774 GEnumClass *enum_class;
778 enum_class = g_type_class_ref (type);
780 value = g_enum_get_value (enum_class, enum_value);
782 retval = g_strdup (value->value_nick);
784 g_type_class_unref (enum_class);
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"
796 * This function adds the page setup from @setup to @key_file.
801 gtk_page_setup_to_key_file (GtkPageSetup *setup,
803 const gchar *group_name)
805 GtkPaperSize *paper_size;
808 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
809 g_return_if_fail (key_file != NULL);
812 group_name = KEYFILE_GROUP_NAME;
814 paper_size = gtk_page_setup_get_paper_size (setup);
815 g_assert (paper_size != NULL);
817 gtk_paper_size_to_key_file (paper_size, key_file, group_name);
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));
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);