File: ucmdbox.pas

package info (click to toggle)
doublecmd 1.0.10-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 44,380 kB
  • sloc: pascal: 368,788; ansic: 6,001; sh: 769; makefile: 196; python: 52; xml: 8
file content (2388 lines) | stat: -rw-r--r-- 67,722 bytes parent folder | download | duplicates (3)
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
{ Copyright (C) 2007 Julian Schutsch

  This source is free software; you can redistribute it and/or modify it under
  the terms of the GNU Lesser General Public License as published by the Free
  Software Foundation; either version 3 of the License, or (at your option)
  any later version.

  This code is distributed in the hope that it will be useful, but WITHOUT ANY
  WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
  details.

  A copy of the GNU Lesser General Public License is available on the World Wide Web
  at <http://www.gnu.org/copyleft/lgpl.html>. You can also obtain it by writing
  to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
  MA 02111-1307, USA.
  
  Changelog:
    9.27.2007 : Seperation from another Package, first Release under GPL
                Version 0.1
   10.02.2007 : Licence Changed to LGPL
                Added   : History
                Added   : Password Input mode
                Fixed   : Blank Screen when Resizing so that TopLine disappears.
                Added   : Fixed Prompt Description infront of Input, moves along with it
                Etc     : Functions, minor Bugs
                Missing : FreeLineWidth full support
                Version 0.2
   10.08.2007 : Removed : Fixed Line Width Support, Source now less complex
                Added   : Paste/Copy/Cut Ability, Select with Mouse and Shift/Keys
                Added   : TRTLCriticalsection called FLock to make Writeln/Write Threadsafe
                Fixed   : GTK 1/2 Linux support, several changes to make that work...
                Removed : LineWidth, can cause Property Loading Errors if used with old Apps !
                Workarn : GTK Font height gets 2 added on all plattforms, means, win32 have two extra dots unecessarily, can't solve that !
                Fixed   : Pos 1/End Key changes Scrollbar (Different GTK behaviour !)
                Version 0.3
   12.06.2008 : Optimized Color String output, still needs testing and PWD Strings are not
                changed yet. Improvement visible on Win32, but still to slow, any hacks?
   17.06.2008 : TColorString changed completly, now using Arrays instead of linked lists
   25.06.2008 : Fixed everything for Multispace support
                Added tabulator behaviour
                Caret type and Color now customizable
                Input Selection Colors published
                Speed improvement using precalculated Sum-Widths for TColorString
                Lots of minor UTF8 Bugs fixed
   26.06.2008 : Escape Codes for some sort of Graphical output (Tables, lines, etc)
                Better moving Input
                Bug fixes in MakeInputVisible
   27.06.2008 : Add FGraphicCharWidth
   28.06.2008 : New Escape Code preprocessor
                Support for different modes (ANSI color, CmdBox, None(ignore))
   29.06.2008 : FStringBuffer added,Works without WakeMainThread now as well
                Fixed LineOutAndFill
                Added AutoFollow
   
   Todo    : Input Masks
   Todo    : Docu

}
unit uCmdBox;

{$mode objfpc}{$H+}

interface

uses Classes, SysUtils, ExtCtrls, ComCtrls, Controls, Graphics, Forms, LCLType,
     LCLIntf, LMessages, LResources, ClipBrd, LCLProc, LazUTF8;
  
type TCaretType=(cartLine,cartSubBar,cartBigBar,cartUser);
     TEscapeCodeType=(esctCmdBox,esctAnsi,esctNone);
     TEscapeMode=(escmNone,escmOperation,escmData2,escmData1,escmAnsiOperation,escmAnsiSquare);
  
type TCmdBox=class;
type TColorstring=class;
  
type EOnCmdBoxInput=Procedure(ACmdBox:TCmdBox;Input:String) of object;
type EOnCmdBoxInputChange=Procedure(ACmdBox:TCmdBox;InputData:TColorstring) of object;

type

{ TCmdBox }

TCmdBox=class(TCustomControl)
    public
      constructor Create(AComponent:TComponent);override;
      destructor Destroy;override;
    protected
      procedure Paint;override;
      procedure Resize;override;
      procedure UTF8KeyPress(var Key:TUTF8Char);override;
      procedure KeyDown(var Key:Word;Shift:TShiftState);override;
      procedure CreateParams(var Params:TCreateParams);override;
      procedure CreateWnd;override;
      procedure WMVScroll(var message: TLMVScroll);message LM_VSCROLL;
      procedure MouseDown(Button:TMouseButton;Shift:TShiftState;x,y:Integer);override;
      procedure MouseUp(Button:TMouseButton;Shift:TShiftState;x,y:Integer);override;
      procedure MouseMove(Shift:TShiftState;x,y:Integer);override;
      procedure EraseBackground(DC:HDC);override;
    private
      FLock               : System.TRTLCriticalSection;
      FCaretTimer         : TTimer;
      FCaretVisible       : Boolean;
      FLineCount          : Integer;
      FLines              : array of TColorstring;
      FLineHeights        : array of Integer;
      FLineHeightSum      : array of Integer;
      FTopLine            : Integer;
      FPageHeight         : Integer;
      FVisibleLines       : Integer;
      FVSBVisible         : Boolean;
      FVSBPos             : Integer;
      FVSBWidth           : Integer;
      FClientWidth        : Integer;
      FClientHeight       : Integer;
      FCaretX             : Integer;
      FOutX,FOutY         : Integer;
      FInputX,FInputY     : Integer;
      FInputPos           : Integer;
      FCharHeight         : Integer;
      FLineOfTopLine      : Integer;
      FVisibleLineCount   : Integer;
      FInput              : Boolean;
      FInputBuffer        : TColorstring;
      FInputVisible       : Boolean;
      FInputMinPos        : Integer;
      FUTF8InputMinPos    : Integer;
      FOnInput            : EOnCmdBoxInput;
      FOnAny              : EOnCmdBoxInputChange;
      FOnInputChange      : EOnCmdBoxInputChange;
      FBackGroundColor    : TColor;
      FCurrentColor       : TColor;
      FCurrentBackGround  : TColor;
      FFont               : TFont;
      FPassWordChar       : TUTF8Char;
      FInputIsPassWord    : Boolean;
      FHistory            : Array of TColorstring;
      FHistoryLength      : Integer;
      FHistoryMax         : Integer;
      FHistoryPos         : Integer;
      FInputColor         : TColor;
      FInputBackground    : TColor;
      FInputSelColor      : TColor;
      FInputSelBackGround : TColor;
      FMouseDown          : Boolean;
      FSelStart,FSelEnd   : Integer;
      FMouseDownInputPos  : Integer;
      FCurrentString      : String;
      FCaretColor         : TColor;
      FCaretType          : TCaretType;
      FCaretWidth         : Integer;
      FCaretHeight        : Integer;
      FCaretYShift        : Integer;
      FTabWidth           : Integer;
      FGraphicCharWidth   : Integer;
      FEscapeCodeType     : TEscapeCodeType;
      FEscapeMode         : TEscapeMode;
      FEscapeData         : String;
      FStringBuffer       : TStringList;
      FAutoFollow         : Boolean;
      procedure CaretTimerExecute(Sender:TObject);
      procedure SetLineCount(c:Integer);
      procedure SetTopLine(Nr:Integer);
      procedure AdjustScrollBars;
      function AdjustLineHeight(i:Integer):Integer;
      procedure MakeInputVisible;
      procedure MakeOutVisible;
      procedure SetFont(F:TFont);
      procedure SetBackGroundColor(c:Tcolor);
      function GetSystemMetricsGapSize(const Index:Integer):Integer;
      procedure ScrollBarRange(Which: Integer; aRange,aPage: Integer);
      procedure ScrollBarPosition(Which,Value:Integer);
      function UpdateLineHeights:Integer;
      procedure TranslateScrollBarPosition;
      procedure ScrollUp;
      procedure SetHistoryMax(v:Integer);
      procedure InsertHistory;
      procedure SetHistoryPos(v:Integer);
      function GetHistory(i:Integer):string;
      procedure DeleteHistoryEntry(i:Integer);
      procedure MakeFirstHistoryEntry(i:Integer);
      function MoveInputCaretTo(x,y:Integer;chl:Boolean):Boolean;
      procedure SetSelection(Start,Ende:Integer);
      procedure LeftSelection(Start,Ende:Integer);
      procedure RightSelection(Start,Ende:Integer);
      procedure DeleteSelected;
      procedure SetOutY(v:Integer);
      procedure IntWrite;
      procedure MultiWrite;
      procedure SetCaretType(ACaretType:TCaretType);
      procedure SetCaretWidth(AValue:Integer);
      procedure SetCaretHeight(AValue:Integer);
      procedure SetCaretYShift(AValue:Integer);
      procedure SetTabWidth(AValue:Integer);
      function GetCaretInterval:Integer;
      procedure SetCaretInterval(AValue:Integer);

    public
      
      function HistoryHas(s:string):Boolean;
      function HistoryIndexOf(s:string):Integer;
      procedure ClearHistory;
      procedure TextColor(C:TColor);
      procedure TextBackground(C:TColor);
      procedure TextColors(FC,BC:TColor);
      procedure Write(s:String);
      procedure Writeln(s:String);
      procedure WriteStream(Stream:TStream);
      procedure Clear;
      procedure StartRead(DFC,DBC:TColor;const Desc:String;IFC,IBC:TColor);
      procedure StartReadPassWord(DFC,DBC:TColor;const Desc:String;IFC,IBC:TColor);
      procedure StopRead;
      procedure CopyToClipBoard;
      procedure PasteFromClipBoard;
      procedure CutToClipBoard;
      procedure ClearLine;
      property OutX               : Integer   read FOutX        write FOutX;
      property OutY               : Integer   read FOutY        write SetOutY;
      property TopLine            : Integer   read FTopLine     write SetTopLine;
      property History[i:Integer] : string    read GetHistory;
      property InputPos           : Integer   read FInputPos    write FInputPos;
      function HistoryCount       : Integer;

    published
      property Align;
      property Anchors;
      property ShowHint;
      property CaretColor              : TColor          read FCaretColor         write FCaretColor;
      property CaretType               : TCaretType      read FCaretType          write SetCaretType;
      property CaretWidth              : Integer         read FCaretWidth         write SetCaretWidth;
      property CaretHeight             : Integer         read FCaretHeight        write SetCaretHeight;
      property CaretYShift             : Integer         read FCaretYShift        write SetCaretYShift;
      property OnInput                 : EOnCmdBoxInput  read FOnInput            write FOnInput;
      property OnInputChange           : EOnCmdBoxInputChange read FOnInputChange write FOnInputChange;
      property OnAny                   : EOnCmdBoxInputChange read FOnAny         write FOnAny;
      property LineCount               : Integer         read FLineCount          write SetLineCount;
      property Font                    : TFont           read FFont               write SetFont;
      property BackGroundColor         : TColor          read FBackgroundColor    write SetBackGroundColor;
      property TabWidth                : Integer         read FTabWidth           write SetTabWidth;
      property PassWordChar            : TUTF8Char       read FPassWordChar       write FPassWordChar;
      property HistoryMax              : Integer         read FHistoryMax         write SetHistoryMax;
      property InputSelColor           : TColor          read FInputSelColor      write FInputSelColor;
      property InputSelBackGround      : TColor          read FInputSelBackGround write FInputSelBackGround;
      property CaretInterval           : Integer         read GetCaretInterval    write SetCaretInterval;
      property EscapeCodeType          : TEscapeCodeType read FEscapeCodeType     write FEscapeCodeType;
      property GraphicalCharacterWidth : Integer         read FGraphicCharWidth   write FGraphicCharWidth;
      property AutoFollow              : Boolean         read FAutoFollow         write FAutoFollow;
      property DoubleBuffered default true;
     end;
     
type TColorChar=record
      FChar        : TUTF8Char;
      FCharWidth   : Integer;
      FSumWidth    : Integer;
      FWordStart   : Integer;
      FFrontColor  : TColor;
      FBackColor   : TColor;
     end;
     
type TColorString=class
      private
      FChars         : array of TColorChar;
      FSumWidth      : Integer;
      FPassWordStart : Integer;
      FPassWordChar  : TUTF8Char;
      FTabWidth      : Integer;
      procedure MinimumLength(V:Integer;FC,BC:TColor);
      procedure MaximumLength(V:Integer);
      procedure UpdateSum;
      public
      constructor Create(AFont:TFont);
      destructor Destroy;override;
      procedure Clear;
      procedure OverWrite(S:String;Pos:Integer;FC,BC:TColor);
      procedure OverWriteChar(s:TUTF8Char;Pos,ADefWidth:Integer;FC,BC:TColor);
      procedure OverWrite(S:TColorstring;Pos:Integer);
      procedure OverWritePW(S:TColorstring;PWS,Pos:Integer;PWC:String);
      procedure PartOverWrite(S:TColorstring;Start,Ende,Pos:Integer);
      procedure LineOutAndFill(ACanvas:TCanvas;AX,AY,ALeftX,AWrapWidth,ACH,ACB,ACaretPos:Integer;ABC,ACC:TColor;ACaretHeight,ACaretWidth,ACaretYShift:Integer;ADrawCaret:Boolean);
      function Getstring:String;
      function GetPartstring(Start,Ende:Integer):String;
      procedure Delete(Index:Integer);
      procedure Delete(Index,Len:Integer);
      procedure Insert(Index:Integer;C:String;FC,BC:TColor);
      procedure BColorBlock(StartPos,EndPos:Integer;C:TColor);
      procedure ColorBlock(StartPos,EndPos:Integer;FC,BC:TColor);
      function LineCount(AWrapWidth,ACaretPos,ACaretWidth:Integer):Integer;
      function GetLength:Integer;
      function GetLineOfCaret(AWrapWidth,ACaretPos,ACaretWidth:Integer):Integer;
      function GetCharPosition(AWrapWidth,ALine,AXPos:Integer):Integer;
      private
      FFont              : TFont;
      FDefaultBackGround : TColor;
      public
      property TabWidth          : Integer read FTabWidth write FTabWidth;
      property PassWordChar      : TUTF8Char read FPassWordChar write FPassWordChar;
      property PassWordStart     : Integer read FPassWordStart write FPassWordStart;
      property Length            : Integer read GetLength;
      property DefaultBackGround : TColor read FDefaultBackground write FDefaultBackground;
      property Font              : TFont read FFont write FFont;
     end;

procedure Register;

implementation

procedure TColorString.UpdateSum;
var i             : Integer;
    LastWordStart : Integer;
    SumWidth      : Integer;
begin
 LastWordStart := 0;
 SumWidth      := 0;
 for i:=0 to High(FChars) do
 begin
  with FChars[i] do
  begin
   FWordStart := LastWordStart;
   case FChar[1] of
    #9:
    begin
     FCharWidth    := (SumWidth div FTabWidth+1)*FTabWidth-SumWidth;
     LastWordStart := i+1;
    end;
    #27:
    begin
     case FChar[2] of
      #9:
      begin
       FCharWidth    := (SumWidth div FTabWidth+1)*FTabWidth-SumWidth;
       LastWordStart := i+1;
      end;
      #10:LastWordStart:=i+1;
      #32,#46,#196,#205:
      begin
       FCharWidth    := Ord(FChar[3]);
       LastWordStart := i+1;
      end;
      #33,#47,#197,#206:
      begin
       FCharWidth    := (Ord(FChar[3])+Ord(FChar[4])*256)-SumWidth;
       if FCharWidth<0 then FCharWidth:=0;
       LastWordStart := i+1;
      end;
     end;
    end;
    else if FChar=' ' then LastWordStart:=i+1;
   end;
   SumWidth   := SumWidth+FCharWidth;
   FSumWidth  := SumWidth;
  end;
 end;
 FSumWidth:=SumWidth;
end;

function TColorString.GetLength:Integer;
begin
 Result:=System.Length(FChars);
end;

procedure TCmdBox.SetTabWidth(AValue:Integer);
var i:Integer;
begin
 FTabWidth:=AValue;
 for i:=0 to FLineCount-1 do FLines[i].TabWidth:=AValue;
 UpdateLineHeights;
 Invalidate;
end;

procedure TCmdBox.SetCaretWidth(AValue:Integer);
begin
 FCaretWidth := AValue;
 FCaretType  := cartUser;
end;

procedure TCmdBox.SetCaretHeight(AValue:Integer);
begin
 FCaretHeight := AValue;
 FCaretType   := cartUser;
end;

procedure TCmdBox.SetCaretYShift(AValue:Integer);
begin
 FCaretYShift := AValue;
 FCaretType   := cartUser;
end;

procedure TCmdBox.SetCaretType(ACaretType:TCaretType);
begin
 case ACaretType of
  cartLine:
  begin
   if HandleAllocated then FCaretHeight:=FFont.GetTextHeight('A')-3 else FCaretHeight:=-1;
   FCaretWidth  := 1;
   FCaretYShift := 3;
  end;
  cartSubBar:
  begin
   FCaretWidth  := -1;
   FCaretHeight := 3;
   FCaretYShift := 0;
  end;
  cartBigBar:
  begin
   if HandleAllocated then FCaretHeight:=FFont.GetTextHeight('A')-3 else FCaretHeight:=-1;
   FCaretWidth  := -1;
   FCaretYShift := 3;
  end;
 end;
 Invalidate;
 FCaretType:=ACaretType;
end;

// TOdo : Use string buffer instead of string (speed improvement expected)
procedure TColorString.LineOutAndFill(ACanvas:TCanvas;AX,AY,ALeftX,AWrapWidth,ACH,ACB,ACaretPos:Integer;ABC,ACC:TColor;ACaretHeight,ACaretWidth,ACaretYShift:Integer;ADrawCaret:Boolean);
var LineStart         : Integer;
    LineEnd           : Integer;
    MidWidth          : Integer;
    LineStartSumWidth : Integer;
    x                 : Integer;
    LastLineSumWidth  : Integer;
    ACHH              : Integer;
    ACBH              : Integer;

procedure DrawLine;
var SameColor      : String;
    SameForeColor  : TColor;
    SameBackColor  : TColor;
    SameColorX     : Integer;
    SameColorWidth : Integer;
    LP             : Integer;
    CaretX         : Integer;
    CaretW         : Integer;
    CW             : Integer;
    xp             : Integer;
begin
 if (AY<=-ACH) and (AY>ACanvas.Height) then
 begin
  Inc(AY,ACH);
  Ax:=ALeftx;
  Exit;
 end;
 SameColor:='';
 ACanvas.Brush.Style:=bsSolid;
 // TODO: Please try to reproduce this Ultra-Shit Error which couples GetTextWidth input to
 //       TextOut Output. I can't solve this...i am not Stupid Enough for such errors!
 // (Is in bug report, should be fixed somewhere in the future!)
 ACanvas.Font.GetTextWidth('%%%_$%_Hallo\\\\\\\\\32489738');
 // End of shit
 LP     := LineStart;
 CaretX := -1;
 while LineStart<>LineEnd+1 do
 begin
  with FChars[LineStart] do
  begin
   CW:=FCharWidth;
   if FChar=#9 then
   begin
    if SameColor<>'' then
    begin
     ACanvas.Font.Color  :=SameForeColor;
     ACanvas.Brush.Color :=SameBackColor;
     ACanvas.FillRect(SameColorX,AY,SameColorX+SameColorWidth,Ay+ACH);
     ACanvas.TextOut(SameColorX,AY,SameColor);
     Inc(SameColorX,SameColorWidth);
     SameColor:='';
    end else SameColorX:=AX;
    ACanvas.Brush.Color:=FBackColor;
    ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
   end
   else
   if FChar[1]=#27 then
   begin
    if SameColor<>'' then
    begin
     ACanvas.Font.Color  :=SameForeColor;
     ACanvas.Brush.Color :=SameBackColor;
     ACanvas.FillRect(SameColorX,AY,SameColorX+SameColorWidth,Ay+ACH);
     ACanvas.TextOut(SameColorX,AY,SameColor);
     Inc(SameColorX,SameColorWidth);
     SameColor:='';
    end else SameColorX:=AX;
    case FChar[2] of
     #9:
     begin
      case FChar[3] of
       #46:
       begin
        ACanvas.Pen.Color:=FFrontColor;
        ACanvas.Pen.Style:=psDash;
        ACanvas.Brush.Color:=FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
        xp:=SameColorX;
        if xp mod 2<>0 then Inc(xp);
        while xp<SameColorX+FCharWidth do
        begin
         ACanvas.Pixels[xp,AY+ACH-3]:=FFrontColor;
         Inc(xp,2);
        end;
       end;
       #196:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Pen.Style   := psSolid;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
        ACanvas.Line(SameColorX,AY+ACHH,SameColorX+FCharWidth,AY+ACHH);
       end;
      end;
     end;
     #10:
     begin
      CW:=AWrapWidth-SameColorX;
      case FChar[3] of
       #179:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.Line(SameColorX+CW-ACBH,AY,SameColorX+CW-ACBH,AY+ACH);
       end;
       #180:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.Line(SameColorX+CW-ACBH,AY,SameColorX+CW-ACBH,AY+ACH);
        ACanvas.Line(SameColorX,AY+ACHH,SameColorX+CW-ACBH,AY+ACHH);
       end;
       #191:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.MoveTo(SameColorX,AY+ACHH);
        ACanvas.LineTo(SameColorX+CW-ACBH,AY+ACHH);
        ACanvas.LineTo(SameColorX+CW-ACBH,AY+ACH);
       end;
       #196:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.Line(SameColorX,AY+ACHH,SameColorX+CW,AY+ACHH);
       end;
       #205:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.Line(SameColorX,AY+ACHH-1,SameColorX+CW,AY+ACHH-1);
        ACanvas.Line(SameColorX,AY+ACHH+1,SameColorX+CW,AY+ACHH+1);
       end;
       #217:
       begin
        ACanvas.Pen.Color   := FFrontColor;
        ACanvas.Brush.Color := FBackColor;
        ACanvas.Fillrect(SameColorX,AY,SameColorX+CW,AY+ACH);
        ACanvas.MoveTo(SameColorX,AY+ACHH);
        ACanvas.LineTo(SameColorX+CW-ACBH,AY+ACHH);
        ACanvas.LineTo(SameColorX+CW-ACBH,AY-1);
       end;
      end;
     end;
     #32,#33:
     begin
      ACanvas.Brush.Color:=FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
     end;
     #46,#47:
     begin
      ACanvas.Pen.Color:=FFrontColor;
      ACanvas.Brush.Color:=FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      xp:=SameColorX;
      if xp mod 2<>0 then Inc(xp);
      while xp<SameColorX+FCharWidth do
      begin
       ACanvas.Pixels[xp,AY+ACH-3]:=FFrontColor;
       Inc(xp,2);
      end;
     end;
     #196,#197:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX,AY+ACHH,SameColorX+FCharWidth,AY+ACHH);
     end;
     #179:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX+ACBH,AY,SameColorX+ACBH,AY+ACH);
     end;
     #193:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX,AY+ACHH,SameColorX+ACB,AY+ACHH);
      ACanvas.Line(SameColorX+ACBH,AY,SameColorX+ACBH,AY+ACHH);
     end;
     #194:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX,AY+ACHH,SameColorX+ACB,AY+ACHH);
      ACanvas.Line(SameColorX+ACBH,AY+ACHH,SameColorX+ACBH,AY+ACH);
     end;
     #198:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX,AY+ACHH,SameColorX+ACB,AY+ACHH);
      ACanvas.Line(SameColorX+ACBH,AY,SameColorX+ACBH,AY+ACH);
     end;
     #195:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.Line(SameColorX+ACBH,AY,SameColorX+ACBH,AY+ACH);
      ACanvas.Line(SameColorX+ACBH,AY+ACHH,SameColorX+ACB,AY+ACHH);
     end;
     #217:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.MoveTo(SameColorX+ACBH,AY);
      ACanvas.LineTo(SameColorX+ACBH,AY+ACHH);
      ACanvas.LineTo(SameColorX+ACB,AY+ACHH);
     end;
     #218:
     begin
      ACanvas.Pen.Color   := FFrontColor;
      ACanvas.Brush.Color := FBackColor;
      ACanvas.Fillrect(SameColorX,AY,SameColorX+FCharWidth,AY+ACH);
      ACanvas.MoveTo(SameColorX+ACBH,AY+ACH);
      ACanvas.LineTo(SameColorX+ACBH,AY+ACHH);
      ACanvas.LineTo(SameColorX+ACB,AY+ACHH);
     end;
    end;
   end
   else
   if SameColor='' then
   begin
    if (LP>=FPassWordStart) then
    begin
     SameColor      := FPassWordChar;
     SameColorWidth := FFont.GetTextWidth(FPassWordChar);
    end
    else
    begin
     SameColor      := FChar;
     SameColorWidth := FCharWidth;
    end;
    SameColorX    := AX;
    SameForeColor := FFrontColor;
    SameBackColor := FBackColor;
   end
   else
   begin
    if (SameForeColor=FFrontColor) and (SameBackColor=FBackColor) then
    begin
     if (LP>=FPassWordStart) then
     begin
      SameColor:=SameColor+FPassWordChar;
      Inc(SameColorWidth,FFont.GetTextWidth(FPassWordChar));
     end
     else
     begin
      SameColor:=SameColor+FChar;
      Inc(SameColorWidth,FCharWidth);
     end;
    end
    else
    begin
     ACanvas.Font.Color  :=SameForeColor;
     ACanvas.Brush.Color :=SameBackColor;
     ACanvas.FillRect(SameColorX,Ay,SameColorX+SameColorWidth,Ay+ACH);
     ACanvas.TextOut(SameColorX,AY,SameColor);
     if (LP>=FPassWordStart) then
     begin
      SameColor      := FPassWordChar;
      SameColorWidth := FFont.GetTextWidth(FPassWordChar);
     end
     else
     begin
      SameColor      := FChar;
      SameColorWidth := FCharWidth;
     end;
     SameForeColor  := FFrontColor;
     SameBackColor  := FBackColor;
     SameColorX     := AX;
    end;
   end;
   if LP=ACaretPos then
   begin
    CaretX:=AX;
    CaretW:=FCharWidth;
   end;
   Inc(AX,CW);
   Inc(LP);
  end;
  Inc(LineStart);
 end;
 if SameColor<>'' then
 begin
  ACanvas.Font.Color  := SameForeColor;
  ACanvas.Brush.Color := SameBackColor;
  ACanvas.FillRect(SameColorX,Ay,SameColorX+SameColorWidth,Ay+ACH);
  ACanvas.TextOut(SameColorX,AY,SameColor);
 end;
 ACanvas.FillRect(AX,AY,AWrapWidth,AY+ACH);
 AX:=ALeftX;
 Inc(AY,ACH);
 if ADrawCaret and (CaretX>=0) then
 begin
  ACanvas.Brush.Color:=ACC;
  if ACaretWidth>=0 then CaretW:=ACaretWidth;
  ACanvas.FillRect(CaretX,AY-ACaretHeight-ACaretYShift,CaretX+CaretW,AY-ACaretYShift);
 end;
end;

begin
 if AWrapWidth<0 then AWrapWidth:=0;
 if System.Length(FChars)=0 then
 begin
  ACanvas.Brush.Style := bsSolid;
  ACanvas.Brush.Color := ABC;
  ACanvas.FillRect(AX,AY,AWrapWidth,AY+ACH);
  Exit;
 end;
 ACHH              := ACH div 2;
 ACBH              := ACB div 2;
 MidWidth          := FSumWidth div System.Length(FChars);
 LineStart         := 0;
 LineStartSumWidth := 0;
 LastLineSumWidth  := 0;
 x                 := 0;
 while LineStart<System.Length(FChars) do
 begin
  x:=LineStart+AWrapWidth div MidWidth;
  if x>High(FChars) then x:=High(FChars);
  while (x<High(FChars)) and (FChars[x].FSumWidth-LineStartSumWidth<AWrapWidth) do Inc(x);
  while (x>LineStart) and (FChars[x].FSumWidth-LineStartSumWidth>=AWrapWidth) do
   with FChars[x] do if (FChar<>' ') and (FWordStart>LineStart) then x:=FWordStart-1 else Dec(x);
  LineEnd:=x;
  DrawLine;
  LastLineSumWidth  := LineStartSumWidth;
  LineStartSumWidth := FChars[x].FSumWidth;
  LineStart         := x+1;
 end;
 if ACaretPos>=LineStart then
 begin
  if ACaretWidth>=0 then x:=ACaretWidth else x:=FFont.GetTextWidth('A');
  AX:=LineStartSumWidth-LastLineSumWidth+(ACaretPos-LineStart)*x;
  if Ax+x>AWrapWidth then
  begin
   Ax:=0;
   ACanvas.Brush.Color:=ABC;
   ACanvas.FillRect(0,AY,AWrapWidth,AY+ACH);
   Inc(Ay,ACH);
  end;
  if ADrawCaret then
  begin
   ACanvas.Brush.Color:=ACC;
   ACanvas.FillRect(AX,AY-ACaretHeight-ACaretYShift,AX+x,AY-ACaretYShift);
  end;
 end;
end;

function TColorString.GetCharPosition(AWrapWidth,ALine,AXPos:Integer):Integer;
var x,MidWidth,LineStart,LineStartSumWidth,LastLineSumWidth,LastLineStart:Integer;
begin
 if AWrapWidth<0 then AWrapWidth:=0;
 if System.Length(FChars)=0 then
 begin
  Result:=0;
  Exit;
 end;
 MidWidth := FSumWidth div System.Length(FChars);
 if MidWidth=0 then
 begin
  Result:=0;
  Exit;
 end;
 LineStart         := 0;
 LineStartSumWidth := 0;
 LastLineSumWidth  := 0;
 x:=0;
 while (LineStart<System.Length(FChars)) and (ALine>=0) do
 begin
  x:=LineStart+AWrapWidth div MidWidth;
  if x>High(FChars) then x:=High(FChars);
  while (x<High(FChars)) and (FChars[x].FSumWidth-LineStartSumWidth<AWrapWidth) do Inc(x);
  while (x>LineStart) and (FChars[x].FSumWidth-LineStartSumWidth>=AWrapWidth) do
   with FChars[x] do if (FChar<>' ') and (FWordStart>LineStart) then x:=FWordStart-1 else Dec(x);
  LastLineSumWidth  := LineStartSumWidth;
  LineStartSumWidth := FChars[x].FSumWidth;
  LastLineStart     := LineStart;
  LineStart         := x+1;
  Dec(ALine);
 end;
 Result:=LastLineStart;
 while (Result<LineStart) and (FChars[Result].FSumWidth-LastLineSumWidth<=AXPos) do Inc(Result);
end;

function TColorString.GetLineOfCaret(AWrapWidth,ACaretPos,ACaretWidth:Integer):Integer;
var x,MidWidth,LineStart,LineStartSumWidth,LastLineSumWidth:Integer;
begin
 if AWrapWidth<0 then AWrapWidth:=0;
 if System.Length(FChars)=0 then
 begin
  Result:=0;
  Exit;
 end;
 MidWidth := FSumWidth div System.Length(FChars);
 if MidWidth=0 then
 begin
  Result:=0;
  Exit;
 end;
 LineStart         := 0;
 LineStartSumWidth := 0;
 LastLineSumWidth  := 0;
 Result            := 0;
 x:=0;
 while LineStart<System.Length(FChars) do
 begin
  x:=LineStart+AWrapWidth div MidWidth;
  if x>High(FChars) then x:=High(FChars);
  while (x<High(FChars)) and (FChars[x].FSumWidth-LineStartSumWidth<AWrapWidth) do Inc(x);
  while (x>LineStart) and (FChars[x].FSumWidth-LineStartSumWidth>=AWrapWidth) do
   with FChars[x] do if (FChar<>' ') and (FWordStart>LineStart) then x:=FWordStart-1 else Dec(x);
  LastLineSumWidth  := LineStartSumWidth;
  LineStartSumWidth := FChars[x].FSumWidth;
  LineStart         := x+1;
  if ACaretPos<x then Exit;
  Inc(Result);
 end;
 if ACaretWidth>=0 then x:=ACaretWidth else x:=FFont.GetTextWidth('A');
 if (ACaretPos>LineStart) or (LineStartSumWidth-LastLineSumWidth+(ACaretPos-LineStart)*x+x<=AWrapWidth) then Dec(Result);
end;

function TColorString.LineCount(AWrapWidth,ACaretPos,ACaretWidth:Integer):Integer;
var x                 : Integer;
    MidWidth          : Integer;
    LineStart         : Integer;
    LineStartSumWidth : Integer;
    LastLineSumWidth  : Integer;
begin
 if AWrapWidth<0 then AWrapWidth:=0;
 if System.Length(FChars)=0 then
 begin
  Result:=1;
  Exit;
 end;
 MidWidth := FSumWidth div System.Length(FChars);
 if MidWidth=0 then
 begin
  Result:=1;
  Exit;
 end;
 LineStart         := 0;
 LineStartSumWidth := 0;
 LastLineSumWidth  := 0;
 Result:=0;
 x:=0;
 while LineStart<System.Length(FChars) do
 begin
  x:=LineStart+AWrapWidth div MidWidth;
  if x>High(FChars) then x:=High(FChars);
  while (x<High(FChars)) and (FChars[x].FSumWidth-LineStartSumWidth<AWrapWidth) do Inc(x);
  while (x>LineStart) and (FChars[x].FSumWidth-LineStartSumWidth>=AWrapWidth) do
   with FChars[x] do if (FChar<>' ') and (FWordStart>LineStart) then x:=FWordStart-1 else Dec(x);
  LastLineSumWidth  := LineStartSumWidth;
  LineStartSumWidth := FChars[x].FSumWidth;
  LineStart         := x+1;
  Inc(Result);
 end;
 if ACaretWidth>=0 then x:=ACaretWidth else x:=FFont.GetTextWidth('A');
 if (ACaretPos>=LineStart) and (LineStartSumWidth-LastLineSumWidth+(ACaretPos-LineStart)*x+x>AWrapWidth) then Inc(Result);
end;

constructor TColorString.Create(AFont:TFont);
begin
 inherited Create;
 FTabWidth      := 1;
 FFont          := AFont;
 FPassWordStart := MaxInt;
end;

procedure TColorstring.BColorBlock(StartPos,EndPos:Integer;C:TColor);
var i:Integer;
begin
 if StartPos<0 then StartPos:=0;
 if EndPos>High(FChars) then EndPos:=High(FChars);
 for i:=StartPos to EndPos do FChars[i].FBackColor:=C;
end;

procedure TColorstring.ColorBlock(StartPos,EndPos:Integer;FC,BC:TColor);
var i:Integer;
begin
 if StartPos<0 then StartPos:=0;
 if EndPos>High(FChars) then EndPos:=High(FChars);
 for i:=StartPos to EndPos do
 begin
  FChars[i].FFrontColor := FC;
  FChars[i].FBackColor  := BC;
 end;
end;

procedure TColorstring.Insert(Index:Integer;C:string;FC,BC:TColor);
var i      : Integer;
    l      : Integer;
    Pp     : Integer;
    OldLen : Integer;
    SLen   : Integer;
begin
 OldLen := System.Length(FChars);
 SLen   := UTF8Length(C);
 if OldLen<Index then MinimumLength(Index+SLen,FC,BC) else
 begin
  MinimumLength(SLen+OldLen,FC,BC);
  for i:=OldLen-1 downto Index do FChars[i+SLen]:=FChars[i];
 end;
 pp := 1;
 for i:=0 to SLen-1 do
 begin
  l:=UTF8CharacterLength(@C[Pp]);
  with FChars[Index+i] do
  begin
   FChar := Copy(C,Pp,l);
   if Index+i>=FPassWordStart then FCharWidth:=FFont.GetTextWidth(FPassWordChar) else FCharWidth:=FFont.GetTextWidth(FChar);
   FFrontColor := FC;
   FBackColor  := BC;
  end;
  Inc(pp,l);
 end;
 UpdateSum;
end;

procedure TColorstring.Delete(Index,Len:Integer);
var i:Integer;
begin
 if (Len=0) or (Index>=System.Length(FChars)) then Exit;
 if Index+Len>System.Length(FChars) then Len:=System.Length(FChars)-Index;
 for i:=Index to System.Length(FChars)-Len-1 do FChars[i]:=FChars[i+Len];
 SetLength(FChars,System.Length(FChars)-Len);
 UpdateSum;
end;

procedure TColorstring.Delete(Index:Integer);
var i:Integer;
begin
 if (Index>=System.Length(FChars)) then Exit;
 for i:=Index to System.Length(FChars)-2 do FChars[i]:=FChars[i+1];
 SetLength(FChars,System.Length(FChars)-1);
 UpdateSum;
end;

function TColorstring.GetPartstring(Start,Ende:Integer):string;
var i,n : Integer;
    Len : Integer;
begin
 if Start<0 then Start:=0;
 if Ende>High(FChars) then Ende:=High(FChars);
 Len:=0;
 for i:=Start to Ende do Inc(Len,System.Length(FChars[i].FChar));
 SetLength(Result,Len);
 Len:=1;
 for i:=Start to Ende do
 begin
  with FChars[i] do
  begin
   for n:=1 to System.Length(FChar) do
   begin
    Result[Len]:=FChar[n];
    Inc(Len);
   end;
  end;
 end;
end;

function TColorstring.Getstring:String;
var i,n : Integer;
    Len : Integer;
begin
 Len := 0;
 for i:=0 to High(FChars) do Inc(Len,System.Length(FChars[i].FChar));
 SetLength(Result,Len);
 Len := 1;
 for i:=0 to High(FChars) do
 begin
  with FChars[i] do
  begin
   for n:=1 to System.Length(FChar) do
   begin
    Result[Len]:=FChar[n];
    Inc(Len);
   end;
  end;
 end;
end;

procedure TColorstring.OverWritePW(S:TColorstring;PWS,Pos:Integer;PWC:String);
var i    : Integer;
    CPassWordStart:Integer;
begin
 MinimumLength(Pos+S.Length,CLSilver,S.FDefaultBackGround);
 CPassWordStart:=PWS;
 for i:=0 to S.Length-1 do
 begin
  FChars[i+Pos]:=S.FChars[i];
  if CPassWordStart<=0 then FChars[i+Pos].FChar:=PWC;
  Dec(CPassWordStart);
 end;
 UpdateSum;
end;

procedure TColorstring.OverWrite(S:TColorstring;Pos:Integer);
var i : Integer;
begin
 MinimumLength(Pos+S.Length,CLSilver,S.FDefaultBackGround);
 for i:=0 to S.Length-1 do FChars[i+Pos]:=S.FChars[i];
 UpdateSum;
end;

procedure TColorstring.PartOverWrite(S:TColorstring;Start,Ende,Pos:Integer);
var i : Integer;
begin
 MinimumLength(Pos+Ende-Start,CLSilver,S.FDefaultBackGround);
 for i:=0 to Ende-Start-1 do FChars[i+Pos]:=S.FChars[i+Start];
 UpdateSum;
end;

procedure TColorstring.OverWrite(s:String;Pos:Integer;FC,BC:TColor);
var i,Pp,l : Integer;
begin
 MinimumLength(Pos+UTF8Length(S),FC,BC);
 Pp:=1;
 for i:=0 to UTF8Length(S)-1 do
 begin
  l:=UTF8CharacterLength(@s[Pp]);
  with FChars[i+Pos] do
  begin
   FChar       := Copy(S,Pp,l);
   FCharWidth  := FFont.GetTextWidth(FChar);
   FFrontColor := FC;
   FBackColor  := BC;
  end;
  Inc(Pp,l);
 end;
 UpdateSum;
end;

procedure TColorstring.OverWriteChar(s:TUTF8Char;Pos,ADefWidth:Integer;FC,BC:TColor);
begin
 MinimumLength(Pos+1,FC,BC);
 with FChars[Pos] do
 begin
  FChar       := s;
  FCharWidth  := ADefWidth;
  FFrontColor := FC;
  FBackColor  := BC;
 end;
 UpdateSum;
end;

procedure TColorstring.MinimumLength(V:Integer;FC,BC:TColor);
var OldLen,i:Integer;
begin
 if System.Length(FChars)<V then
 begin
  OldLen:=System.Length(FChars);
  SetLength(FChars,V);
  for i:=OldLen to High(FChars) do
  begin
   with FChars[i] do
   begin
    FChar       := ' ';
    FCharWidth  := FFont.GetTextWidth(' ');
    FFrontColor := FC;
    FBackColor  := BC;
   end;
  end;
 end;
end;

procedure TColorstring.MaximumLength(V:Integer);
begin
 if System.Length(FChars)>V then SetLength(FChars,V);
end;

procedure TColorstring.Clear;
begin
 FChars:=Nil;
end;

procedure TCmdBox.ClearLine;
begin
 if FLines[FOutY].Length<>0 then
 begin
  FLines[FOutY].Clear;
  FOutX:=0;
  if FInput then FInputY:=FOutY;
  Invalidate;
 end;
end;

function TCmdBox.GetCaretInterval:Integer;
begin
 Result:=FCaretTimer.Interval;
end;

procedure TCmdBox.SetCaretInterval(AValue:Integer);
begin
 FCaretTimer.Interval:=AValue;
end;

procedure TCmdBox.MultiWrite;
var DoWrite:Boolean;
begin
 System.EnterCriticalSection(FLock);
 DoWrite:=FStringBuffer.Count<>0;
 if DoWrite then
 begin
  FCurrentString:=FStringBuffer[0];
  FStringBuffer.Delete(0);
 end;
 System.LeaveCriticalSection(FLock);
 if DoWrite then IntWrite;
end;

procedure TCmdBox.Write(S:String);
begin
 if ThreadID=MainThreadId then
 begin
  MultiWrite;
  FCurrentString:=S;
  IntWrite;
 end
 else
 begin
  System.EnterCriticalSection(FLock);
  FStringBuffer.Add(S);
  System.LeaveCriticalSection(FLock);
  if Assigned(WakeMainThread) then TThread.Synchronize(Nil,@MultiWrite);
 end;
end;

function TCmdBox.HistoryIndexOf(s:string):Integer;
begin
 for Result:=0 to HistoryCount-1 do if History[Result]=s then Exit;
 Result:=-1;
end;

function TCmdBox.HistoryHas(s:string):Boolean;
var i:Integer;
begin
 Result := True;
 for i:=0 to HistoryCount-1 do if History[i]=s then Exit;
 Result := False;
end;

function TCmdBox.HistoryCount:Integer;
begin
 HistoryCount:=FHistoryLength-Ord(FInput);
end;

function TCmdBox.GetHistory(i:Integer):string;
begin
 Inc(i,Ord(FInput));
 if (i>=0) and (i<FHistoryLength) then GetHistory:=FHistory[i].Getstring else GetHistory:='';
end;

procedure TCmdBox.EraseBackGround(DC:HDC);
begin
end;

procedure TCmdBox.ClearHistory;
begin
 FHistoryLength := Ord(FInput);
 FHistoryPos    := 0;
end;

procedure TCmdBox.SetHistoryMax(v:Integer);
var i:Integer;
begin
 if v<1 then v:=1;
 if v<>FHistoryMax then
 begin
  if FHistoryLength>v then FHistoryLength:=v;
  for i:=v to FHistoryMax-1 do FHistory[i].Free;
  SetLength(FHistory,v);
  for i:=FHistoryMax to v-1 do FHistory[i]:=TColorstring.Create(Canvas.Font);
  FHistoryMax:=v;
 end;
end;

procedure TCmdBox.WriteStream(Stream:TStream);
var c:wideString;
begin
 while Stream.Position<Stream.Size do
 begin
  // Not very efficient, but should work...
  Stream.Read(c,1);
  Write(c);
 end;
end;

procedure TCmdBox.LeftSelection(Start,Ende:Integer);
begin
 if FSelStart=-1 then
 begin
  SetSelection(Start,Ende);
 end
 else
 begin
  if FSelStart=Start then SetSelection(-1,0) else
  begin
   if FSelStart<Start then
   begin
    SetSelection(FSelStart,Start);
   end else SetSelection(Start,FSelEnd+1);
  end;
 end;
end;

procedure TCmdBox.RightSelection(Start,Ende:Integer);
begin
 if FSelStart=-1 then
 begin
  SetSelection(Start,Ende);
 end
 else
 begin
  if FSelEnd+1=Ende then SetSelection(-1,0) else
  begin
   if FSelstart<Start then
   begin
    SetSelection(FSelStart,Ende);
   end else SetSelection(Ende,FSelEnd+1);
  end;
 end;
end;

procedure TCmdBox.SetSelection(Start,Ende:Integer);
begin
 if FSelStart<>-1 then FInputBuffer.ColorBlock(FSelStart,FSelEnd,FInputColor,FInputBackGround);
 if Start=Ende then FSelStart:=-1 else
 begin
  if Start<Ende then
  begin
   FSelStart := Start;
   FSelEnd   := Ende-1;
  end
  else
  begin
   FSelStart := Ende;
   FSelEnd   := Start-1;
  end;
 end;
 if FSelStart<>-1 then FInputBuffer.ColorBlock(FSelStart,FSelEnd,FInputSelColor,FInputSelBackGround);
end;

procedure TCmdBox.CopyToClipBoard;
begin
 if FSelStart<>-1 then
 begin
  ClipBoard.AsText:=FInputBuffer.GetPartstring(FSelStart,FSelEnd);
 end;
end;

procedure TCmdBox.PasteFromClipBoard;
var s:widestring;
    l,Pp:Integer;
begin
 if ClipBoard.HasFormat(CF_TEXT) then
 begin
  s  := ClipBoard.AsText;
  Pp := 1;
  while pp<=Length(s) do
  begin
   l := UTF8CharacterLength(@S[Pp]);
   if (l=1) and (Byte(S[Pp])<32) then Delete(s,Pp,1) else inc(Pp,l);
  end;
  FInputBuffer.Insert(InputPos,s,FInputColor,FInputBackGround);
  InputPos := InputPos + UTF8Length(s);
  FCaretX:=FInputX+InputPos;
  AdjustScrollBars;
  MakeInputVisible;
  FHistoryPos:=0;
  if Assigned(FOnInputChange) then FOnInputChange(Self,FInputBuffer);
  if Assigned(FOnAny) then FOnAny(Self,FInputBuffer);
 end;
end;

procedure TCmdBox.DeleteSelected;
begin
 if FSelStart<>-1 then
 begin
  FInputBuffer.Delete(FSelStart,FSelEnd-FSelStart+1);
  FInputPos := FSelStart;
  FCaretX   := FInputX+FInputPos;
  FSelStart := -1;
 end;
end;

procedure TCmdBox.CutToClipBoard;
begin
 if FSelStart<>-1 then
 begin
  ClipBoard.AsText:=FInputBuffer.GetPartstring(FSelStart,FSelEnd);
  DeleteSelected;
 end;
end;

procedure TCmdBox.MouseMove(Shift:TShiftState;x,y:Integer);
begin
 if FMouseDown then
 begin
  if MoveInputCaretTo(x,y,false) then SetSelection(FMouseDownInputPos,FInputPos);
 end;
end;

function TCmdBox.MoveInputCaretTo(x,y:Integer;chl:Boolean):Boolean;
var h,sl,q:Integer;
begin
 if not FInput then Exit;
 y  := y div FCharHeight;
 h  := FLineHeightSum[FTopLine]+FLineOfTopLine+y;
 sl := FTopLine;
 while (sl<FLineCount-1) and (FLineHeightSum[sl+1]<=h) do inc(sl);
 if (sl=FInputY) or (not chl) then
 begin
  Dec(h,FLineHeightSum[FInputY]);
  q:=FInputBuffer.GetCharPosition(FClientWidth,h,x);
  if (q<FInputMinPos) then q:=FInputMinPos;
  if (q-FInputX>FInputBuffer.Length) then q:=FInputBuffer.Length-FInputX;
  FCaretX   := q;
  FInputPos := FCaretX-FInputX;
  if Assigned(FOnAny) then FOnAny(Self,FInputBuffer);
  Invalidate;
  Result:=True;
 end else Result:=False;
end;

procedure TCmdBox.MouseDown(Button:TMouseButton;Shift:TShiftState;x,y:Integer);
begin
 SetFocus;
 MoveInputCaretTo(x,y,True);
 FMouseDown:=True;
 SetSelection(-1,0);
 FMouseDownInputPos:=FInputPos;
 Invalidate;
end;

procedure TCmdBox.MouseUp(Button:TMouseButton;Shift:TShiftState;x,y:Integer);
begin
 FMouseDown:=False;
end;

destructor TColorstring.Destroy;
begin
 Clear;
 inherited Destroy;
end;

procedure TCmdBox.ScrollUp;
var n:Integer;
    Firstwidestring : TColorstring;
begin
 Firstwidestring:=FLines[0];
 for n:=0 to Length(FLines)-2 do Flines[n] := FLines[n+1];
 Firstwidestring.Clear;
 Firstwidestring.FDefaultBackGround := FBackGroundColor;
 Flines[High(Flines)]:=Firstwidestring;
end;

procedure TCmdBox.TextColors(FC,BC:TColor);
begin
 FCurrentColor      := FC;
 FCurrentBackGround := BC;
end;

procedure TCmdBox.TextColor(C:TColor);
begin
 FCurrentColor:=C;
end;

procedure TCmdBox.TextBackGround(C:TColor);
begin
 FCurrentBackGround:=C;
end;

procedure TCmdBox.TranslateScrollBarPosition;
var GLine,Line : Integer;
    He         :Integer;
begin
 if (FLineOfTopLine<FLineHeights[FTopLine]) and (FLineHeightSum[FTopLine]+FLineOfTopLine=FVSBPos) then exit;
 UpdateLineHeights;
 Line  := 0;
 GLine := 0;
 He:=FLineHeights[Line];
 while (Line<LineCount-1) and (Gline+He<=FVSBPos) do
 begin
  Inc(Line);
  Inc(Gline,He);
  He:=FLineHeights[Line];
 end;
 FTopLine       := Line;
 FLineOfTopLine := FVSBPos-GLine;
 Invalidate;
end;

procedure TCmdBox.WMVScroll(var message: TLMVScroll);
var CurrentPos :Integer;
begin
 CurrentPos:=FLineHeightSum[FTopLine]+FLineOfTopLine;
 case message.ScrollCode of
  SB_TOP:           CurrentPos := 0;
  SB_BOTTOM:        CurrentPos := FVisibleLineCount-FPageHeight;
  SB_LINEDOWN:      Inc(CurrentPos);
  SB_LINEUP:        Dec(CurrentPos);
  SB_PAGEDOWN:      Inc(CurrentPos,FPageHeight);
  SB_PAGEUP:        Dec(CurrentPos,FPageHeight);
  SB_THUMBPOSITION: CurrentPos := message.Pos;
  SB_THUMBTRACK:    CurrentPos := message.Pos;
  SB_ENDSCROLL:     Exit;
 end;
 
 if CurrentPos<0 then CurrentPos:=0 else if Currentpos>FVisibleLineCount-FPageHeight then CurrentPos:=FVisibleLineCount-FPageHeight;
 {$IFNDEF LCLGTK}
 ScrollBarPosition(SB_VERT, CurrentPos);
 {$ENDIF}

 FVSBPos:=CurrentPos;
 TranslateScrollBarPosition;
end;

procedure TCmdBox.ScrollBarRange(Which: Integer; aRange,aPage: Integer);
var ScrollInfo: TScrollInfo;
begin
 if HandleAllocated then
 begin
  FillChar(ScrollInfo, SizeOf(ScrollInfo), 0);
  ScrollInfo.cbSize := SizeOf(ScrollInfo);
  ScrollInfo.fMask  := SIF_RANGE or SIF_PAGE or SIF_DISABLENOSCROLL;
  // Dont't know, someone told me to kick it...so i did:P
//  {$ifdef Unix}
{  ScrollInfo.fMask := ScrollInfo.fMask or SIF_UPDATEPOLICY;
  if goThumbTracking in Options then
   ScrollInfo.ntrackPos := SB_POLICY_CONTINUOUS
  else
   ScrollInfo.ntrackPos := SB_POLICY_DISCONTINUOUS;}
//  {$endif}}
  ScrollInfo.nMin := 0;
  ScrollInfo.nMax := ARange;
  if APage<0 then APage := 0;
  ScrollInfo.nPage := APage;
  SetScrollInfo(Handle, Which, ScrollInfo, True);
 end;
end;

procedure TCmdBox.ScrollBarPosition(Which, Value: integer);
var ScrollInfo : TScrollInfo;
    Vis        : Boolean;
begin
 if HandleAllocated then
 begin
  if Which = SB_VERT then Vis := FVSbVisible else
  FillChar(ScrollInfo, SizeOf(ScrollInfo), 0);
  ScrollInfo.cbSize := SizeOf(ScrollInfo);
  ScrollInfo.fMask  := SIF_POS;
  ScrollInfo.nPos   := Value;
  SetScrollInfo(Handle, Which, ScrollInfo, Vis);
 end;
end;

function TCmdBox.GetSystemMetricsGapSize(const Index:Integer):Integer;
begin
 {$ifdef LCLWIN32}
 result:=0;
 {$else}
 result:=3;
 {$endif}
end;

procedure TCmdBox.SetBackGroundColor(c:TColor);
begin
 if c<>FBackGroundColor then
 begin
  FBackGroundColor:=c;
  Invalidate;
 end;
end;

procedure TCmdBox.SetFont(F:TFont);
var DC          : HDC;
    Save        : THandle;
    Metrics     : TTextMetric;
begin
 FFont.Assign(F);
 Canvas.Font := FFont;
 DC          := GetDC(0);
 Save        := SelectObject(DC,FFont.Handle);
 GetTextMetrics(DC, Metrics);
 SelectObject(DC, Save);
 ReleaseDC(0, DC);
 FCharHeight := Abs(Metrics.tmHeight)+2;
 Invalidate;
end;

// Still a Bug: Try having a cmdline with more lines than fit on screen : update doesn't work anymore...

procedure TCmdBox.MakeInputVisible;
var y : Integer;
begin
 if not FAutoFollow then Exit;
 UpdateLineHeights;
 y:=FLineHeightSum[FInputY]+FInputBuffer.GetLineOfCaret(FClientWidth,FCaretX,FCaretWidth);
 if y>=FLineHeightSum[FTopLine]+FLineOfTopLine+FPageHeight then
 begin
  While y>=FLineHeightSum[FTopLine]+FLineHeights[FTopLine]+FPageHeight-1 do Inc(FTopLine);
  FLineOfTopLine:=y-(FLineHeightSum[FTopLine]+FPageHeight)+1;
 end
 else if y<FLineHeightSum[FTopLine]+FLineOfTopLine then
 begin
  FLineOfTopLine:=0;
  While y<FLineHeightSum[FTopLine] do Dec(FTopLine);
  FLineOfTopLine:=y-FLineHeightSum[FTopLine];
 end;
 y:=FLineHeightSUm[FTopLine]+FLineOfTopLine;
 if y<>FVSBPos then
 begin
  FVSBPos:=y;
  if HandleAllocated then ScrollBarPosition(SB_Vert,y);
 end;
end;

procedure TCmdBox.MakeOutVisible;
var y : Integer;
begin
 if not FAutoFollow then Exit;
 UpdateLineHeights;
 y:=FLineHeightSum[FOutY]+FLines[FOutY].GetLineOfCaret(FClientWidth,FOutX,FCaretWidth);
 if y>=FLineHeightSum[FTopLine]+FLineOfTopLine+FPageHeight then
 begin
  While y>=FLineHeightSum[FTopLine]+FLineHeights[FTopLine]+FPageHeight-1 do Inc(FTopLine);
  FLineOfTopLine:=y-(FLineHeightSum[FTopLine]+FPageHeight)+1;
 end
 else if y<FLineHeightSum[FTopLine]+FLineOfTopLine then
 begin
  FLineOfTopLine:=0;
  While y<FLineHeightSum[FTopLine] do Dec(FTopLine);
  FLineOfTopLine:=y-FLineHeightSum[FTopLine];
 end;
 y:=FLineHeightSUm[FTopLine]+FLineOfTopLine;
 if y<>FVSBPos then
 begin
  FVSBPos:=y;
  if HandleAllocated then ScrollBarPosition(SB_Vert,y);
 end;
end;

procedure TCmdBox.SetHistoryPos(v:Integer);
begin
 if FInputIsPassWord then Exit;
 if v<0 then v:=FHistoryLength-1 else if v>=FHistoryLength then v:=0;
 if v<>FHistoryPos then
 begin
  if FHistoryPos=0 then
  begin
   FHistory[0].Clear;
   FHistory[0].PartOverWrite(FInputBuffer,FInputMinPos,FInputBuffer.Length,0);
  end;
  FInputBuffer.MaximumLength(FInputMinPos+FHistory[v].Length);
  FInputBuffer.OverWrite(FHistory[v],FInputMinPos);
  if FInputPos>FInputBuffer.Length then
  begin
   FInputPos:=FInputBuffer.Length;
   FCaretX:=FInputX+FInputPos;
  end;
  FHistoryPos:=v;
 end;
 if Assigned(FOnInputChange) then FOnInputChange(Self,FInputBuffer);
 MakeInputVisible;
 AdjustLineHeight(FInputY);
 AdjustScrollBars;
 Invalidate;
end;

procedure TCmdBox.UTF8KeyPress(var Key:TUTF8Char);
begin
 if not FInput then Exit;
 if key>=#32 then
 begin
  if FSelStart<>-1 then DeleteSelected;
  FInputBuffer.Insert(FInputPos,key,FInputColor,FInputBackGround);
  Inc(FInputPos);
  FCaretX:=FInputX+FInputPos;
  FHistoryPos:=0;
  if assigned(FOnInputChange) then FOnInputChange(Self,FInputBuffer);
 end;
 if Assigned(OnAny) then OnAny(Self,FInputBuffer);
 AdjustScrollBars;
 MakeInputVisible;
 If FInputVisible then Invalidate;
end;

procedure TCmdBox.KeyDown(var Key:Word;Shift:TShiftState);
var s : String;
    i : Integer;
begin
 if not FInput then Exit;
 case Key of
  VK_END:
  begin
   key:=0;
   if (not (ssAlt in Shift)) and FInput and (FInputPos<>FInputBuffer.Length) then
   begin
    if not (ssShift in Shift) then SetSelection(-1,0) else RightSelection(FInputPos,FInputBuffer.Length);
    FInputPos:=FInputBuffer.Length;
    FCaretX:=FInputX+FInputPos;
    MakeInputVisible;
    Invalidate;
   end;
  end;
  VK_HOME:
  begin
   key:=0;
   if (not (ssAlt in Shift)) and FInput and (FInputPos<>FInputMinPos) then
   begin
    if not (ssShift in Shift) then SetSelection(-1,0) else LeftSelection(FInputMinPos,FInputPos);
    FInputPos:=FInputMinPos;
    FCaretX:=FInputX+FInputPos;
    MakeInputVisible;
    Invalidate;
   end;
  end;
  VK_LEFT:
  begin
   if (not (ssAlt in Shift)) and (FInput and (FInputPos>FInputMinPos)) then
   begin
    if not (ssShift in Shift) then SetSelection(-1,0) else LeftSelection(FInputPos-1,FInputPos);
    Dec(FInputPos);
    FCaretX:=FInputX+FInputPos;
    MakeInputVisible;
    Invalidate;
   end;
  end;
  VK_UP:
  begin
   if (not (ssAlt in Shift)) and FInput then
   begin
    SetSelection(-1,0);
    SetHistoryPos(FHistoryPos+1);
   end;
  end;
  VK_DOWN:
  begin
   if (not (ssAlt in Shift)) and FInput then
   begin
    SetSelection(-1,0);
    SetHistoryPos(FHistoryPos-1);
   end;
  end;
  VK_RIGHT:
  begin
   if (not (ssAlt in Shift)) and FInput and (FInputPos<FInputBuffer.Length) then
   begin
    if not (ssShift in Shift) then SetSelection(-1,0) else RightSelection(FInputPos,FInputPos+1);
    Inc(FInputPos);
    FCaretX:=FInputX+FInputPos;
    MakeInputVisible;
    Invalidate;
   end;
  end;
  VK_DELETE:
  begin
   if FInput then
   begin
    if FSelStart<>-1 then DeleteSelected else FInputBuffer.Delete(FInputPos);
    FHistoryPos:=0;
    if assigned(FOnInputChange) then FOnInputChange(Self,FInputBuffer);
    MakeInputVisible;
    AdjustLineHeight(FInputY);
    AdjustScrollBars;
   end;
  end;
  VK_RETURN:
  begin
   if FInput then
   begin
    s := FInputBuffer.GetString;
    s := Copy(s,FUTF8InputMinPos+1,Length(s));
    if (FHistoryPos=0) then
    begin
     if (FInputBuffer.Length=FInputMinPos) or FInputIsPassWord then
     begin
      DeleteHistoryEntry(0);
     end
     else
     begin
      i:=HistoryIndexOf(s);
      if i>=0 then
      begin
       DeleteHistoryEntry(0);
       MakeFirstHistoryEntry(i);
      end
      else
      begin
       FHistory[0].Clear;
       FHistory[0].PartOverWrite(FInputBuffer,FInputMinPos,FInputBuffer.Length,0);
      end;
     end;
    end
    else
    begin
     DeleteHistoryEntry(0);
     MakeFirstHistoryEntry(FHistoryPos);
    end;
    FInput  := False;
    if FLines[FOutY].Length<>0 then
    begin
     if FOutY>=FLineCount-1 then
     begin
      ScrollUp;
      Dec(FOutY);
      FInputY:=FOutY;
      AdjustLineHeight(FOutY);
      UpdateLineHeights;
      TranslateScrollBarPosition;
     end;
     FLines[FOutY+1].Clear;
     FLines[FOutY+1].OverWrite(FLines[FOutY],0);
     FLines[FOutY].Clear;
     if FInputIsPassWord then
      FLines[FOutY].OverWritePW(FInputBuffer,FInputMinPos,FInputX,FPassWordChar)
     else FLines[FOutY].OverWrite(FInputBuffer,FInputX);
    end
    else
    begin
     if FInputIsPassWord then
      FLines[FOutY].OverWritePW(FInputBuffer,FInputMinPos,FInputX,FPassWordChar)
     else FLines[FOutY].OverWrite(FInputBuffer,FInputX);
    end;
    Inc(FOutY);
    if FOutY>=FLineCount then
    begin
     ScrollUp;
     Dec(FOutY);
     FInputY:=FOutY;
     AdjustLineHeight(FOutY);
     UpdateLineHeights;
     TranslateScrollBarPosition;
    end;
    FOutX   := 0;
    FCaretX := 0;
    FInputBuffer.Clear;
    if Assigned(OnInput) then OnInput(Self,s);
    if Assigned(OnAny) then OnAny(Self,FInputBuffer);
    AdjustScrollBars;
    Invalidate;
   end;
  end;
  VK_BACK:
  begin
   if FInput then
   begin
    if FSelStart<>-1 then DeleteSelected else
    begin
     If (FInputPos>FInputMinPos) then
     begin
      Dec(FInputPos);
      FInputBuffer.Delete(FInputPos);
      FCaretX:=FInputX+FInputPos;
     end;
    end;
    FHistoryPos:=0;
    if assigned(FOnInputChange) then FOnInputChange(Self,FInputBuffer);
    if Assigned(OnAny) then OnAny(Self,FInputBuffer);
    AdjustScrollBars;
    MakeInputVisible;
    If FInputVisible then Invalidate;
   end;
  end;
  VK_C:
  begin
   if (FInput) and (ssCtrl in Shift) then CopyToClipBoard;
  end;
  VK_V:
  begin
   if (FInput) and (ssCtrl in Shift) then PasteFromClipBoard;
  end;
  VK_X:
  begin
   if (FInput) and (ssCtrl in Shift) then CutToClipBoard;
  end;
  VK_A:
  begin
   if (FInput) and (ssCtrl in Shift) then
   begin
    SetSelection(FInputMinPos,FInputBuffer.Length);
    FInputPos:=FInputBuffer.Length;
    MakeInputVisible;
    if FInputVisible then Invalidate;
   end;
  end;
 end;
 if Assigned(OnAny) then OnAny(Self,FInputBuffer);
end;

procedure TCmdBox.InsertHistory;
var i : Integer;
    t : TColorstring;
begin
 t:=FHistory[FHistoryMax-1];
 for i:=FHistoryMax-2 downto 0 do
 begin
  FHistory[i+1]:=FHistory[i];
 end;
 FHistory[0] := t;
 FHistoryPos := 0;
 If FHistoryLength<FHistoryMax then Inc(FHistoryLength);
end;

procedure TCmdBox.StartRead(DFC,DBC:TColor;const Desc:String;IFC,IBC:TColor);
var Pp,i,l:Integer;
begin
 Inc(FCaretX,UTF8Length(Desc));
 FInputX             := FOutX;
 if FLines[FOutY].Length=0 then FInputY := FOutY else FInputY:=FOutY+1;
 FInputVisible       := True;
 FInput              := True;
 FUTF8InputMinPos    := Length(Desc);
 i:=0;
 Pp:=1;
 while Pp<=Length(Desc) do
 begin
  if Desc[Pp]=#27 then
  begin
   if Pp+1>Length(Desc) then Break;
   case Desc[Pp+1] of
    #9,#10,#32,#46,#196:
    begin
     if Pp+2>Length(Desc) then Break; //Incomplete Escape Seq...ignore
     l:=3;
    end;
    #33,#47,#197:
    begin
     if Pp+3>Length(Desc) then Break; //Incomplete Escape Seq...ignore
     l:=4;
    end;
    else
    begin
     l:=2;
    end;
   end;
  end else l:=UTF8CharacterLength(@Desc[PP]);
  FInputBuffer.OverWriteChar(Copy(Desc,Pp,l),i,FGraphicCharWidth,DFC,DBC);
  Inc(i);
  Inc(Pp,l);
 end;
 FInputPos           := i;
 FInputMinPos        := i;
// FInputBuffer.OverWrite(Desc,0,DFC,DBC);
 FInputIsPassWord    := False;
 FInputColor         := IFC;
 FInputBackground    := IBC;
 FInputBuffer.PassWordStart:=MaxInt;
 InsertHistory;
 MakeInputVisible;
end;

procedure TCmdBox.StartReadPassWord(DFC,DBC:TColor;const Desc:string;IFC,IBC:TColor);
begin
 StartRead(DFC,DBC,Desc,IFC,IBC);
 FInputBuffer.PassWordStart := UTF8Length(Desc);
 FInputBuffer.PassWordChar  := FPassWordChar;
 FInputIsPassWord           := True;
end;

procedure TCmdBox.StopRead;
begin
  FInput:=False;
end;

procedure TCmdBox.DeleteHistoryEntry(i:Integer);
var j:Integer;
    Temp:TColorstring;
begin
 Temp:=FHistory[i];
 for j:=i to FHistoryLength-2 do FHistory[j]:=FHistory[j+1];
 FHistory[FHistoryLength-1]:=Temp;
 Dec(FHistoryLength);
 if FHistoryPos>=i then Dec(FHistoryPos);
end;

procedure TCmdBox.MakeFirstHistoryEntry(i:Integer);
var Temp:TColorstring;
begin
 if FHistoryPos<>0 then
 begin
  Temp:=FHistory[i];
  for i:=i-1 downto 0 do FHistory[i+1]:=FHistory[i];
  FHistory[0]:=Temp;
 end;
end;

procedure TCmdBox.Clear;
var i:Integer;
begin
 for i:=0 to Length(FLines)-1 do Flines[i].Clear;
 FCaretX := 0;
 FInputY := 0;
 FOutX   := 0;
 FOutY   := 0;
 if FInput then FInputY:=0;
 Invalidate;
end;

procedure TCmdBox.Writeln(s:string);
begin
 write(s+#13#10);
end;

const AnsiColors:array['0'..'7'] of TColor=(clBlack,clRed,clGreen,clYellow,clBlue,clFuchsia,clAqua,clWhite);

procedure TCmdBox.IntWrite;
var Pp   : Integer;
    l    : Integer;
    s    : String;
    EscPos     : Integer;
    EscSubMode : Integer;
begin
 S    := FCurrentString;
 Pp   := 1;
 while Pp<=Length(S) do
 begin
  l:=1;
  case FEscapeMode of
   escmNone:
   begin
    if S[Pp]=#27 then
    begin
     case FEscapeCodeType of
      esctCmdBox:
      begin
       FEscapeMode := escmOperation;
       FEscapeData := '';
      end;
      esctAnsi:
      begin
       FEscapeMode := escmAnsiOperation;
       FEscapeData := '';
      end;
      esctNone:
      begin
       // Simply ignore it
      end;
     end;
    end
    else
    begin
     l:=UTF8CharacterLength(@S[Pp]);
     if l=1 then
     begin
      case s[Pp] of
       #13:FOutX:=0;
       #10:
       begin
        AdjustLineHeight(FOutY);
        if FLines[FOutY].Length=0 then FLines[FOutY].DefaultBackGround:=FCurrentBackGround;
        Inc(FOutY);
        if FOutY>=Length(FLines) then
        begin
         ScrollUp;
         Dec(FOutY);
         AdjustLineHeight(FOutY);
         UpdateLineHeights;
         TranslateScrollBarPosition;
        end;
       end;
       else
       begin
        FLines[FOutY].OverWrite(s[Pp],FOutX,FCurrentColor,FCurrentBackGround);
        Inc(FOutX);
       end;
      end;
     end
     else
     begin
      FLines[FOutY].OverWrite(Copy(s,Pp,l),FOutX,FCurrentColor,FCurrentBackGround);
      Inc(FOutX);
     end;
    end;
   end;
   escmOperation:
   begin
    case S[Pp] of
     #9,#10,#32,#46,#196:
     begin
      FEscapeData := S[Pp];
      FEscapeMode := escmData1;
     end;
     #33,#47,#197:
     begin
      FEscapeData := S[Pp];
      FEscapeMode := escmData2;
     end;
     else
     begin
      FLines[FOutY].OverWriteChar(#27+S[Pp],FOutX,FGraphicCharWidth,FCurrentColor,FCurrentBackGround);
      Inc(FOutX);
      FEscapeMode:=escmNone;
     end;
    end;
   end;
   escmData1:
   begin
    FLines[FOutY].OverWriteChar(#27+FEscapeData+S[Pp],FOutX,FGraphicCharWidth,FCurrentColor,FCurrentBackGround);
    Inc(FOutX);
    FEscapeMode:=escmNone;
   end;
   escmData2:
   begin
    FEscapeData := FEscapeData+S[Pp];
    FEscapeMode := escmData1;
   end;
   escmAnsiOperation:
   begin
    case S[Pp] of
     '[':FEscapeMode:=escmAnsiSquare;
     else FEscapeMode:=escmNone;
    end;
   end;
   escmAnsiSquare:
   begin
    case S[Pp] of
     'm':
     begin
      EscPos     := 1;
      EscSubMode := 0;
      while EscPos<=Length(FEscapeData) do
      begin
       case EscSubMode of
        0:
        begin
         case FEscapeData[EscPos] of
          '0':
          begin
           // No Reset Values know here...just assume
           FCurrentColor:=clSilver;
           FCurrentBackGround:=clBlack;
          end;
          '7':
          begin
           // Reverse? What now...
          end;
          '3':EscSubMode:=3;
          '4':EscSubMode:=4;
         end;
        end;
        1:
        begin
         // Just collect the expected ";", not sure what to do if it isn't there...
         EscSubMode:=0;
        end;
        3:
        begin
         if FEscapeData[EscPos] in ['0'..'7'] then FCurrentColor:=AnsiColors[FEscapeData[EscPos]];
         EscSubMode:=1;
        end;
        4:
        begin
         if FEscapeData[EscPos] in ['0'..'7'] then FCurrentBackGround:=AnsiColors[FEscapeData[EscPos]];
         EscSubMode:=1;
        end;
       end;
       Inc(EscPos);
      end;
      FEscapeMode:=escmNone;
     end;
     else
     begin
      FEscapeData:=FEscapeData+S[Pp];
     end;
    end;
   end;
  end;
  Inc(Pp,l);
 end;
 if FInput then
 begin
  if FLines[FOutY].Length=0 then
  begin
   if (FInputY<>FOutY) then FInputY:=FOutY;
  end
  else
  begin
   if FInputY<>FOutY+1 then FInputY:=FOutY+1;
  end;
  if FInputY>=FLineCount then
  begin
   ScrollUp;
   Dec(FOutY);
   Dec(FInputY);
   FInputY:=FOutY;
   AdjustLineHeight(FOutY);
   UpdateLineHeights;
   TranslateScrollBarPosition;
  end;
  MakeInputVisible;
 end else MakeOutVisible;
 AdjustLineHeight(FOutY);
 if not FInput then FCaretX:=FOutX;
 AdjustScrollBars;
end;

procedure TCmdBox.SetOutY(v:Integer);
begin
 if v>FLineCount-1 then v:=FLineCount-1;
 FOutY:=v;
end;

procedure TCmdBox.Resize;
begin
 inherited Resize;
 AdjustScrollBars;
 if FVSBPos>=FVisibleLineCount-FPageHeight then
 begin
  FVSBPos:=FVisibleLineCount-FPageHeight;
  if FVSBPos<0 then FVSBPos:=0;
 end;
 TranslateScrollBarPosition;
end;

function TCmdBox.AdjustLineHeight(i:Integer):Integer;
var LineC  : Integer;
    LineC2 : Integer;
begin
 if (FInputY=i) then
 begin
  LineC  := FLines[i].LineCount(FClientWidth,-1,FCaretWidth);
  LineC2 := FInputBuffer.LineCount(FClientWidth,FCaretX,FCaretWidth);
  if LineC2>LineC then LineC:=LineC2;
 end else LineC:=FLines[i].LineCount(FClientWidth,-1,FCaretWidth);
 Result          := LineC;
 FLineHeights[i] := Result;
end;

function TCmdBox.UpdateLineHeights:integer;
var i:integer;
begin
 Result:=0;
 for i:=0 to FLineCount-1 do
 begin
  FLineHeightSum[i]:=Result;
  Inc(Result,AdjustLineHeight(i));
 end;
end;

procedure TCmdBox.AdjustScrollBars;
var LH : Integer;
begin
 FClientWidth  := Width-FVSBWidth;
 FClientHeight := Height;
 FPageHeight   := FClientHeight div FCharHeight;
 FVisibleLines := FPageHeight+ord(FClientHeight mod FCharHeight<>0);
 LH            := UpdateLineHeights;
 if LH<>FVisibleLineCount then
 begin
  FVisibleLineCount:=LH;
  if FVisibleLineCount<=FVSBPos+FPageHeight then
  begin
   FVSBPos:=FVisibleLineCount-FPageHeight;
   if FVSBPos<0 then FVSBPos:=0;
   if HandleAllocated then ScrollBarPosition(SB_Vert,FVSBPos);
   TranslateScrollBarPosition;
  end;
 end;
 if FVisibleLineCount<FPageHeight then
 begin
  if HandleAllocated then
  begin
   ScrollBarPosition(SB_VERT,0);
   ScrollBarRange(SB_VERT,0,FPageHeight);
   ShowScrollBar(Handle,SB_VERT,True); { Disable the Scrollbar ! }
  end;
 end
 else
 begin
  if HandleAllocated then
  begin
   ScrollBarRange(SB_VERT,FVisibleLineCount,FPageHeight);
   ShowScrollBar(Handle,SB_VERT,True);
  end;
 end;
 Invalidate;
end;

procedure TCmdBox.SetTopLine(Nr:Integer);
begin
 if Nr<>FTopLine then
 begin
  FTopLine:=Nr;
  AdjustScrollBars;
 end;
end;

procedure TCmdBox.SetLineCount(c: Integer);
var i:Integer;
begin
 if c<1 then c:=1;
 if c<>FLineCount then
 begin
  for i:=0 to FLineCount-1 do FLines[i].Free;
  FLineCount:=c;
  SetLength(FLines,FLinecount);
  for i:=0 to FlineCount-1 do
  begin
   FLines[i]:=TColorstring.Create(Canvas.Font);
   FLines[i].DefaultBackGround:=FBackGroundColor;
   FLines[i].TabWidth:=FTabWidth;
  end;
  SetLength(FLineHeights,FLineCount);
  SetLength(FLineHeightSum,FLineCount);
  AdjustScrollBars;
 end;
end;

procedure TCmdBox.Paint;
Var y,m         : Integer;
    CurrentLine : Integer;
begin
 inherited Paint;
 with canvas do
 begin
  if (csdesigning in componentstate) then
  begin
   Brush.Style := bsSolid;
   Brush.Color := clBlack;
   FillRect(0,0,FClientWidth,FClientHeight);
   exit;
  end;
  Font        := FFont;
  Brush.Style := bsSolid;
  m           := FVisibleLines-1;
  y           := -FLineOfTopLine;
  CurrentLine := FTopLine;
  while (y<=m) and (CurrentLine<LineCount) do
  begin
   FLines[CurrentLine].LineOutAndFill(Canvas,0,y*FCharHeight,0,FClientWidth,FCharHeight,FGraphicCharWidth,-1,FBackGroundColor,FCaretColor,FCaretHeight,FCaretWidth,FCaretYShift,False);
   if (FInput) and (FInputY=CurrentLine) then
   begin
    if FInputIsPassWord then
    begin
     FInputBuffer.LineOutAndFill(Canvas,0,y*FCharHeight,0,FClientWidth,FCharHeight,FGraphicCharWidth,FCaretX,FBackGroundColor,FCaretColor,FCaretHeight,FCaretWidth,FCaretYShift,FCaretVisible and Focused);
    end
    else
    begin
     FInputBuffer.LineOutAndFill(Canvas,0,y*FCharHeight,0,FClientWidth,FCharHeight,FGraphicCharWidth,FCaretX,FBackGroundColor,FCaretColor,FCaretHeight,FCaretWidth,FCaretYShift,FCaretVisible and Focused);
    end;
   end;
   Inc(y,FLineHeights[CurrentLine]);
   Inc(CurrentLine);
  end;
  y:=y*FCharHeight;
  if y<FClientHeight then
  begin
   Brush.Color := FBackGroundColor;
   Brush.Style := bsSolid;
   FillRect(0,y,FClientWidth,FClientHeight);
  end;
 end;
end;

procedure TCmdBox.CaretTimerExecute(Sender:TObject);
begin
 if Focused then
 begin
  if not Assigned(WakeMainThread) then MultiWrite;
  FCaretVisible:=not FCaretVisible;
  Invalidate;
 end;
end;

procedure TCmdBox.CreateWnd;
begin
 inherited CreateWnd;
 FVSBWidth  := GetSystemMetrics(SM_CXVSCROLL) + GetSystemMetricsGapSize(SM_CXVSCROLL);
 SetFont(FFont);
 if FCaretHeight=-1 then FCaretHeight := FFont.GetTextHeight('A')-3;
 { Little Hack to prevent "grey bar" Scrollbar at StartUp }
 ShowScrollBar(Handle,SB_VERT,False);
 ShowScrollBar(Handle,SB_VERT,True);
 AdjustScrollBars;
end;

procedure TCmdBox.CreateParams(var Params:TCreateParams);
const ClassStylesOff = CS_VREDRAW or CS_HREDRAW;
begin
 inherited CreateParams(Params);
 with Params do
 begin
  WindowClass.Style:=WIndowClass.Style and DWORD(not ClassStylesOff);
  Style := Style or WS_VSCROLL or WS_HSCROLL or WS_CLIPCHILDREN;
 end;
end;

constructor TCmdBox.Create(AComponent:TComponent);
var i:integer;
begin
 inherited Create(AComponent);
 System.InitCriticalSection(FLock);
 FStringBuffer          := TStringList.Create;
 FCharHeight            := 15; // Just a random value to prevent stupid exceptions
 FSelStart              := -1;
 FLineCount             := 1000;
 FInputVisible          := False;
 FBackGroundColor       := clBlack;
 FGraphicCharWidth      := 10;
 FInputBuffer           := TColorstring.Create(Canvas.Font);
 FEscapeCodeType        := esctCmdBox;
 FAutoFollow            := True;
 SetLength(FLines,FLineCount);
 SetLength(FLineHeights,FLineCount);
 SetLength(FLineHeightSum,FLineCount);
 FTabWidth              := 60;
 for i:=0 to FLineCount-1 do
 begin
  FLines[i]                   := TColorstring.Create(Canvas.Font);
  FLines[i].DefaultBackGround := FBackGroundColor;
  FLines[i].TabWidth          := FTabWidth;
 end;
 FCaretTimer            := TTimer.Create(self);
 FCaretTimer.Interval   := 500;
 FCaretTimer.OnTimer    := @carettimerexecute;
 FCaretTimer.Enabled    := True;
 FCaretVisible          := True;
 FVSBVisible            := True;
 FFont                  := Canvas.Font;
 FCurrentColor          := clSilver;
 FCurrentBackground     := clBlack;
 DoubleBuffered         := True;
 FFont.Color            := ClSilver;
 FCaretColor            := clWhite;
 FCaretType             := cartLine;
 FCaretWidth            := 1;
 FCaretHeight           := -1;
 FCaretYShift           := 3;
 FInputSelBackground    := clWhite;
 FInputSelColor         := clBlue;
 FHistoryMax            := 10;
 FHistoryLength         := 0;
 SetBounds(0,0,200,200);
 SetLength(FHistory,FHistoryMax);
 for i:=0 to FHistoryMax-1 do FHistory[i]:=TColorstring.Create(Canvas.Font);
end;

destructor TCmdBox.Destroy;
var
  i : Integer;
begin
 FCaretTimer.Enabled := False;
 System.DoneCriticalSection(FLock);
 FInputBuffer.Free;
 FStringBuffer.Free;
 for i:=0 to Length(FLines)-1 do FLines[i].Free;
 for i:=0 to Length(FHistory)-1 do FHistory[i].Free;
 inherited Destroy;
end;

procedure Register;
begin
 RegisterComponents('Other',[TCmdBox]);
end;

initialization
 {$I tcmdbox.lrs}
end.