summaryrefslogtreecommitdiff
path: root/desktop/textarea.c
blob: ef2c7ee25d6ffbb1fab1c9997849087ca723e229 (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
/*
 * Copyright 2006 John-Mark Bell <jmb@netsurf-browser.org>
 * Copyright 2009 Paul Blokus <paul_pl@users.sourceforge.net>
 *
 * This file is part of NetSurf, http://www.netsurf-browser.org/
 *
 * NetSurf is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; version 2 of the License.
 *
 * NetSurf 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 General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

/** \file
 * Single/Multi-line UTF-8 text area (implementation)
 */

#include <stdint.h>
#include <string.h>
#include "css/utils.h"
#include "desktop/mouse.h"
#include "desktop/textarea.h"
#include "desktop/textinput.h"
#include "desktop/plotters.h"
#include "desktop/scrollbar.h"
#include "render/font.h"
#include "utils/log.h"
#include "utils/utf8.h"
#include "utils/utils.h"

#define CARET_COLOR 0x0000FF

static plot_style_t pstyle_stroke_caret = {
    .stroke_type = PLOT_OP_TYPE_SOLID,
    .stroke_colour = CARET_COLOR,
    .stroke_width = 1,
};

struct line_info {
	unsigned int b_start;		/**< Byte offset of line start */
	unsigned int b_length;		/**< Byte length of line */
};
struct textarea_drag {
	textarea_drag_type type;
	union {
		struct scrollbar* scrollbar;
	} data;
};

struct textarea_utf8 {
	char *data;		/**< UTF-8 text */
	unsigned int alloc;	/**< Size of allocated text */
	unsigned int len;	/**< Length of text, in bytes */
	unsigned int utf8_len;	/**< Length of text, in characters without
				 *   trailing NULL */
};

struct textarea {

	int scroll_x, scroll_y;		/**< scroll offsets for the textarea */

	struct scrollbar *bar_x;	/**< Horizontal scroll. */
	struct scrollbar *bar_y;	/**< Vertical scroll. */

	unsigned int flags;		/**< Textarea flags */
	int vis_width;			/**< Visible width, in pixels */
	int vis_height;			/**< Visible height, in pixels */

	int pad_top;		/**< Top padding, inside border, in pixels */
	int pad_right;		/**< Right padding, inside border, in pixels */
	int pad_bottom;		/**< Bottom padding, inside border, in pixels */
	int pad_left;		/**< Left padding, inside border, in pixels */

	int border_width;	/**< Border width, in pixels */
	colour border_col;	/**< Border colour */

	plot_font_style_t fstyle;	/**< Text style, inc. textarea bg col */
	plot_font_style_t sel_fstyle;	/**< Selected text style */

	struct textarea_utf8 text;	/**< Textarea text content */
#define PASSWORD_REPLACEMENT "\xe2\x80\xa2"
	struct textarea_utf8 password;	/**< Text for obscured display */

	struct textarea_utf8 *show;	/**< Points at .text or .password */

	struct {
		int line;		/**< Line caret is on */
		int char_off;		/**< Character index of caret on line */
	} caret_pos;

	int caret_x;			/**< cached X coordinate of the caret */
	int caret_y;			/**< cached Y coordinate of the caret */

	int sel_start;	/**< Character index of sel start (inclusive) */
	int sel_end;	/**< Character index of sel end (exclusive) */

	int h_extent;			/**< Width of content in px */
	int v_extent;			/**< Height of content in px */
	int line_count;			/**< Count of lines */
#define LINE_CHUNK_SIZE 16
	struct line_info *lines;	/**< Line info array */
	int line_height;		/**< Line height obtained from style */

	/** Callback function for messages to client */
	textarea_client_callback callback;

	void *data;			/**< Client data for callback */

	int drag_start_char;		/**< Character index of drag start */
	struct textarea_drag drag_info;	/**< Drag information */
};



/**
 * Normalises any line endings within the text, replacing CRLF or CR with
 * LF as necessary. If the textarea is single line, then all linebreaks are
 * converted into spaces.
 *
 * \param ta		Text area
 * \param b_start	Byte offset to start at
 * \param b_len		Byte length to check
 */
static void textarea_normalise_text(struct textarea *ta,
		unsigned int b_start, unsigned int b_len)
{
	bool multi = (ta->flags & TEXTAREA_MULTILINE) ? true : false;
	unsigned int index;

	/* Remove CR characters. If it's a CRLF pair delete the CR, or replace
	 * CR with LF otherwise.
	 */
	for (index = 0; index < b_len; index++) {
		if (ta->text.data[b_start + index] == '\r') {
			if (b_start + index + 1 <= ta->text.len &&
					ta->text.data[b_start + index + 1] ==
							'\n') {
				ta->text.len--;
				ta->text.utf8_len--;
				memmove(ta->text.data + b_start + index,
						ta->text.data + b_start +
								index + 1,
						ta->text.len - b_start - index);
			}
			else
				ta->text.data[b_start + index] = '\n';
		}
		/* Replace newlines with spaces if this is a single line
		 * textarea.
		 */
		if (!multi && (ta->text.data[b_start + index] == '\n'))
			ta->text.data[b_start + index] = ' ';
	}

}


/**
 * Selects a character range in the textarea and redraws it
 *
 * \param ta		Text area
 * \param c_start	First character (inclusive)
 * \param c_end		Last character (exclusive)
 * \return 		true on success false otherwise
 */
static bool textarea_select(struct textarea *ta, int c_start, int c_end)
{
	int swap;
	struct textarea_msg msg;

	/* Ensure start is the beginning of the selection */
	if (c_start > c_end) {
		swap = c_start;
		c_start = c_end;
		c_end = swap;
	}

	if (ta->sel_start == c_start && ta->sel_end == c_end)
		return true;

	ta->sel_start = c_start;
	ta->sel_end = c_end;

	msg.ta = ta;
	msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
	msg.data.redraw.x0 = 0;
	msg.data.redraw.y0 = 0;
	msg.data.redraw.x1 = ta->vis_width;
	msg.data.redraw.y1 = ta->vis_height;

	ta->callback(ta->data, &msg);

	return true;
}


/**
 * Selects a text fragment, relative to current caret position.
 *
 * \param ta  Text area
 * \return True on success, false otherwise
 */
static bool textarea_select_fragment(struct textarea * ta)
{
	int caret_pos, sel_start = 0, sel_end = 0, index;
	size_t b_start, b_end;

	/* Fragment separators must be suitable for URLs and ordinary text */
	static const char *sep = " /:.\r\n";

	caret_pos = textarea_get_caret(ta);
	if (caret_pos < 0) {
		return false;
	}

	/* Compute byte offset of caret position */
	for (b_start = 0, index = 0; index < caret_pos;
			b_start = utf8_next(ta->show->data, ta->show->len,
					    b_start),
			index++) {
		/* Cache the character offset of the last separator */
		if (strchr(sep, ta->show->data[b_start]) != NULL) {
			/* Add one to start to skip over separator */
			sel_start = index + 1;
		}
	}

	/* Search for next separator, if any */
	for (b_end = b_start; b_end < ta->show->len;
			b_end = utf8_next(ta->show->data, ta->show->len,
					  b_end),
			index++) {
		if (strchr(sep, ta->show->data[b_end]) != NULL) {
			sel_end = index;
			break;
		}
	}

	if (sel_start < sel_end) {
		textarea_select(ta, sel_start, sel_end);
		return true;
	}

	return false;
}


/**
 * Scrolls a textarea to make the caret visible (doesn't perform a redraw)
 *
 * \param ta	The text area to be scrolled
 * \return 	true if textarea was scrolled false otherwise
 */
static bool textarea_scroll_visible(struct textarea *ta)
{
	int x0, x1, y0, y1; /* area we want caret inside */
	int x, y; /* caret pos */
	int xs = ta->scroll_x;
	int ys = ta->scroll_y;
	int vis;
	int scrollbar_width;
	bool scrolled = false;

	if (ta->caret_pos.char_off == -1)
		return false;

	scrollbar_width = (ta->bar_y == NULL) ? 0 : SCROLLBAR_WIDTH;
	x0 = ta->border_width + ta->pad_left;
	x1 = ta->vis_width - (ta->border_width + ta->pad_right);

	/* Adjust scroll pos for reduced extents */
	vis = ta->vis_width - 2 * ta->border_width - scrollbar_width;
	if (ta->h_extent - xs < vis)
		xs -= vis - (ta->h_extent - xs);

	/* Get caret pos on screen */
	x = ta->caret_x - xs;

	/* scroll as required */
	if (x < x0)
		xs += (x - x0);
	else if (x > x1)
		xs += (x - x1);

	if (ta->bar_x == NULL && ta->scroll_x != 0 &&
			ta->flags & TEXTAREA_MULTILINE) {
		/* Scrollbar removed, set to zero */
		ta->scroll_x = 0;
		scrolled = true;

	} else if (xs != ta->scroll_x) {
		/* Scrolled, set new pos. */
		if (ta->bar_x != NULL) {
			scrollbar_set(ta->bar_x, xs, false);
			xs = scrollbar_get_offset(ta->bar_x);
			if (xs != ta->scroll_x) {
				ta->scroll_x = xs;
				scrolled = true;
			}

		} else if (!(ta->flags & TEXTAREA_MULTILINE)) {
			ta->scroll_x = xs;
			scrolled = true;

		}
	}

	/* check and change vertical scroll */
	if (ta->flags & TEXTAREA_MULTILINE) {
		scrollbar_width = (ta->bar_x == NULL) ? 0 : SCROLLBAR_WIDTH;
		y0 = 0;
		y1 = ta->vis_height - 2 * ta->border_width -
				ta->pad_top - ta->pad_bottom;

		/* Adjust scroll pos for reduced extents */
		vis = ta->vis_height - 2 * ta->border_width - scrollbar_width;
		if (ta->v_extent - ys < vis)
			ys -= vis - (ta->v_extent - ys);

		/* Get caret pos on screen */
		y = ta->caret_y - ys;

		/* scroll as required */
		if (y < y0)
			ys += (y - y0);
		else if (y + ta->line_height > y1)
			ys += (y + ta->line_height - y1);

		if (ys != ta->scroll_y && ta->bar_y != NULL) {
			/* Scrolled, set new pos. */
			scrollbar_set(ta->bar_y, ys, false);
			ys = scrollbar_get_offset(ta->bar_y);
			if (ys != ta->scroll_y) {
				ta->scroll_y = ys;
				scrolled = true;
			}

		} else if (ta->bar_y == NULL && ta->scroll_y != 0) {
			/* Scrollbar removed, set to zero */
			ta->scroll_y = 0;
			scrolled = true;
		}
	}

	return scrolled;
}


/**
 * Callback for scrollbar widget.
 */
static void textarea_scrollbar_callback(void *client_data,
		struct scrollbar_msg_data *scrollbar_data)
{
	struct textarea *ta = client_data;
	struct textarea_msg msg;

	switch(scrollbar_data->msg) {
	case SCROLLBAR_MSG_MOVED:
		/* Scrolled; redraw everything */
		ta->scroll_x = scrollbar_get_offset(ta->bar_x);
		ta->scroll_y = scrollbar_get_offset(ta->bar_y);

		msg.ta = ta;
		msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
		msg.data.redraw.x0 = 0;
		msg.data.redraw.y0 = 0;
		msg.data.redraw.x1 = ta->vis_width;
		msg.data.redraw.y1 = ta->vis_height;

		ta->callback(ta->data, &msg);

		if (!(ta->flags & TEXTAREA_INTERNAL_CARET)) {
			/* Tell client where caret should be placed */
			msg.ta = ta;
			msg.type = TEXTAREA_MSG_MOVED_CARET;
			msg.data.caret.hidden = false;
			msg.data.caret.x = ta->caret_x - ta->scroll_x;
			msg.data.caret.y = ta->caret_y - ta->scroll_y;
			msg.data.caret.height = ta->line_height;

			ta->callback(ta->data, &msg);
		}
		break;

	case SCROLLBAR_MSG_SCROLL_START:
		ta->drag_info.type = TEXTAREA_DRAG_SCROLLBAR;
		ta->drag_info.data.scrollbar = scrollbar_data->scrollbar;

		msg.ta = ta;
		msg.type = TEXTAREA_MSG_DRAG_REPORT;
		msg.data.drag = ta->drag_info.type;

		/* Tell client we're handling a drag */
		ta->callback(ta->data, &msg);
		break;

	case SCROLLBAR_MSG_SCROLL_FINISHED:
		ta->drag_info.type = TEXTAREA_DRAG_NONE;

		msg.ta = ta;
		msg.type = TEXTAREA_MSG_DRAG_REPORT;
		msg.data.drag = ta->drag_info.type;

		/* Tell client we finished handling the drag */
		ta->callback(ta->data, &msg);
		break;

	default:
		break;
	}
}



/**
 * Reflow a text area from the given line onwards
 *
 * \param ta Text area to reflow
 * \param start Line number to begin reflow on
 * \return true on success false otherwise
 */
static bool textarea_reflow(struct textarea *ta, unsigned int start)
{
	char *text;
	unsigned int len;
	size_t b_off;
	int x;
	char *space, *para_end;
	unsigned int line; /* line count */
	unsigned int scroll_lines;
	int avail_width;
	int h_extent; /* horizontal extent */
	int v_extent; /* vertical extent */
	bool restart;

	if (ta->lines == NULL) {
		ta->lines =
			malloc(LINE_CHUNK_SIZE * sizeof(struct line_info));
		if (ta->lines == NULL) {
			LOG(("malloc failed"));
			return false;
		}
	}

	if (!(ta->flags & TEXTAREA_MULTILINE)) {
		/* Single line */
		int w = ta->vis_width - 2 * ta->border_width -
				ta->pad_left - ta->pad_right;

		if (ta->flags & TEXTAREA_PASSWORD &&
				ta->text.utf8_len != ta->password.utf8_len) {
			/* Make password-obscured text have same number of
			 * characters as underlying text */
			unsigned int c, b;
			int diff = ta->text.utf8_len - ta->password.utf8_len;
			unsigned int rep_len = sizeof(PASSWORD_REPLACEMENT) - 1;
			unsigned int b_len = ta->text.utf8_len * rep_len + 1;

			if (diff > 0 && b_len > ta->password.alloc) {
				/* Increase password alloaction */
				char *temp = realloc(ta->password.data,
						b_len + 64);
				if (temp == NULL) {
					LOG(("realloc failed"));
					return false;
				}

				ta->password.data = temp;
				ta->password.alloc = b_len + 64;
			}

			b_len--;
			for (c = 0; c < b_len; c += rep_len) {
				for (b = 0; b < rep_len; b++) {
					ta->password.data[c + b] =
							PASSWORD_REPLACEMENT[b];
				}
			}
			ta->password.data[b_len] = '\0';
			ta->password.len = b_len + 1;
			ta->password.utf8_len = ta->text.utf8_len;
		}

		ta->lines[0].b_start = 0;
		ta->lines[0].b_length = ta->show->len - 1;

		nsfont.font_width(&ta->fstyle, ta->show->data,
				ta->show->len, &x);

		if (x > w)
			w = x;

		ta->h_extent = w + ta->pad_left + ta->pad_right;
		ta->line_count = 1;

		return true;
	}

	/* Find max number of lines before vertical scrollbar is required */
	scroll_lines = (ta->vis_height - 2 * ta->border_width -
				ta->pad_top - ta->pad_bottom) /
				ta->line_height;

	if ((signed)start > ta->line_count)
		start = 0;
	/** \todo pay attention to start param, for now force start at zero */
	start = 0;

	do {
		/* Set line count to start point */
		line = start;

		/* Find available width */
		avail_width = ta->vis_width - 2 * ta->border_width -
				ta->pad_left - ta->pad_right;
		if (avail_width < 0)
			avail_width = 0;
		h_extent = avail_width;

		if (ta->text.len == 1) {
			/* Handle empty textarea */
			assert(ta->text.data[0] == '\0');
			ta->lines[line].b_start = 0;
			ta->lines[line++].b_length = 0;
		}

		restart = false;
		for (len = ta->text.len - 1, text = ta->text.data; len > 0;
				len -= b_off, text += b_off) {

			/* Find end of paragraph */
			for (para_end = text; para_end < text + len;
					para_end++) {
				if (*para_end == '\n')
					break;
			}

			/* Wrap current line in paragraph */
			nsfont.font_split(&ta->fstyle, text, para_end - text,
					avail_width, &b_off, &x);
			/* b_off now marks space, or end of paragraph */

			if (x > h_extent) {
				h_extent = x;
			}
			if (x > avail_width && ta->bar_x == NULL) {
				/* We need to insert a horizontal scrollbar */
				int w = ta->vis_width - 2 * ta->border_width;
				if (!scrollbar_create(true, w, w, w,
						ta, textarea_scrollbar_callback,
						&(ta->bar_x)))
					return false;
				if (ta->bar_y != NULL)
					scrollbar_make_pair(ta->bar_x,
							ta->bar_y);
				ta->pad_bottom += SCROLLBAR_WIDTH;

				/* Find new max visible lines */
				scroll_lines = (ta->vis_height -
						2 * ta->border_width -
						ta->pad_top - ta->pad_bottom) /
						ta->line_height;
			}

			if (line > 0 && line % LINE_CHUNK_SIZE == 0) {
				struct line_info *temp = realloc(ta->lines,
						(line + LINE_CHUNK_SIZE) *
						sizeof(struct line_info));
				if (temp == NULL) {
					LOG(("realloc failed"));
					return false;
				}

				ta->lines = temp;
			}

			if (para_end == text + b_off && *para_end == '\n') {
				/* Not found any spaces to wrap at, and we
				 * have a newline char */
				ta->lines[line].b_start = text - ta->text.data;
				ta->lines[line++].b_length = para_end - text;

				/* Jump newline */
				b_off++;

				if (len - b_off == 0) {
					/* reached end of input;
					 * add last line */
					ta->lines[line].b_start = text +
							b_off - ta->text.data;
					ta->lines[line++].b_length = 0;
				}

				if (line > scroll_lines && ta->bar_y == NULL)
					break;

				continue;

			} else if (len - b_off > 0) {
				/* soft wraped, find last space (if any) */
				for (space = text + b_off; space > text;
						space--) {
					if (*space == ' ')
						break;
				}

				if (space != text)
					b_off = space + 1 - text;
			}

			ta->lines[line].b_start = text - ta->text.data;
			ta->lines[line++].b_length = b_off;

			if (line > scroll_lines && ta->bar_y == NULL)
				break;
		}

		if (h_extent <= avail_width && ta->bar_x != NULL) {
			/* We need to remove a horizontal scrollbar */
			scrollbar_destroy(ta->bar_x);
			ta->bar_x = NULL;
			ta->pad_bottom -= SCROLLBAR_WIDTH;

			/* Find new max visible lines */
			scroll_lines = (ta->vis_height - 2 * ta->border_width -
					ta->pad_top - ta->pad_bottom) /
					ta->line_height;
		}

		if (line > scroll_lines && ta->bar_y == NULL) {
			/* Add vertical scrollbar */
			int h = ta->vis_height - 2 * ta->border_width;
			if (!scrollbar_create(false, h, h, h,
					ta, textarea_scrollbar_callback,
					&(ta->bar_y)))
				return false;
			if (ta->bar_x != NULL)
				scrollbar_make_pair(ta->bar_x,
						ta->bar_y);
			ta->pad_right += SCROLLBAR_WIDTH;
			restart = true;

		} else if (line <= scroll_lines && ta->bar_y != NULL) {
			/* Remove vertical scrollbar */
			scrollbar_destroy(ta->bar_y);
			ta->bar_y = NULL;
			ta->pad_right -= SCROLLBAR_WIDTH;
			restart = true;
		}
	} while (restart);

	h_extent += ta->pad_left + ta->pad_right -
			(ta->bar_y != NULL ? SCROLLBAR_WIDTH : 0);
	v_extent = line * ta->line_height + ta->pad_top +
				ta->pad_bottom -
				(ta->bar_x != NULL ? SCROLLBAR_WIDTH : 0);

	if (ta->bar_x != NULL) {
		/* Set horizontal scrollbar extents */
		int w = ta->vis_width - 2 * ta->border_width -
				(ta->bar_y != NULL ? SCROLLBAR_WIDTH : 0);
		scrollbar_set_extents(ta->bar_x, w, w, h_extent);
	}

	if (ta->bar_y != NULL) {
		/* Set vertical scrollbar extents */
		int h = ta->vis_height - 2 * ta->border_width;
		scrollbar_set_extents(ta->bar_y, h,
				h - (ta->bar_x != NULL ? SCROLLBAR_WIDTH : 0),
				v_extent);
	}

	ta->h_extent = h_extent;
	ta->v_extent = v_extent;
	ta->line_count = line;

	return true;
}


/**
 * get byte/character offset from the beginning of the text for some coordinates
 *
 * \param ta		Text area
 * \param x		X coordinate
 * \param y		Y coordinate
 * \param c_off		Updated to character offset
 */
static void textarea_get_xy_offset(struct textarea *ta, int x, int y,
		unsigned int *c_off)
{
	size_t bpos, temp; /* Byte positions in utf8 string */
	unsigned int cpos; /* Character positions in utf8 string */
	int line;

	if (!ta->line_count) {
		*c_off = 0;
		return;
	}

	x = x - ta->border_width - ta->pad_left + ta->scroll_x;
	y = y - ta->border_width - ta->pad_top + ta->scroll_y;

	if (x < 0)
		x = 0;

	line = y / ta->line_height;

	if (line < 0)
		line = 0;
	if (ta->line_count - 1 < line)
		line = ta->line_count - 1;

	/* Get byte position */
	nsfont.font_position_in_string(&ta->fstyle,
			ta->show->data + ta->lines[line].b_start,
			ta->lines[line].b_length, x, &bpos, &x);


	/* If the calculated byte offset corresponds with the number of bytes
	 * in the line, and the line has been soft-wrapped, then ensure the
	 * caret offset is before the trailing space character, rather than
	 * after it. Otherwise, the caret will be placed at the start of the
	 * following line, which is undesirable.
	 */
	if (ta->flags & TEXTAREA_MULTILINE &&
			ta->show->data[ta->lines[line].b_start +
					ta->lines[line].b_length] > 0 &&
			bpos == (unsigned)ta->lines[line].b_length &&
			ta->show->data[ta->lines[line].b_start +
					ta->lines[line].b_length - 1] == ' ')
		bpos--;

	/* Get character position */
	for (temp = 0, cpos = 0; temp < bpos + ta->lines[line].b_start;
			temp = utf8_next(ta->show->data, ta->show->len, temp))
		cpos++;

	/* Set the return values */
	*c_off = cpos;
}


/**
 * Set the caret's position
 *
 * \param ta Text area
 * \param x X position of caret in a window relative to text area top left
 * \param y Y position of caret in a window relative to text area top left
 * \return true on success false otherwise
 */
static bool textarea_set_caret_xy(struct textarea *ta, int x, int y)
{
	unsigned int c_off;

	if (ta->flags & TEXTAREA_READONLY)
		return true;

	textarea_get_xy_offset(ta, x, y, &c_off);

	return textarea_set_caret(ta, c_off);
}


/**
 * Insert text into the text area
 *
 * \param ta		Text area
 * \param c_index	0-based character index to insert at
 * \param text		UTF-8 text to insert
 * \param b_len		Byte length of UTF-8 text
 * \return false on memory exhaustion, true otherwise
 */
static bool textarea_insert_text(struct textarea *ta, unsigned int c_index,
		const char *text, size_t b_len)
{
	size_t b_off;

	if (ta->flags & TEXTAREA_READONLY)
		return true;

	/* Find insertion point */
	if (c_index > ta->text.utf8_len)
		c_index = ta->text.utf8_len;

	/* find byte offset of insertion point */
	for (b_off = 0; c_index > 0;
			b_off = utf8_next(ta->text.data, ta->text.len, b_off))
		c_index--;

	if (b_len + ta->text.len >= ta->text.alloc) {
		char *temp = realloc(ta->text.data, b_len + ta->text.len + 64);
		if (temp == NULL) {
			LOG(("realloc failed"));
			return false;
		}

		ta->text.data = temp;
		ta->text.alloc = b_len + ta->text.len + 64;
	}

	/* Shift text following up */
	memmove(ta->text.data + b_off + b_len, ta->text.data + b_off,
			ta->text.len - b_off);
	/* Insert new text */
	memcpy(ta->text.data + b_off, text, b_len);
	ta->text.len += b_len;
	ta->text.utf8_len += utf8_bounded_length(text, b_len);

	textarea_normalise_text(ta, b_off, b_len);

	/** \todo calculate line to reflow from */
	return textarea_reflow(ta, 0);

}


static inline void textarea_char_to_byte_offset(struct textarea_utf8 *text,
		unsigned int start, unsigned int end,
		size_t *b_start, size_t *b_end)
{
	size_t diff = end - start;
	/* find byte offset of replace start */
	for (*b_start = 0; start-- > 0;
			*b_start = utf8_next(text->data, text->len, *b_start))
		; /* do nothing */

	/* find byte length of replaced text */
	for (*b_end = *b_start; diff-- > 0;
			*b_end = utf8_next(text->data, text->len, *b_end))
		; /* do nothing */
}


/**
 * Replace text in a text area
 *
 * \param ta		Text area
 * \param start		Start character index of replaced section (inclusive)
 * \param end		End character index of replaced section (exclusive)
 * \param rep		Replacement UTF-8 text to insert
 * \param rep_len	Byte length of replacement UTF-8 text
 * \param add_to_clipboard	True iff replaced text to be added to clipboard
 * \return false on memory exhaustion, true otherwise
 */
static bool textarea_replace_text(struct textarea *ta, unsigned int start,
		unsigned int end, const char *rep, size_t rep_len,
		bool add_to_clipboard)
{
	size_t b_start, b_end;

	if (ta->flags & TEXTAREA_READONLY)
		return true;

	if (start > ta->text.utf8_len)
		start = ta->text.utf8_len;
	if (end > ta->text.utf8_len)
		end = ta->text.utf8_len;

	if (start == end && rep != NULL)
		return textarea_insert_text(ta, start, rep, rep_len);

	if (start > end)
		return false;

	/* Place CUTs on clipboard */
	if (add_to_clipboard) {
		textarea_char_to_byte_offset(ta->show, start, end,
				&b_start, &b_end);
		gui_set_clipboard(ta->show->data + b_start, b_end - b_start,
				NULL, 0);
	}

	textarea_char_to_byte_offset(&ta->text, start, end, &b_start, &b_end);

	if (rep == NULL) {
		/* No replacement text */
		return true;
	}

	/* Ensure textarea's text buffer is large enough */
	if (rep_len + ta->text.len - (b_end - b_start) >= ta->text.alloc) {
		char *temp = realloc(ta->text.data,
			rep_len + ta->text.len - (b_end - b_start) + 64);
		if (temp == NULL) {
			LOG(("realloc failed"));
			return false;
		}

		ta->text.data = temp;
		ta->text.alloc =
			rep_len + ta->text.len - (b_end - b_start) + 64;
	}

	/* Shift text following to new position */
	memmove(ta->text.data + b_start + rep_len, ta->text.data + b_end,
			ta->text.len - b_end);

	/* Insert new text */
	memcpy(ta->text.data + b_start, rep, rep_len);

	ta->text.len += (int)rep_len - (b_end - b_start);
	ta->text.utf8_len = utf8_length(ta->text.data);
	textarea_normalise_text(ta, b_start, rep_len);

	/** \todo calculate line to reflow from */
	return textarea_reflow(ta, 0);
}


/**
 * Handles the end of a drag operation
 *
 * \param ta	Text area
 * \param mouse	the mouse state at drag end moment
 * \param x	X coordinate
 * \param y	Y coordinate
 * \return true if drag end was handled false otherwise
 */
static bool textarea_drag_end(struct textarea *ta, browser_mouse_state mouse,
		int x, int y)
{
	int c_end;
	unsigned int c_off;
	struct textarea_msg msg;

	assert(ta->drag_info.type != TEXTAREA_DRAG_NONE);

	switch (ta->drag_info.type) {
	case TEXTAREA_DRAG_SCROLLBAR:
		if (ta->drag_info.data.scrollbar == ta->bar_x) {
			x -= ta->border_width;
			y -= ta->vis_height - ta->border_width -
					SCROLLBAR_WIDTH;
		} else {
			x -= ta->vis_width - ta->border_width -
					SCROLLBAR_WIDTH;
			y -= ta->border_width;
		}
		scrollbar_mouse_drag_end(ta->drag_info.data.scrollbar,
				mouse, x, y);
		assert(ta->drag_info.type == TEXTAREA_DRAG_NONE);

		/* Return, since drag end already reported to textarea client */
		return true;

	case TEXTAREA_DRAG_SELECTION:
		ta->drag_info.type = TEXTAREA_DRAG_NONE;

		textarea_get_xy_offset(ta, x, y, &c_off);
		c_end = c_off;

		if (!textarea_select(ta, ta->drag_start_char, c_end))
			return false;

		break;

	default:
		return false;
	}

	/* Report drag end to client, if not already reported */
	assert(ta->drag_info.type == TEXTAREA_DRAG_NONE);

	msg.ta = ta;
	msg.type = TEXTAREA_MSG_DRAG_REPORT;
	msg.data.drag = ta->drag_info.type;

	ta->callback(ta->data, &msg);

	return true;
}




/* exported interface, documented in textarea.h */
struct textarea *textarea_create(const textarea_flags flags,
		const textarea_setup *setup,
		textarea_client_callback callback, void *data)
{
	struct textarea *ret;

	/* Sanity check flags */
	assert(!(flags & TEXTAREA_MULTILINE &&
			flags & TEXTAREA_PASSWORD));

	if (callback == NULL) {
		LOG(("no callback provided"));
		return NULL;
	}

	ret = malloc(sizeof(struct textarea));
	if (ret == NULL) {
		LOG(("malloc failed"));
		return NULL;
	}

	ret->callback = callback;
	ret->data = data;

	ret->flags = flags;
	ret->vis_width = setup->width;
	ret->vis_height = setup->height;

	ret->pad_top = setup->pad_top;
	ret->pad_right = setup->pad_right;
	ret->pad_bottom = setup->pad_bottom;
	ret->pad_left = setup->pad_left;

	ret->border_width = setup->border_width;
	ret->border_col = setup->border_col;

	ret->fstyle = setup->text;

	ret->sel_fstyle = setup->text;
	ret->sel_fstyle.foreground = setup->selected_text;
	ret->sel_fstyle.background = setup->selected_bg;

	ret->scroll_x = 0;
	ret->scroll_y = 0;
	ret->bar_x = NULL;
	ret->bar_y = NULL;
	ret->drag_start_char = 0;
	ret->drag_info.type = TEXTAREA_DRAG_NONE;


	ret->text.data = malloc(64);
	if (ret->text.data == NULL) {
		LOG(("malloc failed"));
		free(ret);
		return NULL;
	}
	ret->text.data[0] = '\0';
	ret->text.alloc = 64;
	ret->text.len = 1;
	ret->text.utf8_len = 0;

	if (flags & TEXTAREA_PASSWORD) {
		ret->password.data = malloc(64);
		if (ret->password.data == NULL) {
			LOG(("malloc failed"));
			free(ret->text.data);
			free(ret);
			return NULL;
		}
		ret->password.data[0] = '\0';
		ret->password.alloc = 64;
		ret->password.len = 1;
		ret->password.utf8_len = 0;

		ret->show = &ret->password;

	} else {
		ret->password.data = NULL;
		ret->password.alloc = 0;
		ret->password.len = 0;
		ret->password.utf8_len = 0;

		ret->show = &ret->text;
	}

	ret->line_height = FIXTOINT(FDIV((FMUL(FLTTOFIX(1.3),
			FMUL(nscss_screen_dpi, INTTOFIX((setup->text.size))))),
			FONT_SIZE_SCALE * F_72));

	ret->caret_pos.line = ret->caret_pos.char_off = -1;
	ret->caret_x = 0;
	ret->caret_y = 0;
	ret->sel_start = -1;
	ret->sel_end = -1;

	ret->line_count = 0;
	ret->lines = NULL;

	return ret;
}


/* exported interface, documented in textarea.h */
void textarea_destroy(struct textarea *ta)
{
	if (ta->bar_x)
		scrollbar_destroy(ta->bar_x);
	if (ta->bar_y)
		scrollbar_destroy(ta->bar_y);

	if (ta->flags & TEXTAREA_PASSWORD)
		free(ta->password.data);

	free(ta->text.data);
	free(ta->lines);
	free(ta);
}


/* exported interface, documented in textarea.h */
bool textarea_set_text(struct textarea *ta, const char *text)
{
	unsigned int len = strlen(text) + 1;

	if (len >= ta->text.alloc) {
		char *temp = realloc(ta->text.data, len + 64);
		if (temp == NULL) {
			LOG(("realloc failed"));
			return false;
		}
		ta->text.data = temp;
		ta->text.alloc = len + 64;
	}

	memcpy(ta->text.data, text, len);
	ta->text.len = len;
	ta->text.utf8_len = utf8_length(ta->text.data);

	textarea_normalise_text(ta, 0, len);

	return textarea_reflow(ta, 0);
}


/* exported interface, documented in textarea.h */
bool textarea_drop_text(struct textarea *ta, const char *text,
		size_t text_length)
{
	struct textarea_msg msg;
	unsigned int caret_pos;
	size_t text_chars;

	if (ta->flags & TEXTAREA_READONLY)
		return false;

	if (text == NULL)
		return false;

	text_chars = utf8_bounded_length(text, text_length);
	caret_pos = textarea_get_caret(ta);

	if (ta->sel_start != -1) {
		if (!textarea_replace_text(ta, ta->sel_start, ta->sel_end,
				text, text_length, false))
			return false;

		caret_pos = ta->sel_start + text_chars;
		ta->sel_start = ta->sel_end = -1;
	} else {
		if (!textarea_replace_text(ta, caret_pos, caret_pos,
				text, text_length, false))
			return false;
		caret_pos += text_chars;
	}

	textarea_set_caret(ta, caret_pos);

	msg.ta = ta;
	msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
	msg.data.redraw.x0 = 0;
	msg.data.redraw.y0 = 0;
	msg.data.redraw.x1 = ta->vis_width;
	msg.data.redraw.y1 = ta->vis_height;

	ta->callback(ta->data, &msg);

	return true;
}


/* exported interface, documented in textarea.h */
int textarea_get_text(struct textarea *ta, char *buf, unsigned int len)
{
	if (buf == NULL && len == 0) {
		/* want length */
		return ta->text.len;

	} else if (buf == NULL) {
		/* Can't write to NULL */
		return -1;
	}

	if (len < ta->text.len) {
		LOG(("buffer too small"));
		return -1;
	}

	memcpy(buf, ta->text.data, ta->text.len);

	return ta->text.len;
}


/* exported interface, documented in textarea.h */
bool textarea_set_caret(struct textarea *ta, int caret)
{
	unsigned int c_len;
	unsigned int b_off;
	int i;
	int index;
	int x, y;
	int x0, y0, x1, y1;
	int text_y_offset;
	int width, height;
	struct textarea_msg msg;

	if (ta->flags & TEXTAREA_READONLY)
		return true;

	c_len = ta->show->utf8_len;

	if (caret != -1 && caret > (signed)c_len)
		caret = c_len;

	if (ta->flags & TEXTAREA_MULTILINE) {
		/* Multiline textarea */
		text_y_offset = ta->border_width + ta->pad_top;
	} else {
		/* Single line text area; text is vertically centered */
		text_y_offset = ta->border_width;
		text_y_offset += (ta->vis_height - ta->line_height + 1) / 2;
	}

	/* Delete the old caret */
	if (ta->caret_pos.char_off != -1) {
		x0 = ta->caret_x - ta->scroll_x;
		y0 = ta->caret_y + text_y_offset - ta->scroll_y;
		width = 2;
		height = ta->line_height;

		msg.ta = ta;
		msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
		msg.data.redraw.x0 = x0;
		msg.data.redraw.y0 = y0;
		msg.data.redraw.x1 = x0 + width;
		msg.data.redraw.y1 = y0 + height;

		ta->callback(ta->data, &msg);
	}

	/* check if the caret has to be drawn at all */
	if (caret != -1) {
		/* Find byte offset of caret position */
		for (b_off = 0; caret > 0; caret--)
			b_off = utf8_next(ta->show->data, ta->show->len, b_off);

		/* Now find line in which byte offset appears */
		for (i = 0; i < ta->line_count - 1; i++)
			if (ta->lines[i + 1].b_start > b_off)
				break;

		ta->caret_pos.line = i;

		/* Now calculate the char. offset of the caret in this line */
		for (c_len = 0, ta->caret_pos.char_off = 0;
				c_len < b_off - ta->lines[i].b_start;
				c_len = utf8_next(ta->show->data +
						ta->lines[i].b_start,
						ta->lines[i].b_length, c_len))
			ta->caret_pos.char_off++;

		/* Finally, redraw the caret */
		index = textarea_get_caret(ta);
		if (index == -1)
			return false;

		/* find byte offset of caret position */
		for (b_off = 0; index-- > 0;
				b_off = utf8_next(ta->show->data,
						ta->show->len, b_off))
			; /* do nothing */

		nsfont.font_width(&ta->fstyle,
				ta->show->data +
				ta->lines[ta->caret_pos.line].b_start,
				b_off - ta->lines[ta->caret_pos.line].b_start,
				&x);

		x += ta->border_width + ta->pad_left;
		ta->caret_x = x;
		y = ta->line_height * ta->caret_pos.line + text_y_offset;
		ta->caret_y = y;

		if (!textarea_scroll_visible(ta) &&
				ta->flags & TEXTAREA_INTERNAL_CARET) {
			/* Didn't scroll, just moved caret.
			 * Caret is internal caret, redraw it */
			x -= ta->scroll_x;
			y -= ta->scroll_y;
			x0 = max(x - 1, ta->border_width);
			y0 = max(y, 0);
			x1 = min(x + 1, ta->vis_width - ta->border_width);
			y1 = min(y + ta->line_height,
					ta->vis_height);

			width = x1 - x0;
			height = y1 - y0;

			if (width > 0 && height > 0) {
				msg.ta = ta;
				msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
				msg.data.redraw.x0 = x0;
				msg.data.redraw.y0 = y0;
				msg.data.redraw.x1 = x0 + width;
				msg.data.redraw.y1 = y0 + height;

				ta->callback(ta->data, &msg);
			}
		}

		if (!(ta->flags & TEXTAREA_INTERNAL_CARET)) {
			/* Tell client where caret should be placed */
			msg.ta = ta;
			msg.type = TEXTAREA_MSG_MOVED_CARET;
			msg.data.caret.hidden = false;
			msg.data.caret.x = x - ta->scroll_x;
			msg.data.caret.y = y - ta->scroll_y;
			msg.data.caret.height = ta->line_height;

			ta->callback(ta->data, &msg);
		}

	} else if (!(ta->flags & TEXTAREA_INTERNAL_CARET)) {
		/* Caret hidden, and client is responsible: tell client */
		msg.ta = ta;
		msg.type = TEXTAREA_MSG_MOVED_CARET;
		msg.data.caret.hidden = true;

		ta->callback(ta->data, &msg);
	}

	return true;
}


/* exported interface, documented in textarea.h */
int textarea_get_caret(struct textarea *ta)
{
	unsigned int c_off = 0, b_off;

	/* Ensure caret isn't hidden */
	if (ta->caret_pos.char_off < 0)
		textarea_set_caret(ta, 0);

	/* if the text is a trailing NUL only */
	if (ta->text.utf8_len == 0)
		return 0;

	/* Calculate character offset of this line's start */
	for (b_off = 0; b_off < ta->lines[ta->caret_pos.line].b_start;
			b_off = utf8_next(ta->text.data, ta->text.len, b_off))
		c_off++;

	return c_off + ta->caret_pos.char_off;
}


/* exported interface, documented in textarea.h */
void textarea_redraw(struct textarea *ta, int x, int y, colour bg, float scale,
		const struct rect *clip, const struct redraw_context *ctx)
{
	const struct plotter_table *plot = ctx->plot;
	int line0, line1, line, left, right, line_y;
	int chars, text_y_offset, text_y_offset_baseline;
	unsigned int c_pos, c_len, c_len_part, b_start, b_end, line_len;
	unsigned int sel_start, sel_end;
	char *line_text;
	struct rect r, s;
	bool selected = false;
	plot_font_style_t fstyle;
	int fsize = ta->fstyle.size;
	int line_height = ta->line_height;
	plot_style_t plot_style_fill_bg = {
		.stroke_type = PLOT_OP_TYPE_NONE,
		.stroke_width = 0,
		.stroke_colour = NS_TRANSPARENT,
		.fill_type = PLOT_OP_TYPE_SOLID,
		.fill_colour = ta->border_col
	};

	r = *clip;

	if (r.x1 < x || r.x0 > x + ta->vis_width || r.y1 < y ||
		   r.y0 > y + ta->vis_height)
		/* Textarea outside the clipping rectangle */
		return;

	if (ta->lines == NULL)
		/* Nothing to redraw */
		return;

	line0 = (r.y0 - y + ta->scroll_y) / ta->line_height - 1;
	line1 = (r.y1 - y + ta->scroll_y) / ta->line_height + 1;

	if (line0 < 0)
		line0 = 0;
	if (line1 < 0)
		line1 = 0;
	if (ta->line_count - 1 < line0)
		line0 = ta->line_count - 1;
	if (ta->line_count - 1 < line1)
		line1 = ta->line_count - 1;
	if (line1 < line0)
		line1 = line0;

	if (r.x0 < x)
		r.x0 = x;
	if (r.y0 < y)
		r.y0 = y;
	if (scale == 1.0) {
		if (r.x1 > x + ta->vis_width)
			r.x1 = x + ta->vis_width;
		if (r.y1 > y + ta->vis_height)
			r.y1 = y + ta->vis_height;
	} else {
		if (r.x1 > x + ta->vis_width * scale)
			r.x1 = x + ta->vis_width * scale;
		if (r.y1 > y + ta->vis_height * scale)
			r.y1 = y + ta->vis_height * scale;
	}

	plot->clip(&r);
	if (ta->border_col != NS_TRANSPARENT &&
			ta->border_width > 0) {
		/* Plot border */
		plot->rectangle(x, y, x + ta->vis_width, y + ta->vis_height,
				&plot_style_fill_bg);
	}
	if (ta->fstyle.background != NS_TRANSPARENT) {
		/* Plot background */
		plot_style_fill_bg.fill_colour = ta->fstyle.background;
		plot->rectangle(x + ta->border_width, y + ta->border_width,
				x + ta->vis_width - ta->border_width,
				y + ta->vis_height - ta->border_width,
				&plot_style_fill_bg);
	}

	if (scale == 1.0) {
		if (r.x0 < x + ta->border_width)
			r.x0 = x + ta->border_width;
		if (r.x1 > x + ta->vis_width - ta->border_width)
			r.x1 = x + ta->vis_width - ta->border_width;
		if (r.y0 < y + ta->border_width)
			r.y0 = y + ta->border_width;
		if (r.y1 > y + ta->vis_height - ta->border_width -
				(ta->bar_x != NULL ? SCROLLBAR_WIDTH : 0))
			r.y1 = y + ta->vis_height - ta->border_width -
					(ta->bar_x != NULL ? SCROLLBAR_WIDTH :
							0);
	} else {
		if (r.x0 < x + ta->border_width * scale)
			r.x0 = x + ta->border_width * scale;
		if (r.x1 > x + (ta->vis_width - ta->border_width) * scale)
			r.x1 = x + (ta->vis_width - ta->border_width) * scale;
		if (r.y0 < y + ta->border_width * scale)
			r.y0 = y + ta->border_width * scale;
		if (r.y1 > y + (ta->vis_height - ta->border_width -
				(ta->bar_x != NULL ? SCROLLBAR_WIDTH : 0)) *
				scale)
			r.y1 = y + (ta->vis_height - ta->border_width -
					(ta->bar_x != NULL ? SCROLLBAR_WIDTH :
							0) * scale);
	}

	if (line0 > 0)
		c_pos = utf8_bounded_length(ta->show->data,
				ta->lines[line0].b_start);
	else
		c_pos = 0;

	text_y_offset = text_y_offset_baseline = ta->border_width;
	if (ta->flags & TEXTAREA_MULTILINE) {
		/* Multiline textarea */
		text_y_offset += ta->pad_top;
		text_y_offset_baseline += (ta->line_height * 3 + 2) / 4 +
				ta->pad_top;
	} else {
		/* Single line text area; text is vertically centered */
		int vis_height = ta->vis_height - 2 * ta->border_width;
		text_y_offset += (vis_height - ta->line_height + 1) / 2;
		text_y_offset_baseline += (vis_height * 3 + 2) / 4;
	}

	if (scale != 1.0) {
		text_y_offset *= scale;
		text_y_offset_baseline *= scale;

		fsize *= scale;
		line_height *= scale;
	}

	plot_style_fill_bg.fill_colour = ta->sel_fstyle.background;

	for (line = line0; (line <= line1) &&
			(y + line * ta->line_height <= r.y1 + ta->scroll_y);
			line++) {
		if (ta->lines[line].b_length == 0) {
			c_pos++;
			continue;
		}

		/* reset clip rectangle */
		plot->clip(&r);

		c_len = utf8_bounded_length(
				&(ta->show->data[ta->lines[line].b_start]),
				ta->lines[line].b_length);

		b_end = 0;
		right = x + ta->border_width + ta->pad_left - ta->scroll_x;

		do {
			sel_start = ta->sel_start;
			sel_end = ta->sel_end;
			/* get length of part of line */
			if (ta->sel_end == -1 || ta->sel_end == ta->sel_start ||
					sel_end <= c_pos ||
					sel_start > c_pos + c_len) {
				/* rest of line unselected */
				selected = false;
				c_len_part = c_len;
				fstyle = ta->fstyle;

			} else if (sel_start <= c_pos &&
					sel_end > c_pos + c_len) {
				/* rest of line selected */
				selected = true;
				c_len_part = c_len;
				fstyle = ta->sel_fstyle;

			} else if (sel_start > c_pos) {
				/* next part of line unselected */
				selected = false;
				c_len_part = sel_start - c_pos;
				fstyle = ta->fstyle;

			} else if (sel_end > c_pos) {
				/* next part of line selected */
				selected = true;
				c_len_part = sel_end - c_pos;
				fstyle = ta->sel_fstyle;

			} else {
				assert(0);
			}
			fstyle.size = fsize;

			line_text = &(ta->show->data[ta->lines[line].b_start]);
			line_len = ta->lines[line].b_length;

			/* find b_start and b_end for this part of the line */
			b_start = b_end;

			chars = c_len_part;
			for (b_end = b_start; chars > 0; chars--)
				b_end = utf8_next(line_text, line_len, b_end);

			/* find clip left/right for this part of line */
			left = right;
			nsfont.font_width(&fstyle, line_text,
					b_end, &right);
			right += x + ta->border_width + ta->pad_left -
					ta->scroll_x;

			/* set clip rectangle for line part */
			s = r;
			if (s.x0 < left)
				s.x0 = left;
			if (s.x1 > right)
				s.x1 = right;

			plot->clip(&s);

			line_y = line * ta->line_height - ta->scroll_y;

			if (scale != 1.0) {
				line_y *= scale;
			}

			if (selected) {
				/* draw selection fill */
				plot->rectangle(s.x0, y + line_y +
					text_y_offset,
					s.x1, y + line_y + line_height +
							text_y_offset,
					&plot_style_fill_bg);
			}

			/* draw text */
			plot->text(x + ta->border_width + ta->pad_left -
					ta->scroll_x,
					y + line_y + text_y_offset_baseline,
					ta->show->data +
							ta->lines[line].b_start,
					ta->lines[line].b_length, &fstyle);

			c_pos += c_len_part;
			c_len -= c_len_part;

		} while (c_pos < c_pos + c_len);

		/* if there is a newline between the lines, skip it */
		if (line < ta->line_count - 1 &&
				ta->lines[line + 1].b_start !=
						ta->lines[line].b_start +
						ta->lines[line].b_length)
			c_pos++;
	}

	plot->clip(clip);

	if ((ta->sel_end == -1 || ta->sel_start == ta->sel_end) &&
			ta->caret_pos.char_off >= 0) {
		/* There is no selection, and caret visible: show caret */
		int caret_y = y - ta->scroll_y + ta->caret_y;

		if (ta->flags & TEXTAREA_INTERNAL_CARET) {
			/* Render our own caret */
			plot->line(x - ta->scroll_x + ta->caret_x, caret_y,
					x - ta->scroll_x + ta->caret_x,
					caret_y + ta->line_height,
					&pstyle_stroke_caret);
		}
	}

	if (ta->bar_x != NULL)
		scrollbar_redraw(ta->bar_x,
				x / scale + ta->border_width,
				y / scale + ta->vis_height - ta->border_width -
						SCROLLBAR_WIDTH,
				clip, scale, ctx);

	if (ta->bar_y != NULL)
		scrollbar_redraw(ta->bar_y,
				x / scale + ta->vis_width - ta->border_width -
						SCROLLBAR_WIDTH,
				y / scale + ta->border_width,
				clip, scale, ctx);
}


/* exported interface, documented in textarea.h */
bool textarea_keypress(struct textarea *ta, uint32_t key)
{
	struct textarea_msg msg;
	char utf8[6];
	unsigned int caret, caret_init, length, l_len, b_off, b_len;
	int c_line, c_chars, line;
	bool redraw = false;
	bool readonly;

	caret_init = caret = textarea_get_caret(ta);
	line = ta->caret_pos.line;
	readonly = (ta->flags & TEXTAREA_READONLY ? true : false);

	if (!(key <= 0x001F || (0x007F <= key && key <= 0x009F))) {
		/* normal character insertion */
		length = utf8_from_ucs4(key, utf8);
		utf8[length] = '\0';

		if (ta->sel_start != -1) {
			if (!textarea_replace_text(ta,
					ta->sel_start,
					ta->sel_end, utf8, length, false))
				return false;

			caret = ta->sel_start + 1;
			ta->sel_start = ta->sel_end = -1;
			redraw = true;
		} else {
			if (!textarea_replace_text(ta,
					caret, caret,
					utf8, length, false))
				return false;
			caret++;
			redraw = true;
		}

	} else switch (key) {
		case KEY_SELECT_ALL:
			caret = ta->text.utf8_len;

			ta->sel_start = 0;
			ta->sel_end = ta->text.utf8_len;
			redraw = true;
			break;
		case KEY_COPY_SELECTION:
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start,
						ta->sel_end,
						NULL, 0, true))
					return false;
			}
			break;
		case KEY_DELETE_LEFT:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
				     		ta->sel_start,
						ta->sel_end, "", 0, false))
					return false;

				caret = ta->sel_start;
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			} else if (caret > 0) {
				if (!textarea_replace_text(ta,
						caret - 1,
						caret, "", 0, false))
					return false;
				caret--;
				redraw = true;
			}
			break;
		case KEY_CR:
		case KEY_NL:
			if (readonly)
				break;
			if(!textarea_insert_text(ta, caret, "\n", 1))
				return false;
			caret++;
			ta->sel_start = ta->sel_end = -1;
			redraw = true;
			break;
		case KEY_CUT_LINE:
			break;
		case KEY_PASTE:
		{
			char *clipboard;
			size_t clipboard_length;
			size_t clipboard_chars;

			if (readonly)
				break;

			gui_get_clipboard(&clipboard, &clipboard_length);
			if (clipboard == NULL)
				return false;
			clipboard_chars = utf8_bounded_length(clipboard,
					clipboard_length);

			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start, ta->sel_end,
						clipboard, clipboard_length,
						false))
					return false;

				caret = ta->sel_start + clipboard_chars;
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			} else {
				if (!textarea_replace_text(ta,
						caret, caret,
						clipboard, clipboard_length,
						false))
					return false;
				caret += clipboard_chars;
				redraw = true;
			}

			free(clipboard);
		}
			break;
		case KEY_CUT_SELECTION:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start,
						ta->sel_end, "", 0, true))
					return false;

				caret = ta->sel_start;
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_ESCAPE:
			/* Fall through to KEY_CLEAR_SELECTION */
		case KEY_CLEAR_SELECTION:
			ta->sel_start = -1;
			ta->sel_end = -1;
			redraw = true;
			break;
		case KEY_LEFT:
			if (readonly)
				break;
			if (caret > 0)
				caret--;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_RIGHT:
			if (readonly)
				break;
			if (caret < ta->text.utf8_len)
				caret++;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_PAGE_UP:
			if (readonly)
				break;
			if (ta->flags & TEXTAREA_MULTILINE) {
				/* +1 because one line is subtracted in
				   KEY_UP */
				line = ta->caret_pos.line - (ta->vis_height +
						ta->line_height - 1) /
						ta->line_height + 1;
			}
			/* fall through */
		case KEY_UP:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			if (!(ta->flags & TEXTAREA_MULTILINE))
				break;

			line--;
			if (line < 0)
				line = 0;
			if (line == ta->caret_pos.line)
				break;

			b_off = ta->lines[line].b_start;
			b_len = ta->lines[line].b_length;

			c_line = ta->caret_pos.line;
			c_chars = ta->caret_pos.char_off;

			if (b_len > 0 && ta->text.data[b_off + b_len - 1] == ' '
					&& line < ta->line_count - 1)
				b_len--;

			l_len = utf8_bounded_length(&(ta->text.data[b_off]),
					b_len);

			ta->caret_pos.line = line;
			ta->caret_pos.char_off = min(l_len,
					(unsigned)ta->caret_pos.char_off);

			caret = textarea_get_caret(ta);

			ta->caret_pos.line = c_line;
			ta->caret_pos.char_off = c_chars;

			break;
		case KEY_PAGE_DOWN:
			if (readonly)
				break;
			if (ta->flags & TEXTAREA_MULTILINE) {
				/* -1 because one line is added in KEY_DOWN */
				line = ta->caret_pos.line + (ta->vis_height +
						ta->line_height - 1) /
						ta->line_height
						- 1;
			}
			/* fall through */
		case KEY_DOWN:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			if (!(ta->flags & TEXTAREA_MULTILINE))
				break;

			line++;
			if (line > ta->line_count - 1)
				line = ta->line_count - 1;
			if (line == ta->caret_pos.line)
				break;

			b_off = ta->lines[line].b_start;
			b_len = ta->lines[line].b_length;

			c_line = ta->caret_pos.line;
			c_chars = ta->caret_pos.char_off;

			if (ta->text.data[b_off + b_len - 1] == ' ' &&
					line < ta->line_count - 1)
				b_len--;

			l_len = utf8_bounded_length(&(ta->text.data[b_off]),
					b_len);

			ta->caret_pos.line = line;
			ta->caret_pos.char_off = min(l_len,
					(unsigned)ta->caret_pos.char_off);

			caret = textarea_get_caret(ta);

			ta->caret_pos.line = c_line;
			ta->caret_pos.char_off = c_chars;

			break;
		case KEY_DELETE_RIGHT:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start,
						ta->sel_end, "", 0, false))
					return false;

				caret = ta->sel_start;
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			} else {
				if (caret < ta->text.utf8_len) {
					if (!textarea_replace_text(ta,
							caret, caret + 1,
							"", 0, false))
						return false;
					redraw = true;
				}
			}
			break;
		case KEY_LINE_START:
			if (readonly)
				break;
			caret -= ta->caret_pos.char_off;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_LINE_END:
			if (readonly)
				break;

			caret = utf8_bounded_length(ta->text.data,
					ta->lines[ta->caret_pos.line].b_start +
					ta->lines[ta->caret_pos.line].b_length);
			if (ta->text.data[ta->lines[ta->caret_pos.line].
					b_start +
					ta->lines[ta->caret_pos.line].b_length
					- 1] == ' ')
				caret--;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_TEXT_START:
			if (readonly)
				break;
			caret = 0;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_TEXT_END:
			if (readonly)
				break;
			caret = ta->text.utf8_len;
			if (ta->sel_start != -1) {
				ta->sel_start = ta->sel_end = -1;
				redraw = true;
			}
			break;
		case KEY_WORD_LEFT:
		case KEY_WORD_RIGHT:
			break;
		case KEY_DELETE_LINE_END:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start,
						ta->sel_end, "", 0, false))
					return false;
				ta->sel_start = ta->sel_end = -1;
			} else {
				b_off = ta->lines[ta->caret_pos.line].b_start;
				b_len = ta->lines[ta->caret_pos.line].b_length;
				l_len = utf8_bounded_length(
						&(ta->text.data[b_off]),
						b_len);
				if (!textarea_replace_text(ta, caret,
						caret + l_len, "", 0, false))
					return false;
			}
			redraw = true;
			break;
		case KEY_DELETE_LINE_START:
			if (readonly)
				break;
			if (ta->sel_start != -1) {
				if (!textarea_replace_text(ta,
						ta->sel_start,
						ta->sel_end, "", 0, false))
					return false;
				ta->sel_start = ta->sel_end = -1;
			} else {
				if (!textarea_replace_text(ta,
						caret - ta->caret_pos.char_off,
					caret, "", 0, false))
					return false;
				caret -= ta->caret_pos.char_off;
			}
			redraw = true;
			break;
		default:
			return false;
	}


	if (caret != caret_init)
		textarea_set_caret(ta, caret);
	//TODO:redraw only the important part
	if (redraw) {
		msg.ta = ta;
		msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
		msg.data.redraw.x0 = 0;
		msg.data.redraw.y0 = 0;
		msg.data.redraw.x1 = ta->vis_width;
		msg.data.redraw.y1 = ta->vis_height;

		ta->callback(ta->data, &msg);
	}

	return true;
}


/* exported interface, documented in textarea.h */
bool textarea_mouse_action(struct textarea *ta, browser_mouse_state mouse,
		int x, int y)
{
	int c_start, c_end;
	int sx, sy; /* xy coord offset for scrollbar */
	int sl; /* scrollbar length */
	unsigned int c_off;
	struct textarea_msg msg;

	if (ta->drag_info.type != TEXTAREA_DRAG_NONE &&
			mouse == BROWSER_MOUSE_HOVER) {
		/* There is a drag that we must end */
		textarea_drag_end(ta, mouse, x, y);
	}

	if (ta->drag_info.type == TEXTAREA_DRAG_SCROLLBAR) {
		/* Scrollbar drag in progress; pass input to scrollbar */
		if (ta->drag_info.data.scrollbar == ta->bar_x) {
			x -= ta->border_width;
			y -= ta->vis_height - ta->border_width -
					SCROLLBAR_WIDTH;
		} else {
			x -= ta->vis_width - ta->border_width -
					SCROLLBAR_WIDTH;
			y -= ta->border_width;
		}
		scrollbar_mouse_action(ta->drag_info.data.scrollbar,
				mouse, x, y);
		return true;
	}

	/* Horizontal scrollbar */
	if (ta->bar_x != NULL && ta->drag_info.type == TEXTAREA_DRAG_NONE) {
		/* No drag happening, but mouse input is over scrollbar;
		 * pass input to scrollbar */
		sx = x - ta->border_width;
		sy = y - (ta->vis_height - ta->border_width - SCROLLBAR_WIDTH);
		sl = ta->vis_width - 2 * ta->border_width -
				(ta->bar_y != NULL ? SCROLLBAR_WIDTH : 0);

		if (sx >= 0 && sy >= 0 && sx < sl && sy < SCROLLBAR_WIDTH) {
			scrollbar_mouse_action(ta->bar_x, mouse, sx, sy);
			return true;
		}
	}

	/* Vertical scrollbar */
	if (ta->bar_y != NULL && ta->drag_info.type == TEXTAREA_DRAG_NONE) {
		/* No drag happening, but mouse input is over scrollbar;
		 * pass input to scrollbar */
		sx = x - (ta->vis_width - ta->border_width - SCROLLBAR_WIDTH);
		sy = y - ta->border_width;
		sl = ta->vis_height - 2 * ta->border_width;

		if (sx >= 0 && sy >= 0 && sx < SCROLLBAR_WIDTH && sy < sl) {
			scrollbar_mouse_action(ta->bar_y, mouse, sx, sy);
			return true;
		}
	}

	/* mouse button pressed above the text area, move caret */
	if (mouse & BROWSER_MOUSE_PRESS_1) {
		if (!(ta->flags & TEXTAREA_READONLY)) {
			textarea_get_xy_offset(ta, x, y, &c_off);
			ta->drag_start_char = c_off;

			textarea_set_caret(ta, c_off);
		}
		if (ta->sel_start != -1) {
			/* remove selection */
			ta->sel_start = ta->sel_end = -1;

			msg.ta = ta;
			msg.type = TEXTAREA_MSG_REDRAW_REQUEST;
			msg.data.redraw.x0 = 0;
			msg.data.redraw.y0 = 0;
			msg.data.redraw.x1 = ta->vis_width;
			msg.data.redraw.y1 = ta->vis_height;

			ta->callback(ta->data, &msg);
		}

	} else if (mouse & BROWSER_MOUSE_DOUBLE_CLICK) {
		if (!(ta->flags & TEXTAREA_READONLY)) {
			textarea_set_caret_xy(ta, x, y);
			return textarea_select_fragment(ta);
		}

	} else if (mouse & BROWSER_MOUSE_DRAG_1) {
		/* Selection start */
		textarea_get_xy_offset(ta, x, y, &c_off);
		c_start = ta->drag_start_char;
		c_end = c_off;
		ta->drag_info.type = TEXTAREA_DRAG_SELECTION;

		msg.ta = ta;
		msg.type = TEXTAREA_MSG_DRAG_REPORT;
		msg.data.drag = ta->drag_info.type;

		ta->callback(ta->data, &msg);

		return textarea_select(ta, c_start, c_end);
	} else if (mouse & BROWSER_MOUSE_HOLDING_1 &&
			ta->drag_info.type == TEXTAREA_DRAG_SELECTION) {
		/* Selection track */
		int scrx = 0;
		int scry = 0;

		textarea_get_xy_offset(ta, x, y, &c_off);
		c_start = ta->drag_start_char;
		c_end = c_off;

		/* selection auto-scroll */
		if (x < 0)
			scrx = x / 4;
		else if (x > ta->vis_width)
			scrx = (x - ta->vis_width) / 4;

		if (y < 0)
			scry = y / 4;
		else if (y > ta->vis_height)
			scry = (y - ta->vis_height) / 4;

		textarea_scroll(ta, scrx, scry);

		return textarea_select(ta, c_start, c_end);
	}

	return true;
}


/* exported interface, documented in textarea.h */
void textarea_get_dimensions(struct textarea *ta, int *width, int *height)
{
	if (width != NULL)
		*width = ta->vis_width;
	if (height != NULL)
		*height = ta->vis_height;
}


/* exported interface, documented in textarea.h */
void textarea_set_dimensions(struct textarea *ta, int width, int height)
{
	ta->vis_width = width;
	ta->vis_height = height;
	textarea_reflow(ta, 0);
}


/* exported interface, documented in textarea.h */
void textarea_set_layout(struct textarea *ta, int width, int height,
		int top, int right, int bottom, int left)
{
	ta->vis_width = width;
	ta->vis_height = height;
	ta->pad_top = top;
	ta->pad_right = right;
	ta->pad_bottom = bottom;
	ta->pad_left = left;
	textarea_reflow(ta, 0);
}


/* exported interface, documented in textarea.h */
bool textarea_scroll(struct textarea *ta, int scrx, int scry)
{
	bool handled_scroll = false;

	if (ta->bar_x != NULL && scrx != 0 &&
			scrollbar_scroll(ta->bar_x, scrx))
		handled_scroll = true;
	if (ta->bar_y != NULL && scry != 0 &&
			scrollbar_scroll(ta->bar_y, scry))
		handled_scroll = true;

	return handled_scroll;
}