X-Git-Url: http://git.tdb.fi/?a=blobdiff_plain;f=source%2Fcontainer.cpp;h=f1f1934c071d724caaf17086076ccde92c111369;hb=HEAD;hp=d5fa6d59e3d0882fc593611c640af316d6124947;hpb=b4a3d651f57c46507aaa99a71a14fea15f0e430d;p=libs%2Fgltk.git diff --git a/source/container.cpp b/source/container.cpp index d5fa6d5..f1f1934 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -12,51 +12,51 @@ hierarchy_error::hierarchy_error(const string &w): { } -Container::Container(): - click_focus(0), - click_button(0), - pointer_focus(0), - pointer_grabbed(false), - input_focus(0), - saved_input_focus(0), - touch_focus(0), - children_rebuild_needed(false) -{ } - Container::~Container() { + // Clear children here while members are still valid while(!children.empty()) - delete children.front()->widget; + { + if(children.back()->own_widget) + /* Avoid destroying the unique_ptr for the widget from within its own + reset() function */ + unique_ptr w = move(children.back()->own_widget); + else + children.pop_back(); + } } -void Container::add(Widget &wdg) +void Container::add(unique_ptr wdg) { - wdg.set_parent(this); - children.push_back(create_child(&wdg)); - if(wdg.get_animation_interval()) - check_animation_interval(); - children_rebuild_needed = true; - signal_rebuild_needed.emit(); - on_child_added(wdg); + add_child(*wdg).own_widget = move(wdg); } void Container::remove(Widget &wdg) { - auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + auto i = find_if(children, [&wdg](const unique_ptr &c){ return c->widget==&wdg; }); if(i==children.end()) throw hierarchy_error("widget not in container"); - wdg.set_parent(0); - delete *i; + unique_ptr owned = move((*i)->own_widget); + if(&wdg==saved_input_focus) + saved_input_focus = nullptr; + wdg.set_parent(nullptr); children.erase(i); if(wdg.get_animation_interval()) check_animation_interval(); on_child_removed(wdg); } -Container::Child *Container::create_child(Widget *wdg) +Container::Child &Container::add_child(Widget &wdg) { - return new Child(*this, wdg); + wdg.set_parent(this); + children.push_back(make_unique(*this, &wdg)); + if(wdg.get_animation_interval()) + check_animation_interval(); + children_rebuild_needed = true; + signal_rebuild_needed.emit(); + on_child_added(wdg); + return *children.back(); } Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const @@ -89,31 +89,30 @@ void Container::reposition_child(Widget &child, const Part &part) const child.set_geometry(determine_child_geometry(child, part)); } -list Container::get_children() const +vector Container::get_children() const { - list result; - for(const Child *c: children) + vector result; + for(const unique_ptr &c: children) result.push_back(c->widget); return result; } -Widget *Container::get_child_at(int x, int y) const +Widget *Container::find_child_at(int x, int y) const { for(auto i=children.end(); i!=children.begin();) if((*--i)->widget->is_visible() && (*i)->widget->get_geometry().is_inside(x, y)) return (*i)->widget; - return 0; + return nullptr; } -Widget *Container::get_descendant_at(int x, int y) const +Widget *Container::find_descendant_at(int x, int y) const { - Widget *wdg = get_child_at(x, y); + Widget *wdg = find_child_at(x, y); if(Container *cont = dynamic_cast(wdg)) { const Geometry &cgeom = wdg->get_geometry(); - Widget *wdg2 = cont->get_descendant_at(x-cgeom.x, y-cgeom.y); - if(wdg2) + if(Widget *wdg2 = cont->find_descendant_at(x-cgeom.x, y-cgeom.y)) return wdg2; } return wdg; @@ -121,11 +120,13 @@ Widget *Container::get_descendant_at(int x, int y) const void Container::raise(Widget &wdg) { - auto i = find_if(children, [&wdg](const Child *c){ return c->widget==&wdg; }); + auto i = find_if(children, [&wdg](const unique_ptr &c){ return c->widget==&wdg; }); if(i==children.end()) throw hierarchy_error("widget not in container"); - children.splice(children.end(), children, i); + unique_ptr c = move(*i); + children.erase(i); + children.push_back(move(c)); } void Container::set_pointer_focus(Widget *wdg, bool grab) @@ -172,7 +173,7 @@ Widget *Container::get_final_input_focus() const void Container::check_animation_interval() { Time::TimeDelta shortest; - for(const Child *c: children) + for(const unique_ptr &c: children) { const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(child_iv && (!shortest || child_iv &c: children) c->widget->rebuild_hierarchy(); } } @@ -220,9 +221,9 @@ void Container::button_release(int x, int y, unsigned btn) { if(child==click_focus && btn==click_button) { - click_focus = 0; + click_focus = nullptr; if(!pointer_focus) - set_pointer_focus(get_child_at(x, y)); + set_pointer_focus(find_child_at(x, y)); } const Geometry &cgeom = child->get_geometry(); @@ -234,7 +235,7 @@ void Container::pointer_motion(int x, int y) { Widget *child = get_pointer_target(x, y, false); if(!pointer_grabbed) - set_pointer_focus((child && child->get_geometry().is_inside(x, y)) ? child : 0); + set_pointer_focus((child && child->get_geometry().is_inside(x, y)) ? child : nullptr); if(child) { @@ -253,18 +254,18 @@ Widget *Container::get_pointer_target(int x, int y, bool touch) const return touch_focus; else { - Widget *child = get_child_at(x, y); + Widget *child = find_child_at(x, y); if(child && child->is_enabled()) return child; else - return 0; + return nullptr; } } void Container::pointer_leave() { Widget::pointer_leave(); - set_pointer_focus(0); + set_pointer_focus(nullptr); } void Container::touch_press(int x, int y, unsigned finger) @@ -286,7 +287,7 @@ void Container::touch_release(int x, int y, unsigned finger) { // TODO track focus for each finger separately if(child==touch_focus) - touch_focus = 0; + touch_focus = nullptr; const Geometry &cgeom = child->get_geometry(); child->touch_release(x-cgeom.x, y-cgeom.y, finger); @@ -328,7 +329,7 @@ bool Container::character(wchar_t ch) void Container::focus_in() { - if(saved_input_focus && saved_input_focus->get_parent()==this) + if(saved_input_focus) set_input_focus(saved_input_focus); Widget::focus_in(); } @@ -336,7 +337,7 @@ void Container::focus_in() void Container::focus_out() { saved_input_focus = input_focus; - set_input_focus(0); + set_input_focus(nullptr); Widget::focus_out(); } @@ -350,7 +351,7 @@ bool Container::navigate(Navigation nav) void Container::animate(const Time::TimeDelta &dt) { - for(Child *c: children) + for(const unique_ptr &c: children) { const Time::TimeDelta &child_iv = c->widget->get_animation_interval(); if(!child_iv) @@ -368,7 +369,7 @@ void Container::animate(const Time::TimeDelta &dt) void Container::on_reparent() { - for(const Child *c: children) + for(const unique_ptr &c: children) { if(Container *o = dynamic_cast(c->widget)) o->on_reparent(); @@ -395,6 +396,12 @@ Container::Child::Child(Container &c, Widget *w): widget->signal_rebuild_needed.connect(sigc::mem_fun(this, &Child::rebuild_needed)); } +Container::Child::Child(Container &c, unique_ptr w): + Child(c, w.get()) +{ + own_widget = move(w); +} + Container::Child::~Child() { visibility_changed(false); @@ -405,11 +412,11 @@ void Container::Child::visibility_changed(bool v) if(!v) { if(widget==container.click_focus) - container.click_focus = 0; + container.click_focus = nullptr; if(widget==container.pointer_focus) - container.set_pointer_focus(0); + container.set_pointer_focus(nullptr); if(widget==container.input_focus) - container.set_input_focus(0); + container.set_input_focus(nullptr); } } @@ -435,7 +442,7 @@ void Container::Child::ungrab_pointer() if(container.pointer_grabbed && container.pointer_focus==widget) { // XXX Should set to the widget under pointer - container.set_pointer_focus(0); + container.set_pointer_focus(nullptr); container.signal_ungrab_pointer.emit(); } }