diff options
Diffstat (limited to 'plugingui/listboxbasic.cc')
-rw-r--r-- | plugingui/listboxbasic.cc | 526 |
1 files changed, 276 insertions, 250 deletions
diff --git a/plugingui/listboxbasic.cc b/plugingui/listboxbasic.cc index df7877d..10d6c66 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,332 @@ #include "painter.h" #include "font.h" -#include <stdio.h> -#include <hugin.hpp> - -void scrolled(void *ptr) -{ - GUI::ListBoxBasic *l = (GUI::ListBoxBasic *)ptr; - l->repaintEvent(NULL); -} +namespace GUI { -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.move(0,0); + scroll.resize(18, 100); - scroll.registerValueChangeHandler(scrolled, this); + CONNECT(&scroll, valueChangeNotifier, + this, &ListBoxBasic::onScrollBarValueChange); - padding = 4; - btn_size = 18; + padding = 4; + btn_size = 18; - selected = -1; - marked = -1; - - clk_handler = NULL; - clk_ptr = NULL; - - sel_handler = NULL; - sel_ptr = NULL; - - 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; + if(marked == -1) + { + marked = index; + } + valueChangedNotifier(); } -void GUI::ListBoxBasic::addItem(std::string name, std::string value) +void ListBoxBasic::addItem(const std::string& name, const std::string& value) { - std::vector<GUI::ListBoxBasic::Item> items; - GUI::ListBoxBasic::Item item; - item.name = name; - item.value = value; - items.push_back(item); - addItems(items); + std::vector<ListBoxBasic::Item> items; + ListBoxBasic::Item item; + item.name = name; + item.value = value; + items.push_back(item); + addItems(items); } -void GUI::ListBoxBasic::addItems(std::vector<GUI::ListBoxBasic::Item> &is) +void ListBoxBasic::addItems(const std::vector<ListBoxBasic::Item>& newItems) { - // DEBUG(list, "addItems %lu\n", is.size()); - std::vector<GUI::ListBoxBasic::Item>::iterator i = is.begin(); - while(i != is.end()) { - items.push_back(*i); - 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()); + for(auto& item : newItems) + { + items.push_back(item); + } + + if(selected == -1) + { + //setSelection((int)items.size() - 1); + setSelection(0); + } + + int numitems = height() / (font.textHeight() + padding); + scroll.setRange(numitems); + scroll.setMaximum(items.size()); + repaintEvent(nullptr); } -void GUI::ListBoxBasic::clear() +void ListBoxBasic::clear() { - items.clear(); - setSelection(-1); - scroll.setValue(0); - repaintEvent(NULL); + items.clear(); + setSelection(-1); + marked = -1; + scroll.setValue(0); + repaintEvent(nullptr); } -bool GUI::ListBoxBasic::selectItem(int index) +bool ListBoxBasic::selectItem(int index) { - if(index < 0 || index > (int)items.size() - 1) return false; - setSelection(index); - repaintEvent(NULL); - return true; -} + if(index < 0 || (index > (int)items.size() - 1)) + { + return false; + } -std::string GUI::ListBoxBasic::selectedName() -{ - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].name; -} + setSelection(index); + repaintEvent(nullptr); -std::string GUI::ListBoxBasic::selectedValue() -{ - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].value; + return true; } -void GUI::ListBoxBasic::clearSelectedValue() +std::string ListBoxBasic::selectedName() { - setSelection(-1); + if(selected < 0 || (selected > (int)items.size() - 1)) + { + return ""; + } + + return items[selected].name; } -void GUI::ListBoxBasic::registerClickHandler(void (*handler)(void *), void *ptr) +std::string ListBoxBasic::selectedValue() { - this->clk_handler = handler; - this->clk_ptr = ptr; + if(selected < 0 || (selected > (int)items.size() - 1)) + { + return ""; + } + + return items[selected].value; } -void GUI::ListBoxBasic::registerSelectHandler(void (*handler)(void *), void *ptr) +void ListBoxBasic::clearSelectedValue() { - this->sel_handler = handler; - this->sel_ptr = ptr; + setSelection(-1); } -void GUI::ListBoxBasic::registerValueChangeHandler(void (*handler)(void *), - void *ptr) +void ListBoxBasic::onScrollBarValueChange(int value) { - this->valch_handler = handler; - this->valch_ptr = ptr; + repaintEvent(nullptr); } -void GUI::ListBoxBasic::repaintEvent(GUI::RepaintEvent *e) +void ListBoxBasic::repaintEvent(RepaintEvent* repaintEvent) { - 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++) + { + auto& item = 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, item.name); + yoffset += font.textHeight() + padding; + } } -void GUI::ListBoxBasic::scrollEvent(ScrollEvent *e) +void ListBoxBasic::scrollEvent(ScrollEvent* scrollEvent) { - scroll.scrollEvent(e); + // forward scroll event to scroll bar. + scroll.scrollEvent(scrollEvent); } -void GUI::ListBoxBasic::keyEvent(GUI::KeyEvent *e) +void ListBoxBasic::keyEvent(KeyEvent* keyEvent) { - 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(keyEvent->direction != Direction::down) + { + return; + } + + switch(keyEvent->keycode) { + case Key::up: + if(marked == 0) + { + return; + } + + marked--; + + if(marked < scroll.value()) + { + scroll.setValue(marked); + } + break; + + case Key::down: + { + // Number of items that can be displayed at a time. + int numitems = height() / (font.textHeight() + padding); + + if(marked == ((int)items.size() - 1)) + { + return; + } + + marked++; + + if(marked > (scroll.value() + numitems - 1)) + { + scroll.setValue(marked - numitems + 1); + } + } + break; + + case Key::home: + marked = 0; + if(marked < scroll.value()) + { + scroll.setValue(marked); + } + break; + + case 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 Key::character: + if(keyEvent->text == " ") + { + setSelection(marked); + //selectionNotifier(); + } + break; + + case Key::enter: + setSelection(marked); + selectionNotifier(); + break; + + default: + break; + } + + repaintEvent(nullptr); } -void GUI::ListBoxBasic::buttonEvent(ButtonEvent *e) +void ListBoxBasic::buttonEvent(ButtonEvent* buttonEvent) { - 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((buttonEvent->x > ((int)width() - btn_size)) && + (buttonEvent->y < ((int)width() - 1))) + { + if(buttonEvent->y > 0 && buttonEvent->y < btn_size) + { + if(buttonEvent->direction == Direction::up) + { + return; + } + scroll.setValue(scroll.value() - 1); + return; + } + + if(buttonEvent->y > ((int)height() - btn_size) && + buttonEvent->y < ((int)height() - 1)) + { + if(buttonEvent->direction == Direction::up) + { + return; + } + scroll.setValue(scroll.value() + 1); + return; + } + } + + if(buttonEvent->direction == Direction::up) + { + int skip = scroll.value(); + size_t yoffset = padding / 2; + for(int idx = skip; idx < (int)items.size(); idx++) + { + yoffset += font.textHeight() + padding; + if(buttonEvent->y < (int)yoffset - (padding / 2)) + { + setSelection(idx); + marked = selected; + clickNotifier(); + break; + } + } + + repaintEvent(nullptr); + } + + if(buttonEvent->direction != Direction::up) + { + int skip = scroll.value(); + size_t yoffset = padding / 2; + for(int idx = skip; idx < (int)items.size(); idx++) + { + yoffset += font.textHeight() + padding; + if(buttonEvent->y < ((int)yoffset - (padding / 2))) + { + marked = idx; + break; + } + } + + repaintEvent(nullptr); + } + + if(buttonEvent->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:: |