File: code_editor.cpp

package info (click to toggle)
mysql-workbench 6.3.8%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 113,932 kB
  • ctags: 87,814
  • sloc: ansic: 955,521; cpp: 427,465; python: 59,728; yacc: 59,129; xml: 54,204; sql: 7,091; objc: 965; makefile: 638; sh: 613; java: 237; perl: 30; ruby: 6; php: 1
file content (1760 lines) | stat: -rw-r--r-- 63,738 bytes parent folder | download
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
/* 
 * Copyright (c) 2010, 2016, Oracle and/or its affiliates. All rights reserved.
 *
 * This program 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.
 * 
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301  USA
 */

#include "base/log.h"
#include "base/drawing.h"
#include "base/string_utilities.h"
#include "base/notifications.h"
#include "base/drawing.h"

#include "tinyxml.h"
#include "SciLexer.h"

#include "mforms/mforms.h"
#include "mdc_image.h"

DEFAULT_LOG_DOMAIN(DOMAIN_MFORMS_BE)

using namespace Scintilla;

using namespace mforms;
using namespace base;

// Marker ID assignments. Markers with higher number overlay lower ones.
// Note: the order here matches the LineMarkup enum, so we can directly use the enum as marker flags.
//       The LineMarkup is a bitmask (so we can create what Scintilla calls a marker set).
#define CE_STATEMENT_MARKER      0
#define CE_ERROR_MARKER          1
#define CE_BREAKPOINT_MARKER     2
#define CE_BREAKPOINT_HIT_MARKER 3
#define CE_CURRENT_LINE_MARKER   4
#define CE_ERROR_CONTINUE_MARKER 5

#define AC_LIST_SEPARATOR '\x19' // Unused codes as separators.
#define AC_TYPE_SEPARATOR '\x18'

#define ERROR_INDICATOR INDIC_CONTAINER
#define ERROR_INDICATOR_VALUE 42 // Arbitrary value.

//----------------- CodeEditorConfig ---------------------------------------------------------------

CodeEditorConfig::CodeEditorConfig(SyntaxHighlighterLanguage language)
{
  _used_language = language;
  _language_element = NULL;
  _document = NULL;

  std::string lexer;
  std::string override_lexer;
  switch (language)
  {
  case mforms::LanguageMySQL50:
    override_lexer = "SCLEX_MYSQL_50";
    lexer = "SCLEX_MYSQL";
    break;
      
  case mforms::LanguageMySQL51:
    override_lexer = "SCLEX_MYSQL_51";
    lexer = "SCLEX_MYSQL";
    break;
      
  case mforms::LanguageMySQL55:
    override_lexer = "SCLEX_MYSQL_55";
    lexer = "SCLEX_MYSQL";
    break;
      
  case mforms::LanguageMySQL56:
    override_lexer = "SCLEX_MYSQL_56";
    lexer = "SCLEX_MYSQL";
    break;
      
  case mforms::LanguageMySQL57:
    override_lexer = "SCLEX_MYSQL_57";
    lexer = "SCLEX_MYSQL";
    break;
      
  case mforms::LanguageHtml:
    lexer = "SCLEX_HTML";
    break;

  case mforms::LanguagePython:
    lexer = "SCLEX_PYTHON";
    break;

  case mforms::LanguageCpp:
    lexer = "SCLEX_CPP";
    break;

  case mforms::LanguageJS:
    lexer = "SCLEX_CPP";
    override_lexer = "SCLEX_CPP_JS";
    break;

  case mforms::LanguageJson:
    lexer = "SCLEX_CPP";
    override_lexer = "SCLEX_CPP_JSON";
    break;

  default:
    return;
  }

  // Load the user's config file if it exists, otherwise use the default one.
  std::string config_file = mforms::Utilities::get_special_folder(mforms::ApplicationData) + "/MySQL/Workbench/code_editor.xml";
  if (!g_file_test(config_file.c_str(), G_FILE_TEST_EXISTS))
    config_file = App::get()->get_resource_path("") + "/data/code_editor.xml";

  _document = new TiXmlDocument(config_file.c_str()); // Remove c_str() as soon as we compile against libc++ on Mac.
  if (!_document->LoadFile())
  {
    log_error("Code Editor Config: cannot load configuration file \"%s\":\n\t%s (row: %d, column: %d)\n",
      config_file.c_str(), _document->ErrorDesc(), _document->ErrorRow(), _document->ErrorCol());
    return;
  }

  TiXmlElement *element = _document->FirstChildElement("languages");
  if (element == NULL)
  {
    log_error("Code Editor: invalid configuration file \"%s\"\n", config_file.c_str());
    return;
  }

  // Load the available language identifiers. All remaining values are loaded on demand.
  for (TiXmlElement *language_element = element->FirstChildElement(); language_element != NULL;
    language_element = language_element->NextSiblingElement())
  {
    std::string language_name = language_element->Attribute("name");
    if (language_name == lexer)
      _language_element = language_element;
    _languages.push_back(language_name);
  }

  if (_language_element == NULL)
  {
    log_warning("Code Editor: could not find settings for language %s in configuration file "
      "\"%s\"\n", lexer.c_str(), config_file.c_str());
    return;
  }

  parse_properties();
  parse_settings();
  parse_keywords();
  parse_styles();
  
  // check if there's another config section containing values that should override the base one
  if (!override_lexer.empty() && override_lexer != lexer)
  {
    bool found = false;
    // Load the available language identifiers. All remaining values are loaded on demand.
    for (TiXmlElement *language_element = element->FirstChildElement(); language_element != NULL;
         language_element = language_element->NextSiblingElement())
    {
      std::string language_name = language_element->Attribute("name");
      if (language_name == override_lexer)
      {
        _language_element = language_element;
        found = true;
        break;
      }
    }

    if (found)
    {
      parse_properties();
      parse_settings();
      parse_keywords();
      parse_styles();
    }
  }
}

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

CodeEditorConfig::~CodeEditorConfig()
{
  delete _document;
}

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

void CodeEditorConfig::parse_properties()
{
  for (TiXmlElement *entry = _language_element->FirstChildElement("property"); entry != NULL;
    entry = entry->NextSiblingElement("property"))
  {
    const char* property_name = entry->Attribute("name");
    const char* property_value = entry->Attribute("value");
    if (property_name != NULL && property_value != NULL)
      _properties[property_name] = property_value;
  }
}

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

void CodeEditorConfig::parse_settings()
{
  for (TiXmlElement *entry = _language_element->FirstChildElement("setting"); entry != NULL;
    entry = entry->NextSiblingElement("setting"))
  {
    const char* property_name = entry->Attribute("name");
    const char* property_value = entry->Attribute("value");
    if (property_name != NULL && property_value != NULL)
      _settings[property_name] = property_value;
  }
}

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

/**
 * Recursively collects text of this and all its child entries.
 */
std::string collect_text(TiXmlNode *entry)
{
  std::string result;
  for (TiXmlNode* child = entry->FirstChild(); child!= NULL; child = child->NextSibling())
  {
    const TiXmlText* childText = child->ToText();
    if (childText)
      result += childText->ValueStr() + collect_text(child);
  }

  return result;
}

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

void CodeEditorConfig::parse_keywords()
{
  for (TiXmlElement* entry = _language_element->FirstChildElement("keywords"); entry != NULL;
    entry = entry->NextSiblingElement("keywords"))
  {
    std::string property_name = entry->Attribute("name");
    std::string text = collect_text(entry);
    _keywords[property_name] = text;
  }
}

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

void CodeEditorConfig::parse_styles()
{
  for (TiXmlElement* entry = _language_element->FirstChildElement("style"); entry != NULL;
    entry = entry->NextSiblingElement("style"))
  {
    int id = -1;
    entry->Attribute("id", &id);
    if (id < 0)
      continue;

    std::map<std::string, std::string> entries;
    for (TiXmlAttribute *attribute = entry->FirstAttribute(); attribute != NULL; attribute = attribute->Next())
    {
      if (strcmp(attribute->Name(), "id") == 0)
        continue;
      entries[attribute->Name()] = attribute->Value();
    }
    _styles[id] = entries;
  }
}

//----------------- CodeEditor ---------------------------------------------------------------------

CodeEditor::CodeEditor(void *host)
  : _host(host)
{
  _code_editor_impl= &ControlFactory::get_instance()->_code_editor_impl;

  _code_editor_impl->create(this);
  _code_editor_impl->send_editor(this, SCI_SETCODEPAGE, SC_CP_UTF8, 0);
  _context_menu = NULL;
  _find_panel = NULL;
  _scroll_on_resize = true;
  _auto_indent = false;

  setup();
}

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

CodeEditor::~CodeEditor()
{
  delete _find_panel;
  
  auto_completion_cancel();
  for (std::map<int, void*>::iterator iterator = _images.begin(); iterator != _images.end(); ++iterator)
    free(iterator->second);
}

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

void CodeEditor::setup()
{
  scoped_connect(Form::main_form()->signal_deactivated(), boost::bind(&CodeEditor::auto_completion_cancel, this));

  _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_NULL, 0);
  _code_editor_impl->send_editor(this, SCI_STYLERESETDEFAULT, 0, 0); // Reset default style to what it was initially.
  _code_editor_impl->send_editor(this, SCI_STYLECLEARALL, 0, 0); // Set all other styles to the default style.

  _code_editor_impl->send_editor(this, SCI_SETMARGINTYPEN, 0, SC_MARGIN_NUMBER);
#if defined(__APPLE__)
  _code_editor_impl->send_editor(this, SCI_STYLESETSIZE, STYLE_LINENUMBER, (sptr_t)10);
#else
  _code_editor_impl->send_editor(this, SCI_STYLESETSIZE, STYLE_LINENUMBER, (sptr_t)8);
#endif
  sptr_t lineNumberStyleWidth = _code_editor_impl->send_editor(this, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t)"_999999");
  _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 0, lineNumberStyleWidth);
  _code_editor_impl->send_editor(this, SCI_SETMARGINSENSITIVEN, 0, 0);

  // Margin: Markers.
  _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 1, 16);
  _code_editor_impl->send_editor(this, SCI_SETMARGINSENSITIVEN, 1, 1);

  // Folder setup.
  _code_editor_impl->send_editor(this, SCI_SETPROPERTY, (uptr_t)"fold", (sptr_t)"1");
  _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 2, 16);
  _code_editor_impl->send_editor(this, SCI_SETMARGINMASKN, 2, SC_MASK_FOLDERS);
  _code_editor_impl->send_editor(this, SCI_SETMARGINSENSITIVEN, 2, 1); // Margin is clickable.
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPEN, SC_MARK_BOXMINUS);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDER, SC_MARK_BOXPLUS);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERSUB, SC_MARK_VLINE);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERTAIL, SC_MARK_LCORNER);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEREND, SC_MARK_BOXPLUSCONNECTED);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDEROPENMID, SC_MARK_BOXMINUSCONNECTED);
  _code_editor_impl->send_editor(this, SCI_MARKERDEFINE, SC_MARKNUM_FOLDERMIDTAIL, SC_MARK_TCORNER);
  for (int n= 25; n < 32; ++n) // Markers 25..31 are reserved for folding.
  {
    _code_editor_impl->send_editor(this, SCI_MARKERSETFORE, n, 0xffffff);
    _code_editor_impl->send_editor(this, SCI_MARKERSETBACK, n, 0x404040);
  }

  // Margin: Line number style.
  _code_editor_impl->send_editor(this, SCI_STYLESETFORE, STYLE_LINENUMBER, 0x404040);
  _code_editor_impl->send_editor(this, SCI_STYLESETBACK, STYLE_LINENUMBER, 0xE0E0E0);

  // Init markers & indicators for highlighting of syntax errors.
  _code_editor_impl->send_editor(this, SCI_INDICSETFORE, ERROR_INDICATOR, 0x2119D0);
  _code_editor_impl->send_editor(this, SCI_INDICSETUNDER, ERROR_INDICATOR, 1);
  _code_editor_impl->send_editor(this, SCI_INDICSETSTYLE, ERROR_INDICATOR, INDIC_SQUIGGLE);

  // Gutter markers for errors and statements, breakpoints and current code line.
  setup_marker(CE_STATEMENT_MARKER, "editor_statement");
  setup_marker(CE_ERROR_MARKER, "editor_error");
  setup_marker(CE_BREAKPOINT_MARKER, "editor_breakpoint");
  setup_marker(CE_BREAKPOINT_HIT_MARKER, "editor_breakpoint_hit");
  setup_marker(CE_CURRENT_LINE_MARKER, "editor_current_pos");
  setup_marker(CE_ERROR_CONTINUE_MARKER, "editor_continue_on_error");

  // Other settings.
  Color color = App::get()->get_system_color(mforms::SystemColorHighlight);
  int rawColor = (int)(255 * color.red) + ((int)(255 * color.green) << 8) +
    ((int)(255 * color.blue) << 16);
  _code_editor_impl->send_editor(this, SCI_SETSELBACK, 1, rawColor);
  _code_editor_impl->send_editor(this, SCI_SETSELFORE, 1, 0xFFFFFF);

  _code_editor_impl->send_editor(this, SCI_SETCARETLINEVISIBLE, 1, 0);
  _code_editor_impl->send_editor(this, SCI_SETCARETLINEBACK, 0xF8C800, 0);
  _code_editor_impl->send_editor(this, SCI_SETCARETLINEBACKALPHA, 20, 0);

  // - Tabulators + indentation
  _code_editor_impl->send_editor(this, SCI_SETTABINDENTS, 1, 0);
  _code_editor_impl->send_editor(this, SCI_SETBACKSPACEUNINDENTS, 1, 0);

  // - Call tips
  _code_editor_impl->send_editor(this, SCI_CALLTIPSETFORE, 0x202020, 0);
  _code_editor_impl->send_editor(this, SCI_CALLTIPSETBACK, 0xF0F0F0, 0);

  _code_editor_impl->send_editor(this, SCI_SETMOUSEDWELLTIME, 200, 0);

  // - Line ending + scrolling.
  _code_editor_impl->send_editor(this, SCI_SETSCROLLWIDTHTRACKING, 1, 0);
  _code_editor_impl->send_editor(this, SCI_SETEOLMODE, SC_EOL_LF, 0);

  // - Auto completion
  _code_editor_impl->send_editor(this, SCI_AUTOCSETSEPARATOR, AC_LIST_SEPARATOR, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETTYPESEPARATOR, AC_TYPE_SEPARATOR, 0);
}

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

void CodeEditor::set_text(const char* text)
{
  _code_editor_impl->send_editor(this, SCI_SETTEXT, 0, (sptr_t)text);
}

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

void CodeEditor::set_value(const std::string &value)
{
  // When passing text as std::string we have a length and can hence use a different
  // way to set the text in the control, which preserves embedded nulls.
  _code_editor_impl->send_editor(this, SCI_CLEARALL, 0, 0);
  _code_editor_impl->send_editor(this, SCI_APPENDTEXT, value.size(), (sptr_t)value.c_str());
}

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

void CodeEditor::set_text_keeping_state(const char* text)
{
  sptr_t caret_position = _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
  sptr_t selection_start = _code_editor_impl->send_editor(this, SCI_GETSELECTIONSTART, 0, 0);
  sptr_t selection_end = _code_editor_impl->send_editor(this, SCI_GETSELECTIONEND, 0, 0);
  sptr_t first_line = _code_editor_impl->send_editor(this, SCI_GETFIRSTVISIBLELINE, 0, 0);

  _code_editor_impl->send_editor(this, SCI_SETTEXT, 0, (sptr_t)text);

  _code_editor_impl->send_editor(this, SCI_SETCURRENTPOS, caret_position, 0);
  _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, selection_start, 0);
  _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, selection_end, 0);
  _code_editor_impl->send_editor(this, SCI_SETFIRSTVISIBLELINE, first_line, 0);
}

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

void CodeEditor::append_text(const char* text, size_t length)
{
  _code_editor_impl->send_editor(this, SCI_APPENDTEXT, length, (sptr_t)text);
}

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

void CodeEditor::replace_selected_text(const std::string& text)
{
  size_t start, length;
  get_selection(start, length);
  _code_editor_impl->send_editor(this, SCI_REPLACESEL, 0, (sptr_t)text.c_str());

  _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, start + text.length(), 0);
  _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, start + text.length(), 0);
}

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

const std::string CodeEditor::get_text(bool selection_only)
{
  char* text;
  sptr_t length;
  if (selection_only)
  {
    length =_code_editor_impl->send_editor(this, SCI_GETSELTEXT, 0, 0);
    text = (char*)malloc(length);
    if (text != NULL) // Can be null if not memory is available.
      _code_editor_impl->send_editor(this, SCI_GETSELTEXT, length, (sptr_t)text);
  }
  else
  {
    length = _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0) + 1;
    text = (char*)malloc(length);
    if (text != NULL)
      _code_editor_impl->send_editor(this, SCI_GETTEXT, length, (sptr_t)text);
  }
  if (text != NULL)
  {
    std::string result(text, length - 1);
    free(text);

    return result;
  }
  return "";
}

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

const std::string CodeEditor::get_text_in_range(size_t start, size_t end)
{
  Scintilla::Sci_TextRange range;

  range.chrg.cpMin = (long)start;

  sptr_t length = _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0);
  range.chrg.cpMax = (long)(end > start + length ? length - start : end);

  range.lpstrText = (char*)malloc(end - start + 1); // Don't forget the 0 terminator.
  _code_editor_impl->send_editor(this, SCI_GETTEXTRANGE, 0, (sptr_t)&range);

  if (range.lpstrText == NULL)
    return std::string();

  std::string result(range.lpstrText, end - start);
  free(range.lpstrText);

  return result;
}

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

std::pair<const char*, size_t> CodeEditor::get_text_ptr()
{
  std::pair<const char*, size_t> result;
  result.first = (const char*)_code_editor_impl->send_editor(this, SCI_GETCHARACTERPOINTER, 0, 0);
  result.second = _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0);

  return result;
}

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

void CodeEditor::set_selection(size_t start, size_t length)
{
  _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, start, 0);
  _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, start + length, 0);
}

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

void CodeEditor::clear_selection()
{
  sptr_t current_pos = _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
  _code_editor_impl->send_editor(this, SCI_SETEMPTYSELECTION, current_pos, 0);
}

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

void CodeEditor::get_selection(size_t &start, size_t &length)
{
  start = _code_editor_impl->send_editor(this, SCI_GETSELECTIONSTART, 0, 0);
  length = _code_editor_impl->send_editor(this, SCI_GETSELECTIONEND, 0, 0) - start;
}

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

bool CodeEditor::get_range_of_line(ssize_t line, ssize_t &start, ssize_t &end)
{
  start = _code_editor_impl->send_editor(this, SCI_POSITIONFROMLINE, line, 0);
  end = _code_editor_impl->send_editor(this, SCI_GETLINEENDPOSITION, line, 0);

  return start < 0 || end < 0;
}

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

void CodeEditor::setup_marker(int marker, const std::string& name)
{
  std::string path = App::get()->get_resource_path(name + ".xpm");

  gchar *content = NULL;
  if (g_file_get_contents(path.c_str(), &content, NULL, NULL))
  {
    _code_editor_impl->send_editor(this, SCI_MARKERDEFINEPIXMAP, marker, (sptr_t)content);
    g_free(content);
  }
  _code_editor_impl->send_editor(this, SCI_MARKERSETBACK, marker, 0xD01921);
}

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

void CodeEditor::check_markers_removed(int position, int length)
{
  if (length == 0)
    return;

  sptr_t current_line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, position, 0);
  sptr_t end_line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, position + length - 1, 0);

  // Ignore the first line if it is only partially deleted (the markers would stay as they are then).
  if (position > _code_editor_impl->send_editor(this, SCI_POSITIONFROMLINE, current_line, 0))
    ++current_line;

  current_line = _code_editor_impl->send_editor(this, SCI_MARKERNEXT, current_line, LineMarkupAll);

  LineMarkupChangeset changeset;
  while (current_line > -1 && current_line <= end_line)
  {
    LineMarkup markup = (LineMarkup)_code_editor_impl->send_editor(this, SCI_MARKERGET, current_line, LineMarkupAll);
    LineMarkupChangeEntry entry = { (int)current_line, 0, markup };
    changeset.push_back(entry);

    // MARKERNEXT effectively searches lines for the given markers, the docs say.
    current_line = _code_editor_impl->send_editor(this, SCI_MARKERNEXT, current_line + 1, LineMarkupAll);
  }

  if (changeset.size() > 0)
    _marker_changed_event(changeset, true);
}

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

void CodeEditor::check_markers_moved(int position, int lines_added)
{
  if (lines_added == 0)
    return;

  sptr_t current_line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, position, 0);

  current_line = _code_editor_impl->send_editor(this, SCI_MARKERNEXT, current_line, LineMarkupAll);
  LineMarkupChangeset changeset;
  while (current_line > -1)
  {
    LineMarkup markup = (LineMarkup)_code_editor_impl->send_editor(this, SCI_MARKERGET, current_line, LineMarkupAll);
    LineMarkupChangeEntry entry = { (int)(current_line - lines_added), (int)current_line, markup };
    changeset.push_back(entry);

    current_line = _code_editor_impl->send_editor(this, SCI_MARKERNEXT, current_line + 1, LineMarkupAll);
  }

  if (changeset.size() > 0)
    _marker_changed_event(changeset, false);
}

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

void CodeEditor::load_configuration(SyntaxHighlighterLanguage language)
{
  CodeEditorConfig config(language);

  // Keywords.
  std::map<std::string, std::string> keywords = config.get_keywords();

  // Key word list sets are from currently active lexer, so that must be set before calling here.
  sptr_t length = _code_editor_impl->send_editor(this, SCI_DESCRIBEKEYWORDSETS, 0, 0);

  char* keyword_sets = (char*)malloc(length + 1);
  _code_editor_impl->send_editor(this, SCI_DESCRIBEKEYWORDSETS, 0, (sptr_t)keyword_sets);
  std::vector<std::string> keyword_list_names = base::split(keyword_sets, "\n");
  free(keyword_sets);

  for (std::map<std::string, std::string>::const_iterator iterator = keywords.begin();
    iterator != keywords.end(); ++iterator)
  {
    std::string list_name = iterator->first;
    int list_index = base::index_of(keyword_list_names, list_name);
    if (list_index > -1)
      _code_editor_impl->send_editor(this, SCI_SETKEYWORDS, list_index, (sptr_t)iterator->second.c_str());
  }

  // Properties.
  std::map<std::string, std::string> properties = config.get_properties();
  for (std::map<std::string, std::string>::const_iterator iterator = properties.begin();
    iterator != properties.end(); ++iterator)
  {
    _code_editor_impl->send_editor(this, SCI_SETPROPERTY, (uptr_t)iterator->first.c_str(), (sptr_t)iterator->second.c_str());
  }

  // Settings.
  std::map<std::string, std::string> settings = config.get_settings();
  for (std::map<std::string, std::string>::const_iterator iterator = settings.begin();
    iterator != settings.end(); ++iterator)
  {
    if (iterator->first == "usetabs")
    {
      int int_value = base::atoi<int>(iterator->second, 0);
      _code_editor_impl->send_editor(this, SCI_SETUSETABS, int_value, 0);
    }
    else
      if (iterator->first == "tabwidth")
      {
        int int_value = base::atoi<int>(iterator->second, 0);
        _code_editor_impl->send_editor(this, SCI_SETTABWIDTH, int_value, 0);
      }
      else
        if (iterator->first == "indentation")
        {
          int int_value = base::atoi<int>(iterator->second, 0);
          _code_editor_impl->send_editor(this, SCI_SETINDENT, int_value, 0);
        }
  }

  // Styles.
  std::map<int, std::map<std::string, std::string> > styles = config.get_styles();
  for (std::map<int, std::map<std::string, std::string> >::const_iterator iterator = styles.begin();
    iterator != styles.end(); ++iterator)
  {
    int id = iterator->first;
    std::map<std::string, std::string> values = iterator->second;
    std::string value = values["fore-color"];
    if (!value.empty())
    {
      // Colors for Scintilla must be converted from HTML to BGR style.
      Color color = Color::parse(value);
      int raw_color = (int)(255 * color.red) + ((int)(255 * color.green) << 8) +
        ((int)(255 * color.blue) << 16);
      _code_editor_impl-> send_editor(this, SCI_STYLESETFORE, id, raw_color);
    }

    value = values["back-color"];
    if (!value.empty())
    {
      Color color = Color::parse(value);
      int raw_color = (int)(255 * color.red) + ((int)(255 * color.green) << 8) +
        ((int)(255 * color.blue) << 16);
      _code_editor_impl->send_editor(this, SCI_STYLESETBACK, id, raw_color);
    }

    value = base::tolower(values["bold"]);
    if (!value.empty())
    {
      bool flag = value == "1" || value == "yes" || value == "true";
      _code_editor_impl->send_editor(this, SCI_STYLESETBOLD, id, flag);
    }

    value = base::tolower(values["italic"]);
    if (!value.empty())
    {
      bool flag = value == "1" || value == "yes" || value == "true";
      _code_editor_impl->send_editor(this, SCI_STYLESETITALIC, id, flag);
    }
  }
}

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

void CodeEditor::set_language(SyntaxHighlighterLanguage language)
{
  switch (language)
  {
  case mforms::LanguageMySQL50:
  case mforms::LanguageMySQL51:
  case mforms::LanguageMySQL55:
  case mforms::LanguageMySQL56:
  case mforms::LanguageMySQL57:
    _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_MYSQL, 0);
    break;

  case mforms::LanguageHtml:
    _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_HTML, 0);
    break;

  case mforms::LanguagePython:
    _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_PYTHON, 0);
    break;

  case mforms::LanguageCpp:
  case mforms::LanguageJS:
  case mforms::LanguageJson:
    _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_CPP, 0);
    break;

  default:
      // No language. Reset all styling so the editor appears without syntax highlighting.
    _code_editor_impl->send_editor(this, SCI_SETLEXER, SCLEX_NULL, 0);
    _code_editor_impl->send_editor(this, SCI_STYLERESETDEFAULT, 0, 0); // Reset default style to what it was initially.
    _code_editor_impl->send_editor(this, SCI_STYLECLEARALL, 0, 0); // Set all other styles to the default style.
    return;
  }

  load_configuration(language);
}

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

void CodeEditor::show_markup(LineMarkup markup, size_t line)
{
  // The marker mask contains one bit for each set marker (0..31).
  sptr_t marker_mask = _code_editor_impl->send_editor(this, SCI_MARKERGET, line, 0);
  sptr_t new_marker_mask = 0;
  if ((markup & mforms::LineMarkupStatement) != 0)
  {
    if ((marker_mask & LineMarkupStatement) == 0)
      new_marker_mask |= LineMarkupStatement;
  }
  if ((markup & mforms::LineMarkupErrorContinue) != 0)
  {
    if ((marker_mask & LineMarkupErrorContinue) == 0)
      new_marker_mask |= LineMarkupErrorContinue;
  }
  if ((markup & mforms::LineMarkupError) != 0)
  {
    if ((marker_mask & LineMarkupError) == 0)
      new_marker_mask |= LineMarkupError;
  }
  if ((markup & mforms::LineMarkupBreakpoint) != 0)
  {
    if ((marker_mask & LineMarkupBreakpoint) == 0)
      new_marker_mask |= LineMarkupBreakpoint;
  }
  if ((markup & mforms::LineMarkupBreakpointHit) != 0)
  {
    if ((marker_mask & LineMarkupBreakpointHit) == 0)
      new_marker_mask |= LineMarkupBreakpointHit;
  }
  if ((markup & mforms::LineMarkupCurrent) != 0)
  {
    if ((marker_mask & LineMarkupCurrent) == 0)
      new_marker_mask |= LineMarkupCurrent;
  }

  if (new_marker_mask != 0)
    _code_editor_impl->send_editor(this, SCI_MARKERADDSET, line, new_marker_mask);
}

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

void CodeEditor::remove_markup(LineMarkup markup, ssize_t line)
{
  if (markup == mforms::LineMarkupAll || line < 0)
  {
    if (line < 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETEALL, -1, 0);
    else
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, -1);
  }
  else
  {
    if ((markup & mforms::LineMarkupStatement) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_STATEMENT_MARKER);
    if ((markup & mforms::LineMarkupError) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_ERROR_MARKER);
    if ((markup & mforms::LineMarkupErrorContinue) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_ERROR_CONTINUE_MARKER);
    if ((markup & mforms::LineMarkupBreakpoint) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_BREAKPOINT_MARKER);
    if ((markup & mforms::LineMarkupBreakpointHit) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_BREAKPOINT_HIT_MARKER);
    if ((markup & mforms::LineMarkupCurrent) != 0)
      _code_editor_impl->send_editor(this, SCI_MARKERDELETE, line, CE_CURRENT_LINE_MARKER);
  }
}

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

bool CodeEditor::has_markup(LineMarkup markup, size_t line)
{
  sptr_t markers = _code_editor_impl->send_editor(this, SCI_MARKERGET, line, 0);

  if ((markup & markers) != 0)
    return true;

  return false;
}

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

void CodeEditor::show_indicator(RangeIndicator indicator, size_t start, size_t length)
{
  // Scintilla supports a model that not only sets an indicator in a given range but additionally
  // assigns a value to this indicator. This is to allow drawing an indicator with different styles.
  // However, currently all values are drawn in the same style and it is neither clear if that will ever
  // change nor what the actual use is, given that you always can set different indicators for different styles.
  // Unfortunately, you cannot query an indicator as such, only its value, so we have to set a value too
  // even if that is quite useless.
  switch (indicator)
  {
  case mforms::RangeIndicatorError:
    _code_editor_impl->send_editor(this, SCI_SETINDICATORCURRENT, ERROR_INDICATOR, 0);
    _code_editor_impl->send_editor(this, SCI_SETINDICATORVALUE, ERROR_INDICATOR_VALUE, 0);
    _code_editor_impl->send_editor(this, SCI_INDICATORFILLRANGE, start, length);

    break;
  case mforms::RangeIndicatorNone: // No effect here. Only used to have "none" value for indicator tests.
    break;
  }
}

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

RangeIndicator CodeEditor::indicator_at(size_t position)
{
  sptr_t result = _code_editor_impl->send_editor(this, SCI_INDICATORVALUEAT, ERROR_INDICATOR, position);
  if (result == ERROR_INDICATOR_VALUE)
    return mforms::RangeIndicatorError;

  return mforms::RangeIndicatorNone;
}

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

void CodeEditor::remove_indicator(RangeIndicator indicator, size_t start, size_t length)
{
  switch (indicator)
  {
  case mforms::RangeIndicatorError:
    _code_editor_impl->send_editor(this, SCI_SETINDICATORCURRENT, ERROR_INDICATOR, 0);
    _code_editor_impl->send_editor(this, SCI_INDICATORCLEARRANGE, start, length);

    break;
  case mforms::RangeIndicatorNone: // No effect here. Only needed to have a "none" value for indicator tests.
    break;
  }
}

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

size_t mforms::CodeEditor::line_count()
{
   return _code_editor_impl->send_editor(this, SCI_GETLINECOUNT, 0, 0);
}

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

size_t CodeEditor::text_length()
{
  return _code_editor_impl->send_editor(this, SCI_GETLENGTH, 0, 0);
}

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

size_t CodeEditor::position_from_line(size_t line_number)
{
  return _code_editor_impl->send_editor(this, SCI_POSITIONFROMLINE, line_number, 0);
}

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

size_t CodeEditor::line_from_position(size_t position)
{
  return _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, position, 0);
}

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

void CodeEditor::set_font(const std::string& fontDescription)
{
  // Set this font for all styles.
  std::string font;
  float size;
  bool bold;
  bool italic;
  if (base::parse_font_description(fontDescription, font, size, bold, italic))
  {
    // NOTE: The original MONOSPACE font in windows was Bitstream Vera Sans Mono
    //       but in Windows 8 the code editors using this font were getting hung so
    //       we decided to use Lucida Console as the new MONOSPACE font in windows.
#ifdef _WIN32
    if (base::toupper(font) == "BITSTREAM VERA SANS MONO")
      font = DEFAULT_MONOSPACE_FONT_FAMILY;
#endif

#if !defined(_WIN32) && !defined(__APPLE__)
    // scintilla requires the ! in front of the font name to interpret it as a pango/fontconfig font
    // the non-pango version is totally unusable
    if (!font.empty() && font[0] != '!')
       font = "!"+font;
#endif
    for (int i = 0; i < 128; i++)
    {
      _code_editor_impl->send_editor(this, SCI_STYLESETFONT, i, (sptr_t)font.c_str());
      _code_editor_impl->send_editor(this, SCI_STYLESETSIZE, i, (sptr_t)size);
      _code_editor_impl->send_editor(this, SCI_STYLESETBOLD, i, (sptr_t)bold);
      _code_editor_impl->send_editor(this, SCI_STYLESETITALIC, i, (sptr_t)italic);
    }
  }

  // Recompute the line number margin width if it is visible.
  sptr_t lineNumberStyleWidth = _code_editor_impl->send_editor(this, SCI_GETMARGINWIDTHN, 0, 0);
  if (lineNumberStyleWidth > 0)
  {
    lineNumberStyleWidth = _code_editor_impl->send_editor(this, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t)"_999999");
    _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 0, lineNumberStyleWidth);
  }
}

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

/**
 * Converts scintilla key modifier codes to mforms codes.
 */
mforms::ModifierKey getModifiers(int scintilla_modifiers)
{
  mforms::ModifierKey modifiers = mforms::ModifierNoModifier;
  if ((scintilla_modifiers & SCMOD_CTRL) == SCMOD_CTRL)
    modifiers = modifiers | mforms::ModifierControl;
  if ((scintilla_modifiers & SCMOD_ALT) == SCMOD_ALT)
    modifiers = modifiers | mforms::ModifierAlt;
  if ((scintilla_modifiers & SCMOD_SHIFT) == SCMOD_SHIFT)
    modifiers = modifiers | mforms::ModifierShift;

  return modifiers;
}

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

void CodeEditor::on_notify(SCNotification* notification)
{
  switch (notification->nmhdr.code)
  {
  case SCN_MARGINCLICK:
  {
    sptr_t line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, notification->position, 0);
    if (notification->margin == 2)
    {
      // A click on the folder margin. Toggle the current line if possible.
      _code_editor_impl->send_editor(this, SCI_TOGGLEFOLD, line, 0);
    }
    
    mforms::ModifierKey modifiers = getModifiers(notification->modifiers);
    _gutter_clicked_event(notification->margin, (int)line, modifiers);
    break;
  };
  
  case SCN_MODIFIED:
  {
    // Check for markers that would get removed.
    // Usually doesn't come with any of the other notification types.
    if ((notification->modificationType & SC_MOD_BEFOREDELETE) != 0)
      check_markers_removed(notification->position, notification->length);

    // Text insertion or removal.
    if ((notification->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT)) != 0)
    {
      check_markers_moved(notification->position, notification->linesAdded);

      _change_event(notification->position, notification->length, notification->linesAdded,
        (notification->modificationType & SC_MOD_INSERTTEXT) != 0);
    }
    break;
  }

  case SCN_AUTOCSELECTION:
    _auto_completion_event(mforms::AutoCompletionSelection, notification->position, notification->text);
    break;

  case SCN_AUTOCCANCELLED:
    _auto_completion_event(mforms::AutoCompletionCancelled, 0, "");
    break;

  case SCN_AUTOCCHARDELETED:
    _auto_completion_event(mforms::AutoCompletionCharDeleted, 0, "");
    break;

  case SCN_DWELLSTART:
    _dwell_event(true, notification->position, notification->x, notification->y);
    break;

  case SCN_DWELLEND:
    _dwell_event(false, 0, 0, 0);
    break;

  case SCN_UPDATEUI:
    switch (notification->updated)
    {
    case SC_UPDATE_CONTENT:   // Contents, styling or markers have been changed.
      break;
    case SC_UPDATE_SELECTION: // Selection has been changed or the caret moved.
      NotificationCenter::get()->send("GNTextSelectionChanged", this);
      break;
    case SC_UPDATE_V_SCROLL:  // Scrolled vertically.
      break;
    case SC_UPDATE_H_SCROLL:  // Scrolled horizontally.
      break;
    }
    break;

  case SCN_CHARADDED:
    _char_added_event(notification->ch);
    if (_auto_indent && notification->ch == '\n')
    {
      sptr_t pos = _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
      sptr_t line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, pos, 0);
      if (line > 0)
      {
		  sptr_t indentation = _code_editor_impl->send_editor(this, SCI_GETLINEINDENTATION, line - 1, 0);
        if (indentation > 0)
        {
          // Switch off tabs for a moment. We don't want a mix of tabs and spaces auto inserted
          // and tabs mess up the new indentation.
          sptr_t use_tabs = _code_editor_impl->send_editor(this, SCI_GETUSETABS, 0, 0);
          _code_editor_impl->send_editor(this, SCI_SETUSETABS, 0, 0);

          _code_editor_impl->send_editor(this, SCI_SETLINEINDENTATION, line, indentation);
          _code_editor_impl->send_editor(this, SCI_GOTOPOS, pos + indentation, 0);

          _code_editor_impl->send_editor(this, SCI_SETUSETABS, use_tabs, 0);
        }
      }

    }
    break;

  case SCN_FOCUSIN:
    focus_changed();
    break;

  case SCN_FOCUSOUT:
    _signal_lost_focus(); // For validation, parsing etc.
    break;
  }
}
//--------------------------------------------------------------------------------------------------

void CodeEditor::on_command(int command)
{
  // TODO: removal candidate.
}

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

void CodeEditor::set_features(CodeEditorFeature features, bool flag)
{
  if ((features & mforms::FeatureWrapText) != 0)
  {
    if (flag)
      _code_editor_impl->send_editor(this, SCI_SETWRAPMODE, SC_WRAP_WORD, 0);
    else
      _code_editor_impl->send_editor(this, SCI_SETWRAPMODE, SC_WRAP_NONE, 0);
  }

  if ((features & mforms::FeatureGutter) != 0)
  {
    if (flag)
    {
      sptr_t width = _code_editor_impl->send_editor(this, SCI_TEXTWIDTH, STYLE_LINENUMBER, (sptr_t)"_999999");

      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 0, width); // line numbers
      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 1, 16); // markers
      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 2, 16); // fold markers
    }
    else
    {
      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 0, 0);
      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 1, 0);
      _code_editor_impl->send_editor(this, SCI_SETMARGINWIDTHN, 2, 0);
    }
  }

  if ((features & mforms::FeatureReadOnly) != 0)
    _code_editor_impl->send_editor(this, SCI_SETREADONLY, flag, 0);

  if ((features & mforms::FeatureShowSpecial) != 0)
  {
    _code_editor_impl->send_editor(this, SCI_SETVIEWEOL, flag, 0);
    if (flag)
      _code_editor_impl->send_editor(this, SCI_SETVIEWWS, SCWS_VISIBLEALWAYS, 0);
    else
      _code_editor_impl->send_editor(this, SCI_SETVIEWWS, SCWS_INVISIBLE, 0);
  }

  if ((features & mforms::FeatureUsePopup) != 0)
    _code_editor_impl->send_editor(this, SCI_USEPOPUP, flag, 0);

  if ((features & mforms::FeatureConvertEolOnPaste) != 0)
    _code_editor_impl->send_editor(this, SCI_SETPASTECONVERTENDINGS, flag, 0);

  if ((features & mforms::FeatureScrollOnResize) != 0)
    _scroll_on_resize = true;

  if ((features & mforms::FeatureFolding) != 0)
    _code_editor_impl->send_editor(this, SCI_SETPROPERTY, (uptr_t)"fold", flag ? (sptr_t)"1" : (sptr_t)"0");

  if ((features & mforms::FeatureAutoIndent) != 0)
    _auto_indent = true;
}

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

void CodeEditor::toggle_features(CodeEditorFeature features)
{
  // Toggling a feature involves querying its current state which is sometimes not possible with a
  // single value, so instead of returning the current state and let the application call
  // set_features we do it internally with this toggle_features function.
  
  if ((features & mforms::FeatureWrapText) != 0)
  {
    if (_code_editor_impl->send_editor(this, SCI_GETWRAPMODE, 0, 0) == SC_WRAP_NONE)
      _code_editor_impl->send_editor(this, SCI_SETWRAPMODE, SC_WRAP_WORD, 0);
    else
      _code_editor_impl->send_editor(this, SCI_SETWRAPMODE, SC_WRAP_NONE, 0);
  }

  if ((features & mforms::FeatureGutter) != 0)
      set_features(mforms::FeatureGutter,
        _code_editor_impl->send_editor(this, SCI_GETMARGINWIDTHN, 0, 0) == 0);

  if ((features & mforms::FeatureReadOnly) != 0)
    set_features(mforms::FeatureReadOnly, 
      _code_editor_impl->send_editor(this, SCI_GETREADONLY, 0, 0) == 0);

  if ((features & mforms::FeatureShowSpecial) != 0)
    set_features(mforms::FeatureShowSpecial, 
      _code_editor_impl->send_editor(this, SCI_GETVIEWEOL, 0, 0) == 0);

  // if ((features & mforms::FeatureUsePopup) != 0)
  // Not searchable so we cannot toggle it.
  
  if ((features & mforms::FeatureConvertEolOnPaste) != 0)
    set_features(mforms::FeatureConvertEolOnPaste,
      _code_editor_impl->send_editor(this, SCI_GETPASTECONVERTENDINGS, 0, 0) != 0);

  if ((features & mforms::FeatureScrollOnResize) != 0)
    _scroll_on_resize = !_scroll_on_resize;

  if ((features & mforms::FeatureFolding) != 0)
  {
    bool folding_enabled = _code_editor_impl->send_editor(this, SCI_GETPROPERTYINT, (uptr_t)"fold", 0) != 0;
    _code_editor_impl->send_editor(this, SCI_SETPROPERTY, (uptr_t)"fold", folding_enabled ? (sptr_t)"0" : (sptr_t)"1");
  }

  if ((features & mforms::FeatureAutoIndent) != 0)
    _auto_indent = !_auto_indent;
}

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

void CodeEditor::set_read_only(bool flag)
{
  _code_editor_impl->send_editor(this, SCI_SETREADONLY, flag, 0);
}

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

void CodeEditor::reset_undo_stack()
{
  _code_editor_impl->send_editor(this, SCI_EMPTYUNDOBUFFER, 0, 0);
}

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

void CodeEditor::reset_dirty()
{
  _code_editor_impl->send_editor(this, SCI_SETSAVEPOINT, 0, 0);
}

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

bool CodeEditor::is_dirty()
{
  return _code_editor_impl->send_editor(this, SCI_GETMODIFY, 0, 0) != 0;
}

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

size_t CodeEditor::get_caret_pos()
{
  return _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
}

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

void CodeEditor::set_caret_pos(size_t position)
{
  _code_editor_impl->send_editor(this, SCI_GOTOPOS, position, 0);
}

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

void CodeEditor::get_line_column_pos(size_t position, size_t &line, size_t &column)
{
  line = _code_editor_impl->send_editor(this, SCI_LINEFROMPOSITION, position, 0);
  column = _code_editor_impl->send_editor(this, SCI_GETCOLUMN, position, 0);;
}

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

bool CodeEditor::can_undo()
{
  return _code_editor_impl->send_editor(this, SCI_CANUNDO, 0, 0) != 0;
}

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

void CodeEditor::undo()
{
  _code_editor_impl->send_editor(this, SCI_UNDO, 0, 0);
}

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

bool CodeEditor::can_redo()
{
  return _code_editor_impl->send_editor(this, SCI_CANREDO, 0, 0) != 0;
}

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

void CodeEditor::redo()
{
  _code_editor_impl->send_editor(this, SCI_REDO, 0, 0);
}

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

bool CodeEditor::can_cut()
{
  return can_copy() && can_delete();
}

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

void CodeEditor::cut()
{
  _code_editor_impl->send_editor(this, SCI_CUT, 0, 0);
}

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

bool CodeEditor::can_copy()
{
  sptr_t length = _code_editor_impl->send_editor(this, SCI_GETSELECTIONEND, 0, 0) -
    _code_editor_impl->send_editor(this, SCI_GETSELECTIONSTART, 0, 0);
  return length > 0;
}

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

void CodeEditor::copy()
{
  _code_editor_impl->send_editor(this, SCI_COPY, 0, 0);
}

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

bool CodeEditor::can_paste()
{
  return _code_editor_impl->send_editor(this, SCI_CANPASTE, 0, 0) != 0;
}

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

void CodeEditor::paste()
{
  _code_editor_impl->send_editor(this, SCI_PASTE, 0, 0);
}

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

bool CodeEditor::can_delete()
{
  return can_copy() && _code_editor_impl->send_editor(this, SCI_GETREADONLY, 0, 0) == 0;
}

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

void CodeEditor::do_delete()
{
  replace_selected_text("");
}

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

void CodeEditor::select_all()
{
  _code_editor_impl->send_editor(this, SCI_SELECTALL, 0, 0);
}

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

void CodeEditor::set_status_text(const std::string& text)
{
  // Optional implementation.
  if (_code_editor_impl->set_status_text != NULL)
    _code_editor_impl->set_status_text(this, text);
}

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

void CodeEditor::show_find_panel(bool replace)
{
  if (_find_panel == NULL)
    _find_panel = new FindPanel(this);
  _find_panel->enable_replace(replace);

  if (_show_find_panel)
    _show_find_panel(this, true);
  _find_panel->focus();
}

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

void CodeEditor::hide_find_panel()
{
  if (_find_panel == NULL)
    return;

  if (_show_find_panel && _find_panel->is_shown())
    _show_find_panel(this, false);
  focus();
}

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

void CodeEditor::set_show_find_panel_callback(boost::function<void (CodeEditor*, bool)> callback)
{
  _show_find_panel = callback;
}

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

bool CodeEditor::find_and_highlight_text(const std::string& search_text, FindFlags flags,
  bool scroll_to, bool backwards)
{
  if (search_text.size() == 0)
    return false;

  bool wrap = (flags & FindWrapAround) != 0;
  int search_flags= 0;
  if (flags & FindMatchCase)
    search_flags |= SCFIND_MATCHCASE;
  if (flags & FindWholeWords)
    search_flags |= SCFIND_WHOLEWORD;
  if (flags & FindRegex)
    search_flags |= SCFIND_REGEXP;

  sptr_t selection_start = _code_editor_impl->send_editor(this, SCI_GETSELECTIONSTART, 0, 0);
  sptr_t selection_end = _code_editor_impl->send_editor(this, SCI_GETSELECTIONEND, 0, 0);

  // Sets the start point for the upcoming search to the begin 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)
    _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, selection_end, 0);
  _code_editor_impl->send_editor(this, SCI_SEARCHANCHOR, 0, 0);

  sptr_t result = 0;

  // The following call will also set the selection if something was found.
  if (backwards)
  {
    result = _code_editor_impl->send_editor(this, SCI_SEARCHPREV, search_flags, (sptr_t) search_text.c_str());
    if (result < 0 && wrap)
    {
      // Try again from the end of the document if nothing could be found so far and
      // wrapped search is set.
      _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART,
        _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0), 0);
      _code_editor_impl->send_editor(this, SCI_SEARCHANCHOR, 0, 0);
      result = _code_editor_impl->send_editor(this, SCI_SEARCHNEXT, search_flags, (sptr_t) search_text.c_str());
    }
  }
  else
  {
    result = _code_editor_impl->send_editor(this, SCI_SEARCHNEXT, search_flags, (sptr_t) search_text.c_str());
    if (result < 0 && wrap)
    {
      // Try again from the start of the document if nothing could be found so far and
      // wrapped search is set.
      _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, 0, 0);
      _code_editor_impl->send_editor(this, SCI_SEARCHANCHOR, 0, 0);
      result = _code_editor_impl->send_editor(this, SCI_SEARCHNEXT, search_flags, (sptr_t) search_text.c_str());
    }
  }

  if (result >= 0)
  {
    if (scroll_to)
      _code_editor_impl->send_editor(this, SCI_SCROLLCARET, 0, 0);
  }
  else
  {
    // Restore the former selection if we did not find anything.
    _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, selection_start, 0);
    _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, selection_end, 0);
  }
  return (result >= 0) ? true : false;

}

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

/**
 * Searches the given text and replaces it by new_text. Returns the number of replacements performed.
 */
size_t CodeEditor::find_and_replace_text(const std::string& search_text, const std::string& new_text,
  FindFlags flags, bool do_all)
{
  if (search_text.size() == 0)
    return 0;

  // The current position is where we start searching for single occurrences. Otherwise we start at
  // the beginning of the document.
  sptr_t start_position;
  if (do_all)
    start_position = 0;
  else
    start_position = _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
  sptr_t endPosition = _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0);

  int search_flags= 0;
  if (flags & FindMatchCase)
    search_flags |= SCFIND_MATCHCASE;
  if (flags & FindWholeWords)
    search_flags |= SCFIND_WHOLEWORD;
  if (flags & FindRegex)
    search_flags |= SCFIND_REGEXP;
  _code_editor_impl->send_editor(this, SCI_SETSEARCHFLAGS, search_flags, 0);
  _code_editor_impl->send_editor(this, SCI_SETTARGETSTART, start_position, 0);
  _code_editor_impl->send_editor(this, SCI_SETTARGETEND, endPosition, 0);

  sptr_t result;

  size_t replace_count = 0;
  if (do_all)
  {
    while (true)
    {
      result = _code_editor_impl->send_editor(this, SCI_SEARCHINTARGET, search_text.size(), (sptr_t)search_text.c_str());
      if (result < 0)
        break;

      replace_count++;
      /*result =*/ _code_editor_impl->send_editor(this, SCI_REPLACETARGET, new_text.size(), (sptr_t)new_text.c_str());

      // The replacement changes the target range to the replaced text. Continue after that until the end.
      // The text length might be changed by the replacement so make sure the target end is the actual
      // text end.
      _code_editor_impl->send_editor(this, SCI_SETTARGETSTART,
        _code_editor_impl->send_editor(this, SCI_GETTARGETEND, 0, 0), 0);
      _code_editor_impl->send_editor(this, SCI_SETTARGETEND,
        _code_editor_impl->send_editor(this, SCI_GETTEXTLENGTH, 0, 0), 0);
    }
  }
  else
  {
    result = _code_editor_impl->send_editor(this, SCI_SEARCHINTARGET, search_text.size(), (sptr_t)search_text.c_str());
    replace_count = (result < 0) ? 0 : 1;

    if (replace_count > 0)
    {
      /*result =*/ _code_editor_impl->send_editor(this, SCI_REPLACETARGET, new_text.size(), (sptr_t)new_text.c_str());

      // For a single replace we set the new selection to the replaced text.
      _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART,
        _code_editor_impl->send_editor(this, SCI_GETTARGETSTART, 0, 0), 0);
      _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND,
        _code_editor_impl->send_editor(this, SCI_GETTARGETEND, 0, 0), 0);
    }
  }

  return replace_count;
}

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

void CodeEditor::jump_to_next_placeholder()
{
  sptr_t current_pos = _code_editor_impl->send_editor(this, SCI_GETCURRENTPOS, 0, 0);
  sptr_t text_size = _code_editor_impl->send_editor(this, SCI_GETLENGTH, 0, 0);
  Sci_TextToFind what;
  
  what.lpstrText = (char*)"<{";
  what.chrg.cpMin = (long)current_pos;
  what.chrg.cpMax = (long)text_size;
  sptr_t result = _code_editor_impl->send_editor(this, SCI_FINDTEXT, 0, (sptr_t)&what);
  bool found = false;
  if (result >= 0)
  {
    static const int max_placeholder_length = 256;
    what.lpstrText = (char*)"}>";
    what.chrg.cpMin = (long)result;
    what.chrg.cpMax = what.chrg.cpMin + max_placeholder_length; // arbitrary max size for placeholder text
    result = _code_editor_impl->send_editor(this, SCI_FINDTEXT, 0, (sptr_t) &what);
    if (result >= 0)
    {
      char buffer[max_placeholder_length];
      TextRange tr;
      tr.chrg.cpMin = what.chrg.cpMin;
      tr.chrg.cpMax = (long)result + 2;
      tr.lpstrText= buffer;
      _code_editor_impl->send_editor(this, SCI_GETTEXTRANGE, 0, (sptr_t)&tr);
      
      if (!memchr(buffer, '\n', tr.chrg.cpMax - tr.chrg.cpMin))
      {
        // jump to placeholder and select it      
        _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, tr.chrg.cpMin, 0);
        _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, tr.chrg.cpMax, 0);
        _code_editor_impl->send_editor(this, SCI_SCROLLCARET, 0, 0);
        found = true;
      }
    }
  }
  if (!found)
  {
    // restore the cursor to where it was
    _code_editor_impl->send_editor(this, SCI_SETSELECTIONSTART, current_pos, 0);
    _code_editor_impl->send_editor(this, SCI_SETSELECTIONEND, current_pos, 0);
  }
}

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

void CodeEditor::auto_completion_show(size_t chars_entered, const std::vector<std::pair<int, std::string> >& entries)
{
  if (entries.size() == 0)
    return;
  
  std::stringstream list;
  for (size_t i = 0; i < entries.size(); ++i)
  {
    if (i > 0)
      list << AC_LIST_SEPARATOR;
    list << entries[i].second;
    if (entries[i].first > -1)
      list << AC_TYPE_SEPARATOR << entries[i].first;
  }
  _code_editor_impl->send_editor(this, SCI_AUTOCSHOW, chars_entered, (sptr_t)list.str().c_str());
}

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

void CodeEditor::auto_completion_show(size_t chars_entered, const std::vector<std::string>& entries)
{
  std::stringstream list;
  for (size_t i = 0; i < entries.size(); ++i)
  {
    if (i > 0)
      list << AC_LIST_SEPARATOR;
    list << entries[i];
  }
  _code_editor_impl->send_editor(this, SCI_AUTOCSHOW, chars_entered, (sptr_t)list.str().c_str());
}

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

void CodeEditor::auto_completion_cancel()
{
  _code_editor_impl->send_editor(this, SCI_AUTOCCANCEL, 0, 0);
}

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

void CodeEditor::auto_completion_options(bool ignore_case, bool choose_single, bool auto_hide,
  bool drop_rest_of_word, bool cancel_at_start)
{
  _code_editor_impl->send_editor(this, SCI_AUTOCSETIGNORECASE, ignore_case, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETCHOOSESINGLE, choose_single, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETAUTOHIDE, auto_hide, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETDROPRESTOFWORD, drop_rest_of_word, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETCANCELATSTART, cancel_at_start, 0);
}

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

void CodeEditor::auto_completion_max_size(int width, int height)
{
  _code_editor_impl->send_editor(this, SCI_AUTOCSETMAXHEIGHT, height, 0);
  _code_editor_impl->send_editor(this, SCI_AUTOCSETMAXWIDTH, width, 0);
}

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

void CodeEditor::auto_completion_register_images(const std::vector<std::pair<int, std::string> >& images)
{
  for (size_t i = 0; i < images.size(); ++i)
  {
    std::string path = App::get()->get_resource_path(images[i].second);
    if (g_file_test(path.c_str(), G_FILE_TEST_EXISTS))
    {
      if (g_str_has_suffix(path.c_str(), ".png"))
      {
        cairo_surface_t *image = mdc::surface_from_png_image(path.c_str());
        if (image == NULL)
          continue;
        if (cairo_surface_status(image) != CAIRO_STATUS_SUCCESS)
        {
          cairo_surface_destroy(image);
          continue;
        }
        
        int width = cairo_image_surface_get_width(image);
        int height = cairo_image_surface_get_height(image);
        _code_editor_impl->send_editor(this, SCI_RGBAIMAGESETWIDTH, width, 0);
        _code_editor_impl->send_editor(this, SCI_RGBAIMAGESETHEIGHT, height, 0);

        unsigned char *data = cairo_image_surface_get_data(image);
        
        // We not only need to keep the data around for the lifetime of the editor we also have
        // to swap blue and red. Allocate memory for this in our internal images list. Release what
        // is there already for this id.
        std::map<int, void*>::iterator entry = _images.find(images[i].first);
        if (entry != _images.end())
          free(entry->second);
        
        unsigned char *target = (unsigned char*)malloc(4 * width * height);
        if (target != NULL)
        {
          _images[images[i].first] = target;
          int j = 0;
          while (j < 4 * width * height)
          {
            target[j] = data[j + 2];
            target[j + 1] = data[j + 1];
            target[j + 2] = data[j];
            target[j + 3] = data[j + 3];
            j += 4;
          }
        }
        _code_editor_impl->send_editor(this, SCI_REGISTERRGBAIMAGE, images[i].first, (sptr_t)target);
        cairo_surface_destroy(image);
      }
      else
        if (g_str_has_suffix(path.c_str(), ".xpm"))
        {
          gchar* contents;
          gsize length;
          if (g_file_get_contents(path.c_str(), &contents, &length, NULL))
          {
            _code_editor_impl->send_editor(this, SCI_REGISTERIMAGE, images[i].first, (sptr_t)contents);
            g_free(contents);
          }
        }
    }
  }
}

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

bool CodeEditor::auto_completion_active()
{
  return _code_editor_impl->send_editor(this, SCI_AUTOCACTIVE, 0, 0) != 0;
}

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

void CodeEditor::auto_completion_stops(const std::string& stops)
{
  _code_editor_impl->send_editor(this, SCI_AUTOCSTOPS, 0, (sptr_t)stops.c_str());
}

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

void CodeEditor::auto_completion_fillups(const std::string& fillups)
{
  _code_editor_impl->send_editor(this, SCI_AUTOCSETFILLUPS, 0, (sptr_t)fillups.c_str());
}

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

void CodeEditor::show_calltip(bool show, size_t position, const std::string& value)
{
  if (show)
    _code_editor_impl->send_editor(this, SCI_CALLTIPSHOW, position, (sptr_t)value.c_str());
  else
    _code_editor_impl->send_editor(this, SCI_CALLTIPCANCEL, 0, 0);
}

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

void CodeEditor::set_eol_mode(mforms::EndOfLineMode mode, bool convert)
{
  _code_editor_impl->send_editor(this, SCI_SETEOLMODE, mode, 0);
  if (convert)
    _code_editor_impl->send_editor(this, SCI_CONVERTEOLS, mode, 0);
}

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

sptr_t CodeEditor::send_editor(unsigned int message, uptr_t wParam, sptr_t lParam)
{
  return _code_editor_impl->send_editor(this, message, wParam, lParam);
}

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

void CodeEditor::lost_focus()
{
  _signal_lost_focus();
}

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

void CodeEditor::resize()
{
  if (_scroll_on_resize)
    _code_editor_impl->send_editor(this, SCI_SCROLLCARET, 0, 0);
}

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