-
Notifications
You must be signed in to change notification settings - Fork 120
/
editors.d
4428 lines (4073 loc) · 169 KB
/
editors.d
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
// Written in the D programming language.
/**
This module contains implementation of editors.
EditLine - single line editor.
EditBox - multiline editor
LogWidget - readonly text box for showing logs
Synopsis:
----
import dlangui.widgets.editors;
----
Copyright: Vadim Lopatin, 2014
License: Boost License 1.0
Authors: Vadim Lopatin, coolreader.org@gmail.com
*/
module dlangui.widgets.editors;
import dlangui.widgets.widget;
import dlangui.widgets.controls;
import dlangui.widgets.scroll;
import dlangui.widgets.layouts;
import dlangui.core.signals;
import dlangui.core.collections;
import dlangui.core.linestream;
import dlangui.platforms.common.platform;
import dlangui.widgets.menu;
import dlangui.widgets.popup;
import dlangui.graphics.colors;
public import dlangui.core.editable;
import std.algorithm;
import std.conv : to;
import std.typecons : Yes, No;
import dlangui.core.streams;
/// Modified state change listener
interface ModifiedStateListener {
void onModifiedStateChange(Widget source, bool modified);
}
/// Modified content listener
interface EditableContentChangeListener {
void onEditableContentChanged(EditableContent source);
}
/// editor state to display in status line
struct EditorStateInfo {
/// editor mode: true if replace mode, false if insert mode
bool replaceMode;
/// cursor position column (1-based)
int col;
/// cursor position line (1-based)
int line;
/// character under cursor
dchar character;
/// returns true if editor is in active state
@property bool active() { return col > 0 && line > 0; }
}
interface EditorStateListener {
void onEditorStateUpdate(Widget source, ref EditorStateInfo editorState);
}
/// Flags used for search / replace / text highlight
enum TextSearchFlag {
CaseSensitive = 1,
WholeWords = 2,
SelectionOnly = 4,
}
/// Editor action codes
enum EditorActions : int {
None = 0,
/// move cursor one char left
Left = 1000,
/// move cursor one char left with selection
SelectLeft,
/// move cursor one char right
Right,
/// move cursor one char right with selection
SelectRight,
/// move cursor one line up
Up,
/// move cursor one line up with selection
SelectUp,
/// move cursor one line down
Down,
/// move cursor one line down with selection
SelectDown,
/// move cursor one word left
WordLeft,
/// move cursor one word left with selection
SelectWordLeft,
/// move cursor one word right
WordRight,
/// move cursor one word right with selection
SelectWordRight,
/// move cursor one page up
PageUp,
/// move cursor one page up with selection
SelectPageUp,
/// move cursor one page down
PageDown,
/// move cursor one page down with selection
SelectPageDown,
/// move cursor to the beginning of page
PageBegin,
/// move cursor to the beginning of page with selection
SelectPageBegin,
/// move cursor to the end of page
PageEnd,
/// move cursor to the end of page with selection
SelectPageEnd,
/// move cursor to the beginning of line
LineBegin,
/// move cursor to the beginning of line with selection
SelectLineBegin,
/// move cursor to the end of line
LineEnd,
/// move cursor to the end of line with selection
SelectLineEnd,
/// move cursor to the beginning of document
DocumentBegin,
/// move cursor to the beginning of document with selection
SelectDocumentBegin,
/// move cursor to the end of document
DocumentEnd,
/// move cursor to the end of document with selection
SelectDocumentEnd,
/// delete char before cursor (backspace)
DelPrevChar,
/// delete char after cursor (del key)
DelNextChar,
/// delete word before cursor (ctrl + backspace)
DelPrevWord,
/// delete char after cursor (ctrl + del key)
DelNextWord,
/// insert new line (Enter)
InsertNewLine,
/// insert new line before current position (Ctrl+Enter)
PrependNewLine,
/// insert new line after current position (Ctrl+Enter)
AppendNewLine,
/// Turn On/Off replace mode
ToggleReplaceMode,
/// Copy selection to clipboard
Copy,
/// Cut selection to clipboard
Cut,
/// Paste selection from clipboard
Paste,
/// Undo last change
Undo,
/// Redo last undoed change
Redo,
/// Tab (e.g., Tab key to insert tab character or indent text)
Tab,
/// Tab (unindent text, or remove whitespace before cursor, usually Shift+Tab)
BackTab,
/// Indent text block or single line
Indent,
/// Unindent text
Unindent,
/// Select whole content (usually, Ctrl+A)
SelectAll,
// Scroll operations
/// Scroll one line up (not changing cursor)
ScrollLineUp,
/// Scroll one line down (not changing cursor)
ScrollLineDown,
/// Scroll one page up (not changing cursor)
ScrollPageUp,
/// Scroll one page down (not changing cursor)
ScrollPageDown,
/// Scroll window left
ScrollLeft,
/// Scroll window right
ScrollRight,
/// Zoom in editor font
ZoomIn,
/// Zoom out editor font
ZoomOut,
/// Togle line comment
ToggleLineComment,
/// Toggle block comment
ToggleBlockComment,
/// Delete current line
DeleteLine,
/// Insert line
InsertLine,
/// Toggle bookmark in current line
ToggleBookmark,
/// move cursor to next bookmark
GoToNextBookmark,
/// move cursor to previous bookmark
GoToPreviousBookmark,
/// Find text
Find,
/// Replace text
Replace,
/// Find next occurence - continue search forward
FindNext,
/// Find previous occurence - continue search backward
FindPrev,
}
void initStandardEditorActions() {
// register editor action names and ids
registerActionEnum!EditorActions();
}
const Action ACTION_EDITOR_COPY = (new Action(EditorActions.Copy, "MENU_EDIT_COPY"c, null, KeyCode.KEY_C, KeyFlag.Control)).addAccelerator(KeyCode.INS, KeyFlag.Control).disableByDefault();
const Action ACTION_EDITOR_PASTE = (new Action(EditorActions.Paste, "MENU_EDIT_PASTE"c, null, KeyCode.KEY_V, KeyFlag.Control)).addAccelerator(KeyCode.INS, KeyFlag.Shift).disableByDefault();
const Action ACTION_EDITOR_CUT = (new Action(EditorActions.Cut, "MENU_EDIT_CUT"c, null, KeyCode.KEY_X, KeyFlag.Control)).addAccelerator(KeyCode.DEL, KeyFlag.Shift).disableByDefault();
const Action ACTION_EDITOR_UNDO = (new Action(EditorActions.Undo, "MENU_EDIT_UNDO"c, null, KeyCode.KEY_Z, KeyFlag.Control)).disableByDefault();
const Action ACTION_EDITOR_REDO = (new Action(EditorActions.Redo, "MENU_EDIT_REDO"c, null, KeyCode.KEY_Y, KeyFlag.Control)).addAccelerator(KeyCode.KEY_Z, KeyFlag.Control|KeyFlag.Shift).disableByDefault();
const Action ACTION_EDITOR_INSERT_NEW_LINE = (new Action(EditorActions.InsertNewLine, KeyCode.RETURN, 0, ActionStateUpdateFlag.never)).addAccelerator(KeyCode.RETURN, KeyFlag.Shift);
const Action ACTION_EDITOR_PREPEND_NEW_LINE = (new Action(EditorActions.PrependNewLine, KeyCode.RETURN, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never));
const Action ACTION_EDITOR_APPEND_NEW_LINE = (new Action(EditorActions.AppendNewLine, KeyCode.RETURN, KeyFlag.Control, ActionStateUpdateFlag.never));
const Action ACTION_EDITOR_DELETE_LINE = (new Action(EditorActions.DeleteLine, KeyCode.KEY_D, KeyFlag.Control, ActionStateUpdateFlag.never)).addAccelerator(KeyCode.KEY_L, KeyFlag.Control);
const Action ACTION_EDITOR_TOGGLE_REPLACE_MODE = (new Action(EditorActions.ToggleReplaceMode, KeyCode.INS, 0, ActionStateUpdateFlag.never));
const Action ACTION_EDITOR_SELECT_ALL = (new Action(EditorActions.SelectAll, KeyCode.KEY_A, KeyFlag.Control, ActionStateUpdateFlag.never));
const Action ACTION_EDITOR_TOGGLE_LINE_COMMENT = (new Action(EditorActions.ToggleLineComment, KeyCode.KEY_DIVIDE, KeyFlag.Control));
const Action ACTION_EDITOR_TOGGLE_BLOCK_COMMENT = (new Action(EditorActions.ToggleBlockComment, KeyCode.KEY_DIVIDE, KeyFlag.Control | KeyFlag.Shift));
const Action ACTION_EDITOR_TOGGLE_BOOKMARK = (new Action(EditorActions.ToggleBookmark, "ACTION_EDITOR_TOGGLE_BOOKMARK"c, null, KeyCode.KEY_B, KeyFlag.Control | KeyFlag.Shift));
const Action ACTION_EDITOR_GOTO_NEXT_BOOKMARK = (new Action(EditorActions.GoToNextBookmark, "ACTION_EDITOR_GOTO_NEXT_BOOKMARK"c, null, KeyCode.DOWN, KeyFlag.Control | KeyFlag.Shift | KeyFlag.Alt));
const Action ACTION_EDITOR_GOTO_PREVIOUS_BOOKMARK = (new Action(EditorActions.GoToPreviousBookmark, "ACTION_EDITOR_GOTO_PREVIOUS_BOOKMARK"c, null, KeyCode.UP, KeyFlag.Control | KeyFlag.Shift | KeyFlag.Alt));
const Action ACTION_EDITOR_FIND = (new Action(EditorActions.Find, "ACTION_EDITOR_FIND"c, null, KeyCode.KEY_F, KeyFlag.Control));
const Action ACTION_EDITOR_FIND_NEXT = (new Action(EditorActions.FindNext, "ACTION_EDITOR_FIND_NEXT"c, null, KeyCode.F3, 0));
const Action ACTION_EDITOR_FIND_PREV = (new Action(EditorActions.FindPrev, "ACTION_EDITOR_FIND_PREV"c, null, KeyCode.F3, KeyFlag.Shift));
const Action ACTION_EDITOR_REPLACE = (new Action(EditorActions.Replace, "ACTION_EDITOR_REPLACE"c, null, KeyCode.KEY_H, KeyFlag.Control));
const Action[] STD_EDITOR_ACTIONS = [ACTION_EDITOR_INSERT_NEW_LINE, ACTION_EDITOR_PREPEND_NEW_LINE,
ACTION_EDITOR_APPEND_NEW_LINE, ACTION_EDITOR_DELETE_LINE, ACTION_EDITOR_TOGGLE_REPLACE_MODE,
ACTION_EDITOR_SELECT_ALL, ACTION_EDITOR_TOGGLE_LINE_COMMENT, ACTION_EDITOR_TOGGLE_BLOCK_COMMENT,
ACTION_EDITOR_TOGGLE_BOOKMARK, ACTION_EDITOR_GOTO_NEXT_BOOKMARK, ACTION_EDITOR_GOTO_PREVIOUS_BOOKMARK,
ACTION_EDITOR_FIND, ACTION_EDITOR_REPLACE,
ACTION_EDITOR_FIND_NEXT, ACTION_EDITOR_FIND_PREV,
ACTION_EDITOR_COPY, ACTION_EDITOR_PASTE, ACTION_EDITOR_CUT,
ACTION_EDITOR_UNDO, ACTION_EDITOR_REDO
];
/// base for all editor widgets
class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemActionHandler {
protected EditableContent _content;
protected int _lineHeight;
protected Point _scrollPos;
protected bool _fixedFont;
protected int _spaceWidth;
protected int _leftPaneWidth; // left pane - can be used to show line numbers, collapse controls, bookmarks, breakpoints, custom icons
protected int _minFontSize = -1; // disable zooming
protected int _maxFontSize = -1; // disable zooming
protected bool _wantTabs = true;
protected bool _showLineNumbers = false; // show line numbers in left pane
protected bool _showModificationMarks = false; // show modification marks in left pane
protected bool _showIcons = false; // show icons in left pane
protected bool _showFolding = false; // show folding controls in left pane
protected int _lineNumbersWidth = 0;
protected int _modificationMarksWidth = 0;
protected int _iconsWidth = 0;
protected int _foldingWidth = 0;
protected bool _selectAllWhenFocusedWithTab = false;
protected bool _deselectAllWhenUnfocused = false;
protected bool _replaceMode;
protected uint _selectionColorFocused = 0xB060A0FF;
protected uint _selectionColorNormal = 0xD060A0FF;
protected uint _searchHighlightColorCurrent = 0x808080FF;
protected uint _searchHighlightColorOther = 0xC08080FF;
protected uint _leftPaneBackgroundColor = 0xF4F4F4;
protected uint _leftPaneBackgroundColor2 = 0xFFFFFF;
protected uint _leftPaneBackgroundColor3 = 0xF8F8F8;
protected uint _leftPaneLineNumberColor = 0x4060D0;
protected uint _leftPaneLineNumberColorEdited = 0xC0C000;
protected uint _leftPaneLineNumberColorSaved = 0x00C000;
protected uint _leftPaneLineNumberColorCurrentLine = 0xFFFFFFFF;
protected uint _leftPaneLineNumberBackgroundColorCurrentLine = 0xC08080FF;
protected uint _leftPaneLineNumberBackgroundColor = 0xF4F4F4;
protected uint _colorIconBreakpoint = 0xFF0000;
protected uint _colorIconBookmark = 0x0000FF;
protected uint _colorIconError = 0x80FF0000;
protected uint _caretColor = 0x000000;
protected uint _caretColorReplace = 0x808080FF;
protected uint _matchingBracketHightlightColor = 0x60FFE0B0;
protected uint _iconsPaneWidth = WIDGET_STYLE_CONSOLE ? 1 : 16;
protected uint _foldingPaneWidth = WIDGET_STYLE_CONSOLE ? 1 : 12;
protected uint _modificationMarksPaneWidth = WIDGET_STYLE_CONSOLE ? 1 : 4;
/// when true, call measureVisibileText on next layout
protected bool _contentChanged = true;
protected bool _copyCurrentLineWhenNoSelection = true;
/// when true allows copy / cut whole current line if there is no selection
@property bool copyCurrentLineWhenNoSelection() { return _copyCurrentLineWhenNoSelection; }
@property EditWidgetBase copyCurrentLineWhenNoSelection(bool flg) { _copyCurrentLineWhenNoSelection = flg; return this; }
protected bool _showTabPositionMarks = false;
/// when true shows mark on tab positions in beginning of line
@property bool showTabPositionMarks() { return _showTabPositionMarks; }
@property EditWidgetBase showTabPositionMarks(bool flg) {
if (flg != _showTabPositionMarks) {
_showTabPositionMarks = flg;
invalidate();
}
return this;
}
/// Modified state change listener (e.g. content has been saved, or first time modified after save)
Signal!ModifiedStateListener modifiedStateChange;
/// Signal to emit when editor content is changed
Signal!EditableContentChangeListener contentChange;
/// Signal to emit when editor cursor position or Insert/Replace mode is changed.
Signal!EditorStateListener editorStateChange;
/// sets focus to this widget or suitable focusable child, returns previously focused widget
override Widget setFocus(FocusReason reason = FocusReason.Unspecified) {
Widget res = super.setFocus(reason);
if (focused) {
showSoftKeyboard();
handleEditorStateChange();
}
return res;
}
/// updates editorStateChange with recent position
protected void handleEditorStateChange() {
if (!editorStateChange.assigned)
return;
EditorStateInfo info;
if (visible) {
info.replaceMode = _replaceMode;
info.line = _caretPos.line + 1;
info.col = _caretPos.pos + 1;
if (_caretPos.line >= 0 && _caretPos.line < _content.length) {
dstring line = _content.line(_caretPos.line);
if (_caretPos.pos >= 0 && _caretPos.pos < line.length)
info.character = line[_caretPos.pos];
else
info.character = '\n';
}
}
editorStateChange(this, info);
}
/// override to support modification of client rect after change, e.g. apply offset
override protected void handleClientRectLayout(ref Rect rc) {
updateLeftPaneWidth();
rc.left += _leftPaneWidth;
}
/// override for multiline editors
protected int lineCount() {
return 1;
}
/// Override for EditBox
void wordWrapRefresh(){return;}
/// To hold _scrollpos.x toggling between normal and word wrap mode
int previousXScrollPos;
protected bool _wordWrap;
/// true if word wrap mode is set
@property bool wordWrap() {
return _wordWrap;
}
/// true if word wrap mode is set
@property EditWidgetBase wordWrap(bool v) {
_wordWrap = v;
//Horizontal scrollbar should not be visible in word wrap mode
if (v)
{
_hscrollbar.visibility(Visibility.Invisible);
previousXScrollPos = _scrollPos.x;
_scrollPos.x = 0;
wordWrapRefresh();
}
else
{
_hscrollbar.visibility(Visibility.Visible);
_scrollPos.x = previousXScrollPos;
}
invalidate();
return this;
}
/// Characters at which content is split for word wrap mode
dchar[] splitChars = [' ', '-', '\t'];
/// Divides up a string for word wrapping, sets info in _span
dstring[] wrapLine(dstring str, int lineNumber) {
FontRef font = font();
dstring[] words = explode(str, splitChars);
int curLineLength = 0;
dchar[] buildingStr;
dstring[] buildingStrArr;
WrapPoint[] wrapPoints;
int wrappedLineCount = 0;
int curLineWidth = 0;
int maxWidth = _clientRect.width;
for (int i = 0; i < words.length; i++)
{
dstring word = words[i];
if (curLineWidth + measureWrappedText(word) > maxWidth)
{
if (curLineWidth > 0)
{
buildingStrArr ~= to!dstring(buildingStr);
wrappedLineCount++;
wrapPoints ~= WrapPoint(curLineLength, curLineWidth);
curLineLength = 0;
curLineWidth = 0;
buildingStr = [];
}
while (measureWrappedText(word) > maxWidth)
{
//For when string still too long
int wrapPoint = findWrapPoint(word);
wrapPoints ~= WrapPoint(wrapPoint, measureWrappedText(word[0..wrapPoint]));
buildingStr ~= word[0 .. wrapPoint];
word = word[wrapPoint .. $];
buildingStrArr ~= to!dstring(buildingStr);
buildingStr = [];
wrappedLineCount++;
}
}
buildingStr ~= word;
curLineLength += to!int(word.length);
curLineWidth += measureWrappedText(word);
}
wrapPoints ~= WrapPoint(curLineLength, curLineWidth);
buildingStrArr ~= to!dstring(buildingStr);
_span ~= LineSpan(lineNumber, wrappedLineCount + 1, wrapPoints, buildingStrArr);
return buildingStrArr;
}
/// Divide (and conquer) text into words
dstring[] explode(dstring str, dchar[] splitChars)
{
dstring[] parts;
int startIndex = 0;
import std.string:indexOfAny;
while (true)
{
int index = to!int(str.indexOfAny(splitChars, startIndex));
if (index == -1)
{
parts ~= str[startIndex .. $];
//Log.d("Explode output: ", parts);
return parts;
}
dstring word = str[startIndex .. index];
dchar nextChar = (str[index .. index + 1])[0];
import std.ascii:isWhite;
if (isWhite(nextChar))
{
parts ~= word;
parts ~= to!dstring(nextChar);
}
else
{
parts ~= word ~ nextChar;
}
startIndex = index + 1;
}
}
/// information about line span into several lines - in word wrap mode
protected LineSpan[] _span;
protected LineSpan[] _spanCache;
/// Finds good visual wrapping point for string
int findWrapPoint(dstring text)
{
int maxWidth = _clientRect.width;
int wrapPoint = 0;
while (true)
{
if (measureWrappedText(text[0 .. wrapPoint]) < maxWidth)
{
wrapPoint++;
}
else
{
return wrapPoint;
}
}
}
/// Calls measureText for word wrap
int measureWrappedText(dstring text)
{
FontRef font = font();
int[] measuredWidths;
measuredWidths.length = text.length;
//DO NOT REMOVE THIS
int boggle = font.measureText(text, measuredWidths);
if (measuredWidths.length > 0)
return measuredWidths[$-1];
return 0;
}
/// Returns number of visible wraps up to a line (not including the first wrapLines themselves)
int wrapsUpTo(int line)
{
int sum;
lineSpanIterate(delegate(LineSpan curSpan)
{
if (curSpan.start < line)
sum += curSpan.len - 1;
});
return sum;
}
/// Returns LineSpan for line based on actual line number
LineSpan getSpan(int lineNumber)
{
LineSpan lineSpan = LineSpan(lineNumber, 0, [WrapPoint(0,0)], []);
lineSpanIterate(delegate(LineSpan curSpan)
{
if (curSpan.start == lineNumber)
lineSpan = curSpan;
});
return lineSpan;
}
/// Based on a TextPosition, finds which wrapLine it is on for its current line
int findWrapLine(TextPosition textPos)
{
int curWrapLine = 0;
int curPosition = textPos.pos;
LineSpan curSpan = getSpan(textPos.line);
while (true)
{
if (curWrapLine == curSpan.wrapPoints.length - 1)
return curWrapLine;
curPosition -= curSpan.wrapPoints[curWrapLine].wrapPos;
if (curPosition < 0)
{
return curWrapLine;
}
curWrapLine++;
}
}
/// Simple way of iterating through _span
void lineSpanIterate(void delegate(LineSpan curSpan) iterator)
{
//TODO: Rename iterator to iteration?
foreach (currentSpan; _span)
iterator(currentSpan);
}
/// override to add custom items on left panel
protected void updateLeftPaneWidth() {
import std.conv : to;
_iconsWidth = _showIcons ? _iconsPaneWidth : 0;
_foldingWidth = _showFolding ? _foldingPaneWidth : 0;
_modificationMarksWidth = _showModificationMarks && (BACKEND_GUI || !_showLineNumbers) ? _modificationMarksPaneWidth : 0;
_lineNumbersWidth = 0;
if (_showLineNumbers) {
dchar[] s = to!(dchar[])(lineCount + 1);
foreach(ref ch; s)
ch = '9';
FontRef fnt = font;
Point sz = fnt.textSize(cast(immutable)s);
_lineNumbersWidth = sz.x;
}
_leftPaneWidth = _lineNumbersWidth + _modificationMarksWidth + _foldingWidth + _iconsWidth;
if (_leftPaneWidth)
_leftPaneWidth += WIDGET_STYLE_CONSOLE ? 1 : 3;
}
protected void drawLeftPaneFolding(DrawBuf buf, Rect rc, int line) {
buf.fillRect(rc, _leftPaneBackgroundColor2);
}
protected void drawLeftPaneIcon(DrawBuf buf, Rect rc, LineIcon icon) {
if (!icon)
return;
if (icon.type == LineIconType.error) {
buf.fillRect(rc, _colorIconError);
} else if (icon.type == LineIconType.bookmark) {
int dh = rc.height / 4;
rc.top += dh;
rc.bottom -= dh;
buf.fillRect(rc, _colorIconBookmark);
} else if (icon.type == LineIconType.breakpoint) {
if (rc.height > rc.width) {
int delta = rc.height - rc.width;
rc.top += delta / 2;
rc.bottom -= (delta + 1) / 2;
} else {
int delta = rc.width - rc.height;
rc.left += delta / 2;
rc.right -= (delta + 1) / 2;
}
int dh = rc.height / 5;
rc.top += dh;
rc.bottom -= dh;
int dw = rc.width / 5;
rc.left += dw;
rc.right -= dw;
buf.fillRect(rc, _colorIconBreakpoint);
}
}
protected void drawLeftPaneIcons(DrawBuf buf, Rect rc, int line) {
buf.fillRect(rc, _leftPaneBackgroundColor3);
drawLeftPaneIcon(buf, rc, content.lineIcons.findByLineAndType(line, LineIconType.error));
drawLeftPaneIcon(buf, rc, content.lineIcons.findByLineAndType(line, LineIconType.bookmark));
drawLeftPaneIcon(buf, rc, content.lineIcons.findByLineAndType(line, LineIconType.breakpoint));
}
protected void drawLeftPaneModificationMarks(DrawBuf buf, Rect rc, int line) {
if (line >= 0 && line < content.length) {
EditStateMark m = content.editMark(line);
if (m == EditStateMark.changed) {
// modified, not saved
buf.fillRect(rc, 0xFFD040);
} else if (m == EditStateMark.saved) {
// modified, saved
buf.fillRect(rc, 0x20C020);
}
}
}
protected void drawLeftPaneLineNumbers(DrawBuf buf, Rect rc, int line) {
import std.conv : to;
uint bgcolor = _leftPaneLineNumberBackgroundColor;
if (line == _caretPos.line && !isFullyTransparentColor(_leftPaneLineNumberBackgroundColorCurrentLine))
bgcolor = _leftPaneLineNumberBackgroundColorCurrentLine;
buf.fillRect(rc, bgcolor);
if (line < 0)
return;
dstring s = to!dstring(line + 1);
FontRef fnt = font;
Point sz = fnt.textSize(s);
int x = rc.right - sz.x;
int y = rc.top + (rc.height - sz.y) / 2;
uint color = _leftPaneLineNumberColor;
if (line == _caretPos.line && !isFullyTransparentColor(_leftPaneLineNumberColorCurrentLine))
color = _leftPaneLineNumberColorCurrentLine;
if (line >= 0 && line < content.length) {
EditStateMark m = content.editMark(line);
if (m == EditStateMark.changed) {
// modified, not saved
color = _leftPaneLineNumberColorEdited;
} else if (m == EditStateMark.saved) {
// modified, saved
color = _leftPaneLineNumberColorSaved;
}
}
fnt.drawText(buf, x, y, s, color);
}
protected bool onLeftPaneMouseClick(MouseEvent event) {
return false;
}
protected bool handleLeftPaneFoldingMouseClick(MouseEvent event, Rect rc, int line) {
return true;
}
protected bool handleLeftPaneModificationMarksMouseClick(MouseEvent event, Rect rc, int line) {
return true;
}
protected bool handleLeftPaneLineNumbersMouseClick(MouseEvent event, Rect rc, int line) {
return true;
}
protected MenuItem getLeftPaneIconsPopupMenu(int line) {
return null;
}
protected bool handleLeftPaneIconsMouseClick(MouseEvent event, Rect rc, int line) {
if (event.button == MouseButton.Right) {
MenuItem menu = getLeftPaneIconsPopupMenu(line);
if (menu) {
if (menu.openingSubmenu.assigned)
if (!menu.openingSubmenu(_popupMenu))
return true;
menu.updateActionState(this);
PopupMenu popupMenu = new PopupMenu(menu);
popupMenu.menuItemAction = this;
PopupWidget popup = window.showPopup(popupMenu, this, PopupAlign.Point | PopupAlign.Right, event.x, event.y);
popup.flags = PopupFlags.CloseOnClickOutside;
}
return true;
}
return true;
}
protected bool handleLeftPaneMouseClick(MouseEvent event, Rect rc, int line) {
rc.right -= 3;
if (_foldingWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _foldingWidth;
if (event.x >= rc2.left && event.x < rc2.right)
return handleLeftPaneFoldingMouseClick(event, rc2, line);
}
if (_modificationMarksWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _modificationMarksWidth;
if (event.x >= rc2.left && event.x < rc2.right)
return handleLeftPaneModificationMarksMouseClick(event, rc2, line);
}
if (_lineNumbersWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _lineNumbersWidth;
if (event.x >= rc2.left && event.x < rc2.right)
return handleLeftPaneLineNumbersMouseClick(event, rc2, line);
}
if (_iconsWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _iconsWidth;
if (event.x >= rc2.left && event.x < rc2.right)
return handleLeftPaneIconsMouseClick(event, rc2, line);
}
return true;
}
protected void drawLeftPane(DrawBuf buf, Rect rc, int line) {
// override for custom drawn left pane
buf.fillRect(rc, _leftPaneBackgroundColor);
//buf.fillRect(Rect(rc.right - 2, rc.top, rc.right - 1, rc.bottom), _leftPaneBackgroundColor2);
//buf.fillRect(Rect(rc.right - 1, rc.top, rc.right - 0, rc.bottom), _leftPaneBackgroundColor3);
rc.right -= WIDGET_STYLE_CONSOLE ? 1 : 3;
if (_foldingWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _foldingWidth;
drawLeftPaneFolding(buf, rc2, line);
}
if (_modificationMarksWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _modificationMarksWidth;
drawLeftPaneModificationMarks(buf, rc2, line);
}
if (_lineNumbersWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _lineNumbersWidth;
drawLeftPaneLineNumbers(buf, rc2, line);
}
if (_iconsWidth) {
Rect rc2 = rc;
rc.right = rc2.left = rc2.right - _iconsWidth;
drawLeftPaneIcons(buf, rc2, line);
}
}
this(string ID, ScrollBarMode hscrollbarMode = ScrollBarMode.Visible, ScrollBarMode vscrollbarMode = ScrollBarMode.Visible) {
super(ID, hscrollbarMode, vscrollbarMode);
focusable = true;
acceleratorMap.add( [
new Action(EditorActions.Up, KeyCode.UP, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectUp, KeyCode.UP, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectUp, KeyCode.UP, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.Down, KeyCode.DOWN, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectDown, KeyCode.DOWN, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectDown, KeyCode.DOWN, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.Left, KeyCode.LEFT, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectLeft, KeyCode.LEFT, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.Right, KeyCode.RIGHT, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectRight, KeyCode.RIGHT, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.WordLeft, KeyCode.LEFT, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectWordLeft, KeyCode.LEFT, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.WordRight, KeyCode.RIGHT, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectWordRight, KeyCode.RIGHT, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.PageUp, KeyCode.PAGEUP, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectPageUp, KeyCode.PAGEUP, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.PageDown, KeyCode.PAGEDOWN, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectPageDown, KeyCode.PAGEDOWN, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.PageBegin, KeyCode.PAGEUP, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectPageBegin, KeyCode.PAGEUP, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.PageEnd, KeyCode.PAGEDOWN, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectPageEnd, KeyCode.PAGEDOWN, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.LineBegin, KeyCode.HOME, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectLineBegin, KeyCode.HOME, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.LineEnd, KeyCode.END, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectLineEnd, KeyCode.END, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.DocumentBegin, KeyCode.HOME, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectDocumentBegin, KeyCode.HOME, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.DocumentEnd, KeyCode.END, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.SelectDocumentEnd, KeyCode.END, KeyFlag.Control | KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.ScrollLineUp, KeyCode.UP, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.ScrollLineDown, KeyCode.DOWN, KeyFlag.Control, ActionStateUpdateFlag.never),
// Backspace/Del
new Action(EditorActions.DelPrevChar, KeyCode.BACK, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.DelNextChar, KeyCode.DEL, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.DelPrevWord, KeyCode.BACK, KeyFlag.Control, ActionStateUpdateFlag.never),
new Action(EditorActions.DelNextWord, KeyCode.DEL, KeyFlag.Control, ActionStateUpdateFlag.never),
// Copy/Paste
new Action(EditorActions.Copy, KeyCode.KEY_C, KeyFlag.Control),
new Action(EditorActions.Copy, KeyCode.KEY_C, KeyFlag.Control | KeyFlag.Shift),
new Action(EditorActions.Copy, KeyCode.INS, KeyFlag.Control),
new Action(EditorActions.Cut, KeyCode.KEY_X, KeyFlag.Control),
new Action(EditorActions.Cut, KeyCode.KEY_X, KeyFlag.Control | KeyFlag.Shift),
new Action(EditorActions.Cut, KeyCode.DEL, KeyFlag.Shift),
new Action(EditorActions.Paste, KeyCode.KEY_V, KeyFlag.Control),
new Action(EditorActions.Paste, KeyCode.KEY_V, KeyFlag.Control | KeyFlag.Shift),
new Action(EditorActions.Paste, KeyCode.INS, KeyFlag.Shift),
// Undo/Redo
new Action(EditorActions.Undo, KeyCode.KEY_Z, KeyFlag.Control),
new Action(EditorActions.Redo, KeyCode.KEY_Y, KeyFlag.Control),
new Action(EditorActions.Redo, KeyCode.KEY_Z, KeyFlag.Control | KeyFlag.Shift),
new Action(EditorActions.Tab, KeyCode.TAB, 0, ActionStateUpdateFlag.never),
new Action(EditorActions.BackTab, KeyCode.TAB, KeyFlag.Shift, ActionStateUpdateFlag.never),
new Action(EditorActions.Find, KeyCode.KEY_F, KeyFlag.Control),
new Action(EditorActions.Replace, KeyCode.KEY_H, KeyFlag.Control),
]);
acceleratorMap.add(STD_EDITOR_ACTIONS);
acceleratorMap.add([ACTION_EDITOR_FIND_NEXT, ACTION_EDITOR_FIND_PREV]);
}
///
override bool onMenuItemAction(const Action action) {
return dispatchAction(action);
}
/// returns true if widget can show popup (e.g. by mouse right click at point x,y)
override bool canShowPopupMenu(int x, int y) {
if (_popupMenu is null)
return false;
if (_popupMenu.openingSubmenu.assigned)
if (!_popupMenu.openingSubmenu(_popupMenu))
return false;
return true;
}
/// returns true if widget is focusable and visible and enabled
override @property bool canFocus() {
// allow to focus even if not enabled
return focusable && visible;
}
/// override to change popup menu items state
override bool isActionEnabled(const Action action) {
switch (action.id) with(EditorActions)
{
case Tab:
case BackTab:
case Indent:
case Unindent:
return enabled;
case Copy:
return _copyCurrentLineWhenNoSelection || !_selectionRange.empty;
case Cut:
return enabled && (_copyCurrentLineWhenNoSelection || !_selectionRange.empty);
case Paste:
return enabled && Platform.instance.hasClipboardText();
case Undo:
return enabled && _content.hasUndo;
case Redo:
return enabled && _content.hasRedo;
case ToggleBookmark:
return _content.multiline;
case GoToNextBookmark:
return _content.multiline && _content.lineIcons.hasBookmarks;
case GoToPreviousBookmark:
return _content.multiline && _content.lineIcons.hasBookmarks;
case Replace:
return _content.multiline && !readOnly;
case Find:
case FindNext:
case FindPrev:
return _content.multiline;
default:
return super.isActionEnabled(action);
}
}
/// shows popup at (x,y)
override void showPopupMenu(int x, int y) {
/// if preparation signal handler assigned, call it; don't show popup if false is returned from handler
if (_popupMenu.openingSubmenu.assigned)
if (!_popupMenu.openingSubmenu(_popupMenu))
return;
_popupMenu.updateActionState(this);
PopupMenu popupMenu = new PopupMenu(_popupMenu);
popupMenu.menuItemAction = this;
PopupWidget popup = window.showPopup(popupMenu, this, PopupAlign.Point | PopupAlign.Right, x, y);
popup.flags = PopupFlags.CloseOnClickOutside;
}
void onPopupMenuItem(MenuItem item) {
// TODO
}
/// returns mouse cursor type for widget
override uint getCursorType(int x, int y) {
return x < _pos.left + _leftPaneWidth ? CursorType.Arrow : CursorType.IBeam;
}
/// set bool property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setBoolProperty", "bool",
"wantTabs", "showIcons", "showFolding", "showModificationMarks", "showLineNumbers", "readOnly", "replaceMode", "useSpacesForTabs", "copyCurrentLineWhenNoSelection", "showTabPositionMarks"));
/// set int property value, for ML loaders
mixin(generatePropertySettersMethodOverride("setIntProperty", "int",
"tabSize"));
/// when true, Tab / Shift+Tab presses are processed internally in widget (e.g. insert tab character) instead of focus change navigation.
@property bool wantTabs() {
return _wantTabs;
}
/// ditto
@property EditWidgetBase wantTabs(bool wantTabs) {
_wantTabs = wantTabs;
return this;
}
/// when true, show icons like bookmarks or breakpoints at the left
@property bool showIcons() {
return _showIcons;
}
/// when true, show icons like bookmarks or breakpoints at the left
@property EditWidgetBase showIcons(bool flg) {
if (_showIcons != flg) {
_showIcons = flg;
updateLeftPaneWidth();
requestLayout();
}
return this;
}
/// when true, show folding controls at the left
@property bool showFolding() {
return _showFolding;
}
/// when true, show folding controls at the left
@property EditWidgetBase showFolding(bool flg) {
if (_showFolding != flg) {
_showFolding = flg;
updateLeftPaneWidth();
requestLayout();
}
return this;
}
/// when true, show modification marks for lines (whether line is unchanged/modified/modified_saved)
@property bool showModificationMarks() {
return _showModificationMarks;
}
/// when true, show modification marks for lines (whether line is unchanged/modified/modified_saved)
@property EditWidgetBase showModificationMarks(bool flg) {
if (_showModificationMarks != flg) {
_showModificationMarks = flg;
updateLeftPaneWidth();
requestLayout();
}
return this;
}
/// when true, line numbers are shown
@property bool showLineNumbers() {
return _showLineNumbers;