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;
182 virtual void Undo(void * data) { type.Undo(this, data); }
183 virtual void Redo(void * data) { type.Redo(this, data); }
185 virtual void Print(void * data) { type.Print(this, data); }
189 if(((Class)type).Destructor)
190 ((void (*)(void *))((Class)type).Destructor)(this);
194 public class UndoBuffer
196 Array<UndoAction> actions { size = 8 };
216 UndoAction action = actions[--curAction];
218 /*Print("Undoing: ");
219 action.Print(data);*/
230 if(curAction < count)
232 UndoAction action = actions[curAction];
235 /*Print("Redoing: ");
236 action.Print(data);*/
244 void Record(UndoAction action)
246 if(!dontRecord && !insideRedo)
248 if(curAction < count)
251 for(c = curAction; c < count; c++)
257 if(count >= actions.size)
258 actions.size += actions.size / 2;
261 /*Print("Recording: ");
262 action.Print(data);*/
264 actions[count++] = action;
267 if(actions.size > count + count / 2 && count + count / 2 >= 8)
268 actions.size = count + count / 2;
275 static class AddCharAction : UndoAction
279 int addedSpaces, addedTabs;
280 type = class(AddCharAction);
282 void Undo(EditBox editBox)
284 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
286 if(addedTabs || addedSpaces)
287 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
288 editBox.UpdateDirty();
291 void Redo(EditBox editBox)
293 editBox.GoToPosition(null, y, x);
295 editBox.UpdateDirty();
298 void Print(EditBox editBox)
300 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
305 static class AddTextAction : UndoAction
309 int addedSpaces, addedTabs;
310 type = class(AddTextAction);
313 void Print(EditBox editBox)
315 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
323 void Undo(EditBox editBox)
328 editBox.GoToPosition(null, y1, x1);
330 l2 = editBox.lines.first;
331 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
333 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
334 if(addedTabs || addedSpaces)
335 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
337 editBox.SetViewToCursor(true);
341 void Redo(EditBox editBox)
343 editBox.GoToPosition(null, y1, x1);
344 editBox.PutS(string);
348 static class DelTextAction : UndoAction
352 bool placeAfter, noHighlight;
354 type = class(DelTextAction);
357 void Print(EditBox editBox)
359 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
362 void Undo(EditBox editBox)
364 editBox.GoToPosition(null, y1, x1);
365 editBox.PutS(string);
369 editBox.GoToPosition(null, y1, x1);
375 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
376 //editBox.SetViewToCursor(true);
379 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
388 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
389 //editBox.SetViewToCursor(true);
392 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
396 void Redo(EditBox editBox)
401 editBox.GoToPosition(null, y1, x1);
404 l2 = editBox.lines.first;
405 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
407 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
408 editBox.SetViewToCursor(true);
418 static class ReplaceTextAction : UndoAction
420 int y1, x1, y2, x2, y3, x3;
424 int addedSpaces, addedTabs;
426 type = class(ReplaceTextAction);
429 void Print(EditBox editBox)
431 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
434 void Undo(EditBox editBox)
439 editBox.GoToPosition(null, y1, x1);
441 l3 = editBox.lines.first;
442 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
444 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
446 editBox.PutS(oldString);
447 if(addedSpaces || addedTabs)
448 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
450 //editBox.PutS(oldString);
453 editBox.GoToPosition(null, y1, x1);
456 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
462 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
466 void Redo(EditBox editBox)
471 editBox.GoToPosition(null, y1, x1);
473 l2 = editBox.lines.first;
474 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
476 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
478 editBox.PutS(newString);
488 static class MoveTextAction : UndoAction
490 int fy1, fx1, fy2, fx2;
492 type = class(MoveTextAction);
494 void Undo(EditBox editBox)
499 void Redo(EditBox editBox)
505 public class EditLine : struct
517 // Only works for single line edit for now...
518 EditBox editBox = this.editBox;
522 get { return this ? buffer : null; }
524 property EditLine prev { get { return this ? prev : null; } };
525 property EditLine next { get { return this ? next : null; } };
526 property int count { get { return this ? count : 0; } };
533 // This makes sure the buffer always contains at least count characters
534 // Keeps a count/2 pad for avoiding always reallocating memory.
535 bool AdjustBuffer(int count)
543 newSize = (count + (count >> 1));
545 // Shrink down the buffer
548 buffer = new char[newSize];
549 if(!buffer) return false;
553 CopyBytes(buffer, this.buffer, count);
556 this.buffer = buffer;
560 // Increase the buffer
561 else if(size < count)
563 buffer = new char[newSize];
564 if(!buffer) return false;
568 CopyBytes(buffer, this.buffer, this.count + 1); // size);
571 this.buffer = buffer;
580 public struct BufferLocation
585 void AdjustDelete(BufferLocation start, BufferLocation end)
587 // Location is before, nothing to do
588 if(y < start.y || (y == start.y && x < start.x))
590 // Location is inside deleted bytes, point to the start
591 if((y >= start.y && (y > start.y || x >= start.x)) &&
592 (y >= end.y && (y > end.y || x >= end.x)))
597 // Location is on another line
599 y -= end.y - start.y;
600 // Location is the last touched line
607 //if(start.line == end.line)
608 x -= end.x - start.x;
617 // Assuming no carriage return before first character ???? fixed?
618 void AdjustAdd(BufferLocation start, BufferLocation end)
620 int numLines = end.y - start.y;
627 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
630 for(c = 0, line = start.line; c<numLines; c++)
633 //x += numLines ? end.x : (end.x - start.x);
634 x += end.x - start.x;
641 public enum EditBoxFindResult { notFound, found, wrapped };
643 static char * keyWords1[] =
646 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
647 "void", "char","int","float","double","signed","unsigned","static", "extern", "struct", "union", "typedef","enum",
649 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
650 "__attribute__", "__stdcall", "_stdcall",
651 "__declspec", "goto",
652 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
653 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
656 "class", "private", "public",
658 "delete", "new", "new0", "renew", "renew0", "define",
661 "dllexport", "dllimport", "stdcall",
662 "subclass", "__on_register_module", "namespace", "using",
663 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
664 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
665 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
668 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
671 "this", "true", "false", "null", "value",
680 static char * keyWords2[] =
682 "defined", "warning", null
685 static char ** keyWords[] = { keyWords1, keyWords2 };
686 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
687 //static int * keyLen[NUM_KEYWORD_GROUPS];
688 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
690 static char searchString[1025], replaceString[1025];
691 static bool matchCase = false, wholeWord = false, searchUp = false;
693 static GoToDialog goToDialog
695 autoCreate = false, isModal = true, text = $"Go To"
698 public class EditBox : CommonControl
700 class_property(icon) = "<:ecere>controls/editBox.png";
704 virtual bool Window::NotifyModified(EditBox editBox);
705 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
706 virtual void Window::NotifyUpdate(EditBox editBox);
707 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
708 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
709 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
711 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
712 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
713 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
715 virtual bool Window::NotifyUnsetModified(EditBox editBox);
717 // Why was this commented out?
718 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
719 // and the scrollArea property isn't compared properly either.
723 if(font) ComputeFont();
724 SetInitSize(initSize);
730 style.vScroll = true;
736 style.hScroll = true;
740 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
741 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
742 property bool readOnly
744 property_category $"Behavior"
747 style.readOnly = value;
748 itemEditCut.disabled = value || !selection;
749 itemEditDelete.disabled = value || !selection;
750 itemEditPaste.disabled = value;
752 get { return style.readOnly; }
754 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
755 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
756 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
757 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
758 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
759 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
760 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
761 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
762 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
763 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
764 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
765 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
766 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
767 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
768 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
769 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
770 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
771 property int numLines { get { return this ? lineCount : 0; } };
772 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
773 property int column { get { return col; } }; // TODO: Add Set
774 property int charPos { get { return x; } }; // TODO: Add Set
775 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
776 property EditLine lastLine { get { return lines.last; } };
777 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
778 property char * contents
780 property_category $"Data"
785 undoBuffer.dontRecord++;
787 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
790 //SetViewToCursor(true);
793 undoBuffer.dontRecord--;
799 char * buffer = null;
802 /* Can't implement this right now because of memory leak... Need string reference counting...
809 for(line = lines.first; line; line = line.next)
810 len += strlen(line.buffer);
812 buffer = new char[len+1];
814 for(line = lines.first; line; line = line.next)
816 int lineLen = strlen(line.buffer);
817 memcpy(buffer + len, line.buffer, lineLen);
823 buffer = this.line ? this.line.buffer : null;
828 property bool overwrite { get { return overwrite; } };
829 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
831 property char * multiLineContents
835 char * buffer = null;
842 for(line = lines.first; line; line = line.next)
843 len += strlen(line.buffer)+1;
845 buffer = new char[len+1];
847 for(line = lines.first; line; line = line.next)
849 int lineLen = strlen(line.buffer);
850 memcpy(buffer + len, line.buffer, lineLen);
852 if(line.next) buffer[len++] = '\n';
865 return this.line.buffer;
870 void SetLineText(char * text)
874 EditLine_SetText(this.line, text);
878 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
879 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
880 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
882 // selectionStart.line, selectionStart.column (With Set)
883 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
897 // Position of Caret (Not necessarily displayed position)
900 // Position of beginning of block (Block ends at (x,y))
902 // line is line at carret, selLine is line at beginning of block
903 EditLine line, selLine, dropLine;
904 // Mouse selection Moving virtual caret
909 // ViewX is x offset in pixels, ViewY is y offset in lines
911 // viewLine is first displayed line
914 // start and end of area to redraw
917 // MaxLine is the longest line
919 // MaxLength is the longest line's length
922 // MouseSelect is true once button is pressed, overwrite is true if mode is on
923 bool mouseSelect, mouseMove, overwrite, wordSelect;
924 // Timer is used for mouse selection scrolling
927 window = this, delay = 0.1;
936 OnMouseMove(mouseX, mouseY, -1);
941 // (mouseX,mouseY) is the position of the mouse in the edit box
946 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
949 bool rightButtonDown;
952 UndoBuffer undoBuffer { data = this };
954 ColorAlpha selectionColor, selectionText;
955 SyntaxColorScheme colorScheme { };
960 Menu editMenu { menu, $"Edit", e };
963 editMenu, $"Cut\tCtrl+X", t, disabled = true;
965 bool NotifySelect(MenuItem item, Modifiers mods)
967 if(!(style.readOnly))
972 MenuItem itemEditCopy
974 editMenu, $"Copy\tCtrl+C", c, disabled = true;
976 bool NotifySelect(MenuItem item, Modifiers mods)
982 MenuItem itemEditPaste
984 editMenu, $"Paste\tCtrl+V", p;
986 bool NotifySelect(MenuItem item, Modifiers mods)
988 if(!(style.readOnly))
993 MenuItem itemEditDelete
995 editMenu, $"Delete\tDel", d, disabled = true;
997 bool NotifySelect(MenuItem item, Modifiers mods)
999 if(!(style.readOnly))
1004 MenuDivider { editMenu };
1005 MenuItem itemEditSelectAll
1007 editMenu, $"Select All\tCtrl+A", a;
1009 bool NotifySelect(MenuItem item, Modifiers mods)
1015 MenuDivider { editMenu };
1016 MenuItem itemEditUndo
1018 editMenu, $"Undo\tCtrl+Z", u;
1021 bool NotifySelect(MenuItem item, Modifiers mods)
1027 MenuItem itemEditRedo
1029 editMenu, $"Redo\tCtrl+Y", o;
1032 bool NotifySelect(MenuItem item, Modifiers mods)
1038 MenuDivider { editMenu };
1041 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1043 bool NotifySelect(MenuItem item, Modifiers mods)
1046 Find(searchString, wholeWord, matchCase, false);
1048 itemEditFind.NotifySelect(this, item, mods);
1054 editMenu, $"Find Next\tF3", n, f3;
1056 bool NotifySelect(MenuItem item, Modifiers mods)
1059 Find(searchString, wholeWord, matchCase, true);
1061 itemEditFind.NotifySelect(this, item, mods);
1065 MenuItem itemEditFind
1067 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1069 bool NotifySelect(MenuItem item, Modifiers mods)
1073 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1074 searchUp = searchUp;
1077 // Fix dialog from above shouldn't be visible
1078 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1079 void NotifyDestroyed(Window window, DialogResult result)
1081 FindDialog dialog = (FindDialog) window;
1082 searchUp = dialog.searchUp;
1083 strcpy(searchString, dialog.searchString);
1084 matchCase = dialog.matchCase;
1085 wholeWord = dialog.wholeWord;
1094 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1096 bool NotifySelect(MenuItem item, Modifiers mods)
1098 ReplaceDialog dialog
1102 searchString = searchString,
1103 replaceString = replaceString,
1104 matchCase = matchCase,
1105 wholeWord = wholeWord,
1109 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1110 void NotifyDestroyed(Window window, DialogResult result)
1112 ReplaceDialog dialog = (ReplaceDialog)window;
1113 char * replace = dialog.replaceString;
1115 strcpy(replaceString, replace);
1116 strcpy(searchString, dialog.searchString);
1117 matchCase = dialog.matchCase;
1118 wholeWord = dialog.wholeWord;
1125 MenuDivider { editMenu };
1128 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1130 bool NotifySelect(MenuItem item, Modifiers mods)
1132 goToDialog.editBox = this;
1133 goToDialog.master = master;
1134 goToDialog.Create();
1138 MenuDivider { editMenu };
1139 MenuItem itemEditInsertTab
1141 editMenu, $"Insert Tabs", i, checkable = true;
1143 bool NotifySelect(MenuItem item, Modifiers mods)
1145 style.useTab = item.checked;
1151 snapVertScroll = true;
1152 snapHorzScroll = true;
1156 static bool syntaxInit = false;
1161 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1163 for(c = 0; keyWords[g][c]; c++);
1164 //keyLen[g] = new int[c];
1165 for(c = 0; keyWords[g][c]; c++)
1167 keyLen[g][c] = strlen(keyWords[g][c]);
1171 colorScheme.commentColor = dimGray;
1172 colorScheme.charLiteralColor = crimson;
1173 colorScheme.stringLiteralColor = crimson;
1174 colorScheme.preprocessorColor = green;
1175 colorScheme.numberColor = teal;
1176 colorScheme.keywordColors = [ blue, blue ];
1179 FontExtent = Display::FontExtent;
1181 lines.offset = (uint)&((EditLine)0).prev;
1183 style = EditBoxBits { hScroll = true };
1185 // cursor = guiApp.GetCursor(IBeam);
1187 // Default Properties
1189 maxLineSize = 1024;*/
1192 maxLineSize = MAXINT;
1197 mouseSelect = this.mouseMove = false;
1200 x = selX = selY = 0;
1203 line = selLine = null;
1209 endY = clientSize.h;
1212 // Default space to 8 in case window is not constructed
1215 undoBuffer.dontRecord++;
1217 undoBuffer.dontRecord--;
1219 viewLine = lines.first;
1220 style.recomputeSyntax = true;
1226 UpdateCaretPosition(true);
1232 lines.Free(EditLine::Free);
1235 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1237 int count = wc - *renderStart;
1240 if(y + space.h >= box.top && y <= box.bottom)
1246 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1247 surface.TextFont(font);
1248 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1249 if(*x + w + XOFFSET > 0)
1250 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1255 w = numSpaces; // * space.w;
1256 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1257 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1258 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1266 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1267 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1273 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1277 *foreground = (*selected) ? selectionForeground : textColor;
1278 *background = selectionBackground;
1279 *opacity = *selected;
1286 if(this.overwrite && active)
1288 if((style.stuckCaret && wc == line.count && !line.next) ||
1289 (!mouseMove && line == this.line && wc == editX))
1298 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1302 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1303 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1304 bool inString = false;
1305 bool inQuotes = false;
1306 bool inPrep = reset ? false : style.inPrep;
1307 bool inSingleLineComment = false;
1308 bool escaped = reset ? false : style.escaped;
1309 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1310 bool continuedString = reset ? false : style.continuedString;
1311 bool continuedQuotes = reset ? false : style.continuedQuotes;
1313 EditLine line = reset ? lines.first : firstLine;
1314 // int maxBackUp = 1000, c;
1315 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1316 for(; line != viewLine; line = line.next)
1318 char * text = line.buffer;
1321 bool lastWasStar = false;
1322 bool firstWord = true;
1323 if(!escaped) inPrep = false;
1324 inSingleLineComment = continuedSingleLineComment;
1326 inString = continuedString;
1327 inQuotes = continuedQuotes;
1330 for(c = 0; (ch = text[c]); c++)
1332 bool wasEscaped = escaped;
1333 bool backLastWasStar = lastWasStar;
1334 bool backWasInMultiLine = wasInMultiLine;
1336 lastWasStar = false;
1337 wasInMultiLine = inMultiLineComment;
1340 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1342 if(text[c+1] == '/')
1344 inSingleLineComment = true;
1346 else if(text[c+1] == '*')
1348 inMultiLineComment = true;
1351 else if(backLastWasStar)
1352 inMultiLineComment = false;
1356 if(backWasInMultiLine) lastWasStar = true;
1358 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1360 if(inString && !wasEscaped)
1369 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1371 if(inQuotes && !wasEscaped)
1383 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1390 else if(ch != ' ' && ch != '\t')
1393 if(line.count && line.text[line.count - 1] == '\\')
1395 continuedSingleLineComment = inSingleLineComment;
1396 continuedString = inString;
1397 continuedQuotes = inQuotes;
1401 continuedSingleLineComment = false;
1402 continuedString = false;
1403 continuedQuotes = false;
1407 style.continuedSingleLineComment = continuedSingleLineComment;
1408 style.continuedString = continuedString;
1409 style.continuedQuotes = continuedQuotes;
1410 style.inMultiLineComment = inMultiLineComment;
1411 style.wasInMultiLine = wasInMultiLine;
1412 style.inPrep = inPrep;
1413 style.escaped = escaped;
1417 /*void OnDrawOverChildren(Surface surface)
1419 if(style.lineNumbers)
1421 int currentLineNumber = this.viewY + 1;
1424 for(i = 0; i * space.h < box.height; i++)
1426 // ********* LINE NUMBERING *********
1427 surface.SetForeground(Color{60, 60, 60});
1428 //Highlight current line
1429 if(this.caretY / space.h == currentLineNumber - 1)
1430 surface.SetBackground(Color{220, 220, 220});
1432 surface.SetBackground(Color{230, 230, 230});
1433 surface.textOpacity = true;
1435 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1436 if(currentLineNumber > this.lineCount)
1437 surface.WriteText(0,i*space.h+1," ",6);
1439 surface.WriteText(0,i*space.h+1,lineText,6);
1441 currentLineNumber++;
1446 void OnRedraw(Surface surface)
1450 bool selected = false, selection = true;
1452 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1453 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1454 Color defaultTextColor = property::foreground;
1457 int maxW = clientSize.w;
1459 Color foreground, background;
1462 // Overwrite Caret Stuff
1463 bool overWrite = false;
1464 int overWriteX, overWriteY;
1467 // ****** SYNTAX STATES ******
1468 bool inMultiLineComment = style.inMultiLineComment;
1469 bool inString = false;
1470 bool inQuotes = false;
1471 bool inPrep = style.inPrep;
1472 bool inSingleLineComment = false;
1473 bool escaped = style.escaped;
1474 bool continuedSingleLineComment = style.continuedSingleLineComment;
1475 bool continuedString = style.continuedString;
1476 bool continuedQuotes = style.continuedQuotes;
1477 bool wasInMultiLine = style.wasInMultiLine;
1478 // ****** ************* ******
1481 defaultTextColor = Color { 85, 85, 85 };
1482 textColor = defaultTextColor;
1485 Abs(selectionBackground.r - property::background.r) +
1486 Abs(selectionBackground.g - property::background.g) +
1487 Abs(selectionBackground.b - property::background.b) < 92)
1489 selectionBackground = formColor;
1490 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1493 surface.TextFont(this.font);
1495 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1496 surface.Area(0,0,MAXINT,MAXINT);
1501 surface.SetBackground(formColor);
1502 surface.Area(0,0,clientSize.w, clientSize.h);
1505 if(this.selX == this.x && this.selY == this.y)
1515 editX = Min(this.x,this.line.count);
1516 selX = Min(this.selX,this.selLine.count);
1519 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1521 foreground = selected ? selectionForeground : textColor;
1522 background = selectionBackground;
1528 surface.SetForeground(foreground);
1529 surface.SetBackground(background);
1530 surface.TextOpacity(opacity);
1532 surface.GetBox(box);
1534 for(line = this.viewLine; line; line = line.next)
1536 int x = -this.viewX;
1540 Color newTextColor = textColor = defaultTextColor;
1541 bool lineComplete = false;
1544 // ****** SYNTAX HIGHLIGHTING ******
1545 bool lastWasStar = false;
1546 bool trailingSpace = false;
1547 bool firstWord = true;
1548 if(!escaped) inPrep = false;
1549 inSingleLineComment = continuedSingleLineComment;
1551 inString = continuedString;
1552 inQuotes = continuedQuotes;
1553 // *********************************
1555 /* === DEBUGGING TOOL FOR MAXLINE ===
1557 if(line == this.maxLine)
1559 surface.SetBackground(GREEN|0xFF000000);
1560 surface.TextOpacity(true);
1564 surface.TextOpacity(selected ? true : false);
1565 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1569 if(line == this.selLine && line == this.line)
1571 end = Max(line.count, this.x);
1572 end = Max(end, this.selX);
1574 else if(line == this.selLine)
1575 end = Max(line.count, this.selX);
1576 else if(line == this.line)
1577 end = Max(line.count, this.x);
1585 bool spacing = true;
1586 bool cantHaveWords = false;
1593 lineComplete = false;
1596 textColor = newTextColor;
1599 foreground = textColor;
1600 surface.SetForeground(textColor);
1604 for(; c<end && !cantHaveWords;)
1608 bufferLen += wordLen;
1612 /*if(line.count > 4000)
1619 for(; c<line.count; c++)
1621 unichar ch = line.buffer[c];
1622 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1623 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1624 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1625 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1626 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1629 trailingSpace = false;
1635 for(; c<line.count; c++)
1637 unichar ch = line.buffer[c];
1638 if(ch == '\t' || ch == ' ')
1640 cantHaveWords = true;
1641 if(ch == ' ' && c == line.count-1)
1642 trailingSpace = true;
1646 if(ch != ' ' && ch != '\t')
1651 if(c == line.count && c < end)
1660 cantHaveWords = true;
1665 lastWasStar = false;
1671 bool backEscaped = escaped;
1672 bool backLastWasStar = lastWasStar;
1673 bool backInMultiLineComment = inMultiLineComment;
1674 bool backInString = inString;
1675 bool backInQuotes = inQuotes;
1676 bool backInPrep = inPrep;
1677 bool backInSingleLineComment = inSingleLineComment;
1678 bool backWasInMultiLine = wasInMultiLine;
1680 char * word = line.buffer + c - wordLen;
1682 bool wasEscaped = escaped;
1684 lastWasStar = false;
1686 wasInMultiLine = inMultiLineComment;
1688 // Determine Syntax Highlighting
1689 newTextColor = defaultTextColor;
1692 if(inSingleLineComment || inMultiLineComment)
1694 newTextColor = colorScheme.commentColor;
1698 newTextColor = colorScheme.charLiteralColor;
1702 newTextColor = colorScheme.stringLiteralColor;
1706 newTextColor = colorScheme.preprocessorColor;
1708 if(wordLen == 1 && word[0] == '/')
1710 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1714 inSingleLineComment = true;
1715 newTextColor = colorScheme.commentColor;
1717 else if(word[1] == '*')
1719 inMultiLineComment = true;
1720 newTextColor = colorScheme.commentColor;
1723 else if(backLastWasStar)
1724 inMultiLineComment = false;
1726 else if(wordLen == 1 && word[0] == '*')
1728 if(backWasInMultiLine)
1731 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1733 if(inString && !wasEscaped)
1740 newTextColor = colorScheme.stringLiteralColor;
1743 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1745 if(inQuotes && !wasEscaped)
1750 newTextColor = colorScheme.charLiteralColor;
1753 else if(wordLen == 1 && word[0] == '\\')
1758 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1760 char * dot = strchr(word, '.');
1761 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1767 exponent = strchr(word, 'p');
1768 if(!exponent) exponent = strchr(word, 'P');
1772 exponent = strchr(word, 'e');
1773 if(!exponent) exponent = strchr(word, 'E');
1775 if(dot && dot > word + wordLen) dot = null;
1776 isReal = dot || exponent;
1778 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1780 strtol(word, &s, 0);
1786 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1789 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1793 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1796 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1799 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1800 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1801 default: valid = false;
1805 // Don't highlight numbers with too many decimal points
1806 if(s[0] == '.' && isdigit(s[1]))
1809 while(s[0] == '.' && isdigit(s[1]))
1811 int newWordLen = s - word;
1812 c += newWordLen - wordLen;
1813 wordLen = newWordLen;
1816 newWordLen = s - word;
1817 c += newWordLen - wordLen;
1818 wordLen = newWordLen;
1822 int newWordLen = s + i - word;
1823 newTextColor = colorScheme.numberColor;
1824 c += newWordLen - wordLen;
1825 wordLen = newWordLen;
1827 else if(dot && dot > word && dot < s)
1828 newTextColor = colorScheme.numberColor;
1833 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1838 newTextColor = colorScheme.preprocessorColor;
1841 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1843 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1845 char ** keys = keyWords[g];
1846 int * len = keyLen[g];
1847 for(ccc = 0; keys[ccc]; ccc++)
1849 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1851 newTextColor = colorScheme.keywordColors[g];
1860 // If highlighting for this word is different, break
1861 if(newTextColor != textColor)
1865 // Better solution than going back?
1868 // Reset syntax flags
1869 escaped = backEscaped;
1870 lastWasStar = backLastWasStar;
1871 inMultiLineComment = backInMultiLineComment;
1872 inString = backInString;
1873 inQuotes = backInQuotes;
1874 inPrep = backInPrep;
1875 inSingleLineComment = backInSingleLineComment;
1876 wasInMultiLine = backWasInMultiLine;
1881 textColor = newTextColor;
1884 foreground = textColor;
1885 surface.SetForeground(textColor);
1892 // If we're not breaking, this can't be rendered as spacing anymore
1895 // Do word wrapping here
1901 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1906 w += numSpaces * space.w;
1908 if(x + viewX + w > maxW)
1910 // Avoid an endless loop until we fix wrapping
1911 if(c - wordLen > start)
1914 lineComplete = true;
1920 bufferLen += wordLen;
1925 int renderStart = start;
1927 bool flagTrailingSpace = false;
1931 // Render checking if we need to split because of selection or to find where to draw insert caret
1932 for(wc = start; wc < start + bufferLen; wc++)
1934 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1935 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1936 if(overWrite == true)
1938 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1939 if(overWriteCh == '\t') overWriteCh = ' ';
1944 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1945 if(flagTrailingSpace) surface.SetBackground(red);
1946 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1947 if(overWrite == true)
1955 surface.TextOpacity(opacity);
1956 surface.SetBackground(background);
1957 surface.SetForeground(foreground);
1964 if(wc < line.count && line.buffer[wc] == '\t')
1966 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1970 numSpaces += space.w;
1974 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1975 if(flagTrailingSpace) surface.SetBackground(red);
1976 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1981 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1982 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1984 if(overWrite == true)
1991 surface.TextOpacity(opacity);
1992 surface.SetBackground(background);
1993 surface.SetForeground(foreground);
1996 if(style.freeCaret && selected)
1998 surface.SetBackground(selectionBackground);
1999 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2000 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
2005 if(style.freeCaret && selected)
2007 surface.SetBackground(selectionBackground);
2008 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2011 if(line.count && line.text[line.count - 1] == '\\')
2013 continuedSingleLineComment = inSingleLineComment;
2014 continuedString = inString;
2015 continuedQuotes = inQuotes;
2019 continuedSingleLineComment = false;
2020 continuedString = false;
2021 continuedQuotes = false;
2025 if(y > box.bottom) // >=clientSize.h)
2031 surface.TextOpacity(true);
2032 surface.SetForeground(black);
2033 surface.SetBackground(Color {255,255,85});
2034 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2038 void FixScrollArea()
2040 if(style.hScroll || style.vScroll)
2042 int width = maxLength + XOFFSET;
2043 int height = lineCount * space.h;
2044 if(style.freeCaret && line)
2049 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2053 if(selX > selLine.count)
2054 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2059 SetScrollLineStep(8, space.h);
2060 SetScrollArea(width, height, true);
2064 void ComputeLength(EditLine line)
2071 for(c = 0; c < line.count; )
2080 for(len = 0; c < line.count; c += numBytes)
2082 ch = line.buffer[c];
2083 numBytes = UTF8_NUM_BYTES(ch);
2085 if(ch == ' ' || ch == '\t')
2092 if(!len && ch == ' ')
2097 else if(!len && ch == '\t')
2099 w = (tabSize * space.w) - (x % (tabSize * space.w));
2103 FontExtent(display, font, line.buffer + start, len, &w, null);
2114 if(line.length > this.maxLength)
2116 this.maxLine = line;
2117 this.maxLength = line.length;
2119 if(style.autoSize) AutoSize();
2128 this.maxLine = null;
2130 for(line = lines.first; line; line = line.next)
2132 if(line.length > this.maxLength)
2134 this.maxLength = line.length;
2135 this.maxLine = line;
2139 if(style.autoSize) AutoSize();
2144 if(this.selY != this.y)
2146 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2150 void ComputeColumn()
2152 // Adjust new edit X position according to tabs
2153 int c, position = 0;
2156 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2158 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2160 position += this.tabSize - (position % this.tabSize);
2164 position += this.x - c;
2165 this.col = position;
2168 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2170 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, true, null);
2173 bool HasCommentOrEscape(EditLine line)
2175 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2180 for(c = line.count-1; c >= 0; c--)
2182 char ch = line.buffer[c];
2188 else //if(ch != ' ' && ch != '\t')
2195 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, bool highlight, int * addedSpacesPtr)
2197 EditLine line = l1, next;
2199 int oldCount1, oldCount2;
2200 int y, firstViewY, firstY, firstDropY, firstSelY;
2203 DelTextAction action = null;
2204 bool hadComment = false;
2208 hadComment = HasCommentOrEscape(line);
2210 if(y2 > y1 || c2 > c1)
2212 if(start < l1.count)
2214 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2218 oldCount1 = l1.count;
2220 while(c1 < oldCount1)
2222 byte ch = buffer[c1];
2223 if(UTF8_IS_FIRST(ch)) break;
2227 oldCount2 = l2.count;
2229 while(c2 < oldCount2)
2231 byte ch = buffer[c2];
2232 if(UTF8_IS_FIRST(ch)) break;
2237 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2242 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2243 string = new char[len];
2244 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter, noHighlight = !highlight };
2245 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2249 //oldCount1 = l1.count;
2250 //oldCount2 = l2.count;
2253 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2254 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2257 if(c2 > oldCount2) c2 = oldCount2;
2258 if(!(style.freeCaret))
2259 if(c1 > oldCount1) c1 = oldCount1;
2260 newLineCount = c1 + l2.count-c2;
2266 buffer = new char[line.size ? line.size : 1];
2268 buffer = new char[line.size];
2270 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2275 if(!line.AdjustBuffer(newLineCount))
2279 /*if(newLineCount > 4000 || newLineCount < 0)
2280 printf("Warning");*/
2282 line.count = newLineCount;
2284 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2289 action.addedSpaces = c1-oldCount1;
2291 if(action.addedSpaces > action.x1)
2297 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2298 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2300 line.buffer[line.count] = '\0';
2307 this.dropX -= c2-c1;
2308 this.dropX = Max(this.dropX,0);
2310 this.x = Max(this.x,0);
2317 firstViewY = this.viewY;
2319 firstDropY = this.dropY;
2320 firstSelY = this.selY;
2321 for(line = l1;line;line = next, y++)
2329 if(line == this.viewLine)
2331 if(this.viewLine.next)
2333 this.viewLine = this.viewLine.next;
2335 style.recomputeSyntax = true;
2339 this.viewLine = this.viewLine.prev;
2341 style.recomputeSyntax = true;
2344 else if(y < firstViewY)
2346 if(line == this.line)
2350 this.line = this.line.next;
2351 this.x = this.line.count;
2356 this.line = this.line.prev;
2357 this.x = this.line.count;
2364 if(line == this.dropLine)
2366 if(this.dropLine.next)
2368 this.dropLine = this.dropLine.next;
2369 this.dropX = this.dropLine.count;
2373 this.dropLine = this.dropLine.prev;
2374 this.dropX = this.dropLine.count;
2378 else if(y < firstDropY)
2380 if(line == this.selLine)
2382 if(this.selLine.next)
2384 this.selLine = this.selLine.next;
2385 this.selX = this.selLine.count;
2389 this.selLine = this.selLine.prev;
2390 this.selX = this.selLine.count;
2394 else if(y < firstSelY)
2398 if(line == l2) break;
2402 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2405 style.recomputeSyntax = true;
2410 bool DelSel(int * addedSpacesPtr)
2412 if(this.line != this.selLine || this.x != this.selX)
2414 if(this.selY < this.y)
2416 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2419 this.line = this.selLine;
2421 else if(this.selY > this.y)
2423 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2426 this.selLine = this.line;
2428 else if(this.selX < this.x)
2430 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, true, addedSpacesPtr);
2433 this.line = this.selLine;
2437 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, true, addedSpacesPtr);
2440 this.selLine = this.line;
2448 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2450 bool hadComment = false;
2451 // Add the line here
2452 EditLine line = this.line;
2454 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2455 if(!style.hScroll && created)
2457 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2462 // Lock if no place to display.
2465 else if(endX > this.x)
2466 max = Max(this.x, line.count);
2470 for(x = 0, c = 0; c < max+count; )
2475 if(c < Min(this.x, line.count))
2476 string = line.buffer + c;
2479 else if(c < endX + count)
2480 string = stringLine + c - endX;
2482 string = line.buffer + c - endX - count;
2486 w = (tabSize * space.w) - (x % (tabSize * space.w));
2490 numBytes = UTF8_NUM_BYTES(*string);
2491 FontExtent(display, this.font, string, numBytes, &w, null);
2495 if(x >= clientSize.w)
2500 c += numBytes; // - 1;
2506 int addedSpaces = 0;
2509 // Add blank spaces if EES_FREECARET
2510 if(this.x > line.count)
2520 for(c = 0; c<line.count; c++)
2522 if(this.line.buffer[c] == '\t')
2523 position += this.tabSize - (position % this.tabSize);
2527 wantedPosition = position + (this.x - line.count);
2529 // A tab is too much...
2530 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2531 addedSpaces = wantedPosition - position;
2536 position += this.tabSize - (position % this.tabSize);
2537 // Add as many tabs as needed
2538 addedTabs += (wantedPosition - position) / this.tabSize;
2539 position += (addedTabs-1) * this.tabSize;
2540 // Finish off with spaces
2541 addedSpaces = wantedPosition - position;
2545 addedSpaces = this.x - line.count;
2548 this.x = Min(this.x, line.count);
2550 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2556 hadComment = HasCommentOrEscape(line);
2558 // Adjust the size of the line
2559 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2563 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2566 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2567 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2570 *addedTabsPtr = addedTabs;
2571 FillBytes(line.buffer+line.count,'\t',addedTabs);
2573 if(addedTabs > 4000 || addedTabs < 0)
2576 line.count += addedTabs;
2582 FillBytes(line.buffer+line.count,' ',addedSpaces);
2584 if(addedSpaces > 4000 || addedSpaces < 0)
2587 line.count += addedSpaces;
2588 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2590 else if(addedSpacesPtr)
2591 *addedSpacesPtr = 0;
2593 if(count > 4000 || count < 0)
2596 line.count += count;
2597 this.x += count + addedTabs + addedSpaces;
2600 this.selLine = this.line;
2602 line.buffer[line.count] = '\0';
2604 ComputeLength(line);
2609 hasComment = HasCommentOrEscape(line);
2610 if(!undoBuffer.insideRedo)
2612 int backDontRecord = undoBuffer.dontRecord;
2613 undoBuffer.dontRecord = 0;
2614 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2615 undoBuffer.dontRecord = backDontRecord;
2617 if(style.syntax && (hadComment || hasComment || line != this.line))
2619 style.recomputeSyntax = true;
2627 void Emptyline(EditLine line, int y)
2630 DelCh(line, y, 0, line.next, y+1, 0, true);
2632 DelCh(line, y, 0, line, y, line.count, true);
2635 void GoToEnd(bool deselect)
2639 this.line = this.lines.last;
2640 if(this.y != this.lineCount-1)
2642 else if (this.x != this.line.count)
2643 DirtyLine(this.lineCount-1);
2644 this.y = this.lineCount-1;
2645 this.x = this.line.count;
2652 void GoToHome(bool deselect)
2656 this.line = this.lines.first;
2659 else if (this.x !=0)
2660 DirtyLine(this.lineCount-1);
2669 // Returns true if it needs scrolling
2670 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2676 bool needHScroll = false;
2683 line = this.viewLine ? (void *)this.viewLine.prev : null;
2688 line = (void *)this.lines.first;
2693 py = Min(py, clientSize.h);
2695 py = Min(py, this.lineCount);
2697 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2703 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2706 px = Min(px,clientSize.w+this.space.w);
2710 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2713 if(tline) *tline = line;
2716 // Prevent divide by 0 from non valid this.font
2718 return (y < this.viewY) || needHScroll;
2720 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2724 // Minimal Update Management Functions
2728 this.endY = clientSize.h-1;
2729 // ErrorLog("DirtyAll\n");
2732 void DirtyEnd(int y)
2734 if((y - this.viewY)*this.space.h < this.startY)
2735 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2736 this.endY = clientSize.h-1;
2737 //ErrorLog("DirtyEnd %d\n", y);
2740 void DirtyLine(int y)
2744 if((y - this.viewY)*this.space.h < this.startY)
2745 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2746 if((y - this.viewY+1)*this.space.h > this.endY)
2747 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2749 //ErrorLog("DirtyLine %d\n", y);
2756 if(style.recomputeSyntax)
2758 FigureStartSyntaxStates(lines.first, true);
2759 style.recomputeSyntax = false;
2762 if(this.startY > this.endY) return;
2763 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2769 box.right = clientSize.w-1;
2770 box.top = this.startY;
2771 box.bottom = this.endY;
2774 this.startY = clientSize.h;
2778 bool IsMouseOnSelection()
2780 bool mouseOnSelection = false;
2783 int minY = Min(this.selY, this.y);
2784 int maxY = Max(this.selY, this.y);
2785 int minX = Min(this.selX, this.x);
2786 int maxX = Max(this.selX, this.x);
2788 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2790 if(maxX != minX || maxY != minY)
2792 if(y > minY && y < maxY)
2793 mouseOnSelection = true;
2794 else if(y == minY && y == maxY)
2795 mouseOnSelection = (x < maxX && x >= minX);
2799 mouseOnSelection = (x >= this.selX);
2800 else if(y == this.y)
2801 mouseOnSelection = (x >= this.x);
2806 mouseOnSelection = (x < this.selX);
2807 else if(y == this.y)
2808 mouseOnSelection = (x < this.x);
2811 return mouseOnSelection;
2814 void UpdateCaretPosition(bool setCaret)
2818 if(mouseMove || (!overwrite && !style.noCaret))
2820 int max = this.mouseMove ? this.dropX : this.x;
2821 int y = this.mouseMove ? this.dropY : this.y;
2822 EditLine line = this.mouseMove ? this.dropLine : this.line;
2824 if(!(style.freeCaret))
2825 max = Min(max, line.count);
2827 if(FontExtent && display)
2829 for(c = 0; c < max; )
2838 for(len = 0; c < Min(max, line.count); c += numBytes)
2840 ch = line.buffer[c];
2841 numBytes = UTF8_NUM_BYTES(ch);
2843 if(ch == ' ' || ch == '\t')
2850 if(!len && ch == ' ')
2855 else if(!len && ch == '\t')
2857 w = (tabSize * space.w) - (x % (tabSize * space.w));
2861 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2873 caretY = y * this.space.h;
2874 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2879 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2880 NotifyCaretMove(master, this, y + 1, x + 1);
2886 void SelectionEnables()
2888 if((x != selX || y != selY) && !selection)
2892 itemEditCut.disabled = false;
2893 itemEditDelete.disabled = false;
2895 itemEditCopy.disabled = false;
2897 this.selection = true;
2899 else if((x == selX && y == selY) && selection)
2901 itemEditCut.disabled = true;
2902 itemEditCopy.disabled = true;
2903 itemEditDelete.disabled = true;
2905 this.selection = false;
2909 void SetSelectCursor()
2911 if(!inactive || !style.noSelect)
2914 cursor = guiApp.GetCursor(arrow);
2915 else if(this.mouseSelect)
2916 cursor = guiApp.GetCursor(iBeam);
2919 if(IsMouseOnSelection())
2920 cursor = guiApp.GetCursor(arrow);
2922 cursor = guiApp.GetCursor(iBeam);
2927 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2930 int x = px ? *px : 0;
2937 if(c < Min(max, line.count))
2941 for(len = 0; c < Min(max, line.count); c += numBytes)
2943 ch = line.buffer[c];
2944 numBytes = UTF8_NUM_BYTES(ch);
2946 if(ch == ' ' || ch == '\t')
2953 if(!len && ch == ' ')
2958 else if(!len && ch == '\t')
2960 w = (tabSize * space.w) - (x % (tabSize * space.w));
2964 FontExtent(display, font, line.buffer + start, len, &w, null);
2968 if(style.freeCaret && c < max)
2977 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2982 int a = start + len;
2985 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2989 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2992 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2996 return Min(this.maxLineSize - 1, start + len);
3002 void SetCursorToViewX()
3004 bool selecting = this.x != selX || y != selY;
3006 // Horizontal Adjustment
3008 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3013 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3025 UpdateCaretPosition(false);
3031 void SetCursorToViewY()
3034 EditLine oldLine = this.line;
3036 bool selecting = this.x != this.selX || this.y != this.selY;
3038 numLines = clientSize.h / this.space.h;
3040 // Vertical Adjustment
3041 if(this.viewY > this.y)
3043 this.y = this.viewY;
3044 this.line = this.viewLine;
3047 if(this.viewY + numLines <= this.y)
3051 this.y = this.viewY-1;
3052 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3059 if(this.line != oldLine)
3061 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3069 this.selLine = this.line;
3072 UpdateCaretPosition(false);
3079 bool SaveFile(char * fileName)
3081 File f = eFile_Open(fileName, FO_WRITE);
3085 eWindow_SetModified(false);
3086 eInstance_Delete(f);
3093 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3097 if(!style.multiLine)
3099 x = (active && this.active && !style.readOnly) ? line.count : 0;
3102 SetViewToCursor(true);
3107 if(!active && modified)
3110 if(!NotifyModified(master, this))
3113 *goOnWithActivation = false;
3120 if(timer) timer.Stop();
3122 mouseSelect = false;
3130 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3131 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3132 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3135 bool OnResizing(int *w, int *h)
3141 *w = space.h * 80 / 14;
3145 void OnResize(int w, int h)
3148 if(!hasHorzScroll && !hasVertScroll && viewLine)
3149 SetViewToCursor(true);
3151 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3153 SetViewToCursor(true);
3158 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3160 if(style.readOnly) return true;
3161 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3162 // Middle button already serves as a dragger as well.
3167 bool OnRightButtonDown(int x, int y, Modifiers mods)
3169 this.rightButtonDown = true;
3174 bool OnRightButtonUp(int x, int y, Modifiers mods)
3177 if(!parent.inactive && rightButtonDown)
3180 Menu contextMenu { };
3182 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3183 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3184 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3185 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3186 MenuDivider { contextMenu };
3187 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3189 popup = PopupMenu { master = this, menu = contextMenu,
3191 nonClient = true, interim = false, parent = parent,
3192 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3194 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3198 rightButtonDown = false;
3202 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3207 if(style.noSelect) return true;
3209 // Should we have a separate 'selectOnActivate' style?
3210 if(!mods.isActivate || (style.readOnly && style.multiLine))
3216 mouseX = mx - XOFFSET;
3219 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3221 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3227 else if(IsMouseOnSelection() && !mods.isActivate)
3237 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3239 if(mods.shift && !mods.isActivate)
3260 UpdateCaretPosition(true);
3261 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3262 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3266 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3270 mouseSelect = false;
3281 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3283 //PrintLn("MouseMove: ", x, ", ", y);
3289 mouseMove = IsMouseOnSelection();
3293 int size = SelSize();
3296 char * text = new char[size+1];
3300 GetSel(text, false);
3302 if(Max(selY, this.y) == dropY)
3306 if(this.dropX > this.selX)
3307 moveX = this.x - this.selX;
3311 if(this.dropX > this.x)
3312 moveX = this.selX - this.x;
3316 this.dropX -= moveX;
3317 this.selX = this.x = this.dropX;
3318 this.selY = this.y = this.dropY;
3319 this.selLine = this.line = this.dropLine;
3321 SetViewToCursor(true);
3326 byte * c = ((EditBox)this).multiLineContents, * c2;
3327 int l1 = c ? strlen(c) : 0, l2;
3330 c2 = ((EditBox)this).multiLineContents;
3331 l2 = c2 ? strlen(c2) : 0;
3332 if(l1 != l2 || (l1 && strcmp(c, c2)))
3362 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3364 //PrintLn("Dropped: ", x, ", ", y);
3366 NotifyDropped(master, this, x, y);
3370 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3371 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3375 bool OnMouseMove(int mx, int my, Modifiers mods)
3381 if(mods != -1 && mods.isSideEffect)
3386 if(style.noSelect) return true;
3387 if(wordSelect) return true;
3388 mouseX = mx - XOFFSET;
3391 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3393 if(this.mouseMove || this.mouseSelect)
3402 ((style.hScroll) || (style.vScroll)))
3409 DirtyLine(this.dropY);
3412 DirtyLine(this.dropY);
3413 this.dropLine = line;
3414 SetViewToCursor(true);
3416 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3419 else if(this.mouseSelect)
3421 DirtyLine(this.selY);
3428 SetViewToCursor(true);
3431 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3438 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3443 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3447 if(style.noSelect) return true;
3448 FindMouse(mx, my, &x, &y, &line, false);
3449 if(!NotifyDoubleClick(master, this, line, mods))
3456 for(c = x; c >= 0; c--)
3459 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3460 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3467 for(c = start; c<line.count; c += numBytes)
3469 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3470 if(!ch || !IS_ALUNDER(ch))
3480 this.line = this.selLine = line;
3481 this.wordSelect = (c != start);
3492 Menu fileMenu { menu, "File", F };
3493 saveDialog = fileDialog;
3494 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3495 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3497 if(style.autoSize) AutoSize();
3505 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3506 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3508 space.w = Max(space.w, 1);
3509 large.w = Max(large.w, 1);
3510 space.h = Max(space.h, 1);
3511 large.h = Max(large.h, 1);
3515 for(line = lines.first; line; line = line.next)
3516 ComputeLength(line);
3521 SetViewToCursor(true);
3529 bool OnLoadGraphics()
3531 FontExtent = Display::FontExtent;
3534 // UpdateCaretPosition(true);
3538 void OnUnloadGraphics()
3543 bool OnKeyHit(Key key, unichar ch)
3545 bool shift = (key.shift) ? true : false;
3547 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3549 if(!ch && !key.alt && !key.ctrl)
3551 key.code = (SmartKey)key.code;
3553 else if(!ch && key.alt)
3556 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3559 if(style.readOnly) break;
3560 if(style.stuckCaret) GoToEnd(true);
3561 if(!(style.freeCaret))
3563 this.x = Min(this.x, this.line.count);
3569 EditLine line = this.line;
3571 for(y = this.y; y>= 0; y--)
3573 c = (y == this.y) ? (Min(this.x-1, line.count-1)) : line.count-1;
3575 // Slow down when going on lines...
3576 if(y != this.y) break;
3580 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3581 if(IS_ALUNDER(line.buffer[c]))
3586 if(!IS_ALUNDER(line.buffer[c]))
3588 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3603 _DelCh(line, y, c+1, this.line, this.y, this.x, true, false, null);
3604 this.x = this.selX = Min(c+1, line.count);
3605 this.y = this.selY = y;
3606 this.line = this.selLine = line;
3607 SetViewToCursor(true);
3618 if(style.readOnly) break;
3619 if(style.stuckCaret) break;
3628 if(this.line != this.selLine || this.x != this.selX)
3631 SetViewToCursor(true);
3636 EditLine line1 = this.line, line2 = this.line;
3637 int x1, y1 = this.y, x2, y2 = this.y;
3638 if(!style.freeCaret)
3640 this.selX = this.x = Min(this.x, this.line.count);
3645 if(x1 < line1.count)
3652 char * buffer = line1.buffer;
3653 for(i = x1; i < line1.count; i++)
3655 if(!IS_ALUNDER(buffer[i]))
3659 for(; i < line1.count; i++)
3661 // Delete trailing whitespace
3662 if(IS_ALUNDER(buffer[i]))
3672 // Avoid creating trailing spaces if there is no content on next line
3675 if(line2 && !line2.count)
3680 _DelCh(line1, y1, x1, line2, y2, x2, false, false, null);
3681 SetViewToCursor(true);
3690 if(!key.alt && !key.ctrl)
3694 bool stuffAfter = false;
3697 bool resetX = false;
3700 if(style.stuckCaret) GoToEnd(true);
3701 if(style.readOnly) break;
3702 if(!(style.multiLine)) break;
3704 for(c = 0; c<this.line.count && c<this.x; c++)
3706 if(this.line.buffer[c] == '\t')
3707 position += this.tabSize - (position % this.tabSize);
3708 else if(this.line.buffer[c] == ' ')
3714 // Prevent adding trailing spaces if at the head of a line
3715 if(c && c == this.x && c < this.line.count && this.x == this.selX && this.y == this.selY)
3727 if(this.x < this.line.count)
3730 //If last character is a { indent one tab
3731 if(this.line.buffer[this.x - 1] == '{')
3733 //Except if the next non space character is a }
3734 bool indent = false;
3736 for(i = this.x; i < this.line.size; i++)
3737 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3739 if(this.line.buffer[i] != '}')
3744 position += this.tabSize;
3747 addString = new char[position + 2];
3748 addString[len++] = '\n';
3749 addString[len] = '\0';
3751 if(stuffAfter || !style.freeCaret)
3753 for(c = 0; c<position; )
3755 if(style.useTab && c + this.tabSize <= position)
3757 addString[len++] = '\t';
3762 addString[len++] = ' ';
3766 addString[len] = '\0';
3770 /*EditLine prevLine = this.line.prev;
3773 // Nuke spaces if that is all that is left on previous line
3775 char * buffer = prevLine.buffer;
3776 for(i = 0; i < prevLine.count; i++)
3777 if(buffer[i] != ' ' && buffer[i] != '\t')
3779 if(i == prevLine.count)
3780 DelCh(prevLine, this.y - 1, 0, prevLine, this.y - 1, prevLine.count, false);
3784 this.x = this.selX = backX;
3787 else if(!stuffAfter && style.freeCaret)
3789 this.x = this.selX = position;
3793 SetViewToCursor(true);
3803 if(style.stuckCaret) break;
3804 if(!(style.freeCaret))
3806 this.x = Min(this.x, this.line.count);
3807 this.selX = Min(this.selX, this.selLine.count);
3810 if(!shift) SelDirty();
3813 bool foundAlpha = false;
3816 EditLine line, lastLine;
3819 for(line = this.line; (line && !found); line = line.prev, y--)
3824 if(this.x == 0 && line != this.line)
3833 if(line == this.line) start = this.x -1; else start = line.count-1;
3834 start = Min(start, line.count-1);
3836 for(c = start; c >= 0;)
3839 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3857 byte ch = line.buffer[c];
3858 if(UTF8_IS_FIRST(ch)) break;
3862 // No next word found,
3863 if(!found && ( this.x > 0 || (!line.count && this.x)))
3877 this.line = lastLine;
3888 byte * buffer = line.buffer;
3891 byte ch = buffer[x];
3892 if(UTF8_IS_FIRST(ch)) break;
3909 if(!shift) _Deselect();
3910 SetViewToCursor(true);
3916 if(style.stuckCaret) break;
3917 if(!(style.freeCaret))
3919 this.x = Min(this.x, this.line.count);
3920 this.selX = Min(this.selX, this.selLine.count);
3923 if(!shift) SelDirty();
3924 if(!shift && (this.x != this.selX || this.y != this.selY));
3927 bool onAChar = false;
3928 if(this.selX != this.x || this.selY != this.y)
3930 if(this.x<this.line.count)
3931 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3933 if(key.shift && onAChar &&
3934 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3936 bool foundAlpha = false;
3938 EditLine line, lastLine;
3940 int lastC, lastY, lastNumBytes;
3942 for(line = this.line; (line && !found); line = line.next, y++)
3944 int start = (line == this.line) ? this.x : 0;
3948 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3954 lastNumBytes = numBytes;
3964 if(!found && (c != this.x || line != this.line))
3977 this.x = lastC + lastNumBytes;
3979 this.line = lastLine;
3986 bool foundAlpha = false;
3991 for(line = this.line; (line && !found); line = line.next, y++)
3993 int start = (line == this.line) ? this.x : 0;
3997 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
4013 // No next word found,
4014 if(!found && (c != this.x || line != this.line))
4018 this.x = line.count;
4030 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
4034 byte * buffer = line.buffer;
4037 byte ch = buffer[x];
4038 if(UTF8_IS_FIRST(ch)) break;
4059 if(!shift) _Deselect();
4060 SetViewToCursor(true);
4067 if(!style.vScroll || hasVertScroll) break;
4073 if(style.stuckCaret) break;
4075 if(!shift) SelDirty();
4085 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4089 if(!shift) _Deselect();
4091 SetViewToCursor(false);
4094 if(caretY == this.y * space.h)
4100 if(!style.freeCaret)
4101 this.x = Min(this.x, line.count);
4111 int sx = 0, sy = this.y * space.h;
4112 char * text = line.text;
4113 int maxW = clientSize.w - sx;
4114 display.FontExtent(font, " ", 1, null, &th);
4118 int startPos = textPos;
4121 bool lineComplete = false;
4123 if(!style.wrap && caretY == MAXINT)
4126 //textPos = line.count;
4127 //lineComplete = true;
4130 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4134 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4137 len = (nextSpace - (text + textPos));
4139 len = line.count - textPos;
4141 if(textPos < line.count)
4143 display.FontExtent(font, text + textPos, len, &w, null);
4145 if(nextSpace) { w += space.w; len++; }
4147 if(style.wrap && x + width + w > maxW && x > 0)
4149 lineComplete = true;
4159 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4163 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4166 if(this.x > line.count)
4169 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4170 len = this.x - startPos;
4171 display.FontExtent(font, text + startPos, len, &x, null);
4175 if(!shift) _Deselect();
4177 SetViewToCursor(false);
4181 if(sy == caretY - th || textPos >= line.count)
4183 if(textPos >= line.count)
4185 int c = textPos - 1;
4186 while(c > 0 && text[c] == ' ') c--;
4190 this.x = line.count;
4193 if(!shift) _Deselect();
4195 SetViewToCursor(false);
4200 } while(textPos < line.count);
4203 if(!shift) _Deselect();
4205 SetViewToCursor(false);
4214 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4215 if(!shift) SelDirty();
4216 this.line = this.line.prev;
4222 if(!shift) _Deselect();
4226 SetViewToCursor(false);
4232 return style.multiLine ? false : true;
4236 if(!style.vScroll || hasVertScroll)
4243 if(style.stuckCaret) break;
4247 int sx = 0, sy = this.y * this.space.h;
4248 int maxW = clientSize.w - sx;
4249 char * text = line.buffer;
4251 if(!shift) SelDirty();
4257 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4267 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4271 if(!shift) _Deselect();
4273 SetViewToCursor(false);
4276 while(!textPos || (style.freeCaret || textPos<line.count))
4278 int startPos = textPos;
4281 bool lineComplete = false;
4282 if(!style.wrap && sy <= caretY)
4284 textPos = line.count;
4285 lineComplete = true;
4287 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4291 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4294 len = (nextSpace - (text + textPos));
4296 len = line.count - textPos;
4298 if(textPos < line.count)
4300 display.FontExtent(font, text + textPos, len, &w, &th);
4302 if(nextSpace) { w += space.w; len++; }
4303 if(style.wrap && x + width + w > maxW && x > 0)
4305 lineComplete = true;
4315 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4319 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4322 if(this.x > line.count)
4325 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4327 len = this.x - startPos;
4328 display.FontExtent(font, text + startPos, len, &x, null);
4331 if(!shift) _Deselect();
4334 SetViewToCursor(false);
4340 this.x = line.count;
4343 if(!shift) _Deselect();
4346 SetViewToCursor(false);
4349 else if(textPos >= line.count && line.next)
4355 sy = this.y * this.space.h;
4356 sx = 0; //textBlock.startX;
4362 sx = 0; //textBlock.startX;
4370 this.x = Min(this.x, line.count);
4371 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4373 if(!shift) _Deselect();
4376 if(this.selX != this.x || this.selY != this.y)
4379 SetViewToCursor(false);
4386 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4387 if(!shift) SelDirty();
4388 this.line = this.line.next;
4392 if(!shift) _Deselect();
4395 if(this.selX != this.x || this.selY != this.y)
4403 return style.multiLine ? false : true;
4406 if(style.stuckCaret) break;
4407 if(!style.multiLine && key.ctrl) break;
4408 if(!(style.freeCaret))
4409 this.selX = Min(this.selX, this.selLine.count);
4411 if(!shift) SelDirty();
4414 this.line = this.lines.first;
4415 if(this.y != 0 || this.x != 0)
4425 EditLine line = this.line;
4427 for(c=0; line.buffer[c]; c++)
4428 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4430 if(shift && (c != 0 || this.x))
4439 if(shift && this.x != 0)
4445 if(!shift) _Deselect();
4446 SetViewToCursor(true);
4452 if(style.stuckCaret) break;
4453 if(!style.multiLine && key.ctrl) break;
4454 if(!style.freeCaret)
4455 this.selX = Min(this.selX, this.selLine.count);
4457 if(!shift) SelDirty();
4462 else if(this.x != this.line.count)
4464 this.x = this.line.count;
4469 if(!shift) _Deselect();
4470 SetViewToCursor(true);
4475 if(style.tabKey && !key.ctrl)
4477 if(this.selY != this.y && style.tabSel)
4479 EditLine firstLine, lastLine;
4483 // Do multi line selection tabbing here
4484 if(this.selY < this.y)
4486 firstLine = this.selLine;
4487 lastLine = this.line;
4493 // Selecting going up
4494 firstLine = this.line;
4495 lastLine = this.selLine;
4502 for(line = firstLine; line; line = line.next, y++)
4504 if(line != lastLine || x)
4508 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4510 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4512 if(line.buffer[c] == '\t')
4517 else if(line.buffer[c] != ' ')
4522 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4525 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4526 char * string = new char[len];
4527 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4528 GetText(string, line, y, 0, line, y, lastC, false, false);
4531 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4532 if(!line.AdjustBuffer(line.count-lastC))
4538 if(line == lastLine) break;
4543 for(line = firstLine; line; line = line.next, y++)
4547 if(line != lastLine || x)
4551 if(line.count + 1 <= this.maxLineSize)
4553 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4555 if(!line.AdjustBuffer(line.count+1))
4558 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4560 AddCharAction action { ch = '\t', x = 0, y = y };
4564 memmove(line.buffer+1,line.buffer,line.size-1);
4566 line.buffer[0] = '\t';
4571 if(line.count + this.tabSize <= this.maxLineSize)
4574 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4575 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4577 if(!line.AdjustBuffer(line.count+this.tabSize))
4581 char * string = new char[this.tabSize + 1];
4582 memset(string, ' ', this.tabSize);
4583 string[this.tabSize] = '\0';
4584 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4587 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4588 line.count+=this.tabSize;
4589 for(c=0; c<this.tabSize; c++)
4590 line.buffer[c] = ' ';
4596 if(line == lastLine) break;
4599 ComputeLength(maxLine);
4611 char * addString = new char[this.tabSize + 1];
4613 if(!(style.freeCaret))
4615 this.x = Min(this.x, this.line.count);
4619 start = Min(this.x, this.selX);
4620 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4622 addString[len++] = ' ';
4630 SetViewToCursor(true);
4639 if(!(style.hScroll) || hasHorzScroll) break;
4640 if(this.viewX < this.maxLength)
4642 //this.viewX+=this.space.w*this.tabSize;
4644 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4651 if(!shift) _Deselect();
4663 if(!(style.hScroll) || hasHorzScroll) break;
4666 //this.viewX-=this.space.w*this.tabSize;
4667 //this.viewX = Max(this.viewX,0);
4669 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4670 // SetCursorToView();
4677 if(!shift) _Deselect();
4692 if(!(style.readOnly))
4698 this.overwrite ^= 1;
4699 UpdateCaretPosition(true);
4704 NotifyOvrToggle(master, this, this.overwrite);
4715 if(style.noSelect) break;
4718 //Save current view position
4719 int tempX = this.viewX;
4720 int tempY = this.viewY;
4724 this.selLine = this.lines.first;
4725 this.y = this.lineCount-1;
4726 this.line = this.lines.last;
4727 this.x = this.line.count;
4730 SetViewToCursor(true);
4732 //Restore previous view position
4733 SetScrollPosition(tempX, tempY * this.space.h);
4739 // TOCHECK: Was there any good reason why we weren't returning false here?
4740 return false; // break;
4745 if(!(style.readOnly))
4753 if(style.readOnly) break;
4759 if(style.readOnly) break;
4764 if(style.readOnly) break;
4768 if(style.readOnly) break;
4772 if(style.readOnly) break;
4773 if(key.shift && key.code == rightBracket)
4775 //Only indent back if you are exactly at one tab.
4777 bool whitespace = true;
4783 EditLine line = this.line;
4785 //Only remove one tab if there is nothing else on the line.
4786 for(i = 0; i < this.line.count; i++)
4788 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4802 for(pos = line.count - 1; pos >= 0; pos--)
4804 char c = line.buffer[pos];
4808 indentwidth += this.tabSize;
4810 //Counting backwards, so when you find a character, indentation is reset
4820 //Place the } to get an undo:
4823 this.x = this.line.count;
4827 putsize = indentwidth;
4829 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4831 newline = new char[putsize+2];
4832 newline[putsize] = '}';
4833 newline[putsize+1] = '\0';
4837 for(; i < indentwidth / this.tabSize; i++)
4839 for(;i < putsize; i++)
4848 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4860 void OnHScroll(ScrollBarAction action, int position, Key key)
4863 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4865 this.viewX = position;
4866 if(action != setRange)
4868 if(!this.mouseMove && style.cursorFollowsView)
4875 void OnVScroll(ScrollBarAction action, int position, Key key)
4877 int oldViewY = this.viewY;
4880 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4882 position /= this.space.h;
4884 if(position < this.viewY)
4886 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4887 style.recomputeSyntax = true;
4889 else if(position > this.viewY)
4891 EditLine oldViewLine = viewLine;
4892 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4893 FigureStartSyntaxStates(oldViewLine, false);
4896 if(action != setRange)
4898 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4901 if(this.x != this.selX || this.y != this.selY)
4905 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4908 int numLines = clientSize.h / this.space.h;
4910 if(Abs(this.viewY - oldViewY) < numLines)
4913 if(this.viewY > oldViewY)
4915 for(y = oldViewY; y <this.viewY; y++)
4916 DirtyLine(y + numLines);
4920 for(y = this.viewY; y <oldViewY; y++)
4921 DirtyLine(y + numLines);
4926 // Fix dirt of stuff before first line...
4927 if(this.viewY - oldViewY > 0)
4929 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4936 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4941 ReplaceTextAction replaceAction = null;
4942 AddCharAction addCharAction = null;
4943 int addedSpaces = 0, addedTabs = 0;
4945 if(ch == '\r') return true;
4946 if(style.stuckCaret /*|EES_READONLY)*/ )
4949 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4951 if(!undoBuffer.dontRecord)
4953 if(selX != x || selY != y)
4958 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4959 oldString = new char[len];
4960 UTF32toUTF8Len(&ch, 1, buffer, 4);
4961 newString = CopyString(buffer);
4962 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4964 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4965 if(selY < y || (selY == y && selX < x))
4967 replaceAction.x1 = selX;
4968 replaceAction.y1 = selY;
4969 replaceAction.x2 = x;
4970 replaceAction.y2 = y;
4974 replaceAction.x1 = x;
4975 replaceAction.y1 = y;
4976 replaceAction.x2 = selX;
4977 replaceAction.y2 = selY;
4979 Record(replaceAction);
4980 undoBuffer.dontRecord++;
4984 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4985 Record(addCharAction);
4991 DelSel(&addedSpaces);
4992 if(this.lineCount+1 > this.maxLines)
4995 Emptyline(this.lines.first,0);
4999 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
5001 // Make sure it fits, but we need a default line is this.font is too big for window
5002 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
5005 if((this.y >= 0) && this.y < this.viewY)
5010 line = EditLine { };
5013 lines.Insert(this.line, line);
5014 line.editBox = this;
5020 // If we're displacing the lines from a current line ...
5023 if(this.line.buffer)
5026 if(this.line.count < endX) endX = this.line.count;
5027 length = this.line.count - endX;
5030 if(!line.AdjustBuffer(length))
5034 if(this.line.buffer)
5036 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
5038 if(endX > 4000 || endX < 0)
5041 this.line.count = endX;
5042 this.line.buffer[this.line.count] = '\0';
5043 this.line.AdjustBuffer(this.line.count);
5044 ComputeLength(this.line);
5048 BufferLocation before = { this.line, this.y, this.x }, after;
5053 line.count = length;
5056 if(length > 4000 || length < 0)
5059 ComputeLength(this.line);
5064 line.buffer[line.count] = '\0';
5067 after.line = this.line, after.y = this.y, after.x = this.x;
5069 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5075 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5076 DelSel(&addedSpaces);
5077 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5078 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5079 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5083 this.selLine = this.line;
5087 replaceAction.x3 = x;
5088 replaceAction.y3 = y;
5089 replaceAction.addedSpaces = addedSpaces;
5090 replaceAction.addedTabs = addedTabs;
5091 undoBuffer.dontRecord--;
5095 addCharAction.x -= addedTabs * (tabSize-1);
5096 addCharAction.addedSpaces = addedSpaces;
5097 addCharAction.addedTabs = addedTabs;
5104 /****************************************************************************
5106 ****************************************************************************/
5109 bool AddCh(unichar ch)
5111 return _AddCh(ch, null, null);
5116 this.modified = true;
5117 NotifyUpdate(master, this);
5118 modifiedDocument = true;
5121 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5124 DelCh(line1, y1, x1, line2, y2, x2, false);
5125 SetViewToCursor(true);
5133 itemEditUndo.disabled = undoBuffer.curAction == 0;
5134 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5135 if(savedAction == undoBuffer.curAction)
5137 modifiedDocument = false;
5139 NotifyUnsetModified(master, this);
5146 itemEditUndo.disabled = undoBuffer.curAction == 0;
5147 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5148 if(savedAction == undoBuffer.curAction)
5150 modifiedDocument = false;
5152 NotifyUnsetModified(master, this);
5156 void Record(UndoAction action)
5158 if(!undoBuffer.dontRecord)
5160 undoBuffer.Record(action);
5161 itemEditUndo.disabled = undoBuffer.curAction == 0;
5162 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5169 this.lines.first, 0,0,
5170 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5173 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5178 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5179 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5180 this.y = line2 ? y2 : (this.lineCount-1);
5181 this.line = line2 ? (EditLine)line2 : this.lines.last;
5184 SetViewToCursor(true);
5188 // TODO: Fix this vs modifiedDocument window property
5189 void SetModified(bool flag)
5193 this.modified = false;
5194 if(flag && !NotifyModified(master, this))
5195 this.modified = true;
5200 bool AddS(char * string)
5207 int addedSpaces = 0, addedTabs = 0;
5208 AddTextAction action = null;
5209 ReplaceTextAction replaceAction = null;
5211 this.pasteOperation = true;
5213 if(style.stuckCaret /*|EES_READONLY)*/ )
5216 if(!undoBuffer.dontRecord)
5218 char * placeString = CopyString(string);
5219 if(!style.multiLine)
5223 for(i = 0; (ch = placeString[i]); i++)
5226 placeString[i] = '\0';
5227 placeString = renew placeString byte[i+1];
5232 if(selX != x || selY != y)
5236 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5237 oldString = new char[len];
5238 newString = placeString;
5239 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5241 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5242 if(selY < y || (selY == y && selX < x))
5244 replaceAction.x1 = selX;
5245 replaceAction.y1 = selY;
5246 replaceAction.x2 = x;
5247 replaceAction.y2 = y;
5251 replaceAction.x1 = x;
5252 replaceAction.y1 = y;
5253 replaceAction.x2 = selX;
5254 replaceAction.y2 = selY;
5256 Record(replaceAction);
5260 if(string[0] == '\n')
5261 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5263 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5271 undoBuffer.dontRecord++;
5272 DelSel(&addedSpaces);
5276 for(c = 0; string[c]; c++)
5278 if(string[c] == '\n' || string[c] == '\r')
5280 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5285 if(string[c] == '\n')
5294 // Reset for next line
5298 if(string[c] == '\r' && *line == '\n')
5310 // Why was this here?
5313 // Add the line here
5315 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5321 undoBuffer.dontRecord--;
5326 action.addedSpaces = addedSpaces;
5327 action.addedTabs = addedTabs;
5329 else if(replaceAction)
5331 replaceAction.y3 = y;
5332 replaceAction.x3 = x;
5333 replaceAction.addedSpaces = addedSpaces;
5334 replaceAction.addedTabs = addedTabs;
5337 UpdateCaretPosition(true);
5339 this.pasteOperation = false;
5352 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5353 SetViewToCursor(true);
5359 void PutCh(unichar ch)
5363 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5364 //if((ch >= 32) || ch == '\n')
5366 int addedSpaces = 0, addedTabs = 0;
5367 ReplaceTextAction replaceAction = null;
5368 AddCharAction addCharAction = null;
5371 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5373 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5378 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5379 oldString = new char[len];
5380 UTF32toUTF8Len(&ch, 1, buffer, 4);
5381 newString = CopyString(buffer);
5382 GetText(oldString, line, y, x, line, y, x+1, false, false);
5383 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5384 replaceAction.x1 = x;
5385 replaceAction.y1 = y;
5386 replaceAction.x2 = x+1;
5387 replaceAction.y2 = y;
5388 Record(replaceAction);
5390 undoBuffer.dontRecord++;
5391 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5392 undoBuffer.dontRecord--;
5394 else if(!undoBuffer.dontRecord)
5396 if(selX != x || selY != y)
5401 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5402 oldString = new char[len];
5403 UTF32toUTF8Len(&ch, 1, buffer, 4);
5404 newString = CopyString(buffer);
5405 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5406 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5407 if(selY < y || (selY == y && selX < x))
5409 replaceAction.x1 = selX;
5410 replaceAction.y1 = selY;
5411 replaceAction.x2 = x;
5412 replaceAction.y2 = y;
5416 replaceAction.x1 = x;
5417 replaceAction.y1 = y;
5418 replaceAction.x2 = selX;
5419 replaceAction.y2 = selY;
5421 Record(replaceAction);
5425 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5426 Record(addCharAction);
5429 undoBuffer.dontRecord++;
5430 result = _AddCh(ch, &addedSpaces, &addedTabs);
5433 replaceAction.x3 = x;
5434 replaceAction.y3 = y;
5435 replaceAction.addedSpaces = addedSpaces;
5436 replaceAction.addedTabs = addedTabs;
5440 addCharAction.x -= addedTabs * (tabSize-1);
5441 addCharAction.addedSpaces = addedSpaces;
5442 addCharAction.addedTabs = addedTabs;
5444 undoBuffer.dontRecord--;
5448 if(result) SetViewToCursor(true);
5452 void PutS(char * string)
5457 SetViewToCursor(true);
5462 void Printf(char * format, ...)
5466 char temp[MAX_F_STRING];
5468 va_start(args, format);
5469 vsnprintf(temp, sizeof(temp), format, args);
5470 temp[sizeof(temp)-1] = 0;
5476 void SetContents(char * format, ...)
5480 undoBuffer.dontRecord++;
5482 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5485 char temp[MAX_F_STRING];
5487 va_start(args, format);
5488 vsnprintf(temp, sizeof(temp), format, args);
5489 temp[sizeof(temp)-1] = 0;
5496 undoBuffer.dontRecord--;
5509 x -= 1 + _DelCh(line, y, x-1, line, y, x, true, false, null);
5512 else if(this.line.prev)
5514 EditLine line = this.line.prev;
5518 _DelCh(line, this.y-1, x, this.line, this.y, this.x, true, false, null);
5526 this.selLine = this.line;
5531 SetViewToCursor(true);
5536 Emptyline(this.line,this.y);
5537 this.selX = this.x = 0;
5540 this.selLine = this.line;
5542 SetViewToCursor(true);
5552 SetViewToCursor(true);
5560 SetViewToCursor(true);
5564 bool GoToLineNum(int lineNum)
5569 EditLine line = this.lines.first;
5570 for(c = 0; c < lineNum && line; c++, line = line.next);
5580 SetViewToCursor(true);
5587 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5588 bool GoToPosition(EditLine line, int y, int x)
5600 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5614 SetViewToCursor(true);
5621 void SetViewToCursor(bool setCaret)
5631 bool dontScroll = false;
5637 selected = selX != this.x || selY != y;
5644 checkLine = dropLine;
5650 checkLine = this.line;
5655 numLines = clientSize.h / space.h;
5657 // This is broken. The EditBox now doesn't do anything different when adding to it,
5658 // regardless of the previous scrolling position. It should be read and then set again
5659 // if one wishes to preserve it.
5660 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5661 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5665 // Horizontal Adjustment
5666 if(!dontScroll && checkLine)
5670 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5673 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5679 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5680 viewX = x - clientSize.w+space.w;
5681 if(x < this.viewX + clientSize.w/2 - space.w)
5682 viewX = Max(0, x - clientSize.w/2 + space.w);
5690 // Vertical Adjustment
5691 if(viewY > checkY) viewY = checkY;
5692 if(viewY + numLines <= checkY)
5694 if(clientSize.h >= space.h)
5696 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5706 for(;dropLine && dropLine.prev && dropY >= numLines;)
5708 dropLine = dropLine.prev;
5712 for(;this.line && this.line.prev && this.y >= numLines;)
5714 this.line = this.line.prev;
5719 SetScrollPosition(viewX, viewY * this.space.h);
5721 UpdateCaretPosition(setCaret);
5727 selLine = this.line;
5737 void CenterOnCursor()
5739 int numLines = clientSize.h / this.space.h;
5740 int y = this.y - numLines / 2;
5742 bool figureSyntax = false;
5743 EditLine oldViewLine = viewLine;
5744 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5749 for(;y < this.viewY; y++)
5751 this.viewLine = this.viewLine.prev;
5752 style.recomputeSyntax = true;
5754 for(;y > this.viewY; y--)
5756 this.viewLine = this.viewLine.next;
5757 figureSyntax = true;
5760 FigureStartSyntaxStates(oldViewLine, false);
5764 SetScrollPosition(this.viewX, viewY * this.space.h);
5765 UpdateCaretPosition(true);
5769 void SetCursorToView()
5780 numLines = clientSize.h / this.space.h;
5784 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5785 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5789 EditLine oldLine = this.line;
5790 bool lastOne = false;
5791 EditLine oldViewLine = this.viewLine;
5792 bool figureSyntax = false;
5794 if(this.y >= this.lineCount-1) return;
5796 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5798 if(this.viewY + numLines < this.lines.count)
5800 this.viewLine = this.viewLine.next;
5802 figureSyntax = true;
5804 else if(c && !lastOne)
5813 FigureStartSyntaxStates(oldViewLine, false);
5814 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5817 SetViewToCursor(false);
5826 if(this.y == 0) return;
5828 numLines = clientSize.h / this.space.h;
5832 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5833 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5837 EditLine oldLine = this.line;
5839 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5844 if(this.viewLine.prev)
5846 this.viewLine = this.viewLine.prev;
5848 style.recomputeSyntax = true;
5852 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5855 SetViewToCursor(false);
5862 if(this.viewLine.prev)
5865 // this.viewLine = this.viewLine.prev;
5868 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5875 if(this.viewLine.next)
5878 // this.viewLine = this.viewLine.next;
5881 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5888 EditLine l1, l2, line;
5889 int x1, x2, nx1, nx2;
5894 if(!this.selLine) return 0;
5895 if(this.selLine == this.line && this.selX == this.x) return 0;
5896 if(this.selY < this.y)
5903 else if(this.selY > this.y)
5910 else if(this.selX < this.x)
5912 l1 = l2 = this.line;
5918 l1 = l2 = this.line;
5922 nx1 = Min(x1,l1.count);
5923 nx2 = Min(x2,l2.count);
5925 // Find Number of Bytes Needed
5927 for(line = l1; line; line = line.next)
5929 if(line == l1) start = nx1; else start = 0;
5930 if(line == l2) end = nx2; else end = line.count;
5932 if(style.freeCaret && line == l2)
5935 count = Max(x2-Max(x1,l1.count),0);
5937 count = Max(x2-l2.count,0);
5941 if(line == l2) break;
5942 // Add Carriage Return / line Feed
5949 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5953 if(!reorder || this.selY < this.y)
5955 if(l1) *l1 = this.selLine;
5956 if(y1) *y1 = this.selY;
5957 if(x1) *x1 = this.selX;
5958 if(l2) *l2 = this.line;
5959 if(y2) *y2 = this.y;
5960 if(x2) *x2 = this.x;
5962 else if(this.selY > this.y)
5964 if(l1) *l1 = this.line;
5965 if(y1) *y1 = this.y;
5966 if(x1) *x1 = this.x;
5967 if(l2) *l2 = this.selLine;
5968 if(y2) *y2 = this.selY;
5969 if(x2) *x2 = this.selX;
5971 else if(this.selX < this.x)
5973 if(l1) *l1 = this.line;
5974 if(y1) *y1 = this.selY;
5975 if(x1) *x1 = this.selX;
5976 if(l2) *l2 = this.line;
5977 if(y2) *y2 = this.y;
5978 if(x2) *x2 = this.x;
5982 if(l1) *l1 = this.line;
5983 if(y1) *y1 = this.y;
5984 if(x1) *x1 = this.x;
5985 if(l2) *l2 = this.line;
5986 if(y2) *y2 = this.selY;
5987 if(x2) *x2 = this.selX;
5992 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5994 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5996 this.selLine = (EditLine)l1;
5999 this.line = (EditLine)l2;
6003 SetViewToCursor(true);
6007 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
6009 EditLine l1, l2, line;
6010 int x1, x2, nx1, nx2;
6041 nx1 = Min(x1,l1.count);
6042 nx2 = Min(x2,l2.count);
6045 for(line = l1; line; line = line.next)
6047 if(line == l1) start = nx1; else start = 0;
6048 if(line == l2) end = nx2; else end = line.count;
6051 CopyBytes(text, line.buffer + start, end - start);
6054 numChars += end-start;
6056 if(style.freeCaret && line == l2 && addSpaces)
6059 count = Max(x2-Max(x1,l1.count),0);
6061 count = Max(x2-l2.count,0);
6064 FillBytes(text,' ',count);
6070 if(line == l2) break;
6082 // '\0' terminate Terminate
6089 void GetSel(char * text, bool addCr)
6091 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6094 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6112 int size = SelSize();
6115 // Try to allocate memory
6116 ClipBoard clipBoard { };
6117 if(clipBoard.Allocate(size+1))
6119 GetSel(clipBoard.memory, true);
6132 ClipBoard clipBoard { };
6133 if(clipBoard.Load())
6134 PutS(clipBoard.memory);
6146 SetViewToCursor(true);
6152 void DeleteSelection()
6157 SetViewToCursor(true);
6163 void Save(File f, bool cr)
6166 savedAction = undoBuffer.curAction;
6168 for(line = this.lines.first; line; line = line.next)
6170 f.Write(line.buffer, line.count, 1);
6173 if(cr) f.Putc('\r');
6179 #define BUFFER_SIZE 16384
6182 undoBuffer.dontRecord++;
6185 char buffer[BUFFER_SIZE];
6189 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6190 buffer[count] = '\0';
6196 undoBuffer.dontRecord--;
6197 undoBuffer.count = 0;
6198 undoBuffer.curAction = 0;
6199 itemEditUndo.disabled = undoBuffer.curAction == 0;
6200 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6203 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6207 bool firstPass = true;
6208 EditBoxFindResult result = found;
6210 if(!this.line) return notFound;
6213 for(line = this.line;;)
6221 line = this.lines.first;
6227 line = this.lines.last;
6228 num = this.lineCount - 1;
6234 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6236 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6240 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6243 if(line == this.line && !firstPass) break;
6261 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6265 int searchLen = strlen(text);
6266 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6268 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6269 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6271 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6278 bool OnKeyDown(Key key, unichar ch)
6281 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6283 if(!NotifyKeyDown(master, this, key, ch))
6293 this.mouseMove = false;
6294 OnLeftButtonUp(0,0,0);
6295 SetViewToCursor(true);
6305 public class EditBoxStream : File
6308 BufferLocation start, sel;
6315 EditBox editBox = this.editBox;
6317 editBox.x = start.x;
6318 editBox.y = start.y;
6319 editBox.line = start.line;
6321 editBox.selX = sel.x;
6322 editBox.selY = sel.y;
6323 editBox.selLine = sel.line;
6325 editBox.SetViewToCursor(true);
6326 //editBox.ComputeColumn();
6330 property EditBox editBox
6338 start.line = value.line;
6342 sel.line = value.selLine;
6345 value.GoToHome(true);
6348 get { return editBox; }
6351 uint Read(byte * buffer, uint size, uint count)
6354 EditBox editBox = this.editBox;
6355 EditLine line = editBox.line;
6361 for(;read < count && line; line = (*&line.next))
6363 int numBytes = Min(count - read, (*&line.count) - x);
6366 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6371 for(;read < count && x < (*&line.count);)
6373 buffer[read++] = (*&line.buffer)[x++];
6380 buffer[read++] = '\n';
6385 if(x == (*&line.count) && (*&line.next))
6394 editBox.line = editBox.selLine = line;
6395 editBox.x = editBox.selX = x;
6396 editBox.y = editBox.selY = y;
6401 bool Seek(int pos, FileSeekMode mode)
6404 EditBox editBox = this.editBox;
6405 EditLine line = editBox.line;
6407 if(mode == FileSeekMode::start)
6409 pos = pos - this.pos;
6419 for(;read < pos && line; line = (*&line.next))
6421 int numBytes = Min(pos - read, (*&line.count) - x);
6424 read += numBytes; x += numBytes;
6427 /*for(;read < pos && x < (*&line.count);)
6455 for(;read < pos && line; line = (*&line.prev))
6457 int numBytes = Min(pos - read, x);
6463 /*for(;read < pos && x > 0;)
6473 x = (*&(*&line.prev).count);
6489 editBox.line = editBox.selLine = line;
6490 editBox.x = editBox.selX = x;
6491 editBox.y = editBox.selY = y;
6496 bool Puts(char * string)
6498 EditBox editBox = this.editBox;
6499 BufferLocation start { editBox.line, editBox.y, editBox.x };
6503 editBox.AddS(string);
6505 pos.line = editBox.line;
6509 this.start.AdjustAdd(start, pos);
6510 sel.AdjustAdd(start, pos);
6515 // NOTE: BYTE, NOT UNICODE CHARACTER!
6518 EditBox editBox = this.editBox;
6519 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6524 utf8Bytes[numBytes++] = ch;
6525 utf8Bytes[numBytes] = 0;
6526 if(UTF8Validate(utf8Bytes))
6528 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6530 pos.line = editBox.line;
6533 this.start.AdjustAdd(start, pos);
6534 sel.AdjustAdd(start, pos);
6541 bool Getc(char * ch)
6543 return Read(ch, 1, 1) ? true : false;
6546 void DeleteBytes(uint count)
6548 EditBox editBox = this.editBox;
6551 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6552 BufferLocation end = pos;
6557 for(;c < count && end.line && end.x < end.line.count;)
6562 if(c < count && end.line && end.line.next)
6564 end.line = end.line.next;
6573 start.AdjustDelete(pos, end);
6574 sel.AdjustDelete(pos, end);
6576 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);