]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Improve list focus management
[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                 if(state&FOCUS)
171                         set_input_focus(items[focus_index]);
172                 signal_item_selected.emit(sel_index);
173         }
174 }
175
176 void List::rebuild_special(const Part &part)
177 {
178         if(part.get_name()=="slider")
179                 reposition_child(slider, part);
180         else if(part.get_name()=="items")
181         {
182                 SetFlag flag(ignore_slider_change);
183                 check_view_range();
184
185                 const Sides &margin = part.get_margin();
186                 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
187                 unsigned y = geom.h-min(geom.h, margin.top);
188                 for(unsigned i=0; i<items.size(); ++i)
189                 {
190                         if(i<first || !y)
191                                 items[i]->set_visible(false);
192                         else
193                         {
194                                 Geometry igeom = items[i]->get_geometry();
195                                 if(igeom.h+margin.bottom<=y)
196                                 {
197                                         items[i]->set_visible(true);
198                                         y -= igeom.h;
199                                         igeom.x = margin.left;
200                                         igeom.y = y;
201                                         igeom.w = w;
202                                         items[i]->set_geometry(igeom);
203                                 }
204                                 else
205                                 {
206                                         items[i]->set_visible(false);
207                                         y = 0;
208                                 }
209                         }
210                 }
211         }
212
213         Widget::rebuild_special(part);
214 }
215
216 void List::render_special(const Part &part, GL::Renderer &renderer) const
217 {
218         if(part.get_name()=="items")
219         {
220                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
221                         items[i]->render(renderer);
222         }
223         else if(part.get_name()=="slider")
224                 slider.render(renderer);
225 }
226
227 void List::button_press(int x, int y, unsigned btn)
228 {
229         if(btn==4 || btn==5)
230         {
231                 unsigned change = 3;
232                 if(btn==4)
233                 {
234                         change = min(first, change);
235                         slider.set_value(max_scroll-(first-change));
236                 }
237                 else if(btn==5)
238                 {
239                         change = min(max_scroll-first, change);
240                         slider.set_value(max_scroll-(first+change));
241                 }
242         }
243         else
244         {
245                 Container::button_press(x, y, btn);
246                 if(click_focus && btn==1)
247                 {
248                         for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
249                                 if(click_focus==items[i])
250                                 {
251                                         set_selected_index(i);
252                                         break;
253                                 }
254                 }
255         }
256 }
257
258 void List::focus_in()
259 {
260         if(focus_index>=0 && items[focus_index]->is_visible())
261                 set_input_focus(items[focus_index]);
262         else
263         {
264                 if(sel_index>=0 && items[sel_index]->is_visible())
265                         set_focus_index(sel_index);
266                 else if(!items.empty())
267                         set_focus_index(first);
268         }
269 }
270
271 bool List::navigate(Navigation nav)
272 {
273         if(nav==NAV_UP && !items.empty())
274         {
275                 if(focus_index>0)
276                         set_focus_index(focus_index-1);
277         }
278         else if(nav==NAV_DOWN && !items.empty())
279         {
280                 if(static_cast<unsigned>(focus_index+1)<items.size())
281                         set_focus_index(focus_index+1);
282         }
283         else if(nav==NAV_ACTIVATE)
284                 set_selected_index(focus_index);
285         else
286                 return false;
287
288         return true;
289 }
290
291 void List::set_focus_index(int i)
292 {
293         focus_index = i;
294         if(focus_index>=0)
295         {
296                 scroll_to_focus();
297                 if(state&FOCUS)
298                         set_input_focus(items[focus_index]);
299         }
300 }
301
302 void List::item_autosize_changed(Item *item)
303 {
304         item->autosize();
305         signal_autosize_changed.emit();
306         rebuild();
307 }
308
309 unsigned List::last_to_first(unsigned last) const
310 {
311         if(!style)
312                 return last;
313
314         unsigned view_h = geom.h;
315         if(const Part *items_part = style->get_part("items"))
316         {
317                 const Sides &margin = items_part->get_margin();
318                 view_h -= margin.top+margin.bottom;
319         }
320
321         unsigned items_h = 0;
322         for(unsigned i=last; i<items.size(); --i)
323         {
324                 items_h += items[i]->get_geometry().h;
325                 if(items_h>view_h)
326                         return min(i+1, last);
327         }
328
329         return 0;
330 }
331
332 void List::check_view_range()
333 {
334         if(!style)
335                 return;
336
337         if(items.empty())
338                 max_scroll = 0;
339         else
340                 max_scroll = last_to_first(items.size()-1);
341
342         if(first>max_scroll)
343                 first = max_scroll;
344
345         slider.set_range(0, max_scroll);
346         slider.set_value(max_scroll-first);
347 }
348
349 void List::scroll_to_focus()
350 {
351         if(focus_index<0 || items[focus_index]->is_visible())
352                 return;
353
354         if(static_cast<unsigned>(focus_index)<first)
355                 slider.set_value(max_scroll-focus_index);
356         else
357                 slider.set_value(max_scroll-last_to_first(focus_index));
358 }
359
360 void List::slider_value_changed(double value)
361 {
362         if(max_scroll>0 && !ignore_slider_change)
363         {
364                 first = max_scroll-static_cast<unsigned>(value);
365                 rebuild();
366         }
367 }
368
369 void List::adjust_index(int &index, int pos, int change)
370 {
371         if(index>pos)
372                 index += change;
373         else if(index==pos)
374                 index = (change>0 ? index+change : -1);
375 }
376
377
378 List::DataObserver::DataObserver(List &l):
379         list(l)
380 {
381         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
382         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
383         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
384         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
385 }
386
387 void List::DataObserver::item_added(unsigned i)
388 {
389         adjust_index(list.sel_index, i, 1);
390         adjust_index(list.focus_index, i, 1);
391
392         Item *item = list.create_item(i);
393         list.items.insert(list.items.begin()+i, item);
394         list.items_changed();
395 }
396
397 void List::DataObserver::item_removed(unsigned i)
398 {
399         bool had_selection = (list.sel_index>=0);
400         adjust_index(list.sel_index, i, -1);
401         adjust_index(list.focus_index, i, -1);
402
403         delete list.items[i];
404         list.items.erase(list.items.begin()+i);
405         list.items_changed();
406
407         if(had_selection && list.sel_index<0)
408                 list.signal_selection_cleared.emit();
409 }
410
411 void List::DataObserver::cleared()
412 {
413         list.sel_index = -1;
414         list.focus_index = -1;
415         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
416                 delete *i;
417         list.items.clear();
418         list.items_changed();
419
420         list.signal_selection_cleared.emit();
421 }
422
423 void List::DataObserver::refresh_item(unsigned i)
424 {
425         delete list.items[i];
426         // Avoid stale pointer while create_item is executing
427         list.items[i] = 0;
428         list.items[i] = list.create_item(i);
429         list.items_changed();
430 }
431
432
433 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
434 {
435         if(part.get_name()=="children")
436         {
437                 const Sides &margin = part.get_margin();
438                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
439                 {
440                         Geometry cgeom;
441                         (*i)->widget->autosize(cgeom);
442                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
443                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
444                 }
445         }
446 }
447
448 void List::Item::set_active(bool a)
449 {
450         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
451 }
452
453 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
454 {
455         if(part.get_name()=="children")
456         {
457                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
458                         (*i)->widget->render(renderer);
459         }
460 }
461
462
463 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
464 {
465         if(widths.size()<children.size())
466                 widths.resize(children.size(), 0);
467
468         unsigned n = 0;
469         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
470         {
471                 Geometry cgeom;
472                 (*i)->widget->autosize(cgeom);
473                 // TODO invent a better way to specify spacings
474                 widths[n] = max(widths[n], cgeom.w+8);
475         }
476 }
477
478 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
479 {
480         if(!style)
481                 return;
482
483         const Part *part = style->get_part("children");
484         if(!part)
485                 return;
486
487         const Sides &margin = part->get_margin();
488         int x = margin.left;
489         unsigned n = 0;
490         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
491         {
492                 (*i)->widget->set_position(x, margin.bottom);
493                 x += widths[n];
494         }
495 }
496
497 void List::MultiColumnItem::on_style_change()
498 {
499         if(!style)
500                 return;
501
502         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
503                 (*i)->widget->autosize();
504
505         vector<unsigned> widths;
506         List *list = static_cast<List *>(parent);
507         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
508                 if(*i!=this)
509                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
510                                 mci->check_widths(widths);
511
512         vector<unsigned> self_widths(widths);
513         check_widths(self_widths);
514         bool update_all = false;
515         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
516                 update_all = self_widths[i]>widths[i];
517
518         if(update_all)
519         {
520                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
521                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
522                                 mci->set_widths(self_widths);
523         }
524
525         set_widths(self_widths);
526 }
527
528
529 List::BasicItem::BasicItem(const string &text):
530         label(text)
531 {
532         add(label);
533 }
534
535 void List::BasicItem::on_style_change()
536 {
537         if(!style)
538                 return;
539
540         label.autosize();
541         if(const Part *part = style->get_part("children"))
542         {
543                 const Sides &margin = part->get_margin();
544                 label.set_position(margin.left, margin.bottom);
545         }
546 }
547
548
549 List::Loader::Loader(List &l):
550         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
551 {
552         add("item", &Loader::item);
553         add("view_size", &List::view_size);
554 }
555
556 void List::Loader::item(const string &v)
557 {
558         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
559 }
560
561 } // namespace GLtk
562 } // namespace Msp