1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
|
/* gtktreestore.c
* Copyright (C) 2000 Red Hat, Inc., Jonathan Blandford <jrb@redhat.com>
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "config.h"
#include <string.h>
#include <gobject/gvaluecollector.h>
#include "gtktreemodel.h"
#include "gtktreestore.h"
#include "gtktreedatalist.h"
#include "gtktreednd.h"
#include "gtkbuildable.h"
#include "gtkintl.h"
#include "gtkalias.h"
#define G_NODE(node) ((GNode *)node)
#define GTK_TREE_STORE_IS_SORTED(tree) (((GtkTreeStore*)(tree))->sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
#define VALID_ITER(iter, tree_store) ((iter)!= NULL && (iter)->user_data != NULL && ((GtkTreeStore*)(tree_store))->stamp == (iter)->stamp)
static void gtk_tree_store_tree_model_init (GtkTreeModelIface *iface);
static void gtk_tree_store_drag_source_init(GtkTreeDragSourceIface *iface);
static void gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface);
static void gtk_tree_store_sortable_init (GtkTreeSortableIface *iface);
static void gtk_tree_store_buildable_init (GtkBuildableIface *iface);
static void gtk_tree_store_finalize (GObject *object);
static GtkTreeModelFlags gtk_tree_store_get_flags (GtkTreeModel *tree_model);
static gint gtk_tree_store_get_n_columns (GtkTreeModel *tree_model);
static GType gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
gint index);
static gboolean gtk_tree_store_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path);
static GtkTreePath *gtk_tree_store_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static void gtk_tree_store_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value);
static gboolean gtk_tree_store_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean gtk_tree_store_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent);
static gboolean gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gint gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter);
static gboolean gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n);
static gboolean gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child);
static void gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
gint n_columns);
static void gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
gint column,
GType type);
static void gtk_tree_store_increment_stamp (GtkTreeStore *tree_store);
/* DND interfaces */
static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
GtkTreePath *path);
static gboolean gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
GtkTreePath *path);
static gboolean gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
GtkTreePath *path,
GtkSelectionData *selection_data);
static gboolean gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
GtkTreePath *dest,
GtkSelectionData *selection_data);
static gboolean gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
GtkTreePath *dest_path,
GtkSelectionData *selection_data);
/* Sortable Interfaces */
static void gtk_tree_store_sort (GtkTreeStore *tree_store);
static void gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gint column,
gboolean emit_signal);
static gboolean gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
gint *sort_column_id,
GtkSortType *order);
static void gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
gint sort_column_id,
GtkSortType order);
static void gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
gint sort_column_id,
GtkTreeIterCompareFunc func,
gpointer data,
GDestroyNotify destroy);
static void gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
GtkTreeIterCompareFunc func,
gpointer data,
GDestroyNotify destroy);
static gboolean gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable);
/* buildable */
static gboolean gtk_tree_store_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
GMarkupParser *parser,
gpointer *data);
static void gtk_tree_store_buildable_custom_finished (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer user_data);
static void validate_gnode (GNode *node);
static void gtk_tree_store_move (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *position,
gboolean before);
static inline void
validate_tree (GtkTreeStore *tree_store)
{
if (gtk_debug_flags & GTK_DEBUG_TREE)
{
g_assert (G_NODE (tree_store->root)->parent == NULL);
validate_gnode (G_NODE (tree_store->root));
}
}
G_DEFINE_TYPE_WITH_CODE (GtkTreeStore, gtk_tree_store, G_TYPE_OBJECT,
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_MODEL,
gtk_tree_store_tree_model_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_SOURCE,
gtk_tree_store_drag_source_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_DRAG_DEST,
gtk_tree_store_drag_dest_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_TREE_SORTABLE,
gtk_tree_store_sortable_init)
G_IMPLEMENT_INTERFACE (GTK_TYPE_BUILDABLE,
gtk_tree_store_buildable_init))
static void
gtk_tree_store_class_init (GtkTreeStoreClass *class)
{
GObjectClass *object_class;
object_class = (GObjectClass *) class;
object_class->finalize = gtk_tree_store_finalize;
}
static void
gtk_tree_store_tree_model_init (GtkTreeModelIface *iface)
{
iface->get_flags = gtk_tree_store_get_flags;
iface->get_n_columns = gtk_tree_store_get_n_columns;
iface->get_column_type = gtk_tree_store_get_column_type;
iface->get_iter = gtk_tree_store_get_iter;
iface->get_path = gtk_tree_store_get_path;
iface->get_value = gtk_tree_store_get_value;
iface->iter_next = gtk_tree_store_iter_next;
iface->iter_children = gtk_tree_store_iter_children;
iface->iter_has_child = gtk_tree_store_iter_has_child;
iface->iter_n_children = gtk_tree_store_iter_n_children;
iface->iter_nth_child = gtk_tree_store_iter_nth_child;
iface->iter_parent = gtk_tree_store_iter_parent;
}
static void
gtk_tree_store_drag_source_init (GtkTreeDragSourceIface *iface)
{
iface->row_draggable = real_gtk_tree_store_row_draggable;
iface->drag_data_delete = gtk_tree_store_drag_data_delete;
iface->drag_data_get = gtk_tree_store_drag_data_get;
}
static void
gtk_tree_store_drag_dest_init (GtkTreeDragDestIface *iface)
{
iface->drag_data_received = gtk_tree_store_drag_data_received;
iface->row_drop_possible = gtk_tree_store_row_drop_possible;
}
static void
gtk_tree_store_sortable_init (GtkTreeSortableIface *iface)
{
iface->get_sort_column_id = gtk_tree_store_get_sort_column_id;
iface->set_sort_column_id = gtk_tree_store_set_sort_column_id;
iface->set_sort_func = gtk_tree_store_set_sort_func;
iface->set_default_sort_func = gtk_tree_store_set_default_sort_func;
iface->has_default_sort_func = gtk_tree_store_has_default_sort_func;
}
void
gtk_tree_store_buildable_init (GtkBuildableIface *iface)
{
iface->custom_tag_start = gtk_tree_store_buildable_custom_tag_start;
iface->custom_finished = gtk_tree_store_buildable_custom_finished;
}
static void
gtk_tree_store_init (GtkTreeStore *tree_store)
{
tree_store->root = g_node_new (NULL);
/* While the odds are against us getting 0...
*/
do
{
tree_store->stamp = g_random_int ();
}
while (tree_store->stamp == 0);
tree_store->sort_list = NULL;
tree_store->sort_column_id = GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID;
tree_store->columns_dirty = FALSE;
}
/**
* gtk_tree_store_new:
* @n_columns: number of columns in the tree store
* @Varargs: all #GType types for the columns, from first to last
*
* Creates a new tree store as with @n_columns columns each of the types passed
* in. Note that only types derived from standard GObject fundamental types
* are supported.
*
* As an example, <literal>gtk_tree_store_new (3, G_TYPE_INT, G_TYPE_STRING,
* GDK_TYPE_PIXBUF);</literal> will create a new #GtkTreeStore with three columns, of type
* <type>int</type>, <type>string</type> and #GdkPixbuf respectively.
*
* Return value: a new #GtkTreeStore
**/
GtkTreeStore *
gtk_tree_store_new (gint n_columns,
...)
{
GtkTreeStore *retval;
va_list args;
gint i;
g_return_val_if_fail (n_columns > 0, NULL);
retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
gtk_tree_store_set_n_columns (retval, n_columns);
va_start (args, n_columns);
for (i = 0; i < n_columns; i++)
{
GType type = va_arg (args, GType);
if (! _gtk_tree_data_list_check_type (type))
{
g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
g_object_unref (retval);
va_end (args);
return NULL;
}
gtk_tree_store_set_column_type (retval, i, type);
}
va_end (args);
return retval;
}
/**
* gtk_tree_store_newv:
* @n_columns: number of columns in the tree store
* @types: (array length=n_columns): an array of #GType types for the columns, from first to last
*
* Non vararg creation function. Used primarily by language bindings.
*
* Return value: a new #GtkTreeStore
**/
GtkTreeStore *
gtk_tree_store_newv (gint n_columns,
GType *types)
{
GtkTreeStore *retval;
gint i;
g_return_val_if_fail (n_columns > 0, NULL);
retval = g_object_new (GTK_TYPE_TREE_STORE, NULL);
gtk_tree_store_set_n_columns (retval, n_columns);
for (i = 0; i < n_columns; i++)
{
if (! _gtk_tree_data_list_check_type (types[i]))
{
g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
g_object_unref (retval);
return NULL;
}
gtk_tree_store_set_column_type (retval, i, types[i]);
}
return retval;
}
/**
* gtk_tree_store_set_column_types:
* @tree_store: A #GtkTreeStore
* @n_columns: Number of columns for the tree store
* @types: (array length=n_columns): An array of #GType types, one for each column
*
* This function is meant primarily for #GObjects that inherit from
* #GtkTreeStore, and should only be used when constructing a new
* #GtkTreeStore. It will not function after a row has been added,
* or a method on the #GtkTreeModel interface is called.
**/
void
gtk_tree_store_set_column_types (GtkTreeStore *tree_store,
gint n_columns,
GType *types)
{
gint i;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (tree_store->columns_dirty == 0);
gtk_tree_store_set_n_columns (tree_store, n_columns);
for (i = 0; i < n_columns; i++)
{
if (! _gtk_tree_data_list_check_type (types[i]))
{
g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (types[i]));
continue;
}
gtk_tree_store_set_column_type (tree_store, i, types[i]);
}
}
static void
gtk_tree_store_set_n_columns (GtkTreeStore *tree_store,
gint n_columns)
{
int i;
if (tree_store->n_columns == n_columns)
return;
tree_store->column_headers = g_renew (GType, tree_store->column_headers, n_columns);
for (i = tree_store->n_columns; i < n_columns; i++)
tree_store->column_headers[i] = G_TYPE_INVALID;
tree_store->n_columns = n_columns;
if (tree_store->sort_list)
_gtk_tree_data_list_header_free (tree_store->sort_list);
tree_store->sort_list = _gtk_tree_data_list_header_new (n_columns, tree_store->column_headers);
}
/**
* gtk_tree_store_set_column_type:
* @tree_store: a #GtkTreeStore
* @column: column number
* @type: type of the data to be stored in @column
*
* Supported types include: %G_TYPE_UINT, %G_TYPE_INT, %G_TYPE_UCHAR,
* %G_TYPE_CHAR, %G_TYPE_BOOLEAN, %G_TYPE_POINTER, %G_TYPE_FLOAT,
* %G_TYPE_DOUBLE, %G_TYPE_STRING, %G_TYPE_OBJECT, and %G_TYPE_BOXED, along with
* subclasses of those types such as %GDK_TYPE_PIXBUF.
*
**/
static void
gtk_tree_store_set_column_type (GtkTreeStore *tree_store,
gint column,
GType type)
{
if (!_gtk_tree_data_list_check_type (type))
{
g_warning ("%s: Invalid type %s\n", G_STRLOC, g_type_name (type));
return;
}
tree_store->column_headers[column] = type;
}
static gboolean
node_free (GNode *node, gpointer data)
{
if (node->data)
_gtk_tree_data_list_free (node->data, (GType*)data);
node->data = NULL;
return FALSE;
}
static void
gtk_tree_store_finalize (GObject *object)
{
GtkTreeStore *tree_store = GTK_TREE_STORE (object);
g_node_traverse (tree_store->root, G_POST_ORDER, G_TRAVERSE_ALL, -1,
node_free, tree_store->column_headers);
g_node_destroy (tree_store->root);
_gtk_tree_data_list_header_free (tree_store->sort_list);
g_free (tree_store->column_headers);
if (tree_store->default_sort_destroy)
{
GDestroyNotify d = tree_store->default_sort_destroy;
tree_store->default_sort_destroy = NULL;
d (tree_store->default_sort_data);
tree_store->default_sort_data = NULL;
}
/* must chain up */
G_OBJECT_CLASS (gtk_tree_store_parent_class)->finalize (object);
}
/* fulfill the GtkTreeModel requirements */
/* NOTE: GtkTreeStore::root is a GNode, that acts as the parent node. However,
* it is not visible to the tree or to the user., and the path "0" refers to the
* first child of GtkTreeStore::root.
*/
static GtkTreeModelFlags
gtk_tree_store_get_flags (GtkTreeModel *tree_model)
{
return GTK_TREE_MODEL_ITERS_PERSIST;
}
static gint
gtk_tree_store_get_n_columns (GtkTreeModel *tree_model)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
tree_store->columns_dirty = TRUE;
return tree_store->n_columns;
}
static GType
gtk_tree_store_get_column_type (GtkTreeModel *tree_model,
gint index)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
g_return_val_if_fail (index < tree_store->n_columns, G_TYPE_INVALID);
tree_store->columns_dirty = TRUE;
return tree_store->column_headers[index];
}
static gboolean
gtk_tree_store_get_iter (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreePath *path)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GtkTreeIter parent;
gint *indices;
gint depth, i;
tree_store->columns_dirty = TRUE;
indices = gtk_tree_path_get_indices (path);
depth = gtk_tree_path_get_depth (path);
g_return_val_if_fail (depth > 0, FALSE);
parent.stamp = tree_store->stamp;
parent.user_data = tree_store->root;
if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[0]))
return FALSE;
for (i = 1; i < depth; i++)
{
parent = *iter;
if (!gtk_tree_store_iter_nth_child (tree_model, iter, &parent, indices[i]))
return FALSE;
}
return TRUE;
}
static GtkTreePath *
gtk_tree_store_get_path (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GtkTreePath *retval;
GNode *tmp_node;
gint i = 0;
g_return_val_if_fail (iter->user_data != NULL, NULL);
g_return_val_if_fail (iter->stamp == tree_store->stamp, NULL);
validate_tree (tree_store);
if (G_NODE (iter->user_data)->parent == NULL &&
G_NODE (iter->user_data) == tree_store->root)
return gtk_tree_path_new ();
g_assert (G_NODE (iter->user_data)->parent != NULL);
if (G_NODE (iter->user_data)->parent == G_NODE (tree_store->root))
{
retval = gtk_tree_path_new ();
tmp_node = G_NODE (tree_store->root)->children;
}
else
{
GtkTreeIter tmp_iter = *iter;
tmp_iter.user_data = G_NODE (iter->user_data)->parent;
retval = gtk_tree_store_get_path (tree_model, &tmp_iter);
tmp_node = G_NODE (iter->user_data)->parent->children;
}
if (retval == NULL)
return NULL;
if (tmp_node == NULL)
{
gtk_tree_path_free (retval);
return NULL;
}
for (; tmp_node; tmp_node = tmp_node->next)
{
if (tmp_node == G_NODE (iter->user_data))
break;
i++;
}
if (tmp_node == NULL)
{
/* We couldn't find node, meaning it's prolly not ours */
/* Perhaps I should do a g_return_if_fail here. */
gtk_tree_path_free (retval);
return NULL;
}
gtk_tree_path_append_index (retval, i);
return retval;
}
static void
gtk_tree_store_get_value (GtkTreeModel *tree_model,
GtkTreeIter *iter,
gint column,
GValue *value)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GtkTreeDataList *list;
gint tmp_column = column;
g_return_if_fail (column < tree_store->n_columns);
g_return_if_fail (VALID_ITER (iter, tree_store));
list = G_NODE (iter->user_data)->data;
while (tmp_column-- > 0 && list)
list = list->next;
if (list)
{
_gtk_tree_data_list_node_to_value (list,
tree_store->column_headers[column],
value);
}
else
{
/* We want to return an initialized but empty (default) value */
g_value_init (value, tree_store->column_headers[column]);
}
}
static gboolean
gtk_tree_store_iter_next (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
g_return_val_if_fail (iter->user_data != NULL, FALSE);
g_return_val_if_fail (iter->stamp == GTK_TREE_STORE (tree_model)->stamp, FALSE);
if (G_NODE (iter->user_data)->next)
{
iter->user_data = G_NODE (iter->user_data)->next;
return TRUE;
}
else
{
iter->stamp = 0;
return FALSE;
}
}
static gboolean
gtk_tree_store_iter_children (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GNode *children;
if (parent)
g_return_val_if_fail (VALID_ITER (parent, tree_store), FALSE);
if (parent)
children = G_NODE (parent->user_data)->children;
else
children = G_NODE (tree_store->root)->children;
if (children)
{
iter->stamp = tree_store->stamp;
iter->user_data = children;
return TRUE;
}
else
{
iter->stamp = 0;
return FALSE;
}
}
static gboolean
gtk_tree_store_iter_has_child (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
g_return_val_if_fail (iter->user_data != NULL, FALSE);
g_return_val_if_fail (VALID_ITER (iter, tree_model), FALSE);
return G_NODE (iter->user_data)->children != NULL;
}
static gint
gtk_tree_store_iter_n_children (GtkTreeModel *tree_model,
GtkTreeIter *iter)
{
GNode *node;
gint i = 0;
g_return_val_if_fail (iter == NULL || iter->user_data != NULL, 0);
if (iter == NULL)
node = G_NODE (GTK_TREE_STORE (tree_model)->root)->children;
else
node = G_NODE (iter->user_data)->children;
while (node)
{
i++;
node = node->next;
}
return i;
}
static gboolean
gtk_tree_store_iter_nth_child (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint n)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GNode *parent_node;
GNode *child;
g_return_val_if_fail (parent == NULL || parent->user_data != NULL, FALSE);
if (parent == NULL)
parent_node = tree_store->root;
else
parent_node = parent->user_data;
child = g_node_nth_child (parent_node, n);
if (child)
{
iter->user_data = child;
iter->stamp = tree_store->stamp;
return TRUE;
}
else
{
iter->stamp = 0;
return FALSE;
}
}
static gboolean
gtk_tree_store_iter_parent (GtkTreeModel *tree_model,
GtkTreeIter *iter,
GtkTreeIter *child)
{
GtkTreeStore *tree_store = (GtkTreeStore *) tree_model;
GNode *parent;
g_return_val_if_fail (iter != NULL, FALSE);
g_return_val_if_fail (VALID_ITER (child, tree_store), FALSE);
parent = G_NODE (child->user_data)->parent;
g_assert (parent != NULL);
if (parent != tree_store->root)
{
iter->user_data = parent;
iter->stamp = tree_store->stamp;
return TRUE;
}
else
{
iter->stamp = 0;
return FALSE;
}
}
/* Does not emit a signal */
static gboolean
gtk_tree_store_real_set_value (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gint column,
GValue *value,
gboolean sort)
{
GtkTreeDataList *list;
GtkTreeDataList *prev;
gint old_column = column;
GValue real_value = {0, };
gboolean converted = FALSE;
gboolean retval = FALSE;
if (! g_type_is_a (G_VALUE_TYPE (value), tree_store->column_headers[column]))
{
if (! (g_value_type_compatible (G_VALUE_TYPE (value), tree_store->column_headers[column]) &&
g_value_type_compatible (tree_store->column_headers[column], G_VALUE_TYPE (value))))
{
g_warning ("%s: Unable to convert from %s to %s\n",
G_STRLOC,
g_type_name (G_VALUE_TYPE (value)),
g_type_name (tree_store->column_headers[column]));
return retval;
}
if (!g_value_transform (value, &real_value))
{
g_warning ("%s: Unable to make conversion from %s to %s\n",
G_STRLOC,
g_type_name (G_VALUE_TYPE (value)),
g_type_name (tree_store->column_headers[column]));
g_value_unset (&real_value);
return retval;
}
converted = TRUE;
}
prev = list = G_NODE (iter->user_data)->data;
while (list != NULL)
{
if (column == 0)
{
if (converted)
_gtk_tree_data_list_value_to_node (list, &real_value);
else
_gtk_tree_data_list_value_to_node (list, value);
retval = TRUE;
if (converted)
g_value_unset (&real_value);
if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
return retval;
}
column--;
prev = list;
list = list->next;
}
if (G_NODE (iter->user_data)->data == NULL)
{
G_NODE (iter->user_data)->data = list = _gtk_tree_data_list_alloc ();
list->next = NULL;
}
else
{
list = prev->next = _gtk_tree_data_list_alloc ();
list->next = NULL;
}
while (column != 0)
{
list->next = _gtk_tree_data_list_alloc ();
list = list->next;
list->next = NULL;
column --;
}
if (converted)
_gtk_tree_data_list_value_to_node (list, &real_value);
else
_gtk_tree_data_list_value_to_node (list, value);
retval = TRUE;
if (converted)
g_value_unset (&real_value);
if (sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, old_column, TRUE);
return retval;
}
/**
* gtk_tree_store_set_value:
* @tree_store: a #GtkTreeStore
* @iter: A valid #GtkTreeIter for the row being modified
* @column: column number to modify
* @value: new value for the cell
*
* Sets the data in the cell specified by @iter and @column.
* The type of @value must be convertible to the type of the
* column.
*
**/
void
gtk_tree_store_set_value (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gint column,
GValue *value)
{
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (VALID_ITER (iter, tree_store));
g_return_if_fail (column >= 0 && column < tree_store->n_columns);
g_return_if_fail (G_IS_VALUE (value));
if (gtk_tree_store_real_set_value (tree_store, iter, column, value, TRUE))
{
GtkTreePath *path;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
gtk_tree_path_free (path);
}
}
static GtkTreeIterCompareFunc
gtk_tree_store_get_compare_func (GtkTreeStore *tree_store)
{
GtkTreeIterCompareFunc func = NULL;
if (GTK_TREE_STORE_IS_SORTED (tree_store))
{
if (tree_store->sort_column_id != -1)
{
GtkTreeDataSortHeader *header;
header = _gtk_tree_data_list_get_header (tree_store->sort_list,
tree_store->sort_column_id);
g_return_val_if_fail (header != NULL, NULL);
g_return_val_if_fail (header->func != NULL, NULL);
func = header->func;
}
else
{
func = tree_store->default_sort_func;
}
}
return func;
}
static void
gtk_tree_store_set_vector_internal (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gboolean *emit_signal,
gboolean *maybe_need_sort,
gint *columns,
GValue *values,
gint n_values)
{
gint i;
GtkTreeIterCompareFunc func = NULL;
func = gtk_tree_store_get_compare_func (tree_store);
if (func != _gtk_tree_data_list_compare_func)
*maybe_need_sort = TRUE;
for (i = 0; i < n_values; i++)
{
*emit_signal = gtk_tree_store_real_set_value (tree_store, iter,
columns[i], &values[i],
FALSE) || *emit_signal;
if (func == _gtk_tree_data_list_compare_func &&
columns[i] == tree_store->sort_column_id)
*maybe_need_sort = TRUE;
}
}
static void
gtk_tree_store_set_valist_internal (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gboolean *emit_signal,
gboolean *maybe_need_sort,
va_list var_args)
{
gint column;
GtkTreeIterCompareFunc func = NULL;
column = va_arg (var_args, gint);
func = gtk_tree_store_get_compare_func (tree_store);
if (func != _gtk_tree_data_list_compare_func)
*maybe_need_sort = TRUE;
while (column != -1)
{
GValue value = { 0, };
gchar *error = NULL;
if (column < 0 || column >= tree_store->n_columns)
{
g_warning ("%s: Invalid column number %d added to iter (remember to end your list of columns with a -1)", G_STRLOC, column);
break;
}
g_value_init (&value, tree_store->column_headers[column]);
G_VALUE_COLLECT (&value, var_args, 0, &error);
if (error)
{
g_warning ("%s: %s", G_STRLOC, error);
g_free (error);
/* we purposely leak the value here, it might not be
* in a sane state if an error condition occoured
*/
break;
}
*emit_signal = gtk_tree_store_real_set_value (tree_store,
iter,
column,
&value,
FALSE) || *emit_signal;
if (func == _gtk_tree_data_list_compare_func &&
column == tree_store->sort_column_id)
*maybe_need_sort = TRUE;
g_value_unset (&value);
column = va_arg (var_args, gint);
}
}
/**
* gtk_tree_store_set_valuesv:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter for the row being modified
* @columns: (array length=n_values): an array of column numbers
* @values: (array length=n_values): an array of GValues
* @n_values: the length of the @columns and @values arrays
*
* A variant of gtk_tree_store_set_valist() which takes
* the columns and values as two arrays, instead of varargs. This
* function is mainly intended for language bindings or in case
* the number of columns to change is not known until run-time.
*
* Since: 2.12
**/
void
gtk_tree_store_set_valuesv (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gint *columns,
GValue *values,
gint n_values)
{
gboolean emit_signal = FALSE;
gboolean maybe_need_sort = FALSE;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (VALID_ITER (iter, tree_store));
gtk_tree_store_set_vector_internal (tree_store, iter,
&emit_signal,
&maybe_need_sort,
columns, values, n_values);
if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
if (emit_signal)
{
GtkTreePath *path;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
gtk_tree_path_free (path);
}
}
/**
* gtk_tree_store_set_valist:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter for the row being modified
* @var_args: <type>va_list</type> of column/value pairs
*
* See gtk_tree_store_set(); this version takes a <type>va_list</type> for
* use by language bindings.
*
**/
void
gtk_tree_store_set_valist (GtkTreeStore *tree_store,
GtkTreeIter *iter,
va_list var_args)
{
gboolean emit_signal = FALSE;
gboolean maybe_need_sort = FALSE;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (VALID_ITER (iter, tree_store));
gtk_tree_store_set_valist_internal (tree_store, iter,
&emit_signal,
&maybe_need_sort,
var_args);
if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, TRUE);
if (emit_signal)
{
GtkTreePath *path;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, iter);
gtk_tree_path_free (path);
}
}
/**
* gtk_tree_store_set:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter for the row being modified
* @Varargs: pairs of column number and value, terminated with -1
*
* Sets the value of one or more cells in the row referenced by @iter.
* The variable argument list should contain integer column numbers,
* each column number followed by the value to be set.
* The list is terminated by a -1. For example, to set column 0 with type
* %G_TYPE_STRING to "Foo", you would write
* <literal>gtk_tree_store_set (store, iter, 0, "Foo", -1)</literal>.
* The value will be copied or referenced by the store if appropriate.
**/
void
gtk_tree_store_set (GtkTreeStore *tree_store,
GtkTreeIter *iter,
...)
{
va_list var_args;
va_start (var_args, iter);
gtk_tree_store_set_valist (tree_store, iter, var_args);
va_end (var_args);
}
/**
* gtk_tree_store_remove:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter
*
* Removes @iter from @tree_store. After being removed, @iter is set to the
* next valid row at that level, or invalidated if it previously pointed to the
* last one.
*
* Return value: %TRUE if @iter is still valid, %FALSE if not.
**/
gboolean
gtk_tree_store_remove (GtkTreeStore *tree_store,
GtkTreeIter *iter)
{
GtkTreePath *path;
GtkTreeIter new_iter = {0,};
GNode *parent;
GNode *next_node;
g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
parent = G_NODE (iter->user_data)->parent;
g_assert (parent != NULL);
next_node = G_NODE (iter->user_data)->next;
if (G_NODE (iter->user_data)->data)
g_node_traverse (G_NODE (iter->user_data), G_POST_ORDER, G_TRAVERSE_ALL,
-1, node_free, tree_store->column_headers);
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
g_node_destroy (G_NODE (iter->user_data));
gtk_tree_model_row_deleted (GTK_TREE_MODEL (tree_store), path);
if (parent != G_NODE (tree_store->root))
{
/* child_toggled */
if (parent->children == NULL)
{
gtk_tree_path_up (path);
new_iter.stamp = tree_store->stamp;
new_iter.user_data = parent;
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &new_iter);
}
}
gtk_tree_path_free (path);
/* revalidate iter */
if (next_node != NULL)
{
iter->stamp = tree_store->stamp;
iter->user_data = next_node;
return TRUE;
}
else
{
iter->stamp = 0;
iter->user_data = NULL;
}
return FALSE;
}
/**
* gtk_tree_store_insert:
* @tree_store: A #GtkTreeStore
* @iter: An unset #GtkTreeIter to set to the new row
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
* @position: position to insert the new row
*
* Creates a new row at @position. If parent is non-%NULL, then the row will be
* made a child of @parent. Otherwise, the row will be created at the toplevel.
* If @position is larger than the number of rows at that level, then the new
* row will be inserted to the end of the list. @iter will be changed to point
* to this new row. The row will be empty after this function is called. To
* fill in values, you need to call gtk_tree_store_set() or
* gtk_tree_store_set_value().
*
**/
void
gtk_tree_store_insert (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint position)
{
GtkTreePath *path;
GNode *parent_node;
GNode *new_node;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (iter != NULL);
if (parent)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (parent)
parent_node = parent->user_data;
else
parent_node = tree_store->root;
tree_store->columns_dirty = TRUE;
new_node = g_node_new (NULL);
iter->stamp = tree_store->stamp;
iter->user_data = new_node;
g_node_insert (parent_node, position, new_node);
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
if (new_node->prev == NULL && new_node->next == NULL)
{
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
}
}
gtk_tree_path_free (path);
validate_tree ((GtkTreeStore*)tree_store);
}
/**
* gtk_tree_store_insert_before:
* @tree_store: A #GtkTreeStore
* @iter: An unset #GtkTreeIter to set to the new row
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
* @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
*
* Inserts a new row before @sibling. If @sibling is %NULL, then the row will
* be appended to @parent 's children. If @parent and @sibling are %NULL, then
* the row will be appended to the toplevel. If both @sibling and @parent are
* set, then @parent must be the parent of @sibling. When @sibling is set,
* @parent is optional.
*
* @iter will be changed to point to this new row. The row will be empty after
* this function is called. To fill in values, you need to call
* gtk_tree_store_set() or gtk_tree_store_set_value().
*
**/
void
gtk_tree_store_insert_before (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent,
GtkTreeIter *sibling)
{
GtkTreePath *path;
GNode *parent_node = NULL;
GNode *new_node;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (iter != NULL);
if (parent != NULL)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (sibling != NULL)
g_return_if_fail (VALID_ITER (sibling, tree_store));
if (parent == NULL && sibling == NULL)
parent_node = tree_store->root;
else if (parent == NULL)
parent_node = G_NODE (sibling->user_data)->parent;
else if (sibling == NULL)
parent_node = G_NODE (parent->user_data);
else
{
g_return_if_fail (G_NODE (sibling->user_data)->parent == G_NODE (parent->user_data));
parent_node = G_NODE (parent->user_data);
}
tree_store->columns_dirty = TRUE;
new_node = g_node_new (NULL);
g_node_insert_before (parent_node,
sibling ? G_NODE (sibling->user_data) : NULL,
new_node);
iter->stamp = tree_store->stamp;
iter->user_data = new_node;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
if (new_node->prev == NULL && new_node->next == NULL)
{
GtkTreeIter parent_iter;
parent_iter.stamp = tree_store->stamp;
parent_iter.user_data = parent_node;
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
}
}
gtk_tree_path_free (path);
validate_tree (tree_store);
}
/**
* gtk_tree_store_insert_after:
* @tree_store: A #GtkTreeStore
* @iter: An unset #GtkTreeIter to set to the new row
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
* @sibling: (allow-none): A valid #GtkTreeIter, or %NULL
*
* Inserts a new row after @sibling. If @sibling is %NULL, then the row will be
* prepended to @parent 's children. If @parent and @sibling are %NULL, then
* the row will be prepended to the toplevel. If both @sibling and @parent are
* set, then @parent must be the parent of @sibling. When @sibling is set,
* @parent is optional.
*
* @iter will be changed to point to this new row. The row will be empty after
* this function is called. To fill in values, you need to call
* gtk_tree_store_set() or gtk_tree_store_set_value().
*
**/
void
gtk_tree_store_insert_after (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent,
GtkTreeIter *sibling)
{
GtkTreePath *path;
GNode *parent_node;
GNode *new_node;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (iter != NULL);
if (parent != NULL)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (sibling != NULL)
g_return_if_fail (VALID_ITER (sibling, tree_store));
if (parent == NULL && sibling == NULL)
parent_node = tree_store->root;
else if (parent == NULL)
parent_node = G_NODE (sibling->user_data)->parent;
else if (sibling == NULL)
parent_node = G_NODE (parent->user_data);
else
{
g_return_if_fail (G_NODE (sibling->user_data)->parent ==
G_NODE (parent->user_data));
parent_node = G_NODE (parent->user_data);
}
tree_store->columns_dirty = TRUE;
new_node = g_node_new (NULL);
g_node_insert_after (parent_node,
sibling ? G_NODE (sibling->user_data) : NULL,
new_node);
iter->stamp = tree_store->stamp;
iter->user_data = new_node;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
if (new_node->prev == NULL && new_node->next == NULL)
{
GtkTreeIter parent_iter;
parent_iter.stamp = tree_store->stamp;
parent_iter.user_data = parent_node;
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, &parent_iter);
}
}
gtk_tree_path_free (path);
validate_tree (tree_store);
}
/**
* gtk_tree_store_insert_with_values:
* @tree_store: A #GtkTreeStore
* @iter: (allow-none): An unset #GtkTreeIter to set the new row, or %NULL.
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
* @position: position to insert the new row
* @Varargs: pairs of column number and value, terminated with -1
*
* Creates a new row at @position. @iter will be changed to point to this
* new row. If @position is larger than the number of rows on the list, then
* the new row will be appended to the list. The row will be filled with
* the values given to this function.
*
* Calling
* <literal>gtk_tree_store_insert_with_values (tree_store, iter, position, ...)</literal>
* has the same effect as calling
* |[
* gtk_tree_store_insert (tree_store, iter, position);
* gtk_tree_store_set (tree_store, iter, ...);
* ]|
* with the different that the former will only emit a row_inserted signal,
* while the latter will emit row_inserted, row_changed and if the tree store
* is sorted, rows_reordered. Since emitting the rows_reordered signal
* repeatedly can affect the performance of the program,
* gtk_tree_store_insert_with_values() should generally be preferred when
* inserting rows in a sorted tree store.
*
* Since: 2.10
*/
void
gtk_tree_store_insert_with_values (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint position,
...)
{
GtkTreePath *path;
GNode *parent_node;
GNode *new_node;
GtkTreeIter tmp_iter;
va_list var_args;
gboolean changed = FALSE;
gboolean maybe_need_sort = FALSE;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
if (!iter)
iter = &tmp_iter;
if (parent)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (parent)
parent_node = parent->user_data;
else
parent_node = tree_store->root;
tree_store->columns_dirty = TRUE;
new_node = g_node_new (NULL);
iter->stamp = tree_store->stamp;
iter->user_data = new_node;
g_node_insert (parent_node, position, new_node);
va_start (var_args, position);
gtk_tree_store_set_valist_internal (tree_store, iter,
&changed, &maybe_need_sort,
var_args);
va_end (var_args);
if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
if (new_node->prev == NULL && new_node->next == NULL)
{
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
}
}
gtk_tree_path_free (path);
validate_tree ((GtkTreeStore *)tree_store);
}
/**
* gtk_tree_store_insert_with_valuesv:
* @tree_store: A #GtkTreeStore
* @iter: (allow-none): An unset #GtkTreeIter to set the new row, or %NULL.
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
* @position: position to insert the new row
* @columns: an array of column numbers
* @values: an array of GValues
* @n_values: the length of the @columns and @values arrays
*
* A variant of gtk_tree_store_insert_with_values() which takes
* the columns and values as two arrays, instead of varargs. This
* function is mainly intended for language bindings.
*
* Since: 2.10
*/
void
gtk_tree_store_insert_with_valuesv (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent,
gint position,
gint *columns,
GValue *values,
gint n_values)
{
GtkTreePath *path;
GNode *parent_node;
GNode *new_node;
GtkTreeIter tmp_iter;
gboolean changed = FALSE;
gboolean maybe_need_sort = FALSE;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
if (!iter)
iter = &tmp_iter;
if (parent)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (parent)
parent_node = parent->user_data;
else
parent_node = tree_store->root;
tree_store->columns_dirty = TRUE;
new_node = g_node_new (NULL);
iter->stamp = tree_store->stamp;
iter->user_data = new_node;
g_node_insert (parent_node, position, new_node);
gtk_tree_store_set_vector_internal (tree_store, iter,
&changed, &maybe_need_sort,
columns, values, n_values);
if (maybe_need_sort && GTK_TREE_STORE_IS_SORTED (tree_store))
gtk_tree_store_sort_iter_changed (tree_store, iter, tree_store->sort_column_id, FALSE);
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
if (new_node->prev == NULL && new_node->next == NULL)
{
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
}
}
gtk_tree_path_free (path);
validate_tree ((GtkTreeStore *)tree_store);
}
/**
* gtk_tree_store_prepend:
* @tree_store: A #GtkTreeStore
* @iter: An unset #GtkTreeIter to set to the prepended row
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
*
* Prepends a new row to @tree_store. If @parent is non-%NULL, then it will prepend
* the new row before the first child of @parent, otherwise it will prepend a row
* to the top level. @iter will be changed to point to this new row. The row
* will be empty after this function is called. To fill in values, you need to
* call gtk_tree_store_set() or gtk_tree_store_set_value().
**/
void
gtk_tree_store_prepend (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
GNode *parent_node;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (iter != NULL);
if (parent != NULL)
g_return_if_fail (VALID_ITER (parent, tree_store));
tree_store->columns_dirty = TRUE;
if (parent == NULL)
parent_node = tree_store->root;
else
parent_node = parent->user_data;
if (parent_node->children == NULL)
{
GtkTreePath *path;
iter->stamp = tree_store->stamp;
iter->user_data = g_node_new (NULL);
g_node_prepend (parent_node, G_NODE (iter->user_data));
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
}
gtk_tree_path_free (path);
}
else
{
gtk_tree_store_insert_after (tree_store, iter, parent, NULL);
}
validate_tree (tree_store);
}
/**
* gtk_tree_store_append:
* @tree_store: A #GtkTreeStore
* @iter: An unset #GtkTreeIter to set to the appended row
* @parent: (allow-none): A valid #GtkTreeIter, or %NULL
*
* Appends a new row to @tree_store. If @parent is non-%NULL, then it will append the
* new row after the last child of @parent, otherwise it will append a row to
* the top level. @iter will be changed to point to this new row. The row will
* be empty after this function is called. To fill in values, you need to call
* gtk_tree_store_set() or gtk_tree_store_set_value().
**/
void
gtk_tree_store_append (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *parent)
{
GNode *parent_node;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (iter != NULL);
if (parent != NULL)
g_return_if_fail (VALID_ITER (parent, tree_store));
if (parent == NULL)
parent_node = tree_store->root;
else
parent_node = parent->user_data;
tree_store->columns_dirty = TRUE;
if (parent_node->children == NULL)
{
GtkTreePath *path;
iter->stamp = tree_store->stamp;
iter->user_data = g_node_new (NULL);
g_node_append (parent_node, G_NODE (iter->user_data));
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_model_row_inserted (GTK_TREE_MODEL (tree_store), path, iter);
if (parent_node != tree_store->root)
{
gtk_tree_path_up (path);
gtk_tree_model_row_has_child_toggled (GTK_TREE_MODEL (tree_store), path, parent);
}
gtk_tree_path_free (path);
}
else
{
gtk_tree_store_insert_before (tree_store, iter, parent, NULL);
}
validate_tree (tree_store);
}
/**
* gtk_tree_store_is_ancestor:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter
* @descendant: A valid #GtkTreeIter
*
* Returns %TRUE if @iter is an ancestor of @descendant. That is, @iter is the
* parent (or grandparent or great-grandparent) of @descendant.
*
* Return value: %TRUE, if @iter is an ancestor of @descendant
**/
gboolean
gtk_tree_store_is_ancestor (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *descendant)
{
g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
g_return_val_if_fail (VALID_ITER (iter, tree_store), FALSE);
g_return_val_if_fail (VALID_ITER (descendant, tree_store), FALSE);
return g_node_is_ancestor (G_NODE (iter->user_data),
G_NODE (descendant->user_data));
}
/**
* gtk_tree_store_iter_depth:
* @tree_store: A #GtkTreeStore
* @iter: A valid #GtkTreeIter
*
* Returns the depth of @iter. This will be 0 for anything on the root level, 1
* for anything down a level, etc.
*
* Return value: The depth of @iter
**/
gint
gtk_tree_store_iter_depth (GtkTreeStore *tree_store,
GtkTreeIter *iter)
{
g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), 0);
g_return_val_if_fail (VALID_ITER (iter, tree_store), 0);
return g_node_depth (G_NODE (iter->user_data)) - 2;
}
/* simple ripoff from g_node_traverse_post_order */
static gboolean
gtk_tree_store_clear_traverse (GNode *node,
GtkTreeStore *store)
{
GtkTreeIter iter;
if (node->children)
{
GNode *child;
child = node->children;
while (child)
{
register GNode *current;
current = child;
child = current->next;
if (gtk_tree_store_clear_traverse (current, store))
return TRUE;
}
if (node->parent)
{
iter.stamp = store->stamp;
iter.user_data = node;
gtk_tree_store_remove (store, &iter);
}
}
else if (node->parent)
{
iter.stamp = store->stamp;
iter.user_data = node;
gtk_tree_store_remove (store, &iter);
}
return FALSE;
}
static void
gtk_tree_store_increment_stamp (GtkTreeStore *tree_store)
{
do
{
tree_store->stamp++;
}
while (tree_store->stamp == 0);
}
/**
* gtk_tree_store_clear:
* @tree_store: a #GtkTreeStore
*
* Removes all rows from @tree_store
**/
void
gtk_tree_store_clear (GtkTreeStore *tree_store)
{
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
gtk_tree_store_clear_traverse (tree_store->root, tree_store);
gtk_tree_store_increment_stamp (tree_store);
}
static gboolean
gtk_tree_store_iter_is_valid_helper (GtkTreeIter *iter,
GNode *first)
{
GNode *node;
node = first;
do
{
if (node == iter->user_data)
return TRUE;
if (node->children)
if (gtk_tree_store_iter_is_valid_helper (iter, node->children))
return TRUE;
node = node->next;
}
while (node);
return FALSE;
}
/**
* gtk_tree_store_iter_is_valid:
* @tree_store: A #GtkTreeStore.
* @iter: A #GtkTreeIter.
*
* WARNING: This function is slow. Only use it for debugging and/or testing
* purposes.
*
* Checks if the given iter is a valid iter for this #GtkTreeStore.
*
* Return value: %TRUE if the iter is valid, %FALSE if the iter is invalid.
*
* Since: 2.2
**/
gboolean
gtk_tree_store_iter_is_valid (GtkTreeStore *tree_store,
GtkTreeIter *iter)
{
g_return_val_if_fail (GTK_IS_TREE_STORE (tree_store), FALSE);
g_return_val_if_fail (iter != NULL, FALSE);
if (!VALID_ITER (iter, tree_store))
return FALSE;
return gtk_tree_store_iter_is_valid_helper (iter, tree_store->root);
}
/* DND */
static gboolean real_gtk_tree_store_row_draggable (GtkTreeDragSource *drag_source,
GtkTreePath *path)
{
return TRUE;
}
static gboolean
gtk_tree_store_drag_data_delete (GtkTreeDragSource *drag_source,
GtkTreePath *path)
{
GtkTreeIter iter;
if (gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_source),
&iter,
path))
{
gtk_tree_store_remove (GTK_TREE_STORE (drag_source),
&iter);
return TRUE;
}
else
{
return FALSE;
}
}
static gboolean
gtk_tree_store_drag_data_get (GtkTreeDragSource *drag_source,
GtkTreePath *path,
GtkSelectionData *selection_data)
{
/* Note that we don't need to handle the GTK_TREE_MODEL_ROW
* target, because the default handler does it for us, but
* we do anyway for the convenience of someone maybe overriding the
* default handler.
*/
if (gtk_tree_set_row_drag_data (selection_data,
GTK_TREE_MODEL (drag_source),
path))
{
return TRUE;
}
else
{
/* FIXME handle text targets at least. */
}
return FALSE;
}
static void
copy_node_data (GtkTreeStore *tree_store,
GtkTreeIter *src_iter,
GtkTreeIter *dest_iter)
{
GtkTreeDataList *dl = G_NODE (src_iter->user_data)->data;
GtkTreeDataList *copy_head = NULL;
GtkTreeDataList *copy_prev = NULL;
GtkTreeDataList *copy_iter = NULL;
GtkTreePath *path;
gint col;
col = 0;
while (dl)
{
copy_iter = _gtk_tree_data_list_node_copy (dl, tree_store->column_headers[col]);
if (copy_head == NULL)
copy_head = copy_iter;
if (copy_prev)
copy_prev->next = copy_iter;
copy_prev = copy_iter;
dl = dl->next;
++col;
}
G_NODE (dest_iter->user_data)->data = copy_head;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), dest_iter);
gtk_tree_model_row_changed (GTK_TREE_MODEL (tree_store), path, dest_iter);
gtk_tree_path_free (path);
}
static void
recursive_node_copy (GtkTreeStore *tree_store,
GtkTreeIter *src_iter,
GtkTreeIter *dest_iter)
{
GtkTreeIter child;
GtkTreeModel *model;
model = GTK_TREE_MODEL (tree_store);
copy_node_data (tree_store, src_iter, dest_iter);
if (gtk_tree_store_iter_children (model,
&child,
src_iter))
{
/* Need to create children and recurse. Note our
* dependence on persistent iterators here.
*/
do
{
GtkTreeIter copy;
/* Gee, a really slow algorithm... ;-) FIXME */
gtk_tree_store_append (tree_store,
©,
dest_iter);
recursive_node_copy (tree_store, &child, ©);
}
while (gtk_tree_store_iter_next (model, &child));
}
}
static gboolean
gtk_tree_store_drag_data_received (GtkTreeDragDest *drag_dest,
GtkTreePath *dest,
GtkSelectionData *selection_data)
{
GtkTreeModel *tree_model;
GtkTreeStore *tree_store;
GtkTreeModel *src_model = NULL;
GtkTreePath *src_path = NULL;
gboolean retval = FALSE;
tree_model = GTK_TREE_MODEL (drag_dest);
tree_store = GTK_TREE_STORE (drag_dest);
validate_tree (tree_store);
if (gtk_tree_get_row_drag_data (selection_data,
&src_model,
&src_path) &&
src_model == tree_model)
{
/* Copy the given row to a new position */
GtkTreeIter src_iter;
GtkTreeIter dest_iter;
GtkTreePath *prev;
if (!gtk_tree_store_get_iter (src_model,
&src_iter,
src_path))
{
goto out;
}
/* Get the path to insert _after_ (dest is the path to insert _before_) */
prev = gtk_tree_path_copy (dest);
if (!gtk_tree_path_prev (prev))
{
GtkTreeIter dest_parent;
GtkTreePath *parent;
GtkTreeIter *dest_parent_p;
/* dest was the first spot at the current depth; which means
* we are supposed to prepend.
*/
/* Get the parent, NULL if parent is the root */
dest_parent_p = NULL;
parent = gtk_tree_path_copy (dest);
if (gtk_tree_path_up (parent) &&
gtk_tree_path_get_depth (parent) > 0)
{
gtk_tree_store_get_iter (tree_model,
&dest_parent,
parent);
dest_parent_p = &dest_parent;
}
gtk_tree_path_free (parent);
parent = NULL;
gtk_tree_store_prepend (tree_store,
&dest_iter,
dest_parent_p);
retval = TRUE;
}
else
{
if (gtk_tree_store_get_iter (tree_model, &dest_iter, prev))
{
GtkTreeIter tmp_iter = dest_iter;
gtk_tree_store_insert_after (tree_store, &dest_iter, NULL,
&tmp_iter);
retval = TRUE;
}
}
gtk_tree_path_free (prev);
/* If we succeeded in creating dest_iter, walk src_iter tree branch,
* duplicating it below dest_iter.
*/
if (retval)
{
recursive_node_copy (tree_store,
&src_iter,
&dest_iter);
}
}
else
{
/* FIXME maybe add some data targets eventually, or handle text
* targets in the simple case.
*/
}
out:
if (src_path)
gtk_tree_path_free (src_path);
return retval;
}
static gboolean
gtk_tree_store_row_drop_possible (GtkTreeDragDest *drag_dest,
GtkTreePath *dest_path,
GtkSelectionData *selection_data)
{
GtkTreeModel *src_model = NULL;
GtkTreePath *src_path = NULL;
GtkTreePath *tmp = NULL;
gboolean retval = FALSE;
/* don't accept drops if the tree has been sorted */
if (GTK_TREE_STORE_IS_SORTED (drag_dest))
return FALSE;
if (!gtk_tree_get_row_drag_data (selection_data,
&src_model,
&src_path))
goto out;
/* can only drag to ourselves */
if (src_model != GTK_TREE_MODEL (drag_dest))
goto out;
/* Can't drop into ourself. */
if (gtk_tree_path_is_ancestor (src_path,
dest_path))
goto out;
/* Can't drop if dest_path's parent doesn't exist */
{
GtkTreeIter iter;
if (gtk_tree_path_get_depth (dest_path) > 1)
{
tmp = gtk_tree_path_copy (dest_path);
gtk_tree_path_up (tmp);
if (!gtk_tree_store_get_iter (GTK_TREE_MODEL (drag_dest),
&iter, tmp))
goto out;
}
}
/* Can otherwise drop anywhere. */
retval = TRUE;
out:
if (src_path)
gtk_tree_path_free (src_path);
if (tmp)
gtk_tree_path_free (tmp);
return retval;
}
/* Sorting and reordering */
typedef struct _SortTuple
{
gint offset;
GNode *node;
} SortTuple;
/* Reordering */
static gint
gtk_tree_store_reorder_func (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
SortTuple *a_reorder;
SortTuple *b_reorder;
a_reorder = (SortTuple *)a;
b_reorder = (SortTuple *)b;
if (a_reorder->offset < b_reorder->offset)
return -1;
if (a_reorder->offset > b_reorder->offset)
return 1;
return 0;
}
/**
* gtk_tree_store_reorder:
* @tree_store: A #GtkTreeStore.
* @parent: A #GtkTreeIter.
* @new_order: an array of integers mapping the new position of each child
* to its old position before the re-ordering,
* i.e. @new_order<literal>[newpos] = oldpos</literal>.
*
* Reorders the children of @parent in @tree_store to follow the order
* indicated by @new_order. Note that this function only works with
* unsorted stores.
*
* Since: 2.2
**/
void
gtk_tree_store_reorder (GtkTreeStore *tree_store,
GtkTreeIter *parent,
gint *new_order)
{
gint i, length = 0;
GNode *level, *node;
GtkTreePath *path;
SortTuple *sort_array;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
g_return_if_fail (parent == NULL || VALID_ITER (parent, tree_store));
g_return_if_fail (new_order != NULL);
if (!parent)
level = G_NODE (tree_store->root)->children;
else
level = G_NODE (parent->user_data)->children;
/* count nodes */
node = level;
while (node)
{
length++;
node = node->next;
}
/* set up sortarray */
sort_array = g_new (SortTuple, length);
node = level;
for (i = 0; i < length; i++)
{
sort_array[new_order[i]].offset = i;
sort_array[i].node = node;
node = node->next;
}
g_qsort_with_data (sort_array,
length,
sizeof (SortTuple),
gtk_tree_store_reorder_func,
NULL);
/* fix up level */
for (i = 0; i < length - 1; i++)
{
sort_array[i].node->next = sort_array[i+1].node;
sort_array[i+1].node->prev = sort_array[i].node;
}
sort_array[length-1].node->next = NULL;
sort_array[0].node->prev = NULL;
if (parent)
G_NODE (parent->user_data)->children = sort_array[0].node;
else
G_NODE (tree_store->root)->children = sort_array[0].node;
/* emit signal */
if (parent)
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), parent);
else
path = gtk_tree_path_new ();
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path,
parent, new_order);
gtk_tree_path_free (path);
g_free (sort_array);
}
/**
* gtk_tree_store_swap:
* @tree_store: A #GtkTreeStore.
* @a: A #GtkTreeIter.
* @b: Another #GtkTreeIter.
*
* Swaps @a and @b in the same level of @tree_store. Note that this function
* only works with unsorted stores.
*
* Since: 2.2
**/
void
gtk_tree_store_swap (GtkTreeStore *tree_store,
GtkTreeIter *a,
GtkTreeIter *b)
{
GNode *tmp, *node_a, *node_b, *parent_node;
GNode *a_prev, *a_next, *b_prev, *b_next;
gint i, a_count, b_count, length, *order;
GtkTreePath *path_a, *path_b;
GtkTreeIter parent;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (VALID_ITER (a, tree_store));
g_return_if_fail (VALID_ITER (b, tree_store));
node_a = G_NODE (a->user_data);
node_b = G_NODE (b->user_data);
/* basic sanity checking */
if (node_a == node_b)
return;
path_a = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), a);
path_b = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), b);
g_return_if_fail (path_a && path_b);
gtk_tree_path_up (path_a);
gtk_tree_path_up (path_b);
if (gtk_tree_path_get_depth (path_a) == 0
|| gtk_tree_path_get_depth (path_b) == 0)
{
if (gtk_tree_path_get_depth (path_a) != gtk_tree_path_get_depth (path_b))
{
gtk_tree_path_free (path_a);
gtk_tree_path_free (path_b);
g_warning ("Given children are not in the same level\n");
return;
}
parent_node = G_NODE (tree_store->root);
}
else
{
if (gtk_tree_path_compare (path_a, path_b))
{
gtk_tree_path_free (path_a);
gtk_tree_path_free (path_b);
g_warning ("Given children don't have a common parent\n");
return;
}
gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &parent,
path_a);
parent_node = G_NODE (parent.user_data);
}
gtk_tree_path_free (path_b);
/* old links which we have to keep around */
a_prev = node_a->prev;
a_next = node_a->next;
b_prev = node_b->prev;
b_next = node_b->next;
/* fix up links if the nodes are next to eachother */
if (a_prev == node_b)
a_prev = node_a;
if (a_next == node_b)
a_next = node_a;
if (b_prev == node_a)
b_prev = node_b;
if (b_next == node_a)
b_next = node_b;
/* counting nodes */
tmp = parent_node->children;
i = a_count = b_count = 0;
while (tmp)
{
if (tmp == node_a)
a_count = i;
if (tmp == node_b)
b_count = i;
tmp = tmp->next;
i++;
}
length = i;
/* hacking the tree */
if (!a_prev)
parent_node->children = node_b;
else
a_prev->next = node_b;
if (a_next)
a_next->prev = node_b;
if (!b_prev)
parent_node->children = node_a;
else
b_prev->next = node_a;
if (b_next)
b_next->prev = node_a;
node_a->prev = b_prev;
node_a->next = b_next;
node_b->prev = a_prev;
node_b->next = a_next;
/* emit signal */
order = g_new (gint, length);
for (i = 0; i < length; i++)
if (i == a_count)
order[i] = b_count;
else if (i == b_count)
order[i] = a_count;
else
order[i] = i;
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store), path_a,
parent_node == tree_store->root
? NULL : &parent, order);
gtk_tree_path_free (path_a);
g_free (order);
}
/* WARNING: this function is *incredibly* fragile. Please smashtest after
* making changes here.
* -Kris
*/
static void
gtk_tree_store_move (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *position,
gboolean before)
{
GNode *parent, *node, *a, *b, *tmp, *tmp_a, *tmp_b;
gint old_pos, new_pos, length, i, *order;
GtkTreePath *path = NULL, *tmppath, *pos_path = NULL;
GtkTreeIter parent_iter, dst_a, dst_b;
gint depth = 0;
gboolean handle_b = TRUE;
g_return_if_fail (GTK_IS_TREE_STORE (tree_store));
g_return_if_fail (!GTK_TREE_STORE_IS_SORTED (tree_store));
g_return_if_fail (VALID_ITER (iter, tree_store));
if (position)
g_return_if_fail (VALID_ITER (position, tree_store));
a = b = NULL;
/* sanity checks */
if (position)
{
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
pos_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
position);
/* if before:
* moving the iter before path or "path + 1" doesn't make sense
* else
* moving the iter before path or "path - 1" doesn't make sense
*/
if (!gtk_tree_path_compare (path, pos_path))
goto free_paths_and_out;
if (before)
gtk_tree_path_next (path);
else
gtk_tree_path_prev (path);
if (!gtk_tree_path_compare (path, pos_path))
goto free_paths_and_out;
if (before)
gtk_tree_path_prev (path);
else
gtk_tree_path_next (path);
if (gtk_tree_path_get_depth (path) != gtk_tree_path_get_depth (pos_path))
{
g_warning ("Given children are not in the same level\n");
goto free_paths_and_out;
}
tmppath = gtk_tree_path_copy (pos_path);
gtk_tree_path_up (path);
gtk_tree_path_up (tmppath);
if (gtk_tree_path_get_depth (path) > 0 &&
gtk_tree_path_compare (path, tmppath))
{
g_warning ("Given children are not in the same level\n");
gtk_tree_path_free (tmppath);
goto free_paths_and_out;
}
gtk_tree_path_free (tmppath);
}
if (!path)
{
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), iter);
gtk_tree_path_up (path);
}
depth = gtk_tree_path_get_depth (path);
if (depth)
{
gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
&parent_iter, path);
parent = G_NODE (parent_iter.user_data);
}
else
parent = G_NODE (tree_store->root);
/* yes, I know that this can be done shorter, but I'm doing it this way
* so the code is also maintainable
*/
if (before && position)
{
b = G_NODE (position->user_data);
if (gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path) - 1] > 0)
{
gtk_tree_path_prev (pos_path);
if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store),
&dst_a, pos_path))
a = G_NODE (dst_a.user_data);
else
a = NULL;
gtk_tree_path_next (pos_path);
}
/* if b is NULL, a is NULL too -- we are at the beginning of the list
* yes and we leak memory here ...
*/
g_return_if_fail (b);
}
else if (before && !position)
{
/* move before without position is appending */
a = NULL;
b = NULL;
}
else /* !before */
{
if (position)
a = G_NODE (position->user_data);
else
a = NULL;
if (position)
{
gtk_tree_path_next (pos_path);
if (gtk_tree_store_get_iter (GTK_TREE_MODEL (tree_store), &dst_b, pos_path))
b = G_NODE (dst_b.user_data);
else
b = NULL;
gtk_tree_path_prev (pos_path);
}
else
{
/* move after without position is prepending */
if (depth)
gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
&parent_iter);
else
gtk_tree_store_iter_children (GTK_TREE_MODEL (tree_store), &dst_b,
NULL);
b = G_NODE (dst_b.user_data);
}
/* if a is NULL, b is NULL too -- we are at the end of the list
* yes and we leak memory here ...
*/
if (position)
g_return_if_fail (a);
}
/* counting nodes */
tmp = parent->children;
length = old_pos = 0;
while (tmp)
{
if (tmp == iter->user_data)
old_pos = length;
tmp = tmp->next;
length++;
}
/* remove node from list */
node = G_NODE (iter->user_data);
tmp_a = node->prev;
tmp_b = node->next;
if (tmp_a)
tmp_a->next = tmp_b;
else
parent->children = tmp_b;
if (tmp_b)
tmp_b->prev = tmp_a;
/* and reinsert the node */
if (a)
{
tmp = a->next;
a->next = node;
node->next = tmp;
node->prev = a;
}
else if (!a && !before)
{
tmp = parent->children;
node->prev = NULL;
parent->children = node;
node->next = tmp;
if (tmp)
tmp->prev = node;
handle_b = FALSE;
}
else if (!a && before)
{
if (!position)
{
node->parent = NULL;
node->next = node->prev = NULL;
/* before with sibling = NULL appends */
g_node_insert_before (parent, NULL, node);
}
else
{
node->parent = NULL;
node->next = node->prev = NULL;
/* after with sibling = NULL prepends */
g_node_insert_after (parent, NULL, node);
}
handle_b = FALSE;
}
if (handle_b)
{
if (b)
{
tmp = b->prev;
b->prev = node;
node->prev = tmp;
node->next = b;
}
else if (!(!a && before)) /* !a && before is completely handled above */
node->next = NULL;
}
/* emit signal */
if (position)
new_pos = gtk_tree_path_get_indices (pos_path)[gtk_tree_path_get_depth (pos_path)-1];
else if (before)
{
if (depth)
new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
&parent_iter) - 1;
else
new_pos = gtk_tree_store_iter_n_children (GTK_TREE_MODEL (tree_store),
NULL) - 1;
}
else
new_pos = 0;
if (new_pos > old_pos)
{
if (before && position)
new_pos--;
}
else
{
if (!before && position)
new_pos++;
}
order = g_new (gint, length);
if (new_pos > old_pos)
{
for (i = 0; i < length; i++)
if (i < old_pos)
order[i] = i;
else if (i >= old_pos && i < new_pos)
order[i] = i + 1;
else if (i == new_pos)
order[i] = old_pos;
else
order[i] = i;
}
else
{
for (i = 0; i < length; i++)
if (i == new_pos)
order[i] = old_pos;
else if (i > new_pos && i <= old_pos)
order[i] = i - 1;
else
order[i] = i;
}
if (depth)
{
tmppath = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store),
&parent_iter);
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
tmppath, &parent_iter, order);
}
else
{
tmppath = gtk_tree_path_new ();
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
tmppath, NULL, order);
}
gtk_tree_path_free (tmppath);
gtk_tree_path_free (path);
if (position)
gtk_tree_path_free (pos_path);
g_free (order);
return;
free_paths_and_out:
gtk_tree_path_free (path);
gtk_tree_path_free (pos_path);
}
/**
* gtk_tree_store_move_before:
* @tree_store: A #GtkTreeStore.
* @iter: A #GtkTreeIter.
* @position: (allow-none): A #GtkTreeIter or %NULL.
*
* Moves @iter in @tree_store to the position before @position. @iter and
* @position should be in the same level. Note that this function only
* works with unsorted stores. If @position is %NULL, @iter will be
* moved to the end of the level.
*
* Since: 2.2
**/
void
gtk_tree_store_move_before (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *position)
{
gtk_tree_store_move (tree_store, iter, position, TRUE);
}
/**
* gtk_tree_store_move_after:
* @tree_store: A #GtkTreeStore.
* @iter: A #GtkTreeIter.
* @position: (allow-none): A #GtkTreeIter.
*
* Moves @iter in @tree_store to the position after @position. @iter and
* @position should be in the same level. Note that this function only
* works with unsorted stores. If @position is %NULL, @iter will be moved
* to the start of the level.
*
* Since: 2.2
**/
void
gtk_tree_store_move_after (GtkTreeStore *tree_store,
GtkTreeIter *iter,
GtkTreeIter *position)
{
gtk_tree_store_move (tree_store, iter, position, FALSE);
}
/* Sorting */
static gint
gtk_tree_store_compare_func (gconstpointer a,
gconstpointer b,
gpointer user_data)
{
GtkTreeStore *tree_store = user_data;
GNode *node_a;
GNode *node_b;
GtkTreeIterCompareFunc func;
gpointer data;
GtkTreeIter iter_a;
GtkTreeIter iter_b;
gint retval;
if (tree_store->sort_column_id != -1)
{
GtkTreeDataSortHeader *header;
header = _gtk_tree_data_list_get_header (tree_store->sort_list,
tree_store->sort_column_id);
g_return_val_if_fail (header != NULL, 0);
g_return_val_if_fail (header->func != NULL, 0);
func = header->func;
data = header->data;
}
else
{
g_return_val_if_fail (tree_store->default_sort_func != NULL, 0);
func = tree_store->default_sort_func;
data = tree_store->default_sort_data;
}
node_a = ((SortTuple *) a)->node;
node_b = ((SortTuple *) b)->node;
iter_a.stamp = tree_store->stamp;
iter_a.user_data = node_a;
iter_b.stamp = tree_store->stamp;
iter_b.user_data = node_b;
retval = (* func) (GTK_TREE_MODEL (user_data), &iter_a, &iter_b, data);
if (tree_store->order == GTK_SORT_DESCENDING)
{
if (retval > 0)
retval = -1;
else if (retval < 0)
retval = 1;
}
return retval;
}
static void
gtk_tree_store_sort_helper (GtkTreeStore *tree_store,
GNode *parent,
gboolean recurse)
{
GtkTreeIter iter;
GArray *sort_array;
GNode *node;
GNode *tmp_node;
gint list_length;
gint i;
gint *new_order;
GtkTreePath *path;
node = parent->children;
if (node == NULL || node->next == NULL)
{
if (recurse && node && node->children)
gtk_tree_store_sort_helper (tree_store, node, TRUE);
return;
}
list_length = 0;
for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
list_length++;
sort_array = g_array_sized_new (FALSE, FALSE, sizeof (SortTuple), list_length);
i = 0;
for (tmp_node = node; tmp_node; tmp_node = tmp_node->next)
{
SortTuple tuple;
tuple.offset = i;
tuple.node = tmp_node;
g_array_append_val (sort_array, tuple);
i++;
}
/* Sort the array */
g_array_sort_with_data (sort_array, gtk_tree_store_compare_func, tree_store);
for (i = 0; i < list_length - 1; i++)
{
g_array_index (sort_array, SortTuple, i).node->next =
g_array_index (sort_array, SortTuple, i + 1).node;
g_array_index (sort_array, SortTuple, i + 1).node->prev =
g_array_index (sort_array, SortTuple, i).node;
}
g_array_index (sort_array, SortTuple, list_length - 1).node->next = NULL;
g_array_index (sort_array, SortTuple, 0).node->prev = NULL;
parent->children = g_array_index (sort_array, SortTuple, 0).node;
/* Let the world know about our new order */
new_order = g_new (gint, list_length);
for (i = 0; i < list_length; i++)
new_order[i] = g_array_index (sort_array, SortTuple, i).offset;
iter.stamp = tree_store->stamp;
iter.user_data = parent;
path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &iter);
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
path, &iter, new_order);
gtk_tree_path_free (path);
g_free (new_order);
g_array_free (sort_array, TRUE);
if (recurse)
{
for (tmp_node = parent->children; tmp_node; tmp_node = tmp_node->next)
{
if (tmp_node->children)
gtk_tree_store_sort_helper (tree_store, tmp_node, TRUE);
}
}
}
static void
gtk_tree_store_sort (GtkTreeStore *tree_store)
{
if (!GTK_TREE_STORE_IS_SORTED (tree_store))
return;
if (tree_store->sort_column_id != -1)
{
GtkTreeDataSortHeader *header = NULL;
header = _gtk_tree_data_list_get_header (tree_store->sort_list,
tree_store->sort_column_id);
/* We want to make sure that we have a function */
g_return_if_fail (header != NULL);
g_return_if_fail (header->func != NULL);
}
else
{
g_return_if_fail (tree_store->default_sort_func != NULL);
}
gtk_tree_store_sort_helper (tree_store, G_NODE (tree_store->root), TRUE);
}
static void
gtk_tree_store_sort_iter_changed (GtkTreeStore *tree_store,
GtkTreeIter *iter,
gint column,
gboolean emit_signal)
{
GNode *prev = NULL;
GNode *next = NULL;
GNode *node;
GtkTreePath *tmp_path;
GtkTreeIter tmp_iter;
gint cmp_a = 0;
gint cmp_b = 0;
gint i;
gint old_location;
gint new_location;
gint *new_order;
gint length;
GtkTreeIterCompareFunc func;
gpointer data;
g_return_if_fail (G_NODE (iter->user_data)->parent != NULL);
tmp_iter.stamp = tree_store->stamp;
if (tree_store->sort_column_id != -1)
{
GtkTreeDataSortHeader *header;
header = _gtk_tree_data_list_get_header (tree_store->sort_list,
tree_store->sort_column_id);
g_return_if_fail (header != NULL);
g_return_if_fail (header->func != NULL);
func = header->func;
data = header->data;
}
else
{
g_return_if_fail (tree_store->default_sort_func != NULL);
func = tree_store->default_sort_func;
data = tree_store->default_sort_data;
}
/* If it's the built in function, we don't sort. */
if (func == _gtk_tree_data_list_compare_func &&
tree_store->sort_column_id != column)
return;
old_location = 0;
node = G_NODE (iter->user_data)->parent->children;
/* First we find the iter, its prev, and its next */
while (node)
{
if (node == G_NODE (iter->user_data))
break;
old_location++;
node = node->next;
}
g_assert (node != NULL);
prev = node->prev;
next = node->next;
/* Check the common case, where we don't need to sort it moved. */
if (prev != NULL)
{
tmp_iter.user_data = prev;
cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
}
if (next != NULL)
{
tmp_iter.user_data = next;
cmp_b = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
}
if (tree_store->order == GTK_SORT_DESCENDING)
{
if (cmp_a < 0)
cmp_a = 1;
else if (cmp_a > 0)
cmp_a = -1;
if (cmp_b < 0)
cmp_b = 1;
else if (cmp_b > 0)
cmp_b = -1;
}
if (prev == NULL && cmp_b <= 0)
return;
else if (next == NULL && cmp_a <= 0)
return;
else if (prev != NULL && next != NULL &&
cmp_a <= 0 && cmp_b <= 0)
return;
/* We actually need to sort it */
/* First, remove the old link. */
if (prev)
prev->next = next;
else
node->parent->children = next;
if (next)
next->prev = prev;
node->prev = NULL;
node->next = NULL;
/* FIXME: as an optimization, we can potentially start at next */
prev = NULL;
node = node->parent->children;
new_location = 0;
tmp_iter.user_data = node;
if (tree_store->order == GTK_SORT_DESCENDING)
cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
else
cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
while ((node->next) && (cmp_a > 0))
{
prev = node;
node = node->next;
new_location++;
tmp_iter.user_data = node;
if (tree_store->order == GTK_SORT_DESCENDING)
cmp_a = (* func) (GTK_TREE_MODEL (tree_store), &tmp_iter, iter, data);
else
cmp_a = (* func) (GTK_TREE_MODEL (tree_store), iter, &tmp_iter, data);
}
if ((!node->next) && (cmp_a > 0))
{
new_location++;
node->next = G_NODE (iter->user_data);
node->next->prev = node;
}
else if (prev)
{
prev->next = G_NODE (iter->user_data);
prev->next->prev = prev;
G_NODE (iter->user_data)->next = node;
G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
}
else
{
G_NODE (iter->user_data)->next = G_NODE (iter->user_data)->parent->children;
G_NODE (iter->user_data)->next->prev = G_NODE (iter->user_data);
G_NODE (iter->user_data)->parent->children = G_NODE (iter->user_data);
}
if (!emit_signal)
return;
/* Emit the reordered signal. */
length = g_node_n_children (node->parent);
new_order = g_new (int, length);
if (old_location < new_location)
for (i = 0; i < length; i++)
{
if (i < old_location ||
i > new_location)
new_order[i] = i;
else if (i >= old_location &&
i < new_location)
new_order[i] = i + 1;
else if (i == new_location)
new_order[i] = old_location;
}
else
for (i = 0; i < length; i++)
{
if (i < new_location ||
i > old_location)
new_order[i] = i;
else if (i > new_location &&
i <= old_location)
new_order[i] = i - 1;
else if (i == new_location)
new_order[i] = old_location;
}
tmp_iter.user_data = node->parent;
tmp_path = gtk_tree_store_get_path (GTK_TREE_MODEL (tree_store), &tmp_iter);
gtk_tree_model_rows_reordered (GTK_TREE_MODEL (tree_store),
tmp_path, &tmp_iter,
new_order);
gtk_tree_path_free (tmp_path);
g_free (new_order);
}
static gboolean
gtk_tree_store_get_sort_column_id (GtkTreeSortable *sortable,
gint *sort_column_id,
GtkSortType *order)
{
GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
if (sort_column_id)
* sort_column_id = tree_store->sort_column_id;
if (order)
* order = tree_store->order;
if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID ||
tree_store->sort_column_id == GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
return FALSE;
return TRUE;
}
static void
gtk_tree_store_set_sort_column_id (GtkTreeSortable *sortable,
gint sort_column_id,
GtkSortType order)
{
GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
if ((tree_store->sort_column_id == sort_column_id) &&
(tree_store->order == order))
return;
if (sort_column_id != GTK_TREE_SORTABLE_UNSORTED_SORT_COLUMN_ID)
{
if (sort_column_id != GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
{
GtkTreeDataSortHeader *header = NULL;
header = _gtk_tree_data_list_get_header (tree_store->sort_list,
sort_column_id);
/* We want to make sure that we have a function */
g_return_if_fail (header != NULL);
g_return_if_fail (header->func != NULL);
}
else
{
g_return_if_fail (tree_store->default_sort_func != NULL);
}
}
tree_store->sort_column_id = sort_column_id;
tree_store->order = order;
gtk_tree_sortable_sort_column_changed (sortable);
gtk_tree_store_sort (tree_store);
}
static void
gtk_tree_store_set_sort_func (GtkTreeSortable *sortable,
gint sort_column_id,
GtkTreeIterCompareFunc func,
gpointer data,
GDestroyNotify destroy)
{
GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
tree_store->sort_list = _gtk_tree_data_list_set_header (tree_store->sort_list,
sort_column_id,
func, data, destroy);
if (tree_store->sort_column_id == sort_column_id)
gtk_tree_store_sort (tree_store);
}
static void
gtk_tree_store_set_default_sort_func (GtkTreeSortable *sortable,
GtkTreeIterCompareFunc func,
gpointer data,
GDestroyNotify destroy)
{
GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
if (tree_store->default_sort_destroy)
{
GDestroyNotify d = tree_store->default_sort_destroy;
tree_store->default_sort_destroy = NULL;
d (tree_store->default_sort_data);
}
tree_store->default_sort_func = func;
tree_store->default_sort_data = data;
tree_store->default_sort_destroy = destroy;
if (tree_store->sort_column_id == GTK_TREE_SORTABLE_DEFAULT_SORT_COLUMN_ID)
gtk_tree_store_sort (tree_store);
}
static gboolean
gtk_tree_store_has_default_sort_func (GtkTreeSortable *sortable)
{
GtkTreeStore *tree_store = (GtkTreeStore *) sortable;
return (tree_store->default_sort_func != NULL);
}
static void
validate_gnode (GNode* node)
{
GNode *iter;
iter = node->children;
while (iter != NULL)
{
g_assert (iter->parent == node);
if (iter->prev)
g_assert (iter->prev->next == iter);
validate_gnode (iter);
iter = iter->next;
}
}
/* GtkBuildable custom tag implementation
*
* <columns>
* <column type="..."/>
* <column type="..."/>
* </columns>
*/
typedef struct {
GtkBuilder *builder;
GObject *object;
GSList *items;
} GSListSubParserData;
static void
tree_model_start_element (GMarkupParseContext *context,
const gchar *element_name,
const gchar **names,
const gchar **values,
gpointer user_data,
GError **error)
{
guint i;
GSListSubParserData *data = (GSListSubParserData*)user_data;
for (i = 0; names[i]; i++)
{
if (strcmp (names[i], "type") == 0)
data->items = g_slist_prepend (data->items, g_strdup (values[i]));
}
}
static void
tree_model_end_element (GMarkupParseContext *context,
const gchar *element_name,
gpointer user_data,
GError **error)
{
GSListSubParserData *data = (GSListSubParserData*)user_data;
g_assert(data->builder);
if (strcmp (element_name, "columns") == 0)
{
GSList *l;
GType *types;
int i;
GType type;
data = (GSListSubParserData*)user_data;
data->items = g_slist_reverse (data->items);
types = g_new0 (GType, g_slist_length (data->items));
for (l = data->items, i = 0; l; l = l->next, i++)
{
type = gtk_builder_get_type_from_name (data->builder, l->data);
if (type == G_TYPE_INVALID)
{
g_warning ("Unknown type %s specified in treemodel %s",
(const gchar*)l->data,
gtk_buildable_get_name (GTK_BUILDABLE (data->object)));
continue;
}
types[i] = type;
g_free (l->data);
}
gtk_tree_store_set_column_types (GTK_TREE_STORE (data->object), i, types);
g_free (types);
}
}
static const GMarkupParser tree_model_parser =
{
tree_model_start_element,
tree_model_end_element
};
static gboolean
gtk_tree_store_buildable_custom_tag_start (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
GMarkupParser *parser,
gpointer *data)
{
GSListSubParserData *parser_data;
if (child)
return FALSE;
if (strcmp (tagname, "columns") == 0)
{
parser_data = g_slice_new0 (GSListSubParserData);
parser_data->builder = builder;
parser_data->items = NULL;
parser_data->object = G_OBJECT (buildable);
*parser = tree_model_parser;
*data = parser_data;
return TRUE;
}
return FALSE;
}
static void
gtk_tree_store_buildable_custom_finished (GtkBuildable *buildable,
GtkBuilder *builder,
GObject *child,
const gchar *tagname,
gpointer user_data)
{
GSListSubParserData *data;
if (strcmp (tagname, "columns"))
return;
data = (GSListSubParserData*)user_data;
g_slist_free (data->items);
g_slice_free (GSListSubParserData, data);
}
#define __GTK_TREE_STORE_C__
#include "gtkaliasdef.c"
|