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;
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
141 void ** array = (void **)((ArrayImpl)this).array;
142 if(type.type == normalClass || type.type == noHeadClass)
144 for(c = 0; c<size; c++)
145 ((void (*)(void *, void *))(void *)type._vTbl[__ecereVMethodID_class_OnFree])(type, array[c]);
147 // TODO: Call OnFree for structClass
148 delete ((ArrayImpl)this).array;
157 if(((ArrayImpl)this).array)
161 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
164 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
173 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
178 public class UndoAction : struct
181 subclass(UndoAction) type;
182 virtual void Undo(void * data) { type.Undo(this, data); }
183 virtual void Redo(void * data) { type.Redo(this, data); }
185 virtual void Print(void * data) { type.Print(this, data); }
189 if(((Class)type).Destructor)
190 ((void (*)(void *))((Class)type).Destructor)(this);
194 public class UndoBuffer
196 Array<UndoAction> actions { size = 8 };
216 UndoAction action = actions[--curAction];
218 /*Print("Undoing: ");
219 action.Print(data);*/
230 if(curAction < count)
232 UndoAction action = actions[curAction];
235 /*Print("Redoing: ");
236 action.Print(data);*/
244 void Record(UndoAction action)
246 if(!dontRecord && !insideRedo)
248 if(curAction < count)
251 for(c = curAction; c < count; c++)
257 if(count >= actions.size)
258 actions.size += actions.size / 2;
261 /*Print("Recording: ");
262 action.Print(data);*/
264 actions[count++] = action;
267 if(actions.size > count + count / 2 && count + count / 2 >= 8)
268 actions.size = count + count / 2;
275 static class AddCharAction : UndoAction
279 int addedSpaces, addedTabs;
280 type = class(AddCharAction);
282 void Undo(EditBox editBox)
284 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
286 if(addedTabs || addedSpaces)
287 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
288 editBox.UpdateDirty();
291 void Redo(EditBox editBox)
293 editBox.GoToPosition(null, y, x);
295 editBox.UpdateDirty();
298 void Print(EditBox editBox)
300 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
305 static class AddTextAction : UndoAction
309 int addedSpaces, addedTabs;
310 type = class(AddTextAction);
313 void Print(EditBox editBox)
315 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
323 void Undo(EditBox editBox)
328 editBox.GoToPosition(null, y1, x1);
330 l2 = editBox.lines.first;
331 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
333 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
334 if(addedTabs || addedSpaces)
335 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
337 editBox.SetViewToCursor(true);
341 void Redo(EditBox editBox)
343 editBox.GoToPosition(null, y1, x1);
344 editBox.PutS(string);
348 static class DelTextAction : UndoAction
354 type = class(DelTextAction);
357 void Print(EditBox editBox)
359 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
362 void Undo(EditBox editBox)
364 editBox.GoToPosition(null, y1, x1);
365 editBox.PutS(string);
369 editBox.GoToPosition(null, y1, x1);
372 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
373 //editBox.SetViewToCursor(true);
376 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
382 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
383 //editBox.SetViewToCursor(true);
386 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
390 void Redo(EditBox editBox)
395 editBox.GoToPosition(null, y1, x1);
398 l2 = editBox.lines.first;
399 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
401 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
402 editBox.SetViewToCursor(true);
412 static class ReplaceTextAction : UndoAction
414 int y1, x1, y2, x2, y3, x3;
418 int addedSpaces, addedTabs;
420 type = class(ReplaceTextAction);
423 void Print(EditBox editBox)
425 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
428 void Undo(EditBox editBox)
433 editBox.GoToPosition(null, y1, x1);
435 l3 = editBox.lines.first;
436 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
438 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
440 editBox.PutS(oldString);
441 if(addedSpaces || addedTabs)
442 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
444 //editBox.PutS(oldString);
447 editBox.GoToPosition(null, y1, x1);
450 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
456 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
460 void Redo(EditBox editBox)
465 editBox.GoToPosition(null, y1, x1);
467 l2 = editBox.lines.first;
468 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
470 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
472 editBox.PutS(newString);
482 static class MoveTextAction : UndoAction
484 int fy1, fx1, fy2, fx2;
486 type = class(MoveTextAction);
488 void Undo(EditBox editBox)
493 void Redo(EditBox editBox)
499 public class EditLine : struct
511 // Only works for single line edit for now...
512 EditBox editBox = this.editBox;
516 get { return this ? buffer : null; }
518 property EditLine prev { get { return this ? prev : null; } };
519 property EditLine next { get { return this ? next : null; } };
520 property int count { get { return this ? count : 0; } };
527 // This makes sure the buffer always contains at least count characters
528 // Keeps a count/2 pad for avoiding always reallocating memory.
529 bool AdjustBuffer(int count)
537 newSize = (count + (count >> 1));
539 // Shrink down the buffer
542 buffer = new char[newSize];
543 if(!buffer) return false;
547 CopyBytes(buffer, this.buffer, count);
550 this.buffer = buffer;
554 // Increase the buffer
555 else if(size < count)
557 buffer = new char[newSize];
558 if(!buffer) return false;
562 CopyBytes(buffer, this.buffer, this.count + 1); // size);
565 this.buffer = buffer;
574 public struct BufferLocation
579 void AdjustDelete(BufferLocation start, BufferLocation end)
581 // Location is before, nothing to do
582 if(y < start.y || (y == start.y && x < start.x))
584 // Location is inside deleted bytes, point to the start
585 if((y >= start.y && (y > start.y || x >= start.x)) &&
586 (y >= end.y && (y > end.y || x >= end.x)))
591 // Location is on another line
593 y -= end.y - start.y;
594 // Location is the last touched line
601 //if(start.line == end.line)
602 x -= end.x - start.x;
611 // Assuming no carriage return before first character ???? fixed?
612 void AdjustAdd(BufferLocation start, BufferLocation end)
614 int numLines = end.y - start.y;
621 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
624 for(c = 0, line = start.line; c<numLines; c++)
627 //x += numLines ? end.x : (end.x - start.x);
628 x += end.x - start.x;
635 public enum EditBoxFindResult { notFound, found, wrapped };
637 static char * keyWords1[] =
640 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
641 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
643 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
644 "__attribute__", "__stdcall", "_stdcall",
645 "__declspec", "goto",
646 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
647 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
650 "class", "private", "public",
652 "delete", "new", "new0", "renew", "renew0", "define",
655 "dllexport", "dllimport", "stdcall",
656 "subclass", "__on_register_module", "namespace", "using",
657 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
658 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
659 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
662 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64", "uintptr", "intptr", "intsize", "uintsize",
665 "this", "true", "false", "null", "value",
674 static char * keyWords2[] =
676 "defined", "warning", null
679 static char ** keyWords[] = { keyWords1, keyWords2 };
680 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
681 //static int * keyLen[NUM_KEYWORD_GROUPS];
682 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
684 static char searchString[1025], replaceString[1025];
685 static bool matchCase = false, wholeWord = false, searchUp = false;
687 static GoToDialog goToDialog
689 autoCreate = false, isModal = true, text = $"Go To"
692 public class EditBox : CommonControl
694 class_property(icon) = "<:ecere>controls/editBox.png";
698 virtual bool Window::NotifyModified(EditBox editBox);
699 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
700 virtual void Window::NotifyUpdate(EditBox editBox);
701 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
702 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
703 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
705 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
706 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
707 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
709 virtual bool Window::NotifyUnsetModified(EditBox editBox);
711 // Why was this commented out?
712 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
713 // and the scrollArea property isn't compared properly either.
717 if(font) ComputeFont();
718 SetInitSize(initSize);
724 style.vScroll = true;
730 style.hScroll = true;
734 property bool textHorzScroll { property_category $"Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
735 property bool textVertScroll { property_category $"Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
736 property bool readOnly
738 property_category $"Behavior"
741 style.readOnly = value;
742 itemEditCut.disabled = value || !selection;
743 itemEditDelete.disabled = value || !selection;
744 itemEditPaste.disabled = value;
746 get { return style.readOnly; }
748 property bool multiLine { property_category $"Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
749 property bool freeCaret { property_category $"Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
750 property bool tabKey { property_category $"Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
751 property int tabSize { property_category $"Behavior" set { tabSize = value; } get { return tabSize; } };
752 property bool tabSelection { property_category $"Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
753 property bool smartHome { property_category $"Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
754 property bool autoEmpty { property_category $"Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
755 property bool noCaret { property_category $"Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
756 property int maxLineSize { property_category $"Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
757 property int maxNumLines { property_category $"Behavior" set { maxLines = value; } get { return maxLines; } };
758 property bool useTab { property_category $"Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
759 property bool syntaxHighlighting { property_category $"Appearance" set { style.syntax = value; } get { return style.syntax; } };
760 property bool noSelect { property_category $"Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
761 property bool allCaps { property_category $"Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
762 property bool autoSize { property_category $"Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
763 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
764 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
765 property int numLines { get { return this ? lineCount : 0; } };
766 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
767 property int column { get { return col; } }; // TODO: Add Set
768 property int charPos { get { return x; } }; // TODO: Add Set
769 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
770 property EditLine lastLine { get { return lines.last; } };
771 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
772 property char * contents
774 property_category $"Data"
780 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
783 //SetViewToCursor(true);
791 char * buffer = null;
794 /* Can't implement this right now because of memory leak... Need string reference counting...
801 for(line = lines.first; line; line = line.next)
802 len += strlen(line.buffer);
804 buffer = new char[len+1];
806 for(line = lines.first; line; line = line.next)
808 int lineLen = strlen(line.buffer);
809 memcpy(buffer + len, line.buffer, lineLen);
815 buffer = this.line ? this.line.buffer : null;
820 property bool overwrite { get { return overwrite; } };
821 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
823 property char * multiLineContents
827 char * buffer = null;
834 for(line = lines.first; line; line = line.next)
835 len += strlen(line.buffer)+1;
837 buffer = new char[len+1];
839 for(line = lines.first; line; line = line.next)
841 int lineLen = strlen(line.buffer);
842 memcpy(buffer + len, line.buffer, lineLen);
844 if(line.next) buffer[len++] = '\n';
857 return this.line.buffer;
862 void SetLineText(char * text)
866 EditLine_SetText(this.line, text);
870 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
871 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
872 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
874 // selectionStart.line, selectionStart.column (With Set)
875 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
889 // Position of Caret (Not necessarily displayed position)
892 // Position of beginning of block (Block ends at (x,y))
894 // line is line at carret, selLine is line at beginning of block
895 EditLine line, selLine, dropLine;
896 // Mouse selection Moving virtual caret
901 // ViewX is x offset in pixels, ViewY is y offset in lines
903 // viewLine is first displayed line
906 // start and end of area to redraw
909 // MaxLine is the longest line
911 // MaxLength is the longest line's length
914 // MouseSelect is true once button is pressed, overwrite is true if mode is on
915 bool mouseSelect, mouseMove, overwrite, wordSelect;
916 // Timer is used for mouse selection scrolling
919 window = this, delay = 0.1;
928 OnMouseMove(mouseX, mouseY, -1);
933 // (mouseX,mouseY) is the position of the mouse in the edit box
938 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
941 bool rightButtonDown;
944 UndoBuffer undoBuffer { data = this };
946 Color selectionColor, selectionText;
947 SyntaxColorScheme colorScheme { };
952 Menu editMenu { menu, $"Edit", e };
955 editMenu, $"Cut\tCtrl+X", t, disabled = true;
957 bool NotifySelect(MenuItem item, Modifiers mods)
959 if(!(style.readOnly))
964 MenuItem itemEditCopy
966 editMenu, $"Copy\tCtrl+C", c, disabled = true;
968 bool NotifySelect(MenuItem item, Modifiers mods)
974 MenuItem itemEditPaste
976 editMenu, $"Paste\tCtrl+V", p;
978 bool NotifySelect(MenuItem item, Modifiers mods)
980 if(!(style.readOnly))
985 MenuItem itemEditDelete
987 editMenu, $"Delete\tDel", d, disabled = true;
989 bool NotifySelect(MenuItem item, Modifiers mods)
991 if(!(style.readOnly))
996 MenuDivider { editMenu };
997 MenuItem itemEditSelectAll
999 editMenu, $"Select All\tCtrl+A", a;
1001 bool NotifySelect(MenuItem item, Modifiers mods)
1007 MenuDivider { editMenu };
1008 MenuItem itemEditUndo
1010 editMenu, $"Undo\tCtrl+Z", u;
1013 bool NotifySelect(MenuItem item, Modifiers mods)
1019 MenuItem itemEditRedo
1021 editMenu, $"Redo\tCtrl+Y", o;
1024 bool NotifySelect(MenuItem item, Modifiers mods)
1030 MenuDivider { editMenu };
1033 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1035 bool NotifySelect(MenuItem item, Modifiers mods)
1038 Find(searchString, wholeWord, matchCase, false);
1040 itemEditFind.NotifySelect(this, item, mods);
1046 editMenu, $"Find Next\tF3", n, f3;
1048 bool NotifySelect(MenuItem item, Modifiers mods)
1051 Find(searchString, wholeWord, matchCase, true);
1053 itemEditFind.NotifySelect(this, item, mods);
1057 MenuItem itemEditFind
1059 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1061 bool NotifySelect(MenuItem item, Modifiers mods)
1065 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1066 searchUp = searchUp;
1069 // Fix dialog from above shouldn't be visible
1070 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1071 void NotifyDestroyed(Window window, DialogResult result)
1073 FindDialog dialog = (FindDialog) window;
1074 searchUp = dialog.searchUp;
1075 strcpy(searchString, dialog.searchString);
1076 matchCase = dialog.matchCase;
1077 wholeWord = dialog.wholeWord;
1086 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1088 bool NotifySelect(MenuItem item, Modifiers mods)
1090 ReplaceDialog dialog
1094 searchString = searchString,
1095 replaceString = replaceString,
1096 matchCase = matchCase,
1097 wholeWord = wholeWord,
1101 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1102 void NotifyDestroyed(Window window, DialogResult result)
1104 ReplaceDialog dialog = (ReplaceDialog)window;
1105 char * replace = dialog.replaceString;
1107 strcpy(replaceString, replace);
1108 strcpy(searchString, dialog.searchString);
1109 matchCase = dialog.matchCase;
1110 wholeWord = dialog.wholeWord;
1117 MenuDivider { editMenu };
1120 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1122 bool NotifySelect(MenuItem item, Modifiers mods)
1124 goToDialog.editBox = this;
1125 goToDialog.master = master;
1126 goToDialog.Create();
1130 MenuDivider { editMenu };
1131 MenuItem itemEditInsertTab
1133 editMenu, $"Insert Tabs", i, checkable = true;
1135 bool NotifySelect(MenuItem item, Modifiers mods)
1137 style.useTab = item.checked;
1143 snapVertScroll = true;
1144 snapHorzScroll = true;
1148 static bool syntaxInit = false;
1153 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1155 for(c = 0; keyWords[g][c]; c++);
1156 //keyLen[g] = new int[c];
1157 for(c = 0; keyWords[g][c]; c++)
1159 keyLen[g][c] = strlen(keyWords[g][c]);
1163 colorScheme.commentColor = dimGray;
1164 colorScheme.charLiteralColor = crimson;
1165 colorScheme.stringLiteralColor = crimson;
1166 colorScheme.preprocessorColor = green;
1167 colorScheme.numberColor = teal;
1168 colorScheme.keywordColors = [ blue, blue ];
1171 FontExtent = Display::FontExtent;
1173 lines.offset = (uint)&((EditLine)0).prev;
1175 style = EditBoxBits { hScroll = true };
1177 // cursor = guiApp.GetCursor(IBeam);
1179 // Default Properties
1181 maxLineSize = 1024;*/
1184 maxLineSize = MAXINT;
1189 mouseSelect = this.mouseMove = false;
1192 x = selX = selY = 0;
1195 line = selLine = null;
1201 endY = clientSize.h;
1204 // Default space to 8 in case window is not constructed
1207 undoBuffer.dontRecord++;
1209 undoBuffer.dontRecord--;
1211 viewLine = lines.first;
1212 style.recomputeSyntax = true;
1218 UpdateCaretPosition(true);
1224 lines.Free(EditLine::Free);
1227 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1229 int count = wc - *renderStart;
1232 if(y + space.h >= box.top && y <= box.bottom)
1238 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1239 surface.TextFont(font);
1240 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1241 if(*x + w + XOFFSET > 0)
1242 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1247 w = numSpaces; // * space.w;
1248 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1249 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1250 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1258 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1259 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1265 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1269 *foreground = (*selected) ? selectionForeground : textColor;
1270 *background = selectionBackground;
1271 *opacity = *selected;
1278 if(this.overwrite && active)
1280 if((style.stuckCaret && wc == line.count && !line.next) ||
1281 (!mouseMove && line == this.line && wc == editX))
1290 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1294 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1295 bool inString = false;
1296 bool inQuotes = false;
1297 bool inPrep = reset ? false : style.inPrep;
1298 bool inSingleLineComment = false;
1299 bool escaped = reset ? false : style.escaped;
1300 bool continuedSingleLineComment = reset ? false : style.continuedSingleLineComment;
1302 EditLine line = reset ? lines.first : firstLine;
1303 // int maxBackUp = 1000, c;
1304 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1305 for(; line != viewLine; line = line.next)
1307 char * text = line.buffer;
1310 bool lastWasStar = false;
1311 bool firstWord = true;
1312 if(!escaped) inPrep = false;
1313 inSingleLineComment = continuedSingleLineComment;
1319 for(c = 0; (ch = text[c]); c++)
1321 bool wasEscaped = escaped;
1322 bool backLastWasStar = lastWasStar;
1324 lastWasStar = false;
1327 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1329 if(text[c+1] == '/')
1331 inSingleLineComment = true;
1333 else if(text[c+1] == '*')
1335 inMultiLineComment = true;
1338 else if(backLastWasStar)
1339 inMultiLineComment = false;
1343 if(!c || text[c-1] != '/') lastWasStar = true;
1345 else if(ch == '\"' && !inSingleLineComment && !inMultiLineComment && !inQuotes)
1347 if(inString && !wasEscaped)
1356 else if(ch == '\'' && !inSingleLineComment && !inMultiLineComment && !inString)
1358 if(inQuotes && !wasEscaped)
1370 else if(ch == '#' && !inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1377 else if(ch != ' ' && ch != '\t')
1380 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1383 style.continuedSingleLineComment = continuedSingleLineComment;
1384 style.inMultiLineComment = inMultiLineComment;
1385 style.inPrep = inPrep;
1386 style.escaped = escaped;
1390 /*void OnDrawOverChildren(Surface surface)
1392 if(style.lineNumbers)
1394 int currentLineNumber = this.viewY + 1;
1397 for(i = 0; i * space.h < box.height; i++)
1399 // ********* LINE NUMBERING *********
1400 surface.SetForeground(Color{60, 60, 60});
1401 //Highlight current line
1402 if(this.caretY / space.h == currentLineNumber - 1)
1403 surface.SetBackground(Color{220, 220, 220});
1405 surface.SetBackground(Color{230, 230, 230});
1406 surface.textOpacity = true;
1408 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1409 if(currentLineNumber > this.lineCount)
1410 surface.WriteText(0,i*space.h+1," ",6);
1412 surface.WriteText(0,i*space.h+1,lineText,6);
1414 currentLineNumber++;
1419 void OnRedraw(Surface surface)
1423 bool selected = false, selection = true;
1425 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1426 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1427 Color defaultTextColor = property::foreground;
1430 int maxW = clientSize.w;
1432 Color foreground, background;
1435 // Overwrite Caret Stuff
1436 bool overWrite = false;
1437 int overWriteX, overWriteY;
1440 // ****** SYNTAX STATES ******
1441 bool inMultiLineComment = style.inMultiLineComment;
1442 bool inString = false;
1443 bool inQuotes = false;
1444 bool inPrep = style.inPrep;
1445 bool inSingleLineComment = false;
1446 bool escaped = style.escaped;
1447 bool continuedSingleLineComment = style.continuedSingleLineComment;
1448 // ****** ************* ******
1451 defaultTextColor = Color { 85, 85, 85 };
1452 textColor = defaultTextColor;
1455 Abs(selectionBackground.r - property::background.r) +
1456 Abs(selectionBackground.g - property::background.g) +
1457 Abs(selectionBackground.b - property::background.b) < 92)
1459 selectionBackground = formColor;
1460 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1463 surface.TextFont(this.font);
1465 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1466 surface.Area(0,0,MAXINT,MAXINT);
1471 surface.SetBackground(formColor);
1472 surface.Area(0,0,clientSize.w, clientSize.h);
1475 if(this.selX == this.x && this.selY == this.y)
1485 editX = Min(this.x,this.line.count);
1486 selX = Min(this.selX,this.selLine.count);
1489 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1491 foreground = selected ? selectionForeground : textColor;
1492 background = selectionBackground;
1498 surface.SetForeground(foreground);
1499 surface.SetBackground(background);
1500 surface.TextOpacity(opacity);
1502 surface.GetBox(box);
1504 for(line = this.viewLine; line; line = line.next)
1506 int x = -this.viewX;
1510 Color newTextColor = textColor = defaultTextColor;
1511 bool lineComplete = false;
1514 // ****** SYNTAX HIGHLIGHTING ******
1515 bool lastWasStar = false;
1516 bool firstWord = true;
1517 if(!escaped) inPrep = false;
1518 inSingleLineComment = continuedSingleLineComment;
1522 // *********************************
1524 /* === DEBUGGING TOOL FOR MAXLINE ===
1526 if(line == this.maxLine)
1528 surface.SetBackground(GREEN|0xFF000000);
1529 surface.TextOpacity(true);
1533 surface.TextOpacity(selected ? true : false);
1534 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1538 if(line == this.selLine && line == this.line)
1540 end = Max(line.count, this.x);
1541 end = Max(end, this.selX);
1543 else if(line == this.selLine)
1544 end = Max(line.count, this.selX);
1545 else if(line == this.line)
1546 end = Max(line.count, this.x);
1554 bool spacing = true;
1555 bool cantHaveWords = false;
1562 lineComplete = false;
1565 textColor = newTextColor;
1568 foreground = textColor;
1569 surface.SetForeground(textColor);
1573 for(; c<end && !cantHaveWords;)
1577 bufferLen += wordLen;
1581 /*if(line.count > 4000)
1588 for(; c<line.count; c++)
1590 unichar ch = line.buffer[c];
1591 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1592 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1593 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1594 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1595 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1603 for(; c<line.count; c++)
1605 unichar ch = line.buffer[c];
1606 if(ch == '\t' || ch == ' ')
1608 cantHaveWords = true;
1612 if(ch != ' ' && ch != '\t')
1617 if(c == line.count && c < end)
1626 cantHaveWords = true;
1631 lastWasStar = false;
1637 bool backEscaped = escaped;
1638 bool backLastWasStar = lastWasStar;
1639 bool backInMultiLineComment = inMultiLineComment;
1640 bool backInString = inString;
1641 bool backInQuotes = inQuotes;
1642 bool backInPrep = inPrep;
1643 bool backInSingleLineComment = inSingleLineComment;
1645 char * word = line.buffer + c - wordLen;
1647 bool wasEscaped = escaped;
1649 lastWasStar = false;
1651 // Determine Syntax Highlighting
1652 newTextColor = defaultTextColor;
1655 if(inSingleLineComment || inMultiLineComment)
1657 newTextColor = colorScheme.commentColor;
1661 newTextColor = colorScheme.charLiteralColor;
1665 newTextColor = colorScheme.stringLiteralColor;
1669 newTextColor = colorScheme.preprocessorColor;
1671 if(wordLen == 1 && word[0] == '/')
1673 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1677 inSingleLineComment = true;
1678 newTextColor = colorScheme.commentColor;
1680 else if(word[1] == '*')
1682 inMultiLineComment = true;
1683 newTextColor = colorScheme.commentColor;
1686 else if(backLastWasStar)
1687 inMultiLineComment = false;
1689 else if(wordLen == 1 && word[0] == '*')
1691 if(c < 2 || word[-1] != '/')
1694 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1696 if(inString && !wasEscaped)
1703 newTextColor = colorScheme.stringLiteralColor;
1706 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1708 if(inQuotes && !wasEscaped)
1713 newTextColor = colorScheme.charLiteralColor;
1716 else if(wordLen == 1 && word[0] == '\\')
1721 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1722 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1724 newTextColor = colorScheme.numberColor;
1728 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1733 newTextColor = colorScheme.preprocessorColor;
1736 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1738 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1740 char ** keys = keyWords[g];
1741 int * len = keyLen[g];
1742 for(ccc = 0; keys[ccc]; ccc++)
1744 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1746 newTextColor = colorScheme.keywordColors[g];
1755 // If highlighting for this word is different, break
1756 if(newTextColor != textColor)
1760 // Better solution than going back?
1763 // Reset syntax flags
1764 escaped = backEscaped;
1765 lastWasStar = backLastWasStar;
1766 inMultiLineComment = backInMultiLineComment;
1767 inString = backInString;
1768 inQuotes = backInQuotes;
1769 inPrep = backInPrep;
1770 inSingleLineComment = backInSingleLineComment;
1775 textColor = newTextColor;
1778 foreground = textColor;
1779 surface.SetForeground(textColor);
1786 // If we're not breaking, this can't be rendered as spacing anymore
1789 // Do word wrapping here
1795 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1800 w += numSpaces * space.w;
1802 if(x + viewX + w > maxW)
1805 lineComplete = true;
1810 bufferLen += wordLen;
1815 int renderStart = start;
1820 // Render checking if we need to split because of selection or to find where to draw insert caret
1821 for(wc = start; wc < start + bufferLen; wc++)
1823 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1824 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1825 if(overWrite == true)
1827 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1828 if(overWriteCh == '\t') overWriteCh = ' ';
1833 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1834 if(overWrite == true)
1842 surface.TextOpacity(opacity);
1843 surface.SetBackground(background);
1844 surface.SetForeground(foreground);
1851 if(wc < line.count && line.buffer[wc] == '\t')
1853 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1857 numSpaces += space.w;
1861 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1866 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1867 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1869 if(overWrite == true)
1876 surface.TextOpacity(opacity);
1877 surface.SetBackground(background);
1878 surface.SetForeground(foreground);
1881 if(style.freeCaret && selected)
1883 surface.SetBackground(selectionBackground);
1884 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1885 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1890 if(style.freeCaret && selected)
1892 surface.SetBackground(selectionBackground);
1893 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1897 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1900 if(y > box.bottom) // >=clientSize.h)
1906 surface.TextOpacity(true);
1907 surface.SetForeground(black);
1908 surface.SetBackground(Color {255,255,85});
1909 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1913 void FixScrollArea()
1915 if(style.hScroll || style.vScroll)
1917 int width = maxLength + XOFFSET;
1918 int height = lineCount * space.h;
1919 if(style.freeCaret && line)
1924 width = Max(line.length + (x - line.count) * space.w, maxLength);
1928 if(selX > selLine.count)
1929 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1934 SetScrollLineStep(8, space.h);
1935 SetScrollArea(width, height, true);
1939 void ComputeLength(EditLine line)
1946 for(c = 0; c < line.count; )
1955 for(len = 0; c < line.count; c += numBytes)
1957 ch = line.buffer[c];
1958 numBytes = UTF8_NUM_BYTES(ch);
1960 if(ch == ' ' || ch == '\t')
1967 if(!len && ch == ' ')
1972 else if(!len && ch == '\t')
1974 w = (tabSize * space.w) - (x % (tabSize * space.w));
1978 FontExtent(display, font, line.buffer + start, len, &w, null);
1989 if(line.length > this.maxLength)
1991 this.maxLine = line;
1992 this.maxLength = line.length;
2001 this.maxLine = null;
2003 for(line = lines.first; line; line = line.next)
2005 if(line.length > this.maxLength)
2007 this.maxLength = line.length;
2008 this.maxLine = line;
2015 if(this.selY != this.y)
2017 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2021 void ComputeColumn()
2023 // Adjust new edit X position according to tabs
2024 int c, position = 0;
2027 for(c = 0; c<this.line.count && c<this.x && (ch = UTF8_GET_CHAR(this.line.buffer + c, nb)); c+= nb)
2029 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2031 position += this.tabSize - (position % this.tabSize);
2035 position += this.x - c;
2036 this.col = position;
2039 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2041 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2044 bool HasCommentOrEscape(EditLine line)
2046 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2051 for(c = line.count-1; c >= 0; c--)
2053 char ch = line.buffer[c];
2059 else //if(ch != ' ' && ch != '\t')
2066 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2068 EditLine line = l1, next;
2070 int oldCount1, oldCount2;
2071 int y, firstViewY, firstY, firstDropY, firstSelY;
2074 DelTextAction action = null;
2075 bool hadComment = false;
2079 hadComment = HasCommentOrEscape(line);
2081 if(y2 > y1 || c2 > c1)
2083 if(start < l1.count)
2085 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2089 oldCount1 = l1.count;
2091 while(c1 < oldCount1)
2093 byte ch = buffer[c1];
2094 if(UTF8_IS_FIRST(ch)) break;
2098 oldCount2 = l2.count;
2100 while(c2 < oldCount2)
2102 byte ch = buffer[c2];
2103 if(UTF8_IS_FIRST(ch)) break;
2108 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2113 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2114 string = new char[len];
2115 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2116 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2120 //oldCount1 = l1.count;
2121 //oldCount2 = l2.count;
2124 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2125 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2128 if(c2 > oldCount2) c2 = oldCount2;
2129 if(!(style.freeCaret))
2130 if(c1 > oldCount1) c1 = oldCount1;
2131 newLineCount = c1 + l2.count-c2;
2137 buffer = new char[line.size ? line.size : 1];
2139 buffer = new char[line.size];
2141 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2146 if(!line.AdjustBuffer(newLineCount))
2150 /*if(newLineCount > 4000 || newLineCount < 0)
2151 printf("Warning");*/
2153 line.count = newLineCount;
2155 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2160 action.addedSpaces = c1-oldCount1;
2162 if(action.addedSpaces > action.x1)
2168 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2169 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2171 line.buffer[line.count] = '\0';
2178 this.dropX -= c2-c1;
2179 this.dropX = Max(this.dropX,0);
2181 this.x = Max(this.x,0);
2188 firstViewY = this.viewY;
2190 firstDropY = this.dropY;
2191 firstSelY = this.selY;
2192 for(line = l1;line;line = next, y++)
2200 if(line == this.viewLine)
2202 if(this.viewLine.next)
2204 this.viewLine = this.viewLine.next;
2206 style.recomputeSyntax = true;
2210 this.viewLine = this.viewLine.prev;
2212 style.recomputeSyntax = true;
2215 else if(y < firstViewY)
2217 if(line == this.line)
2221 this.line = this.line.next;
2222 this.x = this.line.count;
2227 this.line = this.line.prev;
2228 this.x = this.line.count;
2235 if(line == this.dropLine)
2237 if(this.dropLine.next)
2239 this.dropLine = this.dropLine.next;
2240 this.dropX = this.dropLine.count;
2244 this.dropLine = this.dropLine.prev;
2245 this.dropX = this.dropLine.count;
2249 else if(y < firstDropY)
2251 if(line == this.selLine)
2253 if(this.selLine.next)
2255 this.selLine = this.selLine.next;
2256 this.selX = this.selLine.count;
2260 this.selLine = this.selLine.prev;
2261 this.selX = this.selLine.count;
2265 else if(y < firstSelY)
2269 if(line == l2) break;
2273 if(style.autoSize) AutoSize();
2274 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2277 style.recomputeSyntax = true;
2282 bool DelSel(int * addedSpacesPtr)
2284 if(this.line != this.selLine || this.x != this.selX)
2286 if(this.selY < this.y)
2288 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2291 this.line = this.selLine;
2293 else if(this.selY > this.y)
2295 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2298 this.selLine = this.line;
2300 else if(this.selX < this.x)
2302 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2305 this.line = this.selLine;
2309 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2312 this.selLine = this.line;
2320 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2322 bool hadComment = false;
2323 // Add the line here
2324 EditLine line = this.line;
2326 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2327 if(!style.hScroll && created)
2329 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2334 // Lock if no place to display.
2337 else if(endX > this.x)
2338 max = Max(this.x, line.count);
2342 for(x = 0, c = 0; c < max+count; )
2347 if(c < Min(this.x, line.count))
2348 string = line.buffer + c;
2351 else if(c < endX + count)
2352 string = stringLine + c - endX;
2354 string = line.buffer + c - endX - count;
2358 w = (tabSize * space.w) - (x % (tabSize * space.w));
2362 numBytes = UTF8_NUM_BYTES(*string);
2363 FontExtent(display, this.font, string, numBytes, &w, null);
2367 if(x >= clientSize.w)
2372 c += numBytes; // - 1;
2378 int addedSpaces = 0;
2381 // Add blank spaces if EES_FREECARET
2382 if(this.x > line.count)
2392 for(c = 0; c<line.count; c++)
2394 if(this.line.buffer[c] == '\t')
2395 position += this.tabSize - (position % this.tabSize);
2399 wantedPosition = position + (this.x - line.count);
2401 // A tab is too much...
2402 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2403 addedSpaces = wantedPosition - position;
2408 position += this.tabSize - (position % this.tabSize);
2409 // Add as many tabs as needed
2410 addedTabs += (wantedPosition - position) / this.tabSize;
2411 position += (addedTabs-1) * this.tabSize;
2412 // Finish off with spaces
2413 addedSpaces = wantedPosition - position;
2417 addedSpaces = this.x - line.count;
2420 this.x = Min(this.x, line.count);
2422 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2428 hadComment = HasCommentOrEscape(line);
2430 // Adjust the size of the line
2431 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2435 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2438 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2439 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2442 *addedTabsPtr = addedTabs;
2443 FillBytes(line.buffer+line.count,'\t',addedTabs);
2445 if(addedTabs > 4000 || addedTabs < 0)
2448 line.count += addedTabs;
2454 FillBytes(line.buffer+line.count,' ',addedSpaces);
2456 if(addedSpaces > 4000 || addedSpaces < 0)
2459 line.count += addedSpaces;
2460 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2462 else if(addedSpacesPtr)
2463 *addedSpacesPtr = 0;
2465 if(count > 4000 || count < 0)
2468 line.count += count;
2469 this.x += count + addedTabs + addedSpaces;
2472 this.selLine = this.line;
2474 line.buffer[line.count] = '\0';
2476 ComputeLength(line);
2481 hasComment = HasCommentOrEscape(line);
2482 if(!undoBuffer.insideRedo)
2484 int backDontRecord = undoBuffer.dontRecord;
2485 undoBuffer.dontRecord = 0;
2486 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2487 if(style.autoSize) AutoSize();
2488 undoBuffer.dontRecord = backDontRecord;
2490 if(style.syntax && (hadComment || hasComment || line != this.line))
2492 style.recomputeSyntax = true;
2500 void Emptyline(EditLine line, int y)
2503 DelCh(line, y, 0, line.next, y+1, 0, true);
2505 DelCh(line, y, 0, line, y, line.count, true);
2508 void GoToEnd(bool deselect)
2512 this.line = this.lines.last;
2513 if(this.y != this.lineCount-1)
2515 else if (this.x != this.line.count)
2516 DirtyLine(this.lineCount-1);
2517 this.y = this.lineCount-1;
2518 this.x = this.line.count;
2525 void GoToHome(bool deselect)
2529 this.line = this.lines.first;
2532 else if (this.x !=0)
2533 DirtyLine(this.lineCount-1);
2542 // Returns true if it needs scrolling
2543 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2549 bool needHScroll = false;
2556 line = this.viewLine ? (void *)this.viewLine.prev : null;
2561 line = (void *)this.lines.first;
2566 py = Min(py, clientSize.h);
2568 py = Min(py, this.lineCount);
2570 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2576 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2579 px = Min(px,clientSize.w+this.space.w);
2583 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2586 if(tline) *tline = line;
2589 // Prevent divide by 0 from non valid this.font
2591 return (y < this.viewY) || needHScroll;
2593 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2597 // Minimal Update Management Functions
2601 this.endY = clientSize.h-1;
2602 // ErrorLog("DirtyAll\n");
2605 void DirtyEnd(int y)
2607 if((y - this.viewY)*this.space.h < this.startY)
2608 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2609 this.endY = clientSize.h-1;
2610 //ErrorLog("DirtyEnd %d\n", y);
2613 void DirtyLine(int y)
2617 if((y - this.viewY)*this.space.h < this.startY)
2618 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2619 if((y - this.viewY+1)*this.space.h > this.endY)
2620 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2622 //ErrorLog("DirtyLine %d\n", y);
2629 if(style.recomputeSyntax)
2631 FigureStartSyntaxStates(lines.first, true);
2632 style.recomputeSyntax = false;
2635 if(this.startY > this.endY) return;
2636 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2642 box.right = clientSize.w-1;
2643 box.top = this.startY;
2644 box.bottom = this.endY;
2647 this.startY = clientSize.h;
2651 bool IsMouseOnSelection()
2653 bool mouseOnSelection = false;
2656 int minY = Min(this.selY, this.y);
2657 int maxY = Max(this.selY, this.y);
2658 int minX = Min(this.selX, this.x);
2659 int maxX = Max(this.selX, this.x);
2661 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2663 if(maxX != minX || maxY != minY)
2665 if(y > minY && y < maxY)
2666 mouseOnSelection = true;
2667 else if(y == minY && y == maxY)
2668 mouseOnSelection = (x < maxX && x >= minX);
2672 mouseOnSelection = (x >= this.selX);
2673 else if(y == this.y)
2674 mouseOnSelection = (x >= this.x);
2679 mouseOnSelection = (x < this.selX);
2680 else if(y == this.y)
2681 mouseOnSelection = (x < this.x);
2684 return mouseOnSelection;
2687 void UpdateCaretPosition(bool setCaret)
2691 if(mouseMove || (!overwrite && !style.noCaret))
2693 int max = this.mouseMove ? this.dropX : this.x;
2694 int y = this.mouseMove ? this.dropY : this.y;
2695 EditLine line = this.mouseMove ? this.dropLine : this.line;
2697 if(!(style.freeCaret))
2698 max = Min(max, line.count);
2700 if(FontExtent && display)
2702 for(c = 0; c < max; )
2711 for(len = 0; c < Min(max, line.count); c += numBytes)
2713 ch = line.buffer[c];
2714 numBytes = UTF8_NUM_BYTES(ch);
2716 if(ch == ' ' || ch == '\t')
2723 if(!len && ch == ' ')
2728 else if(!len && ch == '\t')
2730 w = (tabSize * space.w) - (x % (tabSize * space.w));
2734 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2746 caretY = y * this.space.h;
2747 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2752 NotifyCaretMove(master, this, y + 1, x + 1);
2758 void SelectionEnables()
2760 if((x != selX || y != selY) && !selection)
2764 itemEditCut.disabled = false;
2765 itemEditDelete.disabled = false;
2767 itemEditCopy.disabled = false;
2769 this.selection = true;
2771 else if((x == selX && y == selY) && selection)
2773 itemEditCut.disabled = true;
2774 itemEditCopy.disabled = true;
2775 itemEditDelete.disabled = true;
2777 this.selection = false;
2781 void SetSelectCursor()
2783 if(!inactive || !style.noSelect)
2786 cursor = guiApp.GetCursor(arrow);
2787 else if(this.mouseSelect)
2788 cursor = guiApp.GetCursor(iBeam);
2791 if(IsMouseOnSelection())
2792 cursor = guiApp.GetCursor(arrow);
2794 cursor = guiApp.GetCursor(iBeam);
2799 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2802 int x = px ? *px : 0;
2809 if(c < Min(max, line.count))
2813 for(len = 0; c < Min(max, line.count); c += numBytes)
2815 ch = line.buffer[c];
2816 numBytes = UTF8_NUM_BYTES(ch);
2818 if(ch == ' ' || ch == '\t')
2825 if(!len && ch == ' ')
2830 else if(!len && ch == '\t')
2832 w = (tabSize * space.w) - (x % (tabSize * space.w));
2836 FontExtent(display, font, line.buffer + start, len, &w, null);
2840 if(style.freeCaret && c < max)
2849 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2854 int a = start + len;
2857 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2861 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2864 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2868 return Min(this.maxLineSize - 1, start + len);
2874 void SetCursorToViewX()
2876 bool selecting = this.x != selX || y != selY;
2878 // Horizontal Adjustment
2880 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2885 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2897 UpdateCaretPosition(false);
2903 void SetCursorToViewY()
2906 EditLine oldLine = this.line;
2908 bool selecting = this.x != this.selX || this.y != this.selY;
2910 numLines = clientSize.h / this.space.h;
2912 // Vertical Adjustment
2913 if(this.viewY > this.y)
2915 this.y = this.viewY;
2916 this.line = this.viewLine;
2919 if(this.viewY + numLines <= this.y)
2923 this.y = this.viewY-1;
2924 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2931 if(this.line != oldLine)
2933 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2941 this.selLine = this.line;
2944 UpdateCaretPosition(false);
2951 bool SaveFile(char * fileName)
2953 File f = eFile_Open(fileName, FO_WRITE);
2957 eWindow_SetModified(false);
2958 eInstance_Delete(f);
2965 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2969 if(!style.multiLine)
2971 x = (active && this.active && !style.readOnly) ? line.count : 0;
2974 SetViewToCursor(true);
2979 if(!active && modified)
2982 if(!NotifyModified(master, this))
2985 *goOnWithActivation = false;
2992 if(timer) timer.Stop();
2994 mouseSelect = false;
3006 // todo: resize width based on largest on-screen-line extent...
3008 display.FontExtent(font, " ", 1, null, &sh);
3012 nh = lineCount * sh + 2;
3013 size.h = nh < minClientSize.h ? minClientSize.h : nh;
3024 int len = line ? strlen(line.text) : 0;
3025 GetDecorationsSize(&dw, &dh);
3026 display.FontExtent(font, " ", 1, null, &sh);
3027 if(len) display.FontExtent(font, line.text, len, &tw, null);
3029 if(nw < minClientSize.w) nw = minClientSize.w;
3031 if(nh < minClientSize.h) nh = minClientSize.h;
3037 bool OnResizing(int *w, int *h)
3043 *w = space.h * 80 / 14;
3047 void OnResize(int w, int h)
3050 if(!hasHorzScroll && !hasVertScroll && viewLine)
3051 SetViewToCursor(true);
3053 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3055 SetViewToCursor(true);
3060 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3062 if(style.readOnly) return true;
3063 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3064 // Middle button already serves as a dragger as well.
3069 bool OnRightButtonDown(int x, int y, Modifiers mods)
3071 this.rightButtonDown = true;
3076 bool OnRightButtonUp(int x, int y, Modifiers mods)
3079 if(!parent.inactive && rightButtonDown)
3082 Menu contextMenu { };
3084 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3085 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3086 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3087 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3088 MenuDivider { contextMenu };
3089 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3091 popup = PopupMenu { master = this, menu = contextMenu,
3093 nonClient = true, interim = false, parent = parent,
3094 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3096 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3100 rightButtonDown = false;
3104 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3109 if(style.noSelect) return true;
3111 // Should we have a separate 'selectOnActivate' style?
3112 if(!mods.isActivate || (style.readOnly && style.multiLine))
3118 mouseX = mx - XOFFSET;
3121 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3123 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3129 else if(IsMouseOnSelection() && !mods.isActivate)
3139 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3141 if(mods.shift && !mods.isActivate)
3156 this.selLine = this.line;
3165 UpdateCaretPosition(true);
3169 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3173 mouseSelect = false;
3184 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3186 //PrintLn("MouseMove: ", x, ", ", y);
3192 mouseMove = IsMouseOnSelection();
3196 int size = SelSize();
3199 char * text = new char[size+1];
3203 GetSel(text, false);
3205 if(Max(selY, this.y) == dropY)
3209 if(this.dropX > this.selX)
3210 moveX = this.x - this.selX;
3214 if(this.dropX > this.x)
3215 moveX = this.selX - this.x;
3219 this.dropX -= moveX;
3220 this.selX = this.x = this.dropX;
3221 this.selY = this.y = this.dropY;
3222 this.selLine = this.line = this.dropLine;
3224 SetViewToCursor(true);
3229 byte * c = ((EditBox)this).multiLineContents, * c2;
3230 int l1 = c ? strlen(c) : 0, l2;
3233 c2 = ((EditBox)this).multiLineContents;
3234 l2 = c2 ? strlen(c2) : 0;
3235 if(l1 != l2 || (l1 && strcmp(c, c2)))
3265 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3267 //PrintLn("Dropped: ", x, ", ", y);
3269 NotifyDropped(master, this, x, y);
3276 bool OnMouseMove(int mx, int my, Modifiers mods)
3282 if(mods != -1 && mods.isSideEffect)
3287 if(style.noSelect) return true;
3288 if(wordSelect) return true;
3289 mouseX = mx - XOFFSET;
3292 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3294 if(this.mouseMove || this.mouseSelect)
3303 ((style.hScroll) || (style.vScroll)))
3310 DirtyLine(this.dropY);
3313 DirtyLine(this.dropY);
3314 this.dropLine = line;
3315 SetViewToCursor(true);
3317 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3320 else if(this.mouseSelect)
3322 DirtyLine(this.selY);
3329 SetViewToCursor(true);
3332 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3339 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3344 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3348 if(style.noSelect) return true;
3349 FindMouse(mx, my, &x, &y, &line, false);
3350 if(!NotifyDoubleClick(master, this, line, mods))
3357 for(c = x; c >= 0; c--)
3360 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3361 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3368 for(c = start; c<line.count; c += numBytes)
3370 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3371 if(!ch || !IS_ALUNDER(ch))
3381 this.line = this.selLine = line;
3382 this.wordSelect = (c != start);
3393 Menu fileMenu { menu, "File", F };
3394 saveDialog = fileDialog;
3395 MenuItem { fileMenu, $"Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3396 MenuItem { fileMenu, $"Save As...", A, NotifySelect = MenuFileSaveAs };
3398 if(style.autoSize) AutoSize();
3406 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3407 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3409 space.w = Max(space.w, 1);
3410 large.w = Max(large.w, 1);
3411 space.h = Max(space.h, 1);
3412 large.h = Max(large.h, 1);
3416 for(line = lines.first; line; line = line.next)
3417 ComputeLength(line);
3422 SetViewToCursor(true);
3430 bool OnLoadGraphics()
3432 FontExtent = Display::FontExtent;
3435 // UpdateCaretPosition(true);
3439 void OnUnloadGraphics()
3444 bool OnKeyHit(Key key, unichar ch)
3446 bool shift = (key.shift) ? true : false;
3448 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3450 if(!ch && !key.alt && !key.ctrl)
3452 key.code = (SmartKey)key.code;
3455 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3458 if(style.readOnly) break;
3459 if(style.stuckCaret) GoToEnd(true);
3460 if(!(style.freeCaret))
3462 this.x = Min(this.x, this.line.count);
3468 EditLine line = this.line;
3470 for(y = this.y; y>= 0; y--)
3472 c = (y == this.y) ? (this.x-1) : line.count-1;
3474 // Slow down when going on lines...
3475 if(y != this.y) break;
3479 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3480 if(IS_ALUNDER(line.buffer[c]))
3485 if(!IS_ALUNDER(line.buffer[c]))
3487 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3501 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3502 this.x = this.selX = Min(c+1, line.count);
3503 this.y = this.selY = y;
3504 this.line = this.selLine = line;
3505 SetViewToCursor(true);
3516 if(style.readOnly) break;
3517 if(style.stuckCaret) break;
3526 if(this.line != this.selLine || this.x != this.selX)
3529 SetViewToCursor(true);
3535 if(this.x < this.line.count)
3539 for(i = this.x; i < this.line.count; i++)
3541 if(!IS_ALUNDER(this.line.buffer[i]))
3545 for(; i < this.line.count; i++)
3547 //Delete trailing whitespace
3548 if(IS_ALUNDER(this.line.buffer[i]))
3551 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3552 SetViewToCursor(true);
3555 else if(this.line.next)
3557 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3558 SetViewToCursor(true);
3564 if(!(style.freeCaret))
3566 this.selX = this.x = Min(this.x, this.line.count);
3569 if(this.x < this.line.count)
3571 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3573 else if(this.line.next)
3575 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3577 SetViewToCursor(true);
3586 if(!key.alt && !key.ctrl)
3590 bool stuffAfter = false;
3594 if(style.stuckCaret) GoToEnd(true);
3595 if(style.readOnly) break;
3596 if(!(style.multiLine)) break;
3598 for(c = 0; c<this.line.count && c<this.x; c++)
3600 if(this.line.buffer[c] == '\t')
3601 position += this.tabSize - (position % this.tabSize);
3602 else if(this.line.buffer[c] == ' ')
3610 if(this.x < this.line.count)
3613 //If last character is a { indent one tab
3614 if(this.line.buffer[this.x - 1] == '{')
3616 //Except if the next non space character is a }
3617 bool indent = false;
3619 for(i = this.x; i < this.line.size; i++)
3620 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3622 if(this.line.buffer[i] != '}')
3627 position += this.tabSize;
3630 addString = new char[position + 2];
3631 addString[len++] = '\n';
3632 addString[len] = '\0';
3634 if(stuffAfter || !style.freeCaret)
3636 for(c = 0; c<position; )
3638 if(style.useTab && c + this.tabSize <= position)
3640 addString[len++] = '\t';
3645 addString[len++] = ' ';
3649 addString[len] = '\0';
3653 if(!stuffAfter && style.freeCaret)
3655 this.x = this.selX = position;
3659 SetViewToCursor(true);
3669 if(style.stuckCaret) break;
3670 if(!(style.freeCaret))
3672 this.x = Min(this.x, this.line.count);
3673 this.selX = Min(this.selX, this.selLine.count);
3676 if(!shift) SelDirty();
3679 bool foundAlpha = false;
3682 EditLine line, lastLine;
3685 for(line = this.line; (line && !found); line = line.prev, y--)
3690 if(this.x == 0 && line != this.line)
3699 if(line == this.line) start = this.x -1; else start = line.count-1;
3700 start = Min(start, line.count-1);
3702 for(c = start; c >= 0;)
3705 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3723 byte ch = line.buffer[c];
3724 if(UTF8_IS_FIRST(ch)) break;
3728 // No next word found,
3729 if(!found && ( this.x > 0 || (!line.count && this.x)))
3743 this.line = lastLine;
3754 byte * buffer = line.buffer;
3757 byte ch = buffer[x];
3758 if(UTF8_IS_FIRST(ch)) break;
3775 if(!shift) Deselect();
3776 SetViewToCursor(true);
3782 if(style.stuckCaret) break;
3783 if(!(style.freeCaret))
3785 this.x = Min(this.x, this.line.count);
3786 this.selX = Min(this.selX, this.selLine.count);
3789 if(!shift) SelDirty();
3790 if(!shift && (this.x != this.selX || this.y != this.selY));
3793 bool onAChar = false;
3794 if(this.selX != this.x || this.selY != this.y)
3796 if(this.x<this.line.count)
3797 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3799 if(key.shift && onAChar &&
3800 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3802 bool foundAlpha = false;
3804 EditLine line, lastLine;
3806 int lastC, lastY, lastNumBytes;
3808 for(line = this.line; (line && !found); line = line.next, y++)
3810 int start = (line == this.line) ? this.x : 0;
3814 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3820 lastNumBytes = numBytes;
3830 if(!found && (c != this.x || line != this.line))
3843 this.x = lastC + lastNumBytes;
3845 this.line = lastLine;
3852 bool foundAlpha = false;
3857 for(line = this.line; (line && !found); line = line.next, y++)
3859 int start = (line == this.line) ? this.x : 0;
3863 for(c = start; c < line.count && (ch = UTF8_GET_CHAR(line.buffer + c, numBytes)); c += numBytes)
3879 // No next word found,
3880 if(!found && (c != this.x || line != this.line))
3884 this.x = line.count;
3896 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3900 byte * buffer = line.buffer;
3903 byte ch = buffer[x];
3904 if(UTF8_IS_FIRST(ch)) break;
3925 if(!shift) Deselect();
3926 SetViewToCursor(true);
3933 if(!style.vScroll || hasVertScroll) break;
3939 if(style.stuckCaret) break;
3941 if(!shift) SelDirty();
3951 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3955 if(!shift) Deselect();
3957 SetViewToCursor(false);
3960 if(caretY == this.y * space.h)
3966 if(!style.freeCaret)
3967 this.x = Min(this.x, line.count);
3977 int sx = 0, sy = this.y * space.h;
3978 char * text = line.text;
3979 int maxW = clientSize.w - sx;
3980 display.FontExtent(font, " ", 1, null, &th);
3984 int startPos = textPos;
3987 bool lineComplete = false;
3989 if(!style.wrap && caretY == MAXINT)
3992 //textPos = line.count;
3993 //lineComplete = true;
3996 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
4000 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4003 len = (nextSpace - (text + textPos));
4005 len = line.count - textPos;
4007 if(textPos < line.count)
4009 display.FontExtent(font, text + textPos, len, &w, null);
4011 if(nextSpace) { w += space.w; len++; }
4013 if(style.wrap && x + width + w > maxW && x > 0)
4015 lineComplete = true;
4025 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4029 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4032 if(this.x > line.count)
4035 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4036 len = this.x - startPos;
4037 display.FontExtent(font, text + startPos, len, &x, null);
4041 if(!shift) Deselect();
4043 SetViewToCursor(false);
4047 if(sy == caretY - th || textPos >= line.count)
4049 if(textPos >= line.count)
4051 int c = textPos - 1;
4052 while(c > 0 && text[c] == ' ') c--;
4056 this.x = line.count;
4059 if(!shift) Deselect();
4061 SetViewToCursor(false);
4066 } while(textPos < line.count);
4069 if(!shift) Deselect();
4071 SetViewToCursor(false);
4080 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4081 if(!shift) SelDirty();
4082 this.line = this.line.prev;
4088 if(!shift) Deselect();
4092 SetViewToCursor(false);
4098 return style.multiLine ? false : true;
4102 if(!style.vScroll || hasVertScroll)
4109 if(style.stuckCaret) break;
4113 int sx = 0, sy = this.y * this.space.h;
4114 int maxW = clientSize.w - sx;
4115 char * text = line.buffer;
4117 if(!shift) SelDirty();
4123 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4133 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4136 if(!shift) Deselect();
4138 if(this.selX != this.x || this.selY != this.y)
4140 SetViewToCursor(false);
4143 while(!textPos || (style.freeCaret || textPos<line.count))
4145 int startPos = textPos;
4148 bool lineComplete = false;
4149 if(!style.wrap && sy <= caretY)
4151 textPos = line.count;
4152 lineComplete = true;
4154 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4158 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4161 len = (nextSpace - (text + textPos));
4163 len = line.count - textPos;
4165 if(textPos < line.count)
4167 display.FontExtent(font, text + textPos, len, &w, &th);
4169 if(nextSpace) { w += space.w; len++; }
4170 if(style.wrap && x + width + w > maxW && x > 0)
4172 lineComplete = true;
4182 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4186 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4189 if(this.x > line.count)
4192 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4194 len = this.x - startPos;
4195 display.FontExtent(font, text + startPos, len, &x, null);
4198 if(!shift) Deselect();
4201 SetViewToCursor(false);
4207 this.x = line.count;
4210 if(!shift) Deselect();
4213 SetViewToCursor(false);
4216 else if(textPos >= line.count && line.next)
4222 sy = this.y * this.space.h;
4223 sx = 0; //textBlock.startX;
4229 sx = 0; //textBlock.startX;
4237 this.x = Min(this.x, line.count);
4238 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4240 if(!shift) Deselect();
4243 if(this.selX != this.x || this.selY != this.y)
4246 SetViewToCursor(false);
4253 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4254 if(!shift) SelDirty();
4255 this.line = this.line.next;
4259 if(!shift) Deselect();
4262 if(this.selX != this.x || this.selY != this.y)
4270 return style.multiLine ? false : true;
4273 if(style.stuckCaret) break;
4274 if(!(style.freeCaret))
4275 this.selX = Min(this.selX, this.selLine.count);
4277 if(!shift) SelDirty();
4280 this.line = this.lines.first;
4281 if(this.y != 0 || this.x != 0)
4291 EditLine line = this.line;
4293 for(c=0; line.buffer[c]; c++)
4294 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4296 if(c != 0 || this.x)
4306 DirtyLine(this.y);*/
4311 if(!shift) Deselect();
4312 SetViewToCursor(true);
4318 if(style.stuckCaret) break;
4319 if(!(style.freeCaret))
4320 this.selX = Min(this.selX, this.selLine.count);
4322 if(!shift) SelDirty();
4327 else if(this.x != this.line.count)
4329 this.x = this.line.count;
4330 //DirtyLine(this.y);
4333 if(!shift) Deselect();
4334 SetViewToCursor(true);
4339 if(style.tabKey && !key.ctrl)
4341 if(this.selY != this.y && style.tabSel)
4343 EditLine firstLine, lastLine;
4347 // Do multi line selection tabbing here
4348 if(this.selY < this.y)
4350 firstLine = this.selLine;
4351 lastLine = this.line;
4357 // Selecting going up
4358 firstLine = this.line;
4359 lastLine = this.selLine;
4366 for(line = firstLine; line; line = line.next, y++)
4368 if(line != lastLine || x)
4372 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4374 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4376 if(line.buffer[c] == '\t')
4381 else if(line.buffer[c] != ' ')
4386 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4389 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4390 char * string = new char[len];
4391 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4392 GetText(string, line, y, 0, line, y, lastC, false, false);
4395 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4396 if(!line.AdjustBuffer(line.count-lastC))
4399 if(style.autoSize) AutoSize();
4403 if(line == lastLine) break;
4408 for(line = firstLine; line; line = line.next, y++)
4412 if(line != lastLine || x)
4416 if(line.count + 1 <= this.maxLineSize)
4418 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4420 if(!line.AdjustBuffer(line.count+1))
4423 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4424 if(style.autoSize) AutoSize();
4426 AddCharAction action { ch = '\t', x = 0, y = y };
4430 memmove(line.buffer+1,line.buffer,line.size-1);
4432 line.buffer[0] = '\t';
4437 if(line.count + this.tabSize <= this.maxLineSize)
4440 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4441 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4442 if(style.autoSize) AutoSize();
4444 if(!line.AdjustBuffer(line.count+this.tabSize))
4448 char * string = new char[this.tabSize + 1];
4449 memset(string, ' ', this.tabSize);
4450 string[this.tabSize] = '\0';
4451 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4454 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4455 line.count+=this.tabSize;
4456 for(c=0; c<this.tabSize; c++)
4457 line.buffer[c] = ' ';
4463 if(line == lastLine) break;
4477 char * addString = new char[this.tabSize + 1];
4479 if(!(style.freeCaret))
4481 this.x = Min(this.x, this.line.count);
4485 start = Min(this.x, this.selX);
4486 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4488 addString[len++] = ' ';
4496 SetViewToCursor(true);
4503 if(!(style.hScroll) || hasHorzScroll) break;
4504 if(this.viewX < this.maxLength)
4506 //this.viewX+=this.space.w*this.tabSize;
4508 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4515 if(!shift) Deselect();
4524 if(!(style.hScroll) || hasHorzScroll) break;
4527 //this.viewX-=this.space.w*this.tabSize;
4528 //this.viewX = Max(this.viewX,0);
4530 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4531 // SetCursorToView();
4538 if(!shift) Deselect();
4552 if(!(style.readOnly))
4558 this.overwrite ^= 1;
4559 UpdateCaretPosition(true);
4564 NotifyOvrToggle(master, this, this.overwrite);
4575 if(style.noSelect) break;
4578 //Save current view position
4579 int tempX = this.viewX;
4580 int tempY = this.viewY;
4584 this.selLine = this.lines.first;
4585 this.y = this.lineCount-1;
4586 this.line = this.lines.last;
4587 this.x = this.line.count;
4590 SetViewToCursor(true);
4592 //Restore previous view position
4593 SetScrollPosition(tempX, tempY * this.space.h);
4599 // TOCHECK: Was there any good reason why we weren't returning false here?
4600 return false; // break;
4605 if(!(style.readOnly))
4613 if(style.readOnly) break;
4619 if(style.readOnly) break;
4624 if(style.readOnly) break;
4628 if(style.readOnly) break;
4632 if(style.readOnly) break;
4633 if(key.shift && key.code == rightBracket)
4635 //Only indent back if you are exactly at one tab.
4637 bool whitespace = true;
4643 EditLine line = this.line;
4645 //Only remove one tab if there is nothing else on the line.
4646 for(i = 0; i < this.line.count; i++)
4648 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4662 for(pos = line.count - 1; pos >= 0; pos--)
4664 char c = line.buffer[pos];
4668 indentwidth += this.tabSize;
4670 //Counting backwards, so when you find a character, indentation is reset
4680 //Place the } to get an undo:
4683 this.x = this.line.count;
4687 putsize = indentwidth;
4689 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4691 newline = new char[putsize+2];
4692 newline[putsize] = '}';
4693 newline[putsize+1] = '\0';
4697 for(; i < indentwidth / this.tabSize; i++)
4699 for(;i < putsize; i++)
4707 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4719 void OnHScroll(ScrollBarAction action, int position, Key key)
4722 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4724 this.viewX = position;
4725 if(action != setRange)
4727 if(!this.mouseMove && style.cursorFollowsView)
4734 void OnVScroll(ScrollBarAction action, int position, Key key)
4736 int oldViewY = this.viewY;
4739 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4741 position /= this.space.h;
4743 if(position < this.viewY)
4745 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4746 style.recomputeSyntax = true;
4748 else if(position > this.viewY)
4750 EditLine oldViewLine = viewLine;
4751 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4752 FigureStartSyntaxStates(oldViewLine, false);
4755 if(action != setRange)
4757 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4760 if(this.x != this.selX || this.y != this.selY)
4764 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4767 int numLines = clientSize.h / this.space.h;
4769 if(Abs(this.viewY - oldViewY) < numLines)
4772 if(this.viewY > oldViewY)
4774 for(y = oldViewY; y <this.viewY; y++)
4775 DirtyLine(y + numLines);
4779 for(y = this.viewY; y <oldViewY; y++)
4780 DirtyLine(y + numLines);
4785 // Fix dirt of stuff before first line...
4786 if(this.viewY - oldViewY > 0)
4788 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4795 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4800 ReplaceTextAction replaceAction = null;
4801 AddCharAction addCharAction = null;
4802 int addedSpaces = 0, addedTabs = 0;
4804 if(ch == '\r') return true;
4805 if(style.stuckCaret /*|EES_READONLY)*/ )
4808 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4810 if(!undoBuffer.dontRecord)
4812 if(selX != x || selY != y)
4817 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4818 oldString = new char[len];
4819 UTF32toUTF8Len(&ch, 1, buffer, 4);
4820 newString = CopyString(buffer);
4821 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4823 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4824 if(selY < y || (selY == y && selX < x))
4826 replaceAction.x1 = selX;
4827 replaceAction.y1 = selY;
4828 replaceAction.x2 = x;
4829 replaceAction.y2 = y;
4833 replaceAction.x1 = x;
4834 replaceAction.y1 = y;
4835 replaceAction.x2 = selX;
4836 replaceAction.y2 = selY;
4838 Record(replaceAction);
4839 undoBuffer.dontRecord++;
4843 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4844 Record(addCharAction);
4850 DelSel(&addedSpaces);
4851 if(this.lineCount+1 > this.maxLines)
4854 Emptyline(this.lines.first,0);
4858 if(!(style.vScroll))
4860 // Make sure it fits, but we need a default line is this.font is too big for window
4861 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4864 if((this.y >= 0) && this.y < this.viewY)
4869 line = EditLine { };
4872 lines.Insert(this.line, line);
4873 line.editBox = this;
4879 // If we're displacing the lines from a current line ...
4882 if(this.line.buffer)
4885 if(this.line.count < endX) endX = this.line.count;
4886 length = this.line.count - endX;
4889 if(!line.AdjustBuffer(length))
4893 if(this.line.buffer)
4895 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4897 if(endX > 4000 || endX < 0)
4900 this.line.count = endX;
4901 this.line.buffer[this.line.count] = '\0';
4902 this.line.AdjustBuffer(this.line.count);
4903 ComputeLength(this.line);
4907 BufferLocation before = { this.line, this.y, this.x }, after;
4913 ComputeLength(this.line);
4916 if(length > 4000 || length < 0)
4919 line.count = length;
4923 line.buffer[line.count] = '\0';
4926 after.line = this.line, after.y = this.y, after.x = this.x;
4928 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4929 if(style.autoSize) AutoSize();
4935 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4936 DelSel(&addedSpaces);
4937 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4938 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4939 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4943 this.selLine = this.line;
4947 replaceAction.x3 = x;
4948 replaceAction.y3 = y;
4949 replaceAction.addedSpaces = addedSpaces;
4950 replaceAction.addedTabs = addedTabs;
4951 undoBuffer.dontRecord--;
4955 addCharAction.x -= addedTabs * (tabSize-1);
4956 addCharAction.addedSpaces = addedSpaces;
4957 addCharAction.addedTabs = addedTabs;
4964 /****************************************************************************
4966 ****************************************************************************/
4969 bool AddCh(unichar ch)
4971 return _AddCh(ch, null, null);
4976 this.modified = true;
4977 NotifyUpdate(master, this);
4978 modifiedDocument = true;
4981 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4984 DelCh(line1, y1, x1, line2, y2, x2, false);
4985 SetViewToCursor(true);
4993 itemEditUndo.disabled = undoBuffer.curAction == 0;
4994 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4995 if(savedAction == undoBuffer.curAction)
4997 modifiedDocument = false;
4999 NotifyUnsetModified(master, this);
5006 itemEditUndo.disabled = undoBuffer.curAction == 0;
5007 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5008 if(savedAction == undoBuffer.curAction)
5010 modifiedDocument = false;
5012 NotifyUnsetModified(master, this);
5016 void Record(UndoAction action)
5018 if(!undoBuffer.dontRecord)
5020 undoBuffer.Record(action);
5021 itemEditUndo.disabled = undoBuffer.curAction == 0;
5022 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5029 this.lines.first, 0,0,
5030 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5033 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5038 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5039 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5040 this.y = line2 ? y2 : (this.lineCount-1);
5041 this.line = line2 ? (EditLine)line2 : this.lines.last;
5044 SetViewToCursor(true);
5048 // TODO: Fix this vs modifiedDocument window property
5049 void SetModified(bool flag)
5053 this.modified = false;
5054 if(flag && !NotifyModified(master, this))
5055 this.modified = true;
5060 bool AddS(char * string)
5067 int addedSpaces = 0, addedTabs = 0;
5068 AddTextAction action = null;
5069 ReplaceTextAction replaceAction = null;
5071 this.pasteOperation = true;
5073 if(style.stuckCaret /*|EES_READONLY)*/ )
5076 if(!undoBuffer.dontRecord)
5078 char * placeString = CopyString(string);
5079 if(!style.multiLine)
5083 for(i = 0; (ch = placeString[i]); i++)
5086 placeString[i] = '\0';
5087 placeString = renew placeString byte[i+1];
5092 if(selX != x || selY != y)
5096 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5097 oldString = new char[len];
5098 newString = placeString;
5099 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5101 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5102 if(selY < y || (selY == y && selX < x))
5104 replaceAction.x1 = selX;
5105 replaceAction.y1 = selY;
5106 replaceAction.x2 = x;
5107 replaceAction.y2 = y;
5111 replaceAction.x1 = x;
5112 replaceAction.y1 = y;
5113 replaceAction.x2 = selX;
5114 replaceAction.y2 = selY;
5116 Record(replaceAction);
5120 if(string[0] == '\n')
5121 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5123 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5131 undoBuffer.dontRecord++;
5132 DelSel(&addedSpaces);
5136 for(c = 0; string[c]; c++)
5138 if(string[c] == '\n' || string[c] == '\r')
5140 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5145 if(string[c] == '\n')
5154 // Reset for next line
5158 if(string[c] == '\r' && *line == '\n')
5170 // Why was this here?
5173 // Add the line here
5175 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5181 undoBuffer.dontRecord--;
5186 action.addedSpaces = addedSpaces;
5187 action.addedTabs = addedTabs;
5189 else if(replaceAction)
5191 replaceAction.y3 = y;
5192 replaceAction.x3 = x;
5193 replaceAction.addedSpaces = addedSpaces;
5194 replaceAction.addedTabs = addedTabs;
5197 UpdateCaretPosition(true);
5199 this.pasteOperation = false;
5212 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5213 SetViewToCursor(true);
5219 void PutCh(unichar ch)
5223 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5224 //if((ch >= 32) || ch == '\n')
5226 int addedSpaces = 0, addedTabs = 0;
5227 ReplaceTextAction replaceAction = null;
5228 AddCharAction addCharAction = null;
5231 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5233 if(this.x < this.line.count && this.overwrite)
5238 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5239 oldString = new char[len];
5240 UTF32toUTF8Len(&ch, 1, buffer, 4);
5241 newString = CopyString(buffer);
5242 GetText(oldString, line, y, x, line, y, x+1, false, false);
5243 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5244 replaceAction.x1 = x;
5245 replaceAction.y1 = y;
5246 replaceAction.x2 = x+1;
5247 replaceAction.y2 = y;
5248 Record(replaceAction);
5250 undoBuffer.dontRecord++;
5251 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5252 undoBuffer.dontRecord--;
5254 else if(!undoBuffer.dontRecord)
5256 if(selX != x || selY != y)
5261 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5262 oldString = new char[len];
5263 UTF32toUTF8Len(&ch, 1, buffer, 4);
5264 newString = CopyString(buffer);
5265 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5266 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5267 if(selY < y || (selY == y && selX < x))
5269 replaceAction.x1 = selX;
5270 replaceAction.y1 = selY;
5271 replaceAction.x2 = x;
5272 replaceAction.y2 = y;
5276 replaceAction.x1 = x;
5277 replaceAction.y1 = y;
5278 replaceAction.x2 = selX;
5279 replaceAction.y2 = selY;
5281 Record(replaceAction);
5285 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5286 Record(addCharAction);
5289 undoBuffer.dontRecord++;
5290 result = _AddCh(ch, &addedSpaces, &addedTabs);
5293 replaceAction.x3 = x;
5294 replaceAction.y3 = y;
5295 replaceAction.addedSpaces = addedSpaces;
5296 replaceAction.addedTabs = addedTabs;
5300 addCharAction.x -= addedTabs * (tabSize-1);
5301 addCharAction.addedSpaces = addedSpaces;
5302 addCharAction.addedTabs = addedTabs;
5304 undoBuffer.dontRecord--;
5308 if(result) SetViewToCursor(true);
5312 void PutS(char * string)
5317 SetViewToCursor(true);
5322 void Printf(char * format, ...)
5326 char temp[MAX_F_STRING];
5328 va_start(args, format);
5329 vsnprintf(temp, sizeof(temp), format, args);
5330 temp[sizeof(temp)-1] = 0;
5336 void SetContents(char * format, ...)
5341 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5344 char temp[MAX_F_STRING];
5346 va_start(args, format);
5347 vsnprintf(temp, sizeof(temp), format, args);
5348 temp[sizeof(temp)-1] = 0;
5364 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5367 else if(this.line.prev)
5369 EditLine line = this.line.prev;
5373 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5381 this.selLine = this.line;
5386 SetViewToCursor(true);
5391 Emptyline(this.line,this.y);
5392 this.selX = this.x = 0;
5395 this.selLine = this.line;
5397 SetViewToCursor(true);
5407 SetViewToCursor(true);
5415 SetViewToCursor(true);
5419 bool GoToLineNum(int lineNum)
5424 EditLine line = this.lines.first;
5425 for(c = 0; c < lineNum && line; c++, line = line.next);
5435 SetViewToCursor(true);
5442 bool GoToPosition(EditLine line, int y, int x)
5454 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5468 SetViewToCursor(true);
5475 void SetViewToCursor(bool setCaret)
5485 bool dontScroll = false;
5491 selected = selX != this.x || selY != y;
5498 checkLine = dropLine;
5504 checkLine = this.line;
5509 numLines = clientSize.h / space.h;
5511 // This is broken. The EditBox now doesn't do anything different when adding to it,
5512 // regardless of the previous scrolling position. It should be read and then set again
5513 // if one wishes to preserve it.
5514 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5515 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5519 // Horizontal Adjustment
5520 if(!dontScroll && checkLine)
5524 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5527 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5533 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5534 viewX = x - clientSize.w+space.w;
5535 if(x < this.viewX + clientSize.w/2 - space.w)
5536 viewX = Max(0, x - clientSize.w/2 + space.w);
5544 // Vertical Adjustment
5545 if(viewY > checkY) viewY = checkY;
5546 if(viewY + numLines <= checkY)
5548 if(clientSize.h >= space.h)
5550 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5560 for(;dropLine && dropLine.prev && dropY >= numLines;)
5562 dropLine = dropLine.prev;
5566 for(;this.line && this.line.prev && this.y >= numLines;)
5568 this.line = this.line.prev;
5573 SetScrollPosition(viewX, viewY * this.space.h);
5575 UpdateCaretPosition(setCaret);
5581 selLine = this.line;
5591 void CenterOnCursor()
5593 int numLines = clientSize.h / this.space.h;
5594 int y = this.y - numLines / 2;
5596 bool figureSyntax = false;
5597 EditLine oldViewLine = viewLine;
5598 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5603 for(;y < this.viewY; y++)
5605 this.viewLine = this.viewLine.prev;
5606 style.recomputeSyntax = true;
5608 for(;y > this.viewY; y--)
5610 this.viewLine = this.viewLine.next;
5611 figureSyntax = true;
5614 FigureStartSyntaxStates(oldViewLine, false);
5618 SetScrollPosition(this.viewX, viewY * this.space.h);
5619 UpdateCaretPosition(true);
5623 void SetCursorToView()
5634 numLines = clientSize.h / this.space.h;
5638 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5639 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5643 EditLine oldLine = this.line;
5644 bool lastOne = false;
5645 EditLine oldViewLine = this.viewLine;
5646 bool figureSyntax = false;
5648 if(this.y >= this.lineCount-1) return;
5650 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5652 if(this.viewY + numLines < this.lines.count)
5654 this.viewLine = this.viewLine.next;
5656 figureSyntax = true;
5658 else if(c && !lastOne)
5667 FigureStartSyntaxStates(oldViewLine, false);
5668 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5671 SetViewToCursor(false);
5680 if(this.y == 0) return;
5682 numLines = clientSize.h / this.space.h;
5686 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5687 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5691 EditLine oldLine = this.line;
5693 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5698 if(this.viewLine.prev)
5700 this.viewLine = this.viewLine.prev;
5702 style.recomputeSyntax = true;
5706 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5709 SetViewToCursor(false);
5716 if(this.viewLine.prev)
5719 // this.viewLine = this.viewLine.prev;
5722 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5729 if(this.viewLine.next)
5732 // this.viewLine = this.viewLine.next;
5735 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5742 EditLine l1, l2, line;
5743 int x1, x2, nx1, nx2;
5748 if(!this.selLine) return 0;
5749 if(this.selLine == this.line && this.selX == this.x) return 0;
5750 if(this.selY < this.y)
5757 else if(this.selY > this.y)
5764 else if(this.selX < this.x)
5766 l1 = l2 = this.line;
5772 l1 = l2 = this.line;
5776 nx1 = Min(x1,l1.count);
5777 nx2 = Min(x2,l2.count);
5779 // Find Number of Bytes Needed
5781 for(line = l1; line; line = line.next)
5783 if(line == l1) start = nx1; else start = 0;
5784 if(line == l2) end = nx2; else end = line.count;
5786 if(style.freeCaret && line == l2)
5789 count = Max(x2-Max(x1,l1.count),0);
5791 count = Max(x2-l2.count,0);
5795 if(line == l2) break;
5796 // Add Carriage Return / line Feed
5803 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5807 if(!reorder || this.selY < this.y)
5809 if(l1) *l1 = this.selLine;
5810 if(y1) *y1 = this.selY;
5811 if(x1) *x1 = this.selX;
5812 if(l2) *l2 = this.line;
5813 if(y2) *y2 = this.y;
5814 if(x2) *x2 = this.x;
5816 else if(this.selY > this.y)
5818 if(l1) *l1 = this.line;
5819 if(y1) *y1 = this.y;
5820 if(x1) *x1 = this.x;
5821 if(l2) *l2 = this.selLine;
5822 if(y2) *y2 = this.selY;
5823 if(x2) *x2 = this.selX;
5825 else if(this.selX < this.x)
5827 if(l1) *l1 = this.line;
5828 if(y1) *y1 = this.selY;
5829 if(x1) *x1 = this.selX;
5830 if(l2) *l2 = this.line;
5831 if(y2) *y2 = this.y;
5832 if(x2) *x2 = this.x;
5836 if(l1) *l1 = this.line;
5837 if(y1) *y1 = this.y;
5838 if(x1) *x1 = this.x;
5839 if(l2) *l2 = this.line;
5840 if(y2) *y2 = this.selY;
5841 if(x2) *x2 = this.selX;
5846 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5848 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5850 this.selLine = (EditLine)l1;
5853 this.line = (EditLine)l2;
5857 SetViewToCursor(true);
5861 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5863 EditLine l1, l2, line;
5864 int x1, x2, nx1, nx2;
5895 nx1 = Min(x1,l1.count);
5896 nx2 = Min(x2,l2.count);
5899 for(line = l1; line; line = line.next)
5901 if(line == l1) start = nx1; else start = 0;
5902 if(line == l2) end = nx2; else end = line.count;
5905 CopyBytes(text, line.buffer + start, end - start);
5908 numChars += end-start;
5910 if(style.freeCaret && line == l2 && addSpaces)
5913 count = Max(x2-Max(x1,l1.count),0);
5915 count = Max(x2-l2.count,0);
5918 FillBytes(text,' ',count);
5924 if(line == l2) break;
5936 // '\0' terminate Terminate
5943 void GetSel(char * text, bool addCr)
5945 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5951 this.selLine = this.line;
5961 int size = SelSize();
5964 // Try to allocate memory
5965 ClipBoard clipBoard { };
5966 if(clipBoard.Allocate(size+1))
5968 GetSel(clipBoard.memory, true);
5981 ClipBoard clipBoard { };
5982 if(clipBoard.Load())
5983 PutS(clipBoard.memory);
5995 SetViewToCursor(true);
6001 void DeleteSelection()
6006 SetViewToCursor(true);
6012 void Save(File f, bool cr)
6015 savedAction = undoBuffer.curAction;
6017 for(line = this.lines.first; line; line = line.next)
6019 f.Write(line.buffer, line.count,1);
6022 if(cr) f.Putc('\r');
6028 #define BUFFER_SIZE 16384
6031 undoBuffer.dontRecord++;
6034 char buffer[BUFFER_SIZE];
6038 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6039 buffer[count] = '\0';
6045 undoBuffer.dontRecord--;
6046 undoBuffer.count = 0;
6047 undoBuffer.curAction = 0;
6048 itemEditUndo.disabled = undoBuffer.curAction == 0;
6049 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6052 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6056 bool firstPass = true;
6057 EditBoxFindResult result = found;
6059 if(!this.line) return notFound;
6062 for(line = this.line;;)
6070 line = this.lines.first;
6076 line = this.lines.last;
6077 num = this.lineCount - 1;
6083 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6085 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6089 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6092 if(line == this.line && !firstPass) break;
6110 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6114 int searchLen = strlen(text);
6115 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6117 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6118 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6120 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6127 bool OnKeyDown(Key key, unichar ch)
6130 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6132 if(!NotifyKeyDown(master, this, key, ch))
6142 this.mouseMove = false;
6143 OnLeftButtonUp(0,0,0);
6144 SetViewToCursor(true);
6154 public class EditBoxStream : File
6157 BufferLocation start, sel;
6164 EditBox editBox = this.editBox;
6166 editBox.x = start.x;
6167 editBox.y = start.y;
6168 editBox.line = start.line;
6170 editBox.selX = sel.x;
6171 editBox.selY = sel.y;
6172 editBox.selLine = sel.line;
6174 editBox.SetViewToCursor(true);
6175 //editBox.ComputeColumn();
6179 property EditBox editBox
6187 start.line = value.line;
6191 sel.line = value.selLine;
6194 value.GoToHome(true);
6197 get { return editBox; }
6200 uint Read(byte * buffer, uint size, uint count)
6203 EditBox editBox = this.editBox;
6204 EditLine line = editBox.line;
6210 for(;read < count && line; line = (*&line.next))
6212 int numBytes = Min(count - read, (*&line.count) - x);
6215 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6220 for(;read < count && x < (*&line.count);)
6222 buffer[read++] = (*&line.buffer)[x++];
6229 buffer[read++] = '\n';
6234 if(x == (*&line.count) && (*&line.next))
6243 editBox.line = editBox.selLine = line;
6244 editBox.x = editBox.selX = x;
6245 editBox.y = editBox.selY = y;
6250 bool Seek(int pos, FileSeekMode mode)
6253 EditBox editBox = this.editBox;
6254 EditLine line = editBox.line;
6256 if(mode == FileSeekMode::start)
6258 pos = pos - this.pos;
6268 for(;read < pos && line; line = (*&line.next))
6270 int numBytes = Min(pos - read, (*&line.count) - x);
6273 read += numBytes; x += numBytes;
6276 /*for(;read < pos && x < (*&line.count);)
6304 for(;read < pos && line; line = (*&line.prev))
6306 int numBytes = Min(pos - read, x);
6312 /*for(;read < pos && x > 0;)
6322 x = (*&(*&line.prev).count);
6338 editBox.line = editBox.selLine = line;
6339 editBox.x = editBox.selX = x;
6340 editBox.y = editBox.selY = y;
6345 bool Puts(char * string)
6347 EditBox editBox = this.editBox;
6348 BufferLocation start { editBox.line, editBox.y, editBox.x };
6352 editBox.AddS(string);
6354 pos.line = editBox.line;
6358 this.start.AdjustAdd(start, pos);
6359 sel.AdjustAdd(start, pos);
6364 // NOTE: BYTE, NOT UNICODE CHARACTER!
6367 EditBox editBox = this.editBox;
6368 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6373 utf8Bytes[numBytes++] = ch;
6374 utf8Bytes[numBytes] = 0;
6375 if(UTF8Validate(utf8Bytes))
6377 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6379 pos.line = editBox.line;
6382 this.start.AdjustAdd(start, pos);
6383 sel.AdjustAdd(start, pos);
6390 bool Getc(char * ch)
6392 return Read(ch, 1, 1) ? true : false;
6395 void DeleteBytes(uint count)
6397 EditBox editBox = this.editBox;
6400 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6401 BufferLocation end = pos;
6406 for(;c < count && end.line && end.x < end.line.count;)
6411 if(c < count && end.line && end.line.next)
6413 end.line = end.line.next;
6422 start.AdjustDelete(pos, end);
6423 sel.AdjustDelete(pos, end);
6425 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);