gtkpagesetup.c 21.2 KB
Newer Older
Cody Russell's avatar
Cody Russell committed
1
/* GTK - The GIMP Toolkit
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
 * gtkpagesetup.c: Page Setup
 * Copyright (C) 2006, Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

#include "config.h"

#include "gtkpagesetup.h"
Matthias Clasen's avatar
Matthias Clasen committed
24
#include "gtkprintutils.h"
Matthias Clasen's avatar
Matthias Clasen committed
25 26 27
#include "gtkprintoperation.h" /* for GtkPrintError */
#include "gtkintl.h"
#include "gtktypebuiltins.h"
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80
/**
 * SECTION:gtkpagesetup
 * @Short_description: Stores page setup information
 * @Title: GtkPageSetup
 *
 * A GtkPageSetup object stores the page size, orientation and margins.
 * The idea is that you can get one of these from the page setup dialog
 * and then pass it to the #GtkPrintOperation when printing.
 * The benefit of splitting this out of the #GtkPrintSettings is that
 * these affect the actual layout of the page, and thus need to be set
 * long before user prints.
 *
 * <para id="print-margins">
 * The margins specified in this object are the "print margins", i.e. the
 * parts of the page that the printer cannot print on. These are different
 * from the layout margins that a word processor uses; they are typically
 * used to determine the <emphasis>minimal</emphasis> size for the layout
 * margins.
 * </para>
 *
 * To obtain a #GtkPageSetup use gtk_page_setup_new() to get the defaults,
 * or use gtk_print_run_page_setup_dialog() to show the page setup dialog
 * and receive the resulting page setup.
 *
 * <example>
 * <title>A page setup dialog</title>
 * <programlisting>
 * static GtkPrintSettings *settings = NULL;
 * static GtkPageSetup *page_setup = NULL;
 *
 * static void
 * do_page_setup (void)
 * {
 *   GtkPageSetup *new_page_setup;
 *
 *   if (settings == NULL)
 *     settings = gtk_print_settings_new (<!-- -->);
 *
 *   new_page_setup = gtk_print_run_page_setup_dialog (GTK_WINDOW (main_window),
 *                                                     page_setup, settings);
 *
 *   if (page_setup)
 *     g_object_unref (page_setup);
 *
 *   page_setup = new_page_setup;
 * }
 * </programlisting>
 * </example>
 *
 * Printing support was added in GTK+ 2.10.
 */

Matthias Clasen's avatar
Matthias Clasen committed
81
#define KEYFILE_GROUP_NAME "Page Setup"
82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133

typedef struct _GtkPageSetupClass GtkPageSetupClass;

#define GTK_IS_PAGE_SETUP_CLASS(klass)  (G_TYPE_CHECK_CLASS_TYPE ((klass), GTK_TYPE_PAGE_SETUP))
#define GTK_PAGE_SETUP_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))
#define GTK_PAGE_SETUP_GET_CLASS(obj)   (G_TYPE_INSTANCE_GET_CLASS ((obj), GTK_TYPE_PAGE_SETUP, GtkPageSetupClass))

struct _GtkPageSetup
{
  GObject parent_instance;

  GtkPageOrientation orientation;
  GtkPaperSize *paper_size;
  /* These are stored in mm */
  double top_margin, bottom_margin, left_margin, right_margin;
};

struct _GtkPageSetupClass
{
  GObjectClass parent_class;
};

G_DEFINE_TYPE (GtkPageSetup, gtk_page_setup, G_TYPE_OBJECT)

static void
gtk_page_setup_finalize (GObject *object)
{
  GtkPageSetup *setup = GTK_PAGE_SETUP (object);
  
  gtk_paper_size_free (setup->paper_size);
  
  G_OBJECT_CLASS (gtk_page_setup_parent_class)->finalize (object);
}

static void
gtk_page_setup_init (GtkPageSetup *setup)
{
  setup->paper_size = gtk_paper_size_new (NULL);
  setup->orientation = GTK_PAGE_ORIENTATION_PORTRAIT;
  setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
  setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
  setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
  setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
}

static void
gtk_page_setup_class_init (GtkPageSetupClass *class)
{
  GObjectClass *gobject_class = (GObjectClass *)class;

  gobject_class->finalize = gtk_page_setup_finalize;
}
Matthias Clasen's avatar
Matthias Clasen committed
134 135 136 137 138 139 140 141 142 143

/**
 * gtk_page_setup_new:
 *
 * Creates a new #GtkPageSetup. 
 * 
 * Return value: a new #GtkPageSetup.
 *
 * Since: 2.10
 */
144 145 146 147 148 149
GtkPageSetup *
gtk_page_setup_new (void)
{
  return g_object_new (GTK_TYPE_PAGE_SETUP, NULL);
}

Matthias Clasen's avatar
Matthias Clasen committed
150 151 152
/**
 * gtk_page_setup_copy:
 * @other: the #GtkPageSetup to copy
153
 *
Matthias Clasen's avatar
Matthias Clasen committed
154
 * Copies a #GtkPageSetup.
155 156
 *
 * Return value: (transfer full): a copy of @other
Matthias Clasen's avatar
Matthias Clasen committed
157 158 159
 *
 * Since: 2.10
 */
160 161 162 163 164 165 166
GtkPageSetup *
gtk_page_setup_copy (GtkPageSetup *other)
{
  GtkPageSetup *copy;

  copy = gtk_page_setup_new ();
  copy->orientation = other->orientation;
167
  gtk_paper_size_free (copy->paper_size);
168 169 170 171 172 173 174 175 176
  copy->paper_size = gtk_paper_size_copy (other->paper_size);
  copy->top_margin = other->top_margin;
  copy->bottom_margin = other->bottom_margin;
  copy->left_margin = other->left_margin;
  copy->right_margin = other->right_margin;

  return copy;
}

Matthias Clasen's avatar
Matthias Clasen committed
177 178 179 180 181 182 183 184 185 186
/**
 * gtk_page_setup_get_orientation:
 * @setup: a #GtkPageSetup
 * 
 * Gets the page orientation of the #GtkPageSetup.
 * 
 * Return value: the page orientation
 *
 * Since: 2.10
 */
187 188 189 190 191 192
GtkPageOrientation
gtk_page_setup_get_orientation (GtkPageSetup *setup)
{
  return setup->orientation;
}

Matthias Clasen's avatar
Matthias Clasen committed
193 194 195 196 197 198 199 200 201
/**
 * gtk_page_setup_set_orientation:
 * @setup: a #GtkPageSetup
 * @orientation: a #GtkPageOrientation value
 * 
 * Sets the page orientation of the #GtkPageSetup.
 *
 * Since: 2.10
 */
202
void
Matthias Clasen's avatar
Matthias Clasen committed
203 204
gtk_page_setup_set_orientation (GtkPageSetup       *setup,
				GtkPageOrientation  orientation)
205 206 207 208
{
  setup->orientation = orientation;
}

Matthias Clasen's avatar
Matthias Clasen committed
209 210 211 212 213 214 215 216 217 218
/**
 * gtk_page_setup_get_paper_size:
 * @setup: a #GtkPageSetup
 * 
 * Gets the paper size of the #GtkPageSetup.
 * 
 * Return value: the paper size
 *
 * Since: 2.10
 */
219 220 221
GtkPaperSize *
gtk_page_setup_get_paper_size (GtkPageSetup *setup)
{
222 223
  g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), NULL);

224 225 226
  return setup->paper_size;
}

Matthias Clasen's avatar
Matthias Clasen committed
227 228 229 230 231 232 233 234 235 236 237
/**
 * gtk_page_setup_set_paper_size:
 * @setup: a #GtkPageSetup
 * @size: a #GtkPaperSize 
 * 
 * Sets the paper size of the #GtkPageSetup without
 * changing the margins. See 
 * gtk_page_setup_set_paper_size_and_default_margins().
 *
 * Since: 2.10
 */
238 239 240 241
void
gtk_page_setup_set_paper_size (GtkPageSetup *setup,
			       GtkPaperSize *size)
{
242 243 244 245 246 247 248
  GtkPaperSize *old_size;

  g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
  g_return_if_fail (size != NULL);

  old_size = setup->paper_size;

249
  setup->paper_size = gtk_paper_size_copy (size);
250 251 252

  if (old_size)
    gtk_paper_size_free (old_size);
253 254
}

Matthias Clasen's avatar
Matthias Clasen committed
255 256 257 258 259 260 261 262 263 264
/**
 * gtk_page_setup_set_paper_size_and_default_margins:
 * @setup: a #GtkPageSetup
 * @size: a #GtkPaperSize 
 * 
 * Sets the paper size of the #GtkPageSetup and modifies
 * the margins according to the new paper size.
 *
 * Since: 2.10
 */
265 266 267 268
void
gtk_page_setup_set_paper_size_and_default_margins (GtkPageSetup *setup,
						   GtkPaperSize *size)
{
269
  gtk_page_setup_set_paper_size (setup, size);
270 271 272 273 274 275
  setup->top_margin = gtk_paper_size_get_default_top_margin (setup->paper_size, GTK_UNIT_MM);
  setup->bottom_margin = gtk_paper_size_get_default_bottom_margin (setup->paper_size, GTK_UNIT_MM);
  setup->left_margin = gtk_paper_size_get_default_left_margin (setup->paper_size, GTK_UNIT_MM);
  setup->right_margin = gtk_paper_size_get_default_right_margin (setup->paper_size, GTK_UNIT_MM);
}

Matthias Clasen's avatar
Matthias Clasen committed
276 277 278 279 280 281 282 283 284 285 286 287
/**
 * gtk_page_setup_get_top_margin:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Gets the top margin in units of @unit.
 * 
 * Return value: the top margin
 *
 * Since: 2.10
 */
gdouble
288 289 290
gtk_page_setup_get_top_margin (GtkPageSetup *setup,
			       GtkUnit       unit)
{
Matthias Clasen's avatar
Matthias Clasen committed
291
  return _gtk_print_convert_from_mm (setup->top_margin, unit);
292 293
}

Matthias Clasen's avatar
Matthias Clasen committed
294 295 296 297 298 299 300 301 302 303
/**
 * gtk_page_setup_set_top_margin:
 * @setup: a #GtkPageSetup
 * @margin: the new top margin in units of @unit
 * @unit: the units for @margin
 * 
 * Sets the top margin of the #GtkPageSetup.
 *
 * Since: 2.10
 */
304 305
void
gtk_page_setup_set_top_margin (GtkPageSetup *setup,
Matthias Clasen's avatar
Matthias Clasen committed
306
			       gdouble       margin,
307 308
			       GtkUnit       unit)
{
Matthias Clasen's avatar
Matthias Clasen committed
309
  setup->top_margin = _gtk_print_convert_to_mm (margin, unit);
310 311
}

Matthias Clasen's avatar
Matthias Clasen committed
312 313 314 315 316 317 318 319 320 321 322 323
/**
 * gtk_page_setup_get_bottom_margin:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Gets the bottom margin in units of @unit.
 * 
 * Return value: the bottom margin
 *
 * Since: 2.10
 */
gdouble
324 325 326
gtk_page_setup_get_bottom_margin (GtkPageSetup *setup,
				  GtkUnit       unit)
{
Matthias Clasen's avatar
Matthias Clasen committed
327
  return _gtk_print_convert_from_mm (setup->bottom_margin, unit);
328 329
}

Matthias Clasen's avatar
Matthias Clasen committed
330 331 332 333 334 335 336 337 338 339
/**
 * gtk_page_setup_set_bottom_margin:
 * @setup: a #GtkPageSetup
 * @margin: the new bottom margin in units of @unit
 * @unit: the units for @margin
 * 
 * Sets the bottom margin of the #GtkPageSetup.
 *
 * Since: 2.10
 */
340 341
void
gtk_page_setup_set_bottom_margin (GtkPageSetup *setup,
Matthias Clasen's avatar
Matthias Clasen committed
342
				  gdouble       margin,
343 344
				  GtkUnit       unit)
{
Matthias Clasen's avatar
Matthias Clasen committed
345
  setup->bottom_margin = _gtk_print_convert_to_mm (margin, unit);
346 347
}

Matthias Clasen's avatar
Matthias Clasen committed
348 349 350 351 352 353 354 355 356 357 358 359 360 361
/**
 * gtk_page_setup_get_left_margin:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Gets the left margin in units of @unit.
 * 
 * Return value: the left margin
 *
 * Since: 2.10
 */
gdouble
gtk_page_setup_get_left_margin (GtkPageSetup *setup,
				GtkUnit       unit)
362
{
Matthias Clasen's avatar
Matthias Clasen committed
363
  return _gtk_print_convert_from_mm (setup->left_margin, unit);
364 365
}

Matthias Clasen's avatar
Matthias Clasen committed
366 367 368 369 370 371 372 373 374 375
/**
 * gtk_page_setup_set_left_margin:
 * @setup: a #GtkPageSetup
 * @margin: the new left margin in units of @unit
 * @unit: the units for @margin
 * 
 * Sets the left margin of the #GtkPageSetup.
 *
 * Since: 2.10
 */
376
void
Matthias Clasen's avatar
Matthias Clasen committed
377 378 379
gtk_page_setup_set_left_margin (GtkPageSetup *setup,
				gdouble       margin,
				GtkUnit       unit)
380
{
Matthias Clasen's avatar
Matthias Clasen committed
381
  setup->left_margin = _gtk_print_convert_to_mm (margin, unit);
382 383
}

Matthias Clasen's avatar
Matthias Clasen committed
384 385 386 387 388 389 390 391 392 393 394 395 396 397
/**
 * gtk_page_setup_get_right_margin:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Gets the right margin in units of @unit.
 * 
 * Return value: the right margin
 *
 * Since: 2.10
 */
gdouble
gtk_page_setup_get_right_margin (GtkPageSetup *setup,
				 GtkUnit       unit)
398
{
Matthias Clasen's avatar
Matthias Clasen committed
399
  return _gtk_print_convert_from_mm (setup->right_margin, unit);
400 401
}

Matthias Clasen's avatar
Matthias Clasen committed
402 403 404 405 406 407 408 409 410 411
/**
 * gtk_page_setup_set_right_margin:
 * @setup: a #GtkPageSetup
 * @margin: the new right margin in units of @unit
 * @unit: the units for @margin
 * 
 * Sets the right margin of the #GtkPageSetup.
 *
 * Since: 2.10
 */
412
void
Matthias Clasen's avatar
Matthias Clasen committed
413 414 415
gtk_page_setup_set_right_margin (GtkPageSetup *setup,
				 gdouble       margin,
				 GtkUnit       unit)
416
{
Matthias Clasen's avatar
Matthias Clasen committed
417
  setup->right_margin = _gtk_print_convert_to_mm (margin, unit);
418 419
}

Matthias Clasen's avatar
Matthias Clasen committed
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
/**
 * gtk_page_setup_get_paper_width:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Returns the paper width in units of @unit.
 * 
 * Note that this function takes orientation, but 
 * not margins into consideration. 
 * See gtk_page_setup_get_page_width().
 *
 * Return value: the paper width.
 *
 * Since: 2.10
 */
gdouble
436 437 438 439 440 441 442 443 444 445
gtk_page_setup_get_paper_width (GtkPageSetup *setup,
				GtkUnit       unit)
{
  if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
      setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
    return gtk_paper_size_get_width (setup->paper_size, unit);
  else
    return gtk_paper_size_get_height (setup->paper_size, unit);
}

Matthias Clasen's avatar
Matthias Clasen committed
446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463
/**
 * gtk_page_setup_get_paper_height:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Returns the paper height in units of @unit.
 * 
 * Note that this function takes orientation, but 
 * not margins into consideration.
 * See gtk_page_setup_get_page_height().
 *
 * Return value: the paper height.
 *
 * Since: 2.10
 */
gdouble
gtk_page_setup_get_paper_height (GtkPageSetup *setup,
				 GtkUnit       unit)
464 465 466 467 468 469 470 471
{
  if (setup->orientation == GTK_PAGE_ORIENTATION_PORTRAIT ||
      setup->orientation == GTK_PAGE_ORIENTATION_REVERSE_PORTRAIT)
    return gtk_paper_size_get_height (setup->paper_size, unit);
  else
    return gtk_paper_size_get_width (setup->paper_size, unit);
}

Matthias Clasen's avatar
Matthias Clasen committed
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489
/**
 * gtk_page_setup_get_page_width:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Returns the page width in units of @unit.
 * 
 * Note that this function takes orientation and
 * margins into consideration. 
 * See gtk_page_setup_get_paper_width().
 *
 * Return value: the page width.
 *
 * Since: 2.10
 */
gdouble
gtk_page_setup_get_page_width (GtkPageSetup *setup,
			       GtkUnit       unit)
490
{
Matthias Clasen's avatar
Matthias Clasen committed
491
  gdouble width;
492 493 494 495
  
  width = gtk_page_setup_get_paper_width (setup, GTK_UNIT_MM);
  width -= setup->left_margin + setup->right_margin;
  
Matthias Clasen's avatar
Matthias Clasen committed
496
  return _gtk_print_convert_from_mm (width, unit);
497 498
}

Matthias Clasen's avatar
Matthias Clasen committed
499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516
/**
 * gtk_page_setup_get_page_height:
 * @setup: a #GtkPageSetup
 * @unit: the unit for the return value
 * 
 * Returns the page height in units of @unit.
 * 
 * Note that this function takes orientation and
 * margins into consideration. 
 * See gtk_page_setup_get_paper_height().
 *
 * Return value: the page height.
 *
 * Since: 2.10
 */
gdouble
gtk_page_setup_get_page_height (GtkPageSetup *setup,
				GtkUnit       unit)
517
{
Matthias Clasen's avatar
Matthias Clasen committed
518
  gdouble height;
519 520 521 522
  
  height = gtk_page_setup_get_paper_height (setup, GTK_UNIT_MM);
  height -= setup->top_margin + setup->bottom_margin;
  
Matthias Clasen's avatar
Matthias Clasen committed
523
  return _gtk_print_convert_from_mm (height, unit);
524 525
}

526 527 528
/**
 * gtk_page_setup_load_file:
 * @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
531 532
 *
 * Reads the page setup from the file @file_name.
533
 * See gtk_page_setup_to_file().
534 535 536
 *
 * Return value: %TRUE on success
 *
537
 * Since: 2.14
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559
 */
gboolean
gtk_page_setup_load_file (GtkPageSetup *setup,
                          const gchar  *file_name,
			  GError      **error)
{
  gboolean retval = FALSE;
  GKeyFile *key_file;

  g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
  g_return_val_if_fail (file_name != NULL, FALSE);

  key_file = g_key_file_new ();

  if (g_key_file_load_from_file (key_file, file_name, 0, error) &&
      gtk_page_setup_load_key_file (setup, key_file, NULL, error))
    retval = TRUE;

  g_key_file_free (key_file);

  return retval;
}
560

Matthias Clasen's avatar
Matthias Clasen committed
561 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
Matthias Clasen's avatar
Matthias Clasen committed
565 566 567 568 569 570 571 572 573 574 575 576 577
 * 
 * Reads the page setup from the file @file_name. Returns a 
 * new #GtkPageSetup object with the restored page setup, 
 * or %NULL if an error occurred. See gtk_page_setup_to_file().
 *
 * Return value: the restored #GtkPageSetup
 * 
 * Since: 2.12
 */
GtkPageSetup *
gtk_page_setup_new_from_file (const gchar  *file_name,
			      GError      **error)
{
578
  GtkPageSetup *setup = gtk_page_setup_new ();
Matthias Clasen's avatar
Matthias Clasen committed
579

580
  if (!gtk_page_setup_load_file (setup, file_name, error))
Matthias Clasen's avatar
Matthias Clasen committed
581
    {
582 583
      g_object_unref (setup);
      setup = NULL;
Matthias Clasen's avatar
Matthias Clasen committed
584 585
    }

586
  return setup;
Matthias Clasen's avatar
Matthias Clasen committed
587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
}

/* something like this should really be in gobject! */
static guint
string_to_enum (GType type,
                const char *enum_string)
{
  GEnumClass *enum_class;
  const GEnumValue *value;
  guint retval = 0;

  g_return_val_if_fail (enum_string != NULL, 0);

  enum_class = g_type_class_ref (type);
  value = g_enum_get_value_by_nick (enum_class, enum_string);
  if (value)
    retval = value->value;

  g_type_class_unref (enum_class);

  return retval;
}

/**
611 612
 * gtk_page_setup_load_key_file:
 * @setup: a #GtkPageSetup
Matthias Clasen's avatar
Matthias Clasen committed
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
Matthias Clasen's avatar
Matthias Clasen committed
615
 *              to use the default name "Page Setup"
616
 * @error: (allow-none): return location for an error, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
617 618
 * 
 * Reads the page setup from the group @group_name in the key file
619
 * @key_file.
Matthias Clasen's avatar
Matthias Clasen committed
620
 * 
621 622
 * Return value: %TRUE on success
 *
623
 * Since: 2.14
Matthias Clasen's avatar
Matthias Clasen committed
624
 */
625 626 627 628 629
gboolean
gtk_page_setup_load_key_file (GtkPageSetup *setup,
                              GKeyFile     *key_file,
                              const gchar  *group_name,
                              GError      **error)
Matthias Clasen's avatar
Matthias Clasen committed
630 631 632 633
{
  GtkPaperSize *paper_size;
  gdouble top, bottom, left, right;
  char *orientation = NULL, *freeme = NULL;
634
  gboolean retval = FALSE;
Matthias Clasen's avatar
Matthias Clasen committed
635 636
  GError *err = NULL;

637 638
  g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
  g_return_val_if_fail (key_file != NULL, FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
639 640 641 642 643 644

  if (!group_name)
    group_name = KEYFILE_GROUP_NAME;

  if (!g_key_file_has_group (key_file, group_name))
    {
645 646 647 648
      g_set_error_literal (error,
                           GTK_PRINT_ERROR,
                           GTK_PRINT_ERROR_INVALID_FILE,
                           _("Not a valid page setup file"));
Matthias Clasen's avatar
Matthias Clasen committed
649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
      goto out;
    }

#define GET_DOUBLE(kf, group, name, v) \
  v = g_key_file_get_double (kf, group, name, &err); \
  if (err != NULL) \
    { \
      g_propagate_error (error, err);\
      goto out;\
    }

  GET_DOUBLE (key_file, group_name, "MarginTop", top);
  GET_DOUBLE (key_file, group_name, "MarginBottom", bottom);
  GET_DOUBLE (key_file, group_name, "MarginLeft", left);
  GET_DOUBLE (key_file, group_name, "MarginRight", right);

#undef GET_DOUBLE

  paper_size = gtk_paper_size_new_from_key_file (key_file, group_name, &err);
  if (!paper_size)
    {
      g_propagate_error (error, err);
      goto out;
    }

674
  gtk_page_setup_set_paper_size (setup, paper_size);
Matthias Clasen's avatar
Matthias Clasen committed
675 676
  gtk_paper_size_free (paper_size);

677 678 679 680
  gtk_page_setup_set_top_margin (setup, top, GTK_UNIT_MM);
  gtk_page_setup_set_bottom_margin (setup, bottom, GTK_UNIT_MM);
  gtk_page_setup_set_left_margin (setup, left, GTK_UNIT_MM);
  gtk_page_setup_set_right_margin (setup, right, GTK_UNIT_MM);
Matthias Clasen's avatar
Matthias Clasen committed
681 682 683 684 685

  orientation = g_key_file_get_string (key_file, group_name,
				       "Orientation", NULL);
  if (orientation)
    {
686
      gtk_page_setup_set_orientation (setup,
Matthias Clasen's avatar
Matthias Clasen committed
687 688 689 690 691
				      string_to_enum (GTK_TYPE_PAGE_ORIENTATION,
						      orientation));
      g_free (orientation);
    }

692 693
  retval = TRUE;

Matthias Clasen's avatar
Matthias Clasen committed
694 695
out:
  g_free (freeme);
696 697 698 699 700 701
  return retval;
}

/**
 * gtk_page_setup_new_from_key_file:
 * @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
705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725
 *
 * Reads the page setup from the group @group_name in the key file
 * @key_file. Returns a new #GtkPageSetup object with the restored
 * page setup, or %NULL if an error occurred.
 *
 * Return value: the restored #GtkPageSetup
 *
 * Since: 2.12
 */
GtkPageSetup *
gtk_page_setup_new_from_key_file (GKeyFile     *key_file,
				  const gchar  *group_name,
				  GError      **error)
{
  GtkPageSetup *setup = gtk_page_setup_new ();

  if (!gtk_page_setup_load_key_file (setup, key_file, group_name, error))
    {
      g_object_unref (setup);
      setup = NULL;
    }
Matthias Clasen's avatar
Matthias Clasen committed
726

727
  return setup;
Matthias Clasen's avatar
Matthias Clasen committed
728 729 730 731
}

/**
 * gtk_page_setup_to_file:
Matthias Clasen's avatar
Matthias Clasen committed
732
 * @setup: a #GtkPageSetup
733
 * @file_name: (type filename): the file to save to
734
 * @error: (allow-none): return location for errors, or %NULL
Matthias Clasen's avatar
Matthias Clasen committed
735
 * 
Matthias Clasen's avatar
Matthias Clasen committed
736
 * This function saves the information from @setup to @file_name.
Matthias Clasen's avatar
Matthias Clasen committed
737 738 739 740 741 742
 * 
 * Return value: %TRUE on success
 *
 * Since: 2.12
 */
gboolean
Matthias Clasen's avatar
Matthias Clasen committed
743
gtk_page_setup_to_file (GtkPageSetup  *setup,
Matthias Clasen's avatar
Matthias Clasen committed
744 745 746 747 748 749 750 751
		        const char    *file_name,
			GError       **error)
{
  GKeyFile *key_file;
  gboolean retval = FALSE;
  char *data = NULL;
  gsize len;

Matthias Clasen's avatar
Matthias Clasen committed
752
  g_return_val_if_fail (GTK_IS_PAGE_SETUP (setup), FALSE);
Matthias Clasen's avatar
Matthias Clasen committed
753 754 755
  g_return_val_if_fail (file_name != NULL, FALSE);

  key_file = g_key_file_new ();
Matthias Clasen's avatar
Matthias Clasen committed
756
  gtk_page_setup_to_key_file (setup, key_file, NULL);
Matthias Clasen's avatar
Matthias Clasen committed
757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792

  data = g_key_file_to_data (key_file, &len, error);
  if (!data)
    goto out;

  retval = g_file_set_contents (file_name, data, len, error);

out:
  g_key_file_free (key_file);
  g_free (data);

  return retval;
}

/* something like this should really be in gobject! */
static char *
enum_to_string (GType type,
                guint enum_value)
{
  GEnumClass *enum_class;
  GEnumValue *value;
  char *retval = NULL;

  enum_class = g_type_class_ref (type);

  value = g_enum_get_value (enum_class, enum_value);
  if (value)
    retval = g_strdup (value->value_nick);

  g_type_class_unref (enum_class);

  return retval;
}

/**
 * gtk_page_setup_to_key_file:
Matthias Clasen's avatar
Matthias Clasen committed
793
 * @setup: a #GtkPageSetup
Matthias Clasen's avatar
Matthias Clasen committed
794 795 796 797
 * @key_file: the #GKeyFile to save the page setup to
 * @group_name: the group to add the settings to in @key_file, 
 *      or %NULL to use the default name "Page Setup"
 * 
Matthias Clasen's avatar
Matthias Clasen committed
798
 * This function adds the page setup from @setup to @key_file.
Matthias Clasen's avatar
Matthias Clasen committed
799 800 801 802
 * 
 * Since: 2.12
 */
void
Matthias Clasen's avatar
Matthias Clasen committed
803
gtk_page_setup_to_key_file (GtkPageSetup *setup,
Matthias Clasen's avatar
Matthias Clasen committed
804 805 806 807 808 809
			    GKeyFile     *key_file,
			    const gchar  *group_name)
{
  GtkPaperSize *paper_size;
  char *orientation;

Matthias Clasen's avatar
Matthias Clasen committed
810
  g_return_if_fail (GTK_IS_PAGE_SETUP (setup));
Matthias Clasen's avatar
Matthias Clasen committed
811 812 813 814 815
  g_return_if_fail (key_file != NULL);

  if (!group_name)
    group_name = KEYFILE_GROUP_NAME;

Matthias Clasen's avatar
Matthias Clasen committed
816
  paper_size = gtk_page_setup_get_paper_size (setup);
Matthias Clasen's avatar
Matthias Clasen committed
817 818 819 820 821
  g_assert (paper_size != NULL);

  gtk_paper_size_to_key_file (paper_size, key_file, group_name);

  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
822
			 "MarginTop", gtk_page_setup_get_top_margin (setup, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
823
  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
824
			 "MarginBottom", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
825
  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
826
			 "MarginLeft", gtk_page_setup_get_left_margin (setup, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
827
  g_key_file_set_double (key_file, group_name,
Matthias Clasen's avatar
Matthias Clasen committed
828
			 "MarginRight", gtk_page_setup_get_right_margin (setup, GTK_UNIT_MM));
Matthias Clasen's avatar
Matthias Clasen committed
829 830

  orientation = enum_to_string (GTK_TYPE_PAGE_ORIENTATION,
Matthias Clasen's avatar
Matthias Clasen committed
831
				gtk_page_setup_get_orientation (setup));
Matthias Clasen's avatar
Matthias Clasen committed
832 833 834 835
  g_key_file_set_string (key_file, group_name,
			 "Orientation", orientation);
  g_free (orientation);
}