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__",
674 "class", "private", "public",
676 "delete", "new", "new0", "renew", "renew0", "define",
679 "dllexport", "dllimport", "stdcall",
680 "subclass", "__on_register_module", "namespace", "using",
681 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
682 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
683 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
686 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
689 "this", "true", "false", "null", "value",
698 static char * keyWords2[] =
700 "defined", "warning", null
703 static char ** keyWords[] = { keyWords1, keyWords2 };
704 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
705 //static int * keyLen[NUM_KEYWORD_GROUPS];
706 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
708 static char searchString[1025], replaceString[1025];
709 static bool matchCase = false, wholeWord = false, searchUp = false;
711 static GoToDialog goToDialog
713 autoCreate = false, isModal = true, text = $"Go To"
716 public class EditBox : CommonControl
718 class_property(icon) = "<:ecere>controls/editBox.png";
722 virtual bool Window::NotifyModified(EditBox editBox);
723 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
724 virtual void Window::NotifyUpdate(EditBox editBox);
725 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
726 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
727 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
729 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
730 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
731 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
733 virtual bool Window::NotifyUnsetModified(EditBox editBox);
735 // Why was this commented out?
736 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
737 // and the scrollArea property isn't compared properly either.
741 if(font) ComputeFont();
742 SetInitSize(initSize);
748 style.vScroll = true;
754 style.hScroll = true;
758 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
759 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
760 property bool readOnly
762 property_category $"Behavior"
765 style.readOnly = value;
766 itemEditCut.disabled = value || !selection;
767 itemEditDelete.disabled = value || !selection;
768 itemEditPaste.disabled = value;
770 get { return style.readOnly; }
772 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
773 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
774 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
775 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
776 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
777 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
778 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
779 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
780 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
781 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
782 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
783 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
784 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
785 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
786 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
787 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
788 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
789 property int numLines { get { return this ? lineCount : 0; } };
790 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
791 property int column { get { return col; } }; // TODO: Add Set
792 property int charPos { get { return x; } }; // TODO: Add Set
793 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
794 property EditLine lastLine { get { return lines.last; } };
795 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
796 property char * contents
798 property_category $"Data"
803 undoBuffer.dontRecord++;
805 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
808 //SetViewToCursor(true);
811 undoBuffer.dontRecord--;
817 char * buffer = null;
820 /* Can't implement this right now because of memory leak... Need string reference counting...
827 for(line = lines.first; line; line = line.next)
828 len += strlen(line.buffer);
830 buffer = new char[len+1];
832 for(line = lines.first; line; line = line.next)
834 int lineLen = strlen(line.buffer);
835 memcpy(buffer + len, line.buffer, lineLen);
841 buffer = this.line ? this.line.buffer : null;
846 property bool overwrite { get { return overwrite; } };
847 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
849 property char * multiLineContents
853 char * buffer = null;
860 for(line = lines.first; line; line = line.next)
861 len += strlen(line.buffer)+1;
863 buffer = new char[len+1];
865 for(line = lines.first; line; line = line.next)
867 int lineLen = strlen(line.buffer);
868 memcpy(buffer + len, line.buffer, lineLen);
870 if(line.next) buffer[len++] = '\n';
883 return this.line.buffer;
888 void SetLineText(char * text)
892 EditLine_SetText(this.line, text);
896 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
897 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
898 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
899 property bool recordUndoEvent { set { undoBuffer.recordAsOne = value; undoBuffer.firstEvent = true; } get { return undoBuffer.recordAsOne; } };
901 // selectionStart.line, selectionStart.column (With Set)
902 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
916 // Position of Caret (Not necessarily displayed position)
919 // Position of beginning of block (Block ends at (x,y))
921 // line is line at carret, selLine is line at beginning of block
922 EditLine line, selLine, dropLine;
923 // Mouse selection Moving virtual caret
928 // ViewX is x offset in pixels, ViewY is y offset in lines
930 // viewLine is first displayed line
933 // start and end of area to redraw
936 // MaxLine is the longest line
938 // MaxLength is the longest line's length
941 // MouseSelect is true once button is pressed, overwrite is true if mode is on
942 bool mouseSelect, mouseMove, overwrite, wordSelect;
943 // Timer is used for mouse selection scrolling
946 window = this, delay = 0.1;
955 OnMouseMove(mouseX, mouseY, -1);
960 // (mouseX,mouseY) is the position of the mouse in the edit box
965 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
968 bool rightButtonDown;
971 UndoBuffer undoBuffer { data = this };
973 ColorAlpha selectionColor, selectionText;
974 SyntaxColorScheme colorScheme { };
979 Menu editMenu { menu, $"Edit", e };
982 editMenu, $"Cut\tCtrl+X", t, disabled = true;
984 bool NotifySelect(MenuItem item, Modifiers mods)
986 if(!(style.readOnly))
991 MenuItem itemEditCopy
993 editMenu, $"Copy\tCtrl+C", c, disabled = true;
995 bool NotifySelect(MenuItem item, Modifiers mods)
1001 MenuItem itemEditPaste
1003 editMenu, $"Paste\tCtrl+V", p;
1005 bool NotifySelect(MenuItem item, Modifiers mods)
1007 if(!(style.readOnly))
1012 MenuItem itemEditDelete
1014 editMenu, $"Delete\tDel", d, disabled = true;
1016 bool NotifySelect(MenuItem item, Modifiers mods)
1018 if(!(style.readOnly))
1023 MenuDivider { editMenu };
1024 MenuItem itemEditSelectAll
1026 editMenu, $"Select All\tCtrl+A", a;
1028 bool NotifySelect(MenuItem item, Modifiers mods)
1034 MenuDivider { editMenu };
1035 MenuItem itemEditUndo
1037 editMenu, $"Undo\tCtrl+Z", u;
1040 bool NotifySelect(MenuItem item, Modifiers mods)
1046 MenuItem itemEditRedo
1048 editMenu, $"Redo\tCtrl+Y", o;
1051 bool NotifySelect(MenuItem item, Modifiers mods)
1057 MenuDivider { editMenu };
1060 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1062 bool NotifySelect(MenuItem item, Modifiers mods)
1065 Find(searchString, wholeWord, matchCase, false);
1067 itemEditFind.NotifySelect(this, item, mods);
1073 editMenu, $"Find Next\tF3", n, f3;
1075 bool NotifySelect(MenuItem item, Modifiers mods)
1078 Find(searchString, wholeWord, matchCase, true);
1080 itemEditFind.NotifySelect(this, item, mods);
1084 MenuItem itemEditFind
1086 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1088 bool NotifySelect(MenuItem item, Modifiers mods)
1092 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1093 searchUp = searchUp;
1096 // Fix dialog from above shouldn't be visible
1097 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1098 void NotifyDestroyed(Window window, DialogResult result)
1100 FindDialog dialog = (FindDialog) window;
1101 searchUp = dialog.searchUp;
1102 strcpy(searchString, dialog.searchString);
1103 matchCase = dialog.matchCase;
1104 wholeWord = dialog.wholeWord;
1113 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1115 bool NotifySelect(MenuItem item, Modifiers mods)
1117 ReplaceDialog dialog
1121 searchString = searchString,
1122 replaceString = replaceString,
1123 matchCase = matchCase,
1124 wholeWord = wholeWord,
1128 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1129 void NotifyDestroyed(Window window, DialogResult result)
1131 ReplaceDialog dialog = (ReplaceDialog)window;
1132 char * replace = dialog.replaceString;
1134 strcpy(replaceString, replace);
1135 strcpy(searchString, dialog.searchString);
1136 matchCase = dialog.matchCase;
1137 wholeWord = dialog.wholeWord;
1144 MenuDivider { editMenu };
1147 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1149 bool NotifySelect(MenuItem item, Modifiers mods)
1151 goToDialog.editBox = this;
1152 goToDialog.master = master;
1153 goToDialog.Create();
1157 MenuDivider { editMenu };
1158 MenuItem itemEditInsertTab
1160 editMenu, $"Insert Tabs", i, checkable = true;
1162 bool NotifySelect(MenuItem item, Modifiers mods)
1164 style.useTab = item.checked;
1170 snapVertScroll = true;
1171 snapHorzScroll = true;
1175 static bool syntaxInit = false;
1180 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1182 for(c = 0; keyWords[g][c]; c++);
1183 //keyLen[g] = new int[c];
1184 for(c = 0; keyWords[g][c]; c++)
1186 keyLen[g][c] = strlen(keyWords[g][c]);
1190 colorScheme.commentColor = dimGray;
1191 colorScheme.charLiteralColor = crimson;
1192 colorScheme.stringLiteralColor = crimson;
1193 colorScheme.preprocessorColor = green;
1194 colorScheme.numberColor = teal;
1195 colorScheme.keywordColors = [ blue, blue ];
1198 FontExtent = Display::FontExtent;
1200 lines.offset = (uint)&((EditLine)0).prev;
1202 style = EditBoxBits { hScroll = true };
1204 // cursor = guiApp.GetCursor(IBeam);
1206 // Default Properties
1208 maxLineSize = 1024;*/
1211 maxLineSize = MAXINT;
1216 mouseSelect = this.mouseMove = false;
1219 x = selX = selY = 0;
1222 line = selLine = null;
1228 endY = clientSize.h;
1231 // Default space to 8 in case window is not constructed
1234 undoBuffer.dontRecord++;
1236 undoBuffer.dontRecord--;
1238 viewLine = lines.first;
1239 style.recomputeSyntax = true;
1245 UpdateCaretPosition(true);
1251 lines.Free(EditLine::Free);
1254 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1256 int count = wc - *renderStart;
1259 if(y + space.h >= box.top && y <= box.bottom)
1265 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1266 surface.TextFont(font);
1267 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1268 if(*x + w + XOFFSET > 0)
1269 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1274 w = numSpaces; // * space.w;
1275 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1276 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1277 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1285 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1286 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1292 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1296 *foreground = (*selected) ? selectionForeground : textColor;
1297 *background = selectionBackground;
1298 *opacity = *selected;
1305 if(this.overwrite && active)
1307 if((style.stuckCaret && wc == line.count && !line.next) ||
1308 (!mouseMove && line == this.line && wc == editX))
1317 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1321 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1322 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1323 bool inString = false;
1324 bool inQuotes = false;
1325 bool inPrep = reset ? false : style.inPrep;
1326 bool inSingleLineComment = false;
1327 bool escaped = reset ? false : style.escaped;
1328 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1329 bool continuedString = reset ? false : style.continuedString;
1330 bool continuedQuotes = reset ? false : style.continuedQuotes;
1332 EditLine line = reset ? lines.first : firstLine;
1333 // int maxBackUp = 1000, c;
1334 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1335 for(; line != viewLine; line = line.next)
1337 char * text = line.buffer;
1340 bool lastWasStar = false;
1341 bool firstWord = true;
1342 if(!escaped) inPrep = false;
1343 inSingleLineComment = continuedSingleLineComment;
1345 inString = continuedString;
1346 inQuotes = continuedQuotes;
1349 for(c = 0; (ch = text[c]); c++)
1351 bool wasEscaped = escaped;
1352 bool backLastWasStar = lastWasStar;
1353 bool backWasInMultiLine = wasInMultiLine;
1355 lastWasStar = false;
1356 wasInMultiLine = inMultiLineComment;
1359 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1361 if(text[c+1] == '/')
1363 inSingleLineComment = true;
1365 else if(text[c+1] == '*')
1367 inMultiLineComment = true;
1370 else if(backLastWasStar)
1371 inMultiLineComment = false;
1375 if(backWasInMultiLine) lastWasStar = true;
1377 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1379 if(inString && !wasEscaped)
1388 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1390 if(inQuotes && !wasEscaped)
1402 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1409 else if(ch != ' ' && ch != '\t')
1412 if(line.count && line.text[line.count - 1] == '\\')
1414 continuedSingleLineComment = inSingleLineComment;
1415 continuedString = inString;
1416 continuedQuotes = inQuotes;
1420 continuedSingleLineComment = false;
1421 continuedString = false;
1422 continuedQuotes = false;
1426 style.continuedSingleLineComment = continuedSingleLineComment;
1427 style.continuedString = continuedString;
1428 style.continuedQuotes = continuedQuotes;
1429 style.inMultiLineComment = inMultiLineComment;
1430 style.wasInMultiLine = wasInMultiLine;
1431 style.inPrep = inPrep;
1432 style.escaped = escaped;
1436 /*void OnDrawOverChildren(Surface surface)
1438 if(style.lineNumbers)
1440 int currentLineNumber = this.viewY + 1;
1443 for(i = 0; i * space.h < box.height; i++)
1445 // ********* LINE NUMBERING *********
1446 surface.SetForeground(Color{60, 60, 60});
1447 //Highlight current line
1448 if(this.caretY / space.h == currentLineNumber - 1)
1449 surface.SetBackground(Color{220, 220, 220});
1451 surface.SetBackground(Color{230, 230, 230});
1452 surface.textOpacity = true;
1454 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1455 if(currentLineNumber > this.lineCount)
1456 surface.WriteText(0,i*space.h+1," ",6);
1458 surface.WriteText(0,i*space.h+1,lineText,6);
1460 currentLineNumber++;
1465 void OnRedraw(Surface surface)
1469 bool selected = false, selection = true;
1471 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1472 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1473 Color defaultTextColor = property::foreground;
1476 int maxW = clientSize.w;
1478 Color foreground, background;
1481 // Overwrite Caret Stuff
1482 bool overWrite = false;
1483 int overWriteX, overWriteY;
1486 // ****** SYNTAX STATES ******
1487 bool inMultiLineComment = style.inMultiLineComment;
1488 bool inString = false;
1489 bool inQuotes = false;
1490 bool inPrep = style.inPrep;
1491 bool inSingleLineComment = false;
1492 bool escaped = style.escaped;
1493 bool continuedSingleLineComment = style.continuedSingleLineComment;
1494 bool continuedString = style.continuedString;
1495 bool continuedQuotes = style.continuedQuotes;
1496 bool wasInMultiLine = style.wasInMultiLine;
1497 // ****** ************* ******
1500 defaultTextColor = Color { 85, 85, 85 };
1501 textColor = defaultTextColor;
1504 Abs(selectionBackground.r - property::background.r) +
1505 Abs(selectionBackground.g - property::background.g) +
1506 Abs(selectionBackground.b - property::background.b) < 92)
1508 selectionBackground = formColor;
1509 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1512 surface.TextFont(this.font);
1514 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1515 surface.Area(0,0,MAXINT,MAXINT);
1520 surface.SetBackground(formColor);
1521 surface.Area(0,0,clientSize.w, clientSize.h);
1524 if(this.selX == this.x && this.selY == this.y)
1534 editX = Min(this.x,this.line.count);
1535 selX = Min(this.selX,this.selLine.count);
1538 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1540 foreground = selected ? selectionForeground : textColor;
1541 background = selectionBackground;
1547 surface.SetForeground(foreground);
1548 surface.SetBackground(background);
1549 surface.TextOpacity(opacity);
1551 surface.GetBox(box);
1553 for(line = this.viewLine; line; line = line.next)
1555 int x = -this.viewX;
1559 Color newTextColor = textColor = defaultTextColor;
1560 bool lineComplete = false;
1563 // ****** SYNTAX HIGHLIGHTING ******
1564 bool lastWasStar = false;
1565 bool trailingSpace = false;
1566 bool firstWord = true;
1567 if(!escaped) inPrep = false;
1568 inSingleLineComment = continuedSingleLineComment;
1570 inString = continuedString;
1571 inQuotes = continuedQuotes;
1572 // *********************************
1574 /* === DEBUGGING TOOL FOR MAXLINE ===
1576 if(line == this.maxLine)
1578 surface.SetBackground(GREEN|0xFF000000);
1579 surface.TextOpacity(true);
1583 surface.TextOpacity(selected ? true : false);
1584 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1588 if(line == this.selLine && line == this.line)
1590 end = Max(line.count, this.x);
1591 end = Max(end, this.selX);
1593 else if(line == this.selLine)
1594 end = Max(line.count, this.selX);
1595 else if(line == this.line)
1596 end = Max(line.count, this.x);
1604 bool spacing = true;
1605 bool cantHaveWords = false;
1612 lineComplete = false;
1615 textColor = newTextColor;
1618 foreground = textColor;
1619 surface.SetForeground(textColor);
1623 for(; c<end && !cantHaveWords;)
1627 bufferLen += wordLen;
1631 /*if(line.count > 4000)
1638 for(; c<line.count; c++)
1640 unichar ch = line.buffer[c];
1641 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1642 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1643 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1644 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1645 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1648 trailingSpace = false;
1654 for(; c<line.count; c++)
1656 unichar ch = line.buffer[c];
1657 if(ch == '\t' || ch == ' ')
1659 cantHaveWords = true;
1660 if(ch == ' ' && c == line.count-1)
1661 trailingSpace = true;
1665 if(ch != ' ' && ch != '\t')
1670 if(c == line.count && c < end)
1679 cantHaveWords = true;
1684 lastWasStar = false;
1690 bool backEscaped = escaped;
1691 bool backLastWasStar = lastWasStar;
1692 bool backInMultiLineComment = inMultiLineComment;
1693 bool backInString = inString;
1694 bool backInQuotes = inQuotes;
1695 bool backInPrep = inPrep;
1696 bool backInSingleLineComment = inSingleLineComment;
1697 bool backWasInMultiLine = wasInMultiLine;
1699 char * word = line.buffer + c - wordLen;
1701 bool wasEscaped = escaped;
1703 lastWasStar = false;
1705 wasInMultiLine = inMultiLineComment;
1707 // Determine Syntax Highlighting
1708 newTextColor = defaultTextColor;
1711 if(inSingleLineComment || inMultiLineComment)
1713 newTextColor = colorScheme.commentColor;
1717 newTextColor = colorScheme.charLiteralColor;
1721 newTextColor = colorScheme.stringLiteralColor;
1725 newTextColor = colorScheme.preprocessorColor;
1727 if(wordLen == 1 && word[0] == '/')
1729 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1733 inSingleLineComment = true;
1734 newTextColor = colorScheme.commentColor;
1736 else if(word[1] == '*')
1738 inMultiLineComment = true;
1739 newTextColor = colorScheme.commentColor;
1742 else if(backLastWasStar)
1743 inMultiLineComment = false;
1745 else if(wordLen == 1 && word[0] == '*')
1747 if(backWasInMultiLine)
1750 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1752 if(inString && !wasEscaped)
1759 newTextColor = colorScheme.stringLiteralColor;
1762 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1764 if(inQuotes && !wasEscaped)
1769 newTextColor = colorScheme.charLiteralColor;
1772 else if(wordLen == 1 && word[0] == '\\')
1777 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1779 char * dot = strchr(word, '.');
1780 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1786 exponent = strchr(word, 'p');
1787 if(!exponent) exponent = strchr(word, 'P');
1791 exponent = strchr(word, 'e');
1792 if(!exponent) exponent = strchr(word, 'E');
1794 if(dot && dot > word + wordLen) dot = null;
1795 isReal = dot || exponent;
1797 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1799 strtol(word, &s, 0);
1805 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1808 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1812 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1815 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1818 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1819 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1820 default: valid = false;
1824 // Don't highlight numbers with too many decimal points
1825 if(s[0] == '.' && isdigit(s[1]))
1828 while(s[0] == '.' && isdigit(s[1]))
1830 int newWordLen = s - word;
1831 c += newWordLen - wordLen;
1832 wordLen = newWordLen;
1835 newWordLen = s - word;
1836 c += newWordLen - wordLen;
1837 wordLen = newWordLen;
1841 int newWordLen = s + i - word;
1842 newTextColor = colorScheme.numberColor;
1843 c += newWordLen - wordLen;
1844 wordLen = newWordLen;
1846 else if(dot && dot > word && dot < s)
1847 newTextColor = colorScheme.numberColor;
1852 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1857 newTextColor = colorScheme.preprocessorColor;
1860 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1862 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1864 char ** keys = keyWords[g];
1865 int * len = keyLen[g];
1866 for(ccc = 0; keys[ccc]; ccc++)
1868 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1870 newTextColor = colorScheme.keywordColors[g];
1879 // If highlighting for this word is different, break
1880 if(newTextColor != textColor)
1884 // Better solution than going back?
1887 // Reset syntax flags
1888 escaped = backEscaped;
1889 lastWasStar = backLastWasStar;
1890 inMultiLineComment = backInMultiLineComment;
1891 inString = backInString;
1892 inQuotes = backInQuotes;
1893 inPrep = backInPrep;
1894 inSingleLineComment = backInSingleLineComment;
1895 wasInMultiLine = backWasInMultiLine;
1900 textColor = newTextColor;
1903 foreground = textColor;
1904 surface.SetForeground(textColor);
1911 // If we're not breaking, this can't be rendered as spacing anymore
1914 // Do word wrapping here
1920 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1925 w += numSpaces * space.w;
1927 if(x + viewX + w > maxW)
1929 // Avoid an endless loop until we fix wrapping
1930 if(c - wordLen > start)
1933 lineComplete = true;
1939 bufferLen += wordLen;
1944 int renderStart = start;
1946 bool flagTrailingSpace = false;
1950 // Render checking if we need to split because of selection or to find where to draw insert caret
1951 for(wc = start; wc < start + bufferLen; wc++)
1953 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1954 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1955 if(overWrite == true)
1957 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1958 if(overWriteCh == '\t') overWriteCh = ' ';
1963 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1964 if(flagTrailingSpace) surface.SetBackground(red);
1965 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1966 if(overWrite == true)
1974 surface.TextOpacity(opacity);
1975 surface.SetBackground(background);
1976 surface.SetForeground(foreground);
1983 if(wc < line.count && line.buffer[wc] == '\t')
1985 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1989 numSpaces += space.w;
1993 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1994 if(flagTrailingSpace) surface.SetBackground(red);
1995 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
2000 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
2001 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
2003 if(overWrite == true)
2010 surface.TextOpacity(opacity);
2011 surface.SetBackground(background);
2012 surface.SetForeground(foreground);
2015 if(style.freeCaret && selected)
2017 surface.SetBackground(selectionBackground);
2018 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2019 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2024 if(style.freeCaret && selected)
2026 surface.SetBackground(selectionBackground);
2027 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2030 if(line.count && line.text[line.count - 1] == '\\')
2032 continuedSingleLineComment = inSingleLineComment;
2033 continuedString = inString;
2034 continuedQuotes = inQuotes;
2038 continuedSingleLineComment = false;
2039 continuedString = false;
2040 continuedQuotes = false;
2044 if(y > box.bottom) // >=clientSize.h)
2050 surface.TextOpacity(true);
2051 surface.SetForeground(black);
2052 surface.SetBackground(Color {255,255,85});
2053 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2057 void FixScrollArea()
2059 if(style.hScroll || style.vScroll)
2061 int width = maxLength + XOFFSET;
2062 int height = lineCount * space.h;
2063 if(style.freeCaret && line)
2068 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2072 if(selX > selLine.count)
2073 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2078 SetScrollLineStep(8, space.h);
2079 SetScrollArea(width, height, true);
2083 void ComputeLength(EditLine line)
2090 for(c = 0; c < line.count; )
2099 for(len = 0; c < line.count; c += numBytes)
2101 ch = line.buffer[c];
2102 numBytes = UTF8_NUM_BYTES(ch);
2104 if(ch == ' ' || ch == '\t')
2111 if(!len && ch == ' ')
2116 else if(!len && ch == '\t')
2118 w = (tabSize * space.w) - (x % (tabSize * space.w));
2122 FontExtent(display, font, line.buffer + start, len, &w, null);
2133 if(line.length > this.maxLength)
2135 this.maxLine = line;
2136 this.maxLength = line.length;
2138 if(style.autoSize) AutoSize();
2147 this.maxLine = null;
2149 for(line = lines.first; line; line = line.next)
2151 if(line.length > this.maxLength)
2153 this.maxLength = line.length;
2154 this.maxLine = line;
2158 if(style.autoSize) AutoSize();
2163 if(this.selY != this.y)
2165 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2169 void ComputeColumn()
2171 // Adjust new edit X position according to tabs
2172 int c, position = 0;
2175 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2177 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2179 position += this.tabSize - (position % this.tabSize);
2183 position += this.x - c;
2184 this.col = position;
2187 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2189 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2192 bool HasCommentOrEscape(EditLine line)
2194 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2199 for(c = line.count-1; c >= 0; c--)
2201 char ch = line.buffer[c];
2207 else //if(ch != ' ' && ch != '\t')
2214 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2216 EditLine line = l1, next;
2218 int oldCount1, oldCount2;
2219 int y, firstViewY, firstY, firstDropY, firstSelY;
2222 DelTextAction action = null;
2223 bool hadComment = false;
2227 hadComment = HasCommentOrEscape(line);
2229 if(y2 > y1 || c2 > c1)
2231 if(start < l1.count)
2233 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2237 oldCount1 = l1.count;
2239 while(c1 < oldCount1)
2241 byte ch = buffer[c1];
2242 if(UTF8_IS_FIRST(ch)) break;
2246 oldCount2 = l2.count;
2248 while(c2 < oldCount2)
2250 byte ch = buffer[c2];
2251 if(UTF8_IS_FIRST(ch)) break;
2256 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2261 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2262 string = new char[len];
2263 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2264 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2268 //oldCount1 = l1.count;
2269 //oldCount2 = l2.count;
2272 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2273 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2276 if(c2 > oldCount2) c2 = oldCount2;
2277 if(!(style.freeCaret))
2278 if(c1 > oldCount1) c1 = oldCount1;
2279 newLineCount = c1 + l2.count-c2;
2285 buffer = new char[line.size ? line.size : 1];
2287 buffer = new char[line.size];
2289 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2294 if(!line.AdjustBuffer(newLineCount))
2298 /*if(newLineCount > 4000 || newLineCount < 0)
2299 printf("Warning");*/
2301 line.count = newLineCount;
2303 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2308 action.addedSpaces = c1-oldCount1;
2310 if(action.addedSpaces > action.x1)
2316 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2317 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2319 line.buffer[line.count] = '\0';
2326 this.dropX -= c2-c1;
2327 this.dropX = Max(this.dropX,0);
2329 this.x = Max(this.x,0);
2336 firstViewY = this.viewY;
2338 firstDropY = this.dropY;
2339 firstSelY = this.selY;
2340 for(line = l1;line;line = next, y++)
2348 if(line == this.viewLine)
2350 if(this.viewLine.next)
2352 this.viewLine = this.viewLine.next;
2354 style.recomputeSyntax = true;
2358 this.viewLine = this.viewLine.prev;
2360 style.recomputeSyntax = true;
2363 else if(y < firstViewY)
2365 if(line == this.line)
2369 this.line = this.line.next;
2370 this.x = this.line.count;
2375 this.line = this.line.prev;
2376 this.x = this.line.count;
2383 if(line == this.dropLine)
2385 if(this.dropLine.next)
2387 this.dropLine = this.dropLine.next;
2388 this.dropX = this.dropLine.count;
2392 this.dropLine = this.dropLine.prev;
2393 this.dropX = this.dropLine.count;
2397 else if(y < firstDropY)
2399 if(line == this.selLine)
2401 if(this.selLine.next)
2403 this.selLine = this.selLine.next;
2404 this.selX = this.selLine.count;
2408 this.selLine = this.selLine.prev;
2409 this.selX = this.selLine.count;
2413 else if(y < firstSelY)
2417 if(line == l2) break;
2421 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2424 style.recomputeSyntax = true;
2429 bool DelSel(int * addedSpacesPtr)
2431 if(this.line != this.selLine || this.x != this.selX)
2433 if(this.selY < this.y)
2435 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2438 this.line = this.selLine;
2440 else if(this.selY > this.y)
2442 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2445 this.selLine = this.line;
2447 else if(this.selX < this.x)
2449 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2452 this.line = this.selLine;
2456 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2459 this.selLine = this.line;
2467 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2469 bool hadComment = false;
2470 // Add the line here
2471 EditLine line = this.line;
2473 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2474 if(!style.hScroll && created)
2476 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2481 // Lock if no place to display.
2484 else if(endX > this.x)
2485 max = Max(this.x, line.count);
2489 for(x = 0, c = 0; c < max+count; )
2494 if(c < Min(this.x, line.count))
2495 string = line.buffer + c;
2498 else if(c < endX + count)
2499 string = stringLine + c - endX;
2501 string = line.buffer + c - endX - count;
2505 w = (tabSize * space.w) - (x % (tabSize * space.w));
2509 numBytes = UTF8_NUM_BYTES(*string);
2510 FontExtent(display, this.font, string, numBytes, &w, null);
2514 if(x >= clientSize.w)
2519 c += numBytes; // - 1;
2525 int addedSpaces = 0;
2528 // Add blank spaces if EES_FREECARET
2529 if(this.x > line.count)
2539 for(c = 0; c<line.count; c++)
2541 if(this.line.buffer[c] == '\t')
2542 position += this.tabSize - (position % this.tabSize);
2546 wantedPosition = position + (this.x - line.count);
2548 // A tab is too much...
2549 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2550 addedSpaces = wantedPosition - position;
2555 position += this.tabSize - (position % this.tabSize);
2556 // Add as many tabs as needed
2557 addedTabs += (wantedPosition - position) / this.tabSize;
2558 position += (addedTabs-1) * this.tabSize;
2559 // Finish off with spaces
2560 addedSpaces = wantedPosition - position;
2564 addedSpaces = this.x - line.count;
2567 this.x = Min(this.x, line.count);
2569 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2575 hadComment = HasCommentOrEscape(line);
2577 // Adjust the size of the line
2578 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2582 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2585 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2586 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2589 *addedTabsPtr = addedTabs;
2590 FillBytes(line.buffer+line.count,'\t',addedTabs);
2592 if(addedTabs > 4000 || addedTabs < 0)
2595 line.count += addedTabs;
2601 FillBytes(line.buffer+line.count,' ',addedSpaces);
2603 if(addedSpaces > 4000 || addedSpaces < 0)
2606 line.count += addedSpaces;
2607 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2609 else if(addedSpacesPtr)
2610 *addedSpacesPtr = 0;
2612 if(count > 4000 || count < 0)
2615 line.count += count;
2616 this.x += count + addedTabs + addedSpaces;
2619 this.selLine = this.line;
2621 line.buffer[line.count] = '\0';
2623 ComputeLength(line);
2628 hasComment = HasCommentOrEscape(line);
2629 if(!undoBuffer.insideRedo)
2631 int backDontRecord = undoBuffer.dontRecord;
2632 undoBuffer.dontRecord = 0;
2633 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2634 undoBuffer.dontRecord = backDontRecord;
2636 if(style.syntax && (hadComment || hasComment || line != this.line))
2638 style.recomputeSyntax = true;
2646 void Emptyline(EditLine line, int y)
2649 DelCh(line, y, 0, line.next, y+1, 0, true);
2651 DelCh(line, y, 0, line, y, line.count, true);
2654 void GoToEnd(bool deselect)
2658 this.line = this.lines.last;
2659 if(this.y != this.lineCount-1)
2661 else if (this.x != this.line.count)
2662 DirtyLine(this.lineCount-1);
2663 this.y = this.lineCount-1;
2664 this.x = this.line.count;
2671 void GoToHome(bool deselect)
2675 this.line = this.lines.first;
2678 else if (this.x !=0)
2679 DirtyLine(this.lineCount-1);
2688 // Returns true if it needs scrolling
2689 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2695 bool needHScroll = false;
2702 line = this.viewLine ? (void *)this.viewLine.prev : null;
2707 line = (void *)this.lines.first;
2712 py = Min(py, clientSize.h);
2714 py = Min(py, this.lineCount);
2716 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2722 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2725 px = Min(px,clientSize.w+this.space.w);
2729 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2732 if(tline) *tline = line;
2735 // Prevent divide by 0 from non valid this.font
2737 return (y < this.viewY) || needHScroll;
2739 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2743 // Minimal Update Management Functions
2747 this.endY = clientSize.h-1;
2748 // ErrorLog("DirtyAll\n");
2751 void DirtyEnd(int y)
2753 if((y - this.viewY)*this.space.h < this.startY)
2754 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2755 this.endY = clientSize.h-1;
2756 //ErrorLog("DirtyEnd %d\n", y);
2759 void DirtyLine(int y)
2763 if((y - this.viewY)*this.space.h < this.startY)
2764 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2765 if((y - this.viewY+1)*this.space.h > this.endY)
2766 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2768 //ErrorLog("DirtyLine %d\n", y);
2775 if(style.recomputeSyntax)
2777 FigureStartSyntaxStates(lines.first, true);
2778 style.recomputeSyntax = false;
2781 if(this.startY > this.endY) return;
2782 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2788 box.right = clientSize.w-1;
2789 box.top = this.startY;
2790 box.bottom = this.endY;
2793 this.startY = clientSize.h;
2797 bool IsMouseOnSelection()
2799 bool mouseOnSelection = false;
2802 int minY = Min(this.selY, this.y);
2803 int maxY = Max(this.selY, this.y);
2804 int minX = Min(this.selX, this.x);
2805 int maxX = Max(this.selX, this.x);
2807 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2809 if(maxX != minX || maxY != minY)
2811 if(y > minY && y < maxY)
2812 mouseOnSelection = true;
2813 else if(y == minY && y == maxY)
2814 mouseOnSelection = (x < maxX && x >= minX);
2818 mouseOnSelection = (x >= this.selX);
2819 else if(y == this.y)
2820 mouseOnSelection = (x >= this.x);
2825 mouseOnSelection = (x < this.selX);
2826 else if(y == this.y)
2827 mouseOnSelection = (x < this.x);
2830 return mouseOnSelection;
2833 void UpdateCaretPosition(bool setCaret)
2837 if(mouseMove || (!overwrite && !style.noCaret))
2839 int max = this.mouseMove ? this.dropX : this.x;
2840 int y = this.mouseMove ? this.dropY : this.y;
2841 EditLine line = this.mouseMove ? this.dropLine : this.line;
2843 if(!(style.freeCaret))
2844 max = Min(max, line.count);
2846 if(FontExtent && display)
2848 for(c = 0; c < max; )
2857 for(len = 0; c < Min(max, line.count); c += numBytes)
2859 ch = line.buffer[c];
2860 numBytes = UTF8_NUM_BYTES(ch);
2862 if(ch == ' ' || ch == '\t')
2869 if(!len && ch == ' ')
2874 else if(!len && ch == '\t')
2876 w = (tabSize * space.w) - (x % (tabSize * space.w));
2880 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2892 caretY = y * this.space.h;
2893 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2898 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2899 NotifyCaretMove(master, this, y + 1, x + 1);
2905 void SelectionEnables()
2907 if((x != selX || y != selY) && !selection)
2911 itemEditCut.disabled = false;
2912 itemEditDelete.disabled = false;
2914 itemEditCopy.disabled = false;
2916 this.selection = true;
2918 else if((x == selX && y == selY) && selection)
2920 itemEditCut.disabled = true;
2921 itemEditCopy.disabled = true;
2922 itemEditDelete.disabled = true;
2924 this.selection = false;
2928 void SetSelectCursor()
2930 if(!inactive || !style.noSelect)
2933 cursor = guiApp.GetCursor(arrow);
2934 else if(this.mouseSelect)
2935 cursor = guiApp.GetCursor(iBeam);
2938 if(IsMouseOnSelection())
2939 cursor = guiApp.GetCursor(arrow);
2941 cursor = guiApp.GetCursor(iBeam);
2946 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2949 int x = px ? *px : 0;
2956 if(c < Min(max, line.count))
2960 for(len = 0; c < Min(max, line.count); c += numBytes)
2962 ch = line.buffer[c];
2963 numBytes = UTF8_NUM_BYTES(ch);
2965 if(ch == ' ' || ch == '\t')
2972 if(!len && ch == ' ')
2977 else if(!len && ch == '\t')
2979 w = (tabSize * space.w) - (x % (tabSize * space.w));
2983 FontExtent(display, font, line.buffer + start, len, &w, null);
2987 if(style.freeCaret && c < max)
2996 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
3001 int a = start + len;
3004 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
3008 FontExtent(display, font, line.buffer + start, a - start, &w, null);
3011 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
3015 return Min(this.maxLineSize - 1, start + len);
3021 void SetCursorToViewX()
3023 bool selecting = this.x != selX || y != selY;
3025 // Horizontal Adjustment
3027 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3032 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3044 UpdateCaretPosition(false);
3050 void SetCursorToViewY()
3053 EditLine oldLine = this.line;
3055 bool selecting = this.x != this.selX || this.y != this.selY;
3057 numLines = clientSize.h / this.space.h;
3059 // Vertical Adjustment
3060 if(this.viewY > this.y)
3062 this.y = this.viewY;
3063 this.line = this.viewLine;
3066 if(this.viewY + numLines <= this.y)
3070 this.y = this.viewY-1;
3071 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3078 if(this.line != oldLine)
3080 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3088 this.selLine = this.line;
3091 UpdateCaretPosition(false);
3098 bool SaveFile(char * fileName)
3100 File f = eFile_Open(fileName, FO_WRITE);
3104 eWindow_SetModified(false);
3105 eInstance_Delete(f);
3112 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3116 if(!style.multiLine)
3118 x = (active && this.active && !style.readOnly) ? line.count : 0;
3121 SetViewToCursor(true);
3126 if(!active && modified)
3129 if(!NotifyModified(master, this))
3132 *goOnWithActivation = false;
3139 if(timer) timer.Stop();
3141 mouseSelect = false;
3149 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3150 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3151 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3154 bool OnResizing(int *w, int *h)
3160 *w = space.h * 80 / 14;
3164 void OnResize(int w, int h)
3167 if(!hasHorzScroll && !hasVertScroll && viewLine)
3168 SetViewToCursor(true);
3170 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3172 SetViewToCursor(true);
3177 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3179 if(style.readOnly) return true;
3180 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3181 // Middle button already serves as a dragger as well.
3186 bool OnRightButtonDown(int x, int y, Modifiers mods)
3188 this.rightButtonDown = true;
3193 bool OnRightButtonUp(int x, int y, Modifiers mods)
3196 if(!parent.inactive && rightButtonDown)
3199 Menu contextMenu { };
3201 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3202 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3203 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3204 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3205 MenuDivider { contextMenu };
3206 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3208 popup = PopupMenu { master = this, menu = contextMenu,
3210 nonClient = true, interim = false, parent = parent,
3211 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3213 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3217 rightButtonDown = false;
3221 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3226 if(style.noSelect) return true;
3228 // Should we have a separate 'selectOnActivate' style?
3229 if(!mods.isActivate || (style.readOnly && style.multiLine))
3235 mouseX = mx - XOFFSET;
3238 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3240 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3246 else if(IsMouseOnSelection() && !mods.isActivate)
3256 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3258 if(mods.shift && !mods.isActivate)
3279 UpdateCaretPosition(true);
3280 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3281 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3285 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3289 mouseSelect = false;
3300 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3302 //PrintLn("MouseMove: ", x, ", ", y);
3308 mouseMove = IsMouseOnSelection();
3312 int size = SelSize();
3315 char * text = new char[size+1];
3319 GetSel(text, false);
3321 if(Max(selY, this.y) == dropY)
3325 if(this.dropX > this.selX)
3326 moveX = this.x - this.selX;
3330 if(this.dropX > this.x)
3331 moveX = this.selX - this.x;
3335 recordUndoEvent = true;
3337 this.dropX -= moveX;
3338 this.selX = this.x = this.dropX;
3339 this.selY = this.y = this.dropY;
3340 this.selLine = this.line = this.dropLine;
3342 recordUndoEvent = false;
3344 SetViewToCursor(true);
3349 byte * c = ((EditBox)this).multiLineContents, * c2;
3350 int l1 = c ? strlen(c) : 0, l2;
3353 c2 = ((EditBox)this).multiLineContents;
3354 l2 = c2 ? strlen(c2) : 0;
3355 if(l1 != l2 || (l1 && strcmp(c, c2)))
3385 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3387 //PrintLn("Dropped: ", x, ", ", y);
3389 NotifyDropped(master, this, x, y);
3393 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3394 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3398 bool OnMouseMove(int mx, int my, Modifiers mods)
3404 if(mods != -1 && mods.isSideEffect)
3409 if(style.noSelect) return true;
3410 if(wordSelect) return true;
3411 mouseX = mx - XOFFSET;
3414 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3416 if(this.mouseMove || this.mouseSelect)
3425 ((style.hScroll) || (style.vScroll)))
3432 DirtyLine(this.dropY);
3435 DirtyLine(this.dropY);
3436 this.dropLine = line;
3437 SetViewToCursor(true);
3439 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3442 else if(this.mouseSelect)
3444 DirtyLine(this.selY);
3451 SetViewToCursor(true);
3454 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3461 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3466 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3470 if(style.noSelect) return true;
3471 FindMouse(mx, my, &x, &y, &line, false);
3472 if(!NotifyDoubleClick(master, this, line, mods))
3479 for(c = x; c >= 0; c--)
3482 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3483 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3490 for(c = start; c<line.count; c += numBytes)
3492 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3493 if(!ch || !IS_ALUNDER(ch))
3503 this.line = this.selLine = line;
3504 this.wordSelect = (c != start);
3515 Menu fileMenu { menu, "File", F };
3516 saveDialog = fileDialog;
3517 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3518 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3520 if(style.autoSize) AutoSize();
3528 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3529 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3531 space.w = Max(space.w, 1);
3532 large.w = Max(large.w, 1);
3533 space.h = Max(space.h, 1);
3534 large.h = Max(large.h, 1);
3538 for(line = lines.first; line; line = line.next)
3539 ComputeLength(line);
3544 SetViewToCursor(true);
3552 bool OnLoadGraphics()
3554 FontExtent = Display::FontExtent;
3557 // UpdateCaretPosition(true);
3561 void OnUnloadGraphics()
3566 bool OnKeyHit(Key key, unichar ch)
3568 bool shift = (key.shift) ? true : false;
3570 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3572 if(!ch && !key.alt && !key.ctrl)
3574 key.code = (SmartKey)key.code;
3576 else if(!ch && key.alt)
3579 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3582 if(style.readOnly) break;
3583 if(style.stuckCaret) GoToEnd(true);
3584 if(!(style.freeCaret))
3586 this.x = Min(this.x, this.line.count);
3592 EditLine line = this.line;
3594 for(y = this.y; y>= 0; y--)
3596 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3598 // Slow down when going on lines...
3599 if(y != this.y) break;
3603 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3604 if(IS_ALUNDER(line.buffer[c]))
3609 if(!IS_ALUNDER(line.buffer[c]))
3611 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3626 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3627 this.x = this.selX = Min(c+1, line.count);
3628 this.y = this.selY = y;
3629 this.line = this.selLine = line;
3630 SetViewToCursor(true);
3641 if(style.readOnly) break;
3642 if(style.stuckCaret) break;
3651 if(this.line != this.selLine || this.x != this.selX)
3654 SetViewToCursor(true);
3659 EditLine line1 = this.line, line2 = this.line;
3660 int x1, y1 = this.y, x2, y2 = this.y;
3661 if(!style.freeCaret)
3663 this.selX = this.x = Min(this.x, this.line.count);
3668 if(x1 < line1.count)
3675 char * buffer = line1.buffer;
3676 for(i = x1; i < line1.count; i++)
3678 if(!IS_ALUNDER(buffer[i]))
3682 for(; i < line1.count; i++)
3684 // Delete trailing whitespace
3685 if(IS_ALUNDER(buffer[i]))
3695 // Avoid creating trailing spaces if there is no content on next line
3698 if(line2 && !line2.count)
3703 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3704 SetViewToCursor(true);
3713 if(!key.alt && !key.ctrl)
3717 bool stuffAfter = false;
3720 /*bool resetX = false;
3723 if(style.stuckCaret) GoToEnd(true);
3724 if(style.readOnly) break;
3725 if(!(style.multiLine)) break;
3727 for(c = 0; c<this.line.count && c<this.x; c++)
3729 if(this.line.buffer[c] == '\t')
3730 position += this.tabSize - (position % this.tabSize);
3731 else if(this.line.buffer[c] == ' ')
3737 // Prevent adding trailing spaces if at the head of a line
3738 /*if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3750 if(this.x < this.line.count)
3753 //If last character is a { indent one tab
3754 if(this.line.buffer[this.x - 1] == '{')
3756 //Except if the next non space character is a }
3757 bool indent = false;
3759 for(i = this.x; i < this.line.size; i++)
3760 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3762 if(this.line.buffer[i] != '}')
3767 position += this.tabSize;
3770 addString = new char[position + 2];
3771 addString[len++] = '\n';
3772 addString[len] = '\0';
3774 if(stuffAfter || !style.freeCaret)
3776 for(c = 0; c<position; )
3778 if(style.useTab && c + this.tabSize <= position)
3780 addString[len++] = '\t';
3785 addString[len++] = ' ';
3789 addString[len] = '\0';
3791 recordUndoEvent = true;
3794 EditLine prevLine = this.line.prev;
3797 // Nuke spaces if that is all that is left on previous line
3799 char * buffer = prevLine.buffer;
3800 for(i = 0; i < prevLine.count; i++)
3801 if(buffer[i] != ' ' && buffer[i] != '\t')
3803 if(i == prevLine.count)
3804 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3808 this.x = this.selX = backX;
3811 else */if(!stuffAfter && style.freeCaret)
3813 this.x = this.selX = position;
3817 SetViewToCursor(true);
3820 recordUndoEvent = false;
3828 if(style.stuckCaret) break;
3829 if(!(style.freeCaret))
3831 this.x = Min(this.x, this.line.count);
3832 this.selX = Min(this.selX, this.selLine.count);
3835 if(!shift) SelDirty();
3838 bool foundAlpha = false;
3841 EditLine line, lastLine;
3844 for(line = this.line; (line && !found); line = line.prev, y--)
3849 if(this.x == 0 && line != this.line)
3858 if(line == this.line) start = this.x -1; else start = line.count-1;
3859 start = Min(start, line.count-1);
3861 for(c = start; c >= 0;)
3864 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3882 byte ch = line.buffer[c];
3883 if(UTF8_IS_FIRST(ch)) break;
3887 // No next word found,
3888 if(!found && ( this.x > 0 || (!line.count && this.x)))
3902 this.line = lastLine;
3913 byte * buffer = line.buffer;
3916 byte ch = buffer[x];
3917 if(UTF8_IS_FIRST(ch)) break;
3934 if(!shift) _Deselect();
3935 SetViewToCursor(true);
3941 if(style.stuckCaret) break;
3942 if(!(style.freeCaret))
3944 this.x = Min(this.x, this.line.count);
3945 this.selX = Min(this.selX, this.selLine.count);
3948 if(!shift) SelDirty();
3949 if(!shift && (this.x != this.selX || this.y != this.selY));
3952 bool onAChar = false;
3953 if(this.selX != this.x || this.selY != this.y)
3955 if(this.x<this.line.count)
3956 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3958 if(key.shift && onAChar &&
3959 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3961 bool foundAlpha = false;
3963 EditLine line, lastLine;
3965 int lastC, lastY, lastNumBytes;
3967 for(line = this.line; (line && !found); line = line.next, y++)
3969 int start = (line == this.line) ? this.x : 0;
3973 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3979 lastNumBytes = numBytes;
3989 if(!found && (c != this.x || line != this.line))
4002 this.x = lastC + lastNumBytes;
4004 this.line = lastLine;
4011 bool foundAlpha = false;
4016 for(line = this.line; (line && !found); line = line.next, y++)
4018 int start = (line == this.line) ? this.x : 0;
4022 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4038 // No next word found,
4039 if(!found && (c != this.x || line != this.line))
4043 this.x = line.count;
4055 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4059 byte * buffer = line.buffer;
4062 byte ch = buffer[x];
4063 if(UTF8_IS_FIRST(ch)) break;
4084 if(!shift) _Deselect();
4085 SetViewToCursor(true);
4092 if(!style.vScroll || hasVertScroll) break;
4098 if(style.stuckCaret) break;
4100 if(!shift) SelDirty();
4110 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4114 if(!shift) _Deselect();
4116 SetViewToCursor(false);
4119 if(caretY == this.y * space.h)
4125 if(!style.freeCaret)
4126 this.x = Min(this.x, line.count);
4136 int sx = 0, sy = this.y * space.h;
4137 char * text = line.text;
4138 int maxW = clientSize.w - sx;
4139 display.FontExtent(font, " ", 1, null, &th);
4143 int startPos = textPos;
4146 bool lineComplete = false;
4148 if(!style.wrap && caretY == MAXINT)
4151 //textPos = line.count;
4152 //lineComplete = true;
4155 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4159 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4162 len = (nextSpace - (text + textPos));
4164 len = line.count - textPos;
4166 if(textPos < line.count)
4168 display.FontExtent(font, text + textPos, len, &w, null);
4170 if(nextSpace) { w += space.w; len++; }
4172 if(style.wrap && x + width + w > maxW && x > 0)
4174 lineComplete = true;
4184 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4188 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4191 if(this.x > line.count)
4194 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4195 len = this.x - startPos;
4196 display.FontExtent(font, text + startPos, len, &x, null);
4200 if(!shift) _Deselect();
4202 SetViewToCursor(false);
4206 if(sy == caretY - th || textPos >= line.count)
4208 if(textPos >= line.count)
4210 int c = textPos - 1;
4211 while(c > 0 && text[c] == ' ') c--;
4215 this.x = line.count;
4218 if(!shift) _Deselect();
4220 SetViewToCursor(false);
4225 } while(textPos < line.count);
4228 if(!shift) _Deselect();
4230 SetViewToCursor(false);
4239 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4240 if(!shift) SelDirty();
4241 this.line = this.line.prev;
4247 if(!shift) _Deselect();
4251 SetViewToCursor(false);
4257 return style.multiLine ? false : true;
4261 if(!style.vScroll || hasVertScroll)
4268 if(style.stuckCaret) break;
4272 int sx = 0, sy = this.y * this.space.h;
4273 int maxW = clientSize.w - sx;
4274 char * text = line.buffer;
4276 if(!shift) SelDirty();
4282 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4292 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4296 if(!shift) _Deselect();
4298 SetViewToCursor(false);
4301 while(!textPos || (style.freeCaret || textPos<line.count))
4303 int startPos = textPos;
4306 bool lineComplete = false;
4307 if(!style.wrap && sy <= caretY)
4309 textPos = line.count;
4310 lineComplete = true;
4312 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4316 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4319 len = (nextSpace - (text + textPos));
4321 len = line.count - textPos;
4323 if(textPos < line.count)
4325 display.FontExtent(font, text + textPos, len, &w, &th);
4327 if(nextSpace) { w += space.w; len++; }
4328 if(style.wrap && x + width + w > maxW && x > 0)
4330 lineComplete = true;
4340 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4344 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4347 if(this.x > line.count)
4350 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4352 len = this.x - startPos;
4353 display.FontExtent(font, text + startPos, len, &x, null);
4356 if(!shift) _Deselect();
4359 SetViewToCursor(false);
4365 this.x = line.count;
4368 if(!shift) _Deselect();
4371 SetViewToCursor(false);
4374 else if(textPos >= line.count && line.next)
4380 sy = this.y * this.space.h;
4381 sx = 0; //textBlock.startX;
4387 sx = 0; //textBlock.startX;
4395 this.x = Min(this.x, line.count);
4396 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4398 if(!shift) _Deselect();
4401 if(this.selX != this.x || this.selY != this.y)
4404 SetViewToCursor(false);
4411 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4412 if(!shift) SelDirty();
4413 this.line = this.line.next;
4417 if(!shift) _Deselect();
4420 if(this.selX != this.x || this.selY != this.y)
4428 return style.multiLine ? false : true;
4431 if(style.stuckCaret) break;
4432 if(!style.multiLine && key.ctrl) break;
4433 if(!(style.freeCaret))
4434 this.selX = Min(this.selX, this.selLine.count);
4436 if(!shift) SelDirty();
4439 this.line = this.lines.first;
4440 if(this.y != 0 || this.x != 0)
4450 EditLine line = this.line;
4452 for(c=0; line.buffer[c]; c++)
4453 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4455 if(shift && (c != 0 || this.x))
4464 if(shift && this.x != 0)
4470 if(!shift) _Deselect();
4471 SetViewToCursor(true);
4477 if(style.stuckCaret) break;
4478 if(!style.multiLine && key.ctrl) break;
4479 if(!style.freeCaret)
4480 this.selX = Min(this.selX, this.selLine.count);
4482 if(!shift) SelDirty();
4487 else if(this.x != this.line.count)
4489 this.x = this.line.count;
4494 if(!shift) _Deselect();
4495 SetViewToCursor(true);
4500 if(style.tabKey && !key.ctrl)
4502 if(this.selY != this.y && style.tabSel)
4504 EditLine firstLine, lastLine;
4508 // Do multi line selection tabbing here
4509 if(this.selY < this.y)
4511 firstLine = this.selLine;
4512 lastLine = this.line;
4518 // Selecting going up
4519 firstLine = this.line;
4520 lastLine = this.selLine;
4527 for(line = firstLine; line; line = line.next, y++)
4529 if(line != lastLine || x)
4533 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4535 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4537 if(line.buffer[c] == '\t')
4542 else if(line.buffer[c] != ' ')
4547 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4550 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4551 char * string = new char[len];
4552 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4553 GetText(string, line, y, 0, line, y, lastC, false, false);
4556 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4557 if(!line.AdjustBuffer(line.count-lastC))
4563 if(line == lastLine) break;
4568 for(line = firstLine; line; line = line.next, y++)
4572 if(line != lastLine || x)
4576 if(line.count + 1 <= this.maxLineSize)
4578 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4580 if(!line.AdjustBuffer(line.count+1))
4583 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4585 AddCharAction action { ch = '\t', x = 0, y = y };
4589 memmove(line.buffer+1,line.buffer,line.size-1);
4591 line.buffer[0] = '\t';
4596 if(line.count + this.tabSize <= this.maxLineSize)
4599 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4600 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4602 if(!line.AdjustBuffer(line.count+this.tabSize))
4606 char * string = new char[this.tabSize + 1];
4607 memset(string, ' ', this.tabSize);
4608 string[this.tabSize] = '\0';
4609 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4612 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4613 line.count+=this.tabSize;
4614 for(c=0; c<this.tabSize; c++)
4615 line.buffer[c] = ' ';
4621 if(line == lastLine) break;
4624 ComputeLength(maxLine);
4636 char * addString = new char[this.tabSize + 1];
4638 if(!(style.freeCaret))
4640 this.x = Min(this.x, this.line.count);
4644 start = Min(this.x, this.selX);
4645 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4647 addString[len++] = ' ';
4655 SetViewToCursor(true);
4664 if(!(style.hScroll) || hasHorzScroll) break;
4665 if(this.viewX < this.maxLength)
4667 //this.viewX+=this.space.w*this.tabSize;
4669 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4676 if(!shift) _Deselect();
4688 if(!(style.hScroll) || hasHorzScroll) break;
4691 //this.viewX-=this.space.w*this.tabSize;
4692 //this.viewX = Max(this.viewX,0);
4694 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4695 // SetCursorToView();
4702 if(!shift) _Deselect();
4717 if(!(style.readOnly))
4723 this.overwrite ^= 1;
4724 UpdateCaretPosition(true);
4729 NotifyOvrToggle(master, this, this.overwrite);
4740 if(style.noSelect) break;
4743 //Save current view position
4744 int tempX = this.viewX;
4745 int tempY = this.viewY;
4749 this.selLine = this.lines.first;
4750 this.y = this.lineCount-1;
4751 this.line = this.lines.last;
4752 this.x = this.line.count;
4755 SetViewToCursor(true);
4757 //Restore previous view position
4758 SetScrollPosition(tempX, tempY * this.space.h);
4764 // TOCHECK: Was there any good reason why we weren't returning false here?
4765 return false; // break;
4770 if(!(style.readOnly))
4778 if(style.readOnly) break;
4784 if(style.readOnly) break;
4789 if(style.readOnly) break;
4793 if(style.readOnly) break;
4797 if(style.readOnly) break;
4798 if(key.shift && key.code == rightBracket)
4800 //Only indent back if you are exactly at one tab.
4802 bool whitespace = true;
4808 EditLine line = this.line;
4810 //Only remove one tab if there is nothing else on the line.
4811 for(i = 0; i < this.line.count; i++)
4813 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4827 for(pos = line.count - 1; pos >= 0; pos--)
4829 char c = line.buffer[pos];
4833 indentwidth += this.tabSize;
4835 //Counting backwards, so when you find a character, indentation is reset
4845 //Place the } to get an undo:
4848 this.x = this.line.count;
4852 putsize = indentwidth;
4854 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4856 newline = new char[putsize+2];
4857 newline[putsize] = '}';
4858 newline[putsize+1] = '\0';
4862 for(; i < indentwidth / this.tabSize; i++)
4864 for(;i < putsize; i++)
4873 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4885 void OnHScroll(ScrollBarAction action, int position, Key key)
4888 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4890 this.viewX = position;
4891 if(action != setRange)
4893 if(!this.mouseMove && style.cursorFollowsView)
4900 void OnVScroll(ScrollBarAction action, int position, Key key)
4902 int oldViewY = this.viewY;
4905 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4907 position /= this.space.h;
4909 if(position < this.viewY)
4911 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4912 style.recomputeSyntax = true;
4914 else if(position > this.viewY)
4916 EditLine oldViewLine = viewLine;
4917 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4918 FigureStartSyntaxStates(oldViewLine, false);
4921 if(action != setRange)
4923 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4926 if(this.x != this.selX || this.y != this.selY)
4930 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4933 int numLines = clientSize.h / this.space.h;
4935 if(Abs(this.viewY - oldViewY) < numLines)
4938 if(this.viewY > oldViewY)
4940 for(y = oldViewY; y <this.viewY; y++)
4941 DirtyLine(y + numLines);
4945 for(y = this.viewY; y <oldViewY; y++)
4946 DirtyLine(y + numLines);
4951 // Fix dirt of stuff before first line...
4952 if(this.viewY - oldViewY > 0)
4954 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4961 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4966 ReplaceTextAction replaceAction = null;
4967 AddCharAction addCharAction = null;
4968 int addedSpaces = 0, addedTabs = 0;
4970 if(ch == '\r') return true;
4971 if(style.stuckCaret /*|EES_READONLY)*/ )
4974 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4976 if(!undoBuffer.dontRecord)
4978 if(selX != x || selY != y)
4983 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4984 oldString = new char[len];
4985 UTF32toUTF8Len(&ch, 1, buffer, 4);
4986 newString = CopyString(buffer);
4987 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4989 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4990 if(selY < y || (selY == y && selX < x))
4992 replaceAction.x1 = selX;
4993 replaceAction.y1 = selY;
4994 replaceAction.x2 = x;
4995 replaceAction.y2 = y;
4999 replaceAction.x1 = x;
5000 replaceAction.y1 = y;
5001 replaceAction.x2 = selX;
5002 replaceAction.y2 = selY;
5004 Record(replaceAction);
5005 undoBuffer.dontRecord++;
5009 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5010 Record(addCharAction);
5016 DelSel(&addedSpaces);
5017 if(this.lineCount+1 > this.maxLines)
5020 Emptyline(this.lines.first,0);
5024 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5026 // Make sure it fits, but we need a default line is this.font is too big for window
5027 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5030 if((this.y >= 0) && this.y < this.viewY)
5035 line = EditLine { };
5038 lines.Insert(this.line, line);
5039 line.editBox = this;
5045 // If we're displacing the lines from a current line ...
5048 if(this.line.buffer)
5051 if(this.line.count < endX) endX = this.line.count;
5052 length = this.line.count - endX;
5055 if(!line.AdjustBuffer(length))
5059 if(this.line.buffer)
5061 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5063 if(endX > 4000 || endX < 0)
5066 this.line.count = endX;
5067 this.line.buffer[this.line.count] = '\0';
5068 this.line.AdjustBuffer(this.line.count);
5069 ComputeLength(this.line);
5073 BufferLocation before = { this.line, this.y, this.x }, after;
5078 line.count = length;
5081 if(length > 4000 || length < 0)
5084 ComputeLength(this.line);
5089 line.buffer[line.count] = '\0';
5092 after.line = this.line, after.y = this.y, after.x = this.x;
5094 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5100 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5101 DelSel(&addedSpaces);
5102 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5103 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5104 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5108 this.selLine = this.line;
5112 replaceAction.x3 = x;
5113 replaceAction.y3 = y;
5114 replaceAction.addedSpaces = addedSpaces;
5115 replaceAction.addedTabs = addedTabs;
5116 undoBuffer.dontRecord--;
5120 addCharAction.x -= addedTabs * (tabSize-1);
5121 addCharAction.addedSpaces = addedSpaces;
5122 addCharAction.addedTabs = addedTabs;
5129 /****************************************************************************
5131 ****************************************************************************/
5134 bool AddCh(unichar ch)
5136 return _AddCh(ch, null, null);
5141 this.modified = true;
5142 NotifyUpdate(master, this);
5143 modifiedDocument = true;
5146 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5149 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
5150 SetViewToCursor(true);
5158 itemEditUndo.disabled = undoBuffer.curAction == 0;
5159 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5160 if(savedAction == undoBuffer.curAction)
5162 modifiedDocument = false;
5164 NotifyUnsetModified(master, this);
5171 itemEditUndo.disabled = undoBuffer.curAction == 0;
5172 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5173 if(savedAction == undoBuffer.curAction)
5175 modifiedDocument = false;
5177 NotifyUnsetModified(master, this);
5181 void Record(UndoAction action)
5183 if(!undoBuffer.dontRecord)
5185 undoBuffer.Record(action);
5186 itemEditUndo.disabled = undoBuffer.curAction == 0;
5187 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5194 this.lines.first, 0,0,
5195 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5198 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5203 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5204 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5205 this.y = line2 ? y2 : (this.lineCount-1);
5206 this.line = line2 ? (EditLine)line2 : this.lines.last;
5209 SetViewToCursor(true);
5213 // TODO: Fix this vs modifiedDocument window property
5214 void SetModified(bool flag)
5218 this.modified = false;
5219 if(flag && !NotifyModified(master, this))
5220 this.modified = true;
5225 bool AddS(char * string)
5232 int addedSpaces = 0, addedTabs = 0;
5233 AddTextAction action = null;
5234 ReplaceTextAction replaceAction = null;
5236 this.pasteOperation = true;
5238 if(style.stuckCaret /*|EES_READONLY)*/ )
5241 if(!undoBuffer.dontRecord)
5243 char * placeString = CopyString(string);
5244 if(!style.multiLine)
5248 for(i = 0; (ch = placeString[i]); i++)
5251 placeString[i] = '\0';
5252 placeString = renew placeString byte[i+1];
5257 if(selX != x || selY != y)
5261 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5262 oldString = new char[len];
5263 newString = placeString;
5264 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5266 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5267 if(selY < y || (selY == y && selX < x))
5269 replaceAction.x1 = selX;
5270 replaceAction.y1 = selY;
5271 replaceAction.x2 = x;
5272 replaceAction.y2 = y;
5276 replaceAction.x1 = x;
5277 replaceAction.y1 = y;
5278 replaceAction.x2 = selX;
5279 replaceAction.y2 = selY;
5281 Record(replaceAction);
5285 if(string[0] == '\n')
5286 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5288 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5296 undoBuffer.dontRecord++;
5297 DelSel(&addedSpaces);
5301 for(c = 0; string[c]; c++)
5303 if(string[c] == '\n' || string[c] == '\r')
5305 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5310 if(string[c] == '\n')
5319 // Reset for next line
5323 if(string[c] == '\r' && *line == '\n')
5335 // Why was this here?
5338 // Add the line here
5340 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5346 undoBuffer.dontRecord--;
5351 action.addedSpaces = addedSpaces;
5352 action.addedTabs = addedTabs;
5354 else if(replaceAction)
5356 replaceAction.y3 = y;
5357 replaceAction.x3 = x;
5358 replaceAction.addedSpaces = addedSpaces;
5359 replaceAction.addedTabs = addedTabs;
5362 UpdateCaretPosition(true);
5364 this.pasteOperation = false;
5377 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5378 SetViewToCursor(true);
5384 void PutCh(unichar ch)
5388 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5389 //if((ch >= 32) || ch == '\n')
5391 int addedSpaces = 0, addedTabs = 0;
5392 ReplaceTextAction replaceAction = null;
5393 AddCharAction addCharAction = null;
5396 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5398 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5403 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5404 oldString = new char[len];
5405 UTF32toUTF8Len(&ch, 1, buffer, 4);
5406 newString = CopyString(buffer);
5407 GetText(oldString, line, y, x, line, y, x+1, false, false);
5408 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5409 replaceAction.x1 = x;
5410 replaceAction.y1 = y;
5411 replaceAction.x2 = x+1;
5412 replaceAction.y2 = y;
5413 Record(replaceAction);
5415 undoBuffer.dontRecord++;
5416 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5417 undoBuffer.dontRecord--;
5419 else if(!undoBuffer.dontRecord)
5421 if(selX != x || selY != y)
5426 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5427 oldString = new char[len];
5428 UTF32toUTF8Len(&ch, 1, buffer, 4);
5429 newString = CopyString(buffer);
5430 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5431 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5432 if(selY < y || (selY == y && selX < x))
5434 replaceAction.x1 = selX;
5435 replaceAction.y1 = selY;
5436 replaceAction.x2 = x;
5437 replaceAction.y2 = y;
5441 replaceAction.x1 = x;
5442 replaceAction.y1 = y;
5443 replaceAction.x2 = selX;
5444 replaceAction.y2 = selY;
5446 Record(replaceAction);
5450 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5451 Record(addCharAction);
5454 undoBuffer.dontRecord++;
5455 result = _AddCh(ch, &addedSpaces, &addedTabs);
5458 replaceAction.x3 = x;
5459 replaceAction.y3 = y;
5460 replaceAction.addedSpaces = addedSpaces;
5461 replaceAction.addedTabs = addedTabs;
5465 addCharAction.x -= addedTabs * (tabSize-1);
5466 addCharAction.addedSpaces = addedSpaces;
5467 addCharAction.addedTabs = addedTabs;
5469 undoBuffer.dontRecord--;
5473 if(result) SetViewToCursor(true);
5477 void PutS(char * string)
5482 SetViewToCursor(true);
5487 void Printf(char * format, ...)
5491 char temp[MAX_F_STRING];
5493 va_start(args, format);
5494 vsnprintf(temp, sizeof(temp), format, args);
5495 temp[sizeof(temp)-1] = 0;
5501 void SetContents(char * format, ...)
5505 undoBuffer.dontRecord++;
5507 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5510 char temp[MAX_F_STRING];
5512 va_start(args, format);
5513 vsnprintf(temp, sizeof(temp), format, args);
5514 temp[sizeof(temp)-1] = 0;
5521 undoBuffer.dontRecord--;
5534 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5537 else if(this.line.prev)
5539 EditLine line = this.line.prev;
5543 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5551 this.selLine = this.line;
5556 SetViewToCursor(true);
5561 Emptyline(this.line,this.y);
5562 this.selX = this.x = 0;
5565 this.selLine = this.line;
5567 SetViewToCursor(true);
5577 SetViewToCursor(true);
5585 SetViewToCursor(true);
5589 bool GoToLineNum(int lineNum)
5594 EditLine line = this.lines.first;
5595 for(c = 0; c < lineNum && line; c++, line = line.next);
5605 SetViewToCursor(true);
5612 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5613 bool GoToPosition(EditLine line, int y, int x)
5625 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5639 SetViewToCursor(true);
5646 void SetViewToCursor(bool setCaret)
5656 bool dontScroll = false;
5662 selected = selX != this.x || selY != y;
5669 checkLine = dropLine;
5675 checkLine = this.line;
5680 numLines = clientSize.h / space.h;
5682 // This is broken. The EditBox now doesn't do anything different when adding to it,
5683 // regardless of the previous scrolling position. It should be read and then set again
5684 // if one wishes to preserve it.
5685 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5686 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5690 // Horizontal Adjustment
5691 if(!dontScroll && checkLine)
5695 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5698 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5704 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5705 viewX = x - clientSize.w+space.w;
5706 if(x < this.viewX + clientSize.w/2 - space.w)
5707 viewX = Max(0, x - clientSize.w/2 + space.w);
5715 // Vertical Adjustment
5716 if(viewY > checkY) viewY = checkY;
5717 if(viewY + numLines <= checkY)
5719 if(clientSize.h >= space.h)
5721 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5731 for(;dropLine && dropLine.prev && dropY >= numLines;)
5733 dropLine = dropLine.prev;
5737 for(;this.line && this.line.prev && this.y >= numLines;)
5739 this.line = this.line.prev;
5744 SetScrollPosition(viewX, viewY * this.space.h);
5746 UpdateCaretPosition(setCaret);
5752 selLine = this.line;
5762 void CenterOnCursor()
5764 int numLines = clientSize.h / this.space.h;
5765 int y = this.y - numLines / 2;
5767 bool figureSyntax = false;
5768 EditLine oldViewLine = viewLine;
5769 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5774 for(;y < this.viewY; y++)
5776 this.viewLine = this.viewLine.prev;
5777 style.recomputeSyntax = true;
5779 for(;y > this.viewY; y--)
5781 this.viewLine = this.viewLine.next;
5782 figureSyntax = true;
5785 FigureStartSyntaxStates(oldViewLine, false);
5789 SetScrollPosition(this.viewX, viewY * this.space.h);
5790 UpdateCaretPosition(true);
5794 void SetCursorToView()
5805 numLines = clientSize.h / this.space.h;
5809 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5810 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5814 EditLine oldLine = this.line;
5815 bool lastOne = false;
5816 EditLine oldViewLine = this.viewLine;
5817 bool figureSyntax = false;
5819 if(this.y >= this.lineCount-1) return;
5821 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5823 if(this.viewY + numLines < this.lines.count)
5825 this.viewLine = this.viewLine.next;
5827 figureSyntax = true;
5829 else if(c && !lastOne)
5838 FigureStartSyntaxStates(oldViewLine, false);
5839 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5842 SetViewToCursor(false);
5851 if(this.y == 0) return;
5853 numLines = clientSize.h / this.space.h;
5857 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5858 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5862 EditLine oldLine = this.line;
5864 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5869 if(this.viewLine.prev)
5871 this.viewLine = this.viewLine.prev;
5873 style.recomputeSyntax = true;
5877 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5880 SetViewToCursor(false);
5887 if(this.viewLine.prev)
5890 // this.viewLine = this.viewLine.prev;
5893 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5900 if(this.viewLine.next)
5903 // this.viewLine = this.viewLine.next;
5906 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5913 EditLine l1, l2, line;
5914 int x1, x2, nx1, nx2;
5919 if(!this.selLine) return 0;
5920 if(this.selLine == this.line && this.selX == this.x) return 0;
5921 if(this.selY < this.y)
5928 else if(this.selY > this.y)
5935 else if(this.selX < this.x)
5937 l1 = l2 = this.line;
5943 l1 = l2 = this.line;
5947 nx1 = Min(x1,l1.count);
5948 nx2 = Min(x2,l2.count);
5950 // Find Number of Bytes Needed
5952 for(line = l1; line; line = line.next)
5954 if(line == l1) start = nx1; else start = 0;
5955 if(line == l2) end = nx2; else end = line.count;
5957 if(style.freeCaret && line == l2)
5960 count = Max(x2-Max(x1,l1.count),0);
5962 count = Max(x2-l2.count,0);
5966 if(line == l2) break;
5967 // Add Carriage Return / line Feed
5974 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5978 if(!reorder || this.selY < this.y)
5980 if(l1) *l1 = this.selLine;
5981 if(y1) *y1 = this.selY;
5982 if(x1) *x1 = this.selX;
5983 if(l2) *l2 = this.line;
5984 if(y2) *y2 = this.y;
5985 if(x2) *x2 = this.x;
5987 else if(this.selY > this.y)
5989 if(l1) *l1 = this.line;
5990 if(y1) *y1 = this.y;
5991 if(x1) *x1 = this.x;
5992 if(l2) *l2 = this.selLine;
5993 if(y2) *y2 = this.selY;
5994 if(x2) *x2 = this.selX;
5996 else if(this.selX < this.x)
5998 if(l1) *l1 = this.line;
5999 if(y1) *y1 = this.selY;
6000 if(x1) *x1 = this.selX;
6001 if(l2) *l2 = this.line;
6002 if(y2) *y2 = this.y;
6003 if(x2) *x2 = this.x;
6007 if(l1) *l1 = this.line;
6008 if(y1) *y1 = this.y;
6009 if(x1) *x1 = this.x;
6010 if(l2) *l2 = this.line;
6011 if(y2) *y2 = this.selY;
6012 if(x2) *x2 = this.selX;
6017 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
6019 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
6021 this.selLine = (EditLine)l1;
6024 this.line = (EditLine)l2;
6028 SetViewToCursor(true);
6032 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6034 EditLine l1, l2, line;
6035 int x1, x2, nx1, nx2;
6066 nx1 = Min(x1,l1.count);
6067 nx2 = Min(x2,l2.count);
6070 for(line = l1; line; line = line.next)
6072 if(line == l1) start = nx1; else start = 0;
6073 if(line == l2) end = nx2; else end = line.count;
6076 CopyBytes(text, line.buffer + start, end - start);
6079 numChars += end-start;
6081 if(style.freeCaret && line == l2 && addSpaces)
6084 count = Max(x2-Max(x1,l1.count),0);
6086 count = Max(x2-l2.count,0);
6089 FillBytes(text,' ',count);
6095 if(line == l2) break;
6107 // '\0' terminate Terminate
6114 void GetSel(char * text, bool addCr)
6116 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6119 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6137 int size = SelSize();
6140 // Try to allocate memory
6141 ClipBoard clipBoard { };
6142 if(clipBoard.Allocate(size+1))
6144 GetSel(clipBoard.memory, true);
6157 ClipBoard clipBoard { };
6158 if(clipBoard.Load())
6159 PutS(clipBoard.memory);
6171 SetViewToCursor(true);
6177 void DeleteSelection()
6182 SetViewToCursor(true);
6188 void Save(File f, bool cr)
6191 savedAction = undoBuffer.curAction;
6193 for(line = this.lines.first; line; line = line.next)
6195 f.Write(line.buffer, line.count, 1);
6198 if(cr) f.Putc('\r');
6204 #define BUFFER_SIZE 16384
6207 undoBuffer.dontRecord++;
6210 char buffer[BUFFER_SIZE];
6214 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6215 buffer[count] = '\0';
6221 undoBuffer.dontRecord--;
6222 undoBuffer.count = 0;
6223 undoBuffer.curAction = 0;
6224 itemEditUndo.disabled = undoBuffer.curAction == 0;
6225 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6228 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6232 bool firstPass = true;
6233 EditBoxFindResult result = found;
6235 if(!this.line) return notFound;
6238 for(line = this.line;;)
6246 line = this.lines.first;
6252 line = this.lines.last;
6253 num = this.lineCount - 1;
6259 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6261 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6265 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6268 if(line == this.line && !firstPass) break;
6286 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6290 int searchLen = strlen(text);
6291 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6293 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6294 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6296 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6303 bool OnKeyDown(Key key, unichar ch)
6306 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6308 if(!NotifyKeyDown(master, this, key, ch))
6318 this.mouseMove = false;
6319 OnLeftButtonUp(0,0,0);
6320 SetViewToCursor(true);
6330 public class EditBoxStream : File
6333 BufferLocation start, sel;
6340 EditBox editBox = this.editBox;
6342 editBox.x = start.x;
6343 editBox.y = start.y;
6344 editBox.line = start.line;
6346 editBox.selX = sel.x;
6347 editBox.selY = sel.y;
6348 editBox.selLine = sel.line;
6350 editBox.SetViewToCursor(true);
6351 //editBox.ComputeColumn();
6355 property EditBox editBox
6363 start.line = value.line;
6367 sel.line = value.selLine;
6370 value.GoToHome(true);
6373 get { return editBox; }
6376 uint Read(byte * buffer, uint size, uint count)
6379 EditBox editBox = this.editBox;
6380 EditLine line = editBox.line;
6386 for(;read < count && line; line = (*&line.next))
6388 int numBytes = Min(count - read, (*&line.count) - x);
6391 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6396 for(;read < count && x < (*&line.count);)
6398 buffer[read++] = (*&line.buffer)[x++];
6405 buffer[read++] = '\n';
6410 if(x == (*&line.count) && (*&line.next))
6419 editBox.line = editBox.selLine = line;
6420 editBox.x = editBox.selX = x;
6421 editBox.y = editBox.selY = y;
6426 bool Seek(int pos, FileSeekMode mode)
6429 EditBox editBox = this.editBox;
6430 EditLine line = editBox.line;
6432 if(mode == FileSeekMode::start)
6434 pos = pos - this.pos;
6444 for(;read < pos && line; line = (*&line.next))
6446 int numBytes = Min(pos - read, (*&line.count) - x);
6449 read += numBytes; x += numBytes;
6452 /*for(;read < pos && x < (*&line.count);)
6480 for(;read < pos && line; line = (*&line.prev))
6482 int numBytes = Min(pos - read, x);
6488 /*for(;read < pos && x > 0;)
6498 x = (*&(*&line.prev).count);
6514 editBox.line = editBox.selLine = line;
6515 editBox.x = editBox.selX = x;
6516 editBox.y = editBox.selY = y;
6521 bool Puts(char * string)
6523 EditBox editBox = this.editBox;
6524 BufferLocation start { editBox.line, editBox.y, editBox.x };
6528 editBox.AddS(string);
6530 pos.line = editBox.line;
6534 this.start.AdjustAdd(start, pos);
6535 sel.AdjustAdd(start, pos);
6540 // NOTE: BYTE, NOT UNICODE CHARACTER!
6543 EditBox editBox = this.editBox;
6544 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6549 utf8Bytes[numBytes++] = ch;
6550 utf8Bytes[numBytes] = 0;
6551 if(UTF8Validate(utf8Bytes))
6553 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6555 pos.line = editBox.line;
6558 this.start.AdjustAdd(start, pos);
6559 sel.AdjustAdd(start, pos);
6566 bool Getc(char * ch)
6568 return Read(ch, 1, 1) ? true : false;
6571 void DeleteBytes(uint count)
6573 EditBox editBox = this.editBox;
6576 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6577 BufferLocation end = pos;
6582 for(;c < count && end.line && end.x < end.line.count;)
6587 if(c < count && end.line && end.line.next)
6589 end.line = end.line.next;
6598 start.AdjustDelete(pos, end);
6599 sel.AdjustDelete(pos, end);
6601 editBox._DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true, false, null);