From 21bb5bd2bd4243dc83a08d6e0329b5de2f96b1fe Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 3 Oct 2015 14:34:20 +0200 Subject: Refactor ComboBox and ListBox(Thin and Basic). --- plugingui/combobox.cc | 312 ++++++++++++++++++++++++++------------------------ 1 file changed, 164 insertions(+), 148 deletions(-) (limited to 'plugingui/combobox.cc') diff --git a/plugingui/combobox.cc b/plugingui/combobox.cc index 4637282..4d2637a 100644 --- a/plugingui/combobox.cc +++ b/plugingui/combobox.cc @@ -33,199 +33,215 @@ #define BORDER 10 -void listboxSelectHandler(void *ptr) -{ - GUI::ComboBox *c = (GUI::ComboBox*)ptr; - GUI::ButtonEvent e; - e.direction = 1; - c->buttonEvent(&e); -} +namespace GUI { -GUI::ComboBox::ComboBox(GUI::Widget *parent) - : GUI::Widget(parent) +void ComboBox::listboxSelectHandler() { - handler = NULL; - ptr = NULL; - - box.topLeft = new Image(":widget_tl.png"); - box.top = new Image(":widget_t.png"); - box.topRight = new Image(":widget_tr.png"); - box.left = new Image(":widget_l.png"); - box.right = new Image(":widget_r.png"); - box.bottomLeft = new Image(":widget_bl.png"); - box.bottom = new Image(":widget_b.png"); - box.bottomRight = new Image(":widget_br.png"); - box.center = new Image(":widget_c.png"); - - listbox = new GUI::ListBoxThin(parent); - listbox->registerSelectHandler(listboxSelectHandler, this); - listbox->registerClickHandler(listboxSelectHandler, this); - listbox->hide(); + ButtonEvent e; + e.direction = 1; + buttonEvent(&e); } -GUI::ComboBox::~ComboBox() +ComboBox::ComboBox(Widget *parent) + : Widget(parent) + , listbox(parent) { + box.topLeft = new Image(":widget_tl.png"); + box.top = new Image(":widget_t.png"); + box.topRight = new Image(":widget_tr.png"); + box.left = new Image(":widget_l.png"); + box.right = new Image(":widget_r.png"); + box.bottomLeft = new Image(":widget_bl.png"); + box.bottom = new Image(":widget_b.png"); + box.bottomRight = new Image(":widget_br.png"); + box.center = new Image(":widget_c.png"); + + CONNECT(&listbox, selectionNotifier, this, &ComboBox::listboxSelectHandler); + CONNECT(&listbox, clickNotifier, this, &ComboBox::listboxSelectHandler); + + listbox.hide(); } -void GUI::ComboBox::addItem(std::string name, std::string value) +ComboBox::~ComboBox() { - listbox->addItem(name, value); + delete box.topLeft; + delete box.top; + delete box.topRight; + delete box.left; + delete box.right; + delete box.bottomLeft; + delete box.bottom; + delete box.bottomRight; + delete box.center; } -void GUI::ComboBox::clear() +void ComboBox::addItem(std::string name, std::string value) { - listbox->clear(); - repaintEvent(NULL); + listbox.addItem(name, value); } -bool GUI::ComboBox::selectItem(int index) +void ComboBox::clear() { - listbox->selectItem(index); - repaintEvent(NULL); - return true; + listbox.clear(); + repaintEvent(nullptr); } -std::string GUI::ComboBox::selectedName() +bool ComboBox::selectItem(int index) { - return listbox->selectedName(); + listbox.selectItem(index); + repaintEvent(nullptr); + return true; } -std::string GUI::ComboBox::selectedValue() +std::string ComboBox::selectedName() { - return listbox->selectedValue(); + return listbox.selectedName(); } -void GUI::ComboBox::registerValueChangedHandler(void (*handler)(void *), - void *ptr) +std::string ComboBox::selectedValue() { - this->handler = handler; - this->ptr = ptr; + return listbox.selectedValue(); } -static void drawArrow(GUI::Painter &p, int x, int y, int w, int h) +static void drawArrow(Painter &p, int x, int y, int w, int h) { - p.drawLine(x, y, x+(w/2), y+h); - p.drawLine(x+(w/2), y+h, x+w, y); + p.drawLine(x, y, x+(w/2), y+h); + p.drawLine(x+(w/2), y+h, x+w, y); - y++; - p.drawLine(x, y, x+(w/2), y+h); - p.drawLine(x+(w/2), y+h, x+w, y); + y++; + p.drawLine(x, y, x+(w/2), y+h); + p.drawLine(x+(w/2), y+h, x+w, y); } -void GUI::ComboBox::repaintEvent(GUI::RepaintEvent *e) +void ComboBox::repaintEvent(RepaintEvent *e) { - Painter p(this); + Painter p(this); + + p.clear(); - p.clear(); + std::string _text = selectedName(); - std::string _text = selectedName(); + int w = width(); + int h = height(); + if(w == 0 || h == 0) + { + return; + } - int w = width(); - int h = height(); - if(w == 0 || h == 0) return; - p.drawBox(0, 0, &box, w, h); + 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, font, _text); + p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); + p.drawText(BORDER - 4 + 3, height()/2+5 + 1 + 1, font, _text); - // p.setColour(Colour(1, 1, 1)); - // p.drawText(BORDER - 4, (height()+font.textHeight()) / 2 + 1, font, _text); + // p.setColour(Colour(1, 1, 1)); + // p.drawText(BORDER - 4, (height()+font.textHeight()) / 2 + 1, font, _text); - //int n = height() / 2; + //int n = height() / 2; - // p.drawLine(width() - n - 6, 1 + 6, width() - 1 - 6, 1 + 6); - { - int w = 10; - int h = 6; - drawArrow(p, width() - 6 - 4 - w, (height() - h) / 2, w, h); - p.drawLine(width() - 6 - 4 - w - 4, 7, - width() - 6 - 4 - w - 4, height() - 8); - } + // p.drawLine(width() - n - 6, 1 + 6, width() - 1 - 6, 1 + 6); + { + int w = 10; + int h = 6; + drawArrow(p, width() - 6 - 4 - w, (height() - h) / 2, w, h); + p.drawLine(width() - 6 - 4 - w - 4, 7, + width() - 6 - 4 - w - 4, height() - 8); + } } -void GUI::ComboBox::scrollEvent(ScrollEvent *e) +void ComboBox::scrollEvent(ScrollEvent *e) { - /* - scroll_offset += e->delta; - if(scroll_offset < 0) scroll_offset = 0; - if(scroll_offset > (items.size() - 1)) - scroll_offset = (items.size() - 1); - repaintEvent(NULL); - */ + /* + scroll_offset += e->delta; + if(scroll_offset < 0) + { + scroll_offset = 0; + } + if(scroll_offset > (items.size() - 1)) + { + scroll_offset = (items.size() - 1); + } + repaintEvent(nullptr); + */ } -void GUI::ComboBox::keyEvent(GUI::KeyEvent *e) +void ComboBox::keyEvent(KeyEvent *e) { - if(e->direction != -1) return; - - /* - switch(e->keycode) { - case GUI::KeyEvent::KEY_UP: - { - selected--; - if(selected < 0) selected = 0; - if(selected < scroll_offset) { - scroll_offset = selected; - if(scroll_offset < 0) scroll_offset = 0; - } - } - break; - case GUI::KeyEvent::KEY_DOWN: - { - // Number of items that can be displayed at a time. - int numitems = height() / (font.textHeight() + padding); - - selected++; - if(selected > (items.size() - 1)) - selected = (items.size() - 1); - if(selected > (scroll_offset + numitems - 1)) { - scroll_offset = selected - numitems + 1; - if(scroll_offset > (items.size() - 1)) - scroll_offset = (items.size() - 1); - } - } - break; - case GUI::KeyEvent::KEY_HOME: - selected = 0; - break; - case GUI::KeyEvent::KEY_END: - selected = items.size() - 1; - break; - default: - break; - } - - repaintEvent(NULL); - */ + if(e->direction != -1) + { + return; + } + + /* + switch(e->keycode) { + case KeyEvent::KEY_UP: + { + selected--; + if(selected < 0) + { + selected = 0; + } + if(selected < scroll_offset) + { + scroll_offset = selected; + if(scroll_offset < 0) + { + scroll_offset = 0; + } + } + } + break; + case KeyEvent::KEY_DOWN: + { + // Number of items that can be displayed at a time. + int numitems = height() / (font.textHeight() + padding); + + selected++; + if(selected > (items.size() - 1)) + { + selected = (items.size() - 1); + } + if(selected > (scroll_offset + numitems - 1)) + { + scroll_offset = selected - numitems + 1; + if(scroll_offset > (items.size() - 1)) + { + scroll_offset = (items.size() - 1); + } + } + } + break; + case KeyEvent::KEY_HOME: + selected = 0; + break; + case KeyEvent::KEY_END: + selected = items.size() - 1; + break; + default: + break; + } + + repaintEvent(nullptr); + */ } -void GUI::ComboBox::buttonEvent(ButtonEvent *e) +void ComboBox::buttonEvent(ButtonEvent *e) { - if(e->direction != 1) return; - - if(!listbox->visible()) { - listbox->resize(width() - 10, 100); - listbox->move(x() + 5, y() + height() - 7); - } else { - if(handler) handler(ptr); - } - - listbox->setVisible(!listbox->visible()); + if(e->direction != 1) + { + return; + } + + if(!listbox.visible()) + { + listbox.resize(width() - 10, 100); + listbox.move(x() + 5, y() + height() - 7); + } + else + { + valueChangedNotifier(listbox.selectedName(), listbox.selectedValue()); + } + + listbox.setVisible(!listbox.visible()); } -#ifdef TEST_COMBOBOX -//Additional dependency files -//deps: -//Required cflags (autoconf vars may be used) -//cflags: -//Required link options (autoconf vars may be used) -//libs: -#include "test.h" - -TEST_BEGIN; - -// TODO: Put some testcode here (see test.h for usable macros). - -TEST_END; - -#endif/*TEST_COMBOBOX*/ +} // GUI:: -- cgit v1.2.3