summaryrefslogtreecommitdiff
path: root/indra/llui/lltextbase.cpp
blob: 3c284b3f038ef4d068c0fe48a1f473cbd9bc6701 (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
/** 
 * @file lltextbase.cpp
 * @author Martin Reddy
 * @brief The base class of text box/editor, providing Url handling support
 *
 * $LicenseInfo:firstyear=2009&license=viewerlgpl$
 * Second Life Viewer Source Code
 * Copyright (C) 2009-2010, Linden Research, Inc.
 * 
 * This library 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;
 * version 2.1 of the License only.
 * 
 * This library 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.
 * 
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 * 
 * Linden Research, Inc., 945 Battery Street, San Francisco, CA  94111  USA
 * $/LicenseInfo$
 */

#include "linden_common.h"

#include "lltextbase.h"

#include "lllocalcliprect.h"
#include "llmenugl.h"
#include "llscrollcontainer.h"
#include "llspellcheck.h"
#include "llstl.h"
#include "lltextparser.h"
#include "lltextutil.h"
#include "lltooltip.h"
#include "lluictrl.h"
#include "llurlaction.h"
#include "llurlregistry.h"
#include "llview.h"
#include "llwindow.h"
#include <boost/bind.hpp>

const F32	CURSOR_FLASH_DELAY = 1.0f;  // in seconds
const S32	CURSOR_THICKNESS = 2;
const F32	TRIPLE_CLICK_INTERVAL = 0.3f;	// delay between double and triple click.

LLTextBase::line_info::line_info(S32 index_start, S32 index_end, LLRect rect, S32 line_num) 
:	mDocIndexStart(index_start), 
	mDocIndexEnd(index_end),
	mRect(rect),
	mLineNum(line_num)
{}

bool LLTextBase::compare_segment_end::operator()(const LLTextSegmentPtr& a, const LLTextSegmentPtr& b) const
{
	// sort empty spans (e.g. 11-11) after previous non-empty spans (e.g. 5-11)
	if (a->getEnd() == b->getEnd())
	{
		return a->getStart() < b->getStart();
	}
	else
	{
		return a->getEnd() < b->getEnd();
	}
}


// helper functors
struct LLTextBase::compare_bottom
{
	bool operator()(const S32& a, const LLTextBase::line_info& b) const
	{
		return a > b.mRect.mBottom; // bottom of a is higher than bottom of b
	}

	bool operator()(const LLTextBase::line_info& a, const S32& b) const
	{
		return a.mRect.mBottom > b; // bottom of a is higher than bottom of b
	}

	bool operator()(const LLTextBase::line_info& a, const LLTextBase::line_info& b) const
	{
		return a.mRect.mBottom > b.mRect.mBottom; // bottom of a is higher than bottom of b
	}

};

// helper functors
struct LLTextBase::compare_top
{
	bool operator()(const S32& a, const LLTextBase::line_info& b) const
	{
		return a > b.mRect.mTop; // top of a is higher than top of b
	}

	bool operator()(const LLTextBase::line_info& a, const S32& b) const
	{
		return a.mRect.mTop > b; // top of a is higher than top of b
	}

	bool operator()(const LLTextBase::line_info& a, const LLTextBase::line_info& b) const
	{
		return a.mRect.mTop > b.mRect.mTop; // top of a is higher than top of b
	}
};

struct LLTextBase::line_end_compare
{
	bool operator()(const S32& pos, const LLTextBase::line_info& info) const
	{
		return (pos < info.mDocIndexEnd);
	}

	bool operator()(const LLTextBase::line_info& info, const S32& pos) const
	{
		return (info.mDocIndexEnd < pos);
	}

	bool operator()(const LLTextBase::line_info& a, const LLTextBase::line_info& b) const
	{
		return (a.mDocIndexEnd < b.mDocIndexEnd);
	}

};

//////////////////////////////////////////////////////////////////////////
//
// LLTextBase
//

// register LLTextBase::Params under name "textbase"
static LLWidgetNameRegistry::StaticRegistrar sRegisterTextBaseParams(&typeid(LLTextBase::Params), "textbase");

LLTextBase::LineSpacingParams::LineSpacingParams()
:	multiple("multiple", 1.f),
	pixels("pixels", 0)
{
}


LLTextBase::Params::Params()
:	cursor_color("cursor_color"),
	text_color("text_color"),
	text_readonly_color("text_readonly_color"),
	text_tentative_color("text_tentative_color"),
	bg_visible("bg_visible", false),
	border_visible("border_visible", false),
	bg_readonly_color("bg_readonly_color"),
	bg_writeable_color("bg_writeable_color"),
	bg_focus_color("bg_focus_color"),
	text_selected_color("text_selected_color"),
	bg_selected_color("bg_selected_color"),
	allow_scroll("allow_scroll", true),
	plain_text("plain_text",false),
	track_end("track_end", false),
	read_only("read_only", false),
	spellcheck("spellcheck", false),
	v_pad("v_pad", 0),
	h_pad("h_pad", 0),
	clip("clip", true),
	clip_partial("clip_partial", true),
	line_spacing("line_spacing"),
	max_text_length("max_length", 255),
	font_shadow("font_shadow"),
	wrap("wrap"),
	use_ellipses("use_ellipses", false),
	parse_urls("parse_urls", false),
	parse_highlights("parse_highlights", false)
{
	addSynonym(track_end, "track_bottom");
	addSynonym(wrap, "word_wrap");
	addSynonym(parse_urls, "allow_html");
}


LLTextBase::LLTextBase(const LLTextBase::Params &p) 
:	LLUICtrl(p, LLTextViewModelPtr(new LLTextViewModel)),
	mURLClickSignal(NULL),
	mIsFriendSignal(NULL),
	mMaxTextByteLength( p.max_text_length ),
	mFont(p.font),
	mFontShadow(p.font_shadow),
	mPopupMenu(NULL),
	mReadOnly(p.read_only),
	mSpellCheck(p.spellcheck),
	mSpellCheckStart(-1),
	mSpellCheckEnd(-1),
	mCursorColor(p.cursor_color),
	mFgColor(p.text_color),
	mBorderVisible( p.border_visible ),
	mReadOnlyFgColor(p.text_readonly_color),
	mTentativeFgColor(p.text_tentative_color()),
	mWriteableBgColor(p.bg_writeable_color),
	mReadOnlyBgColor(p.bg_readonly_color),
	mFocusBgColor(p.bg_focus_color),
	mTextSelectedColor(p.text_selected_color),
	mSelectedBGColor(p.bg_selected_color),
	mReflowIndex(S32_MAX),
	mCursorPos( 0 ),
	mScrollNeeded(FALSE),
	mDesiredXPixel(-1),
	mHPad(p.h_pad),
	mVPad(p.v_pad),
	mHAlign(p.font_halign),
	mVAlign(p.font_valign),
	mLineSpacingMult(p.line_spacing.multiple),
	mLineSpacingPixels(p.line_spacing.pixels),
	mClip(p.clip),
	mClipPartial(p.clip_partial && !p.allow_scroll),
	mTrackEnd( p.track_end ),
	mScrollIndex(-1),
	mSelectionStart( 0 ),
	mSelectionEnd( 0 ),
	mIsSelecting( FALSE ),
	mPlainText ( p.plain_text ),
	mWordWrap(p.wrap),
	mUseEllipses( p.use_ellipses ),
	mParseHTML(p.parse_urls),
	mParseHighlights(p.parse_highlights),
	mBGVisible(p.bg_visible),
	mScroller(NULL),
	mStyleDirty(true)
{
	if(p.allow_scroll)
	{
		LLScrollContainer::Params scroll_params;
		scroll_params.name = "text scroller";
		scroll_params.rect = getLocalRect();
		scroll_params.follows.flags = FOLLOWS_ALL;
		scroll_params.is_opaque = false;
		scroll_params.mouse_opaque = false;
		scroll_params.min_auto_scroll_rate = 200;
		scroll_params.max_auto_scroll_rate = 800;
		scroll_params.border_visible = p.border_visible;
		mScroller = LLUICtrlFactory::create<LLScrollContainer>(scroll_params);
		addChild(mScroller);
	}

	LLView::Params view_params;
	view_params.name = "text_contents";
	view_params.rect =  LLRect(0, 500, 500, 0);
	view_params.mouse_opaque = false;

	mDocumentView = LLUICtrlFactory::create<LLView>(view_params);
	if (mScroller)
	{
		mScroller->addChild(mDocumentView);
	}
	else
	{
		addChild(mDocumentView);
	}

	if (mSpellCheck)
	{
		LLSpellChecker::setSettingsChangeCallback(boost::bind(&LLTextBase::onSpellCheckSettingsChange, this));
	}
	mSpellCheckTimer.reset();

	createDefaultSegment();

	updateRects();
}

LLTextBase::~LLTextBase()
{
	mSegments.clear();
	delete mURLClickSignal;
}

void LLTextBase::initFromParams(const LLTextBase::Params& p)
{
	LLUICtrl::initFromParams(p);
	resetDirty();		// Update saved text state
	updateSegments();

	// HACK: work around enabled == readonly design bug -- RN
	// setEnabled will modify our read only status, so do this after
	// LLTextBase::initFromParams
	if (p.read_only.isProvided())
	{
		mReadOnly = p.read_only;
	}
}

bool LLTextBase::truncate()
{
	BOOL did_truncate = FALSE;

	// First rough check - if we're less than 1/4th the size, we're OK
	if (getLength() >= S32(mMaxTextByteLength / 4))
	{	
		// Have to check actual byte size
		S32 utf8_byte_size = 0;
		LLSD value = getViewModel()->getValue();
		if (value.type() == LLSD::TypeString)
		{
			// save a copy for strings.
			utf8_byte_size = value.size();
		}
		else
		{
			// non string LLSDs need explicit conversion to string
			utf8_byte_size = value.asString().size();
		}

		if ( utf8_byte_size > mMaxTextByteLength )
		{
			// Truncate safely in UTF-8
			std::string temp_utf8_text = value.asString();
			temp_utf8_text = utf8str_truncate( temp_utf8_text, mMaxTextByteLength );
			LLWString text = utf8str_to_wstring( temp_utf8_text );
			// remove extra bit of current string, to preserve formatting, etc.
			removeStringNoUndo(text.size(), getWText().size() - text.size());
			did_truncate = TRUE;
		}
	}

	return did_truncate;
}

const LLStyle::Params& LLTextBase::getStyleParams()
{
	//FIXME: convert mDefaultStyle to a flyweight http://www.boost.org/doc/libs/1_40_0/libs/flyweight/doc/index.html
	//and eliminate color member values
	if (mStyleDirty)
	{
		  mStyle
				  .color(LLUIColor(&mFgColor))						// pass linked color instead of copy of mFGColor
				  .readonly_color(LLUIColor(&mReadOnlyFgColor))
				  .selected_color(LLUIColor(&mTextSelectedColor))
				  .font(mFont)
				  .drop_shadow(mFontShadow);
		  mStyleDirty = false;
	}
	return mStyle;
}

void LLTextBase::beforeValueChange()
{

}

void LLTextBase::onValueChange(S32 start, S32 end)
{
}


// Draws the black box behind the selected text
void LLTextBase::drawSelectionBackground()
{
	// Draw selection even if we don't have keyboard focus for search/replace
	if( hasSelection() && !mLineInfoList.empty())
	{
		std::vector<LLRect> selection_rects;

		S32 selection_left		= llmin( mSelectionStart, mSelectionEnd );
		S32 selection_right		= llmax( mSelectionStart, mSelectionEnd );

		// Skip through the lines we aren't drawing.
		LLRect content_display_rect = getVisibleDocumentRect();

		// binary search for line that starts before top of visible buffer
		line_list_t::const_iterator line_iter = std::lower_bound(mLineInfoList.begin(), mLineInfoList.end(), content_display_rect.mTop, compare_bottom());
		line_list_t::const_iterator end_iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), content_display_rect.mBottom, compare_top());

		bool done = false;

		// Find the coordinates of the selected area
		for (;line_iter != end_iter && !done; ++line_iter)
		{
			// is selection visible on this line?
			if (line_iter->mDocIndexEnd > selection_left && line_iter->mDocIndexStart < selection_right)
			{
				segment_set_t::iterator segment_iter;
				S32 segment_offset;
				getSegmentAndOffset(line_iter->mDocIndexStart, &segment_iter, &segment_offset);
				
				LLRect selection_rect;
				selection_rect.mLeft = line_iter->mRect.mLeft;
				selection_rect.mRight = line_iter->mRect.mLeft;
				selection_rect.mBottom = line_iter->mRect.mBottom;
				selection_rect.mTop = line_iter->mRect.mTop;
					
				for(;segment_iter != mSegments.end(); ++segment_iter, segment_offset = 0)
				{
					LLTextSegmentPtr segmentp = *segment_iter;

					S32 segment_line_start = segmentp->getStart() + segment_offset;
					S32 segment_line_end = llmin(segmentp->getEnd(), line_iter->mDocIndexEnd);

					if (segment_line_start > segment_line_end) break;

					S32 segment_width = 0;
					S32 segment_height = 0;

					// if selection after beginning of segment
					if(selection_left >= segment_line_start)
					{
						S32 num_chars = llmin(selection_left, segment_line_end) - segment_line_start;
						segmentp->getDimensions(segment_offset, num_chars, segment_width, segment_height);
						selection_rect.mLeft += segment_width;
					}

					// if selection_right == segment_line_end then that means we are the first character of the next segment
					// or first character of the next line, in either case we want to add the length of the current segment
					// to the selection rectangle and continue.
					// if selection right > segment_line_end then selection spans end of current segment...
					if (selection_right >= segment_line_end)
					{
						// extend selection slightly beyond end of line
						// to indicate selection of newline character (use "n" character to determine width)
						S32 num_chars = segment_line_end - segment_line_start;
						segmentp->getDimensions(segment_offset, num_chars, segment_width, segment_height);
						selection_rect.mRight += segment_width;
					}
					// else if selection ends on current segment...
					else
					{
						S32 num_chars = selection_right - segment_line_start;
						segmentp->getDimensions(segment_offset, num_chars, segment_width, segment_height);
						selection_rect.mRight += segment_width;

						break;
					}
				}
				selection_rects.push_back(selection_rect);
			}
		}
		
		// Draw the selection box (we're using a box instead of reversing the colors on the selected text).
		gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);
		const LLColor4& color = mSelectedBGColor;
		F32 alpha = hasFocus() ? 0.7f : 0.3f;
		alpha *= getDrawContext().mAlpha;
		LLColor4 selection_color(color.mV[VRED], color.mV[VGREEN], color.mV[VBLUE], alpha);

		for (std::vector<LLRect>::iterator rect_it = selection_rects.begin();
			rect_it != selection_rects.end();
			++rect_it)
		{
			LLRect selection_rect = *rect_it;
			selection_rect = *rect_it;
			selection_rect.translate(mVisibleTextRect.mLeft - content_display_rect.mLeft, mVisibleTextRect.mBottom - content_display_rect.mBottom);
			gl_rect_2d(selection_rect, selection_color);
		}
	}
}

void LLTextBase::drawCursor()
{
	F32 alpha = getDrawContext().mAlpha;

	if( hasFocus()
		&& gFocusMgr.getAppHasFocus()
		&& !mReadOnly)
	{
		const LLWString &wtext = getWText();
		const llwchar* text = wtext.c_str();

		LLRect cursor_rect = getLocalRectFromDocIndex(mCursorPos);
		cursor_rect.translate(-1, 0);
		segment_set_t::iterator seg_it = getSegIterContaining(mCursorPos);

		// take style from last segment
		LLTextSegmentPtr segmentp;

		if (seg_it != mSegments.end())
		{
			segmentp = *seg_it;
		}
		else
		{
			return;
		}

		// Draw the cursor
		// (Flash the cursor every half second starting a fixed time after the last keystroke)
		F32 elapsed = mCursorBlinkTimer.getElapsedTimeF32();
		if( (elapsed < CURSOR_FLASH_DELAY ) || (S32(elapsed * 2) & 1) )
		{

			if (LL_KIM_OVERWRITE == gKeyboard->getInsertMode() && !hasSelection())
			{
				S32 segment_width = 0;
				S32 segment_height = 0;
				segmentp->getDimensions(mCursorPos - segmentp->getStart(), 1, segment_width, segment_height);
				S32 width = llmax(CURSOR_THICKNESS, segment_width);
				cursor_rect.mRight = cursor_rect.mLeft + width;
			}
			else
			{
				cursor_rect.mRight = cursor_rect.mLeft + CURSOR_THICKNESS;
			}
			
			gGL.getTexUnit(0)->unbind(LLTexUnit::TT_TEXTURE);

			LLColor4 cursor_color = mCursorColor.get() % alpha;
			gGL.color4fv( cursor_color.mV );
			
			gl_rect_2d(cursor_rect);

			if (LL_KIM_OVERWRITE == gKeyboard->getInsertMode() && !hasSelection() && text[mCursorPos] != '\n')
			{
				LLColor4 text_color;
				const LLFontGL* fontp;
				text_color = segmentp->getColor();
				fontp = segmentp->getStyle()->getFont();
				fontp->render(text, mCursorPos, cursor_rect, 
					LLColor4(1.f - text_color.mV[VRED], 1.f - text_color.mV[VGREEN], 1.f - text_color.mV[VBLUE], alpha),
					LLFontGL::LEFT, mVAlign,
					LLFontGL::NORMAL,
					LLFontGL::NO_SHADOW,
					1);
			}

			// Make sure the IME is in the right place
			LLRect screen_pos = calcScreenRect();
			LLCoordGL ime_pos( screen_pos.mLeft + llfloor(cursor_rect.mLeft), screen_pos.mBottom + llfloor(cursor_rect.mTop) );

			ime_pos.mX = (S32) (ime_pos.mX * LLUI::getScaleFactor().mV[VX]);
			ime_pos.mY = (S32) (ime_pos.mY * LLUI::getScaleFactor().mV[VY]);
			getWindow()->setLanguageTextInput( ime_pos );
		}
	}
}

void LLTextBase::drawText()
{
	S32 text_len = getLength();

	if (text_len <= 0 && mLabel.empty())
	{
		return;
	}
	else if (useLabel())
	{
		text_len = mLabel.getWString().length();
	}

	S32 selection_left = -1;
	S32 selection_right = -1;
	// Draw selection even if we don't have keyboard focus for search/replace
	if( hasSelection())
	{
		selection_left = llmin( mSelectionStart, mSelectionEnd );
		selection_right = llmax( mSelectionStart, mSelectionEnd );
	}

	std::pair<S32, S32> line_range = getVisibleLines(mClipPartial);
	S32 first_line = line_range.first;
	S32 last_line = line_range.second;
	if (first_line >= last_line)
	{
		return;
	}
	
	S32 line_start = getLineStart(first_line);
	// find first text segment that spans top of visible portion of text buffer
	segment_set_t::iterator seg_iter = getSegIterContaining(line_start);
	if (seg_iter == mSegments.end()) 
	{
		return;
	}

	// Perform spell check if needed
	if ( (getSpellCheck()) && (getWText().length() > 2) )
	{
		// Calculate start and end indices for the spell checking range
		S32 start = line_start, end = getLineEnd(last_line);

		if ( (mSpellCheckStart != start) || (mSpellCheckEnd != end) )
		{
			const LLWString& wstrText = getWText(); 
			mMisspellRanges.clear();

			segment_set_t::const_iterator seg_it = getSegIterContaining(start);
			while (mSegments.end() != seg_it)
			{
				LLTextSegmentPtr text_segment = *seg_it;
				if ( (text_segment.isNull()) || (text_segment->getStart() >= end) )
				{
					break;
				}

				if (!text_segment->canEdit())
				{
					++seg_it;
					continue;
				}

				// Combine adjoining text segments into one
				U32 seg_start = text_segment->getStart(), seg_end = llmin(text_segment->getEnd(), end);
				while (mSegments.end() != ++seg_it)
				{
					text_segment = *seg_it;
					if ( (text_segment.isNull()) || (!text_segment->canEdit()) || (text_segment->getStart() >= end) )
					{
						break;
					}
					seg_end = llmin(text_segment->getEnd(), end);
				}

				// Find the start of the first word
				U32 word_start = seg_start, word_end = -1;
				U32 text_length = wstrText.length();
				while ( (word_start < text_length) && (!LLStringOps::isAlpha(wstrText[word_start])) )
				{
					word_start++;
				}

				// Iterate over all words in the text block and check them one by one
				while (word_start < seg_end)
				{
					// Find the end of the current word (special case handling for "'" when it's used as a contraction)
					word_end = word_start + 1;
					while ( (word_end < seg_end) && 
							((LLWStringUtil::isPartOfWord(wstrText[word_end])) ||
								((L'\'' == wstrText[word_end]) && 
								(LLStringOps::isAlnum(wstrText[word_end - 1])) && (LLStringOps::isAlnum(wstrText[word_end + 1])))) )
					{
						word_end++;
					}
					if (word_end > seg_end)
					{
						break;
					}

					if (word_start < text_length && word_end <= text_length && word_end > word_start)
					{
						std::string word = wstring_to_utf8str(wstrText.substr(word_start, word_end - word_start));

						// Don't process words shorter than 3 characters
						if ( (word.length() >= 3) && (!LLSpellChecker::instance().checkSpelling(word)) )
						{
							mMisspellRanges.push_back(std::pair<U32, U32>(word_start, word_end));
						}
					}

					// Find the start of the next word
					word_start = word_end + 1;
					while ( (word_start < seg_end) && (!LLWStringUtil::isPartOfWord(wstrText[word_start])) )
					{
						word_start++;
					}
				}
			}

			mSpellCheckStart = start;
			mSpellCheckEnd = end;
		}
	}
	else
	{
		mMisspellRanges.clear();
	}

	LLTextSegmentPtr cur_segment = *seg_iter;

	std::list<std::pair<U32, U32> >::const_iterator misspell_it = std::lower_bound(mMisspellRanges.begin(), mMisspellRanges.end(), std::pair<U32, U32>(line_start, 0));
	for (S32 cur_line = first_line; cur_line < last_line; cur_line++)
	{
		S32 next_line = cur_line + 1;
		line_info& line = mLineInfoList[cur_line];

		S32 next_start = -1;
		S32 line_end = text_len;

		if (next_line < getLineCount())
		{
			next_start = getLineStart(next_line);
			line_end = next_start;
		}

		LLRect text_rect(line.mRect);
		text_rect.mRight = mDocumentView->getRect().getWidth(); // clamp right edge to document extents
		text_rect.translate(mDocumentView->getRect().mLeft, mDocumentView->getRect().mBottom); // adjust by scroll position

		// draw a single line of text
		S32 seg_start = line_start;
		while( seg_start < line_end )
		{
			while( cur_segment->getEnd() <= seg_start )
			{
				seg_iter++;
				if (seg_iter == mSegments.end())
				{
					llwarns << "Ran off the segmentation end!" << llendl;

					return;
				}
				cur_segment = *seg_iter;
			}
			
			S32 seg_end = llmin(line_end, cur_segment->getEnd());
			S32 clipped_end	= seg_end - cur_segment->getStart();

			if (mUseEllipses								// using ellipses
				&& clipped_end == line_end					// last segment on line
				&& next_line == last_line					// this is the last visible line
				&& last_line < (S32)mLineInfoList.size())	// and there is more text to display
			{
				// more lines of text to go, but we can't fit them
				// so shrink text rect to force ellipses
				text_rect.mRight -= 2;
			}

			// Draw squiggly lines under any visible misspelled words
			while ( (mMisspellRanges.end() != misspell_it) && (misspell_it->first < seg_end) && (misspell_it->second > seg_start) )
			{
				// Skip the current word if the user is still busy editing it
				if ( (!mSpellCheckTimer.hasExpired()) && (misspell_it->first <= (U32)mCursorPos) && (misspell_it->second >= (U32)mCursorPos) )
				{
					++misspell_it;
 					continue;
				}

				U32 misspell_start = llmax<U32>(misspell_it->first, seg_start), misspell_end = llmin<U32>(misspell_it->second, seg_end);
				S32 squiggle_start = 0, squiggle_end = 0, pony = 0;
				cur_segment->getDimensions(seg_start - cur_segment->getStart(), misspell_start - seg_start, squiggle_start, pony);
				cur_segment->getDimensions(misspell_start - cur_segment->getStart(), misspell_end - misspell_start, squiggle_end, pony);
				squiggle_start += text_rect.mLeft;

				pony = (squiggle_end + 3) / 6;
				squiggle_start += squiggle_end / 2 - pony * 3;
				squiggle_end = squiggle_start + pony * 6;

				S32 squiggle_bottom = text_rect.mBottom + (S32)cur_segment->getStyle()->getFont()->getDescenderHeight();

				gGL.color4ub(255, 0, 0, 200);
				while (squiggle_start + 1 < squiggle_end)
				{
					gl_line_2d(squiggle_start, squiggle_bottom, squiggle_start + 2, squiggle_bottom - 2);
					if (squiggle_start + 3 < squiggle_end)
					{
						gl_line_2d(squiggle_start + 2, squiggle_bottom - 3, squiggle_start + 4, squiggle_bottom - 1);
					}
					squiggle_start += 4;
				}

				if (misspell_it->second > seg_end)
				{
					break;
				}
				++misspell_it;
			}

			text_rect.mLeft = (S32)(cur_segment->draw(seg_start - cur_segment->getStart(), clipped_end, selection_left, selection_right, text_rect));

			seg_start = clipped_end + cur_segment->getStart();
		}

		line_start = next_start;
	}
}

///////////////////////////////////////////////////////////////////
// Returns change in number of characters in mWText

S32 LLTextBase::insertStringNoUndo(S32 pos, const LLWString &wstr, LLTextBase::segment_vec_t* segments )
{
    beforeValueChange();

	S32 old_len = getLength();		// length() returns character length
	S32 insert_len = wstr.length();

	pos = getEditableIndex(pos, true);

	segment_set_t::iterator seg_iter = getEditableSegIterContaining(pos);

	LLTextSegmentPtr default_segment;

	LLTextSegmentPtr segmentp;
	if (seg_iter != mSegments.end())
	{
		segmentp = *seg_iter;
	}
	else
	{
		//segmentp = mSegments.back();
		return pos;
	}

	if (segmentp->canEdit())
	{
		segmentp->setEnd(segmentp->getEnd() + insert_len);
		if (seg_iter != mSegments.end())
		{
			++seg_iter;
		}
	}
	else
	{
		// create default editable segment to hold new text
		LLStyleConstSP sp(new LLStyle(getStyleParams()));
		default_segment = new LLNormalTextSegment( sp, pos, pos + insert_len, *this);
	}

	// shift remaining segments to right
	for(;seg_iter != mSegments.end(); ++seg_iter)
	{
		LLTextSegmentPtr segmentp = *seg_iter;
		segmentp->setStart(segmentp->getStart() + insert_len);
		segmentp->setEnd(segmentp->getEnd() + insert_len);
	}

	// insert new segments
	if (segments)
	{
		if (default_segment.notNull())
		{
			// potentially overwritten by segments passed in
			insertSegment(default_segment);
		}
		for (segment_vec_t::iterator seg_iter = segments->begin();
			seg_iter != segments->end();
			++seg_iter)
		{
			LLTextSegment* segmentp = *seg_iter;
			insertSegment(segmentp);
		}
	}

	getViewModel()->getEditableDisplay().insert(pos, wstr);

	if ( truncate() )
	{
		insert_len = getLength() - old_len;
	}

	onValueChange(pos, pos + insert_len);
	needsReflow(pos);

	return insert_len;
}

S32 LLTextBase::removeStringNoUndo(S32 pos, S32 length)
{

    beforeValueChange();
	segment_set_t::iterator seg_iter = getSegIterContaining(pos);
	while(seg_iter != mSegments.end())
	{
		LLTextSegmentPtr segmentp = *seg_iter;
		S32 end = pos + length;
		if (segmentp->getStart() < pos)
		{
			// deleting from middle of segment
			if (segmentp->getEnd() > end)
			{
				segmentp->setEnd(segmentp->getEnd() - length);
			}
			// truncating segment
			else
			{
				segmentp->setEnd(pos);
			}
		}
		else if (segmentp->getStart() < end)
		{
			// deleting entire segment
			if (segmentp->getEnd() <= end)
			{
				// remove segment
				segmentp->unlinkFromDocument(this);
				segment_set_t::iterator seg_to_erase(seg_iter++);
				mSegments.erase(seg_to_erase);
				continue;
			}
			// deleting head of segment
			else
			{
				segmentp->setStart(pos);
				segmentp->setEnd(segmentp->getEnd() - length);
			}
		}
		else
		{
			// shifting segments backward to fill deleted portion
			segmentp->setStart(segmentp->getStart() - length);
			segmentp->setEnd(segmentp->getEnd() - length);
		}
		++seg_iter;
	}

	getViewModel()->getEditableDisplay().erase(pos, length);

	// recreate default segment in case we erased everything
	createDefaultSegment();

	onValueChange(pos, pos);
	needsReflow(pos);

	return -length;	// This will be wrong if someone calls removeStringNoUndo with an excessive length
}

S32 LLTextBase::overwriteCharNoUndo(S32 pos, llwchar wc)
{
    beforeValueChange();

	if (pos > (S32)getLength())
	{
		return 0;
	}
	getViewModel()->getEditableDisplay()[pos] = wc;

	onValueChange(pos, pos + 1);
	needsReflow(pos);

	return 1;
}


void LLTextBase::createDefaultSegment()
{
	// ensures that there is always at least one segment
	if (mSegments.empty())
	{
		LLStyleConstSP sp(new LLStyle(getStyleParams()));
		LLTextSegmentPtr default_segment = new LLNormalTextSegment( sp, 0, getLength() + 1, *this);
		mSegments.insert(default_segment);
		default_segment->linkToDocument(this);
	}
}

void LLTextBase::insertSegment(LLTextSegmentPtr segment_to_insert)
{
	if (segment_to_insert.isNull()) 
	{
		return;
	}

	segment_set_t::iterator cur_seg_iter = getSegIterContaining(segment_to_insert->getStart());
	S32 reflow_start_index = 0;

	if (cur_seg_iter == mSegments.end())
	{
		mSegments.insert(segment_to_insert);
		segment_to_insert->linkToDocument(this);
		reflow_start_index = segment_to_insert->getStart();
	}
	else
	{
		LLTextSegmentPtr cur_segmentp = *cur_seg_iter;
		reflow_start_index = cur_segmentp->getStart();
		if (cur_segmentp->getStart() < segment_to_insert->getStart())
		{
			S32 old_segment_end = cur_segmentp->getEnd();
			// split old at start point for new segment
			cur_segmentp->setEnd(segment_to_insert->getStart());
			// advance to next segment
			// insert remainder of old segment
			LLStyleConstSP sp = cur_segmentp->getStyle();
			LLTextSegmentPtr remainder_segment = new LLNormalTextSegment( sp, segment_to_insert->getStart(), old_segment_end, *this);
			mSegments.insert(cur_seg_iter, remainder_segment);
			remainder_segment->linkToDocument(this);
			// insert new segment before remainder of old segment
			mSegments.insert(cur_seg_iter, segment_to_insert);

			segment_to_insert->linkToDocument(this);
			// at this point, there will be two overlapping segments owning the text
			// associated with the incoming segment
		}
		else
		{
			mSegments.insert(cur_seg_iter, segment_to_insert);
			segment_to_insert->linkToDocument(this);
		}

		// now delete/truncate remaining segments as necessary
		// cur_seg_iter points to segment before incoming segment
		while(cur_seg_iter != mSegments.end())
		{
			cur_segmentp = *cur_seg_iter;
			if (cur_segmentp == segment_to_insert) 
			{
				++cur_seg_iter;
				continue;
			}

			if (cur_segmentp->getStart() >= segment_to_insert->getStart())
			{
				if(cur_segmentp->getEnd() <= segment_to_insert->getEnd())
				{
					cur_segmentp->unlinkFromDocument(this);
					// grab copy of iterator to erase, and bump it
					segment_set_t::iterator seg_to_erase(cur_seg_iter++);
					mSegments.erase(seg_to_erase);
					continue;
				}
				else
				{
					// last overlapping segment, clip to end of incoming segment
					// and stop traversal
					cur_segmentp->setStart(segment_to_insert->getEnd());
					break;
				}
			}
			++cur_seg_iter;
		}
	}

	// layout potentially changed
	needsReflow(reflow_start_index);
}

BOOL LLTextBase::handleMouseDown(S32 x, S32 y, MASK mask)
{
	// handle triple click
	if (!mTripleClickTimer.hasExpired())
	{
		selectAll();
		return TRUE;
	}

	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleMouseDown(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleMouseDown(x, y, mask);
}

BOOL LLTextBase::handleMouseUp(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleMouseUp(x, y, mask))
	{
		// Did we just click on a link?
		if (mURLClickSignal
			&& cur_segment->getStyle()
			&& cur_segment->getStyle()->isLink())
		{
			// *TODO: send URL here?
			(*mURLClickSignal)(this, LLSD() );
		}
		return TRUE;
	}

	return LLUICtrl::handleMouseUp(x, y, mask);
}

BOOL LLTextBase::handleMiddleMouseDown(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleMiddleMouseDown(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleMiddleMouseDown(x, y, mask);
}

BOOL LLTextBase::handleMiddleMouseUp(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleMiddleMouseUp(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleMiddleMouseUp(x, y, mask);
}

BOOL LLTextBase::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleRightMouseDown(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleRightMouseDown(x, y, mask);
}

BOOL LLTextBase::handleRightMouseUp(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleRightMouseUp(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleRightMouseUp(x, y, mask);
}

BOOL LLTextBase::handleDoubleClick(S32 x, S32 y, MASK mask)
{
	//Don't start triple click timer if user have clicked on scrollbar
	mVisibleTextRect = mScroller ? mScroller->getContentWindowRect() : getLocalRect();
	if (x >= mVisibleTextRect.mLeft && x <= mVisibleTextRect.mRight
	    && y >= mVisibleTextRect.mBottom && y <= mVisibleTextRect.mTop)
	{
		mTripleClickTimer.setTimerExpirySec(TRIPLE_CLICK_INTERVAL);
	}

	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleDoubleClick(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleDoubleClick(x, y, mask);
}

BOOL LLTextBase::handleHover(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleHover(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleHover(x, y, mask);
}

BOOL LLTextBase::handleScrollWheel(S32 x, S32 y, S32 clicks)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleScrollWheel(x, y, clicks))
	{
		return TRUE;
	}

	return LLUICtrl::handleScrollWheel(x, y, clicks);
}

BOOL LLTextBase::handleToolTip(S32 x, S32 y, MASK mask)
{
	LLTextSegmentPtr cur_segment = getSegmentAtLocalPos(x, y);
	if (cur_segment && cur_segment->handleToolTip(x, y, mask))
	{
		return TRUE;
	}

	return LLUICtrl::handleToolTip(x, y, mask);
}


void LLTextBase::reshape(S32 width, S32 height, BOOL called_from_parent)
{
	if (width != getRect().getWidth() || height != getRect().getHeight())
	{
		bool scrolled_to_bottom = mScroller ? mScroller->isAtBottom() : false;

		LLUICtrl::reshape( width, height, called_from_parent );

		if (mScroller && scrolled_to_bottom && mTrackEnd)
		{
			// keep bottom of text buffer visible
			// do this here as well as in reflow to handle case
			// where shrinking from top, which causes buffer to temporarily 
			// not be scrolled to the bottom, since the scroll index
			// specified the _top_ of the visible document region
			mScroller->goToBottom();
		}

		// do this first after reshape, because other things depend on
		// up-to-date mVisibleTextRect
		updateRects();
		
		needsReflow();
	}
}

void LLTextBase::draw()
{
	// reflow if needed, on demand
	reflow();

	// then update scroll position, as cursor may have moved
	if (!mReadOnly)
	{
		updateScrollFromCursor();
	}

	LLRect text_rect;
	if (mScroller)
	{
		mScroller->localRectToOtherView(mScroller->getContentWindowRect(), &text_rect, this);
	}
	else
	{
		LLRect visible_lines_rect;
		std::pair<S32, S32> line_range = getVisibleLines(mClipPartial);
		for (S32 i = line_range.first; i < line_range.second; i++)
		{
			if (visible_lines_rect.isEmpty())
			{
				visible_lines_rect = mLineInfoList[i].mRect;
			}
			else
			{
				visible_lines_rect.unionWith(mLineInfoList[i].mRect);
			}
		}
		text_rect = visible_lines_rect;
		text_rect.translate(mDocumentView->getRect().mLeft, mDocumentView->getRect().mBottom);
	}

	if (mBGVisible)
	{
		F32 alpha = getCurrentTransparency();
		// clip background rect against extents, if we support scrolling
		LLRect bg_rect = mVisibleTextRect;
		if (mScroller)
		{
			bg_rect.intersectWith(text_rect);
		}
		LLColor4 bg_color = mReadOnly 
							? mReadOnlyBgColor.get()
							: hasFocus() 
								? mFocusBgColor.get() 
								: mWriteableBgColor.get();
		gl_rect_2d(text_rect, bg_color % alpha, TRUE);
	}

	bool should_clip = mClip || mScroller != NULL;
	{ LLLocalClipRect clip(text_rect, should_clip);
 
		// draw document view
		if (mScroller)
		{
			drawChild(mScroller);
		}
		else
		{
			drawChild(mDocumentView);
		}
 
		drawSelectionBackground();
		drawText();
		drawCursor();
	}
 
	mDocumentView->setVisible(FALSE);
	LLUICtrl::draw();
	mDocumentView->setVisible(TRUE);
}


//virtual
void LLTextBase::setColor( const LLColor4& c )
{
	mFgColor = c;
	mStyleDirty = true;
}

//virtual 
void LLTextBase::setReadOnlyColor(const LLColor4 &c)
{
	mReadOnlyFgColor = c;
	mStyleDirty = true;
}

//virtual
void LLTextBase::handleVisibilityChange( BOOL new_visibility )
{
	if(!new_visibility && mPopupMenu)
	{
		mPopupMenu->hide();
	}
	LLUICtrl::handleVisibilityChange(new_visibility);
}

//virtual
void LLTextBase::setValue(const LLSD& value )
{
	setText(value.asString());
}

//virtual
BOOL LLTextBase::canDeselect() const 
{ 
	return hasSelection(); 
}


//virtual
void LLTextBase::deselect()
{
	mSelectionStart = 0;
	mSelectionEnd = 0;
	mIsSelecting = FALSE;
}

bool LLTextBase::getSpellCheck() const
{
	return (LLSpellChecker::getUseSpellCheck()) && (!mReadOnly) && (mSpellCheck);
}

const std::string& LLTextBase::getSuggestion(U32 index) const
{
	return (index < mSuggestionList.size()) ? mSuggestionList[index] : LLStringUtil::null;
}

U32 LLTextBase::getSuggestionCount() const
{
	return mSuggestionList.size();
}

void LLTextBase::replaceWithSuggestion(U32 index)
{
	for (std::list<std::pair<U32, U32> >::const_iterator it = mMisspellRanges.begin(); it != mMisspellRanges.end(); ++it)
	{
		if ( (it->first <= (U32)mCursorPos) && (it->second >= (U32)mCursorPos) )
		{
			deselect();

			// Delete the misspelled word
			removeStringNoUndo(it->first, it->second - it->first);

			// Insert the suggestion in its place
			LLWString suggestion = utf8str_to_wstring(mSuggestionList[index]);
			insertStringNoUndo(it->first, utf8str_to_wstring(mSuggestionList[index]));
			setCursorPos(it->first + (S32)suggestion.length());

			break;
		}
	}
	mSpellCheckStart = mSpellCheckEnd = -1;
}

void LLTextBase::addToDictionary()
{
	if (canAddToDictionary())
	{
		LLSpellChecker::instance().addToCustomDictionary(getMisspelledWord(mCursorPos));
	}
}

bool LLTextBase::canAddToDictionary() const
{
	return (getSpellCheck()) && (isMisspelledWord(mCursorPos));
}

void LLTextBase::addToIgnore()
{
	if (canAddToIgnore())
	{
		LLSpellChecker::instance().addToIgnoreList(getMisspelledWord(mCursorPos));
	}
}

bool LLTextBase::canAddToIgnore() const
{
	return (getSpellCheck()) && (isMisspelledWord(mCursorPos));
}

std::string LLTextBase::getMisspelledWord(U32 pos) const
{
	for (std::list<std::pair<U32, U32> >::const_iterator it = mMisspellRanges.begin(); it != mMisspellRanges.end(); ++it)
	{
		if ( (it->first <= pos) && (it->second >= pos) )
		{
			return wstring_to_utf8str(getWText().substr(it->first, it->second - it->first));
		}
	}
	return LLStringUtil::null;
}

bool LLTextBase::isMisspelledWord(U32 pos) const
{
	for (std::list<std::pair<U32, U32> >::const_iterator it = mMisspellRanges.begin(); it != mMisspellRanges.end(); ++it)
	{
		if ( (it->first <= pos) && (it->second >= pos) )
		{
			return true;
		}
	}
	return false;
}

void LLTextBase::onSpellCheckSettingsChange()
{
	// Recheck the spelling on every change
	mMisspellRanges.clear();
	mSpellCheckStart = mSpellCheckEnd = -1;
}

void LLTextBase::onFocusReceived()
{
	LLUICtrl::onFocusReceived();
	if (!getLength() && !mLabel.empty())
	{
		// delete label which is LLLabelTextSegment
		clearSegments();
	}
}

void LLTextBase::onFocusLost()
{
	LLUICtrl::onFocusLost();
	if (!getLength() && !mLabel.empty())
	{
		resetLabel();
	}
}

// Sets the scrollbar from the cursor position
void LLTextBase::updateScrollFromCursor()
{
	// Update scroll position even in read-only mode (when there's no cursor displayed)
	// because startOfDoc()/endOfDoc() modify cursor position. See EXT-736.

	if (!mScrollNeeded || !mScroller)
	{
		return;
	}
	mScrollNeeded = FALSE; 

	// scroll so that the cursor is at the top of the page
	LLRect scroller_doc_window = getVisibleDocumentRect();
	LLRect cursor_rect_doc = getDocRectFromDocIndex(mCursorPos);
	mScroller->scrollToShowRect(cursor_rect_doc, LLRect(0, scroller_doc_window.getHeight() - 5, scroller_doc_window.getWidth(), 5));
}

S32 LLTextBase::getLeftOffset(S32 width)
{
	switch (mHAlign)
	{
	case LLFontGL::LEFT:
		return mHPad;
	case LLFontGL::HCENTER:
		return mHPad + llmax(0, (mVisibleTextRect.getWidth() - width - mHPad) / 2);
	case LLFontGL::RIGHT:
		return mVisibleTextRect.getWidth() - width;
	default:
		return mHPad;
	}
}


static LLFastTimer::DeclareTimer FTM_TEXT_REFLOW ("Text Reflow");
void LLTextBase::reflow()
{
	LLFastTimer ft(FTM_TEXT_REFLOW);

	updateSegments();

	if (mReflowIndex == S32_MAX)
	{
		return;
	}

	bool scrolled_to_bottom = mScroller ? mScroller->isAtBottom() : false;

	LLRect cursor_rect = getLocalRectFromDocIndex(mCursorPos);
	bool follow_selection = getLocalRect().overlaps(cursor_rect); // cursor is (potentially) visible

	// store in top-left relative coordinates to avoid issues with horizontal scrollbar appearing and disappearing
	cursor_rect.mTop = mVisibleTextRect.mTop - cursor_rect.mTop;
	cursor_rect.mBottom = mVisibleTextRect.mTop - cursor_rect.mBottom;

	S32 first_line = getFirstVisibleLine();

	// if scroll anchor not on first line, update it to first character of first line
	if ((first_line < mLineInfoList.size())
		&&	(mScrollIndex <  mLineInfoList[first_line].mDocIndexStart
			||	mScrollIndex >= mLineInfoList[first_line].mDocIndexEnd))
	{
		mScrollIndex = mLineInfoList[first_line].mDocIndexStart;
	}
	LLRect first_char_rect = getLocalRectFromDocIndex(mScrollIndex);
	// store in top-left relative coordinates to avoid issues with horizontal scrollbar appearing and disappearing
	first_char_rect.mTop = mVisibleTextRect.mTop - first_char_rect.mTop;
	first_char_rect.mBottom = mVisibleTextRect.mTop - first_char_rect.mBottom;

	S32 reflow_count = 0;
	while(mReflowIndex < S32_MAX)
	{
		// we can get into an infinite loop if the document height does not monotonically increase
		// with decreasing width (embedded ui elements with alternate layouts).  In that case, 
		// we want to stop reflowing after 2 iterations.  We use 2, since we need to handle the case
		// of introducing a vertical scrollbar causing a reflow with less width.  We should also always
		// use an even number of iterations to avoid user visible oscillation of the layout
		if(++reflow_count > 2)
		{
			lldebugs << "Breaking out of reflow due to possible infinite loop in " << getName() << llendl;
			break;
		}
	
		S32 start_index = mReflowIndex;
		mReflowIndex = S32_MAX;

		// shrink document to minimum size (visible portion of text widget)
		// to force inlined widgets with follows set to shrink
		if (mWordWrap)
		{
			mDocumentView->reshape(mVisibleTextRect.getWidth(), mDocumentView->getRect().getHeight());
		}

		S32 cur_top = 0;

		segment_set_t::iterator seg_iter = mSegments.begin();
		S32 seg_offset = 0;
		S32 line_start_index = 0;
		const S32 text_available_width = mVisibleTextRect.getWidth() - mHPad;  // reserve room for margin
		S32 remaining_pixels = text_available_width;
		S32 line_count = 0;

		// find and erase line info structs starting at start_index and going to end of document
		if (!mLineInfoList.empty())
		{
			// find first element whose end comes after start_index
			line_list_t::iterator iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), start_index, line_end_compare());
			line_start_index = iter->mDocIndexStart;
			line_count = iter->mLineNum;
			cur_top = iter->mRect.mTop;
			getSegmentAndOffset(iter->mDocIndexStart, &seg_iter, &seg_offset);
			mLineInfoList.erase(iter, mLineInfoList.end());
		}

		S32 line_height = 0;

		while(seg_iter != mSegments.end())
		{
			LLTextSegmentPtr segment = *seg_iter;

			// track maximum height of any segment on this line
			S32 cur_index = segment->getStart() + seg_offset;

			// ask segment how many character fit in remaining space
			S32 character_count = segment->getNumChars(getWordWrap() ? llmax(0, remaining_pixels) : S32_MAX,
														seg_offset, 
														cur_index - line_start_index, 
														S32_MAX);

			S32 segment_width, segment_height;
			bool force_newline = segment->getDimensions(seg_offset, character_count, segment_width, segment_height);
			// grow line height as necessary based on reported height of this segment
			line_height = llmax(line_height, segment_height);
			remaining_pixels -= segment_width;

			seg_offset += character_count;

			S32 last_segment_char_on_line = segment->getStart() + seg_offset;

			S32 text_actual_width = text_available_width - remaining_pixels;
			S32 text_left = getLeftOffset(text_actual_width);
			LLRect line_rect(text_left, 
							cur_top, 
							text_left + text_actual_width, 
							cur_top - line_height);

			// if we didn't finish the current segment...
			if (last_segment_char_on_line < segment->getEnd())
			{
				// add line info and keep going
				mLineInfoList.push_back(line_info(
											line_start_index, 
											last_segment_char_on_line, 
											line_rect, 
											line_count));

				line_start_index = segment->getStart() + seg_offset;
				cur_top -= llround((F32)line_height * mLineSpacingMult) + mLineSpacingPixels;
				remaining_pixels = text_available_width;
				line_height = 0;
			}
			// ...just consumed last segment..
			else if (++segment_set_t::iterator(seg_iter) == mSegments.end())
			{
				mLineInfoList.push_back(line_info(
											line_start_index, 
											last_segment_char_on_line, 
											line_rect, 
											line_count));
				cur_top -= llround((F32)line_height * mLineSpacingMult) + mLineSpacingPixels;
				break;
			}
			// ...or finished a segment and there are segments remaining on this line
			else
			{
				// subtract pixels used and increment segment
				if (force_newline)
				{
					mLineInfoList.push_back(line_info(
												line_start_index, 
												last_segment_char_on_line, 
												line_rect, 
												line_count));
					line_start_index = segment->getStart() + seg_offset;
					cur_top -= llround((F32)line_height * mLineSpacingMult) + mLineSpacingPixels;
					line_height = 0;
					remaining_pixels = text_available_width;
				}
				++seg_iter;
				seg_offset = 0;
			}
			if (force_newline) 
			{
				line_count++;
			}
		}

		// calculate visible region for diplaying text
		updateRects();

		for (segment_set_t::iterator segment_it = mSegments.begin();
			segment_it != mSegments.end();
			++segment_it)
		{
			LLTextSegmentPtr segmentp = *segment_it;
			segmentp->updateLayout(*this);

		}
	}

	// apply scroll constraints after reflowing text
	if (!hasMouseCapture() && mScroller)
	{
		if (scrolled_to_bottom && mTrackEnd)
		{
			// keep bottom of text buffer visible
			endOfDoc();
		}
		else if (hasSelection() && follow_selection)
		{
			// keep cursor in same vertical position on screen when selecting text
			LLRect new_cursor_rect_doc = getDocRectFromDocIndex(mCursorPos);
			LLRect old_cursor_rect = cursor_rect;
			old_cursor_rect.mTop = mVisibleTextRect.mTop - cursor_rect.mTop;
			old_cursor_rect.mBottom = mVisibleTextRect.mTop - cursor_rect.mBottom;

			mScroller->scrollToShowRect(new_cursor_rect_doc, old_cursor_rect);
		}
		else
		{
			// keep first line of text visible
			LLRect new_first_char_rect = getDocRectFromDocIndex(mScrollIndex);

			// pass in desired rect in the coordinate frame of the document viewport
			LLRect old_first_char_rect = first_char_rect;
			old_first_char_rect.mTop = mVisibleTextRect.mTop - first_char_rect.mTop;
			old_first_char_rect.mBottom = mVisibleTextRect.mTop - first_char_rect.mBottom;

			mScroller->scrollToShowRect(new_first_char_rect, old_first_char_rect);
		}
	}

	// reset desired x cursor position
	updateCursorXPos();
}

LLRect LLTextBase::getTextBoundingRect()
{
	reflow();
	return mTextBoundingRect;
}


void LLTextBase::clearSegments()
{
	mSegments.clear();
	createDefaultSegment();
}

S32 LLTextBase::getLineStart( S32 line ) const
{
	S32 num_lines = getLineCount();
	if (num_lines == 0)
	{
		return 0;
	}

	line = llclamp(line, 0, num_lines-1);
	return mLineInfoList[line].mDocIndexStart;
}

S32 LLTextBase::getLineEnd( S32 line ) const
{
	S32 num_lines = getLineCount();
	if (num_lines == 0)
	{
		return 0;
	}

	line = llclamp(line, 0, num_lines-1);
	return mLineInfoList[line].mDocIndexEnd;
}



S32 LLTextBase::getLineNumFromDocIndex( S32 doc_index, bool include_wordwrap) const
{
	if (mLineInfoList.empty())
	{
		return 0;
	}
	else
	{
		line_list_t::const_iterator iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), doc_index, line_end_compare());
		if (include_wordwrap)
		{
			return iter - mLineInfoList.begin();
		}
		else
		{
			if (iter == mLineInfoList.end())
			{
				return mLineInfoList.back().mLineNum;
			}
			else
			{
				return iter->mLineNum;
			}
		}
	}
}

// Given an offset into text (pos), find the corresponding line (from the start of the doc) and an offset into the line.
S32 LLTextBase::getLineOffsetFromDocIndex( S32 startpos, bool include_wordwrap) const
{
	if (mLineInfoList.empty())
	{
		return startpos;
	}
	else
	{
		line_list_t::const_iterator iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), startpos, line_end_compare());
		return startpos - iter->mDocIndexStart;
	}
}

S32	LLTextBase::getFirstVisibleLine() const
{
	LLRect visible_region = getVisibleDocumentRect();

	// binary search for line that starts before top of visible buffer
	line_list_t::const_iterator iter = std::lower_bound(mLineInfoList.begin(), mLineInfoList.end(), visible_region.mTop, compare_bottom());

	return iter - mLineInfoList.begin();
}

std::pair<S32, S32>	LLTextBase::getVisibleLines(bool require_fully_visible) 
{
	LLRect visible_region = getVisibleDocumentRect();
	line_list_t::const_iterator first_iter;
	line_list_t::const_iterator last_iter;

	// make sure we have an up-to-date mLineInfoList
	reflow();

	if (require_fully_visible)
	{
		first_iter = std::lower_bound(mLineInfoList.begin(), mLineInfoList.end(), visible_region.mTop, compare_top());
		last_iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), visible_region.mBottom, compare_bottom());
	}
	else
	{
		first_iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), visible_region.mTop, compare_bottom());
		last_iter = std::lower_bound(mLineInfoList.begin(), mLineInfoList.end(), visible_region.mBottom, compare_top());
	}
	return std::pair<S32, S32>(first_iter - mLineInfoList.begin(), last_iter - mLineInfoList.begin());
}



LLTextViewModel* LLTextBase::getViewModel() const
{
	return (LLTextViewModel*)mViewModel.get();
}

void LLTextBase::addDocumentChild(LLView* view) 
{ 
	mDocumentView->addChild(view); 
}

void LLTextBase::removeDocumentChild(LLView* view) 
{ 
	mDocumentView->removeChild(view); 
}


static LLFastTimer::DeclareTimer FTM_UPDATE_TEXT_SEGMENTS("Update Text Segments");
void LLTextBase::updateSegments()
{
	LLFastTimer ft(FTM_UPDATE_TEXT_SEGMENTS);
	createDefaultSegment();
}

void LLTextBase::getSegmentAndOffset( S32 startpos, segment_set_t::const_iterator* seg_iter, S32* offsetp ) const
{
	*seg_iter = getSegIterContaining(startpos);
	if (*seg_iter == mSegments.end())
	{
		*offsetp = 0;
	}
	else
	{
		*offsetp = startpos - (**seg_iter)->getStart();
	}
}

void LLTextBase::getSegmentAndOffset( S32 startpos, segment_set_t::iterator* seg_iter, S32* offsetp )
{
	*seg_iter = getSegIterContaining(startpos);
	if (*seg_iter == mSegments.end())
	{
		*offsetp = 0;
	}
	else
	{
		*offsetp = startpos - (**seg_iter)->getStart();
	}
}

LLTextBase::segment_set_t::iterator LLTextBase::getEditableSegIterContaining(S32 index)
{
	segment_set_t::iterator it = getSegIterContaining(index);
	segment_set_t::iterator orig_it = it;

	if (it == mSegments.end()) return it;

	if (!(*it)->canEdit() 
		&& index == (*it)->getStart() 
		&& it != mSegments.begin())
	{
		it--;
		if ((*it)->canEdit())
		{
			return it;
		}
	}
	return orig_it;
}

LLTextBase::segment_set_t::const_iterator LLTextBase::getEditableSegIterContaining(S32 index) const
{
	segment_set_t::const_iterator it = getSegIterContaining(index);
	segment_set_t::const_iterator orig_it = it;
	if (it == mSegments.end()) return it;

	if (!(*it)->canEdit() 
		&& index == (*it)->getStart() 
		&& it != mSegments.begin())
	{
		it--;
		if ((*it)->canEdit())
		{
			return it;
		}
	}
	return orig_it;
}

LLTextBase::segment_set_t::iterator LLTextBase::getSegIterContaining(S32 index)
{

	static LLPointer<LLIndexSegment> index_segment = new LLIndexSegment();

	S32 text_len = 0;
	if (!useLabel())
	{
		text_len = getLength();
	}
	else
	{
		text_len = mLabel.getWString().length();
	}

	if (index > text_len) { return mSegments.end(); }

	// when there are no segments, we return the end iterator, which must be checked by caller
	if (mSegments.size() <= 1) { return mSegments.begin(); }

	//FIXME: avoid operator new somehow (without running into refcount problems)
	index_segment->setStart(index);
	index_segment->setEnd(index);
	segment_set_t::iterator it = mSegments.upper_bound(index_segment);
	return it;
}

LLTextBase::segment_set_t::const_iterator LLTextBase::getSegIterContaining(S32 index) const
{
	static LLPointer<LLIndexSegment> index_segment = new LLIndexSegment();

	S32 text_len = 0;
	if (!useLabel())
	{
		text_len = getLength();
	}
	else
	{
		text_len = mLabel.getWString().length();
	}

	if (index > text_len) { return mSegments.end(); }

	// when there are no segments, we return the end iterator, which must be checked by caller
	if (mSegments.size() <= 1) { return mSegments.begin(); }

	index_segment->setStart(index);
	index_segment->setEnd(index);
	LLTextBase::segment_set_t::const_iterator it =  mSegments.upper_bound(index_segment);
	return it;
}

// Finds the text segment (if any) at the give local screen position
LLTextSegmentPtr LLTextBase::getSegmentAtLocalPos( S32 x, S32 y, bool hit_past_end_of_line)
{
	// Find the cursor position at the requested local screen position
	S32 offset = getDocIndexFromLocalCoord( x, y, FALSE, hit_past_end_of_line);
	segment_set_t::iterator seg_iter = getSegIterContaining(offset);
	if (seg_iter != mSegments.end())
	{
		return *seg_iter;
	}
	else
	{
		return LLTextSegmentPtr();
	}
}

void LLTextBase::createUrlContextMenu(S32 x, S32 y, const std::string &in_url)
{
	// work out the XUI menu file to use for this url
	LLUrlMatch match;
	std::string url = in_url;
	if (! LLUrlRegistry::instance().findUrl(url, match))
	{
		return;
	}
	
	std::string xui_file = match.getMenuName();
	if (xui_file.empty())
	{
		return;
	}

	// set up the callbacks for all of the potential menu items, N.B. we
	// don't use const ref strings in callbacks in case url goes out of scope
	LLUICtrl::CommitCallbackRegistry::ScopedRegistrar registrar;
	registrar.add("Url.Open", boost::bind(&LLUrlAction::openURL, url));
	registrar.add("Url.OpenInternal", boost::bind(&LLUrlAction::openURLInternal, url));
	registrar.add("Url.OpenExternal", boost::bind(&LLUrlAction::openURLExternal, url));
	registrar.add("Url.Execute", boost::bind(&LLUrlAction::executeSLURL, url));
	registrar.add("Url.Block", boost::bind(&LLUrlAction::blockObject, url));
	registrar.add("Url.Teleport", boost::bind(&LLUrlAction::teleportToLocation, url));
	registrar.add("Url.ShowProfile", boost::bind(&LLUrlAction::showProfile, url));
	registrar.add("Url.AddFriend", boost::bind(&LLUrlAction::addFriend, url));
	registrar.add("Url.RemoveFriend", boost::bind(&LLUrlAction::removeFriend, url));
	registrar.add("Url.SendIM", boost::bind(&LLUrlAction::sendIM, url));
	registrar.add("Url.ShowOnMap", boost::bind(&LLUrlAction::showLocationOnMap, url));
	registrar.add("Url.CopyLabel", boost::bind(&LLUrlAction::copyLabelToClipboard, url));
	registrar.add("Url.CopyUrl", boost::bind(&LLUrlAction::copyURLToClipboard, url));

	// create and return the context menu from the XUI file
	delete mPopupMenu;
	mPopupMenu = LLUICtrlFactory::getInstance()->createFromFile<LLContextMenu>(xui_file, LLMenuGL::sMenuContainer,
																		 LLMenuHolderGL::child_registry_t::instance());	
	if (mIsFriendSignal)
	{
		bool isFriend = *(*mIsFriendSignal)(LLUUID(LLUrlAction::getUserID(url)));
		LLView* addFriendButton = mPopupMenu->getChild<LLView>("add_friend");
		LLView* removeFriendButton = mPopupMenu->getChild<LLView>("remove_friend");

		if (addFriendButton && removeFriendButton)
		{
			addFriendButton->setEnabled(!isFriend);
			removeFriendButton->setEnabled(isFriend);
		}
	}
	
	if (mPopupMenu)
	{
		mPopupMenu->show(x, y);
		LLMenuGL::showPopup(this, mPopupMenu, x, y);
	}
}

void LLTextBase::setText(const LLStringExplicit &utf8str, const LLStyle::Params& input_params)
{
	// clear out the existing text and segments
	getViewModel()->setDisplay(LLWStringUtil::null);

	clearSegments();
//	createDefaultSegment();

	deselect();

	// append the new text (supports Url linking)
	std::string text(utf8str);
	LLStringUtil::removeCRLF(text);

	// appendText modifies mCursorPos...
	appendText(text, false, input_params);
	// ...so move cursor to top after appending text
	if (!mTrackEnd)
	{
		startOfDoc();
	}

	onValueChange(0, getLength());
}

//virtual
std::string LLTextBase::getText() const
{
	return getViewModel()->getValue().asString();
}

// IDEVO - icons can be UI image names or UUID sent from
// server with avatar display name
static LLUIImagePtr image_from_icon_name(const std::string& icon_name)
{
	if (LLUUID::validate(icon_name))
	{
		return LLUI::getUIImageByID( LLUUID(icon_name) );
	}
	else
	{
		return LLUI::getUIImage(icon_name);
	}
}

static LLFastTimer::DeclareTimer FTM_PARSE_HTML("Parse HTML");

void LLTextBase::appendTextImpl(const std::string &new_text, const LLStyle::Params& input_params)
{
	LLStyle::Params style_params(input_params);
	style_params.fillFrom(getStyleParams());

	S32 part = (S32)LLTextParser::WHOLE;
	if (mParseHTML && !style_params.is_link) // Don't search for URLs inside a link segment (STORM-358).
	{
		LLFastTimer _(FTM_PARSE_HTML);
		S32 start=0,end=0;
		LLUrlMatch match;
		std::string text = new_text;
		while ( LLUrlRegistry::instance().findUrl(text, match,
				boost::bind(&LLTextBase::replaceUrl, this, _1, _2, _3)) )
		{
			start = match.getStart();
			end = match.getEnd()+1;

			LLStyle::Params link_params(style_params);
			link_params.overwriteFrom(match.getStyle());

			// output the text before the Url
			if (start > 0)
			{
				if (part == (S32)LLTextParser::WHOLE ||
					part == (S32)LLTextParser::START)
				{
					part = (S32)LLTextParser::START;
				}
				else
				{
					part = (S32)LLTextParser::MIDDLE;
				}
				std::string subtext=text.substr(0,start);
				appendAndHighlightText(subtext, part, style_params); 
			}
			// output the styled Url
			appendAndHighlightTextImpl(match.getLabel(), part, link_params, match.underlineOnHoverOnly());
			
			// set the tooltip for the Url label
			if (! match.getTooltip().empty())
			{
				segment_set_t::iterator it = getSegIterContaining(getLength()-1);
				if (it != mSegments.end())
					{
						LLTextSegmentPtr segment = *it;
						segment->setToolTip(match.getTooltip());
					}
			}

			LLTextUtil::processUrlMatch(&match,this);

			// move on to the rest of the text after the Url
			if (end < (S32)text.length()) 
			{
				text = text.substr(end,text.length() - end);
				end=0;
				part=(S32)LLTextParser::END;
			}
			else
			{
				break;
			}
		}
		if (part != (S32)LLTextParser::WHOLE) 
			part=(S32)LLTextParser::END;
		if (end < (S32)text.length()) 
			appendAndHighlightText(text, part, style_params);		
	}
	else
	{
		appendAndHighlightText(new_text, part, style_params);
	}
}

static LLFastTimer::DeclareTimer FTM_APPEND_TEXT("Append Text");

void LLTextBase::appendText(const std::string &new_text, bool prepend_newline, const LLStyle::Params& input_params)
{
	LLFastTimer _(FTM_APPEND_TEXT);
	if (new_text.empty()) 
		return;

	if(prepend_newline)
		appendLineBreakSegment(input_params);
	appendTextImpl(new_text,input_params);
}

void LLTextBase::setLabel(const LLStringExplicit& label)
{
	mLabel = label;
	resetLabel();
}

BOOL LLTextBase::setLabelArg(const std::string& key, const LLStringExplicit& text )
{
	mLabel.setArg(key, text);
	return TRUE;
}

void LLTextBase::resetLabel()
{
	if (useLabel())
	{
		clearSegments();

		LLStyle* style = new LLStyle(getStyleParams());
		style->setColor(mTentativeFgColor);
		LLStyleConstSP sp(style);

		LLTextSegmentPtr label = new LLLabelTextSegment(sp, 0, mLabel.getWString().length() + 1, *this);
		insertSegment(label);
	}
}

bool LLTextBase::useLabel() const
{
    return !getLength() && !mLabel.empty() && !hasFocus();
}

void LLTextBase::setFont(const LLFontGL* font)
{
	mFont = font;
	mStyleDirty = true;
}

void LLTextBase::needsReflow(S32 index)
{
	lldebugs << "reflow on object " << (void*)this << " index = " << mReflowIndex << ", new index = " << index << llendl;
	mReflowIndex = llmin(mReflowIndex, index);
}

void LLTextBase::appendLineBreakSegment(const LLStyle::Params& style_params)
{
	segment_vec_t segments;
	LLStyleConstSP sp(new LLStyle(style_params));
	segments.push_back(new LLLineBreakTextSegment(sp, getLength()));

	insertStringNoUndo(getLength(), utf8str_to_wstring("\n"), &segments);
}

void LLTextBase::appendImageSegment(const LLStyle::Params& style_params)
{
	if(getPlainText())
	{
		return;
	}
	segment_vec_t segments;
	LLStyleConstSP sp(new LLStyle(style_params));
	segments.push_back(new LLImageTextSegment(sp, getLength(),*this));

	insertStringNoUndo(getLength(), utf8str_to_wstring(" "), &segments);
}

void LLTextBase::appendWidget(const LLInlineViewSegment::Params& params, const std::string& text, bool allow_undo)
{
	segment_vec_t segments;
	LLWString widget_wide_text = utf8str_to_wstring(text);
	segments.push_back(new LLInlineViewSegment(params, getLength(), getLength() + widget_wide_text.size()));

	insertStringNoUndo(getLength(), widget_wide_text, &segments);
}

void LLTextBase::appendAndHighlightTextImpl(const std::string &new_text, S32 highlight_part, const LLStyle::Params& style_params, bool underline_on_hover_only)
{
	// Save old state
	S32 selection_start = mSelectionStart;
	S32 selection_end = mSelectionEnd;
	BOOL was_selecting = mIsSelecting;
	S32 cursor_pos = mCursorPos;
	S32 old_length = getLength();
	BOOL cursor_was_at_end = (mCursorPos == old_length);

	deselect();

	setCursorPos(old_length);

	if (mParseHighlights)
	{
		LLStyle::Params highlight_params(style_params);

		LLSD pieces = LLTextParser::instance().parsePartialLineHighlights(new_text, highlight_params.color(), (LLTextParser::EHighlightPosition)highlight_part);
		for (S32 i = 0; i < pieces.size(); i++)
		{
			LLSD color_llsd = pieces[i]["color"];
			LLColor4 lcolor;
			lcolor.setValue(color_llsd);
			highlight_params.color = lcolor;

			LLWString wide_text;
			wide_text = utf8str_to_wstring(pieces[i]["text"].asString());

			S32 cur_length = getLength();
			LLStyleConstSP sp(new LLStyle(highlight_params));
			LLTextSegmentPtr segmentp;
			if(underline_on_hover_only)
			{
				highlight_params.font.style("NORMAL");
				LLStyleConstSP normal_sp(new LLStyle(highlight_params));
				segmentp = new LLOnHoverChangeableTextSegment(sp, normal_sp, cur_length, cur_length + wide_text.size(), *this);
			}
			else
			{
				segmentp = new LLNormalTextSegment(sp, cur_length, cur_length + wide_text.size(), *this);
			}
			segment_vec_t segments;
			segments.push_back(segmentp);
			insertStringNoUndo(cur_length, wide_text, &segments);
		}
	}
	else
	{
		LLWString wide_text;
		wide_text = utf8str_to_wstring(new_text);

		segment_vec_t segments;
		S32 segment_start = old_length;
		S32 segment_end = old_length + wide_text.size();
		LLStyleConstSP sp(new LLStyle(style_params));
		if (underline_on_hover_only)
		{
			LLStyle::Params normal_style_params(style_params);
			normal_style_params.font.style("NORMAL");
			LLStyleConstSP normal_sp(new LLStyle(normal_style_params));
			segments.push_back(new LLOnHoverChangeableTextSegment(sp, normal_sp, segment_start, segment_end, *this ));
		}
		else
		{
		segments.push_back(new LLNormalTextSegment(sp, segment_start, segment_end, *this ));
		}

		insertStringNoUndo(getLength(), wide_text, &segments);
	}

	// Set the cursor and scroll position
	if( selection_start != selection_end )
	{
		mSelectionStart = selection_start;
		mSelectionEnd = selection_end;

		mIsSelecting = was_selecting;
		setCursorPos(cursor_pos);
	}
	else if( cursor_was_at_end )
	{
		setCursorPos(getLength());
	}
	else
	{
		setCursorPos(cursor_pos);
	}
}

void LLTextBase::appendAndHighlightText(const std::string &new_text, S32 highlight_part, const LLStyle::Params& style_params, bool underline_on_hover_only)
{
	if (new_text.empty()) return; 

	std::string::size_type start = 0;
	std::string::size_type pos = new_text.find("\n",start);
	
	while(pos!=-1)
	{
		if(pos!=start)
		{
			std::string str = std::string(new_text,start,pos-start);
			appendAndHighlightTextImpl(str,highlight_part, style_params, underline_on_hover_only);
		}
		appendLineBreakSegment(style_params);
		start = pos+1;
		pos = new_text.find("\n",start);
	}

	std::string str = std::string(new_text,start,new_text.length()-start);
	appendAndHighlightTextImpl(str,highlight_part, style_params, underline_on_hover_only);
}


void LLTextBase::replaceUrl(const std::string &url,
							const std::string &label,
							const std::string &icon)
{
	// get the full (wide) text for the editor so we can change it
	LLWString text = getWText();
	LLWString wlabel = utf8str_to_wstring(label);
	bool modified = false;
	S32 seg_start = 0;

	// iterate through each segment looking for ones styled as links
	segment_set_t::iterator it;
	for (it = mSegments.begin(); it != mSegments.end(); ++it)
	{
		LLTextSegment *seg = *it;
		LLStyleConstSP style = seg->getStyle();

		// update segment start/end length in case we replaced text earlier
		S32 seg_length = seg->getEnd() - seg->getStart();
		seg->setStart(seg_start);
		seg->setEnd(seg_start + seg_length);

		// if we find a link with our Url, then replace the label
		if (style->getLinkHREF() == url)
		{
			S32 start = seg->getStart();
			S32 end = seg->getEnd();
			text = text.substr(0, start) + wlabel + text.substr(end, text.size() - end + 1);
			seg->setEnd(start + wlabel.size());
			modified = true;
		}

		// Icon might be updated when more avatar or group info
		// becomes available
		if (style->isImage() && style->getLinkHREF() == url)
		{
			LLUIImagePtr image = image_from_icon_name( icon );
			if (image)
			{
				LLStyle::Params icon_params;
				icon_params.image = image;
				LLStyleConstSP new_style(new LLStyle(icon_params));
				seg->setStyle(new_style);
				modified = true;
			}
		}

		// work out the character offset for the next segment
		seg_start = seg->getEnd();
	}

	// update the editor with the new (wide) text string
	if (modified)
	{
		getViewModel()->setDisplay(text);
		deselect();
		setCursorPos(mCursorPos);
		needsReflow();
	}
}


void LLTextBase::setWText(const LLWString& text)
{
	setText(wstring_to_utf8str(text));
}

const LLWString& LLTextBase::getWText() const
{
	return getViewModel()->getDisplay();
}

// If round is true, if the position is on the right half of a character, the cursor
// will be put to its right.  If round is false, the cursor will always be put to the
// character's left.

S32 LLTextBase::getDocIndexFromLocalCoord( S32 local_x, S32 local_y, BOOL round, bool hit_past_end_of_line) const
{
	// Figure out which line we're nearest to.
	LLRect doc_rect = mDocumentView->getRect();

	S32 doc_y = local_y - doc_rect.mBottom;
	
	// binary search for line that starts before local_y
	line_list_t::const_iterator line_iter = std::lower_bound(mLineInfoList.begin(), mLineInfoList.end(), doc_y, compare_bottom());

	if (line_iter == mLineInfoList.end())
	{
		return getLength(); // past the end
	}
	
	S32 pos = getLength();
	S32 start_x = line_iter->mRect.mLeft + doc_rect.mLeft;

	segment_set_t::iterator line_seg_iter;
	S32 line_seg_offset;
	for(getSegmentAndOffset(line_iter->mDocIndexStart, &line_seg_iter, &line_seg_offset);
		line_seg_iter != mSegments.end(); 
		++line_seg_iter, line_seg_offset = 0)
	{
		const LLTextSegmentPtr segmentp = *line_seg_iter;

		S32 segment_line_start = segmentp->getStart() + line_seg_offset;
		S32 segment_line_length = llmin(segmentp->getEnd(), line_iter->mDocIndexEnd) - segment_line_start;
		S32 text_width, text_height;
		bool newline = segmentp->getDimensions(line_seg_offset, segment_line_length, text_width, text_height);

		if(newline)
		{
			pos = segment_line_start + segmentp->getOffset(local_x - start_x, line_seg_offset, segment_line_length, round);
			break;
		}

		// if we've reached a line of text *below* the mouse cursor, doc index is first character on that line
		if (hit_past_end_of_line && doc_y > line_iter->mRect.mTop)
		{
			pos = segment_line_start;
			break;
		}
		if (local_x < start_x + text_width)			// cursor to left of right edge of text
		{
			// Figure out which character we're nearest to.
			S32 offset;
			if (!segmentp->canEdit())
			{
				S32 segment_width, segment_height;
				segmentp->getDimensions(0, segmentp->getEnd() - segmentp->getStart(), segment_width, segment_height);
				if (round && local_x - start_x > segment_width / 2)
				{
					offset = segment_line_length;
				}
				else
				{
					offset = 0;
				}
			}
			else
			{
				offset = segmentp->getOffset(local_x - start_x, line_seg_offset, segment_line_length, round);
			}
			pos = segment_line_start + offset;
			break;
		}
		else if (hit_past_end_of_line && segmentp->getEnd() >= line_iter->mDocIndexEnd)
		{
			if (getLineNumFromDocIndex(line_iter->mDocIndexEnd - 1) == line_iter->mLineNum)
			{
				// if segment wraps to the next line we should step one char back
				// to compensate for the space char between words
				// which is removed due to wrapping
				pos = llclamp(line_iter->mDocIndexEnd - 1, 0, getLength());
			}
			else
			{
				pos = llclamp(line_iter->mDocIndexEnd, 0, getLength());
			}
			break;
		}
		start_x += text_width;
	}

	return pos;
}

// returns rectangle of insertion caret 
// in document coordinate frame from given index into text
LLRect LLTextBase::getDocRectFromDocIndex(S32 pos) const
{
	if (mLineInfoList.empty()) 
	{ 
		return LLRect();
	}

	LLRect doc_rect;

	// clamp pos to valid values
	pos = llclamp(pos, 0, mLineInfoList.back().mDocIndexEnd - 1);

	// find line that contains cursor
	line_list_t::const_iterator line_iter = std::upper_bound(mLineInfoList.begin(), mLineInfoList.end(), pos, line_end_compare());

	doc_rect.mLeft = line_iter->mRect.mLeft; 
	doc_rect.mBottom = line_iter->mRect.mBottom;
	doc_rect.mTop = line_iter->mRect.mTop;

	segment_set_t::iterator line_seg_iter;
	S32 line_seg_offset;
	segment_set_t::iterator cursor_seg_iter;
	S32 cursor_seg_offset;
	getSegmentAndOffset(line_iter->mDocIndexStart, &line_seg_iter, &line_seg_offset);
	getSegmentAndOffset(pos, &cursor_seg_iter, &cursor_seg_offset);

	while(line_seg_iter != mSegments.end())
	{
		const LLTextSegmentPtr segmentp = *line_seg_iter;

		if (line_seg_iter == cursor_seg_iter)
		{
			// cursor advanced to right based on difference in offset of cursor to start of line
			S32 segment_width, segment_height;
			segmentp->getDimensions(line_seg_offset, cursor_seg_offset - line_seg_offset, segment_width, segment_height);
			doc_rect.mLeft += segment_width;

			break;
		}
		else
		{
			// add remainder of current text segment to cursor position
			S32 segment_width, segment_height;
			segmentp->getDimensions(line_seg_offset, (segmentp->getEnd() - segmentp->getStart()) - line_seg_offset, segment_width, segment_height);
			doc_rect.mLeft += segment_width;
			// offset will be 0 for all segments after the first
			line_seg_offset = 0;
			// go to next text segment on this line
			++line_seg_iter;
		}
	}

	// set rect to 0 width
	doc_rect.mRight = doc_rect.mLeft; 

	return doc_rect;
}

LLRect LLTextBase::getLocalRectFromDocIndex(S32 pos) const
{
	LLRect content_window_rect = mScroller ? mScroller->getContentWindowRect() : getLocalRect();
	if (mBorderVisible)
	{
		content_window_rect.stretch(-1);
	}

	LLRect local_rect;

	if (mLineInfoList.empty()) 
	{ 
		// return default height rect in upper left
		local_rect = content_window_rect;
		local_rect.mBottom = local_rect.mTop - mFont->getLineHeight();
		return local_rect;
	}

	// get the rect in document coordinates
	LLRect doc_rect = getDocRectFromDocIndex(pos);

	// compensate for scrolled, inset view of doc
	LLRect scrolled_view_rect = getVisibleDocumentRect();
	local_rect = doc_rect;
	local_rect.translate(content_window_rect.mLeft - scrolled_view_rect.mLeft, 
						content_window_rect.mBottom - scrolled_view_rect.mBottom);

	return local_rect;
}

void LLTextBase::updateCursorXPos()
{
	// reset desired x cursor position
	mDesiredXPixel = getLocalRectFromDocIndex(mCursorPos).mLeft;
}


void LLTextBase::startOfLine()
{
	S32 offset = getLineOffsetFromDocIndex(mCursorPos);
	setCursorPos(mCursorPos - offset);
}

void LLTextBase::endOfLine()
{
	S32 line = getLineNumFromDocIndex(mCursorPos);
	S32 num_lines = getLineCount();
	if (line + 1 >= num_lines)
	{
		setCursorPos(getLength());
	}
	else
	{
		setCursorPos( getLineStart(line + 1) - 1 );
	}
}

void LLTextBase::startOfDoc()
{
	setCursorPos(0);
	if (mScroller)
	{
		mScroller->goToTop();
	}
}

void LLTextBase::endOfDoc()
{
	setCursorPos(getLength());
	if (mScroller)
	{
		mScroller->goToBottom();
	}
}

void LLTextBase::changePage( S32 delta )
{
	const S32 PIXEL_OVERLAP_ON_PAGE_CHANGE = 10;
	if (delta == 0 || !mScroller) return;

	LLRect cursor_rect = getLocalRectFromDocIndex(mCursorPos);

	if( delta == -1 )
	{
		mScroller->pageUp(PIXEL_OVERLAP_ON_PAGE_CHANGE);
	}
	else
	if( delta == 1 )
	{
		mScroller->pageDown(PIXEL_OVERLAP_ON_PAGE_CHANGE);
	}

	if (getLocalRectFromDocIndex(mCursorPos) == cursor_rect)
	{
		// cursor didn't change apparent position, so move to top or bottom of document, respectively
		if (delta < 0)
		{
			startOfDoc();
		}
		else
		{
			endOfDoc();
		}
	}
	else
	{
		setCursorAtLocalPos(cursor_rect.getCenterX(), cursor_rect.getCenterY(), true, false);
	}
}

// Picks a new cursor position based on the screen size of text being drawn.
void LLTextBase::setCursorAtLocalPos( S32 local_x, S32 local_y, bool round, bool keep_cursor_offset )
{
	setCursorPos(getDocIndexFromLocalCoord(local_x, local_y, round), keep_cursor_offset);
}


void LLTextBase::changeLine( S32 delta )
{
	S32 line = getLineNumFromDocIndex(mCursorPos);
	S32 max_line_nb = getLineCount() - 1;
	max_line_nb = (max_line_nb < 0 ? 0 : max_line_nb);
    
	S32 new_line = llclamp(line + delta, 0, max_line_nb);

    if (new_line != line)
    {
        LLRect visible_region = getVisibleDocumentRect();
        S32 new_cursor_pos = getDocIndexFromLocalCoord(mDesiredXPixel,
                                                       mLineInfoList[new_line].mRect.mBottom + mVisibleTextRect.mBottom - visible_region.mBottom, TRUE);
        setCursorPos(new_cursor_pos, true);
    }
}

bool LLTextBase::scrolledToStart()
{
	return mScroller->isAtTop();
}

bool LLTextBase::scrolledToEnd()
{
	return mScroller->isAtBottom();
}

bool LLTextBase::setCursor(S32 row, S32 column)
{
	if (row < 0 || column < 0) return false;

	S32 n_lines = mLineInfoList.size();
	for (S32 line = row; line < n_lines; ++line)
	{
		const line_info& li = mLineInfoList[line];

		if (li.mLineNum < row)
		{
			continue;
		}
		else if (li.mLineNum > row)
		{
			break; // invalid column specified
		}

		// Found the given row.
		S32 line_length = li.mDocIndexEnd - li.mDocIndexStart;;
		if (column >= line_length)
		{
			column -= line_length;
			continue;
		}

		// Found the given column.
		updateCursorXPos();
		S32 doc_pos = li.mDocIndexStart + column;
		return setCursorPos(doc_pos);
	}

	return false; // invalid row or column specified
}


bool LLTextBase::setCursorPos(S32 cursor_pos, bool keep_cursor_offset)
{
	S32 new_cursor_pos = cursor_pos;
	if (new_cursor_pos != mCursorPos)
	{
		new_cursor_pos = getEditableIndex(new_cursor_pos, new_cursor_pos >= mCursorPos);
	}

	mCursorPos = llclamp(new_cursor_pos, 0, (S32)getLength());
	needsScroll();
	if (!keep_cursor_offset)
		updateCursorXPos();
	// did we get requested position?
	return new_cursor_pos == cursor_pos;
}

// constraint cursor to editable segments of document
S32 LLTextBase::getEditableIndex(S32 index, bool increasing_direction)
{
	segment_set_t::iterator segment_iter;
	S32 offset;
	getSegmentAndOffset(index, &segment_iter, &offset);
	if (segment_iter == mSegments.end())
	{
		return 0;
	}

	LLTextSegmentPtr segmentp = *segment_iter;

	if (segmentp->canEdit()) 
	{
		return segmentp->getStart() + offset;			
	}
	else if (segmentp->getStart() < index && index < segmentp->getEnd())
	{
		// bias towards document end
		if (increasing_direction)
		{
			return segmentp->getEnd();
		}
		// bias towards document start
		else
		{
			return segmentp->getStart();
		}
	}
	else
	{
		return index;
	}
}

void LLTextBase::updateRects()
{
	LLRect old_text_rect = mVisibleTextRect;
	mVisibleTextRect = mScroller ? mScroller->getContentWindowRect() : getLocalRect();

	if (mLineInfoList.empty()) 
	{
		mTextBoundingRect = LLRect(0, mVPad, mHPad, 0);
	}
	else
	{
		mTextBoundingRect = mLineInfoList.begin()->mRect;
		for (line_list_t::const_iterator line_iter = ++mLineInfoList.begin();
			line_iter != mLineInfoList.end();
			++line_iter)
		{
			mTextBoundingRect.unionWith(line_iter->mRect);
		}

		mTextBoundingRect.mTop += mVPad;

		S32 delta_pos = 0;
		
		switch(mVAlign)
		{
		case LLFontGL::TOP:
			delta_pos = llmax(mVisibleTextRect.getHeight() - mTextBoundingRect.mTop, -mTextBoundingRect.mBottom);
			break;
		case LLFontGL::VCENTER:
			delta_pos = (llmax(mVisibleTextRect.getHeight() - mTextBoundingRect.mTop, -mTextBoundingRect.mBottom) + (mVisibleTextRect.mBottom - mTextBoundingRect.mBottom)) / 2;
			break;
		case LLFontGL::BOTTOM:
			delta_pos = mVisibleTextRect.mBottom - mTextBoundingRect.mBottom;
			break;
		case LLFontGL::BASELINE:
			// do nothing
			break;
		}
		// move line segments to fit new document rect
		for (line_list_t::iterator it = mLineInfoList.begin(); it != mLineInfoList.end(); ++it)
		{
			it->mRect.translate(0, delta_pos);
		}
		mTextBoundingRect.translate(0, delta_pos);
	}

	// update document container dimensions according to text contents
	LLRect doc_rect;
	// use old mVisibleTextRect constraint document to width of viewable region
	doc_rect.mBottom = llmin(mVisibleTextRect.mBottom,  mTextBoundingRect.mBottom);
	doc_rect.mLeft = 0;

	// allow horizontal scrolling?
	// if so, use entire width of text contents
	// otherwise, stop at width of mVisibleTextRect
	//FIXME: consider use of getWordWrap() instead
	doc_rect.mRight = mScroller 
		? llmax(mVisibleTextRect.getWidth(), mTextBoundingRect.mRight)
		: mVisibleTextRect.getWidth();
	doc_rect.mTop = llmax(mVisibleTextRect.mTop, mTextBoundingRect.mTop);

	if (!mScroller)
	{
		// push doc rect to top of text widget
		switch(mVAlign)
		{
		case LLFontGL::TOP:
			doc_rect.translate(0, mVisibleTextRect.getHeight() - doc_rect.mTop);
			break;
		case LLFontGL::VCENTER:
			doc_rect.translate(0, (mVisibleTextRect.getHeight() - doc_rect.mTop) / 2);
		case LLFontGL::BOTTOM:
		default:
			break;
		}
	}

	mDocumentView->setShape(doc_rect);

	//update mVisibleTextRect *after* mDocumentView has been resized
	// so that scrollbars are added if document needs to scroll
	// since mVisibleTextRect does not include scrollbars
	mVisibleTextRect = mScroller ? mScroller->getContentWindowRect() : getLocalRect();
	//FIXME: replace border with image?
	if (mBorderVisible)
	{
		mVisibleTextRect.stretch(-1);
	}
	if (mVisibleTextRect != old_text_rect)
	{
		needsReflow();
	}

	// update document container again, using new mVisibleTextRect (that has scrollbars enabled as needed)
	doc_rect.mBottom = llmin(mVisibleTextRect.mBottom,  mTextBoundingRect.mBottom);
	doc_rect.mLeft = 0;
	doc_rect.mRight = mScroller 
		? llmax(mVisibleTextRect.getWidth(), mTextBoundingRect.mRight)
		: mVisibleTextRect.getWidth();
	doc_rect.mTop = llmax(mVisibleTextRect.mTop, mTextBoundingRect.mTop);
	if (!mScroller)
	{
		// push doc rect to top of text widget
		switch(mVAlign)
		{
		case LLFontGL::TOP:
			doc_rect.translate(0, mVisibleTextRect.getHeight() - doc_rect.mTop);
			break;
		case LLFontGL::VCENTER:
			doc_rect.translate(0, (mVisibleTextRect.getHeight() - doc_rect.mTop) / 2);
		case LLFontGL::BOTTOM:
		default:
			break;
		}
	}
	mDocumentView->setShape(doc_rect);
}


void LLTextBase::startSelection()
{
	if( !mIsSelecting )
	{
		mIsSelecting = TRUE;
		mSelectionStart = mCursorPos;
		mSelectionEnd = mCursorPos;
	}
}

void LLTextBase::endSelection()
{
	if( mIsSelecting )
	{
		mIsSelecting = FALSE;
		mSelectionEnd = mCursorPos;
	}
}

// get portion of document that is visible in text editor
LLRect LLTextBase::getVisibleDocumentRect() const
{
	if (mScroller)
	{
		return mScroller->getVisibleContentRect();
	}
	else if (mClip)
	{
		LLRect visible_text_rect = getVisibleTextRect();
		LLRect doc_rect = mDocumentView->getRect();
		visible_text_rect.translate(-doc_rect.mLeft, -doc_rect.mBottom);

		// reject partially visible lines
		LLRect visible_lines_rect;
		for (line_list_t::const_iterator it = mLineInfoList.begin(), end_it = mLineInfoList.end();
			it != end_it;
			++it)
		{
			bool line_visible = mClipPartial ? visible_text_rect.contains(it->mRect) : visible_text_rect.overlaps(it->mRect);
			if (line_visible)
			{
				if (visible_lines_rect.isEmpty())
				{
					visible_lines_rect = it->mRect;
				}
				else
				{
					visible_lines_rect.unionWith(it->mRect);
				}
			}
		}
		return visible_lines_rect;
	}
	else
	{	// entire document rect is visible
		// but offset according to height of widget
	
		LLRect doc_rect = mDocumentView->getLocalRect();
		doc_rect.mLeft -= mDocumentView->getRect().mLeft;
		// adjust for height of text above widget baseline
		doc_rect.mBottom = doc_rect.getHeight() - mVisibleTextRect.getHeight();
		return doc_rect;
	}
}

boost::signals2::connection LLTextBase::setURLClickedCallback(const commit_signal_t::slot_type& cb)
{
	if (!mURLClickSignal)
	{
		mURLClickSignal = new commit_signal_t();
	}
	return mURLClickSignal->connect(cb);
}

boost::signals2::connection LLTextBase::setIsFriendCallback(const is_friend_signal_t::slot_type& cb)
{
	if (!mIsFriendSignal)
	{
		mIsFriendSignal = new is_friend_signal_t();
	}
	return mIsFriendSignal->connect(cb);
}

//
// LLTextSegment
//

LLTextSegment::~LLTextSegment()
{}

bool LLTextSegment::getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const { width = 0; height = 0; return false;}
S32	LLTextSegment::getOffset(S32 segment_local_x_coord, S32 start_offset, S32 num_chars, bool round) const { return 0; }
S32	LLTextSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const { return 0; }
void LLTextSegment::updateLayout(const LLTextBase& editor) {}
F32	LLTextSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect) { return draw_rect.mLeft; }
bool LLTextSegment::canEdit() const { return false; }
void LLTextSegment::unlinkFromDocument(LLTextBase*) {}
void LLTextSegment::linkToDocument(LLTextBase*) {}
const LLColor4& LLTextSegment::getColor() const { return LLColor4::white; }
//void LLTextSegment::setColor(const LLColor4 &color) {}
LLStyleConstSP LLTextSegment::getStyle() const {static LLStyleConstSP sp(new LLStyle()); return sp; }
void LLTextSegment::setStyle(LLStyleConstSP style) {}
void LLTextSegment::setToken( LLKeywordToken* token ) {}
LLKeywordToken*	LLTextSegment::getToken() const { return NULL; }
void LLTextSegment::setToolTip( const std::string &msg ) {}
void LLTextSegment::dump() const {}
BOOL LLTextSegment::handleMouseDown(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleMouseUp(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleMiddleMouseDown(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleMiddleMouseUp(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleRightMouseDown(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleRightMouseUp(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleDoubleClick(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleHover(S32 x, S32 y, MASK mask) { return FALSE; }
BOOL LLTextSegment::handleScrollWheel(S32 x, S32 y, S32 clicks) { return FALSE; }
BOOL LLTextSegment::handleToolTip(S32 x, S32 y, MASK mask) { return FALSE; }
const std::string&	LLTextSegment::getName() const 
{
	return LLStringUtil::null;
}
void LLTextSegment::onMouseCaptureLost() {}
void LLTextSegment::screenPointToLocal(S32 screen_x, S32 screen_y, S32* local_x, S32* local_y) const {}
void LLTextSegment::localPointToScreen(S32 local_x, S32 local_y, S32* screen_x, S32* screen_y) const {}
BOOL LLTextSegment::hasMouseCapture() { return FALSE; }

//
// LLNormalTextSegment
//

LLNormalTextSegment::LLNormalTextSegment( LLStyleConstSP style, S32 start, S32 end, LLTextBase& editor ) 
:	LLTextSegment(start, end),
	mStyle( style ),
	mToken(NULL),
	mEditor(editor)
{
	mFontHeight = mStyle->getFont()->getLineHeight();

	LLUIImagePtr image = mStyle->getImage();
	if (image.notNull())
	{
		mImageLoadedConnection = image->addLoadedCallback(boost::bind(&LLTextBase::needsReflow, &mEditor, start));
	}
}

LLNormalTextSegment::LLNormalTextSegment( const LLColor4& color, S32 start, S32 end, LLTextBase& editor, BOOL is_visible) 
:	LLTextSegment(start, end),
	mToken(NULL),
	mEditor(editor)
{
	mStyle = new LLStyle(LLStyle::Params().visible(is_visible).color(color));

	mFontHeight = mStyle->getFont()->getLineHeight();
}

LLNormalTextSegment::~LLNormalTextSegment()
{
	mImageLoadedConnection.disconnect();
}


F32 LLNormalTextSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
{
	if( end - start > 0 )
	{
		return drawClippedSegment( getStart() + start, getStart() + end, selection_start, selection_end, draw_rect);
	}
	return draw_rect.mLeft;
}

// Draws a single text segment, reversing the color for selection if needed.
F32 LLNormalTextSegment::drawClippedSegment(S32 seg_start, S32 seg_end, S32 selection_start, S32 selection_end, LLRect rect)
{
	F32 alpha = LLViewDrawContext::getCurrentContext().mAlpha;

	const LLWString &text = getWText();

	F32 right_x = rect.mLeft;
	if (!mStyle->isVisible())
	{
		return right_x;
	}

	const LLFontGL* font = mStyle->getFont();

	LLColor4 color = (mEditor.getReadOnly() ? mStyle->getReadOnlyColor() : mStyle->getColor())  % alpha;

	if( selection_start > seg_start )
	{
		// Draw normally
		S32 start = seg_start;
		S32 end = llmin( selection_start, seg_end );
		S32 length =  end - start;
		font->render(text, start, 
				 rect, 
				 color, 
				 LLFontGL::LEFT, mEditor.mVAlign, 
				 LLFontGL::NORMAL, 
				 mStyle->getShadowType(), 
				 length,
				 &right_x, 
				 mEditor.getUseEllipses());
	}
	rect.mLeft = (S32)ceil(right_x);
	
	if( (selection_start < seg_end) && (selection_end > seg_start) )
	{
		// Draw reversed
		S32 start = llmax( selection_start, seg_start );
		S32 end = llmin( selection_end, seg_end );
		S32 length = end - start;

		font->render(text, start, 
				 rect,
				 mStyle->getSelectedColor().get(),
				 LLFontGL::LEFT, mEditor.mVAlign, 
				 LLFontGL::NORMAL, 
				 LLFontGL::NO_SHADOW, 
				 length,
				 &right_x, 
				 mEditor.getUseEllipses());
	}
	rect.mLeft = (S32)ceil(right_x);
	if( selection_end < seg_end )
	{
		// Draw normally
		S32 start = llmax( selection_end, seg_start );
		S32 end = seg_end;
		S32 length = end - start;
		font->render(text, start, 
				 rect, 
				 color, 
				 LLFontGL::LEFT, mEditor.mVAlign, 
				 LLFontGL::NORMAL, 
				 mStyle->getShadowType(), 
				 length,
				 &right_x, 
				 mEditor.getUseEllipses());
	}
	return right_x;
}

BOOL LLNormalTextSegment::handleHover(S32 x, S32 y, MASK mask)
{
	if (getStyle() && getStyle()->isLink())
	{
		// Only process the click if it's actually in this segment, not to the right of the end-of-line.
		if(mEditor.getSegmentAtLocalPos(x, y, false) == this)
		{
			LLUI::getWindow()->setCursor(UI_CURSOR_HAND);
			return TRUE;
		}
	}
	return FALSE;
}

BOOL LLNormalTextSegment::handleRightMouseDown(S32 x, S32 y, MASK mask)
{
	if (getStyle() && getStyle()->isLink())
	{
		// Only process the click if it's actually in this segment, not to the right of the end-of-line.
		if(mEditor.getSegmentAtLocalPos(x, y, false) == this)
		{
			mEditor.createUrlContextMenu(x, y, getStyle()->getLinkHREF());
			return TRUE;
		}
	}
	return FALSE;
}

BOOL LLNormalTextSegment::handleMouseDown(S32 x, S32 y, MASK mask)
{
	if (getStyle() && getStyle()->isLink())
	{
		// Only process the click if it's actually in this segment, not to the right of the end-of-line.
		if(mEditor.getSegmentAtLocalPos(x, y, false) == this)
		{
			// eat mouse down event on hyperlinks, so we get the mouse up
			return TRUE;
		}
	}

	return FALSE;
}

BOOL LLNormalTextSegment::handleMouseUp(S32 x, S32 y, MASK mask)
{
	if (getStyle() && getStyle()->isLink())
	{
		// Only process the click if it's actually in this segment, not to the right of the end-of-line.
		if(mEditor.getSegmentAtLocalPos(x, y, false) == this)
		{
			LLUrlAction::clickAction(getStyle()->getLinkHREF());
			return TRUE;
		}
	}

	return FALSE;
}

BOOL LLNormalTextSegment::handleToolTip(S32 x, S32 y, MASK mask)
{
	std::string msg;
	// do we have a tooltip for a loaded keyword (for script editor)?
	if (mToken && !mToken->getToolTip().empty())
	{
		const LLWString& wmsg = mToken->getToolTip();
		LLToolTipMgr::instance().show(wstring_to_utf8str(wmsg));
		return TRUE;
	}
	// or do we have an explicitly set tooltip (e.g., for Urls)
	if (!mTooltip.empty())
	{
		LLToolTipMgr::instance().show(mTooltip);
		return TRUE;
	}

	return FALSE;
}

void LLNormalTextSegment::setToolTip(const std::string& tooltip)
{
	// we cannot replace a keyword tooltip that's loaded from a file
	if (mToken)
	{
		llwarns << "LLTextSegment::setToolTip: cannot replace keyword tooltip." << llendl;
		return;
	}
	mTooltip = tooltip;
}

bool LLNormalTextSegment::getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const
{
	height = 0;
	width = 0;
	if (num_chars > 0)
	{
		height = mFontHeight;
		const LLWString &text = getWText();
		// if last character is a newline, then return true, forcing line break
		width = mStyle->getFont()->getWidth(text.c_str(), mStart + first_char, num_chars);
	}
	return false;
}

S32	LLNormalTextSegment::getOffset(S32 segment_local_x_coord, S32 start_offset, S32 num_chars, bool round) const
{
	const LLWString &text = getWText();
	return mStyle->getFont()->charFromPixelOffset(text.c_str(), mStart + start_offset,
											   (F32)segment_local_x_coord,
											   F32_MAX,
											   num_chars,
											   round);
}

S32	LLNormalTextSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const
{
	const LLWString &text = getWText();

	LLUIImagePtr image = mStyle->getImage();
	if( image.notNull())
	{
		num_pixels = llmax(0, num_pixels - image->getWidth());
	}

	S32 last_char = mEnd;

	// set max characters to length of segment, or to first newline
	max_chars = llmin(max_chars, last_char - (mStart + segment_offset));

	// if no character yet displayed on this line, don't require word wrapping since
	// we can just move to the next line, otherwise insist on it so we make forward progress
	LLFontGL::EWordWrapStyle word_wrap_style = (line_offset == 0) 
		? LLFontGL::WORD_BOUNDARY_IF_POSSIBLE 
		: LLFontGL::ONLY_WORD_BOUNDARIES;
	
	
	S32 offsetLength = text.length() - (segment_offset + mStart);

	if(getLength() < segment_offset + mStart)
	{ 
		llinfos << "getLength() < segment_offset + mStart\t getLength()\t" << getLength() << "\tsegment_offset:\t" 
						<< segment_offset << "\tmStart:\t" << mStart << "\tsegments\t" << mEditor.mSegments.size() << "\tmax_chars\t" << max_chars << llendl;
	}

	if( (offsetLength + 1) < max_chars)
	{
		llinfos << "offsetString.length() + 1 < max_chars\t max_chars:\t" << max_chars << "\toffsetLength:\t" << offsetLength << " getLength() : "
			<< getLength() << "\tsegment_offset:\t" << segment_offset << "\tmStart:\t" << mStart << "\tsegments\t" << mEditor.mSegments.size() << llendl;
	}
	
	S32 num_chars = mStyle->getFont()->maxDrawableChars( text.c_str() + (segment_offset + mStart),
												(F32)num_pixels,
												max_chars, 
												word_wrap_style);

	if (num_chars == 0 
		&& line_offset == 0 
		&& max_chars > 0)
	{
		// If at the beginning of a line, and a single character won't fit, draw it anyway
		num_chars = 1;
	}

	// include *either* the EOF or newline character in this run of text
	// but not both
	S32 last_char_in_run = mStart + segment_offset + num_chars;
	// check length first to avoid indexing off end of string
	if (last_char_in_run < mEnd 
		&& (last_char_in_run >= getLength()))
	{
		num_chars++;
	}
	return num_chars;
}

void LLNormalTextSegment::dump() const
{
	llinfos << "Segment [" << 
//			mColor.mV[VX] << ", " <<
//			mColor.mV[VY] << ", " <<
//			mColor.mV[VZ] << "]\t[" <<
		mStart << ", " <<
		getEnd() << "]" <<
		llendl;
}

/*virtual*/
const LLWString& LLNormalTextSegment::getWText()	const
{
	return mEditor.getWText();
}

/*virtual*/
const S32 LLNormalTextSegment::getLength() const
{
	return mEditor.getLength();
}

LLLabelTextSegment::LLLabelTextSegment( LLStyleConstSP style, S32 start, S32 end, LLTextBase& editor )
:	LLNormalTextSegment(style, start, end, editor)
{
}

LLLabelTextSegment::LLLabelTextSegment( const LLColor4& color, S32 start, S32 end, LLTextBase& editor, BOOL is_visible)
:	LLNormalTextSegment(color, start, end, editor, is_visible)
{
}

/*virtual*/
const LLWString& LLLabelTextSegment::getWText()	const
{
	return mEditor.getWlabel();
}
/*virtual*/
const S32 LLLabelTextSegment::getLength() const
{
	return mEditor.getWlabel().length();
}

//
// LLOnHoverChangeableTextSegment
//

LLOnHoverChangeableTextSegment::LLOnHoverChangeableTextSegment( LLStyleConstSP style, LLStyleConstSP normal_style, S32 start, S32 end, LLTextBase& editor ):
	  LLNormalTextSegment(normal_style, start, end, editor),
	  mHoveredStyle(style),
	  mNormalStyle(normal_style){}

/*virtual*/ 
F32 LLOnHoverChangeableTextSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
{
	F32 result = LLNormalTextSegment::draw(start, end, selection_start, selection_end, draw_rect);
	if (end == mEnd - mStart)
	{
		mStyle = mNormalStyle;
	}
	return result;
}

/*virtual*/
BOOL LLOnHoverChangeableTextSegment::handleHover(S32 x, S32 y, MASK mask)
{
	mStyle = mHoveredStyle;
	return LLNormalTextSegment::handleHover(x, y, mask);
}


//
// LLInlineViewSegment
//

LLInlineViewSegment::LLInlineViewSegment(const Params& p, S32 start, S32 end)
:	LLTextSegment(start, end),
	mView(p.view),
	mForceNewLine(p.force_newline),
	mLeftPad(p.left_pad),
	mRightPad(p.right_pad),
	mTopPad(p.top_pad),
	mBottomPad(p.bottom_pad)
{
} 

LLInlineViewSegment::~LLInlineViewSegment()
{
	mView->die();
}

bool LLInlineViewSegment::getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const
{
	if (first_char == 0 && num_chars == 0) 
	{
		// we didn't fit on a line, the widget will fall on the next line
		// so dimensions here are 0
		width = 0;
		height = 0;
	}
	else
	{
		width = mLeftPad + mRightPad + mView->getRect().getWidth();
		height = mBottomPad + mTopPad + mView->getRect().getHeight();
	}

	return false;
}

S32	LLInlineViewSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const
{
	// if putting a widget anywhere but at the beginning of a line
	// and the widget doesn't fit or mForceNewLine is true
	// then return 0 chars for that line, and all characters for the next
	if (line_offset != 0 
		&& (mForceNewLine || num_pixels < mView->getRect().getWidth())) 
	{
		return 0;
	}
	else
	{
		return mEnd - mStart;
	}
}

void LLInlineViewSegment::updateLayout(const LLTextBase& editor)
{
	LLRect start_rect = editor.getDocRectFromDocIndex(mStart);
	mView->setOrigin(start_rect.mLeft + mLeftPad, start_rect.mBottom + mBottomPad);
}

F32	LLInlineViewSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
{
	// return padded width of widget
	// widget is actually drawn during mDocumentView's draw()
	return (F32)(draw_rect.mLeft + mView->getRect().getWidth() + mLeftPad + mRightPad);
}

void LLInlineViewSegment::unlinkFromDocument(LLTextBase* editor)
{
	editor->removeDocumentChild(mView);
}

void LLInlineViewSegment::linkToDocument(LLTextBase* editor)
{
	editor->addDocumentChild(mView);
}

LLLineBreakTextSegment::LLLineBreakTextSegment(S32 pos):LLTextSegment(pos,pos+1)
{
	LLStyleSP s( new LLStyle(LLStyle::Params().visible(true)));

	mFontHeight = s->getFont()->getLineHeight();
}
LLLineBreakTextSegment::LLLineBreakTextSegment(LLStyleConstSP style,S32 pos):LLTextSegment(pos,pos+1)
{
	mFontHeight = style->getFont()->getLineHeight();
}
LLLineBreakTextSegment::~LLLineBreakTextSegment()
{
}
bool LLLineBreakTextSegment::getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const
{
	width = 0;
	height = mFontHeight;

	return true;
}
S32	LLLineBreakTextSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const
{
	return 1;
}
F32	LLLineBreakTextSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
{
	return  draw_rect.mLeft;
}

LLImageTextSegment::LLImageTextSegment(LLStyleConstSP style,S32 pos,class LLTextBase& editor)
:	LLTextSegment(pos,pos+1),
	mStyle( style ),
	mEditor(editor)
{
}

LLImageTextSegment::~LLImageTextSegment()
{
}

static const S32 IMAGE_HPAD = 3;

bool LLImageTextSegment::getDimensions(S32 first_char, S32 num_chars, S32& width, S32& height) const
{
	width = 0;
	height = mStyle->getFont()->getLineHeight();

	LLUIImagePtr image = mStyle->getImage();
	if( num_chars>0 && image.notNull())
	{
		width += image->getWidth() + IMAGE_HPAD;
		height = llmax(height, image->getHeight() + IMAGE_HPAD );
	}
	return false;
}

S32	 LLImageTextSegment::getNumChars(S32 num_pixels, S32 segment_offset, S32 line_offset, S32 max_chars) const
{
	LLUIImagePtr image = mStyle->getImage();
	
	if (image.isNull())
	{
		return 1;
	}

	S32 image_width = image->getWidth();
	if(line_offset == 0 || num_pixels>image_width + IMAGE_HPAD)
	{
		return 1;
	}

	return 0;
}

F32	LLImageTextSegment::draw(S32 start, S32 end, S32 selection_start, S32 selection_end, const LLRect& draw_rect)
{
	if ( (start >= 0) && (end <= mEnd - mStart))
	{
		LLColor4 color = LLColor4::white % mEditor.getDrawContext().mAlpha;
		LLUIImagePtr image = mStyle->getImage();
		if (image.notNull())
		{
			S32 style_image_height = image->getHeight();
			S32 style_image_width = image->getWidth();
			// Text is drawn from the top of the draw_rect downward
			
			S32 text_center = draw_rect.mTop - (draw_rect.getHeight() / 2);
			// Align image to center of draw rect
			S32 image_bottom = text_center - (style_image_height / 2);
			image->draw(draw_rect.mLeft, image_bottom, 
				style_image_width, style_image_height, color);
			
			const S32 IMAGE_HPAD = 3;
			return draw_rect.mLeft + style_image_width + IMAGE_HPAD;
		}
	}
	return 0.0;
}

void LLTextBase::setWordWrap(bool wrap)
{
	mWordWrap = wrap;
}