]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/panel.cpp
Rework how widget ownership works in Container
[libs/gltk.git] / source / panel.cpp
index fa16ec7de2826733d04a876f7ec445953bc76a8d..ba66c682e85decb9608c084caeaaa6b5763c0318 100644 (file)
@@ -1,6 +1,5 @@
 #include <msp/core/algorithm.h>
 #include <msp/core/maputils.h>
-#include <msp/core/refptr.h>
 #include "button.h"
 #include "column.h"
 #include "draghandle.h"
@@ -27,23 +26,20 @@ namespace GLtk {
 TypeRegistry<Panel::Loader::AddChildType, Panel::Loader &> Panel::widget_registry;
 bool Panel::widget_registry_init_done = false;
 
-Panel::Panel():
-       layout(0)
+Panel::Panel()
 {
        input_type = INPUT_NAVIGATION;
 }
 
-Panel::~Panel()
+Layout &Panel::get_or_create_layout()
 {
-       delete layout;
-       layout = 0;
-}
+       if(!layout)
+       {
+               layout = make_unique<Layout>();
+               layout->set_container(*this);
+       }
 
-void Panel::set_layout(Layout *l)
-{
-       l->set_container(*this);
-       delete layout;
-       layout = l;
+       return *layout;
 }
 
 void Panel::autosize_special(const Part &part, Geometry &ageom) const
@@ -56,7 +52,7 @@ void Panel::render_special(const Part &part, GL::Renderer &renderer) const
 {
        if(part.get_name()=="children")
        {
-               for(const Container::Child *c: children)
+               for(const unique_ptr<Child> &c: children)
                        if(c->widget->is_visible())
                                c->widget->render(renderer);
        }
@@ -135,9 +131,9 @@ bool Panel::navigate(Navigation nav)
 
 Widget *Panel::find_next_child(int origin_x, int origin_y, int origin_dim, int nav_x, int nav_y) const
 {
-       Widget *sibling = 0;
+       Widget *sibling = nullptr;
        int best_score = 0;
-       for(const Child *c: children)
+       for(const unique_ptr<Child> &c: children)
        {
                if(c->widget==input_focus || !c->widget->is_focusable())
                        continue;
@@ -211,7 +207,7 @@ void Panel::on_child_removed(Widget &wdg)
 Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        DataFile::DerivedObjectLoader<Panel, Widget::Loader>(p),
        wdg_map(m),
-       last_widget(0)
+       last_widget(nullptr)
 {
        if(!widget_registry_init_done)
        {
@@ -243,14 +239,6 @@ Panel::Loader::Loader(Panel &p, map<string, Widget *> &m):
        widget_registry.invoke_all(*this);
 }
 
-Layout &Panel::Loader::get_layout()
-{
-       if(!obj.layout)
-               obj.set_layout(new Layout);
-
-       return *obj.layout;
-}
-
 Widget &Panel::Loader::get_last_widget()
 {
        if(!last_widget)
@@ -262,7 +250,7 @@ Widget &Panel::Loader::get_last_widget()
 template<typename T>
 void Panel::Loader::arrangement()
 {
-       T arr(get_layout());
+       T arr(obj.get_or_create_layout());
        ArrangedLoader<T> ldr(*this, arr);
        load_sub_with(ldr);
 }
@@ -271,44 +259,45 @@ void Panel::Loader::constraint(Layout::ConstraintType type, const string &n)
 {
        Widget &src = get_last_widget();
        Widget &tgt = *get_item(wdg_map, n);
-       get_layout().add_constraint(src, type, tgt);
+       obj.get_or_create_layout().add_constraint(src, type, tgt);
 }
 
 void Panel::Loader::expand(bool h, bool v)
 {
-       get_layout().set_expand(get_last_widget(), h, v);
+       obj.get_or_create_layout().set_expand(get_last_widget(), h, v);
 }
 
 void Panel::Loader::ghost(bool g)
 {
-       get_layout().set_ghost(get_last_widget(), g);
+       obj.get_or_create_layout().set_ghost(get_last_widget(), g);
 }
 
 void Panel::Loader::gravity(int h, int v)
 {
-       get_layout().set_gravity(get_last_widget(), h, v);
+       obj.get_or_create_layout().set_gravity(get_last_widget(), h, v);
 }
 
-void Panel::Loader::grid(unsigned cols)
+void Panel::Loader::grid(size_t cols)
 {
-       Grid grd(get_layout(), cols);
+       Grid grd(obj.get_or_create_layout(), cols);
        ArrangedLoader<Grid> ldr(*this, grd);
        load_sub_with(ldr);
 }
 
 void Panel::Loader::layout()
 {
-       Layout::Loader ldr(get_layout(), wdg_map);
+       Layout::Loader ldr(obj.get_or_create_layout(), wdg_map);
        load_sub_with(ldr);
 }
 
 template<>
 void Panel::Loader::unnamed_child<Panel>()
 {
-       RefPtr<Panel> pnl = new Panel();
+       unique_ptr<Panel> pnl = make_unique<Panel>();
        load_sub(*pnl, wdg_map);
-       obj.add(*pnl.get());
-       last_widget = pnl.release();
+       Widget *wdg = pnl.get();
+       obj.add(move(pnl));
+       last_widget = wdg;
 }