]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
5a9f749fd0363b974ee1bfab98c37f37264a4354
[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 "graphic.h"
6 #include "list.h"
7 #include "part.h"
8 #include "style.h"
9 #include "text.h"
10 #include "vslider.h"
11
12 using namespace std;
13
14 namespace Msp {
15 namespace GLtk {
16
17 incompatible_data::incompatible_data(const type_info &ti):
18         logic_error("expected "+Debug::demangle(ti.name()))
19 { }
20
21
22 List::List():
23         data(new BasicListData<string>),
24         own_data(true)
25 {
26         init();
27 }
28
29 List::List(ListData &d):
30         data(&d),
31         own_data(false)
32 {
33         init();
34 }
35
36 void List::init()
37 {
38         input_type = INPUT_NAVIGATION;
39
40         item_factory = 0;
41         sel_index = -1;
42         focus_index = -1;
43         first = 0;
44         max_scroll = 0;
45         view_size = 5;
46         ignore_slider_change = false;
47         dragging = false;
48         drag_start_x = 0;
49         drag_start_y = 0;
50
51         observer = new DataObserver(*this);
52
53         add(slider);
54         slider.set_step(1);
55         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
56 }
57
58 List::~List()
59 {
60         delete item_factory;
61         delete observer;
62         if(own_data)
63                 delete data;
64 }
65
66 void List::autosize_special(const Part &part, Geometry &ageom) const
67 {
68         if(part.get_name()=="items")
69         {
70                 const Sides &margin = part.get_margin();
71
72                 unsigned max_w = 0;
73                 unsigned total_h = 0;
74                 for(unsigned i=0; i<items.size(); ++i)
75                 {
76                         Geometry igeom;
77                         items[i]->autosize(igeom);
78                         max_w = max(max_w, igeom.w);
79                         if(view_size==0 || i<view_size)
80                                 total_h += igeom.h;
81                 }
82
83                 if(!items.empty() && items.size()<view_size)
84                         total_h = total_h*view_size/items.size();
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 s)
141 {
142         view_size = s;
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=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                 check_view_range();
194
195                 const Sides &margin = part.get_margin();
196                 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
197                 unsigned y = geom.h-min(geom.h, margin.top);
198                 for(unsigned i=0; i<items.size(); ++i)
199                 {
200                         if(i<first || !y)
201                                 items[i]->set_visible(false);
202                         else
203                         {
204                                 Geometry igeom = items[i]->get_geometry();
205                                 if(igeom.h+margin.bottom<=y)
206                                 {
207                                         items[i]->set_visible(true);
208                                         y -= igeom.h;
209                                         igeom.x = margin.left;
210                                         igeom.y = y;
211                                         igeom.w = w;
212                                         items[i]->set_geometry(igeom);
213                                 }
214                                 else
215                                 {
216                                         items[i]->set_visible(false);
217                                         y = 0;
218                                 }
219                         }
220                 }
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=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 void List::button_press(int x, int y, unsigned btn)
238 {
239         if(btn==4 || btn==5)
240         {
241                 unsigned change = 3;
242                 if(btn==4)
243                 {
244                         change = min(first, change);
245                         slider.set_value(max_scroll-(first-change));
246                 }
247                 else if(btn==5)
248                 {
249                         change = min(max_scroll-first, change);
250                         slider.set_value(max_scroll-(first+change));
251                 }
252         }
253         else
254         {
255                 Container::button_press(x, y, btn);
256                 if(click_focus && btn==1)
257                         set_selected_item(click_focus);
258         }
259 }
260
261 void List::touch_press(int x, int y, unsigned finger)
262 {
263         if(finger==0)
264         {
265                 dragging = true;
266                 drag_start_x = x;
267                 drag_start_y = y;
268         }
269 }
270
271 void List::touch_release(int x, int y, unsigned finger)
272 {
273         if(finger==0)
274         {
275                 int dx = x-drag_start_x;
276                 int dy = y-drag_start_y;
277                 if(dx*dx+dy*dy<25)
278                 {
279                         Container::touch_press(drag_start_x, drag_start_y, finger);
280                         if(touch_focus)
281                                 set_selected_item(touch_focus);
282                         Container::touch_motion(x, y, finger);
283                         Container::touch_release(x, y, finger);
284                 }
285                 dragging = false;
286         }
287 }
288
289 void List::touch_motion(int, int y, unsigned finger)
290 {
291         if(finger==0 && !items.empty() && dragging)
292         {
293                 int dy = y-drag_start_y;
294                 if(dy>0 && first<max_scroll)
295                 {
296                         int item_h = items[first]->get_geometry().h;
297                         if(dy>item_h)
298                         {
299                                 drag_start_y += item_h;
300                                 slider.set_value(max_scroll-(first+1));
301                         }
302                 }
303                 else if(dy<0 && first>0)
304                 {
305                         int item_h = items[first-1]->get_geometry().h;
306                         if(-dy>item_h)
307                         {
308                                 drag_start_y -= item_h;
309                                 slider.set_value(max_scroll-(first-1));
310                         }
311                 }
312         }
313 }
314
315 void List::focus_in()
316 {
317         if(focus_index>=0 && items[focus_index]->is_visible())
318                 set_input_focus(items[focus_index]);
319         else
320         {
321                 if(sel_index>=0 && items[sel_index]->is_visible())
322                         set_focus_index(sel_index);
323                 else if(!items.empty())
324                         set_focus_index(first);
325         }
326 }
327
328 bool List::navigate(Navigation nav)
329 {
330         if(nav==NAV_UP && !items.empty())
331         {
332                 if(focus_index>0)
333                         set_focus_index(focus_index-1);
334         }
335         else if(nav==NAV_DOWN && !items.empty())
336         {
337                 if(static_cast<unsigned>(focus_index+1)<items.size())
338                         set_focus_index(focus_index+1);
339         }
340         else if(nav==NAV_ACTIVATE)
341                 set_selected_index(focus_index);
342         else
343                 return false;
344
345         return true;
346 }
347
348 void List::set_focus_index(int i)
349 {
350         focus_index = i;
351         if(focus_index>=0)
352         {
353                 scroll_to_focus();
354                 if(state&FOCUS)
355                         set_input_focus(items[focus_index]);
356         }
357 }
358
359 void List::item_autosize_changed(Item *item)
360 {
361         item->autosize();
362         signal_autosize_changed.emit();
363         rebuild();
364 }
365
366 unsigned List::last_to_first(unsigned last) const
367 {
368         if(!style)
369                 return last;
370
371         unsigned view_h = geom.h;
372         if(const Part *items_part = style->get_part("items"))
373         {
374                 const Sides &margin = items_part->get_margin();
375                 view_h -= margin.top+margin.bottom;
376         }
377
378         unsigned items_h = 0;
379         for(unsigned i=last; i<items.size(); --i)
380         {
381                 items_h += items[i]->get_geometry().h;
382                 if(items_h>view_h)
383                         return min(i+1, last);
384         }
385
386         return 0;
387 }
388
389 void List::check_view_range()
390 {
391         if(!style)
392                 return;
393
394         if(items.empty())
395                 max_scroll = 0;
396         else
397                 max_scroll = last_to_first(items.size()-1);
398
399         if(first>max_scroll)
400                 first = max_scroll;
401
402         slider.set_range(0, max_scroll);
403         slider.set_value(max_scroll-first);
404 }
405
406 void List::scroll_to_focus()
407 {
408         if(focus_index<0 || items[focus_index]->is_visible())
409                 return;
410
411         if(static_cast<unsigned>(focus_index)<first)
412                 slider.set_value(max_scroll-focus_index);
413         else
414                 slider.set_value(max_scroll-last_to_first(focus_index));
415 }
416
417 void List::slider_value_changed(double value)
418 {
419         if(max_scroll>0 && !ignore_slider_change)
420         {
421                 first = max_scroll-static_cast<unsigned>(value);
422                 rebuild();
423         }
424 }
425
426 void List::adjust_index(int &index, int pos, int change)
427 {
428         if(index>pos)
429                 index += change;
430         else if(index==pos)
431                 index = (change>0 ? index+change : -1);
432 }
433
434
435 List::DataObserver::DataObserver(List &l):
436         list(l)
437 {
438         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
439         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
440         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
441         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
442 }
443
444 void List::DataObserver::item_added(unsigned i)
445 {
446         adjust_index(list.sel_index, i, 1);
447         adjust_index(list.focus_index, i, 1);
448
449         Item *item = list.create_item(i);
450         list.items.insert(list.items.begin()+i, item);
451         list.items_changed();
452 }
453
454 void List::DataObserver::item_removed(unsigned i)
455 {
456         bool had_selection = (list.sel_index>=0);
457         adjust_index(list.sel_index, i, -1);
458         adjust_index(list.focus_index, i, -1);
459
460         delete list.items[i];
461         list.items.erase(list.items.begin()+i);
462         list.items_changed();
463
464         if(had_selection && list.sel_index<0)
465                 list.signal_selection_cleared.emit();
466 }
467
468 void List::DataObserver::cleared()
469 {
470         list.sel_index = -1;
471         list.focus_index = -1;
472         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
473                 delete *i;
474         list.items.clear();
475         list.items_changed();
476
477         list.signal_selection_cleared.emit();
478 }
479
480 void List::DataObserver::refresh_item(unsigned i)
481 {
482         delete list.items[i];
483         // Avoid stale pointer while create_item is executing
484         list.items[i] = 0;
485         list.items[i] = list.create_item(i);
486         list.items_changed();
487 }
488
489
490 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
491 {
492         if(part.get_name()=="children")
493         {
494                 const Sides &margin = part.get_margin();
495                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
496                 {
497                         Geometry cgeom;
498                         (*i)->widget->autosize(cgeom);
499                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
500                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
501                 }
502         }
503 }
504
505 void List::Item::set_active(bool a)
506 {
507         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
508 }
509
510 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
511 {
512         if(part.get_name()=="children")
513         {
514                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
515                         (*i)->widget->render(renderer);
516         }
517 }
518
519
520 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
521 {
522         if(widths.size()<children.size())
523                 widths.resize(children.size(), 0);
524
525         unsigned n = 0;
526         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
527         {
528                 Geometry cgeom;
529                 (*i)->widget->autosize(cgeom);
530                 // TODO invent a better way to specify spacings
531                 widths[n] = max(widths[n], cgeom.w+8);
532         }
533 }
534
535 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
536 {
537         if(!style)
538                 return;
539
540         const Part *part = style->get_part("children");
541         if(!part)
542                 return;
543
544         const Sides &margin = part->get_margin();
545         int x = margin.left;
546         unsigned n = 0;
547         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
548         {
549                 (*i)->widget->set_position(x, margin.bottom);
550                 x += widths[n];
551         }
552 }
553
554 void List::MultiColumnItem::on_style_change()
555 {
556         if(!style)
557                 return;
558
559         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
560                 (*i)->widget->autosize();
561
562         vector<unsigned> widths;
563         List *list = static_cast<List *>(parent);
564         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
565                 if(*i!=this)
566                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
567                                 mci->check_widths(widths);
568
569         vector<unsigned> self_widths(widths);
570         check_widths(self_widths);
571         bool update_all = false;
572         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
573                 update_all = self_widths[i]>widths[i];
574
575         if(update_all)
576         {
577                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
578                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
579                                 mci->set_widths(self_widths);
580         }
581
582         set_widths(self_widths);
583 }
584
585
586 List::BasicItem::BasicItem(const string &text):
587         label(text)
588 {
589         add(label);
590 }
591
592 void List::BasicItem::on_style_change()
593 {
594         if(!style)
595                 return;
596
597         label.autosize();
598         if(const Part *part = style->get_part("children"))
599         {
600                 const Sides &margin = part->get_margin();
601                 label.set_position(margin.left, margin.bottom);
602         }
603 }
604
605
606 List::Loader::Loader(List &l):
607         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
608 {
609         add("item", &Loader::item);
610         add("view_size", &List::view_size);
611 }
612
613 void List::Loader::item(const string &v)
614 {
615         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
616 }
617
618 } // namespace GLtk
619 } // namespace Msp