From 2deeb15d4de7d3747f1d65637f429cedf794cd35 Mon Sep 17 00:00:00 2001 From: Jonas Suhr Christensen Date: Wed, 5 Nov 2014 14:39:44 +0100 Subject: Added scrollbar. --- plugingui/plugingui.cc | 5 ++- plugingui/textedit.cc | 100 ++++++++++++++++++++++++++++++++++--------------- plugingui/textedit.h | 18 ++++++--- 3 files changed, 85 insertions(+), 38 deletions(-) diff --git a/plugingui/plugingui.cc b/plugingui/plugingui.cc index df357e0..cafc67e 100644 --- a/plugingui/plugingui.cc +++ b/plugingui/plugingui.cc @@ -512,9 +512,10 @@ void PluginGUI::init() */ drumkitinfo = new GUI::TextEdit(window); -// drumkitinfo->setText("Hello World\ten\tto\ttre fire fem seks syv otte ni ti elleve tolv tretten\n\nHej med dig jeg har lavet dette widget og det opfører sig egentlig ganske fint\n\n\nni \n\nti elleve tolv \n\nTEST TEST TEST@\n\ntretten fjorten femten \n\nseksten sytten atten nitten \nHello World\nHello World\n"); drumkitinfo->move(370, 25); - drumkitinfo->resize(300, 300); + drumkitinfo->resize(288, 292); + drumkitinfo->setText("Hello World\ten\tto\ttre fire fem seks syv otte ni ti elleve tolv tretten\n\nHej med dig jeg har lavet dette widget og det opfører sig egentlig ganske fint\n\n\nni \n\nti elleve tolv \n\nTEST TEST TEST@\n\ntretten fjorten femten \n\nseksten sytten atten nitten \nHello World\nHello World\naaaaaaa\nbbbbbbbbb\ncccccccccc\ndddddddd\neeeeeeeee\nfffffffffff\nggggggggg\nasdasdasdasd\nasd\n"); +// drumkitinfo->setText("Hello World\n1111\n2222\n"); // Create filebrowser diff --git a/plugingui/textedit.cc b/plugingui/textedit.cc index 43be7aa..73303f7 100644 --- a/plugingui/textedit.cc +++ b/plugingui/textedit.cc @@ -38,12 +38,22 @@ #define BORDER 10 +static void scrolled(void *ptr) +{ + GUI::TextEdit *l = (GUI::TextEdit *)ptr; + l->repaintEvent(NULL); +} + GUI::TextEdit::TextEdit(Widget *parent) - : GUI::Widget(parent) + : GUI::Widget(parent), scroll(this) { pos = 0; setReadOnly(true); + scroll.move(width()-5,1); + scroll.resize(20, 100); + scroll.registerValueChangeHandler(scrolled, this); + box.topLeft = new Image(":widget_tl.png"); box.top = new Image(":widget_t.png"); box.topRight = new Image(":widget_tr.png"); @@ -57,10 +67,11 @@ GUI::TextEdit::TextEdit(Widget *parent) handler = NULL; } -void GUI::TextEdit::registerEnterPressedHandler(void (*handler)(void *), void *ptr) +void GUI::TextEdit::resize(int height, int width) { - this->handler = handler; - this->ptr = ptr; + Widget::resize(height, width); + scroll.resize(scroll.width(), height-10); + scroll.move(width-30,7); } void GUI::TextEdit::setReadOnly(bool ro) @@ -76,6 +87,12 @@ bool GUI::TextEdit::readOnly() void GUI::TextEdit::setText(std::string text) { _text = text; + preprocessText(); + int ran = height() / font.textHeight(); + DEBUG(textedit, "Setting range and max of scrollbar" + " to '%d' and '%d'\n", ran, preprocessedtext.size()); + scroll.setRange(ran); + scroll.setMaximum(preprocessedtext.size()); repaintEvent(NULL); textChanged(); } @@ -85,29 +102,19 @@ std::string GUI::TextEdit::text() return _text; } -void GUI::TextEdit::buttonEvent(ButtonEvent *e) -{ - if(readOnly()) return; -} +//void GUI::TextEdit::buttonEvent(ButtonEvent *e) +//{ +// if(readOnly()) return; +//} -void GUI::TextEdit::keyEvent(GUI::KeyEvent *e) -{ - if(readOnly()) return; -} +//void GUI::TextEdit::keyEvent(GUI::KeyEvent *e) +//{ +// if(readOnly()) return; +//} -void GUI::TextEdit::repaintEvent(GUI::RepaintEvent *e) +void GUI::TextEdit::preprocessText() { - Painter p(this); - - p.clear(); - - int w = width(); - int h = height(); - if(w == 0 || h == 0) return; - p.drawBox(0, 0, &box, w, h); - - p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); - + preprocessedtext.clear(); std::string text = _text; { // Handle tan characters @@ -131,27 +138,58 @@ void GUI::TextEdit::repaintEvent(GUI::RepaintEvent *e) while(pos != std::string::npos); } - { // Draw and wrap long lines - int ypos = font.textHeight() + 5 + 1 + 1 + 1; + { // Wrap long lines std::list::iterator it; for(it = lines.begin(); it != lines.end(); it++) { std::string line = *it; for(size_t i = 0; i < line.length(); i++) { size_t linewidth = font.textWidth(line.substr(0, i)); - if(linewidth >= width() - BORDER - 4 + 3 - 10) { - p.drawText(BORDER - 4 + 3, ypos, font, line.substr(0, i)); - ypos += font.textHeight(); + if(linewidth >= width() - BORDER - 4 + 3 - 10 - scroll.width()) { + preprocessedtext.push_back(line.substr(0, i)); line = line.substr(i); i = 0; } } - p.drawText(BORDER - 4 + 3, ypos, font, line); - ypos += font.textHeight(); + preprocessedtext.push_back(line); } } } +void GUI::TextEdit::repaintEvent(GUI::RepaintEvent *e) +{ + Painter p(this); + + p.clear(); + + int w = width(); + int h = height(); + if(w == 0 || h == 0) return; + p.drawBox(0, 0, &box, w, h); + + p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); + + int skip = scroll.value(); + + int ypos = font.textHeight() + 5 + 1 + 1 + 1; + std::list::iterator it; + it = preprocessedtext.begin(); + + int c = 0; + for( ; c < skip; c++) { + it++; + } + + c = 0; + for( ; it != preprocessedtext.end(); it++) { + if(c * font.textHeight() >= height() - 8 - font.textHeight()) break; + std::string line = *it; + p.drawText(BORDER - 4 + 3, ypos, font, line); + ypos += font.textHeight(); + c++; + } +} + #ifdef TEST_TEXTEDIT //Additional dependency files //deps: diff --git a/plugingui/textedit.h b/plugingui/textedit.h index e26a88c..3f27575 100644 --- a/plugingui/textedit.h +++ b/plugingui/textedit.h @@ -27,12 +27,14 @@ #ifndef __DRUMGIZMO_TEXTEDIT_H__ #define __DRUMGIZMO_TEXTEDIT_H__ - #include +#include + #include "widget.h" #include "font.h" #include "painter.h" +#include "scrollbar.h" namespace GUI { @@ -48,25 +50,31 @@ public: void setReadOnly(bool readonly); bool readOnly(); - void registerEnterPressedHandler(void (*handler)(void *), void *ptr); + void resize(int width, int height); + + + void preprocessText(); //protected: - virtual void keyEvent(KeyEvent *e); +// virtual void keyEvent(KeyEvent *e); virtual void repaintEvent(RepaintEvent *e); - virtual void buttonEvent(ButtonEvent *e); +// virtual void buttonEvent(ButtonEvent *e); protected: virtual void textChanged() {} private: Painter::Box box; - + ScrollBar scroll; Font font; std::string _text; size_t pos; bool readonly; + + std::list< std::string > preprocessedtext; +// size_t numoflines; void (*handler)(void *); void *ptr; -- cgit v1.2.3