From: Mikko Rasa Date: Mon, 21 Aug 2023 08:22:43 +0000 (+0300) Subject: Use size_t to represent counts and indices X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=aa9b8db38efb9e97460c76e27cecc4d1591b23e5;p=libs%2Fgltk.git Use size_t to represent counts and indices --- diff --git a/source/dropdown.cpp b/source/dropdown.cpp index 68c1bee..ab00c4c 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -51,7 +51,7 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom) const unsigned max_w = 0; const ListData &data = list.get_data(); - for(unsigned i=0; i(font.get_string_width(data.get_string(i))*font_size); max_w = max(max_w, w); @@ -177,7 +177,7 @@ void Dropdown::resize_list() list.set_geometry(lgeom); } -void Dropdown::list_item_selected(unsigned index) +void Dropdown::list_item_selected(size_t index) { if(dropped) { diff --git a/source/dropdown.h b/source/dropdown.h index c5b535d..3a42d00 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -23,7 +23,7 @@ public: void item(const std::string &); }; - sigc::signal signal_item_selected; + sigc::signal signal_item_selected; private: List list; @@ -68,7 +68,7 @@ private: void close_list(); void list_autosize_changed(); void resize_list(); - void list_item_selected(unsigned); + void list_item_selected(std::size_t); void list_selection_cleared(); }; diff --git a/source/entry.cpp b/source/entry.cpp index b113b20..b212f5f 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -51,7 +51,7 @@ void Entry::set_text(const string &t) set_edit_position(text.size()); } -void Entry::insert(unsigned pos, const string &t) +void Entry::insert(size_t pos, const string &t) { if(t.empty()) return; @@ -66,7 +66,7 @@ void Entry::insert(unsigned pos, const string &t) mark_rebuild(); } -void Entry::erase(unsigned pos, unsigned len) +void Entry::erase(size_t pos, size_t len) { if(!len) return; @@ -81,7 +81,7 @@ void Entry::erase(unsigned pos, unsigned len) mark_rebuild(); } -bool Entry::get_selection(unsigned &start, unsigned &end) const +bool Entry::get_selection(size_t &start, size_t &end) const { if(!selection_active) return false; @@ -94,12 +94,12 @@ bool Entry::get_selection(unsigned &start, unsigned &end) const return true; } -void Entry::translate_position(unsigned pos, unsigned &row, unsigned &col) const +void Entry::translate_position(size_t pos, size_t &row, size_t &col) const { text.offset_to_coords(pos, row, col); } -unsigned Entry::translate_position(unsigned row, unsigned col) const +size_t Entry::translate_position(size_t row, size_t col) const { return text.coords_to_offset(row, col); } @@ -139,7 +139,7 @@ void Entry::rebuild_special(const Part &part) if(!text_part || !graphic || !graphic->get_texture()) return; - unsigned row, col; + size_t row, col; text.offset_to_coords(edit_pos, row, col); if(row=first_row+visible_rows) @@ -160,12 +160,12 @@ void Entry::rebuild_special(const Part &part) if(!text_part || !graphic || !graphic->get_texture()) return; - unsigned start, end; + size_t start, end; get_selection(start, end); - unsigned row, col; + size_t row, col; text.offset_to_coords(start, row, col); - unsigned end_row, end_col; + size_t end_row, end_col; text.offset_to_coords(end, end_row, end_col); if(end_row=first_row+visible_rows) @@ -185,7 +185,7 @@ void Entry::rebuild_special(const Part &part) while(row<=end_row) { - unsigned ec = (row==end_row ? end_col : text.get_line_length(row)); + size_t ec = (row==end_row ? end_col : text.get_line_length(row)); if(ec>col) { Geometry rgeom = text.coords_to_geometry(*text_part, geom, first_row, row, col); @@ -234,7 +234,7 @@ bool Entry::key_press(unsigned key, unsigned mod) erase_selection(true); else if(edit_pos>0) { - unsigned start_pos = text.move_offset(edit_pos, -1); + size_t start_pos = text.move_offset(edit_pos, -1); erase(start_pos, edit_pos-start_pos); } } @@ -244,7 +244,7 @@ bool Entry::key_press(unsigned key, unsigned mod) erase_selection(true); else { - unsigned end_pos = text.move_offset(edit_pos, 1); + size_t end_pos = text.move_offset(edit_pos, 1); erase(edit_pos, end_pos-edit_pos); } } @@ -252,25 +252,25 @@ bool Entry::key_press(unsigned key, unsigned mod) insert(edit_pos, "\n"); else if(key==Input::KEY_END) { - unsigned row, col; + size_t row, col; text.offset_to_coords(edit_pos, row, col); set_edit_position(text.coords_to_offset(row, text.get_line_length(row)), mod==MOD_SHIFT); } else if(key==Input::KEY_HOME) { - unsigned row, col; + size_t row, col; text.offset_to_coords(edit_pos, row, col); set_edit_position(text.coords_to_offset(row, 0), mod==MOD_SHIFT); } else if(key==Input::KEY_PGUP) { - unsigned row, col; + size_t row, col; text.offset_to_coords(edit_pos, row, col); set_edit_position(text.coords_to_offset((row0 ? text.coords_to_offset(row-1, col) : 0), select); } @@ -377,10 +377,10 @@ void Entry::move_edit_position(Navigation nav, bool select) throw invalid_argument("Entry::move_edit_position"); } -void Entry::adjust_edit_position_for_change(unsigned pos, int change) +void Entry::adjust_edit_position_for_change(size_t pos, ptrdiff_t change) { - unsigned old_edit_pos = edit_pos; - unsigned old_select_pos = selection_pos; + size_t old_edit_pos = edit_pos; + size_t old_select_pos = selection_pos; if(change>0) { @@ -392,35 +392,35 @@ void Entry::adjust_edit_position_for_change(unsigned pos, int change) else if(change<0) { if(edit_pos>=pos) - edit_pos -= min(edit_pos-pos, -change); + edit_pos -= min(edit_pos-pos, -change); if(selection_active && selection_pos>=pos) - selection_pos -= min(selection_pos-pos, -change); + selection_pos -= min(selection_pos-pos, -change); } if(edit_pos!=old_edit_pos) signal_edit_position_changed.emit(edit_pos); if(selection_active && (edit_pos!=old_edit_pos || selection_pos!=old_select_pos)) { - unsigned start, end; + size_t start, end; if(get_selection(start, end)) signal_selection_changed.emit(start, end); } } -void Entry::set_edit_position(unsigned ep, bool select) +void Entry::set_edit_position(size_t ep, bool select) { bool selection_was_active = selection_active; if(select && !selection_active) selection_pos = edit_pos; selection_active = select; - unsigned old_edit_pos = edit_pos; + size_t old_edit_pos = edit_pos; edit_pos = min(ep, text.size()); if(edit_pos!=old_edit_pos) { signal_edit_position_changed.emit(edit_pos); - unsigned start, end; + size_t start, end; if(get_selection(start, end)) signal_selection_changed.emit(start, end); else if(selection_was_active) @@ -434,7 +434,7 @@ void Entry::set_edit_position(unsigned ep, bool select) void Entry::erase_selection(bool emit_change) { - unsigned start, end; + size_t start, end; if(!get_selection(start, end)) return; @@ -470,16 +470,16 @@ void Entry::check_view_range() visible_rows = text.get_visible_lines(*text_part, geom, nullptr); - unsigned row, col; + size_t row, col; text.offset_to_coords(edit_pos, row, col); - unsigned old_first_row = first_row; + size_t old_first_row = first_row; if(first_row>row) first_row = row; else if(row>=first_row+visible_rows) first_row = row+1-visible_rows; - unsigned scroll = max(text.get_n_lines(), visible_rows)-visible_rows; + size_t scroll = max(text.get_n_lines(), visible_rows)-visible_rows; if(first_row>scroll) first_row = scroll; @@ -495,8 +495,8 @@ void Entry::slider_value_changed(double value) { if(text.get_n_lines()>visible_rows) { - unsigned old_first_row = first_row; - first_row = text.get_n_lines()-visible_rows-static_cast(value); + size_t old_first_row = first_row; + first_row = text.get_n_lines()-visible_rows-static_cast(value); if(first_row!=old_first_row) signal_scroll_position_changed.emit(first_row); mark_rebuild(); diff --git a/source/entry.h b/source/entry.h index 0573671..ee390a0 100644 --- a/source/entry.h +++ b/source/entry.h @@ -42,15 +42,15 @@ private: bool multiline = false; unsigned edit_width = 10; unsigned edit_height = 1; - unsigned edit_pos = 0; - unsigned first_row = 0; + std::size_t edit_pos = 0; + std::size_t first_row = 0; unsigned visible_rows = 1; const Part *text_part = nullptr; VSlider *slider = nullptr; bool got_key_press = false; bool cursor_blink = true; bool selection_active = false; - unsigned selection_pos = 0; + std::size_t selection_pos = 0; public: Entry(const std::string & = std::string()); @@ -62,17 +62,17 @@ private: public: void set_text(const std::string &); - void insert(unsigned, const std::string &); - void erase(unsigned, unsigned); + void insert(std::size_t, const std::string &); + void erase(std::size_t, std::size_t); const std::string &get_text() const { return text.get(); } - void set_edit_position(unsigned p) { set_edit_position(p, false); } - unsigned get_edit_position() const { return edit_pos; } + void set_edit_position(std::size_t p) { set_edit_position(p, false); } + std::size_t get_edit_position() const { return edit_pos; } unsigned get_scroll_position() const { return first_row; } unsigned get_visible_rows() const { return visible_rows; } - bool get_selection(unsigned &, unsigned &) const; - void translate_position(unsigned, unsigned &, unsigned &) const; - unsigned translate_position(unsigned, unsigned) const; + bool get_selection(std::size_t &, std::size_t &) const; + void translate_position(std::size_t, std::size_t &, std::size_t &) const; + std::size_t translate_position(std::size_t, std::size_t) const; /** Sets the minimum size of the editing area, in characters and rows. This only affects autosizing. */ @@ -98,8 +98,8 @@ private: void on_style_change() override; void move_edit_position(Navigation, bool); - void adjust_edit_position_for_change(unsigned, int); - void set_edit_position(unsigned, bool); + void adjust_edit_position_for_change(std::size_t, std::ptrdiff_t); + void set_edit_position(std::size_t, bool); void erase_selection(bool); void check_cursor_blink(); void check_view_range(); diff --git a/source/graphic.cpp b/source/graphic.cpp index 095e194..fb3f889 100644 --- a/source/graphic.cpp +++ b/source/graphic.cpp @@ -16,18 +16,18 @@ void Graphic::build(unsigned wd, unsigned ht, GL::PrimitiveBuilder &bld) const create_texcoords(slice.x, slice.x+slice.w, border.left, border.right, texture->get_width(), u); create_texcoords(slice.y, slice.y+slice.h, border.bottom, border.top, texture->get_height(), v); - unsigned xmin = border.left ? 0 : 1; - unsigned xmax = x.size()-(border.right ? 2 : 3); - unsigned ymin = border.bottom ? 0 : 1; - unsigned ymax = y.size()-(border.top ? 2 : 3); + size_t xmin = border.left ? 0 : 1; + size_t xmax = x.size()-(border.right ? 2 : 3); + size_t ymin = border.bottom ? 0 : 1; + size_t ymax = y.size()-(border.top ? 2 : 3); bld.color(1.0f, 1.0f, 1.0f); - for(unsigned i=ymin; i<=ymax; ++i) + for(size_t i=ymin; i<=ymax; ++i) { - unsigned i2 = (i==0 ? 0 : i==y.size()-2 ? 2 : 1); - for(unsigned j=xmin; j<=xmax; ++j) + size_t i2 = (i==0 ? 0 : i==y.size()-2 ? 2 : 1); + for(size_t j=xmin; j<=xmax; ++j) { - unsigned j2 = (j==0 ? 0 : j==x.size()-2 ? 2 : 1); + size_t j2 = (j==0 ? 0 : j==x.size()-2 ? 2 : 1); if(j==xmin || (j>1 && jxmin) diff --git a/source/grid.cpp b/source/grid.cpp index c8631cf..eb32cc9 100644 --- a/source/grid.cpp +++ b/source/grid.cpp @@ -3,7 +3,7 @@ namespace Msp { namespace GLtk { -Grid::Grid(Layout &l, unsigned c): +Grid::Grid(Layout &l, size_t c): Arrangement(l), columns(c) { } diff --git a/source/grid.h b/source/grid.h index 4ed43d5..be1c266 100644 --- a/source/grid.h +++ b/source/grid.h @@ -31,10 +31,10 @@ private: Edge row_top; Edge row_bottom; bool first_row = true; - unsigned column = 0; + std::size_t column = 0; public: - Grid(Layout &, unsigned); + Grid(Layout &, std::size_t); void skip(); void next_row(); diff --git a/source/layout.cpp b/source/layout.cpp index 4d6b664..e9461b5 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -19,44 +19,44 @@ public: { private: LinearProgram &linprog; - unsigned index; + size_t index; public: - Row(LinearProgram &, unsigned); + Row(LinearProgram &, size_t); - float &operator[](unsigned); + float &operator[](size_t); float &back(); }; private: struct Column { - unsigned basic; + size_t basic; std::vector values; Column(); }; - unsigned n_columns = 1; - unsigned n_rows = 1; + size_t n_columns = 1; + size_t n_rows = 1; std::vector columns; bool solved = false; bool infeasible = false; public: - LinearProgram(unsigned); + LinearProgram(size_t); Row add_row(); - Row operator[](unsigned); + Row operator[](size_t); Row get_objective_row(); - float get_variable(unsigned); + float get_variable(size_t); bool solve(); private: void prepare_columns(); void add_artificial_variables(); void remove_artificial_variables(); - unsigned find_minimal_ratio(unsigned); - void make_basic_column(unsigned, unsigned); + size_t find_minimal_ratio(size_t); + void make_basic_column(size_t, size_t); bool pivot(); }; @@ -199,7 +199,7 @@ void Layout::remove_widget(Widget &wdg) void Layout::update_slot_indices() { n_active_slots = 0; - unsigned n_floating = 0; + size_t n_floating = 0; for(Slot *s: slots) { if(s->widget.is_visible() || s->ghost) @@ -351,7 +351,7 @@ void Layout::solve_constraints(int dir, SolveMode mode) remaining three are slack columns; see below for their purposes. */ LinearProgram linprog(n_active_slots*5+n_slack_vars[dir]+1); float weight = slots.size()+1; - unsigned k = n_active_slots*5; + size_t k = n_active_slots*5; for(const Slot *s: slots) { if(s->index<0) @@ -624,7 +624,7 @@ void operator>>(const LexicalConverter &conv, Layout::ConstraintType &ctype) } -Layout::LinearProgram::LinearProgram(unsigned s): +Layout::LinearProgram::LinearProgram(size_t s): n_columns(s), columns(n_columns) { } @@ -634,7 +634,7 @@ Layout::LinearProgram::Row Layout::LinearProgram::add_row() return Row(*this, n_rows++); } -Layout::LinearProgram::Row Layout::LinearProgram::operator[](unsigned r) +Layout::LinearProgram::Row Layout::LinearProgram::operator[](size_t r) { if(r>=n_rows) throw out_of_range("LinearProgram::operator[]"); @@ -647,14 +647,14 @@ Layout::LinearProgram::Row Layout::LinearProgram::get_objective_row() return Row(*this, 0); } -float Layout::LinearProgram::get_variable(unsigned i) +float Layout::LinearProgram::get_variable(size_t i) { if(!solved || infeasible) throw logic_error("not solved"); if(i+1>=n_columns) throw out_of_range("LinearProgram::get_variable"); - if(unsigned r = columns[i].basic) + if(size_t r = columns[i].basic) return columns.back().values[r]; else return 0; @@ -707,7 +707,7 @@ void Layout::LinearProgram::prepare_columns() if(c.values.size()>=2 && c.values.back()!=0.0f && (constants.size()=0.0f) && obj_coeff[c.values.size()-1]==0.0f) { bool basic = true; - for(unsigned j=1; (basic && j+1 artificial_rows(n_rows-1); - for(unsigned i=0; i artificial_rows(n_rows-1); + for(size_t i=0; i::infinity(); - unsigned row = 0; - for(unsigned i=1; i0) { float ratio = columns.back().values[i]/columns[c].values[i]; @@ -826,11 +826,11 @@ unsigned Layout::LinearProgram::find_minimal_ratio(unsigned c) return row; } -void Layout::LinearProgram::make_basic_column(unsigned c, unsigned r) +void Layout::LinearProgram::make_basic_column(size_t c, size_t r) { /* Perform row transfer operations to make the pivot column basic, containing a 1 on the pivot row. */ - for(unsigned i=0; i0) - if(unsigned row = find_minimal_ratio(i)) + if(size_t row = find_minimal_ratio(i)) { make_basic_column(i, row); return true; @@ -869,12 +869,12 @@ bool Layout::LinearProgram::pivot() } -Layout::LinearProgram::Row::Row(LinearProgram &lp, unsigned i): +Layout::LinearProgram::Row::Row(LinearProgram &lp, size_t i): linprog(lp), index(i) { } -float &Layout::LinearProgram::Row::operator[](unsigned c) +float &Layout::LinearProgram::Row::operator[](size_t c) { if(c>=linprog.n_columns) throw out_of_range("Row::operator[]"); diff --git a/source/layout.h b/source/layout.h index 14def25..2e58505 100644 --- a/source/layout.h +++ b/source/layout.h @@ -174,8 +174,8 @@ private: Container *container = nullptr; std::vector slots; - unsigned n_active_slots = 0; - unsigned n_slack_vars[2] = { 0, 0 }; + std::size_t n_active_slots = 0; + std::size_t n_slack_vars[2] = { 0, 0 }; Sides margin{ 8 }; unsigned row_spacing = 5; unsigned col_spacing = 4; 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) diff --git a/source/list.h b/source/list.h index 060fb81..0938eaf 100644 --- a/source/list.h +++ b/source/list.h @@ -36,6 +36,8 @@ public: GRID }; + static constexpr size_t INVALID_INDEX = std::numeric_limits::max(); + class Loader: public DataFile::DerivedObjectLoader { public: @@ -54,10 +56,10 @@ private: public: DataObserver(List &); - void item_added(unsigned); - void item_removed(unsigned); + void item_added(std::size_t); + void item_removed(std::size_t); void cleared(); - void refresh_item(unsigned); + void refresh_item(std::size_t); }; public: @@ -114,7 +116,7 @@ private: virtual ~ItemFactory() = default; virtual void set_data(const ListData &) = 0; - virtual Item *create_item(unsigned) const = 0; + virtual Item *create_item(std::size_t) const = 0; }; template @@ -137,7 +139,7 @@ private: throw incompatible_data(typeid(ValueType)); } - Item *create_item(unsigned i) const override + Item *create_item(std::size_t i) const override { return new I(data->get(i)); } @@ -145,14 +147,14 @@ private: struct Row { - unsigned first; + std::size_t first; unsigned height; - Row(unsigned f): first(f), height(0) { } + Row(std::size_t f): first(f), height(0) { } }; public: - sigc::signal signal_item_selected; + sigc::signal signal_item_selected; sigc::signal signal_selection_cleared; private: @@ -161,10 +163,10 @@ private: DataObserver *observer = nullptr; ItemFactory *item_factory = nullptr; ViewMode view_mode = LIST; - int sel_index = -1; - int focus_index = -1; - unsigned first_row = 0; - unsigned max_scroll = 0; + std::size_t sel_index = INVALID_INDEX; + std::size_t focus_index = INVALID_INDEX; + std::size_t first_row = 0; + std::size_t max_scroll = 0; unsigned view_rows = 5; unsigned view_columns = 5; const Part *items_part = nullptr; @@ -203,7 +205,7 @@ public: item_factory = f; } private: - Item *create_item(unsigned); + Item *create_item(std::size_t); public: void set_view_mode(ViewMode); @@ -211,7 +213,7 @@ public: void set_view_size(unsigned, unsigned); void set_view_all(); - void set_selected_index(int); + void set_selected_index(std::size_t); int get_selected_index() const { return sel_index; } private: void set_selected_item(Widget *); @@ -231,16 +233,16 @@ private: void on_style_change() override; void move_focus(Navigation, bool); - void set_focus_index(int); + void set_focus_index(std::size_t); void item_autosize_changed(Item *); void reposition_items(bool); - unsigned last_to_first_row(unsigned) const; - unsigned item_index_to_row(unsigned) const; + std::size_t last_to_first_row(std::size_t) const; + std::size_t item_index_to_row(std::size_t) const; void check_view_range(); void scroll_to_focus(); void slider_value_changed(double); - static void adjust_index(int &, int, int); + static void adjust_index(std::size_t &, std::size_t, std::ptrdiff_t); }; MSPGLTK_API void operator>>(const LexicalConverter &, List::ViewMode &); diff --git a/source/listdata.h b/source/listdata.h index 77be5ee..cd4fb9a 100644 --- a/source/listdata.h +++ b/source/listdata.h @@ -14,19 +14,19 @@ namespace GLtk { class MSPGLTK_API ListData { public: - sigc::signal signal_item_added; - sigc::signal signal_item_removed; + sigc::signal signal_item_added; + sigc::signal signal_item_removed; sigc::signal signal_cleared; - sigc::signal signal_refresh_item; + sigc::signal signal_refresh_item; protected: ListData() = default; public: virtual ~ListData() = default; - virtual unsigned size() const = 0; - virtual std::string get_string(unsigned) const = 0; - void refresh(unsigned i) const + virtual std::size_t size() const = 0; + virtual std::string get_string(std::size_t) const = 0; + void refresh(std::size_t i) const { if(i>=size()) throw std::out_of_range("ListData::refresh"); @@ -45,7 +45,7 @@ protected: public: void append(const T &v) { insert(items.size(), v); } - void insert(unsigned i, const T &v) + void insert(std::size_t i, const T &v) { if(i>items.size()) throw std::out_of_range("ListDataStore::insert"); @@ -54,7 +54,7 @@ public: signal_item_added.emit(i); } - const T &get(unsigned i) const + const T &get(std::size_t i) const { if(i>=items.size()) throw std::out_of_range("ListDataStore::get"); @@ -62,23 +62,23 @@ public: return items[i]; } - int find(const T &v) const + std::size_t find(const T &v) const { - for(unsigned i=0; i::max(); } using ListData::refresh; void refresh(const T &v) const { - int i = find(v); - if(i>=0) + std::size_t i = find(v); + if(i=items.size()) throw std::out_of_range("ListDataStore::remove"); @@ -93,14 +93,14 @@ public: signal_cleared.emit(); } - unsigned size() const override { return items.size(); } + std::size_t size() const override { return items.size(); } }; template class BasicListData: public ListDataStore { public: - virtual std::string get_string(unsigned i) const + virtual std::string get_string(std::size_t i) const { return lexical_cast(this->get(i)); } }; @@ -116,7 +116,7 @@ private: public: FunctionListData(Func f): func(f) { } - std::string get_string(unsigned i) const override + std::string get_string(std::size_t i) const override { return func(this->get(i)); } }; diff --git a/source/panel.cpp b/source/panel.cpp index 1556826..31cdf4b 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -288,7 +288,7 @@ void Panel::Loader::gravity(int h, int v) get_layout().set_gravity(get_last_widget(), h, v); } -void Panel::Loader::grid(unsigned cols) +void Panel::Loader::grid(size_t cols) { Grid grd(get_layout(), cols); ArrangedLoader ldr(*this, grd); diff --git a/source/panel.h b/source/panel.h index ce4b2a7..e9744fd 100644 --- a/source/panel.h +++ b/source/panel.h @@ -46,7 +46,7 @@ public: void expand(bool, bool); void ghost(bool); void gravity(int, int); - void grid(unsigned); + void grid(std::size_t); void layout(); template void unnamed_child(); diff --git a/source/slider.cpp b/source/slider.cpp index 29f9067..b860d7d 100644 --- a/source/slider.cpp +++ b/source/slider.cpp @@ -22,7 +22,7 @@ void Slider::set_value(double v) value = max; else { - unsigned steps = static_cast((v-min)/step+0.5); + double steps = round((v-min)/step); value = min+steps*step; } diff --git a/source/text.cpp b/source/text.cpp index 166bbb5..2e4d9cb 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -18,8 +18,8 @@ struct Text::RenderData struct Text::CoordsToGeomData { - unsigned row; - unsigned col; + size_t row; + size_t col; Geometry result; }; @@ -80,7 +80,7 @@ void Text::set(const string &t) find_lines(); } -void Text::erase(unsigned pos, unsigned len) +void Text::erase(size_t pos, size_t len) { check_alignment(pos); check_alignment(pos+len); @@ -100,7 +100,7 @@ void Text::erase(unsigned pos, unsigned len) } } -void Text::insert(unsigned pos, const string &s) +void Text::insert(size_t pos, const string &s) { check_alignment(pos); text.insert(pos, s); @@ -119,7 +119,7 @@ void Text::insert(unsigned pos, const string &s) } } -unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const +size_t Text::get_visible_lines(const Part &part, const Geometry &parent, unsigned *fit_height) const { const GL::Font &font = style->get_font(); float font_size = style->get_font_size(); @@ -129,21 +129,21 @@ unsigned Text::get_visible_lines(const Part &part, const Geometry &parent, unsig const Sides &margin = part.get_margin(); unsigned vmargin = margin.top+margin.bottom; unsigned free_height = max(parent.h, vmargin)-vmargin+line_spacing-line_height; - unsigned n_lines = min(lines.size(), max(free_height/line_spacing, 1U)); + size_t n_lines = min(lines.size(), max(free_height/line_spacing, 1U)); if(fit_height) *fit_height = line_height+(n_lines-1)*line_spacing; return n_lines; } -unsigned Text::get_line_length(unsigned i) const +size_t Text::get_line_length(size_t i) const { if(i>=lines.size()) throw out_of_range("Text::get_line_length"); return lines[i].length; } -unsigned Text::move_offset(unsigned offs, int change) const +size_t Text::move_offset(size_t offs, ptrdiff_t change) const { check_alignment(offs); if(!change) @@ -169,7 +169,7 @@ unsigned Text::move_offset(unsigned offs, int change) const return i-text.begin(); } -void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const +void Text::offset_to_coords(size_t offs, size_t &row, size_t &col) const { if(lines.empty()) { @@ -178,7 +178,7 @@ void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const return; } - for(unsigned i=0; i=lines[i].start && offs<=lines[i].start+lines[i].bytes) { row = i; @@ -190,7 +190,7 @@ void Text::offset_to_coords(unsigned offs, unsigned &row, unsigned &col) const } } -unsigned Text::coords_to_offset(unsigned row, unsigned col) const +size_t Text::coords_to_offset(size_t row, size_t col) const { if(row>=lines.size()) return text.size(); @@ -210,7 +210,7 @@ unsigned Text::coords_to_offset(unsigned row, unsigned col) const } } -Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsigned first_row, unsigned row, unsigned col) const +Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, size_t first_row, size_t row, size_t col) const { if(row>=lines.size()) row = lines.size()-1; @@ -232,7 +232,7 @@ void Text::build(const Part &part, State state, const Geometry &parent, PartCach build(part, state, parent, 0, cache); } -void Text::build(const Part &part, State state, const Geometry &parent, unsigned first_row, PartCache &cache) const +void Text::build(const Part &part, State state, const Geometry &parent, size_t first_row, PartCache &cache) const { if(!style || lines.empty()) return; @@ -280,18 +280,18 @@ void Text::find_lines() } } -unsigned Text::count_characters(unsigned start, unsigned bytes) const +size_t Text::count_characters(size_t start, size_t bytes) const { StringCodec::Utf8::Decoder dec; auto i = text.begin()+start; auto end = i+bytes; - unsigned count = 0; + size_t count = 0; for(; i -void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, void (Text::*func)(unsigned, const Geometry &, T &) const, T &data) const +void Text::process_lines(const Part &part, const Geometry &parent, size_t first_row, void (Text::*func)(size_t, const Geometry &, T &) const, T &data) const { if(!style) return; @@ -310,10 +310,10 @@ void Text::process_lines(const Part &part, const Geometry &parent, unsigned firs int y_offset = static_cast(-font.get_descent()*font_size); unsigned fit_height; - unsigned n_lines = get_visible_lines(part, parent, &fit_height); - first_row = min(first_row, lines.size()-n_lines); + size_t n_lines = get_visible_lines(part, parent, &fit_height); + first_row = min(first_row, lines.size()-n_lines); - for(unsigned i=0; iget_font().build_string(text.substr(line.start, line.bytes), *data.bld); } -void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomData &data) const +void Text::coords_to_geom_line(size_t i, const Geometry &rgeom, CoordsToGeomData &data) const { if(i==data.row) { @@ -349,7 +349,7 @@ void Text::coords_to_geom_line(unsigned i, const Geometry &rgeom, CoordsToGeomDa else { StringCodec::Utf8::Decoder dec; - for(unsigned c=data.col; c; --c) + for(size_t c=data.col; c; --c) dec.decode_char(text, j); } float w = style->get_font().get_string_width(string(begin, j)); diff --git a/source/text.h b/source/text.h index 6de3573..3387f36 100644 --- a/source/text.h +++ b/source/text.h @@ -22,9 +22,9 @@ class MSPGLTK_API Text private: struct Line { - unsigned start = 0; - unsigned bytes = 0; - unsigned length = 0; + std::size_t start = 0; + std::size_t bytes = 0; + std::size_t length = 0; unsigned width = 0; }; @@ -46,32 +46,32 @@ public: void autosize(const Part &, Geometry &) const; void set(const std::string &); - void erase(unsigned, unsigned); - void insert(unsigned, const std::string &); + void erase(std::size_t, std::size_t); + void insert(std::size_t, const std::string &); const std::string &get() const { return text; } - unsigned size() const { return text.size(); } - unsigned get_n_lines() const { return lines.size(); } - unsigned get_visible_lines(const Part &, const Geometry &, unsigned *) const; - unsigned get_line_length(unsigned) const; - unsigned move_offset(unsigned, int) const; - void offset_to_coords(unsigned, unsigned &, unsigned &) const; - unsigned coords_to_offset(unsigned, unsigned) const; - Geometry coords_to_geometry(const Part &, const Geometry &, unsigned, unsigned, unsigned) const; + std::size_t size() const { return text.size(); } + std::size_t get_n_lines() const { return lines.size(); } + std::size_t get_visible_lines(const Part &, const Geometry &, unsigned *) const; + std::size_t get_line_length(std::size_t) const; + std::size_t move_offset(std::size_t, std::ptrdiff_t) const; + void offset_to_coords(std::size_t, std::size_t &, std::size_t &) const; + std::size_t coords_to_offset(std::size_t, std::size_t) const; + Geometry coords_to_geometry(const Part &, const Geometry &, std::size_t, std::size_t, std::size_t) const; void build(const Part &, State, const Geometry &, PartCache &) const; - void build(const Part &, State, const Geometry &, unsigned, PartCache &) const; + void build(const Part &, State, const Geometry &, std::size_t, PartCache &) const; Text &operator=(const std::string &); private: void find_lines(); - unsigned count_characters(unsigned, unsigned) const; - void check_alignment(unsigned) const; + std::size_t count_characters(std::size_t, std::size_t) const; + void check_alignment(std::size_t) const; template - void process_lines(const Part &, const Geometry &, unsigned, void (Text::*)(unsigned, const Geometry &, T &) const, T &) const; + void process_lines(const Part &, const Geometry &, std::size_t, void (Text::*)(std::size_t, const Geometry &, T &) const, T &) const; - void build_line(unsigned, const Geometry &, RenderData &) const; - void coords_to_geom_line(unsigned, const Geometry &, CoordsToGeomData &) const; + void build_line(std::size_t, const Geometry &, RenderData &) const; + void coords_to_geom_line(std::size_t, const Geometry &, CoordsToGeomData &) const; }; } // namespace GLtk