Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2012-02-12 12:12:53 +0000
committerStijn Buys <ingar@osirion.org>2012-02-12 12:12:53 +0000
commit130a23f483fe2e486e6d9d804b9d998c159fbcb0 (patch)
tree6360656b183866ec24476a79d9389a47a6ad60c1 /src/ui/listview.cc
parentddee07454e4910a218ed58febe910904bd50ae57 (diff)
Scroll listview content by item count instead of by height,
enabled listview mousewheel scrolling.
Diffstat (limited to 'src/ui/listview.cc')
-rw-r--r--src/ui/listview.cc53
1 files changed, 46 insertions, 7 deletions
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<Widget *>(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<Widget *>(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<const ListItem *>(first);