From 25bc5ef4b9ff7cf51a407dca7bd52eec7eda87f7 Mon Sep 17 00:00:00 2001 From: Grim Maple Date: Fri, 2 Dec 2022 18:45:15 +0300 Subject: [PATCH] Make Editors respect onKeyEvent --- src/dlangui/widgets/editors.d | 74 ++++++++++++++++++----------------- 1 file changed, 39 insertions(+), 35 deletions(-) diff --git a/src/dlangui/widgets/editors.d b/src/dlangui/widgets/editors.d index a2741d12..9d66e8c3 100644 --- a/src/dlangui/widgets/editors.d +++ b/src/dlangui/widgets/editors.d @@ -385,10 +385,10 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction /// Override for EditBox void wordWrapRefresh(){return;} - + /// To hold _scrollpos.x toggling between normal and word wrap mode int previousXScrollPos; - + protected bool _wordWrap; /// true if word wrap mode is set @property bool wordWrap() { @@ -416,7 +416,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction /// Characters at which content is split for word wrap mode dchar[] splitChars = [' ', '-', '\t']; - + /// Divides up a string for word wrapping, sets info in _span dstring[] wrapLine(dstring str, int lineNumber) { FontRef font = font(); @@ -473,17 +473,17 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction while (true) { int index = to!int(str.indexOfAny(splitChars, startIndex)); - + if (index == -1) { parts ~= str[startIndex .. $]; //Log.d("Explode output: ", parts); return parts; } - + dstring word = str[startIndex .. index]; dchar nextChar = (str[index .. index + 1])[0]; - + import std.ascii:isWhite; if (isWhite(nextChar)) { @@ -497,11 +497,11 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction startIndex = index + 1; } } - + /// information about line span into several lines - in word wrap mode protected LineSpan[] _span; protected LineSpan[] _spanCache; - + /// Finds good visual wrapping point for string int findWrapPoint(dstring text) { @@ -519,7 +519,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction } } } - + /// Calls measureText for word wrap int measureWrappedText(dstring text) { @@ -532,7 +532,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction return measuredWidths[$-1]; return 0; } - + /// Returns number of visible wraps up to a line (not including the first wrapLines themselves) int wrapsUpTo(int line) { @@ -544,7 +544,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction }); return sum; } - + /// Returns LineSpan for line based on actual line number LineSpan getSpan(int lineNumber) { @@ -556,7 +556,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction }); return lineSpan; } - + /// Based on a TextPosition, finds which wrapLine it is on for its current line int findWrapLine(TextPosition textPos) { @@ -569,13 +569,13 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction return curWrapLine; curPosition -= curSpan.wrapPoints[curWrapLine].wrapPos; if (curPosition < 0) - { + { return curWrapLine; } curWrapLine++; } } - + /// Simple way of iterating through _span void lineSpanIterate(void delegate(LineSpan curSpan) iterator) { @@ -1290,7 +1290,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction //In word wrap mode, set by caretRect so ensureCaretVisible will know when to scroll protected int caretHeightOffset; - + /// returns cursor rectangle protected Rect caretRect() { Rect caretRc = textPosToClient(_caretPos); @@ -1458,22 +1458,22 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction _textToHighlightOptions = textToHighlightOptions; invalidate(); } - + /// Used instead of using clientToTextPos for mouse input when in word wrap mode protected TextPosition wordWrapMouseOffset(int x, int y) { if(_span.length == 0) return clientToTextPos(Point(x,y)); int selectedVisibleLine = y / _lineHeight; - + LineSpan _curSpan; - + int wrapLine = 0; int curLine = 0; bool foundWrap = false; int accumulativeWidths = 0; int curWrapOfSpan = 0; - + lineSpanIterate(delegate(LineSpan curSpan){ while (!foundWrap) { @@ -1497,7 +1497,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction } curWrapOfSpan = 0; }); - + int fakeLineHeight = curLine * _lineHeight; return clientToTextPos(Point(x + accumulativeWidths,fakeLineHeight)); } @@ -2103,6 +2103,8 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction /// handle keys override bool onKeyEvent(KeyEvent event) { //Log.d("onKeyEvent ", event.action, " ", event.keyCode, " flags ", event.flags); + if(super.onKeyEvent(event)) + return true; if (focused) startCaretBlinking(); cancelHoverTimer(); bool ctrlOrAltPressed = !!(event.flags & KeyFlag.Control); //(event.flags & (KeyFlag.Control /* | KeyFlag.Alt */)); @@ -2133,7 +2135,7 @@ class EditWidgetBase : ScrollWidgetBase, EditableContentListener, MenuItemAction //if (event.keyCode == KeyCode.RETURN && !readOnly && !_content.multiline) { // return true; //} - return super.onKeyEvent(event); + return true; } /// Handle Ctrl + Left mouse click on text @@ -2311,7 +2313,7 @@ class EditLine : EditWidgetBase { protected Point _measuredTextToSetWidgetSize; protected dstring _textToSetWidgetSize = "aaaaa"d; - + @property void textToSetWidgetSize(dstring newText) { _textToSetWidgetSize = newText; requestLayout(); @@ -2320,7 +2322,7 @@ class EditLine : EditWidgetBase { @property dstring textToSetWidgetSize() { return _textToSetWidgetSize; } - + protected int[] _measuredTextToSetWidgetSizeWidths; protected dchar _passwordChar = 0; @@ -2449,6 +2451,8 @@ class EditLine : EditWidgetBase { /// handle keys override bool onKeyEvent(KeyEvent event) { + if(super.onKeyEvent(event)) + return true; if (enterKey.assigned) { if (event.keyCode == KeyCode.RETURN && event.modifiers == 0) { if (event.action == KeyAction.KeyDown) @@ -2459,7 +2463,7 @@ class EditLine : EditWidgetBase { } } } - return super.onKeyEvent(event); + return true; } /// process mouse event; return true if event is processed by widget. @@ -2537,16 +2541,16 @@ class SpinCtrl : HorizontalLayout { TextWidget label; int min, max; - + private EditLine linEdit; private Button butUp, butDown; - + @property int value() { return linEdit.text.to!int; } @property void value(int val) { linEdit.text = val.to!dstring; } - + override @property bool enabled() { return linEdit.enabled; } alias enabled = Widget.enabled; @property void enabled(bool status) { @@ -2634,10 +2638,10 @@ class SpinCtrl : HorizontalLayout { linEdit.text = (val - 1).to!dstring; return true; }; - + enabled = true; } - + } /// multiline editor @@ -2687,14 +2691,14 @@ class EditBox : EditWidgetBase { { _needRewrap = true; } - + override @property int fontSize() const { return super.fontSize(); } override @property Widget fontSize(int size) { // Need to rewrap if fontSize changed _needRewrap = true; return super.fontSize(size); } - + override protected int lineCount() { return _content.length; } @@ -3545,7 +3549,7 @@ class EditBox : EditWidgetBase { buf.fillRect(rc, color); } } - + /// Used in place of directly calling buf.fillRect in word wrap mode void wordWrapFillRect(DrawBuf buf, int line, Rect lineToDivide, uint color) { @@ -3828,10 +3832,10 @@ class EditBox : EditWidgetBase { //TODO: Don't erase spans which have not been modified, cache them _span = []; } - + private bool _needRewrap = true; private int lastStartingLine; - + override protected void drawClient(DrawBuf buf) { // update matched braces if (!content.findMatchedBraces(_caretPos, _matchingBraces)) { @@ -3840,7 +3844,7 @@ class EditBox : EditWidgetBase { } Rect rc = _clientRect; - + if (_contentChanged) _needRewrap = true; if (lastStartingLine != _firstVisibleLine)