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