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