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