diff options
author | Jonas Suhr Christensen <jsc@umbraculum.org> | 2014-09-30 11:49:37 +0200 |
---|---|---|
committer | Jonas Suhr Christensen <jsc@umbraculum.org> | 2014-09-30 11:49:37 +0200 |
commit | a5873ae2b89d7ac8fc650a0f75778f8dad07a102 (patch) | |
tree | 8b46da9d0d3eca412a0f96fa3dbd0b68d5234b7f /plugingui/lineedit.cc | |
parent | 03174415207996a70cab47f6d2eab6ffc182a24f (diff) |
Following marker.
Diffstat (limited to 'plugingui/lineedit.cc')
-rw-r--r-- | plugingui/lineedit.cc | 75 |
1 files changed, 64 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 <assert.h> + #include <hugin.hpp> #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 |