1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
16 data(new BasicListData<string>),
22 List::List(ListData &d):
35 observer = new DataObserver(*this);
39 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
54 void List::autosize_rows(unsigned n)
61 if(const Part *items_part = style->get_part("items"))
63 const Sides &margin = items_part->get_margin();
67 for(unsigned i=0; (i<n && i<items.size()); ++i)
70 const Geometry &igeom = items[i]->get_geometry();
71 max_w = max(max_w, igeom.w);
76 total_h = total_h*n/items.size();
78 geom.w = max(geom.w, max_w+margin.left+margin.right);
79 geom.h = max(geom.h, total_h+margin.top+margin.bottom);
86 void List::autosize_all()
88 autosize_rows(data->size());
91 void List::set_data(ListData &d)
99 observer = new DataObserver(*this);
104 void List::items_changed()
107 signal_autosize_changed.emit();
111 List::Item *List::create_item(unsigned index)
113 return new BasicItem(data->get_string(index));
116 void List::set_selected_index(int i)
118 if(i>static_cast<int>(data->size()))
119 throw out_of_range("List::set_selected_index");
122 items[sel_index]->set_active(false);
128 items[sel_index]->set_active(true);
129 signal_item_selected.emit(sel_index);
133 void List::render_special(const Part &part, GL::Renderer &renderer) const
135 if(part.get_name()=="items")
137 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
138 items[i]->render(renderer);
140 else if(part.get_name()=="slider")
141 slider.render(renderer);
144 void List::button_press(int x, int y, unsigned btn)
146 Container::button_press(x, y, btn);
147 if(click_focus && btn==1)
149 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
150 if(click_focus==items[i])
152 set_selected_index(i);
158 void List::on_geometry_change()
166 void List::on_style_change()
177 void List::reposition_slider()
182 if(const Part *slider_part = style->get_part("slider"))
184 Geometry sgeom = slider_part->get_geometry();
185 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
186 slider.set_geometry(sgeom);
190 void List::reposition_items()
195 if(const Part *items_part = style->get_part("items"))
197 const Sides &margin = items_part->get_margin();
198 unsigned w = geom.w-margin.left-margin.right;
199 unsigned y = geom.h-margin.top;
200 for(unsigned i=0; i<items.size(); ++i)
203 items[i]->set_visible(false);
206 Geometry igeom = items[i]->get_geometry();
207 if(igeom.h+margin.bottom<=y)
209 items[i]->set_visible(true);
211 igeom.x = margin.left;
214 items[i]->set_geometry(igeom);
218 items[i]->set_visible(false);
226 void List::check_view_range()
232 if(const Part *items_part = style->get_part("items"))
234 const Sides &margin = items_part->get_margin();
235 h -= margin.top+margin.bottom;
238 max_scroll = items.size();
239 for(unsigned i=items.size(); i-->0; )
241 unsigned ih = items[i]->get_geometry().h;
252 slider.set_range(0, max_scroll);
253 slider.set_value(max_scroll-first);
256 void List::slider_value_changed(double value)
260 first = max_scroll-static_cast<unsigned>(value);
266 List::DataObserver::DataObserver(List &l):
269 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
270 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
271 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
272 list.data->signal_refresh_strings.connect(sigc::mem_fun(this, &DataObserver::refresh_strings));
275 void List::DataObserver::item_added(unsigned i)
277 if(list.sel_index>=static_cast<int>(i))
280 Item *item = list.create_item(i);
282 list.items.insert(list.items.begin()+i, item);
283 list.items_changed();
286 void List::DataObserver::item_removed(unsigned i)
288 if(list.sel_index>static_cast<int>(i))
290 else if(list.sel_index==static_cast<int>(i))
293 delete list.items[i];
294 list.items.erase(list.items.begin()+i);
295 list.items_changed();
298 void List::DataObserver::cleared()
301 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
304 list.items_changed();
307 void List::DataObserver::refresh_strings()
309 list.items_changed();
313 void List::Item::autosize()
317 if(const Part *part = style->get_part("children"))
319 const Sides &margin = part->get_margin();
320 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
322 const Geometry &cgeom = (*i)->widget->get_geometry();
323 geom.w = max(geom.w, cgeom.x+cgeom.w+margin.right);
324 geom.h = max(geom.h, cgeom.y+cgeom.h+margin.top);
329 void List::Item::set_active(bool a)
331 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
334 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
336 if(part.get_name()=="children")
338 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
339 (*i)->widget->render(renderer);
344 List::BasicItem::BasicItem(const string &text):
350 void List::BasicItem::on_style_change()
356 if(const Part *part = style->get_part("children"))
358 const Sides &margin = part->get_margin();
359 label.set_position(margin.left, margin.bottom);
364 List::Loader::Loader(List &l):
365 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
367 add("item", &Loader::item);
370 void List::Loader::item(const string &v)
372 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);