]> 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 6311a411a57fb1b16e03dcdd24564242a2ac5902..d100dacf94aaa80538194fc77e2d95a486c869b3 100644 (file)
@@ -24,10 +24,12 @@ protected:
        struct Child: public sigc::trackable
        {
                Container &container;
+               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);
@@ -52,9 +54,15 @@ protected:
 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:
+       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;
@@ -102,6 +110,16 @@ protected:
        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