dialog-printer-setup.c 50.8 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2
/**
3 4 5
 * dialog-printer-setup.c: Printer setup dialog box
 *
 * Author:
6
 *  Wayne Schuller (k_wayne@linuxpower.org)
7
 *  Miguel de Icaza (miguel@gnu.org)
jpekka's avatar
jpekka committed
8 9 10 11 12 13 14 15 16 17 18 19 20 21
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
22
 */
23
#include <gnumeric-config.h>
24
#include <glib/gi18n-lib.h>
25 26
#include <gnumeric.h>
#include "dialogs.h"
27
#include "help.h"
28 29

#include <gui-util.h>
30
#include <commands.h>
31 32 33 34 35 36 37
#include <print-info.h>
#include <print.h>
#include <ranges.h>
#include <sheet.h>
#include <workbook.h>
#include <workbook-edit.h>
#include <style.h>
38
#include <gnumeric-gconf.h>
39

40
#include <libgnomeprint/gnome-print-job.h>
41
#include <libgnomeprint/gnome-print-unit.h>
42
#include <libgnomeprintui/gnome-print-paper-selector.h>
43
#include <libgnomeprintui/gnome-print-unit-selector.h>
44

45 46 47 48 49
#include <goffice/cut-n-paste/foocanvas/foo-canvas.h>
#include <goffice/cut-n-paste/foocanvas/foo-canvas-util.h>
#include <goffice/cut-n-paste/foocanvas/foo-canvas-line.h>
#include <goffice/cut-n-paste/foocanvas/foo-canvas-text.h>
#include <goffice/cut-n-paste/foocanvas/foo-canvas-rect-ellipse.h>
50
#include <glade/glade.h>
51 52 53 54
#include <gtk/gtkspinbutton.h>
#include <gtk/gtknotebook.h>
#include <gtk/gtktable.h>
#include <gtk/gtktogglebutton.h>
55 56 57 58 59
#include <gtk/gtkbox.h>
#include <gtk/gtkcombobox.h>
#include <gtk/gtkcelllayout.h>
#include <gtk/gtkcellrenderertext.h>
/*#include <gtk/gtkmenuitem.h>*/
60

61
/* FIXME: do not hardcode pixel counts.  */
62
#define PREVIEW_X 170
63
#define PREVIEW_Y 170
64 65 66 67 68
#define PREVIEW_MARGIN_X 20
#define PREVIEW_MARGIN_Y 20
#define PAGE_X (PREVIEW_X - PREVIEW_MARGIN_X)
#define PAGE_Y (PREVIEW_Y - PREVIEW_MARGIN_Y)

69 70 71
#define MARGIN_COLOR_DEFAULT "gray"
#define MARGIN_COLOR_ACTIVE "black"

72 73 74 75 76 77
#define HF_PREVIEW_X 350
#define HF_PREVIEW_Y 50
#define HF_PREVIEW_SHADOW 2
#define HF_PREVIEW_PADDING 5
#define HF_PREVIEW_MARGIN 15

78 79
#define PRINTER_SETUP_KEY "printer-setup-dialog"

80
#define HF_PAGE 1
81

82 83 84 85 86
/* FIXME: Now that we have added a header/footer sample
 * preview widget, we should rename the preview widget for the margins
 * to be more specific.
 * eg: PreviewInfo should become MarginPreviewInfo.
 */
87
typedef struct {
88
	/* The Canvas object */
89 90 91
	GtkWidget        *canvas;

	/* Objects in the Preview Canvas */
92
	FooCanvasItem  *group;
93

94 95 96 97 98
	/* Values for the scaling of the nice preview */
	int offset_x, offset_y;	/* For centering the small page preview */
	double scale;
} PreviewInfo;

99 100 101 102 103
typedef struct {
	/* The Canvas object for the header/footer sample preview */
	GtkWidget        *canvas;

	/* Objects in the Preview Canvas */
104 105 106
	FooCanvasItem  *left;
	FooCanvasItem  *middle;
	FooCanvasItem  *right;
107 108 109

} HFPreviewInfo;

110
typedef enum {
111
	MARGIN_NONE,
112 113 114 115 116 117 118 119 120 121 122 123 124
	MARGIN_LEFT,
	MARGIN_RIGHT,
	MARGIN_TOP,
	MARGIN_BOTTOM,
	MARGIN_HEADER,
	MARGIN_FOOTER
} MarginOrientation;

typedef struct {
	double     value;
	GtkSpinButton *spin;
	GtkAdjustment *adj;

125
	FooCanvasItem *line;
126
	GnomePrintUnit const *unit;
127 128 129 130 131
	MarginOrientation orientation;
	double bound_x1, bound_y1, bound_x2, bound_y2;
	PreviewInfo *pi;
} UnitInfo;

132
typedef struct {
133
	WorkbookControlGUI  *wbcg;
134
	Sheet            *sheet;
135 136
	GladeXML         *gui;
	PrintInformation *pi;
137
	GnomePrintConfig *gp_config;
138
	GtkWidget        *dialog;
139
	GtkWidget        *sheet_selector;
140
	GtkWidget        *unit_selector;
141 142

	struct {
143
		UnitInfo top, bottom, left, right;
144 145 146 147
		UnitInfo header, footer;
	} margins;

	PreviewInfo preview;
148 149 150

	GtkWidget *icon_rd;
	GtkWidget *icon_dr;
151 152 153
	GnmExprEntry *area_entry;
	GnmExprEntry *top_entry;
	GnmExprEntry *left_entry;
154 155

	/* The header and footer data. */
156 157
	PrintHF *header;
	PrintHF *footer;
158

159 160 161 162
	/* The header and footer customize dialogs. */
	GtkWidget *customize_header;
	GtkWidget *customize_footer;

163 164 165
	/* The header and footer preview widgets. */
	HFPreviewInfo *pi_header;
	HFPreviewInfo *pi_footer;
166
} PrinterSetupState;
Morten Welinder's avatar
Morten Welinder committed
167

168 169 170
typedef struct {
	PrinterSetupState *state;
	UnitInfo *target;
171 172
} UnitInfo_cbdata;

173
static void fetch_settings (PrinterSetupState *state);
174
static void do_hf_customize (gboolean header, PrinterSetupState *state);
175

176
/**
177 178 179
 * spin_button_set_bound
 * @spin           spinbutton
 * @space_to_grow  how much higher value may go
180
  *
181 182 183 184 185
 * Allow the value in spin button to increase by at most space_to_grow.
 * If space_to_grow is negative, e.g. after paper size change,
 * spin_button_set_bound adjusts the margin and returns a less
 *  negative space_to_grow.
 */
186 187
static void
spin_button_set_bound (UnitInfo *unit, double space_to_grow)
188
{
189
	double value;
190

191 192
	g_return_if_fail (unit != NULL);
	g_return_if_fail (GTK_IS_SPIN_BUTTON (unit->spin));
193

194 195 196
	value = space_to_grow;
	gnome_print_convert_distance (&value, GNOME_PRINT_PS_UNIT, unit->unit);
	gtk_spin_button_set_range (unit->spin, 0, value);
197

198
	return;
199 200
}

201
/**
202
 * get_paper_width
203
 * @state :
204
 *
205
 * Return paper width in points, taking page orientation into account.
206 207
 */
static double
208
get_paper_pswidth (PrinterSetupState *state)
209
{
210 211
	double height;
	double width;
212
	if (gnome_print_config_get_page_size (state->gp_config, &width, &height))
213
		return width;
214
	else
215
		return 1.0;
216 217
}

218
/**
219
 * get_paper_psheight
220
 * @state :
221
 *
222
 * Return paper height in points, taking page orientation into account.
223 224
 */
static double
225
get_paper_psheight (PrinterSetupState *state)
226
{
227 228
	double height;
	double width;
229
	if (gnome_print_config_get_page_size (state->gp_config, &width, &height))
230
		return height;
231
	else
232
		return 0.0;
233 234
}

235
/**
236
 * get_printable_height (in points)
237
 * @state :
238 239 240
 *
 */
static double
241
get_printable_height (PrinterSetupState *state)
242
{
243 244 245
	double top = 0, bottom = 0, left = 0, right = 0, height;
	double header = state->margins.header.value;
	double footer = state->margins.footer.value;
Morten Welinder's avatar
Morten Welinder committed
246

247 248 249 250 251 252 253
	print_info_get_margins   (state->pi, &top, &bottom, &left, &right);
	gnome_print_convert_distance (&header, state->margins.header.unit, GNOME_PRINT_PS_UNIT);
	gnome_print_convert_distance (&footer, state->margins.footer.unit, GNOME_PRINT_PS_UNIT);

	height = get_paper_psheight (state) - top - bottom - header - footer;

	return height;
254 255
}

256
/**
257
 * set_vertical_bounds
258
 * @state :
259 260
 * @unit          unit
 *
261
 * Set the upper bounds for headers and footers.
262
 */
263
static void
264
set_vertical_bounds (PrinterSetupState *state)
265
{
266 267 268 269 270 271 272 273 274 275 276
	double printable_height = get_printable_height (state);
	double header = state->margins.header.value;
	double footer = state->margins.footer.value;

	gnome_print_convert_distance (&header, state->margins.header.unit, GNOME_PRINT_PS_UNIT);
	gnome_print_convert_distance (&footer, state->margins.footer.unit, GNOME_PRINT_PS_UNIT);

	spin_button_set_bound (&state->margins.header,
			       MAX (0, printable_height) + header);
	spin_button_set_bound (&state->margins.footer,
			       MAX (0, printable_height) + footer);
277 278
}

279
static void
280
preview_page_destroy (PrinterSetupState *state)
281
{
282 283 284
	if (state->preview.group) {
		gtk_object_destroy (GTK_OBJECT (state->preview.group));
		state->preview.group = NULL;
285 286 287
	}
}

288
static void
289
move_line (FooCanvasItem *item,
290 291 292
	   double x1, double y1,
	   double x2, double y2)
{
293
	FooCanvasPoints *points;
294

295
	points = foo_canvas_points_new (2);
Morten Welinder's avatar
Morten Welinder committed
296 297 298 299
	points->coords[0] = x1;
	points->coords[1] = y1;
	points->coords[2] = x2;
	points->coords[3] = y2;
300

301
	foo_canvas_item_set (item,
302 303
			       "points", points,
			       NULL);
304
	foo_canvas_points_unref (points);
305 306
}

307 308
static FooCanvasItem *
make_line (FooCanvasGroup *g, double x1, double y1, double x2, double y2)
309
{
310 311
	FooCanvasPoints *points;
	FooCanvasItem *item;
312

313
	points = foo_canvas_points_new (2);
Morten Welinder's avatar
Morten Welinder committed
314 315 316 317
	points->coords[0] = x1;
	points->coords[1] = y1;
	points->coords[2] = x2;
	points->coords[3] = y2;
318

319 320
	item = foo_canvas_item_new (
		FOO_CANVAS_GROUP (g), foo_canvas_line_get_type (),
321
		"points", points,
322 323
		"width-pixels", 1,
		"fill-color",   MARGIN_COLOR_DEFAULT,
324
		NULL);
325
	foo_canvas_points_unref (points);
326 327 328 329 330

	return item;
}

static void
331
draw_margin (UnitInfo *uinfo, PrinterSetupState *state)
332
{
333 334 335
	double x1, y1, x2, y2, value;
	GnomePrintUnit const *gp_unit = gnome_print_unit_selector_get_unit (
		GNOME_PRINT_UNIT_SELECTOR (state->unit_selector));
336 337
	double top = 0, bottom = 0, left = 0, right = 0;
	print_info_get_margins (state->pi, &top, &bottom, &left, &right);
338 339 340 341 342 343 344 345 346

	x1 = uinfo->bound_x1;
	y1 = uinfo->bound_y1;
	x2 = uinfo->bound_x2;
	y2 = uinfo->bound_y2;

	switch (uinfo->orientation)
	{
	case MARGIN_LEFT:
347
		x1 += uinfo->pi->scale * left;
348 349 350 351 352 353
		if (x1 < x2)
			x2 = x1;
		else
			x1 = x2;
		break;
	case MARGIN_RIGHT:
354
		x2 -= uinfo->pi->scale * right;
355 356 357 358 359 360
		if (x2 < x1)
			x2 = x1;
		else
			x1 = x2;
		break;
	case MARGIN_TOP:
361
		y1 += uinfo->pi->scale * top;
362 363 364 365 366 367
		if (y1 < y2)
			y2 = y1;
		else
			y1 = y2;
		break;
	case MARGIN_BOTTOM:
368
		y2 -= uinfo->pi->scale * bottom;
369 370 371 372 373 374
		if (y2 < y1)
			y2 = y1;
		else
			y1 = y2;
		break;
	case MARGIN_HEADER:
375
		value = uinfo->value;
Morten Welinder's avatar
Morten Welinder committed
376
		gnome_print_convert_distance (&value, gp_unit, GNOME_PRINT_PS_UNIT);
377
		y1 += (uinfo->pi->scale * top + uinfo->pi->scale * value);
378 379
		y2 = y1;
		break;
380
	case MARGIN_FOOTER:
381
		value = uinfo->value;
Morten Welinder's avatar
Morten Welinder committed
382
		gnome_print_convert_distance (&value, gp_unit, GNOME_PRINT_PS_UNIT);
383
		y2 -= (uinfo->pi->scale * bottom + uinfo->pi->scale * value);
384
		y1 = y2;
385
		break;
386 387
	default:
		return;
388 389 390 391 392 393
	}

	move_line (uinfo->line, x1, y1, x2, y2);
}

static void
394
create_margin (PrinterSetupState *state,
395 396 397 398 399
	       UnitInfo *uinfo,
	       MarginOrientation orientation,
	       double x1, double y1,
	       double x2, double y2)
{
400
	FooCanvasGroup *g = FOO_CANVAS_GROUP (state->preview.group);
401

402
	uinfo->pi = &state->preview;
403 404 405 406 407 408
	uinfo->line = make_line (g, x1 + 8, y1, x1 + 8, y2);
	uinfo->orientation = orientation;
	uinfo->bound_x1 = x1;
	uinfo->bound_y1 = y1;
	uinfo->bound_x2 = x2;
	uinfo->bound_y2 = y2;
409

410
	draw_margin (uinfo, state);
411 412 413
}

static void
414
draw_margins (PrinterSetupState *state, double x1, double y1, double x2, double y2)
415 416
{
	/* Margins */
417
	create_margin (state, &state->margins.left, MARGIN_LEFT,
418
		       x1, y1, x2, y2);
419
	create_margin (state, &state->margins.right, MARGIN_RIGHT,
420
		       x1, y1, x2, y2);
421
	create_margin (state, &state->margins.top, MARGIN_TOP,
422
		       x1, y1, x2, y2);
423
	create_margin (state, &state->margins.bottom, MARGIN_BOTTOM,
424
		       x1, y1, x2, y2);
425 426 427 428 429 430

	/* Headers & footers */
	create_margin (state, &state->margins.header, MARGIN_HEADER,
		       x1, y1, x2, y2);
	create_margin (state, &state->margins.footer, MARGIN_FOOTER,
		       x1, y1, x2, y2);
431 432
}

433
static void
434
preview_page_create (PrinterSetupState *state)
435 436 437
{
	double x1, y1, x2, y2;
	double width, height;
438
	PreviewInfo *pi = &state->preview;
439

440 441
	width  = get_paper_pswidth  (state);
	height = get_paper_psheight (state);
442

443
	if (width < height)
444 445 446 447
		pi->scale = PAGE_Y / height;
	else
		pi->scale = PAGE_X / width;

448
	pi->offset_x = (PREVIEW_X - (width  * pi->scale)) / 2;
449
	pi->offset_y = (PREVIEW_Y - (height * pi->scale)) / 2;
450
/*	pi->offset_x = pi->offset_y = 0; */
451 452 453 454 455
	x1 = pi->offset_x + 0 * pi->scale;
	y1 = pi->offset_y + 0 * pi->scale;
	x2 = pi->offset_x + width * pi->scale;
	y2 = pi->offset_y + height * pi->scale;

456 457 458
	pi->group = foo_canvas_item_new (
		foo_canvas_root (FOO_CANVAS (pi->canvas)),
		foo_canvas_group_get_type (),
459 460 461
		"x", 0.0,
		"y", 0.0,
		NULL);
462

463 464
	foo_canvas_item_new (FOO_CANVAS_GROUP (pi->group),
		FOO_TYPE_CANVAS_RECT,
465 466 467 468
		"x1",  	      	 (double) x1+2,
		"y1",  	      	 (double) y1+2,
		"x2",  	      	 (double) x2+2,
		"y2",         	 (double) y2+2,
469 470 471
		"fill-color",    "black",
		"outline-color", "black",
		"width-pixels",   1,
472
		NULL);
473

474 475
	foo_canvas_item_new (FOO_CANVAS_GROUP (pi->group),
		FOO_TYPE_CANVAS_RECT,
476 477 478 479
		"x1",  	      	 (double) x1,
		"y1",  	      	 (double) y1,
		"x2",  	      	 (double) x2,
		"y2",         	 (double) y2,
480 481 482
		"fill-color",    "white",
		"outline-color", "black",
		"width-pixels",   1,
483
		NULL);
484

485
	draw_margins (state, x1, y1, x2, y2);
486 487
}

488
/**
489 490 491 492 493 494 495 496 497 498 499 500 501 502 503
 * spin_button_adapt_to_unit
 * @spin      spinbutton
 * @new_unit  new unit
 *
 * Select suitable increments and number of digits for the unit.
 *
 * The increments are not scaled linearly. We assume that if you are using a
 * fine grained unit - pts or mm - you want to fine tune the margin. For cm
 * and in, a useful coarse increment is used, which is a round number in that
 * unit.
 *
 * NOTE: According to docs, we should be using gtk_spin_button_configure
 * here. But as of gtk+ 1.2.7, climb_rate has no effect for that call.
 */
static void
504
spin_button_adapt_to_unit (GtkSpinButton *spin, const GnomePrintUnit *new_unit)
505 506 507 508 509 510 511 512 513
{
	GtkAdjustment *adjustment;
	gfloat step_increment;
	guint digits;

	g_return_if_fail (GTK_IS_SPIN_BUTTON (spin));
	adjustment = gtk_spin_button_get_adjustment (spin);
	g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));

514
	if (new_unit->unittobase <= 3) {
515
		/* "mm" and "pt" */
516 517
		step_increment = 1.0;
		digits = 1;
518 519
	} else if (new_unit->unittobase <= 30) {
		/* "cm" */
520 521
		step_increment = 0.5;
		digits = 2;
522
	} else {
523 524
		step_increment = 0.25;
		digits = 2;
525
	}
526

527 528 529 530
	adjustment->step_increment = step_increment;
	adjustment->page_increment = step_increment * 10;
	gtk_adjustment_changed (adjustment);
	gtk_spin_button_set_digits (spin, digits);
531 532
}

533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565
static void
canvas_update (PrinterSetupState *state)
{
	guchar *unit_txt;

	preview_page_destroy (state);
	preview_page_create (state);
	set_vertical_bounds (state);

	unit_txt = gnome_print_config_get (state->gp_config, GNOME_PRINT_KEY_PREFERED_UNIT);
	if (unit_txt) {
		GnomePrintUnitSelector *sel =
			GNOME_PRINT_UNIT_SELECTOR (state->unit_selector);
		const GnomePrintUnit *unit =
			gnome_print_unit_get_by_abbreviation (unit_txt);

		g_free (unit_txt);
		gnome_print_unit_selector_set_unit (sel, unit);
		spin_button_adapt_to_unit (state->margins.header.spin, unit);
		spin_button_adapt_to_unit (state->margins.footer.spin, unit);
	}
}

static void
notebook_flipped (G_GNUC_UNUSED GtkNotebook *notebook,
		  G_GNUC_UNUSED GtkNotebookPage *page,
		  gint page_num,
		  PrinterSetupState *state)
{
	if (page_num == HF_PAGE)
		canvas_update (state);
}

566
static void
567
cb_unit_changed (UnitInfo_cbdata *data)
568
{
569 570 571 572
	data->target->value = gtk_adjustment_get_value (data->target->adj);
	data->target->unit = gnome_print_unit_selector_get_unit (
		GNOME_PRINT_UNIT_SELECTOR (data->state->unit_selector));
	set_vertical_bounds (data->state);
573
	/* Adjust the display to the current values */
574
	draw_margin (data->target, data->state);
575 576
}

577
static gboolean
578
cb_unit_activated (UnitInfo_cbdata *data)
579
{
580
	foo_canvas_item_set (data->target->line,
581 582
			     "fill-color", MARGIN_COLOR_ACTIVE,
			     NULL);
583
	return FALSE;
584 585
}

586
static gboolean
587
cb_unit_deactivated (UnitInfo_cbdata *data)
588
{
589
	foo_canvas_item_set (data->target->line,
590 591
			     "fill-color", MARGIN_COLOR_DEFAULT,
			     NULL);
592
	return FALSE;
593 594
}

595
static void
596
unit_editor_configure (UnitInfo *target, PrinterSetupState *state,
597
		       const char *spin_name,
598
		       double init_points)
599
{
600
	GtkSpinButton *spin;
601
	UnitInfo_cbdata *cbdata;
602
	int len;
603

604
	spin = GTK_SPIN_BUTTON (glade_xml_get_widget (state->gui, spin_name));
605

606 607
	target->value = init_points;
	target->unit = GNOME_PRINT_PS_UNIT                               ;
608 609 610

	target->adj = GTK_ADJUSTMENT (gtk_adjustment_new (
		target->value,
611
		0.0, 100000.0, 0.5, 1.0, 0));
612 613
	target->spin = spin;
	gtk_spin_button_configure (spin, target->adj, 1, 1);
614
	gtk_widget_ensure_style (GTK_WIDGET (spin));
Jody Goldberg's avatar
Jody Goldberg committed
615
	len = go_pango_measure_string (
616
		gtk_widget_get_pango_context (gtk_widget_get_toplevel (GTK_WIDGET (spin))),
617 618 619
		GTK_WIDGET (spin)->style->font_desc,
		"123.45XXX");
	gtk_widget_set_size_request (GTK_WIDGET (spin), len, -1);
620
	gnumeric_editable_enters (GTK_WINDOW (state->dialog),
621
				  GTK_WIDGET (spin));
622 623

	cbdata = g_new (UnitInfo_cbdata, 1);
624
	cbdata->state = state;
625
	cbdata->target = target;
626
	g_signal_connect_swapped (G_OBJECT (target->spin),
627
		"focus_in_event",
628 629
		G_CALLBACK (cb_unit_activated), cbdata);
	g_signal_connect_swapped (G_OBJECT (target->spin),
630
		"focus_out_event",
631
		G_CALLBACK (cb_unit_deactivated), cbdata);
632 633
	gnome_print_unit_selector_add_adjustment (GNOME_PRINT_UNIT_SELECTOR (state->unit_selector),
						  target->adj);
634
	g_signal_connect_swapped (G_OBJECT (target->spin),
635
		"value_changed",
636
		G_CALLBACK (cb_unit_changed), cbdata);
Jody Goldberg's avatar
Jody Goldberg committed
637 638 639
	g_object_set_data_full (G_OBJECT (target->spin),
		"cbdata", cbdata, g_free); /* avoid a leak */

640 641
}

642 643 644 645
static void
cb_unit_selector_changed (GnomePrintUnitSelector *sel, PrinterSetupState *state)
{
	const GnomePrintUnit *unit;
Morten Welinder's avatar
Morten Welinder committed
646

647 648 649
	g_return_if_fail (state != NULL);

	unit = gnome_print_unit_selector_get_unit (sel);
650
	if (unit) {
651
		gnome_print_config_set (state->gp_config, GNOME_PRINT_KEY_PREFERED_UNIT,
652
					unit->abbr);
653 654
		spin_button_adapt_to_unit (state->margins.header.spin, unit);
		spin_button_adapt_to_unit (state->margins.footer.spin, unit);
655
	}
656 657 658
}


659
/**
660 661
 * Each margin is stored with a unit. We use the top margin unit for display
 * and ignore desired display for the others.
662 663 664 665 666
 *
 * Header and footer are stored with Excel semantics, but displayed with
 * more natural semantics. In Excel, both top margin and header are measured
 * from top of sheet. The Gnumeric user interface presents header as the
 * band between top margin and the print area. Bottom margin and footer are
667
 * handled likewise. See illustration at top of src/print.c
668
 */
669
static void
670
do_setup_margin (PrinterSetupState *state)
671
{
672
	GtkWidget *table;
673
	GtkBox *container;
674 675
	PrintMargins const *pm;
	GnomePrintUnit const *displayed_unit;
676
	double header = 0, footer = 0, left = 0, right = 0;
677

678
	g_return_if_fail (state && state->pi);
679

680 681
	print_info_get_margins   (state->pi, &header, &footer, &left, &right);

682
	pm = &state->pi->margin;
683
	displayed_unit = pm->top.desired_display;
684

685 686 687
	state->preview.canvas = foo_canvas_new ();
	foo_canvas_set_scroll_region (
		FOO_CANVAS (state->preview.canvas),
688
		0.0, 0.0, PREVIEW_X, PREVIEW_Y);
689
	gtk_widget_set_size_request (state->preview.canvas, PREVIEW_X, PREVIEW_Y);
690
	gtk_widget_show (state->preview.canvas);
691

692 693
	table = glade_xml_get_widget (state->gui, "margin-table");
	state->unit_selector = gnome_print_unit_selector_new (GNOME_PRINT_UNIT_ABSOLUTE);
694
	gtk_table_attach (GTK_TABLE (table), state->unit_selector, 1, 2, 4, 5,
695 696 697 698
			  GTK_FILL, GTK_FILL | GTK_SHRINK, 0, 0);
	g_signal_connect (G_OBJECT (state->unit_selector), "modified",
			  G_CALLBACK (cb_unit_selector_changed), state);
	gtk_widget_show (state->unit_selector);
699

Jody Goldberg's avatar
Jody Goldberg committed
700
	unit_editor_configure (&state->margins.header, state, "spin-header",
701
			       MAX (pm->top.points - header, 0.0));
Jody Goldberg's avatar
Jody Goldberg committed
702
	unit_editor_configure (&state->margins.footer, state, "spin-footer",
703
			       MAX (pm->bottom.points - footer, 0.0));
704

705
	container = GTK_BOX (glade_xml_get_widget (state->gui,
706
						   "container-margin-page"));
707
	gtk_box_pack_start (container, state->preview.canvas, TRUE, TRUE, 0);
708

709
	if (state->pi->center_vertically)
710 711
		gtk_toggle_button_set_active (
			GTK_TOGGLE_BUTTON (
712
				glade_xml_get_widget (state->gui, "center-vertical")),
713 714
			TRUE);

715
	if (state->pi->center_horizontally)
716 717
		gtk_toggle_button_set_active (
			GTK_TOGGLE_BUTTON (
718
				glade_xml_get_widget (state->gui, "center-horizontal")),
719 720 721
			TRUE);
}

722 723
/* Display the header or footer preview in the print setup dialog.
 * Use the canvas widget in the HFPreviewInfo struct.
724
 *
725 726
 */
static void
727
display_hf_preview (PrinterSetupState *state, gboolean header)
728
{
729
	gchar *text = NULL;
730 731
	PrintHF *sample = NULL;
	HFRenderInfo *hfi;
732
	HFPreviewInfo *pi;
733

Morten Welinder's avatar
Morten Welinder committed
734
	g_return_if_fail (state != NULL);
735

736 737 738
	hfi = hf_render_info_new ();

	hfi->page = 1;
739 740
	hfi->pages = 99;
	hfi->sheet = state->sheet;
741 742

	if (header) {
743
		pi = state->pi_header;
744 745
		sample = state->header;
	} else {
746
		pi = state->pi_footer;
747 748 749
		sample = state->footer;
	}

750
	text = hf_format_render (sample->left_format, hfi, HF_RENDER_PRINT);
751
	foo_canvas_item_set (pi->left, "text", text ? text : "", NULL);
Morten Welinder's avatar
Morten Welinder committed
752
	g_free (text);
753 754

	text = hf_format_render (sample->middle_format, hfi, HF_RENDER_PRINT);
755
	foo_canvas_item_set (pi->middle, "text", text ? text : "", NULL);
Morten Welinder's avatar
Morten Welinder committed
756
	g_free (text);
757 758

	text  = hf_format_render (sample->right_format, hfi, HF_RENDER_PRINT);
759
	foo_canvas_item_set (pi->right, "text", text ? text : "", NULL);
Morten Welinder's avatar
Morten Welinder committed
760
	g_free (text);
761 762 763 764

	hf_render_info_destroy (hfi);
}

765
static void
766
do_header_customize (PrinterSetupState *state)
767
{
768
	do_hf_customize (TRUE, state);
769 770 771
}

static void
772
do_footer_customize (PrinterSetupState *state)
773
{
774
	do_hf_customize (FALSE, state);
775 776
}

777
static void
778
header_changed (GtkComboBox *menu, PrinterSetupState *state)
779
{
780 781 782 783 784 785 786 787 788 789 790 791
	GList *selection = g_list_nth (hf_formats,
		gtk_combo_box_get_active (menu));
	PrintHF *format = (selection)? selection->data: NULL;

	if (format == NULL) {
		do_header_customize (state);
	} else {
		print_hf_free (state->header);
		state->header = print_hf_copy (format);
	}
	
		display_hf_preview (state, TRUE);
792 793 794
}

static void
795
footer_changed (GtkComboBox *menu, PrinterSetupState *state)
796
{
797 798 799 800 801 802 803 804 805 806 807 808
	GList *selection = g_list_nth (hf_formats,
		gtk_combo_box_get_active (menu));
	PrintHF *format = (selection)? selection->data: NULL;

	if (format == NULL) {
		do_footer_customize (state);
	} else {
		print_hf_free (state->footer);
		state->footer = print_hf_copy (format);
	}

	display_hf_preview (state, FALSE);
809 810
}

811
/*
812 813 814 815
 * Fills one of the GtkCombos for headers or footers with the list
 * of existing header/footer formats
 */
static void
816
fill_hf (PrinterSetupState *state, GtkComboBox *om, GCallback callback, gboolean header)
817 818 819
{
	GList *l;
	HFRenderInfo *hfi;
820 821
	GtkListStore *store;
	GtkTreeIter iter;
822 823
	char *res;
	PrintHF *select = header ? state->header : state->footer;
824
	int i, idx = 0;
825

826 827 828 829
	hfi = hf_render_info_new ();
	hfi->page = 1;
	hfi->pages = 1;

830 831
	store = gtk_list_store_new (1, G_TYPE_STRING);
	gtk_combo_box_set_model (om, GTK_TREE_MODEL (store));
832

833
	for (i = 0, l = hf_formats; l; l = l->next, i++) {
834 835
		PrintHF *format = l->data;
		char *left, *middle, *right;
836

837
		if (print_hf_same (format, select))
838
			idx = i;
839

840
		left   = hf_format_render (format->left_format, hfi, HF_RENDER_PRINT);
841
		middle = hf_format_render (format->middle_format, hfi, HF_RENDER_PRINT);
842
		right  = hf_format_render (format->right_format, hfi, HF_RENDER_PRINT);
843 844 845

		res = g_strdup_printf (
			"%s%s%s%s%s",
846 847 848
			left, (*left && (*middle || *right)) ? ", " : "",
			middle, (*middle && *right) ? ", " : "",
			right);
849

850 851 852 853
		gtk_list_store_append (store, &iter);
		gtk_list_store_set (store, &iter,
					0, res,
					-1);
854

855 856 857 858 859
		g_free (res);
		g_free (left);
		g_free (middle);
		g_free (right);
	}
860 861

	/* Add menu option to customize the header/footer. */
862
	if (header)
Morten Welinder's avatar
Morten Welinder committed
863
		res = g_strdup_printf (_("Customize header"));
864
	else
Morten Welinder's avatar
Morten Welinder committed
865
		res = g_strdup_printf (_("Customize footer"));
866 867 868 869
	gtk_list_store_append (store, &iter);
	gtk_list_store_set (store, &iter,
				0, res,
				-1);
870 871
	g_free (res);

872 873
	gtk_combo_box_set_active (om, idx);
	g_signal_connect (G_OBJECT (om), "changed", callback, state);
874

875 876 877
	hf_render_info_destroy (hfi);
}

878 879 880
static void
do_setup_hf_menus (PrinterSetupState *state)
{
881 882
	GtkComboBox *header;
	GtkComboBox *footer;
883

Morten Welinder's avatar
Morten Welinder committed
884
	g_return_if_fail (state != NULL);
885

886 887
	header = GTK_COMBO_BOX (glade_xml_get_widget (state->gui, "option-menu-header"));
	footer = GTK_COMBO_BOX (glade_xml_get_widget (state->gui, "option-menu-footer"));
888 889

	if (state->header)
890
		fill_hf (state, header, G_CALLBACK (header_changed), TRUE);
891
	if (state->footer)
892
		fill_hf (state, footer, G_CALLBACK (footer_changed), FALSE);
893 894
}

895
static char *
896
text_get (GtkEditable *text_widget)
897
{
898
	return gtk_editable_get_chars (GTK_EDITABLE (text_widget), 0, -1);
899 900
}

901
static void
902
hf_customize_apply (GtkWidget *dialog)
903 904 905 906 907 908
{
	GladeXML *gui;
	GtkEntry *left, *middle, *right;
	char *left_format, *right_format, *middle_format;
	PrintHF **config = NULL;
	gboolean header;
909
	PrinterSetupState *state;
910

911
	g_return_if_fail (dialog != NULL);
912

913 914 915 916 917 918 919 920 921 922 923 924 925
	gui = glade_get_widget_tree (GTK_WIDGET (dialog));

        if (gui == NULL)
                return;

	left   = GTK_ENTRY (glade_xml_get_widget (gui, "left-format"));
	middle = GTK_ENTRY (glade_xml_get_widget (gui, "middle-format"));
	right  = GTK_ENTRY (glade_xml_get_widget (gui, "right-format"));

	left_format   = text_get (GTK_EDITABLE (left));
	middle_format = text_get (GTK_EDITABLE (middle));
	right_format  = text_get (GTK_EDITABLE (right));

926 927
	header = GPOINTER_TO_INT (g_object_get_data (G_OBJECT (dialog), "header"));
	state = g_object_get_data (G_OBJECT (dialog), "state");
928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943

	if (header)
		config = &state->header;
	else
		config = &state->footer;

	print_hf_free (*config);
	*config = print_hf_new (left_format, middle_format, right_format);

	g_free (left_format);
	g_free (middle_format);
	g_free (right_format);

	print_hf_register (*config);

	do_setup_hf_menus (state);
944
	display_hf_preview (state, header);
945 946 947

	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "apply_button"), FALSE);
	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "ok_button"), FALSE);
948 949
}

950 951 952 953 954 955 956 957

static void
hf_customize_ok (GtkWidget *dialog)
{
	hf_customize_apply (dialog);
	gtk_widget_destroy (dialog);
}

958
static gboolean
959
cb_hf_changed (GladeXML *gui)
960
{
961
	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "apply_button"), TRUE);
962 963
	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "ok_button"), TRUE);
	return FALSE;
964 965 966
}

/*
967
 * Open up a DIALOG to allow the user to customize the header
968 969 970 971
 * or the footer.
 */
static void
do_hf_customize (gboolean header, PrinterSetupState *state)
972
{
973
	GladeXML *gui;
974
	GtkEntry *left, *middle, *right;
975
	GtkWidget *dialog;
976 977
	PrintHF **config = NULL;

978 979 980 981 982 983 984 985 986 987 988 989
	/* Check if this dialog isn't already created. */
	if (header)
		dialog = state->customize_header;
	else
		dialog = state->customize_footer;

	if (dialog != NULL) {
		gdk_window_show (dialog->window);
		gdk_window_raise (dialog->window);
		return;
	}

990
	gui = gnm_glade_xml_new (GO_CMD_CONTEXT (state->wbcg),
991
		"hf-config.glade", NULL, NULL);
992
        if (gui == NULL)
993 994
                return;

995
	left   = GTK_ENTRY (glade_xml_get_widget (gui, "left-format"));
996
	middle = GTK_ENTRY (glade_xml_get_widget (gui, "middle-format"));
997
	right  = GTK_ENTRY (glade_xml_get_widget (gui, "right-format"));
998
	dialog = glade_xml_get_widget (gui, "hf-config");
999

1000
	if (header) {
1001 1002
		config = &state->header;
		state->customize_header = dialog;
1003
		gtk_window_set_title (GTK_WINDOW (dialog), _("Custom header configuration"));
1004

1005
	} else {
1006 1007
		config = &state->footer;
		state->customize_footer = dialog;
1008 1009 1010
		gtk_window_set_title (GTK_WINDOW (dialog), _("Custom footer configuration"));
	}

1011 1012 1013 1014
	gtk_entry_set_text (left, (*config)->left_format);
	gtk_entry_set_text (middle, (*config)->middle_format);
	gtk_entry_set_text (right, (*config)->right_format);

1015 1016 1017
	gnumeric_editable_enters (GTK_WINDOW (dialog), GTK_WIDGET (left));
	gnumeric_editable_enters (GTK_WINDOW (dialog), GTK_WIDGET (middle));
	gnumeric_editable_enters (GTK_WINDOW (dialog), GTK_WIDGET (right));
1018

1019 1020 1021 1022 1023 1024
	g_signal_connect_swapped (G_OBJECT (glade_xml_get_widget (gui, "apply_button")),
		"clicked", G_CALLBACK (hf_customize_apply), dialog);
	g_signal_connect_swapped (G_OBJECT (glade_xml_get_widget (gui, "ok_button")),
		"clicked", G_CALLBACK (hf_customize_ok), dialog);
	g_signal_connect_swapped (G_OBJECT (glade_xml_get_widget (gui, "cancel_button")),
		"clicked", G_CALLBACK (gtk_widget_destroy), dialog);
1025
	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "apply_button"), FALSE);
Morten Welinder's avatar
Morten Welinder committed
1026
	gtk_widget_set_sensitive (glade_xml_get_widget (gui, "ok_button"), FALSE);
1027

1028
	if (header)
Morten Welinder's avatar
Morten Welinder committed
1029
		g_signal_connect (G_OBJECT (dialog), "destroy",
1030
				  G_CALLBACK (gtk_widget_destroyed), &state->customize_header);
1031
	else
Morten Welinder's avatar
Morten Welinder committed
1032
		g_signal_connect (G_OBJECT (dialog), "destroy",
1033
				  G_CALLBACK (gtk_widget_destroyed), &state->customize_footer);
1034 1035


1036
	/* Remember whether it is customizing header or footer. */
1037 1038
	g_object_set_data (G_OBJECT (dialog), "header", GINT_TO_POINTER (header));
	g_object_set_data (G_OBJECT (dialog), "state", state);
1039

1040
	/* Setup bindings to mark when the entries are modified. */
1041 1042 1043 1044 1045 1046
	g_signal_connect_swapped (G_OBJECT (left),
		"changed", G_CALLBACK (cb_hf_changed), gui);
	g_signal_connect_swapped (G_OBJECT (middle),
		"changed", G_CALLBACK (cb_hf_changed), gui);
	g_signal_connect_swapped (G_OBJECT (right),
		"changed", G_CALLBACK (cb_hf_changed), gui);
1047 1048


1049 1050 1051
	gnumeric_init_help_button (glade_xml_get_widget (gui, "help_button"),
		header  ? GNUMERIC_HELP_LINK_PRINTER_SETUP_HEADER_CUSTOMIZATION
			: GNUMERIC_HELP_LINK_PRINTER_SETUP_FOOTER_CUSTOMIZATION);
Morten Welinder's avatar
Morten Welinder committed
1052

1053
	/* Let them begin typing into the first entry widget. */
Morten Welinder's avatar
Morten Welinder committed
1054
	gtk_widget_grab_focus (GTK_WIDGET (left));
1055

1056 1057
	gtk_window_set_transient_for (GTK_WINDOW (dialog), GTK_WINDOW (state->dialog));
	gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER_ON_PARENT);
1058

1059
	gtk_widget_show (dialog);
1060
}
1061

1062 1063 1064 1065 1066 1067
/* header/footer_preview_event
 * If the user double clicks on a header/footer preview canvas, we will
 * open up the dialog to modify the header or footer.
 * They can also do this from the option menu.
 */
static gboolean
1068
header_preview_event (G_GNUC_UNUSED FooCanvas *canvas,
1069
		      GdkEvent *event, PrinterSetupState *state)
1070
{
1071
	if (event == NULL ||
1072 1073
	    event->type != GDK_2BUTTON_PRESS ||
	    event->button.button != 1)
1074 1075 1076 1077
		return FALSE;
	do_hf_customize (TRUE, state);
	return TRUE;
}
1078

1079
static gboolean
1080
footer_preview_event (G_GNUC_UNUSED FooCanvas *canvas,
1081
		      GdkEvent *event, PrinterSetupState *state)
1082 1083
{
	if (event == NULL ||
1084 1085
	    event->type != GDK_2BUTTON_PRESS ||
	    event->button.button != 1)
1086 1087 1088
		return FALSE;
	do_hf_customize (FALSE, state);
	return TRUE;
1089 1090
}

1091
/* create_hf_preview_canvas
1092 1093 1094
 * Creates the canvas to do a header or footer preview in the print setup.
 *
 */
1095
static void
Morten Welinder's avatar
Morten Welinder committed
1096
create_hf_preview_canvas (PrinterSetupState *state, gboolean header)
1097
{
1098 1099
	GtkWidget *wid;
	HFPreviewInfo *pi;
1100
	PangoFontDescription *font_desc;
1101 1102 1103 1104 1105 1106 1107
	gdouble width = HF_PREVIEW_X;
	gdouble height = HF_PREVIEW_Y;
	gdouble shadow = HF_PREVIEW_SHADOW;
	gdouble padding = HF_PREVIEW_PADDING;
	gdouble margin = HF_PREVIEW_MARGIN;
	gdouble bottom_margin = height - margin;

Morten Welinder's avatar
Morten Welinder committed
1108
	pi = g_new (HFPreviewInfo, 1);
1109 1110