diff options
author | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-03 14:34:20 +0200 |
---|---|---|
committer | Bent Bisballe Nyeng <deva@aasimon.org> | 2015-10-03 14:34:20 +0200 |
commit | 21bb5bd2bd4243dc83a08d6e0329b5de2f96b1fe (patch) | |
tree | beef1c65f08a9677611b20fbb86deff5f1820bd9 /plugingui/listboxbasic.cc | |
parent | 3be8598f672eb5b8e48373ed4ee59feff543ada2 (diff) |
Refactor ComboBox and ListBox(Thin and Basic).
Diffstat (limited to 'plugingui/listboxbasic.cc')
-rw-r--r-- | plugingui/listboxbasic.cc | 510 |
1 files changed, 263 insertions, 247 deletions
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 <stdio.h> -#include <hugin.hpp> +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<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(std::vector<ListBoxBasic::Item> &is) { - // 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++; - } + 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:: |