File: ScintillaView.mm

package info (click to toggle)
codequery 1.0.1%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 17,860 kB
  • sloc: cpp: 151,420; xml: 16,576; python: 5,602; ansic: 5,487; makefile: 559; perl: 496; ruby: 209; sql: 194; sh: 106; php: 53; vhdl: 51; erlang: 47; objc: 22; lisp: 18; cobol: 18; modula3: 17; asm: 14; fortran: 12; ml: 11; tcl: 6
file content (2246 lines) | stat: -rw-r--r-- 79,761 bytes parent folder | download | duplicates (2)
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

/**
 * Implementation of the native Cocoa View that serves as container for the scintilla parts.
 * @file ScintillaView.mm
 *
 * Created by Mike Lischke.
 *
 * Copyright 2011, 2013, Oracle and/or its affiliates. All rights reserved.
 * Copyright 2009, 2011 Sun Microsystems, Inc. All rights reserved.
 * This file is dual licensed under LGPL v2.1 and the Scintilla license (http://www.scintilla.org/License.txt).
 */

#include <cmath>

#include <string_view>
#include <vector>
#include <optional>

#import "ScintillaTypes.h"
#import "ScintillaMessages.h"
#import "ScintillaStructures.h"

#import "Debugging.h"
#import "Geometry.h"
#import "Platform.h"
#import "ScintillaView.h"
#import "ScintillaCocoa.h"

#if !__has_feature(objc_arc)
#error ARC must be enabled
#endif

using namespace Scintilla;
using namespace Scintilla::Internal;

// Add backend property to ScintillaView as a private category.
// Specified here as backend accessed by SCIMarginView and SCIContentView.
@interface ScintillaView()
@property(nonatomic, readonly) Scintilla::Internal::ScintillaCocoa *backend;
@end

// Two additional cursors we need, which aren't provided by Cocoa.
static NSCursor *reverseArrowCursor;
static NSCursor *waitCursor;

NSString *const SCIUpdateUINotification = @"SCIUpdateUI";

/**
 * Provide an NSCursor object that matches the Window::Cursor enumeration.
 */
static NSCursor *cursorFromEnum(Window::Cursor cursor) {
	switch (cursor) {
	case Window::Cursor::text:
		return [NSCursor IBeamCursor];
	case Window::Cursor::arrow:
		return [NSCursor arrowCursor];
	case Window::Cursor::wait:
		return waitCursor;
	case Window::Cursor::horizontal:
		return [NSCursor resizeLeftRightCursor];
	case Window::Cursor::vertical:
		return [NSCursor resizeUpDownCursor];
	case Window::Cursor::reverseArrow:
		return reverseArrowCursor;
	case Window::Cursor::up:
	default:
		return [NSCursor arrowCursor];
	}
}

@implementation SCIScrollView
- (void) tile {
	[super tile];

#if defined(MAC_OS_X_VERSION_10_14)
	if (std::floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_13) {
		NSRect frame = self.contentView.frame;
		frame.origin.x = self.verticalRulerView.requiredThickness;
		frame.size.width -= frame.origin.x;
		self.contentView.frame = frame;
	}
#endif
}
@end

// Add marginWidth and owner properties as a private category.
@interface SCIMarginView()
@property(assign) int marginWidth;
@property(nonatomic, weak) ScintillaView *owner;
@end

@implementation SCIMarginView {
	int marginWidth;
	ScintillaView *__weak owner;
	NSMutableArray *currentCursors;
}

@synthesize marginWidth, owner;

- (instancetype) initWithScrollView: (NSScrollView *) aScrollView {
	self = [super initWithScrollView: aScrollView orientation: NSVerticalRuler];
	if (self != nil) {
		owner = nil;
		marginWidth = 20;
		currentCursors = [NSMutableArray arrayWithCapacity: 0];
		for (size_t i=0; i<=SC_MAX_MARGIN; i++) {
			[currentCursors addObject: reverseArrowCursor];
		}
		self.clientView = aScrollView.documentView;
		if ([self respondsToSelector: @selector(setAccessibilityLabel:)])
			self.accessibilityLabel = @"Scintilla Margin";
	}
	return self;
}


- (void) setFrame: (NSRect) frame {
	super.frame = frame;

	[self.window invalidateCursorRectsForView: self];
}

- (CGFloat) requiredThickness {
	return marginWidth;
}

- (void) drawHashMarksAndLabelsInRect: (NSRect) aRect {
	if (owner) {
		NSRect contentRect = self.scrollView.contentView.bounds;
		NSRect marginRect = self.bounds;
		// Ensure paint to bottom of view to avoid glitches
		if (marginRect.size.height > contentRect.size.height) {
			// Legacy scroll bar mode leaves a poorly painted corner
			aRect = marginRect;
		}
		owner.backend->PaintMargin(aRect);
	}
}

/**
 * Called by the framework if it wants to show a context menu for the margin.
 */
- (NSMenu *) menuForEvent: (NSEvent *) theEvent {
	NSMenu *menu = [owner menuForEvent: theEvent];
	if (menu) {
		return menu;
	} else if (owner.backend->ShouldDisplayPopupOnMargin()) {
		return owner.backend->CreateContextMenu(theEvent);
	} else {
		return nil;
	}
}

- (void) mouseDown: (NSEvent *) theEvent {
	NSClipView *textView = self.scrollView.contentView;
	[textView.window makeFirstResponder: textView];
	owner.backend->MouseDown(theEvent);
}

- (void) rightMouseDown: (NSEvent *) theEvent {
	[NSMenu popUpContextMenu: [self menuForEvent: theEvent] withEvent: theEvent forView: self];

	owner.backend->RightMouseDown(theEvent);
}

- (void) mouseDragged: (NSEvent *) theEvent {
	owner.backend->MouseMove(theEvent);
}

- (void) mouseMoved: (NSEvent *) theEvent {
	owner.backend->MouseMove(theEvent);
}

- (void) mouseUp: (NSEvent *) theEvent {
	owner.backend->MouseUp(theEvent);
}

// Not a simple button so return failure
- (BOOL) accessibilityPerformPress {
	return NO;
}

/**
 * This method is called to give us the opportunity to define our mouse sensitive rectangle.
 */
- (void) resetCursorRects {
	[super resetCursorRects];

	int x = 0;
	NSRect marginRect = self.bounds;
	size_t co = currentCursors.count;
	for (size_t i=0; i<co; i++) {
		long cursType = owner.backend->WndProc(Message::GetMarginCursorN, i, 0);
		long width =owner.backend->WndProc(Message::GetMarginWidthN, i, 0);
		NSCursor *cc = cursorFromEnum(static_cast<Window::Cursor>(cursType));
		currentCursors[i] = cc;
		marginRect.origin.x = x;
		marginRect.size.width = width;
		[self addCursorRect: marginRect cursor: cc];
		x += width;
	}
}

- (void) drawRect: (NSRect) rect {
	if (!NSContainsRect(self.bounds, rect)) {
	    rect = self.bounds;
	}
	[super drawRect:rect];
}

@end

// Add owner property as a private category.
@interface SCIContentView()
@property(nonatomic, weak) ScintillaView *owner;
@end

@implementation SCIContentView {
	ScintillaView *__weak mOwner;
	NSCursor *mCurrentCursor;
	NSTrackingArea *trackingArea;

	// Set when we are in composition mode and partial input is displayed.
	NSRange mMarkedTextRange;
}

@synthesize owner = mOwner;

//--------------------------------------------------------------------------------------------------

- (NSView *) initWithFrame: (NSRect) frame {
	self = [super initWithFrame: frame];

	if (self != nil) {
		// Some initialization for our view.
		mCurrentCursor = [NSCursor arrowCursor];
		trackingArea = nil;
		mMarkedTextRange = NSMakeRange(NSNotFound, 0);

		if (@available(macOS 10.13, *)) {
			[self registerForDraggedTypes: @[NSPasteboardTypeString, ScintillaRecPboardType, NSPasteboardTypeFileURL]];
		} else {
			// Use old deprecated type
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wdeprecated-declarations"
			[self registerForDraggedTypes: @[NSPasteboardTypeString, ScintillaRecPboardType, NSFilenamesPboardType]];
#pragma clang diagnostic pop
		}

		// Set up accessibility in the text role
		if ([self respondsToSelector: @selector(setAccessibilityElement:)]) {
			self.accessibilityElement = TRUE;
			self.accessibilityEnabled = TRUE;
			self.accessibilityLabel = NSLocalizedString(@"Scintilla", nil);	// No real localization
			self.accessibilityRoleDescription = @"source code editor";
			self.accessibilityRole = NSAccessibilityTextAreaRole;
			self.accessibilityIdentifier = @"Scintilla";
		}
	}

	return self;
}

//--------------------------------------------------------------------------------------------------

/**
 * When the view is resized or scrolled we need to update our tracking area.
 */
- (void) updateTrackingAreas {
	if (trackingArea) {
		[self removeTrackingArea: trackingArea];
	}

	int opts = (NSTrackingActiveAlways | NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved);
	trackingArea = [[NSTrackingArea alloc] initWithRect: self.bounds
						    options: opts
						      owner: self
						   userInfo: nil];
	[self addTrackingArea: trackingArea];
	[super updateTrackingAreas];
}

//--------------------------------------------------------------------------------------------------

/**
 * When the view is resized we need to let the backend know.
 */
- (void) setFrame: (NSRect) frame {
	super.frame = frame;

	mOwner.backend->Resize();

	[super prepareContentInRect: [self visibleRect]];
}

//--------------------------------------------------------------------------------------------------

/**
 * Called by the backend if a new cursor must be set for the view.
 */
- (void) setCursor: (int) cursor {
	Window::Cursor eCursor = (Window::Cursor)cursor;
	mCurrentCursor = cursorFromEnum(eCursor);

	// Trigger recreation of the cursor rectangle(s).
	[self.window invalidateCursorRectsForView: self];
	[mOwner updateMarginCursors];
}

//--------------------------------------------------------------------------------------------------

/**
 * This method is called to give us the opportunity to define our mouse sensitive rectangle.
 */
- (void) resetCursorRects {
	[super resetCursorRects];

	// We only have one cursor rect: our bounds.
	const NSRect visibleBounds = mOwner.backend->GetBounds();
	[self addCursorRect: visibleBounds cursor: mCurrentCursor];
}

//--------------------------------------------------------------------------------------------------

/**
 * Called before repainting.
 */
- (void) viewWillDraw {
	if (!mOwner) {
		[super viewWillDraw];
		return;
	}

	const NSRect *rects;
	NSInteger nRects = 0;
	[self getRectsBeingDrawn: &rects count: &nRects];
	if (nRects > 0) {
		NSRect rectUnion = rects[0];
		for (int i=0; i<nRects; i++) {
			rectUnion = NSUnionRect(rectUnion, rects[i]);
		}
		mOwner.backend->WillDraw(rectUnion);
	}
	[super viewWillDraw];
}

//--------------------------------------------------------------------------------------------------

/**
 * Called before responsive scrolling overdraw.
 */
- (void) prepareContentInRect: (NSRect) rect {
	if (mOwner)
		mOwner.backend->WillDraw(rect);
#if MAC_OS_X_VERSION_MAX_ALLOWED > 1080
	[super prepareContentInRect: rect];
#endif
}

//--------------------------------------------------------------------------------------------------

/**
 * Gets called by the runtime when the effective appearance changes.
 */
- (void) viewDidChangeEffectiveAppearance {
	if (mOwner.backend) {
		mOwner.backend->UpdateBaseElements();
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Gets called by the runtime when the view needs repainting.
 */
- (void) drawRect: (NSRect) rect {
	CGContextRef context = CGContextCurrent();

	if (!mOwner.backend->Draw(rect, context)) {
		dispatch_async(dispatch_get_main_queue(), ^ {
			[self setNeedsDisplay: YES];
		});
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Windows uses a client coordinate system where the upper left corner is the origin in a window
 * (and so does Scintilla). We have to adjust for that. However by returning YES here, we are
 * already done with that.
 * Note that because of returning YES here most coordinates we use now (e.g. for painting,
 * invalidating rectangles etc.) are given with +Y pointing down!
 */
- (BOOL) isFlipped {
	return YES;
}

//--------------------------------------------------------------------------------------------------

- (BOOL) isOpaque {
	return YES;
}

//--------------------------------------------------------------------------------------------------

/**
 * Implement the "click through" behavior by telling the caller we accept the first mouse event too.
 */
- (BOOL) acceptsFirstMouse: (NSEvent *) theEvent {
#pragma unused(theEvent)
	return YES;
}

//--------------------------------------------------------------------------------------------------

/**
 * Make this view accepting events as first responder.
 */
- (BOOL) acceptsFirstResponder {
	return YES;
}

//--------------------------------------------------------------------------------------------------

/**
 * Called by the framework if it wants to show a context menu for the editor.
 */
- (NSMenu *) menuForEvent: (NSEvent *) theEvent {
	NSMenu *menu = [mOwner menuForEvent: theEvent];
	if (menu) {
		return menu;
	} else if (mOwner.backend->ShouldDisplayPopupOnText()) {
		return mOwner.backend->CreateContextMenu(theEvent);
	} else {
		return nil;
	}
}

//--------------------------------------------------------------------------------------------------

// Adoption of NSTextInputClient protocol.

- (NSAttributedString *) attributedSubstringForProposedRange: (NSRange) aRange actualRange: (NSRangePointer) actualRange {
	const NSInteger lengthCharacters = self.accessibilityNumberOfCharacters;
	if (aRange.location > lengthCharacters) {
		return nil;
	}
	const NSRange posRange = mOwner.backend->PositionsFromCharacters(aRange);
	// The backend validated aRange and may have removed characters beyond the end of the document.
	const NSRange charRange = mOwner.backend->CharactersFromPositions(posRange);
	if (!NSEqualRanges(aRange, charRange)) {
		*actualRange = charRange;
	}

	[mOwner message: SCI_SETTARGETRANGE wParam: posRange.location lParam: NSMaxRange(posRange)];
	std::string text([mOwner message: SCI_TARGETASUTF8], 0);
	[mOwner message: SCI_TARGETASUTF8 wParam: 0 lParam: reinterpret_cast<sptr_t>(&text[0])];
	text = FixInvalidUTF8(text);
	NSString *result = @(text.c_str());
	NSMutableAttributedString *asResult = [[NSMutableAttributedString alloc] initWithString: result];

	const NSRange rangeAS = NSMakeRange(0, asResult.length);
	// SCI_GETSTYLEAT reports a signed byte but want an unsigned to index into styles
	const char styleByte = static_cast<char>([mOwner message: SCI_GETSTYLEAT wParam: posRange.location]);
	const long style = static_cast<unsigned char>(styleByte);
	std::string fontName([mOwner message: SCI_STYLEGETFONT wParam: style lParam: 0], 0);
	[mOwner message: SCI_STYLEGETFONT wParam: style lParam: (sptr_t)&fontName[0]];
	const CGFloat fontSize = [mOwner message: SCI_STYLEGETSIZEFRACTIONAL wParam: style] / 100.0f;
	NSString *sFontName = @(fontName.c_str());
	NSFont *font = [NSFont fontWithName: sFontName size: fontSize];
	if (font) {
		[asResult addAttribute: NSFontAttributeName value: font range: rangeAS];
	}

	return asResult;
}

//--------------------------------------------------------------------------------------------------

- (NSUInteger) characterIndexForPoint: (NSPoint) point {
	const NSRect rectPoint = {point, NSZeroSize};
	const NSRect rectInWindow = [self.window convertRectFromScreen: rectPoint];
	const NSRect rectLocal = [self.superview.superview convertRect: rectInWindow fromView: nil];

	const long position = [mOwner message: SCI_CHARPOSITIONFROMPOINT
				       wParam: rectLocal.origin.x
				       lParam: rectLocal.origin.y];
	if (position == Sci::invalidPosition) {
		return NSNotFound;
	} else {
		const NSRange index = mOwner.backend->CharactersFromPositions(NSMakeRange(position, 0));
		return index.location;
	}
}

//--------------------------------------------------------------------------------------------------

- (void) doCommandBySelector: (SEL) selector {
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Warc-performSelector-leaks"
	if ([self respondsToSelector: selector])
		[self performSelector: selector withObject: nil];
#pragma clang diagnostic pop
}

//--------------------------------------------------------------------------------------------------

- (NSRect) firstRectForCharacterRange: (NSRange) aRange actualRange: (NSRangePointer) actualRange {
#pragma unused(actualRange)
	const NSRange posRange = mOwner.backend->PositionsFromCharacters(aRange);

	NSRect rect;
	rect.origin.x = [mOwner message: SCI_POINTXFROMPOSITION wParam: 0 lParam: posRange.location];
	rect.origin.y = [mOwner message: SCI_POINTYFROMPOSITION wParam: 0 lParam: posRange.location];
	const NSUInteger rangeEnd = NSMaxRange(posRange);
	rect.size.width = [mOwner message: SCI_POINTXFROMPOSITION wParam: 0 lParam: rangeEnd] - rect.origin.x;
	rect.size.height = [mOwner message: SCI_POINTYFROMPOSITION wParam: 0 lParam: rangeEnd] - rect.origin.y;
	rect.size.height += [mOwner message: SCI_TEXTHEIGHT wParam: 0 lParam: 0];
	const NSRect rectInWindow = [self.superview.superview convertRect: rect toView: nil];
	const NSRect rectScreen = [self.window convertRectToScreen: rectInWindow];

	return rectScreen;
}

//--------------------------------------------------------------------------------------------------

- (BOOL) hasMarkedText {
	return mMarkedTextRange.length > 0;
}

//--------------------------------------------------------------------------------------------------

/**
 * General text input. Used to insert new text at the current input position, replacing the current
 * selection if there is any.
 * First removes the replacementRange.
 */
- (void) insertText: (id) aString replacementRange: (NSRange) replacementRange {
	if ((mMarkedTextRange.location != NSNotFound) && (replacementRange.location != NSNotFound)) {
		NSLog(@"Trying to insertText when there is both a marked range and a replacement range");
	}

	// Remove any previously marked text first.
	mOwner.backend->CompositionUndo();
	if (mMarkedTextRange.location != NSNotFound) {
		const NSRange posRangeMark = mOwner.backend->PositionsFromCharacters(mMarkedTextRange);
		[mOwner message: SCI_SETEMPTYSELECTION wParam: posRangeMark.location];
	}
	mMarkedTextRange = NSMakeRange(NSNotFound, 0);

	if (replacementRange.location == (NSNotFound-1))
		// This occurs when the accent popup is visible and menu selected.
		// Its replacing a non-existent position so do nothing.
		return;

	if (replacementRange.location != NSNotFound) {
		const NSRange posRangeReplacement = mOwner.backend->PositionsFromCharacters(replacementRange);
		[mOwner message: SCI_DELETERANGE
			 wParam: posRangeReplacement.location
			 lParam: posRangeReplacement.length];
		[mOwner message: SCI_SETEMPTYSELECTION wParam: posRangeReplacement.location];
	}

	NSString *newText = @"";
	if ([aString isKindOfClass: [NSString class]])
		newText = (NSString *) aString;
	else if ([aString isKindOfClass: [NSAttributedString class]])
		newText = (NSString *) [aString string];

	mOwner.backend->InsertText(newText, CharacterSource::DirectInput);
}

//--------------------------------------------------------------------------------------------------

- (NSRange) markedRange {
	return mMarkedTextRange;
}

//--------------------------------------------------------------------------------------------------

- (NSRange) selectedRange {
	const NSRange posRangeSel = [mOwner selectedRangePositions];
	if (posRangeSel.length == 0) {
		NSTextInputContext *tic = [NSTextInputContext currentInputContext];
		// Chinese input causes malloc crash when empty selection returned with actual
		// position so return NSNotFound.
		// If this is applied to European input, it stops the accented character
		// chooser from appearing.
		// May need to add more input source names.
		if ([tic.selectedKeyboardInputSource
				isEqualToString: @"com.apple.inputmethod.TCIM.Cangjie"]) {
			return NSMakeRange(NSNotFound, 0);
		}
	}
	return mOwner.backend->CharactersFromPositions(posRangeSel);
}

//--------------------------------------------------------------------------------------------------

/**
 * Called by the input manager to set text which might be combined with further input to form
 * the final text (e.g. composition of ^ and a to â).
 *
 * @param aString The text to insert, either what has been marked already or what is selected already
 *                or simply added at the current insertion point. Depending on what is available.
 * @param range The range of the new text to select (given relative to the insertion point of the new text).
 * @param replacementRange The range to remove before insertion.
 */
- (void) setMarkedText: (id) aString selectedRange: (NSRange) range replacementRange: (NSRange) replacementRange {
	NSString *newText = @"";
	if ([aString isKindOfClass: [NSString class]])
		newText = (NSString *) aString;
	else if ([aString isKindOfClass: [NSAttributedString class]])
		newText = (NSString *) [aString string];

	// Replace marked text if there is one.
	if (mMarkedTextRange.length > 0) {
		mOwner.backend->CompositionUndo();
		if (replacementRange.location != NSNotFound) {
			// This situation makes no sense and has not occurred in practice.
			NSLog(@"Can not handle a replacement range when there is also a marked range");
		} else {
			replacementRange = mMarkedTextRange;
			const NSRange posRangeMark = mOwner.backend->PositionsFromCharacters(mMarkedTextRange);
			[mOwner message: SCI_SETEMPTYSELECTION wParam: posRangeMark.location];
		}
	} else {
		// Must perform deletion before entering composition mode or else
		// both document and undo history will not contain the deleted text
		// leading to an inaccurate and unusable undo history.

		// Convert selection virtual space into real space
		mOwner.backend->ConvertSelectionVirtualSpace();

		if (replacementRange.location != NSNotFound) {
			const NSRange posRangeReplacement = mOwner.backend->PositionsFromCharacters(replacementRange);
			[mOwner message: SCI_DELETERANGE
				 wParam: posRangeReplacement.location
				 lParam: posRangeReplacement.length];
		} else { // No marked or replacement range, so replace selection
			if (!mOwner.backend->ScintillaCocoa::ClearAllSelections()) {
				// Some of the selection is protected so can not perform composition here
				return;
			}
			// Ensure only a single selection.
			mOwner.backend->SelectOnlyMainSelection();
			const NSRange posRangeSel = [mOwner selectedRangePositions];
			replacementRange = mOwner.backend->CharactersFromPositions(posRangeSel);
		}
	}

	// To support IME input to multiple selections, the following code would
	// need to insert newText at each selection, mark each piece of new text and then
	// select range relative to each insertion.

	if (newText.length) {
		// Switching into composition.
		mOwner.backend->CompositionStart();

		NSRange posRangeCurrent = mOwner.backend->PositionsFromCharacters(NSMakeRange(replacementRange.location, 0));
		// Note: Scintilla internally works almost always with bytes instead chars, so we need to take
		//       this into account when determining selection ranges and such.
		ptrdiff_t lengthInserted = mOwner.backend->InsertText(newText, CharacterSource::TentativeInput);
		posRangeCurrent.length = lengthInserted;
		mMarkedTextRange = mOwner.backend->CharactersFromPositions(posRangeCurrent);
		// Mark the just inserted text. Keep the marked range for later reset.
		[mOwner setGeneralProperty: SCI_SETINDICATORCURRENT value: INDICATOR_IME];
		[mOwner setGeneralProperty: SCI_INDICATORFILLRANGE
				 parameter: posRangeCurrent.location
				     value: posRangeCurrent.length];
	} else {
		mMarkedTextRange = NSMakeRange(NSNotFound, 0);
		// Re-enable undo action collection if composition ended (indicated by an empty mark string).
		mOwner.backend->CompositionCommit();
	}

	// Select the part which is indicated in the given range. It does not scroll the caret into view.
	if (range.length > 0) {
		// range is in characters so convert to bytes for selection.
		range.location += replacementRange.location;
		NSRange posRangeSelect = mOwner.backend->PositionsFromCharacters(range);
		[mOwner setGeneralProperty: SCI_SETSELECTION parameter: NSMaxRange(posRangeSelect) value: posRangeSelect.location];
	}
}

//--------------------------------------------------------------------------------------------------

- (void) unmarkText {
	if (mMarkedTextRange.length > 0) {
		mOwner.backend->CompositionCommit();
		mMarkedTextRange = NSMakeRange(NSNotFound, 0);
	}
}

//--------------------------------------------------------------------------------------------------

- (NSArray *) validAttributesForMarkedText {
	return @[];
}

// End of the NSTextInputClient protocol adoption.

//--------------------------------------------------------------------------------------------------

/**
 * Generic input method. It is used to pass on keyboard input to Scintilla. The control itself only
 * handles shortcuts. The input is then forwarded to the Cocoa text input system, which in turn does
 * its own input handling (character composition via NSTextInputClient protocol):
 */
- (void) keyDown: (NSEvent *) theEvent {
	bool handled = false;
	if (mMarkedTextRange.length == 0)
		handled = mOwner.backend->KeyboardInput(theEvent);
	if (!handled) {
		NSArray *events = @[theEvent];
		[self interpretKeyEvents: events];
	}
}

//--------------------------------------------------------------------------------------------------

- (void) mouseDown: (NSEvent *) theEvent {
	mOwner.backend->MouseDown(theEvent);
}

//--------------------------------------------------------------------------------------------------

- (void) mouseDragged: (NSEvent *) theEvent {
	mOwner.backend->MouseMove(theEvent);
}

//--------------------------------------------------------------------------------------------------

- (void) mouseUp: (NSEvent *) theEvent {
	mOwner.backend->MouseUp(theEvent);
}

//--------------------------------------------------------------------------------------------------

- (void) mouseMoved: (NSEvent *) theEvent {
	mOwner.backend->MouseMove(theEvent);
}

//--------------------------------------------------------------------------------------------------

- (void) mouseEntered: (NSEvent *) theEvent {
	mOwner.backend->MouseEntered(theEvent);
}

//--------------------------------------------------------------------------------------------------

- (void) mouseExited: (NSEvent *) theEvent {
	mOwner.backend->MouseExited(theEvent);
}

//--------------------------------------------------------------------------------------------------

/**
 * Implementing scrollWheel makes scrolling work better even if just
 * calling super.
 * Mouse wheel with command key may magnify text if enabled.
 * Pinch gestures and key commands can also be used for magnification.
 */
- (void) scrollWheel: (NSEvent *) theEvent {
#ifdef SCROLL_WHEEL_MAGNIFICATION
	if (([theEvent modifierFlags] & NSEventModifierFlagCommand) != 0) {
		mOwner.backend->MouseWheel(theEvent);
		return;
	}
#endif
	[super scrollWheel: theEvent];
}

//--------------------------------------------------------------------------------------------------

/**
 * Ensure scrolling is aligned to whole lines instead of starting part-way through a line
 */
- (NSRect) adjustScroll: (NSRect) proposedVisibleRect {
	if (!mOwner)
		return proposedVisibleRect;
	NSRect rc = proposedVisibleRect;
	// Snap to lines
	NSRect contentRect = self.bounds;
	if ((rc.origin.y > 0) && (NSMaxY(rc) < contentRect.size.height)) {
		// Only snap for positions inside the document - allow outside
		// for overshoot.
		long lineHeight = mOwner.backend->WndProc(Message::TextHeight, 0, 0);
		rc.origin.y = std::round(rc.origin.y / lineHeight) * lineHeight;
	}
	// Snap to whole points - on retina displays this avoids visual debris
	// when scrolling horizontally.
	if ((rc.origin.x > 0) && (NSMaxX(rc) < contentRect.size.width)) {
		// Only snap for positions inside the document - allow outside
		// for overshoot.
		rc.origin.x = std::round(rc.origin.x);
	}
	return rc;
}

//--------------------------------------------------------------------------------------------------

/**
 * The editor is getting the foreground control (the one getting the input focus).
 */
- (BOOL) becomeFirstResponder {
	mOwner.backend->SetFirstResponder(true);
	return YES;
}

//--------------------------------------------------------------------------------------------------

/**
 * The editor is losing the input focus.
 */
- (BOOL) resignFirstResponder {
	mOwner.backend->SetFirstResponder(false);
	return YES;
}

//--------------------------------------------------------------------------------------------------

/**
 * Implement NSDraggingSource.
 */

- (NSDragOperation) draggingSession: (NSDraggingSession *) session
	sourceOperationMaskForDraggingContext: (NSDraggingContext) context {
#pragma unused(session)
	switch (context) {
	case NSDraggingContextOutsideApplication:
		return NSDragOperationCopy | NSDragOperationMove | NSDragOperationDelete;

	case NSDraggingContextWithinApplication:
	default:
		return NSDragOperationCopy | NSDragOperationMove | NSDragOperationDelete;
	}
}

- (void) draggingSession: (NSDraggingSession *) session
	    movedToPoint: (NSPoint) screenPoint {
#pragma unused(session, screenPoint)
}

- (void) draggingSession: (NSDraggingSession *) session
	    endedAtPoint: (NSPoint) screenPoint
	       operation: (NSDragOperation) operation {
#pragma unused(session, screenPoint)
	if (operation == NSDragOperationDelete) {
		mOwner.backend->WndProc(Message::Clear, 0, 0);
	}
}

/**
 * Implement NSDraggingDestination.
 */

//--------------------------------------------------------------------------------------------------

/**
 * Called when an external drag operation enters the view.
 */
- (NSDragOperation) draggingEntered: (id <NSDraggingInfo>) sender {
	return mOwner.backend->DraggingEntered(sender);
}

//--------------------------------------------------------------------------------------------------

/**
 * Called frequently during an external drag operation if we are the target.
 */
- (NSDragOperation) draggingUpdated: (id <NSDraggingInfo>) sender {
	return mOwner.backend->DraggingUpdated(sender);
}

//--------------------------------------------------------------------------------------------------

/**
 * Drag image left the view. Clean up if necessary.
 */
- (void) draggingExited: (id <NSDraggingInfo>) sender {
	mOwner.backend->DraggingExited(sender);
}

//--------------------------------------------------------------------------------------------------

- (BOOL) prepareForDragOperation: (id <NSDraggingInfo>) sender {
#pragma unused(sender)
	return YES;
}

//--------------------------------------------------------------------------------------------------

- (BOOL) performDragOperation: (id <NSDraggingInfo>) sender {
	return mOwner.backend->PerformDragOperation(sender);
}

//--------------------------------------------------------------------------------------------------

/**
 * Drag operation is done. Notify editor.
 */
- (void) concludeDragOperation: (id <NSDraggingInfo>) sender {
	// Clean up is the same as if we are no longer the drag target.
	mOwner.backend->DraggingExited(sender);
}

//--------------------------------------------------------------------------------------------------

// NSResponder actions.

- (void) selectAll: (id) sender {
#pragma unused(sender)
	mOwner.backend->SelectAll();
}

- (void) deleteBackward: (id) sender {
#pragma unused(sender)
	mOwner.backend->DeleteBackward();
}

- (void) cut: (id) sender {
#pragma unused(sender)
	mOwner.backend->Cut();
}

- (void) copy: (id) sender {
#pragma unused(sender)
	mOwner.backend->Copy();
}

- (void) paste: (id) sender {
#pragma unused(sender)
	if (mMarkedTextRange.location != NSNotFound) {
		[[NSTextInputContext currentInputContext] discardMarkedText];
		mOwner.backend->CompositionCommit();
		mMarkedTextRange = NSMakeRange(NSNotFound, 0);
	}
	mOwner.backend->Paste();
}

- (void) undo: (id) sender {
#pragma unused(sender)
	if (mMarkedTextRange.location != NSNotFound) {
		[[NSTextInputContext currentInputContext] discardMarkedText];
		mOwner.backend->CompositionCommit();
		mMarkedTextRange = NSMakeRange(NSNotFound, 0);
	}
	mOwner.backend->Undo();
}

- (void) redo: (id) sender {
#pragma unused(sender)
	mOwner.backend->Redo();
}

- (BOOL) canUndo {
	return mOwner.backend->CanUndo() && (mMarkedTextRange.location == NSNotFound);
}

- (BOOL) canRedo {
	return mOwner.backend->CanRedo();
}

- (BOOL) validateUserInterfaceItem: (id <NSValidatedUserInterfaceItem>) anItem {
	SEL action = anItem.action;
	if (action==@selector(undo:)) {
		return [self canUndo];
	} else if (action==@selector(redo:)) {
		return [self canRedo];
	} else if (action==@selector(cut:) || action==@selector(copy:) || action==@selector(clear:)) {
		return mOwner.backend->HasSelection();
	} else if (action==@selector(paste:)) {
		return mOwner.backend->CanPaste();
	}
	return YES;
}

- (void) clear: (id) sender {
	[self deleteBackward: sender];
}

- (BOOL) isEditable {
	return mOwner.backend->WndProc(Message::GetReadOnly, 0, 0) == 0;
}

#pragma mark - NSAccessibility

//--------------------------------------------------------------------------------------------------

// Adoption of NSAccessibility protocol.
// NSAccessibility wants to pass ranges in UTF-16 code units, not bytes (like Scintilla)
// or characters.
// Needs more testing with non-ASCII and non-BMP text.
// Needs to take account of folding and wraping.

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Text of the whole document as a string.
 */
- (id) accessibilityValue {
	const sptr_t length = [mOwner message: SCI_GETLENGTH];
	return mOwner.backend->RangeTextAsString(NSMakeRange(0, length));
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Line of the caret.
 */
- (NSInteger) accessibilityInsertionPointLineNumber {
	const Sci::Position caret = [mOwner message: SCI_GETCURRENTPOS];
	const NSRange rangeCharactersCaret = mOwner.backend->CharactersFromPositions(NSMakeRange(caret, 0));
	return mOwner.backend->VisibleLineForIndex(rangeCharactersCaret.location);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Not implemented and not called by VoiceOver.
 */
- (NSRange) accessibilityRangeForPosition: (NSPoint) point {
	return NSMakeRange(0, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Number of characters in the whole document.
 */
- (NSInteger) accessibilityNumberOfCharacters {
	sptr_t length = [mOwner message: SCI_GETLENGTH];
	const NSRange posRange = mOwner.backend->CharactersFromPositions(NSMakeRange(length, 0));
	return posRange.location;
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : The selection text as a string.
 */
- (NSString *) accessibilitySelectedText {
	const sptr_t positionBegin = [mOwner message: SCI_GETSELECTIONSTART];
	const sptr_t positionEnd = [mOwner message: SCI_GETSELECTIONEND];
	const NSRange posRangeSel = NSMakeRange(positionBegin, positionEnd-positionBegin);
	return mOwner.backend->RangeTextAsString(posRangeSel);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : The character range of the main selection.
 */
- (NSRange) accessibilitySelectedTextRange {
	const sptr_t positionBegin = [mOwner message: SCI_GETSELECTIONSTART];
	const sptr_t positionEnd = [mOwner message: SCI_GETSELECTIONEND];
	const NSRange posRangeSel = NSMakeRange(positionBegin, positionEnd-positionBegin);
	return mOwner.backend->CharactersFromPositions(posRangeSel);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : The setter for accessibilitySelectedTextRange.
 * This method is the only setter required for reasonable VoiceOver behaviour.
 */
- (void) setAccessibilitySelectedTextRange: (NSRange) range {
	NSRange rangePositions = mOwner.backend->PositionsFromCharacters(range);
	[mOwner message: SCI_SETSELECTION wParam: rangePositions.location lParam: NSMaxRange(rangePositions)];
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Range of the glyph at a character index.
 * Currently doesn't try to handle composite characters.
 */
- (NSRange) accessibilityRangeForIndex: (NSInteger) index {
	sptr_t length = [mOwner message: SCI_GETLENGTH];
	const NSRange rangeLength = mOwner.backend->CharactersFromPositions(NSMakeRange(length, 0));
	NSRange rangePositions = NSMakeRange(length, 0);
	if (index < rangeLength.location) {
		rangePositions = mOwner.backend->PositionsFromCharacters(NSMakeRange(index, 1));
	}
	return mOwner.backend->CharactersFromPositions(rangePositions);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : All the text ranges.
 * Currently only returns the main selection.
 */
- (NSArray<NSValue *> *) accessibilitySelectedTextRanges {
	const sptr_t positionBegin = [mOwner message: SCI_GETSELECTIONSTART];
	const sptr_t positionEnd = [mOwner message: SCI_GETSELECTIONEND];
	const NSRange posRangeSel = NSMakeRange(positionBegin, positionEnd-positionBegin);
	NSRange rangeCharacters = mOwner.backend->CharactersFromPositions(posRangeSel);
	NSValue *valueRange = [NSValue valueWithRange: (NSRange)rangeCharacters];
	return @[valueRange];
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Character range currently visible.
 */
- (NSRange) accessibilityVisibleCharacterRange {
	const sptr_t lineTopVisible = [mOwner message: SCI_GETFIRSTVISIBLELINE];
	const sptr_t lineTop = [mOwner message: SCI_DOCLINEFROMVISIBLE wParam: lineTopVisible];
	const sptr_t lineEndVisible = lineTopVisible + [mOwner message: SCI_LINESONSCREEN] - 1;
	const sptr_t lineEnd = [mOwner message: SCI_DOCLINEFROMVISIBLE wParam: lineEndVisible];
	const sptr_t posStartView = [mOwner message: SCI_POSITIONFROMLINE wParam: lineTop];
	const sptr_t posEndView = [mOwner message: SCI_GETLINEENDPOSITION wParam: lineEnd];
	const NSRange posRangeSel = NSMakeRange(posStartView, posEndView-posStartView);
	return mOwner.backend->CharactersFromPositions(posRangeSel);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Character range of a line.
 */
- (NSRange) accessibilityRangeForLine: (NSInteger) line {
	return mOwner.backend->RangeForVisibleLine(line);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Line number of a text position in characters.
 */
- (NSInteger) accessibilityLineForIndex: (NSInteger) index {
	return mOwner.backend->VisibleLineForIndex(index);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : A rectangle that covers a range which will be shown as the
 * VoiceOver cursor.
 * Producing a nice rectangle is a little tricky particularly when including new
 * lines. Needs to improve the case where parts of two lines are included.
 */
- (NSRect) accessibilityFrameForRange: (NSRange) range {
	const NSRect rectInView = mOwner.backend->FrameForRange(range);
	const NSRect rectInWindow = [self.superview.superview convertRect: rectInView toView: nil];
	return [self.window convertRectToScreen: rectInWindow];
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : A range of text as a string.
 */
- (NSString *) accessibilityStringForRange: (NSRange) range {
	const NSRange posRange = mOwner.backend->PositionsFromCharacters(range);
	return mOwner.backend->RangeTextAsString(posRange);
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : A range of text as an attributed string.
 * Currently no attributes are set.
 */
- (NSAttributedString *) accessibilityAttributedStringForRange: (NSRange) range {
	const NSRange posRange = mOwner.backend->PositionsFromCharacters(range);
	NSString *result = mOwner.backend->RangeTextAsString(posRange);
	return [[NSMutableAttributedString alloc] initWithString: result];
}

//--------------------------------------------------------------------------------------------------

/**
 * NSAccessibility : Show the context menu at the caret.
 */
- (BOOL) accessibilityPerformShowMenu {
	const sptr_t caret = [mOwner message: SCI_GETCURRENTPOS];
	NSRect rect;
	rect.origin.x = [mOwner message: SCI_POINTXFROMPOSITION wParam: 0 lParam: caret];
	rect.origin.y = [mOwner message: SCI_POINTYFROMPOSITION wParam: 0 lParam: caret];
	rect.origin.y += [mOwner message: SCI_TEXTHEIGHT wParam: 0 lParam: 0];
	rect.size.width = 1.0;
	rect.size.height = 1.0;
	NSRect rectInWindow = [self.superview.superview convertRect: rect toView: nil];
	NSPoint pt = rectInWindow.origin;
	NSEvent *event = [NSEvent mouseEventWithType: NSEventTypeRightMouseDown
					    location: pt
				       modifierFlags: 0
					   timestamp: 0
					windowNumber: self.window.windowNumber
					     context: nil
					 eventNumber: 0
					  clickCount: 1
					    pressure: 0.0];
	NSMenu *menu = mOwner.backend->CreateContextMenu(event);
	[NSMenu popUpContextMenu: menu withEvent: event forView: self];
	return YES;
}

//--------------------------------------------------------------------------------------------------


@end

//--------------------------------------------------------------------------------------------------

@implementation ScintillaView {
	// The back end is kind of a controller and model in one.
	// It uses the content view for display.
	Scintilla::Internal::ScintillaCocoa *mBackend;

	// This is the actual content to which the backend renders itself.
	SCIContentView *mContent;

	NSScrollView *scrollView;
	SCIMarginView *marginView;

	CGFloat zoomDelta;

	// Area to display additional controls (e.g. zoom info, caret position, status info).
	NSView <InfoBarCommunicator> *mInfoBar;
	BOOL mInfoBarAtTop;

	id<ScintillaNotificationProtocol> __unsafe_unretained mDelegate;
}

@synthesize backend = mBackend;
@synthesize delegate = mDelegate;
@synthesize scrollView;

/**
 * ScintillaView is a composite control made from an NSView and an embedded NSView that is
 * used as canvas for the output (by the backend, using its CGContext), plus other elements
 * (scrollers, info bar).
 */

//--------------------------------------------------------------------------------------------------

/**
 * Initialize custom cursor.
 */
+ (void) initialize {
	if (self == [ScintillaView class]) {
		NSBundle *bundle = [NSBundle bundleForClass: [ScintillaView class]];

		NSString *path = [bundle pathForResource: @"mac_cursor_busy" ofType: @"tiff" inDirectory: nil];
		NSImage *image = [[NSImage alloc] initWithContentsOfFile: path];
		if (image) {
			waitCursor = [[NSCursor alloc] initWithImage: image hotSpot: NSMakePoint(2, 2)];
		} else {
			NSLog(@"Wait cursor is invalid.");
			waitCursor = [NSCursor arrowCursor];
		}

		path = [bundle pathForResource: @"mac_cursor_flipped" ofType: @"tiff" inDirectory: nil];
		image = [[NSImage alloc] initWithContentsOfFile: path];
		if (image) {
			reverseArrowCursor = [[NSCursor alloc] initWithImage: image hotSpot: NSMakePoint(15, 2)];
		} else {
			NSLog(@"Reverse arrow cursor is invalid.");
			reverseArrowCursor = [NSCursor arrowCursor];
		}
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Specify the SCIContentView class. Can be overridden in a subclass to provide an SCIContentView subclass.
 */

+ (Class) contentViewClass {
	return [SCIContentView class];
}

//--------------------------------------------------------------------------------------------------

/**
 * Receives zoom messages, for example when a "pinch zoom" is performed on the trackpad.
 */
- (void) magnifyWithEvent: (NSEvent *) event {
#if MAC_OS_X_VERSION_MAX_ALLOWED > MAC_OS_X_VERSION_10_5
	zoomDelta += event.magnification * 10.0;

	if (std::abs(zoomDelta)>=1.0) {
		long zoomFactor = static_cast<long>([self getGeneralProperty: SCI_GETZOOM] + zoomDelta);
		[self setGeneralProperty: SCI_SETZOOM parameter: zoomFactor value: 0];
		zoomDelta = 0.0;
	}
#endif
}

- (void) beginGestureWithEvent: (NSEvent *) event {
// Scintilla is only interested in this event as the starft of a zoom
#pragma unused(event)
	zoomDelta = 0.0;
}

//--------------------------------------------------------------------------------------------------

/**
 * Sends a new notification of the given type to the default notification center.
 */
- (void) sendNotification: (NSString *) notificationName {
	NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
	[center postNotificationName: notificationName object: self];
}

//--------------------------------------------------------------------------------------------------

/**
 * Called by a connected component (usually the info bar) if something changed there.
 *
 * @param type The type of the notification.
 * @param message Carries the new status message if the type is a status message change.
 * @param location Carries the new location (e.g. caret) if the type is a caret change or similar type.
 * @param value Carries the new zoom value if the type is a zoom change.
 */
- (void) notify: (NotificationType) type message: (NSString *) message location: (NSPoint) location
	  value: (float) value {
// These parameters are just to conform to the protocol
#pragma unused(message)
#pragma unused(location)
	switch (type) {
	case IBNZoomChanged: {
			// Compute point increase/decrease based on default font size.
			long fontSize = [self getGeneralProperty: SCI_STYLEGETSIZE parameter: STYLE_DEFAULT];
			int zoom = (int)(fontSize * (value - 1));
			[self setGeneralProperty: SCI_SETZOOM value: zoom];
			break;
		}
	default:
		break;
	};
}

//--------------------------------------------------------------------------------------------------

- (void) setCallback: (id <InfoBarCommunicator>) callback {
// Not used. Only here to satisfy protocol.
#pragma unused(callback)
}

//--------------------------------------------------------------------------------------------------

/**
 * Prevents drawing of the inner view to avoid flickering when doing many visual updates
 * (like clearing all marks and setting new ones etc.).
 */
- (void) suspendDrawing: (BOOL) suspend {
	if (@available(macOS 10.14, *)) {
		// Don't try where deprecated
	} else {
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
		if (suspend)
			[self.window disableFlushWindow];
		else
			[self.window enableFlushWindow];
#pragma GCC diagnostic pop
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Method receives notifications from Scintilla (e.g. for handling clicks on the
 * folder margin or changes in the editor).
 * A delegate can be set to receive all notifications. If set no handling takes place here, except
 * for action pertaining to internal stuff (like the info bar).
 */
- (void) notification: (SCNotification *) scn {
	// Parent notification. Details are passed as SCNotification structure.

	if (mDelegate != nil) {
		[mDelegate notification: scn];
		if (scn->nmhdr.code != static_cast<unsigned int>(Notification::Zoom) &&
		    scn->nmhdr.code != static_cast<unsigned int>(Notification::UpdateUI))
			return;
	}

	switch (static_cast<Notification>(scn->nmhdr.code)) {
	case Notification::MarginClick: {
			if (scn->margin == 2) {
				// Click on the folder margin. Toggle the current line if possible.
				long line = [self getGeneralProperty: SCI_LINEFROMPOSITION parameter: scn->position];
				[self setGeneralProperty: SCI_TOGGLEFOLD value: line];
			}
			break;
		};
	case Notification::Modified: {
			// Decide depending on the modification type what to do.
			// There can be more than one modification carried by one notification.
			if (scn->modificationType &
				static_cast<int>((ModificationFlags::InsertText | ModificationFlags::DeleteText)))
				[self sendNotification: NSTextDidChangeNotification];
			break;
		}
	case Notification::Zoom: {
			// A zoom change happened. Notify info bar if there is one.
			float zoom = [self getGeneralProperty: SCI_GETZOOM parameter: 0];
			long fontSize = [self getGeneralProperty: SCI_STYLEGETSIZE parameter: STYLE_DEFAULT];
			float factor = (zoom / fontSize) + 1;
			[mInfoBar notify: IBNZoomChanged message: nil location: NSZeroPoint value: factor];
			break;
		}
	case Notification::UpdateUI: {
			// Triggered whenever changes in the UI state need to be reflected.
			// These can be: caret changes, selection changes etc.
			NSPoint caretPosition = mBackend->GetCaretPosition();
			[mInfoBar notify: IBNCaretChanged message: nil location: caretPosition value: 0];
			[self sendNotification: SCIUpdateUINotification];
			if (scn->updated & static_cast<int>((Update::Selection | Update::Content))) {
				[self sendNotification: NSTextViewDidChangeSelectionNotification];
			}
			break;
		}
	case Notification::FocusOut:
		[self sendNotification: NSTextDidEndEditingNotification];
		break;
	case Notification::FocusIn: // Nothing to do for now.
		break;
	default:
		break;
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Setup a special indicator used in the editor to provide visual feedback for
 * input composition, depending on language, keyboard etc.
 */
- (void) updateIndicatorIME {
	[self setColorProperty: SCI_INDICSETFORE parameter: INDICATOR_IME fromHTML: @"#FF0000"];
	const bool drawInBackground = [self message: SCI_GETPHASESDRAW] != 0;
	[self setGeneralProperty: SCI_INDICSETUNDER parameter: INDICATOR_IME value: drawInBackground];
	[self setGeneralProperty: SCI_INDICSETSTYLE parameter: INDICATOR_IME value: INDIC_PLAIN];
	[self setGeneralProperty: SCI_INDICSETALPHA parameter: INDICATOR_IME value: 100];
}

//--------------------------------------------------------------------------------------------------

/**
 * Initialization of the view. Used to setup a few other things we need.
 */
- (instancetype) initWithFrame: (NSRect) frame {
	self = [super initWithFrame: frame];
	if (self) {
		mContent = [[[[self class] contentViewClass] alloc] initWithFrame: NSZeroRect];
		mContent.owner = self;

		// Initialize the scrollers but don't show them yet.
		// Pick an arbitrary size, just to make NSScroller selecting the proper scroller direction
		// (horizontal or vertical).
		NSRect scrollerRect = NSMakeRect(0, 0, 100, 10);
		scrollView = (NSScrollView *)[[SCIScrollView alloc] initWithFrame: scrollerRect];
#if defined(MAC_OS_X_VERSION_10_14)
		// Let SCIScrollView account for other subviews such as vertical ruler by turning off
		// automaticallyAdjustsContentInsets.
		if (std::floor(NSAppKitVersionNumber) > NSAppKitVersionNumber10_13) {
			scrollView.contentView.automaticallyAdjustsContentInsets = NO;
			scrollView.contentView.contentInsets = NSEdgeInsetsMake(0., 0., 0., 0.);
		}
#endif
		scrollView.documentView = mContent;
		[scrollView setHasVerticalScroller: YES];
		[scrollView setHasHorizontalScroller: YES];
		scrollView.autoresizingMask = NSViewWidthSizable|NSViewHeightSizable;
		//[scrollView setScrollerStyle:NSScrollerStyleLegacy];
		//[scrollView setScrollerKnobStyle:NSScrollerKnobStyleDark];
		//[scrollView setHorizontalScrollElasticity:NSScrollElasticityNone];
		[self addSubview: scrollView];

		marginView = [[SCIMarginView alloc] initWithScrollView: scrollView];
		marginView.owner = self;
		marginView.ruleThickness = marginView.requiredThickness;
		scrollView.verticalRulerView = marginView;
		[scrollView setHasHorizontalRuler: NO];
		[scrollView setHasVerticalRuler: YES];
		[scrollView setRulersVisible: YES];

		mBackend = new ScintillaCocoa(self, mContent, marginView);

		// Establish a connection from the back end to this container so we can handle situations
		// which require our attention.
		mBackend->SetDelegate(self);

		[self updateIndicatorIME];

		NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
		[center addObserver: self
			   selector: @selector(applicationDidResignActive:)
			       name: NSApplicationDidResignActiveNotification
			     object: nil];

		[center addObserver: self
			   selector: @selector(applicationDidBecomeActive:)
			       name: NSApplicationDidBecomeActiveNotification
			     object: nil];

		[center addObserver: self
			   selector: @selector(windowWillMove:)
			       name: NSWindowWillMoveNotification
			     object: self.window];

		[center addObserver: self
			   selector: @selector(defaultsDidChange:)
			       name: NSSystemColorsDidChangeNotification
			     object: self.window];

		[scrollView.contentView setPostsBoundsChangedNotifications: YES];
		[center addObserver: self
			   selector: @selector(scrollerAction:)
			       name: NSViewBoundsDidChangeNotification
			     object: scrollView.contentView];

		mBackend->UpdateBaseElements();
	}
	return self;
}

//--------------------------------------------------------------------------------------------------

- (void) dealloc {
	[[NSNotificationCenter defaultCenter] removeObserver: self];
	mBackend->Finalise();
	delete mBackend;
	mBackend = NULL;
	mContent.owner = nil;
	[marginView setClientView: nil];
	[scrollView removeFromSuperview];
}

//--------------------------------------------------------------------------------------------------

- (void) applicationDidResignActive: (NSNotification *) note {
#pragma unused(note)
	mBackend->ActiveStateChanged(false);
}

//--------------------------------------------------------------------------------------------------

- (void) applicationDidBecomeActive: (NSNotification *) note {
#pragma unused(note)
	mBackend->ActiveStateChanged(true);
}

//--------------------------------------------------------------------------------------------------

- (void) windowWillMove: (NSNotification *) note {
#pragma unused(note)
	mBackend->WindowWillMove();
}

//--------------------------------------------------------------------------------------------------

- (void) defaultsDidChange: (NSNotification *) note {
#pragma unused(note)
	mBackend->UpdateBaseElements();
}

//--------------------------------------------------------------------------------------------------

- (void) viewDidMoveToWindow {
	[super viewDidMoveToWindow];

	[self positionSubViews];

	// Enable also mouse move events for our window (and so this view).
	[self.window setAcceptsMouseMovedEvents: YES];
}

//--------------------------------------------------------------------------------------------------

/**
 * Used to position and size the parts of the editor (content, scrollers, info bar).
 */
- (void) positionSubViews {
	CGFloat scrollerWidth = [NSScroller scrollerWidthForControlSize: NSControlSizeRegular
							  scrollerStyle: NSScrollerStyleLegacy];

	NSSize size = self.frame.size;
	NSRect barFrame = {{0, size.height - scrollerWidth}, {size.width, scrollerWidth}};
	BOOL infoBarVisible = mInfoBar != nil && !mInfoBar.hidden;

	// Horizontal offset of the content. Almost always 0 unless the vertical scroller
	// is on the left side.
	CGFloat contentX = 0;
	NSRect scrollRect = {{contentX, 0}, {size.width, size.height}};

	// Info bar frame.
	if (infoBarVisible) {
		scrollRect.size.height -= scrollerWidth;
		// Initial value already is as if the bar is at top.
		if (!mInfoBarAtTop) {
			scrollRect.origin.y += scrollerWidth;
			barFrame.origin.y = 0;
		}
	}

	if (!NSEqualRects(scrollView.frame, scrollRect)) {
		scrollView.frame = scrollRect;
	}

	if (infoBarVisible)
		mInfoBar.frame = barFrame;
}

//--------------------------------------------------------------------------------------------------

/**
 * Set the width of the margin.
 */
- (void) setMarginWidth: (int) width {
	if (marginView.ruleThickness != width) {
		marginView.marginWidth = width;
		marginView.ruleThickness = marginView.requiredThickness;
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Triggered by one of the scrollers when it gets manipulated by the user. Notify the backend
 * about the change.
 */
- (void) scrollerAction: (id) sender {
#pragma unused(sender)
	mBackend->UpdateForScroll();
}

//--------------------------------------------------------------------------------------------------

/**
 * Used to reposition our content depending on the size of the view.
 */
- (void) setFrame: (NSRect) newFrame {
	NSRect previousFrame = self.frame;
	super.frame = newFrame;
	[self positionSubViews];
	if (!NSEqualRects(previousFrame, newFrame)) {
		mBackend->Resize();
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Getter for the currently selected text in raw form (no formatting information included).
 * If there is no text available an empty string is returned.
 */
- (NSString *) selectedString {
	NSString *result = @"";

	const long length = mBackend->WndProc(Message::GetSelText, 0, 0);
	if (length > 0) {
		std::string buffer(length + 1, '\0');
		try {
			mBackend->WndProc(Message::GetSelText, length + 1, (sptr_t) &buffer[0]);

			result = @(buffer.c_str());
		} catch (...) {
		}
	}

	return result;
}

//--------------------------------------------------------------------------------------------------

/**
 * Delete a range from the document.
 */
- (void) deleteRange: (NSRange) aRange {
	if (aRange.length > 0) {
		NSRange posRange = mBackend->PositionsFromCharacters(aRange);
		[self message: SCI_DELETERANGE wParam: posRange.location lParam: posRange.length];
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Getter for the current text in raw form (no formatting information included).
 * If there is no text available an empty string is returned.
 */
- (NSString *) string {
	NSString *result = @"";

	const long length = mBackend->WndProc(Message::GetLength, 0, 0);
	if (length > 0) {
		std::string buffer(length + 1, '\0');
		try {
			mBackend->WndProc(Message::GetText, length + 1, (sptr_t) &buffer[0]);

			result = @(buffer.c_str());
		} catch (...) {
		}
	}

	return result;
}

//--------------------------------------------------------------------------------------------------

/**
 * Setter for the current text (no formatting included).
 */
- (void) setString: (NSString *) aString {
	const char *text = aString.UTF8String;
	mBackend->WndProc(Message::SetText, 0, (long) text);
}

//--------------------------------------------------------------------------------------------------

- (void) insertString: (NSString *) aString atOffset: (int) offset {
	const char *text = aString.UTF8String;
	mBackend->WndProc(Message::AddText, offset, (long) text);
}

//--------------------------------------------------------------------------------------------------

- (void) setEditable: (BOOL) editable {
	mBackend->WndProc(Message::SetReadOnly, editable ? 0 : 1, 0);
}

//--------------------------------------------------------------------------------------------------

- (BOOL) isEditable {
	return mBackend->WndProc(Message::GetReadOnly, 0, 0) == 0;
}

//--------------------------------------------------------------------------------------------------

- (SCIContentView *) content {
	return mContent;
}

//--------------------------------------------------------------------------------------------------

- (void) updateMarginCursors {
	[self.window invalidateCursorRectsForView: marginView];
}

//--------------------------------------------------------------------------------------------------

/**
 * Direct call into the backend to allow uninterpreted access to it. The values to be passed in and
 * the result heavily depend on the message that is used for the call. Refer to the Scintilla
 * documentation to learn what can be used here.
 */
+ (sptr_t) directCall: (ScintillaView *) sender message: (unsigned int) message wParam: (uptr_t) wParam
	       lParam: (sptr_t) lParam {
	return ScintillaCocoa::DirectFunction(
		       reinterpret_cast<sptr_t>(sender->mBackend), message, wParam, lParam);
}

- (sptr_t) message: (unsigned int) message wParam: (uptr_t) wParam lParam: (sptr_t) lParam {
	return mBackend->WndProc(static_cast<Message>(message), wParam, lParam);
}

- (sptr_t) message: (unsigned int) message wParam: (uptr_t) wParam {
	return mBackend->WndProc(static_cast<Message>(message), wParam, 0);
}

- (sptr_t) message: (unsigned int) message {
	return mBackend->WndProc(static_cast<Message>(message), 0, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * This is a helper method to set properties in the backend, with native parameters.
 *
 * @param property Main property like SCI_STYLESETFORE for which a value is to be set.
 * @param parameter Additional info for this property like a parameter or index.
 * @param value The actual value. It depends on the property what this parameter means.
 */
- (void) setGeneralProperty: (int) property parameter: (long) parameter value: (long) value {
	mBackend->WndProc(static_cast<Message>(property), parameter, value);
}

//--------------------------------------------------------------------------------------------------

/**
 * A simplified version for setting properties which only require one parameter.
 *
 * @param property Main property like SCI_STYLESETFORE for which a value is to be set.
 * @param value The actual value. It depends on the property what this parameter means.
 */
- (void) setGeneralProperty: (int) property value: (long) value {
	mBackend->WndProc(static_cast<Message>(property), value, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * This is a helper method to get a property in the backend, with native parameters.
 *
 * @param property Main property like SCI_STYLESETFORE for which a value is to get.
 * @param parameter Additional info for this property like a parameter or index.
 * @param extra Yet another parameter if needed.
 * @result A generic value which must be interpreted depending on the property queried.
 */
- (long) getGeneralProperty: (int) property parameter: (long) parameter extra: (long) extra {
	return mBackend->WndProc(static_cast<Message>(property), parameter, extra);
}

//--------------------------------------------------------------------------------------------------

/**
 * Convenience function to avoid unneeded extra parameter.
 */
- (long) getGeneralProperty: (int) property parameter: (long) parameter {
	return mBackend->WndProc(static_cast<Message>(property), parameter, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * Convenience function to avoid unneeded parameters.
 */
- (long) getGeneralProperty: (int) property {
	return mBackend->WndProc(static_cast<Message>(property), 0, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * Use this variant if you have to pass in a reference to something (e.g. a text range).
 */
- (long) getGeneralProperty: (int) property ref: (const void *) ref {
	return mBackend->WndProc(static_cast<Message>(property), 0, (sptr_t) ref);
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property setter for colors.
 */
- (void) setColorProperty: (int) property parameter: (long) parameter value: (NSColor *) value {
	NSColor *deviceColor = [value colorUsingColorSpace: [NSColorSpace deviceRGBColorSpace]];
	long red = static_cast<long>(deviceColor.redComponent * 255);
	long green = static_cast<long>(deviceColor.greenComponent * 255);
	long blue = static_cast<long>(deviceColor.blueComponent * 255);

	long color = (blue << 16) + (green << 8) + red;
	mBackend->WndProc(static_cast<Message>(property), parameter, color);
}

//--------------------------------------------------------------------------------------------------

/**
 * Another color property setting, which allows to specify the color as string like in HTML
 * documents (i.e. with leading # and either 3 hex digits or 6).
 */
- (void) setColorProperty: (int) property parameter: (long) parameter fromHTML: (NSString *) fromHTML {
	if (fromHTML.length > 3 && [fromHTML characterAtIndex: 0] == '#') {
		bool longVersion = fromHTML.length > 6;
		int index = 1;

		char value[3] = {0, 0, 0};
		value[0] = static_cast<char>([fromHTML characterAtIndex: index++]);
		if (longVersion)
			value[1] = static_cast<char>([fromHTML characterAtIndex: index++]);
		else
			value[1] = value[0];

		unsigned rawRed;
		[[NSScanner scannerWithString: @(value)] scanHexInt: &rawRed];

		value[0] = static_cast<char>([fromHTML characterAtIndex: index++]);
		if (longVersion)
			value[1] = static_cast<char>([fromHTML characterAtIndex: index++]);
		else
			value[1] = value[0];

		unsigned rawGreen;
		[[NSScanner scannerWithString: @(value)] scanHexInt: &rawGreen];

		value[0] = static_cast<char>([fromHTML characterAtIndex: index++]);
		if (longVersion)
			value[1] = static_cast<char>([fromHTML characterAtIndex: index++]);
		else
			value[1] = value[0];

		unsigned rawBlue;
		[[NSScanner scannerWithString: @(value)] scanHexInt: &rawBlue];

		long color = (rawBlue << 16) + (rawGreen << 8) + rawRed;
		mBackend->WndProc(static_cast<Message>(property), parameter, color);
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property getter for colors.
 */
- (NSColor *) getColorProperty: (int) property parameter: (long) parameter {
	long color = mBackend->WndProc(static_cast<Message>(property), parameter, 0);
	CGFloat red = (color & 0xFF) / 255.0;
	CGFloat green = ((color >> 8) & 0xFF) / 255.0;
	CGFloat blue = ((color >> 16) & 0xFF) / 255.0;
	NSColor *result = [NSColor colorWithDeviceRed: red green: green blue: blue alpha: 1];
	return result;
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property setter for references (pointers, addresses).
 */
- (void) setReferenceProperty: (int) property parameter: (long) parameter value: (const void *) value {
	mBackend->WndProc(static_cast<Message>(property), parameter, (sptr_t) value);
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property getter for references (pointers, addresses).
 */
- (const void *) getReferenceProperty: (int) property parameter: (long) parameter {
	return (const void *) mBackend->WndProc(static_cast<Message>(property), parameter, 0);
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property setter for string values.
 */
- (void) setStringProperty: (int) property parameter: (long) parameter value: (NSString *) value {
	const char *rawValue = value.UTF8String;
	mBackend->WndProc(static_cast<Message>(property), parameter, (sptr_t) rawValue);
}


//--------------------------------------------------------------------------------------------------

/**
 * Specialized property getter for string values.
 */
- (NSString *) getStringProperty: (int) property parameter: (long) parameter {
	const char *rawValue = (const char *) mBackend->WndProc(static_cast<Message>(property), parameter, 0);
	return @(rawValue);
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property setter for lexer properties, which are commonly passed as strings.
 */
- (void) setLexerProperty: (NSString *) name value: (NSString *) value {
	const char *rawName = name.UTF8String;
	const char *rawValue = value.UTF8String;
	mBackend->WndProc(Message::SetProperty, (sptr_t) rawName, (sptr_t) rawValue);
}

//--------------------------------------------------------------------------------------------------

/**
 * Specialized property getter for references (pointers, addresses).
 */
- (NSString *) getLexerProperty: (NSString *) name {
	const char *rawName = name.UTF8String;
	const char *result = (const char *) mBackend->WndProc(Message::SetProperty, (sptr_t) rawName, 0);
	return @(result);
}

//--------------------------------------------------------------------------------------------------

/**
 * Sets the notification callback
 */
- (void) registerNotifyCallback: (intptr_t) windowid value: (SciNotifyFunc) callback {
	mBackend->RegisterNotifyCallback(windowid, callback);
}


//--------------------------------------------------------------------------------------------------

/**
 * Sets the new control which is displayed as info bar at the top or bottom of the editor.
 * Set newBar to nil if you want to hide the bar again.
 * The info bar's height is set to the height of the scrollbar.
 */
- (void) setInfoBar: (NSView <InfoBarCommunicator> *) newBar top: (BOOL) top {
	if (mInfoBar != newBar) {
		[mInfoBar removeFromSuperview];

		mInfoBar = newBar;
		mInfoBarAtTop = top;
		if (mInfoBar != nil) {
			[self addSubview: mInfoBar];
			[mInfoBar setCallback: self];
		}

		[self positionSubViews];
	}
}

//--------------------------------------------------------------------------------------------------

/**
 * Sets the edit's info bar status message. This call only has an effect if there is an info bar.
 */
- (void) setStatusText: (NSString *) text {
	if (mInfoBar != nil)
		[mInfoBar notify: IBNStatusChanged message: text location: NSZeroPoint value: 0];
}

//--------------------------------------------------------------------------------------------------

- (NSRange) selectedRange {
	return [mContent selectedRange];
}

//--------------------------------------------------------------------------------------------------

/**
 * Return the main selection as an NSRange of positions (not characters).
 * Unlike selectedRange, this can return empty ranges inside the document.
 */

- (NSRange) selectedRangePositions {
	const sptr_t positionBegin = [self message: SCI_GETSELECTIONSTART];
	const sptr_t positionEnd = [self message: SCI_GETSELECTIONEND];
	return NSMakeRange(positionBegin, positionEnd-positionBegin);
}


//--------------------------------------------------------------------------------------------------

- (void) insertText: (id) aString {
	if ([aString isKindOfClass: [NSString class]])
		mBackend->InsertText(aString, CharacterSource::DirectInput);
	else if ([aString isKindOfClass: [NSAttributedString class]])
		mBackend->InsertText([aString string], CharacterSource::DirectInput);
}

//--------------------------------------------------------------------------------------------------

/**
 * For backwards compatibility.
 */
- (BOOL) findAndHighlightText: (NSString *) searchText
		    matchCase: (BOOL) matchCase
		    wholeWord: (BOOL) wholeWord
		     scrollTo: (BOOL) scrollTo
			 wrap: (BOOL) wrap {
	return [self findAndHighlightText: searchText
				matchCase: matchCase
				wholeWord: wholeWord
				 scrollTo: scrollTo
				     wrap: wrap
				backwards: NO];
}

//--------------------------------------------------------------------------------------------------

/**
 * Searches and marks the first occurrence of the given text and optionally scrolls it into view.
 *
 * @result YES if something was found, NO otherwise.
 */
- (BOOL) findAndHighlightText: (NSString *) searchText
		    matchCase: (BOOL) matchCase
		    wholeWord: (BOOL) wholeWord
		     scrollTo: (BOOL) scrollTo
			 wrap: (BOOL) wrap
		    backwards: (BOOL) backwards {
	FindOption searchFlags = FindOption::None;
	if (matchCase)
		searchFlags = searchFlags | FindOption::MatchCase;
	if (wholeWord)
		searchFlags = searchFlags | FindOption::WholeWord;

	long selectionStart = [self getGeneralProperty: SCI_GETSELECTIONSTART parameter: 0];
	long selectionEnd = [self getGeneralProperty: SCI_GETSELECTIONEND parameter: 0];

	// Sets the start point for the coming search to the beginning of the current selection.
	// For forward searches we have therefore to set the selection start to the current selection end
	// for proper incremental search. This does not harm as we either get a new selection if something
	// is found or the previous selection is restored.
	if (!backwards)
		[self getGeneralProperty: SCI_SETSELECTIONSTART parameter: selectionEnd];
	[self setGeneralProperty: SCI_SEARCHANCHOR value: 0];
	sptr_t result;
	const char *textToSearch = searchText.UTF8String;

	// The following call will also set the selection if something was found.
	if (backwards) {
		result = [ScintillaView directCall: self
					   message: SCI_SEARCHPREV
					    wParam: (uptr_t) searchFlags
					    lParam: (sptr_t) textToSearch];
		if (result < 0 && wrap) {
			// Try again from the end of the document if nothing could be found so far and
			// wrapped search is set.
			[self getGeneralProperty: SCI_SETSELECTIONSTART parameter: [self getGeneralProperty: SCI_GETTEXTLENGTH parameter: 0]];
			[self setGeneralProperty: SCI_SEARCHANCHOR value: 0];
			result = [ScintillaView directCall: self
						   message: SCI_SEARCHNEXT
						    wParam: (uptr_t) searchFlags
						    lParam: (sptr_t) textToSearch];
		}
	} else {
		result = [ScintillaView directCall: self
					   message: SCI_SEARCHNEXT
					    wParam: (uptr_t) searchFlags
					    lParam: (sptr_t) textToSearch];
		if (result < 0 && wrap) {
			// Try again from the start of the document if nothing could be found so far and
			// wrapped search is set.
			[self getGeneralProperty: SCI_SETSELECTIONSTART parameter: 0];
			[self setGeneralProperty: SCI_SEARCHANCHOR value: 0];
			result = [ScintillaView directCall: self
						   message: SCI_SEARCHNEXT
						    wParam: (uptr_t) searchFlags
						    lParam: (sptr_t) textToSearch];
		}
	}

	if (result >= 0) {
		if (scrollTo)
			[self setGeneralProperty: SCI_SCROLLCARET value: 0];
	} else {
		// Restore the former selection if we did not find anything.
		[self setGeneralProperty: SCI_SETSELECTIONSTART value: selectionStart];
		[self setGeneralProperty: SCI_SETSELECTIONEND value: selectionEnd];
	}
	return (result >= 0) ? YES : NO;
}

//--------------------------------------------------------------------------------------------------

/**
 * Searches the given text and replaces
 *
 * @result Number of entries replaced, 0 if none.
 */
- (int) findAndReplaceText: (NSString *) searchText
		    byText: (NSString *) newText
		 matchCase: (BOOL) matchCase
		 wholeWord: (BOOL) wholeWord
		     doAll: (BOOL) doAll {
	// The current position is where we start searching for single occurrences. Otherwise we start at
	// the beginning of the document.
	long startPosition;
	if (doAll)
		startPosition = 0; // Start at the beginning of the text if we replace all occurrences.
	else
		// For a single replacement we start at the current caret position.
		startPosition = [self getGeneralProperty: SCI_GETCURRENTPOS];
	long endPosition = [self getGeneralProperty: SCI_GETTEXTLENGTH];

	FindOption searchFlags = FindOption::None;
	if (matchCase)
		searchFlags = searchFlags | FindOption::MatchCase;
	if (wholeWord)
		searchFlags = searchFlags | FindOption::WholeWord;
	[self setGeneralProperty: SCI_SETSEARCHFLAGS value: (long)searchFlags];
	[self setGeneralProperty: SCI_SETTARGETSTART value: startPosition];
	[self setGeneralProperty: SCI_SETTARGETEND value: endPosition];

	const char *textToSearch = searchText.UTF8String;
	long sourceLength = strlen(textToSearch); // Length in bytes.
	const char *replacement = newText.UTF8String;
	long targetLength = strlen(replacement);  // Length in bytes.
	sptr_t result;

	int replaceCount = 0;
	if (doAll) {
		while (true) {
			result = [ScintillaView directCall: self
						   message: SCI_SEARCHINTARGET
						    wParam: sourceLength
						    lParam: (sptr_t) textToSearch];
			if (result < 0)
				break;

			replaceCount++;
			[ScintillaView directCall: self
					  message: SCI_REPLACETARGET
					   wParam: targetLength
					   lParam: (sptr_t) replacement];

			// The replacement changes the target range to the replaced text. Continue after that till the end.
			// The text length might be changed by the replacement so make sure the target end is the actual
			// text end.
			[self setGeneralProperty: SCI_SETTARGETSTART value: [self getGeneralProperty: SCI_GETTARGETEND]];
			[self setGeneralProperty: SCI_SETTARGETEND value: [self getGeneralProperty: SCI_GETTEXTLENGTH]];
		}
	} else {
		result = [ScintillaView directCall: self
					   message: SCI_SEARCHINTARGET
					    wParam: sourceLength
					    lParam: (sptr_t) textToSearch];
		replaceCount = (result < 0) ? 0 : 1;

		if (replaceCount > 0) {
			[ScintillaView directCall: self
					  message: SCI_REPLACETARGET
					   wParam: targetLength
					   lParam: (sptr_t) replacement];

			// For a single replace we set the new selection to the replaced text.
			[self setGeneralProperty: SCI_SETSELECTIONSTART value: [self getGeneralProperty: SCI_GETTARGETSTART]];
			[self setGeneralProperty: SCI_SETSELECTIONEND value: [self getGeneralProperty: SCI_GETTARGETEND]];
		}
	}

	return replaceCount;
}

//--------------------------------------------------------------------------------------------------

- (void) setFontName: (NSString *) font
		size: (int) size
		bold: (BOOL) bold
	      italic: (BOOL) italic {
	for (int i = 0; i < 128; i++) {
		[self setGeneralProperty: SCI_STYLESETFONT
			       parameter: i
				   value: (sptr_t)font.UTF8String];
		[self setGeneralProperty: SCI_STYLESETSIZE
			       parameter: i
				   value: size];
		[self setGeneralProperty: SCI_STYLESETBOLD
			       parameter: i
				   value: bold];
		[self setGeneralProperty: SCI_STYLESETITALIC
			       parameter: i
				   value: italic];
	}
}

//--------------------------------------------------------------------------------------------------

@end