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, xAdjustment;
308 type = class(AddCharAction);
310 void Undo(EditBox editBox)
312 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1) - xAdjustment);
314 if(addedTabs || addedSpaces)
316 editBox.DelCh(editBox.line, y, x - xAdjustment - (addedSpaces + addedTabs), editBox.line, y, x - xAdjustment, false);
317 editBox.GoToPosition(editBox.line, y, x);
319 editBox.UpdateDirty();
322 void Redo(EditBox editBox)
324 editBox.GoToPosition(null, y, x);
326 editBox.UpdateDirty();
329 void Print(EditBox editBox)
331 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
336 static class AddTextAction : UndoAction
340 int addedSpaces, addedTabs, xAdjustment;
341 type = class(AddTextAction);
344 void Print(EditBox editBox)
346 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
354 void Undo(EditBox editBox)
359 editBox.GoToPosition(null, y1, x1);
361 l2 = editBox.lines.first;
362 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
364 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
365 if(addedTabs || addedSpaces)
366 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
368 editBox.SetViewToCursor(true);
372 void Redo(EditBox editBox)
374 editBox.GoToPosition(null, y1, x1);
375 editBox.PutS(string);
379 static class DelTextAction : UndoAction
383 bool placeAfter, noHighlight;
385 type = class(DelTextAction);
388 void Print(EditBox editBox)
390 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
393 void Undo(EditBox editBox)
395 editBox.GoToPosition(null, y1, x1);
396 editBox.PutS(string);
400 editBox.GoToPosition(null, y1, x1);
406 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
407 //editBox.SetViewToCursor(true);
410 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
419 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
420 //editBox.SetViewToCursor(true);
423 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
427 void Redo(EditBox editBox)
432 editBox.GoToPosition(null, y1, x1);
435 l2 = editBox.lines.first;
436 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
438 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
439 editBox.SetViewToCursor(true);
449 static class ReplaceTextAction : UndoAction
451 int y1, x1, y2, x2, y3, x3;
455 int addedSpaces, addedTabs, xAdjustment;
457 type = class(ReplaceTextAction);
460 void Print(EditBox editBox)
462 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
465 void Undo(EditBox editBox)
470 editBox.GoToPosition(null, y1, x1);
472 l3 = editBox.lines.first;
473 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
475 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
477 editBox.PutS(oldString);
478 if(addedSpaces || addedTabs)
479 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
481 //editBox.PutS(oldString);
484 editBox.GoToPosition(null, y1, x1);
487 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
493 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
497 void Redo(EditBox editBox)
502 editBox.GoToPosition(null, y1, x1);
504 l2 = editBox.lines.first;
505 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
507 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
509 editBox.PutS(newString);
519 static class MoveTextAction : UndoAction
521 int fy1, fx1, fy2, fx2;
523 type = class(MoveTextAction);
525 void Undo(EditBox editBox)
530 void Redo(EditBox editBox)
536 public class EditLine : struct
544 property const char * text
548 // Only works for single line edit for now...
549 EditBox editBox = this.editBox;
553 get { return this ? buffer : null; }
555 property EditLine prev { get { return this ? prev : null; } };
556 property EditLine next { get { return this ? next : null; } };
557 property int count { get { return this ? count : 0; } };
564 // This makes sure the buffer always contains at least count characters
565 // Keeps a count/2 pad for avoiding always reallocating memory.
566 bool AdjustBuffer(int count)
574 newSize = (count + (count >> 1));
576 // Shrink down the buffer
579 buffer = new char[newSize];
580 if(!buffer) return false;
584 CopyBytes(buffer, this.buffer, count);
587 this.buffer = buffer;
591 // Increase the buffer
592 else if(size < count)
594 buffer = new char[newSize];
595 if(!buffer) return false;
599 CopyBytes(buffer, this.buffer, this.count + 1); // size);
602 this.buffer = buffer;
611 public struct BufferLocation
616 void AdjustDelete(BufferLocation start, BufferLocation end)
618 // Location is before, nothing to do
619 if(y < start.y || (y == start.y && x < start.x))
621 // Location is inside deleted bytes, point to the start
622 if((y >= start.y && (y > start.y || x >= start.x)) &&
623 (y >= end.y && (y > end.y || x >= end.x)))
628 // Location is on another line
630 y -= end.y - start.y;
631 // Location is the last touched line
638 //if(start.line == end.line)
639 x -= end.x - start.x;
648 // Assuming no carriage return before first character ???? fixed?
649 void AdjustAdd(BufferLocation start, BufferLocation end)
651 int numLines = end.y - start.y;
658 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
661 for(c = 0, line = start.line; c<numLines; c++)
664 //x += numLines ? end.x : (end.x - start.x);
665 x += end.x - start.x;
672 public enum EditBoxFindResult { notFound, found, wrapped };
674 static const char * keyWords1[] =
677 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
678 "void", "char","int","float","double","signed","unsigned","static", "extern", "struct", "union", "typedef","enum",
680 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
681 "__attribute__", "__stdcall", "_stdcall",
682 "__declspec", "goto",
683 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
684 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
685 "__restrict__", "__restrict", "restrict",
688 "class", "private", "public",
690 "delete", "new", "new0", "renew", "renew0", "define",
693 "dllexport", "dllimport", "stdcall",
694 "subclass", "__on_register_module", "namespace", "using",
695 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
696 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
697 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
700 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
703 "this", "true", "false", "null", "value",
712 static const char * keyWords2[] =
714 "defined", "warning",
715 "include", "pragma", "elif", "ifdef", "ifndef", "endif", "undef", "line",
719 static const char ** keyWords[] = { keyWords1, keyWords2 };
720 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
721 //static int * keyLen[NUM_KEYWORD_GROUPS];
722 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
724 static char searchString[1025], replaceString[1025];
725 static bool matchCase = false, wholeWord = false, searchUp = false;
727 static GoToDialog goToDialog
729 autoCreate = false, isModal = true, text = $"Go To"
732 public class EditBox : CommonControl
734 class_property(icon) = "<:ecere>controls/editBox.png";
738 virtual bool Window::NotifyModified(EditBox editBox);
739 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
740 virtual void Window::NotifyUpdate(EditBox editBox);
741 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
742 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
743 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
745 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
746 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
747 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
749 virtual bool Window::NotifyUnsetModified(EditBox editBox);
751 // Why was this commented out?
752 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
753 // and the scrollArea property isn't compared properly either.
757 if(font) ComputeFont();
758 SetInitSize(initSize);
764 style.vScroll = true;
770 style.hScroll = true;
774 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
775 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
776 property bool readOnly
778 property_category $"Behavior"
781 style.readOnly = value;
782 itemEditCut.disabled = value || !selection;
783 itemEditDelete.disabled = value || !selection;
784 itemEditPaste.disabled = value;
786 get { return style.readOnly; }
788 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
789 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
790 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
791 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
792 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
793 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
794 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
795 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
796 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
797 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
798 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
799 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
800 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
801 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
802 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
803 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
804 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
805 property int numLines { get { return this ? lineCount : 0; } };
806 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
807 property int column { get { return col; } }; // TODO: Add Set
808 property int charPos { get { return x; } }; // TODO: Add Set
809 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
810 property EditLine lastLine { get { return lines.last; } };
811 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
812 property const char * contents
814 property_category $"Data"
819 undoBuffer.dontRecord++;
821 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
824 //SetViewToCursor(true);
827 undoBuffer.dontRecord--;
833 char * buffer = null;
836 /* Can't implement this right now because of memory leak... Need string reference counting...
843 for(line = lines.first; line; line = line.next)
844 len += strlen(line.buffer);
846 buffer = new char[len+1];
848 for(line = lines.first; line; line = line.next)
850 int lineLen = strlen(line.buffer);
851 memcpy(buffer + len, line.buffer, lineLen);
857 buffer = this.line ? this.line.buffer : null;
862 property bool overwrite { get { return overwrite; } };
863 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
865 property char * multiLineContents
869 char * buffer = null;
876 for(line = lines.first; line; line = line.next)
877 len += strlen(line.buffer)+1;
879 buffer = new char[len+1];
881 for(line = lines.first; line; line = line.next)
883 int lineLen = strlen(line.buffer);
884 memcpy(buffer + len, line.buffer, lineLen);
886 if(line.next) buffer[len++] = '\n';
899 return this.line.buffer;
904 void SetLineText(const char * text)
908 EditLine_SetText(this.line, text);
912 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
913 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
914 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
915 property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };
917 // selectionStart.line, selectionStart.column (With Set)
918 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
932 // Position of Caret (Not necessarily displayed position)
935 // Position of beginning of block (Block ends at (x,y))
937 // line is line at carret, selLine is line at beginning of block
938 EditLine line, selLine, dropLine;
939 // Mouse selection Moving virtual caret
944 // ViewX is x offset in pixels, ViewY is y offset in lines
946 // viewLine is first displayed line
949 // start and end of area to redraw
952 // MaxLine is the longest line
954 // MaxLength is the longest line's length
957 // MouseSelect is true once button is pressed, overwrite is true if mode is on
958 bool mouseSelect, mouseMove, overwrite, wordSelect;
959 // Timer is used for mouse selection scrolling
962 window = this, delay = 0.1;
971 OnMouseMove(mouseX, mouseY, -1);
976 // (mouseX,mouseY) is the position of the mouse in the edit box
981 void (* FontExtent)(Display display, Font font, const char * text, int len, int * width, int * height, int prevGlyph, int * rPrevGlyph, int * overHang);
984 bool rightButtonDown;
987 UndoBuffer undoBuffer { data = this };
989 ColorAlpha selectionColor, selectionText;
990 SyntaxColorScheme colorScheme { };
995 Menu editMenu { menu, $"Edit", e };
998 editMenu, $"Cut\tCtrl+X", t, disabled = true;
1000 bool NotifySelect(MenuItem item, Modifiers mods)
1002 if(!(style.readOnly))
1007 MenuItem itemEditCopy
1009 editMenu, $"Copy\tCtrl+C", c, disabled = true;
1011 bool NotifySelect(MenuItem item, Modifiers mods)
1017 MenuItem itemEditPaste
1019 editMenu, $"Paste\tCtrl+V", p;
1021 bool NotifySelect(MenuItem item, Modifiers mods)
1023 if(!(style.readOnly))
1028 MenuItem itemEditDelete
1030 editMenu, $"Delete\tDel", d, disabled = true;
1032 bool NotifySelect(MenuItem item, Modifiers mods)
1034 if(!(style.readOnly))
1039 MenuDivider { editMenu };
1040 MenuItem itemEditSelectAll
1042 editMenu, $"Select All\tCtrl+A", a;
1044 bool NotifySelect(MenuItem item, Modifiers mods)
1050 MenuDivider { editMenu };
1051 MenuItem itemEditUndo
1053 editMenu, $"Undo\tCtrl+Z", u;
1056 bool NotifySelect(MenuItem item, Modifiers mods)
1062 MenuItem itemEditRedo
1064 editMenu, $"Redo\tCtrl+Y", o;
1067 bool NotifySelect(MenuItem item, Modifiers mods)
1073 MenuDivider { editMenu };
1076 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1078 bool NotifySelect(MenuItem item, Modifiers mods)
1081 Find(searchString, wholeWord, matchCase, false);
1083 itemEditFind.NotifySelect(this, item, mods);
1089 editMenu, $"Find Next\tF3", n, f3;
1091 bool NotifySelect(MenuItem item, Modifiers mods)
1094 Find(searchString, wholeWord, matchCase, true);
1096 itemEditFind.NotifySelect(this, item, mods);
1100 MenuItem itemEditFind
1102 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1104 bool NotifySelect(MenuItem item, Modifiers mods)
1108 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1109 searchUp = searchUp;
1112 // Fix dialog from above shouldn't be visible
1113 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1114 void NotifyDestroyed(Window window, DialogResult result)
1116 FindDialog dialog = (FindDialog) window;
1117 searchUp = dialog.searchUp;
1118 strcpy(searchString, dialog.searchString);
1119 matchCase = dialog.matchCase;
1120 wholeWord = dialog.wholeWord;
1129 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1131 bool NotifySelect(MenuItem item, Modifiers mods)
1133 ReplaceDialog dialog
1137 searchString = searchString,
1138 replaceString = replaceString,
1139 matchCase = matchCase,
1140 wholeWord = wholeWord,
1144 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1145 void NotifyDestroyed(Window window, DialogResult result)
1147 ReplaceDialog dialog = (ReplaceDialog)window;
1148 const char * replace = dialog.replaceString;
1150 strcpy(replaceString, replace);
1151 strcpy(searchString, dialog.searchString);
1152 matchCase = dialog.matchCase;
1153 wholeWord = dialog.wholeWord;
1160 MenuDivider { editMenu };
1163 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1165 bool NotifySelect(MenuItem item, Modifiers mods)
1167 goToDialog.editBox = this;
1168 goToDialog.master = master;
1169 goToDialog.Create();
1173 MenuDivider { editMenu };
1174 MenuItem itemEditInsertTab
1176 editMenu, $"Insert Tabs", i, checkable = true;
1178 bool NotifySelect(MenuItem item, Modifiers mods)
1180 style.useTab = item.checked;
1186 snapVertScroll = true;
1187 snapHorzScroll = true;
1191 static bool syntaxInit = false;
1196 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1198 for(c = 0; keyWords[g][c]; c++);
1199 //keyLen[g] = new int[c];
1200 for(c = 0; keyWords[g][c]; c++)
1202 keyLen[g][c] = strlen(keyWords[g][c]);
1206 colorScheme.commentColor = dimGray;
1207 colorScheme.charLiteralColor = crimson;
1208 colorScheme.stringLiteralColor = crimson;
1209 colorScheme.preprocessorColor = green;
1210 colorScheme.numberColor = teal;
1211 colorScheme.keywordColors = [ blue, blue ];
1214 FontExtent = Display::FontExtent2;
1216 lines.offset = (uint)(uintptr)&((EditLine)0).prev;
1218 style = EditBoxBits { hScroll = true };
1220 // cursor = guiApp.GetCursor(IBeam);
1222 // Default Properties
1224 maxLineSize = 1024;*/
1227 maxLineSize = MAXINT;
1232 mouseSelect = this.mouseMove = false;
1235 x = selX = selY = 0;
1238 line = selLine = null;
1244 endY = clientSize.h;
1247 // Default space to 8 in case window is not constructed
1250 undoBuffer.dontRecord++;
1252 undoBuffer.dontRecord--;
1254 viewLine = lines.first;
1255 style.recomputeSyntax = true;
1261 UpdateCaretPosition(true);
1267 lines.Free(EditLine::Free);
1270 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int * previousGlyph, int * oh, int numSpaces, bool drawSpaces, Box box)
1272 int count = wc - *renderStart;
1275 if(y + space.h >= box.top && y <= box.bottom)
1282 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1283 surface.TextFont(font);
1284 surface.TextExtent2(line.buffer + *renderStart, count, &w, null, *previousGlyph, null, &coh);
1286 if(*x + w + coh + XOFFSET > 0)
1288 surface.WriteText2(XOFFSET + *x,y, line.buffer + *renderStart, count, *previousGlyph, previousGlyph);
1290 //surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1296 w = numSpaces; // * space.w;
1297 if(*x + w + XOFFSET > 0 && (drawSpaces))
1298 surface.Area(XOFFSET + *x /*- 1*/ + *oh, y, XOFFSET + *x + w, y + space.h-1);
1299 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1307 bool CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1308 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, int *overwrite)
1314 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1318 *foreground = (*selected) ? selectionForeground : textColor;
1319 *background = selectionBackground;
1320 *opacity = *selected;
1327 if(this.overwrite && active)
1329 if((style.stuckCaret && wc == line.count && !line.next) ||
1330 (!mouseMove && line == this.line && wc == editX))
1339 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1343 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1344 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1345 bool inString = false;
1346 bool inQuotes = false;
1347 bool inPrep = reset ? false : style.inPrep;
1348 bool inSingleLineComment = false;
1349 bool escaped = reset ? false : style.escaped;
1350 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1351 bool continuedString = reset ? false : style.continuedString;
1352 bool continuedQuotes = reset ? false : style.continuedQuotes;
1354 EditLine line = reset ? lines.first : firstLine;
1355 // int maxBackUp = 1000, c;
1356 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1357 for(; line != viewLine; line = line.next)
1359 char * text = line.buffer;
1362 bool lastWasStar = false;
1363 bool firstWord = true;
1364 if(!escaped) inPrep = false;
1365 inSingleLineComment = continuedSingleLineComment;
1367 inString = continuedString;
1368 inQuotes = continuedQuotes;
1371 for(c = 0; (ch = text[c]); c++)
1373 bool wasEscaped = escaped;
1374 bool backLastWasStar = lastWasStar;
1375 bool backWasInMultiLine = wasInMultiLine;
1377 lastWasStar = false;
1378 wasInMultiLine = inMultiLineComment;
1381 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1383 if(text[c+1] == '/')
1385 inSingleLineComment = true;
1387 else if(text[c+1] == '*')
1389 inMultiLineComment = true;
1392 else if(backLastWasStar)
1393 inMultiLineComment = false;
1397 if(backWasInMultiLine) lastWasStar = true;
1399 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1401 if(inString && !wasEscaped)
1410 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1412 if(inQuotes && !wasEscaped)
1424 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1431 else if(ch != ' ' && ch != '\t')
1434 if(line.count && line.text[line.count - 1] == '\\')
1436 continuedSingleLineComment = inSingleLineComment;
1437 continuedString = inString;
1438 continuedQuotes = inQuotes;
1442 continuedSingleLineComment = false;
1443 continuedString = false;
1444 continuedQuotes = false;
1448 style.continuedSingleLineComment = continuedSingleLineComment;
1449 style.continuedString = continuedString;
1450 style.continuedQuotes = continuedQuotes;
1451 style.inMultiLineComment = inMultiLineComment;
1452 style.wasInMultiLine = wasInMultiLine;
1453 style.inPrep = inPrep;
1454 style.escaped = escaped;
1458 /*void OnDrawOverChildren(Surface surface)
1460 if(style.lineNumbers)
1462 int currentLineNumber = this.viewY + 1;
1465 for(i = 0; i * space.h < box.height; i++)
1467 // ********* LINE NUMBERING *********
1468 surface.SetForeground(Color{60, 60, 60});
1469 //Highlight current line
1470 if(this.caretY / space.h == currentLineNumber - 1)
1471 surface.SetBackground(Color{220, 220, 220});
1473 surface.SetBackground(Color{230, 230, 230});
1474 surface.textOpacity = true;
1476 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1477 if(currentLineNumber > this.lineCount)
1478 surface.WriteText(0,i*space.h+1," ",6);
1480 surface.WriteText(0,i*space.h+1,lineText,6);
1482 currentLineNumber++;
1487 static inline int countTabsExtraSpaces(char * buffer, int tabSize, int start, int end)
1489 // TODO: Handle tabs position properly with UTF-8 (and everywhere else)
1490 int p = 0, i, extra = 0;
1491 for(i = 0; i < end; i++)
1493 if(buffer[i] == '\t')
1495 int t = tabSize - (p % tabSize);
1506 void OnRedraw(Surface surface)
1510 bool selected = false, selection = true;
1512 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1513 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1514 Color defaultTextColor = property::foreground;
1517 int maxW = clientSize.w;
1519 Color foreground, background;
1522 // Overwrite Caret Stuff
1524 int overWriteX = 0, overWriteY = 0;
1527 // ****** SYNTAX STATES ******
1528 bool inMultiLineComment = style.inMultiLineComment;
1529 bool inString = false;
1530 bool inQuotes = false;
1531 bool inPrep = style.inPrep;
1532 bool inSingleLineComment = false;
1533 bool escaped = style.escaped;
1534 bool continuedSingleLineComment = style.continuedSingleLineComment;
1535 bool continuedString = style.continuedString;
1536 bool continuedQuotes = style.continuedQuotes;
1537 bool wasInMultiLine = style.wasInMultiLine;
1538 // ****** ************* ******
1540 // For drawing selection background
1541 EditLine selStartLine = this.y < this.selY ? this.line : this.selLine;
1542 EditLine selEndLine = this.y < this.selY ? this.selLine : this.line;
1543 int selStartX = this.y < this.selY || (this.y == this.selY && this.x < this.selX) ? this.x : this.selX;
1544 int selEndX = this.y > this.selY || (this.y == this.selY && this.x > this.selX) ? this.x : this.selX;
1545 ///////////////////////////////////
1548 defaultTextColor = Color { 85, 85, 85 };
1549 textColor = defaultTextColor;
1552 Abs(selectionBackground.r - property::background.r) +
1553 Abs(selectionBackground.g - property::background.g) +
1554 Abs(selectionBackground.b - property::background.b) < 92)
1556 selectionBackground = formColor;
1557 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1560 surface.TextFont(this.font);
1562 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1563 surface.Area(0,0,MAXINT,MAXINT);
1568 surface.SetBackground(formColor);
1569 surface.Area(0,0,clientSize.w, clientSize.h);
1572 if(this.selX == this.x && this.selY == this.y)
1582 editX = Min(this.x,this.line.count);
1583 selX = Min(this.selX,this.selLine.count);
1586 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1588 foreground = selected ? selectionForeground : textColor;
1589 background = selectionBackground;
1595 surface.SetForeground(foreground);
1596 surface.SetBackground(background);
1597 surface.TextOpacity(false);
1599 surface.GetBox(box);
1601 for(line = this.viewLine; line; line = line.next)
1603 int x = -this.viewX;
1607 Color newTextColor = textColor = defaultTextColor;
1608 bool lineComplete = false;
1611 // ****** SYNTAX HIGHLIGHTING ******
1612 bool lastWasStar = false;
1613 bool trailingSpace = false;
1614 bool firstWord = true;
1615 if(!escaped) inPrep = false;
1616 inSingleLineComment = continuedSingleLineComment;
1618 inString = continuedString;
1619 inQuotes = continuedQuotes;
1620 // *********************************
1622 /* === DEBUGGING TOOL FOR MAXLINE ===
1624 if(line == this.maxLine)
1626 surface.SetBackground(GREEN|0xFF000000);
1627 surface.TextOpacity(true);
1631 surface.TextOpacity(selected ? true : false);
1632 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1636 // Draw Selection Background all at once here
1637 if(selected || line == this.line || line == this.selLine)
1639 int sx = XOFFSET + x, sy = y;
1642 char * buffer = line.buffer;
1643 if(line != this.line && line != this.selLine)
1647 tw = clientSize.w - sx;
1652 surface.TextExtent2(buffer, line.count, &tw, &th, 0, null, &oh);
1653 tw += countTabsExtraSpaces(buffer, tabSize, 0, line.count) * space.w;
1656 else if(line == selStartLine)
1659 int start = Min(line.count, selStartX);
1660 int end = Min(line.count, selEndX);
1661 surface.TextExtent2(buffer, start, &tw, &th, 0, &prevGlyph, null);
1663 sx += countTabsExtraSpaces(buffer, tabSize, 0, start) * space.w;
1664 if(selStartX > start) sx += space.w * (selStartX - start);
1665 if(style.freeCaret && line != selEndLine)
1667 tw = clientSize.w - sx;
1670 else if(line != selEndLine)
1672 surface.TextExtent2(buffer + start, line.count - start, &tw, &th, prevGlyph, null, &oh);
1673 tw += countTabsExtraSpaces(buffer, tabSize, start, line.count) * space.w;
1677 surface.TextExtent2(buffer + start, end - start, &tw, &th, prevGlyph, null, &oh);
1678 tw += countTabsExtraSpaces(buffer, tabSize, start, end) * space.w;
1679 end = Max(end, selStartX);
1680 if(selEndX > end) { tw += space.w * (selEndX - end); th = space.h; }
1683 else if(line == selEndLine)
1685 int end = Min(line.count, selEndX);
1686 surface.TextExtent2(buffer, end, &tw, &th, 0, null, &oh);
1687 tw += countTabsExtraSpaces(buffer, tabSize, 0, end) * space.w;
1688 if(selEndX - end) { tw += space.w * (selEndX - end); th = space.h; }
1691 surface.Area(sx, sy, sx + tw-1, sy + th-1);
1694 if(line == this.selLine && line == this.line)
1696 end = Max(line.count, this.x);
1697 end = Max(end, this.selX);
1699 else if(line == this.selLine)
1700 end = Max(line.count, this.selX);
1701 else if(line == this.line)
1702 end = Max(line.count, this.x);
1710 bool spacing = true;
1711 bool cantHaveWords = false;
1718 lineComplete = false;
1721 textColor = newTextColor;
1724 foreground = textColor;
1725 surface.SetForeground(textColor);
1729 for(; c<end && !cantHaveWords;)
1733 bufferLen += wordLen;
1737 /*if(line.count > 4000)
1744 for(; c<line.count; c++)
1746 unichar ch = line.buffer[c];
1747 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1748 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1749 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1750 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1751 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1754 trailingSpace = false;
1760 for(; c<line.count; c++)
1762 unichar ch = line.buffer[c];
1763 if(ch == '\t' || ch == ' ')
1765 cantHaveWords = true;
1766 if(ch == ' ' && c == line.count-1)
1767 trailingSpace = true;
1771 if(ch != ' ' && ch != '\t')
1776 if(c == line.count && c < end)
1785 cantHaveWords = true;
1790 lastWasStar = false;
1796 bool backEscaped = escaped;
1797 bool backLastWasStar = lastWasStar;
1798 bool backInMultiLineComment = inMultiLineComment;
1799 bool backInString = inString;
1800 bool backInQuotes = inQuotes;
1801 bool backInPrep = inPrep;
1802 bool backInSingleLineComment = inSingleLineComment;
1803 bool backWasInMultiLine = wasInMultiLine;
1805 char * word = line.buffer + c - wordLen;
1807 bool wasEscaped = escaped;
1809 lastWasStar = false;
1811 wasInMultiLine = inMultiLineComment;
1813 // Determine Syntax Highlighting
1814 newTextColor = defaultTextColor;
1817 if(inSingleLineComment || inMultiLineComment)
1819 newTextColor = colorScheme.commentColor;
1823 newTextColor = colorScheme.charLiteralColor;
1827 newTextColor = colorScheme.stringLiteralColor;
1831 newTextColor = colorScheme.preprocessorColor;
1833 if(wordLen == 1 && word[0] == '/')
1835 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1839 inSingleLineComment = true;
1840 newTextColor = colorScheme.commentColor;
1842 else if(word[1] == '*')
1844 inMultiLineComment = true;
1845 newTextColor = colorScheme.commentColor;
1848 else if(backLastWasStar)
1849 inMultiLineComment = false;
1851 else if(wordLen == 1 && word[0] == '*')
1853 if(backWasInMultiLine)
1856 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1858 if(inString && !wasEscaped)
1865 newTextColor = colorScheme.stringLiteralColor;
1868 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1870 if(inQuotes && !wasEscaped)
1875 newTextColor = colorScheme.charLiteralColor;
1878 else if(wordLen == 1 && word[0] == '\\')
1883 else if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1885 char * dot = word[wordLen] == '.' ? word + wordLen : (word[0] == '.' && (word == line.buffer || word[-1] == '-' || isspace(word[-1])) ? word : null);
1886 bool isReal = dot != null;
1893 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1896 exponent = strchrmax(word, 'p', wordLen);
1897 if(!exponent) exponent = strchrmax(word, 'P', wordLen);
1901 exponent = strchrmax(word, 'e', wordLen);
1902 if(!exponent) exponent = strchrmax(word, 'E', wordLen);
1904 isReal = exponent != null;
1907 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1909 strtol(word, &s, 0);
1915 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1918 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1922 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1925 if(gotL > 2 || (isReal && (gotL == 2 || gotF)) || (gotL == 2 && (s[i-1] != ch)))
1928 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1929 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1930 default: valid = false;
1934 // Don't highlight numbers with too many decimal points
1935 if(s[0] == '.' && isdigit(s[1]))
1938 while(s[0] == '.' && isdigit(s[1]))
1940 int newWordLen = s - word;
1941 c += newWordLen - wordLen;
1942 wordLen = newWordLen;
1945 newWordLen = s - word;
1946 c += newWordLen - wordLen;
1947 wordLen = newWordLen;
1951 int newWordLen = s + i - word;
1952 newTextColor = colorScheme.numberColor;
1953 c += newWordLen - wordLen;
1954 wordLen = newWordLen;
1956 else if(dot && dot > word && dot < s)
1957 newTextColor = colorScheme.numberColor;
1962 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1967 newTextColor = wordLen == 1 ? colorScheme.keywordColors[1] : colorScheme.preprocessorColor;
1970 if(x < box.right && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1972 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1974 const char ** keys = keyWords[g];
1975 int * len = keyLen[g];
1976 for(ccc = 0; keys[ccc]; ccc++)
1978 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1980 newTextColor = colorScheme.keywordColors[g];
1989 // If highlighting for this word is different, break
1990 if(newTextColor != textColor)
1994 // Better solution than going back?
1997 // Reset syntax flags
1998 escaped = backEscaped;
1999 lastWasStar = backLastWasStar;
2000 inMultiLineComment = backInMultiLineComment;
2001 inString = backInString;
2002 inQuotes = backInQuotes;
2003 inPrep = backInPrep;
2004 inSingleLineComment = backInSingleLineComment;
2005 wasInMultiLine = backWasInMultiLine;
2010 textColor = newTextColor;
2013 foreground = textColor;
2014 surface.SetForeground(textColor);
2021 // If we're not breaking, this can't be rendered as spacing anymore
2024 // Do word wrapping here
2031 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null, 0, null, &oh);
2036 w += numSpaces * space.w;
2038 if(x + viewX + w > maxW)
2040 // Avoid an endless loop until we fix wrapping
2041 if(c - wordLen > start)
2044 lineComplete = true;
2050 bufferLen += wordLen;
2055 int renderStart = start;
2057 bool flagTrailingSpace = false;
2060 int previousGlyph = 0;
2062 // Render checking if we need to split because of selection or to find where to draw insert caret
2063 for(wc = start; wc < start + bufferLen; wc++)
2065 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
2066 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
2069 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
2070 if(overWriteCh == '\t') overWriteCh = ' ';
2075 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
2076 if(flagTrailingSpace) surface.SetBackground(red);
2077 FlushBuffer(surface, line, wc, &renderStart, &x, y, &previousGlyph, &overHang, numSpaces, flagTrailingSpace, box);
2078 if(flagTrailingSpace) surface.SetBackground(background);
2087 surface.SetForeground(foreground);
2094 if(wc < line.count && line.buffer[wc] == '\t')
2096 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
2100 numSpaces += space.w;
2104 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
2105 if(flagTrailingSpace) surface.SetBackground(red);
2106 FlushBuffer(surface, line, wc, &renderStart, &x, y, &previousGlyph, &overHang, numSpaces, flagTrailingSpace, box);
2107 if(flagTrailingSpace) surface.SetBackground(background);
2112 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2113 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2122 surface.SetBackground(background);
2123 surface.SetForeground(foreground);
2126 if(line.count && line.text[line.count - 1] == '\\')
2128 continuedSingleLineComment = inSingleLineComment;
2129 continuedString = inString;
2130 continuedQuotes = inQuotes;
2134 continuedSingleLineComment = false;
2135 continuedString = false;
2136 continuedQuotes = false;
2140 if(y > box.bottom) // >=clientSize.h)
2146 surface.TextOpacity(true);
2147 surface.SetForeground(black);
2148 surface.SetBackground(Color {255,255,85});
2149 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2153 void FixScrollArea()
2155 if(style.hScroll || style.vScroll)
2157 int width = maxLength + XOFFSET;
2158 int height = lineCount * space.h;
2159 if(style.freeCaret && line)
2164 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2168 if(selX > selLine.count)
2169 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2174 SetScrollLineStep(8, space.h);
2175 SetScrollArea(width, height, true);
2179 void ComputeLength(EditLine line)
2184 for(c = 0; c < line.count; )
2193 for(len = 0; c < line.count; c += numBytes)
2195 ch = line.buffer[c];
2196 numBytes = UTF8_NUM_BYTES(ch);
2198 if(ch == ' ' || ch == '\t')
2205 if(!len && ch == ' ')
2210 else if(!len && ch == '\t')
2212 w = (tabSize * space.w) - (x % (tabSize * space.w));
2218 FontExtent(display, font, line.buffer + start, len, &w, null, 0, null, &oh);
2230 if(line.length > this.maxLength)
2232 this.maxLine = line;
2233 this.maxLength = line.length;
2235 if(style.autoSize) AutoSize();
2244 this.maxLine = null;
2246 for(line = lines.first; line; line = line.next)
2248 if(line.length > this.maxLength)
2250 this.maxLength = line.length;
2251 this.maxLine = line;
2255 if(style.autoSize) AutoSize();
2260 if(this.selY != this.y)
2262 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2266 void ComputeColumn()
2268 // Adjust new edit X position according to tabs
2269 int c, position = 0;
2272 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2274 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2276 position += this.tabSize - (position % this.tabSize);
2280 position += this.x - c;
2281 this.col = position;
2284 void DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2286 _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2289 bool HasCommentOrEscape(EditLine line)
2291 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2296 for(c = line.count-1; c >= 0; c--)
2298 char ch = line.buffer[c];
2304 else //if(ch != ' ' && ch != '\t')
2311 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2313 EditLine line = l1, next;
2315 int oldCount1, oldCount2;
2316 int y, firstViewY, firstY, firstDropY, firstSelY;
2319 DelTextAction action = null;
2320 bool hadComment = false;
2324 hadComment = HasCommentOrEscape(line);
2326 if(y2 > y1 || c2 > c1)
2328 if(start < l1.count)
2330 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2334 oldCount1 = l1.count;
2336 while(c1 < oldCount1)
2338 byte ch = buffer[c1];
2339 if(UTF8_IS_FIRST(ch)) break;
2343 oldCount2 = l2.count;
2345 while(c2 < oldCount2)
2347 byte ch = buffer[c2];
2348 if(UTF8_IS_FIRST(ch)) break;
2353 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2358 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2359 string = new char[len];
2360 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2361 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2365 //oldCount1 = l1.count;
2366 //oldCount2 = l2.count;
2369 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2370 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2373 if(c2 > oldCount2) c2 = oldCount2;
2374 if(!(style.freeCaret))
2375 if(c1 > oldCount1) c1 = oldCount1;
2376 newLineCount = c1 + l2.count-c2;
2382 buffer = new char[line.size ? line.size : 1];
2384 buffer = new char[line.size];
2385 // TODO: Better handling of these allocation failures
2386 if(!buffer) return extras;
2387 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2392 // TODO: Better handling of these allocation failures
2393 if(!line.AdjustBuffer(newLineCount))
2397 /*if(newLineCount > 4000 || newLineCount < 0)
2398 printf("Warning");*/
2400 line.count = newLineCount;
2402 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2407 action.addedSpaces = c1-oldCount1;
2409 if(action.addedSpaces > action.x1)
2415 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2416 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2418 line.buffer[line.count] = '\0';
2425 this.dropX -= c2-c1;
2426 this.dropX = Max(this.dropX,0);
2428 this.x = Max(this.x,0);
2435 firstViewY = this.viewY;
2437 firstDropY = this.dropY;
2438 firstSelY = this.selY;
2439 for(line = l1;line;line = next, y++)
2447 if(line == this.viewLine)
2449 if(this.viewLine.next)
2451 this.viewLine = this.viewLine.next;
2453 style.recomputeSyntax = true;
2457 this.viewLine = this.viewLine.prev;
2459 style.recomputeSyntax = true;
2462 else if(y < firstViewY)
2464 if(line == this.line)
2468 this.line = this.line.next;
2469 this.x = this.line.count;
2474 this.line = this.line.prev;
2475 this.x = this.line.count;
2482 if(line == this.dropLine)
2484 if(this.dropLine.next)
2486 this.dropLine = this.dropLine.next;
2487 this.dropX = this.dropLine.count;
2491 this.dropLine = this.dropLine.prev;
2492 this.dropX = this.dropLine.count;
2496 else if(y < firstDropY)
2498 if(line == this.selLine)
2500 if(this.selLine.next)
2502 this.selLine = this.selLine.next;
2503 this.selX = this.selLine.count;
2507 this.selLine = this.selLine.prev;
2508 this.selX = this.selLine.count;
2512 else if(y < firstSelY)
2516 if(line == l2) break;
2520 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2523 style.recomputeSyntax = true;
2528 bool DelSel(int * addedSpacesPtr)
2530 if(this.line != this.selLine || this.x != this.selX)
2532 if(this.selY < this.y)
2534 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2537 this.line = this.selLine;
2539 else if(this.selY > this.y)
2541 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2544 this.selLine = this.line;
2546 else if(this.selX < this.x)
2548 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2551 this.line = this.selLine;
2555 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2558 this.selLine = this.line;
2566 bool AddToLine(const char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr, int * xAdjustmentPtr)
2568 bool hadComment = false;
2569 // Add the line here
2570 EditLine line = this.line;
2572 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2573 if(!style.hScroll && created)
2575 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2580 // Lock if no place to display.
2583 else if(endX > this.x)
2584 max = Max(this.x, line.count);
2588 for(x = 0, c = 0; c < max+count; )
2592 const char * string;
2593 if(c < Min(this.x, line.count))
2594 string = line.buffer + c;
2597 else if(c < endX + count)
2598 string = stringLine + c - endX;
2600 string = line.buffer + c - endX - count;
2604 w = (tabSize * space.w) - (x % (tabSize * space.w));
2609 numBytes = UTF8_NUM_BYTES(*string);
2610 FontExtent(display, this.font, string, numBytes, &w, null, 0, null, &oh);
2614 if(x >= clientSize.w)
2619 c += numBytes; // - 1;
2625 int addedSpaces = 0;
2627 int xAdjustment = 0;
2629 // Add blank spaces if EES_FREECARET
2630 if(this.x > line.count)
2640 for(c = 0; c<line.count; c++)
2642 if(this.line.buffer[c] == '\t')
2643 position += this.tabSize - (position % this.tabSize);
2647 wantedPosition = position + (this.x - line.count);
2649 // A tab is too much...
2650 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2651 addedSpaces = wantedPosition - position;
2654 xAdjustment = wantedPosition - position;
2658 position += this.tabSize - (position % this.tabSize);
2659 // Add as many tabs as needed
2660 addedTabs += (wantedPosition - position) / this.tabSize;
2661 position += (addedTabs-1) * this.tabSize;
2662 // Finish off with spaces
2663 addedSpaces = wantedPosition - position;
2665 xAdjustment -= addedSpaces + addedTabs;
2669 addedSpaces = this.x - line.count;
2672 this.x = Min(this.x, line.count);
2674 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2680 hadComment = HasCommentOrEscape(line);
2682 // Adjust the size of the line
2683 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2687 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2690 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2691 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2694 FillBytes(line.buffer+line.count,'\t',addedTabs);
2696 if(addedTabs > 4000 || addedTabs < 0)
2699 line.count += addedTabs;
2704 FillBytes(line.buffer+line.count,' ',addedSpaces);
2706 if(addedSpaces > 4000 || addedSpaces < 0)
2709 line.count += addedSpaces;
2712 if(addedTabsPtr) *addedTabsPtr = addedTabs;
2713 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2714 if(xAdjustmentPtr) *xAdjustmentPtr = xAdjustment;
2716 if(count > 4000 || count < 0)
2719 line.count += count;
2720 this.x += count + addedTabs + addedSpaces;
2723 this.selLine = this.line;
2725 line.buffer[line.count] = '\0';
2727 ComputeLength(line);
2732 hasComment = HasCommentOrEscape(line);
2733 if(!undoBuffer.insideRedo)
2735 int backDontRecord = undoBuffer.dontRecord;
2736 undoBuffer.dontRecord = 0;
2737 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2738 undoBuffer.dontRecord = backDontRecord;
2740 if(style.syntax && (hadComment || hasComment || line != this.line))
2742 style.recomputeSyntax = true;
2750 void Emptyline(EditLine line, int y)
2753 DelCh(line, y, 0, line.next, y+1, 0, true);
2755 DelCh(line, y, 0, line, y, line.count, true);
2758 void GoToEnd(bool deselect)
2762 this.line = this.lines.last;
2763 if(this.y != this.lineCount-1)
2765 else if (this.x != this.line.count)
2766 DirtyLine(this.lineCount-1);
2767 this.y = this.lineCount-1;
2768 this.x = this.line.count;
2775 void GoToHome(bool deselect)
2779 this.line = this.lines.first;
2782 else if (this.x !=0)
2783 DirtyLine(this.lineCount-1);
2792 // Returns true if it needs scrolling
2793 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2798 bool needHScroll = false;
2805 line = this.viewLine ? (void *)this.viewLine.prev : null;
2810 line = (void *)this.lines.first;
2815 py = Min(py, clientSize.h);
2817 py = Min(py, this.lineCount);
2819 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2825 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2828 px = Min(px,clientSize.w+this.space.w);
2832 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2835 if(tline) *tline = line;
2838 // Prevent divide by 0 from non valid this.font
2840 return (y < this.viewY) || needHScroll;
2842 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2846 // Minimal Update Management Functions
2850 this.endY = clientSize.h-1;
2851 // ErrorLog("DirtyAll\n");
2854 void DirtyEnd(int y)
2856 if((y - this.viewY)*this.space.h < this.startY)
2857 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2858 this.endY = clientSize.h-1;
2859 //ErrorLog("DirtyEnd %d\n", y);
2862 void DirtyLine(int y)
2866 if((y - this.viewY)*this.space.h < this.startY)
2867 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2868 if((y - this.viewY+1)*this.space.h > this.endY)
2869 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2871 //ErrorLog("DirtyLine %d\n", y);
2878 if(style.recomputeSyntax)
2880 FigureStartSyntaxStates(lines.first, true);
2881 style.recomputeSyntax = false;
2884 if(this.startY > this.endY) return;
2885 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2891 box.right = clientSize.w-1;
2892 box.top = this.startY;
2893 box.bottom = this.endY;
2896 this.startY = clientSize.h;
2900 bool IsMouseOnSelection()
2902 bool mouseOnSelection = false;
2905 int minY = Min(this.selY, this.y);
2906 int maxY = Max(this.selY, this.y);
2907 int minX = Min(this.selX, this.x);
2908 int maxX = Max(this.selX, this.x);
2910 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2912 if(maxX != minX || maxY != minY)
2914 if(y > minY && y < maxY)
2915 mouseOnSelection = true;
2916 else if(y == minY && y == maxY)
2917 mouseOnSelection = (x < maxX && x >= minX);
2921 mouseOnSelection = (x >= this.selX);
2922 else if(y == this.y)
2923 mouseOnSelection = (x >= this.x);
2928 mouseOnSelection = (x < this.selX);
2929 else if(y == this.y)
2930 mouseOnSelection = (x < this.x);
2933 return mouseOnSelection;
2936 void UpdateCaretPosition(bool setCaret)
2940 if(mouseMove || !style.noCaret)
2942 int max = this.mouseMove ? this.dropX : this.x;
2943 int y = this.mouseMove ? this.dropY : this.y;
2944 EditLine line = this.mouseMove ? this.dropLine : this.line;
2946 if(!(style.freeCaret))
2947 max = Min(max, line.count);
2949 if(FontExtent && display)
2951 for(c = 0; c < max; )
2960 for(len = 0; c < Min(max, line.count); c += numBytes)
2962 ch = line.buffer[c];
2963 numBytes = UTF8_NUM_BYTES(ch);
2965 if(ch == ' ' || ch == '\t')
2972 if(!len && ch == ' ')
2977 else if(!len && ch == '\t')
2979 w = (tabSize * space.w) - (x % (tabSize * space.w));
2985 FontExtent(display, this.font, line.buffer + start, len, &w, null, 0, null, &oh);
2998 caretY = y * this.space.h;
3000 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
3007 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
3008 NotifyCaretMove(master, this, y + 1, x + 1);
3014 void SelectionEnables()
3016 if((x != selX || y != selY) && !selection)
3020 itemEditCut.disabled = false;
3021 itemEditDelete.disabled = false;
3023 itemEditCopy.disabled = false;
3025 this.selection = true;
3027 else if((x == selX && y == selY) && selection)
3029 itemEditCut.disabled = true;
3030 itemEditCopy.disabled = true;
3031 itemEditDelete.disabled = true;
3033 this.selection = false;
3037 void SetSelectCursor()
3039 if(!inactive || !style.noSelect)
3042 cursor = guiApp.GetCursor(arrow);
3043 else if(this.mouseSelect)
3044 cursor = guiApp.GetCursor(iBeam);
3047 if(IsMouseOnSelection())
3048 cursor = guiApp.GetCursor(arrow);
3050 cursor = guiApp.GetCursor(iBeam);
3055 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
3058 int x = px ? *px : 0;
3064 if(c < Min(max, line.count))
3068 for(len = 0; c < Min(max, line.count); c += numBytes)
3070 ch = line.buffer[c];
3071 numBytes = UTF8_NUM_BYTES(ch);
3073 if(ch == ' ' || ch == '\t')
3080 if(!len && ch == ' ')
3085 else if(!len && ch == '\t')
3087 w = (tabSize * space.w) - (x % (tabSize * space.w));
3093 FontExtent(display, font, line.buffer + start, len, &w, null, 0, null, &oh);
3098 if(style.freeCaret && c < max)
3107 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
3112 int a = start + len;
3115 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
3121 FontExtent(display, font, line.buffer + start, a - start, &w, null, 0, null, &oh);
3125 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
3129 return Min(this.maxLineSize - 1, start + len);
3135 void SetCursorToViewX()
3137 bool selecting = this.x != selX || y != selY;
3139 // Horizontal Adjustment
3141 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3146 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3158 UpdateCaretPosition(false);
3164 void SetCursorToViewY()
3167 EditLine oldLine = this.line;
3169 bool selecting = this.x != this.selX || this.y != this.selY;
3171 numLines = clientSize.h / this.space.h;
3173 // Vertical Adjustment
3174 if(this.viewY > this.y)
3176 this.y = this.viewY;
3177 this.line = this.viewLine;
3180 if(this.viewY + numLines <= this.y)
3184 this.y = this.viewY-1;
3185 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3192 if(this.line != oldLine)
3194 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3202 this.selLine = this.line;
3205 UpdateCaretPosition(false);
3212 bool SaveFile(const char * fileName)
3214 File f = eFile_Open(fileName, FO_WRITE);
3218 eWindow_SetModified(false);
3219 eInstance_Delete(f);
3226 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3230 if(!style.multiLine)
3232 x = (active && this.active && !style.readOnly) ? line.count : 0;
3235 SetViewToCursor(true);
3240 if(!active && modified)
3243 if(!NotifyModified(master, this))
3246 *goOnWithActivation = false;
3253 if(timer) timer.Stop();
3255 mouseSelect = false;
3263 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3264 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3265 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3268 bool OnResizing(int *w, int *h)
3274 *w = space.h * 80 / 14;
3278 void OnResize(int w, int h)
3281 if(!hasHorzScroll && !hasVertScroll && viewLine)
3282 SetViewToCursor(true);
3284 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3286 SetViewToCursor(true);
3291 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3293 if(style.readOnly) return true;
3294 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3295 // Middle button already serves as a dragger as well.
3300 bool OnRightButtonDown(int x, int y, Modifiers mods)
3302 this.rightButtonDown = true;
3307 bool OnRightButtonUp(int x, int y, Modifiers mods)
3310 if(!parent.inactive && rightButtonDown)
3313 Menu contextMenu { };
3315 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3316 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3317 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3318 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3319 MenuDivider { contextMenu };
3320 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3322 popup = PopupMenu { master = this, menu = contextMenu,
3324 nonClient = true, interim = false, parent = parent,
3325 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3327 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3331 rightButtonDown = false;
3335 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3340 if(style.noSelect) return true;
3342 // Should we have a separate 'selectOnActivate' style?
3343 if(!mods.isActivate || (style.readOnly && style.multiLine))
3349 mouseX = mx - XOFFSET;
3352 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3354 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3360 else if(IsMouseOnSelection() && !mods.isActivate)
3370 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3372 if(mods.shift && !mods.isActivate)
3393 UpdateCaretPosition(true);
3394 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3395 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3399 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3403 mouseSelect = false;
3414 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3416 //PrintLn("MouseMove: ", x, ", ", y);
3422 mouseMove = IsMouseOnSelection();
3426 int size = SelSize();
3429 char * text = new char[size+1];
3433 GetSel(text, false);
3435 if(Max(selY, this.y) == dropY)
3439 if(this.dropX > this.selX)
3440 moveX = this.x - this.selX;
3444 if(this.dropX > this.x)
3445 moveX = this.selX - this.x;
3449 recordUndoEvent = true;
3451 this.dropX -= moveX;
3452 this.selX = this.x = this.dropX;
3453 this.selY = this.y = this.dropY;
3454 this.selLine = this.line = this.dropLine;
3456 recordUndoEvent = false;
3458 SetViewToCursor(true);
3463 byte * c = ((EditBox)this).multiLineContents, * c2;
3464 int l1 = c ? strlen(c) : 0, l2;
3467 c2 = ((EditBox)this).multiLineContents;
3468 l2 = c2 ? strlen(c2) : 0;
3469 if(l1 != l2 || (l1 && strcmp(c, c2)))
3499 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3501 //PrintLn("Dropped: ", x, ", ", y);
3503 NotifyDropped(master, this, x, y);
3507 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3508 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3512 bool OnMouseMove(int mx, int my, Modifiers mods)
3518 if(mods != -1 && mods.isSideEffect)
3523 if(style.noSelect) return true;
3524 if(wordSelect) return true;
3525 mouseX = mx - XOFFSET;
3528 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3530 if(this.mouseMove || this.mouseSelect)
3539 ((style.hScroll) || (style.vScroll)))
3546 DirtyLine(this.dropY);
3549 DirtyLine(this.dropY);
3550 this.dropLine = line;
3551 SetViewToCursor(true);
3553 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3556 else if(this.mouseSelect)
3558 DirtyLine(this.selY);
3565 SetViewToCursor(true);
3568 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3575 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3580 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3584 if(style.noSelect) return true;
3585 FindMouse(mx, my, &x, &y, &line, false);
3586 if(!NotifyDoubleClick(master, this, line, mods))
3593 for(c = x; c >= 0; c--)
3596 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3597 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3604 for(c = start; c<line.count; c += numBytes)
3606 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3607 if(!ch || !IS_ALUNDER(ch))
3617 this.line = this.selLine = line;
3618 this.wordSelect = (c != start);
3629 Menu fileMenu { menu, "File", F };
3630 saveDialog = fileDialog;
3631 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3632 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3634 if(style.autoSize) AutoSize();
3643 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h, 0, null, &oh);
3644 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h, 0, null, &oh);
3646 space.w = Max(space.w, 1);
3647 large.w = Max(large.w, 1);
3648 space.h = Max(space.h, 1);
3649 large.h = Max(large.h, 1);
3653 for(line = lines.first; line; line = line.next)
3654 ComputeLength(line);
3659 SetViewToCursor(true);
3667 bool OnLoadGraphics()
3669 FontExtent = Display::FontExtent2;
3672 // UpdateCaretPosition(true);
3676 void OnUnloadGraphics()
3681 bool OnKeyHit(Key key, unichar ch)
3683 bool shift = (key.shift) ? true : false;
3685 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3687 if(!ch && !key.alt && !key.ctrl)
3689 key.code = (SmartKey)key.code;
3691 else if(!ch && key.alt)
3694 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3697 if(style.readOnly) break;
3698 if(style.stuckCaret) GoToEnd(true);
3699 if(!(style.freeCaret))
3701 this.x = Min(this.x, this.line.count);
3707 EditLine line = this.line;
3709 for(y = this.y; y>= 0; y--)
3711 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3713 // Slow down when going on lines...
3714 if(y != this.y) break;
3718 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3719 if(IS_ALUNDER(line.buffer[c]))
3724 if(!IS_ALUNDER(line.buffer[c]))
3726 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3741 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3742 this.x = this.selX = Min(c+1, line.count);
3743 this.y = this.selY = y;
3744 this.line = this.selLine = line;
3745 SetViewToCursor(true);
3756 if(style.readOnly) break;
3757 if(style.stuckCaret) break;
3766 if(this.line != this.selLine || this.x != this.selX)
3769 SetViewToCursor(true);
3774 EditLine line1 = this.line, line2 = this.line;
3775 int x1, y1 = this.y, x2, y2 = this.y;
3776 if(!style.freeCaret)
3778 this.selX = this.x = Min(this.x, this.line.count);
3783 if(x1 < line1.count)
3789 char * buffer = line1.buffer;
3790 for(i = x1; i < line1.count; i++)
3792 if(!IS_ALUNDER(buffer[i]))
3796 for(; i < line1.count; i++)
3798 // Delete trailing whitespace
3799 if(IS_ALUNDER(buffer[i]))
3809 // Avoid creating trailing spaces if there is no content on next line
3812 if(line2 && !line2.count)
3817 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3818 SetViewToCursor(true);
3827 if(!key.alt && !key.ctrl)
3831 bool stuffAfter = false;
3834 /*bool resetX = false;
3837 if(style.stuckCaret) GoToEnd(true);
3838 if(style.readOnly) break;
3839 if(!(style.multiLine)) break;
3841 for(c = 0; c<this.line.count && c<this.x; c++)
3843 if(this.line.buffer[c] == '\t')
3844 position += this.tabSize - (position % this.tabSize);
3845 else if(this.line.buffer[c] == ' ')
3851 // Prevent adding trailing spaces if at the head of a line
3852 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3864 if(this.x < this.line.count)
3867 //If last character is a { indent one tab
3868 c = Min(x, line.count);
3869 if(c > 0 && line.buffer[c - 1] == '{')
3871 //Except if the next non space character is a }
3872 bool indent = false;
3874 for(i = c; i <= this.line.count; i++) // indent will be set to true on nul terminating char
3875 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3877 if(this.line.buffer[i] != '}')
3882 position += this.tabSize;
3885 addString = new char[position + 2];
3886 addString[len++] = '\n';
3887 addString[len] = '\0';
3889 if(stuffAfter || !style.freeCaret)
3891 for(c = 0; c<position; )
3893 if(style.useTab && c + this.tabSize <= position)
3895 addString[len++] = '\t';
3900 addString[len++] = ' ';
3904 addString[len] = '\0';
3906 recordUndoEvent = true;
3909 EditLine prevLine = this.line.prev;
3912 // Nuke spaces if that is all that is left on previous line
3914 char * buffer = prevLine.buffer;
3915 for(i = 0; i < prevLine.count; i++)
3916 if(buffer[i] != ' ' && buffer[i] != '\t')
3918 if(i == prevLine.count)
3919 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3923 this.x = this.selX = backX;
3926 else */if(!stuffAfter && style.freeCaret)
3928 this.x = this.selX = position;
3932 SetViewToCursor(true);
3935 recordUndoEvent = false;
3943 if(style.stuckCaret) break;
3944 if(!(style.freeCaret))
3946 this.x = Min(this.x, this.line.count);
3947 this.selX = Min(this.selX, this.selLine.count);
3950 if(!shift) SelDirty();
3953 bool foundAlpha = false;
3956 EditLine line, lastLine = null;
3957 int lastC = 0, lastY = 0;
3959 for(line = this.line; (line && !found); line = line.prev, y--)
3964 if(this.x == 0 && line != this.line)
3973 if(line == this.line) start = this.x -1; else start = line.count-1;
3974 start = Min(start, line.count-1);
3976 for(c = start; c >= 0;)
3979 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3997 byte ch = line.buffer[c];
3998 if(UTF8_IS_FIRST(ch)) break;
4002 // No next word found,
4003 if(!found && ( this.x > 0 || (!line.count && this.x)))
4017 this.line = lastLine;
4028 byte * buffer = (byte *)line.buffer;
4031 byte ch = buffer[x];
4032 if(UTF8_IS_FIRST(ch)) break;
4049 if(!shift) _Deselect();
4050 SetViewToCursor(true);
4056 if(style.stuckCaret) break;
4057 if(!(style.freeCaret))
4059 this.x = Min(this.x, this.line.count);
4060 this.selX = Min(this.selX, this.selLine.count);
4063 if(!shift) SelDirty();
4064 if(!shift && (this.x != this.selX || this.y != this.selY));
4067 bool onAChar = false;
4068 if(this.selX != this.x || this.selY != this.y)
4070 if(this.x<this.line.count)
4071 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
4073 if(key.shift && onAChar &&
4074 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
4076 bool foundAlpha = false;
4078 EditLine line = null, lastLine = null;
4080 int lastC = 0, lastY = 0, lastNumBytes = 0;
4082 for(line = this.line; (line && !found); line = line.next, y++)
4084 int start = (line == this.line) ? this.x : 0;
4088 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4094 lastNumBytes = numBytes;
4104 if(!found && (c != this.x || line != this.line))
4117 this.x = lastC + lastNumBytes;
4119 this.line = lastLine;
4126 bool foundAlpha = false;
4131 for(line = this.line; (line && !found); line = line.next, y++)
4133 int start = (line == this.line) ? this.x : 0;
4137 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4153 // No next word found,
4154 if(!found && (c != this.x || line != this.line))
4158 this.x = line.count;
4170 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4174 byte * buffer = (byte *)line.buffer;
4177 byte ch = buffer[x];
4178 if(UTF8_IS_FIRST(ch)) break;
4199 if(!shift) _Deselect();
4200 SetViewToCursor(true);
4207 if(!style.vScroll || hasVertScroll) break;
4213 if(style.stuckCaret) break;
4215 if(!shift) SelDirty();
4225 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4229 if(!shift) _Deselect();
4231 SetViewToCursor(false);
4234 if(caretY == this.y * space.h)
4240 if(!style.freeCaret)
4241 this.x = Min(this.x, line.count);
4251 int sx = 0, sy = this.y * space.h;
4252 char * text = line.text;
4253 int maxW = clientSize.w - sx;
4254 display.FontExtent(font, " ", 1, null, &th);
4258 int startPos = textPos;
4261 bool lineComplete = false;
4263 if(!style.wrap && caretY == MAXINT)
4266 //textPos = line.count;
4267 //lineComplete = true;
4270 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4274 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4277 len = (nextSpace - (text + textPos));
4279 len = line.count - textPos;
4281 if(textPos < line.count)
4283 display.FontExtent(font, text + textPos, len, &w, null);
4285 if(nextSpace) { w += space.w; len++; }
4287 if(style.wrap && x + width + w > maxW && x > 0)
4289 lineComplete = true;
4299 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4303 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4306 if(this.x > line.count)
4309 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4310 len = this.x - startPos;
4311 display.FontExtent(font, text + startPos, len, &x, null);
4315 if(!shift) _Deselect();
4317 SetViewToCursor(false);
4321 if(sy == caretY - th || textPos >= line.count)
4323 if(textPos >= line.count)
4325 int c = textPos - 1;
4326 while(c > 0 && text[c] == ' ') c--;
4330 this.x = line.count;
4333 if(!shift) _Deselect();
4335 SetViewToCursor(false);
4340 } while(textPos < line.count);
4343 if(!shift) _Deselect();
4345 SetViewToCursor(false);
4354 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4355 if(!shift) SelDirty();
4356 this.line = this.line.prev;
4362 if(!shift) _Deselect();
4366 SetViewToCursor(false);
4372 return style.multiLine ? false : true;
4376 if(!style.vScroll || hasVertScroll)
4383 if(style.stuckCaret) break;
4388 int sx = 0, sy = this.y * this.space.h;
4389 int maxW = clientSize.w - sx;
4390 char * text = line.buffer;
4393 if(!shift) SelDirty();
4399 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4409 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4413 if(!shift) _Deselect();
4415 SetViewToCursor(false);
4418 while(!textPos || (style.freeCaret || textPos<line.count))
4420 int startPos = textPos;
4423 bool lineComplete = false;
4424 if(!style.wrap && sy <= caretY)
4426 textPos = line.count;
4427 lineComplete = true;
4429 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4433 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4436 len = (nextSpace - (text + textPos));
4438 len = line.count - textPos;
4440 if(textPos < line.count)
4442 display.FontExtent(font, text + textPos, len, &w, &th);
4444 if(nextSpace) { w += space.w; len++; }
4445 if(style.wrap && x + width + w > maxW && x > 0)
4447 lineComplete = true;
4457 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4461 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4464 if(this.x > line.count)
4467 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4469 len = this.x - startPos;
4470 display.FontExtent(font, text + startPos, len, &x, null);
4473 if(!shift) _Deselect();
4476 SetViewToCursor(false);
4482 this.x = line.count;
4485 if(!shift) _Deselect();
4488 SetViewToCursor(false);
4491 else if(textPos >= line.count && line.next)
4497 sy = this.y * this.space.h;
4498 sx = 0; //textBlock.startX;
4504 sx = 0; //textBlock.startX;
4512 this.x = Min(this.x, line.count);
4513 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4515 if(!shift) _Deselect();
4518 if(this.selX != this.x || this.selY != this.y)
4521 SetViewToCursor(false);
4528 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4529 if(!shift) SelDirty();
4530 this.line = this.line.next;
4534 if(!shift) _Deselect();
4537 if(this.selX != this.x || this.selY != this.y)
4545 return style.multiLine ? false : true;
4548 if(style.stuckCaret) break;
4549 if(!style.multiLine && key.ctrl) break;
4550 if(!(style.freeCaret))
4551 this.selX = Min(this.selX, this.selLine.count);
4553 if(!shift) SelDirty();
4556 this.line = this.lines.first;
4557 if(this.y != 0 || this.x != 0)
4567 EditLine line = this.line;
4569 for(c=0; line.buffer[c]; c++)
4570 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4572 if(overwrite || (shift && (c != 0 || this.x)))
4581 if(overwrite || (shift && this.x != 0))
4587 if(!shift) _Deselect();
4588 SetViewToCursor(true);
4594 if(style.stuckCaret) break;
4595 if(!style.multiLine && key.ctrl) break;
4596 if(!style.freeCaret)
4597 this.selX = Min(this.selX, this.selLine.count);
4599 if(!shift) SelDirty();
4604 else if(this.x != this.line.count)
4606 this.x = this.line.count;
4607 if(overwrite || shift)
4611 if(!shift) _Deselect();
4612 SetViewToCursor(true);
4617 if(style.tabKey && !key.ctrl)
4619 if(this.selY != this.y && style.tabSel)
4621 EditLine firstLine, lastLine;
4625 // Do multi line selection tabbing here
4626 if(this.selY < this.y)
4628 firstLine = this.selLine;
4629 lastLine = this.line;
4635 // Selecting going up
4636 firstLine = this.line;
4637 lastLine = this.selLine;
4644 for(line = firstLine; line; line = line.next, y++)
4646 if(line != lastLine || x)
4650 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4652 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4654 if(line.buffer[c] == '\t')
4659 else if(line.buffer[c] != ' ')
4664 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4667 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4668 char * string = new char[len];
4669 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4670 GetText(string, line, y, 0, line, y, lastC, false, false);
4673 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4674 if(!line.AdjustBuffer(line.count-lastC))
4680 if(line == lastLine) break;
4685 for(line = firstLine; line; line = line.next, y++)
4689 if(line != lastLine || x)
4693 if(line.count + 1 <= this.maxLineSize)
4695 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4697 if(!line.AdjustBuffer(line.count+1))
4700 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4702 AddCharAction action { ch = '\t', x = 0, y = y };
4706 memmove(line.buffer+1,line.buffer,line.size-1);
4708 line.buffer[0] = '\t';
4713 if(line.count + this.tabSize <= this.maxLineSize)
4716 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4717 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4719 if(!line.AdjustBuffer(line.count+this.tabSize))
4723 char * string = new char[this.tabSize + 1];
4724 memset(string, ' ', this.tabSize);
4725 string[this.tabSize] = '\0';
4726 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4729 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4730 line.count+=this.tabSize;
4731 for(c=0; c<this.tabSize; c++)
4732 line.buffer[c] = ' ';
4738 if(line == lastLine) break;
4741 ComputeLength(maxLine);
4753 char * addString = new char[this.tabSize + 1];
4755 if(!(style.freeCaret))
4757 this.x = Min(this.x, this.line.count);
4761 start = Min(this.x, this.selX);
4762 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4764 addString[len++] = ' ';
4772 SetViewToCursor(true);
4781 if(!(style.hScroll) || hasHorzScroll) break;
4782 if(this.viewX < this.maxLength)
4784 //this.viewX+=this.space.w*this.tabSize;
4786 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4793 if(!shift) _Deselect();
4805 if(!(style.hScroll) || hasHorzScroll) break;
4808 //this.viewX-=this.space.w*this.tabSize;
4809 //this.viewX = Max(this.viewX,0);
4811 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4812 // SetCursorToView();
4819 if(!shift) _Deselect();
4832 else if(!style.readOnly)
4836 if(!(style.readOnly))
4842 this.overwrite ^= 1;
4843 UpdateCaretPosition(true);
4848 NotifyOvrToggle(master, this, this.overwrite);
4860 if(style.noSelect) break;
4863 //Save current view position
4864 int tempX = this.viewX;
4865 int tempY = this.viewY;
4869 this.selLine = this.lines.first;
4870 this.y = this.lineCount-1;
4871 this.line = this.lines.last;
4872 this.x = this.line.count;
4875 SetViewToCursor(true);
4877 //Restore previous view position
4878 SetScrollPosition(tempX, tempY * this.space.h);
4884 // TOCHECK: Was there any good reason why we weren't returning false here?
4885 return false; // break;
4890 if(!(style.readOnly))
4898 if(style.readOnly) break;
4904 if(style.readOnly) break;
4909 if(style.readOnly) break;
4913 if(style.readOnly) break;
4917 if(style.readOnly) break;
4918 if(key.shift && key.code == rightBracket)
4920 //Only indent back if you are exactly at one tab.
4922 //bool whitespace = true;
4927 int indentwidth = 0;
4928 EditLine line = this.line;
4930 //Only remove one tab if there is nothing else on the line.
4931 for(i = 0; i < this.line.count; i++)
4933 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4947 for(pos = line.count - 1; pos >= 0; pos--)
4949 char c = line.buffer[pos];
4953 indentwidth += this.tabSize;
4955 //Counting backwards, so when you find a character, indentation is reset
4965 //Place the } to get an undo:
4968 this.x = this.line.count;
4972 putsize = indentwidth;
4974 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4976 newline = new char[putsize+2];
4977 newline[putsize] = '}';
4978 newline[putsize+1] = '\0';
4982 for(; i < indentwidth / this.tabSize; i++)
4984 for(;i < putsize; i++)
4993 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
5005 void OnHScroll(ScrollBarAction action, int position, Key key)
5008 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
5010 this.viewX = position;
5011 if(action != setRange)
5013 if(!this.mouseMove && style.cursorFollowsView)
5020 void OnVScroll(ScrollBarAction action, int position, Key key)
5022 int oldViewY = this.viewY;
5025 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
5027 position /= this.space.h;
5029 if(position < this.viewY)
5031 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
5032 style.recomputeSyntax = true;
5034 else if(position > this.viewY)
5036 EditLine oldViewLine = viewLine;
5037 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
5038 FigureStartSyntaxStates(oldViewLine, false);
5041 if(action != setRange)
5043 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
5046 if(this.x != this.selX || this.y != this.selY)
5050 Scroll(0, (this.viewY - oldViewY) * this.space.h);
5053 int numLines = clientSize.h / this.space.h;
5055 if(Abs(this.viewY - oldViewY) < numLines)
5058 if(this.viewY > oldViewY)
5060 for(y = oldViewY; y <this.viewY; y++)
5061 DirtyLine(y + numLines);
5065 for(y = this.viewY; y <oldViewY; y++)
5066 DirtyLine(y + numLines);
5071 // Fix dirt of stuff before first line...
5072 if(this.viewY - oldViewY > 0)
5074 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
5081 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr, int * xAdjustmentPtr)
5084 int length, endX = 0;
5086 ReplaceTextAction replaceAction = null;
5087 AddCharAction addCharAction = null;
5088 int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
5090 if(ch == '\r') return true;
5091 if(style.stuckCaret /*|EES_READONLY)*/ )
5094 if(ch == '\n' && !(style.multiLine) && this.line) return false;
5096 if(!undoBuffer.dontRecord)
5098 if(selX != x || selY != y)
5103 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5104 oldString = new char[len];
5105 UTF32toUTF8Len(&ch, 1, buffer, 4);
5106 newString = CopyString(buffer);
5107 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5109 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5110 if(selY < y || (selY == y && selX < x))
5112 replaceAction.x1 = selX;
5113 replaceAction.y1 = selY;
5114 replaceAction.x2 = x;
5115 replaceAction.y2 = y;
5119 replaceAction.x1 = x;
5120 replaceAction.y1 = y;
5121 replaceAction.x2 = selX;
5122 replaceAction.y2 = selY;
5124 Record(replaceAction);
5125 undoBuffer.dontRecord++;
5129 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5130 Record(addCharAction);
5136 DelSel(&addedSpaces);
5137 if(this.lineCount+1 > this.maxLines)
5140 Emptyline(this.lines.first,0);
5144 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5146 // Make sure it fits, but we need a default line is this.font is too big for window
5147 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5150 if((this.y >= 0) && this.y < this.viewY)
5155 line = EditLine { };
5158 lines.Insert(this.line, line);
5159 line.editBox = this;
5165 // If we're displacing the lines from a current line ...
5168 if(this.line.buffer)
5171 if(this.line.count < endX) endX = this.line.count;
5172 length = this.line.count - endX;
5175 if(!line.AdjustBuffer(length))
5179 if(this.line.buffer)
5181 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5183 if(endX > 4000 || endX < 0)
5186 this.line.count = endX;
5187 this.line.buffer[this.line.count] = '\0';
5188 this.line.AdjustBuffer(this.line.count);
5189 ComputeLength(this.line);
5193 BufferLocation before = { this.line, this.y, this.x }, after;
5198 line.count = length;
5201 if(length > 4000 || length < 0)
5204 ComputeLength(this.line);
5209 line.buffer[line.count] = '\0';
5212 after.line = this.line, after.y = this.y, after.x = this.x;
5214 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5220 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5221 DelSel(&addedSpaces);
5222 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs, &xAdjustment);
5223 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5224 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5225 if(xAdjustmentPtr) *xAdjustmentPtr = xAdjustment;
5229 this.selLine = this.line;
5233 replaceAction.x3 = x;
5234 replaceAction.y3 = y;
5235 replaceAction.addedSpaces = addedSpaces;
5236 replaceAction.addedTabs = addedTabs;
5237 undoBuffer.dontRecord--;
5241 addCharAction.x -= addedTabs * (tabSize-1);
5242 addCharAction.addedSpaces = addedSpaces;
5243 addCharAction.addedTabs = addedTabs;
5250 /****************************************************************************
5252 ****************************************************************************/
5255 bool AddCh(unichar ch)
5257 return _AddCh(ch, null, null, null);
5262 this.modified = true;
5263 NotifyUpdate(master, this);
5264 modifiedDocument = true;
5267 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5270 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5271 SetViewToCursor(true);
5279 itemEditUndo.disabled = undoBuffer.curAction == 0;
5280 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5286 if(savedAction == undoBuffer.curAction)
5288 modifiedDocument = false;
5290 NotifyUnsetModified(master, this);
5297 itemEditUndo.disabled = undoBuffer.curAction == 0;
5298 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5304 if(savedAction == undoBuffer.curAction)
5306 modifiedDocument = false;
5308 NotifyUnsetModified(master, this);
5312 void Record(UndoAction action)
5314 if(!undoBuffer.dontRecord)
5316 undoBuffer.Record(action);
5317 itemEditUndo.disabled = undoBuffer.curAction == 0;
5318 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5325 this.lines.first, 0,0,
5326 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5329 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5334 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5335 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5336 this.y = line2 ? y2 : (this.lineCount-1);
5337 this.line = line2 ? (EditLine)line2 : this.lines.last;
5340 SetViewToCursor(true);
5344 // TODO: Fix this vs modifiedDocument window property
5345 void SetModified(bool flag)
5349 this.modified = false;
5350 if(flag && !NotifyModified(master, this))
5351 this.modified = true;
5356 bool AddS(const char * string)
5363 int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
5364 AddTextAction action = null;
5365 ReplaceTextAction replaceAction = null;
5367 this.pasteOperation = true;
5369 if(style.stuckCaret /*|EES_READONLY)*/ )
5372 if(!undoBuffer.dontRecord)
5374 char * placeString = CopyString(string);
5375 if(!style.multiLine)
5379 for(i = 0; (ch = placeString[i]); i++)
5382 placeString[i] = '\0';
5383 placeString = renew placeString byte[i+1];
5388 if(selX != x || selY != y)
5392 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5393 oldString = new char[len];
5394 newString = placeString;
5395 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5397 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5398 if(selY < y || (selY == y && selX < x))
5400 replaceAction.x1 = selX;
5401 replaceAction.y1 = selY;
5402 replaceAction.x2 = x;
5403 replaceAction.y2 = y;
5407 replaceAction.x1 = x;
5408 replaceAction.y1 = y;
5409 replaceAction.x2 = selX;
5410 replaceAction.y2 = selY;
5412 Record(replaceAction);
5416 if(string[0] == '\n')
5417 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5419 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5427 undoBuffer.dontRecord++;
5428 DelSel(&addedSpaces);
5432 for(c = 0; string[c]; c++)
5434 if(string[c] == '\n' || string[c] == '\r')
5436 if(!AddToLine(line, count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs, xAdjustment ? null : &xAdjustment))
5441 if(string[c] == '\n')
5450 // Reset for next line
5454 if(string[c] == '\r' && *line == '\n')
5466 // Why was this here?
5469 // Add the line here
5471 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs, xAdjustment ? null : &xAdjustment))
5477 undoBuffer.dontRecord--;
5482 action.addedSpaces = addedSpaces;
5483 action.addedTabs = addedTabs;
5484 action.xAdjustment = xAdjustment;
5486 else if(replaceAction)
5488 replaceAction.y3 = y;
5489 replaceAction.x3 = x;
5490 replaceAction.addedSpaces = addedSpaces;
5491 replaceAction.addedTabs = addedTabs;
5492 replaceAction.xAdjustment = xAdjustment;
5495 UpdateCaretPosition(true);
5497 this.pasteOperation = false;
5510 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5511 SetViewToCursor(true);
5517 void PutCh(unichar ch)
5521 if((ch >= 32 /*&& ch <=126*/) || ch == '\n' || ch == '\t')
5522 //if((ch >= 32) || ch == '\n')
5524 int addedSpaces = 0, addedTabs = 0, xAdjustment = 0;
5525 ReplaceTextAction replaceAction = null;
5526 AddCharAction addCharAction = null;
5529 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5531 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5536 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5537 oldString = new char[len];
5538 UTF32toUTF8Len(&ch, 1, buffer, 4);
5539 newString = CopyString(buffer);
5540 GetText(oldString, line, y, x, line, y, x+1, false, false);
5541 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5542 replaceAction.x1 = x;
5543 replaceAction.y1 = y;
5544 replaceAction.x2 = x+1;
5545 replaceAction.y2 = y;
5546 Record(replaceAction);
5548 undoBuffer.dontRecord++;
5549 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5550 undoBuffer.dontRecord--;
5552 else if(!undoBuffer.dontRecord)
5554 if(selX != x || selY != y)
5559 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5560 oldString = new char[len];
5561 UTF32toUTF8Len(&ch, 1, buffer, 4);
5562 newString = CopyString(buffer);
5563 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5564 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5565 if(selY < y || (selY == y && selX < x))
5567 replaceAction.x1 = selX;
5568 replaceAction.y1 = selY;
5569 replaceAction.x2 = x;
5570 replaceAction.y2 = y;
5574 replaceAction.x1 = x;
5575 replaceAction.y1 = y;
5576 replaceAction.x2 = selX;
5577 replaceAction.y2 = selY;
5579 Record(replaceAction);
5583 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5584 Record(addCharAction);
5587 undoBuffer.dontRecord++;
5588 result = _AddCh(ch, &addedSpaces, &addedTabs, &xAdjustment);
5591 replaceAction.x3 = x;
5592 replaceAction.y3 = y;
5593 replaceAction.addedSpaces = addedSpaces;
5594 replaceAction.addedTabs = addedTabs;
5595 replaceAction.addedTabs = xAdjustment;
5599 addCharAction.addedSpaces = addedSpaces;
5600 addCharAction.addedTabs = addedTabs;
5601 addCharAction.xAdjustment = xAdjustment;
5603 undoBuffer.dontRecord--;
5607 if(result) SetViewToCursor(true);
5611 void PutS(const char * string)
5616 SetViewToCursor(true);
5621 void Printf(const char * format, ...)
5625 char temp[MAX_F_STRING];
5627 va_start(args, format);
5628 vsnprintf(temp, sizeof(temp), format, args);
5629 temp[sizeof(temp)-1] = 0;
5635 void SetContents(const char * format, ...)
5639 undoBuffer.dontRecord++;
5641 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5644 char temp[MAX_F_STRING];
5646 va_start(args, format);
5647 vsnprintf(temp, sizeof(temp), format, args);
5648 temp[sizeof(temp)-1] = 0;
5655 undoBuffer.dontRecord--;
5668 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5671 else if(this.line.prev)
5673 EditLine line = this.line.prev;
5677 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5685 this.selLine = this.line;
5690 SetViewToCursor(true);
5695 Emptyline(this.line,this.y);
5696 this.selX = this.x = 0;
5699 this.selLine = this.line;
5701 SetViewToCursor(true);
5711 SetViewToCursor(true);
5719 SetViewToCursor(true);
5723 bool GoToLineNum(int lineNum)
5728 EditLine line = this.lines.first;
5729 for(c = 0; c < lineNum && line; c++, line = line.next);
5739 SetViewToCursor(true);
5746 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5747 bool GoToPosition(EditLine line, int y, int x)
5759 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5773 SetViewToCursor(true);
5780 void SetViewToCursor(bool setCaret)
5789 bool dontScroll = false;
5795 selected = selX != this.x || selY != y;
5802 checkLine = dropLine;
5808 checkLine = this.line;
5813 numLines = clientSize.h / space.h;
5815 // This is broken. The EditBox now doesn't do anything different when adding to it,
5816 // regardless of the previous scrolling position. It should be read and then set again
5817 // if one wishes to preserve it.
5818 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5819 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5823 // Horizontal Adjustment
5824 if(!dontScroll && checkLine)
5828 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5831 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5837 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5838 viewX = x - clientSize.w+space.w;
5839 if(x < this.viewX + clientSize.w/2 - space.w)
5840 viewX = Max(0, x - clientSize.w/2 + space.w);
5848 // Vertical Adjustment
5849 if(viewY > checkY) viewY = checkY;
5850 if(viewY + numLines <= checkY)
5852 if(clientSize.h >= space.h)
5854 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5864 for(;dropLine && dropLine.prev && dropY >= numLines;)
5866 dropLine = dropLine.prev;
5870 for(;this.line && this.line.prev && this.y >= numLines;)
5872 this.line = this.line.prev;
5877 SetScrollPosition(viewX, viewY * this.space.h);
5879 UpdateCaretPosition(setCaret);
5885 selLine = this.line;
5895 void CenterOnCursor()
5897 int numLines = clientSize.h / this.space.h;
5898 int y = this.y - numLines / 2;
5900 bool figureSyntax = false;
5901 EditLine oldViewLine = viewLine;
5902 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5907 for(;y < this.viewY; y++)
5909 this.viewLine = this.viewLine.prev;
5910 style.recomputeSyntax = true;
5912 for(;y > this.viewY; y--)
5914 this.viewLine = this.viewLine.next;
5915 figureSyntax = true;
5918 FigureStartSyntaxStates(oldViewLine, false);
5922 SetScrollPosition(this.viewX, viewY * this.space.h);
5923 UpdateCaretPosition(true);
5927 void SetCursorToView()
5938 numLines = clientSize.h / this.space.h;
5942 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5943 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5947 //EditLine oldLine = this.line;
5948 bool lastOne = false;
5949 EditLine oldViewLine = this.viewLine;
5950 bool figureSyntax = false;
5952 if(this.y >= this.lineCount-1) return;
5954 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5956 if(this.viewY + numLines < this.lines.count)
5958 this.viewLine = this.viewLine.next;
5960 figureSyntax = true;
5962 else if(c && !lastOne)
5971 FigureStartSyntaxStates(oldViewLine, false);
5972 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5975 SetViewToCursor(false);
5984 if(this.y == 0) return;
5986 numLines = clientSize.h / this.space.h;
5990 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5991 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5995 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
6000 if(this.viewLine.prev)
6002 this.viewLine = this.viewLine.prev;
6004 style.recomputeSyntax = true;
6008 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
6011 SetViewToCursor(false);
6018 if(this.viewLine.prev)
6021 // this.viewLine = this.viewLine.prev;
6024 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
6031 if(this.viewLine.next)
6034 // this.viewLine = this.viewLine.next;
6037 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
6044 EditLine l1, l2, line;
6045 int x1, x2, nx1, nx2;
6050 if(!this.selLine) return 0;
6051 if(this.selLine == this.line && this.selX == this.x) return 0;
6052 if(this.selY < this.y)
6059 else if(this.selY > this.y)
6066 else if(this.selX < this.x)
6068 l1 = l2 = this.line;
6074 l1 = l2 = this.line;
6078 nx1 = Min(x1,l1.count);
6079 nx2 = Min(x2,l2.count);
6081 // Find Number of Bytes Needed
6083 for(line = l1; line; line = line.next)
6085 if(line == l1) start = nx1; else start = 0;
6086 if(line == l2) end = nx2; else end = line.count;
6088 if(style.freeCaret && line == l2)
6091 count = Max(x2-Max(x1,l1.count),0);
6093 count = Max(x2-l2.count,0);
6097 if(line == l2) break;
6098 // Add Carriage Return / line Feed
6105 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
6109 if(!reorder || this.selY < this.y)
6111 if(l1) *l1 = this.selLine;
6112 if(y1) *y1 = this.selY;
6113 if(x1) *x1 = this.selX;
6114 if(l2) *l2 = this.line;
6115 if(y2) *y2 = this.y;
6116 if(x2) *x2 = this.x;
6118 else if(this.selY > this.y)
6120 if(l1) *l1 = this.line;
6121 if(y1) *y1 = this.y;
6122 if(x1) *x1 = this.x;
6123 if(l2) *l2 = this.selLine;
6124 if(y2) *y2 = this.selY;
6125 if(x2) *x2 = this.selX;
6127 else if(this.selX < this.x)
6129 if(l1) *l1 = this.line;
6130 if(y1) *y1 = this.selY;
6131 if(x1) *x1 = this.selX;
6132 if(l2) *l2 = this.line;
6133 if(y2) *y2 = this.y;
6134 if(x2) *x2 = this.x;
6138 if(l1) *l1 = this.line;
6139 if(y1) *y1 = this.y;
6140 if(x1) *x1 = this.x;
6141 if(l2) *l2 = this.line;
6142 if(y2) *y2 = this.selY;
6143 if(x2) *x2 = this.selX;
6148 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6150 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6152 this.selLine = (EditLine)l1;
6155 this.line = (EditLine)l2;
6159 SetViewToCursor(true);
6163 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6165 EditLine l1, l2, line;
6166 int x1, x2, nx1, nx2;
6197 nx1 = Min(x1,l1.count);
6198 nx2 = Min(x2,l2.count);
6201 for(line = l1; line; line = line.next)
6203 if(line == l1) start = nx1; else start = 0;
6204 if(line == l2) end = nx2; else end = line.count;
6207 CopyBytes(text, line.buffer + start, end - start);
6210 numChars += end-start;
6212 if(style.freeCaret && line == l2 && addSpaces)
6215 count = Max(x2-Max(x1,l1.count),0);
6217 count = Max(x2-l2.count,0);
6220 FillBytes(text,' ',count);
6226 if(line == l2) break;
6238 // '\0' terminate Terminate
6245 void GetSel(char * text, bool addCr)
6247 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6250 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6268 int size = SelSize();
6271 // Try to allocate memory
6272 ClipBoard clipBoard { };
6273 if(clipBoard.Allocate(size+1))
6275 GetSel(clipBoard.memory, true);
6288 ClipBoard clipBoard { };
6289 if(clipBoard.Load())
6290 PutS(clipBoard.memory);
6302 SetViewToCursor(true);
6308 void DeleteSelection()
6313 SetViewToCursor(true);
6319 void Save(File f, bool cr)
6322 savedAction = undoBuffer.curAction;
6324 for(line = this.lines.first; line; line = line.next)
6326 f.Write(line.buffer, line.count, 1);
6329 if(cr) f.Putc('\r');
6335 #define BUFFER_SIZE 16384
6338 undoBuffer.dontRecord++;
6341 char buffer[BUFFER_SIZE];
6345 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6346 buffer[count] = '\0';
6352 undoBuffer.dontRecord--;
6353 undoBuffer.count = 0;
6354 undoBuffer.curAction = 0;
6355 itemEditUndo.disabled = undoBuffer.curAction == 0;
6356 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6359 EditBoxFindResult Find(const char * text, bool matchWord, bool matchCase, bool isSearchDown)
6363 bool firstPass = true;
6364 EditBoxFindResult result = found;
6366 if(!this.line) return notFound;
6369 for(line = this.line;;)
6377 line = this.lines.first;
6383 line = this.lines.last;
6384 num = this.lineCount - 1;
6390 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6392 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6396 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6399 if(line == this.line && !firstPass) break;
6417 EditBoxFindResult FindInSelection(const char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6421 int searchLen = strlen(text);
6422 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6424 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6425 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6427 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6434 bool OnKeyDown(Key key, unichar ch)
6437 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6439 if(!NotifyKeyDown(master, this, key, ch))
6449 this.mouseMove = false;
6450 OnLeftButtonUp(0,0,0);
6451 SetViewToCursor(true);
6461 public class EditBoxStream : File
6464 BufferLocation start, sel;
6471 EditBox editBox = this.editBox;
6473 editBox.x = start.x;
6474 editBox.y = start.y;
6475 editBox.line = start.line;
6477 editBox.selX = sel.x;
6478 editBox.selY = sel.y;
6479 editBox.selLine = sel.line;
6481 editBox.SetViewToCursor(true);
6482 //editBox.ComputeColumn();
6486 property EditBox editBox
6494 start.line = value.line;
6498 sel.line = value.selLine;
6501 value.GoToHome(true);
6504 get { return editBox; }
6507 uint Read(byte * buffer, uint size, uint count)
6510 EditBox editBox = this.editBox;
6511 EditLine line = editBox.line;
6517 for(;read < count && line; line = (*&line.next))
6519 int numBytes = Min(count - read, (*&line.count) - x);
6522 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6527 for(;read < count && x < (*&line.count);)
6529 buffer[read++] = (*&line.buffer)[x++];
6536 buffer[read++] = '\n';
6541 if(x == (*&line.count) && (*&line.next))
6550 editBox.line = editBox.selLine = line;
6551 editBox.x = editBox.selX = x;
6552 editBox.y = editBox.selY = y;
6557 bool Seek(int pos, FileSeekMode mode)
6560 EditBox editBox = this.editBox;
6561 EditLine line = editBox.line;
6563 if(mode == FileSeekMode::start)
6565 pos = pos - this.pos;
6575 for(;read < pos && line; line = (*&line.next))
6577 int numBytes = Min(pos - read, (*&line.count) - x);
6580 read += numBytes; x += numBytes;
6583 /*for(;read < pos && x < (*&line.count);)
6611 for(;read < pos && line; line = (*&line.prev))
6613 int numBytes = Min(pos - read, x);
6619 /*for(;read < pos && x > 0;)
6629 x = (*&(*&line.prev).count);
6645 editBox.line = editBox.selLine = line;
6646 editBox.x = editBox.selX = x;
6647 editBox.y = editBox.selY = y;
6652 bool Puts(const char * string)
6654 EditBox editBox = this.editBox;
6655 BufferLocation start { editBox.line, editBox.y, editBox.x };
6659 editBox.AddS(string);
6661 pos.line = editBox.line;
6665 this.start.AdjustAdd(start, pos);
6666 sel.AdjustAdd(start, pos);
6671 // NOTE: BYTE, NOT UNICODE CHARACTER!
6674 EditBox editBox = this.editBox;
6675 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6680 utf8Bytes[numBytes++] = ch;
6681 utf8Bytes[numBytes] = 0;
6682 if(UTF8Validate((char *)utf8Bytes))
6684 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6686 pos.line = editBox.line;
6689 this.start.AdjustAdd(start, pos);
6690 sel.AdjustAdd(start, pos);
6697 bool Getc(char * ch)
6699 return Read(ch, 1, 1) ? true : false;
6702 void DeleteBytes(uint count)
6704 EditBox editBox = this.editBox;
6707 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6708 BufferLocation end = pos;
6713 for(;c < count && end.line && end.x < end.line.count;)
6718 if(c < count && end.line && end.line.next)
6720 end.line = end.line.next;
6729 start.AdjustDelete(pos, end);
6730 sel.AdjustDelete(pos, end);
6732 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);