diff options
Diffstat (limited to 'plugingui/listbox.cc')
-rw-r--r-- | plugingui/listbox.cc | 234 |
1 files changed, 15 insertions, 219 deletions
diff --git a/plugingui/listbox.cc b/plugingui/listbox.cc index 3dfba5c..6c6c6e7 100644 --- a/plugingui/listbox.cc +++ b/plugingui/listbox.cc @@ -44,104 +44,53 @@ GUI::ListBox::ListBox(GUI::Widget *parent) box.bottomRight = new Image(":widget_br.png"); box.center = new Image(":widget_c.png"); - padding = 4; - btn_size = 14; - - scroll_offset = 0; - selected = -1; - marked = -1; - - clk_handler = NULL; - clk_ptr = NULL; - - sel_handler = NULL; - sel_ptr = NULL; - - valch_handler = NULL; - valch_ptr = NULL; + basic = new GUI::ListBoxBasic(this); + basic->move(box.left->width(), box.top->height()); } GUI::ListBox::~ListBox() { } -void GUI::ListBox::setSelection(int index) -{ - selected = index; - if(valch_handler) valch_handler(valch_ptr); -} - void GUI::ListBox::addItem(std::string name, std::string value) { - struct item i; - i.name = name; - i.value = value; - - items.push_back(i); - - // sort - for(int x = 0; x < (int)items.size() - 1; x++) { - for(int y = 0; y < (int)items.size() - 1; y++) { - if(items[x].name < items[y].name) { - - if(x == selected) setSelection(y); - else if(selected == y) setSelection(x); - - struct item tmp = items[x]; - items[x] = items[y]; - items[y] = tmp; - } - } - } - - if(selected == -1) setSelection((int)items.size() - 1); + basic->addItem(name, value); } void GUI::ListBox::clear() { - items.clear(); - setSelection(-1); - scroll_offset = 0; - repaintEvent(NULL); + basic->clear(); } bool GUI::ListBox::selectItem(int index) { - if(index < 0 || index > (int)items.size() - 1) return false; - setSelection(index); - repaintEvent(NULL); - return true; + return basic->selectItem(index); } std::string GUI::ListBox::selectedName() { - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].name; + return basic->selectedName(); } std::string GUI::ListBox::selectedValue() { - if(selected < 0 || selected > (int)items.size() - 1) return ""; - return items[selected].value; + return basic->selectedValue(); } void GUI::ListBox::registerClickHandler(void (*handler)(void *), void *ptr) { - this->clk_handler = handler; - this->clk_ptr = ptr; + basic->registerClickHandler(handler, ptr); } void GUI::ListBox::registerSelectHandler(void (*handler)(void *), void *ptr) -{ - this->sel_handler = handler; - this->sel_ptr = ptr; +{ + basic->registerSelectHandler(handler, ptr); } void GUI::ListBox::registerValueChangeHandler(void (*handler)(void *), void *ptr) { - this->valch_handler = handler; - this->valch_ptr = ptr; + basic->registerValueChangeHandler(handler, ptr); } void GUI::ListBox::repaintEvent(GUI::RepaintEvent *e) @@ -154,164 +103,11 @@ void GUI::ListBox::repaintEvent(GUI::RepaintEvent *e) int h = height(); if(w == 0 || h == 0) return; p.drawBox(0, 0, &box, w, h); - - int yoffset = padding / 2; - int skip = scroll_offset; - for(int idx = skip; idx < (int)items.size(); idx++) { - struct item *i = &items[idx]; - if(idx == selected) { - p.setColour(Colour(0.6, 0.9)); - p.drawFilledRectangle(1, - yoffset - (padding / 2), - width() - 1, - yoffset + (font.textHeight() + 1)); - } - - if(idx == marked) { - p.setColour(Colour(1, 0.9)); - p.drawRectangle(1, - yoffset - (padding / 2), - width() - 1, - yoffset + (font.textHeight() + 1)); - } - - p.setColour(Colour(1, 1)); - p.drawText(2, yoffset + font.textHeight(), font, i->name); - yoffset += font.textHeight() + padding; - } - - p.drawRectangle(width() - btn_size, 0, width() - 1, btn_size); - p.drawRectangle(width() - btn_size, height() - btn_size, - width() - 1, height() - 1); } -void GUI::ListBox::scrollEvent(ScrollEvent *e) +void GUI::ListBox::resize(int width, int height) { - scroll_offset += e->delta; - if(scroll_offset < 0) scroll_offset = 0; - if(scroll_offset > ((int)items.size() - 1)) - scroll_offset = ((int)items.size() - 1); - repaintEvent(NULL); -} - -void GUI::ListBox::keyEvent(GUI::KeyEvent *e) -{ - if(e->direction != -1) return; - - switch(e->keycode) { - case GUI::KeyEvent::KEY_UP: - { - marked--; - if(marked < 0) marked = 0; - - if(marked < scroll_offset) { - scroll_offset = marked; - 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); - - marked++; - if(marked > ((int)items.size() - 1)) marked = (int)items.size() - 1; - - if(marked > (scroll_offset + numitems - 1)) { - scroll_offset = marked - numitems + 1; - if(scroll_offset > ((int)items.size() - 1)) - scroll_offset = ((int)items.size() - 1); - } - } - break; - case GUI::KeyEvent::KEY_HOME: - marked = 0; - if(marked < scroll_offset) { - scroll_offset = marked; - if(scroll_offset < 0) scroll_offset = 0; - } - 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_offset + numitems - 1)) { - scroll_offset = marked - numitems + 1; - if(scroll_offset > ((int)items.size() - 1)) - scroll_offset = ((int)items.size() - 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); -} - -void GUI::ListBox::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_offset--; - if(scroll_offset < 0) scroll_offset = 0; - repaintEvent(NULL); - return; - } - - if(e->y > ((int)height() - btn_size) && e->y < ((int)height() - 1)) { - if(e->direction == -1) return; - scroll_offset++; - if(scroll_offset > ((int)items.size() - 1)) - scroll_offset = ((int)items.size() - 1); - repaintEvent(NULL); - return; - } - } - - if(e->direction == -1) { - int skip = scroll_offset; - 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_offset; - 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); + GUI::Widget::resize(width, height); + basic->resize(width - (box.left->width() + box.right->width()), + height - (box.top->height() + box.bottom->height())); } |