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 data(new BasicListData<string>),
30 List::List(ListData &d):
39 input_type = INPUT_NAVIGATION;
50 ignore_slider_change = false;
55 observer = new DataObserver(*this);
59 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
70 void List::autosize_special(const Part &part, Geometry &ageom) const
72 if(part.get_name()=="items")
74 const Sides &margin = part.get_margin();
78 for(unsigned i=0; i<items.size(); ++i)
81 items[i]->autosize(igeom);
82 items_w = max(items_w, igeom.w);
83 items_h = max(items_h, igeom.h);
88 unsigned r = view_rows;
89 unsigned c = view_columns;
91 r = sqrt(items.size());
93 r = (items.size()+c-1)/c;
95 c = (items.size()+r-1)/r;
100 items_h *= (view_rows==0 ? items.size() : view_rows);
102 ageom.w = max(ageom.w, items_w+margin.left+margin.right);
103 ageom.h = max(ageom.h, items_h+margin.top+margin.bottom);
105 else if(part.get_name()=="slider")
106 autosize_child(slider, part, ageom);
109 void List::set_data(ListData &d)
112 item_factory->set_data(d);
120 observer = new DataObserver(*this);
122 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
125 unsigned n_items = data->size();
126 for(unsigned i=0; i<n_items; ++i)
128 Item *item = create_item(i);
129 items.push_back(item);
135 void List::items_changed()
137 signal_autosize_changed.emit();
141 List::Item *List::create_item(unsigned index)
145 item = item_factory->create_item(index);
147 item = new BasicItem(data->get_string(index));
148 if(static_cast<int>(index)==sel_index)
149 item->set_active(true);
152 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
156 void List::set_view_size(unsigned s)
161 void List::set_view_size(unsigned r, unsigned c)
165 signal_autosize_changed.emit();
168 void List::set_view_all()
173 void List::set_selected_index(int i)
175 if(i>=static_cast<int>(data->size()))
176 throw out_of_range("List::set_selected_index");
178 if(i==sel_index || (i<0 && sel_index<0))
182 items[sel_index]->set_active(false);
188 signal_selection_cleared.emit();
194 items[sel_index]->set_active(true);
196 set_input_focus(items[focus_index]);
197 signal_item_selected.emit(sel_index);
201 void List::set_selected_item(Widget *item)
203 for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
205 return set_selected_index(i);
208 void List::rebuild_special(const Part &part)
210 if(part.get_name()=="slider")
211 reposition_child(slider, part);
212 else if(part.get_name()=="items")
214 SetFlag flag(ignore_slider_change);
215 reposition_items(true);
216 unsigned old_first_row = first_row;
217 unsigned old_max_scroll = max_scroll;
219 if(first_row!=old_first_row || max_scroll!=old_max_scroll)
220 reposition_items(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=rows[first_row].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 bool List::key_press(unsigned key, unsigned mod)
239 if(key==Input::KEY_UP && mod==MOD_CTRL)
240 move_focus(NAV_UP, false);
241 else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
242 move_focus(NAV_DOWN, false);
249 void List::button_press(int x, int y, unsigned btn)
256 change = min(first_row, change);
257 slider.set_value(max_scroll-(first_row-change));
261 change = min(max_scroll-first_row, change);
262 slider.set_value(max_scroll-(first_row+change));
267 Container::button_press(x, y, btn);
268 if(click_focus && btn==1)
269 set_selected_item(click_focus);
273 void List::touch_press(int x, int y, unsigned finger)
283 void List::touch_release(int x, int y, unsigned finger)
287 int dx = x-drag_start_x;
288 int dy = y-drag_start_y;
291 Container::touch_press(drag_start_x, drag_start_y, finger);
293 set_selected_item(touch_focus);
294 Container::touch_motion(x, y, finger);
295 Container::touch_release(x, y, finger);
301 void List::touch_motion(int, int y, unsigned finger)
303 if(finger==0 && !items.empty() && dragging)
305 int dy = y-drag_start_y;
306 if(dy>0 && first_row<max_scroll)
308 int row_h = rows[first_row].height;
311 drag_start_y += row_h;
312 slider.set_value(max_scroll-(first_row+1));
315 else if(dy<0 && first_row>0)
317 int row_h = rows[first_row-1].height;
320 drag_start_y -= row_h;
321 slider.set_value(max_scroll-(first_row-1));
327 void List::focus_in()
329 Container::focus_in();
330 if(focus_index>=0 && items[focus_index]->is_visible())
331 set_input_focus(items[focus_index]);
334 if(sel_index>=0 && items[sel_index]->is_visible())
335 set_focus_index(sel_index);
336 else if(!items.empty())
337 set_focus_index(rows[first_row].first);
341 bool List::navigate(Navigation nav)
343 if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty())
344 move_focus(nav, true);
345 else if(nav==NAV_ACTIVATE)
346 set_selected_index(focus_index);
353 void List::on_style_change()
355 items_part = (style ? style->get_part("items") : 0);
358 void List::move_focus(Navigation nav, bool select)
360 if(nav==NAV_UP && view_mode==GRID)
362 unsigned row = item_index_to_row(focus_index);
364 set_focus_index(rows[row-1].first+focus_index-rows[row].first);
368 else if(nav==NAV_DOWN && view_mode==GRID)
370 unsigned row = item_index_to_row(focus_index);
371 if(row+1<rows.size())
372 set_focus_index(rows[row+1].first+focus_index-rows[row].first);
374 set_focus_index(items.size()-1);
376 else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
379 set_focus_index(focus_index-1);
381 else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
383 if(static_cast<unsigned>(focus_index+1)<items.size())
384 set_focus_index(focus_index+1);
388 set_selected_index(focus_index);
391 void List::set_focus_index(int i)
398 set_input_focus(items[focus_index]);
402 void List::item_autosize_changed(Item *item)
405 signal_autosize_changed.emit();
409 void List::reposition_items(bool record_rows)
420 const Sides &margin = items_part->get_margin();
421 unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
425 for(unsigned i=0; i<items.size(); ++i)
427 const Geometry &igeom = items[i]->get_geometry();
429 if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
434 if(record_rows && i>0)
436 rows.back().height = row_h;
442 if(first_row<rows.size() && i==rows[first_row].first)
443 y = geom.h-min(geom.h, margin.top);
446 items[i]->set_visible(false);
447 else if(igeom.h+margin.bottom<=y)
449 items[i]->set_visible(true);
450 unsigned iw = (view_mode==GRID ? igeom.w : view_w);
451 items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h));
455 for(unsigned j=rows.back().first; j<=i; ++j)
456 items[j]->set_visible(false);
461 row_h = max(row_h, igeom.h);
465 rows.back().height = row_h;
468 unsigned List::last_to_first_row(unsigned last) const
473 const Sides &margin = items_part->get_margin();
474 unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
476 unsigned items_h = 0;
477 for(unsigned i=last; i<rows.size(); --i)
479 items_h += rows[i].height;
481 return min(i+1, last);
487 unsigned List::item_index_to_row(unsigned index) const
489 for(unsigned i=0; i+1<rows.size(); ++i)
490 if(rows[i+1].first>index)
492 return rows.size()-1;
495 void List::check_view_range()
503 max_scroll = last_to_first_row(rows.size()-1);
505 if(first_row>max_scroll)
506 first_row = max_scroll;
508 slider.set_range(0, max_scroll);
509 slider.set_page_size(rows.size()-max_scroll);
510 slider.set_value(max_scroll-first_row);
513 void List::scroll_to_focus()
515 if(focus_index<0 || items[focus_index]->is_visible())
518 unsigned focus_row = item_index_to_row(static_cast<unsigned>(focus_index));
519 if(focus_row<first_row)
520 slider.set_value(max_scroll-focus_row);
522 slider.set_value(max_scroll-last_to_first_row(focus_row));
525 void List::slider_value_changed(double value)
527 if(max_scroll>0 && !ignore_slider_change)
529 first_row = max_scroll-static_cast<unsigned>(value);
534 void List::adjust_index(int &index, int pos, int change)
539 index = (change>0 ? index+change : -1);
543 List::DataObserver::DataObserver(List &l):
546 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
547 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
548 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
549 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
552 void List::DataObserver::item_added(unsigned i)
554 adjust_index(list.sel_index, i, 1);
555 adjust_index(list.focus_index, i, 1);
557 Item *item = list.create_item(i);
558 list.items.insert(list.items.begin()+i, item);
559 list.items_changed();
562 void List::DataObserver::item_removed(unsigned i)
564 bool had_selection = (list.sel_index>=0);
565 adjust_index(list.sel_index, i, -1);
566 adjust_index(list.focus_index, i, -1);
568 delete list.items[i];
569 list.items.erase(list.items.begin()+i);
570 list.items_changed();
572 if(had_selection && list.sel_index<0)
573 list.signal_selection_cleared.emit();
576 void List::DataObserver::cleared()
579 list.focus_index = -1;
580 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
583 list.items_changed();
585 list.signal_selection_cleared.emit();
588 void List::DataObserver::refresh_item(unsigned i)
590 delete list.items[i];
591 // Avoid stale pointer while create_item is executing
593 list.items[i] = list.create_item(i);
594 list.items_changed();
600 input_type = INPUT_NAVIGATION;
603 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
605 if(part.get_name()=="children")
607 const Sides &margin = part.get_margin();
608 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
611 (*i)->widget->autosize(cgeom);
612 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
613 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
618 void List::Item::set_active(bool a)
620 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
623 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
625 if(part.get_name()=="children")
627 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
628 (*i)->widget->render(renderer);
633 void List::SimpleItem::on_style_change()
635 if(!style || children.empty())
638 Widget *child = children.front()->widget;
640 if(const Part *part = style->get_part("children"))
642 const Sides &margin = part->get_margin();
643 child->set_position(margin.left, margin.bottom);
648 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
650 if(widths.size()<children.size())
651 widths.resize(children.size(), 0);
654 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
657 (*i)->widget->autosize(cgeom);
658 // TODO invent a better way to specify spacings
659 widths[n] = max(widths[n], cgeom.w+8);
663 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
668 const Part *part = style->get_part("children");
672 const Sides &margin = part->get_margin();
675 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
677 (*i)->widget->set_position(x, margin.bottom);
682 void List::MultiColumnItem::on_style_change()
687 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
688 (*i)->widget->autosize();
690 vector<unsigned> widths;
691 List *list = static_cast<List *>(parent);
692 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
694 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
695 mci->check_widths(widths);
697 vector<unsigned> self_widths(widths);
698 check_widths(self_widths);
699 bool update_all = false;
700 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
701 update_all = self_widths[i]>widths[i];
705 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
706 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
707 mci->set_widths(self_widths);
710 set_widths(self_widths);
714 List::BasicItem::BasicItem(const string &text):
721 List::Loader::Loader(List &l):
722 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
724 add("item", &Loader::item);
725 add("view_mode", &List::view_mode);
726 add("view_size", &List::view_rows);
727 add("view_size", &List::view_rows, &List::view_columns);
730 void List::Loader::item(const string &v)
732 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
736 void operator>>(const LexicalConverter &conv, List::ViewMode &vm)
738 const string &str = conv.get();
744 throw lexical_error(format("conversion of '%s' to List::ViewMode", str));