summaryrefslogtreecommitdiff
path: root/liblogdiag/ld-canvas.c
blob: ec960f8065d61aa51f9c77eac4f70f57833deaf1 (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
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
/*
 * ld-canvas.c
 *
 * This file is a part of logdiag.
 * Copyright Přemysl Janouch 2010 - 2011. All rights reserved.
 *
 * See the file LICENSE for licensing information.
 *
 */

#include <math.h>
#include <string.h>
#include <gdk/gdkkeysyms.h>

#include "liblogdiag.h"
#include "config.h"


/**
 * SECTION:ld-canvas
 * @short_description: A canvas.
 * @see_also: #LdDiagram
 *
 * #LdCanvas displays and enables the user to manipulate with an #LdDiagram.
 */

/* Milimetres per inch. */
#define MM_PER_INCH 25.4
/* The default screen resolution in DPI units. */
#define DEFAULT_SCREEN_RESOLUTION 96

/* The maximal, minimal and default values of zoom. */
#define ZOOM_MIN 0.01
#define ZOOM_MAX 100
#define ZOOM_DEFAULT 1
/* Multiplication factor for zooming with mouse wheel. */
#define ZOOM_WHEEL_STEP 1.4

/* When drawing is requested, extend all sides of
 * the rectangle to be drawn by this number of pixels.
 */
#define QUEUE_DRAW_EXTEND 3
/* Cursor tolerance for object borders. */
#define OBJECT_BORDER_TOLERANCE 3
/* Tolerance on all sides of symbols for strokes. */
#define SYMBOL_CLIP_TOLERANCE 5

/* Size of a highlighted terminal. */
#define TERMINAL_RADIUS 5
/* Tolerance around terminal points. */
#define TERMINAL_HOVER_TOLERANCE 8

/*
 * OperationEnd:
 *
 * Called upon ending an operation.
 */
typedef void (*OperationEnd) (LdCanvas *self);

enum
{
	OPER_0,
	OPER_ADD_OBJECT
};

typedef struct _AddObjectData AddObjectData;

struct _AddObjectData
{
	LdDiagramObject *object;
	gboolean visible;
};

enum
{
	COLOR_BASE,
	COLOR_GRID,
	COLOR_OBJECT,
	COLOR_SELECTION,
	COLOR_TERMINAL,
	COLOR_COUNT
};

typedef struct _LdCanvasColor LdCanvasColor;

struct _LdCanvasColor
{
	gdouble r;
	gdouble g;
	gdouble b;
	gdouble a;
};

/*
 * LdCanvasPrivate:
 * @diagram: A diagram object assigned to this canvas as a model.
 * @library: A library object assigned to this canvas as a model.
 * @adjustment_h: An adjustment object for the horizontal axis, if any.
 * @adjustment_v: An adjustment object for the vertical axis, if any.
 * @x: The X coordinate of the center of view.
 * @y: The Y coordinate of the center of view.
 * @zoom: The current zoom of the canvas.
 * @operation: The current operation.
 * @operation_data: Data related to the current operation.
 * @operation_end: A callback to end the operation.
 * @palette: Colors used by the widget.
 */
struct _LdCanvasPrivate
{
	LdDiagram *diagram;
	LdLibrary *library;

	GtkAdjustment *adjustment_h;
	GtkAdjustment *adjustment_v;

	gdouble x;
	gdouble y;
	gdouble zoom;

	LdPoint terminal;
	gboolean terminal_highlighted;

	gint operation;
	union
	{
		AddObjectData add_object;
	}
	operation_data;
	OperationEnd operation_end;

	LdCanvasColor palette[COLOR_COUNT];
};

#define OPER_DATA(self, member) ((self)->priv->operation_data.member)
#define COLOR_GET(self, name) (&(self)->priv->palette[name])

/*
 * DrawData:
 * @self: Our #LdCanvas.
 * @cr: A cairo context to draw on.
 * @exposed_rect: The area that is to be redrawn.
 * @scale: Computed size of one diagram unit in pixels.
 */
typedef struct _DrawData DrawData;

struct _DrawData
{
	LdCanvas *self;
	cairo_t *cr;
	LdRectangle exposed_rect;
	gdouble scale;
};

enum
{
	PROP_0,
	PROP_DIAGRAM,
	PROP_LIBRARY,
	PROP_ZOOM
};

static void ld_canvas_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec);
static void ld_canvas_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec);
static void ld_canvas_finalize (GObject *gobject);

static void ld_canvas_real_set_scroll_adjustments
	(LdCanvas *self, GtkAdjustment *horizontal, GtkAdjustment *vertical);
static void on_adjustment_value_changed
	(GtkAdjustment *adjustment, LdCanvas *self);
static void on_size_allocate (GtkWidget *widget, GtkAllocation *allocation,
	gpointer user_data);
static void update_adjustments (LdCanvas *self);

static void diagram_connect_signals (LdCanvas *self);
static void diagram_disconnect_signals (LdCanvas *self);

static gdouble ld_canvas_get_base_unit_in_px (GtkWidget *self);
static gdouble ld_canvas_get_scale_in_px (LdCanvas *self);

static void simulate_motion (LdCanvas *self);
static gboolean on_motion_notify (GtkWidget *widget, GdkEventMotion *event,
	gpointer user_data);
static gboolean on_leave_notify (GtkWidget *widget, GdkEventCrossing *event,
	gpointer user_data);
static gboolean on_button_press (GtkWidget *widget, GdkEventButton *event,
	gpointer user_data);
static gboolean on_button_release (GtkWidget *widget, GdkEventButton *event,
	gpointer user_data);
static gboolean on_scroll (GtkWidget *widget, GdkEventScroll *event,
	gpointer user_data);

static void ld_canvas_color_set (LdCanvasColor *color,
	gdouble r, gdouble g, gdouble b, gdouble a);
static void ld_canvas_color_apply (LdCanvasColor *color, cairo_t *cr);

static void move_object_to_coords (LdCanvas *self, LdDiagramObject *object,
	gdouble x, gdouble y);
static LdDiagramObject *get_object_at_coords (LdCanvas *self,
	gdouble x, gdouble y);
static gboolean is_object_selected (LdCanvas *self, LdDiagramObject *object);
static LdSymbol *resolve_diagram_symbol (LdCanvas *self,
	LdDiagramSymbol *diagram_symbol);
static gboolean get_symbol_area (LdCanvas *self, LdDiagramSymbol *symbol,
	LdRectangle *rect);
static gboolean get_symbol_clip_area (LdCanvas *self, LdDiagramSymbol *symbol,
	LdRectangle *rect);
static gboolean get_object_area (LdCanvas *self, LdDiagramObject *object,
	LdRectangle *rect);
static gboolean object_hit_test (LdCanvas *self, LdDiagramObject *object,
	gdouble x, gdouble y);
static void check_terminals (LdCanvas *self, gdouble x, gdouble y);
static void hide_terminals (LdCanvas *self);
static void queue_draw (LdCanvas *self, LdRectangle *rect);
static void queue_object_draw (LdCanvas *self, LdDiagramObject *object);
static void queue_terminal_draw (LdCanvas *self, LdPoint *terminal);

static void ld_canvas_real_cancel_operation (LdCanvas *self);
static void ld_canvas_add_object_end (LdCanvas *self);

static gboolean on_expose_event (GtkWidget *widget, GdkEventExpose *event,
	gpointer user_data);
static void draw_grid (GtkWidget *widget, DrawData *data);
static void draw_diagram (GtkWidget *widget, DrawData *data);
static void draw_terminal (GtkWidget *widget, DrawData *data);
static void draw_object (LdDiagramObject *diagram_object, DrawData *data);
static void draw_symbol (LdDiagramSymbol *diagram_symbol, DrawData *data);


G_DEFINE_TYPE (LdCanvas, ld_canvas, GTK_TYPE_DRAWING_AREA);

static void
ld_canvas_class_init (LdCanvasClass *klass)
{
	GObjectClass *object_class;
	GtkWidgetClass *widget_class;
	GtkBindingSet *binding_set;
	GParamSpec *pspec;

	widget_class = GTK_WIDGET_CLASS (klass);

	object_class = G_OBJECT_CLASS (klass);
	object_class->get_property = ld_canvas_get_property;
	object_class->set_property = ld_canvas_set_property;
	object_class->finalize = ld_canvas_finalize;

	klass->set_scroll_adjustments = ld_canvas_real_set_scroll_adjustments;
	klass->cancel_operation = ld_canvas_real_cancel_operation;

	binding_set = gtk_binding_set_by_class (klass);
	gtk_binding_entry_add_signal (binding_set, GDK_Escape, 0,
		"cancel-operation", 0);

/**
 * LdCanvas:diagram:
 *
 * The underlying #LdDiagram object of this canvas.
 */
	pspec = g_param_spec_object ("diagram", "Diagram",
		"The underlying diagram object of this canvas.",
		LD_TYPE_DIAGRAM, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_DIAGRAM, pspec);

/**
 * LdCanvas:library:
 *
 * The #LdLibrary that this canvas retrieves symbols from.
 */
	pspec = g_param_spec_object ("library", "Library",
		"The library that this canvas retrieves symbols from.",
		LD_TYPE_LIBRARY, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_LIBRARY, pspec);

/**
 * LdCanvas:zoom:
 *
 * The zoom of this canvas.
 */
	pspec = g_param_spec_double ("zoom", "Zoom",
		"The zoom of this canvas.",
		ZOOM_MIN, ZOOM_MAX, ZOOM_DEFAULT, G_PARAM_READWRITE);
	g_object_class_install_property (object_class, PROP_ZOOM, pspec);

/**
 * LdCanvas::set-scroll-adjustments:
 * @horizontal: The horizontal #GtkAdjustment.
 * @vertical: The vertical #GtkAdjustment.
 *
 * Set scroll adjustments for the canvas.
 */
	widget_class->set_scroll_adjustments_signal = g_signal_new
		("set-scroll-adjustments", G_TYPE_FROM_CLASS (widget_class),
		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		G_STRUCT_OFFSET (LdCanvasClass, set_scroll_adjustments),
		NULL, NULL,
		g_cclosure_user_marshal_VOID__OBJECT_OBJECT,
		G_TYPE_NONE, 2, GTK_TYPE_ADJUSTMENT, GTK_TYPE_ADJUSTMENT);

/**
 * LdCanvas::cancel-operation:
 *
 * Cancel any current operation.
 */
	klass->cancel_operation_signal = g_signal_new
		("cancel-operation", G_TYPE_FROM_CLASS (klass),
		G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		G_STRUCT_OFFSET (LdCanvasClass, cancel_operation), NULL, NULL,
		g_cclosure_marshal_VOID__VOID,
		G_TYPE_NONE, 0);

	g_type_class_add_private (klass, sizeof (LdCanvasPrivate));
}

static void
ld_canvas_init (LdCanvas *self)
{
	self->priv = G_TYPE_INSTANCE_GET_PRIVATE
		(self, LD_TYPE_CANVAS, LdCanvasPrivate);

	self->priv->x = 0;
	self->priv->y = 0;
	self->priv->zoom = ZOOM_DEFAULT;

	ld_canvas_color_set (COLOR_GET (self, COLOR_BASE), 1, 1, 1, 1);
	ld_canvas_color_set (COLOR_GET (self, COLOR_GRID), 0.5, 0.5, 0.5, 1);
	ld_canvas_color_set (COLOR_GET (self, COLOR_OBJECT), 0, 0, 0, 1);
	ld_canvas_color_set (COLOR_GET (self, COLOR_SELECTION), 0, 0, 1, 1);
	ld_canvas_color_set (COLOR_GET (self, COLOR_TERMINAL), 1, 0.5, 0.5, 1);

	g_signal_connect (self, "size-allocate",
		G_CALLBACK (on_size_allocate), NULL);
	g_signal_connect (self, "expose-event",
		G_CALLBACK (on_expose_event), NULL);

	g_signal_connect (self, "motion-notify-event",
		G_CALLBACK (on_motion_notify), NULL);
	g_signal_connect (self, "leave-notify-event",
		G_CALLBACK (on_leave_notify), NULL);
	g_signal_connect (self, "button-press-event",
		G_CALLBACK (on_button_press), NULL);
	g_signal_connect (self, "button-release-event",
		G_CALLBACK (on_button_release), NULL);
	g_signal_connect (self, "scroll-event",
		G_CALLBACK (on_scroll), NULL);

	g_object_set (self, "can-focus", TRUE, NULL);

	gtk_widget_add_events (GTK_WIDGET (self),
		GDK_POINTER_MOTION_MASK | GDK_POINTER_MOTION_HINT_MASK
		| GDK_BUTTON_PRESS_MASK | GDK_BUTTON_RELEASE_MASK
		| GDK_LEAVE_NOTIFY_MASK);
}

static void
ld_canvas_finalize (GObject *gobject)
{
	LdCanvas *self;

	self = LD_CANVAS (gobject);

	ld_canvas_real_set_scroll_adjustments (self, NULL, NULL);

	if (self->priv->diagram)
	{
		diagram_disconnect_signals (self);
		g_object_unref (self->priv->diagram);
	}
	if (self->priv->library)
		g_object_unref (self->priv->library);

	/* Chain up to the parent class. */
	G_OBJECT_CLASS (ld_canvas_parent_class)->finalize (gobject);
}

static void
ld_canvas_get_property (GObject *object, guint property_id,
	GValue *value, GParamSpec *pspec)
{
	LdCanvas *self;

	self = LD_CANVAS (object);
	switch (property_id)
	{
	case PROP_DIAGRAM:
		g_value_set_object (value, ld_canvas_get_diagram (self));
		break;
	case PROP_LIBRARY:
		g_value_set_object (value, ld_canvas_get_library (self));
		break;
	case PROP_ZOOM:
		g_value_set_double (value, ld_canvas_get_zoom (self));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_canvas_set_property (GObject *object, guint property_id,
	const GValue *value, GParamSpec *pspec)
{
	LdCanvas *self;

	self = LD_CANVAS (object);
	switch (property_id)
	{
	case PROP_DIAGRAM:
		ld_canvas_set_diagram (self, LD_DIAGRAM (g_value_get_object (value)));
		break;
	case PROP_LIBRARY:
		ld_canvas_set_library (self, LD_LIBRARY (g_value_get_object (value)));
		break;
	case PROP_ZOOM:
		ld_canvas_set_zoom (self, g_value_get_double (value));
		break;
	default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
	}
}

static void
ld_canvas_real_set_scroll_adjustments (LdCanvas *self,
	GtkAdjustment *horizontal, GtkAdjustment *vertical)
{
	/* TODO: Infinite canvas. */
	GtkWidget *widget;
	gdouble scale;

	widget = GTK_WIDGET (self);
	scale = ld_canvas_get_scale_in_px (self);

	if (horizontal != self->priv->adjustment_h)
	{
		if (self->priv->adjustment_h)
		{
			g_signal_handlers_disconnect_by_func (self->priv->adjustment_h,
				on_adjustment_value_changed, self);
			g_object_unref (self->priv->adjustment_h);

			self->priv->adjustment_h = NULL;
		}
		if (horizontal)
		{
			g_object_ref (horizontal);
			g_signal_connect (horizontal, "value-changed",
				G_CALLBACK (on_adjustment_value_changed), self);

			horizontal->upper = 100;
			horizontal->lower = -100;
			horizontal->step_increment = 0.5;
			horizontal->page_increment = 5;
			horizontal->page_size = widget->allocation.width / scale;
			horizontal->value = -horizontal->page_size / 2;

			self->priv->adjustment_h = horizontal;
		}
	}

	if (vertical != self->priv->adjustment_v)
	{
		if (self->priv->adjustment_v)
		{
			g_signal_handlers_disconnect_by_func (self->priv->adjustment_v,
				on_adjustment_value_changed, self);
			g_object_unref (self->priv->adjustment_v);

			self->priv->adjustment_v = NULL;
		}
		if (vertical)
		{
			g_object_ref (vertical);
			g_signal_connect (vertical, "value-changed",
				G_CALLBACK (on_adjustment_value_changed), self);

			vertical->upper = 100;
			vertical->lower = -100;
			vertical->step_increment = 0.5;
			vertical->page_increment = 5;
			vertical->page_size = widget->allocation.height / scale;
			vertical->value = -vertical->page_size / 2;

			self->priv->adjustment_v = vertical;
		}
	}
}

static void
on_adjustment_value_changed (GtkAdjustment *adjustment, LdCanvas *self)
{
	GtkWidget *widget;
	gdouble scale;

	widget = GTK_WIDGET (self);
	scale = ld_canvas_get_scale_in_px (self);

	if (adjustment == self->priv->adjustment_h)
	{
		self->priv->x = adjustment->value
			+ widget->allocation.width / scale / 2;
		gtk_widget_queue_draw (widget);
	}
	else if (adjustment == self->priv->adjustment_v)
	{
		self->priv->y = adjustment->value
			+ widget->allocation.height / scale / 2;
		gtk_widget_queue_draw (widget);
	}
}

static void
on_size_allocate (GtkWidget *widget, GtkAllocation *allocation,
	gpointer user_data)
{
	LdCanvas *self;

	self = LD_CANVAS (widget);

	/* FIXME: If the new allocation is bigger, we may see more than
	 *        what we're supposed to be able to see -> adjust X and Y.
	 *
	 *        If the visible area is so large that we simply must see more,
	 *        let's disable the scrollbars in question.
	 */
	update_adjustments (self);
}

static void
update_adjustments (LdCanvas *self)
{
	gdouble scale;

	scale = ld_canvas_get_scale_in_px (self);

	if (self->priv->adjustment_h)
	{
		self->priv->adjustment_h->page_size
			= GTK_WIDGET (self)->allocation.width  / scale;
		self->priv->adjustment_h->value
			= self->priv->x - self->priv->adjustment_h->page_size / 2;
		gtk_adjustment_changed (self->priv->adjustment_h);
	}
	if (self->priv->adjustment_v)
	{
		self->priv->adjustment_v->page_size
			= GTK_WIDGET (self)->allocation.height / scale;
		self->priv->adjustment_v->value
			= self->priv->y - self->priv->adjustment_v->page_size / 2;
		gtk_adjustment_changed (self->priv->adjustment_v);
	}
}


/* ===== Generic interface etc. ============================================ */

/**
 * ld_canvas_new:
 *
 * Create an instance.
 */
LdCanvas *
ld_canvas_new (void)
{
	return g_object_new (LD_TYPE_CANVAS, NULL);
}

/**
 * ld_canvas_set_diagram:
 * @self: An #LdCanvas object.
 * @diagram: The #LdDiagram to be assigned to the canvas.
 *
 * Assign an #LdDiagram object to the canvas.
 */
void
ld_canvas_set_diagram (LdCanvas *self, LdDiagram *diagram)
{
	g_return_if_fail (LD_IS_CANVAS (self));
	g_return_if_fail (LD_IS_DIAGRAM (diagram));

	if (self->priv->diagram)
	{
		diagram_disconnect_signals (self);
		g_object_unref (self->priv->diagram);
	}

	self->priv->diagram = diagram;
	diagram_connect_signals (self);
	g_object_ref (diagram);

	g_object_notify (G_OBJECT (self), "diagram");
}

/**
 * ld_canvas_get_diagram:
 * @self: An #LdCanvas object.
 *
 * Get the #LdDiagram object assigned to this canvas.
 * The reference count on the diagram is not incremented.
 */
LdDiagram *
ld_canvas_get_diagram (LdCanvas *self)
{
	g_return_val_if_fail (LD_IS_CANVAS (self), NULL);
	return self->priv->diagram;
}

static void
diagram_connect_signals (LdCanvas *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self->priv->diagram));

	g_signal_connect_swapped (self->priv->diagram, "changed",
		G_CALLBACK (gtk_widget_queue_draw), self);
	g_signal_connect_swapped (self->priv->diagram, "selection-changed",
		G_CALLBACK (gtk_widget_queue_draw), self);
}

static void
diagram_disconnect_signals (LdCanvas *self)
{
	g_return_if_fail (LD_IS_DIAGRAM (self->priv->diagram));

	g_signal_handlers_disconnect_matched (self->priv->diagram,
		G_SIGNAL_MATCH_FUNC | G_SIGNAL_MATCH_DATA, 0, 0, NULL,
		gtk_widget_queue_draw, self);
}

/**
 * ld_canvas_set_library:
 * @self: An #LdCanvas object.
 * @library: The #LdLibrary to be assigned to the canvas.
 *
 * Assign an #LdLibrary object to the canvas.
 */
void
ld_canvas_set_library (LdCanvas *self, LdLibrary *library)
{
	g_return_if_fail (LD_IS_CANVAS (self));
	g_return_if_fail (LD_IS_LIBRARY (library));

	if (self->priv->library)
		g_object_unref (self->priv->library);

	self->priv->library = library;
	g_object_ref (library);

	g_object_notify (G_OBJECT (self), "library");
}

/**
 * ld_canvas_get_library:
 * @self: An #LdCanvas object.
 *
 * Get the #LdLibrary object assigned to this canvas.
 * The reference count on the library is not incremented.
 */
LdLibrary *
ld_canvas_get_library (LdCanvas *self)
{
	g_return_val_if_fail (LD_IS_CANVAS (self), NULL);
	return self->priv->library;
}

/*
 * ld_canvas_get_base_unit_in_px:
 * @self: A #GtkWidget object to retrieve DPI from (indirectly).
 *
 * Return value: Length of the base unit in pixels.
 */
static gdouble
ld_canvas_get_base_unit_in_px (GtkWidget *self)
{
	gdouble resolution;

	g_return_val_if_fail (GTK_IS_WIDGET (self), 1);

	resolution = gdk_screen_get_resolution (gtk_widget_get_screen (self));
	if (resolution == -1)
		resolution = DEFAULT_SCREEN_RESOLUTION;

	/* XXX: It might look better if the unit was rounded to a whole number. */
	return resolution / MM_PER_INCH * LD_CANVAS_BASE_UNIT_LENGTH;
}

/*
 * ld_canvas_get_scale_in_px:
 * @self: An #LdCanvas object.
 *
 * Return value: Displayed length of the base unit in pixels.
 */
static gdouble
ld_canvas_get_scale_in_px (LdCanvas *self)
{
	g_return_val_if_fail (LD_IS_CANVAS (self), 1);

	return ld_canvas_get_base_unit_in_px (GTK_WIDGET (self))
		* self->priv->zoom;
}

/**
 * ld_canvas_widget_to_diagram_coords:
 * @self: An #LdCanvas object.
 * @wx: The X coordinate to be translated.
 * @wy: The Y coordinate to be translated.
 * @dx: (out): The translated X coordinate.
 * @dy: (out): The translated Y coordinate.
 *
 * Translate coordinates located inside the canvas window
 * into diagram coordinates.
 */
void
ld_canvas_widget_to_diagram_coords (LdCanvas *self,
	gdouble wx, gdouble wy, gdouble *dx, gdouble *dy)
{
	GtkWidget *widget;
	gdouble scale;

	g_return_if_fail (LD_IS_CANVAS (self));
	g_return_if_fail (dx != NULL);
	g_return_if_fail (dy != NULL);

	widget = GTK_WIDGET (self);
	scale = ld_canvas_get_scale_in_px (self);

	/* We know diagram coordinates of the center of the canvas, so we may
	 * translate the given X and Y coordinates to this center and then scale
	 * them by dividing them by the current scale.
	 */
	*dx = self->priv->x + (wx - (widget->allocation.width  * 0.5)) / scale;
	*dy = self->priv->y + (wy - (widget->allocation.height * 0.5)) / scale;
}

/**
 * ld_canvas_diagram_to_widget_coords:
 * @self: An #LdCanvas object.
 * @dx: The X coordinate to be translated.
 * @dy: The Y coordinate to be translated.
 * @wx: (out): The translated X coordinate.
 * @wy: (out): The translated Y coordinate.
 *
 * Translate diagram coordinates into canvas coordinates.
 */
void
ld_canvas_diagram_to_widget_coords (LdCanvas *self,
	gdouble dx, gdouble dy, gdouble *wx, gdouble *wy)
{
	GtkWidget *widget;
	gdouble scale;

	g_return_if_fail (LD_IS_CANVAS (self));
	g_return_if_fail (wx != NULL);
	g_return_if_fail (wy != NULL);

	widget = GTK_WIDGET (self);
	scale = ld_canvas_get_scale_in_px (self);

	/* Just the reversal of ld_canvas_widget_to_diagram_coords(). */
	*wx = scale * (dx - self->priv->x) + 0.5 * widget->allocation.width;
	*wy = scale * (dy - self->priv->y) + 0.5 * widget->allocation.height;
}

/**
 * ld_canvas_get_zoom:
 * @self: An #LdCanvas object.
 *
 * Return value: Zoom of the canvas.
 */
gdouble
ld_canvas_get_zoom (LdCanvas *self)
{
	g_return_val_if_fail (LD_IS_CANVAS (self), -1);
	return self->priv->zoom;
}

/**
 * ld_canvas_set_zoom:
 * @self: An #LdCanvas object.
 * @zoom: The zoom.
 *
 * Set zoom of the canvas.
 */
void
ld_canvas_set_zoom (LdCanvas *self, gdouble zoom)
{
	gdouble clamped_zoom;

	g_return_if_fail (LD_IS_CANVAS (self));

	clamped_zoom = CLAMP (zoom, ZOOM_MIN, ZOOM_MAX);
	if (self->priv->zoom == clamped_zoom)
		return;

	self->priv->zoom = clamped_zoom;

	simulate_motion (self);
	update_adjustments (self);
	gtk_widget_queue_draw (GTK_WIDGET (self));

	g_object_notify (G_OBJECT (self), "zoom");
}


/* ===== Operations ======================================================== */

static void
ld_canvas_real_cancel_operation (LdCanvas *self)
{
	g_return_if_fail (LD_IS_CANVAS (self));

	if (self->priv->operation)
	{
		if (self->priv->operation_end)
			self->priv->operation_end (self);
		self->priv->operation = OPER_0;
		self->priv->operation_end = NULL;
	}
}

/**
 * ld_canvas_add_object_begin:
 * @self: An #LdCanvas object.
 * @object: (transfer full): The object to be added to the diagram.
 *
 * Begin an operation for adding an object into the diagram.
 */
void
ld_canvas_add_object_begin (LdCanvas *self, LdDiagramObject *object)
{
	AddObjectData *data;

	g_return_if_fail (LD_IS_CANVAS (self));
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (object));

	ld_canvas_real_cancel_operation (self);

	self->priv->operation = OPER_ADD_OBJECT;
	self->priv->operation_end = ld_canvas_add_object_end;

	data = &OPER_DATA (self, add_object);
	data->object = object;
}

static void
ld_canvas_add_object_end (LdCanvas *self)
{
	AddObjectData *data;

	data = &OPER_DATA (self, add_object);
	if (data->object)
	{
		queue_object_draw (self, data->object);
		g_object_unref (data->object);
		data->object = NULL;
	}
}


/* ===== Events, rendering ================================================= */

static void
ld_canvas_color_set (LdCanvasColor *color,
	gdouble r, gdouble g, gdouble b, gdouble a)
{
	color->r = r;
	color->g = g;
	color->b = b;
	color->a = a;
}

static void
ld_canvas_color_apply (LdCanvasColor *color, cairo_t *cr)
{
	cairo_set_source_rgba (cr, color->r, color->g, color->b, color->a);
}

static void
move_object_to_coords (LdCanvas *self, LdDiagramObject *object,
	gdouble x, gdouble y)
{
	gdouble dx, dy;

	ld_canvas_widget_to_diagram_coords (self, x, y, &dx, &dy);
	g_object_set (object, "x", floor (dx + 0.5), "y", floor (dy + 0.5), NULL);
}

static LdDiagramObject *
get_object_at_coords (LdCanvas *self, gdouble x, gdouble y)
{
	GList *objects, *iter;

	/* Iterate from the top object downwards. */
	objects = (GList *) ld_diagram_get_objects (self->priv->diagram);
	for (iter = g_list_last (objects); iter; iter = g_list_previous (iter))
	{
		LdDiagramObject *object;

		object = LD_DIAGRAM_OBJECT (iter->data);
		if (object_hit_test (self, object, x, y))
			return object;
	}
	return NULL;
}

static gboolean
is_object_selected (LdCanvas *self, LdDiagramObject *object)
{
	return g_list_find (ld_diagram_get_selection (self->priv->diagram),
		object) != NULL;
}

static LdSymbol *
resolve_diagram_symbol (LdCanvas *self, LdDiagramSymbol *diagram_symbol)
{
	if (!self->priv->library)
		return NULL;

	return ld_library_find_symbol (self->priv->library,
		ld_diagram_symbol_get_class (diagram_symbol));
}

static gboolean
get_symbol_area (LdCanvas *self, LdDiagramSymbol *symbol, LdRectangle *rect)
{
	gdouble object_x, object_y;
	LdSymbol *library_symbol;
	LdRectangle area;
	gdouble x1, x2;
	gdouble y1, y2;

	g_object_get (symbol, "x", &object_x, "y", &object_y, NULL);

	library_symbol = resolve_diagram_symbol (self, symbol);
	if (library_symbol)
		ld_symbol_get_area (library_symbol, &area);
	else
		return FALSE;

	/* TODO: Rotate the rectangle for other orientations. */
	ld_canvas_diagram_to_widget_coords (self,
		object_x + area.x,
		object_y + area.y,
		&x1, &y1);
	ld_canvas_diagram_to_widget_coords (self,
		object_x + area.x + area.width,
		object_y + area.y + area.height,
		&x2, &y2);

	rect->x = x1;
	rect->y = y1;
	rect->width  = x2 - x1;
	rect->height = y2 - y1;
	return TRUE;
}

static gboolean
get_symbol_clip_area (LdCanvas *self, LdDiagramSymbol *symbol,
	LdRectangle *rect)
{
	LdRectangle object_rect;

	if (!get_object_area (self, LD_DIAGRAM_OBJECT (symbol), &object_rect))
		return FALSE;

	*rect = object_rect;
	ld_rectangle_extend (rect, SYMBOL_CLIP_TOLERANCE);
	return TRUE;
}

static gboolean
get_object_area (LdCanvas *self, LdDiagramObject *object, LdRectangle *rect)
{
	if (LD_IS_DIAGRAM_SYMBOL (object))
		return get_symbol_area (self, LD_DIAGRAM_SYMBOL (object), rect);
	return FALSE;
}

static gboolean
object_hit_test (LdCanvas *self, LdDiagramObject *object, gdouble x, gdouble y)
{
	LdRectangle rect;

	if (!get_object_area (self, object, &rect))
		return FALSE;
	ld_rectangle_extend (&rect, OBJECT_BORDER_TOLERANCE);
	return ld_rectangle_contains (&rect, x, y);
}

static void
check_terminals (LdCanvas *self, gdouble x, gdouble y)
{
	GList *objects, *iter;
	LdDiagramSymbol *closest_symbol = NULL;
	gdouble closest_distance = TERMINAL_HOVER_TOLERANCE;
	LdPoint closest_terminal;

	objects = (GList *) ld_diagram_get_objects (self->priv->diagram);
	for (iter = objects; iter; iter = g_list_next (iter))
	{
		LdDiagramObject *diagram_object;
		gdouble object_x, object_y;
		LdDiagramSymbol *diagram_symbol;
		LdSymbol *symbol;
		const LdPointArray *terminals;
		gint i;

		if (!LD_IS_DIAGRAM_SYMBOL (iter->data))
			continue;

		diagram_symbol = LD_DIAGRAM_SYMBOL (iter->data);
		symbol = resolve_diagram_symbol (self, diagram_symbol);
		if (!symbol)
			continue;

		diagram_object = LD_DIAGRAM_OBJECT (iter->data);
		g_object_get (diagram_object, "x", &object_x, "y", &object_y, NULL);

		terminals = ld_symbol_get_terminals (symbol);

		for (i = 0; i < terminals->num_points; i++)
		{
			LdPoint cur_term;
			gdouble distance;

			cur_term = terminals->points[i];
			cur_term.x += object_x;
			cur_term.y += object_y;
			ld_canvas_diagram_to_widget_coords (self,
				cur_term.x, cur_term.y, &cur_term.x, &cur_term.y);

			distance = ld_point_distance (&cur_term, x, y);
			if (distance <= closest_distance)
			{
				closest_symbol = diagram_symbol;
				closest_distance = distance;
				closest_terminal = cur_term;
			}
		}
	}

	hide_terminals (self);

	if (closest_symbol)
	{
		self->priv->terminal_highlighted = TRUE;
		self->priv->terminal = closest_terminal;
		queue_terminal_draw (self, &closest_terminal);
	}
}

static void
hide_terminals (LdCanvas *self)
{
	if (self->priv->terminal_highlighted)
	{
		self->priv->terminal_highlighted = FALSE;
		queue_terminal_draw (self, &self->priv->terminal);
	}
}

static void
queue_draw (LdCanvas *self, LdRectangle *rect)
{
	LdRectangle area;

	area = *rect;
	ld_rectangle_extend (&area, QUEUE_DRAW_EXTEND);
	gtk_widget_queue_draw_area (GTK_WIDGET (self),
		area.x, area.y, area.width, area.height);
}

static void
queue_object_draw (LdCanvas *self, LdDiagramObject *object)
{
	if (LD_IS_DIAGRAM_SYMBOL (object))
	{
		LdRectangle rect;

		if (!get_symbol_clip_area (self, LD_DIAGRAM_SYMBOL (object), &rect))
			return;
		queue_draw (self, &rect);
	}
}

static void
queue_terminal_draw (LdCanvas *self, LdPoint *terminal)
{
	LdRectangle rect;

	rect.x = terminal->x - TERMINAL_RADIUS;
	rect.y = terminal->y - TERMINAL_RADIUS;
	rect.width  = 2 * TERMINAL_RADIUS;
	rect.height = 2 * TERMINAL_RADIUS;
	queue_draw (self, &rect);
}

static void
simulate_motion (LdCanvas *self)
{
	GdkEventMotion event;
	GtkWidget *widget;
	gint x, y;
	GdkModifierType state;

	widget = GTK_WIDGET (self);

	if (gdk_window_get_pointer (widget->window, &x, &y, &state)
		!= widget->window)
		return;

	memset (&event, 0, sizeof (event));
	event.type = GDK_MOTION_NOTIFY;
	event.window = widget->window;
	event.x = x;
	event.y = y;
	event.state = state;

	on_motion_notify (widget, &event, NULL);
}

static gboolean
on_motion_notify (GtkWidget *widget, GdkEventMotion *event, gpointer user_data)
{
	LdCanvas *self;

	self = LD_CANVAS (widget);
	switch (self->priv->operation)
	{
		AddObjectData *data;

	case OPER_ADD_OBJECT:
		data = &OPER_DATA (self, add_object);
		data->visible = TRUE;

		queue_object_draw (self, data->object);
		move_object_to_coords (self, data->object, event->x, event->y);
		queue_object_draw (self, data->object);
		break;
	case OPER_0:
		check_terminals (self, event->x, event->y);
		break;
	}
	return FALSE;
}

static gboolean
on_leave_notify (GtkWidget *widget, GdkEventCrossing *event, gpointer user_data)
{
	LdCanvas *self;

	self = LD_CANVAS (widget);
	switch (self->priv->operation)
	{
		AddObjectData *data;

	case OPER_ADD_OBJECT:
		data = &OPER_DATA (self, add_object);
		data->visible = FALSE;

		queue_object_draw (self, data->object);
		break;
	}
	return FALSE;
}

static gboolean
on_button_press (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
	LdCanvas *self;

	if (!gtk_widget_has_focus (widget))
		gtk_widget_grab_focus (widget);

	self = LD_CANVAS (widget);
	switch (self->priv->operation)
	{
		AddObjectData *data;

	case OPER_ADD_OBJECT:
		data = &OPER_DATA (self, add_object);

		queue_object_draw (self, data->object);
		move_object_to_coords (self, data->object, event->x, event->y);

		if (self->priv->diagram)
			ld_diagram_insert_object (self->priv->diagram, data->object, -1);

		/* XXX: "cancel" causes confusion. */
		ld_canvas_real_cancel_operation (self);
		break;
	case OPER_0:
		if (self->priv->diagram)
		{
			LdDiagramObject *object;

			if (event->state != GDK_SHIFT_MASK)
				ld_diagram_unselect_all (self->priv->diagram);

			object = get_object_at_coords (self, event->x, event->y);
			if (object)
				ld_diagram_select (self->priv->diagram, object);
		}
		break;
	}
	return FALSE;
}

static gboolean
on_button_release (GtkWidget *widget, GdkEventButton *event, gpointer user_data)
{
	return FALSE;
}

static gboolean
on_scroll (GtkWidget *widget, GdkEventScroll *event, gpointer user_data)
{
	gdouble prev_x, prev_y;
	gdouble new_x, new_y;
	LdCanvas *self;

	self = LD_CANVAS (widget);

	ld_canvas_widget_to_diagram_coords (self,
		event->x, event->y, &prev_x, &prev_y);

	switch (event->direction)
	{
	case GDK_SCROLL_UP:
		ld_canvas_set_zoom (self, self->priv->zoom * ZOOM_WHEEL_STEP);
		break;
	case GDK_SCROLL_DOWN:
		ld_canvas_set_zoom (self, self->priv->zoom / ZOOM_WHEEL_STEP);
		break;
	default:
		return FALSE;
	}

	ld_canvas_widget_to_diagram_coords (self,
		event->x, event->y, &new_x, &new_y);

	/* Focus on the point under the cursor. */
	self->priv->x += prev_x - new_x;
	self->priv->y += prev_y - new_y;

	check_terminals (self, event->x, event->y);
	return TRUE;
}

static gboolean
on_expose_event (GtkWidget *widget, GdkEventExpose *event, gpointer user_data)
{
	DrawData data;

	data.cr = gdk_cairo_create (widget->window);
	data.self = LD_CANVAS (widget);
	data.scale = ld_canvas_get_scale_in_px (data.self);
	data.exposed_rect.x = event->area.x;
	data.exposed_rect.y = event->area.y;
	data.exposed_rect.width = event->area.width;
	data.exposed_rect.height = event->area.height;

	gdk_cairo_rectangle (data.cr, &event->area);
	cairo_clip (data.cr);

	ld_canvas_color_apply (COLOR_GET (data.self, COLOR_BASE), data.cr);
	cairo_paint (data.cr);

	draw_grid (widget, &data);
	draw_diagram (widget, &data);
	draw_terminal (widget, &data);

	cairo_destroy (data.cr);
	return FALSE;
}

static void
draw_grid (GtkWidget *widget, DrawData *data)
{
	gdouble grid_step;
	gdouble x_init, y_init;
	gdouble x, y;

	grid_step = data->scale;
	while (grid_step < 5)
		grid_step *= 5;

	ld_canvas_color_apply (COLOR_GET (data->self, COLOR_GRID), data->cr);
	cairo_set_line_width (data->cr, 1);
	cairo_set_line_cap (data->cr, CAIRO_LINE_CAP_ROUND);

	/* Get coordinates of the top-left point. */
	ld_canvas_widget_to_diagram_coords (data->self,
		data->exposed_rect.x, data->exposed_rect.y, &x_init, &y_init);
	ld_canvas_diagram_to_widget_coords (data->self,
		ceil (x_init), ceil (y_init), &x_init, &y_init);

	/* Iterate over all the points. */
	for     (x = x_init; x <= data->exposed_rect.x + data->exposed_rect.width;
			 x += grid_step)
	{
		for (y = y_init; y <= data->exposed_rect.y + data->exposed_rect.height;
			 y += grid_step)
		{
			cairo_move_to (data->cr, x, y);
			cairo_line_to (data->cr, x, y);
		}
	}
	cairo_stroke (data->cr);
}

static void
draw_terminal (GtkWidget *widget, DrawData *data)
{
	LdCanvasPrivate *priv;

	priv = data->self->priv;
	if (!priv->terminal_highlighted)
		return;

	ld_canvas_color_apply (COLOR_GET (data->self, COLOR_TERMINAL), data->cr);
	cairo_set_line_width (data->cr, 1);

	cairo_new_path (data->cr);
	cairo_arc (data->cr, priv->terminal.x, priv->terminal.y,
		TERMINAL_RADIUS, 0, 2 * G_PI);
	cairo_stroke (data->cr);
}

static void
draw_diagram (GtkWidget *widget, DrawData *data)
{
	GList *objects, *iter;

	if (!data->self->priv->diagram)
		return;

	cairo_save (data->cr);
	cairo_set_line_width (data->cr, 1 / data->scale);

	/* Draw objects from the diagram, from bottom to top. */
	objects = (GList *) ld_diagram_get_objects (data->self->priv->diagram);
	for (iter = objects; iter; iter = g_list_next (iter))
		draw_object (LD_DIAGRAM_OBJECT (iter->data), data);

	switch (data->self->priv->operation)
	{
		AddObjectData *op_data;

	case OPER_ADD_OBJECT:
		op_data = &OPER_DATA (data->self, add_object);
		if (op_data->visible)
			draw_object (op_data->object, data);
		break;
	}

	cairo_restore (data->cr);
}

static void
draw_object (LdDiagramObject *diagram_object, DrawData *data)
{
	g_return_if_fail (LD_IS_DIAGRAM_OBJECT (diagram_object));
	g_return_if_fail (data != NULL);

	if (is_object_selected (data->self, diagram_object))
		ld_canvas_color_apply (COLOR_GET (data->self,
			COLOR_SELECTION), data->cr);
	else
		ld_canvas_color_apply (COLOR_GET (data->self,
			COLOR_OBJECT), data->cr);

	if (LD_IS_DIAGRAM_SYMBOL (diagram_object))
		draw_symbol (LD_DIAGRAM_SYMBOL (diagram_object), data);
}

static void
draw_symbol (LdDiagramSymbol *diagram_symbol, DrawData *data)
{
	LdSymbol *symbol;
	LdRectangle clip_rect;
	gdouble x, y;

	symbol = resolve_diagram_symbol (data->self, diagram_symbol);

	/* TODO: Resolve this better; draw a cross or whatever. */
	if (!symbol)
	{
		g_warning ("Cannot find symbol %s in the library.",
			ld_diagram_symbol_get_class (diagram_symbol));
		return;
	}

	if (!get_symbol_clip_area (data->self, diagram_symbol, &clip_rect)
		|| !ld_rectangle_intersects (&clip_rect, &data->exposed_rect))
		return;

	cairo_save (data->cr);

	cairo_rectangle (data->cr, clip_rect.x, clip_rect.y,
		clip_rect.width, clip_rect.height);
	cairo_clip (data->cr);

	/* TODO: Rotate the space for other orientations. */
	ld_canvas_diagram_to_widget_coords (data->self,
		ld_diagram_object_get_x (LD_DIAGRAM_OBJECT (diagram_symbol)),
		ld_diagram_object_get_y (LD_DIAGRAM_OBJECT (diagram_symbol)),
		&x, &y);
	cairo_translate (data->cr, x, y);
	cairo_scale (data->cr, data->scale, data->scale);
	ld_symbol_draw (symbol, data->cr);

	cairo_restore (data->cr);
}