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(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1329 if(inString && !wasEscaped)
1338 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1340 if(inQuotes && !wasEscaped)
1352 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1359 else if(ch != ' ' && ch != '\t')
1362 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1365 style.continuedSingleLineComment = continuedSingleLineComment;
1366 style.inMultiLineComment = inMultiLineComment;
1367 style.inPrep = inPrep;
1368 style.escaped = escaped;
1372 /*void OnDrawOverChildren(Surface surface)
1374 if(style.lineNumbers)
1376 int currentLineNumber = this.viewY + 1;
1379 for(i = 0; i * space.h < box.height; i++)
1381 // ********* LINE NUMBERING *********
1382 surface.SetForeground(Color{60, 60, 60});
1383 //Highlight current line
1384 if(this.caretY / space.h == currentLineNumber - 1)
1385 surface.SetBackground(Color{220, 220, 220});
1387 surface.SetBackground(Color{230, 230, 230});
1388 surface.textOpacity = true;
1390 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1391 if(currentLineNumber > this.lineCount)
1392 surface.WriteText(0,i*space.h+1," ",6);
1394 surface.WriteText(0,i*space.h+1,lineText,6);
1396 currentLineNumber++;
1401 void OnRedraw(Surface surface)
1405 bool selected = false, selection = true;
1407 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1408 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1409 Color defaultTextColor = property::foreground;
1412 int maxW = clientSize.w;
1414 Color foreground, background;
1417 // Overwrite Caret Stuff
1418 bool overWrite = false;
1419 int overWriteX, overWriteY;
1422 // ****** SYNTAX STATES ******
1423 bool inMultiLineComment = style.inMultiLineComment;
1424 bool inString = false;
1425 bool inQuotes = false;
1426 bool inPrep = style.inPrep;
1427 bool inSingleLineComment = false;
1428 bool escaped = style.escaped;
1429 bool continuedSingleLineComment = style.continuedSingleLineComment;
1430 // ****** ************* ******
1433 defaultTextColor = Color { 85, 85, 85 };
1434 textColor = defaultTextColor;
1437 Abs(selectionBackground.r - property::background.r) +
1438 Abs(selectionBackground.g - property::background.g) +
1439 Abs(selectionBackground.b - property::background.b) < 92)
1441 selectionBackground = formColor;
1442 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1445 surface.TextFont(this.font);
1447 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1448 surface.Area(0,0,MAXINT,MAXINT);
1453 surface.SetBackground(formColor);
1454 surface.Area(0,0,clientSize.w, clientSize.h);
1457 if(this.selX == this.x && this.selY == this.y)
1467 editX = Min(this.x,this.line.count);
1468 selX = Min(this.selX,this.selLine.count);
1471 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1473 foreground = selected ? selectionForeground : textColor;
1474 background = selectionBackground;
1480 surface.SetForeground(foreground);
1481 surface.SetBackground(background);
1482 surface.TextOpacity(opacity);
1484 surface.GetBox(box);
1486 for(line = this.viewLine; line; line = line.next)
1488 int x = -this.viewX;
1492 Color newTextColor = textColor = defaultTextColor;
1493 bool lineComplete = false;
1496 // ****** SYNTAX HIGHLIGHTING ******
1497 bool lastWasStar = false;
1498 bool firstWord = true;
1499 if(!escaped) inPrep = false;
1500 inSingleLineComment = continuedSingleLineComment;
1504 // *********************************
1506 /* === DEBUGGING TOOL FOR MAXLINE ===
1508 if(line == this.maxLine)
1510 surface.SetBackground(GREEN|0xFF000000);
1511 surface.TextOpacity(true);
1515 surface.TextOpacity(selected ? true : false);
1516 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1520 if(line == this.selLine && line == this.line)
1522 end = Max(line.count, this.x);
1523 end = Max(end, this.selX);
1525 else if(line == this.selLine)
1526 end = Max(line.count, this.selX);
1527 else if(line == this.line)
1528 end = Max(line.count, this.x);
1536 bool spacing = true;
1537 bool cantHaveWords = false;
1544 lineComplete = false;
1547 textColor = newTextColor;
1550 foreground = textColor;
1551 surface.SetForeground(textColor);
1555 for(; c<end && !cantHaveWords;)
1559 bufferLen += wordLen;
1563 /*if(line.count > 4000)
1570 for(; c<line.count; c++)
1572 unichar ch = line.buffer[c];
1573 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1574 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1575 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1576 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1577 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1585 for(; c<line.count; c++)
1587 unichar ch = line.buffer[c];
1588 if(ch == '\t' || ch == ' ')
1590 cantHaveWords = true;
1594 if(ch != ' ' && ch != '\t')
1599 if(c == line.count && c < end)
1608 cantHaveWords = true;
1613 lastWasStar = false;
1619 bool backEscaped = escaped;
1620 bool backLastWasStar = lastWasStar;
1621 bool backInMultiLineComment = inMultiLineComment;
1622 bool backInString = inString;
1623 bool backInQuotes = inQuotes;
1624 bool backInPrep = inPrep;
1625 bool backInSingleLineComment = inSingleLineComment;
1627 char * word = line.buffer + c - wordLen;
1629 bool wasEscaped = escaped;
1631 lastWasStar = false;
1633 // Determine Syntax Highlighting
1634 newTextColor = defaultTextColor;
1637 if(inSingleLineComment || inMultiLineComment)
1639 newTextColor = colorScheme.commentColor;
1643 newTextColor = colorScheme.charLiteralColor;
1647 newTextColor = colorScheme.stringLiteralColor;
1651 newTextColor = colorScheme.preprocessorColor;
1653 if(wordLen == 1 && word[0] == '/')
1655 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1659 inSingleLineComment = true;
1660 newTextColor = colorScheme.commentColor;
1662 else if(word[1] == '*')
1664 inMultiLineComment = true;
1665 newTextColor = colorScheme.commentColor;
1668 else if(backLastWasStar)
1669 inMultiLineComment = false;
1671 else if(wordLen == 1 && word[0] == '*')
1673 if(!c || word[-1] != '/')
1676 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1678 if(inString && !wasEscaped)
1685 newTextColor = colorScheme.stringLiteralColor;
1688 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1690 if(inQuotes && !wasEscaped)
1695 newTextColor = colorScheme.charLiteralColor;
1698 else if(wordLen == 1 && word[0] == '\\')
1703 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1704 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1706 newTextColor = colorScheme.numberColor;
1710 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1715 newTextColor = colorScheme.preprocessorColor;
1718 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1720 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1722 char ** keys = keyWords[g];
1723 int * len = keyLen[g];
1724 for(ccc = 0; keys[ccc]; ccc++)
1726 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1728 newTextColor = colorScheme.keywordColors[g];
1737 // If highlighting for this word is different, break
1738 if(newTextColor != textColor)
1742 // Better solution than going back?
1745 // Reset syntax flags
1746 escaped = backEscaped;
1747 lastWasStar = backLastWasStar;
1748 inMultiLineComment = backInMultiLineComment;
1749 inString = backInString;
1750 inQuotes = backInQuotes;
1751 inPrep = backInPrep;
1752 inSingleLineComment = backInSingleLineComment;
1757 textColor = newTextColor;
1760 foreground = textColor;
1761 surface.SetForeground(textColor);
1768 // If we're not breaking, this can't be rendered as spacing anymore
1771 // Do word wrapping here
1777 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1782 w += numSpaces * space.w;
1784 if(x + viewX + w > maxW)
1787 lineComplete = true;
1792 bufferLen += wordLen;
1797 int renderStart = start;
1802 // Render checking if we need to split because of selection or to find where to draw insert caret
1803 for(wc = start; wc < start + bufferLen; wc++)
1805 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1806 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1807 if(overWrite == true)
1809 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1810 if(overWriteCh == '\t') overWriteCh = ' ';
1815 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1816 if(overWrite == true)
1824 surface.TextOpacity(opacity);
1825 surface.SetBackground(background);
1826 surface.SetForeground(foreground);
1833 if(wc < line.count && line.buffer[wc] == '\t')
1835 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1839 numSpaces += space.w;
1843 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1848 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1849 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1851 if(overWrite == true)
1858 surface.TextOpacity(opacity);
1859 surface.SetBackground(background);
1860 surface.SetForeground(foreground);
1863 if(style.freeCaret && selected)
1865 surface.SetBackground(selectionBackground);
1866 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1867 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1872 if(style.freeCaret && selected)
1874 surface.SetBackground(selectionBackground);
1875 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1879 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1882 if(y > box.bottom) // >=clientSize.h)
1888 surface.TextOpacity(true);
1889 surface.SetForeground(black);
1890 surface.SetBackground(Color {255,255,85});
1891 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1895 void FixScrollArea()
1897 if(style.hScroll || style.vScroll)
1899 int width = maxLength + XOFFSET;
1900 int height = lineCount * space.h;
1901 if(style.freeCaret && line)
1906 width = Max(line.length + (x - line.count) * space.w, maxLength);
1910 if(selX > selLine.count)
1911 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1916 SetScrollLineStep(8, space.h);
1917 SetScrollArea(width, height, true);
1921 void ComputeLength(EditLine line)
1928 for(c = 0; c < line.count; )
1937 for(len = 0; c < line.count; c += numBytes)
1939 ch = line.buffer[c];
1940 numBytes = UTF8_NUM_BYTES(ch);
1942 if(ch == ' ' || ch == '\t')
1949 if(!len && ch == ' ')
1954 else if(!len && ch == '\t')
1956 w = (tabSize * space.w) - (x % (tabSize * space.w));
1960 FontExtent(display, font, line.buffer + start, len, &w, null);
1971 if(line.length > this.maxLength)
1973 this.maxLine = line;
1974 this.maxLength = line.length;
1983 this.maxLine = null;
1985 for(line = lines.first; line; line = line.next)
1987 if(line.length > this.maxLength)
1989 this.maxLength = line.length;
1990 this.maxLine = line;
1997 if(this.selY != this.y)
1999 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2003 void ComputeColumn()
2005 // Adjust new edit X position according to tabs
2006 int c, position = 0;
2009 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2011 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2012 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2014 position += this.tabSize - (position % this.tabSize);
2018 position += this.x - c;
2019 this.col = position;
2022 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2024 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2027 bool HasCommentOrEscape(EditLine line)
2029 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2034 for(c = line.count-1; c >= 0; c--)
2036 char ch = line.buffer[c];
2042 else //if(ch != ' ' && ch != '\t')
2049 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2051 EditLine line = l1, next;
2053 int oldCount1, oldCount2;
2054 int y, firstViewY, firstY, firstDropY, firstSelY;
2057 DelTextAction action = null;
2058 bool hadComment = false;
2062 hadComment = HasCommentOrEscape(line);
2064 if(y2 > y1 || c2 > c1)
2066 if(start < l1.count)
2068 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2072 oldCount1 = l1.count;
2074 while(c1 < oldCount1)
2076 byte ch = buffer[c1];
2077 if(UTF8_IS_FIRST(ch)) break;
2081 oldCount2 = l2.count;
2083 while(c2 < oldCount2)
2085 byte ch = buffer[c2];
2086 if(UTF8_IS_FIRST(ch)) break;
2091 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2096 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2097 string = new char[len];
2098 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2099 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2103 //oldCount1 = l1.count;
2104 //oldCount2 = l2.count;
2107 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2108 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2111 if(c2 > oldCount2) c2 = oldCount2;
2112 if(!(style.freeCaret))
2113 if(c1 > oldCount1) c1 = oldCount1;
2114 newLineCount = c1 + l2.count-c2;
2120 buffer = new char[line.size ? line.size : 1];
2122 buffer = new char[line.size];
2124 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2129 if(!line.AdjustBuffer(newLineCount))
2133 /*if(newLineCount > 4000 || newLineCount < 0)
2134 printf("Warning");*/
2136 line.count = newLineCount;
2138 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2143 action.addedSpaces = c1-oldCount1;
2145 if(action.addedSpaces > action.x1)
2151 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2152 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2154 line.buffer[line.count] = '\0';
2161 this.dropX -= c2-c1;
2162 this.dropX = Max(this.dropX,0);
2164 this.x = Max(this.x,0);
2171 firstViewY = this.viewY;
2173 firstDropY = this.dropY;
2174 firstSelY = this.selY;
2175 for(line = l1;line;line = next, y++)
2183 if(line == this.viewLine)
2185 if(this.viewLine.next)
2187 this.viewLine = this.viewLine.next;
2189 style.recomputeSyntax = true;
2193 this.viewLine = this.viewLine.prev;
2195 style.recomputeSyntax = true;
2198 else if(y < firstViewY)
2200 if(line == this.line)
2204 this.line = this.line.next;
2205 this.x = this.line.count;
2210 this.line = this.line.prev;
2211 this.x = this.line.count;
2218 if(line == this.dropLine)
2220 if(this.dropLine.next)
2222 this.dropLine = this.dropLine.next;
2223 this.dropX = this.dropLine.count;
2227 this.dropLine = this.dropLine.prev;
2228 this.dropX = this.dropLine.count;
2232 else if(y < firstDropY)
2234 if(line == this.selLine)
2236 if(this.selLine.next)
2238 this.selLine = this.selLine.next;
2239 this.selX = this.selLine.count;
2243 this.selLine = this.selLine.prev;
2244 this.selX = this.selLine.count;
2248 else if(y < firstSelY)
2252 if(line == l2) break;
2256 if(style.autoSize) AutoSize();
2257 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2260 style.recomputeSyntax = true;
2265 bool DelSel(int * addedSpacesPtr)
2267 if(this.line != this.selLine || this.x != this.selX)
2269 if(this.selY < this.y)
2271 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2274 this.line = this.selLine;
2276 else if(this.selY > this.y)
2278 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2281 this.selLine = this.line;
2283 else if(this.selX < this.x)
2285 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2288 this.line = this.selLine;
2292 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2295 this.selLine = this.line;
2303 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2305 bool hadComment = false;
2306 // Add the line here
2307 EditLine line = this.line;
2309 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2310 if(!style.hScroll && created)
2312 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2317 // Lock if no place to display.
2320 else if(endX > this.x)
2321 max = Max(this.x, line.count);
2325 for(x = 0, c = 0; c < max+count; )
2330 if(c < Min(this.x, line.count))
2331 string = line.buffer + c;
2334 else if(c < endX + count)
2335 string = stringLine + c - endX;
2337 string = line.buffer + c - endX - count;
2341 w = (tabSize * space.w) - (x % (tabSize * space.w));
2345 numBytes = UTF8_NUM_BYTES(*string);
2346 FontExtent(display, this.font, string, numBytes, &w, null);
2350 if(x >= clientSize.w)
2355 c += numBytes; // - 1;
2361 int addedSpaces = 0;
2364 // Add blank spaces if EES_FREECARET
2365 if(this.x > line.count)
2375 for(c = 0; c<line.count; c++)
2377 if(this.line.buffer[c] == '\t')
2378 position += this.tabSize - (position % this.tabSize);
2382 wantedPosition = position + (this.x - line.count);
2384 // A tab is too much...
2385 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2386 addedSpaces = wantedPosition - position;
2391 position += this.tabSize - (position % this.tabSize);
2392 // Add as many tabs as needed
2393 addedTabs += (wantedPosition - position) / this.tabSize;
2394 position += (addedTabs-1) * this.tabSize;
2395 // Finish off with spaces
2396 addedSpaces = wantedPosition - position;
2400 addedSpaces = this.x - line.count;
2403 this.x = Min(this.x, line.count);
2405 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2411 hadComment = HasCommentOrEscape(line);
2413 // Adjust the size of the line
2414 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2418 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2421 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2422 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2425 *addedTabsPtr = addedTabs;
2426 FillBytes(line.buffer+line.count,'\t',addedTabs);
2428 if(addedTabs > 4000 || addedTabs < 0)
2431 line.count += addedTabs;
2437 FillBytes(line.buffer+line.count,' ',addedSpaces);
2439 if(addedSpaces > 4000 || addedSpaces < 0)
2442 line.count += addedSpaces;
2443 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2445 else if(addedSpacesPtr)
2446 *addedSpacesPtr = 0;
2448 if(count > 4000 || count < 0)
2451 line.count += count;
2452 this.x += count + addedTabs + addedSpaces;
2455 this.selLine = this.line;
2457 line.buffer[line.count] = '\0';
2459 ComputeLength(line);
2464 hasComment = HasCommentOrEscape(line);
2465 if(!undoBuffer.insideRedo)
2467 int backDontRecord = undoBuffer.dontRecord;
2468 undoBuffer.dontRecord = 0;
2469 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2470 if(style.autoSize) AutoSize();
2471 undoBuffer.dontRecord = backDontRecord;
2473 if(style.syntax && (hadComment || hasComment || line != this.line))
2475 style.recomputeSyntax = true;
2483 void Emptyline(EditLine line, int y)
2486 DelCh(line, y, 0, line.next, y+1, 0, true);
2488 DelCh(line, y, 0, line, y, line.count, true);
2491 void GoToEnd(bool deselect)
2495 this.line = this.lines.last;
2496 if(this.y != this.lineCount-1)
2498 else if (this.x != this.line.count)
2499 DirtyLine(this.lineCount-1);
2500 this.y = this.lineCount-1;
2501 this.x = this.line.count;
2508 void GoToHome(bool deselect)
2512 this.line = this.lines.first;
2515 else if (this.x !=0)
2516 DirtyLine(this.lineCount-1);
2525 // Returns true if it needs scrolling
2526 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2532 bool needHScroll = false;
2539 line = this.viewLine ? (void *)this.viewLine.prev : null;
2544 line = (void *)this.lines.first;
2549 py = Min(py, clientSize.h);
2551 py = Min(py, this.lineCount);
2553 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2559 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2562 px = Min(px,clientSize.w+this.space.w);
2566 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2569 if(tline) *tline = line;
2572 // Prevent divide by 0 from non valid this.font
2574 return (y < this.viewY) || needHScroll;
2576 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2580 // Minimal Update Management Functions
2584 this.endY = clientSize.h-1;
2585 // ErrorLog("DirtyAll\n");
2588 void DirtyEnd(int y)
2590 if((y - this.viewY)*this.space.h < this.startY)
2591 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2592 this.endY = clientSize.h-1;
2593 //ErrorLog("DirtyEnd %d\n", y);
2596 void DirtyLine(int y)
2600 if((y - this.viewY)*this.space.h < this.startY)
2601 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2602 if((y - this.viewY+1)*this.space.h > this.endY)
2603 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2605 //ErrorLog("DirtyLine %d\n", y);
2612 if(style.recomputeSyntax)
2614 FigureStartSyntaxStates(lines.first, true);
2615 style.recomputeSyntax = false;
2618 if(this.startY > this.endY) return;
2619 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2625 box.right = clientSize.w-1;
2626 box.top = this.startY;
2627 box.bottom = this.endY;
2630 this.startY = clientSize.h;
2634 bool IsMouseOnSelection()
2636 bool mouseOnSelection = false;
2639 int minY = Min(this.selY, this.y);
2640 int maxY = Max(this.selY, this.y);
2641 int minX = Min(this.selX, this.x);
2642 int maxX = Max(this.selX, this.x);
2644 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2646 if(maxX != minX || maxY != minY)
2648 if(y > minY && y < maxY)
2649 mouseOnSelection = true;
2650 else if(y == minY && y == maxY)
2651 mouseOnSelection = (x < maxX && x >= minX);
2655 mouseOnSelection = (x >= this.selX);
2656 else if(y == this.y)
2657 mouseOnSelection = (x >= this.x);
2662 mouseOnSelection = (x < this.selX);
2663 else if(y == this.y)
2664 mouseOnSelection = (x < this.x);
2667 return mouseOnSelection;
2670 void UpdateCaretPosition(bool setCaret)
2674 if(mouseMove || (!overwrite && !style.noCaret))
2676 int max = this.mouseMove ? this.dropX : this.x;
2677 int y = this.mouseMove ? this.dropY : this.y;
2678 EditLine line = this.mouseMove ? this.dropLine : this.line;
2680 if(!(style.freeCaret))
2681 max = Min(max, line.count);
2683 if(FontExtent && display)
2685 for(c = 0; c < max; )
2694 for(len = 0; c < Min(max, line.count); c += numBytes)
2696 ch = line.buffer[c];
2697 numBytes = UTF8_NUM_BYTES(ch);
2699 if(ch == ' ' || ch == '\t')
2706 if(!len && ch == ' ')
2711 else if(!len && ch == '\t')
2713 w = (tabSize * space.w) - (x % (tabSize * space.w));
2717 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2729 caretY = y * this.space.h;
2730 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2735 NotifyCaretMove(master, this, y + 1, x + 1);
2741 void SelectionEnables()
2743 if((x != selX || y != selY) && !selection)
2747 itemEditCut.disabled = false;
2748 itemEditDelete.disabled = false;
2750 itemEditCopy.disabled = false;
2752 this.selection = true;
2754 else if((x == selX && y == selY) && selection)
2756 itemEditCut.disabled = true;
2757 itemEditCopy.disabled = true;
2758 itemEditDelete.disabled = true;
2760 this.selection = false;
2764 void SetSelectCursor()
2766 if(!inactive || !style.noSelect)
2769 cursor = guiApp.GetCursor(arrow);
2770 else if(this.mouseSelect)
2771 cursor = guiApp.GetCursor(iBeam);
2774 if(IsMouseOnSelection())
2775 cursor = guiApp.GetCursor(arrow);
2777 cursor = guiApp.GetCursor(iBeam);
2782 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2785 int x = px ? *px : 0;
2792 if(c < Min(max, line.count))
2796 for(len = 0; c < Min(max, line.count); c += numBytes)
2798 ch = line.buffer[c];
2799 numBytes = UTF8_NUM_BYTES(ch);
2801 if(ch == ' ' || ch == '\t')
2808 if(!len && ch == ' ')
2813 else if(!len && ch == '\t')
2815 w = (tabSize * space.w) - (x % (tabSize * space.w));
2819 FontExtent(display, font, line.buffer + start, len, &w, null);
2823 if(style.freeCaret && c < max)
2832 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2837 int a = start + len;
2840 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2844 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2847 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2851 return Min(this.maxLineSize - 1, start + len);
2857 void SetCursorToViewX()
2859 bool selecting = this.x != selX || y != selY;
2861 // Horizontal Adjustment
2863 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2868 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2880 UpdateCaretPosition(false);
2886 void SetCursorToViewY()
2889 EditLine oldLine = this.line;
2891 bool selecting = this.x != this.selX || this.y != this.selY;
2893 numLines = clientSize.h / this.space.h;
2895 // Vertical Adjustment
2896 if(this.viewY > this.y)
2898 this.y = this.viewY;
2899 this.line = this.viewLine;
2902 if(this.viewY + numLines <= this.y)
2906 this.y = this.viewY-1;
2907 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2914 if(this.line != oldLine)
2916 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2924 this.selLine = this.line;
2927 UpdateCaretPosition(false);
2934 bool SaveFile(char * fileName)
2936 File f = eFile_Open(fileName, FO_WRITE);
2940 eWindow_SetModified(false);
2941 eInstance_Delete(f);
2948 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2952 if(!style.multiLine)
2954 x = (active && this.active && !style.readOnly) ? line.count : 0;
2957 SetViewToCursor(true);
2962 if(!active && modified)
2965 if(!NotifyModified(master, this))
2968 *goOnWithActivation = false;
2975 if(timer) timer.Stop();
2977 mouseSelect = false;
2989 // todo: resize width based on largest on-screen-line extent...
2991 display.FontExtent(font, " ", 1, null, &sh);
2995 nh = lineCount * sh;
2996 size.h = nh < minClientSize.h ? minClientSize.h : nh;
3007 int len = line ? strlen(line.text) : 0;
3008 GetDecorationsSize(&dw, &dh);
3009 display.FontExtent(font, " ", 1, null, &sh);
3010 if(len) display.FontExtent(font, line.text, len, &tw, null);
3012 if(nw < minClientSize.w) nw = minClientSize.w;
3014 if(nh < minClientSize.h) nh = minClientSize.h;
3020 bool OnResizing(int *w, int *h)
3026 *w = space.h * 80 / 14;
3030 void OnResize(int w, int h)
3033 if(!hasHorzScroll && !hasVertScroll && viewLine)
3034 SetViewToCursor(true);
3036 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3038 SetViewToCursor(true);
3043 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3045 if(style.readOnly) return true;
3046 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3047 // Middle button already serves as a dragger as well.
3052 bool OnRightButtonDown(int x, int y, Modifiers mods)
3054 this.rightButtonDown = true;
3059 bool OnRightButtonUp(int x, int y, Modifiers mods)
3062 if(!parent.inactive && rightButtonDown)
3065 Menu contextMenu { };
3067 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3068 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3069 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3070 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3071 MenuDivider { contextMenu };
3072 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3074 popup = PopupMenu { master = this, menu = contextMenu,
3076 nonClient = true, interim = false, parent = parent,
3077 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3079 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3083 rightButtonDown = false;
3087 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3092 if(style.noSelect) return true;
3094 // Should we have a separate 'selectOnActivate' style?
3095 if(!mods.isActivate || (style.readOnly && style.multiLine))
3101 mouseX = mx - XOFFSET;
3104 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3106 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3112 else if(IsMouseOnSelection() && !mods.isActivate)
3122 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3124 if(mods.shift && !mods.isActivate)
3139 this.selLine = this.line;
3148 UpdateCaretPosition(true);
3152 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3156 mouseSelect = false;
3167 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3169 //PrintLn("MouseMove: ", x, ", ", y);
3175 mouseMove = IsMouseOnSelection();
3179 int size = SelSize();
3182 char * text = new char[size+1];
3186 GetSel(text, false);
3188 if(Max(selY, this.y) == dropY)
3192 if(this.dropX > this.selX)
3193 moveX = this.x - this.selX;
3197 if(this.dropX > this.x)
3198 moveX = this.selX - this.x;
3202 this.dropX -= moveX;
3203 this.selX = this.x = this.dropX;
3204 this.selY = this.y = this.dropY;
3205 this.selLine = this.line = this.dropLine;
3207 SetViewToCursor(true);
3212 byte * c = ((EditBox)this).multiLineContents, * c2;
3213 int l1 = c ? strlen(c) : 0, l2;
3216 c2 = ((EditBox)this).multiLineContents;
3217 l2 = c2 ? strlen(c2) : 0;
3218 if(l1 != l2 || (l1 && strcmp(c, c2)))
3248 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3250 //PrintLn("Dropped: ", x, ", ", y);
3252 NotifyDropped(master, this, x, y);
3259 bool OnMouseMove(int mx, int my, Modifiers mods)
3265 if(mods != -1 && mods.isSideEffect)
3270 if(style.noSelect) return true;
3271 if(wordSelect) return true;
3272 mouseX = mx - XOFFSET;
3275 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3277 if(this.mouseMove || this.mouseSelect)
3286 ((style.hScroll) || (style.vScroll)))
3293 DirtyLine(this.dropY);
3296 DirtyLine(this.dropY);
3297 this.dropLine = line;
3298 SetViewToCursor(true);
3300 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3303 else if(this.mouseSelect)
3305 DirtyLine(this.selY);
3312 SetViewToCursor(true);
3315 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3322 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3327 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3331 if(style.noSelect) return true;
3332 FindMouse(mx, my, &x, &y, &line, false);
3333 if(!NotifyDoubleClick(master, this, line, mods))
3340 for(c = x; c >= 0; c--)
3343 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3344 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3351 for(c = start; c<line.count; c += numBytes)
3353 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3364 this.line = this.selLine = line;
3365 this.wordSelect = (c != start);
3377 Menu fileMenu { menu, "File", F };
3378 saveDialog = fileDialog;
3379 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3380 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3390 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3391 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3393 space.w = Max(space.w, 1);
3394 large.w = Max(large.w, 1);
3395 space.h = Max(space.h, 1);
3396 large.h = Max(large.h, 1);
3400 for(line = lines.first; line; line = line.next)
3401 ComputeLength(line);
3406 SetViewToCursor(true);
3414 bool OnLoadGraphics()
3416 FontExtent = Display::FontExtent;
3419 // UpdateCaretPosition(true);
3423 void OnUnloadGraphics()
3428 bool OnKeyHit(Key key, unichar ch)
3430 bool shift = (key.shift) ? true : false;
3432 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3434 if(!ch && !key.alt && !key.ctrl)
3436 key.code = (SmartKey)key.code;
3439 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3442 if(style.readOnly) break;
3443 if(style.stuckCaret) GoToEnd(true);
3444 if(!(style.freeCaret))
3446 this.x = Min(this.x, this.line.count);
3452 EditLine line = this.line;
3454 for(y = this.y; y>= 0; y--)
3456 c = (y == this.y) ? (this.x-1) : line.count-1;
3458 // Slow down when going on lines...
3459 if(y != this.y) break;
3463 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3464 if(IS_ALUNDER(line.buffer[c]))
3469 if(!IS_ALUNDER(line.buffer[c]))
3471 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3485 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3486 this.x = this.selX = Min(c+1, line.count);
3487 this.y = this.selY = y;
3488 this.line = this.selLine = line;
3489 SetViewToCursor(true);
3500 if(style.readOnly) break;
3501 if(style.stuckCaret) break;
3510 if(this.line != this.selLine || this.x != this.selX)
3513 SetViewToCursor(true);
3519 if(this.x < this.line.count)
3523 for(i = this.x; i < this.line.count; i++)
3525 if(!IS_ALUNDER(this.line.buffer[i]))
3529 for(; i < this.line.count; i++)
3531 //Delete trailing whitespace
3532 if(IS_ALUNDER(this.line.buffer[i]))
3535 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3536 SetViewToCursor(true);
3539 else if(this.line.next)
3541 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3542 SetViewToCursor(true);
3548 if(!(style.freeCaret))
3550 this.selX = this.x = Min(this.x, this.line.count);
3553 if(this.x < this.line.count)
3555 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3557 else if(this.line.next)
3559 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3561 SetViewToCursor(true);
3570 if(!key.alt && !key.ctrl)
3574 bool stuffAfter = false;
3578 if(style.stuckCaret) GoToEnd(true);
3579 if(style.readOnly) break;
3580 if(!(style.multiLine)) break;
3582 for(c = 0; c<this.line.count && c<this.x; c++)
3584 if(this.line.buffer[c] == '\t')
3585 position += this.tabSize - (position % this.tabSize);
3586 else if(this.line.buffer[c] == ' ')
3594 if(this.x < this.line.count)
3597 //If last character is a { indent one tab
3598 if(this.line.buffer[this.x - 1] == '{')
3600 //Except if the next non space character is a }
3601 bool indent = false;
3603 for(i = this.x; i < this.line.size; i++)
3604 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3606 if(this.line.buffer[i] != '}')
3611 position += this.tabSize;
3614 addString = new char[position + 2];
3615 addString[len++] = '\n';
3616 addString[len] = '\0';
3618 if(stuffAfter || !style.freeCaret)
3620 for(c = 0; c<position; )
3622 if(style.useTab && c + this.tabSize <= position)
3624 addString[len++] = '\t';
3629 addString[len++] = ' ';
3633 addString[len] = '\0';
3637 if(!stuffAfter && style.freeCaret)
3639 this.x = this.selX = position;
3643 SetViewToCursor(true);
3653 if(style.stuckCaret) break;
3654 if(!(style.freeCaret))
3656 this.x = Min(this.x, this.line.count);
3657 this.selX = Min(this.selX, this.selLine.count);
3660 if(!shift) SelDirty();
3663 bool foundAlpha = false;
3666 EditLine line, lastLine;
3669 for(line = this.line; (line && !found); line = line.prev, y--)
3674 if(this.x == 0 && line != this.line)
3683 if(line == this.line) start = this.x -1; else start = line.count-1;
3684 start = Min(start, line.count-1);
3686 for(c = start; c >= 0;)
3689 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3707 byte ch = line.buffer[c];
3708 if(UTF8_IS_FIRST(ch)) break;
3712 // No next word found,
3713 if(!found && ( this.x > 0 || (!line.count && this.x)))
3727 this.line = lastLine;
3738 byte * buffer = line.buffer;
3741 byte ch = buffer[x];
3742 if(UTF8_IS_FIRST(ch)) break;
3759 if(!shift) Deselect();
3760 SetViewToCursor(true);
3766 if(style.stuckCaret) break;
3767 if(!(style.freeCaret))
3769 this.x = Min(this.x, this.line.count);
3770 this.selX = Min(this.selX, this.selLine.count);
3773 if(!shift) SelDirty();
3774 if(!shift && (this.x != this.selX || this.y != this.selY));
3777 bool onAChar = false;
3778 if(this.selX != this.x || this.selY != this.y)
3780 if(this.x<this.line.count)
3781 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3783 if(key.shift && onAChar &&
3784 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3786 bool foundAlpha = false;
3788 EditLine line, lastLine;
3790 int lastC, lastY, lastNumBytes;
3792 for(line = this.line; (line && !found); line = line.next, y++)
3794 int start = (line == this.line) ? this.x : 0;
3797 for(c = start; c < line.count; c += numBytes)
3799 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3804 lastNumBytes = numBytes;
3814 if(!found && (c != this.x || line != this.line))
3827 this.x = lastC + lastNumBytes;
3829 this.line = lastLine;
3836 bool foundAlpha = false;
3841 for(line = this.line; (line && !found); line = line.next, y++)
3843 int start = (line == this.line) ? this.x : 0;
3846 for(c = start; c < line.count; c += numBytes)
3848 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3863 // No next word found,
3864 if(!found && (c != this.x || line != this.line))
3868 this.x = line.count;
3880 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3884 byte * buffer = line.buffer;
3887 byte ch = buffer[x];
3888 if(UTF8_IS_FIRST(ch)) break;
3909 if(!shift) Deselect();
3910 SetViewToCursor(true);
3917 if(!style.vScroll || hasVertScroll) break;
3923 if(style.stuckCaret) break;
3925 if(!shift) SelDirty();
3935 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3939 if(!shift) Deselect();
3941 SetViewToCursor(false);
3944 if(caretY == this.y * space.h)
3950 if(!style.freeCaret)
3951 this.x = Min(this.x, line.count);
3961 int sx = 0, sy = this.y * space.h;
3962 char * text = line.text;
3963 int maxW = clientSize.w - sx;
3964 display.FontExtent(font, " ", 1, null, &th);
3968 int startPos = textPos;
3971 bool lineComplete = false;
3973 if(!style.wrap && caretY == MAXINT)
3976 //textPos = line.count;
3977 //lineComplete = true;
3980 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3984 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3987 len = (nextSpace - (text + textPos));
3989 len = line.count - textPos;
3991 if(textPos < line.count)
3993 display.FontExtent(font, text + textPos, len, &w, null);
3995 if(nextSpace) { w += space.w; len++; }
3997 if(style.wrap && x + width + w > maxW && x > 0)
3999 lineComplete = true;
4009 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4013 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4016 if(this.x > line.count)
4019 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4020 len = this.x - startPos;
4021 display.FontExtent(font, text + startPos, len, &x, null);
4025 if(!shift) Deselect();
4027 SetViewToCursor(false);
4031 if(sy == caretY - th || textPos >= line.count)
4033 if(textPos >= line.count)
4035 int c = textPos - 1;
4036 while(c > 0 && text[c] == ' ') c--;
4040 this.x = line.count;
4043 if(!shift) Deselect();
4045 SetViewToCursor(false);
4050 } while(textPos < line.count);
4053 if(!shift) Deselect();
4055 SetViewToCursor(false);
4064 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4065 if(!shift) SelDirty();
4066 this.line = this.line.prev;
4072 if(!shift) Deselect();
4076 SetViewToCursor(false);
4082 return style.multiLine ? false : true;
4086 if(!style.vScroll || hasVertScroll)
4093 if(style.stuckCaret) break;
4097 int sx = 0, sy = this.y * this.space.h;
4098 int maxW = clientSize.w - sx;
4099 char * text = line.buffer;
4101 if(!shift) SelDirty();
4107 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4117 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4120 if(!shift) Deselect();
4122 if(this.selX != this.x || this.selY != this.y)
4124 SetViewToCursor(false);
4127 while(!textPos || (style.freeCaret || textPos<line.count))
4129 int startPos = textPos;
4132 bool lineComplete = false;
4133 if(!style.wrap && sy <= caretY)
4135 textPos = line.count;
4136 lineComplete = true;
4138 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4142 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4145 len = (nextSpace - (text + textPos));
4147 len = line.count - textPos;
4149 if(textPos < line.count)
4151 display.FontExtent(font, text + textPos, len, &w, &th);
4153 if(nextSpace) { w += space.w; len++; }
4154 if(style.wrap && x + width + w > maxW && x > 0)
4156 lineComplete = true;
4166 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4170 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4173 if(this.x > line.count)
4176 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4178 len = this.x - startPos;
4179 display.FontExtent(font, text + startPos, len, &x, null);
4182 if(!shift) Deselect();
4185 SetViewToCursor(false);
4191 this.x = line.count;
4194 if(!shift) Deselect();
4197 SetViewToCursor(false);
4200 else if(textPos >= line.count && line.next)
4206 sy = this.y * this.space.h;
4207 sx = 0; //textBlock.startX;
4213 sx = 0; //textBlock.startX;
4221 this.x = Min(this.x, line.count);
4222 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4224 if(!shift) Deselect();
4227 if(this.selX != this.x || this.selY != this.y)
4230 SetViewToCursor(false);
4237 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4238 if(!shift) SelDirty();
4239 this.line = this.line.next;
4243 if(!shift) Deselect();
4246 if(this.selX != this.x || this.selY != this.y)
4254 return style.multiLine ? false : true;
4257 if(style.stuckCaret) break;
4258 if(!(style.freeCaret))
4259 this.selX = Min(this.selX, this.selLine.count);
4261 if(!shift) SelDirty();
4264 this.line = this.lines.first;
4265 if(this.y != 0 || this.x != 0)
4275 EditLine line = this.line;
4277 for(c=0; line.buffer[c]; c++)
4278 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4280 if(c != 0 || this.x)
4290 DirtyLine(this.y);*/
4295 if(!shift) Deselect();
4296 SetViewToCursor(true);
4302 if(style.stuckCaret) break;
4303 if(!(style.freeCaret))
4304 this.selX = Min(this.selX, this.selLine.count);
4306 if(!shift) SelDirty();
4311 else if(this.x != this.line.count)
4313 this.x = this.line.count;
4314 //DirtyLine(this.y);
4317 if(!shift) Deselect();
4318 SetViewToCursor(true);
4323 if(style.tabKey && !key.ctrl)
4325 if(this.selY != this.y && style.tabSel)
4327 EditLine firstLine, lastLine;
4331 // Do multi line selection tabbing here
4332 if(this.selY < this.y)
4334 firstLine = this.selLine;
4335 lastLine = this.line;
4341 // Selecting going up
4342 firstLine = this.line;
4343 lastLine = this.selLine;
4350 for(line = firstLine; line; line = line.next, y++)
4352 if(line != lastLine || x)
4356 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4358 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4360 if(line.buffer[c] == '\t')
4365 else if(line.buffer[c] != ' ')
4370 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4373 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4374 char * string = new char[len];
4375 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4376 GetText(string, line, y, 0, line, y, lastC, false, false);
4379 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4380 if(!line.AdjustBuffer(line.count-lastC))
4383 if(style.autoSize) AutoSize();
4387 if(line == lastLine) break;
4392 for(line = firstLine; line; line = line.next, y++)
4396 if(line != lastLine || x)
4400 if(line.count + 1 <= this.maxLineSize)
4402 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4404 if(!line.AdjustBuffer(line.count+1))
4407 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4408 if(style.autoSize) AutoSize();
4410 AddCharAction action { ch = '\t', x = 0, y = y };
4414 memmove(line.buffer+1,line.buffer,line.size-1);
4416 line.buffer[0] = '\t';
4421 if(line.count + this.tabSize <= this.maxLineSize)
4424 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4425 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4426 if(style.autoSize) AutoSize();
4428 if(!line.AdjustBuffer(line.count+this.tabSize))
4432 char * string = new char[this.tabSize + 1];
4433 memset(string, ' ', this.tabSize);
4434 string[this.tabSize] = '\0';
4435 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4438 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4439 line.count+=this.tabSize;
4440 for(c=0; c<this.tabSize; c++)
4441 line.buffer[c] = ' ';
4447 if(line == lastLine) break;
4461 char * addString = new char[this.tabSize + 1];
4463 if(!(style.freeCaret))
4465 this.x = Min(this.x, this.line.count);
4470 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4472 addString[len++] = ' ';
4480 SetViewToCursor(true);
4487 if(!(style.hScroll) || hasHorzScroll) break;
4488 if(this.viewX < this.maxLength)
4490 //this.viewX+=this.space.w*this.tabSize;
4492 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4499 if(!shift) Deselect();
4508 if(!(style.hScroll) || hasHorzScroll) break;
4511 //this.viewX-=this.space.w*this.tabSize;
4512 //this.viewX = Max(this.viewX,0);
4514 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4515 // SetCursorToView();
4522 if(!shift) Deselect();
4536 if(!(style.readOnly))
4542 this.overwrite ^= 1;
4543 UpdateCaretPosition(true);
4548 NotifyOvrToggle(master, this, this.overwrite);
4559 if(style.noSelect) break;
4562 //Save current view position
4563 int tempX = this.viewX;
4564 int tempY = this.viewY;
4568 this.selLine = this.lines.first;
4569 this.y = this.lineCount-1;
4570 this.line = this.lines.last;
4571 this.x = this.line.count;
4574 SetViewToCursor(true);
4576 //Restore previous view position
4577 SetScrollPosition(tempX, tempY * this.space.h);
4583 // TOCHECK: Was there any good reason why we weren't returning false here?
4584 return false; // break;
4589 if(!(style.readOnly))
4597 if(style.readOnly) break;
4603 if(style.readOnly) break;
4608 if(style.readOnly) break;
4612 if(style.readOnly) break;
4616 if(style.readOnly) break;
4617 if(key.shift && key.code == rightBracket)
4619 //Only indent back if you are exactly at one tab.
4621 bool whitespace = true;
4627 EditLine line = this.line;
4629 //Only remove one tab if there is nothing else on the line.
4630 for(i = 0; i < this.line.count; i++)
4632 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4646 for(pos = line.count - 1; pos >= 0; pos--)
4648 char c = line.buffer[pos];
4652 indentwidth += this.tabSize;
4654 //Counting backwards, so when you find a character, indentation is reset
4664 //Place the } to get an undo:
4667 this.x = this.line.count;
4671 putsize = indentwidth;
4673 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4675 newline = new char[putsize+2];
4676 newline[putsize] = '}';
4677 newline[putsize+1] = '\0';
4681 for(; i < indentwidth / this.tabSize; i++)
4683 for(;i < putsize; i++)
4691 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4703 void OnHScroll(ScrollBarAction action, int position, Key key)
4706 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4708 this.viewX = position;
4709 if(action != setRange)
4711 if(!this.mouseMove && style.cursorFollowsView)
4718 void OnVScroll(ScrollBarAction action, int position, Key key)
4720 int oldViewY = this.viewY;
4723 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4725 position /= this.space.h;
4727 if(position < this.viewY)
4729 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4730 style.recomputeSyntax = true;
4732 else if(position > this.viewY)
4734 EditLine oldViewLine = viewLine;
4735 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4736 FigureStartSyntaxStates(oldViewLine, false);
4739 if(action != setRange)
4741 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4744 if(this.x != this.selX || this.y != this.selY)
4748 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4751 int numLines = clientSize.h / this.space.h;
4753 if(Abs(this.viewY - oldViewY) < numLines)
4756 if(this.viewY > oldViewY)
4758 for(y = oldViewY; y <this.viewY; y++)
4759 DirtyLine(y + numLines);
4763 for(y = this.viewY; y <oldViewY; y++)
4764 DirtyLine(y + numLines);
4769 // Fix dirt of stuff before first line...
4770 if(this.viewY - oldViewY > 0)
4772 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4779 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4784 ReplaceTextAction replaceAction = null;
4785 AddCharAction addCharAction = null;
4786 int addedSpaces = 0, addedTabs = 0;
4788 if(ch == '\r') return true;
4789 if(style.stuckCaret /*|EES_READONLY)*/ )
4792 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4794 if(!undoBuffer.dontRecord)
4796 if(selX != x || selY != y)
4801 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4802 oldString = new char[len];
4803 UTF32toUTF8Len(&ch, 1, buffer, 4);
4804 newString = CopyString(buffer);
4805 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4807 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4808 if(selY < y || (selY == y && selX < x))
4810 replaceAction.x1 = selX;
4811 replaceAction.y1 = selY;
4812 replaceAction.x2 = x;
4813 replaceAction.y2 = y;
4817 replaceAction.x1 = x;
4818 replaceAction.y1 = y;
4819 replaceAction.x2 = selX;
4820 replaceAction.y2 = selY;
4822 Record(replaceAction);
4823 undoBuffer.dontRecord++;
4827 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4828 Record(addCharAction);
4834 DelSel(&addedSpaces);
4835 if(this.lineCount+1 > this.maxLines)
4838 Emptyline(this.lines.first,0);
4842 if(!(style.vScroll))
4844 // Make sure it fits, but we need a default line is this.font is too big for window
4845 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4848 if((this.y >= 0) && this.y < this.viewY)
4853 line = EditLine { };
4856 lines.Insert(this.line, line);
4857 line.editBox = this;
4863 // If we're displacing the lines from a current line ...
4866 if(this.line.buffer)
4869 if(this.line.count < endX) endX = this.line.count;
4870 length = this.line.count - endX;
4873 if(!line.AdjustBuffer(length))
4877 if(this.line.buffer)
4879 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4881 if(endX > 4000 || endX < 0)
4884 this.line.count = endX;
4885 this.line.buffer[this.line.count] = '\0';
4886 this.line.AdjustBuffer(this.line.count);
4887 ComputeLength(this.line);
4891 BufferLocation before = { this.line, this.y, this.x }, after;
4897 ComputeLength(this.line);
4900 if(length > 4000 || length < 0)
4903 line.count = length;
4907 line.buffer[line.count] = '\0';
4910 after.line = this.line, after.y = this.y, after.x = this.x;
4912 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4913 if(style.autoSize) AutoSize();
4919 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4920 DelSel(&addedSpaces);
4921 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4922 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4923 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4927 this.selLine = this.line;
4931 replaceAction.x3 = x;
4932 replaceAction.y3 = y;
4933 replaceAction.addedSpaces = addedSpaces;
4934 replaceAction.addedTabs = addedTabs;
4935 undoBuffer.dontRecord--;
4939 addCharAction.addedSpaces = addedSpaces;
4940 addCharAction.addedTabs = addedTabs;
4947 /****************************************************************************
4949 ****************************************************************************/
4952 bool AddCh(unichar ch)
4954 return _AddCh(ch, null, null);
4959 this.modified = true;
4960 NotifyUpdate(master, this);
4961 modifiedDocument = true;
4964 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4967 DelCh(line1, y1, x1, line2, y2, x2, false);
4968 SetViewToCursor(true);
4976 itemEditUndo.disabled = undoBuffer.curAction == 0;
4977 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4978 if(savedAction == undoBuffer.curAction)
4980 modifiedDocument = false;
4982 NotifyUnsetModified(master, this);
4989 itemEditUndo.disabled = undoBuffer.curAction == 0;
4990 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4991 if(savedAction == undoBuffer.curAction)
4993 modifiedDocument = false;
4995 NotifyUnsetModified(master, this);
4999 void Record(UndoAction action)
5001 if(!undoBuffer.dontRecord)
5003 undoBuffer.Record(action);
5004 itemEditUndo.disabled = undoBuffer.curAction == 0;
5005 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5012 this.lines.first, 0,0,
5013 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5016 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5021 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5022 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5023 this.y = line2 ? y2 : (this.lineCount-1);
5024 this.line = line2 ? (EditLine)line2 : this.lines.last;
5027 SetViewToCursor(true);
5031 // TODO: Fix this vs modifiedDocument window property
5032 void SetModified(bool flag)
5036 this.modified = false;
5037 if(flag && !NotifyModified(master, this))
5038 this.modified = true;
5043 bool AddS(char * string)
5050 int addedSpaces = 0, addedTabs = 0;
5051 AddTextAction action = null;
5052 ReplaceTextAction replaceAction = null;
5054 this.pasteOperation = true;
5056 if(style.stuckCaret /*|EES_READONLY)*/ )
5059 if(!undoBuffer.dontRecord)
5061 char * placeString = CopyString(string);
5062 if(!style.multiLine)
5066 for(i = 0; (ch = placeString[i]); i++)
5069 placeString[i] = '\0';
5070 placeString = renew placeString byte[i+1];
5075 if(selX != x || selY != y)
5079 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5080 oldString = new char[len];
5081 newString = placeString;
5082 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5084 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5085 if(selY < y || (selY == y && selX < x))
5087 replaceAction.x1 = selX;
5088 replaceAction.y1 = selY;
5089 replaceAction.x2 = x;
5090 replaceAction.y2 = y;
5094 replaceAction.x1 = x;
5095 replaceAction.y1 = y;
5096 replaceAction.x2 = selX;
5097 replaceAction.y2 = selY;
5099 Record(replaceAction);
5103 if(string[0] == '\n')
5104 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5106 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5114 undoBuffer.dontRecord++;
5115 DelSel(&addedSpaces);
5119 for(c = 0; string[c]; c++)
5121 if(string[c] == '\n' || string[c] == '\r')
5123 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5128 if(string[c] == '\n')
5137 // Reset for next line
5141 if(string[c] == '\r' && *line == '\n')
5153 // Why was this here?
5156 // Add the line here
5158 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5164 undoBuffer.dontRecord--;
5169 action.addedSpaces = addedSpaces;
5170 action.addedTabs = addedTabs;
5172 else if(replaceAction)
5174 replaceAction.y3 = y;
5175 replaceAction.x3 = x;
5176 replaceAction.addedSpaces = addedSpaces;
5177 replaceAction.addedTabs = addedTabs;
5180 UpdateCaretPosition(true);
5182 this.pasteOperation = false;
5195 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5196 SetViewToCursor(true);
5202 void PutCh(unichar ch)
5206 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5207 //if((ch >= 32) || ch == '\n')
5209 int addedSpaces = 0, addedTabs = 0;
5210 ReplaceTextAction replaceAction = null;
5211 AddCharAction addCharAction = null;
5214 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5216 if(this.x < this.line.count && this.overwrite)
5221 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5222 oldString = new char[len];
5223 UTF32toUTF8Len(&ch, 1, buffer, 4);
5224 newString = CopyString(buffer);
5225 GetText(oldString, line, y, x, line, y, x+1, false, false);
5226 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5227 replaceAction.x1 = x;
5228 replaceAction.y1 = y;
5229 replaceAction.x2 = x+1;
5230 replaceAction.y2 = y;
5231 Record(replaceAction);
5233 undoBuffer.dontRecord++;
5234 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5235 undoBuffer.dontRecord--;
5237 else if(!undoBuffer.dontRecord)
5239 if(selX != x || selY != y)
5244 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5245 oldString = new char[len];
5246 UTF32toUTF8Len(&ch, 1, buffer, 4);
5247 newString = CopyString(buffer);
5248 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5249 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5250 if(selY < y || (selY == y && selX < x))
5252 replaceAction.x1 = selX;
5253 replaceAction.y1 = selY;
5254 replaceAction.x2 = x;
5255 replaceAction.y2 = y;
5259 replaceAction.x1 = x;
5260 replaceAction.y1 = y;
5261 replaceAction.x2 = selX;
5262 replaceAction.y2 = selY;
5264 Record(replaceAction);
5268 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5269 Record(addCharAction);
5272 undoBuffer.dontRecord++;
5273 result = _AddCh(ch, &addedSpaces, &addedTabs);
5276 replaceAction.x3 = x;
5277 replaceAction.y3 = y;
5278 replaceAction.addedSpaces = addedSpaces;
5279 replaceAction.addedTabs = addedTabs;
5283 addCharAction.addedSpaces = addedSpaces;
5284 addCharAction.addedTabs = addedTabs;
5286 undoBuffer.dontRecord--;
5290 if(result) SetViewToCursor(true);
5294 void PutS(char * string)
5299 SetViewToCursor(true);
5304 void Printf(char * format, ...)
5308 char temp[MAX_F_STRING];
5310 va_start(args, format);
5311 vsprintf(temp, format, args);
5317 void SetContents(char * format, ...)
5322 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5325 char temp[MAX_F_STRING];
5327 va_start(args, format);
5328 vsprintf(temp, format, args);
5344 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5347 else if(this.line.prev)
5349 EditLine line = this.line.prev;
5353 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5361 this.selLine = this.line;
5366 SetViewToCursor(true);
5371 Emptyline(this.line,this.y);
5372 this.selX = this.x = 0;
5375 this.selLine = this.line;
5377 SetViewToCursor(true);
5387 SetViewToCursor(true);
5395 SetViewToCursor(true);
5399 bool GoToLineNum(int lineNum)
5404 EditLine line = this.lines.first;
5405 for(c = 0; c < lineNum && line; c++, line = line.next);
5415 SetViewToCursor(true);
5422 bool GoToPosition(EditLine line, int y, int x)
5434 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5448 SetViewToCursor(true);
5455 void SetViewToCursor(bool setCaret)
5465 bool dontScroll = false;
5471 selected = selX != this.x || selY != y;
5478 checkLine = dropLine;
5484 checkLine = this.line;
5489 numLines = clientSize.h / space.h;
5491 // This is broken. The EditBox now doesn't do anything different when adding to it,
5492 // regardless of the previous scrolling position. It should be read and then set again
5493 // if one wishes to preserve it.
5494 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5495 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5499 // Horizontal Adjustment
5500 if(!dontScroll && checkLine)
5504 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5507 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5513 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5514 viewX = x - clientSize.w+space.w;
5515 if(x < this.viewX + clientSize.w/2 - space.w)
5516 viewX = Max(0, x - clientSize.w/2 + space.w);
5524 // Vertical Adjustment
5525 if(viewY > checkY) viewY = checkY;
5526 if(viewY + numLines <= checkY)
5528 if(clientSize.h >= space.h)
5530 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5540 for(;dropLine && dropLine.prev && dropY >= numLines;)
5542 dropLine = dropLine.prev;
5546 for(;this.line && this.line.prev && this.y >= numLines;)
5548 this.line = this.line.prev;
5553 SetScrollPosition(viewX, viewY * this.space.h);
5555 UpdateCaretPosition(setCaret);
5561 selLine = this.line;
5571 void CenterOnCursor()
5573 int numLines = clientSize.h / this.space.h;
5574 int y = this.y - numLines / 2;
5576 bool figureSyntax = false;
5577 EditLine oldViewLine = viewLine;
5578 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5583 for(;y < this.viewY; y++)
5585 this.viewLine = this.viewLine.prev;
5586 style.recomputeSyntax = true;
5588 for(;y > this.viewY; y--)
5590 this.viewLine = this.viewLine.next;
5591 figureSyntax = true;
5594 FigureStartSyntaxStates(oldViewLine, false);
5598 SetScrollPosition(this.viewX, viewY * this.space.h);
5599 UpdateCaretPosition(true);
5603 void SetCursorToView()
5614 numLines = clientSize.h / this.space.h;
5618 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5619 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5623 EditLine oldLine = this.line;
5624 bool lastOne = false;
5625 EditLine oldViewLine = this.viewLine;
5626 bool figureSyntax = false;
5628 if(this.y >= this.lineCount-1) return;
5630 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5632 if(this.viewY + numLines < this.lines.count)
5634 this.viewLine = this.viewLine.next;
5636 figureSyntax = true;
5638 else if(c && !lastOne)
5647 FigureStartSyntaxStates(oldViewLine, false);
5648 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5651 SetViewToCursor(false);
5660 if(this.y == 0) return;
5662 numLines = clientSize.h / this.space.h;
5666 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5667 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5671 EditLine oldLine = this.line;
5673 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5678 if(this.viewLine.prev)
5680 this.viewLine = this.viewLine.prev;
5682 style.recomputeSyntax = true;
5686 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5689 SetViewToCursor(false);
5696 if(this.viewLine.prev)
5699 // this.viewLine = this.viewLine.prev;
5702 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5709 if(this.viewLine.next)
5712 // this.viewLine = this.viewLine.next;
5715 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5722 EditLine l1, l2, line;
5723 int x1, x2, nx1, nx2;
5728 if(!this.selLine) return 0;
5729 if(this.selLine == this.line && this.selX == this.x) return 0;
5730 if(this.selY < this.y)
5737 else if(this.selY > this.y)
5744 else if(this.selX < this.x)
5746 l1 = l2 = this.line;
5752 l1 = l2 = this.line;
5756 nx1 = Min(x1,l1.count);
5757 nx2 = Min(x2,l2.count);
5759 // Find Number of Bytes Needed
5761 for(line = l1; line; line = line.next)
5763 if(line == l1) start = nx1; else start = 0;
5764 if(line == l2) end = nx2; else end = line.count;
5766 if(style.freeCaret && line == l2)
5769 count = Max(x2-Max(x1,l1.count),0);
5771 count = Max(x2-l2.count,0);
5775 if(line == l2) break;
5776 // Add Carriage Return / line Feed
5783 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5787 if(!reorder || this.selY < this.y)
5789 if(l1) *l1 = this.selLine;
5790 if(y1) *y1 = this.selY;
5791 if(x1) *x1 = this.selX;
5792 if(l2) *l2 = this.line;
5793 if(y2) *y2 = this.y;
5794 if(x2) *x2 = this.x;
5796 else if(this.selY > this.y)
5798 if(l1) *l1 = this.line;
5799 if(y1) *y1 = this.y;
5800 if(x1) *x1 = this.x;
5801 if(l2) *l2 = this.selLine;
5802 if(y2) *y2 = this.selY;
5803 if(x2) *x2 = this.selX;
5805 else if(this.selX < this.x)
5807 if(l1) *l1 = this.line;
5808 if(y1) *y1 = this.selY;
5809 if(x1) *x1 = this.selX;
5810 if(l2) *l2 = this.line;
5811 if(y2) *y2 = this.y;
5812 if(x2) *x2 = this.x;
5816 if(l1) *l1 = this.line;
5817 if(y1) *y1 = this.y;
5818 if(x1) *x1 = this.x;
5819 if(l2) *l2 = this.line;
5820 if(y2) *y2 = this.selY;
5821 if(x2) *x2 = this.selX;
5826 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5828 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5830 this.selLine = (EditLine)l1;
5833 this.line = (EditLine)l2;
5837 SetViewToCursor(true);
5841 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5843 EditLine l1, l2, line;
5844 int x1, x2, nx1, nx2;
5875 nx1 = Min(x1,l1.count);
5876 nx2 = Min(x2,l2.count);
5879 for(line = l1; line; line = line.next)
5881 if(line == l1) start = nx1; else start = 0;
5882 if(line == l2) end = nx2; else end = line.count;
5885 CopyBytes(text, line.buffer + start, end - start);
5888 numChars += end-start;
5890 if(style.freeCaret && line == l2 && addSpaces)
5893 count = Max(x2-Max(x1,l1.count),0);
5895 count = Max(x2-l2.count,0);
5898 FillBytes(text,' ',count);
5904 if(line == l2) break;
5916 // '\0' terminate Terminate
5923 void GetSel(char * text, bool addCr)
5925 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5931 this.selLine = this.line;
5941 int size = SelSize();
5944 // Try to allocate memory
5945 ClipBoard clipBoard { };
5946 if(clipBoard.Allocate(size+1))
5948 GetSel(clipBoard.memory, true);
5961 ClipBoard clipBoard { };
5962 if(clipBoard.Load())
5963 PutS(clipBoard.memory);
5974 SetViewToCursor(true);
5979 void DeleteSelection()
5984 SetViewToCursor(true);
5990 void Save(File f, bool cr)
5993 savedAction = undoBuffer.curAction;
5995 for(line = this.lines.first; line; line = line.next)
5997 f.Write(line.buffer, line.count,1);
6000 if(cr) f.Putc('\r');
6006 #define BUFFER_SIZE 16384
6009 undoBuffer.dontRecord++;
6012 char buffer[BUFFER_SIZE];
6016 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6017 buffer[count] = '\0';
6023 undoBuffer.dontRecord--;
6024 undoBuffer.count = 0;
6025 undoBuffer.curAction = 0;
6026 itemEditUndo.disabled = undoBuffer.curAction == 0;
6027 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6030 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6034 bool firstPass = true;
6035 EditBoxFindResult result = found;
6037 if(!this.line) return notFound;
6040 for(line = this.line;;)
6048 line = this.lines.first;
6054 line = this.lines.last;
6055 num = this.lineCount - 1;
6061 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6063 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6067 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6070 if(line == this.line && !firstPass) break;
6088 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6092 int searchLen = strlen(text);
6093 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6095 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6096 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6098 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6105 bool OnKeyDown(Key key, unichar ch)
6108 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6110 if(!NotifyKeyDown(master, this, key, ch))
6120 this.mouseMove = false;
6121 OnLeftButtonUp(0,0,0);
6122 SetViewToCursor(true);
6132 public class EditBoxStream : File
6135 BufferLocation start, sel;
6142 EditBox editBox = this.editBox;
6144 editBox.x = start.x;
6145 editBox.y = start.y;
6146 editBox.line = start.line;
6148 editBox.selX = sel.x;
6149 editBox.selY = sel.y;
6150 editBox.selLine = sel.line;
6152 editBox.SetViewToCursor(true);
6153 //editBox.ComputeColumn();
6157 property EditBox editBox
6165 start.line = value.line;
6169 sel.line = value.selLine;
6172 value.GoToHome(true);
6175 get { return editBox; }
6178 uint Read(byte * buffer, uint size, uint count)
6181 EditBox editBox = this.editBox;
6182 EditLine line = editBox.line;
6188 for(;read < count && line; line = (*&line.next))
6190 int numBytes = Min(count - read, (*&line.count) - x);
6193 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6198 for(;read < count && x < (*&line.count);)
6200 buffer[read++] = (*&line.buffer)[x++];
6207 buffer[read++] = '\n';
6212 if(x == (*&line.count) && (*&line.next))
6221 editBox.line = editBox.selLine = line;
6222 editBox.x = editBox.selX = x;
6223 editBox.y = editBox.selY = y;
6228 bool Seek(int pos, FileSeekMode mode)
6231 EditBox editBox = this.editBox;
6232 EditLine line = editBox.line;
6234 if(mode == FileSeekMode::start)
6236 pos = pos - this.pos;
6246 for(;read < pos && line; line = (*&line.next))
6248 int numBytes = Min(pos - read, (*&line.count) - x);
6251 read += numBytes; x += numBytes;
6254 /*for(;read < pos && x < (*&line.count);)
6282 for(;read < pos && line; line = (*&line.prev))
6284 int numBytes = Min(pos - read, x);
6290 /*for(;read < pos && x > 0;)
6300 x = (*&(*&line.prev).count);
6316 editBox.line = editBox.selLine = line;
6317 editBox.x = editBox.selX = x;
6318 editBox.y = editBox.selY = y;
6323 bool Puts(char * string)
6325 EditBox editBox = this.editBox;
6326 BufferLocation start { editBox.line, editBox.y, editBox.x };
6330 editBox.AddS(string);
6332 pos.line = editBox.line;
6336 this.start.AdjustAdd(start, pos);
6337 sel.AdjustAdd(start, pos);
6342 // NOTE: BYTE, NOT UNICODE CHARACTER!
6345 EditBox editBox = this.editBox;
6346 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6351 utf8Bytes[numBytes++] = ch;
6352 utf8Bytes[numBytes] = 0;
6353 if(UTF8Validate(utf8Bytes))
6355 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6357 pos.line = editBox.line;
6360 this.start.AdjustAdd(start, pos);
6361 sel.AdjustAdd(start, pos);
6368 bool Getc(char * ch)
6370 return Read(ch, 1, 1) ? true : false;
6373 void DeleteBytes(uint count)
6375 EditBox editBox = this.editBox;
6378 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6379 BufferLocation end = pos;
6384 for(;c < count && end.line && end.x < end.line.count;)
6389 if(c < count && end.line && end.line.next)
6391 end.line = end.line.next;
6400 start.AdjustDelete(pos, end);
6401 sel.AdjustDelete(pos, end);
6403 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);