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