1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
24 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
32 void List::autosize_rows(unsigned n)
41 const Sides &margin = items_part->get_margin();
42 const GL::Font &font = style->get_font();
43 float font_size = style->get_font_size();
46 for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
48 unsigned w = static_cast<unsigned>(font.get_string_width(*i)*font_size);
49 max_w = max(max_w, w);
52 geom.w = max(geom.w, max_w+margin.left+margin.right);
53 geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
56 if(const Part *slider_part = style->get_part("slider"))
58 Geometry sgeom = slider_part->get_geometry();
59 if(!sgeom.w || !sgeom.h)
63 sgeom.w = slider.get_geometry().w;
65 sgeom.h = slider.get_geometry().h;
68 const Sides &margin = slider_part->get_margin();
69 geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
70 geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
79 void List::autosize_all()
81 autosize_rows(items.size());
84 void List::append(const string &v)
90 void List::insert(unsigned i, const string &v)
93 throw out_of_range("List::insert");
95 items.insert(items.begin()+i, v);
99 void List::remove(unsigned i)
102 throw out_of_range("List::remove");
104 items.erase(items.begin()+i);
105 if(sel_index>static_cast<int>(i))
107 else if(sel_index==static_cast<int>(i))
120 void List::items_changed()
123 signal_autosize_changed.emit();
127 void List::set_selected_index(int i)
131 else if(i<static_cast<int>(items.size()))
134 signal_item_selected.emit(sel_index, items[sel_index]);
137 throw out_of_range("List::set_selected_index");
140 const string &List::get_selected() const
143 throw logic_error("sel_index<0");
145 return items[sel_index];
148 void List::rebuild_special(const Part &part, CachedPart &cache)
150 if(part.get_name()=="items")
152 const Sides &margin = part.get_margin();
153 Geometry pgeom = geom;
154 pgeom.h = row_height+margin.top+margin.bottom;
156 const GL::Font &font = style->get_font();
157 cache.texture = &font.get_texture();
160 GL::MeshBuilder bld(*cache.mesh);
161 bld.color(style->get_font_color());
162 bld.matrix() *= GL::Matrix::translation(pgeom.x, pgeom.y+geom.h-pgeom.h, 0);
164 for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
167 bld.matrix() *= GL::Matrix::translation(0, -static_cast<int>(row_height), 0);
169 GL::MatrixStack::Push _pushm(bld.matrix());
170 bld.matrix() *= GL::Matrix::scaling(style->get_font_size());
172 style->get_font().build_string(items[first+i], bld);
175 else if(part.get_name()=="selection")
177 if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
179 const Sides &margin = part.get_margin();
181 Geometry pgeom = geom;
182 pgeom.h = row_height;
183 pgeom.w -= margin.left+margin.right;
185 Geometry rgeom = part.get_geometry();
186 rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
187 rgeom.x += margin.left;
188 part.get_alignment().apply(rgeom, pgeom);
190 cache.texture = part.get_graphic(state)->get_texture();
193 GL::MeshBuilder bld(*cache.mesh);
194 bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0);
195 part.get_graphic(state)->build(rgeom.w, rgeom.h, bld);
202 void List::render_special(const Part &part, GL::Renderer &renderer) const
204 if(part.get_name()=="slider")
205 slider.render(renderer);
208 void List::button_press(int x, int y, unsigned btn)
210 Container::button_press(x, y, btn);
211 if(!click_focus && btn==1)
214 y += items_part->get_margin().top;
216 unsigned i = (geom.h-1-y)/row_height;
217 if(i<n_visible && first+i<items.size())
221 signal_item_selected.emit(sel_index, items[sel_index]);
227 void List::on_geometry_change()
234 void List::on_style_change()
244 items_part = style->get_part("items");
246 const GL::Font &font = style->get_font();
247 row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*style->get_font_size());
252 void List::reposition_slider()
257 if(const Part *slider_part = style->get_part("slider"))
259 Geometry sgeom = slider_part->get_geometry();
260 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
261 slider.set_geometry(sgeom);
265 void List::check_view_range()
270 const Sides &margin = items_part->get_margin();
271 h -= margin.top+margin.bottom;
274 n_visible = h/row_height;
276 if(first+n_visible>items.size())
278 if(items.size()>n_visible)
279 first = items.size()-n_visible;
284 if(items.size()>n_visible)
286 slider.set_range(0, items.size()-n_visible);
287 slider.set_value(items.size()-n_visible-first);
291 slider.set_range(0, 0);
296 void List::slider_value_changed(double value)
298 if(items.size()>n_visible)
300 first = items.size()-n_visible-static_cast<unsigned>(value);
306 List::Loader::Loader(List &l):
309 add("item", &Loader::item);
312 void List::Loader::item(const string &v)
314 dynamic_cast<List &>(obj).append(v);