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 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);
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
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().
484 * Return value: the restored #GtkPageSetup
489 gtk_page_setup_new_from_file (const gchar *file_name,
492 GtkPageSetup *page_setup;
496 g_return_val_if_fail (file_name != NULL, NULL);
498 key_file = g_key_file_new ();
499 if (!g_key_file_load_from_file (key_file, file_name, 0, &err))
501 g_key_file_free (key_file);
502 g_propagate_error (error, err);
506 page_setup = gtk_page_setup_new_from_key_file (key_file, NULL, error);
507 g_key_file_free (key_file);
512 /* something like this should really be in gobject! */
514 string_to_enum (GType type,
515 const char *enum_string)
517 GEnumClass *enum_class;
518 const GEnumValue *value;
521 g_return_val_if_fail (enum_string != NULL, 0);
523 enum_class = g_type_class_ref (type);
524 value = g_enum_get_value_by_nick (enum_class, enum_string);
526 retval = value->value;
528 g_type_class_unref (enum_class);
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
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.
544 * Return value: the restored #GtkPageSetup
549 gtk_page_setup_new_from_key_file (GKeyFile *key_file,
550 const gchar *group_name,
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;
560 g_return_val_if_fail (key_file != NULL, NULL);
563 group_name = KEYFILE_GROUP_NAME;
565 if (!g_key_file_has_group (key_file, group_name))
569 GTK_PRINT_ERROR_INVALID_FILE,
570 _("Not a valid page setup file"));
575 #define GET_DOUBLE(kf, group, name, v) \
576 v = g_key_file_get_double (kf, group, name, &err); \
579 g_propagate_error (error, err);\
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);
591 paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
594 g_propagate_error (error, err);
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);
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);
607 orientation = g_key_file_get_string (key_file, group_name,
608 "Orientation", NULL);
611 gtk_page_setup_set_orientation (page_setup,
612 string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
614 g_free (orientation);
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
629 * This function saves the information from @setup to @file_name.
631 * Return value: %TRUE on success
636 gtk_page_setup_to_file (GtkPageSetup *setup,
637 const char *file_name,
641 gboolean retval = FALSE;
645 g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
646 g_return_val_if_fail (file_name != NULL, FALSE);
648 key_file = g_key_file_new ();
649 gtk_page_setup_to_key_file (setup, key_file, NULL);
651 data = g_key_file_to_data (key_file, &len, error);
655 retval = g_file_set_contents (file_name, data, len, error);
658 g_key_file_free (key_file);
664 /* something like this should really be in gobject! */
666 enum_to_string (GType type,
669 GEnumClass *enum_class;
673 enum_class = g_type_class_ref (type);
675 value = g_enum_get_value (enum_class, enum_value);
677 retval = g_strdup (value->value_nick);
679 g_type_class_unref (enum_class);
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"
691 * This function adds the page setup from @setup to @key_file.
696 gtk_page_setup_to_key_file (GtkPageSetup *setup,
698 const gchar *group_name)
700 GtkPaperSize *paper_size;
703 g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
704 g_return_if_fail (key_file != NULL);
707 group_name = KEYFILE_GROUP_NAME;
709 paper_size = gtk_page_setup_get_paper_size (setup);
710 g_assert (paper_size != NULL);
712 gtk_paper_size_to_key_file (paper_size, key_file, group_name);
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));
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);
730 #define __GTK_PAGE_SETUP_C__
731 #include "gtkaliasdef.c"