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 * SECTION:gtkpagesetup
31 * @Short_description: Stores page setup information
32 * @Title: GtkPageSetup
34 * A GtkPageSetup object stores the page size, orientation and margins.
35 * The idea is that you can get one of these from the page setup dialog
36 * and then pass it to the #GtkPrintOperation when printing.
37 * The benefit of splitting this out of the #GtkPrintSettings is that
38 * these affect the actual layout of the page, and thus need to be set
39 * long before user prints.
41 * <para id="print-margins">
42 * The margins specified in this object are the "print margins", i.e. the
43 * parts of the page that the printer cannot print on. These are different
44 * from the layout margins that a word processor uses; they are typically
45 * used to determine the <emphasis>minimal</emphasis> size for the layout
49 * To obtain a #GtkPageSetup use gtk_page_setup_new() to get the defaults,
50 * or use gtk_print_run_page_setup_dialog() to show the page setup dialog
51 * and receive the resulting page setup.
54 * <title>A page setup dialog</title>
56 * static GtkPrintSettings *settings = NULL;
57 * static GtkPageSetup *page_setup = NULL;
60 * do_page_setup (void)
62 * GtkPageSetup *new_page_setup;
64 * if (settings == NULL)
65 * settings = gtk_print_settings_new (<!-- -->);
67 * new_page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (main_window),
68 * page_setup, settings);
71 * g_object_unref (page_setup);
73 * page_setup = new_page_setup;
78 * Printing support was added in GTK+ 2.10.
81 #define KEYFILE_GROUP_NAME "Page Setup"
83 typedef struct _GtkPageSetupClass GtkPageSetupClass;
85 #define GTK_IS_PAGE_SETUP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PAGE_SETUP))
86 #define GTK_PAGE_SETUP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
87 #define GTK_PAGE_SETUP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
91 GObject parent_instance;
93 GtkPageOrientation orientation;
94 GtkPaperSize *paper_size;
95 /* These are stored in mm */
96 double top_margin, bottom_margin, left_margin, right_margin;
99 struct _GtkPageSetupClass
101 GObjectClass parent_class;
104 G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)
107 gtk_page_setup_finalize (GObject *object)
109 GtkPageSetup *setup = GTK_PAGE_SETUP (object);
111 gtk_paper_size_free (setup->paper_size);
113 G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
117 gtk_page_setup_init (GtkPageSetup *setup)
119 setup->paper_size = gtk_paper_size_new (NULL);
120 setup->orientation = GTK_PAGE_ORIENTATION_PORTRAIT;
121 setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
122 setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
123 setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
124 setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
128 gtk_page_setup_class_init (GtkPageSetupClass *class)
130 GObjectClass *gobject_class = (GObjectClass *)class;
132 gobject_class->finalize = gtk_page_setup_finalize;
136 * gtk_page_setup_new:
138 * Creates a new #GtkPageSetup.
140 * Return value: a new #GtkPageSetup.
145 gtk_page_setup_new (void)
147 return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
151 * gtk_page_setup_copy:
152 * @other: the #GtkPageSetup to copy
154 * Copies a #GtkPageSetup.
156 * Return value: (transfer full): a copy of @other
161 gtk_page_setup_copy (GtkPageSetup *other)
165 copy = gtk_page_setup_new ();
166 copy->orientation = other->orientation;
167 gtk_paper_size_free (copy->paper_size);
168 copy->paper_size = gtk_paper_size_copy (other->paper_size);
169 copy->top_margin = other->top_margin;
170 copy->bottom_margin = other->bottom_margin;
171 copy->left_margin = other->left_margin;
172 copy->right_margin = other->right_margin;
178 * gtk_page_setup_get_orientation:
179 * @setup: a #GtkPageSetup
181 * Gets the page orientation of the #GtkPageSetup.
183 * Return value: the page orientation
188 gtk_page_setup_get_orientation (GtkPageSetup *setup)
190 return setup->orientation;
194 * gtk_page_setup_set_orientation:
195 * @setup: a #GtkPageSetup
196 * @orientation: a #GtkPageOrientation value
198 * Sets the page orientation of the #GtkPageSetup.
203 gtk_page_setup_set_orientation (GtkPageSetup *setup,
204 GtkPageOrientation orientation)
206 setup->orientation = orientation;
210 * gtk_page_setup_get_paper_size:
211 * @setup: a #GtkPageSetup
213 * Gets the paper size of the #GtkPageSetup.
215 * Return value: the paper size
220 gtk_page_setup_get_paper_size (GtkPageSetup *setup)
222 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);
224 return setup->paper_size;
228 * gtk_page_setup_set_paper_size:
229 * @setup: a #GtkPageSetup
230 * @size: a #GtkPaperSize
232 * Sets the paper size of the #GtkPageSetup without
233 * changing the margins. See
234 * gtk_page_setup_set_paper_size_and_default_margins().
239 gtk_page_setup_set_paper_size (GtkPageSetup *setup,
242 GtkPaperSize *old_size;
244 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
245 g_return_if_fail (size != NULL);
247 old_size = setup->paper_size;
249 setup->paper_size = gtk_paper_size_copy (size);
252 gtk_paper_size_free (old_size);
256 * gtk_page_setup_set_paper_size_and_default_margins:
257 * @setup: a #GtkPageSetup
258 * @size: a #GtkPaperSize
260 * Sets the paper size of the #GtkPageSetup and modifies
261 * the margins according to the new paper size.
266 gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
269 gtk_page_setup_set_paper_size (setup, size);
270 setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
271 setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
272 setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
273 setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
277 * gtk_page_setup_get_top_margin:
278 * @setup: a #GtkPageSetup
279 * @unit: the unit for the return value
281 * Gets the top margin in units of @unit.
283 * Return value: the top margin
288 gtk_page_setup_get_top_margin (GtkPageSetup *setup,
291 return _gtk_print_convert_from_mm (setup->top_margin, unit);
295 * gtk_page_setup_set_top_margin:
296 * @setup: a #GtkPageSetup
297 * @margin: the new top margin in units of @unit
298 * @unit: the units for @margin
300 * Sets the top margin of the #GtkPageSetup.
305 gtk_page_setup_set_top_margin (GtkPageSetup *setup,
309 setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
313 * gtk_page_setup_get_bottom_margin:
314 * @setup: a #GtkPageSetup
315 * @unit: the unit for the return value
317 * Gets the bottom margin in units of @unit.
319 * Return value: the bottom margin
324 gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
327 return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
331 * gtk_page_setup_set_bottom_margin:
332 * @setup: a #GtkPageSetup
333 * @margin: the new bottom margin in units of @unit
334 * @unit: the units for @margin
336 * Sets the bottom margin of the #GtkPageSetup.
341 gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
345 setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
349 * gtk_page_setup_get_left_margin:
350 * @setup: a #GtkPageSetup
351 * @unit: the unit for the return value
353 * Gets the left margin in units of @unit.
355 * Return value: the left margin
360 gtk_page_setup_get_left_margin (GtkPageSetup *setup,
363 return _gtk_print_convert_from_mm (setup->left_margin, unit);
367 * gtk_page_setup_set_left_margin:
368 * @setup: a #GtkPageSetup
369 * @margin: the new left margin in units of @unit
370 * @unit: the units for @margin
372 * Sets the left margin of the #GtkPageSetup.
377 gtk_page_setup_set_left_margin (GtkPageSetup *setup,
381 setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
385 * gtk_page_setup_get_right_margin:
386 * @setup: a #GtkPageSetup
387 * @unit: the unit for the return value
389 * Gets the right margin in units of @unit.
391 * Return value: the right margin
396 gtk_page_setup_get_right_margin (GtkPageSetup *setup,
399 return _gtk_print_convert_from_mm (setup->right_margin, unit);
403 * gtk_page_setup_set_right_margin:
404 * @setup: a #GtkPageSetup
405 * @margin: the new right margin in units of @unit
406 * @unit: the units for @margin
408 * Sets the right margin of the #GtkPageSetup.
413 gtk_page_setup_set_right_margin (GtkPageSetup *setup,
417 setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
421 * gtk_page_setup_get_paper_width:
422 * @setup: a #GtkPageSetup
423 * @unit: the unit for the return value
425 * Returns the paper width in units of @unit.
427 * Note that this function takes orientation, but
428 * not margins into consideration.
429 * See gtk_page_setup_get_page_width().
431 * Return value: the paper width.
436 gtk_page_setup_get_paper_width (GtkPageSetup *setup,
439 if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
440 setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
441 return gtk_paper_size_get_width (setup->paper_size, unit);
443 return gtk_paper_size_get_height (setup->paper_size, unit);
447 * gtk_page_setup_get_paper_height:
448 * @setup: a #GtkPageSetup
449 * @unit: the unit for the return value
451 * Returns the paper height in units of @unit.
453 * Note that this function takes orientation, but
454 * not margins into consideration.
455 * See gtk_page_setup_get_page_height().
457 * Return value: the paper height.
462 gtk_page_setup_get_paper_height (GtkPageSetup *setup,
465 if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
466 setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
467 return gtk_paper_size_get_height (setup->paper_size, unit);
469 return gtk_paper_size_get_width (setup->paper_size, unit);
473 * gtk_page_setup_get_page_width:
474 * @setup: a #GtkPageSetup
475 * @unit: the unit for the return value
477 * Returns the page width in units of @unit.
479 * Note that this function takes orientation and
480 * margins into consideration.
481 * See gtk_page_setup_get_paper_width().
483 * Return value: the page width.
488 gtk_page_setup_get_page_width (GtkPageSetup *setup,
493 width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
494 width -= setup->left_margin + setup->right_margin;
496 return _gtk_print_convert_from_mm (width, unit);
500 * gtk_page_setup_get_page_height:
501 * @setup: a #GtkPageSetup
502 * @unit: the unit for the return value
504 * Returns the page height in units of @unit.
506 * Note that this function takes orientation and
507 * margins into consideration.
508 * See gtk_page_setup_get_paper_height().
510 * Return value: the page height.
515 gtk_page_setup_get_page_height (GtkPageSetup *setup,
520 height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
521 height -= setup->top_margin + setup->bottom_margin;
523 return _gtk_print_convert_from_mm (height, unit);
527 * gtk_page_setup_load_file:
528 * @setup: a #GtkPageSetup
529 * @file_name: (type filename): the filename to read the page setup from
530 * @error: (allow-none): return location for an error, or %NULL
532 * Reads the page setup from the file @file_name.
533 * See gtk_page_setup_to_file().
535 * Return value: %TRUE on success
540 gtk_page_setup_load_file (GtkPageSetup *setup,
541 const gchar *file_name,
544 gboolean retval = FALSE;
547 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
548 g_return_val_if_fail (file_name != NULL, FALSE);
550 key_file = g_key_file_new ();
552 if (g_key_file_load_from_file (key_file, file_name, 0, error) &&
553 gtk_page_setup_load_key_file (setup, key_file, NULL, error))
556 g_key_file_free (key_file);
562 * gtk_page_setup_new_from_file:
563 * @file_name: (type filename): the filename to read the page setup from
564 * @error: (allow-none): return location for an error, or %NULL
566 * Reads the page setup from the file @file_name. Returns a
567 * new #GtkPageSetup object with the restored page setup,
568 * or %NULL if an error occurred. See gtk_page_setup_to_file().
570 * Return value: the restored #GtkPageSetup
575 gtk_page_setup_new_from_file (const gchar *file_name,
578 GtkPageSetup *setup = gtk_page_setup_new ();
580 if (!gtk_page_setup_load_file (setup, file_name, error))
582 g_object_unref (setup);
589 /* something like this should really be in gobject! */
591 string_to_enum (GType type,
592 const char *enum_string)
594 GEnumClass *enum_class;
595 const GEnumValue *value;
598 g_return_val_if_fail (enum_string != NULL, 0);
600 enum_class = g_type_class_ref (type);
601 value = g_enum_get_value_by_nick (enum_class, enum_string);
603 retval = value->value;
605 g_type_class_unref (enum_class);
611 * gtk_page_setup_load_key_file:
612 * @setup: a #GtkPageSetup
613 * @key_file: the #GKeyFile to retrieve the page_setup from
614 * @group_name: (allow-none): the name of the group in the key_file to read, or %NULL
615 * to use the default name "Page Setup"
616 * @error: (allow-none): return location for an error, or %NULL
618 * Reads the page setup from the group @group_name in the key file
621 * Return value: %TRUE on success
626 gtk_page_setup_load_key_file (GtkPageSetup *setup,
628 const gchar *group_name,
631 GtkPaperSize *paper_size;
632 gdouble top, bottom, left, right;
633 char *orientation = NULL, *freeme = NULL;
634 gboolean retval = FALSE;
637 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
638 g_return_val_if_fail (key_file != NULL, FALSE);
641 group_name = KEYFILE_GROUP_NAME;
643 if (!g_key_file_has_group (key_file, group_name))
645 g_set_error_literal (error,
647 GTK_PRINT_ERROR_INVALID_FILE,
648 _("Not a valid page setup file"));
652 #define GET_DOUBLE(kf, group, name, v) \
653 v = g_key_file_get_double (kf, group, name, &err); \
656 g_propagate_error (error, err);\
660 GET_DOUBLE (key_file, group_name, "MarginTop", top);
661 GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
662 GET_DOUBLE (key_file, group_name, "MarginLeft", left);
663 GET_DOUBLE (key_file, group_name, "MarginRight", right);
667 paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
670 g_propagate_error (error, err);
674 gtk_page_setup_set_paper_size (setup, paper_size);
675 gtk_paper_size_free (paper_size);
677 gtk_page_setup_set_top_margin (setup, top, GTK_UNIT_MM);
678 gtk_page_setup_set_bottom_margin (setup, bottom, GTK_UNIT_MM);
679 gtk_page_setup_set_left_margin (setup, left, GTK_UNIT_MM);
680 gtk_page_setup_set_right_margin (setup, right, GTK_UNIT_MM);
682 orientation = g_key_file_get_string (key_file, group_name,
683 "Orientation", NULL);
686 gtk_page_setup_set_orientation (setup,
687 string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
689 g_free (orientation);
700 * gtk_page_setup_new_from_key_file:
701 * @key_file: the #GKeyFile to retrieve the page_setup from
702 * @group_name: (allow-none): the name of the group in the key_file to read, or %NULL
703 * to use the default name "Page Setup"
704 * @error: (allow-none): return location for an error, or %NULL
706 * Reads the page setup from the group @group_name in the key file
707 * @key_file. Returns a new #GtkPageSetup object with the restored
708 * page setup, or %NULL if an error occurred.
710 * Return value: the restored #GtkPageSetup
715 gtk_page_setup_new_from_key_file (GKeyFile *key_file,
716 const gchar *group_name,
719 GtkPageSetup *setup = gtk_page_setup_new ();
721 if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
723 g_object_unref (setup);
731 * gtk_page_setup_to_file:
732 * @setup: a #GtkPageSetup
733 * @file_name: (type filename): the file to save to
734 * @error: (allow-none): return location for errors, or %NULL
736 * This function saves the information from @setup to @file_name.
738 * Return value: %TRUE on success
743 gtk_page_setup_to_file (GtkPageSetup *setup,
744 const char *file_name,
748 gboolean retval = FALSE;
752 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
753 g_return_val_if_fail (file_name != NULL, FALSE);
755 key_file = g_key_file_new ();
756 gtk_page_setup_to_key_file (setup, key_file, NULL);
758 data = g_key_file_to_data (key_file, &len, error);
762 retval = g_file_set_contents (file_name, data, len, error);
765 g_key_file_free (key_file);
771 /* something like this should really be in gobject! */
773 enum_to_string (GType type,
776 GEnumClass *enum_class;
780 enum_class = g_type_class_ref (type);
782 value = g_enum_get_value (enum_class, enum_value);
784 retval = g_strdup (value->value_nick);
786 g_type_class_unref (enum_class);
792 * gtk_page_setup_to_key_file:
793 * @setup: a #GtkPageSetup
794 * @key_file: the #GKeyFile to save the page setup to
795 * @group_name: the group to add the settings to in @key_file,
796 * or %NULL to use the default name "Page Setup"
798 * This function adds the page setup from @setup to @key_file.
803 gtk_page_setup_to_key_file (GtkPageSetup *setup,
805 const gchar *group_name)
807 GtkPaperSize *paper_size;
810 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
811 g_return_if_fail (key_file != NULL);
814 group_name = KEYFILE_GROUP_NAME;
816 paper_size = gtk_page_setup_get_paper_size (setup);
817 g_assert (paper_size != NULL);
819 gtk_paper_size_to_key_file (paper_size, key_file, group_name);
821 g_key_file_set_double (key_file, group_name,
822 "MarginTop", gtk_page_setup_get_top_margin (setup, GTK_UNIT_MM));
823 g_key_file_set_double (key_file, group_name,
824 "MarginBottom", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM));
825 g_key_file_set_double (key_file, group_name,
826 "MarginLeft", gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM));
827 g_key_file_set_double (key_file, group_name,
828 "MarginRight", gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM));
830 orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
831 gtk_page_setup_get_orientation (setup));
832 g_key_file_set_string (key_file, group_name,
833 "Orientation", orientation);
834 g_free (orientation);