summaryrefslogtreecommitdiff
path: root/src/eom-print-preview.c
blob: f0b85bf990e2715f17fbced9fb393d56a0924ec5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
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
81
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
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
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
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
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
/* Eye Of MATE -- Print Preview Widget
 *
 * Copyright (C) 2006-2008 The Free Software Foundation
 *
 * Author: Claudio Saavedra <csaavedra@gnome.org>
 *
 * 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <gtk/gtk.h>
#include <cairo.h>
#include <gdk/gdkkeysyms.h>

#include "eom-image.h"
#include "eom-print-preview.h"

struct _EomPrintPreviewPrivate {
	GtkWidget *area;
	GdkPixbuf *image;
	GdkPixbuf *image_scaled;

	/* The surface to set to the cairo context, created from the image */
	cairo_surface_t *surface;

        /* Flag whether we have to create surface */
	gboolean flag_create_surface;

	/* the alignment of the image in the page */
	gfloat image_x_align, image_y_align;

	/* real paper size, in inches */
	gfloat p_width, p_height;

	/* page margins, in inches */
	gfloat l_margin, r_margin, t_margin, b_margin;

	/* page margins, relatives to the widget size */
	gint l_rmargin, r_rmargin, t_rmargin, b_rmargin;

	/* image width, relative to the widget size */
	gint r_width, r_height;

	/* scale of the image, as defined by the user */
	gfloat i_scale;

	/* scale of the page, relative to the widget size */
	gfloat p_scale;

	/* whether we are currently grabbing the image */
	gboolean grabbed;

	/* the last cursor position */
	gdouble cursorx, cursory;

	/* if we reject to move the image,
	   store the delta here */
	gdouble r_dx, r_dy;
};

/* Signal IDs */
enum {
	SIGNAL_IMAGE_MOVED,
	SIGNAL_LAST
};

static guint preview_signals [SIGNAL_LAST] = { 0 };

enum {
	PROP_0,
	PROP_IMAGE,
	PROP_IMAGE_X_ALIGN,
	PROP_IMAGE_Y_ALIGN,
	PROP_IMAGE_SCALE,
	PROP_PAPER_WIDTH,
	PROP_PAPER_HEIGHT,
	PROP_PAGE_LEFT_MARGIN,
	PROP_PAGE_RIGHT_MARGIN,
	PROP_PAGE_TOP_MARGIN,
	PROP_PAGE_BOTTOM_MARGIN
};

G_DEFINE_TYPE_WITH_PRIVATE (EomPrintPreview, eom_print_preview, GTK_TYPE_ASPECT_FRAME)

static void eom_print_preview_draw (EomPrintPreview *preview, cairo_t *cr);
static void eom_print_preview_finalize (GObject *object);
static void update_relative_sizes (EomPrintPreview *preview);
static void create_surface (EomPrintPreview *preview);
static void create_image_scaled (EomPrintPreview *preview);
static gboolean create_surface_when_idle (EomPrintPreview *preview);

static void
eom_print_preview_get_property (GObject    *object,
				guint       prop_id,
				GValue     *value,
				GParamSpec *pspec)
{
	EomPrintPreviewPrivate *priv = EOM_PRINT_PREVIEW (object)->priv;

	switch (prop_id) {
	case PROP_IMAGE:
		g_value_set_object (value, priv->image);
		break;
	case PROP_IMAGE_X_ALIGN:
		g_value_set_float (value, priv->image_x_align);
		break;
	case PROP_IMAGE_Y_ALIGN:
		g_value_set_float (value, priv->image_y_align);
		break;
	case PROP_IMAGE_SCALE:
		g_value_set_float (value, priv->i_scale);
		break;
	case PROP_PAPER_WIDTH:
		g_value_set_float (value, priv->p_width);
		break;
	case PROP_PAPER_HEIGHT:
		g_value_set_float (value, priv->p_height);
		break;
	case PROP_PAGE_LEFT_MARGIN:
		g_value_set_float (value, priv->l_margin);
		break;
	case PROP_PAGE_RIGHT_MARGIN:
		g_value_set_float (value, priv->r_margin);
		break;
	case PROP_PAGE_TOP_MARGIN:
		g_value_set_float (value, priv->t_margin);
		break;
	case PROP_PAGE_BOTTOM_MARGIN:
		g_value_set_float (value, priv->b_margin);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}
}

static void
eom_print_preview_set_property (GObject      *object,
				guint         prop_id,
				const GValue *value,
				GParamSpec   *pspec)
{
	EomPrintPreviewPrivate *priv = EOM_PRINT_PREVIEW (object)->priv;
	gboolean paper_size_changed = FALSE;

	switch (prop_id) {
	case PROP_IMAGE:
		if (priv->image) {
			g_object_unref (priv->image);
		}
		priv->image = GDK_PIXBUF (g_value_dup_object (value));

		if (priv->image_scaled) {
			g_object_unref (priv->image_scaled);
			priv->image_scaled = NULL;
		}

		priv->flag_create_surface = TRUE;
		break;
	case PROP_IMAGE_X_ALIGN:
		priv->image_x_align = g_value_get_float (value);
		break;
	case PROP_IMAGE_Y_ALIGN:
		priv->image_y_align = g_value_get_float (value);
		break;
	case PROP_IMAGE_SCALE:
		priv->i_scale = g_value_get_float (value);
		priv->flag_create_surface = TRUE;
		break;
	case PROP_PAPER_WIDTH:
		priv->p_width = g_value_get_float (value);
		paper_size_changed = TRUE;
		break;
	case PROP_PAPER_HEIGHT:
		priv->p_height = g_value_get_float (value);
		paper_size_changed = TRUE;
		break;
	case PROP_PAGE_LEFT_MARGIN:
		priv->l_margin = g_value_get_float (value);
		break;
	case PROP_PAGE_RIGHT_MARGIN:
		priv->r_margin = g_value_get_float (value);
		break;
	case PROP_PAGE_TOP_MARGIN:
		priv->t_margin = g_value_get_float (value);
		break;
	case PROP_PAGE_BOTTOM_MARGIN:
		priv->b_margin = g_value_get_float (value);
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
	}

	if (paper_size_changed) {
		g_object_set (object,
			      "ratio", priv->p_width/priv->p_height,
			      NULL);
	}

	update_relative_sizes (EOM_PRINT_PREVIEW (object));
	gtk_widget_queue_draw (priv->area);
}

static void
eom_print_preview_class_init (EomPrintPreviewClass *klass)
{
	GObjectClass *gobject_class;

	gobject_class = (GObjectClass*) klass;

	gobject_class->get_property = eom_print_preview_get_property;
	gobject_class->set_property = eom_print_preview_set_property;
	gobject_class->finalize     = eom_print_preview_finalize;

/**
 * EomPrintPreview:image:
 *
 * The "image" property defines the image that is previewed
 * in the widget.
 */
	g_object_class_install_property (gobject_class,
					 PROP_IMAGE,
					 g_param_spec_object ("image",
							      "Image to show in the preview",
							      "",
							      G_TYPE_OBJECT,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:image-x-align:
 *
 * The "image-x-align" property defines the horizontal alignment
 * of the image in the widget.
 */
	g_object_class_install_property (gobject_class,
					 PROP_IMAGE_X_ALIGN,
					 g_param_spec_float ("image-x-align",
							      "Horizontal alignment for the image",
							      "",
							      0,
							      1,
							      0.5,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:image-y-align:
 *
 * The "image-y-align" property defines the horizontal alignment
 * of the image in the widget.
 */
	g_object_class_install_property (gobject_class,
					 PROP_IMAGE_Y_ALIGN,
					 g_param_spec_float ("image-y-align",
							      "Vertical alignment for the image",
							      "",
							      0,
							      1,
							      0.5,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:image-scale:
 *
 * The "image-scale" property defines the scaling of the image
 * that the user wants for the printing.
 */
	g_object_class_install_property (gobject_class,
					 PROP_IMAGE_SCALE,
					 g_param_spec_float ("image-scale",
							     "The scale for the image",
							      "",
							      0,
							      1,
							      1,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:paper-width:
 *
 * The width of the previewed paper, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAPER_WIDTH,
					 g_param_spec_float ("paper-width",
							     "Real paper width in inches",
							     "",
							     0,
							     100,
							     8.5,
							     G_PARAM_READWRITE));

/**
 * EomPrintPreview:paper-height:
 *
 * The height of the previewed paper, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAPER_HEIGHT,
					 g_param_spec_float ("paper-height",
							     "Real paper height in inches",
							      "",
							      0,
							      200,
							      11,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:page-left-margin:
 *
 * The size of the page's left margin, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAGE_LEFT_MARGIN,
					 g_param_spec_float ("page-left-margin",
							     "Left margin of the page in inches",
							     "",
							     0,
							     100,
							     0.25,
							     G_PARAM_READWRITE));

/**
 * EomPrintPreview:page-right-margin:
 *
 * The size of the page's right margin, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAGE_RIGHT_MARGIN,
					 g_param_spec_float ("page-right-margin",
							     "Right margin of the page in inches",
							      "",
							      0,
							      200,
							      0.25,
							      G_PARAM_READWRITE));
/**
 * EomPrintPreview:page-top-margin:
 *
 * The size of the page's top margin, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAGE_TOP_MARGIN,
					 g_param_spec_float ("page-top-margin",
							     "Top margin of the page in inches",
							     "",
							      0,
							      100,
							      0.25,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview:page-bottom-margin:
 *
 * The size of the page's bottom margin, in inches.
 */
	g_object_class_install_property (gobject_class,
					 PROP_PAGE_BOTTOM_MARGIN,
					 g_param_spec_float ("page-bottom-margin",
							     "Bottom margin of the page in inches",
							      "",
							      0,
							      200,
							      0.56,
							      G_PARAM_READWRITE));

/**
 * EomPrintPreview::image-moved:
 * @preview: the object which received the signal
 *
 * The #EomPrintPreview::image-moved signal is emitted when the position
 * of the image is changed.
 */
	preview_signals [SIGNAL_IMAGE_MOVED] =
		g_signal_new ("image_moved",
			      G_TYPE_FROM_CLASS (gobject_class),
			      G_SIGNAL_RUN_FIRST, 0, NULL, NULL,
			      g_cclosure_marshal_VOID__VOID, G_TYPE_NONE,
			      0, NULL);
}

static void
eom_print_preview_finalize (GObject *object)
{
	EomPrintPreviewPrivate *priv;

	priv = EOM_PRINT_PREVIEW (object)->priv;

	if (priv->image) {
		g_object_unref (priv->image);
		priv->image = NULL;
	}

	if (priv->image_scaled) {
		g_object_unref (priv->image_scaled);
		priv->image_scaled = NULL;
	}

	if (priv->surface) {
		cairo_surface_destroy (priv->surface);
		priv->surface = NULL;
	}

	G_OBJECT_CLASS (eom_print_preview_parent_class)->finalize (object);
}

static void
eom_print_preview_init (EomPrintPreview *preview)
{
	EomPrintPreviewPrivate *priv;
	gfloat ratio;

	priv = preview->priv = eom_print_preview_get_instance_private (preview);

	priv->area = GTK_WIDGET (gtk_drawing_area_new ());

	gtk_container_add (GTK_CONTAINER (preview), priv->area);

	priv->p_width  =  8.5;
	priv->p_height = 11.0;

	ratio = priv->p_width/priv->p_height;

	gtk_aspect_frame_set (GTK_ASPECT_FRAME (preview),
			      0.5, 0.5, ratio, FALSE);

	priv->image = NULL;
	priv->image_scaled = NULL;
	priv->image_x_align = 0.5;
	priv->image_y_align = 0.5;
	priv->i_scale = 1;

	priv->surface = NULL;
	priv->flag_create_surface = TRUE;

	priv->p_scale = 0;

	priv->l_margin = 0.25;
	priv->r_margin = 0.25;
	priv->t_margin = 0.25;
	priv->b_margin = 0.56;

	priv->grabbed = FALSE;
	priv->cursorx = 0;
	priv->cursory = 0;
	priv->r_dx    = 0;
	priv->r_dy    = 0;
}

static gboolean button_press_event_cb   (GtkWidget *widget, GdkEventButton *bev, gpointer user_data);
static gboolean button_release_event_cb (GtkWidget *widget, GdkEventButton *bev, gpointer user_data);
static gboolean motion_notify_event_cb  (GtkWidget *widget, GdkEventMotion *mev, gpointer user_data);
static gboolean key_press_event_cb      (GtkWidget *widget, GdkEventKey *event, gpointer user_data);

static gboolean draw_cb (GtkDrawingArea *drawing_area, cairo_t *cr, gpointer user_data);
static void size_allocate_cb (GtkWidget *widget, GtkAllocation *allocation, gpointer user_data);

/**
 * eom_print_preview_new_with_pixbuf:
 * @pixbuf: a #GdkPixbuf
 *
 * Creates a new #EomPrintPreview widget, and sets the #GdkPixbuf to preview
 * on it.
 *
 * Returns: A new #EomPrintPreview widget.
 **/
GtkWidget *
eom_print_preview_new_with_pixbuf (GdkPixbuf *pixbuf)
{
	EomPrintPreview *preview;

	g_return_val_if_fail (GDK_IS_PIXBUF (pixbuf), NULL);

	preview = EOM_PRINT_PREVIEW (eom_print_preview_new ());

	preview->priv->image = g_object_ref (pixbuf);

	update_relative_sizes (preview);

	return GTK_WIDGET (preview);
}

/**
 * eom_print_preview_new:
 *
 * Creates a new #EomPrintPreview widget, setting it to the default values,
 * and leaving the page empty. You still need to set the #EomPrintPreview:image
 * property to make it useful.
 *
 * Returns: A new and empty #EomPrintPreview widget.
 **/
GtkWidget *
eom_print_preview_new (void)
{
	EomPrintPreview *preview;
	GtkWidget *area;

	preview = g_object_new (EOM_TYPE_PRINT_PREVIEW, NULL);

	area = preview->priv->area;

	gtk_widget_set_events (area,
			       GDK_EXPOSURE_MASK            |
			       GDK_POINTER_MOTION_MASK      |
			       GDK_BUTTON_PRESS_MASK        |
			       GDK_BUTTON_RELEASE_MASK      |
			       GDK_KEY_PRESS_MASK);

	g_object_set (G_OBJECT (area),
		      "can-focus", TRUE,
		      NULL);

/* 	update_relative_sizes (preview); */

	g_signal_connect (area, "draw",
	                  G_CALLBACK (draw_cb),
	                  preview);

	g_signal_connect (area, "motion-notify-event",
			  G_CALLBACK (motion_notify_event_cb),
	                  preview);

 	g_signal_connect (area, "button-press-event",
	                  G_CALLBACK (button_press_event_cb),
	                  preview);

	g_signal_connect (area, "button-release-event",
	                  G_CALLBACK (button_release_event_cb),
	                  preview);

	g_signal_connect (area, "key-press-event",
	                  G_CALLBACK (key_press_event_cb),
	                  preview);

	g_signal_connect (area, "size-allocate",
	                  G_CALLBACK (size_allocate_cb),
	                  preview);

	return GTK_WIDGET (preview);
}

static gboolean
draw_cb (GtkDrawingArea *drawing_area,
		 cairo_t *cr,
		 gpointer  user_data)
{
	update_relative_sizes (EOM_PRINT_PREVIEW (user_data));

	eom_print_preview_draw (EOM_PRINT_PREVIEW (user_data), cr);

	if (cairo_status (cr) != CAIRO_STATUS_SUCCESS) {
		fprintf (stderr, "Cairo is unhappy: %s\n",
		    cairo_status_to_string (cairo_status (cr)));
	}

	return TRUE;
}

/**
 * get_current_image_coordinates:
 * @preview: an #EomPrintPreview
 * @x0: A pointer where to store the x coordinate.
 * @y0: A pointer where to store the y coordinate.
 *
 * This function returns the current image coordinates, according
 * with the properties of the given @preview widget.
 **/
static void
get_current_image_coordinates (EomPrintPreview *preview,
			       gint *x0, gint *y0)
{
	EomPrintPreviewPrivate *priv;
	GtkAllocation allocation;

	priv = preview->priv;
	gtk_widget_get_allocation (GTK_WIDGET (priv->area), &allocation);

	*x0 = (gint)((1 - priv->image_x_align)*priv->l_rmargin +  priv->image_x_align*(allocation.width - priv->r_rmargin - priv->r_width));
	*y0 = (gint)((1 - priv->image_y_align)*priv->t_rmargin +  priv->image_y_align*(allocation.height - priv->b_rmargin - priv->r_height));
}

/**
 * press_inside_image_area:
 * @preview: an #EomPrintPreview
 * @x: the points x coordinate
 * @y: the points y coordinate
 *
 * Returns whether the given point is inside the image area.
 *
 * Returns: %TRUE if the given point is inside of the image area,
 * %FALSE otherwise.
 **/
static gboolean
press_inside_image_area (EomPrintPreview *preview,
			 guint x,
			 guint y)
{
	EomPrintPreviewPrivate *priv;
	gint x0, y0;

	priv = preview->priv;
	get_current_image_coordinates (preview, &x0, &y0);

	if (x >= x0 &&  y >= y0 &&
	    x <= x0 + priv->r_width && y <= y0 + priv->r_height)
		return TRUE;

	return FALSE;
}

static void
create_image_scaled (EomPrintPreview *preview)
{
	EomPrintPreviewPrivate *priv = preview->priv;

	if (!priv->image_scaled) {
		gint i_width, i_height;
		GtkAllocation allocation;

		gtk_widget_get_allocation (priv->area, &allocation);
		i_width = gdk_pixbuf_get_width (priv->image);
		i_height = gdk_pixbuf_get_height (priv->image);

                if ((i_width > allocation.width) ||
		    (i_height > allocation.height)) {
			gdouble scale;
			scale = MIN ((gdouble) allocation.width/i_width,
				     (gdouble) allocation.height/i_height);
			scale *= gtk_widget_get_scale_factor (GTK_WIDGET (priv->area));
			priv->image_scaled = gdk_pixbuf_scale_simple (priv->image,
								      i_width*scale,
								      i_height*scale,
								      GDK_INTERP_TILES);
		} else {
			priv->image_scaled = priv->image;
			g_object_ref (priv->image_scaled);
		}
	}
}

static GdkPixbuf *
create_preview_buffer (EomPrintPreview *preview)
{
	GdkPixbuf *pixbuf;
	gint width, height, widget_scale;
	GdkInterpType type = GDK_INTERP_TILES;

	if (preview->priv->image == NULL) {
		return NULL;
	}

	create_image_scaled (preview);

	width  = gdk_pixbuf_get_width (preview->priv->image);
	height = gdk_pixbuf_get_height (preview->priv->image);
	widget_scale = gtk_widget_get_scale_factor (GTK_WIDGET (preview->priv->area));

	width   *= preview->priv->i_scale * preview->priv->p_scale
			* widget_scale;
	height  *= preview->priv->i_scale * preview->priv->p_scale
			* widget_scale;

	if (width < 1 || height < 1)
		return NULL;

	/* to use GDK_INTERP_TILES for small pixbufs is expensive and unnecessary */
	if (width < 25 || height < 25)
		type = GDK_INTERP_NEAREST;

	if (preview->priv->image_scaled) {
		pixbuf = gdk_pixbuf_scale_simple (preview->priv->image_scaled,
						  width, height, type);
	} else {
		pixbuf = gdk_pixbuf_scale_simple (preview->priv->image,
						  width, height, type);
	}

	return pixbuf;
}

static void
create_surface (EomPrintPreview *preview)
{
	EomPrintPreviewPrivate *priv = preview->priv;
	GdkPixbuf *pixbuf;

	if (priv->surface) {
		cairo_surface_destroy (priv->surface);
		priv->surface = NULL;
	}

	pixbuf = create_preview_buffer (preview);
	if (pixbuf) {
		priv->surface =
			gdk_cairo_surface_create_from_pixbuf (pixbuf, 0,
			                                      gtk_widget_get_window (GTK_WIDGET (preview)));
		g_object_unref (pixbuf);
	}
	priv->flag_create_surface = FALSE;
}

static gboolean
create_surface_when_idle (EomPrintPreview *preview)
{
	create_surface (preview);

	return FALSE;
}

static gboolean
button_press_event_cb (GtkWidget *widget,
		       GdkEventButton *event,
		       gpointer user_data)
{
	EomPrintPreview *preview = EOM_PRINT_PREVIEW (user_data);

	preview->priv->cursorx = event->x;
	preview->priv->cursory = event->y;

	switch (event->button) {
	case 1:
		preview->priv->grabbed = press_inside_image_area (preview, event->x, event->y);
		break;
	}

	if (preview->priv->grabbed) {
		gtk_widget_queue_draw (GTK_WIDGET (preview));
	}

	gtk_widget_grab_focus (preview->priv->area);

	return FALSE;
}

static gboolean
button_release_event_cb (GtkWidget *widget,
			 GdkEventButton *event,
			 gpointer user_data)
{
	EomPrintPreview *preview = EOM_PRINT_PREVIEW (user_data);

	switch (event->button) {
	case 1:
		preview->priv->grabbed = FALSE;
		preview->priv->r_dx = 0;
		preview->priv->r_dy = 0;
		gtk_widget_queue_draw (GTK_WIDGET (preview));

	}
	return FALSE;
}

static gboolean
key_press_event_cb (GtkWidget   *widget,
		    GdkEventKey *event,
		    gpointer     user_data)
{
	gfloat delta, align;
	gboolean stop_emission = FALSE;
	const gchar *property;

	delta = 0;

	switch (event->keyval) {
	case GDK_KEY_Left:
		property = "image-x-align";
		delta = -0.01;
		break;
	case GDK_KEY_Right:
		property = "image-x-align";
		delta = 0.01;
		break;
	case GDK_KEY_Up:
		property = "image-y-align";
		delta = -0.01;
		break;
	case GDK_KEY_Down:
		property = "image-y-align";
		delta = 0.01;
		break;
	}

	if (delta != 0) {
		g_object_get (G_OBJECT (user_data),
			      property, &align,
			      NULL);

		align += delta;
		align = CLAMP (align, 0, 1);
		g_object_set (G_OBJECT (user_data),
			      property, align,
			      NULL);

		stop_emission = TRUE;
		g_signal_emit (user_data, preview_signals[SIGNAL_IMAGE_MOVED], 0);
	}

	return stop_emission;
}

static gboolean
motion_notify_event_cb (GtkWidget      *widget,
			GdkEventMotion *event,
			gpointer        user_data)
{
	EomPrintPreviewPrivate *priv = EOM_PRINT_PREVIEW (user_data)->priv;
	gdouble dx, dy;
	GtkAllocation allocation;

	if (priv->grabbed) {
		dx = event->x - priv->cursorx;
		dy = event->y - priv->cursory;

		gtk_widget_get_allocation (widget, &allocation);

		/* Make sure the image stays inside the margins */

		priv->image_x_align += (dx + priv->r_dx)/(allocation.width  - priv->r_width - priv->l_rmargin - priv->r_rmargin);
		if (priv->image_x_align < 0. || priv->image_x_align > 1.) {
			priv->image_x_align = CLAMP (priv->image_x_align, 0., 1.);
			priv->r_dx += dx;
		}
		else
			priv->r_dx = 0;

		priv->image_y_align += (dy + priv->r_dy)/(allocation.height - priv->r_height - priv->t_rmargin - priv->b_rmargin);
		if (priv->image_y_align < 0. || priv->image_y_align > 1.) {
			priv->image_y_align = CLAMP (priv->image_y_align, 0., 1.);
			priv->r_dy += dy;
		} else
			priv->r_dy = 0;

		/* we do this to correctly change the property values */
		g_object_set (EOM_PRINT_PREVIEW (user_data),
			      "image-x-align", priv->image_x_align,
			      "image-y-align", priv->image_y_align,
			      NULL);

		priv->cursorx = event->x;
		priv->cursory = event->y;

		g_signal_emit (user_data, preview_signals[SIGNAL_IMAGE_MOVED], 0);
	} else {
		if (press_inside_image_area (EOM_PRINT_PREVIEW (user_data), event->x, event->y)) {
		  	GdkCursor *cursor;
			cursor = gdk_cursor_new_for_display (gtk_widget_get_display (widget),
							     GDK_FLEUR);
			gdk_window_set_cursor (gtk_widget_get_window (widget),
					       cursor);
			g_object_unref (cursor);
		} else {
			gdk_window_set_cursor (gtk_widget_get_window (widget),
					       NULL);
		}
	}
	return FALSE;
}

static void
size_allocate_cb (GtkWidget *widget,
		  GtkAllocation *allocation,
		  gpointer user_data)
{
	EomPrintPreview *preview;

	preview = EOM_PRINT_PREVIEW (user_data);
	update_relative_sizes (preview);

	preview->priv->flag_create_surface = TRUE;

	if (preview->priv->image_scaled) {
		g_object_unref (preview->priv->image_scaled);
		preview->priv->image_scaled = NULL;
	}

	g_idle_add ((GSourceFunc) create_surface_when_idle, preview);
}

static void
eom_print_preview_draw (EomPrintPreview *preview, cairo_t *cr)
{
	EomPrintPreviewPrivate *priv;
	GtkWidget *area;
	GtkAllocation allocation;
	gint x0, y0;
	gboolean has_focus;

	priv = preview->priv;
	area = priv->area;

	has_focus = gtk_widget_has_focus (area);

	gtk_widget_get_allocation (area, &allocation);

	/* draw the page */
	cairo_set_source_rgb (cr, 1., 1., 1.);
 	cairo_rectangle (cr, 0, 0, allocation.width, allocation.height);
 	cairo_fill (cr);

	/* draw the page margins */
	cairo_set_source_rgb (cr, 0., 0., 0.);
	cairo_set_line_width (cr, 0.1);
	cairo_rectangle (cr,
			 priv->l_rmargin, priv->t_rmargin,
			 allocation.width - priv->l_rmargin - priv->r_rmargin,
			 allocation.height - priv->t_rmargin - priv->b_rmargin);
	cairo_stroke (cr);

	get_current_image_coordinates (preview, &x0, &y0);

	if (priv->flag_create_surface) {
		create_surface (preview);
	}

	if (priv->surface) {
		cairo_set_source_surface (cr, priv->surface, x0, y0);
		cairo_paint (cr);
	} else if (priv->image_scaled) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		gdouble scale = priv->i_scale * priv->p_scale *
			gdk_pixbuf_get_width (priv->image) / gdk_pixbuf_get_width (priv->image_scaled);
		x0 /= scale;
		y0 /= scale;

		cairo_scale (cr, scale, scale);
		gdk_cairo_set_source_pixbuf (cr, priv->image_scaled, x0, y0);
		cairo_paint (cr);
	} else if (priv->image) {
		/* just in the remote case we don't have the surface */

		/* adjust (x0, y0) to the new scale */
		x0 /=  priv->i_scale * priv->p_scale;
		y0 /=  priv->i_scale * priv->p_scale;

		cairo_scale (cr, priv->i_scale*priv->p_scale, priv->i_scale*priv->p_scale);
		gdk_cairo_set_source_pixbuf (cr, priv->image, x0, y0);
		cairo_paint (cr);
	}

	if (has_focus) {
		GtkStyleContext *ctx;

		ctx = gtk_widget_get_style_context (area);
		gtk_render_focus (ctx, cr, 0, 0,
				  allocation.width, allocation.height);
	}
}

static void
update_relative_sizes (EomPrintPreview *preview)
{
	EomPrintPreviewPrivate *priv;
	GtkAllocation allocation;
	gint i_width, i_height;

	priv = preview->priv;

	if (priv->image != NULL) {
		i_width = gdk_pixbuf_get_width (priv->image);
		i_height = gdk_pixbuf_get_height (priv->image);
	} else {
		i_width = i_height = 0;
	}

	gtk_widget_get_allocation (priv->area, &allocation);

	priv->p_scale = (gfloat) allocation.width / (priv->p_width * 72.0);

	priv->r_width  = (gint) i_width  * priv->i_scale * priv->p_scale;
	priv->r_height = (gint) i_height * priv->i_scale * priv->p_scale;

	priv->l_rmargin = (gint) (72. * priv->l_margin * priv->p_scale);
	priv->r_rmargin = (gint) (72. * priv->r_margin * priv->p_scale);
	priv->t_rmargin = (gint) (72. * priv->t_margin * priv->p_scale);
	priv->b_rmargin = (gint) (72. * priv->b_margin * priv->p_scale);
}

/**
 * eom_print_preview_set_page_margins:
 * @preview: a #EomPrintPreview
 * @l_margin: Left margin.
 * @r_margin: Right margin.
 * @t_margin: Top margin.
 * @b_margin: Bottom margin.
 *
 * Manually set the margins, in inches.
 **/
void
eom_print_preview_set_page_margins (EomPrintPreview *preview,
				    gfloat l_margin,
				    gfloat r_margin,
				    gfloat t_margin,
				    gfloat b_margin)
{
	g_return_if_fail (EOM_IS_PRINT_PREVIEW (preview));

	g_object_set (G_OBJECT(preview),
		      "page-left-margin",   l_margin,
		      "page-right-margin",  r_margin,
		      "page-top-margin",    t_margin,
		      "page-bottom-margin", r_margin,
		      NULL);
}

/**
 * eom_print_preview_set_from_page_setup:
 * @preview: a #EomPrintPreview
 * @setup: a #GtkPageSetup to set the properties from
 *
 * Sets up the page properties from a #GtkPageSetup. Useful when using the
 * widget with the GtkPrint API.
 **/
void
eom_print_preview_set_from_page_setup (EomPrintPreview *preview,
				       GtkPageSetup *setup)
{
	g_return_if_fail (EOM_IS_PRINT_PREVIEW (preview));
	g_return_if_fail (GTK_IS_PAGE_SETUP (setup));

	g_object_set (G_OBJECT (preview),
		      "page-left-margin", gtk_page_setup_get_left_margin (setup, GTK_UNIT_INCH),
		      "page-right-margin", gtk_page_setup_get_right_margin (setup, GTK_UNIT_INCH),
		      "page-top-margin", gtk_page_setup_get_top_margin (setup, GTK_UNIT_INCH),
		      "page-bottom-margin", gtk_page_setup_get_bottom_margin (setup, GTK_UNIT_INCH),
		      "paper-width", gtk_page_setup_get_paper_width (setup, GTK_UNIT_INCH),
		      "paper-height", gtk_page_setup_get_paper_height (setup, GTK_UNIT_INCH),
		      NULL);

}

/**
 * eom_print_preview_get_image_position:
 * @preview: a #EomPrintPreview
 * @x: a pointer to a #gdouble, or %NULL to ignore it
 * @y: a pointer to a #gdouble, or %NULL to ignore it
 *
 * Gets current image position in inches, relative to the margins. A
 * (0, 0) position is the intersection between the left and top margins.
 **/
void
eom_print_preview_get_image_position (EomPrintPreview *preview,
				      gdouble *x,
				      gdouble *y)
{
	EomPrintPreviewPrivate *priv;
	gdouble width, height;

	g_return_if_fail (EOM_IS_PRINT_PREVIEW (preview));

	priv = preview->priv;

	if (x != NULL) {
		width  = gdk_pixbuf_get_width (priv->image)  * priv->i_scale / 72.;
		*x = priv->image_x_align * (priv->p_width  - priv->l_margin - priv->r_margin - width);
	}
	if (y != NULL) {
		height = gdk_pixbuf_get_height (priv->image) * priv->i_scale / 72.;
		*y = priv->image_y_align * (priv->p_height - priv->t_margin - priv->b_margin - height);
	}
}

/**
 * eom_print_preview_set_image_position:
 * @preview: a #EomPrintPreview
 * @x: The X coordinate, in inches, or -1 to ignore it.
 * @y: The Y coordinate, in inches, or -1 to ignore it.
 *
 * Sets the image position. You can pass -1 to one of the coordinates if you
 * only want to set the other.
 **/
void
eom_print_preview_set_image_position (EomPrintPreview *preview,
				      gdouble x,
				      gdouble y)
{
	EomPrintPreviewPrivate *priv;
	gfloat x_align, y_align;
	gdouble width, height;

	g_return_if_fail (EOM_IS_PRINT_PREVIEW (preview));

	priv = preview->priv;

	if (x != -1) {
		width  = gdk_pixbuf_get_width (priv->image) * priv->i_scale / 72.;
		x_align = CLAMP (x/(priv->p_width - priv->l_margin - priv->r_margin - width), 0, 1);
		g_object_set (preview, "image-x-align", x_align, NULL);
	}

	if (y != -1) {
		height  = gdk_pixbuf_get_height (priv->image) * priv->i_scale / 72.;
		y_align = CLAMP (y/(priv->p_height - priv->t_margin - priv->b_margin - height), 0, 1);
		g_object_set (preview, "image-y-align", y_align, NULL);
	}
}

/**
 * eom_print_preview_set_scale:
 * @preview: a #EomPrintPreview
 * @scale: a scale value, between 0 and 1.
 *
 * Sets the scale for the image.
 **/
void
eom_print_preview_set_scale (EomPrintPreview *preview,
			     gfloat           scale)
{
	g_return_if_fail (EOM_IS_PRINT_PREVIEW (preview));

	g_object_set (preview,
		      "image-scale", scale,
		      NULL);
}