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