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) const
60 if(part.get_name()=="items")
62 const Sides &margin = part.get_margin();
66 for(unsigned i=0; i<items.size(); ++i)
69 items[i]->autosize(igeom);
70 max_w = max(max_w, igeom.w);
71 if(view_size==0 || i<view_size)
75 if(!items.empty() && items.size()<view_size)
76 total_h = total_h*view_size/items.size();
78 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
79 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
81 else if(part.get_name()=="slider")
82 autosize_child(slider, part, ageom);
85 void List::set_data(ListData &d)
88 item_factory->set_data(d);
96 observer = new DataObserver(*this);
98 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
101 unsigned n_items = data->size();
102 for(unsigned i=0; i<n_items; ++i)
104 Item *item = create_item(i);
105 items.push_back(item);
111 void List::items_changed()
113 signal_autosize_changed.emit();
117 List::Item *List::create_item(unsigned index)
121 item = item_factory->create_item(index);
123 item = new BasicItem(data->get_string(index));
126 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
130 void List::set_view_size(unsigned s)
133 signal_autosize_changed.emit();
136 void List::set_view_all()
141 void List::set_selected_index(int i)
143 if(i>=static_cast<int>(data->size()))
144 throw out_of_range("List::set_selected_index");
150 items[sel_index]->set_active(false);
156 items[sel_index]->set_active(true);
157 signal_item_selected.emit(sel_index);
161 void List::rebuild_special(const Part &part)
163 if(part.get_name()=="slider")
164 reposition_child(slider, part);
165 else if(part.get_name()=="items")
167 const Sides &margin = part.get_margin();
168 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
169 unsigned y = geom.h-min(geom.h, margin.top);
170 for(unsigned i=0; i<items.size(); ++i)
173 items[i]->set_visible(false);
176 Geometry igeom = items[i]->get_geometry();
177 if(igeom.h+margin.bottom<=y)
179 items[i]->set_visible(true);
181 igeom.x = margin.left;
184 items[i]->set_geometry(igeom);
188 items[i]->set_visible(false);
197 Widget::rebuild_special(part);
200 void List::render_special(const Part &part, GL::Renderer &renderer) const
202 if(part.get_name()=="items")
204 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
205 items[i]->render(renderer);
207 else if(part.get_name()=="slider")
208 slider.render(renderer);
211 void List::button_press(int x, int y, unsigned btn)
213 Container::button_press(x, y, btn);
214 if(click_focus && btn==1)
216 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
217 if(click_focus==items[i])
219 set_selected_index(i);
225 void List::item_autosize_changed(Item *item)
228 signal_autosize_changed.emit();
232 void List::check_view_range()
238 if(const Part *items_part = style->get_part("items"))
240 const Sides &margin = items_part->get_margin();
241 h -= margin.top+margin.bottom;
244 max_scroll = items.size();
245 for(unsigned i=items.size(); i-->0; )
247 unsigned ih = items[i]->get_geometry().h;
258 slider.set_range(0, max_scroll);
259 slider.set_value(max_scroll-first);
262 void List::slider_value_changed(double value)
266 first = max_scroll-static_cast<unsigned>(value);
272 List::DataObserver::DataObserver(List &l):
275 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
276 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
277 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
278 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
281 void List::DataObserver::item_added(unsigned i)
283 if(list.sel_index>=static_cast<int>(i))
286 Item *item = list.create_item(i);
287 list.items.insert(list.items.begin()+i, item);
288 list.items_changed();
291 void List::DataObserver::item_removed(unsigned i)
293 if(list.sel_index>static_cast<int>(i))
295 else if(list.sel_index==static_cast<int>(i))
298 delete list.items[i];
299 list.items.erase(list.items.begin()+i);
300 list.items_changed();
303 void List::DataObserver::cleared()
306 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
309 list.items_changed();
312 void List::DataObserver::refresh_item(unsigned i)
314 delete list.items[i];
315 // Avoid stale pointer while create_item is executing
317 list.items[i] = list.create_item(i);
318 list.items_changed();
322 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
324 if(part.get_name()=="children")
326 const Sides &margin = part.get_margin();
327 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
330 (*i)->widget->autosize(cgeom);
331 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
332 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
337 void List::Item::set_active(bool a)
339 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
342 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
344 if(part.get_name()=="children")
346 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
347 (*i)->widget->render(renderer);
352 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
354 if(widths.size()<children.size())
355 widths.resize(children.size(), 0);
358 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
361 (*i)->widget->autosize(cgeom);
362 // TODO invent a better way to specify spacings
363 widths[n] = max(widths[n], cgeom.w+8);
367 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
372 const Part *part = style->get_part("children");
376 const Sides &margin = part->get_margin();
379 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
381 (*i)->widget->set_position(x, margin.bottom);
386 void List::MultiColumnItem::on_style_change()
391 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
392 (*i)->widget->autosize();
394 vector<unsigned> widths;
395 List *list = static_cast<List *>(parent);
396 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
398 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
399 mci->check_widths(widths);
401 vector<unsigned> self_widths(widths);
402 check_widths(self_widths);
403 bool update_all = false;
404 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
405 update_all = self_widths[i]>widths[i];
409 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
410 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
411 mci->set_widths(self_widths);
414 set_widths(self_widths);
418 List::BasicItem::BasicItem(const string &text):
424 void List::BasicItem::on_style_change()
430 if(const Part *part = style->get_part("children"))
432 const Sides &margin = part->get_margin();
433 label.set_position(margin.left, margin.bottom);
438 List::Loader::Loader(List &l):
439 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
441 add("item", &Loader::item);
442 add("view_size", &List::view_size);
445 void List::Loader::item(const string &v)
447 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);