1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 public class SyntaxColorScheme
18 Color charLiteralColor;
19 Color stringLiteralColor;
20 Color preprocessorColor;
22 private Array<Color> keywordColors { };
24 public property Container<Color> keywordColors
26 set { keywordColors.Copy((void *)value); }
27 get { return keywordColors; }
35 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
37 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
39 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
41 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
42 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
43 #define UTF8_GET_CHAR(string, numBytes) \
47 byte b = (string)[0]; \
50 numBytes = b ? 1 : 0; \
64 if(b & 0x08) { numBytes = 0; } \
73 for(i = 0; i<numBytes; i++) \
76 ch |= (b = (string)[i]) & mask; \
78 if(i > 1 && (!(b & 0x80) || (b & 0x40))) \
85 ch > 0x10FFFF || (ch >= 0xD800 && ch <= 0xDFFF) || \
86 (ch < 0x80 && numBytes > 1) || \
87 (ch < 0x800 && numBytes > 2) || \
88 (ch < 0x10000 && numBytes > 3))\
98 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
99 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
102 bool inMultiLineComment:1, inPrep:1, escaped:1, continuedSingleLineComment:1, wasInMultiLine:1;
104 bool recomputeSyntax:1;
105 bool cursorFollowsView:1;
107 // bool lineNumbers:1;
112 void UnregisterClass_EditBox()
115 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
124 extern int __ecereVMethodID_class_OnFree;
127 static class ArrayImpl
134 public class OldArray
139 void ** array = (void **)((ArrayImpl)this).array;
140 if(type.type == normalClass || type.type == noHeadClass)
142 for(c = 0; c<size; c++)
143 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
145 // TODO: Call OnFree for structClass
146 delete ((ArrayImpl)this).array;
155 if(((ArrayImpl)this).array)
159 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
162 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
171 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
178 public class UndoAction : struct
181 subclass(UndoAction) type;
182 virtual void Undo(void * data) { type.Undo(this, data); }
183 virtual void Redo(void * data) { type.Redo(this, data); }
185 virtual void Print(void * data) { type.Print(this, data); }
189 if(((Class)type).Destructor)
190 ((void (*)(void *))((Class)type).Destructor)(this);
194 public class UndoBuffer
196 Array<UndoAction> actions { size = 8 };
216 UndoAction action = actions[--curAction];
218 /*Print("Undoing: ");
219 action.Print(data);*/
230 if(curAction < count)
232 UndoAction action = actions[curAction];
235 /*Print("Redoing: ");
236 action.Print(data);*/
244 void Record(UndoAction action)
246 if(!dontRecord && !insideRedo)
248 if(curAction < count)
251 for(c = curAction; c < count; c++)
257 if(count >= actions.size)
258 actions.size += actions.size / 2;
261 /*Print("Recording: ");
262 action.Print(data);*/
264 actions[count++] = action;
267 if(actions.size > count + count / 2 && count + count / 2 >= 8)
268 actions.size = count + count / 2;
275 static class AddCharAction : UndoAction
279 int addedSpaces, addedTabs;
280 type = class(AddCharAction);
282 void Undo(EditBox editBox)
284 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
286 if(addedTabs || addedSpaces)
287 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
288 editBox.UpdateDirty();
291 void Redo(EditBox editBox)
293 editBox.GoToPosition(null, y, x);
295 editBox.UpdateDirty();
298 void Print(EditBox editBox)
300 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
305 static class AddTextAction : UndoAction
309 int addedSpaces, addedTabs;
310 type = class(AddTextAction);
313 void Print(EditBox editBox)
315 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
323 void Undo(EditBox editBox)
328 editBox.GoToPosition(null, y1, x1);
330 l2 = editBox.lines.first;
331 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
333 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
334 if(addedTabs || addedSpaces)
335 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
337 editBox.SetViewToCursor(true);
341 void Redo(EditBox editBox)
343 editBox.GoToPosition(null, y1, x1);
344 editBox.PutS(string);
348 static class DelTextAction : UndoAction
354 type = class(DelTextAction);
357 void Print(EditBox editBox)
359 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
362 void Undo(EditBox editBox)
364 editBox.GoToPosition(null, y1, x1);
365 editBox.PutS(string);
369 editBox.GoToPosition(null, y1, x1);
372 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
373 //editBox.SetViewToCursor(true);
376 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
382 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
383 //editBox.SetViewToCursor(true);
386 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
390 void Redo(EditBox editBox)
395 editBox.GoToPosition(null, y1, x1);
398 l2 = editBox.lines.first;
399 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
401 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
402 editBox.SetViewToCursor(true);
412 static class ReplaceTextAction : UndoAction
414 int y1, x1, y2, x2, y3, x3;
418 int addedSpaces, addedTabs;
420 type = class(ReplaceTextAction);
423 void Print(EditBox editBox)
425 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
428 void Undo(EditBox editBox)
433 editBox.GoToPosition(null, y1, x1);
435 l3 = editBox.lines.first;
436 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
438 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
440 editBox.PutS(oldString);
441 if(addedSpaces || addedTabs)
442 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
444 //editBox.PutS(oldString);
447 editBox.GoToPosition(null, y1, x1);
450 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
456 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
460 void Redo(EditBox editBox)
465 editBox.GoToPosition(null, y1, x1);
467 l2 = editBox.lines.first;
468 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
470 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
472 editBox.PutS(newString);
482 static class MoveTextAction : UndoAction
484 int fy1, fx1, fy2, fx2;
486 type = class(MoveTextAction);
488 void Undo(EditBox editBox)
493 void Redo(EditBox editBox)
499 public class EditLine : struct
511 // Only works for single line edit for now...
512 EditBox editBox = this.editBox;
516 get { return this ? buffer : null; }
518 property EditLine prev { get { return this ? prev : null; } };
519 property EditLine next { get { return this ? next : null; } };
520 property int count { get { return this ? count : 0; } };
527 // This makes sure the buffer always contains at least count characters
528 // Keeps a count/2 pad for avoiding always reallocating memory.
529 bool AdjustBuffer(int count)
537 newSize = (count + (count >> 1));
539 // Shrink down the buffer
542 buffer = new char[newSize];
543 if(!buffer) return false;
547 CopyBytes(buffer, this.buffer, count);
550 this.buffer = buffer;
554 // Increase the buffer
555 else if(size < count)
557 buffer = new char[newSize];
558 if(!buffer) return false;
562 CopyBytes(buffer, this.buffer, this.count + 1); // size);
565 this.buffer = buffer;
574 public struct BufferLocation
579 void AdjustDelete(BufferLocation start, BufferLocation end)
581 // Location is before, nothing to do
582 if(y < start.y || (y == start.y && x < start.x))
584 // Location is inside deleted bytes, point to the start
585 if((y >= start.y && (y > start.y || x >= start.x)) &&
586 (y >= end.y && (y > end.y || x >= end.x)))
591 // Location is on another line
593 y -= end.y - start.y;
594 // Location is the last touched line
601 //if(start.line == end.line)
602 x -= end.x - start.x;
611 // Assuming no carriage return before first character ???? fixed?
612 void AdjustAdd(BufferLocation start, BufferLocation end)
614 int numLines = end.y - start.y;
621 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
624 for(c = 0, line = start.line; c<numLines; c++)
627 //x += numLines ? end.x : (end.x - start.x);
628 x += end.x - start.x;
635 public enum EditBoxFindResult { notFound, found, wrapped };
637 static char * keyWords1[] =
640 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
641 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
643 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
644 "__attribute__", "__stdcall", "_stdcall",
645 "__declspec", "goto",
646 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
647 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
650 "class", "private", "public",
652 "delete", "new", "new0", "renew", "renew0", "define",
655 "dllexport", "dllimport", "stdcall",
656 "subclass", "__on_register_module", "namespace", "using",
657 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
658 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
659 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
662 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
665 "this", "true", "false", "null", "value",
674 static char * keyWords2[] =
676 "defined", "warning", null
679 static char ** keyWords[] = { keyWords1, keyWords2 };
680 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
681 //static int * keyLen[NUM_KEYWORD_GROUPS];
682 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
684 static char searchString[1025], replaceString[1025];
685 static bool matchCase = false, wholeWord = false, searchUp = false;
687 static GoToDialog goToDialog
689 autoCreate = false, isModal = true, text = $"Go To"
692 public class EditBox : CommonControl
694 class_property(icon) = "<:ecere>controls/editBox.png";
698 virtual bool Window::NotifyModified(EditBox editBox);
699 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
700 virtual void Window::NotifyUpdate(EditBox editBox);
701 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
702 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
703 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
705 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
706 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
707 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
709 virtual bool Window::NotifyUnsetModified(EditBox editBox);
711 // Why was this commented out?
712 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
713 // and the scrollArea property isn't compared properly either.
717 if(font) ComputeFont();
718 SetInitSize(initSize);
724 style.vScroll = true;
730 style.hScroll = true;
734 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
735 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
736 property bool readOnly
738 property_category $"Behavior"
741 style.readOnly = value;
742 itemEditCut.disabled = value || !selection;
743 itemEditDelete.disabled = value || !selection;
744 itemEditPaste.disabled = value;
746 get { return style.readOnly; }
748 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
749 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
750 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
751 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
752 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
753 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
754 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
755 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
756 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
757 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
758 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
759 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
760 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
761 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
762 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
763 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
764 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
765 property int numLines { get { return this ? lineCount : 0; } };
766 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
767 property int column { get { return col; } }; // TODO: Add Set
768 property int charPos { get { return x; } }; // TODO: Add Set
769 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
770 property EditLine lastLine { get { return lines.last; } };
771 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
772 property char * contents
774 property_category $"Data"
779 undoBuffer.dontRecord++;
781 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
784 //SetViewToCursor(true);
787 undoBuffer.dontRecord--;
793 char * buffer = null;
796 /* Can't implement this right now because of memory leak... Need string reference counting...
803 for(line = lines.first; line; line = line.next)
804 len += strlen(line.buffer);
806 buffer = new char[len+1];
808 for(line = lines.first; line; line = line.next)
810 int lineLen = strlen(line.buffer);
811 memcpy(buffer + len, line.buffer, lineLen);
817 buffer = this.line ? this.line.buffer : null;
822 property bool overwrite { get { return overwrite; } };
823 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
825 property char * multiLineContents
829 char * buffer = null;
836 for(line = lines.first; line; line = line.next)
837 len += strlen(line.buffer)+1;
839 buffer = new char[len+1];
841 for(line = lines.first; line; line = line.next)
843 int lineLen = strlen(line.buffer);
844 memcpy(buffer + len, line.buffer, lineLen);
846 if(line.next) buffer[len++] = '\n';
859 return this.line.buffer;
864 void SetLineText(char * text)
868 EditLine_SetText(this.line, text);
872 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
873 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
874 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
876 // selectionStart.line, selectionStart.column (With Set)
877 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
891 // Position of Caret (Not necessarily displayed position)
894 // Position of beginning of block (Block ends at (x,y))
896 // line is line at carret, selLine is line at beginning of block
897 EditLine line, selLine, dropLine;
898 // Mouse selection Moving virtual caret
903 // ViewX is x offset in pixels, ViewY is y offset in lines
905 // viewLine is first displayed line
908 // start and end of area to redraw
911 // MaxLine is the longest line
913 // MaxLength is the longest line's length
916 // MouseSelect is true once button is pressed, overwrite is true if mode is on
917 bool mouseSelect, mouseMove, overwrite, wordSelect;
918 // Timer is used for mouse selection scrolling
921 window = this, delay = 0.1;
930 OnMouseMove(mouseX, mouseY, -1);
935 // (mouseX,mouseY) is the position of the mouse in the edit box
940 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
943 bool rightButtonDown;
946 UndoBuffer undoBuffer { data = this };
948 ColorAlpha selectionColor, selectionText;
949 SyntaxColorScheme colorScheme { };
954 Menu editMenu { menu, $"Edit", e };
957 editMenu, $"Cut\tCtrl+X", t, disabled = true;
959 bool NotifySelect(MenuItem item, Modifiers mods)
961 if(!(style.readOnly))
966 MenuItem itemEditCopy
968 editMenu, $"Copy\tCtrl+C", c, disabled = true;
970 bool NotifySelect(MenuItem item, Modifiers mods)
976 MenuItem itemEditPaste
978 editMenu, $"Paste\tCtrl+V", p;
980 bool NotifySelect(MenuItem item, Modifiers mods)
982 if(!(style.readOnly))
987 MenuItem itemEditDelete
989 editMenu, $"Delete\tDel", d, disabled = true;
991 bool NotifySelect(MenuItem item, Modifiers mods)
993 if(!(style.readOnly))
998 MenuDivider { editMenu };
999 MenuItem itemEditSelectAll
1001 editMenu, $"Select All\tCtrl+A", a;
1003 bool NotifySelect(MenuItem item, Modifiers mods)
1009 MenuDivider { editMenu };
1010 MenuItem itemEditUndo
1012 editMenu, $"Undo\tCtrl+Z", u;
1015 bool NotifySelect(MenuItem item, Modifiers mods)
1021 MenuItem itemEditRedo
1023 editMenu, $"Redo\tCtrl+Y", o;
1026 bool NotifySelect(MenuItem item, Modifiers mods)
1032 MenuDivider { editMenu };
1035 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1037 bool NotifySelect(MenuItem item, Modifiers mods)
1040 Find(searchString, wholeWord, matchCase, false);
1042 itemEditFind.NotifySelect(this, item, mods);
1048 editMenu, $"Find Next\tF3", n, f3;
1050 bool NotifySelect(MenuItem item, Modifiers mods)
1053 Find(searchString, wholeWord, matchCase, true);
1055 itemEditFind.NotifySelect(this, item, mods);
1059 MenuItem itemEditFind
1061 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1063 bool NotifySelect(MenuItem item, Modifiers mods)
1067 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1068 searchUp = searchUp;
1071 // Fix dialog from above shouldn't be visible
1072 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1073 void NotifyDestroyed(Window window, DialogResult result)
1075 FindDialog dialog = (FindDialog) window;
1076 searchUp = dialog.searchUp;
1077 strcpy(searchString, dialog.searchString);
1078 matchCase = dialog.matchCase;
1079 wholeWord = dialog.wholeWord;
1088 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1090 bool NotifySelect(MenuItem item, Modifiers mods)
1092 ReplaceDialog dialog
1096 searchString = searchString,
1097 replaceString = replaceString,
1098 matchCase = matchCase,
1099 wholeWord = wholeWord,
1103 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1104 void NotifyDestroyed(Window window, DialogResult result)
1106 ReplaceDialog dialog = (ReplaceDialog)window;
1107 char * replace = dialog.replaceString;
1109 strcpy(replaceString, replace);
1110 strcpy(searchString, dialog.searchString);
1111 matchCase = dialog.matchCase;
1112 wholeWord = dialog.wholeWord;
1119 MenuDivider { editMenu };
1122 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1124 bool NotifySelect(MenuItem item, Modifiers mods)
1126 goToDialog.editBox = this;
1127 goToDialog.master = master;
1128 goToDialog.Create();
1132 MenuDivider { editMenu };
1133 MenuItem itemEditInsertTab
1135 editMenu, $"Insert Tabs", i, checkable = true;
1137 bool NotifySelect(MenuItem item, Modifiers mods)
1139 style.useTab = item.checked;
1145 snapVertScroll = true;
1146 snapHorzScroll = true;
1150 static bool syntaxInit = false;
1155 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1157 for(c = 0; keyWords[g][c]; c++);
1158 //keyLen[g] = new int[c];
1159 for(c = 0; keyWords[g][c]; c++)
1161 keyLen[g][c] = strlen(keyWords[g][c]);
1165 colorScheme.commentColor = dimGray;
1166 colorScheme.charLiteralColor = crimson;
1167 colorScheme.stringLiteralColor = crimson;
1168 colorScheme.preprocessorColor = green;
1169 colorScheme.numberColor = teal;
1170 colorScheme.keywordColors = [ blue, blue ];
1173 FontExtent = Display::FontExtent;
1175 lines.offset = (uint)&((EditLine)0).prev;
1177 style = EditBoxBits { hScroll = true };
1179 // cursor = guiApp.GetCursor(IBeam);
1181 // Default Properties
1183 maxLineSize = 1024;*/
1186 maxLineSize = MAXINT;
1191 mouseSelect = this.mouseMove = false;
1194 x = selX = selY = 0;
1197 line = selLine = null;
1203 endY = clientSize.h;
1206 // Default space to 8 in case window is not constructed
1209 undoBuffer.dontRecord++;
1211 undoBuffer.dontRecord--;
1213 viewLine = lines.first;
1214 style.recomputeSyntax = true;
1220 UpdateCaretPosition(true);
1226 lines.Free(EditLine::Free);
1229 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, bool drawSpaces, Box box)
1231 int count = wc - *renderStart;
1234 if(y + space.h >= box.top && y <= box.bottom)
1240 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1241 surface.TextFont(font);
1242 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1243 if(*x + w + XOFFSET > 0)
1244 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1249 w = numSpaces; // * space.w;
1250 if(*x + w + XOFFSET > 0 && (drawSpaces || surface.GetTextOpacity()))
1251 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1252 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1260 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1261 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1267 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1271 *foreground = (*selected) ? selectionForeground : textColor;
1272 *background = selectionBackground;
1273 *opacity = *selected;
1280 if(this.overwrite && active)
1282 if((style.stuckCaret && wc == line.count && !line.next) ||
1283 (!mouseMove && line == this.line && wc == editX))
1292 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1296 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1297 bool wasInMultiLine = reset ? false : style.wasInMultiLine;
1298 bool inString = false;
1299 bool inQuotes = false;
1300 bool inPrep = reset ? false : style.inPrep;
1301 bool inSingleLineComment = false;
1302 bool escaped = reset ? false : style.escaped;
1303 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1305 EditLine line = reset ? lines.first : firstLine;
1306 // int maxBackUp = 1000, c;
1307 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1308 for(; line != viewLine; line = line.next)
1310 char * text = line.buffer;
1313 bool lastWasStar = false;
1314 bool firstWord = true;
1315 if(!escaped) inPrep = false;
1316 inSingleLineComment = continuedSingleLineComment;
1322 for(c = 0; (ch = text[c]); c++)
1324 bool wasEscaped = escaped;
1325 bool backLastWasStar = lastWasStar;
1326 bool backWasInMultiLine = wasInMultiLine;
1328 lastWasStar = false;
1329 wasInMultiLine = inMultiLineComment;
1332 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1334 if(text[c+1] == '/')
1336 inSingleLineComment = true;
1338 else if(text[c+1] == '*')
1340 inMultiLineComment = true;
1343 else if(backLastWasStar)
1344 inMultiLineComment = false;
1348 if(backWasInMultiLine) lastWasStar = true;
1350 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1352 if(inString && !wasEscaped)
1361 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1363 if(inQuotes && !wasEscaped)
1375 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1382 else if(ch != ' ' && ch != '\t')
1385 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1388 style.continuedSingleLineComment = continuedSingleLineComment;
1389 style.inMultiLineComment = inMultiLineComment;
1390 style.wasInMultiLine = wasInMultiLine;
1391 style.inPrep = inPrep;
1392 style.escaped = escaped;
1396 /*void OnDrawOverChildren(Surface surface)
1398 if(style.lineNumbers)
1400 int currentLineNumber = this.viewY + 1;
1403 for(i = 0; i * space.h < box.height; i++)
1405 // ********* LINE NUMBERING *********
1406 surface.SetForeground(Color{60, 60, 60});
1407 //Highlight current line
1408 if(this.caretY / space.h == currentLineNumber - 1)
1409 surface.SetBackground(Color{220, 220, 220});
1411 surface.SetBackground(Color{230, 230, 230});
1412 surface.textOpacity = true;
1414 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1415 if(currentLineNumber > this.lineCount)
1416 surface.WriteText(0,i*space.h+1," ",6);
1418 surface.WriteText(0,i*space.h+1,lineText,6);
1420 currentLineNumber++;
1425 void OnRedraw(Surface surface)
1429 bool selected = false, selection = true;
1431 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1432 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1433 Color defaultTextColor = property::foreground;
1436 int maxW = clientSize.w;
1438 Color foreground, background;
1441 // Overwrite Caret Stuff
1442 bool overWrite = false;
1443 int overWriteX, overWriteY;
1446 // ****** SYNTAX STATES ******
1447 bool inMultiLineComment = style.inMultiLineComment;
1448 bool inString = false;
1449 bool inQuotes = false;
1450 bool inPrep = style.inPrep;
1451 bool inSingleLineComment = false;
1452 bool escaped = style.escaped;
1453 bool continuedSingleLineComment = style.continuedSingleLineComment;
1454 bool wasInMultiLine = style.wasInMultiLine;
1455 // ****** ************* ******
1458 defaultTextColor = Color { 85, 85, 85 };
1459 textColor = defaultTextColor;
1462 Abs(selectionBackground.r - property::background.r) +
1463 Abs(selectionBackground.g - property::background.g) +
1464 Abs(selectionBackground.b - property::background.b) < 92)
1466 selectionBackground = formColor;
1467 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1470 surface.TextFont(this.font);
1472 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1473 surface.Area(0,0,MAXINT,MAXINT);
1478 surface.SetBackground(formColor);
1479 surface.Area(0,0,clientSize.w, clientSize.h);
1482 if(this.selX == this.x && this.selY == this.y)
1492 editX = Min(this.x,this.line.count);
1493 selX = Min(this.selX,this.selLine.count);
1496 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1498 foreground = selected ? selectionForeground : textColor;
1499 background = selectionBackground;
1505 surface.SetForeground(foreground);
1506 surface.SetBackground(background);
1507 surface.TextOpacity(opacity);
1509 surface.GetBox(box);
1511 for(line = this.viewLine; line; line = line.next)
1513 int x = -this.viewX;
1517 Color newTextColor = textColor = defaultTextColor;
1518 bool lineComplete = false;
1521 // ****** SYNTAX HIGHLIGHTING ******
1522 bool lastWasStar = false;
1523 bool trailingSpace = false;
1524 bool firstWord = true;
1525 if(!escaped) inPrep = false;
1526 inSingleLineComment = continuedSingleLineComment;
1530 // *********************************
1532 /* === DEBUGGING TOOL FOR MAXLINE ===
1534 if(line == this.maxLine)
1536 surface.SetBackground(GREEN|0xFF000000);
1537 surface.TextOpacity(true);
1541 surface.TextOpacity(selected ? true : false);
1542 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1546 if(line == this.selLine && line == this.line)
1548 end = Max(line.count, this.x);
1549 end = Max(end, this.selX);
1551 else if(line == this.selLine)
1552 end = Max(line.count, this.selX);
1553 else if(line == this.line)
1554 end = Max(line.count, this.x);
1562 bool spacing = true;
1563 bool cantHaveWords = false;
1570 lineComplete = false;
1573 textColor = newTextColor;
1576 foreground = textColor;
1577 surface.SetForeground(textColor);
1581 for(; c<end && !cantHaveWords;)
1585 bufferLen += wordLen;
1589 /*if(line.count > 4000)
1596 for(; c<line.count; c++)
1598 unichar ch = line.buffer[c];
1599 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1600 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1601 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1602 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1603 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1606 trailingSpace = false;
1612 for(; c<line.count; c++)
1614 unichar ch = line.buffer[c];
1615 if(ch == '\t' || ch == ' ')
1617 cantHaveWords = true;
1618 if(ch == ' ' && c == line.count-1)
1619 trailingSpace = true;
1623 if(ch != ' ' && ch != '\t')
1628 if(c == line.count && c < end)
1637 cantHaveWords = true;
1642 lastWasStar = false;
1648 bool backEscaped = escaped;
1649 bool backLastWasStar = lastWasStar;
1650 bool backInMultiLineComment = inMultiLineComment;
1651 bool backInString = inString;
1652 bool backInQuotes = inQuotes;
1653 bool backInPrep = inPrep;
1654 bool backInSingleLineComment = inSingleLineComment;
1655 bool backWasInMultiLine = wasInMultiLine;
1657 char * word = line.buffer + c - wordLen;
1659 bool wasEscaped = escaped;
1661 lastWasStar = false;
1663 wasInMultiLine = inMultiLineComment;
1665 // Determine Syntax Highlighting
1666 newTextColor = defaultTextColor;
1669 if(inSingleLineComment || inMultiLineComment)
1671 newTextColor = colorScheme.commentColor;
1675 newTextColor = colorScheme.charLiteralColor;
1679 newTextColor = colorScheme.stringLiteralColor;
1683 newTextColor = colorScheme.preprocessorColor;
1685 if(wordLen == 1 && word[0] == '/')
1687 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1691 inSingleLineComment = true;
1692 newTextColor = colorScheme.commentColor;
1694 else if(word[1] == '*')
1696 inMultiLineComment = true;
1697 newTextColor = colorScheme.commentColor;
1700 else if(backLastWasStar)
1701 inMultiLineComment = false;
1703 else if(wordLen == 1 && word[0] == '*')
1705 if(backWasInMultiLine)
1708 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1710 if(inString && !wasEscaped)
1717 newTextColor = colorScheme.stringLiteralColor;
1720 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1722 if(inQuotes && !wasEscaped)
1727 newTextColor = colorScheme.charLiteralColor;
1730 else if(wordLen == 1 && word[0] == '\\')
1735 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1737 char * dot = strchr(word, '.');
1739 if(dot && dot > word + wordLen) dot = null;
1741 strtod((dot == word + wordLen) ? (dot+1) : word, &s);
1743 strtol(word, &s, 0);
1746 if((dot && *s == 'f' && !isalnum(s[1]) && s[1] != '_') || (!isalpha(*s) && *s != '_'))
1748 int newWordLen = s + ((*s == 'f') ? 1 : 0) - word;
1749 newTextColor = colorScheme.numberColor;
1750 c += newWordLen - wordLen;
1751 wordLen = newWordLen;
1753 else if(dot && dot > word && (isalpha(dot[1]) || dot[1] == '_'))
1754 newTextColor = colorScheme.numberColor;
1759 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1764 newTextColor = colorScheme.preprocessorColor;
1767 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1769 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1771 char ** keys = keyWords[g];
1772 int * len = keyLen[g];
1773 for(ccc = 0; keys[ccc]; ccc++)
1775 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1777 newTextColor = colorScheme.keywordColors[g];
1786 // If highlighting for this word is different, break
1787 if(newTextColor != textColor)
1791 // Better solution than going back?
1794 // Reset syntax flags
1795 escaped = backEscaped;
1796 lastWasStar = backLastWasStar;
1797 inMultiLineComment = backInMultiLineComment;
1798 inString = backInString;
1799 inQuotes = backInQuotes;
1800 inPrep = backInPrep;
1801 inSingleLineComment = backInSingleLineComment;
1802 wasInMultiLine = backWasInMultiLine;
1807 textColor = newTextColor;
1810 foreground = textColor;
1811 surface.SetForeground(textColor);
1818 // If we're not breaking, this can't be rendered as spacing anymore
1821 // Do word wrapping here
1827 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1832 w += numSpaces * space.w;
1834 if(x + viewX + w > maxW)
1836 // Avoid an endless loop until we fix wrapping
1837 if(c - wordLen > start)
1840 lineComplete = true;
1846 bufferLen += wordLen;
1851 int renderStart = start;
1853 bool flagTrailingSpace = false;
1857 // Render checking if we need to split because of selection or to find where to draw insert caret
1858 for(wc = start; wc < start + bufferLen; wc++)
1860 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1861 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1862 if(overWrite == true)
1864 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1865 if(overWriteCh == '\t') overWriteCh = ' ';
1870 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1871 if(flagTrailingSpace) surface.SetBackground(red);
1872 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1873 if(overWrite == true)
1881 surface.TextOpacity(opacity);
1882 surface.SetBackground(background);
1883 surface.SetForeground(foreground);
1890 if(wc < line.count && line.buffer[wc] == '\t')
1892 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1896 numSpaces += space.w;
1900 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1901 if(flagTrailingSpace) surface.SetBackground(red);
1902 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1907 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1908 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1910 if(overWrite == true)
1917 surface.TextOpacity(opacity);
1918 surface.SetBackground(background);
1919 surface.SetForeground(foreground);
1922 if(style.freeCaret && selected)
1924 surface.SetBackground(selectionBackground);
1925 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1926 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1931 if(style.freeCaret && selected)
1933 surface.SetBackground(selectionBackground);
1934 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1938 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1941 if(y > box.bottom) // >=clientSize.h)
1947 surface.TextOpacity(true);
1948 surface.SetForeground(black);
1949 surface.SetBackground(Color {255,255,85});
1950 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1954 void FixScrollArea()
1956 if(style.hScroll || style.vScroll)
1958 int width = maxLength + XOFFSET;
1959 int height = lineCount * space.h;
1960 if(style.freeCaret && line)
1965 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
1969 if(selX > selLine.count)
1970 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
1975 SetScrollLineStep(8, space.h);
1976 SetScrollArea(width, height, true);
1980 void ComputeLength(EditLine line)
1987 for(c = 0; c < line.count; )
1996 for(len = 0; c < line.count; c += numBytes)
1998 ch = line.buffer[c];
1999 numBytes = UTF8_NUM_BYTES(ch);
2001 if(ch == ' ' || ch == '\t')
2008 if(!len && ch == ' ')
2013 else if(!len && ch == '\t')
2015 w = (tabSize * space.w) - (x % (tabSize * space.w));
2019 FontExtent(display, font, line.buffer + start, len, &w, null);
2030 if(line.length > this.maxLength)
2032 this.maxLine = line;
2033 this.maxLength = line.length;
2035 if(style.autoSize) AutoSize();
2044 this.maxLine = null;
2046 for(line = lines.first; line; line = line.next)
2048 if(line.length > this.maxLength)
2050 this.maxLength = line.length;
2051 this.maxLine = line;
2055 if(style.autoSize) AutoSize();
2060 if(this.selY != this.y)
2062 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2066 void ComputeColumn()
2068 // Adjust new edit X position according to tabs
2069 int c, position = 0;
2072 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2074 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2076 position += this.tabSize - (position % this.tabSize);
2080 position += this.x - c;
2081 this.col = position;
2084 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2086 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2089 bool HasCommentOrEscape(EditLine line)
2091 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2096 for(c = line.count-1; c >= 0; c--)
2098 char ch = line.buffer[c];
2104 else //if(ch != ' ' && ch != '\t')
2111 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2113 EditLine line = l1, next;
2115 int oldCount1, oldCount2;
2116 int y, firstViewY, firstY, firstDropY, firstSelY;
2119 DelTextAction action = null;
2120 bool hadComment = false;
2124 hadComment = HasCommentOrEscape(line);
2126 if(y2 > y1 || c2 > c1)
2128 if(start < l1.count)
2130 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2134 oldCount1 = l1.count;
2136 while(c1 < oldCount1)
2138 byte ch = buffer[c1];
2139 if(UTF8_IS_FIRST(ch)) break;
2143 oldCount2 = l2.count;
2145 while(c2 < oldCount2)
2147 byte ch = buffer[c2];
2148 if(UTF8_IS_FIRST(ch)) break;
2153 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2158 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2159 string = new char[len];
2160 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2161 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2165 //oldCount1 = l1.count;
2166 //oldCount2 = l2.count;
2169 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2170 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2173 if(c2 > oldCount2) c2 = oldCount2;
2174 if(!(style.freeCaret))
2175 if(c1 > oldCount1) c1 = oldCount1;
2176 newLineCount = c1 + l2.count-c2;
2182 buffer = new char[line.size ? line.size : 1];
2184 buffer = new char[line.size];
2186 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2191 if(!line.AdjustBuffer(newLineCount))
2195 /*if(newLineCount > 4000 || newLineCount < 0)
2196 printf("Warning");*/
2198 line.count = newLineCount;
2200 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2205 action.addedSpaces = c1-oldCount1;
2207 if(action.addedSpaces > action.x1)
2213 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2214 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2216 line.buffer[line.count] = '\0';
2223 this.dropX -= c2-c1;
2224 this.dropX = Max(this.dropX,0);
2226 this.x = Max(this.x,0);
2233 firstViewY = this.viewY;
2235 firstDropY = this.dropY;
2236 firstSelY = this.selY;
2237 for(line = l1;line;line = next, y++)
2245 if(line == this.viewLine)
2247 if(this.viewLine.next)
2249 this.viewLine = this.viewLine.next;
2251 style.recomputeSyntax = true;
2255 this.viewLine = this.viewLine.prev;
2257 style.recomputeSyntax = true;
2260 else if(y < firstViewY)
2262 if(line == this.line)
2266 this.line = this.line.next;
2267 this.x = this.line.count;
2272 this.line = this.line.prev;
2273 this.x = this.line.count;
2280 if(line == this.dropLine)
2282 if(this.dropLine.next)
2284 this.dropLine = this.dropLine.next;
2285 this.dropX = this.dropLine.count;
2289 this.dropLine = this.dropLine.prev;
2290 this.dropX = this.dropLine.count;
2294 else if(y < firstDropY)
2296 if(line == this.selLine)
2298 if(this.selLine.next)
2300 this.selLine = this.selLine.next;
2301 this.selX = this.selLine.count;
2305 this.selLine = this.selLine.prev;
2306 this.selX = this.selLine.count;
2310 else if(y < firstSelY)
2314 if(line == l2) break;
2318 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2321 style.recomputeSyntax = true;
2326 bool DelSel(int * addedSpacesPtr)
2328 if(this.line != this.selLine || this.x != this.selX)
2330 if(this.selY < this.y)
2332 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2335 this.line = this.selLine;
2337 else if(this.selY > this.y)
2339 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2342 this.selLine = this.line;
2344 else if(this.selX < this.x)
2346 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2349 this.line = this.selLine;
2353 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2356 this.selLine = this.line;
2364 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2366 bool hadComment = false;
2367 // Add the line here
2368 EditLine line = this.line;
2370 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2371 if(!style.hScroll && created)
2373 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2378 // Lock if no place to display.
2381 else if(endX > this.x)
2382 max = Max(this.x, line.count);
2386 for(x = 0, c = 0; c < max+count; )
2391 if(c < Min(this.x, line.count))
2392 string = line.buffer + c;
2395 else if(c < endX + count)
2396 string = stringLine + c - endX;
2398 string = line.buffer + c - endX - count;
2402 w = (tabSize * space.w) - (x % (tabSize * space.w));
2406 numBytes = UTF8_NUM_BYTES(*string);
2407 FontExtent(display, this.font, string, numBytes, &w, null);
2411 if(x >= clientSize.w)
2416 c += numBytes; // - 1;
2422 int addedSpaces = 0;
2425 // Add blank spaces if EES_FREECARET
2426 if(this.x > line.count)
2436 for(c = 0; c<line.count; c++)
2438 if(this.line.buffer[c] == '\t')
2439 position += this.tabSize - (position % this.tabSize);
2443 wantedPosition = position + (this.x - line.count);
2445 // A tab is too much...
2446 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2447 addedSpaces = wantedPosition - position;
2452 position += this.tabSize - (position % this.tabSize);
2453 // Add as many tabs as needed
2454 addedTabs += (wantedPosition - position) / this.tabSize;
2455 position += (addedTabs-1) * this.tabSize;
2456 // Finish off with spaces
2457 addedSpaces = wantedPosition - position;
2461 addedSpaces = this.x - line.count;
2464 this.x = Min(this.x, line.count);
2466 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2472 hadComment = HasCommentOrEscape(line);
2474 // Adjust the size of the line
2475 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2479 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2482 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2483 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2486 *addedTabsPtr = addedTabs;
2487 FillBytes(line.buffer+line.count,'\t',addedTabs);
2489 if(addedTabs > 4000 || addedTabs < 0)
2492 line.count += addedTabs;
2498 FillBytes(line.buffer+line.count,' ',addedSpaces);
2500 if(addedSpaces > 4000 || addedSpaces < 0)
2503 line.count += addedSpaces;
2504 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2506 else if(addedSpacesPtr)
2507 *addedSpacesPtr = 0;
2509 if(count > 4000 || count < 0)
2512 line.count += count;
2513 this.x += count + addedTabs + addedSpaces;
2516 this.selLine = this.line;
2518 line.buffer[line.count] = '\0';
2520 ComputeLength(line);
2525 hasComment = HasCommentOrEscape(line);
2526 if(!undoBuffer.insideRedo)
2528 int backDontRecord = undoBuffer.dontRecord;
2529 undoBuffer.dontRecord = 0;
2530 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2531 undoBuffer.dontRecord = backDontRecord;
2533 if(style.syntax && (hadComment || hasComment || line != this.line))
2535 style.recomputeSyntax = true;
2543 void Emptyline(EditLine line, int y)
2546 DelCh(line, y, 0, line.next, y+1, 0, true);
2548 DelCh(line, y, 0, line, y, line.count, true);
2551 void GoToEnd(bool deselect)
2555 this.line = this.lines.last;
2556 if(this.y != this.lineCount-1)
2558 else if (this.x != this.line.count)
2559 DirtyLine(this.lineCount-1);
2560 this.y = this.lineCount-1;
2561 this.x = this.line.count;
2568 void GoToHome(bool deselect)
2572 this.line = this.lines.first;
2575 else if (this.x !=0)
2576 DirtyLine(this.lineCount-1);
2585 // Returns true if it needs scrolling
2586 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2592 bool needHScroll = false;
2599 line = this.viewLine ? (void *)this.viewLine.prev : null;
2604 line = (void *)this.lines.first;
2609 py = Min(py, clientSize.h);
2611 py = Min(py, this.lineCount);
2613 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2619 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2622 px = Min(px,clientSize.w+this.space.w);
2626 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2629 if(tline) *tline = line;
2632 // Prevent divide by 0 from non valid this.font
2634 return (y < this.viewY) || needHScroll;
2636 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2640 // Minimal Update Management Functions
2644 this.endY = clientSize.h-1;
2645 // ErrorLog("DirtyAll\n");
2648 void DirtyEnd(int y)
2650 if((y - this.viewY)*this.space.h < this.startY)
2651 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2652 this.endY = clientSize.h-1;
2653 //ErrorLog("DirtyEnd %d\n", y);
2656 void DirtyLine(int y)
2660 if((y - this.viewY)*this.space.h < this.startY)
2661 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2662 if((y - this.viewY+1)*this.space.h > this.endY)
2663 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2665 //ErrorLog("DirtyLine %d\n", y);
2672 if(style.recomputeSyntax)
2674 FigureStartSyntaxStates(lines.first, true);
2675 style.recomputeSyntax = false;
2678 if(this.startY > this.endY) return;
2679 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2685 box.right = clientSize.w-1;
2686 box.top = this.startY;
2687 box.bottom = this.endY;
2690 this.startY = clientSize.h;
2694 bool IsMouseOnSelection()
2696 bool mouseOnSelection = false;
2699 int minY = Min(this.selY, this.y);
2700 int maxY = Max(this.selY, this.y);
2701 int minX = Min(this.selX, this.x);
2702 int maxX = Max(this.selX, this.x);
2704 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2706 if(maxX != minX || maxY != minY)
2708 if(y > minY && y < maxY)
2709 mouseOnSelection = true;
2710 else if(y == minY && y == maxY)
2711 mouseOnSelection = (x < maxX && x >= minX);
2715 mouseOnSelection = (x >= this.selX);
2716 else if(y == this.y)
2717 mouseOnSelection = (x >= this.x);
2722 mouseOnSelection = (x < this.selX);
2723 else if(y == this.y)
2724 mouseOnSelection = (x < this.x);
2727 return mouseOnSelection;
2730 void UpdateCaretPosition(bool setCaret)
2734 if(mouseMove || (!overwrite && !style.noCaret))
2736 int max = this.mouseMove ? this.dropX : this.x;
2737 int y = this.mouseMove ? this.dropY : this.y;
2738 EditLine line = this.mouseMove ? this.dropLine : this.line;
2740 if(!(style.freeCaret))
2741 max = Min(max, line.count);
2743 if(FontExtent && display)
2745 for(c = 0; c < max; )
2754 for(len = 0; c < Min(max, line.count); c += numBytes)
2756 ch = line.buffer[c];
2757 numBytes = UTF8_NUM_BYTES(ch);
2759 if(ch == ' ' || ch == '\t')
2766 if(!len && ch == ' ')
2771 else if(!len && ch == '\t')
2773 w = (tabSize * space.w) - (x % (tabSize * space.w));
2777 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2789 caretY = y * this.space.h;
2790 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2795 NotifyCaretMove(master, this, y + 1, x + 1);
2801 void SelectionEnables()
2803 if((x != selX || y != selY) && !selection)
2807 itemEditCut.disabled = false;
2808 itemEditDelete.disabled = false;
2810 itemEditCopy.disabled = false;
2812 this.selection = true;
2814 else if((x == selX && y == selY) && selection)
2816 itemEditCut.disabled = true;
2817 itemEditCopy.disabled = true;
2818 itemEditDelete.disabled = true;
2820 this.selection = false;
2824 void SetSelectCursor()
2826 if(!inactive || !style.noSelect)
2829 cursor = guiApp.GetCursor(arrow);
2830 else if(this.mouseSelect)
2831 cursor = guiApp.GetCursor(iBeam);
2834 if(IsMouseOnSelection())
2835 cursor = guiApp.GetCursor(arrow);
2837 cursor = guiApp.GetCursor(iBeam);
2842 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2845 int x = px ? *px : 0;
2852 if(c < Min(max, line.count))
2856 for(len = 0; c < Min(max, line.count); c += numBytes)
2858 ch = line.buffer[c];
2859 numBytes = UTF8_NUM_BYTES(ch);
2861 if(ch == ' ' || ch == '\t')
2868 if(!len && ch == ' ')
2873 else if(!len && ch == '\t')
2875 w = (tabSize * space.w) - (x % (tabSize * space.w));
2879 FontExtent(display, font, line.buffer + start, len, &w, null);
2883 if(style.freeCaret && c < max)
2892 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2897 int a = start + len;
2900 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2904 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2907 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2911 return Min(this.maxLineSize - 1, start + len);
2917 void SetCursorToViewX()
2919 bool selecting = this.x != selX || y != selY;
2921 // Horizontal Adjustment
2923 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2928 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2940 UpdateCaretPosition(false);
2946 void SetCursorToViewY()
2949 EditLine oldLine = this.line;
2951 bool selecting = this.x != this.selX || this.y != this.selY;
2953 numLines = clientSize.h / this.space.h;
2955 // Vertical Adjustment
2956 if(this.viewY > this.y)
2958 this.y = this.viewY;
2959 this.line = this.viewLine;
2962 if(this.viewY + numLines <= this.y)
2966 this.y = this.viewY-1;
2967 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2974 if(this.line != oldLine)
2976 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2984 this.selLine = this.line;
2987 UpdateCaretPosition(false);
2994 bool SaveFile(char * fileName)
2996 File f = eFile_Open(fileName, FO_WRITE);
3000 eWindow_SetModified(false);
3001 eInstance_Delete(f);
3008 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3012 if(!style.multiLine)
3014 x = (active && this.active && !style.readOnly) ? line.count : 0;
3017 SetViewToCursor(true);
3022 if(!active && modified)
3025 if(!NotifyModified(master, this))
3028 *goOnWithActivation = false;
3035 if(timer) timer.Stop();
3037 mouseSelect = false;
3045 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3046 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3047 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3050 bool OnResizing(int *w, int *h)
3056 *w = space.h * 80 / 14;
3060 void OnResize(int w, int h)
3063 if(!hasHorzScroll && !hasVertScroll && viewLine)
3064 SetViewToCursor(true);
3066 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3068 SetViewToCursor(true);
3073 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3075 if(style.readOnly) return true;
3076 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3077 // Middle button already serves as a dragger as well.
3082 bool OnRightButtonDown(int x, int y, Modifiers mods)
3084 this.rightButtonDown = true;
3089 bool OnRightButtonUp(int x, int y, Modifiers mods)
3092 if(!parent.inactive && rightButtonDown)
3095 Menu contextMenu { };
3097 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3098 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3099 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3100 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3101 MenuDivider { contextMenu };
3102 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3104 popup = PopupMenu { master = this, menu = contextMenu,
3106 nonClient = true, interim = false, parent = parent,
3107 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3109 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3113 rightButtonDown = false;
3117 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3122 if(style.noSelect) return true;
3124 // Should we have a separate 'selectOnActivate' style?
3125 if(!mods.isActivate || (style.readOnly && style.multiLine))
3131 mouseX = mx - XOFFSET;
3134 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3136 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3142 else if(IsMouseOnSelection() && !mods.isActivate)
3152 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3154 if(mods.shift && !mods.isActivate)
3175 UpdateCaretPosition(true);
3179 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3183 mouseSelect = false;
3194 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3196 //PrintLn("MouseMove: ", x, ", ", y);
3202 mouseMove = IsMouseOnSelection();
3206 int size = SelSize();
3209 char * text = new char[size+1];
3213 GetSel(text, false);
3215 if(Max(selY, this.y) == dropY)
3219 if(this.dropX > this.selX)
3220 moveX = this.x - this.selX;
3224 if(this.dropX > this.x)
3225 moveX = this.selX - this.x;
3229 this.dropX -= moveX;
3230 this.selX = this.x = this.dropX;
3231 this.selY = this.y = this.dropY;
3232 this.selLine = this.line = this.dropLine;
3234 SetViewToCursor(true);
3239 byte * c = ((EditBox)this).multiLineContents, * c2;
3240 int l1 = c ? strlen(c) : 0, l2;
3243 c2 = ((EditBox)this).multiLineContents;
3244 l2 = c2 ? strlen(c2) : 0;
3245 if(l1 != l2 || (l1 && strcmp(c, c2)))
3275 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3277 //PrintLn("Dropped: ", x, ", ", y);
3279 NotifyDropped(master, this, x, y);
3286 bool OnMouseMove(int mx, int my, Modifiers mods)
3292 if(mods != -1 && mods.isSideEffect)
3297 if(style.noSelect) return true;
3298 if(wordSelect) return true;
3299 mouseX = mx - XOFFSET;
3302 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3304 if(this.mouseMove || this.mouseSelect)
3313 ((style.hScroll) || (style.vScroll)))
3320 DirtyLine(this.dropY);
3323 DirtyLine(this.dropY);
3324 this.dropLine = line;
3325 SetViewToCursor(true);
3327 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3330 else if(this.mouseSelect)
3332 DirtyLine(this.selY);
3339 SetViewToCursor(true);
3342 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3349 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3354 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3358 if(style.noSelect) return true;
3359 FindMouse(mx, my, &x, &y, &line, false);
3360 if(!NotifyDoubleClick(master, this, line, mods))
3367 for(c = x; c >= 0; c--)
3370 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3371 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3378 for(c = start; c<line.count; c += numBytes)
3380 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3381 if(!ch || !IS_ALUNDER(ch))
3391 this.line = this.selLine = line;
3392 this.wordSelect = (c != start);
3403 Menu fileMenu { menu, "File", F };
3404 saveDialog = fileDialog;
3405 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3406 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3408 if(style.autoSize) AutoSize();
3416 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3417 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3419 space.w = Max(space.w, 1);
3420 large.w = Max(large.w, 1);
3421 space.h = Max(space.h, 1);
3422 large.h = Max(large.h, 1);
3426 for(line = lines.first; line; line = line.next)
3427 ComputeLength(line);
3432 SetViewToCursor(true);
3440 bool OnLoadGraphics()
3442 FontExtent = Display::FontExtent;
3445 // UpdateCaretPosition(true);
3449 void OnUnloadGraphics()
3454 bool OnKeyHit(Key key, unichar ch)
3456 bool shift = (key.shift) ? true : false;
3458 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3460 if(!ch && !key.alt && !key.ctrl)
3462 key.code = (SmartKey)key.code;
3464 else if(!ch && key.alt)
3467 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3470 if(style.readOnly) break;
3471 if(style.stuckCaret) GoToEnd(true);
3472 if(!(style.freeCaret))
3474 this.x = Min(this.x, this.line.count);
3480 EditLine line = this.line;
3482 for(y = this.y; y>= 0; y--)
3484 c = (y == this.y) ? (this.x-1) : line.count-1;
3486 // Slow down when going on lines...
3487 if(y != this.y) break;
3491 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3492 if(IS_ALUNDER(line.buffer[c]))
3497 if(!IS_ALUNDER(line.buffer[c]))
3499 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3513 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3514 this.x = this.selX = Min(c+1, line.count);
3515 this.y = this.selY = y;
3516 this.line = this.selLine = line;
3517 SetViewToCursor(true);
3528 if(style.readOnly) break;
3529 if(style.stuckCaret) break;
3538 if(this.line != this.selLine || this.x != this.selX)
3541 SetViewToCursor(true);
3547 if(this.x < this.line.count)
3551 for(i = this.x; i < this.line.count; i++)
3553 if(!IS_ALUNDER(this.line.buffer[i]))
3557 for(; i < this.line.count; i++)
3559 //Delete trailing whitespace
3560 if(IS_ALUNDER(this.line.buffer[i]))
3563 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3564 SetViewToCursor(true);
3567 else if(this.line.next)
3569 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3570 SetViewToCursor(true);
3576 if(!(style.freeCaret))
3578 this.selX = this.x = Min(this.x, this.line.count);
3581 if(this.x < this.line.count)
3583 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3585 else if(this.line.next)
3587 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3589 SetViewToCursor(true);
3598 if(!key.alt && !key.ctrl)
3602 bool stuffAfter = false;
3606 if(style.stuckCaret) GoToEnd(true);
3607 if(style.readOnly) break;
3608 if(!(style.multiLine)) break;
3610 for(c = 0; c<this.line.count && c<this.x; c++)
3612 if(this.line.buffer[c] == '\t')
3613 position += this.tabSize - (position % this.tabSize);
3614 else if(this.line.buffer[c] == ' ')
3622 if(this.x < this.line.count)
3625 //If last character is a { indent one tab
3626 if(this.line.buffer[this.x - 1] == '{')
3628 //Except if the next non space character is a }
3629 bool indent = false;
3631 for(i = this.x; i < this.line.size; i++)
3632 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3634 if(this.line.buffer[i] != '}')
3639 position += this.tabSize;
3642 addString = new char[position + 2];
3643 addString[len++] = '\n';
3644 addString[len] = '\0';
3646 if(stuffAfter || !style.freeCaret)
3648 for(c = 0; c<position; )
3650 if(style.useTab && c + this.tabSize <= position)
3652 addString[len++] = '\t';
3657 addString[len++] = ' ';
3661 addString[len] = '\0';
3665 if(!stuffAfter && style.freeCaret)
3667 this.x = this.selX = position;
3671 SetViewToCursor(true);
3681 if(style.stuckCaret) break;
3682 if(!(style.freeCaret))
3684 this.x = Min(this.x, this.line.count);
3685 this.selX = Min(this.selX, this.selLine.count);
3688 if(!shift) SelDirty();
3691 bool foundAlpha = false;
3694 EditLine line, lastLine;
3697 for(line = this.line; (line && !found); line = line.prev, y--)
3702 if(this.x == 0 && line != this.line)
3711 if(line == this.line) start = this.x -1; else start = line.count-1;
3712 start = Min(start, line.count-1);
3714 for(c = start; c >= 0;)
3717 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3735 byte ch = line.buffer[c];
3736 if(UTF8_IS_FIRST(ch)) break;
3740 // No next word found,
3741 if(!found && ( this.x > 0 || (!line.count && this.x)))
3755 this.line = lastLine;
3766 byte * buffer = line.buffer;
3769 byte ch = buffer[x];
3770 if(UTF8_IS_FIRST(ch)) break;
3787 if(!shift) _Deselect();
3788 SetViewToCursor(true);
3794 if(style.stuckCaret) break;
3795 if(!(style.freeCaret))
3797 this.x = Min(this.x, this.line.count);
3798 this.selX = Min(this.selX, this.selLine.count);
3801 if(!shift) SelDirty();
3802 if(!shift && (this.x != this.selX || this.y != this.selY));
3805 bool onAChar = false;
3806 if(this.selX != this.x || this.selY != this.y)
3808 if(this.x<this.line.count)
3809 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3811 if(key.shift && onAChar &&
3812 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3814 bool foundAlpha = false;
3816 EditLine line, lastLine;
3818 int lastC, lastY, lastNumBytes;
3820 for(line = this.line; (line && !found); line = line.next, y++)
3822 int start = (line == this.line) ? this.x : 0;
3826 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3832 lastNumBytes = numBytes;
3842 if(!found && (c != this.x || line != this.line))
3855 this.x = lastC + lastNumBytes;
3857 this.line = lastLine;
3864 bool foundAlpha = false;
3869 for(line = this.line; (line && !found); line = line.next, y++)
3871 int start = (line == this.line) ? this.x : 0;
3875 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3891 // No next word found,
3892 if(!found && (c != this.x || line != this.line))
3896 this.x = line.count;
3908 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3912 byte * buffer = line.buffer;
3915 byte ch = buffer[x];
3916 if(UTF8_IS_FIRST(ch)) break;
3937 if(!shift) _Deselect();
3938 SetViewToCursor(true);
3945 if(!style.vScroll || hasVertScroll) break;
3951 if(style.stuckCaret) break;
3953 if(!shift) SelDirty();
3963 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3967 if(!shift) _Deselect();
3969 SetViewToCursor(false);
3972 if(caretY == this.y * space.h)
3978 if(!style.freeCaret)
3979 this.x = Min(this.x, line.count);
3989 int sx = 0, sy = this.y * space.h;
3990 char * text = line.text;
3991 int maxW = clientSize.w - sx;
3992 display.FontExtent(font, " ", 1, null, &th);
3996 int startPos = textPos;
3999 bool lineComplete = false;
4001 if(!style.wrap && caretY == MAXINT)
4004 //textPos = line.count;
4005 //lineComplete = true;
4008 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4012 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4015 len = (nextSpace - (text + textPos));
4017 len = line.count - textPos;
4019 if(textPos < line.count)
4021 display.FontExtent(font, text + textPos, len, &w, null);
4023 if(nextSpace) { w += space.w; len++; }
4025 if(style.wrap && x + width + w > maxW && x > 0)
4027 lineComplete = true;
4037 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4041 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4044 if(this.x > line.count)
4047 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4048 len = this.x - startPos;
4049 display.FontExtent(font, text + startPos, len, &x, null);
4053 if(!shift) _Deselect();
4055 SetViewToCursor(false);
4059 if(sy == caretY - th || textPos >= line.count)
4061 if(textPos >= line.count)
4063 int c = textPos - 1;
4064 while(c > 0 && text[c] == ' ') c--;
4068 this.x = line.count;
4071 if(!shift) _Deselect();
4073 SetViewToCursor(false);
4078 } while(textPos < line.count);
4081 if(!shift) _Deselect();
4083 SetViewToCursor(false);
4092 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4093 if(!shift) SelDirty();
4094 this.line = this.line.prev;
4100 if(!shift) _Deselect();
4104 SetViewToCursor(false);
4110 return style.multiLine ? false : true;
4114 if(!style.vScroll || hasVertScroll)
4121 if(style.stuckCaret) break;
4125 int sx = 0, sy = this.y * this.space.h;
4126 int maxW = clientSize.w - sx;
4127 char * text = line.buffer;
4129 if(!shift) SelDirty();
4135 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4145 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4149 if(!shift) _Deselect();
4151 SetViewToCursor(false);
4154 while(!textPos || (style.freeCaret || textPos<line.count))
4156 int startPos = textPos;
4159 bool lineComplete = false;
4160 if(!style.wrap && sy <= caretY)
4162 textPos = line.count;
4163 lineComplete = true;
4165 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4169 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4172 len = (nextSpace - (text + textPos));
4174 len = line.count - textPos;
4176 if(textPos < line.count)
4178 display.FontExtent(font, text + textPos, len, &w, &th);
4180 if(nextSpace) { w += space.w; len++; }
4181 if(style.wrap && x + width + w > maxW && x > 0)
4183 lineComplete = true;
4193 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4197 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4200 if(this.x > line.count)
4203 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4205 len = this.x - startPos;
4206 display.FontExtent(font, text + startPos, len, &x, null);
4209 if(!shift) _Deselect();
4212 SetViewToCursor(false);
4218 this.x = line.count;
4221 if(!shift) _Deselect();
4224 SetViewToCursor(false);
4227 else if(textPos >= line.count && line.next)
4233 sy = this.y * this.space.h;
4234 sx = 0; //textBlock.startX;
4240 sx = 0; //textBlock.startX;
4248 this.x = Min(this.x, line.count);
4249 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4251 if(!shift) _Deselect();
4254 if(this.selX != this.x || this.selY != this.y)
4257 SetViewToCursor(false);
4264 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4265 if(!shift) SelDirty();
4266 this.line = this.line.next;
4270 if(!shift) _Deselect();
4273 if(this.selX != this.x || this.selY != this.y)
4281 return style.multiLine ? false : true;
4284 if(style.stuckCaret) break;
4285 if(!style.multiLine && key.ctrl) break;
4286 if(!(style.freeCaret))
4287 this.selX = Min(this.selX, this.selLine.count);
4289 if(!shift) SelDirty();
4292 this.line = this.lines.first;
4293 if(this.y != 0 || this.x != 0)
4303 EditLine line = this.line;
4305 for(c=0; line.buffer[c]; c++)
4306 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4308 if(shift && (c != 0 || this.x))
4317 if(shift && this.x != 0)
4323 if(!shift) _Deselect();
4324 SetViewToCursor(true);
4330 if(style.stuckCaret) break;
4331 if(!style.multiLine && key.ctrl) break;
4332 if(!style.freeCaret)
4333 this.selX = Min(this.selX, this.selLine.count);
4335 if(!shift) SelDirty();
4340 else if(this.x != this.line.count)
4342 this.x = this.line.count;
4347 if(!shift) _Deselect();
4348 SetViewToCursor(true);
4353 if(style.tabKey && !key.ctrl)
4355 if(this.selY != this.y && style.tabSel)
4357 EditLine firstLine, lastLine;
4361 // Do multi line selection tabbing here
4362 if(this.selY < this.y)
4364 firstLine = this.selLine;
4365 lastLine = this.line;
4371 // Selecting going up
4372 firstLine = this.line;
4373 lastLine = this.selLine;
4380 for(line = firstLine; line; line = line.next, y++)
4382 if(line != lastLine || x)
4386 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4388 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4390 if(line.buffer[c] == '\t')
4395 else if(line.buffer[c] != ' ')
4400 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4403 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4404 char * string = new char[len];
4405 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4406 GetText(string, line, y, 0, line, y, lastC, false, false);
4409 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4410 if(!line.AdjustBuffer(line.count-lastC))
4416 if(line == lastLine) break;
4421 for(line = firstLine; line; line = line.next, y++)
4425 if(line != lastLine || x)
4429 if(line.count + 1 <= this.maxLineSize)
4431 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4433 if(!line.AdjustBuffer(line.count+1))
4436 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4438 AddCharAction action { ch = '\t', x = 0, y = y };
4442 memmove(line.buffer+1,line.buffer,line.size-1);
4444 line.buffer[0] = '\t';
4449 if(line.count + this.tabSize <= this.maxLineSize)
4452 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4453 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4455 if(!line.AdjustBuffer(line.count+this.tabSize))
4459 char * string = new char[this.tabSize + 1];
4460 memset(string, ' ', this.tabSize);
4461 string[this.tabSize] = '\0';
4462 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4465 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4466 line.count+=this.tabSize;
4467 for(c=0; c<this.tabSize; c++)
4468 line.buffer[c] = ' ';
4474 if(line == lastLine) break;
4477 ComputeLength(maxLine);
4489 char * addString = new char[this.tabSize + 1];
4491 if(!(style.freeCaret))
4493 this.x = Min(this.x, this.line.count);
4497 start = Min(this.x, this.selX);
4498 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4500 addString[len++] = ' ';
4508 SetViewToCursor(true);
4517 if(!(style.hScroll) || hasHorzScroll) break;
4518 if(this.viewX < this.maxLength)
4520 //this.viewX+=this.space.w*this.tabSize;
4522 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4529 if(!shift) _Deselect();
4541 if(!(style.hScroll) || hasHorzScroll) break;
4544 //this.viewX-=this.space.w*this.tabSize;
4545 //this.viewX = Max(this.viewX,0);
4547 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4548 // SetCursorToView();
4555 if(!shift) _Deselect();
4570 if(!(style.readOnly))
4576 this.overwrite ^= 1;
4577 UpdateCaretPosition(true);
4582 NotifyOvrToggle(master, this, this.overwrite);
4593 if(style.noSelect) break;
4596 //Save current view position
4597 int tempX = this.viewX;
4598 int tempY = this.viewY;
4602 this.selLine = this.lines.first;
4603 this.y = this.lineCount-1;
4604 this.line = this.lines.last;
4605 this.x = this.line.count;
4608 SetViewToCursor(true);
4610 //Restore previous view position
4611 SetScrollPosition(tempX, tempY * this.space.h);
4617 // TOCHECK: Was there any good reason why we weren't returning false here?
4618 return false; // break;
4623 if(!(style.readOnly))
4631 if(style.readOnly) break;
4637 if(style.readOnly) break;
4642 if(style.readOnly) break;
4646 if(style.readOnly) break;
4650 if(style.readOnly) break;
4651 if(key.shift && key.code == rightBracket)
4653 //Only indent back if you are exactly at one tab.
4655 bool whitespace = true;
4661 EditLine line = this.line;
4663 //Only remove one tab if there is nothing else on the line.
4664 for(i = 0; i < this.line.count; i++)
4666 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4680 for(pos = line.count - 1; pos >= 0; pos--)
4682 char c = line.buffer[pos];
4686 indentwidth += this.tabSize;
4688 //Counting backwards, so when you find a character, indentation is reset
4698 //Place the } to get an undo:
4701 this.x = this.line.count;
4705 putsize = indentwidth;
4707 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4709 newline = new char[putsize+2];
4710 newline[putsize] = '}';
4711 newline[putsize+1] = '\0';
4715 for(; i < indentwidth / this.tabSize; i++)
4717 for(;i < putsize; i++)
4726 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4738 void OnHScroll(ScrollBarAction action, int position, Key key)
4741 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4743 this.viewX = position;
4744 if(action != setRange)
4746 if(!this.mouseMove && style.cursorFollowsView)
4753 void OnVScroll(ScrollBarAction action, int position, Key key)
4755 int oldViewY = this.viewY;
4758 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4760 position /= this.space.h;
4762 if(position < this.viewY)
4764 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4765 style.recomputeSyntax = true;
4767 else if(position > this.viewY)
4769 EditLine oldViewLine = viewLine;
4770 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4771 FigureStartSyntaxStates(oldViewLine, false);
4774 if(action != setRange)
4776 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4779 if(this.x != this.selX || this.y != this.selY)
4783 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4786 int numLines = clientSize.h / this.space.h;
4788 if(Abs(this.viewY - oldViewY) < numLines)
4791 if(this.viewY > oldViewY)
4793 for(y = oldViewY; y <this.viewY; y++)
4794 DirtyLine(y + numLines);
4798 for(y = this.viewY; y <oldViewY; y++)
4799 DirtyLine(y + numLines);
4804 // Fix dirt of stuff before first line...
4805 if(this.viewY - oldViewY > 0)
4807 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4814 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4819 ReplaceTextAction replaceAction = null;
4820 AddCharAction addCharAction = null;
4821 int addedSpaces = 0, addedTabs = 0;
4823 if(ch == '\r') return true;
4824 if(style.stuckCaret /*|EES_READONLY)*/ )
4827 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4829 if(!undoBuffer.dontRecord)
4831 if(selX != x || selY != y)
4836 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4837 oldString = new char[len];
4838 UTF32toUTF8Len(&ch, 1, buffer, 4);
4839 newString = CopyString(buffer);
4840 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4842 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4843 if(selY < y || (selY == y && selX < x))
4845 replaceAction.x1 = selX;
4846 replaceAction.y1 = selY;
4847 replaceAction.x2 = x;
4848 replaceAction.y2 = y;
4852 replaceAction.x1 = x;
4853 replaceAction.y1 = y;
4854 replaceAction.x2 = selX;
4855 replaceAction.y2 = selY;
4857 Record(replaceAction);
4858 undoBuffer.dontRecord++;
4862 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4863 Record(addCharAction);
4869 DelSel(&addedSpaces);
4870 if(this.lineCount+1 > this.maxLines)
4873 Emptyline(this.lines.first,0);
4877 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
4879 // Make sure it fits, but we need a default line is this.font is too big for window
4880 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4883 if((this.y >= 0) && this.y < this.viewY)
4888 line = EditLine { };
4891 lines.Insert(this.line, line);
4892 line.editBox = this;
4898 // If we're displacing the lines from a current line ...
4901 if(this.line.buffer)
4904 if(this.line.count < endX) endX = this.line.count;
4905 length = this.line.count - endX;
4908 if(!line.AdjustBuffer(length))
4912 if(this.line.buffer)
4914 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4916 if(endX > 4000 || endX < 0)
4919 this.line.count = endX;
4920 this.line.buffer[this.line.count] = '\0';
4921 this.line.AdjustBuffer(this.line.count);
4922 ComputeLength(this.line);
4926 BufferLocation before = { this.line, this.y, this.x }, after;
4931 line.count = length;
4934 if(length > 4000 || length < 0)
4937 ComputeLength(this.line);
4942 line.buffer[line.count] = '\0';
4945 after.line = this.line, after.y = this.y, after.x = this.x;
4947 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4953 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4954 DelSel(&addedSpaces);
4955 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4956 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4957 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4961 this.selLine = this.line;
4965 replaceAction.x3 = x;
4966 replaceAction.y3 = y;
4967 replaceAction.addedSpaces = addedSpaces;
4968 replaceAction.addedTabs = addedTabs;
4969 undoBuffer.dontRecord--;
4973 addCharAction.x -= addedTabs * (tabSize-1);
4974 addCharAction.addedSpaces = addedSpaces;
4975 addCharAction.addedTabs = addedTabs;
4982 /****************************************************************************
4984 ****************************************************************************/
4987 bool AddCh(unichar ch)
4989 return _AddCh(ch, null, null);
4994 this.modified = true;
4995 NotifyUpdate(master, this);
4996 modifiedDocument = true;
4999 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5002 DelCh(line1, y1, x1, line2, y2, x2, false);
5003 SetViewToCursor(true);
5011 itemEditUndo.disabled = undoBuffer.curAction == 0;
5012 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5013 if(savedAction == undoBuffer.curAction)
5015 modifiedDocument = false;
5017 NotifyUnsetModified(master, this);
5024 itemEditUndo.disabled = undoBuffer.curAction == 0;
5025 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5026 if(savedAction == undoBuffer.curAction)
5028 modifiedDocument = false;
5030 NotifyUnsetModified(master, this);
5034 void Record(UndoAction action)
5036 if(!undoBuffer.dontRecord)
5038 undoBuffer.Record(action);
5039 itemEditUndo.disabled = undoBuffer.curAction == 0;
5040 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5047 this.lines.first, 0,0,
5048 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5051 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5056 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5057 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5058 this.y = line2 ? y2 : (this.lineCount-1);
5059 this.line = line2 ? (EditLine)line2 : this.lines.last;
5062 SetViewToCursor(true);
5066 // TODO: Fix this vs modifiedDocument window property
5067 void SetModified(bool flag)
5071 this.modified = false;
5072 if(flag && !NotifyModified(master, this))
5073 this.modified = true;
5078 bool AddS(char * string)
5085 int addedSpaces = 0, addedTabs = 0;
5086 AddTextAction action = null;
5087 ReplaceTextAction replaceAction = null;
5089 this.pasteOperation = true;
5091 if(style.stuckCaret /*|EES_READONLY)*/ )
5094 if(!undoBuffer.dontRecord)
5096 char * placeString = CopyString(string);
5097 if(!style.multiLine)
5101 for(i = 0; (ch = placeString[i]); i++)
5104 placeString[i] = '\0';
5105 placeString = renew placeString byte[i+1];
5110 if(selX != x || selY != y)
5114 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5115 oldString = new char[len];
5116 newString = placeString;
5117 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5119 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5120 if(selY < y || (selY == y && selX < x))
5122 replaceAction.x1 = selX;
5123 replaceAction.y1 = selY;
5124 replaceAction.x2 = x;
5125 replaceAction.y2 = y;
5129 replaceAction.x1 = x;
5130 replaceAction.y1 = y;
5131 replaceAction.x2 = selX;
5132 replaceAction.y2 = selY;
5134 Record(replaceAction);
5138 if(string[0] == '\n')
5139 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5141 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5149 undoBuffer.dontRecord++;
5150 DelSel(&addedSpaces);
5154 for(c = 0; string[c]; c++)
5156 if(string[c] == '\n' || string[c] == '\r')
5158 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5163 if(string[c] == '\n')
5172 // Reset for next line
5176 if(string[c] == '\r' && *line == '\n')
5188 // Why was this here?
5191 // Add the line here
5193 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5199 undoBuffer.dontRecord--;
5204 action.addedSpaces = addedSpaces;
5205 action.addedTabs = addedTabs;
5207 else if(replaceAction)
5209 replaceAction.y3 = y;
5210 replaceAction.x3 = x;
5211 replaceAction.addedSpaces = addedSpaces;
5212 replaceAction.addedTabs = addedTabs;
5215 UpdateCaretPosition(true);
5217 this.pasteOperation = false;
5230 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5231 SetViewToCursor(true);
5237 void PutCh(unichar ch)
5241 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5242 //if((ch >= 32) || ch == '\n')
5244 int addedSpaces = 0, addedTabs = 0;
5245 ReplaceTextAction replaceAction = null;
5246 AddCharAction addCharAction = null;
5249 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5251 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5256 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5257 oldString = new char[len];
5258 UTF32toUTF8Len(&ch, 1, buffer, 4);
5259 newString = CopyString(buffer);
5260 GetText(oldString, line, y, x, line, y, x+1, false, false);
5261 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5262 replaceAction.x1 = x;
5263 replaceAction.y1 = y;
5264 replaceAction.x2 = x+1;
5265 replaceAction.y2 = y;
5266 Record(replaceAction);
5268 undoBuffer.dontRecord++;
5269 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5270 undoBuffer.dontRecord--;
5272 else if(!undoBuffer.dontRecord)
5274 if(selX != x || selY != y)
5279 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5280 oldString = new char[len];
5281 UTF32toUTF8Len(&ch, 1, buffer, 4);
5282 newString = CopyString(buffer);
5283 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5284 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5285 if(selY < y || (selY == y && selX < x))
5287 replaceAction.x1 = selX;
5288 replaceAction.y1 = selY;
5289 replaceAction.x2 = x;
5290 replaceAction.y2 = y;
5294 replaceAction.x1 = x;
5295 replaceAction.y1 = y;
5296 replaceAction.x2 = selX;
5297 replaceAction.y2 = selY;
5299 Record(replaceAction);
5303 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5304 Record(addCharAction);
5307 undoBuffer.dontRecord++;
5308 result = _AddCh(ch, &addedSpaces, &addedTabs);
5311 replaceAction.x3 = x;
5312 replaceAction.y3 = y;
5313 replaceAction.addedSpaces = addedSpaces;
5314 replaceAction.addedTabs = addedTabs;
5318 addCharAction.x -= addedTabs * (tabSize-1);
5319 addCharAction.addedSpaces = addedSpaces;
5320 addCharAction.addedTabs = addedTabs;
5322 undoBuffer.dontRecord--;
5326 if(result) SetViewToCursor(true);
5330 void PutS(char * string)
5335 SetViewToCursor(true);
5340 void Printf(char * format, ...)
5344 char temp[MAX_F_STRING];
5346 va_start(args, format);
5347 vsnprintf(temp, sizeof(temp), format, args);
5348 temp[sizeof(temp)-1] = 0;
5354 void SetContents(char * format, ...)
5358 undoBuffer.dontRecord++;
5360 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5363 char temp[MAX_F_STRING];
5365 va_start(args, format);
5366 vsnprintf(temp, sizeof(temp), format, args);
5367 temp[sizeof(temp)-1] = 0;
5374 undoBuffer.dontRecord--;
5384 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5387 else if(this.line.prev)
5389 EditLine line = this.line.prev;
5393 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5401 this.selLine = this.line;
5406 SetViewToCursor(true);
5411 Emptyline(this.line,this.y);
5412 this.selX = this.x = 0;
5415 this.selLine = this.line;
5417 SetViewToCursor(true);
5427 SetViewToCursor(true);
5435 SetViewToCursor(true);
5439 bool GoToLineNum(int lineNum)
5444 EditLine line = this.lines.first;
5445 for(c = 0; c < lineNum && line; c++, line = line.next);
5455 SetViewToCursor(true);
5462 bool GoToPosition(EditLine line, int y, int x)
5474 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5488 SetViewToCursor(true);
5495 void SetViewToCursor(bool setCaret)
5505 bool dontScroll = false;
5511 selected = selX != this.x || selY != y;
5518 checkLine = dropLine;
5524 checkLine = this.line;
5529 numLines = clientSize.h / space.h;
5531 // This is broken. The EditBox now doesn't do anything different when adding to it,
5532 // regardless of the previous scrolling position. It should be read and then set again
5533 // if one wishes to preserve it.
5534 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5535 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5539 // Horizontal Adjustment
5540 if(!dontScroll && checkLine)
5544 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5547 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5553 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5554 viewX = x - clientSize.w+space.w;
5555 if(x < this.viewX + clientSize.w/2 - space.w)
5556 viewX = Max(0, x - clientSize.w/2 + space.w);
5564 // Vertical Adjustment
5565 if(viewY > checkY) viewY = checkY;
5566 if(viewY + numLines <= checkY)
5568 if(clientSize.h >= space.h)
5570 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5580 for(;dropLine && dropLine.prev && dropY >= numLines;)
5582 dropLine = dropLine.prev;
5586 for(;this.line && this.line.prev && this.y >= numLines;)
5588 this.line = this.line.prev;
5593 SetScrollPosition(viewX, viewY * this.space.h);
5595 UpdateCaretPosition(setCaret);
5601 selLine = this.line;
5611 void CenterOnCursor()
5613 int numLines = clientSize.h / this.space.h;
5614 int y = this.y - numLines / 2;
5616 bool figureSyntax = false;
5617 EditLine oldViewLine = viewLine;
5618 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5623 for(;y < this.viewY; y++)
5625 this.viewLine = this.viewLine.prev;
5626 style.recomputeSyntax = true;
5628 for(;y > this.viewY; y--)
5630 this.viewLine = this.viewLine.next;
5631 figureSyntax = true;
5634 FigureStartSyntaxStates(oldViewLine, false);
5638 SetScrollPosition(this.viewX, viewY * this.space.h);
5639 UpdateCaretPosition(true);
5643 void SetCursorToView()
5654 numLines = clientSize.h / this.space.h;
5658 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5659 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5663 EditLine oldLine = this.line;
5664 bool lastOne = false;
5665 EditLine oldViewLine = this.viewLine;
5666 bool figureSyntax = false;
5668 if(this.y >= this.lineCount-1) return;
5670 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5672 if(this.viewY + numLines < this.lines.count)
5674 this.viewLine = this.viewLine.next;
5676 figureSyntax = true;
5678 else if(c && !lastOne)
5687 FigureStartSyntaxStates(oldViewLine, false);
5688 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5691 SetViewToCursor(false);
5700 if(this.y == 0) return;
5702 numLines = clientSize.h / this.space.h;
5706 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5707 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5711 EditLine oldLine = this.line;
5713 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5718 if(this.viewLine.prev)
5720 this.viewLine = this.viewLine.prev;
5722 style.recomputeSyntax = true;
5726 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5729 SetViewToCursor(false);
5736 if(this.viewLine.prev)
5739 // this.viewLine = this.viewLine.prev;
5742 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5749 if(this.viewLine.next)
5752 // this.viewLine = this.viewLine.next;
5755 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5762 EditLine l1, l2, line;
5763 int x1, x2, nx1, nx2;
5768 if(!this.selLine) return 0;
5769 if(this.selLine == this.line && this.selX == this.x) return 0;
5770 if(this.selY < this.y)
5777 else if(this.selY > this.y)
5784 else if(this.selX < this.x)
5786 l1 = l2 = this.line;
5792 l1 = l2 = this.line;
5796 nx1 = Min(x1,l1.count);
5797 nx2 = Min(x2,l2.count);
5799 // Find Number of Bytes Needed
5801 for(line = l1; line; line = line.next)
5803 if(line == l1) start = nx1; else start = 0;
5804 if(line == l2) end = nx2; else end = line.count;
5806 if(style.freeCaret && line == l2)
5809 count = Max(x2-Max(x1,l1.count),0);
5811 count = Max(x2-l2.count,0);
5815 if(line == l2) break;
5816 // Add Carriage Return / line Feed
5823 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5827 if(!reorder || this.selY < this.y)
5829 if(l1) *l1 = this.selLine;
5830 if(y1) *y1 = this.selY;
5831 if(x1) *x1 = this.selX;
5832 if(l2) *l2 = this.line;
5833 if(y2) *y2 = this.y;
5834 if(x2) *x2 = this.x;
5836 else if(this.selY > this.y)
5838 if(l1) *l1 = this.line;
5839 if(y1) *y1 = this.y;
5840 if(x1) *x1 = this.x;
5841 if(l2) *l2 = this.selLine;
5842 if(y2) *y2 = this.selY;
5843 if(x2) *x2 = this.selX;
5845 else if(this.selX < this.x)
5847 if(l1) *l1 = this.line;
5848 if(y1) *y1 = this.selY;
5849 if(x1) *x1 = this.selX;
5850 if(l2) *l2 = this.line;
5851 if(y2) *y2 = this.y;
5852 if(x2) *x2 = this.x;
5856 if(l1) *l1 = this.line;
5857 if(y1) *y1 = this.y;
5858 if(x1) *x1 = this.x;
5859 if(l2) *l2 = this.line;
5860 if(y2) *y2 = this.selY;
5861 if(x2) *x2 = this.selX;
5866 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5868 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5870 this.selLine = (EditLine)l1;
5873 this.line = (EditLine)l2;
5877 SetViewToCursor(true);
5881 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5883 EditLine l1, l2, line;
5884 int x1, x2, nx1, nx2;
5915 nx1 = Min(x1,l1.count);
5916 nx2 = Min(x2,l2.count);
5919 for(line = l1; line; line = line.next)
5921 if(line == l1) start = nx1; else start = 0;
5922 if(line == l2) end = nx2; else end = line.count;
5925 CopyBytes(text, line.buffer + start, end - start);
5928 numChars += end-start;
5930 if(style.freeCaret && line == l2 && addSpaces)
5933 count = Max(x2-Max(x1,l1.count),0);
5935 count = Max(x2-l2.count,0);
5938 FillBytes(text,' ',count);
5944 if(line == l2) break;
5956 // '\0' terminate Terminate
5963 void GetSel(char * text, bool addCr)
5965 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5968 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
5986 int size = SelSize();
5989 // Try to allocate memory
5990 ClipBoard clipBoard { };
5991 if(clipBoard.Allocate(size+1))
5993 GetSel(clipBoard.memory, true);
6006 ClipBoard clipBoard { };
6007 if(clipBoard.Load())
6008 PutS(clipBoard.memory);
6020 SetViewToCursor(true);
6026 void DeleteSelection()
6031 SetViewToCursor(true);
6037 void Save(File f, bool cr)
6040 savedAction = undoBuffer.curAction;
6042 for(line = this.lines.first; line; line = line.next)
6044 if(style.syntax && line.count && isspace(line.buffer[line.count-1]))
6047 for(c=line.count-2; c>=-1; c--)
6049 if(c == -1 || !isspace(line.buffer[c]))
6052 line.buffer[c] = '\0';
6053 line.count -= (line.count - c);
6058 f.Write(line.buffer, line.count,1);
6061 if(cr) f.Putc('\r');
6067 #define BUFFER_SIZE 16384
6070 undoBuffer.dontRecord++;
6073 char buffer[BUFFER_SIZE];
6077 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6078 buffer[count] = '\0';
6084 undoBuffer.dontRecord--;
6085 undoBuffer.count = 0;
6086 undoBuffer.curAction = 0;
6087 itemEditUndo.disabled = undoBuffer.curAction == 0;
6088 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6091 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6095 bool firstPass = true;
6096 EditBoxFindResult result = found;
6098 if(!this.line) return notFound;
6101 for(line = this.line;;)
6109 line = this.lines.first;
6115 line = this.lines.last;
6116 num = this.lineCount - 1;
6122 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6124 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6128 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6131 if(line == this.line && !firstPass) break;
6149 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6153 int searchLen = strlen(text);
6154 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6156 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6157 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6159 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6166 bool OnKeyDown(Key key, unichar ch)
6169 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6171 if(!NotifyKeyDown(master, this, key, ch))
6181 this.mouseMove = false;
6182 OnLeftButtonUp(0,0,0);
6183 SetViewToCursor(true);
6193 public class EditBoxStream : File
6196 BufferLocation start, sel;
6203 EditBox editBox = this.editBox;
6205 editBox.x = start.x;
6206 editBox.y = start.y;
6207 editBox.line = start.line;
6209 editBox.selX = sel.x;
6210 editBox.selY = sel.y;
6211 editBox.selLine = sel.line;
6213 editBox.SetViewToCursor(true);
6214 //editBox.ComputeColumn();
6218 property EditBox editBox
6226 start.line = value.line;
6230 sel.line = value.selLine;
6233 value.GoToHome(true);
6236 get { return editBox; }
6239 uint Read(byte * buffer, uint size, uint count)
6242 EditBox editBox = this.editBox;
6243 EditLine line = editBox.line;
6249 for(;read < count && line; line = (*&line.next))
6251 int numBytes = Min(count - read, (*&line.count) - x);
6254 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6259 for(;read < count && x < (*&line.count);)
6261 buffer[read++] = (*&line.buffer)[x++];
6268 buffer[read++] = '\n';
6273 if(x == (*&line.count) && (*&line.next))
6282 editBox.line = editBox.selLine = line;
6283 editBox.x = editBox.selX = x;
6284 editBox.y = editBox.selY = y;
6289 bool Seek(int pos, FileSeekMode mode)
6292 EditBox editBox = this.editBox;
6293 EditLine line = editBox.line;
6295 if(mode == FileSeekMode::start)
6297 pos = pos - this.pos;
6307 for(;read < pos && line; line = (*&line.next))
6309 int numBytes = Min(pos - read, (*&line.count) - x);
6312 read += numBytes; x += numBytes;
6315 /*for(;read < pos && x < (*&line.count);)
6343 for(;read < pos && line; line = (*&line.prev))
6345 int numBytes = Min(pos - read, x);
6351 /*for(;read < pos && x > 0;)
6361 x = (*&(*&line.prev).count);
6377 editBox.line = editBox.selLine = line;
6378 editBox.x = editBox.selX = x;
6379 editBox.y = editBox.selY = y;
6384 bool Puts(char * string)
6386 EditBox editBox = this.editBox;
6387 BufferLocation start { editBox.line, editBox.y, editBox.x };
6391 editBox.AddS(string);
6393 pos.line = editBox.line;
6397 this.start.AdjustAdd(start, pos);
6398 sel.AdjustAdd(start, pos);
6403 // NOTE: BYTE, NOT UNICODE CHARACTER!
6406 EditBox editBox = this.editBox;
6407 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6412 utf8Bytes[numBytes++] = ch;
6413 utf8Bytes[numBytes] = 0;
6414 if(UTF8Validate(utf8Bytes))
6416 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6418 pos.line = editBox.line;
6421 this.start.AdjustAdd(start, pos);
6422 sel.AdjustAdd(start, pos);
6429 bool Getc(char * ch)
6431 return Read(ch, 1, 1) ? true : false;
6434 void DeleteBytes(uint count)
6436 EditBox editBox = this.editBox;
6439 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6440 BufferLocation end = pos;
6445 for(;c < count && end.line && end.x < end.line.count;)
6450 if(c < count && end.line && end.line.next)
6452 end.line = end.line.next;
6461 start.AdjustDelete(pos, end);
6462 sel.AdjustDelete(pos, end);
6464 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);