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