9 hierarchy_error::hierarchy_error(const string &w):
14 Container::Container():
18 pointer_grabbed(false),
22 Container::~Container()
24 while(!children.empty())
25 delete children.front()->widget;
28 void Container::add(Widget &wdg)
31 children.push_back(create_child(&wdg));
35 void Container::remove(Widget &wdg)
37 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
38 if((*i)->widget==&wdg)
43 on_child_removed(wdg);
47 throw hierarchy_error("widget not in container");
50 Container::Child *Container::create_child(Widget *wdg)
52 return new Child(*this, wdg);
55 Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
57 Geometry pgeom = part.get_geometry();
58 if(!pgeom.w || !pgeom.h)
61 child.autosize(cgeom);
68 part.get_alignment().apply(pgeom, geom, part.get_margin());
72 void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
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);
80 void Container::reposition_child(Widget &child, const Part &part) const
82 child.set_geometry(determine_child_geometry(child, part));
85 list<Widget *> Container::get_children() const
87 list<Widget *> result;
88 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
89 result.push_back((*i)->widget);
93 Widget *Container::get_child_at(int x, int y)
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))
102 Widget *Container::get_descendant_at(int x, int y)
104 Widget *wdg = get_child_at(x, y);
105 if(Container *cont = dynamic_cast<Container *>(wdg))
107 const Geometry &cgeom = wdg->get_geometry();
108 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
115 void Container::raise(Widget &wdg)
117 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
118 if((*i)->widget==&wdg)
120 children.splice(children.end(), children, i);
124 throw hierarchy_error("widget not in container");
127 void Container::set_pointer_focus(Widget *wdg)
129 if(wdg!=pointer_focus)
132 pointer_focus->pointer_leave();
137 pointer_focus->pointer_enter();
141 void Container::set_input_focus(Widget *wdg)
146 input_focus->focus_out();
153 input_focus->focus_in();
158 Widget *Container::get_final_input_focus() const
160 if(Container *container = dynamic_cast<Container *>(input_focus))
161 if(Widget *focus = container->get_final_input_focus())
167 void Container::button_press(int x, int y, unsigned btn)
169 if(Widget *child = get_pointer_target(x, y))
173 set_pointer_focus(child);
174 if(child->is_focusable())
175 set_input_focus(child);
181 const Geometry &cgeom = child->get_geometry();
182 child->button_press(x-cgeom.x, y-cgeom.y, btn);
186 void Container::button_release(int x, int y, unsigned btn)
188 if(Widget *child = get_pointer_target(x, y))
190 if(child==click_focus && btn==click_button)
194 set_pointer_focus(get_child_at(x, y));
197 const Geometry &cgeom = child->get_geometry();
198 child->button_release(x-cgeom.x, y-cgeom.y, btn);
202 void Container::pointer_motion(int x, int y)
204 Widget *child = get_pointer_target(x, y);
206 set_pointer_focus((!click_focus || child->get_geometry().is_inside(x, y)) ? child : 0);
210 const Geometry &cgeom = child->get_geometry();
211 child->pointer_motion(x-cgeom.x, y-cgeom.y);
215 Widget *Container::get_pointer_target(int x, int y)
218 return pointer_focus;
223 Widget *child = get_child_at(x, y);
224 if(child && child->is_enabled())
231 void Container::pointer_leave()
233 Widget::pointer_leave();
234 set_pointer_focus(0);
237 void Container::key_press(unsigned key, unsigned mod)
240 input_focus->key_press(key, mod);
243 void Container::key_release(unsigned key, unsigned mod)
246 input_focus->key_release(key, mod);
249 void Container::character(wchar_t ch)
252 input_focus->character(ch);
255 void Container::focus_out()
261 void Container::on_reparent()
263 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
265 if(Container *c = dynamic_cast<Container *>((*i)->widget))
267 (*i)->widget->update_style();
272 Container::Child::Child(Container &c, Widget *w):
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));
282 Container::Child::~Child()
284 visibility_changed(false);
287 void Container::Child::visibility_changed(bool v)
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);
300 void Container::Child::request_focus()
302 container.set_input_focus(widget);
303 if(container.parent && container.visible)
304 container.set_focus();
307 void Container::Child::grab_pointer()
309 if(!container.pointer_grabbed)
311 container.set_pointer_focus(widget);
312 container.pointer_grabbed = true;
313 container.signal_grab_pointer.emit();
317 void Container::Child::ungrab_pointer()
319 if(container.pointer_grabbed && container.pointer_focus==widget)
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();