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