1. windwiny
  2. wxPython

Source

wxPython / contrib / gizmos / wxCode / src / treelistctrl.cpp

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

// ===========================================================================
// declarations
// ===========================================================================

// ---------------------------------------------------------------------------
// headers
// ---------------------------------------------------------------------------

// For compilers that support precompilation, includes "wx.h".
#include "wx/wxprec.h"

#ifdef __BORLANDC__
    #pragma hdrstop
#endif


#include <wx/treebase.h>
#include <wx/timer.h>
#include <wx/textctrl.h>
#include <wx/imaglist.h>
#include <wx/settings.h>
#include <wx/dcclient.h>
#include <wx/dcscreen.h>
#include <wx/dcmemory.h>
#include <wx/scrolwin.h>
#if wxCHECK_VERSION(2, 7, 0)
#include <wx/renderer.h>
#endif

#ifdef __WXMAC__
#include "wx/osx/private.h"
#endif

#include "wx/treelistctrl.h"


// ---------------------------------------------------------------------------
// array types
// ---------------------------------------------------------------------------

class  wxTreeListItem;

#if !wxCHECK_VERSION(2, 5, 0)
WX_DEFINE_ARRAY(wxTreeListItem *, wxArrayTreeListItems);
#else
WX_DEFINE_ARRAY_PTR(wxTreeListItem *, wxArrayTreeListItems);
#endif

#include <wx/dynarray.h>
WX_DECLARE_OBJARRAY(wxTreeListColumnInfo, wxArrayTreeListColumnInfo);
#include <wx/arrimpl.cpp>
WX_DEFINE_OBJARRAY(wxArrayTreeListColumnInfo);


// --------------------------------------------------------------------------
// constants
// --------------------------------------------------------------------------

static const int NO_IMAGE = -1;

static const int LINEHEIGHT = 10;
static const int LINEATROOT = 5;
static const int MARGIN = 2;
static const int MININDENT = 16;
static const int BTNWIDTH = 9;
static const int BTNHEIGHT = 9;
static const int EXTRA_WIDTH = 4;
static const int EXTRA_HEIGHT = 4;
static const int HEADER_OFFSET_X = 1;
static const int HEADER_OFFSET_Y = 1;

static const int DRAG_TIMER_TICKS = 250; // minimum drag wait time in ms
static const int FIND_TIMER_TICKS = 500; // minimum find wait time in ms
static const int RENAME_TIMER_TICKS = 250; // minimum rename wait time in ms

const wxChar* wxTreeListCtrlNameStr = _T("treelistctrl");

static wxTreeListColumnInfo wxInvalidTreeListColumnInfo;


// ---------------------------------------------------------------------------
// private classes
// ---------------------------------------------------------------------------
//-----------------------------------------------------------------------------
//  wxTreeListHeaderWindow (internal)
//-----------------------------------------------------------------------------

class  wxTreeListHeaderWindow : public wxWindow
{
protected:
    wxTreeListMainWindow *m_owner;
    const wxCursor *m_currentCursor;
    const wxCursor *m_resizeCursor;
    bool m_isDragging;

    // column being resized
    int m_column;

    // divider line position in logical (unscrolled) coords
    int m_currentX;

    // minimal position beyond which the divider line can't be dragged in
    // logical coords
    int m_minX;

    wxArrayTreeListColumnInfo m_columns;

    // total width of the columns
    int m_total_col_width;

#if wxCHECK_VERSION_FULL(2, 7, 0, 1)
    // which col header is currently highlighted with mouse-over
    int m_hotTrackCol;

    int XToCol(int x);
    void RefreshColLabel(int col);
#endif
    
public:
    wxTreeListHeaderWindow();

    wxTreeListHeaderWindow( wxWindow *win,
                            wxWindowID id,
                            wxTreeListMainWindow *owner,
                            const wxPoint &pos = wxDefaultPosition,
                            const wxSize &size = wxDefaultSize,
                            long style = 0,
                            const wxString &name = _T("wxtreelistctrlcolumntitles") );

    virtual ~wxTreeListHeaderWindow();

    void DoDrawRect( wxDC *dc, int x, int y, int w, int h );
    void DrawCurrent();
    void AdjustDC(wxDC& dc);

    void OnPaint( wxPaintEvent &event );
    void OnMouse( wxMouseEvent &event );
    void OnSetFocus( wxFocusEvent &event );

    // total width of all columns
    int GetWidth() const { return m_total_col_width; }

    // column manipulation
    int GetColumnCount() const { return m_columns.GetCount(); }

    void AddColumn (const wxTreeListColumnInfo& colInfo);

    void InsertColumn (int before, const wxTreeListColumnInfo& colInfo);

    void RemoveColumn (int column);

    // column information manipulation
    const wxTreeListColumnInfo& GetColumn (int column) const{
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     wxInvalidTreeListColumnInfo, _T("Invalid column"));
        return m_columns[column];
    }
    wxTreeListColumnInfo& GetColumn (int column) {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     wxInvalidTreeListColumnInfo, _T("Invalid column"));
        return m_columns[column];
    }
    void SetColumn (int column, const wxTreeListColumnInfo& info);

    wxString GetColumnText (int column) const {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     wxEmptyString, _T("Invalid column"));
        return m_columns[column].GetText();
    }
    void SetColumnText (int column, const wxString& text) {
        wxCHECK_RET ((column >= 0) && (column < GetColumnCount()),
                     _T("Invalid column"));
        m_columns[column].SetText (text);
    }

    int GetColumnAlignment (int column) const {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     wxALIGN_LEFT, _T("Invalid column"));
        return m_columns[column].GetAlignment();
    }
    void SetColumnAlignment (int column, int flag) {
        wxCHECK_RET ((column >= 0) && (column < GetColumnCount()),
                     _T("Invalid column"));
        m_columns[column].SetAlignment (flag);
    }

    int GetColumnWidth (int column) const {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     -1, _T("Invalid column"));
        return m_columns[column].GetWidth();
    }
    void SetColumnWidth (int column, int width);

    bool IsColumnEditable (int column) const {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     false, _T("Invalid column"));
        return m_columns[column].IsEditable();
    }

    bool IsColumnShown (int column) const {
        wxCHECK_MSG ((column >= 0) && (column < GetColumnCount()),
                     true, _T("Invalid column"));
        return m_columns[column].IsShown();
    }

    // needs refresh
    bool m_dirty;

private:
    // common part of all ctors
    void Init();

    void SendListEvent(wxEventType type, wxPoint pos);

    DECLARE_DYNAMIC_CLASS(wxTreeListHeaderWindow)
    DECLARE_EVENT_TABLE()
};


// this is the "true" control
class  wxTreeListMainWindow: public wxScrolledWindow
{
public:
    // creation
    // --------
    wxTreeListMainWindow() { Init(); }

    wxTreeListMainWindow (wxTreeListCtrl *parent, wxWindowID id = -1,
               const wxPoint& pos = wxDefaultPosition,
               const wxSize& size = wxDefaultSize,
               long style = wxTR_DEFAULT_STYLE,
               const wxValidator &validator = wxDefaultValidator,
               const wxString& name = _T("wxtreelistmainwindow"))
    {
        Init();
        Create (parent, id, pos, size, style, validator, name);
    }

    virtual ~wxTreeListMainWindow();

    bool Create(wxTreeListCtrl *parent, wxWindowID id = -1,
                const wxPoint& pos = wxDefaultPosition,
                const wxSize& size = wxDefaultSize,
                long style = wxTR_DEFAULT_STYLE,
                const wxValidator &validator = wxDefaultValidator,
                const wxString& name = _T("wxtreelistctrl"));

    // accessors
    // ---------

    // return true if this is a virtual list control
    bool IsVirtual() const { return HasFlag(wxTR_VIRTUAL); }

    // get the total number of items in the control
    size_t GetCount() const;

    // indent is the number of pixels the children are indented relative to
    // the parents position. SetIndent() also redraws the control
    // immediately.
    unsigned int GetIndent() const { return m_indent; }
    void SetIndent(unsigned int indent);

    // see wxTreeListCtrl for the meaning
    unsigned int GetLineSpacing() const { return m_linespacing; }
    void SetLineSpacing(unsigned int spacing);

    // image list: these functions allow to associate an image list with
    // the control and retrieve it. Note that when assigned with
    // SetImageList, the control does _not_ delete
    // the associated image list when it's deleted in order to allow image
    // lists to be shared between different controls. If you use
    // AssignImageList, the control _does_ delete the image list.

    // The normal image list is for the icons which correspond to the
    // normal tree item state (whether it is selected or not).
    // Additionally, the application might choose to show a state icon
    // which corresponds to an app-defined item state (for example,
    // checked/unchecked) which are taken from the state image list.
    wxImageList *GetImageList() const { return m_imageListNormal; }
    wxImageList *GetStateImageList() const { return m_imageListState; }
    wxImageList *GetButtonsImageList() const { return m_imageListButtons; }

    void SetImageList(wxImageList *imageList);
    void SetStateImageList(wxImageList *imageList);
    void SetButtonsImageList(wxImageList *imageList);
    void AssignImageList(wxImageList *imageList);
    void AssignStateImageList(wxImageList *imageList);
    void AssignButtonsImageList(wxImageList *imageList);

    // Functions to work with tree ctrl items.

    // accessors
    // ---------

    // retrieve item's label
    wxString GetItemText (const wxTreeItemId& item) const
    { return GetItemText (item, GetMainColumn()); }
    wxString GetItemText (const wxTreeItemId& item, int column) const;
    wxString GetItemText (wxTreeItemData* item, int column) const;

    // get one of the images associated with the item (normal by default)
    int GetItemImage (const wxTreeItemId& item,
                      wxTreeItemIcon which = wxTreeItemIcon_Normal) const
    { return GetItemImage (item, GetMainColumn(), which); }
    int GetItemImage (const wxTreeItemId& item, int column,
                      wxTreeItemIcon which = wxTreeItemIcon_Normal) const;

    // get the data associated with the item
    wxTreeItemData *GetItemData(const wxTreeItemId& item) const;

    bool GetItemBold(const wxTreeItemId& item) const;
    wxColour GetItemTextColour(const wxTreeItemId& item) const;
    wxColour GetItemBackgroundColour(const wxTreeItemId& item) const;
    wxFont GetItemFont(const wxTreeItemId& item) const;

    // modifiers
    // ---------

    // set item's label
    void SetItemText (const wxTreeItemId& item, const wxString& text)
    { SetItemText (item, GetMainColumn(), text); }
    void SetItemText (const wxTreeItemId& item, int column, const wxString& text);

    // get one of the images associated with the item (normal by default)
    void SetItemImage (const wxTreeItemId& item, int image,
                       wxTreeItemIcon which = wxTreeItemIcon_Normal)
    { SetItemImage (item, GetMainColumn(), image, which); }
    void SetItemImage (const wxTreeItemId& item, int column, int image,
                       wxTreeItemIcon which = wxTreeItemIcon_Normal);

    // associate some data with the item
    void SetItemData(const wxTreeItemId& item, wxTreeItemData *data);

    // force appearance of [+] button near the item. This is useful to
    // allow the user to expand the items which don't have any children now
    // - but instead add them only when needed, thus minimizing memory
    // usage and loading time.
    void SetItemHasChildren(const wxTreeItemId& item, bool has = true);

    // the item will be shown in bold
    void SetItemBold(const wxTreeItemId& item, bool bold = true);

    // set the item's text colour
    void SetItemTextColour(const wxTreeItemId& item, const wxColour& colour);

    // set the item's background colour
    void SetItemBackgroundColour(const wxTreeItemId& item, const wxColour& colour);

    // set the item's font (should be of the same height for all items)
    void SetItemFont(const wxTreeItemId& item, const wxFont& font);

    // set the window font
    virtual bool SetFont( const wxFont &font );

    // set the styles.  No need to specify a GetWindowStyle here since
    // the base wxWindow member function will do it for us
    void SetWindowStyle(const long styles);

    // item status inquiries
    // ---------------------

    // is the item visible (it might be outside the view or not expanded)?
    bool IsVisible(const wxTreeItemId& item, bool fullRow) const;
    // does the item has any children?
    bool HasChildren(const wxTreeItemId& item) const;
    // is the item expanded (only makes sense if HasChildren())?
    bool IsExpanded(const wxTreeItemId& item) const;
    // is this item currently selected (the same as has focus)?
    bool IsSelected(const wxTreeItemId& item) const;
    // is item text in bold font?
    bool IsBold(const wxTreeItemId& item) const;
        // does the layout include space for a button?

    // number of children
    // ------------------

    // if 'recursively' is false, only immediate children count, otherwise
    // the returned number is the number of all items in this branch
    size_t GetChildrenCount(const wxTreeItemId& item, bool recursively = true);

    // navigation
    // ----------

    // wxTreeItemId.IsOk() will return false if there is no such item

    // get the root tree item
    wxTreeItemId GetRootItem() const { return m_rootItem; }

    // get the item currently selected, only if a single item is selected
    wxTreeItemId GetSelection() const { return m_selectItem; }

    // get all the items currently selected, return count of items
    size_t GetSelections(wxArrayTreeItemIds&) const;

    // get the parent of this item (may return NULL if root)
    wxTreeItemId GetItemParent(const wxTreeItemId& item) const;

    // for this enumeration function you must pass in a "cookie" parameter
    // which is opaque for the application but is necessary for the library
    // to make these functions reentrant (i.e. allow more than one
    // enumeration on one and the same object simultaneously). Of course,
    // the "cookie" passed to GetFirstChild() and GetNextChild() should be
    // the same!

    // get child of this item
#if !wxCHECK_VERSION(2, 5, 0)
    wxTreeItemId GetFirstChild(const wxTreeItemId& item, long& cookie) const;
    wxTreeItemId GetNextChild(const wxTreeItemId& item, long& cookie) const;
    wxTreeItemId GetPrevChild(const wxTreeItemId& item, long& cookie) const;
    wxTreeItemId GetLastChild(const wxTreeItemId& item, long& cookie) const;
#else
    wxTreeItemId GetFirstChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
    wxTreeItemId GetNextChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
    wxTreeItemId GetPrevChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
    wxTreeItemId GetLastChild(const wxTreeItemId& item, wxTreeItemIdValue& cookie) const;
#endif

    // get sibling of this item
    wxTreeItemId GetNextSibling(const wxTreeItemId& item) const;
    wxTreeItemId GetPrevSibling(const wxTreeItemId& item) const;

    // get item in the full tree (currently only for internal use)
    wxTreeItemId GetNext(const wxTreeItemId& item, bool fulltree = true) const;
    wxTreeItemId GetPrev(const wxTreeItemId& item, bool fulltree = true) const;

    // get expanded item, see IsExpanded()
    wxTreeItemId GetFirstExpandedItem() const;
    wxTreeItemId GetNextExpanded(const wxTreeItemId& item) const;
    wxTreeItemId GetPrevExpanded(const wxTreeItemId& item) const;

    // get visible item, see IsVisible()
    wxTreeItemId GetFirstVisibleItem(bool fullRow) const;
    wxTreeItemId GetNextVisible(const wxTreeItemId& item, bool fullRow) const;
    wxTreeItemId GetPrevVisible(const wxTreeItemId& item, bool fullRow) const;

    // operations
    // ----------

    // add the root node to the tree
    wxTreeItemId AddRoot (const wxString& text,
                          int image = -1, int selectedImage = -1,
                          wxTreeItemData *data = NULL);

    // insert a new item in as the first child of the parent
    wxTreeItemId PrependItem(const wxTreeItemId& parent,
                             const wxString& text,
                             int image = -1, int selectedImage = -1,
                             wxTreeItemData *data = NULL);

    // insert a new item after a given one
    wxTreeItemId InsertItem(const wxTreeItemId& parent,
                            const wxTreeItemId& idPrevious,
                            const wxString& text,
                            int image = -1, int selectedImage = -1,
                            wxTreeItemData *data = NULL);

    // insert a new item before the one with the given index
    wxTreeItemId InsertItem(const wxTreeItemId& parent,
                            size_t index,
                            const wxString& text,
                            int image = -1, int selectedImage = -1,
                            wxTreeItemData *data = NULL);

    // insert a new item in as the last child of the parent
    wxTreeItemId AppendItem(const wxTreeItemId& parent,
                            const wxString& text,
                            int image = -1, int selectedImage = -1,
                            wxTreeItemData *data = NULL);

    // delete this item and associated data if any
    void Delete(const wxTreeItemId& item);
    // delete all children (but don't delete the item itself)
    // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
    void DeleteChildren(const wxTreeItemId& item);
    // delete the root and all its children from the tree
    // NB: this won't send wxEVT_COMMAND_TREE_ITEM_DELETED events
    void DeleteRoot();

    // expand this item
    void Expand(const wxTreeItemId& item);
    // expand this item and all subitems recursively
    void ExpandAll(const wxTreeItemId& item);
    // collapse the item without removing its children
    void Collapse(const wxTreeItemId& item);
    // collapse the item and remove all children
    void CollapseAndReset(const wxTreeItemId& item);
    // toggles the current state
    void Toggle(const wxTreeItemId& item);

    // remove the selection from currently selected item (if any)
    void Unselect();
    void UnselectAll();
    // select this item
    void SelectItem(const wxTreeItemId& item, const wxTreeItemId& prev = (wxTreeItemId*)NULL,
                    bool unselect_others = true);
    void SelectAll();
    // make sure this item is visible (expanding the parent item and/or
    // scrolling to this item if necessary)
    void EnsureVisible(const wxTreeItemId& item);
    // scroll to this item (but don't expand its parent)
    void ScrollTo(const wxTreeItemId& item);
    void AdjustMyScrollbars();

    // The first function is more portable (because easier to implement
    // on other platforms), but the second one returns some extra info.
    wxTreeItemId HitTest (const wxPoint& point)
        { int flags; int column; return HitTest (point, flags, column); }
    wxTreeItemId HitTest (const wxPoint& point, int& flags)
        { int column; return HitTest (point, flags, column); }
    wxTreeItemId HitTest (const wxPoint& point, int& flags, int& column);


    // get the bounding rectangle of the item (or of its label only)
    bool GetBoundingRect(const wxTreeItemId& item,
                         wxRect& rect,
                         bool textOnly = false) const;

    // Start editing the item label: this (temporarily) replaces the item
    // with a one line edit control. The item will be selected if it hadn't
    // been before.
    void EditLabel (const wxTreeItemId& item, int column);

    // sorting
    // this function is called to compare 2 items and should return -1, 0
    // or +1 if the first item is less than, equal to or greater than the
    // second one. The base class version performs alphabetic comparaison
    // of item labels (GetText)
    virtual int OnCompareItems(const wxTreeItemId& item1,
                               const wxTreeItemId& item2);
    // sort the children of this item using OnCompareItems
    //
    // NB: this function is not reentrant and not MT-safe (FIXME)!
    void SortChildren(const wxTreeItemId& item);

    // searching
    wxTreeItemId FindItem (const wxTreeItemId& item, const wxString& str, int mode = 0);

    // implementation only from now on

    // overridden base class virtuals
    virtual bool SetBackgroundColour(const wxColour& colour);
    virtual bool SetForegroundColour(const wxColour& colour);

    // drop over item
    void SetDragItem (const wxTreeItemId& item = (wxTreeItemId*)NULL);

    // callbacks
    void OnPaint( wxPaintEvent &event );
    void OnSetFocus( wxFocusEvent &event );
    void OnKillFocus( wxFocusEvent &event );
    void OnChar( wxKeyEvent &event );
    void OnMouse( wxMouseEvent &event );
    void OnIdle( wxIdleEvent &event );
    void OnScroll(wxScrollWinEvent& event);

    // implementation helpers
    void SendDeleteEvent(wxTreeListItem *itemBeingDeleted);

    int GetColumnCount() const
    { return m_owner->GetHeaderWindow()->GetColumnCount(); }

    void SetMainColumn (int column)
    { if ((column >= 0) && (column < GetColumnCount())) m_main_column = column; }

    int GetMainColumn() const { return m_main_column; }

    int GetBestColumnWidth (int column, wxTreeItemId parent = wxTreeItemId());
    int GetItemWidth (int column, wxTreeListItem *item);
    wxFont GetItemFont (wxTreeListItem *item);

    void SetFocus();

    wxTreeItemId GetCurrentItem() const  { return m_curItem; }
    void SetCurrentItem(const wxTreeItemId& newItem)
    {
        wxTreeListItem *oldItem = m_curItem;
        m_curItem = (wxTreeListItem*)newItem.m_pItem; 
        if (oldItem) RefreshLine(oldItem);
    }

protected:
    wxTreeListCtrl* m_owner;

    int m_main_column;

    friend class wxTreeListItem;
    friend class wxTreeListRenameTimer;
    friend class wxEditTextCtrl;

    wxFont               m_normalFont;
    wxFont               m_boldFont;

    wxTreeListItem       *m_rootItem; // root item
    wxTreeListItem       *m_curItem; // current item, either selected or marked
    wxTreeListItem       *m_drag_item; // Item being dragged currently
    wxTreeListItem       *m_shiftItem; // item, where the shift key was pressed
    wxTreeListItem       *m_editItem; // item, which is currently edited
    wxTreeListItem       *m_selectItem; // current selected item, not with wxTR_MULTIPLE
    wxTreeListItem       *m_select_me;
    
    int                  m_curColumn;

    int                  m_btnWidth, m_btnWidth2;
    int                  m_btnHeight, m_btnHeight2;
    int                  m_imgWidth, m_imgWidth2;
    int                  m_imgHeight, m_imgHeight2;
    unsigned short       m_indent;
    int                  m_lineHeight;
    unsigned short       m_linespacing;
    wxPen                m_dottedPen;
    wxBrush             *m_hilightBrush,
                        *m_hilightUnfocusedBrush;
    bool                 m_hasFocus;
    bool                 m_should_return;
public:
    bool                 m_dirty;
protected:
    bool                 m_ownsImageListNormal,
                         m_ownsImageListState,
                         m_ownsImageListButtons;
    bool                 m_isDragging; // true between BEGIN/END drag events
    bool                 m_renameAccept;
    bool                 m_lastOnSame;  // last click on the same item as prev
    bool                 m_left_down_selection;

    wxImageList         *m_imageListNormal,
                        *m_imageListState,
                        *m_imageListButtons;

    int                  m_dragCount;
    wxTimer             *m_dragTimer;
    wxTreeListItem      *m_dragItem;

    wxTimer             *m_renameTimer;
    wxString             m_renameRes;

    // char navigation
    wxTimer             *m_findTimer;
    wxString             m_findStr;

    // the common part of all ctors
    void Init();

    // misc helpers
    wxTreeItemId DoInsertItem(const wxTreeItemId& parent,
                              size_t previous,
                              const wxString& text,
                              int image, int selectedImage,
                              wxTreeItemData *data);
    bool HasButtons(void) const
        { return (m_imageListButtons) || HasFlag (wxTR_TWIST_BUTTONS|wxTR_HAS_BUTTONS); }

protected:
    void CalculateLineHeight();
    int  GetLineHeight(wxTreeListItem *item) const;
    void PaintLevel( wxTreeListItem *item, wxDC& dc, int level, int &y,
                     int x_maincol);
    void PaintItem( wxTreeListItem *item, wxDC& dc);

    void CalculateLevel( wxTreeListItem *item, wxDC &dc, int level, int &y,
                         int x_maincol);
    void CalculatePositions();
    void CalculateSize( wxTreeListItem *item, wxDC &dc );

    void RefreshSubtree (wxTreeListItem *item);
    void RefreshLine (wxTreeListItem *item);

    // redraw all selected items
    void RefreshSelected();

    // RefreshSelected() recursive helper
    void RefreshSelectedUnder (wxTreeListItem *item);

    void OnRenameTimer();
    void OnRenameAccept();

    void FillArray(wxTreeListItem*, wxArrayTreeItemIds&) const;
    bool TagAllChildrenUntilLast (wxTreeListItem *crt_item, wxTreeListItem *last_item);
    bool TagNextChildren (wxTreeListItem *crt_item, wxTreeListItem *last_item);
    void UnselectAllChildren (wxTreeListItem *item );

private:
    DECLARE_EVENT_TABLE()
    DECLARE_DYNAMIC_CLASS(wxTreeListMainWindow)
};


// timer used for enabling in-place edit
class  wxTreeListRenameTimer: public wxTimer
{
public:
    wxTreeListRenameTimer( wxTreeListMainWindow *owner );

    void Notify();

private:
    wxTreeListMainWindow   *m_owner;
};

// control used for in-place edit
class  wxEditTextCtrl: public wxTextCtrl
{
public:
    wxEditTextCtrl (wxWindow *parent,
                    const wxWindowID id,
                    bool *accept,
                    wxString *res,
                    wxTreeListMainWindow *owner,
                    const wxString &value = wxEmptyString,
                    const wxPoint &pos = wxDefaultPosition,
                    const wxSize &size = wxDefaultSize,
                    int style = 0,
                    const wxValidator& validator = wxDefaultValidator,
                    const wxString &name = wxTextCtrlNameStr );

    void OnChar( wxKeyEvent &event );
    void OnKeyUp( wxKeyEvent &event );
    void OnKillFocus( wxFocusEvent &event );

private:
    bool               *m_accept;
    wxString           *m_res;
    wxTreeListMainWindow  *m_owner;
    wxString            m_startValue;
    bool                m_finished;

    DECLARE_EVENT_TABLE()
};

// a tree item
class  wxTreeListItem
{
public:
    // ctors & dtor
    wxTreeListItem() { m_data = NULL; }
    wxTreeListItem( wxTreeListMainWindow *owner,
                    wxTreeListItem *parent,
                    const wxArrayString& text,
                    int image,
                    int selImage,
                    wxTreeItemData *data );

    ~wxTreeListItem();

    // trivial accessors
    wxArrayTreeListItems& GetChildren() { return m_children; }

    const wxString GetText() const
    {
        return GetText(0);
    }
    const wxString GetText (int column) const
    {
        if(m_text.GetCount() > 0)
        {
            if( IsVirtual() )   return m_owner->GetItemText( m_data, column );
            else                return m_text[column];
        }
        return wxEmptyString;
    }

    int GetImage (wxTreeItemIcon which = wxTreeItemIcon_Normal) const
        { return m_images[which]; }
    int GetImage (int column, wxTreeItemIcon which=wxTreeItemIcon_Normal) const
    {
        if(column == m_owner->GetMainColumn()) return m_images[which];
        if(column < (int)m_col_images.GetCount()) return m_col_images[column];
        return NO_IMAGE;
    }

    wxTreeItemData *GetData() const { return m_data; }

    // returns the current image for the item (depending on its
    // selected/expanded/whatever state)
    int GetCurrentImage() const;

    void SetText (const wxString &text );
    void SetText (int column, const wxString& text)
    {
        if (column < (int)m_text.GetCount()) {
            m_text[column] = text;
        }else if (column < m_owner->GetColumnCount()) {
            int howmany = m_owner->GetColumnCount();
            for (int i = m_text.GetCount(); i < howmany; ++i) m_text.Add (wxEmptyString);
            m_text[column] = text;
        }
    }
    void SetImage (int image, wxTreeItemIcon which) { m_images[which] = image; }
    void SetImage (int column, int image, wxTreeItemIcon which)
    {
        if (column == m_owner->GetMainColumn()) {
            m_images[which] = image;
        }else if (column < (int)m_col_images.GetCount()) {
            m_col_images[column] = image;
        }else if (column < m_owner->GetColumnCount()) {
            int howmany = m_owner->GetColumnCount();
            for (int i = m_col_images.GetCount(); i < howmany; ++i) m_col_images.Add (NO_IMAGE);
            m_col_images[column] = image;
        }
    }

    void SetData(wxTreeItemData *data) { m_data = data; }

    void SetHasPlus(bool has = true) { m_hasPlus = has; }

    void SetBold(bool bold) { m_isBold = bold; }

    int GetX() const { return m_x; }
    int GetY() const { return m_y; }

    void SetX (int x) { m_x = x; }
    void SetY (int y) { m_y = y; }

    int  GetHeight() const { return m_height; }
    int  GetWidth()  const { return m_width; }

    void SetHeight (int height) { m_height = height; }
    void SetWidth (int width) { m_width = width; }

    int GetTextX() const { return m_text_x; }
    void SetTextX (int text_x) { m_text_x = text_x; }

    wxTreeListItem *GetItemParent() const { return m_parent; }

    // operations
    // deletes all children notifying the treectrl about it if !NULL
    // pointer given
    void DeleteChildren(wxTreeListMainWindow *tree = NULL);

    // get count of all children (and grand children if 'recursively')
    size_t GetChildrenCount(bool recursively = true) const;

    void Insert(wxTreeListItem *child, size_t index)
    { m_children.Insert(child, index); }

    void GetSize( int &x, int &y, const wxTreeListMainWindow* );

    // return the item at given position (or NULL if no item), onButton is
    // true if the point belongs to the item's button, otherwise it lies
    // on the button's label
    wxTreeListItem *HitTest (const wxPoint& point,
                             const wxTreeListMainWindow *,
                             int &flags, int& column, int level);

    void Expand() { m_isCollapsed = false; }
    void Collapse() { m_isCollapsed = true; }

    void SetHilight( bool set = true ) { m_hasHilight = set; }

    // status inquiries
    bool HasChildren() const { return !m_children.IsEmpty(); }
    bool IsSelected()  const { return m_hasHilight != 0; }
    bool IsExpanded()  const { return !m_isCollapsed; }
    bool HasPlus()     const { return m_hasPlus || HasChildren(); }
    bool IsBold()      const { return m_isBold != 0; }
    bool IsVirtual()   const { return m_owner->IsVirtual(); }

    // attributes
    // get them - may be NULL
    wxTreeItemAttr *GetAttributes() const { return m_attr; }
    // get them ensuring that the pointer is not NULL
    wxTreeItemAttr& Attr()
    {
        if ( !m_attr )
        {
            m_attr = new wxTreeItemAttr;
            m_ownsAttr = true;
        }
        return *m_attr;
    }
    // set them
    void SetAttributes(wxTreeItemAttr *attr)
    {
        if ( m_ownsAttr ) delete m_attr;
        m_attr = attr;
        m_ownsAttr = false;
    }
    // set them and delete when done
    void AssignAttributes(wxTreeItemAttr *attr)
    {
        SetAttributes(attr);
        m_ownsAttr = true;
    }

private:
    wxTreeListMainWindow  *m_owner;        // control the item belongs to

    // since there can be very many of these, we save size by chosing
    // the smallest representation for the elements and by ordering
    // the members to avoid padding.
    wxArrayString      m_text;    // labels to be rendered for item

    wxTreeItemData     *m_data;         // user-provided data

    wxArrayTreeListItems m_children; // list of children
    wxTreeListItem  *m_parent;       // parent of this item

    wxTreeItemAttr     *m_attr;         // attributes???

    // tree ctrl images for the normal, selected, expanded and
    // expanded+selected states
    short               m_images[wxTreeItemIcon_Max];
    wxArrayShort m_col_images; // images for the various columns (!= main)

    // main column item positions
    wxCoord             m_x;            // (virtual) offset from left (vertical line)
    wxCoord             m_y;            // (virtual) offset from top
    wxCoord             m_text_x;       // item offset from left
    short               m_width;        // width of this item
    unsigned char       m_height;       // height of this item

    // use bitfields to save size
    int                 m_isCollapsed :1;
    int                 m_hasHilight  :1; // same as focused
    int                 m_hasPlus     :1; // used for item which doesn't have
                                          // children but has a [+] button
    int                 m_isBold      :1; // render the label in bold font
    int                 m_ownsAttr    :1; // delete attribute when done
};

// ===========================================================================
// implementation
// ===========================================================================


// ---------------------------------------------------------------------------
// internal helpers
// ---------------------------------------------------------------------------

// check if the given item is under another one
static bool IsDescendantOf(const wxTreeListItem *parent, const wxTreeListItem *item)
{
    while ( item )
    {
        if ( item == parent )
        {
            // item is a descendant of parent
            return true;
        }

        item = item->GetItemParent();
    }

    return false;
}


// ---------------------------------------------------------------------------
// wxTreeListRenameTimer (internal)
// ---------------------------------------------------------------------------

wxTreeListRenameTimer::wxTreeListRenameTimer( wxTreeListMainWindow *owner )
{
    m_owner = owner;
}

void wxTreeListRenameTimer::Notify()
{
    m_owner->OnRenameTimer();
}

//-----------------------------------------------------------------------------
// wxEditTextCtrl (internal)
//-----------------------------------------------------------------------------

BEGIN_EVENT_TABLE (wxEditTextCtrl,wxTextCtrl)
    EVT_CHAR           (wxEditTextCtrl::OnChar)
    EVT_KEY_UP         (wxEditTextCtrl::OnKeyUp)
    EVT_KILL_FOCUS     (wxEditTextCtrl::OnKillFocus)
END_EVENT_TABLE()

wxEditTextCtrl::wxEditTextCtrl (wxWindow *parent,
                                const wxWindowID id,
                                bool *accept,
                                wxString *res,
                                wxTreeListMainWindow *owner,
                                const wxString &value,
                                const wxPoint &pos,
                                const wxSize &size,
                                int style,
                                const wxValidator& validator,
                                const wxString &name)
    : wxTextCtrl (parent, id, value, pos, size,
                  style|wxSIMPLE_BORDER|wxTE_PROCESS_ENTER, validator, name)
{
    m_res = res;
    m_accept = accept;
    m_owner = owner;
    (*m_accept) = false;
    (*m_res) = wxEmptyString;
    m_startValue = value;
    m_finished = false;
}

void wxEditTextCtrl::OnChar( wxKeyEvent &event )
{
    if (event.GetKeyCode() == WXK_RETURN)
    {
        (*m_accept) = true;
        (*m_res) = GetValue();

        if ((*m_res) != m_startValue)
            m_owner->OnRenameAccept();

        if (!wxPendingDelete.Member(this))
            wxPendingDelete.Append(this);

        m_finished = true;
        m_owner->SetFocus(); // This doesn't work. TODO.

        return;
    }
    if (event.GetKeyCode() == WXK_ESCAPE)
    {
        (*m_accept) = false;
        (*m_res) = wxEmptyString;

        if (!wxPendingDelete.Member(this))
            wxPendingDelete.Append(this);

        m_finished = true;
        m_owner->SetFocus(); // This doesn't work. TODO.

        return;
    }
    event.Skip();
}

void wxEditTextCtrl::OnKeyUp( wxKeyEvent &event )
{
    if (m_finished)
    {
        event.Skip();
        return;
    }

    // auto-grow the textctrl:
    wxSize parentSize = m_owner->GetSize();
    wxPoint myPos = GetPosition();
    wxSize mySize = GetSize();
    int sx, sy;
    GetTextExtent(GetValue() + _T("M"), &sx, &sy);
    if (myPos.x + sx > parentSize.x) sx = parentSize.x - myPos.x;
    if (mySize.x > sx) sx = mySize.x;
    SetSize(sx, -1);

    event.Skip();
}

void wxEditTextCtrl::OnKillFocus( wxFocusEvent &event )
{
    if (m_finished)
    {
        event.Skip();
        return;
    }

    if (!wxPendingDelete.Member(this))
        wxPendingDelete.Append(this);

    (*m_accept) = true;
    (*m_res) = GetValue();

    if ((*m_res) != m_startValue)
        m_owner->OnRenameAccept();
}

//-----------------------------------------------------------------------------
//  wxTreeListHeaderWindow
//-----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxTreeListHeaderWindow,wxWindow);

BEGIN_EVENT_TABLE(wxTreeListHeaderWindow,wxWindow)
    EVT_PAINT         (wxTreeListHeaderWindow::OnPaint)
    EVT_MOUSE_EVENTS  (wxTreeListHeaderWindow::OnMouse)
    EVT_SET_FOCUS     (wxTreeListHeaderWindow::OnSetFocus)
END_EVENT_TABLE()

void wxTreeListHeaderWindow::Init()
{
    m_currentCursor = (wxCursor *) NULL;
    m_isDragging = false;
    m_dirty = false;
    m_total_col_width = 0;
    m_hotTrackCol = -1;
}

wxTreeListHeaderWindow::wxTreeListHeaderWindow()
{
    Init();

    m_owner = (wxTreeListMainWindow *) NULL;
    m_resizeCursor = (wxCursor *) NULL;
}

wxTreeListHeaderWindow::wxTreeListHeaderWindow( wxWindow *win,
                                                wxWindowID id,
                                                wxTreeListMainWindow *owner,
                                                const wxPoint& pos,
                                                const wxSize& size,
                                                long style,
                                                const wxString &name )
    : wxWindow( win, id, pos, size, style, name )
{
    Init();

    m_owner = owner;
    m_resizeCursor = new wxCursor(wxCURSOR_SIZEWE);

#if !wxCHECK_VERSION(2, 5, 0)
    SetBackgroundColour (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_BTNFACE));
#else
    SetBackgroundColour (wxSystemSettings::GetColour (wxSYS_COLOUR_BTNFACE));
#endif
}

wxTreeListHeaderWindow::~wxTreeListHeaderWindow()
{
    delete m_resizeCursor;
}

void wxTreeListHeaderWindow::DoDrawRect( wxDC *dc, int x, int y, int w, int h )
{
#if !wxCHECK_VERSION(2, 5, 0)
    wxPen pen (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_BTNSHADOW ), 1, wxSOLID);
#else
    wxPen pen (wxSystemSettings::GetColour (wxSYS_COLOUR_BTNSHADOW ), 1, wxSOLID);
#endif

    const int m_corner = 1;

    dc->SetBrush( *wxTRANSPARENT_BRUSH );
#if defined( __WXMAC__  )
    dc->SetPen (pen);
#else // !GTK, !Mac
    dc->SetPen( *wxBLACK_PEN );
#endif
    dc->DrawLine( x+w-m_corner+1, y, x+w, y+h );  // right (outer)
    dc->DrawRectangle( x, y+h, w+1, 1 );          // bottom (outer)

#if defined( __WXMAC__  )
    pen = wxPen( wxColour( 0x88 , 0x88 , 0x88 ), 1, wxSOLID );
#endif
    dc->SetPen( pen );
    dc->DrawLine( x+w-m_corner, y, x+w-1, y+h );  // right (inner)
    dc->DrawRectangle( x+1, y+h-1, w-2, 1 );      // bottom (inner)

    dc->SetPen( *wxWHITE_PEN );
    dc->DrawRectangle( x, y, w-m_corner+1, 1 );   // top (outer)
    dc->DrawRectangle( x, y, 1, h );              // left (outer)
    dc->DrawLine( x, y+h-1, x+1, y+h-1 );
    dc->DrawLine( x+w-1, y, x+w-1, y+1 );
}

// shift the DC origin to match the position of the main window horz
// scrollbar: this allows us to always use logical coords
void wxTreeListHeaderWindow::AdjustDC(wxDC& dc)
{
    int xpix;
    m_owner->GetScrollPixelsPerUnit( &xpix, NULL );
    int x;
    m_owner->GetViewStart( &x, NULL );

    // account for the horz scrollbar offset
    dc.SetDeviceOrigin( -x * xpix, 0 );
}

void wxTreeListHeaderWindow::OnPaint( wxPaintEvent &WXUNUSED(event) )
{
#ifdef __WXGTK__
    wxClientDC dc( this );
#else
    wxPaintDC dc( this );
#endif

    PrepareDC( dc );
    AdjustDC( dc );

    int x = HEADER_OFFSET_X;

    // width and height of the entire header window
    int w, h;
    GetClientSize( &w, &h );
    m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
    dc.SetBackgroundMode(wxTRANSPARENT);

#if wxCHECK_VERSION_FULL(2, 7, 0, 1)
    int numColumns = GetColumnCount();
    for ( int i = 0; i < numColumns && x < w; i++ )
    {
        if (!IsColumnShown (i)) continue; // do next column if not shown

        wxHeaderButtonParams params;

        // TODO: columnInfo should have label colours...
        params.m_labelColour = wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT );
        params.m_labelFont = GetFont();

        wxTreeListColumnInfo& column = GetColumn(i);
        int wCol = column.GetWidth();
        int flags = 0;
        wxRect rect(x, 0, wCol, h);
        x += wCol;

        if ( i == m_hotTrackCol)
            flags |= wxCONTROL_CURRENT;
        
        params.m_labelText = column.GetText();
        params.m_labelAlignment = column.GetAlignment();

        int image = column.GetImage();
        wxImageList* imageList = m_owner->GetImageList();
        if ((image != -1) && imageList) 
            params.m_labelBitmap = imageList->GetBitmap(image);
                
        wxRendererNative::Get().DrawHeaderButton(this, dc, rect, flags,
                                                 wxHDR_SORT_ICON_NONE, &params);
    }

    if (x < w) {
        wxRect rect(x, 0, w-x, h);
        wxRendererNative::Get().DrawHeaderButton(this, dc, rect);
    }
    
#else  // not 2.7.0.1+
    
    dc.SetFont( GetFont() );

    // do *not* use the listctrl colour for headers - one day we will have a
    // function to set it separately
    //dc.SetTextForeground( *wxBLACK );
#if !wxCHECK_VERSION(2, 5, 0)
    dc.SetTextForeground (wxSystemSettings::GetSystemColour( wxSYS_COLOUR_WINDOWTEXT ));
#else
    dc.SetTextForeground (wxSystemSettings::GetColour( wxSYS_COLOUR_WINDOWTEXT ));
#endif

    int numColumns = GetColumnCount();
    for ( int i = 0; i < numColumns && x < w; i++ )
    {
        if (!IsColumnShown (i)) continue; // do next column if not shown

        wxTreeListColumnInfo& column = GetColumn(i);
        int wCol = column.GetWidth();

        // the width of the rect to draw: make it smaller to fit entirely
        // inside the column rect
        int cw = wCol - 2;

#if !wxCHECK_VERSION(2, 7, 0)
        dc.SetPen( *wxWHITE_PEN );
        DoDrawRect( &dc, x, HEADER_OFFSET_Y, cw, h-2 );
#else
        wxRect rect(x, HEADER_OFFSET_Y, cw, h-2);
        wxRendererNative::GetDefault().DrawHeaderButton (this, dc, rect);
#endif

        // if we have an image, draw it on the right of the label
        int image = column.GetImage(); //item.m_image;
        int ix = -2, iy = 0;
        wxImageList* imageList = m_owner->GetImageList();
        if ((image != -1) && imageList) {
            imageList->GetSize (image, ix, iy);
        }

        // extra margins around the text label
        int text_width = 0;
        int text_x = x;
        int image_offset = cw - ix - 1;

        switch(column.GetAlignment()) {
        case wxALIGN_LEFT:
            text_x += EXTRA_WIDTH;
            cw -= ix + 2;
            break;
        case wxALIGN_RIGHT:
            dc.GetTextExtent (column.GetText(), &text_width, NULL);
            text_x += cw - text_width - EXTRA_WIDTH - MARGIN;
            image_offset = 0;
            break;
        case wxALIGN_CENTER:
            dc.GetTextExtent(column.GetText(), &text_width, NULL);
            text_x += (cw - text_width)/2 + ix + 2;
            image_offset = (cw - text_width - ix - 2)/2 - MARGIN;
            break;
        }

        // draw the image
        if ((image != -1) && imageList) {
            imageList->Draw (image, dc, x + image_offset/*cw - ix - 1*/,
                             HEADER_OFFSET_Y + (h - 4 - iy)/2,
                             wxIMAGELIST_DRAW_TRANSPARENT);
        }

        // draw the text clipping it so that it doesn't overwrite the column boundary
        wxDCClipper clipper(dc, x, HEADER_OFFSET_Y, cw, h - 4 );
        dc.DrawText (column.GetText(), text_x, HEADER_OFFSET_Y + EXTRA_HEIGHT );

        // next column
        x += wCol;
    }

    int more_w = m_owner->GetSize().x - x - HEADER_OFFSET_X;
    if (more_w > 0) {
#if !wxCHECK_VERSION(2, 7, 0)
        DoDrawRect (&dc, x, HEADER_OFFSET_Y, more_w, h-2 );
#else
        wxRect rect (x, HEADER_OFFSET_Y, more_w, h-2);
        wxRendererNative::GetDefault().DrawHeaderButton (this, dc, rect);
#endif
    }
#endif // 2.7.0.1
}

void wxTreeListHeaderWindow::DrawCurrent()
{
    int x1 = m_currentX;
    int y1 = 0;
    ClientToScreen (&x1, &y1);

    int x2 = m_currentX-1;
#ifdef __WXMSW__
    ++x2; // but why ????
#endif
    int y2 = 0;
    m_owner->GetClientSize( NULL, &y2 );
    m_owner->ClientToScreen( &x2, &y2 );

    wxScreenDC dc;
    dc.SetLogicalFunction (wxINVERT);
    dc.SetPen (wxPen (*wxBLACK, 2, wxSOLID));
    dc.SetBrush (*wxTRANSPARENT_BRUSH);

    AdjustDC(dc);
    dc.DrawLine (x1, y1, x2, y2);
    dc.SetLogicalFunction (wxCOPY);
    dc.SetPen (wxNullPen);
    dc.SetBrush (wxNullBrush);
}

#if wxCHECK_VERSION_FULL(2, 7, 0, 1)
int wxTreeListHeaderWindow::XToCol(int x)
{
    int colLeft = 0;
    int numColumns = GetColumnCount();
    for ( int col = 0; col < numColumns; col++ )
    {
        if (!IsColumnShown(col)) continue; 
        wxTreeListColumnInfo& column = GetColumn(col);

        if ( x < (colLeft + column.GetWidth()) )
             return col;
        
        colLeft += column.GetWidth();
    }
    return -1;
}


void wxTreeListHeaderWindow::RefreshColLabel(int col)
{
    if ( col >= GetColumnCount() )
        return;
    
    int x = 0;
    int width = 0;
    int idx = 0;
    do {
        if (!IsColumnShown(idx)) continue; 
        wxTreeListColumnInfo& column = GetColumn(idx);
        x += width;
        width = column.GetWidth();
    } while (++idx <= col);
    
    m_owner->CalcScrolledPosition(x, 0, &x, NULL);
    RefreshRect(wxRect(x, 0, width, GetSize().GetHeight()));   
}
#endif

        
void wxTreeListHeaderWindow::OnMouse (wxMouseEvent &event) {

    // we want to work with logical coords
    int x;
    m_owner->CalcUnscrolledPosition(event.GetX(), 0, &x, NULL);
    int y = event.GetY();

#if wxCHECK_VERSION_FULL(2, 7, 0, 1)
    if ( event.Moving() )
    {
        int col = XToCol(x);
        if ( col != m_hotTrackCol )
        {
            // Refresh the col header so it will be painted with hot tracking
            // (if supported by the native renderer.)
            RefreshColLabel(col);

            // Also refresh the old hot header
            if ( m_hotTrackCol >= 0 )
                RefreshColLabel(m_hotTrackCol);

            m_hotTrackCol = col;
        }
    }

    if ( event.Leaving() && m_hotTrackCol >= 0 )
    {
        // Leaving the window so clear any hot tracking indicator that may be present
        RefreshColLabel(m_hotTrackCol);
        m_hotTrackCol = -1;
    }
#endif

    if (m_isDragging) {

        SendListEvent (wxEVT_COMMAND_LIST_COL_DRAGGING, event.GetPosition());

        // we don't draw the line beyond our window, but we allow dragging it
        // there
        int w = 0;
        GetClientSize( &w, NULL );
        m_owner->CalcUnscrolledPosition(w, 0, &w, NULL);
        w -= 6;

        // erase the line if it was drawn
        if (m_currentX < w) DrawCurrent();

        if (event.ButtonUp()) {
            m_isDragging = false;
            if (HasCapture()) ReleaseMouse();
            m_dirty = true;
            SetColumnWidth (m_column, m_currentX - m_minX);
            Refresh();
            SendListEvent (wxEVT_COMMAND_LIST_COL_END_DRAG, event.GetPosition());
        }else{
            m_currentX = wxMax (m_minX + 7, x);

            // draw in the new location
            if (m_currentX < w) DrawCurrent();
        }

    }else{ // not dragging

        m_minX = 0;
        bool hit_border = false;

        // end of the current column
        int xpos = 0;

        // find the column where this event occured
        int countCol = GetColumnCount();
        for (int column = 0; column < countCol; column++) {
            if (!IsColumnShown (column)) continue; // do next if not shown

            xpos += GetColumnWidth (column);
            m_column = column;
            if ((abs (x-xpos) < 3) && (y < 22)) {
                // near the column border
                hit_border = true;
                break;
            }

            if (x < xpos) {
                // inside the column
                break;
            }

            m_minX = xpos;
        }

        if (event.LeftDown() || event.RightUp()) {
            if (hit_border && event.LeftDown()) {
                m_isDragging = true;
                CaptureMouse();
                m_currentX = x;
                DrawCurrent();
                SendListEvent (wxEVT_COMMAND_LIST_COL_BEGIN_DRAG, event.GetPosition());
            }else{ // click on a column
                wxEventType evt = event.LeftDown()? wxEVT_COMMAND_LIST_COL_CLICK:
                                                    wxEVT_COMMAND_LIST_COL_RIGHT_CLICK;
                SendListEvent (evt, event.GetPosition());
            }
        }else if (event.LeftDClick() && hit_border) {
            SetColumnWidth (m_column, m_owner->GetBestColumnWidth (m_column));
            Refresh();

        }else if (event.Moving()) {
            bool setCursor;
            if (hit_border) {
                setCursor = m_currentCursor == wxSTANDARD_CURSOR;
                m_currentCursor = m_resizeCursor;
            }else{
                setCursor = m_currentCursor != wxSTANDARD_CURSOR;
                m_currentCursor = wxSTANDARD_CURSOR;
            }
            if (setCursor) SetCursor (*m_currentCursor);
        }

    }
}

void wxTreeListHeaderWindow::OnSetFocus (wxFocusEvent &WXUNUSED(event)) {
    m_owner->SetFocus();
}

void wxTreeListHeaderWindow::SendListEvent (wxEventType type, wxPoint pos) {
    wxWindow *parent = GetParent();
    wxListEvent le (type, parent->GetId());
    le.SetEventObject (parent);
    le.m_pointDrag = pos;

    // the position should be relative to the parent window, not
    // this one for compatibility with MSW and common sense: the
    // user code doesn't know anything at all about this header
    // window, so why should it get positions relative to it?
    le.m_pointDrag.y -= GetSize().y;
    le.m_col = m_column;
    parent->GetEventHandler()->ProcessEvent (le);
}

void wxTreeListHeaderWindow::AddColumn (const wxTreeListColumnInfo& colInfo) {
    m_columns.Add (colInfo);
    m_total_col_width += colInfo.GetWidth();
    m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
}

void wxTreeListHeaderWindow::SetColumnWidth (int column, int width) {
    wxCHECK_RET ((column >= 0) && (column < GetColumnCount()), _T("Invalid column"));
    m_total_col_width -= m_columns[column].GetWidth();
    m_columns[column].SetWidth(width);
    m_total_col_width += width;
    m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
}

void wxTreeListHeaderWindow::InsertColumn (int before, const wxTreeListColumnInfo& colInfo) {
    wxCHECK_RET ((before >= 0) && (before < GetColumnCount()), _T("Invalid column"));
    m_columns.Insert (colInfo, before);
    m_total_col_width += colInfo.GetWidth();
    m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
}

void wxTreeListHeaderWindow::RemoveColumn (int column) {
    wxCHECK_RET ((column >= 0) && (column < GetColumnCount()), _T("Invalid column"));
    m_total_col_width -= m_columns[column].GetWidth();
    m_columns.RemoveAt (column);
    m_owner->AdjustMyScrollbars();
    m_owner->m_dirty = true;
}

void wxTreeListHeaderWindow::SetColumn (int column, const wxTreeListColumnInfo& info) {
    wxCHECK_RET ((column >= 0) && (column < GetColumnCount()), _T("Invalid column"));
    int w = m_columns[column].GetWidth();
    m_columns[column] = info;
    if (w != info.GetWidth()) {
        m_total_col_width += info.GetWidth() - w;
        m_owner->AdjustMyScrollbars();
    }
    m_owner->m_dirty = true;
}

// ---------------------------------------------------------------------------
// wxTreeListItem
// ---------------------------------------------------------------------------

wxTreeListItem::wxTreeListItem (wxTreeListMainWindow *owner,
                                wxTreeListItem *parent,
                                const wxArrayString& text,
                                int image, int selImage,
                                wxTreeItemData *data)
              : m_text (text) {

    m_images[wxTreeItemIcon_Normal] = image;
    m_images[wxTreeItemIcon_Selected] = selImage;
    m_images[wxTreeItemIcon_Expanded] = NO_IMAGE;
    m_images[wxTreeItemIcon_SelectedExpanded] = NO_IMAGE;

    m_data = data;
    m_x = 0;
    m_y = 0;
    m_text_x = 0;

    m_isCollapsed = true;
    m_hasHilight = false;
    m_hasPlus = false;
    m_isBold = false;

    m_owner = owner;
    m_parent = parent;

    m_attr = (wxTreeItemAttr *)NULL;
    m_ownsAttr = false;

    // We don't know the height here yet.
    m_width = 0;
    m_height = 0;
}

wxTreeListItem::~wxTreeListItem() {
    delete m_data;
    if (m_ownsAttr) delete m_attr;

    wxASSERT_MSG( m_children.IsEmpty(), _T("please call DeleteChildren() before destructor"));
}

void wxTreeListItem::DeleteChildren (wxTreeListMainWindow *tree) {
    size_t count = m_children.Count();
    for (size_t n = 0; n < count; n++) {
        wxTreeListItem *child = m_children[n];
        if (tree) {
            tree->SendDeleteEvent (child);
            if (tree->m_selectItem == child) tree->m_selectItem = (wxTreeListItem*)NULL;
            if (tree->m_curItem == child) tree->m_curItem = this;
        }
        child->DeleteChildren (tree);
        delete child;
    }
    m_children.Empty();
}

void wxTreeListItem::SetText (const wxString &text) {
    if (m_text.GetCount() > 0) {
        m_text[0] = text;
    }else{
        m_text.Add (text);
    }
}

size_t wxTreeListItem::GetChildrenCount (bool recursively) const {
    size_t count = m_children.Count();
    if (!recursively) return count;

    size_t total = count;
    for (size_t n = 0; n < count; ++n) {
        total += m_children[n]->GetChildrenCount();
    }
    return total;
}

void wxTreeListItem::GetSize (int &x, int &y, const wxTreeListMainWindow *theButton) {
    int bottomY = m_y + theButton->GetLineHeight (this);
    if (y < bottomY) y = bottomY;
    int width = m_x +  m_width;
    if ( x < width ) x = width;

    if (IsExpanded()) {
        size_t count = m_children.Count();
        for (size_t n = 0; n < count; ++n ) {
            m_children[n]->GetSize (x, y, theButton);
        }
    }
}

wxTreeListItem *wxTreeListItem::HitTest (const wxPoint& point,
                                         const wxTreeListMainWindow *theCtrl,
                                         int &flags, int& column, int level) {

    // for a hidden root node, don't evaluate it, but do evaluate children
    if (!theCtrl->HasFlag(wxTR_HIDE_ROOT) || (level > 0)) {

        // reset any previous hit infos
        flags = 0;
        column = -1;
        wxTreeListHeaderWindow* header_win = theCtrl->m_owner->GetHeaderWindow();

        // check for right of all columns (outside)
        if (point.x > header_win->GetWidth()) return (wxTreeListItem*) NULL;

        // evaluate if y-pos is okay
        int h = theCtrl->GetLineHeight (this);
        if ((point.y >= m_y) && (point.y <= m_y + h)) {

            int maincol = theCtrl->GetMainColumn();

            // check for above/below middle
            int y_mid = m_y + h/2;
            if (point.y < y_mid) {
                flags |= wxTREE_HITTEST_ONITEMUPPERPART;
            }else{
                flags |= wxTREE_HITTEST_ONITEMLOWERPART;
            }

            // check for button hit
            if (HasPlus() && theCtrl->HasButtons()) {
                int bntX = m_x - theCtrl->m_btnWidth2;
                int bntY = y_mid - theCtrl->m_btnHeight2;
                if ((point.x >= bntX) && (point.x <= (bntX + theCtrl->m_btnWidth)) &&
                    (point.y >= bntY) && (point.y <= (bntY + theCtrl->m_btnHeight))) {
                    flags |= wxTREE_HITTEST_ONITEMBUTTON;
                    column = maincol;
                    return this;
                }
            }

            // check for image hit
            if (theCtrl->m_imgWidth > 0) {
                int imgX = m_text_x - theCtrl->m_imgWidth - MARGIN;
                int imgY = y_mid - theCtrl->m_imgHeight2;
                if ((point.x >= imgX) && (point.x <= (imgX + theCtrl->m_imgWidth)) &&
                    (point.y >= imgY) && (point.y <= (imgY + theCtrl->m_imgHeight))) {
                    flags |= wxTREE_HITTEST_ONITEMICON;
                    column = maincol;
                    return this;
                }
            }

            // check for label hit
            if ((point.x >= m_text_x) && (point.x <= (m_text_x + m_width))) {
                flags |= wxTREE_HITTEST_ONITEMLABEL;
                column = maincol;
                return this;
            }

            // check for indent hit after button and image hit
            if (point.x < m_x) {
                flags |= wxTREE_HITTEST_ONITEMINDENT;
                column = -1; // considered not belonging to main column
                return this;
            }

            // check for right of label
            int end = 0;
            for (int i = 0; i <= maincol; ++i) end += header_win->GetColumnWidth (i);
            if ((point.x > (m_text_x + m_width)) && (point.x <= end)) {
                flags |= wxTREE_HITTEST_ONITEMRIGHT;
                column = -1; // considered not belonging to main column
                return this;
            }

            // else check for each column except main
            int x = 0;
            for (int j = 0; j < theCtrl->GetColumnCount(); ++j) {
                if (!header_win->IsColumnShown(j)) continue;
                int w = header_win->GetColumnWidth (j);
                if ((j != maincol) && (point.x >= x && point.x < x+w)) {
                    flags |= wxTREE_HITTEST_ONITEMCOLUMN;
                    column = j;
                    return this;
                }
                x += w;
            }

            // no special flag or column found
            return this;

        }

        // if children not expanded, return no item
        if (!IsExpanded()) return (wxTreeListItem*) NULL;
    }

    // in any case evaluate children
    wxTreeListItem *child;
    size_t count = m_children.Count();
    for (size_t n = 0; n < count; n++) {
        child = m_children[n]->HitTest (point, theCtrl, flags, column, level+1);
        if (child) return child;
    }

    // not found
    return (wxTreeListItem*) NULL;
}

int wxTreeListItem::GetCurrentImage() const {
    int image = NO_IMAGE;
    if (IsExpanded()) {
        if (IsSelected()) {
            image = GetImage (wxTreeItemIcon_SelectedExpanded);
        }else{
            image = GetImage (wxTreeItemIcon_Expanded);
        }
    }else{ // not expanded
        if (IsSelected()) {
            image = GetImage (wxTreeItemIcon_Selected);
        }else{
            image = GetImage (wxTreeItemIcon_Normal);
        }
    }

    // maybe it doesn't have the specific image, try the default one instead
    if (image == NO_IMAGE) image = GetImage();

    return image;
}

// ---------------------------------------------------------------------------
// wxTreeListMainWindow implementation
// ---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxTreeListMainWindow, wxScrolledWindow)

BEGIN_EVENT_TABLE(wxTreeListMainWindow, wxScrolledWindow)
    EVT_PAINT          (wxTreeListMainWindow::OnPaint)
    EVT_MOUSE_EVENTS   (wxTreeListMainWindow::OnMouse)
    EVT_CHAR           (wxTreeListMainWindow::OnChar)
    EVT_SET_FOCUS      (wxTreeListMainWindow::OnSetFocus)
    EVT_KILL_FOCUS     (wxTreeListMainWindow::OnKillFocus)
    EVT_IDLE           (wxTreeListMainWindow::OnIdle)
    EVT_SCROLLWIN      (wxTreeListMainWindow::OnScroll)
END_EVENT_TABLE()


// ---------------------------------------------------------------------------
// construction/destruction
// ---------------------------------------------------------------------------

void wxTreeListMainWindow::Init() {

    m_rootItem = (wxTreeListItem*)NULL;
    m_curItem = (wxTreeListItem*)NULL;
    m_shiftItem = (wxTreeListItem*)NULL;
    m_editItem = (wxTreeListItem*)NULL;
    m_selectItem = (wxTreeListItem*)NULL;
    m_select_me = (wxTreeListItem*)NULL;
    
    m_curColumn = -1; // no current column
    m_drag_item = (wxTreeListItem*)NULL;

    m_hasFocus = false;
    m_dirty = false;
    m_should_return = false;

    m_lineHeight = LINEHEIGHT;
    m_indent = MININDENT; // min. indent
    m_linespacing = 4;

#if !wxCHECK_VERSION(2, 5, 0)
    m_hilightBrush = new wxBrush (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_HIGHLIGHT), wxSOLID);
    m_hilightUnfocusedBrush = new wxBrush (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_BTNSHADOW), wxSOLID);
#else
    m_hilightBrush = new wxBrush (wxSystemSettings::GetColour (wxSYS_COLOUR_HIGHLIGHT), wxSOLID);
    m_hilightUnfocusedBrush = new wxBrush (wxSystemSettings::GetColour (wxSYS_COLOUR_BTNSHADOW), wxSOLID);
#endif

    m_imageListNormal = (wxImageList *) NULL;
    m_imageListButtons = (wxImageList *) NULL;
    m_imageListState = (wxImageList *) NULL;
    m_ownsImageListNormal = m_ownsImageListButtons =
    m_ownsImageListState = false;

    m_imgWidth = 0, m_imgWidth2 = 0;
    m_imgHeight = 0, m_imgHeight2 = 0;
    m_btnWidth = 0, m_btnWidth2 = 0;
    m_btnHeight = 0, m_btnHeight2 = 0;

    m_dragCount = 0;
    m_isDragging = false;
    m_dragTimer = new wxTimer (this, -1);
    m_dragItem = (wxTreeListItem*)NULL;

    m_renameTimer = new wxTreeListRenameTimer (this);
    m_lastOnSame = false;
    m_left_down_selection = false;

    m_findTimer = new wxTimer (this, -1);

#if defined( __WXMAC__ ) && defined(__WXMAC_CARBON__)
    m_normalFont.MacCreateFromThemeFont (kThemeViewsFont);
#else
    m_normalFont = wxSystemSettings::GetFont (wxSYS_DEFAULT_GUI_FONT);
#endif
    m_boldFont = wxFont( m_normalFont.GetPointSize(),
                         (m_normalFont.GetFamily() != wxFONTFAMILY_UNKNOWN ? m_normalFont.GetFamily() : wxSWISS),
                         m_normalFont.GetStyle(),
                         wxBOLD,
                         m_normalFont.GetUnderlined(),
                         m_normalFont.GetFaceName(),
                         m_normalFont.GetEncoding());
}

bool wxTreeListMainWindow::Create (wxTreeListCtrl *parent,
                                   wxWindowID id,
                                   const wxPoint& pos,
                                   const wxSize& size,
                                   long style,
                                   const wxValidator &validator,
                                   const wxString& name) {

#ifdef __WXMAC__
    style &= ~wxTR_LINES_AT_ROOT;
    style |= wxTR_NO_LINES;

    int major,minor;
    wxGetOsVersion( &major, &minor );
    if (major < 10) style |= wxTR_ROW_LINES;
#endif

    wxScrolledWindow::Create (parent, id, pos, size, style|wxHSCROLL|wxVSCROLL|wxWANTS_CHARS, name);

#if wxUSE_VALIDATORS
    SetValidator(validator);
#endif

#if !wxCHECK_VERSION(2, 5, 0)
    SetBackgroundColour (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_LISTBOX));
#else
    SetBackgroundColour (wxSystemSettings::GetColour (wxSYS_COLOUR_LISTBOX));
#endif

#ifdef __WXMSW__
    {
        int i, j;
        wxBitmap bmp(8, 8);
        wxMemoryDC bdc;
        bdc.SelectObject(bmp);
        bdc.SetPen(*wxGREY_PEN);
        bdc.DrawRectangle(-1, -1, 10, 10);
        for (i = 0; i < 8; i++) {
            for (j = 0; j < 8; j++) {
                if (!((i + j) & 1)) {
                    bdc.DrawPoint(i, j);
                }
            }
        }

        m_dottedPen = wxPen(bmp, 1);
    }
#else
//?    m_dottedPen = wxPen( *wxGREY_PEN, 1, wxDOT );  // too slow under XFree86
    m_dottedPen = wxPen( _T("grey"), 0, 0 ); // Bitmap based pen is not supported by GTK!
#endif

    m_owner = parent;
    m_main_column = 0;

    return true;
}

wxTreeListMainWindow::~wxTreeListMainWindow() {
    delete m_hilightBrush;
    delete m_hilightUnfocusedBrush;

    delete m_dragTimer;
    delete m_renameTimer;
    delete m_findTimer;
    if (m_ownsImageListNormal) delete m_imageListNormal;
    if (m_ownsImageListState) delete m_imageListState;
    if (m_ownsImageListButtons) delete m_imageListButtons;

    DeleteRoot();
}


//-----------------------------------------------------------------------------
// accessors
//-----------------------------------------------------------------------------

size_t wxTreeListMainWindow::GetCount() const {
    return m_rootItem == NULL? 0: m_rootItem->GetChildrenCount();
}

void wxTreeListMainWindow::SetIndent (unsigned int indent) {
    m_indent = wxMax ((unsigned)MININDENT, indent);
    m_dirty = true;
}

void wxTreeListMainWindow::SetLineSpacing (unsigned int spacing) {
    m_linespacing = spacing;
    m_dirty = true;
    CalculateLineHeight();
}

size_t wxTreeListMainWindow::GetChildrenCount (const wxTreeItemId& item,
                                               bool recursively) {
    wxCHECK_MSG (item.IsOk(), 0u, _T("invalid tree item"));
    return ((wxTreeListItem*)item.m_pItem)->GetChildrenCount (recursively);
}

void wxTreeListMainWindow::SetWindowStyle (const long styles) {
    // right now, just sets the styles.  Eventually, we may
    // want to update the inherited styles, but right now
    // none of the parents has updatable styles
    m_windowStyle = styles;
    m_dirty = true;
}

//-----------------------------------------------------------------------------
// functions to work with tree items
//-----------------------------------------------------------------------------

int wxTreeListMainWindow::GetItemImage (const wxTreeItemId& item, int column,
                                        wxTreeItemIcon which) const {
    wxCHECK_MSG (item.IsOk(), -1, _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->GetImage (column, which);
}

wxTreeItemData *wxTreeListMainWindow::GetItemData (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), NULL, _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->GetData();
}

bool wxTreeListMainWindow::GetItemBold (const wxTreeItemId& item) const {
    wxCHECK_MSG(item.IsOk(), false, _T("invalid tree item"));
    return ((wxTreeListItem *)item.m_pItem)->IsBold();
}

wxColour wxTreeListMainWindow::GetItemTextColour (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxNullColour, _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    return pItem->Attr().GetTextColour();
}

wxColour wxTreeListMainWindow::GetItemBackgroundColour (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxNullColour, _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    return pItem->Attr().GetBackgroundColour();
}

wxFont wxTreeListMainWindow::GetItemFont (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxNullFont, _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    return pItem->Attr().GetFont();
}

void wxTreeListMainWindow::SetItemImage (const wxTreeItemId& item, int column,
                                         int image, wxTreeItemIcon which) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    pItem->SetImage (column, image, which);
    wxClientDC dc (this);
    CalculateSize (pItem, dc);
    RefreshLine (pItem);
}

void wxTreeListMainWindow::SetItemData (const wxTreeItemId& item,
                                        wxTreeItemData *data) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    ((wxTreeListItem*) item.m_pItem)->SetData(data);
}

void wxTreeListMainWindow::SetItemHasChildren (const wxTreeItemId& item,
                                               bool has) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    pItem->SetHasPlus (has);
    RefreshLine (pItem);
}

void wxTreeListMainWindow::SetItemBold (const wxTreeItemId& item, bool bold) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    if (pItem->IsBold() != bold) { // avoid redrawing if no real change
        pItem->SetBold (bold);
        RefreshLine (pItem);
    }
}

void wxTreeListMainWindow::SetItemTextColour (const wxTreeItemId& item,
                                              const wxColour& colour) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    pItem->Attr().SetTextColour (colour);
    RefreshLine (pItem);
}

void wxTreeListMainWindow::SetItemBackgroundColour (const wxTreeItemId& item,
                                                    const wxColour& colour) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    pItem->Attr().SetBackgroundColour (colour);
    RefreshLine (pItem);
}

void wxTreeListMainWindow::SetItemFont (const wxTreeItemId& item,
                                        const wxFont& font) {
    wxCHECK_RET (item.IsOk(), _T("invalid tree item"));
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    pItem->Attr().SetFont (font);
    RefreshLine (pItem);
}

bool wxTreeListMainWindow::SetFont (const wxFont &font) {
    wxScrolledWindow::SetFont (font);
    m_normalFont = font;
    m_boldFont = wxFont (m_normalFont.GetPointSize(),
                         m_normalFont.GetFamily(),
                         m_normalFont.GetStyle(),
                         wxBOLD,
                         m_normalFont.GetUnderlined(),
                         m_normalFont.GetFaceName());
    CalculateLineHeight();
    return true;
}


// ----------------------------------------------------------------------------
// item status inquiries
// ----------------------------------------------------------------------------

bool wxTreeListMainWindow::IsVisible (const wxTreeItemId& item, bool fullRow) const {
    wxCHECK_MSG (item.IsOk(), false, _T("invalid tree item"));

    // An item is only visible if it's not a descendant of a collapsed item
    wxTreeListItem *pItem = (wxTreeListItem*) item.m_pItem;
    wxTreeListItem* parent = pItem->GetItemParent();
    while (parent) {
        if (parent == m_rootItem && HasFlag(wxTR_HIDE_ROOT)) break;
        if (!parent->IsExpanded()) return false;
        parent = parent->GetItemParent();
    }

    wxSize clientSize = GetClientSize();
    wxRect rect;
    if ((!GetBoundingRect (item, rect)) ||
        ((!fullRow && rect.GetWidth() == 0) || rect.GetHeight() == 0) ||
        (rect.GetBottom() < 0 || rect.GetTop() > clientSize.y) ||
        (!fullRow && (rect.GetRight() < 0 || rect.GetLeft() > clientSize.x))) return false;

    return true;
}

bool wxTreeListMainWindow::HasChildren (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), false, _T("invalid tree item"));

    // consider that the item does have children if it has the "+" button: it
    // might not have them (if it had never been expanded yet) but then it
    // could have them as well and it's better to err on this side rather than
    // disabling some operations which are restricted to the items with
    // children for an item which does have them
    return ((wxTreeListItem*) item.m_pItem)->HasPlus();
}

bool wxTreeListMainWindow::IsExpanded (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), false, _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->IsExpanded();
}

bool wxTreeListMainWindow::IsSelected (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), false, _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->IsSelected();
}

bool wxTreeListMainWindow::IsBold (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), false, _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->IsBold();
}

// ----------------------------------------------------------------------------
// navigation
// ----------------------------------------------------------------------------

wxTreeItemId wxTreeListMainWindow::GetItemParent (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    return ((wxTreeListItem*) item.m_pItem)->GetItemParent();
}

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListMainWindow::GetFirstChild (const wxTreeItemId& item,
                                                  long& cookie) const {
#else
wxTreeItemId wxTreeListMainWindow::GetFirstChild (const wxTreeItemId& item,
                                                  wxTreeItemIdValue& cookie) const {
#endif
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxArrayTreeListItems& children = ((wxTreeListItem*) item.m_pItem)->GetChildren();
    cookie = 0;
    return (!children.IsEmpty())? wxTreeItemId(children.Item(0)): wxTreeItemId();
}

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListMainWindow::GetNextChild (const wxTreeItemId& item,
                                                 long& cookie) const {
#else
wxTreeItemId wxTreeListMainWindow::GetNextChild (const wxTreeItemId& item,
                                                 wxTreeItemIdValue& cookie) const {
#endif
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxArrayTreeListItems& children = ((wxTreeListItem*) item.m_pItem)->GetChildren();
    // it's ok to cast cookie to long, we never have indices which overflow "void*"
    long *pIndex = ((long*)&cookie);
    return ((*pIndex)+1 < (long)children.Count())? wxTreeItemId(children.Item(++(*pIndex))): wxTreeItemId();
}

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListMainWindow::GetPrevChild (const wxTreeItemId& item,
                                                 long& cookie) const {
#else
wxTreeItemId wxTreeListMainWindow::GetPrevChild (const wxTreeItemId& item,
                                                 wxTreeItemIdValue& cookie) const {
#endif
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxArrayTreeListItems& children = ((wxTreeListItem*) item.m_pItem)->GetChildren();
    // it's ok to cast cookie to long, we never have indices which overflow "void*"
    long *pIndex = (long*)&cookie;
    return ((*pIndex)-1 >= 0)? wxTreeItemId(children.Item(--(*pIndex))): wxTreeItemId();
}

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListMainWindow::GetLastChild (const wxTreeItemId& item,
                                                 long& cookie) const {
#else
wxTreeItemId wxTreeListMainWindow::GetLastChild (const wxTreeItemId& item,
                                                 wxTreeItemIdValue& cookie) const {
#endif
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxArrayTreeListItems& children = ((wxTreeListItem*) item.m_pItem)->GetChildren();
    // it's ok to cast cookie to long, we never have indices which overflow "void*"
    long *pIndex = ((long*)&cookie);
    (*pIndex) = children.Count();
    return (!children.IsEmpty())? wxTreeItemId(children.Last()): wxTreeItemId();
}

wxTreeItemId wxTreeListMainWindow::GetNextSibling (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));

    // get parent
    wxTreeListItem *i = (wxTreeListItem*) item.m_pItem;
    wxTreeListItem *parent = i->GetItemParent();
    if (!parent) return wxTreeItemId(); // root item doesn't have any siblings

    // get index
    wxArrayTreeListItems& siblings = parent->GetChildren();
    size_t index = siblings.Index (i);
    wxASSERT (index != (size_t)wxNOT_FOUND); // I'm not a child of my parent?
    return (index < siblings.Count()-1)? wxTreeItemId(siblings[index+1]): wxTreeItemId();
}

wxTreeItemId wxTreeListMainWindow::GetPrevSibling (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));

    // get parent
    wxTreeListItem *i = (wxTreeListItem*) item.m_pItem;
    wxTreeListItem *parent = i->GetItemParent();
    if (!parent) return wxTreeItemId(); // root item doesn't have any siblings

    // get index
    wxArrayTreeListItems& siblings = parent->GetChildren();
    size_t index = siblings.Index(i);
    wxASSERT (index != (size_t)wxNOT_FOUND); // I'm not a child of my parent?
    return (index >= 1)? wxTreeItemId(siblings[index-1]): wxTreeItemId();
}

// Only for internal use right now, but should probably be public
wxTreeItemId wxTreeListMainWindow::GetNext (const wxTreeItemId& item, bool fulltree) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));

    // if there are any children, return first child
    if (fulltree || ((wxTreeListItem*)item.m_pItem)->IsExpanded()) {
        wxArrayTreeListItems& children = ((wxTreeListItem*)item.m_pItem)->GetChildren();
        if (children.GetCount() > 0) return children.Item (0);
    }

    // get sibling of this item or of the ancestors instead
    wxTreeItemId next;
    wxTreeItemId parent = item;
    do {
        next = GetNextSibling (parent);
        parent = GetItemParent (parent);
    } while (!next.IsOk() && parent.IsOk());
    return next;
}

// Only for internal use right now, but should probably be public
wxTreeItemId wxTreeListMainWindow::GetPrev (const wxTreeItemId& item, bool fulltree) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));

    // if there are any children, return last child
    if (fulltree || ((wxTreeListItem*)item.m_pItem)->IsExpanded()) {
        wxArrayTreeListItems& children = ((wxTreeListItem*)item.m_pItem)->GetChildren();
        if (children.GetCount() > 0) return children.Item (children.GetCount()-1);
    }

    // get sibling of this item or of the ancestors instead
    wxTreeItemId next;
    wxTreeItemId parent = item;
    do {
        next = GetPrevSibling (parent);
        parent = GetItemParent (parent);
    } while (!next.IsOk() && parent.IsOk());
    return next;
}

wxTreeItemId wxTreeListMainWindow::GetFirstExpandedItem() const {
    return GetNextExpanded (GetRootItem());
}

wxTreeItemId wxTreeListMainWindow::GetNextExpanded (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    return GetNext (item, false);
}

wxTreeItemId wxTreeListMainWindow::GetPrevExpanded (const wxTreeItemId& item) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    return GetPrev (item, false);
}

wxTreeItemId wxTreeListMainWindow::GetFirstVisibleItem (bool fullRow) const {
    return GetNextVisible (GetRootItem(), fullRow);
}

wxTreeItemId wxTreeListMainWindow::GetNextVisible (const wxTreeItemId& item, bool fullRow) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxTreeItemId id = GetNext (item, false);
    while (id.IsOk()) {
        if (IsVisible (id, fullRow)) return id;
        id = GetNext (id, false);
    }
    return wxTreeItemId();
}

wxTreeItemId wxTreeListMainWindow::GetPrevVisible (const wxTreeItemId& item, bool fullRow) const {
    wxCHECK_MSG (item.IsOk(), wxTreeItemId(), _T("invalid tree item"));
    wxTreeItemId id = GetPrev (item, true);
    while (id.IsOk()) {
        if (IsVisible (id, fullRow)) return id;
        id = GetPrev(id, true);
    }
    return wxTreeItemId();
}

// ----------------------------------------------------------------------------
// operations
// ----------------------------------------------------------------------------

wxTreeItemId wxTreeListMainWindow::DoInsertItem (const wxTreeItemId& parentId,
                                                 size_t previous,
                                                 const wxString& text,
                                                 int image, int selImage,
                                                 wxTreeItemData *data) {
    wxTreeListItem *parent = (wxTreeListItem*)parentId.m_pItem;
    wxCHECK_MSG (parent, wxTreeItemId(), _T("item must have a parent, at least root!") );
    m_dirty = true; // do this first so stuff below doesn't cause flicker

    wxArrayString arr;
    arr.Alloc (GetColumnCount());
    for (int i = 0; i < (int)GetColumnCount(); ++i) arr.Add (wxEmptyString);
    arr[m_main_column] = text;
    wxTreeListItem *item = new wxTreeListItem (this, parent, arr, image, selImage, data);
    if (data != NULL) {
#if !wxCHECK_VERSION(2, 5, 0)
        data->SetId ((long)item);
#else
        data->SetId (item);
#endif
    }
    parent->Insert (item, previous);

    return item;
}

wxTreeItemId wxTreeListMainWindow::AddRoot (const wxString& text,
                                            int image, int selImage,
                                            wxTreeItemData *data) {
    wxCHECK_MSG(!m_rootItem, wxTreeItemId(), _T("tree can have only one root"));
    wxCHECK_MSG(GetColumnCount(), wxTreeItemId(), _T("Add column(s) before adding the root item"));
    m_dirty = true; // do this first so stuff below doesn't cause flicker

    wxArrayString arr;
    arr.Alloc (GetColumnCount());
    for (int i = 0; i < (int)GetColumnCount(); ++i) arr.Add (wxEmptyString);
    arr[m_main_column] = text;
    m_rootItem = new wxTreeListItem (this, (wxTreeListItem *)NULL, arr, image, selImage, data);
    if (data != NULL) {
#if !wxCHECK_VERSION(2, 5, 0)
        data->SetId((long)m_rootItem);
#else
        data->SetId(m_rootItem);
#endif
    }
    if (HasFlag(wxTR_HIDE_ROOT)) {
        // if we will hide the root, make sure children are visible
        m_rootItem->SetHasPlus();
        m_rootItem->Expand();
#if !wxCHECK_VERSION(2, 5, 0)
        long cookie = 0;
#else
        wxTreeItemIdValue cookie = 0;
#endif
        m_curItem = (wxTreeListItem*)GetFirstChild (m_rootItem, cookie).m_pItem;
    }
    return m_rootItem;
}

wxTreeItemId wxTreeListMainWindow::PrependItem (const wxTreeItemId& parent,
                                                const wxString& text,
                                                int image, int selImage,
                                                wxTreeItemData *data) {
    return DoInsertItem (parent, 0u, text, image, selImage, data);
}

wxTreeItemId wxTreeListMainWindow::InsertItem (const wxTreeItemId& parentId,
                                               const wxTreeItemId& idPrevious,
                                               const wxString& text,
                                               int image, int selImage,
                                               wxTreeItemData *data) {
    wxTreeListItem *parent = (wxTreeListItem*)parentId.m_pItem;
    wxCHECK_MSG (parent, wxTreeItemId(), _T("item must have a parent, at least root!") );

    int index = parent->GetChildren().Index((wxTreeListItem*) idPrevious.m_pItem);
    wxASSERT_MSG( index != wxNOT_FOUND,
                  _T("previous item in wxTreeListMainWindow::InsertItem() is not a sibling") );

    return DoInsertItem (parentId, ++index, text, image, selImage, data);
}

wxTreeItemId wxTreeListMainWindow::InsertItem (const wxTreeItemId& parentId,
                                               size_t before,
                                               const wxString& text,
                                               int image, int selImage,
                                               wxTreeItemData *data) {
    wxTreeListItem *parent = (wxTreeListItem*)parentId.m_pItem;
    wxCHECK_MSG (parent, wxTreeItemId(), _T("item must have a parent, at least root!") );

    return DoInsertItem (parentId, before, text, image, selImage, data);
}

wxTreeItemId wxTreeListMainWindow::AppendItem (const wxTreeItemId& parentId,
                                               const wxString& text,
                                               int image, int selImage,
                                               wxTreeItemData *data) {
    wxTreeListItem *parent = (wxTreeListItem*) parentId.m_pItem;
    wxCHECK_MSG (parent, wxTreeItemId(), _T("item must have a parent, at least root!") );

    return DoInsertItem (parent, parent->GetChildren().Count(), text, image, selImage, data);
}

void wxTreeListMainWindow::SendDeleteEvent (wxTreeListItem *item) {
    // send event to user code
    wxTreeEvent event (wxEVT_COMMAND_TREE_DELETE_ITEM, m_owner->GetId());
#if !wxCHECK_VERSION(2, 5, 0)
    event.SetItem ((long)item);
#else
    event.SetItem (item);
#endif
    event.SetEventObject (m_owner);
    m_owner->GetEventHandler()->ProcessEvent (event);
}

void wxTreeListMainWindow::Delete (const wxTreeItemId& itemId) {
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;
    wxCHECK_RET (item != m_rootItem, _T("invalid item, root may not be deleted this way!"));
    m_dirty = true; // do this first so stuff below doesn't cause flicker

    // don't stay with invalid m_shiftItem or we will crash in the next call to OnChar()
    bool changeKeyCurrent = false;
    wxTreeListItem *itemKey = m_shiftItem;
    while (itemKey) {
        if (itemKey == item) { // m_shiftItem is a descendant of the item being deleted
            changeKeyCurrent = true;
            break;
        }
        itemKey = itemKey->GetItemParent();
    }

    wxTreeListItem *parent = item->GetItemParent();


    // m_select_me records whether we need to select
    // a different item, in idle time.
    if ( m_select_me && IsDescendantOf(item, m_select_me) )
    {
        m_select_me = parent;
    }

    if ( IsDescendantOf(item, m_curItem) )
    {
        // Don't silently change the selection:
        // do it properly in idle time, so event
        // handlers get called.

        // m_current = parent;
        m_curItem = NULL;
        m_select_me = parent;
    }
    
    // remove the item from the tree
    if (parent) {
        parent->GetChildren().Remove (item);  // remove by value
    }
    if (changeKeyCurrent)  m_shiftItem = parent;

    SendDeleteEvent (item);
    if (m_selectItem == item) m_selectItem = (wxTreeListItem*)NULL;
    item->DeleteChildren (this);

    if (item == m_select_me)
        m_select_me = NULL;
    
    delete item;
}

void wxTreeListMainWindow::DeleteChildren (const wxTreeItemId& itemId) {
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;
    m_dirty = true; // do this first so stuff below doesn't cause flicker

    item->DeleteChildren (this);
}

void wxTreeListMainWindow::DeleteRoot() {
    if (m_rootItem) {
        m_dirty = true;
        SendDeleteEvent (m_rootItem);
        m_curItem = (wxTreeListItem*)NULL;
        m_selectItem= (wxTreeListItem*)NULL;
        m_rootItem->DeleteChildren (this);
        delete m_rootItem;
        m_rootItem = NULL;
    }
}

void wxTreeListMainWindow::Expand (const wxTreeItemId& itemId) {
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;
    wxCHECK_RET (item, _T("invalid item in wxTreeListMainWindow::Expand") );

    if (!item->HasPlus() || item->IsExpanded()) return;

    // send event to user code
    wxTreeEvent event (wxEVT_COMMAND_TREE_ITEM_EXPANDING, m_owner->GetId());
#if !wxCHECK_VERSION(2, 5, 0)
    event.SetItem ((long)item);
#else
    event.SetItem (item);
#endif
    event.SetEventObject (m_owner);
    if (m_owner->GetEventHandler()->ProcessEvent (event) && !event.IsAllowed()) return; // expand canceled

    item->Expand();
    m_dirty = true;

    // send event to user code
    event.SetEventType (wxEVT_COMMAND_TREE_ITEM_EXPANDED);
    m_owner->GetEventHandler()->ProcessEvent (event);
}

void wxTreeListMainWindow::ExpandAll (const wxTreeItemId& itemId) {
    Expand (itemId);
    if (!IsExpanded (itemId)) return;
#if !wxCHECK_VERSION(2, 5, 0)
    long cookie;
#else
    wxTreeItemIdValue cookie;
#endif
    wxTreeItemId child = GetFirstChild (itemId, cookie);
    while (child.IsOk()) {
        ExpandAll (child);
        child = GetNextChild (itemId, cookie);
    }
}

void wxTreeListMainWindow::Collapse (const wxTreeItemId& itemId) {
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;
    wxCHECK_RET (item, _T("invalid item in wxTreeListMainWindow::Collapse") );

    if (!item->HasPlus() || !item->IsExpanded()) return;

    // send event to user code
    wxTreeEvent event (wxEVT_COMMAND_TREE_ITEM_COLLAPSING, m_owner->GetId() );
#if !wxCHECK_VERSION(2, 5, 0)
    event.SetItem ((long)item);
#else
    event.SetItem (item);
#endif
    event.SetEventObject (m_owner);
    if (m_owner->GetEventHandler()->ProcessEvent (event) && !event.IsAllowed()) return; // collapse canceled

    item->Collapse();
    m_dirty = true;

    // send event to user code
    event.SetEventType (wxEVT_COMMAND_TREE_ITEM_COLLAPSED);
    GetEventHandler()->ProcessEvent (event);
}

void wxTreeListMainWindow::CollapseAndReset (const wxTreeItemId& item) {
    Collapse (item);
    DeleteChildren (item);
}

void wxTreeListMainWindow::Toggle (const wxTreeItemId& itemId) {
    if (IsExpanded (itemId)) {
        Collapse (itemId);
    }else{
        Expand (itemId);
    }
}

void wxTreeListMainWindow::Unselect() {
    if (m_selectItem) {
        m_selectItem->SetHilight (false);
        RefreshLine (m_selectItem);
        m_selectItem = (wxTreeListItem*)NULL;
    }
}

void wxTreeListMainWindow::UnselectAllChildren (wxTreeListItem *item) {
    if (item->IsSelected()) {
        item->SetHilight (false);
        RefreshLine (item);
        if (item == m_selectItem) m_selectItem = (wxTreeListItem*)NULL;
    }
    if (item->HasChildren()) {
        wxArrayTreeListItems& children = item->GetChildren();
        size_t count = children.Count();
        for (size_t n = 0; n < count; ++n) {
            UnselectAllChildren (children[n]);
        }
    }
}

void wxTreeListMainWindow::UnselectAll() {
    UnselectAllChildren ((wxTreeListItem*)GetRootItem().m_pItem);
}

// Recursive function !
// To stop we must have crt_item<last_item
// Algorithm :
// Tag all next children, when no more children,
// Move to parent (not to tag)
// Keep going... if we found last_item, we stop.
bool wxTreeListMainWindow::TagNextChildren (wxTreeListItem *crt_item,
                                            wxTreeListItem *last_item) {
    wxTreeListItem *parent = crt_item->GetItemParent();

    if (!parent) {// This is root item
        return TagAllChildrenUntilLast (crt_item, last_item);
    }

    wxArrayTreeListItems& children = parent->GetChildren();
    int index = children.Index(crt_item);
    wxASSERT (index != wxNOT_FOUND); // I'm not a child of my parent?

    if ((parent->HasChildren() && parent->IsExpanded()) ||
        ((parent == (wxTreeListItem*)GetRootItem().m_pItem) && HasFlag(wxTR_HIDE_ROOT))) {
        size_t count = children.Count();
        for (size_t n = (index+1); n < count; ++n) {
            if (TagAllChildrenUntilLast (children[n], last_item)) return true;
        }
    }

    return TagNextChildren (parent, last_item);
}

bool wxTreeListMainWindow::TagAllChildrenUntilLast (wxTreeListItem *crt_item,
                                                    wxTreeListItem *last_item) {
    crt_item->SetHilight (true);
    RefreshLine(crt_item);

    if (crt_item==last_item) return true;

    if (crt_item->HasChildren() && crt_item->IsExpanded()) {
        wxArrayTreeListItems& children = crt_item->GetChildren();
        size_t count = children.Count();
        for (size_t n = 0; n < count; ++n) {
            if (TagAllChildrenUntilLast (children[n], last_item)) return true;
        }
    }

    return false;
}

void wxTreeListMainWindow::SelectItem (const wxTreeItemId& itemId,
                                       const wxTreeItemId& lastId,
                                       bool unselect_others) {
    wxCHECK_RET (itemId.IsOk(), _T("invalid tree item") );

    bool is_single = !HasFlag(wxTR_MULTIPLE);
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;

    // single selection requires unselect others
    if (is_single) unselect_others = true;

    // send event to the user code
    wxTreeEvent event( wxEVT_COMMAND_TREE_SEL_CHANGING, m_owner->GetId() );
#if !wxCHECK_VERSION(2, 5, 0)
    event.SetItem ((long)item);
    event.SetOldItem ((long)m_curItem);
#else
    event.SetItem (item);
    event.SetOldItem (m_curItem);
#endif
    event.SetEventObject (m_owner);
    if (m_owner->GetEventHandler()->ProcessEvent (event) && !event.IsAllowed()) return;

    // unselect all if unselect other items
    bool unselected = false; // see that UnselectAll is done only once
    if (unselect_others) {
        if (is_single) {
            Unselect(); // to speed up thing
        }else{
            UnselectAll();
            unselected = true;
        }
    }

    // select item or item range
    if (!is_single && lastId.IsOk() && (itemId != lastId)) {

        if (!unselected && unselect_others) UnselectAll();
        wxTreeListItem *last = (wxTreeListItem*) lastId.m_pItem;

        // ensure that the position of the item it calculated in any case
        if (m_dirty) CalculatePositions();

        // select item range according Y-position
        if (last->GetY() < item->GetY()) {
            if (!TagAllChildrenUntilLast (last, item)) {
                TagNextChildren (last, item);
            }
        }else{
            if (!TagAllChildrenUntilLast (item, last)) {
                TagNextChildren (item, last);
            }
        }

    }else{

        // select item according its old selection
        item->SetHilight (!item->IsSelected());
        RefreshLine (item);
        if (unselect_others) {
            m_selectItem = (item->IsSelected())? item: (wxTreeListItem*)NULL;
            m_curItem = m_selectItem;
        }
    }

    // send event to user code
    event.SetEventType(wxEVT_COMMAND_TREE_SEL_CHANGED);
    m_owner->GetEventHandler()->ProcessEvent (event);
}

void wxTreeListMainWindow::SelectAll() {
    wxCHECK_RET (HasFlag(wxTR_MULTIPLE), _T("invalid tree style, must have wxTR_MULTIPLE style to select all items"));

    // send event to user code
    wxTreeEvent event (wxEVT_COMMAND_TREE_SEL_CHANGING, m_owner->GetId());
    event.SetItem (GetRootItem());
#if !wxCHECK_VERSION(2, 5, 0)
    event.SetOldItem ((long)m_curItem);
#else
    event.SetOldItem (m_curItem);
#endif
    event.SetEventObject (m_owner);
    if (m_owner->GetEventHandler()->ProcessEvent (event) && !event.IsAllowed()) return;

#if !wxCHECK_VERSION(2, 5, 0)
    long cookie = 0;
#else
    wxTreeItemIdValue cookie = 0;
#endif
    wxTreeItemId root = GetRootItem();
    wxTreeListItem *first = (wxTreeListItem *)GetFirstChild (root, cookie).m_pItem;
    wxTreeListItem *last = (wxTreeListItem *)GetLastChild (root, cookie).m_pItem;
    if (!first || !last) return;
    if (!TagAllChildrenUntilLast (first, last)) {
        TagNextChildren (first, last);
    }

    // send event to user code
    event.SetEventType (wxEVT_COMMAND_TREE_SEL_CHANGED);
    m_owner->GetEventHandler()->ProcessEvent (event);
}

void wxTreeListMainWindow::FillArray (wxTreeListItem *item,
                                      wxArrayTreeItemIds &array) const {
    if (item->IsSelected()) array.Add (wxTreeItemId(item));

    if (item->HasChildren()) {
        wxArrayTreeListItems& children = item->GetChildren();
        size_t count = children.GetCount();
        for (size_t n = 0; n < count; ++n) FillArray (children[n], array);
    }
}

size_t wxTreeListMainWindow::GetSelections (wxArrayTreeItemIds &array) const {
    array.Empty();
    wxTreeItemId idRoot = GetRootItem();
    if (idRoot.IsOk()) FillArray ((wxTreeListItem*) idRoot.m_pItem, array);
    return array.Count();
}

void wxTreeListMainWindow::EnsureVisible (const wxTreeItemId& item) {
    if (!item.IsOk()) return; // do nothing if no item

    // first expand all parent branches
    wxTreeListItem *gitem = (wxTreeListItem*) item.m_pItem;
    wxTreeListItem *parent = gitem->GetItemParent();
    while (parent) {
        Expand (parent);
        parent = parent->GetItemParent();
    }

    ScrollTo (item);
    RefreshLine (gitem);
}

void wxTreeListMainWindow::ScrollTo (const wxTreeItemId &item) {
    if (!item.IsOk()) return; // do nothing if no item

    // ensure that the position of the item it calculated in any case
    if (m_dirty) CalculatePositions();

    wxTreeListItem *gitem = (wxTreeListItem*) item.m_pItem;

    // now scroll to the item
    int item_y = gitem->GetY();

    int xUnit, yUnit;
    GetScrollPixelsPerUnit (&xUnit, &yUnit);
    int start_x = 0;
    int start_y = 0;
    GetViewStart (&start_x, &start_y);
    start_y *= yUnit;

    int client_h = 0;
    int client_w = 0;
    GetClientSize (&client_w, &client_h);

    int x = 0;
    int y = 0;
    m_rootItem->GetSize (x, y, this);
    x = m_owner->GetHeaderWindow()->GetWidth();
    y += yUnit + 2; // one more scrollbar unit + 2 pixels
    int x_pos = GetScrollPos( wxHORIZONTAL );

    if (item_y < start_y+3) {
        // going down, item should appear at top
        SetScrollbars (xUnit, yUnit, xUnit ? x/xUnit : 0, yUnit ? y/yUnit : 0, x_pos, yUnit ? item_y/yUnit : 0);
    }else if (item_y+GetLineHeight(gitem) > start_y+client_h) {
        // going up, item should appear at bottom
        item_y += yUnit + 2;
        SetScrollbars (xUnit, yUnit, xUnit ? x/xUnit : 0, yUnit ? y/yUnit : 0, x_pos, yUnit ? (item_y+GetLineHeight(gitem)-client_h)/yUnit : 0 );
    }
}

// FIXME: tree sorting functions are not reentrant and not MT-safe!
static wxTreeListMainWindow *s_treeBeingSorted = NULL;

static int LINKAGEMODE tree_ctrl_compare_func(wxTreeListItem **item1,
                                  wxTreeListItem **item2)
{
    wxCHECK_MSG (s_treeBeingSorted, 0, _T("bug in wxTreeListMainWindow::SortChildren()") );

    return s_treeBeingSorted->OnCompareItems(*item1, *item2);
}

int wxTreeListMainWindow::OnCompareItems(const wxTreeItemId& item1,
                               const wxTreeItemId& item2)
{
    return m_owner->OnCompareItems (item1, item2);
}

void wxTreeListMainWindow::SortChildren (const wxTreeItemId& itemId) {
    wxCHECK_RET (itemId.IsOk(), _T("invalid tree item"));

    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;

    wxCHECK_RET (!s_treeBeingSorted,
                 _T("wxTreeListMainWindow::SortChildren is not reentrant") );

    wxArrayTreeListItems& children = item->GetChildren();
    if ( children.Count() > 1 ) {
        m_dirty = true;
        s_treeBeingSorted = this;
        children.Sort(tree_ctrl_compare_func);
        s_treeBeingSorted = NULL;
    }
}

wxTreeItemId wxTreeListMainWindow::FindItem (const wxTreeItemId& item, const wxString& str, int mode) {
    wxString itemText;
    // determine start item
    wxTreeItemId next = item;
    if (next.IsOk()) {
        if (mode & wxTL_MODE_NAV_LEVEL) {
            next = GetNextSibling (next);
        }else if (mode & wxTL_MODE_NAV_VISIBLE) { //
            next = GetNextVisible (next, false);
        }else if (mode & wxTL_MODE_NAV_EXPANDED) {
            next = GetNextExpanded (next);
        }else{ // (mode & wxTL_MODE_NAV_FULLTREE) default
            next = GetNext (next, true);
        }
    }

#if !wxCHECK_VERSION(2, 5, 0)
    long cookie = 0;
#else
    wxTreeItemIdValue cookie = 0;
#endif
    if (!next.IsOk()) {
        next = (wxTreeListItem*)GetRootItem().m_pItem;
        if (HasFlag(wxTR_HIDE_ROOT)) {
            next = (wxTreeListItem*)GetFirstChild (GetRootItem().m_pItem, cookie).m_pItem;
        }
    }
    if (!next.IsOk()) return (wxTreeItemId*)NULL;

    // start checking the next items
    while (next.IsOk() && (next != item)) {
        if (mode & wxTL_MODE_FIND_PARTIAL) {
            itemText = GetItemText (next).Mid (0, str.Length());
        }else{
            itemText = GetItemText (next);
        }
        if (mode & wxTL_MODE_FIND_NOCASE) {
            if (itemText.CmpNoCase (str) == 0) return next;
        }else{
            if (itemText.Cmp (str) == 0) return next;
        }
        if (mode & wxTL_MODE_NAV_LEVEL) {
            next = GetNextSibling (next);
        }else if (mode & wxTL_MODE_NAV_VISIBLE) { //
            next = GetNextVisible (next, false);
        }else if (mode & wxTL_MODE_NAV_EXPANDED) {
            next = GetNextExpanded (next);
        }else{ // (mode & wxTL_MODE_NAV_FULLTREE) default
            next = GetNext (next, true);
        }
        if (!next.IsOk() && item.IsOk()) {
            next = (wxTreeListItem*)GetRootItem().m_pItem;
            if (HasFlag(wxTR_HIDE_ROOT)) {
                next = (wxTreeListItem*)GetNextChild (GetRootItem().m_pItem, cookie).m_pItem;
            }
        }
    }
    return (wxTreeItemId*)NULL;
}

void wxTreeListMainWindow::SetDragItem (const wxTreeItemId& item) {
    wxTreeListItem *prevItem = m_dragItem;
    m_dragItem = (wxTreeListItem*) item.m_pItem;
    if (prevItem) RefreshLine (prevItem);
    if (m_dragItem) RefreshLine (m_dragItem);
}

void wxTreeListMainWindow::CalculateLineHeight() {
    wxClientDC dc (this);
    dc.SetFont (m_normalFont);
    m_lineHeight = (int)(dc.GetCharHeight() + m_linespacing);

    if (m_imageListNormal) {
        // Calculate a m_lineHeight value from the normal Image sizes.
        // May be toggle off. Then wxTreeListMainWindow will spread when
        // necessary (which might look ugly).
        int n = m_imageListNormal->GetImageCount();
        for (int i = 0; i < n ; i++) {
            int width = 0, height = 0;
            m_imageListNormal->GetSize(i, width, height);
            if (height > m_lineHeight) m_lineHeight = height + m_linespacing;
        }
    }

    if (m_imageListButtons) {
        // Calculate a m_lineHeight value from the Button image sizes.
        // May be toggle off. Then wxTreeListMainWindow will spread when
        // necessary (which might look ugly).
        int n = m_imageListButtons->GetImageCount();
        for (int i = 0; i < n ; i++) {
            int width = 0, height = 0;
            m_imageListButtons->GetSize(i, width, height);
            if (height > m_lineHeight) m_lineHeight = height + m_linespacing;
        }
    }

    if (m_lineHeight < 30) { // add 10% space if greater than 30 pixels
        m_lineHeight += 2; // minimal 2 pixel space
    }else{
        m_lineHeight += m_lineHeight / 10; // otherwise 10% space
    }
}

void wxTreeListMainWindow::SetImageList (wxImageList *imageList) {
    if (m_ownsImageListNormal) delete m_imageListNormal;
    m_imageListNormal = imageList;
    m_ownsImageListNormal = false;
    m_dirty = true;
    CalculateLineHeight();
}

void wxTreeListMainWindow::SetStateImageList (wxImageList *imageList) {
    if (m_ownsImageListState) delete m_imageListState;
    m_imageListState = imageList;
    m_ownsImageListState = false;
}

void wxTreeListMainWindow::SetButtonsImageList (wxImageList *imageList) {
    if (m_ownsImageListButtons) delete m_imageListButtons;
    m_imageListButtons = imageList;
    m_ownsImageListButtons = false;
    m_dirty = true;
    CalculateLineHeight();
}

void wxTreeListMainWindow::AssignImageList (wxImageList *imageList) {
    SetImageList(imageList);
    m_ownsImageListNormal = true;
}

void wxTreeListMainWindow::AssignStateImageList (wxImageList *imageList) {
    SetStateImageList(imageList);
    m_ownsImageListState = true;
}

void wxTreeListMainWindow::AssignButtonsImageList (wxImageList *imageList) {
    SetButtonsImageList(imageList);
    m_ownsImageListButtons = true;
}

// ----------------------------------------------------------------------------
// helpers
// ----------------------------------------------------------------------------

void wxTreeListMainWindow::AdjustMyScrollbars() {
    if (m_rootItem) {
        int xUnit, yUnit;
        GetScrollPixelsPerUnit (&xUnit, &yUnit);
        if (xUnit == 0) xUnit = GetCharWidth();
        if (yUnit == 0) yUnit = m_lineHeight;
        int x = 0, y = 0;
        m_rootItem->GetSize (x, y, this);
        y += yUnit + 2; // one more scrollbar unit + 2 pixels
        int x_pos = GetScrollPos (wxHORIZONTAL);
        int y_pos = GetScrollPos (wxVERTICAL);
        x = m_owner->GetHeaderWindow()->GetWidth() + 2;
        if (x < GetClientSize().GetWidth()) x_pos = 0;
        SetScrollbars (xUnit, yUnit, x/xUnit, y/yUnit, x_pos, y_pos);
    }else{
        SetScrollbars (0, 0, 0, 0);
    }
}

int wxTreeListMainWindow::GetLineHeight (wxTreeListItem *item) const {
    if (GetWindowStyleFlag() & wxTR_HAS_VARIABLE_ROW_HEIGHT) {
        return item->GetHeight();
    }else{
        return m_lineHeight;
    }
}

void wxTreeListMainWindow::PaintItem (wxTreeListItem *item, wxDC& dc) {

    wxTreeItemAttr *attr = item->GetAttributes();

    dc.SetFont (GetItemFont (item));

    wxColour colText;
    if (attr && attr->HasTextColour()) {
        colText = attr->GetTextColour();
    }else{
        colText = GetForegroundColour();
    }
#if !wxCHECK_VERSION(2, 5, 0)
    wxColour colTextHilight = wxSystemSettings::GetSystemColour (wxSYS_COLOUR_HIGHLIGHTTEXT);
#else
    wxColour colTextHilight = wxSystemSettings::GetColour (wxSYS_COLOUR_HIGHLIGHTTEXT);
#endif

    int total_w = m_owner->GetHeaderWindow()->GetWidth();
    int total_h = GetLineHeight(item);
    int off_h = HasFlag(wxTR_ROW_LINES) ? 1 : 0;
    int off_w = HasFlag(wxTR_COLUMN_LINES) ? 1 : 0;
    wxDCClipper clipper (dc, 0, item->GetY(), total_w, total_h); // only within line

    int text_w = 0, text_h = 0;
    dc.GetTextExtent( item->GetText(GetMainColumn()), &text_w, &text_h );

    // determine background and show it
    wxColour colBg;
    if (attr && attr->HasBackgroundColour()) {
        colBg = attr->GetBackgroundColour();
    }else{
        colBg = m_backgroundColour;
    }
    dc.SetBrush (wxBrush (colBg, wxSOLID));
    dc.SetPen (*wxTRANSPARENT_PEN);
    if (HasFlag (wxTR_FULL_ROW_HIGHLIGHT)) {
        if (item == m_dragItem) {
            dc.SetBrush (*m_hilightBrush);
#ifndef __WXMAC__ // don't draw rect outline if we already have the background color
            dc.SetPen ((item == m_dragItem)? *wxBLACK_PEN: *wxTRANSPARENT_PEN);
#endif // !__WXMAC__
            dc.SetTextForeground (colTextHilight);
        }else if (item->IsSelected()) {
#if defined(__WXGTK2__) || defined(__WXMAC__)
        int flags = wxCONTROL_SELECTED;
        if (m_hasFocus)
        {
            flags |= wxCONTROL_FOCUSED;
#ifdef __WXMAC__
            dc.SetTextForeground( *wxWHITE );
#endif
        }
        wxRendererNative::GetDefault().DrawItemSelectionRect( m_owner, dc, wxRect( 0, item->GetY() + off_h, total_w, total_h - off_h ), flags); 
#else
            if (!m_isDragging && m_hasFocus) {
                dc.SetBrush (*m_hilightBrush);
                dc.SetPen (*wxBLACK_PEN);
            }else{
                dc.SetBrush (*m_hilightUnfocusedBrush);
                dc.SetPen (*wxTRANSPARENT_PEN);
            }
            dc.SetTextForeground (colTextHilight);
#endif // defined(__WXGTK2__) || defined(__WXMAC__)
        }else if (item == m_curItem) {
            dc.SetPen (m_hasFocus? *wxBLACK_PEN: *wxTRANSPARENT_PEN);
        }else{
            dc.SetTextForeground (colText);
        }
#if !defined(__WXGTK2__) && !defined(__WXMAC__)
        dc.DrawRectangle (0, item->GetY() + off_h, total_w, total_h - off_h);
#endif 
    }else{
        dc.SetTextForeground (colText);
    }

    int text_extraH = (total_h > text_h) ? (total_h - text_h)/2 : 0;
    int img_extraH = (total_h > m_imgHeight)? (total_h-m_imgHeight)/2: 0;
    int x_colstart = 0;
    for (int i = 0; i < GetColumnCount(); ++i ) {
        if (!m_owner->GetHeaderWindow()->IsColumnShown(i)) continue;

        int col_w = m_owner->GetHeaderWindow()->GetColumnWidth(i);
        wxDCClipper clipper (dc, x_colstart, item->GetY(), col_w, total_h); // only within column

        int x = 0;
        int image = NO_IMAGE;
        int image_w = 0;
        if(i == GetMainColumn()) {
            x = item->GetX() + MARGIN;
            if (HasButtons()) {
                x += (m_btnWidth-m_btnWidth2) + LINEATROOT;
            }else{
                x -= m_indent/2;
            }
            if (m_imageListNormal) image = item->GetCurrentImage();
        }else{
            x = x_colstart + MARGIN;
            image = item->GetImage(i);
        }
        if (image != NO_IMAGE) image_w = m_imgWidth + MARGIN;

        // honor text alignment
        wxString text = item->GetText(i);
        int w = 0;
        switch ( m_owner->GetHeaderWindow()->GetColumn(i).GetAlignment() ) {
        case wxALIGN_LEFT:
            // nothing to do, already left aligned
            break;
        case wxALIGN_RIGHT:
            dc.GetTextExtent (text, &text_w, NULL);
            w = col_w - (image_w + text_w + off_w + MARGIN);
            x += (w > 0)? w: 0;
            break;
        case wxALIGN_CENTER:
            dc.GetTextExtent(text, &text_w, NULL);
            w = (col_w - (image_w + text_w + off_w + MARGIN))/2;
            x += (w > 0)? w: 0;
            break;
        }
        int text_x = x + image_w;
        if (i == GetMainColumn()) item->SetTextX (text_x);

        if (!HasFlag (wxTR_FULL_ROW_HIGHLIGHT)) {
            if (i == GetMainColumn()) {
                if (item == m_dragItem) {
                    dc.SetBrush (*m_hilightBrush);
#ifndef __WXMAC__ // don't draw rect outline if we already have the background color
                    dc.SetPen ((item == m_dragItem)? *wxBLACK_PEN: *wxTRANSPARENT_PEN);
#endif // !__WXMAC__
                    dc.SetTextForeground (colTextHilight);
                }else if (item->IsSelected()) {
#if defined(__WXGTK2__) || defined(__WXMAC__)
                    int flags = wxCONTROL_SELECTED;
                    if (m_hasFocus)
                    {
                        flags |= wxCONTROL_FOCUSED;
#ifdef __WXMAC__
                        dc.SetTextForeground( *wxWHITE );
#endif
                    }
                    wxRendererNative::GetDefault().DrawItemSelectionRect( m_owner, dc, wxRect( 0, item->GetY() + off_h, total_w, total_h - off_h ), flags); 
#else 
                    if (!m_isDragging && m_hasFocus) {
                        dc.SetBrush (*m_hilightBrush);
                        dc.SetPen (*wxBLACK_PEN);
                    }else{
                        dc.SetBrush (*m_hilightUnfocusedBrush);
                        dc.SetPen (*wxTRANSPARENT_PEN);
                    }
                    dc.SetTextForeground (colTextHilight);
#endif // defined(__WXGTK2__) || defined(__WXMAC__)
                }else if (item == m_curItem) {
                    dc.SetPen (m_hasFocus? *wxBLACK_PEN: *wxTRANSPARENT_PEN);
                }else{
                    dc.SetTextForeground (colText);
                }
#if !defined(__WXGTK2__) && !defined(__WXMAC__)
                dc.DrawRectangle (text_x, item->GetY() + off_h, text_w, total_h - off_h);
#endif
            }else{
                dc.SetTextForeground (colText);
            }
        }

        if (HasFlag(wxTR_COLUMN_LINES)) { // vertical lines between columns
#if !wxCHECK_VERSION(2, 5, 0)
            wxPen pen (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_3DLIGHT ), 1, wxSOLID);
#else
            wxPen pen (wxSystemSettings::GetColour (wxSYS_COLOUR_3DLIGHT ), 1, wxSOLID);
#endif
            dc.SetPen ((GetBackgroundColour() == *wxWHITE)? pen: *wxWHITE_PEN);
            dc.DrawLine (x_colstart+col_w-1, item->GetY(), x_colstart+col_w-1, item->GetY()+total_h);
        }

        dc.SetBackgroundMode (wxTRANSPARENT);

        if (image != NO_IMAGE) {
            int y = item->GetY() + img_extraH;
            m_imageListNormal->Draw (image, dc, x, y, wxIMAGELIST_DRAW_TRANSPARENT );
        }
        int text_y = item->GetY() + text_extraH;
        dc.DrawText (text, (wxCoord)text_x, (wxCoord)text_y);

        x_colstart += col_w;
    }

    // restore normal font
    dc.SetFont( m_normalFont );
}

// Now y stands for the top of the item, whereas it used to stand for middle !
void wxTreeListMainWindow::PaintLevel (wxTreeListItem *item, wxDC &dc,
                                       int level, int &y, int x_maincol) {

    // Handle hide root (only level 0)
    if (HasFlag(wxTR_HIDE_ROOT) && (level == 0)) {
        wxArrayTreeListItems& children = item->GetChildren();
        for (size_t n = 0; n < children.Count(); n++) {
            PaintLevel (children[n], dc, 1, y, x_maincol);
        }
        // end after expanding root
        return;
    }

    // calculate position of vertical lines
    int x = x_maincol + MARGIN; // start of column
    if (HasFlag(wxTR_LINES_AT_ROOT)) x += LINEATROOT; // space for lines at root
    if (HasButtons()) {
        x += (m_btnWidth-m_btnWidth2); // half button space
    }else{
        x += (m_indent-m_indent/2);
    }
    if (HasFlag(wxTR_HIDE_ROOT)) {
        x += m_indent * (level-1); // indent but not level 1
    }else{
        x += m_indent * level; // indent according to level
    }

    // set position of vertical line
    item->SetX (x);
    item->SetY (y);

    int h = GetLineHeight (item);
    int y_top = y;
    int y_mid = y_top + (h/2);
    y += h;

    int exposed_x = dc.LogicalToDeviceX(0);
    int exposed_y = dc.LogicalToDeviceY(y_top);

    if (IsExposed(exposed_x, exposed_y, 10000, h)) { // 10000 = very much

        if (HasFlag(wxTR_ROW_LINES)) { // horizontal lines between rows
            //dc.DestroyClippingRegion();
            int total_width = m_owner->GetHeaderWindow()->GetWidth();
            // if the background colour is white, choose a
            // contrasting color for the lines
#if !wxCHECK_VERSION(2, 5, 0)
            wxPen pen (wxSystemSettings::GetSystemColour (wxSYS_COLOUR_3DLIGHT ), 1, wxSOLID);
#else
            wxPen pen (wxSystemSettings::GetColour (wxSYS_COLOUR_3DLIGHT ), 1, wxSOLID);
#endif
            dc.SetPen ((GetBackgroundColour() == *wxWHITE)? pen: *wxWHITE_PEN);
            dc.DrawLine (0, y_top, total_width, y_top);
            dc.DrawLine (0, y_top+h, total_width, y_top+h);
        }

        // draw item
        PaintItem (item, dc);

        // restore DC objects
        dc.SetBrush(*wxWHITE_BRUSH);
        dc.SetPen(m_dottedPen);

        // clip to the column width
        int clip_width = m_owner->GetHeaderWindow()->
                            GetColumn(m_main_column).GetWidth();
        wxDCClipper clipper(dc, x_maincol, y_top, clip_width, 10000);

        if (!HasFlag(wxTR_NO_LINES)) { // connection lines

            // draw the horizontal line here
            dc.SetPen(m_dottedPen);
            int x2 = x - m_indent;
            if (x2 < (x_maincol + MARGIN)) x2 = x_maincol + MARGIN;
            int x3 = x + (m_btnWidth-m_btnWidth2);
            if (HasButtons()) {
                if (item->HasPlus()) {
                    dc.DrawLine (x2, y_mid, x - m_btnWidth2, y_mid);
                    dc.DrawLine (x3, y_mid, x3 + LINEATROOT, y_mid);
                }else{
                    dc.DrawLine (x2, y_mid, x3 + LINEATROOT, y_mid);
                }
            }else{
                dc.DrawLine (x2, y_mid, x - m_indent/2, y_mid);
            }
        }

        if (item->HasPlus() && HasButtons()) { // should the item show a button?

            if (m_imageListButtons) {

                // draw the image button here
                int image = wxTreeItemIcon_Normal;
                if (item->IsExpanded()) image = wxTreeItemIcon_Expanded;
                if (item->IsSelected()) image += wxTreeItemIcon_Selected - wxTreeItemIcon_Normal;
                int xx = x - m_btnWidth2 + MARGIN;
                int yy = y_mid - m_btnHeight2;
                dc.SetClippingRegion(xx, yy, m_btnWidth, m_btnHeight);
                m_imageListButtons->Draw (image, dc, xx, yy, wxIMAGELIST_DRAW_TRANSPARENT);
                dc.DestroyClippingRegion();

            }else if (HasFlag (wxTR_TWIST_BUTTONS)) {

                // draw the twisty button here
                dc.SetPen(*wxBLACK_PEN);
                dc.SetBrush(*m_hilightBrush);
                wxPoint button[3];
                if (item->IsExpanded()) {
                    button[0].x = x - (m_btnWidth2+1);
                    button[0].y = y_mid - (m_btnHeight/3);
                    button[1].x = x + (m_btnWidth2+1);
                    button[1].y = button[0].y;
                    button[2].x = x;
                    button[2].y = button[0].y + (m_btnHeight2+1);
                }else{
                    button[0].x = x - (m_btnWidth/3);
                    button[0].y = y_mid - (m_btnHeight2+1);
                    button[1].x = button[0].x;
                    button[1].y = y_mid + (m_btnHeight2+1);
                    button[2].x = button[0].x + (m_btnWidth2+1);
                    button[2].y = y_mid;
                }
                dc.DrawPolygon(3, button);

            }else{ // if (HasFlag(wxTR_HAS_BUTTONS))

                // draw the plus sign here
#if !wxCHECK_VERSION(2, 7, 0)
                dc.SetPen(*wxGREY_PEN);
                dc.SetBrush(*wxWHITE_BRUSH);
                dc.DrawRectangle (x-m_btnWidth2, y_mid-m_btnHeight2, m_btnWidth, m_btnHeight);
                dc.SetPen(*wxBLACK_PEN);
                dc.DrawLine (x-(m_btnWidth2-2), y_mid, x+(m_btnWidth2-1), y_mid);
                if (!item->IsExpanded()) { // change "-" to "+"
                    dc.DrawLine (x, y_mid-(m_btnHeight2-2), x, y_mid+(m_btnHeight2-1));
                }
#else
                wxRect rect (x-m_btnWidth2, y_mid-m_btnHeight2, m_btnWidth, m_btnHeight);
                int flag = item->IsExpanded()? wxCONTROL_EXPANDED: 0;
                wxRendererNative::GetDefault().DrawTreeItemButton (this, dc, rect, flag);
#endif

            }

        }

    }

    // restore DC objects
    dc.SetBrush(*wxWHITE_BRUSH);
    dc.SetPen(m_dottedPen);
    dc.SetTextForeground(*wxBLACK);

    if (item->IsExpanded())
    {
        wxArrayTreeListItems& children = item->GetChildren();

        // clip to the column width
        int clip_width = m_owner->GetHeaderWindow()->
                            GetColumn(m_main_column).GetWidth();

        // process lower levels
        int oldY;
        if (m_imgWidth > 0) {
            oldY = y_mid + m_imgHeight2;
        }else{
            oldY = y_mid + h/2;
        }
        int y2;
        for (size_t n = 0; n < children.Count(); ++n) {

            y2 = y + h/2;
            PaintLevel (children[n], dc, level+1, y, x_maincol);

            // draw vertical line
            wxDCClipper clipper(dc, x_maincol, y_top, clip_width, 10000);
            if (!HasFlag (wxTR_NO_LINES)) {
                x = item->GetX();
                dc.DrawLine (x, oldY, x, y2);
                oldY = y2;
            }
        }
    }
}


// ----------------------------------------------------------------------------
// wxWindows callbacks
// ----------------------------------------------------------------------------

void wxTreeListMainWindow::OnPaint (wxPaintEvent &WXUNUSED(event)) {

    wxPaintDC dc (this);
    PrepareDC (dc);

    if (!m_rootItem || (GetColumnCount() <= 0)) return;

    // calculate button size
    if (m_imageListButtons) {
        m_imageListButtons->GetSize (0, m_btnWidth, m_btnHeight);
    }else if (HasButtons()) {
        m_btnWidth = BTNWIDTH;
        m_btnHeight = BTNHEIGHT;
    }
    m_btnWidth2 = m_btnWidth/2;
    m_btnHeight2 = m_btnHeight/2;

    // calculate image size
    if (m_imageListNormal) {
        m_imageListNormal->GetSize (0, m_imgWidth, m_imgHeight);
    }
    m_imgWidth2 = m_imgWidth/2;
    m_imgHeight2 = m_imgHeight/2;

    // calculate indent size
    if (m_imageListButtons) {
        m_indent = wxMax (MININDENT, m_btnWidth + MARGIN);
    }else if (HasButtons()) {
        m_indent = wxMax (MININDENT, m_btnWidth + LINEATROOT);
    }

    // set default values
    dc.SetFont( m_normalFont );
    dc.SetPen( m_dottedPen );

    // calculate column start and paint
    int x_maincol = 0;
    int i = 0;
    for (i = 0; i < (int)GetMainColumn(); ++i) {
        if (!m_owner->GetHeaderWindow()->IsColumnShown(i)) continue;
        x_maincol += m_owner->GetHeaderWindow()->GetColumnWidth (i);
    }
    int y = 0;
    PaintLevel (m_rootItem, dc, 0, y, x_maincol);
}

void wxTreeListMainWindow::OnSetFocus (wxFocusEvent &event) {

    m_hasFocus = true;
    RefreshSelected();
    if (m_curItem) RefreshLine (m_curItem);
    event.Skip();
}

void wxTreeListMainWindow::OnKillFocus( wxFocusEvent &event )
{
    m_hasFocus = false;
    RefreshSelected();
    if (m_curItem) RefreshLine (m_curItem);
    event.Skip();
}

void wxTreeListMainWindow::OnChar (wxKeyEvent &event) {
    // send event to user code
    wxTreeEvent nevent (wxEVT_COMMAND_TREE_KEY_DOWN, m_owner->GetId());
    nevent.SetKeyEvent (event);
    nevent.SetEventObject (m_owner);
    if (m_owner->GetEventHandler()->ProcessEvent (nevent)) return; // handled in user code

    // determine first current if none
    bool curItemSet = false;
    if (!m_curItem) {
        m_curItem = (wxTreeListItem*)GetRootItem().m_pItem;
        if (HasFlag(wxTR_HIDE_ROOT)) {
#if !wxCHECK_VERSION(2, 5, 0)
            long cookie = 0;
#else
            wxTreeItemIdValue cookie = 0;
#endif
            m_curItem = (wxTreeListItem*)GetFirstChild (m_curItem, cookie).m_pItem;
        }
        curItemSet = true;
    }
    if (!m_curItem) return; // do nothing if empty tree

    // remember item at shift down
    if (HasFlag(wxTR_MULTIPLE) && event.ShiftDown()) {
        if (!m_shiftItem) m_shiftItem = m_curItem;
    }else{
        m_shiftItem = (wxTreeListItem*)NULL;
    }

    // process all cases
    wxTreeItemId newItem = (wxTreeItemId*)NULL;
    switch (event.GetKeyCode()) {

        // '+': Expand subtree
        case '+':
        case WXK_ADD: {
            if (m_curItem->HasPlus() && !IsExpanded (m_curItem)) Expand (m_curItem);
        }break;

        // '-': collapse subtree
        case '-':
        case WXK_SUBTRACT: {
            if (m_curItem->HasPlus() && IsExpanded (m_curItem)) Collapse (m_curItem);
        }break;

        // '*': expand/collapse all subtrees // TODO: Mak it more useful
        case '*':
        case WXK_MULTIPLY: {
            if (m_curItem->HasPlus() && !IsExpanded (m_curItem)) {
                ExpandAll (m_curItem);
            }else if (m_curItem->HasPlus()) {
                Collapse (m_curItem); // TODO: CollapseAll
            }
        }break;

        // ' ': toggle current item
        case ' ': {
            SelectItem (m_curItem, (wxTreeListItem*)NULL, false);
        }break;

        // <RETURN>: activate current item
        case WXK_RETURN: {
            wxTreeEvent aevent (wxEVT_COMMAND_TREE_ITEM_ACTIVATED, m_owner->GetId());
#if !wxCHECK_VERSION(2, 5, 0)
            aevent.SetItem ((long)m_curItem);
#else
            aevent.SetItem (m_curItem);
#endif
            aevent.SetEventObject (m_owner);
            m_owner->GetEventHandler()->ProcessEvent (aevent);
        }break;

        // <BKSP>: go to the parent without collapsing
        case WXK_BACK: {
            newItem = GetItemParent (m_curItem);
            if ((newItem == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) {
                newItem = GetPrevSibling (m_curItem); // get sibling instead of root
            }
        }break;

        // <UP>: go to the previous sibling or to the last of its children, to the parent
        case WXK_UP: {
            newItem = GetPrevSibling (m_curItem);
            if (newItem) {
#if !wxCHECK_VERSION(2, 5, 0)
                long cookie = 0;
#else
                wxTreeItemIdValue cookie = 0;
#endif
                while (IsExpanded (newItem) && HasChildren (newItem)) {
                    newItem = GetLastChild (newItem, cookie);
                }
            }else {
                newItem = GetItemParent (m_curItem);
                if ((newItem == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) {
                    newItem = (wxTreeItemId*)NULL; // don't go to root if it is hidden
                }
            }
        }break;

        // <LEFT>: if expanded collapse subtree, else go to the parent
        case WXK_LEFT: {
            if (IsExpanded (m_curItem)) {
                Collapse (m_curItem);
            }else{
                newItem = GetItemParent (m_curItem);
                if ((newItem == GetRootItem()) && HasFlag(wxTR_HIDE_ROOT)) {
                    newItem = GetPrevSibling (m_curItem); // go to sibling if it is hidden
                }
            }
        }break;

        // <RIGHT>: if possible expand subtree, else go go to the first child
        case WXK_RIGHT: {
            if (m_curItem->HasPlus() && !IsExpanded (m_curItem)) {
                Expand (m_curItem);
            }else{
                if (IsExpanded (m_curItem) && HasChildren (m_curItem)) {
#if !wxCHECK_VERSION(2, 5, 0)
                    long cookie = 0;
#else
                    wxTreeItemIdValue cookie = 0;
#endif
                    newItem = GetFirstChild (m_curItem, cookie);
                }
            }
        }break;

        // <DOWN>: if expanded go to the first child, else to the next sibling, ect
        case WXK_DOWN: {
            if (curItemSet) {
                newItem = m_curItem;
            }else{
                if (IsExpanded (m_curItem) && HasChildren (m_curItem)) {
#if !wxCHECK_VERSION(2, 5, 0)
                    long cookie = 0;
#else
                    wxTreeItemIdValue cookie = 0;
#endif
                    newItem = GetFirstChild( m_curItem, cookie );
                }
                if (!newItem) {
                    wxTreeItemId parent = m_curItem;
                    do {
                        newItem = GetNextSibling (parent);
                        parent = GetItemParent (parent);
                    } while (!newItem && parent);
                }
            }
        }break;

        // <END>: go to last item of the root
        case WXK_END: {
#if !wxCHECK_VERSION(2, 5, 0)
            long cookie = 0;
#else
            wxTreeItemIdValue cookie = 0;
#endif
            newItem = GetLastChild (GetRootItem(), cookie);
        }break;

        // <HOME>: go to root
        case WXK_HOME: {
            newItem = GetRootItem();
            if (HasFlag(wxTR_HIDE_ROOT)) {
#if !wxCHECK_VERSION(2, 5, 0)
                long cookie = 0;
#else
                wxTreeItemIdValue cookie = 0;
#endif
                newItem = GetFirstChild (newItem, cookie);
            }
        }break;

        // any char: go to the next matching string
        default:
            if (event.GetKeyCode() >= (int)' ') {
                if (!m_findTimer->IsRunning()) m_findStr.Clear();
                m_findStr.Append ((char)event.GetKeyCode());
                m_findTimer->Start (FIND_TIMER_TICKS, wxTIMER_ONE_SHOT);
                wxTreeItemId prev = m_curItem? (wxTreeItemId*)m_curItem: (wxTreeItemId*)NULL;
                while (true) {
                    newItem = FindItem (prev, m_findStr, wxTL_MODE_NAV_EXPANDED |
                                                         wxTL_MODE_FIND_PARTIAL |
                                                         wxTL_MODE_FIND_NOCASE);
                    if (newItem || (m_findStr.Length() <= 1)) break;
                    m_findStr.RemoveLast();
                };
            }
            event.Skip();

    }

    // select and show the new item
    if (newItem) {
        if (!event.ControlDown()) {
            bool unselect_others = !((event.ShiftDown() || event.ControlDown()) &&
                                      HasFlag(wxTR_MULTIPLE));
            SelectItem (newItem, m_shiftItem, unselect_others);
        }
        EnsureVisible (newItem);
        wxTreeListItem *oldItem = m_curItem;
        m_curItem = (wxTreeListItem*)newItem.m_pItem; // make the new item the current item
        if (oldItem) RefreshLine (oldItem);
    }
}

wxTreeItemId wxTreeListMainWindow::HitTest (const wxPoint& point, int& flags, int& column) {

    int w, h;
    GetSize(&w, &h);
    flags=0;
    column = -1;
    if (point.x<0) flags |= wxTREE_HITTEST_TOLEFT;
    if (point.x>w) flags |= wxTREE_HITTEST_TORIGHT;
    if (point.y<0) flags |= wxTREE_HITTEST_ABOVE;
    if (point.y>h) flags |= wxTREE_HITTEST_BELOW;
    if (flags) return wxTreeItemId();

    if (!m_rootItem) {
        flags = wxTREE_HITTEST_NOWHERE;
        column = -1;
        return wxTreeItemId();
    }

    wxTreeListItem *hit = m_rootItem->HitTest (CalcUnscrolledPosition(point),
                                               this, flags, column, 0);
    if (!hit) {
        flags = wxTREE_HITTEST_NOWHERE;
        column = -1;
        return wxTreeItemId();
    }
    return hit;
}

// get the bounding rectangle of the item (or of its label only)
bool wxTreeListMainWindow::GetBoundingRect (const wxTreeItemId& itemId, wxRect& rect,
                                            bool WXUNUSED(textOnly)) const {
    wxCHECK_MSG (itemId.IsOk(), false, _T("invalid item in wxTreeListMainWindow::GetBoundingRect") );

    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;

    int xUnit, yUnit;
    GetScrollPixelsPerUnit (&xUnit, &yUnit);
    int startX, startY;
    GetViewStart(& startX, & startY);

    rect.x = item->GetX() - startX * xUnit;
    rect.y = item->GetY() - startY * yUnit;
    rect.width = item->GetWidth();
    rect.height = GetLineHeight (item);

    return true;
}

/* **** */

void wxTreeListMainWindow::EditLabel (const wxTreeItemId& item, int column) {
    if (!item.IsOk()) return;
    if (!((column >= 0) && (column < GetColumnCount()))) return;

    m_editItem = (wxTreeListItem*) item.m_pItem;

    wxTreeEvent te( wxEVT_COMMAND_TREE_BEGIN_LABEL_EDIT, m_owner->GetId() );
#if !wxCHECK_VERSION(2, 5, 0)
    te.SetItem ((long)m_editItem);
#else
    te.SetItem (m_editItem);
#endif
    te.SetInt (column);
    te.SetEventObject (m_owner );
    m_owner->GetEventHandler()->ProcessEvent (te);

    if (!te.IsAllowed()) return;

    // ensure that the position of the item it calculated in any case
    if (m_dirty) CalculatePositions();

    wxTreeListHeaderWindow* header_win = m_owner->GetHeaderWindow();
    int x = 0;
    int y = m_editItem->GetY() + 1; // wxTextCtrl needs 1 pixels above the text
    int w = 0;
    int h = m_editItem->GetHeight();
    long style = 0;
    if (column == GetMainColumn()) {
        x += m_editItem->GetTextX() - 2; // wxTextCtrl needs 2 pixels before the text
        w = wxMin (m_editItem->GetWidth(), m_owner->GetHeaderWindow()->GetWidth() - x);
    }else{
        for (int i = 0; i < column; ++i) x += header_win->GetColumnWidth (i); // start of column
        switch (header_win->GetColumnAlignment (column)) {
            case wxALIGN_LEFT: {style = wxTE_LEFT; break;}
            case wxALIGN_RIGHT: {style = wxTE_RIGHT; break;}
            case wxALIGN_CENTER: {style = wxTE_CENTER; break;}
        }
        w = header_win->GetColumnWidth (column); // width of column
    }

    wxClientDC dc (this);
    PrepareDC (dc);
    x = dc.LogicalToDeviceX (x);
    y = dc.LogicalToDeviceY (y);

    wxEditTextCtrl *text = new wxEditTextCtrl (this, -1, &m_renameAccept, &m_renameRes,
                                               this, m_editItem->GetText (column),
                                               wxPoint (x, y), wxSize (w, h), style);
    text->SetFocus();
}

void wxTreeListMainWindow::OnRenameTimer() {
    EditLabel (m_curItem, m_curColumn);
}

void wxTreeListMainWindow::OnRenameAccept() {

    // TODO if the validator fails this causes a crash
    wxTreeEvent le( wxEVT_COMMAND_TREE_END_LABEL_EDIT, m_owner->GetId() );
#if !wxCHECK_VERSION(2, 5, 0)
    le.SetItem((long)m_editItem);
#else
    le.SetItem(m_editItem);
#endif
    le.SetEventObject( /*this*/m_owner );
    le.SetLabel( m_renameRes );
    m_owner->GetEventHandler()->ProcessEvent( le );

    if (!le.IsAllowed()) return;

    SetItemText (m_editItem, m_curColumn, m_renameRes);
}

void wxTreeListMainWindow::OnMouse (wxMouseEvent &event) {
    if (!m_rootItem) return;

    // we process left mouse up event (enables in-place edit), right down
    // (pass to the user code), left dbl click (activate item) and
    // dragging/moving events for items drag-and-drop
    if (!(event.LeftDown() ||
          event.LeftUp() ||
          event.RightDown() ||
          event.RightUp() ||
          event.LeftDClick() ||
          event.Dragging() ||
          (event.GetWheelRotation() != 0 )/*? TODO ||
          event.Moving()?*/)) {
        m_owner->GetEventHandler()->ProcessEvent (event);
        return;
    }

    // set focus if window clicked
    if (event.LeftDown() || event.RightDown()) SetFocus();

    // determine event
    wxPoint p = wxPoint (event.GetX(), event.GetY());
    int flags = 0;
    wxTreeListItem *item = m_rootItem->HitTest (CalcUnscrolledPosition (p),
                                                this, flags, m_curColumn, 0);

    // we only process dragging here
    if (event.Dragging() && m_should_return) { 
        if (m_isDragging) return; // nothing to do, already done
        if (item == NULL) return; // we need an item to dragging
        else{
            SetCurrentItem( item );
        }

        // determine drag start
        if (m_dragCount == 0) {
            m_dragTimer->Start (DRAG_TIMER_TICKS, wxTIMER_ONE_SHOT);
        }
        m_dragCount++;
        if (m_dragCount < 3) return; // minimum drag 3 pixel
        if (m_dragTimer->IsRunning()) return;

        // we're going to drag
        m_dragCount = 0;
        m_isDragging = true;
        CaptureMouse();
        RefreshSelected();

        // send drag start event
        wxEventType command = event.LeftIsDown()
                              ? wxEVT_COMMAND_TREE_BEGIN_DRAG
                              : wxEVT_COMMAND_TREE_BEGIN_RDRAG;
        wxTreeEvent nevent (command, m_owner->GetId());
        nevent.SetEventObject (m_owner);
#if !wxCHECK_VERSION(2, 5, 0)
        nevent.SetItem ((long)m_drag_item); // the item the drag is ended
#else
        nevent.SetItem (m_drag_item); // the item the drag is ended
#endif
        nevent.SetPoint (p);
        nevent.Veto(); // dragging must be explicit allowed!
        m_owner->GetEventHandler()->ProcessEvent (nevent);

    }else if (m_isDragging) { // any other event but not event.Dragging()

        // end dragging
        m_dragCount = 0;
        m_isDragging = false;
        if (HasCapture()) ReleaseMouse();
        RefreshSelected();

        // send drag end event event
        wxTreeEvent nevent (wxEVT_COMMAND_TREE_END_DRAG, m_owner->GetId());
        nevent.SetEventObject (m_owner);
#if !wxCHECK_VERSION(2, 5, 0)
        nevent.SetItem ((long)item); // the item the drag is started
#else
        nevent.SetItem (item); // the item the drag is started
#endif
        nevent.SetPoint (p);
        m_drag_item = (wxTreeListItem *)NULL;
        m_owner->GetEventHandler()->ProcessEvent (nevent);

    }else if (m_dragCount > 0) { // just in case dragging is initiated

        // end dragging
        m_dragCount = 0;

    }

    // we process only the messages which happen on tree items
    if (item == NULL) {
        m_owner->GetEventHandler()->ProcessEvent (event);
        return;
    }

    // remember item at shift down
    if (event.ShiftDown())  {
        if (!m_shiftItem) m_shiftItem = m_curItem;
    }else{
        m_shiftItem = (wxTreeListItem*)NULL;
    }

    if (event.RightUp()) {

        SetFocus();
        wxTreeEvent nevent (wxEVT_COMMAND_TREE_ITEM_RIGHT_CLICK, m_owner->GetId());
        nevent.SetEventObject (m_owner);
#if !wxCHECK_VERSION(2, 5, 0)
        nevent.SetItem ((long)item); // the item clicked
#else
        nevent.SetItem (item); // the item clicked
#endif
        nevent.SetInt (m_curColumn); // the colum clicked
        nevent.SetPoint (p);
        m_owner->GetEventHandler()->ProcessEvent (nevent);

    }else if (event.LeftUp()) {
        m_should_return = false;
        if (m_lastOnSame) {
            if ((item == m_curItem) && (m_curColumn != -1) &&
                (m_owner->GetHeaderWindow()->IsColumnEditable (m_curColumn)) &&
                (flags & (wxTREE_HITTEST_ONITEMLABEL | wxTREE_HITTEST_ONITEMCOLUMN))){
                m_renameTimer->Start (RENAME_TIMER_TICKS, wxTIMER_ONE_SHOT);
            }
            m_lastOnSame = false;
        }

        if (((flags & wxTREE_HITTEST_ONITEMBUTTON) ||
             (flags & wxTREE_HITTEST_ONITEMICON)) &&
            HasButtons() && item->HasPlus()) {

            // only toggle the item for a single click, double click on
            // the button doesn't do anything (it toggles the item twice)
            if (event.LeftDown()) Toggle (item);

            // don't select the item if the button was clicked
            return;
        }

        // determine the selection if not done by left down
        if (!m_left_down_selection) {
            bool unselect_others = !((event.ShiftDown() || event.ControlDown()) &&
                                     HasFlag(wxTR_MULTIPLE));
            SelectItem (item, m_shiftItem, unselect_others);
            EnsureVisible (item);
            m_curItem = item; // make the new item the current item
        }else{
            m_left_down_selection = false;
        }

    }else if (event.LeftDown() || event.RightDown() || event.LeftDClick()) {

        if (event.LeftDown() || event.RightDown()) {
            if (event.LeftDown( )){
                m_drag_item = item;
                m_should_return = true;
            }
            SetFocus();
            m_lastOnSame = item == m_curItem;
        }

        if (((flags & wxTREE_HITTEST_ONITEMBUTTON) ||
             (flags & wxTREE_HITTEST_ONITEMICON)) &&
            item->HasPlus()) {

            // only toggle the item for a single click, double click on
            // the button doesn't do anything (it toggles the item twice)
            if (event.LeftDown()) Toggle (item);

            // don't select the item if the button was clicked
            return;
        }

        // determine the selection if the current item is not selected
        if (!item->IsSelected()) {
            if ( !m_should_return) return;
            bool unselect_others = !((event.ShiftDown() || event.ControlDown()) &&
                                     HasFlag(wxTR_MULTIPLE));
            SelectItem (item, m_shiftItem, unselect_others);
            m_should_return = true;
            EnsureVisible (item);
            m_curItem = item; // make the new item the current item
            m_left_down_selection = true;
        }

        // For some reason, Windows isn't recognizing a left double-click,
        // so we need to simulate it here.  Allow 200 milliseconds for now.
        if (event.LeftDClick()) {

            // double clicking should not start editing the item label
            m_renameTimer->Stop();
            m_lastOnSame = false;

            // send activate event first
            wxTreeEvent nevent (wxEVT_COMMAND_TREE_ITEM_ACTIVATED, m_owner->GetId());
            nevent.SetEventObject (m_owner);
#if !wxCHECK_VERSION(2, 5, 0)
            nevent.SetItem ((long)item); // the item clicked
#else
            nevent.SetItem (item); // the item clicked
#endif
            nevent.SetInt (m_curColumn); // the colum clicked
            nevent.SetPoint (p);
            if (!m_owner->GetEventHandler()->ProcessEvent (nevent)) {

                // if the user code didn't process the activate event,
                // handle it ourselves by toggling the item when it is
                // double clicked
                if (item->HasPlus()) Toggle(item);
            }
        }

    }else{ // any other event skip just in case

        event.Skip();

    }
}

void wxTreeListMainWindow::OnIdle (wxIdleEvent &WXUNUSED(event)) {
    /* after all changes have been done to the tree control,
     * we actually redraw the tree when everything is over */

    if (!m_dirty) return;
    m_dirty = false;

    // Check if we need to select the root item
    // because nothing else has been selected.
    // Delaying it means that we can invoke event handlers
    // as required, when a first item is selected.
    if (!m_owner->HasFlag(wxTR_MULTIPLE) && !m_owner->GetSelection().IsOk())
    {
        if (m_select_me)
            m_owner->SelectItem(m_select_me);
        else if (m_owner->GetRootItem().IsOk())
            m_owner->SelectItem(m_owner->GetRootItem());
        m_select_me = NULL;
        m_curItem = (wxTreeListItem*)m_owner->GetSelection().m_pItem;

    }
    
    CalculatePositions();
    Refresh();
    AdjustMyScrollbars();
}

void wxTreeListMainWindow::OnScroll (wxScrollWinEvent& event) {
    // FIXME
#if defined(__WXGTK__) && !defined(__WXUNIVERSAL__)
    wxScrolledWindow::OnScroll(event);
#else
    HandleOnScroll( event );
#endif

    if(event.GetOrientation() == wxHORIZONTAL) {
        m_owner->GetHeaderWindow()->Refresh();
        m_owner->GetHeaderWindow()->Update();
    }
}

void wxTreeListMainWindow::CalculateSize (wxTreeListItem *item, wxDC &dc) {
    wxCoord text_w = 0;
    wxCoord text_h = 0;

    dc.SetFont (GetItemFont (item));

    dc.GetTextExtent (item->GetText (m_main_column), &text_w, &text_h);

    // restore normal font
    dc.SetFont (m_normalFont);

    int total_h = (m_imgHeight > text_h) ? m_imgHeight : text_h;
    if (total_h < 30) { // add 10% space if greater than 30 pixels
        total_h += 2; // minimal 2 pixel space
    }else{
        total_h += total_h / 10; // otherwise 10% space
    }

    item->SetHeight (total_h);
    if (total_h > m_lineHeight) m_lineHeight = total_h;
    item->SetWidth(m_imgWidth + text_w+2);
}

// -----------------------------------------------------------------------------
void wxTreeListMainWindow::CalculateLevel (wxTreeListItem *item, wxDC &dc,
                                           int level, int &y, int x_colstart) {

    // calculate position of vertical lines
    int x = x_colstart + MARGIN; // start of column
    if (HasFlag(wxTR_LINES_AT_ROOT)) x += LINEATROOT; // space for lines at root
    if (HasButtons()) {
        x += (m_btnWidth-m_btnWidth2); // half button space
    }else{
        x += (m_indent-m_indent/2);
    }
    if (HasFlag(wxTR_HIDE_ROOT)) {
        x += m_indent * (level-1); // indent but not level 1
    }else{
        x += m_indent * level; // indent according to level
    }

    // a hidden root is not evaluated, but its children are always
    if (HasFlag(wxTR_HIDE_ROOT) && (level == 0)) goto Recurse;

    CalculateSize( item, dc );

    // set its position
    item->SetX (x);
    item->SetY (y);
    y += GetLineHeight(item);

    // we don't need to calculate collapsed branches
    if ( !item->IsExpanded() ) return;

Recurse:
    wxArrayTreeListItems& children = item->GetChildren();
    long n, count = (long)children.Count();
    ++level;
    for (n = 0; n < count; ++n) {
        CalculateLevel( children[n], dc, level, y, x_colstart );  // recurse
    }
}

void wxTreeListMainWindow::CalculatePositions() {
    if ( !m_rootItem ) return;

    wxClientDC dc(this);
    PrepareDC( dc );

    dc.SetFont( m_normalFont );

    dc.SetPen( m_dottedPen );
    //if(GetImageList() == NULL)
    // m_lineHeight = (int)(dc.GetCharHeight() + 4);

    int y = 2;
    int x_colstart = 0;
    for (int i = 0; i < (int)GetMainColumn(); ++i) {
        if (!m_owner->GetHeaderWindow()->IsColumnShown(i)) continue;
        x_colstart += m_owner->GetHeaderWindow()->GetColumnWidth(i);
    }
    CalculateLevel( m_rootItem, dc, 0, y, x_colstart ); // start recursion
}

void wxTreeListMainWindow::RefreshSubtree (wxTreeListItem *item) {
    if (m_dirty) return;
    wxCHECK_RET (item != NULL, _T("bug : invalid item in wxTreeListMainWindow::RefreshSubtree") );

    wxClientDC dc(this);
    PrepareDC(dc);

    int cw = 0;
    int ch = 0;
    GetVirtualSize( &cw, &ch );

    wxRect rect;
    rect.x = dc.LogicalToDeviceX( 0 );
    rect.width = cw;
    rect.y = dc.LogicalToDeviceY( item->GetY() - 2 );
    rect.height = ch;

    Refresh (true, &rect );
    AdjustMyScrollbars();
}

void wxTreeListMainWindow::RefreshLine (wxTreeListItem *item) {
    if (m_dirty) return;
    wxCHECK_RET (item != NULL, _T("bug : invalid item in wxTreeListMainWindow::RefreshLine") );

    wxClientDC dc(this);
    PrepareDC( dc );

    int cw = 0;
    int ch = 0;
    GetVirtualSize( &cw, &ch );

    wxRect rect;
    rect.x = dc.LogicalToDeviceX( 0 );
    rect.y = dc.LogicalToDeviceY( item->GetY() );
    rect.width = cw;
    rect.height = GetLineHeight(item); //dc.GetCharHeight() + 6;

    Refresh (true, &rect);
}

void wxTreeListMainWindow::RefreshSelected() {
    // TODO: this is awfully inefficient, we should keep the list of all
    //       selected items internally, should be much faster
    if (m_rootItem) {
        RefreshSelectedUnder (m_rootItem);
    }
}

void wxTreeListMainWindow::RefreshSelectedUnder (wxTreeListItem *item) {
    if (item->IsSelected()) {
        RefreshLine (item);
    }

    const wxArrayTreeListItems& children = item->GetChildren();
    long count = children.GetCount();
    for (long n = 0; n < count; n++ ) {
        RefreshSelectedUnder (children[n]);
    }
}

// ----------------------------------------------------------------------------
// changing colours: we need to refresh the tree control
// ----------------------------------------------------------------------------

bool wxTreeListMainWindow::SetBackgroundColour (const wxColour& colour) {
    if (!wxWindow::SetBackgroundColour(colour)) return false;

    Refresh();
    return true;
}

bool wxTreeListMainWindow::SetForegroundColour (const wxColour& colour) {
    if (!wxWindow::SetForegroundColour(colour)) return false;

    Refresh();
    return true;
}

void wxTreeListMainWindow::SetItemText (const wxTreeItemId& itemId, int column,
                                        const wxString& text) {
    wxCHECK_RET (itemId.IsOk(), _T("invalid tree item"));

    wxClientDC dc (this);
    wxTreeListItem *item = (wxTreeListItem*) itemId.m_pItem;
    item->SetText (column, text);
    CalculateSize (item, dc);
    RefreshLine (item);
}

wxString wxTreeListMainWindow::GetItemText (const wxTreeItemId& itemId,
                                            int column) const {
    wxCHECK_MSG (itemId.IsOk(), _T(""), _T("invalid tree item") );

    if( IsVirtual() )   return m_owner->OnGetItemText(((wxTreeListItem*) itemId.m_pItem)->GetData(),column);
    else                return ((wxTreeListItem*) itemId.m_pItem)->GetText (column);
}

wxString wxTreeListMainWindow::GetItemText (wxTreeItemData* item,
int column) const {
   wxASSERT_MSG( IsVirtual(), _T("can be used only with virtual control") );
   return m_owner->OnGetItemText(item,column);
}

void wxTreeListMainWindow::SetFocus() {
    wxWindow::SetFocus();
}

wxFont wxTreeListMainWindow::GetItemFont (wxTreeListItem *item) {
    wxTreeItemAttr *attr = item->GetAttributes();

    if (attr && attr->HasFont()) {
        return attr->GetFont();
    }else if (item->IsBold()) {
        return m_boldFont;
    }else{
        return m_normalFont;
   }
}

int wxTreeListMainWindow::GetItemWidth (int column, wxTreeListItem *item) {
    if (!item) return 0;

    // determine item width
    int w = 0, h = 0;
    wxFont font = GetItemFont (item);
    GetTextExtent (item->GetText (column), &w, &h, NULL, NULL, font.Ok()? &font: NULL);
    w += 2*MARGIN;

    // calculate width
    int width = w + 2*MARGIN;
    if (column == GetMainColumn()) {
        width += MARGIN;
        if (HasFlag(wxTR_LINES_AT_ROOT)) width += LINEATROOT;
        if (HasButtons()) width += m_btnWidth + LINEATROOT;
        if (item->GetCurrentImage() != NO_IMAGE) width += m_imgWidth;

        // count indent level
        int level = 0;
        wxTreeListItem *parent = item->GetItemParent();
        wxTreeListItem *root = (wxTreeListItem*)GetRootItem().m_pItem;
        while (parent && (!HasFlag(wxTR_HIDE_ROOT) || (parent != root))) {
            level++;
            parent = parent->GetItemParent();
        }
        if (level) width += level * GetIndent();
    }

    return width;
}

int wxTreeListMainWindow::GetBestColumnWidth (int column, wxTreeItemId parent) {
    int maxWidth, h;
    GetClientSize (&maxWidth, &h);
    int width = 0;

    // get root if on item
    if (!parent.IsOk()) parent = GetRootItem();

    // add root width
    if (!HasFlag(wxTR_HIDE_ROOT)) {
        int w = GetItemWidth (column, (wxTreeListItem*)parent.m_pItem);
        if (width < w) width = w;
        if (width > maxWidth) return maxWidth;
    }

    wxTreeItemIdValue cookie = 0;
    wxTreeItemId item = GetFirstChild (parent, cookie);
    while (item.IsOk()) {
        int w = GetItemWidth (column, (wxTreeListItem*)item.m_pItem);
        if (width < w) width = w;
        if (width > maxWidth) return maxWidth;

        // check the children of this item
        if (((wxTreeListItem*)item.m_pItem)->IsExpanded()) {
            int w = GetBestColumnWidth (column, item);
            if (width < w) width = w;
            if (width > maxWidth) return maxWidth;
        }

        // next sibling
        item = GetNextChild (parent, cookie);
    }

    return width;
}


//-----------------------------------------------------------------------------
//  wxTreeListCtrl
//-----------------------------------------------------------------------------

IMPLEMENT_DYNAMIC_CLASS(wxTreeListCtrl, wxControl);

BEGIN_EVENT_TABLE(wxTreeListCtrl, wxControl)
    EVT_SIZE(wxTreeListCtrl::OnSize)
END_EVENT_TABLE();

bool wxTreeListCtrl::Create(wxWindow *parent, wxWindowID id,
                            const wxPoint& pos,
                            const wxSize& size,
                            long style, const wxValidator &validator,
                            const wxString& name)
{
    long main_style = style & ~(wxSIMPLE_BORDER|wxSUNKEN_BORDER|wxDOUBLE_BORDER|
                                wxRAISED_BORDER|wxSTATIC_BORDER);
    long ctrl_style = style & ~(wxVSCROLL|wxHSCROLL);

    if (!wxControl::Create(parent, id, pos, size, ctrl_style, validator, name)) {
       return false;
    }
    m_main_win = new wxTreeListMainWindow (this, -1, wxPoint(0, 0), size,
                                           main_style, validator);
    m_header_win = new wxTreeListHeaderWindow (this, -1, m_main_win,
                                               wxPoint(0, 0), wxDefaultSize,
                                               wxTAB_TRAVERSAL);
    CalculateAndSetHeaderHeight();
    return true;
}

void wxTreeListCtrl::CalculateAndSetHeaderHeight()
{
    if (m_header_win) {
        int h;
#if wxCHECK_VERSION_FULL(2, 7, 0, 1)
        h = wxRendererNative::Get().GetHeaderButtonHeight(m_header_win);
#else
        // we use 'g' to get the descent, too
        int w, d;
        m_header_win->GetTextExtent(_T("Hg"), &w, &h, &d);
        h += d + 2 * HEADER_OFFSET_Y + EXTRA_HEIGHT;
#endif
        // only update if changed
        if (h != m_headerHeight) {
            m_headerHeight = h;
            DoHeaderLayout();
        }
    }
}

void wxTreeListCtrl::DoHeaderLayout()
{
    int w, h;
    GetClientSize(&w, &h);
    if (m_header_win) {
        m_header_win->SetSize (0, 0, w, m_headerHeight);
        m_header_win->Refresh();
    }
    if (m_main_win) {
        m_main_win->SetSize (0, m_headerHeight + 1, w, h - m_headerHeight - 1);
    }
}

void wxTreeListCtrl::OnSize(wxSizeEvent& WXUNUSED(event))
{
    DoHeaderLayout();
}

size_t wxTreeListCtrl::GetCount() const { return m_main_win->GetCount(); }

unsigned int wxTreeListCtrl::GetIndent() const
{ return m_main_win->GetIndent(); }

void wxTreeListCtrl::SetIndent(unsigned int indent)
{ m_main_win->SetIndent(indent); }

unsigned int wxTreeListCtrl::GetLineSpacing() const
{ return m_main_win->GetLineSpacing(); }

void wxTreeListCtrl::SetLineSpacing(unsigned int spacing)
{ m_main_win->SetLineSpacing(spacing); }

wxImageList* wxTreeListCtrl::GetImageList() const
{ return m_main_win->GetImageList(); }

wxImageList* wxTreeListCtrl::GetStateImageList() const
{ return m_main_win->GetStateImageList(); }

wxImageList* wxTreeListCtrl::GetButtonsImageList() const
{ return m_main_win->GetButtonsImageList(); }

void wxTreeListCtrl::SetImageList(wxImageList* imageList)
{ m_main_win->SetImageList(imageList); }

void wxTreeListCtrl::SetStateImageList(wxImageList* imageList)
{ m_main_win->SetStateImageList(imageList); }

void wxTreeListCtrl::SetButtonsImageList(wxImageList* imageList)
{ m_main_win->SetButtonsImageList(imageList); }

void wxTreeListCtrl::AssignImageList(wxImageList* imageList)
{ m_main_win->AssignImageList(imageList); }

void wxTreeListCtrl::AssignStateImageList(wxImageList* imageList)
{ m_main_win->AssignStateImageList(imageList); }

void wxTreeListCtrl::AssignButtonsImageList(wxImageList* imageList)
{ m_main_win->AssignButtonsImageList(imageList); }

wxString wxTreeListCtrl::GetItemText(const wxTreeItemId& item, int column) const
{ return m_main_win->GetItemText (item, column); }

int wxTreeListCtrl::GetItemImage(const wxTreeItemId& item, int column,
                                 wxTreeItemIcon which) const
{ return m_main_win->GetItemImage(item, column, which); }

wxTreeItemData* wxTreeListCtrl::GetItemData(const wxTreeItemId& item) const
{ return m_main_win->GetItemData(item); }

bool wxTreeListCtrl::GetItemBold(const wxTreeItemId& item) const
{ return m_main_win->GetItemBold(item); }

wxColour wxTreeListCtrl::GetItemTextColour(const wxTreeItemId& item) const
{ return m_main_win->GetItemTextColour(item); }

wxColour wxTreeListCtrl::GetItemBackgroundColour(const wxTreeItemId& item)
    const
{ return m_main_win->GetItemBackgroundColour(item); }

wxFont wxTreeListCtrl::GetItemFont(const wxTreeItemId& item) const
{ return m_main_win->GetItemFont(item); }


void wxTreeListCtrl::SetItemText(const wxTreeItemId& item, int column,
                                 const wxString& text)
{ m_main_win->SetItemText (item, column, text); }

void wxTreeListCtrl::SetItemImage(const wxTreeItemId& item,
                                  int column,
                                  int image,
                                  wxTreeItemIcon which)
{ m_main_win->SetItemImage(item, column, image, which); }

void wxTreeListCtrl::SetItemData(const wxTreeItemId& item,
                                 wxTreeItemData* data)
{ m_main_win->SetItemData(item, data); }

void wxTreeListCtrl::SetItemHasChildren(const wxTreeItemId& item, bool has)
{ m_main_win->SetItemHasChildren(item, has); }

void wxTreeListCtrl::SetItemBold(const wxTreeItemId& item, bool bold)
{ m_main_win->SetItemBold(item, bold); }

void wxTreeListCtrl::SetItemTextColour(const wxTreeItemId& item,
                                       const wxColour& colour)
{ m_main_win->SetItemTextColour(item, colour); }

void wxTreeListCtrl::SetItemBackgroundColour(const wxTreeItemId& item,
                                             const wxColour& colour)
{ m_main_win->SetItemBackgroundColour(item, colour); }

void wxTreeListCtrl::SetItemFont(const wxTreeItemId& item,
                                 const wxFont& font)
{ m_main_win->SetItemFont(item, font); }

bool wxTreeListCtrl::SetFont(const wxFont& font)
{
    if (m_header_win) {
        m_header_win->SetFont(font);
        CalculateAndSetHeaderHeight();
        m_header_win->Refresh();
    }
    if (m_main_win) {
        return m_main_win->SetFont(font);
    }else{
        return false;
    }
}

void wxTreeListCtrl::SetWindowStyle(const long style)
{
    if(m_main_win)
        m_main_win->SetWindowStyle(style);
    m_windowStyle = style;
    // TODO: provide something like wxTL_NO_HEADERS to hide m_header_win
}

long wxTreeListCtrl::GetWindowStyle() const
{
    long style = m_windowStyle;
    if(m_main_win)
        style |= m_main_win->GetWindowStyle();
    return style;
}

bool wxTreeListCtrl::IsVisible(const wxTreeItemId& item, bool fullRow) const
{ return m_main_win->IsVisible(item, fullRow); }

bool wxTreeListCtrl::HasChildren(const wxTreeItemId& item) const
{ return m_main_win->HasChildren(item); }

bool wxTreeListCtrl::IsExpanded(const wxTreeItemId& item) const
{ return m_main_win->IsExpanded(item); }

bool wxTreeListCtrl::IsSelected(const wxTreeItemId& item) const
{ return m_main_win->IsSelected(item); }

bool wxTreeListCtrl::IsBold(const wxTreeItemId& item) const
{ return m_main_win->IsBold(item); }

size_t wxTreeListCtrl::GetChildrenCount(const wxTreeItemId& item, bool rec)
{ return m_main_win->GetChildrenCount(item, rec); }

wxTreeItemId wxTreeListCtrl::GetRootItem() const
{ return m_main_win->GetRootItem(); }

wxTreeItemId wxTreeListCtrl::GetSelection() const
{ return m_main_win->GetSelection(); }

size_t wxTreeListCtrl::GetSelections(wxArrayTreeItemIds& arr) const
{ return m_main_win->GetSelections(arr); }

wxTreeItemId wxTreeListCtrl::GetItemParent(const wxTreeItemId& item) const
{ return m_main_win->GetItemParent(item); }

wxTreeItemId wxTreeListCtrl::GetCurrentItem() const
{ return m_main_win->GetCurrentItem(); }

void wxTreeListCtrl::SetCurrentItem(const wxTreeItemId& newItem)
{ m_main_win->SetCurrentItem(newItem); }


#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListCtrl::GetFirstChild (const wxTreeItemId& item,
                                            long& cookie) const
#else
wxTreeItemId wxTreeListCtrl::GetFirstChild (const wxTreeItemId& item,
                                            wxTreeItemIdValue& cookie) const
#endif
{ return m_main_win->GetFirstChild(item, cookie); }

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListCtrl::GetNextChild (const wxTreeItemId& item,
                                           long& cookie) const
#else
wxTreeItemId wxTreeListCtrl::GetNextChild (const wxTreeItemId& item,
                                           wxTreeItemIdValue& cookie) const
#endif
{ return m_main_win->GetNextChild(item, cookie); }

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListCtrl::GetPrevChild (const wxTreeItemId& item,
                                           long& cookie) const
#else
wxTreeItemId wxTreeListCtrl::GetPrevChild (const wxTreeItemId& item,
                                           wxTreeItemIdValue& cookie) const
#endif
{ return m_main_win->GetPrevChild(item, cookie); }

#if !wxCHECK_VERSION(2, 5, 0)
wxTreeItemId wxTreeListCtrl::GetLastChild (const wxTreeItemId& item,
                                           long& cookie) const
#else
wxTreeItemId wxTreeListCtrl::GetLastChild (const wxTreeItemId& item,
                                           wxTreeItemIdValue& cookie) const
#endif
{ return m_main_win->GetLastChild(item, cookie); }


wxTreeItemId wxTreeListCtrl::GetNextSibling(const wxTreeItemId& item) const
{ return m_main_win->GetNextSibling(item); }

wxTreeItemId wxTreeListCtrl::GetPrevSibling(const wxTreeItemId& item) const
{ return m_main_win->GetPrevSibling(item); }

wxTreeItemId wxTreeListCtrl::GetNext(const wxTreeItemId& item) const
{ return m_main_win->GetNext(item, true); }

wxTreeItemId wxTreeListCtrl::GetPrev(const wxTreeItemId& item) const
{ return m_main_win->GetPrev(item, true); }

wxTreeItemId wxTreeListCtrl::GetFirstExpandedItem() const
{ return m_main_win->GetFirstExpandedItem(); }

wxTreeItemId wxTreeListCtrl::GetNextExpanded(const wxTreeItemId& item) const
{ return m_main_win->GetNextExpanded(item); }

wxTreeItemId wxTreeListCtrl::GetPrevExpanded(const wxTreeItemId& item) const
{ return m_main_win->GetPrevExpanded(item); }

wxTreeItemId wxTreeListCtrl::GetFirstVisibleItem(bool fullRow) const
{ return m_main_win->GetFirstVisibleItem(fullRow); }

wxTreeItemId wxTreeListCtrl::GetNextVisible(const wxTreeItemId& item, bool fullRow) const
{ return m_main_win->GetNextVisible(item, fullRow); }

wxTreeItemId wxTreeListCtrl::GetPrevVisible(const wxTreeItemId& item, bool fullRow) const
{ return m_main_win->GetPrevVisible(item, fullRow); }

wxTreeItemId wxTreeListCtrl::AddRoot (const wxString& text, int image,
                                      int selectedImage, wxTreeItemData* data)
{ return m_main_win->AddRoot (text, image, selectedImage, data); }

wxTreeItemId wxTreeListCtrl::PrependItem(const wxTreeItemId& parent,
                                         const wxString& text, int image,
                                         int selectedImage,
                                         wxTreeItemData* data)
{ return m_main_win->PrependItem(parent, text, image, selectedImage, data); }

wxTreeItemId wxTreeListCtrl::InsertItem(const wxTreeItemId& parent,
                                        const wxTreeItemId& previous,
                                        const wxString& text, int image,
                                        int selectedImage,
                                        wxTreeItemData* data)
{
    return m_main_win->InsertItem(parent, previous, text, image,
                                  selectedImage, data);
}

wxTreeItemId wxTreeListCtrl::InsertItem(const wxTreeItemId& parent,
                                        size_t index,
                                        const wxString& text, int image,
                                        int selectedImage,
                                        wxTreeItemData* data)
{
    return m_main_win->InsertItem(parent, index, text, image,
                                  selectedImage, data);
}

wxTreeItemId wxTreeListCtrl::AppendItem(const wxTreeItemId& parent,
                                        const wxString& text, int image,
                                        int selectedImage,
                                        wxTreeItemData* data)
{ return m_main_win->AppendItem(parent, text, image, selectedImage, data); }

void wxTreeListCtrl::Delete(const wxTreeItemId& item)
{ m_main_win->Delete(item); }

void wxTreeListCtrl::DeleteChildren(const wxTreeItemId& item)
{ m_main_win->DeleteChildren(item); }

void wxTreeListCtrl::DeleteRoot()
{ m_main_win->DeleteRoot(); }

void wxTreeListCtrl::Expand(const wxTreeItemId& item)
{ m_main_win->Expand(item); }

void wxTreeListCtrl::ExpandAll(const wxTreeItemId& item)
{ m_main_win->ExpandAll(item); }

void wxTreeListCtrl::Collapse(const wxTreeItemId& item)
{ m_main_win->Collapse(item); }

void wxTreeListCtrl::CollapseAndReset(const wxTreeItemId& item)
{ m_main_win->CollapseAndReset(item); }

void wxTreeListCtrl::Toggle(const wxTreeItemId& item)
{ m_main_win->Toggle(item); }

void wxTreeListCtrl::Unselect()
{ m_main_win->Unselect(); }

void wxTreeListCtrl::UnselectAll()
{ m_main_win->UnselectAll(); }

void wxTreeListCtrl::SelectItem(const wxTreeItemId& item, const wxTreeItemId& last,
                                bool unselect_others)
{ m_main_win->SelectItem (item, last, unselect_others); }

void wxTreeListCtrl::SelectAll()
{ m_main_win->SelectAll(); }

void wxTreeListCtrl::EnsureVisible(const wxTreeItemId& item)
{ m_main_win->EnsureVisible(item); }

void wxTreeListCtrl::ScrollTo(const wxTreeItemId& item)
{ m_main_win->ScrollTo(item); }

wxTreeItemId wxTreeListCtrl::HitTest(const wxPoint& pos, int& flags, int& column)
{
    return m_main_win->HitTest (pos, flags, column);
}

bool wxTreeListCtrl::GetBoundingRect(const wxTreeItemId& item, wxRect& rect,
                                     bool textOnly) const
{ return m_main_win->GetBoundingRect(item, rect, textOnly); }

void wxTreeListCtrl::EditLabel (const wxTreeItemId& item, int column)
{ m_main_win->EditLabel (item, column); }

int wxTreeListCtrl::OnCompareItems(const wxTreeItemId& item1,
                                   const wxTreeItemId& item2)
{
    // do the comparison here, and not delegate to m_main_win, in order
    // to let the user override it
    //return m_main_win->OnCompareItems(item1, item2);
    return wxStrcmp(GetItemText(item1), GetItemText(item2));
}

void wxTreeListCtrl::SortChildren(const wxTreeItemId& item)
{ m_main_win->SortChildren(item); }

wxTreeItemId wxTreeListCtrl::FindItem (const wxTreeItemId& item, const wxString& str, int mode)
{ return m_main_win->FindItem (item, str, mode); }

void wxTreeListCtrl::SetDragItem (const wxTreeItemId& item)
{ m_main_win->SetDragItem (item); }

bool wxTreeListCtrl::SetBackgroundColour(const wxColour& colour)
{
    if (!m_main_win) return false;
    return m_main_win->SetBackgroundColour(colour);
}

bool wxTreeListCtrl::SetForegroundColour(const wxColour& colour)
{
    if (!m_main_win) return false;
    return m_main_win->SetForegroundColour(colour);
}

int wxTreeListCtrl::GetColumnCount() const
{ return m_main_win->GetColumnCount(); }

void wxTreeListCtrl::SetColumnWidth(int column, int width)
{
    if (width == wxLIST_AUTOSIZE_USEHEADER)
    {
        wxFont font = m_header_win->GetFont();
        m_header_win->GetTextExtent(m_header_win->GetColumnText(column), &width, NULL, NULL, NULL, font.Ok()? &font: NULL);
        //search wxTreeListHeaderWindow::OnPaint to understand this:
        width += 2*EXTRA_WIDTH + MARGIN;
    }
    else if (width == wxLIST_AUTOSIZE)
    {
        width = m_main_win->GetBestColumnWidth(column);
    }
    
    m_header_win->SetColumnWidth (column, width);
    m_header_win->Refresh();
}

int wxTreeListCtrl::GetColumnWidth(int column) const
{ return m_header_win->GetColumnWidth(column); }

void wxTreeListCtrl::SetMainColumn(int column)
{ m_main_win->SetMainColumn(column); }

int wxTreeListCtrl::GetMainColumn() const
{ return m_main_win->GetMainColumn(); }

void wxTreeListCtrl::SetColumnText(int column, const wxString& text)
{
    m_header_win->SetColumnText (column, text);
    m_header_win->Refresh();
}

wxString wxTreeListCtrl::GetColumnText(int column) const
{ return m_header_win->GetColumnText(column); }

void wxTreeListCtrl::AddColumn(const wxTreeListColumnInfo& colInfo)
{
    m_header_win->AddColumn (colInfo);
    DoHeaderLayout();
}

void wxTreeListCtrl::InsertColumn(int before, const wxTreeListColumnInfo& colInfo)
{
    m_header_win->InsertColumn (before, colInfo);
    m_header_win->Refresh();
}

void wxTreeListCtrl::RemoveColumn(int column)
{
    m_header_win->RemoveColumn (column);
    m_header_win->Refresh();
}

void wxTreeListCtrl::SetColumn(int column, const wxTreeListColumnInfo& colInfo)
{
    m_header_win->SetColumn (column, colInfo);
    m_header_win->Refresh();
}

const wxTreeListColumnInfo& wxTreeListCtrl::GetColumn(int column) const
{ return m_header_win->GetColumn(column); }

wxTreeListColumnInfo& wxTreeListCtrl::GetColumn(int column)
{ return m_header_win->GetColumn(column); }

void wxTreeListCtrl::SetColumnImage(int column, int image)
{
    m_header_win->SetColumn (column, GetColumn(column).SetImage(image));
    m_header_win->Refresh();
}

int wxTreeListCtrl::GetColumnImage(int column) const
{
    return m_header_win->GetColumn(column).GetImage();
}

void wxTreeListCtrl::SetColumnEditable(int column, bool shown)
{
    m_header_win->SetColumn (column, GetColumn(column).SetEditable(shown));
}

void wxTreeListCtrl::SetColumnShown(int column, bool shown)
{
    wxASSERT_MSG (column != GetMainColumn(), _T("The main column may not be hidden") );
    m_header_win->SetColumn (column, GetColumn(column).SetShown(GetMainColumn()==column? true: shown));
    m_header_win->Refresh();
}

bool wxTreeListCtrl::IsColumnEditable(int column) const
{
    return m_header_win->GetColumn(column).IsEditable();
}

bool wxTreeListCtrl::IsColumnShown(int column) const
{
    return m_header_win->GetColumn(column).IsShown();
}

void wxTreeListCtrl::SetColumnAlignment (int column, int flag)
{
    m_header_win->SetColumn(column, GetColumn(column).SetAlignment(flag));
    m_header_win->Refresh();
}

int wxTreeListCtrl::GetColumnAlignment(int column) const
{
    return m_header_win->GetColumn(column).GetAlignment();
}

void wxTreeListCtrl::Refresh(bool erase, const wxRect* rect)
{
    m_main_win->Refresh (erase, rect);
    m_header_win->Refresh (erase, rect);
}

void wxTreeListCtrl::SetFocus()
{ m_main_win->SetFocus(); }

wxSize wxTreeListCtrl::DoGetBestSize() const
{
    // something is better than nothing...
    return wxSize (200,200); // but it should be specified values! FIXME
}

wxString wxTreeListCtrl::OnGetItemText( wxTreeItemData* WXUNUSED(item), long WXUNUSED(column)) const
{
    return wxEmptyString;
}