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));
126 if(static_cast<int>(index)==sel_index)
127 item->set_active(true);
130 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
134 void List::set_view_size(unsigned s)
137 signal_autosize_changed.emit();
140 void List::set_view_all()
145 void List::set_selected_index(int i)
147 if(i>=static_cast<int>(data->size()))
148 throw out_of_range("List::set_selected_index");
150 if(i==sel_index || (i<0 && sel_index<0))
154 items[sel_index]->set_active(false);
158 signal_selection_cleared.emit();
163 items[sel_index]->set_active(true);
164 signal_item_selected.emit(sel_index);
168 void List::rebuild_special(const Part &part)
170 if(part.get_name()=="slider")
171 reposition_child(slider, part);
172 else if(part.get_name()=="items")
174 SetFlag flag(ignore_slider_change);
177 const Sides &margin = part.get_margin();
178 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
179 unsigned y = geom.h-min(geom.h, margin.top);
180 for(unsigned i=0; i<items.size(); ++i)
183 items[i]->set_visible(false);
186 Geometry igeom = items[i]->get_geometry();
187 if(igeom.h+margin.bottom<=y)
189 items[i]->set_visible(true);
191 igeom.x = margin.left;
194 items[i]->set_geometry(igeom);
198 items[i]->set_visible(false);
205 Widget::rebuild_special(part);
208 void List::render_special(const Part &part, GL::Renderer &renderer) const
210 if(part.get_name()=="items")
212 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
213 items[i]->render(renderer);
215 else if(part.get_name()=="slider")
216 slider.render(renderer);
219 void List::button_press(int x, int y, unsigned btn)
226 change = min(first, change);
227 slider.set_value(max_scroll-(first-change));
231 change = min(max_scroll-first, change);
232 slider.set_value(max_scroll-(first+change));
237 Container::button_press(x, y, btn);
238 if(click_focus && btn==1)
240 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
241 if(click_focus==items[i])
243 set_selected_index(i);
250 void List::item_autosize_changed(Item *item)
253 signal_autosize_changed.emit();
257 unsigned List::last_to_first(unsigned last) const
262 unsigned view_h = geom.h;
263 if(const Part *items_part = style->get_part("items"))
265 const Sides &margin = items_part->get_margin();
266 view_h -= margin.top+margin.bottom;
269 unsigned items_h = 0;
270 for(unsigned i=last; i<items.size(); --i)
272 items_h += items[i]->get_geometry().h;
274 return min(i+1, last);
280 void List::check_view_range()
288 max_scroll = last_to_first(items.size()-1);
293 slider.set_range(0, max_scroll);
294 slider.set_value(max_scroll-first);
297 void List::slider_value_changed(double value)
299 if(max_scroll>0 && !ignore_slider_change)
301 first = max_scroll-static_cast<unsigned>(value);
307 List::DataObserver::DataObserver(List &l):
310 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
311 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
312 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
313 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
316 void List::DataObserver::item_added(unsigned i)
318 if(list.sel_index>=static_cast<int>(i))
321 Item *item = list.create_item(i);
322 list.items.insert(list.items.begin()+i, item);
323 list.items_changed();
326 void List::DataObserver::item_removed(unsigned i)
328 bool had_selection = (list.sel_index>=0);
329 if(list.sel_index>static_cast<int>(i))
331 else if(list.sel_index==static_cast<int>(i))
334 delete list.items[i];
335 list.items.erase(list.items.begin()+i);
336 list.items_changed();
338 if(had_selection && list.sel_index<0)
339 list.signal_selection_cleared.emit();
342 void List::DataObserver::cleared()
345 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
348 list.items_changed();
350 list.signal_selection_cleared.emit();
353 void List::DataObserver::refresh_item(unsigned i)
355 delete list.items[i];
356 // Avoid stale pointer while create_item is executing
358 list.items[i] = list.create_item(i);
359 list.items_changed();
363 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
365 if(part.get_name()=="children")
367 const Sides &margin = part.get_margin();
368 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
371 (*i)->widget->autosize(cgeom);
372 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
373 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
378 void List::Item::set_active(bool a)
380 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
383 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
385 if(part.get_name()=="children")
387 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
388 (*i)->widget->render(renderer);
393 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
395 if(widths.size()<children.size())
396 widths.resize(children.size(), 0);
399 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
402 (*i)->widget->autosize(cgeom);
403 // TODO invent a better way to specify spacings
404 widths[n] = max(widths[n], cgeom.w+8);
408 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
413 const Part *part = style->get_part("children");
417 const Sides &margin = part->get_margin();
420 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
422 (*i)->widget->set_position(x, margin.bottom);
427 void List::MultiColumnItem::on_style_change()
432 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
433 (*i)->widget->autosize();
435 vector<unsigned> widths;
436 List *list = static_cast<List *>(parent);
437 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
439 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
440 mci->check_widths(widths);
442 vector<unsigned> self_widths(widths);
443 check_widths(self_widths);
444 bool update_all = false;
445 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
446 update_all = self_widths[i]>widths[i];
450 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
451 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
452 mci->set_widths(self_widths);
455 set_widths(self_widths);
459 List::BasicItem::BasicItem(const string &text):
465 void List::BasicItem::on_style_change()
471 if(const Part *part = style->get_part("children"))
473 const Sides &margin = part->get_margin();
474 label.set_position(margin.left, margin.bottom);
479 List::Loader::Loader(List &l):
480 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
482 add("item", &Loader::item);
483 add("view_size", &List::view_size);
486 void List::Loader::item(const string &v)
488 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);