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