1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 char * strchrmax(const char * s, int c, int max)
19 for(i = 0; i < max && (ch = s[i]); i++)
25 public class SyntaxColorScheme
29 Color charLiteralColor;
30 Color stringLiteralColor;
31 Color preprocessorColor;
33 private Array<Color> keywordColors { };
35 public property Container<Color> keywordColors
37 set { keywordColors.Copy((void *)value); }
38 get { return keywordColors; }
46 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
48 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
50 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
52 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
53 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
54 #define UTF8_GET_CHAR(string, numBytes) \
58 byte b = (string)[0]; \
61 numBytes = b ? 1 : 0; \
75 if(b & 0x08) { numBytes = 0; } \
84 for(i = 0; i<numBytes; i++) \
87 ch |= (b = (string)[i]) & mask; \
89 if(i > 1 && (!(b & 0x80) || (b & 0x40))) \
96 ch > 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF) || \
97 (ch < 0x80 && numBytes > 1) || \
98 (ch < 0x800 && numBytes > 2) || \
99 (ch < 0x10000 && numBytes > 3))\
109 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
110 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
113 bool inMultiLineComment:1, inPrep:1, escaped:1, continuedSingleLineComment:1, wasInMultiLine:1, continuedString:1, continuedQuotes:1;
115 bool recomputeSyntax:1;
116 bool cursorFollowsView:1;
118 // bool lineNumbers:1;
123 void UnregisterClass_EditBox()
126 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
135 extern int __ecereVMethodID_class_OnFree;
138 static class ArrayImpl
145 public class OldArray
150 void ** array = (void **)((ArrayImpl)this).array;
151 if(type.type == normalClass || type.type == noHeadClass)
153 for(c = 0; c<size; c++)
154 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
156 // TODO: Call OnFree for structClass
157 delete ((ArrayImpl)this).array;
166 if(((ArrayImpl)this).array)
170 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
173 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
182 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
189 public class UndoAction : struct
192 subclass(UndoAction) type;
195 virtual void Undo(void * data) { type.Undo(this, data); }
196 virtual void Redo(void * data) { type.Redo(this, data); }
198 virtual void Print(void * data) { type.Print(this, data); }
202 if(((Class)type).Destructor)
203 ((void (*)(void *))((Class)type).Destructor)(this);
207 public class UndoBuffer
209 Array<UndoAction> actions { size = 8 };
228 bool continued = true;
229 while(curAction > 0 && continued)
231 UndoAction action = actions[--curAction];
235 /*Print("Undoing: ");
236 action.Print(data);*/
241 continued = curAction > 0 && actions[curAction-1].continued;
247 bool continued = true;
248 while(curAction < count && continued)
250 UndoAction action = actions[curAction];
251 continued = action.continued;
257 /*Print("Redoing: ");
258 action.Print(data);*/
267 void Record(UndoAction action)
269 if(!dontRecord && !insideRedo)
271 if(curAction < count)
274 for(c = curAction; c < count; c++)
280 if(count >= actions.size)
281 actions.size += actions.size / 2;
284 /*Print("Recording: ");
285 action.Print(data);*/
289 if(!firstEvent && count > 0)
290 actions[count-1].continued = true;
293 actions[count++] = action;
296 if(actions.size > count + count / 2 && count + count / 2 >= 8)
297 actions.size = count + count / 2;
304 static class AddCharAction : UndoAction
308 int addedSpaces, addedTabs;
309 type = class(AddCharAction);
311 void Undo(EditBox editBox)
313 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
315 if(addedTabs || addedSpaces)
316 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
317 editBox.UpdateDirty();
320 void Redo(EditBox editBox)
322 editBox.GoToPosition(null, y, x);
324 editBox.UpdateDirty();
327 void Print(EditBox editBox)
329 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
334 static class AddTextAction : UndoAction
338 int addedSpaces, addedTabs;
339 type = class(AddTextAction);
342 void Print(EditBox editBox)
344 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
352 void Undo(EditBox editBox)
357 editBox.GoToPosition(null, y1, x1);
359 l2 = editBox.lines.first;
360 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
362 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
363 if(addedTabs || addedSpaces)
364 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
366 editBox.SetViewToCursor(true);
370 void Redo(EditBox editBox)
372 editBox.GoToPosition(null, y1, x1);
373 editBox.PutS(string);
377 static class DelTextAction : UndoAction
381 bool placeAfter, noHighlight;
383 type = class(DelTextAction);
386 void Print(EditBox editBox)
388 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
391 void Undo(EditBox editBox)
393 editBox.GoToPosition(null, y1, x1);
394 editBox.PutS(string);
398 editBox.GoToPosition(null, y1, x1);
404 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
405 //editBox.SetViewToCursor(true);
408 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
417 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
418 //editBox.SetViewToCursor(true);
421 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
425 void Redo(EditBox editBox)
430 editBox.GoToPosition(null, y1, x1);
433 l2 = editBox.lines.first;
434 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
436 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
437 editBox.SetViewToCursor(true);
447 static class ReplaceTextAction : UndoAction
449 int y1, x1, y2, x2, y3, x3;
453 int addedSpaces, addedTabs;
455 type = class(ReplaceTextAction);
458 void Print(EditBox editBox)
460 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
463 void Undo(EditBox editBox)
468 editBox.GoToPosition(null, y1, x1);
470 l3 = editBox.lines.first;
471 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
473 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
475 editBox.PutS(oldString);
476 if(addedSpaces || addedTabs)
477 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
479 //editBox.PutS(oldString);
482 editBox.GoToPosition(null, y1, x1);
485 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
491 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
495 void Redo(EditBox editBox)
500 editBox.GoToPosition(null, y1, x1);
502 l2 = editBox.lines.first;
503 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
505 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
507 editBox.PutS(newString);
517 static class MoveTextAction : UndoAction
519 int fy1, fx1, fy2, fx2;
521 type = class(MoveTextAction);
523 void Undo(EditBox editBox)
528 void Redo(EditBox editBox)
534 public class EditLine : struct
546 // Only works for single line edit for now...
547 EditBox editBox = this.editBox;
551 get { return this ? buffer : null; }
553 property EditLine prev { get { return this ? prev : null; } };
554 property EditLine next { get { return this ? next : null; } };
555 property int count { get { return this ? count : 0; } };
562 // This makes sure the buffer always contains at least count characters
563 // Keeps a count/2 pad for avoiding always reallocating memory.
564 bool AdjustBuffer(int count)
572 newSize = (count + (count >> 1));
574 // Shrink down the buffer
577 buffer = new char[newSize];
578 if(!buffer) return false;
582 CopyBytes(buffer, this.buffer, count);
585 this.buffer = buffer;
589 // Increase the buffer
590 else if(size < count)
592 buffer = new char[newSize];
593 if(!buffer) return false;
597 CopyBytes(buffer, this.buffer, this.count + 1); // size);
600 this.buffer = buffer;
609 public struct BufferLocation
614 void AdjustDelete(BufferLocation start, BufferLocation end)
616 // Location is before, nothing to do
617 if(y < start.y || (y == start.y && x < start.x))
619 // Location is inside deleted bytes, point to the start
620 if((y >= start.y && (y > start.y || x >= start.x)) &&
621 (y >= end.y && (y > end.y || x >= end.x)))
626 // Location is on another line
628 y -= end.y - start.y;
629 // Location is the last touched line
636 //if(start.line == end.line)
637 x -= end.x - start.x;
646 // Assuming no carriage return before first character ???? fixed?
647 void AdjustAdd(BufferLocation start, BufferLocation end)
649 int numLines = end.y - start.y;
656 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
659 for(c = 0, line = start.line; c<numLines; c++)
662 //x += numLines ? end.x : (end.x - start.x);
663 x += end.x - start.x;
670 public enum EditBoxFindResult { notFound, found, wrapped };
672 static char * keyWords1[] =
675 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
676 "void", "char","int","float","double","signed","unsigned","static", "extern", "struct", "union", "typedef","enum",
678 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
679 "__attribute__", "__stdcall", "_stdcall",
680 "__declspec", "goto",
681 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
682 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
683 "__restrict__", "__restrict", "restrict",
686 "class", "private", "public",
688 "delete", "new", "new0", "renew", "renew0", "define",
691 "dllexport", "dllimport", "stdcall",
692 "subclass", "__on_register_module", "namespace", "using",
693 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
694 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
695 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
698 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
701 "this", "true", "false", "null", "value",
710 static char * keyWords2[] =
712 "defined", "warning", null
715 static char ** keyWords[] = { keyWords1, keyWords2 };
716 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
717 //static int * keyLen[NUM_KEYWORD_GROUPS];
718 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
720 static char searchString[1025], replaceString[1025];
721 static bool matchCase = false, wholeWord = false, searchUp = false;
723 static GoToDialog goToDialog
725 autoCreate = false, isModal = true, text = $"Go To"
728 public class EditBox : CommonControl
730 class_property(icon) = "<:ecere>controls/editBox.png";
734 virtual bool Window::NotifyModified(EditBox editBox);
735 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
736 virtual void Window::NotifyUpdate(EditBox editBox);
737 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
738 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
739 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
741 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
742 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
743 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
745 virtual bool Window::NotifyUnsetModified(EditBox editBox);
747 // Why was this commented out?
748 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
749 // and the scrollArea property isn't compared properly either.
753 if(font) ComputeFont();
754 SetInitSize(initSize);
760 style.vScroll = true;
766 style.hScroll = true;
770 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
771 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
772 property bool readOnly
774 property_category $"Behavior"
777 style.readOnly = value;
778 itemEditCut.disabled = value || !selection;
779 itemEditDelete.disabled = value || !selection;
780 itemEditPaste.disabled = value;
782 get { return style.readOnly; }
784 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
785 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
786 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
787 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
788 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
789 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
790 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
791 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
792 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
793 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
794 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
795 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
796 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
797 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
798 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
799 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
800 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
801 property int numLines { get { return this ? lineCount : 0; } };
802 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
803 property int column { get { return col; } }; // TODO: Add Set
804 property int charPos { get { return x; } }; // TODO: Add Set
805 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
806 property EditLine lastLine { get { return lines.last; } };
807 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
808 property char * contents
810 property_category $"Data"
815 undoBuffer.dontRecord++;
817 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
820 //SetViewToCursor(true);
823 undoBuffer.dontRecord--;
829 char * buffer = null;
832 /* Can't implement this right now because of memory leak... Need string reference counting...
839 for(line = lines.first; line; line = line.next)
840 len += strlen(line.buffer);
842 buffer = new char[len+1];
844 for(line = lines.first; line; line = line.next)
846 int lineLen = strlen(line.buffer);
847 memcpy(buffer + len, line.buffer, lineLen);
853 buffer = this.line ? this.line.buffer : null;
858 property bool overwrite { get { return overwrite; } };
859 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
861 property char * multiLineContents
865 char * buffer = null;
872 for(line = lines.first; line; line = line.next)
873 len += strlen(line.buffer)+1;
875 buffer = new char[len+1];
877 for(line = lines.first; line; line = line.next)
879 int lineLen = strlen(line.buffer);
880 memcpy(buffer + len, line.buffer, lineLen);
882 if(line.next) buffer[len++] = '\n';
895 return this.line.buffer;
900 void SetLineText(char * text)
904 EditLine_SetText(this.line, text);
908 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
909 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
910 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
911 property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };
913 // selectionStart.line, selectionStart.column (With Set)
914 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
928 // Position of Caret (Not necessarily displayed position)
931 // Position of beginning of block (Block ends at (x,y))
933 // line is line at carret, selLine is line at beginning of block
934 EditLine line, selLine, dropLine;
935 // Mouse selection Moving virtual caret
940 // ViewX is x offset in pixels, ViewY is y offset in lines
942 // viewLine is first displayed line
945 // start and end of area to redraw
948 // MaxLine is the longest line
950 // MaxLength is the longest line's length
953 // MouseSelect is true once button is pressed, overwrite is true if mode is on
954 bool mouseSelect, mouseMove, overwrite, wordSelect;
955 // Timer is used for mouse selection scrolling
958 window = this, delay = 0.1;
967 OnMouseMove(mouseX, mouseY, -1);
972 // (mouseX,mouseY) is the position of the mouse in the edit box
977 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
980 bool rightButtonDown;
983 UndoBuffer undoBuffer { data = this };
985 ColorAlpha selectionColor, selectionText;
986 SyntaxColorScheme colorScheme { };
991 Menu editMenu { menu, $"Edit", e };
994 editMenu, $"Cut\tCtrl+X", t, disabled = true;
996 bool NotifySelect(MenuItem item, Modifiers mods)
998 if(!(style.readOnly))
1003 MenuItem itemEditCopy
1005 editMenu, $"Copy\tCtrl+C", c, disabled = true;
1007 bool NotifySelect(MenuItem item, Modifiers mods)
1013 MenuItem itemEditPaste
1015 editMenu, $"Paste\tCtrl+V", p;
1017 bool NotifySelect(MenuItem item, Modifiers mods)
1019 if(!(style.readOnly))
1024 MenuItem itemEditDelete
1026 editMenu, $"Delete\tDel", d, disabled = true;
1028 bool NotifySelect(MenuItem item, Modifiers mods)
1030 if(!(style.readOnly))
1035 MenuDivider { editMenu };
1036 MenuItem itemEditSelectAll
1038 editMenu, $"Select All\tCtrl+A", a;
1040 bool NotifySelect(MenuItem item, Modifiers mods)
1046 MenuDivider { editMenu };
1047 MenuItem itemEditUndo
1049 editMenu, $"Undo\tCtrl+Z", u;
1052 bool NotifySelect(MenuItem item, Modifiers mods)
1058 MenuItem itemEditRedo
1060 editMenu, $"Redo\tCtrl+Y", o;
1063 bool NotifySelect(MenuItem item, Modifiers mods)
1069 MenuDivider { editMenu };
1072 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1074 bool NotifySelect(MenuItem item, Modifiers mods)
1077 Find(searchString, wholeWord, matchCase, false);
1079 itemEditFind.NotifySelect(this, item, mods);
1085 editMenu, $"Find Next\tF3", n, f3;
1087 bool NotifySelect(MenuItem item, Modifiers mods)
1090 Find(searchString, wholeWord, matchCase, true);
1092 itemEditFind.NotifySelect(this, item, mods);
1096 MenuItem itemEditFind
1098 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1100 bool NotifySelect(MenuItem item, Modifiers mods)
1104 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1105 searchUp = searchUp;
1108 // Fix dialog from above shouldn't be visible
1109 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1110 void NotifyDestroyed(Window window, DialogResult result)
1112 FindDialog dialog = (FindDialog) window;
1113 searchUp = dialog.searchUp;
1114 strcpy(searchString, dialog.searchString);
1115 matchCase = dialog.matchCase;
1116 wholeWord = dialog.wholeWord;
1125 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1127 bool NotifySelect(MenuItem item, Modifiers mods)
1129 ReplaceDialog dialog
1133 searchString = searchString,
1134 replaceString = replaceString,
1135 matchCase = matchCase,
1136 wholeWord = wholeWord,
1140 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1141 void NotifyDestroyed(Window window, DialogResult result)
1143 ReplaceDialog dialog = (ReplaceDialog)window;
1144 char * replace = dialog.replaceString;
1146 strcpy(replaceString, replace);
1147 strcpy(searchString, dialog.searchString);
1148 matchCase = dialog.matchCase;
1149 wholeWord = dialog.wholeWord;
1156 MenuDivider { editMenu };
1159 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1161 bool NotifySelect(MenuItem item, Modifiers mods)
1163 goToDialog.editBox = this;
1164 goToDialog.master = master;
1165 goToDialog.Create();
1169 MenuDivider { editMenu };
1170 MenuItem itemEditInsertTab
1172 editMenu, $"Insert Tabs", i, checkable = true;
1174 bool NotifySelect(MenuItem item, Modifiers mods)
1176 style.useTab = item.checked;
1182 snapVertScroll = true;
1183 snapHorzScroll = true;
1187 static bool syntaxInit = false;
1192 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1194 for(c = 0; keyWords[g][c]; c++);
1195 //keyLen[g] = new int[c];
1196 for(c = 0; keyWords[g][c]; c++)
1198 keyLen[g][c] = strlen(keyWords[g][c]);
1202 colorScheme.commentColor = dimGray;
1203 colorScheme.charLiteralColor = crimson;
1204 colorScheme.stringLiteralColor = crimson;
1205 colorScheme.preprocessorColor = green;
1206 colorScheme.numberColor = teal;
1207 colorScheme.keywordColors = [ blue, blue ];
1210 FontExtent = Display::FontExtent;
1212 lines.offset = (uint)&((EditLine)0).prev;
1214 style = EditBoxBits { hScroll = true };
1216 // cursor = guiApp.GetCursor(IBeam);
1218 // Default Properties
1220 maxLineSize = 1024;*/
1223 maxLineSize = MAXINT;
1228 mouseSelect = this.mouseMove = false;
1231 x = selX = selY = 0;
1234 line = selLine = null;
1240 endY = clientSize.h;
1243 // Default space to 8 in case window is not constructed
1246 undoBuffer.dontRecord++;
1248 undoBuffer.dontRecord--;
1250 viewLine = lines.first;
1251 style.recomputeSyntax = true;
1257 UpdateCaretPosition(true);
1263 lines.Free(EditLine::Free);
1266 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1268 int count = wc - *renderStart;
1271 if(y + space.h >= box.top && y <= box.bottom)
1277 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1278 surface.TextFont(font);
1279 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1280 if(*x + w + XOFFSET > 0)
1281 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1286 w = numSpaces; // * space.w;
1287 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1288 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1289 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1297 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1298 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1304 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1308 *foreground = (*selected) ? selectionForeground : textColor;
1309 *background = selectionBackground;
1310 *opacity = *selected;
1317 if(this.overwrite && active)
1319 if((style.stuckCaret && wc == line.count && !line.next) ||
1320 (!mouseMove && line == this.line && wc == editX))
1329 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1333 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1334 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1335 bool inString = false;
1336 bool inQuotes = false;
1337 bool inPrep = reset ? false : style.inPrep;
1338 bool inSingleLineComment = false;
1339 bool escaped = reset ? false : style.escaped;
1340 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1341 bool continuedString = reset ? false : style.continuedString;
1342 bool continuedQuotes = reset ? false : style.continuedQuotes;
1344 EditLine line = reset ? lines.first : firstLine;
1345 // int maxBackUp = 1000, c;
1346 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1347 for(; line != viewLine; line = line.next)
1349 char * text = line.buffer;
1352 bool lastWasStar = false;
1353 bool firstWord = true;
1354 if(!escaped) inPrep = false;
1355 inSingleLineComment = continuedSingleLineComment;
1357 inString = continuedString;
1358 inQuotes = continuedQuotes;
1361 for(c = 0; (ch = text[c]); c++)
1363 bool wasEscaped = escaped;
1364 bool backLastWasStar = lastWasStar;
1365 bool backWasInMultiLine = wasInMultiLine;
1367 lastWasStar = false;
1368 wasInMultiLine = inMultiLineComment;
1371 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1373 if(text[c+1] == '/')
1375 inSingleLineComment = true;
1377 else if(text[c+1] == '*')
1379 inMultiLineComment = true;
1382 else if(backLastWasStar)
1383 inMultiLineComment = false;
1387 if(backWasInMultiLine) lastWasStar = true;
1389 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1391 if(inString && !wasEscaped)
1400 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1402 if(inQuotes && !wasEscaped)
1414 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1421 else if(ch != ' ' && ch != '\t')
1424 if(line.count && line.text[line.count - 1] == '\\')
1426 continuedSingleLineComment = inSingleLineComment;
1427 continuedString = inString;
1428 continuedQuotes = inQuotes;
1432 continuedSingleLineComment = false;
1433 continuedString = false;
1434 continuedQuotes = false;
1438 style.continuedSingleLineComment = continuedSingleLineComment;
1439 style.continuedString = continuedString;
1440 style.continuedQuotes = continuedQuotes;
1441 style.inMultiLineComment = inMultiLineComment;
1442 style.wasInMultiLine = wasInMultiLine;
1443 style.inPrep = inPrep;
1444 style.escaped = escaped;
1448 /*void OnDrawOverChildren(Surface surface)
1450 if(style.lineNumbers)
1452 int currentLineNumber = this.viewY + 1;
1455 for(i = 0; i * space.h < box.height; i++)
1457 // ********* LINE NUMBERING *********
1458 surface.SetForeground(Color{60, 60, 60});
1459 //Highlight current line
1460 if(this.caretY / space.h == currentLineNumber - 1)
1461 surface.SetBackground(Color{220, 220, 220});
1463 surface.SetBackground(Color{230, 230, 230});
1464 surface.textOpacity = true;
1466 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1467 if(currentLineNumber > this.lineCount)
1468 surface.WriteText(0,i*space.h+1," ",6);
1470 surface.WriteText(0,i*space.h+1,lineText,6);
1472 currentLineNumber++;
1477 void OnRedraw(Surface surface)
1481 bool selected = false, selection = true;
1483 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1484 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1485 Color defaultTextColor = property::foreground;
1488 int maxW = clientSize.w;
1490 Color foreground, background;
1493 // Overwrite Caret Stuff
1494 bool overWrite = false;
1495 int overWriteX, overWriteY;
1498 // ****** SYNTAX STATES ******
1499 bool inMultiLineComment = style.inMultiLineComment;
1500 bool inString = false;
1501 bool inQuotes = false;
1502 bool inPrep = style.inPrep;
1503 bool inSingleLineComment = false;
1504 bool escaped = style.escaped;
1505 bool continuedSingleLineComment = style.continuedSingleLineComment;
1506 bool continuedString = style.continuedString;
1507 bool continuedQuotes = style.continuedQuotes;
1508 bool wasInMultiLine = style.wasInMultiLine;
1509 // ****** ************* ******
1512 defaultTextColor = Color { 85, 85, 85 };
1513 textColor = defaultTextColor;
1516 Abs(selectionBackground.r - property::background.r) +
1517 Abs(selectionBackground.g - property::background.g) +
1518 Abs(selectionBackground.b - property::background.b) < 92)
1520 selectionBackground = formColor;
1521 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1524 surface.TextFont(this.font);
1526 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1527 surface.Area(0,0,MAXINT,MAXINT);
1532 surface.SetBackground(formColor);
1533 surface.Area(0,0,clientSize.w, clientSize.h);
1536 if(this.selX == this.x && this.selY == this.y)
1546 editX = Min(this.x,this.line.count);
1547 selX = Min(this.selX,this.selLine.count);
1550 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1552 foreground = selected ? selectionForeground : textColor;
1553 background = selectionBackground;
1559 surface.SetForeground(foreground);
1560 surface.SetBackground(background);
1561 surface.TextOpacity(opacity);
1563 surface.GetBox(box);
1565 for(line = this.viewLine; line; line = line.next)
1567 int x = -this.viewX;
1571 Color newTextColor = textColor = defaultTextColor;
1572 bool lineComplete = false;
1575 // ****** SYNTAX HIGHLIGHTING ******
1576 bool lastWasStar = false;
1577 bool trailingSpace = false;
1578 bool firstWord = true;
1579 if(!escaped) inPrep = false;
1580 inSingleLineComment = continuedSingleLineComment;
1582 inString = continuedString;
1583 inQuotes = continuedQuotes;
1584 // *********************************
1586 /* === DEBUGGING TOOL FOR MAXLINE ===
1588 if(line == this.maxLine)
1590 surface.SetBackground(GREEN|0xFF000000);
1591 surface.TextOpacity(true);
1595 surface.TextOpacity(selected ? true : false);
1596 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1600 if(line == this.selLine && line == this.line)
1602 end = Max(line.count, this.x);
1603 end = Max(end, this.selX);
1605 else if(line == this.selLine)
1606 end = Max(line.count, this.selX);
1607 else if(line == this.line)
1608 end = Max(line.count, this.x);
1616 bool spacing = true;
1617 bool cantHaveWords = false;
1624 lineComplete = false;
1627 textColor = newTextColor;
1630 foreground = textColor;
1631 surface.SetForeground(textColor);
1635 for(; c<end && !cantHaveWords;)
1639 bufferLen += wordLen;
1643 /*if(line.count > 4000)
1650 for(; c<line.count; c++)
1652 unichar ch = line.buffer[c];
1653 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1654 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1655 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1656 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1657 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1660 trailingSpace = false;
1666 for(; c<line.count; c++)
1668 unichar ch = line.buffer[c];
1669 if(ch == '\t' || ch == ' ')
1671 cantHaveWords = true;
1672 if(ch == ' ' && c == line.count-1)
1673 trailingSpace = true;
1677 if(ch != ' ' && ch != '\t')
1682 if(c == line.count && c < end)
1691 cantHaveWords = true;
1696 lastWasStar = false;
1702 bool backEscaped = escaped;
1703 bool backLastWasStar = lastWasStar;
1704 bool backInMultiLineComment = inMultiLineComment;
1705 bool backInString = inString;
1706 bool backInQuotes = inQuotes;
1707 bool backInPrep = inPrep;
1708 bool backInSingleLineComment = inSingleLineComment;
1709 bool backWasInMultiLine = wasInMultiLine;
1711 char * word = line.buffer + c - wordLen;
1713 bool wasEscaped = escaped;
1715 lastWasStar = false;
1717 wasInMultiLine = inMultiLineComment;
1719 // Determine Syntax Highlighting
1720 newTextColor = defaultTextColor;
1723 if(inSingleLineComment || inMultiLineComment)
1725 newTextColor = colorScheme.commentColor;
1729 newTextColor = colorScheme.charLiteralColor;
1733 newTextColor = colorScheme.stringLiteralColor;
1737 newTextColor = colorScheme.preprocessorColor;
1739 if(wordLen == 1 && word[0] == '/')
1741 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1745 inSingleLineComment = true;
1746 newTextColor = colorScheme.commentColor;
1748 else if(word[1] == '*')
1750 inMultiLineComment = true;
1751 newTextColor = colorScheme.commentColor;
1754 else if(backLastWasStar)
1755 inMultiLineComment = false;
1757 else if(wordLen == 1 && word[0] == '*')
1759 if(backWasInMultiLine)
1762 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1764 if(inString && !wasEscaped)
1771 newTextColor = colorScheme.stringLiteralColor;
1774 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1776 if(inQuotes && !wasEscaped)
1781 newTextColor = colorScheme.charLiteralColor;
1784 else if(wordLen == 1 && word[0] == '\\')
1789 else if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1791 char * dot = strchrmax(word, '.', wordLen);
1792 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1798 exponent = strchrmax(word, 'p', wordLen);
1799 if(!exponent) exponent = strchrmax(word, 'P', wordLen);
1803 exponent = strchrmax(word, 'e', wordLen);
1804 if(!exponent) exponent = strchrmax(word, 'E', wordLen);
1806 //if(exponent && exponent > word + wordLen) exponent = null;
1807 //if(dot && dot > word + wordLen) dot = null;
1808 isReal = dot || exponent;
1810 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1812 strtol(word, &s, 0);
1818 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1821 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1825 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1828 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1831 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1832 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1833 default: valid = false;
1837 // Don't highlight numbers with too many decimal points
1838 if(s[0] == '.' && isdigit(s[1]))
1841 while(s[0] == '.' && isdigit(s[1]))
1843 int newWordLen = s - word;
1844 c += newWordLen - wordLen;
1845 wordLen = newWordLen;
1848 newWordLen = s - word;
1849 c += newWordLen - wordLen;
1850 wordLen = newWordLen;
1854 int newWordLen = s + i - word;
1855 newTextColor = colorScheme.numberColor;
1856 c += newWordLen - wordLen;
1857 wordLen = newWordLen;
1859 else if(dot && dot > word && dot < s)
1860 newTextColor = colorScheme.numberColor;
1865 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1870 newTextColor = colorScheme.preprocessorColor;
1873 if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1875 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1877 char ** keys = keyWords[g];
1878 int * len = keyLen[g];
1879 for(ccc = 0; keys[ccc]; ccc++)
1881 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1883 newTextColor = colorScheme.keywordColors[g];
1892 // If highlighting for this word is different, break
1893 if(newTextColor != textColor)
1897 // Better solution than going back?
1900 // Reset syntax flags
1901 escaped = backEscaped;
1902 lastWasStar = backLastWasStar;
1903 inMultiLineComment = backInMultiLineComment;
1904 inString = backInString;
1905 inQuotes = backInQuotes;
1906 inPrep = backInPrep;
1907 inSingleLineComment = backInSingleLineComment;
1908 wasInMultiLine = backWasInMultiLine;
1913 textColor = newTextColor;
1916 foreground = textColor;
1917 surface.SetForeground(textColor);
1924 // If we're not breaking, this can't be rendered as spacing anymore
1927 // Do word wrapping here
1933 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1938 w += numSpaces * space.w;
1940 if(x + viewX + w > maxW)
1942 // Avoid an endless loop until we fix wrapping
1943 if(c - wordLen > start)
1946 lineComplete = true;
1952 bufferLen += wordLen;
1957 int renderStart = start;
1959 bool flagTrailingSpace = false;
1963 // Render checking if we need to split because of selection or to find where to draw insert caret
1964 for(wc = start; wc < start + bufferLen; wc++)
1966 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1967 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1968 if(overWrite == true)
1970 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1971 if(overWriteCh == '\t') overWriteCh = ' ';
1976 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1977 if(flagTrailingSpace) surface.SetBackground(red);
1978 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1979 if(overWrite == true)
1987 surface.TextOpacity(opacity);
1988 surface.SetBackground(background);
1989 surface.SetForeground(foreground);
1996 if(wc < line.count && line.buffer[wc] == '\t')
1998 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
2002 numSpaces += space.w;
2006 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
2007 if(flagTrailingSpace) surface.SetBackground(red);
2008 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
2013 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2014 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2016 if(overWrite == true)
2023 surface.TextOpacity(opacity);
2024 surface.SetBackground(background);
2025 surface.SetForeground(foreground);
2028 if(style.freeCaret && selected)
2030 surface.SetBackground(selectionBackground);
2031 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2032 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2037 if(style.freeCaret && selected)
2039 surface.SetBackground(selectionBackground);
2040 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2043 if(line.count && line.text[line.count - 1] == '\\')
2045 continuedSingleLineComment = inSingleLineComment;
2046 continuedString = inString;
2047 continuedQuotes = inQuotes;
2051 continuedSingleLineComment = false;
2052 continuedString = false;
2053 continuedQuotes = false;
2057 if(y > box.bottom) // >=clientSize.h)
2063 surface.TextOpacity(true);
2064 surface.SetForeground(black);
2065 surface.SetBackground(Color {255,255,85});
2066 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2070 void FixScrollArea()
2072 if(style.hScroll || style.vScroll)
2074 int width = maxLength + XOFFSET;
2075 int height = lineCount * space.h;
2076 if(style.freeCaret && line)
2081 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2085 if(selX > selLine.count)
2086 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2091 SetScrollLineStep(8, space.h);
2092 SetScrollArea(width, height, true);
2096 void ComputeLength(EditLine line)
2103 for(c = 0; c < line.count; )
2112 for(len = 0; c < line.count; c += numBytes)
2114 ch = line.buffer[c];
2115 numBytes = UTF8_NUM_BYTES(ch);
2117 if(ch == ' ' || ch == '\t')
2124 if(!len && ch == ' ')
2129 else if(!len && ch == '\t')
2131 w = (tabSize * space.w) - (x % (tabSize * space.w));
2135 FontExtent(display, font, line.buffer + start, len, &w, null);
2146 if(line.length > this.maxLength)
2148 this.maxLine = line;
2149 this.maxLength = line.length;
2151 if(style.autoSize) AutoSize();
2160 this.maxLine = null;
2162 for(line = lines.first; line; line = line.next)
2164 if(line.length > this.maxLength)
2166 this.maxLength = line.length;
2167 this.maxLine = line;
2171 if(style.autoSize) AutoSize();
2176 if(this.selY != this.y)
2178 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2182 void ComputeColumn()
2184 // Adjust new edit X position according to tabs
2185 int c, position = 0;
2188 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2190 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2192 position += this.tabSize - (position % this.tabSize);
2196 position += this.x - c;
2197 this.col = position;
2200 void DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2202 _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2205 bool HasCommentOrEscape(EditLine line)
2207 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2212 for(c = line.count-1; c >= 0; c--)
2214 char ch = line.buffer[c];
2220 else //if(ch != ' ' && ch != '\t')
2227 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2229 EditLine line = l1, next;
2231 int oldCount1, oldCount2;
2232 int y, firstViewY, firstY, firstDropY, firstSelY;
2235 DelTextAction action = null;
2236 bool hadComment = false;
2240 hadComment = HasCommentOrEscape(line);
2242 if(y2 > y1 || c2 > c1)
2244 if(start < l1.count)
2246 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2250 oldCount1 = l1.count;
2252 while(c1 < oldCount1)
2254 byte ch = buffer[c1];
2255 if(UTF8_IS_FIRST(ch)) break;
2259 oldCount2 = l2.count;
2261 while(c2 < oldCount2)
2263 byte ch = buffer[c2];
2264 if(UTF8_IS_FIRST(ch)) break;
2269 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2274 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2275 string = new char[len];
2276 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2277 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2281 //oldCount1 = l1.count;
2282 //oldCount2 = l2.count;
2285 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2286 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2289 if(c2 > oldCount2) c2 = oldCount2;
2290 if(!(style.freeCaret))
2291 if(c1 > oldCount1) c1 = oldCount1;
2292 newLineCount = c1 + l2.count-c2;
2298 buffer = new char[line.size ? line.size : 1];
2300 buffer = new char[line.size];
2301 // TODO: Better handling of these allocation failures
2302 if(!buffer) return extras;
2303 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2308 // TODO: Better handling of these allocation failures
2309 if(!line.AdjustBuffer(newLineCount))
2313 /*if(newLineCount > 4000 || newLineCount < 0)
2314 printf("Warning");*/
2316 line.count = newLineCount;
2318 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2323 action.addedSpaces = c1-oldCount1;
2325 if(action.addedSpaces > action.x1)
2331 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2332 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2334 line.buffer[line.count] = '\0';
2341 this.dropX -= c2-c1;
2342 this.dropX = Max(this.dropX,0);
2344 this.x = Max(this.x,0);
2351 firstViewY = this.viewY;
2353 firstDropY = this.dropY;
2354 firstSelY = this.selY;
2355 for(line = l1;line;line = next, y++)
2363 if(line == this.viewLine)
2365 if(this.viewLine.next)
2367 this.viewLine = this.viewLine.next;
2369 style.recomputeSyntax = true;
2373 this.viewLine = this.viewLine.prev;
2375 style.recomputeSyntax = true;
2378 else if(y < firstViewY)
2380 if(line == this.line)
2384 this.line = this.line.next;
2385 this.x = this.line.count;
2390 this.line = this.line.prev;
2391 this.x = this.line.count;
2398 if(line == this.dropLine)
2400 if(this.dropLine.next)
2402 this.dropLine = this.dropLine.next;
2403 this.dropX = this.dropLine.count;
2407 this.dropLine = this.dropLine.prev;
2408 this.dropX = this.dropLine.count;
2412 else if(y < firstDropY)
2414 if(line == this.selLine)
2416 if(this.selLine.next)
2418 this.selLine = this.selLine.next;
2419 this.selX = this.selLine.count;
2423 this.selLine = this.selLine.prev;
2424 this.selX = this.selLine.count;
2428 else if(y < firstSelY)
2432 if(line == l2) break;
2436 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2439 style.recomputeSyntax = true;
2444 bool DelSel(int * addedSpacesPtr)
2446 if(this.line != this.selLine || this.x != this.selX)
2448 if(this.selY < this.y)
2450 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2453 this.line = this.selLine;
2455 else if(this.selY > this.y)
2457 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2460 this.selLine = this.line;
2462 else if(this.selX < this.x)
2464 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2467 this.line = this.selLine;
2471 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2474 this.selLine = this.line;
2482 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2484 bool hadComment = false;
2485 // Add the line here
2486 EditLine line = this.line;
2488 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2489 if(!style.hScroll && created)
2491 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2496 // Lock if no place to display.
2499 else if(endX > this.x)
2500 max = Max(this.x, line.count);
2504 for(x = 0, c = 0; c < max+count; )
2509 if(c < Min(this.x, line.count))
2510 string = line.buffer + c;
2513 else if(c < endX + count)
2514 string = stringLine + c - endX;
2516 string = line.buffer + c - endX - count;
2520 w = (tabSize * space.w) - (x % (tabSize * space.w));
2524 numBytes = UTF8_NUM_BYTES(*string);
2525 FontExtent(display, this.font, string, numBytes, &w, null);
2529 if(x >= clientSize.w)
2534 c += numBytes; // - 1;
2540 int addedSpaces = 0;
2543 // Add blank spaces if EES_FREECARET
2544 if(this.x > line.count)
2554 for(c = 0; c<line.count; c++)
2556 if(this.line.buffer[c] == '\t')
2557 position += this.tabSize - (position % this.tabSize);
2561 wantedPosition = position + (this.x - line.count);
2563 // A tab is too much...
2564 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2565 addedSpaces = wantedPosition - position;
2570 position += this.tabSize - (position % this.tabSize);
2571 // Add as many tabs as needed
2572 addedTabs += (wantedPosition - position) / this.tabSize;
2573 position += (addedTabs-1) * this.tabSize;
2574 // Finish off with spaces
2575 addedSpaces = wantedPosition - position;
2579 addedSpaces = this.x - line.count;
2582 this.x = Min(this.x, line.count);
2584 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2590 hadComment = HasCommentOrEscape(line);
2592 // Adjust the size of the line
2593 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2597 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2600 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2601 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2604 *addedTabsPtr = addedTabs;
2605 FillBytes(line.buffer+line.count,'\t',addedTabs);
2607 if(addedTabs > 4000 || addedTabs < 0)
2610 line.count += addedTabs;
2616 FillBytes(line.buffer+line.count,' ',addedSpaces);
2618 if(addedSpaces > 4000 || addedSpaces < 0)
2621 line.count += addedSpaces;
2622 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2624 else if(addedSpacesPtr)
2625 *addedSpacesPtr = 0;
2627 if(count > 4000 || count < 0)
2630 line.count += count;
2631 this.x += count + addedTabs + addedSpaces;
2634 this.selLine = this.line;
2636 line.buffer[line.count] = '\0';
2638 ComputeLength(line);
2643 hasComment = HasCommentOrEscape(line);
2644 if(!undoBuffer.insideRedo)
2646 int backDontRecord = undoBuffer.dontRecord;
2647 undoBuffer.dontRecord = 0;
2648 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2649 undoBuffer.dontRecord = backDontRecord;
2651 if(style.syntax && (hadComment || hasComment || line != this.line))
2653 style.recomputeSyntax = true;
2661 void Emptyline(EditLine line, int y)
2664 DelCh(line, y, 0, line.next, y+1, 0, true);
2666 DelCh(line, y, 0, line, y, line.count, true);
2669 void GoToEnd(bool deselect)
2673 this.line = this.lines.last;
2674 if(this.y != this.lineCount-1)
2676 else if (this.x != this.line.count)
2677 DirtyLine(this.lineCount-1);
2678 this.y = this.lineCount-1;
2679 this.x = this.line.count;
2686 void GoToHome(bool deselect)
2690 this.line = this.lines.first;
2693 else if (this.x !=0)
2694 DirtyLine(this.lineCount-1);
2703 // Returns true if it needs scrolling
2704 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2710 bool needHScroll = false;
2717 line = this.viewLine ? (void *)this.viewLine.prev : null;
2722 line = (void *)this.lines.first;
2727 py = Min(py, clientSize.h);
2729 py = Min(py, this.lineCount);
2731 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2737 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2740 px = Min(px,clientSize.w+this.space.w);
2744 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2747 if(tline) *tline = line;
2750 // Prevent divide by 0 from non valid this.font
2752 return (y < this.viewY) || needHScroll;
2754 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2758 // Minimal Update Management Functions
2762 this.endY = clientSize.h-1;
2763 // ErrorLog("DirtyAll\n");
2766 void DirtyEnd(int y)
2768 if((y - this.viewY)*this.space.h < this.startY)
2769 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2770 this.endY = clientSize.h-1;
2771 //ErrorLog("DirtyEnd %d\n", y);
2774 void DirtyLine(int y)
2778 if((y - this.viewY)*this.space.h < this.startY)
2779 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2780 if((y - this.viewY+1)*this.space.h > this.endY)
2781 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2783 //ErrorLog("DirtyLine %d\n", y);
2790 if(style.recomputeSyntax)
2792 FigureStartSyntaxStates(lines.first, true);
2793 style.recomputeSyntax = false;
2796 if(this.startY > this.endY) return;
2797 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2803 box.right = clientSize.w-1;
2804 box.top = this.startY;
2805 box.bottom = this.endY;
2808 this.startY = clientSize.h;
2812 bool IsMouseOnSelection()
2814 bool mouseOnSelection = false;
2817 int minY = Min(this.selY, this.y);
2818 int maxY = Max(this.selY, this.y);
2819 int minX = Min(this.selX, this.x);
2820 int maxX = Max(this.selX, this.x);
2822 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2824 if(maxX != minX || maxY != minY)
2826 if(y > minY && y < maxY)
2827 mouseOnSelection = true;
2828 else if(y == minY && y == maxY)
2829 mouseOnSelection = (x < maxX && x >= minX);
2833 mouseOnSelection = (x >= this.selX);
2834 else if(y == this.y)
2835 mouseOnSelection = (x >= this.x);
2840 mouseOnSelection = (x < this.selX);
2841 else if(y == this.y)
2842 mouseOnSelection = (x < this.x);
2845 return mouseOnSelection;
2848 void UpdateCaretPosition(bool setCaret)
2852 if(mouseMove || !style.noCaret)
2854 int max = this.mouseMove ? this.dropX : this.x;
2855 int y = this.mouseMove ? this.dropY : this.y;
2856 EditLine line = this.mouseMove ? this.dropLine : this.line;
2858 if(!(style.freeCaret))
2859 max = Min(max, line.count);
2861 if(FontExtent && display)
2863 for(c = 0; c < max; )
2872 for(len = 0; c < Min(max, line.count); c += numBytes)
2874 ch = line.buffer[c];
2875 numBytes = UTF8_NUM_BYTES(ch);
2877 if(ch == ' ' || ch == '\t')
2884 if(!len && ch == ' ')
2889 else if(!len && ch == '\t')
2891 w = (tabSize * space.w) - (x % (tabSize * space.w));
2895 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2907 caretY = y * this.space.h;
2909 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2916 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2917 NotifyCaretMove(master, this, y + 1, x + 1);
2923 void SelectionEnables()
2925 if((x != selX || y != selY) && !selection)
2929 itemEditCut.disabled = false;
2930 itemEditDelete.disabled = false;
2932 itemEditCopy.disabled = false;
2934 this.selection = true;
2936 else if((x == selX && y == selY) && selection)
2938 itemEditCut.disabled = true;
2939 itemEditCopy.disabled = true;
2940 itemEditDelete.disabled = true;
2942 this.selection = false;
2946 void SetSelectCursor()
2948 if(!inactive || !style.noSelect)
2951 cursor = guiApp.GetCursor(arrow);
2952 else if(this.mouseSelect)
2953 cursor = guiApp.GetCursor(iBeam);
2956 if(IsMouseOnSelection())
2957 cursor = guiApp.GetCursor(arrow);
2959 cursor = guiApp.GetCursor(iBeam);
2964 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2967 int x = px ? *px : 0;
2974 if(c < Min(max, line.count))
2978 for(len = 0; c < Min(max, line.count); c += numBytes)
2980 ch = line.buffer[c];
2981 numBytes = UTF8_NUM_BYTES(ch);
2983 if(ch == ' ' || ch == '\t')
2990 if(!len && ch == ' ')
2995 else if(!len && ch == '\t')
2997 w = (tabSize * space.w) - (x % (tabSize * space.w));
3001 FontExtent(display, font, line.buffer + start, len, &w, null);
3005 if(style.freeCaret && c < max)
3014 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
3019 int a = start + len;
3022 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
3026 FontExtent(display, font, line.buffer + start, a - start, &w, null);
3029 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
3033 return Min(this.maxLineSize - 1, start + len);
3039 void SetCursorToViewX()
3041 bool selecting = this.x != selX || y != selY;
3043 // Horizontal Adjustment
3045 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3050 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3062 UpdateCaretPosition(false);
3068 void SetCursorToViewY()
3071 EditLine oldLine = this.line;
3073 bool selecting = this.x != this.selX || this.y != this.selY;
3075 numLines = clientSize.h / this.space.h;
3077 // Vertical Adjustment
3078 if(this.viewY > this.y)
3080 this.y = this.viewY;
3081 this.line = this.viewLine;
3084 if(this.viewY + numLines <= this.y)
3088 this.y = this.viewY-1;
3089 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3096 if(this.line != oldLine)
3098 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3106 this.selLine = this.line;
3109 UpdateCaretPosition(false);
3116 bool SaveFile(char * fileName)
3118 File f = eFile_Open(fileName, FO_WRITE);
3122 eWindow_SetModified(false);
3123 eInstance_Delete(f);
3130 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3134 if(!style.multiLine)
3136 x = (active && this.active && !style.readOnly) ? line.count : 0;
3139 SetViewToCursor(true);
3144 if(!active && modified)
3147 if(!NotifyModified(master, this))
3150 *goOnWithActivation = false;
3157 if(timer) timer.Stop();
3159 mouseSelect = false;
3167 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3168 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3169 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3172 bool OnResizing(int *w, int *h)
3178 *w = space.h * 80 / 14;
3182 void OnResize(int w, int h)
3185 if(!hasHorzScroll && !hasVertScroll && viewLine)
3186 SetViewToCursor(true);
3188 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3190 SetViewToCursor(true);
3195 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3197 if(style.readOnly) return true;
3198 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3199 // Middle button already serves as a dragger as well.
3204 bool OnRightButtonDown(int x, int y, Modifiers mods)
3206 this.rightButtonDown = true;
3211 bool OnRightButtonUp(int x, int y, Modifiers mods)
3214 if(!parent.inactive && rightButtonDown)
3217 Menu contextMenu { };
3219 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3220 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3221 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3222 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3223 MenuDivider { contextMenu };
3224 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3226 popup = PopupMenu { master = this, menu = contextMenu,
3228 nonClient = true, interim = false, parent = parent,
3229 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3231 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3235 rightButtonDown = false;
3239 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3244 if(style.noSelect) return true;
3246 // Should we have a separate 'selectOnActivate' style?
3247 if(!mods.isActivate || (style.readOnly && style.multiLine))
3253 mouseX = mx - XOFFSET;
3256 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3258 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3264 else if(IsMouseOnSelection() && !mods.isActivate)
3274 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3276 if(mods.shift && !mods.isActivate)
3297 UpdateCaretPosition(true);
3298 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3299 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3303 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3307 mouseSelect = false;
3318 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3320 //PrintLn("MouseMove: ", x, ", ", y);
3326 mouseMove = IsMouseOnSelection();
3330 int size = SelSize();
3333 char * text = new char[size+1];
3337 GetSel(text, false);
3339 if(Max(selY, this.y) == dropY)
3343 if(this.dropX > this.selX)
3344 moveX = this.x - this.selX;
3348 if(this.dropX > this.x)
3349 moveX = this.selX - this.x;
3353 recordUndoEvent = true;
3355 this.dropX -= moveX;
3356 this.selX = this.x = this.dropX;
3357 this.selY = this.y = this.dropY;
3358 this.selLine = this.line = this.dropLine;
3360 recordUndoEvent = false;
3362 SetViewToCursor(true);
3367 byte * c = ((EditBox)this).multiLineContents, * c2;
3368 int l1 = c ? strlen(c) : 0, l2;
3371 c2 = ((EditBox)this).multiLineContents;
3372 l2 = c2 ? strlen(c2) : 0;
3373 if(l1 != l2 || (l1 && strcmp(c, c2)))
3403 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3405 //PrintLn("Dropped: ", x, ", ", y);
3407 NotifyDropped(master, this, x, y);
3411 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3412 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3416 bool OnMouseMove(int mx, int my, Modifiers mods)
3422 if(mods != -1 && mods.isSideEffect)
3427 if(style.noSelect) return true;
3428 if(wordSelect) return true;
3429 mouseX = mx - XOFFSET;
3432 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3434 if(this.mouseMove || this.mouseSelect)
3443 ((style.hScroll) || (style.vScroll)))
3450 DirtyLine(this.dropY);
3453 DirtyLine(this.dropY);
3454 this.dropLine = line;
3455 SetViewToCursor(true);
3457 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3460 else if(this.mouseSelect)
3462 DirtyLine(this.selY);
3469 SetViewToCursor(true);
3472 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3479 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3484 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3488 if(style.noSelect) return true;
3489 FindMouse(mx, my, &x, &y, &line, false);
3490 if(!NotifyDoubleClick(master, this, line, mods))
3497 for(c = x; c >= 0; c--)
3500 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3501 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3508 for(c = start; c<line.count; c += numBytes)
3510 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3511 if(!ch || !IS_ALUNDER(ch))
3521 this.line = this.selLine = line;
3522 this.wordSelect = (c != start);
3533 Menu fileMenu { menu, "File", F };
3534 saveDialog = fileDialog;
3535 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3536 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3538 if(style.autoSize) AutoSize();
3546 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3547 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3549 space.w = Max(space.w, 1);
3550 large.w = Max(large.w, 1);
3551 space.h = Max(space.h, 1);
3552 large.h = Max(large.h, 1);
3556 for(line = lines.first; line; line = line.next)
3557 ComputeLength(line);
3562 SetViewToCursor(true);
3570 bool OnLoadGraphics()
3572 FontExtent = Display::FontExtent;
3575 // UpdateCaretPosition(true);
3579 void OnUnloadGraphics()
3584 bool OnKeyHit(Key key, unichar ch)
3586 bool shift = (key.shift) ? true : false;
3588 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3590 if(!ch && !key.alt && !key.ctrl)
3592 key.code = (SmartKey)key.code;
3594 else if(!ch && key.alt)
3597 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3600 if(style.readOnly) break;
3601 if(style.stuckCaret) GoToEnd(true);
3602 if(!(style.freeCaret))
3604 this.x = Min(this.x, this.line.count);
3610 EditLine line = this.line;
3612 for(y = this.y; y>= 0; y--)
3614 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3616 // Slow down when going on lines...
3617 if(y != this.y) break;
3621 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3622 if(IS_ALUNDER(line.buffer[c]))
3627 if(!IS_ALUNDER(line.buffer[c]))
3629 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3644 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3645 this.x = this.selX = Min(c+1, line.count);
3646 this.y = this.selY = y;
3647 this.line = this.selLine = line;
3648 SetViewToCursor(true);
3659 if(style.readOnly) break;
3660 if(style.stuckCaret) break;
3669 if(this.line != this.selLine || this.x != this.selX)
3672 SetViewToCursor(true);
3677 EditLine line1 = this.line, line2 = this.line;
3678 int x1, y1 = this.y, x2, y2 = this.y;
3679 if(!style.freeCaret)
3681 this.selX = this.x = Min(this.x, this.line.count);
3686 if(x1 < line1.count)
3693 char * buffer = line1.buffer;
3694 for(i = x1; i < line1.count; i++)
3696 if(!IS_ALUNDER(buffer[i]))
3700 for(; i < line1.count; i++)
3702 // Delete trailing whitespace
3703 if(IS_ALUNDER(buffer[i]))
3713 // Avoid creating trailing spaces if there is no content on next line
3716 if(line2 && !line2.count)
3721 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3722 SetViewToCursor(true);
3731 if(!key.alt && !key.ctrl)
3735 bool stuffAfter = false;
3738 /*bool resetX = false;
3741 if(style.stuckCaret) GoToEnd(true);
3742 if(style.readOnly) break;
3743 if(!(style.multiLine)) break;
3745 for(c = 0; c<this.line.count && c<this.x; c++)
3747 if(this.line.buffer[c] == '\t')
3748 position += this.tabSize - (position % this.tabSize);
3749 else if(this.line.buffer[c] == ' ')
3755 // Prevent adding trailing spaces if at the head of a line
3756 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3768 if(this.x < this.line.count)
3771 //If last character is a { indent one tab
3772 c = Min(x, line.count);
3773 if(c > 0 && line.buffer[c - 1] == '{')
3775 //Except if the next non space character is a }
3776 bool indent = false;
3778 for(i = c; i <= this.line.count; i++) // indent will be set to true on nul terminating char
3779 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3781 if(this.line.buffer[i] != '}')
3786 position += this.tabSize;
3789 addString = new char[position + 2];
3790 addString[len++] = '\n';
3791 addString[len] = '\0';
3793 if(stuffAfter || !style.freeCaret)
3795 for(c = 0; c<position; )
3797 if(style.useTab && c + this.tabSize <= position)
3799 addString[len++] = '\t';
3804 addString[len++] = ' ';
3808 addString[len] = '\0';
3810 recordUndoEvent = true;
3813 EditLine prevLine = this.line.prev;
3816 // Nuke spaces if that is all that is left on previous line
3818 char * buffer = prevLine.buffer;
3819 for(i = 0; i < prevLine.count; i++)
3820 if(buffer[i] != ' ' && buffer[i] != '\t')
3822 if(i == prevLine.count)
3823 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3827 this.x = this.selX = backX;
3830 else */if(!stuffAfter && style.freeCaret)
3832 this.x = this.selX = position;
3836 SetViewToCursor(true);
3839 recordUndoEvent = false;
3847 if(style.stuckCaret) break;
3848 if(!(style.freeCaret))
3850 this.x = Min(this.x, this.line.count);
3851 this.selX = Min(this.selX, this.selLine.count);
3854 if(!shift) SelDirty();
3857 bool foundAlpha = false;
3860 EditLine line, lastLine;
3863 for(line = this.line; (line && !found); line = line.prev, y--)
3868 if(this.x == 0 && line != this.line)
3877 if(line == this.line) start = this.x -1; else start = line.count-1;
3878 start = Min(start, line.count-1);
3880 for(c = start; c >= 0;)
3883 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3901 byte ch = line.buffer[c];
3902 if(UTF8_IS_FIRST(ch)) break;
3906 // No next word found,
3907 if(!found && ( this.x > 0 || (!line.count && this.x)))
3921 this.line = lastLine;
3932 byte * buffer = line.buffer;
3935 byte ch = buffer[x];
3936 if(UTF8_IS_FIRST(ch)) break;
3953 if(!shift) _Deselect();
3954 SetViewToCursor(true);
3960 if(style.stuckCaret) break;
3961 if(!(style.freeCaret))
3963 this.x = Min(this.x, this.line.count);
3964 this.selX = Min(this.selX, this.selLine.count);
3967 if(!shift) SelDirty();
3968 if(!shift && (this.x != this.selX || this.y != this.selY));
3971 bool onAChar = false;
3972 if(this.selX != this.x || this.selY != this.y)
3974 if(this.x<this.line.count)
3975 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3977 if(key.shift && onAChar &&
3978 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3980 bool foundAlpha = false;
3982 EditLine line, lastLine;
3984 int lastC, lastY, lastNumBytes;
3986 for(line = this.line; (line && !found); line = line.next, y++)
3988 int start = (line == this.line) ? this.x : 0;
3992 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3998 lastNumBytes = numBytes;
4008 if(!found && (c != this.x || line != this.line))
4021 this.x = lastC + lastNumBytes;
4023 this.line = lastLine;
4030 bool foundAlpha = false;
4035 for(line = this.line; (line && !found); line = line.next, y++)
4037 int start = (line == this.line) ? this.x : 0;
4041 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4057 // No next word found,
4058 if(!found && (c != this.x || line != this.line))
4062 this.x = line.count;
4074 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4078 byte * buffer = line.buffer;
4081 byte ch = buffer[x];
4082 if(UTF8_IS_FIRST(ch)) break;
4103 if(!shift) _Deselect();
4104 SetViewToCursor(true);
4111 if(!style.vScroll || hasVertScroll) break;
4117 if(style.stuckCaret) break;
4119 if(!shift) SelDirty();
4129 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4133 if(!shift) _Deselect();
4135 SetViewToCursor(false);
4138 if(caretY == this.y * space.h)
4144 if(!style.freeCaret)
4145 this.x = Min(this.x, line.count);
4155 int sx = 0, sy = this.y * space.h;
4156 char * text = line.text;
4157 int maxW = clientSize.w - sx;
4158 display.FontExtent(font, " ", 1, null, &th);
4162 int startPos = textPos;
4165 bool lineComplete = false;
4167 if(!style.wrap && caretY == MAXINT)
4170 //textPos = line.count;
4171 //lineComplete = true;
4174 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4178 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4181 len = (nextSpace - (text + textPos));
4183 len = line.count - textPos;
4185 if(textPos < line.count)
4187 display.FontExtent(font, text + textPos, len, &w, null);
4189 if(nextSpace) { w += space.w; len++; }
4191 if(style.wrap && x + width + w > maxW && x > 0)
4193 lineComplete = true;
4203 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4207 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4210 if(this.x > line.count)
4213 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4214 len = this.x - startPos;
4215 display.FontExtent(font, text + startPos, len, &x, null);
4219 if(!shift) _Deselect();
4221 SetViewToCursor(false);
4225 if(sy == caretY - th || textPos >= line.count)
4227 if(textPos >= line.count)
4229 int c = textPos - 1;
4230 while(c > 0 && text[c] == ' ') c--;
4234 this.x = line.count;
4237 if(!shift) _Deselect();
4239 SetViewToCursor(false);
4244 } while(textPos < line.count);
4247 if(!shift) _Deselect();
4249 SetViewToCursor(false);
4258 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4259 if(!shift) SelDirty();
4260 this.line = this.line.prev;
4266 if(!shift) _Deselect();
4270 SetViewToCursor(false);
4276 return style.multiLine ? false : true;
4280 if(!style.vScroll || hasVertScroll)
4287 if(style.stuckCaret) break;
4291 int sx = 0, sy = this.y * this.space.h;
4292 int maxW = clientSize.w - sx;
4293 char * text = line.buffer;
4295 if(!shift) SelDirty();
4301 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4311 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4315 if(!shift) _Deselect();
4317 SetViewToCursor(false);
4320 while(!textPos || (style.freeCaret || textPos<line.count))
4322 int startPos = textPos;
4325 bool lineComplete = false;
4326 if(!style.wrap && sy <= caretY)
4328 textPos = line.count;
4329 lineComplete = true;
4331 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4335 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4338 len = (nextSpace - (text + textPos));
4340 len = line.count - textPos;
4342 if(textPos < line.count)
4344 display.FontExtent(font, text + textPos, len, &w, &th);
4346 if(nextSpace) { w += space.w; len++; }
4347 if(style.wrap && x + width + w > maxW && x > 0)
4349 lineComplete = true;
4359 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4363 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4366 if(this.x > line.count)
4369 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4371 len = this.x - startPos;
4372 display.FontExtent(font, text + startPos, len, &x, null);
4375 if(!shift) _Deselect();
4378 SetViewToCursor(false);
4384 this.x = line.count;
4387 if(!shift) _Deselect();
4390 SetViewToCursor(false);
4393 else if(textPos >= line.count && line.next)
4399 sy = this.y * this.space.h;
4400 sx = 0; //textBlock.startX;
4406 sx = 0; //textBlock.startX;
4414 this.x = Min(this.x, line.count);
4415 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4417 if(!shift) _Deselect();
4420 if(this.selX != this.x || this.selY != this.y)
4423 SetViewToCursor(false);
4430 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4431 if(!shift) SelDirty();
4432 this.line = this.line.next;
4436 if(!shift) _Deselect();
4439 if(this.selX != this.x || this.selY != this.y)
4447 return style.multiLine ? false : true;
4450 if(style.stuckCaret) break;
4451 if(!style.multiLine && key.ctrl) break;
4452 if(!(style.freeCaret))
4453 this.selX = Min(this.selX, this.selLine.count);
4455 if(!shift) SelDirty();
4458 this.line = this.lines.first;
4459 if(this.y != 0 || this.x != 0)
4469 EditLine line = this.line;
4471 for(c=0; line.buffer[c]; c++)
4472 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4474 if(overwrite || (shift && (c != 0 || this.x)))
4483 if(overwrite || (shift && this.x != 0))
4489 if(!shift) _Deselect();
4490 SetViewToCursor(true);
4496 if(style.stuckCaret) break;
4497 if(!style.multiLine && key.ctrl) break;
4498 if(!style.freeCaret)
4499 this.selX = Min(this.selX, this.selLine.count);
4501 if(!shift) SelDirty();
4506 else if(this.x != this.line.count)
4508 this.x = this.line.count;
4509 if(overwrite || shift)
4513 if(!shift) _Deselect();
4514 SetViewToCursor(true);
4519 if(style.tabKey && !key.ctrl)
4521 if(this.selY != this.y && style.tabSel)
4523 EditLine firstLine, lastLine;
4527 // Do multi line selection tabbing here
4528 if(this.selY < this.y)
4530 firstLine = this.selLine;
4531 lastLine = this.line;
4537 // Selecting going up
4538 firstLine = this.line;
4539 lastLine = this.selLine;
4546 for(line = firstLine; line; line = line.next, y++)
4548 if(line != lastLine || x)
4552 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4554 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4556 if(line.buffer[c] == '\t')
4561 else if(line.buffer[c] != ' ')
4566 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4569 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4570 char * string = new char[len];
4571 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4572 GetText(string, line, y, 0, line, y, lastC, false, false);
4575 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4576 if(!line.AdjustBuffer(line.count-lastC))
4582 if(line == lastLine) break;
4587 for(line = firstLine; line; line = line.next, y++)
4591 if(line != lastLine || x)
4595 if(line.count + 1 <= this.maxLineSize)
4597 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4599 if(!line.AdjustBuffer(line.count+1))
4602 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4604 AddCharAction action { ch = '\t', x = 0, y = y };
4608 memmove(line.buffer+1,line.buffer,line.size-1);
4610 line.buffer[0] = '\t';
4615 if(line.count + this.tabSize <= this.maxLineSize)
4618 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4619 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4621 if(!line.AdjustBuffer(line.count+this.tabSize))
4625 char * string = new char[this.tabSize + 1];
4626 memset(string, ' ', this.tabSize);
4627 string[this.tabSize] = '\0';
4628 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4631 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4632 line.count+=this.tabSize;
4633 for(c=0; c<this.tabSize; c++)
4634 line.buffer[c] = ' ';
4640 if(line == lastLine) break;
4643 ComputeLength(maxLine);
4655 char * addString = new char[this.tabSize + 1];
4657 if(!(style.freeCaret))
4659 this.x = Min(this.x, this.line.count);
4663 start = Min(this.x, this.selX);
4664 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4666 addString[len++] = ' ';
4674 SetViewToCursor(true);
4683 if(!(style.hScroll) || hasHorzScroll) break;
4684 if(this.viewX < this.maxLength)
4686 //this.viewX+=this.space.w*this.tabSize;
4688 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4695 if(!shift) _Deselect();
4707 if(!(style.hScroll) || hasHorzScroll) break;
4710 //this.viewX-=this.space.w*this.tabSize;
4711 //this.viewX = Max(this.viewX,0);
4713 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4714 // SetCursorToView();
4721 if(!shift) _Deselect();
4734 else if(!style.readOnly)
4738 if(!(style.readOnly))
4744 this.overwrite ^= 1;
4745 UpdateCaretPosition(true);
4750 NotifyOvrToggle(master, this, this.overwrite);
4762 if(style.noSelect) break;
4765 //Save current view position
4766 int tempX = this.viewX;
4767 int tempY = this.viewY;
4771 this.selLine = this.lines.first;
4772 this.y = this.lineCount-1;
4773 this.line = this.lines.last;
4774 this.x = this.line.count;
4777 SetViewToCursor(true);
4779 //Restore previous view position
4780 SetScrollPosition(tempX, tempY * this.space.h);
4786 // TOCHECK: Was there any good reason why we weren't returning false here?
4787 return false; // break;
4792 if(!(style.readOnly))
4800 if(style.readOnly) break;
4806 if(style.readOnly) break;
4811 if(style.readOnly) break;
4815 if(style.readOnly) break;
4819 if(style.readOnly) break;
4820 if(key.shift && key.code == rightBracket)
4822 //Only indent back if you are exactly at one tab.
4824 bool whitespace = true;
4830 EditLine line = this.line;
4832 //Only remove one tab if there is nothing else on the line.
4833 for(i = 0; i < this.line.count; i++)
4835 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4849 for(pos = line.count - 1; pos >= 0; pos--)
4851 char c = line.buffer[pos];
4855 indentwidth += this.tabSize;
4857 //Counting backwards, so when you find a character, indentation is reset
4867 //Place the } to get an undo:
4870 this.x = this.line.count;
4874 putsize = indentwidth;
4876 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4878 newline = new char[putsize+2];
4879 newline[putsize] = '}';
4880 newline[putsize+1] = '\0';
4884 for(; i < indentwidth / this.tabSize; i++)
4886 for(;i < putsize; i++)
4895 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4907 void OnHScroll(ScrollBarAction action, int position, Key key)
4910 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4912 this.viewX = position;
4913 if(action != setRange)
4915 if(!this.mouseMove && style.cursorFollowsView)
4922 void OnVScroll(ScrollBarAction action, int position, Key key)
4924 int oldViewY = this.viewY;
4927 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4929 position /= this.space.h;
4931 if(position < this.viewY)
4933 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4934 style.recomputeSyntax = true;
4936 else if(position > this.viewY)
4938 EditLine oldViewLine = viewLine;
4939 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4940 FigureStartSyntaxStates(oldViewLine, false);
4943 if(action != setRange)
4945 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4948 if(this.x != this.selX || this.y != this.selY)
4952 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4955 int numLines = clientSize.h / this.space.h;
4957 if(Abs(this.viewY - oldViewY) < numLines)
4960 if(this.viewY > oldViewY)
4962 for(y = oldViewY; y <this.viewY; y++)
4963 DirtyLine(y + numLines);
4967 for(y = this.viewY; y <oldViewY; y++)
4968 DirtyLine(y + numLines);
4973 // Fix dirt of stuff before first line...
4974 if(this.viewY - oldViewY > 0)
4976 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4983 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4988 ReplaceTextAction replaceAction = null;
4989 AddCharAction addCharAction = null;
4990 int addedSpaces = 0, addedTabs = 0;
4992 if(ch == '\r') return true;
4993 if(style.stuckCaret /*|EES_READONLY)*/ )
4996 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4998 if(!undoBuffer.dontRecord)
5000 if(selX != x || selY != y)
5005 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5006 oldString = new char[len];
5007 UTF32toUTF8Len(&ch, 1, buffer, 4);
5008 newString = CopyString(buffer);
5009 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5011 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5012 if(selY < y || (selY == y && selX < x))
5014 replaceAction.x1 = selX;
5015 replaceAction.y1 = selY;
5016 replaceAction.x2 = x;
5017 replaceAction.y2 = y;
5021 replaceAction.x1 = x;
5022 replaceAction.y1 = y;
5023 replaceAction.x2 = selX;
5024 replaceAction.y2 = selY;
5026 Record(replaceAction);
5027 undoBuffer.dontRecord++;
5031 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5032 Record(addCharAction);
5038 DelSel(&addedSpaces);
5039 if(this.lineCount+1 > this.maxLines)
5042 Emptyline(this.lines.first,0);
5046 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5048 // Make sure it fits, but we need a default line is this.font is too big for window
5049 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5052 if((this.y >= 0) && this.y < this.viewY)
5057 line = EditLine { };
5060 lines.Insert(this.line, line);
5061 line.editBox = this;
5067 // If we're displacing the lines from a current line ...
5070 if(this.line.buffer)
5073 if(this.line.count < endX) endX = this.line.count;
5074 length = this.line.count - endX;
5077 if(!line.AdjustBuffer(length))
5081 if(this.line.buffer)
5083 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5085 if(endX > 4000 || endX < 0)
5088 this.line.count = endX;
5089 this.line.buffer[this.line.count] = '\0';
5090 this.line.AdjustBuffer(this.line.count);
5091 ComputeLength(this.line);
5095 BufferLocation before = { this.line, this.y, this.x }, after;
5100 line.count = length;
5103 if(length > 4000 || length < 0)
5106 ComputeLength(this.line);
5111 line.buffer[line.count] = '\0';
5114 after.line = this.line, after.y = this.y, after.x = this.x;
5116 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5122 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5123 DelSel(&addedSpaces);
5124 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5125 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5126 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5130 this.selLine = this.line;
5134 replaceAction.x3 = x;
5135 replaceAction.y3 = y;
5136 replaceAction.addedSpaces = addedSpaces;
5137 replaceAction.addedTabs = addedTabs;
5138 undoBuffer.dontRecord--;
5142 addCharAction.x -= addedTabs * (tabSize-1);
5143 addCharAction.addedSpaces = addedSpaces;
5144 addCharAction.addedTabs = addedTabs;
5151 /****************************************************************************
5153 ****************************************************************************/
5156 bool AddCh(unichar ch)
5158 return _AddCh(ch, null, null);
5163 this.modified = true;
5164 NotifyUpdate(master, this);
5165 modifiedDocument = true;
5168 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5171 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5172 SetViewToCursor(true);
5180 itemEditUndo.disabled = undoBuffer.curAction == 0;
5181 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5182 if(savedAction == undoBuffer.curAction)
5184 modifiedDocument = false;
5186 NotifyUnsetModified(master, this);
5193 itemEditUndo.disabled = undoBuffer.curAction == 0;
5194 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5195 if(savedAction == undoBuffer.curAction)
5197 modifiedDocument = false;
5199 NotifyUnsetModified(master, this);
5203 void Record(UndoAction action)
5205 if(!undoBuffer.dontRecord)
5207 undoBuffer.Record(action);
5208 itemEditUndo.disabled = undoBuffer.curAction == 0;
5209 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5216 this.lines.first, 0,0,
5217 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5220 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5225 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5226 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5227 this.y = line2 ? y2 : (this.lineCount-1);
5228 this.line = line2 ? (EditLine)line2 : this.lines.last;
5231 SetViewToCursor(true);
5235 // TODO: Fix this vs modifiedDocument window property
5236 void SetModified(bool flag)
5240 this.modified = false;
5241 if(flag && !NotifyModified(master, this))
5242 this.modified = true;
5247 bool AddS(char * string)
5254 int addedSpaces = 0, addedTabs = 0;
5255 AddTextAction action = null;
5256 ReplaceTextAction replaceAction = null;
5258 this.pasteOperation = true;
5260 if(style.stuckCaret /*|EES_READONLY)*/ )
5263 if(!undoBuffer.dontRecord)
5265 char * placeString = CopyString(string);
5266 if(!style.multiLine)
5270 for(i = 0; (ch = placeString[i]); i++)
5273 placeString[i] = '\0';
5274 placeString = renew placeString byte[i+1];
5279 if(selX != x || selY != y)
5283 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5284 oldString = new char[len];
5285 newString = placeString;
5286 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5288 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5289 if(selY < y || (selY == y && selX < x))
5291 replaceAction.x1 = selX;
5292 replaceAction.y1 = selY;
5293 replaceAction.x2 = x;
5294 replaceAction.y2 = y;
5298 replaceAction.x1 = x;
5299 replaceAction.y1 = y;
5300 replaceAction.x2 = selX;
5301 replaceAction.y2 = selY;
5303 Record(replaceAction);
5307 if(string[0] == '\n')
5308 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5310 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5318 undoBuffer.dontRecord++;
5319 DelSel(&addedSpaces);
5323 for(c = 0; string[c]; c++)
5325 if(string[c] == '\n' || string[c] == '\r')
5327 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5332 if(string[c] == '\n')
5341 // Reset for next line
5345 if(string[c] == '\r' && *line == '\n')
5357 // Why was this here?
5360 // Add the line here
5362 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5368 undoBuffer.dontRecord--;
5373 action.addedSpaces = addedSpaces;
5374 action.addedTabs = addedTabs;
5376 else if(replaceAction)
5378 replaceAction.y3 = y;
5379 replaceAction.x3 = x;
5380 replaceAction.addedSpaces = addedSpaces;
5381 replaceAction.addedTabs = addedTabs;
5384 UpdateCaretPosition(true);
5386 this.pasteOperation = false;
5399 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5400 SetViewToCursor(true);
5406 void PutCh(unichar ch)
5410 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5411 //if((ch >= 32) || ch == '\n')
5413 int addedSpaces = 0, addedTabs = 0;
5414 ReplaceTextAction replaceAction = null;
5415 AddCharAction addCharAction = null;
5418 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5420 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5425 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5426 oldString = new char[len];
5427 UTF32toUTF8Len(&ch, 1, buffer, 4);
5428 newString = CopyString(buffer);
5429 GetText(oldString, line, y, x, line, y, x+1, false, false);
5430 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5431 replaceAction.x1 = x;
5432 replaceAction.y1 = y;
5433 replaceAction.x2 = x+1;
5434 replaceAction.y2 = y;
5435 Record(replaceAction);
5437 undoBuffer.dontRecord++;
5438 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5439 undoBuffer.dontRecord--;
5441 else if(!undoBuffer.dontRecord)
5443 if(selX != x || selY != y)
5448 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5449 oldString = new char[len];
5450 UTF32toUTF8Len(&ch, 1, buffer, 4);
5451 newString = CopyString(buffer);
5452 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5453 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5454 if(selY < y || (selY == y && selX < x))
5456 replaceAction.x1 = selX;
5457 replaceAction.y1 = selY;
5458 replaceAction.x2 = x;
5459 replaceAction.y2 = y;
5463 replaceAction.x1 = x;
5464 replaceAction.y1 = y;
5465 replaceAction.x2 = selX;
5466 replaceAction.y2 = selY;
5468 Record(replaceAction);
5472 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5473 Record(addCharAction);
5476 undoBuffer.dontRecord++;
5477 result = _AddCh(ch, &addedSpaces, &addedTabs);
5480 replaceAction.x3 = x;
5481 replaceAction.y3 = y;
5482 replaceAction.addedSpaces = addedSpaces;
5483 replaceAction.addedTabs = addedTabs;
5487 addCharAction.x -= addedTabs * (tabSize-1);
5488 addCharAction.addedSpaces = addedSpaces;
5489 addCharAction.addedTabs = addedTabs;
5491 undoBuffer.dontRecord--;
5495 if(result) SetViewToCursor(true);
5499 void PutS(char * string)
5504 SetViewToCursor(true);
5509 void Printf(char * format, ...)
5513 char temp[MAX_F_STRING];
5515 va_start(args, format);
5516 vsnprintf(temp, sizeof(temp), format, args);
5517 temp[sizeof(temp)-1] = 0;
5523 void SetContents(char * format, ...)
5527 undoBuffer.dontRecord++;
5529 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5532 char temp[MAX_F_STRING];
5534 va_start(args, format);
5535 vsnprintf(temp, sizeof(temp), format, args);
5536 temp[sizeof(temp)-1] = 0;
5543 undoBuffer.dontRecord--;
5556 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5559 else if(this.line.prev)
5561 EditLine line = this.line.prev;
5565 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5573 this.selLine = this.line;
5578 SetViewToCursor(true);
5583 Emptyline(this.line,this.y);
5584 this.selX = this.x = 0;
5587 this.selLine = this.line;
5589 SetViewToCursor(true);
5599 SetViewToCursor(true);
5607 SetViewToCursor(true);
5611 bool GoToLineNum(int lineNum)
5616 EditLine line = this.lines.first;
5617 for(c = 0; c < lineNum && line; c++, line = line.next);
5627 SetViewToCursor(true);
5634 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5635 bool GoToPosition(EditLine line, int y, int x)
5647 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5661 SetViewToCursor(true);
5668 void SetViewToCursor(bool setCaret)
5678 bool dontScroll = false;
5684 selected = selX != this.x || selY != y;
5691 checkLine = dropLine;
5697 checkLine = this.line;
5702 numLines = clientSize.h / space.h;
5704 // This is broken. The EditBox now doesn't do anything different when adding to it,
5705 // regardless of the previous scrolling position. It should be read and then set again
5706 // if one wishes to preserve it.
5707 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5708 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5712 // Horizontal Adjustment
5713 if(!dontScroll && checkLine)
5717 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5720 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5726 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5727 viewX = x - clientSize.w+space.w;
5728 if(x < this.viewX + clientSize.w/2 - space.w)
5729 viewX = Max(0, x - clientSize.w/2 + space.w);
5737 // Vertical Adjustment
5738 if(viewY > checkY) viewY = checkY;
5739 if(viewY + numLines <= checkY)
5741 if(clientSize.h >= space.h)
5743 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5753 for(;dropLine && dropLine.prev && dropY >= numLines;)
5755 dropLine = dropLine.prev;
5759 for(;this.line && this.line.prev && this.y >= numLines;)
5761 this.line = this.line.prev;
5766 SetScrollPosition(viewX, viewY * this.space.h);
5768 UpdateCaretPosition(setCaret);
5774 selLine = this.line;
5784 void CenterOnCursor()
5786 int numLines = clientSize.h / this.space.h;
5787 int y = this.y - numLines / 2;
5789 bool figureSyntax = false;
5790 EditLine oldViewLine = viewLine;
5791 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5796 for(;y < this.viewY; y++)
5798 this.viewLine = this.viewLine.prev;
5799 style.recomputeSyntax = true;
5801 for(;y > this.viewY; y--)
5803 this.viewLine = this.viewLine.next;
5804 figureSyntax = true;
5807 FigureStartSyntaxStates(oldViewLine, false);
5811 SetScrollPosition(this.viewX, viewY * this.space.h);
5812 UpdateCaretPosition(true);
5816 void SetCursorToView()
5827 numLines = clientSize.h / this.space.h;
5831 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5832 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5836 EditLine oldLine = this.line;
5837 bool lastOne = false;
5838 EditLine oldViewLine = this.viewLine;
5839 bool figureSyntax = false;
5841 if(this.y >= this.lineCount-1) return;
5843 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5845 if(this.viewY + numLines < this.lines.count)
5847 this.viewLine = this.viewLine.next;
5849 figureSyntax = true;
5851 else if(c && !lastOne)
5860 FigureStartSyntaxStates(oldViewLine, false);
5861 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5864 SetViewToCursor(false);
5873 if(this.y == 0) return;
5875 numLines = clientSize.h / this.space.h;
5879 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5880 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5884 EditLine oldLine = this.line;
5886 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5891 if(this.viewLine.prev)
5893 this.viewLine = this.viewLine.prev;
5895 style.recomputeSyntax = true;
5899 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5902 SetViewToCursor(false);
5909 if(this.viewLine.prev)
5912 // this.viewLine = this.viewLine.prev;
5915 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5922 if(this.viewLine.next)
5925 // this.viewLine = this.viewLine.next;
5928 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5935 EditLine l1, l2, line;
5936 int x1, x2, nx1, nx2;
5941 if(!this.selLine) return 0;
5942 if(this.selLine == this.line && this.selX == this.x) return 0;
5943 if(this.selY < this.y)
5950 else if(this.selY > this.y)
5957 else if(this.selX < this.x)
5959 l1 = l2 = this.line;
5965 l1 = l2 = this.line;
5969 nx1 = Min(x1,l1.count);
5970 nx2 = Min(x2,l2.count);
5972 // Find Number of Bytes Needed
5974 for(line = l1; line; line = line.next)
5976 if(line == l1) start = nx1; else start = 0;
5977 if(line == l2) end = nx2; else end = line.count;
5979 if(style.freeCaret && line == l2)
5982 count = Max(x2-Max(x1,l1.count),0);
5984 count = Max(x2-l2.count,0);
5988 if(line == l2) break;
5989 // Add Carriage Return / line Feed
5996 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
6000 if(!reorder || this.selY < this.y)
6002 if(l1) *l1 = this.selLine;
6003 if(y1) *y1 = this.selY;
6004 if(x1) *x1 = this.selX;
6005 if(l2) *l2 = this.line;
6006 if(y2) *y2 = this.y;
6007 if(x2) *x2 = this.x;
6009 else if(this.selY > this.y)
6011 if(l1) *l1 = this.line;
6012 if(y1) *y1 = this.y;
6013 if(x1) *x1 = this.x;
6014 if(l2) *l2 = this.selLine;
6015 if(y2) *y2 = this.selY;
6016 if(x2) *x2 = this.selX;
6018 else if(this.selX < this.x)
6020 if(l1) *l1 = this.line;
6021 if(y1) *y1 = this.selY;
6022 if(x1) *x1 = this.selX;
6023 if(l2) *l2 = this.line;
6024 if(y2) *y2 = this.y;
6025 if(x2) *x2 = this.x;
6029 if(l1) *l1 = this.line;
6030 if(y1) *y1 = this.y;
6031 if(x1) *x1 = this.x;
6032 if(l2) *l2 = this.line;
6033 if(y2) *y2 = this.selY;
6034 if(x2) *x2 = this.selX;
6039 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6041 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6043 this.selLine = (EditLine)l1;
6046 this.line = (EditLine)l2;
6050 SetViewToCursor(true);
6054 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6056 EditLine l1, l2, line;
6057 int x1, x2, nx1, nx2;
6088 nx1 = Min(x1,l1.count);
6089 nx2 = Min(x2,l2.count);
6092 for(line = l1; line; line = line.next)
6094 if(line == l1) start = nx1; else start = 0;
6095 if(line == l2) end = nx2; else end = line.count;
6098 CopyBytes(text, line.buffer + start, end - start);
6101 numChars += end-start;
6103 if(style.freeCaret && line == l2 && addSpaces)
6106 count = Max(x2-Max(x1,l1.count),0);
6108 count = Max(x2-l2.count,0);
6111 FillBytes(text,' ',count);
6117 if(line == l2) break;
6129 // '\0' terminate Terminate
6136 void GetSel(char * text, bool addCr)
6138 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6141 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6159 int size = SelSize();
6162 // Try to allocate memory
6163 ClipBoard clipBoard { };
6164 if(clipBoard.Allocate(size+1))
6166 GetSel(clipBoard.memory, true);
6179 ClipBoard clipBoard { };
6180 if(clipBoard.Load())
6181 PutS(clipBoard.memory);
6193 SetViewToCursor(true);
6199 void DeleteSelection()
6204 SetViewToCursor(true);
6210 void Save(File f, bool cr)
6213 savedAction = undoBuffer.curAction;
6215 for(line = this.lines.first; line; line = line.next)
6217 f.Write(line.buffer, line.count, 1);
6220 if(cr) f.Putc('\r');
6226 #define BUFFER_SIZE 16384
6229 undoBuffer.dontRecord++;
6232 char buffer[BUFFER_SIZE];
6236 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6237 buffer[count] = '\0';
6243 undoBuffer.dontRecord--;
6244 undoBuffer.count = 0;
6245 undoBuffer.curAction = 0;
6246 itemEditUndo.disabled = undoBuffer.curAction == 0;
6247 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6250 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6254 bool firstPass = true;
6255 EditBoxFindResult result = found;
6257 if(!this.line) return notFound;
6260 for(line = this.line;;)
6268 line = this.lines.first;
6274 line = this.lines.last;
6275 num = this.lineCount - 1;
6281 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6283 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6287 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6290 if(line == this.line && !firstPass) break;
6308 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6312 int searchLen = strlen(text);
6313 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6315 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6316 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6318 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6325 bool OnKeyDown(Key key, unichar ch)
6328 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6330 if(!NotifyKeyDown(master, this, key, ch))
6340 this.mouseMove = false;
6341 OnLeftButtonUp(0,0,0);
6342 SetViewToCursor(true);
6352 public class EditBoxStream : File
6355 BufferLocation start, sel;
6362 EditBox editBox = this.editBox;
6364 editBox.x = start.x;
6365 editBox.y = start.y;
6366 editBox.line = start.line;
6368 editBox.selX = sel.x;
6369 editBox.selY = sel.y;
6370 editBox.selLine = sel.line;
6372 editBox.SetViewToCursor(true);
6373 //editBox.ComputeColumn();
6377 property EditBox editBox
6385 start.line = value.line;
6389 sel.line = value.selLine;
6392 value.GoToHome(true);
6395 get { return editBox; }
6398 uint Read(byte * buffer, uint size, uint count)
6401 EditBox editBox = this.editBox;
6402 EditLine line = editBox.line;
6408 for(;read < count && line; line = (*&line.next))
6410 int numBytes = Min(count - read, (*&line.count) - x);
6413 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6418 for(;read < count && x < (*&line.count);)
6420 buffer[read++] = (*&line.buffer)[x++];
6427 buffer[read++] = '\n';
6432 if(x == (*&line.count) && (*&line.next))
6441 editBox.line = editBox.selLine = line;
6442 editBox.x = editBox.selX = x;
6443 editBox.y = editBox.selY = y;
6448 bool Seek(int pos, FileSeekMode mode)
6451 EditBox editBox = this.editBox;
6452 EditLine line = editBox.line;
6454 if(mode == FileSeekMode::start)
6456 pos = pos - this.pos;
6466 for(;read < pos && line; line = (*&line.next))
6468 int numBytes = Min(pos - read, (*&line.count) - x);
6471 read += numBytes; x += numBytes;
6474 /*for(;read < pos && x < (*&line.count);)
6502 for(;read < pos && line; line = (*&line.prev))
6504 int numBytes = Min(pos - read, x);
6510 /*for(;read < pos && x > 0;)
6520 x = (*&(*&line.prev).count);
6536 editBox.line = editBox.selLine = line;
6537 editBox.x = editBox.selX = x;
6538 editBox.y = editBox.selY = y;
6543 bool Puts(char * string)
6545 EditBox editBox = this.editBox;
6546 BufferLocation start { editBox.line, editBox.y, editBox.x };
6550 editBox.AddS(string);
6552 pos.line = editBox.line;
6556 this.start.AdjustAdd(start, pos);
6557 sel.AdjustAdd(start, pos);
6562 // NOTE: BYTE, NOT UNICODE CHARACTER!
6565 EditBox editBox = this.editBox;
6566 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6571 utf8Bytes[numBytes++] = ch;
6572 utf8Bytes[numBytes] = 0;
6573 if(UTF8Validate(utf8Bytes))
6575 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6577 pos.line = editBox.line;
6580 this.start.AdjustAdd(start, pos);
6581 sel.AdjustAdd(start, pos);
6588 bool Getc(char * ch)
6590 return Read(ch, 1, 1) ? true : false;
6593 void DeleteBytes(uint count)
6595 EditBox editBox = this.editBox;
6598 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6599 BufferLocation end = pos;
6604 for(;c < count && end.line && end.x < end.line.count;)
6609 if(c < count && end.line && end.line.next)
6611 end.line = end.line.next;
6620 start.AdjustDelete(pos, end);
6621 sel.AdjustDelete(pos, end);
6623 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);