]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Add a level of indirection to decouple list rows from items
[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 "graphic.h"
7 #include "list.h"
8 #include "part.h"
9 #include "style.h"
10 #include "text.h"
11 #include "vslider.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         sel_index = -1;
43         focus_index = -1;
44         first_row = 0;
45         max_scroll = 0;
46         view_rows = 5;
47         items_part = 0;
48         ignore_slider_change = false;
49         dragging = false;
50         drag_start_x = 0;
51         drag_start_y = 0;
52
53         observer = new DataObserver(*this);
54
55         add(slider);
56         slider.set_step(1);
57         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
58 }
59
60 List::~List()
61 {
62         delete item_factory;
63         delete observer;
64         if(own_data)
65                 delete data;
66 }
67
68 void List::autosize_special(const Part &part, Geometry &ageom) const
69 {
70         if(part.get_name()=="items")
71         {
72                 const Sides &margin = part.get_margin();
73
74                 unsigned max_w = 0;
75                 unsigned max_h = 0;
76                 for(unsigned i=0; i<items.size(); ++i)
77                 {
78                         Geometry igeom;
79                         items[i]->autosize(igeom);
80                         max_w = max(max_w, igeom.w);
81                         max_h = max(max_h, igeom.h);
82                 }
83
84                 unsigned total_h = max_h*(view_rows==0 ? items.size() : view_rows);
85
86                 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
87                 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
88         }
89         else if(part.get_name()=="slider")
90                 autosize_child(slider, part, ageom);
91 }
92
93 void List::set_data(ListData &d)
94 {
95         if(item_factory)
96                 item_factory->set_data(d);
97
98         delete observer;
99         if(own_data)
100                 delete data;
101
102         data = &d;
103         own_data = false;
104         observer = new DataObserver(*this);
105
106         for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
107                 delete *i;
108         items.clear();
109         unsigned n_items = data->size();
110         for(unsigned i=0; i<n_items; ++i)
111         {
112                 Item *item = create_item(i);
113                 items.push_back(item);
114         }
115
116         items_changed();
117 }
118
119 void List::items_changed()
120 {
121         signal_autosize_changed.emit();
122         rebuild();
123 }
124
125 List::Item *List::create_item(unsigned index)
126 {
127         Item *item = 0;
128         if(item_factory)
129                 item = item_factory->create_item(index);
130         else
131                 item = new BasicItem(data->get_string(index));
132         if(static_cast<int>(index)==sel_index)
133                 item->set_active(true);
134         add(*item);
135         item->autosize();
136         item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
137         return item;
138 }
139
140 void List::set_view_size(unsigned r)
141 {
142         view_rows = r;
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) && !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)
339         {
340                 if(focus_index>0)
341                         set_focus_index(focus_index-1);
342         }
343         else if(nav==NAV_DOWN)
344         {
345                 if(static_cast<unsigned>(focus_index+1)<items.size())
346                         set_focus_index(focus_index+1);
347         }
348
349         if(select)
350                 set_selected_index(focus_index);
351 }
352
353 void List::set_focus_index(int i)
354 {
355         focus_index = i;
356         if(focus_index>=0)
357         {
358                 scroll_to_focus();
359                 if(state&FOCUS)
360                         set_input_focus(items[focus_index]);
361         }
362 }
363
364 void List::item_autosize_changed(Item *item)
365 {
366         item->autosize();
367         signal_autosize_changed.emit();
368         rebuild();
369 }
370
371 void List::reposition_items(bool record_rows)
372 {
373         if(!items_part)
374                 return;
375
376         if(record_rows)
377         {
378                 rows.clear();
379                 rows.push_back(0);
380         }
381
382         const Sides &margin = items_part->get_margin();
383         unsigned view_w = geom.w-min(geom.w, margin.left+margin.right);
384         unsigned y = 0;
385         unsigned row_h = 0;
386         for(unsigned i=0; i<items.size(); ++i)
387         {
388                 const Geometry &igeom = items[i]->get_geometry();
389
390                 if(y)
391                         y -= row_h;
392                 if(record_rows && i>0)
393                 {
394                         rows.back().height = row_h;
395                         rows.push_back(i);
396                 }
397                 row_h = 0;
398
399                 if(first_row<rows.size() && i==rows[first_row].first)
400                         y = geom.h-min(geom.h, margin.top);
401
402                 if(!y)
403                         items[i]->set_visible(false);
404                 else if(igeom.h+margin.bottom<=y)
405                 {
406                         items[i]->set_visible(true);
407                         items[i]->set_geometry(Geometry(margin.left, y-igeom.h, view_w, igeom.h));
408                 }
409                 else
410                 {
411                         for(unsigned j=rows.back().first; j<=i; ++j)
412                                 items[j]->set_visible(false);
413                         y = 0;
414                 }
415
416                 row_h = max(row_h, igeom.h);
417         }
418
419         if(record_rows)
420                 rows.back().height = row_h;
421 }
422
423 unsigned List::last_to_first_row(unsigned last) const
424 {
425         if(!items_part)
426                 return last;
427
428         const Sides &margin = items_part->get_margin();
429         unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
430
431         unsigned items_h = 0;
432         for(unsigned i=last; i<rows.size(); --i)
433         {
434                 items_h += rows[i].height;
435                 if(items_h>view_h)
436                         return min(i+1, last);
437         }
438
439         return 0;
440 }
441
442 unsigned List::item_index_to_row(unsigned index) const
443 {
444         for(unsigned i=0; i+1<rows.size(); ++i)
445                 if(rows[i+1].first>index)
446                         return i;
447         return rows.size()-1;
448 }
449
450 void List::check_view_range()
451 {
452         if(!style)
453                 return;
454
455         if(items.empty())
456                 max_scroll = 0;
457         else
458                 max_scroll = last_to_first_row(rows.size()-1);
459
460         if(first_row>max_scroll)
461                 first_row = max_scroll;
462
463         slider.set_range(0, max_scroll);
464         slider.set_value(max_scroll-first_row);
465 }
466
467 void List::scroll_to_focus()
468 {
469         if(focus_index<0 || items[focus_index]->is_visible())
470                 return;
471
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);
475         else
476                 slider.set_value(max_scroll-last_to_first_row(focus_row));
477 }
478
479 void List::slider_value_changed(double value)
480 {
481         if(max_scroll>0 && !ignore_slider_change)
482         {
483                 first_row = max_scroll-static_cast<unsigned>(value);
484                 rebuild();
485         }
486 }
487
488 void List::adjust_index(int &index, int pos, int change)
489 {
490         if(index>pos)
491                 index += change;
492         else if(index==pos)
493                 index = (change>0 ? index+change : -1);
494 }
495
496
497 List::DataObserver::DataObserver(List &l):
498         list(l)
499 {
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));
504 }
505
506 void List::DataObserver::item_added(unsigned i)
507 {
508         adjust_index(list.sel_index, i, 1);
509         adjust_index(list.focus_index, i, 1);
510
511         Item *item = list.create_item(i);
512         list.items.insert(list.items.begin()+i, item);
513         list.items_changed();
514 }
515
516 void List::DataObserver::item_removed(unsigned i)
517 {
518         bool had_selection = (list.sel_index>=0);
519         adjust_index(list.sel_index, i, -1);
520         adjust_index(list.focus_index, i, -1);
521
522         delete list.items[i];
523         list.items.erase(list.items.begin()+i);
524         list.items_changed();
525
526         if(had_selection && list.sel_index<0)
527                 list.signal_selection_cleared.emit();
528 }
529
530 void List::DataObserver::cleared()
531 {
532         list.sel_index = -1;
533         list.focus_index = -1;
534         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
535                 delete *i;
536         list.items.clear();
537         list.items_changed();
538
539         list.signal_selection_cleared.emit();
540 }
541
542 void List::DataObserver::refresh_item(unsigned i)
543 {
544         delete list.items[i];
545         // Avoid stale pointer while create_item is executing
546         list.items[i] = 0;
547         list.items[i] = list.create_item(i);
548         list.items_changed();
549 }
550
551
552 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
553 {
554         if(part.get_name()=="children")
555         {
556                 const Sides &margin = part.get_margin();
557                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
558                 {
559                         Geometry cgeom;
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);
563                 }
564         }
565 }
566
567 void List::Item::set_active(bool a)
568 {
569         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
570 }
571
572 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
573 {
574         if(part.get_name()=="children")
575         {
576                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
577                         (*i)->widget->render(renderer);
578         }
579 }
580
581
582 void List::SimpleItem::on_style_change()
583 {
584         if(!style || children.empty())
585                 return;
586
587         Widget *child = children.front()->widget;
588         child->autosize();
589         if(const Part *part = style->get_part("children"))
590         {
591                 const Sides &margin = part->get_margin();
592                 child->set_position(margin.left, margin.bottom);
593         }
594 }
595
596
597 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
598 {
599         if(widths.size()<children.size())
600                 widths.resize(children.size(), 0);
601
602         unsigned n = 0;
603         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
604         {
605                 Geometry cgeom;
606                 (*i)->widget->autosize(cgeom);
607                 // TODO invent a better way to specify spacings
608                 widths[n] = max(widths[n], cgeom.w+8);
609         }
610 }
611
612 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
613 {
614         if(!style)
615                 return;
616
617         const Part *part = style->get_part("children");
618         if(!part)
619                 return;
620
621         const Sides &margin = part->get_margin();
622         int x = margin.left;
623         unsigned n = 0;
624         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
625         {
626                 (*i)->widget->set_position(x, margin.bottom);
627                 x += widths[n];
628         }
629 }
630
631 void List::MultiColumnItem::on_style_change()
632 {
633         if(!style)
634                 return;
635
636         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
637                 (*i)->widget->autosize();
638
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)
642                 if(*i!=this)
643                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
644                                 mci->check_widths(widths);
645
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];
651
652         if(update_all)
653         {
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);
657         }
658
659         set_widths(self_widths);
660 }
661
662
663 List::BasicItem::BasicItem(const string &text):
664         label(text)
665 {
666         add(label);
667 }
668
669
670 List::Loader::Loader(List &l):
671         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
672 {
673         add("item", &Loader::item);
674         add("view_size", &List::view_rows);
675 }
676
677 void List::Loader::item(const string &v)
678 {
679         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
680 }
681
682 } // namespace GLtk
683 } // namespace Msp