9 hierarchy_error::hierarchy_error(const string &w):
14 Container::Container():
18 pointer_grabbed(false),
24 Container::~Container()
26 while(!children.empty())
27 delete children.front()->widget;
30 void Container::add(Widget &wdg)
33 children.push_back(create_child(&wdg));
34 if(wdg.get_animation_interval())
35 check_animation_interval();
39 void Container::remove(Widget &wdg)
41 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
42 if((*i)->widget==&wdg)
47 if(wdg.get_animation_interval())
48 check_animation_interval();
49 on_child_removed(wdg);
53 throw hierarchy_error("widget not in container");
56 Container::Child *Container::create_child(Widget *wdg)
58 return new Child(*this, wdg);
61 Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
63 Geometry pgeom = part.get_geometry();
64 if(!pgeom.w || !pgeom.h)
67 child.autosize(cgeom);
74 part.get_alignment().apply(pgeom, geom, part.get_margin());
78 void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
80 Geometry cgeom = determine_child_geometry(child, part);
81 const Sides &margin = part.get_margin();
82 ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right);
83 ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom);
86 void Container::reposition_child(Widget &child, const Part &part) const
88 child.set_geometry(determine_child_geometry(child, part));
91 list<Widget *> Container::get_children() const
93 list<Widget *> result;
94 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
95 result.push_back((*i)->widget);
99 Widget *Container::get_child_at(int x, int y) const
101 for(list<Child *>::const_iterator i=children.end(); i!=children.begin();)
102 if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y))
108 Widget *Container::get_descendant_at(int x, int y) const
110 Widget *wdg = get_child_at(x, y);
111 if(Container *cont = dynamic_cast<Container *>(wdg))
113 const Geometry &cgeom = wdg->get_geometry();
114 Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y);
121 void Container::raise(Widget &wdg)
123 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
124 if((*i)->widget==&wdg)
126 children.splice(children.end(), children, i);
130 throw hierarchy_error("widget not in container");
133 void Container::set_pointer_focus(Widget *wdg, bool grab)
135 if(wdg!=pointer_focus)
138 pointer_focus->pointer_leave();
141 pointer_grabbed = grab;
144 pointer_focus->pointer_enter();
147 pointer_grabbed = grab;
150 void Container::set_input_focus(Widget *wdg)
155 input_focus->focus_out();
158 on_input_focus_changed(input_focus);
161 input_focus->focus_in();
165 Widget *Container::get_final_input_focus() const
167 if(Container *container = dynamic_cast<Container *>(input_focus))
168 if(Widget *focus = container->get_final_input_focus())
174 void Container::check_animation_interval()
176 Time::TimeDelta shortest;
177 for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
179 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
180 if(child_iv && (!shortest || child_iv<shortest))
184 if(shortest!=anim_interval)
185 set_animation_interval(shortest);
188 void Container::button_press(int x, int y, unsigned btn)
190 if(Widget *child = get_pointer_target(x, y, false))
194 set_pointer_focus(child);
195 if(child->is_focusable())
196 set_input_focus(child);
202 const Geometry &cgeom = child->get_geometry();
203 child->button_press(x-cgeom.x, y-cgeom.y, btn);
207 void Container::button_release(int x, int y, unsigned btn)
209 if(Widget *child = get_pointer_target(x, y, false))
211 if(child==click_focus && btn==click_button)
215 set_pointer_focus(get_child_at(x, y));
218 const Geometry &cgeom = child->get_geometry();
219 child->button_release(x-cgeom.x, y-cgeom.y, btn);
223 void Container::pointer_motion(int x, int y)
225 Widget *child = get_pointer_target(x, y, false);
227 set_pointer_focus((!click_focus || child->get_geometry().is_inside(x, y)) ? child : 0);
231 const Geometry &cgeom = child->get_geometry();
232 child->pointer_motion(x-cgeom.x, y-cgeom.y);
236 Widget *Container::get_pointer_target(int x, int y, bool touch) const
239 return pointer_focus;
240 else if(!touch && click_focus)
242 else if(touch && touch_focus)
246 Widget *child = get_child_at(x, y);
247 if(child && child->is_enabled())
254 void Container::pointer_leave()
256 Widget::pointer_leave();
257 set_pointer_focus(0);
260 void Container::touch_press(int x, int y, unsigned finger)
262 if(Widget *child = get_pointer_target(x, y, true))
264 // TODO track focus for each finger separately
268 const Geometry &cgeom = child->get_geometry();
269 child->touch_press(x-cgeom.x, y-cgeom.y, finger);
273 void Container::touch_release(int x, int y, unsigned finger)
275 if(Widget *child = get_pointer_target(x, y, true))
277 // TODO track focus for each finger separately
278 if(child==touch_focus)
281 const Geometry &cgeom = child->get_geometry();
282 child->touch_release(x-cgeom.x, y-cgeom.y, finger);
286 void Container::touch_motion(int x, int y, unsigned finger)
288 if(Widget *child = get_pointer_target(x, y, true))
290 const Geometry &cgeom = child->get_geometry();
291 child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
295 bool Container::key_press(unsigned key, unsigned mod)
297 if(input_focus && input_focus->is_enabled())
298 return input_focus->key_press(key, mod);
303 bool Container::key_release(unsigned key, unsigned mod)
305 if(input_focus && input_focus->is_enabled())
306 return input_focus->key_release(key, mod);
311 bool Container::character(wchar_t ch)
313 if(input_focus && input_focus->is_enabled())
314 return input_focus->character(ch);
319 void Container::focus_in()
321 if(saved_input_focus && saved_input_focus->get_parent()==this)
322 set_input_focus(saved_input_focus);
326 void Container::focus_out()
328 saved_input_focus = input_focus;
333 bool Container::navigate(Navigation nav)
335 if(input_focus && input_focus->is_enabled())
336 return input_focus->navigate(nav);
341 void Container::animate(const Time::TimeDelta &dt)
343 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
345 const Time::TimeDelta &child_iv = (*i)->widget->get_animation_interval();
349 (*i)->time_since_animate += dt;
350 if((*i)->time_since_animate>=child_iv)
352 Time::TimeDelta child_dt = (*i)->time_since_animate;
353 (*i)->time_since_animate = min((*i)->time_since_animate-child_iv, child_iv);
354 (*i)->widget->animate(child_dt);
359 void Container::on_reparent()
361 for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
363 if(Container *c = dynamic_cast<Container *>((*i)->widget))
365 (*i)->widget->update_style();
369 void Container::on_input_focus_changed(Widget *wdg)
376 Container::Child::Child(Container &c, Widget *w):
380 widget->signal_visibility_changed.connect(sigc::mem_fun(this, &Child::visibility_changed));
381 widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
382 widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
383 widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
384 widget->signal_request_animation.connect(sigc::mem_fun(this, &Child::request_animation));
387 Container::Child::~Child()
389 visibility_changed(false);
392 void Container::Child::visibility_changed(bool v)
396 if(widget==container.click_focus)
397 container.click_focus = 0;
398 if(widget==container.pointer_focus)
399 container.set_pointer_focus(0);
400 if(widget==container.input_focus)
401 container.set_input_focus(0);
405 void Container::Child::request_focus()
407 if(container.parent && container.visible)
408 container.set_focus();
409 if(container.state&FOCUS)
410 container.set_input_focus(widget);
413 void Container::Child::grab_pointer()
415 if(!container.pointer_grabbed)
417 container.set_pointer_focus(widget, true);
418 container.signal_grab_pointer.emit();
422 void Container::Child::ungrab_pointer()
424 if(container.pointer_grabbed && container.pointer_focus==widget)
426 // XXX Should set to the widget under pointer
427 container.set_pointer_focus(0);
428 container.signal_ungrab_pointer.emit();
432 void Container::Child::request_animation(const Time::TimeDelta &interval)
435 time_since_animate = Time::zero;
436 container.check_animation_interval();