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>
19 incompatible_data::incompatible_data(const type_info &ti):
20 logic_error("expected "+Debug::demangle(ti.name()))
25 data(new BasicListData<string>),
31 List::List(ListData &d):
40 input_type = INPUT_NAVIGATION;
51 ignore_slider_change = false;
56 observer = new DataObserver(*this);
60 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
71 void List::autosize_special(const Part &part, Geometry &ageom) const
73 if(part.get_name()=="items")
75 const Sides &margin = part.get_margin();
79 for(unsigned i=0; i<items.size(); ++i)
82 items[i]->autosize(igeom);
83 items_w = max(items_w, igeom.w);
84 items_h = max(items_h, igeom.h);
89 unsigned r = view_rows;
90 unsigned c = view_columns;
92 r = sqrt(items.size());
94 r = (items.size()+c-1)/c;
96 c = (items.size()+r-1)/r;
101 items_h *= (view_rows==0 ? items.size() : view_rows);
103 ageom.w = max(ageom.w, items_w+margin.left+margin.right);
104 ageom.h = max(ageom.h, items_h+margin.top+margin.bottom);
106 else if(part.get_name()=="slider")
107 autosize_child(slider, part, ageom);
110 void List::set_data(ListData &d)
113 item_factory->set_data(d);
121 observer = new DataObserver(*this);
123 for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
126 unsigned n_items = data->size();
127 for(unsigned i=0; i<n_items; ++i)
129 Item *item = create_item(i);
130 items.push_back(item);
136 void List::items_changed()
138 signal_autosize_changed.emit();
142 List::Item *List::create_item(unsigned index)
146 item = item_factory->create_item(index);
148 item = new BasicItem(data->get_string(index));
149 if(static_cast<int>(index)==sel_index)
150 item->set_active(true);
153 item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
157 void List::set_view_size(unsigned s)
162 void List::set_view_size(unsigned r, unsigned c)
166 signal_autosize_changed.emit();
169 void List::set_view_all()
174 void List::set_selected_index(int i)
176 if(i>=static_cast<int>(data->size()))
177 throw out_of_range("List::set_selected_index");
179 if(i==sel_index || (i<0 && sel_index<0))
183 items[sel_index]->set_active(false);
189 signal_selection_cleared.emit();
195 items[sel_index]->set_active(true);
197 set_input_focus(items[focus_index]);
198 signal_item_selected.emit(sel_index);
202 void List::set_selected_item(Widget *item)
204 for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
206 return set_selected_index(i);
209 void List::rebuild_special(const Part &part)
211 if(part.get_name()=="slider")
212 reposition_child(slider, part);
213 else if(part.get_name()=="items")
215 SetFlag flag(ignore_slider_change);
216 reposition_items(true);
217 unsigned old_first_row = first_row;
218 unsigned old_max_scroll = max_scroll;
220 if(first_row!=old_first_row || max_scroll!=old_max_scroll)
221 reposition_items(false);
224 Widget::rebuild_special(part);
227 void List::render_special(const Part &part, GL::Renderer &renderer) const
229 if(part.get_name()=="items")
231 for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
232 items[i]->render(renderer);
234 else if(part.get_name()=="slider")
235 slider.render(renderer);
238 bool List::key_press(unsigned key, unsigned mod)
240 if(key==Input::KEY_UP && mod==MOD_CTRL)
241 move_focus(NAV_UP, false);
242 else if(key==Input::KEY_DOWN && mod==MOD_CTRL)
243 move_focus(NAV_DOWN, false);
250 void List::button_press(int x, int y, unsigned btn)
257 change = min(first_row, change);
258 slider.set_value(max_scroll-(first_row-change));
262 change = min(max_scroll-first_row, change);
263 slider.set_value(max_scroll-(first_row+change));
268 Container::button_press(x, y, btn);
269 if(click_focus && btn==1)
270 set_selected_item(click_focus);
274 void List::touch_press(int x, int y, unsigned finger)
284 void List::touch_release(int x, int y, unsigned finger)
288 int dx = x-drag_start_x;
289 int dy = y-drag_start_y;
292 Container::touch_press(drag_start_x, drag_start_y, finger);
294 set_selected_item(touch_focus);
295 Container::touch_motion(x, y, finger);
296 Container::touch_release(x, y, finger);
302 void List::touch_motion(int, int y, unsigned finger)
304 if(finger==0 && !items.empty() && dragging)
306 int dy = y-drag_start_y;
307 if(dy>0 && first_row<max_scroll)
309 int row_h = rows[first_row].height;
312 drag_start_y += row_h;
313 slider.set_value(max_scroll-(first_row+1));
316 else if(dy<0 && first_row>0)
318 int row_h = rows[first_row-1].height;
321 drag_start_y -= row_h;
322 slider.set_value(max_scroll-(first_row-1));
328 void List::focus_in()
330 Container::focus_in();
331 if(focus_index>=0 && items[focus_index]->is_visible())
332 set_input_focus(items[focus_index]);
335 if(sel_index>=0 && items[sel_index]->is_visible())
336 set_focus_index(sel_index);
337 else if(!items.empty())
338 set_focus_index(rows[first_row].first);
342 bool List::navigate(Navigation nav)
344 if((nav==NAV_UP || nav==NAV_DOWN || ((nav==NAV_LEFT || nav==NAV_RIGHT) && view_mode==GRID)) && !items.empty())
345 move_focus(nav, true);
346 else if(nav==NAV_ACTIVATE)
347 set_selected_index(focus_index);
354 void List::on_style_change()
356 items_part = (style ? style->get_part("items") : 0);
359 void List::move_focus(Navigation nav, bool select)
361 if(nav==NAV_UP && view_mode==GRID)
363 unsigned row = item_index_to_row(focus_index);
365 set_focus_index(rows[row-1].first+focus_index-rows[row].first);
369 else if(nav==NAV_DOWN && view_mode==GRID)
371 unsigned row = item_index_to_row(focus_index);
372 if(row+1<rows.size())
373 set_focus_index(rows[row+1].first+focus_index-rows[row].first);
375 set_focus_index(items.size()-1);
377 else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
380 set_focus_index(focus_index-1);
382 else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
384 if(static_cast<unsigned>(focus_index+1)<items.size())
385 set_focus_index(focus_index+1);
389 set_selected_index(focus_index);
392 void List::set_focus_index(int i)
399 set_input_focus(items[focus_index]);
403 void List::item_autosize_changed(Item *item)
406 signal_autosize_changed.emit();
410 void List::reposition_items(bool record_rows)
421 const Sides &margin = items_part->get_margin();
422 unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
426 for(unsigned i=0; i<items.size(); ++i)
428 const Geometry &igeom = items[i]->get_geometry();
430 if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
435 if(record_rows && i>0)
437 rows.back().height = row_h;
443 if(first_row<rows.size() && i==rows[first_row].first)
444 y = geom.h-min(geom.h, margin.top);
447 items[i]->set_visible(false);
448 else if(igeom.h+margin.bottom<=y)
450 items[i]->set_visible(true);
451 unsigned iw = (view_mode==GRID ? igeom.w : view_w);
452 items[i]->set_geometry(Geometry(margin.left+x, y-igeom.h, iw, igeom.h));
456 for(unsigned j=rows.back().first; j<=i; ++j)
457 items[j]->set_visible(false);
462 row_h = max(row_h, igeom.h);
466 rows.back().height = row_h;
469 unsigned List::last_to_first_row(unsigned last) const
474 const Sides &margin = items_part->get_margin();
475 unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
477 unsigned items_h = 0;
478 for(unsigned i=last; i<rows.size(); --i)
480 items_h += rows[i].height;
482 return min(i+1, last);
488 unsigned List::item_index_to_row(unsigned index) const
490 for(unsigned i=0; i+1<rows.size(); ++i)
491 if(rows[i+1].first>index)
493 return rows.size()-1;
496 void List::check_view_range()
504 max_scroll = last_to_first_row(rows.size()-1);
506 if(first_row>max_scroll)
507 first_row = max_scroll;
509 slider.set_range(0, 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));