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 ++++++++++++++-------------- plugingui/combobox.h | 44 ++-- plugingui/filebrowser.cc | 11 +- plugingui/filebrowser.h | 1 + plugingui/listbox.cc | 116 +++++------ plugingui/listbox.h | 41 ++-- plugingui/listboxbasic.cc | 510 ++++++++++++++++++++++++---------------------- plugingui/listboxbasic.h | 84 ++++---- plugingui/listboxthin.cc | 112 +++++----- plugingui/listboxthin.h | 40 ++-- plugingui/notifier.h | 2 +- 11 files changed, 649 insertions(+), 624 deletions(-) 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:: diff --git a/plugingui/combobox.h b/plugingui/combobox.h index bc4ae38..da157ef 100644 --- a/plugingui/combobox.h +++ b/plugingui/combobox.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_COMBOBOX_H__ -#define __DRUMGIZMO_COMBOBOX_H__ +#pragma once #include #include @@ -39,35 +38,32 @@ namespace GUI { class ComboBox : public Widget { public: - ComboBox(Widget *parent); - ~ComboBox(); + ComboBox(Widget *parent); + ~ComboBox(); - bool isFocusable() { return true; } + void addItem(std::string name, std::string value); - void addItem(std::string name, std::string value); + void clear(); + bool selectItem(int index); + std::string selectedName(); + std::string selectedValue(); - void clear(); - bool selectItem(int index); - std::string selectedName(); - std::string selectedValue(); + // From Widget: + bool isFocusable() override { return true; } + virtual void repaintEvent(RepaintEvent *e) override; + virtual void buttonEvent(ButtonEvent *e) override; + virtual void scrollEvent(ScrollEvent *e) override; + virtual void keyEvent(KeyEvent *e) override; - void registerValueChangedHandler(void (*handler)(void *), void *ptr); - - virtual void repaintEvent(RepaintEvent *e); - virtual void buttonEvent(ButtonEvent *e); - virtual void scrollEvent(ScrollEvent *e); - virtual void keyEvent(KeyEvent *e); + Notifier valueChangedNotifier; private: - Painter::Box box; - - GUI::Font font; - GUI::ListBoxThin *listbox; + Painter::Box box; - void (*handler)(void *); - void *ptr; -}; + void listboxSelectHandler(); + Font font; + ListBoxThin listbox; }; -#endif/*__DRUMGIZMO_COMBOBOX_H__*/ +} // GUI:: diff --git a/plugingui/filebrowser.cc b/plugingui/filebrowser.cc index b32b586..5846ffe 100644 --- a/plugingui/filebrowser.cc +++ b/plugingui/filebrowser.cc @@ -193,13 +193,13 @@ GUI::FileBrowser::FileBrowser(GUI::Widget *parent) prv->lineedit->registerEnterPressedHandler(handleKeyEvent, prv); prv->listbox = &listbox; - listbox.registerSelectHandler(changeDir, prv); + CONNECT(&listbox, selectionNotifier, this, &FileBrowser::listSelectionChanged); btn_sel.setText("Select"); - CONNECT((&btn_sel), clickNotifier, this, &FileBrowser::selectButtonClicked); + CONNECT(&btn_sel, clickNotifier, this, &FileBrowser::selectButtonClicked); btn_esc.setText("Cancel"); - CONNECT((&btn_esc), clickNotifier, this, &FileBrowser::cancelButtonClicked); + CONNECT(&btn_esc, clickNotifier, this, &FileBrowser::cancelButtonClicked); changeDir(prv); @@ -267,6 +267,11 @@ void GUI::FileBrowser::repaintEvent(GUI::RepaintEvent *e) p.drawImageStretched(0,0, &back, width(), height()); } +void GUI::FileBrowser::listSelectionChanged() +{ + changeDir(prv); +} + void GUI::FileBrowser::selectButtonClicked() { changeDir(prv); diff --git a/plugingui/filebrowser.h b/plugingui/filebrowser.h index 9eb9f54..93a745e 100644 --- a/plugingui/filebrowser.h +++ b/plugingui/filebrowser.h @@ -57,6 +57,7 @@ public: virtual void resize(int w, int h); private: + void listSelectionChanged(); void selectButtonClicked(); void cancelButtonClicked(); diff --git a/plugingui/listbox.cc b/plugingui/listbox.cc index ed873b2..22263b0 100644 --- a/plugingui/listbox.cc +++ b/plugingui/listbox.cc @@ -29,95 +29,97 @@ #include "painter.h" #include "font.h" -#include - -GUI::ListBox::ListBox(GUI::Widget *parent) - : GUI::Widget(parent) +namespace GUI { + +ListBox::ListBox(Widget *parent) + : Widget(parent) + , selectionNotifier(basic.selectionNotifier) + , clickNotifier(basic.clickNotifier) + , valueChangedNotifier(basic.valueChangedNotifier) + , basic(this) { - 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"); - - basic = new GUI::ListBoxBasic(this); - basic->move(box.left->width(), box.top->height()); + 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"); + + basic.move(box.left->width(), box.top->height()); } -GUI::ListBox::~ListBox() +ListBox::~ListBox() { + 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::ListBox::addItem(std::string name, std::string value) +void ListBox::addItem(std::string name, std::string value) { - basic->addItem(name, value); + basic.addItem(name, value); } -void GUI::ListBox::addItems(std::vector &items) +void ListBox::addItems(std::vector &items) { - basic->addItems(items); + basic.addItems(items); } -void GUI::ListBox::clear() +void ListBox::clear() { - basic->clear(); + basic.clear(); } -bool GUI::ListBox::selectItem(int index) +bool ListBox::selectItem(int index) { - return basic->selectItem(index); + return basic.selectItem(index); } -std::string GUI::ListBox::selectedName() +std::string ListBox::selectedName() { - return basic->selectedName(); + return basic.selectedName(); } -std::string GUI::ListBox::selectedValue() +std::string ListBox::selectedValue() { - return basic->selectedValue(); + return basic.selectedValue(); } -void GUI::ListBox::clearSelectedValue() +void ListBox::clearSelectedValue() { - basic->clearSelectedValue(); + basic.clearSelectedValue(); } -void GUI::ListBox::registerClickHandler(void (*handler)(void *), void *ptr) +void ListBox::repaintEvent(RepaintEvent *e) { - basic->registerClickHandler(handler, ptr); -} + Painter p(this); -void GUI::ListBox::registerSelectHandler(void (*handler)(void *), void *ptr) -{ - basic->registerSelectHandler(handler, ptr); -} + p.clear(); -void GUI::ListBox::registerValueChangeHandler(void (*handler)(void *), - void *ptr) -{ - basic->registerValueChangeHandler(handler, ptr); -} + int w = width(); + int h = height(); + if(w == 0 || h == 0) + { + return; + } -void GUI::ListBox::repaintEvent(GUI::RepaintEvent *e) -{ - GUI::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.drawBox(0, 0, &box, w, h); } -void GUI::ListBox::resize(int width, int height) +void ListBox::resize(int width, int height) { - GUI::Widget::resize(width, height); - basic->resize(width - (box.left->width() + box.right->width()), - height - (box.top->height() + box.bottom->height())); + Widget::resize(width, height); + basic.resize(width - (box.left->width() + box.right->width()), + height - (box.top->height() + box.bottom->height())); } + +} // GUI:: diff --git a/plugingui/listbox.h b/plugingui/listbox.h index c8677e7..cd54aa9 100644 --- a/plugingui/listbox.h +++ b/plugingui/listbox.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_LISTBOX_H__ -#define __DRUMGIZMO_LISTBOX_H__ +#pragma once #include #include @@ -38,31 +37,31 @@ namespace GUI { class ListBox : public Widget { public: - ListBox(Widget *parent); - ~ListBox(); + ListBox(Widget *parent); + ~ListBox(); - void addItem(std::string name, std::string value); - void addItems(std::vector &items); + void addItem(std::string name, std::string value); + void addItems(std::vector &items); - void clear(); - bool selectItem(int index); - std::string selectedName(); - std::string selectedValue(); - void clearSelectedValue(); + void clear(); + bool selectItem(int index); + std::string selectedName(); + std::string selectedValue(); + void clearSelectedValue(); - void registerSelectHandler(void (*handler)(void *), void *ptr); - void registerClickHandler(void (*handler)(void *), void *ptr); - void registerValueChangeHandler(void (*handler)(void *), void *ptr); + // From Widget: + virtual void repaintEvent(RepaintEvent *e) override; + virtual void resize(int w, int h) override; - virtual void repaintEvent(GUI::RepaintEvent *e); - virtual void resize(int w, int h); + // Forwarded notifiers from ListBoxBasic::basic + Notifier<>& selectionNotifier; + Notifier<>& clickNotifier; + Notifier<>& valueChangedNotifier; private: - ListBoxBasic *basic; + ListBoxBasic basic; - Painter::Box box; + Painter::Box box; }; -}; - -#endif/*__DRUMGIZMO_LISTBOX_H__*/ +} // GUI:: diff --git a/plugingui/listboxbasic.cc b/plugingui/listboxbasic.cc index df7877d..4e57807 100644 --- a/plugingui/listboxbasic.cc +++ b/plugingui/listboxbasic.cc @@ -1,4 +1,3 @@ - /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ /*************************************************************************** * listboxbasic.cc @@ -30,305 +29,322 @@ #include "painter.h" #include "font.h" -#include -#include +namespace GUI { void scrolled(void *ptr) { - GUI::ListBoxBasic *l = (GUI::ListBoxBasic *)ptr; - l->repaintEvent(NULL); + ListBoxBasic *l = (ListBoxBasic *)ptr; + l->repaintEvent(NULL); } -GUI::ListBoxBasic::ListBoxBasic(GUI::Widget *parent) - : GUI::Widget(parent), scroll(this), bg_img(":widget_c.png") +ListBoxBasic::ListBoxBasic(Widget *parent) + : Widget(parent) + , scroll(this) + , bg_img(":widget_c.png") { - scroll.move(0,0); - scroll.resize(18, 100); - - scroll.registerValueChangeHandler(scrolled, this); - - padding = 4; - btn_size = 18; - - selected = -1; - marked = -1; + scroll.move(0,0); + scroll.resize(18, 100); - clk_handler = NULL; - clk_ptr = NULL; + scroll.registerValueChangeHandler(scrolled, this); - sel_handler = NULL; - sel_ptr = NULL; + padding = 4; + btn_size = 18; - valch_handler = NULL; - valch_ptr = NULL; + selected = -1; + marked = -1; } -GUI::ListBoxBasic::~ListBoxBasic() +ListBoxBasic::~ListBoxBasic() { } -void GUI::ListBoxBasic::setSelection(int index) +void ListBoxBasic::setSelection(int index) { - selected = index; - if(valch_handler) valch_handler(valch_ptr); + selected = index; + valueChangedNotifier(); } -void GUI::ListBoxBasic::addItem(std::string name, std::string value) +void ListBoxBasic::addItem(std::string name, std::string value) { - std::vector items; - GUI::ListBoxBasic::Item item; - item.name = name; - item.value = value; - items.push_back(item); - addItems(items); + std::vector items; + ListBoxBasic::Item item; + item.name = name; + item.value = value; + items.push_back(item); + addItems(items); } -void GUI::ListBoxBasic::addItems(std::vector &is) +void ListBoxBasic::addItems(std::vector &is) { - // DEBUG(list, "addItems %lu\n", is.size()); - std::vector::iterator i = is.begin(); - while(i != is.end()) { - items.push_back(*i); - i++; - } + for(auto i = is.begin(); i != is.end(); ++i) + { + items.push_back(*i); + } -/* - // sort - for(int x = 0; x < (int)items.size(); x++) { - for(int y = 0; y < (int)items.size(); y++) { - if(items[x].name < items[y].name) { - if(x == selected) setSelection(y); - else if(selected == y) setSelection(x); - - GUI::ListBoxBasic::Item tmp = items[x]; - items[x] = items[y]; - items[y] = tmp; - } - } - } -*/ - - if(selected == -1) setSelection((int)items.size() - 1); - setSelection(0); - - int numitems = height() / (font.textHeight() + padding); - scroll.setRange(numitems); - scroll.setMaximum(items.size()); -} + if(selected == -1) + { + setSelection((int)items.size() - 1); + } -void GUI::ListBoxBasic::clear() -{ - items.clear(); - setSelection(-1); - scroll.setValue(0); - repaintEvent(NULL); -} + setSelection(0); -bool GUI::ListBoxBasic::selectItem(int index) -{ - if(index < 0 || index > (int)items.size() - 1) return false; - setSelection(index); - repaintEvent(NULL); - return true; + int numitems = height() / (font.textHeight() + padding); + scroll.setRange(numitems); + scroll.setMaximum(items.size()); } -std::string GUI::ListBoxBasic::selectedName() +void ListBoxBasic::clear() { - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].name; + items.clear(); + setSelection(-1); + scroll.setValue(0); + repaintEvent(nullptr); } -std::string GUI::ListBoxBasic::selectedValue() +bool ListBoxBasic::selectItem(int index) { - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].value; -} + if(index < 0 || (index > (int)items.size() - 1)) + { + return false; + } -void GUI::ListBoxBasic::clearSelectedValue() -{ - setSelection(-1); + setSelection(index); + repaintEvent(nullptr); + + return true; } -void GUI::ListBoxBasic::registerClickHandler(void (*handler)(void *), void *ptr) +std::string ListBoxBasic::selectedName() { - this->clk_handler = handler; - this->clk_ptr = ptr; + if(selected < 0 || (selected > (int)items.size() - 1)) + { + return ""; + } + + return items[selected].name; } -void GUI::ListBoxBasic::registerSelectHandler(void (*handler)(void *), void *ptr) +std::string ListBoxBasic::selectedValue() { - this->sel_handler = handler; - this->sel_ptr = ptr; + if(selected < 0 || (selected > (int)items.size() - 1)) + { + return ""; + } + + return items[selected].value; } -void GUI::ListBoxBasic::registerValueChangeHandler(void (*handler)(void *), - void *ptr) +void ListBoxBasic::clearSelectedValue() { - this->valch_handler = handler; - this->valch_ptr = ptr; + setSelection(-1); } -void GUI::ListBoxBasic::repaintEvent(GUI::RepaintEvent *e) +void ListBoxBasic::repaintEvent(RepaintEvent *e) { - DEBUG(list, "repaint\n"); - GUI::Painter p(this); - - p.clear(); - - int w = width(); - int h = height(); - if(w == 0 || h == 0) return; - - p.drawImageStretched(0, 0, &bg_img, w, h); - - p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); - - int yoffset = padding / 2; - int skip = scroll.value(); - int numitems = height() / (font.textHeight() + padding) + 1; - for(int idx = skip; idx < (int)items.size() && idx < skip + numitems; idx++) { - GUI::ListBoxBasic::Item *i = &items[idx]; - if(idx == selected) { - p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 0.5)); - p.drawFilledRectangle(0, - yoffset - (padding / 2), - width() - 1, - yoffset + (font.textHeight() + 1)); - } - - if(idx == marked) { - p.drawRectangle(0, - yoffset - (padding / 2), - width() - 1, - yoffset + (font.textHeight() + 1)); - } - - p.setColour(GUI::Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); - - p.drawText(2, yoffset + font.textHeight(), font, i->name); - yoffset += font.textHeight() + padding; - } + Painter p(this); + + p.clear(); + + int w = width(); + int h = height(); + + if(w == 0 || h == 0) + { + return; + } + + p.drawImageStretched(0, 0, &bg_img, w, h); + + p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); + + int yoffset = padding / 2; + int skip = scroll.value(); + int numitems = height() / (font.textHeight() + padding) + 1; + for(int idx = skip; (idx < (int)items.size()) && (idx < (skip + numitems)); + idx++) + { + ListBoxBasic::Item *i = &items[idx]; + if(idx == selected) + { + p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 0.5)); + p.drawFilledRectangle(0, + yoffset - (padding / 2), + width() - 1, + yoffset + (font.textHeight() + 1)); + } + + if(idx == marked) + { + p.drawRectangle(0, + yoffset - (padding / 2), + width() - 1, + yoffset + (font.textHeight() + 1)); + } + + p.setColour(Colour(183.0/255.0, 219.0/255.0 , 255.0/255.0, 1)); + + p.drawText(2, yoffset + font.textHeight(), font, i->name); + yoffset += font.textHeight() + padding; + } } -void GUI::ListBoxBasic::scrollEvent(ScrollEvent *e) +void ListBoxBasic::scrollEvent(ScrollEvent *e) { - scroll.scrollEvent(e); + scroll.scrollEvent(e); } -void GUI::ListBoxBasic::keyEvent(GUI::KeyEvent *e) +void ListBoxBasic::keyEvent(KeyEvent *e) { - if(e->direction != -1) return; - - switch(e->keycode) { - case GUI::KeyEvent::KEY_UP: - { - marked--; - if(marked < 0) marked = 0; - - if(marked < scroll.value()) { - scroll.setValue(marked); - } - } - break; - case GUI::KeyEvent::KEY_DOWN: - { - // Number of items that can be displayed at a time. - int numitems = height() / (font.textHeight() + padding); - - marked++; - if(marked > ((int)items.size() - 1)) marked = (int)items.size() - 1; - - if(marked > (scroll.value() + numitems - 1)) { - scroll.setValue(marked - numitems + 1); - } - } - break; - case GUI::KeyEvent::KEY_HOME: - marked = 0; - if(marked < scroll.value()) { - scroll.setValue(marked); - } - break; - case GUI::KeyEvent::KEY_END: - { - // Number of items that can be displayed at a time. - int numitems = height() / (font.textHeight() + padding); - - marked = (int)items.size() - 1; - if(marked > (scroll.value() + numitems - 1)) { - scroll.setValue(marked - numitems + 1); - } - } - break; - case GUI::KeyEvent::KEY_CHARACTER: - if(e->text == " ") { - setSelection(marked); - // if(sel_handler) sel_handler(sel_ptr); - } - break; - case GUI::KeyEvent::KEY_ENTER: - setSelection(marked); - if(sel_handler) sel_handler(sel_ptr); - break; - default: - break; - } - - repaintEvent(NULL); + if(e->direction != -1) + { + return; + } + + switch(e->keycode) { + case KeyEvent::KEY_UP: + marked--; + if(marked < 0) + { + marked = 0; + } + + if(marked < scroll.value()) + { + scroll.setValue(marked); + } + break; + + case KeyEvent::KEY_DOWN: + { + // Number of items that can be displayed at a time. + int numitems = height() / (font.textHeight() + padding); + + marked++; + if(marked > ((int)items.size() - 1)) + { + marked = (int)items.size() - 1; + } + + if(marked > (scroll.value() + numitems - 1)) + { + scroll.setValue(marked - numitems + 1); + } + } + break; + + case KeyEvent::KEY_HOME: + marked = 0; + if(marked < scroll.value()) + { + scroll.setValue(marked); + } + break; + + case KeyEvent::KEY_END: + { + // Number of items that can be displayed at a time. + int numitems = height() / (font.textHeight() + padding); + + marked = (int)items.size() - 1; + if(marked > (scroll.value() + numitems - 1)) + { + scroll.setValue(marked - numitems + 1); + } + } + break; + + case KeyEvent::KEY_CHARACTER: + if(e->text == " ") + { + setSelection(marked); + //selectionNotifier(); + } + break; + + case KeyEvent::KEY_ENTER: + setSelection(marked); + selectionNotifier(); + break; + + default: + break; + } + + repaintEvent(nullptr); } -void GUI::ListBoxBasic::buttonEvent(ButtonEvent *e) +void ListBoxBasic::buttonEvent(ButtonEvent *e) { - if(e->x > ((int)width() - btn_size) && e->y < ((int)width() - 1)) { - if(e->y > 0 && e->y < btn_size) { - if(e->direction == -1) return; - scroll.setValue(scroll.value() - 1); - return; - } - - if(e->y > ((int)height() - btn_size) && e->y < ((int)height() - 1)) { - if(e->direction == -1) return; - scroll.setValue(scroll.value() + 1); - return; - } - } - - if(e->direction == -1) { - int skip = scroll.value(); - size_t yoffset = padding / 2; - for(int idx = skip; idx < (int)items.size(); idx++) { - yoffset += font.textHeight() + padding; - if(e->y < (int)yoffset - (padding / 2)) { - setSelection(idx); - marked = selected; - if(clk_handler) clk_handler(clk_ptr); - break; - } - } - - repaintEvent(NULL); - } - - if(e->direction != -1) { - int skip = scroll.value(); - size_t yoffset = padding / 2; - for(int idx = skip; idx < (int)items.size(); idx++) { - yoffset += font.textHeight() + padding; - if(e->y < (int)yoffset - (padding / 2)) { - marked = idx; - break; - } - } - - repaintEvent(NULL); - } - - if(e->doubleclick && sel_handler) sel_handler(sel_ptr); + if((e->x > ((int)width() - btn_size)) && (e->y < ((int)width() - 1))) + { + if(e->y > 0 && e->y < btn_size) + { + if(e->direction == -1) + { + return; + } + scroll.setValue(scroll.value() - 1); + return; + } + + if(e->y > ((int)height() - btn_size) && e->y < ((int)height() - 1)) + { + if(e->direction == -1) + { + return; + } + scroll.setValue(scroll.value() + 1); + return; + } + } + + if(e->direction == -1) + { + int skip = scroll.value(); + size_t yoffset = padding / 2; + for(int idx = skip; idx < (int)items.size(); idx++) + { + yoffset += font.textHeight() + padding; + if(e->y < (int)yoffset - (padding / 2)) + { + setSelection(idx); + marked = selected; + clickNotifier(); + break; + } + } + + repaintEvent(nullptr); + } + + if(e->direction != -1) + { + int skip = scroll.value(); + size_t yoffset = padding / 2; + for(int idx = skip; idx < (int)items.size(); idx++) + { + yoffset += font.textHeight() + padding; + if(e->y < ((int)yoffset - (padding / 2))) + { + marked = idx; + break; + } + } + + repaintEvent(nullptr); + } + + if(e->doubleclick) + { + selectionNotifier(); + } } -void GUI::ListBoxBasic::resize(int w, int h) +void ListBoxBasic::resize(int w, int h) { - GUI::Widget::resize(w,h); - scroll.move(w - scroll.width(), 0); - scroll.resize(scroll.width(), h); + Widget::resize(w,h); + scroll.move(w - scroll.width(), 0); + scroll.resize(scroll.width(), h); } + +} // GUI:: diff --git a/plugingui/listboxbasic.h b/plugingui/listboxbasic.h index deb4a67..ef6aceb 100644 --- a/plugingui/listboxbasic.h +++ b/plugingui/listboxbasic.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_LISTBOXBASIC_H__ -#define __DRUMGIZMO_LISTBOXBASIC_H__ +#pragma once #include #include @@ -33,69 +32,58 @@ #include "widget.h" #include "font.h" #include "painter.h" - #include "scrollbar.h" +#include "notifier.h" namespace GUI { class ListBoxBasic : public Widget { public: - class Item { - public: - std::string name; - std::string value; - }; - - ListBoxBasic(Widget *parent); - ~ListBoxBasic(); + class Item { + public: + std::string name; + std::string value; + }; - bool isFocusable() { return true; } + ListBoxBasic(Widget *parent); + ~ListBoxBasic(); - void addItem(std::string name, std::string value); - void addItems(std::vector &items); + void addItem(std::string name, std::string value); + void addItems(std::vector &items); - void clear(); - bool selectItem(int index); - std::string selectedName(); - std::string selectedValue(); + void clear(); + bool selectItem(int index); + std::string selectedName(); + std::string selectedValue(); - void clearSelectedValue(); + void clearSelectedValue(); - void registerSelectHandler(void (*handler)(void *), void *ptr); - void registerClickHandler(void (*handler)(void *), void *ptr); - void registerValueChangeHandler(void (*handler)(void *), void *ptr); + // From Widget: + bool isFocusable() override { return true; } + virtual void repaintEvent(RepaintEvent *e) override; + virtual void buttonEvent(ButtonEvent *e) override; + virtual void scrollEvent(ScrollEvent *e) override; + virtual void keyEvent(KeyEvent *e) override; + virtual void resize(int w, int h) override; - virtual void repaintEvent(RepaintEvent *e); - virtual void buttonEvent(ButtonEvent *e); - virtual void scrollEvent(ScrollEvent *e); - virtual void keyEvent(KeyEvent *e); - virtual void resize(int w, int h); + Notifier<> selectionNotifier; + Notifier<> clickNotifier; + Notifier<> valueChangedNotifier; private: - ScrollBar scroll; - - Image bg_img; - - void setSelection(int index); + ScrollBar scroll; - std::vector items; + Image bg_img; - int selected; - int marked; - GUI::Font font; - int padding; - int btn_size; + void setSelection(int index); - void (*sel_handler)(void *); - void *sel_ptr; - - void (*clk_handler)(void *); - void *clk_ptr; - - void (*valch_handler)(void *); - void *valch_ptr; -}; + std::vector items; + int selected; + int marked; + Font font; + int padding; + int btn_size; }; -#endif/*__DRUMGIZMO_LISTBOXBASIC_H__*/ +} // GUI:: diff --git a/plugingui/listboxthin.cc b/plugingui/listboxthin.cc index 3f0fbc6..18cfc76 100644 --- a/plugingui/listboxthin.cc +++ b/plugingui/listboxthin.cc @@ -29,90 +29,92 @@ #include "painter.h" #include "font.h" -#include - -GUI::ListBoxThin::ListBoxThin(GUI::Widget *parent) - : GUI::Widget(parent) +namespace GUI { + +ListBoxThin::ListBoxThin(Widget *parent) + : Widget(parent) + , selectionNotifier(basic.selectionNotifier) + , clickNotifier(basic.clickNotifier) + , valueChangedNotifier(basic.valueChangedNotifier) + , basic(this) { - box.topLeft = new Image(":thinlistbox_tl.png"); - box.top = new Image(":thinlistbox_t.png"); - box.topRight = new Image(":thinlistbox_tr.png"); - box.left = new Image(":thinlistbox_l.png"); - box.right = new Image(":thinlistbox_r.png"); - box.bottomLeft = new Image(":thinlistbox_bl.png"); - box.bottom = new Image(":thinlistbox_b.png"); - box.bottomRight = new Image(":thinlistbox_br.png"); - box.center = new Image(":thinlistbox_c.png"); - - basic = new GUI::ListBoxBasic(this); - basic->move(box.left->width(), box.top->height()); + box.topLeft = new Image(":thinlistbox_tl.png"); + box.top = new Image(":thinlistbox_t.png"); + box.topRight = new Image(":thinlistbox_tr.png"); + box.left = new Image(":thinlistbox_l.png"); + box.right = new Image(":thinlistbox_r.png"); + box.bottomLeft = new Image(":thinlistbox_bl.png"); + box.bottom = new Image(":thinlistbox_b.png"); + box.bottomRight = new Image(":thinlistbox_br.png"); + box.center = new Image(":thinlistbox_c.png"); + + basic.move(box.left->width(), box.top->height()); } -GUI::ListBoxThin::~ListBoxThin() +ListBoxThin::~ListBoxThin() { + 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::ListBoxThin::addItem(std::string name, std::string value) +void ListBoxThin::addItem(std::string name, std::string value) { - basic->addItem(name, value); + basic.addItem(name, value); } -void GUI::ListBoxThin::addItems(std::vector &items) +void ListBoxThin::addItems(std::vector &items) { - basic->addItems(items); + basic.addItems(items); } -void GUI::ListBoxThin::clear() +void ListBoxThin::clear() { - basic->clear(); + basic.clear(); } -bool GUI::ListBoxThin::selectItem(int index) +bool ListBoxThin::selectItem(int index) { - return basic->selectItem(index); + return basic.selectItem(index); } -std::string GUI::ListBoxThin::selectedName() +std::string ListBoxThin::selectedName() { - return basic->selectedName(); + return basic.selectedName(); } -std::string GUI::ListBoxThin::selectedValue() +std::string ListBoxThin::selectedValue() { - return basic->selectedValue(); + return basic.selectedValue(); } -void GUI::ListBoxThin::registerClickHandler(void (*handler)(void *), void *ptr) +void ListBoxThin::repaintEvent(RepaintEvent *e) { - basic->registerClickHandler(handler, ptr); -} + Painter p(this); -void GUI::ListBoxThin::registerSelectHandler(void (*handler)(void *), void *ptr) -{ - basic->registerSelectHandler(handler, ptr); -} + p.clear(); -void GUI::ListBoxThin::registerValueChangeHandler(void (*handler)(void *), - void *ptr) -{ - basic->registerValueChangeHandler(handler, ptr); -} + int w = width(); + int h = height(); + if(w == 0 || h == 0) + { + return; + } -void GUI::ListBoxThin::repaintEvent(GUI::RepaintEvent *e) -{ - GUI::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.drawBox(0, 0, &box, w, h); } -void GUI::ListBoxThin::resize(int width, int height) +void ListBoxThin::resize(int width, int height) { - GUI::Widget::resize(width, height); - basic->resize(width - (box.left->width() + box.right->width()), - height - (box.top->height() + box.bottom->height())); + Widget::resize(width, height); + basic.resize(width - (box.left->width() + box.right->width()), + height - (box.top->height() + box.bottom->height())); } + +} // GUI:: diff --git a/plugingui/listboxthin.h b/plugingui/listboxthin.h index 9c5363a..19114d5 100644 --- a/plugingui/listboxthin.h +++ b/plugingui/listboxthin.h @@ -24,8 +24,7 @@ * along with DrumGizmo; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. */ -#ifndef __DRUMGIZMO_LISTBOXTHIN_H__ -#define __DRUMGIZMO_LISTBOXTHIN_H__ +#pragma once #include #include @@ -33,35 +32,36 @@ #include "widget.h" #include "painter.h" #include "listboxbasic.h" +#include "notifier.h" namespace GUI { class ListBoxThin : public Widget { public: - ListBoxThin(Widget *parent); - ~ListBoxThin(); + ListBoxThin(Widget *parent); + ~ListBoxThin(); - void addItem(std::string name, std::string value); - void addItems(std::vector &items); + void addItem(std::string name, std::string value); + void addItems(std::vector &items); - void clear(); - bool selectItem(int index); - std::string selectedName(); - std::string selectedValue(); + void clear(); + bool selectItem(int index); + std::string selectedName(); + std::string selectedValue(); - void registerSelectHandler(void (*handler)(void *), void *ptr); - void registerClickHandler(void (*handler)(void *), void *ptr); - void registerValueChangeHandler(void (*handler)(void *), void *ptr); + // From Widget: + virtual void repaintEvent(GUI::RepaintEvent *e) override; + virtual void resize(int w, int h) override; - virtual void repaintEvent(GUI::RepaintEvent *e); - virtual void resize(int w, int h); + // Forwarded notifier from ListBoxBasic::basic + Notifier<>& selectionNotifier; + Notifier<>& clickNotifier; + Notifier<>& valueChangedNotifier; private: - ListBoxBasic *basic; + ListBoxBasic basic; - Painter::Box box; + Painter::Box box; }; -}; - -#endif/*__DRUMGIZMO_LISTBOXTHIN_H__*/ +} // GUI:: diff --git a/plugingui/notifier.h b/plugingui/notifier.h index 160fe4e..57ca453 100644 --- a/plugingui/notifier.h +++ b/plugingui/notifier.h @@ -156,4 +156,4 @@ private: } // GUI:: -#define CONNECT(SRC, SIG, TAR, SLO) SRC->SIG.connect(TAR, SLO) +#define CONNECT(SRC, SIG, TAR, SLO) (SRC)->SIG.connect(TAR, SLO) -- cgit v1.2.3