]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/container.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / container.cpp
index fade54edbb00e58eb87283bb42f460bf99052dfe..f1f1934c071d724caaf17086076ccde92c111369 100644 (file)
@@ -1,4 +1,6 @@
+#include <msp/core/algorithm.h>
 #include "container.h"
+#include "part.h"
 
 using namespace std;
 
@@ -10,72 +12,107 @@ hierarchy_error::hierarchy_error(const string &w):
 { }
 
 
-Container::Container():
-       click_focus(0),
-       click_button(0),
-       pointer_focus(0),
-       pointer_grabbed(false),
-       input_focus(0)
-{ }
-
 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<Widget> w = move(children.back()->own_widget);
+               else
+                       children.pop_back();
+       }
+}
+
+void Container::add(unique_ptr<Widget> wdg)
+{
+       add_child(*wdg).own_widget = move(wdg);
 }
 
-void Container::add(Widget &wdg)
+void Container::remove(Widget &wdg)
+{
+       auto i = find_if(children, [&wdg](const unique_ptr<Child> &c){ return c->widget==&wdg; });
+       if(i==children.end())
+               throw hierarchy_error("widget not in container");
+
+       unique_ptr<Widget> 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::add_child(Widget &wdg)
 {
        wdg.set_parent(this);
-       children.push_back(create_child(&wdg));
+       children.push_back(make_unique<Child>(*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();
 }
 
-void Container::remove(Widget &wdg)
+Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const
 {
-       for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
-               if((*i)->widget==&wdg)
-               {
-                       wdg.set_parent(0);
-                       delete *i;
-                       children.erase(i);
-                       on_child_removed(wdg);
-                       return;
-               }
+       Geometry pgeom = part.get_geometry();
+       if(!pgeom.w || !pgeom.h)
+       {
+               Geometry cgeom;
+               child.autosize(cgeom);
+               if(!pgeom.w)
+                       pgeom.w = cgeom.w;
+               if(!pgeom.h)
+                       pgeom.h = cgeom.h;
+       }
 
-       throw hierarchy_error("widget not in container");
+       part.get_alignment().apply(pgeom, geom, part.get_margin());
+       return pgeom;
 }
 
-Container::Child *Container::create_child(Widget *wdg)
+void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const
 {
-       return new Child(*this, wdg);
+       Geometry cgeom = determine_child_geometry(child, part);
+       const Sides &margin = part.get_margin();
+       ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right);
+       ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom);
 }
 
-list<Widget *> Container::get_children() const
+void Container::reposition_child(Widget &child, const Part &part) const
 {
-       list<Widget *> result;
-       for(list<Child *>::const_iterator i=children.begin(); i!=children.end(); ++i)
-               result.push_back((*i)->widget);
+       child.set_geometry(determine_child_geometry(child, part));
+}
+
+vector<Widget *> Container::get_children() const
+{
+       vector<Widget *> result;
+       for(const unique_ptr<Child> &c: children)
+               result.push_back(c->widget);
        return result;
 }
 
-Widget *Container::get_child_at(int x, int y)
+Widget *Container::find_child_at(int x, int y) const
 {
-       for(list<Child *>::iterator i=children.end(); i!=children.begin();)
+       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)
+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<Container *>(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;
@@ -83,17 +120,16 @@ Widget *Container::get_descendant_at(int x, int y)
 
 void Container::raise(Widget &wdg)
 {
-       for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
-               if((*i)->widget==&wdg)
-               {
-                       children.splice(children.end(), children, i);
-                       return;
-               }
+       auto i = find_if(children, [&wdg](const unique_ptr<Child> &c){ return c->widget==&wdg; });
+       if(i==children.end())
+               throw hierarchy_error("widget not in container");
 
-       throw hierarchy_error("widget not in container");
+       unique_ptr<Child> c = move(*i);
+       children.erase(i);
+       children.push_back(move(c));
 }
 
-void Container::set_pointer_focus(Widget *wdg)
+void Container::set_pointer_focus(Widget *wdg, bool grab)
 {
        if(wdg!=pointer_focus)
        {
@@ -101,10 +137,13 @@ void Container::set_pointer_focus(Widget *wdg)
                        pointer_focus->pointer_leave();
 
                pointer_focus = wdg;
+               pointer_grabbed = grab;
 
                if(pointer_focus)
                        pointer_focus->pointer_enter();
        }
+       else
+               pointer_grabbed = grab;
 }
 
 void Container::set_input_focus(Widget *wdg)
@@ -115,12 +154,10 @@ void Container::set_input_focus(Widget *wdg)
                        input_focus->focus_out();
 
                input_focus = wdg;
+               on_input_focus_changed(input_focus);
 
                if(input_focus)
-               {
-                       raise(*wdg);
                        input_focus->focus_in();
-               }
        }
 }
 
@@ -133,135 +170,219 @@ Widget *Container::get_final_input_focus() const
        return input_focus;
 }
 
-void Container::button_press(int x, int y, unsigned btn)
+void Container::check_animation_interval()
 {
-       if(pointer_grabbed)
-       {
-               const Geometry &cgeom = pointer_focus->get_geometry();
-               pointer_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
-       }
-       else
+       Time::TimeDelta shortest;
+       for(const unique_ptr<Child> &c: children)
        {
-               if(Widget *wdg = get_child_at(x, y))
-               {
-                       set_pointer_focus(wdg);
-                       if(wdg->is_focusable())
-                               set_input_focus(wdg);
-               }
-               if(click_focus)
-               {
-                       const Geometry &cgeom = click_focus->get_geometry();
-                       click_focus->button_press(x-cgeom.x, y-cgeom.y, btn);
-               }
-               else
-               {
-                       if(Widget *wdg = get_child_at(x, y))
-                       {
-                               click_focus = wdg;
-                               click_button = btn;
-
-                               const Geometry &cgeom = wdg->get_geometry();
-                               wdg->button_press(x-cgeom.x, y-cgeom.y, btn);
-                       }
-               }
+               const Time::TimeDelta &child_iv = c->widget->get_animation_interval();
+               if(child_iv && (!shortest || child_iv<shortest))
+                       shortest = child_iv;
        }
+
+       if(shortest!=anim_interval)
+               set_animation_interval(shortest);
 }
 
-void Container::button_release(int x, int y, unsigned btn)
+void Container::rebuild_hierarchy()
 {
-       if(pointer_grabbed)
+       Widget::rebuild_hierarchy();
+
+       if(children_rebuild_needed)
        {
-               const Geometry &cgeom = pointer_focus->get_geometry();
-               pointer_focus->button_release(x-cgeom.x, y-cgeom.y, btn);
+               children_rebuild_needed = false;
+               for(const unique_ptr<Child> &c: children)
+                       c->widget->rebuild_hierarchy();
        }
-       else if(click_focus)
+}
+
+void Container::button_press(int x, int y, unsigned btn)
+{
+       if(Widget *child = get_pointer_target(x, y, false))
        {
-               Widget *wdg = click_focus;
+               if(!click_focus)
+               {
+                       set_pointer_focus(child);
+                       if(child->is_focusable())
+                               set_input_focus(child);
 
-               if(btn==click_button)
-                       click_focus = 0;
+                       click_focus = child;
+                       click_button = btn;
+               }
 
-               const Geometry &cgeom = wdg->get_geometry();
-               wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
+               const Geometry &cgeom = child->get_geometry();
+               child->button_press(x-cgeom.x, y-cgeom.y, btn);
        }
-       else
+}
+
+void Container::button_release(int x, int y, unsigned btn)
+{
+       if(Widget *child = get_pointer_target(x, y, false))
        {
-               if(Widget *wdg = get_child_at(x, y))
+               if(child==click_focus && btn==click_button)
                {
-                       const Geometry &cgeom = wdg->get_geometry();
-                       wdg->button_release(x-cgeom.x, y-cgeom.y, btn);
+                       click_focus = nullptr;
+                       if(!pointer_focus)
+                               set_pointer_focus(find_child_at(x, y));
                }
+
+               const Geometry &cgeom = child->get_geometry();
+               child->button_release(x-cgeom.x, y-cgeom.y, btn);
        }
 }
 
 void Container::pointer_motion(int x, int y)
 {
-       if(pointer_grabbed)
+       Widget *child = get_pointer_target(x, y, false);
+       if(!pointer_grabbed)
+               set_pointer_focus((child && child->get_geometry().is_inside(x, y)) ? child : nullptr);
+
+       if(child)
        {
-               const Geometry &cgeom = pointer_focus->get_geometry();
-               pointer_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
+               const Geometry &cgeom = child->get_geometry();
+               child->pointer_motion(x-cgeom.x, y-cgeom.y);
        }
+}
+
+Widget *Container::get_pointer_target(int x, int y, bool touch) const
+{
+       if(pointer_grabbed)
+               return pointer_focus;
+       else if(!touch && click_focus)
+               return click_focus;
+       else if(touch && touch_focus)
+               return touch_focus;
        else
        {
-               set_pointer_focus(get_child_at(x, y));
-               if(click_focus)
-               {
-                       const Geometry &cgeom = click_focus->get_geometry();
-                       click_focus->pointer_motion(x-cgeom.x, y-cgeom.y);
-               }
+               Widget *child = find_child_at(x, y);
+               if(child && child->is_enabled())
+                       return child;
                else
-               {
-                       Widget *wdg = get_child_at(x, y);
-                       if(wdg)
-                       {
-                               const Geometry &cgeom = wdg->get_geometry();
-                               wdg->pointer_motion(x-cgeom.x, y-cgeom.y);
-                       }
-               }
+                       return nullptr;
        }
 }
 
 void Container::pointer_leave()
 {
        Widget::pointer_leave();
-       click_focus = 0;
-       set_pointer_focus(0);
+       set_pointer_focus(nullptr);
+}
+
+void Container::touch_press(int x, int y, unsigned finger)
+{
+       if(Widget *child = get_pointer_target(x, y, true))
+       {
+               // TODO track focus for each finger separately
+               if(!touch_focus)
+                       touch_focus = child;
+
+               const Geometry &cgeom = child->get_geometry();
+               child->touch_press(x-cgeom.x, y-cgeom.y, finger);
+       }
 }
 
-void Container::key_press(unsigned key, unsigned mod)
+void Container::touch_release(int x, int y, unsigned finger)
 {
-       if(input_focus)
-               input_focus->key_press(key, mod);
+       if(Widget *child = get_pointer_target(x, y, true))
+       {
+               // TODO track focus for each finger separately
+               if(child==touch_focus)
+                       touch_focus = nullptr;
+
+               const Geometry &cgeom = child->get_geometry();
+               child->touch_release(x-cgeom.x, y-cgeom.y, finger);
+       }
+}
+
+void Container::touch_motion(int x, int y, unsigned finger)
+{
+       if(Widget *child = get_pointer_target(x, y, true))
+       {
+               const Geometry &cgeom = child->get_geometry();
+               child->touch_motion(x-cgeom.x, y-cgeom.y, finger);
+       }
 }
 
-void Container::key_release(unsigned key, unsigned mod)
+bool Container::key_press(unsigned key, unsigned mod)
 {
-       if(input_focus)
-               input_focus->key_release(key, mod);
+       if(input_focus && input_focus->is_enabled())
+               return input_focus->key_press(key, mod);
+       else
+               return false;
 }
 
-void Container::character(wchar_t ch)
+bool Container::key_release(unsigned key, unsigned mod)
 {
-       if(input_focus)
-               input_focus->character(ch);
+       if(input_focus && input_focus->is_enabled())
+               return input_focus->key_release(key, mod);
+       else
+               return false;
+}
+
+bool Container::character(wchar_t ch)
+{
+       if(input_focus && input_focus->is_enabled())
+               return input_focus->character(ch);
+       else
+               return false;
+}
+
+void Container::focus_in()
+{
+       if(saved_input_focus)
+               set_input_focus(saved_input_focus);
+       Widget::focus_in();
 }
 
 void Container::focus_out()
 {
-       set_input_focus(0);
+       saved_input_focus = input_focus;
+       set_input_focus(nullptr);
        Widget::focus_out();
 }
 
+bool Container::navigate(Navigation nav)
+{
+       if(input_focus && input_focus->is_enabled())
+               return input_focus->navigate(nav);
+       else
+               return false;
+}
+
+void Container::animate(const Time::TimeDelta &dt)
+{
+       for(const unique_ptr<Child> &c: children)
+       {
+               const Time::TimeDelta &child_iv = c->widget->get_animation_interval();
+               if(!child_iv)
+                       continue;
+
+               c->time_since_animate += dt;
+               if(c->time_since_animate>=child_iv)
+               {
+                       Time::TimeDelta child_dt = c->time_since_animate;
+                       c->time_since_animate = min(c->time_since_animate-child_iv, child_iv);
+                       c->widget->animate(child_dt);
+               }
+       }
+}
+
 void Container::on_reparent()
 {
-       for(list<Child *>::iterator i=children.begin(); i!=children.end(); ++i)
+       for(const unique_ptr<Child> &c: children)
        {
-               if(Container *c = dynamic_cast<Container *>((*i)->widget))
-                       c->on_reparent();
-               (*i)->widget->update_style();
+               if(Container *o = dynamic_cast<Container *>(c->widget))
+                       o->on_reparent();
+               c->widget->update_style();
        }
 }
 
+void Container::on_input_focus_changed(Widget *wdg)
+{
+       if(wdg)
+               raise(*wdg);
+}
+
 
 Container::Child::Child(Container &c, Widget *w):
        container(c),
@@ -271,6 +392,14 @@ Container::Child::Child(Container &c, Widget *w):
        widget->signal_request_focus.connect(sigc::mem_fun(this, &Child::request_focus));
        widget->signal_grab_pointer.connect(sigc::mem_fun(this, &Child::grab_pointer));
        widget->signal_ungrab_pointer.connect(sigc::mem_fun(this, &Child::ungrab_pointer));
+       widget->signal_request_animation.connect(sigc::mem_fun(this, &Child::request_animation));
+       widget->signal_rebuild_needed.connect(sigc::mem_fun(this, &Child::rebuild_needed));
+}
+
+Container::Child::Child(Container &c, unique_ptr<Widget> w):
+       Child(c, w.get())
+{
+       own_widget = move(w);
 }
 
 Container::Child::~Child()
@@ -283,27 +412,27 @@ 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);
        }
 }
 
 void Container::Child::request_focus()
 {
-       container.set_input_focus(widget);
        if(container.parent && container.visible)
                container.set_focus();
+       if(container.state&FOCUS)
+               container.set_input_focus(widget);
 }
 
 void Container::Child::grab_pointer()
 {
        if(!container.pointer_grabbed)
        {
-               container.set_pointer_focus(widget);
-               container.pointer_grabbed = true;
+               container.set_pointer_focus(widget, true);
                container.signal_grab_pointer.emit();
        }
 }
@@ -313,11 +442,23 @@ 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.pointer_grabbed = false;
+               container.set_pointer_focus(nullptr);
                container.signal_ungrab_pointer.emit();
        }
 }
 
+void Container::Child::request_animation(const Time::TimeDelta &interval)
+{
+       if(!interval)
+               time_since_animate = Time::zero;
+       container.check_animation_interval();
+}
+
+void Container::Child::rebuild_needed()
+{
+       container.children_rebuild_needed = true;
+       container.signal_rebuild_needed.emit();
+}
+
 } // namespace GLtk
 } // namespace Msp