]> git.tdb.fi Git - libs/gltk.git/blob - source/container.cpp
Rudimentary touchscreen support
[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::key_press(unsigned key, unsigned mod)
241 {
242         if(input_focus)
243                 input_focus->key_press(key, mod);
244 }
245
246 void Container::key_release(unsigned key, unsigned mod)
247 {
248         if(input_focus)
249                 input_focus->key_release(key, mod);
250 }
251
252 void Container::character(wchar_t ch)
253 {
254         if(input_focus)
255                 input_focus->character(ch);
256 }
257
258 void Container::touch_press(int x, int y, unsigned finger)
259 {
260         if(Widget *child = get_pointer_target(x, y, true))
261         {
262                 // TODO track focus for each finger separately
263                 if(!touch_focus)
264                         touch_focus = child;
265
266                 const Geometry &cgeom = child->get_geometry();
267                 child->touch_press(x-cgeom.x, y-cgeom.y, finger);
268         }
269 }
270
271 void Container::touch_release(int x, int y, unsigned finger)
272 {
273         if(Widget *child = get_pointer_target(x, y, true))
274         {
275                 // TODO track focus for each finger separately
276                 if(child==touch_focus)
277                         touch_focus = 0;
278
279                 const Geometry &cgeom = child->get_geometry();
280                 child->touch_release(x-cgeom.x, y-cgeom.y, finger);
281         }
282 }
283
284 void Container::touch_motion(int x, int y, unsigned finger)
285 {
286         if(Widget *child = get_pointer_target(x, y, true))
287         {
288                 const Geometry &cgeom = child->get_geometry();
289                 child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
290         }
291 }
292
293 void Container::focus_out()
294 {
295         set_input_focus(0);
296         Widget::focus_out();
297 }
298
299 void Container::on_reparent()
300 {
301         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
302         {
303                 if(Container *c = dynamic_cast<Container *>((*i)->widget))
304                         c->on_reparent();
305                 (*i)->widget->update_style();
306         }
307 }
308
309
310 Container::Child::Child(Container &c, Widget *w):
311         container(c),
312         widget(w)
313 {
314         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
315         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
316         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
317         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
318 }
319
320 Container::Child::~Child()
321 {
322         visibility_changed(false);
323 }
324
325 void Container::Child::visibility_changed(bool v)
326 {
327         if(!v)
328         {
329                 if(widget==container.click_focus)
330                         container.click_focus = 0;
331                 if(widget==container.pointer_focus)
332                         container.set_pointer_focus(0);
333                 if(widget==container.input_focus)
334                         container.set_input_focus(0);
335         }
336 }
337
338 void Container::Child::request_focus()
339 {
340         container.set_input_focus(widget);
341         if(container.parent && container.visible)
342                 container.set_focus();
343 }
344
345 void Container::Child::grab_pointer()
346 {
347         if(!container.pointer_grabbed)
348         {
349                 container.set_pointer_focus(widget);
350                 container.pointer_grabbed = true;
351                 container.signal_grab_pointer.emit();
352         }
353 }
354
355 void Container::Child::ungrab_pointer()
356 {
357         if(container.pointer_grabbed && container.pointer_focus==widget)
358         {
359                 // XXX Should set to the widget under pointer
360                 container.set_pointer_focus(0);
361                 container.pointer_grabbed = false;
362                 container.signal_ungrab_pointer.emit();
363         }
364 }
365
366 } // namespace GLtk
367 } // namespace Msp