From a5873ae2b89d7ac8fc650a0f75778f8dad07a102 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Tue, 30 Sep 2014 11:49:37 +0200 Subject: Following marker. --- plugingui/lineedit.cc | 75 +++++++++++++++++++++++++++++++++++++++++++-------- plugingui/lineedit.h | 9 +++++++ 2 files changed, 73 insertions(+), 11 deletions(-) diff --git a/plugingui/lineedit.cc b/plugingui/lineedit.cc index 9c14ab6..46354bb 100644 --- a/plugingui/lineedit.cc +++ b/plugingui/lineedit.cc @@ -30,6 +30,8 @@ #include "window.h" +#include + #include #define BORDER 10 @@ -38,6 +40,7 @@ GUI::LineEdit::LineEdit(Widget *parent) : GUI::Widget(parent) { pos = 0; + offsetpos = 0; setReadOnly(false); box.topLeft = new Image(":widget_tl.png"); @@ -72,7 +75,8 @@ bool GUI::LineEdit::readOnly() void GUI::LineEdit::setText(std::string text) { _text = text; - if(_text.size() < pos) pos = text.size(); + pos = text.size(); +// if(_text.size() < pos) pos = text.size(); repaintEvent(NULL); textChanged(); @@ -87,14 +91,16 @@ void GUI::LineEdit::buttonEvent(ButtonEvent *e) { if(readOnly()) return; + if(e->direction == 1) { - for(int i = 0; i < (int)_text.length(); i++) { - if(e->x < (int)(font.textWidth(_text.substr(0, i)) + BORDER)) { - pos = i; + for(int i = 0; i < (int)_visibletext.length(); i++) { + if(e->x < (int)(font.textWidth(_visibletext.substr(0, i)) + BORDER)) { + printf("i, Offset: %d, %d\n", i, offsetpos); + pos = i + offsetpos; break; } } - if(e->x >= (int)(font.textWidth(_text) + BORDER)) pos = _text.length(); +// if(e->x >= (int)(font.textWidth(_visibletext) + BORDER)) pos = _visibletext.length(); repaintEvent(NULL); } } @@ -111,7 +117,9 @@ void GUI::LineEdit::keyEvent(GUI::KeyEvent *e) if(e->keycode == GUI::KeyEvent::KEY_LEFT) { if(pos) pos--; - + if(offsetpos >= pos) walkstate = WALK_LEFT; +// else walkstate = NOOP; + } else if(e->keycode == GUI::KeyEvent::KEY_HOME) { pos = 0; @@ -120,7 +128,9 @@ void GUI::LineEdit::keyEvent(GUI::KeyEvent *e) } else if(e->keycode == GUI::KeyEvent::KEY_RIGHT) { if(pos < _text.length()) pos++; - + if(offsetpos + _visibletext.length() <= pos) walkstate = WALK_RIGHT; +// else walkstate = NOOP; + } else if(e->keycode == GUI::KeyEvent::KEY_DELETE) { if(pos < _text.length()) { std::string t = _text.substr(0, pos); @@ -144,6 +154,7 @@ void GUI::LineEdit::keyEvent(GUI::KeyEvent *e) _text = pre + e->text + post; change = true; pos++; + } else if(e->keycode == GUI::KeyEvent::KEY_ENTER) { if(handler) handler(ptr); } @@ -165,18 +176,60 @@ void GUI::LineEdit::repaintEvent(GUI::RepaintEvent *e) p.drawBox(0, 0, &box, w, h); p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); - p.drawText(BORDER - 4 + 3, height()/2+5 + 1 + 1 + 1, font, _text); + + if(walkstate == WALK_LEFT) { + _visibletext = _text.substr(pos, std::string::npos); + offsetpos = pos; + } + else if(walkstate == WALK_RIGHT) { + int d = (offsetpos < _text.length()) ? 1 : 0; + _visibletext = _text.substr(offsetpos + d); + offsetpos = offsetpos + d; + } + else { + _visibletext = _text; + offsetpos = 0; + } + while(true) { + int textwidth = font.textWidth(_visibletext); + if(textwidth > w - BORDER - 4 + 3) { + if(walkstate == WALK_LEFT) { + _visibletext = _visibletext.substr(0, _visibletext.length()-1); + } + else if(walkstate == WALK_RIGHT) { + _visibletext = _visibletext.substr(0, _visibletext.length()-1); + } + else { + if(offsetpos < pos) { + _visibletext = _visibletext.substr(1); + offsetpos++; + } + else { + _visibletext = _visibletext.substr(0, _visibletext.length() - 1); + } + } + } + else { + DEBUG(lienedit, "Full text: '%s'\n", _text.c_str()); + DEBUG(lineedit, "Drawing text in lineedit '%s'\n", _visibletext.c_str()); + DEBUG(lineedit, "Offset, pos: %d, %d\n", offsetpos, pos); + break; + } + } + + walkstate = NOOP; + + p.drawText(BORDER - 4 + 3, height()/2+5 + 1 + 1 + 1, font, _visibletext); if(readOnly()) return; if(hasKeyboardFocus()) { - size_t px = font.textWidth(_text.substr(0, pos)); +// size_t px = font.textWidth(_text.substr(0, pos - offsetpos)); + size_t px = font.textWidth(_visibletext.substr(0, pos - offsetpos)); //p.setColour(Colour(0.8)); p.drawLine(px + BORDER - 1 - 4 + 3, 6, px + BORDER - 1 - 4 + 3, height() - 7); } - - DEBUG(lineedit, "Weeeh!\n"); } #ifdef TEST_LINEEDIT diff --git a/plugingui/lineedit.h b/plugingui/lineedit.h index 2c37522..f85e9bd 100644 --- a/plugingui/lineedit.h +++ b/plugingui/lineedit.h @@ -65,6 +65,15 @@ private: std::string _text; size_t pos; + std::string _visibletext; + size_t offsetpos; + + enum state_t { + NOOP = 0, + WALK_LEFT = 1, + WALK_RIGHT = 2 + }; + state_t walkstate; bool readonly; -- cgit v1.2.3