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