]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Pass List::focus_in to base 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 "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         Container::focus_in();
318         if(focus_index>=0 && items[focus_index]->is_visible())
319                 set_input_focus(items[focus_index]);
320         else
321         {
322                 if(sel_index>=0 && items[sel_index]->is_visible())
323                         set_focus_index(sel_index);
324                 else if(!items.empty())
325                         set_focus_index(first);
326         }
327 }
328
329 bool List::navigate(Navigation nav)
330 {
331         if(nav==NAV_UP && !items.empty())
332         {
333                 if(focus_index>0)
334                         set_focus_index(focus_index-1);
335         }
336         else if(nav==NAV_DOWN && !items.empty())
337         {
338                 if(static_cast<unsigned>(focus_index+1)<items.size())
339                         set_focus_index(focus_index+1);
340         }
341         else if(nav==NAV_ACTIVATE)
342                 set_selected_index(focus_index);
343         else
344                 return false;
345
346         return true;
347 }
348
349 void List::set_focus_index(int i)
350 {
351         focus_index = i;
352         if(focus_index>=0)
353         {
354                 scroll_to_focus();
355                 if(state&FOCUS)
356                         set_input_focus(items[focus_index]);
357         }
358 }
359
360 void List::item_autosize_changed(Item *item)
361 {
362         item->autosize();
363         signal_autosize_changed.emit();
364         rebuild();
365 }
366
367 unsigned List::last_to_first(unsigned last) const
368 {
369         if(!style)
370                 return last;
371
372         unsigned view_h = geom.h;
373         if(const Part *items_part = style->get_part("items"))
374         {
375                 const Sides &margin = items_part->get_margin();
376                 view_h -= margin.top+margin.bottom;
377         }
378
379         unsigned items_h = 0;
380         for(unsigned i=last; i<items.size(); --i)
381         {
382                 items_h += items[i]->get_geometry().h;
383                 if(items_h>view_h)
384                         return min(i+1, last);
385         }
386
387         return 0;
388 }
389
390 void List::check_view_range()
391 {
392         if(!style)
393                 return;
394
395         if(items.empty())
396                 max_scroll = 0;
397         else
398                 max_scroll = last_to_first(items.size()-1);
399
400         if(first>max_scroll)
401                 first = max_scroll;
402
403         slider.set_range(0, max_scroll);
404         slider.set_value(max_scroll-first);
405 }
406
407 void List::scroll_to_focus()
408 {
409         if(focus_index<0 || items[focus_index]->is_visible())
410                 return;
411
412         if(static_cast<unsigned>(focus_index)<first)
413                 slider.set_value(max_scroll-focus_index);
414         else
415                 slider.set_value(max_scroll-last_to_first(focus_index));
416 }
417
418 void List::slider_value_changed(double value)
419 {
420         if(max_scroll>0 && !ignore_slider_change)
421         {
422                 first = max_scroll-static_cast<unsigned>(value);
423                 rebuild();
424         }
425 }
426
427 void List::adjust_index(int &index, int pos, int change)
428 {
429         if(index>pos)
430                 index += change;
431         else if(index==pos)
432                 index = (change>0 ? index+change : -1);
433 }
434
435
436 List::DataObserver::DataObserver(List &l):
437         list(l)
438 {
439         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
440         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
441         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
442         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
443 }
444
445 void List::DataObserver::item_added(unsigned i)
446 {
447         adjust_index(list.sel_index, i, 1);
448         adjust_index(list.focus_index, i, 1);
449
450         Item *item = list.create_item(i);
451         list.items.insert(list.items.begin()+i, item);
452         list.items_changed();
453 }
454
455 void List::DataObserver::item_removed(unsigned i)
456 {
457         bool had_selection = (list.sel_index>=0);
458         adjust_index(list.sel_index, i, -1);
459         adjust_index(list.focus_index, i, -1);
460
461         delete list.items[i];
462         list.items.erase(list.items.begin()+i);
463         list.items_changed();
464
465         if(had_selection && list.sel_index<0)
466                 list.signal_selection_cleared.emit();
467 }
468
469 void List::DataObserver::cleared()
470 {
471         list.sel_index = -1;
472         list.focus_index = -1;
473         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
474                 delete *i;
475         list.items.clear();
476         list.items_changed();
477
478         list.signal_selection_cleared.emit();
479 }
480
481 void List::DataObserver::refresh_item(unsigned i)
482 {
483         delete list.items[i];
484         // Avoid stale pointer while create_item is executing
485         list.items[i] = 0;
486         list.items[i] = list.create_item(i);
487         list.items_changed();
488 }
489
490
491 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
492 {
493         if(part.get_name()=="children")
494         {
495                 const Sides &margin = part.get_margin();
496                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
497                 {
498                         Geometry cgeom;
499                         (*i)->widget->autosize(cgeom);
500                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
501                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
502                 }
503         }
504 }
505
506 void List::Item::set_active(bool a)
507 {
508         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
509 }
510
511 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
512 {
513         if(part.get_name()=="children")
514         {
515                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
516                         (*i)->widget->render(renderer);
517         }
518 }
519
520
521 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
522 {
523         if(widths.size()<children.size())
524                 widths.resize(children.size(), 0);
525
526         unsigned n = 0;
527         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
528         {
529                 Geometry cgeom;
530                 (*i)->widget->autosize(cgeom);
531                 // TODO invent a better way to specify spacings
532                 widths[n] = max(widths[n], cgeom.w+8);
533         }
534 }
535
536 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
537 {
538         if(!style)
539                 return;
540
541         const Part *part = style->get_part("children");
542         if(!part)
543                 return;
544
545         const Sides &margin = part->get_margin();
546         int x = margin.left;
547         unsigned n = 0;
548         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
549         {
550                 (*i)->widget->set_position(x, margin.bottom);
551                 x += widths[n];
552         }
553 }
554
555 void List::MultiColumnItem::on_style_change()
556 {
557         if(!style)
558                 return;
559
560         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
561                 (*i)->widget->autosize();
562
563         vector<unsigned> widths;
564         List *list = static_cast<List *>(parent);
565         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
566                 if(*i!=this)
567                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
568                                 mci->check_widths(widths);
569
570         vector<unsigned> self_widths(widths);
571         check_widths(self_widths);
572         bool update_all = false;
573         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
574                 update_all = self_widths[i]>widths[i];
575
576         if(update_all)
577         {
578                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
579                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
580                                 mci->set_widths(self_widths);
581         }
582
583         set_widths(self_widths);
584 }
585
586
587 List::BasicItem::BasicItem(const string &text):
588         label(text)
589 {
590         add(label);
591 }
592
593 void List::BasicItem::on_style_change()
594 {
595         if(!style)
596                 return;
597
598         label.autosize();
599         if(const Part *part = style->get_part("children"))
600         {
601                 const Sides &margin = part->get_margin();
602                 label.set_position(margin.left, margin.bottom);
603         }
604 }
605
606
607 List::Loader::Loader(List &l):
608         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
609 {
610         add("item", &Loader::item);
611         add("view_size", &List::view_size);
612 }
613
614 void List::Loader::item(const string &v)
615 {
616         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
617 }
618
619 } // namespace GLtk
620 } // namespace Msp