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