9 hierarchy_error::hierarchy_error(const string &w):
14 Container::Container():
18 pointer_grabbed(false),
23 Container::~Container()
25 while(!children.empty())
26 delete children.front()->widget;
29 void Container::add(Widget &wdg)
32 children.push_back(create_child(&wdg));
33 if(wdg.get_animation_interval())
34 check_animation_interval();
38 void Container::remove(Widget &wdg)
40 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
41 if((*i)->widget==&wdg)
46 if(wdg.get_animation_interval())
47 check_animation_interval();
48 on_child_removed(wdg);
52 throw hierarchy_error("widget not in container");
55 Container::Child *Container::create_child(Widget *wdg)
57 return new Child(*this, wdg);
60 Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
62 Geometry pgeom = part.get_geometry();
63 if(!pgeom.w || !pgeom.h)
66 child.autosize(cgeom);
73 part.get_alignment().apply(pgeom, geom, part.get_margin());
77 void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
79 Geometry cgeom = determine_child_geometry(child, part);
80 const Sides &margin = part.get_margin();
81 ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right);
82 ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom);
85 void Container::reposition_child(Widget &child, const Part &part) const
87 child.set_geometry(determine_child_geometry(child, part));
90 list<Widget *> Container::get_children() const
92 list<Widget *> result;
93 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
94 result.push_back((*i)->widget);
98 Widget *Container::get_child_at(int x, int y) const
100 for(list<Child *>::const_iterator i=children.end(); i!=children.begin();)
101 if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y))
107 Widget *Container::get_descendant_at(int x, int y) const
109 Widget *wdg = get_child_at(x, y);
110 if(Container *cont = dynamic_cast<Container *>(wdg))
112 const Geometry &cgeom = wdg->get_geometry();
113 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
120 void Container::raise(Widget &wdg)
122 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
123 if((*i)->widget==&wdg)
125 children.splice(children.end(), children, i);
129 throw hierarchy_error("widget not in container");
132 void Container::set_pointer_focus(Widget *wdg)
134 if(wdg!=pointer_focus)
137 pointer_focus->pointer_leave();
142 pointer_focus->pointer_enter();
146 void Container::set_input_focus(Widget *wdg)
151 input_focus->focus_out();
158 input_focus->focus_in();
163 Widget *Container::get_final_input_focus() const
165 if(Container *container = dynamic_cast<Container *>(input_focus))
166 if(Widget *focus = container->get_final_input_focus())
172 void Container::check_animation_interval()
174 Time::TimeDelta shortest;
175 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
177 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
178 if(child_iv && (!shortest || child_iv<shortest))
182 if(shortest!=anim_interval)
183 set_animation_interval(shortest);
186 void Container::button_press(int x, int y, unsigned btn)
188 if(Widget *child = get_pointer_target(x, y, false))
192 set_pointer_focus(child);
193 if(child->is_focusable())
194 set_input_focus(child);
200 const Geometry &cgeom = child->get_geometry();
201 child->button_press(x-cgeom.x, y-cgeom.y, btn);
205 void Container::button_release(int x, int y, unsigned btn)
207 if(Widget *child = get_pointer_target(x, y, false))
209 if(child==click_focus && btn==click_button)
213 set_pointer_focus(get_child_at(x, y));
216 const Geometry &cgeom = child->get_geometry();
217 child->button_release(x-cgeom.x, y-cgeom.y, btn);
221 void Container::pointer_motion(int x, int y)
223 Widget *child = get_pointer_target(x, y, false);
225 set_pointer_focus((!click_focus || child->get_geometry().is_inside(x, y)) ? child : 0);
229 const Geometry &cgeom = child->get_geometry();
230 child->pointer_motion(x-cgeom.x, y-cgeom.y);
234 Widget *Container::get_pointer_target(int x, int y, bool touch) const
237 return pointer_focus;
238 else if(!touch && click_focus)
240 else if(touch && touch_focus)
244 Widget *child = get_child_at(x, y);
245 if(child && child->is_enabled())
252 void Container::pointer_leave()
254 Widget::pointer_leave();
255 set_pointer_focus(0);
258 void Container::touch_press(int x, int y, unsigned finger)
260 if(Widget *child = get_pointer_target(x, y, true))
262 // TODO track focus for each finger separately
266 const Geometry &cgeom = child->get_geometry();
267 child->touch_press(x-cgeom.x, y-cgeom.y, finger);
271 void Container::touch_release(int x, int y, unsigned finger)
273 if(Widget *child = get_pointer_target(x, y, true))
275 // TODO track focus for each finger separately
276 if(child==touch_focus)
279 const Geometry &cgeom = child->get_geometry();
280 child->touch_release(x-cgeom.x, y-cgeom.y, finger);
284 void Container::touch_motion(int x, int y, unsigned finger)
286 if(Widget *child = get_pointer_target(x, y, true))
288 const Geometry &cgeom = child->get_geometry();
289 child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
293 bool Container::key_press(unsigned key, unsigned mod)
295 if(input_focus && input_focus->is_enabled())
296 return input_focus->key_press(key, mod);
301 bool Container::key_release(unsigned key, unsigned mod)
303 if(input_focus && input_focus->is_enabled())
304 return input_focus->key_release(key, mod);
309 bool Container::character(wchar_t ch)
311 if(input_focus && input_focus->is_enabled())
312 return input_focus->character(ch);
317 void Container::focus_out()
323 bool Container::navigate(Navigation nav)
325 if(input_focus && input_focus->is_enabled())
326 return input_focus->navigate(nav);
331 void Container::animate(const Time::TimeDelta &dt)
333 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
335 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
339 (*i)->time_since_animate += dt;
340 if((*i)->time_since_animate>=child_iv)
342 Time::TimeDelta child_dt = (*i)->time_since_animate;
343 (*i)->time_since_animate = min((*i)->time_since_animate-child_iv, child_iv);
344 (*i)->widget->animate(child_dt);
349 void Container::on_reparent()
351 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
353 if(Container *c = dynamic_cast<Container *>((*i)->widget))
355 (*i)->widget->update_style();
360 Container::Child::Child(Container &c, Widget *w):
364 widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
365 widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
366 widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
367 widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
368 widget->signal_request_animation.connect(sigc::mem_fun(this, &Child::request_animation));
371 Container::Child::~Child()
373 visibility_changed(false);
376 void Container::Child::visibility_changed(bool v)
380 if(widget==container.click_focus)
381 container.click_focus = 0;
382 if(widget==container.pointer_focus)
383 container.set_pointer_focus(0);
384 if(widget==container.input_focus)
385 container.set_input_focus(0);
389 void Container::Child::request_focus()
391 if(container.parent && container.visible)
392 container.set_focus();
393 if(container.state&FOCUS)
394 container.set_input_focus(widget);
397 void Container::Child::grab_pointer()
399 if(!container.pointer_grabbed)
401 container.set_pointer_focus(widget);
402 container.pointer_grabbed = true;
403 container.signal_grab_pointer.emit();
407 void Container::Child::ungrab_pointer()
409 if(container.pointer_grabbed && container.pointer_focus==widget)
411 // XXX Should set to the widget under pointer
412 container.set_pointer_focus(0);
413 container.pointer_grabbed = false;
414 container.signal_ungrab_pointer.emit();
418 void Container::Child::request_animation(const Time::TimeDelta &interval)
421 time_since_animate = Time::zero;
422 container.check_animation_interval();