summaryrefslogtreecommitdiff
path: root/tools/glade/glade/editor.c
blob: 424c9e43b26eb29622704210de7b46b3123ab387 (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
/*  Gtk+ User Interface Builder
 *  Copyright (C) 1998  Damon Chaplin
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 */

#include <string.h>

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

#include "gladeconfig.h"

#ifdef USE_GNOME
#include <gnome.h>
#endif

#include "glade_clipboard.h"
#include "glade_palette.h"
#include "glade_project.h"
#include "glade_project_window.h"
#include "property.h"
#include "gbwidget.h"
#include "utils.h"
#include "editor.h"
#include "tree.h"
#include "gb.h"

/* The pixmap to use for placeholders */
#include "graphics/placeholder.xpm"

#define MIN_WIDGET_WIDTH		16
#define MIN_WIDGET_HEIGHT		16
#define MAX_INITIAL_WIDGET_WIDTH	300
#define MAX_INITIAL_WIDGET_HEIGHT	200
#define DEFAULT_WIDGET_WIDTH		50
#define DEFAULT_WIDGET_HEIGHT		50

#define PLACEHOLDER_WIDTH  16
#define PLACEHOLDER_HEIGHT 16


/* The grid (for fixed containers) */
#define GB_GRID_DOTS	1
#define GB_GRID_LINES	2
gboolean editor_show_grid = TRUE;
static gint editor_grid_horz_spacing = 8;
static gint editor_grid_vert_spacing = 8;
static gboolean editor_grid_style = GB_GRID_DOTS;


/* Snapping to the grid */
#define GB_SNAP_TOP	1 << 1
#define GB_SNAP_BOTTOM	1 << 2
#define GB_SNAP_LEFT	1 << 3
#define GB_SNAP_RIGHT	1 << 4
/*#define GB_SNAP_CENTER        1 << 5  maybe in future */
gboolean editor_snap_to_grid = TRUE;
static gint editor_snap_to_grid_x = GB_SNAP_LEFT | GB_SNAP_RIGHT;
static gint editor_snap_to_grid_y = GB_SNAP_TOP | GB_SNAP_BOTTOM;


/* Dragging (in a fixed container) - remembers which part of the widget is
   being dragged, the offset of the mouse (used when moving) and the initial
   widget rectangle (used when resizing) */
#define GB_DRAG_NONE    1
#define GB_TOP_LEFT     2
#define GB_TOP_RIGHT    3
#define GB_BOTTOM_LEFT  4
#define GB_BOTTOM_RIGHT 5
#define GB_MIDDLE       6
static gint drag_action;
static gboolean drag_has_pointer_grab = FALSE;
static GtkWidget *dragging_widget = NULL;
static gint drag_offset_x;
static gint drag_offset_y;
static gint drag_widget_x1, drag_widget_y1, drag_widget_x2, drag_widget_y2;

/* The list of selected widgets */
static GList *selected_widgets = NULL;

/* The cursors used when selecting/adding/moving/resizing widgets */
static GdkCursor *cursor_selector;
static GdkCursor *cursor_add_widget;
static GdkCursor *cursor_add_to_fixed;
static GdkCursor *cursor_move;
static GdkCursor *cursor_top_left;
static GdkCursor *cursor_top_right;
static GdkCursor *cursor_bottom_left;
static GdkCursor *cursor_bottom_right;

/* Struct only used for find_child_at callback */
typedef struct _GbFindChildAtData GbFindChildAtData;
struct _GbFindChildAtData
  {
    gint x;
    gint y;
    GtkWidget *found_child;
  };

/* Experimental code to allow typing labels for widgets when the mouse is
   over them. */
GtkWidget *mouse_over_widget = NULL;


/* Static functions */
static void editor_on_widget_realize (GtkWidget *widget,
				      gpointer data);

static gint editor_on_key_press_event (GtkWidget * widget,
				       GdkEventKey * event,
				       gpointer data);
static gint editor_on_key_release_event (GtkWidget * widget,
					 GdkEventKey * event,
					 gpointer data);

static void add_mouse_signals_recursive (GtkWidget *widget,
					 gpointer data);
static gint editor_on_button_press (GtkWidget * signal_widget,
				    GdkEventButton * event,
				    gpointer data);
static gint editor_on_button_release (GtkWidget * widget,
				      GdkEvent * event,
				      gpointer data);

static gboolean editor_check_ignore_event (GtkWidget *widget,
					   GdkEventAny *event);

static gint editor_on_motion_notify (GtkWidget * signal_widget,
				     GdkEventMotion * event,
				     gpointer data);
static gint editor_set_cursor (GtkWidget * signal_widget,
			       GdkEventMotion * event);
static gint editor_do_drag_action (GtkWidget * signal_widget,
				   GdkEventMotion * event);

static gint editor_on_enter_notify (GtkWidget        *widget,
				    GdkEventCrossing *event);
static gint editor_on_leave_notify (GtkWidget        *widget,
				    GdkEventCrossing *event);

static void placeholder_replace (GtkWidget * placeholder);
static void placeholder_finish_replace (GtkWidget * new_widget,
					GbWidgetNewData * data);
static void on_placeholder_destroy (GtkWidget * widget,
				    gpointer data);
static void on_placeholder_size_request (GtkWidget * widget,
					 GtkRequisition *requisition,
					 gpointer data);

static void add_widget_to_fixed (GtkWidget * parent,
				 gint x,
				 gint y);
static void add_widget_to_fixed_finish (GtkWidget * new_widget,
					GbWidgetNewData * data);
#if GLADE_SUPPORTS_GTK_PACKER
static void add_widget_to_container (GtkWidget * parent);
static void add_widget_to_container_finish (GtkWidget * new_widget,
					    GbWidgetNewData * data);
#endif
static gint expose_widget (GtkWidget * widget,
			   GdkEventExpose * event,
			   GladeWidgetData * wdata);
static void draw_grid (GtkWidget * widget);
static void paint_widget (GtkWidget * widget, GdkEventExpose *event);
static void clear_child_windows (GdkWindow * window,
				 gint x,
				 gint y,
				 gint w,
				 gint h);
static gint editor_idle_handler (GdkWindow *expose_win);

static void delete (GtkWidget * widget);
static void delete_placeholder (GtkWidget * widget);

static void on_grid_settings_response (GtkWidget * widget,
				       gint response_id,
				       gpointer data);
static void editor_redraw_component (GtkWidget * widget,
				     gpointer data);

static void on_snap_settings_response (GtkWidget * widget,
				       gint response_id,
				       gpointer data);
static gint snap_top_edge (gint y);
static gint snap_bottom_edge (gint y);
static gint snap_left_edge (gint x);
static gint snap_right_edge (gint x);

static gint get_position_in_widget (GtkWidget * widget,
				    gint x,
				    gint y);
static void raise_fixed_child (GtkWidget * widget);

static void find_child_at (GtkWidget * widget,
			   GbFindChildAtData * data);


#if 0
/* This is probably what we should have used to get all button presses and
   releases on widget etc. But it may be too awkward to change now.
   Note that we'll also get all signals for Glade's own widgets.

   Actually I don't think this would help that much, since emission hooks
   are run after RUN_FIRST class handlers, and you can't stop a signal from
   them.

   So we'd have to substitute a Glade function for all 'button_press' class
   functions, and only call the original class functions when appropriate.
   We'd still need to be able to stop signal handlers. Hmm. */
static gboolean
button_press_event_hook (GSignalInvocationHint *ihint,
			 guint                  n_param_values,
			 const GValue          *param_values,
			 gpointer               data)
{
  GtkWidget *widget;
  GdkEventButton *event;

  widget = g_value_get_object (param_values + 0);
  event = g_value_get_boxed (param_values + 1);

  g_print ("Watch: \"%s\" emitted for %s at %g,%g\n",
	   gtk_signal_name (ihint->signal_id),
	   gtk_type_name (GTK_OBJECT_TYPE (widget)),
	   event->x, event->y);

  /* Test: try to stop the emission. Can't! */
  /*g_signal_stop_emission (widget, ihint->signal_id, 0);*/


  /* If we return FALSE our emission hook is removed. */
  return TRUE;
}


static gboolean
button_release_event_hook (GSignalInvocationHint *ihint,
			   guint                  n_param_values,
			   const GValue          *param_values,
			   gpointer               data)
{
  GtkWidget *widget;
  GdkEvent *event;

  widget = g_value_get_object (param_values + 0);
  event = g_value_get_boxed (param_values + 1);

  g_print ("Watch: \"%s\" emitted for %s type:%i\n",
	   gtk_signal_name (ihint->signal_id),
	   gtk_type_name (GTK_OBJECT_TYPE (widget)),
	   event->type);

  /* If we return FALSE our emission hook is removed. */
  return TRUE;
}
#endif

void
editor_init ()
{
  /* Create all cursors needed */
  cursor_selector = gdk_cursor_new (GDK_TOP_LEFT_ARROW);
  cursor_add_widget = gdk_cursor_new (GDK_PLUS);
  cursor_add_to_fixed = gdk_cursor_new (GDK_TCROSS);
  cursor_move = gdk_cursor_new (GDK_FLEUR);
  cursor_top_left = gdk_cursor_new (GDK_TOP_LEFT_CORNER);
  cursor_top_right = gdk_cursor_new (GDK_TOP_RIGHT_CORNER);
  cursor_bottom_left = gdk_cursor_new (GDK_BOTTOM_LEFT_CORNER);
  cursor_bottom_right = gdk_cursor_new (GDK_BOTTOM_RIGHT_CORNER);

#if 0
 {
   guint button_press_signal_id, button_release_signal_id;

   button_press_signal_id = gtk_signal_lookup ("button_press_event",
					       GTK_TYPE_WIDGET);
   g_signal_add_emission_hook (button_press_signal_id, 0,
			       button_press_event_hook, NULL, NULL);
   
   button_release_signal_id = gtk_signal_lookup ("button_release_event",
						 GTK_TYPE_WIDGET);
   g_signal_add_emission_hook (button_release_signal_id, 0,
			       button_release_event_hook, NULL, NULL);
 }
#endif
}

gint
editor_close_window (GtkWidget * widget,
		     GdkEvent * event,
		     gpointer data)
{
  glade_util_close_window (widget);
  return TRUE;
}


/*
 * Grid settings dialog
 */
gboolean
editor_get_show_grid ()
{
  return editor_show_grid;
}


void
editor_set_show_grid (gboolean show)
{
  if (editor_show_grid == show)
    return;

  editor_show_grid = show;
  if (current_project != NULL)
    glade_project_foreach_component (current_project, editor_redraw_component,
				     NULL);
}


void
editor_show_grid_settings_dialog (GtkWidget *widget)
{
  GtkWidget *dialog, *vbox, *table, *label, *button, *spinbutton;
  GtkObject *adjustment;
  GSList *group;
  GtkWindow *transient_parent;

  transient_parent = (GtkWindow*) glade_util_get_toplevel (widget);
  dialog = gtk_dialog_new_with_buttons (_("Grid Options"), transient_parent, 0,
					GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					GTK_STOCK_OK, GTK_RESPONSE_OK,
					NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  gtk_window_set_wmclass (GTK_WINDOW (dialog), "grid_options", "Glade");

  vbox = GTK_DIALOG (dialog)->vbox;

  table = gtk_table_new (2, 3, FALSE);
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 5);
  gtk_widget_show (table);

  label = gtk_label_new (_("Horizontal Spacing:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_FILL, 0, 5, 5);
  gtk_widget_show (label);

  adjustment = gtk_adjustment_new (editor_grid_horz_spacing, 1, 1000, 1,
				   10, 10);
  spinbutton = glade_util_spin_button_new (GTK_OBJECT (dialog), "spinbutton1",
					   GTK_ADJUSTMENT (adjustment), 0, 0);
  gtk_table_attach (GTK_TABLE (table), spinbutton, 1, 3, 0, 1,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (spinbutton);
  gtk_widget_grab_focus (spinbutton);

  label = gtk_label_new (_("Vertical Spacing:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 1, 2,
		    GTK_FILL, 0, 5, 5);
  gtk_widget_show (label);

  adjustment = gtk_adjustment_new (editor_grid_vert_spacing, 1, 1000, 1,
				   10, 10);
  spinbutton = glade_util_spin_button_new (GTK_OBJECT (dialog), "spinbutton2",
					   GTK_ADJUSTMENT (adjustment), 0, 0);
  gtk_table_attach (GTK_TABLE (table), spinbutton, 1, 3, 1, 2,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (spinbutton);

  table = gtk_table_new (1, 3, FALSE);
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 0);
  gtk_widget_show (table);

  label = gtk_label_new (_("Grid Style:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_FILL, 0, 5, 5);
  gtk_widget_show (label);

  button = gtk_radio_button_new_with_label (NULL, _("Dots"));
  if (editor_grid_style == GB_GRID_DOTS)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 0, 1,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button1", button);
  group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (button));

  button = gtk_radio_button_new_with_label (group, _("Lines"));
  if (editor_grid_style == GB_GRID_LINES)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 2, 3, 0, 1,
		    GTK_EXPAND | GTK_FILL, 0, 5, 1);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button2", button);

  gtk_signal_connect (GTK_OBJECT (dialog), "response",
		      GTK_SIGNAL_FUNC (on_grid_settings_response),
		      NULL);

  gtk_widget_show (dialog);
}


static void
on_grid_settings_response (GtkWidget * widget, gint response_id, gpointer data)
{
  GtkWidget *dialog, *spinbutton, *button;

  dialog = gtk_widget_get_toplevel (widget);

  if (response_id != GTK_RESPONSE_OK)
    {
      gtk_widget_destroy (dialog);
      return;
    }

  spinbutton = gtk_object_get_data (GTK_OBJECT (dialog), "spinbutton1");
  editor_grid_horz_spacing = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
							       (spinbutton));
  spinbutton = gtk_object_get_data (GTK_OBJECT (dialog), "spinbutton2");
  editor_grid_vert_spacing = gtk_spin_button_get_value_as_int (GTK_SPIN_BUTTON
							       (spinbutton));

  button = gtk_object_get_data (GTK_OBJECT (dialog), "button1");
  if (GTK_TOGGLE_BUTTON (button)->active)
    editor_grid_style = GB_GRID_DOTS;
  else
    editor_grid_style = GB_GRID_LINES;

  gtk_widget_destroy (dialog);

  /* redraw all windows */
  if (current_project != NULL)
    glade_project_foreach_component (current_project, editor_redraw_component,
				     NULL);
}


static void
editor_redraw_component (GtkWidget * widget, gpointer data)
{
  gtk_widget_queue_draw (widget);
}


/*
 * Snap settings
 */
gboolean
editor_get_snap_to_grid ()
{
  return editor_snap_to_grid;
}


void
editor_set_snap_to_grid (gboolean snap)
{
  editor_snap_to_grid = snap;
}


void
editor_show_snap_settings_dialog (GtkWidget *widget)
{
  GtkWidget *dialog, *vbox, *table, *label, *button;
  GtkWindow *transient_parent;

  transient_parent = (GtkWindow*) glade_util_get_toplevel (widget);
  dialog = gtk_dialog_new_with_buttons (_("Snap Options"), transient_parent, 0,
					GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					GTK_STOCK_OK, GTK_RESPONSE_OK,
					NULL);
  gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_OK);
  gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_MOUSE);
  gtk_window_set_wmclass (GTK_WINDOW (dialog), "snap_options", "Glade");

  vbox = GTK_DIALOG (dialog)->vbox;

  table = gtk_table_new (4, 2, FALSE);
  gtk_box_pack_start (GTK_BOX (vbox), table, TRUE, TRUE, 5);
  gtk_widget_show (table);

  /* Horizontal snapping */
  label = gtk_label_new (_("Horizontal Snapping:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 0, 1,
		    GTK_FILL, 0, 5, 5);
  gtk_widget_show (label);

  button = gtk_check_button_new_with_label (_("Left"));
  if (editor_snap_to_grid_x & GB_SNAP_LEFT)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 0, 1,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button1", button);
  gtk_widget_grab_focus (button);

  button = gtk_check_button_new_with_label (_("Right"));
  if (editor_snap_to_grid_x & GB_SNAP_RIGHT)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 1, 2,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button2", button);

  /* Vertical snapping */
  label = gtk_label_new (_("Vertical Snapping:"));
  gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
  gtk_table_attach (GTK_TABLE (table), label, 0, 1, 2, 3,
		    GTK_FILL, 0, 5, 5);
  gtk_widget_show (label);

  button = gtk_check_button_new_with_label (_("Top"));
  if (editor_snap_to_grid_y & GB_SNAP_TOP)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 2, 3,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button3", button);

  button = gtk_check_button_new_with_label (_("Bottom"));
  if (editor_snap_to_grid_y & GB_SNAP_BOTTOM)
    gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (button), TRUE);
  gtk_table_attach (GTK_TABLE (table), button, 1, 2, 3, 4,
		    GTK_EXPAND | GTK_FILL, 0, 5, 5);
  gtk_widget_show (button);
  gtk_object_set_data (GTK_OBJECT (dialog), "button4", button);

  gtk_signal_connect (GTK_OBJECT (dialog), "response",
		      GTK_SIGNAL_FUNC (on_snap_settings_response),
		      NULL);

  gtk_widget_show (dialog);
}


static void
on_snap_settings_response (GtkWidget * widget, gint response_id, gpointer data)
{
  GtkWidget *dialog, *button;

  dialog = gtk_widget_get_toplevel (widget);

  if (response_id != GTK_RESPONSE_OK)
    {
      gtk_widget_destroy (dialog);
      return;
    }

  editor_snap_to_grid_x = 0;
  editor_snap_to_grid_y = 0;
  button = gtk_object_get_data (GTK_OBJECT (dialog), "button1");
  if (GTK_TOGGLE_BUTTON (button)->active)
    editor_snap_to_grid_x |= GB_SNAP_LEFT;
  button = gtk_object_get_data (GTK_OBJECT (dialog), "button2");
  if (GTK_TOGGLE_BUTTON (button)->active)
    editor_snap_to_grid_x |= GB_SNAP_RIGHT;
  button = gtk_object_get_data (GTK_OBJECT (dialog), "button3");
  if (GTK_TOGGLE_BUTTON (button)->active)
    editor_snap_to_grid_y |= GB_SNAP_TOP;
  button = gtk_object_get_data (GTK_OBJECT (dialog), "button4");
  if (GTK_TOGGLE_BUTTON (button)->active)
    editor_snap_to_grid_y |= GB_SNAP_BOTTOM;

  gtk_widget_destroy (dialog);
}


static gint
snap_top_edge (gint y)
{
  if (editor_snap_to_grid && (editor_snap_to_grid_y & GB_SNAP_TOP))
    {
      y += editor_grid_vert_spacing / 2;
      y -= y % editor_grid_vert_spacing;
    }
  return y;
}


static gint
snap_bottom_edge (gint y)
{
  if (editor_snap_to_grid && (editor_snap_to_grid_y & GB_SNAP_BOTTOM))
    {
      y += editor_grid_vert_spacing / 2;
      y -= y % editor_grid_vert_spacing;
    }
  return y;
}


static gint
snap_left_edge (gint x)
{
  if (editor_snap_to_grid && (editor_snap_to_grid_x & GB_SNAP_LEFT))
    {
      x += editor_grid_horz_spacing / 2;
      x -= x % editor_grid_horz_spacing;
    }
  return x;
}


static gint
snap_right_edge (gint x)
{
  if (editor_snap_to_grid && (editor_snap_to_grid_x & GB_SNAP_RIGHT))
    {
      x += editor_grid_horz_spacing / 2;
      x -= x % editor_grid_horz_spacing;
    }
  return x;
}




static void
on_placeholder_size_allocate (GtkWidget * widget,
			      GtkAllocation *allocation,
			      gpointer data)
{
#if 0
  g_print ("In on_placeholder_size_allocate %i,%i %ix%i\n",
	   allocation->x, allocation->y,
	   allocation->width, allocation->height);
#endif
}


GtkWidget *
editor_new_placeholder ()
{
  GtkWidget *placeholder = gtk_drawing_area_new ();
  gtk_widget_set_events (placeholder,
			 gtk_widget_get_events (placeholder)
			 | GDK_EXPOSURE_MASK | GDK_BUTTON_PRESS_MASK
			 | GDK_BUTTON_RELEASE_MASK
			 | GDK_POINTER_MOTION_MASK | GDK_BUTTON1_MOTION_MASK);
  gtk_widget_set_usize (placeholder, PLACEHOLDER_WIDTH, PLACEHOLDER_HEIGHT);
  gtk_widget_show (placeholder);
  editor_add_draw_signals (placeholder);
  editor_add_mouse_signals (placeholder);
  gtk_object_set_data (GTK_OBJECT (placeholder), GB_PLACEHOLDER_KEY, "True");

  /* GnomeDock workaround. */
  gtk_signal_connect_after (GTK_OBJECT (placeholder), "size_request",
			    GTK_SIGNAL_FUNC (on_placeholder_size_request),
			    NULL);

  /* FIXME: Just a test. */
  gtk_signal_connect_after (GTK_OBJECT (placeholder), "size_allocate",
			    GTK_SIGNAL_FUNC (on_placeholder_size_allocate),
			    NULL);


  gtk_signal_connect (GTK_OBJECT (placeholder), "destroy",
		      GTK_SIGNAL_FUNC (on_placeholder_destroy), NULL);
  return placeholder;
}


static void
on_placeholder_destroy (GtkWidget * widget,
			gpointer data)
{
  MSG ("IN on_placeholder_destroy");
  /* If the entire project is being destroyed, we don't need to update the
     selection or the widget tree. */

  /* FIXME: GTK+2 */
  if (!(GTK_OBJECT_FLAGS (current_project) & GTK_IN_DESTRUCTION))
    editor_remove_widget_from_selection (widget);
}


/* This is a gnome-libs 1.0.5 bug workaround - GnomeDockItem doesn't use
   the child requisition properly. */
static void
on_placeholder_size_request (GtkWidget * widget,
			     GtkRequisition *requisition,
			     gpointer data)
{
  MSG ("IN on_placeholder_size_request");
#ifdef USE_GNOME
  if (BONOBO_IS_DOCK_ITEM (widget->parent))
    {
      requisition->width = PLACEHOLDER_WIDTH;
      requisition->height = PLACEHOLDER_HEIGHT;
    }
#endif
}


static void
placeholder_replace (GtkWidget * placeholder)
{
  char *class_name;

  class_name = glade_palette_get_widget_class (GLADE_PALETTE (glade_palette));
  g_return_if_fail (class_name != NULL);
  glade_palette_reset_selection (GLADE_PALETTE (glade_palette), TRUE);
  gb_widget_new_full (class_name, TRUE, placeholder->parent, placeholder, 0, 0,
		      placeholder_finish_replace, GB_CREATING, NULL);
}


static void
placeholder_finish_replace (GtkWidget * new_widget, GbWidgetNewData * data)
{
  editor_clear_selection (NULL);

  /* GtkToolItem widgets can only be added into toolbars and cause problems
     inside other containers, so check that here. Note that we have to use
     special tool items for placeholders so data->parent is usually a
     GtkToolItem rather than a GtkToolbar. */
  if (GTK_IS_TOOL_ITEM (new_widget)
      && (!data->parent || (!GTK_IS_TOOLBAR (data->parent)
			    && !GTK_IS_TOOL_ITEM (data->parent))))
    {
      glade_util_show_message_box (_("GtkToolItem widgets can only be added to a GtkToolbar."), data->parent);
      gtk_widget_destroy (new_widget);
      return;
    }

  /* If a scrollable widget is added, we automatically add a parent scrolled
     window, if there isn't one already. */
  if (GTK_WIDGET_CLASS (G_OBJECT_GET_CLASS (new_widget))->set_scroll_adjustments_signal)
    {
      if (data->parent && !GTK_IS_SCROLLED_WINDOW (data->parent))
	{
	  GtkWidget *scrolledwin;

	  scrolledwin = gb_widget_new ("GtkScrolledWindow", data->parent);
	  if (!gb_widget_replace_child (data->parent, data->current_child,
					scrolledwin))
	    {
	      glade_util_show_message_box (_("Couldn't insert a GtkScrolledWindow widget."), data->parent);
	      gtk_widget_destroy (scrolledwin);
	      gtk_widget_destroy (new_widget);
	      return;
	    }

	  if (GTK_BIN (scrolledwin)->child)
	    gtk_container_remove (GTK_CONTAINER (scrolledwin),
				  GTK_BIN (scrolledwin)->child);
	  gtk_container_add (GTK_CONTAINER (scrolledwin), new_widget);

	  /* Set the shadow to In, since that is normal. */
	  gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolledwin),
					       GTK_SHADOW_IN);

	  /* Set both scrollbar policies to automatic for GtkIconView. */
	  if (GTK_IS_ICON_VIEW (new_widget))
	    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
					    GTK_POLICY_AUTOMATIC,
					    GTK_POLICY_AUTOMATIC);

	  /* GtkText doesn't support horizontal scrolling, so we may as well
	     get rid of the scrollbar. */
#if GLADE_SUPPORTS_GTK_TEXT
	  if (GTK_IS_TEXT (new_widget))
	    gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolledwin),
					    GTK_POLICY_NEVER,
					    GTK_POLICY_ALWAYS);
#endif

	  tree_add_widget (scrolledwin);
	  tree_add_widget (new_widget);
	  gb_widget_show_properties (new_widget);
	  glade_project_set_changed (current_project, TRUE);
	  return;
	}
    }
  /* If a non-scrollable widget is added to a GtkScrolledWindow, we add a
     viewport automatically. */
  else if (data->parent && GTK_IS_SCROLLED_WINDOW (data->parent))
    {
      GtkWidget *viewport;

      viewport = gb_widget_new ("GtkViewport", data->parent);
      if (!gb_widget_replace_child (data->parent, data->current_child,
				    viewport))
	{
	  glade_util_show_message_box (_("Couldn't insert a GtkViewport widget."), data->parent);
	  gtk_widget_destroy (viewport);
	  gtk_widget_destroy (new_widget);
	  return;
	}

      if (GTK_BIN (viewport)->child)
	gtk_container_remove (GTK_CONTAINER (viewport),
			      GTK_BIN (viewport)->child);
      gtk_container_add (GTK_CONTAINER (viewport), new_widget);

      tree_add_widget (viewport);
      tree_add_widget (new_widget);
      gb_widget_show_properties (new_widget);
      glade_project_set_changed (current_project, TRUE);
      return;
    }

  /* Replace placeholder in parent container */
  if (gb_widget_replace_child (data->parent, data->current_child, new_widget))
    {
      gb_widget_show_properties (new_widget);
      tree_add_widget (new_widget);
      glade_project_set_changed (current_project, TRUE);
    }
  else
    {
      glade_util_show_message_box (_("Couldn't add new widget."),
				   data->parent);

      /* I think we can safely destroy the new widget. */
      gtk_widget_destroy (new_widget);
    }
}





/* Note: returns last found child if children overlap */
static void
find_child_at (GtkWidget * widget, GbFindChildAtData * data)
{
#if 0
  g_print ("In find_child_at: %s X:%i Y:%i W:%i H:%i\n",
	   gtk_widget_get_name (widget),
	   widget->allocation.x, widget->allocation.y,
	   widget->allocation.width, widget->allocation.height);
#endif
  /* Notebook pages are visible but not mapped if they are not showing. */
  if (GTK_WIDGET_VISIBLE (widget) && GTK_WIDGET_MAPPED (widget)
      && (widget->allocation.x <= data->x)
      && (widget->allocation.y <= data->y)
      && (widget->allocation.x + widget->allocation.width > data->x)
      && (widget->allocation.y + widget->allocation.height > data->y))
    {
#if 0
      g_print ("found child:%s", gtk_widget_get_name (widget));
#endif
      data->found_child = widget;
    }
}


/* Checks if point is in the given notebook's tabs. */
static void
find_notebook_tab (GtkWidget * widget, GbFindChildAtData * data)
{
  gint nchildren, i;
  GtkWidget *child_page, *child_tab;

  /* FIXME: Check this works. I had to change it for GTK+ 2. */
  nchildren = g_list_length (GTK_NOTEBOOK (widget)->children);
  for (i = 0; i < nchildren; i++)
    {
      child_page = gtk_notebook_get_nth_page (GTK_NOTEBOOK (widget), i);
      child_tab = gtk_notebook_get_tab_label (GTK_NOTEBOOK (widget),
					      child_page);
      if (child_tab
	  && (child_tab->allocation.x <= data->x)
	  && (child_tab->allocation.y <= data->y)
	  && (child_tab->allocation.x + child_tab->allocation.width > data->x)
	  && (child_tab->allocation.y + child_tab->allocation.height > data->y))
	{
	  /* Make sure this is a GbWidget. */
	  /*if (GB_IS_GB_WIDGET (child_tab))*/
	    data->found_child = child_tab;
	}
    }
}


/* This function is passed a widget which has received a mouse event, and
   the coordinates of that event. It returns the widget which the event is
   really meant for (which could be a descendent of the given widget), and
   the position of the event in the widget's allocated area. */
static GtkWidget *
editor_get_event_widget (GtkWidget *widget, GdkWindow *window, gint x, gint y,
			 gint * x_return, gint * y_return)
{
  GbFindChildAtData data;
  gint win_x, win_y, saved_x, saved_y;
  GtkWidget *found_gbwidget = NULL, *saved_widget;
  gint found_x = 0, found_y = 0;
  GdkWindow *parent_window = NULL;

#if 0
  g_print ("\n\nOriginal:%s X:%i Y:%i\n", gtk_widget_get_name (widget), x, y);
  if (widget->parent)
    g_print ("  Parent: %s\n", gtk_widget_get_name (widget->parent));
#endif

  /* FIXME: GTK bug workaround? - need to translate coords if mouse button was
     pressed in a child window. */
  /* Remember widgets can have other windows besides their main one, and
     when dragging the event may be sent to the parent's window? */
  /* SPECIAL CODE: GnomeDockItem widgets which are floating use a different
     window. */
  if (widget->parent)
    {
      if (GTK_IS_LAYOUT (widget->parent))
	parent_window = GTK_LAYOUT (widget->parent)->bin_window;

#ifdef USE_GNOME
      if (BONOBO_IS_DOCK_ITEM (widget)
	       && BONOBO_DOCK_ITEM (widget)->is_floating)
	{
	  parent_window = BONOBO_DOCK_ITEM (widget)->float_window;
	}
      else if (BONOBO_IS_DOCK_ITEM (widget->parent)
	       && BONOBO_DOCK_ITEM (widget->parent)->is_floating)
	{
	  parent_window = BONOBO_DOCK_ITEM (widget->parent)->float_window;
	}
#endif
    }

  if (!parent_window)
    parent_window = widget->parent ? widget->parent->window : widget->window;

  while (window && window != parent_window)
    {
      gdk_window_get_position (window, &win_x, &win_y);
#if 0
      g_print ("  adding X:%i Y:%i\n", win_x, win_y);
#endif
      x += win_x;
      y += win_y;
      window = gdk_window_get_parent (window);
    }
  if (window != parent_window)
    {
      MSG ("  editor_get_event_widget - unknown window");
      return NULL;
    }

#if 0
  g_print ("  Translated X:%i Y:%i\n", x, y);
#endif

  /* We now have correct coordinates relative to the parent's window,
     i.e. in the same coordinate space as the widget's allocation.
     Now we find out which widget this event is really for.
     We step down the widget tree, trying to find the widget at the given
     position. We have to translate coordinates for children of widgets with
     windows. We may need to use bin_window for viewport. */
  if (GB_IS_GB_WIDGET (widget) || GB_IS_PLACEHOLDER (widget))
    {
#if 0
      g_print ("Found child:%s\n", gtk_widget_get_name (widget));
#endif
      found_gbwidget = widget;
      found_x = x;
      found_y = y;
    }

  /* Save the widget and the x & y coords. */
  saved_widget = widget;
  saved_x = x;
  saved_y = y;

  /* Now we want to convert the coordinates into the widget's childrens'
     coordinate space, so if the widget has a window, we have to subtract the
     position of it (since the child coordinates are relative to that).
     Viewports need special treatment. */
  if (!GTK_WIDGET_NO_WINDOW (widget) && widget->parent)
    {
      gdk_window_get_position (widget->window, &win_x, &win_y);
      x -= win_x;
      y -= win_y;

      /* SPECIAL CODE: need to translate to bin_window for a viewport. */
      if (GTK_IS_VIEWPORT (widget))
	{
	  gdk_window_get_position (GTK_VIEWPORT (widget)->bin_window,
				   &win_x, &win_y);
	  x -= win_x;
	  y -= win_y;
	}

      /* SPECIAL CODE: need to translate to bin_window for a layout. */
      if (GTK_IS_LAYOUT (widget))
	{
	  gdk_window_get_position (GTK_LAYOUT (widget)->bin_window,
				   &win_x, &win_y);
	  x -= win_x;
	  y -= win_y;
	}
#if 0
      g_print ("  Translated X:%i Y:%i\n", x, y);
#endif
    }

  for (;;)
    {
      if (!GTK_IS_CONTAINER (widget) || GTK_IS_MENU_BAR (widget))
	break;
      data.x = x;
      data.y = y;
      data.found_child = NULL;
#if 0
      g_print ("...Finding child widget\n");
#endif
      gtk_container_forall (GTK_CONTAINER (widget),
			    (GtkCallback) find_child_at, &data);
      /* SPECIAL CODE - Check for notebook tabs. */
      if (GTK_IS_NOTEBOOK (widget))
	find_notebook_tab (widget, &data);

      if (data.found_child)
	{
#if 0
	  g_print ("Found child:%s\n", gtk_widget_get_name (data.found_child));
#endif
	  widget = data.found_child;
	  if (GB_IS_GB_WIDGET (widget) || GB_IS_PLACEHOLDER (widget))
	    {
	      found_gbwidget = widget;
	      found_x = x;
	      found_y = y;
	    }
	}
      else
	break;

      if (!GTK_WIDGET_NO_WINDOW (widget))
	{
	  gdk_window_get_position (widget->window, &win_x, &win_y);
	  x -= win_x;
	  y -= win_y;

	  /* SPECIAL CODE: need to translate to bin_window for a viewport. */
	  if (GTK_IS_VIEWPORT (widget))
	    {
	      gdk_window_get_position (GTK_VIEWPORT (widget)->bin_window,
				       &win_x, &win_y);
	      x -= win_x;
	      y -= win_y;
	    }
#if 0
	  g_print ("  Translated X:%i Y:%i\n", x, y);
#endif
	}
    }

  /* If we haven't found a GbWidget yet, try moving up the hierarchy. */
  widget = saved_widget;
  x = saved_x;
  y = saved_y;
  while (!found_gbwidget && widget->parent)
    {
      widget = widget->parent;
#if 0
      g_print ("  Trying parent: %s X:%i Y:%i\n",
	       gtk_widget_get_name (widget), x, y);
#endif
      if (GB_IS_GB_WIDGET (widget) || GB_IS_PLACEHOLDER (widget))
	{
	  found_gbwidget = widget;
	  found_x = x;
	  found_y = y;
	}
      else if (!GTK_WIDGET_NO_WINDOW (widget))
	{
	  gdk_window_get_position (widget->window, &win_x, &win_y);
	  x += win_x;
	  y += win_y;

	  /* SPECIAL CODE; use bin_window for viewport. */
	  if (GTK_IS_VIEWPORT (widget))
	    {
	      gdk_window_get_position (GTK_VIEWPORT (widget)->bin_window,
				       &win_x, &win_y);
	      x += win_x;
	      y += win_y;
	    }
#if 0
	  g_print ("  Translated X:%i Y:%i\n", x, y);
#endif
	}
    }

  if (!found_gbwidget)
    return NULL;

  *x_return = found_x - found_gbwidget->allocation.x;
  *y_return = found_y - found_gbwidget->allocation.y;

#if 0
  g_print ("  Event widget: %s X:%i Y:%i\n",
	   gtk_widget_get_name (found_gbwidget), *x_return, *y_return);
#endif

  return found_gbwidget;
}


/* We use the "event" signal since it is emitted before the "button_press"
   or "button_release" signal is emitted. This means we can catch it and
   stop the normal signal handlers. */
static gint
editor_on_event (GtkWidget * signal_widget,
		 GdkEvent * event,
		 gpointer user_data)
{
#if 0
  g_print ("In editor_on_event widget: %s\n",
	   gtk_widget_get_name (signal_widget));
#endif

  if (event->type == GDK_BUTTON_PRESS
      || event->type == GDK_2BUTTON_PRESS
      || event->type == GDK_3BUTTON_PRESS)
    return editor_on_button_press (signal_widget, (GdkEventButton*) event,
				   user_data);

  if (event->type == GDK_BUTTON_RELEASE)
    return editor_on_button_release (signal_widget, event, user_data);

  return FALSE;
}


static gint
editor_on_button_press (GtkWidget * signal_widget,
			GdkEventButton * event,
			gpointer user_data)
{
  GtkWidget *widget;
  gint x, y;

#if 0
  g_print ("In editor_on_button_press widget: %s\n",
	   gtk_widget_get_name (signal_widget));
#endif
  widget = editor_get_event_widget (signal_widget, event->window,
				    event->x, event->y, &x, &y);
  if (widget == NULL)
    return FALSE;

  if (editor_check_ignore_event (widget, (GdkEventAny*) event))
    return FALSE;

  MSG ("...Checking which button pressed");
  /* We only want single button press events. */
  if (event->button == 1 && event->type == GDK_BUTTON_PRESS)
    {
      if (glade_palette_is_selector_on (GLADE_PALETTE (glade_palette)))
	{
	  gboolean handled;
	  MSG ("...Selecting widget");
	  handled = editor_select_widget (widget, event, x, y);
#if 0
	  if (handled)
	    gtk_signal_emit_stop_by_name (GTK_OBJECT (signal_widget),
					  "button_press_event");
#endif
	  return handled;
	}
#ifdef USE_GNOME
      else if (GTK_IS_FIXED (widget)
	       || (GTK_IS_LAYOUT (widget) && !GNOME_IS_CANVAS (widget)))
#else
      else if (GTK_IS_FIXED (widget) || GTK_IS_LAYOUT (widget))
#endif
	{
	  add_widget_to_fixed (widget, x, y);
#if 0
	  gtk_signal_emit_stop_by_name (GTK_OBJECT (signal_widget),
					"button_press_event");
#endif
	  return TRUE;
	}
      else if (GB_IS_PLACEHOLDER (widget))
	{
	  placeholder_replace (widget);
#if 0
	  gtk_signal_emit_stop_by_name (GTK_OBJECT (signal_widget),
					"button_press_event");
#endif
	  return TRUE;
	}
#if GLADE_SUPPORTS_GTK_PACKER
      else if (GTK_IS_PACKER (widget))
	{
	  add_widget_to_container (widget);
#if 0
	  gtk_signal_emit_stop_by_name (GTK_OBJECT (signal_widget),
					"button_press_event");
#endif
	  return TRUE;
	}
#endif
      else
        {
         static gboolean already_shown = FALSE;
         if (already_shown) 
           {
             /* Beep if user does mistake of invalid positioning from second
              * time.
              */
             gdk_beep ();	
           }
         else
           {
             glade_util_show_message_box (_("You can't add a widget at the selected position.\n"
					    "\n"
					    "Tip: GTK+ uses containers to lay out widgets.\n"
					    "Try deleting the existing widget and using\n"
					    "a box or table container instead.\n"), widget);
             already_shown = TRUE;
           }
        }
    }
  else if (event->button == 3)
    {
      gb_widget_show_popup_menu (widget, event);
#if 0
      gtk_signal_emit_stop_by_name (GTK_OBJECT (signal_widget),
				    "button_press_event");
#endif
      return TRUE;
    }
  return FALSE;
}


static gint
editor_on_button_release (GtkWidget * widget,
			  GdkEvent * event,
			  gpointer data)
{
  MSG ("In editor_on_button_release");
  if (drag_has_pointer_grab)
    {
#if 0
      g_print ("###### Releasing pointer grab\n");
#endif
      gdk_pointer_ungrab (event->button.time);
      drag_has_pointer_grab = FALSE;
    }

  if (dragging_widget)
    {
      drag_action = GB_DRAG_NONE;
      MSG ("  removing grab");
      gtk_grab_remove (dragging_widget);
      dragging_widget = NULL;
    }
  return FALSE;
}


/* This adds a new widget to a GtkFixed or GtkLayout container. */
static void
add_widget_to_fixed (GtkWidget * parent, gint x, gint y)
{
  char *class_name;

  class_name = glade_palette_get_widget_class (GLADE_PALETTE (glade_palette));
  g_return_if_fail (class_name != NULL);
  glade_palette_reset_selection (GLADE_PALETTE (glade_palette), TRUE);
  gb_widget_new_full (class_name, TRUE, parent, NULL, x, y,
		      add_widget_to_fixed_finish, GB_CREATING, NULL);
}


/* This finishes off adding a new widget to a GtkFixed or GtkLayout container,
   possibly after a dialog has been shown to set some initial properties of
   the new widget (e.g. number of rows for a new GtkCList). */
static void
add_widget_to_fixed_finish (GtkWidget * new_widget, GbWidgetNewData * data)
{
  GtkRequisition requisition = {0, 0};
  GladeWidgetData *wdata;
  GtkWidget *parent = data->parent;
  gint x = data->x;
  gint y = data->y;
  gint w, h;

  g_return_if_fail (parent != NULL);
  g_return_if_fail (GTK_IS_FIXED (parent) || GTK_IS_LAYOUT (parent));

  /* I think we should do this first, in case the widget needs to be
     realized in order to calculate its requisition. */
  gtk_widget_hide (new_widget);

  if (GTK_IS_FIXED (data->parent))
    {
      gtk_fixed_put (GTK_FIXED (data->parent), new_widget, x, y);
      /*gtk_widget_set_uposition (new_widget, x, y);*/
    }
  else if (GTK_IS_LAYOUT (data->parent))
    {
      GtkLayout *layout = GTK_LAYOUT (data->parent);
      x += layout->hadjustment->value;
      y += layout->vadjustment->value;
      gtk_layout_put (layout, new_widget, x, y);
      /*gtk_widget_set_uposition (new_widget, x, y);*/
    }


  gtk_widget_size_request (new_widget, &requisition);
  w = requisition.width;
  h = requisition.height;
  if (w < MIN_WIDGET_WIDTH || h < MIN_WIDGET_HEIGHT)
    {
      MSG3 ("Default widget size too small: %s W:%i H:%i\n",
	    gtk_widget_get_name (new_widget), w, h);
    }
  if (w > MAX_INITIAL_WIDGET_WIDTH || h > MAX_INITIAL_WIDGET_HEIGHT)
    {
      MSG3 ("Default widget size too big: %s W:%i H:%i\n",
	    gtk_widget_get_name (new_widget), w, h);
    }

  if (w == 0)
    w = DEFAULT_WIDGET_WIDTH;
  if (h == 0)
    h = DEFAULT_WIDGET_HEIGHT;
  w = MAX (w, MIN_WIDGET_WIDTH);
  w = MIN (w, MAX_INITIAL_WIDGET_WIDTH);
  h = MAX (h, MIN_WIDGET_HEIGHT);
  h = MIN (h, MAX_INITIAL_WIDGET_HEIGHT);

  /* FIXME: Make sure a gamma curve is a reasonable size. */
  if (GTK_IS_GAMMA_CURVE (new_widget))
    w = 80;

  /* FIXME: Make sure a clist/ctree or notebook is a reasonable size. */
  if (GTK_IS_CLIST (new_widget) || GTK_IS_NOTEBOOK (new_widget))
    {
      w = 200;
      h = 100;
    }

  /* Children of fixed widgets always have their x, y, width & height set
     explicitly. */
  wdata = gtk_object_get_data (GTK_OBJECT (new_widget), GB_WIDGET_DATA_KEY);
  g_return_if_fail (wdata != NULL);
  wdata->flags |= GLADE_WIDTH_SET | GLADE_HEIGHT_SET;

  /* Calculate real position & size according to grid & snapping */
  /* FIXME: only snaps top & left at present */
  if (editor_snap_to_grid)
    {
      if (editor_snap_to_grid_x & GB_SNAP_LEFT)
	{
	  /*x += editor_grid_horz_spacing / 2; */
	  x -= x % editor_grid_horz_spacing;
	}
      if (editor_snap_to_grid_y & GB_SNAP_TOP)
	{
	  /*y += editor_grid_vert_spacing / 2; */
	  y -= y % editor_grid_vert_spacing;
	}
    }

  gb_widget_set_usize (new_widget, w, h);
  wdata->width = w;
  wdata->height = h;

  if (GTK_IS_FIXED (data->parent))
    {
      gtk_fixed_move (GTK_FIXED (data->parent), new_widget, x, y);
      /*gtk_widget_set_uposition (new_widget, x, y);*/
    }
  else if (GTK_IS_LAYOUT (data->parent))
    {
      gtk_layout_move (GTK_LAYOUT (data->parent), new_widget, x, y);
      /*gtk_widget_set_uposition (new_widget, x, y);*/
    }

  gtk_widget_show (new_widget);
  gb_widget_show_properties (new_widget);
  tree_add_widget (new_widget);
  glade_project_set_changed (data->project, TRUE);
}


#if GLADE_SUPPORTS_GTK_PACKER
static void
add_widget_to_container (GtkWidget * parent)
{
  char *class_name;

  class_name = glade_palette_get_widget_class (GLADE_PALETTE (glade_palette));
  g_return_if_fail (class_name != NULL);
  glade_palette_reset_selection (GLADE_PALETTE (glade_palette), TRUE);
  gb_widget_new_full (class_name, TRUE, parent, NULL, 0, 0,
		      add_widget_to_container_finish, GB_CREATING, NULL);
}


static void
add_widget_to_container_finish (GtkWidget * new_widget, GbWidgetNewData * data)
{
  gtk_container_add (GTK_CONTAINER (data->parent), new_widget);
  gtk_widget_show (new_widget);
  gb_widget_show_properties (new_widget);
  tree_add_widget (new_widget);
  glade_project_set_changed (data->project, TRUE);
}
#endif

/*
 * Clears all currently selected widgets, except the given widget.
 * Returns TRUE if given widget is selected.
 */
gint
editor_clear_selection (GtkWidget * leave_widget)
{
  GList *child = selected_widgets, *next_child;
  GtkWidget *widget;
  gint selected = FALSE;

  while (child)
    {
      next_child = child->next;
      widget = child->data;
      if (widget == leave_widget)
	{
	  selected = TRUE;
	}
      else
	{
	  selected_widgets = g_list_remove (selected_widgets, widget);
	  tree_select_widget (widget, FALSE);
	  editor_refresh_widget_selection (widget);
	}
      child = next_child;
    }
  return selected;
}


void
editor_remove_widget_from_selection (GtkWidget * widget)
{
  selected_widgets = g_list_remove (selected_widgets, widget);

  /* If the widget is not being destroyed, we deselect it in the tree.
     If it is being destroyed, it will get removed from the tree so we don't
     have to bother. */
  if (!(GTK_OBJECT_FLAGS (widget) & GTK_IN_DESTRUCTION))
    tree_select_widget (widget, FALSE);
}


void
editor_deselect_all_placeholders (void)
{
  GList *child, *next_child;
  GtkWidget *widget;

  child = selected_widgets;
  while (child)
    {
      next_child = child->next;
      widget = child->data;

      if (GB_IS_PLACEHOLDER (widget))
	{
	  selected_widgets = g_list_remove (selected_widgets, widget);
	  editor_refresh_widget_selection (widget);
	}
      child = next_child;
    }
}


gboolean
editor_is_selected (GtkWidget *widget)
{
  GList *elem = selected_widgets;

  while (elem)
    {
      if (GTK_WIDGET (elem->data) == widget)
	return TRUE;
      elem = elem->next;
    }
  return FALSE;
}


void
editor_dump_selection (void)
{
  GList *elem = selected_widgets;

  g_print ("Selected widgets:\n");
  while (elem)
    {
      g_print ("  %p: %s\n", elem->data,
	       gtk_widget_get_name (GTK_WIDGET (elem->data)));
      elem = elem->next;
    }
}


static gint
get_position_in_widget (GtkWidget * widget, gint x, gint y)
{
  gint width = widget->allocation.width;
  gint height = widget->allocation.height;
  if (x < GB_CORNER_WIDTH && y < GB_CORNER_HEIGHT)
    return GB_TOP_LEFT;
  if (x > width - GB_CORNER_WIDTH && y < GB_CORNER_HEIGHT)
    return GB_TOP_RIGHT;
  if (x < GB_CORNER_WIDTH && y > height - GB_CORNER_HEIGHT)
    return GB_BOTTOM_LEFT;
  if (x > width - GB_CORNER_WIDTH && y > height - GB_CORNER_HEIGHT)
    return GB_BOTTOM_RIGHT;
  return GB_MIDDLE;
}


static void
raise_fixed_child (GtkWidget * widget)
{
  GtkFixed *fixed;

  g_return_if_fail (GTK_IS_FIXED (widget->parent));
  fixed = GTK_FIXED (widget->parent);
  /* If widget hasn't got a window, move it to the back of the parent fixed's
     children. If it has got a window, raise it. */
  /* Note: this is slightly naughty as it changes the GtkFixed's GList of
     children, but it's better than removing the widget and adding it again. */
  if (GTK_WIDGET_NO_WINDOW (widget))
    {
      GList *child;
      GtkFixedChild *data;
      child = fixed->children;
      while (child)
	{
	  data = child->data;
	  if (data->widget == widget)
	    {
	      fixed->children = g_list_remove (fixed->children, data);
	      fixed->children = g_list_append (fixed->children, data);
	      break;
	    }
	  child = child->next;
	}
    }
  else
    {
      gdk_window_raise (widget->window);
    }
}


gboolean
editor_select_widget_control (GtkWidget * widget)
{
  MSG ("IN editor_select_widget_control");
  /* If widget is currently selected, deslect it, else add it to
     selected. */
  if (g_list_find (selected_widgets, widget))
    {
      selected_widgets = g_list_remove (selected_widgets, widget);
    }
  else
    {
      gb_widget_show_properties (widget);
      selected_widgets = g_list_append (selected_widgets, widget);
      glade_project_view_clear_component_selection (current_project_view,
						    widget);
    }
  editor_refresh_widget_selection (widget);
  return TRUE;
}
 
/* FIXME: handle widget-tree better now it has extended selection */
GList *
editor_get_selection ()
{
  return g_list_first (selected_widgets);
}


/* This sets the list of selected widgets, possibly NULL. It takes control
   of the GList, so don't free it. */
void
editor_set_selection (GList *new_selection)
{
  GList *old_selection, *elem;

  old_selection = selected_widgets;

  selected_widgets = new_selection;

  for (elem = old_selection; elem; elem = elem->next)
    editor_refresh_widget_selection (elem->data);

  for (elem = new_selection; elem; elem = elem->next)
    {
      /* Only refresh it if it wasn't already refreshed above. */
      if (!g_list_find (old_selection, elem->data))
	editor_refresh_widget_selection (elem->data);
    }

  /* If just one widget is selected, show its properties. */
  if (g_list_length (new_selection) == 1)
    gb_widget_show_properties (GTK_WIDGET (new_selection->data));

  g_list_free (old_selection);
}


static gint
get_notebook_page (GtkNotebook * notebook,
		   GtkWidget * widget)
{
  gint nchildren, page;

  nchildren = g_list_length (notebook->children);

  for (page = 0; page < nchildren; page++)
    {
      GtkWidget *child, *tab;

      child = gtk_notebook_get_nth_page (notebook, page);
      tab = gtk_notebook_get_tab_label (notebook, child);
      if (tab == widget)
	return page;
    }

  return -1;
}



/* FIXME: When the editor is rewritten as a GtkObject, we need simple
   functions to select/deselect widgets separate from the complicated
   stuff used for moving/resizing. */
gboolean
editor_select_widget (GtkWidget * widget, GdkEventButton * event,
		      gint x, gint y)
{
  gint already_selected, page;
  GtkWidget *select_widget, *ancestor;
  gboolean handled = FALSE;

  MSG ("IN editor_select_widget");
#if 0
  g_print ("=+=+ widget: %s Alloc X:%i Y:%i W:%i H:%i\n",
	   gtk_widget_get_name (widget),
	   widget->allocation.x, widget->allocation.y,
	   widget->allocation.width, widget->allocation.height);
#endif

  /* reset any move/resize action */
  drag_action = GB_DRAG_NONE;

  /* Shift + selection. Step through parents of this widget, until a selected
     widget is found. Then clear selection & select that widgets parent.
     If no parents were selected, or the top-level parent was selected,
     select this widget. */
  if (event && event->state & GDK_SHIFT_MASK)
    {
      gint found = FALSE;
      ancestor = widget;
      while (ancestor)
	{
	  if (g_list_find (selected_widgets, ancestor))
	    {
	      found = TRUE;
	      break;
	    }
	  ancestor = ancestor->parent;
	}
      if (found && ancestor->parent != NULL)
	{
	  select_widget = ancestor->parent;
	  /* If widget is not a GbWidget, i.e. it has no GladeWidgetData,
	     skip it. */
	  while (select_widget)
	    {
	      if (GB_IS_GB_WIDGET (select_widget))
		break;
	      select_widget = select_widget->parent;
	    }
	}
      else
	{
	  select_widget = widget;
	}
      g_return_val_if_fail (select_widget != NULL, FALSE);
      gb_widget_show_properties (select_widget);
      already_selected = editor_clear_selection (select_widget);
      if (already_selected)
	return FALSE;
      selected_widgets = g_list_append (selected_widgets, select_widget);
      tree_select_widget (select_widget, TRUE);
      glade_project_view_clear_component_selection (current_project_view,
						    widget);
      editor_refresh_widget_selection (select_widget);
      return TRUE;
    }

#if 0
  /* Control + selection. If widget is currently selected, deslect it, else
     add it to the selected widgets. */
  /* I've taken this out as it can cause crashes and it isn't very useful. */
  if (event && event->state & GDK_CONTROL_MASK)
    {
      /* If widget is currently selected, deselect it, else add it to
         selected. */
      editor_select_widget_control (widget);

      if (g_list_find (selected_widgets, widget))
	tree_select_widget (widget, TRUE);
      else
	tree_select_widget (widget, FALSE);

      return TRUE;
    }
#endif

  /* Normal selection. If the widget is currently selected, just get the
     data for a possible resize/drag. If it is not selected, clear all
     currently selected widgets, then select this one.
     Also remember where the button press occurred, in case widget is being
     moved or resized (in a fixed container). */
  gb_widget_show_properties (widget);

  if (!g_list_find (selected_widgets, widget))
    {
      editor_clear_selection (NULL);
      selected_widgets = g_list_append (selected_widgets, widget);
      tree_select_widget (widget, TRUE);
      handled = TRUE;
      glade_project_view_clear_component_selection (current_project_view,
						    widget);
      /* If parent is a fixed container, move widget to front */
      if (widget->parent && GTK_IS_FIXED (widget->parent))
	raise_fixed_child (widget);

      /* SPECIAL CODE: If the widget or an ancestor is a notebook tab,
         show the page */
      ancestor = widget;
      while (ancestor->parent)
	{
	  if (GTK_IS_NOTEBOOK (ancestor->parent))
	    {
	      page = get_notebook_page (GTK_NOTEBOOK (ancestor->parent),
					ancestor);
	      if (page != -1)
		gtk_notebook_set_current_page (GTK_NOTEBOOK (ancestor->parent),
					       page);
	      break;
	    }
	  ancestor = ancestor->parent;
	}

      editor_refresh_widget_selection (widget);
    }


  /* NOTE: this will only work in a GtkFixed or a GtkLayout. */
  if (widget->parent && event
      && (GTK_IS_FIXED (widget->parent) || GTK_IS_LAYOUT (widget->parent)))
    {
      if (gdk_pointer_grab (event->window, FALSE,
			    GDK_POINTER_MOTION_HINT_MASK |
			    GDK_BUTTON1_MOTION_MASK |
			    GDK_BUTTON_RELEASE_MASK,
			    NULL, NULL, event->time))
	return FALSE;

#if 0
      g_print ("###### Grabbed pointer\n");
#endif
      drag_has_pointer_grab = TRUE;
      drag_action = get_position_in_widget (widget, x, y);
      drag_offset_x = x;
      drag_offset_y = y;

      drag_widget_x1 = widget->allocation.x;
      drag_widget_y1 = widget->allocation.y;

      if (GTK_IS_FIXED (widget->parent))
	{
	  drag_widget_x1 -= widget->parent->allocation.x;
	  drag_widget_y1 -= widget->parent->allocation.y;
	}

      drag_widget_x2 = drag_widget_x1 + widget->allocation.width;
      drag_widget_y2 = drag_widget_y1 + widget->allocation.height;

#if 0
      g_print ("drag_action:%i, offset_x:%i offset_y:%i "
	       "x1:%i y1:%i x2:%i y2:%i\n",
	       drag_action, drag_offset_x, drag_offset_y,
	       drag_widget_x1, drag_widget_y1,
	       drag_widget_x2, drag_widget_y2);
#endif

      /* We return TRUE to make sure the signal is stopped so the widget
	 doesn't popup a menu or something, which would make moving/resizing
	 very difficult. */
      return TRUE;
    }
  return handled;
}


/* This returns TRUE if the event should be ignored. Currently we only
   ignore events in clist/ctree resize windows, but more may be added. */
static gboolean
editor_check_ignore_event (GtkWidget *widget,
			   GdkEventAny *event)
{
  GtkWidget *window_widget;

  g_return_val_if_fail (event != NULL, FALSE);

  gdk_window_get_user_data (event->window, (gpointer) &window_widget);

  if (GTK_IS_CLIST (window_widget))
    {
      gint i;
      for (i = 0; i < GTK_CLIST (window_widget)->columns; i++)
	{
	  GtkCListColumn *col = &GTK_CLIST (window_widget)->column[i];
	  if (event->window == col->window)
	    {
	      MSG ("Ignored event (clist resize window)");
	      return TRUE;
	    }
	}
    }
  return FALSE;
}


static gint
editor_on_motion_notify (GtkWidget * signal_widget,
			 GdkEventMotion * event,
			 gpointer data)
{
#if 0
  g_print ("In editor_on_motion_notify: %s\n",
	   gtk_widget_get_name (signal_widget));
#endif

  if (event->state & GDK_BUTTON1_MASK)
    return editor_do_drag_action (signal_widget, event);
  else
    return editor_set_cursor (signal_widget, event);
}


static gint
editor_set_cursor (GtkWidget * signal_widget,
		   GdkEventMotion * event)
{
  /* We remember the last cursor set and the last window, so we don't set the
     same cursor on the same window repeatedly. */
  static GdkWindow *last_window = NULL;
  static GdkCursor *last_cursor = NULL;

  GtkWidget *widget, *event_widget;
  GdkCursor *cursor = NULL;
  gint x, y, event_x, event_y, pos;

#if 0
  g_print ("In editor_set_cursor %s hint:%i\n",
	   gtk_widget_get_name (signal_widget), event->is_hint);
#endif

  /* First of all, we find out which widget the event originated from.
     We step up the parents until we find the current widget, checking if
     any GbWidgets are children. If they are, we have already seen this event
     so we just return. */
  event_widget = gtk_get_event_widget ((GdkEvent*) event);
  if (event_widget)
    {
      while (event_widget != signal_widget)
	{
	  if (event_widget == NULL)
	    {
	      g_warning ("motion_notify - didn't find signal widget");
	      break;
	    }

	  if (GB_IS_GB_WIDGET (event_widget))
	    return FALSE;

	  event_widget = event_widget->parent;
	}

    }

  if (event->is_hint)
    gdk_window_get_pointer (event->window, &event_x, &event_y, NULL);
  else
    {
      event_x = event->x;
      event_y = event->y;
    }

  widget = editor_get_event_widget (signal_widget, event->window,
				    event_x, event_y, &x, &y);
  if (widget == NULL)
    return FALSE;

  if (editor_check_ignore_event (widget, (GdkEventAny*) event))
    return FALSE;

  /* Remember the widget, so we can redirect key presses to it. But only
     keep pointers to GbWidgets, as we can reset the pointer to NULL when the
     widget is destroyed. */
  if (GB_IS_GB_WIDGET (widget))
    {
#if 0
      g_print ("   mouse_over_widget: %s\n", gtk_widget_get_name (widget));
#endif
      mouse_over_widget = widget;
    }

#if 0
  g_print ("editor_set_cursor widget: %s (%p) X:%i Y:%i\n",
	   gtk_widget_get_name (widget), widget, x, y);
#endif
  if (glade_palette_is_selector_on (GLADE_PALETTE (glade_palette)))
    {
#ifdef USE_GNOME
      if (widget->parent
	  && (GTK_IS_FIXED (widget)
	      || (GTK_IS_LAYOUT (widget) && !GNOME_IS_CANVAS (widget))))
#else
      if (widget->parent
	  && (GTK_IS_FIXED (widget->parent)
	      || GTK_IS_LAYOUT (widget->parent)))
#endif
	{
	  pos = get_position_in_widget (widget, x, y);
	  switch (pos)
	    {
	    case GB_TOP_LEFT:
#if 0
	      g_print ("TOP_LEFT\n");
#endif
	      cursor = cursor_top_left;
	      break;
	    case GB_TOP_RIGHT:
#if 0
	      g_print ("TOP_RIGHT\n");
#endif
	      cursor = cursor_top_right;
	      break;
	    case GB_BOTTOM_LEFT:
#if 0
	      g_print ("BOTTOM_LEFT\n");
#endif
	      cursor = cursor_bottom_left;
	      break;
	    case GB_BOTTOM_RIGHT:
#if 0
	      g_print ("BOTTOM_RIGHT\n");
#endif
	      cursor = cursor_bottom_right;
	      break;
	    case GB_MIDDLE:
#if 0
	      g_print ("MIDDLE\n");
#endif
	      cursor = cursor_move;
	      break;
	    }
	}
      else
	{
	  cursor = cursor_selector;
	}
    }
  else
    {
      if (GTK_IS_FIXED (widget) || GTK_IS_LAYOUT (widget)
	  || (widget->parent && (GTK_IS_FIXED (widget->parent)
				 || GTK_IS_LAYOUT (widget->parent))))
	cursor = cursor_add_to_fixed;
      else
	cursor = cursor_add_widget;
    }

  if (cursor)
    {
      if (last_window != event->window || last_cursor != cursor)
	{
	  gdk_window_set_cursor (event->window, cursor);
	  last_window = event->window;
	  last_cursor = cursor;

	}
    }

  return FALSE;
}


static gint
editor_do_drag_action (GtkWidget * signal_widget, GdkEventMotion * event)
{
  GtkWidget *widget;
  GladeWidgetData *wdata;
  gint x, y, event_x, event_y;
  gint mouse_x, mouse_y, new_x = 0, new_y = 0, new_width = 0, new_height = 0;
  gint old_x, old_y, old_width, old_height;

#if 0
  g_print ("In editor_do_drag_action %s hint:%i\n",
	   gtk_widget_get_name (signal_widget), event->is_hint);
#endif

  /* If no move/resize action was started in the button_press event, return. */
  if (drag_action == GB_DRAG_NONE)
    return FALSE;

  if (event->is_hint)
    gdk_window_get_pointer (event->window, &event_x, &event_y, NULL);
  else
    {
      event_x = event->x;
      event_y = event->y;
    }

#if 0
  g_print ("In editor_do_drag_action %s hint:%i %i,%i\n",
	   gtk_widget_get_name (signal_widget), event->is_hint,
	   event_x, event_y);
#endif

  /* Use our function to figure out which widget the mouse is in, and where
     in the widget. */
  widget = editor_get_event_widget (signal_widget, event->window,
				    event_x, event_y, &x, &y);
  if (widget == NULL)
    return FALSE;

  if (editor_check_ignore_event (widget, (GdkEventAny*) event))
    return FALSE;


  if (!widget->parent
      || (!GTK_IS_FIXED (widget->parent) && !GTK_IS_LAYOUT (widget->parent)))
    return FALSE;

  if (dragging_widget == NULL)
    {
      dragging_widget = widget;
      gtk_grab_add (widget);
    }
  else
    {
      if (dragging_widget != widget)
	return FALSE;
    }

  wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  g_return_val_if_fail (wdata != NULL, FALSE);

  old_x = widget->allocation.x;
  old_y = widget->allocation.y;
  old_width = widget->allocation.width;
  old_height = widget->allocation.height;

  /* GtkFixed doesn't normally have a window now, so we need to subtract its
     position so our coordinates are relative to it. */
  if (GTK_IS_FIXED (widget->parent))
    {
      old_x -= widget->parent->allocation.x;
      old_y -= widget->parent->allocation.y;
    }

  gdk_window_get_pointer (widget->parent->window, &mouse_x, &mouse_y, NULL);
  if (GTK_IS_FIXED (widget->parent))
    {
      mouse_x -= widget->parent->allocation.x;
      mouse_y -= widget->parent->allocation.y;
    }
  if (GTK_IS_LAYOUT (widget->parent))
    {
      mouse_x += GTK_LAYOUT (widget->parent)->hadjustment->value;
      mouse_y += GTK_LAYOUT (widget->parent)->vadjustment->value;
      old_x += GTK_LAYOUT (widget->parent)->hadjustment->value;
      old_y += GTK_LAYOUT (widget->parent)->vadjustment->value;
    }

  switch (drag_action)
    {
    case GB_TOP_LEFT:
      new_x = snap_left_edge (mouse_x);
      new_y = snap_top_edge (mouse_y);
      new_width = drag_widget_x2 - new_x;
      new_height = drag_widget_y2 - new_y;
      if (new_width < MIN_WIDGET_WIDTH)
	{
	  new_width = MIN_WIDGET_WIDTH;
	  new_x = drag_widget_x2 - new_width;
	}
      if (new_height < MIN_WIDGET_HEIGHT)
	{
	  new_height = MIN_WIDGET_HEIGHT;
	  new_y = drag_widget_y2 - new_height;
	}
      break;

    case GB_TOP_RIGHT:
      new_x = drag_widget_x1;
      new_y = snap_top_edge (mouse_y);
      new_width = snap_right_edge (mouse_x) - new_x;
      new_height = drag_widget_y2 - new_y;
      if (new_width < MIN_WIDGET_WIDTH)
	{
	  new_width = MIN_WIDGET_WIDTH;
	}
      if (new_height < MIN_WIDGET_HEIGHT)
	{
	  new_height = MIN_WIDGET_HEIGHT;
	  new_y = drag_widget_y2 - new_height;
	}
      break;

    case GB_BOTTOM_LEFT:
      new_x = snap_left_edge (mouse_x);
      new_y = drag_widget_y1;
      new_width = drag_widget_x2 - new_x;
      new_height = snap_bottom_edge (mouse_y) - new_y;
      if (new_width < MIN_WIDGET_WIDTH)
	{
	  new_width = MIN_WIDGET_WIDTH;
	  new_x = drag_widget_x2 - new_width;
	}
      if (new_height < MIN_WIDGET_HEIGHT)
	new_height = MIN_WIDGET_HEIGHT;
      break;

    case GB_BOTTOM_RIGHT:
      new_x = drag_widget_x1;
      new_y = drag_widget_y1;
      new_width = snap_right_edge (mouse_x) - new_x;
      new_height = snap_bottom_edge (mouse_y) - new_y;
      if (new_width < MIN_WIDGET_WIDTH)
	new_width = MIN_WIDGET_WIDTH;
      if (new_height < MIN_WIDGET_HEIGHT)
	new_height = MIN_WIDGET_HEIGHT;
      break;

    case GB_MIDDLE:
      new_x = snap_left_edge (mouse_x - drag_offset_x);
      new_y = snap_top_edge (mouse_y - drag_offset_y);
      new_width = widget->allocation.width;
      new_height = widget->allocation.height;
      if (new_x < 0)
	new_x = 0;
      if (new_y < 0)
	new_y = 0;
      break;
    }

#if 0
      g_print ("old_x: %i old_y: %i new_x: %i new_y: %i w: %i h: %i\n",
	       widget->allocation.x, widget->allocation.y,
	       new_x, new_y, new_width, new_height);
#endif

  /* Only move/resize widget if values have changed */
  if (new_width != widget->allocation.width
      || new_height != widget->allocation.height)
    {
      wdata->width = new_width;
      wdata->height = new_height;
      gb_widget_set_usize (widget, new_width, new_height);
    }

  if (new_x != old_x || new_y != old_y)
    {
#if 0
      g_print ("  moving widget\n");
#endif
      if (GTK_IS_FIXED (widget->parent))
	{
	  /* FIXME: GTK+ bug workaround. The widget doesn't move if we only
	     call gtk_fixed_move(). This is very slow as well. */
	  gtk_fixed_move (GTK_FIXED (widget->parent), widget, new_x, new_y);
	  /*gtk_widget_set_uposition (widget, new_x, new_y);*/
	}
      else if (GTK_IS_LAYOUT (widget->parent))
	{
	  gtk_layout_move (GTK_LAYOUT (widget->parent), widget, new_x, new_y);
	}

      gtk_widget_queue_draw (widget->parent);
    }

  return TRUE;
}


/*
 * Adding signals to widgets to allow manipulation, e.g. selecting/drawing
 */

static void
editor_on_widget_realize (GtkWidget *widget, gpointer data)
{
#if 0
  static GdkPixmap *placeholder_pixmap = NULL;

  g_print ("In editor_on_widget_realize widget:%s (%p)\n",
	   gtk_widget_get_name (widget), widget);

  if (GB_IS_PLACEHOLDER (widget))
    {
      /* Create placeholder pixmap if it hasn't already been created.
	 There may be a problem with multi-depth displays. */
      if (placeholder_pixmap == NULL)
	{
	  placeholder_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
							     NULL, NULL,
							     placeholder_xpm);
	  if (!placeholder_pixmap)
	    {
	      g_warning ("Couldn't create placeholder pixmap\n");
	      /* FIXME: Use a color instead? */
	    }
	}

      if (placeholder_pixmap != NULL)
	gdk_window_set_back_pixmap (widget->window, placeholder_pixmap, FALSE);
    }
#endif
}


/* This adds the button signals to an existing widget (currently only the
   Clist title buttons). */
void
editor_add_mouse_signals_to_existing (GtkWidget * widget)
{
  gtk_signal_connect (GTK_OBJECT (widget), "event",
		      GTK_SIGNAL_FUNC (editor_on_event), NULL);
#if 0
  gtk_signal_connect (GTK_OBJECT (widget), "button_press_event",
		      GTK_SIGNAL_FUNC (editor_on_button_press), NULL);
  gtk_signal_connect (GTK_OBJECT (widget), "button_release_event",
		      GTK_SIGNAL_FUNC (editor_on_button_release), NULL);
#endif
}


static gint
editor_on_enter_notify (GtkWidget        *widget,
			GdkEventCrossing *event)
{
  /* We try to stop enter/leave notify events when moving/resizing widget in
     a GtkFixed/GtkLayout as it causes flicker. */
  return dragging_widget ? TRUE : FALSE;
}


static gint
editor_on_leave_notify (GtkWidget        *widget,
			GdkEventCrossing *event)
{
  /* We try to stop enter/leave notify events when moving/resizing widget in
     a GtkFixed/GtkLayout as it causes flicker. */
  return dragging_widget ? TRUE : FALSE;
}


static void
add_mouse_signals_recursive (GtkWidget *widget, gpointer data)
{
#if 0
  g_print ("Adding mouse signals to:%s (%s, %p)\n",
	   gtk_widget_get_name (widget),
	   gtk_type_name (GTK_OBJECT_TYPE (widget)),
	   widget);
#endif

  /* FIXME: We don't add signals to menu items, since it currently makes it
     impossible to popup the menus in a menubar. */
  if (GTK_IS_MENU_ITEM (widget))
    return;

  /* Ensure that the event mask is set so we get button press & release
     events. */
  if (!GTK_WIDGET_NO_WINDOW (widget))
    {
      if (!GTK_WIDGET_REALIZED (widget))
	{
	  gtk_widget_set_events (widget, gtk_widget_get_events (widget)
				 | GDK_BUTTON_PRESS_MASK
				 | GDK_BUTTON_RELEASE_MASK);
	}
      else
	{
	  GdkEventMask event_mask;
	  
	  /* FIXME: Here we set the event mask for the main window of a widget,
	     but widgets can have more than one window. How do we get all the
	     windows of a widget? */
	  event_mask = gdk_window_get_events (widget->window);
	  gdk_window_set_events (widget->window, event_mask
				 | GDK_BUTTON_PRESS_MASK
				 | GDK_BUTTON_RELEASE_MASK);
	}
    }

  gtk_signal_connect (GTK_OBJECT (widget), "event",
		      GTK_SIGNAL_FUNC (editor_on_event), NULL);
#if 0
  gtk_signal_connect (GTK_OBJECT (widget), "button_press_event",
		      GTK_SIGNAL_FUNC (editor_on_button_press), NULL);
  gtk_signal_connect (GTK_OBJECT (widget), "button_release_event",
		      GTK_SIGNAL_FUNC (editor_on_button_release), NULL);
#endif

  /* We connect to these so we can stop widgets getting them while we are
     dragging/resizing. It stops widgets changing state, i.e. normal/active
     and so cuts down on flickering a bit. */
  gtk_signal_connect (GTK_OBJECT (widget), "enter_notify_event",
		      GTK_SIGNAL_FUNC (editor_on_enter_notify), NULL);
  gtk_signal_connect (GTK_OBJECT (widget), "leave_notify_event",
		      GTK_SIGNAL_FUNC (editor_on_leave_notify), NULL);

  gb_widget_children_foreach (widget,
			      (GtkCallback) add_mouse_signals_recursive, NULL);
}

/* We need to be careful about passing events on to widgets, especially with
   regard to mouse grabs - in a GtkEntry the mouse is grabbed while selecting
   text, and this can cause all sorts of problems for Glade. */
void
editor_add_mouse_signals (GtkWidget * widget)
{
  /* Widgets without windows will not get events directly from X Windows,
     but they may have child widgets which pass events up to them, e.g.
     a GtkCombo has a GtkEntry which will get X events.
     This doesn't matter too much since we have to call a function to figure
     out which widget the event is for anyway. */
  add_mouse_signals_recursive (widget, NULL);

  gtk_signal_connect_after (GTK_OBJECT (widget), "realize",
			    GTK_SIGNAL_FUNC (editor_on_widget_realize), NULL);
}

void
editor_add_key_signals (GtkWidget * widget)
{
  /* We only add key signal handlers to windows. */
  if (!GTK_IS_WINDOW (widget))
    return;

  gtk_signal_connect (GTK_OBJECT (widget), "key_press_event",
		      GTK_SIGNAL_FUNC (editor_on_key_press_event), NULL);
  gtk_signal_connect (GTK_OBJECT (widget), "key_release_event",
		      GTK_SIGNAL_FUNC (editor_on_key_release_event), NULL);
}


void
on_size_allocate (GtkWidget * widget,
		  GtkAllocation *allocation,
		  GladeWidgetData * wdata)
{
  /* Reset the flag, since the size is allocated now. Note that wdata may be
     NULL as widget may be a placeholder. */
  if (wdata)
    {
      wdata->flags &= ~GLADE_SIZE_NOT_ALLOCATED;
    }

#if 0
  g_print ("In on_size_allocate: %s (%p) x:%i y:%i w:%i h:%i\n",
	   gtk_widget_get_name (widget), widget, allocation->x, allocation->y,
	   allocation->width, allocation->height);
#endif

  if (property_get_widget () == widget)
    {
      gb_widget_show_position_properties (widget);

      if (widget->parent && GTK_IS_FIXED (widget->parent))
	{
	  property_set_auto_apply (FALSE);
	  property_set_int (GladeFixedChildX,
			    allocation->x - widget->parent->allocation.x);
	  property_set_int (GladeFixedChildY,
			    allocation->y - widget->parent->allocation.y);
	  property_set_auto_apply (TRUE);
	}
      else if (widget->parent && GTK_IS_LAYOUT (widget->parent))
	{
	  property_set_auto_apply (FALSE);
	  property_set_int (GladeLayoutChildX, allocation->x);
	  property_set_int (GladeLayoutChildY, allocation->y);
	  property_set_auto_apply (TRUE);
	}
    }
}


void
editor_add_draw_signals (GtkWidget * widget)
{
  GladeWidgetData *widget_data;

  /* FIXME: Note that we set GDK_POINTER_MOTION_HINT_MASK here. This may not
     be wise since widgets may be designed to work with normal motion events
     only. Also this won't work if the widget is already realized. */
  if (!GTK_WIDGET_NO_WINDOW (widget) && !GTK_WIDGET_REALIZED (widget))
    gtk_widget_set_events (widget, gtk_widget_get_events (widget)
			   | GDK_EXPOSURE_MASK | GDK_POINTER_MOTION_MASK
			   | GDK_BUTTON1_MOTION_MASK
			   | GDK_POINTER_MOTION_HINT_MASK);

  widget_data = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);

  gtk_signal_connect (GTK_OBJECT (widget), "expose_event",
		      GTK_SIGNAL_FUNC (expose_widget), widget_data);
  gtk_signal_connect_after (GTK_OBJECT (widget), "size_allocate",
			    GTK_SIGNAL_FUNC (on_size_allocate), widget_data);

  /* FIXME: mouse signal - This also needs to be added to all children. */
  gtk_signal_connect (GTK_OBJECT (widget), "motion_notify_event",
		      GTK_SIGNAL_FUNC (editor_on_motion_notify), NULL);

  /* Needed for scrolled window, clist? & possibly other widgets */
  if (GTK_IS_CONTAINER (widget))
    gtk_container_forall (GTK_CONTAINER (widget),
			  (GtkCallback) editor_add_draw_signals, NULL);
}



static gint
expose_widget (GtkWidget * widget, GdkEventExpose * event,
	       GladeWidgetData * widget_data)
{
  GtkWidgetClass *class;

#if 0
  g_print ("In expose_event widget:%s (%p)\n", gtk_widget_get_name (widget),
	   widget);
  g_print ("Area x:%i y:%i w:%i h:%i\n", event->area.x, event->area.y,
	   event->area.width, event->area.height);
#endif

  /* Run the class handler here, then we return TRUE to stop the signal. */
  class = GTK_WIDGET_GET_CLASS (widget);
  if (class->expose_event)
    class->expose_event (widget, event);

  /* Ignore spurious exposes before widget is positioned. */
  if (widget->allocation.x == -1 || widget->allocation.y == -1)
    return TRUE;

  paint_widget (widget, event);

  return TRUE;
}


void
paint_widget (GtkWidget * widget, GdkEventExpose *event)
{
  static GdkPixmap *placeholder_pixmap = NULL;
  GType type;

  MSG3 ("Painting widget: %s W:%i H:%i", gtk_widget_get_name (widget),
	widget->allocation.width, widget->allocation.height);

  /* Check widget is drawable in case it has been deleted. */
  if (!GTK_WIDGET_DRAWABLE (widget))
    return;

  /* Don't try to draw anything if the width or height of the widget is 0. */
  if (widget->allocation.width == 0 || widget->allocation.height == 0)
    return;

  /* If widget is a placeholder, draw the placeholder pixmap in it and a
     3D border around it. */
  if (GB_IS_PLACEHOLDER (widget))
    {
      GdkGC *light_gc;
      GdkGC *dark_gc;
      gint w, h;

      light_gc = widget->style->light_gc[GTK_STATE_NORMAL];
      dark_gc = widget->style->dark_gc[GTK_STATE_NORMAL];
      gdk_window_get_size (widget->window, &w, &h);

      /* Draw the background pixmap. */
      if (placeholder_pixmap == NULL)
	{
	  /* FIXME: Use a hash of placeholder pixmaps? So we always use the
	     correct depth? */
	  placeholder_pixmap = gdk_pixmap_create_from_xpm_d (widget->window,
							     NULL, NULL,
							     placeholder_xpm);
	  if (!placeholder_pixmap)
	    {
	      g_warning ("Couldn't create placeholder pixmap\n");
	      /* FIXME: Use a color instead? */
	    }
	}

      if (placeholder_pixmap)
	{
	  gdk_gc_set_fill (light_gc, GDK_TILED);
	  gdk_gc_set_tile (light_gc, placeholder_pixmap);
	  gdk_draw_rectangle (widget->window, light_gc, TRUE, 0, 0, w, h);
	  gdk_gc_set_fill (light_gc, GDK_SOLID);
	}

      gdk_draw_line (widget->window, light_gc, 0, 0, w - 1, 0);
      gdk_draw_line (widget->window, light_gc, 0, 0, 0, h - 1);
      gdk_draw_line (widget->window, dark_gc, 0, h - 1, w - 1, h - 1);
      gdk_draw_line (widget->window, dark_gc, w - 1, 0, w - 1, h - 1);
    }

  if (event->window)
    {
      gpointer expose_widget;

      gdk_window_get_user_data (event->window, &expose_widget);

      if (expose_widget)
	{
	  gtk_idle_add_priority (GTK_PRIORITY_DEFAULT + 10,
				 (GtkFunction)editor_idle_handler,
				 event->window);

	  /* We ref the window, to make sure it isn't freed before the idle
	     handler. We unref it there. */
	  gdk_window_ref (event->window);
	}
    }

  /* Draw grid for fixed containers */
  type = G_OBJECT_TYPE (widget);
  if (GB_IS_GB_WIDGET (widget)
      && (type == GTK_TYPE_FIXED || type == GTK_TYPE_LAYOUT))
    draw_grid (widget);
}


/* This returns the window that the given widget's position is relative to.
   Usually this is the widget's parent's window. But if the widget is a
   toplevel, we use its own window, as it doesn't have a parent.
   Some widgets also lay out widgets in different ways. */
static GdkWindow*
glade_util_get_window_positioned_in (GtkWidget *widget)
{
	GtkWidget *parent;

	parent = widget->parent;

#ifdef USE_GNOME
	/* BonoboDockItem widgets use a different window when floating. */
	if (BONOBO_IS_DOCK_ITEM (widget)
	    && BONOBO_DOCK_ITEM (widget)->is_floating) {
		return BONOBO_DOCK_ITEM (widget)->float_window;
	}

	if (parent && BONOBO_IS_DOCK_ITEM (parent)
	    && BONOBO_DOCK_ITEM (parent)->is_floating) {
		return BONOBO_DOCK_ITEM (parent)->float_window;
	}
#endif

	if (parent)
		return parent->window;

	return widget->window;
}

static void
glade_util_draw_nodes (GdkWindow *window, GdkGC *gc,
		       gint x, gint y,
		       gint width, gint height)
{
#if 0
  g_print ("draw_nodes window: %p %i,%i %ix%i\n",
	   window, x, y, width, height);
#endif
	if (width > GB_CORNER_WIDTH && height > GB_CORNER_HEIGHT) {
		gdk_draw_rectangle (window, gc, TRUE,
				    x, y,
				    GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
		gdk_draw_rectangle (window, gc, TRUE,
				    x, y + height - GB_CORNER_HEIGHT,
				    GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
		gdk_draw_rectangle (window, gc, TRUE,
				    x + width - GB_CORNER_WIDTH, y,
				    GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
		gdk_draw_rectangle (window, gc, TRUE,
				    x + width - GB_CORNER_WIDTH,
				    y + height - GB_CORNER_HEIGHT,
				    GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
	}

	gdk_draw_rectangle (window, gc, FALSE, x, y, width - 1, height - 1);
}

/* This calculates the offset of the given window within its toplevel.
   It also returns the toplevel. */
static void
glade_util_calculate_window_offset (GdkWindow *window,
				    gint *x, gint *y,
				    GdkWindow **toplevel)
{
	gint tmp_x, tmp_y;

	/* Calculate the offset of the window within its toplevel. */
	*x = 0;
	*y = 0;

	for (;;) {
		if (gdk_window_get_window_type (window) != GDK_WINDOW_CHILD)
			break;
		gdk_window_get_position (window, &tmp_x, &tmp_y);
		*x += tmp_x;
		*y += tmp_y;
		window = gdk_window_get_parent (window);
	}

	*toplevel = window;
}

/* This returns TRUE if it is OK to draw the selection nodes for the given
   selected widget inside the given window that has received an expose event.
   For most widgets it returns TRUE, but if a selected widget is inside a
   widget like a viewport, that uses its own coordinate system, then it only
   returns TRUE if the expose window is inside the viewport as well. */
static gboolean
glade_util_can_draw_nodes (GtkWidget *sel_widget, GdkWindow *sel_win,
			   GdkWindow *expose_win)
{
	GtkWidget *widget, *viewport = NULL;
	GdkWindow *viewport_win = NULL;

	/* Check if the selected widget is inside a viewport. */
	for (widget = sel_widget->parent; widget; widget = widget->parent) {
		if (GTK_IS_VIEWPORT (widget)) {
			viewport = widget;
			viewport_win = GTK_VIEWPORT (widget)->bin_window;
			break;
		}
	}

	/* If there is no viewport-type widget above the selected widget,
	   it is OK to draw the selection anywhere. */
	if (!viewport)
		return TRUE;

	/* If we have a viewport-type widget, check if the expose_win is
	   beneath the viewport. If it is, we can draw in it. If not, we
	   can't.*/
	for (;;) {
		if (expose_win == sel_win)
			return TRUE;
		if (expose_win == viewport_win)
			return FALSE;
		if (gdk_window_get_window_type (expose_win) != GDK_WINDOW_CHILD)
			break;
		expose_win = gdk_window_get_parent (expose_win);
	}

	return FALSE;
}


/* This is coped from glade3/src/glade-utils.c glade_util_draw_nodes_idle(). */
static gint
editor_idle_handler (GdkWindow *expose_win)
{
	GtkWidget *expose_widget;
	gint expose_win_x, expose_win_y;
	gint expose_win_w, expose_win_h;
	GdkWindow *expose_toplevel;
	GdkGC *gc;
	GList *elem;
	gpointer expose_widget_ptr;

	/* Find the corresponding GtkWidget. */
	gdk_window_get_user_data (expose_win, &expose_widget_ptr);
	expose_widget = GTK_WIDGET (expose_widget_ptr);

	/* Check that the window is still alive. */
	if (!expose_widget || !GTK_WIDGET_DRAWABLE (expose_widget)
	    || !gdk_window_is_viewable (expose_win))
		goto out;

	gc = expose_widget->style->black_gc;

	/* Calculate the offset of the expose window within its toplevel. */
	glade_util_calculate_window_offset (expose_win,
					    &expose_win_x,
					    &expose_win_y,
					    &expose_toplevel);

#if 0
	g_print ("expose_win: %p x: %i y: %i toplevel: %p\n",
		 expose_win, expose_win_x, expose_win_y, expose_toplevel);
#endif
	gdk_drawable_get_size (expose_win,
			       &expose_win_w, &expose_win_h);
#if 0
	g_print ("drawable size %ix%i\n", expose_win_w, expose_win_h);
#endif
	/* Step through all the selected widgets in the project. */
	for (elem = selected_widgets; elem; elem = elem->next) {
		GtkWidget *sel_widget;
		GdkWindow *sel_win, *sel_toplevel;
		gint sel_x, sel_y, x, y, w, h, sel_alloc_x, sel_alloc_y;

		sel_widget = elem->data;

		/* Skip the selected widget if it isn't realized. */
		if (!GTK_WIDGET_REALIZED (sel_widget))
		  continue;

		sel_win = glade_util_get_window_positioned_in (sel_widget);

		/* Calculate the offset of the selected widget's window
		   within its toplevel. */
		glade_util_calculate_window_offset (sel_win, &sel_x, &sel_y,
						    &sel_toplevel);

#if 0
		g_print ("sel_win: %p x: %i y: %i toplevel: %p allocation %i,%i\n",
			 sel_win, sel_x, sel_y, sel_toplevel,
			 sel_widget->allocation.x, sel_widget->allocation.y);
#endif
		/* Toplevel windows/dialogs may have their allocation set
		   relative to the root window, so we need to ignore that. */
		if (sel_widget->parent)
		  {
		    sel_alloc_x = sel_widget->allocation.x;
		    sel_alloc_y = sel_widget->allocation.y;
		  }
		else
		  {
		    sel_alloc_x = 0;
		    sel_alloc_y = 0;
		  }		    

		/* We only draw the nodes if the window that got the expose
		   event is in the same toplevel as the selected widget. */
		if (expose_toplevel == sel_toplevel
		    && glade_util_can_draw_nodes (sel_widget, sel_win,
						  expose_win)) {
			x = sel_x + sel_alloc_x - expose_win_x;
			y = sel_y + sel_alloc_y - expose_win_y;
			w = sel_widget->allocation.width;
			h = sel_widget->allocation.height;

#if 0
			g_print ("checking coords %i,%i %ix%i\n",
				 x, y, w, h);
#endif
			/* Draw the selection nodes if they intersect the
			   expose window bounds. */
			if (x < expose_win_w && x + w >= 0
			    && y < expose_win_h && y + h >= 0) {
				glade_util_draw_nodes (expose_win, gc,
						       x, y, w, h);
			}
		}
	}

 out:
	/* Remove the reference added in glade_util_queue_draw_nodes(). */
	gdk_window_unref (expose_win);

	/* Return FALSE so the idle handler isn't called again. */
	return FALSE;
}


static void
draw_grid (GtkWidget * widget)
{
  GdkGC *gc = widget->style->dark_gc[GTK_STATE_NORMAL];
  gint min_x = 0, max_x = widget->allocation.width - 1;
  gint min_y = 0, max_y = widget->allocation.height - 1;
  gint gridx, gridy, origin_x, origin_y;
  GdkWindow *window;

  if (!editor_show_grid)
    return;

  if (GTK_IS_LAYOUT (widget))
    {
      gint offset;

      /* The window size is the entire size of the layout. The allocation is
	 the part that is showing. */
      window = GTK_LAYOUT (widget)->bin_window;

      origin_x = (int) GTK_LAYOUT (widget)->hadjustment->value;
      min_x += origin_x;
      max_x += origin_x;
      offset = origin_x % editor_grid_horz_spacing;
      if (offset != 0)
	origin_x += editor_grid_horz_spacing - offset;

      origin_y = (int) GTK_LAYOUT (widget)->vadjustment->value;
      min_y += origin_y;
      max_y += origin_y;
      offset = origin_y % editor_grid_vert_spacing;
      if (offset != 0)
	origin_y += editor_grid_vert_spacing - offset;
    }
  else
    {
      /* The GtkFixed container doesn't have a window in GTK+ 2.0 (by default),
	 so we have to use the allocation. */
      window = widget->window;
      origin_x = widget->allocation.x;
      origin_y = widget->allocation.y;
      max_x += origin_x;
      max_y += origin_y;
    }

  /* Note: should we take the border_width into account? - i.e. start the
     grid inside the border. It makes it awkward if you change the border
     size. */
  if (editor_grid_style == GB_GRID_DOTS)
    {
      for (gridx = origin_x; gridx <= max_x; gridx += editor_grid_horz_spacing)
	{
	  for (gridy = origin_y; gridy <= max_y;
	       gridy += editor_grid_vert_spacing)
	    gdk_draw_point (window, gc, gridx, gridy);
	}
    }
  else
    {
      for (gridx = origin_x; gridx <= max_x; gridx += editor_grid_horz_spacing)
	gdk_draw_line (window, gc, gridx, min_y, gridx, max_y);
      for (gridy = origin_y; gridy <= max_y; gridy += editor_grid_vert_spacing)
	gdk_draw_line (window, gc, min_x, gridy, max_x, gridy);
    }
}

/*
 * Redraw the given widget completely, including all space allocated by its
 * parent (since this may be used for drawing the widget's selection).
 * If widget has no parent (i.e. its a toplevel window) just clear
 * it all and redraw.
 */

void
editor_refresh_widget (GtkWidget * widget)
{
#if 0
  g_print ("In editor_refresh_widget widget: %s (%p)\n",
	   gtk_widget_get_name (widget), widget);
#endif

  editor_refresh_widget_area (widget,
			      widget->allocation.x,
			      widget->allocation.y,
			      widget->allocation.width,
			      widget->allocation.height);
  gtk_widget_draw (widget, NULL);
}


void
editor_refresh_widget_selection (GtkWidget * widget)
{
  gint x, y, w, h;

#if 0
  g_print ("In editor_refresh_widget_selection widget: %s (%p)\n",
	   gtk_widget_get_name (widget), widget);
#endif

  x = widget->allocation.x;
  y = widget->allocation.y;
  w = widget->allocation.width;
  h = widget->allocation.height;

  /* Don't try to refresh an area if the width or height is 0. */
  if (w == 0 || h == 0)
    return;

  /* Clear the four corners. */
  editor_refresh_widget_area (widget,
			      x, y,
			      GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
  editor_refresh_widget_area (widget,
			      x, y + h - GB_CORNER_HEIGHT,
			      GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
  editor_refresh_widget_area (widget,
			      x + w - GB_CORNER_WIDTH, y,
			      GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
  editor_refresh_widget_area (widget,
			      x + w - GB_CORNER_WIDTH,
			      y + h - GB_CORNER_HEIGHT,
			      GB_CORNER_WIDTH, GB_CORNER_HEIGHT);
  /* Clear the four lines along the edges. */
  editor_refresh_widget_area (widget,
			      x + GB_CORNER_WIDTH, y,
			      w - 2 * GB_CORNER_WIDTH, 1);
  editor_refresh_widget_area (widget,
			      x + GB_CORNER_WIDTH, y + h - 1,
			      w - 2 * GB_CORNER_WIDTH, 1);
  editor_refresh_widget_area (widget,
			      x, y + GB_CORNER_HEIGHT,
			      1, h - 2 * GB_CORNER_HEIGHT);
  editor_refresh_widget_area (widget,
			      x + w - 1,
			      y + GB_CORNER_HEIGHT,
			      1, h - 2 * GB_CORNER_HEIGHT);

  gtk_widget_draw (widget, NULL);
}


void
editor_refresh_widget_area (GtkWidget * widget, gint x, gint y, gint w, gint h)
{
  GdkWindow *window;

  if (!GTK_WIDGET_DRAWABLE (widget))
    return;

  /* Don't try to refresh an area if the width or height is 0. */
  if (w <= 0 || h <= 0)
    return;

  window = glade_util_get_window_positioned_in (widget);
  gdk_window_clear_area (window, x, y, w, h);
  clear_child_windows (window, x, y, w, h);
}


/* This clears all child windows which fall within the given rectangle.
   If the rectangle width is -1, then all children are cleared. */
static void
clear_child_windows (GdkWindow * window, gint x, gint y, gint w, gint h)
{
  GList *children, *orig_children;
  GdkWindow *child_window;
  gint win_x, win_y, win_w, win_h;
  GdkRectangle area, child, intersection;

  area.x = x;
  area.y = y;
  area.width = w;
  area.height = h;

  orig_children = children = gdk_window_get_children (window);
  while (children)
    {
      child_window = children->data;
      gdk_window_get_position (child_window, &win_x, &win_y);
      gdk_window_get_size (child_window, &win_w, &win_h);

      child.x = win_x;
      child.y = win_y;
      child.width = win_w;
      child.height = win_h;

      if (gdk_rectangle_intersect (&area, &child, &intersection))
	{
	  /* We need to make sure this is not an InputOnly window, or we get
	     a BadMatch. CList uses InputOnly windows - for resizing columns.
	  */
	  if (! GDK_WINDOW_OBJECT(child_window)->input_only)
	    {
	      /* Convert to the child's coordinate space. */
	      intersection.x -= child.x;
	      intersection.y -= child.y;
	      gdk_window_clear_area (child_window,
				     intersection.x, intersection.y,
				     intersection.width, intersection.height);
	      clear_child_windows (child_window,
				   intersection.x, intersection.y,
				   intersection.width, intersection.height);
	    }
	}
      children = children->next;
    }
  g_list_free (orig_children);
}



/*
 * Key events
 */
static gint
editor_on_key_press_event (GtkWidget * widget, GdkEventKey * event,
			   gpointer data)
{
  gboolean handled = FALSE;
  guint key = event->keyval;

  MSG ("In on_key_press_event");
  switch (key)
    {
    case GDK_Delete:
      /* If we are typing over the widget, the delete key is used for that
	 rather than deleting the widget. */
      if (!property_is_typing_over_widget ())
	{
	  if (selected_widgets)
	    delete (selected_widgets->data);
	  handled = TRUE;
	}
      break;
    case GDK_Escape:
      editor_clear_selection (NULL);
      handled = TRUE;
      break;
    case GDK_l:
      /* Ctrl-L refreshes the entire window/dialog. */
      if (event->state & GDK_CONTROL_MASK)
	{
	  editor_refresh_widget (glade_util_get_toplevel (widget));
	  handled = TRUE;
	}
      break;
    case GDK_r:
      /* Ctrl-R hides the window and shows it again in the same position.
	 Hopefully it will appear at the 'real' size. */
      if (event->state & GDK_CONTROL_MASK)
	{
	  GtkWidget *toplevel;

	  /* See also gb_widget_redisplay_window() in gbwidget.c. */
	  toplevel = glade_util_get_toplevel (widget);
	  glade_util_close_window (toplevel);
	  gtk_window_reshow_with_initial_size (GTK_WINDOW (toplevel));
	  handled = TRUE;
	}
      break;
    }

#if 0
  /* We don't want modifier keys to be redirected, since trying to use an
     accelerator, e.g. Ctrl-X to cut the widget, would clear the label. */
  if (!handled
      && !(event->state & GDK_CONTROL_MASK)
      && key != GDK_Caps_Lock
      && key != GDK_Tab && key != GDK_KP_Tab
      && key != GDK_Left && key != GDK_KP_Left
      && key != GDK_Right && key != GDK_KP_Right
      && key != GDK_Up && key != GDK_KP_Up
      && key != GDK_Down && key != GDK_KP_Down
      && key != GDK_Page_Up && key != GDK_KP_Page_Up
      && key != GDK_Page_Down && key != GDK_KP_Page_Down
      && key != GDK_Home && key != GDK_KP_Home
      && key != GDK_End && key != GDK_KP_End
      && key != GDK_Control_L && key != GDK_Control_R
      && key != GDK_Shift_L && key != GDK_Shift_R
      && key != GDK_Meta_L && key != GDK_Meta_R
      && key != GDK_Alt_L && key != GDK_Alt_R
      && key != GDK_Super_L && key != GDK_Super_R
      && key != GDK_Hyper_L && key != GDK_Hyper_R)
    {
      /* Experimental code. */
#if 0
      g_print ("Set label? widget=%s\n",
	       mouse_over_widget ? gtk_widget_get_name (mouse_over_widget) : "NULL");
#endif
      if (mouse_over_widget
	  && (GTK_IS_ACCEL_LABEL (mouse_over_widget)
	      || GTK_IS_LABEL (mouse_over_widget)
	      || GTK_IS_BUTTON (mouse_over_widget)))
	{
	  gb_widget_show_properties (mouse_over_widget);
	  property_redirect_key_press (event);
	  handled = TRUE;
	}
    }
#endif

  if (handled)
    {
#if 0
      gtk_signal_emit_stop_by_name (GTK_OBJECT (widget), "key_press_event");
#endif
      return TRUE;
    }
  return FALSE;
}


static gint
editor_on_key_release_event (GtkWidget * widget, GdkEventKey * event,
			     gpointer data)
{
  MSG ("In on_key_release_event");

  return FALSE;
}



/* This is when the 'Select' menuitem on the popup menu is selected */
void
editor_on_select_activate (GtkWidget * menuitem, GtkWidget * widget)
{
  editor_select_widget (widget, NULL, 0, 0);
}



void
editor_on_delete ()
{
  if (selected_widgets)
    delete (selected_widgets->data);
}


/* This is when the 'Cut' menuitem on the popup menu is selected */
void
editor_on_cut_activate (GtkWidget * menuitem, GtkWidget * widget)
{
  glade_clipboard_cut (GLADE_CLIPBOARD (glade_clipboard), current_project,
		       widget);
}


/* This is when the 'Copy' menuitem on the popup menu is selected */
void
editor_on_copy_activate (GtkWidget * menuitem, GtkWidget * widget)
{
  glade_clipboard_copy (GLADE_CLIPBOARD (glade_clipboard), current_project,
			widget);
}


/* This is when the 'Cut' menuitem on the popup menu is selected */
void
editor_on_paste_activate (GtkWidget * menuitem, GtkWidget * widget)
{
  glade_clipboard_paste (GLADE_CLIPBOARD (glade_clipboard), current_project,
			 widget);
}


/* This is when the 'Delete' menuitem on the popup menu is selected */
void
editor_on_delete_activate (GtkWidget * menuitem, GtkWidget * widget)
{
  delete (widget);
}


static void
delete (GtkWidget * widget)
{
  if (GB_IS_PLACEHOLDER (widget))
    delete_placeholder (widget);
  else
    editor_delete_widget (widget);
}


static void
delete_placeholder (GtkWidget * placeholder)
{
  GtkWidget *parent = placeholder->parent;
  gchar *child_name;

  /* SPECIAL CODE: Don't allow placeholders in clist titles to be deleted. */
  child_name = gb_widget_get_child_name (placeholder);
  if (child_name)
    {
      if (!strcmp (child_name, GladeChildCListTitle))
	{
	  MSG1 ("Not deleting special widget: %s\n", child_name);
	  return;
	}
    }

  /* SPECIAL CODE: Don't allow placeholder in BonoboDock to be deleted. */
#ifdef USE_GNOME	  
  if (BONOBO_IS_DOCK (parent))
    return;
#endif

  /* Remove widget from the selection */
  editor_clear_selection (NULL);

  /* Can't delete children of a paned or a viewport */
  if (GTK_IS_PANED (parent) || GTK_IS_VIEWPORT (parent))
    return;

  /* For a Clist, we can delete everything except column title widgets */
  if (GTK_IS_CLIST (parent))
    {
      g_warning ("Deleting a widget in a clist - not implemented yet");
      return;
    }

  /* If the parent is a toolitem, delete that. */
  if (GTK_IS_TOOL_ITEM (parent))
    {
      gtk_container_remove (GTK_CONTAINER (parent->parent), parent);
      return;
    }

  /* Widgets with these parents can all be deleted OK */
  if (GTK_IS_TOOLBAR (parent)
#if GLADE_SUPPORTS_GTK_TREE
      || GTK_IS_TREE (parent)
#endif
      || GTK_IS_LIST (parent))
    {
      gtk_widget_destroy (placeholder);
      return;
    }

  /* For a frame we can delete a placeholder in the label widget on its own.
     Otherwise we delete the parent, just like GtkBin. */
  if (GTK_IS_FRAME (parent))
    {
      if (gtk_frame_get_label_widget (GTK_FRAME (parent)) == placeholder)
	gtk_widget_destroy (placeholder);
      else
	editor_delete_widget (parent);
      return;
    }

  /* For these widgets replace the parent with a placeholder, or delete the
     component if parent is a toplevel widget */
  if (GTK_IS_BIN (parent) || GTK_IS_BUTTON (parent))
    {
      editor_delete_widget (parent);
      return;
    }

  /* For a box, if the placeholder is the only child then replace the box with
     a placeholder, else just delete the placeholder */
  if (GTK_IS_BOX (parent))
    {
      if (g_list_length (GTK_BOX (parent)->children) == 1)
	{
	  editor_delete_widget (parent);
	}
      else
	{
	  gtk_container_remove (GTK_CONTAINER (parent), placeholder);
	  /* Shouldn't really need to do this */
	  gtk_widget_queue_resize (parent);
	}
      return;
    }

  /* For a notebook, if placeholder is the only page, replace the notebook with
     a placeholder, else delete the current notebook page (i.e. placeholder) */
  if (GTK_IS_NOTEBOOK (parent))
    {
      if (g_list_length (GTK_NOTEBOOK (parent)->children) == 1)
	{
	  editor_delete_widget (parent);
	}
      else
	{
	  gtk_notebook_remove_page (GTK_NOTEBOOK (parent), gtk_notebook_get_current_page (GTK_NOTEBOOK (parent)));
	}
      return;
    }

  /* For a table, can't delete placeholder, unless there is only 1 row or
     column. In this case delete the placeholder, and move all the other
     children up/left. If the table is 1 x 1 then delete the table. */
  if (GTK_IS_TABLE (parent))
    {
      gint nrows, ncols, position = 0, distance_to_move = 0;
      GList *item;
      GtkTableChild *table_child;

      nrows = GTK_TABLE (parent)->nrows;
      ncols = GTK_TABLE (parent)->ncols;
      if (nrows > 1 && ncols > 1)
	return;
      if (nrows == 1 && ncols == 1)
	{
	  editor_delete_widget (parent);
	  return;
	}

      /* Find out where placeholder is */
      item = GTK_TABLE (parent)->children;
      while (item)
	{
	  table_child = (GtkTableChild *) item->data;

	  if (table_child->widget == placeholder)
	    {
	      /* Calculate how far up/left we will have to move the rest of the
	         children */
	      if (nrows == 1)
		{
		  position = table_child->left_attach;
		  distance_to_move = table_child->right_attach
		    - table_child->left_attach;
		}
	      else
		{
		  position = table_child->top_attach;
		  distance_to_move = table_child->bottom_attach
		    - table_child->top_attach;
		}
	      break;
	    }
	  item = item->next;
	}
      /* Shouldn't reach the end of the list */
      g_return_if_fail (item != NULL);
      gtk_widget_destroy (placeholder);

      /* Now step through the table again, moving children up or left */
      item = GTK_TABLE (parent)->children;
      while (item)
	{
	  table_child = (GtkTableChild *) item->data;
	  if (nrows == 1)
	    {
	      if (table_child->left_attach > position)
		{
		  table_child->left_attach -= distance_to_move;
		  table_child->right_attach -= distance_to_move;
		}
	    }
	  else
	    {
	      if (table_child->top_attach > position)
		{
		  table_child->top_attach -= distance_to_move;
		  table_child->bottom_attach -= distance_to_move;
		}
	    }
	  item = item->next;
	}

      /* Now update the tables nrows & ncols */
      if (nrows == 1)
	GTK_TABLE (parent)->ncols -= distance_to_move;
      else
	GTK_TABLE (parent)->nrows -= distance_to_move;

      return;
    }

  g_warning ("Don't know how to delete widget");
}


void
editor_delete_widget (GtkWidget * widget)
{
  GtkWidget *parent, *placeholder;
  gchar *error;

  MSG1 ("In editor_delete_widget: %s\n", gtk_widget_get_name (widget));

  error = editor_can_delete_widget (widget);
  if (error)
    {
      glade_util_show_message_box (error, widget);
      return;
    }

  /* If we are deleting a GtkTextView set the text to "". This avoids an odd
     crash. See bug #111604. */
  if (GTK_IS_TEXT_VIEW (widget))
    gtk_text_buffer_set_text (gtk_text_view_get_buffer (GTK_TEXT_VIEW (widget)), "", 0);

  /* Set properties widget to NULL, in case the widget or parent is deleted */
  property_set_widget (NULL);

  /* Remove widget from the selection */
  editor_clear_selection (NULL);

  if (GTK_IS_MENU (widget))
    parent = gtk_menu_get_attach_widget (GTK_MENU (widget));
  else
    parent = widget->parent;

  /* If widget is a toplevel widget (i.e. a project component) delete the
     component. */
  if (parent == NULL)
    {
      glade_project_remove_component (current_project, widget);
      return;
    }

  /* If the widget's parent is a fixed container or a packer remove the widget
     completely. */
  if (GTK_IS_FIXED (widget->parent)
#if GLADE_SUPPORTS_GTK_PACKER
      || GTK_IS_PACKER (widget->parent)
#endif
      || GTK_IS_LAYOUT (widget->parent))
    {
      gtk_widget_destroy (widget);
      return;
    }

  /* If the widget's parent is a button box remove widget completely. */
  if (GTK_IS_BUTTON_BOX (widget->parent))
    {
      gtk_widget_destroy (widget);
      return;
    }

  /* If the widget is a menu item remove widget completely. */
  if (GTK_IS_MENU_ITEM (widget))
    {
      gtk_widget_destroy (widget);
      return;
    }

#ifdef USE_GNOME
  /* GnomeDockItem widgets are also removed completely rather than being
     replaced by a placeholder. */
  if (BONOBO_IS_DOCK_ITEM (widget))
    {
      gtk_widget_destroy (widget);
      return;
    }

  /* If this is a page in a GnomeDruid, then if it is the only page delete
     the entire GnomeDruid, else delete the page. We need to make sure the
     current page is set to something else before deleting the page. */
  if (GNOME_IS_DRUID_PAGE (widget))
    {
      gint num_pages;
      GList *children, *elem;
      GnomeDruidPage *new_current_page;

      g_return_if_fail (GNOME_IS_DRUID (parent));

      children = gtk_container_get_children (GTK_CONTAINER (parent));
      num_pages = g_list_length (children);
      if (num_pages == 1)
	{
	  editor_delete_widget (parent);
	}
      else
	{
	  elem = g_list_find (children, widget);
	  g_return_if_fail (elem != NULL);

	  if (elem->next)
	    new_current_page = elem->next->data;
	  else
	    new_current_page = elem->prev->data;

	  gnome_druid_set_page (GNOME_DRUID (parent), new_current_page);

	  gtk_widget_destroy (widget);
	}

      g_list_free (children);

      return;
    }
#endif

  /* Replace normal widget's with a placeholder  & select it so it can also
     be deleted easily using the Delete key. But we must be careful since
     there is a slight chance that the placeholder will be automatically
     destroyed, e.g. if it is placed in a table which already has another
     widget in the same position. */
  placeholder = editor_new_placeholder ();
  gtk_widget_ref (placeholder);
  if (gb_widget_replace_child (widget->parent, widget, placeholder))
    {
      if (placeholder->parent)
	editor_select_widget (placeholder, NULL, 0, 0);
      gtk_widget_unref (placeholder);
    }
  else
    {
      glade_util_show_message_box (_("Couldn't delete widget."), widget);
      gtk_object_sink (GTK_OBJECT (placeholder));
      gtk_widget_unref (placeholder);
    }

  MSG ("Out editor_delete_widget");
}


/* This sees if a widget can be deleted. It returns an appropriate error
   message if it can't. */
gchar*
editor_can_delete_widget (GtkWidget * widget)
{
  gchar *child_name;

  /* Button & item children are special - they can be deleted. */
  if (widget->parent && GB_IS_GB_WIDGET (widget->parent)
      && (GTK_IS_BUTTON (widget->parent) || GTK_IS_ITEM (widget->parent)))
    return NULL;

  /* Don't allow widgets which aren't GbWidgets to be deleted, since we know
     nothing about them. */
  if (!GB_IS_PLACEHOLDER (widget) && !GB_IS_GB_WIDGET (widget))
    return _("The widget can't be deleted");

  /* Non-toplevel menus are created automatically so we can't delete them. */
  if (GTK_IS_MENU (widget) && gtk_menu_get_attach_widget (GTK_MENU (widget)))
    return _("The widget can't be deleted");


  /* SPECIAL CODE: Don't allow dialog buttons & widgets to be deleted. */
  child_name = gb_widget_get_child_name (widget);
  if (child_name)
    {
      if (!strcmp (child_name, GladeChildOKButton)
	  || !strcmp (child_name, GladeChildCancelButton)
	  || !strcmp (child_name, GladeChildHelpButton)
	  || !strcmp (child_name, GladeChildApplyButton)
	  || !strcmp (child_name, GladeChildSaveButton)
	  || !strcmp (child_name, GladeChildCloseButton)
	  || !strcmp (child_name, GladeChildDialogVBox)
	  || !strcmp (child_name, GladeChildDialogActionArea)
	  || !strcmp (child_name, GladeChildComboEntry)
	  || !strcmp (child_name, GladeChildComboList)
	  || !strcmp (child_name, GladeChildFontSelection)
	  || !strcmp (child_name, GladeChildColorSelection)
#ifdef USE_GNOME
	  || !strcmp (child_name, GladeChildGnomeAppDock)
	  || !strcmp (child_name, GladeChildGnomeAppBar)
	  || !strcmp (child_name, GladeChildGnomeDruidVBox)
	  || !strcmp (child_name, GladeChildGnomeEntry)
#endif
	  )
	{
	  return _("The widget is created automatically as part of the parent widget, and it can't be deleted.");
	}
    }

  return NULL;
}


/* Called when a GbWidget is destroyed so the editor can remove any references
   to it. */
void
editor_on_widget_destroyed		(GtkWidget	    *widget)
{
#if 0
  const char *name = gtk_widget_get_name (widget);
  g_print ("In editor_on_widget_destroyed: %s\n", name ? name : "NULL");
#endif

  if (mouse_over_widget == widget)
    {
#if 0
      g_print ("  resetting mouse_over_widget to NULL\n");
#endif
      mouse_over_widget = NULL;
    }
}