X-Git-Url: http://git.tdb.fi/?p=libs%2Fgltk.git;a=blobdiff_plain;f=source%2Flist.cpp;fp=source%2Flist.cpp;h=4b6854f72a91fd5cf1785af6adf2a76b70dc1b25;hp=963b442775f982840f27b6d796437f02227e9fac;hb=aa9b8db38efb9e97460c76e27cecc4d1591b23e5;hpb=762a689d799075c0a10e1633bf95d53de80b4572 diff --git a/source/list.cpp b/source/list.cpp index 963b442..4b6854f 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -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; iautosize(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; isize(); + for(size_t i=0; icreate_item(index); else item = new BasicItem(data->get_string(index)); - if(static_cast(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(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; (iis_visible()); ++i) + for(size_t i=rows[first_row].first; (iis_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; (iis_visible()); ++i) + for(size_t i=rows[first_row].first; (iis_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(focus_index+1)=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; iget_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; iview_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+1index) 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(focus_index)); + size_t focus_row = item_index_to_row(focus_index); if(focus_row0 && !ignore_slider_change) { - first_row = max_scroll-static_cast(value); + first_row = max_scroll-static_cast(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 &widths) const if(widths.size() &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 self_widths(widths); check_widths(self_widths); bool update_all = false; - for(unsigned i=0; (!update_all && iwidths[i]; if(update_all)