8 hierarchy_error::hierarchy_error(const string &w):
13 Container::Container():
17 pointer_grabbed(false),
21 Container::~Container()
23 while(!children.empty())
24 delete children.front()->widget;
27 void Container::add(Widget &wdg)
30 children.push_back(create_child(&wdg));
34 void Container::remove(Widget &wdg)
36 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
37 if((*i)->widget==&wdg)
42 on_child_removed(wdg);
46 throw hierarchy_error("widget not in container");
49 Container::Child *Container::create_child(Widget *wdg)
51 return new Child(*this, wdg);
54 list<Widget *> Container::get_children() const
56 list<Widget *> result;
57 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
58 result.push_back((*i)->widget);
62 Widget *Container::get_child_at(int x, int y)
64 for(list<Child *>::iterator i=children.end(); i!=children.begin();)
65 if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y))
71 Widget *Container::get_descendant_at(int x, int y)
73 Widget *wdg = get_child_at(x, y);
74 if(Container *cont = dynamic_cast<Container *>(wdg))
76 const Geometry &cgeom = wdg->get_geometry();
77 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
84 void Container::raise(Widget &wdg)
86 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
87 if((*i)->widget==&wdg)
89 children.splice(children.end(), children, i);
93 throw hierarchy_error("widget not in container");
96 void Container::set_pointer_focus(Widget *wdg)
98 if(wdg!=pointer_focus)
101 pointer_focus->pointer_leave();
106 pointer_focus->pointer_enter();
110 void Container::set_input_focus(Widget *wdg)
115 input_focus->focus_out();
122 input_focus->focus_in();
127 Widget *Container::get_final_input_focus() const
129 if(Container *container = dynamic_cast<Container *>(input_focus))
130 if(Widget *focus = container->get_final_input_focus())
136 void Container::button_press(int x, int y, unsigned btn)
138 if(Widget *child = get_pointer_target(x, y))
142 set_pointer_focus(child);
143 if(child->is_focusable())
144 set_input_focus(child);
150 const Geometry &cgeom = child->get_geometry();
151 child->button_press(x-cgeom.x, y-cgeom.y, btn);
155 void Container::button_release(int x, int y, unsigned btn)
157 if(Widget *child = get_pointer_target(x, y))
159 if(child==click_focus && btn==click_button)
163 set_pointer_focus(get_child_at(x, y));
166 const Geometry &cgeom = child->get_geometry();
167 child->button_release(x-cgeom.x, y-cgeom.y, btn);
171 void Container::pointer_motion(int x, int y)
173 Widget *child = get_pointer_target(x, y);
175 set_pointer_focus((!click_focus || child->get_geometry().is_inside(x, y)) ? child : 0);
179 const Geometry &cgeom = child->get_geometry();
180 child->pointer_motion(x-cgeom.x, y-cgeom.y);
184 Widget *Container::get_pointer_target(int x, int y)
187 return pointer_focus;
192 Widget *child = get_child_at(x, y);
193 if(child && child->is_enabled())
200 void Container::pointer_leave()
202 Widget::pointer_leave();
203 set_pointer_focus(0);
206 void Container::key_press(unsigned key, unsigned mod)
209 input_focus->key_press(key, mod);
212 void Container::key_release(unsigned key, unsigned mod)
215 input_focus->key_release(key, mod);
218 void Container::character(wchar_t ch)
221 input_focus->character(ch);
224 void Container::focus_out()
230 void Container::on_reparent()
232 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
234 if(Container *c = dynamic_cast<Container *>((*i)->widget))
236 (*i)->widget->update_style();
241 Container::Child::Child(Container &c, Widget *w):
245 widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
246 widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
247 widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
248 widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
251 Container::Child::~Child()
253 visibility_changed(false);
256 void Container::Child::visibility_changed(bool v)
260 if(widget==container.click_focus)
261 container.click_focus = 0;
262 if(widget==container.pointer_focus)
263 container.set_pointer_focus(0);
264 if(widget==container.input_focus)
265 container.set_input_focus(0);
269 void Container::Child::request_focus()
271 container.set_input_focus(widget);
272 if(container.parent && container.visible)
273 container.set_focus();
276 void Container::Child::grab_pointer()
278 if(!container.pointer_grabbed)
280 container.set_pointer_focus(widget);
281 container.pointer_grabbed = true;
282 container.signal_grab_pointer.emit();
286 void Container::Child::ungrab_pointer()
288 if(container.pointer_grabbed && container.pointer_focus==widget)
290 // XXX Should set to the widget under pointer
291 container.set_pointer_focus(0);
292 container.pointer_grabbed = false;
293 container.signal_ungrab_pointer.emit();