summaryrefslogtreecommitdiff
path: root/src/glade-menu-editor.c
blob: cd0e3615122142e860bb761cfd2d3e27e14666e4 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/*
 *  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.
 */

/*
 * The Menu Editor window, based on initial work by Javier Arriero País
 * and John Looney.  Adapted to glade-3 by Joaquin Cuenca Abela
 */

#include <string.h>
#include <time.h>

#include <gdk/gdkkeysyms.h>

#undef USE_GNOME

#include "glade.h"
#include "glade-menu-editor.h"
#include "glade-keys-dialog.h"
#include "glade-project.h"
#include "glade-utils.h"
#include "glade-debug.h"

#define GladeMenuEditorIndexKey "glade-menu-editor-index-key"
#define GladeMenuEditorStockIDKey "glade-menu-editor-stock-id"

/* How many pixels to indent levels of the menu hierarchy in the clist. */
#define GLADE_MENU_EDITOR_INDENT 10

/* The text to display if the item is a separator. */
#define GLADE_MENU_EDITOR_SEPARATOR "---"

enum {
	LABEL_COLUMN = 0,
	TYPE_COLUMN,
	ACCEL_COLUMN,
	NAME_COLUMN,
	HANDLER_COLUMN,
	ACTIVE_COLUMN,
	GROUP_COLUMN,
	ICON_COLUMN,
	N_COLUMNS
};

typedef enum {
	GB_MENU_ITEM_NORMAL,
	GB_MENU_ITEM_CHECK,
	GB_MENU_ITEM_RADIO
} GbMenuItemType;

/* This holds information on one menu item. */
typedef struct _GbMenuItemData GbMenuItemData;
struct _GbMenuItemData
{
	gint stock_item_index;	/* Stock menu item index, 0 == None. */
	gchar *label;		/* Text to display. */
	gchar *name;		/* Widget name. */
	gchar *handler;		/* Handler to call when selected. */
	time_t last_mod_time;	/* The time the handler was last updated. */
	gchar *icon;		/* Icon filename, or Stock icon name. */
	gchar *tooltip;		/* Tooltip text. */
	GbMenuItemType type;	/* Type - normal/check/radio. */
	gboolean active;	/* If the item is initially active. */
	GbMenuItemData *group;	/* Points to the item data of the first widget
				   in the radio group, or NULL if it is in its
				   own group or is the first item in group. */
	guint8 modifiers;	/* Control/Shift/Alt flags. */
	gchar *key;		/* Name of accelerator key. */

	gint level;		/* Level in menu hierarchy. */
	gboolean generate_name;	/* If the name should be auto-generated. */
	gboolean generate_handler;	/* If the handler should be auto-generated. */

#ifdef OLD
	GladeWidgetData *wdata;	/* The widget data associated with the old
				   menu item, if there was one. */
#endif
};


static void set_entry_text (GtkEntry *entry, const gchar *text);
static void glade_menu_editor_reset (GladeMenuEditor *menued);


static void glade_menu_editor_class_init (GladeMenuEditorClass *class);
static void glade_menu_editor_init (GladeMenuEditor *dialog);
static void glade_menu_editor_destroy (GtkObject *object);

static GtkWindowClass *parent_class = NULL;

/**
 * glade_menu_editor_get_type:
 *
 * Creates the typecode for the #GladeMenuEditor object type.
 *
 * Returns: the typecode for the #GladeMenuEditor object type
 */
GType
glade_menu_editor_get_type (void)
{
	static GType type = 0;

	if (!type) {
		static const GTypeInfo info = {
			sizeof (GladeMenuEditorClass),
			(GBaseInitFunc) NULL,
			(GBaseFinalizeFunc) NULL,
			(GClassInitFunc) glade_menu_editor_class_init,
			(GClassFinalizeFunc) NULL,
			NULL,
			sizeof (GladeMenuEditor),
			0,
			(GInstanceInitFunc) glade_menu_editor_init,
		};

		type = g_type_register_static (GTK_TYPE_WINDOW, "GladeMenuEditor", &info, 0);
	}

	return type;
}

static void
glade_menu_editor_class_init (GladeMenuEditorClass *class)
{
	GtkWidgetClass *widget_class;
	GtkObjectClass *object_class;

	widget_class = GTK_WIDGET_CLASS (class);
	object_class = GTK_OBJECT_CLASS (class);

	parent_class = g_type_class_peek_parent (class);

	object_class->destroy = glade_menu_editor_destroy;
}

static void
glade_menu_editor_init (GladeMenuEditor * menued)
{
	menued->keys_dialog = NULL;
	menued->project = NULL;
	menued->menu = NULL;
	menued->updating_widgets = FALSE;
	menued->gnome_support = FALSE;
}

static void
glade_menu_editor_destroy (GtkObject *object)
{
	GladeMenuEditor *menued;
	GSList *elem;

	g_return_if_fail (object != NULL);
	g_return_if_fail (GLADE_IS_MENU_EDITOR (object));

	menued = GLADE_MENU_EDITOR (object);

	/* Free all the GbMenuItemData elements & disconnect our destroy handler
	   on the menu widget. */
	glade_menu_editor_reset (menued);

	if (menued->keys_dialog) {
		gtk_widget_destroy (menued->keys_dialog);
		menued->keys_dialog = NULL;
	}

	for (elem = menued->stock_items; elem; elem = elem->next)
		g_free (elem->data);
	g_slist_free (menued->stock_items);
	menued->stock_items = NULL;

#warning "This should probably chain up to parent's destroy."
}

/**
 * This returns the index of the currently selected row in the clist, or -1
 * if no item is currently selected.
 */
static gint
get_selected_row (GladeMenuEditor *menued)
{
	if (GTK_CLIST (menued->clist)->selection == NULL)
		return -1;
	return GPOINTER_TO_INT (GTK_CLIST (menued->clist)->selection->data);
}

/**
 * This returns the currently selected item, or NULL if no item is 
 * currently selected.
 */
static GbMenuItemData*
get_selected_item (GladeMenuEditor * menued)
{
	GbMenuItemData *item;
	gint row;

	row = get_selected_row (menued);
	if (row == -1)
		return NULL;
	item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);
	return item;
}

/**
 * This set the sensitivity of the buttons according to the current state.
 */
static void
set_interface_state (GladeMenuEditor * menued)
{
	GbMenuItemData *item, *tmp_item;
	GtkCList *clist;
	gboolean up_button_sens = FALSE, down_button_sens = FALSE;
	gboolean left_button_sens = FALSE, right_button_sens = FALSE;
	gboolean add_button_sens = FALSE, delete_button_sens = FALSE;
	gboolean state_sens = FALSE, group_sens = FALSE;
	gboolean form_sens = FALSE, type_sens = FALSE, accel_sens = FALSE;
	gboolean label_sens = FALSE, icon_sens = FALSE;
	gint index;

	clist = GTK_CLIST (menued->clist);

	/* Figure out which of the arrow buttons should be sensitive. */

	/* The Add button is always sensitive, since empty labels are separators. */
	add_button_sens = TRUE;

	/* The Delete button and the entire form are sensitive if an item is
	   selected in the clist. */
	index = get_selected_row (menued);
	if (index != -1) {
		form_sens = TRUE;
		type_sens = TRUE;
		label_sens = TRUE;
		icon_sens = TRUE;
		delete_button_sens = TRUE;

		if (index > 0)
			up_button_sens = TRUE;
		if (index < clist->rows - 1)
			down_button_sens = TRUE;

		item = (GbMenuItemData *) gtk_clist_get_row_data (clist, index);
		if (item->level > 0)
			left_button_sens = TRUE;

		/* The accelerator modifier and key are sensitive if this is not a
		   toplevel item on a menubar. */
		if (!GTK_IS_MENU_BAR (menued->menu) || item->level != 0)
			accel_sens = TRUE;

		if (index > 0) {
			tmp_item = (GbMenuItemData *) gtk_clist_get_row_data (clist,
									      index - 1);
			if (tmp_item->level >= item->level)
				right_button_sens = TRUE;
		}

		/* Figure out if the radio group widgets should be sensitive. */
		if (GTK_TOGGLE_BUTTON (menued->radio_radiobutton)->active) {
			group_sens = TRUE;
			state_sens = TRUE;
			icon_sens = FALSE;
		}

		if (GTK_TOGGLE_BUTTON (menued->check_radiobutton)->active) {
			state_sens = TRUE;
			icon_sens = FALSE;
		}

		if (item->stock_item_index) {
			/* For the 'New' menu item, a label and tooltip must be provided. */
			if (menued->gnome_support) {
#ifdef USE_GNOME
				if (item->stock_item_index != GladeStockMenuItemNew)
					label_sens = FALSE;
#endif
			} else {
				/* We don't allow the "New" label to be changed for GTK+ stock
				   items now, to be compatable with libglade. It did make it a
				   bit too complicated anyway. */
#if 0
				const char *stock_id = g_slist_nth_data (menued->stock_items,
									 item->stock_item_index - 1);
				if (strcmp (stock_id, GTK_STOCK_NEW))
#endif
					label_sens = FALSE;
			}

			icon_sens = FALSE;
			type_sens = FALSE;
			accel_sens = FALSE;
		}
	}

	/* Now set the sensitivity of the widgets. */
	gtk_widget_set_sensitive (menued->stock_label, form_sens);
	gtk_widget_set_sensitive (menued->stock_combo, form_sens);

	gtk_widget_set_sensitive (menued->icon_label, icon_sens);
	gtk_widget_set_sensitive (menued->icon_widget, icon_sens);
	gtk_widget_set_sensitive (menued->icon_button, icon_sens);

	gtk_widget_set_sensitive (menued->name_label, form_sens);
	gtk_widget_set_sensitive (menued->name_entry, form_sens);
	gtk_widget_set_sensitive (menued->handler_label, form_sens);
	gtk_widget_set_sensitive (menued->handler_entry, form_sens);

	gtk_widget_set_sensitive (menued->label_label, label_sens);
	gtk_widget_set_sensitive (menued->label_entry, label_sens);
	gtk_widget_set_sensitive (menued->tooltip_label, label_sens);
	gtk_widget_set_sensitive (menued->tooltip_entry, label_sens);

	gtk_widget_set_sensitive (menued->add_button, add_button_sens);
	gtk_widget_set_sensitive (menued->add_separator_button, add_button_sens);
	gtk_widget_set_sensitive (menued->delete_button, delete_button_sens);

	gtk_widget_set_sensitive (menued->type_frame, type_sens);
	gtk_widget_set_sensitive (menued->state_label, state_sens);
	gtk_widget_set_sensitive (menued->state_togglebutton, state_sens);
	gtk_widget_set_sensitive (menued->group_label, group_sens);
	gtk_widget_set_sensitive (menued->group_combo, group_sens);

	gtk_widget_set_sensitive (menued->accel_frame, accel_sens);
}

/**
 * This clears the form, ready to add a new item. If full is TRUE it resets
 * the type checkbuttons, group and accelerator modifiers. When adding items
 * full is set to FALSE so the user can add several items of the same type.
 */
static void
clear_form (GladeMenuEditor *menued, gboolean full)
{
	gtk_list_select_item (GTK_LIST (GTK_COMBO (menued->stock_combo)->list), 0);
	set_entry_text (GTK_ENTRY (GTK_COMBO (menued->icon_widget)->entry), "");
	set_entry_text (GTK_ENTRY (menued->label_entry), "");
	set_entry_text (GTK_ENTRY (menued->name_entry), "");
	set_entry_text (GTK_ENTRY (menued->handler_entry), "");
	set_entry_text (GTK_ENTRY (menued->tooltip_entry), "");
	set_entry_text (GTK_ENTRY (menued->accel_key_entry), "");

	if (full) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->normal_radiobutton), TRUE);
		set_entry_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry), "");
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_ctrl_checkbutton), FALSE);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_shift_checkbutton), FALSE);
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_alt_checkbutton), FALSE);
	}
}

/* Make sure all item group fields point to the first item in the group. */
static void
normalize_radio_groups (GladeMenuEditor *menued)
{
	GbMenuItemData *item, *group;
	GList *groups = NULL;
	gint rows, row;

	/* Step through each row checking each radio item. */
	rows = GTK_CLIST (menued->clist)->rows;
	for (row = 0; row < rows; row++) {
		item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);
		if (item->type != GB_MENU_ITEM_RADIO)
			continue;

		/* Follow the chain of groups until we get the real one. */
		group = item->group;
		while (group && group->group && group->group != group)
			group = group->group;

		/* If it is a new group, add it to the list. */
		if (!group || group == item) {
			groups = g_list_prepend (groups, item);
			item->group = NULL;
		}
		/* Else check if the group item is after the current one. If it is,
		   then we make the current item the group leader. We assume that if
		   we haven't seen the group item yet, it must be after this one. */
		else if (!g_list_find (groups, group)) {
			groups = g_list_prepend (groups, item);
			group->group = item;
			item->group = NULL;
		} else {
			item->group = group;
		}
	}

	g_list_free (groups);
}

/* This recreates the list of available radio groups, and puts them in the
   combo's drop-down list. */
static void
update_radio_groups (GladeMenuEditor * menued)
{
	GbMenuItemData *item, *tmp_item;
	GList *groups = NULL, *elem;
	gint item_row, row, parent_row, rows;
	GtkCombo *combo;
	GtkList *list;
	GtkWidget *li;

	/* Make sure all item group fields point to the first item in the group. */
	normalize_radio_groups (menued);

	item_row = get_selected_row (menued);
	if (item_row == -1) {
		gtk_list_clear_items (GTK_LIST (GTK_COMBO (menued->group_combo)->list),
				      0, -1);
		return;
	}

	item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), item_row);

	/* Step backwards to find the parent item. */
	parent_row = -1;
	for (row = item_row - 1; row > 0; row--) {
		tmp_item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);
		if (tmp_item->level < item->level) {
			parent_row = row;
			break;
		}
	}

	/* Now step through the items, checking all items that are on the same
	   level as the current one, until we reach the end of the list or find an
	   item on a higher level. */
	rows = GTK_CLIST (menued->clist)->rows;
	for (row = parent_row + 1; row < rows; row++) {
		tmp_item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);
		if (tmp_item->level < item->level)
			break;

		if (tmp_item->level == item->level
		    && tmp_item->type == GB_MENU_ITEM_RADIO
		    && tmp_item->name && tmp_item->name[0]) {
			/* If the item has its group set to NULL or itself, then it is a new
			   group, so add its name. */
			if (!tmp_item->group || tmp_item->group == tmp_item)
				groups = g_list_prepend (groups, tmp_item->name);
		}
	}

	groups = g_list_sort (groups, (GCompareFunc)strcmp);

	combo = GTK_COMBO (menued->group_combo);
	list = GTK_LIST (combo->list);

	/* We have to block the combo's list's selection changed signal, or it
	   causes problems. */
	gtk_signal_handler_block (GTK_OBJECT (list),
				  GTK_COMBO (menued->group_combo)->list_change_id);

	gtk_list_clear_items (list, 0, -1);

	/* Add the special 'New' item to create a new group. */
	li = gtk_list_item_new_with_label (_("New"));
	gtk_widget_show (li);
	gtk_container_add (GTK_CONTAINER (list), li);
	gtk_combo_set_item_string (combo, GTK_ITEM (li), "");

	/* Add a separator. */
	li = gtk_list_item_new ();
	gtk_widget_show (li);
	gtk_container_add (GTK_CONTAINER (list), li);

	for (elem = groups; elem; elem = elem->next) {
		li = gtk_list_item_new_with_label (elem->data);
		gtk_widget_show (li);
		gtk_container_add (GTK_CONTAINER (list), li);
	}

	gtk_signal_handler_unblock (GTK_OBJECT (list),
				    GTK_COMBO (menued->group_combo)->list_change_id);
	g_list_free (groups);
}

/* This finds the group item to use for the given radiomenuitem widget.
   It searches the list of group items to find the first one that is also in
   the radiomenuitem's group list. If none is found, it creates a new group
   and adds it to the list. */
static GbMenuItemData*
find_radio_group (GtkRadioMenuItem *menuitem,
		  GList **groups,
		  GbMenuItemData *item)
{
	GSList *item_group_list;
	GList *elem;

	item_group_list = menuitem->group;

	/* The groups list contains pairs of GSList + GbMenuItemData*. */
	for (elem = *groups; elem; elem = elem->next->next) {
		GSList *elem_group = elem->data;
		GbMenuItemData *elem_item = elem->next->data;

		if (elem_group == item_group_list)
			return elem_item;
	}

	/* We couldn't find an existing group that matches, so we create a new one.
	 */
	*groups = g_list_prepend (*groups, item);
	*groups = g_list_prepend (*groups, item_group_list);

	return NULL;
}

/* This is called to make sure that no items have the given item as their
   group leader. It is called when an item is removed, or when its type is
   changed from a radio item to something else.

   It steps through the list, checking for radio items with
   the group field set to the given item. The first time it finds one, it
   creates a new group. It sets the group field of all other items in the
   same group to the new first item in the group. */
static void
remove_from_radio_group (GladeMenuEditor * menued,
			 GbMenuItemData *item)
{
	GbMenuItemData *new_group_item = NULL;
	gint rows, row;

	rows = GTK_CLIST (menued->clist)->rows;
	for (row = 0; row < rows; row++) {
		GbMenuItemData *tmp_item;
		tmp_item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);

		if (tmp_item->type == GB_MENU_ITEM_RADIO
		    && tmp_item->group == item && tmp_item != item) {
			if (new_group_item) {
				tmp_item->group = new_group_item;
			}
			else {
				tmp_item->group = NULL;
				new_group_item = tmp_item;
			}
		}
	}
}

static gchar *
get_stock_item_label (GladeMenuEditor *menued, gint stock_item_index)
{
	if (menued->gnome_support) {
#ifdef USE_GNOME
		/* Most of the label text is from Gnome, but 2 of them are ours,
		   so we use a utility function to find the translation. */
		return glade_gnome_gettext (GladeStockMenuItemValues[stock_item_index].label);
#else
		/* This shouldn't happen. */
		g_warning ("Trying to use GNOME stock items in GTK+ version of Glade");
		return NULL;
#endif
	} else {
		gchar *stock_id;
		GtkStockItem item;

		if (stock_item_index <= 0)
			return _("None");

		stock_id = g_slist_nth_data (menued->stock_items, stock_item_index - 1);
		gtk_stock_lookup (stock_id, &item);

		return item.label;
	}
}

/* This gets a string representing the accelerator key + modifiers.
   It returns a pointer to a static buffer. */
static gchar *
get_accel_string (gchar * key, guint8 modifiers)
{
	static gchar buffer[32];

	buffer[0] = '\0';
	if (modifiers & GDK_CONTROL_MASK)
		strcat (buffer, "C+");
	if (modifiers & GDK_SHIFT_MASK)
		strcat (buffer, "S+");
	if (modifiers & GDK_MOD1_MASK)
		strcat (buffer, "A+");
	if (key)
		strcat (buffer, key);
	return buffer;
}

/* This returns the default name of the widget, given its label. The returned
   string should be freed at some point. */
static gchar*
generate_name (GladeMenuEditor *menued,
	       gchar *label)
{
	gchar *prefix, *name, *src, *dest;

	/* For empty labels, i.e. separators, use 'separator'. */
	if (label == NULL || label[0] == '\0') {
		return glade_project_new_widget_name (menued->project, "separator");
	}

	prefix = g_malloc (strlen (label) + 1);
	/* Convert spaces to underscores, and ignore periods (e.g. in "Open...")
	   and underscores (e.g. in "_Open"). */
	for (src = label, dest = prefix; *src; src++) {
		if (*src == ' ')
			*dest++ = '_';
		else if (*src == '.')
			continue;
		else if (*src == '_')
			continue;
		else
			*dest++ = *src;
	}
	*dest = '\0';

	if (dest >= prefix + strlen (label) + 1)
		g_warning ("Buffer overflow");

	/* Get rid of any trailing digits. */
	dest--;
	while (*dest >= '0' && *dest <= '9') {
		*dest = '\0';
		dest--;
	}

	name = glade_project_new_widget_name (menued->project, prefix);
	g_free (prefix);

	return name;
}

/* This returns TRUE id the item in the given row is a parent, or FALSE
   if the row doesn't exist or isn't a parent. */
static gboolean
is_parent (GladeMenuEditor *menued,
	   gint row)
{
	GtkCList *clist;
	GbMenuItemData *item, *next_item;

	clist = GTK_CLIST (menued->clist);
	if (row < 0 || row >= clist->rows - 1)
		return FALSE;
	item = (GbMenuItemData *) gtk_clist_get_row_data (clist, row);
	next_item = (GbMenuItemData *) gtk_clist_get_row_data (clist, row + 1);
	if (next_item->level > item->level)
		return TRUE;
	return FALSE;
}

/* This returns the default 'activate' handler name, given the name of the
   item. The returned string should be freed at some point. */
static gchar*
generate_handler (GladeMenuEditor *menued, gint row, gchar *label, gchar *name)
{
	gchar *handler, *start = "on_", *end = "_activate";

	/* For empty labels, i.e. separators, and items with submenus, there is no
	   handler by default. */
	if (label == NULL || label[0] == '\0' || is_parent (menued, row))
		return NULL;

	handler = g_malloc (strlen (name) + strlen (start) + strlen (end) + 1);
	strcpy (handler, start);
	strcat (handler, name);
	strcat (handler, end);

	return handler;
}

/**
 * This shows the properties of the item currently selected in the clist.
 */
static void
show_item_properties (GladeMenuEditor *menued)
{
	GbMenuItemData *item;
	GtkWidget *clist;

	clist = menued->clist;
	item = get_selected_item (menued);
	if (item == NULL)
		return;

	menued->updating_widgets = TRUE;

	/* Now set them to the item's properties. */
	set_entry_text (GTK_ENTRY (GTK_COMBO (menued->stock_combo)->entry),
			get_stock_item_label (menued, item->stock_item_index));
#if 0
	g_print ("Setting label_entry to: %s\n", item->label);
#endif
	set_entry_text (GTK_ENTRY (menued->label_entry), item->label);
#if 0
	g_print ("Setting name_entry to: %s\n", item->name);
#endif
	set_entry_text (GTK_ENTRY (menued->name_entry), item->name);
#if 0
	g_print ("Setting handler_entry to: %s\n", item->handler);
#endif
	set_entry_text (GTK_ENTRY (menued->handler_entry), item->handler);
	set_entry_text (GTK_ENTRY (GTK_COMBO (menued->icon_widget)->entry),
			item->icon);
	set_entry_text (GTK_ENTRY (menued->tooltip_entry), item->tooltip);

	if (item->type == GB_MENU_ITEM_NORMAL) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->normal_radiobutton), TRUE);
		set_entry_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry), "");
	} else if (item->type == GB_MENU_ITEM_CHECK) {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->check_radiobutton), TRUE);
		set_entry_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry), "");
	} else {
		gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->radio_radiobutton), TRUE);
		set_entry_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry),
				item->group ? item->group->name : item->name);
	}

	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->state_togglebutton), item->active);

	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_ctrl_checkbutton), (item->modifiers & GDK_CONTROL_MASK) ? TRUE : FALSE);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_shift_checkbutton), (item->modifiers & GDK_SHIFT_MASK) ? TRUE : FALSE);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_alt_checkbutton), (item->modifiers & GDK_MOD1_MASK) ? TRUE : FALSE);
	set_entry_text (GTK_ENTRY (menued->accel_key_entry),
			item->key ? item->key : "");

	update_radio_groups (menued);

	menued->updating_widgets = FALSE;
}

/* This returns a copy of the given property string, or NULL if it is an
   empty string. */
static gchar*
copy_item_property (gchar *property)
{
	if (property[0] == '\0')
		return NULL;
	return g_strdup (property);
}

/* This checks if the new value is different to the old, but an empty string
   in new is taken to be equal to NULL as well. */
static gboolean
item_property_changed (gchar *new, gchar *old)
{
	if (old == NULL)
		return (new == NULL || new[0] == '\0') ? FALSE : TRUE;
	if (new == NULL)
		return TRUE;
	if (!strcmp (old, new))
		return FALSE;
	return TRUE;
}

/**
 * This makes sure the default handlers are updated as items are moved around.
 */
static void
check_generated_handlers (GladeMenuEditor *menued)
{
	GtkCList *clist;
	GbMenuItemData *item;
	gint row;
	gchar *handler;

	clist = GTK_CLIST (menued->clist);
	for (row = 0; row < clist->rows; row++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (clist, row);
		if (item->generate_handler) {
			handler = generate_handler (menued, row, item->label, item->name);
			if (item_property_changed (handler, item->handler)) {
				g_free (item->handler);
				item->handler = handler;
				gtk_clist_set_text (clist, row, HANDLER_COLUMN, handler);
			}
			else {
				g_free (handler);
			}
		}
	}
}

static GbMenuItemData *
find_group_item (GladeMenuEditor *menued, char *group_name)
{
	gint rows, row;

	if (!group_name || !group_name[0])
		return NULL;

	rows = GTK_CLIST (menued->clist)->rows;

	for (row = 0; row < rows; row++) {
		GbMenuItemData *item;
		item = gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);

		if (item->name && !strcmp (item->name, group_name))
			return item;
	}

	return NULL;
}

/* This updates the currently selected item in the clist, updating each field
   if it is different to the settings in the form elements. */
static void
update_current_item (GladeMenuEditor * menued)
{
	GbMenuItemData *item, *group_item;
	GtkCList *clist;
	gchar *name, *label, *handler, *tooltip, *group_name, *key;
	gchar *icon;
	GbMenuItemType type;
	gint row;
	guint8 modifiers;
	gboolean active, update_accelerator = FALSE;

	clist = GTK_CLIST (menued->clist);
	row = get_selected_row (menued);
	if (row == -1)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (menued->clist),
							  row);

	name = (gchar*) gtk_entry_get_text (GTK_ENTRY (menued->name_entry));
	if (item_property_changed (name, item->name)) {
		g_free (item->name);
		item->name = copy_item_property (name);
		gtk_clist_set_text (clist, row, NAME_COLUMN,
				    item->name ? item->name : "");
	}

	label = (gchar*) gtk_entry_get_text (GTK_ENTRY (menued->label_entry));
	if (item_property_changed (label, item->label)) {
		g_free (item->label);
		item->label = copy_item_property (label);
		gtk_clist_set_text (clist, row, LABEL_COLUMN,
				    item->label ? item->label : GLADE_MENU_EDITOR_SEPARATOR);
	}

	handler = (gchar*) gtk_entry_get_text (GTK_ENTRY (menued->handler_entry));
	if (item_property_changed (handler, item->handler)) {
		g_free (item->handler);
		item->handler = copy_item_property (handler);
		gtk_clist_set_text (clist, row, HANDLER_COLUMN,
				    item->handler ? item->handler : "");

		/* This is a flag to indicate that the last_mod_time should be set when
		   the 'Apply' button is clicked. */
		item->last_mod_time = (time_t) -2;
	}

	icon = (gchar*) gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (menued->icon_widget)->entry));
	if (item_property_changed (icon, item->icon)) {
		g_free (item->icon);
		item->icon = copy_item_property (icon);
		gtk_clist_set_text (clist, row, ICON_COLUMN,
				    item->icon ? item->icon : "");
	}

	tooltip = (gchar*) gtk_entry_get_text (GTK_ENTRY (menued->tooltip_entry));
	if (item_property_changed (tooltip, item->tooltip)) {
		g_free (item->tooltip);
		item->tooltip = copy_item_property (tooltip);
	}

	if (GTK_TOGGLE_BUTTON (menued->normal_radiobutton)->active)
		type = GB_MENU_ITEM_NORMAL;
	else if (GTK_TOGGLE_BUTTON (menued->check_radiobutton)->active)
		type = GB_MENU_ITEM_CHECK;
	else
		type = GB_MENU_ITEM_RADIO;
	if (item->type != type) {
		/* If the item is changing from a radio item to something else, make
		   sure other items in the same group no longer point to it. */
		if (item->type == GB_MENU_ITEM_RADIO)
			remove_from_radio_group (menued, item);

		item->type = type;
		if (type == GB_MENU_ITEM_NORMAL)
			gtk_clist_set_text (clist, row, TYPE_COLUMN, "");
		else if (type == GB_MENU_ITEM_CHECK)
			gtk_clist_set_text (clist, row, TYPE_COLUMN, _("Check"));
		else if (type == GB_MENU_ITEM_RADIO)
			gtk_clist_set_text (clist, row, TYPE_COLUMN, _("Radio"));
	}

	active = GTK_TOGGLE_BUTTON (menued->state_togglebutton)->active	? TRUE : FALSE;
	if (active != item->active) {
		item->active = active;
		gtk_clist_set_text (clist, row, ACTIVE_COLUMN, active ? _("Yes") : "");
	}

	group_name = (gchar*) gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry));
	group_item = find_group_item (menued, group_name);
	if (group_item != item->group) {
		char *group_text = group_item ? group_item->name : item->name;
		if (item->type != GB_MENU_ITEM_RADIO)
			group_text = NULL;
		item->group = group_item;
		gtk_clist_set_text (clist, row, GROUP_COLUMN,
				    group_text ? group_text : "");
	}

	key = (gchar*) gtk_entry_get_text (GTK_ENTRY (menued->accel_key_entry));
	if (item_property_changed (key, item->key)) {
		g_free (item->key);
		item->key = copy_item_property (key);
		update_accelerator = TRUE;
	}

	modifiers = 0;
	if (GTK_TOGGLE_BUTTON (menued->accel_ctrl_checkbutton)->active)
		modifiers |= GDK_CONTROL_MASK;
	if (GTK_TOGGLE_BUTTON (menued->accel_shift_checkbutton)->active)
		modifiers |= GDK_SHIFT_MASK;
	if (GTK_TOGGLE_BUTTON (menued->accel_alt_checkbutton)->active)
		modifiers |= GDK_MOD1_MASK;
	if (modifiers != item->modifiers) {
		item->modifiers = modifiers;
		update_accelerator = TRUE;
	}

	if (update_accelerator)
		gtk_clist_set_text (clist, row, ACCEL_COLUMN,
				    get_accel_string (item->key, item->modifiers));

	set_interface_state (menued);
}

/* This makes sure the given row is visible. */
static void
ensure_visible (GtkWidget *clist, gint row)
{
	if (gtk_clist_row_is_visible (GTK_CLIST (clist), row) != GTK_VISIBILITY_FULL)
		gtk_clist_moveto(GTK_CLIST(clist), row, -1, 0.5, 0);
}

/**
 * This adds the item to the clist at the given position.
 */
static void
insert_item (GtkCList *clist, GbMenuItemData *item, gint row)
{
	gchar *rowdata[N_COLUMNS];

	/* Empty labels are understood to be separators. */
	if (item->label && strlen (item->label) > 0)
		rowdata[LABEL_COLUMN] = item->label;
	else
		rowdata[LABEL_COLUMN] = GLADE_MENU_EDITOR_SEPARATOR;
	if (item->type == GB_MENU_ITEM_NORMAL)
		rowdata[TYPE_COLUMN] = "";
	else if (item->type == GB_MENU_ITEM_CHECK)
		rowdata[TYPE_COLUMN] = _("Check");
	else if (item->type == GB_MENU_ITEM_RADIO)
		rowdata[TYPE_COLUMN] = _("Radio");
	rowdata[ACCEL_COLUMN] = get_accel_string (item->key, item->modifiers);
	rowdata[NAME_COLUMN] = item->name ? item->name : "";
	rowdata[HANDLER_COLUMN] = item->handler ? item->handler : "";
	rowdata[ICON_COLUMN] = item->icon ? item->icon : "";
	rowdata[ACTIVE_COLUMN] = item->active ? _("Yes") : "";
	rowdata[GROUP_COLUMN] = item->group ? item->group->name : item->name;
	if (item->type != GB_MENU_ITEM_RADIO || !rowdata[GROUP_COLUMN])
		rowdata[GROUP_COLUMN] = "";

	if (row >= 0)
		gtk_clist_insert (clist, row, rowdata);
	else
		row = gtk_clist_append (GTK_CLIST (clist), rowdata);

	gtk_clist_set_row_data (GTK_CLIST (clist), row, item);
	gtk_clist_set_shift (GTK_CLIST (clist), row, 0, 0, item->level * GLADE_MENU_EDITOR_INDENT);
}

/**
 * This inserts the given list of items at the given position in the clist.
 */
static void
insert_items (GtkWidget *clist, GList *items, gint row)
{
	GbMenuItemData *item;

	for (; items; items = items->next) {
		item = (GbMenuItemData *) items->data;
		insert_item (GTK_CLIST (clist), item, row++);
	}
}

/**
 * This adds a new menuitem. If separator is FALSE, it adds a normal item
 * with the label 'New Item'. If separator is TRUE it adds a separator.
 * It is added to the clist beneath the currently selected item, or at the
 * end of the list if no item is selected. If as_child is TRUE it adds the
 * item as a child of the selected item, else it adds it as a sibling.
 */
static void
add_item (GladeMenuEditor *menued, gboolean as_child, gboolean separator)
{
	GbMenuItemData *item, *selected_item;
	GtkWidget *clist;
	gint row;

	item = g_new0 (GbMenuItemData, 1);
	item->stock_item_index = 0;

	if (separator) {
		item->label = NULL;
		item->name = glade_project_new_widget_name (menued->project, "separator");
	} else {
		item->label = glade_project_new_widget_name (menued->project, "item");
		item->name = g_strdup (item->label);
	}

	item->handler = generate_handler (menued, -1, item->label, item->name);
	/* This is a flag to indicate that the last_mod_time should be set when
	   the 'Apply' button is clicked. */
	item->last_mod_time = (time_t) -2;
	item->icon = NULL;
	item->tooltip = NULL;
	item->type = GB_MENU_ITEM_NORMAL;
	item->active = FALSE;
	item->group = NULL;
	item->modifiers = 0;
	item->key = NULL;
	item->level = 0;
	item->generate_name = TRUE;
	item->generate_handler = TRUE;

	clist = menued->clist;
	row = get_selected_row (menued);
	if (row != -1) {
		selected_item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
		item->level = selected_item->level + (as_child ? 1 : 0);
		insert_item (GTK_CLIST (clist), item, row + 1);
		gtk_clist_select_row (GTK_CLIST (clist), row + 1, 0);
		ensure_visible (clist, row + 1);
	} else {
		item->level = 0;
		insert_item (GTK_CLIST (clist), item, -1);
		gtk_clist_select_row (GTK_CLIST (clist), GTK_CLIST (clist)->rows - 1, 0);
		ensure_visible (clist, GTK_CLIST (clist)->rows - 1);
	}

	set_interface_state (menued);
	gtk_widget_grab_focus (menued->label_entry);
	gtk_editable_select_region (GTK_EDITABLE (menued->label_entry), 0, -1);
}

/**
 * This removes an item and its children from the clist, and returns
 * a list of the removed items.
 */
static GList *
remove_item_and_children (GtkWidget *clist, gint row)
{
	GList *items = NULL;
	GbMenuItemData *item;
	gint level;

	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	level = item->level;
	items = g_list_append (items, item);
	gtk_clist_remove (GTK_CLIST (clist), row);

	while (row < GTK_CLIST (clist)->rows) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
		if (item->level > level) {
			items = g_list_append (items, item);
			gtk_clist_remove (GTK_CLIST (clist), row);
		}
		else
			break;
	}
	return items;
}

static void
glade_menu_editor_free_item (GbMenuItemData *item)
{
#if 0
	g_free (item->name);
	g_free (item->label);
	g_free (item->handler);
	g_free (item->icon);
	g_free (item->tooltip);
	g_free (item->key);

	if (item->wdata)
		glade_widget_data_free (item->wdata);

	g_free (item);
#endif
}


/**************************************************************************
 * Signal Handlers
 **************************************************************************/

static void
on_clist_select_row (GtkWidget * clist,
		     gint row,
		     gint column,
		     GdkEventButton * event,
		     gpointer user_data)
{
	GladeMenuEditor *menued;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (clist));

	show_item_properties (menued);

	if (event && !GTK_WIDGET_HAS_FOCUS (clist))
		gtk_widget_grab_focus (clist);

	set_interface_state (menued);
}

static void
on_clist_unselect_row (GtkWidget * clist,
		       gint row,
		       gint column,
		       GdkEventButton * event,
		       gpointer user_data)
{
	GladeMenuEditor *menued;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (clist));

	clear_form (menued, FALSE);

	if (event && !GTK_WIDGET_HAS_FOCUS (clist))
		gtk_widget_grab_focus (clist);

	set_interface_state (menued);
}

/* This will only call update_current_item if the text is different to the
   corresponding item field, since we don't want to propogate updates when
   we are setting the entry. */
static void
on_entry_changed (GtkWidget *entry, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkCList *clist;
	GbMenuItemData *item;
	gchar *text, *item_text;
	gboolean changed = FALSE;
	gint row;

	g_debug(("In on_entry_changed\n"));

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (entry));

	/* If we are setting the widget values, just return. */
	if (menued->updating_widgets)
		return;

	clist = GTK_CLIST (menued->clist);
	row = get_selected_row (menued);

	if (row == -1)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (menued->clist), row);

	/* put the new text on the "text" variable */
	text = (gchar*) gtk_entry_get_text (GTK_ENTRY (entry));

	/* put the old text on the "item_text" variable */
	if (entry == menued->label_entry) {
		item_text = item->label;
	} else if (entry == menued->name_entry) {    
		item_text = item->name;
	} else if (entry == menued->handler_entry) {
		item_text = item->handler;
	} else if (entry == GTK_COMBO (menued->icon_widget)->entry) {
		/* If the user selects the 'None' item from the combo, we reset the
		   text to "" and return. This callback will be called again. */
		if (!strcmp (text, _("None"))) {
			set_entry_text (GTK_ENTRY (entry), "");
			return;
		}

		item_text = item->icon;
	} else if (entry == menued->tooltip_entry) {
		item_text = item->tooltip;
	} else if (entry == GTK_COMBO (menued->group_combo)->entry) {
		item_text = item->group ? item->group->name : item->name;
	} else if (entry == menued->accel_key_entry) {
		item_text = item->key;
	} else
		return;

	/* if the text has not changed, then just return */
	if (item_text == NULL && *text != 0)
		changed = TRUE;
	else if (item_text && text && strcmp (text, item_text) != 0)
		changed = TRUE;

	if (!changed)
		return;
	
	if (entry == menued->label_entry) {
		if (item->generate_name) {
			/*** glade_project_release_widget_name (menued->project, item->name); ***/
			g_free (item->name);
			item->name = generate_name (menued, text);
			set_entry_text (GTK_ENTRY (menued->name_entry),
					item->name ? item->name : "");
			gtk_clist_set_text (clist, row, NAME_COLUMN,
					    item->name ? item->name : "");
			if (item->generate_handler) {
				g_free (item->handler);
					
				item->handler = generate_handler (menued, row, text,
								  item->name);
				set_entry_text (GTK_ENTRY (menued->handler_entry),
						item->handler ? item->handler : "");
				gtk_clist_set_text (clist, row, HANDLER_COLUMN,
						    item->handler ? item->handler : "");
			}
		}
	} else if (entry == menued->name_entry) {
		item->generate_name = FALSE;
		if (item->generate_handler) {
			g_free (item->handler);
			item->handler = generate_handler (menued, row, item->label,
							  text);
			set_entry_text (GTK_ENTRY (menued->handler_entry),
					item->handler ? item->handler : "");
			gtk_clist_set_text (clist, row, HANDLER_COLUMN,
					    item->handler ? item->handler : "");
		}
	} else if (entry == menued->handler_entry) {
		item->generate_handler = FALSE;
	}

	update_current_item (menued);
	set_interface_state (menued);
}

/* FIXME: This is mostly a temporary hack until GtkEntry is fixed in GTK+
   2.0.x so we don't get "changed" twice when we call gtk_entry_set_text().
   Though we also need this to set it to "" when NULL is passed in. */
static void
set_entry_text (GtkEntry *entry, const gchar *text)
{
	gint tmp_pos;

	GtkEditable *editable;

	g_return_if_fail (GTK_IS_ENTRY (entry));

	if (!text)
		text = "";

	editable = GTK_EDITABLE (entry);
  
	g_signal_handlers_block_by_func (editable, on_entry_changed, NULL);
	gtk_editable_delete_text (GTK_EDITABLE(entry), 0, -1);
	g_signal_handlers_unblock_by_func (editable, on_entry_changed, NULL);

	tmp_pos = 0;
	gtk_editable_insert_text (editable, text, strlen (text), &tmp_pos);
}

/* This will only call update_current_item if the text is different to the
   corresponding item field, since we don't want to propogate updates when
   we are setting the entry. */
static void
on_stock_item_entry_changed (GtkWidget * entry,
			     gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkCList *clist;
	GbMenuItemData *item;
	gint row;
	GtkListItem *listitem;
	gint stock_item_index = 0;
	const gchar *text;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (entry));

	text = gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (menued->stock_combo)->entry));
	/* FIXME GTK+ 1.3.x bug workaround. It emits "changed" twice, once when the
	   existing text is deleted. So we just return if the text is empty. */
	if (text[0] == '\0')
		return;

	clist = GTK_CLIST (menued->clist);
	row = get_selected_row (menued);
	if (row == -1)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (menued->clist),
							  row);

	/* Find the index of the selected item. */
	listitem = glade_util_gtk_combo_find (GTK_COMBO (menued->stock_combo));
	g_return_if_fail (listitem != NULL);

	if (menued->gnome_support) {
#ifdef USE_GNOME
		stock_item_index = g_list_index (GTK_LIST (GTK_COMBO (menued->stock_combo)->list)->children, listitem);
#endif
	}
	else {
		stock_item_index = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (listitem), GladeMenuEditorIndexKey));
	}

	if (item->stock_item_index != stock_item_index) {
		item->stock_item_index = stock_item_index;

		/* If the stock item is reset to 'None', get a new item name, and reset
		   generate_name/handler to TRUE. */
		if (stock_item_index == 0) {
			item->generate_name = TRUE;
			item->generate_handler = TRUE;

			item->label = glade_project_new_widget_name (menued->project, "item");
			item->name = g_strdup (item->label);
			item->handler = generate_handler (menued, row, item->label,
							  item->name);
			show_item_properties (menued);

			gtk_clist_set_text (clist, row, LABEL_COLUMN,
					    item->label ? item->label : GLADE_MENU_EDITOR_SEPARATOR);
			gtk_clist_set_text (clist, row, NAME_COLUMN,
					    item->name ? item->name : "");
			gtk_clist_set_text (clist, row, HANDLER_COLUMN,
					    item->handler ? item->handler : "");
		}
		else {
			/* These will trigger callbacks, and will generate the name
			   and handler if appropriate. */
			set_entry_text (GTK_ENTRY (menued->label_entry),
					gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (menued->stock_combo)->entry)));

			/* Stock menu items are all normal items. */
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->normal_radiobutton), TRUE);
			set_entry_text (GTK_ENTRY (GTK_COMBO (menued->group_combo)->entry), "");

			/* Reset the accelerator keys, as that is handled automatically. */
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_ctrl_checkbutton), FALSE);
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_shift_checkbutton), FALSE);
			gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->accel_alt_checkbutton), FALSE);
			set_entry_text (GTK_ENTRY (menued->accel_key_entry), "");
		}
	}

	set_interface_state (menued);
}

static gboolean
on_label_entry_key_press (GtkWidget *widget,
			  GdkEventKey *event,
			  gpointer user_data)
{
	GladeMenuEditor *menued;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (widget));

	/* If the Return key is pressed, we add a new item beneath the selected item.
	   This makes it very easy to add several menus. If the Control key is
	   pressed, we add the item as a child, otherwise we add it as a sibling. */
	if (event->keyval == GDK_Return) {
		if (event->state & GDK_CONTROL_MASK) {
			add_item (menued, TRUE, FALSE);
			/* Since we are added a child, we may need to set the parent's
			   handler to NULL if it has been auto-generated. */
			check_generated_handlers (menued);
		} else {
			add_item (menued, FALSE, FALSE);
		}
		return TRUE;
	}
	return FALSE;
}

static void
on_radiobutton_toggled (GtkWidget *togglebutton, gpointer user_data)
{
	GladeMenuEditor *menued;
	GbMenuItemData *item;
	GbMenuItemType type = 0;
	gboolean changed = FALSE;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (togglebutton)));
	item = get_selected_item (menued);
	if (item == NULL)
		return;

	if (togglebutton == menued->normal_radiobutton)
		type = GB_MENU_ITEM_NORMAL;
	else if (togglebutton == menued->check_radiobutton)
		type = GB_MENU_ITEM_CHECK;
	else if (togglebutton == menued->radio_radiobutton)
		type = GB_MENU_ITEM_RADIO;

	if (GTK_TOGGLE_BUTTON (togglebutton)->active) {
		if (type != item->type)
			changed = TRUE;
	} else {
		if (type == item->type)
			changed = TRUE;
	}

	if (changed) {
		update_current_item (menued);
		set_interface_state (menued);
	}
}

static void
on_checkbutton_toggled (GtkWidget *togglebutton, gpointer user_data)
{
	GladeMenuEditor *menued;
	GbMenuItemData *item;
	guint active;
	guint8 currently_active = 0;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (togglebutton)));
	active = GTK_TOGGLE_BUTTON (togglebutton)->active;
	item = get_selected_item (menued);
	if (item == NULL)
		return;
	if (togglebutton == menued->accel_ctrl_checkbutton)
		currently_active = item->modifiers & GDK_CONTROL_MASK;
	if (togglebutton == menued->accel_shift_checkbutton)
		currently_active = item->modifiers & GDK_SHIFT_MASK;
	if (togglebutton == menued->accel_alt_checkbutton)
		currently_active = item->modifiers & GDK_MOD1_MASK;

	if ((active && !currently_active) || (!active && currently_active)) {
		update_current_item (menued);
		set_interface_state (menued);
	}
}

static void
on_state_button_toggled (GtkToggleButton *togglebutton, gpointer user_data)
{
	GladeMenuEditor *menued;
	GbMenuItemData *item;
	GtkWidget *label;
	guint active;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (togglebutton)));
	active = GTK_TOGGLE_BUTTON (togglebutton)->active;
	label = GTK_BIN (togglebutton)->child;
	gtk_label_set_text (GTK_LABEL (label), active ? _("Yes") : _("No"));

	item = get_selected_item (menued);
	if (item == NULL)
		return;
	if ((item->active && !active) || (!item->active && active))
		update_current_item (menued);
}

static void
on_accel_key_button_clicked (GtkButton *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	gint response;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));

	if (menued->keys_dialog == NULL) {
		menued->keys_dialog = glade_keys_dialog_new ();
		g_signal_connect (G_OBJECT (menued->keys_dialog), "delete_event",
				  G_CALLBACK (gtk_widget_hide_on_delete), NULL);
	}

	response = gtk_dialog_run (GTK_DIALOG (menued->keys_dialog));
	if (response == GTK_RESPONSE_OK) {
		gchar *key_sym;

		key_sym = glade_keys_dialog_get_selected_key_symbol (GLADE_KEYS_DIALOG (menued->keys_dialog));
		if (key_sym)
			set_entry_text (GTK_ENTRY (menued->accel_key_entry), key_sym);
	}
	gtk_widget_hide (menued->keys_dialog);
}

static void
on_up_button_clicked (GtkButton *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *clist;
	GbMenuItemData *item, *prev_item;
	gint row, new_row, i, level;
	GList *items;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	clist = menued->clist;
	row = get_selected_row (menued);
	if (row == -1 || row == 0)
		return;

	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	level = item->level;

	/* Find the new position of the item and its children. */
	new_row = -1;
	for (i = row - 1; i >= 0; i--) {
		prev_item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist),
								       i);
		if (prev_item->level == level) {
			new_row = i;
			break;
		}
		else if (prev_item->level < level)
			break;
	}

	/* Return if we can't move the item up. */
	if (new_row == -1)
		return;

	/* Remove item and children. */
	items = remove_item_and_children (clist, row);

	/* Now insert at new position. */
	insert_items (clist, items, new_row);
	ensure_visible (clist, new_row);

	g_list_free (items);

	/* Make sure all items in the group point to the first one. */
	normalize_radio_groups (menued);

	gtk_clist_select_row (GTK_CLIST (clist), new_row, 0);
	set_interface_state (menued);
}

static void
on_down_button_clicked (GtkButton *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *clist;
	GbMenuItemData *item, *next_item;
	gint row, new_row, i, level;
	gboolean found_next_item;
	GList *items;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	clist = menued->clist;
	row = get_selected_row (menued);
	if (row == -1 || row == GTK_CLIST (clist)->rows - 1)
		return;

	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	level = item->level;

	/* Find the new position of the item and its children. */
	new_row = -1;
	found_next_item = FALSE;
	for (i = row + 1; i < GTK_CLIST (clist)->rows; i++) {
		next_item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), i);
		/* We have to skip all the children of the next item as well. */
		if (next_item->level == level) {
			if (found_next_item) {
				new_row = i;
				break;
			}
			else
				found_next_item = TRUE;
		}
		else if (next_item->level < level)
			break;
	}

	/* Return if we can't move the item up. */
	if (new_row == -1) {
		if (found_next_item)
			new_row = i;
		else
			return;
	}

	/* Remove item and children. */
	items = remove_item_and_children (clist, row);
	/* Remember that the new_row needs to be shifted because we deleted items. */
	new_row -= g_list_length (items);

	/* Now insert at new position. */
	insert_items (clist, items, new_row);
	ensure_visible (clist, new_row);

	g_list_free (items);

	/* Make sure all items in the group point to the first one. */
	normalize_radio_groups (menued);

	gtk_clist_select_row (GTK_CLIST (clist), new_row, 0);
	set_interface_state (menued);
}

static void
on_left_button_clicked (GtkButton *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *clist;
	GbMenuItemData *item;
	gint row, i, level;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	clist = menued->clist;
	row = get_selected_row (menued);
	if (row == -1)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	level = item->level;
	if (item->level > 0)
		item->level--;
	gtk_clist_set_shift (GTK_CLIST (clist), row, 0, 0, item->level * GLADE_MENU_EDITOR_INDENT);

	for (i = row + 1; i < GTK_CLIST (clist)->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), i);
		if (item->level <= level)
			break;
		item->level--;
		gtk_clist_set_shift (GTK_CLIST (clist), i, 0, 0,
				     item->level * GLADE_MENU_EDITOR_INDENT);
	}
	check_generated_handlers (menued);
	set_interface_state (menued);
}

static void
on_right_button_clicked (GtkButton *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *clist;
	GbMenuItemData *item, *prev_item;
	gint row, i, level;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	clist = menued->clist;
	row = get_selected_row (menued);
	if (row == -1 || row == 0)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	prev_item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist),
							       row - 1);
	if (prev_item->level < item->level)
		return;

	level = item->level;
	item->level++;
	gtk_clist_set_shift (GTK_CLIST (clist), row, 0, 0, item->level * GLADE_MENU_EDITOR_INDENT);

	for (i = row + 1; i < GTK_CLIST (clist)->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), i);
		if (item->level <= level)
			break;
		item->level++;
		gtk_clist_set_shift (GTK_CLIST (clist), i, 0, 0,
				     item->level * GLADE_MENU_EDITOR_INDENT);
	}

	check_generated_handlers (menued);
	set_interface_state (menued);
}

static void
on_add_button_clicked (GtkWidget *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	add_item (menued, FALSE, FALSE);
}

static void
on_add_child_button_clicked (GtkWidget *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	add_item (menued, TRUE, FALSE);
}

static void
on_add_separator_button_clicked (GtkWidget *button, gpointer user_data)
{
	GladeMenuEditor *menued;
	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (button)));
	add_item (menued, FALSE, TRUE);
}

static void
on_delete_button_clicked (GtkWidget *widget, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *clist;
	GbMenuItemData *item;
	gint row, level, i;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (widget)));
	clist = menued->clist;
	row = get_selected_row (menued);
	if (row == -1)
		return;
	item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), row);
	level = item->level;

	gtk_clist_remove (GTK_CLIST (clist), row);

	/* Update any other items in the same radio group. */
	if (item->type == GB_MENU_ITEM_RADIO)
		remove_from_radio_group (menued, item);

	glade_menu_editor_free_item (item);

	/* Move all children up a level */
	for (i = row; i < GTK_CLIST (clist)->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (clist), i);
		if (item->level <= level)
			break;
		item->level--;
		gtk_clist_set_shift (GTK_CLIST (clist), i, 0, 0,
				     item->level * GLADE_MENU_EDITOR_INDENT);
	}

	gtk_clist_select_row (GTK_CLIST (clist), row, 0);
	set_interface_state (menued);
}

static gboolean
on_key_press (GtkWidget * widget,
	      GdkEventKey * event,
	      gpointer user_data)
{
	switch (event->keyval) {
	case GDK_Delete:
		on_delete_button_clicked (widget, NULL);
		break;
	}

	return FALSE;
}

/**************************************************************************
 * File Selection for selecting icon xpm files.
 **************************************************************************/
static void
on_icon_button_clicked (GtkWidget *widget, gpointer user_data)
{
	GladeMenuEditor *menued;
	GtkWidget *filechooser;
	const gchar *filename = NULL;
	gint filename_len;
	const gchar *icon;

	menued = GLADE_MENU_EDITOR (gtk_widget_get_toplevel (GTK_WIDGET (widget)));

	filechooser = glade_util_file_chooser_new (_("Select icon"), GTK_WINDOW (menued),
						   GTK_FILE_CHOOSER_ACTION_OPEN);

	icon = gtk_entry_get_text (GTK_ENTRY (GTK_COMBO (menued->icon_widget)->entry));
	gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (filechooser), icon);

	if (gtk_dialog_run (GTK_DIALOG(filechooser)) == GTK_RESPONSE_OK)
		filename = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (filechooser));

	gtk_widget_destroy (filechooser);

	if (!filename)
		return;

	/* If the filename ends in '/' it means the user wants to reset the
	   pixmap to NULL. */
	filename_len = strlen (filename);
	if (filename_len > 0 && filename[filename_len - 1] == '/')
		filename = "";

	set_entry_text (GTK_ENTRY (GTK_COMBO (menued->icon_widget)->entry),
			filename);
}

/* This checks if the given icon string is a stock icon name, and if it is
   it returns the stock ID instead. If not, it just returns the icon. */
static gchar *
get_stock_id_from_icon_name (GladeMenuEditor *menued, gchar *icon)
{
	GList *clist;

	if (!icon || *icon == '\0')
		return NULL;

	clist = GTK_LIST (GTK_COMBO (menued->icon_widget)->list)->children;

	while (clist && clist->data) {
		gchar* ltext = glade_util_gtk_combo_func (GTK_LIST_ITEM (clist->data));
		if (!ltext)
			continue;
		if (!strcmp (ltext, icon)) {
			gchar *stock_id = gtk_object_get_data (GTK_OBJECT (clist->data),
							       GladeMenuEditorStockIDKey);
			return stock_id;
		}
		clist = clist->next;
	}

	return icon;
}

/* This creates a radio menu item using the appropriate radio group.
   If the item's group is not NULL and doesn't points to the item itself,
   then it will search the list of groups to find the group. If it can't find
   a group, or it didn't have one it creates a new group.
   If it creates a new group it adds this item to the list of groups. */
static GtkWidget*
create_radio_menu_item (GtkMenuShell *menu,
			GbMenuItemData *item,
			GHashTable *group_hash)
{
	GtkWidget *menuitem;
	GSList *group = NULL;

	if (item->group && item->group != item) {
		GtkRadioMenuItem *group_widget = g_hash_table_lookup (group_hash,
								      item->group);
		if (group_widget)
			group = gtk_radio_menu_item_get_group (group_widget);
	}

	menuitem = gtk_radio_menu_item_new (group);
	if (!group)
		g_hash_table_insert (group_hash, item, menuitem);

	return menuitem;
}

/* This updates the menu, based on the settings in the menu editor.
   It removes all the current children of the menu and recreates it.
   Note that it has to reload all the xpm files for pixmaps, so its not
   very efficient, but they're small so it shouldn't be too bad. */
static void
glade_menu_editor_update_menu (GladeMenuEditor *menued)
{
	GbMenuItemData *item;
	GtkWidget *menuitem, *label, *prev_item = NULL, *child_menu;
	GtkCList *clist;
	GtkMenuShell *current_menu;
	GList *menus;
	GHashTable *group_hash;
	gchar *child_name;
	gint i, level;
	/*** GbWidget *gbwidget;
	GladeWidgetData *wdata; ***/
	GtkAccelGroup *accel_group;
	GtkWidget *pixmap = NULL;
	gboolean use_pixmap_menu_item;
	gchar *stock_id, *icon_name;
#ifdef USE_GNOME
	GnomeUIInfo *uiinfo;
#endif

	/* Remove existing children of the menu. Note that this will result in the
	   old widget names being released, so we need to reserve the new names,
	   even if they are the same. */
	while (menued->menu->children) {
		menuitem = menued->menu->children->data;
		gtk_widget_destroy (menuitem);
	}

	/* FIXME: This seems to be necessary to re-initialise the menu. I don't know
	   why. */
	menued->menu->menu_flag = TRUE;

	clist = GTK_CLIST (menued->clist);

	/* Now reserve all the the new widget names. */
	for (i = 0; i < clist->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (clist, i);

		if (item->name && item->name[0])
			; /*** glade_project_reserve_name (menued->project, item->name); ***/
	}

	/* Now add widgets according to the items in the menu editor clist. */
	level = 0;
	menus = g_list_append (NULL, menued->menu);
	group_hash = g_hash_table_new (NULL, NULL);

	/* Make sure all items in the group point to the first one. */
	normalize_radio_groups (menued);

	for (i = 0; i < clist->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (clist, i);

		icon_name = get_stock_id_from_icon_name (menued, item->icon);
		stock_id = NULL;

#if 0
		/*** ***/
		if (item->level > level) {
			child_menu = gb_widget_new_full ("GtkMenu", FALSE, NULL, NULL, 0, 0,
							 NULL, GB_CREATING, NULL);
			child_name = g_strdup_printf ("%s_menu", prev_item->name);
			gtk_widget_set_name (child_menu, child_name);
			g_free (child_name);
			level = item->level;
			/* We use the menus GList as a stack, pushing menus onto the
			   front. */
			menus = g_list_prepend (menus, child_menu);
			gtk_menu_item_set_submenu (GTK_MENU_ITEM (prev_item), child_menu);
			tree_add_widget (child_menu);
		}
#endif
		while (item->level < level) {
			/* This removes/pops the first menu in the list. */
			menus = g_list_remove_link (menus, menus);
			level--;
		}
		current_menu = GTK_MENU_SHELL (menus->data);

		if (item->label && strlen (item->label) > 0) {
			label = gtk_accel_label_new ("");
			gtk_label_set_text_with_mnemonic (GTK_LABEL (label), item->label);
			gtk_misc_set_alignment (GTK_MISC (label), 0.0, 0.5);
			gtk_widget_show (label);

			pixmap = NULL;
			use_pixmap_menu_item = FALSE;

			if (menued->gnome_support) {
#ifdef USE_GNOME
				if (item->stock_item_index) {
					uiinfo = &GladeStockMenuItemValues[item->stock_item_index];
					if (uiinfo->pixmap_type == GNOME_APP_PIXMAP_STOCK) {
						pixmap = gtk_image_new_from_stock (uiinfo->pixmap_info,
										   GTK_ICON_SIZE_MENU);
						use_pixmap_menu_item = TRUE;
					}
				}
#endif
			}
			else {
				if (item->stock_item_index) {
					stock_id = g_slist_nth_data (menued->stock_items,
								     item->stock_item_index - 1);
#if 0
					g_print ("Stock ID for icon: %s\n", stock_id);
#endif
					pixmap = gtk_image_new_from_stock (stock_id,
									   GTK_ICON_SIZE_MENU);
					use_pixmap_menu_item = TRUE;
				}
			}

#if 0
			if (!item->stock_item_index && icon_name) {
				pixmap = gb_widget_new ("GtkImage", NULL);
				if (glade_util_check_is_stock_id (icon_name)) {
					gtk_image_set_from_stock (GTK_IMAGE (pixmap), icon_name,
								  GTK_ICON_SIZE_MENU);
				}
				else {
					gtk_image_set_from_file (GTK_IMAGE (pixmap), icon_name);

					/* Add the pixmap to the project, so the file is copied.
					   It should be removed from the project in the menuitem
					   GbWidget.destroy function. */
					glade_project_add_pixmap (menued->project, icon_name);
				}
				gb_widget_set_child_name (pixmap, GladeChildMenuItemImage);
				use_pixmap_menu_item = TRUE;
			}
#endif
			
			if (item->type == GB_MENU_ITEM_NORMAL) {
				if (use_pixmap_menu_item) {
					menuitem = gtk_image_menu_item_new ();
					if (pixmap) {
						gtk_widget_show (pixmap);
						gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (menuitem),
									       pixmap);
					}
				} else {
					menuitem = gtk_menu_item_new ();
				}
				gtk_container_add (GTK_CONTAINER (menuitem), label);
			} else if (item->type == GB_MENU_ITEM_CHECK) {
				menuitem = gtk_check_menu_item_new ();
				if (item->active)
					gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem),
									TRUE);
				gtk_container_add (GTK_CONTAINER (menuitem), label);
			} else {
				menuitem = create_radio_menu_item (current_menu,
								   item, group_hash);
				if (item->active)
					gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (menuitem),
									TRUE);
				gtk_container_add (GTK_CONTAINER (menuitem), label);
			}

			gtk_accel_label_set_accel_widget (GTK_ACCEL_LABEL (label), menuitem);
		} else {
			/* This creates a separator. */
			menuitem = gtk_menu_item_new ();
		}
		gtk_widget_show (menuitem);

		/* Save the stock name and icon in the menuitem. */
		if (item->stock_item_index) {
			if (menued->gnome_support) {
#ifdef USE_GNOME
				gtk_object_set_data (GTK_OBJECT (menuitem),
						     GladeMenuItemStockIndexKey,
						     GINT_TO_POINTER (item->stock_item_index));
#endif
			} else {
				gchar *stock_id;

				stock_id = g_slist_nth_data (menued->stock_items,
							     item->stock_item_index - 1);

				gtk_object_set_data_full (GTK_OBJECT (menuitem),
							  GLADE_MENU_ITEM_STOCK_ID_KEY,
							  g_strdup (stock_id),
							  g_free);
			}
		} else if (item->icon) {
			gtk_object_set_data_full (GTK_OBJECT (pixmap), GLADE_ICON_KEY,
						  g_strdup (icon_name), g_free);
		}


		/* Turn it into a GbWidget, and add the 'activate' handler and the
		   accelerator. */
		if (item->name == NULL || item->name[0] == '\0') {
			g_free (item->name);
			item->name = glade_project_new_widget_name (menued->project, "item");
		}

		/*** gbwidget = gb_widget_lookup_class (gtk_type_name (GTK_OBJECT_TYPE (menuitem)));

		if (item->wdata)
			wdata = glade_widget_data_copy (item->wdata);
		else
			wdata = glade_widget_data_new (gbwidget);

		wdata->gbwidget = gbwidget;

		gb_widget_create_from_full (menuitem, NULL, wdata);
		gtk_widget_set_name (menuitem, item->name);

		if (item->active)
			wdata->flags |= GLADE_ACTIVE;
		else
			wdata->flags &= ~GLADE_ACTIVE;

		g_free (wdata->tooltip);
		wdata->tooltip = g_strdup (item->tooltip); ***/
		/* FIXME: Should set tooltip? Or for Gnome install in appbar? */

#if 0
		/*** ***/
		if (item->handler && strlen (item->handler) > 0) {
			GladeSignal *signal = g_new (GladeSignal, 1);
			signal->name = g_strdup ("activate");
			signal->handler = g_strdup (item->handler);
			signal->object = NULL;
			signal->after = FALSE;
			signal->data = NULL;

			/* If the last mod time is set to the special value, we set it to
			   the current time now. */
			if (item->last_mod_time == (time_t) -2) {
				item->last_mod_time = time (NULL);
				if (item->last_mod_time == (time_t) -1)
					g_warning ("Can't get current time");
			}

			signal->last_modification_time = item->last_mod_time;
			wdata->signals = g_list_append (wdata->signals, signal);
		}

		if (item->key && strlen (item->key) > 0) {
			GladeAccelerator *accel = g_new (GladeAccelerator, 1);
			guint key;
			accel->modifiers = item->modifiers;
			accel->key = g_strdup (item->key);
			accel->signal = g_strdup ("activate");
			wdata->accelerators = g_list_append (wdata->accelerators, accel);

			/* We can only add accelerators to menus, not menubars. */
			if (GTK_IS_MENU (current_menu)) {
				key = glade_keys_dialog_find_key (item->key);
				accel_group = GTK_MENU (current_menu)->accel_group;
				gtk_widget_add_accelerator (menuitem, "activate", accel_group,
							    key, item->modifiers,
							    GTK_ACCEL_VISIBLE);
			}
		}

		if (menued->gnome_support) {
#ifdef USE_GNOME
			/* For stock menu items, we use the configured accelerator keys. */
			if (GTK_IS_MENU (current_menu)
			    && item->stock_item_index && uiinfo->accelerator_key != 0) {
				accel_group = GTK_MENU (current_menu)->accel_group;
				gtk_widget_add_accelerator (menuitem, "activate", accel_group,
							    uiinfo->accelerator_key,
							    uiinfo->ac_mods,
							    GTK_ACCEL_VISIBLE);
			}
#endif
		} else {
			/* For stock menu items, we use the configured accelerator keys. */
			if (GTK_IS_MENU (current_menu) && item->stock_item_index && stock_id) {
				GtkStockItem item;

				gtk_stock_lookup (stock_id, &item);
				if (item.keyval) {
					accel_group = GTK_MENU (current_menu)->accel_group;
					gtk_widget_add_accelerator (menuitem, "activate",
								    accel_group,
								    item.keyval, item.modifier,
								    GTK_ACCEL_VISIBLE);
				}
			}
		}
#endif
		/* Add the menuitem to the current menu. */
		gtk_menu_shell_append (current_menu, menuitem);
		/*** tree_add_widget (menuitem); ***/
		prev_item = menuitem;
	}
	g_list_free (menus);
	g_hash_table_destroy (group_hash);

	/* Make sure the displayed item is correct. */
	show_item_properties (menued);
}

static void
on_menu_editor_ok (GtkWidget *button, GladeMenuEditor *menued)
{
	glade_menu_editor_update_menu (menued);
	gtk_widget_destroy (GTK_WIDGET (menued));
}

static void
on_menu_editor_apply (GtkWidget *button, GladeMenuEditor *menued)
{
	glade_menu_editor_update_menu (menued);
}

static void
on_menu_editor_close (GtkWidget *widget, GladeMenuEditor *menued)
{
	gtk_widget_destroy (GTK_WIDGET (menued));
}


/**************************************************************************
 * Public functions
 **************************************************************************/

static void
glade_menu_editor_construct (GladeMenuEditor *menued,
			     GladeProject *project)
{
	GtkWidget *vbox2, *vbox1, *scrolled_win;
	GtkWidget *hbox1;
	GtkWidget *vbox3;
	GtkWidget *table1;
	GtkWidget *eventbox3;
	GtkWidget *eventbox2;
	GtkWidget *eventbox1;
	GtkWidget *table2;
	GSList *table2_group = NULL;
	GtkWidget *table3;
	GtkWidget *accel_key_button;
	GtkWidget *hbox2;
	GtkWidget *label9;
	GtkWidget *label8;
	GtkWidget *hbuttonbox3;
	GtkWidget *arrow1;
	GtkWidget *arrow2;
	GtkWidget *arrow3;
	GtkWidget *arrow4;
	GtkWidget *button_table;
	GtkWidget *hseparator1;
	GtkWidget *hbuttonbox1;
	GtkWidget *listitem;
	GtkTooltips *tooltips;
	gchar *titles[N_COLUMNS];
	gint row;
	GSList *elem;
	gchar *stock_id;
	GtkWidget *icon_hbox;
	gchar *label_text;
	gint idx;
#ifdef USE_GNOME
	GnomeUIInfo *uiinfo;
	GtkWidget *pixmap, *label, *hbox, *separator;
#endif

	menued->project = project;
#ifdef USE_GNOME
	if (glade_project_get_gnome_support (project))
		menued->gnome_support = TRUE;
#endif

	tooltips = gtk_tooltips_new ();

	gtk_container_set_border_width (GTK_CONTAINER (menued), 8);
	gtk_window_set_title (GTK_WINDOW (menued), _ ("Menu Editor"));
	gtk_window_set_policy (GTK_WINDOW (menued), FALSE, TRUE, FALSE);
	gtk_window_set_wmclass (GTK_WINDOW (menued), "menu_editor", "Glade");

	vbox2 = gtk_vbox_new (FALSE, 0);
	gtk_widget_show (vbox2);
	gtk_container_add (GTK_CONTAINER (menued), vbox2);

	hbox1 = gtk_hbox_new (FALSE, 6);
	gtk_widget_show (hbox1);
	gtk_box_pack_start (GTK_BOX (vbox2), hbox1, TRUE, TRUE, 0);

	vbox1 = gtk_vbox_new (FALSE, 4);
	gtk_widget_show (vbox1);
	gtk_box_pack_start (GTK_BOX (hbox1), vbox1, TRUE, TRUE, 0);

	titles[LABEL_COLUMN]		= _("Label");
	titles[TYPE_COLUMN]		= _("Type");
	titles[ACCEL_COLUMN]		= _("Accelerator");
	titles[NAME_COLUMN]		= _("Name");
	titles[HANDLER_COLUMN]	= _("Handler");
	titles[ACTIVE_COLUMN]	= _("Active");
	titles[GROUP_COLUMN]		= _("Group");
	titles[ICON_COLUMN]		= _("Icon");

	menued->clist = gtk_clist_new_with_titles (N_COLUMNS, titles);
	gtk_widget_show (menued->clist);
	GTK_WIDGET_SET_FLAGS (menued->clist, GTK_CAN_FOCUS);
	gtk_signal_connect (GTK_OBJECT (menued->clist), "key_press_event",
			    GTK_SIGNAL_FUNC (on_key_press),
			    NULL);
	gtk_widget_set_usize (menued->clist, 300, -1);
	gtk_signal_connect (GTK_OBJECT (menued->clist), "select_row",
			    GTK_SIGNAL_FUNC (on_clist_select_row), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->clist), "unselect_row",
			    GTK_SIGNAL_FUNC (on_clist_unselect_row), NULL);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), LABEL_COLUMN, 144);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), TYPE_COLUMN, 42);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), ACCEL_COLUMN, 120);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), NAME_COLUMN, 100);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), HANDLER_COLUMN, 172);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), ICON_COLUMN, 172);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), ACTIVE_COLUMN, 42);
	gtk_clist_set_column_width (GTK_CLIST (menued->clist), GROUP_COLUMN, 75);
	gtk_clist_column_titles_show (GTK_CLIST (menued->clist));
	gtk_clist_column_titles_passive (GTK_CLIST (menued->clist));

	scrolled_win = gtk_scrolled_window_new (NULL, NULL);
	gtk_container_add (GTK_CONTAINER (scrolled_win), menued->clist);
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (scrolled_win),
					GTK_POLICY_AUTOMATIC, GTK_POLICY_AUTOMATIC);
	gtk_box_pack_start (GTK_BOX (vbox1), scrolled_win, TRUE, TRUE, 0);
	gtk_widget_show (scrolled_win);

	hbuttonbox3 = gtk_hbutton_box_new ();
	gtk_widget_show (hbuttonbox3);
	gtk_box_pack_start (GTK_BOX (vbox1), hbuttonbox3, FALSE, TRUE, 0);
	gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox3),
				   GTK_BUTTONBOX_SPREAD);
	gtk_box_set_spacing (GTK_BOX (hbuttonbox3), 6);

	menued->up_button = gtk_button_new ();
	gtk_widget_show (menued->up_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox3), menued->up_button);
	gtk_tooltips_set_tip (tooltips, menued->up_button, _ ("Move the item and its children up one place in the list"), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->up_button), "clicked",
			    GTK_SIGNAL_FUNC (on_up_button_clicked),
			    NULL);

	arrow1 = gtk_arrow_new (GTK_ARROW_UP, GTK_SHADOW_OUT);
	gtk_widget_show (arrow1);
	gtk_container_add (GTK_CONTAINER (menued->up_button), arrow1);

	menued->down_button = gtk_button_new ();
	gtk_widget_show (menued->down_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox3), menued->down_button);
	gtk_tooltips_set_tip (tooltips, menued->down_button, _ ("Move the item and its children down one place in the list"), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->down_button), "clicked",
			    GTK_SIGNAL_FUNC (on_down_button_clicked),
			    NULL);

	arrow2 = gtk_arrow_new (GTK_ARROW_DOWN, GTK_SHADOW_OUT);
	gtk_widget_show (arrow2);
	gtk_container_add (GTK_CONTAINER (menued->down_button), arrow2);

	menued->left_button = gtk_button_new ();
	gtk_widget_show (menued->left_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox3), menued->left_button);
	gtk_tooltips_set_tip (tooltips, menued->left_button, _ ("Move the item and its children up one level"), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->left_button), "clicked",
			    GTK_SIGNAL_FUNC (on_left_button_clicked),
			    NULL);

	arrow3 = gtk_arrow_new (GTK_ARROW_LEFT, GTK_SHADOW_OUT);
	gtk_widget_show (arrow3);
	gtk_container_add (GTK_CONTAINER (menued->left_button), arrow3);

	menued->right_button = gtk_button_new ();
	gtk_widget_show (menued->right_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox3), menued->right_button);
	gtk_tooltips_set_tip (tooltips, menued->right_button, _ ("Move the item and its children down one level"), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->right_button), "clicked",
			    GTK_SIGNAL_FUNC (on_right_button_clicked),
			    NULL);

	arrow4 = gtk_arrow_new (GTK_ARROW_RIGHT, GTK_SHADOW_OUT);
	gtk_widget_show (arrow4);
	gtk_container_add (GTK_CONTAINER (menued->right_button), arrow4);

	vbox3 = gtk_vbox_new (FALSE, 6);
	gtk_widget_show (vbox3);
	gtk_box_pack_start (GTK_BOX (hbox1), vbox3, FALSE, TRUE, 0);

	table1 = gtk_table_new (5, 3, FALSE);
	gtk_widget_show (table1);
	gtk_box_pack_start (GTK_BOX (vbox3), table1, FALSE, TRUE, 0);
	gtk_table_set_row_spacings (GTK_TABLE (table1), 2);
	gtk_table_set_col_spacings (GTK_TABLE (table1), 4);
	row = 0;

	menued->stock_items = gtk_stock_list_ids ();
	menued->stock_items = g_slist_sort (menued->stock_items,
					    glade_util_compare_stock_labels);

	if (!menued->gnome_support) {
		eventbox1 = gtk_event_box_new ();
		gtk_widget_show (eventbox1);
		gtk_table_attach (GTK_TABLE (table1), eventbox1, 0, 1, row, row + 1,
				  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
		gtk_tooltips_set_tip (tooltips, eventbox1, _ ("The stock item to use."),
				      NULL);

		menued->stock_label = gtk_label_new (_ ("Stock Item:"));
		gtk_widget_show (menued->stock_label);
		gtk_container_add (GTK_CONTAINER (eventbox1), menued->stock_label);
		gtk_misc_set_alignment (GTK_MISC (menued->stock_label), 0, 0.5);

		menued->stock_combo = gtk_combo_new ();
		gtk_widget_set_usize (menued->stock_combo, 100, -1);
		gtk_table_attach (GTK_TABLE (table1), menued->stock_combo, 1, 3,
				  row, row + 1, GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
		gtk_combo_set_value_in_list (GTK_COMBO (menued->stock_combo),
					     TRUE, FALSE);
		gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (menued->stock_combo)->entry),
					   FALSE);
		gtk_widget_show (menued->stock_combo);

		listitem = gtk_list_item_new_with_label (_("None"));
		gtk_widget_show (listitem);
		gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->stock_combo)->list),
				   listitem);

		for (elem = menued->stock_items, idx = 1; elem; elem = elem->next, idx++) {
			GtkStockItem item;
			GtkWidget *listitem, *hbox, *image, *label;
			GtkIconSet *icon_set;
			GtkIconSize *sizes;
			gint n_sizes, i;
			gboolean has_menu_size;

			stock_id = elem->data;

			/* Check that the icon has a menu size. */
			icon_set = gtk_icon_factory_lookup_default (stock_id);
			if (!icon_set)
				continue;

			gtk_icon_set_get_sizes (icon_set, &sizes, &n_sizes);
			has_menu_size = FALSE;
			for (i = 0; i < n_sizes; i++) {
				if (sizes[i] == GTK_ICON_SIZE_MENU)
					has_menu_size = TRUE;
			}
			g_free (sizes);

			if (!has_menu_size)
				continue;

			if (gtk_stock_lookup (stock_id, &item)) {
				listitem = gtk_list_item_new ();
				gtk_object_set_data (GTK_OBJECT (listitem),
						     GladeMenuEditorIndexKey,
						     GINT_TO_POINTER (idx));
				gtk_widget_show (listitem);
				hbox = gtk_hbox_new (FALSE, 3);
				gtk_container_add (GTK_CONTAINER (listitem), hbox);
				gtk_widget_show (hbox);

				image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);

				if (image) {
					gtk_widget_show (image);
					gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);
				}

				label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
				gtk_label_set_text_with_mnemonic (GTK_LABEL (label), item.label);
				gtk_widget_show (label);
				gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

				gtk_combo_set_item_string (GTK_COMBO (menued->stock_combo),
							   GTK_ITEM (listitem), item.label);
				
				gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->stock_combo)->list), listitem);
			}
		}
		gtk_signal_connect (GTK_OBJECT (GTK_COMBO (menued->stock_combo)->entry),
				    "changed",
				    GTK_SIGNAL_FUNC (on_stock_item_entry_changed),
				    NULL);
		row++;
	}

#ifdef USE_GNOME
	if (menued->gnome_support) {
		/* Gnome stock item. */
		eventbox1 = gtk_event_box_new ();
		gtk_widget_show (eventbox1);
		gtk_table_attach (GTK_TABLE (table1), eventbox1, 0, 1, row, row + 1,
				  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
		gtk_tooltips_set_tip (tooltips, eventbox1,
				      _ ("The stock Gnome item to use."), NULL);

		menued->stock_label = gtk_label_new (_ ("Stock Item:"));
		gtk_widget_show (menued->stock_label);
		gtk_container_add (GTK_CONTAINER (eventbox1), menued->stock_label);
		gtk_misc_set_alignment (GTK_MISC (menued->stock_label), 0, 0.5);

		menued->stock_combo = gtk_combo_new ();
		gtk_widget_set_usize (menued->stock_combo, 100, -1);
		gtk_table_attach (GTK_TABLE (table1), menued->stock_combo, 1, 3,
				  row, row + 1, GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
		gtk_combo_set_value_in_list (GTK_COMBO (menued->stock_combo),
					     TRUE, FALSE);
		gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (menued->stock_combo)->entry),
					   FALSE);
		gtk_widget_show (menued->stock_combo);

		for (uiinfo = GladeStockMenuItemValues;
		     uiinfo->type != GNOME_APP_UI_ENDOFINFO;
		     uiinfo++) {
			pixmap = NULL;
			label = NULL;
			label_text = NULL;
			
			if (uiinfo->type == GNOME_APP_UI_ITEM_CONFIGURABLE)
				gnome_app_ui_configure_configurable (uiinfo);

			uiinfo->widget = NULL;

			switch (uiinfo->type) {
			case GNOME_APP_UI_SEPARATOR:
				uiinfo->widget = gtk_list_item_new ();
				gtk_widget_show (uiinfo->widget);
				separator = gtk_hseparator_new();
				gtk_widget_show (separator);
				gtk_container_add (GTK_CONTAINER (uiinfo->widget), separator);
				gtk_widget_set_sensitive (uiinfo->widget, FALSE);
				gtk_combo_set_item_string (GTK_COMBO (menued->stock_combo),
							   GTK_ITEM (uiinfo->widget), "");
				gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->stock_combo)->list), uiinfo->widget);
				break;
			case GNOME_APP_UI_ITEM:
			case GNOME_APP_UI_SUBTREE_STOCK:
				if (uiinfo->pixmap_type == GNOME_APP_PIXMAP_STOCK)
					pixmap = gtk_image_new_from_stock (uiinfo->pixmap_info, GTK_ICON_SIZE_MENU);
				
				if (uiinfo->label && uiinfo->label[0]) {
					uiinfo->widget = gtk_list_item_new ();
					gtk_widget_show (uiinfo->widget);
					hbox = gtk_hbox_new (FALSE, 3);
					gtk_container_add (GTK_CONTAINER (uiinfo->widget), hbox);
					gtk_widget_show (hbox);

					if (!pixmap && uiinfo->type != GNOME_APP_UI_SUBTREE_STOCK) {
						/* Create a dummy widget to fill in the space. */
						pixmap = gtk_alignment_new (0, 0, 0, 0);
						gtk_widget_set_size_request (pixmap, 16, -1);
					}

					if (pixmap) {
						gtk_widget_show (pixmap);
						gtk_box_pack_start (GTK_BOX (hbox), pixmap,
								    FALSE, FALSE, 0);
					}

					label = gtk_accel_label_new ("");
					/* Most of the label text is from Gnome, but 2 of them are
					   ours, so we use a utility function to find the translation.
					*/
					label_text = glade_gnome_gettext (uiinfo->label);
					gtk_label_set_text_with_mnemonic (GTK_LABEL (label),
									  label_text);
					gtk_widget_show (label);
					gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);

					gtk_combo_set_item_string (GTK_COMBO (menued->stock_combo),
								   GTK_ITEM (uiinfo->widget),
								   label_text);

					gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->stock_combo)->list), uiinfo->widget);
				}
				break;

			default:
				g_warning ("Invalid UIINFO item type");
			}
		}
		gtk_signal_connect (GTK_OBJECT (GTK_COMBO (menued->stock_combo)->entry),
				    "changed",
				    GTK_SIGNAL_FUNC (on_stock_item_entry_changed),
				    NULL);

		row++;
	}
#endif /* USE_GNOME */

	/* Item Label. */
	eventbox1 = gtk_event_box_new ();
	gtk_widget_show (eventbox1);
	gtk_table_attach (GTK_TABLE (table1), eventbox1, 0, 1, row, row + 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox1, _ ("The text of the menu item, or empty for separators. Hit return to add a new item below this one. Ctl-Return adds a child item beneath this one."), NULL);

	menued->label_label = gtk_label_new (_ ("Label:"));
	gtk_widget_show (menued->label_label);
	gtk_container_add (GTK_CONTAINER (eventbox1), menued->label_label);
	gtk_misc_set_alignment (GTK_MISC (menued->label_label), 0, 0.5);

	menued->label_entry = gtk_entry_new ();
	gtk_widget_show (menued->label_entry);
	gtk_table_attach (GTK_TABLE (table1), menued->label_entry, 1, 3,
			  row, row + 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_signal_connect (GTK_OBJECT (menued->label_entry), "changed",
			    GTK_SIGNAL_FUNC (on_entry_changed), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->label_entry), "key_press_event",
			    GTK_SIGNAL_FUNC (on_label_entry_key_press),
			    NULL);
	row++;

	/* Item Name. */
	eventbox2 = gtk_event_box_new ();
	gtk_widget_show (eventbox2);
	gtk_table_attach (GTK_TABLE (table1), eventbox2, 0, 1, row, row + 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox2, _ ("The name of the widget"),
			      NULL);

	menued->name_label = gtk_label_new (_ ("Name:"));
	gtk_widget_show (menued->name_label);
	gtk_container_add (GTK_CONTAINER (eventbox2), menued->name_label);
	gtk_misc_set_alignment (GTK_MISC (menued->name_label), 0, 0.5);

	menued->name_entry = gtk_entry_new ();
	gtk_widget_show (menued->name_entry);
	gtk_table_attach (GTK_TABLE (table1), menued->name_entry, 1, 3, row, row + 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_signal_connect (GTK_OBJECT (menued->name_entry), "changed",
			    GTK_SIGNAL_FUNC (on_entry_changed), NULL);
	row++;

	/* Item Handler. */
	eventbox3 = gtk_event_box_new ();
	gtk_widget_show (eventbox3);
	gtk_table_attach (GTK_TABLE (table1), eventbox3, 0, 1, row, row + 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox3, _ ("The function to be called when the item is selected"), NULL);

	menued->handler_label = gtk_label_new (_ ("Handler:"));
	gtk_widget_show (menued->handler_label);
	gtk_container_add (GTK_CONTAINER (eventbox3), menued->handler_label);
	gtk_misc_set_alignment (GTK_MISC (menued->handler_label), 0, 0.5);

	menued->handler_entry = gtk_entry_new ();
	gtk_widget_show (menued->handler_entry);
	gtk_table_attach (GTK_TABLE (table1), menued->handler_entry, 1, 3,
			  row, row + 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_signal_connect (GTK_OBJECT (menued->handler_entry), "changed",
			    GTK_SIGNAL_FUNC (on_entry_changed), NULL);
	row++;

	/* Item Icon. */
	eventbox1 = gtk_event_box_new ();
	gtk_widget_show (eventbox1);
	gtk_table_attach (GTK_TABLE (table1), eventbox1, 0, 1, row, row + 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox1, _ ("An optional icon to show on the left of the menu item."), NULL);

	menued->icon_label = gtk_label_new (_ ("Icon:"));
	gtk_widget_show (menued->icon_label);
	gtk_container_add (GTK_CONTAINER (eventbox1), menued->icon_label);
	gtk_misc_set_alignment (GTK_MISC (menued->icon_label), 0, 0.5);

	icon_hbox = gtk_hbox_new (FALSE, 2);
	gtk_table_attach (GTK_TABLE (table1), icon_hbox, 1, 3, row, row + 1,
			  GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_widget_show (icon_hbox);

	menued->icon_widget = gtk_combo_new ();
	gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (menued->icon_widget)->entry),
				   FALSE);
	gtk_widget_set_usize (menued->icon_widget, 100, -1);

	/* Add a "None" item first, so it is easy to reset the pixmap. */
	listitem = gtk_list_item_new_with_label (_("None"));
	gtk_widget_show (listitem);
	gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->icon_widget)->list),
			   listitem);

	for (elem = menued->stock_items; elem; elem = elem->next) {
		GtkStockItem item;
		GtkWidget *listitem, *hbox, *image, *label;
		GtkIconSet *icon_set;
		GtkIconSize *sizes;
		gint n_sizes, i;
		gboolean has_menu_size;

		stock_id = elem->data;

		/* Check that the icon has a menu size. */
		icon_set = gtk_icon_factory_lookup_default (stock_id);
		if (!icon_set)
			continue;

		gtk_icon_set_get_sizes (icon_set, &sizes, &n_sizes);
		has_menu_size = FALSE;
		for (i = 0; i < n_sizes; i++) {
			if (sizes[i] == GTK_ICON_SIZE_MENU)
				has_menu_size = TRUE;
		}
		g_free (sizes);
		if (!has_menu_size)
			continue;

		image = gtk_image_new_from_stock (stock_id, GTK_ICON_SIZE_MENU);
		if (image) {
			listitem = gtk_list_item_new ();
			/* We store a pointer to the stock_id from the stock_items list,
			   so we don't need to free it, but if stock_items is destroyed it
			   will no longer be valid. */
			gtk_object_set_data (GTK_OBJECT (listitem),
					     GladeMenuEditorStockIDKey, stock_id);
			gtk_widget_show (listitem);
			hbox = gtk_hbox_new (FALSE, 3);
			gtk_container_add (GTK_CONTAINER (listitem), hbox);
			gtk_widget_show (hbox);
			gtk_widget_show (image);
			gtk_box_pack_start (GTK_BOX (hbox), image, FALSE, FALSE, 0);

			if (gtk_stock_lookup (stock_id, &item))
				label_text = item.label;
			else
				/* FIXME: We have no name to use for the image. */
				label_text = stock_id;

			label = gtk_type_new (GTK_TYPE_ACCEL_LABEL);
			gtk_label_set_text_with_mnemonic (GTK_LABEL (label), label_text);

			gtk_widget_show (label);
			gtk_box_pack_start (GTK_BOX (hbox), label, FALSE, FALSE, 0);
			gtk_combo_set_item_string (GTK_COMBO (menued->icon_widget),
						   GTK_ITEM (listitem), label_text);
			gtk_container_add (GTK_CONTAINER (GTK_COMBO (menued->icon_widget)->list),
					   listitem);
		}
	}

	gtk_signal_connect (GTK_OBJECT (GTK_COMBO (menued->icon_widget)->entry),
			    "changed", GTK_SIGNAL_FUNC (on_entry_changed), NULL);

	gtk_widget_show (menued->icon_widget);
	gtk_box_pack_start (GTK_BOX (icon_hbox), menued->icon_widget,
			    TRUE, TRUE, 0);

	menued->icon_button = gtk_button_new_with_label ("...");
	gtk_widget_show (menued->icon_button);
	gtk_box_pack_start (GTK_BOX (icon_hbox), menued->icon_button,
			    FALSE, FALSE, 0);
	gtk_signal_connect (GTK_OBJECT (menued->icon_button), "clicked",
			    GTK_SIGNAL_FUNC (on_icon_button_clicked), NULL);
	row++;

	/* Tooltip. */
	eventbox3 = gtk_event_box_new ();
	gtk_widget_show (eventbox3);
	gtk_table_attach (GTK_TABLE (table1), eventbox3, 0, 1, row, row + 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox3, _ ("The tip to show when the mouse is over the item"), NULL);

	menued->tooltip_label = gtk_label_new (_ ("Tooltip:"));
	gtk_widget_show (menued->tooltip_label);
	gtk_container_add (GTK_CONTAINER (eventbox3), menued->tooltip_label);
	gtk_misc_set_alignment (GTK_MISC (menued->tooltip_label), 0, 0.5);

	menued->tooltip_entry = gtk_entry_new ();
	gtk_widget_show (menued->tooltip_entry);
	gtk_table_attach (GTK_TABLE (table1), menued->tooltip_entry, 1, 3,
			  row, row + 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_signal_connect (GTK_OBJECT (menued->tooltip_entry), "changed",
			    GTK_SIGNAL_FUNC (on_entry_changed), NULL);
	row++;

	/* Buttons to add/delete items. */
	button_table = gtk_table_new (2, 2, TRUE);
	gtk_table_set_row_spacings (GTK_TABLE (button_table), 1);
	gtk_table_set_col_spacings (GTK_TABLE (button_table), 2);
	gtk_widget_show (button_table);
	gtk_box_pack_start (GTK_BOX (vbox3), button_table, FALSE, TRUE, 0);

	menued->add_button = gtk_button_new_with_mnemonic (_("_Add"));
	gtk_widget_show (menued->add_button);
	gtk_table_attach (GTK_TABLE (button_table), menued->add_button,
			  0, 1, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, menued->add_button,
			      _ ("Add a new item below the selected item."), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->add_button), "clicked",
			    GTK_SIGNAL_FUNC (on_add_button_clicked),
			    NULL);

	menued->add_child_button = gtk_button_new_with_mnemonic (_("Add _Child"));
	gtk_widget_show (menued->add_child_button);
	gtk_table_attach (GTK_TABLE (button_table), menued->add_child_button,
			  1, 2, 0, 1, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, menued->add_child_button,
			      _ ("Add a new child item below the selected item."),
			      NULL);
	gtk_signal_connect (GTK_OBJECT (menued->add_child_button), "clicked",
			    GTK_SIGNAL_FUNC (on_add_child_button_clicked),
			    NULL);

	menued->add_separator_button = gtk_button_new_with_mnemonic (_("Add _Separator"));
	gtk_widget_show (menued->add_separator_button);
	gtk_table_attach (GTK_TABLE (button_table), menued->add_separator_button,
			  0, 1, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, menued->add_separator_button,
			      _ ("Add a separator below the selected item."), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->add_separator_button), "clicked",
			    GTK_SIGNAL_FUNC (on_add_separator_button_clicked),
			    NULL);

	menued->delete_button = gtk_button_new_with_mnemonic (_("_Delete"));
	gtk_widget_show (menued->delete_button);
	gtk_table_attach (GTK_TABLE (button_table), menued->delete_button,
			  1, 2, 1, 2, GTK_EXPAND | GTK_FILL, GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, menued->delete_button,
			      _ ("Delete the current item"), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->delete_button), "clicked",
			    GTK_SIGNAL_FUNC (on_delete_button_clicked),
			    NULL);

	/* Type radio options and toggle options. */
	menued->type_frame = gtk_frame_new (_ ("Item Type:"));
	gtk_widget_show (menued->type_frame);
	gtk_box_pack_start (GTK_BOX (vbox3), menued->type_frame, FALSE, TRUE, 0);

	table2 = gtk_table_new (3, 3, FALSE);
	gtk_table_set_col_spacings (GTK_TABLE (table2), 4);
	gtk_table_set_row_spacings (GTK_TABLE (table2), 1);
	gtk_widget_show (table2);
	gtk_container_add (GTK_CONTAINER (menued->type_frame), table2);
	gtk_container_set_border_width (GTK_CONTAINER (table2), 4);

	eventbox1 = gtk_event_box_new ();
	gtk_widget_show (eventbox1);
	gtk_table_attach (GTK_TABLE (table2), eventbox1, 1, 2, 1, 2,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_tooltips_set_tip (tooltips, eventbox1,
			      _ ("If the item is initially on."), NULL);

	menued->state_label = gtk_label_new (_("Active:"));
	gtk_misc_set_alignment (GTK_MISC (menued->state_label), 0, 0.5);
	gtk_widget_show (menued->state_label);
	gtk_container_add (GTK_CONTAINER (eventbox1), menued->state_label);

	menued->state_togglebutton = gtk_toggle_button_new_with_label (_("No"));
	gtk_widget_show (menued->state_togglebutton);
	gtk_table_attach (GTK_TABLE (table2), menued->state_togglebutton, 2, 3, 1, 2,
			  GTK_EXPAND | GTK_FILL, 0, 0, 0);

	hbox2 = gtk_hbox_new (FALSE, 4);
	gtk_widget_show (hbox2);
	gtk_table_attach (GTK_TABLE (table2), hbox2, 1, 3, 2, 3,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

	eventbox1 = gtk_event_box_new ();
	gtk_widget_show (eventbox1);
	gtk_box_pack_start (GTK_BOX (hbox2), eventbox1, FALSE, TRUE, 0);
	gtk_tooltips_set_tip (tooltips, eventbox1,
			      _ ("The radio menu item's group"), NULL);

	menued->group_label = gtk_label_new (_ ("Group:"));
	gtk_misc_set_alignment (GTK_MISC (menued->group_label), 0, 0.5);
	gtk_widget_show (menued->group_label);
	gtk_container_add (GTK_CONTAINER (eventbox1), menued->group_label);

	menued->group_combo = gtk_combo_new ();
	gtk_editable_set_editable (GTK_EDITABLE (GTK_COMBO (menued->group_combo)->entry),
				   FALSE);
	gtk_widget_set_usize (GTK_COMBO (menued->group_combo)->entry, 60, -1);
	gtk_widget_show (menued->group_combo);
	gtk_box_pack_start (GTK_BOX (hbox2), menued->group_combo, TRUE, TRUE, 0);
	gtk_signal_connect (GTK_OBJECT (GTK_COMBO (menued->group_combo)->entry),
			    "changed", GTK_SIGNAL_FUNC (on_entry_changed), NULL);

	menued->radio_radiobutton = gtk_radio_button_new_with_label (table2_group,
								     _ ("Radio"));
	table2_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (menued->radio_radiobutton));
	gtk_widget_show (menued->radio_radiobutton);
	gtk_table_attach (GTK_TABLE (table2), menued->radio_radiobutton, 0, 1, 2, 3,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

	menued->check_radiobutton = gtk_radio_button_new_with_label (table2_group,
								     _ ("Check"));
	table2_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (menued->check_radiobutton));
	gtk_widget_show (menued->check_radiobutton);
	gtk_table_attach (GTK_TABLE (table2), menued->check_radiobutton, 0, 1, 1, 2,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

	menued->normal_radiobutton = gtk_radio_button_new_with_label (table2_group,
								      _ ("Normal"));
	table2_group = gtk_radio_button_get_group (GTK_RADIO_BUTTON (menued->normal_radiobutton));
	gtk_widget_show (menued->normal_radiobutton);
	gtk_table_attach (GTK_TABLE (table2), menued->normal_radiobutton, 0, 1, 0, 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_toggle_button_set_active (GTK_TOGGLE_BUTTON (menued->normal_radiobutton), TRUE);


	/* Accelerator key options. */
	menued->accel_frame = gtk_frame_new (_ ("Accelerator:"));
	gtk_widget_show (menued->accel_frame);
	gtk_box_pack_start (GTK_BOX (vbox3), menued->accel_frame, FALSE, TRUE, 0);

	table3 = gtk_table_new (2, 2, FALSE);
	gtk_widget_show (table3);
	gtk_container_add (GTK_CONTAINER (menued->accel_frame), table3);
	gtk_container_set_border_width (GTK_CONTAINER (table3), 4);
	gtk_table_set_row_spacings (GTK_TABLE (table3), 2);
	gtk_table_set_col_spacings (GTK_TABLE (table3), 4);

	hbox2 = gtk_hbox_new (FALSE, 2);
	gtk_widget_show (hbox2);
	gtk_table_attach (GTK_TABLE (table3), hbox2, 1, 2, 1, 2,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

	menued->accel_key_entry = gtk_entry_new ();
	gtk_widget_set_usize (menued->accel_key_entry, 100, -1);
	gtk_widget_show (menued->accel_key_entry);
	gtk_box_pack_start (GTK_BOX (hbox2), menued->accel_key_entry,
			    TRUE, TRUE, 0);
	gtk_signal_connect (GTK_OBJECT (menued->accel_key_entry), "changed",
			    GTK_SIGNAL_FUNC (on_entry_changed), NULL);

	accel_key_button = gtk_button_new_with_label ("...");
	gtk_widget_show (accel_key_button);
	gtk_box_pack_start (GTK_BOX (hbox2), accel_key_button,
			    FALSE, TRUE, 0);
	gtk_signal_connect (GTK_OBJECT (accel_key_button), "clicked",
			    GTK_SIGNAL_FUNC (on_accel_key_button_clicked),
			    NULL);

	hbox2 = gtk_hbox_new (TRUE, 0);
	gtk_widget_show (hbox2);
	gtk_table_attach (GTK_TABLE (table3), hbox2, 1, 2, 0, 1,
			  GTK_EXPAND | GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);

	menued->accel_ctrl_checkbutton = gtk_check_button_new_with_label (_ ("Ctrl"));
	gtk_widget_show (menued->accel_ctrl_checkbutton);
	gtk_box_pack_start (GTK_BOX (hbox2), menued->accel_ctrl_checkbutton,
			    TRUE, TRUE, 0);

	menued->accel_shift_checkbutton = gtk_check_button_new_with_label (_ ("Shift"));
	gtk_widget_show (menued->accel_shift_checkbutton);
	gtk_box_pack_start (GTK_BOX (hbox2), menued->accel_shift_checkbutton,
			    TRUE, TRUE, 0);

	menued->accel_alt_checkbutton = gtk_check_button_new_with_label (_ ("Alt"));
	gtk_widget_show (menued->accel_alt_checkbutton);
	gtk_box_pack_start (GTK_BOX (hbox2), menued->accel_alt_checkbutton,
			    TRUE, TRUE, 0);

	label9 = gtk_label_new (_ ("Key:"));
	gtk_widget_show (label9);
	gtk_table_attach (GTK_TABLE (table3), label9, 0, 1, 1, 2,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_misc_set_alignment (GTK_MISC (label9), 0, 0.5);

	label8 = gtk_label_new (_ ("Modifiers:"));
	gtk_widget_show (label8);
	gtk_table_attach (GTK_TABLE (table3), label8, 0, 1, 0, 1,
			  GTK_FILL, GTK_EXPAND | GTK_FILL, 0, 0);
	gtk_misc_set_alignment (GTK_MISC (label8), 0, 0.5);


	hseparator1 = gtk_hseparator_new ();
	gtk_widget_show (hseparator1);
	gtk_box_pack_start (GTK_BOX (vbox2), hseparator1, FALSE, TRUE, 8);


	/* OK, Apply & Cancel buttons. */
	hbuttonbox1 = gtk_hbutton_box_new ();
	gtk_widget_show (hbuttonbox1);
	gtk_box_pack_start (GTK_BOX (vbox2), hbuttonbox1, FALSE, TRUE, 0);
	gtk_button_box_set_layout (GTK_BUTTON_BOX (hbuttonbox1), GTK_BUTTONBOX_END);
	gtk_box_set_spacing (GTK_BOX (hbuttonbox1), 8);

	menued->cancel_button = gtk_button_new_from_stock (GTK_STOCK_CANCEL);
	gtk_widget_show (menued->cancel_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox1), menued->cancel_button);
	GTK_WIDGET_SET_FLAGS (menued->cancel_button, GTK_CAN_DEFAULT);

	menued->apply_button = gtk_button_new_from_stock (GTK_STOCK_APPLY);
	gtk_widget_show (menued->apply_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox1), menued->apply_button);
	GTK_WIDGET_SET_FLAGS (menued->apply_button, GTK_CAN_DEFAULT);

	menued->ok_button = gtk_button_new_from_stock (GTK_STOCK_OK);
	gtk_widget_show (menued->ok_button);
	gtk_container_add (GTK_CONTAINER (hbuttonbox1), menued->ok_button);
	GTK_WIDGET_SET_FLAGS (menued->ok_button, GTK_CAN_DEFAULT);
	gtk_widget_grab_default (menued->ok_button);


	/* Now set up all the signal handlers. */
	gtk_signal_connect_after (GTK_OBJECT (menued->normal_radiobutton), "toggled",
				  GTK_SIGNAL_FUNC (on_radiobutton_toggled), NULL);
	gtk_signal_connect_after (GTK_OBJECT (menued->check_radiobutton), "toggled",
				  GTK_SIGNAL_FUNC (on_radiobutton_toggled), NULL);
	gtk_signal_connect_after (GTK_OBJECT (menued->radio_radiobutton), "toggled",
				  GTK_SIGNAL_FUNC (on_radiobutton_toggled), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->state_togglebutton), "toggled",
			    GTK_SIGNAL_FUNC (on_state_button_toggled), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->accel_ctrl_checkbutton), "toggled",
			    GTK_SIGNAL_FUNC (on_checkbutton_toggled), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->accel_shift_checkbutton), "toggled",
			    GTK_SIGNAL_FUNC (on_checkbutton_toggled), NULL);
	gtk_signal_connect (GTK_OBJECT (menued->accel_alt_checkbutton), "toggled",
			    GTK_SIGNAL_FUNC (on_checkbutton_toggled), NULL);

	gtk_signal_connect (GTK_OBJECT (menued->ok_button), "clicked",
			    GTK_SIGNAL_FUNC (on_menu_editor_ok), menued);
	gtk_signal_connect (GTK_OBJECT (menued->apply_button), "clicked",
			    GTK_SIGNAL_FUNC (on_menu_editor_apply), menued);
	gtk_signal_connect (GTK_OBJECT (menued->cancel_button), "clicked",
			    GTK_SIGNAL_FUNC (on_menu_editor_close), menued);

	set_interface_state (menued);
}

/* This checks if the given icon_name is a stock ID, and if it is it returns
   the text to display instead. If not, it returns icon_name. */
static gchar *
get_icon_name_from_stock_id (GladeMenuEditor *menued, gchar *icon_name)
{
	GtkStockItem item;

	if (icon_name && gtk_stock_lookup (icon_name, &item))
		return item.label;
	else
		return icon_name;
}

/* This recursively adds menus. */
static void
set_submenu (GladeMenuEditor *menued,
	     GtkMenuShell    *menu,
	     gint	      level)
{
#if 0
	GbMenuItemData *item;
	GtkWidget *menuitem, *label;
	GList *child, *tmp_list;
	/*** GladeWidgetData *wdata; ***/
	gchar *icon_name;
	GList *groups = NULL;

	child = menu->children;
	while (child) {
		menuitem = GTK_WIDGET (child->data);

		if (!GTK_IS_MENU_ITEM (menuitem)) {
			g_warning ("Menu widget is not a menu item");
			child = child->next;
			continue;
		}
		/* FIXME: We can't handle tearoff menuitems at present. */
		if (GTK_IS_TEAROFF_MENU_ITEM (menuitem)) {
			child = child->next;
			continue;
		}

		wdata = (GladeWidgetData*) gtk_object_get_data (GTK_OBJECT(menuitem),
								GB_WIDGET_DATA_KEY);
		if (wdata == NULL) {
			g_warning ("Menu widget has no GladeWidgetData");
			child = child->next;
			continue;
		}

		item = g_new0 (GbMenuItemData, 1);

		if (menued->gnome_support) {
#ifdef USE_GNOME
			item->stock_item_index = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (menuitem), GladeMenuItemStockIndexKey));
#endif
		}
		else {
			gchar *stock_id;
			GSList *elem;
			gint idx;

			stock_id = gtk_object_get_data (GTK_OBJECT (menuitem),
							GladeMenuItemStockIDKey);
			item->stock_item_index = 0;
			if (stock_id) {
				for (elem = menued->stock_items, idx = 1; elem;
				     elem = elem->next, idx++) {
					if (!strcmp (elem->data, stock_id)) {
						item->stock_item_index = idx;
						break;
					}
				}
			}
		}

		item->name = g_strdup (gtk_widget_get_name (menuitem));
		item->label = NULL;
		item->handler = NULL;
		item->last_mod_time = 0;
		item->icon = NULL;

		/* If it isn't a stock item, we check if it has an icon set. */
		if (item->stock_item_index == 0 && GTK_IS_IMAGE_MENU_ITEM (menuitem)) {
			GtkWidget *image;
			image = gtk_image_menu_item_get_image (GTK_IMAGE_MENU_ITEM (menuitem));
			if (image) {
				icon_name = gtk_object_get_data (GTK_OBJECT (image), GladeIconKey);
				item->icon = g_strdup (get_icon_name_from_stock_id (menued, icon_name));
			}
		}

		item->tooltip = g_strdup (wdata->tooltip);
		item->type = GB_MENU_ITEM_NORMAL;
		item->active = FALSE;
		item->group = NULL;
		item->modifiers = 0;
		item->key = NULL;
		item->level = level;
		item->generate_name = FALSE;
		item->generate_handler = FALSE;
		item->wdata = glade_widget_data_copy (wdata);

		if (GTK_IS_RADIO_MENU_ITEM (menuitem)) {
			item->type = GB_MENU_ITEM_RADIO;
			item->group = find_radio_group (GTK_RADIO_MENU_ITEM (menuitem),
							&groups, item);
		}
		else if (GTK_IS_CHECK_MENU_ITEM (menuitem))
			item->type = GB_MENU_ITEM_CHECK;

		label = GTK_BIN (menuitem)->child;
		if (label && GTK_IS_LABEL (label))
			item->label = glade_util_get_label_text (label);

		if (GTK_IS_CHECK_MENU_ITEM (menuitem)
		    && GTK_CHECK_MENU_ITEM (menuitem)->active)
			item->active = TRUE;
	
		/* Find 'activate' handler in widget data. */
#if 0
		/*** ***/
		tmp_list = item->wdata->signals;
		while (tmp_list) {
			GladeSignal *signal = (GladeSignal *) tmp_list->data;
			if (!strcmp (signal->name, "activate")) {
				item->handler = g_strdup (signal->handler);
				item->last_mod_time = signal->last_modification_time;

				/* Remove the signal from our copy of the GladeWidgetData. */
				glade_widget_data_free_signal (signal);
				item->wdata->signals = g_list_remove (item->wdata->signals,
								      signal);
				break;
			}
			tmp_list = tmp_list->next;
		}
#endif
		/* Find 'activate' accelerator in widget data. */
		tmp_list = item->wdata->accelerators;
		while (tmp_list) {
			GladeAccelerator *accel = (GladeAccelerator *) tmp_list->data;
			if (!strcmp (accel->signal, "activate")) {
				item->key = g_strdup (accel->key);
				item->modifiers = accel->modifiers;

				/* Remove the accel from our copy of the GladeWidgetData. */
				glade_widget_data_free_accel (accel);
				item->wdata->accelerators = g_list_remove (item->wdata->accelerators,
									   accel);
				break;
			}
			tmp_list = tmp_list->next;
		}


		insert_item (GTK_CLIST (menued->clist), item, -1);

		if (GTK_MENU_ITEM (menuitem)->submenu) {
			set_submenu (menued,
				     GTK_MENU_SHELL (GTK_MENU_ITEM (menuitem)->submenu),
				     level + 1);
		}

		child = child->next;
	}

	g_list_free (groups);
#endif
}

static void
glade_menu_editor_on_menu_destroyed (GtkWidget *menu, GtkWidget *menued)
{
	gtk_widget_destroy (menued);
}

/**
 * This converts the given menu/menubar into the list of items displayed in
 * the menu editor.
 */
static void
glade_menu_editor_set_menu (GladeMenuEditor *menued, GtkMenuShell *menu)
{
	/* First clear any current items and radio groups. */
	glade_menu_editor_reset (menued);

	/* Connect to the destroy signal of the menu widget, so we can destroy the
	   menu editor when the widget is destroyed. */
	menued->menu_destroy_handler_id = gtk_signal_connect (GTK_OBJECT (menu),
							      "destroy",
							      GTK_SIGNAL_FUNC (glade_menu_editor_on_menu_destroyed),
							      menued);

	/* Now add each of the menus/menuitems in the given menu. */
	menued->menu = menu;

	set_submenu (menued, menu, 0);
}

/**
 * glade_menu_editor_new:
 * @project:
 * @menu:
 *
 * This creates a menu editor to edit the given menubar or popup menu in
 * the given project. When the user selects the 'OK' or 'Apply' buttons,
 * the menu widget will be updated. If the menu is destroyed, the menu editor
 * is automatically destroyed as well.
 *
 * Returns: a new #GtkMenuEditor, cast as a #GtkWidget
 */
GtkWidget*
glade_menu_editor_new (GladeProject *project, GtkMenuShell *menu)
{
	GladeMenuEditor *menued;

	menued = gtk_type_new (glade_menu_editor_get_type ());
	glade_menu_editor_construct (menued, project);
	glade_menu_editor_set_menu (menued, menu);

	return GTK_WIDGET (menued);
}

/* This clears the clist, freeing all GbMenuItemDatas, and resets the radio
   group combo, freeing all the current radio groups. */
static void
glade_menu_editor_reset (GladeMenuEditor *menued)
{
	GbMenuItemData *item;
	gint i;

	/* Disconnect our destroy handler on the menu widget. */
	if (menued->menu) {
		gtk_signal_disconnect (GTK_OBJECT (menued->menu),
				       menued->menu_destroy_handler_id);
		menued->menu = NULL;
	}

	for (i = 0; i < GTK_CLIST (menued->clist)->rows; i++) {
		item = (GbMenuItemData *) gtk_clist_get_row_data (GTK_CLIST (menued->clist), i);
		glade_menu_editor_free_item (item);

	}
	gtk_clist_clear (GTK_CLIST (menued->clist));

	gtk_list_clear_items (GTK_LIST (GTK_COMBO (menued->group_combo)->list),
			      0, -1);
}