1 namespace gui::controls;
12 #define XOFFSET (3 + (/*style.lineNumbers?6 * this.space.w:*/0))
14 #define YOFFSET (style.multiLine ? 1 : ((clientSize.h + 1 - space.h) / 2))
16 #define IS_ALUNDER(ch) (/*(ch) == '_' || */CharMatchCategories((ch), letters|numbers|marks|connector))
18 #define UTF8_IS_FIRST(x) (__extension__({ byte b = x; (!(b) || !((b) & 0x80) || ((b) & 0x40)); }))
19 #define UTF8_NUM_BYTES(x) (__extension__({ byte b = x; (b & 0x80 && b & 0x40) ? ((b & 0x20) ? ((b & 0x10) ? 4 : 3) : 2) : 1; }))
20 #define UTF8_GET_CHAR(string, numBytes) \
24 byte b = (string)[0]; \
29 if(b & 0x80 && b & 0x40) \
44 for(i = 0; i<numBytes; i++) \
47 ch |= (string)[i] & mask; \
55 bool autoEmpty:1, readOnly:1, multiLine:1, stuckCaret:1, freeCaret:1, select:1, hScroll:1, vScroll:1, smartHome:1;
56 bool noCaret:1, noSelect:1, tabKey:1, useTab:1, tabSel:1, allCaps:1, syntax:1, wrap:1;
59 bool inMultiLineComment:1, inPrep:1, escaped:1;
61 bool recomputeSyntax:1;
62 bool cursorFollowsView:1;
64 // bool lineNumbers:1;
68 void UnregisterClass_EditBox()
71 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
80 extern int __ecereVMethodID_class_OnFree;
83 static class ArrayImpl
97 void ** array = (void **)((ArrayImpl)this).array;
98 if(type.type == normalClass || type.type == noHeadClass)
100 for(c = 0; c<size; c++)
101 type._vTbl[__ecereVMethodID_class_OnFree](type, array[c]);
103 // TODO: Call OnFree for structClass
104 delete ((ArrayImpl)this).array;
113 if(((ArrayImpl)this).array)
117 ((ArrayImpl)this).array = renew0 ((ArrayImpl)this).array byte[type.typeSize * value];
120 ((ArrayImpl)this).array = new0 byte[value * type.typeSize];
129 memcpy(((ArrayImpl)this).array, value, type.typeSize * size);
134 public class UndoAction : struct
137 subclass(UndoAction) type;
138 virtual void Undo(void * data) { type.Undo(this, data); }
139 virtual void Redo(void * data) { type.Redo(this, data); }
141 virtual void Print(void * data) { type.Print(this, data); }
145 if(((Class)type).Destructor)
146 ((void (*)(void *))((Class)type).Destructor)(this);
151 class ArrayUndoActions : OldArray
153 type = class(UndoAction);
157 public class UndoBuffer
159 ArrayUndoActions actions { size = 8 };
174 UndoAction action = actions._[--curAction];
176 /*Print("Undoing: ");
177 action.Print(data);*/
188 if(curAction < count)
190 UndoAction action = actions._[curAction];
193 /*Print("Redoing: ");
194 action.Print(data);*/
202 void Record(UndoAction action)
204 if(!dontRecord && !insideRedo)
206 if(curAction < count)
209 for(c = curAction; c < count; c++)
215 if(count >= actions.size)
216 actions.size += actions.size / 2;
219 /*Print("Recording: ");
220 action.Print(data);*/
222 actions._[count++] = action;
225 if(actions.size > count + count / 2 && count + count / 2 >= 8)
226 actions.size = count + count / 2;
233 static class AddCharAction : UndoAction
237 int addedSpaces, addedTabs;
238 type = class(AddCharAction);
240 void Undo(EditBox editBox)
242 editBox.GoToPosition(null, (ch == '\n') ? (y + 1) : y, (ch == '\n') ? 0 : (x + 1));
244 if(addedTabs || addedSpaces)
245 editBox.DelCh(editBox.line, y, x - (addedSpaces + addedTabs), editBox.line, y, x, false);
246 editBox.UpdateDirty();
249 void Redo(EditBox editBox)
251 editBox.GoToPosition(null, y, x);
253 editBox.UpdateDirty();
256 void Print(EditBox editBox)
258 PrintLn("AddChar: y = ", y, "x = ", x, ", ch = ", ch, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
263 static class AddTextAction : UndoAction
267 int addedSpaces, addedTabs;
268 type = class(AddTextAction);
271 void Print(EditBox editBox)
273 PrintLn("AddText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs);
281 void Undo(EditBox editBox)
286 editBox.GoToPosition(null, y1, x1);
288 l2 = editBox.lines.first;
289 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
291 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
292 if(addedTabs || addedSpaces)
293 editBox.DelCh(editBox.line, y1, x1 - (addedSpaces + addedTabs), editBox.line, y1, x1, false);
295 editBox.SetViewToCursor(true);
299 void Redo(EditBox editBox)
301 editBox.GoToPosition(null, y1, x1);
302 editBox.PutS(string);
306 static class DelTextAction : UndoAction
312 type = class(DelTextAction);
315 void Print(EditBox editBox)
317 PrintLn("DelText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", string = ", string, ", addedSpaces = ", addedSpaces, ", placeAfter = ", placeAfter);
320 void Undo(EditBox editBox)
322 editBox.GoToPosition(null, y1, x1);
323 editBox.PutS(string);
327 editBox.GoToPosition(null, y1, x1);
330 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
331 //editBox.SetViewToCursor(true);
334 editBox.DelCh(editBox.line, y1, x1 - addedSpaces, editBox.line, y1, x1, false);
340 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
341 //editBox.SetViewToCursor(true);
344 editBox.DelCh(editBox.selLine, y1, x1 - addedSpaces, editBox.selLine, y1, x1, false);
348 void Redo(EditBox editBox)
353 editBox.GoToPosition(null, y1, x1);
356 l2 = editBox.lines.first;
357 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
359 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
360 editBox.SetViewToCursor(true);
370 static class ReplaceTextAction : UndoAction
372 int y1, x1, y2, x2, y3, x3;
376 int addedSpaces, addedTabs;
378 type = class(ReplaceTextAction);
381 void Print(EditBox editBox)
383 PrintLn("ReplaceText: y1 = ", y1, "x1 = ", x1, ", y2 = ", y2, ", x2 = ", x2, ", y3 = ", y3, ", x3 = ", x3, ", oldString = ", oldString, ", newString = ", newString, ", addedSpaces = ", addedSpaces, ", addedTabs = ", addedTabs, ", placeAfter = ", placeAfter);
386 void Undo(EditBox editBox)
391 editBox.GoToPosition(null, y1, x1);
393 l3 = editBox.lines.first;
394 for(c = 0; c < y3 && l3; c++, l3 = l3.next);
396 editBox.DelCh(l1, y1, x1, l3, y3, x3, true);
398 if(addedSpaces || addedTabs)
399 editBox.DelCh(l1, y1, x1 - (addedSpaces + addedTabs), l1, y1, x1, false);
401 editBox.PutS(oldString);
404 editBox.GoToPosition(null, y1, x1);
407 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
413 { int c; editBox.selLine = editBox.lines.first; for(c = 0; c < editBox.selY && editBox.selLine; c++, editBox.selLine = editBox.selLine.next); }
417 void Redo(EditBox editBox)
422 editBox.GoToPosition(null, y1, x1);
424 l2 = editBox.lines.first;
425 for(c = 0; c < y2 && l2; c++, l2 = l2.next);
427 editBox.DelCh(l1, y1, x1, l2, y2, x2, true);
429 editBox.PutS(newString);
439 static class MoveTextAction : UndoAction
441 int fy1, fx1, fy2, fx2;
443 type = class(MoveTextAction);
445 void Undo(EditBox editBox)
450 void Redo(EditBox editBox)
456 public class EditLine : struct
468 // Only works for single line edit for now...
469 EditBox editBox = this.editBox;
473 get { return this ? buffer : null; }
475 property EditLine prev { get { return this ? prev : null; } };
476 property EditLine next { get { return this ? next : null; } };
477 property int count { get { return this ? count : 0; } };
484 // This makes sure the buffer always contains at least count characters
485 // Keeps a count/2 pad for avoiding always reallocating memory.
486 bool AdjustBuffer(int count)
494 newSize = (count + (count >> 1));
496 // Shrink down the buffer
499 buffer = new char[newSize];
500 if(!buffer) return false;
504 CopyBytes(buffer, this.buffer, count);
507 this.buffer = buffer;
511 // Increase the buffer
512 else if(size < count)
514 buffer = new char[newSize];
515 if(!buffer) return false;
519 CopyBytes(buffer, this.buffer, this.count + 1); // size);
522 this.buffer = buffer;
531 public struct BufferLocation
536 void AdjustDelete(BufferLocation start, BufferLocation end)
538 // Location is before, nothing to do
539 if(y < start.y || (y == start.y && x < start.x))
541 // Location is inside deleted bytes, point to the start
542 if((y >= start.y && (y > start.y || x >= start.x)) &&
543 (y >= end.y && (y > end.y || x >= end.x)))
548 // Location is on another line
550 y -= end.y - start.y;
551 // Location is the last touched line
558 //if(start.line == end.line)
559 x -= end.x - start.x;
568 // Assuming no carriage return before first character ???? fixed?
569 void AdjustAdd(BufferLocation start, BufferLocation end)
571 int numLines = end.y - start.y;
578 if(x > start.x || (x == start.x /*&& (numLines ? true : false)*/))
581 for(c = 0, line = start.line; c<numLines; c++)
584 //x += numLines ? end.x : (end.x - start.x);
585 x += end.x - start.x;
592 public enum EditBoxFindResult { notFound, found, wrapped };
594 static char * keyWords1[] =
597 "return","break","continue","default","switch","case","if","else","for","while", "do","long","short",
598 "void", "char","int","float","double","unsigned","static", "extern", "struct", "union", "typedef","enum",
600 "#include", "#define", "#pragma", "#if", "#else", "#elif", "#ifdef", "#ifndef", "#endif", "#undef", "#line",
601 "__attribute__", "__stdcall", "_stdcall",
602 "__declspec", "goto",
603 "inline", "__inline__", "_inline", "__inline", "__typeof","__extension__",
604 "asm", "__asm", "_asm", "volatile", "#cpu", "__stdcall__",
607 "class", "private", "public",
609 "delete", "new", "new0", "renew", "renew0", "define",
612 "dllexport", "dllimport", "stdcall",
613 "subclass", "__on_register_module", "namespace", "using",
614 "typed_object", "any_object", "incref", "register", "watch", "stopwatching", "firewatchers", "watchable", "class_designer",
615 "class_fixed", "class_no_expansion", "isset", "class_default_property", "property_category", "class_data", "class_property",
616 "virtual", "thisclass","unichar", "dbtable", "dbindex", "database_open", "dbfield",
619 "uint", "uint32", "uint16", "uint64", "bool", "byte", "int64",
622 "this", "true", "false", "null", "value",
631 static char * keyWords2[] =
633 "defined", "warning", null
636 static char ** keyWords[] = { keyWords1, keyWords2 };
637 #define NUM_KEYWORD_GROUPS (sizeof(keyWords) / sizeof(char **))
638 static Color colorGroups[] =
643 //static int * keyLen[NUM_KEYWORD_GROUPS];
644 static int keyLen[NUM_KEYWORD_GROUPS][sizeof(keyWords1)];
647 static FileFilter filters[] =
649 { "All files", null },
651 "Text Files (*.txt)",
655 static FileListConfig fileListConfig = { "", "", filters, sizeof(filters), null, 0 };
657 static char searchString[1025], replaceString[1025];
658 static bool matchCase = false, wholeWord = false, searchUp = false;
660 static GoToDialog goToDialog
662 autoCreate = false, isModal = true, text = "Go To"
665 public class EditBox : CommonControl
667 class_property(icon) = "<:ecere>controls/editBox.png";
671 virtual bool Window::NotifyModified(EditBox editBox);
672 virtual void Window::NotifyCaretMove(EditBox editBox, int line, int charPos);
673 virtual void Window::NotifyUpdate(EditBox editBox);
674 virtual bool Window::NotifyDoubleClick(EditBox editBox, EditLine line, Modifiers mods);
675 virtual void Window::NotifyOvrToggle(EditBox editBox, bool overwrite);
676 virtual bool Window::NotifyKeyDown(EditBox editBox, Key key, unichar ch);
678 virtual bool Window::NotifyCharsAdded(EditBox editBox, BufferLocation before, BufferLocation after, bool pasteOperation);
679 virtual bool Window::NotifyCharsDeleted(EditBox editBox, BufferLocation beforeLoc, BufferLocation after, bool pasteOperation);
680 virtual bool Window::NotifyDropped(EditBox editBox, int x, int y);
682 virtual bool Window::NotifyUnsetModified(EditBox editBox);
684 // Why was this commented out?
685 // It is required otherwise updating font property from property sheet doesn't immediately reflect in form designer,
686 // and the scrollArea property isn't compared properly either.
690 if(font) ComputeFont();
691 SetInitSize(initSize);
697 style.vScroll = true;
703 style.hScroll = true;
707 property bool textHorzScroll { property_category "Behavior" set { style.hScroll = value; } get { return style.hScroll; } }; // Should cut the text on set to false
708 property bool textVertScroll { property_category "Behavior" set { style.vScroll = value; } get { return style.vScroll; } };
709 property bool readOnly
711 property_category "Behavior"
714 style.readOnly = value;
715 itemEditCut.disabled = value || !selection;
716 itemEditDelete.disabled = value || !selection;
717 itemEditPaste.disabled = value;
719 get { return style.readOnly; }
721 property bool multiLine { property_category "Behavior" set { style.multiLine = value; } get { return style.multiLine; } };
722 property bool freeCaret { property_category "Behavior" set { style.freeCaret = value; } get { return style.freeCaret; } };
723 property bool tabKey { property_category "Behavior" set { style.tabKey = value; } get { return style.tabKey; } };
724 property int tabSize { property_category "Behavior" set { tabSize = value; } get { return tabSize; } };
725 property bool tabSelection { property_category "Behavior" set { style.tabSel = value; if(value) style.tabKey = true; } get { return style.tabSel; } };
726 property bool smartHome { property_category "Behavior" set { style.smartHome = value; } get { return style.smartHome; } };
727 property bool autoEmpty { property_category "Behavior" set { style.autoEmpty = value; } get { return style.autoEmpty; } };
728 property bool noCaret { property_category "Behavior" set { style.noCaret = value; if(value) { style.readOnly = true; style.stuckCaret = true; } } get { return style.noCaret; } };
729 property int maxLineSize { property_category "Behavior" set { maxLineSize = value; } get { return maxLineSize; } };
730 property int maxNumLines { property_category "Behavior" set { maxLines = value; } get { return maxLines; } };
731 property bool useTab { property_category "Behavior" set { style.useTab = value; itemEditInsertTab.checked = value; } get { return style.useTab; } };
732 property bool syntaxHighlighting { property_category "Appearance" set { style.syntax = value; } get { return style.syntax; } };
733 property bool noSelect { property_category "Behavior" set { style.noSelect = value; } get { return style.noSelect; } };
734 property bool allCaps { property_category "Behavior" set { style.allCaps = value; } get { return style.allCaps; } };
735 property bool wrap { set { style.wrap = value; Update(null); } get { return style.wrap; } };
736 //property bool lineNumbers { set { style.lineNumbers = value; } get { return style.lineNumbers; } };
737 property int numLines { get { return this ? lineCount : 0; } };
738 property int lineNumber { get { return y; } }; // TODO: Change to property of EditLine this.line.number
739 property int column { get { return col; } }; // TODO: Add Set
740 property int charPos { get { return x; } }; // TODO: Add Set
741 property EditLine firstLine { get { return lines.first; } }; // Change these to a List<EditLine>... (this.lines[10].text)
742 property EditLine lastLine { get { return lines.last; } };
743 property EditLine line { get { return this.line; } }; // TODO: Add Set this.line = this.lines[10]
744 property char * contents
746 property_category "Data"
752 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
755 //SetViewToCursor(true);
763 char * buffer = null;
766 /* Can't implement this right now because of memory leak... Need string reference counting...
773 for(line = lines.first; line; line = line.next)
774 len += strlen(line.buffer);
776 buffer = new char[len+1];
778 for(line = lines.first; line; line = line.next)
780 int lineLen = strlen(line.buffer);
781 memcpy(buffer + len, line.buffer, lineLen);
787 buffer = this.line ? this.line.buffer : null;
792 property bool overwrite { get { return overwrite; } };
793 property bool caretFollowsScrolling { get { return style.cursorFollowsView; } set { style.cursorFollowsView = value; } }
795 property char * multiLineContents
799 char * buffer = null;
806 for(line = lines.first; line; line = line.next)
807 len += strlen(line.buffer)+1;
809 buffer = new char[len+1];
811 for(line = lines.first; line; line = line.next)
813 int lineLen = strlen(line.buffer);
814 memcpy(buffer + len, line.buffer, lineLen);
816 if(line.next) buffer[len++] = '\n';
829 return this.line.buffer;
834 void SetLineText(char * text)
838 EditLine_SetText(this.line, text);
843 // selectionStart.line, selectionStart.column (With Set)
844 // selection.line1, selection.line2, selection.column1, selection.column2 (Read only)
858 // Position of Caret (Not necessarily displayed position)
861 // Position of beginning of block (Block ends at (x,y))
863 // line is line at carret, selLine is line at beginning of block
864 EditLine line, selLine, dropLine;
865 // Mouse selection Moving virtual caret
870 // ViewX is x offset in pixels, ViewY is y offset in lines
872 // viewLine is first displayed line
875 // start and end of area to redraw
878 // MaxLine is the longest line
880 // MaxLength is the longest line's length
883 // MouseSelect is true once button is pressed, overwrite is true if mode is on
884 bool mouseSelect, mouseMove, overwrite, wordSelect;
885 // Timer is used for mouse selection scrolling
888 window = this, delay = 0.1;
897 OnMouseMove(mouseX, mouseY, -1);
902 // (mouseX,mouseY) is the position of the mouse in the edit box
907 void (* FontExtent)(Display display, Font font, char * text, int len, int * width, int * height);
910 bool rightButtonDown;
913 UndoBuffer undoBuffer { data = this };
919 Menu editMenu { menu, "Edit", e };
922 editMenu, "Cut\tCtrl+X", t, disabled = true;
924 bool NotifySelect(MenuItem item, Modifiers mods)
926 if(!(style.readOnly))
931 MenuItem itemEditCopy
933 editMenu, "Copy\tCtrl+C", c, disabled = true;
935 bool NotifySelect(MenuItem item, Modifiers mods)
941 MenuItem itemEditPaste
943 editMenu, "Paste\tCtrl+V", p;
945 bool NotifySelect(MenuItem item, Modifiers mods)
947 if(!(style.readOnly))
952 MenuItem itemEditDelete
954 editMenu, "Delete\tDel", d, disabled = true;
956 bool NotifySelect(MenuItem item, Modifiers mods)
958 if(!(style.readOnly))
963 MenuDivider { editMenu };
964 MenuItem itemEditSelectAll
966 editMenu, "Select All\tCtrl+A", a;
968 bool NotifySelect(MenuItem item, Modifiers mods)
974 MenuDivider { editMenu };
975 MenuItem itemEditUndo
977 editMenu, "Undo\tCtrl+Z", u;
980 bool NotifySelect(MenuItem item, Modifiers mods)
986 MenuItem itemEditRedo
988 editMenu, "Redo\tCtrl+Y", o;
991 bool NotifySelect(MenuItem item, Modifiers mods)
997 MenuDivider { editMenu };
1000 editMenu, "Find Previous\tShift-F3", e, shiftF3;
1002 bool NotifySelect(MenuItem item, Modifiers mods)
1005 Find(searchString, wholeWord, matchCase, false);
1007 itemEditFind.NotifySelect(this, item, mods);
1013 editMenu, "Find Next\tF3", n, f3;
1015 bool NotifySelect(MenuItem item, Modifiers mods)
1018 Find(searchString, wholeWord, matchCase, true);
1020 itemEditFind.NotifySelect(this, item, mods);
1024 MenuItem itemEditFind
1026 editMenu, "Find...\tCtrl+F", f, ctrlF;
1028 bool NotifySelect(MenuItem item, Modifiers mods)
1032 editBox = this, master = master, isModal = true, searchString = searchString, matchCase = matchCase, wholeWord = wholeWord,
1033 searchUp = searchUp;
1036 // Fix dialog from above shouldn't be visible
1037 // void NotifyDestroyed(FindDialog dialog, DialogResult result)
1038 void NotifyDestroyed(Window window, DialogResult result)
1040 FindDialog dialog = (FindDialog) window;
1041 searchUp = dialog.searchUp;
1042 strcpy(searchString, dialog.searchString);
1043 matchCase = dialog.matchCase;
1044 wholeWord = dialog.wholeWord;
1053 editMenu, "Replace...\tCtrl+R", r, ctrlR;
1055 bool NotifySelect(MenuItem item, Modifiers mods)
1057 ReplaceDialog dialog
1061 searchString = searchString,
1062 replaceString = replaceString,
1063 matchCase = matchCase,
1064 wholeWord = wholeWord,
1068 // void NotifyDestroyed(ReplaceDialog dialog, DialogResult result)
1069 void NotifyDestroyed(Window window, DialogResult result)
1071 ReplaceDialog dialog = (ReplaceDialog)window;
1072 char * replace = dialog.replaceString;
1074 strcpy(replaceString, replace);
1075 strcpy(searchString, dialog.searchString);
1076 matchCase = dialog.matchCase;
1077 wholeWord = dialog.wholeWord;
1084 MenuDivider { editMenu };
1087 editMenu, "Go To...\tCtrl+G", g, ctrlG;
1089 bool NotifySelect(MenuItem item, Modifiers mods)
1091 goToDialog.editBox = this;
1092 goToDialog.master = master;
1093 goToDialog.Create();
1097 MenuDivider { editMenu };
1098 MenuItem itemEditInsertTab
1100 editMenu, "Insert Tabs", i, checkable = true;
1102 bool NotifySelect(MenuItem item, Modifiers mods)
1104 style.useTab = item.checked;
1110 snapVertScroll = true;
1111 snapHorzScroll = true;
1115 static bool syntaxInit = false;
1120 for(g = 0; g<NUM_KEYWORD_GROUPS; g++)
1122 for(c = 0; keyWords[g][c]; c++);
1123 //keyLen[g] = new int[c];
1124 for(c = 0; keyWords[g][c]; c++)
1126 keyLen[g][c] = strlen(keyWords[g][c]);
1131 FontExtent = Display::FontExtent;
1133 lines.offset = (uint)&((EditLine)0).prev;
1135 style = EditBoxBits { hScroll = true };
1137 // cursor = guiApp.GetCursor(IBeam);
1139 // Default Properties
1141 maxLineSize = 1024;*/
1144 maxLineSize = MAXINT;
1149 mouseSelect = this.mouseMove = false;
1152 x = selX = selY = 0;
1155 line = selLine = null;
1161 endY = clientSize.h;
1164 // Default space to 8 in case window is not constructed
1167 undoBuffer.dontRecord++;
1169 undoBuffer.dontRecord--;
1171 viewLine = lines.first;
1172 style.recomputeSyntax = true;
1178 UpdateCaretPosition(true);
1184 lines.Free(EditLine::Free);
1187 void FlushBuffer(Surface surface, EditLine line, int wc, int * renderStart, int * x, int y, int numSpaces, Box box)
1189 int count = wc - *renderStart;
1192 if(y + space.h >= box.top && y <= box.bottom)
1198 //FontExtent(display, font, line.buffer + *renderStart, count, &w, null);
1199 surface.TextFont(font);
1200 surface.TextExtent(line.buffer + *renderStart, count, &w, null);
1201 if(*x + w + XOFFSET > 0)
1202 surface.WriteText(XOFFSET + *x,y, line.buffer + *renderStart, count);
1207 w = numSpaces; // * space.w;
1208 if(*x + w + XOFFSET > 0 && surface.GetTextOpacity())
1209 surface.Area(XOFFSET + *x - 1, y, XOFFSET + *x + w, y + space.h-1);
1210 // WHATS UP WITH THIS... surface.Area(XOFFSET + *x, y, XOFFSET + *x + w, y + space.h-1);
1218 int CheckColors(EditLine line, int wc, bool selection, int selX, int editX, bool *selected,
1219 Color selectionForeground, Color selectionBackground, Color textColor, Color *foreground, Color *background, bool *opacity, bool *overwrite)
1225 if((wc == selX && line == selLine) || (wc == editX && line == this.line))
1229 *foreground = (*selected) ? selectionForeground : textColor;
1230 *background = selectionBackground;
1231 *opacity = *selected;
1238 if(this.overwrite && active)
1240 if((style.stuckCaret && wc == line.count && !line.next) ||
1241 (!mouseMove && line == this.line && wc == editX))
1250 void FigureStartSyntaxStates(EditLine firstLine, bool reset)
1254 bool inMultiLineComment = reset ? false : style.inMultiLineComment;
1255 bool inString = false;
1256 bool inQuotes = false;
1257 bool inPrep = reset ? false : style.inPrep;
1258 bool inSingleLineComment = false;
1259 bool escaped = reset ? false : style.escaped;
1260 bool continuedSingleLineComment = false;
1262 EditLine line = reset ? lines.first : firstLine;
1263 // int maxBackUp = 1000, c;
1264 // for(c = 0, line = viewLine; c < maxBackUp && line && line.prev; line = line.prev);
1265 for(; line != viewLine; line = line.next)
1267 char * text = line.buffer;
1270 bool lastWasStar = false;
1271 bool firstWord = true;
1272 if(!escaped) inPrep = false;
1273 inSingleLineComment = continuedSingleLineComment;
1279 for(c = 0; (ch = text[c]); c++)
1281 bool wasEscaped = escaped;
1282 bool backLastWasStar = lastWasStar;
1284 lastWasStar = false;
1287 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1289 if(text[c+1] == '/')
1291 inSingleLineComment = true;
1293 else if(text[c+1] == '*')
1295 inMultiLineComment = true;
1298 else if(backLastWasStar)
1299 inMultiLineComment = false;
1303 if(!c || text[c-1] != '/') lastWasStar = true;
1305 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && ch == '\"')
1307 if(inString && !wasEscaped)
1316 else if(!inSingleLineComment && !inMultiLineComment && !inString && ch == '\'')
1318 if(inQuotes && !wasEscaped)
1332 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && ch == '#')
1344 continuedSingleLineComment = false;
1345 if(line.count && line.text[line.count - 1] == '\\')
1346 continuedSingleLineComment = true;
1348 style.inMultiLineComment = inMultiLineComment;
1349 style.inPrep = inPrep;
1350 style.escaped = escaped;
1354 /*void OnDrawOverChildren(Surface surface)
1356 if(style.lineNumbers)
1358 int currentLineNumber = this.viewY + 1;
1361 for(i = 0; i * space.h < box.height; i++)
1363 // ********* LINE NUMBERING *********
1364 surface.SetForeground(Color{60, 60, 60});
1365 //Highlight current line
1366 if(this.caretY / space.h == currentLineNumber - 1)
1367 surface.SetBackground(Color{220, 220, 220});
1369 surface.SetBackground(Color{230, 230, 230});
1370 surface.textOpacity = true;
1372 sprintf(lineText,"%5u ", currentLineNumber % 100000);
1373 if(currentLineNumber > this.lineCount)
1374 surface.WriteText(0,i*space.h+1," ",6);
1376 surface.WriteText(0,i*space.h+1,lineText,6);
1378 currentLineNumber++;
1383 void OnRedraw(Surface surface)
1387 bool selected = false, selection = true;
1389 Color selectionBackground = SELECTION_COLOR, selectionForeground = white;
1390 Color defaultTextColor = property::foreground;
1393 int maxW = clientSize.w;
1395 Color foreground, background;
1398 // Overwrite Caret Stuff
1399 bool overWrite = false;
1400 int overWriteX, overWriteY;
1403 // ****** SYNTAX STATES ******
1404 bool inMultiLineComment = style.inMultiLineComment;
1405 bool inString = false;
1406 bool inQuotes = false;
1407 bool inPrep = style.inPrep;
1408 bool inSingleLineComment = false;
1409 bool escaped = style.escaped;
1410 bool continuedSingleLineComment = false;
1411 // ****** ************* ******
1414 defaultTextColor = Color { 85, 85, 85 };
1415 textColor = defaultTextColor;
1418 Abs(selectionBackground.r - property::background.r) +
1419 Abs(selectionBackground.g - property::background.g) +
1420 Abs(selectionBackground.b - property::background.b) < 92)
1422 selectionBackground = activeBorder;
1423 selectionForeground = SELECTION_COLOR;
1426 surface.TextFont(this.font);
1428 surface.SetBackground(GDefaultPalette()[RND_Get(1, 15)]);
1429 surface.Area(0,0,MAXINT,MAXINT);
1434 surface.SetBackground(activeBorder);
1435 surface.Area(0,0,clientSize.w, clientSize.h);
1438 if(this.selX == this.x && this.selY == this.y)
1448 editX = Min(this.x,this.line.count);
1449 selX = Min(this.selX,this.selLine.count);
1452 selected = (this.selY < this.viewY) ^ (this.y < this.viewY);
1454 foreground = selected ? selectionForeground : textColor;
1455 background = selectionBackground;
1461 surface.SetForeground(foreground);
1462 surface.SetBackground(background);
1463 surface.TextOpacity(opacity);
1465 surface.GetBox(box);
1467 for(line = this.viewLine; line; line = line.next)
1469 int x = -this.viewX;
1473 Color newTextColor = textColor = defaultTextColor;
1474 bool lineComplete = false;
1477 // ****** SYNTAX HIGHLIGHTING ******
1478 bool lastWasStar = false;
1479 bool firstWord = true;
1480 if(!escaped) inPrep = false;
1481 inSingleLineComment = continuedSingleLineComment;
1485 // *********************************
1487 /* === DEBUGGING TOOL FOR MAXLINE ===
1489 if(line == this.maxLine)
1491 surface.SetBackground(GREEN|0xFF000000);
1492 surface.TextOpacity(true);
1496 surface.TextOpacity(selected ? true : false);
1497 surface.SetBackground(selected ? SELECTION_COLOR|0xFF000000 : BLACK|0xFF000000);
1501 if(line == this.selLine && line == this.line)
1503 end = Max(line.count, this.x);
1504 end = Max(end, this.selX);
1506 else if(line == this.selLine)
1507 end = Max(line.count, this.selX);
1508 else if(line == this.line)
1509 end = Max(line.count, this.x);
1517 bool spacing = true;
1518 bool cantHaveWords = false;
1525 lineComplete = false;
1528 textColor = newTextColor;
1530 surface.SetForeground(textColor);
1533 for(; c<end && !cantHaveWords;)
1537 bufferLen += wordLen;
1541 /*if(line.count > 4000)
1548 for(; c<line.count; c++)
1550 unichar ch = line.buffer[c];
1551 unichar bf = (wordLen == 1) ? line.buffer[c-1] : 0;
1552 //if(ch == ' ' || ch == '\t' || (wordLen && (ch == '(' || ch == ')' || ch == ';' || ch == ':')) || (wordLen == 1 && line.buffer[c-1] == '('))
1553 if(CharMatchCategories(ch, separators) || /*ch == ' ' ||*/ ch == '\t' ||
1554 (wordLen && !CharMatchCategories(ch, numbers|letters|marks|connector) && ch != '#' /*&& ch != '_'*/) ||
1555 (bf && !CharMatchCategories(bf, numbers|letters|separators|marks|connector) && bf != '#' && bf != '\t' /*&& bf != '_' && bf != ' '*/))
1563 for(; c<line.count; c++)
1565 unichar ch = line.buffer[c];
1566 if(ch == '\t' || ch == ' ')
1568 cantHaveWords = true;
1572 if(ch != ' ' && ch != '\t')
1577 if(c == line.count && c < end)
1586 cantHaveWords = true;
1591 lastWasStar = false;
1597 bool backEscaped = escaped;
1598 bool backLastWasStar = lastWasStar;
1599 bool backInMultiLineComment = inMultiLineComment;
1600 bool backInString = inString;
1601 bool backInQuotes = inQuotes;
1602 bool backInPrep = inPrep;
1603 bool backInSingleLineComment = inSingleLineComment;
1605 char * word = line.buffer + c - wordLen;
1607 bool wasEscaped = escaped;
1609 lastWasStar = false;
1611 // Determine Syntax Highlighting
1612 newTextColor = defaultTextColor;
1615 if(inSingleLineComment || inMultiLineComment)
1617 newTextColor = dimGray;
1621 newTextColor = crimson;
1625 newTextColor = crimson;
1629 newTextColor = green;
1631 if(wordLen == 1 && word[0] == '/')
1633 if(!inSingleLineComment && !inMultiLineComment && !inQuotes && !inString)
1637 inSingleLineComment = true;
1638 newTextColor = dimGray;
1640 else if(word[1] == '*')
1642 inMultiLineComment = true;
1643 newTextColor = dimGray;
1646 else if(backLastWasStar)
1647 inMultiLineComment = false;
1649 else if(wordLen == 1 && word[0] == '*')
1651 if(!c || word[-1] != '/')
1654 else if(!inSingleLineComment && !inMultiLineComment && !inQuotes && wordLen == 1 && word[0] == '\"')
1656 if(inString && !wasEscaped)
1663 newTextColor = crimson;
1666 else if(!inSingleLineComment && !inMultiLineComment && !inString && wordLen == 1 && word[0] == '\'')
1668 if(inQuotes && !wasEscaped)
1673 newTextColor = crimson;
1676 else if(wordLen == 1 && word[0] == '\\')
1681 else if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment &&
1682 ( ( isdigit(word[0]) /*&& (!c || word[-1] == ' ' || word[-1] == '\t')*/ ) || (word[0] == '.' && isdigit(word[1]))))
1684 newTextColor = teal;
1688 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment && word[0] == '#')
1693 newTextColor = green;
1696 if(!inQuotes && !inString && !inMultiLineComment && !inSingleLineComment)
1698 for(g = 0; g < ((inPrep && word[0] != '#') ? 2 : 1); g++)
1700 char ** keys = keyWords[g];
1701 int * len = keyLen[g];
1702 for(ccc = 0; keys[ccc]; ccc++)
1704 if(len[ccc] == wordLen && !strncmp(keys[ccc], word, wordLen))
1706 newTextColor = colorGroups[g];
1715 // If highlighting for this word is different, break
1716 if(newTextColor != textColor)
1720 // Better solution than going back?
1723 // Reset syntax flags
1724 escaped = backEscaped;
1725 lastWasStar = backLastWasStar;
1726 inMultiLineComment = backInMultiLineComment;
1727 inString = backInString;
1728 inQuotes = backInQuotes;
1729 inPrep = backInPrep;
1730 inSingleLineComment = backInSingleLineComment;
1735 textColor = newTextColor;
1737 surface.SetForeground(textColor);
1743 // If we're not breaking, this can't be rendered as spacing anymore
1746 // Do word wrapping here
1752 FontExtent(display, font, line.buffer + start, bufferLen + wordLen, &tw, null);
1757 w += numSpaces * space.w;
1759 if(x + viewX + w > maxW)
1762 lineComplete = true;
1767 bufferLen += wordLen;
1772 int renderStart = start;
1777 // Render checking if we need to split because of selection or to find where to draw insert caret
1778 for(wc = start; wc < start + bufferLen; wc++)
1780 flush = CheckColors(line, wc, selection, selX, editX, &selected, selectionForeground,
1781 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite);
1782 if(overWrite == true)
1784 overWriteCh = (wc < line.count) ? line.buffer[wc] : ' ';
1785 if(overWriteCh == '\t') overWriteCh = ' ';
1790 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1791 if(overWrite == true)
1799 surface.TextOpacity(opacity);
1800 surface.SetBackground(background);
1801 surface.SetForeground(foreground);
1808 if(wc < line.count && line.buffer[wc] == '\t')
1810 numSpaces += (tabSize * space.w) - ((x + numSpaces + viewX) % (tabSize * space.w));
1814 numSpaces += space.w;
1818 FlushBuffer(surface, line, wc, &renderStart, &x, y, numSpaces, box);
1823 if(CheckColors(line, c, selection, selX, editX, &selected, selectionForeground,
1824 selectionBackground, textColor, &foreground, &background, &opacity, &overWrite))
1826 if(overWrite == true)
1833 surface.TextOpacity(opacity);
1834 surface.SetBackground(background);
1835 surface.SetForeground(foreground);
1838 if(style.freeCaret && selected)
1840 surface.SetBackground(selectionBackground);
1841 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1842 // TEST: surface.Area(x + XOFFSET,y,clientSize.w-1,y+this.space.h-1);
1847 if(style.freeCaret && selected)
1849 surface.SetBackground(selectionBackground);
1850 surface.Area(x + XOFFSET - 1,y,clientSize.w-1,y+this.space.h-1);
1854 continuedSingleLineComment = false;
1855 if(line.count && line.text[line.count - 1] == '\\')
1856 continuedSingleLineComment = true;
1859 if(y > box.bottom) // >=clientSize.h)
1865 surface.TextOpacity(true);
1866 surface.SetForeground(black);
1867 surface.SetBackground(Color {255,255,85});
1868 surface.WriteText(XOFFSET + overWriteX,overWriteY, &overWriteCh, 1);
1872 void FixScrollArea()
1874 if(style.hScroll || style.vScroll)
1876 int width = maxLength + XOFFSET;
1877 int height = lineCount * space.h;
1878 if(style.freeCaret && line)
1883 width = Max(line.length + (x - line.count) * space.w, maxLength);
1887 if(selX > selLine.count)
1888 width = Max(selLine.length + (selX - selLine.count) * space.w, maxLength);
1893 SetScrollLineStep(8, space.h);
1894 SetScrollArea(width, height, true);
1898 void ComputeLength(EditLine line)
1905 for(c = 0; c < line.count; )
1914 for(len = 0; c < line.count; c += numBytes)
1916 ch = line.buffer[c];
1917 numBytes = UTF8_NUM_BYTES(ch);
1919 if(ch == ' ' || ch == '\t')
1926 if(!len && ch == ' ')
1931 else if(!len && ch == '\t')
1933 w = (tabSize * space.w) - (x % (tabSize * space.w));
1937 FontExtent(display, font, line.buffer + start, len, &w, null);
1948 if(line.length > this.maxLength)
1950 this.maxLine = line;
1951 this.maxLength = line.length;
1960 this.maxLine = null;
1962 for(line = lines.first; line; line = line.next)
1964 if(line.length > this.maxLength)
1966 this.maxLength = line.length;
1967 this.maxLine = line;
1974 if(this.selY != this.y)
1976 else if(this.selX != this.x) // commented out to erase caret: if(this.selX != this.x)
1980 void ComputeColumn()
1982 // Adjust new edit X position according to tabs
1983 int c, position = 0;
1986 for(c = 0; c<this.line.count && c<this.x; c+= nb)
1988 ch = UTF8_GET_CHAR(this.line.buffer + c, nb);
1989 // TODO: MIGHT WANT TO RETHINK WHAT COLUMN SHOULD BE REGARDING TABS
1991 position += this.tabSize - (position % this.tabSize);
1995 position += this.x - c;
1996 this.col = position;
1999 int DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter)
2001 return _DelCh(l1, y1, c1, l2, y2, c2, placeAfter, null);
2004 bool HasCommentOrEscape(EditLine line)
2006 bool hadComment = strstr(line.buffer, "/*") || strstr(line.buffer, "*/");
2011 for(c = line.count-1; c >= 0; c--)
2013 char ch = line.buffer[c];
2019 else //if(ch != ' ' && ch != '\t')
2026 int _DelCh(EditLine l1, int y1, int c1, EditLine l2, int y2, int c2, bool placeAfter, int * addedSpacesPtr)
2028 EditLine line = l1, next;
2030 int oldCount1, oldCount2;
2031 int y, firstViewY, firstY, firstDropY, firstSelY;
2034 DelTextAction action = null;
2035 bool hadComment = false;
2039 hadComment = HasCommentOrEscape(line);
2041 if(y2 > y1 || c2 > c1)
2043 if(start < l1.count)
2045 while(!UTF8_IS_FIRST(l1.buffer[start]) && start)
2050 while(c1 < oldCount1)
2052 byte ch = buffer[c1];
2053 if(UTF8_IS_FIRST(ch)) break;
2058 while(c2 < oldCount2)
2060 byte ch = buffer[c2];
2061 if(UTF8_IS_FIRST(ch)) break;
2066 if(!undoBuffer.dontRecord && (y2 > y1 || c2 > c1))
2071 len = GetText(null, l1, y1, start, l2, y2, c2, false, false);
2072 string = new char[len];
2073 action = DelTextAction { y1 = y1, x1 = start, y2 = y2, x2 = c2, string = string, placeAfter = placeAfter };
2074 GetText(string, l1, y1, start, l2, y2, c2, false, false);
2078 oldCount1 = l1.count;
2079 oldCount2 = l2.count;
2082 BufferLocation before = { l1,y1,c1}, after = { l2,y2,c2 };
2083 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
2086 if(c2 > oldCount2) c2 = oldCount2;
2087 if(!(style.freeCaret))
2088 if(c1 > oldCount1) c1 = oldCount1;
2089 newLineCount = c1 + l2.count-c2;
2095 buffer = new char[line.size ? line.size : 1];
2097 buffer = new char[line.size];
2099 CopyBytes(buffer,l2.buffer,oldCount1 + 1/*line.count + 1*//*line.size*/);
2104 if(!line.AdjustBuffer(newLineCount))
2108 /*if(newLineCount > 4000 || newLineCount < 0)
2109 printf("Warning");*/
2111 line.count = newLineCount;
2113 memmove(l1.buffer+c1, buffer+c2,line.count-c1);
2118 action.addedSpaces = c1-oldCount1;
2120 if(action.addedSpaces > action.x1)
2126 if(addedSpacesPtr) *addedSpacesPtr = c1-oldCount1;
2127 FillBytes(l1.buffer+oldCount1,' ',c1-oldCount1);
2129 line.buffer[line.count] = '\0';
2136 this.dropX -= c2-c1;
2137 this.dropX = Max(this.dropX,0);
2139 this.x = Max(this.x,0);
2146 firstViewY = this.viewY;
2148 firstDropY = this.dropY;
2149 firstSelY = this.selY;
2150 for(line = l1;line;line = next, y++)
2158 if(line == this.viewLine)
2160 if(this.viewLine.next)
2162 this.viewLine = this.viewLine.next;
2164 style.recomputeSyntax = true;
2168 this.viewLine = this.viewLine.prev;
2170 style.recomputeSyntax = true;
2173 else if(y < firstViewY)
2175 if(line == this.line)
2179 this.line = this.line.next;
2180 this.x = this.line.count;
2185 this.line = this.line.prev;
2186 this.x = this.line.count;
2193 if(line == this.dropLine)
2195 if(this.dropLine.next)
2197 this.dropLine = this.dropLine.next;
2198 this.dropX = this.dropLine.count;
2202 this.dropLine = this.dropLine.prev;
2203 this.dropX = this.dropLine.count;
2207 else if(y < firstDropY)
2209 if(line == this.selLine)
2211 if(this.selLine.next)
2213 this.selLine = this.selLine.next;
2214 this.selX = this.selLine.count;
2218 this.selLine = this.selLine.prev;
2219 this.selX = this.selLine.count;
2223 else if(y < firstSelY)
2227 if(line == l2) break;
2231 if(style.syntax && (hadComment || HasCommentOrEscape(this.line)))
2234 style.recomputeSyntax = true;
2239 bool DelSel(int * addedSpacesPtr)
2241 if(this.line != this.selLine || this.x != this.selX)
2243 if(this.selY < this.y)
2245 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2248 this.line = this.selLine;
2250 else if(this.selY > this.y)
2252 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2255 this.selLine = this.line;
2257 else if(this.selX < this.x)
2259 _DelCh(this.selLine, this.selY, this.selX, this.line, this.y, this.x, true, addedSpacesPtr);
2262 this.line = this.selLine;
2266 _DelCh(this.line, this.y, this.x, this.selLine, this.selY, this.selX, false, addedSpacesPtr);
2269 this.selLine = this.line;
2277 bool AddToLine(char * stringLine, int count, bool LFComing, int * addedSpacesPtr, int * addedTabsPtr)
2279 bool hadComment = false;
2280 // Add the line here
2281 EditLine line = this.line;
2283 // The purpose of this is solely to lock a max number of characters if no HSCROLLING is present
2284 if(!style.hScroll && created)
2286 int endX = (style.freeCaret) ? this.x : Min(this.x, line.count);
2291 // Lock if no place to display.
2294 else if(endX > this.x)
2295 max = Max(this.x, line.count);
2299 for(x = 0, c = 0; c < max+count; )
2304 if(c < Min(this.x, line.count))
2305 string = line.buffer + c;
2308 else if(c < endX + count)
2309 string = stringLine + c - endX;
2311 string = line.buffer + c - endX - count;
2315 w = (tabSize * space.w) - (x % (tabSize * space.w));
2319 numBytes = UTF8_NUM_BYTES(*string);
2320 FontExtent(display, this.font, string, numBytes, &w, null);
2324 if(x >= clientSize.w)
2329 c += numBytes; // - 1;
2335 int addedSpaces = 0;
2338 // Add blank spaces if EES_FREECARET
2339 if(this.x > line.count)
2349 for(c = 0; c<line.count; c++)
2351 if(this.line.buffer[c] == '\t')
2352 position += this.tabSize - (position % this.tabSize);
2356 wantedPosition = position + (this.x - line.count);
2358 // A tab is too much...
2359 if(position + (this.tabSize - (position % this.tabSize)) > wantedPosition)
2360 addedSpaces = wantedPosition - position;
2365 position += this.tabSize - (position % this.tabSize);
2366 // Add as many tabs as needed
2367 addedTabs += (wantedPosition - position) / this.tabSize;
2368 position += (addedTabs-1) * this.tabSize;
2369 // Finish off with spaces
2370 addedSpaces = wantedPosition - position;
2374 addedSpaces = this.x - line.count;
2377 this.x = Min(this.x, line.count);
2379 if(line.count + count + addedSpaces + addedTabs > this.maxLineSize)
2385 hadComment = HasCommentOrEscape(line);
2387 // Adjust the size of the line
2388 if(!line.AdjustBuffer(line.count+count+addedTabs+addedSpaces))
2392 BufferLocation before = { this.line, this.y, this.x }, after = { this.line, this.y, this.x };
2395 memmove(line.buffer+this.x+count, line.buffer+this.x,line.count-this.x);
2396 CopyBytes(line.buffer + this.x + addedTabs + addedSpaces, stringLine, count);
2399 *addedTabsPtr = addedTabs;
2400 FillBytes(line.buffer+line.count,'\t',addedTabs);
2402 if(addedTabs > 4000 || addedTabs < 0)
2405 line.count += addedTabs;
2411 FillBytes(line.buffer+line.count,' ',addedSpaces);
2413 if(addedSpaces > 4000 || addedSpaces < 0)
2416 line.count += addedSpaces;
2417 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
2419 else if(addedSpacesPtr)
2420 *addedSpacesPtr = 0;
2422 if(count > 4000 || count < 0)
2425 line.count += count;
2426 this.x += count + addedTabs + addedSpaces;
2429 this.selLine = this.line;
2431 line.buffer[line.count] = '\0';
2433 ComputeLength(line);
2438 hasComment = HasCommentOrEscape(line);
2439 if(!undoBuffer.insideRedo)
2441 int backDontRecord = undoBuffer.dontRecord;
2442 undoBuffer.dontRecord = 0;
2443 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
2444 undoBuffer.dontRecord = backDontRecord;
2446 if(style.syntax && (hadComment || hasComment || line != this.line))
2448 style.recomputeSyntax = true;
2456 void Emptyline(EditLine line, int y)
2459 DelCh(line, y, 0, line.next, y+1, 0, true);
2461 DelCh(line, y, 0, line, y, line.count, true);
2464 void GoToEnd(bool deselect)
2468 this.line = this.lines.last;
2469 if(this.y != this.lineCount-1)
2471 else if (this.x != this.line.count)
2472 DirtyLine(this.lineCount-1);
2473 this.y = this.lineCount-1;
2474 this.x = this.line.count;
2481 void GoToHome(bool deselect)
2485 this.line = this.lines.first;
2488 else if (this.x !=0)
2489 DirtyLine(this.lineCount-1);
2498 // Returns true if it needs scrolling
2499 bool FindMouse(int px, int py, int * tx, int * ty, EditLine * tline, bool half)
2505 bool needHScroll = false;
2512 line = this.viewLine ? (void *)this.viewLine.prev : null;
2517 line = (void *)this.lines.first;
2522 py = Min(py, clientSize.h);
2524 py = Min(py, this.lineCount);
2526 for(c = 0, line = this.viewLine; (line != (void *)this.lines.last && c<py); line = line.next, c++)
2532 if( (px >= clientSize.w || px < clientSize.w/2) && this.viewX)
2535 px = Min(px,clientSize.w+this.space.w);
2539 *tx = AdjustXPosition(line, px + viewX, half, null, MAXINT, 0);
2542 if(tline) *tline = line;
2545 // Prevent divide by 0 from non valid this.font
2547 return (y < this.viewY) || needHScroll;
2549 return (y < this.viewY || y >= this.viewY + clientSize.h / this.space.h) || needHScroll;
2553 // Minimal Update Management Functions
2557 this.endY = clientSize.h-1;
2558 // ErrorLog("DirtyAll\n");
2561 void DirtyEnd(int y)
2563 if((y - this.viewY)*this.space.h < this.startY)
2564 this.startY = (y - this.viewY)*this.space.h+ YOFFSET;
2565 this.endY = clientSize.h-1;
2566 //ErrorLog("DirtyEnd %d\n", y);
2569 void DirtyLine(int y)
2573 if((y - this.viewY)*this.space.h < this.startY)
2574 this.startY = (y - this.viewY)*this.space.h + YOFFSET;
2575 if((y - this.viewY+1)*this.space.h > this.endY)
2576 this.endY = (y - this.viewY+1)*this.space.h-1 + YOFFSET;
2578 //ErrorLog("DirtyLine %d\n", y);
2585 if(style.recomputeSyntax)
2587 FigureStartSyntaxStates(lines.first, true);
2588 style.recomputeSyntax = false;
2591 if(this.startY > this.endY) return;
2592 if(this.startY <= 0 && this.endY >= clientSize.h-1)
2598 box.right = clientSize.w-1;
2599 box.top = this.startY;
2600 box.bottom = this.endY;
2603 this.startY = clientSize.h;
2607 bool IsMouseOnSelection()
2609 bool mouseOnSelection = false;
2612 int minY = Min(this.selY, this.y);
2613 int maxY = Max(this.selY, this.y);
2614 int minX = Min(this.selX, this.x);
2615 int maxX = Max(this.selX, this.x);
2617 FindMouse(this.mouseX - this.space.w / 2, this.mouseY, &x, &y, null, false);
2619 if(maxX != minX || maxY != minY)
2621 if(y > minY && y < maxY)
2622 mouseOnSelection = true;
2623 else if(y == minY && y == maxY)
2624 mouseOnSelection = (x < maxX && x >= minX);
2628 mouseOnSelection = (x >= this.selX);
2629 else if(y == this.y)
2630 mouseOnSelection = (x >= this.x);
2635 mouseOnSelection = (x < this.selX);
2636 else if(y == this.y)
2637 mouseOnSelection = (x < this.x);
2640 return mouseOnSelection;
2643 void UpdateCaretPosition(bool setCaret)
2647 if(mouseMove || (!overwrite && !style.noCaret))
2649 int max = this.mouseMove ? this.dropX : this.x;
2650 int y = this.mouseMove ? this.dropY : this.y;
2651 EditLine line = this.mouseMove ? this.dropLine : this.line;
2653 if(!(style.freeCaret))
2654 max = Min(max, line.count);
2656 if(FontExtent && display)
2658 for(c = 0; c < max; )
2667 for(len = 0; c < Min(max, line.count); c += numBytes)
2669 ch = line.buffer[c];
2670 numBytes = UTF8_NUM_BYTES(ch);
2672 if(ch == ' ' || ch == '\t')
2679 if(!len && ch == ' ')
2684 else if(!len && ch == '\t')
2686 w = (tabSize * space.w) - (x % (tabSize * space.w));
2690 FontExtent(display, this.font, line.buffer + start, len, &w, null);
2702 caretY = y * this.space.h;
2703 SetCaret(x + XOFFSET-2, y * space.h + YOFFSET, space.h);
2708 NotifyCaretMove(master, this, y + 1, x + 1);
2714 void SelectionEnables()
2716 if((x != selX || y != selY) && !selection)
2720 itemEditCut.disabled = false;
2721 itemEditDelete.disabled = false;
2723 itemEditCopy.disabled = false;
2725 this.selection = true;
2727 else if((x == selX && y == selY) && selection)
2729 itemEditCut.disabled = true;
2730 itemEditCopy.disabled = true;
2731 itemEditDelete.disabled = true;
2733 this.selection = false;
2737 void SetSelectCursor()
2739 if(!inactive || !style.noSelect)
2742 cursor = guiApp.GetCursor(arrow);
2743 else if(this.mouseSelect)
2744 cursor = guiApp.GetCursor(iBeam);
2747 if(IsMouseOnSelection())
2748 cursor = guiApp.GetCursor(arrow);
2750 cursor = guiApp.GetCursor(iBeam);
2755 int AdjustXPosition(EditLine line, int position, bool half, int * px, int max, int sc)
2758 int x = px ? *px : 0;
2765 if(c < Min(max, line.count))
2769 for(len = 0; c < Min(max, line.count); c += numBytes)
2771 ch = line.buffer[c];
2772 numBytes = UTF8_NUM_BYTES(ch);
2774 if(ch == ' ' || ch == '\t')
2781 if(!len && ch == ' ')
2786 else if(!len && ch == '\t')
2788 w = (tabSize * space.w) - (x % (tabSize * space.w));
2792 FontExtent(display, font, line.buffer + start, len, &w, null);
2796 if(style.freeCaret && c < max)
2805 if(x + (((half && len == 1) ? (w / 2) : w)) >= position)
2810 int a = start + len;
2813 while(a > 0 && !UTF8_IS_FIRST(line.buffer[--a]));
2817 FontExtent(display, font, line.buffer + start, a - start, &w, null);
2820 if(position > x + (half ? ((w + lastW) / 2) : lastW)) break;
2824 return Min(this.maxLineSize - 1, start + len);
2830 void SetCursorToViewX()
2832 bool selecting = this.x != selX || y != selY;
2834 // Horizontal Adjustment
2836 int c = AdjustXPosition(line, viewX, false, &x, MAXINT, 0);
2841 c = AdjustXPosition(line, viewX + clientSize.w - 1, false, &x, MAXINT, c);
2853 UpdateCaretPosition(false);
2859 void SetCursorToViewY()
2862 EditLine oldLine = this.line;
2864 bool selecting = this.x != this.selX || this.y != this.selY;
2866 numLines = clientSize.h / this.space.h;
2868 // Vertical Adjustment
2869 if(this.viewY > this.y)
2871 this.y = this.viewY;
2872 this.line = this.viewLine;
2875 if(this.viewY + numLines <= this.y)
2879 this.y = this.viewY-1;
2880 for(c = 0, line = this.viewLine; line && c<numLines; line = line.next, c++)
2887 if(this.line != oldLine)
2889 this.x = AdjustXPosition(this.line, caretX, true, null, MAXINT, 0);
2897 this.selLine = this.line;
2900 UpdateCaretPosition(false);
2907 bool SaveFile(char * fileName)
2909 File f = eFile_Open(fileName, FO_WRITE);
2913 eWindow_SetModified(false);
2914 eInstance_Delete(f);
2921 bool OnActivate(bool active, Window previous, bool * goOnWithActivation, bool direct)
2925 if(!style.multiLine)
2927 x = (active && this.active && !style.readOnly) ? line.count : 0;
2930 SetViewToCursor(true);
2935 if(!active && modified)
2938 if(!NotifyModified(master, this))
2941 *goOnWithActivation = false;
2948 if(timer) timer.Stop();
2950 mouseSelect = false;
2956 bool OnResizing(int *w, int *h)
2962 *w = space.h * 80 / 14;
2966 void OnResize(int w, int h)
2969 if(!hasHorzScroll && !hasVertScroll && viewLine)
2970 SetViewToCursor(true);
2972 //if(!hasHorzScroll && !hasVertScroll && viewLine)
2974 SetViewToCursor(true);
2979 bool OnMiddleButtonDown(int x, int y, Modifiers mods)
2981 if(style.readOnly) return true;
2986 bool OnRightButtonDown(int x, int y, Modifiers mods)
2988 this.rightButtonDown = true;
2993 bool OnRightButtonUp(int x, int y, Modifiers mods)
2996 if(!parent.inactive && rightButtonDown)
2999 Menu contextMenu { };
3001 MenuItem { contextMenu, "Cut\tCtrl+X", t, NotifySelect = itemEditCut.NotifySelect, disabled = !selection || style.readOnly };
3002 MenuItem { contextMenu, "Copy\tCtrl+C", c, NotifySelect = itemEditCopy.NotifySelect, disabled = !selection };
3003 MenuItem { contextMenu, "Paste\tCtrl+V", p, NotifySelect = itemEditPaste.NotifySelect, disabled = style.readOnly };
3004 MenuItem { contextMenu, "Delete\tDel", d, NotifySelect = itemEditDelete.NotifySelect, disabled = !selection || style.readOnly };
3005 MenuDivider { contextMenu };
3006 MenuItem { contextMenu, "Select All\tCtrl+A", a, NotifySelect = itemEditSelectAll.NotifySelect };
3008 popup = PopupMenu { master = this, menu = contextMenu,
3010 nonClient = true, interim = false, parent = parent,
3011 position = { x + clientStart.x + parent.clientStart.x + position.x, y + cientStart.y + parent.sy + clientStart.y + position.y };
3013 position = { x + clientStart.x + absPosition.x - guiApp.desktop.position.x, y + clientStart.y + absPosition.y - guiApp.desktop.position.y }
3017 rightButtonDown = false;
3021 bool OnLeftButtonDown(int mx, int my, Modifiers mods)
3026 if(style.noSelect) return true;
3028 if(!mods.isActivate)
3034 mouseX = mx - XOFFSET;
3037 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3039 //PrintLn("OnLeftButtonDown: ", x, ", ", y);
3045 else if(IsMouseOnSelection() && !mods.isActivate)
3055 if(!mouseMove && !wordSelect && (!mods.isActivate || style.multiLine))
3057 if(mods.shift && !mods.isActivate)
3072 this.selLine = this.line;
3081 UpdateCaretPosition(true);
3085 bool OnLeftButtonUp(int x, int y, Modifiers mods)
3089 mouseSelect = false;
3100 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3102 //PrintLn("MouseMove: ", x, ", ", y);
3108 mouseMove = IsMouseOnSelection();
3112 int size = SelSize();
3115 char * text = new char[size+1];
3119 GetSel(text, false);
3121 if(Max(selY, this.y) == dropY)
3125 if(this.dropX > this.selX)
3126 moveX = this.x - this.selX;
3130 if(this.dropX > this.x)
3131 moveX = this.selX - this.x;
3135 this.dropX -= moveX;
3136 this.selX = this.x = this.dropX;
3137 this.selY = this.y = this.dropY;
3138 this.selLine = this.line = this.dropLine;
3140 SetViewToCursor(true);
3145 byte * c = ((EditBox)this).multiLineContents, * c2;
3146 int l1 = c ? strlen(c) : 0, l2;
3149 c2 = ((EditBox)this).multiLineContents;
3150 l2 = c2 ? strlen(c2) : 0;
3151 if(l1 != l2 || (l1 && strcmp(c, c2)))
3181 FindMouse(mouseX, mouseY, &x, &y, &line, true);
3183 //PrintLn("Dropped: ", x, ", ", y);
3185 NotifyDropped(master, this, x, y);
3192 bool OnMouseMove(int mx, int my, Modifiers mods)
3198 if(mods != -1 && mods.isSideEffect)
3203 if(style.noSelect) return true;
3204 if(wordSelect) return true;
3205 mouseX = mx - XOFFSET;
3208 needScroll = FindMouse(this.mouseX, this.mouseY, &x, &y, &line, true);
3210 if(this.mouseMove || this.mouseSelect)
3219 ((style.hScroll) || (style.vScroll)))
3226 DirtyLine(this.dropY);
3229 DirtyLine(this.dropY);
3230 this.dropLine = line;
3231 SetViewToCursor(true);
3233 //PrintLn("MouseMove: ", "dropX = ", x, ", dropY = ", y);
3236 else if(this.mouseSelect)
3238 DirtyLine(this.selY);
3245 SetViewToCursor(true);
3248 //PrintLn("MouseSelect: ", "x = ", x, ", y = ", y);
3255 bool OnLeftDoubleClick(int mx, int my, Modifiers mods)
3260 //PrintLn("OnLeftDoubleClick: ", mx, ", ", my, ", mods = ", mods);
3264 if(style.noSelect) return true;
3265 FindMouse(mx, my, &x, &y, &line, false);
3266 if(!NotifyDoubleClick(master, this, line, mods))
3273 for(c = x; c >= 0; c--)
3276 while(c > 0 && !UTF8_IS_FIRST(line.buffer[c])) c--;
3277 ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3284 for(c = start; c<line.count; c += numBytes)
3286 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3297 this.line = this.selLine = line;
3298 this.wordSelect = (c != start);
3310 Menu fileMenu { menu, "File", F };
3311 saveDialog = fileDialog;
3312 MenuItem { fileMenu, "Save\tCtrl+S", S, CtrlS, NotifySelect = MenuFileSave };
3313 MenuItem { fileMenu, "Save As...", A, NotifySelect = MenuFileSaveAs };
3323 FontExtent(display, font, " ", 1, (int *)&space.w, (int *)&space.h);
3324 FontExtent(display, font, "W", 1, (int *)&large.w, (int *)&large.h);
3326 space.w = Max(space.w, 1);
3327 large.w = Max(large.w, 1);
3328 space.h = Max(space.h, 1);
3329 large.h = Max(large.h, 1);
3333 for(line = lines.first; line; line = line.next)
3334 ComputeLength(line);
3339 SetViewToCursor(true);
3347 bool OnLoadGraphics()
3349 FontExtent = Display::FontExtent;
3352 // UpdateCaretPosition(true);
3356 void OnUnloadGraphics()
3361 bool OnKeyHit(Key key, unichar ch)
3363 bool shift = (key.shift) ? true : false;
3365 //PrintLn("OnKeyHit: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
3367 if(!ch && !key.alt && !key.ctrl)
3369 key.code = (SmartKey)key.code;
3372 switch(key.code) //(ch || key.alt || key.ctrl) ? key.code : (Key)(SmartKey)key.code)
3375 if(style.readOnly) break;
3376 if(style.stuckCaret) GoToEnd(true);
3377 if(!(style.freeCaret))
3379 this.x = Min(this.x, this.line.count);
3385 EditLine line = this.line;
3387 for(y = this.y; y>= 0; y--)
3389 c = (y == this.y) ? (this.x-1) : line.count-1;
3391 // Slow down when going on lines...
3392 if(y != this.y) break;
3396 //if(this.line.buffer[c] != '\t' && this.line.buffer[c] != ' ')
3397 if(IS_ALUNDER(line.buffer[c]))
3402 if(!IS_ALUNDER(line.buffer[c]))
3404 //if(this.line.buffer[c] == '\t' || this.line.buffer[c] == ' ')
3418 DelCh(line,y,c+1,this.line,this.y,this.x, true);
3419 this.x = this.selX = Min(c+1, line.count);
3420 this.y = this.selY = y;
3421 this.line = this.selLine = line;
3422 SetViewToCursor(true);
3433 if(style.readOnly) break;
3434 if(style.stuckCaret) break;
3443 if(this.line != this.selLine || this.x != this.selX)
3446 SetViewToCursor(true);
3452 if(this.x < this.line.count)
3456 //Can't delete right away as that would change the line.count
3457 for(i = this.x; i < this.line.count; i++)
3459 if(!IS_ALUNDER(this.line.buffer[i]))
3461 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3465 for(; i < this.line.count; i++)
3467 //Delete trailing whitespace
3468 if(IS_ALUNDER(this.line.buffer[i]))
3470 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3473 SetViewToCursor(true);
3476 else if(this.line.next)
3478 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3479 SetViewToCursor(true);
3485 if(!(style.freeCaret))
3487 this.selX = this.x = Min(this.x, this.line.count);
3490 if(this.x < this.line.count)
3492 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, false);
3494 else if(this.line.next)
3496 DelCh(this.line, this.y, this.x, this.line.next, this.y+1, 0, false);
3498 SetViewToCursor(true);
3507 if(!key.alt && !key.ctrl)
3511 bool stuffAfter = false;
3515 if(style.stuckCaret) GoToEnd(true);
3516 if(style.readOnly) break;
3517 if(!(style.multiLine)) break;
3519 for(c = 0; c<this.line.count && c<this.x; c++)
3521 if(this.line.buffer[c] == '\t')
3522 position += this.tabSize - (position % this.tabSize);
3523 else if(this.line.buffer[c] == ' ')
3531 if(this.x < this.line.count)
3534 //If last character is a { indent one tab
3535 if(this.line.buffer[this.x - 1] == '{')
3537 //Except if the next non space character is a }
3538 bool indent = false;
3540 for(i = this.x; i < this.line.size; i++)
3541 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
3543 if(this.line.buffer[i] != '}')
3548 position += this.tabSize;
3551 addString = new char[position + 2];
3552 addString[len++] = '\n';
3553 addString[len] = '\0';
3555 if(stuffAfter || !style.freeCaret)
3557 for(c = 0; c<position; )
3559 if(style.useTab && c + this.tabSize <= position)
3561 addString[len++] = '\t';
3566 addString[len++] = ' ';
3570 addString[len] = '\0';
3574 if(!stuffAfter && style.freeCaret)
3576 this.x = this.selX = position;
3580 SetViewToCursor(true);
3590 if(style.stuckCaret) break;
3591 if(!(style.freeCaret))
3593 this.x = Min(this.x, this.line.count);
3594 this.selX = Min(this.selX, this.selLine.count);
3597 if(!shift) SelDirty();
3600 bool foundAlpha = false;
3603 EditLine line, lastLine;
3606 for(line = this.line; (line && !found); line = line.prev, y--)
3611 if(this.x == 0 && line != this.line)
3620 if(line == this.line) start = this.x -1; else start = line.count-1;
3621 start = Min(start, line.count-1);
3623 for(c = start; c >= 0;)
3626 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3644 byte ch = line.buffer[c];
3645 if(UTF8_IS_FIRST(ch)) break;
3649 // No next word found,
3650 if(!found && ( this.x > 0 || (!line.count && this.x)))
3664 this.line = lastLine;
3675 byte * buffer = line.buffer;
3678 byte ch = buffer[x];
3679 if(UTF8_IS_FIRST(ch)) break;
3696 if(!shift) Deselect();
3697 SetViewToCursor(true);
3703 if(style.stuckCaret) break;
3704 if(!(style.freeCaret))
3706 this.x = Min(this.x, this.line.count);
3707 this.selX = Min(this.selX, this.selLine.count);
3710 if(!shift) SelDirty();
3711 if(!shift && (this.x != this.selX || this.y != this.selY));
3714 bool onAChar = false;
3715 if(this.selX != this.x || this.selY != this.y)
3717 if(this.x<this.line.count)
3718 if(this.line.buffer[this.x] != '\t' && this.line.buffer[this.x] !=' ')
3720 if(key.shift && onAChar &&
3721 ((this.y > this.selY)||((this.selY == this.y)&&(this.x >= this.selX))))
3723 bool foundAlpha = false;
3725 EditLine line, lastLine;
3727 int lastC, lastY, lastNumBytes;
3729 for(line = this.line; (line && !found); line = line.next, y++)
3731 int start = (line == this.line) ? this.x : 0;
3734 for(c = start; c < line.count; c += numBytes)
3736 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3741 lastNumBytes = numBytes;
3751 if(!found && (c != this.x || line != this.line))
3764 this.x = lastC + lastNumBytes;
3766 this.line = lastLine;
3773 bool foundAlpha = false;
3778 for(line = this.line; (line && !found); line = line.next, y++)
3780 int start = (line == this.line) ? this.x : 0;
3783 for(c = start; c < line.count; c += numBytes)
3785 unichar ch = UTF8_GET_CHAR(line.buffer + c, numBytes);
3800 // No next word found,
3801 if(!found && (c != this.x || line != this.line))
3805 this.x = line.count;
3817 if(x < line.count || (style.freeCaret && line.count < maxLineSize))
3821 byte * buffer = line.buffer;
3824 byte ch = buffer[x];
3825 if(UTF8_IS_FIRST(ch)) break;
3846 if(!shift) Deselect();
3847 SetViewToCursor(true);
3854 if(!style.vScroll || hasVertScroll) break;
3860 if(style.stuckCaret) break;
3862 if(!shift) SelDirty();
3872 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
3876 if(!shift) Deselect();
3878 SetViewToCursor(false);
3881 if(caretY == this.y * space.h)
3887 if(!style.freeCaret)
3888 this.x = Min(this.x, line.count);
3898 int sx = 0, sy = this.y * space.h;
3899 char * text = line.text;
3900 int maxW = clientSize.w - sx;
3901 display.FontExtent(font, " ", 1, null, &th);
3905 int startPos = textPos;
3908 bool lineComplete = false;
3910 if(!style.wrap && caretY == MAXINT)
3913 //textPos = line.count;
3914 //lineComplete = true;
3917 for(; (style.freeCaret || textPos < line.count) && !lineComplete;)
3921 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
3924 len = (nextSpace - (text + textPos));
3926 len = line.count - textPos;
3928 if(textPos < line.count)
3930 display.FontExtent(font, text + textPos, len, &w, null);
3932 if(nextSpace) { w += space.w; len++; }
3934 if(style.wrap && x + width + w > maxW && x > 0)
3936 lineComplete = true;
3946 if((!style.freeCaret && textPos >= line.count) || (sy == caretY - th && caretX <= x + width + sx))
3950 while(this.x > 0 && x + sx > caretX && this.x > startPos)
3953 if(this.x > line.count)
3956 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
3957 len = this.x - startPos;
3958 display.FontExtent(font, text + startPos, len, &x, null);
3962 if(!shift) Deselect();
3964 SetViewToCursor(false);
3968 if(sy == caretY - th || textPos >= line.count)
3970 if(textPos >= line.count)
3972 int c = textPos - 1;
3973 while(c > 0 && text[c] == ' ') c--;
3977 this.x = line.count;
3980 if(!shift) Deselect();
3982 SetViewToCursor(false);
3987 } while(textPos < line.count);
3990 if(!shift) Deselect();
3992 SetViewToCursor(false);
4001 int x = AdjustXPosition(this.line, this.line.prev, true, null, MAXINT, 0);
4002 if(!shift) SelDirty();
4003 this.line = this.line.prev;
4009 if(!shift) Deselect();
4013 SetViewToCursor(false);
4019 return style.multiLine ? false : true;
4023 if(!style.vScroll || hasVertScroll)
4030 if(style.stuckCaret) break;
4034 int sx = 0, sy = this.y * this.space.h;
4035 int maxW = clientSize.w - sx;
4036 char * text = line.buffer;
4038 if(!shift) SelDirty();
4044 if(AdjustXPosition(line, maxW, this.x, line.count, true, null, MAXINT, 0) <= line.count)
4054 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
4057 if(!shift) Deselect();
4059 if(this.selX != this.x || this.selY != this.y)
4061 SetViewToCursor(false);
4064 while(!textPos || (style.freeCaret || textPos<line.count))
4066 int startPos = textPos;
4069 bool lineComplete = false;
4070 if(!style.wrap && sy <= caretY)
4072 textPos = line.count;
4073 lineComplete = true;
4075 for(; (style.freeCaret || textPos<line.count) && !lineComplete;)
4079 char * nextSpace = (textPos < line.count) ? strchr(text + textPos, ' ') : (text + textPos);
4082 len = (nextSpace - (text + textPos));
4084 len = line.count - textPos;
4086 if(textPos < line.count)
4088 display.FontExtent(font, text + textPos, len, &w, &th);
4090 if(nextSpace) { w += space.w; len++; }
4091 if(style.wrap && x + width + w > maxW && x > 0)
4093 lineComplete = true;
4103 if(sy > caretY && ((!style.freeCaret && textPos >= line.count) || caretX <= x + width + sx))
4107 while(this.x > 0 && x + sx > caretX && textPos > startPos)
4110 if(this.x > line.count)
4113 while(this.x > 0 && !UTF8_IS_FIRST(text[--this.x]));
4115 len = this.x - startPos;
4116 display.FontExtent(font, text + startPos, len, &x, null);
4119 if(!shift) Deselect();
4122 SetViewToCursor(false);
4128 this.x = line.count;
4131 if(!shift) Deselect();
4134 SetViewToCursor(false);
4137 else if(textPos >= line.count && line.next)
4143 sy = this.y * this.space.h;
4144 sx = 0; //textBlock.startX;
4150 sx = 0; //textBlock.startX;
4158 this.x = Min(this.x, line.count);
4159 //int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4161 if(!shift) Deselect();
4164 if(this.selX != this.x || this.selY != this.y)
4167 SetViewToCursor(false);
4174 int x = AdjustXPosition(this.line, this.line.next, true, null, MAXINT, 0);
4175 if(!shift) SelDirty();
4176 this.line = this.line.next;
4180 if(!shift) Deselect();
4183 if(this.selX != this.x || this.selY != this.y)
4191 return style.multiLine ? false : true;
4194 if(style.stuckCaret) break;
4195 if(!(style.freeCaret))
4196 this.selX = Min(this.selX, this.selLine.count);
4198 if(!shift) SelDirty();
4201 this.line = this.lines.first;
4202 if(this.y != 0 || this.x != 0)
4212 EditLine line = this.line;
4214 for(c=0; line.buffer[c]; c++)
4215 if(line.buffer[c] != ' ' && line.buffer[c] != '\t')
4217 if(c != 0 || this.x)
4227 DirtyLine(this.y);*/
4232 if(!shift) Deselect();
4233 SetViewToCursor(true);
4239 if(style.stuckCaret) break;
4240 if(!(style.freeCaret))
4241 this.selX = Min(this.selX, this.selLine.count);
4243 if(!shift) SelDirty();
4248 else if(this.x != this.line.count)
4250 this.x = this.line.count;
4251 //DirtyLine(this.y);
4254 if(!shift) Deselect();
4255 SetViewToCursor(true);
4260 if(style.tabKey && !key.ctrl)
4262 if(this.selY != this.y && style.tabSel)
4264 EditLine firstLine, lastLine;
4268 // Do multi line selection tabbing here
4269 if(this.selY < this.y)
4271 firstLine = this.selLine;
4272 lastLine = this.line;
4278 // Selecting going up
4279 firstLine = this.line;
4280 lastLine = this.selLine;
4287 for(line = firstLine; line; line = line.next, y++)
4289 if(line != lastLine || x)
4293 BufferLocation before = { line, y, 0 }, after = { line, y, 0 };
4295 for(c=0; c<line.count && lastC < this.tabSize; c++, lastC++)
4297 if(line.buffer[c] == '\t')
4302 else if(line.buffer[c] != ' ')
4307 NotifyCharsDeleted(master, this, &before, &after, this.pasteOperation);
4310 int len = GetText(null, line, y, 0, line, y, lastC, false, false);
4311 char * string = new char[len];
4312 DelTextAction action { y1 = y, x1 = 0, y2 = y, x2 = lastC, string = string, placeAfter = true };
4313 GetText(string, line, y, 0, line, y, lastC, false, false);
4316 memmove(line.buffer,line.buffer+lastC,line.size-lastC);
4317 if(!line.AdjustBuffer(line.count-lastC))
4323 if(line == lastLine) break;
4328 for(line = firstLine; line; line = line.next, y++)
4332 if(line != lastLine || x)
4336 if(line.count + 1 <= this.maxLineSize)
4338 BufferLocation before = { line, y, 0 }, after = { line, y, 1 };
4340 if(!line.AdjustBuffer(line.count+1))
4343 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4345 AddCharAction action { ch = '\t', x = 0, y = y };
4349 memmove(line.buffer+1,line.buffer,line.size-1);
4351 line.buffer[0] = '\t';
4356 if(line.count + this.tabSize <= this.maxLineSize)
4359 BufferLocation before = { line, y, 0 }, after = { line, y, this.tabSize };
4360 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4362 if(!line.AdjustBuffer(line.count+this.tabSize))
4366 char * string = new char[this.tabSize + 1];
4367 memset(string, ' ', this.tabSize);
4368 string[this.tabSize] = '\0';
4369 Record(AddTextAction { string = string, x1 = 0, y1 = y, x2 = this.tabSize, y2 = y });
4372 memmove(line.buffer+this.tabSize,line.buffer,line.size-(this.tabSize));
4373 line.count+=this.tabSize;
4374 for(c=0; c<this.tabSize; c++)
4375 line.buffer[c] = ' ';
4381 if(line == lastLine) break;
4395 char * addString = new char[this.tabSize + 1];
4397 if(!(style.freeCaret))
4399 this.x = Min(this.x, this.line.count);
4404 for(c=start; ((c == start) || ((c) % this.tabSize)); c++)
4406 addString[len++] = ' ';
4414 SetViewToCursor(true);
4421 if(!(style.hScroll) || hasHorzScroll) break;
4422 if(this.viewX < this.maxLength)
4424 //this.viewX+=this.space.w*this.tabSize;
4426 SetScrollPosition((this.viewX + this.space.w*this.tabSize), this.viewY * this.space.h);
4433 if(!shift) Deselect();
4442 if(!(style.hScroll) || hasHorzScroll) break;
4445 //this.viewX-=this.space.w*this.tabSize;
4446 //this.viewX = Max(this.viewX,0);
4448 SetScrollPosition((this.viewX-this.space.w*this.tabSize), this.viewY * this.space.h);
4449 // SetCursorToView();
4456 if(!shift) Deselect();
4470 if(!(style.readOnly))
4476 this.overwrite ^= 1;
4477 UpdateCaretPosition(true);
4482 NotifyOvrToggle(master, this, this.overwrite);
4493 if(style.noSelect) break;
4496 //Save current view position
4497 int tempX = this.viewX;
4498 int tempY = this.viewY;
4502 this.selLine = this.lines.first;
4503 this.y = this.lineCount-1;
4504 this.line = this.lines.last;
4505 this.x = this.line.count;
4508 SetViewToCursor(true);
4510 //Restore previous view position
4511 SetScrollPosition(tempX, tempY * this.space.h);
4517 // TOCHECK: Was there any good reason why we weren't returning false here?
4518 return false; // break;
4523 if(!(style.readOnly))
4531 if(style.readOnly) break;
4537 if(style.readOnly) break;
4542 if(style.readOnly) break;
4546 if(style.readOnly) break;
4550 if(style.readOnly) break;
4551 if(key.shift && key.code == rightBracket)
4553 //Only indent back if you are exactly at one tab.
4555 bool whitespace = true;
4561 EditLine line = this.line;
4563 //Only remove one tab if there is nothing else on the line.
4564 for(i = 0; i < this.line.count; i++)
4566 if(this.line.buffer[i] != ' ' && this.line.buffer[i] != '\t')
4580 for(pos = line.count - 1; pos >= 0; pos--)
4582 char c = line.buffer[pos];
4586 indentwidth += this.tabSize;
4588 //Counting backwards, so when you find a character, indentation is reset
4598 //Place the } to get an undo:
4601 this.x = this.line.count;
4605 putsize = indentwidth;
4607 putsize = indentwidth / this.tabSize + indentwidth % this.tabSize;
4609 newline = new char[putsize+2];
4610 newline[putsize] = '}';
4611 newline[putsize+1] = '\0';
4615 for(; i < indentwidth / this.tabSize; i++)
4617 for(;i < putsize; i++)
4625 } else if(!key.ctrl && !key.alt && ch != 128 && ch >= 32)
4637 void OnHScroll(ScrollBarAction action, int position, Key key)
4640 //PrintLn("OnHScroll: ", action, ", pos = ", position, ", key = ", key);
4642 this.viewX = position;
4643 if(action != setRange)
4645 if(!this.mouseMove && style.cursorFollowsView)
4652 void OnVScroll(ScrollBarAction action, int position, Key key)
4654 int oldViewY = this.viewY;
4657 //PrintLn("OnVScroll: ", action, ", pos = ", position, ", key = ", key);
4659 position /= this.space.h;
4661 if(position < this.viewY)
4663 for(; position < this.viewY && this.viewLine.prev; this.viewLine = this.viewLine.prev, this.viewY--);
4664 style.recomputeSyntax = true;
4666 else if(position > this.viewY)
4668 EditLine oldViewLine = viewLine;
4669 for(; position > this.viewY && this.viewLine.next; this.viewLine = this.viewLine.next, this.viewY++);
4670 FigureStartSyntaxStates(oldViewLine, false);
4673 if(action != setRange)
4675 if(!this.mouseMove && style.cursorFollowsView && !SelSize()) SetCursorToViewY();
4678 if(this.x != this.selX || this.y != this.selY)
4682 Scroll(0, (this.viewY - oldViewY) * this.space.h);
4685 int numLines = clientSize.h / this.space.h;
4687 if(Abs(this.viewY - oldViewY) < numLines)
4690 if(this.viewY > oldViewY)
4692 for(y = oldViewY; y <this.viewY; y++)
4693 DirtyLine(y + numLines);
4697 for(y = this.viewY; y <oldViewY; y++)
4698 DirtyLine(y + numLines);
4703 // Fix dirt of stuff before first line...
4704 if(this.viewY - oldViewY > 0)
4706 Box box { 0,0, clientSize.w-1, YOFFSET-1 };
4713 bool _AddCh(unichar ch, int * addedSpacesPtr, int * addedTabsPtr)
4718 ReplaceTextAction replaceAction = null;
4719 AddCharAction addCharAction = null;
4720 int addedSpaces = 0, addedTabs = 0;
4722 if(ch == '\r') return true;
4723 if(style.stuckCaret /*|EES_READONLY)*/ )
4726 if(ch == '\n' && !(style.multiLine) && this.line) return false;
4728 if(!undoBuffer.dontRecord)
4730 if(selX != x || selY != y)
4735 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
4736 oldString = new char[len];
4737 UTF32toUTF8Len(&ch, 1, buffer, 4);
4738 newString = CopyString(buffer);
4739 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
4741 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
4742 if(selY < y || (selY == y && selX < x))
4744 replaceAction.x1 = selX;
4745 replaceAction.y1 = selY;
4746 replaceAction.x2 = x;
4747 replaceAction.y2 = y;
4751 replaceAction.x1 = x;
4752 replaceAction.y1 = y;
4753 replaceAction.x2 = selX;
4754 replaceAction.y2 = selY;
4756 Record(replaceAction);
4757 undoBuffer.dontRecord++;
4761 addCharAction = AddCharAction { y = y, x = x, ch = ch };
4762 Record(addCharAction);
4768 DelSel(&addedSpaces);
4769 if(this.lineCount+1 > this.maxLines)
4772 Emptyline(this.lines.first,0);
4776 if(!(style.vScroll))
4778 // Make sure it fits, but we need a default line is this.font is too big for window
4779 if(this.space.h * (this.lineCount+1) > clientSize.h && this.line)
4782 if((this.y >= 0) && this.y < this.viewY)
4787 line = EditLine { };
4790 lines.Insert(this.line, line);
4791 line.editBox = this;
4797 // If we're displacing the lines from a current line ...
4800 if(this.line.buffer)
4803 if(this.line.count < endX) endX = this.line.count;
4804 length = this.line.count - endX;
4807 if(!line.AdjustBuffer(length))
4811 if(this.line.buffer)
4813 CopyBytes(line.buffer,this.line.buffer+endX, length+1);
4815 if(endX > 4000 || endX < 0)
4818 this.line.count = endX;
4819 this.line.buffer[this.line.count] = '\0';
4820 this.line.AdjustBuffer(this.line.count);
4821 ComputeLength(this.line);
4825 BufferLocation before = { this.line, this.y, this.x }, after;
4831 ComputeLength(this.line);
4834 if(length > 4000 || length < 0)
4837 line.count = length;
4841 line.buffer[line.count] = '\0';
4844 after.line = this.line, after.y = this.y, after.x = this.x;
4846 NotifyCharsAdded(master, this, &before, &after, this.pasteOperation);
4852 int count = UTF32toUTF8Len(&ch, 1, string, 5);
4853 DelSel(&addedSpaces);
4854 result = AddToLine(string, count, false, addedSpaces ? null : &addedSpaces, &addedTabs);
4855 if(addedSpacesPtr) *addedSpacesPtr = addedSpaces;
4856 if(addedTabsPtr) *addedTabsPtr = addedTabs;
4860 this.selLine = this.line;
4864 replaceAction.x3 = x;
4865 replaceAction.y3 = y;
4866 replaceAction.addedSpaces = addedSpaces;
4867 replaceAction.addedTabs = addedTabs;
4868 undoBuffer.dontRecord--;
4872 addCharAction.addedSpaces = addedSpaces;
4873 addCharAction.addedTabs = addedTabs;
4880 /****************************************************************************
4882 ****************************************************************************/
4885 bool AddCh(unichar ch)
4887 return _AddCh(ch, null, null);
4892 this.modified = true;
4893 NotifyUpdate(master, this);
4894 modifiedDocument = true;
4897 void Delete(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4900 DelCh(line1, y1, x1, line2, y2, x2, false);
4901 SetViewToCursor(true);
4909 itemEditUndo.disabled = undoBuffer.curAction == 0;
4910 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4911 if(savedAction == undoBuffer.curAction)
4913 modifiedDocument = false;
4915 NotifyUnsetModified(master, this);
4922 itemEditUndo.disabled = undoBuffer.curAction == 0;
4923 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4924 if(savedAction == undoBuffer.curAction)
4926 modifiedDocument = false;
4928 NotifyUnsetModified(master, this);
4932 void Record(UndoAction action)
4934 if(!undoBuffer.dontRecord)
4936 undoBuffer.Record(action);
4937 itemEditUndo.disabled = undoBuffer.curAction == 0;
4938 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
4945 this.lines.first, 0,0,
4946 this.lines.last, this.lines.count-1, strlen(((EditLine)this.lines.last).buffer));
4949 void Select(EditLine line1, int y1, int x1, EditLine line2, int y2, int x2)
4954 this.selLine = line1 ? (EditLine)line1 : this.lines.first;
4955 this.x = line2 ? x2 : ((EditLine)this.lines.last).count;
4956 this.y = line2 ? y2 : (this.lineCount-1);
4957 this.line = line2 ? (EditLine)line2 : this.lines.last;
4960 SetViewToCursor(true);
4964 // TODO: Fix this vs modifiedDocument window property
4965 void SetModified(bool flag)
4969 this.modified = false;
4970 if(flag && !NotifyModified(master, this))
4971 this.modified = true;
4976 bool AddS(char * string)
4983 int addedSpaces = 0, addedTabs = 0;
4984 AddTextAction action = null;
4985 ReplaceTextAction replaceAction = null;
4987 this.pasteOperation = true;
4989 if(style.stuckCaret /*|EES_READONLY)*/ )
4992 if(!undoBuffer.dontRecord)
4994 char * placeString = CopyString(string);
4995 if(!style.multiLine)
4999 for(i = 0; (ch = placeString[i]); i++)
5002 placeString[i] = '\0';
5003 placeString = renew placeString byte[i+1];
5008 if(selX != x || selY != y)
5012 int len = GetText(null, selLine, selY, selX, this.line, y, x, false, false);
5013 oldString = new char[len];
5014 newString = placeString;
5015 GetText(oldString, selLine, selY, selX, this.line, y, x, false, false);
5017 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5018 if(selY < y || (selY == y && selX < x))
5020 replaceAction.x1 = selX;
5021 replaceAction.y1 = selY;
5022 replaceAction.x2 = x;
5023 replaceAction.y2 = y;
5027 replaceAction.x1 = x;
5028 replaceAction.y1 = y;
5029 replaceAction.x2 = selX;
5030 replaceAction.y2 = selY;
5032 Record(replaceAction);
5036 if(string[0] == '\n')
5037 action = AddTextAction { y1 = y, x1 = Min(this.line.count, x), string = placeString };
5039 action = AddTextAction { y1 = y, x1 = x, string = placeString };
5047 undoBuffer.dontRecord++;
5048 DelSel(&addedSpaces);
5052 for(c = 0; string[c]; c++)
5054 if(string[c] == '\n' || string[c] == '\r')
5056 if(!AddToLine(line,count, true, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5061 if(string[c] == '\n')
5069 // Reset for next line
5073 if(string[c] == '\r' && *line == '\n')
5085 // Why was this here?
5088 // Add the line here
5090 if(!AddToLine(line,count,false, addedSpaces ? null : &addedSpaces, addedTabs ? null : &addedTabs))
5096 undoBuffer.dontRecord--;
5101 action.addedSpaces = addedSpaces;
5102 action.addedTabs = addedTabs;
5104 else if(replaceAction)
5106 replaceAction.y3 = y;
5107 replaceAction.x3 = x;
5108 replaceAction.addedSpaces = addedSpaces;
5109 replaceAction.addedTabs = addedTabs;
5112 UpdateCaretPosition(true);
5114 this.pasteOperation = false;
5127 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5128 SetViewToCursor(true);
5134 void PutCh(unichar ch)
5138 if((ch >= 32 /*&& ch <=126*/) || ch == '\n')
5139 //if((ch >= 32) || ch == '\n')
5141 int addedSpaces = 0, addedTabs = 0;
5142 ReplaceTextAction replaceAction = null;
5143 AddCharAction addCharAction = null;
5146 ch = (ch < 128) ? toupper(ch) : ch; // TODO: UNICODE TO UPPER
5148 if(this.x < this.line.count && this.overwrite)
5153 int len = GetText(null, line, y, x, line, y, x+1, false, false);
5154 oldString = new char[len];
5155 UTF32toUTF8Len(&ch, 1, buffer, 4);
5156 newString = CopyString(buffer);
5157 GetText(oldString, line, y, x, line, y, x+1, false, false);
5158 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = false };
5159 replaceAction.x1 = x;
5160 replaceAction.y1 = y;
5161 replaceAction.x2 = x+1;
5162 replaceAction.y2 = y;
5163 Record(replaceAction);
5165 undoBuffer.dontRecord++;
5166 DelCh(this.line, this.y, this.x, this.line, this.y, this.x+1, true);
5167 undoBuffer.dontRecord--;
5169 else if(!undoBuffer.dontRecord)
5171 if(selX != x || selY != y)
5176 int len = GetText(null, line, y, x, selLine, selY, selX, false, false);
5177 oldString = new char[len];
5178 UTF32toUTF8Len(&ch, 1, buffer, 4);
5179 newString = CopyString(buffer);
5180 GetText(oldString, line, y, x, selLine, selY, selX, false, false);
5181 replaceAction = ReplaceTextAction { newString = newString, oldString = oldString, placeAfter = true };
5182 if(selY < y || (selY == y && selX < x))
5184 replaceAction.x1 = selX;
5185 replaceAction.y1 = selY;
5186 replaceAction.x2 = x;
5187 replaceAction.y2 = y;
5191 replaceAction.x1 = x;
5192 replaceAction.y1 = y;
5193 replaceAction.x2 = selX;
5194 replaceAction.y2 = selY;
5196 Record(replaceAction);
5200 addCharAction = AddCharAction { y = y, x = x, ch = ch };
5201 Record(addCharAction);
5204 undoBuffer.dontRecord++;
5205 result = _AddCh(ch, &addedSpaces, &addedTabs);
5208 replaceAction.x3 = x;
5209 replaceAction.y3 = y;
5210 replaceAction.addedSpaces = addedSpaces;
5211 replaceAction.addedTabs = addedTabs;
5215 addCharAction.addedSpaces = addedSpaces;
5216 addCharAction.addedTabs = addedTabs;
5218 undoBuffer.dontRecord--;
5222 if(result) SetViewToCursor(true);
5226 void PutS(char * string)
5231 SetViewToCursor(true);
5236 void Printf(char * format, ...)
5240 char temp[MAX_F_STRING];
5242 va_start(args, format);
5243 vsprintf(temp, format, args);
5249 void SetContents(char * format, ...)
5254 DelCh(this.lines.first, 0, 0, this.lines.last, this.lineCount-1, ((EditLine)(this.lines.last)).count, true);
5257 char temp[MAX_F_STRING];
5259 va_start(args, format);
5260 vsprintf(temp, format, args);
5276 x -= 1 + DelCh(line, y, x-1, line, y, x, true);
5279 else if(this.line.prev)
5281 EditLine line = this.line.prev;
5285 DelCh(line, this.y-1, x, this.line, this.y, this.x, true);
5293 this.selLine = this.line;
5298 SetViewToCursor(true);
5303 Emptyline(this.line,this.y);
5304 this.selX = this.x = 0;
5307 this.selLine = this.line;
5309 SetViewToCursor(true);
5319 SetViewToCursor(true);
5327 SetViewToCursor(true);
5331 bool GoToLineNum(int lineNum)
5336 EditLine line = this.lines.first;
5337 for(c = 0; c < lineNum && line; c++, line = line.next);
5347 SetViewToCursor(true);
5354 bool GoToPosition(EditLine line, int y, int x)
5366 for(line = this.lines.first, c = 0; c<y && line; c++, line = line.next);
5380 SetViewToCursor(true);
5387 void SetViewToCursor(bool setCaret)
5397 bool dontScroll = false;
5403 selected = selX != this.x || selY != y;
5410 checkLine = dropLine;
5416 checkLine = this.line;
5421 numLines = clientSize.h / space.h;
5423 // This is broken. The EditBox now doesn't do anything different when adding to it,
5424 // regardless of the previous scrolling position. It should be read and then set again
5425 // if one wishes to preserve it.
5426 /* // Don't scroll view to cursor if we're in a EES_NOCARET box
5427 if(style.noCaret && this.viewY < lineCount - numLines - 1)
5431 // Horizontal Adjustment
5432 if(!dontScroll && checkLine)
5436 dropX = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5439 this.x = AdjustXPosition(this.line, MAXINT, true, &x, checkX, 0);
5445 if(x + space.w >= this.viewX + clientSize.w && clientSize.w >= space.w)
5446 viewX = x - clientSize.w+space.w;
5447 if(x < this.viewX + clientSize.w/2 - space.w)
5448 viewX = Max(0, x - clientSize.w/2 + space.w);
5456 // Vertical Adjustment
5457 if(viewY > checkY) viewY = checkY;
5458 if(viewY + numLines <= checkY)
5460 if(clientSize.h >= space.h)
5462 for(line = viewLine; line && (viewY + numLines <= checkY); line = line.next)
5472 for(;dropLine && dropLine.prev && dropY >= numLines;)
5474 dropLine = dropLine.prev;
5478 for(;this.line && this.line.prev && this.y >= numLines;)
5480 this.line = this.line.prev;
5485 SetScrollPosition(viewX, viewY * this.space.h);
5487 UpdateCaretPosition(setCaret);
5493 selLine = this.line;
5503 void CenterOnCursor()
5505 int numLines = clientSize.h / this.space.h;
5506 int y = this.y - numLines / 2;
5508 bool figureSyntax = false;
5509 EditLine oldViewLine = viewLine;
5510 if(y > this.lineCount - numLines) y = this.lineCount-numLines;
5515 for(;y < this.viewY; y++)
5517 this.viewLine = this.viewLine.prev;
5518 style.recomputeSyntax = true;
5520 for(;y > this.viewY; y--)
5522 this.viewLine = this.viewLine.next;
5523 figureSyntax = true;
5526 FigureStartSyntaxStates(oldViewLine, false);
5530 SetScrollPosition(this.viewX, viewY * this.space.h);
5531 UpdateCaretPosition(true);
5535 void SetCursorToView()
5546 numLines = clientSize.h / this.space.h;
5550 for(c=0, line = this.viewLine.next; line && c<numLines && (this.viewY < this.lineCount - numLines); line = line.next, c++);
5551 SetScrollPosition(this.viewX, (this.viewY + c) * this.space.h);
5555 EditLine oldLine = this.line;
5556 bool lastOne = false;
5557 EditLine oldViewLine = this.viewLine;
5558 bool figureSyntax = false;
5560 if(this.y >= this.lineCount-1) return;
5562 for(c=0, line = this.line.next; line && c<numLines; line = line.next, c++)
5564 if(this.viewY + numLines < this.lines.count)
5566 this.viewLine = this.viewLine.next;
5568 figureSyntax = true;
5570 else if(c && !lastOne)
5579 FigureStartSyntaxStates(oldViewLine, false);
5580 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5583 SetViewToCursor(false);
5592 if(this.y == 0) return;
5594 numLines = clientSize.h / this.space.h;
5598 for(c=0, line = this.viewLine.prev; line && c<numLines; line = line.prev, c++);
5599 SetScrollPosition(this.viewX, (this.viewY - c) * this.space.h);
5603 EditLine oldLine = this.line;
5605 for(c=0, line = this.line.prev; line && c<numLines; line = line.prev, c++)
5610 if(this.viewLine.prev)
5612 this.viewLine = this.viewLine.prev;
5614 style.recomputeSyntax = true;
5618 this.x = AdjustXPosition(line, caretX, true, null, MAXINT, 0);
5621 SetViewToCursor(false);
5628 if(this.viewLine.prev)
5631 // this.viewLine = this.viewLine.prev;
5634 SetScrollPosition(this.viewX, (this.viewY - 1) * this.space.h);
5641 if(this.viewLine.next)
5644 // this.viewLine = this.viewLine.next;
5647 SetScrollPosition(this.viewX, (this.viewY + 1) * this.space.h);
5654 EditLine l1, l2, line;
5655 int x1, x2, nx1, nx2;
5660 if(!this.selLine) return 0;
5661 if(this.selLine == this.line && this.selX == this.x) return 0;
5662 if(this.selY < this.y)
5669 else if(this.selY > this.y)
5676 else if(this.selX < this.x)
5678 l1 = l2 = this.line;
5684 l1 = l2 = this.line;
5688 nx1 = Min(x1,l1.count);
5689 nx2 = Min(x2,l2.count);
5691 // Find Number of Bytes Needed
5693 for(line = l1; line; line = line.next)
5695 if(line == l1) start = nx1; else start = 0;
5696 if(line == l2) end = nx2; else end = line.count;
5698 if(style.freeCaret && line == l2)
5701 count = Max(x2-Max(x1,l1.count),0);
5703 count = Max(x2-l2.count,0);
5707 if(line == l2) break;
5708 // Add Carriage Return / line Feed
5715 void GetSelPos(EditLine * l1, int *y1, int *x1, EditLine * l2, int * y2, int * x2, bool reorder)
5719 if(!reorder || this.selY < this.y)
5721 if(l1) *l1 = this.selLine;
5722 if(y1) *y1 = this.selY;
5723 if(x1) *x1 = this.selX;
5724 if(l2) *l2 = this.line;
5725 if(y2) *y2 = this.y;
5726 if(x2) *x2 = this.x;
5728 else if(this.selY > this.y)
5730 if(l1) *l1 = this.line;
5731 if(y1) *y1 = this.y;
5732 if(x1) *x1 = this.x;
5733 if(l2) *l2 = this.selLine;
5734 if(y2) *y2 = this.selY;
5735 if(x2) *x2 = this.selX;
5737 else if(this.selX < this.x)
5739 if(l1) *l1 = this.line;
5740 if(y1) *y1 = this.selY;
5741 if(x1) *x1 = this.selX;
5742 if(l2) *l2 = this.line;
5743 if(y2) *y2 = this.y;
5744 if(x2) *x2 = this.x;
5748 if(l1) *l1 = this.line;
5749 if(y1) *y1 = this.y;
5750 if(x1) *x1 = this.x;
5751 if(l2) *l2 = this.line;
5752 if(y2) *y2 = this.selY;
5753 if(x2) *x2 = this.selX;
5758 void SetSelPos(EditLine l1, int y1, int x1, EditLine l2, int y2, int x2)
5760 if(this && (this.selY != y1 || this.selX != x1 || this.y != y2 || this.x != x2))
5762 this.selLine = (EditLine)l1;
5765 this.line = (EditLine)l2;
5769 SetViewToCursor(true);
5773 int GetText(char * text, EditLine _l1, int _y1, int _x1, EditLine _l2, int _y2, int _x2, bool addCr, bool addSpaces)
5775 EditLine l1, l2, line;
5776 int x1, x2, nx1, nx2;
5807 nx1 = Min(x1,l1.count);
5808 nx2 = Min(x2,l2.count);
5811 for(line = l1; line; line = line.next)
5813 if(line == l1) start = nx1; else start = 0;
5814 if(line == l2) end = nx2; else end = line.count;
5817 CopyBytes(text, line.buffer + start, end - start);
5820 numChars += end-start;
5822 if(style.freeCaret && line == l2 && addSpaces)
5825 count = Max(x2-Max(x1,l1.count),0);
5827 count = Max(x2-l2.count,0);
5830 FillBytes(text,' ',count);
5836 if(line == l2) break;
5848 // '\0' terminate Terminate
5855 void GetSel(char * text, bool addCr)
5857 GetText(text, line, y, x, selLine, selY, selX, addCr, true);
5863 this.selLine = this.line;
5873 int size = SelSize();
5876 // Try to allocate memory
5877 ClipBoard clipBoard { };
5878 if(clipBoard.Allocate(size+1))
5880 GetSel(clipBoard.memory, true);
5893 ClipBoard clipBoard { };
5894 if(clipBoard.Load())
5895 PutS(clipBoard.memory);
5906 SetViewToCursor(true);
5911 void DeleteSelection()
5916 SetViewToCursor(true);
5922 void Save(File f, bool lf)
5925 savedAction = undoBuffer.curAction;
5927 for(line = this.lines.first; line; line = line.next)
5929 f.Write(line.buffer, line.count,1);
5932 if(lf) f.Putc('\r');
5938 #define BUFFER_SIZE 16384
5941 undoBuffer.dontRecord++;
5944 char buffer[BUFFER_SIZE];
5948 int count = f.Read(buffer, 1, BUFFER_SIZE-1);
5949 buffer[count] = '\0';
5955 undoBuffer.dontRecord--;
5956 undoBuffer.count = 0;
5957 undoBuffer.curAction = 0;
5958 itemEditUndo.disabled = undoBuffer.curAction == 0;
5959 itemEditRedo.disabled = undoBuffer.curAction == undoBuffer.count;
5962 EditBoxFindResult Find(char * text, bool matchWord, bool matchCase, bool isSearchDown)
5966 bool firstPass = true;
5967 EditBoxFindResult result = found;
5969 if(!this.line) return notFound;
5972 for(line = this.line;;)
5980 line = this.lines.first;
5986 line = this.lines.last;
5987 num = this.lineCount - 1;
5993 string = SearchString(line.buffer, firstPass ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
5995 string = RSearchString(line.buffer,text,firstPass ? (Min(this.x,line.count)-1) : line.count,matchCase,matchWord);
5999 Select((void *)line,num,string - line.buffer,(void *)line,num,string - line.buffer + strlen(text));
6002 if(line == this.line && !firstPass) break;
6020 EditBoxFindResult FindInSelection(char * text, bool matchWord, bool matchCase, EditLine l2, int y2, int x2)
6024 int searchLen = strlen(text);
6025 for(line = (EditLine)this.line, y = this.y; y <= y2; line = line.next, y++)
6027 char * string = SearchString(line.buffer, (y == this.y) ? Min(this.x,line.count) : 0,text,matchCase,matchWord);
6028 if(string && (y < y2 || (string - line.buffer) + searchLen <= x2))
6030 Select((void *)line,y,string - line.buffer,(void *)line,y,string - line.buffer + strlen(text));
6037 bool OnKeyDown(Key key, unichar ch)
6040 //PrintLn("OnKeyDown: code = ", key.code, ", mods = ", key.modifiers, ", ch = ", (int)ch);
6042 if(!NotifyKeyDown(master, this, key, ch))
6052 this.mouseMove = false;
6053 OnLeftButtonUp(0,0,0);
6054 SetViewToCursor(true);
6064 public class EditBoxStream : File
6067 BufferLocation start, sel;
6074 EditBox editBox = this.editBox;
6076 editBox.x = start.x;
6077 editBox.y = start.y;
6078 editBox.line = start.line;
6080 editBox.selX = sel.x;
6081 editBox.selY = sel.y;
6082 editBox.selLine = sel.line;
6084 editBox.SetViewToCursor(true);
6085 //editBox.ComputeColumn();
6089 property EditBox editBox
6097 start.line = value.line;
6101 sel.line = value.selLine;
6104 value.GoToHome(true);
6107 get { return editBox; }
6110 uint Read(byte * buffer, uint size, uint count)
6113 EditBox editBox = this.editBox;
6114 EditLine line = editBox.line;
6120 for(;read < count && line; line = (*&line.next))
6122 int numBytes = Min(count - read, (*&line.count) - x);
6125 memcpy(buffer + read, (*&line.buffer) + x, numBytes);
6130 for(;read < count && x < (*&line.count);)
6132 buffer[read++] = (*&line.buffer)[x++];
6139 buffer[read++] = '\n';
6144 if(x == (*&line.count) && (*&line.next))
6153 editBox.line = editBox.selLine = line;
6154 editBox.x = editBox.selX = x;
6155 editBox.y = editBox.selY = y;
6160 bool Seek(int pos, FileSeekMode mode)
6163 EditBox editBox = this.editBox;
6164 EditLine line = editBox.line;
6166 if(mode == FileSeekMode::start)
6168 pos = pos - this.pos;
6178 for(;read < pos && line; line = (*&line.next))
6180 int numBytes = Min(pos - read, (*&line.count) - x);
6183 read += numBytes; x += numBytes;
6186 /*for(;read < pos && x < (*&line.count);)
6214 for(;read < pos && line; line = (*&line.prev))
6216 int numBytes = Min(pos - read, x);
6222 /*for(;read < pos && x > 0;)
6232 x = (*&(*&line.prev).count);
6248 editBox.line = editBox.selLine = line;
6249 editBox.x = editBox.selX = x;
6250 editBox.y = editBox.selY = y;
6255 bool Puts(char * string)
6257 EditBox editBox = this.editBox;
6258 BufferLocation start { editBox.line, editBox.y, editBox.x };
6262 editBox.AddS(string);
6264 pos.line = editBox.line;
6268 this.start.AdjustAdd(start, pos);
6269 sel.AdjustAdd(start, pos);
6274 // NOTE: BYTE, NOT UNICODE CHARACTER!
6277 EditBox editBox = this.editBox;
6278 BufferLocation start = { editBox.line, editBox.y, editBox.x };
6283 utf8Bytes[numBytes++] = ch;
6284 utf8Bytes[numBytes] = 0;
6285 if(UTF8Validate(utf8Bytes))
6287 editBox.AddCh(UTF8_GET_CHAR(utf8Bytes, numBytes));
6289 pos.line = editBox.line;
6292 this.start.AdjustAdd(start, pos);
6293 sel.AdjustAdd(start, pos);
6300 bool Getc(char * ch)
6302 return Read(ch, 1, 1) ? true : false;
6305 void DeleteBytes(uint count)
6307 EditBox editBox = this.editBox;
6310 BufferLocation pos { editBox.line, editBox.y, editBox.x };
6311 BufferLocation end = pos;
6316 for(;c < count && end.line && end.x < end.line.count;)
6321 if(c < count && end.line && end.line.next)
6323 end.line = end.line.next;
6332 start.AdjustDelete(pos, end);
6333 sel.AdjustDelete(pos, end);
6335 editBox.DelCh(pos.line, pos.y, pos.x, end.line, end.y, end.x, true);