]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Refactor the traversal logic out of List::check_view_range
[libs/gltk.git] / source / list.cpp
index f61d7c26dec8f3ba194058050a4411f423e6e646..b9713422f11236fa86bc23a87a64f59523365569 100644 (file)
@@ -147,13 +147,16 @@ void List::set_selected_index(int i)
        if(i>=static_cast<int>(data->size()))
                throw out_of_range("List::set_selected_index");
 
-       if(i==sel_index)
+       if(i==sel_index || (i<0 && sel_index<0))
                return;
 
        if(sel_index>=0)
                items[sel_index]->set_active(false);
        if(i<0)
+       {
                sel_index = -1;
+               signal_selection_cleared.emit();
+       }
        else
        {
                sel_index = i;
@@ -251,29 +254,39 @@ void List::item_autosize_changed(Item *item)
        rebuild();
 }
 
-void List::check_view_range()
+unsigned List::last_to_first(unsigned last) const
 {
        if(!style)
-               return;
+               return last;
 
-       unsigned h = geom.h;
+       unsigned view_h = geom.h;
        if(const Part *items_part = style->get_part("items"))
        {
                const Sides &margin = items_part->get_margin();
-               h -= margin.top+margin.bottom;
+               view_h -= margin.top+margin.bottom;
        }
 
-       max_scroll = items.size();
-       for(unsigned i=items.size(); i-->0; )
+       unsigned items_h = 0;
+       for(unsigned i=last; i<items.size(); --i)
        {
-               unsigned ih = items[i]->get_geometry().h;
-               if(ih<=h)
-               {
-                       h -= ih;
-                       --max_scroll;
-               }
+               items_h += items[i]->get_geometry().h;
+               if(items_h>view_h)
+                       return min(i+1, last);
        }
 
+       return 0;
+}
+
+void List::check_view_range()
+{
+       if(!style)
+               return;
+
+       if(items.empty())
+               max_scroll = 0;
+       else
+               max_scroll = last_to_first(items.size()-1);
+
        if(first>max_scroll)
                first = max_scroll;
 
@@ -312,6 +325,7 @@ void List::DataObserver::item_added(unsigned i)
 
 void List::DataObserver::item_removed(unsigned i)
 {
+       bool had_selection = (list.sel_index>=0);
        if(list.sel_index>static_cast<int>(i))
                --list.sel_index;
        else if(list.sel_index==static_cast<int>(i))
@@ -320,6 +334,9 @@ void List::DataObserver::item_removed(unsigned i)
        delete list.items[i];
        list.items.erase(list.items.begin()+i);
        list.items_changed();
+
+       if(had_selection && list.sel_index<0)
+               list.signal_selection_cleared.emit();
 }
 
 void List::DataObserver::cleared()
@@ -329,6 +346,8 @@ void List::DataObserver::cleared()
                delete *i;
        list.items.clear();
        list.items_changed();
+
+       list.signal_selection_cleared.emit();
 }
 
 void List::DataObserver::refresh_item(unsigned i)