]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/container.h
Rework how widget ownership works in Container
[libs/gltk.git] / source / container.h
index 61502e44177c2d963f617da91fc1e4e5d86a570d..d100dacf94aaa80538194fc77e2d95a486c869b3 100644 (file)
@@ -1,8 +1,9 @@
 #ifndef MSP_GLTK_CONTAINER_H_
 #define MSP_GLTK_CONTAINER_H_
 
-#include <list>
+#include <memory>
 #include <stdexcept>
+#include <vector>
 #include <sigc++/trackable.h>
 #include "mspgltk_api.h"
 #include "widget.h"
@@ -14,7 +15,6 @@ class MSPGLTK_API hierarchy_error: public std::logic_error
 {
 public:
        hierarchy_error(const std::string &);
-       virtual ~hierarchy_error() throw() { }
 };
 
 
@@ -24,10 +24,12 @@ protected:
        struct Child: public sigc::trackable
        {
                Container &container;
-               Widget *widget = 0;
+               std::unique_ptr<Widget> own_widget;
+               Widget *widget = nullptr;
                Time::TimeDelta time_since_animate;
 
                Child(Container &, Widget *);
+               Child(Container &, std::unique_ptr<Widget>);
                virtual ~Child();
 
                void visibility_changed(bool);
@@ -38,31 +40,36 @@ protected:
                void rebuild_needed();
        };
 
-       std::list<Child *> children;
-       Widget *click_focus = 0;
+       std::vector<std::unique_ptr<Child>> children;
+       Widget *click_focus = nullptr;
        unsigned click_button = 0;
-       Widget *pointer_focus = 0;
+       Widget *pointer_focus = nullptr;
        bool pointer_grabbed = false;
-       Widget *input_focus = 0;
-       Widget *saved_input_focus = 0;
-       Widget *touch_focus = 0;
+       Widget *input_focus = nullptr;
+       Widget *saved_input_focus = nullptr;
+       Widget *touch_focus = nullptr;
        bool children_rebuild_needed = false;
 
        Container() = default;
 public:
        virtual ~Container();
 
-       void add(Widget &);
+       void add(Widget &w) { add_child(w); }
+       void add(std::unique_ptr<Widget>);
+
+       template<typename T, typename... Args>
+       T &add_new(Args &&...);
+
        void remove(Widget &);
 protected:
-       virtual Child *create_child(Widget *);
+       Child &add_child(Widget &);
        Geometry determine_child_geometry(const Widget &, const Part &) const;
        void autosize_child(const Widget &, const Part &, Geometry &) const;
        void reposition_child(Widget &, const Part &) const;
 public:
-       std::list<Widget *> get_children() const;
-       Widget *get_child_at(int, int) const;
-       Widget *get_descendant_at(int, int) const;
+       std::vector<Widget *> get_children() const;
+       Widget *find_child_at(int, int) const;
+       Widget *find_descendant_at(int, int) const;
        void raise(Widget &);
 
 protected:
@@ -76,33 +83,43 @@ private:
        void check_animation_interval();
 
 protected:
-       virtual void rebuild_hierarchy();
+       void rebuild_hierarchy() override;
 
 public:
-       virtual void button_press(int, int, unsigned);
-       virtual void button_release(int, int, unsigned);
-       virtual void pointer_motion(int, int);
+       void button_press(int, int, unsigned) override;
+       void button_release(int, int, unsigned) override;
+       void pointer_motion(int, int) override;
 private:
        Widget *get_pointer_target(int, int, bool) const;
 public:
-       virtual void pointer_leave();
-       virtual void touch_press(int, int, unsigned);
-       virtual void touch_release(int, int, unsigned);
-       virtual void touch_motion(int, int, unsigned);
-       virtual bool key_press(unsigned, unsigned);
-       virtual bool key_release(unsigned, unsigned);
-       virtual bool character(wchar_t);
-       virtual void focus_in();
-       virtual void focus_out();
-       virtual bool navigate(Navigation);
-       virtual void animate(const Time::TimeDelta &);
+       void pointer_leave() override;
+       void touch_press(int, int, unsigned) override;
+       void touch_release(int, int, unsigned) override;
+       void touch_motion(int, int, unsigned) override;
+       bool key_press(unsigned, unsigned) override;
+       bool key_release(unsigned, unsigned) override;
+       bool character(wchar_t) override;
+       void focus_in() override;
+       void focus_out() override;
+       bool navigate(Navigation) override;
+       void animate(const Time::TimeDelta &) override;
 protected:
-       virtual void on_reparent();
+       void on_reparent() override;
        virtual void on_child_added(Widget &) { }
        virtual void on_child_removed(Widget &) { }
        virtual void on_input_focus_changed(Widget *);
 };
 
+
+template<typename T, typename... Args>
+T &Container::add_new(Args &&... args)
+{
+       std::unique_ptr<T> wdg = std::make_unique<T>(std::forward<Args>(args)...);
+       T *ptr = wdg.get();
+       add(move(wdg));
+       return *ptr;
+}
+
 } // namespace GLtk
 } // namespace Msp