]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
83af9320aec7e6c093b8f31bb2e58bd0b9b12498
[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         add(*item);
127         item->autosize();
128         item->signal_autosize_changed.connect(sigc::bind(sigc::mem_fun(this, &List::item_autosize_changed), item));
129         return item;
130 }
131
132 void List::set_view_size(unsigned s)
133 {
134         view_size = s;
135         signal_autosize_changed.emit();
136 }
137
138 void List::set_view_all()
139 {
140         set_view_size(0);
141 }
142
143 void List::set_selected_index(int i)
144 {
145         if(i>=static_cast<int>(data->size()))
146                 throw out_of_range("List::set_selected_index");
147
148         if(i==sel_index)
149                 return;
150
151         if(sel_index>=0)
152                 items[sel_index]->set_active(false);
153         if(i<0)
154                 sel_index = -1;
155         else
156         {
157                 sel_index = i;
158                 items[sel_index]->set_active(true);
159                 signal_item_selected.emit(sel_index);
160         }
161 }
162
163 void List::rebuild_special(const Part &part)
164 {
165         if(part.get_name()=="slider")
166                 reposition_child(slider, part);
167         else if(part.get_name()=="items")
168         {
169                 SetFlag flag(ignore_slider_change);
170                 check_view_range();
171
172                 const Sides &margin = part.get_margin();
173                 unsigned w = geom.w-min(geom.w, margin.left+margin.right);
174                 unsigned y = geom.h-min(geom.h, margin.top);
175                 for(unsigned i=0; i<items.size(); ++i)
176                 {
177                         if(i<first || !y)
178                                 items[i]->set_visible(false);
179                         else
180                         {
181                                 Geometry igeom = items[i]->get_geometry();
182                                 if(igeom.h+margin.bottom<=y)
183                                 {
184                                         items[i]->set_visible(true);
185                                         y -= igeom.h;
186                                         igeom.x = margin.left;
187                                         igeom.y = y;
188                                         igeom.w = w;
189                                         items[i]->set_geometry(igeom);
190                                 }
191                                 else
192                                 {
193                                         items[i]->set_visible(false);
194                                         y = 0;
195                                 }
196                         }
197                 }
198         }
199
200         Widget::rebuild_special(part);
201 }
202
203 void List::render_special(const Part &part, GL::Renderer &renderer) const
204 {
205         if(part.get_name()=="items")
206         {
207                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
208                         items[i]->render(renderer);
209         }
210         else if(part.get_name()=="slider")
211                 slider.render(renderer);
212 }
213
214 void List::button_press(int x, int y, unsigned btn)
215 {
216         Container::button_press(x, y, btn);
217         if(click_focus && btn==1)
218         {
219                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
220                         if(click_focus==items[i])
221                         {
222                                 set_selected_index(i);
223                                 break;
224                         }
225         }
226 }
227
228 void List::item_autosize_changed(Item *item)
229 {
230         item->autosize();
231         signal_autosize_changed.emit();
232         rebuild();
233 }
234
235 void List::check_view_range()
236 {
237         if(!style)
238                 return;
239
240         unsigned h = geom.h;
241         if(const Part *items_part = style->get_part("items"))
242         {
243                 const Sides &margin = items_part->get_margin();
244                 h -= margin.top+margin.bottom;
245         }
246
247         max_scroll = items.size();
248         for(unsigned i=items.size(); i-->0; )
249         {
250                 unsigned ih = items[i]->get_geometry().h;
251                 if(ih<=h)
252                 {
253                         h -= ih;
254                         --max_scroll;
255                 }
256         }
257
258         if(first>max_scroll)
259                 first = max_scroll;
260
261         slider.set_range(0, max_scroll);
262         slider.set_value(max_scroll-first);
263 }
264
265 void List::slider_value_changed(double value)
266 {
267         if(max_scroll>0 && !ignore_slider_change)
268         {
269                 first = max_scroll-static_cast<unsigned>(value);
270                 rebuild();
271         }
272 }
273
274
275 List::DataObserver::DataObserver(List &l):
276         list(l)
277 {
278         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
279         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
280         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
281         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
282 }
283
284 void List::DataObserver::item_added(unsigned i)
285 {
286         if(list.sel_index>=static_cast<int>(i))
287                 ++list.sel_index;
288
289         Item *item = list.create_item(i);
290         list.items.insert(list.items.begin()+i, item);
291         list.items_changed();
292 }
293
294 void List::DataObserver::item_removed(unsigned i)
295 {
296         if(list.sel_index>static_cast<int>(i))
297                 --list.sel_index;
298         else if(list.sel_index==static_cast<int>(i))
299                 list.sel_index = -1;
300
301         delete list.items[i];
302         list.items.erase(list.items.begin()+i);
303         list.items_changed();
304 }
305
306 void List::DataObserver::cleared()
307 {
308         list.sel_index = -1;
309         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
310                 delete *i;
311         list.items.clear();
312         list.items_changed();
313 }
314
315 void List::DataObserver::refresh_item(unsigned i)
316 {
317         delete list.items[i];
318         // Avoid stale pointer while create_item is executing
319         list.items[i] = 0;
320         list.items[i] = list.create_item(i);
321         list.items_changed();
322 }
323
324
325 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
326 {
327         if(part.get_name()=="children")
328         {
329                 const Sides &margin = part.get_margin();
330                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
331                 {
332                         Geometry cgeom;
333                         (*i)->widget->autosize(cgeom);
334                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
335                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
336                 }
337         }
338 }
339
340 void List::Item::set_active(bool a)
341 {
342         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
343 }
344
345 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
346 {
347         if(part.get_name()=="children")
348         {
349                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
350                         (*i)->widget->render(renderer);
351         }
352 }
353
354
355 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
356 {
357         if(widths.size()<children.size())
358                 widths.resize(children.size(), 0);
359
360         unsigned n = 0;
361         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
362         {
363                 Geometry cgeom;
364                 (*i)->widget->autosize(cgeom);
365                 // TODO invent a better way to specify spacings
366                 widths[n] = max(widths[n], cgeom.w+8);
367         }
368 }
369
370 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
371 {
372         if(!style)
373                 return;
374
375         const Part *part = style->get_part("children");
376         if(!part)
377                 return;
378
379         const Sides &margin = part->get_margin();
380         int x = margin.left;
381         unsigned n = 0;
382         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
383         {
384                 (*i)->widget->set_position(x, margin.bottom);
385                 x += widths[n];
386         }
387 }
388
389 void List::MultiColumnItem::on_style_change()
390 {
391         if(!style)
392                 return;
393
394         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
395                 (*i)->widget->autosize();
396
397         vector<unsigned> widths;
398         List *list = static_cast<List *>(parent);
399         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
400                 if(*i!=this)
401                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
402                                 mci->check_widths(widths);
403
404         vector<unsigned> self_widths(widths);
405         check_widths(self_widths);
406         bool update_all = false;
407         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
408                 update_all = self_widths[i]>widths[i];
409
410         if(update_all)
411         {
412                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
413                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
414                                 mci->set_widths(self_widths);
415         }
416
417         set_widths(self_widths);
418 }
419
420
421 List::BasicItem::BasicItem(const string &text):
422         label(text)
423 {
424         add(label);
425 }
426
427 void List::BasicItem::on_style_change()
428 {
429         if(!style)
430                 return;
431
432         label.autosize();
433         if(const Part *part = style->get_part("children"))
434         {
435                 const Sides &margin = part->get_margin();
436                 label.set_position(margin.left, margin.bottom);
437         }
438 }
439
440
441 List::Loader::Loader(List &l):
442         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
443 {
444         add("item", &Loader::item);
445         add("view_size", &List::view_size);
446 }
447
448 void List::Loader::item(const string &v)
449 {
450         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
451 }
452
453 } // namespace GLtk
454 } // namespace Msp