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]; \
52 if(b & 0x80 && b & 0x40) \
67 for(i = 0; i<numBytes; i++) \
70 ch |= (string)[i] & mask; \
78 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
79 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
82 bool inMultiLineComment:1, inPrep:1, escaped:1;
84 bool recomputeSyntax:1;
85 bool cursorFollowsView:1;
87 // bool lineNumbers:1;
92 void UnregisterClass_EditBox()
95 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
104 extern int __ecereVMethodID_class_OnFree;
107 static class ArrayImpl
114 public class OldArray
121 void ** array = (void **)((ArrayImpl)this).array;
122 if(type.type == normalClass || type.type == noHeadClass)
124 for(c = 0; c<size; c++)
125 type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
127 // TODO: Call OnFree for structClass
128 delete ((ArrayImpl)this).array;
137 if(((ArrayImpl)this).array)
141 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
144 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
153 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
158 public class UndoAction : struct
161 subclass(UndoAction) type;
162 virtual void Undo(void * data) { type.Undo(this, data); }
163 virtual void Redo(void * data) { type.Redo(this, data); }
165 virtual void Print(void * data) { type.Print(this, data); }
169 if(((Class)type).Destructor)
170 ((void (*)(void *))((Class)type).Destructor)(this);
175 class ArrayUndoActions : OldArray
177 type = class(UndoAction);
181 public class UndoBuffer
183 ArrayUndoActions actions { size = 8 };
198 UndoAction action = actions._[--curAction];
200 /*Print("Undoing: ");
201 action.Print(data);*/
212 if(curAction < count)
214 UndoAction action = actions._[curAction];
217 /*Print("Redoing: ");
218 action.Print(data);*/
226 void Record(UndoAction action)
228 if(!dontRecord && !insideRedo)
230 if(curAction < count)
233 for(c = curAction; c < count; c++)
239 if(count >= actions.size)
240 actions.size += actions.size / 2;
243 /*Print("Recording: ");
244 action.Print(data);*/
246 actions._[count++] = action;
249 if(actions.size > count + count / 2 && count + count / 2 >= 8)
250 actions.size = count + count / 2;
257 static class AddCharAction : UndoAction
261 int addedSpaces, addedTabs;
262 type = class(AddCharAction);
264 void Undo(EditBox editBox)
266 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
268 if(addedTabs || addedSpaces)
269 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
270 editBox.UpdateDirty();
273 void Redo(EditBox editBox)
275 editBox.GoToPosition(null, y, x);
277 editBox.UpdateDirty();
280 void Print(EditBox editBox)
282 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
287 static class AddTextAction : UndoAction
291 int addedSpaces, addedTabs;
292 type = class(AddTextAction);
295 void Print(EditBox editBox)
297 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
305 void Undo(EditBox editBox)
310 editBox.GoToPosition(null, y1, x1);
312 l2 = editBox.lines.first;
313 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
315 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
316 if(addedTabs || addedSpaces)
317 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
319 editBox.SetViewToCursor(true);
323 void Redo(EditBox editBox)
325 editBox.GoToPosition(null, y1, x1);
326 editBox.PutS(string);
330 static class DelTextAction : UndoAction
336 type = class(DelTextAction);
339 void Print(EditBox editBox)
341 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
344 void Undo(EditBox editBox)
346 editBox.GoToPosition(null, y1, x1);
347 editBox.PutS(string);
351 editBox.GoToPosition(null, y1, x1);
354 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
355 //editBox.SetViewToCursor(true);
358 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
364 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
365 //editBox.SetViewToCursor(true);
368 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
372 void Redo(EditBox editBox)
377 editBox.GoToPosition(null, y1, x1);
380 l2 = editBox.lines.first;
381 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
383 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
384 editBox.SetViewToCursor(true);
394 static class ReplaceTextAction : UndoAction
396 int y1, x1, y2, x2, y3, x3;
400 int addedSpaces, addedTabs;
402 type = class(ReplaceTextAction);
405 void Print(EditBox editBox)
407 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
410 void Undo(EditBox editBox)
415 editBox.GoToPosition(null, y1, x1);
417 l3 = editBox.lines.first;
418 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
420 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
422 editBox.PutS(oldString);
423 if(addedSpaces || addedTabs)
424 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
426 //editBox.PutS(oldString);
429 editBox.GoToPosition(null, y1, x1);
432 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
438 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
442 void Redo(EditBox editBox)
447 editBox.GoToPosition(null, y1, x1);
449 l2 = editBox.lines.first;
450 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
452 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
454 editBox.PutS(newString);
464 static class MoveTextAction : UndoAction
466 int fy1, fx1, fy2, fx2;
468 type = class(MoveTextAction);
470 void Undo(EditBox editBox)
475 void Redo(EditBox editBox)
481 public class EditLine : struct
493 // Only works for single line edit for now...
494 EditBox editBox = this.editBox;
498 get { return this ? buffer : null; }
500 property EditLine prev { get { return this ? prev : null; } };
501 property EditLine next { get { return this ? next : null; } };
502 property int count { get { return this ? count : 0; } };
509 // This makes sure the buffer always contains at least count characters
510 // Keeps a count/2 pad for avoiding always reallocating memory.
511 bool AdjustBuffer(int count)
519 newSize = (count + (count >> 1));
521 // Shrink down the buffer
524 buffer = new char[newSize];
525 if(!buffer) return false;
529 CopyBytes(buffer, this.buffer, count);
532 this.buffer = buffer;
536 // Increase the buffer
537 else if(size < count)
539 buffer = new char[newSize];
540 if(!buffer) return false;
544 CopyBytes(buffer, this.buffer, this.count + 1); // size);
547 this.buffer = buffer;
556 public struct BufferLocation
561 void AdjustDelete(BufferLocation start, BufferLocation end)
563 // Location is before, nothing to do
564 if(y < start.y || (y == start.y && x < start.x))
566 // Location is inside deleted bytes, point to the start
567 if((y >= start.y && (y > start.y || x >= start.x)) &&
568 (y >= end.y && (y > end.y || x >= end.x)))
573 // Location is on another line
575 y -= end.y - start.y;
576 // Location is the last touched line
583 //if(start.line == end.line)
584 x -= end.x - start.x;
593 // Assuming no carriage return before first character ???? fixed?
594 void AdjustAdd(BufferLocation start, BufferLocation end)
596 int numLines = end.y - start.y;
603 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
606 for(c = 0, line = start.line; c<numLines; c++)
609 //x += numLines ? end.x : (end.x - start.x);
610 x += end.x - start.x;
617 public enum EditBoxFindResult { notFound, found, wrapped };
619 static char * keyWords1[] =
622 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
623 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
625 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
626 "__attribute__", "__stdcall", "_stdcall",
627 "__declspec", "goto",
628 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
629 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
632 "class", "private", "public",
634 "delete", "new", "new0", "renew", "renew0", "define",
637 "dllexport", "dllimport", "stdcall",
638 "subclass", "__on_register_module", "namespace", "using",
639 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
640 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
641 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
644 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
647 "this", "true", "false", "null", "value",
656 static char * keyWords2[] =
658 "defined", "warning", null
661 static char ** keyWords[] = { keyWords1, keyWords2 };
662 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
663 //static int * keyLen[NUM_KEYWORD_GROUPS];
664 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
666 static char searchString[1025], replaceString[1025];
667 static bool matchCase = false, wholeWord = false, searchUp = false;
669 static GoToDialog goToDialog
671 autoCreate = false, isModal = true, text = $"Go To"
674 public class EditBox : CommonControl
676 class_property(icon) = "<:ecere>controls/editBox.png";
680 virtual bool Window::NotifyModified(EditBox editBox);
681 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
682 virtual void Window::NotifyUpdate(EditBox editBox);
683 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
684 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
685 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
687 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
688 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
689 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
691 virtual bool Window::NotifyUnsetModified(EditBox editBox);
693 // Why was this commented out?
694 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
695 // and the scrollArea property isn't compared properly either.
699 if(font) ComputeFont();
700 SetInitSize(initSize);
706 style.vScroll = true;
712 style.hScroll = true;
716 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
717 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
718 property bool readOnly
720 property_category $"Behavior"
723 style.readOnly = value;
724 itemEditCut.disabled = value || !selection;
725 itemEditDelete.disabled = value || !selection;
726 itemEditPaste.disabled = value;
728 get { return style.readOnly; }
730 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
731 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
732 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
733 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
734 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
735 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
736 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
737 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
738 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
739 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
740 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
741 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
742 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
743 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
744 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
745 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
746 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
747 property int numLines { get { return this ? lineCount : 0; } };
748 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
749 property int column { get { return col; } }; // TODO: Add Set
750 property int charPos { get { return x; } }; // TODO: Add Set
751 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
752 property EditLine lastLine { get { return lines.last; } };
753 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
754 property char * contents
756 property_category $"Data"
762 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
765 //SetViewToCursor(true);
773 char * buffer = null;
776 /* Can't implement this right now because of memory leak... Need string reference counting...
783 for(line = lines.first; line; line = line.next)
784 len += strlen(line.buffer);
786 buffer = new char[len+1];
788 for(line = lines.first; line; line = line.next)
790 int lineLen = strlen(line.buffer);
791 memcpy(buffer + len, line.buffer, lineLen);
797 buffer = this.line ? this.line.buffer : null;
802 property bool overwrite { get { return overwrite; } };
803 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
805 property char * multiLineContents
809 char * buffer = null;
816 for(line = lines.first; line; line = line.next)
817 len += strlen(line.buffer)+1;
819 buffer = new char[len+1];
821 for(line = lines.first; line; line = line.next)
823 int lineLen = strlen(line.buffer);
824 memcpy(buffer + len, line.buffer, lineLen);
826 if(line.next) buffer[len++] = '\n';
839 return this.line.buffer;
844 void SetLineText(char * text)
848 EditLine_SetText(this.line, text);
852 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
853 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
854 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
856 // selectionStart.line, selectionStart.column (With Set)
857 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
871 // Position of Caret (Not necessarily displayed position)
874 // Position of beginning of block (Block ends at (x,y))
876 // line is line at carret, selLine is line at beginning of block
877 EditLine line, selLine, dropLine;
878 // Mouse selection Moving virtual caret
883 // ViewX is x offset in pixels, ViewY is y offset in lines
885 // viewLine is first displayed line
888 // start and end of area to redraw
891 // MaxLine is the longest line
893 // MaxLength is the longest line's length
896 // MouseSelect is true once button is pressed, overwrite is true if mode is on
897 bool mouseSelect, mouseMove, overwrite, wordSelect;
898 // Timer is used for mouse selection scrolling
901 window = this, delay = 0.1;
910 OnMouseMove(mouseX, mouseY, -1);
915 // (mouseX,mouseY) is the position of the mouse in the edit box
920 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
923 bool rightButtonDown;
926 UndoBuffer undoBuffer { data = this };
928 Color selectionColor, selectionText;
929 SyntaxColorScheme colorScheme { };
934 Menu editMenu { menu, $"Edit", e };
937 editMenu, $"Cut\tCtrl+X", t, disabled = true;
939 bool NotifySelect(MenuItem item, Modifiers mods)
941 if(!(style.readOnly))
946 MenuItem itemEditCopy
948 editMenu, $"Copy\tCtrl+C", c, disabled = true;
950 bool NotifySelect(MenuItem item, Modifiers mods)
956 MenuItem itemEditPaste
958 editMenu, $"Paste\tCtrl+V", p;
960 bool NotifySelect(MenuItem item, Modifiers mods)
962 if(!(style.readOnly))
967 MenuItem itemEditDelete
969 editMenu, $"Delete\tDel", d, disabled = true;
971 bool NotifySelect(MenuItem item, Modifiers mods)
973 if(!(style.readOnly))
978 MenuDivider { editMenu };
979 MenuItem itemEditSelectAll
981 editMenu, $"Select All\tCtrl+A", a;
983 bool NotifySelect(MenuItem item, Modifiers mods)
989 MenuDivider { editMenu };
990 MenuItem itemEditUndo
992 editMenu, $"Undo\tCtrl+Z", u;
995 bool NotifySelect(MenuItem item, Modifiers mods)
1001 MenuItem itemEditRedo
1003 editMenu, $"Redo\tCtrl+Y", o;
1006 bool NotifySelect(MenuItem item, Modifiers mods)
1012 MenuDivider { editMenu };
1015 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1017 bool NotifySelect(MenuItem item, Modifiers mods)
1020 Find(searchString, wholeWord, matchCase, false);
1022 itemEditFind.NotifySelect(this, item, mods);
1028 editMenu, $"Find Next\tF3", n, f3;
1030 bool NotifySelect(MenuItem item, Modifiers mods)
1033 Find(searchString, wholeWord, matchCase, true);
1035 itemEditFind.NotifySelect(this, item, mods);
1039 MenuItem itemEditFind
1041 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1043 bool NotifySelect(MenuItem item, Modifiers mods)
1047 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1048 searchUp = searchUp;
1051 // Fix dialog from above shouldn't be visible
1052 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1053 void NotifyDestroyed(Window window, DialogResult result)
1055 FindDialog dialog = (FindDialog) window;
1056 searchUp = dialog.searchUp;
1057 strcpy(searchString, dialog.searchString);
1058 matchCase = dialog.matchCase;
1059 wholeWord = dialog.wholeWord;
1068 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1070 bool NotifySelect(MenuItem item, Modifiers mods)
1072 ReplaceDialog dialog
1076 searchString = searchString,
1077 replaceString = replaceString,
1078 matchCase = matchCase,
1079 wholeWord = wholeWord,
1083 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1084 void NotifyDestroyed(Window window, DialogResult result)
1086 ReplaceDialog dialog = (ReplaceDialog)window;
1087 char * replace = dialog.replaceString;
1089 strcpy(replaceString, replace);
1090 strcpy(searchString, dialog.searchString);
1091 matchCase = dialog.matchCase;
1092 wholeWord = dialog.wholeWord;
1099 MenuDivider { editMenu };
1102 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1104 bool NotifySelect(MenuItem item, Modifiers mods)
1106 goToDialog.editBox = this;
1107 goToDialog.master = master;
1108 goToDialog.Create();
1112 MenuDivider { editMenu };
1113 MenuItem itemEditInsertTab
1115 editMenu, $"Insert Tabs", i, checkable = true;
1117 bool NotifySelect(MenuItem item, Modifiers mods)
1119 style.useTab = item.checked;
1125 snapVertScroll = true;
1126 snapHorzScroll = true;
1130 static bool syntaxInit = false;
1135 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1137 for(c = 0; keyWords[g][c]; c++);
1138 //keyLen[g] = new int[c];
1139 for(c = 0; keyWords[g][c]; c++)
1141 keyLen[g][c] = strlen(keyWords[g][c]);
1145 colorScheme.commentColor = dimGray;
1146 colorScheme.charLiteralColor = crimson;
1147 colorScheme.stringLiteralColor = crimson;
1148 colorScheme.preprocessorColor = green;
1149 colorScheme.numberColor = teal;
1150 colorScheme.keywordColors = [ blue, blue ];
1153 FontExtent = Display::FontExtent;
1155 lines.offset = (uint)&((EditLine)0).prev;
1157 style = EditBoxBits { hScroll = true };
1159 // cursor = guiApp.GetCursor(IBeam);
1161 // Default Properties
1163 maxLineSize = 1024;*/
1166 maxLineSize = MAXINT;
1171 mouseSelect = this.mouseMove = false;
1174 x = selX = selY = 0;
1177 line = selLine = null;
1183 endY = clientSize.h;
1186 // Default space to 8 in case window is not constructed
1189 undoBuffer.dontRecord++;
1191 undoBuffer.dontRecord--;
1193 viewLine = lines.first;
1194 style.recomputeSyntax = true;
1200 UpdateCaretPosition(true);
1206 lines.Free(EditLine::Free);
1209 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1211 int count = wc - *renderStart;
1214 if(y + space.h >= box.top && y <= box.bottom)
1220 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1221 surface.TextFont(font);
1222 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1223 if(*x + w + XOFFSET > 0)
1224 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1229 w = numSpaces; // * space.w;
1230 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1231 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1232 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1240 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1241 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1247 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1251 *foreground = (*selected) ? selectionForeground : textColor;
1252 *background = selectionBackground;
1253 *opacity = *selected;
1260 if(this.overwrite && active)
1262 if((style.stuckCaret && wc == line.count && !line.next) ||
1263 (!mouseMove && line == this.line && wc == editX))
1272 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1276 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1277 bool inString = false;
1278 bool inQuotes = false;
1279 bool inPrep = reset ? false : style.inPrep;
1280 bool inSingleLineComment = false;
1281 bool escaped = reset ? false : style.escaped;
1282 bool continuedSingleLineComment = false;
1284 EditLine line = reset ? lines.first : firstLine;
1285 // int maxBackUp = 1000, c;
1286 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1287 for(; line != viewLine; line = line.next)
1289 char * text = line.buffer;
1292 bool lastWasStar = false;
1293 bool firstWord = true;
1294 if(!escaped) inPrep = false;
1295 inSingleLineComment = continuedSingleLineComment;
1301 for(c = 0; (ch = text[c]); c++)
1303 bool wasEscaped = escaped;
1304 bool backLastWasStar = lastWasStar;
1306 lastWasStar = false;
1309 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1311 if(text[c+1] == '/')
1313 inSingleLineComment = true;
1315 else if(text[c+1] == '*')
1317 inMultiLineComment = true;
1320 else if(backLastWasStar)
1321 inMultiLineComment = false;
1325 if(!c || text[c-1] != '/') lastWasStar = true;
1327 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && ch == '\"')
1329 if(inString && !wasEscaped)
1338 else if(!inSingleLineComment && !inMultiLineComment && !inString && ch == '\'')
1340 if(inQuotes && !wasEscaped)
1354 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && ch == '#')
1366 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1368 style.inMultiLineComment = inMultiLineComment;
1369 style.inPrep = inPrep;
1370 style.escaped = escaped;
1374 /*void OnDrawOverChildren(Surface surface)
1376 if(style.lineNumbers)
1378 int currentLineNumber = this.viewY + 1;
1381 for(i = 0; i * space.h < box.height; i++)
1383 // ********* LINE NUMBERING *********
1384 surface.SetForeground(Color{60, 60, 60});
1385 //Highlight current line
1386 if(this.caretY / space.h == currentLineNumber - 1)
1387 surface.SetBackground(Color{220, 220, 220});
1389 surface.SetBackground(Color{230, 230, 230});
1390 surface.textOpacity = true;
1392 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1393 if(currentLineNumber > this.lineCount)
1394 surface.WriteText(0,i*space.h+1," ",6);
1396 surface.WriteText(0,i*space.h+1,lineText,6);
1398 currentLineNumber++;
1403 void OnRedraw(Surface surface)
1407 bool selected = false, selection = true;
1409 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1410 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1411 Color defaultTextColor = property::foreground;
1414 int maxW = clientSize.w;
1416 Color foreground, background;
1419 // Overwrite Caret Stuff
1420 bool overWrite = false;
1421 int overWriteX, overWriteY;
1424 // ****** SYNTAX STATES ******
1425 bool inMultiLineComment = style.inMultiLineComment;
1426 bool inString = false;
1427 bool inQuotes = false;
1428 bool inPrep = style.inPrep;
1429 bool inSingleLineComment = false;
1430 bool escaped = style.escaped;
1431 bool continuedSingleLineComment = false;
1432 // ****** ************* ******
1435 defaultTextColor = Color { 85, 85, 85 };
1436 textColor = defaultTextColor;
1439 Abs(selectionBackground.r - property::background.r) +
1440 Abs(selectionBackground.g - property::background.g) +
1441 Abs(selectionBackground.b - property::background.b) < 92)
1443 selectionBackground = formColor;
1444 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1447 surface.TextFont(this.font);
1449 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1450 surface.Area(0,0,MAXINT,MAXINT);
1455 surface.SetBackground(formColor);
1456 surface.Area(0,0,clientSize.w, clientSize.h);
1459 if(this.selX == this.x && this.selY == this.y)
1469 editX = Min(this.x,this.line.count);
1470 selX = Min(this.selX,this.selLine.count);
1473 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1475 foreground = selected ? selectionForeground : textColor;
1476 background = selectionBackground;
1482 surface.SetForeground(foreground);
1483 surface.SetBackground(background);
1484 surface.TextOpacity(opacity);
1486 surface.GetBox(box);
1488 for(line = this.viewLine; line; line = line.next)
1490 int x = -this.viewX;
1494 Color newTextColor = textColor = defaultTextColor;
1495 bool lineComplete = false;
1498 // ****** SYNTAX HIGHLIGHTING ******
1499 bool lastWasStar = false;
1500 bool firstWord = true;
1501 if(!escaped) inPrep = false;
1502 inSingleLineComment = continuedSingleLineComment;
1506 // *********************************
1508 /* === DEBUGGING TOOL FOR MAXLINE ===
1510 if(line == this.maxLine)
1512 surface.SetBackground(GREEN|0xFF000000);
1513 surface.TextOpacity(true);
1517 surface.TextOpacity(selected ? true : false);
1518 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1522 if(line == this.selLine && line == this.line)
1524 end = Max(line.count, this.x);
1525 end = Max(end, this.selX);
1527 else if(line == this.selLine)
1528 end = Max(line.count, this.selX);
1529 else if(line == this.line)
1530 end = Max(line.count, this.x);
1538 bool spacing = true;
1539 bool cantHaveWords = false;
1546 lineComplete = false;
1549 textColor = newTextColor;
1551 surface.SetForeground(textColor);
1554 for(; c<end && !cantHaveWords;)
1558 bufferLen += wordLen;
1562 /*if(line.count > 4000)
1569 for(; c<line.count; c++)
1571 unichar ch = line.buffer[c];
1572 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1573 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1574 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1575 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1576 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1584 for(; c<line.count; c++)
1586 unichar ch = line.buffer[c];
1587 if(ch == '\t' || ch == ' ')
1589 cantHaveWords = true;
1593 if(ch != ' ' && ch != '\t')
1598 if(c == line.count && c < end)
1607 cantHaveWords = true;
1612 lastWasStar = false;
1618 bool backEscaped = escaped;
1619 bool backLastWasStar = lastWasStar;
1620 bool backInMultiLineComment = inMultiLineComment;
1621 bool backInString = inString;
1622 bool backInQuotes = inQuotes;
1623 bool backInPrep = inPrep;
1624 bool backInSingleLineComment = inSingleLineComment;
1626 char * word = line.buffer + c - wordLen;
1628 bool wasEscaped = escaped;
1630 lastWasStar = false;
1632 // Determine Syntax Highlighting
1633 newTextColor = defaultTextColor;
1636 if(inSingleLineComment || inMultiLineComment)
1638 newTextColor = colorScheme.commentColor;
1642 newTextColor = colorScheme.charLiteralColor;
1646 newTextColor = colorScheme.stringLiteralColor;
1650 newTextColor = colorScheme.preprocessorColor;
1652 if(wordLen == 1 && word[0] == '/')
1654 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1658 inSingleLineComment = true;
1659 newTextColor = colorScheme.commentColor;
1661 else if(word[1] == '*')
1663 inMultiLineComment = true;
1664 newTextColor = colorScheme.commentColor;
1667 else if(backLastWasStar)
1668 inMultiLineComment = false;
1670 else if(wordLen == 1 && word[0] == '*')
1672 if(!c || word[-1] != '/')
1675 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1677 if(inString && !wasEscaped)
1684 newTextColor = colorScheme.stringLiteralColor;
1687 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1689 if(inQuotes && !wasEscaped)
1694 newTextColor = colorScheme.charLiteralColor;
1697 else if(wordLen == 1 && word[0] == '\\')
1702 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1703 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1705 newTextColor = colorScheme.numberColor;
1709 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1714 newTextColor = colorScheme.preprocessorColor;
1717 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1719 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1721 char ** keys = keyWords[g];
1722 int * len = keyLen[g];
1723 for(ccc = 0; keys[ccc]; ccc++)
1725 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1727 newTextColor = colorScheme.keywordColors[g];
1736 // If highlighting for this word is different, break
1737 if(newTextColor != textColor)
1741 // Better solution than going back?
1744 // Reset syntax flags
1745 escaped = backEscaped;
1746 lastWasStar = backLastWasStar;
1747 inMultiLineComment = backInMultiLineComment;
1748 inString = backInString;
1749 inQuotes = backInQuotes;
1750 inPrep = backInPrep;
1751 inSingleLineComment = backInSingleLineComment;
1756 textColor = newTextColor;
1758 surface.SetForeground(textColor);
1764 // If we're not breaking, this can't be rendered as spacing anymore
1767 // Do word wrapping here
1773 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1778 w += numSpaces * space.w;
1780 if(x + viewX + w > maxW)
1783 lineComplete = true;
1788 bufferLen += wordLen;
1793 int renderStart = start;
1798 // Render checking if we need to split because of selection or to find where to draw insert caret
1799 for(wc = start; wc < start + bufferLen; wc++)
1801 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1802 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1803 if(overWrite == true)
1805 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1806 if(overWriteCh == '\t') overWriteCh = ' ';
1811 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1812 if(overWrite == true)
1820 surface.TextOpacity(opacity);
1821 surface.SetBackground(background);
1822 surface.SetForeground(foreground);
1829 if(wc < line.count && line.buffer[wc] == '\t')
1831 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1835 numSpaces += space.w;
1839 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1844 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1845 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1847 if(overWrite == true)
1854 surface.TextOpacity(opacity);
1855 surface.SetBackground(background);
1856 surface.SetForeground(foreground);
1859 if(style.freeCaret && selected)
1861 surface.SetBackground(selectionBackground);
1862 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1863 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1868 if(style.freeCaret && selected)
1870 surface.SetBackground(selectionBackground);
1871 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1875 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1878 if(y > box.bottom) // >=clientSize.h)
1884 surface.TextOpacity(true);
1885 surface.SetForeground(black);
1886 surface.SetBackground(Color {255,255,85});
1887 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1891 void FixScrollArea()
1893 if(style.hScroll || style.vScroll)
1895 int width = maxLength + XOFFSET;
1896 int height = lineCount * space.h;
1897 if(style.freeCaret && line)
1902 width = Max(line.length + (x - line.count) * space.w, maxLength);
1906 if(selX > selLine.count)
1907 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1912 SetScrollLineStep(8, space.h);
1913 SetScrollArea(width, height, true);
1917 void ComputeLength(EditLine line)
1924 for(c = 0; c < line.count; )
1933 for(len = 0; c < line.count; c += numBytes)
1935 ch = line.buffer[c];
1936 numBytes = UTF8_NUM_BYTES(ch);
1938 if(ch == ' ' || ch == '\t')
1945 if(!len && ch == ' ')
1950 else if(!len && ch == '\t')
1952 w = (tabSize * space.w) - (x % (tabSize * space.w));
1956 FontExtent(display, font, line.buffer + start, len, &w, null);
1967 if(line.length > this.maxLength)
1969 this.maxLine = line;
1970 this.maxLength = line.length;
1979 this.maxLine = null;
1981 for(line = lines.first; line; line = line.next)
1983 if(line.length > this.maxLength)
1985 this.maxLength = line.length;
1986 this.maxLine = line;
1993 if(this.selY != this.y)
1995 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
1999 void ComputeColumn()
2001 // Adjust new edit X position according to tabs
2002 int c, position = 0;
2005 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2007 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2008 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2010 position += this.tabSize - (position % this.tabSize);
2014 position += this.x - c;
2015 this.col = position;
2018 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2020 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2023 bool HasCommentOrEscape(EditLine line)
2025 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2030 for(c = line.count-1; c >= 0; c--)
2032 char ch = line.buffer[c];
2038 else //if(ch != ' ' && ch != '\t')
2045 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2047 EditLine line = l1, next;
2049 int oldCount1, oldCount2;
2050 int y, firstViewY, firstY, firstDropY, firstSelY;
2053 DelTextAction action = null;
2054 bool hadComment = false;
2058 hadComment = HasCommentOrEscape(line);
2060 if(y2 > y1 || c2 > c1)
2062 if(start < l1.count)
2064 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2068 oldCount1 = l1.count;
2070 while(c1 < oldCount1)
2072 byte ch = buffer[c1];
2073 if(UTF8_IS_FIRST(ch)) break;
2077 oldCount2 = l2.count;
2079 while(c2 < oldCount2)
2081 byte ch = buffer[c2];
2082 if(UTF8_IS_FIRST(ch)) break;
2087 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2092 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2093 string = new char[len];
2094 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2095 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2099 //oldCount1 = l1.count;
2100 //oldCount2 = l2.count;
2103 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2104 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2107 if(c2 > oldCount2) c2 = oldCount2;
2108 if(!(style.freeCaret))
2109 if(c1 > oldCount1) c1 = oldCount1;
2110 newLineCount = c1 + l2.count-c2;
2116 buffer = new char[line.size ? line.size : 1];
2118 buffer = new char[line.size];
2120 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2125 if(!line.AdjustBuffer(newLineCount))
2129 /*if(newLineCount > 4000 || newLineCount < 0)
2130 printf("Warning");*/
2132 line.count = newLineCount;
2134 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2139 action.addedSpaces = c1-oldCount1;
2141 if(action.addedSpaces > action.x1)
2147 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2148 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2150 line.buffer[line.count] = '\0';
2157 this.dropX -= c2-c1;
2158 this.dropX = Max(this.dropX,0);
2160 this.x = Max(this.x,0);
2167 firstViewY = this.viewY;
2169 firstDropY = this.dropY;
2170 firstSelY = this.selY;
2171 for(line = l1;line;line = next, y++)
2179 if(line == this.viewLine)
2181 if(this.viewLine.next)
2183 this.viewLine = this.viewLine.next;
2185 style.recomputeSyntax = true;
2189 this.viewLine = this.viewLine.prev;
2191 style.recomputeSyntax = true;
2194 else if(y < firstViewY)
2196 if(line == this.line)
2200 this.line = this.line.next;
2201 this.x = this.line.count;
2206 this.line = this.line.prev;
2207 this.x = this.line.count;
2214 if(line == this.dropLine)
2216 if(this.dropLine.next)
2218 this.dropLine = this.dropLine.next;
2219 this.dropX = this.dropLine.count;
2223 this.dropLine = this.dropLine.prev;
2224 this.dropX = this.dropLine.count;
2228 else if(y < firstDropY)
2230 if(line == this.selLine)
2232 if(this.selLine.next)
2234 this.selLine = this.selLine.next;
2235 this.selX = this.selLine.count;
2239 this.selLine = this.selLine.prev;
2240 this.selX = this.selLine.count;
2244 else if(y < firstSelY)
2248 if(line == l2) break;
2252 if(style.autoSize) AutoSize();
2253 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2256 style.recomputeSyntax = true;
2261 bool DelSel(int * addedSpacesPtr)
2263 if(this.line != this.selLine || this.x != this.selX)
2265 if(this.selY < this.y)
2267 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2270 this.line = this.selLine;
2272 else if(this.selY > this.y)
2274 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2277 this.selLine = this.line;
2279 else if(this.selX < this.x)
2281 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2284 this.line = this.selLine;
2288 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2291 this.selLine = this.line;
2299 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2301 bool hadComment = false;
2302 // Add the line here
2303 EditLine line = this.line;
2305 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2306 if(!style.hScroll && created)
2308 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2313 // Lock if no place to display.
2316 else if(endX > this.x)
2317 max = Max(this.x, line.count);
2321 for(x = 0, c = 0; c < max+count; )
2326 if(c < Min(this.x, line.count))
2327 string = line.buffer + c;
2330 else if(c < endX + count)
2331 string = stringLine + c - endX;
2333 string = line.buffer + c - endX - count;
2337 w = (tabSize * space.w) - (x % (tabSize * space.w));
2341 numBytes = UTF8_NUM_BYTES(*string);
2342 FontExtent(display, this.font, string, numBytes, &w, null);
2346 if(x >= clientSize.w)
2351 c += numBytes; // - 1;
2357 int addedSpaces = 0;
2360 // Add blank spaces if EES_FREECARET
2361 if(this.x > line.count)
2371 for(c = 0; c<line.count; c++)
2373 if(this.line.buffer[c] == '\t')
2374 position += this.tabSize - (position % this.tabSize);
2378 wantedPosition = position + (this.x - line.count);
2380 // A tab is too much...
2381 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2382 addedSpaces = wantedPosition - position;
2387 position += this.tabSize - (position % this.tabSize);
2388 // Add as many tabs as needed
2389 addedTabs += (wantedPosition - position) / this.tabSize;
2390 position += (addedTabs-1) * this.tabSize;
2391 // Finish off with spaces
2392 addedSpaces = wantedPosition - position;
2396 addedSpaces = this.x - line.count;
2399 this.x = Min(this.x, line.count);
2401 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2407 hadComment = HasCommentOrEscape(line);
2409 // Adjust the size of the line
2410 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2414 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2417 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2418 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2421 *addedTabsPtr = addedTabs;
2422 FillBytes(line.buffer+line.count,'\t',addedTabs);
2424 if(addedTabs > 4000 || addedTabs < 0)
2427 line.count += addedTabs;
2433 FillBytes(line.buffer+line.count,' ',addedSpaces);
2435 if(addedSpaces > 4000 || addedSpaces < 0)
2438 line.count += addedSpaces;
2439 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2441 else if(addedSpacesPtr)
2442 *addedSpacesPtr = 0;
2444 if(count > 4000 || count < 0)
2447 line.count += count;
2448 this.x += count + addedTabs + addedSpaces;
2451 this.selLine = this.line;
2453 line.buffer[line.count] = '\0';
2455 ComputeLength(line);
2460 hasComment = HasCommentOrEscape(line);
2461 if(!undoBuffer.insideRedo)
2463 int backDontRecord = undoBuffer.dontRecord;
2464 undoBuffer.dontRecord = 0;
2465 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2466 if(style.autoSize) AutoSize();
2467 undoBuffer.dontRecord = backDontRecord;
2469 if(style.syntax && (hadComment || hasComment || line != this.line))
2471 style.recomputeSyntax = true;
2479 void Emptyline(EditLine line, int y)
2482 DelCh(line, y, 0, line.next, y+1, 0, true);
2484 DelCh(line, y, 0, line, y, line.count, true);
2487 void GoToEnd(bool deselect)
2491 this.line = this.lines.last;
2492 if(this.y != this.lineCount-1)
2494 else if (this.x != this.line.count)
2495 DirtyLine(this.lineCount-1);
2496 this.y = this.lineCount-1;
2497 this.x = this.line.count;
2504 void GoToHome(bool deselect)
2508 this.line = this.lines.first;
2511 else if (this.x !=0)
2512 DirtyLine(this.lineCount-1);
2521 // Returns true if it needs scrolling
2522 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2528 bool needHScroll = false;
2535 line = this.viewLine ? (void *)this.viewLine.prev : null;
2540 line = (void *)this.lines.first;
2545 py = Min(py, clientSize.h);
2547 py = Min(py, this.lineCount);
2549 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2555 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2558 px = Min(px,clientSize.w+this.space.w);
2562 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2565 if(tline) *tline = line;
2568 // Prevent divide by 0 from non valid this.font
2570 return (y < this.viewY) || needHScroll;
2572 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2576 // Minimal Update Management Functions
2580 this.endY = clientSize.h-1;
2581 // ErrorLog("DirtyAll\n");
2584 void DirtyEnd(int y)
2586 if((y - this.viewY)*this.space.h < this.startY)
2587 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2588 this.endY = clientSize.h-1;
2589 //ErrorLog("DirtyEnd %d\n", y);
2592 void DirtyLine(int y)
2596 if((y - this.viewY)*this.space.h < this.startY)
2597 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2598 if((y - this.viewY+1)*this.space.h > this.endY)
2599 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2601 //ErrorLog("DirtyLine %d\n", y);
2608 if(style.recomputeSyntax)
2610 FigureStartSyntaxStates(lines.first, true);
2611 style.recomputeSyntax = false;
2614 if(this.startY > this.endY) return;
2615 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2621 box.right = clientSize.w-1;
2622 box.top = this.startY;
2623 box.bottom = this.endY;
2626 this.startY = clientSize.h;
2630 bool IsMouseOnSelection()
2632 bool mouseOnSelection = false;
2635 int minY = Min(this.selY, this.y);
2636 int maxY = Max(this.selY, this.y);
2637 int minX = Min(this.selX, this.x);
2638 int maxX = Max(this.selX, this.x);
2640 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2642 if(maxX != minX || maxY != minY)
2644 if(y > minY && y < maxY)
2645 mouseOnSelection = true;
2646 else if(y == minY && y == maxY)
2647 mouseOnSelection = (x < maxX && x >= minX);
2651 mouseOnSelection = (x >= this.selX);
2652 else if(y == this.y)
2653 mouseOnSelection = (x >= this.x);
2658 mouseOnSelection = (x < this.selX);
2659 else if(y == this.y)
2660 mouseOnSelection = (x < this.x);
2663 return mouseOnSelection;
2666 void UpdateCaretPosition(bool setCaret)
2670 if(mouseMove || (!overwrite && !style.noCaret))
2672 int max = this.mouseMove ? this.dropX : this.x;
2673 int y = this.mouseMove ? this.dropY : this.y;
2674 EditLine line = this.mouseMove ? this.dropLine : this.line;
2676 if(!(style.freeCaret))
2677 max = Min(max, line.count);
2679 if(FontExtent && display)
2681 for(c = 0; c < max; )
2690 for(len = 0; c < Min(max, line.count); c += numBytes)
2692 ch = line.buffer[c];
2693 numBytes = UTF8_NUM_BYTES(ch);
2695 if(ch == ' ' || ch == '\t')
2702 if(!len && ch == ' ')
2707 else if(!len && ch == '\t')
2709 w = (tabSize * space.w) - (x % (tabSize * space.w));
2713 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2725 caretY = y * this.space.h;
2726 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2731 NotifyCaretMove(master, this, y + 1, x + 1);
2737 void SelectionEnables()
2739 if((x != selX || y != selY) && !selection)
2743 itemEditCut.disabled = false;
2744 itemEditDelete.disabled = false;
2746 itemEditCopy.disabled = false;
2748 this.selection = true;
2750 else if((x == selX && y == selY) && selection)
2752 itemEditCut.disabled = true;
2753 itemEditCopy.disabled = true;
2754 itemEditDelete.disabled = true;
2756 this.selection = false;
2760 void SetSelectCursor()
2762 if(!inactive || !style.noSelect)
2765 cursor = guiApp.GetCursor(arrow);
2766 else if(this.mouseSelect)
2767 cursor = guiApp.GetCursor(iBeam);
2770 if(IsMouseOnSelection())
2771 cursor = guiApp.GetCursor(arrow);
2773 cursor = guiApp.GetCursor(iBeam);
2778 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2781 int x = px ? *px : 0;
2788 if(c < Min(max, line.count))
2792 for(len = 0; c < Min(max, line.count); c += numBytes)
2794 ch = line.buffer[c];
2795 numBytes = UTF8_NUM_BYTES(ch);
2797 if(ch == ' ' || ch == '\t')
2804 if(!len && ch == ' ')
2809 else if(!len && ch == '\t')
2811 w = (tabSize * space.w) - (x % (tabSize * space.w));
2815 FontExtent(display, font, line.buffer + start, len, &w, null);
2819 if(style.freeCaret && c < max)
2828 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2833 int a = start + len;
2836 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2840 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2843 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2847 return Min(this.maxLineSize - 1, start + len);
2853 void SetCursorToViewX()
2855 bool selecting = this.x != selX || y != selY;
2857 // Horizontal Adjustment
2859 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2864 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2876 UpdateCaretPosition(false);
2882 void SetCursorToViewY()
2885 EditLine oldLine = this.line;
2887 bool selecting = this.x != this.selX || this.y != this.selY;
2889 numLines = clientSize.h / this.space.h;
2891 // Vertical Adjustment
2892 if(this.viewY > this.y)
2894 this.y = this.viewY;
2895 this.line = this.viewLine;
2898 if(this.viewY + numLines <= this.y)
2902 this.y = this.viewY-1;
2903 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2910 if(this.line != oldLine)
2912 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2920 this.selLine = this.line;
2923 UpdateCaretPosition(false);
2930 bool SaveFile(char * fileName)
2932 File f = eFile_Open(fileName, FO_WRITE);
2936 eWindow_SetModified(false);
2937 eInstance_Delete(f);
2944 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2948 if(!style.multiLine)
2950 x = (active && this.active && !style.readOnly) ? line.count : 0;
2953 SetViewToCursor(true);
2958 if(!active && modified)
2961 if(!NotifyModified(master, this))
2964 *goOnWithActivation = false;
2971 if(timer) timer.Stop();
2973 mouseSelect = false;
2985 // todo: resize width based on largest on-screen-line extent...
2987 display.FontExtent(font, " ", 1, null, &sh);
2991 nh = lineCount * sh;
2992 size.h = nh < minClientSize.h ? minClientSize.h : nh;
3003 int len = line ? strlen(line.text) : 0;
3004 GetDecorationsSize(&dw, &dh);
3005 display.FontExtent(font, " ", 1, null, &sh);
3006 if(len) display.FontExtent(font, line.text, len, &tw, null);
3008 if(nw < minClientSize.w) nw = minClientSize.w;
3010 if(nh < minClientSize.h) nh = minClientSize.h;
3016 bool OnResizing(int *w, int *h)
3022 *w = space.h * 80 / 14;
3026 void OnResize(int w, int h)
3029 if(!hasHorzScroll && !hasVertScroll && viewLine)
3030 SetViewToCursor(true);
3032 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3034 SetViewToCursor(true);
3039 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3041 if(style.readOnly) return true;
3042 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3043 // Middle button already serves as a dragger as well.
3048 bool OnRightButtonDown(int x, int y, Modifiers mods)
3050 this.rightButtonDown = true;
3055 bool OnRightButtonUp(int x, int y, Modifiers mods)
3058 if(!parent.inactive && rightButtonDown)
3061 Menu contextMenu { };
3063 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3064 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3065 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3066 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3067 MenuDivider { contextMenu };
3068 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3070 popup = PopupMenu { master = this, menu = contextMenu,
3072 nonClient = true, interim = false, parent = parent,
3073 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3075 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3079 rightButtonDown = false;
3083 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3088 if(style.noSelect) return true;
3090 // Should we have a separate 'selectOnActivate' style?
3091 if(!mods.isActivate || (style.readOnly && style.multiLine))
3097 mouseX = mx - XOFFSET;
3100 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3102 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3108 else if(IsMouseOnSelection() && !mods.isActivate)
3118 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3120 if(mods.shift && !mods.isActivate)
3135 this.selLine = this.line;
3144 UpdateCaretPosition(true);
3148 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3152 mouseSelect = false;
3163 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3165 //PrintLn("MouseMove: ", x, ", ", y);
3171 mouseMove = IsMouseOnSelection();
3175 int size = SelSize();
3178 char * text = new char[size+1];
3182 GetSel(text, false);
3184 if(Max(selY, this.y) == dropY)
3188 if(this.dropX > this.selX)
3189 moveX = this.x - this.selX;
3193 if(this.dropX > this.x)
3194 moveX = this.selX - this.x;
3198 this.dropX -= moveX;
3199 this.selX = this.x = this.dropX;
3200 this.selY = this.y = this.dropY;
3201 this.selLine = this.line = this.dropLine;
3203 SetViewToCursor(true);
3208 byte * c = ((EditBox)this).multiLineContents, * c2;
3209 int l1 = c ? strlen(c) : 0, l2;
3212 c2 = ((EditBox)this).multiLineContents;
3213 l2 = c2 ? strlen(c2) : 0;
3214 if(l1 != l2 || (l1 && strcmp(c, c2)))
3244 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3246 //PrintLn("Dropped: ", x, ", ", y);
3248 NotifyDropped(master, this, x, y);
3255 bool OnMouseMove(int mx, int my, Modifiers mods)
3261 if(mods != -1 && mods.isSideEffect)
3266 if(style.noSelect) return true;
3267 if(wordSelect) return true;
3268 mouseX = mx - XOFFSET;
3271 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3273 if(this.mouseMove || this.mouseSelect)
3282 ((style.hScroll) || (style.vScroll)))
3289 DirtyLine(this.dropY);
3292 DirtyLine(this.dropY);
3293 this.dropLine = line;
3294 SetViewToCursor(true);
3296 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3299 else if(this.mouseSelect)
3301 DirtyLine(this.selY);
3308 SetViewToCursor(true);
3311 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3318 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3323 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3327 if(style.noSelect) return true;
3328 FindMouse(mx, my, &x, &y, &line, false);
3329 if(!NotifyDoubleClick(master, this, line, mods))
3336 for(c = x; c >= 0; c--)
3339 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3340 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3347 for(c = start; c<line.count; c += numBytes)
3349 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3360 this.line = this.selLine = line;
3361 this.wordSelect = (c != start);
3373 Menu fileMenu { menu, "File", F };
3374 saveDialog = fileDialog;
3375 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3376 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3386 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3387 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3389 space.w = Max(space.w, 1);
3390 large.w = Max(large.w, 1);
3391 space.h = Max(space.h, 1);
3392 large.h = Max(large.h, 1);
3396 for(line = lines.first; line; line = line.next)
3397 ComputeLength(line);
3402 SetViewToCursor(true);
3410 bool OnLoadGraphics()
3412 FontExtent = Display::FontExtent;
3415 // UpdateCaretPosition(true);
3419 void OnUnloadGraphics()
3424 bool OnKeyHit(Key key, unichar ch)
3426 bool shift = (key.shift) ? true : false;
3428 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3430 if(!ch && !key.alt && !key.ctrl)
3432 key.code = (SmartKey)key.code;
3435 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3438 if(style.readOnly) break;
3439 if(style.stuckCaret) GoToEnd(true);
3440 if(!(style.freeCaret))
3442 this.x = Min(this.x, this.line.count);
3448 EditLine line = this.line;
3450 for(y = this.y; y>= 0; y--)
3452 c = (y == this.y) ? (this.x-1) : line.count-1;
3454 // Slow down when going on lines...
3455 if(y != this.y) break;
3459 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3460 if(IS_ALUNDER(line.buffer[c]))
3465 if(!IS_ALUNDER(line.buffer[c]))
3467 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3481 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3482 this.x = this.selX = Min(c+1, line.count);
3483 this.y = this.selY = y;
3484 this.line = this.selLine = line;
3485 SetViewToCursor(true);
3496 if(style.readOnly) break;
3497 if(style.stuckCaret) break;
3506 if(this.line != this.selLine || this.x != this.selX)
3509 SetViewToCursor(true);
3515 if(this.x < this.line.count)
3519 for(i = this.x; i < this.line.count; i++)
3521 if(!IS_ALUNDER(this.line.buffer[i]))
3525 for(; i < this.line.count; i++)
3527 //Delete trailing whitespace
3528 if(IS_ALUNDER(this.line.buffer[i]))
3531 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3532 SetViewToCursor(true);
3535 else if(this.line.next)
3537 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3538 SetViewToCursor(true);
3544 if(!(style.freeCaret))
3546 this.selX = this.x = Min(this.x, this.line.count);
3549 if(this.x < this.line.count)
3551 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3553 else if(this.line.next)
3555 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3557 SetViewToCursor(true);
3566 if(!key.alt && !key.ctrl)
3570 bool stuffAfter = false;
3574 if(style.stuckCaret) GoToEnd(true);
3575 if(style.readOnly) break;
3576 if(!(style.multiLine)) break;
3578 for(c = 0; c<this.line.count && c<this.x; c++)
3580 if(this.line.buffer[c] == '\t')
3581 position += this.tabSize - (position % this.tabSize);
3582 else if(this.line.buffer[c] == ' ')
3590 if(this.x < this.line.count)
3593 //If last character is a { indent one tab
3594 if(this.line.buffer[this.x - 1] == '{')
3596 //Except if the next non space character is a }
3597 bool indent = false;
3599 for(i = this.x; i < this.line.size; i++)
3600 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3602 if(this.line.buffer[i] != '}')
3607 position += this.tabSize;
3610 addString = new char[position + 2];
3611 addString[len++] = '\n';
3612 addString[len] = '\0';
3614 if(stuffAfter || !style.freeCaret)
3616 for(c = 0; c<position; )
3618 if(style.useTab && c + this.tabSize <= position)
3620 addString[len++] = '\t';
3625 addString[len++] = ' ';
3629 addString[len] = '\0';
3633 if(!stuffAfter && style.freeCaret)
3635 this.x = this.selX = position;
3639 SetViewToCursor(true);
3649 if(style.stuckCaret) break;
3650 if(!(style.freeCaret))
3652 this.x = Min(this.x, this.line.count);
3653 this.selX = Min(this.selX, this.selLine.count);
3656 if(!shift) SelDirty();
3659 bool foundAlpha = false;
3662 EditLine line, lastLine;
3665 for(line = this.line; (line && !found); line = line.prev, y--)
3670 if(this.x == 0 && line != this.line)
3679 if(line == this.line) start = this.x -1; else start = line.count-1;
3680 start = Min(start, line.count-1);
3682 for(c = start; c >= 0;)
3685 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3703 byte ch = line.buffer[c];
3704 if(UTF8_IS_FIRST(ch)) break;
3708 // No next word found,
3709 if(!found && ( this.x > 0 || (!line.count && this.x)))
3723 this.line = lastLine;
3734 byte * buffer = line.buffer;
3737 byte ch = buffer[x];
3738 if(UTF8_IS_FIRST(ch)) break;
3755 if(!shift) Deselect();
3756 SetViewToCursor(true);
3762 if(style.stuckCaret) break;
3763 if(!(style.freeCaret))
3765 this.x = Min(this.x, this.line.count);
3766 this.selX = Min(this.selX, this.selLine.count);
3769 if(!shift) SelDirty();
3770 if(!shift && (this.x != this.selX || this.y != this.selY));
3773 bool onAChar = false;
3774 if(this.selX != this.x || this.selY != this.y)
3776 if(this.x<this.line.count)
3777 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3779 if(key.shift && onAChar &&
3780 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3782 bool foundAlpha = false;
3784 EditLine line, lastLine;
3786 int lastC, lastY, lastNumBytes;
3788 for(line = this.line; (line && !found); line = line.next, y++)
3790 int start = (line == this.line) ? this.x : 0;
3793 for(c = start; c < line.count; c += numBytes)
3795 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3800 lastNumBytes = numBytes;
3810 if(!found && (c != this.x || line != this.line))
3823 this.x = lastC + lastNumBytes;
3825 this.line = lastLine;
3832 bool foundAlpha = false;
3837 for(line = this.line; (line && !found); line = line.next, y++)
3839 int start = (line == this.line) ? this.x : 0;
3842 for(c = start; c < line.count; c += numBytes)
3844 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3859 // No next word found,
3860 if(!found && (c != this.x || line != this.line))
3864 this.x = line.count;
3876 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3880 byte * buffer = line.buffer;
3883 byte ch = buffer[x];
3884 if(UTF8_IS_FIRST(ch)) break;
3905 if(!shift) Deselect();
3906 SetViewToCursor(true);
3913 if(!style.vScroll || hasVertScroll) break;
3919 if(style.stuckCaret) break;
3921 if(!shift) SelDirty();
3931 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3935 if(!shift) Deselect();
3937 SetViewToCursor(false);
3940 if(caretY == this.y * space.h)
3946 if(!style.freeCaret)
3947 this.x = Min(this.x, line.count);
3957 int sx = 0, sy = this.y * space.h;
3958 char * text = line.text;
3959 int maxW = clientSize.w - sx;
3960 display.FontExtent(font, " ", 1, null, &th);
3964 int startPos = textPos;
3967 bool lineComplete = false;
3969 if(!style.wrap && caretY == MAXINT)
3972 //textPos = line.count;
3973 //lineComplete = true;
3976 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3980 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3983 len = (nextSpace - (text + textPos));
3985 len = line.count - textPos;
3987 if(textPos < line.count)
3989 display.FontExtent(font, text + textPos, len, &w, null);
3991 if(nextSpace) { w += space.w; len++; }
3993 if(style.wrap && x + width + w > maxW && x > 0)
3995 lineComplete = true;
4005 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4009 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4012 if(this.x > line.count)
4015 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4016 len = this.x - startPos;
4017 display.FontExtent(font, text + startPos, len, &x, null);
4021 if(!shift) Deselect();
4023 SetViewToCursor(false);
4027 if(sy == caretY - th || textPos >= line.count)
4029 if(textPos >= line.count)
4031 int c = textPos - 1;
4032 while(c > 0 && text[c] == ' ') c--;
4036 this.x = line.count;
4039 if(!shift) Deselect();
4041 SetViewToCursor(false);
4046 } while(textPos < line.count);
4049 if(!shift) Deselect();
4051 SetViewToCursor(false);
4060 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4061 if(!shift) SelDirty();
4062 this.line = this.line.prev;
4068 if(!shift) Deselect();
4072 SetViewToCursor(false);
4078 return style.multiLine ? false : true;
4082 if(!style.vScroll || hasVertScroll)
4089 if(style.stuckCaret) break;
4093 int sx = 0, sy = this.y * this.space.h;
4094 int maxW = clientSize.w - sx;
4095 char * text = line.buffer;
4097 if(!shift) SelDirty();
4103 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4113 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4116 if(!shift) Deselect();
4118 if(this.selX != this.x || this.selY != this.y)
4120 SetViewToCursor(false);
4123 while(!textPos || (style.freeCaret || textPos<line.count))
4125 int startPos = textPos;
4128 bool lineComplete = false;
4129 if(!style.wrap && sy <= caretY)
4131 textPos = line.count;
4132 lineComplete = true;
4134 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4138 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4141 len = (nextSpace - (text + textPos));
4143 len = line.count - textPos;
4145 if(textPos < line.count)
4147 display.FontExtent(font, text + textPos, len, &w, &th);
4149 if(nextSpace) { w += space.w; len++; }
4150 if(style.wrap && x + width + w > maxW && x > 0)
4152 lineComplete = true;
4162 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4166 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4169 if(this.x > line.count)
4172 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4174 len = this.x - startPos;
4175 display.FontExtent(font, text + startPos, len, &x, null);
4178 if(!shift) Deselect();
4181 SetViewToCursor(false);
4187 this.x = line.count;
4190 if(!shift) Deselect();
4193 SetViewToCursor(false);
4196 else if(textPos >= line.count && line.next)
4202 sy = this.y * this.space.h;
4203 sx = 0; //textBlock.startX;
4209 sx = 0; //textBlock.startX;
4217 this.x = Min(this.x, line.count);
4218 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4220 if(!shift) Deselect();
4223 if(this.selX != this.x || this.selY != this.y)
4226 SetViewToCursor(false);
4233 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4234 if(!shift) SelDirty();
4235 this.line = this.line.next;
4239 if(!shift) Deselect();
4242 if(this.selX != this.x || this.selY != this.y)
4250 return style.multiLine ? false : true;
4253 if(style.stuckCaret) break;
4254 if(!(style.freeCaret))
4255 this.selX = Min(this.selX, this.selLine.count);
4257 if(!shift) SelDirty();
4260 this.line = this.lines.first;
4261 if(this.y != 0 || this.x != 0)
4271 EditLine line = this.line;
4273 for(c=0; line.buffer[c]; c++)
4274 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4276 if(c != 0 || this.x)
4286 DirtyLine(this.y);*/
4291 if(!shift) Deselect();
4292 SetViewToCursor(true);
4298 if(style.stuckCaret) break;
4299 if(!(style.freeCaret))
4300 this.selX = Min(this.selX, this.selLine.count);
4302 if(!shift) SelDirty();
4307 else if(this.x != this.line.count)
4309 this.x = this.line.count;
4310 //DirtyLine(this.y);
4313 if(!shift) Deselect();
4314 SetViewToCursor(true);
4319 if(style.tabKey && !key.ctrl)
4321 if(this.selY != this.y && style.tabSel)
4323 EditLine firstLine, lastLine;
4327 // Do multi line selection tabbing here
4328 if(this.selY < this.y)
4330 firstLine = this.selLine;
4331 lastLine = this.line;
4337 // Selecting going up
4338 firstLine = this.line;
4339 lastLine = this.selLine;
4346 for(line = firstLine; line; line = line.next, y++)
4348 if(line != lastLine || x)
4352 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4354 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4356 if(line.buffer[c] == '\t')
4361 else if(line.buffer[c] != ' ')
4366 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4369 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4370 char * string = new char[len];
4371 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4372 GetText(string, line, y, 0, line, y, lastC, false, false);
4375 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4376 if(!line.AdjustBuffer(line.count-lastC))
4379 if(style.autoSize) AutoSize();
4383 if(line == lastLine) break;
4388 for(line = firstLine; line; line = line.next, y++)
4392 if(line != lastLine || x)
4396 if(line.count + 1 <= this.maxLineSize)
4398 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4400 if(!line.AdjustBuffer(line.count+1))
4403 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4404 if(style.autoSize) AutoSize();
4406 AddCharAction action { ch = '\t', x = 0, y = y };
4410 memmove(line.buffer+1,line.buffer,line.size-1);
4412 line.buffer[0] = '\t';
4417 if(line.count + this.tabSize <= this.maxLineSize)
4420 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4421 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4422 if(style.autoSize) AutoSize();
4424 if(!line.AdjustBuffer(line.count+this.tabSize))
4428 char * string = new char[this.tabSize + 1];
4429 memset(string, ' ', this.tabSize);
4430 string[this.tabSize] = '\0';
4431 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4434 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4435 line.count+=this.tabSize;
4436 for(c=0; c<this.tabSize; c++)
4437 line.buffer[c] = ' ';
4443 if(line == lastLine) break;
4457 char * addString = new char[this.tabSize + 1];
4459 if(!(style.freeCaret))
4461 this.x = Min(this.x, this.line.count);
4466 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4468 addString[len++] = ' ';
4476 SetViewToCursor(true);
4483 if(!(style.hScroll) || hasHorzScroll) break;
4484 if(this.viewX < this.maxLength)
4486 //this.viewX+=this.space.w*this.tabSize;
4488 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4495 if(!shift) Deselect();
4504 if(!(style.hScroll) || hasHorzScroll) break;
4507 //this.viewX-=this.space.w*this.tabSize;
4508 //this.viewX = Max(this.viewX,0);
4510 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4511 // SetCursorToView();
4518 if(!shift) Deselect();
4532 if(!(style.readOnly))
4538 this.overwrite ^= 1;
4539 UpdateCaretPosition(true);
4544 NotifyOvrToggle(master, this, this.overwrite);
4555 if(style.noSelect) break;
4558 //Save current view position
4559 int tempX = this.viewX;
4560 int tempY = this.viewY;
4564 this.selLine = this.lines.first;
4565 this.y = this.lineCount-1;
4566 this.line = this.lines.last;
4567 this.x = this.line.count;
4570 SetViewToCursor(true);
4572 //Restore previous view position
4573 SetScrollPosition(tempX, tempY * this.space.h);
4579 // TOCHECK: Was there any good reason why we weren't returning false here?
4580 return false; // break;
4585 if(!(style.readOnly))
4593 if(style.readOnly) break;
4599 if(style.readOnly) break;
4604 if(style.readOnly) break;
4608 if(style.readOnly) break;
4612 if(style.readOnly) break;
4613 if(key.shift && key.code == rightBracket)
4615 //Only indent back if you are exactly at one tab.
4617 bool whitespace = true;
4623 EditLine line = this.line;
4625 //Only remove one tab if there is nothing else on the line.
4626 for(i = 0; i < this.line.count; i++)
4628 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4642 for(pos = line.count - 1; pos >= 0; pos--)
4644 char c = line.buffer[pos];
4648 indentwidth += this.tabSize;
4650 //Counting backwards, so when you find a character, indentation is reset
4660 //Place the } to get an undo:
4663 this.x = this.line.count;
4667 putsize = indentwidth;
4669 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4671 newline = new char[putsize+2];
4672 newline[putsize] = '}';
4673 newline[putsize+1] = '\0';
4677 for(; i < indentwidth / this.tabSize; i++)
4679 for(;i < putsize; i++)
4687 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4699 void OnHScroll(ScrollBarAction action, int position, Key key)
4702 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4704 this.viewX = position;
4705 if(action != setRange)
4707 if(!this.mouseMove && style.cursorFollowsView)
4714 void OnVScroll(ScrollBarAction action, int position, Key key)
4716 int oldViewY = this.viewY;
4719 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4721 position /= this.space.h;
4723 if(position < this.viewY)
4725 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4726 style.recomputeSyntax = true;
4728 else if(position > this.viewY)
4730 EditLine oldViewLine = viewLine;
4731 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4732 FigureStartSyntaxStates(oldViewLine, false);
4735 if(action != setRange)
4737 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4740 if(this.x != this.selX || this.y != this.selY)
4744 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4747 int numLines = clientSize.h / this.space.h;
4749 if(Abs(this.viewY - oldViewY) < numLines)
4752 if(this.viewY > oldViewY)
4754 for(y = oldViewY; y <this.viewY; y++)
4755 DirtyLine(y + numLines);
4759 for(y = this.viewY; y <oldViewY; y++)
4760 DirtyLine(y + numLines);
4765 // Fix dirt of stuff before first line...
4766 if(this.viewY - oldViewY > 0)
4768 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4775 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4780 ReplaceTextAction replaceAction = null;
4781 AddCharAction addCharAction = null;
4782 int addedSpaces = 0, addedTabs = 0;
4784 if(ch == '\r') return true;
4785 if(style.stuckCaret /*|EES_READONLY)*/ )
4788 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4790 if(!undoBuffer.dontRecord)
4792 if(selX != x || selY != y)
4797 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4798 oldString = new char[len];
4799 UTF32toUTF8Len(&ch, 1, buffer, 4);
4800 newString = CopyString(buffer);
4801 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4803 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4804 if(selY < y || (selY == y && selX < x))
4806 replaceAction.x1 = selX;
4807 replaceAction.y1 = selY;
4808 replaceAction.x2 = x;
4809 replaceAction.y2 = y;
4813 replaceAction.x1 = x;
4814 replaceAction.y1 = y;
4815 replaceAction.x2 = selX;
4816 replaceAction.y2 = selY;
4818 Record(replaceAction);
4819 undoBuffer.dontRecord++;
4823 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4824 Record(addCharAction);
4830 DelSel(&addedSpaces);
4831 if(this.lineCount+1 > this.maxLines)
4834 Emptyline(this.lines.first,0);
4838 if(!(style.vScroll))
4840 // Make sure it fits, but we need a default line is this.font is too big for window
4841 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4844 if((this.y >= 0) && this.y < this.viewY)
4849 line = EditLine { };
4852 lines.Insert(this.line, line);
4853 line.editBox = this;
4859 // If we're displacing the lines from a current line ...
4862 if(this.line.buffer)
4865 if(this.line.count < endX) endX = this.line.count;
4866 length = this.line.count - endX;
4869 if(!line.AdjustBuffer(length))
4873 if(this.line.buffer)
4875 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4877 if(endX > 4000 || endX < 0)
4880 this.line.count = endX;
4881 this.line.buffer[this.line.count] = '\0';
4882 this.line.AdjustBuffer(this.line.count);
4883 ComputeLength(this.line);
4887 BufferLocation before = { this.line, this.y, this.x }, after;
4893 ComputeLength(this.line);
4896 if(length > 4000 || length < 0)
4899 line.count = length;
4903 line.buffer[line.count] = '\0';
4906 after.line = this.line, after.y = this.y, after.x = this.x;
4908 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4909 if(style.autoSize) AutoSize();
4915 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4916 DelSel(&addedSpaces);
4917 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4918 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4919 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4923 this.selLine = this.line;
4927 replaceAction.x3 = x;
4928 replaceAction.y3 = y;
4929 replaceAction.addedSpaces = addedSpaces;
4930 replaceAction.addedTabs = addedTabs;
4931 undoBuffer.dontRecord--;
4935 addCharAction.addedSpaces = addedSpaces;
4936 addCharAction.addedTabs = addedTabs;
4943 /****************************************************************************
4945 ****************************************************************************/
4948 bool AddCh(unichar ch)
4950 return _AddCh(ch, null, null);
4955 this.modified = true;
4956 NotifyUpdate(master, this);
4957 modifiedDocument = true;
4960 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4963 DelCh(line1, y1, x1, line2, y2, x2, false);
4964 SetViewToCursor(true);
4972 itemEditUndo.disabled = undoBuffer.curAction == 0;
4973 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4974 if(savedAction == undoBuffer.curAction)
4976 modifiedDocument = false;
4978 NotifyUnsetModified(master, this);
4985 itemEditUndo.disabled = undoBuffer.curAction == 0;
4986 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4987 if(savedAction == undoBuffer.curAction)
4989 modifiedDocument = false;
4991 NotifyUnsetModified(master, this);
4995 void Record(UndoAction action)
4997 if(!undoBuffer.dontRecord)
4999 undoBuffer.Record(action);
5000 itemEditUndo.disabled = undoBuffer.curAction == 0;
5001 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5008 this.lines.first, 0,0,
5009 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5012 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5017 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5018 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5019 this.y = line2 ? y2 : (this.lineCount-1);
5020 this.line = line2 ? (EditLine)line2 : this.lines.last;
5023 SetViewToCursor(true);
5027 // TODO: Fix this vs modifiedDocument window property
5028 void SetModified(bool flag)
5032 this.modified = false;
5033 if(flag && !NotifyModified(master, this))
5034 this.modified = true;
5039 bool AddS(char * string)
5046 int addedSpaces = 0, addedTabs = 0;
5047 AddTextAction action = null;
5048 ReplaceTextAction replaceAction = null;
5050 this.pasteOperation = true;
5052 if(style.stuckCaret /*|EES_READONLY)*/ )
5055 if(!undoBuffer.dontRecord)
5057 char * placeString = CopyString(string);
5058 if(!style.multiLine)
5062 for(i = 0; (ch = placeString[i]); i++)
5065 placeString[i] = '\0';
5066 placeString = renew placeString byte[i+1];
5071 if(selX != x || selY != y)
5075 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5076 oldString = new char[len];
5077 newString = placeString;
5078 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5080 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5081 if(selY < y || (selY == y && selX < x))
5083 replaceAction.x1 = selX;
5084 replaceAction.y1 = selY;
5085 replaceAction.x2 = x;
5086 replaceAction.y2 = y;
5090 replaceAction.x1 = x;
5091 replaceAction.y1 = y;
5092 replaceAction.x2 = selX;
5093 replaceAction.y2 = selY;
5095 Record(replaceAction);
5099 if(string[0] == '\n')
5100 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5102 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5110 undoBuffer.dontRecord++;
5111 DelSel(&addedSpaces);
5115 for(c = 0; string[c]; c++)
5117 if(string[c] == '\n' || string[c] == '\r')
5119 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5124 if(string[c] == '\n')
5133 // Reset for next line
5137 if(string[c] == '\r' && *line == '\n')
5149 // Why was this here?
5152 // Add the line here
5154 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5160 undoBuffer.dontRecord--;
5165 action.addedSpaces = addedSpaces;
5166 action.addedTabs = addedTabs;
5168 else if(replaceAction)
5170 replaceAction.y3 = y;
5171 replaceAction.x3 = x;
5172 replaceAction.addedSpaces = addedSpaces;
5173 replaceAction.addedTabs = addedTabs;
5176 UpdateCaretPosition(true);
5178 this.pasteOperation = false;
5191 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5192 SetViewToCursor(true);
5198 void PutCh(unichar ch)
5202 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5203 //if((ch >= 32) || ch == '\n')
5205 int addedSpaces = 0, addedTabs = 0;
5206 ReplaceTextAction replaceAction = null;
5207 AddCharAction addCharAction = null;
5210 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5212 if(this.x < this.line.count && this.overwrite)
5217 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5218 oldString = new char[len];
5219 UTF32toUTF8Len(&ch, 1, buffer, 4);
5220 newString = CopyString(buffer);
5221 GetText(oldString, line, y, x, line, y, x+1, false, false);
5222 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5223 replaceAction.x1 = x;
5224 replaceAction.y1 = y;
5225 replaceAction.x2 = x+1;
5226 replaceAction.y2 = y;
5227 Record(replaceAction);
5229 undoBuffer.dontRecord++;
5230 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5231 undoBuffer.dontRecord--;
5233 else if(!undoBuffer.dontRecord)
5235 if(selX != x || selY != y)
5240 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5241 oldString = new char[len];
5242 UTF32toUTF8Len(&ch, 1, buffer, 4);
5243 newString = CopyString(buffer);
5244 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5245 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5246 if(selY < y || (selY == y && selX < x))
5248 replaceAction.x1 = selX;
5249 replaceAction.y1 = selY;
5250 replaceAction.x2 = x;
5251 replaceAction.y2 = y;
5255 replaceAction.x1 = x;
5256 replaceAction.y1 = y;
5257 replaceAction.x2 = selX;
5258 replaceAction.y2 = selY;
5260 Record(replaceAction);
5264 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5265 Record(addCharAction);
5268 undoBuffer.dontRecord++;
5269 result = _AddCh(ch, &addedSpaces, &addedTabs);
5272 replaceAction.x3 = x;
5273 replaceAction.y3 = y;
5274 replaceAction.addedSpaces = addedSpaces;
5275 replaceAction.addedTabs = addedTabs;
5279 addCharAction.addedSpaces = addedSpaces;
5280 addCharAction.addedTabs = addedTabs;
5282 undoBuffer.dontRecord--;
5286 if(result) SetViewToCursor(true);
5290 void PutS(char * string)
5295 SetViewToCursor(true);
5300 void Printf(char * format, ...)
5304 char temp[MAX_F_STRING];
5306 va_start(args, format);
5307 vsprintf(temp, format, args);
5313 void SetContents(char * format, ...)
5318 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5321 char temp[MAX_F_STRING];
5323 va_start(args, format);
5324 vsprintf(temp, format, args);
5340 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5343 else if(this.line.prev)
5345 EditLine line = this.line.prev;
5349 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5357 this.selLine = this.line;
5362 SetViewToCursor(true);
5367 Emptyline(this.line,this.y);
5368 this.selX = this.x = 0;
5371 this.selLine = this.line;
5373 SetViewToCursor(true);
5383 SetViewToCursor(true);
5391 SetViewToCursor(true);
5395 bool GoToLineNum(int lineNum)
5400 EditLine line = this.lines.first;
5401 for(c = 0; c < lineNum && line; c++, line = line.next);
5411 SetViewToCursor(true);
5418 bool GoToPosition(EditLine line, int y, int x)
5430 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5444 SetViewToCursor(true);
5451 void SetViewToCursor(bool setCaret)
5461 bool dontScroll = false;
5467 selected = selX != this.x || selY != y;
5474 checkLine = dropLine;
5480 checkLine = this.line;
5485 numLines = clientSize.h / space.h;
5487 // This is broken. The EditBox now doesn't do anything different when adding to it,
5488 // regardless of the previous scrolling position. It should be read and then set again
5489 // if one wishes to preserve it.
5490 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5491 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5495 // Horizontal Adjustment
5496 if(!dontScroll && checkLine)
5500 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5503 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5509 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5510 viewX = x - clientSize.w+space.w;
5511 if(x < this.viewX + clientSize.w/2 - space.w)
5512 viewX = Max(0, x - clientSize.w/2 + space.w);
5520 // Vertical Adjustment
5521 if(viewY > checkY) viewY = checkY;
5522 if(viewY + numLines <= checkY)
5524 if(clientSize.h >= space.h)
5526 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5536 for(;dropLine && dropLine.prev && dropY >= numLines;)
5538 dropLine = dropLine.prev;
5542 for(;this.line && this.line.prev && this.y >= numLines;)
5544 this.line = this.line.prev;
5549 SetScrollPosition(viewX, viewY * this.space.h);
5551 UpdateCaretPosition(setCaret);
5557 selLine = this.line;
5567 void CenterOnCursor()
5569 int numLines = clientSize.h / this.space.h;
5570 int y = this.y - numLines / 2;
5572 bool figureSyntax = false;
5573 EditLine oldViewLine = viewLine;
5574 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5579 for(;y < this.viewY; y++)
5581 this.viewLine = this.viewLine.prev;
5582 style.recomputeSyntax = true;
5584 for(;y > this.viewY; y--)
5586 this.viewLine = this.viewLine.next;
5587 figureSyntax = true;
5590 FigureStartSyntaxStates(oldViewLine, false);
5594 SetScrollPosition(this.viewX, viewY * this.space.h);
5595 UpdateCaretPosition(true);
5599 void SetCursorToView()
5610 numLines = clientSize.h / this.space.h;
5614 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5615 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5619 EditLine oldLine = this.line;
5620 bool lastOne = false;
5621 EditLine oldViewLine = this.viewLine;
5622 bool figureSyntax = false;
5624 if(this.y >= this.lineCount-1) return;
5626 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5628 if(this.viewY + numLines < this.lines.count)
5630 this.viewLine = this.viewLine.next;
5632 figureSyntax = true;
5634 else if(c && !lastOne)
5643 FigureStartSyntaxStates(oldViewLine, false);
5644 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5647 SetViewToCursor(false);
5656 if(this.y == 0) return;
5658 numLines = clientSize.h / this.space.h;
5662 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5663 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5667 EditLine oldLine = this.line;
5669 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5674 if(this.viewLine.prev)
5676 this.viewLine = this.viewLine.prev;
5678 style.recomputeSyntax = true;
5682 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5685 SetViewToCursor(false);
5692 if(this.viewLine.prev)
5695 // this.viewLine = this.viewLine.prev;
5698 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5705 if(this.viewLine.next)
5708 // this.viewLine = this.viewLine.next;
5711 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5718 EditLine l1, l2, line;
5719 int x1, x2, nx1, nx2;
5724 if(!this.selLine) return 0;
5725 if(this.selLine == this.line && this.selX == this.x) return 0;
5726 if(this.selY < this.y)
5733 else if(this.selY > this.y)
5740 else if(this.selX < this.x)
5742 l1 = l2 = this.line;
5748 l1 = l2 = this.line;
5752 nx1 = Min(x1,l1.count);
5753 nx2 = Min(x2,l2.count);
5755 // Find Number of Bytes Needed
5757 for(line = l1; line; line = line.next)
5759 if(line == l1) start = nx1; else start = 0;
5760 if(line == l2) end = nx2; else end = line.count;
5762 if(style.freeCaret && line == l2)
5765 count = Max(x2-Max(x1,l1.count),0);
5767 count = Max(x2-l2.count,0);
5771 if(line == l2) break;
5772 // Add Carriage Return / line Feed
5779 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5783 if(!reorder || this.selY < this.y)
5785 if(l1) *l1 = this.selLine;
5786 if(y1) *y1 = this.selY;
5787 if(x1) *x1 = this.selX;
5788 if(l2) *l2 = this.line;
5789 if(y2) *y2 = this.y;
5790 if(x2) *x2 = this.x;
5792 else if(this.selY > this.y)
5794 if(l1) *l1 = this.line;
5795 if(y1) *y1 = this.y;
5796 if(x1) *x1 = this.x;
5797 if(l2) *l2 = this.selLine;
5798 if(y2) *y2 = this.selY;
5799 if(x2) *x2 = this.selX;
5801 else if(this.selX < this.x)
5803 if(l1) *l1 = this.line;
5804 if(y1) *y1 = this.selY;
5805 if(x1) *x1 = this.selX;
5806 if(l2) *l2 = this.line;
5807 if(y2) *y2 = this.y;
5808 if(x2) *x2 = this.x;
5812 if(l1) *l1 = this.line;
5813 if(y1) *y1 = this.y;
5814 if(x1) *x1 = this.x;
5815 if(l2) *l2 = this.line;
5816 if(y2) *y2 = this.selY;
5817 if(x2) *x2 = this.selX;
5822 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5824 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5826 this.selLine = (EditLine)l1;
5829 this.line = (EditLine)l2;
5833 SetViewToCursor(true);
5837 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5839 EditLine l1, l2, line;
5840 int x1, x2, nx1, nx2;
5871 nx1 = Min(x1,l1.count);
5872 nx2 = Min(x2,l2.count);
5875 for(line = l1; line; line = line.next)
5877 if(line == l1) start = nx1; else start = 0;
5878 if(line == l2) end = nx2; else end = line.count;
5881 CopyBytes(text, line.buffer + start, end - start);
5884 numChars += end-start;
5886 if(style.freeCaret && line == l2 && addSpaces)
5889 count = Max(x2-Max(x1,l1.count),0);
5891 count = Max(x2-l2.count,0);
5894 FillBytes(text,' ',count);
5900 if(line == l2) break;
5912 // '\0' terminate Terminate
5919 void GetSel(char * text, bool addCr)
5921 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5927 this.selLine = this.line;
5937 int size = SelSize();
5940 // Try to allocate memory
5941 ClipBoard clipBoard { };
5942 if(clipBoard.Allocate(size+1))
5944 GetSel(clipBoard.memory, true);
5957 ClipBoard clipBoard { };
5958 if(clipBoard.Load())
5959 PutS(clipBoard.memory);
5970 SetViewToCursor(true);
5975 void DeleteSelection()
5980 SetViewToCursor(true);
5986 void Save(File f, bool cr)
5989 savedAction = undoBuffer.curAction;
5991 for(line = this.lines.first; line; line = line.next)
5993 f.Write(line.buffer, line.count,1);
5996 if(cr) f.Putc('\r');
6002 #define BUFFER_SIZE 16384
6005 undoBuffer.dontRecord++;
6008 char buffer[BUFFER_SIZE];
6012 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6013 buffer[count] = '\0';
6019 undoBuffer.dontRecord--;
6020 undoBuffer.count = 0;
6021 undoBuffer.curAction = 0;
6022 itemEditUndo.disabled = undoBuffer.curAction == 0;
6023 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6026 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6030 bool firstPass = true;
6031 EditBoxFindResult result = found;
6033 if(!this.line) return notFound;
6036 for(line = this.line;;)
6044 line = this.lines.first;
6050 line = this.lines.last;
6051 num = this.lineCount - 1;
6057 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6059 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6063 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6066 if(line == this.line && !firstPass) break;
6084 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6088 int searchLen = strlen(text);
6089 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6091 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6092 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6094 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6101 bool OnKeyDown(Key key, unichar ch)
6104 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6106 if(!NotifyKeyDown(master, this, key, ch))
6116 this.mouseMove = false;
6117 OnLeftButtonUp(0,0,0);
6118 SetViewToCursor(true);
6128 public class EditBoxStream : File
6131 BufferLocation start, sel;
6138 EditBox editBox = this.editBox;
6140 editBox.x = start.x;
6141 editBox.y = start.y;
6142 editBox.line = start.line;
6144 editBox.selX = sel.x;
6145 editBox.selY = sel.y;
6146 editBox.selLine = sel.line;
6148 editBox.SetViewToCursor(true);
6149 //editBox.ComputeColumn();
6153 property EditBox editBox
6161 start.line = value.line;
6165 sel.line = value.selLine;
6168 value.GoToHome(true);
6171 get { return editBox; }
6174 uint Read(byte * buffer, uint size, uint count)
6177 EditBox editBox = this.editBox;
6178 EditLine line = editBox.line;
6184 for(;read < count && line; line = (*&line.next))
6186 int numBytes = Min(count - read, (*&line.count) - x);
6189 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6194 for(;read < count && x < (*&line.count);)
6196 buffer[read++] = (*&line.buffer)[x++];
6203 buffer[read++] = '\n';
6208 if(x == (*&line.count) && (*&line.next))
6217 editBox.line = editBox.selLine = line;
6218 editBox.x = editBox.selX = x;
6219 editBox.y = editBox.selY = y;
6224 bool Seek(int pos, FileSeekMode mode)
6227 EditBox editBox = this.editBox;
6228 EditLine line = editBox.line;
6230 if(mode == FileSeekMode::start)
6232 pos = pos - this.pos;
6242 for(;read < pos && line; line = (*&line.next))
6244 int numBytes = Min(pos - read, (*&line.count) - x);
6247 read += numBytes; x += numBytes;
6250 /*for(;read < pos && x < (*&line.count);)
6278 for(;read < pos && line; line = (*&line.prev))
6280 int numBytes = Min(pos - read, x);
6286 /*for(;read < pos && x > 0;)
6296 x = (*&(*&line.prev).count);
6312 editBox.line = editBox.selLine = line;
6313 editBox.x = editBox.selX = x;
6314 editBox.y = editBox.selY = y;
6319 bool Puts(char * string)
6321 EditBox editBox = this.editBox;
6322 BufferLocation start { editBox.line, editBox.y, editBox.x };
6326 editBox.AddS(string);
6328 pos.line = editBox.line;
6332 this.start.AdjustAdd(start, pos);
6333 sel.AdjustAdd(start, pos);
6338 // NOTE: BYTE, NOT UNICODE CHARACTER!
6341 EditBox editBox = this.editBox;
6342 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6347 utf8Bytes[numBytes++] = ch;
6348 utf8Bytes[numBytes] = 0;
6349 if(UTF8Validate(utf8Bytes))
6351 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6353 pos.line = editBox.line;
6356 this.start.AdjustAdd(start, pos);
6357 sel.AdjustAdd(start, pos);
6364 bool Getc(char * ch)
6366 return Read(ch, 1, 1) ? true : false;
6369 void DeleteBytes(uint count)
6371 EditBox editBox = this.editBox;
6374 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6375 BufferLocation end = pos;
6380 for(;c < count && end.line && end.x < end.line.count;)
6385 if(c < count && end.line && end.line.next)
6387 end.line = end.line.next;
6396 start.AdjustDelete(pos, end);
6397 sel.AdjustDelete(pos, end);
6399 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);