]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
897ea39ebadb972decb3e7fe8dd8a876ac58089d
[libs/gltk.git] / source / list.cpp
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>
7 #include "graphic.h"
8 #include "list.h"
9 #include "part.h"
10 #include "style.h"
11 #include "text.h"
12
13 using namespace std;
14
15 namespace Msp {
16 namespace GLtk {
17
18 incompatible_data::incompatible_data(const type_info &ti):
19         logic_error("expected "+Debug::demangle(ti.name()))
20 { }
21
22
23 List::List():
24         List(*new BasicListData<string>)
25 {
26         own_data = true;
27 }
28
29 List::List(ListData &d):
30         data(&d),
31         observer(new DataObserver(*this))
32 {
33         input_type = INPUT_NAVIGATION;
34
35         add(slider);
36         slider.set_step(1);
37         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
38 }
39
40 List::~List()
41 {
42         delete item_factory;
43         delete observer;
44         if(own_data)
45                 delete data;
46 }
47
48 void List::autosize_special(const Part &part, Geometry &ageom) const
49 {
50         if(part.get_name()=="items")
51         {
52                 const Sides &margin = part.get_margin();
53
54                 unsigned items_w = 0;
55                 unsigned items_h = 0;
56                 for(unsigned i=0; i<items.size(); ++i)
57                 {
58                         Geometry igeom;
59                         items[i]->autosize(igeom);
60                         items_w = max(items_w, igeom.w);
61                         items_h = max(items_h, igeom.h);
62                 }
63
64                 if(view_mode==GRID)
65                 {
66                         unsigned r = view_rows;
67                         unsigned c = view_columns;
68                         if(r==0 && c==0)
69                                 r = sqrt(items.size());
70                         if(r==0)
71                                 r = (items.size()+c-1)/c;
72                         if(c==0)
73                                 c = (items.size()+r-1)/r;
74                         items_w *= c;
75                         items_h *= r;
76                 }
77                 else
78                         items_h *= (view_rows==0 ? items.size() : view_rows);
79
80                 ageom.w = max(ageom.w, items_w+margin.left+margin.right);
81                 ageom.h = max(ageom.h, items_h+margin.top+margin.bottom);
82         }
83         else if(part.get_name()=="slider")
84                 autosize_child(slider, part, ageom);
85 }
86
87 void List::set_data(ListData &d)
88 {
89         if(item_factory)
90                 item_factory->set_data(d);
91
92         delete observer;
93         if(own_data)
94                 delete data;
95
96         data = &d;
97         own_data = false;
98         observer = new DataObserver(*this);
99
100         for(Item *i: items)
101                 delete i;
102         items.clear();
103         unsigned n_items = data->size();
104         for(unsigned i=0; i<n_items; ++i)
105         {
106                 Item *item = create_item(i);
107                 items.push_back(item);
108         }
109
110         items_changed();
111 }
112
113 void List::items_changed()
114 {
115         signal_autosize_changed.emit();
116         mark_rebuild();
117 }
118
119 List::Item *List::create_item(unsigned index)
120 {
121         Item *item = 0;
122         if(item_factory)
123                 item = item_factory->create_item(index);
124         else
125                 item = new BasicItem(data->get_string(index));
126         if(static_cast<int>(index)==sel_index)
127                 item->set_active(true);
128         add(*item);
129         item->autosize();
130         item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
131         return item;
132 }
133
134 void List::set_view_size(unsigned s)
135 {
136         set_view_size(s, s);
137 }
138
139 void List::set_view_size(unsigned r, unsigned c)
140 {
141         view_rows = r;
142         view_columns = c;
143         signal_autosize_changed.emit();
144 }
145
146 void List::set_view_all()
147 {
148         set_view_size(0);
149 }
150
151 void List::set_selected_index(int i)
152 {
153         if(i>=static_cast<int>(data->size()))
154                 throw out_of_range("List::set_selected_index");
155
156         if(i==sel_index || (i<0 && sel_index<0))
157                 return;
158
159         if(sel_index>=0)
160                 items[sel_index]->set_active(false);
161         if(i<0)
162         {
163                 sel_index = -1;
164                 focus_index = -1;
165                 set_input_focus(0);
166                 signal_selection_cleared.emit();
167         }
168         else
169         {
170                 sel_index = i;
171                 focus_index = i;
172                 items[sel_index]->set_active(true);
173                 if(state&FOCUS)
174                         set_input_focus(items[focus_index]);
175                 signal_item_selected.emit(sel_index);
176         }
177 }
178
179 void List::set_selected_item(Widget *item)
180 {
181         for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
182                 if(item==items[i])
183                         return set_selected_index(i);
184 }
185
186 void List::rebuild_special(const Part &part)
187 {
188         if(part.get_name()=="slider")
189                 reposition_child(slider, part);
190         else if(part.get_name()=="items")
191         {
192                 SetFlag flag(ignore_slider_change);
193                 reposition_items(true);
194                 unsigned old_first_row = first_row;
195                 unsigned old_max_scroll = max_scroll;
196                 check_view_range();
197                 if(first_row!=old_first_row || max_scroll!=old_max_scroll)
198                         reposition_items(false);
199         }
200
201         Widget::rebuild_special(part);
202 }
203
204 void List::render_special(const Part &part, GL::Renderer &renderer) const
205 {
206         if(part.get_name()=="items")
207         {
208                 for(unsigned i=rows[first_row].first; (i<items.size() && items[i]->is_visible()); ++i)
209                         items[i]->render(renderer);
210         }
211         else if(part.get_name()=="slider")
212                 slider.render(renderer);
213 }
214
215 bool List::key_press(unsigned key, unsigned mod)
216 {
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);
221         else
222                 return false;
223
224         return true;
225 }
226
227 void List::button_press(int x, int y, unsigned btn)
228 {
229         if(btn==4 || btn==5)
230         {
231                 unsigned change = 3;
232                 if(btn==4)
233                 {
234                         change = min(first_row, change);
235                         slider.set_value(max_scroll-(first_row-change));
236                 }
237                 else if(btn==5)
238                 {
239                         change = min(max_scroll-first_row, change);
240                         slider.set_value(max_scroll-(first_row+change));
241                 }
242         }
243         else
244         {
245                 Container::button_press(x, y, btn);
246                 if(click_focus && btn==1)
247                         set_selected_item(click_focus);
248         }
249 }
250
251 void List::touch_press(int x, int y, unsigned finger)
252 {
253         if(finger==0)
254         {
255                 dragging = true;
256                 drag_start_x = x;
257                 drag_start_y = y;
258         }
259 }
260
261 void List::touch_release(int x, int y, unsigned finger)
262 {
263         if(finger==0)
264         {
265                 int dx = x-drag_start_x;
266                 int dy = y-drag_start_y;
267                 if(dx*dx+dy*dy<25)
268                 {
269                         Container::touch_press(drag_start_x, drag_start_y, finger);
270                         if(touch_focus)
271                                 set_selected_item(touch_focus);
272                         Container::touch_motion(x, y, finger);
273                         Container::touch_release(x, y, finger);
274                 }
275                 dragging = false;
276         }
277 }
278
279 void List::touch_motion(int, int y, unsigned finger)
280 {
281         if(finger==0 && !items.empty() && dragging)
282         {
283                 int dy = y-drag_start_y;
284                 if(dy>0 && first_row<max_scroll)
285                 {
286                         int row_h = rows[first_row].height;
287                         if(dy>row_h)
288                         {
289                                 drag_start_y += row_h;
290                                 slider.set_value(max_scroll-(first_row+1));
291                         }
292                 }
293                 else if(dy<0 && first_row>0)
294                 {
295                         int row_h = rows[first_row-1].height;
296                         if(-dy>row_h)
297                         {
298                                 drag_start_y -= row_h;
299                                 slider.set_value(max_scroll-(first_row-1));
300                         }
301                 }
302         }
303 }
304
305 void List::focus_in()
306 {
307         Container::focus_in();
308         if(focus_index>=0 && items[focus_index]->is_visible())
309                 set_input_focus(items[focus_index]);
310         else
311         {
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);
316         }
317 }
318
319 bool List::navigate(Navigation nav)
320 {
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);
325         else
326                 return false;
327
328         return true;
329 }
330
331 void List::on_style_change()
332 {
333         items_part = (style ? style->get_part("items") : 0);
334 }
335
336 void List::move_focus(Navigation nav, bool select)
337 {
338         if(nav==NAV_UP && view_mode==GRID)
339         {
340                 unsigned row = item_index_to_row(focus_index);
341                 if(row>0)
342                         set_focus_index(rows[row-1].first+focus_index-rows[row].first);
343                 else
344                         set_focus_index(0);
345         }
346         else if(nav==NAV_DOWN && view_mode==GRID)
347         {
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);
351                 else
352                         set_focus_index(items.size()-1);
353         }
354         else if(nav==NAV_UP || (nav==NAV_LEFT && view_mode==GRID))
355         {
356                 if(focus_index>0)
357                         set_focus_index(focus_index-1);
358         }
359         else if(nav==NAV_DOWN || (nav==NAV_RIGHT && view_mode==GRID))
360         {
361                 if(static_cast<unsigned>(focus_index+1)<items.size())
362                         set_focus_index(focus_index+1);
363         }
364
365         if(select)
366                 set_selected_index(focus_index);
367 }
368
369 void List::set_focus_index(int i)
370 {
371         focus_index = i;
372         if(focus_index>=0)
373         {
374                 scroll_to_focus();
375                 if(state&FOCUS)
376                         set_input_focus(items[focus_index]);
377         }
378 }
379
380 void List::item_autosize_changed(Item *item)
381 {
382         item->autosize();
383         signal_autosize_changed.emit();
384         mark_rebuild();
385 }
386
387 void List::reposition_items(bool record_rows)
388 {
389         if(!items_part)
390                 return;
391
392         if(record_rows)
393         {
394                 rows.clear();
395                 rows.push_back(0);
396         }
397
398         const Sides &margin = items_part->get_margin();
399         unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
400         unsigned x = 0;
401         unsigned y = 0;
402         unsigned row_h = 0;
403         for(unsigned i=0; i<items.size(); ++i)
404         {
405                 const Geometry &igeom = items[i]->get_geometry();
406
407                 if(view_mode!=GRID || (x>0 && x+igeom.w>view_w))
408                 {
409                         x = 0;
410                         if(y)
411                                 y -= row_h;
412                         if(record_rows && i>0)
413                         {
414                                 rows.back().height = row_h;
415                                 rows.push_back(i);
416                         }
417                         row_h = 0;
418                 }
419
420                 if(first_row<rows.size() && i==rows[first_row].first)
421                         y = geom.h-min(geom.h, margin.top);
422
423                 if(!y)
424                         items[i]->set_visible(false);
425                 else if(igeom.h+margin.bottom<=y)
426                 {
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));
430                 }
431                 else
432                 {
433                         for(unsigned j=rows.back().first; j<=i; ++j)
434                                 items[j]->set_visible(false);
435                         y = 0;
436                 }
437
438                 x += igeom.w;
439                 row_h = max(row_h, igeom.h);
440         }
441
442         if(record_rows)
443                 rows.back().height = row_h;
444 }
445
446 unsigned List::last_to_first_row(unsigned last) const
447 {
448         if(!items_part)
449                 return last;
450
451         const Sides &margin = items_part->get_margin();
452         unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
453
454         unsigned items_h = 0;
455         for(unsigned i=last; i<rows.size(); --i)
456         {
457                 items_h += rows[i].height;
458                 if(items_h>view_h)
459                         return min(i+1, last);
460         }
461
462         return 0;
463 }
464
465 unsigned List::item_index_to_row(unsigned index) const
466 {
467         for(unsigned i=0; i+1<rows.size(); ++i)
468                 if(rows[i+1].first>index)
469                         return i;
470         return rows.size()-1;
471 }
472
473 void List::check_view_range()
474 {
475         if(!style)
476                 return;
477
478         if(items.empty())
479                 max_scroll = 0;
480         else
481                 max_scroll = last_to_first_row(rows.size()-1);
482
483         if(first_row>max_scroll)
484                 first_row = max_scroll;
485
486         slider.set_range(0, max_scroll);
487         slider.set_page_size(rows.size()-max_scroll);
488         slider.set_value(max_scroll-first_row);
489 }
490
491 void List::scroll_to_focus()
492 {
493         if(focus_index<0 || items[focus_index]->is_visible())
494                 return;
495
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);
499         else
500                 slider.set_value(max_scroll-last_to_first_row(focus_row));
501 }
502
503 void List::slider_value_changed(double value)
504 {
505         if(max_scroll>0 && !ignore_slider_change)
506         {
507                 first_row = max_scroll-static_cast<unsigned>(value);
508                 mark_rebuild();
509         }
510 }
511
512 void List::adjust_index(int &index, int pos, int change)
513 {
514         if(index>pos)
515                 index += change;
516         else if(index==pos)
517                 index = (change>0 ? index+change : -1);
518 }
519
520
521 List::DataObserver::DataObserver(List &l):
522         list(l)
523 {
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));
528 }
529
530 void List::DataObserver::item_added(unsigned i)
531 {
532         adjust_index(list.sel_index, i, 1);
533         adjust_index(list.focus_index, i, 1);
534
535         Item *item = list.create_item(i);
536         list.items.insert(list.items.begin()+i, item);
537         list.items_changed();
538 }
539
540 void List::DataObserver::item_removed(unsigned i)
541 {
542         bool had_selection = (list.sel_index>=0);
543         adjust_index(list.sel_index, i, -1);
544         adjust_index(list.focus_index, i, -1);
545
546         delete list.items[i];
547         list.items.erase(list.items.begin()+i);
548         list.items_changed();
549
550         if(had_selection && list.sel_index<0)
551                 list.signal_selection_cleared.emit();
552 }
553
554 void List::DataObserver::cleared()
555 {
556         list.sel_index = -1;
557         list.focus_index = -1;
558         for(Item *i: list.items)
559                 delete i;
560         list.items.clear();
561         list.items_changed();
562
563         list.signal_selection_cleared.emit();
564 }
565
566 void List::DataObserver::refresh_item(unsigned i)
567 {
568         delete list.items[i];
569         // Avoid stale pointer while create_item is executing
570         list.items[i] = 0;
571         list.items[i] = list.create_item(i);
572         list.items_changed();
573 }
574
575
576 List::Item::Item()
577 {
578         input_type = INPUT_NAVIGATION;
579 }
580
581 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
582 {
583         if(part.get_name()=="children")
584         {
585                 const Sides &margin = part.get_margin();
586                 for(const Child *c: children)
587                 {
588                         Geometry cgeom;
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);
592                 }
593         }
594 }
595
596 void List::Item::set_active(bool a)
597 {
598         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
599 }
600
601 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
602 {
603         if(part.get_name()=="children")
604         {
605                 for(const Child *c: children)
606                         c->widget->render(renderer);
607         }
608 }
609
610
611 void List::SimpleItem::on_style_change()
612 {
613         if(!style || children.empty())
614                 return;
615
616         Widget *child = children.front()->widget;
617         child->autosize();
618         if(const Part *part = style->get_part("children"))
619         {
620                 const Sides &margin = part->get_margin();
621                 child->set_position(margin.left, margin.bottom);
622         }
623 }
624
625
626 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
627 {
628         if(widths.size()<children.size())
629                 widths.resize(children.size(), 0);
630
631         unsigned n = 0;
632         for(const Child *c: children)
633         {
634                 Geometry cgeom;
635                 c->widget->autosize(cgeom);
636                 // TODO invent a better way to specify spacings
637                 widths[n] = max(widths[n], cgeom.w+8);
638                 ++n;
639         }
640 }
641
642 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
643 {
644         if(!style)
645                 return;
646
647         const Part *part = style->get_part("children");
648         if(!part)
649                 return;
650
651         const Sides &margin = part->get_margin();
652         int x = margin.left;
653         unsigned n = 0;
654         for(const Child *c: children)
655         {
656                 c->widget->set_position(x, margin.bottom);
657                 x += widths[n++];
658         }
659 }
660
661 void List::MultiColumnItem::on_style_change()
662 {
663         if(!style)
664                 return;
665
666         for(const Child *c: children)
667                 c->widget->autosize();
668
669         vector<unsigned> widths;
670         List *list = static_cast<List *>(parent);
671         for(Item *i: list->items)
672                 if(i!=this)
673                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
674                                 mci->check_widths(widths);
675
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];
681
682         if(update_all)
683         {
684                 for(Item *i: list->items)
685                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(i))
686                                 mci->set_widths(self_widths);
687         }
688
689         set_widths(self_widths);
690 }
691
692
693 List::BasicItem::BasicItem(const string &text):
694         label(text)
695 {
696         add(label);
697 }
698
699
700 List::Loader::Loader(List &l):
701         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
702 {
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);
707 }
708
709 void List::Loader::item(const string &v)
710 {
711         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
712 }
713
714
715 void operator>>(const LexicalConverter &conv, List::ViewMode &vm)
716 {
717         const string &str = conv.get();
718         if(str=="LIST")
719                 vm = List::LIST;
720         else if(str=="GRID")
721                 vm = List::GRID;
722         else
723                 throw lexical_error(format("conversion of '%s' to List::ViewMode", str));
724 }
725
726 } // namespace GLtk
727 } // namespace Msp