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 float font_size = style->get_font()->get_default_size();
43 for(vector<string>::iterator i=items.begin(); i!=items.end(); ++i)
45 unsigned w = static_cast<unsigned>(style->get_font()->get_string_width(*i)*font_size);
46 geom.w = max(geom.w, w);
49 geom.h = items.size()*row_height;
53 const Sides &margin = items_part->get_margin();
54 geom.w += margin.left+margin.right;
55 geom.h += margin.top+margin.bottom;
59 void List::append(const string &v)
65 void List::insert(unsigned i, const string &v)
68 throw InvalidParameterValue("Index out of range");
70 items.insert(items.begin()+i, v);
74 void List::remove(unsigned i)
77 throw InvalidParameterValue("Index out of range");
79 items.erase(items.begin()+i);
80 if(sel_index>static_cast<int>(i))
82 else if(sel_index==static_cast<int>(i))
96 void List::set_selected_index(int i)
100 else if(i<static_cast<int>(items.size()))
103 signal_item_selected.emit(sel_index, items[sel_index]);
106 throw InvalidParameterValue("Index out of range");
109 const string &List::get_selected() const
112 throw InvalidState("No selection");
114 return items[sel_index];
117 void List::render_special(const Part &part) const
119 if(part.get_name()=="items")
121 const Sides &margin = part.get_margin();
122 Geometry pgeom = geom;
123 pgeom.h = row_height+margin.top+margin.bottom;
125 GL::PushMatrix push_mtx;
126 GL::translate(0, geom.h-pgeom.h, 0);
128 for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
131 GL::translate(0, -static_cast<int>(row_height), 0);
132 Text(*style, items[first+i]).render(part, pgeom);
135 else if(part.get_name()=="selection")
137 if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
139 const Sides &margin = part.get_margin();
141 Geometry pgeom = geom;
142 pgeom.h = row_height;
143 pgeom.w -= margin.left+margin.right;
145 Geometry rgeom = part.get_geometry();
146 rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
147 rgeom.x += margin.left;
148 part.get_alignment().apply(rgeom, pgeom);
151 GL::translate(rgeom.x, rgeom.y, 0);
152 part.get_graphic(state)->render(rgeom.w, rgeom.h);
156 else if(part.get_name()=="slider")
160 void List::button_press(int x, int y, unsigned btn)
162 Container::button_press(x, y, btn);
163 if(!click_focus && btn==1)
166 y += items_part->get_margin().top;
168 unsigned i = (geom.h-1-y)/row_height;
169 if(i<n_visible && first+i<items.size())
173 signal_item_selected.emit(sel_index, items[sel_index]);
178 void List::on_geometry_change()
185 void List::on_style_change()
195 items_part = style->get_part("items");
197 const GL::Font &font = *style->get_font();
198 row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
203 void List::reposition_slider()
208 if(const Part *slider_part = style->get_part("slider"))
210 Geometry sgeom = slider_part->get_geometry();
211 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
212 slider.set_geometry(sgeom);
216 void List::check_view_range()
221 const Sides &margin = items_part->get_margin();
222 h -= margin.top+margin.bottom;
225 n_visible = h/row_height;
227 if(first+n_visible>items.size())
229 if(items.size()>n_visible)
230 first = items.size()-n_visible;
235 if(items.size()>n_visible)
237 slider.set_range(0, items.size()-n_visible);
238 slider.set_value(items.size()-n_visible-first);
242 slider.set_range(0, 0);
247 void List::slider_value_changed(double value)
249 if(items.size()>n_visible)
250 first = items.size()-n_visible-static_cast<unsigned>(value);
254 List::Loader::Loader(List &l):
257 add("item", &Loader::item);
260 void List::Loader::item(const string &v)
262 dynamic_cast<List &>(wdg).append(v);