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