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(exponent && exponent > word + wordLen) exponent = null;
1796 if(dot && dot > word + wordLen) dot = null;
1797 isReal = dot || exponent;
1799 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1801 strtol(word, &s, 0);
1807 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1810 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1814 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1817 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1820 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1821 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1822 default: valid = false;
1826 // Don't highlight numbers with too many decimal points
1827 if(s[0] == '.' && isdigit(s[1]))
1830 while(s[0] == '.' && isdigit(s[1]))
1832 int newWordLen = s - word;
1833 c += newWordLen - wordLen;
1834 wordLen = newWordLen;
1837 newWordLen = s - word;
1838 c += newWordLen - wordLen;
1839 wordLen = newWordLen;
1843 int newWordLen = s + i - word;
1844 newTextColor = colorScheme.numberColor;
1845 c += newWordLen - wordLen;
1846 wordLen = newWordLen;
1848 else if(dot && dot > word && dot < s)
1849 newTextColor = colorScheme.numberColor;
1854 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1859 newTextColor = colorScheme.preprocessorColor;
1862 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1864 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1866 char ** keys = keyWords[g];
1867 int * len = keyLen[g];
1868 for(ccc = 0; keys[ccc]; ccc++)
1870 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1872 newTextColor = colorScheme.keywordColors[g];
1881 // If highlighting for this word is different, break
1882 if(newTextColor != textColor)
1886 // Better solution than going back?
1889 // Reset syntax flags
1890 escaped = backEscaped;
1891 lastWasStar = backLastWasStar;
1892 inMultiLineComment = backInMultiLineComment;
1893 inString = backInString;
1894 inQuotes = backInQuotes;
1895 inPrep = backInPrep;
1896 inSingleLineComment = backInSingleLineComment;
1897 wasInMultiLine = backWasInMultiLine;
1902 textColor = newTextColor;
1905 foreground = textColor;
1906 surface.SetForeground(textColor);
1913 // If we're not breaking, this can't be rendered as spacing anymore
1916 // Do word wrapping here
1922 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1927 w += numSpaces * space.w;
1929 if(x + viewX + w > maxW)
1931 // Avoid an endless loop until we fix wrapping
1932 if(c - wordLen > start)
1935 lineComplete = true;
1941 bufferLen += wordLen;
1946 int renderStart = start;
1948 bool flagTrailingSpace = false;
1952 // Render checking if we need to split because of selection or to find where to draw insert caret
1953 for(wc = start; wc < start + bufferLen; wc++)
1955 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1956 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1957 if(overWrite == true)
1959 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1960 if(overWriteCh == '\t') overWriteCh = ' ';
1965 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1966 if(flagTrailingSpace) surface.SetBackground(red);
1967 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1968 if(overWrite == true)
1976 surface.TextOpacity(opacity);
1977 surface.SetBackground(background);
1978 surface.SetForeground(foreground);
1985 if(wc < line.count && line.buffer[wc] == '\t')
1987 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1991 numSpaces += space.w;
1995 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1996 if(flagTrailingSpace) surface.SetBackground(red);
1997 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
2002 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2003 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2005 if(overWrite == true)
2012 surface.TextOpacity(opacity);
2013 surface.SetBackground(background);
2014 surface.SetForeground(foreground);
2017 if(style.freeCaret && selected)
2019 surface.SetBackground(selectionBackground);
2020 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2021 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2026 if(style.freeCaret && selected)
2028 surface.SetBackground(selectionBackground);
2029 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2032 if(line.count && line.text[line.count - 1] == '\\')
2034 continuedSingleLineComment = inSingleLineComment;
2035 continuedString = inString;
2036 continuedQuotes = inQuotes;
2040 continuedSingleLineComment = false;
2041 continuedString = false;
2042 continuedQuotes = false;
2046 if(y > box.bottom) // >=clientSize.h)
2052 surface.TextOpacity(true);
2053 surface.SetForeground(black);
2054 surface.SetBackground(Color {255,255,85});
2055 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2059 void FixScrollArea()
2061 if(style.hScroll || style.vScroll)
2063 int width = maxLength + XOFFSET;
2064 int height = lineCount * space.h;
2065 if(style.freeCaret && line)
2070 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2074 if(selX > selLine.count)
2075 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2080 SetScrollLineStep(8, space.h);
2081 SetScrollArea(width, height, true);
2085 void ComputeLength(EditLine line)
2092 for(c = 0; c < line.count; )
2101 for(len = 0; c < line.count; c += numBytes)
2103 ch = line.buffer[c];
2104 numBytes = UTF8_NUM_BYTES(ch);
2106 if(ch == ' ' || ch == '\t')
2113 if(!len && ch == ' ')
2118 else if(!len && ch == '\t')
2120 w = (tabSize * space.w) - (x % (tabSize * space.w));
2124 FontExtent(display, font, line.buffer + start, len, &w, null);
2135 if(line.length > this.maxLength)
2137 this.maxLine = line;
2138 this.maxLength = line.length;
2140 if(style.autoSize) AutoSize();
2149 this.maxLine = null;
2151 for(line = lines.first; line; line = line.next)
2153 if(line.length > this.maxLength)
2155 this.maxLength = line.length;
2156 this.maxLine = line;
2160 if(style.autoSize) AutoSize();
2165 if(this.selY != this.y)
2167 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2171 void ComputeColumn()
2173 // Adjust new edit X position according to tabs
2174 int c, position = 0;
2177 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2179 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2181 position += this.tabSize - (position % this.tabSize);
2185 position += this.x - c;
2186 this.col = position;
2189 void DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2191 _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2194 bool HasCommentOrEscape(EditLine line)
2196 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2201 for(c = line.count-1; c >= 0; c--)
2203 char ch = line.buffer[c];
2209 else //if(ch != ' ' && ch != '\t')
2216 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2218 EditLine line = l1, next;
2220 int oldCount1, oldCount2;
2221 int y, firstViewY, firstY, firstDropY, firstSelY;
2224 DelTextAction action = null;
2225 bool hadComment = false;
2229 hadComment = HasCommentOrEscape(line);
2231 if(y2 > y1 || c2 > c1)
2233 if(start < l1.count)
2235 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2239 oldCount1 = l1.count;
2241 while(c1 < oldCount1)
2243 byte ch = buffer[c1];
2244 if(UTF8_IS_FIRST(ch)) break;
2248 oldCount2 = l2.count;
2250 while(c2 < oldCount2)
2252 byte ch = buffer[c2];
2253 if(UTF8_IS_FIRST(ch)) break;
2258 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2263 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2264 string = new char[len];
2265 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2266 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2270 //oldCount1 = l1.count;
2271 //oldCount2 = l2.count;
2274 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2275 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2278 if(c2 > oldCount2) c2 = oldCount2;
2279 if(!(style.freeCaret))
2280 if(c1 > oldCount1) c1 = oldCount1;
2281 newLineCount = c1 + l2.count-c2;
2287 buffer = new char[line.size ? line.size : 1];
2289 buffer = new char[line.size];
2290 // TODO: Better handling of these allocation failures
2291 if(!buffer) return extras;
2292 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2297 // TODO: Better handling of these allocation failures
2298 if(!line.AdjustBuffer(newLineCount))
2302 /*if(newLineCount > 4000 || newLineCount < 0)
2303 printf("Warning");*/
2305 line.count = newLineCount;
2307 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2312 action.addedSpaces = c1-oldCount1;
2314 if(action.addedSpaces > action.x1)
2320 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2321 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2323 line.buffer[line.count] = '\0';
2330 this.dropX -= c2-c1;
2331 this.dropX = Max(this.dropX,0);
2333 this.x = Max(this.x,0);
2340 firstViewY = this.viewY;
2342 firstDropY = this.dropY;
2343 firstSelY = this.selY;
2344 for(line = l1;line;line = next, y++)
2352 if(line == this.viewLine)
2354 if(this.viewLine.next)
2356 this.viewLine = this.viewLine.next;
2358 style.recomputeSyntax = true;
2362 this.viewLine = this.viewLine.prev;
2364 style.recomputeSyntax = true;
2367 else if(y < firstViewY)
2369 if(line == this.line)
2373 this.line = this.line.next;
2374 this.x = this.line.count;
2379 this.line = this.line.prev;
2380 this.x = this.line.count;
2387 if(line == this.dropLine)
2389 if(this.dropLine.next)
2391 this.dropLine = this.dropLine.next;
2392 this.dropX = this.dropLine.count;
2396 this.dropLine = this.dropLine.prev;
2397 this.dropX = this.dropLine.count;
2401 else if(y < firstDropY)
2403 if(line == this.selLine)
2405 if(this.selLine.next)
2407 this.selLine = this.selLine.next;
2408 this.selX = this.selLine.count;
2412 this.selLine = this.selLine.prev;
2413 this.selX = this.selLine.count;
2417 else if(y < firstSelY)
2421 if(line == l2) break;
2425 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2428 style.recomputeSyntax = true;
2433 bool DelSel(int * addedSpacesPtr)
2435 if(this.line != this.selLine || this.x != this.selX)
2437 if(this.selY < this.y)
2439 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2442 this.line = this.selLine;
2444 else if(this.selY > this.y)
2446 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2449 this.selLine = this.line;
2451 else if(this.selX < this.x)
2453 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2456 this.line = this.selLine;
2460 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2463 this.selLine = this.line;
2471 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2473 bool hadComment = false;
2474 // Add the line here
2475 EditLine line = this.line;
2477 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2478 if(!style.hScroll && created)
2480 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2485 // Lock if no place to display.
2488 else if(endX > this.x)
2489 max = Max(this.x, line.count);
2493 for(x = 0, c = 0; c < max+count; )
2498 if(c < Min(this.x, line.count))
2499 string = line.buffer + c;
2502 else if(c < endX + count)
2503 string = stringLine + c - endX;
2505 string = line.buffer + c - endX - count;
2509 w = (tabSize * space.w) - (x % (tabSize * space.w));
2513 numBytes = UTF8_NUM_BYTES(*string);
2514 FontExtent(display, this.font, string, numBytes, &w, null);
2518 if(x >= clientSize.w)
2523 c += numBytes; // - 1;
2529 int addedSpaces = 0;
2532 // Add blank spaces if EES_FREECARET
2533 if(this.x > line.count)
2543 for(c = 0; c<line.count; c++)
2545 if(this.line.buffer[c] == '\t')
2546 position += this.tabSize - (position % this.tabSize);
2550 wantedPosition = position + (this.x - line.count);
2552 // A tab is too much...
2553 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2554 addedSpaces = wantedPosition - position;
2559 position += this.tabSize - (position % this.tabSize);
2560 // Add as many tabs as needed
2561 addedTabs += (wantedPosition - position) / this.tabSize;
2562 position += (addedTabs-1) * this.tabSize;
2563 // Finish off with spaces
2564 addedSpaces = wantedPosition - position;
2568 addedSpaces = this.x - line.count;
2571 this.x = Min(this.x, line.count);
2573 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2579 hadComment = HasCommentOrEscape(line);
2581 // Adjust the size of the line
2582 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2586 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2589 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2590 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2593 *addedTabsPtr = addedTabs;
2594 FillBytes(line.buffer+line.count,'\t',addedTabs);
2596 if(addedTabs > 4000 || addedTabs < 0)
2599 line.count += addedTabs;
2605 FillBytes(line.buffer+line.count,' ',addedSpaces);
2607 if(addedSpaces > 4000 || addedSpaces < 0)
2610 line.count += addedSpaces;
2611 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2613 else if(addedSpacesPtr)
2614 *addedSpacesPtr = 0;
2616 if(count > 4000 || count < 0)
2619 line.count += count;
2620 this.x += count + addedTabs + addedSpaces;
2623 this.selLine = this.line;
2625 line.buffer[line.count] = '\0';
2627 ComputeLength(line);
2632 hasComment = HasCommentOrEscape(line);
2633 if(!undoBuffer.insideRedo)
2635 int backDontRecord = undoBuffer.dontRecord;
2636 undoBuffer.dontRecord = 0;
2637 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2638 undoBuffer.dontRecord = backDontRecord;
2640 if(style.syntax && (hadComment || hasComment || line != this.line))
2642 style.recomputeSyntax = true;
2650 void Emptyline(EditLine line, int y)
2653 DelCh(line, y, 0, line.next, y+1, 0, true);
2655 DelCh(line, y, 0, line, y, line.count, true);
2658 void GoToEnd(bool deselect)
2662 this.line = this.lines.last;
2663 if(this.y != this.lineCount-1)
2665 else if (this.x != this.line.count)
2666 DirtyLine(this.lineCount-1);
2667 this.y = this.lineCount-1;
2668 this.x = this.line.count;
2675 void GoToHome(bool deselect)
2679 this.line = this.lines.first;
2682 else if (this.x !=0)
2683 DirtyLine(this.lineCount-1);
2692 // Returns true if it needs scrolling
2693 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2699 bool needHScroll = false;
2706 line = this.viewLine ? (void *)this.viewLine.prev : null;
2711 line = (void *)this.lines.first;
2716 py = Min(py, clientSize.h);
2718 py = Min(py, this.lineCount);
2720 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2726 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2729 px = Min(px,clientSize.w+this.space.w);
2733 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2736 if(tline) *tline = line;
2739 // Prevent divide by 0 from non valid this.font
2741 return (y < this.viewY) || needHScroll;
2743 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2747 // Minimal Update Management Functions
2751 this.endY = clientSize.h-1;
2752 // ErrorLog("DirtyAll\n");
2755 void DirtyEnd(int y)
2757 if((y - this.viewY)*this.space.h < this.startY)
2758 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2759 this.endY = clientSize.h-1;
2760 //ErrorLog("DirtyEnd %d\n", y);
2763 void DirtyLine(int y)
2767 if((y - this.viewY)*this.space.h < this.startY)
2768 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2769 if((y - this.viewY+1)*this.space.h > this.endY)
2770 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2772 //ErrorLog("DirtyLine %d\n", y);
2779 if(style.recomputeSyntax)
2781 FigureStartSyntaxStates(lines.first, true);
2782 style.recomputeSyntax = false;
2785 if(this.startY > this.endY) return;
2786 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2792 box.right = clientSize.w-1;
2793 box.top = this.startY;
2794 box.bottom = this.endY;
2797 this.startY = clientSize.h;
2801 bool IsMouseOnSelection()
2803 bool mouseOnSelection = false;
2806 int minY = Min(this.selY, this.y);
2807 int maxY = Max(this.selY, this.y);
2808 int minX = Min(this.selX, this.x);
2809 int maxX = Max(this.selX, this.x);
2811 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2813 if(maxX != minX || maxY != minY)
2815 if(y > minY && y < maxY)
2816 mouseOnSelection = true;
2817 else if(y == minY && y == maxY)
2818 mouseOnSelection = (x < maxX && x >= minX);
2822 mouseOnSelection = (x >= this.selX);
2823 else if(y == this.y)
2824 mouseOnSelection = (x >= this.x);
2829 mouseOnSelection = (x < this.selX);
2830 else if(y == this.y)
2831 mouseOnSelection = (x < this.x);
2834 return mouseOnSelection;
2837 void UpdateCaretPosition(bool setCaret)
2841 if(mouseMove || !style.noCaret)
2843 int max = this.mouseMove ? this.dropX : this.x;
2844 int y = this.mouseMove ? this.dropY : this.y;
2845 EditLine line = this.mouseMove ? this.dropLine : this.line;
2847 if(!(style.freeCaret))
2848 max = Min(max, line.count);
2850 if(FontExtent && display)
2852 for(c = 0; c < max; )
2861 for(len = 0; c < Min(max, line.count); c += numBytes)
2863 ch = line.buffer[c];
2864 numBytes = UTF8_NUM_BYTES(ch);
2866 if(ch == ' ' || ch == '\t')
2873 if(!len && ch == ' ')
2878 else if(!len && ch == '\t')
2880 w = (tabSize * space.w) - (x % (tabSize * space.w));
2884 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2896 caretY = y * this.space.h;
2898 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2905 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2906 NotifyCaretMove(master, this, y + 1, x + 1);
2912 void SelectionEnables()
2914 if((x != selX || y != selY) && !selection)
2918 itemEditCut.disabled = false;
2919 itemEditDelete.disabled = false;
2921 itemEditCopy.disabled = false;
2923 this.selection = true;
2925 else if((x == selX && y == selY) && selection)
2927 itemEditCut.disabled = true;
2928 itemEditCopy.disabled = true;
2929 itemEditDelete.disabled = true;
2931 this.selection = false;
2935 void SetSelectCursor()
2937 if(!inactive || !style.noSelect)
2940 cursor = guiApp.GetCursor(arrow);
2941 else if(this.mouseSelect)
2942 cursor = guiApp.GetCursor(iBeam);
2945 if(IsMouseOnSelection())
2946 cursor = guiApp.GetCursor(arrow);
2948 cursor = guiApp.GetCursor(iBeam);
2953 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2956 int x = px ? *px : 0;
2963 if(c < Min(max, line.count))
2967 for(len = 0; c < Min(max, line.count); c += numBytes)
2969 ch = line.buffer[c];
2970 numBytes = UTF8_NUM_BYTES(ch);
2972 if(ch == ' ' || ch == '\t')
2979 if(!len && ch == ' ')
2984 else if(!len && ch == '\t')
2986 w = (tabSize * space.w) - (x % (tabSize * space.w));
2990 FontExtent(display, font, line.buffer + start, len, &w, null);
2994 if(style.freeCaret && c < max)
3003 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
3008 int a = start + len;
3011 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
3015 FontExtent(display, font, line.buffer + start, a - start, &w, null);
3018 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
3022 return Min(this.maxLineSize - 1, start + len);
3028 void SetCursorToViewX()
3030 bool selecting = this.x != selX || y != selY;
3032 // Horizontal Adjustment
3034 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3039 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3051 UpdateCaretPosition(false);
3057 void SetCursorToViewY()
3060 EditLine oldLine = this.line;
3062 bool selecting = this.x != this.selX || this.y != this.selY;
3064 numLines = clientSize.h / this.space.h;
3066 // Vertical Adjustment
3067 if(this.viewY > this.y)
3069 this.y = this.viewY;
3070 this.line = this.viewLine;
3073 if(this.viewY + numLines <= this.y)
3077 this.y = this.viewY-1;
3078 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3085 if(this.line != oldLine)
3087 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3095 this.selLine = this.line;
3098 UpdateCaretPosition(false);
3105 bool SaveFile(char * fileName)
3107 File f = eFile_Open(fileName, FO_WRITE);
3111 eWindow_SetModified(false);
3112 eInstance_Delete(f);
3119 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3123 if(!style.multiLine)
3125 x = (active && this.active && !style.readOnly) ? line.count : 0;
3128 SetViewToCursor(true);
3133 if(!active && modified)
3136 if(!NotifyModified(master, this))
3139 *goOnWithActivation = false;
3146 if(timer) timer.Stop();
3148 mouseSelect = false;
3156 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3157 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3158 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3161 bool OnResizing(int *w, int *h)
3167 *w = space.h * 80 / 14;
3171 void OnResize(int w, int h)
3174 if(!hasHorzScroll && !hasVertScroll && viewLine)
3175 SetViewToCursor(true);
3177 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3179 SetViewToCursor(true);
3184 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3186 if(style.readOnly) return true;
3187 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3188 // Middle button already serves as a dragger as well.
3193 bool OnRightButtonDown(int x, int y, Modifiers mods)
3195 this.rightButtonDown = true;
3200 bool OnRightButtonUp(int x, int y, Modifiers mods)
3203 if(!parent.inactive && rightButtonDown)
3206 Menu contextMenu { };
3208 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3209 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3210 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3211 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3212 MenuDivider { contextMenu };
3213 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3215 popup = PopupMenu { master = this, menu = contextMenu,
3217 nonClient = true, interim = false, parent = parent,
3218 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3220 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3224 rightButtonDown = false;
3228 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3233 if(style.noSelect) return true;
3235 // Should we have a separate 'selectOnActivate' style?
3236 if(!mods.isActivate || (style.readOnly && style.multiLine))
3242 mouseX = mx - XOFFSET;
3245 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3247 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3253 else if(IsMouseOnSelection() && !mods.isActivate)
3263 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3265 if(mods.shift && !mods.isActivate)
3286 UpdateCaretPosition(true);
3287 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3288 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3292 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3296 mouseSelect = false;
3307 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3309 //PrintLn("MouseMove: ", x, ", ", y);
3315 mouseMove = IsMouseOnSelection();
3319 int size = SelSize();
3322 char * text = new char[size+1];
3326 GetSel(text, false);
3328 if(Max(selY, this.y) == dropY)
3332 if(this.dropX > this.selX)
3333 moveX = this.x - this.selX;
3337 if(this.dropX > this.x)
3338 moveX = this.selX - this.x;
3342 recordUndoEvent = true;
3344 this.dropX -= moveX;
3345 this.selX = this.x = this.dropX;
3346 this.selY = this.y = this.dropY;
3347 this.selLine = this.line = this.dropLine;
3349 recordUndoEvent = false;
3351 SetViewToCursor(true);
3356 byte * c = ((EditBox)this).multiLineContents, * c2;
3357 int l1 = c ? strlen(c) : 0, l2;
3360 c2 = ((EditBox)this).multiLineContents;
3361 l2 = c2 ? strlen(c2) : 0;
3362 if(l1 != l2 || (l1 && strcmp(c, c2)))
3392 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3394 //PrintLn("Dropped: ", x, ", ", y);
3396 NotifyDropped(master, this, x, y);
3400 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3401 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3405 bool OnMouseMove(int mx, int my, Modifiers mods)
3411 if(mods != -1 && mods.isSideEffect)
3416 if(style.noSelect) return true;
3417 if(wordSelect) return true;
3418 mouseX = mx - XOFFSET;
3421 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3423 if(this.mouseMove || this.mouseSelect)
3432 ((style.hScroll) || (style.vScroll)))
3439 DirtyLine(this.dropY);
3442 DirtyLine(this.dropY);
3443 this.dropLine = line;
3444 SetViewToCursor(true);
3446 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3449 else if(this.mouseSelect)
3451 DirtyLine(this.selY);
3458 SetViewToCursor(true);
3461 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3468 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3473 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3477 if(style.noSelect) return true;
3478 FindMouse(mx, my, &x, &y, &line, false);
3479 if(!NotifyDoubleClick(master, this, line, mods))
3486 for(c = x; c >= 0; c--)
3489 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3490 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3497 for(c = start; c<line.count; c += numBytes)
3499 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3500 if(!ch || !IS_ALUNDER(ch))
3510 this.line = this.selLine = line;
3511 this.wordSelect = (c != start);
3522 Menu fileMenu { menu, "File", F };
3523 saveDialog = fileDialog;
3524 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3525 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3527 if(style.autoSize) AutoSize();
3535 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3536 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3538 space.w = Max(space.w, 1);
3539 large.w = Max(large.w, 1);
3540 space.h = Max(space.h, 1);
3541 large.h = Max(large.h, 1);
3545 for(line = lines.first; line; line = line.next)
3546 ComputeLength(line);
3551 SetViewToCursor(true);
3559 bool OnLoadGraphics()
3561 FontExtent = Display::FontExtent;
3564 // UpdateCaretPosition(true);
3568 void OnUnloadGraphics()
3573 bool OnKeyHit(Key key, unichar ch)
3575 bool shift = (key.shift) ? true : false;
3577 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3579 if(!ch && !key.alt && !key.ctrl)
3581 key.code = (SmartKey)key.code;
3583 else if(!ch && key.alt)
3586 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3589 if(style.readOnly) break;
3590 if(style.stuckCaret) GoToEnd(true);
3591 if(!(style.freeCaret))
3593 this.x = Min(this.x, this.line.count);
3599 EditLine line = this.line;
3601 for(y = this.y; y>= 0; y--)
3603 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3605 // Slow down when going on lines...
3606 if(y != this.y) break;
3610 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3611 if(IS_ALUNDER(line.buffer[c]))
3616 if(!IS_ALUNDER(line.buffer[c]))
3618 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3633 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3634 this.x = this.selX = Min(c+1, line.count);
3635 this.y = this.selY = y;
3636 this.line = this.selLine = line;
3637 SetViewToCursor(true);
3648 if(style.readOnly) break;
3649 if(style.stuckCaret) break;
3658 if(this.line != this.selLine || this.x != this.selX)
3661 SetViewToCursor(true);
3666 EditLine line1 = this.line, line2 = this.line;
3667 int x1, y1 = this.y, x2, y2 = this.y;
3668 if(!style.freeCaret)
3670 this.selX = this.x = Min(this.x, this.line.count);
3675 if(x1 < line1.count)
3682 char * buffer = line1.buffer;
3683 for(i = x1; i < line1.count; i++)
3685 if(!IS_ALUNDER(buffer[i]))
3689 for(; i < line1.count; i++)
3691 // Delete trailing whitespace
3692 if(IS_ALUNDER(buffer[i]))
3702 // Avoid creating trailing spaces if there is no content on next line
3705 if(line2 && !line2.count)
3710 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3711 SetViewToCursor(true);
3720 if(!key.alt && !key.ctrl)
3724 bool stuffAfter = false;
3727 /*bool resetX = false;
3730 if(style.stuckCaret) GoToEnd(true);
3731 if(style.readOnly) break;
3732 if(!(style.multiLine)) break;
3734 for(c = 0; c<this.line.count && c<this.x; c++)
3736 if(this.line.buffer[c] == '\t')
3737 position += this.tabSize - (position % this.tabSize);
3738 else if(this.line.buffer[c] == ' ')
3744 // Prevent adding trailing spaces if at the head of a line
3745 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3757 if(this.x < this.line.count)
3760 //If last character is a { indent one tab
3761 if(this.line.buffer[this.x - 1] == '{')
3763 //Except if the next non space character is a }
3764 bool indent = false;
3766 for(i = this.x; i < this.line.size; i++)
3767 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3769 if(this.line.buffer[i] != '}')
3774 position += this.tabSize;
3777 addString = new char[position + 2];
3778 addString[len++] = '\n';
3779 addString[len] = '\0';
3781 if(stuffAfter || !style.freeCaret)
3783 for(c = 0; c<position; )
3785 if(style.useTab && c + this.tabSize <= position)
3787 addString[len++] = '\t';
3792 addString[len++] = ' ';
3796 addString[len] = '\0';
3798 recordUndoEvent = true;
3801 EditLine prevLine = this.line.prev;
3804 // Nuke spaces if that is all that is left on previous line
3806 char * buffer = prevLine.buffer;
3807 for(i = 0; i < prevLine.count; i++)
3808 if(buffer[i] != ' ' && buffer[i] != '\t')
3810 if(i == prevLine.count)
3811 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3815 this.x = this.selX = backX;
3818 else */if(!stuffAfter && style.freeCaret)
3820 this.x = this.selX = position;
3824 SetViewToCursor(true);
3827 recordUndoEvent = false;
3835 if(style.stuckCaret) break;
3836 if(!(style.freeCaret))
3838 this.x = Min(this.x, this.line.count);
3839 this.selX = Min(this.selX, this.selLine.count);
3842 if(!shift) SelDirty();
3845 bool foundAlpha = false;
3848 EditLine line, lastLine;
3851 for(line = this.line; (line && !found); line = line.prev, y--)
3856 if(this.x == 0 && line != this.line)
3865 if(line == this.line) start = this.x -1; else start = line.count-1;
3866 start = Min(start, line.count-1);
3868 for(c = start; c >= 0;)
3871 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3889 byte ch = line.buffer[c];
3890 if(UTF8_IS_FIRST(ch)) break;
3894 // No next word found,
3895 if(!found && ( this.x > 0 || (!line.count && this.x)))
3909 this.line = lastLine;
3920 byte * buffer = line.buffer;
3923 byte ch = buffer[x];
3924 if(UTF8_IS_FIRST(ch)) break;
3941 if(!shift) _Deselect();
3942 SetViewToCursor(true);
3948 if(style.stuckCaret) break;
3949 if(!(style.freeCaret))
3951 this.x = Min(this.x, this.line.count);
3952 this.selX = Min(this.selX, this.selLine.count);
3955 if(!shift) SelDirty();
3956 if(!shift && (this.x != this.selX || this.y != this.selY));
3959 bool onAChar = false;
3960 if(this.selX != this.x || this.selY != this.y)
3962 if(this.x<this.line.count)
3963 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3965 if(key.shift && onAChar &&
3966 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3968 bool foundAlpha = false;
3970 EditLine line, lastLine;
3972 int lastC, lastY, lastNumBytes;
3974 for(line = this.line; (line && !found); line = line.next, y++)
3976 int start = (line == this.line) ? this.x : 0;
3980 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3986 lastNumBytes = numBytes;
3996 if(!found && (c != this.x || line != this.line))
4009 this.x = lastC + lastNumBytes;
4011 this.line = lastLine;
4018 bool foundAlpha = false;
4023 for(line = this.line; (line && !found); line = line.next, y++)
4025 int start = (line == this.line) ? this.x : 0;
4029 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4045 // No next word found,
4046 if(!found && (c != this.x || line != this.line))
4050 this.x = line.count;
4062 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4066 byte * buffer = line.buffer;
4069 byte ch = buffer[x];
4070 if(UTF8_IS_FIRST(ch)) break;
4091 if(!shift) _Deselect();
4092 SetViewToCursor(true);
4099 if(!style.vScroll || hasVertScroll) break;
4105 if(style.stuckCaret) break;
4107 if(!shift) SelDirty();
4117 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4121 if(!shift) _Deselect();
4123 SetViewToCursor(false);
4126 if(caretY == this.y * space.h)
4132 if(!style.freeCaret)
4133 this.x = Min(this.x, line.count);
4143 int sx = 0, sy = this.y * space.h;
4144 char * text = line.text;
4145 int maxW = clientSize.w - sx;
4146 display.FontExtent(font, " ", 1, null, &th);
4150 int startPos = textPos;
4153 bool lineComplete = false;
4155 if(!style.wrap && caretY == MAXINT)
4158 //textPos = line.count;
4159 //lineComplete = true;
4162 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4166 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4169 len = (nextSpace - (text + textPos));
4171 len = line.count - textPos;
4173 if(textPos < line.count)
4175 display.FontExtent(font, text + textPos, len, &w, null);
4177 if(nextSpace) { w += space.w; len++; }
4179 if(style.wrap && x + width + w > maxW && x > 0)
4181 lineComplete = true;
4191 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4195 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4198 if(this.x > line.count)
4201 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4202 len = this.x - startPos;
4203 display.FontExtent(font, text + startPos, len, &x, null);
4207 if(!shift) _Deselect();
4209 SetViewToCursor(false);
4213 if(sy == caretY - th || textPos >= line.count)
4215 if(textPos >= line.count)
4217 int c = textPos - 1;
4218 while(c > 0 && text[c] == ' ') c--;
4222 this.x = line.count;
4225 if(!shift) _Deselect();
4227 SetViewToCursor(false);
4232 } while(textPos < line.count);
4235 if(!shift) _Deselect();
4237 SetViewToCursor(false);
4246 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4247 if(!shift) SelDirty();
4248 this.line = this.line.prev;
4254 if(!shift) _Deselect();
4258 SetViewToCursor(false);
4264 return style.multiLine ? false : true;
4268 if(!style.vScroll || hasVertScroll)
4275 if(style.stuckCaret) break;
4279 int sx = 0, sy = this.y * this.space.h;
4280 int maxW = clientSize.w - sx;
4281 char * text = line.buffer;
4283 if(!shift) SelDirty();
4289 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4299 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4303 if(!shift) _Deselect();
4305 SetViewToCursor(false);
4308 while(!textPos || (style.freeCaret || textPos<line.count))
4310 int startPos = textPos;
4313 bool lineComplete = false;
4314 if(!style.wrap && sy <= caretY)
4316 textPos = line.count;
4317 lineComplete = true;
4319 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4323 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4326 len = (nextSpace - (text + textPos));
4328 len = line.count - textPos;
4330 if(textPos < line.count)
4332 display.FontExtent(font, text + textPos, len, &w, &th);
4334 if(nextSpace) { w += space.w; len++; }
4335 if(style.wrap && x + width + w > maxW && x > 0)
4337 lineComplete = true;
4347 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4351 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4354 if(this.x > line.count)
4357 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4359 len = this.x - startPos;
4360 display.FontExtent(font, text + startPos, len, &x, null);
4363 if(!shift) _Deselect();
4366 SetViewToCursor(false);
4372 this.x = line.count;
4375 if(!shift) _Deselect();
4378 SetViewToCursor(false);
4381 else if(textPos >= line.count && line.next)
4387 sy = this.y * this.space.h;
4388 sx = 0; //textBlock.startX;
4394 sx = 0; //textBlock.startX;
4402 this.x = Min(this.x, line.count);
4403 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4405 if(!shift) _Deselect();
4408 if(this.selX != this.x || this.selY != this.y)
4411 SetViewToCursor(false);
4418 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4419 if(!shift) SelDirty();
4420 this.line = this.line.next;
4424 if(!shift) _Deselect();
4427 if(this.selX != this.x || this.selY != this.y)
4435 return style.multiLine ? false : true;
4438 if(style.stuckCaret) break;
4439 if(!style.multiLine && key.ctrl) break;
4440 if(!(style.freeCaret))
4441 this.selX = Min(this.selX, this.selLine.count);
4443 if(!shift) SelDirty();
4446 this.line = this.lines.first;
4447 if(this.y != 0 || this.x != 0)
4457 EditLine line = this.line;
4459 for(c=0; line.buffer[c]; c++)
4460 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4462 if(overwrite || (shift && (c != 0 || this.x)))
4471 if(overwrite || (shift && this.x != 0))
4477 if(!shift) _Deselect();
4478 SetViewToCursor(true);
4484 if(style.stuckCaret) break;
4485 if(!style.multiLine && key.ctrl) break;
4486 if(!style.freeCaret)
4487 this.selX = Min(this.selX, this.selLine.count);
4489 if(!shift) SelDirty();
4494 else if(this.x != this.line.count)
4496 this.x = this.line.count;
4497 if(overwrite || shift)
4501 if(!shift) _Deselect();
4502 SetViewToCursor(true);
4507 if(style.tabKey && !key.ctrl)
4509 if(this.selY != this.y && style.tabSel)
4511 EditLine firstLine, lastLine;
4515 // Do multi line selection tabbing here
4516 if(this.selY < this.y)
4518 firstLine = this.selLine;
4519 lastLine = this.line;
4525 // Selecting going up
4526 firstLine = this.line;
4527 lastLine = this.selLine;
4534 for(line = firstLine; line; line = line.next, y++)
4536 if(line != lastLine || x)
4540 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4542 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4544 if(line.buffer[c] == '\t')
4549 else if(line.buffer[c] != ' ')
4554 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4557 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4558 char * string = new char[len];
4559 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4560 GetText(string, line, y, 0, line, y, lastC, false, false);
4563 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4564 if(!line.AdjustBuffer(line.count-lastC))
4570 if(line == lastLine) break;
4575 for(line = firstLine; line; line = line.next, y++)
4579 if(line != lastLine || x)
4583 if(line.count + 1 <= this.maxLineSize)
4585 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4587 if(!line.AdjustBuffer(line.count+1))
4590 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4592 AddCharAction action { ch = '\t', x = 0, y = y };
4596 memmove(line.buffer+1,line.buffer,line.size-1);
4598 line.buffer[0] = '\t';
4603 if(line.count + this.tabSize <= this.maxLineSize)
4606 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4607 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4609 if(!line.AdjustBuffer(line.count+this.tabSize))
4613 char * string = new char[this.tabSize + 1];
4614 memset(string, ' ', this.tabSize);
4615 string[this.tabSize] = '\0';
4616 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4619 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4620 line.count+=this.tabSize;
4621 for(c=0; c<this.tabSize; c++)
4622 line.buffer[c] = ' ';
4628 if(line == lastLine) break;
4631 ComputeLength(maxLine);
4643 char * addString = new char[this.tabSize + 1];
4645 if(!(style.freeCaret))
4647 this.x = Min(this.x, this.line.count);
4651 start = Min(this.x, this.selX);
4652 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4654 addString[len++] = ' ';
4662 SetViewToCursor(true);
4671 if(!(style.hScroll) || hasHorzScroll) break;
4672 if(this.viewX < this.maxLength)
4674 //this.viewX+=this.space.w*this.tabSize;
4676 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4683 if(!shift) _Deselect();
4695 if(!(style.hScroll) || hasHorzScroll) break;
4698 //this.viewX-=this.space.w*this.tabSize;
4699 //this.viewX = Max(this.viewX,0);
4701 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4702 // SetCursorToView();
4709 if(!shift) _Deselect();
4722 else if(!style.readOnly)
4726 if(!(style.readOnly))
4732 this.overwrite ^= 1;
4733 UpdateCaretPosition(true);
4738 NotifyOvrToggle(master, this, this.overwrite);
4750 if(style.noSelect) break;
4753 //Save current view position
4754 int tempX = this.viewX;
4755 int tempY = this.viewY;
4759 this.selLine = this.lines.first;
4760 this.y = this.lineCount-1;
4761 this.line = this.lines.last;
4762 this.x = this.line.count;
4765 SetViewToCursor(true);
4767 //Restore previous view position
4768 SetScrollPosition(tempX, tempY * this.space.h);
4774 // TOCHECK: Was there any good reason why we weren't returning false here?
4775 return false; // break;
4780 if(!(style.readOnly))
4788 if(style.readOnly) break;
4794 if(style.readOnly) break;
4799 if(style.readOnly) break;
4803 if(style.readOnly) break;
4807 if(style.readOnly) break;
4808 if(key.shift && key.code == rightBracket)
4810 //Only indent back if you are exactly at one tab.
4812 bool whitespace = true;
4818 EditLine line = this.line;
4820 //Only remove one tab if there is nothing else on the line.
4821 for(i = 0; i < this.line.count; i++)
4823 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4837 for(pos = line.count - 1; pos >= 0; pos--)
4839 char c = line.buffer[pos];
4843 indentwidth += this.tabSize;
4845 //Counting backwards, so when you find a character, indentation is reset
4855 //Place the } to get an undo:
4858 this.x = this.line.count;
4862 putsize = indentwidth;
4864 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4866 newline = new char[putsize+2];
4867 newline[putsize] = '}';
4868 newline[putsize+1] = '\0';
4872 for(; i < indentwidth / this.tabSize; i++)
4874 for(;i < putsize; i++)
4883 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4895 void OnHScroll(ScrollBarAction action, int position, Key key)
4898 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4900 this.viewX = position;
4901 if(action != setRange)
4903 if(!this.mouseMove && style.cursorFollowsView)
4910 void OnVScroll(ScrollBarAction action, int position, Key key)
4912 int oldViewY = this.viewY;
4915 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4917 position /= this.space.h;
4919 if(position < this.viewY)
4921 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4922 style.recomputeSyntax = true;
4924 else if(position > this.viewY)
4926 EditLine oldViewLine = viewLine;
4927 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4928 FigureStartSyntaxStates(oldViewLine, false);
4931 if(action != setRange)
4933 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4936 if(this.x != this.selX || this.y != this.selY)
4940 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4943 int numLines = clientSize.h / this.space.h;
4945 if(Abs(this.viewY - oldViewY) < numLines)
4948 if(this.viewY > oldViewY)
4950 for(y = oldViewY; y <this.viewY; y++)
4951 DirtyLine(y + numLines);
4955 for(y = this.viewY; y <oldViewY; y++)
4956 DirtyLine(y + numLines);
4961 // Fix dirt of stuff before first line...
4962 if(this.viewY - oldViewY > 0)
4964 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4971 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4976 ReplaceTextAction replaceAction = null;
4977 AddCharAction addCharAction = null;
4978 int addedSpaces = 0, addedTabs = 0;
4980 if(ch == '\r') return true;
4981 if(style.stuckCaret /*|EES_READONLY)*/ )
4984 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4986 if(!undoBuffer.dontRecord)
4988 if(selX != x || selY != y)
4993 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4994 oldString = new char[len];
4995 UTF32toUTF8Len(&ch, 1, buffer, 4);
4996 newString = CopyString(buffer);
4997 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4999 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5000 if(selY < y || (selY == y && selX < x))
5002 replaceAction.x1 = selX;
5003 replaceAction.y1 = selY;
5004 replaceAction.x2 = x;
5005 replaceAction.y2 = y;
5009 replaceAction.x1 = x;
5010 replaceAction.y1 = y;
5011 replaceAction.x2 = selX;
5012 replaceAction.y2 = selY;
5014 Record(replaceAction);
5015 undoBuffer.dontRecord++;
5019 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5020 Record(addCharAction);
5026 DelSel(&addedSpaces);
5027 if(this.lineCount+1 > this.maxLines)
5030 Emptyline(this.lines.first,0);
5034 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5036 // Make sure it fits, but we need a default line is this.font is too big for window
5037 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5040 if((this.y >= 0) && this.y < this.viewY)
5045 line = EditLine { };
5048 lines.Insert(this.line, line);
5049 line.editBox = this;
5055 // If we're displacing the lines from a current line ...
5058 if(this.line.buffer)
5061 if(this.line.count < endX) endX = this.line.count;
5062 length = this.line.count - endX;
5065 if(!line.AdjustBuffer(length))
5069 if(this.line.buffer)
5071 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5073 if(endX > 4000 || endX < 0)
5076 this.line.count = endX;
5077 this.line.buffer[this.line.count] = '\0';
5078 this.line.AdjustBuffer(this.line.count);
5079 ComputeLength(this.line);
5083 BufferLocation before = { this.line, this.y, this.x }, after;
5088 line.count = length;
5091 if(length > 4000 || length < 0)
5094 ComputeLength(this.line);
5099 line.buffer[line.count] = '\0';
5102 after.line = this.line, after.y = this.y, after.x = this.x;
5104 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5110 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5111 DelSel(&addedSpaces);
5112 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5113 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5114 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5118 this.selLine = this.line;
5122 replaceAction.x3 = x;
5123 replaceAction.y3 = y;
5124 replaceAction.addedSpaces = addedSpaces;
5125 replaceAction.addedTabs = addedTabs;
5126 undoBuffer.dontRecord--;
5130 addCharAction.x -= addedTabs * (tabSize-1);
5131 addCharAction.addedSpaces = addedSpaces;
5132 addCharAction.addedTabs = addedTabs;
5139 /****************************************************************************
5141 ****************************************************************************/
5144 bool AddCh(unichar ch)
5146 return _AddCh(ch, null, null);
5151 this.modified = true;
5152 NotifyUpdate(master, this);
5153 modifiedDocument = true;
5156 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5159 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5160 SetViewToCursor(true);
5168 itemEditUndo.disabled = undoBuffer.curAction == 0;
5169 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5170 if(savedAction == undoBuffer.curAction)
5172 modifiedDocument = false;
5174 NotifyUnsetModified(master, this);
5181 itemEditUndo.disabled = undoBuffer.curAction == 0;
5182 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5183 if(savedAction == undoBuffer.curAction)
5185 modifiedDocument = false;
5187 NotifyUnsetModified(master, this);
5191 void Record(UndoAction action)
5193 if(!undoBuffer.dontRecord)
5195 undoBuffer.Record(action);
5196 itemEditUndo.disabled = undoBuffer.curAction == 0;
5197 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5204 this.lines.first, 0,0,
5205 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5208 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5213 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5214 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5215 this.y = line2 ? y2 : (this.lineCount-1);
5216 this.line = line2 ? (EditLine)line2 : this.lines.last;
5219 SetViewToCursor(true);
5223 // TODO: Fix this vs modifiedDocument window property
5224 void SetModified(bool flag)
5228 this.modified = false;
5229 if(flag && !NotifyModified(master, this))
5230 this.modified = true;
5235 bool AddS(char * string)
5242 int addedSpaces = 0, addedTabs = 0;
5243 AddTextAction action = null;
5244 ReplaceTextAction replaceAction = null;
5246 this.pasteOperation = true;
5248 if(style.stuckCaret /*|EES_READONLY)*/ )
5251 if(!undoBuffer.dontRecord)
5253 char * placeString = CopyString(string);
5254 if(!style.multiLine)
5258 for(i = 0; (ch = placeString[i]); i++)
5261 placeString[i] = '\0';
5262 placeString = renew placeString byte[i+1];
5267 if(selX != x || selY != y)
5271 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5272 oldString = new char[len];
5273 newString = placeString;
5274 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5276 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5277 if(selY < y || (selY == y && selX < x))
5279 replaceAction.x1 = selX;
5280 replaceAction.y1 = selY;
5281 replaceAction.x2 = x;
5282 replaceAction.y2 = y;
5286 replaceAction.x1 = x;
5287 replaceAction.y1 = y;
5288 replaceAction.x2 = selX;
5289 replaceAction.y2 = selY;
5291 Record(replaceAction);
5295 if(string[0] == '\n')
5296 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5298 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5306 undoBuffer.dontRecord++;
5307 DelSel(&addedSpaces);
5311 for(c = 0; string[c]; c++)
5313 if(string[c] == '\n' || string[c] == '\r')
5315 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5320 if(string[c] == '\n')
5329 // Reset for next line
5333 if(string[c] == '\r' && *line == '\n')
5345 // Why was this here?
5348 // Add the line here
5350 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5356 undoBuffer.dontRecord--;
5361 action.addedSpaces = addedSpaces;
5362 action.addedTabs = addedTabs;
5364 else if(replaceAction)
5366 replaceAction.y3 = y;
5367 replaceAction.x3 = x;
5368 replaceAction.addedSpaces = addedSpaces;
5369 replaceAction.addedTabs = addedTabs;
5372 UpdateCaretPosition(true);
5374 this.pasteOperation = false;
5387 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5388 SetViewToCursor(true);
5394 void PutCh(unichar ch)
5398 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5399 //if((ch >= 32) || ch == '\n')
5401 int addedSpaces = 0, addedTabs = 0;
5402 ReplaceTextAction replaceAction = null;
5403 AddCharAction addCharAction = null;
5406 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5408 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5413 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5414 oldString = new char[len];
5415 UTF32toUTF8Len(&ch, 1, buffer, 4);
5416 newString = CopyString(buffer);
5417 GetText(oldString, line, y, x, line, y, x+1, false, false);
5418 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5419 replaceAction.x1 = x;
5420 replaceAction.y1 = y;
5421 replaceAction.x2 = x+1;
5422 replaceAction.y2 = y;
5423 Record(replaceAction);
5425 undoBuffer.dontRecord++;
5426 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5427 undoBuffer.dontRecord--;
5429 else if(!undoBuffer.dontRecord)
5431 if(selX != x || selY != y)
5436 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5437 oldString = new char[len];
5438 UTF32toUTF8Len(&ch, 1, buffer, 4);
5439 newString = CopyString(buffer);
5440 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5441 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5442 if(selY < y || (selY == y && selX < x))
5444 replaceAction.x1 = selX;
5445 replaceAction.y1 = selY;
5446 replaceAction.x2 = x;
5447 replaceAction.y2 = y;
5451 replaceAction.x1 = x;
5452 replaceAction.y1 = y;
5453 replaceAction.x2 = selX;
5454 replaceAction.y2 = selY;
5456 Record(replaceAction);
5460 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5461 Record(addCharAction);
5464 undoBuffer.dontRecord++;
5465 result = _AddCh(ch, &addedSpaces, &addedTabs);
5468 replaceAction.x3 = x;
5469 replaceAction.y3 = y;
5470 replaceAction.addedSpaces = addedSpaces;
5471 replaceAction.addedTabs = addedTabs;
5475 addCharAction.x -= addedTabs * (tabSize-1);
5476 addCharAction.addedSpaces = addedSpaces;
5477 addCharAction.addedTabs = addedTabs;
5479 undoBuffer.dontRecord--;
5483 if(result) SetViewToCursor(true);
5487 void PutS(char * string)
5492 SetViewToCursor(true);
5497 void Printf(char * format, ...)
5501 char temp[MAX_F_STRING];
5503 va_start(args, format);
5504 vsnprintf(temp, sizeof(temp), format, args);
5505 temp[sizeof(temp)-1] = 0;
5511 void SetContents(char * format, ...)
5515 undoBuffer.dontRecord++;
5517 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5520 char temp[MAX_F_STRING];
5522 va_start(args, format);
5523 vsnprintf(temp, sizeof(temp), format, args);
5524 temp[sizeof(temp)-1] = 0;
5531 undoBuffer.dontRecord--;
5544 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5547 else if(this.line.prev)
5549 EditLine line = this.line.prev;
5553 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5561 this.selLine = this.line;
5566 SetViewToCursor(true);
5571 Emptyline(this.line,this.y);
5572 this.selX = this.x = 0;
5575 this.selLine = this.line;
5577 SetViewToCursor(true);
5587 SetViewToCursor(true);
5595 SetViewToCursor(true);
5599 bool GoToLineNum(int lineNum)
5604 EditLine line = this.lines.first;
5605 for(c = 0; c < lineNum && line; c++, line = line.next);
5615 SetViewToCursor(true);
5622 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5623 bool GoToPosition(EditLine line, int y, int x)
5635 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5649 SetViewToCursor(true);
5656 void SetViewToCursor(bool setCaret)
5666 bool dontScroll = false;
5672 selected = selX != this.x || selY != y;
5679 checkLine = dropLine;
5685 checkLine = this.line;
5690 numLines = clientSize.h / space.h;
5692 // This is broken. The EditBox now doesn't do anything different when adding to it,
5693 // regardless of the previous scrolling position. It should be read and then set again
5694 // if one wishes to preserve it.
5695 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5696 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5700 // Horizontal Adjustment
5701 if(!dontScroll && checkLine)
5705 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5708 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5714 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5715 viewX = x - clientSize.w+space.w;
5716 if(x < this.viewX + clientSize.w/2 - space.w)
5717 viewX = Max(0, x - clientSize.w/2 + space.w);
5725 // Vertical Adjustment
5726 if(viewY > checkY) viewY = checkY;
5727 if(viewY + numLines <= checkY)
5729 if(clientSize.h >= space.h)
5731 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5741 for(;dropLine && dropLine.prev && dropY >= numLines;)
5743 dropLine = dropLine.prev;
5747 for(;this.line && this.line.prev && this.y >= numLines;)
5749 this.line = this.line.prev;
5754 SetScrollPosition(viewX, viewY * this.space.h);
5756 UpdateCaretPosition(setCaret);
5762 selLine = this.line;
5772 void CenterOnCursor()
5774 int numLines = clientSize.h / this.space.h;
5775 int y = this.y - numLines / 2;
5777 bool figureSyntax = false;
5778 EditLine oldViewLine = viewLine;
5779 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5784 for(;y < this.viewY; y++)
5786 this.viewLine = this.viewLine.prev;
5787 style.recomputeSyntax = true;
5789 for(;y > this.viewY; y--)
5791 this.viewLine = this.viewLine.next;
5792 figureSyntax = true;
5795 FigureStartSyntaxStates(oldViewLine, false);
5799 SetScrollPosition(this.viewX, viewY * this.space.h);
5800 UpdateCaretPosition(true);
5804 void SetCursorToView()
5815 numLines = clientSize.h / this.space.h;
5819 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5820 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5824 EditLine oldLine = this.line;
5825 bool lastOne = false;
5826 EditLine oldViewLine = this.viewLine;
5827 bool figureSyntax = false;
5829 if(this.y >= this.lineCount-1) return;
5831 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5833 if(this.viewY + numLines < this.lines.count)
5835 this.viewLine = this.viewLine.next;
5837 figureSyntax = true;
5839 else if(c && !lastOne)
5848 FigureStartSyntaxStates(oldViewLine, false);
5849 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5852 SetViewToCursor(false);
5861 if(this.y == 0) return;
5863 numLines = clientSize.h / this.space.h;
5867 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5868 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5872 EditLine oldLine = this.line;
5874 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5879 if(this.viewLine.prev)
5881 this.viewLine = this.viewLine.prev;
5883 style.recomputeSyntax = true;
5887 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5890 SetViewToCursor(false);
5897 if(this.viewLine.prev)
5900 // this.viewLine = this.viewLine.prev;
5903 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5910 if(this.viewLine.next)
5913 // this.viewLine = this.viewLine.next;
5916 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5923 EditLine l1, l2, line;
5924 int x1, x2, nx1, nx2;
5929 if(!this.selLine) return 0;
5930 if(this.selLine == this.line && this.selX == this.x) return 0;
5931 if(this.selY < this.y)
5938 else if(this.selY > this.y)
5945 else if(this.selX < this.x)
5947 l1 = l2 = this.line;
5953 l1 = l2 = this.line;
5957 nx1 = Min(x1,l1.count);
5958 nx2 = Min(x2,l2.count);
5960 // Find Number of Bytes Needed
5962 for(line = l1; line; line = line.next)
5964 if(line == l1) start = nx1; else start = 0;
5965 if(line == l2) end = nx2; else end = line.count;
5967 if(style.freeCaret && line == l2)
5970 count = Max(x2-Max(x1,l1.count),0);
5972 count = Max(x2-l2.count,0);
5976 if(line == l2) break;
5977 // Add Carriage Return / line Feed
5984 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5988 if(!reorder || this.selY < this.y)
5990 if(l1) *l1 = this.selLine;
5991 if(y1) *y1 = this.selY;
5992 if(x1) *x1 = this.selX;
5993 if(l2) *l2 = this.line;
5994 if(y2) *y2 = this.y;
5995 if(x2) *x2 = this.x;
5997 else if(this.selY > this.y)
5999 if(l1) *l1 = this.line;
6000 if(y1) *y1 = this.y;
6001 if(x1) *x1 = this.x;
6002 if(l2) *l2 = this.selLine;
6003 if(y2) *y2 = this.selY;
6004 if(x2) *x2 = this.selX;
6006 else if(this.selX < this.x)
6008 if(l1) *l1 = this.line;
6009 if(y1) *y1 = this.selY;
6010 if(x1) *x1 = this.selX;
6011 if(l2) *l2 = this.line;
6012 if(y2) *y2 = this.y;
6013 if(x2) *x2 = this.x;
6017 if(l1) *l1 = this.line;
6018 if(y1) *y1 = this.y;
6019 if(x1) *x1 = this.x;
6020 if(l2) *l2 = this.line;
6021 if(y2) *y2 = this.selY;
6022 if(x2) *x2 = this.selX;
6027 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6029 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6031 this.selLine = (EditLine)l1;
6034 this.line = (EditLine)l2;
6038 SetViewToCursor(true);
6042 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6044 EditLine l1, l2, line;
6045 int x1, x2, nx1, nx2;
6076 nx1 = Min(x1,l1.count);
6077 nx2 = Min(x2,l2.count);
6080 for(line = l1; line; line = line.next)
6082 if(line == l1) start = nx1; else start = 0;
6083 if(line == l2) end = nx2; else end = line.count;
6086 CopyBytes(text, line.buffer + start, end - start);
6089 numChars += end-start;
6091 if(style.freeCaret && line == l2 && addSpaces)
6094 count = Max(x2-Max(x1,l1.count),0);
6096 count = Max(x2-l2.count,0);
6099 FillBytes(text,' ',count);
6105 if(line == l2) break;
6117 // '\0' terminate Terminate
6124 void GetSel(char * text, bool addCr)
6126 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6129 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6147 int size = SelSize();
6150 // Try to allocate memory
6151 ClipBoard clipBoard { };
6152 if(clipBoard.Allocate(size+1))
6154 GetSel(clipBoard.memory, true);
6167 ClipBoard clipBoard { };
6168 if(clipBoard.Load())
6169 PutS(clipBoard.memory);
6181 SetViewToCursor(true);
6187 void DeleteSelection()
6192 SetViewToCursor(true);
6198 void Save(File f, bool cr)
6201 savedAction = undoBuffer.curAction;
6203 for(line = this.lines.first; line; line = line.next)
6205 f.Write(line.buffer, line.count, 1);
6208 if(cr) f.Putc('\r');
6214 #define BUFFER_SIZE 16384
6217 undoBuffer.dontRecord++;
6220 char buffer[BUFFER_SIZE];
6224 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6225 buffer[count] = '\0';
6231 undoBuffer.dontRecord--;
6232 undoBuffer.count = 0;
6233 undoBuffer.curAction = 0;
6234 itemEditUndo.disabled = undoBuffer.curAction == 0;
6235 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6238 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6242 bool firstPass = true;
6243 EditBoxFindResult result = found;
6245 if(!this.line) return notFound;
6248 for(line = this.line;;)
6256 line = this.lines.first;
6262 line = this.lines.last;
6263 num = this.lineCount - 1;
6269 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6271 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6275 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6278 if(line == this.line && !firstPass) break;
6296 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6300 int searchLen = strlen(text);
6301 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6303 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6304 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6306 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6313 bool OnKeyDown(Key key, unichar ch)
6316 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6318 if(!NotifyKeyDown(master, this, key, ch))
6328 this.mouseMove = false;
6329 OnLeftButtonUp(0,0,0);
6330 SetViewToCursor(true);
6340 public class EditBoxStream : File
6343 BufferLocation start, sel;
6350 EditBox editBox = this.editBox;
6352 editBox.x = start.x;
6353 editBox.y = start.y;
6354 editBox.line = start.line;
6356 editBox.selX = sel.x;
6357 editBox.selY = sel.y;
6358 editBox.selLine = sel.line;
6360 editBox.SetViewToCursor(true);
6361 //editBox.ComputeColumn();
6365 property EditBox editBox
6373 start.line = value.line;
6377 sel.line = value.selLine;
6380 value.GoToHome(true);
6383 get { return editBox; }
6386 uint Read(byte * buffer, uint size, uint count)
6389 EditBox editBox = this.editBox;
6390 EditLine line = editBox.line;
6396 for(;read < count && line; line = (*&line.next))
6398 int numBytes = Min(count - read, (*&line.count) - x);
6401 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6406 for(;read < count && x < (*&line.count);)
6408 buffer[read++] = (*&line.buffer)[x++];
6415 buffer[read++] = '\n';
6420 if(x == (*&line.count) && (*&line.next))
6429 editBox.line = editBox.selLine = line;
6430 editBox.x = editBox.selX = x;
6431 editBox.y = editBox.selY = y;
6436 bool Seek(int pos, FileSeekMode mode)
6439 EditBox editBox = this.editBox;
6440 EditLine line = editBox.line;
6442 if(mode == FileSeekMode::start)
6444 pos = pos - this.pos;
6454 for(;read < pos && line; line = (*&line.next))
6456 int numBytes = Min(pos - read, (*&line.count) - x);
6459 read += numBytes; x += numBytes;
6462 /*for(;read < pos && x < (*&line.count);)
6490 for(;read < pos && line; line = (*&line.prev))
6492 int numBytes = Min(pos - read, x);
6498 /*for(;read < pos && x > 0;)
6508 x = (*&(*&line.prev).count);
6524 editBox.line = editBox.selLine = line;
6525 editBox.x = editBox.selX = x;
6526 editBox.y = editBox.selY = y;
6531 bool Puts(char * string)
6533 EditBox editBox = this.editBox;
6534 BufferLocation start { editBox.line, editBox.y, editBox.x };
6538 editBox.AddS(string);
6540 pos.line = editBox.line;
6544 this.start.AdjustAdd(start, pos);
6545 sel.AdjustAdd(start, pos);
6550 // NOTE: BYTE, NOT UNICODE CHARACTER!
6553 EditBox editBox = this.editBox;
6554 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6559 utf8Bytes[numBytes++] = ch;
6560 utf8Bytes[numBytes] = 0;
6561 if(UTF8Validate(utf8Bytes))
6563 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6565 pos.line = editBox.line;
6568 this.start.AdjustAdd(start, pos);
6569 sel.AdjustAdd(start, pos);
6576 bool Getc(char * ch)
6578 return Read(ch, 1, 1) ? true : false;
6581 void DeleteBytes(uint count)
6583 EditBox editBox = this.editBox;
6586 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6587 BufferLocation end = pos;
6592 for(;c < count && end.line && end.x < end.line.count;)
6597 if(c < count && end.line && end.line.next)
6599 end.line = end.line.next;
6608 start.AdjustDelete(pos, end);
6609 sel.AdjustDelete(pos, end);
6611 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);