From 52bd0792d15e9a814cd38cf77b26784003b8569f Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Sun, 29 Jan 2012 15:16:17 +0000 Subject: --- src/ui/listview.cc | 77 ++++++++++++++++++++++++++++++++++++++---------------- 1 file changed, 55 insertions(+), 22 deletions(-) (limited to 'src/ui/listview.cc') diff --git a/src/ui/listview.cc b/src/ui/listview.cc index 99bcd3c..cd87639 100644 --- a/src/ui/listview.cc +++ b/src/ui/listview.cc @@ -11,12 +11,16 @@ namespace ui ListView::ListView(Widget *parent) : Widget(parent) { + listview_scroll = 0.0f; + listview_selecteditem = 0; + set_label("listview"); set_border(true); set_background(true); - listview_scroll = 0.0f; - listview_selecteditem = 0; + listview_scrollbar = new ScrollBar(this); + listview_scrollbar->set_background(false); + listview_scrollbar->set_border(false); } ListView::~ListView() @@ -30,11 +34,11 @@ void ListView::set_scroll(float scroll) listview_scroll = 0; // calculate maximal scroll size - float top = 0; - for (Children::iterator it = children().begin(); it != children().end(); it++) { - top += (*it)->height(); + float total_height = 0; + for (Children::iterator it = children().begin(); it != children().end(); it++) { + total_height += (*it)->height(); } - if (listview_scroll > top) listview_scroll = top; + if (listview_scroll > total_height) listview_scroll = total_height; } void ListView::inc_scroll(float scroll) @@ -49,24 +53,33 @@ void ListView::dec_scroll(float scroll) void ListView::resize() { - float content_top = 0; + listview_scrollbar->set_size(font()->height(), height()); + listview_scrollbar->set_location(width() - listview_scrollbar->width(), 0); + + float total_height = 0; // reposition all children within the container for (Children::iterator it = children().begin(); it != children().end(); it++) { - (*it)->set_width(width()); - if (content_top - listview_scroll < 0) { - // child widget is invisible - (*it)->hide(); - } else if ((content_top - listview_scroll) >= height()) { - // child widget is invisible - (*it)->hide(); - } else { - (*it)->show(); - (*it)->set_location(0, content_top - listview_scroll); + if ((*it) != static_cast(listview_scrollbar)) { + (*it)->set_width(width() - listview_scrollbar->width()); + if (total_height - listview_scroll < 0) { + // child widget is invisible + (*it)->hide(); + } else if ((total_height - listview_scroll) >= height()) { + // child widget is invisible + (*it)->hide(); + } else { + (*it)->show(); + (*it)->set_location(0, total_height - listview_scroll); + } + + total_height += (*it)->height(); } - - content_top += (*it)->height(); } + + listview_scrollbar->set_range(0.0f, total_height); + listview_scrollbar->set_value(listview_scroll); + } void ListView::deselect() @@ -85,11 +98,23 @@ void ListView::clear() { listview_selecteditem = 0; remove_children(); + + listview_scrollbar = new ScrollBar(this); + listview_scrollbar->set_background(false); + listview_scrollbar->set_border(false); + listview_scrollbar->set_range(0.0f, 0.0f); } bool ListView::on_emit(Widget *sender, const Event event, void *data) { - if ((sender->parent() == this) && (event == Widget::EventListItemClicked)) { + if (sender == listview_scrollbar) { + if (event == Widget::EventScrollBarChanged) { + listview_scroll = listview_scrollbar->value(); + resize(); + } + return true; + + } else if ((sender->parent() == this) && (event == Widget::EventListItemClicked)) { listview_selecteditem = static_cast(sender); emit(EventListViewChanged); return true; @@ -102,7 +127,11 @@ bool compare_listitems(const Widget *first, const Widget *second) { const ListItem *firstitem = dynamic_cast(first); const ListItem *seconditem = dynamic_cast(second); - assert(firstitem && seconditem); + + if (!firstitem) + return true; + else if (!seconditem) + return false; if (firstitem->sortkey() < seconditem->sortkey()) { return true; @@ -122,7 +151,11 @@ bool compare_listitems_reverse(const Widget *first, const Widget *second) { const ListItem *firstitem = dynamic_cast(first); const ListItem *seconditem = dynamic_cast(second); - assert(firstitem && seconditem); + + if (!firstitem) + return true; + else if (!seconditem) + return false; if (firstitem->sortkey() < seconditem->sortkey()) { return false; -- cgit v1.2.3