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()))
24 List(*new BasicListData<string>)
29 List::List(ListData &d):
31 observer(new DataObserver(*this))
33 input_type = INPUT_NAVIGATION;
37 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
48 void List::autosize_special(const Part &part, Geometry &ageom) const
50 if(part.get_name()=="items")
52 const Sides &margin = part.get_margin();
56 for(unsigned i=0; i<items.size(); ++i)
59 items[i]->autosize(igeom);
60 items_w = max(items_w, igeom.w);
61 items_h = max(items_h, igeom.h);
66 unsigned r = view_rows;
67 unsigned c = view_columns;
69 r = sqrt(items.size());
71 r = (items.size()+c-1)/c;
73 c = (items.size()+r-1)/r;
78 items_h *= (view_rows==0 ? items.size() : view_rows);
80 ageom.w = max(ageom.w, items_w+margin.left+margin.right);
81 ageom.h = max(ageom.h, items_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);
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)
139 void List::set_view_size(unsigned r, unsigned c)
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=rows[first_row].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);
193 reposition_items(true);
194 unsigned old_first_row = first_row;
195 unsigned old_max_scroll = max_scroll;
197 if(first_row!=old_first_row || max_scroll!=old_max_scroll)
198 reposition_items(false);
201 Widget::rebuild_special(part);
204 void List::render_special(const Part &part, GL::Renderer &renderer) const
206 if(part.get_name()=="items")
208 for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
209 items[i]->render(renderer);
211 else if(part.get_name()=="slider")
212 slider.render(renderer);
215 bool List::key_press(unsigned key, unsigned mod)
217 if(key==Input::KEY_UP && mod==MOD_CTRL)
218 move_focus(NAV_UP, false);
219 else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
220 move_focus(NAV_DOWN, false);
227 void List::button_press(int x, int y, unsigned btn)
234 change = min(first_row, change);
235 slider.set_value(max_scroll-(first_row-change));
239 change = min(max_scroll-first_row, change);
240 slider.set_value(max_scroll-(first_row+change));
245 Container::button_press(x, y, btn);
246 if(click_focus && btn==1)
247 set_selected_item(click_focus);
251 void List::touch_press(int x, int y, unsigned finger)
261 void List::touch_release(int x, int y, unsigned finger)
265 int dx = x-drag_start_x;
266 int dy = y-drag_start_y;
269 Container::touch_press(drag_start_x, drag_start_y, finger);
271 set_selected_item(touch_focus);
272 Container::touch_motion(x, y, finger);
273 Container::touch_release(x, y, finger);
279 void List::touch_motion(int, int y, unsigned finger)
281 if(finger==0 && !items.empty() && dragging)
283 int dy = y-drag_start_y;
284 if(dy>0 && first_row<max_scroll)
286 int row_h = rows[first_row].height;
289 drag_start_y += row_h;
290 slider.set_value(max_scroll-(first_row+1));
293 else if(dy<0 && first_row>0)
295 int row_h = rows[first_row-1].height;
298 drag_start_y -= row_h;
299 slider.set_value(max_scroll-(first_row-1));
305 void List::focus_in()
307 Container::focus_in();
308 if(focus_index>=0 && items[focus_index]->is_visible())
309 set_input_focus(items[focus_index]);
312 if(sel_index>=0 && items[sel_index]->is_visible())
313 set_focus_index(sel_index);
314 else if(!items.empty())
315 set_focus_index(rows[first_row].first);
319 bool List::navigate(Navigation nav)
321 if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty())
322 move_focus(nav, true);
323 else if(nav==NAV_ACTIVATE)
324 set_selected_index(focus_index);
331 void List::on_style_change()
333 items_part = (style ? style->get_part("items") : 0);
336 void List::move_focus(Navigation nav, bool select)
338 if(nav==NAV_UP && view_mode==GRID)
340 unsigned row = item_index_to_row(focus_index);
342 set_focus_index(rows[row-1].first+focus_index-rows[row].first);
346 else if(nav==NAV_DOWN && view_mode==GRID)
348 unsigned row = item_index_to_row(focus_index);
349 if(row+1<rows.size())
350 set_focus_index(rows[row+1].first+focus_index-rows[row].first);
352 set_focus_index(items.size()-1);
354 else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
357 set_focus_index(focus_index-1);
359 else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
361 if(static_cast<unsigned>(focus_index+1)<items.size())
362 set_focus_index(focus_index+1);
366 set_selected_index(focus_index);
369 void List::set_focus_index(int i)
376 set_input_focus(items[focus_index]);
380 void List::item_autosize_changed(Item *item)
383 signal_autosize_changed.emit();
387 void List::reposition_items(bool record_rows)
398 const Sides &margin = items_part->get_margin();
399 unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
403 for(unsigned i=0; i<items.size(); ++i)
405 const Geometry &igeom = items[i]->get_geometry();
407 if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
412 if(record_rows && i>0)
414 rows.back().height = row_h;
420 if(first_row<rows.size() && i==rows[first_row].first)
421 y = geom.h-min(geom.h, margin.top);
424 items[i]->set_visible(false);
425 else if(igeom.h+margin.bottom<=y)
427 items[i]->set_visible(true);
428 unsigned iw = (view_mode==GRID ? igeom.w : view_w);
429 items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h));
433 for(unsigned j=rows.back().first; j<=i; ++j)
434 items[j]->set_visible(false);
439 row_h = max(row_h, igeom.h);
443 rows.back().height = row_h;
446 unsigned List::last_to_first_row(unsigned last) const
451 const Sides &margin = items_part->get_margin();
452 unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
454 unsigned items_h = 0;
455 for(unsigned i=last; i<rows.size(); --i)
457 items_h += rows[i].height;
459 return min(i+1, last);
465 unsigned List::item_index_to_row(unsigned index) const
467 for(unsigned i=0; i+1<rows.size(); ++i)
468 if(rows[i+1].first>index)
470 return rows.size()-1;
473 void List::check_view_range()
481 max_scroll = last_to_first_row(rows.size()-1);
483 if(first_row>max_scroll)
484 first_row = max_scroll;
486 slider.set_range(0, max_scroll);
487 slider.set_page_size(rows.size()-max_scroll);
488 slider.set_value(max_scroll-first_row);
491 void List::scroll_to_focus()
493 if(focus_index<0 || items[focus_index]->is_visible())
496 unsigned focus_row = item_index_to_row(static_cast<unsigned>(focus_index));
497 if(focus_row<first_row)
498 slider.set_value(max_scroll-focus_row);
500 slider.set_value(max_scroll-last_to_first_row(focus_row));
503 void List::slider_value_changed(double value)
505 if(max_scroll>0 && !ignore_slider_change)
507 first_row = max_scroll-static_cast<unsigned>(value);
512 void List::adjust_index(int &index, int pos, int change)
517 index = (change>0 ? index+change : -1);
521 List::DataObserver::DataObserver(List &l):
524 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
525 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
526 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
527 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
530 void List::DataObserver::item_added(unsigned i)
532 adjust_index(list.sel_index, i, 1);
533 adjust_index(list.focus_index, i, 1);
535 Item *item = list.create_item(i);
536 list.items.insert(list.items.begin()+i, item);
537 list.items_changed();
540 void List::DataObserver::item_removed(unsigned i)
542 bool had_selection = (list.sel_index>=0);
543 adjust_index(list.sel_index, i, -1);
544 adjust_index(list.focus_index, i, -1);
546 delete list.items[i];
547 list.items.erase(list.items.begin()+i);
548 list.items_changed();
550 if(had_selection && list.sel_index<0)
551 list.signal_selection_cleared.emit();
554 void List::DataObserver::cleared()
557 list.focus_index = -1;
558 for(Item *i: list.items)
561 list.items_changed();
563 list.signal_selection_cleared.emit();
566 void List::DataObserver::refresh_item(unsigned i)
568 delete list.items[i];
569 // Avoid stale pointer while create_item is executing
571 list.items[i] = list.create_item(i);
572 list.items_changed();
578 input_type = INPUT_NAVIGATION;
581 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
583 if(part.get_name()=="children")
585 const Sides &margin = part.get_margin();
586 for(const Child *c: children)
589 c->widget->autosize(cgeom);
590 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
591 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
596 void List::Item::set_active(bool a)
598 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
601 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
603 if(part.get_name()=="children")
605 for(const Child *c: children)
606 c->widget->render(renderer);
611 void List::SimpleItem::on_style_change()
613 if(!style || children.empty())
616 Widget *child = children.front()->widget;
618 if(const Part *part = style->get_part("children"))
620 const Sides &margin = part->get_margin();
621 child->set_position(margin.left, margin.bottom);
626 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
628 if(widths.size()<children.size())
629 widths.resize(children.size(), 0);
632 for(const Child *c: children)
635 c->widget->autosize(cgeom);
636 // TODO invent a better way to specify spacings
637 widths[n] = max(widths[n], cgeom.w+8);
642 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
647 const Part *part = style->get_part("children");
651 const Sides &margin = part->get_margin();
654 for(const Child *c: children)
656 c->widget->set_position(x, margin.bottom);
661 void List::MultiColumnItem::on_style_change()
666 for(const Child *c: children)
667 c->widget->autosize();
669 vector<unsigned> widths;
670 List *list = static_cast<List *>(parent);
671 for(Item *i: list->items)
673 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
674 mci->check_widths(widths);
676 vector<unsigned> self_widths(widths);
677 check_widths(self_widths);
678 bool update_all = false;
679 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
680 update_all = self_widths[i]>widths[i];
684 for(Item *i: list->items)
685 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
686 mci->set_widths(self_widths);
689 set_widths(self_widths);
693 List::BasicItem::BasicItem(const string &text):
700 List::Loader::Loader(List &l):
701 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
703 add("item", &Loader::item);
704 add("view_mode", &List::view_mode);
705 add("view_size", &List::view_rows);
706 add("view_size", &List::view_rows, &List::view_columns);
709 void List::Loader::item(const string &v)
711 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
715 void operator>>(const LexicalConverter &conv, List::ViewMode &vm)
717 const string &str = conv.get();
723 throw lexical_error(format("conversion of '%s' to List::ViewMode", str));