]> git.tdb.fi Git - libs/gltk.git/blob - source/container.cpp
Refactor child positioning logic
[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 { }
21
22 Container::~Container()
23 {
24         while(!children.empty())
25                 delete children.front()->widget;
26 }
27
28 void Container::add(Widget &wdg)
29 {
30         wdg.set_parent(this);
31         children.push_back(create_child(&wdg));
32         on_child_added(wdg);
33 }
34
35 void Container::remove(Widget &wdg)
36 {
37         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
38                 if((*i)->widget==&wdg)
39                 {
40                         wdg.set_parent(0);
41                         delete *i;
42                         children.erase(i);
43                         on_child_removed(wdg);
44                         return;
45                 }
46
47         throw hierarchy_error("widget not in container");
48 }
49
50 Container::Child *Container::create_child(Widget *wdg)
51 {
52         return new Child(*this, wdg);
53 }
54
55 Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
56 {
57         Geometry pgeom = part.get_geometry();
58         if(!pgeom.w || !pgeom.h)
59         {
60                 Geometry cgeom;
61                 child.autosize(cgeom);
62                 if(!pgeom.w)
63                         pgeom.w = cgeom.w;
64                 if(!pgeom.h)
65                         pgeom.h = cgeom.h;
66         }
67
68         part.get_alignment().apply(pgeom, geom, part.get_margin());
69         return pgeom;
70 }
71
72 void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
73 {
74         Geometry cgeom = determine_child_geometry(child, part);
75         const Sides &margin = part.get_margin();
76         ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right);
77         ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom);
78 }
79
80 void Container::reposition_child(Widget &child, const Part &part) const
81 {
82         child.set_geometry(determine_child_geometry(child, part));
83 }
84
85 list<Widget *> Container::get_children() const
86 {
87         list<Widget *> result;
88         for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
89                 result.push_back((*i)->widget);
90         return result;
91 }
92
93 Widget *Container::get_child_at(int x, int y)
94 {
95         for(list<Child *>::iterator i=children.end(); i!=children.begin();)
96                 if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y))
97                         return (*i)->widget;
98
99         return 0;
100 }
101
102 Widget *Container::get_descendant_at(int x, int y)
103 {
104         Widget *wdg = get_child_at(x, y);
105         if(Container *cont = dynamic_cast<Container *>(wdg))
106         {
107                 const Geometry &cgeom = wdg->get_geometry();
108                 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
109                 if(wdg2)
110                         return wdg2;
111         }
112         return wdg;
113 }
114
115 void Container::raise(Widget &wdg)
116 {
117         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
118                 if((*i)->widget==&wdg)
119                 {
120                         children.splice(children.end(), children, i);
121                         return;
122                 }
123
124         throw hierarchy_error("widget not in container");
125 }
126
127 void Container::set_pointer_focus(Widget *wdg)
128 {
129         if(wdg!=pointer_focus)
130         {
131                 if(pointer_focus)
132                         pointer_focus->pointer_leave();
133
134                 pointer_focus = wdg;
135
136                 if(pointer_focus)
137                         pointer_focus->pointer_enter();
138         }
139 }
140
141 void Container::set_input_focus(Widget *wdg)
142 {
143         if(wdg!=input_focus)
144         {
145                 if(input_focus)
146                         input_focus->focus_out();
147
148                 input_focus = wdg;
149
150                 if(input_focus)
151                 {
152                         raise(*wdg);
153                         input_focus->focus_in();
154                 }
155         }
156 }
157
158 Widget *Container::get_final_input_focus() const
159 {
160         if(Container *container = dynamic_cast<Container *>(input_focus))
161                 if(Widget *focus = container->get_final_input_focus())
162                         return focus;
163
164         return input_focus;
165 }
166
167 void Container::button_press(int x, int y, unsigned btn)
168 {
169         if(Widget *child = get_pointer_target(x, y))
170         {
171                 if(!click_focus)
172                 {
173                         set_pointer_focus(child);
174                         if(child->is_focusable())
175                                 set_input_focus(child);
176
177                         click_focus = child;
178                         click_button = btn;
179                 }
180
181                 const Geometry &cgeom = child->get_geometry();
182                 child->button_press(x-cgeom.x, y-cgeom.y, btn);
183         }
184 }
185
186 void Container::button_release(int x, int y, unsigned btn)
187 {
188         if(Widget *child = get_pointer_target(x, y))
189         {
190                 if(child==click_focus && btn==click_button)
191                 {
192                         click_focus = 0;
193                         if(!pointer_focus)
194                                 set_pointer_focus(get_child_at(x, y));
195                 }
196
197                 const Geometry &cgeom = child->get_geometry();
198                 child->button_release(x-cgeom.x, y-cgeom.y, btn);
199         }
200 }
201
202 void Container::pointer_motion(int x, int y)
203 {
204         Widget *child = get_pointer_target(x, y);
205         if(!pointer_grabbed)
206                 set_pointer_focus((!click_focus || child->get_geometry().is_inside(x, y)) ? child : 0);
207
208         if(child)
209         {
210                 const Geometry &cgeom = child->get_geometry();
211                 child->pointer_motion(x-cgeom.x, y-cgeom.y);
212         }
213 }
214
215 Widget *Container::get_pointer_target(int x, int y)
216 {
217         if(pointer_grabbed)
218                 return pointer_focus;
219         else if(click_focus)
220                 return click_focus;
221         else
222         {
223                 Widget *child = get_child_at(x, y);
224                 if(child && child->is_enabled())
225                         return child;
226                 else
227                         return 0;
228         }
229 }
230
231 void Container::pointer_leave()
232 {
233         Widget::pointer_leave();
234         set_pointer_focus(0);
235 }
236
237 void Container::key_press(unsigned key, unsigned mod)
238 {
239         if(input_focus)
240                 input_focus->key_press(key, mod);
241 }
242
243 void Container::key_release(unsigned key, unsigned mod)
244 {
245         if(input_focus)
246                 input_focus->key_release(key, mod);
247 }
248
249 void Container::character(wchar_t ch)
250 {
251         if(input_focus)
252                 input_focus->character(ch);
253 }
254
255 void Container::focus_out()
256 {
257         set_input_focus(0);
258         Widget::focus_out();
259 }
260
261 void Container::on_reparent()
262 {
263         for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
264         {
265                 if(Container *c = dynamic_cast<Container *>((*i)->widget))
266                         c->on_reparent();
267                 (*i)->widget->update_style();
268         }
269 }
270
271
272 Container::Child::Child(Container &c, Widget *w):
273         container(c),
274         widget(w)
275 {
276         widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
277         widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
278         widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
279         widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
280 }
281
282 Container::Child::~Child()
283 {
284         visibility_changed(false);
285 }
286
287 void Container::Child::visibility_changed(bool v)
288 {
289         if(!v)
290         {
291                 if(widget==container.click_focus)
292                         container.click_focus = 0;
293                 if(widget==container.pointer_focus)
294                         container.set_pointer_focus(0);
295                 if(widget==container.input_focus)
296                         container.set_input_focus(0);
297         }
298 }
299
300 void Container::Child::request_focus()
301 {
302         container.set_input_focus(widget);
303         if(container.parent && container.visible)
304                 container.set_focus();
305 }
306
307 void Container::Child::grab_pointer()
308 {
309         if(!container.pointer_grabbed)
310         {
311                 container.set_pointer_focus(widget);
312                 container.pointer_grabbed = true;
313                 container.signal_grab_pointer.emit();
314         }
315 }
316
317 void Container::Child::ungrab_pointer()
318 {
319         if(container.pointer_grabbed && container.pointer_focus==widget)
320         {
321                 // XXX Should set to the widget under pointer
322                 container.set_pointer_focus(0);
323                 container.pointer_grabbed = false;
324                 container.signal_ungrab_pointer.emit();
325         }
326 }
327
328 } // namespace GLtk
329 } // namespace Msp