]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
5d765008765c9ccbece8c28e77c9a62da5a4cecb
[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         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_size==0 ? items.size() : view_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 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, change);
257                         slider.set_value(max_scroll-(first-change));
258                 }
259                 else if(btn==5)
260                 {
261                         change = min(max_scroll-first, change);
262                         slider.set_value(max_scroll-(first+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<max_scroll)
307                 {
308                         int item_h = items[first]->get_geometry().h;
309                         if(dy>item_h)
310                         {
311                                 drag_start_y += item_h;
312                                 slider.set_value(max_scroll-(first+1));
313                         }
314                 }
315                 else if(dy<0 && first>0)
316                 {
317                         int item_h = items[first-1]->get_geometry().h;
318                         if(-dy>item_h)
319                         {
320                                 drag_start_y -= item_h;
321                                 slider.set_value(max_scroll-(first-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(first);
338         }
339 }
340
341 bool List::navigate(Navigation nav)
342 {
343         if((nav==NAV_UP || nav==NAV_DOWN) && !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)
361         {
362                 if(focus_index>0)
363                         set_focus_index(focus_index-1);
364         }
365         else if(nav==NAV_DOWN)
366         {
367                 if(static_cast<unsigned>(focus_index+1)<items.size())
368                         set_focus_index(focus_index+1);
369         }
370
371         if(select)
372                 set_selected_index(focus_index);
373 }
374
375 void List::set_focus_index(int i)
376 {
377         focus_index = i;
378         if(focus_index>=0)
379         {
380                 scroll_to_focus();
381                 if(state&FOCUS)
382                         set_input_focus(items[focus_index]);
383         }
384 }
385
386 void List::item_autosize_changed(Item *item)
387 {
388         item->autosize();
389         signal_autosize_changed.emit();
390         rebuild();
391 }
392
393 unsigned List::last_to_first(unsigned last) const
394 {
395         if(!items_part)
396                 return last;
397
398         const Sides &margin = items_part->get_margin();
399         unsigned view_h = geom.h-min(geom.h, margin.top+margin.bottom);
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::SimpleItem::on_style_change()
544 {
545         if(!style || children.empty())
546                 return;
547
548         Widget *child = children.front()->widget;
549         child->autosize();
550         if(const Part *part = style->get_part("children"))
551         {
552                 const Sides &margin = part->get_margin();
553                 child->set_position(margin.left, margin.bottom);
554         }
555 }
556
557
558 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
559 {
560         if(widths.size()<children.size())
561                 widths.resize(children.size(), 0);
562
563         unsigned n = 0;
564         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
565         {
566                 Geometry cgeom;
567                 (*i)->widget->autosize(cgeom);
568                 // TODO invent a better way to specify spacings
569                 widths[n] = max(widths[n], cgeom.w+8);
570         }
571 }
572
573 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
574 {
575         if(!style)
576                 return;
577
578         const Part *part = style->get_part("children");
579         if(!part)
580                 return;
581
582         const Sides &margin = part->get_margin();
583         int x = margin.left;
584         unsigned n = 0;
585         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
586         {
587                 (*i)->widget->set_position(x, margin.bottom);
588                 x += widths[n];
589         }
590 }
591
592 void List::MultiColumnItem::on_style_change()
593 {
594         if(!style)
595                 return;
596
597         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
598                 (*i)->widget->autosize();
599
600         vector<unsigned> widths;
601         List *list = static_cast<List *>(parent);
602         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
603                 if(*i!=this)
604                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
605                                 mci->check_widths(widths);
606
607         vector<unsigned> self_widths(widths);
608         check_widths(self_widths);
609         bool update_all = false;
610         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
611                 update_all = self_widths[i]>widths[i];
612
613         if(update_all)
614         {
615                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
616                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
617                                 mci->set_widths(self_widths);
618         }
619
620         set_widths(self_widths);
621 }
622
623
624 List::BasicItem::BasicItem(const string &text):
625         label(text)
626 {
627         add(label);
628 }
629
630
631 List::Loader::Loader(List &l):
632         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
633 {
634         add("item", &Loader::item);
635         add("view_size", &List::view_size);
636 }
637
638 void List::Loader::item(const string &v)
639 {
640         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
641 }
642
643 } // namespace GLtk
644 } // namespace Msp