1 #include <msp/core/raii.h>
2 #include <msp/debug/demangle.h>
3 #include <msp/gl/matrix.h>
4 #include <msp/gl/meshbuilder.h>
5 #include <msp/input/keys.h>
6 #include <msp/strings/format.h>
18 incompatible_data::incompatible_data(const type_info &ti):
19 logic_error("expected "+Debug::demangle(ti.name()))
23 List::List(unique_ptr<ListData> d):
30 List(make_unique<BasicListData<string>>())
33 List::List(ListData &d):
35 observer(make_unique<DataObserver>(*this))
37 input_type = INPUT_NAVIGATION;
41 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
44 void List::autosize_special(const Part &part, Geometry &ageom) const
46 if(part.get_name()=="items")
48 const Sides &margin = part.get_margin();
52 for(size_t i=0; i<items.size(); ++i)
55 items[i]->autosize(igeom);
56 items_w = max(items_w, igeom.w);
57 items_h = max(items_h, igeom.h);
62 unsigned r = view_rows;
63 unsigned c = view_columns;
65 r = sqrt(items.size());
67 r = (items.size()+c-1)/c;
69 c = (items.size()+r-1)/r;
74 items_h *= (view_rows==0 ? items.size() : view_rows);
76 ageom.w = max(ageom.w, items_w+margin.left+margin.right);
77 ageom.h = max(ageom.h, items_h+margin.top+margin.bottom);
79 else if(part.get_name()=="slider")
80 autosize_child(slider, part, ageom);
83 void List::set_data(ListData &d)
86 item_factory->set_data(d);
92 observer = make_unique<DataObserver>(*this);
95 size_t n_items = data->size();
96 for(size_t i=0; i<n_items; ++i)
97 items.emplace_back(create_item(i));
102 void List::items_changed()
104 signal_autosize_changed.emit();
108 unique_ptr<List::Item> List::create_item(size_t index)
110 unique_ptr<Item> item;
112 item = item_factory->create_item(index);
114 item = make_unique<BasicItem>(data->get_string(index));
116 item->set_active(true);
119 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item.get()));
123 void List::set_view_size(unsigned s)
128 void List::set_view_size(unsigned r, unsigned c)
132 signal_autosize_changed.emit();
135 void List::set_view_all()
140 void List::set_selected_index(size_t i)
142 if(i>=data->size() && i!=INVALID_INDEX)
143 throw out_of_range("List::set_selected_index");
148 if(sel_index!=INVALID_INDEX)
149 items[sel_index]->set_active(false);
155 set_input_focus(nullptr);
156 signal_selection_cleared.emit();
160 items[sel_index]->set_active(true);
162 set_input_focus(items[focus_index].get());
163 signal_item_selected.emit(sel_index);
167 void List::set_selected_item(Widget *item)
169 for(size_t i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
170 if(item==items[i].get())
171 return set_selected_index(i);
174 void List::rebuild_special(const Part &part)
176 if(part.get_name()=="slider")
177 reposition_child(slider, part);
178 else if(part.get_name()=="items")
180 SetFlag flag(ignore_slider_change);
181 reposition_items(true);
182 size_t old_first_row = first_row;
183 size_t old_max_scroll = max_scroll;
185 if(first_row!=old_first_row || max_scroll!=old_max_scroll)
186 reposition_items(false);
189 Widget::rebuild_special(part);
192 void List::render_special(const Part &part, GL::Renderer &renderer) const
194 if(part.get_name()=="items")
196 for(size_t i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
197 items[i]->render(renderer);
199 else if(part.get_name()=="slider")
200 slider.render(renderer);
203 bool List::key_press(unsigned key, unsigned mod)
205 if(key==Input::KEY_UP && mod==MOD_CTRL)
206 move_focus(NAV_UP, false);
207 else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
208 move_focus(NAV_DOWN, false);
215 void List::button_press(int x, int y, unsigned btn)
222 change = min(first_row, change);
223 slider.set_value(max_scroll-(first_row-change));
227 change = min(max_scroll-first_row, change);
228 slider.set_value(max_scroll-(first_row+change));
233 Container::button_press(x, y, btn);
234 if(click_focus && btn==1)
235 set_selected_item(click_focus);
239 void List::touch_press(int x, int y, unsigned finger)
249 void List::touch_release(int x, int y, unsigned finger)
253 int dx = x-drag_start_x;
254 int dy = y-drag_start_y;
257 Container::touch_press(drag_start_x, drag_start_y, finger);
259 set_selected_item(touch_focus);
260 Container::touch_motion(x, y, finger);
261 Container::touch_release(x, y, finger);
267 void List::touch_motion(int, int y, unsigned finger)
269 if(finger==0 && !items.empty() && dragging)
271 int dy = y-drag_start_y;
272 if(dy>0 && first_row<max_scroll)
274 int row_h = rows[first_row].height;
277 drag_start_y += row_h;
278 slider.set_value(max_scroll-(first_row+1));
281 else if(dy<0 && first_row>0)
283 int row_h = rows[first_row-1].height;
286 drag_start_y -= row_h;
287 slider.set_value(max_scroll-(first_row-1));
293 void List::focus_in()
295 Container::focus_in();
296 if(focus_index!=INVALID_INDEX && items[focus_index]->is_visible())
297 set_input_focus(items[focus_index].get());
300 if(sel_index!=INVALID_INDEX && items[sel_index]->is_visible())
301 set_focus_index(sel_index);
302 else if(!items.empty())
303 set_focus_index(rows[first_row].first);
307 bool List::navigate(Navigation nav)
309 if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty())
310 move_focus(nav, true);
311 else if(nav==NAV_ACTIVATE)
312 set_selected_index(focus_index);
319 void List::on_style_change()
321 items_part = (style ? style->find_part("items") : nullptr);
324 void List::move_focus(Navigation nav, bool select)
326 if(nav==NAV_UP && view_mode==GRID)
328 size_t row = item_index_to_row(focus_index);
330 set_focus_index(rows[row-1].first+focus_index-rows[row].first);
334 else if(nav==NAV_DOWN && view_mode==GRID)
336 size_t row = item_index_to_row(focus_index);
337 if(row+1<rows.size())
338 set_focus_index(rows[row+1].first+focus_index-rows[row].first);
340 set_focus_index(items.size()-1);
342 else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
345 set_focus_index(focus_index-1);
347 else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
349 if(focus_index+1<items.size())
350 set_focus_index(focus_index+1);
354 set_selected_index(focus_index);
357 void List::set_focus_index(size_t i)
360 if(focus_index!=INVALID_INDEX)
364 set_input_focus(items[focus_index].get());
368 void List::item_autosize_changed(Item *item)
371 signal_autosize_changed.emit();
375 void List::reposition_items(bool record_rows)
386 const Sides &margin = items_part->get_margin();
387 unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
391 for(size_t i=0; i<items.size(); ++i)
393 const Geometry &igeom = items[i]->get_geometry();
395 if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
400 if(record_rows && i>0)
402 rows.back().height = row_h;
408 if(first_row<rows.size() && i==rows[first_row].first)
409 y = geom.h-min(geom.h, margin.top);
412 items[i]->set_visible(false);
413 else if(igeom.h+margin.bottom<=y)
415 items[i]->set_visible(true);
416 unsigned iw = (view_mode==GRID ? igeom.w : view_w);
417 items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h));
421 for(size_t j=rows.back().first; j<=i; ++j)
422 items[j]->set_visible(false);
427 row_h = max(row_h, igeom.h);
431 rows.back().height = row_h;
434 size_t List::last_to_first_row(size_t last) const
439 const Sides &margin = items_part->get_margin();
440 unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
442 unsigned items_h = 0;
443 for(size_t i=last; i<rows.size(); --i)
445 items_h += rows[i].height;
447 return min(i+1, last);
453 size_t List::item_index_to_row(size_t index) const
455 for(size_t i=0; i+1<rows.size(); ++i)
456 if(rows[i+1].first>index)
458 return rows.size()-1;
461 void List::check_view_range()
469 max_scroll = last_to_first_row(rows.size()-1);
471 if(first_row>max_scroll)
472 first_row = max_scroll;
474 slider.set_range(0, max_scroll);
475 slider.set_page_size(rows.size()-max_scroll);
476 slider.set_value(max_scroll-first_row);
479 void List::scroll_to_focus()
481 if(focus_index==INVALID_INDEX || items[focus_index]->is_visible())
484 size_t focus_row = item_index_to_row(focus_index);
485 if(focus_row<first_row)
486 slider.set_value(max_scroll-focus_row);
488 slider.set_value(max_scroll-last_to_first_row(focus_row));
491 void List::slider_value_changed(double value)
493 if(max_scroll>0 && !ignore_slider_change)
495 first_row = max_scroll-static_cast<size_t>(value);
500 void List::adjust_index(size_t &index, size_t pos, ptrdiff_t change)
502 if(index==INVALID_INDEX)
507 index = (change>0 ? index+change : INVALID_INDEX);
511 List::DataObserver::DataObserver(List &l):
514 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
515 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
516 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
517 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
520 void List::DataObserver::item_added(size_t i)
522 adjust_index(list.sel_index, i, 1);
523 adjust_index(list.focus_index, i, 1);
525 list.items.insert(list.items.begin()+i, list.create_item(i));
526 list.items_changed();
529 void List::DataObserver::item_removed(size_t i)
531 bool had_selection = (list.sel_index!=INVALID_INDEX);
532 adjust_index(list.sel_index, i, -1);
533 adjust_index(list.focus_index, i, -1);
535 list.items.erase(list.items.begin()+i);
536 list.items_changed();
538 if(had_selection && list.sel_index==INVALID_INDEX)
539 list.signal_selection_cleared.emit();
542 void List::DataObserver::cleared()
544 list.sel_index = INVALID_INDEX;
545 list.focus_index = INVALID_INDEX;
547 list.items_changed();
549 list.signal_selection_cleared.emit();
552 void List::DataObserver::refresh_item(size_t i)
554 // Destroy the old item before calling create_item
555 list.items[i].reset();
556 list.items[i] = list.create_item(i);
557 list.items_changed();
563 input_type = INPUT_NAVIGATION;
566 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
568 if(part.get_name()=="children")
570 const Sides &margin = part.get_margin();
571 for(const unique_ptr<Child> &c: children)
574 c->widget->autosize(cgeom);
575 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
576 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
581 void List::Item::set_active(bool a)
583 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
586 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
588 if(part.get_name()=="children")
590 for(const unique_ptr<Child> &c: children)
591 c->widget->render(renderer);
596 void List::SimpleItem::on_style_change()
598 if(!style || children.empty())
601 Widget *child = children.front()->widget;
603 if(const Part *part = style->find_part("children"))
605 const Sides &margin = part->get_margin();
606 child->set_position(margin.left, margin.bottom);
611 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
613 if(widths.size()<children.size())
614 widths.resize(children.size(), 0);
617 for(const unique_ptr<Child> &c: children)
620 c->widget->autosize(cgeom);
621 // TODO invent a better way to specify spacings
622 widths[n] = max(widths[n], cgeom.w+8);
627 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
632 const Part *part = style->find_part("children");
636 const Sides &margin = part->get_margin();
639 for(const unique_ptr<Child> &c: children)
641 c->widget->set_position(x, margin.bottom);
646 void List::MultiColumnItem::on_style_change()
651 for(const unique_ptr<Child> &c: children)
652 c->widget->autosize();
654 vector<unsigned> widths;
655 List *list = static_cast<List *>(parent);
656 for(const unique_ptr<Item> &i: list->items)
658 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i.get()))
659 mci->check_widths(widths);
661 vector<unsigned> self_widths(widths);
662 check_widths(self_widths);
663 bool update_all = false;
664 for(size_t i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
665 update_all = self_widths[i]>widths[i];
669 for(const unique_ptr<Item> &i: list->items)
670 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i.get()))
671 mci->set_widths(self_widths);
674 set_widths(self_widths);
678 List::BasicItem::BasicItem(const string &text):
685 List::Loader::Loader(List &l):
686 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
688 add("item", &Loader::item);
689 add("view_mode", &List::view_mode);
690 add("view_size", &List::view_rows);
691 add("view_size", &List::view_rows, &List::view_columns);
694 void List::Loader::item(const string &v)
696 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
700 void operator>>(const LexicalConverter &conv, List::ViewMode &vm)
702 const string &str = conv.get();
708 throw lexical_error(format("conversion of '%s' to List::ViewMode", str));