summaryrefslogtreecommitdiff
path: root/ext/tk/lib/tkextlib/treectrl/tktreectrl.rb
blob: e10e6e299e02456693767e54d8345337cd2432cf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
#
#  tkextlib/treectrl/tktreectrl.rb
#                               by Hidetoshi NAGAI (nagai@ai.kyutech.ac.jp)
#

require 'tk'

# call setup script for general 'tkextlib' libraries
require 'tkextlib/setup.rb'

# call setup script
require 'tkextlib/treectrl/setup.rb'

# TkPackage.require('treectrl', '1.0')
# TkPackage.require('treectrl', '1.1')
TkPackage.require('treectrl')

module Tk
  class TreeCtrl < TkWindow
    BindTag_FileList = TkBindTag.new_by_name('TreeCtrlFileList')

    PACKAGE_NAME = 'treectrl'.freeze
    def self.package_name
      PACKAGE_NAME
    end

    def self.package_version
      begin
        TkPackage.require('treectrl')
      rescue
        ''
      end
    end

    HasColumnCreateCommand = 
      (TkPackage.vcompare(self.package_version, '1.1') >= 0)

    # dummy :: 
    #  pkgIndex.tcl of TreeCtrl-1.0 doesn't support auto_load for 
    #  'loupe' command (probably it is bug, I think). 
    #  So, calling a 'treectrl' command for loading the dll with 
    #  the auto_load facility. 
    begin
      tk_call('treectrl')
    rescue
    end
    def self.loupe(img, x, y, w, h, zoom)
      # NOTE: platform == 'unix' only

      # img  => TkPhotoImage
      # x, y => screen coords 
      # w, h => magnifier width and height
      # zoom => zooming rate
      Tk.tk_call_without_enc('loupe', img, x, y, w, h, zoom)
    end

    def self.text_layout(font, text, keys={})
      TkComm.list(Tk.tk_call_without_enc('textlayout', font, text, keys))
    end

    def self.image_tint(img, color, alpha)
      Tk.tk_call_without_enc('imagetint', img, color, alpha)
    end

    class NotifyEvent < TkUtil::CallbackSubst
    end

    module ConfigMethod
    end
  end
  TreeCtrl_Widget = TreeCtrl
end

##############################################

class Tk::TreeCtrl::NotifyEvent
  # [ <'%' subst-key char>, <proc type char>, <instance var (accessor) name>]
  KEY_TBL = [
    [ ?c, ?n, :item_num ], 
    [ ?d, ?s, :detail ], 
    [ ?D, ?l, :items ], 
    [ ?e, ?e, :event ], 
    [ ?I, ?n, :id ], 
    [ ?l, ?n, :lower_bound ], 
    [ ?p, ?n, :active_id ], 
    [ ?P, ?e, :pattern ], 
    [ ?S, ?l, :sel_items ], 
    [ ?T, ?w, :widget ], 
    [ ?u, ?n, :upper_bound ], 
    [ ?W, ?o, :object ], 
    [ ??, ?x, :parm_info ], 
    nil
  ]

  # [ <proc type char>, <proc/method to convert tcl-str to ruby-obj>]
  PROC_TBL = [
    [ ?n, TkComm.method(:num_or_str) ], 
    [ ?s, TkComm.method(:string) ], 
    [ ?l, TkComm.method(:list) ], 
    [ ?w, TkComm.method(:window) ], 

    [ ?e, proc{|val|
        case val
        when /^<<[^<>]+>>$/
          TkVirtualEvent.getobj(val[1..-2])
        when /^<[^<>]+>$/
          val[1..-2]
        else
          val
        end
      }
    ], 

    [ ?o, proc{|val| TkComm.tk_tcl2ruby(val)} ], 

    [ ?x, proc{|val|
        begin
          inf = {}
          Hash[*(TkComm.list(val))].each{|k, v|
            if keyinfo = KEY_TBL.assoc(k[0])
              if cmd = PROC_TBL.assoc(keyinfo[1])
                begin
                  new_v = cmd.call(v)
                  v = new_v
                rescue
                end
              end
            end
            inf[k] = v
          }
          inf
        rescue
          val
        end
      } ], 

    nil
  ]

=begin
  # for Ruby m17n :: ?x --> String --> char-code ( getbyte(0) )
  KEY_TBL.map!{|inf|
    if inf.kind_of?(Array)
      inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
      inf[1] = inf[1].getbyte(0) if inf[1].kind_of?(String)
    end
    inf
  }

  PROC_TBL.map!{|inf|
    if inf.kind_of?(Array)
      inf[0] = inf[0].getbyte(0) if inf[0].kind_of?(String)
    end
    inf
  }
=end

  # setup tables to be used by scan_args, _get_subst_key, _get_all_subst_keys
  #
  #     _get_subst_key() and _get_all_subst_keys() generates key-string 
  #     which describe how to convert callback arguments to ruby objects. 
  #     When binding parameters are given, use _get_subst_key(). 
  #     But when no parameters are given, use _get_all_subst_keys() to 
  #     create a Event class object as a callback parameter. 
  #
  #     scan_args() is used when doing callback. It convert arguments 
  #     ( which are Tcl strings ) to ruby objects based on the key string 
  #     that is generated by _get_subst_key() or _get_all_subst_keys(). 
  #
  _setup_subst_table(KEY_TBL, PROC_TBL);
end

##############################################

module Tk::TreeCtrl::ConfigMethod
  include TkItemConfigMethod

  def treectrl_tagid(key, obj)
    if key.kind_of?(Array)
      key = key.join(' ')
    else
      key = key.to_s
    end

    if (obj.kind_of?(Tk::TreeCtrl::Column) ||
        obj.kind_of?(Tk::TreeCtrl::Element) ||
        obj.kind_of?(Tk::TreeCtrl::Item) ||
        obj.kind_of?(Tk::TreeCtrl::Style)) 
      obj = obj.id
    end

    case key
    when 'column'
      obj

    when 'debug'
      None

    when 'dragimage'
      None

    when 'element'
      obj

    when 'item element'
      obj

    when 'marquee'
      None

    when 'notify'
      obj

    when 'style'
      obj

    else
      obj
    end
  end

  def tagid(mixed_id)
    if mixed_id == 'debug'
      ['debug', None]
    elsif mixed_id == 'dragimage'
      ['dragimage', None]
    elsif mixed_id == 'marquee'
      ['marquee', None]
    elsif mixed_id.kind_of?(Array)
      [mixed_id[0], treectrl_tagid(*mixed_id)]
    else
      tagid(mixed_id.split(':'))
    end
  end

  def __item_cget_cmd(mixed_id)
    if mixed_id[0] == 'column' && mixed_id[1] == 'drag'
      return [self.path, 'column', 'dragcget']
    end 

    if mixed_id[1].kind_of?(Array)
      id = mixed_id[1]
    else
      id = [mixed_id[1]]
    end

    if mixed_id[0].kind_of?(Array)
      ([self.path].concat(mixed_id[0]) << 'cget').concat(id)
    else
      [self.path, mixed_id[0], 'cget'].concat(id)
    end
  end
  private :__item_cget_cmd

  def __item_config_cmd(mixed_id)
    if mixed_id[0] == 'column' && mixed_id[1] == 'drag'
      return [self.path, 'column', 'dragconfigure']
    end 

    if mixed_id[1].kind_of?(Array)
      id = mixed_id[1]
    else
      id = [mixed_id[1]]
    end

    if mixed_id[0].kind_of?(Array)
      ([self.path].concat(mixed_id[0]) << 'configure').concat(id)
    else
      [self.path, mixed_id[0], 'configure'].concat(id)
    end
  end
  private :__item_config_cmd

  def __item_pathname(id)
    if id.kind_of?(Array)
      key = id[0]
      if key.kind_of?(Array)
        key = key.join(' ')
      end

      tag = id[1]
      if tag.kind_of?(Array)
        tag = tag.join(' ')
      end

      id = [key, tag].join(':')
    end
    [self.path, id].join(';')
  end
  private :__item_pathname

  def __item_configinfo_struct(id)
    if id.kind_of?(Array) && id[0].to_s == 'notify'
      {:key=>0, :alias=>nil, :db_name=>nil, :db_class=>nil, 
        :default_value=>nil, :current_value=>1}
    else
      {:key=>0, :alias=>1, :db_name=>1, :db_class=>2, 
        :default_value=>3, :current_value=>4}
    end
  end
  private :__item_configinfo_struct


  def __item_font_optkeys(id)
    if id.kind_of?(Array) && (id[0] == 'element' || 
                              (id[0].kind_of?(Array) && id[0][1] == 'element'))
      []
    else
      ['font']
    end
  end
  private :__item_font_optkeys

  def __item_numstrval_optkeys(id)
    if id == 'debug'
      ['displaydelay']
    else
      super(id)
    end
  end
  private :__item_numstrval_optkeys

  def __item_boolval_optkeys(id)
    if id == 'debug'
      ['data', 'display', 'enable']
    elsif id == 'dragimage'
      ['visible']
    elsif id == 'marquee'
      ['visible']
    elsif id.kind_of?(Array)
      case id[0]
      when 'item'
        ['button', 'visible']
      when 'column'
        if id[1] == 'drag'
          ['enable']
        else
          ['button', 'expand', 'resize', 'squeeze', 'sunken', 
            'visible', 'widthhack']
        end
      when 'element'
        ['draw', 'filled', 'showfocus', 'destroy']
      when 'notify'
        ['active']
      when 'style'
        ['detach']
      else
        if id[0].kind_of?(Array) && id[0][1] == 'element'
          ['filled', 'showfocus']
        else
          super(id)
        end
      end
    else
      super(id)
    end
  end
  private :__item_boolval_optkeys

  def __item_strval_optkeys(id)
    if id == 'debug'
      ['erasecolor']
    elsif id.kind_of?(Array)
      case id[0]
      when 'column'
        if id[1] == 'drag'
          ['indicatorcolor']
        else
          super(id) << 'textcolor'
        end
      when 'element'
        super(id) << 'fill' << 'outline' << 'format'
      else
        super(id)
      end
    else
      super(id)
    end
  end
  private :__item_strval_optkeys

  def __item_listval_optkeys(id)
    if id.kind_of?(Array)
      case id[0]
      when 'column'
        ['itembackground']
      when 'element'
        ['relief']
      when 'style'
        ['union']
      else
        if id[0].kind_of?(Array) && id[0][1] == 'element'
          ['relief']
        else
          []
        end
      end
    else
      []
    end
  end
  private :__item_listval_optkeys

  def __item_keyonly_optkeys(id)  # { def_key=>(undef_key|nil), ... }
    {
      'notreally'=>nil, 
      'increasing'=>'decreasing',
      'decreasing'=>'increasing', 
      'ascii'=>nil,
      'dictionary'=>nil, 
      'integer'=>nil, 
      'real'=>nil
    }
  end
  private :__item_keyonly_optkeys

  def column_cget(tagOrId, option)
    itemcget(['column', tagOrId], option)
  end
  def column_cget_strict(tagOrId, option)
    itemcget_strict(['column', tagOrId], option)
  end
  def column_configure(tagOrId, slot, value=None)
    itemconfigure(['column', tagOrId], slot, value)
  end
  def column_configinfo(tagOrId, slot=nil)
    itemconfiginfo(['column', tagOrId], slot)
  end
  def current_column_configinfo(tagOrId, slot=nil)
    current_itemconfiginfo(['column', tagOrId], slot)
  end

  def column_dragcget(option)
    itemcget(['column', 'drag'], option)
  end
  def column_dragcget_strict(option)
    itemcget_strict(['column', 'drag'], option)
  end
  def column_dragconfigure(slot, value=None)
    itemconfigure(['column', 'drag'], slot, value)
  end
  def column_dragconfiginfo(slot=nil)
    itemconfiginfo(['column', 'drag'], slot)
  end
  def current_column_dragconfiginfo(slot=nil)
    current_itemconfiginfo(['column', 'drag'], slot)
  end

  def debug_cget(option)
    itemcget('debug', option)
  end
  def debug_cget_strict(option)
    itemcget_strict('debug', option)
  end
  def debug_configure(slot, value=None)
    itemconfigure('debug', slot, value)
  end
  def debug_configinfo(slot=nil)
    itemconfiginfo('debug', slot)
  end
  def current_debug_configinfo(slot=nil)
    current_itemconfiginfo('debug', slot)
  end

  def dragimage_cget(option)
    itemcget('dragimage', option)
  end
  def dragimage_cget_strict(option)
    itemcget_strict('dragimage', option)
  end
  def dragimage_configure(slot, value=None)
    itemconfigure('dragimage', slot, value)
  end
  def dragimage_configinfo(slot=nil)
    itemconfiginfo('dragimage', slot)
  end
  def current_dragimage_configinfo(slot=nil)
    current_itemconfiginfo('dragimage', slot)
  end

  def element_cget(tagOrId, option)
    itemcget(['element', tagOrId], option)
  end
  def element_cget_strict(tagOrId, option)
    itemcget_strict(['element', tagOrId], option)
  end
  def element_configure(tagOrId, slot, value=None)
    itemconfigure(['element', tagOrId], slot, value)
  end
  def element_configinfo(tagOrId, slot=nil)
    itemconfiginfo(['element', tagOrId], slot)
  end
  def current_element_configinfo(tagOrId, slot=nil)
    current_itemconfiginfo(['element', tagOrId], slot)
  end

  def item_cget(tagOrId, option)
    itemcget(['item', tagOrId], option)
  end
  def item_cget_strict(tagOrId, option)
    itemcget_strict(['item', tagOrId], option)
  end
  def item_configure(tagOrId, slot, value=None)
    itemconfigure(['item', tagOrId], slot, value)
  end
  def item_configinfo(tagOrId, slot=nil)
    itemconfiginfo(['item', tagOrId], slot)
  end
  def current_item_configinfo(tagOrId, slot=nil)
    current_itemconfiginfo(['item', tagOrId], slot)
  end

  def item_element_cget(item, column, elem, option)
    itemcget([['item', 'element'], [item, column, elem]], option)
  end
  def item_element_cget_strict(item, column, elem, option)
    itemcget_strict([['item', 'element'], [item, column, elem]], option)
  end
  def item_element_configure(item, column, elem, slot, value=None)
    itemconfigure([['item', 'element'], [item, column, elem]], slot, value)
  end
  def item_element_configinfo(item, column, elem, slot=nil)
    itemconfiginfo([['item', 'element'], [item, column, elem]], slot)
  end
  def current_item_element_configinfo(item, column, elem, slot=nil)
    current_itemconfiginfo([['item', 'element'], [item, column, elem]], slot)
  end

  def marquee_cget(option)
    itemcget('marquee', option)
  end
  def marquee_cget_strict(option)
    itemcget_strict('marquee', option)
  end
  def marquee_configure(slot, value=None)
    itemconfigure('marquee', slot, value)
  end
  def marquee_configinfo(slot=nil)
    itemconfiginfo('marquee', slot)
  end
  def current_marquee_configinfo(slot=nil)
    current_itemconfiginfo('marquee', slot)
  end

  def notify_cget(win, pattern, option)
    pattern = "<#{pattern}>"
    # "notify" doesn't have cget subcommand.
    current_itemconfiginfo(['notify', [win, pattern]])[option.to_s]
  end
  def notify_cget_strict(win, pattern, option)
    pattern = "<#{pattern}>"
    # "notify" doesn't have cget subcommand.
    info = current_itemconfiginfo(['notify', [win, pattern]])
    option = option.to_s
    unless info.has_key?(option)
      fail RuntimeError, "unknown option \"#{option}\""
    else
      info[option]
    end
  end
  def notify_configure(win, pattern, slot, value=None)
    pattern = "<#{pattern}>"
    itemconfigure(['notify', [win, pattern]], slot, value)
  end
  def notify_configinfo(win, pattern, slot=nil)
    pattern = "<#{pattern}>"
    itemconfiginfo(['notify', [win, pattern]], slot)
  end
  def current_notify_configinfo(tagOrId, slot=nil)
    pattern = "<#{pattern}>"
    current_itemconfiginfo(['notify', [win, pattern]], slot)
  end

  def style_cget(tagOrId, option)
    itemcget(['style', tagOrId], option)
  end
  def style_cget_strict(tagOrId, option)
    itemcget_strict(['style', tagOrId], option)
  end
  def style_configure(tagOrId, slot, value=None)
    itemconfigure(['style', tagOrId], slot, value)
  end
  def style_configinfo(tagOrId, slot=nil)
    itemconfiginfo(['style', tagOrId], slot)
  end
  def current_style_configinfo(tagOrId, slot=nil)
    current_itemconfiginfo(['style', tagOrId], slot)
  end

  private :itemcget, :itemcget_strict
  private :itemconfigure, :itemconfiginfo, :current_itemconfiginfo
end

##############################################

class Tk::TreeCtrl
  include Tk::TreeCtrl::ConfigMethod
  include Scrollable

  TkCommandNames = ['treectrl'.freeze].freeze
  WidgetClassName = 'TreeCtrl'.freeze
  WidgetClassNames[WidgetClassName] = self

  #########################

  def __destroy_hook__
    Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.delete(@path)
    }
    Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.delete(@path)
    }
    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.delete(@path)
    }
    Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.delete(@path)
    }
  end

  #########################

  def __strval_optkeys
    super() + [
      'buttoncolor', 'columnprefix', 'itemprefix', 'linecolor'
    ]
  end
  private :__strval_optkeys

  def __boolval_optkeys
    [
      'itemwidthequal', 'usetheme', 
      'showbuttons', 'showheader', 'showlines', 'showroot', 
      'showrootbutton', 'showrootlines',
    ]
  end
  private :__boolval_optkeys

  def __listval_optkeys
    [ 'defaultstyle' ]
  end
  private :__listval_optkeys

  #########################

  def install_bind(cmd, *args)
    install_bind_for_event_class(Tk::TreeCtrl::NotifyEvent, cmd, *args)
  end

  #########################

  def create_self(keys)
    if keys and keys != None
      tk_call_without_enc(self.class::TkCommandNames[0], @path, 
                          *hash_kv(keys, true))
    else
      tk_call_without_enc(self.class::TkCommandNames[0], @path)
    end
  end
  private :create_self

  #########################

  def activate(desc)
    tk_send('activate', desc)
    self
  end

  def canvasx(x)
    number(tk_send('canvasx', x))
  end

  def canvasy(y)
    number(tk_send('canvasy', y))
  end

  def collapse(*dsc)
    tk_send_without_enc('collapse', *(dsc.map!{|d| _get_eval_string(d, true)}))
    self
  end

  def collapse_recurse(*dsc)
    tk_send_without_enc('collapse', '-recurse', 
                        *(dsc.map!{|d| _get_eval_string(d, true)}))
    self
  end

  def column_bbox(idx)
    list(tk_send('column', 'bbox', idx))
  end

  def column_compare(column1, op, column2)
    bool(tk_send('column', 'compare', column1, op, column2))
  end

  def column_count
    num_or_str(tk_send('column', 'count'))
  end

  def column_create(keys=nil)
    if keys && keys.kind_of?(Hash)
      num_or_str(tk_send('column', 'create', *hash_kv(keys)))
    else
      num_or_str(tk_send('column', 'create'))
    end
  end

  def column_delete(idx)
    Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[self.path]
        Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[self.path].delete(idx)
      end
    }
    tk_send('column', 'delete', idx)
    self
  end

  def column_index(idx)
    num_or_str(tk_send('column', 'index', idx))
  end

  def column_id(idx)
    tk_send('column', 'id', idx)
  end

  def column_list(visible=false)
    if visible
      simplelist(tk_send('column', 'list', '-visible'))
    else
      simplelist(tk_send('column', 'list'))
    end
  end
  def column_visible_list
    column_list(true)
  end

  def column_move(idx, before)
    tk_send('column', 'move', idx, before)
    self
  end

  def column_needed_width(idx)
    num_or_str(tk_send('column', 'neededwidth', idx))
  end
  alias column_neededwidth column_needed_width

  def column_order(column, visible=false)
    if visible
      num_or_str(tk_send('column', 'order', column, '-visible'))
    else
      num_or_str(tk_send('column', 'order', column))
    end
  end
  def column_visible_order(column)
    column_order(column, true)
  end

  def column_width(idx)
    num_or_str(tk_send('column', 'width', idx))
  end

  def compare(item1, op, item2)
    bool(tk_send('compare', item1, op, item2))
  end

  def contentbox()
    list(tk_send('contentbox'))
  end

  def depth(item=None)
    num_or_str(tk_send_without_enc('depth', _get_eval_string(item, true)))
  end

  def dragimage_add(item, *args)
    tk_send('dragimage', 'add', item, *args)
    self
  end

  def dragimage_clear()
    tk_send('dragimage', 'clear')
    self
  end

  def dragimage_offset(*args) # x, y
    if args.empty?
      list(tk_send('dragimage', 'offset'))
    else
      tk_send('dragimage', 'offset', *args)
      self
    end
  end

  def dragimage_visible(*args) # mode
    if args..empty?
      bool(tk_send('dragimage', 'visible'))
    else
      tk_send('dragimage', 'visible', *args)
      self
    end
  end
  def dragimage_visible?
    dragimage_visible()
  end

  def debug_dinfo
    tk_send('debug', 'dinfo')
    self
  end

  def debug_scroll
    tk_send('debug', 'scroll')
  end

  def element_create(elem, type, keys=nil)
    if keys && keys.kind_of?(Hash)
      tk_send('element', 'create', elem, type, *hash_kv(keys))
    else
      tk_send('element', 'create', elem, type)
    end
  end

  def element_delete(*elems)
    Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[self.path]
        elems.each{|elem|
          Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[self.path].delete(elem)
        }
      end
    }
    tk_send('element', 'delete', *elems)
    self
  end

  def element_names()
    list(tk_send('element', 'names')).collect!{|elem|
      Tk::TreeCtrl::Element.id2obj(self, elem)
    }
  end

  def _conv_element_perstate_val(opt, val)
    case opt
    when 'background', 'foreground', 'fill', 'outline', 'format'
      val
    when 'draw', 'filled', 'showfocus', 'destroy'
      bool(val)
    else
      tk_tcl2ruby(val)
    end
  end
  private :_conv_element_perstate_val

  def element_perstate(elem, opt, st_list)
    tk_send('element', 'perstate', elem, "-{opt}", st_list)
  end

  def element_type(elem)
    tk_send('element', 'type', elem)
  end

  def element_class(elem)
    Tk::TreeCtrl::Element.type2class(element_type(elem))
  end

  def expand(*dsc)
    tk_send('expand', *dsc)
    self
  end

  def expand_recurse(*dsc)
    tk_send('expand', '-recurse', *dsc)
    self
  end

  def identify(x, y)
    lst = list(tk_send('identify', x, y))

    if lst[0] == 'item'
      lst[1] = Tk::TreeCtrl::Item.id2obj(self, lst[1])
      size = lst.size
      i = 2
      while i < size
        case lst[i]
        when 'line'
          i += 1
          lst[i] = Tk::TreeCtrl::Item.id2obj(self, lst[i])
          i += 1

        when 'button'
          i += 1

        when 'column'
          i += 2

        when 'elem'
          i += 1
          lst[i] = Tk::TreeCtrl::Element.id2obj(self, lst[i])
          i += 1

        else
          i += 1
        end
      end
    end

    lst
  end

  def index(idx)
    num_or_str(tk_send('index', idx))
  end

  def item_ancestors(item)
    list(tk_send('item', 'ancestors', item)).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def item_bbox(item, *args)
    list(tk_send('item', 'bbox', item, *args))
  end

  def item_children(item)
    list(tk_send('item', 'children', item)).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def item_collapse(item)
    tk_send_without_enc('item', 'collapse', _get_eval_string(item, true))
    self
  end

  def item_collapse_recurse(item)
    tk_send_without_enc('item', 'collapse', 
                        _get_eval_string(item, true), '-recurse')
    self
  end

  def item_compare(item1, op, item2)
    bool(tk_send('item', 'compare', item1, op, item2))
  end

  def item_complex(item, *args)
    tk_send_without_enc('item', 'complex', 
                        _get_eval_string(item, true), 
                        *(args.map!{|arg| _get_eval_string(arg, true)}))
    self
  end

  def item_count
    num_or_str(tk_send('item', 'count'))
  end

  def item_create(keys={})
    num_or_str(tk_send_without_enc('item', 'create', *hash_kv(keys, true)))
  end

  def _erase_children(item)
    item_children(item).each{|i| _erase_children(i)}
    # table is already locked
    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[self.path].delete(item)
  end
  private :_erase_children

  def item_delete(first, last=None)
    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[self.path]
        if first == 'all' || first == :all || last == 'all' || last == :all
          Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[self.path].clear
        elsif last == None
          _erase_children(first)
        else
          self.range(first, last).each{|id|
            _erase_children(id)
          }
        end
      end
    }
    tk_send('item', 'delete', first, last)
    self
  end

  def item_dump(item)
    list(tk_send('item', 'dump', item))
  end

  def item_dump_hash(item)
    Hash[*list(tk_send('item', 'dump', item))]
  end

  def item_element_actual(item, column, elem, key)
    tk_send('item', 'element', 'actual', item, column, elem, "-#{key}")
  end

  def item_element_perstate(elem, opt, st_list)
    tk_send('item', 'element', 'perstate', elem, "-{opt}", st_list)
  end

  def item_expand(item)
    tk_send('item', 'expand', item)
    self
  end

  def item_expand_recurse(item)
    tk_send('item', 'expand', item, '-recurse')
    self
  end

  def item_firstchild(parent, child=nil)
    if child
      tk_send_without_enc('item', 'firstchild', 
                          _get_eval_string(parent, true), 
                          _get_eval_string(child, true))
      self
    else
      id = num_or_str(tk_send_without_enc('item', 'firstchild', 
                                          _get_eval_string(parent, true)))
      Tk::TreeCtrl::Item.id2obj(self, id)
    end
  end
  alias item_first_child item_firstchild

  def item_hasbutton(item, st=None)
    if st == None
      bool(tk_send_without_enc('item', 'hasbutton', 
                               _get_eval_string(item, true)))
    else
      tk_send_without_enc('item', 'hasbutton', 
                          _get_eval_string(item, true), 
                          _get_eval_string(st))
      self
    end
  end
  alias item_has_button item_hasbutton

  def item_hasbutton?(item)
    item_hasbutton(item)
  end
  alias item_has_button? item_hasbutton?

  def item_id(item)
    tk_send('item', 'id', item)
  end

  def item_image(item, column=nil, *args)
    if args.empty?
      if column
        img = tk_send('item', 'image', item, column)
        TkImage::Tk_IMGTBL[img]? TkImage::Tk_IMGTBL[img] : img
      else
        simplelist(tk_send('item', 'image', item)).collect!{|img|
          TkImage::Tk_IMGTBL[img]? TkImage::Tk_IMGTBL[img] : img
        }
      end
    else
      tk_send('item', 'image', item, column, *args)
      self
    end
  end
  def get_item_image(item, column=nil)
    item_image(item, column)
  end
  def set_item_image(item, col, img, *args)
    item_image(item, col, img, *args)
  end

  def item_index(item)
    list(tk_send('item', 'index', item))
  end

  def item_isancestor(item, des)
    bool(tk_send('item', 'isancestor', item, des))
  end
  alias item_is_ancestor  item_isancestor
  alias item_isancestor?  item_isancestor
  alias item_is_ancestor? item_isancestor

  def item_isopen(item)
    bool(tk_send('item', 'isopen', item))
  end
  alias item_is_open    item_isopen
  alias item_isopen?    item_isopen
  alias item_is_open?   item_isopen
  alias item_isopened?  item_isopen
  alias item_is_opened? item_isopen

  def item_lastchild(parent, child=nil)
    if child
      tk_send_without_enc('item', 'lastchild', 
                          _get_eval_string(parent, true),
                          _get_eval_string(child, true))
      self
    else
      id = num_or_str(tk_send_without_enc('item', 'lastchild', 
                                          _get_eval_string(parent, true)))
      Tk::TreeCtrl::Item.id2obj(self, id)
    end
  end
  alias item_last_child item_lastchild

  def item_nextsibling(sibling, nxt=nil)
    if nxt
      tk_send('item', 'nextsibling', sibling, nxt)
      self
    else
      id = num_or_str(tk_send('item', 'nextsibling', sibling))
      Tk::TreeCtrl::Item.id2obj(self, id)
    end
  end
  alias item_next_sibling item_nextsibling

  def item_numchildren(item)
    number(tk_send_without_enc('item', 'numchildren', 
                               _get_eval_string(item, true)))
  end
  alias item_num_children  item_numchildren
  alias item_children_size item_numchildren

  def item_order(item, visible=false)
    if visible
      ret = num_or_str(tk_send('item', 'order', item, '-visible'))
    else
      ret = num_or_str(tk_send('item', 'order', item))
    end

    (ret.kind_of?(Fixnum) && ret < 0)? nil: ret
  end
  def item_visible_order(item)
    item_order(item, true)
  end

  def item_parent(item)
    id = num_or_str(tk_send('item', 'parent', item))
    Tk::TreeCtrl::Item.id2obj(self, id)
  end

  def item_prevsibling(sibling, prev=nil)
    if prev
      tk_send('item', 'prevsibling', sibling, prev)
      self
    else
      id = num_or_str(tk_send('item', 'prevsibling', sibling))
      Tk::TreeCtrl::Item.id2obj(self, id)
    end
  end
  alias item_prev_sibling item_prevsibling

  def item_range(first, last)
    simplelist(tk_send('item', 'range', first, last))
  end

  def item_remove(item)
    tk_send('item', 'remove', item)
    self
  end

  def item_rnc(item)
    list(tk_send('item', 'rnc', item))
  end

  def _item_sort_core(real_sort, item, *opts)
    # opts ::= sort_param [, sort_param, ... ]
    # sort_param ::= {key=>val, ...}
    #                [type, desc, {key=>val, ...}]
    #                param
    opts = opts.collect{|param|
      if param.kind_of?(Hash)
        param = _symbolkey2str(param)
        if param.key?('column')
          key = '-column'
          desc = param.delete('column')
        elsif param.key?('element')
          key = '-element'
          desc = param.delete('element')
        else
          key = nil
        end

        if param.empty?
          param = None
        else
          param = hash_kv(__conv_item_keyonly_opts(item, param))
        end

        if key
          [key, desc].concat(param)
        else
          param
        end

      elsif param.kind_of?(Array)
        if param[2].kind_of?(Hash)
          param[2] = hash_kv(__conv_item_keyonly_opts(item, param[2]))
        end
        param

      elsif param.kind_of?(String) && param =~ /\A[a-z]+\Z/
        '-' << param

      elsif param.kind_of?(Symbol)
        '-' << param.to_s

      else
        param
      end
    }.flatten

    if real_sort
      tk_send('item', 'sort', item, *opts)
      self
    else
      list(tk_send('item', 'sort', item, '-notreally', *opts))
    end
  end
  private :_item_sort_core

  def item_sort(item, *opts)
    _item_sort_core(true, item, *opts)
  end
  def item_sort_not_really(item, *opts)
    _item_sort_core(false, item, *opts)
  end

  def item_span(item, column=nil, *args)
    if args.empty?
      if column
        list(tk_send('item', 'span', item, column))
      else
        simplelist(tk_send('item', 'span', item)).collect!{|elem| list(elem)}
      end
    else
      tk_send('item', 'span', item, column, *args)
      self
    end
  end
  def get_item_span(item, column=nil)
    item_span(item, column)
  end
  def set_item_span(item, col, num, *args)
    item_span(item, col, num, *args)
  end

  def item_state_forcolumn(item, column, *args)
    tk_send('item', 'state', 'forcolumn', item, column, *args)
  end
  alias item_state_for_column item_state_forcolumn

  def item_state_get(item, *args)
    if args.empty?
      list(tk_send('item', 'state', 'get', item *args))
    else
      bool(tk_send('item', 'state', 'get', item))
    end
  end

  def item_state_set(item, *args)
    tk_send('item', 'state', 'set', item, *args)
  end

  def item_style_elements(item, column)
    list(tk_send('item', 'style', 'elements', item, column)).collect!{|id|
      Tk::TreeCtrl::Style.id2obj(self, id)
    }
  end

  def item_style_map(item, column, style, map)
    tk_send('item', 'style', 'map', item, column, style, map)
    self
  end

  def item_style_set(item, column=nil, *args)
    if args.empty?
      if column
        id = tk_send_without_enc('item', 'style', 'set', 
                                 _get_eval_string(item, true), 
                                 _get_eval_string(column, true))
        Tk::TreeCtrl::Style.id2obj(self, id)
      else
        list(tk_send_without_enc('item', 'style', 'set', 
                                 _get_eval_string(item, true))).collect!{|id|
          Tk::TreeCtrl::Style.id2obj(self, id)
        }
      end
    else
      tk_send_without_enc('item', 'style', 'set', 
                          _get_eval_string(item, true), 
                          _get_eval_string(column, true), 
                          *(args.flatten.map!{|arg|
                              _get_eval_string(arg, true)
                            }))
      self
    end
  end

  def item_text(item, column, txt=nil, *args)
    if args.empty?
      if txt
        tk_send('item', 'text', item, column, txt)
        self
      else
        tk_send('item', 'text', item, column)
      end
    else
      tk_send('item', 'text', item, column, txt, *args)
      self
    end
  end

  def item_toggle(item)
    tk_send('item', 'toggle', item)
    self
  end

  def item_toggle_recurse(item)
    tk_send('item', 'toggle', item, '-recurse')
    self
  end

  def item_visible(item, st=None)
    if st == None
      bool(tk_send('item', 'visible', item))
    else
      tk_send('item', 'visible', item, st)
      self
    end
  end
  def item_visible?(item)
    item_visible(item)
  end

  def marquee_anchor(*args)
    if args.empty?
      list(tk_send('marquee', 'anchor'))
    else
      tk_send('marquee', 'anchor', *args)
      self
    end
  end

  def marquee_coords(*args)
    if args.empty?
      list(tk_send('marquee', 'coords'))
    else
      tk_send('marquee', 'coords', *args)
      self
    end
  end

  def marquee_corner(*args)
    if args.empty?
      tk_send('marquee', 'corner')
    else
      tk_send('marquee', 'corner', *args)
      self
    end
  end

  def marquee_identify()
    list(tk_send('marquee', 'identify')).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def marquee_visible(st=None)
    if st == None
      bool(tk_send('marquee', 'visible'))
    else
      tk_send('marquee', 'visible', st)
      self
    end
  end
  def marquee_visible?()
    marquee_visible()
  end

  #def notify_bind(obj, event, cmd=Proc.new, *args)
  #  _bind([@path, 'notify', 'bind', obj], event, cmd, *args)
  #  self
  #end
  def notify_bind(obj, event, *args)
    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
    if TkComm._callback_entry?(args[0]) || !block_given?
      cmd = args.shift
    else
      cmd = Proc.new
    end
    _bind([@path, 'notify', 'bind', obj], event, cmd, *args)
    self
  end

  #def notify_bind_append(obj, event, cmd=Proc.new, *args)
  #  _bind_append([@path, 'notify', 'bind', obj], event, cmd, *args)
  #  self
  #end
  def notify_bind_append(obj, event, *args)
    # if args[0].kind_of?(Proc) || args[0].kind_of?(Method)
    if TkComm._callback_entry?(args[0]) || !block_given?
      cmd = args.shift
    else
      cmd = Proc.new
    end
    _bind_append([@path, 'notify', 'bind', obj], event, cmd, *args)
    self
  end

  def notify_bind_remove(obj, event)
    _bind_remove([@path, 'notify', 'bind', obj], event)
    self
  end

  def notify_bindinfo(obj, event=nil)
    _bindinfo([@path, 'notify', 'bind', obj], event)
  end

  def notify_detailnames(event)
    list(tk_send('notify', 'detailnames', event))
  end

  def notify_eventnames()
    list(tk_send('notify', 'eventnames'))
  end

  def notify_generate(pattern, char_map=None, percents_cmd=None)
    pattern = "<#{pattern}>"
    tk_send('notify', 'generate', pattern, char_map, percents_cmd)
    self
  end

  def notify_install(pattern, percents_cmd=nil, &b)
    pattern = "<#{pattern}>"
    percents_cmd = Proc.new(&b) if !percents_cmd && b
    if percents_cmd
      procedure(tk_send('notify', 'install', pattern, percents_cmd))
    else
      procedure(tk_send('notify', 'install', pattern))
    end
  end

  def notify_install_detail(event, detail, percents_cmd=nil, &b)
    percents_cmd = Proc.new(&b) if !percents_cmd && b
    if percents_cmd
      tk_send('notify', 'install', 'detail', event, detail, percents_cmd)
    else
      tk_send('notify', 'install', 'detail', event, detail)
    end
  end

  def notify_install_event(event, percents_cmd=nil, &b)
    percents_cmd = Proc.new(&b) if !percents_cmd && b
    if percents_cmd
      tk_send('notify', 'install', 'event', event, percents_cmd)
    else
      tk_send('notify', 'install', 'event', event)
    end
  end

  def notify_linkage(pattern, detail=None)
    if detail != None
      tk_send('notify', 'linkage', pattern, detail)
    else
      begin
        if pattern.to_s.index(?-)
          # TreeCtrl 1.1 format?
          begin
            tk_send('notify', 'linkage', "<#{pattern}>")
          rescue
            # TreeCtrl 1.0?
            tk_send('notify', 'linkage', pattern)
          end
        else
          # TreeCtrl 1.0 format?
          begin
            tk_send('notify', 'linkage', pattern)
          rescue
            # TreeCtrl 1.1?
            tk_send('notify', 'linkage', "<#{pattern}>")
          end
        end
      end
    end
  end

  def notify_unbind(pattern=nil)
    if pattern
      tk_send('notify', 'unbind', "<#{pattern}>")
    else
      tk_send('notify', 'unbind')
    end
    self
  end

  def notify_uninstall(pattern)
    pattern = "<#{pattern}>"
    tk_send('notify', 'uninstall', pattern)
    self
  end

  def notify_uninstall_detail(event, detail)
    tk_send('notify', 'uninstall', 'detail', event, detail)
    self
  end

  def notify_uninstall_event(event)
    tk_send('notify', 'uninstall', 'event', event)
    self
  end

  def numcolumns()
    num_or_str(tk_send('numcolumns'))
  end
  alias num_columns  numcolumns
  alias columns_size numcolumns

  def numitems()
    num_or_str(tk_send('numitems'))
  end
  alias num_items  numitems
  alias items_size numitems

  def orphans()
    list(tk_send('orphans')).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def range(first, last)
    list(tk_send('range', first, last)).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def state_define(name)
    tk_send('state', 'define', name)
    self
  end

  def state_linkage(name)
    tk_send('state', 'linkage', name)
  end

  def state_names()
    list(tk_send('state', 'names'))
  end

  def state_undefine(*names)
    tk_send('state', 'undefine', *names)
    self
  end

  def see(item)
    tk_send('see', item)
    self
  end

  def selection_add(first, last=None)
    tk_send('selection', 'add', first, last)
    self
  end

  def selection_anchor(item=None)
    id = num_or_str(tk_send('selection', 'anchor', item))
    Tk::TreeCtrl::Item.id2obj(self, id)
  end

  def selection_clear(*args) # first, last
    tk_send('selection', 'clear', *args)
    self
  end

  def selection_count()
    number(tk_send('selection', 'count'))
  end

  def selection_get()
    list(tk_send('selection', 'get')).collect!{|id|
      Tk::TreeCtrl::Item.id2obj(self, id)
    }
  end

  def selection_includes(item)
    bool(tk_send('selection', 'includes', item))
  end

  def selection_modify(sel, desel)
    tk_send('selection', 'modify', sel, desel)
    self
  end

  def style_create(style, keys=None)
    if keys && keys != None
      tk_send('style', 'create', style, *hash_kv(keys))
    else
      tk_send('style', 'create', style)
    end
  end

  def style_delete(*args)
    Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[self.path]
        args.each{|sty|
          Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[self.path].delete(sty)
        }
      end
    }
    tk_send('style', 'delete', *args)
    self
  end

  def style_elements(style, *elems)
    if elems.empty?
      list(tk_send('style', 'elements', style)).collect!{|id|
        Tk::TreeCtrl::Element.id2obj(self, id)
      }
    else
      tk_send('style', 'elements', style, elems.flatten)
      self
    end
  end

  def _conv_style_layout_val(sty, val)
    case sty.to_s
    when 'padx', 'pady', 'ipadx', 'ipady'
      lst = list(val)
      (lst.size == 1)? lst[0]: lst
    when 'detach', 'indent'
      bool(val)
    when 'union'
      simplelist(val).collect!{|elem|
        Tk::TreeCtrl::Element.id2obj(self, elem)
      }
    else
      val
    end
  end
  private :_conv_style_layout_val

  def style_layout(style, elem, keys=None)
    if keys && keys != None
      if keys.kind_of?(Hash)
        tk_send('style', 'layout', style, elem, *hash_kv(keys))
        self
      else
        _conv_style_layout_val(keys, 
                               tk_send('style', 'layout', 
                                       style, elem, "-#{keys}"))
      end
    else
      ret = Hash.new
      Hash[*simplelist(tk_send('style', 'layout', style, elem))].each{|k, v|
        k = k[1..-1]
        ret[k] = _conv_style_layout_val(k, v)
      }
      ret
    end
  end
  def get_style_layout(style, elem, opt=None)
    style_layout(style, elem, opt)
  end
  def set_style_layout(style, elem, slot, value=None)
    if slot.kind_of?(Hash)
      style_layout(style, elem, slot)
    else
      style_layout(style, elem, {slot=>value})
    end
  end

  def style_names()
    list(tk_send('style', 'names')).collect!{|id|
      Tk::TreeCtrl::Style.id2obj(self, id)
    }
  end

  def toggle(*items)
    tk_send('toggle', *items)
    self
  end

  def toggle_recurse()
    tk_send('toggle', '-recurse', *items)
    self
  end
end

#####################

class Tk::TreeCtrl::Column < TkObject
  TreeCtrlColumnID_TBL = TkCore::INTERP.create_table

  (TreeCtrlColumnID = ['treectrl_column'.freeze, '00000'.taint]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

  TkCore::INTERP.init_ip_env{
    Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.clear
    }
  }

  def self.id2obj(tree, id)
    tpath = tree.path
    Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[tpath]
        Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[tpath][id]? \
                   Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[tpath][id] : id
      else
        id
      end
    }
  end

  def initialize(parent, keys={})
    @tree = parent
    @tpath = parent.path

    keys = _symbolkey2str(keys)

    Tk::TreeCtrl::Column::TreeCtrlColumnID.mutex.synchronize{
      @path = @id = 
        keys.delete('tag') ||
        Tk::TreeCtrl::Column::TreeCtrlColumnID.join(TkCore::INTERP._ip_id_)
      Tk::TreeCtrl::Column::TreeCtrlColumnID[1].succ!
    }

    keys['tag'] = @id

    Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[@tpath] ||= {} 
      Tk::TreeCtrl::Column::TreeCtrlColumnID_TBL[@tpath][@id] = self
    }

    @tree.column_create(keys)
  end

  def id
    @id
  end

  def to_s
    @id.to_s.dup
  end

  def cget(opt)
    @tree.column_cget(@tree.column_index(@id), opt)
  end
  def cget_strict(opt)
    @tree.column_cget_strict(@tree.column_index(@id), opt)
  end

  def configure(*args)
    @tree.column_configure(@tree.column_index(@id), *args)
  end

  def configinfo(*args)
    @tree.column_configinfo(@tree.column_index(@id), *args)
  end

  def current_configinfo(*args)
    @tree.current_column_configinfo(@tree.column_index(@id), *args)
  end

  def delete
    @tree.column_delete(@tree.column_index(@id))
    self
  end

  def index
    @tree.column_index(@id)
  end

  def move(before)
    @tree.column_move(@tree.column_index(@id), before)
    self
  end

  def needed_width
    @tree.column_needed_width(@tree.column_index(@id))
  end
  alias neededwidth needed_width

  def current_width
    @tree.column_width(@tree.column_index(@id))
  end
end

#####################

class Tk::TreeCtrl::Element < TkObject
  TreeCtrlElementID_TBL = TkCore::INTERP.create_table

  (TreeCtrlElementID = ['treectrl_element'.freeze, '00000'.taint]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }
  TreeCtrlElemTypeToClass = {}

  TkCore::INTERP.init_ip_env{
    Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.clear
    }
  }

  def self.type2class(type)
    TreeCtrlElemTypeToClass[type] || type
  end

  def self.id2obj(tree, id)
    tpath = tree.path
    Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[tpath]
        Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[tpath][id]? \
                 Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[tpath][id] : id
      else
        id
      end
    }
  end

  def initialize(parent, type, keys=nil)
    @tree = parent
    @tpath = parent.path
    @type = type.to_s
    Tk::TreeCtrl::Element::TreeCtrlElementID.mutex.synchronize{
      @path = @id = 
        Tk::TreeCtrl::Element::TreeCtrlElementID.join(TkCore::INTERP._ip_id_)
      Tk::TreeCtrl::Element::TreeCtrlElementID[1].succ!
    }

    Tk::TreeCtrl::Element::TreeCtrlElementID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[@tpath] ||= {} 
      Tk::TreeCtrl::Element::TreeCtrlElementID_TBL[@tpath][@id] = self
    }

    @tree.element_create(@id, @type, keys)
  end

  def id
    @id
  end

  def to_s
    @id.dup
  end

  def cget(opt)
    @tree.element_cget(@id, opt)
  end
  def cget_strict(opt)
    @tree.element_cget_strict(@id, opt)
  end

  def configure(*args)
    @tree.element_configure(@id, *args)
  end

  def configinfo(*args)
    @tree.element_configinfo(@id, *args)
  end

  def current_configinfo(*args)
    @tree.current_element_configinfo(@id, *args)
  end

  def delete
    @tree.element_delete(@id)
    self
  end

  def element_type
    @tree.element_type(@id)
  end

  def element_class
    @tree.element_class(@id)
  end
end

class Tk::TreeCtrl::BitmapElement < Tk::TreeCtrl::Element
  TreeCtrlElemTypeToClass['bitmap'] = self

  def initialize(parent, keys=nil)
    super(parent, 'bitmap', keys)
  end
end

class Tk::TreeCtrl::BorderElement < Tk::TreeCtrl::Element
  TreeCtrlElemTypeToClass['border'] = self

  def initialize(parent, keys=nil)
    super(parent, 'border', keys)
  end
end

class Tk::TreeCtrl::ImageElement < Tk::TreeCtrl::Element
  TreeCtrlElemTypeToClass['image'] = self

  def initialize(parent, keys=nil)
    super(parent, 'image', keys)
  end
end

class Tk::TreeCtrl::RectangleElement < Tk::TreeCtrl::Element
  TreeCtrlElemTypeToClass['rect'] = self

  def initialize(parent, keys=nil)
    super(parent, 'rect', keys)
  end
end

#####################

class Tk::TreeCtrl::Item < TkObject
  TreeCtrlItemID_TBL = TkCore::INTERP.create_table

  TkCore::INTERP.init_ip_env{
    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.clear
    }
  }

  def self.id2obj(tree, id)
    tpath = tree.path
    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[tpath]
        Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[tpath][id]? \
                        Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[tpath][id] : id
      else
        id
      end
    }
  end

  def initialize(parent, keys={})
    @tree = parent
    @tpath = parent.path
    @path = @id = @tree.item_create(keys)

    Tk::TreeCtrl::Item::TreeCtrlItemID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[@tpath] ||= {} 
      Tk::TreeCtrl::Item::TreeCtrlItemID_TBL[@tpath][@id] = self
    }
  end

  def id
    @id
  end

  def to_s
    @id.to_s.dup
  end

  def ancestors
    @tree.item_ancestors(@id)
  end

  def bbox(*args)
    @tree.item_bbox(@id, *args)
  end

  def children
    @tree.item_children(@id)
  end

  def collapse
    @tree.item_collapse(@id)
    self
  end

  def collapse_recurse
    @tree.item_collapse_recurse(@id)
    self
  end

  def complex(*args)
    @tree.item_complex(@id, *args)
    self
  end

  def cget(opt)
    @tree.item_cget(@id, opt)
  end
  def cget_strict(opt)
    @tree.item_cget_strict(@id, opt)
  end

  def configure(*args)
    @tree.item_configure(@id, *args)
  end

  def configinfo(*args)
    @tree.item_configinfo(@id, *args)
  end

  def current_configinfo(*args)
    @tree.current_item_configinfo(@id, *args)
  end

  def delete
    @tree.item_delete(@id)
    self
  end

  def element_dump
    @tree.item_dump(@id)
  end

  def element_dump_hash
    @tree.item_dump_hash(@id)
  end

  def element_actual(column, elem, key)
    @tree.item_element_actual(@id, column, elem, key)
  end

  def element_cget(opt)
    @tree.item_element_cget(@id, opt)
  end
  def element_cget_strict(opt)
    @tree.item_element_cget_strict(@id, opt)
  end

  def element_configure(*args)
    @tree.item_element_configure(@id, *args)
  end

  def element_configinfo(*args)
    @tree.item_element_configinfo(@id, *args)
  end

  def current_element_configinfo(*args)
    @tree.current_item_element_configinfo(@id, *args)
  end

  def expand
    @tree.item_expand(@id)
    self
  end

  def expand_recurse
    @tree.item_expand_recurse(@id)
    self
  end

  def firstchild(child=nil)
    if child
      @tree.item_firstchild(@id, child)
      self
    else
      @tree.item_firstchild(@id)
    end
  end
  alias first_child firstchild

  def hasbutton(st=None)
    if st == None
      @tree.item_hasbutton(@id)
    else
      @tree.item_hasbutton(@id, st)
      self
    end
  end
  alias has_button hasbutton

  def hasbutton?
    @tree.item_hasbutton(@id)
  end
  alias has_button? hasbutton?

  def index
    @tree.item_index(@id)
  end

  def isancestor(des)
    @tree.item_isancestor(@id, des)
  end
  alias is_ancestor  isancestor
  alias isancestor?  isancestor
  alias is_ancestor? isancestor
  alias ancestor?    isancestor

  def isopen
    @tree.item_isopen(@id)
  end
  alias is_open    isopen
  alias isopen?    isopen
  alias is_open?   isopen
  alias isopened?  isopen
  alias is_opened? isopen
  alias open?      isopen

  def lastchild(child=nil)
    if child
      @tree.item_lastchild(@id, child)
      self
    else
      @tree.item_lastchild(@id)
    end
  end
  alias last_child lastchild

  def nextsibling(nxt=nil)
    if nxt
      @tree.item_nextsibling(@id, nxt)
      self
    else
      @tree.item_nextsibling(@id)
    end
  end
  alias next_sibling nextsibling

  def numchildren
    @tree.item_numchildren(@id)
  end
  alias num_children  numchildren
  alias children_size numchildren

  def parent_index
    @tree.item_parent(@id)
  end

  def prevsibling(nxt=nil)
    if nxt
      @tree.item_prevsibling(@id, nxt)
      self
    else
      @tree.item_prevsibling(@id)
    end
  end
  alias prev_sibling prevsibling

  def remove
    @tree.item_remove(@id)
  end

  def rnc
    @tree.item_rnc(@id)
  end

  def sort(*opts)
    @tree.item_sort(@id, *opts)
  end
  def sort_not_really(*opts)
    @tree.item_sort_not_really(@id, *opts)
    self
  end

  def state_forcolumn(column, *args)
    @tree.item_state_forcolumn(@id, column, *args)
    self
  end
  alias state_for_column state_forcolumn

  def state_get(*args)
    @tree.item_state_get(@id, *args)
  end

  def state_set(*args)
    @tree.item_state_set(@id, *args)
    self
  end

  def style_elements(column)
    @tree.item_style_elements(@id, column)
  end

  def style_map(column, style, map)
    @tree.item_style_map(@id, column, style, map)
    self
  end

  def style_set(column=nil, *args)
    if args.empty?
      @tree.item_style_set(@id, column)
    else
      @tree.item_style_set(@id, column, *args)
      self
    end
  end

  def item_text(column, txt=nil, *args)
    if args.empty?
      if txt
        @tree.item_text(@id, column, txt)
        self
      else
        @tree.item_text(@id, column)
      end
    else
      @tree.item_text(@id, column, txt, *args)
      self
    end
  end

  def toggle
    @tree.item_toggle(@id)
    self
  end

  def toggle_recurse
    @tree.item_toggle_recurse(@id)
    self
  end

  def visible(st=None)
    if st == None
      @tree.item_visible(@id)
    else
      @tree.item_visible(@id, st)
      self
    end
  end
end

#####################

class Tk::TreeCtrl::Style < TkObject
  TreeCtrlStyleID_TBL = TkCore::INTERP.create_table

  (TreeCtrlStyleID = ['treectrl_style'.freeze, '00000'.taint]).instance_eval{
    @mutex = Mutex.new
    def mutex; @mutex; end
    freeze
  }

  TkCore::INTERP.init_ip_env{
    Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.clear
    }
  }

  def self.id2obj(tree, id)
    tpath = tree.path
    Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.mutex.synchronize{
      if Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[tpath]
        Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[tpath][id]? \
                     Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[tpath][id] : id
      else
        id
      end
    }
  end

  def initialize(parent, keys=nil)
    @tree = parent
    @tpath = parent.path

    Tk::TreeCtrl::Style::TreeCtrlStyleID.mutex.synchronize{
      @path = @id = 
        Tk::TreeCtrl::Style::TreeCtrlStyleID.join(TkCore::INTERP._ip_id_)
      Tk::TreeCtrl::Style::TreeCtrlStyleID[1].succ!
    }

    Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL.mutex.synchronize{
      Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[@tpath] ||= {} 
      Tk::TreeCtrl::Style::TreeCtrlStyleID_TBL[@tpath][@id] = self
    }

    @tree.style_create(@id, keys)
  end

  def id
    @id
  end

  def to_s
    @id.dup
  end

  def cget(opt)
    @tree.style_cget(@id, opt)
  end
  def cget_strict(opt)
    @tree.style_cget_strict(@id, opt)
  end

  def configure(*args)
    @tree.style_configure(@id, *args)
  end

  def configinfo(*args)
    @tree.style_configinfo(@id, *args)
  end

  def current_configinfo(*args)
    @tree.current_style_configinfo(@id, *args)
  end

  def delete
    @tree.style_delete(@id)
    self
  end

  def elements(*elems)
    if elems.empty?
      @tree.style_elements(@id)
    else
      @tree.style_elements(@id, *elems)
      self
    end
  end

  def layout(elem, keys=None)
    if keys && keys != None && keys.kind_of?(Hash)
      @tree.style_layout(@id, elem, keys)
      self
    else
      @tree.style_layout(@id, elem, keys)
    end
  end
end

module Tk::TreeCtrl::BindCallback
  include Tk
  extend Tk
end

class << Tk::TreeCtrl::BindCallback
  def percentsCmd(*args)
    tk_call('::TreeCtrl::PercentsCmd', *args)
  end
  def cursorCheck(w, x, y)
    tk_call('::TreeCtrl::CursorCheck', w, x, y)
  end
  def cursorCheckAux(w)
    tk_call('::TreeCtrl::CursorCheckAux', w)
  end
  def cursorCancel(w)
    tk_call('::TreeCtrl::CursorCancel', w)
  end
  def buttonPress1(w, x, y)
    tk_call('::TreeCtrl::ButtonPress1', w, x, y)
  end
  def doubleButton1(w, x, y)
    tk_call('::TreeCtrl::DoubleButton1', w, x, y)
  end
  def motion1(w, x, y)
    tk_call('::TreeCtrl::Motion1', w, x, y)
  end
  def leave1(w, x, y)
    tk_call('::TreeCtrl::Leave1', w, x, y)
  end
  def release1(w, x, y)
    tk_call('::TreeCtrl::Release1', w, x, y)
  end
  def beginSelect(w, el)
    tk_call('::TreeCtrl::BeginSelect', w, el)
  end
  def motion(w, le)
    tk_call('::TreeCtrl::Motion', w, el)
  end
  def beginExtend(w, el)
    tk_call('::TreeCtrl::BeginExtend', w, el)
  end
  def beginToggle(w, el)
    tk_call('::TreeCtrl::BeginToggle', w, el)
  end
  def cancelRepeat
    tk_call('::TreeCtrl::CancelRepeat')
  end
  def autoScanCheck(w, x, y)
    tk_call('::TreeCtrl::AutoScanCheck', w, x, y)
  end
  def autoScanCheckAux(w)
    tk_call('::TreeCtrl::AutoScanCheckAux', w)
  end
  def autoScanCancel(w)
    tk_call('::TreeCtrl::AutoScanCancel', w)
  end
  def up_down(w, n)
    tk_call('::TreeCtrl::UpDown', w, n)
  end
  def left_right(w, n)
    tk_call('::TreeCtrl::LeftRight', w, n)
  end
  def setActiveItem(w, idx)
    tk_call('::TreeCtrl::SetActiveItem', w, idx)
  end
  def extendUpDown(w, amount)
    tk_call('::TreeCtrl::ExtendUpDown', w, amount)
  end
  def dataExtend(w, el)
    tk_call('::TreeCtrl::DataExtend', w, el)
  end
  def cancel(w)
    tk_call('::TreeCtrl::Cancel', w)
  end
  def selectAll(w)
    tk_call('::TreeCtrl::selectAll', w)
  end
  def marqueeBegin(w, x, y)
    tk_call('::TreeCtrl::MarqueeBegin', w, x, y)
  end
  def marqueeUpdate(w, x, y)
    tk_call('::TreeCtrl::MarqueeUpdate', w, x, y)
  end
  def marqueeEnd(w, x, y)
    tk_call('::TreeCtrl::MarqueeEnd', w, x, y)
  end
  def scanMark(w, x, y)
    tk_call('::TreeCtrl::ScanMark', w, x, y)
  end
  def scanDrag(w, x, y)
    tk_call('::TreeCtrl::ScanDrag', w, x, y)
  end

  # filelist-bindings
  def fileList_button1(w, x, y)
    tk_call('::TreeCtrl::FileListButton1', w, x, y)
  end
  def fileList_motion1(w, x, y)
    tk_call('::TreeCtrl::FileListMotion1', w, x, y)
  end
  def fileList_motion(w, x, y)
    tk_call('::TreeCtrl::FileListMotion', w, x, y)
  end
  def fileList_leave1(w, x, y)
    tk_call('::TreeCtrl::FileListLeave1', w, x, y)
  end
  def fileList_release1(w, x, y)
    tk_call('::TreeCtrl::FileListRelease1', w, x, y)
  end
  def fileList_edit(w, i, s, e)
    tk_call('::TreeCtrl::FileListEdit', w, i, s, e)
  end
  def fileList_editCancel(w)
    tk_call('::TreeCtrl::FileListEditCancel', w)
  end
  def fileList_autoScanCheck(w, x, y)
    tk_call('::TreeCtrl::FileListAutoScanCheck', w, x, y)
  end
  def fileList_autoScanCheckAux(w)
    tk_call('::TreeCtrl::FileListAutoScanCheckAux', w)
  end

  def entryOpen(w, item, col, elem)
    tk_call('::TreeCtrl::EntryOpen', w, item, col, elem)
  end
  def entryExpanderOpen(w, item, col, elem)
    tk_call('::TreeCtrl::EntryExpanderOpen', w, item, col, elem)
  end
  def entryClose(w, accept)
    tk_call('::TreeCtrl::EntryClose', w, accept)
  end
  def entryExpanderKeypress(w)
    tk_call('::TreeCtrl::EntryExpanderKeypress', w)
  end
  def textOpen(w, item, col, elem, width=0, height=0)
    tk_call('::TreeCtrl::TextOpen', w, item, col, elem, width, height)
  end
  def textExpanderOpen(w, item, col, elem, width)
    tk_call('::TreeCtrl::TextOpen', w, item, col, elem, width)
  end
  def textClose(w, accept)
    tk_call('::TreeCtrl::TextClose', w, accept)
  end
  def textExpanderKeypress(w)
    tk_call('::TreeCtrl::TextExpanderKeypress', w)
  end
end