Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/goocanvas/src/goocanvas.c
blob: 2eb41b37bd97ad3770fb22a70ade44f1844e1043 (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
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
/*
 * GooCanvas. Copyright (C) 2005 Damon Chaplin.
 * Released under the GNU LGPL license. See COPYING for details.
 *
 * goocanvas.c - the main canvas widget.
 */

/**
 * SECTION: goocanvas
 * @Title: GooCanvas
 * @Short_Description: the main canvas widget.
 *
 * #GooCanvas is the main widget containing a number of canvas items.
 *
 * Here is a simple example:
 *
 * <informalexample><programlisting>
 *  &num;include &lt;goocanvas.h&gt;
 *  
 *  static gboolean on_rect_button_press (GooCanvasItem  *view,
 *                                        GooCanvasItem  *target,
 *                                        GdkEventButton *event,
 *                                        gpointer        data);
 *  
 *  int
 *  main (int argc, char *argv[])
 *  {
 *    GtkWidget *window, *scrolled_win, *canvas;
 *    GooCanvasItem *root, *rect_item, *text_item;
 *  
 *    /&ast; Initialize GTK+. &ast;/
 *    gtk_set_locale&nbsp;();
 *    gtk_init (&amp;argc, &amp;argv);
 *  
 *    /&ast; Create the window and widgets. &ast;/
 *    window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
 *    gtk_window_set_default_size (GTK_WINDOW (window), 640, 600);
 *    gtk_widget_show (window);
 *    g_signal_connect (window, "delete_event", (GtkSignalFunc) on_delete_event,
 *                      NULL);
 *  
 *    scrolled_win = gtk_scrolled_window_new (NULL, NULL);
 *    gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win),
 *                                         GTK_SHADOW_IN);
 *    gtk_widget_show (scrolled_win);
 *    gtk_container_add (GTK_CONTAINER (window), scrolled_win);
 *  
 *    canvas = goo_canvas_new&nbsp;();
 *    gtk_widget_set_size_request (canvas, 600, 450);
 *    goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, 1000, 1000);
 *    gtk_widget_show (canvas);
 *    gtk_container_add (GTK_CONTAINER (scrolled_win), canvas);
 *  
 *    root = goo_canvas_get_root_item (GOO_CANVAS (canvas));
 *  
 *    /&ast; Add a few simple items. &ast;/
 *    rect_item = goo_canvas_rect_new (root, 100, 100, 400, 400,
 *                                     "line-width", 10.0,
 *                                     "radius-x", 20.0,
 *                                     "radius-y", 10.0,
 *                                     "stroke-color", "yellow",
 *                                     "fill-color", "red",
 *                                     NULL);
 *  
 *    text_item = goo_canvas_text_new (root, "Hello World", 300, 300, -1,
 *                                     GTK_ANCHOR_CENTER,
 *                                     "font", "Sans 24",
 *                                     NULL);
 *    goo_canvas_item_rotate (text_item, 45, 300, 300);
 *  
 *    /&ast; Connect a signal handler for the rectangle item. &ast;/
 *    g_signal_connect (rect_item, "button_press_event",
 *                      (GtkSignalFunc) on_rect_button_press, NULL);
 *  
 *    /&ast; Pass control to the GTK+ main event loop. &ast;/
 *    gtk_main&nbsp;();
 *  
 *    return 0;
 *  }
 *  
 *  
 *  /&ast; This handles button presses in item views. We simply output a message to
 *     the console. &ast;/
 *  static gboolean
 *  on_rect_button_press (GooCanvasItem  *item,
 *                        GooCanvasItem  *target,
 *                        GdkEventButton *event,
 *                        gpointer        data)
 *  {
 *    g_print ("rect item received button press event\n");
 *    return TRUE;
 *  }
 *  
 * </programlisting></informalexample>
 */
#include <config.h>
#include <math.h>
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include "goocanvasatk.h"
#include "goocanvas.h"
#include "goocanvasitemmodel.h"
#include "goocanvasitem.h"
#include "goocanvasgroup.h"
#include "goocanvasmarshal.h"


enum {
  PROP_0,

  PROP_SCALE,
  PROP_SCALE_X,
  PROP_SCALE_Y,
  PROP_ANCHOR,
  PROP_X1,
  PROP_Y1,
  PROP_X2,
  PROP_Y2,
  PROP_AUTOMATIC_BOUNDS,
  PROP_BOUNDS_FROM_ORIGIN,
  PROP_BOUNDS_PADDING,
  PROP_UNITS,
  PROP_RESOLUTION_X,
  PROP_RESOLUTION_Y,
  PROP_BACKGROUND_COLOR,
  PROP_BACKGROUND_COLOR_RGB,
  PROP_INTEGER_LAYOUT, 
  PROP_CLEAR_BACKGROUND
};

enum {
  ITEM_CREATED,

  LAST_SIGNAL
};

static guint canvas_signals[LAST_SIGNAL] = { 0 };

static void     goo_canvas_dispose	   (GObject          *object);
static void     goo_canvas_finalize	   (GObject          *object);
static void     goo_canvas_realize         (GtkWidget        *widget);
static void     goo_canvas_unrealize	   (GtkWidget	     *widget);
static void     goo_canvas_map		   (GtkWidget	     *widget);
static void     goo_canvas_style_set	   (GtkWidget	     *widget,
					    GtkStyle	     *old_style);
static void     goo_canvas_size_request    (GtkWidget        *widget,
					    GtkRequisition   *requisition);
static void     goo_canvas_size_allocate   (GtkWidget        *widget,
					    GtkAllocation    *allocation);
static void     goo_canvas_set_adjustments (GooCanvas        *canvas,
					    GtkAdjustment    *hadj,
					    GtkAdjustment    *vadj);
static gboolean goo_canvas_expose_event	   (GtkWidget        *widget,
					    GdkEventExpose   *event);
static gboolean goo_canvas_button_press    (GtkWidget        *widget,
					    GdkEventButton   *event);
static gboolean goo_canvas_button_release  (GtkWidget        *widget,
					    GdkEventButton   *event);
static gboolean goo_canvas_motion          (GtkWidget        *widget,
					    GdkEventMotion   *event);
static gboolean goo_canvas_scroll          (GtkWidget        *widget,
					    GdkEventScroll   *event);
static gboolean goo_canvas_focus	   (GtkWidget        *widget,
					    GtkDirectionType  direction);
static gboolean goo_canvas_key_press       (GtkWidget        *widget,
					    GdkEventKey      *event);
static gboolean goo_canvas_key_release     (GtkWidget        *widget,
					    GdkEventKey      *event);
static gboolean goo_canvas_crossing        (GtkWidget        *widget,
					    GdkEventCrossing *event);
static gboolean goo_canvas_focus_in        (GtkWidget        *widget,
					    GdkEventFocus    *event);
static gboolean goo_canvas_focus_out       (GtkWidget        *widget,
					    GdkEventFocus    *event);
static gboolean goo_canvas_grab_broken     (GtkWidget        *widget,
					    GdkEventGrabBroken *event);
static void     goo_canvas_get_property    (GObject          *object,
					    guint             prop_id,
					    GValue           *value,
					    GParamSpec       *pspec);
static void     goo_canvas_set_property    (GObject          *object,
					    guint             prop_id,
					    const GValue     *value,
					    GParamSpec       *pspec);
static void     goo_canvas_remove          (GtkContainer     *container,
					    GtkWidget        *widget);
static void     goo_canvas_forall          (GtkContainer     *container,
					    gboolean          include_internals,
					    GtkCallback       callback,
					    gpointer          callback_data);

static void	goo_canvas_set_scale_internal (GooCanvas     *canvas,
					       gdouble        scale_x,
					       gdouble        scale_y);

static void     set_item_pointer           (GooCanvasItem   **item,
					    GooCanvasItem    *new_item);
static void     update_pointer_item        (GooCanvas        *canvas,
					    GdkEvent         *event);
static void     reconfigure_canvas	   (GooCanvas        *canvas,
					    gboolean          redraw_if_needed);
static void	goo_canvas_update_automatic_bounds (GooCanvas       *canvas);


G_DEFINE_TYPE (GooCanvas, goo_canvas, GTK_TYPE_CONTAINER)

/* This evaluates to TRUE if an item is still in the canvas. */
#define ITEM_IS_VALID(item) (goo_canvas_item_get_canvas (item))

#define GOO_CANVAS_DEFAULT_WIDTH	1000.0
#define GOO_CANVAS_DEFAULT_HEIGHT	1000.0

static void
goo_canvas_class_init (GooCanvasClass *klass)
{
  GObjectClass *gobject_class = (GObjectClass*) klass;
  GtkWidgetClass *widget_class = (GtkWidgetClass*) klass;
  GtkContainerClass *container_class = (GtkContainerClass*) klass;

  gobject_class->dispose	     = goo_canvas_dispose;
  gobject_class->finalize	     = goo_canvas_finalize;
  gobject_class->get_property	     = goo_canvas_get_property;
  gobject_class->set_property	     = goo_canvas_set_property;

  widget_class->realize              = goo_canvas_realize;
  widget_class->unrealize            = goo_canvas_unrealize;
  widget_class->map                  = goo_canvas_map;
  widget_class->size_request         = goo_canvas_size_request;
  widget_class->size_allocate        = goo_canvas_size_allocate;
  widget_class->style_set            = goo_canvas_style_set;
  widget_class->expose_event         = goo_canvas_expose_event;
  widget_class->button_press_event   = goo_canvas_button_press;
  widget_class->button_release_event = goo_canvas_button_release;
  widget_class->motion_notify_event  = goo_canvas_motion;
  widget_class->scroll_event         = goo_canvas_scroll;
  widget_class->focus                = goo_canvas_focus;
  widget_class->key_press_event      = goo_canvas_key_press;
  widget_class->key_release_event    = goo_canvas_key_release;
  widget_class->enter_notify_event   = goo_canvas_crossing;
  widget_class->leave_notify_event   = goo_canvas_crossing;
  widget_class->focus_in_event       = goo_canvas_focus_in;
  widget_class->focus_out_event      = goo_canvas_focus_out;
  widget_class->grab_broken_event    = goo_canvas_grab_broken;

  container_class->remove	     = goo_canvas_remove;
  container_class->forall            = goo_canvas_forall;

  klass->set_scroll_adjustments      = goo_canvas_set_adjustments;

  /* Register our accessible factory, but only if accessibility is enabled. */
  if (!ATK_IS_NO_OP_OBJECT_FACTORY (atk_registry_get_factory (atk_get_default_registry (), GTK_TYPE_WIDGET)))
    {
      atk_registry_set_factory_type (atk_get_default_registry (),
				     GOO_TYPE_CANVAS,
				     goo_canvas_accessible_factory_get_type ());
    }

  g_object_class_install_property (gobject_class, PROP_SCALE,
				   g_param_spec_double ("scale",
							_("Scale"),
							_("The magnification factor of the canvas"),
							0.0, G_MAXDOUBLE, 1.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_SCALE_X,
				   g_param_spec_double ("scale-x",
							_("Scale X"),
							_("The horizontal magnification factor of the canvas"),
							0.0, G_MAXDOUBLE, 1.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_SCALE_Y,
				   g_param_spec_double ("scale-y",
							_("Scale Y"),
							_("The vertical magnification factor of the canvas"),
							0.0, G_MAXDOUBLE, 1.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_ANCHOR,
				   g_param_spec_enum ("anchor",
						      _("Anchor"),
						      _("Where to place the canvas when it is smaller than the widget's allocated area"),
						      GTK_TYPE_ANCHOR_TYPE,
						      GTK_ANCHOR_NW,
						      G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_X1,
				   g_param_spec_double ("x1",
							_("X1"),
							_("The x coordinate of the left edge of the canvas bounds, in canvas units"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE, 0.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_Y1,
				   g_param_spec_double ("y1",
							_("Y1"),
							_("The y coordinate of the top edge of the canvas bounds, in canvas units"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE, 0.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_X2,
				   g_param_spec_double ("x2",
							_("X2"),
							_("The x coordinate of the right edge of the canvas bounds, in canvas units"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							GOO_CANVAS_DEFAULT_WIDTH,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_Y2,
				   g_param_spec_double ("y2",
							_("Y2"),
							_("The y coordinate of the bottom edge of the canvas bounds, in canvas units"),
							-G_MAXDOUBLE,
							G_MAXDOUBLE,
							GOO_CANVAS_DEFAULT_HEIGHT,
							G_PARAM_READWRITE));


  g_object_class_install_property (gobject_class, PROP_AUTOMATIC_BOUNDS,
                                   g_param_spec_boolean ("automatic-bounds",
							 _("Automatic Bounds"),
							 _("If the bounds are automatically calculated based on the bounds of all the items in the canvas"),
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_BOUNDS_FROM_ORIGIN,
                                   g_param_spec_boolean ("bounds-from-origin",
							 _("Bounds From Origin"),
							 _("If the automatic bounds are calculated from the origin"),
							 TRUE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_BOUNDS_PADDING,
				   g_param_spec_double ("bounds-padding",
							_("Bounds Padding"),
							_("The padding added to the automatic bounds"),
							0.0, G_MAXDOUBLE, 0.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_UNITS,
				   g_param_spec_enum ("units",
						      _("Units"),
						      _("The units to use for the canvas"),
						      GTK_TYPE_UNIT,
						      GTK_UNIT_PIXEL,
						      G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_RESOLUTION_X,
				   g_param_spec_double ("resolution-x",
							_("Resolution X"),
							_("The horizontal resolution of the display, in dots per inch"),
							0.0, G_MAXDOUBLE,
							96.0,
							G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_RESOLUTION_Y,
				   g_param_spec_double ("resolution-y",
							_("Resolution Y"),
							_("The vertical resolution of the display, in dots per inch"),
							0.0, G_MAXDOUBLE,
							96.0,
							G_PARAM_READWRITE));

  /* Convenience properties - writable only. */
  g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR,
				   g_param_spec_string ("background-color",
							_("Background Color"),
							_("The color to use for the canvas background"),
							NULL,
							G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class, PROP_BACKGROUND_COLOR_RGB,
				   g_param_spec_uint ("background-color-rgb",
						      _("Background Color RGB"),
						      _("The color to use for the canvas background, specified as a 24-bit integer value, 0xRRGGBB"),
						      0, G_MAXUINT, 0,
						      G_PARAM_WRITABLE));

  g_object_class_install_property (gobject_class, PROP_INTEGER_LAYOUT,
                                   g_param_spec_boolean ("integer-layout",
							 _("Integer Layout"),
							 _("If all item layout is done to the nearest integer"),
							 FALSE,
							 G_PARAM_READWRITE));

  g_object_class_install_property (gobject_class, PROP_CLEAR_BACKGROUND, 
                                   g_param_spec_boolean ("clear-background",
							 _("Clear Background"),
							 _("If the background is cleared before the canvas is painted"),
							 TRUE,
							 G_PARAM_READWRITE));

  /**
   * GooCanvas::set-scroll-adjustments
   * @canvas: the canvas.
   * @hadjustment: the horizontal adjustment.
   * @vadjustment: the vertical adjustment.
   *
   * This is used when the #GooCanvas is placed inside a #GtkScrolledWindow,
   * to connect up the adjustments so scrolling works properly.
   *
   * It isn't useful for applications.
   */
  widget_class->set_scroll_adjustments_signal =
    g_signal_new ("set_scroll_adjustments",
		  G_OBJECT_CLASS_TYPE (gobject_class),
		  G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
		  G_STRUCT_OFFSET (GooCanvasClass, set_scroll_adjustments),
		  NULL, NULL,
		  goo_canvas_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2,
		  GTK_TYPE_ADJUSTMENT,
		  GTK_TYPE_ADJUSTMENT);

  /* Signals. */

  /**
   * GooCanvas::item-created
   * @canvas: the canvas.
   * @item: the new item.
   * @model: the item's model.
   *
   * This is emitted when a new canvas item is created, in model/view mode.
   *
   * Applications can set up signal handlers for the new items here.
   */
  canvas_signals[ITEM_CREATED] =
    g_signal_new ("item-created",
		  G_TYPE_FROM_CLASS (gobject_class),
		  G_SIGNAL_RUN_LAST,
		  G_STRUCT_OFFSET (GooCanvasClass, item_created),
		  NULL, NULL,
		  goo_canvas_marshal_VOID__OBJECT_OBJECT,
		  G_TYPE_NONE, 2,
		  GOO_TYPE_CANVAS_ITEM,
		  GOO_TYPE_CANVAS_ITEM_MODEL);
}


static void
goo_canvas_init (GooCanvas *canvas)
{
  /* We set GTK_CAN_FOCUS by default, so it works as people expect.
     Though developers can turn this off if not needed for efficiency. */
  GTK_WIDGET_SET_FLAGS (canvas, GTK_CAN_FOCUS);

  canvas->scale_x = 1.0;
  canvas->scale_y = 1.0;
  canvas->scale = 1.0;
  canvas->need_update = TRUE;
  canvas->need_entire_subtree_update = TRUE;
  canvas->crossing_event.type = GDK_LEAVE_NOTIFY;
  canvas->anchor = GTK_ANCHOR_NORTH_WEST;
  canvas->clear_background = TRUE;

  /* Set the default bounds to a reasonable size. */
  canvas->bounds.x1 = 0.0;
  canvas->bounds.y1 = 0.0;
  canvas->bounds.x2 = GOO_CANVAS_DEFAULT_WIDTH;
  canvas->bounds.y2 = GOO_CANVAS_DEFAULT_HEIGHT;
  canvas->automatic_bounds = FALSE;
  canvas->bounds_from_origin = TRUE;
  canvas->bounds_padding = 0.0;

  canvas->units = GTK_UNIT_PIXEL;
  canvas->resolution_x = 96.0;
  canvas->resolution_y = 96.0;

  /* Create our own adjustments, in case we aren't inserted into a scrolled
     window. The accessibility code needs these. */
  canvas->hadjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
							    0.0, 0.0, 0.0));
  canvas->vadjustment = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0,
							    0.0, 0.0, 0.0));

  g_object_ref_sink (canvas->hadjustment);
  g_object_ref_sink (canvas->vadjustment);

  canvas->model_to_item = g_hash_table_new (g_direct_hash, g_direct_equal);

  /* Use a simple group as the default root item, which is fine 99% of the
     time. Apps can set their own root item if required. */
  canvas->root_item = goo_canvas_group_new (NULL, NULL);
  goo_canvas_item_set_canvas (canvas->root_item, canvas);
}


/**
 * goo_canvas_new:
 * 
 * Creates a new #GooCanvas widget.
 *
 * A #GooCanvasGroup is created automatically as the root item of the canvas,
 * though this can be overriden with goo_canvas_set_root_item() or
 * goo_canvas_set_root_item_model().
 * 
 * Returns: a new #GooCanvas widget.
 **/
GtkWidget*
goo_canvas_new (void)
{
  return GTK_WIDGET (g_object_new (GOO_TYPE_CANVAS, NULL));
}

static void
goo_canvas_dispose (GObject *object)
{
  GooCanvas *canvas = (GooCanvas*) object;

  if (canvas->model_to_item)
    {
      g_hash_table_destroy (canvas->model_to_item);
      canvas->model_to_item = NULL;
    }

  if (canvas->root_item)
    {
      g_object_unref (canvas->root_item);
      canvas->root_item = NULL;
    }

  if (canvas->root_item_model)
    {
      g_object_unref (canvas->root_item_model);
      canvas->root_item_model = NULL;
    }

  if (canvas->idle_id)
    {
      g_source_remove (canvas->idle_id);
      canvas->idle_id = 0;
    }

  /* Release any references we hold to items. */
  set_item_pointer (&canvas->pointer_item, NULL);
  set_item_pointer (&canvas->pointer_grab_item, NULL);
  set_item_pointer (&canvas->pointer_grab_initial_item, NULL);
  set_item_pointer (&canvas->focused_item, NULL);
  set_item_pointer (&canvas->keyboard_grab_item, NULL);

  if (canvas->hadjustment)
    {
      g_object_unref (canvas->hadjustment);
      canvas->hadjustment = NULL;
    }

  if (canvas->vadjustment)
    {
      g_object_unref (canvas->vadjustment);
      canvas->vadjustment = NULL;
    }

  G_OBJECT_CLASS (goo_canvas_parent_class)->dispose (object);
}


static void
goo_canvas_finalize (GObject *object)
{
  /*GooCanvas *canvas = (GooCanvas*) object;*/

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


/**
 * goo_canvas_get_default_line_width:
 * @canvas: a #GooCanvas.
 * 
 * Gets the default line width, which depends on the current units setting.
 * 
 * Returns: the default line width of the canvas.
 **/
gdouble
goo_canvas_get_default_line_width (GooCanvas *canvas)
{
  gdouble line_width = 2.0;

  if (!canvas)
    return 2.0;

  /* We use the same default as cairo when using pixels, i.e. 2 pixels.
     For other units we use 2 points, or thereabouts. */
  switch (canvas->units)
    {
    case GTK_UNIT_PIXEL:
      line_width = 2.0;
      break;
    case GTK_UNIT_POINTS:
      line_width = 2.0;
      break;
    case GTK_UNIT_INCH:
      line_width = 2.0 / 72.0;
      break;
    case GTK_UNIT_MM:
      line_width = 0.7;
      break;
    }

  return line_width;
}


/**
 * goo_canvas_create_cairo_context:
 * @canvas: a #GooCanvas.
 * 
 * Creates a cairo context, initialized with the default canvas settings.
 * 
 * Returns: a new cairo context. It should be freed with cairo_destroy().
 **/
cairo_t*
goo_canvas_create_cairo_context (GooCanvas *canvas)
{
  cairo_t *cr;
  cairo_surface_t *surface;

  /* If the canvas is realized we can use the GDK function to create a cairo
     context for the canvas window. Otherwise we create a small temporary
     image surface. */
  if (canvas && canvas->canvas_window)
    {
      cr = gdk_cairo_create (canvas->canvas_window);
    }
  else
    {
      surface = cairo_image_surface_create (CAIRO_FORMAT_ARGB32, 1, 1);
      cr = cairo_create (surface);
      /* The cairo context will keep a reference to the surface so we can
	 drop our reference. */
      cairo_surface_destroy (surface);
    }

  /* We use CAIRO_ANTIALIAS_GRAY as the default antialiasing mode, as that is
     what is recommended when using unhinted text. */
  cairo_set_antialias (cr, CAIRO_ANTIALIAS_GRAY);

  /* Set the default line width based on the current units setting. */
  cairo_set_line_width (cr, goo_canvas_get_default_line_width (canvas));

  return cr;
}


static void
goo_canvas_get_property    (GObject            *object,
			    guint               prop_id,
			    GValue             *value,
			    GParamSpec         *pspec)
{
  GooCanvas *canvas = (GooCanvas*) object;

  switch (prop_id)
    {
    case PROP_SCALE:
      g_value_set_double (value, canvas->scale);
      break;
    case PROP_SCALE_X:
      g_value_set_double (value, canvas->scale_x);
      break;
    case PROP_SCALE_Y:
      g_value_set_double (value, canvas->scale_y);
      break;
    case PROP_ANCHOR:
      g_value_set_enum (value, canvas->anchor);
      break;
    case PROP_X1:
      g_value_set_double (value, canvas->bounds.x1);
      break;
    case PROP_Y1:
      g_value_set_double (value, canvas->bounds.y1);
      break;
    case PROP_X2:
      g_value_set_double (value, canvas->bounds.x2);
      break;
    case PROP_Y2:
      g_value_set_double (value, canvas->bounds.y2);
      break;
    case PROP_AUTOMATIC_BOUNDS:
      g_value_set_boolean (value, canvas->automatic_bounds);
      break;
    case PROP_BOUNDS_FROM_ORIGIN:
      g_value_set_boolean (value, canvas->bounds_from_origin);
      break;
    case PROP_BOUNDS_PADDING:
      g_value_set_double (value, canvas->bounds_padding);
      break;
    case PROP_UNITS:
      g_value_set_enum (value, canvas->units);
      break;
    case PROP_RESOLUTION_X:
      g_value_set_double (value, canvas->resolution_x);
      break;
    case PROP_RESOLUTION_Y:
      g_value_set_double (value, canvas->resolution_y);
      break;
    case PROP_INTEGER_LAYOUT:
      g_value_set_boolean (value, canvas->integer_layout);
      break;
    case PROP_CLEAR_BACKGROUND:
      g_value_set_boolean (value, canvas->clear_background);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }
}


static void
goo_canvas_set_property    (GObject            *object,
			    guint               prop_id,
			    const GValue       *value,
			    GParamSpec         *pspec)
{
  GooCanvas *canvas = (GooCanvas*) object;
  GdkColor color = { 0, 0, 0, 0, };
  gboolean need_reconfigure = FALSE;
  gboolean need_update_automatic_bounds = FALSE;
  guint rgb;

  switch (prop_id)
    {
    case PROP_SCALE:
      goo_canvas_set_scale (canvas, g_value_get_double (value));
      break;
    case PROP_SCALE_X:
      goo_canvas_set_scale_internal (canvas, g_value_get_double (value),
				     canvas->scale_y);
      break;
    case PROP_SCALE_Y:
      goo_canvas_set_scale_internal (canvas, canvas->scale_x,
				     g_value_get_double (value));
      break;
    case PROP_ANCHOR:
      canvas->anchor = g_value_get_enum (value);
      need_reconfigure = TRUE;
      break;
    case PROP_X1:
      canvas->bounds.x1 = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_Y1:
      canvas->bounds.y1 = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_X2:
      canvas->bounds.x2 = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_Y2:
      canvas->bounds.y2 = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_AUTOMATIC_BOUNDS:
      canvas->automatic_bounds = g_value_get_boolean (value);
      if (canvas->automatic_bounds)
	need_update_automatic_bounds = TRUE;
      break;
    case PROP_BOUNDS_FROM_ORIGIN:
      canvas->bounds_from_origin = g_value_get_boolean (value);
      if (canvas->automatic_bounds)
	need_update_automatic_bounds = TRUE;
      break;
    case PROP_BOUNDS_PADDING:
      canvas->bounds_padding = g_value_get_double (value);
      if (canvas->automatic_bounds)
	need_update_automatic_bounds = TRUE;
      break;
    case PROP_UNITS:
      canvas->units = g_value_get_enum (value);
      need_reconfigure = TRUE;
      break;
    case PROP_RESOLUTION_X:
      canvas->resolution_x = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_RESOLUTION_Y:
      canvas->resolution_y = g_value_get_double (value);
      need_reconfigure = TRUE;
      break;
    case PROP_BACKGROUND_COLOR:
      if (!g_value_get_string (value))
	gtk_widget_modify_base ((GtkWidget*) canvas, GTK_STATE_NORMAL, NULL);
      else if (gdk_color_parse (g_value_get_string (value), &color))
	gtk_widget_modify_base ((GtkWidget*) canvas, GTK_STATE_NORMAL, &color);
      else
	g_warning ("Unknown color: %s", g_value_get_string (value));
      break;
    case PROP_BACKGROUND_COLOR_RGB:
      rgb = g_value_get_uint (value);
      color.red   = ((rgb >> 16) & 0xFF) * 257;
      color.green = ((rgb >> 8)  & 0xFF) * 257;
      color.blue  = ((rgb)       & 0xFF) * 257;
      gtk_widget_modify_base ((GtkWidget*) canvas, GTK_STATE_NORMAL, &color);
      break;
    case PROP_INTEGER_LAYOUT:
      canvas->integer_layout = g_value_get_boolean (value);
      canvas->need_entire_subtree_update = TRUE;
      goo_canvas_request_update (canvas);
      break;
    case PROP_CLEAR_BACKGROUND:
      canvas->clear_background = g_value_get_boolean (value);
      break;

    default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
      break;
  }

  if (need_update_automatic_bounds)
    {
      goo_canvas_update_automatic_bounds (canvas);
    }

  if (need_reconfigure)
    {
      reconfigure_canvas (canvas, FALSE);
      gtk_widget_queue_draw (GTK_WIDGET (canvas));
    }
}


/**
 * goo_canvas_get_root_item_model:
 * @canvas: a #GooCanvas.
 * 
 * Gets the root item model of the canvas.
 * 
 * Returns: the root item model, or %NULL if there is no root item model.
 **/
GooCanvasItemModel*
goo_canvas_get_root_item_model (GooCanvas	*canvas)
{
  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);

  return canvas->root_item_model;
}


/**
 * goo_canvas_set_root_item_model:
 * @canvas: a #GooCanvas.
 * @model: a #GooCanvasItemModel.
 * 
 * Sets the root item model of the canvas.
 *
 * A hierarchy of canvas items will be created, corresponding to the hierarchy
 * of items in the model. Any current canvas items will be removed.
 **/
void
goo_canvas_set_root_item_model (GooCanvas          *canvas,
				GooCanvasItemModel *model)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_ITEM_MODEL (model));

  if (canvas->root_item_model == model)
    return;

  if (canvas->root_item_model)
    {
      g_object_unref (canvas->root_item_model);
      canvas->root_item_model = NULL;
    }

  if (canvas->root_item)
    {
      g_object_unref (canvas->root_item);
      canvas->root_item = NULL;
    }

  if (model)
    {
      canvas->root_item_model = g_object_ref (model);

      /* Create a hierarchy of canvas items for all the items in the model. */
      canvas->root_item = goo_canvas_create_item (canvas, model);
    }
  else
    {
      /* The model has been reset so we go back to a default root group. */
      canvas->root_item = goo_canvas_group_new (NULL, NULL);
    }

  goo_canvas_item_set_canvas (canvas->root_item, canvas);
  canvas->need_update = TRUE;

  if (GTK_WIDGET_REALIZED (canvas))
    goo_canvas_update (canvas);

  gtk_widget_queue_draw (GTK_WIDGET (canvas));
}


/**
 * goo_canvas_get_root_item:
 * @canvas: a #GooCanvas.
 * 
 * Gets the root item of the canvas, usually a #GooCanvasGroup.
 * 
 * Returns: the root item, or %NULL if there is no root item.
 **/
GooCanvasItem*
goo_canvas_get_root_item (GooCanvas     *canvas)
{
  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);

  return canvas->root_item;
}


/**
 * goo_canvas_set_root_item:
 * @canvas: a #GooCanvas.
 * @item: the root canvas item.
 * 
 * Sets the root item of the canvas. Any existing canvas items are removed.
 **/
void
goo_canvas_set_root_item    (GooCanvas		*canvas,
			     GooCanvasItem      *item)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_ITEM (item));

  if (canvas->root_item == item)
    return;

  /* Remove any current model. */
  if (canvas->root_item_model)
    {
      g_object_unref (canvas->root_item_model);
      canvas->root_item_model = NULL;
    }

  if (canvas->root_item)
    g_object_unref (canvas->root_item);

  canvas->root_item = g_object_ref (item);
  goo_canvas_item_set_canvas (canvas->root_item, canvas);

  canvas->need_update = TRUE;

  if (GTK_WIDGET_REALIZED (canvas))
    goo_canvas_update (canvas);

  gtk_widget_queue_draw (GTK_WIDGET (canvas));
}


/**
 * goo_canvas_get_item:
 * @canvas: a #GooCanvas.
 * @model: a #GooCanvasItemModel.
 * 
 * Gets the canvas item associated with the given #GooCanvasItemModel.
 * This is only useful when goo_canvas_set_root_item_model() has been used to
 * set a model for the canvas.
 *
 * For simple applications you can use goo_canvas_get_item() to set up
 * signal handlers for your items, e.g.
 *
 * <informalexample><programlisting>
 *    item = goo_canvas_get_item (GOO_CANVAS (canvas), my_item);
 *    g_signal_connect (item, "button_press_event",
 *                      (GtkSignalFunc) on_my_item_button_press, NULL);
 * </programlisting></informalexample>
 *
 * More complex applications may want to use the #GooCanvas::item-created
 * signal to hook up their signal handlers.
 *
 * Returns: the canvas item corresponding to the given #GooCanvasItemModel,
 *  or %NULL if no canvas item has been created for it yet.
 **/
GooCanvasItem*
goo_canvas_get_item (GooCanvas          *canvas,
		     GooCanvasItemModel *model)
{
  GooCanvasItem *item = NULL;

  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);
  g_return_val_if_fail (GOO_IS_CANVAS_ITEM_MODEL (model), NULL);

  if (canvas->model_to_item)
    item = g_hash_table_lookup (canvas->model_to_item, model);

  /* If the item model has a canvas item check it is valid. */
  g_return_val_if_fail (!item || GOO_IS_CANVAS_ITEM (item), NULL);

  return item;
}


/**
 * goo_canvas_get_item_at:
 * @canvas: a #GooCanvas.
 * @x: the x coordinate of the point.
 * @y: the y coordinate of the point
 * @is_pointer_event: %TRUE if the "pointer-events" property of
 *  items should be used to determine which parts of the item are tested.
 * 
 * Gets the item at the given point.
 * 
 * Returns: the item found at the given point, or %NULL if no item was found.
 **/
GooCanvasItem*
goo_canvas_get_item_at (GooCanvas     *canvas,
			gdouble        x,
			gdouble        y,
			gboolean       is_pointer_event)
{
  cairo_t *cr;
  GooCanvasItem *result = NULL;
  GList *list;

  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);

  /* If no root item is set, just return NULL. */
  if (!canvas->root_item)
    return NULL;

  cr = goo_canvas_create_cairo_context (canvas);
  list = goo_canvas_item_get_items_at (canvas->root_item, x, y, cr,
				       is_pointer_event, TRUE, NULL);
  cairo_destroy (cr);

  /* We just return the top item in the list. */
  if (list)
    result = list->data;

  g_list_free (list);

  return result;
}


/**
 * goo_canvas_get_items_at:
 * @canvas: a #GooCanvas.
 * @x: the x coordinate of the point.
 * @y: the y coordinate of the point
 * @is_pointer_event: %TRUE if the "pointer-events" property of
 *  items should be used to determine which parts of the item are tested.
 * 
 * Gets all items at the given point.
 * 
 * Returns: a list of items found at the given point, with the top item at
 *  the start of the list, or %NULL if no items were found. The list must be
 *  freed with g_list_free().
 **/
GList*
goo_canvas_get_items_at (GooCanvas     *canvas,
			 gdouble        x,
			 gdouble        y,
			 gboolean       is_pointer_event)
{
  cairo_t *cr;
  GList *result;

  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);

  /* If no root item is set, just return NULL. */
  if (!canvas->root_item)
    return NULL;

  cr = goo_canvas_create_cairo_context (canvas);
  result = goo_canvas_item_get_items_at (canvas->root_item, x, y, cr,
					 is_pointer_event, TRUE, NULL);
  cairo_destroy (cr);

  return result;
}


static GList*
goo_canvas_get_items_in_area_recurse (GooCanvas		    *canvas,
				      GooCanvasItem         *item,
				      const GooCanvasBounds *area,
				      gboolean		     inside_area,
				      gboolean               allow_overlaps,
				      gboolean               include_containers,
				      GList                 *found_items)
{
  GooCanvasBounds bounds;
  gboolean completely_inside = FALSE, completely_outside = FALSE;
  gboolean is_container, add_item = FALSE;
  gint n_children, i;

  /* First check the item/container itself. */
  goo_canvas_item_get_bounds (item, &bounds);

  is_container = goo_canvas_item_is_container (item);

  if (bounds.x1 >= area->x1 && bounds.x2 <= area->x2
      && bounds.y1 >= area->y1 && bounds.y2 <= area->y2)
    completely_inside = TRUE;

  if (bounds.x1 > area->x2 || bounds.x2 < area->x1
      || bounds.y1 > area->y2 || bounds.y2 < area->y1)
    completely_outside = TRUE;

  if (inside_area)
    {
      if (completely_inside
	  || (allow_overlaps && !completely_outside))
	add_item = TRUE;
    }
  else
    {
      if (completely_outside
	  || (allow_overlaps && !completely_inside))
	add_item = TRUE;
    }

  if (add_item && (!is_container || include_containers))
    found_items = g_list_prepend (found_items, item);

  /* Now check any children, if appropriate. */
  if ((inside_area && !completely_outside)
      || (!inside_area && !completely_inside))
    {
      n_children = goo_canvas_item_get_n_children (item);
      for (i = 0; i < n_children; i++)
	{
	  GooCanvasItem *child = goo_canvas_item_get_child (item, i);
	  found_items = goo_canvas_get_items_in_area_recurse (canvas, child,
							      area,
							      inside_area,
							      allow_overlaps,
							      include_containers,
							      found_items);
	}
    }

  return found_items;
}


/**
 * goo_canvas_get_items_in_area:
 * @canvas: a #GooCanvas.
 * @area: the area to compare with each item's bounds.
 * @inside_area: %TRUE if items inside @area should be returned, or %FALSE if
 *  items outside @area should be returned.
 * @allow_overlaps: %TRUE if items which are partly inside and partly outside
 *  should be returned.
 * @include_containers: %TRUE if containers should be checked as well as
 *  normal items.
 * 
 * Gets a list of items inside or outside a given area.
 * 
 * Returns: a list of items in the given area, or %NULL if no items are found.
 *  The list should be freed with g_list_free().
 **/
GList*
goo_canvas_get_items_in_area (GooCanvas		    *canvas,
			      const GooCanvasBounds *area,
			      gboolean		     inside_area,
			      gboolean               allow_overlaps,
			      gboolean               include_containers)
{
  g_return_val_if_fail (GOO_IS_CANVAS (canvas), NULL);

  /* If no root item is set, just return NULL. */
  if (!canvas->root_item)
    return NULL;

  return goo_canvas_get_items_in_area_recurse (canvas, canvas->root_item,
					       area, inside_area,
					       allow_overlaps,
					       include_containers, NULL);
}


static void
goo_canvas_realize (GtkWidget *widget)
{
  GooCanvas *canvas;
  GdkWindowAttr attributes;
  gint attributes_mask;
  gint width_pixels, height_pixels;
  GList *tmp_list;

  g_return_if_fail (GOO_IS_CANVAS (widget));

  canvas = GOO_CANVAS (widget);
  GTK_WIDGET_SET_FLAGS (canvas, GTK_REALIZED);

  attributes.window_type = GDK_WINDOW_CHILD;
  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.wclass = GDK_INPUT_OUTPUT;
  attributes.visual = gtk_widget_get_visual (widget);
  attributes.colormap = gtk_widget_get_colormap (widget);
  attributes.event_mask = GDK_VISIBILITY_NOTIFY_MASK;

  attributes_mask = GDK_WA_X | GDK_WA_Y | GDK_WA_VISUAL | GDK_WA_COLORMAP;

  widget->window = gdk_window_new (gtk_widget_get_parent_window (widget),
				   &attributes, attributes_mask);
  gdk_window_set_user_data (widget->window, widget);

  /* We want to round the sizes up to the next pixel. */
  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->device_to_pixels_x) + 1;
  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->device_to_pixels_y) + 1;

  attributes.x = canvas->hadjustment ? - canvas->hadjustment->value : 0,
  attributes.y = canvas->vadjustment ? - canvas->vadjustment->value : 0;
  attributes.width = MAX (width_pixels, widget->allocation.width);
  attributes.height = MAX (height_pixels, widget->allocation.height);
  attributes.event_mask = GDK_EXPOSURE_MASK
			 | GDK_SCROLL_MASK
			 | GDK_BUTTON_PRESS_MASK
			 | GDK_BUTTON_RELEASE_MASK
			 | GDK_POINTER_MOTION_MASK
			 | GDK_POINTER_MOTION_HINT_MASK
			 | GDK_KEY_PRESS_MASK
			 | GDK_KEY_RELEASE_MASK
			 | GDK_ENTER_NOTIFY_MASK
			 | GDK_LEAVE_NOTIFY_MASK
			 | GDK_FOCUS_CHANGE_MASK
                         | gtk_widget_get_events (widget);

  canvas->canvas_window = gdk_window_new (widget->window,
					  &attributes, attributes_mask);
  gdk_window_set_user_data (canvas->canvas_window, widget);

  attributes.x = widget->allocation.x;
  attributes.y = widget->allocation.y;
  attributes.width = widget->allocation.width;
  attributes.height = widget->allocation.height;
  attributes.event_mask = 0;

  canvas->tmp_window = gdk_window_new (gtk_widget_get_parent_window (widget),
				       &attributes, attributes_mask);
  gdk_window_set_user_data (canvas->tmp_window, widget);

  widget->style = gtk_style_attach (widget->style, widget->window);

  /* Make sure the window backgrounds aren't set, to avoid flicker when
     scrolling (due to the delay between X clearing the background and
     GooCanvas painting it). */
  gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
  gdk_window_set_back_pixmap (canvas->canvas_window, NULL, FALSE);
  gdk_window_set_back_pixmap (canvas->tmp_window, NULL, FALSE);

  /* Set the parent window of all the child widget items. */
  tmp_list = canvas->widget_items;
  while (tmp_list)
    {
      GooCanvasWidget *witem = tmp_list->data;
      tmp_list = tmp_list->next;

      if (witem->widget)
	gtk_widget_set_parent_window (witem->widget, canvas->canvas_window);
    }

  goo_canvas_update (GOO_CANVAS (widget));
}


static void 
goo_canvas_unrealize (GtkWidget *widget)
{
  GooCanvas *canvas;

  g_return_if_fail (GOO_IS_CANVAS (widget));

  canvas = GOO_CANVAS (widget);

  gdk_window_set_user_data (canvas->canvas_window, NULL);
  gdk_window_destroy (canvas->canvas_window);
  canvas->canvas_window = NULL;

  gdk_window_set_user_data (canvas->tmp_window, NULL);
  gdk_window_destroy (canvas->tmp_window);
  canvas->tmp_window = NULL;

  if (GTK_WIDGET_CLASS (goo_canvas_parent_class)->unrealize)
    GTK_WIDGET_CLASS (goo_canvas_parent_class)->unrealize (widget);
}


static void 
goo_canvas_map (GtkWidget *widget)
{
  GooCanvas *canvas;
  GList *tmp_list;

  g_return_if_fail (GOO_IS_CANVAS (widget));

  canvas = GOO_CANVAS (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_MAPPED);

  tmp_list = canvas->widget_items;
  while (tmp_list)
    {
      GooCanvasWidget *witem = tmp_list->data;
      tmp_list = tmp_list->next;

      if (witem->widget && GTK_WIDGET_VISIBLE (witem->widget))
	{
	  if (!GTK_WIDGET_MAPPED (witem->widget))
	    gtk_widget_map (witem->widget);
	}
    }

  gdk_window_show (canvas->canvas_window);
  gdk_window_show (widget->window);
}


static void
goo_canvas_style_set (GtkWidget *widget,
		      GtkStyle  *old_style)
{
  if (GTK_WIDGET_CLASS (goo_canvas_parent_class)->style_set)
    GTK_WIDGET_CLASS (goo_canvas_parent_class)->style_set (widget, old_style);

  if (GTK_WIDGET_REALIZED (widget))
    {
      /* Make sure the window backgrounds aren't set, to avoid flicker when
	 scrolling (due to the delay between X clearing the background and
	 GooCanvas painting it). */
      gdk_window_set_back_pixmap (widget->window, NULL, FALSE);
      gdk_window_set_back_pixmap (GOO_CANVAS (widget)->canvas_window, NULL, FALSE);
    }
}


static void
goo_canvas_configure_hadjustment (GooCanvas *canvas,
				  gint       window_width)
{
  GtkWidget *widget = GTK_WIDGET (canvas);
  GtkAdjustment *adj = canvas->hadjustment;
  gboolean changed = FALSE;
  gboolean value_changed = FALSE;
  gdouble max_value;

  if (adj->upper != window_width)
    {
      adj->upper = window_width;
      changed = TRUE;
    }

  if (adj->page_size != widget->allocation.width)
    {
      adj->page_size = widget->allocation.width;
      adj->page_increment = adj->page_size * 0.9;
      adj->step_increment = adj->page_size * 0.1;
      changed = TRUE;
    }
      
  max_value = MAX (0.0, adj->upper - adj->page_size);
  if (adj->value > max_value)
    {
      adj->value = max_value;
      value_changed = TRUE;
    }
  
  if (changed)
    gtk_adjustment_changed (adj);

  if (value_changed)
    gtk_adjustment_value_changed (adj);
}


static void
goo_canvas_configure_vadjustment (GooCanvas *canvas,
				  gint       window_height)
{
  GtkWidget *widget = GTK_WIDGET (canvas);
  GtkAdjustment *adj = canvas->vadjustment;
  gboolean changed = FALSE;
  gboolean value_changed = FALSE;
  gdouble max_value;

  if (adj->upper != window_height)
    {
      adj->upper = window_height;
      changed = TRUE;
    }

  if (adj->page_size != widget->allocation.height)
    {
      adj->page_size = widget->allocation.height;
      adj->page_increment = adj->page_size * 0.9;
      adj->step_increment = adj->page_size * 0.1;
      changed = TRUE;
    }
      
  max_value = MAX (0.0, adj->upper - adj->page_size);
  if (adj->value > max_value)
    {
      adj->value = max_value;
      value_changed = TRUE;
    }
  
  if (changed)
    gtk_adjustment_changed (adj);

  if (value_changed)
    gtk_adjustment_value_changed (adj);
}


static void
recalculate_scales (GooCanvas *canvas)
{
  switch (canvas->units)
    {
    case GTK_UNIT_PIXEL:
      canvas->device_to_pixels_x = canvas->scale_x;
      canvas->device_to_pixels_y = canvas->scale_y;
      break;
    case GTK_UNIT_POINTS:
      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 72.0);
      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 72.0);
      break;
    case GTK_UNIT_INCH:
      canvas->device_to_pixels_x = canvas->scale_x * canvas->resolution_x;
      canvas->device_to_pixels_y = canvas->scale_y * canvas->resolution_y;
      break;
    case GTK_UNIT_MM:
      /* There are 25.4 mm to an inch. */
      canvas->device_to_pixels_x = canvas->scale_x * (canvas->resolution_x / 25.4);
      canvas->device_to_pixels_y = canvas->scale_y * (canvas->resolution_y / 25.4);
      break;
    }
}


/* This makes sure the canvas is all set up correctly, i.e. the scrollbar
   adjustments are set, the canvas x & y offsets are calculated, and the
   canvas window is sized. */
static void
reconfigure_canvas (GooCanvas *canvas,
		    gboolean   redraw_if_needed)
{
  gint width_pixels, height_pixels;
  gint window_x = 0, window_y = 0, window_width, window_height;
  gint new_x_offset = 0, new_y_offset = 0;
  GtkWidget *widget;

  widget = GTK_WIDGET (canvas);

  /* Make sure the bounds are sane. */
  if (canvas->bounds.x2 < canvas->bounds.x1)
    canvas->bounds.x2 = canvas->bounds.x1;
  if (canvas->bounds.y2 < canvas->bounds.y1)
    canvas->bounds.y2 = canvas->bounds.y1;

  /* Recalculate device_to_pixels_x & device_to_pixels_y. */
  recalculate_scales (canvas);

  /* This is the natural size of the canvas window in pixels, rounded up to
     the next pixel. */
  width_pixels = ((canvas->bounds.x2 - canvas->bounds.x1) * canvas->device_to_pixels_x) + 1;
  height_pixels = ((canvas->bounds.y2 - canvas->bounds.y1) * canvas->device_to_pixels_y) + 1;

  /* The actual window size is always at least as big as the widget's window.*/
  window_width = MAX (width_pixels, widget->allocation.width);
  window_height = MAX (height_pixels, widget->allocation.height);

  /* If the width or height is smaller than the window, we need to calculate
     the canvas x & y offsets according to the anchor. */
  if (width_pixels < widget->allocation.width)
    {
      switch (canvas->anchor)
	{
	case GTK_ANCHOR_NORTH_WEST:
	case GTK_ANCHOR_WEST:
	case GTK_ANCHOR_SOUTH_WEST:
	  new_x_offset = 0;
	  break;
	case GTK_ANCHOR_NORTH:
	case GTK_ANCHOR_CENTER:
	case GTK_ANCHOR_SOUTH:
	  new_x_offset = (widget->allocation.width - width_pixels) / 2;
	  break;
	case GTK_ANCHOR_NORTH_EAST:
	case GTK_ANCHOR_EAST:
	case GTK_ANCHOR_SOUTH_EAST:
	  new_x_offset = widget->allocation.width - width_pixels;
	  break;
	}
    }

  if (height_pixels < widget->allocation.height)
    {
      switch (canvas->anchor)
	{
	case GTK_ANCHOR_NORTH_WEST:
	case GTK_ANCHOR_NORTH:
	case GTK_ANCHOR_NORTH_EAST:
	  new_y_offset = 0;
	  break;
	case GTK_ANCHOR_WEST:
	case GTK_ANCHOR_CENTER:
	case GTK_ANCHOR_EAST:
	  new_y_offset = (widget->allocation.height - height_pixels) / 2;
	  break;
	case GTK_ANCHOR_SOUTH_WEST:
	case GTK_ANCHOR_SOUTH:
	case GTK_ANCHOR_SOUTH_EAST:
	  new_y_offset = widget->allocation.height - height_pixels;
	  break;
	}
    }

  canvas->freeze_count++;

  if (canvas->hadjustment)
    {
      goo_canvas_configure_hadjustment (canvas, window_width);
      window_x = - canvas->hadjustment->value;
    }

  if (canvas->vadjustment)
    {
      goo_canvas_configure_vadjustment (canvas, window_height);
      window_y = - canvas->vadjustment->value;
    }

  canvas->freeze_count--;

  if (GTK_WIDGET_REALIZED (canvas))
    {
      gdk_window_move_resize (canvas->canvas_window, window_x, window_y,
			      window_width, window_height);
    }

  /* If one of the offsets has changed we have to redraw the widget. */
  if (canvas->canvas_x_offset != new_x_offset
      || canvas->canvas_y_offset != new_y_offset)
    {
      canvas->canvas_x_offset = new_x_offset;
      canvas->canvas_y_offset = new_y_offset;

      if (redraw_if_needed)
	gtk_widget_queue_draw (GTK_WIDGET (canvas));
    }
}


static void     
goo_canvas_size_request (GtkWidget      *widget,
			 GtkRequisition *requisition)
{
  GList *tmp_list;
  GooCanvas *canvas;

  g_return_if_fail (GOO_IS_CANVAS (widget));

  canvas = GOO_CANVAS (widget);

  requisition->width = 0;
  requisition->height = 0;

  tmp_list = canvas->widget_items;

  while (tmp_list)
    {
      GooCanvasWidget *witem = tmp_list->data;
      GtkRequisition child_requisition;
      
      tmp_list = tmp_list->next;

      if (witem->widget)
	gtk_widget_size_request (witem->widget, &child_requisition);
    }
}


static void
goo_canvas_allocate_child_widget (GooCanvas       *canvas,
				  GooCanvasWidget *witem)
{
  GooCanvasBounds bounds;
  GtkAllocation allocation;

  goo_canvas_item_get_bounds ((GooCanvasItem*) witem, &bounds);

  goo_canvas_convert_to_pixels (canvas, &bounds.x1, &bounds.y1);
  goo_canvas_convert_to_pixels (canvas, &bounds.x2, &bounds.y2);

  /* Note that we only really support integers for the bounds, and we don't
     support scaling of a canvas with widget items in it. */
  allocation.x = bounds.x1;
  allocation.y = bounds.y1;
  allocation.width = bounds.x2 - allocation.x;
  allocation.height = bounds.y2 - allocation.y;
  
  gtk_widget_size_allocate (witem->widget, &allocation);
}


static void     
goo_canvas_size_allocate (GtkWidget     *widget,
			  GtkAllocation *allocation)
{
  GooCanvas *canvas;
  GList *tmp_list;

  g_return_if_fail (GOO_IS_CANVAS (widget));

  canvas = GOO_CANVAS (widget);

  widget->allocation = *allocation;

  if (GTK_WIDGET_REALIZED (widget))
    {
      /* We can only allocate our children when we are realized, since we
	 need a window to create a cairo_t which we use for layout. */
      tmp_list = canvas->widget_items;
      while (tmp_list)
	{
	  GooCanvasWidget *witem = tmp_list->data;
	  tmp_list = tmp_list->next;

	  if (witem->widget)
	    goo_canvas_allocate_child_widget (canvas, witem);
	}

      gdk_window_move_resize (widget->window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
      gdk_window_move_resize (canvas->tmp_window,
			      allocation->x, allocation->y,
			      allocation->width, allocation->height);
    }

  reconfigure_canvas (canvas, TRUE);
}


static void
goo_canvas_adjustment_value_changed (GtkAdjustment *adjustment,
				     GooCanvas     *canvas)
{
  AtkObject *accessible;

  if (!canvas->freeze_count && GTK_WIDGET_REALIZED (canvas))
    {
      gdk_window_move (canvas->canvas_window,
		       - canvas->hadjustment->value,
		       - canvas->vadjustment->value);
      
      /* If this is callback from a signal for one of the scrollbars, process
	 updates here for smoother scrolling. */
      if (adjustment)
	gdk_window_process_updates (canvas->canvas_window, TRUE);

      /* Notify any accessibility modules that the view has changed. */
      accessible = gtk_widget_get_accessible (GTK_WIDGET (canvas));
      g_signal_emit_by_name (accessible, "visible_data_changed");
    }
}


/* Sets either or both adjustments, If hadj or vadj is NULL a new adjustment
   is created. */
static void           
goo_canvas_set_adjustments (GooCanvas     *canvas,
			    GtkAdjustment *hadj,
			    GtkAdjustment *vadj)
{
  gboolean need_reconfigure = FALSE;

  g_return_if_fail (GOO_IS_CANVAS (canvas));

  if (hadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (hadj));
  else if (canvas->hadjustment)
    hadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));

  if (vadj)
    g_return_if_fail (GTK_IS_ADJUSTMENT (vadj));
  else if (canvas->vadjustment)
    vadj = GTK_ADJUSTMENT (gtk_adjustment_new (0.0, 0.0, 0.0, 0.0, 0.0, 0.0));
  
  if (canvas->hadjustment && (canvas->hadjustment != hadj))
    {
      g_signal_handlers_disconnect_by_func (canvas->hadjustment,
					    goo_canvas_adjustment_value_changed,
					    canvas);
      g_object_unref (canvas->hadjustment);
    }
  
  if (canvas->vadjustment && (canvas->vadjustment != vadj))
    {
      g_signal_handlers_disconnect_by_func (canvas->vadjustment,
					    goo_canvas_adjustment_value_changed,
					    canvas);
      g_object_unref (canvas->vadjustment);
    }
  
  if (canvas->hadjustment != hadj)
    {
      canvas->hadjustment = hadj;
      g_object_ref_sink (canvas->hadjustment);

      g_signal_connect (canvas->hadjustment, "value_changed",
			G_CALLBACK (goo_canvas_adjustment_value_changed),
			canvas);
      need_reconfigure = TRUE;
    }
  
  if (canvas->vadjustment != vadj)
    {
      canvas->vadjustment = vadj;
      g_object_ref_sink (canvas->vadjustment);

      g_signal_connect (canvas->vadjustment, "value_changed",
			G_CALLBACK (goo_canvas_adjustment_value_changed),
			canvas);
      need_reconfigure = TRUE;
    }

  if (need_reconfigure)
    reconfigure_canvas (canvas, TRUE);
}


/* Sets one of our pointers to an item, adding a reference to it and
   releasing any reference to the current item. */
static void
set_item_pointer (GooCanvasItem **item,
		  GooCanvasItem  *new)
{
  /* If the item hasn't changed, just return. */
  if (*item == new)
    return;

  /* Unref the current item, if it isn't NULL. */
  if (*item)
    g_object_unref (*item);

  /* Set the new item. */
  *item = new;

  /* Add a reference to it, if it isn't NULL. */
  if (*item)
    g_object_ref (*item);
}


/**
 * goo_canvas_get_bounds:
 * @canvas: a #GooCanvas.
 * @left: a pointer to a #gdouble to return the left edge, or %NULL.
 * @top: a pointer to a #gdouble to return the top edge, or %NULL.
 * @right: a pointer to a #gdouble to return the right edge, or %NULL.
 * @bottom: a pointer to a #gdouble to return the bottom edge, or %NULL.
 * 
 * Gets the bounds of the canvas, in canvas units.
 *
 * By default, canvas units are pixels, though the #GooCanvas:units property
 * can be used to change the units to points, inches or millimeters.
 **/
void
goo_canvas_get_bounds	(GooCanvas *canvas,
			 gdouble   *left,
			 gdouble   *top,
			 gdouble   *right,
			 gdouble   *bottom)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));

  if (left)
    *left = canvas->bounds.x1;
  if (top)
    *top = canvas->bounds.y1;
  if (right)
    *right = canvas->bounds.x2;
  if (bottom)
    *bottom = canvas->bounds.y2;
}


/**
 * goo_canvas_set_bounds:
 * @canvas: a #GooCanvas.
 * @left: the left edge.
 * @top: the top edge.
 * @right: the right edge.
 * @bottom: the bottom edge.
 * 
 * Sets the bounds of the #GooCanvas, in canvas units.
 *
 * By default, canvas units are pixels, though the #GooCanvas:units property
 * can be used to change the units to points, inches or millimeters.
 **/
void
goo_canvas_set_bounds	(GooCanvas *canvas,
			 gdouble    left,
			 gdouble    top,
			 gdouble    right,
			 gdouble    bottom)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));

  canvas->bounds.x1 = left;
  canvas->bounds.y1 = top;
  canvas->bounds.x2 = right;
  canvas->bounds.y2 = bottom;

  reconfigure_canvas (canvas, FALSE);
  gtk_widget_queue_draw (GTK_WIDGET (canvas));
}


/**
 * goo_canvas_scroll_to:
 * @canvas: a #GooCanvas.
 * @left: the x coordinate to scroll to.
 * @top: the y coordinate to scroll to.
 * 
 * Scrolls the canvas, placing the given point as close to the top-left of
 * the view as possible.
 **/
void
goo_canvas_scroll_to	     (GooCanvas     *canvas,
			      gdouble        left,
			      gdouble        top)
{
  gdouble x = left, y = top;

  g_return_if_fail (GOO_IS_CANVAS (canvas));

  /* The scrollbar adjustments use pixel values, so convert to pixels. */
  goo_canvas_convert_to_pixels (canvas, &x, &y);

  /* Make sure we stay within the bounds. */
  x = CLAMP (x, canvas->hadjustment->lower,
	     canvas->hadjustment->upper - canvas->hadjustment->page_size);
  y = CLAMP (y, canvas->vadjustment->lower,
	     canvas->vadjustment->upper - canvas->vadjustment->page_size);

  canvas->freeze_count++;

  gtk_adjustment_set_value (canvas->hadjustment, x);
  gtk_adjustment_set_value (canvas->vadjustment, y);

  canvas->freeze_count--;
  goo_canvas_adjustment_value_changed (NULL, canvas);
}


/* This makes sure the given item is displayed, scrolling if necessary. */
static void
goo_canvas_scroll_to_item (GooCanvas     *canvas,
			   GooCanvasItem *item)
{
  GooCanvasBounds bounds;
  gdouble hvalue, vvalue;

  goo_canvas_item_get_bounds (item, &bounds);

  goo_canvas_convert_to_pixels (canvas, &bounds.x1, &bounds.y1);
  goo_canvas_convert_to_pixels (canvas, &bounds.x2, &bounds.y2);

  canvas->freeze_count++;

  /* Remember the current adjustment values. */
  hvalue = canvas->hadjustment->value;
  vvalue = canvas->vadjustment->value;

  /* Update the adjustments so the item is displayed. */
  gtk_adjustment_clamp_page (canvas->hadjustment, bounds.x1, bounds.x2);
  gtk_adjustment_clamp_page (canvas->vadjustment, bounds.y1, bounds.y2);

  canvas->freeze_count--;

  /* If the adjustments have changed we need to scroll. */
  if (hvalue != canvas->hadjustment->value
      || vvalue != canvas->vadjustment->value)
    goo_canvas_adjustment_value_changed (NULL, canvas);
}


/**
 * goo_canvas_get_scale:
 * @canvas: a #GooCanvas.
 * 
 * Gets the current scale of the canvas.
 *
 * The scale specifies the magnification factor of the canvas, e.g. if an item
 * has a width of 2 pixels and the scale is set to 3, it will be displayed with
 * a width of 2 x 3 = 6 pixels.
 *
 * Returns: the current scale setting.
 **/
gdouble
goo_canvas_get_scale	(GooCanvas *canvas)
{
  g_return_val_if_fail (GOO_IS_CANVAS (canvas), 1.0);

  return canvas->scale;
}


static void
goo_canvas_set_scale_internal	(GooCanvas *canvas,
				 gdouble    scale_x,
				 gdouble    scale_y)
{
  gdouble x, y;

  g_return_if_fail (GOO_IS_CANVAS (canvas));

  /* Calculate the coords of the current center point in pixels. */
  x = canvas->hadjustment->value + canvas->hadjustment->page_size / 2;
  y = canvas->vadjustment->value + canvas->vadjustment->page_size / 2;

  /* Convert from pixel units to device units. */
  goo_canvas_convert_from_pixels (canvas, &x, &y);

  /* Show our temporary window above the canvas window, so that the windowing
     system doesn't try to scroll the contents when we change the adjustments.
     Since we are changing the scale we need to redraw everything so the
     scrolling is unnecessary and really ugly.
     FIXME: There is a possible issue with keyboard focus/input methods here,
     since hidden windows can't have the keyboard focus. */
  if (GTK_WIDGET_MAPPED (canvas))
    gdk_window_show (canvas->tmp_window);

  canvas->freeze_count++;

  canvas->scale_x = scale_x;
  canvas->scale_y = scale_y;
  canvas->scale = MIN (scale_x, scale_y);
  reconfigure_canvas (canvas, FALSE);

  /* Convert from the center point to the new desired top-left posision. */
  x -= canvas->hadjustment->page_size / canvas->device_to_pixels_x / 2;
  y -= canvas->vadjustment->page_size / canvas->device_to_pixels_y / 2;

  /* Now try to scroll to it. */
  goo_canvas_scroll_to (canvas, x, y);

  canvas->freeze_count--;
  goo_canvas_adjustment_value_changed (NULL, canvas);

  /* Now hide the temporary window, so the canvas window will get an expose
     event. */
  if (GTK_WIDGET_MAPPED (canvas))
    gdk_window_hide (canvas->tmp_window);
}


/**
 * goo_canvas_set_scale:
 * @canvas: a #GooCanvas.
 * @scale: the new scale setting.
 * 
 * Sets the scale of the canvas.
 *
 * The scale specifies the magnification factor of the canvas, e.g. if an item
 * has a width of 2 pixels and the scale is set to 3, it will be displayed with
 * a width of 2 x 3 = 6 pixels.
 **/
void
goo_canvas_set_scale	(GooCanvas *canvas,
			 gdouble    scale)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));

  goo_canvas_set_scale_internal (canvas, scale, scale);
}


/**
 * goo_canvas_unregister_item:
 * @canvas: a #GooCanvas.
 * @model: the item model whose canvas item is being finalized.
 * 
 * This function is only intended to be used when implementing new canvas
 * items.
 *
 * It should be called in the finalize method of #GooCanvasItem
 * objects, to remove the canvas item from the #GooCanvas's hash table.
 **/
void
goo_canvas_unregister_item (GooCanvas          *canvas,
			    GooCanvasItemModel *model)
{
  if (canvas->model_to_item)
    g_hash_table_remove (canvas->model_to_item, model);
}


/**
 * goo_canvas_create_item:
 * @canvas: a #GooCanvas.
 * @model: the item model to create a canvas item for.
 * 
 * This function is only intended to be used when implementing new canvas
 * items, typically container items such as #GooCanvasGroup.
 *
 * It creates a new canvas item for the given item model, and recursively
 * creates items for any children.
 *
 * It uses the create_item() virtual method if it has been set.
 * Subclasses of #GooCanvas can define this method if they want to use
 * custom views for items.
 *
 * It emits the #GooCanvas::item-created signal after creating the view, so
 * application code can connect signal handlers to the new view if desired.
 * 
 * Returns: a new canvas item.
 **/
GooCanvasItem*
goo_canvas_create_item  (GooCanvas          *canvas,
			 GooCanvasItemModel *model)
{
  GooCanvasItem *item = NULL;

  /* Use the virtual method if it has been set. */
  if (GOO_CANVAS_GET_CLASS (canvas)->create_item)
    item = GOO_CANVAS_GET_CLASS (canvas)->create_item (canvas, model);

  /* The virtual method can return NULL to use the default view for an item. */
  if (!item)
    item = GOO_CANVAS_ITEM_MODEL_GET_IFACE (model)->create_item (model,
								 canvas);

  if (canvas->model_to_item)
    g_hash_table_insert (canvas->model_to_item, model, item);

  /* Emit a signal so apps can hook up signal handlers if they want. */
  g_signal_emit (canvas, canvas_signals[ITEM_CREATED], 0, item, model);

  return item;
}


static void
goo_canvas_update_automatic_bounds (GooCanvas       *canvas)
{
  GooCanvasBounds bounds = { 0.0, 0.0, GOO_CANVAS_DEFAULT_WIDTH,
			     GOO_CANVAS_DEFAULT_HEIGHT };

  if (canvas->root_item)
    goo_canvas_item_get_bounds (canvas->root_item, &bounds);

  /* Calculate the new automatic bounds, which is the bounds of all the items
     in the canvas plus any specified padding. If bounds_from_origin is set
     x1 and y1 are set to 0.0. */
  if (canvas->bounds_from_origin)
    {
      bounds.x1 = 0.0;
      bounds.y1 = 0.0;
      bounds.x2 += canvas->bounds_padding;
      bounds.y2 += canvas->bounds_padding;
    }
  else
    {
      bounds.x1 -= canvas->bounds_padding;
      bounds.y1 -= canvas->bounds_padding;
      bounds.x2 += canvas->bounds_padding;
      bounds.y2 += canvas->bounds_padding;
    }

  /* Make sure the bounds are sane. */
  if (bounds.x2 < bounds.x1)
    bounds.x2 = bounds.x1;
  if (bounds.y2 < bounds.y1)
    bounds.y2 = bounds.y1;

  /* If the bounds have changed, reconfigure the canvas and redraw. */
  if (bounds.x1 != canvas->bounds.x1
      || bounds.y1 != canvas->bounds.y1
      || bounds.x2 != canvas->bounds.x2
      || bounds.y2 != canvas->bounds.y2)
    {
      canvas->bounds = bounds;
      reconfigure_canvas (canvas, FALSE);
      gtk_widget_queue_draw (GTK_WIDGET (canvas));
    }
}


static void
goo_canvas_update_internal (GooCanvas *canvas,
			    cairo_t   *cr)
{
  GooCanvasBounds bounds;

  /* It is possible that processing the first set of updates causes other
     updates to be scheduled, so we loop round until all are done. Items
     should ensure that they don't cause this to loop forever. */
  while (canvas->need_update)
    {
      gboolean entire_tree = canvas->need_entire_subtree_update;

      canvas->need_update = FALSE;
      canvas->need_entire_subtree_update = FALSE;
      if (canvas->root_item)
	goo_canvas_item_update (canvas->root_item, entire_tree, cr, &bounds);
    }

  /* If the bounds are automatically-calculated, update them now. */
  if (canvas->root_item && canvas->automatic_bounds)
    goo_canvas_update_automatic_bounds (canvas);

  /* Check which item is under the pointer. */
  update_pointer_item (canvas, NULL);
}


/**
 * goo_canvas_update:
 * @canvas: a #GooCanvas.
 * 
 * This function is only intended to be used by subclasses of #GooCanvas or
 * #GooCanvasItem implementations.
 *
 * It updates any items that need updating.
 *
 * If the bounds of items change, they will request a redraw of the old and
 * new bounds so the display is updated correctly.
 **/
void
goo_canvas_update (GooCanvas *canvas)
{
  cairo_t *cr = goo_canvas_create_cairo_context (canvas);
  goo_canvas_update_internal (canvas, cr);
  cairo_destroy (cr);
}


static gint
goo_canvas_idle_handler (GooCanvas *canvas)
{
  GDK_THREADS_ENTER ();

  goo_canvas_update (canvas);

  /* Reset idle id. Note that we do this after goo_canvas_update(), to
     make sure we don't schedule another idle handler while that is running. */
  canvas->idle_id = 0;

  GDK_THREADS_LEAVE ();

  /* Return FALSE to remove the idle handler. */
  return FALSE;
}


/**
 * goo_canvas_request_update:
 * @canvas: a #GooCanvas.
 * 
 * This function is only intended to be used by subclasses of #GooCanvas or
 * #GooCanvasItem implementations.
 *
 * It schedules an update of the #GooCanvas. This will be performed in
 * the idle loop, after all pending events have been handled, but before
 * the canvas has been repainted.
 **/
void
goo_canvas_request_update (GooCanvas   *canvas)
{
  canvas->need_update = TRUE;

  /* We have to wait until we are realized. We'll do a full update then. */
  if (!GTK_WIDGET_REALIZED (canvas))
    return;

  /* We use a higher priority than the normal GTK+ resize/redraw idle handlers
   * so the canvas state will be updated before allocating sizes & redrawing.
   */
  if (!canvas->idle_id)
    canvas->idle_id = g_idle_add_full (GTK_PRIORITY_RESIZE - 5, (GSourceFunc) goo_canvas_idle_handler, canvas, NULL);
}


/**
 * goo_canvas_request_redraw:
 * @canvas: a #GooCanvas.
 * @bounds: the bounds to redraw.
 * 
 * This function is only intended to be used by subclasses of #GooCanvas or
 * #GooCanvasItem implementations.
 *
 * Requests that the given bounds be redrawn.
 **/
void
goo_canvas_request_redraw (GooCanvas             *canvas,
			   const GooCanvasBounds *bounds)
{
  GdkRectangle rect;

  if (!GTK_WIDGET_DRAWABLE (canvas) || (bounds->x1 == bounds->x2))
    return;

  /* We subtract one from the left & top edges, in case anti-aliasing makes
     the drawing use an extra pixel. */
  rect.x = (double) (bounds->x1 - canvas->bounds.x1) * canvas->device_to_pixels_x - 1;
  rect.y = (double) (bounds->y1 - canvas->bounds.y1) * canvas->device_to_pixels_y - 1;

  /* We add an extra one here for the same reason. (The other extra one is to
     round up to the next pixel.) And one for luck! */
  rect.width = (double) (bounds->x2 - canvas->bounds.x1) * canvas->device_to_pixels_x
    - rect.x + 2 + 1;
  rect.height = (double) (bounds->y2 - canvas->bounds.y1) * canvas->device_to_pixels_y
    - rect.y + 2 + 1;

  rect.x += canvas->canvas_x_offset;
  rect.y += canvas->canvas_y_offset;

  gdk_window_invalidate_rect (canvas->canvas_window, &rect, FALSE);
}


static gboolean
goo_canvas_expose_event (GtkWidget      *widget,
			 GdkEventExpose *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
  GooCanvasBounds bounds, root_item_bounds;
  cairo_t *cr;

  if (!canvas->root_item)
    return FALSE;

  if (event->window != canvas->canvas_window)
    return FALSE;

  /* Clear the background. */
  if (canvas->clear_background)
    {
      gdk_draw_rectangle (canvas->canvas_window,
			  widget->style->base_gc[widget->state], TRUE,
			  event->area.x, event->area.y,
			  event->area.width, event->area.height);
    }

  cr = goo_canvas_create_cairo_context (canvas);

  if (canvas->need_update)
    goo_canvas_update_internal (canvas, cr);

  bounds.x1 = ((event->area.x - canvas->canvas_x_offset) / canvas->device_to_pixels_x)
    + canvas->bounds.x1;
  bounds.y1 = ((event->area.y - canvas->canvas_y_offset) / canvas->device_to_pixels_y)
    + canvas->bounds.y1;
  bounds.x2 = (event->area.width / canvas->device_to_pixels_x) + bounds.x1;
  bounds.y2 = (event->area.height / canvas->device_to_pixels_y) + bounds.y1;

  /* Translate it to use the canvas pixel offsets (used when the canvas is
     smaller than the window and the anchor isn't set to NORTH_WEST). */
  cairo_translate (cr, canvas->canvas_x_offset, canvas->canvas_y_offset);

  /* Scale it so we can use canvas coordinates. */
  cairo_scale (cr, canvas->device_to_pixels_x, canvas->device_to_pixels_y);

  /* Translate it so the top-left of the canvas becomes (0,0). */
  cairo_translate (cr, -canvas->bounds.x1, -canvas->bounds.y1);

  /* Clip to the canvas bounds, if necessary. We only need to clip if the
     items in the canvas extend outside the canvas bounds and the canvas
     bounds is less than the area being painted. */
  goo_canvas_item_get_bounds (canvas->root_item, &root_item_bounds);
  if ((root_item_bounds.x1 < canvas->bounds.x1
       && canvas->bounds.x1 > bounds.x1)
      || (root_item_bounds.x2 > canvas->bounds.x2
	  && canvas->bounds.x2 < bounds.x2)
      || (root_item_bounds.y1 < canvas->bounds.y1
	  && canvas->bounds.y1 > bounds.y1)
      || (root_item_bounds.y2 > canvas->bounds.y2
	  && canvas->bounds.y2 < bounds.y2))
    {
      cairo_new_path (cr);
      cairo_move_to (cr, canvas->bounds.x1, canvas->bounds.y1);
      cairo_line_to (cr, canvas->bounds.x2, canvas->bounds.y1);
      cairo_line_to (cr, canvas->bounds.x2, canvas->bounds.y2);
      cairo_line_to (cr, canvas->bounds.x1, canvas->bounds.y2);
      cairo_close_path (cr);
      cairo_clip (cr);
    }

  goo_canvas_item_paint (canvas->root_item, cr, &bounds, canvas->scale);

  cairo_destroy (cr);

  GTK_WIDGET_CLASS (goo_canvas_parent_class)->expose_event (widget, event);

  return FALSE;
}


/**
 * goo_canvas_render:
 * @canvas: a #GooCanvas.
 * @cr: a cairo context.
 * @bounds: the area to render, or %NULL to render the entire canvas.
 * @scale: the scale to compare with each item's visibility
 * threshold to see if they should be rendered. This only affects items that
 * have their visibility set to %GOO_CANVAS_ITEM_VISIBLE_ABOVE_THRESHOLD.
 * 
 * Renders all or part of a canvas to the given cairo context.
 **/
void
goo_canvas_render (GooCanvas             *canvas,
		   cairo_t               *cr,
		   const GooCanvasBounds *bounds,
		   gdouble                scale)
{
  if (canvas->need_update)
    goo_canvas_update (canvas);

  /* Set the default line width based on the current units setting. */
  cairo_set_line_width (cr, goo_canvas_get_default_line_width (canvas));

  if (bounds)
    {
      /* Clip to the given bounds. */
      cairo_new_path (cr);
      cairo_move_to (cr, bounds->x1, bounds->y1);
      cairo_line_to (cr, bounds->x2, bounds->y1);
      cairo_line_to (cr, bounds->x2, bounds->y2);
      cairo_line_to (cr, bounds->x1, bounds->y2);
      cairo_close_path (cr);
      cairo_clip (cr);

      goo_canvas_item_paint (canvas->root_item, cr, bounds, scale);
    }
  else
    {
      goo_canvas_item_paint (canvas->root_item, cr, &canvas->bounds, scale);
    }
}


/*
 * Keyboard/Mouse Event & Grab Handling.
 */

/* Initializes a synthesized crossing event from a given button press/release,
   motion, or crossing event. */
static void
initialize_crossing_event (GooCanvas *canvas,
			   GdkEvent  *event)
{
  GdkEventCrossing *crossing_event = &canvas->crossing_event;

  /* Initialize the crossing event. */
  crossing_event->type       = event->any.type;
  crossing_event->window     = event->any.window;
  crossing_event->send_event = event->any.send_event;
  crossing_event->subwindow  = NULL;
  crossing_event->detail     = GDK_NOTIFY_ANCESTOR;
  crossing_event->focus      = FALSE;
  crossing_event->mode       = GDK_CROSSING_NORMAL;

  switch (event->type)
    {
    case GDK_MOTION_NOTIFY:
      crossing_event->time   = event->motion.time;
      crossing_event->x      = event->motion.x;
      crossing_event->y      = event->motion.y;
      crossing_event->x_root = event->motion.x_root;
      crossing_event->y_root = event->motion.y_root;
      crossing_event->state  = event->motion.state;
      break;

    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      crossing_event->time   = event->crossing.time;
      crossing_event->x      = event->crossing.x;
      crossing_event->y      = event->crossing.y;
      crossing_event->x_root = event->crossing.x_root;
      crossing_event->y_root = event->crossing.y_root;
      crossing_event->state  = event->crossing.state;
      break;

    default:
      /* It must be a button press/release event. */
      crossing_event->time   = event->button.time;
      crossing_event->x      = event->button.x;
      crossing_event->y      = event->button.y;
      crossing_event->x_root = event->button.x_root;
      crossing_event->y_root = event->button.y_root;
      crossing_event->state  = event->button.state;
      break;
    }
}


/* Emits a signal for an item and all its parents, until one of them
   returns TRUE. */
static gboolean
propagate_event (GooCanvas     *canvas,
		 GooCanvasItem *item,
		 gchar         *signal_name,
		 GdkEvent      *event)
{
  GooCanvasItem *ancestor;
  gboolean stop_emission = FALSE, valid;

  /* Don't emit any events if the canvas is not realized. */
  if (!GTK_WIDGET_REALIZED (canvas))
    return FALSE;

  if (item)
    {
      /* Check if the item is still in the canvas. */
      if (!ITEM_IS_VALID (item))
	return FALSE;
      ancestor = item;
    }
  else
    {
      /* If there is no target item, we send the event to the root item,
	 with target set to NULL. */
      ancestor = canvas->root_item;
    }

  /* Make sure the item pointer remains valid throughout the emission. */
  if (item)
    g_object_ref (item);

  while (ancestor)
    {
      g_object_ref (ancestor);

      g_signal_emit_by_name (ancestor, signal_name, item, event,
			     &stop_emission);

      /* Check if the ancestor is still in the canvas. */
      valid = ITEM_IS_VALID (ancestor) ? TRUE : FALSE;

      g_object_unref (ancestor);

      if (stop_emission || !valid)
	break;

      ancestor = goo_canvas_item_get_parent (ancestor);
    }

  if (item)
    g_object_unref (item);

  return stop_emission;
}


/* This is called to emit pointer events - enter/leave notify, motion notify,
   and button press/release. */
static gboolean
emit_pointer_event (GooCanvas *canvas,
		    gchar     *signal_name,
		    GdkEvent  *original_event)
{
  GdkEvent event = *original_event;
  GooCanvasItem *target_item = canvas->pointer_item;
  double *x, *y, *x_root, *y_root;

  /* Check if an item has grabbed the pointer. */
  if (canvas->pointer_grab_item)
    {
      /* When the pointer is grabbed, it receives all the pointer motion,
	 button press/release events and its own enter/leave notify events.
	 Enter/leave notify events for other items are discarded. */
      if ((event.type == GDK_ENTER_NOTIFY || event.type == GDK_LEAVE_NOTIFY)
	  && canvas->pointer_item != canvas->pointer_grab_item)
	return FALSE;

      target_item = canvas->pointer_grab_item;
    }

  /* Check if the target item is still in the canvas. */
  if (target_item && !ITEM_IS_VALID (target_item))
    return FALSE;

  /* Translate the x & y coordinates to the item's space. */
  switch (event.type)
    {
    case GDK_MOTION_NOTIFY:
      x = &event.motion.x;
      y = &event.motion.y;
      x_root = &event.motion.x_root;
      y_root = &event.motion.y_root;
      break;
    case GDK_ENTER_NOTIFY:
    case GDK_LEAVE_NOTIFY:
      x = &event.crossing.x;
      y = &event.crossing.y;
      x_root = &event.crossing.x_root;
      y_root = &event.crossing.y_root;
      break;
    default:
      /* It must be a button press/release event. */
      x = &event.button.x;
      y = &event.button.y;
      x_root = &event.button.x_root;
      y_root = &event.button.y_root;
      break;
    }

  /* Add 0.5 to the pixel coordinates so we use the center of the pixel. */
  *x += 0.5;
  *y += 0.5;

  /* Convert to the canvas coordinate space. */
  goo_canvas_convert_from_pixels (canvas, x, y);

  /* Copy to the x_root & y_root fields. */
  *x_root = *x;
  *y_root = *y;

  /* Convert to the item's coordinate space. */
  goo_canvas_convert_to_item_space (canvas, target_item, x, y);

  return propagate_event (canvas, target_item, signal_name, &event);
}


/* Finds the item that the mouse is over, using the given event's
 * coordinates. It emits enter/leave events for items as appropriate.
 */
static void
update_pointer_item (GooCanvas *canvas,
		     GdkEvent  *event)
{
  GooCanvasItem *new_item = NULL;

  if (event)
    initialize_crossing_event (canvas, event);

  /* If the event type is GDK_LEAVE_NOTIFY, the mouse has left the canvas,
     so we leave new_item as NULL, otherwise we find which item is
     underneath the mouse. Note that we initialize the type to GDK_LEAVE_NOTIFY
     in goo_canvas_init() to indicate the mouse isn't in the canvas. */
  if (canvas->crossing_event.type != GDK_LEAVE_NOTIFY && canvas->root_item)
    {
      double x = canvas->crossing_event.x;
      double y = canvas->crossing_event.y;

      goo_canvas_convert_from_pixels (canvas, &x, &y);
      new_item = goo_canvas_get_item_at (canvas, x, y, TRUE);
    }

  /* If the current item hasn't changed, just return. */
  if (new_item == canvas->pointer_item)
    return;

  /* Ref the new item, in case it is removed below. */
  if (new_item)
    g_object_ref (new_item);

  /* Emit a leave-notify event for the current item. */
  if (canvas->pointer_item)
    {
      canvas->crossing_event.type = GDK_LEAVE_NOTIFY;
      emit_pointer_event (canvas, "leave_notify_event",
			  (GdkEvent*) &canvas->crossing_event);
    }

  /* If there is no new item we are done. */
  if (!new_item)
    {
      set_item_pointer (&canvas->pointer_item, NULL);
      return;
    }

  /* If the new item isn't in the canvas any more, don't use it. */
  if (!ITEM_IS_VALID (new_item))
    {
      set_item_pointer (&canvas->pointer_item, NULL);
      g_object_unref (new_item);
      return;
    }

  /* Emit an enter-notify for the new current item. */
  set_item_pointer (&canvas->pointer_item, new_item);
  canvas->crossing_event.type = GDK_ENTER_NOTIFY;
  emit_pointer_event (canvas, "enter_notify_event",
		      (GdkEvent*) &canvas->crossing_event);

  g_object_unref (new_item);
}


static gboolean
goo_canvas_crossing        (GtkWidget        *widget,
			    GdkEventCrossing *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);

  if (event->window != canvas->canvas_window)
    return FALSE;

  /* This will result in synthesizing focus_in/out events as appropriate. */
  update_pointer_item (canvas, (GdkEvent*) event);

  return FALSE;
}


static gboolean
goo_canvas_motion          (GtkWidget      *widget,
			    GdkEventMotion *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);

  if (event->window != canvas->canvas_window)
    return FALSE;

  /* For motion notify hint events we need to call gdk_window_get_pointer()
     to let X know we're ready for another pointer event. */
  if (event->is_hint)
    gdk_window_get_pointer (event->window, NULL, NULL, NULL);

  update_pointer_item (canvas, (GdkEvent*) event);

  return emit_pointer_event (canvas, "motion_notify_event", (GdkEvent*) event);
}


static gboolean
goo_canvas_button_press (GtkWidget      *widget,
			 GdkEventButton *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
  GdkDisplay *display;

  if (event->window != canvas->canvas_window)
    return FALSE;

  update_pointer_item (canvas, (GdkEvent*) event);

  /* Check if this is the start of an implicit pointer grab, i.e. if we
     don't already have a grab and the app has no active grab. */
  display = gtk_widget_get_display (widget);
  if (!canvas->pointer_grab_item
      && !gdk_display_pointer_is_grabbed (display))
    {
      set_item_pointer (&canvas->pointer_grab_initial_item,
			canvas->pointer_item);
      set_item_pointer (&canvas->pointer_grab_item,
			canvas->pointer_item);
      canvas->pointer_grab_button = event->button;
    }

  return emit_pointer_event (canvas, "button_press_event", (GdkEvent*) event);
}


static gboolean
goo_canvas_button_release (GtkWidget      *widget,
			   GdkEventButton *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
  GdkDisplay *display;
  gboolean retval;

  if (event->window != canvas->canvas_window)
    return FALSE;

  update_pointer_item (canvas, (GdkEvent*) event);

  retval = emit_pointer_event (canvas, "button_release_event",
			       (GdkEvent*) event);

  /* Check if an implicit (passive) grab has ended. */
  display = gtk_widget_get_display (widget);
  if (canvas->pointer_grab_item
      && event->button == canvas->pointer_grab_button
      && !gdk_display_pointer_is_grabbed (display))
    {
      /* We set the pointer item back to the item it was in before the
	 grab, so we'll synthesize enter/leave notify events as appropriate. */
      if (canvas->pointer_grab_initial_item
	  && ITEM_IS_VALID (canvas->pointer_grab_initial_item))
	set_item_pointer (&canvas->pointer_item,
			  canvas->pointer_grab_initial_item);
      else
	set_item_pointer (&canvas->pointer_item, NULL);

      set_item_pointer (&canvas->pointer_grab_item, NULL);
      set_item_pointer (&canvas->pointer_grab_initial_item, NULL);

      update_pointer_item (canvas, (GdkEvent*) event);
    }

  return retval;
}


static gint
goo_canvas_scroll	(GtkWidget      *widget,
			 GdkEventScroll *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
  GtkAdjustment *adj;
  gdouble delta, new_value;

  if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_DOWN)
    adj = canvas->vadjustment;
  else
    adj = canvas->hadjustment;

  delta = pow (adj->page_size, 2.0 / 3.0);

  if (event->direction == GDK_SCROLL_UP || event->direction == GDK_SCROLL_LEFT)
    delta = - delta;

  new_value = CLAMP (adj->value + delta, adj->lower,
		     adj->upper - adj->page_size);
      
  gtk_adjustment_set_value (adj, new_value);

  return TRUE;
}


static gboolean
goo_canvas_focus_in        (GtkWidget      *widget,
			    GdkEventFocus  *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);

  GTK_WIDGET_SET_FLAGS (widget, GTK_HAS_FOCUS);

  if (canvas->focused_item)
    return propagate_event (canvas, canvas->focused_item,
			    "focus_in_event", (GdkEvent*) event);
  else
    return FALSE;
}


static gboolean
goo_canvas_focus_out       (GtkWidget      *widget,
			    GdkEventFocus  *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);

  GTK_WIDGET_UNSET_FLAGS (widget, GTK_HAS_FOCUS);

  if (canvas->focused_item)
    return propagate_event (canvas, canvas->focused_item,
			    "focus_out_event", (GdkEvent*) event);
  else
    return FALSE;
}


static gboolean
goo_canvas_key_press       (GtkWidget      *widget,
			    GdkEventKey    *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
	
  if (GTK_WIDGET_HAS_FOCUS (canvas) && canvas->focused_item)
    if (propagate_event (canvas, canvas->focused_item, "key_press_event",
			 (GdkEvent*) event))
    return TRUE;

  return GTK_WIDGET_CLASS (goo_canvas_parent_class)->key_press_event (widget, event);
}


static gboolean
goo_canvas_key_release     (GtkWidget      *widget,
			    GdkEventKey    *event)
{
  GooCanvas *canvas = GOO_CANVAS (widget);
	
  if (GTK_WIDGET_HAS_FOCUS (canvas) && canvas->focused_item)
    if (propagate_event (canvas, canvas->focused_item, "key_release_event",
			 (GdkEvent*) event))
    return TRUE;

  return GTK_WIDGET_CLASS (goo_canvas_parent_class)->key_release_event (widget, event);
}


static void
generate_grab_broken (GooCanvas     *canvas,
		      GooCanvasItem *item,
		      gboolean       keyboard,
		      gboolean       implicit)

{
  GdkEventGrabBroken event;
  
  if (!ITEM_IS_VALID (item))
    return;

  event.type = GDK_GRAB_BROKEN;
  event.window = canvas->canvas_window;
  event.send_event = 0;
  event.keyboard = keyboard;
  event.implicit = implicit;
  event.grab_window = event.window;

  propagate_event (canvas, item, "grab_broken_event",
		   (GdkEvent*) &event);
}


static gboolean
goo_canvas_grab_broken     (GtkWidget          *widget,
			    GdkEventGrabBroken *event)
{
  GooCanvas *canvas;

  g_return_val_if_fail (GOO_IS_CANVAS (widget), FALSE);

  canvas = GOO_CANVAS (widget);

  if (event->keyboard)
    {
      if (canvas->keyboard_grab_item)
	{
	  generate_grab_broken (canvas, canvas->keyboard_grab_item,
				event->keyboard, event->implicit);
	  set_item_pointer (&canvas->keyboard_grab_item, NULL);
	}
    }
  else
    {
      if (canvas->pointer_grab_item)
	{
	  generate_grab_broken (canvas, canvas->pointer_grab_item,
				event->keyboard, event->implicit);
	  set_item_pointer (&canvas->pointer_grab_item, NULL);
	}
    }

  return TRUE;
}


/**
 * goo_canvas_grab_focus:
 * @canvas: a #GooCanvas.
 * @item: the item to grab the focus.
 * 
 * Grabs the keyboard focus for the given item.
 **/
void
goo_canvas_grab_focus (GooCanvas     *canvas,
		       GooCanvasItem *item)
{
  GdkEventFocus event;

  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_ITEM (item));
  g_return_if_fail (GTK_WIDGET_CAN_FOCUS (canvas));

  if (canvas->focused_item) {
    event.type = GDK_FOCUS_CHANGE;
    event.window = canvas->canvas_window;
    event.send_event = FALSE;
    event.in = FALSE;

    propagate_event (canvas, canvas->focused_item,
		     "focus_out_event", (GdkEvent*) &event);
  }

  set_item_pointer (&canvas->focused_item, item);

  gtk_widget_grab_focus (GTK_WIDGET (canvas));

  if (canvas->focused_item) {
    event.type = GDK_FOCUS_CHANGE;                        
    event.window = canvas->canvas_window;
    event.send_event = FALSE;                             
    event.in = TRUE;                                      

    propagate_event (canvas, canvas->focused_item,
		     "focus_in_event", (GdkEvent*) &event);
  }                               
}


/*
 * Pointer/keyboard grabbing & ungrabbing.
 */

/**
 * goo_canvas_pointer_grab:
 * @canvas: a #GooCanvas.
 * @item: the item to grab the pointer for.
 * @event_mask: the events to receive during the grab.
 * @cursor: the cursor to display during the grab, or NULL.
 * @time: the time of the event that lead to the pointer grab. This should
 *  come from the relevant #GdkEvent.
 * 
 * Attempts to grab the pointer for the given item.
 * 
 * Returns: %GDK_GRAB_SUCCESS if the grab succeeded.
 **/
GdkGrabStatus
goo_canvas_pointer_grab (GooCanvas     *canvas,
			 GooCanvasItem *item,
			 GdkEventMask   event_mask,
			 GdkCursor     *cursor,
			 guint32        time)
{
  GdkGrabStatus status = GDK_GRAB_SUCCESS;

  g_return_val_if_fail (GOO_IS_CANVAS (canvas), GDK_GRAB_NOT_VIEWABLE);
  g_return_val_if_fail (GOO_IS_CANVAS_ITEM (item), GDK_GRAB_NOT_VIEWABLE);

  /* If another item already has the pointer grab, we need to synthesize a
     grab-broken event for the current grab item. */
  if (canvas->pointer_grab_item
      && canvas->pointer_grab_item != item)
    {
      generate_grab_broken (canvas, canvas->pointer_grab_item,
			    FALSE, FALSE);
      set_item_pointer (&canvas->pointer_grab_item, NULL);
    }

  /* This overrides any existing grab. */
  status = gdk_pointer_grab (canvas->canvas_window, FALSE,
			     event_mask, NULL, cursor, time);

  if (status == GDK_GRAB_SUCCESS)
    {
      set_item_pointer (&canvas->pointer_grab_initial_item,
			     canvas->pointer_item);
      set_item_pointer (&canvas->pointer_grab_item,
			     item);
    }

  return status;
}


/**
 * goo_canvas_pointer_ungrab:
 * @canvas: a #GooCanvas.
 * @item: the item that has the grab.
 * @time: the time of the event that lead to the pointer ungrab. This should
 *  come from the relevant #GdkEvent.
 * 
 * Ungrabs the pointer, if the given item has the pointer grab.
 **/
void
goo_canvas_pointer_ungrab (GooCanvas     *canvas,
			   GooCanvasItem *item,
			   guint32        time)
{
  GdkDisplay *display;

  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_ITEM (item));

  /* If the item doesn't actually have the pointer grab, just return. */
  if (canvas->pointer_grab_item != item)
    return;

  /* If it is an active pointer grab, ungrab it explicitly. */
  display = gtk_widget_get_display (GTK_WIDGET (canvas));
  if (gdk_display_pointer_is_grabbed (display))
    gdk_display_pointer_ungrab (display, time);

  /* We set the pointer item back to the item it was in before the
     grab, so we'll synthesize enter/leave notify events as appropriate. */
  if (canvas->pointer_grab_initial_item
      && ITEM_IS_VALID (canvas->pointer_grab_initial_item))
    set_item_pointer (&canvas->pointer_item,
		      canvas->pointer_grab_initial_item);
  else
    set_item_pointer (&canvas->pointer_item, NULL);

  set_item_pointer (&canvas->pointer_grab_item, NULL);
  set_item_pointer (&canvas->pointer_grab_initial_item, NULL);

  update_pointer_item (canvas, NULL);
 }


/**
 * goo_canvas_keyboard_grab:
 * @canvas: a #GooCanvas.
 * @item: the item to grab the keyboard for.
 * @owner_events: %TRUE if keyboard events for this application will be
 *  reported normally, or %FALSE if all keyboard events will be reported with
 *  respect to the grab item.
 * @time: the time of the event that lead to the keyboard grab. This should
 *  come from the relevant #GdkEvent.
 * 
 * Attempts to grab the keyboard for the given item.
 * 
 * Returns: %GDK_GRAB_SUCCESS if the grab succeeded.
 **/
GdkGrabStatus
goo_canvas_keyboard_grab (GooCanvas     *canvas,
			  GooCanvasItem *item,
			  gboolean       owner_events,
			  guint32        time)
{
  GdkGrabStatus status = GDK_GRAB_SUCCESS;

  g_return_val_if_fail (GOO_IS_CANVAS (canvas), GDK_GRAB_NOT_VIEWABLE);
  g_return_val_if_fail (GOO_IS_CANVAS_ITEM (item), GDK_GRAB_NOT_VIEWABLE);

  /* Check if the item already has the keyboard grab. */
  if (canvas->keyboard_grab_item == item)
    return GDK_GRAB_ALREADY_GRABBED;

  /* If another item already has the keyboard grab, we need to synthesize a
     grab-broken event for the current grab item. */
  if (canvas->keyboard_grab_item)
    {
      generate_grab_broken (canvas, canvas->keyboard_grab_item, TRUE, FALSE);
      set_item_pointer (&canvas->keyboard_grab_item, NULL);
    }

  /* This overrides any existing grab. */
  status = gdk_keyboard_grab (canvas->canvas_window,
			      owner_events, time);

  if (status == GDK_GRAB_SUCCESS)
    set_item_pointer (&canvas->keyboard_grab_item, item);

  return status;
}


/**
 * goo_canvas_keyboard_ungrab:
 * @canvas: a #GooCanvas.
 * @item: the item that has the keyboard grab.
 * @time: the time of the event that lead to the keyboard ungrab. This should
 *  come from the relevant #GdkEvent.
 * 
 * Ungrabs the keyboard, if the given item has the keyboard grab.
 **/
void
goo_canvas_keyboard_ungrab (GooCanvas     *canvas,
			    GooCanvasItem *item,
			    guint32        time)
{
  GdkDisplay *display;

  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_ITEM (item));

  /* If the item doesn't actually have the keyboard grab, just return. */
  if (canvas->keyboard_grab_item != item)
    return;

  set_item_pointer (&canvas->keyboard_grab_item, NULL);

  display = gtk_widget_get_display (GTK_WIDGET (canvas));
  gdk_display_keyboard_ungrab (display, time);
}


/*
 * Coordinate conversion.
 */

/**
 * goo_canvas_convert_to_pixels:
 * @canvas: a #GooCanvas.
 * @x: a pointer to the x coordinate to convert.
 * @y: a pointer to the y coordinate to convert.
 * 
 * Converts a coordinate from the canvas coordinate space to pixels.
 *
 * The canvas coordinate space is specified in the call to
 * goo_canvas_set_bounds().
 *
 * The pixel coordinate space specifies pixels from the top-left of the entire
 * canvas window, according to the current scale setting.
 * See goo_canvas_set_scale().
 **/
void
goo_canvas_convert_to_pixels (GooCanvas     *canvas,
			      gdouble       *x,
			      gdouble       *y)
{
  *x = ((*x - canvas->bounds.x1) * canvas->device_to_pixels_x) + canvas->canvas_x_offset;
  *y = ((*y - canvas->bounds.y1) * canvas->device_to_pixels_y) + canvas->canvas_y_offset;
}


/**
 * goo_canvas_convert_from_pixels:
 * @canvas: a #GooCanvas.
 * @x: a pointer to the x coordinate to convert.
 * @y: a pointer to the y coordinate to convert.
 * 
 * Converts a coordinate from pixels to the canvas coordinate space.
 *
 * The pixel coordinate space specifies pixels from the top-left of the entire
 * canvas window, according to the current scale setting.
 * See goo_canvas_set_scale().
 *
 * The canvas coordinate space is specified in the call to
 * goo_canvas_set_bounds().
 *
 **/
void
goo_canvas_convert_from_pixels (GooCanvas     *canvas,
				gdouble       *x,
				gdouble       *y)
{
  *x = ((*x - canvas->canvas_x_offset) / canvas->device_to_pixels_x) + canvas->bounds.x1;
  *y = ((*y - canvas->canvas_y_offset) / canvas->device_to_pixels_y) + canvas->bounds.y1;
}


static void
goo_canvas_convert_from_window_pixels (GooCanvas     *canvas,
				       gdouble       *x,
				       gdouble       *y)
{
  *x += canvas->hadjustment->value;
  *y += canvas->vadjustment->value;
  goo_canvas_convert_from_pixels (canvas, x, y);
}


/**
 * goo_canvas_convert_to_item_space:
 * @canvas: a #GooCanvas.
 * @item: a #GooCanvasItem.
 * @x: a pointer to the x coordinate to convert.
 * @y: a pointer to the y coordinate to convert.
 * 
 * Converts a coordinate from the canvas coordinate space to the given
 * item's coordinate space, applying all transformation matrices including the
 * item's own transformation matrix, if it has one.
 **/
void
goo_canvas_convert_to_item_space (GooCanvas     *canvas,
				  GooCanvasItem *item,
				  gdouble       *x,
				  gdouble       *y)
{
  GooCanvasItem *tmp = item, *parent, *child;
  GList *list = NULL, *l;
  cairo_matrix_t item_transform, inverse = { 1, 0, 0, 1, 0, 0 };
  gboolean has_transform;

  /* Step up from the item to the top, pushing items onto the list. */
  while (tmp)
    {
      list = g_list_prepend (list, tmp);
      tmp = goo_canvas_item_get_parent (tmp);
    }

  /* Now step down applying the inverse of each item's transformation. */
  for (l = list; l; l = l->next)
    {
      parent = (GooCanvasItem*) l->data;
      child = l->next ? (GooCanvasItem*) l->next->data : NULL;
      has_transform = goo_canvas_item_get_transform_for_child (parent, child,
							       &item_transform);
      if (has_transform)
	{
	  cairo_matrix_invert (&item_transform);
	  cairo_matrix_multiply (&inverse, &inverse, &item_transform);
	}
    }
  g_list_free (list);

  /* Now convert the coordinates. */
  cairo_matrix_transform_point (&inverse, x, y);
}


/**
 * goo_canvas_convert_from_item_space:
 * @canvas: a #GooCanvas.
 * @item: a #GooCanvasItem.
 * @x: a pointer to the x coordinate to convert.
 * @y: a pointer to the y coordinate to convert.
 * 
 * Converts a coordinate from the given item's coordinate space to the canvas
 * coordinate space, applying all transformation matrices including the
 * item's own transformation matrix, if it has one.
 **/
void
goo_canvas_convert_from_item_space (GooCanvas     *canvas,
				    GooCanvasItem *item,
				    gdouble       *x,
				    gdouble       *y)
{
  GooCanvasItem *tmp = item, *parent, *child;
  GList *list = NULL, *l;
  cairo_matrix_t item_transform, transform = { 1, 0, 0, 1, 0, 0 };
  gboolean has_transform;

  /* Step up from the item to the top, pushing items onto the list. */
  while (tmp)
    {
      list = g_list_prepend (list, tmp);
      tmp = goo_canvas_item_get_parent (tmp);
    }

  /* Now step down applying each item's transformation. */
  for (l = list; l; l = l->next)
    {
      parent = (GooCanvasItem*) l->data;
      child = l->next ? (GooCanvasItem*) l->next->data : NULL;
      has_transform = goo_canvas_item_get_transform_for_child (parent, child,
							       &item_transform);
      if (has_transform)
	{
	  cairo_matrix_multiply (&transform, &transform, &item_transform);
	}
    }
  g_list_free (list);

  /* Now convert the coordinates. */
  cairo_matrix_transform_point (&transform, x, y);
}


/*
 * Keyboard focus navigation.
 */

typedef struct _GooCanvasFocusData GooCanvasFocusData;
struct _GooCanvasFocusData
{
  /* The item to start from, usually the currently focused item, or NULL. */
  GooCanvasItem *start_item;

  /* The bounds of the start item. We try to find the next closest one in the
     desired direction. */
  GooCanvasBounds start_bounds;
  gdouble start_center_x, start_center_y;

  /* The direction to move the focus in. */
  GtkDirectionType  direction;

  /* The text direction of the widget. */
  GtkTextDirection text_direction;

  /* The best item found so far, and the offsets for it. */
  GooCanvasItem *best_item;
  gdouble best_x_offset, best_y_offset, best_score;

  /* The offsets for the item being tested. */
  GooCanvasBounds current_bounds;
  gdouble current_x_offset, current_y_offset, current_score;
};


/* This tries to figure out the bounds of the start item or widget. */
static void
goo_canvas_get_start_bounds (GooCanvas          *canvas,
			     GooCanvasFocusData *data)
{
  GooCanvasBounds *bounds;
  GtkWidget *toplevel, *focus_widget;
  GtkAllocation *allocation;
  gint focus_widget_x, focus_widget_y;

  /* If an item is currently focused, we just need its bounds. */
  if (data->start_item)
    {
      goo_canvas_item_get_bounds (data->start_item, &data->start_bounds);
      return;
    }

  /* Otherwise try to get the currently focused widget in the window. */
  toplevel = gtk_widget_get_toplevel (GTK_WIDGET (canvas));
  bounds = &data->start_bounds;
  if (toplevel && GTK_IS_WINDOW (toplevel)
      && GTK_WINDOW (toplevel)->focus_widget)
    {
      focus_widget = GTK_WINDOW (toplevel)->focus_widget;

      /* Translate the allocation to be relative to the GooCanvas.
	 Skip ancestor widgets as the coords won't help. */
      if (!gtk_widget_is_ancestor (GTK_WIDGET (canvas), focus_widget)
	  && gtk_widget_translate_coordinates (focus_widget,
					       GTK_WIDGET (canvas),
					       0, 0,
					       &focus_widget_x,
					       &focus_widget_y))
	{
	  /* Translate into device units. */
	  bounds->x1 = focus_widget_x;
	  bounds->y1 = focus_widget_y;
	  bounds->x2 = focus_widget_x + focus_widget->allocation.width;
	  bounds->y2 = focus_widget_y + focus_widget->allocation.height;

	  goo_canvas_convert_from_window_pixels (canvas, &bounds->x1,
						 &bounds->y1);
	  goo_canvas_convert_from_window_pixels (canvas, &bounds->x2,
						 &bounds->y2);
	  return;
	}
    }

  /* As a last resort, we guess a starting position based on the direction. */
  allocation = &GTK_WIDGET (canvas)->allocation;
  switch (data->direction)
    {
    case GTK_DIR_DOWN:
    case GTK_DIR_RIGHT:
      /* Start from top-left. */
      bounds->x1 = 0.0;
      bounds->y1 = 0.0;
      break;

    case GTK_DIR_UP:
      /* Start from bottom-left. */
      bounds->x1 = 0.0;
      bounds->y1 = allocation->height;
      break;

    case GTK_DIR_LEFT:
      /* Start from top-right. */
      bounds->x1 = allocation->width;
      bounds->y1 = 0.0;
      break;

    case GTK_DIR_TAB_FORWARD:
      bounds->y1 = 0.0;
      if (data->text_direction == GTK_TEXT_DIR_RTL)
	/* Start from top-right. */
	bounds->x1 = allocation->width;
      else
	/* Start from top-left. */
	bounds->x1 = 0.0;
      break;

    case GTK_DIR_TAB_BACKWARD:
      bounds->y1 = allocation->height;
      if (data->text_direction == GTK_TEXT_DIR_RTL)
	/* Start from bottom-left. */
	bounds->x1 = 0.0;
      else
	/* Start from bottom-right. */
	bounds->x1 = allocation->width;
      break;
    }

  goo_canvas_convert_from_window_pixels (canvas, &bounds->x1, &bounds->y1);
  bounds->x2 = bounds->x1;
  bounds->y2 = bounds->y1;
}


/* Check if the given item is a better candidate for the focus than
   the current best one in the data struct. */
static gboolean
goo_canvas_focus_check_is_best (GooCanvas          *canvas,
				GooCanvasItem      *item,
				GooCanvasFocusData *data)
{
  gdouble center_x, center_y;
  gdouble abs_x_offset = 0.0, abs_y_offset = 0.0;

  data->current_score = 0.0;

  goo_canvas_item_get_bounds (item, &data->current_bounds);
  center_x = (data->current_bounds.x1 + data->current_bounds.x2) / 2.0;
  center_y = (data->current_bounds.y1 + data->current_bounds.y2) / 2.0;

  /* Calculate the offsets of the center of this item from the center
     of the current focus item or widget. */
  data->current_x_offset = center_x - data->start_center_x;
  data->current_y_offset = center_y - data->start_center_y;

  /* If the item overlaps the current one at all we use 0 as the offset. */
  if (data->current_bounds.x1 > data->start_bounds.x2
      || data->current_bounds.x2 < data->start_bounds.x2)
      abs_x_offset = fabs (data->current_x_offset);

  if (data->current_bounds.y1 > data->start_bounds.y2
      || data->current_bounds.y2 < data->start_bounds.y2)
      abs_y_offset = fabs (data->current_y_offset);

  /* FIXME: I'm still not sure about the score calculations here. There
     are still a few odd jumps when using keyboard focus navigation. */
  switch (data->direction)
    {
    case GTK_DIR_UP:
      /* If the y offset is > 0 we can discard this item. */
      if (data->current_y_offset >= 0 || abs_x_offset > abs_y_offset)
	return FALSE;

      /* Compute a score (lower is best) and check if it is the best. */
      data->current_score = abs_x_offset * 2 + abs_y_offset;
      if (!data->best_item || data->current_score < data->best_score)
	return TRUE;
      break;

    case GTK_DIR_DOWN:
      /* If the y offset is < 0 we can discard this item. */
      if (data->current_y_offset <= 0 || abs_x_offset > abs_y_offset)
	return FALSE;

      /* Compute a score (lower is best) and check if it is the best. */
      data->current_score = abs_x_offset /** 2*/ + abs_y_offset;
      if (!data->best_item || data->current_score < data->best_score)
	return TRUE;
      break;

    case GTK_DIR_LEFT:
      /* If the x offset is > 0 we can discard this item. */
      if (data->current_x_offset >= 0 || abs_y_offset > abs_x_offset)
	return FALSE;

      /* Compute a score (lower is best) and check if it is the best. */
      data->current_score = abs_y_offset * 2 + abs_x_offset;
      if (!data->best_item || data->current_score < data->best_score)
	return TRUE;
      break;

    case GTK_DIR_RIGHT:
      /* If the x offset is < 0 we can discard this item. */
      if (data->current_x_offset <= 0 || abs_y_offset > abs_x_offset)
	return FALSE;

      /* Compute a score (lower is best) and check if it is the best. */
      data->current_score = abs_y_offset * 2 + abs_x_offset;
      if (!data->best_item || data->current_score < data->best_score)
	return TRUE;
      break;

    case GTK_DIR_TAB_BACKWARD:
      /* We need to handle this differently depending on text direction. */
      if (data->text_direction == GTK_TEXT_DIR_RTL)
	{
	  /* If the y offset is > 0, or it is 0 and the x offset > 0 we can
	     discard this item. */
	  if (data->current_y_offset > 0
	      || (data->current_y_offset == 0 && data->current_x_offset < 0))
	    return FALSE;

	  /* If the y offset is > the current best y offset, this is best. */
	  if (!data->best_item || data->current_y_offset > data->best_y_offset)
	    return TRUE;

	  /* If the y offsets are the same, choose the largest x offset. */
	  if (data->current_y_offset == data->best_y_offset
	      && data->current_x_offset < data->best_x_offset)
	    return TRUE;
	}
      else
	{
	  /* If the y offset is > 0, or it is 0 and the x offset > 0 we can
	     discard this item. */
	  if (data->current_y_offset > 0
	      || (data->current_y_offset == 0 && data->current_x_offset > 0))
	    return FALSE;

	  /* If the y offset is > the current best y offset, this is best. */
	  if (!data->best_item || data->current_y_offset > data->best_y_offset)
	    return TRUE;

	  /* If the y offsets are the same, choose the largest x offset. */
	  if (data->current_y_offset == data->best_y_offset
	      && data->current_x_offset > data->best_x_offset)
	    return TRUE;
	}
      break;

    case GTK_DIR_TAB_FORWARD:
      /* We need to handle this differently depending on text direction. */
      if (data->text_direction == GTK_TEXT_DIR_RTL)
	{
	  /* If the y offset is < 0, or it is 0 and the x offset > 0 we can
	     discard this item. */
	  if (data->current_y_offset < 0
	      || (data->current_y_offset == 0 && data->current_x_offset > 0))
	    return FALSE;

	  /* If the y offset is < the current best y offset, this is best. */
	  if (!data->best_item || data->current_y_offset < data->best_y_offset)
	    return TRUE;

	  /* If the y offsets are the same, choose the largest x offset. */
	  if (data->current_y_offset == data->best_y_offset
	      && data->current_x_offset > data->best_x_offset)
	    return TRUE;
	}
      else
	{
	  /* If the y offset is < 0, or it is 0 and the x offset < 0 we can
	     discard this item. */
	  if (data->current_y_offset < 0
	      || (data->current_y_offset == 0 && data->current_x_offset < 0))
	    return FALSE;

	  /* If the y offset is < the current best y offset, this is best. */
	  if (!data->best_item || data->current_y_offset < data->best_y_offset)
	    return TRUE;

	  /* If the y offsets are the same, choose the smallest x offset. */
	  if (data->current_y_offset == data->best_y_offset
	      && data->current_x_offset < data->best_x_offset)
	    return TRUE;
	}
      break;
    }

  return FALSE;
}


/* Recursively look for the next best item in the desired direction. */
static void
goo_canvas_focus_recurse (GooCanvas          *canvas,
			  GooCanvasItem      *item,
			  GooCanvasFocusData *data)
{
  GooCanvasItem *child;
  gboolean can_focus = FALSE, is_best;
  gint n_children, i;

  /* If the item is not a possible candidate, just return. */
  is_best = goo_canvas_focus_check_is_best (canvas, item, data);

  if (is_best && goo_canvas_item_is_visible (item))
    {
      /* Check if the item can take the focus. */
      if (GOO_IS_CANVAS_WIDGET (item))
	{
	  /* We have to assume that widget items can take focus, since any
	     of their descendants may take the focus. */
	  if (((GooCanvasWidget*) item)->widget)
	    can_focus = TRUE;
	}
      else
	{
	  g_object_get (item, "can-focus", &can_focus, NULL);
	}

      /* If the item can take the focus itself, and it isn't the current
	 focus item, save its score and return. (If it is a container it takes
	 precedence over its children). */
      if (can_focus && item != data->start_item)
	{
	  data->best_item = item;
	  data->best_x_offset = data->current_x_offset;
	  data->best_y_offset = data->current_y_offset;
	  data->best_score = data->current_score;
	  return;
	}
    }

  /* If the item is a container, check the children recursively. */
  n_children = goo_canvas_item_get_n_children (item);
  if (n_children)
    {
      /* Check if we can skip the entire group. */
      switch (data->direction)
	{
	case GTK_DIR_UP:
	  /* If the group is below the bottom of the current focused item
	     we can skip it. */
	  if (data->current_bounds.y1 > data->start_bounds.y2)
	    return;
	  break;
	case GTK_DIR_DOWN:
	  /* If the group is above the top of the current focused item
	     we can skip it. */
	  if (data->current_bounds.y2 < data->start_bounds.y1)
	    return;
	  break;
	case GTK_DIR_LEFT:
	  /* If the group is to the right of the current focused item
	     we can skip it. */
	  if (data->current_bounds.x1 > data->start_bounds.x2)
	    return;
	  break;
	case GTK_DIR_RIGHT:
	  /* If the group is to the left of the current focused item
	     we can skip it. */
	  if (data->current_bounds.x2 < data->start_bounds.x1)
	    return;
	  break;
	default:
	  break;
	}

      for (i = 0; i < n_children; i++)
	{
	  child = goo_canvas_item_get_child (item, i);
	  goo_canvas_focus_recurse (canvas, child, data);
	}
    }
}


/* FIXME: We could add support for a focus chain, like GtkContainer. */
static gboolean
goo_canvas_focus (GtkWidget        *widget,
		  GtkDirectionType  direction)
{
  GooCanvas *canvas;
  GooCanvasFocusData data;
  GtkWidget *old_focus_child;
  gboolean found_item;
  gint try;

  g_return_val_if_fail (GOO_IS_CANVAS (widget), FALSE);

  canvas = GOO_CANVAS (widget);

  /* If keyboard navigation has been turned off for the canvas, return FALSE.*/
  if (!GTK_WIDGET_CAN_FOCUS (canvas))
    return FALSE;

  /* If a child widget has the focus, try moving the focus within that. */
  old_focus_child = GTK_CONTAINER (canvas)->focus_child;
  if (old_focus_child && gtk_widget_child_focus (old_focus_child, direction))
    return TRUE;

  data.direction = direction;
  data.text_direction = gtk_widget_get_direction (widget);
  data.start_item = NULL;

  if (GTK_WIDGET_HAS_FOCUS (canvas))
    data.start_item = canvas->focused_item;
  else if (old_focus_child && GOO_IS_CANVAS_WIDGET (old_focus_child))
    data.start_item = g_object_get_data (G_OBJECT (old_focus_child),
					 "goo-canvas-item");

  /* Keep looping until we find an item to focus or we fail. I've added a
     limit on the number of tries just in case we get into an infinite loop. */
  for (try = 1; try < 1000; try++)
    {
      /* Get the bounds of the currently focused item or widget. */
      goo_canvas_get_start_bounds (canvas, &data);
      data.start_center_x = (data.start_bounds.x1 + data.start_bounds.x2) / 2.0;
      data.start_center_y = (data.start_bounds.y1 + data.start_bounds.y2) / 2.0;
      data.best_item = NULL;

      /* Recursively look for the next best item in the desired direction. */
      goo_canvas_focus_recurse (canvas, canvas->root_item, &data);

      /* If we found an item to focus, grab the focus and return TRUE. */
      if (!data.best_item)
	break;

      if (GOO_IS_CANVAS_WIDGET (data.best_item))
	{
	  found_item = gtk_widget_child_focus (((GooCanvasWidget*) data.best_item)->widget, direction);
	}
      else
	{
	  found_item = TRUE;
	  goo_canvas_grab_focus (canvas, data.best_item);
	}
      
      if (found_item)
	{
	  goo_canvas_scroll_to_item (canvas, data.best_item);
	  return TRUE;
	}

      /* Try again from the last item tried. */
      data.start_item = data.best_item;
    }

  return FALSE;
}


/**
 * goo_canvas_register_widget_item:
 * @canvas: a #GooCanvas.
 * @witem: a #GooCanvasWidget item.
 * 
 * This function should only be used by #GooCanvasWidget and subclass
 * implementations.
 *
 * It registers a widget item with the canvas, so that the canvas can do the
 * necessary actions to move and resize the widget as needed.
 **/
void
goo_canvas_register_widget_item   (GooCanvas          *canvas,
				   GooCanvasWidget    *witem)
{
  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_WIDGET (witem));

  canvas->widget_items = g_list_append (canvas->widget_items, witem);
}


/**
 * goo_canvas_unregister_widget_item:
 * @canvas: a #GooCanvas.
 * @witem: a #GooCanvasWidget item.
 * 
 * This function should only be used by #GooCanvasWidget and subclass
 * implementations.
 *
 * It unregisters a widget item from the canvas, when the item is no longer in
 * the canvas.
 **/
void
goo_canvas_unregister_widget_item (GooCanvas          *canvas,
				   GooCanvasWidget    *witem)
{
  GList *tmp_list;
  GooCanvasWidget *tmp_witem;

  g_return_if_fail (GOO_IS_CANVAS (canvas));
  g_return_if_fail (GOO_IS_CANVAS_WIDGET (witem));

  tmp_list = canvas->widget_items;
  while (tmp_list)
    {
      tmp_witem = tmp_list->data;
      if (tmp_witem == witem)
	break;
      tmp_list = tmp_list->next;
    }

  if (tmp_list)
    {
      canvas->widget_items = g_list_remove_link (canvas->widget_items,
						 tmp_list);
      g_list_free_1 (tmp_list);
    }
}


static void
goo_canvas_forall (GtkContainer *container,
		   gboolean      include_internals,
		   GtkCallback   callback,
		   gpointer      callback_data)
{
  GooCanvas *canvas;
  GList *tmp_list;
  GooCanvasWidget *witem;

  g_return_if_fail (GOO_IS_CANVAS (container));
  g_return_if_fail (callback != NULL);

  canvas = GOO_CANVAS (container);

  tmp_list = canvas->widget_items;
  while (tmp_list)
    {
      witem = tmp_list->data;
      tmp_list = tmp_list->next;

      if (witem->widget)
	(* callback) (witem->widget, callback_data);
    }
}


static void
goo_canvas_remove (GtkContainer *container, 
		   GtkWidget    *widget)
{
  GooCanvas *canvas;
  GList *tmp_list;
  GooCanvasWidget *witem;
  GooCanvasItem *parent;
  gint child_num;
  
  g_return_if_fail (GOO_IS_CANVAS (container));
  
  canvas = GOO_CANVAS (container);

  tmp_list = canvas->widget_items;
  while (tmp_list)
    {
      witem = tmp_list->data;
      tmp_list = tmp_list->next;

      if (witem->widget == widget)
	{
	  parent = goo_canvas_item_get_parent ((GooCanvasItem*) witem);
	  child_num = goo_canvas_item_find_child (parent,
						  (GooCanvasItem*) witem);
	  goo_canvas_item_remove_child (parent, child_num);

	  break;
	}
    }
}