]> git.tdb.fi Git - libs/gltk.git/blob - source/list.cpp
Combine common parts of Column and Row into LinearArrangement
[libs/gltk.git] / source / list.cpp
1 #include <msp/gl/matrix.h>
2 #include <msp/gl/meshbuilder.h>
3 #include "graphic.h"
4 #include "list.h"
5 #include "part.h"
6 #include "style.h"
7 #include "text.h"
8 #include "vslider.h"
9
10 using namespace std;
11
12 namespace Msp {
13 namespace GLtk {
14
15 List::List():
16         data(new BasicListData<string>),
17         own_data(true)
18 {
19         init();
20 }
21
22 List::List(ListData &d):
23         data(&d),
24         own_data(false)
25 {
26         init();
27 }
28
29 void List::init()
30 {
31         sel_index = -1;
32         first = 0;
33         max_scroll = 0;
34
35         observer = new DataObserver(*this);
36
37         add(slider);
38         slider.set_step(1);
39         slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed));
40 }
41
42 List::~List()
43 {
44         delete observer;
45         if(own_data)
46                 delete data;
47 }
48
49 void List::autosize()
50 {
51         autosize_rows(5);
52 }
53
54 void List::autosize_rows(unsigned n)
55 {
56         if(!style)
57                 return;
58
59         Widget::autosize();
60
61         if(const Part *items_part = style->get_part("items"))
62         {
63                 const Sides &margin = items_part->get_margin();
64
65                 unsigned max_w = 0;
66                 unsigned total_h = 0;
67                 for(unsigned i=0; (i<n && i<items.size()); ++i)
68                 {
69                         items[i]->autosize();
70                         const Geometry &igeom = items[i]->get_geometry();
71                         max_w = max(max_w, igeom.w);
72                         total_h += igeom.h;
73                 }
74
75                 if(items.size()<n)
76                         total_h = total_h*n/items.size();
77
78                 geom.w = max(geom.w, max_w+margin.left+margin.right);
79                 geom.h = max(geom.h, total_h+margin.top+margin.bottom);
80         }
81
82         check_view_range();
83         rebuild();
84 }
85
86 void List::autosize_all()
87 {
88         autosize_rows(data->size());
89 }
90
91 void List::set_data(ListData &d)
92 {
93         delete observer;
94         if(own_data)
95                 delete data;
96
97         data = &d;
98         own_data = false;
99         observer = new DataObserver(*this);
100
101         items_changed();
102 }
103
104 void List::items_changed()
105 {
106         check_view_range();
107         signal_autosize_changed.emit();
108         reposition_items();
109 }
110
111 List::Item *List::create_item(unsigned index)
112 {
113         return new BasicItem(data->get_string(index));
114 }
115
116 void List::set_selected_index(int i)
117 {
118         if(i>static_cast<int>(data->size()))
119                 throw out_of_range("List::set_selected_index");
120
121         if(sel_index>=0)
122                 items[sel_index]->set_active(false);
123         if(i<0)
124                 sel_index = -1;
125         else
126         {
127                 sel_index = i;
128                 items[sel_index]->set_active(true);
129                 signal_item_selected.emit(sel_index);
130         }
131 }
132
133 void List::render_special(const Part &part, GL::Renderer &renderer) const
134 {
135         if(part.get_name()=="items")
136         {
137                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
138                         items[i]->render(renderer);
139         }
140         else if(part.get_name()=="slider")
141                 slider.render(renderer);
142 }
143
144 void List::button_press(int x, int y, unsigned btn)
145 {
146         Container::button_press(x, y, btn);
147         if(click_focus && btn==1)
148         {
149                 for(unsigned i=first; (i<items.size() && items[i]->is_visible()); ++i)
150                         if(click_focus==items[i])
151                         {
152                                 set_selected_index(i);
153                                 break;
154                         }
155         }
156 }
157
158 void List::on_geometry_change()
159 {
160         reposition_slider();
161         reposition_items();
162
163         check_view_range();
164 }
165
166 void List::on_style_change()
167 {
168         if(!style)
169                 return;
170
171         reposition_slider();
172         reposition_items();
173
174         check_view_range();
175 }
176
177 void List::reposition_slider()
178 {
179         if(!style)
180                 return;
181
182         if(const Part *slider_part = style->get_part("slider"))
183         {
184                 Geometry sgeom = slider_part->get_geometry();
185                 slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin());
186                 slider.set_geometry(sgeom);
187         }
188 }
189
190 void List::reposition_items()
191 {
192         if(!style)
193                 return;
194
195         if(const Part *items_part = style->get_part("items"))
196         {
197                 const Sides &margin = items_part->get_margin();
198                 unsigned w = geom.w-margin.left-margin.right;
199                 unsigned y = geom.h-margin.top;
200                 for(unsigned i=0; i<items.size(); ++i)
201                 {
202                         if(i<first || !y)
203                                 items[i]->set_visible(false);
204                         else
205                         {
206                                 Geometry igeom = items[i]->get_geometry();
207                                 if(igeom.h+margin.bottom<=y)
208                                 {
209                                         items[i]->set_visible(true);
210                                         y -= igeom.h;
211                                         igeom.x = margin.left;
212                                         igeom.y = y;
213                                         igeom.w = w;
214                                         items[i]->set_geometry(igeom);
215                                 }
216                                 else
217                                 {
218                                         items[i]->set_visible(false);
219                                         y = 0;
220                                 }
221                         }
222                 }
223         }
224 }
225
226 void List::check_view_range()
227 {
228         if(!style)
229                 return;
230
231         unsigned h = geom.h;
232         if(const Part *items_part = style->get_part("items"))
233         {
234                 const Sides &margin = items_part->get_margin();
235                 h -= margin.top+margin.bottom;
236         }
237
238         max_scroll = items.size();
239         for(unsigned i=items.size(); i-->0; )
240         {
241                 unsigned ih = items[i]->get_geometry().h;
242                 if(ih<=h)
243                 {
244                         h -= ih;
245                         --max_scroll;
246                 }
247         }
248
249         if(first>max_scroll)
250                 first = max_scroll;
251
252         slider.set_range(0, max_scroll);
253         slider.set_value(max_scroll-first);
254 }
255
256 void List::slider_value_changed(double value)
257 {
258         if(max_scroll>0)
259         {
260                 first = max_scroll-static_cast<unsigned>(value);
261                 reposition_items();
262         }
263 }
264
265
266 List::DataObserver::DataObserver(List &l):
267         list(l)
268 {
269         list.data->signal_item_added.connect(sigc::mem_fun(this, &DataObserver::item_added));
270         list.data->signal_item_removed.connect(sigc::mem_fun(this, &DataObserver::item_removed));
271         list.data->signal_cleared.connect(sigc::mem_fun(this, &DataObserver::cleared));
272         list.data->signal_refresh_strings.connect(sigc::mem_fun(this, &DataObserver::refresh_strings));
273 }
274
275 void List::DataObserver::item_added(unsigned i)
276 {
277         if(list.sel_index>=static_cast<int>(i))
278                 ++list.sel_index;
279
280         Item *item = list.create_item(i);
281         list.add(*item);
282         list.items.insert(list.items.begin()+i, item);
283         list.items_changed();
284 }
285
286 void List::DataObserver::item_removed(unsigned i)
287 {
288         if(list.sel_index>static_cast<int>(i))
289                 --list.sel_index;
290         else if(list.sel_index==static_cast<int>(i))
291                 list.sel_index = -1;
292
293         delete list.items[i];
294         list.items.erase(list.items.begin()+i);
295         list.items_changed();
296 }
297
298 void List::DataObserver::cleared()
299 {
300         list.sel_index = -1;
301         for(vector<Item *>::iterator i=list.items.begin(); i!=list.items.end(); ++i)
302                 delete *i;
303         list.items.clear();
304         list.items_changed();
305 }
306
307 void List::DataObserver::refresh_strings()
308 {
309         list.items_changed();
310 }
311
312
313 void List::Item::autosize()
314 {
315         Widget::autosize();
316
317         if(const Part *part = style->get_part("children"))
318         {
319                 const Sides &margin = part->get_margin();
320                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
321                 {
322                         const Geometry &cgeom = (*i)->widget->get_geometry();
323                         geom.w = max(geom.w, cgeom.x+cgeom.w+margin.right);
324                         geom.h = max(geom.h, cgeom.y+cgeom.h+margin.top);
325                 }
326         }
327 }
328
329 void List::Item::set_active(bool a)
330 {
331         set_state(ACTIVE, (a ? ACTIVE : NORMAL));
332 }
333
334 void List::Item::render_special(const Part &part, GL::Renderer &renderer) const
335 {
336         if(part.get_name()=="children")
337         {
338                 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
339                         (*i)->widget->render(renderer);
340         }
341 }
342
343
344 List::BasicItem::BasicItem(const string &text):
345         label(text)
346 {
347         add(label);
348 }
349
350 void List::BasicItem::on_style_change()
351 {
352         if(!style)
353                 return;
354
355         label.autosize();
356         if(const Part *part = style->get_part("children"))
357         {
358                 const Sides &margin = part->get_margin();
359                 label.set_position(margin.left, margin.bottom);
360         }
361 }
362
363
364 List::Loader::Loader(List &l):
365         DataFile::DerivedObjectLoader<List, Widget::Loader>(l)
366 {
367         add("item", &Loader::item);
368 }
369
370 void List::Loader::item(const string &v)
371 {
372         dynamic_cast<BasicListData<string> &>(*obj.data).append(v);
373 }
374
375 } // namespace GLtk
376 } // namespace Msp