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>
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;
48 ignore_slider_change = false;
53 observer = new DataObserver(*this);
57 slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
68 void List::autosize_special(const Part &part, Geometry &ageom) const
70 if(part.get_name()=="items")
72 const Sides &margin = part.get_margin();
76 for(unsigned i=0; i<items.size(); ++i)
79 items[i]->autosize(igeom);
80 max_w = max(max_w, igeom.w);
81 max_h = max(max_h, igeom.h);
84 unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows);
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 r)
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) && !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)
341 set_focus_index(focus_index-1);
343 else if(nav==NAV_DOWN)
345 if(static_cast<unsigned>(focus_index+1)<items.size())
346 set_focus_index(focus_index+1);
350 set_selected_index(focus_index);
353 void List::set_focus_index(int i)
360 set_input_focus(items[focus_index]);
364 void List::item_autosize_changed(Item *item)
367 signal_autosize_changed.emit();
371 void List::reposition_items(bool record_rows)
382 const Sides &margin = items_part->get_margin();
383 unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
386 for(unsigned i=0; i<items.size(); ++i)
388 const Geometry &igeom = items[i]->get_geometry();
392 if(record_rows && i>0)
394 rows.back().height = row_h;
399 if(first_row<rows.size() && i==rows[first_row].first)
400 y = geom.h-min(geom.h, margin.top);
403 items[i]->set_visible(false);
404 else if(igeom.h+margin.bottom<=y)
406 items[i]->set_visible(true);
407 items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h));
411 for(unsigned j=rows.back().first; j<=i; ++j)
412 items[j]->set_visible(false);
416 row_h = max(row_h, igeom.h);
420 rows.back().height = row_h;
423 unsigned List::last_to_first_row(unsigned last) const
428 const Sides &margin = items_part->get_margin();
429 unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
431 unsigned items_h = 0;
432 for(unsigned i=last; i<rows.size(); --i)
434 items_h += rows[i].height;
436 return min(i+1, last);
442 unsigned List::item_index_to_row(unsigned index) const
444 for(unsigned i=0; i+1<rows.size(); ++i)
445 if(rows[i+1].first>index)
447 return rows.size()-1;
450 void List::check_view_range()
458 max_scroll = last_to_first_row(rows.size()-1);
460 if(first_row>max_scroll)
461 first_row = max_scroll;
463 slider.set_range(0, max_scroll);
464 slider.set_value(max_scroll-first_row);
467 void List::scroll_to_focus()
469 if(focus_index<0 || items[focus_index]->is_visible())
472 unsigned focus_row = item_index_to_row(static_cast<unsigned>(focus_index));
473 if(focus_row<first_row)
474 slider.set_value(max_scroll-focus_row);
476 slider.set_value(max_scroll-last_to_first_row(focus_row));
479 void List::slider_value_changed(double value)
481 if(max_scroll>0 && !ignore_slider_change)
483 first_row = max_scroll-static_cast<unsigned>(value);
488 void List::adjust_index(int &index, int pos, int change)
493 index = (change>0 ? index+change : -1);
497 List::DataObserver::DataObserver(List &l):
500 list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
501 list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
502 list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
503 list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
506 void List::DataObserver::item_added(unsigned i)
508 adjust_index(list.sel_index, i, 1);
509 adjust_index(list.focus_index, i, 1);
511 Item *item = list.create_item(i);
512 list.items.insert(list.items.begin()+i, item);
513 list.items_changed();
516 void List::DataObserver::item_removed(unsigned i)
518 bool had_selection = (list.sel_index>=0);
519 adjust_index(list.sel_index, i, -1);
520 adjust_index(list.focus_index, i, -1);
522 delete list.items[i];
523 list.items.erase(list.items.begin()+i);
524 list.items_changed();
526 if(had_selection && list.sel_index<0)
527 list.signal_selection_cleared.emit();
530 void List::DataObserver::cleared()
533 list.focus_index = -1;
534 for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
537 list.items_changed();
539 list.signal_selection_cleared.emit();
542 void List::DataObserver::refresh_item(unsigned i)
544 delete list.items[i];
545 // Avoid stale pointer while create_item is executing
547 list.items[i] = list.create_item(i);
548 list.items_changed();
552 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
554 if(part.get_name()=="children")
556 const Sides &margin = part.get_margin();
557 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
560 (*i)->widget->autosize(cgeom);
561 ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
562 ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
567 void List::Item::set_active(bool a)
569 set_state(ACTIVE, (a ? ACTIVE : NORMAL));
572 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
574 if(part.get_name()=="children")
576 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
577 (*i)->widget->render(renderer);
582 void List::SimpleItem::on_style_change()
584 if(!style || children.empty())
587 Widget *child = children.front()->widget;
589 if(const Part *part = style->get_part("children"))
591 const Sides &margin = part->get_margin();
592 child->set_position(margin.left, margin.bottom);
597 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
599 if(widths.size()<children.size())
600 widths.resize(children.size(), 0);
603 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
606 (*i)->widget->autosize(cgeom);
607 // TODO invent a better way to specify spacings
608 widths[n] = max(widths[n], cgeom.w+8);
612 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
617 const Part *part = style->get_part("children");
621 const Sides &margin = part->get_margin();
624 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
626 (*i)->widget->set_position(x, margin.bottom);
631 void List::MultiColumnItem::on_style_change()
636 for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
637 (*i)->widget->autosize();
639 vector<unsigned> widths;
640 List *list = static_cast<List *>(parent);
641 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
643 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
644 mci->check_widths(widths);
646 vector<unsigned> self_widths(widths);
647 check_widths(self_widths);
648 bool update_all = false;
649 for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
650 update_all = self_widths[i]>widths[i];
654 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
655 if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
656 mci->set_widths(self_widths);
659 set_widths(self_widths);
663 List::BasicItem::BasicItem(const string &text):
670 List::Loader::Loader(List &l):
671 DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
673 add("item", &Loader::item);
674 add("view_size", &List::view_rows);
677 void List::Loader::item(const string &v)
679 dynamic_cast<BasicListData<string> &>(*obj.data).append(v);