1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 public class SyntaxColorScheme
18 Color charLiteralColor;
19 Color stringLiteralColor;
20 Color preprocessorColor;
22 private Array<Color> keywordColors { };
24 public property Container<Color> keywordColors
26 set { keywordColors.Copy((void *)value); }
27 get { return keywordColors; }
35 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
37 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
39 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
41 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
42 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
43 #define UTF8_GET_CHAR(string, numBytes) \
47 byte b = (string)[0]; \
50 numBytes = b ? 1 : 0; \
64 if(b & 0x08) { numBytes = 0; } \
73 for(i = 0; i<numBytes; i++) \
76 ch |= (b = (string)[i]) & mask; \
78 if(i > 1 && (!(b & 0x80) || (b & 0x40))) \
85 ch > 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF) || \
86 (ch < 0x80 && numBytes > 1) || \
87 (ch < 0x800 && numBytes > 2) || \
88 (ch < 0x10000 && numBytes > 3))\
98 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
99 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
102 bool inMultiLineComment:1, inPrep:1, escaped:1, continuedSingleLineComment:1, wasInMultiLine:1, continuedString:1, continuedQuotes:1;
104 bool recomputeSyntax:1;
105 bool cursorFollowsView:1;
107 // bool lineNumbers:1;
112 void UnregisterClass_EditBox()
115 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
124 extern int __ecereVMethodID_class_OnFree;
127 static class ArrayImpl
134 public class OldArray
139 void ** array = (void **)((ArrayImpl)this).array;
140 if(type.type == normalClass || type.type == noHeadClass)
142 for(c = 0; c<size; c++)
143 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
145 // TODO: Call OnFree for structClass
146 delete ((ArrayImpl)this).array;
155 if(((ArrayImpl)this).array)
159 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
162 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
171 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
178 public class UndoAction : struct
181 subclass(UndoAction) type;
184 virtual void Undo(void * data) { type.Undo(this, data); }
185 virtual void Redo(void * data) { type.Redo(this, data); }
187 virtual void Print(void * data) { type.Print(this, data); }
191 if(((Class)type).Destructor)
192 ((void (*)(void *))((Class)type).Destructor)(this);
196 public class UndoBuffer
198 Array<UndoAction> actions { size = 8 };
217 bool continued = true;
218 while(curAction > 0 && continued)
220 UndoAction action = actions[--curAction];
224 /*Print("Undoing: ");
225 action.Print(data);*/
230 continued = curAction > 0 && actions[curAction-1].continued;
236 bool continued = true;
237 while(curAction < count && continued)
239 UndoAction action = actions[curAction];
240 continued = action.continued;
246 /*Print("Redoing: ");
247 action.Print(data);*/
256 void Record(UndoAction action)
258 if(!dontRecord && !insideRedo)
260 if(curAction < count)
263 for(c = curAction; c < count; c++)
269 if(count >= actions.size)
270 actions.size += actions.size / 2;
273 /*Print("Recording: ");
274 action.Print(data);*/
278 if(!firstEvent && count > 0)
279 actions[count-1].continued = true;
282 actions[count++] = action;
285 if(actions.size > count + count / 2 && count + count / 2 >= 8)
286 actions.size = count + count / 2;
293 static class AddCharAction : UndoAction
297 int addedSpaces, addedTabs;
298 type = class(AddCharAction);
300 void Undo(EditBox editBox)
302 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
304 if(addedTabs || addedSpaces)
305 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
306 editBox.UpdateDirty();
309 void Redo(EditBox editBox)
311 editBox.GoToPosition(null, y, x);
313 editBox.UpdateDirty();
316 void Print(EditBox editBox)
318 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
323 static class AddTextAction : UndoAction
327 int addedSpaces, addedTabs;
328 type = class(AddTextAction);
331 void Print(EditBox editBox)
333 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
341 void Undo(EditBox editBox)
346 editBox.GoToPosition(null, y1, x1);
348 l2 = editBox.lines.first;
349 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
351 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
352 if(addedTabs || addedSpaces)
353 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
355 editBox.SetViewToCursor(true);
359 void Redo(EditBox editBox)
361 editBox.GoToPosition(null, y1, x1);
362 editBox.PutS(string);
366 static class DelTextAction : UndoAction
370 bool placeAfter, noHighlight;
372 type = class(DelTextAction);
375 void Print(EditBox editBox)
377 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
380 void Undo(EditBox editBox)
382 editBox.GoToPosition(null, y1, x1);
383 editBox.PutS(string);
387 editBox.GoToPosition(null, y1, x1);
393 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
394 //editBox.SetViewToCursor(true);
397 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
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.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
414 void Redo(EditBox editBox)
419 editBox.GoToPosition(null, y1, x1);
422 l2 = editBox.lines.first;
423 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
425 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
426 editBox.SetViewToCursor(true);
436 static class ReplaceTextAction : UndoAction
438 int y1, x1, y2, x2, y3, x3;
442 int addedSpaces, addedTabs;
444 type = class(ReplaceTextAction);
447 void Print(EditBox editBox)
449 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
452 void Undo(EditBox editBox)
457 editBox.GoToPosition(null, y1, x1);
459 l3 = editBox.lines.first;
460 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
462 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
464 editBox.PutS(oldString);
465 if(addedSpaces || addedTabs)
466 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
468 //editBox.PutS(oldString);
471 editBox.GoToPosition(null, y1, x1);
474 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
480 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
484 void Redo(EditBox editBox)
489 editBox.GoToPosition(null, y1, x1);
491 l2 = editBox.lines.first;
492 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
494 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
496 editBox.PutS(newString);
506 static class MoveTextAction : UndoAction
508 int fy1, fx1, fy2, fx2;
510 type = class(MoveTextAction);
512 void Undo(EditBox editBox)
517 void Redo(EditBox editBox)
523 public class EditLine : struct
535 // Only works for single line edit for now...
536 EditBox editBox = this.editBox;
540 get { return this ? buffer : null; }
542 property EditLine prev { get { return this ? prev : null; } };
543 property EditLine next { get { return this ? next : null; } };
544 property int count { get { return this ? count : 0; } };
551 // This makes sure the buffer always contains at least count characters
552 // Keeps a count/2 pad for avoiding always reallocating memory.
553 bool AdjustBuffer(int count)
561 newSize = (count + (count >> 1));
563 // Shrink down the buffer
566 buffer = new char[newSize];
567 if(!buffer) return false;
571 CopyBytes(buffer, this.buffer, count);
574 this.buffer = buffer;
578 // Increase the buffer
579 else if(size < count)
581 buffer = new char[newSize];
582 if(!buffer) return false;
586 CopyBytes(buffer, this.buffer, this.count + 1); // size);
589 this.buffer = buffer;
598 public struct BufferLocation
603 void AdjustDelete(BufferLocation start, BufferLocation end)
605 // Location is before, nothing to do
606 if(y < start.y || (y == start.y && x < start.x))
608 // Location is inside deleted bytes, point to the start
609 if((y >= start.y && (y > start.y || x >= start.x)) &&
610 (y >= end.y && (y > end.y || x >= end.x)))
615 // Location is on another line
617 y -= end.y - start.y;
618 // Location is the last touched line
625 //if(start.line == end.line)
626 x -= end.x - start.x;
635 // Assuming no carriage return before first character ???? fixed?
636 void AdjustAdd(BufferLocation start, BufferLocation end)
638 int numLines = end.y - start.y;
645 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
648 for(c = 0, line = start.line; c<numLines; c++)
651 //x += numLines ? end.x : (end.x - start.x);
652 x += end.x - start.x;
659 public enum EditBoxFindResult { notFound, found, wrapped };
661 static char * keyWords1[] =
664 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
665 "void", "char","int","float","double","signed","unsigned","static", "extern", "struct", "union", "typedef","enum",
667 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
668 "__attribute__", "__stdcall", "_stdcall",
669 "__declspec", "goto",
670 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
671 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
672 "__restrict__", "__restrict", "restrict",
675 "class", "private", "public",
677 "delete", "new", "new0", "renew", "renew0", "define",
680 "dllexport", "dllimport", "stdcall",
681 "subclass", "__on_register_module", "namespace", "using",
682 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
683 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
684 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
687 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
690 "this", "true", "false", "null", "value",
699 static char * keyWords2[] =
701 "defined", "warning", null
704 static char ** keyWords[] = { keyWords1, keyWords2 };
705 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
706 //static int * keyLen[NUM_KEYWORD_GROUPS];
707 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
709 static char searchString[1025], replaceString[1025];
710 static bool matchCase = false, wholeWord = false, searchUp = false;
712 static GoToDialog goToDialog
714 autoCreate = false, isModal = true, text = $"Go To"
717 public class EditBox : CommonControl
719 class_property(icon) = "<:ecere>controls/editBox.png";
723 virtual bool Window::NotifyModified(EditBox editBox);
724 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
725 virtual void Window::NotifyUpdate(EditBox editBox);
726 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
727 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
728 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
730 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
731 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
732 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
734 virtual bool Window::NotifyUnsetModified(EditBox editBox);
736 // Why was this commented out?
737 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
738 // and the scrollArea property isn't compared properly either.
742 if(font) ComputeFont();
743 SetInitSize(initSize);
749 style.vScroll = true;
755 style.hScroll = true;
759 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
760 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
761 property bool readOnly
763 property_category $"Behavior"
766 style.readOnly = value;
767 itemEditCut.disabled = value || !selection;
768 itemEditDelete.disabled = value || !selection;
769 itemEditPaste.disabled = value;
771 get { return style.readOnly; }
773 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
774 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
775 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
776 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
777 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
778 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
779 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
780 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
781 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
782 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
783 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
784 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
785 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
786 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
787 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
788 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
789 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
790 property int numLines { get { return this ? lineCount : 0; } };
791 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
792 property int column { get { return col; } }; // TODO: Add Set
793 property int charPos { get { return x; } }; // TODO: Add Set
794 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
795 property EditLine lastLine { get { return lines.last; } };
796 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
797 property char * contents
799 property_category $"Data"
804 undoBuffer.dontRecord++;
806 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
809 //SetViewToCursor(true);
812 undoBuffer.dontRecord--;
818 char * buffer = null;
821 /* Can't implement this right now because of memory leak... Need string reference counting...
828 for(line = lines.first; line; line = line.next)
829 len += strlen(line.buffer);
831 buffer = new char[len+1];
833 for(line = lines.first; line; line = line.next)
835 int lineLen = strlen(line.buffer);
836 memcpy(buffer + len, line.buffer, lineLen);
842 buffer = this.line ? this.line.buffer : null;
847 property bool overwrite { get { return overwrite; } };
848 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
850 property char * multiLineContents
854 char * buffer = null;
861 for(line = lines.first; line; line = line.next)
862 len += strlen(line.buffer)+1;
864 buffer = new char[len+1];
866 for(line = lines.first; line; line = line.next)
868 int lineLen = strlen(line.buffer);
869 memcpy(buffer + len, line.buffer, lineLen);
871 if(line.next) buffer[len++] = '\n';
884 return this.line.buffer;
889 void SetLineText(char * text)
893 EditLine_SetText(this.line, text);
897 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
898 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
899 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
900 property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };
902 // selectionStart.line, selectionStart.column (With Set)
903 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
917 // Position of Caret (Not necessarily displayed position)
920 // Position of beginning of block (Block ends at (x,y))
922 // line is line at carret, selLine is line at beginning of block
923 EditLine line, selLine, dropLine;
924 // Mouse selection Moving virtual caret
929 // ViewX is x offset in pixels, ViewY is y offset in lines
931 // viewLine is first displayed line
934 // start and end of area to redraw
937 // MaxLine is the longest line
939 // MaxLength is the longest line's length
942 // MouseSelect is true once button is pressed, overwrite is true if mode is on
943 bool mouseSelect, mouseMove, overwrite, wordSelect;
944 // Timer is used for mouse selection scrolling
947 window = this, delay = 0.1;
956 OnMouseMove(mouseX, mouseY, -1);
961 // (mouseX,mouseY) is the position of the mouse in the edit box
966 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
969 bool rightButtonDown;
972 UndoBuffer undoBuffer { data = this };
974 ColorAlpha selectionColor, selectionText;
975 SyntaxColorScheme colorScheme { };
980 Menu editMenu { menu, $"Edit", e };
983 editMenu, $"Cut\tCtrl+X", t, disabled = true;
985 bool NotifySelect(MenuItem item, Modifiers mods)
987 if(!(style.readOnly))
992 MenuItem itemEditCopy
994 editMenu, $"Copy\tCtrl+C", c, disabled = true;
996 bool NotifySelect(MenuItem item, Modifiers mods)
1002 MenuItem itemEditPaste
1004 editMenu, $"Paste\tCtrl+V", p;
1006 bool NotifySelect(MenuItem item, Modifiers mods)
1008 if(!(style.readOnly))
1013 MenuItem itemEditDelete
1015 editMenu, $"Delete\tDel", d, disabled = true;
1017 bool NotifySelect(MenuItem item, Modifiers mods)
1019 if(!(style.readOnly))
1024 MenuDivider { editMenu };
1025 MenuItem itemEditSelectAll
1027 editMenu, $"Select All\tCtrl+A", a;
1029 bool NotifySelect(MenuItem item, Modifiers mods)
1035 MenuDivider { editMenu };
1036 MenuItem itemEditUndo
1038 editMenu, $"Undo\tCtrl+Z", u;
1041 bool NotifySelect(MenuItem item, Modifiers mods)
1047 MenuItem itemEditRedo
1049 editMenu, $"Redo\tCtrl+Y", o;
1052 bool NotifySelect(MenuItem item, Modifiers mods)
1058 MenuDivider { editMenu };
1061 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1063 bool NotifySelect(MenuItem item, Modifiers mods)
1066 Find(searchString, wholeWord, matchCase, false);
1068 itemEditFind.NotifySelect(this, item, mods);
1074 editMenu, $"Find Next\tF3", n, f3;
1076 bool NotifySelect(MenuItem item, Modifiers mods)
1079 Find(searchString, wholeWord, matchCase, true);
1081 itemEditFind.NotifySelect(this, item, mods);
1085 MenuItem itemEditFind
1087 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1089 bool NotifySelect(MenuItem item, Modifiers mods)
1093 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1094 searchUp = searchUp;
1097 // Fix dialog from above shouldn't be visible
1098 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1099 void NotifyDestroyed(Window window, DialogResult result)
1101 FindDialog dialog = (FindDialog) window;
1102 searchUp = dialog.searchUp;
1103 strcpy(searchString, dialog.searchString);
1104 matchCase = dialog.matchCase;
1105 wholeWord = dialog.wholeWord;
1114 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1116 bool NotifySelect(MenuItem item, Modifiers mods)
1118 ReplaceDialog dialog
1122 searchString = searchString,
1123 replaceString = replaceString,
1124 matchCase = matchCase,
1125 wholeWord = wholeWord,
1129 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1130 void NotifyDestroyed(Window window, DialogResult result)
1132 ReplaceDialog dialog = (ReplaceDialog)window;
1133 char * replace = dialog.replaceString;
1135 strcpy(replaceString, replace);
1136 strcpy(searchString, dialog.searchString);
1137 matchCase = dialog.matchCase;
1138 wholeWord = dialog.wholeWord;
1145 MenuDivider { editMenu };
1148 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1150 bool NotifySelect(MenuItem item, Modifiers mods)
1152 goToDialog.editBox = this;
1153 goToDialog.master = master;
1154 goToDialog.Create();
1158 MenuDivider { editMenu };
1159 MenuItem itemEditInsertTab
1161 editMenu, $"Insert Tabs", i, checkable = true;
1163 bool NotifySelect(MenuItem item, Modifiers mods)
1165 style.useTab = item.checked;
1171 snapVertScroll = true;
1172 snapHorzScroll = true;
1176 static bool syntaxInit = false;
1181 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1183 for(c = 0; keyWords[g][c]; c++);
1184 //keyLen[g] = new int[c];
1185 for(c = 0; keyWords[g][c]; c++)
1187 keyLen[g][c] = strlen(keyWords[g][c]);
1191 colorScheme.commentColor = dimGray;
1192 colorScheme.charLiteralColor = crimson;
1193 colorScheme.stringLiteralColor = crimson;
1194 colorScheme.preprocessorColor = green;
1195 colorScheme.numberColor = teal;
1196 colorScheme.keywordColors = [ blue, blue ];
1199 FontExtent = Display::FontExtent;
1201 lines.offset = (uint)&((EditLine)0).prev;
1203 style = EditBoxBits { hScroll = true };
1205 // cursor = guiApp.GetCursor(IBeam);
1207 // Default Properties
1209 maxLineSize = 1024;*/
1212 maxLineSize = MAXINT;
1217 mouseSelect = this.mouseMove = false;
1220 x = selX = selY = 0;
1223 line = selLine = null;
1229 endY = clientSize.h;
1232 // Default space to 8 in case window is not constructed
1235 undoBuffer.dontRecord++;
1237 undoBuffer.dontRecord--;
1239 viewLine = lines.first;
1240 style.recomputeSyntax = true;
1246 UpdateCaretPosition(true);
1252 lines.Free(EditLine::Free);
1255 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1257 int count = wc - *renderStart;
1260 if(y + space.h >= box.top && y <= box.bottom)
1266 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1267 surface.TextFont(font);
1268 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1269 if(*x + w + XOFFSET > 0)
1270 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1275 w = numSpaces; // * space.w;
1276 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1277 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1278 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1286 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1287 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1293 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1297 *foreground = (*selected) ? selectionForeground : textColor;
1298 *background = selectionBackground;
1299 *opacity = *selected;
1306 if(this.overwrite && active)
1308 if((style.stuckCaret && wc == line.count && !line.next) ||
1309 (!mouseMove && line == this.line && wc == editX))
1318 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1322 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1323 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1324 bool inString = false;
1325 bool inQuotes = false;
1326 bool inPrep = reset ? false : style.inPrep;
1327 bool inSingleLineComment = false;
1328 bool escaped = reset ? false : style.escaped;
1329 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1330 bool continuedString = reset ? false : style.continuedString;
1331 bool continuedQuotes = reset ? false : style.continuedQuotes;
1333 EditLine line = reset ? lines.first : firstLine;
1334 // int maxBackUp = 1000, c;
1335 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1336 for(; line != viewLine; line = line.next)
1338 char * text = line.buffer;
1341 bool lastWasStar = false;
1342 bool firstWord = true;
1343 if(!escaped) inPrep = false;
1344 inSingleLineComment = continuedSingleLineComment;
1346 inString = continuedString;
1347 inQuotes = continuedQuotes;
1350 for(c = 0; (ch = text[c]); c++)
1352 bool wasEscaped = escaped;
1353 bool backLastWasStar = lastWasStar;
1354 bool backWasInMultiLine = wasInMultiLine;
1356 lastWasStar = false;
1357 wasInMultiLine = inMultiLineComment;
1360 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1362 if(text[c+1] == '/')
1364 inSingleLineComment = true;
1366 else if(text[c+1] == '*')
1368 inMultiLineComment = true;
1371 else if(backLastWasStar)
1372 inMultiLineComment = false;
1376 if(backWasInMultiLine) lastWasStar = true;
1378 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1380 if(inString && !wasEscaped)
1389 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1391 if(inQuotes && !wasEscaped)
1403 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1410 else if(ch != ' ' && ch != '\t')
1413 if(line.count && line.text[line.count - 1] == '\\')
1415 continuedSingleLineComment = inSingleLineComment;
1416 continuedString = inString;
1417 continuedQuotes = inQuotes;
1421 continuedSingleLineComment = false;
1422 continuedString = false;
1423 continuedQuotes = false;
1427 style.continuedSingleLineComment = continuedSingleLineComment;
1428 style.continuedString = continuedString;
1429 style.continuedQuotes = continuedQuotes;
1430 style.inMultiLineComment = inMultiLineComment;
1431 style.wasInMultiLine = wasInMultiLine;
1432 style.inPrep = inPrep;
1433 style.escaped = escaped;
1437 /*void OnDrawOverChildren(Surface surface)
1439 if(style.lineNumbers)
1441 int currentLineNumber = this.viewY + 1;
1444 for(i = 0; i * space.h < box.height; i++)
1446 // ********* LINE NUMBERING *********
1447 surface.SetForeground(Color{60, 60, 60});
1448 //Highlight current line
1449 if(this.caretY / space.h == currentLineNumber - 1)
1450 surface.SetBackground(Color{220, 220, 220});
1452 surface.SetBackground(Color{230, 230, 230});
1453 surface.textOpacity = true;
1455 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1456 if(currentLineNumber > this.lineCount)
1457 surface.WriteText(0,i*space.h+1," ",6);
1459 surface.WriteText(0,i*space.h+1,lineText,6);
1461 currentLineNumber++;
1466 void OnRedraw(Surface surface)
1470 bool selected = false, selection = true;
1472 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1473 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1474 Color defaultTextColor = property::foreground;
1477 int maxW = clientSize.w;
1479 Color foreground, background;
1482 // Overwrite Caret Stuff
1483 bool overWrite = false;
1484 int overWriteX, overWriteY;
1487 // ****** SYNTAX STATES ******
1488 bool inMultiLineComment = style.inMultiLineComment;
1489 bool inString = false;
1490 bool inQuotes = false;
1491 bool inPrep = style.inPrep;
1492 bool inSingleLineComment = false;
1493 bool escaped = style.escaped;
1494 bool continuedSingleLineComment = style.continuedSingleLineComment;
1495 bool continuedString = style.continuedString;
1496 bool continuedQuotes = style.continuedQuotes;
1497 bool wasInMultiLine = style.wasInMultiLine;
1498 // ****** ************* ******
1501 defaultTextColor = Color { 85, 85, 85 };
1502 textColor = defaultTextColor;
1505 Abs(selectionBackground.r - property::background.r) +
1506 Abs(selectionBackground.g - property::background.g) +
1507 Abs(selectionBackground.b - property::background.b) < 92)
1509 selectionBackground = formColor;
1510 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1513 surface.TextFont(this.font);
1515 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1516 surface.Area(0,0,MAXINT,MAXINT);
1521 surface.SetBackground(formColor);
1522 surface.Area(0,0,clientSize.w, clientSize.h);
1525 if(this.selX == this.x && this.selY == this.y)
1535 editX = Min(this.x,this.line.count);
1536 selX = Min(this.selX,this.selLine.count);
1539 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1541 foreground = selected ? selectionForeground : textColor;
1542 background = selectionBackground;
1548 surface.SetForeground(foreground);
1549 surface.SetBackground(background);
1550 surface.TextOpacity(opacity);
1552 surface.GetBox(box);
1554 for(line = this.viewLine; line; line = line.next)
1556 int x = -this.viewX;
1560 Color newTextColor = textColor = defaultTextColor;
1561 bool lineComplete = false;
1564 // ****** SYNTAX HIGHLIGHTING ******
1565 bool lastWasStar = false;
1566 bool trailingSpace = false;
1567 bool firstWord = true;
1568 if(!escaped) inPrep = false;
1569 inSingleLineComment = continuedSingleLineComment;
1571 inString = continuedString;
1572 inQuotes = continuedQuotes;
1573 // *********************************
1575 /* === DEBUGGING TOOL FOR MAXLINE ===
1577 if(line == this.maxLine)
1579 surface.SetBackground(GREEN|0xFF000000);
1580 surface.TextOpacity(true);
1584 surface.TextOpacity(selected ? true : false);
1585 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1589 if(line == this.selLine && line == this.line)
1591 end = Max(line.count, this.x);
1592 end = Max(end, this.selX);
1594 else if(line == this.selLine)
1595 end = Max(line.count, this.selX);
1596 else if(line == this.line)
1597 end = Max(line.count, this.x);
1605 bool spacing = true;
1606 bool cantHaveWords = false;
1613 lineComplete = false;
1616 textColor = newTextColor;
1619 foreground = textColor;
1620 surface.SetForeground(textColor);
1624 for(; c<end && !cantHaveWords;)
1628 bufferLen += wordLen;
1632 /*if(line.count > 4000)
1639 for(; c<line.count; c++)
1641 unichar ch = line.buffer[c];
1642 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1643 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1644 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1645 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1646 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1649 trailingSpace = false;
1655 for(; c<line.count; c++)
1657 unichar ch = line.buffer[c];
1658 if(ch == '\t' || ch == ' ')
1660 cantHaveWords = true;
1661 if(ch == ' ' && c == line.count-1)
1662 trailingSpace = true;
1666 if(ch != ' ' && ch != '\t')
1671 if(c == line.count && c < end)
1680 cantHaveWords = true;
1685 lastWasStar = false;
1691 bool backEscaped = escaped;
1692 bool backLastWasStar = lastWasStar;
1693 bool backInMultiLineComment = inMultiLineComment;
1694 bool backInString = inString;
1695 bool backInQuotes = inQuotes;
1696 bool backInPrep = inPrep;
1697 bool backInSingleLineComment = inSingleLineComment;
1698 bool backWasInMultiLine = wasInMultiLine;
1700 char * word = line.buffer + c - wordLen;
1702 bool wasEscaped = escaped;
1704 lastWasStar = false;
1706 wasInMultiLine = inMultiLineComment;
1708 // Determine Syntax Highlighting
1709 newTextColor = defaultTextColor;
1712 if(inSingleLineComment || inMultiLineComment)
1714 newTextColor = colorScheme.commentColor;
1718 newTextColor = colorScheme.charLiteralColor;
1722 newTextColor = colorScheme.stringLiteralColor;
1726 newTextColor = colorScheme.preprocessorColor;
1728 if(wordLen == 1 && word[0] == '/')
1730 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1734 inSingleLineComment = true;
1735 newTextColor = colorScheme.commentColor;
1737 else if(word[1] == '*')
1739 inMultiLineComment = true;
1740 newTextColor = colorScheme.commentColor;
1743 else if(backLastWasStar)
1744 inMultiLineComment = false;
1746 else if(wordLen == 1 && word[0] == '*')
1748 if(backWasInMultiLine)
1751 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1753 if(inString && !wasEscaped)
1760 newTextColor = colorScheme.stringLiteralColor;
1763 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1765 if(inQuotes && !wasEscaped)
1770 newTextColor = colorScheme.charLiteralColor;
1773 else if(wordLen == 1 && word[0] == '\\')
1778 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1780 char * dot = strchr(word, '.');
1781 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1787 exponent = strchr(word, 'p');
1788 if(!exponent) exponent = strchr(word, 'P');
1792 exponent = strchr(word, 'e');
1793 if(!exponent) exponent = strchr(word, 'E');
1795 if(dot && dot > word + wordLen) dot = null;
1796 isReal = dot || exponent;
1798 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1800 strtol(word, &s, 0);
1806 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1809 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1813 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1816 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1819 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1820 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1821 default: valid = false;
1825 // Don't highlight numbers with too many decimal points
1826 if(s[0] == '.' && isdigit(s[1]))
1829 while(s[0] == '.' && isdigit(s[1]))
1831 int newWordLen = s - word;
1832 c += newWordLen - wordLen;
1833 wordLen = newWordLen;
1836 newWordLen = s - word;
1837 c += newWordLen - wordLen;
1838 wordLen = newWordLen;
1842 int newWordLen = s + i - word;
1843 newTextColor = colorScheme.numberColor;
1844 c += newWordLen - wordLen;
1845 wordLen = newWordLen;
1847 else if(dot && dot > word && dot < s)
1848 newTextColor = colorScheme.numberColor;
1853 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1858 newTextColor = colorScheme.preprocessorColor;
1861 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1863 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1865 char ** keys = keyWords[g];
1866 int * len = keyLen[g];
1867 for(ccc = 0; keys[ccc]; ccc++)
1869 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1871 newTextColor = colorScheme.keywordColors[g];
1880 // If highlighting for this word is different, break
1881 if(newTextColor != textColor)
1885 // Better solution than going back?
1888 // Reset syntax flags
1889 escaped = backEscaped;
1890 lastWasStar = backLastWasStar;
1891 inMultiLineComment = backInMultiLineComment;
1892 inString = backInString;
1893 inQuotes = backInQuotes;
1894 inPrep = backInPrep;
1895 inSingleLineComment = backInSingleLineComment;
1896 wasInMultiLine = backWasInMultiLine;
1901 textColor = newTextColor;
1904 foreground = textColor;
1905 surface.SetForeground(textColor);
1912 // If we're not breaking, this can't be rendered as spacing anymore
1915 // Do word wrapping here
1921 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1926 w += numSpaces * space.w;
1928 if(x + viewX + w > maxW)
1930 // Avoid an endless loop until we fix wrapping
1931 if(c - wordLen > start)
1934 lineComplete = true;
1940 bufferLen += wordLen;
1945 int renderStart = start;
1947 bool flagTrailingSpace = false;
1951 // Render checking if we need to split because of selection or to find where to draw insert caret
1952 for(wc = start; wc < start + bufferLen; wc++)
1954 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1955 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1956 if(overWrite == true)
1958 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1959 if(overWriteCh == '\t') overWriteCh = ' ';
1964 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1965 if(flagTrailingSpace) surface.SetBackground(red);
1966 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1967 if(overWrite == true)
1975 surface.TextOpacity(opacity);
1976 surface.SetBackground(background);
1977 surface.SetForeground(foreground);
1984 if(wc < line.count && line.buffer[wc] == '\t')
1986 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1990 numSpaces += space.w;
1994 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1995 if(flagTrailingSpace) surface.SetBackground(red);
1996 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
2001 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2002 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2004 if(overWrite == true)
2011 surface.TextOpacity(opacity);
2012 surface.SetBackground(background);
2013 surface.SetForeground(foreground);
2016 if(style.freeCaret && selected)
2018 surface.SetBackground(selectionBackground);
2019 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2020 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2025 if(style.freeCaret && selected)
2027 surface.SetBackground(selectionBackground);
2028 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2031 if(line.count && line.text[line.count - 1] == '\\')
2033 continuedSingleLineComment = inSingleLineComment;
2034 continuedString = inString;
2035 continuedQuotes = inQuotes;
2039 continuedSingleLineComment = false;
2040 continuedString = false;
2041 continuedQuotes = false;
2045 if(y > box.bottom) // >=clientSize.h)
2051 surface.TextOpacity(true);
2052 surface.SetForeground(black);
2053 surface.SetBackground(Color {255,255,85});
2054 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2058 void FixScrollArea()
2060 if(style.hScroll || style.vScroll)
2062 int width = maxLength + XOFFSET;
2063 int height = lineCount * space.h;
2064 if(style.freeCaret && line)
2069 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2073 if(selX > selLine.count)
2074 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2079 SetScrollLineStep(8, space.h);
2080 SetScrollArea(width, height, true);
2084 void ComputeLength(EditLine line)
2091 for(c = 0; c < line.count; )
2100 for(len = 0; c < line.count; c += numBytes)
2102 ch = line.buffer[c];
2103 numBytes = UTF8_NUM_BYTES(ch);
2105 if(ch == ' ' || ch == '\t')
2112 if(!len && ch == ' ')
2117 else if(!len && ch == '\t')
2119 w = (tabSize * space.w) - (x % (tabSize * space.w));
2123 FontExtent(display, font, line.buffer + start, len, &w, null);
2134 if(line.length > this.maxLength)
2136 this.maxLine = line;
2137 this.maxLength = line.length;
2139 if(style.autoSize) AutoSize();
2148 this.maxLine = null;
2150 for(line = lines.first; line; line = line.next)
2152 if(line.length > this.maxLength)
2154 this.maxLength = line.length;
2155 this.maxLine = line;
2159 if(style.autoSize) AutoSize();
2164 if(this.selY != this.y)
2166 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2170 void ComputeColumn()
2172 // Adjust new edit X position according to tabs
2173 int c, position = 0;
2176 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2178 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2180 position += this.tabSize - (position % this.tabSize);
2184 position += this.x - c;
2185 this.col = position;
2188 void DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2190 _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2193 bool HasCommentOrEscape(EditLine line)
2195 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2200 for(c = line.count-1; c >= 0; c--)
2202 char ch = line.buffer[c];
2208 else //if(ch != ' ' && ch != '\t')
2215 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2217 EditLine line = l1, next;
2219 int oldCount1, oldCount2;
2220 int y, firstViewY, firstY, firstDropY, firstSelY;
2223 DelTextAction action = null;
2224 bool hadComment = false;
2228 hadComment = HasCommentOrEscape(line);
2230 if(y2 > y1 || c2 > c1)
2232 if(start < l1.count)
2234 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2238 oldCount1 = l1.count;
2240 while(c1 < oldCount1)
2242 byte ch = buffer[c1];
2243 if(UTF8_IS_FIRST(ch)) break;
2247 oldCount2 = l2.count;
2249 while(c2 < oldCount2)
2251 byte ch = buffer[c2];
2252 if(UTF8_IS_FIRST(ch)) break;
2257 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2262 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2263 string = new char[len];
2264 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2265 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2269 //oldCount1 = l1.count;
2270 //oldCount2 = l2.count;
2273 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2274 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2277 if(c2 > oldCount2) c2 = oldCount2;
2278 if(!(style.freeCaret))
2279 if(c1 > oldCount1) c1 = oldCount1;
2280 newLineCount = c1 + l2.count-c2;
2286 buffer = new char[line.size ? line.size : 1];
2288 buffer = new char[line.size];
2289 // TODO: Better handling of these allocation failures
2290 if(!buffer) return extras;
2291 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2296 // TODO: Better handling of these allocation failures
2297 if(!line.AdjustBuffer(newLineCount))
2301 /*if(newLineCount > 4000 || newLineCount < 0)
2302 printf("Warning");*/
2304 line.count = newLineCount;
2306 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2311 action.addedSpaces = c1-oldCount1;
2313 if(action.addedSpaces > action.x1)
2319 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2320 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2322 line.buffer[line.count] = '\0';
2329 this.dropX -= c2-c1;
2330 this.dropX = Max(this.dropX,0);
2332 this.x = Max(this.x,0);
2339 firstViewY = this.viewY;
2341 firstDropY = this.dropY;
2342 firstSelY = this.selY;
2343 for(line = l1;line;line = next, y++)
2351 if(line == this.viewLine)
2353 if(this.viewLine.next)
2355 this.viewLine = this.viewLine.next;
2357 style.recomputeSyntax = true;
2361 this.viewLine = this.viewLine.prev;
2363 style.recomputeSyntax = true;
2366 else if(y < firstViewY)
2368 if(line == this.line)
2372 this.line = this.line.next;
2373 this.x = this.line.count;
2378 this.line = this.line.prev;
2379 this.x = this.line.count;
2386 if(line == this.dropLine)
2388 if(this.dropLine.next)
2390 this.dropLine = this.dropLine.next;
2391 this.dropX = this.dropLine.count;
2395 this.dropLine = this.dropLine.prev;
2396 this.dropX = this.dropLine.count;
2400 else if(y < firstDropY)
2402 if(line == this.selLine)
2404 if(this.selLine.next)
2406 this.selLine = this.selLine.next;
2407 this.selX = this.selLine.count;
2411 this.selLine = this.selLine.prev;
2412 this.selX = this.selLine.count;
2416 else if(y < firstSelY)
2420 if(line == l2) break;
2424 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2427 style.recomputeSyntax = true;
2432 bool DelSel(int * addedSpacesPtr)
2434 if(this.line != this.selLine || this.x != this.selX)
2436 if(this.selY < this.y)
2438 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2441 this.line = this.selLine;
2443 else if(this.selY > this.y)
2445 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2448 this.selLine = this.line;
2450 else if(this.selX < this.x)
2452 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2455 this.line = this.selLine;
2459 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2462 this.selLine = this.line;
2470 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2472 bool hadComment = false;
2473 // Add the line here
2474 EditLine line = this.line;
2476 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2477 if(!style.hScroll && created)
2479 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2484 // Lock if no place to display.
2487 else if(endX > this.x)
2488 max = Max(this.x, line.count);
2492 for(x = 0, c = 0; c < max+count; )
2497 if(c < Min(this.x, line.count))
2498 string = line.buffer + c;
2501 else if(c < endX + count)
2502 string = stringLine + c - endX;
2504 string = line.buffer + c - endX - count;
2508 w = (tabSize * space.w) - (x % (tabSize * space.w));
2512 numBytes = UTF8_NUM_BYTES(*string);
2513 FontExtent(display, this.font, string, numBytes, &w, null);
2517 if(x >= clientSize.w)
2522 c += numBytes; // - 1;
2528 int addedSpaces = 0;
2531 // Add blank spaces if EES_FREECARET
2532 if(this.x > line.count)
2542 for(c = 0; c<line.count; c++)
2544 if(this.line.buffer[c] == '\t')
2545 position += this.tabSize - (position % this.tabSize);
2549 wantedPosition = position + (this.x - line.count);
2551 // A tab is too much...
2552 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2553 addedSpaces = wantedPosition - position;
2558 position += this.tabSize - (position % this.tabSize);
2559 // Add as many tabs as needed
2560 addedTabs += (wantedPosition - position) / this.tabSize;
2561 position += (addedTabs-1) * this.tabSize;
2562 // Finish off with spaces
2563 addedSpaces = wantedPosition - position;
2567 addedSpaces = this.x - line.count;
2570 this.x = Min(this.x, line.count);
2572 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2578 hadComment = HasCommentOrEscape(line);
2580 // Adjust the size of the line
2581 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2585 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2588 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2589 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2592 *addedTabsPtr = addedTabs;
2593 FillBytes(line.buffer+line.count,'\t',addedTabs);
2595 if(addedTabs > 4000 || addedTabs < 0)
2598 line.count += addedTabs;
2604 FillBytes(line.buffer+line.count,' ',addedSpaces);
2606 if(addedSpaces > 4000 || addedSpaces < 0)
2609 line.count += addedSpaces;
2610 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2612 else if(addedSpacesPtr)
2613 *addedSpacesPtr = 0;
2615 if(count > 4000 || count < 0)
2618 line.count += count;
2619 this.x += count + addedTabs + addedSpaces;
2622 this.selLine = this.line;
2624 line.buffer[line.count] = '\0';
2626 ComputeLength(line);
2631 hasComment = HasCommentOrEscape(line);
2632 if(!undoBuffer.insideRedo)
2634 int backDontRecord = undoBuffer.dontRecord;
2635 undoBuffer.dontRecord = 0;
2636 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2637 undoBuffer.dontRecord = backDontRecord;
2639 if(style.syntax && (hadComment || hasComment || line != this.line))
2641 style.recomputeSyntax = true;
2649 void Emptyline(EditLine line, int y)
2652 DelCh(line, y, 0, line.next, y+1, 0, true);
2654 DelCh(line, y, 0, line, y, line.count, true);
2657 void GoToEnd(bool deselect)
2661 this.line = this.lines.last;
2662 if(this.y != this.lineCount-1)
2664 else if (this.x != this.line.count)
2665 DirtyLine(this.lineCount-1);
2666 this.y = this.lineCount-1;
2667 this.x = this.line.count;
2674 void GoToHome(bool deselect)
2678 this.line = this.lines.first;
2681 else if (this.x !=0)
2682 DirtyLine(this.lineCount-1);
2691 // Returns true if it needs scrolling
2692 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2698 bool needHScroll = false;
2705 line = this.viewLine ? (void *)this.viewLine.prev : null;
2710 line = (void *)this.lines.first;
2715 py = Min(py, clientSize.h);
2717 py = Min(py, this.lineCount);
2719 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2725 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2728 px = Min(px,clientSize.w+this.space.w);
2732 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2735 if(tline) *tline = line;
2738 // Prevent divide by 0 from non valid this.font
2740 return (y < this.viewY) || needHScroll;
2742 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2746 // Minimal Update Management Functions
2750 this.endY = clientSize.h-1;
2751 // ErrorLog("DirtyAll\n");
2754 void DirtyEnd(int y)
2756 if((y - this.viewY)*this.space.h < this.startY)
2757 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2758 this.endY = clientSize.h-1;
2759 //ErrorLog("DirtyEnd %d\n", y);
2762 void DirtyLine(int y)
2766 if((y - this.viewY)*this.space.h < this.startY)
2767 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2768 if((y - this.viewY+1)*this.space.h > this.endY)
2769 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2771 //ErrorLog("DirtyLine %d\n", y);
2778 if(style.recomputeSyntax)
2780 FigureStartSyntaxStates(lines.first, true);
2781 style.recomputeSyntax = false;
2784 if(this.startY > this.endY) return;
2785 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2791 box.right = clientSize.w-1;
2792 box.top = this.startY;
2793 box.bottom = this.endY;
2796 this.startY = clientSize.h;
2800 bool IsMouseOnSelection()
2802 bool mouseOnSelection = false;
2805 int minY = Min(this.selY, this.y);
2806 int maxY = Max(this.selY, this.y);
2807 int minX = Min(this.selX, this.x);
2808 int maxX = Max(this.selX, this.x);
2810 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2812 if(maxX != minX || maxY != minY)
2814 if(y > minY && y < maxY)
2815 mouseOnSelection = true;
2816 else if(y == minY && y == maxY)
2817 mouseOnSelection = (x < maxX && x >= minX);
2821 mouseOnSelection = (x >= this.selX);
2822 else if(y == this.y)
2823 mouseOnSelection = (x >= this.x);
2828 mouseOnSelection = (x < this.selX);
2829 else if(y == this.y)
2830 mouseOnSelection = (x < this.x);
2833 return mouseOnSelection;
2836 void UpdateCaretPosition(bool setCaret)
2840 if(mouseMove || (!overwrite && !style.noCaret))
2842 int max = this.mouseMove ? this.dropX : this.x;
2843 int y = this.mouseMove ? this.dropY : this.y;
2844 EditLine line = this.mouseMove ? this.dropLine : this.line;
2846 if(!(style.freeCaret))
2847 max = Min(max, line.count);
2849 if(FontExtent && display)
2851 for(c = 0; c < max; )
2860 for(len = 0; c < Min(max, line.count); c += numBytes)
2862 ch = line.buffer[c];
2863 numBytes = UTF8_NUM_BYTES(ch);
2865 if(ch == ' ' || ch == '\t')
2872 if(!len && ch == ' ')
2877 else if(!len && ch == '\t')
2879 w = (tabSize * space.w) - (x % (tabSize * space.w));
2883 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2895 caretY = y * this.space.h;
2896 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2901 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2902 NotifyCaretMove(master, this, y + 1, x + 1);
2908 void SelectionEnables()
2910 if((x != selX || y != selY) && !selection)
2914 itemEditCut.disabled = false;
2915 itemEditDelete.disabled = false;
2917 itemEditCopy.disabled = false;
2919 this.selection = true;
2921 else if((x == selX && y == selY) && selection)
2923 itemEditCut.disabled = true;
2924 itemEditCopy.disabled = true;
2925 itemEditDelete.disabled = true;
2927 this.selection = false;
2931 void SetSelectCursor()
2933 if(!inactive || !style.noSelect)
2936 cursor = guiApp.GetCursor(arrow);
2937 else if(this.mouseSelect)
2938 cursor = guiApp.GetCursor(iBeam);
2941 if(IsMouseOnSelection())
2942 cursor = guiApp.GetCursor(arrow);
2944 cursor = guiApp.GetCursor(iBeam);
2949 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2952 int x = px ? *px : 0;
2959 if(c < Min(max, line.count))
2963 for(len = 0; c < Min(max, line.count); c += numBytes)
2965 ch = line.buffer[c];
2966 numBytes = UTF8_NUM_BYTES(ch);
2968 if(ch == ' ' || ch == '\t')
2975 if(!len && ch == ' ')
2980 else if(!len && ch == '\t')
2982 w = (tabSize * space.w) - (x % (tabSize * space.w));
2986 FontExtent(display, font, line.buffer + start, len, &w, null);
2990 if(style.freeCaret && c < max)
2999 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
3004 int a = start + len;
3007 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
3011 FontExtent(display, font, line.buffer + start, a - start, &w, null);
3014 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
3018 return Min(this.maxLineSize - 1, start + len);
3024 void SetCursorToViewX()
3026 bool selecting = this.x != selX || y != selY;
3028 // Horizontal Adjustment
3030 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3035 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3047 UpdateCaretPosition(false);
3053 void SetCursorToViewY()
3056 EditLine oldLine = this.line;
3058 bool selecting = this.x != this.selX || this.y != this.selY;
3060 numLines = clientSize.h / this.space.h;
3062 // Vertical Adjustment
3063 if(this.viewY > this.y)
3065 this.y = this.viewY;
3066 this.line = this.viewLine;
3069 if(this.viewY + numLines <= this.y)
3073 this.y = this.viewY-1;
3074 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3081 if(this.line != oldLine)
3083 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3091 this.selLine = this.line;
3094 UpdateCaretPosition(false);
3101 bool SaveFile(char * fileName)
3103 File f = eFile_Open(fileName, FO_WRITE);
3107 eWindow_SetModified(false);
3108 eInstance_Delete(f);
3115 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3119 if(!style.multiLine)
3121 x = (active && this.active && !style.readOnly) ? line.count : 0;
3124 SetViewToCursor(true);
3129 if(!active && modified)
3132 if(!NotifyModified(master, this))
3135 *goOnWithActivation = false;
3142 if(timer) timer.Stop();
3144 mouseSelect = false;
3152 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3153 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3154 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3157 bool OnResizing(int *w, int *h)
3163 *w = space.h * 80 / 14;
3167 void OnResize(int w, int h)
3170 if(!hasHorzScroll && !hasVertScroll && viewLine)
3171 SetViewToCursor(true);
3173 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3175 SetViewToCursor(true);
3180 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3182 if(style.readOnly) return true;
3183 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3184 // Middle button already serves as a dragger as well.
3189 bool OnRightButtonDown(int x, int y, Modifiers mods)
3191 this.rightButtonDown = true;
3196 bool OnRightButtonUp(int x, int y, Modifiers mods)
3199 if(!parent.inactive && rightButtonDown)
3202 Menu contextMenu { };
3204 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3205 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3206 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3207 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3208 MenuDivider { contextMenu };
3209 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3211 popup = PopupMenu { master = this, menu = contextMenu,
3213 nonClient = true, interim = false, parent = parent,
3214 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3216 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3220 rightButtonDown = false;
3224 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3229 if(style.noSelect) return true;
3231 // Should we have a separate 'selectOnActivate' style?
3232 if(!mods.isActivate || (style.readOnly && style.multiLine))
3238 mouseX = mx - XOFFSET;
3241 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3243 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3249 else if(IsMouseOnSelection() && !mods.isActivate)
3259 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3261 if(mods.shift && !mods.isActivate)
3282 UpdateCaretPosition(true);
3283 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3284 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3288 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3292 mouseSelect = false;
3303 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3305 //PrintLn("MouseMove: ", x, ", ", y);
3311 mouseMove = IsMouseOnSelection();
3315 int size = SelSize();
3318 char * text = new char[size+1];
3322 GetSel(text, false);
3324 if(Max(selY, this.y) == dropY)
3328 if(this.dropX > this.selX)
3329 moveX = this.x - this.selX;
3333 if(this.dropX > this.x)
3334 moveX = this.selX - this.x;
3338 recordUndoEvent = true;
3340 this.dropX -= moveX;
3341 this.selX = this.x = this.dropX;
3342 this.selY = this.y = this.dropY;
3343 this.selLine = this.line = this.dropLine;
3345 recordUndoEvent = false;
3347 SetViewToCursor(true);
3352 byte * c = ((EditBox)this).multiLineContents, * c2;
3353 int l1 = c ? strlen(c) : 0, l2;
3356 c2 = ((EditBox)this).multiLineContents;
3357 l2 = c2 ? strlen(c2) : 0;
3358 if(l1 != l2 || (l1 && strcmp(c, c2)))
3388 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3390 //PrintLn("Dropped: ", x, ", ", y);
3392 NotifyDropped(master, this, x, y);
3396 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3397 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3401 bool OnMouseMove(int mx, int my, Modifiers mods)
3407 if(mods != -1 && mods.isSideEffect)
3412 if(style.noSelect) return true;
3413 if(wordSelect) return true;
3414 mouseX = mx - XOFFSET;
3417 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3419 if(this.mouseMove || this.mouseSelect)
3428 ((style.hScroll) || (style.vScroll)))
3435 DirtyLine(this.dropY);
3438 DirtyLine(this.dropY);
3439 this.dropLine = line;
3440 SetViewToCursor(true);
3442 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3445 else if(this.mouseSelect)
3447 DirtyLine(this.selY);
3454 SetViewToCursor(true);
3457 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3464 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3469 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3473 if(style.noSelect) return true;
3474 FindMouse(mx, my, &x, &y, &line, false);
3475 if(!NotifyDoubleClick(master, this, line, mods))
3482 for(c = x; c >= 0; c--)
3485 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3486 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3493 for(c = start; c<line.count; c += numBytes)
3495 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3496 if(!ch || !IS_ALUNDER(ch))
3506 this.line = this.selLine = line;
3507 this.wordSelect = (c != start);
3518 Menu fileMenu { menu, "File", F };
3519 saveDialog = fileDialog;
3520 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3521 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3523 if(style.autoSize) AutoSize();
3531 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3532 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3534 space.w = Max(space.w, 1);
3535 large.w = Max(large.w, 1);
3536 space.h = Max(space.h, 1);
3537 large.h = Max(large.h, 1);
3541 for(line = lines.first; line; line = line.next)
3542 ComputeLength(line);
3547 SetViewToCursor(true);
3555 bool OnLoadGraphics()
3557 FontExtent = Display::FontExtent;
3560 // UpdateCaretPosition(true);
3564 void OnUnloadGraphics()
3569 bool OnKeyHit(Key key, unichar ch)
3571 bool shift = (key.shift) ? true : false;
3573 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3575 if(!ch && !key.alt && !key.ctrl)
3577 key.code = (SmartKey)key.code;
3579 else if(!ch && key.alt)
3582 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3585 if(style.readOnly) break;
3586 if(style.stuckCaret) GoToEnd(true);
3587 if(!(style.freeCaret))
3589 this.x = Min(this.x, this.line.count);
3595 EditLine line = this.line;
3597 for(y = this.y; y>= 0; y--)
3599 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3601 // Slow down when going on lines...
3602 if(y != this.y) break;
3606 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3607 if(IS_ALUNDER(line.buffer[c]))
3612 if(!IS_ALUNDER(line.buffer[c]))
3614 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3629 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3630 this.x = this.selX = Min(c+1, line.count);
3631 this.y = this.selY = y;
3632 this.line = this.selLine = line;
3633 SetViewToCursor(true);
3644 if(style.readOnly) break;
3645 if(style.stuckCaret) break;
3654 if(this.line != this.selLine || this.x != this.selX)
3657 SetViewToCursor(true);
3662 EditLine line1 = this.line, line2 = this.line;
3663 int x1, y1 = this.y, x2, y2 = this.y;
3664 if(!style.freeCaret)
3666 this.selX = this.x = Min(this.x, this.line.count);
3671 if(x1 < line1.count)
3678 char * buffer = line1.buffer;
3679 for(i = x1; i < line1.count; i++)
3681 if(!IS_ALUNDER(buffer[i]))
3685 for(; i < line1.count; i++)
3687 // Delete trailing whitespace
3688 if(IS_ALUNDER(buffer[i]))
3698 // Avoid creating trailing spaces if there is no content on next line
3701 if(line2 && !line2.count)
3706 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3707 SetViewToCursor(true);
3716 if(!key.alt && !key.ctrl)
3720 bool stuffAfter = false;
3723 /*bool resetX = false;
3726 if(style.stuckCaret) GoToEnd(true);
3727 if(style.readOnly) break;
3728 if(!(style.multiLine)) break;
3730 for(c = 0; c<this.line.count && c<this.x; c++)
3732 if(this.line.buffer[c] == '\t')
3733 position += this.tabSize - (position % this.tabSize);
3734 else if(this.line.buffer[c] == ' ')
3740 // Prevent adding trailing spaces if at the head of a line
3741 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3753 if(this.x < this.line.count)
3756 //If last character is a { indent one tab
3757 if(this.line.buffer[this.x - 1] == '{')
3759 //Except if the next non space character is a }
3760 bool indent = false;
3762 for(i = this.x; i < this.line.size; i++)
3763 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3765 if(this.line.buffer[i] != '}')
3770 position += this.tabSize;
3773 addString = new char[position + 2];
3774 addString[len++] = '\n';
3775 addString[len] = '\0';
3777 if(stuffAfter || !style.freeCaret)
3779 for(c = 0; c<position; )
3781 if(style.useTab && c + this.tabSize <= position)
3783 addString[len++] = '\t';
3788 addString[len++] = ' ';
3792 addString[len] = '\0';
3794 recordUndoEvent = true;
3797 EditLine prevLine = this.line.prev;
3800 // Nuke spaces if that is all that is left on previous line
3802 char * buffer = prevLine.buffer;
3803 for(i = 0; i < prevLine.count; i++)
3804 if(buffer[i] != ' ' && buffer[i] != '\t')
3806 if(i == prevLine.count)
3807 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3811 this.x = this.selX = backX;
3814 else */if(!stuffAfter && style.freeCaret)
3816 this.x = this.selX = position;
3820 SetViewToCursor(true);
3823 recordUndoEvent = false;
3831 if(style.stuckCaret) break;
3832 if(!(style.freeCaret))
3834 this.x = Min(this.x, this.line.count);
3835 this.selX = Min(this.selX, this.selLine.count);
3838 if(!shift) SelDirty();
3841 bool foundAlpha = false;
3844 EditLine line, lastLine;
3847 for(line = this.line; (line && !found); line = line.prev, y--)
3852 if(this.x == 0 && line != this.line)
3861 if(line == this.line) start = this.x -1; else start = line.count-1;
3862 start = Min(start, line.count-1);
3864 for(c = start; c >= 0;)
3867 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3885 byte ch = line.buffer[c];
3886 if(UTF8_IS_FIRST(ch)) break;
3890 // No next word found,
3891 if(!found && ( this.x > 0 || (!line.count && this.x)))
3905 this.line = lastLine;
3916 byte * buffer = line.buffer;
3919 byte ch = buffer[x];
3920 if(UTF8_IS_FIRST(ch)) break;
3937 if(!shift) _Deselect();
3938 SetViewToCursor(true);
3944 if(style.stuckCaret) break;
3945 if(!(style.freeCaret))
3947 this.x = Min(this.x, this.line.count);
3948 this.selX = Min(this.selX, this.selLine.count);
3951 if(!shift) SelDirty();
3952 if(!shift && (this.x != this.selX || this.y != this.selY));
3955 bool onAChar = false;
3956 if(this.selX != this.x || this.selY != this.y)
3958 if(this.x<this.line.count)
3959 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3961 if(key.shift && onAChar &&
3962 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3964 bool foundAlpha = false;
3966 EditLine line, lastLine;
3968 int lastC, lastY, lastNumBytes;
3970 for(line = this.line; (line && !found); line = line.next, y++)
3972 int start = (line == this.line) ? this.x : 0;
3976 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3982 lastNumBytes = numBytes;
3992 if(!found && (c != this.x || line != this.line))
4005 this.x = lastC + lastNumBytes;
4007 this.line = lastLine;
4014 bool foundAlpha = false;
4019 for(line = this.line; (line && !found); line = line.next, y++)
4021 int start = (line == this.line) ? this.x : 0;
4025 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4041 // No next word found,
4042 if(!found && (c != this.x || line != this.line))
4046 this.x = line.count;
4058 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4062 byte * buffer = line.buffer;
4065 byte ch = buffer[x];
4066 if(UTF8_IS_FIRST(ch)) break;
4087 if(!shift) _Deselect();
4088 SetViewToCursor(true);
4095 if(!style.vScroll || hasVertScroll) break;
4101 if(style.stuckCaret) break;
4103 if(!shift) SelDirty();
4113 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4117 if(!shift) _Deselect();
4119 SetViewToCursor(false);
4122 if(caretY == this.y * space.h)
4128 if(!style.freeCaret)
4129 this.x = Min(this.x, line.count);
4139 int sx = 0, sy = this.y * space.h;
4140 char * text = line.text;
4141 int maxW = clientSize.w - sx;
4142 display.FontExtent(font, " ", 1, null, &th);
4146 int startPos = textPos;
4149 bool lineComplete = false;
4151 if(!style.wrap && caretY == MAXINT)
4154 //textPos = line.count;
4155 //lineComplete = true;
4158 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4162 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4165 len = (nextSpace - (text + textPos));
4167 len = line.count - textPos;
4169 if(textPos < line.count)
4171 display.FontExtent(font, text + textPos, len, &w, null);
4173 if(nextSpace) { w += space.w; len++; }
4175 if(style.wrap && x + width + w > maxW && x > 0)
4177 lineComplete = true;
4187 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4191 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4194 if(this.x > line.count)
4197 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4198 len = this.x - startPos;
4199 display.FontExtent(font, text + startPos, len, &x, null);
4203 if(!shift) _Deselect();
4205 SetViewToCursor(false);
4209 if(sy == caretY - th || textPos >= line.count)
4211 if(textPos >= line.count)
4213 int c = textPos - 1;
4214 while(c > 0 && text[c] == ' ') c--;
4218 this.x = line.count;
4221 if(!shift) _Deselect();
4223 SetViewToCursor(false);
4228 } while(textPos < line.count);
4231 if(!shift) _Deselect();
4233 SetViewToCursor(false);
4242 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4243 if(!shift) SelDirty();
4244 this.line = this.line.prev;
4250 if(!shift) _Deselect();
4254 SetViewToCursor(false);
4260 return style.multiLine ? false : true;
4264 if(!style.vScroll || hasVertScroll)
4271 if(style.stuckCaret) break;
4275 int sx = 0, sy = this.y * this.space.h;
4276 int maxW = clientSize.w - sx;
4277 char * text = line.buffer;
4279 if(!shift) SelDirty();
4285 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4295 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4299 if(!shift) _Deselect();
4301 SetViewToCursor(false);
4304 while(!textPos || (style.freeCaret || textPos<line.count))
4306 int startPos = textPos;
4309 bool lineComplete = false;
4310 if(!style.wrap && sy <= caretY)
4312 textPos = line.count;
4313 lineComplete = true;
4315 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4319 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4322 len = (nextSpace - (text + textPos));
4324 len = line.count - textPos;
4326 if(textPos < line.count)
4328 display.FontExtent(font, text + textPos, len, &w, &th);
4330 if(nextSpace) { w += space.w; len++; }
4331 if(style.wrap && x + width + w > maxW && x > 0)
4333 lineComplete = true;
4343 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4347 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4350 if(this.x > line.count)
4353 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4355 len = this.x - startPos;
4356 display.FontExtent(font, text + startPos, len, &x, null);
4359 if(!shift) _Deselect();
4362 SetViewToCursor(false);
4368 this.x = line.count;
4371 if(!shift) _Deselect();
4374 SetViewToCursor(false);
4377 else if(textPos >= line.count && line.next)
4383 sy = this.y * this.space.h;
4384 sx = 0; //textBlock.startX;
4390 sx = 0; //textBlock.startX;
4398 this.x = Min(this.x, line.count);
4399 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4401 if(!shift) _Deselect();
4404 if(this.selX != this.x || this.selY != this.y)
4407 SetViewToCursor(false);
4414 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4415 if(!shift) SelDirty();
4416 this.line = this.line.next;
4420 if(!shift) _Deselect();
4423 if(this.selX != this.x || this.selY != this.y)
4431 return style.multiLine ? false : true;
4434 if(style.stuckCaret) break;
4435 if(!style.multiLine && key.ctrl) break;
4436 if(!(style.freeCaret))
4437 this.selX = Min(this.selX, this.selLine.count);
4439 if(!shift) SelDirty();
4442 this.line = this.lines.first;
4443 if(this.y != 0 || this.x != 0)
4453 EditLine line = this.line;
4455 for(c=0; line.buffer[c]; c++)
4456 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4458 if(shift && (c != 0 || this.x))
4467 if(shift && this.x != 0)
4473 if(!shift) _Deselect();
4474 SetViewToCursor(true);
4480 if(style.stuckCaret) break;
4481 if(!style.multiLine && key.ctrl) break;
4482 if(!style.freeCaret)
4483 this.selX = Min(this.selX, this.selLine.count);
4485 if(!shift) SelDirty();
4490 else if(this.x != this.line.count)
4492 this.x = this.line.count;
4497 if(!shift) _Deselect();
4498 SetViewToCursor(true);
4503 if(style.tabKey && !key.ctrl)
4505 if(this.selY != this.y && style.tabSel)
4507 EditLine firstLine, lastLine;
4511 // Do multi line selection tabbing here
4512 if(this.selY < this.y)
4514 firstLine = this.selLine;
4515 lastLine = this.line;
4521 // Selecting going up
4522 firstLine = this.line;
4523 lastLine = this.selLine;
4530 for(line = firstLine; line; line = line.next, y++)
4532 if(line != lastLine || x)
4536 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4538 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4540 if(line.buffer[c] == '\t')
4545 else if(line.buffer[c] != ' ')
4550 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4553 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4554 char * string = new char[len];
4555 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4556 GetText(string, line, y, 0, line, y, lastC, false, false);
4559 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4560 if(!line.AdjustBuffer(line.count-lastC))
4566 if(line == lastLine) break;
4571 for(line = firstLine; line; line = line.next, y++)
4575 if(line != lastLine || x)
4579 if(line.count + 1 <= this.maxLineSize)
4581 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4583 if(!line.AdjustBuffer(line.count+1))
4586 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4588 AddCharAction action { ch = '\t', x = 0, y = y };
4592 memmove(line.buffer+1,line.buffer,line.size-1);
4594 line.buffer[0] = '\t';
4599 if(line.count + this.tabSize <= this.maxLineSize)
4602 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4603 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4605 if(!line.AdjustBuffer(line.count+this.tabSize))
4609 char * string = new char[this.tabSize + 1];
4610 memset(string, ' ', this.tabSize);
4611 string[this.tabSize] = '\0';
4612 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4615 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4616 line.count+=this.tabSize;
4617 for(c=0; c<this.tabSize; c++)
4618 line.buffer[c] = ' ';
4624 if(line == lastLine) break;
4627 ComputeLength(maxLine);
4639 char * addString = new char[this.tabSize + 1];
4641 if(!(style.freeCaret))
4643 this.x = Min(this.x, this.line.count);
4647 start = Min(this.x, this.selX);
4648 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4650 addString[len++] = ' ';
4658 SetViewToCursor(true);
4667 if(!(style.hScroll) || hasHorzScroll) break;
4668 if(this.viewX < this.maxLength)
4670 //this.viewX+=this.space.w*this.tabSize;
4672 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4679 if(!shift) _Deselect();
4691 if(!(style.hScroll) || hasHorzScroll) break;
4694 //this.viewX-=this.space.w*this.tabSize;
4695 //this.viewX = Max(this.viewX,0);
4697 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4698 // SetCursorToView();
4705 if(!shift) _Deselect();
4718 else if(!style.readOnly)
4722 if(!(style.readOnly))
4728 this.overwrite ^= 1;
4729 UpdateCaretPosition(true);
4734 NotifyOvrToggle(master, this, this.overwrite);
4746 if(style.noSelect) break;
4749 //Save current view position
4750 int tempX = this.viewX;
4751 int tempY = this.viewY;
4755 this.selLine = this.lines.first;
4756 this.y = this.lineCount-1;
4757 this.line = this.lines.last;
4758 this.x = this.line.count;
4761 SetViewToCursor(true);
4763 //Restore previous view position
4764 SetScrollPosition(tempX, tempY * this.space.h);
4770 // TOCHECK: Was there any good reason why we weren't returning false here?
4771 return false; // break;
4776 if(!(style.readOnly))
4784 if(style.readOnly) break;
4790 if(style.readOnly) break;
4795 if(style.readOnly) break;
4799 if(style.readOnly) break;
4803 if(style.readOnly) break;
4804 if(key.shift && key.code == rightBracket)
4806 //Only indent back if you are exactly at one tab.
4808 bool whitespace = true;
4814 EditLine line = this.line;
4816 //Only remove one tab if there is nothing else on the line.
4817 for(i = 0; i < this.line.count; i++)
4819 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4833 for(pos = line.count - 1; pos >= 0; pos--)
4835 char c = line.buffer[pos];
4839 indentwidth += this.tabSize;
4841 //Counting backwards, so when you find a character, indentation is reset
4851 //Place the } to get an undo:
4854 this.x = this.line.count;
4858 putsize = indentwidth;
4860 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4862 newline = new char[putsize+2];
4863 newline[putsize] = '}';
4864 newline[putsize+1] = '\0';
4868 for(; i < indentwidth / this.tabSize; i++)
4870 for(;i < putsize; i++)
4879 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4891 void OnHScroll(ScrollBarAction action, int position, Key key)
4894 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4896 this.viewX = position;
4897 if(action != setRange)
4899 if(!this.mouseMove && style.cursorFollowsView)
4906 void OnVScroll(ScrollBarAction action, int position, Key key)
4908 int oldViewY = this.viewY;
4911 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4913 position /= this.space.h;
4915 if(position < this.viewY)
4917 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4918 style.recomputeSyntax = true;
4920 else if(position > this.viewY)
4922 EditLine oldViewLine = viewLine;
4923 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4924 FigureStartSyntaxStates(oldViewLine, false);
4927 if(action != setRange)
4929 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4932 if(this.x != this.selX || this.y != this.selY)
4936 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4939 int numLines = clientSize.h / this.space.h;
4941 if(Abs(this.viewY - oldViewY) < numLines)
4944 if(this.viewY > oldViewY)
4946 for(y = oldViewY; y <this.viewY; y++)
4947 DirtyLine(y + numLines);
4951 for(y = this.viewY; y <oldViewY; y++)
4952 DirtyLine(y + numLines);
4957 // Fix dirt of stuff before first line...
4958 if(this.viewY - oldViewY > 0)
4960 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4967 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4972 ReplaceTextAction replaceAction = null;
4973 AddCharAction addCharAction = null;
4974 int addedSpaces = 0, addedTabs = 0;
4976 if(ch == '\r') return true;
4977 if(style.stuckCaret /*|EES_READONLY)*/ )
4980 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4982 if(!undoBuffer.dontRecord)
4984 if(selX != x || selY != y)
4989 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4990 oldString = new char[len];
4991 UTF32toUTF8Len(&ch, 1, buffer, 4);
4992 newString = CopyString(buffer);
4993 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4995 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4996 if(selY < y || (selY == y && selX < x))
4998 replaceAction.x1 = selX;
4999 replaceAction.y1 = selY;
5000 replaceAction.x2 = x;
5001 replaceAction.y2 = y;
5005 replaceAction.x1 = x;
5006 replaceAction.y1 = y;
5007 replaceAction.x2 = selX;
5008 replaceAction.y2 = selY;
5010 Record(replaceAction);
5011 undoBuffer.dontRecord++;
5015 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5016 Record(addCharAction);
5022 DelSel(&addedSpaces);
5023 if(this.lineCount+1 > this.maxLines)
5026 Emptyline(this.lines.first,0);
5030 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5032 // Make sure it fits, but we need a default line is this.font is too big for window
5033 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5036 if((this.y >= 0) && this.y < this.viewY)
5041 line = EditLine { };
5044 lines.Insert(this.line, line);
5045 line.editBox = this;
5051 // If we're displacing the lines from a current line ...
5054 if(this.line.buffer)
5057 if(this.line.count < endX) endX = this.line.count;
5058 length = this.line.count - endX;
5061 if(!line.AdjustBuffer(length))
5065 if(this.line.buffer)
5067 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5069 if(endX > 4000 || endX < 0)
5072 this.line.count = endX;
5073 this.line.buffer[this.line.count] = '\0';
5074 this.line.AdjustBuffer(this.line.count);
5075 ComputeLength(this.line);
5079 BufferLocation before = { this.line, this.y, this.x }, after;
5084 line.count = length;
5087 if(length > 4000 || length < 0)
5090 ComputeLength(this.line);
5095 line.buffer[line.count] = '\0';
5098 after.line = this.line, after.y = this.y, after.x = this.x;
5100 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5106 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5107 DelSel(&addedSpaces);
5108 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5109 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5110 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5114 this.selLine = this.line;
5118 replaceAction.x3 = x;
5119 replaceAction.y3 = y;
5120 replaceAction.addedSpaces = addedSpaces;
5121 replaceAction.addedTabs = addedTabs;
5122 undoBuffer.dontRecord--;
5126 addCharAction.x -= addedTabs * (tabSize-1);
5127 addCharAction.addedSpaces = addedSpaces;
5128 addCharAction.addedTabs = addedTabs;
5135 /****************************************************************************
5137 ****************************************************************************/
5140 bool AddCh(unichar ch)
5142 return _AddCh(ch, null, null);
5147 this.modified = true;
5148 NotifyUpdate(master, this);
5149 modifiedDocument = true;
5152 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5155 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5156 SetViewToCursor(true);
5164 itemEditUndo.disabled = undoBuffer.curAction == 0;
5165 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5166 if(savedAction == undoBuffer.curAction)
5168 modifiedDocument = false;
5170 NotifyUnsetModified(master, this);
5177 itemEditUndo.disabled = undoBuffer.curAction == 0;
5178 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5179 if(savedAction == undoBuffer.curAction)
5181 modifiedDocument = false;
5183 NotifyUnsetModified(master, this);
5187 void Record(UndoAction action)
5189 if(!undoBuffer.dontRecord)
5191 undoBuffer.Record(action);
5192 itemEditUndo.disabled = undoBuffer.curAction == 0;
5193 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5200 this.lines.first, 0,0,
5201 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5204 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5209 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5210 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5211 this.y = line2 ? y2 : (this.lineCount-1);
5212 this.line = line2 ? (EditLine)line2 : this.lines.last;
5215 SetViewToCursor(true);
5219 // TODO: Fix this vs modifiedDocument window property
5220 void SetModified(bool flag)
5224 this.modified = false;
5225 if(flag && !NotifyModified(master, this))
5226 this.modified = true;
5231 bool AddS(char * string)
5238 int addedSpaces = 0, addedTabs = 0;
5239 AddTextAction action = null;
5240 ReplaceTextAction replaceAction = null;
5242 this.pasteOperation = true;
5244 if(style.stuckCaret /*|EES_READONLY)*/ )
5247 if(!undoBuffer.dontRecord)
5249 char * placeString = CopyString(string);
5250 if(!style.multiLine)
5254 for(i = 0; (ch = placeString[i]); i++)
5257 placeString[i] = '\0';
5258 placeString = renew placeString byte[i+1];
5263 if(selX != x || selY != y)
5267 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5268 oldString = new char[len];
5269 newString = placeString;
5270 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5272 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5273 if(selY < y || (selY == y && selX < x))
5275 replaceAction.x1 = selX;
5276 replaceAction.y1 = selY;
5277 replaceAction.x2 = x;
5278 replaceAction.y2 = y;
5282 replaceAction.x1 = x;
5283 replaceAction.y1 = y;
5284 replaceAction.x2 = selX;
5285 replaceAction.y2 = selY;
5287 Record(replaceAction);
5291 if(string[0] == '\n')
5292 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5294 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5302 undoBuffer.dontRecord++;
5303 DelSel(&addedSpaces);
5307 for(c = 0; string[c]; c++)
5309 if(string[c] == '\n' || string[c] == '\r')
5311 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5316 if(string[c] == '\n')
5325 // Reset for next line
5329 if(string[c] == '\r' && *line == '\n')
5341 // Why was this here?
5344 // Add the line here
5346 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5352 undoBuffer.dontRecord--;
5357 action.addedSpaces = addedSpaces;
5358 action.addedTabs = addedTabs;
5360 else if(replaceAction)
5362 replaceAction.y3 = y;
5363 replaceAction.x3 = x;
5364 replaceAction.addedSpaces = addedSpaces;
5365 replaceAction.addedTabs = addedTabs;
5368 UpdateCaretPosition(true);
5370 this.pasteOperation = false;
5383 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5384 SetViewToCursor(true);
5390 void PutCh(unichar ch)
5394 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5395 //if((ch >= 32) || ch == '\n')
5397 int addedSpaces = 0, addedTabs = 0;
5398 ReplaceTextAction replaceAction = null;
5399 AddCharAction addCharAction = null;
5402 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5404 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5409 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5410 oldString = new char[len];
5411 UTF32toUTF8Len(&ch, 1, buffer, 4);
5412 newString = CopyString(buffer);
5413 GetText(oldString, line, y, x, line, y, x+1, false, false);
5414 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5415 replaceAction.x1 = x;
5416 replaceAction.y1 = y;
5417 replaceAction.x2 = x+1;
5418 replaceAction.y2 = y;
5419 Record(replaceAction);
5421 undoBuffer.dontRecord++;
5422 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5423 undoBuffer.dontRecord--;
5425 else if(!undoBuffer.dontRecord)
5427 if(selX != x || selY != y)
5432 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5433 oldString = new char[len];
5434 UTF32toUTF8Len(&ch, 1, buffer, 4);
5435 newString = CopyString(buffer);
5436 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5437 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5438 if(selY < y || (selY == y && selX < x))
5440 replaceAction.x1 = selX;
5441 replaceAction.y1 = selY;
5442 replaceAction.x2 = x;
5443 replaceAction.y2 = y;
5447 replaceAction.x1 = x;
5448 replaceAction.y1 = y;
5449 replaceAction.x2 = selX;
5450 replaceAction.y2 = selY;
5452 Record(replaceAction);
5456 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5457 Record(addCharAction);
5460 undoBuffer.dontRecord++;
5461 result = _AddCh(ch, &addedSpaces, &addedTabs);
5464 replaceAction.x3 = x;
5465 replaceAction.y3 = y;
5466 replaceAction.addedSpaces = addedSpaces;
5467 replaceAction.addedTabs = addedTabs;
5471 addCharAction.x -= addedTabs * (tabSize-1);
5472 addCharAction.addedSpaces = addedSpaces;
5473 addCharAction.addedTabs = addedTabs;
5475 undoBuffer.dontRecord--;
5479 if(result) SetViewToCursor(true);
5483 void PutS(char * string)
5488 SetViewToCursor(true);
5493 void Printf(char * format, ...)
5497 char temp[MAX_F_STRING];
5499 va_start(args, format);
5500 vsnprintf(temp, sizeof(temp), format, args);
5501 temp[sizeof(temp)-1] = 0;
5507 void SetContents(char * format, ...)
5511 undoBuffer.dontRecord++;
5513 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5516 char temp[MAX_F_STRING];
5518 va_start(args, format);
5519 vsnprintf(temp, sizeof(temp), format, args);
5520 temp[sizeof(temp)-1] = 0;
5527 undoBuffer.dontRecord--;
5540 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5543 else if(this.line.prev)
5545 EditLine line = this.line.prev;
5549 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5557 this.selLine = this.line;
5562 SetViewToCursor(true);
5567 Emptyline(this.line,this.y);
5568 this.selX = this.x = 0;
5571 this.selLine = this.line;
5573 SetViewToCursor(true);
5583 SetViewToCursor(true);
5591 SetViewToCursor(true);
5595 bool GoToLineNum(int lineNum)
5600 EditLine line = this.lines.first;
5601 for(c = 0; c < lineNum && line; c++, line = line.next);
5611 SetViewToCursor(true);
5618 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5619 bool GoToPosition(EditLine line, int y, int x)
5631 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5645 SetViewToCursor(true);
5652 void SetViewToCursor(bool setCaret)
5662 bool dontScroll = false;
5668 selected = selX != this.x || selY != y;
5675 checkLine = dropLine;
5681 checkLine = this.line;
5686 numLines = clientSize.h / space.h;
5688 // This is broken. The EditBox now doesn't do anything different when adding to it,
5689 // regardless of the previous scrolling position. It should be read and then set again
5690 // if one wishes to preserve it.
5691 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5692 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5696 // Horizontal Adjustment
5697 if(!dontScroll && checkLine)
5701 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5704 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5710 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5711 viewX = x - clientSize.w+space.w;
5712 if(x < this.viewX + clientSize.w/2 - space.w)
5713 viewX = Max(0, x - clientSize.w/2 + space.w);
5721 // Vertical Adjustment
5722 if(viewY > checkY) viewY = checkY;
5723 if(viewY + numLines <= checkY)
5725 if(clientSize.h >= space.h)
5727 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5737 for(;dropLine && dropLine.prev && dropY >= numLines;)
5739 dropLine = dropLine.prev;
5743 for(;this.line && this.line.prev && this.y >= numLines;)
5745 this.line = this.line.prev;
5750 SetScrollPosition(viewX, viewY * this.space.h);
5752 UpdateCaretPosition(setCaret);
5758 selLine = this.line;
5768 void CenterOnCursor()
5770 int numLines = clientSize.h / this.space.h;
5771 int y = this.y - numLines / 2;
5773 bool figureSyntax = false;
5774 EditLine oldViewLine = viewLine;
5775 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5780 for(;y < this.viewY; y++)
5782 this.viewLine = this.viewLine.prev;
5783 style.recomputeSyntax = true;
5785 for(;y > this.viewY; y--)
5787 this.viewLine = this.viewLine.next;
5788 figureSyntax = true;
5791 FigureStartSyntaxStates(oldViewLine, false);
5795 SetScrollPosition(this.viewX, viewY * this.space.h);
5796 UpdateCaretPosition(true);
5800 void SetCursorToView()
5811 numLines = clientSize.h / this.space.h;
5815 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5816 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5820 EditLine oldLine = this.line;
5821 bool lastOne = false;
5822 EditLine oldViewLine = this.viewLine;
5823 bool figureSyntax = false;
5825 if(this.y >= this.lineCount-1) return;
5827 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5829 if(this.viewY + numLines < this.lines.count)
5831 this.viewLine = this.viewLine.next;
5833 figureSyntax = true;
5835 else if(c && !lastOne)
5844 FigureStartSyntaxStates(oldViewLine, false);
5845 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5848 SetViewToCursor(false);
5857 if(this.y == 0) return;
5859 numLines = clientSize.h / this.space.h;
5863 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5864 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5868 EditLine oldLine = this.line;
5870 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5875 if(this.viewLine.prev)
5877 this.viewLine = this.viewLine.prev;
5879 style.recomputeSyntax = true;
5883 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5886 SetViewToCursor(false);
5893 if(this.viewLine.prev)
5896 // this.viewLine = this.viewLine.prev;
5899 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5906 if(this.viewLine.next)
5909 // this.viewLine = this.viewLine.next;
5912 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5919 EditLine l1, l2, line;
5920 int x1, x2, nx1, nx2;
5925 if(!this.selLine) return 0;
5926 if(this.selLine == this.line && this.selX == this.x) return 0;
5927 if(this.selY < this.y)
5934 else if(this.selY > this.y)
5941 else if(this.selX < this.x)
5943 l1 = l2 = this.line;
5949 l1 = l2 = this.line;
5953 nx1 = Min(x1,l1.count);
5954 nx2 = Min(x2,l2.count);
5956 // Find Number of Bytes Needed
5958 for(line = l1; line; line = line.next)
5960 if(line == l1) start = nx1; else start = 0;
5961 if(line == l2) end = nx2; else end = line.count;
5963 if(style.freeCaret && line == l2)
5966 count = Max(x2-Max(x1,l1.count),0);
5968 count = Max(x2-l2.count,0);
5972 if(line == l2) break;
5973 // Add Carriage Return / line Feed
5980 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5984 if(!reorder || this.selY < this.y)
5986 if(l1) *l1 = this.selLine;
5987 if(y1) *y1 = this.selY;
5988 if(x1) *x1 = this.selX;
5989 if(l2) *l2 = this.line;
5990 if(y2) *y2 = this.y;
5991 if(x2) *x2 = this.x;
5993 else if(this.selY > this.y)
5995 if(l1) *l1 = this.line;
5996 if(y1) *y1 = this.y;
5997 if(x1) *x1 = this.x;
5998 if(l2) *l2 = this.selLine;
5999 if(y2) *y2 = this.selY;
6000 if(x2) *x2 = this.selX;
6002 else if(this.selX < this.x)
6004 if(l1) *l1 = this.line;
6005 if(y1) *y1 = this.selY;
6006 if(x1) *x1 = this.selX;
6007 if(l2) *l2 = this.line;
6008 if(y2) *y2 = this.y;
6009 if(x2) *x2 = this.x;
6013 if(l1) *l1 = this.line;
6014 if(y1) *y1 = this.y;
6015 if(x1) *x1 = this.x;
6016 if(l2) *l2 = this.line;
6017 if(y2) *y2 = this.selY;
6018 if(x2) *x2 = this.selX;
6023 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6025 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6027 this.selLine = (EditLine)l1;
6030 this.line = (EditLine)l2;
6034 SetViewToCursor(true);
6038 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6040 EditLine l1, l2, line;
6041 int x1, x2, nx1, nx2;
6072 nx1 = Min(x1,l1.count);
6073 nx2 = Min(x2,l2.count);
6076 for(line = l1; line; line = line.next)
6078 if(line == l1) start = nx1; else start = 0;
6079 if(line == l2) end = nx2; else end = line.count;
6082 CopyBytes(text, line.buffer + start, end - start);
6085 numChars += end-start;
6087 if(style.freeCaret && line == l2 && addSpaces)
6090 count = Max(x2-Max(x1,l1.count),0);
6092 count = Max(x2-l2.count,0);
6095 FillBytes(text,' ',count);
6101 if(line == l2) break;
6113 // '\0' terminate Terminate
6120 void GetSel(char * text, bool addCr)
6122 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6125 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6143 int size = SelSize();
6146 // Try to allocate memory
6147 ClipBoard clipBoard { };
6148 if(clipBoard.Allocate(size+1))
6150 GetSel(clipBoard.memory, true);
6163 ClipBoard clipBoard { };
6164 if(clipBoard.Load())
6165 PutS(clipBoard.memory);
6177 SetViewToCursor(true);
6183 void DeleteSelection()
6188 SetViewToCursor(true);
6194 void Save(File f, bool cr)
6197 savedAction = undoBuffer.curAction;
6199 for(line = this.lines.first; line; line = line.next)
6201 f.Write(line.buffer, line.count, 1);
6204 if(cr) f.Putc('\r');
6210 #define BUFFER_SIZE 16384
6213 undoBuffer.dontRecord++;
6216 char buffer[BUFFER_SIZE];
6220 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6221 buffer[count] = '\0';
6227 undoBuffer.dontRecord--;
6228 undoBuffer.count = 0;
6229 undoBuffer.curAction = 0;
6230 itemEditUndo.disabled = undoBuffer.curAction == 0;
6231 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6234 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6238 bool firstPass = true;
6239 EditBoxFindResult result = found;
6241 if(!this.line) return notFound;
6244 for(line = this.line;;)
6252 line = this.lines.first;
6258 line = this.lines.last;
6259 num = this.lineCount - 1;
6265 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6267 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6271 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6274 if(line == this.line && !firstPass) break;
6292 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6296 int searchLen = strlen(text);
6297 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6299 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6300 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6302 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6309 bool OnKeyDown(Key key, unichar ch)
6312 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6314 if(!NotifyKeyDown(master, this, key, ch))
6324 this.mouseMove = false;
6325 OnLeftButtonUp(0,0,0);
6326 SetViewToCursor(true);
6336 public class EditBoxStream : File
6339 BufferLocation start, sel;
6346 EditBox editBox = this.editBox;
6348 editBox.x = start.x;
6349 editBox.y = start.y;
6350 editBox.line = start.line;
6352 editBox.selX = sel.x;
6353 editBox.selY = sel.y;
6354 editBox.selLine = sel.line;
6356 editBox.SetViewToCursor(true);
6357 //editBox.ComputeColumn();
6361 property EditBox editBox
6369 start.line = value.line;
6373 sel.line = value.selLine;
6376 value.GoToHome(true);
6379 get { return editBox; }
6382 uint Read(byte * buffer, uint size, uint count)
6385 EditBox editBox = this.editBox;
6386 EditLine line = editBox.line;
6392 for(;read < count && line; line = (*&line.next))
6394 int numBytes = Min(count - read, (*&line.count) - x);
6397 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6402 for(;read < count && x < (*&line.count);)
6404 buffer[read++] = (*&line.buffer)[x++];
6411 buffer[read++] = '\n';
6416 if(x == (*&line.count) && (*&line.next))
6425 editBox.line = editBox.selLine = line;
6426 editBox.x = editBox.selX = x;
6427 editBox.y = editBox.selY = y;
6432 bool Seek(int pos, FileSeekMode mode)
6435 EditBox editBox = this.editBox;
6436 EditLine line = editBox.line;
6438 if(mode == FileSeekMode::start)
6440 pos = pos - this.pos;
6450 for(;read < pos && line; line = (*&line.next))
6452 int numBytes = Min(pos - read, (*&line.count) - x);
6455 read += numBytes; x += numBytes;
6458 /*for(;read < pos && x < (*&line.count);)
6486 for(;read < pos && line; line = (*&line.prev))
6488 int numBytes = Min(pos - read, x);
6494 /*for(;read < pos && x > 0;)
6504 x = (*&(*&line.prev).count);
6520 editBox.line = editBox.selLine = line;
6521 editBox.x = editBox.selX = x;
6522 editBox.y = editBox.selY = y;
6527 bool Puts(char * string)
6529 EditBox editBox = this.editBox;
6530 BufferLocation start { editBox.line, editBox.y, editBox.x };
6534 editBox.AddS(string);
6536 pos.line = editBox.line;
6540 this.start.AdjustAdd(start, pos);
6541 sel.AdjustAdd(start, pos);
6546 // NOTE: BYTE, NOT UNICODE CHARACTER!
6549 EditBox editBox = this.editBox;
6550 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6555 utf8Bytes[numBytes++] = ch;
6556 utf8Bytes[numBytes] = 0;
6557 if(UTF8Validate(utf8Bytes))
6559 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6561 pos.line = editBox.line;
6564 this.start.AdjustAdd(start, pos);
6565 sel.AdjustAdd(start, pos);
6572 bool Getc(char * ch)
6574 return Read(ch, 1, 1) ? true : false;
6577 void DeleteBytes(uint count)
6579 EditBox editBox = this.editBox;
6582 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6583 BufferLocation end = pos;
6588 for(;c < count && end.line && end.x < end.line.count;)
6593 if(c < count && end.line && end.line.next)
6595 end.line = end.line.next;
6604 start.AdjustDelete(pos, end);
6605 sel.AdjustDelete(pos, end);
6607 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);