From 130a23f483fe2e486e6d9d804b9d998c159fbcb0 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 12 Feb 2012 12:12:53 +0000 Subject: Scroll listview content by item count instead of by height, enabled listview mousewheel scrolling. --- src/ui/listview.cc | 53 ++++++++++++++++++++++++++++++++++++++++++++++------- src/ui/listview.h | 13 ++++++++++--- 2 files changed, 56 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 185b229..ac2a4e8 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -34,11 +34,18 @@ void ListView::set_scroll(float scroll) listview_scroll = 0; // calculate maximal scroll size - float total_height = 0; + float nb_items = 0; for (Children::iterator it = children().begin(); it != children().end(); it++) { - total_height += (*it)->height(); + if ((*it) != static_cast(listview_scrollbar)) { + nb_items += 1.0f; + } + } + if (nb_items > 0.0f) { + nb_items -= 1.0f; + } + if (listview_scroll > nb_items) { + listview_scroll = nb_items; } - if (listview_scroll > total_height) listview_scroll = total_height; } void ListView::inc_scroll(float scroll) @@ -57,28 +64,37 @@ void ListView::resize() listview_scrollbar->set_location(width() - listview_scrollbar->width(), 0); float total_height = 0; + float visible_height = 0; + float nb_items = 0; // reposition all children within the container for (Children::iterator it = children().begin(); it != children().end(); it++) { if ((*it) != static_cast(listview_scrollbar)) { (*it)->set_width(width() - listview_scrollbar->width()); - if (total_height - listview_scroll < 0) { + + if (nb_items - listview_scroll < 0) { // child widget is invisible (*it)->hide(); - } else if ((total_height - listview_scroll + (*it)->height()) > height()) { + } else if ((visible_height + (*it)->height()) > height()) { // child widget is invisible (*it)->hide(); } else { + // child widget is visible (*it)->show(); - (*it)->set_location(0, total_height - listview_scroll); + (*it)->set_location(0, visible_height); + visible_height += (*it)->height(); } total_height += (*it)->height(); + nb_items += 1.0f; + } } if (total_height > height()) { - listview_scrollbar->set_range(0.0f, total_height - height()); + if (nb_items > 0.0f) + nb_items -= 1.0f; + listview_scrollbar->set_range(0.0f, nb_items); listview_scrollbar->set_value(listview_scroll); listview_scrollbar->show(); } else { @@ -134,6 +150,29 @@ bool ListView::on_emit(Widget *sender, const Event event, void *data) return false; } +bool ListView::on_keypress(const int key, const unsigned int modifier) +{ + switch (key) { + + case 512 + SDL_BUTTON_WHEELDOWN: + inc_scroll(1.0f); + resize(); + return true; + break; + + case 512 + SDL_BUTTON_WHEELUP: + dec_scroll(1.0f); + resize(); + return true; + break; + + default: + break; + } + + return false; +} + bool compare_listitems(const Widget *first, const Widget *second) { const ListItem *firstitem = dynamic_cast(first); diff --git a/src/ui/listview.h b/src/ui/listview.h index b097184..b4c00dc 100644 --- a/src/ui/listview.h +++ b/src/ui/listview.h @@ -64,22 +64,29 @@ public: * @brief sort child ListItems according to their sortkey * Sort will fail with an assert error if the ListView * contains any non-ListItem child widgets - */ + * */ void sort(); /** * @brief reverse sort child ListItems according to their sortkey * Sort will fail with an assert error if the ListView * contains any non-ListItem child widgets - */ + * */ void sort_reverse(); protected: virtual void resize(); - /// emit event handler + /** + * @brief emit event handler + * */ virtual bool on_emit(Widget *sender, const Event event, void *data); + /** + * @brief keypress event handler + * */ + virtual bool on_keypress(const int key, const unsigned int modifier); + private: float listview_scroll; -- cgit v1.2.3