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, continuedSingleLineComment: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 = reset ? false : style.continuedSingleLineComment;
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 == '#')
1364 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1367 style.continuedSingleLineComment = continuedSingleLineComment;
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 = style.continuedSingleLineComment;
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;
1552 foreground = textColor;
1553 surface.SetForeground(textColor);
1557 for(; c<end && !cantHaveWords;)
1561 bufferLen += wordLen;
1565 /*if(line.count > 4000)
1572 for(; c<line.count; c++)
1574 unichar ch = line.buffer[c];
1575 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1576 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1577 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1578 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1579 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1587 for(; c<line.count; c++)
1589 unichar ch = line.buffer[c];
1590 if(ch == '\t' || ch == ' ')
1592 cantHaveWords = true;
1596 if(ch != ' ' && ch != '\t')
1601 if(c == line.count && c < end)
1610 cantHaveWords = true;
1615 lastWasStar = false;
1621 bool backEscaped = escaped;
1622 bool backLastWasStar = lastWasStar;
1623 bool backInMultiLineComment = inMultiLineComment;
1624 bool backInString = inString;
1625 bool backInQuotes = inQuotes;
1626 bool backInPrep = inPrep;
1627 bool backInSingleLineComment = inSingleLineComment;
1629 char * word = line.buffer + c - wordLen;
1631 bool wasEscaped = escaped;
1633 lastWasStar = false;
1635 // Determine Syntax Highlighting
1636 newTextColor = defaultTextColor;
1639 if(inSingleLineComment || inMultiLineComment)
1641 newTextColor = colorScheme.commentColor;
1645 newTextColor = colorScheme.charLiteralColor;
1649 newTextColor = colorScheme.stringLiteralColor;
1653 newTextColor = colorScheme.preprocessorColor;
1655 if(wordLen == 1 && word[0] == '/')
1657 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1661 inSingleLineComment = true;
1662 newTextColor = colorScheme.commentColor;
1664 else if(word[1] == '*')
1666 inMultiLineComment = true;
1667 newTextColor = colorScheme.commentColor;
1670 else if(backLastWasStar)
1671 inMultiLineComment = false;
1673 else if(wordLen == 1 && word[0] == '*')
1675 if(!c || word[-1] != '/')
1678 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1680 if(inString && !wasEscaped)
1687 newTextColor = colorScheme.stringLiteralColor;
1690 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1692 if(inQuotes && !wasEscaped)
1697 newTextColor = colorScheme.charLiteralColor;
1700 else if(wordLen == 1 && word[0] == '\\')
1705 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1706 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1708 newTextColor = colorScheme.numberColor;
1712 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1717 newTextColor = colorScheme.preprocessorColor;
1720 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1722 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1724 char ** keys = keyWords[g];
1725 int * len = keyLen[g];
1726 for(ccc = 0; keys[ccc]; ccc++)
1728 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1730 newTextColor = colorScheme.keywordColors[g];
1739 // If highlighting for this word is different, break
1740 if(newTextColor != textColor)
1744 // Better solution than going back?
1747 // Reset syntax flags
1748 escaped = backEscaped;
1749 lastWasStar = backLastWasStar;
1750 inMultiLineComment = backInMultiLineComment;
1751 inString = backInString;
1752 inQuotes = backInQuotes;
1753 inPrep = backInPrep;
1754 inSingleLineComment = backInSingleLineComment;
1759 textColor = newTextColor;
1762 foreground = textColor;
1763 surface.SetForeground(textColor);
1770 // If we're not breaking, this can't be rendered as spacing anymore
1773 // Do word wrapping here
1779 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1784 w += numSpaces * space.w;
1786 if(x + viewX + w > maxW)
1789 lineComplete = true;
1794 bufferLen += wordLen;
1799 int renderStart = start;
1804 // Render checking if we need to split because of selection or to find where to draw insert caret
1805 for(wc = start; wc < start + bufferLen; wc++)
1807 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1808 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1809 if(overWrite == true)
1811 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1812 if(overWriteCh == '\t') overWriteCh = ' ';
1817 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1818 if(overWrite == true)
1826 surface.TextOpacity(opacity);
1827 surface.SetBackground(background);
1828 surface.SetForeground(foreground);
1835 if(wc < line.count && line.buffer[wc] == '\t')
1837 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1841 numSpaces += space.w;
1845 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1850 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1851 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1853 if(overWrite == true)
1860 surface.TextOpacity(opacity);
1861 surface.SetBackground(background);
1862 surface.SetForeground(foreground);
1865 if(style.freeCaret && selected)
1867 surface.SetBackground(selectionBackground);
1868 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1869 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1874 if(style.freeCaret && selected)
1876 surface.SetBackground(selectionBackground);
1877 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1881 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1884 if(y > box.bottom) // >=clientSize.h)
1890 surface.TextOpacity(true);
1891 surface.SetForeground(black);
1892 surface.SetBackground(Color {255,255,85});
1893 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1897 void FixScrollArea()
1899 if(style.hScroll || style.vScroll)
1901 int width = maxLength + XOFFSET;
1902 int height = lineCount * space.h;
1903 if(style.freeCaret && line)
1908 width = Max(line.length + (x - line.count) * space.w, maxLength);
1912 if(selX > selLine.count)
1913 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1918 SetScrollLineStep(8, space.h);
1919 SetScrollArea(width, height, true);
1923 void ComputeLength(EditLine line)
1930 for(c = 0; c < line.count; )
1939 for(len = 0; c < line.count; c += numBytes)
1941 ch = line.buffer[c];
1942 numBytes = UTF8_NUM_BYTES(ch);
1944 if(ch == ' ' || ch == '\t')
1951 if(!len && ch == ' ')
1956 else if(!len && ch == '\t')
1958 w = (tabSize * space.w) - (x % (tabSize * space.w));
1962 FontExtent(display, font, line.buffer + start, len, &w, null);
1973 if(line.length > this.maxLength)
1975 this.maxLine = line;
1976 this.maxLength = line.length;
1985 this.maxLine = null;
1987 for(line = lines.first; line; line = line.next)
1989 if(line.length > this.maxLength)
1991 this.maxLength = line.length;
1992 this.maxLine = line;
1999 if(this.selY != this.y)
2001 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2005 void ComputeColumn()
2007 // Adjust new edit X position according to tabs
2008 int c, position = 0;
2011 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2013 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2014 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2016 position += this.tabSize - (position % this.tabSize);
2020 position += this.x - c;
2021 this.col = position;
2024 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2026 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2029 bool HasCommentOrEscape(EditLine line)
2031 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2036 for(c = line.count-1; c >= 0; c--)
2038 char ch = line.buffer[c];
2044 else //if(ch != ' ' && ch != '\t')
2051 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2053 EditLine line = l1, next;
2055 int oldCount1, oldCount2;
2056 int y, firstViewY, firstY, firstDropY, firstSelY;
2059 DelTextAction action = null;
2060 bool hadComment = false;
2064 hadComment = HasCommentOrEscape(line);
2066 if(y2 > y1 || c2 > c1)
2068 if(start < l1.count)
2070 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2074 oldCount1 = l1.count;
2076 while(c1 < oldCount1)
2078 byte ch = buffer[c1];
2079 if(UTF8_IS_FIRST(ch)) break;
2083 oldCount2 = l2.count;
2085 while(c2 < oldCount2)
2087 byte ch = buffer[c2];
2088 if(UTF8_IS_FIRST(ch)) break;
2093 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2098 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2099 string = new char[len];
2100 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2101 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2105 //oldCount1 = l1.count;
2106 //oldCount2 = l2.count;
2109 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2110 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2113 if(c2 > oldCount2) c2 = oldCount2;
2114 if(!(style.freeCaret))
2115 if(c1 > oldCount1) c1 = oldCount1;
2116 newLineCount = c1 + l2.count-c2;
2122 buffer = new char[line.size ? line.size : 1];
2124 buffer = new char[line.size];
2126 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2131 if(!line.AdjustBuffer(newLineCount))
2135 /*if(newLineCount > 4000 || newLineCount < 0)
2136 printf("Warning");*/
2138 line.count = newLineCount;
2140 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2145 action.addedSpaces = c1-oldCount1;
2147 if(action.addedSpaces > action.x1)
2153 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2154 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2156 line.buffer[line.count] = '\0';
2163 this.dropX -= c2-c1;
2164 this.dropX = Max(this.dropX,0);
2166 this.x = Max(this.x,0);
2173 firstViewY = this.viewY;
2175 firstDropY = this.dropY;
2176 firstSelY = this.selY;
2177 for(line = l1;line;line = next, y++)
2185 if(line == this.viewLine)
2187 if(this.viewLine.next)
2189 this.viewLine = this.viewLine.next;
2191 style.recomputeSyntax = true;
2195 this.viewLine = this.viewLine.prev;
2197 style.recomputeSyntax = true;
2200 else if(y < firstViewY)
2202 if(line == this.line)
2206 this.line = this.line.next;
2207 this.x = this.line.count;
2212 this.line = this.line.prev;
2213 this.x = this.line.count;
2220 if(line == this.dropLine)
2222 if(this.dropLine.next)
2224 this.dropLine = this.dropLine.next;
2225 this.dropX = this.dropLine.count;
2229 this.dropLine = this.dropLine.prev;
2230 this.dropX = this.dropLine.count;
2234 else if(y < firstDropY)
2236 if(line == this.selLine)
2238 if(this.selLine.next)
2240 this.selLine = this.selLine.next;
2241 this.selX = this.selLine.count;
2245 this.selLine = this.selLine.prev;
2246 this.selX = this.selLine.count;
2250 else if(y < firstSelY)
2254 if(line == l2) break;
2258 if(style.autoSize) AutoSize();
2259 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2262 style.recomputeSyntax = true;
2267 bool DelSel(int * addedSpacesPtr)
2269 if(this.line != this.selLine || this.x != this.selX)
2271 if(this.selY < this.y)
2273 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2276 this.line = this.selLine;
2278 else if(this.selY > this.y)
2280 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2283 this.selLine = this.line;
2285 else if(this.selX < this.x)
2287 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2290 this.line = this.selLine;
2294 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2297 this.selLine = this.line;
2305 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2307 bool hadComment = false;
2308 // Add the line here
2309 EditLine line = this.line;
2311 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2312 if(!style.hScroll && created)
2314 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2319 // Lock if no place to display.
2322 else if(endX > this.x)
2323 max = Max(this.x, line.count);
2327 for(x = 0, c = 0; c < max+count; )
2332 if(c < Min(this.x, line.count))
2333 string = line.buffer + c;
2336 else if(c < endX + count)
2337 string = stringLine + c - endX;
2339 string = line.buffer + c - endX - count;
2343 w = (tabSize * space.w) - (x % (tabSize * space.w));
2347 numBytes = UTF8_NUM_BYTES(*string);
2348 FontExtent(display, this.font, string, numBytes, &w, null);
2352 if(x >= clientSize.w)
2357 c += numBytes; // - 1;
2363 int addedSpaces = 0;
2366 // Add blank spaces if EES_FREECARET
2367 if(this.x > line.count)
2377 for(c = 0; c<line.count; c++)
2379 if(this.line.buffer[c] == '\t')
2380 position += this.tabSize - (position % this.tabSize);
2384 wantedPosition = position + (this.x - line.count);
2386 // A tab is too much...
2387 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2388 addedSpaces = wantedPosition - position;
2393 position += this.tabSize - (position % this.tabSize);
2394 // Add as many tabs as needed
2395 addedTabs += (wantedPosition - position) / this.tabSize;
2396 position += (addedTabs-1) * this.tabSize;
2397 // Finish off with spaces
2398 addedSpaces = wantedPosition - position;
2402 addedSpaces = this.x - line.count;
2405 this.x = Min(this.x, line.count);
2407 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2413 hadComment = HasCommentOrEscape(line);
2415 // Adjust the size of the line
2416 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2420 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2423 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2424 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2427 *addedTabsPtr = addedTabs;
2428 FillBytes(line.buffer+line.count,'\t',addedTabs);
2430 if(addedTabs > 4000 || addedTabs < 0)
2433 line.count += addedTabs;
2439 FillBytes(line.buffer+line.count,' ',addedSpaces);
2441 if(addedSpaces > 4000 || addedSpaces < 0)
2444 line.count += addedSpaces;
2445 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2447 else if(addedSpacesPtr)
2448 *addedSpacesPtr = 0;
2450 if(count > 4000 || count < 0)
2453 line.count += count;
2454 this.x += count + addedTabs + addedSpaces;
2457 this.selLine = this.line;
2459 line.buffer[line.count] = '\0';
2461 ComputeLength(line);
2466 hasComment = HasCommentOrEscape(line);
2467 if(!undoBuffer.insideRedo)
2469 int backDontRecord = undoBuffer.dontRecord;
2470 undoBuffer.dontRecord = 0;
2471 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2472 if(style.autoSize) AutoSize();
2473 undoBuffer.dontRecord = backDontRecord;
2475 if(style.syntax && (hadComment || hasComment || line != this.line))
2477 style.recomputeSyntax = true;
2485 void Emptyline(EditLine line, int y)
2488 DelCh(line, y, 0, line.next, y+1, 0, true);
2490 DelCh(line, y, 0, line, y, line.count, true);
2493 void GoToEnd(bool deselect)
2497 this.line = this.lines.last;
2498 if(this.y != this.lineCount-1)
2500 else if (this.x != this.line.count)
2501 DirtyLine(this.lineCount-1);
2502 this.y = this.lineCount-1;
2503 this.x = this.line.count;
2510 void GoToHome(bool deselect)
2514 this.line = this.lines.first;
2517 else if (this.x !=0)
2518 DirtyLine(this.lineCount-1);
2527 // Returns true if it needs scrolling
2528 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2534 bool needHScroll = false;
2541 line = this.viewLine ? (void *)this.viewLine.prev : null;
2546 line = (void *)this.lines.first;
2551 py = Min(py, clientSize.h);
2553 py = Min(py, this.lineCount);
2555 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2561 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2564 px = Min(px,clientSize.w+this.space.w);
2568 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2571 if(tline) *tline = line;
2574 // Prevent divide by 0 from non valid this.font
2576 return (y < this.viewY) || needHScroll;
2578 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2582 // Minimal Update Management Functions
2586 this.endY = clientSize.h-1;
2587 // ErrorLog("DirtyAll\n");
2590 void DirtyEnd(int y)
2592 if((y - this.viewY)*this.space.h < this.startY)
2593 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2594 this.endY = clientSize.h-1;
2595 //ErrorLog("DirtyEnd %d\n", y);
2598 void DirtyLine(int y)
2602 if((y - this.viewY)*this.space.h < this.startY)
2603 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2604 if((y - this.viewY+1)*this.space.h > this.endY)
2605 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2607 //ErrorLog("DirtyLine %d\n", y);
2614 if(style.recomputeSyntax)
2616 FigureStartSyntaxStates(lines.first, true);
2617 style.recomputeSyntax = false;
2620 if(this.startY > this.endY) return;
2621 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2627 box.right = clientSize.w-1;
2628 box.top = this.startY;
2629 box.bottom = this.endY;
2632 this.startY = clientSize.h;
2636 bool IsMouseOnSelection()
2638 bool mouseOnSelection = false;
2641 int minY = Min(this.selY, this.y);
2642 int maxY = Max(this.selY, this.y);
2643 int minX = Min(this.selX, this.x);
2644 int maxX = Max(this.selX, this.x);
2646 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2648 if(maxX != minX || maxY != minY)
2650 if(y > minY && y < maxY)
2651 mouseOnSelection = true;
2652 else if(y == minY && y == maxY)
2653 mouseOnSelection = (x < maxX && x >= minX);
2657 mouseOnSelection = (x >= this.selX);
2658 else if(y == this.y)
2659 mouseOnSelection = (x >= this.x);
2664 mouseOnSelection = (x < this.selX);
2665 else if(y == this.y)
2666 mouseOnSelection = (x < this.x);
2669 return mouseOnSelection;
2672 void UpdateCaretPosition(bool setCaret)
2676 if(mouseMove || (!overwrite && !style.noCaret))
2678 int max = this.mouseMove ? this.dropX : this.x;
2679 int y = this.mouseMove ? this.dropY : this.y;
2680 EditLine line = this.mouseMove ? this.dropLine : this.line;
2682 if(!(style.freeCaret))
2683 max = Min(max, line.count);
2685 if(FontExtent && display)
2687 for(c = 0; c < max; )
2696 for(len = 0; c < Min(max, line.count); c += numBytes)
2698 ch = line.buffer[c];
2699 numBytes = UTF8_NUM_BYTES(ch);
2701 if(ch == ' ' || ch == '\t')
2708 if(!len && ch == ' ')
2713 else if(!len && ch == '\t')
2715 w = (tabSize * space.w) - (x % (tabSize * space.w));
2719 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2731 caretY = y * this.space.h;
2732 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2737 NotifyCaretMove(master, this, y + 1, x + 1);
2743 void SelectionEnables()
2745 if((x != selX || y != selY) && !selection)
2749 itemEditCut.disabled = false;
2750 itemEditDelete.disabled = false;
2752 itemEditCopy.disabled = false;
2754 this.selection = true;
2756 else if((x == selX && y == selY) && selection)
2758 itemEditCut.disabled = true;
2759 itemEditCopy.disabled = true;
2760 itemEditDelete.disabled = true;
2762 this.selection = false;
2766 void SetSelectCursor()
2768 if(!inactive || !style.noSelect)
2771 cursor = guiApp.GetCursor(arrow);
2772 else if(this.mouseSelect)
2773 cursor = guiApp.GetCursor(iBeam);
2776 if(IsMouseOnSelection())
2777 cursor = guiApp.GetCursor(arrow);
2779 cursor = guiApp.GetCursor(iBeam);
2784 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2787 int x = px ? *px : 0;
2794 if(c < Min(max, line.count))
2798 for(len = 0; c < Min(max, line.count); c += numBytes)
2800 ch = line.buffer[c];
2801 numBytes = UTF8_NUM_BYTES(ch);
2803 if(ch == ' ' || ch == '\t')
2810 if(!len && ch == ' ')
2815 else if(!len && ch == '\t')
2817 w = (tabSize * space.w) - (x % (tabSize * space.w));
2821 FontExtent(display, font, line.buffer + start, len, &w, null);
2825 if(style.freeCaret && c < max)
2834 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2839 int a = start + len;
2842 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2846 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2849 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2853 return Min(this.maxLineSize - 1, start + len);
2859 void SetCursorToViewX()
2861 bool selecting = this.x != selX || y != selY;
2863 // Horizontal Adjustment
2865 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2870 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2882 UpdateCaretPosition(false);
2888 void SetCursorToViewY()
2891 EditLine oldLine = this.line;
2893 bool selecting = this.x != this.selX || this.y != this.selY;
2895 numLines = clientSize.h / this.space.h;
2897 // Vertical Adjustment
2898 if(this.viewY > this.y)
2900 this.y = this.viewY;
2901 this.line = this.viewLine;
2904 if(this.viewY + numLines <= this.y)
2908 this.y = this.viewY-1;
2909 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2916 if(this.line != oldLine)
2918 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2926 this.selLine = this.line;
2929 UpdateCaretPosition(false);
2936 bool SaveFile(char * fileName)
2938 File f = eFile_Open(fileName, FO_WRITE);
2942 eWindow_SetModified(false);
2943 eInstance_Delete(f);
2950 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2954 if(!style.multiLine)
2956 x = (active && this.active && !style.readOnly) ? line.count : 0;
2959 SetViewToCursor(true);
2964 if(!active && modified)
2967 if(!NotifyModified(master, this))
2970 *goOnWithActivation = false;
2977 if(timer) timer.Stop();
2979 mouseSelect = false;
2991 // todo: resize width based on largest on-screen-line extent...
2993 display.FontExtent(font, " ", 1, null, &sh);
2997 nh = lineCount * sh;
2998 size.h = nh < minClientSize.h ? minClientSize.h : nh;
3009 int len = line ? strlen(line.text) : 0;
3010 GetDecorationsSize(&dw, &dh);
3011 display.FontExtent(font, " ", 1, null, &sh);
3012 if(len) display.FontExtent(font, line.text, len, &tw, null);
3014 if(nw < minClientSize.w) nw = minClientSize.w;
3016 if(nh < minClientSize.h) nh = minClientSize.h;
3022 bool OnResizing(int *w, int *h)
3028 *w = space.h * 80 / 14;
3032 void OnResize(int w, int h)
3035 if(!hasHorzScroll && !hasVertScroll && viewLine)
3036 SetViewToCursor(true);
3038 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3040 SetViewToCursor(true);
3045 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3047 if(style.readOnly) return true;
3048 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3049 // Middle button already serves as a dragger as well.
3054 bool OnRightButtonDown(int x, int y, Modifiers mods)
3056 this.rightButtonDown = true;
3061 bool OnRightButtonUp(int x, int y, Modifiers mods)
3064 if(!parent.inactive && rightButtonDown)
3067 Menu contextMenu { };
3069 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3070 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3071 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3072 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3073 MenuDivider { contextMenu };
3074 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3076 popup = PopupMenu { master = this, menu = contextMenu,
3078 nonClient = true, interim = false, parent = parent,
3079 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3081 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3085 rightButtonDown = false;
3089 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3094 if(style.noSelect) return true;
3096 // Should we have a separate 'selectOnActivate' style?
3097 if(!mods.isActivate || (style.readOnly && style.multiLine))
3103 mouseX = mx - XOFFSET;
3106 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3108 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3114 else if(IsMouseOnSelection() && !mods.isActivate)
3124 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3126 if(mods.shift && !mods.isActivate)
3141 this.selLine = this.line;
3150 UpdateCaretPosition(true);
3154 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3158 mouseSelect = false;
3169 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3171 //PrintLn("MouseMove: ", x, ", ", y);
3177 mouseMove = IsMouseOnSelection();
3181 int size = SelSize();
3184 char * text = new char[size+1];
3188 GetSel(text, false);
3190 if(Max(selY, this.y) == dropY)
3194 if(this.dropX > this.selX)
3195 moveX = this.x - this.selX;
3199 if(this.dropX > this.x)
3200 moveX = this.selX - this.x;
3204 this.dropX -= moveX;
3205 this.selX = this.x = this.dropX;
3206 this.selY = this.y = this.dropY;
3207 this.selLine = this.line = this.dropLine;
3209 SetViewToCursor(true);
3214 byte * c = ((EditBox)this).multiLineContents, * c2;
3215 int l1 = c ? strlen(c) : 0, l2;
3218 c2 = ((EditBox)this).multiLineContents;
3219 l2 = c2 ? strlen(c2) : 0;
3220 if(l1 != l2 || (l1 && strcmp(c, c2)))
3250 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3252 //PrintLn("Dropped: ", x, ", ", y);
3254 NotifyDropped(master, this, x, y);
3261 bool OnMouseMove(int mx, int my, Modifiers mods)
3267 if(mods != -1 && mods.isSideEffect)
3272 if(style.noSelect) return true;
3273 if(wordSelect) return true;
3274 mouseX = mx - XOFFSET;
3277 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3279 if(this.mouseMove || this.mouseSelect)
3288 ((style.hScroll) || (style.vScroll)))
3295 DirtyLine(this.dropY);
3298 DirtyLine(this.dropY);
3299 this.dropLine = line;
3300 SetViewToCursor(true);
3302 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3305 else if(this.mouseSelect)
3307 DirtyLine(this.selY);
3314 SetViewToCursor(true);
3317 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3324 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3329 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3333 if(style.noSelect) return true;
3334 FindMouse(mx, my, &x, &y, &line, false);
3335 if(!NotifyDoubleClick(master, this, line, mods))
3342 for(c = x; c >= 0; c--)
3345 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3346 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3353 for(c = start; c<line.count; c += numBytes)
3355 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3366 this.line = this.selLine = line;
3367 this.wordSelect = (c != start);
3379 Menu fileMenu { menu, "File", F };
3380 saveDialog = fileDialog;
3381 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3382 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3392 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3393 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3395 space.w = Max(space.w, 1);
3396 large.w = Max(large.w, 1);
3397 space.h = Max(space.h, 1);
3398 large.h = Max(large.h, 1);
3402 for(line = lines.first; line; line = line.next)
3403 ComputeLength(line);
3408 SetViewToCursor(true);
3416 bool OnLoadGraphics()
3418 FontExtent = Display::FontExtent;
3421 // UpdateCaretPosition(true);
3425 void OnUnloadGraphics()
3430 bool OnKeyHit(Key key, unichar ch)
3432 bool shift = (key.shift) ? true : false;
3434 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3436 if(!ch && !key.alt && !key.ctrl)
3438 key.code = (SmartKey)key.code;
3441 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3444 if(style.readOnly) break;
3445 if(style.stuckCaret) GoToEnd(true);
3446 if(!(style.freeCaret))
3448 this.x = Min(this.x, this.line.count);
3454 EditLine line = this.line;
3456 for(y = this.y; y>= 0; y--)
3458 c = (y == this.y) ? (this.x-1) : line.count-1;
3460 // Slow down when going on lines...
3461 if(y != this.y) break;
3465 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3466 if(IS_ALUNDER(line.buffer[c]))
3471 if(!IS_ALUNDER(line.buffer[c]))
3473 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3487 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3488 this.x = this.selX = Min(c+1, line.count);
3489 this.y = this.selY = y;
3490 this.line = this.selLine = line;
3491 SetViewToCursor(true);
3502 if(style.readOnly) break;
3503 if(style.stuckCaret) break;
3512 if(this.line != this.selLine || this.x != this.selX)
3515 SetViewToCursor(true);
3521 if(this.x < this.line.count)
3525 for(i = this.x; i < this.line.count; i++)
3527 if(!IS_ALUNDER(this.line.buffer[i]))
3531 for(; i < this.line.count; i++)
3533 //Delete trailing whitespace
3534 if(IS_ALUNDER(this.line.buffer[i]))
3537 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3538 SetViewToCursor(true);
3541 else if(this.line.next)
3543 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3544 SetViewToCursor(true);
3550 if(!(style.freeCaret))
3552 this.selX = this.x = Min(this.x, this.line.count);
3555 if(this.x < this.line.count)
3557 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3559 else if(this.line.next)
3561 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3563 SetViewToCursor(true);
3572 if(!key.alt && !key.ctrl)
3576 bool stuffAfter = false;
3580 if(style.stuckCaret) GoToEnd(true);
3581 if(style.readOnly) break;
3582 if(!(style.multiLine)) break;
3584 for(c = 0; c<this.line.count && c<this.x; c++)
3586 if(this.line.buffer[c] == '\t')
3587 position += this.tabSize - (position % this.tabSize);
3588 else if(this.line.buffer[c] == ' ')
3596 if(this.x < this.line.count)
3599 //If last character is a { indent one tab
3600 if(this.line.buffer[this.x - 1] == '{')
3602 //Except if the next non space character is a }
3603 bool indent = false;
3605 for(i = this.x; i < this.line.size; i++)
3606 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3608 if(this.line.buffer[i] != '}')
3613 position += this.tabSize;
3616 addString = new char[position + 2];
3617 addString[len++] = '\n';
3618 addString[len] = '\0';
3620 if(stuffAfter || !style.freeCaret)
3622 for(c = 0; c<position; )
3624 if(style.useTab && c + this.tabSize <= position)
3626 addString[len++] = '\t';
3631 addString[len++] = ' ';
3635 addString[len] = '\0';
3639 if(!stuffAfter && style.freeCaret)
3641 this.x = this.selX = position;
3645 SetViewToCursor(true);
3655 if(style.stuckCaret) break;
3656 if(!(style.freeCaret))
3658 this.x = Min(this.x, this.line.count);
3659 this.selX = Min(this.selX, this.selLine.count);
3662 if(!shift) SelDirty();
3665 bool foundAlpha = false;
3668 EditLine line, lastLine;
3671 for(line = this.line; (line && !found); line = line.prev, y--)
3676 if(this.x == 0 && line != this.line)
3685 if(line == this.line) start = this.x -1; else start = line.count-1;
3686 start = Min(start, line.count-1);
3688 for(c = start; c >= 0;)
3691 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3709 byte ch = line.buffer[c];
3710 if(UTF8_IS_FIRST(ch)) break;
3714 // No next word found,
3715 if(!found && ( this.x > 0 || (!line.count && this.x)))
3729 this.line = lastLine;
3740 byte * buffer = line.buffer;
3743 byte ch = buffer[x];
3744 if(UTF8_IS_FIRST(ch)) break;
3761 if(!shift) Deselect();
3762 SetViewToCursor(true);
3768 if(style.stuckCaret) break;
3769 if(!(style.freeCaret))
3771 this.x = Min(this.x, this.line.count);
3772 this.selX = Min(this.selX, this.selLine.count);
3775 if(!shift) SelDirty();
3776 if(!shift && (this.x != this.selX || this.y != this.selY));
3779 bool onAChar = false;
3780 if(this.selX != this.x || this.selY != this.y)
3782 if(this.x<this.line.count)
3783 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3785 if(key.shift && onAChar &&
3786 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3788 bool foundAlpha = false;
3790 EditLine line, lastLine;
3792 int lastC, lastY, lastNumBytes;
3794 for(line = this.line; (line && !found); line = line.next, y++)
3796 int start = (line == this.line) ? this.x : 0;
3799 for(c = start; c < line.count; c += numBytes)
3801 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3806 lastNumBytes = numBytes;
3816 if(!found && (c != this.x || line != this.line))
3829 this.x = lastC + lastNumBytes;
3831 this.line = lastLine;
3838 bool foundAlpha = false;
3843 for(line = this.line; (line && !found); line = line.next, y++)
3845 int start = (line == this.line) ? this.x : 0;
3848 for(c = start; c < line.count; c += numBytes)
3850 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3865 // No next word found,
3866 if(!found && (c != this.x || line != this.line))
3870 this.x = line.count;
3882 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3886 byte * buffer = line.buffer;
3889 byte ch = buffer[x];
3890 if(UTF8_IS_FIRST(ch)) break;
3911 if(!shift) Deselect();
3912 SetViewToCursor(true);
3919 if(!style.vScroll || hasVertScroll) break;
3925 if(style.stuckCaret) break;
3927 if(!shift) SelDirty();
3937 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3941 if(!shift) Deselect();
3943 SetViewToCursor(false);
3946 if(caretY == this.y * space.h)
3952 if(!style.freeCaret)
3953 this.x = Min(this.x, line.count);
3963 int sx = 0, sy = this.y * space.h;
3964 char * text = line.text;
3965 int maxW = clientSize.w - sx;
3966 display.FontExtent(font, " ", 1, null, &th);
3970 int startPos = textPos;
3973 bool lineComplete = false;
3975 if(!style.wrap && caretY == MAXINT)
3978 //textPos = line.count;
3979 //lineComplete = true;
3982 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3986 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3989 len = (nextSpace - (text + textPos));
3991 len = line.count - textPos;
3993 if(textPos < line.count)
3995 display.FontExtent(font, text + textPos, len, &w, null);
3997 if(nextSpace) { w += space.w; len++; }
3999 if(style.wrap && x + width + w > maxW && x > 0)
4001 lineComplete = true;
4011 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4015 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4018 if(this.x > line.count)
4021 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4022 len = this.x - startPos;
4023 display.FontExtent(font, text + startPos, len, &x, null);
4027 if(!shift) Deselect();
4029 SetViewToCursor(false);
4033 if(sy == caretY - th || textPos >= line.count)
4035 if(textPos >= line.count)
4037 int c = textPos - 1;
4038 while(c > 0 && text[c] == ' ') c--;
4042 this.x = line.count;
4045 if(!shift) Deselect();
4047 SetViewToCursor(false);
4052 } while(textPos < line.count);
4055 if(!shift) Deselect();
4057 SetViewToCursor(false);
4066 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4067 if(!shift) SelDirty();
4068 this.line = this.line.prev;
4074 if(!shift) Deselect();
4078 SetViewToCursor(false);
4084 return style.multiLine ? false : true;
4088 if(!style.vScroll || hasVertScroll)
4095 if(style.stuckCaret) break;
4099 int sx = 0, sy = this.y * this.space.h;
4100 int maxW = clientSize.w - sx;
4101 char * text = line.buffer;
4103 if(!shift) SelDirty();
4109 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4119 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4122 if(!shift) Deselect();
4124 if(this.selX != this.x || this.selY != this.y)
4126 SetViewToCursor(false);
4129 while(!textPos || (style.freeCaret || textPos<line.count))
4131 int startPos = textPos;
4134 bool lineComplete = false;
4135 if(!style.wrap && sy <= caretY)
4137 textPos = line.count;
4138 lineComplete = true;
4140 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4144 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4147 len = (nextSpace - (text + textPos));
4149 len = line.count - textPos;
4151 if(textPos < line.count)
4153 display.FontExtent(font, text + textPos, len, &w, &th);
4155 if(nextSpace) { w += space.w; len++; }
4156 if(style.wrap && x + width + w > maxW && x > 0)
4158 lineComplete = true;
4168 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4172 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4175 if(this.x > line.count)
4178 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4180 len = this.x - startPos;
4181 display.FontExtent(font, text + startPos, len, &x, null);
4184 if(!shift) Deselect();
4187 SetViewToCursor(false);
4193 this.x = line.count;
4196 if(!shift) Deselect();
4199 SetViewToCursor(false);
4202 else if(textPos >= line.count && line.next)
4208 sy = this.y * this.space.h;
4209 sx = 0; //textBlock.startX;
4215 sx = 0; //textBlock.startX;
4223 this.x = Min(this.x, line.count);
4224 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4226 if(!shift) Deselect();
4229 if(this.selX != this.x || this.selY != this.y)
4232 SetViewToCursor(false);
4239 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4240 if(!shift) SelDirty();
4241 this.line = this.line.next;
4245 if(!shift) Deselect();
4248 if(this.selX != this.x || this.selY != this.y)
4256 return style.multiLine ? false : true;
4259 if(style.stuckCaret) break;
4260 if(!(style.freeCaret))
4261 this.selX = Min(this.selX, this.selLine.count);
4263 if(!shift) SelDirty();
4266 this.line = this.lines.first;
4267 if(this.y != 0 || this.x != 0)
4277 EditLine line = this.line;
4279 for(c=0; line.buffer[c]; c++)
4280 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4282 if(c != 0 || this.x)
4292 DirtyLine(this.y);*/
4297 if(!shift) Deselect();
4298 SetViewToCursor(true);
4304 if(style.stuckCaret) break;
4305 if(!(style.freeCaret))
4306 this.selX = Min(this.selX, this.selLine.count);
4308 if(!shift) SelDirty();
4313 else if(this.x != this.line.count)
4315 this.x = this.line.count;
4316 //DirtyLine(this.y);
4319 if(!shift) Deselect();
4320 SetViewToCursor(true);
4325 if(style.tabKey && !key.ctrl)
4327 if(this.selY != this.y && style.tabSel)
4329 EditLine firstLine, lastLine;
4333 // Do multi line selection tabbing here
4334 if(this.selY < this.y)
4336 firstLine = this.selLine;
4337 lastLine = this.line;
4343 // Selecting going up
4344 firstLine = this.line;
4345 lastLine = this.selLine;
4352 for(line = firstLine; line; line = line.next, y++)
4354 if(line != lastLine || x)
4358 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4360 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4362 if(line.buffer[c] == '\t')
4367 else if(line.buffer[c] != ' ')
4372 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4375 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4376 char * string = new char[len];
4377 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4378 GetText(string, line, y, 0, line, y, lastC, false, false);
4381 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4382 if(!line.AdjustBuffer(line.count-lastC))
4385 if(style.autoSize) AutoSize();
4389 if(line == lastLine) break;
4394 for(line = firstLine; line; line = line.next, y++)
4398 if(line != lastLine || x)
4402 if(line.count + 1 <= this.maxLineSize)
4404 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4406 if(!line.AdjustBuffer(line.count+1))
4409 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4410 if(style.autoSize) AutoSize();
4412 AddCharAction action { ch = '\t', x = 0, y = y };
4416 memmove(line.buffer+1,line.buffer,line.size-1);
4418 line.buffer[0] = '\t';
4423 if(line.count + this.tabSize <= this.maxLineSize)
4426 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4427 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4428 if(style.autoSize) AutoSize();
4430 if(!line.AdjustBuffer(line.count+this.tabSize))
4434 char * string = new char[this.tabSize + 1];
4435 memset(string, ' ', this.tabSize);
4436 string[this.tabSize] = '\0';
4437 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4440 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4441 line.count+=this.tabSize;
4442 for(c=0; c<this.tabSize; c++)
4443 line.buffer[c] = ' ';
4449 if(line == lastLine) break;
4463 char * addString = new char[this.tabSize + 1];
4465 if(!(style.freeCaret))
4467 this.x = Min(this.x, this.line.count);
4472 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4474 addString[len++] = ' ';
4482 SetViewToCursor(true);
4489 if(!(style.hScroll) || hasHorzScroll) break;
4490 if(this.viewX < this.maxLength)
4492 //this.viewX+=this.space.w*this.tabSize;
4494 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4501 if(!shift) Deselect();
4510 if(!(style.hScroll) || hasHorzScroll) break;
4513 //this.viewX-=this.space.w*this.tabSize;
4514 //this.viewX = Max(this.viewX,0);
4516 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4517 // SetCursorToView();
4524 if(!shift) Deselect();
4538 if(!(style.readOnly))
4544 this.overwrite ^= 1;
4545 UpdateCaretPosition(true);
4550 NotifyOvrToggle(master, this, this.overwrite);
4561 if(style.noSelect) break;
4564 //Save current view position
4565 int tempX = this.viewX;
4566 int tempY = this.viewY;
4570 this.selLine = this.lines.first;
4571 this.y = this.lineCount-1;
4572 this.line = this.lines.last;
4573 this.x = this.line.count;
4576 SetViewToCursor(true);
4578 //Restore previous view position
4579 SetScrollPosition(tempX, tempY * this.space.h);
4585 // TOCHECK: Was there any good reason why we weren't returning false here?
4586 return false; // break;
4591 if(!(style.readOnly))
4599 if(style.readOnly) break;
4605 if(style.readOnly) break;
4610 if(style.readOnly) break;
4614 if(style.readOnly) break;
4618 if(style.readOnly) break;
4619 if(key.shift && key.code == rightBracket)
4621 //Only indent back if you are exactly at one tab.
4623 bool whitespace = true;
4629 EditLine line = this.line;
4631 //Only remove one tab if there is nothing else on the line.
4632 for(i = 0; i < this.line.count; i++)
4634 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4648 for(pos = line.count - 1; pos >= 0; pos--)
4650 char c = line.buffer[pos];
4654 indentwidth += this.tabSize;
4656 //Counting backwards, so when you find a character, indentation is reset
4666 //Place the } to get an undo:
4669 this.x = this.line.count;
4673 putsize = indentwidth;
4675 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4677 newline = new char[putsize+2];
4678 newline[putsize] = '}';
4679 newline[putsize+1] = '\0';
4683 for(; i < indentwidth / this.tabSize; i++)
4685 for(;i < putsize; i++)
4693 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4705 void OnHScroll(ScrollBarAction action, int position, Key key)
4708 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4710 this.viewX = position;
4711 if(action != setRange)
4713 if(!this.mouseMove && style.cursorFollowsView)
4720 void OnVScroll(ScrollBarAction action, int position, Key key)
4722 int oldViewY = this.viewY;
4725 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4727 position /= this.space.h;
4729 if(position < this.viewY)
4731 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4732 style.recomputeSyntax = true;
4734 else if(position > this.viewY)
4736 EditLine oldViewLine = viewLine;
4737 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4738 FigureStartSyntaxStates(oldViewLine, false);
4741 if(action != setRange)
4743 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4746 if(this.x != this.selX || this.y != this.selY)
4750 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4753 int numLines = clientSize.h / this.space.h;
4755 if(Abs(this.viewY - oldViewY) < numLines)
4758 if(this.viewY > oldViewY)
4760 for(y = oldViewY; y <this.viewY; y++)
4761 DirtyLine(y + numLines);
4765 for(y = this.viewY; y <oldViewY; y++)
4766 DirtyLine(y + numLines);
4771 // Fix dirt of stuff before first line...
4772 if(this.viewY - oldViewY > 0)
4774 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4781 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4786 ReplaceTextAction replaceAction = null;
4787 AddCharAction addCharAction = null;
4788 int addedSpaces = 0, addedTabs = 0;
4790 if(ch == '\r') return true;
4791 if(style.stuckCaret /*|EES_READONLY)*/ )
4794 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4796 if(!undoBuffer.dontRecord)
4798 if(selX != x || selY != y)
4803 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4804 oldString = new char[len];
4805 UTF32toUTF8Len(&ch, 1, buffer, 4);
4806 newString = CopyString(buffer);
4807 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4809 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4810 if(selY < y || (selY == y && selX < x))
4812 replaceAction.x1 = selX;
4813 replaceAction.y1 = selY;
4814 replaceAction.x2 = x;
4815 replaceAction.y2 = y;
4819 replaceAction.x1 = x;
4820 replaceAction.y1 = y;
4821 replaceAction.x2 = selX;
4822 replaceAction.y2 = selY;
4824 Record(replaceAction);
4825 undoBuffer.dontRecord++;
4829 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4830 Record(addCharAction);
4836 DelSel(&addedSpaces);
4837 if(this.lineCount+1 > this.maxLines)
4840 Emptyline(this.lines.first,0);
4844 if(!(style.vScroll))
4846 // Make sure it fits, but we need a default line is this.font is too big for window
4847 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4850 if((this.y >= 0) && this.y < this.viewY)
4855 line = EditLine { };
4858 lines.Insert(this.line, line);
4859 line.editBox = this;
4865 // If we're displacing the lines from a current line ...
4868 if(this.line.buffer)
4871 if(this.line.count < endX) endX = this.line.count;
4872 length = this.line.count - endX;
4875 if(!line.AdjustBuffer(length))
4879 if(this.line.buffer)
4881 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4883 if(endX > 4000 || endX < 0)
4886 this.line.count = endX;
4887 this.line.buffer[this.line.count] = '\0';
4888 this.line.AdjustBuffer(this.line.count);
4889 ComputeLength(this.line);
4893 BufferLocation before = { this.line, this.y, this.x }, after;
4899 ComputeLength(this.line);
4902 if(length > 4000 || length < 0)
4905 line.count = length;
4909 line.buffer[line.count] = '\0';
4912 after.line = this.line, after.y = this.y, after.x = this.x;
4914 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4915 if(style.autoSize) AutoSize();
4921 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4922 DelSel(&addedSpaces);
4923 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4924 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4925 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4929 this.selLine = this.line;
4933 replaceAction.x3 = x;
4934 replaceAction.y3 = y;
4935 replaceAction.addedSpaces = addedSpaces;
4936 replaceAction.addedTabs = addedTabs;
4937 undoBuffer.dontRecord--;
4941 addCharAction.addedSpaces = addedSpaces;
4942 addCharAction.addedTabs = addedTabs;
4949 /****************************************************************************
4951 ****************************************************************************/
4954 bool AddCh(unichar ch)
4956 return _AddCh(ch, null, null);
4961 this.modified = true;
4962 NotifyUpdate(master, this);
4963 modifiedDocument = true;
4966 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4969 DelCh(line1, y1, x1, line2, y2, x2, false);
4970 SetViewToCursor(true);
4978 itemEditUndo.disabled = undoBuffer.curAction == 0;
4979 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4980 if(savedAction == undoBuffer.curAction)
4982 modifiedDocument = false;
4984 NotifyUnsetModified(master, this);
4991 itemEditUndo.disabled = undoBuffer.curAction == 0;
4992 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4993 if(savedAction == undoBuffer.curAction)
4995 modifiedDocument = false;
4997 NotifyUnsetModified(master, this);
5001 void Record(UndoAction action)
5003 if(!undoBuffer.dontRecord)
5005 undoBuffer.Record(action);
5006 itemEditUndo.disabled = undoBuffer.curAction == 0;
5007 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5014 this.lines.first, 0,0,
5015 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5018 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5023 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5024 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5025 this.y = line2 ? y2 : (this.lineCount-1);
5026 this.line = line2 ? (EditLine)line2 : this.lines.last;
5029 SetViewToCursor(true);
5033 // TODO: Fix this vs modifiedDocument window property
5034 void SetModified(bool flag)
5038 this.modified = false;
5039 if(flag && !NotifyModified(master, this))
5040 this.modified = true;
5045 bool AddS(char * string)
5052 int addedSpaces = 0, addedTabs = 0;
5053 AddTextAction action = null;
5054 ReplaceTextAction replaceAction = null;
5056 this.pasteOperation = true;
5058 if(style.stuckCaret /*|EES_READONLY)*/ )
5061 if(!undoBuffer.dontRecord)
5063 char * placeString = CopyString(string);
5064 if(!style.multiLine)
5068 for(i = 0; (ch = placeString[i]); i++)
5071 placeString[i] = '\0';
5072 placeString = renew placeString byte[i+1];
5077 if(selX != x || selY != y)
5081 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5082 oldString = new char[len];
5083 newString = placeString;
5084 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5086 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5087 if(selY < y || (selY == y && selX < x))
5089 replaceAction.x1 = selX;
5090 replaceAction.y1 = selY;
5091 replaceAction.x2 = x;
5092 replaceAction.y2 = y;
5096 replaceAction.x1 = x;
5097 replaceAction.y1 = y;
5098 replaceAction.x2 = selX;
5099 replaceAction.y2 = selY;
5101 Record(replaceAction);
5105 if(string[0] == '\n')
5106 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5108 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5116 undoBuffer.dontRecord++;
5117 DelSel(&addedSpaces);
5121 for(c = 0; string[c]; c++)
5123 if(string[c] == '\n' || string[c] == '\r')
5125 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5130 if(string[c] == '\n')
5139 // Reset for next line
5143 if(string[c] == '\r' && *line == '\n')
5155 // Why was this here?
5158 // Add the line here
5160 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5166 undoBuffer.dontRecord--;
5171 action.addedSpaces = addedSpaces;
5172 action.addedTabs = addedTabs;
5174 else if(replaceAction)
5176 replaceAction.y3 = y;
5177 replaceAction.x3 = x;
5178 replaceAction.addedSpaces = addedSpaces;
5179 replaceAction.addedTabs = addedTabs;
5182 UpdateCaretPosition(true);
5184 this.pasteOperation = false;
5197 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5198 SetViewToCursor(true);
5204 void PutCh(unichar ch)
5208 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5209 //if((ch >= 32) || ch == '\n')
5211 int addedSpaces = 0, addedTabs = 0;
5212 ReplaceTextAction replaceAction = null;
5213 AddCharAction addCharAction = null;
5216 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5218 if(this.x < this.line.count && this.overwrite)
5223 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5224 oldString = new char[len];
5225 UTF32toUTF8Len(&ch, 1, buffer, 4);
5226 newString = CopyString(buffer);
5227 GetText(oldString, line, y, x, line, y, x+1, false, false);
5228 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5229 replaceAction.x1 = x;
5230 replaceAction.y1 = y;
5231 replaceAction.x2 = x+1;
5232 replaceAction.y2 = y;
5233 Record(replaceAction);
5235 undoBuffer.dontRecord++;
5236 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5237 undoBuffer.dontRecord--;
5239 else if(!undoBuffer.dontRecord)
5241 if(selX != x || selY != y)
5246 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5247 oldString = new char[len];
5248 UTF32toUTF8Len(&ch, 1, buffer, 4);
5249 newString = CopyString(buffer);
5250 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5251 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5252 if(selY < y || (selY == y && selX < x))
5254 replaceAction.x1 = selX;
5255 replaceAction.y1 = selY;
5256 replaceAction.x2 = x;
5257 replaceAction.y2 = y;
5261 replaceAction.x1 = x;
5262 replaceAction.y1 = y;
5263 replaceAction.x2 = selX;
5264 replaceAction.y2 = selY;
5266 Record(replaceAction);
5270 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5271 Record(addCharAction);
5274 undoBuffer.dontRecord++;
5275 result = _AddCh(ch, &addedSpaces, &addedTabs);
5278 replaceAction.x3 = x;
5279 replaceAction.y3 = y;
5280 replaceAction.addedSpaces = addedSpaces;
5281 replaceAction.addedTabs = addedTabs;
5285 addCharAction.addedSpaces = addedSpaces;
5286 addCharAction.addedTabs = addedTabs;
5288 undoBuffer.dontRecord--;
5292 if(result) SetViewToCursor(true);
5296 void PutS(char * string)
5301 SetViewToCursor(true);
5306 void Printf(char * format, ...)
5310 char temp[MAX_F_STRING];
5312 va_start(args, format);
5313 vsprintf(temp, format, args);
5319 void SetContents(char * format, ...)
5324 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5327 char temp[MAX_F_STRING];
5329 va_start(args, format);
5330 vsprintf(temp, format, args);
5346 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5349 else if(this.line.prev)
5351 EditLine line = this.line.prev;
5355 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5363 this.selLine = this.line;
5368 SetViewToCursor(true);
5373 Emptyline(this.line,this.y);
5374 this.selX = this.x = 0;
5377 this.selLine = this.line;
5379 SetViewToCursor(true);
5389 SetViewToCursor(true);
5397 SetViewToCursor(true);
5401 bool GoToLineNum(int lineNum)
5406 EditLine line = this.lines.first;
5407 for(c = 0; c < lineNum && line; c++, line = line.next);
5417 SetViewToCursor(true);
5424 bool GoToPosition(EditLine line, int y, int x)
5436 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5450 SetViewToCursor(true);
5457 void SetViewToCursor(bool setCaret)
5467 bool dontScroll = false;
5473 selected = selX != this.x || selY != y;
5480 checkLine = dropLine;
5486 checkLine = this.line;
5491 numLines = clientSize.h / space.h;
5493 // This is broken. The EditBox now doesn't do anything different when adding to it,
5494 // regardless of the previous scrolling position. It should be read and then set again
5495 // if one wishes to preserve it.
5496 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5497 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5501 // Horizontal Adjustment
5502 if(!dontScroll && checkLine)
5506 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5509 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5515 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5516 viewX = x - clientSize.w+space.w;
5517 if(x < this.viewX + clientSize.w/2 - space.w)
5518 viewX = Max(0, x - clientSize.w/2 + space.w);
5526 // Vertical Adjustment
5527 if(viewY > checkY) viewY = checkY;
5528 if(viewY + numLines <= checkY)
5530 if(clientSize.h >= space.h)
5532 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5542 for(;dropLine && dropLine.prev && dropY >= numLines;)
5544 dropLine = dropLine.prev;
5548 for(;this.line && this.line.prev && this.y >= numLines;)
5550 this.line = this.line.prev;
5555 SetScrollPosition(viewX, viewY * this.space.h);
5557 UpdateCaretPosition(setCaret);
5563 selLine = this.line;
5573 void CenterOnCursor()
5575 int numLines = clientSize.h / this.space.h;
5576 int y = this.y - numLines / 2;
5578 bool figureSyntax = false;
5579 EditLine oldViewLine = viewLine;
5580 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5585 for(;y < this.viewY; y++)
5587 this.viewLine = this.viewLine.prev;
5588 style.recomputeSyntax = true;
5590 for(;y > this.viewY; y--)
5592 this.viewLine = this.viewLine.next;
5593 figureSyntax = true;
5596 FigureStartSyntaxStates(oldViewLine, false);
5600 SetScrollPosition(this.viewX, viewY * this.space.h);
5601 UpdateCaretPosition(true);
5605 void SetCursorToView()
5616 numLines = clientSize.h / this.space.h;
5620 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5621 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5625 EditLine oldLine = this.line;
5626 bool lastOne = false;
5627 EditLine oldViewLine = this.viewLine;
5628 bool figureSyntax = false;
5630 if(this.y >= this.lineCount-1) return;
5632 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5634 if(this.viewY + numLines < this.lines.count)
5636 this.viewLine = this.viewLine.next;
5638 figureSyntax = true;
5640 else if(c && !lastOne)
5649 FigureStartSyntaxStates(oldViewLine, false);
5650 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5653 SetViewToCursor(false);
5662 if(this.y == 0) return;
5664 numLines = clientSize.h / this.space.h;
5668 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5669 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5673 EditLine oldLine = this.line;
5675 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5680 if(this.viewLine.prev)
5682 this.viewLine = this.viewLine.prev;
5684 style.recomputeSyntax = true;
5688 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5691 SetViewToCursor(false);
5698 if(this.viewLine.prev)
5701 // this.viewLine = this.viewLine.prev;
5704 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5711 if(this.viewLine.next)
5714 // this.viewLine = this.viewLine.next;
5717 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5724 EditLine l1, l2, line;
5725 int x1, x2, nx1, nx2;
5730 if(!this.selLine) return 0;
5731 if(this.selLine == this.line && this.selX == this.x) return 0;
5732 if(this.selY < this.y)
5739 else if(this.selY > this.y)
5746 else if(this.selX < this.x)
5748 l1 = l2 = this.line;
5754 l1 = l2 = this.line;
5758 nx1 = Min(x1,l1.count);
5759 nx2 = Min(x2,l2.count);
5761 // Find Number of Bytes Needed
5763 for(line = l1; line; line = line.next)
5765 if(line == l1) start = nx1; else start = 0;
5766 if(line == l2) end = nx2; else end = line.count;
5768 if(style.freeCaret && line == l2)
5771 count = Max(x2-Max(x1,l1.count),0);
5773 count = Max(x2-l2.count,0);
5777 if(line == l2) break;
5778 // Add Carriage Return / line Feed
5785 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5789 if(!reorder || this.selY < this.y)
5791 if(l1) *l1 = this.selLine;
5792 if(y1) *y1 = this.selY;
5793 if(x1) *x1 = this.selX;
5794 if(l2) *l2 = this.line;
5795 if(y2) *y2 = this.y;
5796 if(x2) *x2 = this.x;
5798 else if(this.selY > this.y)
5800 if(l1) *l1 = this.line;
5801 if(y1) *y1 = this.y;
5802 if(x1) *x1 = this.x;
5803 if(l2) *l2 = this.selLine;
5804 if(y2) *y2 = this.selY;
5805 if(x2) *x2 = this.selX;
5807 else if(this.selX < this.x)
5809 if(l1) *l1 = this.line;
5810 if(y1) *y1 = this.selY;
5811 if(x1) *x1 = this.selX;
5812 if(l2) *l2 = this.line;
5813 if(y2) *y2 = this.y;
5814 if(x2) *x2 = this.x;
5818 if(l1) *l1 = this.line;
5819 if(y1) *y1 = this.y;
5820 if(x1) *x1 = this.x;
5821 if(l2) *l2 = this.line;
5822 if(y2) *y2 = this.selY;
5823 if(x2) *x2 = this.selX;
5828 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5830 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5832 this.selLine = (EditLine)l1;
5835 this.line = (EditLine)l2;
5839 SetViewToCursor(true);
5843 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5845 EditLine l1, l2, line;
5846 int x1, x2, nx1, nx2;
5877 nx1 = Min(x1,l1.count);
5878 nx2 = Min(x2,l2.count);
5881 for(line = l1; line; line = line.next)
5883 if(line == l1) start = nx1; else start = 0;
5884 if(line == l2) end = nx2; else end = line.count;
5887 CopyBytes(text, line.buffer + start, end - start);
5890 numChars += end-start;
5892 if(style.freeCaret && line == l2 && addSpaces)
5895 count = Max(x2-Max(x1,l1.count),0);
5897 count = Max(x2-l2.count,0);
5900 FillBytes(text,' ',count);
5906 if(line == l2) break;
5918 // '\0' terminate Terminate
5925 void GetSel(char * text, bool addCr)
5927 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5933 this.selLine = this.line;
5943 int size = SelSize();
5946 // Try to allocate memory
5947 ClipBoard clipBoard { };
5948 if(clipBoard.Allocate(size+1))
5950 GetSel(clipBoard.memory, true);
5963 ClipBoard clipBoard { };
5964 if(clipBoard.Load())
5965 PutS(clipBoard.memory);
5976 SetViewToCursor(true);
5981 void DeleteSelection()
5986 SetViewToCursor(true);
5992 void Save(File f, bool cr)
5995 savedAction = undoBuffer.curAction;
5997 for(line = this.lines.first; line; line = line.next)
5999 f.Write(line.buffer, line.count,1);
6002 if(cr) f.Putc('\r');
6008 #define BUFFER_SIZE 16384
6011 undoBuffer.dontRecord++;
6014 char buffer[BUFFER_SIZE];
6018 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6019 buffer[count] = '\0';
6025 undoBuffer.dontRecord--;
6026 undoBuffer.count = 0;
6027 undoBuffer.curAction = 0;
6028 itemEditUndo.disabled = undoBuffer.curAction == 0;
6029 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6032 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6036 bool firstPass = true;
6037 EditBoxFindResult result = found;
6039 if(!this.line) return notFound;
6042 for(line = this.line;;)
6050 line = this.lines.first;
6056 line = this.lines.last;
6057 num = this.lineCount - 1;
6063 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6065 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6069 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6072 if(line == this.line && !firstPass) break;
6090 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6094 int searchLen = strlen(text);
6095 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6097 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6098 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6100 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6107 bool OnKeyDown(Key key, unichar ch)
6110 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6112 if(!NotifyKeyDown(master, this, key, ch))
6122 this.mouseMove = false;
6123 OnLeftButtonUp(0,0,0);
6124 SetViewToCursor(true);
6134 public class EditBoxStream : File
6137 BufferLocation start, sel;
6144 EditBox editBox = this.editBox;
6146 editBox.x = start.x;
6147 editBox.y = start.y;
6148 editBox.line = start.line;
6150 editBox.selX = sel.x;
6151 editBox.selY = sel.y;
6152 editBox.selLine = sel.line;
6154 editBox.SetViewToCursor(true);
6155 //editBox.ComputeColumn();
6159 property EditBox editBox
6167 start.line = value.line;
6171 sel.line = value.selLine;
6174 value.GoToHome(true);
6177 get { return editBox; }
6180 uint Read(byte * buffer, uint size, uint count)
6183 EditBox editBox = this.editBox;
6184 EditLine line = editBox.line;
6190 for(;read < count && line; line = (*&line.next))
6192 int numBytes = Min(count - read, (*&line.count) - x);
6195 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6200 for(;read < count && x < (*&line.count);)
6202 buffer[read++] = (*&line.buffer)[x++];
6209 buffer[read++] = '\n';
6214 if(x == (*&line.count) && (*&line.next))
6223 editBox.line = editBox.selLine = line;
6224 editBox.x = editBox.selX = x;
6225 editBox.y = editBox.selY = y;
6230 bool Seek(int pos, FileSeekMode mode)
6233 EditBox editBox = this.editBox;
6234 EditLine line = editBox.line;
6236 if(mode == FileSeekMode::start)
6238 pos = pos - this.pos;
6248 for(;read < pos && line; line = (*&line.next))
6250 int numBytes = Min(pos - read, (*&line.count) - x);
6253 read += numBytes; x += numBytes;
6256 /*for(;read < pos && x < (*&line.count);)
6284 for(;read < pos && line; line = (*&line.prev))
6286 int numBytes = Min(pos - read, x);
6292 /*for(;read < pos && x > 0;)
6302 x = (*&(*&line.prev).count);
6318 editBox.line = editBox.selLine = line;
6319 editBox.x = editBox.selX = x;
6320 editBox.y = editBox.selY = y;
6325 bool Puts(char * string)
6327 EditBox editBox = this.editBox;
6328 BufferLocation start { editBox.line, editBox.y, editBox.x };
6332 editBox.AddS(string);
6334 pos.line = editBox.line;
6338 this.start.AdjustAdd(start, pos);
6339 sel.AdjustAdd(start, pos);
6344 // NOTE: BYTE, NOT UNICODE CHARACTER!
6347 EditBox editBox = this.editBox;
6348 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6353 utf8Bytes[numBytes++] = ch;
6354 utf8Bytes[numBytes] = 0;
6355 if(UTF8Validate(utf8Bytes))
6357 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6359 pos.line = editBox.line;
6362 this.start.AdjustAdd(start, pos);
6363 sel.AdjustAdd(start, pos);
6370 bool Getc(char * ch)
6372 return Read(ch, 1, 1) ? true : false;
6375 void DeleteBytes(uint count)
6377 EditBox editBox = this.editBox;
6380 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6381 BufferLocation end = pos;
6386 for(;c < count && end.line && end.x < end.line.count;)
6391 if(c < count && end.line && end.line.next)
6393 end.line = end.line.next;
6402 start.AdjustDelete(pos, end);
6403 sel.AdjustDelete(pos, end);
6405 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);