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
|
/*
* Copyright (C) 2002 JoaquÃn Cuenca Abela
*
* 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Authors:
* JoaquÃn Cuenca Abela <e98cuenc@yahoo.com>
* Archit Baweja <bighead@users.sourceforge.net>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
/**
* SECTION:glade-command
* @Short_Description: An event filter to implement the Undo/Redo stack.
*
* The Glade Command api allows us to view user actions as items and execute
* and undo those items; each #GladeProject has its own Undo/Redo stack.
*/
#include <gtk/gtk.h>
#include <string.h>
#include <glib/gi18n-lib.h>
#include "glade.h"
#include "glade-project.h"
#include "glade-xml-utils.h"
#include "glade-widget.h"
#include "glade-palette.h"
#include "glade-command.h"
#include "glade-property.h"
#include "glade-property-class.h"
#include "glade-debug.h"
#include "glade-placeholder.h"
#include "glade-clipboard.h"
#include "glade-signal.h"
#include "glade-app.h"
#include "glade-name-context.h"
struct _GladeCommandPrivate
{
GladeProject *project; /* The project this command is created for */
gchar *description; /* a string describing the command.
* It's used in the undo/redo menu entry.
*/
gint group_id; /* If this is part of a command group, this is
* the group id (id is needed only to ensure that
* consecutive groups dont get merged).
*/
};
/* Concerning placeholders: we do not hold any reference to placeholders,
* placeholders that are supplied by the backend are not reffed, placeholders
* that are created by glade-command are temporarily owned by glade-command
* untill they are added to a container; in which case it belongs to GTK+
* and the backend (but I prefer to think of it as the backend).
*/
typedef struct
{
GladeWidget *widget;
GladeWidget *parent;
GList *reffed;
GladePlaceholder *placeholder;
gboolean props_recorded;
GList *pack_props;
gchar *special_type;
gulong handler_id;
} CommandData;
/* Group description used for the current group
*/
static gchar *gc_group_description = NULL;
/* Use an id to avoid grouping together consecutive groups
*/
static gint gc_group_id = 1;
/* Groups can be grouped together, push/pop must balance (duh!)
*/
static gint gc_group_depth = 0;
G_DEFINE_TYPE_WITH_PRIVATE (GladeCommand, glade_command, G_TYPE_OBJECT)
static void
glade_command_finalize (GObject *obj)
{
GladeCommand *cmd = (GladeCommand *) obj;
g_return_if_fail (cmd != NULL);
if (cmd->priv->description)
g_free (cmd->priv->description);
/* Call the base class dtor */
(*G_OBJECT_CLASS (glade_command_parent_class)->finalize) (obj);
}
static gboolean
glade_command_unifies_impl (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
return FALSE;
}
static void
glade_command_collapse_impl (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
g_return_if_reached ();
}
static void
glade_command_init (GladeCommand *command)
{
command->priv = glade_command_get_instance_private (command);
}
static void
glade_command_class_init (GladeCommandClass *klass)
{
GObjectClass *object_class;
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = glade_command_finalize;
klass->undo = NULL;
klass->execute = NULL;
klass->unifies = glade_command_unifies_impl;
klass->collapse = glade_command_collapse_impl;
}
/* Macros for defining the derived command types */
#define MAKE_TYPE(func, type, parent) \
GType \
func ## _get_type (void) \
{ \
static GType cmd_type = 0; \
\
if (!cmd_type) \
{ \
static const GTypeInfo info = \
{ \
sizeof (type ## Class), \
(GBaseInitFunc) NULL, \
(GBaseFinalizeFunc) NULL, \
(GClassInitFunc) func ## _class_init, \
(GClassFinalizeFunc) NULL, \
NULL, \
sizeof (type), \
0, \
(GInstanceInitFunc) NULL \
}; \
\
cmd_type = g_type_register_static (parent, #type, &info, 0); \
} \
\
return cmd_type; \
} \
#define GLADE_MAKE_COMMAND(type, func) \
static gboolean \
func ## _undo (GladeCommand *me); \
static gboolean \
func ## _execute (GladeCommand *me); \
static void \
func ## _finalize (GObject *object); \
static gboolean \
func ## _unifies (GladeCommand *this_cmd, GladeCommand *other_cmd); \
static void \
func ## _collapse (GladeCommand *this_cmd, GladeCommand *other_cmd); \
static void \
func ## _class_init (gpointer parent_tmp, gpointer notused) \
{ \
GladeCommandClass *parent = parent_tmp; \
GObjectClass* object_class; \
object_class = G_OBJECT_CLASS (parent); \
parent->undo = func ## _undo; \
parent->execute = func ## _execute; \
parent->unifies = func ## _unifies; \
parent->collapse = func ## _collapse; \
object_class->finalize = func ## _finalize; \
} \
typedef struct { \
GladeCommandClass cmd; \
} type ## Class; \
static MAKE_TYPE(func, type, GLADE_TYPE_COMMAND)
G_CONST_RETURN gchar *
glade_command_description (GladeCommand *command)
{
g_return_val_if_fail (GLADE_IS_COMMAND (command), NULL);
return command->priv->description;
}
gint
glade_command_group_id (GladeCommand *command)
{
g_return_val_if_fail (GLADE_IS_COMMAND (command), -1);
return command->priv->group_id;
}
/**
* glade_command_execute:
* @command: A #GladeCommand
*
* Executes @command
*
* Returns: whether the command was successfully executed
*/
gboolean
glade_command_execute (GladeCommand *command)
{
g_return_val_if_fail (GLADE_IS_COMMAND (command), FALSE);
return GLADE_COMMAND_GET_CLASS (command)->execute (command);
}
/**
* glade_command_undo:
* @command: A #GladeCommand
*
* Undo the effects of @command
*
* Returns: whether the command was successfully reversed
*/
gboolean
glade_command_undo (GladeCommand *command)
{
g_return_val_if_fail (GLADE_IS_COMMAND (command), FALSE);
return GLADE_COMMAND_GET_CLASS (command)->undo (command);
}
/**
* glade_command_unifies:
* @command: A #GladeCommand
* @other: another #GladeCommand
*
* Checks whether @command and @other can be unified
* to make one single command.
*
* Returns: whether they can be unified.
*/
gboolean
glade_command_unifies (GladeCommand *command, GladeCommand *other)
{
g_return_val_if_fail (command, FALSE);
/* Cannot unify with a part of a command group.
* Unify atomic commands only
*/
if (command->priv->group_id != 0 || (other && other->priv->group_id != 0))
return FALSE;
return GLADE_COMMAND_GET_CLASS (command)->unifies (command, other);
}
/**
* glade_command_collapse:
* @command: A #GladeCommand
* @other: another #GladeCommand
*
* Merges @other into @command, so that @command now
* covers both commands and @other can be dispensed with.
*/
void
glade_command_collapse (GladeCommand *command, GladeCommand *other)
{
g_return_if_fail (command);
GLADE_COMMAND_GET_CLASS (command)->collapse (command, other);
}
/**
* glade_command_push_group:
* @fmt: The collective desctiption of the command group.
* only the description of the first group on the
* stack is used when embedding groups.
* @...: args to the format string.
*
* Marks the begining of a group.
*/
void
glade_command_push_group (const gchar *fmt, ...)
{
va_list args;
g_return_if_fail (fmt);
/* Only use the description for the first group.
*/
if (gc_group_depth++ == 0)
{
va_start (args, fmt);
gc_group_description = g_strdup_vprintf (fmt, args);
va_end (args);
}
}
/**
* glade_command_pop_group:
*
* Mark the end of a command group.
*/
void
glade_command_pop_group (void)
{
if (gc_group_depth-- == 1)
{
gc_group_description = (g_free (gc_group_description), NULL);
gc_group_id++;
}
if (gc_group_depth < 0)
g_critical ("Unbalanced group stack detected in %s\n", G_STRFUNC);
}
gint
glade_command_get_group_depth (void)
{
return gc_group_depth;
}
static void
glade_command_check_group (GladeCommand *cmd)
{
g_return_if_fail (GLADE_IS_COMMAND (cmd));
if (gc_group_description)
{
cmd->priv->description =
(g_free (cmd->priv->description), g_strdup (gc_group_description));
cmd->priv->group_id = gc_group_id;
}
}
/****************************************************/
/******* GLADE_COMMAND_PROPERTY_ENABLED *******/
/****************************************************/
typedef struct
{
GladeCommand parent;
GladeProperty *property;
gboolean old_enabled;
gboolean new_enabled;
} GladeCommandPropertyEnabled;
/* standard macros */
GLADE_MAKE_COMMAND (GladeCommandPropertyEnabled, glade_command_property_enabled);
#define GLADE_COMMAND_PROPERTY_ENABLED_TYPE (glade_command_property_enabled_get_type ())
#define GLADE_COMMAND_PROPERTY_ENABLED(o) (G_TYPE_CHECK_INSTANCE_CAST \
((o), GLADE_COMMAND_PROPERTY_ENABLED_TYPE, \
GladeCommandPropertyEnabled))
#define GLADE_COMMAND_PROPERTY_ENABLED_CLASS(k) (G_TYPE_CHECK_CLASS_CAST \
((k), GLADE_COMMAND_PROPERTY_ENABLED_TYPE, \
GladeCommandPropertyEnabledClass))
#define GLADE_IS_COMMAND_PROPERTY_ENABLED(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_PROPERTY_ENABLED_TYPE))
#define GLADE_IS_COMMAND_PROPERTY_ENABLED_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_PROPERTY_ENABLED_TYPE))
static gboolean
glade_command_property_enabled_execute (GladeCommand *cmd)
{
GladeCommandPropertyEnabled *me = GLADE_COMMAND_PROPERTY_ENABLED (cmd);
glade_property_set_enabled (me->property, me->new_enabled);
return TRUE;
}
static gboolean
glade_command_property_enabled_undo (GladeCommand *cmd)
{
GladeCommandPropertyEnabled *me = GLADE_COMMAND_PROPERTY_ENABLED (cmd);
glade_property_set_enabled (me->property, me->old_enabled);
return TRUE;
}
static void
glade_command_property_enabled_finalize (GObject *obj)
{
GladeCommandPropertyEnabled *me;
g_return_if_fail (GLADE_IS_COMMAND_PROPERTY_ENABLED (obj));
me = GLADE_COMMAND_PROPERTY_ENABLED (obj);
g_object_unref (me->property);
glade_command_finalize (obj);
}
static gboolean
glade_command_property_enabled_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandPropertyEnabled *cmd1;
GladeCommandPropertyEnabled *cmd2;
if (!other_cmd)
{
if (GLADE_IS_COMMAND_PROPERTY_ENABLED (this_cmd))
{
cmd1 = (GladeCommandPropertyEnabled *) this_cmd;
return (cmd1->old_enabled == cmd1->new_enabled);
}
return FALSE;
}
if (GLADE_IS_COMMAND_PROPERTY_ENABLED (this_cmd) &&
GLADE_IS_COMMAND_PROPERTY_ENABLED (other_cmd))
{
cmd1 = GLADE_COMMAND_PROPERTY_ENABLED (this_cmd);
cmd2 = GLADE_COMMAND_PROPERTY_ENABLED (other_cmd);
return (cmd1->property == cmd2->property);
}
return FALSE;
}
static void
glade_command_property_enabled_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
GladeCommandPropertyEnabled *this = GLADE_COMMAND_PROPERTY_ENABLED (this_cmd);
GladeCommandPropertyEnabled *other = GLADE_COMMAND_PROPERTY_ENABLED (other_cmd);
GladeWidget *widget;
GladePropertyClass *pclass;
this->new_enabled = other->new_enabled;
widget = glade_property_get_widget (this->property);
pclass = glade_property_get_class (this->property);
g_free (this_cmd->priv->description);
if (this->new_enabled)
this_cmd->priv->description =
g_strdup_printf (_("Enabling property %s on widget %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget));
else
this_cmd->priv->description =
g_strdup_printf (_("Disabling property %s on widget %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget));
}
/**
* glade_command_set_property_enabled:
* @property: An optional #GladeProperty
* @enabled: Whether the property should be enabled
*
* Enables or disables @property.
*
* @property must be an optional property.
*/
void
glade_command_set_property_enabled (GladeProperty *property,
gboolean enabled)
{
GladeCommandPropertyEnabled *me;
GladeCommand *cmd;
GladeWidget *widget;
GladePropertyClass *pclass;
gboolean old_enabled;
/* Sanity checks */
g_return_if_fail (GLADE_IS_PROPERTY (property));
widget = glade_property_get_widget (property);
g_return_if_fail (GLADE_IS_WIDGET (widget));
/* Only applies to optional properties */
pclass = glade_property_get_class (property);
g_return_if_fail (glade_property_class_optional (pclass));
/* Fetch current state */
old_enabled = glade_property_get_enabled (property);
/* Avoid useless command */
if (old_enabled == enabled)
return;
me = g_object_new (GLADE_COMMAND_PROPERTY_ENABLED_TYPE, NULL);
cmd = GLADE_COMMAND (me);
cmd->priv->project = glade_widget_get_project (widget);
me->property = g_object_ref (property);
me->new_enabled = enabled;
me->old_enabled = old_enabled;
if (enabled)
cmd->priv->description =
g_strdup_printf (_("Enabling property %s on widget %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget));
else
cmd->priv->description =
g_strdup_printf (_("Disabling property %s on widget %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget));
glade_command_check_group (GLADE_COMMAND (me));
if (glade_command_property_enabled_execute (GLADE_COMMAND (me)))
glade_project_push_undo (cmd->priv->project, cmd);
else
g_object_unref (G_OBJECT (me));
}
/**************************************************/
/******* GLADE_COMMAND_SET_PROPERTY *******/
/**************************************************/
/* create a new GladeCommandSetProperty class. Objects of this class will
* encapsulate a "set property" operation */
typedef struct
{
GladeCommand parent;
gboolean set_once;
gboolean undo;
GList *sdata;
} GladeCommandSetProperty;
/* standard macros */
GLADE_MAKE_COMMAND (GladeCommandSetProperty, glade_command_set_property);
#define GLADE_COMMAND_SET_PROPERTY_TYPE (glade_command_set_property_get_type ())
#define GLADE_COMMAND_SET_PROPERTY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_SET_PROPERTY_TYPE, GladeCommandSetProperty))
#define GLADE_COMMAND_SET_PROPERTY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_SET_PROPERTY_TYPE, GladeCommandSetPropertyClass))
#define GLADE_IS_COMMAND_SET_PROPERTY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_SET_PROPERTY_TYPE))
#define GLADE_IS_COMMAND_SET_PROPERTY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_SET_PROPERTY_TYPE))
/* Undo the last "set property command" */
static gboolean
glade_command_set_property_undo (GladeCommand *cmd)
{
return glade_command_set_property_execute (cmd);
}
/*
* Execute the set property command and revert it. IE, after the execution of
* this function cmd will point to the undo action
*/
static gboolean
glade_command_set_property_execute (GladeCommand *cmd)
{
GladeCommandSetProperty *me = (GladeCommandSetProperty *) cmd;
GList *l;
gboolean success;
gboolean retval = FALSE;
g_return_val_if_fail (me != NULL, FALSE);
if (me->set_once != FALSE)
glade_property_push_superuser ();
for (l = me->sdata; l; l = l->next)
{
GValue new_value = { 0, };
GCSetPropData *sdata = l->data;
GladePropertyClass *pclass = glade_property_get_class (sdata->property);
GladeWidget *widget = glade_property_get_widget (sdata->property);
g_value_init (&new_value, G_VALUE_TYPE (sdata->new_value));
if (me->undo)
g_value_copy (sdata->old_value, &new_value);
else
g_value_copy (sdata->new_value, &new_value);
#ifdef GLADE_ENABLE_DEBUG
if (glade_get_debug_flags () & GLADE_DEBUG_COMMANDS)
{
gchar *str =
glade_widget_adaptor_string_from_value
(glade_property_class_get_adaptor (pclass), pclass, &new_value);
g_print ("Setting %s property of %s to %s (sumode: %d)\n",
glade_property_class_id (pclass),
glade_widget_get_name (widget),
str, glade_property_superuser ());
g_free (str);
}
#endif
/* Packing properties need to be refreshed here since
* they are reset when they get added to containers.
*/
if (glade_property_class_get_is_packing (pclass))
{
GladeProperty *tmp_prop;
tmp_prop = glade_widget_get_pack_property (widget, glade_property_class_id (pclass));
if (sdata->property != tmp_prop)
{
g_object_unref (sdata->property);
sdata->property = g_object_ref (tmp_prop);
}
}
success = glade_property_set_value (sdata->property, &new_value);
retval = retval || success;
if (!me->set_once && success)
{
/* If some verify functions didnt pass on
* the first go.. we need to record the actual
* properties here. XXX should be able to use glade_property_get_value() here
*/
g_value_copy (glade_property_inline_value (sdata->property), sdata->new_value);
}
g_value_unset (&new_value);
}
if (me->set_once != FALSE)
glade_property_pop_superuser ();
me->set_once = TRUE;
me->undo = !me->undo;
return retval;
}
static void
glade_command_set_property_finalize (GObject *obj)
{
GladeCommandSetProperty *me;
GList *l;
me = GLADE_COMMAND_SET_PROPERTY (obj);
for (l = me->sdata; l; l = l->next)
{
GCSetPropData *sdata = l->data;
if (sdata->property)
g_object_unref (G_OBJECT (sdata->property));
if (sdata->old_value)
{
if (G_VALUE_TYPE (sdata->old_value) != 0)
g_value_unset (sdata->old_value);
g_free (sdata->old_value);
}
if (G_VALUE_TYPE (sdata->new_value) != 0)
g_value_unset (sdata->new_value);
g_free (sdata->new_value);
}
glade_command_finalize (obj);
}
static gboolean
glade_command_set_property_unifies (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
GladeCommandSetProperty *cmd1, *cmd2;
GladePropertyClass *pclass1, *pclass2;
GCSetPropData *pdata1, *pdata2;
GladeWidget *widget1, *widget2;
GList *list, *l;
if (!other_cmd)
{
if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd))
{
cmd1 = (GladeCommandSetProperty *) this_cmd;
for (list = cmd1->sdata; list; list = list->next)
{
pdata1 = list->data;
pclass1 = glade_property_get_class (pdata1->property);
if (glade_property_class_compare (pclass1,
pdata1->old_value,
pdata1->new_value))
return FALSE;
}
return TRUE;
}
return FALSE;
}
if (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) &&
GLADE_IS_COMMAND_SET_PROPERTY (other_cmd))
{
cmd1 = (GladeCommandSetProperty *) this_cmd;
cmd2 = (GladeCommandSetProperty *) other_cmd;
if (g_list_length (cmd1->sdata) != g_list_length (cmd2->sdata))
return FALSE;
for (list = cmd1->sdata; list; list = list->next)
{
pdata1 = list->data;
pclass1 = glade_property_get_class (pdata1->property);
widget1 = glade_property_get_widget (pdata1->property);
for (l = cmd2->sdata; l; l = l->next)
{
pdata2 = l->data;
pclass2 = glade_property_get_class (pdata2->property);
widget2 = glade_property_get_widget (pdata2->property);
if (widget1 == widget2 &&
glade_property_class_match (pclass1, pclass2))
break;
}
/* If both lists are the same length, and one class type
* is not found in the other list, then we cant unify.
*/
if (l == NULL)
return FALSE;
}
return TRUE;
}
return FALSE;
}
static void
glade_command_set_property_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
GladeCommandSetProperty *cmd1, *cmd2;
GCSetPropData *pdata1, *pdata2;
GladePropertyClass *pclass1, *pclass2;
GList *list, *l;
g_return_if_fail (GLADE_IS_COMMAND_SET_PROPERTY (this_cmd) &&
GLADE_IS_COMMAND_SET_PROPERTY (other_cmd));
cmd1 = (GladeCommandSetProperty *) this_cmd;
cmd2 = (GladeCommandSetProperty *) other_cmd;
for (list = cmd1->sdata; list; list = list->next)
{
pdata1 = list->data;
pclass1 = glade_property_get_class (pdata1->property);
for (l = cmd2->sdata; l; l = l->next)
{
pdata2 = l->data;
pclass2 = glade_property_get_class (pdata2->property);
if (glade_property_class_match (pclass1, pclass2))
{
/* Merge the GCSetPropData structs manually here
*/
g_value_copy (pdata2->new_value, pdata1->new_value);
break;
}
}
}
/* Set the description
*/
g_free (this_cmd->priv->description);
this_cmd->priv->description = other_cmd->priv->description;
other_cmd->priv->description = NULL;
}
#define MAX_UNDO_MENU_ITEM_VALUE_LEN 10
static gchar *
glade_command_set_property_description (GladeCommandSetProperty *me)
{
GCSetPropData *sdata;
gchar *description = NULL;
gchar *value_name;
GladePropertyClass *pclass;
GladeWidget *widget;
g_assert (me->sdata);
if (g_list_length (me->sdata) > 1)
description = g_strdup_printf (_("Setting multiple properties"));
else
{
sdata = me->sdata->data;
pclass = glade_property_get_class (sdata->property);
widget = glade_property_get_widget (sdata->property);
value_name = glade_widget_adaptor_string_from_value
(glade_property_class_get_adaptor (pclass), pclass, sdata->new_value);
if (!value_name || strlen (value_name) > MAX_UNDO_MENU_ITEM_VALUE_LEN
|| strchr (value_name, '_'))
{
description = g_strdup_printf (_("Setting %s of %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget));
}
else
{
description = g_strdup_printf (_("Setting %s of %s to %s"),
glade_property_class_get_name (pclass),
glade_widget_get_name (widget),
value_name);
}
g_free (value_name);
}
return description;
}
/**
* glade_command_set_properties_list:
*
* @props (element-type GladeProperty): List of #GladeProperty
*/
void
glade_command_set_properties_list (GladeProject *project, GList *props)
{
GladeCommandSetProperty *me;
GladeCommand *cmd;
GCSetPropData *sdata;
GList *list;
gboolean success;
gboolean multiple;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_return_if_fail (props);
me = (GladeCommandSetProperty *)
g_object_new (GLADE_COMMAND_SET_PROPERTY_TYPE, NULL);
cmd = GLADE_COMMAND (me);
cmd->priv->project = project;
/* Ref all props */
for (list = props; list; list = list->next)
{
sdata = list->data;
g_object_ref (G_OBJECT (sdata->property));
}
me->sdata = props;
cmd->priv->description = glade_command_set_property_description (me);
multiple = g_list_length (me->sdata) > 1;
if (multiple)
glade_command_push_group ("%s", cmd->priv->description);
glade_command_check_group (GLADE_COMMAND (me));
/* Push onto undo stack only if it executes successfully. */
success = glade_command_set_property_execute (cmd);
if (success)
glade_project_push_undo (cmd->priv->project, cmd);
else
g_object_unref (G_OBJECT (me));
if (multiple)
glade_command_pop_group ();
}
void
glade_command_set_properties (GladeProperty *property,
const GValue *old_value,
const GValue *new_value,
...)
{
GCSetPropData *sdata;
GladeProperty *prop;
GladeWidget *widget;
GladeProject *project;
GValue *ovalue, *nvalue;
GList *list = NULL;
va_list vl;
g_return_if_fail (GLADE_IS_PROPERTY (property));
/* Add first set */
sdata = g_new (GCSetPropData, 1);
sdata->property = property;
sdata->old_value = g_new0 (GValue, 1);
sdata->new_value = g_new0 (GValue, 1);
g_value_init (sdata->old_value, G_VALUE_TYPE (old_value));
g_value_init (sdata->new_value, G_VALUE_TYPE (new_value));
g_value_copy (old_value, sdata->old_value);
g_value_copy (new_value, sdata->new_value);
list = g_list_prepend (list, sdata);
va_start (vl, new_value);
while ((prop = va_arg (vl, GladeProperty *)) != NULL)
{
ovalue = va_arg (vl, GValue *);
nvalue = va_arg (vl, GValue *);
g_assert (GLADE_IS_PROPERTY (prop));
g_assert (G_IS_VALUE (ovalue));
g_assert (G_IS_VALUE (nvalue));
sdata = g_new (GCSetPropData, 1);
sdata->property = g_object_ref (G_OBJECT (prop));
sdata->old_value = g_new0 (GValue, 1);
sdata->new_value = g_new0 (GValue, 1);
g_value_init (sdata->old_value, G_VALUE_TYPE (ovalue));
g_value_init (sdata->new_value, G_VALUE_TYPE (nvalue));
g_value_copy (ovalue, sdata->old_value);
g_value_copy (nvalue, sdata->new_value);
list = g_list_prepend (list, sdata);
}
va_end (vl);
widget = glade_property_get_widget (property);
project = glade_widget_get_project (widget);
glade_command_set_properties_list (project, list);
}
void
glade_command_set_property_value (GladeProperty *property, const GValue *pvalue)
{
/* Dont generate undo/redo items for unchanging property values.
*/
if (glade_property_equals_value (property, pvalue))
return;
glade_command_set_properties (property, glade_property_inline_value (property), pvalue, NULL);
}
void
glade_command_set_property (GladeProperty * property, ...)
{
GValue *value;
va_list args;
g_return_if_fail (GLADE_IS_PROPERTY (property));
va_start (args, property);
value = glade_property_class_make_gvalue_from_vl (glade_property_get_class (property), args);
va_end (args);
glade_command_set_property_value (property, value);
}
/**************************************************/
/******* GLADE_COMMAND_SET_NAME *******/
/**************************************************/
/* create a new GladeCommandSetName class. Objects of this class will
* encapsulate a "set name" operation */
typedef struct
{
GladeCommand parent;
GladeWidget *widget;
gchar *old_name;
gchar *name;
} GladeCommandSetName;
/* standard macros */
GLADE_MAKE_COMMAND (GladeCommandSetName, glade_command_set_name);
#define GLADE_COMMAND_SET_NAME_TYPE (glade_command_set_name_get_type ())
#define GLADE_COMMAND_SET_NAME(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_SET_NAME_TYPE, GladeCommandSetName))
#define GLADE_COMMAND_SET_NAME_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_SET_NAME_TYPE, GladeCommandSetNameClass))
#define GLADE_IS_COMMAND_SET_NAME(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_SET_NAME_TYPE))
#define GLADE_IS_COMMAND_SET_NAME_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_SET_NAME_TYPE))
/* Undo the last "set name command" */
static gboolean
glade_command_set_name_undo (GladeCommand *cmd)
{
return glade_command_set_name_execute (cmd);
}
/*
* Execute the set name command and revert it. Ie, after the execution of this
* function cmd will point to the undo action
*/
static gboolean
glade_command_set_name_execute (GladeCommand *cmd)
{
GladeCommandSetName *me = GLADE_COMMAND_SET_NAME (cmd);
char *tmp;
g_return_val_if_fail (me != NULL, TRUE);
g_return_val_if_fail (me->widget != NULL, TRUE);
g_return_val_if_fail (me->name != NULL, TRUE);
glade_project_set_widget_name (cmd->priv->project, me->widget, me->name);
tmp = me->old_name;
me->old_name = me->name;
me->name = tmp;
return TRUE;
}
static void
glade_command_set_name_finalize (GObject *obj)
{
GladeCommandSetName *me;
g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (obj));
me = GLADE_COMMAND_SET_NAME (obj);
g_free (me->old_name);
g_free (me->name);
glade_command_finalize (obj);
}
static gboolean
glade_command_set_name_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandSetName *cmd1;
GladeCommandSetName *cmd2;
if (!other_cmd)
{
if (GLADE_IS_COMMAND_SET_NAME (this_cmd))
{
cmd1 = (GladeCommandSetName *) this_cmd;
return (g_strcmp0 (cmd1->old_name, cmd1->name) == 0);
}
return FALSE;
}
if (GLADE_IS_COMMAND_SET_NAME (this_cmd) &&
GLADE_IS_COMMAND_SET_NAME (other_cmd))
{
cmd1 = GLADE_COMMAND_SET_NAME (this_cmd);
cmd2 = GLADE_COMMAND_SET_NAME (other_cmd);
return (cmd1->widget == cmd2->widget);
}
return FALSE;
}
static void
glade_command_set_name_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
GladeCommandSetName *nthis = GLADE_COMMAND_SET_NAME (this_cmd);
GladeCommandSetName *nother = GLADE_COMMAND_SET_NAME (other_cmd);
g_return_if_fail (GLADE_IS_COMMAND_SET_NAME (this_cmd) &&
GLADE_IS_COMMAND_SET_NAME (other_cmd));
g_free (nthis->old_name);
nthis->old_name = nother->old_name;
nother->old_name = NULL;
g_free (this_cmd->priv->description);
this_cmd->priv->description =
g_strdup_printf (_("Renaming %s to %s"), nthis->name, nthis->old_name);
}
/* this function takes the ownership of name */
void
glade_command_set_name (GladeWidget *widget, const gchar *name)
{
GladeCommandSetName *me;
GladeCommand *cmd;
g_return_if_fail (GLADE_IS_WIDGET (widget));
g_return_if_fail (name && name[0]);
/* Dont spam the queue with false name changes.
*/
if (!strcmp (glade_widget_get_name (widget), name))
return;
me = g_object_new (GLADE_COMMAND_SET_NAME_TYPE, NULL);
cmd = GLADE_COMMAND (me);
cmd->priv->project = glade_widget_get_project (widget);
me->widget = widget;
me->name = g_strdup (name);
me->old_name = g_strdup (glade_widget_get_name (widget));
cmd->priv->description =
g_strdup_printf (_("Renaming %s to %s"), me->old_name, me->name);
glade_command_check_group (GLADE_COMMAND (me));
if (glade_command_set_name_execute (GLADE_COMMAND (me)))
glade_project_push_undo (cmd->priv->project, cmd);
else
g_object_unref (G_OBJECT (me));
}
/******************************************************************************
*
* add/remove
*
* These canonical commands add/remove a widget list to/from the project.
*
*****************************************************************************/
typedef struct
{
GladeCommand parent;
GList *widgets;
gboolean add;
gboolean from_clipboard;
} GladeCommandAddRemove;
GLADE_MAKE_COMMAND (GladeCommandAddRemove, glade_command_add_remove);
#define GLADE_COMMAND_ADD_REMOVE_TYPE (glade_command_add_remove_get_type ())
#define GLADE_COMMAND_ADD_REMOVE(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_ADD_REMOVE_TYPE, GladeCommandAddRemove))
#define GLADE_COMMAND_ADD_REMOVE_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_ADD_REMOVE_TYPE, GladeCommandAddRemoveClass))
#define GLADE_IS_COMMAND_ADD_REMOVE(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_ADD_REMOVE_TYPE))
#define GLADE_IS_COMMAND_ADD_REMOVE_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_ADD_REMOVE_TYPE))
static void
glade_command_placeholder_destroyed (GtkWidget *object, CommandData *cdata)
{
if (GTK_WIDGET (cdata->placeholder) == object)
{
cdata->placeholder = NULL;
cdata->handler_id = 0;
}
}
static void
glade_command_placeholder_connect (CommandData *cdata,
GladePlaceholder *placeholder)
{
g_assert (cdata && cdata->placeholder == NULL);
/* Something like a no-op with no placeholder */
if ((cdata->placeholder = placeholder) == NULL)
return;
cdata->handler_id = g_signal_connect
(placeholder, "destroy",
G_CALLBACK (glade_command_placeholder_destroyed), cdata);
}
/**
* get_all_parentless_reffed_widgetst:
*
* @props (element-type GladeWidget) : List of #GladeWidget
* @return (element-type GladeWidget) : List of #GladeWidget
*/
static GList *
get_all_parentless_reffed_widgets (GList *reffed, GladeWidget *widget)
{
GList *children, *l, *list;
GladeWidget *child;
if ((list = glade_widget_get_parentless_reffed_widgets (widget)) != NULL)
reffed = g_list_concat (reffed, list);
children = glade_widget_get_children (widget);
for (l = children; l; l = l->next)
{
child = glade_widget_get_from_gobject (l->data);
reffed = get_all_parentless_reffed_widgets (reffed, child);
}
g_list_free (children);
return reffed;
}
/**
* glade_command_add:
* @widgets (element-type GladeWidget): a #Glist
* @parent: a #GladeWidget
* @placeholder: a #GladePlaceholder
* @pasting: whether we are pasting an existing widget or creating a new one.
*
* Performs an add command on all widgets in @widgets to @parent, possibly
* replacing @placeholder (note toplevels dont need a parent; the active project
* will be used when pasting toplevel objects).
* Pasted widgets will persist packing properties from thier cut/copy source
* while newly added widgets will prefer packing defaults.
*
*/
void
glade_command_add (GList *widgets,
GladeWidget *parent,
GladePlaceholder *placeholder,
GladeProject *project,
gboolean pasting)
{
GladeCommandAddRemove *me;
GladeCommand *cmd;
CommandData *cdata;
GladeWidget *widget = NULL;
GladeWidgetAdaptor *adaptor;
GList *l, *list, *children, *placeholders = NULL;
GtkWidget *child;
g_return_if_fail (widgets && widgets->data);
g_return_if_fail (parent == NULL || GLADE_IS_WIDGET (parent));
me = g_object_new (GLADE_COMMAND_ADD_REMOVE_TYPE, NULL);
cmd = GLADE_COMMAND (me);
me->add = TRUE;
me->from_clipboard = pasting;
/* Things can go wrong in this function if the dataset is inacurate,
* we make no real attempt here to recover, just g_critical() and
* fix the bugs as they pop up.
*/
widget = GLADE_WIDGET (widgets->data);
adaptor = glade_widget_get_adaptor (widget);
if (placeholder && GWA_IS_TOPLEVEL (adaptor) == FALSE)
cmd->priv->project = glade_placeholder_get_project (placeholder);
else
cmd->priv->project = project;
GLADE_COMMAND (me)->priv->description =
g_strdup_printf (_("Add %s"), g_list_length (widgets) == 1 ?
glade_widget_get_name (widget) : _("multiple"));
for (list = widgets; list && list->data; list = list->next)
{
widget = list->data;
cdata = g_new0 (CommandData, 1);
if (glade_widget_get_internal (widget))
g_critical ("Internal widget in Add");
adaptor = glade_widget_get_adaptor (widget);
/* Widget */
cdata->widget = g_object_ref (G_OBJECT (widget));
/* Parentless ref */
if ((cdata->reffed =
get_all_parentless_reffed_widgets (cdata->reffed, widget)) != NULL)
g_list_foreach (cdata->reffed, (GFunc) g_object_ref, NULL);
/* Parent */
if (parent == NULL)
cdata->parent = glade_widget_get_parent (widget);
else if (placeholder && GWA_IS_TOPLEVEL (adaptor) == FALSE)
cdata->parent = glade_placeholder_get_parent (placeholder);
else
cdata->parent = parent;
/* Placeholder */
if (placeholder != NULL && g_list_length (widgets) == 1)
{
glade_command_placeholder_connect (cdata, placeholder);
}
else if (cdata->parent &&
glade_widget_placeholder_relation (cdata->parent, widget))
{
if ((children =
glade_widget_adaptor_get_children (glade_widget_get_adaptor (cdata->parent),
glade_widget_get_object (cdata->parent))) != NULL)
{
for (l = children; l && l->data; l = l->next)
{
child = l->data;
/* Find a placeholder for this child */
if (GLADE_IS_PLACEHOLDER (child) &&
g_list_find (placeholders, child) == NULL)
{
placeholders = g_list_append (placeholders, child);
glade_command_placeholder_connect (cdata, GLADE_PLACEHOLDER (child));
break;
}
}
g_list_free (children);
}
}
me->widgets = g_list_prepend (me->widgets, cdata);
}
glade_command_check_group (GLADE_COMMAND (me));
/*
* Push it onto the undo stack only on success
*/
if (glade_command_add_remove_execute (cmd))
glade_project_push_undo (cmd->priv->project, cmd);
else
g_object_unref (G_OBJECT (me));
if (placeholders)
g_list_free (placeholders);
}
static void
glade_command_delete_prop_refs (GladeWidget *widget)
{
GladeProperty *property;
GList *refs, *l;
refs = glade_widget_list_prop_refs (widget);
for (l = refs; l; l = l->next)
{
property = l->data;
glade_command_set_property (property, NULL);
}
g_list_free (refs);
}
static void glade_command_remove (GList *widgets);
static void
glade_command_remove_locked (GladeWidget *widget, GList *reffed)
{
GList list = { 0, }, *widgets, *l;
GladeWidget *locked;
widgets = glade_widget_list_locked_widgets (widget);
for (l = widgets; l; l = l->next)
{
locked = l->data;
list.data = locked;
if (g_list_find (reffed, locked))
continue;
glade_command_unlock_widget (locked);
glade_command_remove (&list);
}
g_list_free (widgets);
}
/**
* glade_command_remove:
* @widgets (element-type GladeWidget): a #GList of #GladeWidgets
* @return_placeholders: whether or not to return a list of placehodlers
*
* Performs a remove command on all widgets in @widgets from @parent.
*/
static void
glade_command_remove (GList *widgets)
{
GladeCommandAddRemove *me;
GladeWidget *widget = NULL;
GladeWidget *lock;
CommandData *cdata;
GtkWidget *placeholder;
GList *list, *l;
g_return_if_fail (widgets != NULL);
/* internal children cannot be deleted. Notify the user. */
for (list = widgets; list && list->data; list = list->next)
{
widget = list->data;
lock = glade_widget_get_locker (widget);
if (glade_widget_get_internal (widget))
{
glade_util_ui_message (glade_app_get_window (),
GLADE_UI_WARN, NULL,
_
("You cannot remove a widget internal to a composite widget."));
return;
}
else if (lock)
{
glade_util_ui_message (glade_app_get_window (),
GLADE_UI_WARN, NULL,
_("%s is locked by %s, edit %s first."),
glade_widget_get_name (widget),
glade_widget_get_name (lock),
glade_widget_get_name (lock));
return;
}
}
me = g_object_new (GLADE_COMMAND_ADD_REMOVE_TYPE, NULL);
me->add = FALSE;
me->from_clipboard = FALSE;
GLADE_COMMAND (me)->priv->project = glade_widget_get_project (widget);
GLADE_COMMAND (me)->priv->description = g_strdup ("dummy");
if (g_list_length (widgets) == 1)
glade_command_push_group (_("Remove %s"),
glade_widget_get_name (GLADE_WIDGET (widgets->data)));
else
glade_command_push_group (_("Remove multiple"));
for (list = widgets; list && list->data; list = list->next)
{
widget = list->data;
cdata = g_new0 (CommandData, 1);
cdata->widget = g_object_ref (G_OBJECT (widget));
cdata->parent = glade_widget_get_parent (widget);
if ((cdata->reffed =
get_all_parentless_reffed_widgets (cdata->reffed, widget)) != NULL)
g_list_foreach (cdata->reffed, (GFunc) g_object_ref, NULL);
/* If we're removing the template widget, then we need to unset it as template */
if (glade_project_get_template (GLADE_COMMAND (me)->priv->project) == widget)
glade_command_set_project_template (GLADE_COMMAND (me)->priv->project, NULL);
/* Undoably unset any object properties that may point to the removed object */
glade_command_delete_prop_refs (widget);
/* Undoably unlock and remove any widgets locked by this widget */
glade_command_remove_locked (widget, cdata->reffed);
if (cdata->parent != NULL &&
glade_widget_placeholder_relation (cdata->parent, cdata->widget))
{
placeholder = glade_placeholder_new ();
glade_command_placeholder_connect
(cdata, GLADE_PLACEHOLDER (placeholder));
}
me->widgets = g_list_prepend (me->widgets, cdata);
/* Record packing props if not deleted from the clipboard */
if (me->from_clipboard == FALSE)
{
for (l = glade_widget_get_packing_properties (widget); l; l = l->next)
cdata->pack_props =
g_list_prepend (cdata->pack_props,
glade_property_dup (GLADE_PROPERTY (l->data),
cdata->widget));
}
}
g_assert (widget);
glade_command_check_group (GLADE_COMMAND (me));
if (glade_command_add_remove_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project,
GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
glade_command_pop_group ();
} /* end of glade_command_remove() */
static void
glade_command_transfer_props (GladeWidget *gnew, GList *saved_props)
{
GList *l;
for (l = saved_props; l; l = l->next)
{
GladeProperty *prop, *sprop = l->data;
GladePropertyClass *pclass = glade_property_get_class (sprop);
prop = glade_widget_get_pack_property (gnew, glade_property_class_id (pclass));
if (prop && glade_property_class_transfer_on_paste (pclass) &&
glade_property_class_match (glade_property_get_class (prop), pclass))
glade_property_set_value (prop, glade_property_inline_value (sprop));
}
}
static gboolean
glade_command_add_execute (GladeCommandAddRemove *me)
{
CommandData *cdata;
GList *list, *l, *saved_props;
gchar *special_child_type;
if (me->widgets)
{
glade_project_selection_clear (GLADE_COMMAND (me)->priv->project, FALSE);
for (list = me->widgets; list && list->data; list = list->next)
{
cdata = list->data;
saved_props = NULL;
GLADE_NOTE (COMMANDS,
g_print ("Adding widget '%s' to parent '%s' "
"(from clipboard: %s, props recorded: %s, have placeholder: %s, child_type: %s)\n",
glade_widget_get_name (cdata->widget),
cdata->parent ? glade_widget_get_name (cdata->parent) : "(none)",
me->from_clipboard ? "yes" : "no",
cdata->props_recorded ? "yes" : "no",
cdata->placeholder ? "yes" : "no",
cdata->special_type));
if (cdata->parent != NULL)
{
/* Prepare special-child-type for the paste. */
if (me->from_clipboard)
{
/* Only transfer properties when they are from the clipboard,
* otherwise prioritize packing defaults.
*/
saved_props =
glade_widget_dup_properties (cdata->widget,
glade_widget_get_packing_properties (cdata->widget),
FALSE, FALSE, FALSE);
glade_widget_set_packing_properties (cdata->widget, cdata->parent);
}
/* Clear it the first time around, ensure we record it after adding */
if (cdata->props_recorded == FALSE)
g_object_set_data (glade_widget_get_object (cdata->widget),
"special-child-type", NULL);
else
g_object_set_data_full (glade_widget_get_object (cdata->widget),
"special-child-type",
g_strdup (cdata->special_type),
g_free);
/* glade_command_paste ganauntees that if
* there we are pasting to a placeholder,
* there is only one widget.
*/
if (cdata->placeholder)
glade_widget_replace (cdata->parent,
G_OBJECT (cdata->placeholder),
glade_widget_get_object (cdata->widget));
else
glade_widget_add_child (cdata->parent,
cdata->widget,
cdata->props_recorded == FALSE);
glade_command_transfer_props (cdata->widget, saved_props);
if (saved_props)
{
g_list_foreach (saved_props, (GFunc) g_object_unref, NULL);
g_list_free (saved_props);
}
/* Now that we've added, apply any packing props if nescisary. */
for (l = cdata->pack_props; l; l = l->next)
{
GValue value = { 0, };
GladeProperty *saved_prop = l->data;
GladePropertyClass *pclass = glade_property_get_class (saved_prop);
GladeProperty *widget_prop =
glade_widget_get_pack_property (cdata->widget, glade_property_class_id (pclass));
glade_property_get_value (saved_prop, &value);
glade_property_set_value (widget_prop, &value);
glade_property_sync (widget_prop);
g_value_unset (&value);
}
if (cdata->props_recorded == FALSE)
{
/* Save the packing properties after the initial paste.
* (this will be the defaults returned by the container
* implementation after initially adding them).
*
* Otherwise this recorded marker was set when cutting
*/
g_assert (cdata->pack_props == NULL);
for (l = glade_widget_get_packing_properties (cdata->widget); l; l = l->next)
cdata->pack_props =
g_list_prepend (cdata->pack_props,
glade_property_dup (GLADE_PROPERTY (l->data),
cdata->widget));
/* Record the special-type here after replacing */
if ((special_child_type =
g_object_get_data (glade_widget_get_object (cdata->widget),
"special-child-type")) != NULL)
{
g_free (cdata->special_type);
cdata->special_type = g_strdup (special_child_type);
}
GLADE_NOTE (COMMANDS,
g_print ("Recorded properties for adding widget '%s' to parent '%s' (special child: %s)\n",
glade_widget_get_name (cdata->widget),
cdata->parent ? glade_widget_get_name (cdata->parent) : "(none)",
cdata->special_type));
/* Mark the properties as recorded */
cdata->props_recorded = TRUE;
}
}
glade_project_add_object (GLADE_COMMAND (me)->priv->project,
glade_widget_get_object (cdata->widget));
for (l = cdata->reffed; l; l = l->next)
{
GladeWidget *reffed = l->data;
glade_project_add_object (GLADE_COMMAND (me)->priv->project,
glade_widget_get_object (reffed));
}
glade_project_selection_add (GLADE_COMMAND (me)->priv->project,
glade_widget_get_object (cdata->widget), FALSE);
glade_widget_show (cdata->widget);
}
glade_project_queue_selection_changed (GLADE_COMMAND (me)->priv->project);
}
return TRUE;
} /* end of glade_command_add_execute() */
static gboolean
glade_command_remove_execute (GladeCommandAddRemove *me)
{
CommandData *cdata;
GladeWidget *reffed;
GList *list, *l;
gchar *special_child_type;
for (list = me->widgets; list && list->data; list = list->next)
{
cdata = list->data;
GLADE_NOTE (COMMANDS,
g_print ("Removing widget '%s' from parent '%s' "
"(from clipboard: %s, props recorded: %s, have placeholder: %s, child_type: %s)\n",
glade_widget_get_name (cdata->widget),
cdata->parent ? glade_widget_get_name (cdata->parent) : "(none)",
me->from_clipboard ? "yes" : "no",
cdata->props_recorded ? "yes" : "no",
cdata->placeholder ? "yes" : "no",
cdata->special_type));
glade_widget_hide (cdata->widget);
if (cdata->props_recorded == FALSE)
{
/* Record the special-type here after replacing */
if ((special_child_type =
g_object_get_data (glade_widget_get_object (cdata->widget),
"special-child-type")) != NULL)
{
g_free (cdata->special_type);
cdata->special_type = g_strdup (special_child_type);
}
GLADE_NOTE (COMMANDS,
g_print ("Recorded properties for removing widget '%s' from parent '%s' (special child: %s)\n",
glade_widget_get_name (cdata->widget),
cdata->parent ? glade_widget_get_name (cdata->parent) : "(none)",
cdata->special_type));
/* Mark the properties as recorded */
cdata->props_recorded = TRUE;
}
glade_project_remove_object (GLADE_COMMAND (me)->priv->project,
glade_widget_get_object (cdata->widget));
for (l = cdata->reffed; l; l = l->next)
{
reffed = l->data;
glade_project_remove_object (GLADE_COMMAND (me)->priv->project,
glade_widget_get_object (reffed));
}
if (cdata->parent)
{
if (cdata->placeholder)
glade_widget_replace (cdata->parent, glade_widget_get_object (cdata->widget),
G_OBJECT (cdata->placeholder));
else
glade_widget_remove_child (cdata->parent, cdata->widget);
}
}
return TRUE;
}
/*
* Execute the cmd and revert it. Ie, after the execution of this
* function cmd will point to the undo action
*/
static gboolean
glade_command_add_remove_execute (GladeCommand *cmd)
{
GladeCommandAddRemove *me = (GladeCommandAddRemove *) cmd;
gboolean retval;
if (me->add)
retval = glade_command_add_execute (me);
else
retval = glade_command_remove_execute (me);
me->add = !me->add;
return retval;
}
static gboolean
glade_command_add_remove_undo (GladeCommand *cmd)
{
return glade_command_add_remove_execute (cmd);
}
static void
glade_command_add_remove_finalize (GObject *obj)
{
GladeCommandAddRemove *cmd;
CommandData *cdata;
GList *list;
g_return_if_fail (GLADE_IS_COMMAND_ADD_REMOVE (obj));
cmd = GLADE_COMMAND_ADD_REMOVE (obj);
for (list = cmd->widgets; list && list->data; list = list->next)
{
cdata = list->data;
if (cdata->placeholder)
{
if (cdata->handler_id)
g_signal_handler_disconnect (cdata->placeholder, cdata->handler_id);
if (g_object_is_floating (G_OBJECT (cdata->placeholder)))
gtk_widget_destroy (GTK_WIDGET (cdata->placeholder));
}
if (cdata->widget)
g_object_unref (G_OBJECT (cdata->widget));
g_list_foreach (cdata->reffed, (GFunc) g_object_unref, NULL);
g_list_free (cdata->reffed);
}
g_list_free (cmd->widgets);
glade_command_finalize (obj);
}
static gboolean
glade_command_add_remove_unifies (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
return FALSE;
}
static void
glade_command_add_remove_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
g_return_if_reached ();
}
/******************************************************************************
*
* The following are command aliases. Their implementations are the actual
* glade commands.
*
*****************************************************************************/
/**
* glade_command_create:
* @adaptor: A #GladeWidgetAdaptor
* @parent (allow-none): the parent #GladeWidget to add the new widget to.
* @placeholder (allow-none): the placeholder which will be substituted by the widget
* @project: the project his widget belongs to.
*
* Creates a new widget using @adaptor and put in place of the @placeholder
* in the @project
*
* Returns: the newly created widget.
*/
GladeWidget *
glade_command_create (GladeWidgetAdaptor *adaptor,
GladeWidget *parent,
GladePlaceholder *placeholder,
GladeProject *project)
{
GladeWidget *widget;
GList *widgets = NULL;
g_return_val_if_fail (GLADE_IS_WIDGET_ADAPTOR (adaptor), NULL);
g_return_val_if_fail (GLADE_IS_PROJECT (project), NULL);
/* attempt to create the widget -- widget may be null, e.g. the user clicked cancel on a query */
widget = glade_widget_adaptor_create_widget (adaptor, TRUE,
"parent", parent,
"project", project, NULL);
if (widget == NULL)
{
return NULL;
}
if (parent && !glade_widget_add_verify (parent, widget, TRUE))
{
g_object_ref_sink (widget);
g_object_unref (widget);
return NULL;
}
widgets = g_list_prepend (widgets, widget);
glade_command_push_group (_("Create %s"), glade_widget_get_name (widget));
glade_command_add (widgets, parent, placeholder, project, FALSE);
glade_command_pop_group ();
g_list_free (widgets);
/* Make selection change immediately when a widget is created */
glade_project_selection_changed (project);
return widget;
}
/**
* glade_command_delete:
* @widgets (element-type GladeWidget): a #GList of #GladeWidgets
*
* Performs a delete command on the list of widgets.
*/
void
glade_command_delete (GList *widgets)
{
GladeWidget *widget;
g_return_if_fail (widgets != NULL);
widget = widgets->data;
glade_command_push_group (_("Delete %s"),
g_list_length (widgets) == 1 ?
glade_widget_get_name (widget) : _("multiple"));
glade_command_remove (widgets);
glade_command_pop_group ();
}
/**
* glade_command_cut:
* @widgets (element-type GladeWidget): a #GList of #GladeWidgets
*
* Removes the list of widgets and adds them to the clipboard.
*/
void
glade_command_cut (GList *widgets)
{
GladeWidget *widget;
GList *l;
g_return_if_fail (widgets != NULL);
for (l = widgets; l; l = l->next)
g_object_set_data (G_OBJECT (l->data), "glade-command-was-cut",
GINT_TO_POINTER (TRUE));
widget = widgets->data;
glade_command_push_group (_("Cut %s"),
g_list_length (widgets) == 1 ?
glade_widget_get_name (widget) : _("multiple"));
glade_command_remove (widgets);
glade_command_pop_group ();
glade_clipboard_add (glade_app_get_clipboard (), widgets);
}
#if 0
static void
glade_command_break_references_for_widget (GladeWidget *widget, GList *widgets)
{
GList *l, *children;
for (l = widget->properties; l; l = l->next)
{
property = l->data;
if (glade_property_class_is_object (property->klass) &&
property->klass->parentless_widget == FALSE)
{
GList *obj_list;
GObject *reffed_obj = NULL;
GladeWidget *reffed_widget;
if (GLADE_IS_PARAM_SPEC_OBJECTS (klass->pspec))
{
glade_property_get (property, &obj_list);
}
else
{
glade_property_get (property, &reffed_obj);
}
}
}
children = glade_widget_adaptor_get_children (widget->adaptor,
widget->object);
for (l = children; l; l = l->next)
{
if ((child = glade_widget_get_from_gobject (l->data)) != NULL)
glade_command_break_references_for_widget (child, widgets);
}
g_list_free (children);
}
static void
glade_command_break_references (GladeProject *project, GList *widgets)
{
GList *list;
GladeWidget *widget;
for (list = widgets; list && list->data; list = list->next)
{
widget = l->data;
if (project == widget->project)
continue;
glade_command_break_references_for_widget (widget, widgets);
}
}
#endif
/**
* glade_command_paste:
* @widgets (element-type GladeWidget): a #GList of #GladeWidget
* @parent (allow-none): a #GladeWidget
* @placeholder (allow-none): a #GladePlaceholder
*
* Performs a paste command on all widgets in @widgets to @parent, possibly
* replacing @placeholder (note toplevels dont need a parent; the active project
* will be used when pasting toplevel objects).
*/
void
glade_command_paste (GList *widgets,
GladeWidget *parent,
GladePlaceholder *placeholder,
GladeProject *project)
{
GList *list, *copied_widgets = NULL;
GladeWidget *copied_widget = NULL;
gboolean exact;
g_return_if_fail (widgets != NULL);
for (list = widgets; list && list->data; list = list->next)
{
exact =
GPOINTER_TO_INT (g_object_get_data
(G_OBJECT (list->data), "glade-command-was-cut"));
copied_widget = glade_widget_dup (list->data, exact);
copied_widgets = g_list_prepend (copied_widgets, copied_widget);
}
glade_command_push_group (_("Paste %s"),
g_list_length (widgets) == 1 ?
glade_widget_get_name (copied_widget) : _("multiple"));
glade_command_add (copied_widgets, parent, placeholder, project, TRUE);
glade_command_pop_group ();
if (copied_widgets)
g_list_free (copied_widgets);
}
/**
* glade_command_dnd:
* @widgets (element-type GladeWidget): a #GList of #GladeWidget
* @parent (allow-none): a #GladeWidget
* @placeholder (allow-none): a #GladePlaceholder
*
* Performs a drag-n-drop command, i.e. removes the list of widgets and adds them
* to the new parent, possibly replacing @placeholder (note toplevels dont need a
* parent; the active project will be used when pasting toplevel objects).
*/
void
glade_command_dnd (GList *widgets,
GladeWidget *parent,
GladePlaceholder *placeholder)
{
GladeWidget *widget;
GladeProject *project;
g_return_if_fail (widgets != NULL);
widget = widgets->data;
if (parent)
project = glade_widget_get_project (parent);
else if (placeholder)
project = glade_placeholder_get_project (placeholder);
else
project = glade_widget_get_project (widget);
g_return_if_fail (project);
glade_command_push_group (_("Drag %s and Drop to %s"),
g_list_length (widgets) == 1 ?
glade_widget_get_name (widget) : _("multiple"),
parent ? glade_widget_get_name (parent) : _("root"));
glade_command_remove (widgets);
glade_command_add (widgets, parent, placeholder, project, TRUE);
glade_command_pop_group ();
}
/*********************************************************/
/******* GLADE_COMMAND_ADD_SIGNAL *******/
/*********************************************************/
/* create a new GladeCommandAddRemoveChangeSignal class. Objects of this class will
* encapsulate an "add or remove signal handler" operation */
typedef enum
{
GLADE_ADD,
GLADE_REMOVE,
GLADE_CHANGE
} GladeAddType;
typedef struct
{
GladeCommand parent;
GladeWidget *widget;
GladeSignal *signal;
GladeSignal *new_signal;
GladeAddType type;
} GladeCommandAddSignal;
/* standard macros */
GLADE_MAKE_COMMAND (GladeCommandAddSignal, glade_command_add_signal);
#define GLADE_COMMAND_ADD_SIGNAL_TYPE (glade_command_add_signal_get_type ())
#define GLADE_COMMAND_ADD_SIGNAL(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_ADD_SIGNAL_TYPE, GladeCommandAddSignal))
#define GLADE_COMMAND_ADD_SIGNAL_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_ADD_SIGNAL_TYPE, GladeCommandAddSignalClass))
#define GLADE_IS_COMMAND_ADD_SIGNAL(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_ADD_SIGNAL_TYPE))
#define GLADE_IS_COMMAND_ADD_SIGNAL_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_ADD_SIGNAL_TYPE))
static void
glade_command_add_signal_finalize (GObject *obj)
{
GladeCommandAddSignal *cmd = GLADE_COMMAND_ADD_SIGNAL (obj);
g_object_unref (cmd->widget);
if (cmd->signal)
g_object_unref (cmd->signal);
if (cmd->new_signal)
g_object_unref (cmd->new_signal);
glade_command_finalize (obj);
}
static gboolean
glade_command_add_signal_undo (GladeCommand *this_cmd)
{
return glade_command_add_signal_execute (this_cmd);
}
static gboolean
glade_command_add_signal_execute (GladeCommand *this_cmd)
{
GladeCommandAddSignal *cmd = GLADE_COMMAND_ADD_SIGNAL (this_cmd);
GladeSignal *temp;
switch (cmd->type)
{
case GLADE_ADD:
glade_widget_add_signal_handler (cmd->widget, cmd->signal);
cmd->type = GLADE_REMOVE;
break;
case GLADE_REMOVE:
glade_widget_remove_signal_handler (cmd->widget, cmd->signal);
cmd->type = GLADE_ADD;
break;
case GLADE_CHANGE:
glade_widget_change_signal_handler (cmd->widget,
cmd->signal, cmd->new_signal);
temp = cmd->signal;
cmd->signal = cmd->new_signal;
cmd->new_signal = temp;
break;
default:
break;
}
return TRUE;
}
static gboolean
glade_command_add_signal_unifies (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
return FALSE;
}
static void
glade_command_add_signal_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
g_return_if_reached ();
}
static void
glade_command_add_remove_change_signal (GladeWidget *glade_widget,
const GladeSignal *signal,
const GladeSignal *new_signal,
GladeAddType type)
{
GladeCommandAddSignal *me = GLADE_COMMAND_ADD_SIGNAL
(g_object_new (GLADE_COMMAND_ADD_SIGNAL_TYPE, NULL));
GladeCommand *cmd = GLADE_COMMAND (me);
/* we can only add/remove a signal to a widget that has been wrapped by a GladeWidget */
g_assert (glade_widget != NULL);
g_assert (glade_widget_get_project (glade_widget) != NULL);
me->widget = g_object_ref (glade_widget);
me->type = type;
me->signal = glade_signal_clone (signal);
me->new_signal = new_signal ? glade_signal_clone (new_signal) : NULL;
cmd->priv->project = glade_widget_get_project (glade_widget);
cmd->priv->description =
g_strdup_printf (type == GLADE_ADD ? _("Add signal handler %s") :
type == GLADE_REMOVE ? _("Remove signal handler %s") :
_("Change signal handler %s"),
glade_signal_get_handler ((GladeSignal *)signal));
glade_command_check_group (GLADE_COMMAND (me));
if (glade_command_add_signal_execute (cmd))
glade_project_push_undo (cmd->priv->project, cmd);
else
g_object_unref (G_OBJECT (me));
}
/**
* glade_command_add_signal:
* @glade_widget: a #GladeWidget
* @signal: a #GladeSignal
*
* TODO: write me
*/
void
glade_command_add_signal (GladeWidget *glade_widget, const GladeSignal *signal)
{
glade_command_add_remove_change_signal
(glade_widget, signal, NULL, GLADE_ADD);
}
/**
* glade_command_remove_signal:
* @glade_widget: a #GladeWidget
* @signal: a #GladeSignal
*
* TODO: write me
*/
void
glade_command_remove_signal (GladeWidget *glade_widget,
const GladeSignal *signal)
{
glade_command_add_remove_change_signal
(glade_widget, signal, NULL, GLADE_REMOVE);
}
/**
* glade_command_change_signal:
* @glade_widget: a #GladeWidget
* @old_signal: a #GladeSignal
* @new_signal: a #GladeSignal
*
* TODO: write me
*/
void
glade_command_change_signal (GladeWidget *glade_widget,
const GladeSignal *old_signal,
const GladeSignal *new_signal)
{
glade_command_add_remove_change_signal
(glade_widget, old_signal, new_signal, GLADE_CHANGE);
}
/******************************************************************************
*
* set i18n metadata
*
* This command sets the i18n metadata on a label property.
*
*****************************************************************************/
typedef struct
{
GladeCommand parent;
GladeProperty *property;
gboolean translatable;
gchar *context;
gchar *comment;
gboolean old_translatable;
gchar *old_context;
gchar *old_comment;
} GladeCommandSetI18n;
GLADE_MAKE_COMMAND (GladeCommandSetI18n, glade_command_set_i18n);
#define GLADE_COMMAND_SET_I18N_TYPE (glade_command_set_i18n_get_type ())
#define GLADE_COMMAND_SET_I18N(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_SET_I18N_TYPE, GladeCommandSetI18n))
#define GLADE_COMMAND_SET_I18N_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_SET_I18N_TYPE, GladeCommandSetI18nClass))
#define GLADE_IS_COMMAND_SET_I18N(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_SET_I18N_TYPE))
#define GLADE_IS_COMMAND_SET_I18N_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_SET_I18N_TYPE))
static gboolean
glade_command_set_i18n_execute (GladeCommand *cmd)
{
GladeCommandSetI18n *me = (GladeCommandSetI18n *) cmd;
gboolean temp_translatable;
gchar *temp_context;
gchar *temp_comment;
/* sanity check */
g_return_val_if_fail (me != NULL, TRUE);
g_return_val_if_fail (me->property != NULL, TRUE);
/* set the new values in the property */
glade_property_i18n_set_translatable (me->property, me->translatable);
glade_property_i18n_set_context (me->property, me->context);
glade_property_i18n_set_comment (me->property, me->comment);
/* swap the current values with the old values to prepare for undo */
temp_translatable = me->translatable;
temp_context = me->context;
temp_comment = me->comment;
me->translatable = me->old_translatable;
me->context = me->old_context;
me->comment = me->old_comment;
me->old_translatable = temp_translatable;
me->old_context = temp_context;
me->old_comment = temp_comment;
return TRUE;
}
static gboolean
glade_command_set_i18n_undo (GladeCommand *cmd)
{
return glade_command_set_i18n_execute (cmd);
}
static void
glade_command_set_i18n_finalize (GObject *obj)
{
GladeCommandSetI18n *me;
g_return_if_fail (GLADE_IS_COMMAND_SET_I18N (obj));
me = GLADE_COMMAND_SET_I18N (obj);
g_free (me->context);
g_free (me->comment);
g_free (me->old_context);
g_free (me->old_comment);
glade_command_finalize (obj);
}
static gboolean
glade_command_set_i18n_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandSetI18n *cmd1;
GladeCommandSetI18n *cmd2;
if (GLADE_IS_COMMAND_SET_I18N (this_cmd) &&
GLADE_IS_COMMAND_SET_I18N (other_cmd))
{
cmd1 = GLADE_COMMAND_SET_I18N (this_cmd);
cmd2 = GLADE_COMMAND_SET_I18N (other_cmd);
return (cmd1->property == cmd2->property);
}
return FALSE;
}
static void
glade_command_set_i18n_collapse (GladeCommand *this_cmd,
GladeCommand *other_cmd)
{
/* this command is the one that will be used for an undo of the sequence of like commands */
GladeCommandSetI18n *this = GLADE_COMMAND_SET_I18N (this_cmd);
/* the other command contains the values that will be used for a redo */
GladeCommandSetI18n *other = GLADE_COMMAND_SET_I18N (other_cmd);
g_return_if_fail (GLADE_IS_COMMAND_SET_I18N (this_cmd) &&
GLADE_IS_COMMAND_SET_I18N (other_cmd));
/* adjust this command to contain, as its old values, the other command's current values */
this->old_translatable = other->old_translatable;
g_free (this->old_context);
g_free (this->old_comment);
this->old_context = other->old_context;
this->old_comment = other->old_comment;
other->old_context = NULL;
other->old_comment = NULL;
}
/**
* glade_command_set_i18n:
* @property: a #GladeProperty
* @translatable: a #gboolean
* @context: a #const gchar *
* @comment: a #const gchar *
*
* Sets the i18n data on the property.
*/
void
glade_command_set_i18n (GladeProperty *property,
gboolean translatable,
const gchar *context,
const gchar *comment)
{
GladeCommandSetI18n *me;
g_return_if_fail (property);
/* check that something changed before continuing with the command */
if (translatable == glade_property_i18n_get_translatable (property) &&
!g_strcmp0 (glade_property_i18n_get_context (property), context) &&
!g_strcmp0 (glade_property_i18n_get_comment (property), comment))
return;
/* load up the command */
me = g_object_new (GLADE_COMMAND_SET_I18N_TYPE, NULL);
me->property = property;
me->translatable = translatable;
me->context = g_strdup (context);
me->comment = g_strdup (comment);
me->old_translatable = glade_property_i18n_get_translatable (property);
me->old_context = g_strdup (glade_property_i18n_get_context (property));
me->old_comment = g_strdup (glade_property_i18n_get_comment (property));
GLADE_COMMAND (me)->priv->project =
glade_widget_get_project (glade_property_get_widget (property));
GLADE_COMMAND (me)->priv->description =
g_strdup_printf (_("Setting i18n metadata"));;
glade_command_check_group (GLADE_COMMAND (me));
/* execute the command and push it on the stack if successful */
if (glade_command_set_i18n_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project, GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
}
/******************************************************************************
*
* This command sets protection warnings on widgets
*
*****************************************************************************/
typedef struct
{
GladeCommand parent;
GladeWidget *widget;
GladeWidget *locked;
gboolean locking;
} GladeCommandLock;
GLADE_MAKE_COMMAND (GladeCommandLock, glade_command_lock);
#define GLADE_COMMAND_LOCK_TYPE (glade_command_lock_get_type ())
#define GLADE_COMMAND_LOCK(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_LOCK_TYPE, GladeCommandLock))
#define GLADE_COMMAND_LOCK_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_LOCK_TYPE, GladeCommandLockClass))
#define GLADE_IS_COMMAND_LOCK(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_LOCK_TYPE))
#define GLADE_IS_COMMAND_LOCK_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_LOCK_TYPE))
static gboolean
glade_command_lock_execute (GladeCommand *cmd)
{
GladeCommandLock *me = (GladeCommandLock *) cmd;
/* set the new policy */
if (me->locking)
glade_widget_lock (me->widget, me->locked);
else
glade_widget_unlock (me->locked);
/* swap the current values with the old values to prepare for undo */
me->locking = !me->locking;
return TRUE;
}
static gboolean
glade_command_lock_undo (GladeCommand *cmd)
{
return glade_command_lock_execute (cmd);
}
static void
glade_command_lock_finalize (GObject *obj)
{
GladeCommandLock *me = (GladeCommandLock *) obj;
g_object_unref (me->widget);
g_object_unref (me->locked);
glade_command_finalize (obj);
}
static gboolean
glade_command_lock_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
/* GladeCommandLock *cmd1; */
/* GladeCommandLock *cmd2; */
/* No point here, this command undoubtedly always runs in groups */
return FALSE;
}
static void
glade_command_lock_collapse (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
/* this command is the one that will be used for an undo of the sequence of like commands */
//GladeCommandLock *this = GLADE_COMMAND_LOCK (this_cmd);
/* the other command contains the values that will be used for a redo */
//GladeCommandLock *other = GLADE_COMMAND_LOCK (other_cmd);
g_return_if_fail (GLADE_IS_COMMAND_LOCK (this_cmd) &&
GLADE_IS_COMMAND_LOCK (other_cmd));
/* no unify/collapse */
}
/**
* glade_command_lock_widget:
* @widget: A #GladeWidget
* @locked: The #GladeWidget to lock
*
* Sets @locked to be in a locked up state
* spoken for by @widget, locked widgets cannot
* be removed from the project until unlocked.
*/
void
glade_command_lock_widget (GladeWidget *widget, GladeWidget *locked)
{
GladeCommandLock *me;
g_return_if_fail (GLADE_IS_WIDGET (widget));
g_return_if_fail (GLADE_IS_WIDGET (locked));
g_return_if_fail (glade_widget_get_locker (locked) == NULL);
/* load up the command */
me = g_object_new (GLADE_COMMAND_LOCK_TYPE, NULL);
me->widget = g_object_ref (widget);
me->locked = g_object_ref (locked);
me->locking = TRUE;
GLADE_COMMAND (me)->priv->project = glade_widget_get_project (widget);
GLADE_COMMAND (me)->priv->description =
g_strdup_printf (_("Locking %s by widget %s"),
glade_widget_get_name (locked),
glade_widget_get_name (widget));
glade_command_check_group (GLADE_COMMAND (me));
/* execute the command and push it on the stack if successful
* this sets the actual policy
*/
if (glade_command_lock_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project, GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
}
/**
* glade_command_unlock_widget:
* @widget: A #GladeWidget
*
* Unlocks @widget so that it can be removed
* from the project again
*
*/
void
glade_command_unlock_widget (GladeWidget *widget)
{
GladeCommandLock *me;
g_return_if_fail (GLADE_IS_WIDGET (widget));
g_return_if_fail (GLADE_IS_WIDGET (glade_widget_get_locker (widget)));
/* load up the command */
me = g_object_new (GLADE_COMMAND_LOCK_TYPE, NULL);
me->widget = g_object_ref (glade_widget_get_locker (widget));
me->locked = g_object_ref (widget);
me->locking = FALSE;
GLADE_COMMAND (me)->priv->project = glade_widget_get_project (widget);
GLADE_COMMAND (me)->priv->description =
g_strdup_printf (_("Unlocking %s"), glade_widget_get_name (widget));
glade_command_check_group (GLADE_COMMAND (me));
/* execute the command and push it on the stack if successful
* this sets the actual policy
*/
if (glade_command_lock_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project, GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
}
/******************************************************************************
*
* This command sets the target version of a GladeProject
*
*****************************************************************************/
typedef struct
{
GladeCommand parent;
gchar *catalog;
gint old_major;
gint old_minor;
gint new_major;
gint new_minor;
} GladeCommandTarget;
GLADE_MAKE_COMMAND (GladeCommandTarget, glade_command_target);
#define GLADE_COMMAND_TARGET_TYPE (glade_command_target_get_type ())
#define GLADE_COMMAND_TARGET(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_TARGET_TYPE, GladeCommandTarget))
#define GLADE_COMMAND_TARGET_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_TARGET_TYPE, GladeCommandTargetClass))
#define GLADE_IS_COMMAND_TARGET(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_TARGET_TYPE))
#define GLADE_IS_COMMAND_TARGET_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_TARGET_TYPE))
static gboolean
glade_command_target_execute (GladeCommand *cmd)
{
GladeCommandTarget *me = (GladeCommandTarget *) cmd;
glade_project_set_target_version (cmd->priv->project,
me->catalog,
me->new_major,
me->new_minor);
return TRUE;
}
static gboolean
glade_command_target_undo (GladeCommand *cmd)
{
GladeCommandTarget *me = (GladeCommandTarget *) cmd;
glade_project_set_target_version (cmd->priv->project,
me->catalog,
me->old_major,
me->old_minor);
return TRUE;
}
static void
glade_command_target_finalize (GObject *obj)
{
GladeCommandTarget *me = (GladeCommandTarget *) obj;
g_free (me->catalog);
glade_command_finalize (obj);
}
static gboolean
glade_command_target_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandTarget *me;
/* Do we unify with self ? */
if (!other_cmd)
{
if (GLADE_IS_COMMAND_TARGET (this_cmd))
{
me = (GladeCommandTarget *) this_cmd;
return (me->old_major == me->new_major &&
me->old_minor == me->new_minor);
}
return FALSE;
}
if (GLADE_IS_COMMAND_TARGET (this_cmd) &&
GLADE_IS_COMMAND_TARGET (other_cmd))
{
GladeCommandTarget *other;
me = (GladeCommandTarget *) this_cmd;
other = (GladeCommandTarget *) other_cmd;
return g_strcmp0 (me->catalog, other->catalog) == 0;
}
return FALSE;
}
static void
glade_command_target_collapse (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandTarget *this;
GladeCommandTarget *other;
g_return_if_fail (GLADE_IS_COMMAND_TARGET (this_cmd) &&
GLADE_IS_COMMAND_TARGET (other_cmd));
this = GLADE_COMMAND_TARGET (this_cmd);
other = GLADE_COMMAND_TARGET (other_cmd);
this->new_major = other->new_major;
this->new_minor = other->new_minor;
g_free (this_cmd->priv->description);
this_cmd->priv->description =
g_strdup_printf (_("Setting target version of '%s' to %d.%d"),
this->catalog, this->new_major, this->new_minor);
}
/**
* glade_command_set_project_target:
* @project: A #GladeProject
* @catalog: The name of the catalog to set the project's target for
* @major: The new major version of @catalog to target
* @minor: The new minor version of @catalog to target
*
* Sets the target of @catalog to @major.@minor in @project.
*/
void
glade_command_set_project_target (GladeProject *project,
const gchar *catalog,
gint major,
gint minor)
{
GladeCommandTarget *me;
gint old_major = 0;
gint old_minor = 0;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_return_if_fail (catalog && catalog[0]);
g_return_if_fail (major >= 0);
g_return_if_fail (minor >= 0);
/* load up the command */
me = g_object_new (GLADE_COMMAND_TARGET_TYPE, NULL);
GLADE_COMMAND (me)->priv->project = project;
me->catalog = g_strdup (catalog);
glade_project_get_target_version (project, me->catalog, &old_major, &old_minor);
me->new_major = major;
me->new_minor = minor;
me->old_major = old_major;
me->old_minor = old_minor;
GLADE_COMMAND (me)->priv->description =
g_strdup_printf (_("Setting target version of '%s' to %d.%d"),
me->catalog, me->new_major, me->new_minor);
glade_command_check_group (GLADE_COMMAND (me));
/* execute the command and push it on the stack if successful
* this sets the actual policy
*/
if (glade_command_target_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project, GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
}
/******************************************************************************
*
* This command sets can set different properties of a GladeProject
*
*****************************************************************************/
typedef gchar *(*DescriptionNewFunc) (GladeCommand *);
typedef struct
{
GladeCommand parent;
const gchar *property_id; /* Intern string */
DescriptionNewFunc description_new; /* Used to update command description */
GValue old_value;
GValue new_value;
} GladeCommandProperty;
GLADE_MAKE_COMMAND (GladeCommandProperty, glade_command_property);
#define GLADE_COMMAND_PROPERTY_TYPE (glade_command_property_get_type ())
#define GLADE_COMMAND_PROPERTY(o) (G_TYPE_CHECK_INSTANCE_CAST ((o), GLADE_COMMAND_PROPERTY_TYPE, GladeCommandProperty))
#define GLADE_COMMAND_PROPERTY_CLASS(k) (G_TYPE_CHECK_CLASS_CAST ((k), GLADE_COMMAND_PROPERTY_TYPE, GladeCommandPropertyClass))
#define GLADE_IS_COMMAND_PROPERTY(o) (G_TYPE_CHECK_INSTANCE_TYPE ((o), GLADE_COMMAND_PROPERTY_TYPE))
#define GLADE_IS_COMMAND_PROPERTY_CLASS(k) (G_TYPE_CHECK_CLASS_TYPE ((k), GLADE_COMMAND_PROPERTY_TYPE))
/* Return true if a == b, this could be exported in glade_utils */
static gboolean
glade_command_property_compare (GValue *a, GValue *b)
{
if (G_VALUE_TYPE (a) != G_VALUE_TYPE (b))
{
g_warning ("Comparing a %s with a %s type is not supported",
G_VALUE_TYPE_NAME (a), G_VALUE_TYPE_NAME (b));
return FALSE;
}
if (G_VALUE_HOLDS_STRING (a))
return g_strcmp0 (g_value_get_string (a), g_value_get_string (b)) == 0;
else if (G_VALUE_HOLDS_OBJECT (a))
return g_value_get_object (a) == g_value_get_object (b);
else if (G_VALUE_HOLDS_BOOLEAN (a))
return g_value_get_boolean (a) == g_value_get_boolean (b);
else if (G_VALUE_HOLDS_CHAR (a))
return g_value_get_schar (a) == g_value_get_schar (b);
else if (G_VALUE_HOLDS_DOUBLE (a))
return g_value_get_double (a) == g_value_get_double (b);
else if (G_VALUE_HOLDS_ENUM (a))
return g_value_get_enum (a) == g_value_get_enum (b);
else if (G_VALUE_HOLDS_FLAGS (a))
return g_value_get_flags (a) == g_value_get_flags (b);
else if (G_VALUE_HOLDS_FLOAT (a))
return g_value_get_float (a) == g_value_get_float (b);
else if (G_VALUE_HOLDS_GTYPE (a))
return g_value_get_gtype (a) == g_value_get_gtype (b);
else if (G_VALUE_HOLDS_INT (a))
return g_value_get_int (a) == g_value_get_int (b);
else if (G_VALUE_HOLDS_INT64 (a))
return g_value_get_int64 (a) == g_value_get_int64 (b);
else if (G_VALUE_HOLDS_LONG (a))
return g_value_get_long (a) == g_value_get_long (b);
else if (G_VALUE_HOLDS_POINTER (a))
return g_value_get_pointer (a) == g_value_get_pointer (b);
else if (G_VALUE_HOLDS_UCHAR (a))
return g_value_get_uchar (a) == g_value_get_uchar (b);
else if (G_VALUE_HOLDS_UINT (a))
return g_value_get_uint (a) == g_value_get_uint (b);
else if (G_VALUE_HOLDS_UINT64 (a))
return g_value_get_uint64 (a) == g_value_get_uint64 (b);
else if (G_VALUE_HOLDS_ULONG (a))
return g_value_get_ulong (a) == g_value_get_ulong (b);
g_warning ("%s type not supported", G_VALUE_TYPE_NAME (a));
return FALSE;
}
static gboolean
glade_command_property_execute (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
g_object_set_property (G_OBJECT (cmd->priv->project), me->property_id, &me->new_value);
return TRUE;
}
static gboolean
glade_command_property_undo (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
g_object_set_property (G_OBJECT (cmd->priv->project), me->property_id, &me->old_value);
return TRUE;
}
static void
glade_command_property_finalize (GObject *obj)
{
GladeCommandProperty *me = (GladeCommandProperty *) obj;
/* NOTE: we do not free me->property_id because it is an intern string */
g_value_unset (&me->new_value);
g_value_unset (&me->old_value);
glade_command_finalize (obj);
}
static gboolean
glade_command_property_unifies (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
/* Do we unify with self ? */
if (!other_cmd)
{
if (GLADE_IS_COMMAND_PROPERTY (this_cmd))
{
GladeCommandProperty *me = (GladeCommandProperty *) this_cmd;
return glade_command_property_compare (&me->new_value, &me->old_value);
}
else
return FALSE;
}
if (GLADE_IS_COMMAND_PROPERTY (this_cmd) && GLADE_IS_COMMAND_PROPERTY (other_cmd))
{
GladeCommandProperty *this = (GladeCommandProperty *) this_cmd;
GladeCommandProperty *other = (GladeCommandProperty *) other_cmd;
/* Intern strings can be compared by comparind the pointers */
return this->property_id == other->property_id;
}
return FALSE;
}
static void
glade_command_property_update_description (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
g_free (cmd->priv->description);
if (me->description_new)
cmd->priv->description = me->description_new (cmd);
else
cmd->priv->description = g_strdup_printf (_("Setting project's %s property"),
me->property_id);
}
static void
glade_command_property_collapse (GladeCommand *this_cmd, GladeCommand *other_cmd)
{
GladeCommandProperty *this;
GladeCommandProperty *other;
g_return_if_fail (GLADE_IS_COMMAND_PROPERTY (this_cmd) &&
GLADE_IS_COMMAND_PROPERTY (other_cmd));
this = GLADE_COMMAND_PROPERTY (this_cmd);
other = GLADE_COMMAND_PROPERTY (other_cmd);
g_return_if_fail (this->property_id == other->property_id);
g_value_copy (&other->new_value, &this->new_value);
glade_command_property_update_description (this_cmd);
}
/**
* glade_command_set_project_property:
* @project: A #GladeProject
* @description_new: function to create the command description.
* @property_id: property this command should use
* @new_value: the value to set @property_id
*
* Sets @new_value as the @property_id property for @project.
*/
static void
glade_command_set_project_property (GladeProject *project,
DescriptionNewFunc description_new,
const gchar *property_id,
GValue *new_value)
{
GladeCommandProperty *me;
GValue old_value = G_VALUE_INIT;
g_value_init (&old_value, G_VALUE_TYPE (new_value));
g_object_get_property (G_OBJECT (project), property_id, &old_value);
if (glade_command_property_compare (&old_value, new_value))
{
g_value_unset (&old_value);
return;
}
me = g_object_new (GLADE_COMMAND_PROPERTY_TYPE, NULL);
GLADE_COMMAND (me)->priv->project = project;
me->description_new = description_new;
me->property_id = g_intern_static_string (property_id);
/* move the old value to the comand struct */
me->old_value = old_value;
/* set new value */
g_value_init (&me->new_value, G_VALUE_TYPE (new_value));
g_value_copy (new_value, &me->new_value);
glade_command_property_update_description (GLADE_COMMAND (me));
glade_command_check_group (GLADE_COMMAND (me));
/* execute the command and push it on the stack if successful
* this sets the actual policy
*/
if (glade_command_property_execute (GLADE_COMMAND (me)))
glade_project_push_undo (GLADE_COMMAND (me)->priv->project, GLADE_COMMAND (me));
else
g_object_unref (G_OBJECT (me));
}
/**
* glade_command_set_project_license:
* @project: A #GladeProject
* @license: License of @project
*
* Sets the license agreement for @project. It will be saved in the xml as comment.
*/
void
glade_command_set_project_license (GladeProject *project, const gchar *license)
{
GValue new_value = G_VALUE_INIT;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_value_init (&new_value, G_TYPE_STRING);
g_value_set_string (&new_value, license);
glade_command_set_project_property (project, NULL, "license", &new_value);
g_value_unset (&new_value);
}
static gchar *
gcp_resource_path_description_new (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
return g_strdup_printf (_("Setting resource path to '%s'"),
g_value_get_string (&me->new_value));
}
/**
* glade_command_set_project_resource_path:
* @project: A #GladeProject
* @path: path to load resources from.
*
* Sets a resource path @project.
*/
void
glade_command_set_project_resource_path (GladeProject *project, const gchar *path)
{
GValue new_value = G_VALUE_INIT;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_value_init (&new_value, G_TYPE_STRING);
g_value_set_string (&new_value, path);
glade_command_set_project_property (project, gcp_resource_path_description_new,
"resource-path", &new_value);
g_value_unset (&new_value);
}
static gchar *
gcp_domain_description_new (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
return g_strdup_printf (_("Setting translation domain to '%s'"),
g_value_get_string (&me->new_value));
}
/**
* glade_command_set_project_domain:
* @project: A #GladeProject
* @domain: The translation domain for @project
*
* Sets @domain as the translation domain for @project.
*/
void
glade_command_set_project_domain (GladeProject *project,
const gchar *domain)
{
GValue new_value = G_VALUE_INIT;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_value_init (&new_value, G_TYPE_STRING);
g_value_set_string (&new_value, domain);
glade_command_set_project_property (project, gcp_domain_description_new,
"translation-domain", &new_value);
g_value_unset (&new_value);
}
static gchar *
gcp_template_description_new (GladeCommand *cmd)
{
GladeCommandProperty *me = (GladeCommandProperty *) cmd;
GObject *new_template = g_value_get_object (&me->new_value);
GObject *old_template = g_value_get_object (&me->old_value);
if (new_template == NULL && old_template != NULL)
return g_strdup_printf (_("Unsetting widget '%s' as template"),
glade_widget_get_name (GLADE_WIDGET (old_template)));
else if (new_template != NULL)
return g_strdup_printf (_("Setting widget '%s' as template"),
glade_widget_get_name (GLADE_WIDGET (new_template)));
else
return g_strdup (_("Unsetting template"));
}
/**
* glade_command_set_project_template:
* @project: A #GladeProject
* @widget: The #GladeWidget to make template
*
* Sets @widget to be the template widget in @project.
*/
void
glade_command_set_project_template (GladeProject *project,
GladeWidget *widget)
{
GValue new_value = G_VALUE_INIT;
g_return_if_fail (GLADE_IS_PROJECT (project));
g_value_init (&new_value, G_TYPE_OBJECT);
g_value_set_object (&new_value, widget);
glade_command_set_project_property (project, gcp_template_description_new,
"template", &new_value);
g_value_unset (&new_value);
}
|