Added some basic line editing to the EditText widget

svn-id: r6393
This commit is contained in:
Oliver Kiehl 2003-01-10 21:33:42 +00:00
parent 23a02b712a
commit ebdf89e418
4 changed files with 46 additions and 5 deletions

View File

@ -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)

View File

@ -110,7 +110,9 @@ public:
}
void deleteLastChar();
void deleteChar(int p);
void clear();
void insertChar(char c, int p);
void toLowercase();
void toUppercase();

View File

@ -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;

View File

@ -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);