]> git.tdb.fi Git - libs/gltk.git/blob - source/container.cpp
Move navigation logic from Container to Panel
[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)
308                 return input_focus->navigate(nav);
309         else
310                 return false;
311 }
312
313 void Container::on_reparent()
314 {
315         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
316         {
317                 if(Container *c = dynamic_cast<Container *>((*i)->widget))
318                         c->on_reparent();
319                 (*i)->widget->update_style();
320         }
321 }
322
323
324 Container::Child::Child(Container &c, Widget *w):
325         container(c),
326         widget(w)
327 {
328         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
329         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
330         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
331         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
332 }
333
334 Container::Child::~Child()
335 {
336         visibility_changed(false);
337 }
338
339 void Container::Child::visibility_changed(bool v)
340 {
341         if(!v)
342         {
343                 if(widget==container.click_focus)
344                         container.click_focus = 0;
345                 if(widget==container.pointer_focus)
346                         container.set_pointer_focus(0);
347                 if(widget==container.input_focus)
348                         container.set_input_focus(0);
349         }
350 }
351
352 void Container::Child::request_focus()
353 {
354         container.set_input_focus(widget);
355         if(container.parent && container.visible)
356                 container.set_focus();
357 }
358
359 void Container::Child::grab_pointer()
360 {
361         if(!container.pointer_grabbed)
362         {
363                 container.set_pointer_focus(widget);
364                 container.pointer_grabbed = true;
365                 container.signal_grab_pointer.emit();
366         }
367 }
368
369 void Container::Child::ungrab_pointer()
370 {
371         if(container.pointer_grabbed && container.pointer_focus==widget)
372         {
373                 // XXX Should set to the widget under pointer
374                 container.set_pointer_focus(0);
375                 container.pointer_grabbed = false;
376                 container.signal_ungrab_pointer.emit();
377         }
378 }
379
380 } // namespace GLtk
381 } // namespace Msp