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, continuedString:1, continuedQuotes: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","signed","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;
1304 bool continuedString = reset ? false : style.continuedString;
1305 bool continuedQuotes = reset ? false : style.continuedQuotes;
1307 EditLine line = reset ? lines.first : firstLine;
1308 // int maxBackUp = 1000, c;
1309 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1310 for(; line != viewLine; line = line.next)
1312 char * text = line.buffer;
1315 bool lastWasStar = false;
1316 bool firstWord = true;
1317 if(!escaped) inPrep = false;
1318 inSingleLineComment = continuedSingleLineComment;
1320 inString = continuedString;
1321 inQuotes = continuedQuotes;
1324 for(c = 0; (ch = text[c]); c++)
1326 bool wasEscaped = escaped;
1327 bool backLastWasStar = lastWasStar;
1328 bool backWasInMultiLine = wasInMultiLine;
1330 lastWasStar = false;
1331 wasInMultiLine = inMultiLineComment;
1334 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1336 if(text[c+1] == '/')
1338 inSingleLineComment = true;
1340 else if(text[c+1] == '*')
1342 inMultiLineComment = true;
1345 else if(backLastWasStar)
1346 inMultiLineComment = false;
1350 if(backWasInMultiLine) lastWasStar = true;
1352 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1354 if(inString && !wasEscaped)
1363 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1365 if(inQuotes && !wasEscaped)
1377 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1384 else if(ch != ' ' && ch != '\t')
1387 if(line.count && line.text[line.count - 1] == '\\')
1389 continuedSingleLineComment = inSingleLineComment;
1390 continuedString = inString;
1391 continuedQuotes = inQuotes;
1395 continuedSingleLineComment = false;
1396 continuedString = false;
1397 continuedQuotes = false;
1401 style.continuedSingleLineComment = continuedSingleLineComment;
1402 style.continuedString = continuedString;
1403 style.continuedQuotes = continuedQuotes;
1404 style.inMultiLineComment = inMultiLineComment;
1405 style.wasInMultiLine = wasInMultiLine;
1406 style.inPrep = inPrep;
1407 style.escaped = escaped;
1411 /*void OnDrawOverChildren(Surface surface)
1413 if(style.lineNumbers)
1415 int currentLineNumber = this.viewY + 1;
1418 for(i = 0; i * space.h < box.height; i++)
1420 // ********* LINE NUMBERING *********
1421 surface.SetForeground(Color{60, 60, 60});
1422 //Highlight current line
1423 if(this.caretY / space.h == currentLineNumber - 1)
1424 surface.SetBackground(Color{220, 220, 220});
1426 surface.SetBackground(Color{230, 230, 230});
1427 surface.textOpacity = true;
1429 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1430 if(currentLineNumber > this.lineCount)
1431 surface.WriteText(0,i*space.h+1," ",6);
1433 surface.WriteText(0,i*space.h+1,lineText,6);
1435 currentLineNumber++;
1440 void OnRedraw(Surface surface)
1444 bool selected = false, selection = true;
1446 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1447 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1448 Color defaultTextColor = property::foreground;
1451 int maxW = clientSize.w;
1453 Color foreground, background;
1456 // Overwrite Caret Stuff
1457 bool overWrite = false;
1458 int overWriteX, overWriteY;
1461 // ****** SYNTAX STATES ******
1462 bool inMultiLineComment = style.inMultiLineComment;
1463 bool inString = false;
1464 bool inQuotes = false;
1465 bool inPrep = style.inPrep;
1466 bool inSingleLineComment = false;
1467 bool escaped = style.escaped;
1468 bool continuedSingleLineComment = style.continuedSingleLineComment;
1469 bool continuedString = style.continuedString;
1470 bool continuedQuotes = style.continuedQuotes;
1471 bool wasInMultiLine = style.wasInMultiLine;
1472 // ****** ************* ******
1475 defaultTextColor = Color { 85, 85, 85 };
1476 textColor = defaultTextColor;
1479 Abs(selectionBackground.r - property::background.r) +
1480 Abs(selectionBackground.g - property::background.g) +
1481 Abs(selectionBackground.b - property::background.b) < 92)
1483 selectionBackground = formColor;
1484 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1487 surface.TextFont(this.font);
1489 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1490 surface.Area(0,0,MAXINT,MAXINT);
1495 surface.SetBackground(formColor);
1496 surface.Area(0,0,clientSize.w, clientSize.h);
1499 if(this.selX == this.x && this.selY == this.y)
1509 editX = Min(this.x,this.line.count);
1510 selX = Min(this.selX,this.selLine.count);
1513 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1515 foreground = selected ? selectionForeground : textColor;
1516 background = selectionBackground;
1522 surface.SetForeground(foreground);
1523 surface.SetBackground(background);
1524 surface.TextOpacity(opacity);
1526 surface.GetBox(box);
1528 for(line = this.viewLine; line; line = line.next)
1530 int x = -this.viewX;
1534 Color newTextColor = textColor = defaultTextColor;
1535 bool lineComplete = false;
1538 // ****** SYNTAX HIGHLIGHTING ******
1539 bool lastWasStar = false;
1540 bool trailingSpace = false;
1541 bool firstWord = true;
1542 if(!escaped) inPrep = false;
1543 inSingleLineComment = continuedSingleLineComment;
1545 inString = continuedString;
1546 inQuotes = continuedQuotes;
1547 // *********************************
1549 /* === DEBUGGING TOOL FOR MAXLINE ===
1551 if(line == this.maxLine)
1553 surface.SetBackground(GREEN|0xFF000000);
1554 surface.TextOpacity(true);
1558 surface.TextOpacity(selected ? true : false);
1559 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1563 if(line == this.selLine && line == this.line)
1565 end = Max(line.count, this.x);
1566 end = Max(end, this.selX);
1568 else if(line == this.selLine)
1569 end = Max(line.count, this.selX);
1570 else if(line == this.line)
1571 end = Max(line.count, this.x);
1579 bool spacing = true;
1580 bool cantHaveWords = false;
1587 lineComplete = false;
1590 textColor = newTextColor;
1593 foreground = textColor;
1594 surface.SetForeground(textColor);
1598 for(; c<end && !cantHaveWords;)
1602 bufferLen += wordLen;
1606 /*if(line.count > 4000)
1613 for(; c<line.count; c++)
1615 unichar ch = line.buffer[c];
1616 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1617 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1618 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1619 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1620 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1623 trailingSpace = false;
1629 for(; c<line.count; c++)
1631 unichar ch = line.buffer[c];
1632 if(ch == '\t' || ch == ' ')
1634 cantHaveWords = true;
1635 if(ch == ' ' && c == line.count-1)
1636 trailingSpace = true;
1640 if(ch != ' ' && ch != '\t')
1645 if(c == line.count && c < end)
1654 cantHaveWords = true;
1659 lastWasStar = false;
1665 bool backEscaped = escaped;
1666 bool backLastWasStar = lastWasStar;
1667 bool backInMultiLineComment = inMultiLineComment;
1668 bool backInString = inString;
1669 bool backInQuotes = inQuotes;
1670 bool backInPrep = inPrep;
1671 bool backInSingleLineComment = inSingleLineComment;
1672 bool backWasInMultiLine = wasInMultiLine;
1674 char * word = line.buffer + c - wordLen;
1676 bool wasEscaped = escaped;
1678 lastWasStar = false;
1680 wasInMultiLine = inMultiLineComment;
1682 // Determine Syntax Highlighting
1683 newTextColor = defaultTextColor;
1686 if(inSingleLineComment || inMultiLineComment)
1688 newTextColor = colorScheme.commentColor;
1692 newTextColor = colorScheme.charLiteralColor;
1696 newTextColor = colorScheme.stringLiteralColor;
1700 newTextColor = colorScheme.preprocessorColor;
1702 if(wordLen == 1 && word[0] == '/')
1704 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1708 inSingleLineComment = true;
1709 newTextColor = colorScheme.commentColor;
1711 else if(word[1] == '*')
1713 inMultiLineComment = true;
1714 newTextColor = colorScheme.commentColor;
1717 else if(backLastWasStar)
1718 inMultiLineComment = false;
1720 else if(wordLen == 1 && word[0] == '*')
1722 if(backWasInMultiLine)
1725 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1727 if(inString && !wasEscaped)
1734 newTextColor = colorScheme.stringLiteralColor;
1737 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1739 if(inQuotes && !wasEscaped)
1744 newTextColor = colorScheme.charLiteralColor;
1747 else if(wordLen == 1 && word[0] == '\\')
1752 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && (isdigit(word[0]) || (word[0] == '.' && isdigit(word[1]))))
1754 char * dot = strchr(word, '.');
1755 bool isHex = (word[0] == '0' && (word[1] == 'x' || word[1] == 'X'));
1761 exponent = strchr(word, 'p');
1762 if(!exponent) exponent = strchr(word, 'P');
1766 exponent = strchr(word, 'e');
1767 if(!exponent) exponent = strchr(word, 'E');
1769 if(dot && dot > word + wordLen) dot = null;
1770 isReal = dot || exponent;
1772 strtod(word, &s); // strtod() seems to break on hex floats (e.g. 0x23e3p12, 0x1.fp3)
1774 strtol(word, &s, 0);
1780 int gotF = 0, gotL = 0, gotU = 0, gotI = 0;
1783 for(i = 0; valid && i < 5 && (ch = s[i]) && (isalnum(ch) || ch == '_'); i++)
1787 case 'f': case 'F': gotF++; if(gotF > 1 || !isReal) valid = false; break;
1790 if(gotL > 2 || isReal || (gotL == 2 && (s[i-1] != ch)))
1793 case 'u': case 'U': gotU++; if(gotU > 1 || isReal) valid = false; break;
1794 case 'i': case 'I': case 'j': case 'J': gotI++; if(gotI > 1) valid = false; break;
1795 default: valid = false;
1799 // Don't highlight numbers with too many decimal points
1800 if(s[0] == '.' && isdigit(s[1]))
1803 while(s[0] == '.' && isdigit(s[1]))
1805 int newWordLen = s - word;
1806 c += newWordLen - wordLen;
1807 wordLen = newWordLen;
1810 newWordLen = s - word;
1811 c += newWordLen - wordLen;
1812 wordLen = newWordLen;
1816 int newWordLen = s + i - word;
1817 newTextColor = colorScheme.numberColor;
1818 c += newWordLen - wordLen;
1819 wordLen = newWordLen;
1821 else if(dot && dot > word && dot < s)
1822 newTextColor = colorScheme.numberColor;
1827 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1832 newTextColor = colorScheme.preprocessorColor;
1835 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1837 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1839 char ** keys = keyWords[g];
1840 int * len = keyLen[g];
1841 for(ccc = 0; keys[ccc]; ccc++)
1843 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1845 newTextColor = colorScheme.keywordColors[g];
1854 // If highlighting for this word is different, break
1855 if(newTextColor != textColor)
1859 // Better solution than going back?
1862 // Reset syntax flags
1863 escaped = backEscaped;
1864 lastWasStar = backLastWasStar;
1865 inMultiLineComment = backInMultiLineComment;
1866 inString = backInString;
1867 inQuotes = backInQuotes;
1868 inPrep = backInPrep;
1869 inSingleLineComment = backInSingleLineComment;
1870 wasInMultiLine = backWasInMultiLine;
1875 textColor = newTextColor;
1878 foreground = textColor;
1879 surface.SetForeground(textColor);
1886 // If we're not breaking, this can't be rendered as spacing anymore
1889 // Do word wrapping here
1895 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1900 w += numSpaces * space.w;
1902 if(x + viewX + w > maxW)
1904 // Avoid an endless loop until we fix wrapping
1905 if(c - wordLen > start)
1908 lineComplete = true;
1914 bufferLen += wordLen;
1919 int renderStart = start;
1921 bool flagTrailingSpace = false;
1925 // Render checking if we need to split because of selection or to find where to draw insert caret
1926 for(wc = start; wc < start + bufferLen; wc++)
1928 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1929 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1930 if(overWrite == true)
1932 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1933 if(overWriteCh == '\t') overWriteCh = ' ';
1938 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1939 if(flagTrailingSpace) surface.SetBackground(red);
1940 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1941 if(overWrite == true)
1949 surface.TextOpacity(opacity);
1950 surface.SetBackground(background);
1951 surface.SetForeground(foreground);
1958 if(wc < line.count && line.buffer[wc] == '\t')
1960 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1964 numSpaces += space.w;
1968 flagTrailingSpace = numSpaces && trailingSpace && style.syntax && start + bufferLen == line.count && line != this.line;
1969 if(flagTrailingSpace) surface.SetBackground(red);
1970 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, flagTrailingSpace, box);
1975 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1976 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1978 if(overWrite == true)
1985 surface.TextOpacity(opacity);
1986 surface.SetBackground(background);
1987 surface.SetForeground(foreground);
1990 if(style.freeCaret && selected)
1992 surface.SetBackground(selectionBackground);
1993 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1994 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1999 if(style.freeCaret && selected)
2001 surface.SetBackground(selectionBackground);
2002 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
2005 if(line.count && line.text[line.count - 1] == '\\')
2007 continuedSingleLineComment = inSingleLineComment;
2008 continuedString = inString;
2009 continuedQuotes = inQuotes;
2013 continuedSingleLineComment = false;
2014 continuedString = false;
2015 continuedQuotes = false;
2019 if(y > box.bottom) // >=clientSize.h)
2025 surface.TextOpacity(true);
2026 surface.SetForeground(black);
2027 surface.SetBackground(Color {255,255,85});
2028 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
2032 void FixScrollArea()
2034 if(style.hScroll || style.vScroll)
2036 int width = maxLength + XOFFSET;
2037 int height = lineCount * space.h;
2038 if(style.freeCaret && line)
2043 width = Max(line.length + (x - line.count) * space.w, maxLength + XOFFSET);
2047 if(selX > selLine.count)
2048 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength + XOFFSET);
2053 SetScrollLineStep(8, space.h);
2054 SetScrollArea(width, height, true);
2058 void ComputeLength(EditLine line)
2065 for(c = 0; c < line.count; )
2074 for(len = 0; c < line.count; c += numBytes)
2076 ch = line.buffer[c];
2077 numBytes = UTF8_NUM_BYTES(ch);
2079 if(ch == ' ' || ch == '\t')
2086 if(!len && ch == ' ')
2091 else if(!len && ch == '\t')
2093 w = (tabSize * space.w) - (x % (tabSize * space.w));
2097 FontExtent(display, font, line.buffer + start, len, &w, null);
2108 if(line.length > this.maxLength)
2110 this.maxLine = line;
2111 this.maxLength = line.length;
2113 if(style.autoSize) AutoSize();
2122 this.maxLine = null;
2124 for(line = lines.first; line; line = line.next)
2126 if(line.length > this.maxLength)
2128 this.maxLength = line.length;
2129 this.maxLine = line;
2133 if(style.autoSize) AutoSize();
2138 if(this.selY != this.y)
2140 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2144 void ComputeColumn()
2146 // Adjust new edit X position according to tabs
2147 int c, position = 0;
2150 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2152 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2154 position += this.tabSize - (position % this.tabSize);
2158 position += this.x - c;
2159 this.col = position;
2162 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2164 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2167 bool HasCommentOrEscape(EditLine line)
2169 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2174 for(c = line.count-1; c >= 0; c--)
2176 char ch = line.buffer[c];
2182 else //if(ch != ' ' && ch != '\t')
2189 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2191 EditLine line = l1, next;
2193 int oldCount1, oldCount2;
2194 int y, firstViewY, firstY, firstDropY, firstSelY;
2197 DelTextAction action = null;
2198 bool hadComment = false;
2202 hadComment = HasCommentOrEscape(line);
2204 if(y2 > y1 || c2 > c1)
2206 if(start < l1.count)
2208 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2212 oldCount1 = l1.count;
2214 while(c1 < oldCount1)
2216 byte ch = buffer[c1];
2217 if(UTF8_IS_FIRST(ch)) break;
2221 oldCount2 = l2.count;
2223 while(c2 < oldCount2)
2225 byte ch = buffer[c2];
2226 if(UTF8_IS_FIRST(ch)) break;
2231 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2236 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2237 string = new char[len];
2238 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2239 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2243 //oldCount1 = l1.count;
2244 //oldCount2 = l2.count;
2247 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2248 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2251 if(c2 > oldCount2) c2 = oldCount2;
2252 if(!(style.freeCaret))
2253 if(c1 > oldCount1) c1 = oldCount1;
2254 newLineCount = c1 + l2.count-c2;
2260 buffer = new char[line.size ? line.size : 1];
2262 buffer = new char[line.size];
2264 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2269 if(!line.AdjustBuffer(newLineCount))
2273 /*if(newLineCount > 4000 || newLineCount < 0)
2274 printf("Warning");*/
2276 line.count = newLineCount;
2278 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2283 action.addedSpaces = c1-oldCount1;
2285 if(action.addedSpaces > action.x1)
2291 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2292 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2294 line.buffer[line.count] = '\0';
2301 this.dropX -= c2-c1;
2302 this.dropX = Max(this.dropX,0);
2304 this.x = Max(this.x,0);
2311 firstViewY = this.viewY;
2313 firstDropY = this.dropY;
2314 firstSelY = this.selY;
2315 for(line = l1;line;line = next, y++)
2323 if(line == this.viewLine)
2325 if(this.viewLine.next)
2327 this.viewLine = this.viewLine.next;
2329 style.recomputeSyntax = true;
2333 this.viewLine = this.viewLine.prev;
2335 style.recomputeSyntax = true;
2338 else if(y < firstViewY)
2340 if(line == this.line)
2344 this.line = this.line.next;
2345 this.x = this.line.count;
2350 this.line = this.line.prev;
2351 this.x = this.line.count;
2358 if(line == this.dropLine)
2360 if(this.dropLine.next)
2362 this.dropLine = this.dropLine.next;
2363 this.dropX = this.dropLine.count;
2367 this.dropLine = this.dropLine.prev;
2368 this.dropX = this.dropLine.count;
2372 else if(y < firstDropY)
2374 if(line == this.selLine)
2376 if(this.selLine.next)
2378 this.selLine = this.selLine.next;
2379 this.selX = this.selLine.count;
2383 this.selLine = this.selLine.prev;
2384 this.selX = this.selLine.count;
2388 else if(y < firstSelY)
2392 if(line == l2) break;
2396 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2399 style.recomputeSyntax = true;
2404 bool DelSel(int * addedSpacesPtr)
2406 if(this.line != this.selLine || this.x != this.selX)
2408 if(this.selY < this.y)
2410 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2413 this.line = this.selLine;
2415 else if(this.selY > this.y)
2417 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2420 this.selLine = this.line;
2422 else if(this.selX < this.x)
2424 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2427 this.line = this.selLine;
2431 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2434 this.selLine = this.line;
2442 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2444 bool hadComment = false;
2445 // Add the line here
2446 EditLine line = this.line;
2448 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2449 if(!style.hScroll && created)
2451 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2456 // Lock if no place to display.
2459 else if(endX > this.x)
2460 max = Max(this.x, line.count);
2464 for(x = 0, c = 0; c < max+count; )
2469 if(c < Min(this.x, line.count))
2470 string = line.buffer + c;
2473 else if(c < endX + count)
2474 string = stringLine + c - endX;
2476 string = line.buffer + c - endX - count;
2480 w = (tabSize * space.w) - (x % (tabSize * space.w));
2484 numBytes = UTF8_NUM_BYTES(*string);
2485 FontExtent(display, this.font, string, numBytes, &w, null);
2489 if(x >= clientSize.w)
2494 c += numBytes; // - 1;
2500 int addedSpaces = 0;
2503 // Add blank spaces if EES_FREECARET
2504 if(this.x > line.count)
2514 for(c = 0; c<line.count; c++)
2516 if(this.line.buffer[c] == '\t')
2517 position += this.tabSize - (position % this.tabSize);
2521 wantedPosition = position + (this.x - line.count);
2523 // A tab is too much...
2524 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2525 addedSpaces = wantedPosition - position;
2530 position += this.tabSize - (position % this.tabSize);
2531 // Add as many tabs as needed
2532 addedTabs += (wantedPosition - position) / this.tabSize;
2533 position += (addedTabs-1) * this.tabSize;
2534 // Finish off with spaces
2535 addedSpaces = wantedPosition - position;
2539 addedSpaces = this.x - line.count;
2542 this.x = Min(this.x, line.count);
2544 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2550 hadComment = HasCommentOrEscape(line);
2552 // Adjust the size of the line
2553 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2557 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2560 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2561 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2564 *addedTabsPtr = addedTabs;
2565 FillBytes(line.buffer+line.count,'\t',addedTabs);
2567 if(addedTabs > 4000 || addedTabs < 0)
2570 line.count += addedTabs;
2576 FillBytes(line.buffer+line.count,' ',addedSpaces);
2578 if(addedSpaces > 4000 || addedSpaces < 0)
2581 line.count += addedSpaces;
2582 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2584 else if(addedSpacesPtr)
2585 *addedSpacesPtr = 0;
2587 if(count > 4000 || count < 0)
2590 line.count += count;
2591 this.x += count + addedTabs + addedSpaces;
2594 this.selLine = this.line;
2596 line.buffer[line.count] = '\0';
2598 ComputeLength(line);
2603 hasComment = HasCommentOrEscape(line);
2604 if(!undoBuffer.insideRedo)
2606 int backDontRecord = undoBuffer.dontRecord;
2607 undoBuffer.dontRecord = 0;
2608 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2609 undoBuffer.dontRecord = backDontRecord;
2611 if(style.syntax && (hadComment || hasComment || line != this.line))
2613 style.recomputeSyntax = true;
2621 void Emptyline(EditLine line, int y)
2624 DelCh(line, y, 0, line.next, y+1, 0, true);
2626 DelCh(line, y, 0, line, y, line.count, true);
2629 void GoToEnd(bool deselect)
2633 this.line = this.lines.last;
2634 if(this.y != this.lineCount-1)
2636 else if (this.x != this.line.count)
2637 DirtyLine(this.lineCount-1);
2638 this.y = this.lineCount-1;
2639 this.x = this.line.count;
2646 void GoToHome(bool deselect)
2650 this.line = this.lines.first;
2653 else if (this.x !=0)
2654 DirtyLine(this.lineCount-1);
2663 // Returns true if it needs scrolling
2664 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2670 bool needHScroll = false;
2677 line = this.viewLine ? (void *)this.viewLine.prev : null;
2682 line = (void *)this.lines.first;
2687 py = Min(py, clientSize.h);
2689 py = Min(py, this.lineCount);
2691 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2697 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2700 px = Min(px,clientSize.w+this.space.w);
2704 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2707 if(tline) *tline = line;
2710 // Prevent divide by 0 from non valid this.font
2712 return (y < this.viewY) || needHScroll;
2714 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2718 // Minimal Update Management Functions
2722 this.endY = clientSize.h-1;
2723 // ErrorLog("DirtyAll\n");
2726 void DirtyEnd(int y)
2728 if((y - this.viewY)*this.space.h < this.startY)
2729 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2730 this.endY = clientSize.h-1;
2731 //ErrorLog("DirtyEnd %d\n", y);
2734 void DirtyLine(int y)
2738 if((y - this.viewY)*this.space.h < this.startY)
2739 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2740 if((y - this.viewY+1)*this.space.h > this.endY)
2741 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2743 //ErrorLog("DirtyLine %d\n", y);
2750 if(style.recomputeSyntax)
2752 FigureStartSyntaxStates(lines.first, true);
2753 style.recomputeSyntax = false;
2756 if(this.startY > this.endY) return;
2757 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2763 box.right = clientSize.w-1;
2764 box.top = this.startY;
2765 box.bottom = this.endY;
2768 this.startY = clientSize.h;
2772 bool IsMouseOnSelection()
2774 bool mouseOnSelection = false;
2777 int minY = Min(this.selY, this.y);
2778 int maxY = Max(this.selY, this.y);
2779 int minX = Min(this.selX, this.x);
2780 int maxX = Max(this.selX, this.x);
2782 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2784 if(maxX != minX || maxY != minY)
2786 if(y > minY && y < maxY)
2787 mouseOnSelection = true;
2788 else if(y == minY && y == maxY)
2789 mouseOnSelection = (x < maxX && x >= minX);
2793 mouseOnSelection = (x >= this.selX);
2794 else if(y == this.y)
2795 mouseOnSelection = (x >= this.x);
2800 mouseOnSelection = (x < this.selX);
2801 else if(y == this.y)
2802 mouseOnSelection = (x < this.x);
2805 return mouseOnSelection;
2808 void UpdateCaretPosition(bool setCaret)
2812 if(mouseMove || (!overwrite && !style.noCaret))
2814 int max = this.mouseMove ? this.dropX : this.x;
2815 int y = this.mouseMove ? this.dropY : this.y;
2816 EditLine line = this.mouseMove ? this.dropLine : this.line;
2818 if(!(style.freeCaret))
2819 max = Min(max, line.count);
2821 if(FontExtent && display)
2823 for(c = 0; c < max; )
2832 for(len = 0; c < Min(max, line.count); c += numBytes)
2834 ch = line.buffer[c];
2835 numBytes = UTF8_NUM_BYTES(ch);
2837 if(ch == ' ' || ch == '\t')
2844 if(!len && ch == ' ')
2849 else if(!len && ch == '\t')
2851 w = (tabSize * space.w) - (x % (tabSize * space.w));
2855 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2867 caretY = y * this.space.h;
2868 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2873 // TOFIX: Mismatch between NotifyCaretMove() and NotifyDropped() / GoToPosition()
2874 NotifyCaretMove(master, this, y + 1, x + 1);
2880 void SelectionEnables()
2882 if((x != selX || y != selY) && !selection)
2886 itemEditCut.disabled = false;
2887 itemEditDelete.disabled = false;
2889 itemEditCopy.disabled = false;
2891 this.selection = true;
2893 else if((x == selX && y == selY) && selection)
2895 itemEditCut.disabled = true;
2896 itemEditCopy.disabled = true;
2897 itemEditDelete.disabled = true;
2899 this.selection = false;
2903 void SetSelectCursor()
2905 if(!inactive || !style.noSelect)
2908 cursor = guiApp.GetCursor(arrow);
2909 else if(this.mouseSelect)
2910 cursor = guiApp.GetCursor(iBeam);
2913 if(IsMouseOnSelection())
2914 cursor = guiApp.GetCursor(arrow);
2916 cursor = guiApp.GetCursor(iBeam);
2921 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2924 int x = px ? *px : 0;
2931 if(c < Min(max, line.count))
2935 for(len = 0; c < Min(max, line.count); c += numBytes)
2937 ch = line.buffer[c];
2938 numBytes = UTF8_NUM_BYTES(ch);
2940 if(ch == ' ' || ch == '\t')
2947 if(!len && ch == ' ')
2952 else if(!len && ch == '\t')
2954 w = (tabSize * space.w) - (x % (tabSize * space.w));
2958 FontExtent(display, font, line.buffer + start, len, &w, null);
2962 if(style.freeCaret && c < max)
2971 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2976 int a = start + len;
2979 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2983 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2986 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2990 return Min(this.maxLineSize - 1, start + len);
2996 void SetCursorToViewX()
2998 bool selecting = this.x != selX || y != selY;
3000 // Horizontal Adjustment
3002 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
3007 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
3019 UpdateCaretPosition(false);
3025 void SetCursorToViewY()
3028 EditLine oldLine = this.line;
3030 bool selecting = this.x != this.selX || this.y != this.selY;
3032 numLines = clientSize.h / this.space.h;
3034 // Vertical Adjustment
3035 if(this.viewY > this.y)
3037 this.y = this.viewY;
3038 this.line = this.viewLine;
3041 if(this.viewY + numLines <= this.y)
3045 this.y = this.viewY-1;
3046 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
3053 if(this.line != oldLine)
3055 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
3063 this.selLine = this.line;
3066 UpdateCaretPosition(false);
3073 bool SaveFile(char * fileName)
3075 File f = eFile_Open(fileName, FO_WRITE);
3079 eWindow_SetModified(false);
3080 eInstance_Delete(f);
3087 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
3091 if(!style.multiLine)
3093 x = (active && this.active && !style.readOnly) ? line.count : 0;
3096 SetViewToCursor(true);
3101 if(!active && modified)
3104 if(!NotifyModified(master, this))
3107 *goOnWithActivation = false;
3114 if(timer) timer.Stop();
3116 mouseSelect = false;
3124 int aw = maxLength + 12, ah = Max(lineCount, 1) * space.h + 2;
3125 int nw = minClientSize.w, nh = minClientSize.h, xw = maxClientSize.w, xh = maxClientSize.h;
3126 clientSize = { nw && aw < nw ? nw : xw && aw > xw ? xw : aw, nh && ah < nh ? nh : xh && ah > xh ? xh : ah };
3129 bool OnResizing(int *w, int *h)
3135 *w = space.h * 80 / 14;
3139 void OnResize(int w, int h)
3142 if(!hasHorzScroll && !hasVertScroll && viewLine)
3143 SetViewToCursor(true);
3145 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3147 SetViewToCursor(true);
3152 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3154 if(style.readOnly) return true;
3155 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3156 // Middle button already serves as a dragger as well.
3161 bool OnRightButtonDown(int x, int y, Modifiers mods)
3163 this.rightButtonDown = true;
3168 bool OnRightButtonUp(int x, int y, Modifiers mods)
3171 if(!parent.inactive && rightButtonDown)
3174 Menu contextMenu { };
3176 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3177 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3178 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3179 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3180 MenuDivider { contextMenu };
3181 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3183 popup = PopupMenu { master = this, menu = contextMenu,
3185 nonClient = true, interim = false, parent = parent,
3186 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3188 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3192 rightButtonDown = false;
3196 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3201 if(style.noSelect) return true;
3203 // Should we have a separate 'selectOnActivate' style?
3204 if(!mods.isActivate || (style.readOnly && style.multiLine))
3210 mouseX = mx - XOFFSET;
3213 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3215 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3221 else if(IsMouseOnSelection() && !mods.isActivate)
3231 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3233 if(mods.shift && !mods.isActivate)
3254 UpdateCaretPosition(true);
3255 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3256 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3260 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3264 mouseSelect = false;
3275 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3277 //PrintLn("MouseMove: ", x, ", ", y);
3283 mouseMove = IsMouseOnSelection();
3287 int size = SelSize();
3290 char * text = new char[size+1];
3294 GetSel(text, false);
3296 if(Max(selY, this.y) == dropY)
3300 if(this.dropX > this.selX)
3301 moveX = this.x - this.selX;
3305 if(this.dropX > this.x)
3306 moveX = this.selX - this.x;
3310 this.dropX -= moveX;
3311 this.selX = this.x = this.dropX;
3312 this.selY = this.y = this.dropY;
3313 this.selLine = this.line = this.dropLine;
3315 SetViewToCursor(true);
3320 byte * c = ((EditBox)this).multiLineContents, * c2;
3321 int l1 = c ? strlen(c) : 0, l2;
3324 c2 = ((EditBox)this).multiLineContents;
3325 l2 = c2 ? strlen(c2) : 0;
3326 if(l1 != l2 || (l1 && strcmp(c, c2)))
3356 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3358 //PrintLn("Dropped: ", x, ", ", y);
3360 NotifyDropped(master, this, x, y);
3364 // Return false because DataBoxes automatically set EditBox editor's clickThrough to true for MouseMove events
3365 // ( for tool tips -- see 95ee4962c4c7bc3fe0a04aa6a4f98cacada40884)
3369 bool OnMouseMove(int mx, int my, Modifiers mods)
3375 if(mods != -1 && mods.isSideEffect)
3380 if(style.noSelect) return true;
3381 if(wordSelect) return true;
3382 mouseX = mx - XOFFSET;
3385 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3387 if(this.mouseMove || this.mouseSelect)
3396 ((style.hScroll) || (style.vScroll)))
3403 DirtyLine(this.dropY);
3406 DirtyLine(this.dropY);
3407 this.dropLine = line;
3408 SetViewToCursor(true);
3410 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3413 else if(this.mouseSelect)
3415 DirtyLine(this.selY);
3422 SetViewToCursor(true);
3425 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3432 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3437 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3441 if(style.noSelect) return true;
3442 FindMouse(mx, my, &x, &y, &line, false);
3443 if(!NotifyDoubleClick(master, this, line, mods))
3450 for(c = x; c >= 0; c--)
3453 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3454 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3461 for(c = start; c<line.count; c += numBytes)
3463 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3464 if(!ch || !IS_ALUNDER(ch))
3474 this.line = this.selLine = line;
3475 this.wordSelect = (c != start);
3486 Menu fileMenu { menu, "File", F };
3487 saveDialog = fileDialog;
3488 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3489 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3491 if(style.autoSize) AutoSize();
3499 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3500 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3502 space.w = Max(space.w, 1);
3503 large.w = Max(large.w, 1);
3504 space.h = Max(space.h, 1);
3505 large.h = Max(large.h, 1);
3509 for(line = lines.first; line; line = line.next)
3510 ComputeLength(line);
3515 SetViewToCursor(true);
3523 bool OnLoadGraphics()
3525 FontExtent = Display::FontExtent;
3528 // UpdateCaretPosition(true);
3532 void OnUnloadGraphics()
3537 bool OnKeyHit(Key key, unichar ch)
3539 bool shift = (key.shift) ? true : false;
3541 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3543 if(!ch && !key.alt && !key.ctrl)
3545 key.code = (SmartKey)key.code;
3547 else if(!ch && key.alt)
3550 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3553 if(style.readOnly) break;
3554 if(style.stuckCaret) GoToEnd(true);
3555 if(!(style.freeCaret))
3557 this.x = Min(this.x, this.line.count);
3563 EditLine line = this.line;
3565 for(y = this.y; y>= 0; y--)
3567 c = (y == this.y) ? (this.x-1) : line.count-1;
3569 // Slow down when going on lines...
3570 if(y != this.y) break;
3574 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3575 if(IS_ALUNDER(line.buffer[c]))
3580 if(!IS_ALUNDER(line.buffer[c]))
3582 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3596 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3597 this.x = this.selX = Min(c+1, line.count);
3598 this.y = this.selY = y;
3599 this.line = this.selLine = line;
3600 SetViewToCursor(true);
3611 if(style.readOnly) break;
3612 if(style.stuckCaret) break;
3621 if(this.line != this.selLine || this.x != this.selX)
3624 SetViewToCursor(true);
3630 if(this.x < this.line.count)
3634 for(i = this.x; i < this.line.count; i++)
3636 if(!IS_ALUNDER(this.line.buffer[i]))
3640 for(; i < this.line.count; i++)
3642 //Delete trailing whitespace
3643 if(IS_ALUNDER(this.line.buffer[i]))
3646 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3647 SetViewToCursor(true);
3650 else if(this.line.next)
3652 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3653 SetViewToCursor(true);
3659 if(!(style.freeCaret))
3661 this.selX = this.x = Min(this.x, this.line.count);
3664 if(this.x < this.line.count)
3666 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3668 else if(this.line.next)
3670 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3672 SetViewToCursor(true);
3681 if(!key.alt && !key.ctrl)
3685 bool stuffAfter = false;
3689 if(style.stuckCaret) GoToEnd(true);
3690 if(style.readOnly) break;
3691 if(!(style.multiLine)) break;
3693 for(c = 0; c<this.line.count && c<this.x; c++)
3695 if(this.line.buffer[c] == '\t')
3696 position += this.tabSize - (position % this.tabSize);
3697 else if(this.line.buffer[c] == ' ')
3705 if(this.x < this.line.count)
3708 //If last character is a { indent one tab
3709 if(this.line.buffer[this.x - 1] == '{')
3711 //Except if the next non space character is a }
3712 bool indent = false;
3714 for(i = this.x; i < this.line.size; i++)
3715 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3717 if(this.line.buffer[i] != '}')
3722 position += this.tabSize;
3725 addString = new char[position + 2];
3726 addString[len++] = '\n';
3727 addString[len] = '\0';
3729 if(stuffAfter || !style.freeCaret)
3731 for(c = 0; c<position; )
3733 if(style.useTab && c + this.tabSize <= position)
3735 addString[len++] = '\t';
3740 addString[len++] = ' ';
3744 addString[len] = '\0';
3748 if(!stuffAfter && style.freeCaret)
3750 this.x = this.selX = position;
3754 SetViewToCursor(true);
3764 if(style.stuckCaret) break;
3765 if(!(style.freeCaret))
3767 this.x = Min(this.x, this.line.count);
3768 this.selX = Min(this.selX, this.selLine.count);
3771 if(!shift) SelDirty();
3774 bool foundAlpha = false;
3777 EditLine line, lastLine;
3780 for(line = this.line; (line && !found); line = line.prev, y--)
3785 if(this.x == 0 && line != this.line)
3794 if(line == this.line) start = this.x -1; else start = line.count-1;
3795 start = Min(start, line.count-1);
3797 for(c = start; c >= 0;)
3800 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3818 byte ch = line.buffer[c];
3819 if(UTF8_IS_FIRST(ch)) break;
3823 // No next word found,
3824 if(!found && ( this.x > 0 || (!line.count && this.x)))
3838 this.line = lastLine;
3849 byte * buffer = line.buffer;
3852 byte ch = buffer[x];
3853 if(UTF8_IS_FIRST(ch)) break;
3870 if(!shift) _Deselect();
3871 SetViewToCursor(true);
3877 if(style.stuckCaret) break;
3878 if(!(style.freeCaret))
3880 this.x = Min(this.x, this.line.count);
3881 this.selX = Min(this.selX, this.selLine.count);
3884 if(!shift) SelDirty();
3885 if(!shift && (this.x != this.selX || this.y != this.selY));
3888 bool onAChar = false;
3889 if(this.selX != this.x || this.selY != this.y)
3891 if(this.x<this.line.count)
3892 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3894 if(key.shift && onAChar &&
3895 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3897 bool foundAlpha = false;
3899 EditLine line, lastLine;
3901 int lastC, lastY, lastNumBytes;
3903 for(line = this.line; (line && !found); line = line.next, y++)
3905 int start = (line == this.line) ? this.x : 0;
3909 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3915 lastNumBytes = numBytes;
3925 if(!found && (c != this.x || line != this.line))
3938 this.x = lastC + lastNumBytes;
3940 this.line = lastLine;
3947 bool foundAlpha = false;
3952 for(line = this.line; (line && !found); line = line.next, y++)
3954 int start = (line == this.line) ? this.x : 0;
3958 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3974 // No next word found,
3975 if(!found && (c != this.x || line != this.line))
3979 this.x = line.count;
3991 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3995 byte * buffer = line.buffer;
3998 byte ch = buffer[x];
3999 if(UTF8_IS_FIRST(ch)) break;
4020 if(!shift) _Deselect();
4021 SetViewToCursor(true);
4028 if(!style.vScroll || hasVertScroll) break;
4034 if(style.stuckCaret) break;
4036 if(!shift) SelDirty();
4046 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4050 if(!shift) _Deselect();
4052 SetViewToCursor(false);
4055 if(caretY == this.y * space.h)
4061 if(!style.freeCaret)
4062 this.x = Min(this.x, line.count);
4072 int sx = 0, sy = this.y * space.h;
4073 char * text = line.text;
4074 int maxW = clientSize.w - sx;
4075 display.FontExtent(font, " ", 1, null, &th);
4079 int startPos = textPos;
4082 bool lineComplete = false;
4084 if(!style.wrap && caretY == MAXINT)
4087 //textPos = line.count;
4088 //lineComplete = true;
4091 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4095 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4098 len = (nextSpace - (text + textPos));
4100 len = line.count - textPos;
4102 if(textPos < line.count)
4104 display.FontExtent(font, text + textPos, len, &w, null);
4106 if(nextSpace) { w += space.w; len++; }
4108 if(style.wrap && x + width + w > maxW && x > 0)
4110 lineComplete = true;
4120 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4124 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4127 if(this.x > line.count)
4130 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4131 len = this.x - startPos;
4132 display.FontExtent(font, text + startPos, len, &x, null);
4136 if(!shift) _Deselect();
4138 SetViewToCursor(false);
4142 if(sy == caretY - th || textPos >= line.count)
4144 if(textPos >= line.count)
4146 int c = textPos - 1;
4147 while(c > 0 && text[c] == ' ') c--;
4151 this.x = line.count;
4154 if(!shift) _Deselect();
4156 SetViewToCursor(false);
4161 } while(textPos < line.count);
4164 if(!shift) _Deselect();
4166 SetViewToCursor(false);
4175 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4176 if(!shift) SelDirty();
4177 this.line = this.line.prev;
4183 if(!shift) _Deselect();
4187 SetViewToCursor(false);
4193 return style.multiLine ? false : true;
4197 if(!style.vScroll || hasVertScroll)
4204 if(style.stuckCaret) break;
4208 int sx = 0, sy = this.y * this.space.h;
4209 int maxW = clientSize.w - sx;
4210 char * text = line.buffer;
4212 if(!shift) SelDirty();
4218 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4228 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4232 if(!shift) _Deselect();
4234 SetViewToCursor(false);
4237 while(!textPos || (style.freeCaret || textPos<line.count))
4239 int startPos = textPos;
4242 bool lineComplete = false;
4243 if(!style.wrap && sy <= caretY)
4245 textPos = line.count;
4246 lineComplete = true;
4248 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4252 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4255 len = (nextSpace - (text + textPos));
4257 len = line.count - textPos;
4259 if(textPos < line.count)
4261 display.FontExtent(font, text + textPos, len, &w, &th);
4263 if(nextSpace) { w += space.w; len++; }
4264 if(style.wrap && x + width + w > maxW && x > 0)
4266 lineComplete = true;
4276 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4280 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4283 if(this.x > line.count)
4286 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4288 len = this.x - startPos;
4289 display.FontExtent(font, text + startPos, len, &x, null);
4292 if(!shift) _Deselect();
4295 SetViewToCursor(false);
4301 this.x = line.count;
4304 if(!shift) _Deselect();
4307 SetViewToCursor(false);
4310 else if(textPos >= line.count && line.next)
4316 sy = this.y * this.space.h;
4317 sx = 0; //textBlock.startX;
4323 sx = 0; //textBlock.startX;
4331 this.x = Min(this.x, line.count);
4332 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4334 if(!shift) _Deselect();
4337 if(this.selX != this.x || this.selY != this.y)
4340 SetViewToCursor(false);
4347 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4348 if(!shift) SelDirty();
4349 this.line = this.line.next;
4353 if(!shift) _Deselect();
4356 if(this.selX != this.x || this.selY != this.y)
4364 return style.multiLine ? false : true;
4367 if(style.stuckCaret) break;
4368 if(!style.multiLine && key.ctrl) break;
4369 if(!(style.freeCaret))
4370 this.selX = Min(this.selX, this.selLine.count);
4372 if(!shift) SelDirty();
4375 this.line = this.lines.first;
4376 if(this.y != 0 || this.x != 0)
4386 EditLine line = this.line;
4388 for(c=0; line.buffer[c]; c++)
4389 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4391 if(shift && (c != 0 || this.x))
4400 if(shift && this.x != 0)
4406 if(!shift) _Deselect();
4407 SetViewToCursor(true);
4413 if(style.stuckCaret) break;
4414 if(!style.multiLine && key.ctrl) break;
4415 if(!style.freeCaret)
4416 this.selX = Min(this.selX, this.selLine.count);
4418 if(!shift) SelDirty();
4423 else if(this.x != this.line.count)
4425 this.x = this.line.count;
4430 if(!shift) _Deselect();
4431 SetViewToCursor(true);
4436 if(style.tabKey && !key.ctrl)
4438 if(this.selY != this.y && style.tabSel)
4440 EditLine firstLine, lastLine;
4444 // Do multi line selection tabbing here
4445 if(this.selY < this.y)
4447 firstLine = this.selLine;
4448 lastLine = this.line;
4454 // Selecting going up
4455 firstLine = this.line;
4456 lastLine = this.selLine;
4463 for(line = firstLine; line; line = line.next, y++)
4465 if(line != lastLine || x)
4469 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4471 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4473 if(line.buffer[c] == '\t')
4478 else if(line.buffer[c] != ' ')
4483 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4486 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4487 char * string = new char[len];
4488 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4489 GetText(string, line, y, 0, line, y, lastC, false, false);
4492 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4493 if(!line.AdjustBuffer(line.count-lastC))
4499 if(line == lastLine) break;
4504 for(line = firstLine; line; line = line.next, y++)
4508 if(line != lastLine || x)
4512 if(line.count + 1 <= this.maxLineSize)
4514 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4516 if(!line.AdjustBuffer(line.count+1))
4519 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4521 AddCharAction action { ch = '\t', x = 0, y = y };
4525 memmove(line.buffer+1,line.buffer,line.size-1);
4527 line.buffer[0] = '\t';
4532 if(line.count + this.tabSize <= this.maxLineSize)
4535 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4536 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4538 if(!line.AdjustBuffer(line.count+this.tabSize))
4542 char * string = new char[this.tabSize + 1];
4543 memset(string, ' ', this.tabSize);
4544 string[this.tabSize] = '\0';
4545 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4548 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4549 line.count+=this.tabSize;
4550 for(c=0; c<this.tabSize; c++)
4551 line.buffer[c] = ' ';
4557 if(line == lastLine) break;
4560 ComputeLength(maxLine);
4572 char * addString = new char[this.tabSize + 1];
4574 if(!(style.freeCaret))
4576 this.x = Min(this.x, this.line.count);
4580 start = Min(this.x, this.selX);
4581 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4583 addString[len++] = ' ';
4591 SetViewToCursor(true);
4600 if(!(style.hScroll) || hasHorzScroll) break;
4601 if(this.viewX < this.maxLength)
4603 //this.viewX+=this.space.w*this.tabSize;
4605 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4612 if(!shift) _Deselect();
4624 if(!(style.hScroll) || hasHorzScroll) break;
4627 //this.viewX-=this.space.w*this.tabSize;
4628 //this.viewX = Max(this.viewX,0);
4630 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4631 // SetCursorToView();
4638 if(!shift) _Deselect();
4653 if(!(style.readOnly))
4659 this.overwrite ^= 1;
4660 UpdateCaretPosition(true);
4665 NotifyOvrToggle(master, this, this.overwrite);
4676 if(style.noSelect) break;
4679 //Save current view position
4680 int tempX = this.viewX;
4681 int tempY = this.viewY;
4685 this.selLine = this.lines.first;
4686 this.y = this.lineCount-1;
4687 this.line = this.lines.last;
4688 this.x = this.line.count;
4691 SetViewToCursor(true);
4693 //Restore previous view position
4694 SetScrollPosition(tempX, tempY * this.space.h);
4700 // TOCHECK: Was there any good reason why we weren't returning false here?
4701 return false; // break;
4706 if(!(style.readOnly))
4714 if(style.readOnly) break;
4720 if(style.readOnly) break;
4725 if(style.readOnly) break;
4729 if(style.readOnly) break;
4733 if(style.readOnly) break;
4734 if(key.shift && key.code == rightBracket)
4736 //Only indent back if you are exactly at one tab.
4738 bool whitespace = true;
4744 EditLine line = this.line;
4746 //Only remove one tab if there is nothing else on the line.
4747 for(i = 0; i < this.line.count; i++)
4749 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4763 for(pos = line.count - 1; pos >= 0; pos--)
4765 char c = line.buffer[pos];
4769 indentwidth += this.tabSize;
4771 //Counting backwards, so when you find a character, indentation is reset
4781 //Place the } to get an undo:
4784 this.x = this.line.count;
4788 putsize = indentwidth;
4790 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4792 newline = new char[putsize+2];
4793 newline[putsize] = '}';
4794 newline[putsize+1] = '\0';
4798 for(; i < indentwidth / this.tabSize; i++)
4800 for(;i < putsize; i++)
4809 else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4821 void OnHScroll(ScrollBarAction action, int position, Key key)
4824 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4826 this.viewX = position;
4827 if(action != setRange)
4829 if(!this.mouseMove && style.cursorFollowsView)
4836 void OnVScroll(ScrollBarAction action, int position, Key key)
4838 int oldViewY = this.viewY;
4841 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4843 position /= this.space.h;
4845 if(position < this.viewY)
4847 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4848 style.recomputeSyntax = true;
4850 else if(position > this.viewY)
4852 EditLine oldViewLine = viewLine;
4853 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4854 FigureStartSyntaxStates(oldViewLine, false);
4857 if(action != setRange)
4859 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4862 if(this.x != this.selX || this.y != this.selY)
4866 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4869 int numLines = clientSize.h / this.space.h;
4871 if(Abs(this.viewY - oldViewY) < numLines)
4874 if(this.viewY > oldViewY)
4876 for(y = oldViewY; y <this.viewY; y++)
4877 DirtyLine(y + numLines);
4881 for(y = this.viewY; y <oldViewY; y++)
4882 DirtyLine(y + numLines);
4887 // Fix dirt of stuff before first line...
4888 if(this.viewY - oldViewY > 0)
4890 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4897 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4902 ReplaceTextAction replaceAction = null;
4903 AddCharAction addCharAction = null;
4904 int addedSpaces = 0, addedTabs = 0;
4906 if(ch == '\r') return true;
4907 if(style.stuckCaret /*|EES_READONLY)*/ )
4910 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4912 if(!undoBuffer.dontRecord)
4914 if(selX != x || selY != y)
4919 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4920 oldString = new char[len];
4921 UTF32toUTF8Len(&ch, 1, buffer, 4);
4922 newString = CopyString(buffer);
4923 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4925 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4926 if(selY < y || (selY == y && selX < x))
4928 replaceAction.x1 = selX;
4929 replaceAction.y1 = selY;
4930 replaceAction.x2 = x;
4931 replaceAction.y2 = y;
4935 replaceAction.x1 = x;
4936 replaceAction.y1 = y;
4937 replaceAction.x2 = selX;
4938 replaceAction.y2 = selY;
4940 Record(replaceAction);
4941 undoBuffer.dontRecord++;
4945 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4946 Record(addCharAction);
4952 DelSel(&addedSpaces);
4953 if(this.lineCount+1 > this.maxLines)
4956 Emptyline(this.lines.first,0);
4960 if(!(style.autoSize && (!maxClientSize.h || maxClientSize.h > clientSize.h + this.space.h)) && !(style.vScroll))
4962 // Make sure it fits, but we need a default line is this.font is too big for window
4963 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4966 if((this.y >= 0) && this.y < this.viewY)
4971 line = EditLine { };
4974 lines.Insert(this.line, line);
4975 line.editBox = this;
4981 // If we're displacing the lines from a current line ...
4984 if(this.line.buffer)
4987 if(this.line.count < endX) endX = this.line.count;
4988 length = this.line.count - endX;
4991 if(!line.AdjustBuffer(length))
4995 if(this.line.buffer)
4997 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4999 if(endX > 4000 || endX < 0)
5002 this.line.count = endX;
5003 this.line.buffer[this.line.count] = '\0';
5004 this.line.AdjustBuffer(this.line.count);
5005 ComputeLength(this.line);
5009 BufferLocation before = { this.line, this.y, this.x }, after;
5014 line.count = length;
5017 if(length > 4000 || length < 0)
5020 ComputeLength(this.line);
5025 line.buffer[line.count] = '\0';
5028 after.line = this.line, after.y = this.y, after.x = this.x;
5030 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
5036 int count = UTF32toUTF8Len(&ch, 1, string, 5);
5037 DelSel(&addedSpaces);
5038 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
5039 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
5040 if(addedTabsPtr) *addedTabsPtr = addedTabs;
5044 this.selLine = this.line;
5048 replaceAction.x3 = x;
5049 replaceAction.y3 = y;
5050 replaceAction.addedSpaces = addedSpaces;
5051 replaceAction.addedTabs = addedTabs;
5052 undoBuffer.dontRecord--;
5056 addCharAction.x -= addedTabs * (tabSize-1);
5057 addCharAction.addedSpaces = addedSpaces;
5058 addCharAction.addedTabs = addedTabs;
5065 /****************************************************************************
5067 ****************************************************************************/
5070 bool AddCh(unichar ch)
5072 return _AddCh(ch, null, null);
5077 this.modified = true;
5078 NotifyUpdate(master, this);
5079 modifiedDocument = true;
5082 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5085 DelCh(line1, y1, x1, line2, y2, x2, false);
5086 SetViewToCursor(true);
5094 itemEditUndo.disabled = undoBuffer.curAction == 0;
5095 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5096 if(savedAction == undoBuffer.curAction)
5098 modifiedDocument = false;
5100 NotifyUnsetModified(master, this);
5107 itemEditUndo.disabled = undoBuffer.curAction == 0;
5108 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5109 if(savedAction == undoBuffer.curAction)
5111 modifiedDocument = false;
5113 NotifyUnsetModified(master, this);
5117 void Record(UndoAction action)
5119 if(!undoBuffer.dontRecord)
5121 undoBuffer.Record(action);
5122 itemEditUndo.disabled = undoBuffer.curAction == 0;
5123 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5130 this.lines.first, 0,0,
5131 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5134 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5139 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5140 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5141 this.y = line2 ? y2 : (this.lineCount-1);
5142 this.line = line2 ? (EditLine)line2 : this.lines.last;
5145 SetViewToCursor(true);
5149 // TODO: Fix this vs modifiedDocument window property
5150 void SetModified(bool flag)
5154 this.modified = false;
5155 if(flag && !NotifyModified(master, this))
5156 this.modified = true;
5161 bool AddS(char * string)
5168 int addedSpaces = 0, addedTabs = 0;
5169 AddTextAction action = null;
5170 ReplaceTextAction replaceAction = null;
5172 this.pasteOperation = true;
5174 if(style.stuckCaret /*|EES_READONLY)*/ )
5177 if(!undoBuffer.dontRecord)
5179 char * placeString = CopyString(string);
5180 if(!style.multiLine)
5184 for(i = 0; (ch = placeString[i]); i++)
5187 placeString[i] = '\0';
5188 placeString = renew placeString byte[i+1];
5193 if(selX != x || selY != y)
5197 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5198 oldString = new char[len];
5199 newString = placeString;
5200 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5202 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5203 if(selY < y || (selY == y && selX < x))
5205 replaceAction.x1 = selX;
5206 replaceAction.y1 = selY;
5207 replaceAction.x2 = x;
5208 replaceAction.y2 = y;
5212 replaceAction.x1 = x;
5213 replaceAction.y1 = y;
5214 replaceAction.x2 = selX;
5215 replaceAction.y2 = selY;
5217 Record(replaceAction);
5221 if(string[0] == '\n')
5222 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5224 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5232 undoBuffer.dontRecord++;
5233 DelSel(&addedSpaces);
5237 for(c = 0; string[c]; c++)
5239 if(string[c] == '\n' || string[c] == '\r')
5241 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5246 if(string[c] == '\n')
5255 // Reset for next line
5259 if(string[c] == '\r' && *line == '\n')
5271 // Why was this here?
5274 // Add the line here
5276 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5282 undoBuffer.dontRecord--;
5287 action.addedSpaces = addedSpaces;
5288 action.addedTabs = addedTabs;
5290 else if(replaceAction)
5292 replaceAction.y3 = y;
5293 replaceAction.x3 = x;
5294 replaceAction.addedSpaces = addedSpaces;
5295 replaceAction.addedTabs = addedTabs;
5298 UpdateCaretPosition(true);
5300 this.pasteOperation = false;
5313 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5314 SetViewToCursor(true);
5320 void PutCh(unichar ch)
5324 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5325 //if((ch >= 32) || ch == '\n')
5327 int addedSpaces = 0, addedTabs = 0;
5328 ReplaceTextAction replaceAction = null;
5329 AddCharAction addCharAction = null;
5332 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5334 if(this.overwrite && selX == x && selY == y && this.x < this.line.count)
5339 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5340 oldString = new char[len];
5341 UTF32toUTF8Len(&ch, 1, buffer, 4);
5342 newString = CopyString(buffer);
5343 GetText(oldString, line, y, x, line, y, x+1, false, false);
5344 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5345 replaceAction.x1 = x;
5346 replaceAction.y1 = y;
5347 replaceAction.x2 = x+1;
5348 replaceAction.y2 = y;
5349 Record(replaceAction);
5351 undoBuffer.dontRecord++;
5352 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5353 undoBuffer.dontRecord--;
5355 else if(!undoBuffer.dontRecord)
5357 if(selX != x || selY != y)
5362 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5363 oldString = new char[len];
5364 UTF32toUTF8Len(&ch, 1, buffer, 4);
5365 newString = CopyString(buffer);
5366 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5367 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5368 if(selY < y || (selY == y && selX < x))
5370 replaceAction.x1 = selX;
5371 replaceAction.y1 = selY;
5372 replaceAction.x2 = x;
5373 replaceAction.y2 = y;
5377 replaceAction.x1 = x;
5378 replaceAction.y1 = y;
5379 replaceAction.x2 = selX;
5380 replaceAction.y2 = selY;
5382 Record(replaceAction);
5386 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5387 Record(addCharAction);
5390 undoBuffer.dontRecord++;
5391 result = _AddCh(ch, &addedSpaces, &addedTabs);
5394 replaceAction.x3 = x;
5395 replaceAction.y3 = y;
5396 replaceAction.addedSpaces = addedSpaces;
5397 replaceAction.addedTabs = addedTabs;
5401 addCharAction.x -= addedTabs * (tabSize-1);
5402 addCharAction.addedSpaces = addedSpaces;
5403 addCharAction.addedTabs = addedTabs;
5405 undoBuffer.dontRecord--;
5409 if(result) SetViewToCursor(true);
5413 void PutS(char * string)
5418 SetViewToCursor(true);
5423 void Printf(char * format, ...)
5427 char temp[MAX_F_STRING];
5429 va_start(args, format);
5430 vsnprintf(temp, sizeof(temp), format, args);
5431 temp[sizeof(temp)-1] = 0;
5437 void SetContents(char * format, ...)
5441 undoBuffer.dontRecord++;
5443 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5446 char temp[MAX_F_STRING];
5448 va_start(args, format);
5449 vsnprintf(temp, sizeof(temp), format, args);
5450 temp[sizeof(temp)-1] = 0;
5457 undoBuffer.dontRecord--;
5467 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5470 else if(this.line.prev)
5472 EditLine line = this.line.prev;
5476 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5484 this.selLine = this.line;
5489 SetViewToCursor(true);
5494 Emptyline(this.line,this.y);
5495 this.selX = this.x = 0;
5498 this.selLine = this.line;
5500 SetViewToCursor(true);
5510 SetViewToCursor(true);
5518 SetViewToCursor(true);
5522 bool GoToLineNum(int lineNum)
5527 EditLine line = this.lines.first;
5528 for(c = 0; c < lineNum && line; c++, line = line.next);
5538 SetViewToCursor(true);
5545 // NOTE: Mismatch with NotifyCaretMove() for x/y + 1
5546 bool GoToPosition(EditLine line, int y, int x)
5558 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5572 SetViewToCursor(true);
5579 void SetViewToCursor(bool setCaret)
5589 bool dontScroll = false;
5595 selected = selX != this.x || selY != y;
5602 checkLine = dropLine;
5608 checkLine = this.line;
5613 numLines = clientSize.h / space.h;
5615 // This is broken. The EditBox now doesn't do anything different when adding to it,
5616 // regardless of the previous scrolling position. It should be read and then set again
5617 // if one wishes to preserve it.
5618 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5619 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5623 // Horizontal Adjustment
5624 if(!dontScroll && checkLine)
5628 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5631 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5637 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5638 viewX = x - clientSize.w+space.w;
5639 if(x < this.viewX + clientSize.w/2 - space.w)
5640 viewX = Max(0, x - clientSize.w/2 + space.w);
5648 // Vertical Adjustment
5649 if(viewY > checkY) viewY = checkY;
5650 if(viewY + numLines <= checkY)
5652 if(clientSize.h >= space.h)
5654 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5664 for(;dropLine && dropLine.prev && dropY >= numLines;)
5666 dropLine = dropLine.prev;
5670 for(;this.line && this.line.prev && this.y >= numLines;)
5672 this.line = this.line.prev;
5677 SetScrollPosition(viewX, viewY * this.space.h);
5679 UpdateCaretPosition(setCaret);
5685 selLine = this.line;
5695 void CenterOnCursor()
5697 int numLines = clientSize.h / this.space.h;
5698 int y = this.y - numLines / 2;
5700 bool figureSyntax = false;
5701 EditLine oldViewLine = viewLine;
5702 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5707 for(;y < this.viewY; y++)
5709 this.viewLine = this.viewLine.prev;
5710 style.recomputeSyntax = true;
5712 for(;y > this.viewY; y--)
5714 this.viewLine = this.viewLine.next;
5715 figureSyntax = true;
5718 FigureStartSyntaxStates(oldViewLine, false);
5722 SetScrollPosition(this.viewX, viewY * this.space.h);
5723 UpdateCaretPosition(true);
5727 void SetCursorToView()
5738 numLines = clientSize.h / this.space.h;
5742 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5743 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5747 EditLine oldLine = this.line;
5748 bool lastOne = false;
5749 EditLine oldViewLine = this.viewLine;
5750 bool figureSyntax = false;
5752 if(this.y >= this.lineCount-1) return;
5754 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5756 if(this.viewY + numLines < this.lines.count)
5758 this.viewLine = this.viewLine.next;
5760 figureSyntax = true;
5762 else if(c && !lastOne)
5771 FigureStartSyntaxStates(oldViewLine, false);
5772 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5775 SetViewToCursor(false);
5784 if(this.y == 0) return;
5786 numLines = clientSize.h / this.space.h;
5790 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5791 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5795 EditLine oldLine = this.line;
5797 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5802 if(this.viewLine.prev)
5804 this.viewLine = this.viewLine.prev;
5806 style.recomputeSyntax = true;
5810 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5813 SetViewToCursor(false);
5820 if(this.viewLine.prev)
5823 // this.viewLine = this.viewLine.prev;
5826 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5833 if(this.viewLine.next)
5836 // this.viewLine = this.viewLine.next;
5839 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5846 EditLine l1, l2, line;
5847 int x1, x2, nx1, nx2;
5852 if(!this.selLine) return 0;
5853 if(this.selLine == this.line && this.selX == this.x) return 0;
5854 if(this.selY < this.y)
5861 else if(this.selY > this.y)
5868 else if(this.selX < this.x)
5870 l1 = l2 = this.line;
5876 l1 = l2 = this.line;
5880 nx1 = Min(x1,l1.count);
5881 nx2 = Min(x2,l2.count);
5883 // Find Number of Bytes Needed
5885 for(line = l1; line; line = line.next)
5887 if(line == l1) start = nx1; else start = 0;
5888 if(line == l2) end = nx2; else end = line.count;
5890 if(style.freeCaret && line == l2)
5893 count = Max(x2-Max(x1,l1.count),0);
5895 count = Max(x2-l2.count,0);
5899 if(line == l2) break;
5900 // Add Carriage Return / line Feed
5907 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5911 if(!reorder || this.selY < this.y)
5913 if(l1) *l1 = this.selLine;
5914 if(y1) *y1 = this.selY;
5915 if(x1) *x1 = this.selX;
5916 if(l2) *l2 = this.line;
5917 if(y2) *y2 = this.y;
5918 if(x2) *x2 = this.x;
5920 else if(this.selY > this.y)
5922 if(l1) *l1 = this.line;
5923 if(y1) *y1 = this.y;
5924 if(x1) *x1 = this.x;
5925 if(l2) *l2 = this.selLine;
5926 if(y2) *y2 = this.selY;
5927 if(x2) *x2 = this.selX;
5929 else if(this.selX < this.x)
5931 if(l1) *l1 = this.line;
5932 if(y1) *y1 = this.selY;
5933 if(x1) *x1 = this.selX;
5934 if(l2) *l2 = this.line;
5935 if(y2) *y2 = this.y;
5936 if(x2) *x2 = this.x;
5940 if(l1) *l1 = this.line;
5941 if(y1) *y1 = this.y;
5942 if(x1) *x1 = this.x;
5943 if(l2) *l2 = this.line;
5944 if(y2) *y2 = this.selY;
5945 if(x2) *x2 = this.selX;
5950 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5952 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5954 this.selLine = (EditLine)l1;
5957 this.line = (EditLine)l2;
5961 SetViewToCursor(true);
5965 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5967 EditLine l1, l2, line;
5968 int x1, x2, nx1, nx2;
5999 nx1 = Min(x1,l1.count);
6000 nx2 = Min(x2,l2.count);
6003 for(line = l1; line; line = line.next)
6005 if(line == l1) start = nx1; else start = 0;
6006 if(line == l2) end = nx2; else end = line.count;
6009 CopyBytes(text, line.buffer + start, end - start);
6012 numChars += end-start;
6014 if(style.freeCaret && line == l2 && addSpaces)
6017 count = Max(x2-Max(x1,l1.count),0);
6019 count = Max(x2-l2.count,0);
6022 FillBytes(text,' ',count);
6028 if(line == l2) break;
6040 // '\0' terminate Terminate
6047 void GetSel(char * text, bool addCr)
6049 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
6052 private void _Deselect() // This assumes marking lines as dirty is handled by the caller
6070 int size = SelSize();
6073 // Try to allocate memory
6074 ClipBoard clipBoard { };
6075 if(clipBoard.Allocate(size+1))
6077 GetSel(clipBoard.memory, true);
6090 ClipBoard clipBoard { };
6091 if(clipBoard.Load())
6092 PutS(clipBoard.memory);
6104 SetViewToCursor(true);
6110 void DeleteSelection()
6115 SetViewToCursor(true);
6121 void Save(File f, bool cr)
6124 savedAction = undoBuffer.curAction;
6126 for(line = this.lines.first; line; line = line.next)
6128 f.Write(line.buffer, line.count, 1);
6131 if(cr) f.Putc('\r');
6137 #define BUFFER_SIZE 16384
6140 undoBuffer.dontRecord++;
6143 char buffer[BUFFER_SIZE];
6147 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6148 buffer[count] = '\0';
6154 undoBuffer.dontRecord--;
6155 undoBuffer.count = 0;
6156 undoBuffer.curAction = 0;
6157 itemEditUndo.disabled = undoBuffer.curAction == 0;
6158 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6161 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6165 bool firstPass = true;
6166 EditBoxFindResult result = found;
6168 if(!this.line) return notFound;
6171 for(line = this.line;;)
6179 line = this.lines.first;
6185 line = this.lines.last;
6186 num = this.lineCount - 1;
6192 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6194 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6198 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6201 if(line == this.line && !firstPass) break;
6219 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6223 int searchLen = strlen(text);
6224 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6226 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6227 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6229 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6236 bool OnKeyDown(Key key, unichar ch)
6239 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6241 if(!NotifyKeyDown(master, this, key, ch))
6251 this.mouseMove = false;
6252 OnLeftButtonUp(0,0,0);
6253 SetViewToCursor(true);
6263 public class EditBoxStream : File
6266 BufferLocation start, sel;
6273 EditBox editBox = this.editBox;
6275 editBox.x = start.x;
6276 editBox.y = start.y;
6277 editBox.line = start.line;
6279 editBox.selX = sel.x;
6280 editBox.selY = sel.y;
6281 editBox.selLine = sel.line;
6283 editBox.SetViewToCursor(true);
6284 //editBox.ComputeColumn();
6288 property EditBox editBox
6296 start.line = value.line;
6300 sel.line = value.selLine;
6303 value.GoToHome(true);
6306 get { return editBox; }
6309 uint Read(byte * buffer, uint size, uint count)
6312 EditBox editBox = this.editBox;
6313 EditLine line = editBox.line;
6319 for(;read < count && line; line = (*&line.next))
6321 int numBytes = Min(count - read, (*&line.count) - x);
6324 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6329 for(;read < count && x < (*&line.count);)
6331 buffer[read++] = (*&line.buffer)[x++];
6338 buffer[read++] = '\n';
6343 if(x == (*&line.count) && (*&line.next))
6352 editBox.line = editBox.selLine = line;
6353 editBox.x = editBox.selX = x;
6354 editBox.y = editBox.selY = y;
6359 bool Seek(int pos, FileSeekMode mode)
6362 EditBox editBox = this.editBox;
6363 EditLine line = editBox.line;
6365 if(mode == FileSeekMode::start)
6367 pos = pos - this.pos;
6377 for(;read < pos && line; line = (*&line.next))
6379 int numBytes = Min(pos - read, (*&line.count) - x);
6382 read += numBytes; x += numBytes;
6385 /*for(;read < pos && x < (*&line.count);)
6413 for(;read < pos && line; line = (*&line.prev))
6415 int numBytes = Min(pos - read, x);
6421 /*for(;read < pos && x > 0;)
6431 x = (*&(*&line.prev).count);
6447 editBox.line = editBox.selLine = line;
6448 editBox.x = editBox.selX = x;
6449 editBox.y = editBox.selY = y;
6454 bool Puts(char * string)
6456 EditBox editBox = this.editBox;
6457 BufferLocation start { editBox.line, editBox.y, editBox.x };
6461 editBox.AddS(string);
6463 pos.line = editBox.line;
6467 this.start.AdjustAdd(start, pos);
6468 sel.AdjustAdd(start, pos);
6473 // NOTE: BYTE, NOT UNICODE CHARACTER!
6476 EditBox editBox = this.editBox;
6477 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6482 utf8Bytes[numBytes++] = ch;
6483 utf8Bytes[numBytes] = 0;
6484 if(UTF8Validate(utf8Bytes))
6486 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6488 pos.line = editBox.line;
6491 this.start.AdjustAdd(start, pos);
6492 sel.AdjustAdd(start, pos);
6499 bool Getc(char * ch)
6501 return Read(ch, 1, 1) ? true : false;
6504 void DeleteBytes(uint count)
6506 EditBox editBox = this.editBox;
6509 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6510 BufferLocation end = pos;
6515 for(;c < count && end.line && end.x < end.line.count;)
6520 if(c < count && end.line && end.line.next)
6522 end.line = end.line.next;
6531 start.AdjustDelete(pos, end);
6532 sel.AdjustDelete(pos, end);
6534 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);