summaryrefslogtreecommitdiffstats
path: root/tqtinterface/qt4/src/attic/qtmultilineedit.cpp
blob: 104ff600f51d4062b6b785464c24a25b3a52d37e (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
/**********************************************************************
**
** Implementation of TQtMultiLineEdit widget class
**
** Created : 961005
**
** Copyright (C) 2010 Timothy Pearson and (C) 1992-2008 Trolltech ASA.
**
** This file contains a class moved out of the TQt GUI Toolkit API. It
** may be used, distributed and modified without limitation.
**
**********************************************************************/

#include "tqtmultilineedit.h"
#ifndef TQT_NO_TQTMULTILINEEDIT
#include "tqpainter.h"
#include "tqscrollbar.h"
#include "tqclipboard.h"
#include "tqpixmap.h"
#include "tqregexp.h"
#include "tqapplication.h"
#include "tqdragobject.h"
#include "tqpopupmenu.h"
#include "tqtimer.h"
#include "tqdict.h"
#include "tqcursor.h"
#ifndef TQT_NO_COMPAT
#include "tqstyle.h"
#endif


class TQtMultiLineEditCommand
{
public:
    enum Commands { Invalid, Begin, End, Insert, Delete };
    virtual ~TQtMultiLineEditCommand() {};
    virtual Commands type() { return Invalid; };
    virtual int terminator() { return 0; }

    virtual bool merge( TQtMultiLineEditCommand* ) { return FALSE;}
};

class TQBeginCommand : public TQtMultiLineEditCommand
{

public:
    TQBeginCommand() {}
    int terminator() { return 1; }
    Commands type() { return Begin; };
};

class TQEndCommand : public TQtMultiLineEditCommand
{
public:
    TQEndCommand() {}
    int terminator() { return -1; }
    Commands type() { return End; };
};

// TQtMultiLineEditUndoRedo methods
class TQDelTextCmd : public TQtMultiLineEditCommand
{
public:
    int mOffset;
    TQString mStr;

    // have to handle deletion of current selection
    TQDelTextCmd(int offset, const TQString &str )
	: mOffset( offset ),
	  mStr ( str )
    {
    }
    Commands type() { return Delete; };

    bool merge( TQtMultiLineEditCommand* other)
    {
	if ( other->type() == type() ) {
	    TQDelTextCmd* o = (TQDelTextCmd*) other;
	    if ( mOffset + int(mStr.length()) == o->mOffset ) {
		o->mStr.prepend( mStr );
		o->mOffset = mOffset;
		return TRUE;
	    }
	}
	return FALSE;
    }


};

class TQInsTextCmd : public TQDelTextCmd
{

public:
    TQInsTextCmd(int offset,const TQString &str )
	: TQDelTextCmd( offset, str )
    {
    }

    Commands type() { return Insert; };

    bool merge( TQtMultiLineEditCommand* other)
    {
	if ( other->type() == type() ) {
	    TQInsTextCmd* o = (TQInsTextCmd*) other;
	    if ( mOffset == o->mOffset + int(o->mStr.length()) ) {
		o->mStr += mStr;
		return TRUE;
	    }
	}
	return FALSE;
    }
};


/*
  \class TQtMultiLineEdit qtmultilineedit.h

  \brief The TQtMultiLineEdit widget is a simple editor for inputting text.

  \obsolete

  The TQtMultiLineEdit widget provides multiple line text input and display.
  It is intended for moderate amounts of text. There are no arbitrary
  limitations, but if you try to handle megabytes of data, performance
  will suffer.

  Per default, the edit widget does not perform any word
  wrapping. This can be adjusted by calling setWordWrap(). Both
  dynamic wrapping according to the visible width or a fixed number of
  character or pixels is supported.

  The widget can be used to display text by calling setReadOnly(TRUE).

  The default key bindings are described in keyPressEvent(); they cannot
  be customized except by inheriting the class.

  <img src=qmlined-m.png> <img src=qmlined-w.png>
*/

/*
    \property TQtMultiLineEdit::numLines
    \brief the number of lines in the multi-line edit

  numLines() returns the number of lines in the editor. The count
  includes any empty lines at top and bottom, so for an empty editor
  this method will return 1.
*/
/*
    \property TQtMultiLineEdit::atBeginning
    \brief whether the cursor is at the beginning

    atBeginning() returns TRUE if the cursor is placed at the
    beginning of the text.
*/
/*
    \property TQtMultiLineEdit::atEnd
    \brief whether the cursor is at the end

  atEnd() returns TRUE if the cursor is placed at the end of the text.
*/
/*
    \property TQtMultiLineEdit::maxLineWidth
    \brief the maximum line width in pixels
  Returns the width in pixels of the longest text line in this editor.
*/
/*
    \property TQtMultiLineEdit::tqalignment
    \brief the tqalignment

    Possible values are \c AlignLeft, \c Align(H)Center and \c
    AlignRight.
  \sa TQt::AlignmentFlags
*/
/*
    \property TQtMultiLineEdit::edited
    \brief whether the text had been edited

edited() returns the edited flag of the line edit.  If this returns FALSE,
the contents has not been changed since the construction of the
TQtMultiLineEdit (or the last call to setEdited( FALSE ), if any).  If
it returns TRUE, the contents have been edited, or setEdited( TRUE )
has been called.

setEdited() sets the edited flag of this line edit to \e e.  The
edited flag is never read by TQtMultiLineEdit, but is changed to TRUE
whenever the user changes its contents.

This is useful e.g. for things that need to provide a default value,
but cannot find the default at once.  Just open the widget without the
best default and when the default is known, check the edited() return
value and set the line edit's contents if the user has not started
editing the line edit.  Another example is to detect whether the
contents need saving.

*/
/*
    \property TQtMultiLineEdit::echoMode
    \brief the echo mode
*/
/*
    \property TQtMultiLineEdit::maxLength
    \brief the maximum length of the text

  The currently set text length limit, or -1 if there is
  no limit (this is the default).

*/
/*
    \property TQtMultiLineEdit::maxLines
    \brief the maximum number of lines
  The currently set line limit, or -1 if there is
  no limit (the default).

  Note that excess lines are deleted from the \e bottom of the
  lines. If you want teletype behaviour with lines disappearing
  from the \e top as the limit is exceed, you probably just want
  to use removeLine(0) prior to adding an excess line.

*/
/*
    \property TQtMultiLineEdit::hMargin
    \brief the horizontal margin
  The horizontal margin current set.  The default is 3.
*/
/*
    \property TQtMultiLineEdit::wordWrap
    \brief the word wrap mode

  By default, wrapping keeps words intact. To allow breaking within
  words, set the wrap policy to \c Anywhere (see setWrapPolicy() ).

  The default wrap mode is \c NoWrap.

  \sa wordWrap(), setWrapColumnOrWidth(), setWrapPolicy()
*/
/*
    \property TQtMultiLineEdit::wrapColumnOrWidth
    \brief the wrap width in columns or pixels
  The wrap column or wrap width, depending on the word wrap
  mode.
  \sa setWordWrap(), setWrapColumnOrWidth()
*/
/*
    \property TQtMultiLineEdit::wrapPolicy
    \brief the wrap policy mode
   The default is \c AtWhiteSpace.

*/
/*
    \property TQtMultiLineEdit::autoUpdate
    \brief whether auto update is enabled

  autoUpdate() returns TRUE if the view updates itself automatically
  whenever it is changed in some way.

  If autoUpdate() is TRUE (this is the default) then the editor updates
  itself automatically whenever it has changed in some way (generally,
  when text has been inserted or deleted).

  If autoUpdate() is FALSE, the view does NOT tqrepaint itself, or update
  its internal state variables itself when it is changed.  This can be
  useful to avoid flicker during large changes, and is singularly
  useless otherwise: Disable auto-update, do the changes, re-enable
  auto-update, and call tqrepaint().

  \warning Do not leave the view in this state for a long time
  (i.e. between events ). If, for example, the user interacts with the
  view when auto-update is off, strange things can happen.

  Setting auto-update to TRUE does not tqrepaint the view, you must call
  tqrepaint() to do this (preferable tqrepaint(FALSE) to avoid flicker).

  \sa autoUpdate() tqrepaint()

*/
/*
    \property TQtMultiLineEdit::undoEnabled
    \brief whether undo is enabled
*/
/*
    \property TQtMultiLineEdit::undoDepth
    \brief the undo depth

  The maximum number of operations that can be stored on the undo stack.

  \sa setUndoDepth()
*/
/*
    \property TQtMultiLineEdit::readOnly
    \brief whether the multi-line edit is read-only
*/
/*
    \property TQtMultiLineEdit::overWriteMode
    \brief the overwrite mode
*/
/*
    \property TQtMultiLineEdit::text
    \brief the multi-line edit's text
*/
/*
    \property TQtMultiLineEdit::length
    \brief the length of the text
*/

static const char * const arrow_xpm[] = {
    "     8     8        2            1",
    ". c None",
    "# c #000000",
    "........",
    "..####..",
    "..#####.",
    ".....##.",
    ".#..###.",
    ".#####..",
    ".####...",
    ".#####.."
};

enum {
    IdUndo,
    IdRedo,
#ifndef TQT_NO_CLIPBOARD
    IdCut,
    IdCopy,
    IdPaste,
    IdPasteSpecial,
#endif
    IdClear,
    IdSelectAll,
    IdCount
};

struct TQtMultiLineData
{
    TQtMultiLineData() :
	isHandlingEvent(FALSE),
	edited(FALSE),
	maxLineWidth(0),
	align(TQt::AlignLeft),
	maxlines(-1),
	maxlinelen(-1),
	maxlen(-1),
	wordwrap( TQtMultiLineEdit::NoWrap ),
	wrapcol( -1 ),
	wrappolicy( TQtMultiLineEdit::AtWhiteSpace ),
	// This doesn't use font bearings, as textWidthWithTabs does that.
	// This is just an aesthetics value.
	// It should probably be TQMAX(0,3-fontMetrics().minLeftBearing()) though,
	// as bearings give some border anyway.
	lr_marg(3),
	marg_extra(0),
	echomode(TQtMultiLineEdit::Normal),
	val(0),
	dnd_primed(FALSE),
	dnd_forcecursor(FALSE),
	undo( TRUE ),
	undodepth( 256 )
    {
	undoList.setAutoDelete( TRUE );
	redoList.setAutoDelete( TRUE );
	clearChartable();
    }
    bool isHandlingEvent;
    bool edited;
    int  maxLineWidth;
    int	 scrollTime;
    int	 scrollAccel;
    int  align;
    int  numlines;
    int  maxlines;
    int  maxlinelen;
    int  maxlen;
    TQtMultiLineEdit::WordWrap wordwrap;
    int wrapcol;
    TQtMultiLineEdit::WrapPolicy wrappolicy;
    int lr_marg;
    int marg_extra;
    TQtMultiLineEdit::EchoMode echomode;
    const TQValidator* val;

    bool dnd_primed; // If TRUE, user has pressed
    bool dnd_forcecursor; // If TRUE show cursor for DND feedback,
    // even if !hasFocus()
    TQPtrList<TQtMultiLineEditCommand> undoList;
    TQPtrList<TQtMultiLineEditCommand> redoList;
    bool undo;
    int undodepth;
    short chartable[256];
    void clearChartable()
    {
	int i = 256;
	while ( i )
	    chartable[--i] = 0;
    }
    TQPixmap arrow;
    TQPoint dnd_startpos;
    TQTimer *blinkTimer, *scrollTimer;
#ifndef TQT_NO_DRAGANDDROP
    TQTimer *dnd_timer;
#endif
};


#define CLEAR_UNDO {d->undoList.clear(); emit undoAvailable( FALSE );\
    d->redoList.clear(); emit redoAvailable( FALSE );}

void TQtMultiLineEdit::addUndoCmd(TQtMultiLineEditCommand* c)
{
    if ( d->undoList.isEmpty() )
	emit undoAvailable(TRUE);
    else if ( c->merge( d->undoList.last() ) ) {
	delete c;
	return;
    }
    if ( int(d->undoList.count()) >= d->undodepth )
	d->undoList.removeFirst();
    d->undoList.append(c);

    if ( !d->redoList.isEmpty() ) {
	d->redoList.clear();
	emit redoAvailable( FALSE );
    }
}

void TQtMultiLineEdit::addRedoCmd(TQtMultiLineEditCommand* c)
{
    if ( d->redoList.isEmpty() )
	emit redoAvailable(TRUE);
    d->redoList.append(c);
}

static const int initialScrollTime = 50; // mark text scroll time
static const int initialScrollAccel = 5; // mark text scroll accel (0=fastest)
static const int scroll_margin = 16;     // auto-scroll edge in DND

#define WORD_WRAP ( d->wordwrap != TQtMultiLineEdit::NoWrap )
#define DYNAMIC_WRAP ( d->wordwrap == TQtMultiLineEdit::WidgetWidth )
#define FIXED_WIDTH_WRAP ( d->wordwrap == TQtMultiLineEdit::FixedPixelWidth )
#define FIXED_COLUMN_WRAP ( d->wordwrap == TQtMultiLineEdit::FixedColumnWidth )
#define BREAK_WITHIN_WORDS ( d->wrappolicy == TQtMultiLineEdit::Anywhere )

static int defTabStop = 8;

static int tabStopDist( const TQFontMetrics &fm )
{
    int dist;
    dist = fm.width( TQChar('x' ));
    if( dist == 0 )
       dist = fm.maxWidth();
    return defTabStop*dist;
}


/*
  Sets the distance between tab stops for all TQtMultiLineEdit instances
  to \a ex, which is measured in multiples of the width of a lower case 'x'
  in the widget's font. The initial value is 8.

  \warning This function does not cause a redraw. It is best to call
  it before any TQtMultiLineEdit widgets are shown.

  \sa defaultTabStop()
*/

void TQtMultiLineEdit::setDefaultTabStop( int ex )
{
    defTabStop = ex;
}



/*
  Returns the distance between tab stops.

  \sa setDefaultTabStop();
*/

int TQtMultiLineEdit::defaultTabStop()
{
    return defTabStop;
}




static int textWidthWithTabs( const TQFontMetrics &fm, const TQString &s, uint start, uint nChars, int align )
{
    if ( s.isEmpty() )
	return 0;

    int dist = -fm.leftBearing( s[(int)start] );
    int i = start;
    int tabDist = -1; // lazy eval
    while ( (uint)i < s.length() && (uint)i < start+nChars ) {
	if ( s[i] == '\t' && align == TQt::AlignLeft ) {
	    if ( tabDist<0 )
		tabDist = tabStopDist(fm);
	    dist = ( (dist+tabDist+1)/tabDist ) * tabDist;
	    i++;
	} else {
	    int ii = i;
	    while ( (uint)i < s.length() && (uint)i < start + nChars && ( align != TQt::AlignLeft || s[i] != '\t' ) )
		i++;
	    dist += fm.width( s.mid(ii,i-ii) );
	}
    }
    return dist;
}

static int xPosToCursorPos( const TQString &s, const TQFontMetrics &fm,
			    int xPos, int width, int align )
{
    int i = 0;
    int	  dist;
    int tabDist;

    if ( s.isEmpty() )
	return 0;
    if ( xPos > width )
	xPos = width;
    if ( xPos <= 0 )
	return 0;

    dist    = -fm.leftBearing( s[0] );

    if ( align == TQt::AlignCenter || align == TQt::AlignHCenter )
	dist = ( width - textWidthWithTabs( fm, s, 0, s.length(), align ) ) / 2;
    else if ( align == TQt::AlignRight )
	dist = width - textWidthWithTabs( fm, s, 0, s.length(), align );

    int     distBeforeLastTab = dist;
    tabDist = tabStopDist(fm);
    while ( (uint)i < s.length() && dist < xPos ) {
	if ( s[i] == '\t' && align == TQt::AlignLeft ) {
	    distBeforeLastTab = dist;
	    dist = (dist/tabDist + 1) * tabDist;
	} else {
	    dist += fm.width( s[i] );
	}
	i++;
    }
    if ( dist > xPos ) {
	if ( dist > width ) {
	    i--;
	} else {
	    if ( s[i-1] == '\t' && align == TQt::AlignLeft ) { // dist equals a tab stop position
		if ( xPos - distBeforeLastTab < (dist - distBeforeLastTab)/2 )
		    i--;
	    } else {
		if ( fm.width(s[i-1])/2 < dist-xPos )
		    i--;
	    }
	}
    }
    return i;
}

/*
  Constructs a new, empty, TQtMultiLineEdit with parent \a parent and
  called \a name.
*/

TQtMultiLineEdit::TQtMultiLineEdit( TQWidget *parent , const char *name )
    :TQtTableView( parent, name, WStaticContents | WRepaintNoErase )
{
    d = new TQtMultiLineData;
    TQFontMetrics fm( font() );
    setCellHeight( fm.lineSpacing() );
    setNumCols( 1 );

    contents = new TQPtrList<TQtMultiLineEditRow>;
    contents->setAutoDelete( TRUE );

    cursorX = 0; cursorY = 0;
    curXPos = 0;

    setTableFlags( Tbl_autoVScrollBar|Tbl_autoHScrollBar|
		   Tbl_smoothVScrolling |
		   Tbl_clipCellPainting
		   );
    setFrameStyle( TQFrame::WinPanel | TQFrame::Sunken );
    setBackgroundMode( PaletteBase );
    setWFlags( WResizeNoErase );
    setKeyCompression( TRUE );
    setFocusPolicy( WheelFocus );
#ifndef TQT_NO_CURSOR
    setCursor( ibeamCursor );
    verticalScrollBar()->setCursor( arrowCursor );
    horizontalScrollBar()->setCursor( arrowCursor );
#endif
    readOnly 	   = FALSE;
    cursorOn	   = FALSE;
    markIsOn	   = FALSE;
    dragScrolling  = FALSE;
    dragMarking    = FALSE;
    textDirty	   = FALSE;
    wordMark	   = FALSE;
    overWrite	   = FALSE;
    markAnchorX    = 0;
    markAnchorY    = 0;
    markDragX      = 0;
    markDragY      = 0;
    d->blinkTimer = new TQTimer( this );
    connect( d->blinkTimer, TQT_SIGNAL( timeout() ),
	     this, TQT_SLOT( blinkTimerTimeout() ) );
    d->scrollTimer = new TQTimer( this );
    connect( d->scrollTimer, TQT_SIGNAL( timeout() ),
	     this, TQT_SLOT( scrollTimerTimeout() ) );
#ifndef TQT_NO_DRAGANDDROP
    d->dnd_timer = new TQTimer( this );
    connect( d->dnd_timer, TQT_SIGNAL( timeout() ),
	     this, TQT_SLOT( dndTimeout() ) );
#endif
    d->scrollTime = 0;

    dummy = TRUE;

    int w  = textWidth( TQString::tqfromLatin1("") );
    contents->append( new TQtMultiLineEditRow(TQString::tqfromLatin1(""), w) );
    (void)setNumRowsAndTruncate();
    setWidth( w );
    setAcceptDrops(TRUE);
    if ( d->maxlines >= 0 && d->maxlines <= 6 ) {
	tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ) );
    } else {
	tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) );
    }
}

/*
  \fn int TQtMultiLineEdit::lineLength( int line ) const
  Returns the number of characters at line number \a line.
*/

/* \fn TQString *TQtMultiLineEdit::getString( int line ) const

  Returns a pointer to the text at line \a line.
*/

/* \fn void TQtMultiLineEdit::textChanged()

  This signal is emitted when the text is changed by an event or by a
  slot. Note that the signal is not emitted when you call a non-slot
  function such as insertLine().

  \sa returnPressed()
*/

/* \fn void TQtMultiLineEdit::returnPressed()

  This signal is emitted when the user presses the return or enter
  key. It is not emitted if isReadOnly() is TRUE.

  \sa textChanged()
*/

/*
  \fn void TQtMultiLineEdit::undoAvailable (bool yes)

  This signal is emitted when the availability of undo changes.
  If \a yes is TRUE, then undo() will work until
  undoAvailable( FALSE ) is next emitted.
*/

/*
  \fn void TQtMultiLineEdit::redoAvailable (bool yes)

  This signal is emitted when the availability of redo changes.
  If \a yes is TRUE, then redo() will work until
  redoAvailable( FALSE ) is next emitted.
*/

/*
  \fn void TQtMultiLineEdit::copyAvailable (bool yes)

  This signal is emitted when the availability of cut/copy changes.
  If \a yes is TRUE, then cut() and copy() will work until
  copyAvailable( FALSE ) is next emitted.
*/


/*
  If \a on is FALSE, this multi line edit accepts text input.
  Scrolling and cursor movements are accepted in any case.

  \sa isReadOnly() TQWidget::setEnabled()
*/

void TQtMultiLineEdit::setReadOnly( bool on )
{
    if ( readOnly != on ) {
	readOnly = on;
#ifndef TQT_NO_CURSOR
	setCursor( on ? arrowCursor : ibeamCursor );
#endif
    }
}

/*
*/
int TQtMultiLineEdit::maxLineWidth() const
{
    return d->maxLineWidth;
}

/*
  Destroys the TQtMultiLineEdit
*/

TQtMultiLineEdit::~TQtMultiLineEdit()
{
    delete contents;
    delete d;
}

static TQPixmap *buffer = 0;

static void cleanupMLBuffer()
{
    delete buffer;
    buffer = 0;
}

static TQPixmap *getCacheBuffer( TQSize sz )
{
    if ( !buffer ) {
	qAddPostRoutine( cleanupMLBuffer );
	buffer = new TQPixmap;
    }

    if ( buffer->width() < sz.width() || buffer->height() < sz.height() )
	buffer->resize( sz );
    return buffer;
}

/*
  Implements the basic drawing logic. Paints the line at row \a row
  using painter \a painter. The \a col parameter is ignored.
*/
void TQtMultiLineEdit::paintCell( TQPainter *painter, int row, int )
{
    const TQColorGroup & g = tqcolorGroup();
    TQFontMetrics fm( painter->font() );
    TQString s = stringShown(row);
    if ( s.isNull() ) {
	qWarning( "TQtMultiLineEdit::paintCell: (%s) no text at line %d",
		  name( "unnamed" ), row );
	return;
    }
    TQRect updateR = cellUpdateRect();
    TQPixmap *buffer = getCacheBuffer( updateR.size() );
    ASSERT(buffer);
    buffer->fill ( g.base() );

    TQPainter p( buffer );
    p.setFont( painter->font() );
    p.translate( -updateR.left(), -updateR.top() );

    p.setTabStops( tabStopDist(fm) );

    int yPos = 0;
    int markX1, markX2;				// in x-coordinate pixels
    markX1 = markX2 = 0;			// avoid gcc warning
    if ( markIsOn ) {
	int markBeginX, markBeginY;
	int markEndX, markEndY;
	getMarkedRegion( &markBeginY, &markBeginX, &markEndY, &markEndX );
	if ( row >= markBeginY && row <= markEndY ) {
	    if ( row == markBeginY ) {
		markX1 = markBeginX;
		if ( row == markEndY ) 		// both marks on same row
		    markX2 = markEndX;
		else
		    markX2 = s.length();	// mark till end of line
	    } else {
		if ( row == markEndY ) {
		    markX1 = 0;
		    markX2 = markEndX;
		} else {
		    markX1 = 0;			// whole line is marked
		    markX2 = s.length();	// whole line is marked
		}
	    }
	}
    }
    p.setPen( g.text() );
    TQtMultiLineEditRow* r = contents->at( row );
    int wcell = cellWidth() - 2*d->lr_marg;// - d->marg_extra;
    int wrow = r->w;
    int x = d->lr_marg - p.fontMetrics().leftBearing(s[0]);
    if ( d->align == TQt::AlignCenter || d->align == TQt::AlignHCenter )
	x += (wcell - wrow) / 2;
    else if ( d->align == TQt::AlignRight )
	x += wcell - wrow;
    p.drawText( x,  yPos, cellWidth()-d->lr_marg-x, cellHeight(),
		d->align == AlignLeft?ExpandTabs:0, s );
    if ( !r->newline && BREAK_WITHIN_WORDS )
	p.drawPixmap( x + wrow - d->lr_marg - d->marg_extra, yPos, d->arrow );
#if 0
    if ( r->newline )
	p.drawLine( d->lr_marg,  yPos+cellHeight()-2, cellWidth() - d->lr_marg, yPos+cellHeight()-2);
#endif
    if ( markX1 != markX2 ) {
	int sLength = s.length();
	int xpos1   =  mapToView( markX1, row );
	int xpos2   =  mapToView( markX2, row );
	int fillxpos1 = xpos1;
	int fillxpos2 = xpos2;
	if ( markX1 == 0 )
	    fillxpos1 -= 2;
	if ( markX2 == sLength )
	    fillxpos2 += 3;
	p.setClipping( TRUE );
	p.setClipRect( fillxpos1 - updateR.left(), 0,
		       fillxpos2 - fillxpos1, cellHeight(row) );
	p.fillRect( fillxpos1, 0, fillxpos2 - fillxpos1, cellHeight(row),
		    g.brush( TQColorGroup::Highlight ) );
	p.setPen( g.highlightedText() );
	p.drawText( x,  yPos, cellWidth()-d->lr_marg-x, cellHeight(),
		    d->align == AlignLeft?ExpandTabs:0, s );
	p.setClipping( FALSE );
    }

    if ( row == cursorY && cursorOn && !readOnly ) {
	int cursorPos = TQMIN( (int)s.length(), cursorX );
	int cXPos   = TQMAX( mapToView( cursorPos, row ), 0 );
	int cYPos   = 0;
	if ( hasFocus() || d->dnd_forcecursor ) {
	    p.setPen( g.text() );
	    /* styled?
	       p.drawLine( cXPos - 2, cYPos,
	       cXPos + 2, cYPos );
	    */
	    p.drawLine( cXPos, cYPos,
			cXPos, cYPos + fm.height() - 2);
	    /* styled?
	       p.drawLine( cXPos - 2, cYPos + fm.height() - 2,
	       cXPos + 2, cYPos + fm.height() - 2);
	    */

#ifndef TQT_NO_TRANSFORMATIONS
	    // TODO: set it other times, eg. when scrollbar moves view
	    TQWMatrix wm = painter->tqworldMatrix();
	    setMicroFocusHint( int(wm.dx()+cXPos),
			       int (wm.dy()+cYPos),
			       1, fm.ascent() );
#else
	    setMicroFocusHint( cXPos,
			       cYPos,
			       1, fm.ascent() );
#endif
	}
    }
    p.end();
    painter->drawPixmap( updateR.left(), updateR.top(), *buffer,
			 0, 0, updateR.width(), updateR.height() );
}


/*
    \overload
  Returns the width in pixels of the string \a s.
  NOTE: only appropriate for whole lines.
*/

int TQtMultiLineEdit::textWidth( const TQString &s )
{
    int w = 0;
    if ( !s.isNull() ) {
	w = textWidthWithTabs( TQFontMetrics( font() ), s, 0, s.length(),
			       d->align );
    }
    return w + 2 * d->lr_marg + d->marg_extra;
}


/*
  Returns the width in pixels of the text at line \a line.
*/

int TQtMultiLineEdit::textWidth( int line )
{
    if ( d->echomode == Password) {
	TQString s = stringShown(line);
	return textWidth( s );
    }
    TQtMultiLineEditRow* r = contents->at(line);
    return r?r->w:0;
}

/*
  Starts the cursor blinking.
*/

void TQtMultiLineEdit::focusInEvent( TQFocusEvent * )
{
    stopAutoScroll();
    if ( !d->blinkTimer->isActive() )
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
    cursorOn = TRUE;
    updateCell( cursorY, 0, FALSE );
}


/*\reimp
*/
void TQtMultiLineEdit::leaveEvent( TQEvent * )
{
}


/*\reimp
*/
void TQtMultiLineEdit::focusOutEvent( TQFocusEvent * )
{
    stopAutoScroll();
    d->blinkTimer->stop();
    if ( cursorOn ) {
	cursorOn = FALSE;
	updateCell( cursorY, 0, FALSE );
    }
}


/*
  \reimp
  Present for binary compatibility only!
*/

void TQtMultiLineEdit::timerEvent( TQTimerEvent * )
{
    // ############ Remove in 3.0!!!!!!!!
}

#ifndef TQT_NO_DRAGANDDROP
void TQtMultiLineEdit::doDrag()
{
    if ( d->dnd_timer ) {
	d->dnd_timer->stop();
    }
    TQDragObject *drag_text = new TQTextDrag(markedText(), this);
    if ( readOnly ) {
	drag_text->dragCopy();
    } else {
	if ( drag_text->drag() && TQDragObject::target() != this ) {
	    del();
	    if ( textDirty && !d->isHandlingEvent )
		emit textChanged();
	}
    }
    d->dnd_primed = FALSE;
}
#endif

/*
  If there is marked text, sets \a line1, \a col1, \a line2 and \a col2
  to the start and end of the marked region and returns TRUE. Returns
  FALSE if there is no marked text.
 */
bool TQtMultiLineEdit::getMarkedRegion( int *line1, int *col1,
				      int *line2, int *col2 ) const
{
    if ( !markIsOn || !line1 || !line2 || !col1 || !col2 )
	return FALSE;
    if ( markAnchorY < markDragY ||
	 markAnchorY == markDragY && markAnchorX < markDragX) {
	*line1 = markAnchorY;
	*col1 = markAnchorX;
	*line2 = markDragY;
	*col2 = markDragX;
	if ( *line2 > numLines() - 1 ) {
	    *line2 = numLines() - 1;
	    *col2 = lineLength( *line2 );
	}
    } else {
	*line1 = markDragY;
	*col1 = markDragX;
	*line2 = markAnchorY;
	*col2 = markAnchorX;
	if ( *line2 > numLines() - 1 ) {
	    *line2 = numLines() - 1;
	    *col2 = lineLength( *line2 );
	}
    }
    return markIsOn;
}


/*
  Returns TRUE if there is marked text.
*/

bool TQtMultiLineEdit::hasMarkedText() const
{
    return markIsOn;
}


/*
  Returns a copy of the marked text.
*/

TQString TQtMultiLineEdit::markedText() const
{
    int markBeginX, markBeginY;
    int markEndX, markEndY;
    if ( !getMarkedRegion( &markBeginY, &markBeginX, &markEndY, &markEndX ) )
	return TQString();
    if ( markBeginY == markEndY ) { //just one line
	TQString *s  = getString( markBeginY );
	return s->mid( markBeginX, markEndX - markBeginX );
    } else { //multiline
	TQString *firstS, *lastS;
	firstS = getString( markBeginY );
	lastS  = getString( markEndY );
	int i;
	TQString tmp;
	if ( firstS )
	    tmp += firstS->mid(markBeginX);
	if ( contents->at( markBeginY )->newline )
	    tmp += '\n';

	for( i = markBeginY + 1; i < markEndY ; i++ ) {
	    tmp += *getString(i);
	    if ( contents->at( i )->newline )
		tmp += '\n';
	}

	if ( lastS ) {
	    tmp += lastS->left(markEndX);
	} else {
	    tmp.truncate(tmp.length()-1);
	}

	return tmp;
    }
}



/*
  Returns the text at line number \a line (possibly the empty string),
  or a \link TQString::operator!() null string\endlink if \a line is invalid.
*/

TQString TQtMultiLineEdit::textLine( int line ) const
{
    TQString *s = getString(line);
    if ( s ) {
	if ( s->isNull() )
	    return TQString::tqfromLatin1("");
	else
	    return *s;
    } else
	return TQString::null;
}


/*
  Returns a copy of the whole text. If the multi line edit contains no
  text, a
  \link TQString::operator!() null string\endlink
  is returned.
*/

TQString TQtMultiLineEdit::text() const
{
    TQString tmp;
    for( int i = 0 ; i < (int)contents->count() ; i++ ) {
	tmp += *getString(i);
	if ( i+1 < (int)contents->count() && contents->at(i)->newline )
	    tmp += '\n';
    }
    return tmp;
}


/*
  Selects all text without moving the cursor.
*/

void TQtMultiLineEdit::selectAll()
{
    markAnchorX    = 0;
    markAnchorY    = 0;
    markDragY = numLines() - 1;
    markDragX = lineLength( markDragY );
    turnMark( markDragX != markAnchorX || markDragY != markAnchorY );
    if ( autoUpdate() )
	update();
}



/*
  Deselects all text (i.e. removes marking) and leaves the cursor at the
  current position.
*/

void TQtMultiLineEdit::deselect()
{
    turnMark( FALSE );
}


/*
  Sets the text to \a s, removing old text, if any.
*/

void TQtMultiLineEdit::setText( const TQString &s )
{
    bool oldUndo = isUndoEnabled();
    setUndoEnabled( FALSE );
    bool oldAuto = autoUpdate();
    setAutoUpdate( FALSE );
    bool b = tqsignalsBlocked();
    blockSignals( TRUE );
    clear();
    CLEAR_UNDO
    blockSignals( b );
    insertLine( s, -1 );
    emit textChanged();
    setAutoUpdate(oldAuto);
    if ( autoUpdate() )
	update();
    setUndoEnabled( oldUndo );
}


/*
  Appends \a s to the text.
*/

void TQtMultiLineEdit::append( const TQString &s )
{
    bool oldUndo = isUndoEnabled();
    setUndoEnabled( FALSE );
    insertLine( s, -1 );
    setUndoEnabled( oldUndo );
    emit textChanged();
}

/* \reimp
Passes wheel events to the vertical scrollbar.
*/
void TQtMultiLineEdit::wheelEvent( TQWheelEvent *e ){
    TQApplication::sendEvent( verticalScrollBar(), e);
}


/*
  The key press event handler converts a key press in event \a e to
  some line editor action.

  Here are the default key bindings when isReadOnly() is FALSE:
  \list
  \i <i> Left Arrow </i> Move the cursor one character leftwards
  \i <i> Right Arrow </i> Move the cursor one character rightwards
  \i <i> Up Arrow </i> Move the cursor one line upwards
  \i <i> Down Arrow </i> Move the cursor one line downwards
  \i <i> Page Up </i> Move the cursor one page upwards
  \i <i> Page Down </i> Move the cursor one page downwards
  \i <i> Backspace </i> Delete the character to the left of the cursor
  \i <i> Home </i> Move the cursor to the beginning of the line
  \i <i> End </i> Move the cursor to the end of the line
  \i <i> Delete </i> Delete the character to the right of the cursor
  \i <i> Shift - Left Arrow </i> Mark text one character leftwards
  \i <i> Shift - Right Arrow </i> Mark text one character rightwards
  \i <i> Control-A </i> Move the cursor to the beginning of the line
  \i <i> Control-B </i> Move the cursor one character leftwards
  \i <i> Control-C </i> Copy the marked text to the clipboard
  \i <i> Control-D </i> Delete the character to the right of the cursor
  \i <i> Control-E </i> Move the cursor to the end of the line
  \i <i> Control-F </i> Move the cursor one character rightwards
  \i <i> Control-H </i> Delete the character to the left of the cursor
  \i <i> Control-K </i> Delete to end of line
  \i <i> Control-N </i> Move the cursor one line downwards
  \i <i> Control-P </i> Move the cursor one line upwards
  \i <i> Control-V </i> Paste the clipboard text into line edit
  \i <i> Control-X </i> Cut the marked text, copy to clipboard
  \i <i> Control-Z </i> Undo the last operation
  \i <i> Control-Y </i> Redo the last operation
  \i <i> Control - Left Arrow </i> Move the cursor one word leftwards
  \i <i> Control - Right Arrow </i> Move the cursor one word rightwards
  \i <i> Control - Up Arrow </i> Move the cursor one word upwards
  \i <i> Control - Down Arrow </i> Move the cursor one word downwards
  \i <i> Control - Home Arrow </i> Move the cursor to the beginning of the text
  \i <i> Control - End Arrow </i> Move the cursor to the end of the text
  \endlist
  In addition, the following key bindings are used on Windows:
  \list
  \i <i> Shift - Delete </i> Cut the marked text, copy to clipboard
  \i <i> Shift - Insert </i> Paste the clipboard text into line edit
  \i <i> Control - Insert </i> Copy the marked text to the clipboard
  \endlist
  All other keys with valid ASCII codes insert themselves into the line.

  Here are the default key bindings when isReadOnly() is TRUE:
  \list
  \i <i> Left Arrow </i> Scrolls the table rightwards
  \i <i> Right Arrow </i> Scrolls the table rightwards
  \i <i> Up Arrow </i> Scrolls the table one line downwards
  \i <i> Down Arrow </i> Scrolls the table one line upwards
  \i <i> Page Up </i> Scrolls the table one page downwards
  \i <i> Page Down </i> Scrolls the table one page upwards
  \i <i> Control-C </i> Copy the marked text to the clipboard
  \endlist

*/

void TQtMultiLineEdit::keyPressEvent( TQKeyEvent *e )
{
    textDirty = FALSE;
    d->isHandlingEvent = TRUE;
    int unknown = 0;
    if ( readOnly ) {
	int pageSize = viewHeight() / cellHeight();

	switch ( e->key() ) {
	case Key_Left:
	    setXOffset( xOffset() - viewWidth()/10 );
	    break;
	case Key_Right:
	    setXOffset( xOffset() + viewWidth()/10 );
	    break;
	case Key_Up:
	    setTopCell( topCell() - 1 );
	    break;
	case Key_Down:
	    setTopCell( topCell() + 1 );
	    break;
	case Key_Home:
	    setCursorPosition(0,0, e->state() & ShiftButton );
	    break;
	case Key_End:
	    setCursorPosition( numLines()-1, lineLength( numLines()-1 ),
			       e->state() & ShiftButton );
	    break;
        case Key_Next:
	    setTopCell( topCell() + pageSize );
	    break;
	case Key_Prior:
	    setTopCell( TQMAX( topCell() - pageSize, 0 ) );
	    break;
#ifndef TQT_NO_CLIPBOARD
	case Key_C:
	    if ( echoMode() == Normal && (e->state()&ControlButton) )
		copy();
	    else
		unknown++;
	    break;
	case Key_F16: // Copy key on Sun keyboards
	    if ( echoMode() == Normal )
		copy();
	    else
		unknown++;
	    break;
#endif
	default:
	    unknown++;
	}
	if ( unknown )
	    e->ignore();
	d->isHandlingEvent = FALSE;
	return;
    }
    if ( e->text().length() &&
	 e->key() != Key_Return &&
	 e->key() != Key_Enter &&
	 e->key() != Key_Delete &&
	 e->key() != Key_Backspace &&
	 (!e->ascii() || e->ascii()>=32)
	 ) {
	insert( e->text() );
	//TQApplication::sendPostedEvents( this, TQEvent::Paint );
	if ( textDirty )
	    emit textChanged();
	d->isHandlingEvent = FALSE;
	return;
    }
    if ( e->state() & ControlButton ) {
	switch ( e->key() ) {
	case Key_A:
	    home( e->state() & ShiftButton );
	    break;
	case Key_B:
	    cursorLeft( e->state() & ShiftButton );
	    break;
#ifndef TQT_NO_CLIPBOARD
	case Key_C:
	    if ( echoMode() == Normal )
		copy();
	    break;
#endif
	case Key_D:
	    del();
	    break;
	case Key_E:
	    end( e->state() & ShiftButton );
	    break;
	case Key_Left:
	    cursorWordBackward( e->state() & ShiftButton );
	    break;
	case Key_Right:
	    cursorWordForward( e->state() & ShiftButton );
	    break;
	case Key_Up:
	    cursorUp( e->state() & ShiftButton );
	    break;
	case Key_Down:
	    cursorDown( e->state() & ShiftButton );
	    break;
	case Key_Home:
	    setCursorPosition(0,0, e->state() & ShiftButton );
	    break;
	case Key_End:
	    setCursorPosition( numLines()-1, lineLength( numLines()-1 ),
			       e->state() & ShiftButton );
	    break;
	case Key_F:
	    cursorRight( e->state() & ShiftButton );
	    break;
	case Key_H:
	    backspace();
	    break;
	case Key_K:
	    killLine();
	    break;
	case Key_N:
	    cursorDown( e->state() & ShiftButton );
	    break;
	case Key_P:
	    cursorUp( e->state() & ShiftButton );
	    break;
#ifndef TQT_NO_CLIPBOARD
	case Key_V:
	    paste();
	    break;
	case Key_X:
	    cut();
	    break;
#endif
	case Key_Z:
	    undo();
	    break;
	case Key_Y:
	    redo();
	    break;
#if defined (_WS_WIN_)
	case Key_Insert:
	    copy();
#endif
	default:
	    unknown++;
	}
    } else {
	switch ( e->key() ) {
	case Key_Left:
	    cursorLeft( e->state() & ShiftButton );
	    break;
	case Key_Right:
	    cursorRight( e->state() & ShiftButton );
	    break;
	case Key_Up:
	    cursorUp( e->state() & ShiftButton );
	    break;
	case Key_Down:
	    cursorDown( e->state() & ShiftButton );
	    break;
	case Key_Backspace:
	    backspace();
	    break;
	case Key_Home:
	    home( e->state() & ShiftButton );
	    break;
	case Key_End:
	    end( e->state() & ShiftButton );
	    break;
	case Key_Delete:
#if defined (_WS_WIN_)
	    if ( e->state() & ShiftButton ) {
		cut();
		break;
	    }
#endif
	    del();
	    break;
	case Key_Next:
	    pageDown( e->state() & ShiftButton );
	    break;
	case Key_Prior:
	    pageUp( e->state() & ShiftButton );
	    break;
	case Key_Enter:
	case Key_Return:
	    newLine();
	    emit returnPressed();
	    break;
	case Key_Tab:
	    insert( e->text() );
	    break;
#if defined (_WS_WIN_)
	case Key_Insert:
	    if ( e->state() & ShiftButton )
		paste();
	    else
		unknown++;
	    break;
#endif
	case Key_F14: // Undo key on Sun keyboards
	    undo();
	    break;
#ifndef TQT_NO_CLIPBOARD
	case Key_F16: // Copy key on Sun keyboards
	    if ( echoMode() == Normal )
		copy();
	    break;
	case Key_F18: // Paste key on Sun keyboards
	    paste();
	    break;
	case Key_F20: // Paste key on Sun keyboards
	    cut();
	    break;
#endif
	default:
	    unknown++;
	}
    }
    if ( textDirty )
	emit textChanged();

    if ( unknown )				// unknown key
	e->ignore();

    d->isHandlingEvent = FALSE;
}


/*
  Moves the cursor one page down.  If \a mark is TRUE, the text
  is marked.
*/

void TQtMultiLineEdit::pageDown( bool mark )
{
    bool oldAuto = autoUpdate();
    if ( mark )
	setAutoUpdate( FALSE );

    if ( partiallyInvisible( cursorY ) )
	cursorY = topCell();
    int delta = cursorY - topCell();
    int pageSize = viewHeight() / cellHeight();
    int newTopCell = TQMIN( topCell() + pageSize, numLines() - 1 - pageSize );

    if ( pageSize >= numLines() ) { // quick fix to handle small texts
	newTopCell = topCell();
    }
    if ( !curXPos )
	curXPos = mapToView( cursorX, cursorY );
    int oldY = cursorY;

    if ( mark && !hasMarkedText() ) {
	markAnchorX    = cursorX;
	markAnchorY    = cursorY;
    }
    if ( newTopCell != topCell() ) {
	cursorY = newTopCell + delta;
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	setTopCell( newTopCell );
    } else if ( cursorY != (int)contents->count() - 1) { // just move the cursor
	cursorY = TQMIN( cursorY + pageSize, numLines() - 1);
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	makeVisible();
    }
    if ( oldAuto )
	if ( mark ) {
	    setAutoUpdate( TRUE );
	    update();
	} else {
	    updateCell( oldY, 0, FALSE );
	}
    if ( !mark )
	turnMark( FALSE );
}


/*
  Moves the cursor one page up.  If \a mark is TRUE, the text
  is marked.
*/

void TQtMultiLineEdit::pageUp( bool mark )
{
    bool oldAuto = autoUpdate();
    if ( mark )
	setAutoUpdate( FALSE );
    if ( partiallyInvisible( cursorY ) )
	cursorY = topCell();
    int delta = cursorY - topCell();
    int pageSize = viewHeight() / cellHeight();
    bool partial = delta == pageSize && viewHeight() != pageSize * cellHeight();
    int newTopCell = TQMAX( topCell() - pageSize, 0 );
    if ( pageSize > numLines() ) { // quick fix to handle small texts
	newTopCell = 0;
	delta = 0;
    }
    if ( mark && !hasMarkedText() ) {
	markAnchorX    = cursorX;
	markAnchorY    = cursorY;
    }
    if ( !curXPos )
	curXPos = mapToView( cursorX, cursorY );
    int oldY = cursorY;
    if ( newTopCell != topCell() ) {
	cursorY = TQMIN( newTopCell + delta, numLines() - 1 );
	if ( partial )
	    cursorY--;
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	setTopCell( newTopCell );
    } else { // just move the cursor
	cursorY = TQMAX( cursorY - pageSize, 0 );
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
    }
    if ( oldAuto )
	if ( mark ) {
	    setAutoUpdate( TRUE );
	    update();
	} else {
	    updateCell( oldY, 0, FALSE );
	}
    if ( !mark )
	turnMark( FALSE );
}

// THE CORE INSERTION FUNCTION
void TQtMultiLineEdit::insertAtAux( const TQString &txt, int line, int col, bool mark )
{
    dummy = FALSE;
    d->blinkTimer->stop();
    cursorOn = TRUE;
    int oldw = contentsRect().width();

    line = TQMAX( TQMIN( line, numLines() - 1), 0 );
    col = TQMAX( TQMIN( col,  lineLength( line )), 0 );

    TQString itxt = txt;
    TQtMultiLineEditRow  *row = contents->at( line );
    if ( d->maxlen >= 0 && length() + int(txt.length()) > d->maxlen )
	itxt.truncate( d->maxlen - length() );

    row->s.insert( uint(col), itxt );

    if ( mark ) {
	markAnchorX = col;
	markAnchorY = line;
    }
    if ( cursorX == col && cursorY == line ) {
	cursorX += itxt.length();
    }
    TQFontMetrics fm( font() );
    if ( !WORD_WRAP || ( col == 0 && itxt.contains('\n') == int(itxt.length())) )
	wrapLine( line, 0 );
    else if ( WORD_WRAP && itxt.find('\n')<0 && itxt.find('\t')<0
	      && (
		  ( DYNAMIC_WRAP && fm.width( itxt ) + row->w < contentsRect().width() -  2*d->lr_marg - d->marg_extra )
		  ||
		  ( FIXED_WIDTH_WRAP && ( d->wrapcol < 0 || fm.width( itxt ) + row->w < d->wrapcol ) )
		  ||
		  ( FIXED_COLUMN_WRAP && ( d->wrapcol < 0 || int(row->s.length()) < d->wrapcol ) )
		  )
	      && ( itxt.find(' ') < 0 || row->s.find(' ') >= 0 && row->s.find(' ') < col ) ){
	row->w = textWidth( row->s );
	setWidth( TQMAX( maxLineWidth(), row->w) );
	updateCell( line, 0, FALSE );
    }
    else {
	if ( line > 0 && !contents->at( line-1)->newline )
	    rebreakParagraph( line-1 );
	else
	    rebreakParagraph( line );
    }
    if ( mark )
	newMark( cursorX, cursorY, FALSE );

    setNumRowsAndTruncate();

    textDirty = TRUE;
    d->edited = TRUE;
    if ( autoUpdate() ) {
	makeVisible();
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
	if ( DYNAMIC_WRAP && oldw != contentsRect().width() ) {
	    setAutoUpdate( FALSE );
	    rebreakAll();
	    setAutoUpdate( TRUE );
	    update();
	}
    }
}


/*
  Inserts \a txt at line number \a line. If \a line is less than zero,
  or larger than the number of rows, the new text is put at the end.
  If \a txt contains newline characters, several lines are inserted.

  The cursor position is not changed.
*/

void TQtMultiLineEdit::insertLine( const TQString &txt, int line )
{
    TQString s = txt;
    int oldXPos = cursorX;
    int oldYPos = cursorY;
    if ( line < 0 || line >= int( contents->count() ) ) {
	if ( !dummy )
	    contents->append( new TQtMultiLineEditRow(TQString::tqfromLatin1(""), 0) );
	insertAt( s, numLines()-1, 0 );
    } else {
	s.append('\n');
	insertAt( s, line, 0 );
    }
    cursorX = oldXPos;
    cursorY = oldYPos;
}

/*
  Deletes the line at line number \a line. If \a
  line is less than zero, or larger than the number of lines,
  no line is deleted.
*/

void TQtMultiLineEdit::removeLine( int line )
{
    CLEAR_UNDO
    if ( line >= numLines()  )
	return;
    if ( cursorY >= line && cursorY > 0 )
	cursorY--;
    bool updt = autoUpdate() && rowIsVisible( line );
    TQtMultiLineEditRow* r = contents->at( line );
    ASSERT( r );
    bool recalc = r->w == maxLineWidth();
    contents->remove( line );
    if ( contents->count() == 0 ) {
	int w  = textWidth( TQString::tqfromLatin1("") );
	contents->append( new TQtMultiLineEditRow(TQString::tqfromLatin1(""), w) );
	setWidth( w );
	dummy = TRUE;
    }
    if ( setNumRowsAndTruncate() )
	recalc = updt = FALSE;
    if ( recalc )
	updateCellWidth();
    makeVisible();
    if (updt)
	update();
    textDirty = TRUE;
    d->edited = TRUE;
}

/*
  Inserts \a s at the current cursor position.
*/
void TQtMultiLineEdit::insert( const TQString& s )
{
    insert( s, FALSE );
}

/*
  Inserts \a c at the current cursor position.
  (this function is provided for backward compatibility -
  it simply calls insert()).
*/
void TQtMultiLineEdit::insertChar( TQChar c )
{
    insert(c);
}

/*
    \overload
  Inserts string \a str at the current cursor position. If \a mark is
  TRUE the string is marked.
*/

void TQtMultiLineEdit::insert( const TQString& str, bool mark )
{
    dummy = FALSE;
    bool wasMarkedText = hasMarkedText();
    if ( wasMarkedText )
	addUndoCmd( new TQBeginCommand );
    if ( wasMarkedText )
	del();					// ## Will flicker
    TQString *s = getString( cursorY );
    if ( cursorX > (int)s->length() )
	cursorX = s->length();
    else if ( overWrite && !wasMarkedText && cursorX < (int)s->length() )
	del();                                 // ## Will flicker
    insertAt(str, cursorY, cursorX, mark );
    makeVisible();

    if ( wasMarkedText )
	addUndoCmd( new TQEndCommand() );
}

/*
  Makes a line break at the current cursor position.
*/

void TQtMultiLineEdit::newLine()
{
    insert("\n");
}

/*
  Deletes text from the current cursor position to the end of the line.
*/

void TQtMultiLineEdit::killLineAux()
{
    deselect(); // -sanders Don't let del() delete marked region
    TQtMultiLineEditRow* r = contents->at( cursorY );
    if ( cursorX == (int)r->s.length() ) {
	//      if (r->newline) // -sanders Only del newlines!
	del();
	return;
    } else {
	bool recalc = r->w == maxLineWidth();
	r->s.remove( cursorX, r->s.length() );
	r->w = textWidth( r->s );
	updateCell( cursorY, 0, FALSE );
	if ( recalc )
	    updateCellWidth();
	rebreakParagraph( cursorY ); // -sanders
	textDirty = TRUE;
	d->edited = TRUE;
    }
    curXPos  = 0;
    makeVisible();
    turnMark( FALSE );
}


/*
  Moves the cursor one character to the left. If \a mark is TRUE, the text
  is marked. If \a wrap is TRUE, the cursor moves to the end of the
  previous line  if it is placed at the beginning of the current line.

  \sa cursorRight() cursorUp() cursorDown()
*/

void TQtMultiLineEdit::cursorLeft( bool mark, bool wrap )
{
    cursorLeft(mark,!mark,wrap);
}
void TQtMultiLineEdit::cursorLeft( bool mark, bool clear_mark, bool wrap )
{
    if ( cursorX != 0 || cursorY != 0 && wrap ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	d->blinkTimer->stop();
	int ll = lineLength( cursorY );
	if ( cursorX > ll )
	    cursorX = ll;
	cursorOn = TRUE;
	cursorX--;
	if ( cursorX < 0 ) {
	    int oldY = cursorY;
	    if ( cursorY > 0 ) {
		cursorY--;
		cursorX = lineLength( cursorY );
		if ( cursorX > 1 && !isEndOfParagraph( cursorY ) )
		    cursorX--;
	    } else {
		cursorY = 0; //### ?
		cursorX = 0;
	    }
	    updateCell( oldY, 0, FALSE );
	}
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
	updateCell( cursorY, 0, FALSE );
    }
    curXPos  = 0;
    makeVisible();
    if ( clear_mark )
	turnMark( FALSE );
}

/*
  Moves the cursor one character to the right.  If \a mark is TRUE, the text
  is marked. If \a wrap is TRUE, the cursor moves to the beginning of the next
  line if it is placed at the end of the current line.
  \sa cursorLeft() cursorUp() cursorDown()
*/

void TQtMultiLineEdit::cursorRight( bool mark, bool wrap )
{
    cursorRight(mark,!mark,wrap);
}
void TQtMultiLineEdit::cursorRight( bool mark, bool clear_mark, bool wrap )
{
    int strl = lineLength( cursorY );
    if ( strl > 1 && !isEndOfParagraph( cursorY ) )
	 strl--;
    if ( cursorX < strl || cursorY < (int)contents->count() - 1 && wrap ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	d->blinkTimer->stop();
	cursorOn = TRUE;
	cursorX++;
	if ( cursorX > strl ) {
	    int oldY = cursorY;
	    if ( cursorY < (int) contents->count() - 1 ) {
		cursorY++;
		cursorX = 0;
	    } else {
		cursorX = lineLength( cursorY );
	    }
	    updateCell( oldY, 0, FALSE );
	}
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	updateCell( cursorY, 0, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
    }
    curXPos  = 0;
    makeVisible();
    if ( clear_mark )
	turnMark( FALSE );
}

/*
  Moves the cursor up one line.  If \a mark is TRUE, the text
  is marked.
  \sa cursorDown() cursorLeft() cursorRight()
*/

void TQtMultiLineEdit::cursorUp( bool mark )
{
    cursorUp(mark,!mark);
}
void TQtMultiLineEdit::cursorUp( bool mark, bool clear_mark )
{
    if ( cursorY != 0 ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	if ( !curXPos )
	    curXPos = mapToView( cursorX, cursorY );
	int oldY = cursorY;
	d->blinkTimer->stop();
	cursorOn = TRUE;
	cursorY--;
	if ( cursorY < 0 ) {
	    cursorY = 0;
	}
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	updateCell( oldY, 0, FALSE );
	updateCell( cursorY, 0, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
    }
    makeVisible();
    if ( clear_mark )
	turnMark( FALSE );
}

/*
  Moves the cursor one line down.  If \a mark is TRUE, the text
  is marked.
  \sa cursorUp() cursorLeft() cursorRight()
*/

void TQtMultiLineEdit::cursorDown( bool mark )
{
    cursorDown(mark,!mark);
}
void TQtMultiLineEdit::cursorDown( bool mark, bool clear_mark )
{
    int lastLin = contents->count() - 1;
    if ( cursorY != lastLin ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	if ( !curXPos )
	    curXPos = mapToView( cursorX, cursorY );
	int oldY = cursorY;
	d->blinkTimer->stop();
	cursorOn = TRUE;
	cursorY++;
	if ( cursorY > lastLin ) {
	    cursorY = lastLin;
	}
	cursorX = mapFromView( curXPos, cursorY );
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	updateCell( oldY, 0, FALSE );
	updateCell( cursorY, 0, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
    }
    makeVisible();
    if ( clear_mark )
	turnMark( FALSE );
}

/*
  Turns off marked text
*/
void TQtMultiLineEdit::turnMark( bool on )
{
    if ( on != markIsOn ) {
	markIsOn = on;
	if ( echoMode() == Normal )
	    emit copyAvailable( on );
	update();
    }
}




/*
  Deletes the character on the left side of the text cursor and moves
  the cursor one position to the left. If a text has been marked by
  the user (e.g. by clicking and dragging) the cursor is put at the
  beginning of the marked text and the marked text is removed.
  \sa del()
*/

void TQtMultiLineEdit::backspace()
{
    if ( hasMarkedText() ) {
	del();
    } else {
	if ( !atBeginning() ) {
	    cursorLeft( FALSE );
	    del();
	}
    }
    makeVisible();
}

void TQtMultiLineEdit::delAux()
{
    int markBeginX, markBeginY;
    int markEndX, markEndY;
    TQRect oldContents = contentsRect();
    if ( getMarkedRegion( &markBeginY, &markBeginX, &markEndY, &markEndX ) ) {
	turnMark( FALSE );
	textDirty = TRUE;
	d->edited = TRUE;
	if ( markBeginY == markEndY ) { //just one line
	    TQtMultiLineEditRow *r = contents->at( markBeginY );
	    ASSERT(r);
	    bool recalc = r->w == maxLineWidth();
	    r->s.remove( markBeginX, markEndX - markBeginX );
	    r->w = textWidth( r->s );
	    cursorX  = markBeginX;
	    cursorY  = markBeginY;

	    if (autoUpdate() )
		updateCell( cursorY, 0, FALSE );
	    if ( recalc )
		updateCellWidth();
	} else { //multiline
	    bool oldAuto = autoUpdate();
	    setAutoUpdate( FALSE );
	    ASSERT( markBeginY >= 0);
	    ASSERT( markEndY < (int)contents->count() );

	    TQtMultiLineEditRow *firstR, *lastR;
	    firstR = contents->at( markBeginY );
	    lastR  = contents->at( markEndY );
	    ASSERT( firstR != lastR );
	    firstR->s.remove( markBeginX, firstR->s.length() - markBeginX  );
	    lastR->s.remove( 0, markEndX  );
	    firstR->s.append( lastR->s );  // lastS will be removed in loop below
	    firstR->newline = lastR->newline; // Don't forget this -sanders
	    firstR->w = textWidth( firstR->s );

	    for( int i = markBeginY + 1 ; i <= markEndY ; i++ )
		contents->remove( markBeginY + 1 );

	    if ( contents->isEmpty() )
		insertLine( TQString::tqfromLatin1(""), -1 );

	    cursorX  = markBeginX;
	    cursorY  = markBeginY;
	    curXPos  = 0;

	    setNumRowsAndTruncate();
	    updateCellWidth();
	    setAutoUpdate( oldAuto );
	    if ( autoUpdate() )
		update();
	}
	markAnchorY = markDragY = cursorY;
	markAnchorX = markDragX = cursorX;
    } else {
	if ( !atEnd() ) {
	    textDirty = TRUE;
	    d->edited = TRUE;
	    TQtMultiLineEditRow *r = contents->at( cursorY );
	    if ( cursorX == (int) r->s.length() ) { // remove newline
		TQtMultiLineEditRow* other = contents->at( cursorY + 1 );
		if ( ! r->newline && cursorX )
		    r->s.truncate( r->s.length()-1 );

		bool needBreak = !r->s.isEmpty();
		r->s += other->s;
		r->newline =  other->newline;
		contents->remove( cursorY + 1 );
		if ( needBreak )
		    rebreakParagraph( cursorY, 1 );
		else
		    wrapLine( cursorY, 1 );
	    } else {
		bool recalc = r->w == maxLineWidth();
		r->s.remove( cursorX, 1 );
		rebreakParagraph( cursorY );
		if ( recalc )
		    updateCellWidth();
	    }
	}
    }
    if ( DYNAMIC_WRAP && oldContents != contentsRect() ) {
	if ( oldContents.width() != contentsRect().width() ) {
	    bool oldAuto = autoUpdate();
	    setAutoUpdate( FALSE );
	    rebreakAll();
	    setAutoUpdate( oldAuto );
	}
	if ( autoUpdate() )
	    update();
    }
    curXPos  = 0;
    makeVisible();
}

/*
  Moves the text cursor to the left end of the line. If \a mark is
  TRUE, text is marked towards the first position. If it is FALSE and
  the cursor is moved, all marked text is unmarked.

  \sa end()
*/

void TQtMultiLineEdit::home( bool mark )
{
    if ( cursorX != 0 ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	d->blinkTimer->stop();
	cursorX = 0;
	cursorOn = TRUE;
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	updateCell( cursorY, 0, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
    }
    curXPos  = 0;
    if ( !mark )
	turnMark( FALSE );
    makeVisible();
}

/*
  Moves the text cursor to the right end of the line. If \a mark is TRUE
  text is marked towards the last position.  If it is FALSE and the
  cursor is moved, all marked text is unmarked.

  \sa home()
*/

void TQtMultiLineEdit::end( bool mark )
{
    int tlen = lineLength( cursorY );
    if ( cursorX != tlen ) {
	if ( mark && !hasMarkedText() ) {
	    markAnchorX    = cursorX;
	    markAnchorY    = cursorY;
	}
	d->blinkTimer->stop();
	cursorX = tlen;
	cursorOn  = TRUE;
	if ( mark )
	    newMark( cursorX, cursorY, FALSE );
	d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
	updateCell( cursorY, 0, FALSE );
    }
    curXPos  = 0;
    makeVisible();
    if ( !mark )
	turnMark( FALSE );
}

/*\reimp
*/
void TQtMultiLineEdit::mousePressEvent( TQMouseEvent *e )
{
    stopAutoScroll();
    d->dnd_startpos = e->pos();

    if ( e->button() == RightButton ) {
	TQPopupMenu *popup = new TQPopupMenu( this );
	int id[ (int)IdCount ];
	id[ IdUndo ] = popup->insertItem( tr( "Undo" ) );
	id[ IdRedo ] = popup->insertItem( tr( "Redo" ) );
	popup->insertSeparator();
#ifndef TQT_NO_CLIPBOARD
	id[ IdCut ] = popup->insertItem( tr( "Cut" ) );
	id[ IdCopy ] = popup->insertItem( tr( "Copy" ) );
	id[ IdPaste ] = popup->insertItem( tr( "Paste" ) );
#ifndef TQT_NO_MIMECLIPBOARD
	id[ IdPasteSpecial ] = popup->insertItem( tr( "Paste special..." ) );
#endif
#endif
	id[ IdClear ] = popup->insertItem( tr( "Clear" ) );
	popup->insertSeparator();
	id[ IdSelectAll ] = popup->insertItem( tr( "Select All" ) );
	popup->setItemEnabled( id[ IdUndo ],
				  !this->d->undoList.isEmpty() );
	popup->setItemEnabled( id[ IdRedo ],
				  !this->d->redoList.isEmpty() );
#ifndef TQT_NO_CLIPBOARD
	popup->setItemEnabled( id[ IdCut ],
			          !isReadOnly() && hasMarkedText() );
	popup->setItemEnabled( id[ IdCopy ], hasMarkedText() );
	popup->setItemEnabled( id[ IdPaste ],
	    !isReadOnly() && (bool)TQApplication::clipboard()->text().length() );
#ifndef TQT_NO_MIMECLIPBOARD
	// Any non-plain types?
	TQMimeSource* ms = TQApplication::clipboard()->data();
	bool ps = FALSE;
	if ( ms )  {
	    if ( !isReadOnly() ) {
		const char* fmt;
		for (int i=0; !ps && (fmt=ms->format(i)); i++) {
		    ps = qstrnicmp(fmt,"text/",5)==0
		      && qstrnicmp(fmt+5,"plain",5)!=0;
		}
	    }
	}
	popup->setItemEnabled( id[ IdPasteSpecial ], ps );
#endif
#endif
	popup->setItemEnabled( id[ IdClear ],
				  !isReadOnly() && (bool)text().length() );
	int allSelected = markIsOn && markAnchorX == 0 && markAnchorY == 0 &&
			  markDragY == numLines() - 1 && markDragX == lineLength( markDragY );
	popup->setItemEnabled( id[ IdSelectAll ],
				  (bool)text().length() && !allSelected );

	int r = popup->exec( e->globalPos() );
	delete popup;

	if ( r == id[ IdUndo ] )
	    undo();
	else if ( r == id[ IdRedo ] )
	    redo();
#ifndef TQT_NO_CLIPBOARD
	else if ( r == id[ IdCut ] )
	    cut();
	else if ( r == id[ IdCopy ] )
	    copy();
	else if ( r == id[ IdPaste ] )
	    paste();
# ifndef TQT_NO_MIMECLIPBOARD
	else if ( r == id[ IdPasteSpecial ] )
	    pasteSpecial(TQCursor::pos());
# endif
#endif
	else if ( r == id[ IdClear ] )
	    clear();
	else if ( r == id[ IdSelectAll ] )
	    selectAll();
	return;
    }

    if ( e->button() != MidButton && e->button() != LeftButton)
	return;

    int newX, newY;
    pixelPosToCursorPos( e->pos(), &newX, &newY );

    if ( e->state() & ShiftButton ) {
	wordMark = FALSE;
	dragMarking    = TRUE;
	setCursorPosition( newY, newX, TRUE);
	return;
    }

#ifndef TQT_NO_DRAGANDDROP
    if (
	inMark(newX, newY)		// Click on highlighted text
	&& echoMode() == Normal		// No DnD of passwords, etc.
	&& e->pos().y() < totalHeight() // Click past the end is not dragging
	)
    {
	// The user might be trying to drag
	d->dnd_primed = TRUE;
	d->dnd_timer->start( TQApplication::startDragTime(), FALSE );
    } else
#endif
    {
	wordMark = FALSE;
	dragMarking    = TRUE;
	setCursorPixelPosition(e->pos());
    }
}

void TQtMultiLineEdit::pixelPosToCursorPos(TQPoint p, int* x, int* y) const
{
    *y = findRow( p.y() );
    if ( *y < 0 ) {
	if ( p.y() < lineWidth() ) {
	    *y = topCell();
	} else {
	    *y = lastRowVisible();
	    p.setX(cellWidth());
	}
    }
    *y = TQMIN( (int)contents->count() - 1, *y );
    TQFontMetrics fm( font() );
    *x = xPosToCursorPos( stringShown( *y ), fm,
			  p.x() - d->lr_marg + xOffset(),
			  cellWidth() - 2 * d->lr_marg - d->marg_extra,
			  d->align );
    TQtMultiLineEditRow* r = contents->at( *y );
    if (r && !r->newline && ((unsigned int)*x == r->s.length()) && (*x > 0))
	--*x;
}

void TQtMultiLineEdit::setCursorPixelPosition(TQPoint p, bool clear_mark)
{
    int newY;
    pixelPosToCursorPos( p, &cursorX, &newY );
    curXPos        = 0;
    if ( clear_mark ) {
	markAnchorX    = cursorX;
	markAnchorY    = newY;
	bool markWasOn = markIsOn;
	turnMark( FALSE );
	if ( markWasOn ) {
	    cursorY = newY;
	    update();
	    d->isHandlingEvent = FALSE;
	    return;
	}
    }
    if ( cursorY != newY ) {
	int oldY = cursorY;
	cursorY = newY;
	updateCell( oldY, 0, FALSE );
    }
    updateCell( cursorY, 0, FALSE );		// ###
}

void TQtMultiLineEdit::startAutoScroll()
{
    if ( !dragScrolling ) {
	d->scrollTime = initialScrollTime;
	d->scrollAccel = initialScrollAccel;
	d->scrollTimer->start( d->scrollTime, FALSE );
	dragScrolling = TRUE;
    }
}

void TQtMultiLineEdit::stopAutoScroll()
{
    if ( dragScrolling ) {
	d->scrollTimer->stop();
	dragScrolling = FALSE;
    }
}

/*\reimp
*/
void TQtMultiLineEdit::mouseMoveEvent( TQMouseEvent *e )
{
#ifndef TQT_NO_DRAGANDDROP
    d->dnd_timer->stop();
    if ( d->dnd_primed &&
	 ( d->dnd_startpos - e->pos() ).manhattanLength() > TQApplication::startDragDistance() ) {
	doDrag();
	return;
    }
#endif
    if ( !dragMarking )
	return;
    if ( rect().contains( e->pos() ) ) {
	stopAutoScroll();
    } else if ( !dragScrolling ) {
	startAutoScroll();
    }

    int newX, newY;
    pixelPosToCursorPos(e->pos(), &newX, &newY);

    if ( wordMark ) {
	extendSelectionWord( newX, newY);
    }

    if ( markDragX == newX && markDragY == newY )
	return;
    int oldY = markDragY;
    newMark( newX, newY, FALSE );
    for ( int i = TQMIN(oldY,newY); i <= TQMAX(oldY,newY); i++ )
	updateCell( i, 0, FALSE );
}

void TQtMultiLineEdit::extendSelectionWord( int &newX, int&newY)
{
    TQString s = stringShown( newY );
    int lim = s.length();
    if ( newX >= 0 && newX < lim ) {
	int i = newX;
	int startclass = charClass(s.at(i));
	if ( markAnchorY < markDragY ||
	     ( markAnchorY == markDragY && markAnchorX < markDragX ) ) {
	    // going right
	    while ( i < lim && charClass(s.at(i)) == startclass )
		i++;
	} else {
	    // going left
	    while ( i >= 0 && charClass(s.at(i)) == startclass )
		i--;
	    i++;
	}
	newX = i;
    }
}




/*\reimp
*/
void TQtMultiLineEdit::mouseReleaseEvent( TQMouseEvent *e )
{
    stopAutoScroll();
#ifndef TQT_NO_DRAGANDDROP
    if ( d->dnd_timer->isActive() ) {
	d->dnd_timer->stop();
	d->dnd_primed = FALSE;
	setCursorPixelPosition(e->pos());
    }
#endif
    wordMark = FALSE;
    dragMarking   = FALSE;
    textDirty = FALSE;
    d->isHandlingEvent = TRUE;
    if ( markAnchorY == markDragY && markAnchorX == markDragX )
	turnMark( FALSE );

#ifndef TQT_NO_CLIPBOARD
#if defined(_WS_X11_)
    else if ( echoMode() == Normal )
	copy();
#endif

    if ( e->button() == MidButton && !readOnly ) {
#if defined(_WS_X11_)
	paste();		// Will tqrepaint the cursor line.
#else
#ifndef TQT_NO_COMPAT
	if ( style().tqstyleHint(TQStyle::SH_GUIStyle) == TQt::MotifStyle )
	    paste();
#endif
#endif
    }
#endif

    d->isHandlingEvent = FALSE;

    if ( !readOnly && textDirty )
	emit textChanged();
}


/*\reimp
*/
void TQtMultiLineEdit::mouseDoubleClickEvent( TQMouseEvent *m )
{
    if ( m->button() == LeftButton ) {
	if ( m->state() & ShiftButton ) {
	    int newX = cursorX;
	    int newY = cursorY;
	    extendSelectionWord( newX, newY);
	    newMark( newX, newY, FALSE );
	} else {
	    markWord( cursorX, cursorY );
	}
	dragMarking    = TRUE;
	wordMark = TRUE;
	updateCell( cursorY, 0, FALSE );

    }
}

#ifndef TQT_NO_DRAGANDDROP

/*
  \reimp
*/

void TQtMultiLineEdit::dragEnterEvent( TQDragEnterEvent * )
{
    cursorOn = TRUE;
    updateCell( cursorY, 0, FALSE );
}

/*\reimp
*/
void TQtMultiLineEdit::dragMoveEvent( TQDragMoveEvent* event )
{
    if ( readOnly ) return;
    event->accept( TQTextDrag::canDecode(event) );
    d->dnd_forcecursor = TRUE;
    setCursorPixelPosition(event->pos(), FALSE);
    d->dnd_forcecursor = FALSE;
    TQRect inside_margin(scroll_margin, scroll_margin,
			width()-scroll_margin*2, height()-scroll_margin*2);
    if ( !inside_margin.contains(event->pos()) ) {
	startAutoScroll();
    }
    if ( event->source() == this && event->action() == TQDropEvent::Move )
	event->acceptAction();
}

/*\reimp
*/
void TQtMultiLineEdit::dragLeaveEvent( TQDragLeaveEvent* )
{
    if ( cursorOn ) {
	cursorOn = FALSE;
	updateCell( cursorY, 0, FALSE );
    }
    stopAutoScroll();
}

/*\reimp
*/
void TQtMultiLineEdit::dropEvent( TQDropEvent* event )
{
    if ( readOnly ) return;
    TQString text;
    TQCString fmt = pickSpecial(event,FALSE,event->pos());
    if ( TQTextDrag::decode(event, text, fmt) ) {
	int i = -1;
	while ( ( i = text.find( '\r' ) ) != -1 )
	    text.replace( i,1,"" );
	if ( event->source() == this && event->action() == TQDropEvent::Move ) {
	    event->acceptAction();
	    // Careful not to tread on my own feet
	    int newX, newY;
	    pixelPosToCursorPos( event->pos(), &newX, &newY );
	    if ( afterMark( newX, newY ) ) {
		// The tricky case
		int x1, y1, x2, y2;
		getMarkedRegion( &y1, &x1, &y2, &x2 );
		if ( newY == y2 ) {
		    newY = y1;
		    newX = x1 + newX - x2;
		} else {
		    newY -= y2 - y1;
		}
		addUndoCmd( new TQBeginCommand );
		del();
		setCursorPosition(newY, newX);
		insert(text, TRUE);
		addUndoCmd( new TQEndCommand );
	    } else if ( beforeMark( newX, newY ) ) {
		// Easy
		addUndoCmd( new TQBeginCommand );
		del();
		setCursorPosition(newY, newX);
		insert(text, TRUE);
		addUndoCmd( new TQEndCommand );
	    } else {
		// Do nothing.
	    }
	} else {
	    setCursorPixelPosition(event->pos());
	    insert(text, TRUE);
	}
	update();
	emit textChanged();
    }
}

#endif // TQT_NO_DRAGANDDROP


/*
  Returns TRUE if line \a line is invisible or partially invisible.
*/

bool TQtMultiLineEdit::partiallyInvisible( int line )
{
    int y;
    if ( !rowYPos( line, &y ) )
	return TRUE;
    if ( y < 0 )
	return TRUE;
    else if ( y + cellHeight() - 2 > viewHeight() )
	return TRUE;

    return FALSE;
}

/*
  Scrolls such that the cursor is visible
*/

void TQtMultiLineEdit::makeVisible()
{
    if ( !autoUpdate() )
	return;

    if ( partiallyInvisible( cursorY ) ) {
	if ( cursorY >= lastRowVisible() )
	    setBottomCell( cursorY );
	else
	    setTopCell( cursorY );
    }
    int xPos = mapToView( cursorX, cursorY );
    if ( xPos < xOffset() ) {
	int of = xPos - 10; //###
	setXOffset( of );
    } else if ( xPos > xOffset() + viewWidth() ) {
	int of = xPos - viewWidth() + 10; //###
	setXOffset( of );
    }
}

/*
  Computes the character position in line \a line which corresponds
  to pixel \a xPos
*/

int TQtMultiLineEdit::mapFromView( int xPos, int line )
{
    TQString s = stringShown( line );
    if ( !s )
	return 0;
    TQFontMetrics fm( font() );
    int index = xPosToCursorPos( s, fm,
				 xPos - d->lr_marg,
				 cellWidth() - 2 * d->lr_marg - d->marg_extra,
				 d->align );
    TQtMultiLineEditRow* r = contents->at( line );
    if (r && !r->newline && ((unsigned int)index == r->s.length()) && (index > 0))
	--index;
    return index;
}

/*
  Computes the pixel position in line \a line which corresponds to
  character position \a xIndex
*/

int TQtMultiLineEdit::mapToView( int xIndex, int line )
{
    TQString s = stringShown( line );
    xIndex = TQMIN( (int)s.length(), xIndex );
    TQFontMetrics fm( font() );
    int wcell = cellWidth() - 2 * d->lr_marg;// - d->marg_extra;
    int wrow = contents->at( line )->w;
    int w = textWidthWithTabs( fm, s, 0, xIndex, d->align ) - 1;
    if ( d->align == TQt::AlignCenter || d->align == TQt::AlignHCenter )
	w += (wcell - wrow) / 2;
    else if ( d->align == TQt::AlignRight )
	w += wcell - wrow;
    return d->lr_marg + w;
}

/*
  Traverses the list and finds an item with the maximum width, and
  updates the internal list box structures accordingly.
*/

void TQtMultiLineEdit::updateCellWidth()
{
    TQtMultiLineEditRow* r = contents->first();
    int maxW = 0;
    int w;
    switch ( d->echomode ) {
    case Normal:
	while ( r ) {
	    w = r->w;
	    if ( w > maxW )
		maxW = w;
	    r = contents->next();
	}
	break;
    case Password: {
	uint l = 0;
	while ( r ) {
	    l = TQMAX(l,  r->s.length() );
	    r = contents->next();
	}
	TQString t;
	t.fill(TQChar('*'), l);
	maxW = textWidth(t);
    }
    break;
    case NoEcho:
	maxW = textWidth(TQString::tqfromLatin1(""));
    }
    setWidth( maxW );
}


/*
  Sets the bottommost visible line to \a line.
*/

void TQtMultiLineEdit::setBottomCell( int line )
{
    int rowY = cellHeight() * line;
    int newYPos = rowY +  cellHeight() - viewHeight();
    setYOffset( TQMAX( newYPos, 0 ) );
}

#ifndef TQT_NO_CLIPBOARD

/*
  Copies text in MIME subtype \a subtype from the clipboard onto the current
  cursor position.
  Any marked text is first deleted.
*/
void TQtMultiLineEdit::pasteSubType(const TQCString& subtype)
{
    TQCString st = subtype;
    addUndoCmd( new TQBeginCommand );

    if ( hasMarkedText() )
	del();
    TQString t = TQApplication::clipboard()->text(st);
    if ( !t.isEmpty() ) {
	if ( hasMarkedText() )
	    turnMark( FALSE );

#if defined(_OS_WIN32_)
	// Need to convert CRLF to NL
	t.replace( "\r\n", "\n" );
#endif

	for (int i=0; (uint)i<t.length(); i++) {
	    if ( t[i] < ' ' && t[i] != '\n' && t[i] != '\t' )
		t[i] = ' ';
	}
	insertAt( t, cursorY, cursorX );
	turnMark( FALSE );
	curXPos  = 0;
	makeVisible();
    }
    if ( textDirty && !d->isHandlingEvent )
	emit textChanged();

    addUndoCmd( new TQEndCommand );
}

/*
  Copies plain text from the clipboard onto the current cursor position.
  Any marked text is first deleted.
*/
void TQtMultiLineEdit::paste()
{
    pasteSubType("plain");
}

#ifndef TQT_NO_MIMECLIPBOARD
/*
  Prompts the user for a type from a list of text types available,
  Then copies text from the clipboard onto the current cursor position.
  Any marked text is first deleted.
*/
void TQtMultiLineEdit::pasteSpecial(const TQPoint& pt)
{
    TQCString st = pickSpecial(TQApplication::clipboard()->data(),TRUE,pt);
    if ( !st.isEmpty() )
	pasteSubType(st);
}
#endif
#ifndef TQT_NO_MIME
TQCString TQtMultiLineEdit::pickSpecial(TQMimeSource* ms, bool always_ask, const TQPoint& pt)
{
    if ( ms )  {
	TQPopupMenu popup(this);
	TQString fmt;
	int n=0;
	TQDict<void> done;
	for (int i=0; !(fmt=ms->format(i)).isNull(); i++) {
	    int semi=fmt.find(";");
	    if ( semi >= 0 )
		fmt = fmt.left(semi);
	    if ( fmt.left(5) == "text/" ) {
		fmt = fmt.mid(5);
		if ( !done.find(fmt) ) {
		    done.insert(fmt,(void*)1);
		    popup.insertItem(fmt,i);
		    n++;
		}
	    }
	}
	if ( n ) {
	    int i = n==1 && !always_ask ? popup.idAt(0) : popup.exec(pt);
	    if ( i >= 0 )
		return popup.text(i).latin1();
	}
    }
    return TQCString();
}
#endif // TQT_NO_MIME
#endif // TQT_NO_CLIPBOARD


/*
  Removes all text.
*/

void TQtMultiLineEdit::clear()
{
    addUndoCmd( new TQDelTextCmd( 0, text() ) );
    setEdited( TRUE );
    contents->clear();
    cursorX = cursorY = 0;
    int w  = textWidth( TQString::tqfromLatin1("") );
    contents->append( new TQtMultiLineEditRow(TQString::tqfromLatin1(""), w) );
    setNumRowsAndTruncate();
    setWidth( w );
    dummy = TRUE;
    turnMark( FALSE );
    if ( autoUpdate() )
	update();
    if ( !d->isHandlingEvent ) //# && not already empty
	emit textChanged();
    update();
}


/*\reimp
*/

void TQtMultiLineEdit::setFont( const TQFont &font )
{
    TQWidget::setFont( font );
    d->clearChartable();
    TQFontMetrics fm( font );
    setCellHeight( fm.lineSpacing() );
    for ( TQtMultiLineEditRow* r = contents->first(); r; r = contents->next() )
	r->w = textWidth( r->s  );
    rebreakAll();
    updateCellWidth();
}

/*
  Sets a new marked text limit, does not tqrepaint the widget.
*/

void TQtMultiLineEdit::newMark( int posx, int posy, bool /*copy*/ )
{
    if ( markIsOn && markDragX == posx && markDragY == posy &&
	 cursorX   == posx && cursorY   == posy )
	return;
    markDragX  = posx;
    markDragY  = posy;
    cursorX    = posx;
    cursorY    = posy;
    turnMark( markDragX != markAnchorX || markDragY != markAnchorY );
}

bool TQtMultiLineEdit::beforeMark( int posx, int posy ) const
{
    int x1, y1, x2, y2;
    if ( !getMarkedRegion( &y1, &x1, &y2, &x2 ) )
	return FALSE;
    return
	(y1 > posy || y1 == posy && x1 > posx)
	&& (y2 > posy || y2 == posy && x2 > posx);
}

bool TQtMultiLineEdit::afterMark( int posx, int posy ) const
{
    int x1, y1, x2, y2;
    if ( !getMarkedRegion( &y1, &x1, &y2, &x2 ) )
	return FALSE;
    return
	(y1 < posy || y1 == posy && x1 < posx)
	&& (y2 < posy || y2 == posy && x2 < posx);
}

bool TQtMultiLineEdit::inMark( int posx, int posy ) const
{
    int x1, y1, x2, y2;
    if ( !getMarkedRegion( &y1, &x1, &y2, &x2 ) )
	return FALSE;
    return
	(y1 < posy || y1 == posy && x1 <= posx)
	&& (y2 > posy || y2 == posy && x2 >= posx);
}

/*
  Marks the word at character position \a posx, \a posy.
 */
void TQtMultiLineEdit::markWord( int posx, int posy )
{
    TQString& s = contents->at( posy )->s;

    int i = posx - 1;
    while ( i >= 0 && s[i].isPrint() && !s[i].isSpace() )
	i--;
    i++;
    markAnchorY = posy;
    markAnchorX = i;

    while ( s[i].isPrint() && !s[i].isSpace() )
	i++;
    markDragX = i;
    markDragY = posy;
    turnMark( markDragX != markAnchorX || markDragY != markAnchorY );

#ifndef TQT_NO_CLIPBOARD
#if defined(_WS_X11_)
    if ( echoMode() == Normal )
	copy();
#endif
#endif
}

/*
  This may become a protected virtual member in a future TQt.
  This implementation is an example of a useful classification
  that aids selection of common units like filenames and URLs.
*/
int TQtMultiLineEdit::charClass( TQChar ch )
{
    if ( !ch.isPrint() || ch.isSpace() ) return 1;
    else if ( ch.isLetter() || ch=='-' || ch=='+' || ch==':'
	      || ch=='.' || ch=='/' || ch=='\\'
	      || ch=='@' || ch=='$' || ch=='~' ) return 2;
    else return 3;
}

#ifndef TQT_NO_CLIPBOARD
/*
  Copies the marked text to the clipboard.  Will copy only
  if echoMode() is Normal.
*/

void TQtMultiLineEdit::copy() const
{
    TQString t = markedText();
    if ( !t.isEmpty() && echoMode() == Normal ) {
#if defined(_WS_X11_)
	disconnect( TQApplication::clipboard(), TQT_SIGNAL(dataChanged()), this, 0);
#endif
#if defined(_OS_WIN32_)
	// Need to convert NL to CRLF
	t.replace( "\n", "\r\n" );
#endif
	TQApplication::clipboard()->setText( t );
#if defined(_WS_X11_)
	connect( TQApplication::clipboard(), TQT_SIGNAL(dataChanged()),
		 this, TQT_SLOT(clipboardChanged()) );
#endif
    }
}

/* \obsolete

  Backward compatibility.
*/
void TQtMultiLineEdit::copyText() const
{
    copy();
}

/*
  Copies the selected text to the clipboard and deletes the selected text.
*/

void TQtMultiLineEdit::cut()
{
    if ( hasMarkedText() ) {
	if ( echoMode() == Normal )
	    copy();
	del();
	if ( textDirty && !d->isHandlingEvent )
	    emit textChanged();
    }
}

#endif

/*
  This private slot is activated when this line edit owns the clipboard and
  some other widget/application takes over the clipboard. (X11 only)
*/

void TQtMultiLineEdit::clipboardChanged()
{
#if defined(_WS_X11_)
    disconnect( TQApplication::clipboard(), TQT_SIGNAL(dataChanged()),
		this, TQT_SLOT(clipboardChanged()) );
    turnMark( FALSE );
    update();
#endif
}


/*
   Sets maxLineWidth() and maybe cellWidth() to \a w without updating the entire widget.
 */

void TQtMultiLineEdit::setWidth( int w )
{
    if ( w ==d->maxLineWidth )
	return;
    bool u = autoUpdate();
    setAutoUpdate( FALSE );
    d->maxLineWidth = w;
    if ( d->align == AlignLeft )
	setCellWidth( w );
    else
	setCellWidth( TQMAX( w, contentsRect().width() ) );
    setAutoUpdate( u );
    if ( autoUpdate() && d->align != AlignLeft )
	update();
}


/*
  Sets the cursor position to character number \a col in line number \a line.
  The parameters are adjusted to lie within the legal range.

  If \a mark is FALSE, the selection is cleared. otherwise it is extended

  \sa cursorPosition()
*/

void TQtMultiLineEdit::setCursorPosition( int line, int col, bool mark )
{
    if ( mark && !hasMarkedText() ) {
	markAnchorX    = cursorX;
	markAnchorY    = cursorY;
    }
    int oldY = cursorY;
    cursorY = TQMAX( TQMIN( line, numLines() - 1), 0 );
    cursorX = TQMAX( TQMIN( col,  lineLength( cursorY )), 0 );
    curXPos = 0;
    makeVisible();
    if ( mark ) {
	newMark( cursorX, cursorY, FALSE );
	for ( int i = TQMIN(oldY,cursorY); i <= TQMAX(oldY,cursorY); i++ )
	    updateCell( i, 0, FALSE );
    } else {
	updateCell( oldY, 0, FALSE );
	turnMark( FALSE );
    }
}



/* \obsolete

  Use getCursorPosition() instead.
*/

void TQtMultiLineEdit::cursorPosition( int *line, int *col ) const
{
    getCursorPosition(line,col);
}


/*
  Returns the current line and character
  position within that line, in the variables pointed to
  by \a line and \a col respectively.

  \sa setCursorPosition()
*/

void TQtMultiLineEdit::getCursorPosition( int *line, int *col ) const
{
    if ( line )
	*line = cursorY;
    if ( col )
	*col = cursorX;
}


/*
  \sa setAutoUpdate()
*/

bool TQtMultiLineEdit::autoUpdate() const
{
    return TQtTableView::autoUpdate();
}


/*
  Sets the auto-update option of multi-line editor to \a enable.

*/

void TQtMultiLineEdit::setAutoUpdate( bool enable )
{
    TQtTableView::setAutoUpdate( enable );
}

/*
  Sets the fixed height of the TQtMultiLineEdit so that \a lines text lines
  are visible given the current font.

  \sa setMaxLines(), setFixedHeight()
 */
void TQtMultiLineEdit::setFixedVisibleLines( int lines )
{
    int ls = fontMetrics().lineSpacing();
    setFixedHeight( frameWidth()*2 + ls*lines );
    return;
}



/*
  Returns the top center point where the cursor is drawn
*/

TQPoint TQtMultiLineEdit::cursorPoint() const
{
    TQPoint cp( 0, 0 );

    TQFontMetrics fm( font() );
    int col, row;
    col = row = 0;
    cursorPosition( &row, &col );
    TQString line = textLine( row );
    ASSERT( line );
    cp.setX( d->lr_marg + textWidthWithTabs( fm, line, 0, col, d->align ) - 1 );
    cp.setY( (row * cellHeight()) + viewRect().y() );
    return cp;
}


/* \reimp
*/
TQSizePolicy TQtMultiLineEdit::sizePolicy() const
{
    //### removeme 3.0
    return TQWidget::sizePolicy();
}


/*\reimp
*/
TQSize TQtMultiLineEdit::tqsizeHint() const
{
    constPolish();
    int expected_lines;
    if ( d->maxlines >= 0 && d->maxlines <= 6 ) {
	expected_lines = d->maxlines;
    } else {
	expected_lines = 6;
    }
    TQFontMetrics fm( font() );
    int h = fm.lineSpacing()*(expected_lines-1)+fm.height() + frameWidth()*2;
    int w = fm.width('x')*35;

    int maxh = tqmaximumSize().height();
    if ( maxh < TQWIDGETSIZE_MAX )
	h = maxh;

    return TQSize( w, h ).expandedTo( TQApplication::globalStrut() );
}


/*
  Returns a size sufficient for one character, and scroll bars.
*/

TQSize TQtMultiLineEdit::tqminimumSizeHint() const
{
    constPolish();
    TQFontMetrics fm( font() );
    int h = fm.lineSpacing() + frameWidth()*2;
    int w = fm.maxWidth();
    h += frameWidth();
    w += frameWidth();
    if ( testTableFlags(Tbl_hScrollBar|Tbl_autoHScrollBar) )
	w += verticalScrollBar()->tqsizeHint().width();
    if ( testTableFlags(Tbl_vScrollBar|Tbl_autoVScrollBar) )
	h += horizontalScrollBar()->tqsizeHint().height();
    return TQSize(w,h);
}



/*\reimp
*/

void TQtMultiLineEdit::resizeEvent( TQResizeEvent *e )
{
    int oldw = contentsRect().width();
    TQtTableView::resizeEvent( e );
    if ( DYNAMIC_WRAP
	 && (e->oldSize().width() != width()
	     || oldw != contentsRect().width() ) ) {
	bool oldAuto = autoUpdate();
	setAutoUpdate( FALSE );
	rebreakAll();
	if ( oldw != contentsRect().width() )
	    rebreakAll();
	setAutoUpdate( oldAuto );
	if ( autoUpdate() )
	    tqrepaint( FALSE );
    } else if ( d->align != AlignLeft ) {
 	d->maxLineWidth = 0; // trigger update
	updateCellWidth();
    }
    if ( isVisible() )
	deselect();
}

/*
  Sets the tqalignment to \a flags. Possible values are \c AlignLeft, \c
  Align(H)Center and \c AlignRight.

  \sa tqalignment(), TQt::AlignmentFlags
*/
void TQtMultiLineEdit::tqsetAlignment( int flags )
{
    if ( d->align != flags ) {
	d->align = flags;
	update();
    }
}

/*
  Returns the tqalignment.

*/
int TQtMultiLineEdit::tqalignment() const
{
    return d->align;
}


/*
  Not supported at this time.
  \a v is the validator to set.
*/
void TQtMultiLineEdit::setValidator( const TQValidator *v )
{
    d->val = v;
    // #### validate text now
}

/*
  Not supported at this time.
*/
const TQValidator * TQtMultiLineEdit::validator() const
{
    return d->val;
}

/*  \sa edited()
*/
void TQtMultiLineEdit::setEdited( bool e )
{
    d->edited = e;
}

/*  \sa setEdited()
*/
bool TQtMultiLineEdit::edited() const
{
    return d->edited;
}

/* \enum TQtMultiLineEdit::EchoMode

  This enum type describes the ways in which TQLineEdit can display its
  contents.  The currently defined values are: \list

  \i Normal - display characters as they are entered.  This is
  the default.

  \i NoEcho - do not display anything.

  \i Password - display asterisks instead of the characters
  actually entered.

  \endlist

  \sa setEchoMode() echoMode() TQLineEdit::EchoMode
*/


/*
  Sets the echo mode to \a em.  The default is \c Normal.

  The display is updated according.

  \sa setEchoMode()
*/
void TQtMultiLineEdit::setEchoMode( EchoMode em )
{
    if ( d->echomode != em ) {
	d->echomode = em;
	updateCellWidth();
	update();
    }
}

/*
  Returns the currently set echo mode.

  \sa setEchoMode()
*/
TQtMultiLineEdit::EchoMode TQtMultiLineEdit::echoMode() const
{
    return d->echomode;
}


/*
  Returns the string shown at line \a row, including
  processing of the echoMode().
*/

TQString TQtMultiLineEdit::stringShown(int row) const
{
    TQString* s = getString(row);
    if ( !s ) return TQString::null;
    switch ( d->echomode ) {
    case Normal:
	if (!*s) return TQString::tqfromLatin1("");
	return *s;
    case Password:
	{
	    TQString r;
	    r.fill(TQChar('*'), (int)s->length());
	    if ( !r ) r = TQString::tqfromLatin1("");
	    return r;
	}
    case NoEcho:
	return TQString::tqfromLatin1("");
    }
    return TQString::tqfromLatin1("");
}

/*

  \sa maxLength()
*/
void TQtMultiLineEdit::setMaxLength(int m)
{
    d->maxlen = m;
}

/*
  \sa setMaxLength()
*/
int TQtMultiLineEdit::maxLength() const
{
    return d->maxlen;
}


/*
  Returns the length of the current text.

  \sa setMaxLength()
 */
int TQtMultiLineEdit::length() const
{
    int l = 0;
    for ( TQtMultiLineEditRow* r = contents->first(); r; r = contents->next() ) {
	l += r->s.length();
	if ( r->newline )
	    ++l;
    }
    return l-1;
}


/*
  Sets the maximum length of lines to \a m.  Use -1 for unlimited
  (the default).  Existing long lines will be truncated.

  \sa maxLineLength()
*/
void TQtMultiLineEdit::setMaxLineLength(int m)
{
    bool trunc = d->maxlinelen < 0 || m < d->maxlinelen;
    d->maxlinelen = m;
    if ( trunc ) {
	TQtMultiLineEditRow* r = contents->first();
	while ( r ) {
	    r->s.truncate( m );
	    r = contents->next();
	}
	if ( cursorX > m ) cursorX = m;
	if ( markAnchorX > m ) markAnchorX = m;
	if ( markDragX > m ) markDragX = m;
	update();
	updateCellWidth();
    }
}

/*
  Returns the currently set line length limit, or -1 if there is
  no limit (this is the default).

  \sa setMaxLineLength()
*/
int TQtMultiLineEdit::maxLineLength() const
{
    return d->maxlinelen;
}

/*
  Sets the maximum number of lines to \a m.  Use -1 for unlimited
  (the default).  Existing excess lines will be deleted.

  \sa maxLines(), numLines()
*/
void TQtMultiLineEdit::setMaxLines(int m)
{
    if ( m == 0 ) // bad value
	m = -1;
    d->maxlines = m;
    if ( d->maxlines >= 0 && d->maxlines <= 6 ) {
	tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Fixed ) );
    } else {
	tqsetSizePolicy( TQSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::Expanding ) );
    }
    if ( setNumRowsAndTruncate() ) {
	updateCellWidth();
	update();
    }
}

/*
  \sa setMaxLines()
*/
int TQtMultiLineEdit::maxLines() const
{
    return d->maxlines;
}

/*
  Sets the horizontal margin to \a m.

  \sa hMargin()
*/
void TQtMultiLineEdit::setHMargin(int m)
{
    if ( m != d->lr_marg ) {
	d->lr_marg = m;
	updateCellWidth();
	update();
    }
}

/*

  \sa setHMargin()
*/
int TQtMultiLineEdit::hMargin() const
{
    return d->lr_marg;
}

/*
  Marks the text starting at \a row_from, \a col_from and ending
  at \a row_to, \a col_to.
*/
void TQtMultiLineEdit::setSelection( int row_from, int col_from,
				   int row_to, int col_to )
{
    setCursorPosition( row_from, col_from, FALSE );
    setCursorPosition( row_to, col_to, TRUE );
}


/*
  Moves the cursor one word to the right.  If \a mark is TRUE, the text
  is marked.
  \sa cursorWordBackward()
*/
void TQtMultiLineEdit::cursorWordForward( bool mark )
{
    int x = cursorX;
    int y = cursorY;

    if ( x == lineLength( y ) || textLine(y).at(x).isSpace() ) {
	while ( x < lineLength( y ) && textLine(y).at(x).isSpace() )
	    ++x;
	if ( x == lineLength( y ) ) {
	    if ( y < (int)contents->count() - 1) {
		++y;
		x = 0;
		while ( x < lineLength( y ) && textLine(y).at(x).isSpace() )
		    ++x;
	    }
	}
    }
    else {
	while ( x < lineLength( y ) && !textLine(y).at(x).isSpace() )
	    ++x;
	int xspace = x;
	while ( xspace < lineLength( y ) && textLine(y).at(xspace).isSpace() )
	    ++xspace;
	if ( xspace <  lineLength( y ) )
	    x = xspace;
    }
    cursorOn = TRUE;
    int oldY = cursorY;
    setCursorPosition( y, x, mark );
    if ( oldY != cursorY )
	updateCell( oldY, 0, FALSE );
    updateCell( cursorY, 0, FALSE );
    d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
}

/*
  Moves the cursor one word to the left.  If \a mark is TRUE, the text
  is marked.
  \sa cursorWordForward()
*/
void TQtMultiLineEdit::cursorWordBackward( bool mark )
{
    int x = cursorX;
    int y = cursorY;

    while ( x > 0 && textLine(y).at(x-1).isSpace() )
	--x;

    if ( x == 0 ) {
	if ( y > 0 ) {
	    --y;
	    x = lineLength( y );
	    while ( x > 0  && textLine(y).at(x-1).isSpace() )
		--x;
	}
    }
    else {
	while ( x > 0  && !textLine(y).at(x-1).isSpace() )
	    --x;
    }
    cursorOn = TRUE;
    int oldY = cursorY;
    setCursorPosition( y, x, mark );
    if ( oldY != cursorY )
	updateCell( oldY, 0, FALSE );
    updateCell( cursorY, 0, FALSE );
    d->blinkTimer->start( TQApplication::cursorFlashTime() / 2, FALSE );
}

#define DO_BREAK {doBreak = TRUE; if ( lastSpace > a ) { \
i = lastSpace; \
linew = lastw; \
} \
else \
i = TQMAX( a, i-1 );}

void TQtMultiLineEdit::wrapLine( int line, int removed )
{
    TQtMultiLineEditRow* r = contents->at( line );
    int yPos;
    (void) rowYPos( line, &yPos );
    TQFontMetrics fm( font() );
    int i  = 0;
    TQString s = r->s;
    int a = 0;
    int l = line;
    int w = 0;
    int nlines = 0;
    int lastSpace = -1;
    bool doBreak = FALSE;
    int linew = 0;
    int lastw = 0;
    int tabDist = -1; // lazy eval
    while ( i < int(s.length()) ) {
	doBreak = FALSE;
	if ( s[i] == '\t' && d->align == TQt::AlignLeft ) {
	    if ( tabDist<0 )
		tabDist = tabStopDist(fm);
	    linew = ( linew/tabDist + 1 ) * tabDist;
	} else if ( s[i] != '\n' ) {
	    char c = s[i].latin1();
	    if ( c > 0 ) {
		if ( !d->chartable[c] )
		    d->chartable[c] = fm.width( s[i] );
		linew += d->chartable[c];
	    } else {
		linew += fm.width( s[i] );
	    }
	}
	if ( WORD_WRAP &&
	     lastSpace >= a && s[i] != '\n' ) {
	    if ( DYNAMIC_WRAP ) {
		if  (linew >= contentsRect().width() -  2*d->lr_marg - d->marg_extra) {
		    DO_BREAK
		}
	    } else if ( FIXED_COLUMN_WRAP ) {
		if ( d->wrapcol >= 0 && i-a >= d->wrapcol ) {
		    DO_BREAK
		}
	    } else if ( FIXED_WIDTH_WRAP ) {
		if ( d->wrapcol >= 0 && linew > d->wrapcol - d->marg_extra ) {
		    DO_BREAK
		}
	    }
	}
	if ( s[i] == '\n' || doBreak ) {
	    r->s = s.mid( a, i - a + (doBreak?1:0) );
	    r->w = linew - fm.leftBearing(r->s[0]) + 2 * d->lr_marg + d->marg_extra;
	    if ( r->w > w )
		w = r->w;
	    if ( cursorY > l )
		++cursorY;
	    else if ( cursorY == line && cursorX >=a && cursorX <= i +  (doBreak?1:0)) {
		cursorY = l;
		cursorX -= a;
	    }
	    if ( markAnchorY > l )
		++markAnchorY;
	    else if ( markAnchorY == line && markAnchorX >=a && markAnchorX <= i +  (doBreak?1:0)) {
		markAnchorY = l;
		markAnchorX -= a;
	    }
	    a = i + 1;
	    lastSpace = a;
	    linew = 0;
	    bool oldnewline = r->newline;
	    r->newline = !doBreak;
	    r = new TQtMultiLineEditRow( TQString::null, 0, oldnewline );
	    ++nlines;
	    contents->insert( l + 1, r );
	    ++l;
	}
	if ( s[i].isSpace() || BREAK_WITHIN_WORDS ) {
	    lastSpace = i;
	    lastw = linew;
	}
	if ( lastSpace <= a )
	    lastw = linew;

	++i;
    }
    if ( a < int(s.length()) ){
	r->s = s.mid( a, i - a  );
	r->w = linew - fm.leftBearing( r->s[0] ) + 2 * d->lr_marg + d->marg_extra;
    }
    if ( cursorY == line && cursorX >= a ) {
	cursorY = l;
	cursorX -= a;
    }
    if ( markAnchorY == line && markAnchorX >= a ) {
	markAnchorY = l;
	markAnchorX -= a;
    }
    if ( r->w > w )
	w = r->w;

    setWidth( TQMAX( maxLineWidth(), w ) );
    bool oldAuto = autoUpdate();
    setAutoUpdate( FALSE );
    (void)setNumRowsAndTruncate();
    setAutoUpdate( oldAuto );

    yPos += (nlines+1)  * cellHeight();
    int sh = (nlines-removed)  * cellHeight();
    if ( autoUpdate() ) {
	if ( sh && yPos >= contentsRect().top() && yPos < contentsRect().bottom() ) {
	    int h = contentsRect().bottom() - yPos + 1;
	    if ( d->maxlines >= 0 ) {
		int maxy;
		if ( rowYPos( d->maxlines-1, &maxy ) ) {
		    maxy += cellHeight();
		    if ( maxy < contentsRect().bottom() && maxy > yPos )
			h = maxy - yPos + 1;
		}
	    }
	    TQWidget::scroll( 0, sh, TQRect( contentsRect().left(), yPos,
					   contentsRect().width(),
					   h ) );
	}
	for (int ul = 0; ul <= nlines; ++ul )
	    updateCell( line + ul, 0, FALSE );
    }
}

void TQtMultiLineEdit::rebreakParagraph( int line, int removed )
{
    TQtMultiLineEditRow* r = contents->at( line );
    if ( WORD_WRAP ) {
	TQtMultiLineEditRow* other = 0;
	while (line < int(contents->count())-1 && !r->newline ) {
	    other = contents->at( line + 1 );
	    if ( cursorY > line ) {
		--cursorY;
		if ( cursorY == line ) {
		    cursorX += r->s.length();
		}
	    }
	    if ( markAnchorY > line ) {
		--markAnchorY;
		if ( markAnchorY == line ) {
		    markAnchorX += r->s.length();
		}
	    }
	    r->s.append( other->s );
	    r->newline = other->newline;
	    contents->remove( line + 1 );
	    ++removed;
	}
    }
    wrapLine( line, removed );
}

void TQtMultiLineEdit::rebreakAll()
{
    if ( !WORD_WRAP )
	return;
    d->maxLineWidth = 0;
    for (int i = 0; i < int(contents->count()); ++i ) {
	if ( contents->at( i )->newline &&
	     contents->at( i )->w < contentsRect().width() -  2*d->lr_marg - d->marg_extra ) {
	    setWidth( TQMAX( d->maxLineWidth, contents->at( i )->w ) );
	    continue;
	}
	rebreakParagraph( i );
	while ( i < int(contents->count() )
		&& !contents->at( i )->newline )
	    ++i;
    }
}


/* \enum TQtMultiLineEdit::WordWrap

  This enum describes the multiline edit's word wrap mode.

  The following values are valid:
    \list
    \i NoWrap - no word wrap at all.
    \i WidgetWidth - word wrap depending on the current
     width of the editor widget
    \i FixedPixelWidth - wrap according to a fix amount
     of pixels ( see wrapColumnOrWidth() )
    \i FixedColumnWidth - wrap according to a fix character
     column. This is useful whenever you need formatted text that
     can also be displayed gracefully on tqdevices with monospaced
     fonts, for example a standard VT100 terminal. In that case
     wrapColumnOrWidth() should typically be set to 80.
  \endlist

 \sa setWordWrap()
*/

/*
  Sets the word wrap mode to \a mode.

 */
void TQtMultiLineEdit::setWordWrap( WordWrap mode )
{
    if ( mode == d->wordwrap )
	return;
    d->wordwrap = mode;

    if ( BREAK_WITHIN_WORDS  ) {
	d->arrow = TQPixmap( (const char **)arrow_xpm );
	d->marg_extra = 8;
	if ( DYNAMIC_WRAP )
	    clearTableFlags( Tbl_autoHScrollBar );
        else
            setTableFlags( Tbl_autoHScrollBar );
    } else {
	d->marg_extra = 0;
	setTableFlags( Tbl_autoHScrollBar );
    }
    if ( !text().isEmpty() )
        setText( text() );
}

/*
  Returns the current word wrap mode.

  \sa setWordWrap()
 */
TQtMultiLineEdit::WordWrap TQtMultiLineEdit::wordWrap() const
{
    return d->wordwrap;
}

/*
  Sets the wrap column or wrap width to \a value, depending on the
  word wrap mode.

  \sa setWordWrap()
 */
void TQtMultiLineEdit::setWrapColumnOrWidth( int value )
{
    if ( value == d->wrapcol )
	return;
    d->wrapcol = value;
    if ( wordWrap() != NoWrap )
	setText( text() );
}

/*
 */
int TQtMultiLineEdit::wrapColumnOrWidth() const
{
    return d->wrapcol;
}


/* \enum TQtMultiLineEdit::WrapPolicy

  Defines where text can be wrapped in word wrap mode.

  The following values are valid:
  \list
  \i AtWhiteSpace - break only after whitespace
  \i Anywhere - break anywhere
   \endlist

   \sa setWrapPolicy()
*/

/*
  Sets the wrap \a policy, i.e. where text can be wrapped in word wrap
  mode.

  \sa setWordWrap(), wrapPolicy()
 */
void TQtMultiLineEdit::setWrapPolicy( WrapPolicy policy )
{
    if ( d->wrappolicy == policy )
	return;
    d->wrappolicy = policy;
    WordWrap m = d->wordwrap;
    if ( m != NoWrap ) { // trigger update
	d->wordwrap = NoWrap;
	setWordWrap( m );
    }
}

/*

  Returns the current word wrap policy.

  \sa setWrapPolicy()
 */
TQtMultiLineEdit::WrapPolicy TQtMultiLineEdit::wrapPolicy() const
{
    return d->wrappolicy;
}

/*
  Returns wether \a row is the last row in a paragraph.

  This function is only interesting in word wrap mode, otherwise its
  return value is always TRUE.

  \sa setWordWrap()
 */
bool TQtMultiLineEdit::isEndOfParagraph( int row ) const
{
    return contents->at( row )->newline;
}

int TQtMultiLineEdit::positionToOffsetInternal( int row, int col ) const
{
    row = TQMAX( TQMIN( row, numLines() - 1), 0 ); // Sanity check
    col = TQMAX( TQMIN( col,  lineLength( row )), 0 ); // Sanity check
    if ( row == 0 )
	return TQMIN( col, lineLength( 0 ));
    else {
	int lastI;
	lastI  = lineLength( row );
	int i, tmp = 0;

	for( i = 0; i < row ; i++ ) {
	    tmp += lineLength( i );
	    if ( contents->at( i )->newline )
		++tmp;
	}

	tmp += TQMIN( lastI, col );

	return tmp;
    }

}

// if position is <0 = returns row 0, col 0, if position >= amount of text
// returns pointer to end of text.
void TQtMultiLineEdit::offsetToPositionInternal( int position,
				       int *row, int *col ) const
{
    if (position <= 0) {
	*row = 0;
	*col = 0;
	return;
    }
    else {
	int charsLeft = position;
	int i;

	for( i = 0; contents->at( i ); ++i ) {
	    if (lineLength( i ) < charsLeft)
		charsLeft -= lineLength( i );
	    else {
		*row = i;
		*col = charsLeft;
		return;
	    }
	    if ( contents->at( i )->newline )
		--charsLeft;
	}

	if (contents->at( i - 1) && !contents->at( i - 1 )->newline) {
	    *row = i - 1;
	    *col = lineLength( i - 1 );
	}
	else {
	    *row = i;
	    *col = 0;
	}
	return;
    }
}


/*
  Processes an undo/redo command \a cmd, depending on \a undo.
 */
void TQtMultiLineEdit::processCmd( TQtMultiLineEditCommand* cmd, bool undo)
{
    TQDelTextCmd* delcmd = (TQDelTextCmd*) cmd;
    bool ins = TRUE;
    if (cmd->type() == TQtMultiLineEditCommand::Delete )
	ins = undo;
    else if (cmd->type() == TQtMultiLineEditCommand::Insert )
	ins = !undo;
    else
	return;

    if ( ins ) {
	int row, col;
	offsetToPositionInternal( delcmd->mOffset, &row, &col );
	setCursorPosition( row, col, FALSE );
	insertAt( delcmd->mStr, row, col, FALSE );
	offsetToPositionInternal( delcmd->mOffset+delcmd->mStr.length(), &row, &col );
	setCursorPosition( row, col, FALSE );
    } else { // del
	int row, col, rowEnd, colEnd;
	offsetToPositionInternal( delcmd->mOffset, &row, &col );
	offsetToPositionInternal( delcmd->mOffset + delcmd->mStr.length(), &rowEnd, &colEnd );
	markAnchorY    = row;
	markAnchorX    = col;
	setCursorPosition( rowEnd, colEnd, FALSE );
	markDragY    = rowEnd;
	markDragX    = colEnd;
	turnMark( TRUE );
	del();
    }
}

/*
  Undoes the last text operation.
 */
void TQtMultiLineEdit::undo()
{
    if ( d->undoList.isEmpty() || isReadOnly() )
	return;
    textDirty = FALSE;
    int macroLevel = 0;
    bool before = d->undo;
    d->undo = FALSE;
    do {
	TQtMultiLineEditCommand *command = d->undoList.take();
	if ( !command )
	    break;
	processCmd( command, TRUE );
	macroLevel += command->terminator();
	if ( d->undoList.isEmpty() )
	    emit undoAvailable( FALSE );
	addRedoCmd( command );
    } while (macroLevel != 0);
    d->undo = before;
    if ( textDirty )
	emit textChanged();
    textDirty = FALSE;
}

/*
  Redoes the last text operation.
 */
void TQtMultiLineEdit::redo()
{
    if ( d->redoList.isEmpty() || isReadOnly() )
	return;
    textDirty = FALSE;
    int macroLevel = 0;
    bool before = d->undo;
    d->undo = FALSE;
    do {
	TQtMultiLineEditCommand *command = d->redoList.take();
	if ( !command )
	    break;
	processCmd( command, FALSE );
	macroLevel += command->terminator();
	if ( d->redoList.isEmpty() )
	    emit redoAvailable( FALSE );
	if ( d->undoList.isEmpty() )
	    emit undoAvailable(TRUE);
	d->undoList.append( command );
    } while (macroLevel != 0);
    d->undo = before;
    if ( textDirty )
	emit textChanged();
    textDirty = FALSE;
}

/*
  Inserts \a s at line number \a line, after character number \a col
  in the line.
  If \a s contains newline characters, new lines are inserted.
  If \a mark is TRUE the inserted text is selected.

  The cursor position is adjusted. If the insertion position is equal to
  the cursor position, the cursor is placed after the end of the new text.

 */

void TQtMultiLineEdit::insertAt( const TQString &s, int line, int col, bool mark )
{
    if ( d->undo ) {
	d->undo = FALSE;
	TQString itxt = s;
	int offset = positionToOffsetInternal( line, col );
	if ( d->maxlen >= 0 && length() + int(s.length()) > d->maxlen )
	    itxt.truncate( d->maxlen - length() );
	addUndoCmd( new TQInsTextCmd( offset, itxt ) );
	insertAtAux( s, line, col, mark ); // may perform del op
	d->undo = TRUE;
    }
    else
	insertAtAux( s, line, col, mark ); // may perform del op
}

void TQtMultiLineEdit::deleteNextChar( int offset, int row, int col )
{
    int row2, col2;
    setCursorPosition( row, col, FALSE );
    offsetToPositionInternal( offset + 1, &row2, &col2 );
    setCursorPosition( row2, col2, TRUE );

    TQString str = markedText();
    addUndoCmd( new TQDelTextCmd( offset, str ) );

    setCursorPosition( row, col, FALSE );
}

/*
  Deletes text from the current cursor position to the end of the line.
*/

void TQtMultiLineEdit::killLine()
{
    if ( d->undo ) {
	d->undo = FALSE;
	int curY, curX;
	cursorPosition( &curY, &curX );
	int offset = positionToOffsetInternal( curY, curX );
	TQtMultiLineEditRow* r = contents->at( curY );
	deselect();

	addUndoCmd( new TQBeginCommand );
	if (curX == (int)r->s.length()) {
	    if ( ! atEnd() && r->newline )
		deleteNextChar( offset, curY, curX );
	}
	else {
	    TQString str = r->s.mid( curX, r->s.length() );
	    addUndoCmd( new TQDelTextCmd( offset, str ) );
	}

	addUndoCmd( new TQEndCommand );
	killLineAux();
	d->undo = TRUE;
    }
    else
	killLineAux();
}

/*
  Deletes the character on the right side of the text cursor. If a
  text has been marked by the user (e.g. by clicking and dragging) the
  cursor is put at the beginning of the marked text and the marked
  text is removed.  \sa backspace()
*/

void TQtMultiLineEdit::del()
{
    if (d->undo ) {
	d->undo = FALSE;
	bool oldAuto = autoUpdate();
	setAutoUpdate( FALSE );
	int markBeginX, markBeginY;
	int markEndX, markEndY;

	if ( getMarkedRegion( &markBeginY, &markBeginX, &markEndY, &markEndX ) ) {
	    addUndoCmd( new TQBeginCommand );
	    int offset = positionToOffsetInternal( markBeginY, markBeginX );
	    TQString str = markedText();
	    d->undoList.append( new TQDelTextCmd( offset, str ) );
	    addUndoCmd( new TQEndCommand );
	}
	else if ( ! atEnd() ) {
	    int crsorY, crsorX;
	    cursorPosition( &crsorY, &crsorX );
	    int offset = positionToOffsetInternal( crsorY, crsorX );
	    TQtMultiLineEditRow* r = contents->at( crsorY );
	    if (r) {
		if (crsorX != (int)r->s.length())
		    deleteNextChar( offset, crsorY, crsorX );
		else if (r->newline)
		    deleteNextChar( offset, crsorY, crsorX );
		// else noop
	    }
	}
	setAutoUpdate( oldAuto );
	delAux();
	d->undo = TRUE;
    }
    else
	delAux();
}

/*
  Sets undo enabled to \a enable.

  \sa isUndoEnabled()
*/
void TQtMultiLineEdit::setUndoEnabled( bool enable )
{
    if ( d->undo == enable )
	return;
    d->undo = enable;
    if ( !enable ) {
	CLEAR_UNDO
    }
}


/*
  Returns whether the multilineedit is currently undo enabled or not.

  \sa setUndoEnabled()
 */
bool TQtMultiLineEdit::isUndoEnabled() const
{
    return d->undo;
}


/*
  Sets the maximum number of operations that can be stored on the undo
  stack to \a depth.

  \sa undoDepth()
 */
void TQtMultiLineEdit::setUndoDepth( int depth )
{
    d->undodepth = depth;
}


/*
 */
int TQtMultiLineEdit::undoDepth() const
{
    return d->undodepth;
}

void TQtMultiLineEdit::blinkTimerTimeout()
{
    cursorOn = !cursorOn;
    updateCell( cursorY, 0, FALSE );
}

void TQtMultiLineEdit::scrollTimerTimeout()
{
    TQPoint p = mapFromGlobal( TQCursor::pos() );
    if ( d->scrollAccel-- <= 0 && d->scrollTime ) {
	d->scrollAccel = initialScrollAccel;
	d->scrollTime--;
	d->scrollTimer->stop();
	d->scrollTimer->start( d->scrollTime );
    }
    int l = TQMAX(1,(initialScrollTime-d->scrollTime)/5);

	// auto scrolling is dual-use - for highlighting and DND
    int margin = d->dnd_primed ? scroll_margin : 0;
    bool mark = !d->dnd_primed;
    bool clear_mark = d->dnd_primed ? FALSE : !mark;

    for (int i=0; i<l; i++) {
	if ( p.y() < margin ) {
	    cursorUp( mark, clear_mark );
	} else if ( p.y() > height()-margin ) {
	    cursorDown( mark, clear_mark );
	} else if ( p.x() < margin ) {
	    cursorLeft( mark, clear_mark, FALSE );
	} else if ( p.x() > width()-margin ) {
	    cursorRight( mark, clear_mark, FALSE );
	} else {
	    stopAutoScroll();
	    break;
	}
    }
}

void TQtMultiLineEdit::dndTimeout()
{
#ifndef TQT_NO_DRAGANDDROP
    doDrag();
#endif
}

int TQtMultiLineEdit::setNumRowsAndTruncate()
{
    int n = contents->count();
    int r = 0;
    while ( d->maxlines >= 0 && n > d->maxlines ) {
	// truncate
	contents->at(n-2)->newline = TRUE;
	contents->removeLast();
	if ( markAnchorY == n-1 )
	    markAnchorY--;
	if ( markDragY == n-1 )
	    markDragY--;
	if ( cursorY == n-1 ) {
	    cursorY--;
	    cursorX = contents->at(cursorY)->s.length();
	}
	n--;
	r++;
    }
    setNumRows( n );
    return r;
}

/* \reimp
*/
bool TQtMultiLineEdit::event( TQEvent * e )
{
    if ( e->type() == TQEvent::AccelOverride && !isReadOnly() ) {
	TQKeyEvent* ke = (TQKeyEvent*) e;
	if ( ke->state() & ControlButton ) {
	    switch ( ke->key() ) {
	    case Key_A:
	    case Key_E:
#if defined (_WS_WIN_)
	    case Key_Insert:
#endif
	    case Key_X:
	    case Key_V:
	    case Key_C:
	    case Key_Left:
	    case Key_Right:
	    case Key_Up:
	    case Key_Down:
	    case Key_Home:
	    case Key_End:
		ke->accept();
	    default:
		break;
	    }
	} else {
	    switch ( ke->key() ) {
	    case Key_Delete:
	    case Key_Home:
	    case Key_End:
	    case Key_Backspace:
		ke->accept();
	    default:
		break;
	    }
	}
    }
    return TQWidget::event( e );
}

/* \reimp
*/

bool TQtMultiLineEdit::focusNextPrevChild( bool )
{
    return FALSE;
}

#endif