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 + 2;
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);
3376 Menu fileMenu { menu, "File", F };
3377 saveDialog = fileDialog;
3378 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3379 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3381 if(style.autoSize) AutoSize();
3389 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3390 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3392 space.w = Max(space.w, 1);
3393 large.w = Max(large.w, 1);
3394 space.h = Max(space.h, 1);
3395 large.h = Max(large.h, 1);
3399 for(line = lines.first; line; line = line.next)
3400 ComputeLength(line);
3405 SetViewToCursor(true);
3413 bool OnLoadGraphics()
3415 FontExtent = Display::FontExtent;
3418 // UpdateCaretPosition(true);
3422 void OnUnloadGraphics()
3427 bool OnKeyHit(Key key, unichar ch)
3429 bool shift = (key.shift) ? true : false;
3431 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3433 if(!ch && !key.alt && !key.ctrl)
3435 key.code = (SmartKey)key.code;
3438 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3441 if(style.readOnly) break;
3442 if(style.stuckCaret) GoToEnd(true);
3443 if(!(style.freeCaret))
3445 this.x = Min(this.x, this.line.count);
3451 EditLine line = this.line;
3453 for(y = this.y; y>= 0; y--)
3455 c = (y == this.y) ? (this.x-1) : line.count-1;
3457 // Slow down when going on lines...
3458 if(y != this.y) break;
3462 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3463 if(IS_ALUNDER(line.buffer[c]))
3468 if(!IS_ALUNDER(line.buffer[c]))
3470 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3484 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3485 this.x = this.selX = Min(c+1, line.count);
3486 this.y = this.selY = y;
3487 this.line = this.selLine = line;
3488 SetViewToCursor(true);
3499 if(style.readOnly) break;
3500 if(style.stuckCaret) break;
3509 if(this.line != this.selLine || this.x != this.selX)
3512 SetViewToCursor(true);
3518 if(this.x < this.line.count)
3522 for(i = this.x; i < this.line.count; i++)
3524 if(!IS_ALUNDER(this.line.buffer[i]))
3528 for(; i < this.line.count; i++)
3530 //Delete trailing whitespace
3531 if(IS_ALUNDER(this.line.buffer[i]))
3534 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3535 SetViewToCursor(true);
3538 else if(this.line.next)
3540 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3541 SetViewToCursor(true);
3547 if(!(style.freeCaret))
3549 this.selX = this.x = Min(this.x, this.line.count);
3552 if(this.x < this.line.count)
3554 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3556 else if(this.line.next)
3558 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3560 SetViewToCursor(true);
3569 if(!key.alt && !key.ctrl)
3573 bool stuffAfter = false;
3577 if(style.stuckCaret) GoToEnd(true);
3578 if(style.readOnly) break;
3579 if(!(style.multiLine)) break;
3581 for(c = 0; c<this.line.count && c<this.x; c++)
3583 if(this.line.buffer[c] == '\t')
3584 position += this.tabSize - (position % this.tabSize);
3585 else if(this.line.buffer[c] == ' ')
3593 if(this.x < this.line.count)
3596 //If last character is a { indent one tab
3597 if(this.line.buffer[this.x - 1] == '{')
3599 //Except if the next non space character is a }
3600 bool indent = false;
3602 for(i = this.x; i < this.line.size; i++)
3603 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3605 if(this.line.buffer[i] != '}')
3610 position += this.tabSize;
3613 addString = new char[position + 2];
3614 addString[len++] = '\n';
3615 addString[len] = '\0';
3617 if(stuffAfter || !style.freeCaret)
3619 for(c = 0; c<position; )
3621 if(style.useTab && c + this.tabSize <= position)
3623 addString[len++] = '\t';
3628 addString[len++] = ' ';
3632 addString[len] = '\0';
3636 if(!stuffAfter && style.freeCaret)
3638 this.x = this.selX = position;
3642 SetViewToCursor(true);
3652 if(style.stuckCaret) break;
3653 if(!(style.freeCaret))
3655 this.x = Min(this.x, this.line.count);
3656 this.selX = Min(this.selX, this.selLine.count);
3659 if(!shift) SelDirty();
3662 bool foundAlpha = false;
3665 EditLine line, lastLine;
3668 for(line = this.line; (line && !found); line = line.prev, y--)
3673 if(this.x == 0 && line != this.line)
3682 if(line == this.line) start = this.x -1; else start = line.count-1;
3683 start = Min(start, line.count-1);
3685 for(c = start; c >= 0;)
3688 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3706 byte ch = line.buffer[c];
3707 if(UTF8_IS_FIRST(ch)) break;
3711 // No next word found,
3712 if(!found && ( this.x > 0 || (!line.count && this.x)))
3726 this.line = lastLine;
3737 byte * buffer = line.buffer;
3740 byte ch = buffer[x];
3741 if(UTF8_IS_FIRST(ch)) break;
3758 if(!shift) Deselect();
3759 SetViewToCursor(true);
3765 if(style.stuckCaret) break;
3766 if(!(style.freeCaret))
3768 this.x = Min(this.x, this.line.count);
3769 this.selX = Min(this.selX, this.selLine.count);
3772 if(!shift) SelDirty();
3773 if(!shift && (this.x != this.selX || this.y != this.selY));
3776 bool onAChar = false;
3777 if(this.selX != this.x || this.selY != this.y)
3779 if(this.x<this.line.count)
3780 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3782 if(key.shift && onAChar &&
3783 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3785 bool foundAlpha = false;
3787 EditLine line, lastLine;
3789 int lastC, lastY, lastNumBytes;
3791 for(line = this.line; (line && !found); line = line.next, y++)
3793 int start = (line == this.line) ? this.x : 0;
3796 for(c = start; c < line.count; c += numBytes)
3798 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3803 lastNumBytes = numBytes;
3813 if(!found && (c != this.x || line != this.line))
3826 this.x = lastC + lastNumBytes;
3828 this.line = lastLine;
3835 bool foundAlpha = false;
3840 for(line = this.line; (line && !found); line = line.next, y++)
3842 int start = (line == this.line) ? this.x : 0;
3845 for(c = start; c < line.count; c += numBytes)
3847 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3862 // No next word found,
3863 if(!found && (c != this.x || line != this.line))
3867 this.x = line.count;
3879 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3883 byte * buffer = line.buffer;
3886 byte ch = buffer[x];
3887 if(UTF8_IS_FIRST(ch)) break;
3908 if(!shift) Deselect();
3909 SetViewToCursor(true);
3916 if(!style.vScroll || hasVertScroll) break;
3922 if(style.stuckCaret) break;
3924 if(!shift) SelDirty();
3934 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3938 if(!shift) Deselect();
3940 SetViewToCursor(false);
3943 if(caretY == this.y * space.h)
3949 if(!style.freeCaret)
3950 this.x = Min(this.x, line.count);
3960 int sx = 0, sy = this.y * space.h;
3961 char * text = line.text;
3962 int maxW = clientSize.w - sx;
3963 display.FontExtent(font, " ", 1, null, &th);
3967 int startPos = textPos;
3970 bool lineComplete = false;
3972 if(!style.wrap && caretY == MAXINT)
3975 //textPos = line.count;
3976 //lineComplete = true;
3979 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3983 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3986 len = (nextSpace - (text + textPos));
3988 len = line.count - textPos;
3990 if(textPos < line.count)
3992 display.FontExtent(font, text + textPos, len, &w, null);
3994 if(nextSpace) { w += space.w; len++; }
3996 if(style.wrap && x + width + w > maxW && x > 0)
3998 lineComplete = true;
4008 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4012 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4015 if(this.x > line.count)
4018 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4019 len = this.x - startPos;
4020 display.FontExtent(font, text + startPos, len, &x, null);
4024 if(!shift) Deselect();
4026 SetViewToCursor(false);
4030 if(sy == caretY - th || textPos >= line.count)
4032 if(textPos >= line.count)
4034 int c = textPos - 1;
4035 while(c > 0 && text[c] == ' ') c--;
4039 this.x = line.count;
4042 if(!shift) Deselect();
4044 SetViewToCursor(false);
4049 } while(textPos < line.count);
4052 if(!shift) Deselect();
4054 SetViewToCursor(false);
4063 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4064 if(!shift) SelDirty();
4065 this.line = this.line.prev;
4071 if(!shift) Deselect();
4075 SetViewToCursor(false);
4081 return style.multiLine ? false : true;
4085 if(!style.vScroll || hasVertScroll)
4092 if(style.stuckCaret) break;
4096 int sx = 0, sy = this.y * this.space.h;
4097 int maxW = clientSize.w - sx;
4098 char * text = line.buffer;
4100 if(!shift) SelDirty();
4106 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4116 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4119 if(!shift) Deselect();
4121 if(this.selX != this.x || this.selY != this.y)
4123 SetViewToCursor(false);
4126 while(!textPos || (style.freeCaret || textPos<line.count))
4128 int startPos = textPos;
4131 bool lineComplete = false;
4132 if(!style.wrap && sy <= caretY)
4134 textPos = line.count;
4135 lineComplete = true;
4137 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4141 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4144 len = (nextSpace - (text + textPos));
4146 len = line.count - textPos;
4148 if(textPos < line.count)
4150 display.FontExtent(font, text + textPos, len, &w, &th);
4152 if(nextSpace) { w += space.w; len++; }
4153 if(style.wrap && x + width + w > maxW && x > 0)
4155 lineComplete = true;
4165 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4169 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4172 if(this.x > line.count)
4175 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4177 len = this.x - startPos;
4178 display.FontExtent(font, text + startPos, len, &x, null);
4181 if(!shift) Deselect();
4184 SetViewToCursor(false);
4190 this.x = line.count;
4193 if(!shift) Deselect();
4196 SetViewToCursor(false);
4199 else if(textPos >= line.count && line.next)
4205 sy = this.y * this.space.h;
4206 sx = 0; //textBlock.startX;
4212 sx = 0; //textBlock.startX;
4220 this.x = Min(this.x, line.count);
4221 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4223 if(!shift) Deselect();
4226 if(this.selX != this.x || this.selY != this.y)
4229 SetViewToCursor(false);
4236 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4237 if(!shift) SelDirty();
4238 this.line = this.line.next;
4242 if(!shift) Deselect();
4245 if(this.selX != this.x || this.selY != this.y)
4253 return style.multiLine ? false : true;
4256 if(style.stuckCaret) break;
4257 if(!(style.freeCaret))
4258 this.selX = Min(this.selX, this.selLine.count);
4260 if(!shift) SelDirty();
4263 this.line = this.lines.first;
4264 if(this.y != 0 || this.x != 0)
4274 EditLine line = this.line;
4276 for(c=0; line.buffer[c]; c++)
4277 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4279 if(c != 0 || this.x)
4289 DirtyLine(this.y);*/
4294 if(!shift) Deselect();
4295 SetViewToCursor(true);
4301 if(style.stuckCaret) break;
4302 if(!(style.freeCaret))
4303 this.selX = Min(this.selX, this.selLine.count);
4305 if(!shift) SelDirty();
4310 else if(this.x != this.line.count)
4312 this.x = this.line.count;
4313 //DirtyLine(this.y);
4316 if(!shift) Deselect();
4317 SetViewToCursor(true);
4322 if(style.tabKey && !key.ctrl)
4324 if(this.selY != this.y && style.tabSel)
4326 EditLine firstLine, lastLine;
4330 // Do multi line selection tabbing here
4331 if(this.selY < this.y)
4333 firstLine = this.selLine;
4334 lastLine = this.line;
4340 // Selecting going up
4341 firstLine = this.line;
4342 lastLine = this.selLine;
4349 for(line = firstLine; line; line = line.next, y++)
4351 if(line != lastLine || x)
4355 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4357 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4359 if(line.buffer[c] == '\t')
4364 else if(line.buffer[c] != ' ')
4369 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4372 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4373 char * string = new char[len];
4374 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4375 GetText(string, line, y, 0, line, y, lastC, false, false);
4378 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4379 if(!line.AdjustBuffer(line.count-lastC))
4382 if(style.autoSize) AutoSize();
4386 if(line == lastLine) break;
4391 for(line = firstLine; line; line = line.next, y++)
4395 if(line != lastLine || x)
4399 if(line.count + 1 <= this.maxLineSize)
4401 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4403 if(!line.AdjustBuffer(line.count+1))
4406 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4407 if(style.autoSize) AutoSize();
4409 AddCharAction action { ch = '\t', x = 0, y = y };
4413 memmove(line.buffer+1,line.buffer,line.size-1);
4415 line.buffer[0] = '\t';
4420 if(line.count + this.tabSize <= this.maxLineSize)
4423 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4424 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4425 if(style.autoSize) AutoSize();
4427 if(!line.AdjustBuffer(line.count+this.tabSize))
4431 char * string = new char[this.tabSize + 1];
4432 memset(string, ' ', this.tabSize);
4433 string[this.tabSize] = '\0';
4434 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4437 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4438 line.count+=this.tabSize;
4439 for(c=0; c<this.tabSize; c++)
4440 line.buffer[c] = ' ';
4446 if(line == lastLine) break;
4460 char * addString = new char[this.tabSize + 1];
4462 if(!(style.freeCaret))
4464 this.x = Min(this.x, this.line.count);
4469 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4471 addString[len++] = ' ';
4479 SetViewToCursor(true);
4486 if(!(style.hScroll) || hasHorzScroll) break;
4487 if(this.viewX < this.maxLength)
4489 //this.viewX+=this.space.w*this.tabSize;
4491 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4498 if(!shift) Deselect();
4507 if(!(style.hScroll) || hasHorzScroll) break;
4510 //this.viewX-=this.space.w*this.tabSize;
4511 //this.viewX = Max(this.viewX,0);
4513 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4514 // SetCursorToView();
4521 if(!shift) Deselect();
4535 if(!(style.readOnly))
4541 this.overwrite ^= 1;
4542 UpdateCaretPosition(true);
4547 NotifyOvrToggle(master, this, this.overwrite);
4558 if(style.noSelect) break;
4561 //Save current view position
4562 int tempX = this.viewX;
4563 int tempY = this.viewY;
4567 this.selLine = this.lines.first;
4568 this.y = this.lineCount-1;
4569 this.line = this.lines.last;
4570 this.x = this.line.count;
4573 SetViewToCursor(true);
4575 //Restore previous view position
4576 SetScrollPosition(tempX, tempY * this.space.h);
4582 // TOCHECK: Was there any good reason why we weren't returning false here?
4583 return false; // break;
4588 if(!(style.readOnly))
4596 if(style.readOnly) break;
4602 if(style.readOnly) break;
4607 if(style.readOnly) break;
4611 if(style.readOnly) break;
4615 if(style.readOnly) break;
4616 if(key.shift && key.code == rightBracket)
4618 //Only indent back if you are exactly at one tab.
4620 bool whitespace = true;
4626 EditLine line = this.line;
4628 //Only remove one tab if there is nothing else on the line.
4629 for(i = 0; i < this.line.count; i++)
4631 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4645 for(pos = line.count - 1; pos >= 0; pos--)
4647 char c = line.buffer[pos];
4651 indentwidth += this.tabSize;
4653 //Counting backwards, so when you find a character, indentation is reset
4663 //Place the } to get an undo:
4666 this.x = this.line.count;
4670 putsize = indentwidth;
4672 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4674 newline = new char[putsize+2];
4675 newline[putsize] = '}';
4676 newline[putsize+1] = '\0';
4680 for(; i < indentwidth / this.tabSize; i++)
4682 for(;i < putsize; i++)
4690 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4702 void OnHScroll(ScrollBarAction action, int position, Key key)
4705 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4707 this.viewX = position;
4708 if(action != setRange)
4710 if(!this.mouseMove && style.cursorFollowsView)
4717 void OnVScroll(ScrollBarAction action, int position, Key key)
4719 int oldViewY = this.viewY;
4722 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4724 position /= this.space.h;
4726 if(position < this.viewY)
4728 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4729 style.recomputeSyntax = true;
4731 else if(position > this.viewY)
4733 EditLine oldViewLine = viewLine;
4734 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4735 FigureStartSyntaxStates(oldViewLine, false);
4738 if(action != setRange)
4740 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4743 if(this.x != this.selX || this.y != this.selY)
4747 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4750 int numLines = clientSize.h / this.space.h;
4752 if(Abs(this.viewY - oldViewY) < numLines)
4755 if(this.viewY > oldViewY)
4757 for(y = oldViewY; y <this.viewY; y++)
4758 DirtyLine(y + numLines);
4762 for(y = this.viewY; y <oldViewY; y++)
4763 DirtyLine(y + numLines);
4768 // Fix dirt of stuff before first line...
4769 if(this.viewY - oldViewY > 0)
4771 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4778 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4783 ReplaceTextAction replaceAction = null;
4784 AddCharAction addCharAction = null;
4785 int addedSpaces = 0, addedTabs = 0;
4787 if(ch == '\r') return true;
4788 if(style.stuckCaret /*|EES_READONLY)*/ )
4791 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4793 if(!undoBuffer.dontRecord)
4795 if(selX != x || selY != y)
4800 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4801 oldString = new char[len];
4802 UTF32toUTF8Len(&ch, 1, buffer, 4);
4803 newString = CopyString(buffer);
4804 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4806 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4807 if(selY < y || (selY == y && selX < x))
4809 replaceAction.x1 = selX;
4810 replaceAction.y1 = selY;
4811 replaceAction.x2 = x;
4812 replaceAction.y2 = y;
4816 replaceAction.x1 = x;
4817 replaceAction.y1 = y;
4818 replaceAction.x2 = selX;
4819 replaceAction.y2 = selY;
4821 Record(replaceAction);
4822 undoBuffer.dontRecord++;
4826 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4827 Record(addCharAction);
4833 DelSel(&addedSpaces);
4834 if(this.lineCount+1 > this.maxLines)
4837 Emptyline(this.lines.first,0);
4841 if(!(style.vScroll))
4843 // Make sure it fits, but we need a default line is this.font is too big for window
4844 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4847 if((this.y >= 0) && this.y < this.viewY)
4852 line = EditLine { };
4855 lines.Insert(this.line, line);
4856 line.editBox = this;
4862 // If we're displacing the lines from a current line ...
4865 if(this.line.buffer)
4868 if(this.line.count < endX) endX = this.line.count;
4869 length = this.line.count - endX;
4872 if(!line.AdjustBuffer(length))
4876 if(this.line.buffer)
4878 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4880 if(endX > 4000 || endX < 0)
4883 this.line.count = endX;
4884 this.line.buffer[this.line.count] = '\0';
4885 this.line.AdjustBuffer(this.line.count);
4886 ComputeLength(this.line);
4890 BufferLocation before = { this.line, this.y, this.x }, after;
4896 ComputeLength(this.line);
4899 if(length > 4000 || length < 0)
4902 line.count = length;
4906 line.buffer[line.count] = '\0';
4909 after.line = this.line, after.y = this.y, after.x = this.x;
4911 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4912 if(style.autoSize) AutoSize();
4918 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4919 DelSel(&addedSpaces);
4920 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4921 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4922 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4926 this.selLine = this.line;
4930 replaceAction.x3 = x;
4931 replaceAction.y3 = y;
4932 replaceAction.addedSpaces = addedSpaces;
4933 replaceAction.addedTabs = addedTabs;
4934 undoBuffer.dontRecord--;
4938 addCharAction.addedSpaces = addedSpaces;
4939 addCharAction.addedTabs = addedTabs;
4946 /****************************************************************************
4948 ****************************************************************************/
4951 bool AddCh(unichar ch)
4953 return _AddCh(ch, null, null);
4958 this.modified = true;
4959 NotifyUpdate(master, this);
4960 modifiedDocument = true;
4963 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4966 DelCh(line1, y1, x1, line2, y2, x2, false);
4967 SetViewToCursor(true);
4975 itemEditUndo.disabled = undoBuffer.curAction == 0;
4976 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4977 if(savedAction == undoBuffer.curAction)
4979 modifiedDocument = false;
4981 NotifyUnsetModified(master, this);
4988 itemEditUndo.disabled = undoBuffer.curAction == 0;
4989 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4990 if(savedAction == undoBuffer.curAction)
4992 modifiedDocument = false;
4994 NotifyUnsetModified(master, this);
4998 void Record(UndoAction action)
5000 if(!undoBuffer.dontRecord)
5002 undoBuffer.Record(action);
5003 itemEditUndo.disabled = undoBuffer.curAction == 0;
5004 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5011 this.lines.first, 0,0,
5012 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5015 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5020 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5021 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5022 this.y = line2 ? y2 : (this.lineCount-1);
5023 this.line = line2 ? (EditLine)line2 : this.lines.last;
5026 SetViewToCursor(true);
5030 // TODO: Fix this vs modifiedDocument window property
5031 void SetModified(bool flag)
5035 this.modified = false;
5036 if(flag && !NotifyModified(master, this))
5037 this.modified = true;
5042 bool AddS(char * string)
5049 int addedSpaces = 0, addedTabs = 0;
5050 AddTextAction action = null;
5051 ReplaceTextAction replaceAction = null;
5053 this.pasteOperation = true;
5055 if(style.stuckCaret /*|EES_READONLY)*/ )
5058 if(!undoBuffer.dontRecord)
5060 char * placeString = CopyString(string);
5061 if(!style.multiLine)
5065 for(i = 0; (ch = placeString[i]); i++)
5068 placeString[i] = '\0';
5069 placeString = renew placeString byte[i+1];
5074 if(selX != x || selY != y)
5078 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5079 oldString = new char[len];
5080 newString = placeString;
5081 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5083 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5084 if(selY < y || (selY == y && selX < x))
5086 replaceAction.x1 = selX;
5087 replaceAction.y1 = selY;
5088 replaceAction.x2 = x;
5089 replaceAction.y2 = y;
5093 replaceAction.x1 = x;
5094 replaceAction.y1 = y;
5095 replaceAction.x2 = selX;
5096 replaceAction.y2 = selY;
5098 Record(replaceAction);
5102 if(string[0] == '\n')
5103 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5105 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5113 undoBuffer.dontRecord++;
5114 DelSel(&addedSpaces);
5118 for(c = 0; string[c]; c++)
5120 if(string[c] == '\n' || string[c] == '\r')
5122 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5127 if(string[c] == '\n')
5136 // Reset for next line
5140 if(string[c] == '\r' && *line == '\n')
5152 // Why was this here?
5155 // Add the line here
5157 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5163 undoBuffer.dontRecord--;
5168 action.addedSpaces = addedSpaces;
5169 action.addedTabs = addedTabs;
5171 else if(replaceAction)
5173 replaceAction.y3 = y;
5174 replaceAction.x3 = x;
5175 replaceAction.addedSpaces = addedSpaces;
5176 replaceAction.addedTabs = addedTabs;
5179 UpdateCaretPosition(true);
5181 this.pasteOperation = false;
5194 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5195 SetViewToCursor(true);
5201 void PutCh(unichar ch)
5205 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5206 //if((ch >= 32) || ch == '\n')
5208 int addedSpaces = 0, addedTabs = 0;
5209 ReplaceTextAction replaceAction = null;
5210 AddCharAction addCharAction = null;
5213 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5215 if(this.x < this.line.count && this.overwrite)
5220 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5221 oldString = new char[len];
5222 UTF32toUTF8Len(&ch, 1, buffer, 4);
5223 newString = CopyString(buffer);
5224 GetText(oldString, line, y, x, line, y, x+1, false, false);
5225 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5226 replaceAction.x1 = x;
5227 replaceAction.y1 = y;
5228 replaceAction.x2 = x+1;
5229 replaceAction.y2 = y;
5230 Record(replaceAction);
5232 undoBuffer.dontRecord++;
5233 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5234 undoBuffer.dontRecord--;
5236 else if(!undoBuffer.dontRecord)
5238 if(selX != x || selY != y)
5243 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5244 oldString = new char[len];
5245 UTF32toUTF8Len(&ch, 1, buffer, 4);
5246 newString = CopyString(buffer);
5247 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5248 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5249 if(selY < y || (selY == y && selX < x))
5251 replaceAction.x1 = selX;
5252 replaceAction.y1 = selY;
5253 replaceAction.x2 = x;
5254 replaceAction.y2 = y;
5258 replaceAction.x1 = x;
5259 replaceAction.y1 = y;
5260 replaceAction.x2 = selX;
5261 replaceAction.y2 = selY;
5263 Record(replaceAction);
5267 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5268 Record(addCharAction);
5271 undoBuffer.dontRecord++;
5272 result = _AddCh(ch, &addedSpaces, &addedTabs);
5275 replaceAction.x3 = x;
5276 replaceAction.y3 = y;
5277 replaceAction.addedSpaces = addedSpaces;
5278 replaceAction.addedTabs = addedTabs;
5282 addCharAction.addedSpaces = addedSpaces;
5283 addCharAction.addedTabs = addedTabs;
5285 undoBuffer.dontRecord--;
5289 if(result) SetViewToCursor(true);
5293 void PutS(char * string)
5298 SetViewToCursor(true);
5303 void Printf(char * format, ...)
5307 char temp[MAX_F_STRING];
5309 va_start(args, format);
5310 vsprintf(temp, format, args);
5316 void SetContents(char * format, ...)
5321 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5324 char temp[MAX_F_STRING];
5326 va_start(args, format);
5327 vsprintf(temp, format, args);
5343 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5346 else if(this.line.prev)
5348 EditLine line = this.line.prev;
5352 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5360 this.selLine = this.line;
5365 SetViewToCursor(true);
5370 Emptyline(this.line,this.y);
5371 this.selX = this.x = 0;
5374 this.selLine = this.line;
5376 SetViewToCursor(true);
5386 SetViewToCursor(true);
5394 SetViewToCursor(true);
5398 bool GoToLineNum(int lineNum)
5403 EditLine line = this.lines.first;
5404 for(c = 0; c < lineNum && line; c++, line = line.next);
5414 SetViewToCursor(true);
5421 bool GoToPosition(EditLine line, int y, int x)
5433 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5447 SetViewToCursor(true);
5454 void SetViewToCursor(bool setCaret)
5464 bool dontScroll = false;
5470 selected = selX != this.x || selY != y;
5477 checkLine = dropLine;
5483 checkLine = this.line;
5488 numLines = clientSize.h / space.h;
5490 // This is broken. The EditBox now doesn't do anything different when adding to it,
5491 // regardless of the previous scrolling position. It should be read and then set again
5492 // if one wishes to preserve it.
5493 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5494 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5498 // Horizontal Adjustment
5499 if(!dontScroll && checkLine)
5503 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5506 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5512 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5513 viewX = x - clientSize.w+space.w;
5514 if(x < this.viewX + clientSize.w/2 - space.w)
5515 viewX = Max(0, x - clientSize.w/2 + space.w);
5523 // Vertical Adjustment
5524 if(viewY > checkY) viewY = checkY;
5525 if(viewY + numLines <= checkY)
5527 if(clientSize.h >= space.h)
5529 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5539 for(;dropLine && dropLine.prev && dropY >= numLines;)
5541 dropLine = dropLine.prev;
5545 for(;this.line && this.line.prev && this.y >= numLines;)
5547 this.line = this.line.prev;
5552 SetScrollPosition(viewX, viewY * this.space.h);
5554 UpdateCaretPosition(setCaret);
5560 selLine = this.line;
5570 void CenterOnCursor()
5572 int numLines = clientSize.h / this.space.h;
5573 int y = this.y - numLines / 2;
5575 bool figureSyntax = false;
5576 EditLine oldViewLine = viewLine;
5577 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5582 for(;y < this.viewY; y++)
5584 this.viewLine = this.viewLine.prev;
5585 style.recomputeSyntax = true;
5587 for(;y > this.viewY; y--)
5589 this.viewLine = this.viewLine.next;
5590 figureSyntax = true;
5593 FigureStartSyntaxStates(oldViewLine, false);
5597 SetScrollPosition(this.viewX, viewY * this.space.h);
5598 UpdateCaretPosition(true);
5602 void SetCursorToView()
5613 numLines = clientSize.h / this.space.h;
5617 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5618 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5622 EditLine oldLine = this.line;
5623 bool lastOne = false;
5624 EditLine oldViewLine = this.viewLine;
5625 bool figureSyntax = false;
5627 if(this.y >= this.lineCount-1) return;
5629 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5631 if(this.viewY + numLines < this.lines.count)
5633 this.viewLine = this.viewLine.next;
5635 figureSyntax = true;
5637 else if(c && !lastOne)
5646 FigureStartSyntaxStates(oldViewLine, false);
5647 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5650 SetViewToCursor(false);
5659 if(this.y == 0) return;
5661 numLines = clientSize.h / this.space.h;
5665 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5666 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5670 EditLine oldLine = this.line;
5672 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5677 if(this.viewLine.prev)
5679 this.viewLine = this.viewLine.prev;
5681 style.recomputeSyntax = true;
5685 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5688 SetViewToCursor(false);
5695 if(this.viewLine.prev)
5698 // this.viewLine = this.viewLine.prev;
5701 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5708 if(this.viewLine.next)
5711 // this.viewLine = this.viewLine.next;
5714 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5721 EditLine l1, l2, line;
5722 int x1, x2, nx1, nx2;
5727 if(!this.selLine) return 0;
5728 if(this.selLine == this.line && this.selX == this.x) return 0;
5729 if(this.selY < this.y)
5736 else if(this.selY > this.y)
5743 else if(this.selX < this.x)
5745 l1 = l2 = this.line;
5751 l1 = l2 = this.line;
5755 nx1 = Min(x1,l1.count);
5756 nx2 = Min(x2,l2.count);
5758 // Find Number of Bytes Needed
5760 for(line = l1; line; line = line.next)
5762 if(line == l1) start = nx1; else start = 0;
5763 if(line == l2) end = nx2; else end = line.count;
5765 if(style.freeCaret && line == l2)
5768 count = Max(x2-Max(x1,l1.count),0);
5770 count = Max(x2-l2.count,0);
5774 if(line == l2) break;
5775 // Add Carriage Return / line Feed
5782 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5786 if(!reorder || this.selY < this.y)
5788 if(l1) *l1 = this.selLine;
5789 if(y1) *y1 = this.selY;
5790 if(x1) *x1 = this.selX;
5791 if(l2) *l2 = this.line;
5792 if(y2) *y2 = this.y;
5793 if(x2) *x2 = this.x;
5795 else if(this.selY > this.y)
5797 if(l1) *l1 = this.line;
5798 if(y1) *y1 = this.y;
5799 if(x1) *x1 = this.x;
5800 if(l2) *l2 = this.selLine;
5801 if(y2) *y2 = this.selY;
5802 if(x2) *x2 = this.selX;
5804 else if(this.selX < this.x)
5806 if(l1) *l1 = this.line;
5807 if(y1) *y1 = this.selY;
5808 if(x1) *x1 = this.selX;
5809 if(l2) *l2 = this.line;
5810 if(y2) *y2 = this.y;
5811 if(x2) *x2 = this.x;
5815 if(l1) *l1 = this.line;
5816 if(y1) *y1 = this.y;
5817 if(x1) *x1 = this.x;
5818 if(l2) *l2 = this.line;
5819 if(y2) *y2 = this.selY;
5820 if(x2) *x2 = this.selX;
5825 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5827 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5829 this.selLine = (EditLine)l1;
5832 this.line = (EditLine)l2;
5836 SetViewToCursor(true);
5840 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5842 EditLine l1, l2, line;
5843 int x1, x2, nx1, nx2;
5874 nx1 = Min(x1,l1.count);
5875 nx2 = Min(x2,l2.count);
5878 for(line = l1; line; line = line.next)
5880 if(line == l1) start = nx1; else start = 0;
5881 if(line == l2) end = nx2; else end = line.count;
5884 CopyBytes(text, line.buffer + start, end - start);
5887 numChars += end-start;
5889 if(style.freeCaret && line == l2 && addSpaces)
5892 count = Max(x2-Max(x1,l1.count),0);
5894 count = Max(x2-l2.count,0);
5897 FillBytes(text,' ',count);
5903 if(line == l2) break;
5915 // '\0' terminate Terminate
5922 void GetSel(char * text, bool addCr)
5924 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5930 this.selLine = this.line;
5940 int size = SelSize();
5943 // Try to allocate memory
5944 ClipBoard clipBoard { };
5945 if(clipBoard.Allocate(size+1))
5947 GetSel(clipBoard.memory, true);
5960 ClipBoard clipBoard { };
5961 if(clipBoard.Load())
5962 PutS(clipBoard.memory);
5973 SetViewToCursor(true);
5978 void DeleteSelection()
5983 SetViewToCursor(true);
5989 void Save(File f, bool cr)
5992 savedAction = undoBuffer.curAction;
5994 for(line = this.lines.first; line; line = line.next)
5996 f.Write(line.buffer, line.count,1);
5999 if(cr) f.Putc('\r');
6005 #define BUFFER_SIZE 16384
6008 undoBuffer.dontRecord++;
6011 char buffer[BUFFER_SIZE];
6015 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6016 buffer[count] = '\0';
6022 undoBuffer.dontRecord--;
6023 undoBuffer.count = 0;
6024 undoBuffer.curAction = 0;
6025 itemEditUndo.disabled = undoBuffer.curAction == 0;
6026 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6029 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6033 bool firstPass = true;
6034 EditBoxFindResult result = found;
6036 if(!this.line) return notFound;
6039 for(line = this.line;;)
6047 line = this.lines.first;
6053 line = this.lines.last;
6054 num = this.lineCount - 1;
6060 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6062 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6066 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6069 if(line == this.line && !firstPass) break;
6087 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6091 int searchLen = strlen(text);
6092 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6094 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6095 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6097 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6104 bool OnKeyDown(Key key, unichar ch)
6107 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6109 if(!NotifyKeyDown(master, this, key, ch))
6119 this.mouseMove = false;
6120 OnLeftButtonUp(0,0,0);
6121 SetViewToCursor(true);
6131 public class EditBoxStream : File
6134 BufferLocation start, sel;
6141 EditBox editBox = this.editBox;
6143 editBox.x = start.x;
6144 editBox.y = start.y;
6145 editBox.line = start.line;
6147 editBox.selX = sel.x;
6148 editBox.selY = sel.y;
6149 editBox.selLine = sel.line;
6151 editBox.SetViewToCursor(true);
6152 //editBox.ComputeColumn();
6156 property EditBox editBox
6164 start.line = value.line;
6168 sel.line = value.selLine;
6171 value.GoToHome(true);
6174 get { return editBox; }
6177 uint Read(byte * buffer, uint size, uint count)
6180 EditBox editBox = this.editBox;
6181 EditLine line = editBox.line;
6187 for(;read < count && line; line = (*&line.next))
6189 int numBytes = Min(count - read, (*&line.count) - x);
6192 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6197 for(;read < count && x < (*&line.count);)
6199 buffer[read++] = (*&line.buffer)[x++];
6206 buffer[read++] = '\n';
6211 if(x == (*&line.count) && (*&line.next))
6220 editBox.line = editBox.selLine = line;
6221 editBox.x = editBox.selX = x;
6222 editBox.y = editBox.selY = y;
6227 bool Seek(int pos, FileSeekMode mode)
6230 EditBox editBox = this.editBox;
6231 EditLine line = editBox.line;
6233 if(mode == FileSeekMode::start)
6235 pos = pos - this.pos;
6245 for(;read < pos && line; line = (*&line.next))
6247 int numBytes = Min(pos - read, (*&line.count) - x);
6250 read += numBytes; x += numBytes;
6253 /*for(;read < pos && x < (*&line.count);)
6281 for(;read < pos && line; line = (*&line.prev))
6283 int numBytes = Min(pos - read, x);
6289 /*for(;read < pos && x > 0;)
6299 x = (*&(*&line.prev).count);
6315 editBox.line = editBox.selLine = line;
6316 editBox.x = editBox.selX = x;
6317 editBox.y = editBox.selY = y;
6322 bool Puts(char * string)
6324 EditBox editBox = this.editBox;
6325 BufferLocation start { editBox.line, editBox.y, editBox.x };
6329 editBox.AddS(string);
6331 pos.line = editBox.line;
6335 this.start.AdjustAdd(start, pos);
6336 sel.AdjustAdd(start, pos);
6341 // NOTE: BYTE, NOT UNICODE CHARACTER!
6344 EditBox editBox = this.editBox;
6345 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6350 utf8Bytes[numBytes++] = ch;
6351 utf8Bytes[numBytes] = 0;
6352 if(UTF8Validate(utf8Bytes))
6354 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6356 pos.line = editBox.line;
6359 this.start.AdjustAdd(start, pos);
6360 sel.AdjustAdd(start, pos);
6367 bool Getc(char * ch)
6369 return Read(ch, 1, 1) ? true : false;
6372 void DeleteBytes(uint count)
6374 EditBox editBox = this.editBox;
6377 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6378 BufferLocation end = pos;
6383 for(;c < count && end.line && end.x < end.line.count;)
6388 if(c < count && end.line && end.line.next)
6390 end.line = end.line.next;
6399 start.AdjustDelete(pos, end);
6400 sel.AdjustDelete(pos, end);
6402 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);