unsigned max_w = 0;
const ListData &data = list.get_data();
- for(unsigned i=0; i<data.size(); ++i)
+ for(size_t i=0; i<data.size(); ++i)
{
unsigned w = static_cast<unsigned>(font.get_string_width(data.get_string(i))*font_size);
max_w = max(max_w, w);
list.set_geometry(lgeom);
}
-void Dropdown::list_item_selected(unsigned index)
+void Dropdown::list_item_selected(size_t index)
{
if(dropped)
{
void item(const std::string &);
};
- sigc::signal<void, unsigned> signal_item_selected;
+ sigc::signal<void, std::size_t> signal_item_selected;
private:
List list;
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();
};
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;
mark_rebuild();
}
-void Entry::erase(unsigned pos, unsigned len)
+void Entry::erase(size_t pos, size_t len)
{
if(!len)
return;
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;
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);
}
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 || row>=first_row+visible_rows)
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 || row>=first_row+visible_rows)
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);
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);
}
}
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);
}
}
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((row<visible_rows ? 0 : row-visible_rows), col), mod==MOD_SHIFT);
}
else if(key==Input::KEY_PGDN)
{
- unsigned row, col;
+ size_t row, col;
text.offset_to_coords(edit_pos, row, col);
set_edit_position(text.coords_to_offset(row+visible_rows, col), mod==MOD_SHIFT);
}
set_edit_position(text.move_offset(edit_pos, 1), select);
else if(nav==NAV_DOWN)
{
- unsigned row, col;
+ size_t row, col;
text.offset_to_coords(edit_pos, row, col);
set_edit_position(text.coords_to_offset(row+1, col), select);
}
else if(nav==NAV_UP)
{
- unsigned row, col;
+ size_t row, col;
text.offset_to_coords(edit_pos, row, col);
set_edit_position((row>0 ? text.coords_to_offset(row-1, col) : 0), 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)
{
else if(change<0)
{
if(edit_pos>=pos)
- edit_pos -= min<unsigned>(edit_pos-pos, -change);
+ edit_pos -= min<size_t>(edit_pos-pos, -change);
if(selection_active && selection_pos>=pos)
- selection_pos -= min<unsigned>(selection_pos-pos, -change);
+ selection_pos -= min<size_t>(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)
void Entry::erase_selection(bool emit_change)
{
- unsigned start, end;
+ size_t start, end;
if(!get_selection(start, end))
return;
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<size_t>(text.get_n_lines(), visible_rows)-visible_rows;
if(first_row>scroll)
first_row = scroll;
{
if(text.get_n_lines()>visible_rows)
{
- unsigned old_first_row = first_row;
- first_row = text.get_n_lines()-visible_rows-static_cast<unsigned>(value);
+ size_t old_first_row = first_row;
+ first_row = text.get_n_lines()-visible_rows-static_cast<size_t>(value);
if(first_row!=old_first_row)
signal_scroll_position_changed.emit(first_row);
mark_rebuild();
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());
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. */
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();
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 && j<x.size()-2))
{
if(j>xmin)
namespace Msp {
namespace GLtk {
-Grid::Grid(Layout &l, unsigned c):
+Grid::Grid(Layout &l, size_t c):
Arrangement(l),
columns(c)
{ }
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();
{
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<float> values;
Column();
};
- unsigned n_columns = 1;
- unsigned n_rows = 1;
+ size_t n_columns = 1;
+ size_t n_rows = 1;
std::vector<Column> 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();
};
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)
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)
}
-Layout::LinearProgram::LinearProgram(unsigned s):
+Layout::LinearProgram::LinearProgram(size_t s):
n_columns(s),
columns(n_columns)
{ }
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[]");
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;
if(c.values.size()>=2 && c.values.back()!=0.0f && (constants.size()<c.values.size() || c.values.back()*constants[c.values.size()-1]>=0.0f) && obj_coeff[c.values.size()-1]==0.0f)
{
bool basic = true;
- for(unsigned j=1; (basic && j+1<c.values.size()); ++j)
+ for(size_t j=1; (basic && j+1<c.values.size()); ++j)
basic = (c.values[j]==0.0f);
if(basic)
{
for(Column &c: columns)
if(!c.values.empty())
{
- for(unsigned j=0; j<c.values.size(); ++j)
+ for(size_t j=0; j<c.values.size(); ++j)
{
c.values[j] *= row_coeff[j];
c.values.front() += obj_coeff[j]*c.values[j];
void Layout::LinearProgram::add_artificial_variables()
{
- vector<unsigned> artificial_rows(n_rows-1);
- for(unsigned i=0; i<artificial_rows.size(); ++i)
+ vector<size_t> artificial_rows(n_rows-1);
+ for(size_t i=0; i<artificial_rows.size(); ++i)
artificial_rows[i] = i+1;
for(const Column &c: columns)
{
objective = c.values.front();
c.values.front() = 0.0f;
- for(unsigned r: artificial_rows)
+ for(size_t r: artificial_rows)
if(r<c.values.size())
c.values.front() += c.values[r];
}
columns.resize(n_columns+artificial_rows.size());
columns.back() = columns[n_columns-1];
columns[n_columns-1].values.clear();
- for(unsigned i=0; i<artificial_rows.size(); ++i)
+ for(size_t i=0; i<artificial_rows.size(); ++i)
columns[n_columns+i-1].basic = artificial_rows[i];
}
some of the original variables basic instead.
I don't fully understand why this is needed, but it appears to work. */
- for(unsigned i=n_columns-1; i+1<columns.size(); ++i)
+ for(size_t i=n_columns-1; i+1<columns.size(); ++i)
if(columns[i].basic && columns.back().values[columns[i].basic]==0.0f)
{
- for(unsigned j=0; j+1<n_columns; ++j)
+ for(size_t j=0; j+1<n_columns; ++j)
if(!columns[j].basic && columns[j].values[columns[i].basic]!=0.0f)
{
make_basic_column(j, columns[i].basic);
}
}
-unsigned Layout::LinearProgram::find_minimal_ratio(unsigned c)
+size_t Layout::LinearProgram::find_minimal_ratio(size_t c)
{
/* Pick the row with the minimum ratio between the constant column and the
pivot column. This ensures that when the pivot column is made basic, values
The use of n_rows instead of the true size of the column is intentional,
since the relocated objective row must be ignored in phase 1. */
float best = numeric_limits<float>::infinity();
- unsigned row = 0;
- for(unsigned i=1; i<n_rows; ++i)
+ size_t row = 0;
+ for(size_t i=1; i<n_rows; ++i)
if(columns[c].values[i]>0)
{
float ratio = columns.back().values[i]/columns[c].values[i];
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; i<columns.size(); ++i)
+ for(size_t i=0; i<columns.size(); ++i)
if(i!=c && (columns[i].basic==r || (!columns[i].basic && columns[i].values[r])))
{
if(columns[i].basic)
float scale = columns[i].values[r]/columns[c].values[r];
columns[i].values[r] = scale;
- for(unsigned j=0; j<columns[i].values.size(); ++j)
+ for(size_t j=0; j<columns[i].values.size(); ++j)
if(j!=r)
columns[i].values[j] -= scale*columns[c].values[j];
}
/* Pick a nonbasic column and make it basic. Requiring a positive objective
coefficient ensures that the objective function's value will decrease in the
process. */
- for(unsigned i=0; i+1<columns.size(); ++i)
+ for(size_t i=0; i+1<columns.size(); ++i)
if(!columns[i].basic && columns[i].values.front()>0)
- if(unsigned row = find_minimal_ratio(i))
+ if(size_t row = find_minimal_ratio(i))
{
make_basic_column(i, row);
return true;
}
-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[]");
Container *container = nullptr;
std::vector<Slot *> 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;
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);
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);
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();
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]);
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);
}
{
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);
{
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")
{
if(btn==4 || btn==5)
{
- unsigned change = 3;
+ size_t change = 3;
if(btn==4)
{
change = min(first_row, change);
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);
{
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
}
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
}
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);
}
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)
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();
}
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;
}
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;
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)
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;
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
{
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);
}
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);
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);
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();
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
if(widths.size()<children.size())
widths.resize(children.size(), 0);
- unsigned n = 0;
+ size_t n = 0;
for(const Child *c: children)
{
Geometry cgeom;
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);
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)
GRID
};
+ static constexpr size_t INVALID_INDEX = std::numeric_limits<size_t>::max();
+
class Loader: public DataFile::DerivedObjectLoader<List, Widget::Loader>
{
public:
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:
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<typename I>
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));
}
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<void, unsigned> signal_item_selected;
+ sigc::signal<void, std::size_t> signal_item_selected;
sigc::signal<void> signal_selection_cleared;
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;
item_factory = f;
}
private:
- Item *create_item(unsigned);
+ Item *create_item(std::size_t);
public:
void set_view_mode(ViewMode);
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 *);
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 &);
class MSPGLTK_API ListData
{
public:
- sigc::signal<void, unsigned> signal_item_added;
- sigc::signal<void, unsigned> signal_item_removed;
+ sigc::signal<void, std::size_t> signal_item_added;
+ sigc::signal<void, std::size_t> signal_item_removed;
sigc::signal<void> signal_cleared;
- sigc::signal<void, unsigned> signal_refresh_item;
+ sigc::signal<void, std::size_t> 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");
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");
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");
return items[i];
}
- int find(const T &v) const
+ std::size_t find(const T &v) const
{
- for(unsigned i=0; i<items.size(); ++i)
+ for(std::size_t i=0; i<items.size(); ++i)
if(items[i]==v)
return i;
- return -1;
+ return std::numeric_limits<std::size_t>::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())
signal_refresh_item.emit(i);
}
- void remove(unsigned i)
+ void remove(std::size_t i)
{
if(i>=items.size())
throw std::out_of_range("ListDataStore::remove");
signal_cleared.emit();
}
- unsigned size() const override { return items.size(); }
+ std::size_t size() const override { return items.size(); }
};
template<typename T>
class BasicListData: public ListDataStore<T>
{
public:
- virtual std::string get_string(unsigned i) const
+ virtual std::string get_string(std::size_t i) const
{ return lexical_cast<std::string>(this->get(i)); }
};
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)); }
};
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<Grid> ldr(*this, grd);
void expand(bool, bool);
void ghost(bool);
void gravity(int, int);
- void grid(unsigned);
+ void grid(std::size_t);
void layout();
template<typename T>
void unnamed_child();
value = max;
else
{
- unsigned steps = static_cast<unsigned>((v-min)/step+0.5);
+ double steps = round((v-min)/step);
value = min+steps*step;
}
struct Text::CoordsToGeomData
{
- unsigned row;
- unsigned col;
+ size_t row;
+ size_t col;
Geometry result;
};
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);
}
}
-void Text::insert(unsigned pos, const string &s)
+void Text::insert(size_t pos, const string &s)
{
check_alignment(pos);
text.insert(pos, 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();
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<unsigned>(lines.size(), max(free_height/line_spacing, 1U));
+ size_t n_lines = min<size_t>(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)
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())
{
return;
}
- for(unsigned i=0; i<lines.size(); ++i)
+ for(size_t i=0; i<lines.size(); ++i)
if(offs>=lines[i].start && offs<=lines[i].start+lines[i].bytes)
{
row = i;
}
}
-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();
}
}
-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;
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;
}
}
-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<end; dec.decode_char(text, i))
++count;
return count;
}
-void Text::check_alignment(unsigned offs) const
+void Text::check_alignment(size_t offs) const
{
StringCodec::Utf8::Decoder dec;
auto i = text.begin()+offs;
}
template<typename T>
-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;
int y_offset = static_cast<int>(-font.get_descent()*font_size);
unsigned fit_height;
- unsigned n_lines = get_visible_lines(part, parent, &fit_height);
- first_row = min<unsigned>(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; i<n_lines; ++i)
+ for(size_t i=0; i<n_lines; ++i)
{
const Line &line = lines[first_row+i];
}
}
-void Text::build_line(unsigned i, const Geometry &rgeom, RenderData &data) const
+void Text::build_line(size_t i, const Geometry &rgeom, RenderData &data) const
{
const Line &line = lines[i];
style->get_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)
{
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));
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;
};
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<typename T>
- 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