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