]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
6496018380ffbb12411d4a24eb26caa50c9995cd
[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
48         observer = new DataObserver(*this);
49
50         add(slider);
51         slider.set_step(1);
52         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
53 }
54
55 List::~List()
56 {
57         delete item_factory;
58         delete observer;
59         if(own_data)
60                 delete data;
61 }
62
63 void List::autosize_special(const Part &part, Geometry &ageom) const
64 {
65         if(part.get_name()=="items")
66         {
67                 const Sides &margin = part.get_margin();
68
69                 unsigned max_w = 0;
70                 unsigned total_h = 0;
71                 for(unsigned i=0; i<items.size(); ++i)
72                 {
73                         Geometry igeom;
74                         items[i]->autosize(igeom);
75                         max_w = max(max_w, igeom.w);
76                         if(view_size==0 || i<view_size)
77                                 total_h += igeom.h;
78                 }
79
80                 if(!items.empty() && items.size()<view_size)
81                         total_h = total_h*view_size/items.size();
82
83                 ageom.w = max(ageom.w, max_w+margin.left+margin.right);
84                 ageom.h = max(ageom.h, total_h+margin.top+margin.bottom);
85         }
86         else if(part.get_name()=="slider")
87                 autosize_child(slider, part, ageom);
88 }
89
90 void List::set_data(ListData &d)
91 {
92         if(item_factory)
93                 item_factory->set_data(d);
94
95         delete observer;
96         if(own_data)
97                 delete data;
98
99         data = &d;
100         own_data = false;
101         observer = new DataObserver(*this);
102
103         for(vector<Item *>::iterator i=items.begin(); i!=items.end(); ++i)
104                 delete *i;
105         items.clear();
106         unsigned n_items = data->size();
107         for(unsigned i=0; i<n_items; ++i)
108         {
109                 Item *item = create_item(i);
110                 items.push_back(item);
111         }
112
113         items_changed();
114 }
115
116 void List::items_changed()
117 {
118         signal_autosize_changed.emit();
119         rebuild();
120 }
121
122 List::Item *List::create_item(unsigned index)
123 {
124         Item *item = 0;
125         if(item_factory)
126                 item = item_factory->create_item(index);
127         else
128                 item = new BasicItem(data->get_string(index));
129         if(static_cast<int>(index)==sel_index)
130                 item->set_active(true);
131         add(*item);
132         item->autosize();
133         item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
134         return item;
135 }
136
137 void List::set_view_size(unsigned s)
138 {
139         view_size = s;
140         signal_autosize_changed.emit();
141 }
142
143 void List::set_view_all()
144 {
145         set_view_size(0);
146 }
147
148 void List::set_selected_index(int i)
149 {
150         if(i>=static_cast<int>(data->size()))
151                 throw out_of_range("List::set_selected_index");
152
153         if(i==sel_index || (i<0 && sel_index<0))
154                 return;
155
156         if(sel_index>=0)
157                 items[sel_index]->set_active(false);
158         if(i<0)
159         {
160                 sel_index = -1;
161                 focus_index = -1;
162                 set_input_focus(0);
163                 signal_selection_cleared.emit();
164         }
165         else
166         {
167                 sel_index = i;
168                 focus_index = i;
169                 items[sel_index]->set_active(true);
170                 set_input_focus(items[focus_index]);
171                 signal_item_selected.emit(sel_index);
172         }
173 }
174
175 void List::rebuild_special(const Part &part)
176 {
177         if(part.get_name()=="slider")
178                 reposition_child(slider, part);
179         else if(part.get_name()=="items")
180         {
181                 SetFlag flag(ignore_slider_change);
182                 check_view_range();
183
184                 const Sides &margin = part.get_margin();
185                 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
186                 unsigned y = geom.h-min(geom.h, margin.top);
187                 for(unsigned i=0; i<items.size(); ++i)
188                 {
189                         if(i<first || !y)
190                                 items[i]->set_visible(false);
191                         else
192                         {
193                                 Geometry igeom = items[i]->get_geometry();
194                                 if(igeom.h+margin.bottom<=y)
195                                 {
196                                         items[i]->set_visible(true);
197                                         y -= igeom.h;
198                                         igeom.x = margin.left;
199                                         igeom.y = y;
200                                         igeom.w = w;
201                                         items[i]->set_geometry(igeom);
202                                 }
203                                 else
204                                 {
205                                         items[i]->set_visible(false);
206                                         y = 0;
207                                 }
208                         }
209                 }
210         }
211
212         Widget::rebuild_special(part);
213 }
214
215 void List::render_special(const Part &part, GL::Renderer &renderer) const
216 {
217         if(part.get_name()=="items")
218         {
219                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
220                         items[i]->render(renderer);
221         }
222         else if(part.get_name()=="slider")
223                 slider.render(renderer);
224 }
225
226 void List::button_press(int x, int y, unsigned btn)
227 {
228         if(btn==4 || btn==5)
229         {
230                 unsigned change = 3;
231                 if(btn==4)
232                 {
233                         change = min(first, change);
234                         slider.set_value(max_scroll-(first-change));
235                 }
236                 else if(btn==5)
237                 {
238                         change = min(max_scroll-first, change);
239                         slider.set_value(max_scroll-(first+change));
240                 }
241         }
242         else
243         {
244                 Container::button_press(x, y, btn);
245                 if(click_focus && btn==1)
246                 {
247                         for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
248                                 if(click_focus==items[i])
249                                 {
250                                         set_selected_index(i);
251                                         break;
252                                 }
253                 }
254         }
255 }
256
257 void List::focus_in()
258 {
259         if(focus_index<0)
260                 focus_index = sel_index;
261
262         if(focus_index>=0)
263         {
264                 scroll_to_focus();
265                 set_input_focus(items[focus_index]);
266         }
267 }
268
269 bool List::navigate(Navigation nav)
270 {
271         if(nav==NAV_UP)
272         {
273                 if(focus_index<0 && !items.empty())
274                         focus_index = items.size()-1;
275                 else if(focus_index>0)
276                         --focus_index;
277                 else
278                         return false;
279
280                 scroll_to_focus();
281                 set_input_focus(items[focus_index]);
282         }
283         else if(nav==NAV_DOWN)
284         {
285                 if(focus_index<0 && !items.empty())
286                         focus_index = 0;
287                 else if(static_cast<unsigned>(focus_index+1)<items.size())
288                         ++focus_index;
289                 else
290                         return false;
291
292                 scroll_to_focus();
293                 set_input_focus(items[focus_index]);
294         }
295         else if(nav==NAV_ACTIVATE)
296                 set_selected_index(focus_index);
297         else
298                 return false;
299
300         return true;
301 }
302
303 void List::item_autosize_changed(Item *item)
304 {
305         item->autosize();
306         signal_autosize_changed.emit();
307         rebuild();
308 }
309
310 unsigned List::last_to_first(unsigned last) const
311 {
312         if(!style)
313                 return last;
314
315         unsigned view_h = geom.h;
316         if(const Part *items_part = style->get_part("items"))
317         {
318                 const Sides &margin = items_part->get_margin();
319                 view_h -= margin.top+margin.bottom;
320         }
321
322         unsigned items_h = 0;
323         for(unsigned i=last; i<items.size(); --i)
324         {
325                 items_h += items[i]->get_geometry().h;
326                 if(items_h>view_h)
327                         return min(i+1, last);
328         }
329
330         return 0;
331 }
332
333 void List::check_view_range()
334 {
335         if(!style)
336                 return;
337
338         if(items.empty())
339                 max_scroll = 0;
340         else
341                 max_scroll = last_to_first(items.size()-1);
342
343         if(first>max_scroll)
344                 first = max_scroll;
345
346         slider.set_range(0, max_scroll);
347         slider.set_value(max_scroll-first);
348 }
349
350 void List::scroll_to_focus()
351 {
352         if(focus_index<0 || items[focus_index]->is_visible())
353                 return;
354
355         if(static_cast<unsigned>(focus_index)<first)
356                 slider.set_value(max_scroll-focus_index);
357         else
358                 slider.set_value(max_scroll-last_to_first(focus_index));
359 }
360
361 void List::slider_value_changed(double value)
362 {
363         if(max_scroll>0 && !ignore_slider_change)
364         {
365                 first = max_scroll-static_cast<unsigned>(value);
366                 rebuild();
367         }
368 }
369
370 void List::adjust_index(int &index, int pos, int change)
371 {
372         if(index>pos)
373                 index += change;
374         else if(index==pos)
375                 index = (change>0 ? index+change : -1);
376 }
377
378
379 List::DataObserver::DataObserver(List &l):
380         list(l)
381 {
382         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
383         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
384         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
385         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
386 }
387
388 void List::DataObserver::item_added(unsigned i)
389 {
390         adjust_index(list.sel_index, i, 1);
391         adjust_index(list.focus_index, i, 1);
392
393         Item *item = list.create_item(i);
394         list.items.insert(list.items.begin()+i, item);
395         list.items_changed();
396 }
397
398 void List::DataObserver::item_removed(unsigned i)
399 {
400         bool had_selection = (list.sel_index>=0);
401         adjust_index(list.sel_index, i, -1);
402         adjust_index(list.focus_index, i, -1);
403
404         delete list.items[i];
405         list.items.erase(list.items.begin()+i);
406         list.items_changed();
407
408         if(had_selection && list.sel_index<0)
409                 list.signal_selection_cleared.emit();
410 }
411
412 void List::DataObserver::cleared()
413 {
414         list.sel_index = -1;
415         list.focus_index = -1;
416         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
417                 delete *i;
418         list.items.clear();
419         list.items_changed();
420
421         list.signal_selection_cleared.emit();
422 }
423
424 void List::DataObserver::refresh_item(unsigned i)
425 {
426         delete list.items[i];
427         // Avoid stale pointer while create_item is executing
428         list.items[i] = 0;
429         list.items[i] = list.create_item(i);
430         list.items_changed();
431 }
432
433
434 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
435 {
436         if(part.get_name()=="children")
437         {
438                 const Sides &margin = part.get_margin();
439                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
440                 {
441                         Geometry cgeom;
442                         (*i)->widget->autosize(cgeom);
443                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
444                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
445                 }
446         }
447 }
448
449 void List::Item::set_active(bool a)
450 {
451         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
452 }
453
454 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
455 {
456         if(part.get_name()=="children")
457         {
458                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
459                         (*i)->widget->render(renderer);
460         }
461 }
462
463
464 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
465 {
466         if(widths.size()<children.size())
467                 widths.resize(children.size(), 0);
468
469         unsigned n = 0;
470         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
471         {
472                 Geometry cgeom;
473                 (*i)->widget->autosize(cgeom);
474                 // TODO invent a better way to specify spacings
475                 widths[n] = max(widths[n], cgeom.w+8);
476         }
477 }
478
479 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
480 {
481         if(!style)
482                 return;
483
484         const Part *part = style->get_part("children");
485         if(!part)
486                 return;
487
488         const Sides &margin = part->get_margin();
489         int x = margin.left;
490         unsigned n = 0;
491         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
492         {
493                 (*i)->widget->set_position(x, margin.bottom);
494                 x += widths[n];
495         }
496 }
497
498 void List::MultiColumnItem::on_style_change()
499 {
500         if(!style)
501                 return;
502
503         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
504                 (*i)->widget->autosize();
505
506         vector<unsigned> widths;
507         List *list = static_cast<List *>(parent);
508         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
509                 if(*i!=this)
510                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
511                                 mci->check_widths(widths);
512
513         vector<unsigned> self_widths(widths);
514         check_widths(self_widths);
515         bool update_all = false;
516         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
517                 update_all = self_widths[i]>widths[i];
518
519         if(update_all)
520         {
521                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
522                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
523                                 mci->set_widths(self_widths);
524         }
525
526         set_widths(self_widths);
527 }
528
529
530 List::BasicItem::BasicItem(const string &text):
531         label(text)
532 {
533         add(label);
534 }
535
536 void List::BasicItem::on_style_change()
537 {
538         if(!style)
539                 return;
540
541         label.autosize();
542         if(const Part *part = style->get_part("children"))
543         {
544                 const Sides &margin = part->get_margin();
545                 label.set_position(margin.left, margin.bottom);
546         }
547 }
548
549
550 List::Loader::Loader(List &l):
551         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
552 {
553         add("item", &Loader::item);
554         add("view_size", &List::view_size);
555 }
556
557 void List::Loader::item(const string &v)
558 {
559         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
560 }
561
562 } // namespace GLtk
563 } // namespace Msp