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;
92 void UnregisterClass_EditBox()
95 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
104 extern int __ecereVMethodID_class_OnFree;
107 static class ArrayImpl
114 public class OldArray
121 void ** array = (void **)((ArrayImpl)this).array;
122 if(type.type == normalClass || type.type == noHeadClass)
124 for(c = 0; c<size; c++)
125 type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
127 // TODO: Call OnFree for structClass
128 delete ((ArrayImpl)this).array;
137 if(((ArrayImpl)this).array)
141 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
144 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
153 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
158 public class UndoAction : struct
161 subclass(UndoAction) type;
162 virtual void Undo(void * data) { type.Undo(this, data); }
163 virtual void Redo(void * data) { type.Redo(this, data); }
165 virtual void Print(void * data) { type.Print(this, data); }
169 if(((Class)type).Destructor)
170 ((void (*)(void *))((Class)type).Destructor)(this);
175 class ArrayUndoActions : OldArray
177 type = class(UndoAction);
181 public class UndoBuffer
183 ArrayUndoActions actions { size = 8 };
198 UndoAction action = actions._[--curAction];
200 /*Print("Undoing: ");
201 action.Print(data);*/
212 if(curAction < count)
214 UndoAction action = actions._[curAction];
217 /*Print("Redoing: ");
218 action.Print(data);*/
226 void Record(UndoAction action)
228 if(!dontRecord && !insideRedo)
230 if(curAction < count)
233 for(c = curAction; c < count; c++)
239 if(count >= actions.size)
240 actions.size += actions.size / 2;
243 /*Print("Recording: ");
244 action.Print(data);*/
246 actions._[count++] = action;
249 if(actions.size > count + count / 2 && count + count / 2 >= 8)
250 actions.size = count + count / 2;
257 static class AddCharAction : UndoAction
261 int addedSpaces, addedTabs;
262 type = class(AddCharAction);
264 void Undo(EditBox editBox)
266 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
268 if(addedTabs || addedSpaces)
269 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
270 editBox.UpdateDirty();
273 void Redo(EditBox editBox)
275 editBox.GoToPosition(null, y, x);
277 editBox.UpdateDirty();
280 void Print(EditBox editBox)
282 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
287 static class AddTextAction : UndoAction
291 int addedSpaces, addedTabs;
292 type = class(AddTextAction);
295 void Print(EditBox editBox)
297 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
305 void Undo(EditBox editBox)
310 editBox.GoToPosition(null, y1, x1);
312 l2 = editBox.lines.first;
313 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
315 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
316 if(addedTabs || addedSpaces)
317 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
319 editBox.SetViewToCursor(true);
323 void Redo(EditBox editBox)
325 editBox.GoToPosition(null, y1, x1);
326 editBox.PutS(string);
330 static class DelTextAction : UndoAction
336 type = class(DelTextAction);
339 void Print(EditBox editBox)
341 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
344 void Undo(EditBox editBox)
346 editBox.GoToPosition(null, y1, x1);
347 editBox.PutS(string);
351 editBox.GoToPosition(null, y1, x1);
354 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
355 //editBox.SetViewToCursor(true);
358 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
364 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
365 //editBox.SetViewToCursor(true);
368 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
372 void Redo(EditBox editBox)
377 editBox.GoToPosition(null, y1, x1);
380 l2 = editBox.lines.first;
381 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
383 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
384 editBox.SetViewToCursor(true);
394 static class ReplaceTextAction : UndoAction
396 int y1, x1, y2, x2, y3, x3;
400 int addedSpaces, addedTabs;
402 type = class(ReplaceTextAction);
405 void Print(EditBox editBox)
407 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
410 void Undo(EditBox editBox)
415 editBox.GoToPosition(null, y1, x1);
417 l3 = editBox.lines.first;
418 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
420 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
422 editBox.PutS(oldString);
423 if(addedSpaces || addedTabs)
424 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
426 //editBox.PutS(oldString);
429 editBox.GoToPosition(null, y1, x1);
432 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
438 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
442 void Redo(EditBox editBox)
447 editBox.GoToPosition(null, y1, x1);
449 l2 = editBox.lines.first;
450 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
452 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
454 editBox.PutS(newString);
464 static class MoveTextAction : UndoAction
466 int fy1, fx1, fy2, fx2;
468 type = class(MoveTextAction);
470 void Undo(EditBox editBox)
475 void Redo(EditBox editBox)
481 public class EditLine : struct
493 // Only works for single line edit for now...
494 EditBox editBox = this.editBox;
498 get { return this ? buffer : null; }
500 property EditLine prev { get { return this ? prev : null; } };
501 property EditLine next { get { return this ? next : null; } };
502 property int count { get { return this ? count : 0; } };
509 // This makes sure the buffer always contains at least count characters
510 // Keeps a count/2 pad for avoiding always reallocating memory.
511 bool AdjustBuffer(int count)
519 newSize = (count + (count >> 1));
521 // Shrink down the buffer
524 buffer = new char[newSize];
525 if(!buffer) return false;
529 CopyBytes(buffer, this.buffer, count);
532 this.buffer = buffer;
536 // Increase the buffer
537 else if(size < count)
539 buffer = new char[newSize];
540 if(!buffer) return false;
544 CopyBytes(buffer, this.buffer, this.count + 1); // size);
547 this.buffer = buffer;
556 public struct BufferLocation
561 void AdjustDelete(BufferLocation start, BufferLocation end)
563 // Location is before, nothing to do
564 if(y < start.y || (y == start.y && x < start.x))
566 // Location is inside deleted bytes, point to the start
567 if((y >= start.y && (y > start.y || x >= start.x)) &&
568 (y >= end.y && (y > end.y || x >= end.x)))
573 // Location is on another line
575 y -= end.y - start.y;
576 // Location is the last touched line
583 //if(start.line == end.line)
584 x -= end.x - start.x;
593 // Assuming no carriage return before first character ???? fixed?
594 void AdjustAdd(BufferLocation start, BufferLocation end)
596 int numLines = end.y - start.y;
603 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
606 for(c = 0, line = start.line; c<numLines; c++)
609 //x += numLines ? end.x : (end.x - start.x);
610 x += end.x - start.x;
617 public enum EditBoxFindResult { notFound, found, wrapped };
619 static char * keyWords1[] =
622 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
623 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
625 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
626 "__attribute__", "__stdcall", "_stdcall",
627 "__declspec", "goto",
628 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
629 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
632 "class", "private", "public",
634 "delete", "new", "new0", "renew", "renew0", "define",
637 "dllexport", "dllimport", "stdcall",
638 "subclass", "__on_register_module", "namespace", "using",
639 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
640 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
641 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
644 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
647 "this", "true", "false", "null", "value",
656 static char * keyWords2[] =
658 "defined", "warning", null
661 static char ** keyWords[] = { keyWords1, keyWords2 };
662 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
663 //static int * keyLen[NUM_KEYWORD_GROUPS];
664 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
667 static FileFilter filters[] =
669 { "All files", null },
671 "Text Files (*.txt)",
675 static FileListConfig fileListConfig = { "", "", filters, sizeof(filters), null, 0 };
677 static char searchString[1025], replaceString[1025];
678 static bool matchCase = false, wholeWord = false, searchUp = false;
680 static GoToDialog goToDialog
682 autoCreate = false, isModal = true, text = $"Go To"
685 public class EditBox : CommonControl
687 class_property(icon) = "<:ecere>controls/editBox.png";
691 virtual bool Window::NotifyModified(EditBox editBox);
692 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
693 virtual void Window::NotifyUpdate(EditBox editBox);
694 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
695 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
696 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
698 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
699 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
700 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
702 virtual bool Window::NotifyUnsetModified(EditBox editBox);
704 // Why was this commented out?
705 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
706 // and the scrollArea property isn't compared properly either.
710 if(font) ComputeFont();
711 SetInitSize(initSize);
717 style.vScroll = true;
723 style.hScroll = true;
727 property bool textHorzScroll { property_category "Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
728 property bool textVertScroll { property_category "Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
729 property bool readOnly
731 property_category "Behavior"
734 style.readOnly = value;
735 itemEditCut.disabled = value || !selection;
736 itemEditDelete.disabled = value || !selection;
737 itemEditPaste.disabled = value;
739 get { return style.readOnly; }
741 property bool multiLine { property_category "Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
742 property bool freeCaret { property_category "Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
743 property bool tabKey { property_category "Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
744 property int tabSize { property_category "Behavior" set { tabSize = value; } get { return tabSize; } };
745 property bool tabSelection { property_category "Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
746 property bool smartHome { property_category "Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
747 property bool autoEmpty { property_category "Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
748 property bool noCaret { property_category "Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
749 property int maxLineSize { property_category "Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
750 property int maxNumLines { property_category "Behavior" set { maxLines = value; } get { return maxLines; } };
751 property bool useTab { property_category "Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
752 property bool syntaxHighlighting { property_category "Appearance" set { style.syntax = value; } get { return style.syntax; } };
753 property bool noSelect { property_category "Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
754 property bool allCaps { property_category "Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
755 property bool autoSize { property_category "Behavior" set { style.autoSize = value; } get { return style.autoSize; } };
756 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
757 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
758 property int numLines { get { return this ? lineCount : 0; } };
759 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
760 property int column { get { return col; } }; // TODO: Add Set
761 property int charPos { get { return x; } }; // TODO: Add Set
762 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
763 property EditLine lastLine { get { return lines.last; } };
764 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
765 property char * contents
767 property_category "Data"
773 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
776 //SetViewToCursor(true);
784 char * buffer = null;
787 /* Can't implement this right now because of memory leak... Need string reference counting...
794 for(line = lines.first; line; line = line.next)
795 len += strlen(line.buffer);
797 buffer = new char[len+1];
799 for(line = lines.first; line; line = line.next)
801 int lineLen = strlen(line.buffer);
802 memcpy(buffer + len, line.buffer, lineLen);
808 buffer = this.line ? this.line.buffer : null;
813 property bool overwrite { get { return overwrite; } };
814 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
816 property char * multiLineContents
820 char * buffer = null;
827 for(line = lines.first; line; line = line.next)
828 len += strlen(line.buffer)+1;
830 buffer = new char[len+1];
832 for(line = lines.first; line; line = line.next)
834 int lineLen = strlen(line.buffer);
835 memcpy(buffer + len, line.buffer, lineLen);
837 if(line.next) buffer[len++] = '\n';
850 return this.line.buffer;
855 void SetLineText(char * text)
859 EditLine_SetText(this.line, text);
863 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
864 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
865 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
867 // selectionStart.line, selectionStart.column (With Set)
868 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
882 // Position of Caret (Not necessarily displayed position)
885 // Position of beginning of block (Block ends at (x,y))
887 // line is line at carret, selLine is line at beginning of block
888 EditLine line, selLine, dropLine;
889 // Mouse selection Moving virtual caret
894 // ViewX is x offset in pixels, ViewY is y offset in lines
896 // viewLine is first displayed line
899 // start and end of area to redraw
902 // MaxLine is the longest line
904 // MaxLength is the longest line's length
907 // MouseSelect is true once button is pressed, overwrite is true if mode is on
908 bool mouseSelect, mouseMove, overwrite, wordSelect;
909 // Timer is used for mouse selection scrolling
912 window = this, delay = 0.1;
921 OnMouseMove(mouseX, mouseY, -1);
926 // (mouseX,mouseY) is the position of the mouse in the edit box
931 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
934 bool rightButtonDown;
937 UndoBuffer undoBuffer { data = this };
939 Color selectionColor, selectionText;
940 SyntaxColorScheme colorScheme { };
945 Menu editMenu { menu, $"Edit", e };
948 editMenu, $"Cut\tCtrl+X", t, disabled = true;
950 bool NotifySelect(MenuItem item, Modifiers mods)
952 if(!(style.readOnly))
957 MenuItem itemEditCopy
959 editMenu, $"Copy\tCtrl+C", c, disabled = true;
961 bool NotifySelect(MenuItem item, Modifiers mods)
967 MenuItem itemEditPaste
969 editMenu, $"Paste\tCtrl+V", p;
971 bool NotifySelect(MenuItem item, Modifiers mods)
973 if(!(style.readOnly))
978 MenuItem itemEditDelete
980 editMenu, $"Delete\tDel", d, disabled = true;
982 bool NotifySelect(MenuItem item, Modifiers mods)
984 if(!(style.readOnly))
989 MenuDivider { editMenu };
990 MenuItem itemEditSelectAll
992 editMenu, $"Select All\tCtrl+A", a;
994 bool NotifySelect(MenuItem item, Modifiers mods)
1000 MenuDivider { editMenu };
1001 MenuItem itemEditUndo
1003 editMenu, $"Undo\tCtrl+Z", u;
1006 bool NotifySelect(MenuItem item, Modifiers mods)
1012 MenuItem itemEditRedo
1014 editMenu, $"Redo\tCtrl+Y", o;
1017 bool NotifySelect(MenuItem item, Modifiers mods)
1023 MenuDivider { editMenu };
1026 editMenu, $"Find Previous\tShift-F3", e, shiftF3;
1028 bool NotifySelect(MenuItem item, Modifiers mods)
1031 Find(searchString, wholeWord, matchCase, false);
1033 itemEditFind.NotifySelect(this, item, mods);
1039 editMenu, $"Find Next\tF3", n, f3;
1041 bool NotifySelect(MenuItem item, Modifiers mods)
1044 Find(searchString, wholeWord, matchCase, true);
1046 itemEditFind.NotifySelect(this, item, mods);
1050 MenuItem itemEditFind
1052 editMenu, $"Find...\tCtrl+F", f, ctrlF;
1054 bool NotifySelect(MenuItem item, Modifiers mods)
1058 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1059 searchUp = searchUp;
1062 // Fix dialog from above shouldn't be visible
1063 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1064 void NotifyDestroyed(Window window, DialogResult result)
1066 FindDialog dialog = (FindDialog) window;
1067 searchUp = dialog.searchUp;
1068 strcpy(searchString, dialog.searchString);
1069 matchCase = dialog.matchCase;
1070 wholeWord = dialog.wholeWord;
1079 editMenu, $"Replace...\tCtrl+R", r, ctrlR;
1081 bool NotifySelect(MenuItem item, Modifiers mods)
1083 ReplaceDialog dialog
1087 searchString = searchString,
1088 replaceString = replaceString,
1089 matchCase = matchCase,
1090 wholeWord = wholeWord,
1094 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1095 void NotifyDestroyed(Window window, DialogResult result)
1097 ReplaceDialog dialog = (ReplaceDialog)window;
1098 char * replace = dialog.replaceString;
1100 strcpy(replaceString, replace);
1101 strcpy(searchString, dialog.searchString);
1102 matchCase = dialog.matchCase;
1103 wholeWord = dialog.wholeWord;
1110 MenuDivider { editMenu };
1113 editMenu, $"Go To...\tCtrl+G", g, ctrlG;
1115 bool NotifySelect(MenuItem item, Modifiers mods)
1117 goToDialog.editBox = this;
1118 goToDialog.master = master;
1119 goToDialog.Create();
1123 MenuDivider { editMenu };
1124 MenuItem itemEditInsertTab
1126 editMenu, $"Insert Tabs", i, checkable = true;
1128 bool NotifySelect(MenuItem item, Modifiers mods)
1130 style.useTab = item.checked;
1136 snapVertScroll = true;
1137 snapHorzScroll = true;
1141 static bool syntaxInit = false;
1146 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1148 for(c = 0; keyWords[g][c]; c++);
1149 //keyLen[g] = new int[c];
1150 for(c = 0; keyWords[g][c]; c++)
1152 keyLen[g][c] = strlen(keyWords[g][c]);
1156 colorScheme.commentColor = dimGray;
1157 colorScheme.charLiteralColor = crimson;
1158 colorScheme.stringLiteralColor = crimson;
1159 colorScheme.preprocessorColor = green;
1160 colorScheme.numberColor = teal;
1161 colorScheme.keywordColors = [ blue, blue ];
1164 FontExtent = Display::FontExtent;
1166 lines.offset = (uint)&((EditLine)0).prev;
1168 style = EditBoxBits { hScroll = true };
1170 // cursor = guiApp.GetCursor(IBeam);
1172 // Default Properties
1174 maxLineSize = 1024;*/
1177 maxLineSize = MAXINT;
1182 mouseSelect = this.mouseMove = false;
1185 x = selX = selY = 0;
1188 line = selLine = null;
1194 endY = clientSize.h;
1197 // Default space to 8 in case window is not constructed
1200 undoBuffer.dontRecord++;
1202 undoBuffer.dontRecord--;
1204 viewLine = lines.first;
1205 style.recomputeSyntax = true;
1211 UpdateCaretPosition(true);
1217 lines.Free(EditLine::Free);
1220 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1222 int count = wc - *renderStart;
1225 if(y + space.h >= box.top && y <= box.bottom)
1231 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1232 surface.TextFont(font);
1233 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1234 if(*x + w + XOFFSET > 0)
1235 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1240 w = numSpaces; // * space.w;
1241 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1242 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1243 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1251 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1252 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1258 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1262 *foreground = (*selected) ? selectionForeground : textColor;
1263 *background = selectionBackground;
1264 *opacity = *selected;
1271 if(this.overwrite && active)
1273 if((style.stuckCaret && wc == line.count && !line.next) ||
1274 (!mouseMove && line == this.line && wc == editX))
1283 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1287 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1288 bool inString = false;
1289 bool inQuotes = false;
1290 bool inPrep = reset ? false : style.inPrep;
1291 bool inSingleLineComment = false;
1292 bool escaped = reset ? false : style.escaped;
1293 bool continuedSingleLineComment = false;
1295 EditLine line = reset ? lines.first : firstLine;
1296 // int maxBackUp = 1000, c;
1297 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1298 for(; line != viewLine; line = line.next)
1300 char * text = line.buffer;
1303 bool lastWasStar = false;
1304 bool firstWord = true;
1305 if(!escaped) inPrep = false;
1306 inSingleLineComment = continuedSingleLineComment;
1312 for(c = 0; (ch = text[c]); c++)
1314 bool wasEscaped = escaped;
1315 bool backLastWasStar = lastWasStar;
1317 lastWasStar = false;
1320 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1322 if(text[c+1] == '/')
1324 inSingleLineComment = true;
1326 else if(text[c+1] == '*')
1328 inMultiLineComment = true;
1331 else if(backLastWasStar)
1332 inMultiLineComment = false;
1336 if(!c || text[c-1] != '/') lastWasStar = true;
1338 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && ch == '\"')
1340 if(inString && !wasEscaped)
1349 else if(!inSingleLineComment && !inMultiLineComment && !inString && ch == '\'')
1351 if(inQuotes && !wasEscaped)
1365 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && ch == '#')
1377 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1379 style.inMultiLineComment = inMultiLineComment;
1380 style.inPrep = inPrep;
1381 style.escaped = escaped;
1385 /*void OnDrawOverChildren(Surface surface)
1387 if(style.lineNumbers)
1389 int currentLineNumber = this.viewY + 1;
1392 for(i = 0; i * space.h < box.height; i++)
1394 // ********* LINE NUMBERING *********
1395 surface.SetForeground(Color{60, 60, 60});
1396 //Highlight current line
1397 if(this.caretY / space.h == currentLineNumber - 1)
1398 surface.SetBackground(Color{220, 220, 220});
1400 surface.SetBackground(Color{230, 230, 230});
1401 surface.textOpacity = true;
1403 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1404 if(currentLineNumber > this.lineCount)
1405 surface.WriteText(0,i*space.h+1," ",6);
1407 surface.WriteText(0,i*space.h+1,lineText,6);
1409 currentLineNumber++;
1414 void OnRedraw(Surface surface)
1418 bool selected = false, selection = true;
1420 Color selectionBackground = selectionColor ? selectionColor : SELECTION_COLOR;
1421 Color selectionForeground = selectionText ? selectionText : SELECTION_TEXT;
1422 Color defaultTextColor = property::foreground;
1425 int maxW = clientSize.w;
1427 Color foreground, background;
1430 // Overwrite Caret Stuff
1431 bool overWrite = false;
1432 int overWriteX, overWriteY;
1435 // ****** SYNTAX STATES ******
1436 bool inMultiLineComment = style.inMultiLineComment;
1437 bool inString = false;
1438 bool inQuotes = false;
1439 bool inPrep = style.inPrep;
1440 bool inSingleLineComment = false;
1441 bool escaped = style.escaped;
1442 bool continuedSingleLineComment = false;
1443 // ****** ************* ******
1446 defaultTextColor = Color { 85, 85, 85 };
1447 textColor = defaultTextColor;
1450 Abs(selectionBackground.r - property::background.r) +
1451 Abs(selectionBackground.g - property::background.g) +
1452 Abs(selectionBackground.b - property::background.b) < 92)
1454 selectionBackground = activeBorder;
1455 selectionForeground = selectionColor ? selectionColor : SELECTION_COLOR;
1458 surface.TextFont(this.font);
1460 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1461 surface.Area(0,0,MAXINT,MAXINT);
1466 surface.SetBackground(activeBorder);
1467 surface.Area(0,0,clientSize.w, clientSize.h);
1470 if(this.selX == this.x && this.selY == this.y)
1480 editX = Min(this.x,this.line.count);
1481 selX = Min(this.selX,this.selLine.count);
1484 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1486 foreground = selected ? selectionForeground : textColor;
1487 background = selectionBackground;
1493 surface.SetForeground(foreground);
1494 surface.SetBackground(background);
1495 surface.TextOpacity(opacity);
1497 surface.GetBox(box);
1499 for(line = this.viewLine; line; line = line.next)
1501 int x = -this.viewX;
1505 Color newTextColor = textColor = defaultTextColor;
1506 bool lineComplete = false;
1509 // ****** SYNTAX HIGHLIGHTING ******
1510 bool lastWasStar = false;
1511 bool firstWord = true;
1512 if(!escaped) inPrep = false;
1513 inSingleLineComment = continuedSingleLineComment;
1517 // *********************************
1519 /* === DEBUGGING TOOL FOR MAXLINE ===
1521 if(line == this.maxLine)
1523 surface.SetBackground(GREEN|0xFF000000);
1524 surface.TextOpacity(true);
1528 surface.TextOpacity(selected ? true : false);
1529 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1533 if(line == this.selLine && line == this.line)
1535 end = Max(line.count, this.x);
1536 end = Max(end, this.selX);
1538 else if(line == this.selLine)
1539 end = Max(line.count, this.selX);
1540 else if(line == this.line)
1541 end = Max(line.count, this.x);
1549 bool spacing = true;
1550 bool cantHaveWords = false;
1557 lineComplete = false;
1560 textColor = newTextColor;
1562 surface.SetForeground(textColor);
1565 for(; c<end && !cantHaveWords;)
1569 bufferLen += wordLen;
1573 /*if(line.count > 4000)
1580 for(; c<line.count; c++)
1582 unichar ch = line.buffer[c];
1583 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1584 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1585 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1586 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1587 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1595 for(; c<line.count; c++)
1597 unichar ch = line.buffer[c];
1598 if(ch == '\t' || ch == ' ')
1600 cantHaveWords = true;
1604 if(ch != ' ' && ch != '\t')
1609 if(c == line.count && c < end)
1618 cantHaveWords = true;
1623 lastWasStar = false;
1629 bool backEscaped = escaped;
1630 bool backLastWasStar = lastWasStar;
1631 bool backInMultiLineComment = inMultiLineComment;
1632 bool backInString = inString;
1633 bool backInQuotes = inQuotes;
1634 bool backInPrep = inPrep;
1635 bool backInSingleLineComment = inSingleLineComment;
1637 char * word = line.buffer + c - wordLen;
1639 bool wasEscaped = escaped;
1641 lastWasStar = false;
1643 // Determine Syntax Highlighting
1644 newTextColor = defaultTextColor;
1647 if(inSingleLineComment || inMultiLineComment)
1649 newTextColor = colorScheme.commentColor;
1653 newTextColor = colorScheme.charLiteralColor;
1657 newTextColor = colorScheme.stringLiteralColor;
1661 newTextColor = colorScheme.preprocessorColor;
1663 if(wordLen == 1 && word[0] == '/')
1665 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1669 inSingleLineComment = true;
1670 newTextColor = colorScheme.commentColor;
1672 else if(word[1] == '*')
1674 inMultiLineComment = true;
1675 newTextColor = colorScheme.commentColor;
1678 else if(backLastWasStar)
1679 inMultiLineComment = false;
1681 else if(wordLen == 1 && word[0] == '*')
1683 if(!c || word[-1] != '/')
1686 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1688 if(inString && !wasEscaped)
1695 newTextColor = colorScheme.stringLiteralColor;
1698 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1700 if(inQuotes && !wasEscaped)
1705 newTextColor = colorScheme.charLiteralColor;
1708 else if(wordLen == 1 && word[0] == '\\')
1713 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1714 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1716 newTextColor = colorScheme.numberColor;
1720 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1725 newTextColor = colorScheme.preprocessorColor;
1728 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1730 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1732 char ** keys = keyWords[g];
1733 int * len = keyLen[g];
1734 for(ccc = 0; keys[ccc]; ccc++)
1736 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1738 newTextColor = colorScheme.keywordColors[g];
1747 // If highlighting for this word is different, break
1748 if(newTextColor != textColor)
1752 // Better solution than going back?
1755 // Reset syntax flags
1756 escaped = backEscaped;
1757 lastWasStar = backLastWasStar;
1758 inMultiLineComment = backInMultiLineComment;
1759 inString = backInString;
1760 inQuotes = backInQuotes;
1761 inPrep = backInPrep;
1762 inSingleLineComment = backInSingleLineComment;
1767 textColor = newTextColor;
1769 surface.SetForeground(textColor);
1775 // If we're not breaking, this can't be rendered as spacing anymore
1778 // Do word wrapping here
1784 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1789 w += numSpaces * space.w;
1791 if(x + viewX + w > maxW)
1794 lineComplete = true;
1799 bufferLen += wordLen;
1804 int renderStart = start;
1809 // Render checking if we need to split because of selection or to find where to draw insert caret
1810 for(wc = start; wc < start + bufferLen; wc++)
1812 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1813 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1814 if(overWrite == true)
1816 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1817 if(overWriteCh == '\t') overWriteCh = ' ';
1822 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1823 if(overWrite == true)
1831 surface.TextOpacity(opacity);
1832 surface.SetBackground(background);
1833 surface.SetForeground(foreground);
1840 if(wc < line.count && line.buffer[wc] == '\t')
1842 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1846 numSpaces += space.w;
1850 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1855 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1856 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1858 if(overWrite == true)
1865 surface.TextOpacity(opacity);
1866 surface.SetBackground(background);
1867 surface.SetForeground(foreground);
1870 if(style.freeCaret && selected)
1872 surface.SetBackground(selectionBackground);
1873 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1874 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1879 if(style.freeCaret && selected)
1881 surface.SetBackground(selectionBackground);
1882 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1886 continuedSingleLineComment = inSingleLineComment && (line.count && line.text[line.count - 1] == '\\');
1889 if(y > box.bottom) // >=clientSize.h)
1895 surface.TextOpacity(true);
1896 surface.SetForeground(black);
1897 surface.SetBackground(Color {255,255,85});
1898 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1902 void FixScrollArea()
1904 if(style.hScroll || style.vScroll)
1906 int width = maxLength + XOFFSET;
1907 int height = lineCount * space.h;
1908 if(style.freeCaret && line)
1913 width = Max(line.length + (x - line.count) * space.w, maxLength);
1917 if(selX > selLine.count)
1918 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1923 SetScrollLineStep(8, space.h);
1924 SetScrollArea(width, height, true);
1928 void ComputeLength(EditLine line)
1935 for(c = 0; c < line.count; )
1944 for(len = 0; c < line.count; c += numBytes)
1946 ch = line.buffer[c];
1947 numBytes = UTF8_NUM_BYTES(ch);
1949 if(ch == ' ' || ch == '\t')
1956 if(!len && ch == ' ')
1961 else if(!len && ch == '\t')
1963 w = (tabSize * space.w) - (x % (tabSize * space.w));
1967 FontExtent(display, font, line.buffer + start, len, &w, null);
1978 if(line.length > this.maxLength)
1980 this.maxLine = line;
1981 this.maxLength = line.length;
1990 this.maxLine = null;
1992 for(line = lines.first; line; line = line.next)
1994 if(line.length > this.maxLength)
1996 this.maxLength = line.length;
1997 this.maxLine = line;
2004 if(this.selY != this.y)
2006 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2010 void ComputeColumn()
2012 // Adjust new edit X position according to tabs
2013 int c, position = 0;
2016 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2018 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2019 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2021 position += this.tabSize - (position % this.tabSize);
2025 position += this.x - c;
2026 this.col = position;
2029 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2031 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2034 bool HasCommentOrEscape(EditLine line)
2036 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2041 for(c = line.count-1; c >= 0; c--)
2043 char ch = line.buffer[c];
2049 else //if(ch != ' ' && ch != '\t')
2056 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2058 EditLine line = l1, next;
2060 int oldCount1, oldCount2;
2061 int y, firstViewY, firstY, firstDropY, firstSelY;
2064 DelTextAction action = null;
2065 bool hadComment = false;
2069 hadComment = HasCommentOrEscape(line);
2071 if(y2 > y1 || c2 > c1)
2073 if(start < l1.count)
2075 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2079 oldCount1 = l1.count;
2081 while(c1 < oldCount1)
2083 byte ch = buffer[c1];
2084 if(UTF8_IS_FIRST(ch)) break;
2088 oldCount2 = l2.count;
2090 while(c2 < oldCount2)
2092 byte ch = buffer[c2];
2093 if(UTF8_IS_FIRST(ch)) break;
2098 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2103 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2104 string = new char[len];
2105 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2106 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2110 //oldCount1 = l1.count;
2111 //oldCount2 = l2.count;
2114 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2115 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2118 if(c2 > oldCount2) c2 = oldCount2;
2119 if(!(style.freeCaret))
2120 if(c1 > oldCount1) c1 = oldCount1;
2121 newLineCount = c1 + l2.count-c2;
2127 buffer = new char[line.size ? line.size : 1];
2129 buffer = new char[line.size];
2131 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2136 if(!line.AdjustBuffer(newLineCount))
2140 /*if(newLineCount > 4000 || newLineCount < 0)
2141 printf("Warning");*/
2143 line.count = newLineCount;
2145 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2150 action.addedSpaces = c1-oldCount1;
2152 if(action.addedSpaces > action.x1)
2158 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2159 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2161 line.buffer[line.count] = '\0';
2168 this.dropX -= c2-c1;
2169 this.dropX = Max(this.dropX,0);
2171 this.x = Max(this.x,0);
2178 firstViewY = this.viewY;
2180 firstDropY = this.dropY;
2181 firstSelY = this.selY;
2182 for(line = l1;line;line = next, y++)
2190 if(line == this.viewLine)
2192 if(this.viewLine.next)
2194 this.viewLine = this.viewLine.next;
2196 style.recomputeSyntax = true;
2200 this.viewLine = this.viewLine.prev;
2202 style.recomputeSyntax = true;
2205 else if(y < firstViewY)
2207 if(line == this.line)
2211 this.line = this.line.next;
2212 this.x = this.line.count;
2217 this.line = this.line.prev;
2218 this.x = this.line.count;
2225 if(line == this.dropLine)
2227 if(this.dropLine.next)
2229 this.dropLine = this.dropLine.next;
2230 this.dropX = this.dropLine.count;
2234 this.dropLine = this.dropLine.prev;
2235 this.dropX = this.dropLine.count;
2239 else if(y < firstDropY)
2241 if(line == this.selLine)
2243 if(this.selLine.next)
2245 this.selLine = this.selLine.next;
2246 this.selX = this.selLine.count;
2250 this.selLine = this.selLine.prev;
2251 this.selX = this.selLine.count;
2255 else if(y < firstSelY)
2259 if(line == l2) break;
2263 if(style.autoSize) AutoSize();
2264 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2267 style.recomputeSyntax = true;
2272 bool DelSel(int * addedSpacesPtr)
2274 if(this.line != this.selLine || this.x != this.selX)
2276 if(this.selY < this.y)
2278 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2281 this.line = this.selLine;
2283 else if(this.selY > this.y)
2285 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2288 this.selLine = this.line;
2290 else if(this.selX < this.x)
2292 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2295 this.line = this.selLine;
2299 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2302 this.selLine = this.line;
2310 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2312 bool hadComment = false;
2313 // Add the line here
2314 EditLine line = this.line;
2316 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2317 if(!style.hScroll && created)
2319 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2324 // Lock if no place to display.
2327 else if(endX > this.x)
2328 max = Max(this.x, line.count);
2332 for(x = 0, c = 0; c < max+count; )
2337 if(c < Min(this.x, line.count))
2338 string = line.buffer + c;
2341 else if(c < endX + count)
2342 string = stringLine + c - endX;
2344 string = line.buffer + c - endX - count;
2348 w = (tabSize * space.w) - (x % (tabSize * space.w));
2352 numBytes = UTF8_NUM_BYTES(*string);
2353 FontExtent(display, this.font, string, numBytes, &w, null);
2357 if(x >= clientSize.w)
2362 c += numBytes; // - 1;
2368 int addedSpaces = 0;
2371 // Add blank spaces if EES_FREECARET
2372 if(this.x > line.count)
2382 for(c = 0; c<line.count; c++)
2384 if(this.line.buffer[c] == '\t')
2385 position += this.tabSize - (position % this.tabSize);
2389 wantedPosition = position + (this.x - line.count);
2391 // A tab is too much...
2392 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2393 addedSpaces = wantedPosition - position;
2398 position += this.tabSize - (position % this.tabSize);
2399 // Add as many tabs as needed
2400 addedTabs += (wantedPosition - position) / this.tabSize;
2401 position += (addedTabs-1) * this.tabSize;
2402 // Finish off with spaces
2403 addedSpaces = wantedPosition - position;
2407 addedSpaces = this.x - line.count;
2410 this.x = Min(this.x, line.count);
2412 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2418 hadComment = HasCommentOrEscape(line);
2420 // Adjust the size of the line
2421 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2425 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2428 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2429 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2432 *addedTabsPtr = addedTabs;
2433 FillBytes(line.buffer+line.count,'\t',addedTabs);
2435 if(addedTabs > 4000 || addedTabs < 0)
2438 line.count += addedTabs;
2444 FillBytes(line.buffer+line.count,' ',addedSpaces);
2446 if(addedSpaces > 4000 || addedSpaces < 0)
2449 line.count += addedSpaces;
2450 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2452 else if(addedSpacesPtr)
2453 *addedSpacesPtr = 0;
2455 if(count > 4000 || count < 0)
2458 line.count += count;
2459 this.x += count + addedTabs + addedSpaces;
2462 this.selLine = this.line;
2464 line.buffer[line.count] = '\0';
2466 ComputeLength(line);
2471 hasComment = HasCommentOrEscape(line);
2472 if(!undoBuffer.insideRedo)
2474 int backDontRecord = undoBuffer.dontRecord;
2475 undoBuffer.dontRecord = 0;
2476 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2477 if(style.autoSize) AutoSize();
2478 undoBuffer.dontRecord = backDontRecord;
2480 if(style.syntax && (hadComment || hasComment || line != this.line))
2482 style.recomputeSyntax = true;
2490 void Emptyline(EditLine line, int y)
2493 DelCh(line, y, 0, line.next, y+1, 0, true);
2495 DelCh(line, y, 0, line, y, line.count, true);
2498 void GoToEnd(bool deselect)
2502 this.line = this.lines.last;
2503 if(this.y != this.lineCount-1)
2505 else if (this.x != this.line.count)
2506 DirtyLine(this.lineCount-1);
2507 this.y = this.lineCount-1;
2508 this.x = this.line.count;
2515 void GoToHome(bool deselect)
2519 this.line = this.lines.first;
2522 else if (this.x !=0)
2523 DirtyLine(this.lineCount-1);
2532 // Returns true if it needs scrolling
2533 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2539 bool needHScroll = false;
2546 line = this.viewLine ? (void *)this.viewLine.prev : null;
2551 line = (void *)this.lines.first;
2556 py = Min(py, clientSize.h);
2558 py = Min(py, this.lineCount);
2560 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2566 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2569 px = Min(px,clientSize.w+this.space.w);
2573 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2576 if(tline) *tline = line;
2579 // Prevent divide by 0 from non valid this.font
2581 return (y < this.viewY) || needHScroll;
2583 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2587 // Minimal Update Management Functions
2591 this.endY = clientSize.h-1;
2592 // ErrorLog("DirtyAll\n");
2595 void DirtyEnd(int y)
2597 if((y - this.viewY)*this.space.h < this.startY)
2598 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2599 this.endY = clientSize.h-1;
2600 //ErrorLog("DirtyEnd %d\n", y);
2603 void DirtyLine(int y)
2607 if((y - this.viewY)*this.space.h < this.startY)
2608 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2609 if((y - this.viewY+1)*this.space.h > this.endY)
2610 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2612 //ErrorLog("DirtyLine %d\n", y);
2619 if(style.recomputeSyntax)
2621 FigureStartSyntaxStates(lines.first, true);
2622 style.recomputeSyntax = false;
2625 if(this.startY > this.endY) return;
2626 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2632 box.right = clientSize.w-1;
2633 box.top = this.startY;
2634 box.bottom = this.endY;
2637 this.startY = clientSize.h;
2641 bool IsMouseOnSelection()
2643 bool mouseOnSelection = false;
2646 int minY = Min(this.selY, this.y);
2647 int maxY = Max(this.selY, this.y);
2648 int minX = Min(this.selX, this.x);
2649 int maxX = Max(this.selX, this.x);
2651 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2653 if(maxX != minX || maxY != minY)
2655 if(y > minY && y < maxY)
2656 mouseOnSelection = true;
2657 else if(y == minY && y == maxY)
2658 mouseOnSelection = (x < maxX && x >= minX);
2662 mouseOnSelection = (x >= this.selX);
2663 else if(y == this.y)
2664 mouseOnSelection = (x >= this.x);
2669 mouseOnSelection = (x < this.selX);
2670 else if(y == this.y)
2671 mouseOnSelection = (x < this.x);
2674 return mouseOnSelection;
2677 void UpdateCaretPosition(bool setCaret)
2681 if(mouseMove || (!overwrite && !style.noCaret))
2683 int max = this.mouseMove ? this.dropX : this.x;
2684 int y = this.mouseMove ? this.dropY : this.y;
2685 EditLine line = this.mouseMove ? this.dropLine : this.line;
2687 if(!(style.freeCaret))
2688 max = Min(max, line.count);
2690 if(FontExtent && display)
2692 for(c = 0; c < max; )
2701 for(len = 0; c < Min(max, line.count); c += numBytes)
2703 ch = line.buffer[c];
2704 numBytes = UTF8_NUM_BYTES(ch);
2706 if(ch == ' ' || ch == '\t')
2713 if(!len && ch == ' ')
2718 else if(!len && ch == '\t')
2720 w = (tabSize * space.w) - (x % (tabSize * space.w));
2724 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2736 caretY = y * this.space.h;
2737 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2742 NotifyCaretMove(master, this, y + 1, x + 1);
2748 void SelectionEnables()
2750 if((x != selX || y != selY) && !selection)
2754 itemEditCut.disabled = false;
2755 itemEditDelete.disabled = false;
2757 itemEditCopy.disabled = false;
2759 this.selection = true;
2761 else if((x == selX && y == selY) && selection)
2763 itemEditCut.disabled = true;
2764 itemEditCopy.disabled = true;
2765 itemEditDelete.disabled = true;
2767 this.selection = false;
2771 void SetSelectCursor()
2773 if(!inactive || !style.noSelect)
2776 cursor = guiApp.GetCursor(arrow);
2777 else if(this.mouseSelect)
2778 cursor = guiApp.GetCursor(iBeam);
2781 if(IsMouseOnSelection())
2782 cursor = guiApp.GetCursor(arrow);
2784 cursor = guiApp.GetCursor(iBeam);
2789 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2792 int x = px ? *px : 0;
2799 if(c < Min(max, line.count))
2803 for(len = 0; c < Min(max, line.count); c += numBytes)
2805 ch = line.buffer[c];
2806 numBytes = UTF8_NUM_BYTES(ch);
2808 if(ch == ' ' || ch == '\t')
2815 if(!len && ch == ' ')
2820 else if(!len && ch == '\t')
2822 w = (tabSize * space.w) - (x % (tabSize * space.w));
2826 FontExtent(display, font, line.buffer + start, len, &w, null);
2830 if(style.freeCaret && c < max)
2839 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2844 int a = start + len;
2847 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2851 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2854 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2858 return Min(this.maxLineSize - 1, start + len);
2864 void SetCursorToViewX()
2866 bool selecting = this.x != selX || y != selY;
2868 // Horizontal Adjustment
2870 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2875 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2887 UpdateCaretPosition(false);
2893 void SetCursorToViewY()
2896 EditLine oldLine = this.line;
2898 bool selecting = this.x != this.selX || this.y != this.selY;
2900 numLines = clientSize.h / this.space.h;
2902 // Vertical Adjustment
2903 if(this.viewY > this.y)
2905 this.y = this.viewY;
2906 this.line = this.viewLine;
2909 if(this.viewY + numLines <= this.y)
2913 this.y = this.viewY-1;
2914 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2921 if(this.line != oldLine)
2923 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2931 this.selLine = this.line;
2934 UpdateCaretPosition(false);
2941 bool SaveFile(char * fileName)
2943 File f = eFile_Open(fileName, FO_WRITE);
2947 eWindow_SetModified(false);
2948 eInstance_Delete(f);
2955 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2959 if(!style.multiLine)
2961 x = (active && this.active && !style.readOnly) ? line.count : 0;
2964 SetViewToCursor(true);
2969 if(!active && modified)
2972 if(!NotifyModified(master, this))
2975 *goOnWithActivation = false;
2982 if(timer) timer.Stop();
2984 mouseSelect = false;
2996 // todo: resize width based on largest on-screen-line extent...
2998 display.FontExtent(font, " ", 1, null, &sh);
3002 nh = lineCount * sh;
3003 size.h = nh < minClientSize.h ? minClientSize.h : nh;
3014 int len = line ? strlen(line.text) : 0;
3015 GetDecorationsSize(&dw, &dh);
3016 display.FontExtent(font, " ", 1, null, &sh);
3017 if(len) display.FontExtent(font, line.text, len, &tw, null);
3019 if(nw < minClientSize.w) nw = minClientSize.w;
3021 if(nh < minClientSize.h) nh = minClientSize.h;
3027 bool OnResizing(int *w, int *h)
3033 *w = space.h * 80 / 14;
3037 void OnResize(int w, int h)
3040 if(!hasHorzScroll && !hasVertScroll && viewLine)
3041 SetViewToCursor(true);
3043 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3045 SetViewToCursor(true);
3050 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3052 if(style.readOnly) return true;
3053 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3054 // Middle button already serves as a dragger as well.
3059 bool OnRightButtonDown(int x, int y, Modifiers mods)
3061 this.rightButtonDown = true;
3066 bool OnRightButtonUp(int x, int y, Modifiers mods)
3069 if(!parent.inactive && rightButtonDown)
3072 Menu contextMenu { };
3074 MenuItem { contextMenu, $"Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3075 MenuItem { contextMenu, $"Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3076 MenuItem { contextMenu, $"Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3077 MenuItem { contextMenu, $"Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3078 MenuDivider { contextMenu };
3079 MenuItem { contextMenu, $"Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3081 popup = PopupMenu { master = this, menu = contextMenu,
3083 nonClient = true, interim = false, parent = parent,
3084 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3086 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3090 rightButtonDown = false;
3094 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3099 if(style.noSelect) return true;
3101 // Should we have a separate 'selectOnActivate' style?
3102 if(!mods.isActivate || (style.readOnly && style.multiLine))
3108 mouseX = mx - XOFFSET;
3111 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3113 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3119 else if(IsMouseOnSelection() && !mods.isActivate)
3129 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3131 if(mods.shift && !mods.isActivate)
3146 this.selLine = this.line;
3155 UpdateCaretPosition(true);
3159 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3163 mouseSelect = false;
3174 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3176 //PrintLn("MouseMove: ", x, ", ", y);
3182 mouseMove = IsMouseOnSelection();
3186 int size = SelSize();
3189 char * text = new char[size+1];
3193 GetSel(text, false);
3195 if(Max(selY, this.y) == dropY)
3199 if(this.dropX > this.selX)
3200 moveX = this.x - this.selX;
3204 if(this.dropX > this.x)
3205 moveX = this.selX - this.x;
3209 this.dropX -= moveX;
3210 this.selX = this.x = this.dropX;
3211 this.selY = this.y = this.dropY;
3212 this.selLine = this.line = this.dropLine;
3214 SetViewToCursor(true);
3219 byte * c = ((EditBox)this).multiLineContents, * c2;
3220 int l1 = c ? strlen(c) : 0, l2;
3223 c2 = ((EditBox)this).multiLineContents;
3224 l2 = c2 ? strlen(c2) : 0;
3225 if(l1 != l2 || (l1 && strcmp(c, c2)))
3255 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3257 //PrintLn("Dropped: ", x, ", ", y);
3259 NotifyDropped(master, this, x, y);
3266 bool OnMouseMove(int mx, int my, Modifiers mods)
3272 if(mods != -1 && mods.isSideEffect)
3277 if(style.noSelect) return true;
3278 if(wordSelect) return true;
3279 mouseX = mx - XOFFSET;
3282 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3284 if(this.mouseMove || this.mouseSelect)
3293 ((style.hScroll) || (style.vScroll)))
3300 DirtyLine(this.dropY);
3303 DirtyLine(this.dropY);
3304 this.dropLine = line;
3305 SetViewToCursor(true);
3307 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3310 else if(this.mouseSelect)
3312 DirtyLine(this.selY);
3319 SetViewToCursor(true);
3322 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3329 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3334 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3338 if(style.noSelect) return true;
3339 FindMouse(mx, my, &x, &y, &line, false);
3340 if(!NotifyDoubleClick(master, this, line, mods))
3347 for(c = x; c >= 0; c--)
3350 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3351 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3358 for(c = start; c<line.count; c += numBytes)
3360 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3371 this.line = this.selLine = line;
3372 this.wordSelect = (c != start);
3384 Menu fileMenu { menu, "File", F };
3385 saveDialog = fileDialog;
3386 MenuItem { fileMenu, "Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3387 MenuItem { fileMenu, "Save As...", A, NotifySelect = MenuFileSaveAs };
3397 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3398 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3400 space.w = Max(space.w, 1);
3401 large.w = Max(large.w, 1);
3402 space.h = Max(space.h, 1);
3403 large.h = Max(large.h, 1);
3407 for(line = lines.first; line; line = line.next)
3408 ComputeLength(line);
3413 SetViewToCursor(true);
3421 bool OnLoadGraphics()
3423 FontExtent = Display::FontExtent;
3426 // UpdateCaretPosition(true);
3430 void OnUnloadGraphics()
3435 bool OnKeyHit(Key key, unichar ch)
3437 bool shift = (key.shift) ? true : false;
3439 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3441 if(!ch && !key.alt && !key.ctrl)
3443 key.code = (SmartKey)key.code;
3446 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3449 if(style.readOnly) break;
3450 if(style.stuckCaret) GoToEnd(true);
3451 if(!(style.freeCaret))
3453 this.x = Min(this.x, this.line.count);
3459 EditLine line = this.line;
3461 for(y = this.y; y>= 0; y--)
3463 c = (y == this.y) ? (this.x-1) : line.count-1;
3465 // Slow down when going on lines...
3466 if(y != this.y) break;
3470 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3471 if(IS_ALUNDER(line.buffer[c]))
3476 if(!IS_ALUNDER(line.buffer[c]))
3478 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3492 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3493 this.x = this.selX = Min(c+1, line.count);
3494 this.y = this.selY = y;
3495 this.line = this.selLine = line;
3496 SetViewToCursor(true);
3507 if(style.readOnly) break;
3508 if(style.stuckCaret) break;
3517 if(this.line != this.selLine || this.x != this.selX)
3520 SetViewToCursor(true);
3526 if(this.x < this.line.count)
3530 for(i = this.x; i < this.line.count; i++)
3532 if(!IS_ALUNDER(this.line.buffer[i]))
3536 for(; i < this.line.count; i++)
3538 //Delete trailing whitespace
3539 if(IS_ALUNDER(this.line.buffer[i]))
3542 DelCh(this.line, this.y, this.x, this.line, this.y, i, false);
3543 SetViewToCursor(true);
3546 else if(this.line.next)
3548 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3549 SetViewToCursor(true);
3555 if(!(style.freeCaret))
3557 this.selX = this.x = Min(this.x, this.line.count);
3560 if(this.x < this.line.count)
3562 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3564 else if(this.line.next)
3566 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3568 SetViewToCursor(true);
3577 if(!key.alt && !key.ctrl)
3581 bool stuffAfter = false;
3585 if(style.stuckCaret) GoToEnd(true);
3586 if(style.readOnly) break;
3587 if(!(style.multiLine)) break;
3589 for(c = 0; c<this.line.count && c<this.x; c++)
3591 if(this.line.buffer[c] == '\t')
3592 position += this.tabSize - (position % this.tabSize);
3593 else if(this.line.buffer[c] == ' ')
3601 if(this.x < this.line.count)
3604 //If last character is a { indent one tab
3605 if(this.line.buffer[this.x - 1] == '{')
3607 //Except if the next non space character is a }
3608 bool indent = false;
3610 for(i = this.x; i < this.line.size; i++)
3611 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3613 if(this.line.buffer[i] != '}')
3618 position += this.tabSize;
3621 addString = new char[position + 2];
3622 addString[len++] = '\n';
3623 addString[len] = '\0';
3625 if(stuffAfter || !style.freeCaret)
3627 for(c = 0; c<position; )
3629 if(style.useTab && c + this.tabSize <= position)
3631 addString[len++] = '\t';
3636 addString[len++] = ' ';
3640 addString[len] = '\0';
3644 if(!stuffAfter && style.freeCaret)
3646 this.x = this.selX = position;
3650 SetViewToCursor(true);
3660 if(style.stuckCaret) break;
3661 if(!(style.freeCaret))
3663 this.x = Min(this.x, this.line.count);
3664 this.selX = Min(this.selX, this.selLine.count);
3667 if(!shift) SelDirty();
3670 bool foundAlpha = false;
3673 EditLine line, lastLine;
3676 for(line = this.line; (line && !found); line = line.prev, y--)
3681 if(this.x == 0 && line != this.line)
3690 if(line == this.line) start = this.x -1; else start = line.count-1;
3691 start = Min(start, line.count-1);
3693 for(c = start; c >= 0;)
3696 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3714 byte ch = line.buffer[c];
3715 if(UTF8_IS_FIRST(ch)) break;
3719 // No next word found,
3720 if(!found && ( this.x > 0 || (!line.count && this.x)))
3734 this.line = lastLine;
3745 byte * buffer = line.buffer;
3748 byte ch = buffer[x];
3749 if(UTF8_IS_FIRST(ch)) break;
3766 if(!shift) Deselect();
3767 SetViewToCursor(true);
3773 if(style.stuckCaret) break;
3774 if(!(style.freeCaret))
3776 this.x = Min(this.x, this.line.count);
3777 this.selX = Min(this.selX, this.selLine.count);
3780 if(!shift) SelDirty();
3781 if(!shift && (this.x != this.selX || this.y != this.selY));
3784 bool onAChar = false;
3785 if(this.selX != this.x || this.selY != this.y)
3787 if(this.x<this.line.count)
3788 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3790 if(key.shift && onAChar &&
3791 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3793 bool foundAlpha = false;
3795 EditLine line, lastLine;
3797 int lastC, lastY, lastNumBytes;
3799 for(line = this.line; (line && !found); line = line.next, y++)
3801 int start = (line == this.line) ? this.x : 0;
3804 for(c = start; c < line.count; c += numBytes)
3806 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3811 lastNumBytes = numBytes;
3821 if(!found && (c != this.x || line != this.line))
3834 this.x = lastC + lastNumBytes;
3836 this.line = lastLine;
3843 bool foundAlpha = false;
3848 for(line = this.line; (line && !found); line = line.next, y++)
3850 int start = (line == this.line) ? this.x : 0;
3853 for(c = start; c < line.count; c += numBytes)
3855 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3870 // No next word found,
3871 if(!found && (c != this.x || line != this.line))
3875 this.x = line.count;
3887 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3891 byte * buffer = line.buffer;
3894 byte ch = buffer[x];
3895 if(UTF8_IS_FIRST(ch)) break;
3916 if(!shift) Deselect();
3917 SetViewToCursor(true);
3924 if(!style.vScroll || hasVertScroll) break;
3930 if(style.stuckCaret) break;
3932 if(!shift) SelDirty();
3942 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3946 if(!shift) Deselect();
3948 SetViewToCursor(false);
3951 if(caretY == this.y * space.h)
3957 if(!style.freeCaret)
3958 this.x = Min(this.x, line.count);
3968 int sx = 0, sy = this.y * space.h;
3969 char * text = line.text;
3970 int maxW = clientSize.w - sx;
3971 display.FontExtent(font, " ", 1, null, &th);
3975 int startPos = textPos;
3978 bool lineComplete = false;
3980 if(!style.wrap && caretY == MAXINT)
3983 //textPos = line.count;
3984 //lineComplete = true;
3987 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3991 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3994 len = (nextSpace - (text + textPos));
3996 len = line.count - textPos;
3998 if(textPos < line.count)
4000 display.FontExtent(font, text + textPos, len, &w, null);
4002 if(nextSpace) { w += space.w; len++; }
4004 if(style.wrap && x + width + w > maxW && x > 0)
4006 lineComplete = true;
4016 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
4020 while(this.x > 0 && x + sx > caretX && this.x > startPos)
4023 if(this.x > line.count)
4026 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4027 len = this.x - startPos;
4028 display.FontExtent(font, text + startPos, len, &x, null);
4032 if(!shift) Deselect();
4034 SetViewToCursor(false);
4038 if(sy == caretY - th || textPos >= line.count)
4040 if(textPos >= line.count)
4042 int c = textPos - 1;
4043 while(c > 0 && text[c] == ' ') c--;
4047 this.x = line.count;
4050 if(!shift) Deselect();
4052 SetViewToCursor(false);
4057 } while(textPos < line.count);
4060 if(!shift) Deselect();
4062 SetViewToCursor(false);
4071 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4072 if(!shift) SelDirty();
4073 this.line = this.line.prev;
4079 if(!shift) Deselect();
4083 SetViewToCursor(false);
4089 return style.multiLine ? false : true;
4093 if(!style.vScroll || hasVertScroll)
4100 if(style.stuckCaret) break;
4104 int sx = 0, sy = this.y * this.space.h;
4105 int maxW = clientSize.w - sx;
4106 char * text = line.buffer;
4108 if(!shift) SelDirty();
4114 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4124 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4127 if(!shift) Deselect();
4129 if(this.selX != this.x || this.selY != this.y)
4131 SetViewToCursor(false);
4134 while(!textPos || (style.freeCaret || textPos<line.count))
4136 int startPos = textPos;
4139 bool lineComplete = false;
4140 if(!style.wrap && sy <= caretY)
4142 textPos = line.count;
4143 lineComplete = true;
4145 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4149 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4152 len = (nextSpace - (text + textPos));
4154 len = line.count - textPos;
4156 if(textPos < line.count)
4158 display.FontExtent(font, text + textPos, len, &w, &th);
4160 if(nextSpace) { w += space.w; len++; }
4161 if(style.wrap && x + width + w > maxW && x > 0)
4163 lineComplete = true;
4173 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4177 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4180 if(this.x > line.count)
4183 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4185 len = this.x - startPos;
4186 display.FontExtent(font, text + startPos, len, &x, null);
4189 if(!shift) Deselect();
4192 SetViewToCursor(false);
4198 this.x = line.count;
4201 if(!shift) Deselect();
4204 SetViewToCursor(false);
4207 else if(textPos >= line.count && line.next)
4213 sy = this.y * this.space.h;
4214 sx = 0; //textBlock.startX;
4220 sx = 0; //textBlock.startX;
4228 this.x = Min(this.x, line.count);
4229 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4231 if(!shift) Deselect();
4234 if(this.selX != this.x || this.selY != this.y)
4237 SetViewToCursor(false);
4244 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4245 if(!shift) SelDirty();
4246 this.line = this.line.next;
4250 if(!shift) Deselect();
4253 if(this.selX != this.x || this.selY != this.y)
4261 return style.multiLine ? false : true;
4264 if(style.stuckCaret) break;
4265 if(!(style.freeCaret))
4266 this.selX = Min(this.selX, this.selLine.count);
4268 if(!shift) SelDirty();
4271 this.line = this.lines.first;
4272 if(this.y != 0 || this.x != 0)
4282 EditLine line = this.line;
4284 for(c=0; line.buffer[c]; c++)
4285 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4287 if(c != 0 || this.x)
4297 DirtyLine(this.y);*/
4302 if(!shift) Deselect();
4303 SetViewToCursor(true);
4309 if(style.stuckCaret) break;
4310 if(!(style.freeCaret))
4311 this.selX = Min(this.selX, this.selLine.count);
4313 if(!shift) SelDirty();
4318 else if(this.x != this.line.count)
4320 this.x = this.line.count;
4321 //DirtyLine(this.y);
4324 if(!shift) Deselect();
4325 SetViewToCursor(true);
4330 if(style.tabKey && !key.ctrl)
4332 if(this.selY != this.y && style.tabSel)
4334 EditLine firstLine, lastLine;
4338 // Do multi line selection tabbing here
4339 if(this.selY < this.y)
4341 firstLine = this.selLine;
4342 lastLine = this.line;
4348 // Selecting going up
4349 firstLine = this.line;
4350 lastLine = this.selLine;
4357 for(line = firstLine; line; line = line.next, y++)
4359 if(line != lastLine || x)
4363 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4365 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4367 if(line.buffer[c] == '\t')
4372 else if(line.buffer[c] != ' ')
4377 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4380 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4381 char * string = new char[len];
4382 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4383 GetText(string, line, y, 0, line, y, lastC, false, false);
4386 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4387 if(!line.AdjustBuffer(line.count-lastC))
4390 if(style.autoSize) AutoSize();
4394 if(line == lastLine) break;
4399 for(line = firstLine; line; line = line.next, y++)
4403 if(line != lastLine || x)
4407 if(line.count + 1 <= this.maxLineSize)
4409 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4411 if(!line.AdjustBuffer(line.count+1))
4414 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4415 if(style.autoSize) AutoSize();
4417 AddCharAction action { ch = '\t', x = 0, y = y };
4421 memmove(line.buffer+1,line.buffer,line.size-1);
4423 line.buffer[0] = '\t';
4428 if(line.count + this.tabSize <= this.maxLineSize)
4431 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4432 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4433 if(style.autoSize) AutoSize();
4435 if(!line.AdjustBuffer(line.count+this.tabSize))
4439 char * string = new char[this.tabSize + 1];
4440 memset(string, ' ', this.tabSize);
4441 string[this.tabSize] = '\0';
4442 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4445 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4446 line.count+=this.tabSize;
4447 for(c=0; c<this.tabSize; c++)
4448 line.buffer[c] = ' ';
4454 if(line == lastLine) break;
4468 char * addString = new char[this.tabSize + 1];
4470 if(!(style.freeCaret))
4472 this.x = Min(this.x, this.line.count);
4477 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4479 addString[len++] = ' ';
4487 SetViewToCursor(true);
4494 if(!(style.hScroll) || hasHorzScroll) break;
4495 if(this.viewX < this.maxLength)
4497 //this.viewX+=this.space.w*this.tabSize;
4499 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4506 if(!shift) Deselect();
4515 if(!(style.hScroll) || hasHorzScroll) break;
4518 //this.viewX-=this.space.w*this.tabSize;
4519 //this.viewX = Max(this.viewX,0);
4521 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4522 // SetCursorToView();
4529 if(!shift) Deselect();
4543 if(!(style.readOnly))
4549 this.overwrite ^= 1;
4550 UpdateCaretPosition(true);
4555 NotifyOvrToggle(master, this, this.overwrite);
4566 if(style.noSelect) break;
4569 //Save current view position
4570 int tempX = this.viewX;
4571 int tempY = this.viewY;
4575 this.selLine = this.lines.first;
4576 this.y = this.lineCount-1;
4577 this.line = this.lines.last;
4578 this.x = this.line.count;
4581 SetViewToCursor(true);
4583 //Restore previous view position
4584 SetScrollPosition(tempX, tempY * this.space.h);
4590 // TOCHECK: Was there any good reason why we weren't returning false here?
4591 return false; // break;
4596 if(!(style.readOnly))
4604 if(style.readOnly) break;
4610 if(style.readOnly) break;
4615 if(style.readOnly) break;
4619 if(style.readOnly) break;
4623 if(style.readOnly) break;
4624 if(key.shift && key.code == rightBracket)
4626 //Only indent back if you are exactly at one tab.
4628 bool whitespace = true;
4634 EditLine line = this.line;
4636 //Only remove one tab if there is nothing else on the line.
4637 for(i = 0; i < this.line.count; i++)
4639 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4653 for(pos = line.count - 1; pos >= 0; pos--)
4655 char c = line.buffer[pos];
4659 indentwidth += this.tabSize;
4661 //Counting backwards, so when you find a character, indentation is reset
4671 //Place the } to get an undo:
4674 this.x = this.line.count;
4678 putsize = indentwidth;
4680 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4682 newline = new char[putsize+2];
4683 newline[putsize] = '}';
4684 newline[putsize+1] = '\0';
4688 for(; i < indentwidth / this.tabSize; i++)
4690 for(;i < putsize; i++)
4698 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4710 void OnHScroll(ScrollBarAction action, int position, Key key)
4713 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4715 this.viewX = position;
4716 if(action != setRange)
4718 if(!this.mouseMove && style.cursorFollowsView)
4725 void OnVScroll(ScrollBarAction action, int position, Key key)
4727 int oldViewY = this.viewY;
4730 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4732 position /= this.space.h;
4734 if(position < this.viewY)
4736 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4737 style.recomputeSyntax = true;
4739 else if(position > this.viewY)
4741 EditLine oldViewLine = viewLine;
4742 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4743 FigureStartSyntaxStates(oldViewLine, false);
4746 if(action != setRange)
4748 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4751 if(this.x != this.selX || this.y != this.selY)
4755 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4758 int numLines = clientSize.h / this.space.h;
4760 if(Abs(this.viewY - oldViewY) < numLines)
4763 if(this.viewY > oldViewY)
4765 for(y = oldViewY; y <this.viewY; y++)
4766 DirtyLine(y + numLines);
4770 for(y = this.viewY; y <oldViewY; y++)
4771 DirtyLine(y + numLines);
4776 // Fix dirt of stuff before first line...
4777 if(this.viewY - oldViewY > 0)
4779 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4786 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4791 ReplaceTextAction replaceAction = null;
4792 AddCharAction addCharAction = null;
4793 int addedSpaces = 0, addedTabs = 0;
4795 if(ch == '\r') return true;
4796 if(style.stuckCaret /*|EES_READONLY)*/ )
4799 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4801 if(!undoBuffer.dontRecord)
4803 if(selX != x || selY != y)
4808 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4809 oldString = new char[len];
4810 UTF32toUTF8Len(&ch, 1, buffer, 4);
4811 newString = CopyString(buffer);
4812 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4814 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4815 if(selY < y || (selY == y && selX < x))
4817 replaceAction.x1 = selX;
4818 replaceAction.y1 = selY;
4819 replaceAction.x2 = x;
4820 replaceAction.y2 = y;
4824 replaceAction.x1 = x;
4825 replaceAction.y1 = y;
4826 replaceAction.x2 = selX;
4827 replaceAction.y2 = selY;
4829 Record(replaceAction);
4830 undoBuffer.dontRecord++;
4834 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4835 Record(addCharAction);
4841 DelSel(&addedSpaces);
4842 if(this.lineCount+1 > this.maxLines)
4845 Emptyline(this.lines.first,0);
4849 if(!(style.vScroll))
4851 // Make sure it fits, but we need a default line is this.font is too big for window
4852 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4855 if((this.y >= 0) && this.y < this.viewY)
4860 line = EditLine { };
4863 lines.Insert(this.line, line);
4864 line.editBox = this;
4870 // If we're displacing the lines from a current line ...
4873 if(this.line.buffer)
4876 if(this.line.count < endX) endX = this.line.count;
4877 length = this.line.count - endX;
4880 if(!line.AdjustBuffer(length))
4884 if(this.line.buffer)
4886 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4888 if(endX > 4000 || endX < 0)
4891 this.line.count = endX;
4892 this.line.buffer[this.line.count] = '\0';
4893 this.line.AdjustBuffer(this.line.count);
4894 ComputeLength(this.line);
4898 BufferLocation before = { this.line, this.y, this.x }, after;
4904 ComputeLength(this.line);
4907 if(length > 4000 || length < 0)
4910 line.count = length;
4914 line.buffer[line.count] = '\0';
4917 after.line = this.line, after.y = this.y, after.x = this.x;
4919 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4920 if(style.autoSize) AutoSize();
4926 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4927 DelSel(&addedSpaces);
4928 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4929 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4930 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4934 this.selLine = this.line;
4938 replaceAction.x3 = x;
4939 replaceAction.y3 = y;
4940 replaceAction.addedSpaces = addedSpaces;
4941 replaceAction.addedTabs = addedTabs;
4942 undoBuffer.dontRecord--;
4946 addCharAction.addedSpaces = addedSpaces;
4947 addCharAction.addedTabs = addedTabs;
4954 /****************************************************************************
4956 ****************************************************************************/
4959 bool AddCh(unichar ch)
4961 return _AddCh(ch, null, null);
4966 this.modified = true;
4967 NotifyUpdate(master, this);
4968 modifiedDocument = true;
4971 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4974 DelCh(line1, y1, x1, line2, y2, x2, false);
4975 SetViewToCursor(true);
4983 itemEditUndo.disabled = undoBuffer.curAction == 0;
4984 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4985 if(savedAction == undoBuffer.curAction)
4987 modifiedDocument = false;
4989 NotifyUnsetModified(master, this);
4996 itemEditUndo.disabled = undoBuffer.curAction == 0;
4997 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4998 if(savedAction == undoBuffer.curAction)
5000 modifiedDocument = false;
5002 NotifyUnsetModified(master, this);
5006 void Record(UndoAction action)
5008 if(!undoBuffer.dontRecord)
5010 undoBuffer.Record(action);
5011 itemEditUndo.disabled = undoBuffer.curAction == 0;
5012 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5019 this.lines.first, 0,0,
5020 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
5023 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
5028 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
5029 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
5030 this.y = line2 ? y2 : (this.lineCount-1);
5031 this.line = line2 ? (EditLine)line2 : this.lines.last;
5034 SetViewToCursor(true);
5038 // TODO: Fix this vs modifiedDocument window property
5039 void SetModified(bool flag)
5043 this.modified = false;
5044 if(flag && !NotifyModified(master, this))
5045 this.modified = true;
5050 bool AddS(char * string)
5057 int addedSpaces = 0, addedTabs = 0;
5058 AddTextAction action = null;
5059 ReplaceTextAction replaceAction = null;
5061 this.pasteOperation = true;
5063 if(style.stuckCaret /*|EES_READONLY)*/ )
5066 if(!undoBuffer.dontRecord)
5068 char * placeString = CopyString(string);
5069 if(!style.multiLine)
5073 for(i = 0; (ch = placeString[i]); i++)
5076 placeString[i] = '\0';
5077 placeString = renew placeString byte[i+1];
5082 if(selX != x || selY != y)
5086 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5087 oldString = new char[len];
5088 newString = placeString;
5089 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5091 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5092 if(selY < y || (selY == y && selX < x))
5094 replaceAction.x1 = selX;
5095 replaceAction.y1 = selY;
5096 replaceAction.x2 = x;
5097 replaceAction.y2 = y;
5101 replaceAction.x1 = x;
5102 replaceAction.y1 = y;
5103 replaceAction.x2 = selX;
5104 replaceAction.y2 = selY;
5106 Record(replaceAction);
5110 if(string[0] == '\n')
5111 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5113 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5121 undoBuffer.dontRecord++;
5122 DelSel(&addedSpaces);
5126 for(c = 0; string[c]; c++)
5128 if(string[c] == '\n' || string[c] == '\r')
5130 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5135 if(string[c] == '\n')
5144 // Reset for next line
5148 if(string[c] == '\r' && *line == '\n')
5160 // Why was this here?
5163 // Add the line here
5165 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5171 undoBuffer.dontRecord--;
5176 action.addedSpaces = addedSpaces;
5177 action.addedTabs = addedTabs;
5179 else if(replaceAction)
5181 replaceAction.y3 = y;
5182 replaceAction.x3 = x;
5183 replaceAction.addedSpaces = addedSpaces;
5184 replaceAction.addedTabs = addedTabs;
5187 UpdateCaretPosition(true);
5189 this.pasteOperation = false;
5202 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5203 SetViewToCursor(true);
5209 void PutCh(unichar ch)
5213 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5214 //if((ch >= 32) || ch == '\n')
5216 int addedSpaces = 0, addedTabs = 0;
5217 ReplaceTextAction replaceAction = null;
5218 AddCharAction addCharAction = null;
5221 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5223 if(this.x < this.line.count && this.overwrite)
5228 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5229 oldString = new char[len];
5230 UTF32toUTF8Len(&ch, 1, buffer, 4);
5231 newString = CopyString(buffer);
5232 GetText(oldString, line, y, x, line, y, x+1, false, false);
5233 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5234 replaceAction.x1 = x;
5235 replaceAction.y1 = y;
5236 replaceAction.x2 = x+1;
5237 replaceAction.y2 = y;
5238 Record(replaceAction);
5240 undoBuffer.dontRecord++;
5241 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5242 undoBuffer.dontRecord--;
5244 else if(!undoBuffer.dontRecord)
5246 if(selX != x || selY != y)
5251 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5252 oldString = new char[len];
5253 UTF32toUTF8Len(&ch, 1, buffer, 4);
5254 newString = CopyString(buffer);
5255 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5256 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5257 if(selY < y || (selY == y && selX < x))
5259 replaceAction.x1 = selX;
5260 replaceAction.y1 = selY;
5261 replaceAction.x2 = x;
5262 replaceAction.y2 = y;
5266 replaceAction.x1 = x;
5267 replaceAction.y1 = y;
5268 replaceAction.x2 = selX;
5269 replaceAction.y2 = selY;
5271 Record(replaceAction);
5275 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5276 Record(addCharAction);
5279 undoBuffer.dontRecord++;
5280 result = _AddCh(ch, &addedSpaces, &addedTabs);
5283 replaceAction.x3 = x;
5284 replaceAction.y3 = y;
5285 replaceAction.addedSpaces = addedSpaces;
5286 replaceAction.addedTabs = addedTabs;
5290 addCharAction.addedSpaces = addedSpaces;
5291 addCharAction.addedTabs = addedTabs;
5293 undoBuffer.dontRecord--;
5297 if(result) SetViewToCursor(true);
5301 void PutS(char * string)
5306 SetViewToCursor(true);
5311 void Printf(char * format, ...)
5315 char temp[MAX_F_STRING];
5317 va_start(args, format);
5318 vsprintf(temp, format, args);
5324 void SetContents(char * format, ...)
5329 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5332 char temp[MAX_F_STRING];
5334 va_start(args, format);
5335 vsprintf(temp, format, args);
5351 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5354 else if(this.line.prev)
5356 EditLine line = this.line.prev;
5360 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5368 this.selLine = this.line;
5373 SetViewToCursor(true);
5378 Emptyline(this.line,this.y);
5379 this.selX = this.x = 0;
5382 this.selLine = this.line;
5384 SetViewToCursor(true);
5394 SetViewToCursor(true);
5402 SetViewToCursor(true);
5406 bool GoToLineNum(int lineNum)
5411 EditLine line = this.lines.first;
5412 for(c = 0; c < lineNum && line; c++, line = line.next);
5422 SetViewToCursor(true);
5429 bool GoToPosition(EditLine line, int y, int x)
5441 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5455 SetViewToCursor(true);
5462 void SetViewToCursor(bool setCaret)
5472 bool dontScroll = false;
5478 selected = selX != this.x || selY != y;
5485 checkLine = dropLine;
5491 checkLine = this.line;
5496 numLines = clientSize.h / space.h;
5498 // This is broken. The EditBox now doesn't do anything different when adding to it,
5499 // regardless of the previous scrolling position. It should be read and then set again
5500 // if one wishes to preserve it.
5501 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5502 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5506 // Horizontal Adjustment
5507 if(!dontScroll && checkLine)
5511 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5514 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5520 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5521 viewX = x - clientSize.w+space.w;
5522 if(x < this.viewX + clientSize.w/2 - space.w)
5523 viewX = Max(0, x - clientSize.w/2 + space.w);
5531 // Vertical Adjustment
5532 if(viewY > checkY) viewY = checkY;
5533 if(viewY + numLines <= checkY)
5535 if(clientSize.h >= space.h)
5537 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5547 for(;dropLine && dropLine.prev && dropY >= numLines;)
5549 dropLine = dropLine.prev;
5553 for(;this.line && this.line.prev && this.y >= numLines;)
5555 this.line = this.line.prev;
5560 SetScrollPosition(viewX, viewY * this.space.h);
5562 UpdateCaretPosition(setCaret);
5568 selLine = this.line;
5578 void CenterOnCursor()
5580 int numLines = clientSize.h / this.space.h;
5581 int y = this.y - numLines / 2;
5583 bool figureSyntax = false;
5584 EditLine oldViewLine = viewLine;
5585 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5590 for(;y < this.viewY; y++)
5592 this.viewLine = this.viewLine.prev;
5593 style.recomputeSyntax = true;
5595 for(;y > this.viewY; y--)
5597 this.viewLine = this.viewLine.next;
5598 figureSyntax = true;
5601 FigureStartSyntaxStates(oldViewLine, false);
5605 SetScrollPosition(this.viewX, viewY * this.space.h);
5606 UpdateCaretPosition(true);
5610 void SetCursorToView()
5621 numLines = clientSize.h / this.space.h;
5625 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5626 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5630 EditLine oldLine = this.line;
5631 bool lastOne = false;
5632 EditLine oldViewLine = this.viewLine;
5633 bool figureSyntax = false;
5635 if(this.y >= this.lineCount-1) return;
5637 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5639 if(this.viewY + numLines < this.lines.count)
5641 this.viewLine = this.viewLine.next;
5643 figureSyntax = true;
5645 else if(c && !lastOne)
5654 FigureStartSyntaxStates(oldViewLine, false);
5655 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5658 SetViewToCursor(false);
5667 if(this.y == 0) return;
5669 numLines = clientSize.h / this.space.h;
5673 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5674 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5678 EditLine oldLine = this.line;
5680 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5685 if(this.viewLine.prev)
5687 this.viewLine = this.viewLine.prev;
5689 style.recomputeSyntax = true;
5693 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5696 SetViewToCursor(false);
5703 if(this.viewLine.prev)
5706 // this.viewLine = this.viewLine.prev;
5709 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5716 if(this.viewLine.next)
5719 // this.viewLine = this.viewLine.next;
5722 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5729 EditLine l1, l2, line;
5730 int x1, x2, nx1, nx2;
5735 if(!this.selLine) return 0;
5736 if(this.selLine == this.line && this.selX == this.x) return 0;
5737 if(this.selY < this.y)
5744 else if(this.selY > this.y)
5751 else if(this.selX < this.x)
5753 l1 = l2 = this.line;
5759 l1 = l2 = this.line;
5763 nx1 = Min(x1,l1.count);
5764 nx2 = Min(x2,l2.count);
5766 // Find Number of Bytes Needed
5768 for(line = l1; line; line = line.next)
5770 if(line == l1) start = nx1; else start = 0;
5771 if(line == l2) end = nx2; else end = line.count;
5773 if(style.freeCaret && line == l2)
5776 count = Max(x2-Max(x1,l1.count),0);
5778 count = Max(x2-l2.count,0);
5782 if(line == l2) break;
5783 // Add Carriage Return / line Feed
5790 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5794 if(!reorder || this.selY < this.y)
5796 if(l1) *l1 = this.selLine;
5797 if(y1) *y1 = this.selY;
5798 if(x1) *x1 = this.selX;
5799 if(l2) *l2 = this.line;
5800 if(y2) *y2 = this.y;
5801 if(x2) *x2 = this.x;
5803 else if(this.selY > this.y)
5805 if(l1) *l1 = this.line;
5806 if(y1) *y1 = this.y;
5807 if(x1) *x1 = this.x;
5808 if(l2) *l2 = this.selLine;
5809 if(y2) *y2 = this.selY;
5810 if(x2) *x2 = this.selX;
5812 else if(this.selX < this.x)
5814 if(l1) *l1 = this.line;
5815 if(y1) *y1 = this.selY;
5816 if(x1) *x1 = this.selX;
5817 if(l2) *l2 = this.line;
5818 if(y2) *y2 = this.y;
5819 if(x2) *x2 = this.x;
5823 if(l1) *l1 = this.line;
5824 if(y1) *y1 = this.y;
5825 if(x1) *x1 = this.x;
5826 if(l2) *l2 = this.line;
5827 if(y2) *y2 = this.selY;
5828 if(x2) *x2 = this.selX;
5833 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5835 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5837 this.selLine = (EditLine)l1;
5840 this.line = (EditLine)l2;
5844 SetViewToCursor(true);
5848 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5850 EditLine l1, l2, line;
5851 int x1, x2, nx1, nx2;
5882 nx1 = Min(x1,l1.count);
5883 nx2 = Min(x2,l2.count);
5886 for(line = l1; line; line = line.next)
5888 if(line == l1) start = nx1; else start = 0;
5889 if(line == l2) end = nx2; else end = line.count;
5892 CopyBytes(text, line.buffer + start, end - start);
5895 numChars += end-start;
5897 if(style.freeCaret && line == l2 && addSpaces)
5900 count = Max(x2-Max(x1,l1.count),0);
5902 count = Max(x2-l2.count,0);
5905 FillBytes(text,' ',count);
5911 if(line == l2) break;
5923 // '\0' terminate Terminate
5930 void GetSel(char * text, bool addCr)
5932 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5938 this.selLine = this.line;
5948 int size = SelSize();
5951 // Try to allocate memory
5952 ClipBoard clipBoard { };
5953 if(clipBoard.Allocate(size+1))
5955 GetSel(clipBoard.memory, true);
5968 ClipBoard clipBoard { };
5969 if(clipBoard.Load())
5970 PutS(clipBoard.memory);
5981 SetViewToCursor(true);
5986 void DeleteSelection()
5991 SetViewToCursor(true);
5997 void Save(File f, bool cr)
6000 savedAction = undoBuffer.curAction;
6002 for(line = this.lines.first; line; line = line.next)
6004 f.Write(line.buffer, line.count,1);
6007 if(cr) f.Putc('\r');
6013 #define BUFFER_SIZE 16384
6016 undoBuffer.dontRecord++;
6019 char buffer[BUFFER_SIZE];
6023 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
6024 buffer[count] = '\0';
6030 undoBuffer.dontRecord--;
6031 undoBuffer.count = 0;
6032 undoBuffer.curAction = 0;
6033 itemEditUndo.disabled = undoBuffer.curAction == 0;
6034 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
6037 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6041 bool firstPass = true;
6042 EditBoxFindResult result = found;
6044 if(!this.line) return notFound;
6047 for(line = this.line;;)
6055 line = this.lines.first;
6061 line = this.lines.last;
6062 num = this.lineCount - 1;
6068 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6070 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6074 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6077 if(line == this.line && !firstPass) break;
6095 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6099 int searchLen = strlen(text);
6100 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6102 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6103 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6105 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6112 bool OnKeyDown(Key key, unichar ch)
6115 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6117 if(!NotifyKeyDown(master, this, key, ch))
6127 this.mouseMove = false;
6128 OnLeftButtonUp(0,0,0);
6129 SetViewToCursor(true);
6139 public class EditBoxStream : File
6142 BufferLocation start, sel;
6149 EditBox editBox = this.editBox;
6151 editBox.x = start.x;
6152 editBox.y = start.y;
6153 editBox.line = start.line;
6155 editBox.selX = sel.x;
6156 editBox.selY = sel.y;
6157 editBox.selLine = sel.line;
6159 editBox.SetViewToCursor(true);
6160 //editBox.ComputeColumn();
6164 property EditBox editBox
6172 start.line = value.line;
6176 sel.line = value.selLine;
6179 value.GoToHome(true);
6182 get { return editBox; }
6185 uint Read(byte * buffer, uint size, uint count)
6188 EditBox editBox = this.editBox;
6189 EditLine line = editBox.line;
6195 for(;read < count && line; line = (*&line.next))
6197 int numBytes = Min(count - read, (*&line.count) - x);
6200 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6205 for(;read < count && x < (*&line.count);)
6207 buffer[read++] = (*&line.buffer)[x++];
6214 buffer[read++] = '\n';
6219 if(x == (*&line.count) && (*&line.next))
6228 editBox.line = editBox.selLine = line;
6229 editBox.x = editBox.selX = x;
6230 editBox.y = editBox.selY = y;
6235 bool Seek(int pos, FileSeekMode mode)
6238 EditBox editBox = this.editBox;
6239 EditLine line = editBox.line;
6241 if(mode == FileSeekMode::start)
6243 pos = pos - this.pos;
6253 for(;read < pos && line; line = (*&line.next))
6255 int numBytes = Min(pos - read, (*&line.count) - x);
6258 read += numBytes; x += numBytes;
6261 /*for(;read < pos && x < (*&line.count);)
6289 for(;read < pos && line; line = (*&line.prev))
6291 int numBytes = Min(pos - read, x);
6297 /*for(;read < pos && x > 0;)
6307 x = (*&(*&line.prev).count);
6323 editBox.line = editBox.selLine = line;
6324 editBox.x = editBox.selX = x;
6325 editBox.y = editBox.selY = y;
6330 bool Puts(char * string)
6332 EditBox editBox = this.editBox;
6333 BufferLocation start { editBox.line, editBox.y, editBox.x };
6337 editBox.AddS(string);
6339 pos.line = editBox.line;
6343 this.start.AdjustAdd(start, pos);
6344 sel.AdjustAdd(start, pos);
6349 // NOTE: BYTE, NOT UNICODE CHARACTER!
6352 EditBox editBox = this.editBox;
6353 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6358 utf8Bytes[numBytes++] = ch;
6359 utf8Bytes[numBytes] = 0;
6360 if(UTF8Validate(utf8Bytes))
6362 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6364 pos.line = editBox.line;
6367 this.start.AdjustAdd(start, pos);
6368 sel.AdjustAdd(start, pos);
6375 bool Getc(char * ch)
6377 return Read(ch, 1, 1) ? true : false;
6380 void DeleteBytes(uint count)
6382 EditBox editBox = this.editBox;
6385 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6386 BufferLocation end = pos;
6391 for(;c < count && end.line && end.x < end.line.count;)
6396 if(c < count && end.line && end.line.next)
6398 end.line = end.line.next;
6407 start.AdjustDelete(pos, end);
6408 sel.AdjustDelete(pos, end);
6410 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);