1 namespace gui::controls;
4 selectionForeground = white;
5 disabled: defaultTextColor = Color { 85, 85, 85 };
14 public class SyntaxColorScheme
18 Color charLiteralColor;
19 Color stringLiteralColor;
20 Color preprocessorColor;
22 private Array<Color> keywordColors { };
24 public property Container<Color> keywordColors
26 set { keywordColors.Copy((void *)value); }
27 get { return keywordColors; }
35 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
37 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
39 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
41 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
42 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
43 #define UTF8_GET_CHAR(string, numBytes) \
47 byte b = (string)[0]; \
52 if(b & 0x80 && b & 0x40) \
67 for(i = 0; i<numBytes; i++) \
70 ch |= (string)[i] & mask; \
78 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
79 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
82 bool inMultiLineComment:1, inPrep:1, escaped:1;
84 bool recomputeSyntax:1;
85 bool cursorFollowsView:1;
87 // bool lineNumbers:1;
91 void UnregisterClass_EditBox()
94 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
103 extern int __ecereVMethodID_class_OnFree;
106 static class ArrayImpl
113 public class OldArray
120 void ** array = (void **)((ArrayImpl)this).array;
121 if(type.type == normalClass || type.type == noHeadClass)
123 for(c = 0; c<size; c++)
124 type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
126 // TODO: Call OnFree for structClass
127 delete ((ArrayImpl)this).array;
136 if(((ArrayImpl)this).array)
140 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
143 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
152 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
157 public class UndoAction : struct
160 subclass(UndoAction) type;
161 virtual void Undo(void * data) { type.Undo(this, data); }
162 virtual void Redo(void * data) { type.Redo(this, data); }
164 virtual void Print(void * data) { type.Print(this, data); }
168 if(((Class)type).Destructor)
169 ((void (*)(void *))((Class)type).Destructor)(this);
174 class ArrayUndoActions : OldArray
176 type = class(UndoAction);
180 public class UndoBuffer
182 ArrayUndoActions actions { size = 8 };
197 UndoAction action = actions._[--curAction];
199 /*Print("Undoing: ");
200 action.Print(data);*/
211 if(curAction < count)
213 UndoAction action = actions._[curAction];
216 /*Print("Redoing: ");
217 action.Print(data);*/
225 void Record(UndoAction action)
227 if(!dontRecord && !insideRedo)
229 if(curAction < count)
232 for(c = curAction; c < count; c++)
238 if(count >= actions.size)
239 actions.size += actions.size / 2;
242 /*Print("Recording: ");
243 action.Print(data);*/
245 actions._[count++] = action;
248 if(actions.size > count + count / 2 && count + count / 2 >= 8)
249 actions.size = count + count / 2;
256 static class AddCharAction : UndoAction
260 int addedSpaces, addedTabs;
261 type = class(AddCharAction);
263 void Undo(EditBox editBox)
265 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
267 if(addedTabs || addedSpaces)
268 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
269 editBox.UpdateDirty();
272 void Redo(EditBox editBox)
274 editBox.GoToPosition(null, y, x);
276 editBox.UpdateDirty();
279 void Print(EditBox editBox)
281 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
286 static class AddTextAction : UndoAction
290 int addedSpaces, addedTabs;
291 type = class(AddTextAction);
294 void Print(EditBox editBox)
296 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
304 void Undo(EditBox editBox)
309 editBox.GoToPosition(null, y1, x1);
311 l2 = editBox.lines.first;
312 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
314 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
315 if(addedTabs || addedSpaces)
316 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
318 editBox.SetViewToCursor(true);
322 void Redo(EditBox editBox)
324 editBox.GoToPosition(null, y1, x1);
325 editBox.PutS(string);
329 static class DelTextAction : UndoAction
335 type = class(DelTextAction);
338 void Print(EditBox editBox)
340 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
343 void Undo(EditBox editBox)
345 editBox.GoToPosition(null, y1, x1);
346 editBox.PutS(string);
350 editBox.GoToPosition(null, y1, x1);
353 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
354 //editBox.SetViewToCursor(true);
357 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
363 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
364 //editBox.SetViewToCursor(true);
367 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
371 void Redo(EditBox editBox)
376 editBox.GoToPosition(null, y1, x1);
379 l2 = editBox.lines.first;
380 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
382 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
383 editBox.SetViewToCursor(true);
393 static class ReplaceTextAction : UndoAction
395 int y1, x1, y2, x2, y3, x3;
399 int addedSpaces, addedTabs;
401 type = class(ReplaceTextAction);
404 void Print(EditBox editBox)
406 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
409 void Undo(EditBox editBox)
414 editBox.GoToPosition(null, y1, x1);
416 l3 = editBox.lines.first;
417 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
419 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
421 editBox.PutS(oldString);
422 if(addedSpaces || addedTabs)
423 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
425 //editBox.PutS(oldString);
428 editBox.GoToPosition(null, y1, x1);
431 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
437 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
441 void Redo(EditBox editBox)
446 editBox.GoToPosition(null, y1, x1);
448 l2 = editBox.lines.first;
449 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
451 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
453 editBox.PutS(newString);
463 static class MoveTextAction : UndoAction
465 int fy1, fx1, fy2, fx2;
467 type = class(MoveTextAction);
469 void Undo(EditBox editBox)
474 void Redo(EditBox editBox)
480 public class EditLine : struct
492 // Only works for single line edit for now...
493 EditBox editBox = this.editBox;
497 get { return this ? buffer : null; }
499 property EditLine prev { get { return this ? prev : null; } };
500 property EditLine next { get { return this ? next : null; } };
501 property int count { get { return this ? count : 0; } };
508 // This makes sure the buffer always contains at least count characters
509 // Keeps a count/2 pad for avoiding always reallocating memory.
510 bool AdjustBuffer(int count)
518 newSize = (count + (count >> 1));
520 // Shrink down the buffer
523 buffer = new char[newSize];
524 if(!buffer) return false;
528 CopyBytes(buffer, this.buffer, count);
531 this.buffer = buffer;
535 // Increase the buffer
536 else if(size < count)
538 buffer = new char[newSize];
539 if(!buffer) return false;
543 CopyBytes(buffer, this.buffer, this.count + 1); // size);
546 this.buffer = buffer;
555 public struct BufferLocation
560 void AdjustDelete(BufferLocation start, BufferLocation end)
562 // Location is before, nothing to do
563 if(y < start.y || (y == start.y && x < start.x))
565 // Location is inside deleted bytes, point to the start
566 if((y >= start.y && (y > start.y || x >= start.x)) &&
567 (y >= end.y && (y > end.y || x >= end.x)))
572 // Location is on another line
574 y -= end.y - start.y;
575 // Location is the last touched line
582 //if(start.line == end.line)
583 x -= end.x - start.x;
592 // Assuming no carriage return before first character ???? fixed?
593 void AdjustAdd(BufferLocation start, BufferLocation end)
595 int numLines = end.y - start.y;
602 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
605 for(c = 0, line = start.line; c<numLines; c++)
608 //x += numLines ? end.x : (end.x - start.x);
609 x += end.x - start.x;
616 public enum EditBoxFindResult { notFound, found, wrapped };
618 static char * keyWords1[] =
621 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
622 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
624 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
625 "__attribute__", "__stdcall", "_stdcall",
626 "__declspec", "goto",
627 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
628 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
631 "class", "private", "public",
633 "delete", "new", "new0", "renew", "renew0", "define",
636 "dllexport", "dllimport", "stdcall",
637 "subclass", "__on_register_module", "namespace", "using",
638 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
639 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
640 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
643 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
646 "this", "true", "false", "null", "value",
655 static char * keyWords2[] =
657 "defined", "warning", null
660 static char ** keyWords[] = { keyWords1, keyWords2 };
661 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
662 //static int * keyLen[NUM_KEYWORD_GROUPS];
663 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
666 static FileFilter filters[] =
668 { "All files", null },
670 "Text Files (*.txt)",
674 static FileListConfig fileListConfig = { "", "", filters, sizeof(filters), null, 0 };
676 static char searchString[1025], replaceString[1025];
677 static bool matchCase = false, wholeWord = false, searchUp = false;
679 static GoToDialog goToDialog
681 autoCreate = false, isModal = true, text = "Go To"
684 public class EditBox : CommonControl
686 class_property(icon) = "<:ecere>controls/editBox.png";
690 virtual bool Window::NotifyModified(EditBox editBox);
691 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
692 virtual void Window::NotifyUpdate(EditBox editBox);
693 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
694 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
695 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
697 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
698 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
699 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
701 virtual bool Window::NotifyUnsetModified(EditBox editBox);
703 // Why was this commented out?
704 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
705 // and the scrollArea property isn't compared properly either.
709 if(font) ComputeFont();
710 SetInitSize(initSize);
716 style.vScroll = true;
722 style.hScroll = true;
726 property bool textHorzScroll { property_category "Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
727 property bool textVertScroll { property_category "Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
728 property bool readOnly
730 property_category "Behavior"
733 style.readOnly = value;
734 itemEditCut.disabled = value || !selection;
735 itemEditDelete.disabled = value || !selection;
736 itemEditPaste.disabled = value;
738 get { return style.readOnly; }
740 property bool multiLine { property_category "Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
741 property bool freeCaret { property_category "Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
742 property bool tabKey { property_category "Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
743 property int tabSize { property_category "Behavior" set { tabSize = value; } get { return tabSize; } };
744 property bool tabSelection { property_category "Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
745 property bool smartHome { property_category "Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
746 property bool autoEmpty { property_category "Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
747 property bool noCaret { property_category "Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
748 property int maxLineSize { property_category "Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
749 property int maxNumLines { property_category "Behavior" set { maxLines = value; } get { return maxLines; } };
750 property bool useTab { property_category "Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
751 property bool syntaxHighlighting { property_category "Appearance" set { style.syntax = value; } get { return style.syntax; } };
752 property bool noSelect { property_category "Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
753 property bool allCaps { property_category "Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
754 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
755 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
756 property int numLines { get { return this ? lineCount : 0; } };
757 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
758 property int column { get { return col; } }; // TODO: Add Set
759 property int charPos { get { return x; } }; // TODO: Add Set
760 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
761 property EditLine lastLine { get { return lines.last; } };
762 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
763 property char * contents
765 property_category "Data"
771 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
774 //SetViewToCursor(true);
782 char * buffer = null;
785 /* Can't implement this right now because of memory leak... Need string reference counting...
792 for(line = lines.first; line; line = line.next)
793 len += strlen(line.buffer);
795 buffer = new char[len+1];
797 for(line = lines.first; line; line = line.next)
799 int lineLen = strlen(line.buffer);
800 memcpy(buffer + len, line.buffer, lineLen);
806 buffer = this.line ? this.line.buffer : null;
811 property bool overwrite { get { return overwrite; } };
812 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
814 property char * multiLineContents
818 char * buffer = null;
825 for(line = lines.first; line; line = line.next)
826 len += strlen(line.buffer)+1;
828 buffer = new char[len+1];
830 for(line = lines.first; line; line = line.next)
832 int lineLen = strlen(line.buffer);
833 memcpy(buffer + len, line.buffer, lineLen);
835 if(line.next) buffer[len++] = '\n';
848 return this.line.buffer;
853 void SetLineText(char * text)
857 EditLine_SetText(this.line, text);
861 property Color selectionColor { set { selectionColor = value; } get { return selectionColor; } isset { return selectionColor ? true : false; } };
862 property Color selectionText { set { selectionText = value; } get { return selectionText; } isset { return selectionText ? true : false; } };
863 property SyntaxColorScheme syntaxColorScheme { set { delete colorScheme; colorScheme = value; incref colorScheme; } }
865 // selectionStart.line, selectionStart.column (With Set)
866 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
880 // Position of Caret (Not necessarily displayed position)
883 // Position of beginning of block (Block ends at (x,y))
885 // line is line at carret, selLine is line at beginning of block
886 EditLine line, selLine, dropLine;
887 // Mouse selection Moving virtual caret
892 // ViewX is x offset in pixels, ViewY is y offset in lines
894 // viewLine is first displayed line
897 // start and end of area to redraw
900 // MaxLine is the longest line
902 // MaxLength is the longest line's length
905 // MouseSelect is true once button is pressed, overwrite is true if mode is on
906 bool mouseSelect, mouseMove, overwrite, wordSelect;
907 // Timer is used for mouse selection scrolling
910 window = this, delay = 0.1;
919 OnMouseMove(mouseX, mouseY, -1);
924 // (mouseX,mouseY) is the position of the mouse in the edit box
929 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
932 bool rightButtonDown;
935 UndoBuffer undoBuffer { data = this };
937 Color selectionColor, selectionText;
938 SyntaxColorScheme colorScheme { };
943 Menu editMenu { menu, "Edit", e };
946 editMenu, "Cut\tCtrl+X", t, disabled = true;
948 bool NotifySelect(MenuItem item, Modifiers mods)
950 if(!(style.readOnly))
955 MenuItem itemEditCopy
957 editMenu, "Copy\tCtrl+C", c, disabled = true;
959 bool NotifySelect(MenuItem item, Modifiers mods)
965 MenuItem itemEditPaste
967 editMenu, "Paste\tCtrl+V", p;
969 bool NotifySelect(MenuItem item, Modifiers mods)
971 if(!(style.readOnly))
976 MenuItem itemEditDelete
978 editMenu, "Delete\tDel", d, disabled = true;
980 bool NotifySelect(MenuItem item, Modifiers mods)
982 if(!(style.readOnly))
987 MenuDivider { editMenu };
988 MenuItem itemEditSelectAll
990 editMenu, "Select All\tCtrl+A", a;
992 bool NotifySelect(MenuItem item, Modifiers mods)
998 MenuDivider { editMenu };
999 MenuItem itemEditUndo
1001 editMenu, "Undo\tCtrl+Z", u;
1004 bool NotifySelect(MenuItem item, Modifiers mods)
1010 MenuItem itemEditRedo
1012 editMenu, "Redo\tCtrl+Y", o;
1015 bool NotifySelect(MenuItem item, Modifiers mods)
1021 MenuDivider { editMenu };
1024 editMenu, "Find Previous\tShift-F3", e, shiftF3;
1026 bool NotifySelect(MenuItem item, Modifiers mods)
1029 Find(searchString, wholeWord, matchCase, false);
1031 itemEditFind.NotifySelect(this, item, mods);
1037 editMenu, "Find Next\tF3", n, f3;
1039 bool NotifySelect(MenuItem item, Modifiers mods)
1042 Find(searchString, wholeWord, matchCase, true);
1044 itemEditFind.NotifySelect(this, item, mods);
1048 MenuItem itemEditFind
1050 editMenu, "Find...\tCtrl+F", f, ctrlF;
1052 bool NotifySelect(MenuItem item, Modifiers mods)
1056 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1057 searchUp = searchUp;
1060 // Fix dialog from above shouldn't be visible
1061 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1062 void NotifyDestroyed(Window window, DialogResult result)
1064 FindDialog dialog = (FindDialog) window;
1065 searchUp = dialog.searchUp;
1066 strcpy(searchString, dialog.searchString);
1067 matchCase = dialog.matchCase;
1068 wholeWord = dialog.wholeWord;
1077 editMenu, "Replace...\tCtrl+R", r, ctrlR;
1079 bool NotifySelect(MenuItem item, Modifiers mods)
1081 ReplaceDialog dialog
1085 searchString = searchString,
1086 replaceString = replaceString,
1087 matchCase = matchCase,
1088 wholeWord = wholeWord,
1092 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1093 void NotifyDestroyed(Window window, DialogResult result)
1095 ReplaceDialog dialog = (ReplaceDialog)window;
1096 char * replace = dialog.replaceString;
1098 strcpy(replaceString, replace);
1099 strcpy(searchString, dialog.searchString);
1100 matchCase = dialog.matchCase;
1101 wholeWord = dialog.wholeWord;
1108 MenuDivider { editMenu };
1111 editMenu, "Go To...\tCtrl+G", g, ctrlG;
1113 bool NotifySelect(MenuItem item, Modifiers mods)
1115 goToDialog.editBox = this;
1116 goToDialog.master = master;
1117 goToDialog.Create();
1121 MenuDivider { editMenu };
1122 MenuItem itemEditInsertTab
1124 editMenu, "Insert Tabs", i, checkable = true;
1126 bool NotifySelect(MenuItem item, Modifiers mods)
1128 style.useTab = item.checked;
1134 snapVertScroll = true;
1135 snapHorzScroll = true;
1139 static bool syntaxInit = false;
1144 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1146 for(c = 0; keyWords[g][c]; c++);
1147 //keyLen[g] = new int[c];
1148 for(c = 0; keyWords[g][c]; c++)
1150 keyLen[g][c] = strlen(keyWords[g][c]);
1154 colorScheme.commentColor = dimGray;
1155 colorScheme.charLiteralColor = crimson;
1156 colorScheme.stringLiteralColor = crimson;
1157 colorScheme.preprocessorColor = green;
1158 colorScheme.numberColor = teal;
1159 colorScheme.keywordColors = [ blue, blue ];
1162 FontExtent = Display::FontExtent;
1164 lines.offset = (uint)&((EditLine)0).prev;
1166 style = EditBoxBits { hScroll = true };
1168 // cursor = guiApp.GetCursor(IBeam);
1170 // Default Properties
1172 maxLineSize = 1024;*/
1175 maxLineSize = MAXINT;
1180 mouseSelect = this.mouseMove = false;
1183 x = selX = selY = 0;
1186 line = selLine = null;
1192 endY = clientSize.h;
1195 // Default space to 8 in case window is not constructed
1198 undoBuffer.dontRecord++;
1200 undoBuffer.dontRecord--;
1202 viewLine = lines.first;
1203 style.recomputeSyntax = true;
1209 UpdateCaretPosition(true);
1215 lines.Free(EditLine::Free);
1218 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1220 int count = wc - *renderStart;
1223 if(y + space.h >= box.top && y <= box.bottom)
1229 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1230 surface.TextFont(font);
1231 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1232 if(*x + w + XOFFSET > 0)
1233 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1238 w = numSpaces; // * space.w;
1239 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1240 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1241 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1249 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1250 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1256 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1260 *foreground = (*selected) ? selectionForeground : textColor;
1261 *background = selectionBackground;
1262 *opacity = *selected;
1269 if(this.overwrite && active)
1271 if((style.stuckCaret && wc == line.count && !line.next) ||
1272 (!mouseMove && line == this.line && wc == editX))
1281 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1285 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1286 bool inString = false;
1287 bool inQuotes = false;
1288 bool inPrep = reset ? false : style.inPrep;
1289 bool inSingleLineComment = false;
1290 bool escaped = reset ? false : style.escaped;
1291 bool continuedSingleLineComment = false;
1293 EditLine line = reset ? lines.first : firstLine;
1294 // int maxBackUp = 1000, c;
1295 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1296 for(; line != viewLine; line = line.next)
1298 char * text = line.buffer;
1301 bool lastWasStar = false;
1302 bool firstWord = true;
1303 if(!escaped) inPrep = false;
1304 inSingleLineComment = continuedSingleLineComment;
1310 for(c = 0; (ch = text[c]); c++)
1312 bool wasEscaped = escaped;
1313 bool backLastWasStar = lastWasStar;
1315 lastWasStar = false;
1318 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1320 if(text[c+1] == '/')
1322 inSingleLineComment = true;
1324 else if(text[c+1] == '*')
1326 inMultiLineComment = true;
1329 else if(backLastWasStar)
1330 inMultiLineComment = false;
1334 if(!c || text[c-1] != '/') lastWasStar = true;
1336 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && ch == '\"')
1338 if(inString && !wasEscaped)
1347 else if(!inSingleLineComment && !inMultiLineComment && !inString && ch == '\'')
1349 if(inQuotes && !wasEscaped)
1363 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && ch == '#')
1375 continuedSingleLineComment = false;
1376 if(line.count && line.text[line.count - 1] == '\\')
1377 continuedSingleLineComment = true;
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 = false;
1887 if(line.count && line.text[line.count - 1] == '\\')
1888 continuedSingleLineComment = true;
1891 if(y > box.bottom) // >=clientSize.h)
1897 surface.TextOpacity(true);
1898 surface.SetForeground(black);
1899 surface.SetBackground(Color {255,255,85});
1900 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1904 void FixScrollArea()
1906 if(style.hScroll || style.vScroll)
1908 int width = maxLength + XOFFSET;
1909 int height = lineCount * space.h;
1910 if(style.freeCaret && line)
1915 width = Max(line.length + (x - line.count) * space.w, maxLength);
1919 if(selX > selLine.count)
1920 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1925 SetScrollLineStep(8, space.h);
1926 SetScrollArea(width, height, true);
1930 void ComputeLength(EditLine line)
1937 for(c = 0; c < line.count; )
1946 for(len = 0; c < line.count; c += numBytes)
1948 ch = line.buffer[c];
1949 numBytes = UTF8_NUM_BYTES(ch);
1951 if(ch == ' ' || ch == '\t')
1958 if(!len && ch == ' ')
1963 else if(!len && ch == '\t')
1965 w = (tabSize * space.w) - (x % (tabSize * space.w));
1969 FontExtent(display, font, line.buffer + start, len, &w, null);
1980 if(line.length > this.maxLength)
1982 this.maxLine = line;
1983 this.maxLength = line.length;
1992 this.maxLine = null;
1994 for(line = lines.first; line; line = line.next)
1996 if(line.length > this.maxLength)
1998 this.maxLength = line.length;
1999 this.maxLine = line;
2006 if(this.selY != this.y)
2008 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
2012 void ComputeColumn()
2014 // Adjust new edit X position according to tabs
2015 int c, position = 0;
2018 for(c = 0; c<this.line.count && c<this.x; c+= nb)
2020 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
2021 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
2023 position += this.tabSize - (position % this.tabSize);
2027 position += this.x - c;
2028 this.col = position;
2031 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2033 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2036 bool HasCommentOrEscape(EditLine line)
2038 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2043 for(c = line.count-1; c >= 0; c--)
2045 char ch = line.buffer[c];
2051 else //if(ch != ' ' && ch != '\t')
2058 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2060 EditLine line = l1, next;
2062 int oldCount1, oldCount2;
2063 int y, firstViewY, firstY, firstDropY, firstSelY;
2066 DelTextAction action = null;
2067 bool hadComment = false;
2071 hadComment = HasCommentOrEscape(line);
2073 if(y2 > y1 || c2 > c1)
2075 if(start < l1.count)
2077 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2081 oldCount1 = l1.count;
2083 while(c1 < oldCount1)
2085 byte ch = buffer[c1];
2086 if(UTF8_IS_FIRST(ch)) break;
2090 oldCount2 = l2.count;
2092 while(c2 < oldCount2)
2094 byte ch = buffer[c2];
2095 if(UTF8_IS_FIRST(ch)) break;
2100 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2105 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2106 string = new char[len];
2107 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2108 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2112 //oldCount1 = l1.count;
2113 //oldCount2 = l2.count;
2116 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2117 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2120 if(c2 > oldCount2) c2 = oldCount2;
2121 if(!(style.freeCaret))
2122 if(c1 > oldCount1) c1 = oldCount1;
2123 newLineCount = c1 + l2.count-c2;
2129 buffer = new char[line.size ? line.size : 1];
2131 buffer = new char[line.size];
2133 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2138 if(!line.AdjustBuffer(newLineCount))
2142 /*if(newLineCount > 4000 || newLineCount < 0)
2143 printf("Warning");*/
2145 line.count = newLineCount;
2147 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2152 action.addedSpaces = c1-oldCount1;
2154 if(action.addedSpaces > action.x1)
2160 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2161 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2163 line.buffer[line.count] = '\0';
2170 this.dropX -= c2-c1;
2171 this.dropX = Max(this.dropX,0);
2173 this.x = Max(this.x,0);
2180 firstViewY = this.viewY;
2182 firstDropY = this.dropY;
2183 firstSelY = this.selY;
2184 for(line = l1;line;line = next, y++)
2192 if(line == this.viewLine)
2194 if(this.viewLine.next)
2196 this.viewLine = this.viewLine.next;
2198 style.recomputeSyntax = true;
2202 this.viewLine = this.viewLine.prev;
2204 style.recomputeSyntax = true;
2207 else if(y < firstViewY)
2209 if(line == this.line)
2213 this.line = this.line.next;
2214 this.x = this.line.count;
2219 this.line = this.line.prev;
2220 this.x = this.line.count;
2227 if(line == this.dropLine)
2229 if(this.dropLine.next)
2231 this.dropLine = this.dropLine.next;
2232 this.dropX = this.dropLine.count;
2236 this.dropLine = this.dropLine.prev;
2237 this.dropX = this.dropLine.count;
2241 else if(y < firstDropY)
2243 if(line == this.selLine)
2245 if(this.selLine.next)
2247 this.selLine = this.selLine.next;
2248 this.selX = this.selLine.count;
2252 this.selLine = this.selLine.prev;
2253 this.selX = this.selLine.count;
2257 else if(y < firstSelY)
2261 if(line == l2) break;
2265 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2268 style.recomputeSyntax = true;
2273 bool DelSel(int * addedSpacesPtr)
2275 if(this.line != this.selLine || this.x != this.selX)
2277 if(this.selY < this.y)
2279 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2282 this.line = this.selLine;
2284 else if(this.selY > this.y)
2286 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2289 this.selLine = this.line;
2291 else if(this.selX < this.x)
2293 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2296 this.line = this.selLine;
2300 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2303 this.selLine = this.line;
2311 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2313 bool hadComment = false;
2314 // Add the line here
2315 EditLine line = this.line;
2317 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2318 if(!style.hScroll && created)
2320 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2325 // Lock if no place to display.
2328 else if(endX > this.x)
2329 max = Max(this.x, line.count);
2333 for(x = 0, c = 0; c < max+count; )
2338 if(c < Min(this.x, line.count))
2339 string = line.buffer + c;
2342 else if(c < endX + count)
2343 string = stringLine + c - endX;
2345 string = line.buffer + c - endX - count;
2349 w = (tabSize * space.w) - (x % (tabSize * space.w));
2353 numBytes = UTF8_NUM_BYTES(*string);
2354 FontExtent(display, this.font, string, numBytes, &w, null);
2358 if(x >= clientSize.w)
2363 c += numBytes; // - 1;
2369 int addedSpaces = 0;
2372 // Add blank spaces if EES_FREECARET
2373 if(this.x > line.count)
2383 for(c = 0; c<line.count; c++)
2385 if(this.line.buffer[c] == '\t')
2386 position += this.tabSize - (position % this.tabSize);
2390 wantedPosition = position + (this.x - line.count);
2392 // A tab is too much...
2393 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2394 addedSpaces = wantedPosition - position;
2399 position += this.tabSize - (position % this.tabSize);
2400 // Add as many tabs as needed
2401 addedTabs += (wantedPosition - position) / this.tabSize;
2402 position += (addedTabs-1) * this.tabSize;
2403 // Finish off with spaces
2404 addedSpaces = wantedPosition - position;
2408 addedSpaces = this.x - line.count;
2411 this.x = Min(this.x, line.count);
2413 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2419 hadComment = HasCommentOrEscape(line);
2421 // Adjust the size of the line
2422 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2426 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2429 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2430 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2433 *addedTabsPtr = addedTabs;
2434 FillBytes(line.buffer+line.count,'\t',addedTabs);
2436 if(addedTabs > 4000 || addedTabs < 0)
2439 line.count += addedTabs;
2445 FillBytes(line.buffer+line.count,' ',addedSpaces);
2447 if(addedSpaces > 4000 || addedSpaces < 0)
2450 line.count += addedSpaces;
2451 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2453 else if(addedSpacesPtr)
2454 *addedSpacesPtr = 0;
2456 if(count > 4000 || count < 0)
2459 line.count += count;
2460 this.x += count + addedTabs + addedSpaces;
2463 this.selLine = this.line;
2465 line.buffer[line.count] = '\0';
2467 ComputeLength(line);
2472 hasComment = HasCommentOrEscape(line);
2473 if(!undoBuffer.insideRedo)
2475 int backDontRecord = undoBuffer.dontRecord;
2476 undoBuffer.dontRecord = 0;
2477 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
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;
2990 bool OnResizing(int *w, int *h)
2996 *w = space.h * 80 / 14;
3000 void OnResize(int w, int h)
3003 if(!hasHorzScroll && !hasVertScroll && viewLine)
3004 SetViewToCursor(true);
3006 //if(!hasHorzScroll && !hasVertScroll && viewLine)
3008 SetViewToCursor(true);
3013 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
3015 if(style.readOnly) return true;
3016 // We really shouldn't be pasting here, Unix middle button paste is for the selection (not the clipboard), good for the terminal
3017 // Middle button already serves as a dragger as well.
3022 bool OnRightButtonDown(int x, int y, Modifiers mods)
3024 this.rightButtonDown = true;
3029 bool OnRightButtonUp(int x, int y, Modifiers mods)
3032 if(!parent.inactive && rightButtonDown)
3035 Menu contextMenu { };
3037 MenuItem { contextMenu, "Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3038 MenuItem { contextMenu, "Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3039 MenuItem { contextMenu, "Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3040 MenuItem { contextMenu, "Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3041 MenuDivider { contextMenu };
3042 MenuItem { contextMenu, "Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3044 popup = PopupMenu { master = this, menu = contextMenu,
3046 nonClient = true, interim = false, parent = parent,
3047 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3049 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3053 rightButtonDown = false;
3057 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3062 if(style.noSelect) return true;
3064 if(!mods.isActivate)
3070 mouseX = mx - XOFFSET;
3073 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3075 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3081 else if(IsMouseOnSelection() && !mods.isActivate)
3091 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3093 if(mods.shift && !mods.isActivate)
3108 this.selLine = this.line;
3117 UpdateCaretPosition(true);
3121 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3125 mouseSelect = false;
3136 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3138 //PrintLn("MouseMove: ", x, ", ", y);
3144 mouseMove = IsMouseOnSelection();
3148 int size = SelSize();
3151 char * text = new char[size+1];
3155 GetSel(text, false);
3157 if(Max(selY, this.y) == dropY)
3161 if(this.dropX > this.selX)
3162 moveX = this.x - this.selX;
3166 if(this.dropX > this.x)
3167 moveX = this.selX - this.x;
3171 this.dropX -= moveX;
3172 this.selX = this.x = this.dropX;
3173 this.selY = this.y = this.dropY;
3174 this.selLine = this.line = this.dropLine;
3176 SetViewToCursor(true);
3181 byte * c = ((EditBox)this).multiLineContents, * c2;
3182 int l1 = c ? strlen(c) : 0, l2;
3185 c2 = ((EditBox)this).multiLineContents;
3186 l2 = c2 ? strlen(c2) : 0;
3187 if(l1 != l2 || (l1 && strcmp(c, c2)))
3217 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3219 //PrintLn("Dropped: ", x, ", ", y);
3221 NotifyDropped(master, this, x, y);
3228 bool OnMouseMove(int mx, int my, Modifiers mods)
3234 if(mods != -1 && mods.isSideEffect)
3239 if(style.noSelect) return true;
3240 if(wordSelect) return true;
3241 mouseX = mx - XOFFSET;
3244 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3246 if(this.mouseMove || this.mouseSelect)
3255 ((style.hScroll) || (style.vScroll)))
3262 DirtyLine(this.dropY);
3265 DirtyLine(this.dropY);
3266 this.dropLine = line;
3267 SetViewToCursor(true);
3269 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3272 else if(this.mouseSelect)
3274 DirtyLine(this.selY);
3281 SetViewToCursor(true);
3284 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3291 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3296 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3300 if(style.noSelect) return true;
3301 FindMouse(mx, my, &x, &y, &line, false);
3302 if(!NotifyDoubleClick(master, this, line, mods))
3309 for(c = x; c >= 0; c--)
3312 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3313 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3320 for(c = start; c<line.count; c += numBytes)
3322 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3333 this.line = this.selLine = line;
3334 this.wordSelect = (c != start);
3346 Menu fileMenu { menu, "File", F };
3347 saveDialog = fileDialog;
3348 MenuItem { fileMenu, "Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3349 MenuItem { fileMenu, "Save As...", A, NotifySelect = MenuFileSaveAs };
3359 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3360 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3362 space.w = Max(space.w, 1);
3363 large.w = Max(large.w, 1);
3364 space.h = Max(space.h, 1);
3365 large.h = Max(large.h, 1);
3369 for(line = lines.first; line; line = line.next)
3370 ComputeLength(line);
3375 SetViewToCursor(true);
3383 bool OnLoadGraphics()
3385 FontExtent = Display::FontExtent;
3388 // UpdateCaretPosition(true);
3392 void OnUnloadGraphics()
3397 bool OnKeyHit(Key key, unichar ch)
3399 bool shift = (key.shift) ? true : false;
3401 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3403 if(!ch && !key.alt && !key.ctrl)
3405 key.code = (SmartKey)key.code;
3408 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3411 if(style.readOnly) break;
3412 if(style.stuckCaret) GoToEnd(true);
3413 if(!(style.freeCaret))
3415 this.x = Min(this.x, this.line.count);
3421 EditLine line = this.line;
3423 for(y = this.y; y>= 0; y--)
3425 c = (y == this.y) ? (this.x-1) : line.count-1;
3427 // Slow down when going on lines...
3428 if(y != this.y) break;
3432 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3433 if(IS_ALUNDER(line.buffer[c]))
3438 if(!IS_ALUNDER(line.buffer[c]))
3440 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3454 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3455 this.x = this.selX = Min(c+1, line.count);
3456 this.y = this.selY = y;
3457 this.line = this.selLine = line;
3458 SetViewToCursor(true);
3469 if(style.readOnly) break;
3470 if(style.stuckCaret) break;
3479 if(this.line != this.selLine || this.x != this.selX)
3482 SetViewToCursor(true);
3488 if(this.x < this.line.count)
3492 //Can't delete right away as that would change the line.count
3493 for(i = this.x; i < this.line.count; i++)
3495 if(!IS_ALUNDER(this.line.buffer[i]))
3497 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3501 for(; i < this.line.count; i++)
3503 //Delete trailing whitespace
3504 if(IS_ALUNDER(this.line.buffer[i]))
3506 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3509 SetViewToCursor(true);
3512 else if(this.line.next)
3514 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3515 SetViewToCursor(true);
3521 if(!(style.freeCaret))
3523 this.selX = this.x = Min(this.x, this.line.count);
3526 if(this.x < this.line.count)
3528 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3530 else if(this.line.next)
3532 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3534 SetViewToCursor(true);
3543 if(!key.alt && !key.ctrl)
3547 bool stuffAfter = false;
3551 if(style.stuckCaret) GoToEnd(true);
3552 if(style.readOnly) break;
3553 if(!(style.multiLine)) break;
3555 for(c = 0; c<this.line.count && c<this.x; c++)
3557 if(this.line.buffer[c] == '\t')
3558 position += this.tabSize - (position % this.tabSize);
3559 else if(this.line.buffer[c] == ' ')
3567 if(this.x < this.line.count)
3570 //If last character is a { indent one tab
3571 if(this.line.buffer[this.x - 1] == '{')
3573 //Except if the next non space character is a }
3574 bool indent = false;
3576 for(i = this.x; i < this.line.size; i++)
3577 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3579 if(this.line.buffer[i] != '}')
3584 position += this.tabSize;
3587 addString = new char[position + 2];
3588 addString[len++] = '\n';
3589 addString[len] = '\0';
3591 if(stuffAfter || !style.freeCaret)
3593 for(c = 0; c<position; )
3595 if(style.useTab && c + this.tabSize <= position)
3597 addString[len++] = '\t';
3602 addString[len++] = ' ';
3606 addString[len] = '\0';
3610 if(!stuffAfter && style.freeCaret)
3612 this.x = this.selX = position;
3616 SetViewToCursor(true);
3626 if(style.stuckCaret) break;
3627 if(!(style.freeCaret))
3629 this.x = Min(this.x, this.line.count);
3630 this.selX = Min(this.selX, this.selLine.count);
3633 if(!shift) SelDirty();
3636 bool foundAlpha = false;
3639 EditLine line, lastLine;
3642 for(line = this.line; (line && !found); line = line.prev, y--)
3647 if(this.x == 0 && line != this.line)
3656 if(line == this.line) start = this.x -1; else start = line.count-1;
3657 start = Min(start, line.count-1);
3659 for(c = start; c >= 0;)
3662 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3680 byte ch = line.buffer[c];
3681 if(UTF8_IS_FIRST(ch)) break;
3685 // No next word found,
3686 if(!found && ( this.x > 0 || (!line.count && this.x)))
3700 this.line = lastLine;
3711 byte * buffer = line.buffer;
3714 byte ch = buffer[x];
3715 if(UTF8_IS_FIRST(ch)) break;
3732 if(!shift) Deselect();
3733 SetViewToCursor(true);
3739 if(style.stuckCaret) break;
3740 if(!(style.freeCaret))
3742 this.x = Min(this.x, this.line.count);
3743 this.selX = Min(this.selX, this.selLine.count);
3746 if(!shift) SelDirty();
3747 if(!shift && (this.x != this.selX || this.y != this.selY));
3750 bool onAChar = false;
3751 if(this.selX != this.x || this.selY != this.y)
3753 if(this.x<this.line.count)
3754 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3756 if(key.shift && onAChar &&
3757 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3759 bool foundAlpha = false;
3761 EditLine line, lastLine;
3763 int lastC, lastY, lastNumBytes;
3765 for(line = this.line; (line && !found); line = line.next, y++)
3767 int start = (line == this.line) ? this.x : 0;
3770 for(c = start; c < line.count; c += numBytes)
3772 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3777 lastNumBytes = numBytes;
3787 if(!found && (c != this.x || line != this.line))
3800 this.x = lastC + lastNumBytes;
3802 this.line = lastLine;
3809 bool foundAlpha = false;
3814 for(line = this.line; (line && !found); line = line.next, y++)
3816 int start = (line == this.line) ? this.x : 0;
3819 for(c = start; c < line.count; c += numBytes)
3821 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3836 // No next word found,
3837 if(!found && (c != this.x || line != this.line))
3841 this.x = line.count;
3853 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3857 byte * buffer = line.buffer;
3860 byte ch = buffer[x];
3861 if(UTF8_IS_FIRST(ch)) break;
3882 if(!shift) Deselect();
3883 SetViewToCursor(true);
3890 if(!style.vScroll || hasVertScroll) break;
3896 if(style.stuckCaret) break;
3898 if(!shift) SelDirty();
3908 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3912 if(!shift) Deselect();
3914 SetViewToCursor(false);
3917 if(caretY == this.y * space.h)
3923 if(!style.freeCaret)
3924 this.x = Min(this.x, line.count);
3934 int sx = 0, sy = this.y * space.h;
3935 char * text = line.text;
3936 int maxW = clientSize.w - sx;
3937 display.FontExtent(font, " ", 1, null, &th);
3941 int startPos = textPos;
3944 bool lineComplete = false;
3946 if(!style.wrap && caretY == MAXINT)
3949 //textPos = line.count;
3950 //lineComplete = true;
3953 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3957 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3960 len = (nextSpace - (text + textPos));
3962 len = line.count - textPos;
3964 if(textPos < line.count)
3966 display.FontExtent(font, text + textPos, len, &w, null);
3968 if(nextSpace) { w += space.w; len++; }
3970 if(style.wrap && x + width + w > maxW && x > 0)
3972 lineComplete = true;
3982 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
3986 while(this.x > 0 && x + sx > caretX && this.x > startPos)
3989 if(this.x > line.count)
3992 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
3993 len = this.x - startPos;
3994 display.FontExtent(font, text + startPos, len, &x, null);
3998 if(!shift) Deselect();
4000 SetViewToCursor(false);
4004 if(sy == caretY - th || textPos >= line.count)
4006 if(textPos >= line.count)
4008 int c = textPos - 1;
4009 while(c > 0 && text[c] == ' ') c--;
4013 this.x = line.count;
4016 if(!shift) Deselect();
4018 SetViewToCursor(false);
4023 } while(textPos < line.count);
4026 if(!shift) Deselect();
4028 SetViewToCursor(false);
4037 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4038 if(!shift) SelDirty();
4039 this.line = this.line.prev;
4045 if(!shift) Deselect();
4049 SetViewToCursor(false);
4055 return style.multiLine ? false : true;
4059 if(!style.vScroll || hasVertScroll)
4066 if(style.stuckCaret) break;
4070 int sx = 0, sy = this.y * this.space.h;
4071 int maxW = clientSize.w - sx;
4072 char * text = line.buffer;
4074 if(!shift) SelDirty();
4080 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4090 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4093 if(!shift) Deselect();
4095 if(this.selX != this.x || this.selY != this.y)
4097 SetViewToCursor(false);
4100 while(!textPos || (style.freeCaret || textPos<line.count))
4102 int startPos = textPos;
4105 bool lineComplete = false;
4106 if(!style.wrap && sy <= caretY)
4108 textPos = line.count;
4109 lineComplete = true;
4111 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4115 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4118 len = (nextSpace - (text + textPos));
4120 len = line.count - textPos;
4122 if(textPos < line.count)
4124 display.FontExtent(font, text + textPos, len, &w, &th);
4126 if(nextSpace) { w += space.w; len++; }
4127 if(style.wrap && x + width + w > maxW && x > 0)
4129 lineComplete = true;
4139 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4143 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4146 if(this.x > line.count)
4149 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4151 len = this.x - startPos;
4152 display.FontExtent(font, text + startPos, len, &x, null);
4155 if(!shift) Deselect();
4158 SetViewToCursor(false);
4164 this.x = line.count;
4167 if(!shift) Deselect();
4170 SetViewToCursor(false);
4173 else if(textPos >= line.count && line.next)
4179 sy = this.y * this.space.h;
4180 sx = 0; //textBlock.startX;
4186 sx = 0; //textBlock.startX;
4194 this.x = Min(this.x, line.count);
4195 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4197 if(!shift) Deselect();
4200 if(this.selX != this.x || this.selY != this.y)
4203 SetViewToCursor(false);
4210 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4211 if(!shift) SelDirty();
4212 this.line = this.line.next;
4216 if(!shift) Deselect();
4219 if(this.selX != this.x || this.selY != this.y)
4227 return style.multiLine ? false : true;
4230 if(style.stuckCaret) break;
4231 if(!(style.freeCaret))
4232 this.selX = Min(this.selX, this.selLine.count);
4234 if(!shift) SelDirty();
4237 this.line = this.lines.first;
4238 if(this.y != 0 || this.x != 0)
4248 EditLine line = this.line;
4250 for(c=0; line.buffer[c]; c++)
4251 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4253 if(c != 0 || this.x)
4263 DirtyLine(this.y);*/
4268 if(!shift) Deselect();
4269 SetViewToCursor(true);
4275 if(style.stuckCaret) break;
4276 if(!(style.freeCaret))
4277 this.selX = Min(this.selX, this.selLine.count);
4279 if(!shift) SelDirty();
4284 else if(this.x != this.line.count)
4286 this.x = this.line.count;
4287 //DirtyLine(this.y);
4290 if(!shift) Deselect();
4291 SetViewToCursor(true);
4296 if(style.tabKey && !key.ctrl)
4298 if(this.selY != this.y && style.tabSel)
4300 EditLine firstLine, lastLine;
4304 // Do multi line selection tabbing here
4305 if(this.selY < this.y)
4307 firstLine = this.selLine;
4308 lastLine = this.line;
4314 // Selecting going up
4315 firstLine = this.line;
4316 lastLine = this.selLine;
4323 for(line = firstLine; line; line = line.next, y++)
4325 if(line != lastLine || x)
4329 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4331 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4333 if(line.buffer[c] == '\t')
4338 else if(line.buffer[c] != ' ')
4343 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4346 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4347 char * string = new char[len];
4348 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4349 GetText(string, line, y, 0, line, y, lastC, false, false);
4352 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4353 if(!line.AdjustBuffer(line.count-lastC))
4359 if(line == lastLine) break;
4364 for(line = firstLine; line; line = line.next, y++)
4368 if(line != lastLine || x)
4372 if(line.count + 1 <= this.maxLineSize)
4374 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4376 if(!line.AdjustBuffer(line.count+1))
4379 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4381 AddCharAction action { ch = '\t', x = 0, y = y };
4385 memmove(line.buffer+1,line.buffer,line.size-1);
4387 line.buffer[0] = '\t';
4392 if(line.count + this.tabSize <= this.maxLineSize)
4395 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4396 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4398 if(!line.AdjustBuffer(line.count+this.tabSize))
4402 char * string = new char[this.tabSize + 1];
4403 memset(string, ' ', this.tabSize);
4404 string[this.tabSize] = '\0';
4405 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4408 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4409 line.count+=this.tabSize;
4410 for(c=0; c<this.tabSize; c++)
4411 line.buffer[c] = ' ';
4417 if(line == lastLine) break;
4431 char * addString = new char[this.tabSize + 1];
4433 if(!(style.freeCaret))
4435 this.x = Min(this.x, this.line.count);
4440 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4442 addString[len++] = ' ';
4450 SetViewToCursor(true);
4457 if(!(style.hScroll) || hasHorzScroll) break;
4458 if(this.viewX < this.maxLength)
4460 //this.viewX+=this.space.w*this.tabSize;
4462 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4469 if(!shift) Deselect();
4478 if(!(style.hScroll) || hasHorzScroll) break;
4481 //this.viewX-=this.space.w*this.tabSize;
4482 //this.viewX = Max(this.viewX,0);
4484 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4485 // SetCursorToView();
4492 if(!shift) Deselect();
4506 if(!(style.readOnly))
4512 this.overwrite ^= 1;
4513 UpdateCaretPosition(true);
4518 NotifyOvrToggle(master, this, this.overwrite);
4529 if(style.noSelect) break;
4532 //Save current view position
4533 int tempX = this.viewX;
4534 int tempY = this.viewY;
4538 this.selLine = this.lines.first;
4539 this.y = this.lineCount-1;
4540 this.line = this.lines.last;
4541 this.x = this.line.count;
4544 SetViewToCursor(true);
4546 //Restore previous view position
4547 SetScrollPosition(tempX, tempY * this.space.h);
4553 // TOCHECK: Was there any good reason why we weren't returning false here?
4554 return false; // break;
4559 if(!(style.readOnly))
4567 if(style.readOnly) break;
4573 if(style.readOnly) break;
4578 if(style.readOnly) break;
4582 if(style.readOnly) break;
4586 if(style.readOnly) break;
4587 if(key.shift && key.code == rightBracket)
4589 //Only indent back if you are exactly at one tab.
4591 bool whitespace = true;
4597 EditLine line = this.line;
4599 //Only remove one tab if there is nothing else on the line.
4600 for(i = 0; i < this.line.count; i++)
4602 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4616 for(pos = line.count - 1; pos >= 0; pos--)
4618 char c = line.buffer[pos];
4622 indentwidth += this.tabSize;
4624 //Counting backwards, so when you find a character, indentation is reset
4634 //Place the } to get an undo:
4637 this.x = this.line.count;
4641 putsize = indentwidth;
4643 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4645 newline = new char[putsize+2];
4646 newline[putsize] = '}';
4647 newline[putsize+1] = '\0';
4651 for(; i < indentwidth / this.tabSize; i++)
4653 for(;i < putsize; i++)
4661 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4673 void OnHScroll(ScrollBarAction action, int position, Key key)
4676 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4678 this.viewX = position;
4679 if(action != setRange)
4681 if(!this.mouseMove && style.cursorFollowsView)
4688 void OnVScroll(ScrollBarAction action, int position, Key key)
4690 int oldViewY = this.viewY;
4693 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4695 position /= this.space.h;
4697 if(position < this.viewY)
4699 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4700 style.recomputeSyntax = true;
4702 else if(position > this.viewY)
4704 EditLine oldViewLine = viewLine;
4705 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4706 FigureStartSyntaxStates(oldViewLine, false);
4709 if(action != setRange)
4711 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4714 if(this.x != this.selX || this.y != this.selY)
4718 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4721 int numLines = clientSize.h / this.space.h;
4723 if(Abs(this.viewY - oldViewY) < numLines)
4726 if(this.viewY > oldViewY)
4728 for(y = oldViewY; y <this.viewY; y++)
4729 DirtyLine(y + numLines);
4733 for(y = this.viewY; y <oldViewY; y++)
4734 DirtyLine(y + numLines);
4739 // Fix dirt of stuff before first line...
4740 if(this.viewY - oldViewY > 0)
4742 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4749 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4754 ReplaceTextAction replaceAction = null;
4755 AddCharAction addCharAction = null;
4756 int addedSpaces = 0, addedTabs = 0;
4758 if(ch == '\r') return true;
4759 if(style.stuckCaret /*|EES_READONLY)*/ )
4762 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4764 if(!undoBuffer.dontRecord)
4766 if(selX != x || selY != y)
4771 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4772 oldString = new char[len];
4773 UTF32toUTF8Len(&ch, 1, buffer, 4);
4774 newString = CopyString(buffer);
4775 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4777 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4778 if(selY < y || (selY == y && selX < x))
4780 replaceAction.x1 = selX;
4781 replaceAction.y1 = selY;
4782 replaceAction.x2 = x;
4783 replaceAction.y2 = y;
4787 replaceAction.x1 = x;
4788 replaceAction.y1 = y;
4789 replaceAction.x2 = selX;
4790 replaceAction.y2 = selY;
4792 Record(replaceAction);
4793 undoBuffer.dontRecord++;
4797 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4798 Record(addCharAction);
4804 DelSel(&addedSpaces);
4805 if(this.lineCount+1 > this.maxLines)
4808 Emptyline(this.lines.first,0);
4812 if(!(style.vScroll))
4814 // Make sure it fits, but we need a default line is this.font is too big for window
4815 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4818 if((this.y >= 0) && this.y < this.viewY)
4823 line = EditLine { };
4826 lines.Insert(this.line, line);
4827 line.editBox = this;
4833 // If we're displacing the lines from a current line ...
4836 if(this.line.buffer)
4839 if(this.line.count < endX) endX = this.line.count;
4840 length = this.line.count - endX;
4843 if(!line.AdjustBuffer(length))
4847 if(this.line.buffer)
4849 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4851 if(endX > 4000 || endX < 0)
4854 this.line.count = endX;
4855 this.line.buffer[this.line.count] = '\0';
4856 this.line.AdjustBuffer(this.line.count);
4857 ComputeLength(this.line);
4861 BufferLocation before = { this.line, this.y, this.x }, after;
4867 ComputeLength(this.line);
4870 if(length > 4000 || length < 0)
4873 line.count = length;
4877 line.buffer[line.count] = '\0';
4880 after.line = this.line, after.y = this.y, after.x = this.x;
4882 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4888 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4889 DelSel(&addedSpaces);
4890 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4891 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4892 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4896 this.selLine = this.line;
4900 replaceAction.x3 = x;
4901 replaceAction.y3 = y;
4902 replaceAction.addedSpaces = addedSpaces;
4903 replaceAction.addedTabs = addedTabs;
4904 undoBuffer.dontRecord--;
4908 addCharAction.addedSpaces = addedSpaces;
4909 addCharAction.addedTabs = addedTabs;
4916 /****************************************************************************
4918 ****************************************************************************/
4921 bool AddCh(unichar ch)
4923 return _AddCh(ch, null, null);
4928 this.modified = true;
4929 NotifyUpdate(master, this);
4930 modifiedDocument = true;
4933 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4936 DelCh(line1, y1, x1, line2, y2, x2, false);
4937 SetViewToCursor(true);
4945 itemEditUndo.disabled = undoBuffer.curAction == 0;
4946 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4947 if(savedAction == undoBuffer.curAction)
4949 modifiedDocument = false;
4951 NotifyUnsetModified(master, this);
4958 itemEditUndo.disabled = undoBuffer.curAction == 0;
4959 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4960 if(savedAction == undoBuffer.curAction)
4962 modifiedDocument = false;
4964 NotifyUnsetModified(master, this);
4968 void Record(UndoAction action)
4970 if(!undoBuffer.dontRecord)
4972 undoBuffer.Record(action);
4973 itemEditUndo.disabled = undoBuffer.curAction == 0;
4974 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4981 this.lines.first, 0,0,
4982 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
4985 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4990 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
4991 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
4992 this.y = line2 ? y2 : (this.lineCount-1);
4993 this.line = line2 ? (EditLine)line2 : this.lines.last;
4996 SetViewToCursor(true);
5000 // TODO: Fix this vs modifiedDocument window property
5001 void SetModified(bool flag)
5005 this.modified = false;
5006 if(flag && !NotifyModified(master, this))
5007 this.modified = true;
5012 bool AddS(char * string)
5019 int addedSpaces = 0, addedTabs = 0;
5020 AddTextAction action = null;
5021 ReplaceTextAction replaceAction = null;
5023 this.pasteOperation = true;
5025 if(style.stuckCaret /*|EES_READONLY)*/ )
5028 if(!undoBuffer.dontRecord)
5030 char * placeString = CopyString(string);
5031 if(!style.multiLine)
5035 for(i = 0; (ch = placeString[i]); i++)
5038 placeString[i] = '\0';
5039 placeString = renew placeString byte[i+1];
5044 if(selX != x || selY != y)
5048 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5049 oldString = new char[len];
5050 newString = placeString;
5051 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5053 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5054 if(selY < y || (selY == y && selX < x))
5056 replaceAction.x1 = selX;
5057 replaceAction.y1 = selY;
5058 replaceAction.x2 = x;
5059 replaceAction.y2 = y;
5063 replaceAction.x1 = x;
5064 replaceAction.y1 = y;
5065 replaceAction.x2 = selX;
5066 replaceAction.y2 = selY;
5068 Record(replaceAction);
5072 if(string[0] == '\n')
5073 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5075 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5083 undoBuffer.dontRecord++;
5084 DelSel(&addedSpaces);
5088 for(c = 0; string[c]; c++)
5090 if(string[c] == '\n' || string[c] == '\r')
5092 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5097 if(string[c] == '\n')
5105 // Reset for next line
5109 if(string[c] == '\r' && *line == '\n')
5121 // Why was this here?
5124 // Add the line here
5126 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5132 undoBuffer.dontRecord--;
5137 action.addedSpaces = addedSpaces;
5138 action.addedTabs = addedTabs;
5140 else if(replaceAction)
5142 replaceAction.y3 = y;
5143 replaceAction.x3 = x;
5144 replaceAction.addedSpaces = addedSpaces;
5145 replaceAction.addedTabs = addedTabs;
5148 UpdateCaretPosition(true);
5150 this.pasteOperation = false;
5163 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5164 SetViewToCursor(true);
5170 void PutCh(unichar ch)
5174 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5175 //if((ch >= 32) || ch == '\n')
5177 int addedSpaces = 0, addedTabs = 0;
5178 ReplaceTextAction replaceAction = null;
5179 AddCharAction addCharAction = null;
5182 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5184 if(this.x < this.line.count && this.overwrite)
5189 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5190 oldString = new char[len];
5191 UTF32toUTF8Len(&ch, 1, buffer, 4);
5192 newString = CopyString(buffer);
5193 GetText(oldString, line, y, x, line, y, x+1, false, false);
5194 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5195 replaceAction.x1 = x;
5196 replaceAction.y1 = y;
5197 replaceAction.x2 = x+1;
5198 replaceAction.y2 = y;
5199 Record(replaceAction);
5201 undoBuffer.dontRecord++;
5202 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5203 undoBuffer.dontRecord--;
5205 else if(!undoBuffer.dontRecord)
5207 if(selX != x || selY != y)
5212 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5213 oldString = new char[len];
5214 UTF32toUTF8Len(&ch, 1, buffer, 4);
5215 newString = CopyString(buffer);
5216 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5217 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5218 if(selY < y || (selY == y && selX < x))
5220 replaceAction.x1 = selX;
5221 replaceAction.y1 = selY;
5222 replaceAction.x2 = x;
5223 replaceAction.y2 = y;
5227 replaceAction.x1 = x;
5228 replaceAction.y1 = y;
5229 replaceAction.x2 = selX;
5230 replaceAction.y2 = selY;
5232 Record(replaceAction);
5236 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5237 Record(addCharAction);
5240 undoBuffer.dontRecord++;
5241 result = _AddCh(ch, &addedSpaces, &addedTabs);
5244 replaceAction.x3 = x;
5245 replaceAction.y3 = y;
5246 replaceAction.addedSpaces = addedSpaces;
5247 replaceAction.addedTabs = addedTabs;
5251 addCharAction.addedSpaces = addedSpaces;
5252 addCharAction.addedTabs = addedTabs;
5254 undoBuffer.dontRecord--;
5258 if(result) SetViewToCursor(true);
5262 void PutS(char * string)
5267 SetViewToCursor(true);
5272 void Printf(char * format, ...)
5276 char temp[MAX_F_STRING];
5278 va_start(args, format);
5279 vsprintf(temp, format, args);
5285 void SetContents(char * format, ...)
5290 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5293 char temp[MAX_F_STRING];
5295 va_start(args, format);
5296 vsprintf(temp, format, args);
5312 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5315 else if(this.line.prev)
5317 EditLine line = this.line.prev;
5321 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5329 this.selLine = this.line;
5334 SetViewToCursor(true);
5339 Emptyline(this.line,this.y);
5340 this.selX = this.x = 0;
5343 this.selLine = this.line;
5345 SetViewToCursor(true);
5355 SetViewToCursor(true);
5363 SetViewToCursor(true);
5367 bool GoToLineNum(int lineNum)
5372 EditLine line = this.lines.first;
5373 for(c = 0; c < lineNum && line; c++, line = line.next);
5383 SetViewToCursor(true);
5390 bool GoToPosition(EditLine line, int y, int x)
5402 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5416 SetViewToCursor(true);
5423 void SetViewToCursor(bool setCaret)
5433 bool dontScroll = false;
5439 selected = selX != this.x || selY != y;
5446 checkLine = dropLine;
5452 checkLine = this.line;
5457 numLines = clientSize.h / space.h;
5459 // This is broken. The EditBox now doesn't do anything different when adding to it,
5460 // regardless of the previous scrolling position. It should be read and then set again
5461 // if one wishes to preserve it.
5462 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5463 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5467 // Horizontal Adjustment
5468 if(!dontScroll && checkLine)
5472 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5475 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5481 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5482 viewX = x - clientSize.w+space.w;
5483 if(x < this.viewX + clientSize.w/2 - space.w)
5484 viewX = Max(0, x - clientSize.w/2 + space.w);
5492 // Vertical Adjustment
5493 if(viewY > checkY) viewY = checkY;
5494 if(viewY + numLines <= checkY)
5496 if(clientSize.h >= space.h)
5498 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5508 for(;dropLine && dropLine.prev && dropY >= numLines;)
5510 dropLine = dropLine.prev;
5514 for(;this.line && this.line.prev && this.y >= numLines;)
5516 this.line = this.line.prev;
5521 SetScrollPosition(viewX, viewY * this.space.h);
5523 UpdateCaretPosition(setCaret);
5529 selLine = this.line;
5539 void CenterOnCursor()
5541 int numLines = clientSize.h / this.space.h;
5542 int y = this.y - numLines / 2;
5544 bool figureSyntax = false;
5545 EditLine oldViewLine = viewLine;
5546 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5551 for(;y < this.viewY; y++)
5553 this.viewLine = this.viewLine.prev;
5554 style.recomputeSyntax = true;
5556 for(;y > this.viewY; y--)
5558 this.viewLine = this.viewLine.next;
5559 figureSyntax = true;
5562 FigureStartSyntaxStates(oldViewLine, false);
5566 SetScrollPosition(this.viewX, viewY * this.space.h);
5567 UpdateCaretPosition(true);
5571 void SetCursorToView()
5582 numLines = clientSize.h / this.space.h;
5586 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5587 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5591 EditLine oldLine = this.line;
5592 bool lastOne = false;
5593 EditLine oldViewLine = this.viewLine;
5594 bool figureSyntax = false;
5596 if(this.y >= this.lineCount-1) return;
5598 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5600 if(this.viewY + numLines < this.lines.count)
5602 this.viewLine = this.viewLine.next;
5604 figureSyntax = true;
5606 else if(c && !lastOne)
5615 FigureStartSyntaxStates(oldViewLine, false);
5616 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5619 SetViewToCursor(false);
5628 if(this.y == 0) return;
5630 numLines = clientSize.h / this.space.h;
5634 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5635 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5639 EditLine oldLine = this.line;
5641 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5646 if(this.viewLine.prev)
5648 this.viewLine = this.viewLine.prev;
5650 style.recomputeSyntax = true;
5654 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5657 SetViewToCursor(false);
5664 if(this.viewLine.prev)
5667 // this.viewLine = this.viewLine.prev;
5670 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5677 if(this.viewLine.next)
5680 // this.viewLine = this.viewLine.next;
5683 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5690 EditLine l1, l2, line;
5691 int x1, x2, nx1, nx2;
5696 if(!this.selLine) return 0;
5697 if(this.selLine == this.line && this.selX == this.x) return 0;
5698 if(this.selY < this.y)
5705 else if(this.selY > this.y)
5712 else if(this.selX < this.x)
5714 l1 = l2 = this.line;
5720 l1 = l2 = this.line;
5724 nx1 = Min(x1,l1.count);
5725 nx2 = Min(x2,l2.count);
5727 // Find Number of Bytes Needed
5729 for(line = l1; line; line = line.next)
5731 if(line == l1) start = nx1; else start = 0;
5732 if(line == l2) end = nx2; else end = line.count;
5734 if(style.freeCaret && line == l2)
5737 count = Max(x2-Max(x1,l1.count),0);
5739 count = Max(x2-l2.count,0);
5743 if(line == l2) break;
5744 // Add Carriage Return / line Feed
5751 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5755 if(!reorder || this.selY < this.y)
5757 if(l1) *l1 = this.selLine;
5758 if(y1) *y1 = this.selY;
5759 if(x1) *x1 = this.selX;
5760 if(l2) *l2 = this.line;
5761 if(y2) *y2 = this.y;
5762 if(x2) *x2 = this.x;
5764 else if(this.selY > this.y)
5766 if(l1) *l1 = this.line;
5767 if(y1) *y1 = this.y;
5768 if(x1) *x1 = this.x;
5769 if(l2) *l2 = this.selLine;
5770 if(y2) *y2 = this.selY;
5771 if(x2) *x2 = this.selX;
5773 else if(this.selX < this.x)
5775 if(l1) *l1 = this.line;
5776 if(y1) *y1 = this.selY;
5777 if(x1) *x1 = this.selX;
5778 if(l2) *l2 = this.line;
5779 if(y2) *y2 = this.y;
5780 if(x2) *x2 = this.x;
5784 if(l1) *l1 = this.line;
5785 if(y1) *y1 = this.y;
5786 if(x1) *x1 = this.x;
5787 if(l2) *l2 = this.line;
5788 if(y2) *y2 = this.selY;
5789 if(x2) *x2 = this.selX;
5794 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5796 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5798 this.selLine = (EditLine)l1;
5801 this.line = (EditLine)l2;
5805 SetViewToCursor(true);
5809 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5811 EditLine l1, l2, line;
5812 int x1, x2, nx1, nx2;
5843 nx1 = Min(x1,l1.count);
5844 nx2 = Min(x2,l2.count);
5847 for(line = l1; line; line = line.next)
5849 if(line == l1) start = nx1; else start = 0;
5850 if(line == l2) end = nx2; else end = line.count;
5853 CopyBytes(text, line.buffer + start, end - start);
5856 numChars += end-start;
5858 if(style.freeCaret && line == l2 && addSpaces)
5861 count = Max(x2-Max(x1,l1.count),0);
5863 count = Max(x2-l2.count,0);
5866 FillBytes(text,' ',count);
5872 if(line == l2) break;
5884 // '\0' terminate Terminate
5891 void GetSel(char * text, bool addCr)
5893 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5899 this.selLine = this.line;
5909 int size = SelSize();
5912 // Try to allocate memory
5913 ClipBoard clipBoard { };
5914 if(clipBoard.Allocate(size+1))
5916 GetSel(clipBoard.memory, true);
5929 ClipBoard clipBoard { };
5930 if(clipBoard.Load())
5931 PutS(clipBoard.memory);
5942 SetViewToCursor(true);
5947 void DeleteSelection()
5952 SetViewToCursor(true);
5958 void Save(File f, bool lf)
5961 savedAction = undoBuffer.curAction;
5963 for(line = this.lines.first; line; line = line.next)
5965 f.Write(line.buffer, line.count,1);
5968 if(lf) f.Putc('\r');
5974 #define BUFFER_SIZE 16384
5977 undoBuffer.dontRecord++;
5980 char buffer[BUFFER_SIZE];
5984 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
5985 buffer[count] = '\0';
5991 undoBuffer.dontRecord--;
5992 undoBuffer.count = 0;
5993 undoBuffer.curAction = 0;
5994 itemEditUndo.disabled = undoBuffer.curAction == 0;
5995 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5998 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
6002 bool firstPass = true;
6003 EditBoxFindResult result = found;
6005 if(!this.line) return notFound;
6008 for(line = this.line;;)
6016 line = this.lines.first;
6022 line = this.lines.last;
6023 num = this.lineCount - 1;
6029 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6031 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
6035 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6038 if(line == this.line && !firstPass) break;
6056 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6060 int searchLen = strlen(text);
6061 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6063 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6064 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6066 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6073 bool OnKeyDown(Key key, unichar ch)
6076 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6078 if(!NotifyKeyDown(master, this, key, ch))
6088 this.mouseMove = false;
6089 OnLeftButtonUp(0,0,0);
6090 SetViewToCursor(true);
6100 public class EditBoxStream : File
6103 BufferLocation start, sel;
6110 EditBox editBox = this.editBox;
6112 editBox.x = start.x;
6113 editBox.y = start.y;
6114 editBox.line = start.line;
6116 editBox.selX = sel.x;
6117 editBox.selY = sel.y;
6118 editBox.selLine = sel.line;
6120 editBox.SetViewToCursor(true);
6121 //editBox.ComputeColumn();
6125 property EditBox editBox
6133 start.line = value.line;
6137 sel.line = value.selLine;
6140 value.GoToHome(true);
6143 get { return editBox; }
6146 uint Read(byte * buffer, uint size, uint count)
6149 EditBox editBox = this.editBox;
6150 EditLine line = editBox.line;
6156 for(;read < count && line; line = (*&line.next))
6158 int numBytes = Min(count - read, (*&line.count) - x);
6161 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6166 for(;read < count && x < (*&line.count);)
6168 buffer[read++] = (*&line.buffer)[x++];
6175 buffer[read++] = '\n';
6180 if(x == (*&line.count) && (*&line.next))
6189 editBox.line = editBox.selLine = line;
6190 editBox.x = editBox.selX = x;
6191 editBox.y = editBox.selY = y;
6196 bool Seek(int pos, FileSeekMode mode)
6199 EditBox editBox = this.editBox;
6200 EditLine line = editBox.line;
6202 if(mode == FileSeekMode::start)
6204 pos = pos - this.pos;
6214 for(;read < pos && line; line = (*&line.next))
6216 int numBytes = Min(pos - read, (*&line.count) - x);
6219 read += numBytes; x += numBytes;
6222 /*for(;read < pos && x < (*&line.count);)
6250 for(;read < pos && line; line = (*&line.prev))
6252 int numBytes = Min(pos - read, x);
6258 /*for(;read < pos && x > 0;)
6268 x = (*&(*&line.prev).count);
6284 editBox.line = editBox.selLine = line;
6285 editBox.x = editBox.selX = x;
6286 editBox.y = editBox.selY = y;
6291 bool Puts(char * string)
6293 EditBox editBox = this.editBox;
6294 BufferLocation start { editBox.line, editBox.y, editBox.x };
6298 editBox.AddS(string);
6300 pos.line = editBox.line;
6304 this.start.AdjustAdd(start, pos);
6305 sel.AdjustAdd(start, pos);
6310 // NOTE: BYTE, NOT UNICODE CHARACTER!
6313 EditBox editBox = this.editBox;
6314 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6319 utf8Bytes[numBytes++] = ch;
6320 utf8Bytes[numBytes] = 0;
6321 if(UTF8Validate(utf8Bytes))
6323 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6325 pos.line = editBox.line;
6328 this.start.AdjustAdd(start, pos);
6329 sel.AdjustAdd(start, pos);
6336 bool Getc(char * ch)
6338 return Read(ch, 1, 1) ? true : false;
6341 void DeleteBytes(uint count)
6343 EditBox editBox = this.editBox;
6346 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6347 BufferLocation end = pos;
6352 for(;c < count && end.line && end.x < end.line.count;)
6357 if(c < count && end.line && end.line.next)
6359 end.line = end.line.next;
6368 start.AdjustDelete(pos, end);
6369 sel.AdjustDelete(pos, end);
6371 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);