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):
38 input_type = INPUT_NAVIGATION;
46 ignore_slider_change = false;
51 observer = new DataObserver(*this);
55 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
66 void List::autosize_special(const Part &part, Geometry &ageom) const
68 if(part.get_name()=="items")
70 const Sides &margin = part.get_margin();
74 for(unsigned i=0; i<items.size(); ++i)
77 items[i]->autosize(igeom);
78 max_w = max(max_w, igeom.w);
79 if(view_size==0 || i<view_size)
83 if(!items.empty() && items.size()<view_size)
84 total_h = total_h*view_size/items.size();
86 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
87 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
89 else if(part.get_name()=="slider")
90 autosize_child(slider, part, ageom);
93 void List::set_data(ListData &d)
96 item_factory->set_data(d);
104 observer = new DataObserver(*this);
106 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
109 unsigned n_items = data->size();
110 for(unsigned i=0; i<n_items; ++i)
112 Item *item = create_item(i);
113 items.push_back(item);
119 void List::items_changed()
121 signal_autosize_changed.emit();
125 List::Item *List::create_item(unsigned index)
129 item = item_factory->create_item(index);
131 item = new BasicItem(data->get_string(index));
132 if(static_cast<int>(index)==sel_index)
133 item->set_active(true);
136 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
140 void List::set_view_size(unsigned s)
143 signal_autosize_changed.emit();
146 void List::set_view_all()
151 void List::set_selected_index(int i)
153 if(i>=static_cast<int>(data->size()))
154 throw out_of_range("List::set_selected_index");
156 if(i==sel_index || (i<0 && sel_index<0))
160 items[sel_index]->set_active(false);
166 signal_selection_cleared.emit();
172 items[sel_index]->set_active(true);
174 set_input_focus(items[focus_index]);
175 signal_item_selected.emit(sel_index);
179 void List::set_selected_item(Widget *item)
181 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
183 return set_selected_index(i);
186 void List::rebuild_special(const Part &part)
188 if(part.get_name()=="slider")
189 reposition_child(slider, part);
190 else if(part.get_name()=="items")
192 SetFlag flag(ignore_slider_change);
195 const Sides &margin = part.get_margin();
196 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
197 unsigned y = geom.h-min(geom.h, margin.top);
198 for(unsigned i=0; i<items.size(); ++i)
201 items[i]->set_visible(false);
204 Geometry igeom = items[i]->get_geometry();
205 if(igeom.h+margin.bottom<=y)
207 items[i]->set_visible(true);
209 igeom.x = margin.left;
212 items[i]->set_geometry(igeom);
216 items[i]->set_visible(false);
223 Widget::rebuild_special(part);
226 void List::render_special(const Part &part, GL::Renderer &renderer) const
228 if(part.get_name()=="items")
230 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
231 items[i]->render(renderer);
233 else if(part.get_name()=="slider")
234 slider.render(renderer);
237 void List::button_press(int x, int y, unsigned btn)
244 change = min(first, change);
245 slider.set_value(max_scroll-(first-change));
249 change = min(max_scroll-first, change);
250 slider.set_value(max_scroll-(first+change));
255 Container::button_press(x, y, btn);
256 if(click_focus && btn==1)
257 set_selected_item(click_focus);
261 void List::touch_press(int x, int y, unsigned finger)
271 void List::touch_release(int x, int y, unsigned finger)
275 int dx = x-drag_start_x;
276 int dy = y-drag_start_y;
279 Container::touch_press(drag_start_x, drag_start_y, finger);
281 set_selected_item(touch_focus);
282 Container::touch_motion(x, y, finger);
283 Container::touch_release(x, y, finger);
289 void List::touch_motion(int, int y, unsigned finger)
291 if(finger==0 && !items.empty() && dragging)
293 int dy = y-drag_start_y;
294 if(dy>0 && first<max_scroll)
296 int item_h = items[first]->get_geometry().h;
299 drag_start_y += item_h;
300 slider.set_value(max_scroll-(first+1));
303 else if(dy<0 && first>0)
305 int item_h = items[first-1]->get_geometry().h;
308 drag_start_y -= item_h;
309 slider.set_value(max_scroll-(first-1));
315 void List::focus_in()
317 Container::focus_in();
318 if(focus_index>=0 && items[focus_index]->is_visible())
319 set_input_focus(items[focus_index]);
322 if(sel_index>=0 && items[sel_index]->is_visible())
323 set_focus_index(sel_index);
324 else if(!items.empty())
325 set_focus_index(first);
329 bool List::navigate(Navigation nav)
331 if(nav==NAV_UP && !items.empty())
334 set_focus_index(focus_index-1);
336 else if(nav==NAV_DOWN && !items.empty())
338 if(static_cast<unsigned>(focus_index+1)<items.size())
339 set_focus_index(focus_index+1);
341 else if(nav==NAV_ACTIVATE)
342 set_selected_index(focus_index);
349 void List::set_focus_index(int i)
356 set_input_focus(items[focus_index]);
360 void List::item_autosize_changed(Item *item)
363 signal_autosize_changed.emit();
367 unsigned List::last_to_first(unsigned last) const
372 unsigned view_h = geom.h;
373 if(const Part *items_part = style->get_part("items"))
375 const Sides &margin = items_part->get_margin();
376 view_h -= margin.top+margin.bottom;
379 unsigned items_h = 0;
380 for(unsigned i=last; i<items.size(); --i)
382 items_h += items[i]->get_geometry().h;
384 return min(i+1, last);
390 void List::check_view_range()
398 max_scroll = last_to_first(items.size()-1);
403 slider.set_range(0, max_scroll);
404 slider.set_value(max_scroll-first);
407 void List::scroll_to_focus()
409 if(focus_index<0 || items[focus_index]->is_visible())
412 if(static_cast<unsigned>(focus_index)<first)
413 slider.set_value(max_scroll-focus_index);
415 slider.set_value(max_scroll-last_to_first(focus_index));
418 void List::slider_value_changed(double value)
420 if(max_scroll>0 && !ignore_slider_change)
422 first = max_scroll-static_cast<unsigned>(value);
427 void List::adjust_index(int &index, int pos, int change)
432 index = (change>0 ? index+change : -1);
436 List::DataObserver::DataObserver(List &l):
439 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
440 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
441 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
442 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
445 void List::DataObserver::item_added(unsigned i)
447 adjust_index(list.sel_index, i, 1);
448 adjust_index(list.focus_index, i, 1);
450 Item *item = list.create_item(i);
451 list.items.insert(list.items.begin()+i, item);
452 list.items_changed();
455 void List::DataObserver::item_removed(unsigned i)
457 bool had_selection = (list.sel_index>=0);
458 adjust_index(list.sel_index, i, -1);
459 adjust_index(list.focus_index, i, -1);
461 delete list.items[i];
462 list.items.erase(list.items.begin()+i);
463 list.items_changed();
465 if(had_selection && list.sel_index<0)
466 list.signal_selection_cleared.emit();
469 void List::DataObserver::cleared()
472 list.focus_index = -1;
473 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
476 list.items_changed();
478 list.signal_selection_cleared.emit();
481 void List::DataObserver::refresh_item(unsigned i)
483 delete list.items[i];
484 // Avoid stale pointer while create_item is executing
486 list.items[i] = list.create_item(i);
487 list.items_changed();
491 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
493 if(part.get_name()=="children")
495 const Sides &margin = part.get_margin();
496 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
499 (*i)->widget->autosize(cgeom);
500 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
501 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
506 void List::Item::set_active(bool a)
508 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
511 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
513 if(part.get_name()=="children")
515 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
516 (*i)->widget->render(renderer);
521 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
523 if(widths.size()<children.size())
524 widths.resize(children.size(), 0);
527 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
530 (*i)->widget->autosize(cgeom);
531 // TODO invent a better way to specify spacings
532 widths[n] = max(widths[n], cgeom.w+8);
536 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
541 const Part *part = style->get_part("children");
545 const Sides &margin = part->get_margin();
548 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
550 (*i)->widget->set_position(x, margin.bottom);
555 void List::MultiColumnItem::on_style_change()
560 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
561 (*i)->widget->autosize();
563 vector<unsigned> widths;
564 List *list = static_cast<List *>(parent);
565 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
567 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
568 mci->check_widths(widths);
570 vector<unsigned> self_widths(widths);
571 check_widths(self_widths);
572 bool update_all = false;
573 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
574 update_all = self_widths[i]>widths[i];
578 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
579 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
580 mci->set_widths(self_widths);
583 set_widths(self_widths);
587 List::BasicItem::BasicItem(const string &text):
593 void List::BasicItem::on_style_change()
599 if(const Part *part = style->get_part("children"))
601 const Sides &margin = part->get_margin();
602 label.set_position(margin.left, margin.bottom);
607 List::Loader::Loader(List &l):
608 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
610 add("item", &Loader::item);
611 add("view_size", &List::view_size);
614 void List::Loader::item(const string &v)
616 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);