]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Use size_t to represent counts and indices
[libs/gltk.git] / source / list.cpp
index 963b442775f982840f27b6d796437f02227e9fac..4b6854f72a91fd5cf1785af6adf2a76b70dc1b25 100644 (file)
@@ -53,7 +53,7 @@ void List::autosize_special(const Part &part, Geometry &ageom) const
 
                unsigned items_w = 0;
                unsigned items_h = 0;
-               for(unsigned i=0; i<items.size(); ++i)
+               for(size_t i=0; i<items.size(); ++i)
                {
                        Geometry igeom;
                        items[i]->autosize(igeom);
@@ -100,8 +100,8 @@ void List::set_data(ListData &d)
        for(Item *i: items)
                delete i;
        items.clear();
-       unsigned n_items = data->size();
-       for(unsigned i=0; i<n_items; ++i)
+       size_t n_items = data->size();
+       for(size_t i=0; i<n_items; ++i)
        {
                Item *item = create_item(i);
                items.push_back(item);
@@ -116,14 +116,14 @@ void List::items_changed()
        mark_rebuild();
 }
 
-List::Item *List::create_item(unsigned index)
+List::Item *List::create_item(size_t index)
 {
        Item *item = nullptr;
        if(item_factory)
                item = item_factory->create_item(index);
        else
                item = new BasicItem(data->get_string(index));
-       if(static_cast<int>(index)==sel_index)
+       if(index==sel_index)
                item->set_active(true);
        add(*item);
        item->autosize();
@@ -148,27 +148,26 @@ void List::set_view_all()
        set_view_size(0);
 }
 
-void List::set_selected_index(int i)
+void List::set_selected_index(size_t i)
 {
-       if(i>=static_cast<int>(data->size()))
+       if(i>=data->size() && i!=INVALID_INDEX)
                throw out_of_range("List::set_selected_index");
 
-       if(i==sel_index || (i<0 && sel_index<0))
+       if(i==sel_index)
                return;
 
-       if(sel_index>=0)
+       if(sel_index!=INVALID_INDEX)
                items[sel_index]->set_active(false);
-       if(i<0)
+
+       sel_index = i;
+       focus_index = i;
+       if(i==INVALID_INDEX)
        {
-               sel_index = -1;
-               focus_index = -1;
                set_input_focus(nullptr);
                signal_selection_cleared.emit();
        }
        else
        {
-               sel_index = i;
-               focus_index = i;
                items[sel_index]->set_active(true);
                if(state&FOCUS)
                        set_input_focus(items[focus_index]);
@@ -178,7 +177,7 @@ void List::set_selected_index(int i)
 
 void List::set_selected_item(Widget *item)
 {
-       for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
+       for(size_t i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
                if(item==items[i])
                        return set_selected_index(i);
 }
@@ -191,8 +190,8 @@ void List::rebuild_special(const Part &part)
        {
                SetFlag flag(ignore_slider_change);
                reposition_items(true);
-               unsigned old_first_row = first_row;
-               unsigned old_max_scroll = max_scroll;
+               size_t old_first_row = first_row;
+               size_t old_max_scroll = max_scroll;
                check_view_range();
                if(first_row!=old_first_row || max_scroll!=old_max_scroll)
                        reposition_items(false);
@@ -205,7 +204,7 @@ void List::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="items")
        {
-               for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
+               for(size_t i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
                        items[i]->render(renderer);
        }
        else if(part.get_name()=="slider")
@@ -228,7 +227,7 @@ void List::button_press(int x, int y, unsigned btn)
 {
        if(btn==4 || btn==5)
        {
-               unsigned change = 3;
+               size_t change = 3;
                if(btn==4)
                {
                        change = min(first_row, change);
@@ -305,11 +304,11 @@ void List::touch_motion(int, int y, unsigned finger)
 void List::focus_in()
 {
        Container::focus_in();
-       if(focus_index>=0 && items[focus_index]->is_visible())
+       if(focus_index!=INVALID_INDEX && items[focus_index]->is_visible())
                set_input_focus(items[focus_index]);
        else
        {
-               if(sel_index>=0 && items[sel_index]->is_visible())
+               if(sel_index!=INVALID_INDEX && items[sel_index]->is_visible())
                        set_focus_index(sel_index);
                else if(!items.empty())
                        set_focus_index(rows[first_row].first);
@@ -337,7 +336,7 @@ void List::move_focus(Navigation nav, bool select)
 {
        if(nav==NAV_UP && view_mode==GRID)
        {
-               unsigned row = item_index_to_row(focus_index);
+               size_t row = item_index_to_row(focus_index);
                if(row>0)
                        set_focus_index(rows[row-1].first+focus_index-rows[row].first);
                else
@@ -345,7 +344,7 @@ void List::move_focus(Navigation nav, bool select)
        }
        else if(nav==NAV_DOWN && view_mode==GRID)
        {
-               unsigned row = item_index_to_row(focus_index);
+               size_t row = item_index_to_row(focus_index);
                if(row+1<rows.size())
                        set_focus_index(rows[row+1].first+focus_index-rows[row].first);
                else
@@ -358,7 +357,7 @@ void List::move_focus(Navigation nav, bool select)
        }
        else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
        {
-               if(static_cast<unsigned>(focus_index+1)<items.size())
+               if(focus_index+1<items.size())
                        set_focus_index(focus_index+1);
        }
 
@@ -366,10 +365,10 @@ void List::move_focus(Navigation nav, bool select)
                set_selected_index(focus_index);
 }
 
-void List::set_focus_index(int i)
+void List::set_focus_index(size_t i)
 {
        focus_index = i;
-       if(focus_index>=0)
+       if(focus_index!=INVALID_INDEX)
        {
                scroll_to_focus();
                if(state&FOCUS)
@@ -400,7 +399,7 @@ void List::reposition_items(bool record_rows)
        unsigned x = 0;
        unsigned y = 0;
        unsigned row_h = 0;
-       for(unsigned i=0; i<items.size(); ++i)
+       for(size_t i=0; i<items.size(); ++i)
        {
                const Geometry &igeom = items[i]->get_geometry();
 
@@ -430,7 +429,7 @@ void List::reposition_items(bool record_rows)
                }
                else
                {
-                       for(unsigned j=rows.back().first; j<=i; ++j)
+                       for(size_t j=rows.back().first; j<=i; ++j)
                                items[j]->set_visible(false);
                        y = 0;
                }
@@ -443,7 +442,7 @@ void List::reposition_items(bool record_rows)
                rows.back().height = row_h;
 }
 
-unsigned List::last_to_first_row(unsigned last) const
+size_t List::last_to_first_row(size_t last) const
 {
        if(!items_part)
                return last;
@@ -452,7 +451,7 @@ unsigned List::last_to_first_row(unsigned last) const
        unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
 
        unsigned items_h = 0;
-       for(unsigned i=last; i<rows.size(); --i)
+       for(size_t i=last; i<rows.size(); --i)
        {
                items_h += rows[i].height;
                if(items_h>view_h)
@@ -462,9 +461,9 @@ unsigned List::last_to_first_row(unsigned last) const
        return 0;
 }
 
-unsigned List::item_index_to_row(unsigned index) const
+size_t List::item_index_to_row(size_t index) const
 {
-       for(unsigned i=0; i+1<rows.size(); ++i)
+       for(size_t i=0; i+1<rows.size(); ++i)
                if(rows[i+1].first>index)
                        return i;
        return rows.size()-1;
@@ -490,10 +489,10 @@ void List::check_view_range()
 
 void List::scroll_to_focus()
 {
-       if(focus_index<0 || items[focus_index]->is_visible())
+       if(focus_index==INVALID_INDEX || items[focus_index]->is_visible())
                return;
 
-       unsigned focus_row = item_index_to_row(static_cast<unsigned>(focus_index));
+       size_t focus_row = item_index_to_row(focus_index);
        if(focus_row<first_row)
                slider.set_value(max_scroll-focus_row);
        else
@@ -504,17 +503,19 @@ void List::slider_value_changed(double value)
 {
        if(max_scroll>0 && !ignore_slider_change)
        {
-               first_row = max_scroll-static_cast<unsigned>(value);
+               first_row = max_scroll-static_cast<size_t>(value);
                mark_rebuild();
        }
 }
 
-void List::adjust_index(int &index, int pos, int change)
+void List::adjust_index(size_t &index, size_t pos, ptrdiff_t change)
 {
-       if(index>pos)
+       if(index==INVALID_INDEX)
+               return;
+       else if(index>pos)
                index += change;
        else if(index==pos)
-               index = (change>0 ? index+change : -1);
+               index = (change>0 ? index+change : INVALID_INDEX);
 }
 
 
@@ -527,7 +528,7 @@ List::DataObserver::DataObserver(List &l):
        list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
 }
 
-void List::DataObserver::item_added(unsigned i)
+void List::DataObserver::item_added(size_t i)
 {
        adjust_index(list.sel_index, i, 1);
        adjust_index(list.focus_index, i, 1);
@@ -537,9 +538,9 @@ void List::DataObserver::item_added(unsigned i)
        list.items_changed();
 }
 
-void List::DataObserver::item_removed(unsigned i)
+void List::DataObserver::item_removed(size_t i)
 {
-       bool had_selection = (list.sel_index>=0);
+       bool had_selection = (list.sel_index!=INVALID_INDEX);
        adjust_index(list.sel_index, i, -1);
        adjust_index(list.focus_index, i, -1);
 
@@ -547,14 +548,14 @@ void List::DataObserver::item_removed(unsigned i)
        list.items.erase(list.items.begin()+i);
        list.items_changed();
 
-       if(had_selection && list.sel_index<0)
+       if(had_selection && list.sel_index==INVALID_INDEX)
                list.signal_selection_cleared.emit();
 }
 
 void List::DataObserver::cleared()
 {
-       list.sel_index = -1;
-       list.focus_index = -1;
+       list.sel_index = INVALID_INDEX;
+       list.focus_index = INVALID_INDEX;
        for(Item *i: list.items)
                delete i;
        list.items.clear();
@@ -563,7 +564,7 @@ void List::DataObserver::cleared()
        list.signal_selection_cleared.emit();
 }
 
-void List::DataObserver::refresh_item(unsigned i)
+void List::DataObserver::refresh_item(size_t i)
 {
        delete list.items[i];
        // Avoid stale pointer while create_item is executing
@@ -628,7 +629,7 @@ void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
        if(widths.size()<children.size())
                widths.resize(children.size(), 0);
 
-       unsigned n = 0;
+       size_t n = 0;
        for(const Child *c: children)
        {
                Geometry cgeom;
@@ -650,7 +651,7 @@ void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
 
        const Sides &margin = part->get_margin();
        int x = margin.left;
-       unsigned n = 0;
+       size_t n = 0;
        for(const Child *c: children)
        {
                c->widget->set_position(x, margin.bottom);
@@ -676,7 +677,7 @@ void List::MultiColumnItem::on_style_change()
        vector<unsigned> self_widths(widths);
        check_widths(self_widths);
        bool update_all = false;
-       for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
+       for(size_t i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
                update_all = self_widths[i]>widths[i];
 
        if(update_all)