]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Select list items upon navigation by default
[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 = 0;
45         max_scroll = 0;
46         view_size = 5;
47         ignore_slider_change = false;
48         dragging = false;
49         drag_start_x = 0;
50         drag_start_y = 0;
51
52         observer = new DataObserver(*this);
53
54         add(slider);
55         slider.set_step(1);
56         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
57 }
58
59 List::~List()
60 {
61         delete item_factory;
62         delete observer;
63         if(own_data)
64                 delete data;
65 }
66
67 void List::autosize_special(const Part &part, Geometry &ageom) const
68 {
69         if(part.get_name()=="items")
70         {
71                 const Sides &margin = part.get_margin();
72
73                 unsigned max_w = 0;
74                 unsigned total_h = 0;
75                 for(unsigned i=0; i<items.size(); ++i)
76                 {
77                         Geometry igeom;
78                         items[i]->autosize(igeom);
79                         max_w = max(max_w, igeom.w);
80                         if(view_size==0 || i<view_size)
81                                 total_h += igeom.h;
82                 }
83
84                 if(!items.empty() && items.size()<view_size)
85                         total_h = total_h*view_size/items.size();
86
87                 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
88                 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
89         }
90         else if(part.get_name()=="slider")
91                 autosize_child(slider, part, ageom);
92 }
93
94 void List::set_data(ListData &d)
95 {
96         if(item_factory)
97                 item_factory->set_data(d);
98
99         delete observer;
100         if(own_data)
101                 delete data;
102
103         data = &d;
104         own_data = false;
105         observer = new DataObserver(*this);
106
107         for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
108                 delete *i;
109         items.clear();
110         unsigned n_items = data->size();
111         for(unsigned i=0; i<n_items; ++i)
112         {
113                 Item *item = create_item(i);
114                 items.push_back(item);
115         }
116
117         items_changed();
118 }
119
120 void List::items_changed()
121 {
122         signal_autosize_changed.emit();
123         rebuild();
124 }
125
126 List::Item *List::create_item(unsigned index)
127 {
128         Item *item = 0;
129         if(item_factory)
130                 item = item_factory->create_item(index);
131         else
132                 item = new BasicItem(data->get_string(index));
133         if(static_cast<int>(index)==sel_index)
134                 item->set_active(true);
135         add(*item);
136         item->autosize();
137         item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
138         return item;
139 }
140
141 void List::set_view_size(unsigned s)
142 {
143         view_size = s;
144         signal_autosize_changed.emit();
145 }
146
147 void List::set_view_all()
148 {
149         set_view_size(0);
150 }
151
152 void List::set_selected_index(int i)
153 {
154         if(i>=static_cast<int>(data->size()))
155                 throw out_of_range("List::set_selected_index");
156
157         if(i==sel_index || (i<0 && sel_index<0))
158                 return;
159
160         if(sel_index>=0)
161                 items[sel_index]->set_active(false);
162         if(i<0)
163         {
164                 sel_index = -1;
165                 focus_index = -1;
166                 set_input_focus(0);
167                 signal_selection_cleared.emit();
168         }
169         else
170         {
171                 sel_index = i;
172                 focus_index = i;
173                 items[sel_index]->set_active(true);
174                 if(state&FOCUS)
175                         set_input_focus(items[focus_index]);
176                 signal_item_selected.emit(sel_index);
177         }
178 }
179
180 void List::set_selected_item(Widget *item)
181 {
182         for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
183                 if(item==items[i])
184                         return set_selected_index(i);
185 }
186
187 void List::rebuild_special(const Part &part)
188 {
189         if(part.get_name()=="slider")
190                 reposition_child(slider, part);
191         else if(part.get_name()=="items")
192         {
193                 SetFlag flag(ignore_slider_change);
194                 check_view_range();
195
196                 const Sides &margin = part.get_margin();
197                 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
198                 unsigned y = geom.h-min(geom.h, margin.top);
199                 for(unsigned i=0; i<items.size(); ++i)
200                 {
201                         if(i<first || !y)
202                                 items[i]->set_visible(false);
203                         else
204                         {
205                                 Geometry igeom = items[i]->get_geometry();
206                                 if(igeom.h+margin.bottom<=y)
207                                 {
208                                         items[i]->set_visible(true);
209                                         y -= igeom.h;
210                                         igeom.x = margin.left;
211                                         igeom.y = y;
212                                         igeom.w = w;
213                                         items[i]->set_geometry(igeom);
214                                 }
215                                 else
216                                 {
217                                         items[i]->set_visible(false);
218                                         y = 0;
219                                 }
220                         }
221                 }
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=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, change);
258                         slider.set_value(max_scroll-(first-change));
259                 }
260                 else if(btn==5)
261                 {
262                         change = min(max_scroll-first, change);
263                         slider.set_value(max_scroll-(first+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<max_scroll)
308                 {
309                         int item_h = items[first]->get_geometry().h;
310                         if(dy>item_h)
311                         {
312                                 drag_start_y += item_h;
313                                 slider.set_value(max_scroll-(first+1));
314                         }
315                 }
316                 else if(dy<0 && first>0)
317                 {
318                         int item_h = items[first-1]->get_geometry().h;
319                         if(-dy>item_h)
320                         {
321                                 drag_start_y -= item_h;
322                                 slider.set_value(max_scroll-(first-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(first);
339         }
340 }
341
342 bool List::navigate(Navigation nav)
343 {
344         if((nav==NAV_UP || nav==NAV_DOWN) && !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::move_focus(Navigation nav, bool select)
355 {
356         if(nav==NAV_UP)
357         {
358                 if(focus_index>0)
359                         set_focus_index(focus_index-1);
360         }
361         else if(nav==NAV_DOWN)
362         {
363                 if(static_cast<unsigned>(focus_index+1)<items.size())
364                         set_focus_index(focus_index+1);
365         }
366
367         if(select)
368                 set_selected_index(focus_index);
369 }
370
371 void List::set_focus_index(int i)
372 {
373         focus_index = i;
374         if(focus_index>=0)
375         {
376                 scroll_to_focus();
377                 if(state&FOCUS)
378                         set_input_focus(items[focus_index]);
379         }
380 }
381
382 void List::item_autosize_changed(Item *item)
383 {
384         item->autosize();
385         signal_autosize_changed.emit();
386         rebuild();
387 }
388
389 unsigned List::last_to_first(unsigned last) const
390 {
391         if(!style)
392                 return last;
393
394         unsigned view_h = geom.h;
395         if(const Part *items_part = style->get_part("items"))
396         {
397                 const Sides &margin = items_part->get_margin();
398                 view_h -= margin.top+margin.bottom;
399         }
400
401         unsigned items_h = 0;
402         for(unsigned i=last; i<items.size(); --i)
403         {
404                 items_h += items[i]->get_geometry().h;
405                 if(items_h>view_h)
406                         return min(i+1, last);
407         }
408
409         return 0;
410 }
411
412 void List::check_view_range()
413 {
414         if(!style)
415                 return;
416
417         if(items.empty())
418                 max_scroll = 0;
419         else
420                 max_scroll = last_to_first(items.size()-1);
421
422         if(first>max_scroll)
423                 first = max_scroll;
424
425         slider.set_range(0, max_scroll);
426         slider.set_value(max_scroll-first);
427 }
428
429 void List::scroll_to_focus()
430 {
431         if(focus_index<0 || items[focus_index]->is_visible())
432                 return;
433
434         if(static_cast<unsigned>(focus_index)<first)
435                 slider.set_value(max_scroll-focus_index);
436         else
437                 slider.set_value(max_scroll-last_to_first(focus_index));
438 }
439
440 void List::slider_value_changed(double value)
441 {
442         if(max_scroll>0 && !ignore_slider_change)
443         {
444                 first = max_scroll-static_cast<unsigned>(value);
445                 rebuild();
446         }
447 }
448
449 void List::adjust_index(int &index, int pos, int change)
450 {
451         if(index>pos)
452                 index += change;
453         else if(index==pos)
454                 index = (change>0 ? index+change : -1);
455 }
456
457
458 List::DataObserver::DataObserver(List &l):
459         list(l)
460 {
461         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
462         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
463         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
464         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
465 }
466
467 void List::DataObserver::item_added(unsigned i)
468 {
469         adjust_index(list.sel_index, i, 1);
470         adjust_index(list.focus_index, i, 1);
471
472         Item *item = list.create_item(i);
473         list.items.insert(list.items.begin()+i, item);
474         list.items_changed();
475 }
476
477 void List::DataObserver::item_removed(unsigned i)
478 {
479         bool had_selection = (list.sel_index>=0);
480         adjust_index(list.sel_index, i, -1);
481         adjust_index(list.focus_index, i, -1);
482
483         delete list.items[i];
484         list.items.erase(list.items.begin()+i);
485         list.items_changed();
486
487         if(had_selection && list.sel_index<0)
488                 list.signal_selection_cleared.emit();
489 }
490
491 void List::DataObserver::cleared()
492 {
493         list.sel_index = -1;
494         list.focus_index = -1;
495         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
496                 delete *i;
497         list.items.clear();
498         list.items_changed();
499
500         list.signal_selection_cleared.emit();
501 }
502
503 void List::DataObserver::refresh_item(unsigned i)
504 {
505         delete list.items[i];
506         // Avoid stale pointer while create_item is executing
507         list.items[i] = 0;
508         list.items[i] = list.create_item(i);
509         list.items_changed();
510 }
511
512
513 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
514 {
515         if(part.get_name()=="children")
516         {
517                 const Sides &margin = part.get_margin();
518                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
519                 {
520                         Geometry cgeom;
521                         (*i)->widget->autosize(cgeom);
522                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
523                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
524                 }
525         }
526 }
527
528 void List::Item::set_active(bool a)
529 {
530         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
531 }
532
533 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
534 {
535         if(part.get_name()=="children")
536         {
537                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
538                         (*i)->widget->render(renderer);
539         }
540 }
541
542
543 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
544 {
545         if(widths.size()<children.size())
546                 widths.resize(children.size(), 0);
547
548         unsigned n = 0;
549         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
550         {
551                 Geometry cgeom;
552                 (*i)->widget->autosize(cgeom);
553                 // TODO invent a better way to specify spacings
554                 widths[n] = max(widths[n], cgeom.w+8);
555         }
556 }
557
558 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
559 {
560         if(!style)
561                 return;
562
563         const Part *part = style->get_part("children");
564         if(!part)
565                 return;
566
567         const Sides &margin = part->get_margin();
568         int x = margin.left;
569         unsigned n = 0;
570         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
571         {
572                 (*i)->widget->set_position(x, margin.bottom);
573                 x += widths[n];
574         }
575 }
576
577 void List::MultiColumnItem::on_style_change()
578 {
579         if(!style)
580                 return;
581
582         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
583                 (*i)->widget->autosize();
584
585         vector<unsigned> widths;
586         List *list = static_cast<List *>(parent);
587         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
588                 if(*i!=this)
589                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
590                                 mci->check_widths(widths);
591
592         vector<unsigned> self_widths(widths);
593         check_widths(self_widths);
594         bool update_all = false;
595         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
596                 update_all = self_widths[i]>widths[i];
597
598         if(update_all)
599         {
600                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
601                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
602                                 mci->set_widths(self_widths);
603         }
604
605         set_widths(self_widths);
606 }
607
608
609 List::BasicItem::BasicItem(const string &text):
610         label(text)
611 {
612         add(label);
613 }
614
615 void List::BasicItem::on_style_change()
616 {
617         if(!style)
618                 return;
619
620         label.autosize();
621         if(const Part *part = style->get_part("children"))
622         {
623                 const Sides &margin = part->get_margin();
624                 label.set_position(margin.left, margin.bottom);
625         }
626 }
627
628
629 List::Loader::Loader(List &l):
630         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
631 {
632         add("item", &Loader::item);
633         add("view_size", &List::view_size);
634 }
635
636 void List::Loader::item(const string &v)
637 {
638         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
639 }
640
641 } // namespace GLtk
642 } // namespace Msp