improved blinking cursor

This commit is contained in:
thrust26 2020-11-11 19:54:44 +01:00
parent cec27bb899
commit a6922fb989
1 changed files with 31 additions and 21 deletions

View File

@ -133,7 +133,7 @@ bool EditableWidget::handleText(char text)
if(tryInsertChar(text, _caretPos)) if(tryInsertChar(text, _caretPos))
{ {
_caretPos++; setCaretPos(_caretPos + 1);
sendCommand(EditableWidget::kChangedCmd, 0, _id); sendCommand(EditableWidget::kChangedCmd, 0, _id);
setDirty(); setDirty();
return true; return true;
@ -291,7 +291,7 @@ bool EditableWidget::handleKeyDown(StellaKey key, StellaMod mod)
{ {
// Put caret at last difference // Put caret at last difference
myUndoHandler->lastDiff(_editString, oldString); myUndoHandler->lastDiff(_editString, oldString);
_caretPos = myUndoHandler->lastDiff(_editString, oldString); setCaretPos(myUndoHandler->lastDiff(_editString, oldString));
_selectSize = 0; _selectSize = 0;
sendCommand(EditableWidget::kChangedCmd, key, _id); sendCommand(EditableWidget::kChangedCmd, key, _id);
} }
@ -342,22 +342,7 @@ void EditableWidget::drawCaretSelection()
if (!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus) if (!_editable || !isVisible() || !_boss->isVisible() || !_hasFocus)
return; return;
if(_caretEnabled) // Draw the selection
{
FBSurface& s = _boss->dialog().surface();
const Common::Rect& editRect = getEditRect();
int x = editRect.x();
int y = editRect.y();
x += getCaretOffset();
x += _x;
y += _y;
s.vLine(x, y + 2, y + editRect.h() - 2, kTextColorHi);
s.vLine(x-1, y + 2, y + editRect.h() - 2, kTextColorHi);
clearDirty();
}
if(_selectSize) if(_selectSize)
{ {
FBSurface& s = _boss->dialog().surface(); FBSurface& s = _boss->dialog().surface();
@ -393,6 +378,24 @@ void EditableWidget::drawCaretSelection()
s.drawString(_font, text, x, y + 1, w, h, s.drawString(_font, text, x, y + 1, w, h,
kTextColorInv, TextAlign::Left, 0, false); kTextColorInv, TextAlign::Left, 0, false);
} }
// Draw the caret
if(_caretEnabled ^ (_selectSize != 0))
{
FBSurface& s = _boss->dialog().surface();
const Common::Rect& editRect = getEditRect();
int x = editRect.x();
int y = editRect.y();
ColorId color = _caretEnabled ? kTextColorHi : kTextColorInv;
x += getCaretOffset();
x += _x;
y += _y;
s.vLine(x, y + 1, y + editRect.h() - 3, color);
s.vLine(x - 1, y + 1, y + editRect.h() - 3, color);
clearDirty();
}
} }
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -401,6 +404,9 @@ bool EditableWidget::setCaretPos(int newPos)
assert(newPos >= 0 && newPos <= int(_editString.size())); assert(newPos >= 0 && newPos <= int(_editString.size()));
_caretPos = newPos; _caretPos = newPos;
_caretTimer = 0;
_caretEnabled = true;
return adjustOffset(); return adjustOffset();
} }
@ -410,6 +416,8 @@ bool EditableWidget::moveCaretPos(int direction)
if(setCaretPos(_caretPos + direction)) if(setCaretPos(_caretPos + direction))
{ {
_selectSize -= direction; _selectSize -= direction;
_caretTimer = 0;
_caretEnabled = true;
return true; return true;
} }
return false; return false;
@ -487,6 +495,7 @@ bool EditableWidget::killChar(int direction, bool addEdit)
{ {
myUndoHandler->endChars(_editString); myUndoHandler->endChars(_editString);
_editString.erase(_caretPos, 1); _editString.erase(_caretPos, 1);
setCaretPos(_caretPos);
if(addEdit) if(addEdit)
myUndoHandler->doo(_editString); myUndoHandler->doo(_editString);
@ -591,7 +600,7 @@ bool EditableWidget::moveWord(int direction, bool select)
if(select) if(select)
_selectSize++; _selectSize++;
} }
_caretPos = currentPos; setCaretPos(currentPos);
handled = true; handled = true;
} }
else if(direction == +1) // move to first character of next word else if(direction == +1) // move to first character of next word
@ -610,7 +619,7 @@ bool EditableWidget::moveWord(int direction, bool select)
if(select) if(select)
_selectSize--; _selectSize--;
} }
_caretPos = currentPos; setCaretPos(currentPos);
handled = true; handled = true;
} }
@ -665,6 +674,7 @@ bool EditableWidget::killSelectedText(bool addEdit)
_selectSize = -_selectSize; _selectSize = -_selectSize;
} }
_editString.erase(_caretPos, _selectSize); _editString.erase(_caretPos, _selectSize);
setCaretPos(_caretPos);
_selectSize = 0; _selectSize = 0;
if(addEdit) if(addEdit)
myUndoHandler->doo(_editString); myUndoHandler->doo(_editString);
@ -724,7 +734,7 @@ bool EditableWidget::pasteSelectedText()
_editString.insert(_caretPos, buf.str()); _editString.insert(_caretPos, buf.str());
// position cursor at the end of pasted text // position cursor at the end of pasted text
_caretPos += int(buf.str().length()); setCaretPos(_caretPos + int(buf.str().length()));
if(selected || !pasted.empty()) if(selected || !pasted.empty())
{ {