1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 char * strchrmax(const char * s, int c, int max)
18 for(i = 0; i < max && (ch = s[i]); i++)
24 public class SyntaxColorScheme
28 Color charLiteralColor;
29 Color stringLiteralColor;
30 Color preprocessorColor;
32 private Array<Color> keywordColors { };
34 public property Container<Color> keywordColors
36 set { keywordColors.Copy((void *)value); }
37 get { return keywordColors; }
45 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
47 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
49 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
51 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
52 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
53 #define UTF8_GET_CHAR(string, numBytes) \
57 byte b = (string)[0]; \
60 numBytes = b ? 1 : 0; \
74 if(b & 0x08) { numBytes = 0; } \
83 for(i = 0; i<numBytes; i++) \
86 ch |= (b = (string)[i]) & mask; \
88 if(i > 1 && (!(b & 0x80) || (b & 0x40))) \
95 ch > 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF) || \
96 (ch < 0x80 && numBytes > 1) || \
97 (ch < 0x800 && numBytes > 2) || \
98 (ch < 0x10000 && numBytes > 3))\
108 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
109 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
112 bool inMultiLineComment:1, inPrep:1, escaped:1, continuedSingleLineComment:1, wasInMultiLine:1, continuedString:1, continuedQuotes:1;
114 bool recomputeSyntax:1;
115 bool cursorFollowsView:1;
117 // bool lineNumbers:1;
122 void UnregisterClass_EditBox()
125 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
134 extern int __ecereVMethodID_class_OnFree;
137 static class ArrayImpl
144 public class OldArray
149 void ** array = (void **)((ArrayImpl)this).array;
150 if(type.type == normalClass || type.type == noHeadClass)
152 for(c = 0; c<size; c++)
153 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
155 // TODO: Call OnFree for structClass
156 delete ((ArrayImpl)this).array;
165 if(((ArrayImpl)this).array)
169 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
172 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
181 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
188 public class UndoAction : struct
191 subclass(UndoAction) type;
194 virtual void Undo(void * data) { type.Undo(this, data); }
195 virtual void Redo(void * data) { type.Redo(this, data); }
197 virtual void Print(void * data) { type.Print(this, data); }
201 if(((Class)type).Destructor)
202 ((void (*)(void *))((Class)type).Destructor)(this);
206 public class UndoBuffer
208 Array<UndoAction> actions { size = 8 };
227 bool continued = true;
228 while(curAction > 0 && continued)
230 UndoAction action = actions[--curAction];
234 /*Print("Undoing: ");
235 action.Print(data);*/
240 continued = curAction > 0 && actions[curAction-1].continued;
246 bool continued = true;
247 while(curAction < count && continued)
249 UndoAction action = actions[curAction];
250 continued = action.continued;
256 /*Print("Redoing: ");
257 action.Print(data);*/
266 void Record(UndoAction action)
268 if(!dontRecord && !insideRedo)
270 if(curAction < count)
273 for(c = curAction; c < count; c++)
279 if(count >= actions.size)
280 actions.size += actions.size / 2;
283 /*Print("Recording: ");
284 action.Print(data);*/
288 if(!firstEvent && count > 0)
289 actions[count-1].continued = true;
292 actions[count++] = action;
295 if(actions.size > count + count / 2 && count + count / 2 >= 8)
296 actions.size = count + count / 2;
303 static class AddCharAction : UndoAction
307 int addedSpaces, addedTabs;
308 type = class(AddCharAction);
310 void Undo(EditBox editBox)
312 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
314 if(addedTabs || addedSpaces)
315 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
316 editBox.UpdateDirty();
319 void Redo(EditBox editBox)
321 editBox.GoToPosition(null, y, x);
323 editBox.UpdateDirty();
326 void Print(EditBox editBox)
328 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
333 static class AddTextAction : UndoAction
337 int addedSpaces, addedTabs;
338 type = class(AddTextAction);
341 void Print(EditBox editBox)
343 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
351 void Undo(EditBox editBox)
356 editBox.GoToPosition(null, y1, x1);
358 l2 = editBox.lines.first;
359 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
361 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
362 if(addedTabs || addedSpaces)
363 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
365 editBox.SetViewToCursor(true);
369 void Redo(EditBox editBox)
371 editBox.GoToPosition(null, y1, x1);
372 editBox.PutS(string);
376 static class DelTextAction : UndoAction
380 bool placeAfter, noHighlight;
382 type = class(DelTextAction);
385 void Print(EditBox editBox)
387 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
390 void Undo(EditBox editBox)
392 editBox.GoToPosition(null, y1, x1);
393 editBox.PutS(string);
397 editBox.GoToPosition(null, y1, x1);
403 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
404 //editBox.SetViewToCursor(true);
407 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
416 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
417 //editBox.SetViewToCursor(true);
420 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
424 void Redo(EditBox editBox)
429 editBox.GoToPosition(null, y1, x1);
432 l2 = editBox.lines.first;
433 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
435 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
436 editBox.SetViewToCursor(true);
446 static class ReplaceTextAction : UndoAction
448 int y1, x1, y2, x2, y3, x3;
452 int addedSpaces, addedTabs;
454 type = class(ReplaceTextAction);
457 void Print(EditBox editBox)
459 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
462 void Undo(EditBox editBox)
467 editBox.GoToPosition(null, y1, x1);
469 l3 = editBox.lines.first;
470 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
472 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
474 editBox.PutS(oldString);
475 if(addedSpaces || addedTabs)
476 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
478 //editBox.PutS(oldString);
481 editBox.GoToPosition(null, y1, x1);
484 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
490 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
494 void Redo(EditBox editBox)
499 editBox.GoToPosition(null, y1, x1);
501 l2 = editBox.lines.first;
502 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
504 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
506 editBox.PutS(newString);
516 static class MoveTextAction : UndoAction
518 int fy1, fx1, fy2, fx2;
520 type = class(MoveTextAction);
522 void Undo(EditBox editBox)
527 void Redo(EditBox editBox)
533 public class EditLine : struct
541 property const char * text
545 // Only works for single line edit for now...
546 EditBox editBox = this.editBox;
550 get { return this ? buffer : null; }
552 property EditLine prev { get { return this ? prev : null; } };
553 property EditLine next { get { return this ? next : null; } };
554 property int count { get { return this ? count : 0; } };
561 // This makes sure the buffer always contains at least count characters
562 // Keeps a count/2 pad for avoiding always reallocating memory.
563 bool AdjustBuffer(int count)
571 newSize = (count + (count >> 1));
573 // Shrink down the buffer
576 buffer = new char[newSize];
577 if(!buffer) return false;
581 CopyBytes(buffer, this.buffer, count);
584 this.buffer = buffer;
588 // Increase the buffer
589 else if(size < count)
591 buffer = new char[newSize];
592 if(!buffer) return false;
596 CopyBytes(buffer, this.buffer, this.count + 1); // size);
599 this.buffer = buffer;
608 public struct BufferLocation
613 void AdjustDelete(BufferLocation start, BufferLocation end)
615 // Location is before, nothing to do
616 if(y < start.y || (y == start.y && x < start.x))
618 // Location is inside deleted bytes, point to the start
619 if((y >= start.y && (y > start.y || x >= start.x)) &&
620 (y >= end.y && (y > end.y || x >= end.x)))
625 // Location is on another line
627 y -= end.y - start.y;
628 // Location is the last touched line
635 //if(start.line == end.line)
636 x -= end.x - start.x;
645 // Assuming no carriage return before first character ???? fixed?
646 void AdjustAdd(BufferLocation start, BufferLocation end)
648 int numLines = end.y - start.y;
655 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
658 for(c = 0, line = start.line; c<numLines; c++)
661 //x += numLines ? end.x : (end.x - start.x);
662 x += end.x - start.x;
669 public enum EditBoxFindResult { notFound, found, wrapped };
671 static const char * keyWords1[] =
674 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
675 "void", "char","int","float","double","signed","unsigned","static", "extern", "struct", "union", "typedef","enum",
677 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
678 "__attribute__", "__stdcall", "_stdcall",
679 "__declspec", "goto",
680 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
681 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
682 "__restrict__", "__restrict", "restrict",
685 "class", "private", "public",
687 "delete", "new", "new0", "renew", "renew0", "define",
690 "dllexport", "dllimport", "stdcall",
691 "subclass", "__on_register_module", "namespace", "using",
692 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
693 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
694 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
697 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
700 "this", "true", "false", "null", "value",
709 static const char * keyWords2[] =
711 "defined", "warning",
712 "include", "pragma", "elif", "ifdef", "ifndef", "endif", "undef", "line",
716 static const char ** keyWords[] = { keyWords1, keyWords2 };
717 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
718 //static int * keyLen[NUM_KEYWORD_GROUPS];
719 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
721 static char searchString[1025], replaceString[1025];
722 static bool matchCase = false, wholeWord = false, searchUp = false;
724 static GoToDialog goToDialog
726 autoCreate = false, isModal = true, text = $"Go To"
729 public class EditBox : CommonControl
731 class_property(icon) = "<:ecere>controls/editBox.png";
735 virtual bool Window::NotifyModified(EditBox editBox);
736 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
737 virtual void Window::NotifyUpdate(EditBox editBox);
738 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
739 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
740 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
742 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
743 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
744 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
746 virtual bool Window::NotifyUnsetModified(EditBox editBox);
748 // Why was this commented out?
749 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
750 // and the scrollArea property isn't compared properly either.
754 if(font) ComputeFont();
755 SetInitSize(initSize);
761 style.vScroll = true;
767 style.hScroll = true;
771 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
772 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
773 property bool readOnly
775 property_category $"Behavior"
778 style.readOnly = value;
779 itemEditCut.disabled = value || !selection;
780 itemEditDelete.disabled = value || !selection;
781 itemEditPaste.disabled = value;
783 get { return style.readOnly; }
785 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
786 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
787 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
788 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
789 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
790 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
791 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
792 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
793 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
794 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
795 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
796 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
797 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
798 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
799 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
800 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
801 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
802 property int numLines { get { return this ? lineCount : 0; } };
803 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
804 property int column { get { return col; } }; // TODO: Add Set
805 property int charPos { get { return x; } }; // TODO: Add Set
806 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
807 property EditLine lastLine { get { return lines.last; } };
808 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
809 property const char * contents
811 property_category $"Data"
816 undoBuffer.dontRecord++;
818 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
821 //SetViewToCursor(true);
824 undoBuffer.dontRecord--;
830 char * buffer = null;
833 /* Can't implement this right now because of memory leak... Need string reference counting...
840 for(line = lines.first; line; line = line.next)
841 len += strlen(line.buffer);
843 buffer = new char[len+1];
845 for(line = lines.first; line; line = line.next)
847 int lineLen = strlen(line.buffer);
848 memcpy(buffer + len, line.buffer, lineLen);
854 buffer = this.line ? this.line.buffer : null;
859 property bool overwrite { get { return overwrite; } };
860 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
862 property char * multiLineContents
866 char * buffer = null;
873 for(line = lines.first; line; line = line.next)
874 len += strlen(line.buffer)+1;
876 buffer = new char[len+1];
878 for(line = lines.first; line; line = line.next)
880 int lineLen = strlen(line.buffer);
881 memcpy(buffer + len, line.buffer, lineLen);
883 if(line.next) buffer[len++] = '\n';
896 return this.line.buffer;
901 void SetLineText(const char * text)
905 EditLine_SetText(this.line, text);
909 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
910 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
911 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
912 property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };
914 // selectionStart.line, selectionStart.column (With Set)
915 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
929 // Position of Caret (Not necessarily displayed position)
932 // Position of beginning of block (Block ends at (x,y))
934 // line is line at carret, selLine is line at beginning of block
935 EditLine line, selLine, dropLine;
936 // Mouse selection Moving virtual caret
941 // ViewX is x offset in pixels, ViewY is y offset in lines
943 // viewLine is first displayed line
946 // start and end of area to redraw
949 // MaxLine is the longest line
951 // MaxLength is the longest line's length
954 // MouseSelect is true once button is pressed, overwrite is true if mode is on
955 bool mouseSelect, mouseMove, overwrite, wordSelect;
956 // Timer is used for mouse selection scrolling
959 window = this, delay = 0.1;
968 OnMouseMove(mouseX, mouseY, -1);
973 // (mouseX,mouseY) is the position of the mouse in the edit box
978 void (* FontExtent)(Display display, Font font, const char * text, int len, int * width, int * height);
981 bool rightButtonDown;
984 UndoBuffer undoBuffer { data = this };
986 ColorAlpha selectionColor, selectionText;
987 SyntaxColorScheme colorScheme { };
992 Menu editMenu { menu, $"Edit", e };
995 editMenu, $"Cut\tCtrl+X", t, disabled = true;
997 bool NotifySelect(MenuItem item, Modifiers mods)
999 if(!(style.readOnly))
1004 MenuItem itemEditCopy
1006 editMenu, $"Copy\tCtrl+C", c, disabled = true;
1008 bool NotifySelect(MenuItem item, Modifiers mods)
1014 MenuItem itemEditPaste
1016 editMenu, $"Paste\tCtrl+V", p;
1018 bool NotifySelect(MenuItem item, Modifiers mods)
1020 if(!(style.readOnly))
1025 MenuItem itemEditDelete
1027 editMenu, $"Delete\tDel", d, disabled = true;
1029 bool NotifySelect(MenuItem item, Modifiers mods)
1031 if(!(style.readOnly))
1036 MenuDivider { editMenu };
1037 MenuItem itemEditSelectAll
1039 editMenu, $"Select All\tCtrl+A", a;
1041 bool NotifySelect(MenuItem item, Modifiers mods)
1047 MenuDivider { editMenu };
1048 MenuItem itemEditUndo
1050 editMenu, $"Undo\tCtrl+Z", u;
1053 bool NotifySelect(MenuItem item, Modifiers mods)
1059 MenuItem itemEditRedo
1061 editMenu, $"Redo\tCtrl+Y", o;
1064 bool NotifySelect(MenuItem item, Modifiers mods)
1070 MenuDivider { editMenu };
1073 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1075 bool NotifySelect(MenuItem item, Modifiers mods)
1078 Find(searchString, wholeWord, matchCase, false);
1080 itemEditFind.NotifySelect(this, item, mods);
1086 editMenu, $"Find Next\tF3", n, f3;
1088 bool NotifySelect(MenuItem item, Modifiers mods)
1091 Find(searchString, wholeWord, matchCase, true);
1093 itemEditFind.NotifySelect(this, item, mods);
1097 MenuItem itemEditFind
1099 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1101 bool NotifySelect(MenuItem item, Modifiers mods)
1105 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1106 searchUp = searchUp;
1109 // Fix dialog from above shouldn't be visible
1110 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1111 void NotifyDestroyed(Window window, DialogResult result)
1113 FindDialog dialog = (FindDialog) window;
1114 searchUp = dialog.searchUp;
1115 strcpy(searchString, dialog.searchString);
1116 matchCase = dialog.matchCase;
1117 wholeWord = dialog.wholeWord;
1126 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1128 bool NotifySelect(MenuItem item, Modifiers mods)
1130 ReplaceDialog dialog
1134 searchString = searchString,
1135 replaceString = replaceString,
1136 matchCase = matchCase,
1137 wholeWord = wholeWord,
1141 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1142 void NotifyDestroyed(Window window, DialogResult result)
1144 ReplaceDialog dialog = (ReplaceDialog)window;
1145 const char * replace = dialog.replaceString;
1147 strcpy(replaceString, replace);
1148 strcpy(searchString, dialog.searchString);
1149 matchCase = dialog.matchCase;
1150 wholeWord = dialog.wholeWord;
1157 MenuDivider { editMenu };
1160 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1162 bool NotifySelect(MenuItem item, Modifiers mods)
1164 goToDialog.editBox = this;
1165 goToDialog.master = master;
1166 goToDialog.Create();
1170 MenuDivider { editMenu };
1171 MenuItem itemEditInsertTab
1173 editMenu, $"Insert Tabs", i, checkable = true;
1175 bool NotifySelect(MenuItem item, Modifiers mods)
1177 style.useTab = item.checked;
1183 snapVertScroll = true;
1184 snapHorzScroll = true;
1188 static bool syntaxInit = false;
1193 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1195 for(c = 0; keyWords[g][c]; c++);
1196 //keyLen[g] = new int[c];
1197 for(c = 0; keyWords[g][c]; c++)
1199 keyLen[g][c] = strlen(keyWords[g][c]);
1203 colorScheme.commentColor = dimGray;
1204 colorScheme.charLiteralColor = crimson;
1205 colorScheme.stringLiteralColor = crimson;
1206 colorScheme.preprocessorColor = green;
1207 colorScheme.numberColor = teal;
1208 colorScheme.keywordColors = [ blue, blue ];
1211 FontExtent = Display::FontExtent;
1213 lines.offset = (uint)(uintptr)&((EditLine)0).prev;
1215 style = EditBoxBits { hScroll = true };
1217 // cursor = guiApp.GetCursor(IBeam);
1219 // Default Properties
1221 maxLineSize = 1024;*/
1224 maxLineSize = MAXINT;
1229 mouseSelect = this.mouseMove = false;
1232 x = selX = selY = 0;
1235 line = selLine = null;
1241 endY = clientSize.h;
1244 // Default space to 8 in case window is not constructed
1247 undoBuffer.dontRecord++;
1249 undoBuffer.dontRecord--;
1251 viewLine = lines.first;
1252 style.recomputeSyntax = true;
1258 UpdateCaretPosition(true);
1264 lines.Free(EditLine::Free);
1267 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1269 int count = wc - *renderStart;
1272 if(y + space.h >= box.top && y <= box.bottom)
1278 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1279 surface.TextFont(font);
1280 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1281 if(*x + w + XOFFSET > 0)
1282 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1287 w = numSpaces; // * space.w;
1288 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1289 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1290 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1298 bool CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1299 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, int *overwrite)
1305 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1309 *foreground = (*selected) ? selectionForeground : textColor;
1310 *background = selectionBackground;
1311 *opacity = *selected;
1318 if(this.overwrite && active)
1320 if((style.stuckCaret && wc == line.count && !line.next) ||
1321 (!mouseMove && line == this.line && wc == editX))
1330 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1334 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1335 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1336 bool inString = false;
1337 bool inQuotes = false;
1338 bool inPrep = reset ? false : style.inPrep;
1339 bool inSingleLineComment = false;
1340 bool escaped = reset ? false : style.escaped;
1341 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1342 bool continuedString = reset ? false : style.continuedString;
1343 bool continuedQuotes = reset ? false : style.continuedQuotes;
1345 EditLine line = reset ? lines.first : firstLine;
1346 // int maxBackUp = 1000, c;
1347 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1348 for(; line != viewLine; line = line.next)
1350 char * text = line.buffer;
1353 bool lastWasStar = false;
1354 bool firstWord = true;
1355 if(!escaped) inPrep = false;
1356 inSingleLineComment = continuedSingleLineComment;
1358 inString = continuedString;
1359 inQuotes = continuedQuotes;
1362 for(c = 0; (ch = text[c]); c++)
1364 bool wasEscaped = escaped;
1365 bool backLastWasStar = lastWasStar;
1366 bool backWasInMultiLine = wasInMultiLine;
1368 lastWasStar = false;
1369 wasInMultiLine = inMultiLineComment;
1372 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1374 if(text[c+1] == '/')
1376 inSingleLineComment = true;
1378 else if(text[c+1] == '*')
1380 inMultiLineComment = true;
1383 else if(backLastWasStar)
1384 inMultiLineComment = false;
1388 if(backWasInMultiLine) lastWasStar = true;
1390 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1392 if(inString && !wasEscaped)
1401 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1403 if(inQuotes && !wasEscaped)
1415 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1422 else if(ch != ' ' && ch != '\t')
1425 if(line.count && line.text[line.count - 1] == '\\')
1427 continuedSingleLineComment = inSingleLineComment;
1428 continuedString = inString;
1429 continuedQuotes = inQuotes;
1433 continuedSingleLineComment = false;
1434 continuedString = false;
1435 continuedQuotes = false;
1439 style.continuedSingleLineComment = continuedSingleLineComment;
1440 style.continuedString = continuedString;
1441 style.continuedQuotes = continuedQuotes;
1442 style.inMultiLineComment = inMultiLineComment;
1443 style.wasInMultiLine = wasInMultiLine;
1444 style.inPrep = inPrep;
1445 style.escaped = escaped;
1449 /*void OnDrawOverChildren(Surface surface)
1451 if(style.lineNumbers)
1453 int currentLineNumber = this.viewY + 1;
1456 for(i = 0; i * space.h < box.height; i++)
1458 // ********* LINE NUMBERING *********
1459 surface.SetForeground(Color{60, 60, 60});
1460 //Highlight current line
1461 if(this.caretY / space.h == currentLineNumber - 1)
1462 surface.SetBackground(Color{220, 220, 220});
1464 surface.SetBackground(Color{230, 230, 230});
1465 surface.textOpacity = true;
1467 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1468 if(currentLineNumber > this.lineCount)
1469 surface.WriteText(0,i*space.h+1," ",6);
1471 surface.WriteText(0,i*space.h+1,lineText,6);
1473 currentLineNumber++;
1478 void OnRedraw(Surface surface)
1482 bool selected = false, selection = true;
1484 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1485 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1486 Color defaultTextColor = property::foreground;
1489 int maxW = clientSize.w;
1491 Color foreground, background;
1494 // Overwrite Caret Stuff
1496 int overWriteX = 0, overWriteY = 0;
1499 // ****** SYNTAX STATES ******
1500 bool inMultiLineComment = style.inMultiLineComment;
1501 bool inString = false;
1502 bool inQuotes = false;
1503 bool inPrep = style.inPrep;
1504 bool inSingleLineComment = false;
1505 bool escaped = style.escaped;
1506 bool continuedSingleLineComment = style.continuedSingleLineComment;
1507 bool continuedString = style.continuedString;
1508 bool continuedQuotes = style.continuedQuotes;
1509 bool wasInMultiLine = style.wasInMultiLine;
1510 // ****** ************* ******
1513 defaultTextColor = Color { 85, 85, 85 };
1514 textColor = defaultTextColor;
1517 Abs(selectionBackground.r - property::background.r) +
1518 Abs(selectionBackground.g - property::background.g) +
1519 Abs(selectionBackground.b - property::background.b) < 92)
1521 selectionBackground = formColor;
1522 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1525 surface.TextFont(this.font);
1527 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1528 surface.Area(0,0,MAXINT,MAXINT);
1533 surface.SetBackground(formColor);
1534 surface.Area(0,0,clientSize.w, clientSize.h);
1537 if(this.selX == this.x && this.selY == this.y)
1547 editX = Min(this.x,this.line.count);
1548 selX = Min(this.selX,this.selLine.count);
1551 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1553 foreground = selected ? selectionForeground : textColor;
1554 background = selectionBackground;
1560 surface.SetForeground(foreground);
1561 surface.SetBackground(background);
1562 surface.TextOpacity(opacity);
1564 surface.GetBox(box);
1566 for(line = this.viewLine; line; line = line.next)
1568 int x = -this.viewX;
1572 Color newTextColor = textColor = defaultTextColor;
1573 bool lineComplete = false;
1576 // ****** SYNTAX HIGHLIGHTING ******
1577 bool lastWasStar = false;
1578 bool trailingSpace = false;
1579 bool firstWord = true;
1580 if(!escaped) inPrep = false;
1581 inSingleLineComment = continuedSingleLineComment;
1583 inString = continuedString;
1584 inQuotes = continuedQuotes;
1585 // *********************************
1587 /* === DEBUGGING TOOL FOR MAXLINE ===
1589 if(line == this.maxLine)
1591 surface.SetBackground(GREEN|0xFF000000);
1592 surface.TextOpacity(true);
1596 surface.TextOpacity(selected ? true : false);
1597 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1601 if(line == this.selLine && line == this.line)
1603 end = Max(line.count, this.x);
1604 end = Max(end, this.selX);
1606 else if(line == this.selLine)
1607 end = Max(line.count, this.selX);
1608 else if(line == this.line)
1609 end = Max(line.count, this.x);
1617 bool spacing = true;
1618 bool cantHaveWords = false;
1625 lineComplete = false;
1628 textColor = newTextColor;
1631 foreground = textColor;
1632 surface.SetForeground(textColor);
1636 for(; c<end && !cantHaveWords;)
1640 bufferLen += wordLen;
1644 /*if(line.count > 4000)
1651 for(; c<line.count; c++)
1653 unichar ch = line.buffer[c];
1654 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1655 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1656 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1657 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1658 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1661 trailingSpace = false;
1667 for(; c<line.count; c++)
1669 unichar ch = line.buffer[c];
1670 if(ch == '\t' || ch == ' ')
1672 cantHaveWords = true;
1673 if(ch == ' ' && c == line.count-1)
1674 trailingSpace = true;
1678 if(ch != ' ' && ch != '\t')
1683 if(c == line.count && c < end)
1692 cantHaveWords = true;
1697 lastWasStar = false;
1703 bool backEscaped = escaped;
1704 bool backLastWasStar = lastWasStar;
1705 bool backInMultiLineComment = inMultiLineComment;
1706 bool backInString = inString;
1707 bool backInQuotes = inQuotes;
1708 bool backInPrep = inPrep;
1709 bool backInSingleLineComment = inSingleLineComment;
1710 bool backWasInMultiLine = wasInMultiLine;
1712 char * word = line.buffer + c - wordLen;
1714 bool wasEscaped = escaped;
1716 lastWasStar = false;
1718 wasInMultiLine = inMultiLineComment;
1720 // Determine Syntax Highlighting
1721 newTextColor = defaultTextColor;
1724 if(inSingleLineComment || inMultiLineComment)
1726 newTextColor = colorScheme.commentColor;
1730 newTextColor = colorScheme.charLiteralColor;
1734 newTextColor = colorScheme.stringLiteralColor;
1738 newTextColor = colorScheme.preprocessorColor;
1740 if(wordLen == 1 && word[0] == '/')
1742 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1746 inSingleLineComment = true;
1747 newTextColor = colorScheme.commentColor;
1749 else if(word[1] == '*')
1751 inMultiLineComment = true;
1752 newTextColor = colorScheme.commentColor;
1755 else if(backLastWasStar)
1756 inMultiLineComment = false;
1758 else if(wordLen == 1 && word[0] == '*')
1760 if(backWasInMultiLine)
1763 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1765 if(inString && !wasEscaped)
1772 newTextColor = colorScheme.stringLiteralColor;
1775 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1777 if(inQuotes && !wasEscaped)
1782 newTextColor = colorScheme.charLiteralColor;
1785 else if(wordLen == 1 && word[0] == '\\')
1790 else if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1792 char * dot = word[wordLen] == '.' ? word + wordLen : (word[0] == '.' && (word == line.buffer || word[-1] == '-' || isspace(word[-1])) ? word : null);
1793 bool isReal = dot != null;
1800 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1803 exponent = strchrmax(word, 'p', wordLen);
1804 if(!exponent) exponent = strchrmax(word, 'P', wordLen);
1808 exponent = strchrmax(word, 'e', wordLen);
1809 if(!exponent) exponent = strchrmax(word, 'E', wordLen);
1811 isReal = exponent != null;
1814 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1816 strtol(word, &s, 0);
1822 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1825 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1829 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1832 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1835 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1836 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1837 default: valid = false;
1841 // Don't highlight numbers with too many decimal points
1842 if(s[0] == '.' && isdigit(s[1]))
1845 while(s[0] == '.' && isdigit(s[1]))
1847 int newWordLen = s - word;
1848 c += newWordLen - wordLen;
1849 wordLen = newWordLen;
1852 newWordLen = s - word;
1853 c += newWordLen - wordLen;
1854 wordLen = newWordLen;
1858 int newWordLen = s + i - word;
1859 newTextColor = colorScheme.numberColor;
1860 c += newWordLen - wordLen;
1861 wordLen = newWordLen;
1863 else if(dot && dot > word && dot < s)
1864 newTextColor = colorScheme.numberColor;
1869 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1874 newTextColor = wordLen == 1 ? colorScheme.keywordColors[1] : colorScheme.preprocessorColor;
1877 if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1879 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1881 const char ** keys = keyWords[g];
1882 int * len = keyLen[g];
1883 for(ccc = 0; keys[ccc]; ccc++)
1885 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1887 newTextColor = colorScheme.keywordColors[g];
1896 // If highlighting for this word is different, break
1897 if(newTextColor != textColor)
1901 // Better solution than going back?
1904 // Reset syntax flags
1905 escaped = backEscaped;
1906 lastWasStar = backLastWasStar;
1907 inMultiLineComment = backInMultiLineComment;
1908 inString = backInString;
1909 inQuotes = backInQuotes;
1910 inPrep = backInPrep;
1911 inSingleLineComment = backInSingleLineComment;
1912 wasInMultiLine = backWasInMultiLine;
1917 textColor = newTextColor;
1920 foreground = textColor;
1921 surface.SetForeground(textColor);
1928 // If we're not breaking, this can't be rendered as spacing anymore
1931 // Do word wrapping here
1937 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1942 w += numSpaces * space.w;
1944 if(x + viewX + w > maxW)
1946 // Avoid an endless loop until we fix wrapping
1947 if(c - wordLen > start)
1950 lineComplete = true;
1956 bufferLen += wordLen;
1961 int renderStart = start;
1963 bool flagTrailingSpace = false;
1967 // Render checking if we need to split because of selection or to find where to draw insert caret
1968 for(wc = start; wc < start + bufferLen; wc++)
1970 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1971 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1974 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1975 if(overWriteCh == '\t') overWriteCh = ' ';
1980 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1981 if(flagTrailingSpace) surface.SetBackground(red);
1982 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1991 surface.TextOpacity(opacity);
1992 surface.SetBackground(background);
1993 surface.SetForeground(foreground);
2000 if(wc < line.count && line.buffer[wc] == '\t')
2002 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
2006 numSpaces += space.w;
2010 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
2011 if(flagTrailingSpace) surface.SetBackground(red);
2012 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
2017 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2018 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2027 surface.TextOpacity(opacity);
2028 surface.SetBackground(background);
2029 surface.SetForeground(foreground);
2032 if(style.freeCaret && selected)
2034 surface.SetBackground(selectionBackground);
2035 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2036 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2041 if(style.freeCaret && selected)
2043 surface.SetBackground(selectionBackground);
2044 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2047 if(line.count && line.text[line.count - 1] == '\\')
2049 continuedSingleLineComment = inSingleLineComment;
2050 continuedString = inString;
2051 continuedQuotes = inQuotes;
2055 continuedSingleLineComment = false;
2056 continuedString = false;
2057 continuedQuotes = false;
2061 if(y > box.bottom) // >=clientSize.h)
2067 surface.TextOpacity(true);
2068 surface.SetForeground(black);
2069 surface.SetBackground(Color {255,255,85});
2070 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2074 void FixScrollArea()
2076 if(style.hScroll || style.vScroll)
2078 int width = maxLength + XOFFSET;
2079 int height = lineCount * space.h;
2080 if(style.freeCaret && line)
2085 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2089 if(selX > selLine.count)
2090 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2095 SetScrollLineStep(8, space.h);
2096 SetScrollArea(width, height, true);
2100 void ComputeLength(EditLine line)
2105 for(c = 0; c < line.count; )
2114 for(len = 0; c < line.count; c += numBytes)
2116 ch = line.buffer[c];
2117 numBytes = UTF8_NUM_BYTES(ch);
2119 if(ch == ' ' || ch == '\t')
2126 if(!len && ch == ' ')
2131 else if(!len && ch == '\t')
2133 w = (tabSize * space.w) - (x % (tabSize * space.w));
2137 FontExtent(display, font, line.buffer + start, len, &w, null);
2148 if(line.length > this.maxLength)
2150 this.maxLine = line;
2151 this.maxLength = line.length;
2153 if(style.autoSize) AutoSize();
2162 this.maxLine = null;
2164 for(line = lines.first; line; line = line.next)
2166 if(line.length > this.maxLength)
2168 this.maxLength = line.length;
2169 this.maxLine = line;
2173 if(style.autoSize) AutoSize();
2178 if(this.selY != this.y)
2180 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2184 void ComputeColumn()
2186 // Adjust new edit X position according to tabs
2187 int c, position = 0;
2190 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2192 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2194 position += this.tabSize - (position % this.tabSize);
2198 position += this.x - c;
2199 this.col = position;
2202 void DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2204 _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2207 bool HasCommentOrEscape(EditLine line)
2209 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2214 for(c = line.count-1; c >= 0; c--)
2216 char ch = line.buffer[c];
2222 else //if(ch != ' ' && ch != '\t')
2229 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2231 EditLine line = l1, next;
2233 int oldCount1, oldCount2;
2234 int y, firstViewY, firstY, firstDropY, firstSelY;
2237 DelTextAction action = null;
2238 bool hadComment = false;
2242 hadComment = HasCommentOrEscape(line);
2244 if(y2 > y1 || c2 > c1)
2246 if(start < l1.count)
2248 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2252 oldCount1 = l1.count;
2254 while(c1 < oldCount1)
2256 byte ch = buffer[c1];
2257 if(UTF8_IS_FIRST(ch)) break;
2261 oldCount2 = l2.count;
2263 while(c2 < oldCount2)
2265 byte ch = buffer[c2];
2266 if(UTF8_IS_FIRST(ch)) break;
2271 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2276 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2277 string = new char[len];
2278 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2279 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2283 //oldCount1 = l1.count;
2284 //oldCount2 = l2.count;
2287 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2288 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2291 if(c2 > oldCount2) c2 = oldCount2;
2292 if(!(style.freeCaret))
2293 if(c1 > oldCount1) c1 = oldCount1;
2294 newLineCount = c1 + l2.count-c2;
2300 buffer = new char[line.size ? line.size : 1];
2302 buffer = new char[line.size];
2303 // TODO: Better handling of these allocation failures
2304 if(!buffer) return extras;
2305 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2310 // TODO: Better handling of these allocation failures
2311 if(!line.AdjustBuffer(newLineCount))
2315 /*if(newLineCount > 4000 || newLineCount < 0)
2316 printf("Warning");*/
2318 line.count = newLineCount;
2320 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2325 action.addedSpaces = c1-oldCount1;
2327 if(action.addedSpaces > action.x1)
2333 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2334 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2336 line.buffer[line.count] = '\0';
2343 this.dropX -= c2-c1;
2344 this.dropX = Max(this.dropX,0);
2346 this.x = Max(this.x,0);
2353 firstViewY = this.viewY;
2355 firstDropY = this.dropY;
2356 firstSelY = this.selY;
2357 for(line = l1;line;line = next, y++)
2365 if(line == this.viewLine)
2367 if(this.viewLine.next)
2369 this.viewLine = this.viewLine.next;
2371 style.recomputeSyntax = true;
2375 this.viewLine = this.viewLine.prev;
2377 style.recomputeSyntax = true;
2380 else if(y < firstViewY)
2382 if(line == this.line)
2386 this.line = this.line.next;
2387 this.x = this.line.count;
2392 this.line = this.line.prev;
2393 this.x = this.line.count;
2400 if(line == this.dropLine)
2402 if(this.dropLine.next)
2404 this.dropLine = this.dropLine.next;
2405 this.dropX = this.dropLine.count;
2409 this.dropLine = this.dropLine.prev;
2410 this.dropX = this.dropLine.count;
2414 else if(y < firstDropY)
2416 if(line == this.selLine)
2418 if(this.selLine.next)
2420 this.selLine = this.selLine.next;
2421 this.selX = this.selLine.count;
2425 this.selLine = this.selLine.prev;
2426 this.selX = this.selLine.count;
2430 else if(y < firstSelY)
2434 if(line == l2) break;
2438 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2441 style.recomputeSyntax = true;
2446 bool DelSel(int * addedSpacesPtr)
2448 if(this.line != this.selLine || this.x != this.selX)
2450 if(this.selY < this.y)
2452 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2455 this.line = this.selLine;
2457 else if(this.selY > this.y)
2459 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2462 this.selLine = this.line;
2464 else if(this.selX < this.x)
2466 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2469 this.line = this.selLine;
2473 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2476 this.selLine = this.line;
2484 bool AddToLine(const char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2486 bool hadComment = false;
2487 // Add the line here
2488 EditLine line = this.line;
2490 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2491 if(!style.hScroll && created)
2493 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2498 // Lock if no place to display.
2501 else if(endX > this.x)
2502 max = Max(this.x, line.count);
2506 for(x = 0, c = 0; c < max+count; )
2510 const char * string;
2511 if(c < Min(this.x, line.count))
2512 string = line.buffer + c;
2515 else if(c < endX + count)
2516 string = stringLine + c - endX;
2518 string = line.buffer + c - endX - count;
2522 w = (tabSize * space.w) - (x % (tabSize * space.w));
2526 numBytes = UTF8_NUM_BYTES(*string);
2527 FontExtent(display, this.font, string, numBytes, &w, null);
2531 if(x >= clientSize.w)
2536 c += numBytes; // - 1;
2542 int addedSpaces = 0;
2545 // Add blank spaces if EES_FREECARET
2546 if(this.x > line.count)
2556 for(c = 0; c<line.count; c++)
2558 if(this.line.buffer[c] == '\t')
2559 position += this.tabSize - (position % this.tabSize);
2563 wantedPosition = position + (this.x - line.count);
2565 // A tab is too much...
2566 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2567 addedSpaces = wantedPosition - position;
2572 position += this.tabSize - (position % this.tabSize);
2573 // Add as many tabs as needed
2574 addedTabs += (wantedPosition - position) / this.tabSize;
2575 position += (addedTabs-1) * this.tabSize;
2576 // Finish off with spaces
2577 addedSpaces = wantedPosition - position;
2581 addedSpaces = this.x - line.count;
2584 this.x = Min(this.x, line.count);
2586 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2592 hadComment = HasCommentOrEscape(line);
2594 // Adjust the size of the line
2595 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2599 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2602 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2603 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2606 *addedTabsPtr = addedTabs;
2607 FillBytes(line.buffer+line.count,'\t',addedTabs);
2609 if(addedTabs > 4000 || addedTabs < 0)
2612 line.count += addedTabs;
2618 FillBytes(line.buffer+line.count,' ',addedSpaces);
2620 if(addedSpaces > 4000 || addedSpaces < 0)
2623 line.count += addedSpaces;
2624 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2626 else if(addedSpacesPtr)
2627 *addedSpacesPtr = 0;
2629 if(count > 4000 || count < 0)
2632 line.count += count;
2633 this.x += count + addedTabs + addedSpaces;
2636 this.selLine = this.line;
2638 line.buffer[line.count] = '\0';
2640 ComputeLength(line);
2645 hasComment = HasCommentOrEscape(line);
2646 if(!undoBuffer.insideRedo)
2648 int backDontRecord = undoBuffer.dontRecord;
2649 undoBuffer.dontRecord = 0;
2650 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2651 undoBuffer.dontRecord = backDontRecord;
2653 if(style.syntax && (hadComment || hasComment || line != this.line))
2655 style.recomputeSyntax = true;
2663 void Emptyline(EditLine line, int y)
2666 DelCh(line, y, 0, line.next, y+1, 0, true);
2668 DelCh(line, y, 0, line, y, line.count, true);
2671 void GoToEnd(bool deselect)
2675 this.line = this.lines.last;
2676 if(this.y != this.lineCount-1)
2678 else if (this.x != this.line.count)
2679 DirtyLine(this.lineCount-1);
2680 this.y = this.lineCount-1;
2681 this.x = this.line.count;
2688 void GoToHome(bool deselect)
2692 this.line = this.lines.first;
2695 else if (this.x !=0)
2696 DirtyLine(this.lineCount-1);
2705 // Returns true if it needs scrolling
2706 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2711 bool needHScroll = false;
2718 line = this.viewLine ? (void *)this.viewLine.prev : null;
2723 line = (void *)this.lines.first;
2728 py = Min(py, clientSize.h);
2730 py = Min(py, this.lineCount);
2732 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2738 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2741 px = Min(px,clientSize.w+this.space.w);
2745 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2748 if(tline) *tline = line;
2751 // Prevent divide by 0 from non valid this.font
2753 return (y < this.viewY) || needHScroll;
2755 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2759 // Minimal Update Management Functions
2763 this.endY = clientSize.h-1;
2764 // ErrorLog("DirtyAll\n");
2767 void DirtyEnd(int y)
2769 if((y - this.viewY)*this.space.h < this.startY)
2770 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2771 this.endY = clientSize.h-1;
2772 //ErrorLog("DirtyEnd %d\n", y);
2775 void DirtyLine(int y)
2779 if((y - this.viewY)*this.space.h < this.startY)
2780 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2781 if((y - this.viewY+1)*this.space.h > this.endY)
2782 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2784 //ErrorLog("DirtyLine %d\n", y);
2791 if(style.recomputeSyntax)
2793 FigureStartSyntaxStates(lines.first, true);
2794 style.recomputeSyntax = false;
2797 if(this.startY > this.endY) return;
2798 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2804 box.right = clientSize.w-1;
2805 box.top = this.startY;
2806 box.bottom = this.endY;
2809 this.startY = clientSize.h;
2813 bool IsMouseOnSelection()
2815 bool mouseOnSelection = false;
2818 int minY = Min(this.selY, this.y);
2819 int maxY = Max(this.selY, this.y);
2820 int minX = Min(this.selX, this.x);
2821 int maxX = Max(this.selX, this.x);
2823 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2825 if(maxX != minX || maxY != minY)
2827 if(y > minY && y < maxY)
2828 mouseOnSelection = true;
2829 else if(y == minY && y == maxY)
2830 mouseOnSelection = (x < maxX && x >= minX);
2834 mouseOnSelection = (x >= this.selX);
2835 else if(y == this.y)
2836 mouseOnSelection = (x >= this.x);
2841 mouseOnSelection = (x < this.selX);
2842 else if(y == this.y)
2843 mouseOnSelection = (x < this.x);
2846 return mouseOnSelection;
2849 void UpdateCaretPosition(bool setCaret)
2853 if(mouseMove || !style.noCaret)
2855 int max = this.mouseMove ? this.dropX : this.x;
2856 int y = this.mouseMove ? this.dropY : this.y;
2857 EditLine line = this.mouseMove ? this.dropLine : this.line;
2859 if(!(style.freeCaret))
2860 max = Min(max, line.count);
2862 if(FontExtent && display)
2864 for(c = 0; c < max; )
2873 for(len = 0; c < Min(max, line.count); c += numBytes)
2875 ch = line.buffer[c];
2876 numBytes = UTF8_NUM_BYTES(ch);
2878 if(ch == ' ' || ch == '\t')
2885 if(!len && ch == ' ')
2890 else if(!len && ch == '\t')
2892 w = (tabSize * space.w) - (x % (tabSize * space.w));
2896 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2908 caretY = y * this.space.h;
2910 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2917 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2918 NotifyCaretMove(master, this, y + 1, x + 1);
2924 void SelectionEnables()
2926 if((x != selX || y != selY) && !selection)
2930 itemEditCut.disabled = false;
2931 itemEditDelete.disabled = false;
2933 itemEditCopy.disabled = false;
2935 this.selection = true;
2937 else if((x == selX && y == selY) && selection)
2939 itemEditCut.disabled = true;
2940 itemEditCopy.disabled = true;
2941 itemEditDelete.disabled = true;
2943 this.selection = false;
2947 void SetSelectCursor()
2949 if(!inactive || !style.noSelect)
2952 cursor = guiApp.GetCursor(arrow);
2953 else if(this.mouseSelect)
2954 cursor = guiApp.GetCursor(iBeam);
2957 if(IsMouseOnSelection())
2958 cursor = guiApp.GetCursor(arrow);
2960 cursor = guiApp.GetCursor(iBeam);
2965 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2968 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(const 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)
3692 char * buffer = line1.buffer;
3693 for(i = x1; i < line1.count; i++)
3695 if(!IS_ALUNDER(buffer[i]))
3699 for(; i < line1.count; i++)
3701 // Delete trailing whitespace
3702 if(IS_ALUNDER(buffer[i]))
3712 // Avoid creating trailing spaces if there is no content on next line
3715 if(line2 && !line2.count)
3720 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3721 SetViewToCursor(true);
3730 if(!key.alt && !key.ctrl)
3734 bool stuffAfter = false;
3737 /*bool resetX = false;
3740 if(style.stuckCaret) GoToEnd(true);
3741 if(style.readOnly) break;
3742 if(!(style.multiLine)) break;
3744 for(c = 0; c<this.line.count && c<this.x; c++)
3746 if(this.line.buffer[c] == '\t')
3747 position += this.tabSize - (position % this.tabSize);
3748 else if(this.line.buffer[c] == ' ')
3754 // Prevent adding trailing spaces if at the head of a line
3755 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3767 if(this.x < this.line.count)
3770 //If last character is a { indent one tab
3771 c = Min(x, line.count);
3772 if(c > 0 && line.buffer[c - 1] == '{')
3774 //Except if the next non space character is a }
3775 bool indent = false;
3777 for(i = c; i <= this.line.count; i++) // indent will be set to true on nul terminating char
3778 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3780 if(this.line.buffer[i] != '}')
3785 position += this.tabSize;
3788 addString = new char[position + 2];
3789 addString[len++] = '\n';
3790 addString[len] = '\0';
3792 if(stuffAfter || !style.freeCaret)
3794 for(c = 0; c<position; )
3796 if(style.useTab && c + this.tabSize <= position)
3798 addString[len++] = '\t';
3803 addString[len++] = ' ';
3807 addString[len] = '\0';
3809 recordUndoEvent = true;
3812 EditLine prevLine = this.line.prev;
3815 // Nuke spaces if that is all that is left on previous line
3817 char * buffer = prevLine.buffer;
3818 for(i = 0; i < prevLine.count; i++)
3819 if(buffer[i] != ' ' && buffer[i] != '\t')
3821 if(i == prevLine.count)
3822 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3826 this.x = this.selX = backX;
3829 else */if(!stuffAfter && style.freeCaret)
3831 this.x = this.selX = position;
3835 SetViewToCursor(true);
3838 recordUndoEvent = false;
3846 if(style.stuckCaret) break;
3847 if(!(style.freeCaret))
3849 this.x = Min(this.x, this.line.count);
3850 this.selX = Min(this.selX, this.selLine.count);
3853 if(!shift) SelDirty();
3856 bool foundAlpha = false;
3859 EditLine line, lastLine = null;
3860 int lastC = 0, lastY = 0;
3862 for(line = this.line; (line && !found); line = line.prev, y--)
3867 if(this.x == 0 && line != this.line)
3876 if(line == this.line) start = this.x -1; else start = line.count-1;
3877 start = Min(start, line.count-1);
3879 for(c = start; c >= 0;)
3882 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3900 byte ch = line.buffer[c];
3901 if(UTF8_IS_FIRST(ch)) break;
3905 // No next word found,
3906 if(!found && ( this.x > 0 || (!line.count && this.x)))
3920 this.line = lastLine;
3931 byte * buffer = (byte *)line.buffer;
3934 byte ch = buffer[x];
3935 if(UTF8_IS_FIRST(ch)) break;
3952 if(!shift) _Deselect();
3953 SetViewToCursor(true);
3959 if(style.stuckCaret) break;
3960 if(!(style.freeCaret))
3962 this.x = Min(this.x, this.line.count);
3963 this.selX = Min(this.selX, this.selLine.count);
3966 if(!shift) SelDirty();
3967 if(!shift && (this.x != this.selX || this.y != this.selY));
3970 bool onAChar = false;
3971 if(this.selX != this.x || this.selY != this.y)
3973 if(this.x<this.line.count)
3974 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3976 if(key.shift && onAChar &&
3977 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3979 bool foundAlpha = false;
3981 EditLine line = null, lastLine = null;
3983 int lastC = 0, lastY = 0, lastNumBytes = 0;
3985 for(line = this.line; (line && !found); line = line.next, y++)
3987 int start = (line == this.line) ? this.x : 0;
3991 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3997 lastNumBytes = numBytes;
4007 if(!found && (c != this.x || line != this.line))
4020 this.x = lastC + lastNumBytes;
4022 this.line = lastLine;
4029 bool foundAlpha = false;
4034 for(line = this.line; (line && !found); line = line.next, y++)
4036 int start = (line == this.line) ? this.x : 0;
4040 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4056 // No next word found,
4057 if(!found && (c != this.x || line != this.line))
4061 this.x = line.count;
4073 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4077 byte * buffer = (byte *)line.buffer;
4080 byte ch = buffer[x];
4081 if(UTF8_IS_FIRST(ch)) break;
4102 if(!shift) _Deselect();
4103 SetViewToCursor(true);
4110 if(!style.vScroll || hasVertScroll) break;
4116 if(style.stuckCaret) break;
4118 if(!shift) SelDirty();
4128 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4132 if(!shift) _Deselect();
4134 SetViewToCursor(false);
4137 if(caretY == this.y * space.h)
4143 if(!style.freeCaret)
4144 this.x = Min(this.x, line.count);
4154 int sx = 0, sy = this.y * space.h;
4155 char * text = line.text;
4156 int maxW = clientSize.w - sx;
4157 display.FontExtent(font, " ", 1, null, &th);
4161 int startPos = textPos;
4164 bool lineComplete = false;
4166 if(!style.wrap && caretY == MAXINT)
4169 //textPos = line.count;
4170 //lineComplete = true;
4173 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4177 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4180 len = (nextSpace - (text + textPos));
4182 len = line.count - textPos;
4184 if(textPos < line.count)
4186 display.FontExtent(font, text + textPos, len, &w, null);
4188 if(nextSpace) { w += space.w; len++; }
4190 if(style.wrap && x + width + w > maxW && x > 0)
4192 lineComplete = true;
4202 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4206 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4209 if(this.x > line.count)
4212 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4213 len = this.x - startPos;
4214 display.FontExtent(font, text + startPos, len, &x, null);
4218 if(!shift) _Deselect();
4220 SetViewToCursor(false);
4224 if(sy == caretY - th || textPos >= line.count)
4226 if(textPos >= line.count)
4228 int c = textPos - 1;
4229 while(c > 0 && text[c] == ' ') c--;
4233 this.x = line.count;
4236 if(!shift) _Deselect();
4238 SetViewToCursor(false);
4243 } while(textPos < line.count);
4246 if(!shift) _Deselect();
4248 SetViewToCursor(false);
4257 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4258 if(!shift) SelDirty();
4259 this.line = this.line.prev;
4265 if(!shift) _Deselect();
4269 SetViewToCursor(false);
4275 return style.multiLine ? false : true;
4279 if(!style.vScroll || hasVertScroll)
4286 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;
4296 if(!shift) SelDirty();
4302 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4312 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4316 if(!shift) _Deselect();
4318 SetViewToCursor(false);
4321 while(!textPos || (style.freeCaret || textPos<line.count))
4323 int startPos = textPos;
4326 bool lineComplete = false;
4327 if(!style.wrap && sy <= caretY)
4329 textPos = line.count;
4330 lineComplete = true;
4332 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4336 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4339 len = (nextSpace - (text + textPos));
4341 len = line.count - textPos;
4343 if(textPos < line.count)
4345 display.FontExtent(font, text + textPos, len, &w, &th);
4347 if(nextSpace) { w += space.w; len++; }
4348 if(style.wrap && x + width + w > maxW && x > 0)
4350 lineComplete = true;
4360 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4364 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4367 if(this.x > line.count)
4370 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4372 len = this.x - startPos;
4373 display.FontExtent(font, text + startPos, len, &x, null);
4376 if(!shift) _Deselect();
4379 SetViewToCursor(false);
4385 this.x = line.count;
4388 if(!shift) _Deselect();
4391 SetViewToCursor(false);
4394 else if(textPos >= line.count && line.next)
4400 sy = this.y * this.space.h;
4401 sx = 0; //textBlock.startX;
4407 sx = 0; //textBlock.startX;
4415 this.x = Min(this.x, line.count);
4416 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4418 if(!shift) _Deselect();
4421 if(this.selX != this.x || this.selY != this.y)
4424 SetViewToCursor(false);
4431 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4432 if(!shift) SelDirty();
4433 this.line = this.line.next;
4437 if(!shift) _Deselect();
4440 if(this.selX != this.x || this.selY != this.y)
4448 return style.multiLine ? false : true;
4451 if(style.stuckCaret) break;
4452 if(!style.multiLine && key.ctrl) break;
4453 if(!(style.freeCaret))
4454 this.selX = Min(this.selX, this.selLine.count);
4456 if(!shift) SelDirty();
4459 this.line = this.lines.first;
4460 if(this.y != 0 || this.x != 0)
4470 EditLine line = this.line;
4472 for(c=0; line.buffer[c]; c++)
4473 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4475 if(overwrite || (shift && (c != 0 || this.x)))
4484 if(overwrite || (shift && this.x != 0))
4490 if(!shift) _Deselect();
4491 SetViewToCursor(true);
4497 if(style.stuckCaret) break;
4498 if(!style.multiLine && key.ctrl) break;
4499 if(!style.freeCaret)
4500 this.selX = Min(this.selX, this.selLine.count);
4502 if(!shift) SelDirty();
4507 else if(this.x != this.line.count)
4509 this.x = this.line.count;
4510 if(overwrite || shift)
4514 if(!shift) _Deselect();
4515 SetViewToCursor(true);
4520 if(style.tabKey && !key.ctrl)
4522 if(this.selY != this.y && style.tabSel)
4524 EditLine firstLine, lastLine;
4528 // Do multi line selection tabbing here
4529 if(this.selY < this.y)
4531 firstLine = this.selLine;
4532 lastLine = this.line;
4538 // Selecting going up
4539 firstLine = this.line;
4540 lastLine = this.selLine;
4547 for(line = firstLine; line; line = line.next, y++)
4549 if(line != lastLine || x)
4553 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4555 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4557 if(line.buffer[c] == '\t')
4562 else if(line.buffer[c] != ' ')
4567 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4570 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4571 char * string = new char[len];
4572 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4573 GetText(string, line, y, 0, line, y, lastC, false, false);
4576 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4577 if(!line.AdjustBuffer(line.count-lastC))
4583 if(line == lastLine) break;
4588 for(line = firstLine; line; line = line.next, y++)
4592 if(line != lastLine || x)
4596 if(line.count + 1 <= this.maxLineSize)
4598 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4600 if(!line.AdjustBuffer(line.count+1))
4603 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4605 AddCharAction action { ch = '\t', x = 0, y = y };
4609 memmove(line.buffer+1,line.buffer,line.size-1);
4611 line.buffer[0] = '\t';
4616 if(line.count + this.tabSize <= this.maxLineSize)
4619 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4620 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4622 if(!line.AdjustBuffer(line.count+this.tabSize))
4626 char * string = new char[this.tabSize + 1];
4627 memset(string, ' ', this.tabSize);
4628 string[this.tabSize] = '\0';
4629 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4632 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4633 line.count+=this.tabSize;
4634 for(c=0; c<this.tabSize; c++)
4635 line.buffer[c] = ' ';
4641 if(line == lastLine) break;
4644 ComputeLength(maxLine);
4656 char * addString = new char[this.tabSize + 1];
4658 if(!(style.freeCaret))
4660 this.x = Min(this.x, this.line.count);
4664 start = Min(this.x, this.selX);
4665 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4667 addString[len++] = ' ';
4675 SetViewToCursor(true);
4684 if(!(style.hScroll) || hasHorzScroll) break;
4685 if(this.viewX < this.maxLength)
4687 //this.viewX+=this.space.w*this.tabSize;
4689 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4696 if(!shift) _Deselect();
4708 if(!(style.hScroll) || hasHorzScroll) break;
4711 //this.viewX-=this.space.w*this.tabSize;
4712 //this.viewX = Max(this.viewX,0);
4714 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4715 // SetCursorToView();
4722 if(!shift) _Deselect();
4735 else if(!style.readOnly)
4739 if(!(style.readOnly))
4745 this.overwrite ^= 1;
4746 UpdateCaretPosition(true);
4751 NotifyOvrToggle(master, this, this.overwrite);
4763 if(style.noSelect) break;
4766 //Save current view position
4767 int tempX = this.viewX;
4768 int tempY = this.viewY;
4772 this.selLine = this.lines.first;
4773 this.y = this.lineCount-1;
4774 this.line = this.lines.last;
4775 this.x = this.line.count;
4778 SetViewToCursor(true);
4780 //Restore previous view position
4781 SetScrollPosition(tempX, tempY * this.space.h);
4787 // TOCHECK: Was there any good reason why we weren't returning false here?
4788 return false; // break;
4793 if(!(style.readOnly))
4801 if(style.readOnly) break;
4807 if(style.readOnly) break;
4812 if(style.readOnly) break;
4816 if(style.readOnly) break;
4820 if(style.readOnly) break;
4821 if(key.shift && key.code == rightBracket)
4823 //Only indent back if you are exactly at one tab.
4825 //bool whitespace = true;
4830 int indentwidth = 0;
4831 EditLine line = this.line;
4833 //Only remove one tab if there is nothing else on the line.
4834 for(i = 0; i < this.line.count; i++)
4836 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4850 for(pos = line.count - 1; pos >= 0; pos--)
4852 char c = line.buffer[pos];
4856 indentwidth += this.tabSize;
4858 //Counting backwards, so when you find a character, indentation is reset
4868 //Place the } to get an undo:
4871 this.x = this.line.count;
4875 putsize = indentwidth;
4877 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4879 newline = new char[putsize+2];
4880 newline[putsize] = '}';
4881 newline[putsize+1] = '\0';
4885 for(; i < indentwidth / this.tabSize; i++)
4887 for(;i < putsize; i++)
4896 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4908 void OnHScroll(ScrollBarAction action, int position, Key key)
4911 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4913 this.viewX = position;
4914 if(action != setRange)
4916 if(!this.mouseMove && style.cursorFollowsView)
4923 void OnVScroll(ScrollBarAction action, int position, Key key)
4925 int oldViewY = this.viewY;
4928 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4930 position /= this.space.h;
4932 if(position < this.viewY)
4934 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4935 style.recomputeSyntax = true;
4937 else if(position > this.viewY)
4939 EditLine oldViewLine = viewLine;
4940 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4941 FigureStartSyntaxStates(oldViewLine, false);
4944 if(action != setRange)
4946 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4949 if(this.x != this.selX || this.y != this.selY)
4953 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4956 int numLines = clientSize.h / this.space.h;
4958 if(Abs(this.viewY - oldViewY) < numLines)
4961 if(this.viewY > oldViewY)
4963 for(y = oldViewY; y <this.viewY; y++)
4964 DirtyLine(y + numLines);
4968 for(y = this.viewY; y <oldViewY; y++)
4969 DirtyLine(y + numLines);
4974 // Fix dirt of stuff before first line...
4975 if(this.viewY - oldViewY > 0)
4977 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4984 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4987 int length, endX = 0;
4989 ReplaceTextAction replaceAction = null;
4990 AddCharAction addCharAction = null;
4991 int addedSpaces = 0, addedTabs = 0;
4993 if(ch == '\r') return true;
4994 if(style.stuckCaret /*|EES_READONLY)*/ )
4997 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4999 if(!undoBuffer.dontRecord)
5001 if(selX != x || selY != y)
5006 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5007 oldString = new char[len];
5008 UTF32toUTF8Len(&ch, 1, buffer, 4);
5009 newString = CopyString(buffer);
5010 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5012 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5013 if(selY < y || (selY == y && selX < x))
5015 replaceAction.x1 = selX;
5016 replaceAction.y1 = selY;
5017 replaceAction.x2 = x;
5018 replaceAction.y2 = y;
5022 replaceAction.x1 = x;
5023 replaceAction.y1 = y;
5024 replaceAction.x2 = selX;
5025 replaceAction.y2 = selY;
5027 Record(replaceAction);
5028 undoBuffer.dontRecord++;
5032 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5033 Record(addCharAction);
5039 DelSel(&addedSpaces);
5040 if(this.lineCount+1 > this.maxLines)
5043 Emptyline(this.lines.first,0);
5047 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5049 // Make sure it fits, but we need a default line is this.font is too big for window
5050 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5053 if((this.y >= 0) && this.y < this.viewY)
5058 line = EditLine { };
5061 lines.Insert(this.line, line);
5062 line.editBox = this;
5068 // If we're displacing the lines from a current line ...
5071 if(this.line.buffer)
5074 if(this.line.count < endX) endX = this.line.count;
5075 length = this.line.count - endX;
5078 if(!line.AdjustBuffer(length))
5082 if(this.line.buffer)
5084 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5086 if(endX > 4000 || endX < 0)
5089 this.line.count = endX;
5090 this.line.buffer[this.line.count] = '\0';
5091 this.line.AdjustBuffer(this.line.count);
5092 ComputeLength(this.line);
5096 BufferLocation before = { this.line, this.y, this.x }, after;
5101 line.count = length;
5104 if(length > 4000 || length < 0)
5107 ComputeLength(this.line);
5112 line.buffer[line.count] = '\0';
5115 after.line = this.line, after.y = this.y, after.x = this.x;
5117 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5123 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5124 DelSel(&addedSpaces);
5125 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5126 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5127 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5131 this.selLine = this.line;
5135 replaceAction.x3 = x;
5136 replaceAction.y3 = y;
5137 replaceAction.addedSpaces = addedSpaces;
5138 replaceAction.addedTabs = addedTabs;
5139 undoBuffer.dontRecord--;
5143 addCharAction.x -= addedTabs * (tabSize-1);
5144 addCharAction.addedSpaces = addedSpaces;
5145 addCharAction.addedTabs = addedTabs;
5152 /****************************************************************************
5154 ****************************************************************************/
5157 bool AddCh(unichar ch)
5159 return _AddCh(ch, null, null);
5164 this.modified = true;
5165 NotifyUpdate(master, this);
5166 modifiedDocument = true;
5169 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5172 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5173 SetViewToCursor(true);
5181 itemEditUndo.disabled = undoBuffer.curAction == 0;
5182 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5188 if(savedAction == undoBuffer.curAction)
5190 modifiedDocument = false;
5192 NotifyUnsetModified(master, this);
5199 itemEditUndo.disabled = undoBuffer.curAction == 0;
5200 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5206 if(savedAction == undoBuffer.curAction)
5208 modifiedDocument = false;
5210 NotifyUnsetModified(master, this);
5214 void Record(UndoAction action)
5216 if(!undoBuffer.dontRecord)
5218 undoBuffer.Record(action);
5219 itemEditUndo.disabled = undoBuffer.curAction == 0;
5220 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5227 this.lines.first, 0,0,
5228 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5231 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5236 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5237 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5238 this.y = line2 ? y2 : (this.lineCount-1);
5239 this.line = line2 ? (EditLine)line2 : this.lines.last;
5242 SetViewToCursor(true);
5246 // TODO: Fix this vs modifiedDocument window property
5247 void SetModified(bool flag)
5251 this.modified = false;
5252 if(flag && !NotifyModified(master, this))
5253 this.modified = true;
5258 bool AddS(const char * string)
5265 int addedSpaces = 0, addedTabs = 0;
5266 AddTextAction action = null;
5267 ReplaceTextAction replaceAction = null;
5269 this.pasteOperation = true;
5271 if(style.stuckCaret /*|EES_READONLY)*/ )
5274 if(!undoBuffer.dontRecord)
5276 char * placeString = CopyString(string);
5277 if(!style.multiLine)
5281 for(i = 0; (ch = placeString[i]); i++)
5284 placeString[i] = '\0';
5285 placeString = renew placeString byte[i+1];
5290 if(selX != x || selY != y)
5294 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5295 oldString = new char[len];
5296 newString = placeString;
5297 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5299 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5300 if(selY < y || (selY == y && selX < x))
5302 replaceAction.x1 = selX;
5303 replaceAction.y1 = selY;
5304 replaceAction.x2 = x;
5305 replaceAction.y2 = y;
5309 replaceAction.x1 = x;
5310 replaceAction.y1 = y;
5311 replaceAction.x2 = selX;
5312 replaceAction.y2 = selY;
5314 Record(replaceAction);
5318 if(string[0] == '\n')
5319 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5321 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5329 undoBuffer.dontRecord++;
5330 DelSel(&addedSpaces);
5334 for(c = 0; string[c]; c++)
5336 if(string[c] == '\n' || string[c] == '\r')
5338 if(!AddToLine(line, count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5343 if(string[c] == '\n')
5352 // Reset for next line
5356 if(string[c] == '\r' && *line == '\n')
5368 // Why was this here?
5371 // Add the line here
5373 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5379 undoBuffer.dontRecord--;
5384 action.addedSpaces = addedSpaces;
5385 action.addedTabs = addedTabs;
5387 else if(replaceAction)
5389 replaceAction.y3 = y;
5390 replaceAction.x3 = x;
5391 replaceAction.addedSpaces = addedSpaces;
5392 replaceAction.addedTabs = addedTabs;
5395 UpdateCaretPosition(true);
5397 this.pasteOperation = false;
5410 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5411 SetViewToCursor(true);
5417 void PutCh(unichar ch)
5421 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5422 //if((ch >= 32) || ch == '\n')
5424 int addedSpaces = 0, addedTabs = 0;
5425 ReplaceTextAction replaceAction = null;
5426 AddCharAction addCharAction = null;
5429 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5431 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5436 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5437 oldString = new char[len];
5438 UTF32toUTF8Len(&ch, 1, buffer, 4);
5439 newString = CopyString(buffer);
5440 GetText(oldString, line, y, x, line, y, x+1, false, false);
5441 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5442 replaceAction.x1 = x;
5443 replaceAction.y1 = y;
5444 replaceAction.x2 = x+1;
5445 replaceAction.y2 = y;
5446 Record(replaceAction);
5448 undoBuffer.dontRecord++;
5449 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5450 undoBuffer.dontRecord--;
5452 else if(!undoBuffer.dontRecord)
5454 if(selX != x || selY != y)
5459 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5460 oldString = new char[len];
5461 UTF32toUTF8Len(&ch, 1, buffer, 4);
5462 newString = CopyString(buffer);
5463 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5464 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5465 if(selY < y || (selY == y && selX < x))
5467 replaceAction.x1 = selX;
5468 replaceAction.y1 = selY;
5469 replaceAction.x2 = x;
5470 replaceAction.y2 = y;
5474 replaceAction.x1 = x;
5475 replaceAction.y1 = y;
5476 replaceAction.x2 = selX;
5477 replaceAction.y2 = selY;
5479 Record(replaceAction);
5483 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5484 Record(addCharAction);
5487 undoBuffer.dontRecord++;
5488 result = _AddCh(ch, &addedSpaces, &addedTabs);
5491 replaceAction.x3 = x;
5492 replaceAction.y3 = y;
5493 replaceAction.addedSpaces = addedSpaces;
5494 replaceAction.addedTabs = addedTabs;
5498 addCharAction.x -= addedTabs * (tabSize-1);
5499 addCharAction.addedSpaces = addedSpaces;
5500 addCharAction.addedTabs = addedTabs;
5502 undoBuffer.dontRecord--;
5506 if(result) SetViewToCursor(true);
5510 void PutS(const char * string)
5515 SetViewToCursor(true);
5520 void Printf(const char * format, ...)
5524 char temp[MAX_F_STRING];
5526 va_start(args, format);
5527 vsnprintf(temp, sizeof(temp), format, args);
5528 temp[sizeof(temp)-1] = 0;
5534 void SetContents(const char * format, ...)
5538 undoBuffer.dontRecord++;
5540 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5543 char temp[MAX_F_STRING];
5545 va_start(args, format);
5546 vsnprintf(temp, sizeof(temp), format, args);
5547 temp[sizeof(temp)-1] = 0;
5554 undoBuffer.dontRecord--;
5567 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5570 else if(this.line.prev)
5572 EditLine line = this.line.prev;
5576 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5584 this.selLine = this.line;
5589 SetViewToCursor(true);
5594 Emptyline(this.line,this.y);
5595 this.selX = this.x = 0;
5598 this.selLine = this.line;
5600 SetViewToCursor(true);
5610 SetViewToCursor(true);
5618 SetViewToCursor(true);
5622 bool GoToLineNum(int lineNum)
5627 EditLine line = this.lines.first;
5628 for(c = 0; c < lineNum && line; c++, line = line.next);
5638 SetViewToCursor(true);
5645 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5646 bool GoToPosition(EditLine line, int y, int x)
5658 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5672 SetViewToCursor(true);
5679 void SetViewToCursor(bool setCaret)
5688 bool dontScroll = false;
5694 selected = selX != this.x || selY != y;
5701 checkLine = dropLine;
5707 checkLine = this.line;
5712 numLines = clientSize.h / space.h;
5714 // This is broken. The EditBox now doesn't do anything different when adding to it,
5715 // regardless of the previous scrolling position. It should be read and then set again
5716 // if one wishes to preserve it.
5717 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5718 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5722 // Horizontal Adjustment
5723 if(!dontScroll && checkLine)
5727 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5730 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5736 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5737 viewX = x - clientSize.w+space.w;
5738 if(x < this.viewX + clientSize.w/2 - space.w)
5739 viewX = Max(0, x - clientSize.w/2 + space.w);
5747 // Vertical Adjustment
5748 if(viewY > checkY) viewY = checkY;
5749 if(viewY + numLines <= checkY)
5751 if(clientSize.h >= space.h)
5753 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5763 for(;dropLine && dropLine.prev && dropY >= numLines;)
5765 dropLine = dropLine.prev;
5769 for(;this.line && this.line.prev && this.y >= numLines;)
5771 this.line = this.line.prev;
5776 SetScrollPosition(viewX, viewY * this.space.h);
5778 UpdateCaretPosition(setCaret);
5784 selLine = this.line;
5794 void CenterOnCursor()
5796 int numLines = clientSize.h / this.space.h;
5797 int y = this.y - numLines / 2;
5799 bool figureSyntax = false;
5800 EditLine oldViewLine = viewLine;
5801 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5806 for(;y < this.viewY; y++)
5808 this.viewLine = this.viewLine.prev;
5809 style.recomputeSyntax = true;
5811 for(;y > this.viewY; y--)
5813 this.viewLine = this.viewLine.next;
5814 figureSyntax = true;
5817 FigureStartSyntaxStates(oldViewLine, false);
5821 SetScrollPosition(this.viewX, viewY * this.space.h);
5822 UpdateCaretPosition(true);
5826 void SetCursorToView()
5837 numLines = clientSize.h / this.space.h;
5841 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5842 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5846 //EditLine oldLine = this.line;
5847 bool lastOne = false;
5848 EditLine oldViewLine = this.viewLine;
5849 bool figureSyntax = false;
5851 if(this.y >= this.lineCount-1) return;
5853 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5855 if(this.viewY + numLines < this.lines.count)
5857 this.viewLine = this.viewLine.next;
5859 figureSyntax = true;
5861 else if(c && !lastOne)
5870 FigureStartSyntaxStates(oldViewLine, false);
5871 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5874 SetViewToCursor(false);
5883 if(this.y == 0) return;
5885 numLines = clientSize.h / this.space.h;
5889 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5890 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5894 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5899 if(this.viewLine.prev)
5901 this.viewLine = this.viewLine.prev;
5903 style.recomputeSyntax = true;
5907 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5910 SetViewToCursor(false);
5917 if(this.viewLine.prev)
5920 // this.viewLine = this.viewLine.prev;
5923 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5930 if(this.viewLine.next)
5933 // this.viewLine = this.viewLine.next;
5936 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5943 EditLine l1, l2, line;
5944 int x1, x2, nx1, nx2;
5949 if(!this.selLine) return 0;
5950 if(this.selLine == this.line && this.selX == this.x) return 0;
5951 if(this.selY < this.y)
5958 else if(this.selY > this.y)
5965 else if(this.selX < this.x)
5967 l1 = l2 = this.line;
5973 l1 = l2 = this.line;
5977 nx1 = Min(x1,l1.count);
5978 nx2 = Min(x2,l2.count);
5980 // Find Number of Bytes Needed
5982 for(line = l1; line; line = line.next)
5984 if(line == l1) start = nx1; else start = 0;
5985 if(line == l2) end = nx2; else end = line.count;
5987 if(style.freeCaret && line == l2)
5990 count = Max(x2-Max(x1,l1.count),0);
5992 count = Max(x2-l2.count,0);
5996 if(line == l2) break;
5997 // Add Carriage Return / line Feed
6004 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
6008 if(!reorder || this.selY < this.y)
6010 if(l1) *l1 = this.selLine;
6011 if(y1) *y1 = this.selY;
6012 if(x1) *x1 = this.selX;
6013 if(l2) *l2 = this.line;
6014 if(y2) *y2 = this.y;
6015 if(x2) *x2 = this.x;
6017 else if(this.selY > this.y)
6019 if(l1) *l1 = this.line;
6020 if(y1) *y1 = this.y;
6021 if(x1) *x1 = this.x;
6022 if(l2) *l2 = this.selLine;
6023 if(y2) *y2 = this.selY;
6024 if(x2) *x2 = this.selX;
6026 else if(this.selX < this.x)
6028 if(l1) *l1 = this.line;
6029 if(y1) *y1 = this.selY;
6030 if(x1) *x1 = this.selX;
6031 if(l2) *l2 = this.line;
6032 if(y2) *y2 = this.y;
6033 if(x2) *x2 = this.x;
6037 if(l1) *l1 = this.line;
6038 if(y1) *y1 = this.y;
6039 if(x1) *x1 = this.x;
6040 if(l2) *l2 = this.line;
6041 if(y2) *y2 = this.selY;
6042 if(x2) *x2 = this.selX;
6047 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6049 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6051 this.selLine = (EditLine)l1;
6054 this.line = (EditLine)l2;
6058 SetViewToCursor(true);
6062 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6064 EditLine l1, l2, line;
6065 int x1, x2, nx1, nx2;
6096 nx1 = Min(x1,l1.count);
6097 nx2 = Min(x2,l2.count);
6100 for(line = l1; line; line = line.next)
6102 if(line == l1) start = nx1; else start = 0;
6103 if(line == l2) end = nx2; else end = line.count;
6106 CopyBytes(text, line.buffer + start, end - start);
6109 numChars += end-start;
6111 if(style.freeCaret && line == l2 && addSpaces)
6114 count = Max(x2-Max(x1,l1.count),0);
6116 count = Max(x2-l2.count,0);
6119 FillBytes(text,' ',count);
6125 if(line == l2) break;
6137 // '\0' terminate Terminate
6144 void GetSel(char * text, bool addCr)
6146 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6149 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6167 int size = SelSize();
6170 // Try to allocate memory
6171 ClipBoard clipBoard { };
6172 if(clipBoard.Allocate(size+1))
6174 GetSel(clipBoard.memory, true);
6187 ClipBoard clipBoard { };
6188 if(clipBoard.Load())
6189 PutS(clipBoard.memory);
6201 SetViewToCursor(true);
6207 void DeleteSelection()
6212 SetViewToCursor(true);
6218 void Save(File f, bool cr)
6221 savedAction = undoBuffer.curAction;
6223 for(line = this.lines.first; line; line = line.next)
6225 f.Write(line.buffer, line.count, 1);
6228 if(cr) f.Putc('\r');
6234 #define BUFFER_SIZE 16384
6237 undoBuffer.dontRecord++;
6240 char buffer[BUFFER_SIZE];
6244 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6245 buffer[count] = '\0';
6251 undoBuffer.dontRecord--;
6252 undoBuffer.count = 0;
6253 undoBuffer.curAction = 0;
6254 itemEditUndo.disabled = undoBuffer.curAction == 0;
6255 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6258 EditBoxFindResult Find(const char * text, bool matchWord, bool matchCase, bool isSearchDown)
6262 bool firstPass = true;
6263 EditBoxFindResult result = found;
6265 if(!this.line) return notFound;
6268 for(line = this.line;;)
6276 line = this.lines.first;
6282 line = this.lines.last;
6283 num = this.lineCount - 1;
6289 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6291 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6295 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6298 if(line == this.line && !firstPass) break;
6316 EditBoxFindResult FindInSelection(const char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6320 int searchLen = strlen(text);
6321 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6323 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6324 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6326 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6333 bool OnKeyDown(Key key, unichar ch)
6336 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6338 if(!NotifyKeyDown(master, this, key, ch))
6348 this.mouseMove = false;
6349 OnLeftButtonUp(0,0,0);
6350 SetViewToCursor(true);
6360 public class EditBoxStream : File
6363 BufferLocation start, sel;
6370 EditBox editBox = this.editBox;
6372 editBox.x = start.x;
6373 editBox.y = start.y;
6374 editBox.line = start.line;
6376 editBox.selX = sel.x;
6377 editBox.selY = sel.y;
6378 editBox.selLine = sel.line;
6380 editBox.SetViewToCursor(true);
6381 //editBox.ComputeColumn();
6385 property EditBox editBox
6393 start.line = value.line;
6397 sel.line = value.selLine;
6400 value.GoToHome(true);
6403 get { return editBox; }
6406 uint Read(byte * buffer, uint size, uint count)
6409 EditBox editBox = this.editBox;
6410 EditLine line = editBox.line;
6416 for(;read < count && line; line = (*&line.next))
6418 int numBytes = Min(count - read, (*&line.count) - x);
6421 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6426 for(;read < count && x < (*&line.count);)
6428 buffer[read++] = (*&line.buffer)[x++];
6435 buffer[read++] = '\n';
6440 if(x == (*&line.count) && (*&line.next))
6449 editBox.line = editBox.selLine = line;
6450 editBox.x = editBox.selX = x;
6451 editBox.y = editBox.selY = y;
6456 bool Seek(int pos, FileSeekMode mode)
6459 EditBox editBox = this.editBox;
6460 EditLine line = editBox.line;
6462 if(mode == FileSeekMode::start)
6464 pos = pos - this.pos;
6474 for(;read < pos && line; line = (*&line.next))
6476 int numBytes = Min(pos - read, (*&line.count) - x);
6479 read += numBytes; x += numBytes;
6482 /*for(;read < pos && x < (*&line.count);)
6510 for(;read < pos && line; line = (*&line.prev))
6512 int numBytes = Min(pos - read, x);
6518 /*for(;read < pos && x > 0;)
6528 x = (*&(*&line.prev).count);
6544 editBox.line = editBox.selLine = line;
6545 editBox.x = editBox.selX = x;
6546 editBox.y = editBox.selY = y;
6551 bool Puts(const char * string)
6553 EditBox editBox = this.editBox;
6554 BufferLocation start { editBox.line, editBox.y, editBox.x };
6558 editBox.AddS(string);
6560 pos.line = editBox.line;
6564 this.start.AdjustAdd(start, pos);
6565 sel.AdjustAdd(start, pos);
6570 // NOTE: BYTE, NOT UNICODE CHARACTER!
6573 EditBox editBox = this.editBox;
6574 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6579 utf8Bytes[numBytes++] = ch;
6580 utf8Bytes[numBytes] = 0;
6581 if(UTF8Validate((char *)utf8Bytes))
6583 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6585 pos.line = editBox.line;
6588 this.start.AdjustAdd(start, pos);
6589 sel.AdjustAdd(start, pos);
6596 bool Getc(char * ch)
6598 return Read(ch, 1, 1) ? true : false;
6601 void DeleteBytes(uint count)
6603 EditBox editBox = this.editBox;
6606 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6607 BufferLocation end = pos;
6612 for(;c < count && end.line && end.x < end.line.count;)
6617 if(c < count && end.line && end.line.next)
6619 end.line = end.line.next;
6628 start.AdjustDelete(pos, end);
6629 sel.AdjustDelete(pos, end);
6631 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);