1 #include <msp/debug/demangle.h>
2 #include <msp/gl/matrix.h>
3 #include <msp/gl/meshbuilder.h>
16 incompatible_data::incompatible_data(const type_info &ti):
17 logic_error("expected "+Debug::demangle(ti.name()))
22 data(new BasicListData<string>),
28 List::List(ListData &d):
43 observer = new DataObserver(*this);
47 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
58 void List::autosize_special(const Part &part, Geometry &ageom)
60 if(part.get_name()=="items")
62 const Sides &margin = part.get_margin();
66 for(unsigned i=0; i<items.size(); ++i)
68 const Geometry &igeom = items[i]->get_geometry();
69 max_w = max(max_w, igeom.w);
70 if(view_size==0 || i<view_size)
74 if(!items.empty() && items.size()<view_size)
75 total_h = total_h*view_size/items.size();
77 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
78 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
82 void List::set_data(ListData &d)
85 item_factory->set_data(d);
93 observer = new DataObserver(*this);
95 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
98 unsigned n_items = data->size();
99 for(unsigned i=0; i<n_items; ++i)
101 Item *item = create_item(i);
102 items.push_back(item);
108 void List::items_changed()
111 signal_autosize_changed.emit();
115 List::Item *List::create_item(unsigned index)
119 item = item_factory->create_item(index);
121 item = new BasicItem(data->get_string(index));
124 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
128 void List::set_view_size(unsigned s)
131 signal_autosize_changed.emit();
134 void List::set_view_all()
139 void List::set_selected_index(int i)
141 if(i>=static_cast<int>(data->size()))
142 throw out_of_range("List::set_selected_index");
148 items[sel_index]->set_active(false);
154 items[sel_index]->set_active(true);
155 signal_item_selected.emit(sel_index);
159 void List::render_special(const Part &part, GL::Renderer &renderer) const
161 if(part.get_name()=="items")
163 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
164 items[i]->render(renderer);
166 else if(part.get_name()=="slider")
167 slider.render(renderer);
170 void List::button_press(int x, int y, unsigned btn)
172 Container::button_press(x, y, btn);
173 if(click_focus && btn==1)
175 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
176 if(click_focus==items[i])
178 set_selected_index(i);
184 void List::on_geometry_change()
192 void List::on_style_change()
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::item_autosize_changed(Item *item)
218 signal_autosize_changed.emit();
223 void List::reposition_items()
228 if(const Part *items_part = style->get_part("items"))
230 const Sides &margin = items_part->get_margin();
231 unsigned w = geom.w-margin.left-margin.right;
232 unsigned y = geom.h-margin.top;
233 for(unsigned i=0; i<items.size(); ++i)
236 items[i]->set_visible(false);
239 Geometry igeom = items[i]->get_geometry();
240 if(igeom.h+margin.bottom<=y)
242 items[i]->set_visible(true);
244 igeom.x = margin.left;
247 items[i]->set_geometry(igeom);
251 items[i]->set_visible(false);
259 void List::check_view_range()
265 if(const Part *items_part = style->get_part("items"))
267 const Sides &margin = items_part->get_margin();
268 h -= margin.top+margin.bottom;
271 max_scroll = items.size();
272 for(unsigned i=items.size(); i-->0; )
274 unsigned ih = items[i]->get_geometry().h;
285 slider.set_range(0, max_scroll);
286 slider.set_value(max_scroll-first);
289 void List::slider_value_changed(double value)
293 first = max_scroll-static_cast<unsigned>(value);
299 List::DataObserver::DataObserver(List &l):
302 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
303 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
304 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
305 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
308 void List::DataObserver::item_added(unsigned i)
310 if(list.sel_index>=static_cast<int>(i))
313 Item *item = list.create_item(i);
314 list.items.insert(list.items.begin()+i, item);
315 list.items_changed();
318 void List::DataObserver::item_removed(unsigned i)
320 if(list.sel_index>static_cast<int>(i))
322 else if(list.sel_index==static_cast<int>(i))
325 delete list.items[i];
326 list.items.erase(list.items.begin()+i);
327 list.items_changed();
330 void List::DataObserver::cleared()
333 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
336 list.items_changed();
339 void List::DataObserver::refresh_item(unsigned i)
341 delete list.items[i];
342 list.items[i] = list.create_item(i);
343 list.items_changed();
347 void List::Item::autosize_special(const Part &part, Geometry &ageom)
349 if(part.get_name()=="children")
351 const Sides &margin = part.get_margin();
352 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
354 const Geometry &cgeom = (*i)->widget->get_geometry();
355 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
356 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
361 void List::Item::set_active(bool a)
363 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
366 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
368 if(part.get_name()=="children")
370 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
371 (*i)->widget->render(renderer);
376 List::BasicItem::BasicItem(const string &text):
382 void List::BasicItem::on_style_change()
388 if(const Part *part = style->get_part("children"))
390 const Sides &margin = part->get_margin();
391 label.set_position(margin.left, margin.bottom);
396 List::Loader::Loader(List &l):
397 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
399 add("item", &Loader::item);
400 add("view_size", &List::view_size);
403 void List::Loader::item(const string &v)
405 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);