From 2ec1f463ad469d12a5c743ee40f6a96c452eaecf Mon Sep 17 00:00:00 2001 From: thrust26 Date: Wed, 4 Nov 2020 20:18:41 +0100 Subject: [PATCH] finalized EditableWidget enabled selection drawing on all derived widgets --- src/debugger/gui/DataGridWidget.cxx | 2 +- src/debugger/gui/RomListWidget.cxx | 2 +- src/gui/CheckListWidget.cxx | 2 +- src/gui/EditTextWidget.cxx | 6 +- src/gui/EditableWidget.cxx | 106 ++++++++++++++-------------- src/gui/EditableWidget.hxx | 3 +- src/gui/PopUpWidget.cxx | 2 +- src/gui/StringListWidget.cxx | 2 +- 8 files changed, 62 insertions(+), 63 deletions(-) diff --git a/src/debugger/gui/DataGridWidget.cxx b/src/debugger/gui/DataGridWidget.cxx index ad918f65f..ad7548594 100644 --- a/src/debugger/gui/DataGridWidget.cxx +++ b/src/debugger/gui/DataGridWidget.cxx @@ -646,7 +646,7 @@ void DataGridWidget::drawWidget(bool hilite) // Only draw the caret while editing, and if it's in the current viewport if(_editMode) - drawCaret(); + drawCaretSelection(); // Draw the scrollbar if(_scrollBar) diff --git a/src/debugger/gui/RomListWidget.cxx b/src/debugger/gui/RomListWidget.cxx index 07bbbf668..2964e4990 100644 --- a/src/debugger/gui/RomListWidget.cxx +++ b/src/debugger/gui/RomListWidget.cxx @@ -547,7 +547,7 @@ void RomListWidget::drawWidget(bool hilite) s.drawString(_font, editString(), _x + r.x(), ypos, r.w(), textColor, TextAlign::Left, -_editScrollOffset, false); - drawCaret(); + drawCaretSelection(); } else { diff --git a/src/gui/CheckListWidget.cxx b/src/gui/CheckListWidget.cxx index 08852a4f8..079989376 100644 --- a/src/gui/CheckListWidget.cxx +++ b/src/gui/CheckListWidget.cxx @@ -145,7 +145,7 @@ void CheckListWidget::drawWidget(bool hilite) (!_useScrollbar || ((_selectedItem >= _scrollBar->_currentPos) && (_selectedItem < _scrollBar->_currentPos + _rows)))) - drawCaret(); + drawCaretSelection(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/gui/EditTextWidget.cxx b/src/gui/EditTextWidget.cxx index 35e6eaf04..6105e70d3 100644 --- a/src/gui/EditTextWidget.cxx +++ b/src/gui/EditTextWidget.cxx @@ -108,10 +108,8 @@ void EditTextWidget::drawWidget(bool hilite) : onTop && isEnabled() ? _textcolor : kColor, TextAlign::Left, scrollOffset(), !isEditable()); - // Draw selected text - drawSelection(); - // Draw the caret - drawCaret(); + // Draw the caret and selection + drawCaretSelection(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/gui/EditableWidget.cxx b/src/gui/EditableWidget.cxx index 6c0efaa68..57514dc99 100644 --- a/src/gui/EditableWidget.cxx +++ b/src/gui/EditableWidget.cxx @@ -159,12 +159,12 @@ bool EditableWidget::handleControlKeys(StellaKey key, StellaMod mod) if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id); break; - case KBDK_K: // TODO + case KBDK_K: handled = killLine(+1); if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id); break; - case KBDK_U: // TODO + case KBDK_U: handled = killLine(-1); if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id); break; @@ -284,13 +284,17 @@ bool EditableWidget::handleNormalKeys(StellaKey key) break; case KBDK_BACKSPACE: - handled = killChar(-1); + handled = killSelectedText(); + if(!handled) + handled = killChar(-1); if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id); break; case KBDK_DELETE: case KBDK_KP_PERIOD: - handled = killChar(+1); + handled = killSelectedText(); + if(!handled) + handled = killChar(+1); if(handled) sendCommand(EditableWidget::kChangedCmd, key, _id); break; @@ -342,7 +346,7 @@ int EditableWidget::getCaretOffset() const } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void EditableWidget::drawCaret() +void EditableWidget::drawCaretSelection() { // Only draw if item is visible if (!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus) @@ -360,44 +364,36 @@ void EditableWidget::drawCaret() FBSurface& s = _boss->dialog().surface(); s.vLine(x, y + 2, y + editRect.h() - 2, kTextColorHi); s.vLine(x-1, y + 2, y + editRect.h() - 2, kTextColorHi); -} -// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -void EditableWidget::drawSelection() -{ - // Only draw if item is visible - if(!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus - || !_selectSize) - return; - - FBSurface& s = _boss->dialog().surface(); - string text = selectString(); - const Common::Rect& editRect = getEditRect(); - int x = editRect.x(); - int y = editRect.y(); - int w = editRect.w(); - int h = editRect.h(); - int wt = int(text.length()) * _font.getMaxCharWidth() + 1; - int dx = selectStartPos() * _font.getMaxCharWidth() - _editScrollOffset; - - if(dx < 0) + if(_selectSize) { - // selected text starts left of displayed rect - text = text.substr(-(dx - 1) / _font.getMaxCharWidth()); - wt += dx; - dx = 0; + string text = selectString(); + x = editRect.x(); + y = editRect.y(); + int w = editRect.w(); + int h = editRect.h(); + int wt = int(text.length()) * _font.getMaxCharWidth() + 1; + int dx = selectStartPos() * _font.getMaxCharWidth() - _editScrollOffset; + + if(dx < 0) + { + // selected text starts left of displayed rect + text = text.substr(-(dx - 1) / _font.getMaxCharWidth()); + wt += dx; + dx = 0; + } + else + x += dx; + // limit selection to the right of displayed rect + w = std::min(w - dx + 1, wt); + + x += _x; + y += _y; + + s.fillRect(x - 1, y + 1, w + 1, h - 3, kTextColorHi); + s.drawString(_font, text, x, y + 1, w, h, + kTextColorInv, TextAlign::Left, 0, false); } - else - x += dx; - // limit selection to the right of displayed rect - w = std::min(w - dx + 1, wt); - - x += _x; - y += _y; - - s.fillRect(x - 1, y + 1, w + 1, h - 3, kTextColorHi); - s.drawString(_font, text, x, y + 1, w, h, - kTextColorInv, TextAlign::Left, 0, false); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - @@ -466,25 +462,22 @@ int EditableWidget::scrollOffset() // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - bool EditableWidget::killChar(int direction) { - bool handled = killSelectedText(); + bool handled = false; - if(!handled) + if(direction == -1) // Delete previous character (backspace) { - if(direction == -1) // Delete previous character (backspace) - { - if(_caretPos > 0) - { - _caretPos--; - _editString.erase(_caretPos, 1); - handled = true; - } - } - else if(direction == 1) // Delete next character (delete) + if(_caretPos > 0) { + _caretPos--; _editString.erase(_caretPos, 1); handled = true; } } + else if(direction == 1) // Delete next character (delete) + { + _editString.erase(_caretPos, 1); + handled = true; + } return handled; } @@ -503,6 +496,9 @@ bool EditableWidget::killLine(int direction) killChar(-1); handled = true; + // remove selection for removed text + if(_selectSize < 0) + _selectSize = 0; } } else if(direction == 1) // erase from current position to end of line @@ -514,6 +510,9 @@ bool EditableWidget::killLine(int direction) killChar(+1); handled = true; + // remove selection for removed text + if(_selectSize > 0) + _selectSize = 0; } } @@ -546,6 +545,9 @@ bool EditableWidget::killLastWord() killChar(-1); handled = true; + // remove selection for removed word + if(_selectSize < 0) + _selectSize = std::min(_selectSize + count, 0); } return handled; diff --git a/src/gui/EditableWidget.hxx b/src/gui/EditableWidget.hxx index 46cfb1855..95427582f 100644 --- a/src/gui/EditableWidget.hxx +++ b/src/gui/EditableWidget.hxx @@ -72,7 +72,7 @@ class EditableWidget : public Widget, public CommandSender virtual Common::Rect getEditRect() const = 0; virtual int getCaretOffset() const; - void drawCaret(); + void drawCaretSelection(); bool setCaretPos(int newPos); bool moveCaretPos(int direction); bool adjustOffset(); @@ -82,7 +82,6 @@ class EditableWidget : public Widget, public CommandSender string& editString() { return _editString; } const string selectString() const; void resetSelection() { _selectSize = 0; } - void drawSelection(); int scrollOffset(); private: diff --git a/src/gui/PopUpWidget.cxx b/src/gui/PopUpWidget.cxx index c41cfdc00..95f2ac46b 100644 --- a/src/gui/PopUpWidget.cxx +++ b/src/gui/PopUpWidget.cxx @@ -297,7 +297,7 @@ void PopUpWidget::drawWidget(bool hilite) align, editable ? -_editScrollOffset : 0, !editable); if(editable) - drawCaret(); + drawCaretSelection(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - diff --git a/src/gui/StringListWidget.cxx b/src/gui/StringListWidget.cxx index f388aaeff..4171632cb 100644 --- a/src/gui/StringListWidget.cxx +++ b/src/gui/StringListWidget.cxx @@ -112,7 +112,7 @@ void StringListWidget::drawWidget(bool hilite) (!_useScrollbar || ((_selectedItem >= _scrollBar->_currentPos) && (_selectedItem < _scrollBar->_currentPos + _rows)))) - drawCaret(); + drawCaretSelection(); } // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -