summaryrefslogtreecommitdiff
path: root/tools/glade/glade/gbwidget.c
blob: 06969c462e918d83e64a87f83036850569d2d8bf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
/*  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 <gtk/gtk.h>
#include "gladeconfig.h"

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

#include "editor.h"
#include "gb.h"
#include "gbwidget.h"
#include "glade.h"
#include "glade_atk.h"
#include "glade_gnome.h"
#include "glade_gtk12lib.h"
#include "glade_plugin.h"
#include "glade_project.h"
#include "load.h"
#include "palette.h"
#include "property.h"
#include "save.h"
#include "tree.h"
#include "utils.h"

#ifdef USE_GNOME
#include "glade_gnomelib.h"
#endif

#ifdef USE_GNOME_DB
#include "glade_gnomedblib.h"
#endif

/*
 * The hash table associates a Gtk widget class name with a GbWidget struct,
 * which contains a table of functions used by the builder, e.g. creating a
 * new widget, saving the widget etc.
 */
static GHashTable *gb_widget_table = NULL;

/* Tooltips for the created widgets */
static GtkTooltips *gb_widget_tooltips;


/* This is the GTK+ stock id string, used in GtkImageMenuItem widgets. */
const gchar *GladeMenuItemStockIDKey = "GladeMenuItemStockIDKey";

/* This is the old Gnome stock index key. */
const gchar *GladeMenuItemStockIndexKey = "GladeMenuItemStockIndexKey";

/* This is the key we use to store the stock icon name or full pathname. */
const gchar *GladeIconKey = "GladeIconKey";

/* This is used to save a pointer to the GladeWidgetInfo inside a widget while
   loading, so we can resolve ATK relations afterwards. */
const gchar *GladeWidgetInfoKey = "GladeWidgetInfoKey";


static void gb_widget_init_widget_lib (GladePaletteSectionData *sections);

static void on_widget_destroy (GtkWidget * widget,
			       gpointer data);

#ifdef GLADE_STYLE_SUPPORT
static void show_color_properties (GdkColor colors[],
				   gchar * name);
#endif
static void show_accelerators (GtkWidget * widget,
			       GbWidgetGetArgData * data);
static void show_signals (GtkWidget * widget,
			  GbWidgetGetArgData * data);

static void set_standard_properties (GtkWidget * widget,
				     GbWidgetSetArgData * data);
static void set_position_properties (GtkWidget * widget,
				     GbWidgetSetArgData * data);
static void set_special_child_properties (GtkWidget * widget,
					  GbWidgetSetArgData * data);
static void set_lang_specific_properties (GtkWidget * widget,
					  GbWidgetSetArgData * data);
#if 0
static void apply_style (GtkWidget * widget,
			 GbWidgetSetArgData * data);
static gboolean apply_colors (GtkWidget * widget,
			      GbWidgetSetArgData * data,
			      GdkColor colors[],
			      GdkColor new_colors[],
			      gchar * name);
#endif
static void apply_accelerators (GtkWidget * widget,
				GbWidgetSetArgData * data);
static void apply_signals (GtkWidget * widget,
			   GbWidgetSetArgData * data);

static void add_standard_top_menu_items (GtkWidget * widget,
					 GbWidgetCreateMenuData * data);
static void add_standard_bottom_menu_items (GtkWidget * widget,
					    GbWidgetCreateMenuData * data);

static void get_standard_properties (GtkWidget * widget,
				     GbWidgetGetArgData * data);

static void get_position_properties (GtkWidget * widget,
				     GbWidgetGetArgData * data);
static void get_lang_specific_properties (GtkWidget * widget,
					  GbWidgetGetArgData * data);

static void save_accelerators (GtkWidget * widget,
			       GbWidgetGetArgData * data);
static void save_signals (GtkWidget * widget,
			  GbWidgetGetArgData * data);


static void gb_widget_add_alignment (GtkWidget * menuitem,
				     GtkWidget * widget);
static void gb_widget_remove_alignment (GtkWidget * menuitem,
					GtkWidget * widget);
static void gb_widget_add_event_box (GtkWidget * menuitem,
				     GtkWidget * widget);
static void gb_widget_remove_event_box (GtkWidget * menuitem,
					GtkWidget * widget);
static void gb_widget_redisplay_window (GtkWidget * menuitem,
					GtkWidget * widget);
static void gb_widget_add_scrolled_window (GtkWidget * menuitem,
					   GtkWidget * widget);
static void gb_widget_remove_scrolled_window (GtkWidget * menuitem,
					      GtkWidget * widget);

static void table_foreach (GtkTable * table,
			   GtkCallback callback,
			   gpointer callback_data);
static void box_foreach (GtkBox *box,
			 GtkCallback callback,
			 gpointer callback_data);

static gint find_notebook_page (GtkNotebook * notebook,
				GtkWidget * current_child,
				GtkWidget **page,
				GtkWidget **tab_label);


/* These aren't included in the Bonobo headers, so we declare them here to
   avoid warnings. */
#ifdef USE_GNOME
void bonobo_dock_item_get_floating_position (BonoboDockItem *item,
					     gint *x, gint *y);
gboolean bonobo_dock_item_detach (BonoboDockItem *item, gint x, gint y);
#endif

/*************************************************************************
 * Initialization functions
 *************************************************************************/

void
gb_widgets_init ()
{
  gb_widget_table = g_hash_table_new (g_str_hash, g_str_equal);

#ifdef GLADE_STYLE_SUPPORT
  gb_widget_reset_gb_styles ();
#endif

  /* Create tooltips */
  gb_widget_tooltips = gtk_tooltips_new ();

  gb_widget_init_widget_lib (get_gtk_widgets());
#ifdef USE_GNOME
  gb_widget_init_widget_lib (get_gnome_widgets());
#endif
#ifdef USE_GNOME_DB
  gb_widget_init_widget_lib (get_gnome_db_widgets());
#endif

  glade_plugin_load_plugins ();
}

static void
gb_widget_init_widget_lib (GladePaletteSectionData *sections)
{
  gint index, j;
  GladeWidgetInitData *gwid;
  GladePaletteSectionData *palsec;
  GbWidget *(*init_func) ();
  GbWidget *gbwidget;

  index = 0;
  while (1)
    {
      j = 0;
      palsec = &sections[index];
      index++;
      if (!palsec->section)
        break;
      while (1)
        {
          gwid = &palsec->widgets[j];
          j++;
          if (!gwid->name)
            break;  
          init_func = gwid->init_func;
          gbwidget = (*init_func) ();
	  gb_widget_register_gbwidget (gwid->name, gbwidget);
	  palette_add_gbwidget (gbwidget, palsec->section, gwid->name);
        }
    }
}


/* Adds a GbWidget to the hash of known GbWidgets. The class_id is copied. */
void
gb_widget_register_gbwidget (const gchar *class_id,
			     GbWidget    *gbwidget)
{
  gbwidget->class_id = g_strdup (class_id);
  g_hash_table_insert (gb_widget_table, gbwidget->class_id, gbwidget);
}


/* This returns the GbWidget struct corresponding to the given class name. */
GbWidget *
gb_widget_lookup_class (const gchar *class_id)
{
  GbWidget *gbwidget;

  gbwidget = (GbWidget *) g_hash_table_lookup (gb_widget_table, class_id);

  return gbwidget;
}


/* This returns the GbWidget struct corresponding to the given widget. */
GbWidget *
gb_widget_lookup (GtkWidget *widget)
{
  GladeWidgetData *wdata;

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

  if (!wdata) {
    const gchar *class_name;

    MSG ("Widget has no associated GladeWidgetData");

    /* Fall back to the original way we used to get the GbWidget*, since
       some widgets currently don't have a GladeWidgetData attached. */
    class_name = gtk_type_name (GTK_OBJECT_TYPE (widget));
    return (GbWidget *) g_hash_table_lookup (gb_widget_table, class_name);
  }

  return wdata->gbwidget;
}


gchar*
gb_widget_get_class_id (GtkWidget *widget)
{
  GbWidget *gbwidget;

  gbwidget = gb_widget_lookup (widget);

  if (gbwidget)
    return gbwidget->class_id;
  else
    return (char*) gtk_type_name (GTK_OBJECT_TYPE (widget));
}


void
gb_widget_init_struct (GbWidget * gbwidget)
{
  gbwidget->pixmap_struct = NULL;
  gbwidget->class_id = NULL;
  gbwidget->gdkpixmap = NULL;
  gbwidget->mask = NULL;
  gbwidget->tooltip = NULL;
  gbwidget->pixbuf = NULL;
  gbwidget->properties_page_number = GB_PROPERTIES_NOT_CREATED;
  gbwidget->child_properties_page_number = GB_PROPERTIES_NOT_CREATED;

  gbwidget->gb_widget_new = NULL;
  gbwidget->gb_widget_create_from_widget = NULL;

  gbwidget->gb_widget_create_properties = NULL;
  gbwidget->gb_widget_get_properties = NULL;
  gbwidget->gb_widget_set_properties = NULL;

  gbwidget->gb_widget_add_child = NULL;
  gbwidget->gb_widget_get_child = NULL;

  gbwidget->gb_widget_create_child_properties = NULL;
  gbwidget->gb_widget_get_child_properties = NULL;
  gbwidget->gb_widget_set_child_properties = NULL;

  gbwidget->gb_widget_write_add_child_source = NULL;

  gbwidget->gb_widget_create_popup_menu = NULL;

  gbwidget->gb_widget_write_source = NULL;

  gbwidget->gb_widget_destroy = NULL;
}



/*************************************************************************
 * Functions for creating & destroying GbWidgets
 *************************************************************************/

GtkWidget *
gb_widget_new (const gchar * class_id, GtkWidget * parent)
{
  return gb_widget_new_full (class_id, TRUE, parent, NULL, 0, 0, NULL,
			     GB_CREATING, NULL);
}


/* Creates a new widget.
 * class_id is the name of the widget class, e.g. 'GtkLabel'
 * create_default_name is TRUE if you want a default name to be created,
 *   e.g. 'label1'.
 * parent is the widget that the new widget will be added beneath, so that
 *   the callback function knows where to put the widget.
 * current_child is the widget that the new widget will replace, or NULL
 *   if the new widget is just being added. It is used when replacing
 *   placeholders.
 * x & y are the coordinates of the new widget if it is being added to a
 *   GtkFixed container.
 * callback is the function to call once the widget is created to actually
 *   add it to the parent. Some widgets require dialog boxes for creating
 *   them (e.g. the dialog box to set the number of rows/cols in a table).
 *   So we need to provide a function to be called after this is done
 *   (we could have possibly chosen to use modal dialogs instead.)
 * action is either GB_CREATING or GB_LOADING. When loading widgets we don't
 *   want dialog boxes to pop up when they are being created.
 * loading_data is set when action is GB_LOADING, and contains the data used
 *   while loading, so that the GbWidgets can get any properties they need to
 *   create the widget without popping up a dialog box.
 */
GtkWidget *
gb_widget_new_full (const gchar * class_id, gboolean create_default_name,
		    GtkWidget * parent, GtkWidget * current_child,
		    gint x, gint y, GbWidgetNewCallback callback,
		    GbWidgetAction action, GbWidgetSetArgData * loading_data)
{
  GbWidgetNewData *data;
  GtkWidget *new_widget;
  GbWidget *gbwidget;
  GType type;

  gbwidget = gb_widget_lookup_class (class_id);
  g_return_val_if_fail (gbwidget != NULL, NULL);

  /* Note that for custom widgets this won't be found, and so will be 0. */
  type = g_type_from_name (class_id);
#if 0
  g_print ("Class Name: %s Type: %i\n", class_id, type);
#endif

  data = g_new (GbWidgetNewData, 1);
  /* Don't set data->name to NULL, since many widgets use it to set the label
     of the new widget. */
  data->project = current_project;
  data->name = create_default_name ? glade_project_new_widget_name (data->project, class_id) : g_strdup ("");
  data->callback = callback;
  data->parent = parent;
  if (parent) {
    gtk_widget_ref (parent);
    if (!gb_widget_lookup (parent))
       MSG2 ("Registering unknown widget '%s' as parent of '%s'",
	     G_OBJECT_TYPE_NAME (parent), class_id);
  }
  data->current_child = current_child;
  if (current_child)
    gtk_widget_ref (current_child);
  data->x = x;
  data->y = y;
  data->widget_data = glade_widget_data_new (gbwidget);
  data->action = action;
  data->loading_data = loading_data;

  if (gbwidget->gb_widget_new)
    new_widget = (gbwidget->gb_widget_new) (data);
  else if (type != 0)
    new_widget = gtk_widget_new (type, NULL);
  else
    g_return_val_if_fail ((new_widget = NULL), NULL);

  /* If the widget has been created immediately, then we can finish it off now,
     and free the GbWidgetNewData struct, otherwise we leave that to the
     dialog. */
  if (new_widget)
    {
      gb_widget_initialize (new_widget, data);
      if (data->callback)
	(*data->callback) (new_widget, data);
      gb_widget_free_new_data (data);
    }

  return new_widget;
}

static void
gb_widget_real_initialize (GtkWidget * widget, GladeWidgetData * wdata)
{
  g_return_if_fail (wdata != NULL);
  g_return_if_fail (wdata->gbwidget != NULL);

  /* Make sure GtkMenu widgets have visible set to FALSE. */
  if (GTK_IS_MENU (widget) && wdata)
      wdata->flags &= ~GLADE_VISIBLE;

  gtk_object_set_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY, wdata);
  gtk_signal_connect (GTK_OBJECT (widget), "destroy",
		      GTK_SIGNAL_FUNC (on_widget_destroy), NULL);

  editor_add_mouse_signals (widget);
  editor_add_draw_signals (widget);
  editor_add_key_signals (widget);

  if (GTK_IS_WINDOW (widget))
    gtk_window_add_accel_group (GTK_WINDOW (widget),
				glade_get_global_accel_group ());

#ifdef USE_GNOME
  /* FIXME: GnomeLibs 1.0.1 workaround - floating GnomeDockItem's don't work
     properly if we show them before adding to the GnomeDock. */
  if (!GTK_IS_WINDOW (widget) && !GTK_IS_MENU (widget)
      && !BONOBO_IS_DOCK_ITEM (widget))
    gtk_widget_show (widget);
#else
  if (!GTK_IS_WINDOW (widget) && !GTK_IS_MENU (widget))
    gtk_widget_show (widget);
#endif
}

/* This turns a normal widget into a GbWidget, adding a GladeWidgetData struct
   and the necessary signals. The widget must have its parent set already.
   If name is non-NULL, the widget's name will be set to the name with a
   unique ID added on to it, e.g. "ok_button1".
   NOTE: when loading, you should not create the names of any widgets,
   since it may clash with a widget loaded later. Instead leave the name as
   NULL. glade_project_ensure_widgets_named () will be called after the
   project is loaded, and any widgets without names will have names
   generated for them. */
void
gb_widget_create_from		(GtkWidget		*widget,
				 const gchar		*name)
{
  gb_widget_create_from_full (widget, name, NULL);
}


void
gb_widget_create_from_full	(GtkWidget		*widget,
				 const gchar		*name,
				 GladeWidgetData	*wdata)
{
  GbWidget *gbwidget;
  const char *class_id;

  MSG1 ("In create_from, widget name: %s", gtk_widget_get_name (widget));
  if (name)
    {
      char *wname = glade_project_new_widget_name (current_project, name);
      gtk_widget_set_name (widget, wname);
      g_free (wname);
    }

  if (GLADE_IS_CUSTOM_WIDGET (widget))
    class_id = "Custom";
  else
    class_id = gtk_type_name (GTK_OBJECT_TYPE (widget));

  gbwidget = (GbWidget *) g_hash_table_lookup (gb_widget_table, class_id);
  g_return_if_fail (gbwidget != NULL);

  if (!wdata)
    wdata = glade_widget_data_new (gbwidget);
  gb_widget_real_initialize (widget, wdata);

  gbwidget = gb_widget_lookup (widget);
  g_return_if_fail (gbwidget != NULL);

  /* Call any function the GbWidget has for setting up the widget to be used
     within Glade. */
  if (gbwidget->gb_widget_create_from_widget)
    {
      GbWidgetCreateFromData data;

      data.project = current_project;
      (gbwidget->gb_widget_create_from_widget) (widget, &data);
    }
}


void
gb_widget_initialize (GtkWidget * widget, GbWidgetNewData * data)
{
  if (data->name && data->name[0] != '\0')
    gtk_widget_set_name (widget, data->name);
  gb_widget_real_initialize (widget, data->widget_data);

  /* Now we set the widget's real style */
  /* FIXME: check if style should be propagated down from an ancestor? */
#if 0
  if (widget->style != data->widget_data->gbstyle->style)
    {
      gtk_widget_set_style (widget, data->widget_data->gbstyle->style);
    }
#endif

  /* FIXME: GTK workarounds to make sure that some widgets have reasonable
     sizes initially. Quite a few widgets default to a width and height of 0,
     which means that if there is little space available they will disappear,
     so we may need to do more here. */
  if (GTK_IS_ARROW (widget))
    gtk_widget_set_usize (widget, 16, 16);

  /* Set this to NULL so we don't try to free it later. */
  data->widget_data = NULL;
}

/* This returns TRUE if it is OK to complete the new() procedure, i.e. that
   the widget to replace or the parent widget still exist. It is used after
   the OK button is pressed in the dialog boxes for creating new tables/boxes.
   FIXME: I'm not too sure what we should do here. */
gboolean
gb_widget_can_finish_new (GbWidgetNewData * data)
{
  if (data->current_child)
    {
      if (data->current_child->parent == NULL)
	return FALSE;
    }
  else if (data->parent)
    {
      if (data->parent->parent == NULL && !GTK_IS_WINDOW (data->parent))
	return FALSE;
    }
  return TRUE;
}


void
gb_widget_free_new_data (GbWidgetNewData * data)
{
  g_free (data->name);
  g_free (data->widget_data);
  if (data->parent)
    gtk_widget_unref (data->parent);
  if (data->current_child)
    gtk_widget_unref (data->current_child);
  g_free (data);
}


static void
on_widget_destroy (GtkWidget * widget, gpointer user_data)
{
  GbWidget *gbwidget;
  GladeWidgetData *widget_data;
  GbWidgetDestroyData data;

  MSG1 ("IN on_widget_destroy widget:%s", gtk_widget_get_name (widget));

  /* Make sure we don't try to show its properties after it is destroyed. */
  if (property_get_widget () == widget)
    property_set_widget (NULL);

  /* If the entire project is being destroyed, we don't need to update the
     selection or the widget tree. */
  if (!(GTK_OBJECT_FLAGS (current_project) & GTK_IN_DESTRUCTION))
    {
      editor_remove_widget_from_selection (widget);
      tree_remove_widget (widget);
    }

  editor_on_widget_destroyed (widget);

  gbwidget = gb_widget_lookup (widget);
  g_return_if_fail (gbwidget != NULL);

  /* Call the GbWidget destroy function, if it has one. */
  data.project = current_project;
  if (gbwidget->gb_widget_destroy)
    (gbwidget->gb_widget_destroy) (widget, &data);

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

  /* Release the ID. */
  if (widget->name)
    glade_project_release_widget_name (current_project, widget->name);

  glade_widget_data_free (widget_data);

  g_free (gb_widget_get_child_name (widget));

  MSG1 ("OUT on_widget_destroy widget:%s", gtk_widget_get_name (widget));
}


/*************************************************************************
 * Functions for getting/setting the child name of a widget.
 * The child name is used to identify special widgets which have to
 * be treated differently. e.g. Dialog buttons.
 *************************************************************************/

static const gchar *glade_child_name_key  = "glade-child-name";
static GQuark       glade_child_name_key_id = 0;


/* Returns the child name of the widget. */
gchar*
gb_widget_get_child_name (GtkWidget *widget)
{
  if (!glade_child_name_key_id)
    glade_child_name_key_id = g_quark_from_static_string (glade_child_name_key);
  return gtk_object_get_data_by_id (GTK_OBJECT (widget),
				    glade_child_name_key_id);
}


/* Sets the child name of the widget. The child_name string is duplicated. */
void
gb_widget_set_child_name (GtkWidget *widget, const gchar *child_name)
{
  if (!glade_child_name_key_id)
    glade_child_name_key_id = g_quark_from_static_string (glade_child_name_key);
  /* Free any existing child name. */
  g_free (gtk_object_get_data_by_id (GTK_OBJECT (widget),
				     glade_child_name_key_id));
  gtk_object_set_data_by_id (GTK_OBJECT (widget), glade_child_name_key_id,
			     g_strdup (child_name));
}


/*************************************************************************
 * Functions for creating the page of properties specific to this widget
 *************************************************************************/

/* Returns the page number of the new page in the notebook which contains the
   widget's specific properties, or GB_PROPERTIES_NOT_NEEDED if it has none. */
gint
gb_widget_create_properties (GtkWidget * widget)
{
  GtkWidget *page;
  gint page_number;
  GbWidgetCreateArgData data;
  GbWidget *gbwidget;
  gboolean add_border_width = FALSE;

  gbwidget = gb_widget_lookup (widget);
  g_return_val_if_fail (gbwidget != NULL, GB_PROPERTIES_NOT_NEEDED);

  /* We always create the border width property, but hide it when it isn't
     needed. Some specific widgets don't need it, e.g. GtkDialog action areas.
  */
  /*if (glade_util_uses_border_width (widget))*/
  add_border_width = TRUE;

  if (gbwidget->gb_widget_create_properties || add_border_width)
    {
      /* Create skeleton of properties page, so gbwidget just has to add
         properties */
      page = gtk_table_new (1, 3, FALSE);
      gtk_table_set_row_spacings (GTK_TABLE (page), 1);
      gtk_widget_show (page);
      page_number = property_add_gbwidget_page (page);
      property_set_table_position (page, 0);

      /* If widget is a container add a border width property */
      if (add_border_width)
	{
	  gchar *class_id, buf[128];

	  class_id = gb_widget_get_class_id (widget);
	  sprintf (buf, "%s::border_width", class_id);
	  property_add_int_range (buf, _("Border Width:"),
			     _("The width of the border around the container"),
				  0, 1000, 1, 10, 1);
	}

      data.project = current_project;
      if (gbwidget->gb_widget_create_properties)
	(gbwidget->gb_widget_create_properties) (widget, &data);
      return page_number;
    }
  else
    {
      return GB_PROPERTIES_NOT_NEEDED;
    }
}


/*************************************************************************
 * Functions for creating the page of place properties specific to this
 * widget.
 *************************************************************************/

/* Returns the page number of the new page in the notebook which contains the
   widget's properties which applt to any children of the widget,
   or GB_PROPERTIES_NOT_NEEDED if no extra properties are needed for its
   children. */
gint
gb_widget_create_child_properties (GtkWidget * widget)
{
  static GHashTable *page_hash_table = NULL;

  GtkWidget *page;
  gint page_number;
  GbWidgetCreateChildArgData data;
  GbWidget *gbwidget;

  /* Create a hash table to contain functions already called to create child
     packing properties together with the page numbers they returned.
     This lets us use the same functions for multiple widgets, e.g. GtkHBox
     and GtkVBox, GtkHPaned and GtkVPaned. */
  if (page_hash_table == NULL)
    page_hash_table = g_hash_table_new (NULL, NULL);

  gbwidget = gb_widget_lookup (widget);
  g_return_val_if_fail (gbwidget != NULL, GB_PROPERTIES_NOT_NEEDED);

  if (gbwidget->gb_widget_create_child_properties)
    {
      /* First see if the function has already been called. Note that the
	 page numbers in the hash have 1 added to them so we can detect empty
	 values (they won't clash with page 0). */
      /* FIXME: ANSI forbids casting function pointer to data pointer. */
      page_number = GPOINTER_TO_INT (g_hash_table_lookup (page_hash_table, (gconstpointer) gbwidget->gb_widget_create_child_properties));
      if (page_number)
	return page_number - 1;

      /* Create skeleton of properties page, so gbwidget just has to add
         properties */
      page = gtk_table_new (10, 3, FALSE);
      gtk_widget_show (page);
      page_number = property_add_child_packing_page (page);
      /* FIXME: ANSI forbids casting function pointer to data pointer. */
      g_hash_table_insert (page_hash_table,
			   (gpointer) gbwidget->gb_widget_create_child_properties,
			   GINT_TO_POINTER (page_number + 1));
      property_set_table_position (page, 0);

      data.project = current_project;
      (gbwidget->gb_widget_create_child_properties) (widget, &data);
      return page_number;
    }
  else
    {
      return GB_PROPERTIES_NOT_NEEDED;
    }
}


/*************************************************************************
 * Functions for showing the widget's properties
 *************************************************************************/

void
gb_widget_show_properties (GtkWidget * widget)
{
  GbWidgetGetArgData data;
  GbWidget *gbwidget, *parent_gbwidget = NULL;
  GladeWidgetData *widget_data;
  gint page, child_packing_page;

  /* If properties of widget are already shown, just return */
  if (property_get_widget () == widget)
    return;

  /* If widget is a placeholder reset the properties window and return */
  if (GB_IS_PLACEHOLDER (widget))
    {
      property_set_widget (NULL);
      return;
    }

  gbwidget = gb_widget_lookup (widget);
  g_return_if_fail (gbwidget != NULL);

  /* Turn off auto-apply so we can set properties without the 'changed'
     callbacks calling gb_widget_apply_properties (). */
  property_set_auto_apply (FALSE);

  /* Need this here to make sure properties notebook is sensitive */
  property_set_widget (widget);

  page = gbwidget->properties_page_number;
  /* If widget's properties page hasn't been created, create it now */
  if (page == GB_PROPERTIES_NOT_CREATED)
    {
      page = gb_widget_create_properties (widget);
      gbwidget->properties_page_number = page;
    }

  /* Show the widget's own properties page if it has one.
     Need to show the page before setting properties because of the
     Text widget - it must be realized before setting the text :-( */
  if (page == GB_PROPERTIES_NOT_NEEDED)
    property_hide_gbwidget_page ();
  else
    property_show_gbwidget_page (page);

  /* Now see if the parent has child packing properties that need to be
     created or shown. */
  if (widget->parent)
    {
      parent_gbwidget = gb_widget_lookup (widget->parent);

      /* parent_gbwidget may be NULL, e.g. for GnomeDockItems. */
      if (parent_gbwidget)
	{
	  child_packing_page = parent_gbwidget->child_properties_page_number;
	  /* If widget's properties page hasn't been created, create it now */
	  if (child_packing_page == GB_PROPERTIES_NOT_CREATED)
	    {
	      child_packing_page = gb_widget_create_child_properties (widget->parent);
	      parent_gbwidget->child_properties_page_number = child_packing_page;
	    }
      
	  if (child_packing_page == GB_PROPERTIES_NOT_NEEDED)
	    property_hide_child_packing_page ();
	  else
	    property_show_child_packing_page (child_packing_page);
	}
    }
  else
    {
      property_hide_child_packing_page ();
    }

  widget_data = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  g_return_if_fail (widget_data != NULL);
  data.project = current_project;
  data.action = GB_SHOWING;
  data.widget_data = widget_data;
  data.widget = widget;

  get_standard_properties (widget, &data);

  if (gbwidget->gb_widget_get_properties)
    (gbwidget->gb_widget_get_properties) (widget, &data);

  if (parent_gbwidget && parent_gbwidget->gb_widget_get_child_properties)
    (parent_gbwidget->gb_widget_get_child_properties) (widget->parent, widget,
						       &data);

  /* Turn auto-apply back on again */
  property_set_auto_apply (TRUE);
}


/* This is called when the widget's size or position has changed, so that we
   should update the values shown in the properties editor. */
void
gb_widget_show_position_properties (GtkWidget * widget)
{
  GbWidgetGetArgData data;
  GladeWidgetData *widget_data;

  /* Make sure this is the widget shown in the properties editor. */
  if (property_get_widget () != widget)
    return;

  widget_data = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  g_return_if_fail (widget_data != NULL);
  data.project = current_project;
  data.action = GB_SHOWING;
  data.widget_data = widget_data;

  property_set_auto_apply (FALSE);
  get_position_properties (widget, &data);
  property_set_auto_apply (TRUE);
}



#ifdef GLADE_STYLE_SUPPORT
void
gb_widget_show_style (GtkWidget * widget)
{
  GladeWidgetData *wdata;
  GbStyle *gbstyle;
  GtkStyle *style = widget->style;
  gchar buffer[128];
  gint i;

  wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  g_return_if_fail (wdata != NULL);
  gbstyle = wdata->gbstyle;

  property_set_bool (GbStylePropagate, wdata->flags & GLADE_STYLE_PROPAGATE);

  property_set_dialog (GbStyleName, wdata->flags & GLADE_STYLE_IS_UNNAMED ?
		       "" : gbstyle->name, NULL);
  property_set_font (GbStyleFont, style->font, gbstyle->xlfd_fontname);

  /* Colors */
  show_color_properties (style->fg, "fg");
  show_color_properties (style->bg, "bg");
  show_color_properties (style->text, "text");
  show_color_properties (style->base, "base");

  /* Background pixmaps */
  for (i = 0; i < GB_NUM_STYLE_STATES; i++)
    {
      sprintf (buffer, "GtkStyle::%s[%s]", GbBgPixmapName, GbStateNames[i]);
      property_set_bgpixmap (buffer, style->bg_pixmap[i],
			     gbstyle->bg_pixmap_filenames[i]);
    }
}


static void
show_color_properties (GdkColor colors[], gchar * name)
{
  gint state;
  gchar buf[128];

  for (state = 0; state < GB_NUM_STYLE_STATES; state++)
    {
      sprintf (buf, "GtkStyle::%s[%s]", name, GbStateNames[state]);
      property_set_color (buf, &colors[state]);
    }
}
#endif


static void
show_accelerators (GtkWidget * widget, GbWidgetGetArgData * data)
{
  GList *element = data->widget_data->accelerators;
  property_clear_accelerators ();
  while (element)
    {
      property_add_accelerator ((GladeAccelerator *) element->data);
      element = element->next;
    }
}


static void
show_signals (GtkWidget * widget, GbWidgetGetArgData * data)
{
  GList *element = data->widget_data->signals;
  property_clear_signals ();
  MSG1 ("Num signals: %i", g_list_length (element));
  while (element)
    {
      property_add_signal ((GladeSignal *) element->data);
      element = element->next;
    }
}



/*************************************************************************
 * Functions for applying properties to a widget
 *************************************************************************/

void
gb_widget_apply_properties (GtkWidget * widget, GtkWidget * property)
{
  GbWidgetSetArgData data;
  GbWidget *gbwidget;
  GladeWidgetData *widget_data;

  MSG1 ("Applying properties: %s", gtk_widget_get_name (widget));

  gbwidget = gb_widget_lookup (widget);
  g_return_if_fail (gbwidget != NULL);

  widget_data = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  g_return_if_fail (widget_data != NULL);

  data.project = current_project;
  data.action = GB_APPLYING;
  data.widget_data = widget_data;
  data.widget = widget;
  data.property_to_apply = property;

  set_standard_properties (widget, &data);

  MSG ("Calling widget's own apply_properties");
  if (gbwidget->gb_widget_set_properties)
    (gbwidget->gb_widget_set_properties) (widget, &data);
  MSG ("Called widget's own apply_properties");
}


/* Copies the signals from the GladeWidgetInfo into a list of GladeSignal
   structs. */
static GList*
copy_signals (GbWidgetSetArgData * data, GladeWidgetInfo *widget_info)
{
  GList *signals = NULL;
  GladeSignalInfo *signal_info;
  gint i;

  signal_info = widget_info->signals;
  for (i = 0; i < widget_info->n_signals; i++)
    {
      GladeSignal *signal = g_new0 (GladeSignal, 1);

      signal->name = g_strdup (signal_info[i].name);
      signal->handler = g_strdup (signal_info[i].handler);
      signal->object = g_strdup (signal_info[i].object);
      signal->after = signal_info[i].after ? TRUE : FALSE;
      signal->data = NULL; /* Not supported anymore. */
      signal->last_modification_time = load_parse_date (data, signal_info[i].last_modification_time);

      if (data->status == GLADE_STATUS_INVALID_VALUE)
	{
	  g_warning ("Invalid date value: %s", signal_info[i].last_modification_time);
	  data->status = GLADE_STATUS_OK;
	}

      signals = g_list_prepend (signals, signal);
    }

  /* Reverse the list so it stays in the original order. */
  return g_list_reverse (signals);
}


/* Copies the accelerators from the GladeWidgetInfo into a list of
   GladeAccelerator structs. */
static GList*
copy_accels (GladeWidgetInfo *widget_info)
{
  GList *accels = NULL;
  GladeAccelInfo *accel_info;
  gint i;

  accel_info = widget_info->accels;
  for (i = 0; i < widget_info->n_accels; i++)
    {
      GladeAccelerator *accel = g_new0 (GladeAccelerator, 1);

      accel->key = g_strdup (gdk_keyval_name (accel_info[i].key));
      accel->modifiers = accel_info[i].modifiers;
      accel->signal = g_strdup (accel_info[i].signal);

      accels = g_list_prepend (accels, accel);
    }

  /* Reverse the list so it stays in the original order. */
  return g_list_reverse (accels);
}


static void
set_standard_properties (GtkWidget * widget, GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata = data->widget_data;
  gchar *name, *tooltip, *events_str, *ext_events;
  gboolean visible, sensitive, can_default, has_default, can_focus, has_focus;
  gint events, i;

  /* Properties on widget page */
  /* When pasting, we may want to discard the names from the XML, and generate
     a new name instead. */
  if (data->action == GB_LOADING)
    {
      if  (data->xml_buffer && data->discard_names)
	{
	  gchar *class_id = gb_widget_get_class_id (widget);
	  name = glade_project_new_widget_name (data->project, class_id);
	  gtk_widget_set_name (widget, name);
	  g_free (name);
	}
      else
	{
	  gtk_widget_set_name (widget, data->widget_info->name);
	}

      if (glade_util_is_component (widget))
	glade_project_component_changed (data->project, widget);

      /* We have to check if the name has a trailing ID and if so we reserve
	 it so no other widget can use it. */
      glade_project_reserve_name (data->project, gtk_widget_get_name (widget));
    }
  else
    {
      name = gb_widget_input_string (data, GbName);
      if (data->apply)
	{
	  tree_rename_widget (widget, name);
	  gtk_widget_set_name (widget, name);
	  property_update_title ();

	  /* If widget is a toplevel window/dialog set the component's name in
	     the project window */
	  if (glade_util_is_component (widget))
	    glade_project_component_changed (data->project, widget);
	}
    }

  /* If widget is a container, show the border width */
  if (glade_util_uses_border_width (widget))
    {
      gchar buf[128];
      gint border_width;
      gchar *class_id;

      class_id = gb_widget_get_class_id (widget);
      sprintf (buf, "%s::border_width", class_id);
      border_width = gb_widget_input_int (data, buf);
      if (data->apply && GTK_CONTAINER (widget)->border_width != border_width)
	{
	  gtk_container_set_border_width (GTK_CONTAINER (widget),
					  border_width);
	  if (data->action == GB_APPLYING)
	    editor_refresh_widget (widget);
	}
    }

  /* Language-specific properties. */
  set_lang_specific_properties (widget, data);

  /* Special child properties page */
  set_special_child_properties (widget, data);

  /* Properties on standard page */
  set_position_properties (widget, data);

  /* Visible property. Note that we create widgets with visible set to TRUE
     by default, but the property is FALSE by default in the XML file. So
     when loading we make sure we set the flag to the appropriate value. */
  visible = gb_widget_input_bool (data, GbVisible);
  if (data->apply || data->action == GB_LOADING)
    {
      if (!data->apply)
	visible = FALSE;
      if (visible)
	wdata->flags |= GLADE_VISIBLE;
      else
	wdata->flags &= ~GLADE_VISIBLE;
    }

  sensitive = gb_widget_input_bool (data, GbSensitive);
  if (data->apply)
    {
      if (sensitive)
	wdata->flags |= GLADE_SENSITIVE;
      else
	wdata->flags &= ~GLADE_SENSITIVE;
    }

  tooltip = gb_widget_input_string (data, GbTooltip);
  if (data->apply)
    {
      g_free (wdata->tooltip);
      if (tooltip && tooltip[0] == '\0')
	tooltip = NULL;
      wdata->tooltip = g_strdup (tooltip);

      /* SPECIAL CODE: toolitems have a special function. */
      if (GTK_IS_TOOL_ITEM (widget))
	gtk_tool_item_set_tooltip (GTK_TOOL_ITEM (widget),
				   gb_widget_tooltips, tooltip, NULL);
      else
	gtk_tooltips_set_tip (gb_widget_tooltips, widget, tooltip, NULL);
    }

  can_default = gb_widget_input_bool (data, GbCanDefault);
  if (data->apply)
    {
      if (can_default)
	GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_DEFAULT);
      else
	GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_DEFAULT);
      if (data->action == GB_APPLYING)
	editor_refresh_widget (widget);
    }

  has_default = gb_widget_input_bool (data, GbHasDefault);
  if (data->apply)
    {
      if (has_default)
	wdata->flags |= GLADE_GRAB_DEFAULT;
      else
	wdata->flags &= ~GLADE_GRAB_DEFAULT;
    }

  /* Different widgets have different default values for CAN_FOCUS, so when
     we load an XML file we must make sure that we always set it or unset it.
     Also, since we don't save the can_focus flag if it is false, we must make
     sure that we apply it anyway when loading. */
  can_focus = gb_widget_input_bool (data, GbCanFocus);
  if (!data->apply)
    can_focus = FALSE;
  if (data->apply || data->action == GB_LOADING)
    {
      if (can_focus)
	GTK_WIDGET_SET_FLAGS (widget, GTK_CAN_FOCUS);
      else
	GTK_WIDGET_UNSET_FLAGS (widget, GTK_CAN_FOCUS);
    }
  has_focus = gb_widget_input_bool (data, GbHasFocus);
  if (data->apply)
    {
      if (has_focus)
	wdata->flags |= GLADE_GRAB_FOCUS;
      else
	wdata->flags &= ~GLADE_GRAB_FOCUS;
    }

  /* Events & ext events. */
  if (!GTK_WIDGET_NO_WINDOW (widget))
    {
      if (data->action == GB_APPLYING)
	{
	  events = property_events_string_to_value (gb_widget_input_string (data,
								 GbEvents));
	  if (data->apply)
	    wdata->events = events;
	}
      else
	{
	  events_str = gb_widget_input_string (data, GbEvents);
	  if (data->apply)
	    {
	      for (i = 0; i < GB_EVENT_MASKS_COUNT; i++)
		{
		  if (glade_util_strstr (events_str, GbEventMaskSymbols[i]))
		    wdata->events |= GbEventMaskValues[i];
		}
	    }
	}

      ext_events = gb_widget_input_choice (data, GbExtEvents);
      if (data->apply)
	{
	  for (i = 0; GbExtensionModeChoices[i]; i++)
	    {
	      if (!strcmp (ext_events, GbExtensionModeChoices[i])
		  || !strcmp (ext_events, GbExtensionModeSymbols[i]))
		{
		  gtk_widget_set_extension_events (widget, GbExtensionModeValues
						   [i]);
		  break;
		}
	    }
	}
    }

  if (data->action == GB_APPLYING)
    {
#if 0
      apply_style (widget, data);
#endif
      apply_accelerators (widget, data);
      apply_signals (widget, data);

      glade_atk_set_properties (widget, data);
    }
  else
    {
      data->widget_data->signals = copy_signals (data, data->widget_info);
      data->widget_data->accelerators = copy_accels (data->widget_info);

      /* ATK properties are loaded later, after all widgets are created. */
    }
}


static void
set_special_child_properties (GtkWidget * widget,
			      GbWidgetSetArgData * data)
{
  GtkWidget *parent = widget->parent;
  GbWidget *gbparent;

  if (!parent)
    return;

  /* When pasting a widget to replace an existing widget, the child properties
     will already have been set, so we just return. */
  if (data->action == GB_LOADING && data->xml_buffer && data->replacing_widget)
    return;

  gbparent = gb_widget_lookup (parent);
  if (!gbparent)
    {
      MSG1 ("Unknown parent type %s", G_OBJECT_TYPE_NAME (parent));
    }

  /* Tell the load functions to use the child properties array. */
  data->loading_type = GB_CHILD_PROPERTIES;

  if (gbparent && gbparent->gb_widget_set_child_properties)
    {
      (gbparent->gb_widget_set_child_properties) (parent, widget, data);
    }

  data->loading_type = GB_STANDARD_PROPERTIES;
}


static void
set_position_properties (GtkWidget * widget,
			 GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata = data->widget_data;
  gint w, h;
  gboolean applyWidth, applyHeight;
  gboolean set_usize = FALSE;

  w = gb_widget_input_int (data, GbWidth);
  applyWidth = data->apply;
  h = gb_widget_input_int (data, GbHeight);
  applyHeight = data->apply;

  /* When loading we need to remember which values have been set explicitly. */
  if (data->action == GB_LOADING)
    {
      if (applyWidth)
	wdata->flags |= GLADE_WIDTH_SET;
      if (applyHeight)
	wdata->flags |= GLADE_HEIGHT_SET;
    }

#if 0
  g_print ("In set_position_properties X:%i Y:%i W:%i H:%i\n", x, y, w, h);
#endif
  if (GTK_IS_WINDOW (widget))
    {
      if (applyWidth && wdata->width != w)
	{
	  wdata->width = w;
	  set_usize = TRUE;
	}
      if (applyHeight && wdata->height != h)
	{
	  wdata->height = h;
	  set_usize = TRUE;
	}

      if (set_usize)
	{
	  gint w = wdata->flags & GLADE_WIDTH_SET ? wdata->width : -1;
	  gint h = wdata->flags & GLADE_HEIGHT_SET ? wdata->height : -1;
	  gb_widget_set_usize (widget, w, h);
	}
    }
  else if (widget->parent && (GTK_IS_FIXED (widget->parent)
			      || GTK_IS_LAYOUT (widget->parent)))
    {
      /* When pasting a widget to replace an existing widget, the size &
	 position will be set in the replace_child function. */
      if (data->action == GB_LOADING && data->xml_buffer
	  && data->replacing_widget)
	{
	  return;
	}


      if (applyWidth && wdata->width != w)
	{
	  wdata->width = w;
	  set_usize = TRUE;
	}
      if (applyHeight && wdata->height != h)
	{
	  wdata->height = h;
	  set_usize = TRUE;
	}
      if (set_usize)
	gb_widget_set_usize (widget, wdata->width, wdata->height);
    }
  else
    {
      if (applyWidth && wdata->width != w)
	{
	  wdata->width = w;
	  set_usize = TRUE;
	}
      if (applyHeight && wdata->height != h)
	{
	  wdata->height = h;
	  set_usize = TRUE;
	}

      if (set_usize)
	{
	  gint w = wdata->flags & GLADE_WIDTH_SET ? wdata->width : -1;
	  gint h = wdata->flags & GLADE_HEIGHT_SET ? wdata->height : -1;
	  gb_widget_set_usize (widget, w, h);
	}
      MSG2 ("*** Width set:%i Height set:%i", wdata->flags & GLADE_WIDTH_SET,
	    wdata->flags & GLADE_HEIGHT_SET);
    }
}


#if 0
static void
apply_style (GtkWidget * widget,
	     GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata = data->widget_data;
  GtkStyle *style = widget->style, *old_style = NULL;
  GbStyle *gbstyle = wdata->gbstyle, *new_gbstyle;
  GdkFont *font = NULL;
  gchar *style_name, *xlfd_fontname;
  GdkColor fg[GB_NUM_STYLE_STATES];
  GdkColor bg[GB_NUM_STYLE_STATES];
  GdkColor text[GB_NUM_STYLE_STATES];
  GdkColor base[GB_NUM_STYLE_STATES];
  GdkPixmap *bg_pixmap[GB_NUM_STYLE_STATES];
  gchar *bg_pixmap_filenames[GB_NUM_STYLE_STATES];
  gint recreate = FALSE, redraw = FALSE, i;
  gchar buf[128], *filename;
  gboolean named_style;

  style_name = gb_widget_input_dialog (data, GbStyleName);
  named_style = (style_name[0] == '\0') ? FALSE : TRUE;
  if (data->apply)
    {
      if (named_style)
	{
	  new_gbstyle = (GbStyle *) g_hash_table_lookup (gb_style_hash, style_name);
	  g_return_if_fail (new_gbstyle != NULL);
	  if (new_gbstyle != gbstyle)
	    {
	      gbstyle = new_gbstyle;
	      gb_widget_set_gb_style (widget, gbstyle);
	      wdata->flags &= ~GLADE_STYLE_IS_UNNAMED;
	      redraw = TRUE;
	    }
	}
      else
	{
	  wdata->flags |= GLADE_STYLE_IS_UNNAMED;
	}
    }

  font = gb_widget_input_font (data, GbStyleFont, &xlfd_fontname);
  if (data->apply)
    {
      if (font != style->font)
	recreate = TRUE;
    }

  recreate |= apply_colors (widget, data, style->fg, fg, "fg");
  recreate |= apply_colors (widget, data, style->bg, bg, "bg");
  recreate |= apply_colors (widget, data, style->text, text, "text");
  recreate |= apply_colors (widget, data, style->base, base, "base");

  /* Background pixmaps */
  for (i = 0; i < GB_NUM_STYLE_STATES; i++)
    {
      sprintf (buf, "GtkStyle::%s[%s]", GbBgPixmapName, GbStateNames[i]);
      bg_pixmap[i] = gb_widget_input_bgpixmap (data, buf, &filename);
      bg_pixmap_filenames[i] = filename;
      if (data->apply)
	{
	  if (bg_pixmap[i] != style->bg_pixmap[i])
	    recreate = TRUE;
	}
    }

  if (recreate)
    {
      old_style = style;

      /* If the widget is supposedly using an unnamed GbStyle, but currently is
         actually using a named GbStyle (for convenience), then we need to
         create a copy of the GbStyle and place our new style in it. */
      if ((wdata->flags & GLADE_STYLE_IS_UNNAMED) && gbstyle->name)
	{
	  gbstyle = gb_widget_copy_gb_style (gbstyle);
	  g_free (gbstyle->name);
	  gbstyle->name = NULL;
	}

      style = gtk_style_new ();
      for (i = 0; i < GB_NUM_STYLE_STATES; i++)
	{
	  style->fg[i] = fg[i];
	  style->bg[i] = bg[i];
	  style->text[i] = text[i];
	  style->base[i] = base[i];
	  style->bg_pixmap[i] = bg_pixmap[i];
	  if (bg_pixmap[i])
	    gdk_pixmap_ref (bg_pixmap[i]);

	  if (gbstyle->bg_pixmap_filenames[i] != bg_pixmap_filenames[i])
	    {
	      g_free (gbstyle->bg_pixmap_filenames[i]);
	      gbstyle->bg_pixmap_filenames[i] = g_strdup (bg_pixmap_filenames
							  [i]);
	    }
	}
      if (font)
	{
	  gdk_font_unref (style->font);
	  style->font = font;
	  gdk_font_ref (style->font);
	}
      if (strcmp (gbstyle->xlfd_fontname, xlfd_fontname))
	{
	  g_free (gbstyle->xlfd_fontname);
	  gbstyle->xlfd_fontname = g_strdup (xlfd_fontname);
	}

      gbstyle->style = style;
      gtk_style_ref (style);
      gb_widget_set_gb_style (widget, gbstyle);
    }


  /* If a named style has been changed/recreated we have to update all
     widget's that use it. */
  if (recreate || redraw)
    {
      if (named_style)
	{
	  gb_widget_update_gb_styles (gbstyle, gbstyle);
	}
      else
	{
	  editor_refresh_widget (widget);
	}
    }

  if (old_style)
    gtk_style_unref (old_style);
}


/* This makes sure a widget's gbstyle & its style are up to date, and
   if the propagate flag is set it also updates any children.
   But it won't change a descendant's style if it has been set explicitly.
   FIXME: only propagates one level at present, and always sets child's style,
   even if it has a different GbStyle! */
void
gb_widget_set_gb_style (GtkWidget * widget,
			GbStyle * gbstyle)
{
  GladeWidgetData *wdata;

  if (!GB_IS_PLACEHOLDER (widget))
    {
      if (widget->style != gbstyle->style)
	gtk_widget_set_style (widget, gbstyle->style);
    }

  wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
  if (wdata)
    {
      if (wdata->gbstyle != gbstyle)
	{
	  gb_widget_unref_gb_style (wdata->gbstyle);
	  wdata->gbstyle = gbstyle;
	  gb_widget_ref_gb_style (gbstyle);
	}
      /* If propagate style flags is set, propagate style to children */
      if (wdata->flags & GLADE_STYLE_PROPAGATE)
	gb_widget_children_foreach (widget, (GtkCallback) gb_widget_set_gb_style,
			   gbstyle);
    }
}


static gboolean
apply_colors (GtkWidget * widget, GbWidgetSetArgData * data, GdkColor colors[],
	      GdkColor new_colors[], gchar * name)
{
  gint state;
  gchar buf[128];
  GdkColor *color;
  gboolean need_redraw = FALSE;

  for (state = 0; state < GB_NUM_STYLE_STATES; state++)
    {
      sprintf (buf, "GtkStyle::%s[%s]", name, GbStateNames[state]);

      color = gb_widget_input_color (data, buf);
      if (data->apply)
	{
	  new_colors[state] = *color;
	  if (!gdk_color_equal (&new_colors[state], &colors[state]))
	    need_redraw = TRUE;
	}
      else
      {
	/* Copy the old values across. */
	new_colors[state] = colors[state];
      }
    }
  return need_redraw;
}
#endif


/* Currently this frees all the GladeAccelerators and creates them from scratch */
static void
apply_accelerators (GtkWidget * widget, GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata;

  if (data->property_to_apply == NULL
      || property_is_accel_clist (data->property_to_apply))
    {
      wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
      g_return_if_fail (wdata != NULL);

      glade_widget_data_set_accels (wdata, property_get_accelerators ());
    }
}


/* Currently this frees all the GladeSignals and creates them from scratch. */
static void
apply_signals (GtkWidget * widget, GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata;

  if (data->property_to_apply == NULL
      || property_is_signal_clist (data->property_to_apply))
    {
      wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
      g_return_if_fail (wdata != NULL);

      glade_widget_data_set_signals (wdata, property_get_signals ());
    }
}



/*************************************************************************
 * Functions for showing the popup context-sensitive menu of a widget
 *************************************************************************/

static void
popup_done (GtkWidget *widget, gpointer data)
{
  gtk_widget_destroy (widget);
}

void
gb_widget_show_popup_menu (GtkWidget * widget,
			   GdkEventButton * event)
{
  GtkWidget *menu = NULL;
  GbWidget *gbwidget;
  const gchar *name;
  GtkWidget *menuitem, *ancestor, *submenu, *child;
  GbWidgetCreateMenuData data;

  gbwidget = gb_widget_lookup (widget);
  g_return_if_fail (gbwidget != NULL);

  name = gtk_widget_get_name (widget);
  if (GB_IS_PLACEHOLDER (widget))
    name = "Placeholder";

  menu = gtk_menu_new ();
  menuitem = gtk_menu_item_new_with_label (name);
  gtk_widget_show (menuitem);
  gtk_widget_set_sensitive (menuitem, FALSE);
  gtk_container_add (GTK_CONTAINER (menu), menuitem);

  data.project = current_project;
  data.menu = menu;
  data.child = NULL;
  add_standard_top_menu_items (widget, &data);

  if (gbwidget->gb_widget_create_popup_menu)
    (gbwidget->gb_widget_create_popup_menu) (widget, &data);

  add_standard_bottom_menu_items (widget, &data);

  child = widget;
  ancestor = widget->parent;
  while (ancestor)
    {
      name = gtk_widget_get_name (ancestor);
      if (GB_IS_PLACEHOLDER (ancestor))
	name = "Placeholder";

      /* Skip widgets which aren't GbWidgets */
      if (GB_IS_GB_WIDGET (ancestor))
	{
	  /* Add a separator */
	  menuitem = gtk_menu_item_new ();
	  gtk_container_add (GTK_CONTAINER (menu), menuitem);
	  gtk_widget_show (menuitem);

	  menuitem = gtk_menu_item_new_with_label (name);
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (menu), menuitem);

	  /* Create submenu */
	  submenu = gtk_menu_new ();
	  gtk_menu_item_set_submenu (GTK_MENU_ITEM (menuitem), submenu);

	  data.menu = submenu;
	  data.child = child;
	  add_standard_top_menu_items (ancestor, &data);

	  /* Call ancestors function to add any menu items */
	  gbwidget = gb_widget_lookup (ancestor);
	  if (gbwidget != NULL && gbwidget->gb_widget_create_popup_menu)
	    (gbwidget->gb_widget_create_popup_menu) (ancestor, &data);

	  add_standard_bottom_menu_items (ancestor, &data);
	}
      child = ancestor;
      ancestor = ancestor->parent;
    }

  /* Automatically destroy the menu when it is hidden. */
  gtk_signal_connect_after (GTK_OBJECT (menu), "selection-done",
			    GTK_SIGNAL_FUNC (popup_done), menu);

  MSG ("showing popup menu");
  gtk_menu_popup (GTK_MENU (menu), NULL, NULL, NULL, NULL,
		  event->button, event->time);
}


static void
add_standard_top_menu_items (GtkWidget * widget, GbWidgetCreateMenuData * data)
{
  GtkWidget *menuitem;

  menuitem = gtk_menu_item_new_with_label (_("Select"));
  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
		      GTK_SIGNAL_FUNC (editor_on_select_activate), widget);
  gtk_widget_show (menuitem);
  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
}


static void
add_standard_bottom_menu_items (GtkWidget * widget, GbWidgetCreateMenuData * data)
{
  GtkWidget *menuitem;
  gboolean can_delete;

  can_delete = (editor_can_delete_widget (widget) == NULL) ? TRUE : FALSE;

  /* For widgets which can handle scrolling, we add commands to add or remove
     a parent scrolled window. */
  if (GTK_WIDGET_CLASS (G_OBJECT_GET_CLASS (widget))->set_scroll_adjustments_signal)
    {
      if (widget->parent && GTK_IS_SCROLLED_WINDOW (widget->parent))
	{
	  menuitem = gtk_menu_item_new_with_label (_("Remove Scrolled Window"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			      GTK_SIGNAL_FUNC (gb_widget_remove_scrolled_window),
			      widget);
	}
      else
	{
	  menuitem = gtk_menu_item_new_with_label (_("Add Scrolled Window"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			      GTK_SIGNAL_FUNC (gb_widget_add_scrolled_window),
			      widget);
	}
    }

  /* We assume that we can only insert/remove alignments and event boxes if
     the widget can be deleted. */
  if (can_delete
      && !GTK_IS_WINDOW (widget)
      && !GTK_IS_MISC (widget)
      && !GTK_IS_ALIGNMENT (widget)
      && !GTK_IS_MENU (widget)
      && !GTK_IS_MENU_ITEM (widget)
      && !GB_IS_PLACEHOLDER (widget))
    {
      if (GTK_IS_ALIGNMENT (widget->parent))
	{
	  menuitem = gtk_menu_item_new_with_label (_("Remove Alignment"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
		      GTK_SIGNAL_FUNC (gb_widget_remove_alignment), widget);
	}
      else
	{
	  menuitem = gtk_menu_item_new_with_label (_("Add Alignment"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			 GTK_SIGNAL_FUNC (gb_widget_add_alignment), widget);
	}
    }

  if (can_delete
      && GTK_WIDGET_NO_WINDOW (widget)
      && !GTK_IS_EVENT_BOX (widget)
      && !GB_IS_PLACEHOLDER (widget))
    {
      if (GTK_IS_EVENT_BOX (widget->parent))
	{
	  menuitem = gtk_menu_item_new_with_label (_("Remove Event Box"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
		      GTK_SIGNAL_FUNC (gb_widget_remove_event_box), widget);
	}
      else
	{
	  menuitem = gtk_menu_item_new_with_label (_("Add Event Box"));
	  gtk_widget_show (menuitem);
	  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
	  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			 GTK_SIGNAL_FUNC (gb_widget_add_event_box), widget);
	}
    }

  if (GTK_IS_WINDOW (widget))
    {
      menuitem = gtk_menu_item_new_with_label (_("Redisplay"));
      gtk_widget_show (menuitem);
      gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
      gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			  GTK_SIGNAL_FUNC (gb_widget_redisplay_window),
			  widget);
    }

  menuitem = gtk_separator_menu_item_new ();
  gtk_widget_show (menuitem);
  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);

  /* Only show the 'Cut' item if the widget can be deleted. */
  if (can_delete)
    {
      menuitem = gtk_menu_item_new_with_label (_("Cut"));
      gtk_widget_show (menuitem);
      gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
      gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			  GTK_SIGNAL_FUNC (editor_on_cut_activate), widget);
    }
 
  menuitem = gtk_menu_item_new_with_label (_("Copy"));
  gtk_widget_show (menuitem);
  gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
  gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
		      GTK_SIGNAL_FUNC (editor_on_copy_activate), widget);
 
  /* Only show the 'Paste' item if the widget can be deleted. */
  if (can_delete)
    {
      menuitem = gtk_menu_item_new_with_label (_("Paste"));
      gtk_widget_show (menuitem);
      if (!widget->parent)
	gtk_widget_set_sensitive (menuitem, FALSE);
      gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
      gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			  GTK_SIGNAL_FUNC (editor_on_paste_activate), widget);
    }
 
  /* Only show the 'Delete' item if the widget can be deleted. */
  if (can_delete)
    {
      menuitem = gtk_menu_item_new_with_label (_("Delete"));
      gtk_widget_show (menuitem);
      gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
      gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			  GTK_SIGNAL_FUNC (editor_on_delete_activate), widget);
    }
}



/*************************************************************************
 * Functions for loading project files
 *************************************************************************/

/* This reads in a widget, and calls itself recursively to read it's children.
   It assumes a '<widget>' has just been read, and reads up to and including
   '</widget>'. The newly-created widget is returned, mainly so that
   glade_clipboard_paste () can show any windows which are pasted. */
GtkWidget*
gb_widget_load (GtkWidget * widget, GbWidgetSetArgData * data, GtkWidget * parent)
{
  gchar *class_id;
  const gchar *child_name;
  GbWidget *gbwidget, *ancestor_gbwidget;
  GtkWidget *ancestor;
  GladeWidgetData *wdata;
  GladeChildInfo *child_info;
  GladeWidgetInfo *widget_info;
  gint i;
  gboolean skip_child;

  data->action = GB_LOADING;
  data->loading_type = GB_STANDARD_PROPERTIES;

  child_info = data->child_info;
  widget_info = data->widget_info;

  class_id = widget_info ? widget_info->class : NULL;
  MSG1 ("Reading Class: %s", class_id ? class_id : "(placeholder)");

  child_name = child_info ? child_info->internal_child : NULL;

  /* Migrate toolbar buttons from old XML files. See the set_properties
     functions in gbtoolbutton.c etc. for the code that reads in the old
     properties. */
  if (class_id)
    {
      if (!strcmp (class_id, "button"))
	{
	  class_id = "GtkToolButton";
	}
      else if (!strcmp (class_id, "toggle"))
	{
	  class_id = "GtkToggleToolButton";
	}
      else if (!strcmp (class_id, "radio"))
	{
	  class_id = "GtkRadioToolButton";
	}
    }

  /* SPECIAL CODE: CList/CTree title buttons don't save the child name now, so
     we have to add it. */
  if (parent && GTK_IS_CLIST (parent))
    child_name = GladeChildCListTitle;

  /* SPECIAL CODE: Ignore the old 'BonoboDock:contents' child name. */
  if (child_name && !strcmp (child_name, "BonoboDock:contents"))
    child_name = NULL;

  /* SPECIAL CODE: when pasting a widget to replace an existing widget, check
     if the child name should be transferred to the new widget. */
  if (data->xml_buffer && data->replacing_widget && child_name)
    {
      /* These child names should be removed from the widget when it is pasted,
	 since they identify the old position the widget. */
      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)
	  || !strcmp (child_name, GladeChildGnomeAppDock)
	  || !strcmp (child_name, GladeChildGnomeAppBar)
	  || !strcmp (child_name, GladeChildGnomeEntry)
	  )
	{
	  child_name = NULL;
	}
    }

  if (!class_id)
    {
      MSG ("found placeholder");
      widget = editor_new_placeholder ();
      if (!gb_widget_add_child (parent, data, widget))
	{
	  data->status = GLADE_STATUS_ERROR;
	  return NULL;
	}
      if (child_name)
	gb_widget_set_child_name (widget, child_name);
      return NULL;
    }

  gbwidget = gb_widget_lookup_class (class_id);
  if (gbwidget == NULL)
    {
      MSG ("Load error");
      data->status = GLADE_STATUS_CLASS_UNKNOWN;
      g_warning ("Unknown widget class: %s", class_id);
      return NULL;
    }

  /* If this is a special child of a widget, step through the ancestors of
     the widget and try to find it. Note that some special child widgets
     have to be created normally, and then added by the parent container,
     e.g. notebook tabs and clist titles - see gb_widget_add_child(). */
  if (child_name)
    {
      MSG1 ("Child name: %s", child_name);
      ancestor = parent;
      while (ancestor)
	{
	  ancestor_gbwidget = gb_widget_lookup (ancestor);
	  if (ancestor_gbwidget && ancestor_gbwidget->gb_widget_get_child)
	    {
	      widget = (ancestor_gbwidget->gb_widget_get_child) (ancestor,
								 child_name);
	      if (widget)
		break;
	    }

	  ancestor = ancestor->parent;
	}

#if 0
      if (!widget)
	g_print ("Child widget %s not found - may be a problem\n", child_name);
#endif
    }

  /* If this is a standard widget, we need to create it and add it to its
     parent. If the widget has already been created by its parent, we can
     just set the properties. */
  if (widget == NULL)
    {
      MSG ("widget == NULL, has not been created by parent.");
      widget = gb_widget_new_full (class_id, FALSE, parent, NULL,
				   0, 0, NULL, GB_LOADING, data);
      if (!widget)
	{
	  data->status = GLADE_STATUS_ERROR;
	  return NULL;
	}

      wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
      data->widget_data = wdata;
      data->widget = widget;

      /* We get the gbwidget from the widget data, as it may be different from
	 the original one. Currently only Bonobo controls do this. */
      gbwidget = wdata->gbwidget;
      g_assert (gbwidget);

      if (parent == NULL)
	{
	  glade_project_add_component (data->project, widget);
	}
      else
	{
	  if (!gb_widget_add_child (parent, data, widget))
	    {
	      data->status = GLADE_STATUS_ERROR;
	      return NULL;
	    }
	}

      /* This must come after add_child so we can set special child properties,
         and also we may need to realize the widget. It must also come after
	 glade_project_add_component(), in case setting any properties results
	 in a signal being emitted from the project. */
      set_standard_properties (widget, data);

      tree_add_widget (widget);
    }
  else
    {
      MSG ("widget != NULL, has been created by parent.");
      wdata = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);
      if (wdata == NULL)
	g_warning ("Null widget_data\n");
      data->widget_data = wdata;
      data->widget = widget;
      set_standard_properties (widget, data);
      tree_add_widget (widget);
    }

  if (child_name)
    gb_widget_set_child_name (widget, child_name);

  if (data->status != GLADE_STATUS_OK)
    {
      MSG ("Load error");
      return NULL;
    }

  MSG ("Calling widgets set_properties()");
  if (gbwidget->gb_widget_set_properties)
    (gbwidget->gb_widget_set_properties) (widget, data);
  MSG ("Called widgets set_properties()");

  if (data->status != GLADE_STATUS_OK)
    {
      MSG ("Load error");
      return NULL;
    }

  /* Save a pointer to the widget in the all_widgets hash, when loading and
     when pasting. Note that when pasting we use the name before any renaming
     is done, so ATK relation targets within the widget hierarchy being
     pasted will still be correct. */
  if (data->all_widgets)
    {
      const gchar *widget_name = widget_info->name;
      if (widget_name && *widget_name)
	{
#if 0
	  g_print ("Saving pointer to widget: %s, %p\n", widget_name, widget);
#endif
	  g_hash_table_insert (data->all_widgets, (gpointer) widget_name,
			       widget);
	}
    }

  /* Save a pointer to the GladeWidgetInfo in the widget, which we use to
     load the ATK properties after all widgets are created. */
  gtk_object_set_data (GTK_OBJECT (widget), GladeWidgetInfoKey, widget_info);

  /* When pasting, we only ever replace the top widget. All children are added
     as normal. */
  data->replacing_widget = NULL;

  MSG ("Loading children");

  /* load children. */
  for (i = 0; i < widget_info->n_children; i++)
    {
      data->child_info = &widget_info->children[i];
      data->widget_info = data->child_info->child;

      /* We have to reset the widget_data since this may have been changed
	 while loading the last child (and its children) */
      data->widget_data = wdata;
      if (widget->parent && !gb_widget_lookup (widget->parent))
	{
	  MSG1 ("Unusual widget here of type '%s'", 
		G_OBJECT_TYPE_NAME (widget->parent));
	}

      /* Skip the hscrollbar & vscrollbar internal children, from
	 GtkScrolledWindow. libglade-convert outputs these, but we don't
	 use them. */
      skip_child = FALSE;
      if (data->child_info && data->child_info->internal_child)
	{
	  if (!strcmp (data->child_info->internal_child, "vscrollbar")
	      || !strcmp (data->child_info->internal_child, "hscrollbar"))
	    skip_child = TRUE;
	}

      if (!skip_child)
	gb_widget_load (NULL, data, widget);

      /* Reset these, just in case. */
      data->child_info = child_info;
      data->widget_info = widget_info;

      if (data->status != GLADE_STATUS_OK)
	{
	  MSG ("Load error");
	  return NULL;
	}
    }

  /* If the widget is a table, we make sure all empty cells have placeholders
     in them. */
  if (GTK_IS_TABLE (widget))
    {
      gb_table_update_placeholders (widget, -1, -1);
    }

  return widget;
}


/* Adds a widget to a parent, when loading or pasting from the clipboard.
   Returns TRUE on success. */
gboolean
gb_widget_add_child (GtkWidget * parent,
		     GbWidgetSetArgData * data,
		     GtkWidget * child)
{
  GbWidget *gbwidget;

  MSG2 ("Adding %s to %s", gtk_widget_get_name (child),
	parent ? gtk_widget_get_name (parent) : "NULL");

  g_return_val_if_fail (parent != NULL, FALSE);

  /* If we are pasting, the top widget replaces the parent rather than being
     added to it. */
  if (data->xml_buffer && data->replacing_widget)
    {
      return gb_widget_replace_child (parent, data->replacing_widget, child);
    }

  gbwidget = gb_widget_lookup (parent);
  g_return_val_if_fail (gbwidget != NULL, FALSE);

  if (gbwidget->gb_widget_add_child)
    {
      gbwidget->gb_widget_add_child (parent, child, data);
    }
  else if (GTK_IS_BIN (parent))
    {
      if (GTK_BIN (parent)->child)
	gtk_container_remove (GTK_CONTAINER (parent), GTK_BIN (parent)->child);
      gtk_container_add (GTK_CONTAINER (parent), child);
    }
  else if (GTK_IS_CONTAINER (parent))
    {
      gtk_container_add (GTK_CONTAINER (parent), child);
    }

  return TRUE;
}


/*************************************************************************
 * Functions for saving project files
 *************************************************************************/

void
gb_widget_save (GtkWidget * widget,
		GbWidgetGetArgData * data)
{
  GbWidget *gbwidget, *parent_gbwidget;
  GladeWidgetData *widget_data;
  gchar *child_name, *class_id, *id, *class_to_save;
  gboolean is_toplevel;

  class_id = class_to_save = gb_widget_get_class_id (widget);
  child_name = gb_widget_get_child_name (widget);
  is_toplevel = glade_util_is_component (widget);

  /* Bonobo Controls are save with a class of 'BonoboWidget'. */
#ifdef USE_GNOME
  if (gtk_object_get_data (GTK_OBJECT (widget), Moniker))
    class_to_save = "BonoboWidget";
#endif

  /* SPECIAL CODE: CList/CTree title buttons don't save the child name. */
  if (child_name && !strcmp (child_name, GladeChildCListTitle))
    {
      child_name = NULL;
    }

  /* SPECIAL CODE: Don't save a placeholder if its parent is a table, since we
     don't really want placeholders in the XML when the interface is finished,
     but it is quite possible that some table cells will be left blank. */
  if (GB_IS_PLACEHOLDER (widget))
    {
      if (!GTK_IS_TABLE (widget->parent))
	{
	  save_newline (data);

	  if (!is_toplevel)
	    save_child_start_tag (data, child_name);

	  save_placeholder (data);

	  if (!is_toplevel)
	    save_end_tag (data, "child");
	}
      return;
    }

  /* Write out widget data and any child widgets */
  widget_data = gtk_object_get_data (GTK_OBJECT (widget), GB_WIDGET_DATA_KEY);

  /* If this isn't a gbwidget, skip it, but save any child gbwidgets. */
  if (!widget_data)
    {
      /* Recursively save children. */
      gb_widget_children_foreach (widget, (GtkCallback) gb_widget_save, data);
      return;
    }

  data->action = GB_SAVING;
  data->widget_data = widget_data;
  data->widget = widget;

  save_newline (data);
  if (!is_toplevel)
    save_child_start_tag (data, child_name);

  id = (char*) gtk_widget_get_name (widget);
  save_widget_start_tag (data, class_to_save, id);

  get_standard_properties (widget, data);

  /* Call gbwidgets save function for any extra properties */
  gbwidget = gb_widget_lookup_class (class_id);
  g_return_if_fail (gbwidget != NULL);

  if (gbwidget->gb_widget_get_properties)
    (gbwidget->gb_widget_get_properties) (widget, data);

  glade_atk_save_properties (widget, data);

  save_signals (widget, data);
  save_accelerators (widget, data);

  /* Recursively save children. */
  gb_widget_children_foreach (widget, (GtkCallback) gb_widget_save, data);

  save_end_tag (data, "widget");

  /* Call parent widget's function to save child packing properties. */
  if (widget->parent)
    {
      parent_gbwidget = gb_widget_lookup (widget->parent);
      /* parent_gbwidget may be NULL, e.g. for GnomeDockItems. */
      if (parent_gbwidget
	  && parent_gbwidget->gb_widget_get_child_properties)
	(parent_gbwidget->gb_widget_get_child_properties) (widget->parent,
							   widget, data);
#ifdef USE_GNOME
      else if (BONOBO_IS_DOCK_ITEM (widget))
	{
	  gb_bonobo_dock_item_save_packing_properties (widget->parent, widget,
						       data);
	}
#endif
    }

  if (!is_toplevel)
    save_end_tag (data, "child");
}


/* This outputs all the standard widget properties, for showing or saving.
   Note that when saving we try not to save default values. */
static void
get_standard_properties (GtkWidget * widget,
			 GbWidgetGetArgData * data)
{
  gboolean can_default, has_default, can_focus, has_focus, visible, sensitive;
  GladeWidgetData *wdata = data->widget_data;
  gchar *name, *class_id;
  GbWidgetAction action = data->action;
  gchar border_width_property_buf[128];
  gboolean border_width_visible = FALSE;

  /* The class and name (id) are saved in gb_widget_save, so don't save them
     here. */
  name = (char*) gtk_widget_get_name (widget);
  class_id = gb_widget_get_class_id (widget);
  if (action != GB_SAVING)
    {
      gb_widget_output_string (data, GbName, name);
      gb_widget_output_string (data, GbClass, class_id);
    }

  /* If widget is a container, save the border width */
  sprintf (border_width_property_buf, "%s::border_width", class_id);
  if (glade_util_uses_border_width (widget))
    {
      gint border_width;
      border_width_visible = TRUE;
      border_width = GTK_CONTAINER (widget)->border_width;
      if (action != GB_SAVING || border_width > 0)
	{
	  gb_widget_output_int (data, border_width_property_buf, border_width);
	}
    }
  if (action == GB_SHOWING)
    {
      property_set_visible (border_width_property_buf, border_width_visible);
    }

  get_position_properties (widget, data);
  get_lang_specific_properties (widget, data);

  visible = wdata->flags & GLADE_VISIBLE;
  if (action != GB_SAVING || visible)
    gb_widget_output_bool (data, GbVisible, visible);
  sensitive = wdata->flags & GLADE_SENSITIVE;
  if (action != GB_SAVING || !sensitive)
    gb_widget_output_bool (data, GbSensitive, sensitive);

  /* Only widgets with windows can have tooltips at present. Though buttons
     seem to be a special case, as they are NO_WINDOW widgets but have
     InputOnly windows, so tooltip still work. In GTK+ 2 menuitems are like
     buttons. */
  if (!GTK_WIDGET_NO_WINDOW (widget) || GTK_IS_BUTTON (widget)
      || GTK_IS_MENU_ITEM (widget) || GTK_IS_TOOL_BUTTON (widget)
      || GTK_IS_EVENT_BOX (widget))
    {
      if (action != GB_SAVING || wdata->tooltip)
	gb_widget_output_translatable_string (data, GbTooltip, wdata->tooltip);
      if (action == GB_SHOWING)
	property_set_sensitive (GbTooltip, TRUE);
    }
  else if (action == GB_SHOWING)
    {
      /* N/A stands for 'Not Applicable'. It is used when a standard widget
	 property does not apply to the current widget. e.g. widgets without
	 windows can't use the Events property. This appears in the property
	 editor and so should be a short abbreviation. */
      gb_widget_output_string (data, GbTooltip, _("N/A"));
      property_set_sensitive (GbTooltip, FALSE);
    }

  can_default = GTK_WIDGET_CAN_DEFAULT (widget);
  if (action != GB_SAVING || can_default)
    gb_widget_output_bool (data, GbCanDefault, can_default);
  has_default = wdata ? wdata->flags & GLADE_GRAB_DEFAULT : 0;
  if (action != GB_SAVING || has_default)
    gb_widget_output_bool (data, GbHasDefault, has_default);

  can_focus = GTK_WIDGET_CAN_FOCUS (widget);
  if (action != GB_SAVING || can_focus)
    gb_widget_output_bool (data, GbCanFocus, can_focus);
  has_focus = wdata ? wdata->flags & GLADE_GRAB_FOCUS : 0;
  if (action != GB_SAVING || has_focus)
    gb_widget_output_bool (data, GbHasFocus, has_focus);

  if (!GTK_WIDGET_NO_WINDOW (widget))
    {
      GdkExtensionMode ext_mode = gtk_widget_get_extension_events (widget);
      gint i;

      if (action == GB_SAVING)
	{
	  if (wdata && wdata->events)
	    {
	      gchar buffer[1024];
	      gboolean written_first = FALSE;

	      buffer[0] = '\0';
	      for (i = 0; i < GB_EVENT_MASKS_COUNT; i++)
		{
		  if (wdata->events & GbEventMaskValues[i])
		    {
		      if (written_first)
			strcat (buffer, " | ");
		      strcat (buffer, GbEventMaskSymbols[i]);
		      written_first = TRUE;
		    }
		}

	      gb_widget_output_string (data, GbEvents, buffer);
	    }
	}
      else
	{
	  gb_widget_output_string (data, GbEvents,
			   property_events_value_to_string (wdata->events));
	}

      /* Don't save default extension mode ('None') */
      if (action != GB_SAVING || ext_mode != GDK_EXTENSION_EVENTS_NONE)
	{
	  for (i = 0; GbExtensionModeChoices[i]; i++)
	    {
	      if (GbExtensionModeValues[i] == ext_mode)
		gb_widget_output_choice (data, GbExtEvents, i,
					 GbExtensionModeSymbols[i]);
	    }
	}
      if (action == GB_SHOWING)
	{
	  property_set_sensitive (GbEvents, TRUE);
	  property_set_sensitive (GbExtEvents, TRUE);
	}
    }
  else if (action == GB_SHOWING)
    {
      gb_widget_output_dialog (data, GbEvents, _("N/A"), NULL);
      property_set_sensitive (GbEvents, FALSE);
      gb_widget_output_choice (data, GbExtEvents, 0, GbExtensionModeSymbols[0]);
      property_set_sensitive (GbExtEvents, FALSE);
    }

  if (action == GB_SHOWING)
    {
#ifdef GLADE_STYLE_SUPPORT
      gb_widget_show_style (widget);
#endif
      show_accelerators (widget, data);
      show_signals (widget, data);

      glade_atk_get_properties (widget, data);
    }
  else
    {
#ifdef GLADE_STYLE_SUPPORT
      save_style (widget, data);
#endif

      /* These need to be saved in gb_widget_save(), since they must be after
	 all the widget properties. */
#if 0
      save_signals (widget, data);
      save_accelerators (widget, data);
#endif
    }
}


/* This is used when saving or displaying properties. */
static void
get_lang_specific_properties (GtkWidget * widget, GbWidgetGetArgData * data)
{ guint i;

  GladeWidgetData *wdata = data->widget_data;
  GbWidgetAction action = data->action;

  /* Note that when saving we only save the property if it is not the
     default value. Otherwise we get lots of unnecessary XML output. */

  /* C options. */
  if (action != GB_SAVING || wdata->source_file)
    gb_widget_output_filename (data, GbCSourceFile, wdata->source_file);
  if (action != GB_SAVING || wdata->public_field == 0)
    gb_widget_output_bool (data, GbCPublic, wdata->public_field);
  if (data->action == GB_SHOWING)
    {
      /* We only want the source file set for toplevel widgets. */
      if (glade_util_is_component (widget))
	property_set_sensitive (GbCSourceFile, TRUE);
      else
	property_set_sensitive (GbCSourceFile, FALSE);
    }

  /* C++ options. */
  data->agent = "glademm";
  if (action != GB_SAVING || wdata->cxx_separate_class == 1)
    gb_widget_output_bool (data, GbCxxSeparateClass, wdata->cxx_separate_class);
  if (action == GB_SHOWING)
    property_set_sensitive (GbCxxSeparateFile, wdata->cxx_separate_class);
  if (action != GB_SAVING || wdata->cxx_separate_file == 1)
    gb_widget_output_bool (data, GbCxxSeparateFile, wdata->cxx_separate_file);
  if (action != GB_SAVING || wdata->cxx_visibility != 0)
    for (i = 0; GbCxxVisibilityChoices[i]; i++)
	{
	  if (GbCxxVisibilityValues[i] == wdata->cxx_visibility)
	    gb_widget_output_choice (data, GbCxxVisibility, i,
				     GbCxxVisibilitySymbols[i]);
	}
  data->agent = NULL;
}


/* This is used when loading or applying properties. */
static void
set_lang_specific_properties (GtkWidget * widget, GbWidgetSetArgData * data)
{
  GladeWidgetData *wdata = data->widget_data;
  gchar *filename;
  gboolean public_field;
  gboolean cxx_separate_file, cxx_separate_class;
  gchar *cxx_visibility;

  /* C options. */
  filename = gb_widget_input_filename (data, GbCSourceFile);
  if (data->apply)
    {
      g_free (wdata->source_file);
      wdata->source_file = g_strdup (filename);
    }
  public_field = gb_widget_input_bool (data, GbCPublic);
  if (data->apply)
    wdata->public_field = public_field ? 1 : 0;

  /* C++ options. */
  data->agent = "glademm";
  cxx_separate_class = gb_widget_input_bool (data, GbCxxSeparateClass);
  if (data->apply)
    {
      wdata->cxx_separate_class = cxx_separate_class ? 1 : 0;
      if (!wdata->cxx_separate_class)
	wdata->cxx_separate_file = 0;

      if (property_get_widget () == widget)
	{
	  property_set_sensitive (GbCxxSeparateFile, wdata->cxx_separate_class);
	  property_set_auto_apply (FALSE);
	  property_set_bool (GbCxxSeparateFile, FALSE);
	  property_set_auto_apply (TRUE);
	}
    }

  cxx_separate_file = gb_widget_input_bool (data, GbCxxSeparateFile);
  if (data->apply)
    wdata->cxx_separate_file = cxx_separate_file ? 1 : 0;

  cxx_visibility = gb_widget_input_choice (data, GbCxxVisibility);
  if (data->apply)
    {
      guint i;
      for (i = 0; GbCxxVisibilityChoices[i]; i++)
	{
	  if (!strcmp (cxx_visibility, GbCxxVisibilityChoices[i])
	      || !strcmp (cxx_visibility, GbCxxVisibilitySymbols[i]))
	    {
	      wdata->cxx_visibility = GbCxxVisibilityValues[i];
	      break;
	    }
	}
    }
  data->agent = NULL;
}


static void
get_position_properties (GtkWidget * widget,
			 GbWidgetGetArgData * data)
{
  GladeWidgetData *wdata = data->widget_data;
  gboolean wh_applicable = TRUE;
  gboolean show_wh_buttons = TRUE;

#if 0
  g_print ("In get_pos: %s WS:%s HS:%s ",
	gtk_widget_get_name (widget),
	wdata->flags & GLADE_WIDTH_SET ? "1" : "0",
	wdata->flags & GLADE_HEIGHT_SET ?  "1" : "0");
#endif

  /* Don't bother if widget's area hasn't been allocated yet, unless it is a
     window. */
  /* Note: When using the widget tree to view widget properties the widget's
     area may not have been allocated but we mustn't just return here. */
#if 0
  if (data->action == GB_SHOWING && (wdata->flags & GLADE_SIZE_NOT_ALLOCATED)
      && !GTK_IS_WINDOW (widget))
      return;
#endif

  if (GTK_IS_MENU (widget))
    {
      if (data->action == GB_SHOWING)
	{
	  gb_widget_output_int (data, GbWidth, widget->requisition.width);
	  gb_widget_output_int (data, GbHeight, widget->requisition.height);
	}
      wh_applicable = FALSE;
    }
  else if (GTK_IS_WINDOW (widget))
    {
      /* Toplevel window widgets */
      if (data->action == GB_SHOWING)
	{
	  gb_widget_output_int (data, GbWidth, wdata->width);
	  gb_widget_output_int (data, GbHeight, wdata->height);
	}
      else
	{
	  if (wdata->flags & GLADE_WIDTH_SET)
	    gb_widget_output_int (data, GbWidth, wdata->width);
	  if (wdata->flags & GLADE_HEIGHT_SET)
	    gb_widget_output_int (data, GbHeight, wdata->height);
	}
    }
  else if (widget->parent && (GTK_IS_FIXED (widget->parent)
			      || GTK_IS_LAYOUT (widget->parent)))
    {
      /* Widgets in fixed or layout containers. Note that for widgets in a 
       layout the allocation is relative to the window origin and changes as
       the layout is scrolled, so don't output that as the x & y coords. */
      gb_widget_output_int (data, GbWidth, wdata->width);
      gb_widget_output_int (data, GbHeight, wdata->height);
      show_wh_buttons = FALSE;
    }
  else
    {
      /* Widgets in standard containers */
      if (data->action == GB_SHOWING)
	{
	  /* If the width or height has been set explicitly we show the size
	     set, else we show the current requisition. We always remember
	     what we have shown in wdata->width & height so we know if the
	     user changes it. */
	  if (!(wdata->flags & GLADE_WIDTH_SET))
	    wdata->width = widget->requisition.width;
	  gb_widget_output_int (data, GbWidth, wdata->width);

	  if (!(wdata->flags & GLADE_HEIGHT_SET))
	    wdata->height = widget->requisition.height;
	  gb_widget_output_int (data, GbHeight, wdata->height);

	}
      else
	{
	  /* Only save if user has set it explicitly. */
	  if (wdata->flags & GLADE_WIDTH_SET)
	    gb_widget_output_int (data, GbWidth, wdata->width);
	  if (wdata->flags & GLADE_HEIGHT_SET)
	    gb_widget_output_int (data, GbHeight, wdata->height);
	}
    }

  /* Show the buttons for setting the size & positions explicitly, if
     applicable, and set the values sensitive if they are currently set. */
  if (data->action == GB_SHOWING)
    {
      if (wh_applicable)
	{
	  gboolean wsensitive = (wdata->flags & GLADE_WIDTH_SET) ? TRUE : FALSE;
	  gboolean hsensitive = (wdata->flags & GLADE_HEIGHT_SET) ? TRUE : FALSE;

	  property_set_sensitive_full (GbWidth, TRUE, wsensitive,
				       show_wh_buttons);
	  property_set_sensitive_full (GbHeight, TRUE, hsensitive,
				       show_wh_buttons);
	}
      else
	{
	  property_set_sensitive_full (GbWidth, FALSE, FALSE, FALSE);
	  property_set_sensitive_full (GbHeight, FALSE, FALSE, FALSE);
	}
    }

#if 0
  g_print ("\n");
#endif
}


static void
save_accelerators (GtkWidget * widget, GbWidgetGetArgData * data)
{
  GList *item;
  GladeAccelerator *accel;

  if (data->widget_data == NULL)
    return;

  item = data->widget_data->accelerators;
  while (item)
    {
      accel = (GladeAccelerator *) item->data;

      save_accelerator (data, accel->modifiers, accel->key, accel->signal);

      item = item->next;
    }
}


static void
save_signals (GtkWidget * widget, GbWidgetGetArgData * data)
{
  GList *item;
  GladeSignal *signal;

  if (data->widget_data == NULL)
    return;

  item = data->widget_data->signals;
  while (item)
    {
      signal = (GladeSignal *) item->data;

      save_signal (data, signal->name, signal->handler, signal->after,
		   signal->object, signal->last_modification_time);

      item = item->next;
    }
}



/*************************************************************************
 * Functions for replacing widgets in the user interface
 *************************************************************************/

/*
 * Replace a child with a new child. Used to replace placeholders with
 * a widget when adding widgets, and also to replace widgets with
 * placeholders when deleting. Returns TRUE on success.
 * NOTE: gbwidgets do not currently have their own function for this,
 * but we'll probably add it at some point.
 */
gboolean
gb_widget_replace_child (GtkWidget * widget,
			 GtkWidget * current_child,
			 GtkWidget * new_child)
{
  gchar *child_name, *new_child_name;

  /* Copy the child name to the new widget if necessary. */
  child_name = gb_widget_get_child_name (current_child);
  new_child_name = gb_widget_get_child_name (new_child);

  /* We never copy these child names to the new child widget.
     The "vscrollbar" and "hscrollbar" child names come from libglade-convert,
     but we don't use them and we need to get rid of them. */
  if (child_name && (!strcmp (child_name, "vscrollbar")
		     || !strcmp (child_name, "hscrollbar")))
    {
      child_name = NULL;
    }
	  
  /* Copy the old child name across. */
  gb_widget_set_child_name (new_child, child_name);

#ifdef USE_GNOME
  if (BONOBO_IS_DOCK_ITEM (widget))
    {
      gboolean is_floating;
      gint x, y;

      /* GnomeDockItem widgets which are floating automatically disappear when
	 the child is removed, so we remember the position and try to show it
	 again in the same place after adding the new widget. */
      is_floating = BONOBO_DOCK_ITEM (widget)->is_floating ? TRUE : FALSE;
      if (is_floating)
	bonobo_dock_item_get_floating_position (BONOBO_DOCK_ITEM (widget),
					       &x, &y);
      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      gtk_widget_hide (new_child);
      gtk_container_add (GTK_CONTAINER (widget), new_child);
      gtk_widget_show (new_child);

      if (is_floating)
	bonobo_dock_item_detach (BONOBO_DOCK_ITEM (widget), x, y);
    }
  else if (GTK_IS_FRAME (widget))
#else
  if (GTK_IS_FRAME (widget))
#endif
    {
      /* If this is the frame's label widget, we replace that. */
      if (gtk_frame_get_label_widget (GTK_FRAME (widget)) == current_child)
	{
	  gtk_frame_set_label_widget (GTK_FRAME (widget), new_child);
	}
      else
	{
	  gtk_container_remove (GTK_CONTAINER (widget), current_child);
	  gtk_container_add (GTK_CONTAINER (widget), new_child);
	}
    }
  else if (GTK_IS_TOOL_ITEM (widget))
    {
      /* For a GtkToolItem, if the current child is a placeholder and the
	 GtkToolItem is not a GbWidget or the new child is a GtkToolItem,
	 we replace the GtkToolItem instead. Otherwise we replace the
	 placeholder as usual. */
      if (GB_IS_PLACEHOLDER (current_child)
	  && (!GB_IS_GB_WIDGET (widget) || GTK_IS_TOOL_ITEM (new_child)))
	{
	  return gb_widget_replace_child (widget->parent, widget, new_child);
	}
      else
	{
	  gtk_container_remove (GTK_CONTAINER (widget), current_child);
	  gtk_container_add (GTK_CONTAINER (widget), new_child);
	}
    }
  else if (GTK_IS_EXPANDER (widget))
    {
      /* If this is the expander's label widget, we replace that. */
      if (gtk_expander_get_label_widget (GTK_EXPANDER (widget)) == current_child)
	{
	  gtk_expander_set_label_widget (GTK_EXPANDER (widget), new_child);
	}
      else
	{
	  gtk_container_remove (GTK_CONTAINER (widget), current_child);
	  gtk_container_add (GTK_CONTAINER (widget), new_child);
	}
    }
  else if (GTK_IS_BIN (widget))
    {
      /* For a bin, we just delete the existing child and add the new one. */
      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      gtk_container_add (GTK_CONTAINER (widget), new_child);
    }
  else if (GTK_IS_BOX (widget))
    {
      /* For a box, we find out the position of the current child, delete it,
         add the new one, and move it into position with reorder_child().
	 If the existing child is a placeholder and the new one is a menubar
	 or toolbar, we set the packing so it doesn't expand, as that is
	 probably what the user wants. */
      gboolean expand, fill;
      guint padding;
      GtkPackType pack_type;
      gint pos = glade_util_get_box_pos (GTK_BOX (widget), current_child);
      g_return_val_if_fail (pos != -1, FALSE);
      gtk_box_query_child_packing (GTK_BOX (widget), current_child,
				   &expand, &fill, &padding, &pack_type);

      /* If we are replacing a placeholder (i.e. we are adding a new widget),
	 we try to set the expand & fill options to reasonable defaults. */
      if (GB_IS_PLACEHOLDER (current_child))
	{
	  if (GTK_IS_LABEL (new_child)
	      || GTK_IS_BUTTON (new_child)
	      || GTK_IS_OPTION_MENU (new_child)
	      || GTK_IS_PROGRESS (new_child)
	      || GTK_IS_PROGRESS_BAR (new_child)
	      || GTK_IS_MENU_BAR (new_child)
	      || GTK_IS_TOOLBAR (new_child)
	      || GTK_IS_STATUSBAR (new_child))
	    {
	      expand = FALSE;
	      fill = FALSE;
	    }

	  /* In a vbox, entry & combo widgets should not expand/fill either. */
	  if (GTK_IS_VBOX (widget))
	    {
	      if (GTK_IS_ENTRY (new_child)
		  || GTK_IS_COMBO (new_child)
		  || GTK_IS_SPIN_BUTTON (new_child)
#ifdef USE_GNOME
		   || GNOME_IS_DATE_EDIT (new_child)
		   || GNOME_IS_FILE_ENTRY (new_child)
		   || GNOME_IS_PIXMAP_ENTRY (new_child)
#endif
		  )
		{
		  expand = FALSE;
		  fill = FALSE;
		}
	    }
	}

      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      gtk_container_add (GTK_CONTAINER (widget), new_child);

      gtk_box_set_child_packing (GTK_BOX (widget), new_child, expand, fill,
				 padding, pack_type);
      gtk_box_reorder_child (GTK_BOX (widget), new_child, pos);

    }
  else if (GTK_IS_TOOLBAR (widget))
    {
      gint pos;

      pos = gtk_toolbar_get_item_index (GTK_TOOLBAR (widget),
					GTK_TOOL_ITEM (current_child));
      g_return_val_if_fail (pos != -1, FALSE);

      /* FIXME: GTK+/GNOME 2 bug workaround - something keeps a ref to the
	 initial buttons added to a GnomeApp, which causes when they eventually
	 get destroyed later and we try to remove them from the tree. So we
	 remove them from the tree here. */
      tree_remove_widget (current_child);

      gtk_container_remove (GTK_CONTAINER (widget), current_child);

      /* If the new child is a GtkToolItem, we can simply add it at the old
	 position. */
      if (GTK_IS_TOOL_ITEM (new_child))
	{
	  gtk_toolbar_insert (GTK_TOOLBAR (widget),
			      GTK_TOOL_ITEM (new_child), pos);
	}
      /* If the new child is a placeholder, we need to insert a
	 GtkToolItem above it (but not a GbWidget). */
      else if (GB_IS_PLACEHOLDER (new_child))
	{
	  GtkWidget *toolitem = (GtkWidget*) gtk_tool_item_new ();
	  gtk_widget_show (toolitem);
	  gtk_container_add (GTK_CONTAINER (toolitem), new_child);
	  gtk_toolbar_insert (GTK_TOOLBAR (widget),
			      GTK_TOOL_ITEM (toolitem), pos);
	}
      /* If the new child is not a GtkToolItem, we need to insert a
	 GtkToolItem above it, but use a GbWidget so its properties can be
	 set as required. */
      else
	{
	  GtkWidget *toolitem = gb_widget_new ("GtkToolItem", NULL);
	  gtk_widget_show (toolitem);
	  gtk_container_add (GTK_CONTAINER (toolitem), new_child);
	  gtk_toolbar_insert (GTK_TOOLBAR (widget),
			      GTK_TOOL_ITEM (toolitem), pos);
	  tree_add_widget (toolitem);
	}
    }
  else if (GTK_IS_LIST (widget))
    {
      /* For a list, we find out the position of the current child, delete it,
         and add the new one at the same position. */
      gint pos = gtk_list_child_position (GTK_LIST (widget), current_child);
      GList glist =
      {NULL, NULL, NULL};
      glist.data = current_child;
      gtk_list_remove_items (GTK_LIST (widget), &glist);
      glist.data = new_child;
      gtk_list_insert_items (GTK_LIST (widget), &glist, pos);

    }
  else if (GTK_IS_NOTEBOOK (widget))
    {
      /* For a notebook, we find out the position of the current child, delete
         it, and add the new one at the same position. If the current_child is
         a notebook tab, just replace it. */
      GtkWidget *page, *tab_label;
      gint pos;

      pos = find_notebook_page (GTK_NOTEBOOK (widget), current_child,
				&page, &tab_label);
      g_return_val_if_fail (pos != -1, FALSE);

      if (page == current_child)
	{
	  gtk_widget_ref (tab_label);
	  gtk_notebook_remove_page (GTK_NOTEBOOK (widget), pos);
	  gtk_notebook_insert_page (GTK_NOTEBOOK (widget),
				    new_child, tab_label, pos);
	  gtk_notebook_set_current_page (GTK_NOTEBOOK (widget), pos);
	  gtk_widget_unref (tab_label);
	}
      else
	{
	  gtk_widget_ref (page);
	  gtk_notebook_remove_page (GTK_NOTEBOOK (widget), pos);
	  gtk_notebook_insert_page (GTK_NOTEBOOK (widget),
				    page, new_child, pos);
	  gtk_notebook_set_current_page (GTK_NOTEBOOK (widget), pos);
	  gtk_widget_unref (page);
	}

    }
  else if (GTK_IS_PANED (widget))
    {
      /* For paned, we find out the position of the current child, delete it,
         and add the new one at the same position. */
      gint pos = (GTK_PANED (widget)->child1 == current_child) ? 1 : 2;
      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      if (pos == 1)
	gtk_paned_add1 (GTK_PANED (widget), new_child);
      else
	gtk_paned_add2 (GTK_PANED (widget), new_child);

    }
  else if (GTK_IS_TABLE (widget))
    {
      /* For a table, we find out the position & size of the current child,
         delete it, and add the new one in the same place. */
      gint left, right, top, bottom;
      GtkTableChild *tchild;
      GtkAttachOptions xoptions, yoptions;

      tchild = glade_util_find_table_child (GTK_TABLE (widget), current_child);
      g_return_val_if_fail (tchild != NULL, FALSE);

      left = tchild->left_attach;
      right = tchild->right_attach;
      top = tchild->top_attach;
      bottom = tchild->bottom_attach;

      xoptions = 0;
      if (tchild->xexpand)
	xoptions |= GTK_EXPAND;
      if (tchild->xshrink)
	xoptions |= GTK_SHRINK;
      if (tchild->xfill)
	xoptions |= GTK_FILL;

      yoptions = 0;
      if (tchild->yexpand)
	yoptions |= GTK_EXPAND;
      if (tchild->yshrink)
	yoptions |= GTK_SHRINK;
      if (tchild->yfill)
	yoptions |= GTK_FILL;

      /* If we are replacing a placeholder (i.e. we are adding a new widget),
	 we try to set the expand & fill options to reasonable defaults. */
      if (GB_IS_PLACEHOLDER (current_child))
	{
	  if (GTK_IS_LABEL (new_child)
	      || GTK_IS_BUTTON (new_child)
	      || GTK_IS_OPTION_MENU (new_child)
	      || GTK_IS_PROGRESS (new_child)
	      || GTK_IS_PROGRESS_BAR (new_child)
	      || GTK_IS_MENU_BAR (new_child)
	      || GTK_IS_TOOLBAR (new_child)
	      || GTK_IS_STATUSBAR (new_child))
	    {
	      xoptions = GTK_FILL;
	      yoptions = 0;
	    }
	  else if (GTK_IS_ENTRY (new_child)
		   || GTK_IS_COMBO (new_child)
		   || GTK_IS_SPIN_BUTTON (new_child)
#ifdef USE_GNOME
		   || GNOME_IS_DATE_EDIT (new_child)
		   || GNOME_IS_FILE_ENTRY (new_child)
		   || GNOME_IS_PIXMAP_ENTRY (new_child)
#endif
		   )
	    {
	      xoptions = GTK_EXPAND | GTK_FILL;
	      yoptions = 0;
	    }
	}

      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      gtk_table_attach (GTK_TABLE (widget), new_child,
			left, right, top, bottom, xoptions, yoptions, 0, 0);

      /* Note that if we have just added a placeholder, but there is already
	 a widget at the same position in the table, the placeholder will be
	 destroyed immediately. Thus don't rely on the new widget still being
	 alive after calling gb_widget_replace_child(). */
      gb_table_update_placeholders (widget, -1, -1);
    }
#if GLADE_SUPPORTS_GTK_TREE
  else if (GTK_IS_TREE (widget))
    {
      /* For a tree, we find out the position of the current child, delete it,
         and add the new one at the same position. */
      gint pos = gtk_tree_child_position (GTK_TREE (widget), current_child);
      GList glist =
      {NULL, NULL, NULL};
      glist.data = current_child;
      gtk_tree_remove_items (GTK_TREE (widget), &glist);
      gtk_tree_insert (GTK_TREE (widget), new_child, pos);

    }
#endif
  else if (GTK_IS_CLIST (widget))
    {
      /* For a clist, we check if the widget is a column title and if it is
	 we replace it. */
	gint pos;
	GtkCList *clist;

	clist = GTK_CLIST(widget);

	for (pos = 0; pos < clist->columns; pos++)
	{
	    if (clist->column[pos].button == current_child)
	    {
		gtk_clist_set_column_widget(clist, pos, new_child);
 	    }
	}
    }
  else if (GTK_IS_FIXED (widget) || GTK_IS_LAYOUT (widget))
    {
      GladeWidgetData *wdata;
      gint x, y, w, h;

      /* For a fixed, we find the position and size of the existing child,
	 remove it, and add the new one in the same place. */
      wdata = gtk_object_get_data (GTK_OBJECT (current_child),
				   GB_WIDGET_DATA_KEY);
      g_return_val_if_fail (wdata != NULL, FALSE);

      gtk_container_child_get (GTK_CONTAINER (widget), current_child,
			       "x", &x,
			       "y", &y,
			       NULL);
      w = wdata->width;
      h = wdata->height;

      wdata = gtk_object_get_data (GTK_OBJECT (new_child), GB_WIDGET_DATA_KEY);
      g_return_val_if_fail (wdata != NULL, FALSE);
      wdata->flags |= GLADE_WIDTH_SET | GLADE_HEIGHT_SET;
      wdata->width = w;
      wdata->height = h;

      /* Reset the widget's uposition, just in case it gets added to a standard
	 container. I don't think we need this for GTK+ 2. */
      /*gtk_widget_set_uposition (current_child, -1, -1);*/

      /* FIXME: GTK+ 1.2.3 bug workaround. We need to ref the widget to stop
	 gtk_layout_remove() from issuing a warning. */
      gtk_widget_ref (current_child);
      gtk_container_remove (GTK_CONTAINER (widget), current_child);
      gtk_widget_unref (current_child);

      if (GTK_IS_FIXED (widget))
	{
	  gtk_fixed_put (GTK_FIXED (widget), new_child, x, y);
	  /*gtk_widget_set_uposition (new_child, x, y);*/
	}
      else
	{
	  gtk_layout_put (GTK_LAYOUT (widget), new_child, x, y);
	}
      gb_widget_set_usize (new_child, w, h);
    }

#ifdef USE_GNOME
  else if (BONOBO_IS_DOCK (widget))
    {
      /* For a GnomeDock, we call bonobo_dock_set_client_area (). It removes
	 the existing child automatically. */
      bonobo_dock_set_client_area (BONOBO_DOCK (widget), new_child);
    }

#endif

  else if (GTK_IS_CONTAINER (widget))
    {
      /* General code for container - has to remove all children and add back
         NOTE: this may not work for specialised containers.
         NOTE: need to ref widgets? */
      g_warning (_("replacing child of container - not implemented yet\n"));
      return FALSE;
    }

  return TRUE;
}


/*************************************************************************
 * Functions for showing/hiding tooltips of the widgets
 *************************************************************************/

gboolean
gb_widget_get_show_tooltips ()
{
  return GTK_TOOLTIPS (gb_widget_tooltips)->enabled;
}


void
gb_widget_set_show_tooltips (gboolean show)
{
  if (show)
    gtk_tooltips_enable (gb_widget_tooltips);
  else
    gtk_tooltips_disable (gb_widget_tooltips);
}


void
gb_widget_reset_tooltips ()
{
  gtk_object_destroy (GTK_OBJECT (gb_widget_tooltips));
  gb_widget_tooltips = gtk_tooltips_new ();
}



/*************************************************************************
 * Misc. Functions
 *************************************************************************/


/* This is a GTK bug workaround for combo widgets. They should manage the
   size of their GtkEntry, but they don't at present, so we have to set its
   size explicitly (and also in the source code output).
   I think this has been fixed. It seems to be OK in GTK+ 1.2.3. */
void
gb_widget_set_usize (GtkWidget *widget,
		     gint w,
		     gint h)
{
#if 0
  if (GTK_IS_COMBO (widget))
    gtk_widget_set_usize (GTK_COMBO (widget)->entry,
			  w - 16 < 0 ? -1 : w - 16, h);
#endif
  gtk_widget_set_usize (widget, w, h);
}


static gint
find_notebook_page (GtkNotebook * notebook, GtkWidget * current_child,
		    GtkWidget **page, GtkWidget **tab_label)
{
  gint nchildren, i;
  GtkWidget *tmp_page, *tmp_tab_label;

  nchildren = g_list_length (notebook->children);
  for (i = 0; i < nchildren; i++)
    {
      tmp_page = gtk_notebook_get_nth_page (notebook, i);
      tmp_tab_label = gtk_notebook_get_tab_label (notebook, tmp_page);

      if (tmp_page == current_child || tmp_tab_label == current_child)
	{
	  *page = tmp_page;
	  *tab_label = tmp_tab_label;
	  return i;
	}
    }

  return -1;
}

static void
dummy_detach (GtkWidget *attach_widget,
	      GtkMenu *menu)
{
  ;
}

/*
 * FIXME: MAJOR HACK.
 *
 * GtkOptionMenu places the currently selected item inside its button, and
 * removes it from the menu. So we have to hack around that here so that the
 * menu items are all output correctly, in the XML and the source code.
 *
 * We remove the menu from the original option menu and add it to a temporary
 * one with the same name.
 */
static void
option_menu_foreach (GtkOptionMenu *option,
		     GtkCallback callback,
		     gpointer data)
{
  GtkWidget *menu = option->menu;
  GtkWidget *temp;
  int history;

  if (!menu)
    return;

  temp = gtk_option_menu_new ();
  gtk_widget_set_name (temp, gtk_widget_get_name (GTK_WIDGET (option)));
  gtk_object_ref (GTK_OBJECT (temp));
  gtk_object_sink (GTK_OBJECT (temp));

  history = gtk_option_menu_get_history (option);

  gtk_object_ref (GTK_OBJECT (menu));

  gtk_option_menu_set_menu (option, gtk_menu_new ());
  gtk_menu_attach_to_widget (GTK_MENU (menu), temp, dummy_detach);
  (*callback) (menu, data);
  gtk_menu_detach (GTK_MENU (menu));
  gtk_option_menu_set_menu (option, menu);

  gtk_object_unref (GTK_OBJECT (menu));

  gtk_option_menu_set_history (option, history);

  gtk_object_unref (GTK_OBJECT (temp));
}

static void
combo_foreach (GtkCombo *combo,
	       GtkCallback callback,
	       gpointer data)
{
  (*callback) (combo->entry, data);
  (*callback) (combo->list, data);
  (*callback) (combo->button, data);
}


/* This calls the given callback for each child of a widget. It gets round
   some of the quirks of the different versions of GTK, and descends menus
   as well. */
void
gb_widget_children_foreach (GtkWidget *widget,
			    GtkCallback callback,
			    gpointer data)
{
  /* SPECIAL CODE: for table, so we output in the reverse order. */
  if (GTK_IS_TABLE (widget))
    table_foreach (GTK_TABLE (widget), callback, data);
  else if (GTK_IS_COMBO (widget))
    combo_foreach (GTK_COMBO (widget), callback, data);
  else if (GTK_IS_BOX (widget))
    box_foreach (GTK_BOX (widget), callback, data);
  else if (GTK_IS_OPTION_MENU (widget))
    option_menu_foreach (GTK_OPTION_MENU (widget), callback, data);
  else if (GTK_IS_CONTAINER (widget))
    gtk_container_forall (GTK_CONTAINER (widget), callback, data);

  /* SPECIAL CODE: for menu items, descend to child menus. */
  if (GTK_IS_MENU_ITEM (widget) && GTK_MENU_ITEM (widget)->submenu)
    (*callback) (GTK_MENU_ITEM (widget)->submenu, data);
}


/* This function is used to iterate through the table children in reverse.
   It is needed so we output the XML file in the same order each time. */
static void
table_foreach (GtkTable * table,
	       GtkCallback callback,
	       gpointer callback_data)
{
  GList *children;
  GtkTableChild *child;

  children = g_list_last (table->children);
  while (children)
    {
      child = children->data;
      children = children->prev;

      (*callback) (child->widget, callback_data);
    }
}


static void
box_foreach (GtkBox *box,
	     GtkCallback callback,
	     gpointer callback_data)
{
  GList *children;
  GtkBoxChild *child;

  children = box->children;
  while (children)
    {
      child = children->data;
      children = children->next;

      (* callback) (child->widget, callback_data);
    }
}

/*************************************************************************
 * Common popup menu callbacks
 *************************************************************************/

static void
gb_widget_add_alignment (GtkWidget * menuitem,
			 GtkWidget * widget)
{
  GtkWidget *alignment, *parent;

  parent = widget->parent;
  alignment = gb_widget_new ("GtkAlignment", parent);

  gtk_widget_ref (widget);
  if (!gb_widget_replace_child (parent, widget, alignment))
    {
      glade_util_show_message_box (_("Couldn't insert GtkAlignment widget."),
				   parent);
      gtk_widget_destroy (alignment);
      gtk_widget_unref (widget);
      return;
    }
  if (GTK_BIN (alignment)->child)
    gtk_container_remove (GTK_CONTAINER (alignment),
			  GTK_BIN (alignment)->child);
  gtk_container_add (GTK_CONTAINER (alignment), widget);
  gtk_widget_unref (widget);
  tree_insert_widget_parent (alignment, widget);
}


static void
gb_widget_remove_alignment (GtkWidget * menuitem,
			    GtkWidget * widget)
{
  GtkWidget *alignment, *parent;

  alignment = widget->parent;
  g_return_if_fail (GTK_IS_ALIGNMENT (alignment));
  parent = alignment->parent;

  gtk_widget_ref (widget);
  gtk_widget_ref (alignment);

  /* Remove the alignment and all children from the tree. */
  tree_remove_widget (alignment);

  gtk_container_remove (GTK_CONTAINER (alignment), widget);

  if (gb_widget_replace_child (parent, alignment, widget))
    {
      /* Now add the widget and its children back to the tree. */
      tree_add_widget (widget);
    }
  else
    {
      glade_util_show_message_box (_("Couldn't remove GtkAlignment widget."),
				   parent);
      /* Try to put it back as it was. */
      gtk_container_add (GTK_CONTAINER (alignment), widget);
      tree_add_widget (alignment);
    }

  gtk_widget_unref (alignment);
  gtk_widget_unref (widget);
}


static void
gb_widget_add_event_box (GtkWidget * menuitem,
			 GtkWidget * widget)
{
  GtkWidget *event_box, *parent;

  parent = widget->parent;
  event_box = gb_widget_new ("GtkEventBox", parent);

  gtk_widget_ref (widget);
  if (!gb_widget_replace_child (parent, widget, event_box))
    {
      glade_util_show_message_box (_("Couldn't insert GtkEventBox widget."),
				   parent);
      gtk_widget_destroy (event_box);
      gtk_widget_unref (widget);
      return;
    }
  if (GTK_BIN (event_box)->child)
    gtk_container_remove (GTK_CONTAINER (event_box),
			  GTK_BIN (event_box)->child);
  gtk_container_add (GTK_CONTAINER (event_box), widget);
  gtk_widget_unref (widget);
  tree_insert_widget_parent (event_box, widget);
}

static void
gb_widget_remove_event_box (GtkWidget * menuitem,
			    GtkWidget * widget)
{
  GtkWidget *event_box, *parent;

  event_box = widget->parent;
  g_return_if_fail (GTK_IS_EVENT_BOX (event_box));
  parent = event_box->parent;

  gtk_widget_ref (widget);
  gtk_widget_ref (event_box);

  /* Remove the event box and all children from the tree. */
  tree_remove_widget (event_box);

  gtk_container_remove (GTK_CONTAINER (event_box), widget);

  if (gb_widget_replace_child (parent, event_box, widget))
    {
      /* Now add the widget and its children back to the tree. */
      tree_add_widget (widget);
    }
  else
    {
      glade_util_show_message_box (_("Couldn't remove GtkEventBox widget."),
				   parent);
      /* Try to put it back as it was. */
      gtk_container_add (GTK_CONTAINER (event_box), widget);
      tree_add_widget (event_box);
    }
  gtk_widget_unref (event_box);
  gtk_widget_unref (widget);
}


static void
gb_widget_redisplay_window (GtkWidget * menuitem,
			    GtkWidget * widget)
{
  g_return_if_fail (GTK_IS_WINDOW (widget));

  /* See also editor_on_key_press_event() in editor.c. */
  glade_util_close_window (widget);
  gtk_window_reshow_with_initial_size (GTK_WINDOW (widget));
}


static void
gb_widget_add_scrolled_window (GtkWidget * menuitem,
			       GtkWidget * widget)
{
  GtkWidget *scrolledwin, *parent;

  parent = widget->parent;
  scrolledwin = gb_widget_new ("GtkScrolledWindow", parent);

  gtk_widget_ref (widget);
  if (!gb_widget_replace_child (parent, widget, scrolledwin))
    {
      glade_util_show_message_box (_("Couldn't insert GtkScrolledWindow widget."), parent);
      gtk_widget_destroy (scrolledwin);
      gtk_widget_unref (widget);
      return;
    }
  if (GTK_BIN (scrolledwin)->child)
    gtk_container_remove (GTK_CONTAINER (scrolledwin),
			  GTK_BIN (scrolledwin)->child);
  gtk_container_add (GTK_CONTAINER (scrolledwin), widget);
  gtk_widget_unref (widget);
  tree_insert_widget_parent (scrolledwin, widget);
}


static void
gb_widget_remove_scrolled_window (GtkWidget * menuitem,
				  GtkWidget * widget)
{
  GtkWidget *scrolledwin, *parent;

  scrolledwin = widget->parent;
  g_return_if_fail (GTK_IS_SCROLLED_WINDOW (scrolledwin));
  parent = scrolledwin->parent;

  gtk_widget_ref (widget);
  gtk_widget_ref (scrolledwin);

  /* Remove the alignment and all children from the tree. */
  tree_remove_widget (scrolledwin);

  gtk_container_remove (GTK_CONTAINER (scrolledwin), widget);

  if (gb_widget_replace_child (parent, scrolledwin, widget))
    {
      /* Now add the widget and its children back to the tree. */
      tree_add_widget (widget);
    }
  else
    {
      glade_util_show_message_box (_("Couldn't remove GtkScrolledWindow widget."), parent);
      /* Try to put it back as it was. */
      gtk_container_add (GTK_CONTAINER (scrolledwin), widget);
      tree_add_widget (scrolledwin);
    }

  gtk_widget_unref (scrolledwin);
  gtk_widget_unref (widget);
}


/*************************************************************************
 * Common functions used by gbwidgets.
 *************************************************************************/

/* This gets the child label for buttons or menuitems or subclasses.
   This is for showing or saving. */
void
gb_widget_output_child_label (GtkWidget * widget, GbWidgetGetArgData * data,
			      const gchar * Label)
{
  GtkWidget *child;
  gchar *label_text;

  child = GTK_BIN (widget)->child;

  /* Note that we don't want to save the child label if it is a GbWidget,
     since it will be saved as a separate widget. */
  if (child && GTK_IS_LABEL (child) && !GB_IS_GB_WIDGET (child))
    {
      label_text = glade_util_get_label_text (child);
      gb_widget_output_translatable_text (data, Label, label_text);
      g_free (label_text);

      if (data->action == GB_SHOWING)
	property_set_sensitive (Label, TRUE);

      /* All our menuitems use underlined accelerators. */
      if (data->action == GB_SAVING && GTK_IS_MENU_ITEM (widget))
	gb_widget_output_bool (data, "use_underline", TRUE);
    }
  else
    {
      if (data->action == GB_SHOWING)
	{
	  gb_widget_output_translatable_text (data, Label, "");
	  property_set_sensitive (Label, FALSE);
	}
    }
}


/* This sets the child label for buttons/items/menuitems or subclasses.
   This is for applying or loading. */
void
gb_widget_input_child_label (GtkWidget * widget, GbWidgetSetArgData * data,
			     const gchar * Label)
{
  GtkWidget *child, *label;
  gchar *label_text;

  child = GTK_BIN (widget)->child;

  label_text = gb_widget_input_text (data, Label);
  if (data->apply)
    {
      if (child && GTK_IS_LABEL (child))
	{
	  gtk_label_set_text_with_mnemonic (GTK_LABEL (child), label_text);
	}
      else
	{
	  if (child != NULL)
	    gtk_container_remove (GTK_CONTAINER (widget), child);
	  if (GTK_IS_MENU_ITEM (widget))
	    {
	      label = gtk_accel_label_new ("");
	      gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label),
						widget);
	    }
	  else
	    {
	      label = gtk_label_new ("");
	    }
	  gtk_container_add (GTK_CONTAINER (widget), label);
	  gtk_label_set_text_with_mnemonic (GTK_LABEL (label), label_text);
	  gtk_widget_show (label);

	  /* Simple child labels are given different alignments according to
	     the parent. GtkButton and GtkToggleButton are centred. All the
	     others are aligned left. See the GTK _new_with_label() fns. */
	  if (data->action == GB_LOADING)
	    {
	      if (GTK_IS_CHECK_BUTTON (widget)
		  || GTK_IS_ITEM (widget))
		gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
	    }
	}
    }
  /* This isn't very nice. When a text property is got from the property
     editor (i.e. when action is GB_APPLYING) it needs to be freed after. */
  if (data->action == GB_APPLYING)
    g_free (label_text);
}


void
gb_widget_create_child_label_popup_menu (GtkWidget	       *widget,
					 GbWidgetCreateMenuData *data)
{
  GtkWidget *menuitem;

  if (GTK_IS_LABEL (GTK_BIN (widget)->child))
    {
      menuitem = gtk_menu_item_new_with_label (_("Remove Label"));
      gtk_widget_show (menuitem);
      gtk_container_add (GTK_CONTAINER (data->menu), menuitem);
      gtk_signal_connect (GTK_OBJECT (menuitem), "activate",
			  GTK_SIGNAL_FUNC (gb_widget_remove_label), widget);
    }
}


void
gb_widget_remove_label (GtkWidget * menuitem,
			GtkWidget * widget)
{
  GtkWidget *child;

  g_return_if_fail (GTK_IS_BIN (widget));

  child = GTK_BIN (widget)->child;
  if (child && GTK_IS_LABEL (child))
    editor_delete_widget (child);
}


/*************************************************************************
 * Adjustment convenience functions - handles all 6 values.
 *************************************************************************/
void
gb_widget_output_adjustment (GbWidgetGetArgData * data,
			     const gchar * Values[],
			     GtkAdjustment * adjustment,
			     gchar *saved_property_name)
{
  /* Adjustments are now saved as a single property with all 6 values in a
     string, e.g. '0 0 100 1 10 10'. */
  if (data->action == GB_SAVING)
    {
      gchar buffer[256];

      sprintf (buffer, "%.12g %.12g %.12g %.12g %.12g %.12g",
	       adjustment->value,
	       adjustment->lower,
	       adjustment->upper,
	       adjustment->step_increment,
	       adjustment->page_increment,
	       adjustment->page_size);
      save_string (data, saved_property_name, buffer);
    }
  else
    {
      if (Values[0])
	gb_widget_output_float (data, Values[0], adjustment->value);
      if (Values[1])
	gb_widget_output_float (data, Values[1], adjustment->lower);
      if (Values[2])
	gb_widget_output_float (data, Values[2], adjustment->upper);
      if (Values[3])
	gb_widget_output_float (data, Values[3], adjustment->step_increment);
      if (Values[4])
	gb_widget_output_float (data, Values[4], adjustment->page_increment);
      if (Values[5])
	gb_widget_output_float (data, Values[5], adjustment->page_size);
    }
}


gboolean
gb_widget_input_adjustment (GbWidgetSetArgData * data,
			    const gchar * Values[],
			    GtkAdjustment * adjustment,
			    gchar *saved_property_name)
{
  gfloat value, lower, upper, step_inc, page_inc, page_size, tmp;

  /* Adjustments are now saved as a single property with all 6 values in a
     string, e.g. '0 0 100 1 10 10'. */
  if (data->action == GB_LOADING)
    {
      gchar *value = gb_widget_input_string (data, saved_property_name);
      if (data->apply)
	{
	  gchar *ptr = value;

	  adjustment->value = g_strtod (ptr, &ptr);
	  adjustment->lower = g_strtod (ptr, &ptr);
	  adjustment->upper = g_strtod (ptr, &ptr);
	  adjustment->step_increment = g_strtod (ptr, &ptr);
	  adjustment->page_increment = g_strtod (ptr, &ptr);
	  adjustment->page_size = g_strtod (ptr, &ptr);

	  return TRUE;
	}
      else
	return FALSE;
    }

  value = adjustment->value;
  lower = adjustment->lower;
  upper = adjustment->upper;
  step_inc = adjustment->step_increment;
  page_inc = adjustment->page_increment;
  page_size = adjustment->page_size;

  if (Values[0])
    {
      tmp = gb_widget_input_float (data, Values[0]);
      if (data->apply)
	value = tmp;
    }

  if (Values[1])
    {
      tmp = gb_widget_input_float (data, Values[1]);
      if (data->apply)
	lower = tmp;
    }

  if (Values[2])
    {
      tmp = gb_widget_input_float (data, Values[2]);
      if (data->apply)
	upper = tmp;
    }

  if (Values[3])
    {
      tmp = gb_widget_input_float (data, Values[3]);
      if (data->apply)
	step_inc = tmp;
    }

  if (Values[4])
    {
      tmp = gb_widget_input_float (data, Values[4]);
      if (data->apply)
	page_inc = tmp;
    }

  if (Values[5])
    {
      tmp = gb_widget_input_float (data, Values[5]);
      if (data->apply)
	page_size = tmp;
    }

  /* Only return TRUE if one or more of the properties have changed. */
  if (adjustment->value != value
      || adjustment->lower != lower
      || adjustment->upper != upper
      || adjustment->step_increment != step_inc
      || adjustment->page_increment != page_inc
      || adjustment->page_size != page_size)
    {
      adjustment->value = value;
      adjustment->lower = lower;
      adjustment->upper = upper;
      adjustment->step_increment = step_inc;
      adjustment->page_increment = page_inc;
      adjustment->page_size = page_size;
      return TRUE;
    }
  return FALSE;
}


/*************************************************************************
 * Functions to input/output properties.
 *************************************************************************/

/* Inputting Properties - Loading or Applying. */

gchar*
gb_widget_input_string (GbWidgetSetArgData *data,
			const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_string (data, property);
  else
    return property_get_string (property, data->widget,
				data->property_to_apply, &data->apply);
}


/* NOTE: You must free the returned string if data->action == GB_APPLYING */
gchar*
gb_widget_input_text (GbWidgetSetArgData *data,
		      const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_text (data, property);
  else
    return property_get_text (property, data->widget,
			      data->property_to_apply, &data->apply);
}


gint
gb_widget_input_int (GbWidgetSetArgData *data,
		     const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_int (data, property);
  else
    return property_get_int (property, data->property_to_apply,
			     &data->apply);
}


gint
gb_widget_input_optional_int (GbWidgetSetArgData *data,
			      const gchar *property,
			      gboolean *is_set)
{
  gint value;

  if (data->action == GB_LOADING)
    {
      value = load_int (data, property);
      if (is_set)
	*is_set = data->apply;
      return value;
    }
  else
    return property_get_optional_int (property, data->property_to_apply,
				      &data->apply, is_set);
}


gfloat
gb_widget_input_float (GbWidgetSetArgData *data,
		       const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_float (data, property);
  else
    return property_get_float (property, data->property_to_apply,
			       &data->apply);
}


gboolean
gb_widget_input_bool (GbWidgetSetArgData *data,
		      const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_bool (data, property);
  else
    return property_get_bool (property, data->property_to_apply,
			      &data->apply);
}


gchar*
gb_widget_input_choice (GbWidgetSetArgData *data,
			const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_choice (data, property);
  else
    return property_get_choice (property, data->property_to_apply,
				&data->apply);
}


gchar*
gb_widget_input_combo (GbWidgetSetArgData *data,
		       const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_combo (data, property);
  else
    return property_get_combo (property, data->property_to_apply,
			       &data->apply);
}


GdkColor*
gb_widget_input_color (GbWidgetSetArgData *data,
		       const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_color (data, property);
  else
    return property_get_color (property, data->property_to_apply,
			       &data->apply);
}


GdkPixmap*
gb_widget_input_bgpixmap (GbWidgetSetArgData *data,
			  const gchar *property,
			  gchar **filename)
{
  if (data->action == GB_LOADING)
    return load_bgpixmap (data, property, filename);
  else
    return property_get_bgpixmap (property, data->property_to_apply,
				  &data->apply, filename);
}


gpointer
gb_widget_input_dialog (GbWidgetSetArgData *data,
			const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_dialog (data, property);
  else
    return property_get_dialog (property, data->property_to_apply,
				&data->apply);
}


gchar*
gb_widget_input_filename (GbWidgetSetArgData *data,
			  const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_filename (data, property);
  else
    return property_get_filename (property, data->property_to_apply,
				  &data->apply);
}


gchar*
gb_widget_input_pixmap_filename (GbWidgetSetArgData *data,
				 const gchar *property)
{
  if (data->action == GB_LOADING)
    return load_pixmap_filename (data, property);
  else
    return property_get_filename (property, data->property_to_apply,
				  &data->apply);
}


GdkFont*
gb_widget_input_font (GbWidgetSetArgData *data,
		      const gchar *property,
		      gchar **xlfd_fontname)
{
  if (data->action == GB_LOADING)
    return load_font (data, property, xlfd_fontname);
  else
    return property_get_font (property, data->property_to_apply,
			      &data->apply, xlfd_fontname);
}


gchar*
gb_widget_input_stock_item	(GbWidgetSetArgData	*data,
				 const gchar		*property)
{
  if (data->action == GB_LOADING)
    return load_string (data, property);
  else
    return property_get_stock_item (property, data->property_to_apply,
				    &data->apply);
}


gchar*
gb_widget_input_icon	(GbWidgetSetArgData	*data,
			 const gchar		*property)
{
  if (data->action == GB_LOADING)
    return load_icon (data, property);
  else
    return property_get_icon (property, data->property_to_apply,
			      &data->apply);
}

gchar*
gb_widget_input_named_icon	(GbWidgetSetArgData	*data,
				 const gchar		*property)
{
  if (data->action == GB_LOADING)
    return load_string (data, property);
  else
    return property_get_named_icon (property, data->property_to_apply,
				    &data->apply);
}

gint
gb_widget_input_enum (GbWidgetSetArgData        *data,
		      GType                      enum_type,
		      const char               **labels,
		      int                       *values,
		      const gchar               *property)
{
  gint retval = 0;
  int i;
  char *s;

  if (data->action == GB_LOADING) 
    {
      s = load_string (data, property);
      if (data->apply && s)
	retval = glade_enum_from_string (enum_type, s);
    }
  else
    {
      s = property_get_choice (property, data->property_to_apply,
			       &data->apply);
      if (data->apply && s)
	{
	  for (i = 0; labels[i]; i++)
	    {
	      if (!strcmp (labels[i], s))
		{
		  retval = values[i];
		  break;
		}
	    }
	}
    }
  return retval;
}

/* Outputting Properties - Saving or Showing. */
void
gb_widget_output_string (GbWidgetGetArgData *data,
			 const gchar *property,
			 const gchar *value)
{
  if (data->action == GB_SAVING)
    save_string (data, property, value);
  else
    property_set_string (property, value);
}


void
gb_widget_output_translatable_string (GbWidgetGetArgData *data,
				      const gchar *property,
				      const gchar *value)
{
  if (data->action == GB_SAVING)
    save_translatable_string (data, property, value);
  else
    property_set_translatable_string (property, value, data->widget);
}


void
gb_widget_output_text (GbWidgetGetArgData *data,
		       const gchar *property,
		       const gchar *value)
{
  if (data->action == GB_SAVING)
    save_text (data, property, value);
  else
    property_set_text (property, value);
}


void
gb_widget_output_translatable_text (GbWidgetGetArgData *data,
				    const gchar *property,
				    const gchar *value)
{
  if (data->action == GB_SAVING)
    save_translatable_text (data, property, value);
  else
    property_set_translatable_text (property, value, data->widget);
}


void
gb_widget_output_translatable_text_in_lines (GbWidgetGetArgData *data,
					     const gchar *property,
					     const gchar *value)
{
  if (data->action == GB_SAVING)
    save_translatable_text_in_lines (data, property, value);
  else
    property_set_translatable_text (property, value, data->widget);
}


void
gb_widget_output_int (GbWidgetGetArgData *data,
		      const gchar *property,
		      gint value)
{
#if 0
  if (property == GbX)
    g_print ("X: %i ", value);
  else if (property == GbY)
    g_print ("Y: %i ", value);
  else if (property == GbWidth)
    g_print ("W: %i ", value);
  else if (property == GbHeight)
    g_print ("H: %i ", value);
#endif

  if (data->action == GB_SAVING)
    save_int (data, property, value);
  else
    property_set_int (property, value);
}


void
gb_widget_output_optional_int (GbWidgetGetArgData *data,
			       const gchar *property,
			       gint value,
			       gboolean is_set)
{
  if (data->action == GB_SAVING)
    {
      if (is_set)
	save_int (data, property, value);
    }
  else
    property_set_optional_int (property, value, is_set);
}


void
gb_widget_output_float (GbWidgetGetArgData *data,
			const gchar *property,
			gfloat value)
{
  if (data->action == GB_SAVING)
    save_float (data, property, value);
  else
    property_set_float (property, value);
}


void
gb_widget_output_bool (GbWidgetGetArgData *data,
		       const gchar *property,
		       gint value)
{
  if (data->action == GB_SAVING)
    save_bool (data, property, value);
  else
    property_set_bool (property, value);
}


void
gb_widget_output_choice (GbWidgetGetArgData *data,
			 const gchar *property,
			 gint value,
			 const gchar *symbol)
{
  if (data->action == GB_SAVING)
    save_choice (data, property, symbol);
  else
    property_set_choice (property, value);
}


void
gb_widget_output_combo (GbWidgetGetArgData *data,
			const gchar *property,
			const gchar *value)
{
  if (data->action == GB_SAVING)
    save_combo (data, property, value);
  else
    property_set_combo (property, value);
}


void
gb_widget_output_color (GbWidgetGetArgData *data,
			const gchar *property,
			GdkColor *value)
{
  if (data->action == GB_SAVING)
    save_color (data, property, value);
  else
    property_set_color (property, value);
}


void
gb_widget_output_bgpixmap (GbWidgetGetArgData *data,
			   const gchar *property,
			   GdkPixmap *value,
			   const gchar *filename)
{
  if (data->action == GB_SAVING)
    save_bgpixmap (data, property, filename);
  else
    property_set_bgpixmap (property, value, filename);
}


void
gb_widget_output_dialog (GbWidgetGetArgData *data,
			 const gchar *property,
			 const gchar *string,
			 gconstpointer value)
{
  if (data->action == GB_SAVING)
    save_dialog (data, property, value);
  else
    property_set_dialog (property, string, value);
}


/* FIXME: I think this is broken. It should save it relative to the XML file.*/
void
gb_widget_output_filename (GbWidgetGetArgData *data,
			   const gchar *property,
			   const gchar *value)
{
  if (data->action == GB_SAVING)
    save_filename (data, property, value);
  else
    property_set_filename (property, value);
}


void
gb_widget_output_pixmap_filename (GbWidgetGetArgData *data,
				  const gchar *property,
				  const gchar *value)
{
  if (data->action == GB_SAVING)
    save_pixmap_filename (data, property, value);
  else
    property_set_filename (property, value);
}


void
gb_widget_output_font (GbWidgetGetArgData *data,
		       const gchar *property,
		       GdkFont *value,
		       const gchar *xlfd_fontname)
{
  if (data->action == GB_SAVING)
    save_font (data, property, xlfd_fontname);
  else
    property_set_font (property, value, xlfd_fontname);
}


void
gb_widget_output_stock_item (GbWidgetGetArgData *data,
			     const gchar *property,
			     const gchar *value)
{
  if (data->action == GB_SAVING)
    save_string (data, property, value);
  else
    property_set_stock_item (property, value);
}


void
gb_widget_output_icon (GbWidgetGetArgData *data,
		       const gchar *property,
		       const gchar *value)
{
  if (data->action == GB_SAVING)
    save_icon (data, property, value);
  else
    property_set_icon (property, value);
}

void
gb_widget_output_named_icon (GbWidgetGetArgData *data,
			     const gchar *property,
			     const gchar *value)
{
  if (data->action == GB_SAVING)
    save_string (data, property, value);
  else
    property_set_named_icon (property, value);
}

void
gb_widget_output_enum (GbWidgetGetArgData        *data,
		       GType                      enum_type,
		       int                       *values,
		       int                        n_values,
		       const gchar               *property,
		       gint                       value)
{
  const char *s;
  int i;

  if (data->action == GB_SAVING)
    {
      s = glade_string_from_enum (enum_type, value);
      save_string (data, property, s);
    }
  else
    {
      for (i = 0; i < n_values; i++)
	if (values[i] == value)
	  break;

      property_set_choice (property, i < n_values ? values[i] : 0);
    }
}