]> git.tdb.fi Git - libs/gltk.git/blob - source/container.cpp
9ef46f1ab85fcd511238ecb83f3f45cf912b7caa
[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 list<Widget *> Container::get_children() const
84 {
85         list<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                 Widget *wdg2 = cont->find_descendant_at(x-cgeom.x, y-cgeom.y);
107                 if(wdg2)
108                         return wdg2;
109         }
110         return wdg;
111 }
112
113 void Container::raise(Widget &wdg)
114 {
115         auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; });
116         if(i==children.end())
117                 throw hierarchy_error("widget not in container");
118
119         children.splice(children.end(), children, i);
120 }
121
122 void Container::set_pointer_focus(Widget *wdg, bool grab)
123 {
124         if(wdg!=pointer_focus)
125         {
126                 if(pointer_focus)
127                         pointer_focus->pointer_leave();
128
129                 pointer_focus = wdg;
130                 pointer_grabbed = grab;
131
132                 if(pointer_focus)
133                         pointer_focus->pointer_enter();
134         }
135         else
136                 pointer_grabbed = grab;
137 }
138
139 void Container::set_input_focus(Widget *wdg)
140 {
141         if(wdg!=input_focus)
142         {
143                 if(input_focus)
144                         input_focus->focus_out();
145
146                 input_focus = wdg;
147                 on_input_focus_changed(input_focus);
148
149                 if(input_focus)
150                         input_focus->focus_in();
151         }
152 }
153
154 Widget *Container::get_final_input_focus() const
155 {
156         if(Container *container = dynamic_cast<Container *>(input_focus))
157                 if(Widget *focus = container->get_final_input_focus())
158                         return focus;
159
160         return input_focus;
161 }
162
163 void Container::check_animation_interval()
164 {
165         Time::TimeDelta shortest;
166         for(const Child *c: children)
167         {
168                 const Time::TimeDelta &child_iv = c->widget->get_animation_interval();
169                 if(child_iv && (!shortest || child_iv<shortest))
170                         shortest = child_iv;
171         }
172
173         if(shortest!=anim_interval)
174                 set_animation_interval(shortest);
175 }
176
177 void Container::rebuild_hierarchy()
178 {
179         Widget::rebuild_hierarchy();
180
181         if(children_rebuild_needed)
182         {
183                 children_rebuild_needed = false;
184                 for(Child *c: children)
185                         c->widget->rebuild_hierarchy();
186         }
187 }
188
189 void Container::button_press(int x, int y, unsigned btn)
190 {
191         if(Widget *child = get_pointer_target(x, y, false))
192         {
193                 if(!click_focus)
194                 {
195                         set_pointer_focus(child);
196                         if(child->is_focusable())
197                                 set_input_focus(child);
198
199                         click_focus = child;
200                         click_button = btn;
201                 }
202
203                 const Geometry &cgeom = child->get_geometry();
204                 child->button_press(x-cgeom.x, y-cgeom.y, btn);
205         }
206 }
207
208 void Container::button_release(int x, int y, unsigned btn)
209 {
210         if(Widget *child = get_pointer_target(x, y, false))
211         {
212                 if(child==click_focus && btn==click_button)
213                 {
214                         click_focus = nullptr;
215                         if(!pointer_focus)
216                                 set_pointer_focus(find_child_at(x, y));
217                 }
218
219                 const Geometry &cgeom = child->get_geometry();
220                 child->button_release(x-cgeom.x, y-cgeom.y, btn);
221         }
222 }
223
224 void Container::pointer_motion(int x, int y)
225 {
226         Widget *child = get_pointer_target(x, y, false);
227         if(!pointer_grabbed)
228                 set_pointer_focus((child && child->get_geometry().is_inside(x, y)) ? child : nullptr);
229
230         if(child)
231         {
232                 const Geometry &cgeom = child->get_geometry();
233                 child->pointer_motion(x-cgeom.x, y-cgeom.y);
234         }
235 }
236
237 Widget *Container::get_pointer_target(int x, int y, bool touch) const
238 {
239         if(pointer_grabbed)
240                 return pointer_focus;
241         else if(!touch && click_focus)
242                 return click_focus;
243         else if(touch && touch_focus)
244                 return touch_focus;
245         else
246         {
247                 Widget *child = find_child_at(x, y);
248                 if(child && child->is_enabled())
249                         return child;
250                 else
251                         return nullptr;
252         }
253 }
254
255 void Container::pointer_leave()
256 {
257         Widget::pointer_leave();
258         set_pointer_focus(nullptr);
259 }
260
261 void Container::touch_press(int x, int y, unsigned finger)
262 {
263         if(Widget *child = get_pointer_target(x, y, true))
264         {
265                 // TODO track focus for each finger separately
266                 if(!touch_focus)
267                         touch_focus = child;
268
269                 const Geometry &cgeom = child->get_geometry();
270                 child->touch_press(x-cgeom.x, y-cgeom.y, finger);
271         }
272 }
273
274 void Container::touch_release(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(child==touch_focus)
280                         touch_focus = nullptr;
281
282                 const Geometry &cgeom = child->get_geometry();
283                 child->touch_release(x-cgeom.x, y-cgeom.y, finger);
284         }
285 }
286
287 void Container::touch_motion(int x, int y, unsigned finger)
288 {
289         if(Widget *child = get_pointer_target(x, y, true))
290         {
291                 const Geometry &cgeom = child->get_geometry();
292                 child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
293         }
294 }
295
296 bool Container::key_press(unsigned key, unsigned mod)
297 {
298         if(input_focus && input_focus->is_enabled())
299                 return input_focus->key_press(key, mod);
300         else
301                 return false;
302 }
303
304 bool Container::key_release(unsigned key, unsigned mod)
305 {
306         if(input_focus && input_focus->is_enabled())
307                 return input_focus->key_release(key, mod);
308         else
309                 return false;
310 }
311
312 bool Container::character(wchar_t ch)
313 {
314         if(input_focus && input_focus->is_enabled())
315                 return input_focus->character(ch);
316         else
317                 return false;
318 }
319
320 void Container::focus_in()
321 {
322         if(saved_input_focus)
323                 set_input_focus(saved_input_focus);
324         Widget::focus_in();
325 }
326
327 void Container::focus_out()
328 {
329         saved_input_focus = input_focus;
330         set_input_focus(nullptr);
331         Widget::focus_out();
332 }
333
334 bool Container::navigate(Navigation nav)
335 {
336         if(input_focus && input_focus->is_enabled())
337                 return input_focus->navigate(nav);
338         else
339                 return false;
340 }
341
342 void Container::animate(const Time::TimeDelta &dt)
343 {
344         for(Child *c: children)
345         {
346                 const Time::TimeDelta &child_iv = c->widget->get_animation_interval();
347                 if(!child_iv)
348                         continue;
349
350                 c->time_since_animate += dt;
351                 if(c->time_since_animate>=child_iv)
352                 {
353                         Time::TimeDelta child_dt = c->time_since_animate;
354                         c->time_since_animate = min(c->time_since_animate-child_iv, child_iv);
355                         c->widget->animate(child_dt);
356                 }
357         }
358 }
359
360 void Container::on_reparent()
361 {
362         for(const Child *c: children)
363         {
364                 if(Container *o = dynamic_cast<Container *>(c->widget))
365                         o->on_reparent();
366                 c->widget->update_style();
367         }
368 }
369
370 void Container::on_input_focus_changed(Widget *wdg)
371 {
372         if(wdg)
373                 raise(*wdg);
374 }
375
376
377 Container::Child::Child(Container &c, Widget *w):
378         container(c),
379         widget(w)
380 {
381         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
382         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
383         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
384         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
385         widget->signal_request_animation.connect(sigc::mem_fun(this, &Child::request_animation));
386         widget->signal_rebuild_needed.connect(sigc::mem_fun(this, &Child::rebuild_needed));
387 }
388
389 Container::Child::~Child()
390 {
391         visibility_changed(false);
392 }
393
394 void Container::Child::visibility_changed(bool v)
395 {
396         if(!v)
397         {
398                 if(widget==container.click_focus)
399                         container.click_focus = nullptr;
400                 if(widget==container.pointer_focus)
401                         container.set_pointer_focus(nullptr);
402                 if(widget==container.input_focus)
403                         container.set_input_focus(nullptr);
404         }
405 }
406
407 void Container::Child::request_focus()
408 {
409         if(container.parent && container.visible)
410                 container.set_focus();
411         if(container.state&FOCUS)
412                 container.set_input_focus(widget);
413 }
414
415 void Container::Child::grab_pointer()
416 {
417         if(!container.pointer_grabbed)
418         {
419                 container.set_pointer_focus(widget, true);
420                 container.signal_grab_pointer.emit();
421         }
422 }
423
424 void Container::Child::ungrab_pointer()
425 {
426         if(container.pointer_grabbed && container.pointer_focus==widget)
427         {
428                 // XXX Should set to the widget under pointer
429                 container.set_pointer_focus(nullptr);
430                 container.signal_ungrab_pointer.emit();
431         }
432 }
433
434 void Container::Child::request_animation(const Time::TimeDelta &interval)
435 {
436         if(!interval)
437                 time_since_animate = Time::zero;
438         container.check_animation_interval();
439 }
440
441 void Container::Child::rebuild_needed()
442 {
443         container.children_rebuild_needed = true;
444         container.signal_rebuild_needed.emit();
445 }
446
447 } // namespace GLtk
448 } // namespace Msp