]> git.tdb.fi Git - libs/gltk.git/blob - source/container.cpp
Defer widget rebuild to just before they are rendered
[libs/gltk.git] / source / container.cpp
1 #include "container.h"
2 #include "part.h"
3
4 using namespace std;
5
6 namespace Msp {
7 namespace GLtk {
8
9 hierarchy_error::hierarchy_error(const string &w):
10         logic_error(w)
11 { }
12
13
14 Container::Container():
15         click_focus(0),
16         click_button(0),
17         pointer_focus(0),
18         pointer_grabbed(false),
19         input_focus(0),
20         saved_input_focus(0),
21         touch_focus(0),
22         children_rebuild_needed(false)
23 { }
24
25 Container::~Container()
26 {
27         while(!children.empty())
28                 delete children.front()->widget;
29 }
30
31 void Container::add(Widget &wdg)
32 {
33         wdg.set_parent(this);
34         children.push_back(create_child(&wdg));
35         if(wdg.get_animation_interval())
36                 check_animation_interval();
37         children_rebuild_needed = true;
38         on_child_added(wdg);
39 }
40
41 void Container::remove(Widget &wdg)
42 {
43         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
44                 if((*i)->widget==&wdg)
45                 {
46                         wdg.set_parent(0);
47                         delete *i;
48                         children.erase(i);
49                         if(wdg.get_animation_interval())
50                                 check_animation_interval();
51                         on_child_removed(wdg);
52                         return;
53                 }
54
55         throw hierarchy_error("widget not in container");
56 }
57
58 Container::Child *Container::create_child(Widget *wdg)
59 {
60         return new Child(*this, wdg);
61 }
62
63 Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
64 {
65         Geometry pgeom = part.get_geometry();
66         if(!pgeom.w || !pgeom.h)
67         {
68                 Geometry cgeom;
69                 child.autosize(cgeom);
70                 if(!pgeom.w)
71                         pgeom.w = cgeom.w;
72                 if(!pgeom.h)
73                         pgeom.h = cgeom.h;
74         }
75
76         part.get_alignment().apply(pgeom, geom, part.get_margin());
77         return pgeom;
78 }
79
80 void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
81 {
82         Geometry cgeom = determine_child_geometry(child, part);
83         const Sides &margin = part.get_margin();
84         ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right);
85         ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom);
86 }
87
88 void Container::reposition_child(Widget &child, const Part &part) const
89 {
90         child.set_geometry(determine_child_geometry(child, part));
91 }
92
93 list<Widget *> Container::get_children() const
94 {
95         list<Widget *> result;
96         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
97                 result.push_back((*i)->widget);
98         return result;
99 }
100
101 Widget *Container::get_child_at(int x, int y) const
102 {
103         for(list<Child *>::const_iterator i=children.end(); i!=children.begin();)
104                 if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y))
105                         return (*i)->widget;
106
107         return 0;
108 }
109
110 Widget *Container::get_descendant_at(int x, int y) const
111 {
112         Widget *wdg = get_child_at(x, y);
113         if(Container *cont = dynamic_cast<Container *>(wdg))
114         {
115                 const Geometry &cgeom = wdg->get_geometry();
116                 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
117                 if(wdg2)
118                         return wdg2;
119         }
120         return wdg;
121 }
122
123 void Container::raise(Widget &wdg)
124 {
125         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
126                 if((*i)->widget==&wdg)
127                 {
128                         children.splice(children.end(), children, i);
129                         return;
130                 }
131
132         throw hierarchy_error("widget not in container");
133 }
134
135 void Container::set_pointer_focus(Widget *wdg, bool grab)
136 {
137         if(wdg!=pointer_focus)
138         {
139                 if(pointer_focus)
140                         pointer_focus->pointer_leave();
141
142                 pointer_focus = wdg;
143                 pointer_grabbed = grab;
144
145                 if(pointer_focus)
146                         pointer_focus->pointer_enter();
147         }
148         else
149                 pointer_grabbed = grab;
150 }
151
152 void Container::set_input_focus(Widget *wdg)
153 {
154         if(wdg!=input_focus)
155         {
156                 if(input_focus)
157                         input_focus->focus_out();
158
159                 input_focus = wdg;
160                 on_input_focus_changed(input_focus);
161
162                 if(input_focus)
163                         input_focus->focus_in();
164         }
165 }
166
167 Widget *Container::get_final_input_focus() const
168 {
169         if(Container *container = dynamic_cast<Container *>(input_focus))
170                 if(Widget *focus = container->get_final_input_focus())
171                         return focus;
172
173         return input_focus;
174 }
175
176 void Container::check_animation_interval()
177 {
178         Time::TimeDelta shortest;
179         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
180         {
181                 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
182                 if(child_iv && (!shortest || child_iv<shortest))
183                         shortest = child_iv;
184         }
185
186         if(shortest!=anim_interval)
187                 set_animation_interval(shortest);
188 }
189
190 void Container::rebuild_hierarchy()
191 {
192         Widget::rebuild_hierarchy();
193
194         if(children_rebuild_needed)
195         {
196                 children_rebuild_needed = false;
197                 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
198                         (*i)->widget->rebuild_hierarchy();
199         }
200 }
201
202 void Container::button_press(int x, int y, unsigned btn)
203 {
204         if(Widget *child = get_pointer_target(x, y, false))
205         {
206                 if(!click_focus)
207                 {
208                         set_pointer_focus(child);
209                         if(child->is_focusable())
210                                 set_input_focus(child);
211
212                         click_focus = child;
213                         click_button = btn;
214                 }
215
216                 const Geometry &cgeom = child->get_geometry();
217                 child->button_press(x-cgeom.x, y-cgeom.y, btn);
218         }
219 }
220
221 void Container::button_release(int x, int y, unsigned btn)
222 {
223         if(Widget *child = get_pointer_target(x, y, false))
224         {
225                 if(child==click_focus && btn==click_button)
226                 {
227                         click_focus = 0;
228                         if(!pointer_focus)
229                                 set_pointer_focus(get_child_at(x, y));
230                 }
231
232                 const Geometry &cgeom = child->get_geometry();
233                 child->button_release(x-cgeom.x, y-cgeom.y, btn);
234         }
235 }
236
237 void Container::pointer_motion(int x, int y)
238 {
239         Widget *child = get_pointer_target(x, y, false);
240         if(!pointer_grabbed)
241                 set_pointer_focus((child && child->get_geometry().is_inside(x, y)) ? child : 0);
242
243         if(child)
244         {
245                 const Geometry &cgeom = child->get_geometry();
246                 child->pointer_motion(x-cgeom.x, y-cgeom.y);
247         }
248 }
249
250 Widget *Container::get_pointer_target(int x, int y, bool touch) const
251 {
252         if(pointer_grabbed)
253                 return pointer_focus;
254         else if(!touch && click_focus)
255                 return click_focus;
256         else if(touch && touch_focus)
257                 return touch_focus;
258         else
259         {
260                 Widget *child = get_child_at(x, y);
261                 if(child && child->is_enabled())
262                         return child;
263                 else
264                         return 0;
265         }
266 }
267
268 void Container::pointer_leave()
269 {
270         Widget::pointer_leave();
271         set_pointer_focus(0);
272 }
273
274 void Container::touch_press(int x, int y, unsigned finger)
275 {
276         if(Widget *child = get_pointer_target(x, y, true))
277         {
278                 // TODO track focus for each finger separately
279                 if(!touch_focus)
280                         touch_focus = child;
281
282                 const Geometry &cgeom = child->get_geometry();
283                 child->touch_press(x-cgeom.x, y-cgeom.y, finger);
284         }
285 }
286
287 void Container::touch_release(int x, int y, unsigned finger)
288 {
289         if(Widget *child = get_pointer_target(x, y, true))
290         {
291                 // TODO track focus for each finger separately
292                 if(child==touch_focus)
293                         touch_focus = 0;
294
295                 const Geometry &cgeom = child->get_geometry();
296                 child->touch_release(x-cgeom.x, y-cgeom.y, finger);
297         }
298 }
299
300 void Container::touch_motion(int x, int y, unsigned finger)
301 {
302         if(Widget *child = get_pointer_target(x, y, true))
303         {
304                 const Geometry &cgeom = child->get_geometry();
305                 child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
306         }
307 }
308
309 bool Container::key_press(unsigned key, unsigned mod)
310 {
311         if(input_focus && input_focus->is_enabled())
312                 return input_focus->key_press(key, mod);
313         else
314                 return false;
315 }
316
317 bool Container::key_release(unsigned key, unsigned mod)
318 {
319         if(input_focus && input_focus->is_enabled())
320                 return input_focus->key_release(key, mod);
321         else
322                 return false;
323 }
324
325 bool Container::character(wchar_t ch)
326 {
327         if(input_focus && input_focus->is_enabled())
328                 return input_focus->character(ch);
329         else
330                 return false;
331 }
332
333 void Container::focus_in()
334 {
335         if(saved_input_focus && saved_input_focus->get_parent()==this)
336                 set_input_focus(saved_input_focus);
337         Widget::focus_in();
338 }
339
340 void Container::focus_out()
341 {
342         saved_input_focus = input_focus;
343         set_input_focus(0);
344         Widget::focus_out();
345 }
346
347 bool Container::navigate(Navigation nav)
348 {
349         if(input_focus && input_focus->is_enabled())
350                 return input_focus->navigate(nav);
351         else
352                 return false;
353 }
354
355 void Container::animate(const Time::TimeDelta &dt)
356 {
357         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
358         {
359                 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
360                 if(!child_iv)
361                         continue;
362
363                 (*i)->time_since_animate += dt;
364                 if((*i)->time_since_animate>=child_iv)
365                 {
366                         Time::TimeDelta child_dt = (*i)->time_since_animate;
367                         (*i)->time_since_animate = min((*i)->time_since_animate-child_iv, child_iv);
368                         (*i)->widget->animate(child_dt);
369                 }
370         }
371 }
372
373 void Container::on_reparent()
374 {
375         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
376         {
377                 if(Container *c = dynamic_cast<Container *>((*i)->widget))
378                         c->on_reparent();
379                 (*i)->widget->update_style();
380         }
381 }
382
383 void Container::on_input_focus_changed(Widget *wdg)
384 {
385         if(wdg)
386                 raise(*wdg);
387 }
388
389
390 Container::Child::Child(Container &c, Widget *w):
391         container(c),
392         widget(w)
393 {
394         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
395         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
396         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
397         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
398         widget->signal_request_animation.connect(sigc::mem_fun(this, &Child::request_animation));
399         widget->signal_rebuild_needed.connect(sigc::mem_fun(this, &Child::rebuild_needed));
400 }
401
402 Container::Child::~Child()
403 {
404         visibility_changed(false);
405 }
406
407 void Container::Child::visibility_changed(bool v)
408 {
409         if(!v)
410         {
411                 if(widget==container.click_focus)
412                         container.click_focus = 0;
413                 if(widget==container.pointer_focus)
414                         container.set_pointer_focus(0);
415                 if(widget==container.input_focus)
416                         container.set_input_focus(0);
417         }
418 }
419
420 void Container::Child::request_focus()
421 {
422         if(container.parent && container.visible)
423                 container.set_focus();
424         if(container.state&FOCUS)
425                 container.set_input_focus(widget);
426 }
427
428 void Container::Child::grab_pointer()
429 {
430         if(!container.pointer_grabbed)
431         {
432                 container.set_pointer_focus(widget, true);
433                 container.signal_grab_pointer.emit();
434         }
435 }
436
437 void Container::Child::ungrab_pointer()
438 {
439         if(container.pointer_grabbed && container.pointer_focus==widget)
440         {
441                 // XXX Should set to the widget under pointer
442                 container.set_pointer_focus(0);
443                 container.signal_ungrab_pointer.emit();
444         }
445 }
446
447 void Container::Child::request_animation(const Time::TimeDelta &interval)
448 {
449         if(!interval)
450                 time_since_animate = Time::zero;
451         container.check_animation_interval();
452 }
453
454 void Container::Child::rebuild_needed()
455 {
456         container.children_rebuild_needed = true;
457         container.signal_rebuild_needed.emit();
458 }
459
460 } // namespace GLtk
461 } // namespace Msp