summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/src/widgets/tqlistbox.cpp
blob: b829780e01db39377942ef3331cfe79b36775e32 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
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
/**********************************************************************
**
** Implementation of TQListBox widget class
**
** Created : 941121
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file is part of the widgets module of the TQt GUI Toolkit.
**
** This file may be used under the terms of the GNU General
** Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the files LICENSE.GPL2
** and LICENSE.GPL3 included in the packaging of this file.
** Alternatively you may (at your option) use any later version
** of the GNU General Public License if such license has been
** publicly approved by Trolltech ASA (or its successors, if any)
** and the KDE Free TQt Foundation.
**
** Please review the following information to ensure GNU General
** Public Licensing requirements will be met:
** http://trolltech.com/products/qt/licenses/licensing/opensource/.
** If you are unsure which license is appropriate for your use, please
** review the following information:
** http://trolltech.com/products/qt/licenses/licensing/licensingoverview
** or contact the sales department at sales@trolltech.com.
**
** This file may be used under the terms of the Q Public License as
** defined by Trolltech ASA and appearing in the file LICENSE.TQPL
** included in the packaging of this file.  Licensees holding valid TQt
** Commercial licenses may use this file in accordance with the TQt
** Commercial License Agreement provided with the Software.
**
** This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
** INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
** A PARTICULAR PURPOSE. Trolltech reserves all rights not granted
** herein.
**
**********************************************************************/

#include "tqglobal.h"
#if defined(TQ_CC_BOR)
// needed for qsort() because of a std namespace problem on Borland
#include "tqplatformdefs.h"
#endif

#include "tqlistbox.h"
#ifndef TQT_NO_LISTBOX
#include "tqmemarray.h"
#include "tqfontmetrics.h"
#include "tqpainter.h"
#include "tqstrlist.h"
#include "tqpixmap.h"
#include "tqapplication.h"
#include "tqptrdict.h"
#include "tqtimer.h"
#include "tqstringlist.h"
#include "tqstyle.h"
#include "tqpopupmenu.h"
#include "tqguardedptr.h"
#if defined(TQT_ACCESSIBILITY_SUPPORT)
#include "tqaccessible.h"
#endif

#include <stdlib.h>

class TQListBoxPrivate
{
public:
    TQListBoxPrivate( TQListBox *lb ):
	head( 0 ), last( 0 ), cache( 0 ), cacheIndex( -1 ), current( 0 ),
	highlighted( 0 ), tmpCurrent( 0 ), columnPos( 1 ), rowPos( 1 ), rowPosCache( 0 ),
	columnPosOne( 0 ), rowMode( TQListBox::FixedNumber ),
	columnMode( TQListBox::FixedNumber ), numRows( 1 ), numColumns( 1 ),
	currentRow( 0 ), currentColumn( 0 ),
	mousePressRow( -1 ), mousePressColumn( -1 ),
	mouseMoveRow( -1 ), mouseMoveColumn( -1 ), mouseInternalPress( FALSE ),
	scrollTimer( 0 ), updateTimer( 0 ), visibleTimer( 0 ),
	selectionMode( TQListBox::Single ),
	count( 0 ),
	listBox( lb ), currInputString( TQString::null ),
	rowModeWins( FALSE ),
	ignoreMoves( FALSE ),
	layoutDirty( TRUE ),
	mustPaintAll( TRUE ),
	dragging( FALSE ),
	dirtyDrag ( FALSE ),
	variableHeight( TRUE /* !!! ### FALSE */ ),
	variableWidth( FALSE ),
	inMenuMode( FALSE )
    {}
    int tqfindItemByName( int item, const TQString &text );
    ~TQListBoxPrivate();

    TQListBoxItem * head, *last, *cache;
    int cacheIndex;
    TQListBoxItem * current, *highlighted, *tmpCurrent;

    TQMemArray<int> columnPos;
    TQMemArray<int> rowPos;
    int rowPosCache;
    int columnPosOne;

    TQListBox::LayoutMode rowMode;
    TQListBox::LayoutMode columnMode;
    int numRows;
    int numColumns;

    int currentRow;
    int currentColumn;
    int mousePressRow;
    int mousePressColumn;
    int mouseMoveRow;
    int mouseMoveColumn;
    bool mouseInternalPress;

    TQTimer * scrollTimer;
    TQTimer * updateTimer;
    TQTimer * visibleTimer;
    TQTimer * resizeTimer;

    TQPoint scrollPos;

    TQListBox::SelectionMode selectionMode;

    int count;


    TQListBox *listBox;
    TQString currInputString;
    TQTimer *inputTimer;

    TQListBoxItem *pressedItem, *selectAnchor;

    uint select :1;
    uint pressedSelected :1;
    uint rowModeWins :1;
    uint ignoreMoves :1;
    uint clearing :1;
    uint layoutDirty :1;
    uint mustPaintAll :1;
    uint dragging :1;
    uint dirtyDrag :1;
    uint variableHeight :1;
    uint variableWidth :1;
    uint inMenuMode :1;

    TQRect *rubber;
    TQPtrDict<bool> selectable;

    struct SortableItem {
	TQListBoxItem *item;
    };
};


TQListBoxPrivate::~TQListBoxPrivate()
{
    TQ_ASSERT( !head );
}


/*!
    \class TQListBoxItem tqlistbox.h
    \brief The TQListBoxItem class is the base class of all list box items.

    This class is an abstract base class used for all list box items.
    If you need to insert customized items into a TQListBox you must
    inherit this class and reimplement paint(), height() and width().

    \sa TQListBox
*/

/*!
    \fn bool TQListBox::dragSelect() const
    \internal
*/

/*!
    \fn void TQListBox::setDragSelect( bool )
    \internal
*/

/*!
    \fn bool TQListBox::autoScroll() const
    \internal
*/

/*!
    \fn void TQListBox::setAutoScroll( bool )
    \internal
*/

/*!
    \fn bool TQListBox::autoScrollBar() const

    \obsolete

    Returns TRUE if vScrollBarMode() is \c Auto; otherwise returns
    FALSE.
*/

/*!
    \fn void TQListBox::setAutoScrollBar( bool enable )

    \obsolete

    If \a enable is TRUE sets setVScrollBarMode() to \c AlwaysOn;
    otherwise sets setVScrollBarMode() to \c AlwaysOff.
*/

/*!
    \fn bool TQListBox::scrollBar() const

    \obsolete

    Returns FALSE if vScrollBarMode() is \c AlwaysOff; otherwise
    returns TRUE.
*/

/*!
    \fn void TQListBox::setScrollBar( bool enable )

    \obsolete

    If \a enable is TRUE sets setVScrollBarMode() to \c AlwaysOn;
    otherwise sets setVScrollBarMode() to \c AlwaysOff.
*/

/*!
    \fn bool TQListBox::autoBottomScrollBar() const

    \obsolete

    Returns TRUE if hScrollBarMode() is \c Auto; otherwise returns
    FALSE.
*/

/*!
    \fn void TQListBox::setAutoBottomScrollBar( bool enable )

    \obsolete

    If \a enable is TRUE sets setHScrollBarMode() to \c AlwaysOn;
    otherwise sets setHScrollBarMode() to \c AlwaysOff.
*/

/*!
    \fn bool TQListBox::bottomScrollBar() const

    \obsolete

    Returns FALSE if vScrollBarMode() is \c AlwaysOff; otherwise
    returns TRUE.
*/

/*!
    \fn void TQListBox::setBottomScrollBar( bool enable )

    \obsolete

    If \a enable is TRUE sets setHScrollBarMode() to \c AlwaysOn;
    otherwise sets setHScrollBarMode() to \c AlwaysOff.
*/

/*!
    \fn bool TQListBox::smoothScrolling() const
    \internal
*/

/*!
    \fn void TQListBox::setSmoothScrolling( bool )
    \internal
*/

/*!
    \fn bool TQListBox::autoUpdate() const
    \internal
*/

/*!
    \fn void TQListBox::setAutoUpdate( bool )
    \internal
*/

/*!
    \fn void TQListBox::setFixedVisibleLines( int lines )
    \internal
*/

/*!
    \obsolete
    \fn int TQListBox::cellHeight( int i ) const
    Returns the item height of item \a i.
    \sa itemHeight()
*/

/*!
    \obsolete
    \overload int TQListBox::cellHeight() const
    Returns the item height of the first item, item 0.
    \sa itemHeight()
*/

/*!
    \obsolete
    \fn int TQListBox::cellWidth() const
    Returns the maximum item width.
    \sa maxItemWidth()
*/

/*!
    \overload int TQListBox::cellWidth(int i) const
    \internal
*/

/*!
    \obsolete
    \fn int TQListBox::numCols() const
    Returns the number of columns.
    \sa numColumns()
*/

/*!
    \fn void TQListBox::updateCellWidth()
    \internal
*/

/*!
    \obsolete
    \fn int TQListBox::totalWidth() const
    Returns contentsWidth().
*/

/*!
    \obsolete
    \fn int TQListBox::totalHeight() const
    Returns contentsHeight().
*/

/*!
    \obsolete
    \fn int TQListBox::tqfindItem( int yPos ) const
    Returns the index of the item a point (0, \a yPos).
    \sa index() itemAt()
*/


/*!
    Constructs an empty list box item in the list box \a listbox.
*/

TQListBoxItem::TQListBoxItem( TQListBox* listbox )
{
    lbox = listbox;
    s = FALSE;
    dirty = TRUE;
    custom_highlight = FALSE;
    p = n = 0;

    // just something that'll look noticeable in the debugger
    x = y = 42;

    if (listbox)
	listbox->insertItem( this );
}

/*!
    Constructs an empty list box item in the list box \a listbox and
    inserts it after the item \a after or at the beginning if \a after
    is 0.
*/

TQListBoxItem::TQListBoxItem( TQListBox* listbox, TQListBoxItem *after )
{
    lbox = listbox;
    s = FALSE;
    dirty = TRUE;
    custom_highlight = FALSE;
    p = n = 0;

    // just something that'll be noticeable in the debugger
    x = y = 42;

    if (listbox)
	listbox->insertItem( this, after );
}


/*!
    Destroys the list box item.
*/

TQListBoxItem::~TQListBoxItem()
{
    if ( lbox )
	lbox->takeItem( this );
}


/*!
    Defines whether the list box item is responsible for drawing
    itself in a highlighted state when being selected.

    If \a b is FALSE (the default), the list box will draw some
    default highlight indicator before calling paint().

    \sa selected(), paint()
*/
void TQListBoxItem::setCustomHighlighting( bool b )
{
    custom_highlight = b;
}

/*!
    \fn void TQListBoxItem::paint( TQPainter *p )

    Implement this function to draw your item. The painter, \a p, is
    already open for painting.

    \sa height(), width()
*/

/*!
    \fn int TQListBoxItem::width( const TQListBox* lb ) const

    Reimplement this function to return the width of your item. The \a
    lb parameter is the same as listBox() and is provided for
    convenience and compatibility.

    The default implementation returns
    \l{TQApplication::globalStrut()}'s width.

    \sa paint(), height()
*/
int TQListBoxItem::width(const TQListBox*)  const
{
    return TQApplication::globalStrut().width();
}

/*!
    \fn int TQListBoxItem::height( const TQListBox* lb ) const

    Implement this function to return the height of your item. The \a
    lb parameter is the same as listBox() and is provided for
    convenience and compatibility.

    The default implementation returns
    \l{TQApplication::globalStrut()}'s height.

    \sa paint(), width()
*/
int TQListBoxItem::height(const TQListBox*)  const
{
    return TQApplication::globalStrut().height();
}


/*!
    Returns the text of the item. This text is also used for sorting.

    \sa setText()
*/
TQString TQListBoxItem::text() const
{
    return txt;
}

/*!
    Returns the pixmap associated with the item, or 0 if there isn't
    one.

    The default implementation returns 0.
*/
const TQPixmap *TQListBoxItem::pixmap() const
{
    return 0;
}

/*!
    If \a b is TRUE (the default) then this item can be selected by
    the user; otherwise this item cannot be selected by the user.

    \sa isSelectable()
*/

void TQListBoxItem::setSelectable( bool b )
{
    if ( !listBox() )
	return;
    bool *sel = listBox()->d->selectable.tqfind( this );
    if ( !sel )
	listBox()->d->selectable.insert( this, new bool( b ) );
    else
	listBox()->d->selectable.tqreplace( this, new bool( b ) );
}

/*!
    Returns TRUE if this item is selectable (the default); otherwise
    returns FALSE.

    \sa setSelectable()
*/

bool TQListBoxItem::isSelectable() const
{
    if ( !listBox() )
	return TRUE;
    bool *sel = listBox()->d->selectable.tqfind( ( (TQListBoxItem*)this ) );
    if ( !sel )
	return TRUE;
    else
	return *sel;
}

/*!
    \fn void TQListBoxItem::setText( const TQString &text )

    Sets the text of the TQListBoxItem to \a text. This \a text is also
    used for sorting. The text is not shown unless explicitly drawn in
    paint().

    \sa text()
*/


/*!
    \class TQListBoxText tqlistbox.h
    \brief The TQListBoxText class provides list box items that display text.

    \ingroup advanced

    The text is drawn in the widget's current font. If you need
    several different fonts, you must implement your own subclass of
    TQListBoxItem.

    \sa TQListBox, TQListBoxItem
*/


/*!
    Constructs a list box item in list box \a listbox showing the text
    \a text.
*/
TQListBoxText::TQListBoxText( TQListBox *listbox, const TQString &text )
    :TQListBoxItem( listbox )
{
    setText( text );
}

/*!
    Constructs a list box item showing the text \a text.
*/

TQListBoxText::TQListBoxText( const TQString &text )
    :TQListBoxItem()
{
    setText( text );
}

/*!
    Constructs a list box item in list box \a listbox showing the text
    \a text. The item is inserted after the item \a after, or at the
    beginning if \a after is 0.
*/

TQListBoxText::TQListBoxText( TQListBox* listbox, const TQString &text, TQListBoxItem *after )
    : TQListBoxItem( listbox, after )
{
    setText( text );
}

/*!
    Destroys the item.
*/

TQListBoxText::~TQListBoxText()
{
}

/*!
    Draws the text using \a painter.
*/

void TQListBoxText::paint( TQPainter *painter )
{
    int itemHeight = height( listBox() );
    TQFontMetrics fm = painter->fontMetrics();
    int yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
    painter->drawText( 3, yPos, text() );
}

/*!
    Returns the height of a line of text in list box \a lb.

    \sa paint(), width()
*/

int TQListBoxText::height( const TQListBox* lb ) const
{
    int h = lb ? lb->fontMetrics().lineSpacing() + 2 : 0;
    return TQMAX( h, TQApplication::globalStrut().height() );
}

/*!
    Returns the width of this line in list box \a lb.

    \sa paint(), height()
*/

int TQListBoxText::width( const TQListBox* lb ) const
{
    int w = lb ? lb->fontMetrics().width( text() ) + 6 : 0;
    return TQMAX( w, TQApplication::globalStrut().width() );
}

int TQListBoxText::RTTI = 1;

/*!
    \fn int TQListBoxText::rtti() const

    \reimp

    Returns 1.

    Make your derived classes return their own values for rtti(), and
    you can distinguish between listbox items. You should use values
    greater than 1000 preferably a large random number, to allow for
    extensions to this class.
*/

int TQListBoxText::rtti() const
{
    return RTTI;
}

/*!
    \class TQListBoxPixmap tqlistbox.h
    \brief The TQListBoxPixmap class provides list box items with a
    pixmap and optional text.

    \ingroup advanced

    Items of this class are drawn with the pixmap on the left with the
    optional text to the right of the pixmap.

    \sa TQListBox, TQListBoxItem
*/


/*!
    Constructs a new list box item in list box \a listbox showing the
    pixmap \a pixmap.
*/

TQListBoxPixmap::TQListBoxPixmap( TQListBox* listbox, const TQPixmap &pixmap )
    : TQListBoxItem( listbox )
{
    pm = pixmap;
}

/*!
    Constructs a new list box item showing the pixmap \a pixmap.
*/

TQListBoxPixmap::TQListBoxPixmap( const TQPixmap &pixmap )
    : TQListBoxItem()
{
    pm = pixmap;
}

/*!
    Constructs a new list box item in list box \a listbox showing the
    pixmap \a pixmap. The item gets inserted after the item \a after,
    or at the beginning if \a after is 0.
*/

TQListBoxPixmap::TQListBoxPixmap( TQListBox* listbox, const TQPixmap &pixmap, TQListBoxItem *after )
    : TQListBoxItem( listbox, after )
{
    pm = pixmap;
}


/*!
    Destroys the item.
*/

TQListBoxPixmap::~TQListBoxPixmap()
{
}


/*!
    Constructs a new list box item in list box \a listbox showing the
    pixmap \a pix and the text \a text.
*/
TQListBoxPixmap::TQListBoxPixmap( TQListBox* listbox, const TQPixmap &pix, const TQString& text)
    : TQListBoxItem( listbox )
{
    pm = pix;
    setText( text );
}

/*!
    Constructs a new list box item showing the pixmap \a pix and the
    text to \a text.
*/
TQListBoxPixmap::TQListBoxPixmap( const TQPixmap & pix, const TQString& text)
    : TQListBoxItem()
{
    pm = pix;
    setText( text );
}

/*!
    Constructs a new list box item in list box \a listbox showing the
    pixmap \a pix and the string \a text. The item gets inserted after
    the item \a after, or at the beginning if \a after is 0.
*/
TQListBoxPixmap::TQListBoxPixmap( TQListBox* listbox, const TQPixmap & pix, const TQString& text,
				TQListBoxItem *after )
    : TQListBoxItem( listbox, after )
{
    pm = pix;
    setText( text );
}

/*!
    \fn const TQPixmap *TQListBoxPixmap::pixmap() const

    Returns the pixmap associated with the item.
*/


/*!
    Draws the pixmap using \a painter.
*/

void TQListBoxPixmap::paint( TQPainter *painter )
{
    int itemHeight = height( listBox() );
    int yPos;

    const TQPixmap *pm = pixmap();
    if ( pm && ! pm->isNull() ) {
	yPos = ( itemHeight - pm->height() ) / 2;
	painter->drawPixmap( 3, yPos, *pm);
    }

    if ( !text().isEmpty() ) {
	TQFontMetrics fm = painter->fontMetrics();
	yPos = ( ( itemHeight - fm.height() ) / 2 ) + fm.ascent();
	painter->drawText( pm->width() + 5, yPos, text() );
    }
}

/*!
    Returns the height of the pixmap in list box \a lb.

    \sa paint(), width()
*/

int TQListBoxPixmap::height( const TQListBox* lb ) const
{
    int h;
    if ( text().isEmpty() )
	h = pm.height();
    else
	h = TQMAX( pm.height(), lb->fontMetrics().lineSpacing() + 2 );
    return TQMAX( h, TQApplication::globalStrut().height() );
}

/*!
    Returns the width of the pixmap plus some margin in list box \a lb.

    \sa paint(), height()
*/

int TQListBoxPixmap::width( const TQListBox* lb ) const
{
    if ( text().isEmpty() )
	return TQMAX( pm.width() + 6, TQApplication::globalStrut().width() );
    return TQMAX( pm.width() + lb->fontMetrics().width( text() ) + 6,
	    TQApplication::globalStrut().width() );
}

int TQListBoxPixmap::RTTI = 2;

/*!
    \fn int TQListBoxPixmap::rtti() const

    \reimp

    Returns 2.

    Make your derived classes return their own values for rtti(), and
    you can distinguish between listbox items. You should use values
    greater than 1000 preferably a large random number, to allow for
    extensions to this class.
*/

int TQListBoxPixmap::rtti() const
{
    return RTTI;
}

/*!
    \class TQListBox tqlistbox.h
    \brief The TQListBox widget provides a list of selectable, read-only items.

    \ingroup advanced
    \mainclass

    This is typically a single-column list in which either no item or
    one item is selected, but it can also be used in many other ways.

    TQListBox will add scroll bars as necessary, but it isn't intended
    for \e really big lists. If you want more than a few thousand
    items, it's probably better to use a different widget mainly
    because the scroll bars won't provide very good navigation, but
    also because TQListBox may become slow with huge lists. (See
    TQListView and TQTable for possible alternatives.)

    There are a variety of selection modes described in the
    TQListBox::SelectionMode documentation. The default is \c Single
    selection mode, but you can change it using setSelectionMode().
    (setMultiSelection() is still provided for compatibility with TQt
    1.x. We recommend using setSelectionMode() in all code.)

    Because TQListBox offers multiple selection it must display
    keyboard focus and selection state separately. Therefore there are
    functions both to set the selection state of an item, i.e.
    setSelected(), and to set which item displays keyboard focus, i.e.
    setCurrentItem().

    The list box normally arranges its items in a single column and
    adds a vertical scroll bar if required. It is possible to have a
    different fixed number of columns (setColumnMode()), or as many
    columns as will fit in the list box's assigned screen space
    (setColumnMode(FitToWidth)), or to have a fixed number of rows
    (setRowMode()) or as many rows as will fit in the list box's
    assigned screen space (setRowMode(FitToHeight)). In all these
    cases TQListBox will add scroll bars, as appropriate, in at least
    one direction.

    If multiple rows are used, each row can be as high as necessary
    (the normal setting), or you can request that all items will have
    the same height by calling setVariableHeight(FALSE). The same
    applies to a column's width, see setVariableWidth().

    The TQListBox's items are TQListBoxItem objects. TQListBox provides
    methods to insert new items as strings, as pixmaps, and as
    TQListBoxItem * (insertItem() with various arguments), and to
    tqreplace an existing item with a new string, pixmap or TQListBoxItem
    (changeItem() with various arguments). You can also remove items
    singly with removeItem() or clear() the entire list box. Note that
    if you create a TQListBoxItem yourself and insert it, TQListBox
    takes ownership of the item.

    You can also create a TQListBoxItem, such as TQListBoxText or
    TQListBoxPixmap, with the list box as first parameter. The item
    will then append itself. When you delete an item it is
    automatically removed from the list box.

    The list of items can be arbitrarily large; TQListBox will add
    scroll bars if necessary. TQListBox can display a single-column
    (the common case) or multiple-columns, and offers both single and
    multiple selection. TQListBox does not support multiple-column
    items (but TQListView and TQTable do), or tree hierarchies (but
    TQListView does).

    The list box items can be accessed both as TQListBoxItem objects
    (recommended) and using integer indexes (the original TQListBox
    implementation used an array of strings internally, and the API
    still supports this mode of operation). Everything can be done
    using the new objects, and most things can be done using indexes.

    Each item in a TQListBox tqcontains a TQListBoxItem. One of the items
    can be the current item. The currentChanged() signal and the
    highlighted() signal are emitted when a new item becomes current,
    e.g. because the user clicks on it or TQListBox::setCurrentItem()
    is called. The selected() signal is emitted when the user
    double-clicks on an item or presses Enter on the current item.

    If the user does not select anything, no Q_SIGNALS are emitted and
    currentItem() returns -1.

    A list box has \c WheelFocus as a default focusPolicy(), i.e. it
    can get keyboard focus by tabbing, clicking and through the use of
    the mouse wheel.

    New items can be inserted using insertItem(), insertStrList() or
    insertStringList(). inSort() is obsolete because this method is
    quite inefficient. It's preferable to insert the items normally
    and call sort() afterwards, or to insert a sorted TQStringList().

    By default, vertical and horizontal scroll bars are added and
    removed as necessary. setHScrollBarMode() and setVScrollBarMode()
    can be used to change this policy.

    If you need to insert types other than strings and pixmaps, you
    must define new classes which inherit TQListBoxItem.

    \warning The list box assumes ownership of all list box items and
    will delete them when it does not need them any more.

    <img src=qlistbox-m.png> <img src=qlistbox-w.png>

    \sa TQListView TQComboBox TQButtonGroup
    \link guibooks.html#fowler GUI Design Handbook: List Box (two
    sections)\endlink
*/

/*!
    \enum TQListBox::SelectionMode

    This enumerated type is used by TQListBox to indicate how it reacts
    to selection by the user.

    \value Single  When the user selects an item, any already-selected
    item becomes unselected and the user cannot unselect the selected
    item. This means that the user can never clear the selection, even
    though the selection may be cleared by the application programmer
    using TQListBox::clearSelection().

    \value Multi  When the user selects an item the selection status
    of that item is toggled and the other items are left alone. Also,
    multiple items can be selected by dragging the mouse while the
    left mouse button is kept pressed.

    \value Extended When the user selects an item the selection is
    cleared and the new item selected. However, if the user presses
    the Ctrl key when clicking on an item, the clicked item gets
    toggled and all other items are left untouched. And if the user
    presses the Shift key while clicking on an item, all items between
    the current item and the clicked item get selected or unselected,
    depending on the state of the clicked item. Also, multiple items
    can be selected by dragging the mouse while the left mouse button
    is kept pressed.

    \value NoSelection  Items cannot be selected.

    In other words, \c Single is a real single-selection list box, \c
    Multi is a real multi-selection list box, \c Extended is a list
    box in which users can select multiple items but usually want to
    select either just one or a range of contiguous items, and \c
    NoSelection is for a list box where the user can look but not
    touch.
*/


/*!
    \enum TQListBox::LayoutMode

    This enum type is used to specify how TQListBox lays out its rows
    and columns.

    \value FixedNumber  There is a fixed number of rows (or columns).

    \value FitToWidth   There are as many columns as will fit
    on-screen.

    \value FitToHeight  There are as many rows as will fit on-screen.

    \value Variable  There are as many rows as are required by the
    column mode. (Or as many columns as required by the row mode.)

    Example: When you call setRowMode( FitToHeight ), columnMode()
    automatically becomes \c Variable to accommodate the row mode
    you've set.
*/

/*!
    \fn void  TQListBox::onItem( TQListBoxItem *i )

    This signal is emitted when the user moves the mouse cursor onto
    an item, similar to the TQWidget::enterEvent() function. \a i is
    the TQListBoxItem that the mouse has moved on.
*/

// ### bug here too? enter/leave event may noit considered. move the
// mouse out of the window and back in, to the same item - does it
// work?

/*!
    \fn void  TQListBox::onViewport()

    This signal is emitted when the user moves the mouse cursor from
    an item to an empty part of the list box.
*/


/*!
    Constructs a new empty list box called \a name and with tqparent \a
    tqparent.

    Performance is boosted by modifying the widget flags \a f so that
    only part of the TQListBoxItem tqchildren is redrawn. This may be
    unsuitable for custom TQListBoxItem classes, in which case \c
    WStaticContents and \c WNoAutoErase should be cleared
    immediately after construction.

    \sa TQWidget::clearWFlags() TQt::WidgetFlags
*/

TQListBox::TQListBox( TQWidget *tqparent, const char *name, WFlags f )
    : TQScrollView( tqparent, name, (WFlags)(f | TQt::WStaticContents | TQt::WNoAutoErase) )
{
    d = new TQListBoxPrivate( this );
    d->updateTimer = new TQTimer( this, "listbox update timer" );
    d->visibleTimer = new TQTimer( this, "listbox visible timer" );
    d->inputTimer = new TQTimer( this, "listbox input timer" );
    d->resizeTimer = new TQTimer( this, "listbox resize timer" );
    d->clearing = FALSE;
    d->pressedItem = 0;
    d->selectAnchor = 0;
    d->select = FALSE;
    d->rubber = 0;
    d->selectable.setAutoDelete( TRUE );

    setMouseTracking( TRUE );
    viewport()->setMouseTracking( TRUE );

    connect( d->updateTimer, TQT_SIGNAL(timeout()),
	     this, TQT_SLOT(refreshSlot()) );
    connect( d->visibleTimer, TQT_SIGNAL(timeout()),
	     this, TQT_SLOT(ensureCurrentVisible()) );
    connect( d->resizeTimer, TQT_SIGNAL( timeout() ),
	     this, TQT_SLOT( adjustItems() ) );
    viewport()->setBackgroundMode( TQt::PaletteBase );
    setBackgroundMode( TQt::PaletteBackground, TQt::PaletteBase );
    viewport()->setFocusProxy( this );
    viewport()->setFocusPolicy( Qt::WheelFocus );
}


TQListBox * TQListBox::changedListBox = 0;

/*!
    Destroys the list box. Deletes all list box items.
*/

TQListBox::~TQListBox()
{
    if ( changedListBox == this )
	changedListBox = 0;
    clear();
    delete d;
    d = 0;
}

/*!
    \fn void TQListBox::pressed( TQListBoxItem *item )

    This signal is emitted when the user presses any mouse button. If
    \a item is not 0, the cursor is on \a item. If \a item is 0, the
    mouse cursor isn't on any item.

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.
*/

/*!
    \overload void TQListBox::pressed( TQListBoxItem *item, const TQPoint &pnt )

    This signal is emitted when the user presses any mouse button. If
    \a item is not 0, the cursor is on \a item. If \a item is 0, the
    mouse cursor isn't on any item.

    \a pnt is the position of the mouse cursor in the global
    coordinate system (TQMouseEvent::globalPos()).

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.

    \sa mouseButtonPressed() rightButtonPressed() clicked()
*/

/*!
    \fn void TQListBox::clicked( TQListBoxItem *item )

    This signal is emitted when the user clicks any mouse button. If
    \a item is not 0, the cursor is on \a item. If \a item is 0, the
    mouse cursor isn't on any item.

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.
*/

/*!
    \overload void TQListBox::clicked( TQListBoxItem *item, const TQPoint &pnt )

    This signal is emitted when the user clicks any mouse button. If
    \a item is not 0, the cursor is on \a item. If \a item is 0, the
    mouse cursor isn't on any item.

    \a pnt is the position of the mouse cursor in the global
    coordinate system (TQMouseEvent::globalPos()). (If the click's
    press and release differs by a pixel or two, \a pnt is the
    position at release time.)

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.
*/

/*!
    \fn void TQListBox::mouseButtonClicked (int button, TQListBoxItem * item, const TQPoint & pos)

    This signal is emitted when the user clicks mouse button \a
    button. If \a item is not 0, the cursor is on \a item. If \a item
    is 0, the mouse cursor isn't on any item.

    \a pos is the position of the mouse cursor in the global
    coordinate system (TQMouseEvent::globalPos()). (If the click's
    press and release differs by a pixel or two, \a pos is the
    position at release time.)

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.
*/

/*!
    \fn void TQListBox::mouseButtonPressed (int button, TQListBoxItem * item, const TQPoint & pos)

    This signal is emitted when the user presses mouse button \a
    button. If \a item is not 0, the cursor is on \a item. If \a item
    is 0, the mouse cursor isn't on any item.

    \a pos is the position of the mouse cursor in the global
    coordinate system (TQMouseEvent::globalPos()).

    Note that you must not delete any TQListBoxItem objects in Q_SLOTS
    connected to this signal.
*/

/*!
    \fn void TQListBox::doubleClicked( TQListBoxItem *item )

    This signal is emitted whenever an item is double-clicked. It's
    emitted on the second button press, not the second button release.
    If \a item is not 0, the cursor is on \a item. If \a item is 0,
    the mouse cursor isn't on any item.
*/


/*!
    \fn void TQListBox::returnPressed( TQListBoxItem * )

    This signal is emitted when Enter or Return is pressed. The
    argument is currentItem().
*/

/*!
    \fn void TQListBox::rightButtonClicked( TQListBoxItem *, const TQPoint& )

    This signal is emitted when the right button is clicked (i.e. when
    it's released at the same point where it was pressed). The
    arguments are the relevant TQListBoxItem (may be 0) and the point
    in global coordinates.
*/


/*!
    \fn void TQListBox::rightButtonPressed (TQListBoxItem *, const TQPoint & )

    This signal is emitted when the right button is pressed. The
    arguments are the relevant TQListBoxItem (may be 0) and the point
    in global coordinates.
*/

/*!
    \fn void TQListBox::contextMenuRequested( TQListBoxItem *item, const TQPoint & pos )

    This signal is emitted when the user invokes a context menu with
    the right mouse button or with special system keys, with \a item
    being the item under the mouse cursor or the current item,
    respectively.

    \a pos is the position for the context menu in the global
    coordinate system.
*/

/*!
    \fn void TQListBox::selectionChanged()

    This signal is emitted when the selection set of a list box
    changes. This signal is emitted in each selection mode. If the
    user selects five items by drag-selecting, TQListBox tries to emit
    just one selectionChanged() signal so the signal can be connected
    to computationally expensive Q_SLOTS.

    \sa selected() currentItem()
*/

/*!
    \overload void TQListBox::selectionChanged( TQListBoxItem *item )

    This signal is emitted when the selection in a \c Single selection
    list box changes. \a item is the newly selected list box item.

    \sa selected() currentItem()
*/

/*!
    \fn void TQListBox::currentChanged( TQListBoxItem *item )

    This signal is emitted when the user makes a new item the current
    item. \a item is the new current list box item.

    \sa setCurrentItem() currentItem()
*/

/*!
    \fn void TQListBox::highlighted( int index )

    This signal is emitted when the user makes a new item the current
    item. \a index is the index of the new current item.

    \sa currentChanged() selected() currentItem() selectionChanged()
*/

/*!
    \overload void TQListBox::highlighted( TQListBoxItem * )

    This signal is emitted when the user makes a new item the current
    item. The argument is a pointer to the new current item.

    \sa currentChanged() selected() currentItem() selectionChanged()
*/

/*!
    \overload void TQListBox::highlighted( const TQString & )

    This signal is emitted when the user makes a new item the current
    item and the item is (or has) a string. The argument is the text
    of the new current item.

    \sa currentChanged() selected() currentItem() selectionChanged()
*/

/*!
    \fn void TQListBox::selected( int index )

    This signal is emitted when the user double-clicks on an item or
    presses Enter on the current item. \a index is the index of the
    selected item.

    \sa currentChanged() highlighted() selectionChanged()
*/

/*!
    \overload void TQListBox::selected( TQListBoxItem * )

    This signal is emitted when the user double-clicks on an item or
    presses Enter on the current item. The argument is a pointer to
    the new selected item.

    \sa currentChanged() highlighted() selectionChanged()
*/

/*!
    \overload void TQListBox::selected( const TQString &)

    This signal is emitted when the user double-clicks on an item or
    presses Enter on the current item, and the item is (or has) a
    string. The argument is the text of the selected item.

    \sa currentChanged() highlighted() selectionChanged()
*/

/*! \reimp */

void TQListBox::setFont( const TQFont &font )
{
    TQScrollView::setFont( font );
    triggerUpdate( TRUE );
}


/*!
    \property TQListBox::count
    \brief the number of items in the list box
*/

uint TQListBox::count() const
{
    return d->count;
}


/*!
    Inserts the string list \a list into the list at position \a
    index.

    If \a index is negative, \a list is inserted at the end of the
    list. If \a index is too large, the operation is ignored.

    \warning This function uses \c{const char *} rather than TQString,
    so we recommend against using it. It is provided so that legacy
    code will continue to work, and so that programs that certainly
    will not need to handle code outside a single 8-bit locale can use
    it. See insertStringList() which uses real TQStrings.

    \warning This function is never significantly faster than a loop
    around insertItem().

    \sa insertItem(), insertStringList()
*/

void TQListBox::insertStrList( const TQStrList *list, int index )
{
    if ( !list ) {
#if defined(TQT_CHECK_NULL)
	TQ_ASSERT( list != 0 );
#endif
	return;
    }
    insertStrList( *list, index );
}



/*!
    Inserts the string list \a list into the list at position \a
    index.

    If \a index is negative, \a list is inserted at the end of the
    list. If \a index is too large, the operation is ignored.

    \warning This function is never significantly faster than a loop
    around insertItem().

    \sa insertItem(), insertStrList()
*/

void TQListBox::insertStringList( const TQStringList & list, int index )
{
    if ( index < 0 )
	index = count();
    for ( TQStringList::ConstIterator it = list.begin(); it != list.end(); ++it )
	insertItem( new TQListBoxText(*it), index++ );
}



/*!
    \overload

    Inserts the string list \a list into the list at position \a
    index.

    If \a index is negative, \a list is inserted at the end of the
    list. If \a index is too large, the operation is ignored.

    \warning This function uses \c{const char *} rather than TQString,
    so we recommend against using it. It is provided so that legacy
    code will continue to work, and so that programs that certainly
    will not need to handle code outside a single 8-bit locale can use
    it. See insertStringList() which uses real TQStrings.

    \warning This function is never significantly faster than a loop
    around insertItem().

    \sa insertItem(), insertStringList()
*/

void TQListBox::insertStrList( const TQStrList & list, int index )
{
    TQStrListIterator it( list );
    const char* txt;
    if ( index < 0 )
	index = count();
    while ( (txt=it.current()) ) {
	++it;
	insertItem( new TQListBoxText(TQString::tqfromLatin1(txt)),
		    index++ );
    }
    if ( hasFocus() && !d->current )
	setCurrentItem( d->head );
}


/*!
    \overload

    Inserts the \a numStrings strings of the array \a strings into the
    list at position \a index.

    If \a index is negative, insertStrList() inserts \a strings at the
    end of the list. If \a index is too large, the operation is
    ignored.

    \warning This function uses \c{const char *} rather than TQString,
    so we recommend against using it. It is provided so that legacy
    code will continue to work, and so that programs that certainly
    will not need to handle code outside a single 8-bit locale can use
    it. See insertStringList() which uses real TQStrings.

    \warning This function is never significantly faster than a loop
    around insertItem().

    \sa insertItem(), insertStringList()
*/

void TQListBox::insertStrList( const char **strings, int numStrings, int index )
{
    if ( !strings ) {
#if defined(TQT_CHECK_NULL)
	TQ_ASSERT( strings != 0 );
#endif
	return;
    }
    if ( index < 0 )
	index = count();
    int i = 0;
    while ( (numStrings<0 && strings[i]!=0) || i<numStrings ) {
	insertItem( new TQListBoxText( TQString::tqfromLatin1(strings[i])),
		    index + i );
	i++;
    }
    if ( hasFocus() && !d->current )
	setCurrentItem( d->head );
}

/*!
    Inserts the item \a lbi into the list at position \a index.

    If \a index is negative or larger than the number of items in the
    list box, \a lbi is inserted at the end of the list.

    \sa insertStrList()
*/

void TQListBox::insertItem( const TQListBoxItem *lbi, int index )
{
#if defined ( TQT_CHECK_NULL )
    TQ_ASSERT( lbi != 0 );
#else
    if ( !lbi )
	return;
#endif

    if ( index < 0 )
	index = d->count;

    if ( index >= d->count ) {
	insertItem( lbi, d->last );
	return;
    }

    TQListBoxItem * item = (TQListBoxItem *)lbi;
    d->count++;
    d->cache = 0;

    item->lbox = this;
    if ( !d->head || index == 0 ) {
	item->n = d->head;
	item->p = 0;
	d->head = item;
	item->dirty = TRUE;
	if ( item->n )
	    item->n->p = item;
    } else {
	TQListBoxItem * i = d->head;
	while ( i->n && index > 1 ) {
	    i = i->n;
	    index--;
	}
	if ( i->n ) {
	    item->n = i->n;
	    item->p = i;
	    item->n->p = item;
	    item->p->n = item;
	} else {
	    i->n = item;
	    item->p = i;
	    item->n = 0;
	}
    }

    if ( hasFocus() && !d->current ) {
	d->current = d->head;
	updateItem( d->current );
	emit highlighted( d->current );
	emit highlighted( d->current->text() );
	emit highlighted( index );
    }

    triggerUpdate( TRUE );
}

/*!
    \overload

    Inserts the item \a lbi into the list after the item \a after, or
    at the beginning if \a after is 0.

    \sa insertStrList()
*/

void TQListBox::insertItem( const TQListBoxItem *lbi, const TQListBoxItem *after )
{
#if defined ( TQT_CHECK_NULL )
    TQ_ASSERT( lbi != 0 );
#else
    if ( !lbi )
	return;
#endif

    TQListBoxItem * item = (TQListBoxItem*)lbi;
    d->count++;
    d->cache = 0;

    item->lbox = this;
    if ( !d->head || !after ) {
	item->n = d->head;
	item->p = 0;
	d->head = item;
	item->dirty = TRUE;
	if ( item->n )
	    item->n->p = item;
    } else {
	TQListBoxItem * i = (TQListBoxItem*) after;
	if ( i ) {
	    item->n = i->n;
	    item->p = i;
	    if ( item->n )
		item->n->p = item;
	    if ( item->p )
		item->p->n = item;
	}
    }

    if ( after == d->last )
	d->last = (TQListBoxItem*) lbi;

    if ( hasFocus() && !d->current ) {
	d->current = d->head;
	updateItem( d->current );
	emit highlighted( d->current );
	emit highlighted( d->current->text() );
	emit highlighted( index( d->current ) );
    }

    triggerUpdate( TRUE );
}

/*!
    \overload

    Inserts a new list box text item with the text \a text into the
    list at position \a index.

    If \a index is negative, \a text is inserted at the end of the
    list.

    \sa insertStrList()
*/

void TQListBox::insertItem( const TQString &text, int index )
{
    insertItem( new TQListBoxText(text), index );
}

/*!
    \overload

    Inserts a new list box pixmap item with the pixmap \a pixmap into
    the list at position \a index.

    If \a index is negative, \a pixmap is inserted at the end of the
    list.

    \sa insertStrList()
*/

void TQListBox::insertItem( const TQPixmap &pixmap, int index )
{
    insertItem( new TQListBoxPixmap(pixmap), index );
}

/*!
    \overload

    Inserts a new list box pixmap item with the pixmap \a pixmap and
    the text \a text into the list at position \a index.

    If \a index is negative, \a pixmap is inserted at the end of the
    list.

    \sa insertStrList()
*/

void TQListBox::insertItem( const TQPixmap &pixmap, const TQString &text, int index )
{
    insertItem( new TQListBoxPixmap(pixmap, text), index );
}

/*!
    Removes and deletes the item at position \a index. If \a index is
    equal to currentItem(), a new item becomes current and the
    currentChanged() and highlighted() Q_SIGNALS are emitted.

    \sa insertItem(), clear()
*/

void TQListBox::removeItem( int index )
{
    bool wasVisible = itemVisible( currentItem() );
    delete item( index );
    triggerUpdate( TRUE );
    if ( wasVisible )
	ensureCurrentVisible();
}


/*!
    Deletes all the items in the list.

    \sa removeItem()
*/

void TQListBox::clear()
{
    setContentsPos( 0, 0 );
    bool blocked = tqsignalsBlocked();
    blockSignals( TRUE );
    d->clearing = TRUE;
    d->current = 0;
    d->tmpCurrent = 0;
    TQListBoxItem * i = d->head;
    d->head = 0;
    while ( i ) {
	TQListBoxItem * n = i->n;
	i->n = i->p = 0;
	delete i;
	i = n;
    }
    d->count = 0;
    d->numRows = 1;
    d->numColumns = 1;
    d->currentRow = 0;
    d->currentColumn = 0;
    d->mousePressRow = -1;
    d->mousePressColumn = -1;
    d->mouseMoveRow = -1;
    d->mouseMoveColumn = -1;
    d->selectable.clear();
    clearSelection();
    blockSignals( blocked );
    triggerUpdate( TRUE );
    d->last = 0;
    d->clearing = FALSE;
}


/*!
    Returns the text at position \a index, or TQString::null if there
    is no text at that position.

    \sa pixmap()
*/

TQString TQListBox::text( int index ) const
{
    TQListBoxItem * i = item( index );
    if ( i )
	return i->text();
    return TQString::null;
}


/*!
    Returns a pointer to the pixmap at position \a index, or 0 if
    there is no pixmap there.

    \sa text()
*/

const TQPixmap *TQListBox::pixmap( int index ) const
{
    TQListBoxItem * i = item( index );
    if ( i )
	return i->pixmap();
    return 0;
}

/*!
    \overload

    Replaces the item at position \a index with a new list box text
    item with text \a text.

    The operation is ignored if \a index is out of range.

    \sa insertItem(), removeItem()
*/

void TQListBox::changeItem( const TQString &text, int index )
{
    if( index >= 0 && index < (int)count() )
	changeItem( new TQListBoxText(text), index );
}

/*!
    \overload

    Replaces the item at position \a index with a new list box pixmap
    item with pixmap \a pixmap.

    The operation is ignored if \a index is out of range.

    \sa insertItem(), removeItem()
*/

void TQListBox::changeItem( const TQPixmap &pixmap, int index )
{
    if( index >= 0 && index < (int)count() )
	changeItem( new TQListBoxPixmap(pixmap), index );
}

/*!
    \overload

    Replaces the item at position \a index with a new list box pixmap
    item with pixmap \a pixmap and text \a text.

    The operation is ignored if \a index is out of range.

    \sa insertItem(), removeItem()
*/

void TQListBox::changeItem( const TQPixmap &pixmap, const TQString &text, int index )
{
    if( index >= 0 && index < (int)count() )
	changeItem( new TQListBoxPixmap(pixmap, text), index );
}



/*!
    Replaces the item at position \a index with \a lbi.	If \a index is
    negative or too large, changeItem() does nothing.

    The item that has been changed will become selected.

    \sa insertItem(), removeItem()
*/

void TQListBox::changeItem( const TQListBoxItem *lbi, int index )
{
    if ( !lbi || index < 0 || index >= (int)count() )
	return;

    removeItem( index );
    insertItem( lbi, index );
    setCurrentItem( index );
}


/*!
    \property TQListBox::numItemsVisible
    \brief the number of visible items.

    Both partially and entirely visible items are counted.
*/

int TQListBox::numItemsVisible() const
{
    doLayout();

    int columns = 0;

    int x = contentsX();
    int i=0;
    while ( i < (int)d->columnPos.size()-1 &&
	   d->columnPos[i] < x )
	i++;
    if ( i < (int)d->columnPos.size()-1 &&
	 d->columnPos[i] > x )
	columns++;
    x += visibleWidth();
    while ( i < (int)d->columnPos.size()-1 &&
	   d->columnPos[i] < x ) {
	i++;
	columns++;
    }

    int y = contentsY();
    int rows = 0;
    while ( i < (int)d->rowPos.size()-1 &&
	   d->rowPos[i] < y )
	i++;
    if ( i < (int)d->rowPos.size()-1 &&
	 d->rowPos[i] > y )
	rows++;
    y += visibleHeight();
    while ( i < (int)d->rowPos.size()-1 &&
	   d->rowPos[i] < y ) {
	i++;
	rows++;
    }

    return rows*columns;
}

int TQListBox::currentItem() const
{
    if ( !d->current || !d->head )
	return -1;

    return index( d->current );
}


/*!
    \property TQListBox::currentText
    \brief the text of the current item.

    This is equivalent to text(currentItem()).
*/


/*!
    \property TQListBox::currentItem
    \brief the current highlighted item

    When setting this property, the highlighting is moved to the item
    and the list box scrolled as necessary.

    If no item is current, currentItem() returns -1.
*/

void TQListBox::setCurrentItem( int index )
{
    setCurrentItem( item( index ) );
}


/*!
    \overload

    Sets the current item to the TQListBoxItem \a i.
*/
void TQListBox::setCurrentItem( TQListBoxItem * i )
{
    if ( !i || d->current == i || numRows() == 0 )
	return;

    TQRect mfrect = tqitemRect( i );
    if ( mfrect.isValid() )
	setMicroFocusHint( mfrect.x(), mfrect.y(), mfrect.width(), mfrect.height(), FALSE );

    TQListBoxItem * o = d->current;
    d->current = i;
    int ind = index( i );

    if ( i && selectionMode() == Single ) {
	bool changed = FALSE;
	if ( o && o->s ) {
	    changed = TRUE;
	    o->s = FALSE;
	}
	if ( i && !i->s && d->selectionMode != NoSelection && i->isSelectable() ) {
	    i->s = TRUE;
	    changed = TRUE;
	    emit selectionChanged( i );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::StateChanged );
#endif
	}
	if ( changed ) {
	    emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	    TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
#endif
	}
    }

    d->currentColumn = ind / numRows();
    d->currentRow = ind % numRows();
    if ( o )
	updateItem( o );
    if ( i )
	updateItem( i );
    // scroll after the items are redrawn
    d->visibleTimer->start( 1, TRUE );

    TQString tmp;
    if ( i )
	tmp = i->text();
    emit highlighted( i );
    if ( !tmp.isNull() )
	emit highlighted( tmp );
    emit highlighted( ind );
    emit currentChanged( i );

#if defined(TQT_ACCESSIBILITY_SUPPORT)
    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::Focus );
#endif
}


/*!
    Returns a pointer to the item at position \a index, or 0 if \a
    index is out of bounds.

    \sa index()
*/

TQListBoxItem *TQListBox::item( int index ) const
{
    if ( index < 0 || index > d->count -1 )
	return 0;

    TQListBoxItem * i = d->head;

    if ( d->cache && index > 0 ) {
	i = d->cache;
	int idx = d->cacheIndex;
	while ( i && idx < index ) {
	    idx++;
	    i = i->n;
	}
	while ( i && idx > index ) {
	    idx--;
	    i = i->p;
	}
    } else {
	int idx = index;
	while ( i && idx > 0 ) {
	    idx--;
	    i = i->n;
	}
    }

    if ( index > 0 ) {
	d->cache = i;
	d->cacheIndex = index;
    }

    return i;
}


/*!
    Returns the index of \a lbi, or -1 if the item is not in this list
    box or \a lbi is 0.

    \sa item()
*/

int TQListBox::index( const TQListBoxItem * lbi ) const
{
    if ( !lbi )
	return -1;
    TQListBoxItem * i_n = d->head;
    int c_n = 0;
    if ( d->cache ) {
	i_n = d->cache;
	c_n = d->cacheIndex;
    }
    TQListBoxItem* i_p = i_n;
    int c_p = c_n;
    while ( ( i_n != 0 || i_p != 0 ) && i_n != lbi && i_p != lbi ) {
	if ( i_n ) {
	    c_n++;
	    i_n = i_n->n;
	}
	if ( i_p ) {
	    c_p--;
	    i_p = i_p->p;
	}
    }
    if ( i_p == lbi )
	return c_p;
    if ( i_n == lbi )
	return c_n;
    return -1;
}



/*!
    Returns TRUE if the item at position \a index is at least partly
    visible; otherwise returns FALSE.
*/

bool TQListBox::itemVisible( int index )
{
    TQListBoxItem * i = item( index );
    return i ? itemVisible( i ) : FALSE;
}


/*!
    \overload

    Returns TRUE if \a item is at least partly visible; otherwise
    returns FALSE.
*/

bool TQListBox::itemVisible( const TQListBoxItem * item )
{
    if ( d->layoutDirty )
	doLayout();

    int i = index( item );
    int col = i / numRows();
    int row = i % numRows();
    return ( d->columnPos[col] < contentsX()+visibleWidth() &&
	     d->rowPos[row] < contentsY()+visibleHeight() &&
	     d->columnPos[col+1] > contentsX() &&
	     d->rowPos[row+1] > contentsY() );
}


/*! \reimp */

void TQListBox::mousePressEvent( TQMouseEvent *e )
{
    mousePressEventEx( e );
}

void TQListBox::mousePressEventEx( TQMouseEvent *e )
{
    d->mouseInternalPress = TRUE;
    TQListBoxItem * i = itemAt( e->pos() );

    if ( !i && !d->current && d->head ) {
	d->current = d->head;
	updateItem( d->head );
    }

    if ( !i && ( d->selectionMode != Single || e->button() == Qt::RightButton )
	 && !( e->state() & ControlButton ) )
	clearSelection();

    d->select = d->selectionMode == Multi ? ( i ? !i->isSelected() : FALSE ) : TRUE;
    d->pressedSelected = i && i->s;

    if ( i )
	d->selectAnchor = i;
    if ( i ) {
	switch( selectionMode() ) {
	default:
	case Single:
	    if ( !i->s || i != d->current ) {
		if ( i->isSelectable() )
		    setSelected( i, TRUE );
		else
		    setCurrentItem( i );
	    }
	    break;
	case Extended:
	    if ( i ) {
		if ( !(e->state() & ShiftButton) &&
		     !(e->state() & ControlButton) ) {
		    if ( !i->isSelected() ) {
			bool b = tqsignalsBlocked();
			blockSignals( TRUE );
			clearSelection();
			blockSignals( b );
		    }
		    setSelected( i, TRUE );
		    d->dragging = TRUE; // always assume dragging
		} else if ( e->state() & ShiftButton ) {
		    d->pressedSelected = FALSE;
		    TQListBoxItem *oldCurrent = item( currentItem() );
		    bool down = index( oldCurrent ) < index( i );

		    TQListBoxItem *lit = down ? oldCurrent : i;
		    bool select = d->select;
		    bool blocked = tqsignalsBlocked();
		    blockSignals( TRUE );
		    for ( ;; lit = lit->n ) {
			if ( !lit ) {
			    triggerUpdate( FALSE );
			    break;
			}
			if ( down && lit == i ) {
			    setSelected( i, select );
			    triggerUpdate( FALSE );
			    break;
			}
			if ( !down && lit == oldCurrent ) {
			    setSelected( oldCurrent, select );
			    triggerUpdate( FALSE );
			    break;
			}
			setSelected( lit, select );
		    }
		    blockSignals( blocked );
		    emit selectionChanged();
		} else if ( e->state() & ControlButton ) {
		    setSelected( i, !i->isSelected() );
		    d->pressedSelected = FALSE;
		}
		setCurrentItem( i );
	    }
	    break;
	case Multi:
	    //d->current = i;
	    setSelected( i, !i->s );
	    setCurrentItem( i );
	    break;
	case NoSelection:
	    setCurrentItem( i );
	    break;
	}
    } else {
	bool unselect = TRUE;
	if ( e->button() == Qt::LeftButton ) {
	    if ( d->selectionMode == Multi ||
		 d->selectionMode == Extended ) {
		d->tmpCurrent = d->current;
		d->current = 0;
		updateItem( d->tmpCurrent );
		if ( d->rubber )
		    delete d->rubber;
		d->rubber = 0;
		d->rubber = new TQRect( e->x(), e->y(), 0, 0 );

		if ( d->selectionMode == Extended && !( e->state() & ControlButton ) )
		    selectAll( FALSE );
		unselect = FALSE;
	    }
	    if ( unselect && ( e->button() == Qt::RightButton ||
			       ( selectionMode() == Multi || selectionMode() == Extended ) ) )
		clearSelection();
	}
    }

    // for sanity, in case people are event-filtering or whatnot
    delete d->scrollTimer;
    d->scrollTimer = 0;
    if ( i ) {
	d->mousePressColumn = d->currentColumn;
	d->mousePressRow = d->currentRow;
    } else {
	d->mousePressColumn = -1;
	d->mousePressRow = -1;
    }
    d->ignoreMoves = FALSE;

    d->pressedItem = i;

    emit pressed( i );
    emit pressed( i, e->globalPos() );
    emit mouseButtonPressed( e->button(), i, e->globalPos() );
    if ( e->button() == Qt::RightButton )
	emit rightButtonPressed( i, e->globalPos() );
}


/*! \reimp */

void TQListBox::mouseReleaseEvent( TQMouseEvent *e )
{
    if ( d->selectionMode == Extended &&
	d->dragging ) {
	d->dragging = FALSE;
	if (d->current != d->pressedItem) {
	    updateSelection(); // when we drag, we get an update after we release
	}
    }

    if ( d->rubber ) {
	drawRubber();
	delete d->rubber;
	d->rubber = 0;
	d->current = d->tmpCurrent;
	updateItem( d->current );
    }
    if ( d->scrollTimer )
	mouseMoveEvent( e );
    delete d->scrollTimer;
    d->scrollTimer = 0;
    d->ignoreMoves = FALSE;

    if ( d->selectionMode == Extended &&
	 d->current == d->pressedItem &&
	 d->pressedSelected && d->current ) {
	bool block = tqsignalsBlocked();
	blockSignals( TRUE );
	clearSelection();
	blockSignals( block );
	d->current->s = TRUE;
	emit selectionChanged();
    }

    TQListBoxItem * i = itemAt( e->pos() );
    bool emitClicked = (d->mousePressColumn != -1 && d->mousePressRow != -1) || !d->pressedItem;
    emitClicked = emitClicked && d->pressedItem == i;
    d->pressedItem = 0;
    d->mousePressRow = -1;
    d->mousePressColumn = -1;
    d->mouseInternalPress = FALSE;
    if ( emitClicked ) {
	emit clicked( i );
	emit clicked( i, e->globalPos() );
	emit mouseButtonClicked( e->button(), i, e->globalPos() );
	if ( e->button() == Qt::RightButton )
	    emit rightButtonClicked( i, e->globalPos() );
    }
}


/*! \reimp */

void TQListBox::mouseDoubleClickEvent( TQMouseEvent *e )
{
    bool ok = TRUE;
    TQListBoxItem *i = itemAt( e->pos() );
    if ( !i || selectionMode() == NoSelection )
	ok = FALSE;

    d->ignoreMoves = TRUE;

    if ( d->current && ok ) {
	TQListBoxItem * i = d->current;
	TQString tmp = d->current->text();
	emit selected( currentItem() );
	emit selected( i );
	if ( !tmp.isNull() )
	    emit selected( tmp );
	emit doubleClicked( i );
    }
}


/*! \reimp */

void TQListBox::mouseMoveEvent( TQMouseEvent *e )
{
    TQListBoxItem * i = itemAt( e->pos() );
    if ( i != d->highlighted ) {
	if ( i ) {
	    emit onItem( i );
	} else {
	    emit onViewport();
	}
	d->highlighted = i;
    }

    if ( d->rubber ) {
	TQRect r = d->rubber->normalize();
	drawRubber();
	d->rubber->setCoords( d->rubber->x(), d->rubber->y(), e->x(), e->y() );
	doRubberSelection( r, d->rubber->normalize() );
	drawRubber();
	return;
    }

    if ( ( (e->state() & ( Qt::RightButton | Qt::LeftButton | Qt::MidButton ) ) == 0 ) ||
	 d->ignoreMoves )
	return;

    // hack to keep the combo (and what else?) working: if we get a
    // move outside the listbox without having seen a press, discard
    // it.
    if ( !TQRect( 0, 0, visibleWidth(), visibleHeight() ).tqcontains( e->pos() ) &&
	 ( (d->mousePressColumn < 0 && d->mousePressRow < 0) ||
	   (e->state() == Qt::NoButton && !d->pressedItem) ) )
	return;

    // figure out in what direction to drag-select and perhaps scroll
    int dx = 0;
    int x = e->x();
    if ( x >= visibleWidth() ) {
	x = visibleWidth()-1;
	dx = 1;
    } else if ( x < 0 ) {
	x = 0;
	dx = -1;
    }
    d->mouseMoveColumn = columnAt( x + contentsX() );

    // sanitize mousePressColumn, if we got here without a mouse press event
    if ( d->mousePressColumn < 0 && d->mouseMoveColumn >= 0 )
	d->mousePressColumn = d->mouseMoveColumn;
    if ( d->mousePressColumn < 0 && d->currentColumn >= 0 )
	d->mousePressColumn = d->currentColumn;

    // if it's beyond the last column, use the last one
    if ( d->mouseMoveColumn < 0 )
	d->mouseMoveColumn = dx >= 0 ? numColumns()-1 : 0;

    // repeat for y
    int dy = 0;
    int y = e->y();
    if ( y >= visibleHeight() ) {
	y = visibleHeight()-1;
	dy = 1;
    } else if ( y < 0 ) {
	y = 0;
	dy = -1;
    }
    d->mouseMoveRow = rowAt( y + contentsY() );

    if ( d->mousePressRow < 0 && d->mouseMoveRow >= 0 )
	d->mousePressRow = d->mouseMoveRow;
    if ( d->mousePressRow < 0 && d->currentRow >= 0 )
	d->mousePressRow = d->currentRow;

    if ( d->mousePressRow < 0 )
	d->mousePressRow = rowAt( x + contentsX() );

    d->scrollPos = TQPoint( dx, dy );

    if ( ( dx || dy ) && !d->scrollTimer && e->state() == Qt::LeftButton && e->button() != Qt::LeftButton ) {
	// start autoscrolling if necessary
	d->scrollTimer = new TQTimer( this );
	connect( d->scrollTimer, TQT_SIGNAL(timeout()),
		 this, TQT_SLOT(doAutoScroll()) );
	d->scrollTimer->start( 100, FALSE );
	doAutoScroll();
    } else if ( !d->scrollTimer ) {
	// or just select the required bits
	updateSelection();
    }
}



void TQListBox::updateSelection()
{
    if ( d->mouseMoveColumn >= 0 && d->mouseMoveRow >= 0 &&
	 d->mousePressColumn >= 0 && d->mousePressRow >= 0 ) {
	TQListBoxItem * i = item( d->mouseMoveColumn * numRows() +
				 d->mouseMoveRow );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	int ind = index(i);
#endif
	if ( selectionMode() == Single || selectionMode() == NoSelection ) {
	    if ( i && ( d->mouseInternalPress || testWFlags(WType_Popup) ) )
		setCurrentItem( i );
	} else {
	    if ( d->selectionMode == Extended && (
		 ( d->current == d->pressedItem && d->pressedSelected ) ||
		(d->dirtyDrag && !d->dragging) ) ) {
		if (d->dirtyDrag && !d->dragging) // emit after dragging stops
		    d->dirtyDrag = FALSE;
		else
		    clearSelection(); // dont reset drag-selected items
		d->pressedItem = 0;
		if ( i && i->isSelectable() ) {
		    bool block = tqsignalsBlocked();
		    blockSignals( TRUE );
		    i->s = TRUE;
		    blockSignals( block );
		    emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
		    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::StateChanged );
		    TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
		    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::SelectionAdd );
#endif
		}
		triggerUpdate( FALSE );
	    } else {
		int c = TQMIN( d->mouseMoveColumn, d->mousePressColumn );
		int r = TQMIN( d->mouseMoveRow, d->mousePressRow );
		int c2 = TQMAX( d->mouseMoveColumn, d->mousePressColumn );
		int r2 = TQMAX( d->mouseMoveRow, d->mousePressRow );
		bool changed = FALSE;
		while( c <= c2 ) {
		    TQListBoxItem * i = item( c*numRows()+r );
		    int rtmp = r;
		    while( i && rtmp <= r2 ) {
			if ( (bool)i->s != (bool)d->select && i->isSelectable() ) {
			    i->s = d->select;
#if defined(TQT_ACCESSIBILITY_SUPPORT)
			    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::StateChanged );
			    TQAccessible::updateAccessibility( viewport(), ind+1, d->select ? TQAccessible::SelectionAdd : TQAccessible::SelectionRemove );
#endif
			    i->dirty = TRUE;
			    d->dirtyDrag = changed = TRUE;
			}
			i = i->n;
			rtmp++;
		    }
		    c++;
		}
		if ( changed ) {
		    if (!d->dragging) // emit after dragging stops instead
			emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
		    TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
#endif
		    triggerUpdate( FALSE );
		}
	    }
	    if ( i )
		setCurrentItem( i );
	}
    }
}

void TQListBox::repaintSelection()
{
    if ( d->numColumns == 1 ) {
	for ( uint i = topItem(); itemVisible( i ) && i < count(); ++i ) {
	    TQListBoxItem *it = item(i);
	    if ( !it )
		break;
	    if ( it->isSelected() )
		updateItem( it );
	}
    } else {
	for ( uint i = 0; i < count(); ++i ) {
	    TQListBoxItem *it = item(i);
	    if ( !it )
		break;
	    if ( it->isSelected() )
		updateItem( it );
	}
    }
}

/*! \reimp
*/

void TQListBox::contentsContextMenuEvent( TQContextMenuEvent *e )
{
    if ( !tqreceivers( TQT_SIGNAL(contextMenuRequested(TQListBoxItem*,const TQPoint&)) ) ) {
	e->ignore();
	return;
    }
    if ( e->reason() == TQContextMenuEvent::Keyboard ) {
	TQListBoxItem *i = item( currentItem() );
	if ( i ) {
	    TQRect r = tqitemRect( i );
	    emit contextMenuRequested( i, mapToGlobal( r.topLeft() + TQPoint( width() / 2, r.height() / 2 ) ) );
	}
    } else {
	TQListBoxItem * i = itemAt( contentsToViewport( e->pos() ) );
	emit contextMenuRequested( i, e->globalPos() );
    }
}

/*!\reimp
*/
void TQListBox::keyPressEvent( TQKeyEvent *e )
{
    if ( ( e->key() == Qt::Key_Tab || e->key() == Qt::Key_Backtab )
	 && e->state() & ControlButton )
	e->ignore();

    if ( count() == 0 ) {
	e->ignore();
	return;
    }

    TQGuardedPtr<TQListBox> selfCheck = this;

    TQListBoxItem *old = d->current;
    if ( !old ) {
	setCurrentItem( d->head );
	if ( d->selectionMode == Single )
	    setSelected( d->head, TRUE );
	e->ignore();
	return;
    }

    bool selectCurrent = FALSE;
    switch ( e->key() ) {
	case Qt::Key_Up:
	    {
		d->currInputString = TQString::null;
		if ( currentItem() > 0 ) {
		    setCurrentItem( currentItem() - 1 );
		    handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		}
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Down:
	    {
		d->currInputString = TQString::null;
		if ( currentItem() < (int)count() - 1 ) {
		    setCurrentItem( currentItem() + 1 );
		    handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		}
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Left:
	    {
		d->currInputString = TQString::null;
		if ( currentColumn() > 0 ) {
		    setCurrentItem( currentItem() - numRows() );
		    handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		} else if ( numColumns() > 1 && currentItem() > 0 ) {
		    int row = currentRow();
		    setCurrentItem( currentRow() - 1 + ( numColumns() - 1 ) * numRows() );

		    if ( currentItem() == -1 )
			setCurrentItem( row - 1 + ( numColumns() - 2 ) * numRows() );

		    handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		} else {
		    TQApplication::sendEvent( horizontalScrollBar(), e );
		}
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Right:
	    {
		d->currInputString = TQString::null;
		if ( currentColumn() < numColumns()-1 ) {
		    int row = currentRow();
		    int i = currentItem();
		    TQListBoxItem *it = item( i + numRows() );
		    if ( !it )
			it = item( count()-1 );
		    setCurrentItem( it );

		    if ( currentItem() == -1 ) {
			if ( row < numRows() - 1 )
			    setCurrentItem( row + 1 );
			else
			    setCurrentItem( i );
		    }

		    handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		} else if ( numColumns() > 1 && currentRow() < numRows() ) {
		    if ( currentRow() + 1 < numRows() ) {
			setCurrentItem( currentRow() + 1 );
			handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		    }
		} else {
		    TQApplication::sendEvent( horizontalScrollBar(), e );
		}
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case TQt::Key_Next:
	    {
		d->currInputString = TQString::null;
		int i = 0;
		if ( numColumns() == 1 ) {
		    i = currentItem() + numItemsVisible();
		    i = i > (int)count() - 1 ? (int)count() - 1 : i;
		    setCurrentItem( i );
		    setBottomItem( i );
		} else {
		    // I'm not sure about this behavior...
		    if ( currentRow() == numRows() - 1 )
			i = currentItem() + numRows();
		    else
			i = currentItem() + numRows() - currentRow() - 1;
		    i = i > (int)count() - 1 ? (int)count() - 1 : i;
		    setCurrentItem( i );
		}
		handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case TQt::Key_Prior:
	    {
		selectCurrent = TRUE;
		d->currInputString = TQString::null;
		int i;
		if ( numColumns() == 1 ) {
		    i = currentItem() - numItemsVisible();
		    i = i < 0 ? 0 : i;
		    setCurrentItem( i );
		    setTopItem( i );
		} else {
		    // I'm not sure about this behavior...
		    if ( currentRow() == 0 )
			i = currentItem() - numRows();
		    else
			i = currentItem() - currentRow();
		    i = i < 0 ? 0 : i;
		    setCurrentItem( i );
		}
		handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Space:
	    {
		selectCurrent = TRUE;
		d->currInputString = TQString::null;
		toggleCurrentItem();
		if ( selectionMode() == Extended && d->current->isSelected() )
		    emit highlighted( currentItem() );
		if (selfCheck && (!( e->state() & ShiftButton ) || !d->selectAnchor))
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Return:
	case Qt::Key_Enter:
	    {
		selectCurrent = TRUE;
		d->currInputString = TQString::null;
		if ( currentItem() >= 0 && selectionMode() != NoSelection ) {
		    TQString tmp = item( currentItem() )->text();
		    emit selected( currentItem());
		    emit selected( item( currentItem() ) );
		    if ( !tmp.isEmpty() )
			emit selected( tmp );
		    emit returnPressed( item( currentItem() ) );
		}
		if (selfCheck && (!( e->state() & ShiftButton ) || !d->selectAnchor))
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_Home:
	    {
		selectCurrent = TRUE;
		d->currInputString = TQString::null;
		setCurrentItem( 0 );
		handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	case Qt::Key_End:
	    {
		selectCurrent = TRUE;
		d->currInputString = TQString::null;
		int i = (int)count() - 1;
		setCurrentItem( i );
		handleItemChange( old, e->state() & ShiftButton, e->state() & ControlButton );
		if ( !( e->state() & ShiftButton ) || !d->selectAnchor )
		    d->selectAnchor = d->current;
	    }
	    break;
	default:
	    {
		if ( !e->text().isEmpty() && e->text()[ 0 ].isPrint() && count() ) {
		    int curItem = currentItem();
		    if ( curItem == -1 )
			curItem = 0;
		    if ( !d->inputTimer->isActive() ) {
			d->currInputString = e->text();
			curItem = d->tqfindItemByName( ++curItem, d->currInputString );
		    } else {
			d->inputTimer->stop();
			d->currInputString += e->text();
			int oldCurItem = curItem;
			curItem = d->tqfindItemByName( curItem, d->currInputString );
			if ( curItem < 0 ) {
			    curItem = d->tqfindItemByName( ++oldCurItem, e->text() );
			    d->currInputString = e->text();
			}
		    }
		    if ( curItem >= 0 )
			setCurrentItem( curItem );
		    if ( curItem >= 0 && selectionMode() == TQListBox::Extended ) {
			bool changed = FALSE;
			bool block = tqsignalsBlocked();
			blockSignals( TRUE );
			selectAll( FALSE );
			blockSignals( block );
			TQListBoxItem *i = item( curItem );
			if ( !i->s && i->isSelectable() ) {
			    changed = TRUE;
			    i->s = TRUE;
			    updateItem( i );
			}
			if ( changed )
			    emit selectionChanged();
		    }
		    d->inputTimer->start( 400, TRUE );
		} else {
		    d->currInputString = TQString::null;
		    if ( e->state() & ControlButton ) {
			switch ( e->key() ) {
			    case Qt::Key_A:
				selectAll( TRUE );
				break;
			}
		    } else {
			e->ignore();
		    }
		}
	    }
    }

    if (selfCheck && selectCurrent && selectionMode() == Single &&
	d->current && !d->current->s ) {
	    updateItem( d->current );
	    setSelected( d->current, TRUE );
	}
}


/*!\reimp
*/
void TQListBox::focusInEvent( TQFocusEvent* tqfe )
{
    d->mousePressRow = -1;
    d->mousePressColumn = -1;
    d->inMenuMode = FALSE;
#ifdef USE_QT4
    if ( tqfe->reason() != TQFocusEvent::Mouse && !d->current && d->head ) {
#else // USE_QT4
    if ( TQFocusEvent::reason() != TQFocusEvent::Mouse && !d->current && d->head ) {
#endif // USE_QT4
	d->current = d->head;
	TQListBoxItem *i = d->current;
	TQString tmp;
	if ( i )
	    tmp = i->text();
	int tmp2 = index( i );
	emit highlighted( i );
	if ( !tmp.isNull() )
	    emit highlighted( tmp );
	emit highlighted( tmp2 );
	emit currentChanged( i );
    }
    if ( tqstyle().tqstyleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this ) )
	repaintSelection();

    if ( d->current ) {
	updateItem( currentItem() );
	TQRect mfrect = tqitemRect( d->current );
	if ( mfrect.isValid() )
	    setMicroFocusHint( mfrect.x(), mfrect.y(), mfrect.width(), mfrect.height(), FALSE );
    }
}


/*!\reimp
*/
void TQListBox::focusOutEvent( TQFocusEvent* tqfe)
{
    if (tqstyle().tqstyleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this )) {
	d->inMenuMode =
#ifdef USE_QT4
	    tqfe->reason() == TQFocusEvent::Popup ||
#else // USE_QT4
	    TQFocusEvent::reason() == TQFocusEvent::Popup ||
#endif // USE_QT4
 	    (tqApp->tqfocusWidget() && tqApp->tqfocusWidget()->inherits("TQMenuBar"));
 	if ( !d->inMenuMode )
	    repaintSelection();
    }

    if ( d->current )
	updateItem( currentItem() );
}

/*!\reimp
*/
bool TQListBox::eventFilter( TQObject *o, TQEvent *e )
{
    //### remove in 4.0
    return TQScrollView::eventFilter( o, e );
}

/*!
    Repaints the item at position \a index in the list.
*/

void TQListBox::updateItem( int index )
{
    if ( index >= 0 )
	updateItem( item( index ) );
}


/*!
    \overload

    Repaints the TQListBoxItem \a i.
*/

void TQListBox::updateItem( TQListBoxItem * i )
{
    if ( !i )
	return;
    i->dirty = TRUE;
    d->updateTimer->start( 0, TRUE );
}


/*!
    \property TQListBox::selectionMode
    \brief the selection mode of the list box

    Sets the list box's selection mode, which may be one of \c Single
    (the default), \c Extended, \c Multi or \c NoSelection.

    \sa SelectionMode
*/

void TQListBox::setSelectionMode( SelectionMode mode )
{
    if ( d->selectionMode == mode )
	return;

    if ( ( selectionMode() == Multi || selectionMode() == Extended )
	 && ( mode == TQListBox::Single || mode == TQListBox::NoSelection ) ){
	clearSelection();
	if ( ( mode == TQListBox::Single ) && currentItem() )
	    setSelected( currentItem(), TRUE );
    }

    d->selectionMode = mode;
    triggerUpdate( TRUE );
}


TQListBox::SelectionMode TQListBox::selectionMode() const
{
    return d->selectionMode;
}


/*!
  \obsolete
  \property TQListBox::multiSelection
  \brief whether or not the list box is in Multi selection mode

  Consider using the \l TQListBox::selectionMode property instead of
  this property.

  When setting this property, Multi selection mode is used if set to TRUE and
  to Single selection mode if set to FALSE.

  When getting this property, TRUE is returned if the list box is in
  Multi selection mode or Extended selection mode, and FALSE if it is
  in Single selection mode or NoSelection mode.

  \sa selectionMode
*/

bool TQListBox::isMultiSelection() const
{
    return selectionMode() == Multi || selectionMode() == Extended;
}

void TQListBox::setMultiSelection( bool enable )
{
    setSelectionMode( enable ? Multi : Single );
}


/*!
    Toggles the selection status of currentItem() and repaints if the
    list box is a \c Multi selection list box.

    \sa setMultiSelection()
*/

void TQListBox::toggleCurrentItem()
{
    if ( selectionMode() == Single ||
	 selectionMode() == NoSelection ||
	 !d->current )
	return;

    if ( d->current->s || d->current->isSelectable() ) {
	d->current->s = !d->current->s;
	emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	int ind = index( d->current );
	TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
	TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::StateChanged );
	TQAccessible::updateAccessibility( viewport(), ind+1, d->current->s ? TQAccessible::SelectionAdd : TQAccessible::SelectionRemove );
#endif
    }
    updateItem( d->current );
}


/*!
    \overload

    If \a select is TRUE the item at position \a index is selected;
    otherwise the item is deselected.
*/

void TQListBox::setSelected( int index, bool select )
{
    setSelected( item( index ), select );
}


/*!
    Selects \a item if \a select is TRUE or unselects it if \a select
    is FALSE, and repaints the item appropriately.

    If the list box is a \c Single selection list box and \a select is
    TRUE, setSelected() calls setCurrentItem().

    If the list box is a \c Single selection list box, \a select is
    FALSE, setSelected() calls clearSelection().

    \sa setMultiSelection(), setCurrentItem(), clearSelection(), currentItem()
*/

void TQListBox::setSelected( TQListBoxItem * item, bool select )
{
    if ( !item || !item->isSelectable() ||
	(bool)item->s == select || d->selectionMode == NoSelection )
	return;

    int ind = index( item );
    bool emitHighlighted = (d->current != item) ||
			   ( item->s != (uint) select && select );
    if ( selectionMode() == Single ) {
	if ( d->current != item ) {
	    TQListBoxItem *o = d->current;
	    if ( d->current && d->current->s )
		d->current->s = FALSE;
	    d->current = item;
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	    TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::Focus );
#endif
	    d->currentColumn = ind / numRows();
	    d->currentRow = ind % numRows();

	    if ( o )
		updateItem( o );
	}
    }

    item->s = (uint)select;
    updateItem( item );

    if ( d->selectionMode == Single && select ) {
	emit selectionChanged( item );
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( viewport(), ind+1, TQAccessible::StateChanged );
#endif
    }
    emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
    TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
    if ( d->selectionMode != Single )
	TQAccessible::updateAccessibility( viewport(), ind+1, select ? TQAccessible::SelectionAdd : TQAccessible::SelectionRemove );
#endif

    if ( emitHighlighted ) {
	TQString tmp;
	if ( d->current )
	    tmp = d->current->text();
	int tmp2 = index( d->current );
	emit highlighted( d->current );
	if ( !tmp.isNull() )
	    emit highlighted( tmp );
	emit highlighted( tmp2 );
	emit currentChanged( d->current );
    }
}


/*!
    Returns TRUE if item \a i is selected; otherwise returns FALSE.
*/

bool TQListBox::isSelected( int i ) const
{
    if ( selectionMode() == Single && i != currentItem() )
	return FALSE;

    TQListBoxItem * lbi = item( i );
    if ( !lbi )
	return FALSE; // should not happen
    return lbi->s;
}


/*!
    \overload

    Returns TRUE if item \a i is selected; otherwise returns FALSE.
*/

bool TQListBox::isSelected( const TQListBoxItem * i ) const
{
    if ( !i )
	return FALSE;

    return i->s;
}

/*!  Returns the selected item if the list box is in
single-selection mode and an item is selected.

If no items are selected or the list box is in another selection mode
this function returns 0.

\sa setSelected() setMultiSelection()
*/

TQListBoxItem* TQListBox::selectedItem() const
{
    if ( d->selectionMode != Single )
	return 0;
    if ( isSelected( currentItem() ) )
	return  d->current;
    return 0;
}


/*!
    Deselects all items, if possible.

    Note that a \c Single selection list box will automatically select
    an item if it has keyboard focus.
*/

void TQListBox::clearSelection()
{
    selectAll( FALSE );
}

/*!
    In \c Multi and \c Extended modes, this function sets all items to
    be selected if \a select is TRUE, and to be unselected if \a
    select is FALSE.

    In \c Single and \c NoSelection modes, this function only changes
    the selection status of currentItem().
*/

void TQListBox::selectAll( bool select )
{
    if ( selectionMode() == Multi || selectionMode() == Extended ) {
	bool b = tqsignalsBlocked();
	blockSignals( TRUE );
	for ( int i = 0; i < (int)count(); i++ )
	    setSelected( i, select );
	blockSignals( b );
	emit selectionChanged();
    } else if ( d->current ) {
	TQListBoxItem * i = d->current;
	setSelected( i, select );
    }
}

/*!
    Inverts the selection. Only works in \c Multi and \c Extended
    selection mode.
*/

void TQListBox::invertSelection()
{
    if ( d->selectionMode == Single ||
	 d->selectionMode == NoSelection )
	return;

    bool b = tqsignalsBlocked();
    blockSignals( TRUE );
    for ( int i = 0; i < (int)count(); i++ )
	setSelected( i, !item( i )->isSelected() );
    blockSignals( b );
    emit selectionChanged();
}


/*!
  \obsolete
  Not used anymore; provided for binary compatibility
*/

void TQListBox::emitChangedSignal( bool )
{
}


/*! \reimp */

TQSize TQListBox::tqsizeHint() const
{
    if ( cachedSizeHint().isValid() )
	return cachedSizeHint();

    constPolish();
    doLayout();

    int i=0;
    while( i < 10 &&
	   i < (int)d->columnPos.size()-1 &&
	   d->columnPos[i] < 200 )
	i++;
    int x;
    x = TQMIN( 200, d->columnPos[i] +
	      2 * tqstyle().tqpixelMetric( TQStyle::PM_DefaultFrameWidth ) );
    x = TQMAX( 40, x );

    i = 0;
    while( i < 10 &&
	   i < (int)d->rowPos.size()-1 &&
	   d->rowPos[i] < 200 )
	i++;
    int y;
    y = TQMIN( 200, d->rowPos[i] +
	      2 * tqstyle().tqpixelMetric( TQStyle::PM_DefaultFrameWidth ) );
    y = TQMAX( 40, y );

    TQSize s( x, y );
    setCachedSizeHint( s );
    return s;
}

/*!
  \reimp
*/

TQSize TQListBox::tqminimumSizeHint() const
{
    return TQScrollView::tqminimumSizeHint();
}


/*!
    Ensures that a single paint event will occur at the end of the
    current event loop iteration. If \a doLayout is TRUE, the tqlayout
    is also redone.
*/

void TQListBox::triggerUpdate( bool doLayout )
{
    if ( doLayout )
	d->layoutDirty = d->mustPaintAll = TRUE;
    d->updateTimer->start( 0, TRUE );
}


void TQListBox::setColumnMode( LayoutMode mode )
{
    if ( mode == Variable )
	return;
    d->rowModeWins = FALSE;
    d->columnMode = mode;
    triggerUpdate( TRUE );
}


void TQListBox::setColumnMode( int columns )
{
    if ( columns < 1 )
	columns = 1;
    d->columnMode = FixedNumber;
    d->numColumns = columns;
    d->rowModeWins = FALSE;
    triggerUpdate( TRUE );
}

void TQListBox::setRowMode( LayoutMode mode )
{
    if ( mode == Variable )
	return;
    d->rowModeWins = TRUE;
    d->rowMode = mode;
    triggerUpdate( TRUE );
}


void TQListBox::setRowMode( int rows )
{
    if ( rows < 1 )
	rows = 1;
    d->rowMode = FixedNumber;
    d->numRows = rows;
    d->rowModeWins = TRUE;
    triggerUpdate( TRUE );
}

/*!
    \property TQListBox::columnMode
    \brief the column tqlayout mode for this list box.

    setColumnMode() sets the tqlayout mode and adjusts the number of
    displayed columns. The row tqlayout mode automatically becomes \c
    Variable, unless the column mode is \c Variable.

    \sa setRowMode() columnMode() rowMode numColumns
*/


TQListBox::LayoutMode TQListBox::columnMode() const
{
    if ( d->rowModeWins )
	return Variable;
    else
	return d->columnMode;
}


/*!
    \property TQListBox::rowMode
    \brief the row tqlayout mode for this list box

    This property is normally \c Variable.

    setRowMode() sets the tqlayout mode and adjusts the number of
    displayed rows. The column tqlayout mode automatically becomes \c
    Variable, unless the row mode is \c Variable.

    \sa columnMode rowMode
*/


TQListBox::LayoutMode TQListBox::rowMode() const
{
    if ( d->rowModeWins )
	return d->rowMode;
    else
	return Variable;
}


/*!
    \property TQListBox::numColumns
    \brief the number of columns in the list box

    This is normally 1, but can be different if \l
    TQListBox::columnMode or \l TQListBox::rowMode has been set.

    \sa columnMode rowMode numRows
*/

int TQListBox::numColumns() const
{
    if ( count() == 0 )
	return 0;
    if ( !d->rowModeWins && d->columnMode == FixedNumber )
	return d->numColumns;
    doLayout();
    return d->columnPos.size()-1;
}


/*!
    \property TQListBox::numRows
    \brief the number of rows in the list box.

    This is equal to the number of items in the default single-column
    tqlayout, but can be different.

    \sa columnMode rowMode numColumns
*/

int TQListBox::numRows() const
{
    if ( count() == 0 )
	return 0;
    if ( d->rowModeWins && d->rowMode == FixedNumber )
	return d->numRows;
    doLayout();
    return d->rowPos.size()-1;
}


/*!
    This function does the hard tqlayout work. You should never need to
    call it.
*/

void TQListBox::doLayout() const
{
    if ( !d->layoutDirty || d->resizeTimer->isActive() )
	return;
    constPolish();
    int c = count();
    switch( rowMode() ) {
    case FixedNumber:
	// columnMode() is known to be Variable
	tryGeometry( d->numRows, (c+d->numRows-1)/d->numRows );
	break;
    case FitToHeight:
	// columnMode() is known to be Variable
	if ( d->head ) {
	    // this is basically the FitToWidth code, but edited to use rows.
	    int maxh = 0;
	    TQListBoxItem * i = d->head;
	    while ( i ) {
		int h = i->height(this);
		if ( maxh < h )
		    maxh = h;
		i = i->n;
	    }
	    int vh = viewportSize( 1, 1 ).height();
	    do {
		int rows = vh / maxh;
		if ( rows > c )
		    rows = c;
		if ( rows < 1 )
		    rows = 1;
		if ( variableHeight() && rows < c ) {
		    do {
			++rows;
			tryGeometry( rows, (c+rows-1)/rows );
		    } while ( rows <= c &&
			      d->rowPos[(int)d->rowPos.size()-1] <= vh );
		    --rows;
		}
		tryGeometry( rows, (c+rows-1)/rows );
		int nvh = viewportSize( d->columnPos[(int)d->columnPos.size()-1],
					d->rowPos[(int)d->rowPos.size()-1] ).height();
		if ( nvh < vh )
		    vh = nvh;
	    } while ( d->rowPos.size() > 2 &&
		      vh < d->rowPos[(int)d->rowPos.size()-1] );
	} else {
	    tryGeometry( 1, 1 );
	}
	break;
    case Variable:
	if ( columnMode() == FixedNumber ) {
	    tryGeometry( (count()+d->numColumns-1)/d->numColumns,
			 d->numColumns );
	} else if ( d->head ) { // FitToWidth, at least one item
	    int maxw = 0;
	    TQListBoxItem * i = d->head;
	    while ( i ) {
		int w = i->width(this);
		if ( maxw < w )
		    maxw = w;
		i = i->n;
	    }
	    int vw = viewportSize( 1, 1 ).width();
	    do {
		int cols = vw / maxw;
		if ( cols > c )
		    cols = c;
		if ( cols < 1 )
		    cols = 1;
		if ( variableWidth() && cols < c ) {
		    do {
			++cols;
			tryGeometry( (c+cols-1)/cols, cols );
		    } while ( cols <= c &&
			      d->columnPos[(int)d->columnPos.size()-1] <= vw );
		    --cols;
		}
		tryGeometry( (c+cols-1)/cols, cols );
		int nvw = viewportSize( d->columnPos[(int)d->columnPos.size()-1],
					d->rowPos[(int)d->rowPos.size()-1] ).width();
		if ( nvw < vw )
		    vw = nvw;
	    } while ( d->columnPos.size() > 2 &&
		      vw < d->columnPos[(int)d->columnPos.size()-1] );
	} else {
	    tryGeometry( 1, 1 );
	}
	break;
    }

    d->layoutDirty = FALSE;
    int w = d->columnPos[(int)d->columnPos.size()-1];
    int h = d->rowPos[(int)d->rowPos.size()-1];
    TQSize s( viewportSize( w, h ) );
    w = TQMAX( w, s.width() );

    d->columnPosOne = d->columnPos[1];
    // extend the column for simple single-column listboxes
    if ( columnMode() == FixedNumber && d->numColumns == 1 &&
	 d->columnPos[1] < w )
	d->columnPos[1] = w;
    ((TQListBox *)this)->resizeContents( w, h );
}


/*!
    Lay the items out in a \a columns by \a rows array. The array may
    be too big: doLayout() is expected to call this with the right
    values.
*/

void TQListBox::tryGeometry( int rows, int columns ) const
{
    if ( columns < 1 )
	columns = 1;
    d->columnPos.resize( columns+1 );

    if ( rows < 1 )
	rows = 1;
    d->rowPos.resize( rows+1 );

    // funky hack I: dump the height/width of each column/row in
    // {column,row}Pos for later conversion to positions.
    int c;
    for( c=0; c<=columns; c++ )
	d->columnPos[c] = 0;
    int r;
    for( r=0; r<=rows; r++ )
	d->rowPos[r] = 0;
    r = c = 0;
    TQListBoxItem * i = d->head;
    while ( i && c < columns ) {
	if ( i == d->current ) {
	    d->currentRow = r;
	    d->currentColumn = c;
	}

	int w = i->width(this);
	if ( d->columnPos[c] < w )
	    d->columnPos[c] = w;
	int h = i->height(this);
	if ( d->rowPos[r] < h )
	    d->rowPos[r] = h;
	i = i->n;
	r++;
	if ( r == rows ) {
	    r = 0;
	    c++;
	}
    }
    // funky hack II: if not variable {width,height}, unvariablify it.
    if ( !variableWidth() ) {
	int w = 0;
	for( c=0; c<columns; c++ )
	    if ( w < d->columnPos[c] )
		w = d->columnPos[c];
	for( c=0; c<columns; c++ )
	    d->columnPos[c] = w;
    }
    if ( !variableHeight() ) {
	int h = 0;
	for( r=0; r<rows; r++ )
	    if ( h < d->rowPos[r] )
		h = d->rowPos[r];
	for( r=0; r<rows; r++ )
	    d->rowPos[r] = h;
    }
    // repair the hacking.
    int x = 0;
    for( c=0; c<=columns; c++ ) {
	int w = d->columnPos[c];
	d->columnPos[c] = x;
	x += w;
    }
    int y = 0;
    for( r=0; r<=rows; r++ ) {
	int h = d->rowPos[r];
	d->rowPos[r] = y;
	y += h;
    }
}


/*!
    Returns the row index of the current item, or -1 if no item is the
    current item.
*/

int TQListBox::currentRow() const
{
    if ( !d->current )
	return -1;
    if ( d->currentRow < 0 )
	d->layoutDirty = TRUE;
    if ( d->layoutDirty )
	doLayout();
    return d->currentRow;
}


/*!
    Returns the column index of the current item, or -1 if no item is
    the current item.
*/

int TQListBox::currentColumn() const
{
    if ( !d->current )
	return -1;
    if ( d->currentColumn < 0 )
	d->layoutDirty = TRUE;
    if ( d->layoutDirty )
	doLayout();
    return d->currentColumn;
}


void TQListBox::setTopItem( int index )
{
    if ( index >= (int)count() || count() == 0 )
	return;
    int col = index / numRows();
    int y = d->rowPos[index-col*numRows()];
    if ( d->columnPos[col] >= contentsX() &&
	 d->columnPos[col+1] <= contentsX() + visibleWidth() )
	setContentsPos( contentsX(), y );
    else
	setContentsPos( d->columnPos[col], y );
}

/*!
    Scrolls the list box so the item at position \a index in the list
    is displayed in the bottom row of the list box.

    \sa setTopItem()
*/

void TQListBox::setBottomItem( int index )
{
    if ( index >= (int)count() || count() == 0 )
	return;
    int col = index / numRows();
    int y = d->rowPos[1+index-col*numRows()] - visibleHeight();
    if ( y < 0 )
	y = 0;
    if ( d->columnPos[col] >= contentsX() &&
	 d->columnPos[col+1] <= contentsX() + visibleWidth() )
	setContentsPos( contentsX(), y );
    else
	setContentsPos( d->columnPos[col], y );
}


/*!
    Returns the item at point \a p, specified in viewport coordinates,
    or a 0 if there is no item at \a p.

    Use contentsToViewport() to convert between widget coordinates and
    viewport coordinates.
*/

TQListBoxItem * TQListBox::itemAt( const TQPoint& p ) const
{
    if ( d->layoutDirty )
	doLayout();
    TQPoint np = p;

    // take into acount frame margin to get to viewport
    np -= viewport()->pos();
    if (!TQT_TQRECT_OBJECT(viewport()->rect()).tqcontains(np))
	return 0;

    // take into account contents position
    np = viewportToContents( np );

    int x = np.x();
    int y = np.y();

    // return 0 when y is below the last row
    if ( y > d->rowPos[ numRows() ] )
	return 0;

    int col = columnAt( x );
    int row = rowAt( y );

    TQListBoxItem *i = item( col * numRows()  + row );
    if ( i && numColumns() > 1 ) {
	if ( d->columnPos[ col ] + i->width( this ) >= x )
	    return i;
    } else {
	if ( d->columnPos[ col + 1 ] >= x )
	    return i;
    }
    return 0;
}


/*!
    Ensures that the current item is visible.
*/

void TQListBox::ensureCurrentVisible()
{
    if ( !d->current )
	return;

    doLayout();

    int row = currentRow();
    int column = currentColumn();
    int w = ( d->columnPos[column+1] - d->columnPos[column] ) / 2;
    int h = ( d->rowPos[row+1] - d->rowPos[row] ) / 2;
    // next four lines are Bad.  they mean that for pure left-to-right
    // languages, textual list box items are displayed better than
    // before when there is little space.  for non-textual items, or
    // other languages, it means... that you really should have enough
    // space in the first place :)
    if ( numColumns() == 1 )
	w = 0;
    if ( w*2 > viewport()->width() )
	w = viewport()->width()/2;

    ensureVisible( d->columnPos[column] + w, d->rowPos[row] + h, w, h);
}


/*! \internal */

void TQListBox::doAutoScroll()
{
    if ( d->scrollPos.x() < 0 ) {
	// scroll left
	int x = contentsX() - horizontalScrollBar()->lineStep();
	if ( x < 0 )
	    x = 0;
	if ( x != contentsX() ) {
	    d->mouseMoveColumn = columnAt( x );
	    updateSelection();
	    if ( x < contentsX() )
		setContentsPos( x, contentsY() );
	}
    } else if ( d->scrollPos.x() > 0 ) {
	// scroll right
	int x = contentsX() + horizontalScrollBar()->lineStep();
	if ( x + visibleWidth() > contentsWidth() )
	    x = contentsWidth() - visibleWidth();
	if ( x != contentsX() ) {
	    d->mouseMoveColumn = columnAt( x + visibleWidth() - 1 );
	    updateSelection();
	    if ( x > contentsX() )
		setContentsPos( x, contentsY() );
	}
    }

    if ( d->scrollPos.y() < 0 ) {
	// scroll up
	int y = contentsY() - verticalScrollBar()->lineStep();
	if ( y < 0 )
	    y = 0;
	if ( y != contentsY() ) {
	    y = contentsY() - verticalScrollBar()->lineStep();
	    d->mouseMoveRow = rowAt( y );
	    updateSelection();
	}
    } else if ( d->scrollPos.y() > 0 ) {
	// scroll down
	int y = contentsY() + verticalScrollBar()->lineStep();
	if ( y + visibleHeight() > contentsHeight() )
	    y = contentsHeight() - visibleHeight();
	if ( y != contentsY() ) {
	    y = contentsY() + verticalScrollBar()->lineStep();
	    d->mouseMoveRow = rowAt(y + visibleHeight() - 1 );
	    updateSelection();
	}
    }

    if ( d->scrollPos == TQPoint( 0, 0 ) ) {
	delete d->scrollTimer;
	d->scrollTimer = 0;
    }
}


/*!
    \property TQListBox::topItem
    \brief the index of an item at the top of the screen.

    When getting this property and the listbox has multiple columns,
    an arbitrary item is selected and returned.

    When setting this property, the list box is scrolled so the item
    at position \e index in the list is displayed in the top row of
    the list box.
*/

int TQListBox::topItem() const
{
    doLayout();

    // move rightwards to the best column
    int col = columnAt( contentsX() );
    int row = rowAt( contentsY() );
    return col * numRows() + row;
}


/*!
    \property TQListBox::variableHeight
    \brief whether this list box has variable-height rows

    When the list box has variable-height rows (the default), each row
    is as high as the highest item in that row. When it has same-sized
    rows, all rows are as high as the highest item in the list box.

    \sa variableWidth
*/

bool TQListBox::variableHeight() const
{
    return d->variableHeight;
}


void TQListBox::setVariableHeight( bool enable )
{
    if ( (bool)d->variableHeight == enable )
	return;

    d->variableHeight = enable;
    triggerUpdate( TRUE );
}


/*!
    \property TQListBox::variableWidth
    \brief whether this list box has variable-width columns

    When the list box has variable-width columns, each column is as
    wide as the widest item in that column. When it has same-sized
    columns (the default), all columns are as wide as the widest item
    in the list box.

    \sa variableHeight
*/

bool TQListBox::variableWidth() const
{
    return d->variableWidth;
}


void TQListBox::setVariableWidth( bool enable )
{
    if ( (bool)d->variableWidth == enable )
	return;

    d->variableWidth = enable;
    triggerUpdate( TRUE );
}


/*!
    Repaints only what really needs to be repainted.
*/
void TQListBox::refreshSlot()
{
    if ( d->mustPaintAll ||
	 d->layoutDirty ) {
	d->mustPaintAll = FALSE;
	bool currentItemVisible = itemVisible( currentItem() );
	doLayout();
	if ( hasFocus() &&
	     currentItemVisible &&
	     d->currentColumn >= 0 &&
	     d->currentRow >= 0 &&
	     ( d->columnPos[d->currentColumn] < contentsX() ||
	       d->columnPos[d->currentColumn+1]>contentsX()+visibleWidth() ||
	       d->rowPos[d->currentRow] < contentsY() ||
	       d->rowPos[d->currentRow+1] > contentsY()+visibleHeight() ) )
	    ensureCurrentVisible();
	viewport()->tqrepaint( FALSE );
	return;
    }

    TQRegion r;
    int x = contentsX();
    int y = contentsY();
    int col = columnAt( x );
    int row = rowAt( y );
    int top = row;
    while( col < (int)d->columnPos.size()-1 && d->columnPos[col+1] < x )
	col++;
    while( top < (int)d->rowPos.size()-1 && d->rowPos[top+1] < y )
	top++;
    TQListBoxItem * i = item( col * numRows() + row );

    while ( i && (int)col < numColumns() &&
	    d->columnPos[col] < x + visibleWidth()  ) {
	int cw = d->columnPos[col+1] - d->columnPos[col];
	while ( i && row < numRows() && d->rowPos[row] <
		y + visibleHeight() ) {
	    if ( i->dirty )
		r = r.unite( TQRect( d->columnPos[col] - x, d->rowPos[row] - y,
				    cw, d->rowPos[row+1] - d->rowPos[row] ) );
	    row++;
	    i = i->n;
	}
	col++;
	if ( numColumns() > 1 ) {
	    row = top;
	    i = item( col *  numRows() + row );
	}
    }

    if ( r.isEmpty() )
	viewport()->tqrepaint( FALSE );
    else
	viewport()->tqrepaint( r, FALSE );
}


/*! \reimp */

void TQListBox::viewportPaintEvent( TQPaintEvent * e )
{
    doLayout();
    TQWidget* vp = viewport();
    TQPainter p( vp );
    TQRegion r = e->region();

#if 0
    {
	// this stuff has been useful enough times that from now I'm
	//  leaving it in the source.
	uint i = 0;
	qDebug( "%s/%s: %i rects", className(), name(), r.rects().size() );
	while( i < r.rects().size() ) {
	    qDebug( "rect %d: %d, %d, %d, %d", i,
		   r.rects()[i].left(), r.rects()[i].top(),
		   r.rects()[i].width(), r.rects()[i].height() );
	    i++;
	}
	qDebug( "" );
    }
#endif

    int x = contentsX();
    int y = contentsY();
    int w = vp->width();
    int h = vp->height();

    int col = columnAt( x );
    int top = rowAt( y );
    int row = top;

    TQListBoxItem * i = item( col*numRows() + row );

    const TQColorGroup & g = tqcolorGroup();
    p.setPen( g.text() );
    p.setBackgroundColor( backgroundBrush().color() );
    while ( i && (int)col < numColumns() && d->columnPos[col] < x + w ) {
	int cw = d->columnPos[col+1] - d->columnPos[col];
	while ( i && (int)row < numRows() && d->rowPos[row] < y + h ) {
	    int ch = d->rowPos[row+1] - d->rowPos[row];
	    TQRect tqitemRect( d->columnPos[col]-x,  d->rowPos[row]-y, cw, ch );
	    TQRegion tempRegion( tqitemRect );
	    TQRegion itemPaintRegion( tempRegion.intersect( r  ) );
	    if ( !itemPaintRegion.isEmpty() ) {
		p.save();
		p.setClipRegion( itemPaintRegion );
		p.translate( d->columnPos[col]-x, d->rowPos[row]-y );
		paintCell( &p, row, col );
		p.restore();
		r = r.subtract( itemPaintRegion );
	    }
	    row++;
	    if ( i->dirty ) {
		// reset dirty flag only if the entire item was painted
		if ( itemPaintRegion == TQRegion( tqitemRect ) )
		    i->dirty = FALSE;
	    }
	    i = i->n;
	}
	col++;
	if ( numColumns() > 1 ) {
	    row = top;
	    i = item( col *  numRows() + row );
	}
    }

    if ( r.isEmpty() )
	return;
    p.setClipRegion( r );
    p.fillRect( 0, 0, w, h, viewport()->backgroundBrush() );
}


/*!
    Returns the height in pixels of the item with index \a index. \a
    index defaults to 0.

    If \a index is too large, this function returns 0.
*/

int TQListBox::itemHeight( int index ) const
{
    if ( index >= (int)count() || index < 0 )
	return 0;
    int r = index % numRows();
    return d->rowPos[r+1] - d->rowPos[r];
}


/*!
    Returns the index of the column at \a x, which is in the listbox's
    coordinates, not in on-screen coordinates.

    If there is no column that spans \a x, columnAt() returns -1.
*/

int TQListBox::columnAt( int x ) const
{
    if ( x < 0 )
	return -1;
    if ( !d->columnPos.size() )
	return -1;
    if ( x >= d->columnPos[(int)d->columnPos.size()-1 ] )
	return numColumns() - 1;

    int col = 0;
    while( col < (int)d->columnPos.size()-1 && d->columnPos[col+1] < x )
	col++;
    return col;
}


/*!
    Returns the index of the row at \a y, which is in the listbox's
    coordinates, not in on-screen coordinates.

    If there is no row that spans \a y, rowAt() returns -1.
*/

int TQListBox::rowAt( int y ) const
{
    if ( y < 0 )
	return -1;

    // tqfind the top item, use bsearch for speed
    int l = 0;
    int r = d->rowPos.size() - 2;
    if ( r < 0 )
	return -1;
    if ( l <= d->rowPosCache && d->rowPosCache <= r ) {
	if ( d->rowPos[ TQMAX( l, d->rowPosCache - 10 ) ] <= y
	     && y <= d->rowPos[ TQMIN( r, d->rowPosCache + 10 ) ] ) {
	    l = TQMAX( l, d->rowPosCache - 10 );
	    r = TQMIN( r, d->rowPosCache + 10 );
	}
    }
    int i = ( (l+r+1) / 2 );
    while ( r - l ) {
	if ( d->rowPos[i] > y )
	    r = i -1;
	else
	    l = i;
	i = ( (l+r+1) / 2 );
    }
    d->rowPosCache = i;
    if ( d->rowPos[i] <= y && y <= d->rowPos[i+1]  )
	return  i;

    return d->count - 1;
}


/*!
    Returns the rectangle on the screen that \a item occupies in
    viewport()'s coordinates, or an invalid rectangle if \a item is 0
    or is not currently visible.
*/

TQRect TQListBox::tqitemRect( TQListBoxItem *item ) const
{
    if ( d->resizeTimer->isActive() )
	return TQRect( 0, 0, -1, -1 );
    if ( !item )
	return TQRect( 0, 0, -1, -1 );

    int i = index( item );
    int col = i / numRows();
    int row = i % numRows();

    int x = d->columnPos[ col ] - contentsX();
    int y = d->rowPos[ row ] - contentsY();

    TQRect r( x, y, d->columnPos[ col + 1 ] - d->columnPos[ col ],
		  d->rowPos[ row + 1 ] - d->rowPos[ row ] );
    if ( r.intersects( TQRect( 0, 0, visibleWidth(), visibleHeight() ) ) )
	return r;
    return TQRect( 0, 0, -1, -1 );
}


#ifndef TQT_NO_COMPAT

/*!
  \obsolete

  Using this method is quite inefficient. We suggest to use insertItem()
  for inserting and sort() afterwards.

  Inserts \a lbi at its sorted position in the list box and returns the
  position.

  All items must be inserted with inSort() to maintain the sorting
  order. inSort() treats any pixmap (or user-defined type) as
  lexicographically less than any string.

  \sa insertItem(), sort()
*/
int TQListBox::inSort( const TQListBoxItem * lbi )
{
    qObsolete( "TQListBox", "inSort", "insertItem" );
    if ( !lbi )
	return -1;

    TQListBoxItem * i = d->head;
    int c = 0;

    while( i && i->text() < lbi->text() ) {
	i = i->n;
	c++;
    }
    insertItem( lbi, c );
    return c;
}

/*!
  \obsolete
  \overload
  Using this method is quite inefficient. We suggest to use insertItem()
  for inserting and sort() afterwards.

  Inserts a new item of \a text at its sorted position in the list box and
  returns the position.

  All items must be inserted with inSort() to maintain the sorting
  order. inSort() treats any pixmap (or user-defined type) as
  lexicographically less than any string.

  \sa insertItem(), sort()
*/
int TQListBox::inSort( const TQString& text )
{
    qObsolete( "TQListBox", "inSort", "insertItem" );
    return inSort( new TQListBoxText(text) );
}

#endif


/*! \reimp */

void TQListBox::resizeEvent( TQResizeEvent *e )
{
    d->layoutDirty = ( d->layoutDirty ||
		       rowMode() == FitToHeight ||
		       columnMode() == FitToWidth );

    if ( !d->layoutDirty && columnMode() == FixedNumber &&
	 d->numColumns == 1) {
	int w = d->columnPosOne;
	TQSize s( viewportSize( w, contentsHeight() ) );
	w = TQMAX( w, s.width() );
	d->columnPos[1] = TQMAX( w, d->columnPosOne );
	resizeContents( d->columnPos[1], contentsHeight() );
    }

    if ( d->resizeTimer->isActive() )
	d->resizeTimer->stop();
    if ( d->rowMode == FixedNumber && d->columnMode == FixedNumber ) {
	bool currentItemVisible = itemVisible( currentItem() );
	doLayout();
	TQScrollView::resizeEvent( e );
	if ( currentItemVisible )
	    ensureCurrentVisible();
	if ( d->current )
	    viewport()->tqrepaint( tqitemRect( d->current ), FALSE );
    } else if ( ( d->columnMode == FitToWidth || d->rowMode == FitToHeight ) && !(isVisible()) ) {
	TQScrollView::resizeEvent( e );
    } else if ( d->layoutDirty ) {
	d->resizeTimer->start( 100, TRUE );
	resizeContents( contentsWidth() - ( e->oldSize().width() - e->size().width() ),
			contentsHeight() - ( e->oldSize().height() - e->size().height() ) );
	TQScrollView::resizeEvent( e );
    } else {
	TQScrollView::resizeEvent( e );
    }
}

/*!
  \internal
*/

void TQListBox::adjustItems()
{
    triggerUpdate( TRUE );
    ensureCurrentVisible();
}


/*!
    Provided for compatibility with the old TQListBox. We recommend
    using TQListBoxItem::paint() instead.

    Repaints the cell at \a row, \a col using painter \a p.
*/

void TQListBox::paintCell( TQPainter * p, int row, int col )
{
    bool drawActiveSelection = hasFocus() || d->inMenuMode ||
	!tqstyle().tqstyleHint( TQStyle::SH_ItemView_ChangeHighlightOnFocus, this );
    const TQColorGroup &g = ( drawActiveSelection ? tqcolorGroup() : tqpalette().inactive() );

    int cw = d->columnPos[col+1] - d->columnPos[col];
    int ch = d->rowPos[row+1] - d->rowPos[row];
    TQListBoxItem * i = item( col*numRows()+row );
    p->save();
    if ( i->s ) {
	if ( i->custom_highlight ) {
	    p->fillRect( 0, 0, cw, ch,
			 g.brush( TQPalette::backgroundRoleFromMode( viewport()->backgroundMode() ) ) );
	    p->setPen( g.highlightedText() );
	    p->setBackgroundColor( g.highlight() );
	}
	else if ( numColumns()  == 1 ) {
	    p->fillRect( 0, 0, cw, ch, g.brush( TQColorGroup::Highlight ) );
	    p->setPen( g.highlightedText() );
	    p->setBackgroundColor( g.highlight() );
	} else {
	    int iw = i->width( this );
	    p->fillRect( 0, 0, iw, ch, g.brush( TQColorGroup::Highlight ) );
	    p->fillRect( iw, 0, cw - iw + 1, ch,
			 g.brush( TQPalette::backgroundRoleFromMode( viewport()->backgroundMode() ) ) );
	    p->setPen( g.highlightedText() );
	    p->setBackgroundColor( g.highlight() );
	}
    } else {
	p->fillRect( 0, 0, cw, ch,
		     g.brush( TQPalette::backgroundRoleFromMode( viewport()->backgroundMode() ) ) );
    }

    i->paint( p );

    if ( d->current == i && hasFocus() && !i->custom_highlight ) {
	if ( numColumns() > 1 )
	    cw = i->width( this );

	tqstyle().tqdrawPrimitive( TQStyle::PE_FocusRect, p, TQRect( 0, 0, cw, ch ), g,
			       TQStyle::Style_FocusAtBorder,
			       TQStyleOption(i->isSelected() ? g.highlight() : g.base() ) );
    }

    p->restore();
}

/*!
    Returns the width of the widest item in the list box.
*/

long TQListBox::maxItemWidth() const
{
    if ( d->layoutDirty )
	doLayout();
    long m = 0;
    int i = d->columnPos.size();
    while( i-- )
	if ( m < d->columnPos[i] )
	    m = d->columnPos[i];
    return m;
}


/*! \reimp */

void TQListBox::showEvent( TQShowEvent * )
{
    d->ignoreMoves = FALSE;
    d->mousePressRow = -1;
    d->mousePressColumn = -1;
    d->mustPaintAll = FALSE;
    ensureCurrentVisible();
}

#ifndef TQT_NO_COMPAT

/*!
  \obsolete

  Returns the vertical pixel-coordinate in \a *yPos, of the list box
  item at position \a index in the list. Returns FALSE if the item is
  outside the visible area.
*/
bool TQListBox::itemYPos( int index, int *yPos ) const
{
    qObsolete( "TQListBox", "itemYPos" );
    TQListBoxItem* i = item(index);
    if ( !i )
	return FALSE;
    if ( yPos )
	*yPos = i->y;
    return TRUE;
}

#endif

/*!
    \fn bool TQListBoxItem::isSelected() const

    Returns TRUE if the item is selected; otherwise returns FALSE.

    \sa TQListBox::isSelected(), isCurrent()
*/

/*!
  \fn bool TQListBoxItem::selected() const
  \obsolete
*/

/*!
    Returns TRUE if the item is the current item; otherwise returns
    FALSE.

    \sa TQListBox::currentItem(), TQListBox::item(), isSelected()
*/
bool TQListBoxItem::isCurrent() const
{
    return listBox() && listBox()->hasFocus() &&
	listBox()->item( listBox()->currentItem() ) == this;
}
/*!
  \fn bool TQListBoxItem::current() const
  \obsolete
*/

/*!
    \fn void TQListBox::centerCurrentItem()
    \obsolete

    This function does exactly the same as ensureCurrentVisible()

    \sa TQListBox::ensureCurrentVisible()
*/

/*!
    Returns a pointer to the list box containing this item.
*/

TQListBox * TQListBoxItem::listBox() const
{
    return lbox;
}


/*!
    Removes \a item from the list box and causes an update of the
    screen display. The item is not deleted. You should normally not
    need to call this function because TQListBoxItem::~TQListBoxItem()
    calls it. The normal way to delete an item is with \c delete.

    \sa TQListBox::insertItem()
*/
void TQListBox::takeItem( const TQListBoxItem * item )
{
    if ( !item || d->clearing )
	return;
    d->cache = 0;
    d->count--;
    if ( item == d->last )
	d->last = d->last->p;
    if ( item->p && item->p->n == item )
	item->p->n = item->n;
    if ( item->n && item->n->p == item )
	item->n->p = item->p;
    if ( d->head == item ) {
	d->head = item->n;
	d->currentColumn = d->currentRow = -1;
    }

    if ( d->current == item ) {
	d->current = item->n ? item->n : item->p;
	TQListBoxItem *i = d->current;
	TQString tmp;
	if ( i )
	    tmp = i->text();
	int tmp2 = index( i );
	emit highlighted( i );
	if ( !tmp.isNull() )
	    emit highlighted( tmp );
	emit highlighted( tmp2 );
	emit currentChanged( i );
    }
    if ( d->tmpCurrent == item )
        d->tmpCurrent = d->current;
    if ( d->selectAnchor == item )
	d->selectAnchor = d->current;

    if ( item->s )
	emit selectionChanged();
    ((TQListBoxItem *)item)->lbox = 0;
    triggerUpdate( TRUE );
}

/*!
  \internal
  Finds the next item after start beginning with \a text.
*/

int TQListBoxPrivate::tqfindItemByName( int start, const TQString &text )
{
    if ( start < 0 || (uint)start >= listBox->count() )
	start = 0;
    TQString match = text.lower();
    if ( match.length() < 1 )
	return start;
    TQString curText;
    int item = start;
    do {
	curText = listBox->text( item ).lower();
	if ( curText.startsWith( match ) )
	    return item;
	item++;
	if ( (uint)item == listBox->count() )
	    item = 0;
    } while ( item != start );
    return -1;
}

/*!
  \internal --- obsolete!
*/

void TQListBox::clearInputString()
{
    d->currInputString = TQString::null;
}

/*!
    Finds the first list box item that has the text \a text and
    returns it, or returns 0 of no such item could be found.
    The search starts from the current item if the current item exists,
    otherwise it starts from the first list box item.
    If \c ComparisonFlags are specified in \a compare then these flags
    are used, otherwise the default is a case-insensitive, "begins
    with" search.

    \sa TQt::StringComparisonMode
*/

TQListBoxItem *TQListBox::tqfindItem( const TQString &text, TQt::ComparisonFlags compare ) const
{
    if ( text.isEmpty() )
	return 0;

    if ( compare == TQt::CaseSensitive || compare == 0 )
	compare |= TQt::ExactMatch;

    TQString itmtxt;
    TQString comtxt = text;
    if ( ! (compare & TQt::CaseSensitive ) )
	comtxt = text.lower();

    TQListBoxItem *item;
    if ( d->current )
	item = d->current;
    else
	item = d->head;

    TQListBoxItem *beginsWithItem = 0;
    TQListBoxItem *endsWithItem = 0;
    TQListBoxItem *tqcontainsItem = 0;

    if ( item ) {
	for ( ; item; item = item->n ) {
	    if ( ! (compare & TQt::CaseSensitive) )
		itmtxt = item->text().lower();
	    else
		itmtxt = item->text();

	    if ( compare & TQt::ExactMatch && itmtxt == comtxt )
		return item;
	    if ( compare & TQt::BeginsWith && !beginsWithItem && itmtxt.startsWith( comtxt ) )
		beginsWithItem = tqcontainsItem = item;
	    if ( compare & TQt::EndsWith && !endsWithItem && itmtxt.endsWith( comtxt ) )
		endsWithItem = tqcontainsItem = item;
	    if ( compare & TQt::Contains && !tqcontainsItem && itmtxt.tqcontains( comtxt ) )
		tqcontainsItem = item;
	}

	if ( d->current && d->head ) {
	    item = d->head;
	    for ( ; item && item != d->current; item = item->n ) {
		if ( ! (compare & TQt::CaseSensitive) )
		    itmtxt = item->text().lower();
		else
		    itmtxt = item->text();

		if ( compare & TQt::ExactMatch && itmtxt == comtxt )
		    return item;
		if ( compare & TQt::BeginsWith && !beginsWithItem && itmtxt.startsWith( comtxt ) )
		    beginsWithItem = tqcontainsItem = item;
		if ( compare & TQt::EndsWith && !endsWithItem && itmtxt.endsWith( comtxt ) )
		    endsWithItem = tqcontainsItem = item;
		if ( compare & TQt::Contains && !tqcontainsItem && itmtxt.tqcontains( comtxt ) )
		    tqcontainsItem = item;
	    }
	}
    }

    // Obey the priorities
    if ( beginsWithItem )
	return beginsWithItem;
    else if ( endsWithItem )
	return endsWithItem;
    else if ( tqcontainsItem )
	return tqcontainsItem;
    return 0;
}

/*!
  \internal
*/

void TQListBox::drawRubber()
{
    if ( !d->rubber )
	return;
    if ( !d->rubber->width() && !d->rubber->height() )
	return;
    TQPainter p( viewport() );
    p.setRasterOp( TQt::NotROP );
    tqstyle().tqdrawPrimitive( TQStyle::PE_RubberBand, &p, d->rubber->normalize(),
			   tqcolorGroup() );
    p.end();
}

/*!
  \internal
*/

void TQListBox::doRubberSelection( const TQRect &old, const TQRect &rubber )
{
    TQListBoxItem *i = d->head;
    TQRect ir, pr;
    bool changed = FALSE;
    for ( ; i; i = i->n ) {
	ir = tqitemRect( i );
	if ( ir == TQRect( 0, 0, -1, -1 ) )
	    continue;
	if ( i->isSelected() && !ir.intersects( rubber ) && ir.intersects( old ) ) {
	    i->s = FALSE;
	    pr = pr.unite( ir );
	    changed = TRUE;
	} else if ( !i->isSelected() && ir.intersects( rubber ) ) {
	    if ( i->isSelectable() ) {
		i->s = TRUE;
		pr = pr.unite( ir );
		changed = TRUE;
	    }
	}
    }
    if ( changed ) {
	emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
#endif
    }
    viewport()->tqrepaint( pr, TRUE );
}


/*!
    Returns TRUE if the user is selecting items using a rubber band
    rectangle; otherwise returns FALSE.
*/

bool TQListBox::isRubberSelecting() const
{
    return d->rubber != 0;
}


/*!
    Returns the item that comes after this in the list box. If this is
    the last item, 0 is returned.

    \sa prev()
*/

TQListBoxItem *TQListBoxItem::next() const
{
    return n;
}

/*!
    Returns the item which comes before this in the list box. If this
    is the first item, 0 is returned.

    \sa next()
*/

TQListBoxItem *TQListBoxItem::prev() const
{
    return p;
}

/*!
    Returns the first item in this list box. If the list box is empty,
    returns 0.
*/

TQListBoxItem *TQListBox::firstItem() const
{
    return d->head;
}

#if defined(TQ_C_CALLBACKS)
extern "C" {
#endif

#ifdef TQ_OS_TEMP
static int _cdecl cmpListBoxItems( const void *n1, const void *n2 )
#else
static int cmpListBoxItems( const void *n1, const void *n2 )
#endif
{
    if ( !n1 || !n2 )
	return 0;

    TQListBoxPrivate::SortableItem *i1 = (TQListBoxPrivate::SortableItem *)n1;
    TQListBoxPrivate::SortableItem *i2 = (TQListBoxPrivate::SortableItem *)n2;

    return i1->item->text().localeAwareCompare( i2->item->text() );
}

#if defined(TQ_C_CALLBACKS)
}
#endif

/*!
    If \a ascending is TRUE sorts the items in ascending order;
    otherwise sorts in descending order.

    To compare the items, the text (TQListBoxItem::text()) of the items
    is used.
*/

void TQListBox::sort( bool ascending )
{
    if ( count() == 0 )
	return;

    d->cache = 0;

    TQListBoxPrivate::SortableItem *items = new TQListBoxPrivate::SortableItem[ count() ];

    TQListBoxItem *item = d->head;
    int i = 0;
    for ( ; item; item = item->n )
	items[ i++ ].item = item;

    qsort( items, count(), sizeof( TQListBoxPrivate::SortableItem ), cmpListBoxItems );

    TQListBoxItem *prev = 0;
    item = 0;
    if ( ascending ) {
	for ( i = 0; i < (int)count(); ++i ) {
	    item = items[ i ].item;
	    if ( item ) {
		item->p = prev;
		item->dirty = TRUE;
		if ( item->p )
		    item->p->n = item;
		item->n = 0;
	    }
	    if ( i == 0 )
		d->head = item;
	    prev = item;
	}
    } else {
	for ( i = (int)count() - 1; i >= 0 ; --i ) {
	    item = items[ i ].item;
	    if ( item ) {
		item->p = prev;
		item->dirty = TRUE;
		if ( item->p )
		    item->p->n = item;
		item->n = 0;
	    }
	    if ( i == (int)count() - 1 )
		d->head = item;
	    prev = item;
	}
    }
    d->last = item;

    delete [] items;

    // We have to update explicitly in case the current "vieport" overlaps the
    // new viewport we set (starting at (0,0)).
    bool haveToUpdate = contentsX() < visibleWidth() || contentsY() < visibleHeight();
    setContentsPos( 0, 0 );
    if ( haveToUpdate )
	updateContents( 0, 0, visibleWidth(), visibleHeight() );
}

void TQListBox::handleItemChange( TQListBoxItem *old, bool shift, bool control )
{
    if ( d->selectionMode == Single ) {
	// nothing
    } else if ( d->selectionMode == Extended ) {
	if ( shift ) {
	    selectRange( d->selectAnchor ? d->selectAnchor : old,
			 d->current, FALSE, TRUE, (d->selectAnchor && !control) ? TRUE : FALSE );
	} else if ( !control ) {
	    bool block = tqsignalsBlocked();
	    blockSignals( TRUE );
	    selectAll( FALSE );
	    blockSignals( block );
	    setSelected( d->current, TRUE );
	}
    } else if ( d->selectionMode == Multi ) {
	if ( shift )
	    selectRange( old, d->current, TRUE, FALSE );
    }
}

void TQListBox::selectRange( TQListBoxItem *from, TQListBoxItem *to, bool invert, bool includeFirst, bool clearSel )
{
    if ( !from || !to )
	return;
    if ( from == to && !includeFirst )
	return;
    TQListBoxItem *i = 0;
    int index =0;
    int f_idx = -1, t_idx = -1;
    for ( i = d->head; i; i = i->n, index++ ) {
	if ( i == from )
	    f_idx = index;
	if ( i == to )
	    t_idx = index;
	if ( f_idx != -1 && t_idx != -1 )
	    break;
    }
    if ( f_idx > t_idx ) {
	i = from;
	from = to;
	to = i;
	if ( !includeFirst )
	    to = to->prev();
    } else {
	if ( !includeFirst )
	    from = from->next();
    }

    bool changed = FALSE;
    if ( clearSel ) {
	for ( i = d->head; i && i != from; i = i->n ) {
	    if ( i->s ) {
		i->s = FALSE;
		changed = TRUE;
		updateItem( i );
	    }
	}
	for ( i = to->n; i; i = i->n ) {
	    if ( i->s ) {
		i->s = FALSE;
		changed = TRUE;
		updateItem( i );
	    }
	}
    }

    for ( i = from; i; i = i->next() ) {
	if ( !invert ) {
	    if ( !i->s && i->isSelectable() ) {
		i->s = TRUE;
		changed = TRUE;
		updateItem( i );
	    }
	} else {
	    bool sel = !i->s;
	    if ( ((bool)i->s != sel && sel && i->isSelectable()) || !sel ) {
		i->s = sel;
		changed = TRUE;
		updateItem( i );
	    }
	}
	if ( i == to )
	    break;
    }
    if ( changed ) {
	emit selectionChanged();
#if defined(TQT_ACCESSIBILITY_SUPPORT)
	TQAccessible::updateAccessibility( viewport(), 0, TQAccessible::Selection );
#endif
    }
}

/*! \reimp */
void TQListBox::windowActivationChange( bool oldActive )
{
    if ( oldActive && d->scrollTimer )
	d->scrollTimer->stop();
    if ( tqpalette().active() != tqpalette().inactive() )
	viewport()->update();
    TQScrollView::windowActivationChange( oldActive );
}

int TQListBoxItem::RTTI = 0;

/*!
    Returns 0.

    Make your derived classes return their own values for rtti(), and
    you can distinguish between listbox items. You should use values
    greater than 1000 preferably a large random number, to allow for
    extensions to this class.
*/

int TQListBoxItem::rtti() const
{
    return RTTI;
}

#endif // TQT_NO_LISTBOX