From ebdf89e4184240a752061c7c8d5450dbca809f39 Mon Sep 17 00:00:00 2001 From: Oliver Kiehl Date: Fri, 10 Jan 2003 21:33:42 +0000 Subject: Added some basic line editing to the EditText widget svn-id: r6393 --- common/str.cpp | 21 +++++++++++++++++++++ common/str.h | 2 ++ gui/EditTextWidget.cpp | 27 ++++++++++++++++++++++----- gui/EditTextWidget.h | 1 + 4 files changed, 46 insertions(+), 5 deletions(-) diff --git a/common/str.cpp b/common/str.cpp index e8e73a32a2..ffb8c298b0 100644 --- a/common/str.cpp +++ b/common/str.cpp @@ -160,6 +160,16 @@ void String::deleteLastChar() { } } +void String::deleteChar(int p) +{ + if (p >= 0 && p < _len) { + ensureCapacity(_len - 1, true); + while (p++ < _len) + _str[p-1] = _str[p]; + _len--; + } +} + void String::clear() { if (_capacity) { @@ -172,6 +182,17 @@ void String::clear() } } +void String::insertChar(char c, int p) +{ + if (p >= 0 && p <= _len) { + ensureCapacity(++_len, true); + for (int i = _len; i > p; i--) { + _str[i] = _str[i-1]; + } + _str[p] = c; + } +} + void String::toLowercase() { if (_str == 0 || _len == 0) diff --git a/common/str.h b/common/str.h index 4c2a985d46..8ff84c3b5d 100644 --- a/common/str.h +++ b/common/str.h @@ -110,7 +110,9 @@ public: } void deleteLastChar(); + void deleteChar(int p); void clear(); + void insertChar(char c, int p); void toLowercase(); void toUppercase(); diff --git a/gui/EditTextWidget.cpp b/gui/EditTextWidget.cpp index 10caf64cd7..25326eb868 100644 --- a/gui/EditTextWidget.cpp +++ b/gui/EditTextWidget.cpp @@ -31,6 +31,8 @@ EditTextWidget::EditTextWidget(Dialog *boss, int x, int y, int w, int h, const S _caretVisible = false; _caretTime = 0; + + _pos = _label.size(); } void EditTextWidget::handleTickle() @@ -74,19 +76,33 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) break; case 8: // backspace _label.deleteLastChar(); + if (_pos > 0) + _pos--; + dirty = true; + break; + case 127: // delete + _label.deleteChar(_pos); dirty = true; break; case 256+20: // left arrow + if (_pos > 0) + _pos--; break; case 256+19: // right arrow + if (_pos < _label.size()) + _pos++; + break; break; case 256+22: // home + _pos = 0; break; case 256+23: // end + _pos = _label.size(); break; default: if (isprint((char)ascii)) { - _label += (char)ascii; + _label.insertChar((char)ascii, _pos++); + //_label += (char)ascii; dirty = true; } else { handled = false; @@ -123,12 +139,13 @@ void EditTextWidget::drawCaret(bool erase) NewGui *gui = _boss->getGui(); int16 color = erase ? gui->_bgcolor : gui->_textcolorhi; - int x = _x + _boss->getX() + 3; + int x = _x + _boss->getX() + 2; int y = _y + _boss->getY() + 1; - // TODO - once we support "real editing" (i.e. caret can be at any spot), - // x should be calculated based on the current caret position. - int width = gui->getStringWidth(_label); + int width = 0; + for (int i = 0; i < _pos; i++) + width += gui->getCharWidth(_label[i]); + if (width > _w-6) width = _w-6; x += width; diff --git a/gui/EditTextWidget.h b/gui/EditTextWidget.h index 9795bc1012..fa4b4dbc20 100644 --- a/gui/EditTextWidget.h +++ b/gui/EditTextWidget.h @@ -33,6 +33,7 @@ protected: String _backupString; bool _caretVisible; uint32 _caretTime; + int _pos; public: EditTextWidget(Dialog *boss, int x, int y, int w, int h, const String &text); -- cgit v1.2.3