]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Add a selection_cleared signal to List
[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 void List::check_view_range()
258 {
259         if(!style)
260                 return;
261
262         unsigned h = geom.h;
263         if(const Part *items_part = style->get_part("items"))
264         {
265                 const Sides &margin = items_part->get_margin();
266                 h -= margin.top+margin.bottom;
267         }
268
269         max_scroll = items.size();
270         for(unsigned i=items.size(); i-->0; )
271         {
272                 unsigned ih = items[i]->get_geometry().h;
273                 if(ih<=h)
274                 {
275                         h -= ih;
276                         --max_scroll;
277                 }
278         }
279
280         if(first>max_scroll)
281                 first = max_scroll;
282
283         slider.set_range(0, max_scroll);
284         slider.set_value(max_scroll-first);
285 }
286
287 void List::slider_value_changed(double value)
288 {
289         if(max_scroll>0 && !ignore_slider_change)
290         {
291                 first = max_scroll-static_cast<unsigned>(value);
292                 rebuild();
293         }
294 }
295
296
297 List::DataObserver::DataObserver(List &l):
298         list(l)
299 {
300         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
301         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
302         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
303         list.data->signal_refresh_item.connect(sigc::mem_fun(this, &DataObserver::refresh_item));
304 }
305
306 void List::DataObserver::item_added(unsigned i)
307 {
308         if(list.sel_index>=static_cast<int>(i))
309                 ++list.sel_index;
310
311         Item *item = list.create_item(i);
312         list.items.insert(list.items.begin()+i, item);
313         list.items_changed();
314 }
315
316 void List::DataObserver::item_removed(unsigned i)
317 {
318         bool had_selection = (list.sel_index>=0);
319         if(list.sel_index>static_cast<int>(i))
320                 --list.sel_index;
321         else if(list.sel_index==static_cast<int>(i))
322                 list.sel_index = -1;
323
324         delete list.items[i];
325         list.items.erase(list.items.begin()+i);
326         list.items_changed();
327
328         if(had_selection && list.sel_index<0)
329                 list.signal_selection_cleared.emit();
330 }
331
332 void List::DataObserver::cleared()
333 {
334         list.sel_index = -1;
335         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
336                 delete *i;
337         list.items.clear();
338         list.items_changed();
339
340         list.signal_selection_cleared.emit();
341 }
342
343 void List::DataObserver::refresh_item(unsigned i)
344 {
345         delete list.items[i];
346         // Avoid stale pointer while create_item is executing
347         list.items[i] = 0;
348         list.items[i] = list.create_item(i);
349         list.items_changed();
350 }
351
352
353 void List::Item::autosize_special(const Part &part, Geometry &ageom) const
354 {
355         if(part.get_name()=="children")
356         {
357                 const Sides &margin = part.get_margin();
358                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
359                 {
360                         Geometry cgeom;
361                         (*i)->widget->autosize(cgeom);
362                         ageom.w = max(ageom.w, cgeom.x+cgeom.w+margin.right);
363                         ageom.h = max(ageom.h, cgeom.y+cgeom.h+margin.top);
364                 }
365         }
366 }
367
368 void List::Item::set_active(bool a)
369 {
370         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
371 }
372
373 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
374 {
375         if(part.get_name()=="children")
376         {
377                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
378                         (*i)->widget->render(renderer);
379         }
380 }
381
382
383 void List::MultiColumnItem::check_widths(vector<unsigned> &widths) const
384 {
385         if(widths.size()<children.size())
386                 widths.resize(children.size(), 0);
387
388         unsigned n = 0;
389         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
390         {
391                 Geometry cgeom;
392                 (*i)->widget->autosize(cgeom);
393                 // TODO invent a better way to specify spacings
394                 widths[n] = max(widths[n], cgeom.w+8);
395         }
396 }
397
398 void List::MultiColumnItem::set_widths(const vector<unsigned> &widths)
399 {
400         if(!style)
401                 return;
402
403         const Part *part = style->get_part("children");
404         if(!part)
405                 return;
406
407         const Sides &margin = part->get_margin();
408         int x = margin.left;
409         unsigned n = 0;
410         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i, ++n)
411         {
412                 (*i)->widget->set_position(x, margin.bottom);
413                 x += widths[n];
414         }
415 }
416
417 void List::MultiColumnItem::on_style_change()
418 {
419         if(!style)
420                 return;
421
422         for(std::list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
423                 (*i)->widget->autosize();
424
425         vector<unsigned> widths;
426         List *list = static_cast<List *>(parent);
427         for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
428                 if(*i!=this)
429                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
430                                 mci->check_widths(widths);
431
432         vector<unsigned> self_widths(widths);
433         check_widths(self_widths);
434         bool update_all = false;
435         for(unsigned i=0; (!update_all && i<widths.size() && i<self_widths.size()); ++i)
436                 update_all = self_widths[i]>widths[i];
437
438         if(update_all)
439         {
440                 for(vector<Item *>::const_iterator i=list->items.begin(); i!=list->items.end(); ++i)
441                         if(MultiColumnItem *mci = dynamic_cast<MultiColumnItem *>(*i))
442                                 mci->set_widths(self_widths);
443         }
444
445         set_widths(self_widths);
446 }
447
448
449 List::BasicItem::BasicItem(const string &text):
450         label(text)
451 {
452         add(label);
453 }
454
455 void List::BasicItem::on_style_change()
456 {
457         if(!style)
458                 return;
459
460         label.autosize();
461         if(const Part *part = style->get_part("children"))
462         {
463                 const Sides &margin = part->get_margin();
464                 label.set_position(margin.left, margin.bottom);
465         }
466 }
467
468
469 List::Loader::Loader(List &l):
470         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
471 {
472         add("item", &Loader::item);
473         add("view_size", &List::view_size);
474 }
475
476 void List::Loader::item(const string &v)
477 {
478         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
479 }
480
481 } // namespace GLtk
482 } // namespace Msp