1 #include <msp/core/raii.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/gl/matrix.h>
4 #include <msp/gl/meshbuilder.h>
17 incompatible_data::incompatible_data(const type_info &ti):
18 logic_error("expected "+Debug::demangle(ti.name()))
23 data(new BasicListData<string>),
29 List::List(ListData &d):
43 ignore_slider_change = false;
45 observer = new DataObserver(*this);
49 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
60 void List::autosize_special(const Part &part, Geometry &ageom) const
62 if(part.get_name()=="items")
64 const Sides &margin = part.get_margin();
68 for(unsigned i=0; i<items.size(); ++i)
71 items[i]->autosize(igeom);
72 max_w = max(max_w, igeom.w);
73 if(view_size==0 || i<view_size)
77 if(!items.empty() && items.size()<view_size)
78 total_h = total_h*view_size/items.size();
80 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
81 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
83 else if(part.get_name()=="slider")
84 autosize_child(slider, part, ageom);
87 void List::set_data(ListData &d)
90 item_factory->set_data(d);
98 observer = new DataObserver(*this);
100 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
103 unsigned n_items = data->size();
104 for(unsigned i=0; i<n_items; ++i)
106 Item *item = create_item(i);
107 items.push_back(item);
113 void List::items_changed()
115 signal_autosize_changed.emit();
119 List::Item *List::create_item(unsigned index)
123 item = item_factory->create_item(index);
125 item = new BasicItem(data->get_string(index));
128 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
132 void List::set_view_size(unsigned s)
135 signal_autosize_changed.emit();
138 void List::set_view_all()
143 void List::set_selected_index(int i)
145 if(i>=static_cast<int>(data->size()))
146 throw out_of_range("List::set_selected_index");
152 items[sel_index]->set_active(false);
158 items[sel_index]->set_active(true);
159 signal_item_selected.emit(sel_index);
163 void List::rebuild_special(const Part &part)
165 if(part.get_name()=="slider")
166 reposition_child(slider, part);
167 else if(part.get_name()=="items")
169 SetFlag flag(ignore_slider_change);
172 const Sides &margin = part.get_margin();
173 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
174 unsigned y = geom.h-min(geom.h, margin.top);
175 for(unsigned i=0; i<items.size(); ++i)
178 items[i]->set_visible(false);
181 Geometry igeom = items[i]->get_geometry();
182 if(igeom.h+margin.bottom<=y)
184 items[i]->set_visible(true);
186 igeom.x = margin.left;
189 items[i]->set_geometry(igeom);
193 items[i]->set_visible(false);
200 Widget::rebuild_special(part);
203 void List::render_special(const Part &part, GL::Renderer &renderer) const
205 if(part.get_name()=="items")
207 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
208 items[i]->render(renderer);
210 else if(part.get_name()=="slider")
211 slider.render(renderer);
214 void List::button_press(int x, int y, unsigned btn)
216 Container::button_press(x, y, btn);
217 if(click_focus && btn==1)
219 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
220 if(click_focus==items[i])
222 set_selected_index(i);
228 void List::item_autosize_changed(Item *item)
231 signal_autosize_changed.emit();
235 void List::check_view_range()
241 if(const Part *items_part = style->get_part("items"))
243 const Sides &margin = items_part->get_margin();
244 h -= margin.top+margin.bottom;
247 max_scroll = items.size();
248 for(unsigned i=items.size(); i-->0; )
250 unsigned ih = items[i]->get_geometry().h;
261 slider.set_range(0, max_scroll);
262 slider.set_value(max_scroll-first);
265 void List::slider_value_changed(double value)
267 if(max_scroll>0 && !ignore_slider_change)
269 first = max_scroll-static_cast<unsigned>(value);
275 List::DataObserver::DataObserver(List &l):
278 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
279 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
280 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
281 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
284 void List::DataObserver::item_added(unsigned i)
286 if(list.sel_index>=static_cast<int>(i))
289 Item *item = list.create_item(i);
290 list.items.insert(list.items.begin()+i, item);
291 list.items_changed();
294 void List::DataObserver::item_removed(unsigned i)
296 if(list.sel_index>static_cast<int>(i))
298 else if(list.sel_index==static_cast<int>(i))
301 delete list.items[i];
302 list.items.erase(list.items.begin()+i);
303 list.items_changed();
306 void List::DataObserver::cleared()
309 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
312 list.items_changed();
315 void List::DataObserver::refresh_item(unsigned i)
317 delete list.items[i];
318 // Avoid stale pointer while create_item is executing
320 list.items[i] = list.create_item(i);
321 list.items_changed();
325 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
327 if(part.get_name()=="children")
329 const Sides &margin = part.get_margin();
330 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
333 (*i)->widget->autosize(cgeom);
334 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
335 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
340 void List::Item::set_active(bool a)
342 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
345 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
347 if(part.get_name()=="children")
349 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
350 (*i)->widget->render(renderer);
355 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
357 if(widths.size()<children.size())
358 widths.resize(children.size(), 0);
361 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
364 (*i)->widget->autosize(cgeom);
365 // TODO invent a better way to specify spacings
366 widths[n] = max(widths[n], cgeom.w+8);
370 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
375 const Part *part = style->get_part("children");
379 const Sides &margin = part->get_margin();
382 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
384 (*i)->widget->set_position(x, margin.bottom);
389 void List::MultiColumnItem::on_style_change()
394 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
395 (*i)->widget->autosize();
397 vector<unsigned> widths;
398 List *list = static_cast<List *>(parent);
399 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
401 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
402 mci->check_widths(widths);
404 vector<unsigned> self_widths(widths);
405 check_widths(self_widths);
406 bool update_all = false;
407 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
408 update_all = self_widths[i]>widths[i];
412 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
413 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
414 mci->set_widths(self_widths);
417 set_widths(self_widths);
421 List::BasicItem::BasicItem(const string &text):
427 void List::BasicItem::on_style_change()
433 if(const Part *part = style->get_part("children"))
435 const Sides &margin = part->get_margin();
436 label.set_position(margin.left, margin.bottom);
441 List::Loader::Loader(List &l):
442 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
444 add("item", &Loader::item);
445 add("view_size", &List::view_size);
448 void List::Loader::item(const string &v)
450 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);