Patch #732707: EditTextWidget: long string support

svn-id: r7359
This commit is contained in:
Max Horn 2003-05-05 16:10:19 +00:00
parent a0c98d1f14
commit 898387e1ac
4 changed files with 71 additions and 14 deletions

View File

@ -32,6 +32,11 @@ EditTextWidget::EditTextWidget(Dialog *boss, int x, int y, int w, int h, const S
_caretTime = 0;
_pos = _label.size();
NewGui *gui = _boss->getGui();
_labelOffset = (gui->getStringWidth(_label) - (_w - 6));
if (_labelOffset < 0)
_labelOffset = 0;
}
void EditTextWidget::handleTickle() {
@ -67,8 +72,10 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
break;
case 27: // escape
_label = _backupString;
if (_pos >= _label.size())
_pos = _label.size() - 1;
_pos = _label.size() - 1;
_labelOffset = (_boss->getGui()->getStringWidth(_label) - (_w-6));
if (_labelOffset < 0)
_labelOffset = 0;
_boss->releaseFocus();
dirty = true;
break;
@ -84,18 +91,24 @@ bool EditTextWidget::handleKeyDown(uint16 ascii, int keycode, int modifiers) {
dirty = true;
break;
case 256 + 20: // left arrow
if (_pos > 0)
if (_pos > 0) {
_pos--;
dirty = adjustOffset();
}
break;
case 256 + 19: // right arrow
if (_pos < _label.size())
if (_pos < _label.size()) {
_pos++;
dirty = adjustOffset();
}
break;
case 256 + 22: // home
_pos = 0;
dirty = adjustOffset();
break;
case 256 + 23: // end
_pos = _label.size();
dirty = adjustOffset();
break;
default:
if (isprint((char)ascii)) {
@ -123,8 +136,19 @@ void EditTextWidget::drawWidget(bool hilite) {
gui->vline(_x + _w - 1, _y, _y + _h - 1, gui->_shadowcolor);
// Draw the text
_align = (gui->getStringWidth(_label) > _w - 6) ? kTextAlignRight : kTextAlignLeft;
gui->drawString(_label, _x + 2, _y + 3, _w - 6, gui->_textcolor, _align);
adjustOffset();
gui->drawString(_label, _x + 2, _y + 3, _w - 6, gui->_textcolor, kTextAlignLeft, -_labelOffset);
}
int EditTextWidget::getCaretPos() {
NewGui *gui = _boss->getGui();
int caretpos = 0;
for (int i = 0; i < _pos; i++)
caretpos += gui->getCharWidth(_label[i]);
caretpos -= _labelOffset;
return caretpos;
}
void EditTextWidget::drawCaret(bool erase) {
@ -138,12 +162,7 @@ void EditTextWidget::drawCaret(bool erase) {
int x = _x + _boss->getX() + 2;
int y = _y + _boss->getY() + 1;
int width = 0;
for (int i = 0; i < _pos; i++)
width += gui->getCharWidth(_label[i]);
if (gui->getStringWidth(_label) - (_w - 6) > 0)
width -= gui->getStringWidth(_label) - (_w - 6);
int width = getCaretPos();
x += width;
gui->vline(x, y, y + kLineHeight, color);
@ -151,3 +170,36 @@ void EditTextWidget::drawCaret(bool erase) {
_caretVisible = !erase;
}
bool EditTextWidget::adjustOffset() {
// check if the caret is still within the textbox; if it isn't,
// adjust _labelOffset
int caretpos = getCaretPos();
if (caretpos < 0) {
// scroll left
_labelOffset += caretpos;
return true;
}
else if (caretpos >= _w - 6)
{
// scroll right
_labelOffset -= (_w - 6 - caretpos);
return true;
}
else if (_labelOffset > 0)
{
int width = _boss->getGui()->getStringWidth(_label);
if (width - _labelOffset < (_w - 6)) {
// scroll right
_labelOffset = (width - (_w - 6));
if (_labelOffset < 0)
_labelOffset = 0;
}
}
return false;
}

View File

@ -34,6 +34,7 @@ protected:
bool _caretVisible;
uint32 _caretTime;
int _pos;
int _labelOffset;
public:
EditTextWidget(Dialog *boss, int x, int y, int w, int h, const String &text);
@ -47,6 +48,9 @@ protected:
void drawWidget(bool hilite);
void drawCaret(bool erase);
void lostFocusWidget() { _backupString = _label; drawCaret(true); }
int getCaretPos();
bool adjustOffset();
};
#endif

View File

@ -429,13 +429,14 @@ int NewGui::getCharWidth(byte c) {
return guifont[c+6];
}
void NewGui::drawString(const String &str, int x, int y, int w, NewGuiColor color, int align) {
void NewGui::drawString(const String &str, int x, int y, int w, NewGuiColor color, int align, int deltax) {
const int leftX = x, rightX = x + w;
int width = getStringWidth(str);
if (align == kTextAlignCenter)
x = x + (w - width - 1)/2;
else if (align == kTextAlignRight)
x = x + w - width;
x += deltax;
for (int i = 0; i < str.size(); ++i) {
w = getCharWidth(str[i]);

View File

@ -135,7 +135,7 @@ public:
void drawChar(byte c, int x, int y, NewGuiColor color);
int getStringWidth(const String &str);
int getCharWidth(byte c);
void drawString(const String &str, int x, int y, int w, NewGuiColor color, int align = kTextAlignLeft);
void drawString(const String &str, int x, int y, int w, NewGuiColor color, int align = kTextAlignLeft, int deltax = 0);
void blitFromBuffer(int x, int y, int w, int h, const byte *buf, int pitch);
void blitToBuffer(int x, int y, int w, int h, byte *buf, int pitch);