3 This file is part of libmspgltk
4 Copyright © 2007-2011 Mikko Rasa, Mikkosoft Productions
5 Distributed under the LGPL
8 #include <msp/gl/immediate.h>
9 #include <msp/gl/matrix.h>
10 #include <msp/gl/transform.h>
32 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
40 void List::autosize_rows(unsigned n)
49 const Sides &margin = items_part->get_margin();
50 float font_size = style->get_font()->get_default_size();
53 for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
55 unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
56 max_w = max(max_w, w);
59 geom.w = max(geom.w, max_w+margin.left+margin.right);
60 geom.h = max(geom.h, n*row_height+margin.top+margin.bottom);
63 if(const Part *slider_part = style->get_part("slider"))
65 Geometry sgeom = slider_part->get_geometry();
66 if(!sgeom.w || !sgeom.h)
70 sgeom.w = slider.get_geometry().w;
72 sgeom.h = slider.get_geometry().h;
75 const Sides &margin = slider_part->get_margin();
76 geom.w = max(geom.w, sgeom.w+margin.left+margin.right);
77 geom.h = max(geom.h, sgeom.h+margin.top+margin.bottom);
85 void List::autosize_all()
87 autosize_rows(items.size());
90 void List::append(const string &v)
94 signal_autosize_changed.emit();
97 void List::insert(unsigned i, const string &v)
100 throw InvalidParameterValue("Index out of range");
102 items.insert(items.begin()+i, v);
104 signal_autosize_changed.emit();
107 void List::remove(unsigned i)
110 throw InvalidParameterValue("Index out of range");
112 items.erase(items.begin()+i);
113 if(sel_index>static_cast<int>(i))
115 else if(sel_index==static_cast<int>(i))
119 signal_autosize_changed.emit();
128 signal_autosize_changed.emit();
131 void List::set_selected_index(int i)
135 else if(i<static_cast<int>(items.size()))
138 signal_item_selected.emit(sel_index, items[sel_index]);
141 throw InvalidParameterValue("Index out of range");
144 const string &List::get_selected() const
147 throw InvalidState("No selection");
149 return items[sel_index];
152 void List::render_special(const Part &part) const
154 if(part.get_name()=="items")
156 const Sides &margin = part.get_margin();
157 Geometry pgeom = geom;
158 pgeom.h = row_height+margin.top+margin.bottom;
160 GL::PushMatrix push_mtx;
161 GL::translate(0, geom.h-pgeom.h, 0);
163 for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
166 GL::translate(0, -static_cast<int>(row_height), 0);
167 Text(*style, items[first+i]).render(part, pgeom);
170 else if(part.get_name()=="selection")
172 if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
174 const Sides &margin = part.get_margin();
176 Geometry pgeom = geom;
177 pgeom.h = row_height;
178 pgeom.w -= margin.left+margin.right;
180 Geometry rgeom = part.get_geometry();
181 rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
182 rgeom.x += margin.left;
183 part.get_alignment().apply(rgeom, pgeom);
186 GL::translate(rgeom.x, rgeom.y, 0);
187 part.get_graphic(state)->render(rgeom.w, rgeom.h);
191 else if(part.get_name()=="slider")
195 void List::button_press(int x, int y, unsigned btn)
197 Container::button_press(x, y, btn);
198 if(!click_focus && btn==1)
201 y += items_part->get_margin().top;
203 unsigned i = (geom.h-1-y)/row_height;
204 if(i<n_visible && first+i<items.size())
208 signal_item_selected.emit(sel_index, items[sel_index]);
213 void List::on_geometry_change()
220 void List::on_style_change()
230 items_part = style->get_part("items");
232 const GL::Font &font = *style->get_font();
233 row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
238 void List::reposition_slider()
243 if(const Part *slider_part = style->get_part("slider"))
245 Geometry sgeom = slider_part->get_geometry();
246 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
247 slider.set_geometry(sgeom);
251 void List::check_view_range()
256 const Sides &margin = items_part->get_margin();
257 h -= margin.top+margin.bottom;
260 n_visible = h/row_height;
262 if(first+n_visible>items.size())
264 if(items.size()>n_visible)
265 first = items.size()-n_visible;
270 if(items.size()>n_visible)
272 slider.set_range(0, items.size()-n_visible);
273 slider.set_value(items.size()-n_visible-first);
277 slider.set_range(0, 0);
282 void List::slider_value_changed(double value)
284 if(items.size()>n_visible)
285 first = items.size()-n_visible-static_cast<unsigned>(value);
289 List::Loader::Loader(List &l):
292 add("item", &Loader::item);
295 void List::Loader::item(const string &v)
297 dynamic_cast<List &>(obj).append(v);