1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 public class SyntaxColorScheme
18 Color charLiteralColor;
19 Color stringLiteralColor;
20 Color preprocessorColor;
22 private Array<Color> keywordColors { };
24 public property Container<Color> keywordColors
26 set { keywordColors.Copy((void *)value); }
27 get { return keywordColors; }
35 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
37 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
39 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
41 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
42 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
43 #define UTF8_GET_CHAR(string, numBytes) \
47 byte b = (string)[0]; \
52 if(b & 0x80 && b & 0x40) \
67 for(i = 0; i<numBytes; i++) \
70 ch |= (string)[i] & mask; \
78 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
79 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
82 bool inMultiLineComment:1, inPrep:1, escaped:1;
84 bool recomputeSyntax:1;
85 bool cursorFollowsView:1;
87 // bool lineNumbers:1;
91 void UnregisterClass_EditBox()
94 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
103 extern int __ecereVMethodID_class_OnFree;
106 static class ArrayImpl
113 public class OldArray
120 void ** array = (void **)((ArrayImpl)this).array;
121 if(type.type == normalClass || type.type == noHeadClass)
123 for(c = 0; c<size; c++)
124 type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
126 // TODO: Call OnFree for structClass
127 delete ((ArrayImpl)this).array;
136 if(((ArrayImpl)this).array)
140 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
143 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
152 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
157 public class UndoAction : struct
160 subclass(UndoAction) type;
161 virtual void Undo(void * data) { type.Undo(this, data); }
162 virtual void Redo(void * data) { type.Redo(this, data); }
164 virtual void Print(void * data) { type.Print(this, data); }
168 if(((Class)type).Destructor)
169 ((void (*)(void *))((Class)type).Destructor)(this);
174 class ArrayUndoActions : OldArray
176 type = class(UndoAction);
180 public class UndoBuffer
182 ArrayUndoActions actions { size = 8 };
197 UndoAction action = actions._[--curAction];
199 /*Print("Undoing: ");
200 action.Print(data);*/
211 if(curAction < count)
213 UndoAction action = actions._[curAction];
216 /*Print("Redoing: ");
217 action.Print(data);*/
225 void Record(UndoAction action)
227 if(!dontRecord && !insideRedo)
229 if(curAction < count)
232 for(c = curAction; c < count; c++)
238 if(count >= actions.size)
239 actions.size += actions.size / 2;
242 /*Print("Recording: ");
243 action.Print(data);*/
245 actions._[count++] = action;
248 if(actions.size > count + count / 2 && count + count / 2 >= 8)
249 actions.size = count + count / 2;
256 static class AddCharAction : UndoAction
260 int addedSpaces, addedTabs;
261 type = class(AddCharAction);
263 void Undo(EditBox editBox)
265 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
267 if(addedTabs || addedSpaces)
268 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
269 editBox.UpdateDirty();
272 void Redo(EditBox editBox)
274 editBox.GoToPosition(null, y, x);
276 editBox.UpdateDirty();
279 void Print(EditBox editBox)
281 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
286 static class AddTextAction : UndoAction
290 int addedSpaces, addedTabs;
291 type = class(AddTextAction);
294 void Print(EditBox editBox)
296 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
304 void Undo(EditBox editBox)
309 editBox.GoToPosition(null, y1, x1);
311 l2 = editBox.lines.first;
312 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
314 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
315 if(addedTabs || addedSpaces)
316 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
318 editBox.SetViewToCursor(true);
322 void Redo(EditBox editBox)
324 editBox.GoToPosition(null, y1, x1);
325 editBox.PutS(string);
329 static class DelTextAction : UndoAction
335 type = class(DelTextAction);
338 void Print(EditBox editBox)
340 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
343 void Undo(EditBox editBox)
345 editBox.GoToPosition(null, y1, x1);
346 editBox.PutS(string);
350 editBox.GoToPosition(null, y1, x1);
353 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
354 //editBox.SetViewToCursor(true);
357 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
363 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
364 //editBox.SetViewToCursor(true);
367 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
371 void Redo(EditBox editBox)
376 editBox.GoToPosition(null, y1, x1);
379 l2 = editBox.lines.first;
380 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
382 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
383 editBox.SetViewToCursor(true);
393 static class ReplaceTextAction : UndoAction
395 int y1, x1, y2, x2, y3, x3;
399 int addedSpaces, addedTabs;
401 type = class(ReplaceTextAction);
404 void Print(EditBox editBox)
406 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
409 void Undo(EditBox editBox)
414 editBox.GoToPosition(null, y1, x1);
416 l3 = editBox.lines.first;
417 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
419 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
421 editBox.PutS(oldString);
422 if(addedSpaces || addedTabs)
423 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
425 //editBox.PutS(oldString);
428 editBox.GoToPosition(null, y1, x1);
431 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
437 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
441 void Redo(EditBox editBox)
446 editBox.GoToPosition(null, y1, x1);
448 l2 = editBox.lines.first;
449 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
451 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
453 editBox.PutS(newString);
463 static class MoveTextAction : UndoAction
465 int fy1, fx1, fy2, fx2;
467 type = class(MoveTextAction);
469 void Undo(EditBox editBox)
474 void Redo(EditBox editBox)
480 public class EditLine : struct
492 // Only works for single line edit for now...
493 EditBox editBox = this.editBox;
497 get { return this ? buffer : null; }
499 property EditLine prev { get { return this ? prev : null; } };
500 property EditLine next { get { return this ? next : null; } };
501 property int count { get { return this ? count : 0; } };
508 // This makes sure the buffer always contains at least count characters
509 // Keeps a count/2 pad for avoiding always reallocating memory.
510 bool AdjustBuffer(int count)
518 newSize = (count + (count >> 1));
520 // Shrink down the buffer
523 buffer = new char[newSize];
524 if(!buffer) return false;
528 CopyBytes(buffer, this.buffer, count);
531 this.buffer = buffer;
535 // Increase the buffer
536 else if(size < count)
538 buffer = new char[newSize];
539 if(!buffer) return false;
543 CopyBytes(buffer, this.buffer, this.count + 1); // size);
546 this.buffer = buffer;
555 public struct BufferLocation
560 void AdjustDelete(BufferLocation start, BufferLocation end)
562 // Location is before, nothing to do
563 if(y < start.y || (y == start.y && x < start.x))
565 // Location is inside deleted bytes, point to the start
566 if((y >= start.y && (y > start.y || x >= start.x)) &&
567 (y >= end.y && (y > end.y || x >= end.x)))
572 // Location is on another line
574 y -= end.y - start.y;
575 // Location is the last touched line
582 //if(start.line == end.line)
583 x -= end.x - start.x;
592 // Assuming no carriage return before first character ???? fixed?
593 void AdjustAdd(BufferLocation start, BufferLocation end)
595 int numLines = end.y - start.y;
602 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
605 for(c = 0, line = start.line; c<numLines; c++)
608 //x += numLines ? end.x : (end.x - start.x);
609 x += end.x - start.x;
616 public enum EditBoxFindResult { notFound, found, wrapped };
618 static char * keyWords1[] =
621 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
622 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
624 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
625 "__attribute__", "__stdcall", "_stdcall",
626 "__declspec", "goto",
627 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
628 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
631 "class", "private", "public",
633 "delete", "new", "new0", "renew", "renew0", "define",
636 "dllexport", "dllimport", "stdcall",
637 "subclass", "__on_register_module", "namespace", "using",
638 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
639 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
640 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
643 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
646 "this", "true", "false", "null", "value",
655 static char * keyWords2[] =
657 "defined", "warning", null
660 static char ** keyWords[] = { keyWords1, keyWords2 };
661 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
662 //static int * keyLen[NUM_KEYWORD_GROUPS];
663 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
666 static FileFilter filters[] =
668 { "All files", null },
670 "Text Files (*.txt)",
674 static FileListConfig fileListConfig = { "", "", filters, sizeof(filters), null, 0 };
676 static char searchString[1025], replaceString[1025];
677 static bool matchCase = false, wholeWord = false, searchUp = false;
679 static GoToDialog goToDialog
681 autoCreate = false, isModal = true, text = $"Go To"
684 public class EditBox : CommonControl
686 class_property(icon) = "<:ecere>controls/editBox.png";
690 virtual bool Window::NotifyModified(EditBox editBox);
691 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
692 virtual void Window::NotifyUpdate(EditBox editBox);
693 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
694 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
695 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
697 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
698 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
699 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
701 virtual bool Window::NotifyUnsetModified(EditBox editBox);
703 // Why was this commented out?
704 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
705 // and the scrollArea property isn't compared properly either.
709 if(font) ComputeFont();
710 SetInitSize(initSize);
716 style.vScroll = true;
722 style.hScroll = true;
726 property bool textHorzScroll { property_category "Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
727 property bool textVertScroll { property_category "Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
728 property bool readOnly
730 property_category "Behavior"
733 style.readOnly = value;
734 itemEditCut.disabled = value || !selection;
735 itemEditDelete.disabled = value || !selection;
736 itemEditPaste.disabled = value;
738 get { return style.readOnly; }
740 property bool multiLine { property_category "Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
741 property bool freeCaret { property_category "Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
742 property bool tabKey { property_category "Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
743 property int tabSize { property_category "Behavior" set { tabSize = value; } get { return tabSize; } };
744 property bool tabSelection { property_category "Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
745 property bool smartHome { property_category "Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
746 property bool autoEmpty { property_category "Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
747 property bool noCaret { property_category "Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
748 property int maxLineSize { property_category "Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
749 property int maxNumLines { property_category "Behavior" set { maxLines = value; } get { return maxLines; } };
750 property bool useTab { property_category "Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
751 property bool syntaxHighlighting { property_category "Appearance" set { style.syntax = value; } get { return style.syntax; } };
752 property bool noSelect { property_category "Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
753 property bool allCaps { property_category "Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
754 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
755 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
756 property int numLines { get { return this ? lineCount : 0; } };
757 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
758 property int column { get { return col; } }; // TODO: Add Set
759 property int charPos { get { return x; } }; // TODO: Add Set
760 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
761 property EditLine lastLine { get { return lines.last; } };
762 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
763 property char * contents
765 property_category "Data"
771 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
774 //SetViewToCursor(true);
782 char * buffer = null;
785 /* Can't implement this right now because of memory leak... Need string reference counting...
792 for(line = lines.first; line; line = line.next)
793 len += strlen(line.buffer);
795 buffer = new char[len+1];
797 for(line = lines.first; line; line = line.next)
799 int lineLen = strlen(line.buffer);
800 memcpy(buffer + len, line.buffer, lineLen);
806 buffer = this.line ? this.line.buffer : null;
811 property bool overwrite { get { return overwrite; } };
812 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
814 property char * multiLineContents
818 char * buffer = null;
825 for(line = lines.first; line; line = line.next)
826 len += strlen(line.buffer)+1;
828 buffer = new char[len+1];
830 for(line = lines.first; line; line = line.next)
832 int lineLen = strlen(line.buffer);
833 memcpy(buffer + len, line.buffer, lineLen);
835 if(line.next) buffer[len++] = '\n';
848 return this.line.buffer;
853 void SetLineText(char * text)
857 EditLine_SetText(this.line, text);
861 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
862 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
863 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
865 // selectionStart.line, selectionStart.column (With Set)
866 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
880 // Position of Caret (Not necessarily displayed position)
883 // Position of beginning of block (Block ends at (x,y))
885 // line is line at carret, selLine is line at beginning of block
886 EditLine line, selLine, dropLine;
887 // Mouse selection Moving virtual caret
892 // ViewX is x offset in pixels, ViewY is y offset in lines
894 // viewLine is first displayed line
897 // start and end of area to redraw
900 // MaxLine is the longest line
902 // MaxLength is the longest line's length
905 // MouseSelect is true once button is pressed, overwrite is true if mode is on
906 bool mouseSelect, mouseMove, overwrite, wordSelect;
907 // Timer is used for mouse selection scrolling
910 window = this, delay = 0.1;
919 OnMouseMove(mouseX, mouseY, -1);
924 // (mouseX,mouseY) is the position of the mouse in the edit box
929 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
932 bool rightButtonDown;
935 UndoBuffer undoBuffer { data = this };
937 Color selectionColor, selectionText;
938 SyntaxColorScheme colorScheme { };
943 Menu editMenu { menu, $"Edit", e };
946 editMenu, $"Cut\tCtrl+X", t, disabled = true;
948 bool NotifySelect(MenuItem item, Modifiers mods)
950 if(!(style.readOnly))
955 MenuItem itemEditCopy
957 editMenu, $"Copy\tCtrl+C", c, disabled = true;
959 bool NotifySelect(MenuItem item, Modifiers mods)
965 MenuItem itemEditPaste
967 editMenu, $"Paste\tCtrl+V", p;
969 bool NotifySelect(MenuItem item, Modifiers mods)
971 if(!(style.readOnly))
976 MenuItem itemEditDelete
978 editMenu, $"Delete\tDel", d, disabled = true;
980 bool NotifySelect(MenuItem item, Modifiers mods)
982 if(!(style.readOnly))
987 MenuDivider { editMenu };
988 MenuItem itemEditSelectAll
990 editMenu, $"Select All\tCtrl+A", a;
992 bool NotifySelect(MenuItem item, Modifiers mods)
998 MenuDivider { editMenu };
999 MenuItem itemEditUndo
1001 editMenu, $"Undo\tCtrl+Z", u;
1004 bool NotifySelect(MenuItem item, Modifiers mods)
1010 MenuItem itemEditRedo
1012 editMenu, $"Redo\tCtrl+Y", o;
1015 bool NotifySelect(MenuItem item, Modifiers mods)
1021 MenuDivider { editMenu };
1024 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1026 bool NotifySelect(MenuItem item, Modifiers mods)
1029 Find(searchString, wholeWord, matchCase, false);
1031 itemEditFind.NotifySelect(this, item, mods);
1037 editMenu, $"Find Next\tF3", n, f3;
1039 bool NotifySelect(MenuItem item, Modifiers mods)
1042 Find(searchString, wholeWord, matchCase, true);
1044 itemEditFind.NotifySelect(this, item, mods);
1048 MenuItem itemEditFind
1050 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1052 bool NotifySelect(MenuItem item, Modifiers mods)
1056 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1057 searchUp = searchUp;
1060 // Fix dialog from above shouldn't be visible
1061 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1062 void NotifyDestroyed(Window window, DialogResult result)
1064 FindDialog dialog = (FindDialog) window;
1065 searchUp = dialog.searchUp;
1066 strcpy(searchString, dialog.searchString);
1067 matchCase = dialog.matchCase;
1068 wholeWord = dialog.wholeWord;
1077 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1079 bool NotifySelect(MenuItem item, Modifiers mods)
1081 ReplaceDialog dialog
1085 searchString = searchString,
1086 replaceString = replaceString,
1087 matchCase = matchCase,
1088 wholeWord = wholeWord,
1092 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1093 void NotifyDestroyed(Window window, DialogResult result)
1095 ReplaceDialog dialog = (ReplaceDialog)window;
1096 char * replace = dialog.replaceString;
1098 strcpy(replaceString, replace);
1099 strcpy(searchString, dialog.searchString);
1100 matchCase = dialog.matchCase;
1101 wholeWord = dialog.wholeWord;
1108 MenuDivider { editMenu };
1111 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1113 bool NotifySelect(MenuItem item, Modifiers mods)
1115 goToDialog.editBox = this;
1116 goToDialog.master = master;
1117 goToDialog.Create();
1121 MenuDivider { editMenu };
1122 MenuItem itemEditInsertTab
1124 editMenu, $"Insert Tabs", i, checkable = true;
1126 bool NotifySelect(MenuItem item, Modifiers mods)
1128 style.useTab = item.checked;
1134 snapVertScroll = true;
1135 snapHorzScroll = true;
1139 static bool syntaxInit = false;
1144 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1146 for(c = 0; keyWords[g][c]; c++);
1147 //keyLen[g] = new int[c];
1148 for(c = 0; keyWords[g][c]; c++)
1150 keyLen[g][c] = strlen(keyWords[g][c]);
1154 colorScheme.commentColor = dimGray;
1155 colorScheme.charLiteralColor = crimson;
1156 colorScheme.stringLiteralColor = crimson;
1157 colorScheme.preprocessorColor = green;
1158 colorScheme.numberColor = teal;
1159 colorScheme.keywordColors = [ blue, blue ];
1162 FontExtent = Display::FontExtent;
1164 lines.offset = (uint)&((EditLine)0).prev;
1166 style = EditBoxBits { hScroll = true };
1168 // cursor = guiApp.GetCursor(IBeam);
1170 // Default Properties
1172 maxLineSize = 1024;*/
1175 maxLineSize = MAXINT;
1180 mouseSelect = this.mouseMove = false;
1183 x = selX = selY = 0;
1186 line = selLine = null;
1192 endY = clientSize.h;
1195 // Default space to 8 in case window is not constructed
1198 undoBuffer.dontRecord++;
1200 undoBuffer.dontRecord--;
1202 viewLine = lines.first;
1203 style.recomputeSyntax = true;
1209 UpdateCaretPosition(true);
1215 lines.Free(EditLine::Free);
1218 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1220 int count = wc - *renderStart;
1223 if(y + space.h >= box.top && y <= box.bottom)
1229 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1230 surface.TextFont(font);
1231 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1232 if(*x + w + XOFFSET > 0)
1233 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1238 w = numSpaces; // * space.w;
1239 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1240 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1241 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1249 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1250 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1256 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1260 *foreground = (*selected) ? selectionForeground : textColor;
1261 *background = selectionBackground;
1262 *opacity = *selected;
1269 if(this.overwrite && active)
1271 if((style.stuckCaret && wc == line.count && !line.next) ||
1272 (!mouseMove && line == this.line && wc == editX))
1281 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1285 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1286 bool inString = false;
1287 bool inQuotes = false;
1288 bool inPrep = reset ? false : style.inPrep;
1289 bool inSingleLineComment = false;
1290 bool escaped = reset ? false : style.escaped;
1291 bool continuedSingleLineComment = false;
1293 EditLine line = reset ? lines.first : firstLine;
1294 // int maxBackUp = 1000, c;
1295 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1296 for(; line != viewLine; line = line.next)
1298 char * text = line.buffer;
1301 bool lastWasStar = false;
1302 bool firstWord = true;
1303 if(!escaped) inPrep = false;
1304 inSingleLineComment = continuedSingleLineComment;
1310 for(c = 0; (ch = text[c]); c++)
1312 bool wasEscaped = escaped;
1313 bool backLastWasStar = lastWasStar;
1315 lastWasStar = false;
1318 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1320 if(text[c+1] == '/')
1322 inSingleLineComment = true;
1324 else if(text[c+1] == '*')
1326 inMultiLineComment = true;
1329 else if(backLastWasStar)
1330 inMultiLineComment = false;
1334 if(!c || text[c-1] != '/') lastWasStar = true;
1336 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && ch == '\"')
1338 if(inString && !wasEscaped)
1347 else if(!inSingleLineComment && !inMultiLineComment && !inString && ch == '\'')
1349 if(inQuotes && !wasEscaped)
1363 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && ch == '#')
1375 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1377 style.inMultiLineComment = inMultiLineComment;
1378 style.inPrep = inPrep;
1379 style.escaped = escaped;
1383 /*void OnDrawOverChildren(Surface surface)
1385 if(style.lineNumbers)
1387 int currentLineNumber = this.viewY + 1;
1390 for(i = 0; i * space.h < box.height; i++)
1392 // ********* LINE NUMBERING *********
1393 surface.SetForeground(Color{60, 60, 60});
1394 //Highlight current line
1395 if(this.caretY / space.h == currentLineNumber - 1)
1396 surface.SetBackground(Color{220, 220, 220});
1398 surface.SetBackground(Color{230, 230, 230});
1399 surface.textOpacity = true;
1401 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1402 if(currentLineNumber > this.lineCount)
1403 surface.WriteText(0,i*space.h+1," ",6);
1405 surface.WriteText(0,i*space.h+1,lineText,6);
1407 currentLineNumber++;
1412 void OnRedraw(Surface surface)
1416 bool selected = false, selection = true;
1418 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1419 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1420 Color defaultTextColor = property::foreground;
1423 int maxW = clientSize.w;
1425 Color foreground, background;
1428 // Overwrite Caret Stuff
1429 bool overWrite = false;
1430 int overWriteX, overWriteY;
1433 // ****** SYNTAX STATES ******
1434 bool inMultiLineComment = style.inMultiLineComment;
1435 bool inString = false;
1436 bool inQuotes = false;
1437 bool inPrep = style.inPrep;
1438 bool inSingleLineComment = false;
1439 bool escaped = style.escaped;
1440 bool continuedSingleLineComment = false;
1441 // ****** ************* ******
1444 defaultTextColor = Color { 85, 85, 85 };
1445 textColor = defaultTextColor;
1448 Abs(selectionBackground.r - property::background.r) +
1449 Abs(selectionBackground.g - property::background.g) +
1450 Abs(selectionBackground.b - property::background.b) < 92)
1452 selectionBackground = activeBorder;
1453 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1456 surface.TextFont(this.font);
1458 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1459 surface.Area(0,0,MAXINT,MAXINT);
1464 surface.SetBackground(activeBorder);
1465 surface.Area(0,0,clientSize.w, clientSize.h);
1468 if(this.selX == this.x && this.selY == this.y)
1478 editX = Min(this.x,this.line.count);
1479 selX = Min(this.selX,this.selLine.count);
1482 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1484 foreground = selected ? selectionForeground : textColor;
1485 background = selectionBackground;
1491 surface.SetForeground(foreground);
1492 surface.SetBackground(background);
1493 surface.TextOpacity(opacity);
1495 surface.GetBox(box);
1497 for(line = this.viewLine; line; line = line.next)
1499 int x = -this.viewX;
1503 Color newTextColor = textColor = defaultTextColor;
1504 bool lineComplete = false;
1507 // ****** SYNTAX HIGHLIGHTING ******
1508 bool lastWasStar = false;
1509 bool firstWord = true;
1510 if(!escaped) inPrep = false;
1511 inSingleLineComment = continuedSingleLineComment;
1515 // *********************************
1517 /* === DEBUGGING TOOL FOR MAXLINE ===
1519 if(line == this.maxLine)
1521 surface.SetBackground(GREEN|0xFF000000);
1522 surface.TextOpacity(true);
1526 surface.TextOpacity(selected ? true : false);
1527 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1531 if(line == this.selLine && line == this.line)
1533 end = Max(line.count, this.x);
1534 end = Max(end, this.selX);
1536 else if(line == this.selLine)
1537 end = Max(line.count, this.selX);
1538 else if(line == this.line)
1539 end = Max(line.count, this.x);
1547 bool spacing = true;
1548 bool cantHaveWords = false;
1555 lineComplete = false;
1558 textColor = newTextColor;
1560 surface.SetForeground(textColor);
1563 for(; c<end && !cantHaveWords;)
1567 bufferLen += wordLen;
1571 /*if(line.count > 4000)
1578 for(; c<line.count; c++)
1580 unichar ch = line.buffer[c];
1581 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1582 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1583 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1584 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1585 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1593 for(; c<line.count; c++)
1595 unichar ch = line.buffer[c];
1596 if(ch == '\t' || ch == ' ')
1598 cantHaveWords = true;
1602 if(ch != ' ' && ch != '\t')
1607 if(c == line.count && c < end)
1616 cantHaveWords = true;
1621 lastWasStar = false;
1627 bool backEscaped = escaped;
1628 bool backLastWasStar = lastWasStar;
1629 bool backInMultiLineComment = inMultiLineComment;
1630 bool backInString = inString;
1631 bool backInQuotes = inQuotes;
1632 bool backInPrep = inPrep;
1633 bool backInSingleLineComment = inSingleLineComment;
1635 char * word = line.buffer + c - wordLen;
1637 bool wasEscaped = escaped;
1639 lastWasStar = false;
1641 // Determine Syntax Highlighting
1642 newTextColor = defaultTextColor;
1645 if(inSingleLineComment || inMultiLineComment)
1647 newTextColor = colorScheme.commentColor;
1651 newTextColor = colorScheme.charLiteralColor;
1655 newTextColor = colorScheme.stringLiteralColor;
1659 newTextColor = colorScheme.preprocessorColor;
1661 if(wordLen == 1 && word[0] == '/')
1663 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1667 inSingleLineComment = true;
1668 newTextColor = colorScheme.commentColor;
1670 else if(word[1] == '*')
1672 inMultiLineComment = true;
1673 newTextColor = colorScheme.commentColor;
1676 else if(backLastWasStar)
1677 inMultiLineComment = false;
1679 else if(wordLen == 1 && word[0] == '*')
1681 if(!c || word[-1] != '/')
1684 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1686 if(inString && !wasEscaped)
1693 newTextColor = colorScheme.stringLiteralColor;
1696 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1698 if(inQuotes && !wasEscaped)
1703 newTextColor = colorScheme.charLiteralColor;
1706 else if(wordLen == 1 && word[0] == '\\')
1711 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1712 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1714 newTextColor = colorScheme.numberColor;
1718 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1723 newTextColor = colorScheme.preprocessorColor;
1726 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1728 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1730 char ** keys = keyWords[g];
1731 int * len = keyLen[g];
1732 for(ccc = 0; keys[ccc]; ccc++)
1734 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1736 newTextColor = colorScheme.keywordColors[g];
1745 // If highlighting for this word is different, break
1746 if(newTextColor != textColor)
1750 // Better solution than going back?
1753 // Reset syntax flags
1754 escaped = backEscaped;
1755 lastWasStar = backLastWasStar;
1756 inMultiLineComment = backInMultiLineComment;
1757 inString = backInString;
1758 inQuotes = backInQuotes;
1759 inPrep = backInPrep;
1760 inSingleLineComment = backInSingleLineComment;
1765 textColor = newTextColor;
1767 surface.SetForeground(textColor);
1773 // If we're not breaking, this can't be rendered as spacing anymore
1776 // Do word wrapping here
1782 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1787 w += numSpaces * space.w;
1789 if(x + viewX + w > maxW)
1792 lineComplete = true;
1797 bufferLen += wordLen;
1802 int renderStart = start;
1807 // Render checking if we need to split because of selection or to find where to draw insert caret
1808 for(wc = start; wc < start + bufferLen; wc++)
1810 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1811 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1812 if(overWrite == true)
1814 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1815 if(overWriteCh == '\t') overWriteCh = ' ';
1820 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1821 if(overWrite == true)
1829 surface.TextOpacity(opacity);
1830 surface.SetBackground(background);
1831 surface.SetForeground(foreground);
1838 if(wc < line.count && line.buffer[wc] == '\t')
1840 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1844 numSpaces += space.w;
1848 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1853 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1854 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1856 if(overWrite == true)
1863 surface.TextOpacity(opacity);
1864 surface.SetBackground(background);
1865 surface.SetForeground(foreground);
1868 if(style.freeCaret && selected)
1870 surface.SetBackground(selectionBackground);
1871 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1872 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1877 if(style.freeCaret && selected)
1879 surface.SetBackground(selectionBackground);
1880 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1884 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1887 if(y > box.bottom) // >=clientSize.h)
1893 surface.TextOpacity(true);
1894 surface.SetForeground(black);
1895 surface.SetBackground(Color {255,255,85});
1896 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1900 void FixScrollArea()
1902 if(style.hScroll || style.vScroll)
1904 int width = maxLength + XOFFSET;
1905 int height = lineCount * space.h;
1906 if(style.freeCaret && line)
1911 width = Max(line.length + (x - line.count) * space.w, maxLength);
1915 if(selX > selLine.count)
1916 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1921 SetScrollLineStep(8, space.h);
1922 SetScrollArea(width, height, true);
1926 void ComputeLength(EditLine line)
1933 for(c = 0; c < line.count; )
1942 for(len = 0; c < line.count; c += numBytes)
1944 ch = line.buffer[c];
1945 numBytes = UTF8_NUM_BYTES(ch);
1947 if(ch == ' ' || ch == '\t')
1954 if(!len && ch == ' ')
1959 else if(!len && ch == '\t')
1961 w = (tabSize * space.w) - (x % (tabSize * space.w));
1965 FontExtent(display, font, line.buffer + start, len, &w, null);
1976 if(line.length > this.maxLength)
1978 this.maxLine = line;
1979 this.maxLength = line.length;
1988 this.maxLine = null;
1990 for(line = lines.first; line; line = line.next)
1992 if(line.length > this.maxLength)
1994 this.maxLength = line.length;
1995 this.maxLine = line;
2002 if(this.selY != this.y)
2004 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2008 void ComputeColumn()
2010 // Adjust new edit X position according to tabs
2011 int c, position = 0;
2014 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2016 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2017 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2019 position += this.tabSize - (position % this.tabSize);
2023 position += this.x - c;
2024 this.col = position;
2027 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2029 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2032 bool HasCommentOrEscape(EditLine line)
2034 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2039 for(c = line.count-1; c >= 0; c--)
2041 char ch = line.buffer[c];
2047 else //if(ch != ' ' && ch != '\t')
2054 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2056 EditLine line = l1, next;
2058 int oldCount1, oldCount2;
2059 int y, firstViewY, firstY, firstDropY, firstSelY;
2062 DelTextAction action = null;
2063 bool hadComment = false;
2067 hadComment = HasCommentOrEscape(line);
2069 if(y2 > y1 || c2 > c1)
2071 if(start < l1.count)
2073 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2077 oldCount1 = l1.count;
2079 while(c1 < oldCount1)
2081 byte ch = buffer[c1];
2082 if(UTF8_IS_FIRST(ch)) break;
2086 oldCount2 = l2.count;
2088 while(c2 < oldCount2)
2090 byte ch = buffer[c2];
2091 if(UTF8_IS_FIRST(ch)) break;
2096 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2101 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2102 string = new char[len];
2103 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2104 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2108 //oldCount1 = l1.count;
2109 //oldCount2 = l2.count;
2112 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2113 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2116 if(c2 > oldCount2) c2 = oldCount2;
2117 if(!(style.freeCaret))
2118 if(c1 > oldCount1) c1 = oldCount1;
2119 newLineCount = c1 + l2.count-c2;
2125 buffer = new char[line.size ? line.size : 1];
2127 buffer = new char[line.size];
2129 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2134 if(!line.AdjustBuffer(newLineCount))
2138 /*if(newLineCount > 4000 || newLineCount < 0)
2139 printf("Warning");*/
2141 line.count = newLineCount;
2143 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2148 action.addedSpaces = c1-oldCount1;
2150 if(action.addedSpaces > action.x1)
2156 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2157 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2159 line.buffer[line.count] = '\0';
2166 this.dropX -= c2-c1;
2167 this.dropX = Max(this.dropX,0);
2169 this.x = Max(this.x,0);
2176 firstViewY = this.viewY;
2178 firstDropY = this.dropY;
2179 firstSelY = this.selY;
2180 for(line = l1;line;line = next, y++)
2188 if(line == this.viewLine)
2190 if(this.viewLine.next)
2192 this.viewLine = this.viewLine.next;
2194 style.recomputeSyntax = true;
2198 this.viewLine = this.viewLine.prev;
2200 style.recomputeSyntax = true;
2203 else if(y < firstViewY)
2205 if(line == this.line)
2209 this.line = this.line.next;
2210 this.x = this.line.count;
2215 this.line = this.line.prev;
2216 this.x = this.line.count;
2223 if(line == this.dropLine)
2225 if(this.dropLine.next)
2227 this.dropLine = this.dropLine.next;
2228 this.dropX = this.dropLine.count;
2232 this.dropLine = this.dropLine.prev;
2233 this.dropX = this.dropLine.count;
2237 else if(y < firstDropY)
2239 if(line == this.selLine)
2241 if(this.selLine.next)
2243 this.selLine = this.selLine.next;
2244 this.selX = this.selLine.count;
2248 this.selLine = this.selLine.prev;
2249 this.selX = this.selLine.count;
2253 else if(y < firstSelY)
2257 if(line == l2) break;
2261 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2264 style.recomputeSyntax = true;
2269 bool DelSel(int * addedSpacesPtr)
2271 if(this.line != this.selLine || this.x != this.selX)
2273 if(this.selY < this.y)
2275 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2278 this.line = this.selLine;
2280 else if(this.selY > this.y)
2282 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2285 this.selLine = this.line;
2287 else if(this.selX < this.x)
2289 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2292 this.line = this.selLine;
2296 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2299 this.selLine = this.line;
2307 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2309 bool hadComment = false;
2310 // Add the line here
2311 EditLine line = this.line;
2313 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2314 if(!style.hScroll && created)
2316 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2321 // Lock if no place to display.
2324 else if(endX > this.x)
2325 max = Max(this.x, line.count);
2329 for(x = 0, c = 0; c < max+count; )
2334 if(c < Min(this.x, line.count))
2335 string = line.buffer + c;
2338 else if(c < endX + count)
2339 string = stringLine + c - endX;
2341 string = line.buffer + c - endX - count;
2345 w = (tabSize * space.w) - (x % (tabSize * space.w));
2349 numBytes = UTF8_NUM_BYTES(*string);
2350 FontExtent(display, this.font, string, numBytes, &w, null);
2354 if(x >= clientSize.w)
2359 c += numBytes; // - 1;
2365 int addedSpaces = 0;
2368 // Add blank spaces if EES_FREECARET
2369 if(this.x > line.count)
2379 for(c = 0; c<line.count; c++)
2381 if(this.line.buffer[c] == '\t')
2382 position += this.tabSize - (position % this.tabSize);
2386 wantedPosition = position + (this.x - line.count);
2388 // A tab is too much...
2389 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2390 addedSpaces = wantedPosition - position;
2395 position += this.tabSize - (position % this.tabSize);
2396 // Add as many tabs as needed
2397 addedTabs += (wantedPosition - position) / this.tabSize;
2398 position += (addedTabs-1) * this.tabSize;
2399 // Finish off with spaces
2400 addedSpaces = wantedPosition - position;
2404 addedSpaces = this.x - line.count;
2407 this.x = Min(this.x, line.count);
2409 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2415 hadComment = HasCommentOrEscape(line);
2417 // Adjust the size of the line
2418 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2422 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2425 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2426 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2429 *addedTabsPtr = addedTabs;
2430 FillBytes(line.buffer+line.count,'\t',addedTabs);
2432 if(addedTabs > 4000 || addedTabs < 0)
2435 line.count += addedTabs;
2441 FillBytes(line.buffer+line.count,' ',addedSpaces);
2443 if(addedSpaces > 4000 || addedSpaces < 0)
2446 line.count += addedSpaces;
2447 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2449 else if(addedSpacesPtr)
2450 *addedSpacesPtr = 0;
2452 if(count > 4000 || count < 0)
2455 line.count += count;
2456 this.x += count + addedTabs + addedSpaces;
2459 this.selLine = this.line;
2461 line.buffer[line.count] = '\0';
2463 ComputeLength(line);
2468 hasComment = HasCommentOrEscape(line);
2469 if(!undoBuffer.insideRedo)
2471 int backDontRecord = undoBuffer.dontRecord;
2472 undoBuffer.dontRecord = 0;
2473 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2474 undoBuffer.dontRecord = backDontRecord;
2476 if(style.syntax && (hadComment || hasComment || line != this.line))
2478 style.recomputeSyntax = true;
2486 void Emptyline(EditLine line, int y)
2489 DelCh(line, y, 0, line.next, y+1, 0, true);
2491 DelCh(line, y, 0, line, y, line.count, true);
2494 void GoToEnd(bool deselect)
2498 this.line = this.lines.last;
2499 if(this.y != this.lineCount-1)
2501 else if (this.x != this.line.count)
2502 DirtyLine(this.lineCount-1);
2503 this.y = this.lineCount-1;
2504 this.x = this.line.count;
2511 void GoToHome(bool deselect)
2515 this.line = this.lines.first;
2518 else if (this.x !=0)
2519 DirtyLine(this.lineCount-1);
2528 // Returns true if it needs scrolling
2529 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2535 bool needHScroll = false;
2542 line = this.viewLine ? (void *)this.viewLine.prev : null;
2547 line = (void *)this.lines.first;
2552 py = Min(py, clientSize.h);
2554 py = Min(py, this.lineCount);
2556 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2562 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2565 px = Min(px,clientSize.w+this.space.w);
2569 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2572 if(tline) *tline = line;
2575 // Prevent divide by 0 from non valid this.font
2577 return (y < this.viewY) || needHScroll;
2579 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2583 // Minimal Update Management Functions
2587 this.endY = clientSize.h-1;
2588 // ErrorLog("DirtyAll\n");
2591 void DirtyEnd(int y)
2593 if((y - this.viewY)*this.space.h < this.startY)
2594 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2595 this.endY = clientSize.h-1;
2596 //ErrorLog("DirtyEnd %d\n", y);
2599 void DirtyLine(int y)
2603 if((y - this.viewY)*this.space.h < this.startY)
2604 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2605 if((y - this.viewY+1)*this.space.h > this.endY)
2606 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2608 //ErrorLog("DirtyLine %d\n", y);
2615 if(style.recomputeSyntax)
2617 FigureStartSyntaxStates(lines.first, true);
2618 style.recomputeSyntax = false;
2621 if(this.startY > this.endY) return;
2622 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2628 box.right = clientSize.w-1;
2629 box.top = this.startY;
2630 box.bottom = this.endY;
2633 this.startY = clientSize.h;
2637 bool IsMouseOnSelection()
2639 bool mouseOnSelection = false;
2642 int minY = Min(this.selY, this.y);
2643 int maxY = Max(this.selY, this.y);
2644 int minX = Min(this.selX, this.x);
2645 int maxX = Max(this.selX, this.x);
2647 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2649 if(maxX != minX || maxY != minY)
2651 if(y > minY && y < maxY)
2652 mouseOnSelection = true;
2653 else if(y == minY && y == maxY)
2654 mouseOnSelection = (x < maxX && x >= minX);
2658 mouseOnSelection = (x >= this.selX);
2659 else if(y == this.y)
2660 mouseOnSelection = (x >= this.x);
2665 mouseOnSelection = (x < this.selX);
2666 else if(y == this.y)
2667 mouseOnSelection = (x < this.x);
2670 return mouseOnSelection;
2673 void UpdateCaretPosition(bool setCaret)
2677 if(mouseMove || (!overwrite && !style.noCaret))
2679 int max = this.mouseMove ? this.dropX : this.x;
2680 int y = this.mouseMove ? this.dropY : this.y;
2681 EditLine line = this.mouseMove ? this.dropLine : this.line;
2683 if(!(style.freeCaret))
2684 max = Min(max, line.count);
2686 if(FontExtent && display)
2688 for(c = 0; c < max; )
2697 for(len = 0; c < Min(max, line.count); c += numBytes)
2699 ch = line.buffer[c];
2700 numBytes = UTF8_NUM_BYTES(ch);
2702 if(ch == ' ' || ch == '\t')
2709 if(!len && ch == ' ')
2714 else if(!len && ch == '\t')
2716 w = (tabSize * space.w) - (x % (tabSize * space.w));
2720 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2732 caretY = y * this.space.h;
2733 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2738 NotifyCaretMove(master, this, y + 1, x + 1);
2744 void SelectionEnables()
2746 if((x != selX || y != selY) && !selection)
2750 itemEditCut.disabled = false;
2751 itemEditDelete.disabled = false;
2753 itemEditCopy.disabled = false;
2755 this.selection = true;
2757 else if((x == selX && y == selY) && selection)
2759 itemEditCut.disabled = true;
2760 itemEditCopy.disabled = true;
2761 itemEditDelete.disabled = true;
2763 this.selection = false;
2767 void SetSelectCursor()
2769 if(!inactive || !style.noSelect)
2772 cursor = guiApp.GetCursor(arrow);
2773 else if(this.mouseSelect)
2774 cursor = guiApp.GetCursor(iBeam);
2777 if(IsMouseOnSelection())
2778 cursor = guiApp.GetCursor(arrow);
2780 cursor = guiApp.GetCursor(iBeam);
2785 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2788 int x = px ? *px : 0;
2795 if(c < Min(max, line.count))
2799 for(len = 0; c < Min(max, line.count); c += numBytes)
2801 ch = line.buffer[c];
2802 numBytes = UTF8_NUM_BYTES(ch);
2804 if(ch == ' ' || ch == '\t')
2811 if(!len && ch == ' ')
2816 else if(!len && ch == '\t')
2818 w = (tabSize * space.w) - (x % (tabSize * space.w));
2822 FontExtent(display, font, line.buffer + start, len, &w, null);
2826 if(style.freeCaret && c < max)
2835 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2840 int a = start + len;
2843 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2847 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2850 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2854 return Min(this.maxLineSize - 1, start + len);
2860 void SetCursorToViewX()
2862 bool selecting = this.x != selX || y != selY;
2864 // Horizontal Adjustment
2866 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2871 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2883 UpdateCaretPosition(false);
2889 void SetCursorToViewY()
2892 EditLine oldLine = this.line;
2894 bool selecting = this.x != this.selX || this.y != this.selY;
2896 numLines = clientSize.h / this.space.h;
2898 // Vertical Adjustment
2899 if(this.viewY > this.y)
2901 this.y = this.viewY;
2902 this.line = this.viewLine;
2905 if(this.viewY + numLines <= this.y)
2909 this.y = this.viewY-1;
2910 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2917 if(this.line != oldLine)
2919 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2927 this.selLine = this.line;
2930 UpdateCaretPosition(false);
2937 bool SaveFile(char * fileName)
2939 File f = eFile_Open(fileName, FO_WRITE);
2943 eWindow_SetModified(false);
2944 eInstance_Delete(f);
2951 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2955 if(!style.multiLine)
2957 x = (active && this.active && !style.readOnly) ? line.count : 0;
2960 SetViewToCursor(true);
2965 if(!active && modified)
2968 if(!NotifyModified(master, this))
2971 *goOnWithActivation = false;
2978 if(timer) timer.Stop();
2980 mouseSelect = false;
2986 bool OnResizing(int *w, int *h)
2992 *w = space.h * 80 / 14;
2996 void OnResize(int w, int h)
2999 if(!hasHorzScroll && !hasVertScroll && viewLine)
3000 SetViewToCursor(true);
3002 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3004 SetViewToCursor(true);
3009 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3011 if(style.readOnly) return true;
3012 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3013 // Middle button already serves as a dragger as well.
3018 bool OnRightButtonDown(int x, int y, Modifiers mods)
3020 this.rightButtonDown = true;
3025 bool OnRightButtonUp(int x, int y, Modifiers mods)
3028 if(!parent.inactive && rightButtonDown)
3031 Menu contextMenu { };
3033 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3034 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3035 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3036 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3037 MenuDivider { contextMenu };
3038 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3040 popup = PopupMenu { master = this, menu = contextMenu,
3042 nonClient = true, interim = false, parent = parent,
3043 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3045 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3049 rightButtonDown = false;
3053 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3058 if(style.noSelect) return true;
3060 // Should we have a separate 'selectOnActivate' style?
3061 if(!mods.isActivate || (style.readOnly && style.multiLine))
3067 mouseX = mx - XOFFSET;
3070 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3072 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3078 else if(IsMouseOnSelection() && !mods.isActivate)
3088 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3090 if(mods.shift && !mods.isActivate)
3105 this.selLine = this.line;
3114 UpdateCaretPosition(true);
3118 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3122 mouseSelect = false;
3133 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3135 //PrintLn("MouseMove: ", x, ", ", y);
3141 mouseMove = IsMouseOnSelection();
3145 int size = SelSize();
3148 char * text = new char[size+1];
3152 GetSel(text, false);
3154 if(Max(selY, this.y) == dropY)
3158 if(this.dropX > this.selX)
3159 moveX = this.x - this.selX;
3163 if(this.dropX > this.x)
3164 moveX = this.selX - this.x;
3168 this.dropX -= moveX;
3169 this.selX = this.x = this.dropX;
3170 this.selY = this.y = this.dropY;
3171 this.selLine = this.line = this.dropLine;
3173 SetViewToCursor(true);
3178 byte * c = ((EditBox)this).multiLineContents, * c2;
3179 int l1 = c ? strlen(c) : 0, l2;
3182 c2 = ((EditBox)this).multiLineContents;
3183 l2 = c2 ? strlen(c2) : 0;
3184 if(l1 != l2 || (l1 && strcmp(c, c2)))
3214 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3216 //PrintLn("Dropped: ", x, ", ", y);
3218 NotifyDropped(master, this, x, y);
3225 bool OnMouseMove(int mx, int my, Modifiers mods)
3231 if(mods != -1 && mods.isSideEffect)
3236 if(style.noSelect) return true;
3237 if(wordSelect) return true;
3238 mouseX = mx - XOFFSET;
3241 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3243 if(this.mouseMove || this.mouseSelect)
3252 ((style.hScroll) || (style.vScroll)))
3259 DirtyLine(this.dropY);
3262 DirtyLine(this.dropY);
3263 this.dropLine = line;
3264 SetViewToCursor(true);
3266 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3269 else if(this.mouseSelect)
3271 DirtyLine(this.selY);
3278 SetViewToCursor(true);
3281 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3288 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3293 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3297 if(style.noSelect) return true;
3298 FindMouse(mx, my, &x, &y, &line, false);
3299 if(!NotifyDoubleClick(master, this, line, mods))
3306 for(c = x; c >= 0; c--)
3309 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3310 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3317 for(c = start; c<line.count; c += numBytes)
3319 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3330 this.line = this.selLine = line;
3331 this.wordSelect = (c != start);
3343 Menu fileMenu { menu, "File", F };
3344 saveDialog = fileDialog;
3345 MenuItem { fileMenu, "Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3346 MenuItem { fileMenu, "Save As...", A, NotifySelect = MenuFileSaveAs };
3356 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3357 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3359 space.w = Max(space.w, 1);
3360 large.w = Max(large.w, 1);
3361 space.h = Max(space.h, 1);
3362 large.h = Max(large.h, 1);
3366 for(line = lines.first; line; line = line.next)
3367 ComputeLength(line);
3372 SetViewToCursor(true);
3380 bool OnLoadGraphics()
3382 FontExtent = Display::FontExtent;
3385 // UpdateCaretPosition(true);
3389 void OnUnloadGraphics()
3394 bool OnKeyHit(Key key, unichar ch)
3396 bool shift = (key.shift) ? true : false;
3398 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3400 if(!ch && !key.alt && !key.ctrl)
3402 key.code = (SmartKey)key.code;
3405 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3408 if(style.readOnly) break;
3409 if(style.stuckCaret) GoToEnd(true);
3410 if(!(style.freeCaret))
3412 this.x = Min(this.x, this.line.count);
3418 EditLine line = this.line;
3420 for(y = this.y; y>= 0; y--)
3422 c = (y == this.y) ? (this.x-1) : line.count-1;
3424 // Slow down when going on lines...
3425 if(y != this.y) break;
3429 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3430 if(IS_ALUNDER(line.buffer[c]))
3435 if(!IS_ALUNDER(line.buffer[c]))
3437 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3451 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3452 this.x = this.selX = Min(c+1, line.count);
3453 this.y = this.selY = y;
3454 this.line = this.selLine = line;
3455 SetViewToCursor(true);
3466 if(style.readOnly) break;
3467 if(style.stuckCaret) break;
3476 if(this.line != this.selLine || this.x != this.selX)
3479 SetViewToCursor(true);
3485 if(this.x < this.line.count)
3489 //Can't delete right away as that would change the line.count
3490 for(i = this.x; i < this.line.count; i++)
3492 if(!IS_ALUNDER(this.line.buffer[i]))
3494 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3498 for(; i < this.line.count; i++)
3500 //Delete trailing whitespace
3501 if(IS_ALUNDER(this.line.buffer[i]))
3503 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3506 SetViewToCursor(true);
3509 else if(this.line.next)
3511 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3512 SetViewToCursor(true);
3518 if(!(style.freeCaret))
3520 this.selX = this.x = Min(this.x, this.line.count);
3523 if(this.x < this.line.count)
3525 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3527 else if(this.line.next)
3529 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3531 SetViewToCursor(true);
3540 if(!key.alt && !key.ctrl)
3544 bool stuffAfter = false;
3548 if(style.stuckCaret) GoToEnd(true);
3549 if(style.readOnly) break;
3550 if(!(style.multiLine)) break;
3552 for(c = 0; c<this.line.count && c<this.x; c++)
3554 if(this.line.buffer[c] == '\t')
3555 position += this.tabSize - (position % this.tabSize);
3556 else if(this.line.buffer[c] == ' ')
3564 if(this.x < this.line.count)
3567 //If last character is a { indent one tab
3568 if(this.line.buffer[this.x - 1] == '{')
3570 //Except if the next non space character is a }
3571 bool indent = false;
3573 for(i = this.x; i < this.line.size; i++)
3574 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3576 if(this.line.buffer[i] != '}')
3581 position += this.tabSize;
3584 addString = new char[position + 2];
3585 addString[len++] = '\n';
3586 addString[len] = '\0';
3588 if(stuffAfter || !style.freeCaret)
3590 for(c = 0; c<position; )
3592 if(style.useTab && c + this.tabSize <= position)
3594 addString[len++] = '\t';
3599 addString[len++] = ' ';
3603 addString[len] = '\0';
3607 if(!stuffAfter && style.freeCaret)
3609 this.x = this.selX = position;
3613 SetViewToCursor(true);
3623 if(style.stuckCaret) break;
3624 if(!(style.freeCaret))
3626 this.x = Min(this.x, this.line.count);
3627 this.selX = Min(this.selX, this.selLine.count);
3630 if(!shift) SelDirty();
3633 bool foundAlpha = false;
3636 EditLine line, lastLine;
3639 for(line = this.line; (line && !found); line = line.prev, y--)
3644 if(this.x == 0 && line != this.line)
3653 if(line == this.line) start = this.x -1; else start = line.count-1;
3654 start = Min(start, line.count-1);
3656 for(c = start; c >= 0;)
3659 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3677 byte ch = line.buffer[c];
3678 if(UTF8_IS_FIRST(ch)) break;
3682 // No next word found,
3683 if(!found && ( this.x > 0 || (!line.count && this.x)))
3697 this.line = lastLine;
3708 byte * buffer = line.buffer;
3711 byte ch = buffer[x];
3712 if(UTF8_IS_FIRST(ch)) break;
3729 if(!shift) Deselect();
3730 SetViewToCursor(true);
3736 if(style.stuckCaret) break;
3737 if(!(style.freeCaret))
3739 this.x = Min(this.x, this.line.count);
3740 this.selX = Min(this.selX, this.selLine.count);
3743 if(!shift) SelDirty();
3744 if(!shift && (this.x != this.selX || this.y != this.selY));
3747 bool onAChar = false;
3748 if(this.selX != this.x || this.selY != this.y)
3750 if(this.x<this.line.count)
3751 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3753 if(key.shift && onAChar &&
3754 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3756 bool foundAlpha = false;
3758 EditLine line, lastLine;
3760 int lastC, lastY, lastNumBytes;
3762 for(line = this.line; (line && !found); line = line.next, y++)
3764 int start = (line == this.line) ? this.x : 0;
3767 for(c = start; c < line.count; c += numBytes)
3769 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3774 lastNumBytes = numBytes;
3784 if(!found && (c != this.x || line != this.line))
3797 this.x = lastC + lastNumBytes;
3799 this.line = lastLine;
3806 bool foundAlpha = false;
3811 for(line = this.line; (line && !found); line = line.next, y++)
3813 int start = (line == this.line) ? this.x : 0;
3816 for(c = start; c < line.count; c += numBytes)
3818 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3833 // No next word found,
3834 if(!found && (c != this.x || line != this.line))
3838 this.x = line.count;
3850 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3854 byte * buffer = line.buffer;
3857 byte ch = buffer[x];
3858 if(UTF8_IS_FIRST(ch)) break;
3879 if(!shift) Deselect();
3880 SetViewToCursor(true);
3887 if(!style.vScroll || hasVertScroll) break;
3893 if(style.stuckCaret) break;
3895 if(!shift) SelDirty();
3905 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3909 if(!shift) Deselect();
3911 SetViewToCursor(false);
3914 if(caretY == this.y * space.h)
3920 if(!style.freeCaret)
3921 this.x = Min(this.x, line.count);
3931 int sx = 0, sy = this.y * space.h;
3932 char * text = line.text;
3933 int maxW = clientSize.w - sx;
3934 display.FontExtent(font, " ", 1, null, &th);
3938 int startPos = textPos;
3941 bool lineComplete = false;
3943 if(!style.wrap && caretY == MAXINT)
3946 //textPos = line.count;
3947 //lineComplete = true;
3950 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3954 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3957 len = (nextSpace - (text + textPos));
3959 len = line.count - textPos;
3961 if(textPos < line.count)
3963 display.FontExtent(font, text + textPos, len, &w, null);
3965 if(nextSpace) { w += space.w; len++; }
3967 if(style.wrap && x + width + w > maxW && x > 0)
3969 lineComplete = true;
3979 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
3983 while(this.x > 0 && x + sx > caretX && this.x > startPos)
3986 if(this.x > line.count)
3989 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
3990 len = this.x - startPos;
3991 display.FontExtent(font, text + startPos, len, &x, null);
3995 if(!shift) Deselect();
3997 SetViewToCursor(false);
4001 if(sy == caretY - th || textPos >= line.count)
4003 if(textPos >= line.count)
4005 int c = textPos - 1;
4006 while(c > 0 && text[c] == ' ') c--;
4010 this.x = line.count;
4013 if(!shift) Deselect();
4015 SetViewToCursor(false);
4020 } while(textPos < line.count);
4023 if(!shift) Deselect();
4025 SetViewToCursor(false);
4034 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4035 if(!shift) SelDirty();
4036 this.line = this.line.prev;
4042 if(!shift) Deselect();
4046 SetViewToCursor(false);
4052 return style.multiLine ? false : true;
4056 if(!style.vScroll || hasVertScroll)
4063 if(style.stuckCaret) break;
4067 int sx = 0, sy = this.y * this.space.h;
4068 int maxW = clientSize.w - sx;
4069 char * text = line.buffer;
4071 if(!shift) SelDirty();
4077 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4087 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4090 if(!shift) Deselect();
4092 if(this.selX != this.x || this.selY != this.y)
4094 SetViewToCursor(false);
4097 while(!textPos || (style.freeCaret || textPos<line.count))
4099 int startPos = textPos;
4102 bool lineComplete = false;
4103 if(!style.wrap && sy <= caretY)
4105 textPos = line.count;
4106 lineComplete = true;
4108 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4112 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4115 len = (nextSpace - (text + textPos));
4117 len = line.count - textPos;
4119 if(textPos < line.count)
4121 display.FontExtent(font, text + textPos, len, &w, &th);
4123 if(nextSpace) { w += space.w; len++; }
4124 if(style.wrap && x + width + w > maxW && x > 0)
4126 lineComplete = true;
4136 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4140 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4143 if(this.x > line.count)
4146 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4148 len = this.x - startPos;
4149 display.FontExtent(font, text + startPos, len, &x, null);
4152 if(!shift) Deselect();
4155 SetViewToCursor(false);
4161 this.x = line.count;
4164 if(!shift) Deselect();
4167 SetViewToCursor(false);
4170 else if(textPos >= line.count && line.next)
4176 sy = this.y * this.space.h;
4177 sx = 0; //textBlock.startX;
4183 sx = 0; //textBlock.startX;
4191 this.x = Min(this.x, line.count);
4192 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4194 if(!shift) Deselect();
4197 if(this.selX != this.x || this.selY != this.y)
4200 SetViewToCursor(false);
4207 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4208 if(!shift) SelDirty();
4209 this.line = this.line.next;
4213 if(!shift) Deselect();
4216 if(this.selX != this.x || this.selY != this.y)
4224 return style.multiLine ? false : true;
4227 if(style.stuckCaret) break;
4228 if(!(style.freeCaret))
4229 this.selX = Min(this.selX, this.selLine.count);
4231 if(!shift) SelDirty();
4234 this.line = this.lines.first;
4235 if(this.y != 0 || this.x != 0)
4245 EditLine line = this.line;
4247 for(c=0; line.buffer[c]; c++)
4248 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4250 if(c != 0 || this.x)
4260 DirtyLine(this.y);*/
4265 if(!shift) Deselect();
4266 SetViewToCursor(true);
4272 if(style.stuckCaret) break;
4273 if(!(style.freeCaret))
4274 this.selX = Min(this.selX, this.selLine.count);
4276 if(!shift) SelDirty();
4281 else if(this.x != this.line.count)
4283 this.x = this.line.count;
4284 //DirtyLine(this.y);
4287 if(!shift) Deselect();
4288 SetViewToCursor(true);
4293 if(style.tabKey && !key.ctrl)
4295 if(this.selY != this.y && style.tabSel)
4297 EditLine firstLine, lastLine;
4301 // Do multi line selection tabbing here
4302 if(this.selY < this.y)
4304 firstLine = this.selLine;
4305 lastLine = this.line;
4311 // Selecting going up
4312 firstLine = this.line;
4313 lastLine = this.selLine;
4320 for(line = firstLine; line; line = line.next, y++)
4322 if(line != lastLine || x)
4326 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4328 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4330 if(line.buffer[c] == '\t')
4335 else if(line.buffer[c] != ' ')
4340 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4343 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4344 char * string = new char[len];
4345 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4346 GetText(string, line, y, 0, line, y, lastC, false, false);
4349 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4350 if(!line.AdjustBuffer(line.count-lastC))
4356 if(line == lastLine) break;
4361 for(line = firstLine; line; line = line.next, y++)
4365 if(line != lastLine || x)
4369 if(line.count + 1 <= this.maxLineSize)
4371 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4373 if(!line.AdjustBuffer(line.count+1))
4376 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4378 AddCharAction action { ch = '\t', x = 0, y = y };
4382 memmove(line.buffer+1,line.buffer,line.size-1);
4384 line.buffer[0] = '\t';
4389 if(line.count + this.tabSize <= this.maxLineSize)
4392 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4393 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4395 if(!line.AdjustBuffer(line.count+this.tabSize))
4399 char * string = new char[this.tabSize + 1];
4400 memset(string, ' ', this.tabSize);
4401 string[this.tabSize] = '\0';
4402 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4405 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4406 line.count+=this.tabSize;
4407 for(c=0; c<this.tabSize; c++)
4408 line.buffer[c] = ' ';
4414 if(line == lastLine) break;
4428 char * addString = new char[this.tabSize + 1];
4430 if(!(style.freeCaret))
4432 this.x = Min(this.x, this.line.count);
4437 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4439 addString[len++] = ' ';
4447 SetViewToCursor(true);
4454 if(!(style.hScroll) || hasHorzScroll) break;
4455 if(this.viewX < this.maxLength)
4457 //this.viewX+=this.space.w*this.tabSize;
4459 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4466 if(!shift) Deselect();
4475 if(!(style.hScroll) || hasHorzScroll) break;
4478 //this.viewX-=this.space.w*this.tabSize;
4479 //this.viewX = Max(this.viewX,0);
4481 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4482 // SetCursorToView();
4489 if(!shift) Deselect();
4503 if(!(style.readOnly))
4509 this.overwrite ^= 1;
4510 UpdateCaretPosition(true);
4515 NotifyOvrToggle(master, this, this.overwrite);
4526 if(style.noSelect) break;
4529 //Save current view position
4530 int tempX = this.viewX;
4531 int tempY = this.viewY;
4535 this.selLine = this.lines.first;
4536 this.y = this.lineCount-1;
4537 this.line = this.lines.last;
4538 this.x = this.line.count;
4541 SetViewToCursor(true);
4543 //Restore previous view position
4544 SetScrollPosition(tempX, tempY * this.space.h);
4550 // TOCHECK: Was there any good reason why we weren't returning false here?
4551 return false; // break;
4556 if(!(style.readOnly))
4564 if(style.readOnly) break;
4570 if(style.readOnly) break;
4575 if(style.readOnly) break;
4579 if(style.readOnly) break;
4583 if(style.readOnly) break;
4584 if(key.shift && key.code == rightBracket)
4586 //Only indent back if you are exactly at one tab.
4588 bool whitespace = true;
4594 EditLine line = this.line;
4596 //Only remove one tab if there is nothing else on the line.
4597 for(i = 0; i < this.line.count; i++)
4599 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4613 for(pos = line.count - 1; pos >= 0; pos--)
4615 char c = line.buffer[pos];
4619 indentwidth += this.tabSize;
4621 //Counting backwards, so when you find a character, indentation is reset
4631 //Place the } to get an undo:
4634 this.x = this.line.count;
4638 putsize = indentwidth;
4640 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4642 newline = new char[putsize+2];
4643 newline[putsize] = '}';
4644 newline[putsize+1] = '\0';
4648 for(; i < indentwidth / this.tabSize; i++)
4650 for(;i < putsize; i++)
4658 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4670 void OnHScroll(ScrollBarAction action, int position, Key key)
4673 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4675 this.viewX = position;
4676 if(action != setRange)
4678 if(!this.mouseMove && style.cursorFollowsView)
4685 void OnVScroll(ScrollBarAction action, int position, Key key)
4687 int oldViewY = this.viewY;
4690 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4692 position /= this.space.h;
4694 if(position < this.viewY)
4696 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4697 style.recomputeSyntax = true;
4699 else if(position > this.viewY)
4701 EditLine oldViewLine = viewLine;
4702 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4703 FigureStartSyntaxStates(oldViewLine, false);
4706 if(action != setRange)
4708 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4711 if(this.x != this.selX || this.y != this.selY)
4715 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4718 int numLines = clientSize.h / this.space.h;
4720 if(Abs(this.viewY - oldViewY) < numLines)
4723 if(this.viewY > oldViewY)
4725 for(y = oldViewY; y <this.viewY; y++)
4726 DirtyLine(y + numLines);
4730 for(y = this.viewY; y <oldViewY; y++)
4731 DirtyLine(y + numLines);
4736 // Fix dirt of stuff before first line...
4737 if(this.viewY - oldViewY > 0)
4739 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4746 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4751 ReplaceTextAction replaceAction = null;
4752 AddCharAction addCharAction = null;
4753 int addedSpaces = 0, addedTabs = 0;
4755 if(ch == '\r') return true;
4756 if(style.stuckCaret /*|EES_READONLY)*/ )
4759 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4761 if(!undoBuffer.dontRecord)
4763 if(selX != x || selY != y)
4768 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4769 oldString = new char[len];
4770 UTF32toUTF8Len(&ch, 1, buffer, 4);
4771 newString = CopyString(buffer);
4772 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4774 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4775 if(selY < y || (selY == y && selX < x))
4777 replaceAction.x1 = selX;
4778 replaceAction.y1 = selY;
4779 replaceAction.x2 = x;
4780 replaceAction.y2 = y;
4784 replaceAction.x1 = x;
4785 replaceAction.y1 = y;
4786 replaceAction.x2 = selX;
4787 replaceAction.y2 = selY;
4789 Record(replaceAction);
4790 undoBuffer.dontRecord++;
4794 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4795 Record(addCharAction);
4801 DelSel(&addedSpaces);
4802 if(this.lineCount+1 > this.maxLines)
4805 Emptyline(this.lines.first,0);
4809 if(!(style.vScroll))
4811 // Make sure it fits, but we need a default line is this.font is too big for window
4812 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4815 if((this.y >= 0) && this.y < this.viewY)
4820 line = EditLine { };
4823 lines.Insert(this.line, line);
4824 line.editBox = this;
4830 // If we're displacing the lines from a current line ...
4833 if(this.line.buffer)
4836 if(this.line.count < endX) endX = this.line.count;
4837 length = this.line.count - endX;
4840 if(!line.AdjustBuffer(length))
4844 if(this.line.buffer)
4846 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4848 if(endX > 4000 || endX < 0)
4851 this.line.count = endX;
4852 this.line.buffer[this.line.count] = '\0';
4853 this.line.AdjustBuffer(this.line.count);
4854 ComputeLength(this.line);
4858 BufferLocation before = { this.line, this.y, this.x }, after;
4864 ComputeLength(this.line);
4867 if(length > 4000 || length < 0)
4870 line.count = length;
4874 line.buffer[line.count] = '\0';
4877 after.line = this.line, after.y = this.y, after.x = this.x;
4879 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4885 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4886 DelSel(&addedSpaces);
4887 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4888 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4889 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4893 this.selLine = this.line;
4897 replaceAction.x3 = x;
4898 replaceAction.y3 = y;
4899 replaceAction.addedSpaces = addedSpaces;
4900 replaceAction.addedTabs = addedTabs;
4901 undoBuffer.dontRecord--;
4905 addCharAction.addedSpaces = addedSpaces;
4906 addCharAction.addedTabs = addedTabs;
4913 /****************************************************************************
4915 ****************************************************************************/
4918 bool AddCh(unichar ch)
4920 return _AddCh(ch, null, null);
4925 this.modified = true;
4926 NotifyUpdate(master, this);
4927 modifiedDocument = true;
4930 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4933 DelCh(line1, y1, x1, line2, y2, x2, false);
4934 SetViewToCursor(true);
4942 itemEditUndo.disabled = undoBuffer.curAction == 0;
4943 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4944 if(savedAction == undoBuffer.curAction)
4946 modifiedDocument = false;
4948 NotifyUnsetModified(master, this);
4955 itemEditUndo.disabled = undoBuffer.curAction == 0;
4956 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4957 if(savedAction == undoBuffer.curAction)
4959 modifiedDocument = false;
4961 NotifyUnsetModified(master, this);
4965 void Record(UndoAction action)
4967 if(!undoBuffer.dontRecord)
4969 undoBuffer.Record(action);
4970 itemEditUndo.disabled = undoBuffer.curAction == 0;
4971 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4978 this.lines.first, 0,0,
4979 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
4982 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4987 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
4988 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
4989 this.y = line2 ? y2 : (this.lineCount-1);
4990 this.line = line2 ? (EditLine)line2 : this.lines.last;
4993 SetViewToCursor(true);
4997 // TODO: Fix this vs modifiedDocument window property
4998 void SetModified(bool flag)
5002 this.modified = false;
5003 if(flag && !NotifyModified(master, this))
5004 this.modified = true;
5009 bool AddS(char * string)
5016 int addedSpaces = 0, addedTabs = 0;
5017 AddTextAction action = null;
5018 ReplaceTextAction replaceAction = null;
5020 this.pasteOperation = true;
5022 if(style.stuckCaret /*|EES_READONLY)*/ )
5025 if(!undoBuffer.dontRecord)
5027 char * placeString = CopyString(string);
5028 if(!style.multiLine)
5032 for(i = 0; (ch = placeString[i]); i++)
5035 placeString[i] = '\0';
5036 placeString = renew placeString byte[i+1];
5041 if(selX != x || selY != y)
5045 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5046 oldString = new char[len];
5047 newString = placeString;
5048 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5050 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5051 if(selY < y || (selY == y && selX < x))
5053 replaceAction.x1 = selX;
5054 replaceAction.y1 = selY;
5055 replaceAction.x2 = x;
5056 replaceAction.y2 = y;
5060 replaceAction.x1 = x;
5061 replaceAction.y1 = y;
5062 replaceAction.x2 = selX;
5063 replaceAction.y2 = selY;
5065 Record(replaceAction);
5069 if(string[0] == '\n')
5070 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5072 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5080 undoBuffer.dontRecord++;
5081 DelSel(&addedSpaces);
5085 for(c = 0; string[c]; c++)
5087 if(string[c] == '\n' || string[c] == '\r')
5089 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5094 if(string[c] == '\n')
5103 // Reset for next line
5107 if(string[c] == '\r' && *line == '\n')
5119 // Why was this here?
5122 // Add the line here
5124 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5130 undoBuffer.dontRecord--;
5135 action.addedSpaces = addedSpaces;
5136 action.addedTabs = addedTabs;
5138 else if(replaceAction)
5140 replaceAction.y3 = y;
5141 replaceAction.x3 = x;
5142 replaceAction.addedSpaces = addedSpaces;
5143 replaceAction.addedTabs = addedTabs;
5146 UpdateCaretPosition(true);
5148 this.pasteOperation = false;
5161 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5162 SetViewToCursor(true);
5168 void PutCh(unichar ch)
5172 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5173 //if((ch >= 32) || ch == '\n')
5175 int addedSpaces = 0, addedTabs = 0;
5176 ReplaceTextAction replaceAction = null;
5177 AddCharAction addCharAction = null;
5180 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5182 if(this.x < this.line.count && this.overwrite)
5187 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5188 oldString = new char[len];
5189 UTF32toUTF8Len(&ch, 1, buffer, 4);
5190 newString = CopyString(buffer);
5191 GetText(oldString, line, y, x, line, y, x+1, false, false);
5192 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5193 replaceAction.x1 = x;
5194 replaceAction.y1 = y;
5195 replaceAction.x2 = x+1;
5196 replaceAction.y2 = y;
5197 Record(replaceAction);
5199 undoBuffer.dontRecord++;
5200 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5201 undoBuffer.dontRecord--;
5203 else if(!undoBuffer.dontRecord)
5205 if(selX != x || selY != y)
5210 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5211 oldString = new char[len];
5212 UTF32toUTF8Len(&ch, 1, buffer, 4);
5213 newString = CopyString(buffer);
5214 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5215 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5216 if(selY < y || (selY == y && selX < x))
5218 replaceAction.x1 = selX;
5219 replaceAction.y1 = selY;
5220 replaceAction.x2 = x;
5221 replaceAction.y2 = y;
5225 replaceAction.x1 = x;
5226 replaceAction.y1 = y;
5227 replaceAction.x2 = selX;
5228 replaceAction.y2 = selY;
5230 Record(replaceAction);
5234 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5235 Record(addCharAction);
5238 undoBuffer.dontRecord++;
5239 result = _AddCh(ch, &addedSpaces, &addedTabs);
5242 replaceAction.x3 = x;
5243 replaceAction.y3 = y;
5244 replaceAction.addedSpaces = addedSpaces;
5245 replaceAction.addedTabs = addedTabs;
5249 addCharAction.addedSpaces = addedSpaces;
5250 addCharAction.addedTabs = addedTabs;
5252 undoBuffer.dontRecord--;
5256 if(result) SetViewToCursor(true);
5260 void PutS(char * string)
5265 SetViewToCursor(true);
5270 void Printf(char * format, ...)
5274 char temp[MAX_F_STRING];
5276 va_start(args, format);
5277 vsprintf(temp, format, args);
5283 void SetContents(char * format, ...)
5288 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5291 char temp[MAX_F_STRING];
5293 va_start(args, format);
5294 vsprintf(temp, format, args);
5310 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5313 else if(this.line.prev)
5315 EditLine line = this.line.prev;
5319 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5327 this.selLine = this.line;
5332 SetViewToCursor(true);
5337 Emptyline(this.line,this.y);
5338 this.selX = this.x = 0;
5341 this.selLine = this.line;
5343 SetViewToCursor(true);
5353 SetViewToCursor(true);
5361 SetViewToCursor(true);
5365 bool GoToLineNum(int lineNum)
5370 EditLine line = this.lines.first;
5371 for(c = 0; c < lineNum && line; c++, line = line.next);
5381 SetViewToCursor(true);
5388 bool GoToPosition(EditLine line, int y, int x)
5400 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5414 SetViewToCursor(true);
5421 void SetViewToCursor(bool setCaret)
5431 bool dontScroll = false;
5437 selected = selX != this.x || selY != y;
5444 checkLine = dropLine;
5450 checkLine = this.line;
5455 numLines = clientSize.h / space.h;
5457 // This is broken. The EditBox now doesn't do anything different when adding to it,
5458 // regardless of the previous scrolling position. It should be read and then set again
5459 // if one wishes to preserve it.
5460 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5461 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5465 // Horizontal Adjustment
5466 if(!dontScroll && checkLine)
5470 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5473 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5479 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5480 viewX = x - clientSize.w+space.w;
5481 if(x < this.viewX + clientSize.w/2 - space.w)
5482 viewX = Max(0, x - clientSize.w/2 + space.w);
5490 // Vertical Adjustment
5491 if(viewY > checkY) viewY = checkY;
5492 if(viewY + numLines <= checkY)
5494 if(clientSize.h >= space.h)
5496 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5506 for(;dropLine && dropLine.prev && dropY >= numLines;)
5508 dropLine = dropLine.prev;
5512 for(;this.line && this.line.prev && this.y >= numLines;)
5514 this.line = this.line.prev;
5519 SetScrollPosition(viewX, viewY * this.space.h);
5521 UpdateCaretPosition(setCaret);
5527 selLine = this.line;
5537 void CenterOnCursor()
5539 int numLines = clientSize.h / this.space.h;
5540 int y = this.y - numLines / 2;
5542 bool figureSyntax = false;
5543 EditLine oldViewLine = viewLine;
5544 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5549 for(;y < this.viewY; y++)
5551 this.viewLine = this.viewLine.prev;
5552 style.recomputeSyntax = true;
5554 for(;y > this.viewY; y--)
5556 this.viewLine = this.viewLine.next;
5557 figureSyntax = true;
5560 FigureStartSyntaxStates(oldViewLine, false);
5564 SetScrollPosition(this.viewX, viewY * this.space.h);
5565 UpdateCaretPosition(true);
5569 void SetCursorToView()
5580 numLines = clientSize.h / this.space.h;
5584 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5585 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5589 EditLine oldLine = this.line;
5590 bool lastOne = false;
5591 EditLine oldViewLine = this.viewLine;
5592 bool figureSyntax = false;
5594 if(this.y >= this.lineCount-1) return;
5596 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5598 if(this.viewY + numLines < this.lines.count)
5600 this.viewLine = this.viewLine.next;
5602 figureSyntax = true;
5604 else if(c && !lastOne)
5613 FigureStartSyntaxStates(oldViewLine, false);
5614 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5617 SetViewToCursor(false);
5626 if(this.y == 0) return;
5628 numLines = clientSize.h / this.space.h;
5632 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5633 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5637 EditLine oldLine = this.line;
5639 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5644 if(this.viewLine.prev)
5646 this.viewLine = this.viewLine.prev;
5648 style.recomputeSyntax = true;
5652 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5655 SetViewToCursor(false);
5662 if(this.viewLine.prev)
5665 // this.viewLine = this.viewLine.prev;
5668 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5675 if(this.viewLine.next)
5678 // this.viewLine = this.viewLine.next;
5681 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5688 EditLine l1, l2, line;
5689 int x1, x2, nx1, nx2;
5694 if(!this.selLine) return 0;
5695 if(this.selLine == this.line && this.selX == this.x) return 0;
5696 if(this.selY < this.y)
5703 else if(this.selY > this.y)
5710 else if(this.selX < this.x)
5712 l1 = l2 = this.line;
5718 l1 = l2 = this.line;
5722 nx1 = Min(x1,l1.count);
5723 nx2 = Min(x2,l2.count);
5725 // Find Number of Bytes Needed
5727 for(line = l1; line; line = line.next)
5729 if(line == l1) start = nx1; else start = 0;
5730 if(line == l2) end = nx2; else end = line.count;
5732 if(style.freeCaret && line == l2)
5735 count = Max(x2-Max(x1,l1.count),0);
5737 count = Max(x2-l2.count,0);
5741 if(line == l2) break;
5742 // Add Carriage Return / line Feed
5749 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5753 if(!reorder || this.selY < this.y)
5755 if(l1) *l1 = this.selLine;
5756 if(y1) *y1 = this.selY;
5757 if(x1) *x1 = this.selX;
5758 if(l2) *l2 = this.line;
5759 if(y2) *y2 = this.y;
5760 if(x2) *x2 = this.x;
5762 else if(this.selY > this.y)
5764 if(l1) *l1 = this.line;
5765 if(y1) *y1 = this.y;
5766 if(x1) *x1 = this.x;
5767 if(l2) *l2 = this.selLine;
5768 if(y2) *y2 = this.selY;
5769 if(x2) *x2 = this.selX;
5771 else if(this.selX < this.x)
5773 if(l1) *l1 = this.line;
5774 if(y1) *y1 = this.selY;
5775 if(x1) *x1 = this.selX;
5776 if(l2) *l2 = this.line;
5777 if(y2) *y2 = this.y;
5778 if(x2) *x2 = this.x;
5782 if(l1) *l1 = this.line;
5783 if(y1) *y1 = this.y;
5784 if(x1) *x1 = this.x;
5785 if(l2) *l2 = this.line;
5786 if(y2) *y2 = this.selY;
5787 if(x2) *x2 = this.selX;
5792 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5794 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5796 this.selLine = (EditLine)l1;
5799 this.line = (EditLine)l2;
5803 SetViewToCursor(true);
5807 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5809 EditLine l1, l2, line;
5810 int x1, x2, nx1, nx2;
5841 nx1 = Min(x1,l1.count);
5842 nx2 = Min(x2,l2.count);
5845 for(line = l1; line; line = line.next)
5847 if(line == l1) start = nx1; else start = 0;
5848 if(line == l2) end = nx2; else end = line.count;
5851 CopyBytes(text, line.buffer + start, end - start);
5854 numChars += end-start;
5856 if(style.freeCaret && line == l2 && addSpaces)
5859 count = Max(x2-Max(x1,l1.count),0);
5861 count = Max(x2-l2.count,0);
5864 FillBytes(text,' ',count);
5870 if(line == l2) break;
5882 // '\0' terminate Terminate
5889 void GetSel(char * text, bool addCr)
5891 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5897 this.selLine = this.line;
5907 int size = SelSize();
5910 // Try to allocate memory
5911 ClipBoard clipBoard { };
5912 if(clipBoard.Allocate(size+1))
5914 GetSel(clipBoard.memory, true);
5927 ClipBoard clipBoard { };
5928 if(clipBoard.Load())
5929 PutS(clipBoard.memory);
5940 SetViewToCursor(true);
5945 void DeleteSelection()
5950 SetViewToCursor(true);
5956 void Save(File f, bool cr)
5959 savedAction = undoBuffer.curAction;
5961 for(line = this.lines.first; line; line = line.next)
5963 f.Write(line.buffer, line.count,1);
5966 if(cr) f.Putc('\r');
5972 #define BUFFER_SIZE 16384
5975 undoBuffer.dontRecord++;
5978 char buffer[BUFFER_SIZE];
5982 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
5983 buffer[count] = '\0';
5989 undoBuffer.dontRecord--;
5990 undoBuffer.count = 0;
5991 undoBuffer.curAction = 0;
5992 itemEditUndo.disabled = undoBuffer.curAction == 0;
5993 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5996 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6000 bool firstPass = true;
6001 EditBoxFindResult result = found;
6003 if(!this.line) return notFound;
6006 for(line = this.line;;)
6014 line = this.lines.first;
6020 line = this.lines.last;
6021 num = this.lineCount - 1;
6027 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6029 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6033 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6036 if(line == this.line && !firstPass) break;
6054 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6058 int searchLen = strlen(text);
6059 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6061 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6062 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6064 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6071 bool OnKeyDown(Key key, unichar ch)
6074 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6076 if(!NotifyKeyDown(master, this, key, ch))
6086 this.mouseMove = false;
6087 OnLeftButtonUp(0,0,0);
6088 SetViewToCursor(true);
6098 public class EditBoxStream : File
6101 BufferLocation start, sel;
6108 EditBox editBox = this.editBox;
6110 editBox.x = start.x;
6111 editBox.y = start.y;
6112 editBox.line = start.line;
6114 editBox.selX = sel.x;
6115 editBox.selY = sel.y;
6116 editBox.selLine = sel.line;
6118 editBox.SetViewToCursor(true);
6119 //editBox.ComputeColumn();
6123 property EditBox editBox
6131 start.line = value.line;
6135 sel.line = value.selLine;
6138 value.GoToHome(true);
6141 get { return editBox; }
6144 uint Read(byte * buffer, uint size, uint count)
6147 EditBox editBox = this.editBox;
6148 EditLine line = editBox.line;
6154 for(;read < count && line; line = (*&line.next))
6156 int numBytes = Min(count - read, (*&line.count) - x);
6159 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6164 for(;read < count && x < (*&line.count);)
6166 buffer[read++] = (*&line.buffer)[x++];
6173 buffer[read++] = '\n';
6178 if(x == (*&line.count) && (*&line.next))
6187 editBox.line = editBox.selLine = line;
6188 editBox.x = editBox.selX = x;
6189 editBox.y = editBox.selY = y;
6194 bool Seek(int pos, FileSeekMode mode)
6197 EditBox editBox = this.editBox;
6198 EditLine line = editBox.line;
6200 if(mode == FileSeekMode::start)
6202 pos = pos - this.pos;
6212 for(;read < pos && line; line = (*&line.next))
6214 int numBytes = Min(pos - read, (*&line.count) - x);
6217 read += numBytes; x += numBytes;
6220 /*for(;read < pos && x < (*&line.count);)
6248 for(;read < pos && line; line = (*&line.prev))
6250 int numBytes = Min(pos - read, x);
6256 /*for(;read < pos && x > 0;)
6266 x = (*&(*&line.prev).count);
6282 editBox.line = editBox.selLine = line;
6283 editBox.x = editBox.selX = x;
6284 editBox.y = editBox.selY = y;
6289 bool Puts(char * string)
6291 EditBox editBox = this.editBox;
6292 BufferLocation start { editBox.line, editBox.y, editBox.x };
6296 editBox.AddS(string);
6298 pos.line = editBox.line;
6302 this.start.AdjustAdd(start, pos);
6303 sel.AdjustAdd(start, pos);
6308 // NOTE: BYTE, NOT UNICODE CHARACTER!
6311 EditBox editBox = this.editBox;
6312 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6317 utf8Bytes[numBytes++] = ch;
6318 utf8Bytes[numBytes] = 0;
6319 if(UTF8Validate(utf8Bytes))
6321 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6323 pos.line = editBox.line;
6326 this.start.AdjustAdd(start, pos);
6327 sel.AdjustAdd(start, pos);
6334 bool Getc(char * ch)
6336 return Read(ch, 1, 1) ? true : false;
6339 void DeleteBytes(uint count)
6341 EditBox editBox = this.editBox;
6344 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6345 BufferLocation end = pos;
6350 for(;c < count && end.line && end.x < end.line.count;)
6355 if(c < count && end.line && end.line.next)
6357 end.line = end.line.next;
6366 start.AdjustDelete(pos, end);
6367 sel.AdjustDelete(pos, end);
6369 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);