]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor Panel to create Layout internally on request
authorMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 12:42:10 +0000 (15:42 +0300)
committerMikko Rasa <tdb@tdb.fi>
Mon, 21 Aug 2023 12:51:08 +0000 (15:51 +0300)
The Panel always took ownership of the layout anyway, and there seems to
be little point to replace an existing layout with a new one.

examples/widgetdemo/buttondemo.cpp
examples/widgetdemo/demoselector.cpp
examples/widgetdemo/dropdowndemo.cpp
examples/widgetdemo/entrydemo.cpp
examples/widgetdemo/toggledemo.cpp
examples/widgetdemo/widgetdemo.cpp
source/panel.cpp
source/panel.h

index f189056ded2c61353ec6fd86c7a34f61f101ae56..170ce85636fa7b229d9f185f51a07587e95024e9 100644 (file)
@@ -8,7 +8,7 @@ using namespace Msp;
 
 ButtonDemo::ButtonDemo()
 {
-       set_layout(new GLtk::Layout);
+       get_or_create_layout();
 
        GLtk::Label *lbl_message = new GLtk::Label;
        add(*lbl_message);
index de8d02788da66e45dd5f13a2b7d5e888bda17a0e..52ba458eed0b6f9e227ad8c91be64d5c26653cde 100644 (file)
@@ -9,7 +9,7 @@ DemoSelector::DemoSelector():
        demos(&get_demo_title),
        current_demo(0)
 {
-       set_layout(new GLtk::Layout);
+       get_or_create_layout();
        layout->set_margin(GLtk::Sides(8));
 
        GLtk::Column col(*layout);
index 6ee33e0cd9a9c578602a4d2348c2928bd06a6bd3..5ab8782f4211a236dac5751081b2c37f448187fd 100644 (file)
@@ -7,7 +7,7 @@ using namespace Msp;
 
 DropdownDemo::DropdownDemo()
 {
-       set_layout(new GLtk::Layout);
+       get_or_create_layout();
 
        GLtk::Dropdown *drp = new GLtk::Dropdown(categories);
        drp->signal_item_selected.connect(sigc::mem_fun(this, &DropdownDemo::category_selected));
index 64a7bcb1503b735edfef0f064b03e74033ebfb5c..062ce8bf8e581c4c93df4c6eb4736241c148c137 100644 (file)
@@ -6,7 +6,7 @@ using namespace Msp;
 
 EntryDemo::EntryDemo()
 {
-       set_layout(new GLtk::Layout);
+       get_or_create_layout();
 
        GLtk::Label *lbl = new GLtk::Label("Single-line entries:");
        add(*lbl);
index cc928a0fafe66ad40c6039a25bcba2b5679146e2..91849e688ef0a1f81bc0450de9e28441791fc92d 100644 (file)
@@ -7,7 +7,7 @@ using namespace Msp;
 
 ToggleDemo::ToggleDemo()
 {
-       set_layout(new GLtk::Layout);
+       get_or_create_layout();
 
        GLtk::Label *lbl = new GLtk::Label("Standalone toggles:");
        add(*lbl);
@@ -32,7 +32,7 @@ ToggleDemo::ToggleDemo()
                layout->add_constraint(*group, (i==0 ? GLtk::Layout::BELOW : GLtk::Layout::RIGHT_OF), *prev);
                if(i>0)
                        layout->add_constraint(*group, GLtk::Layout::ALIGN_TOP, *prev);
-               group->set_layout(new GLtk::Layout);
+               GLtk::Layout &group_layout = group->get_or_create_layout();
 
                lbl = new GLtk::Label(format("Group %d:", i+1));
                group->add(*lbl);
@@ -45,9 +45,9 @@ ToggleDemo::ToggleDemo()
                        tgl->set_exclusive(true);
                        group->add(*tgl);
 
-                       group->get_layout()->add_constraint(*tgl, GLtk::Layout::BELOW, *prev);
-                       group->get_layout()->add_constraint(*tgl, GLtk::Layout::ALIGN_LEFT, *prev);
-                       group->get_layout()->add_constraint(*tgl, GLtk::Layout::ALIGN_RIGHT, *prev);
+                       group_layout.add_constraint(*tgl, GLtk::Layout::BELOW, *prev);
+                       group_layout.add_constraint(*tgl, GLtk::Layout::ALIGN_LEFT, *prev);
+                       group_layout.add_constraint(*tgl, GLtk::Layout::ALIGN_RIGHT, *prev);
 
                        prev = tgl;
                }
index bfbcfc27c65c5ae07f511aeb9cb8c1cc6e1b41ca..52545a2b2b86c1f099a2a3bc96ff4cbb43d801cd 100644 (file)
@@ -19,10 +19,10 @@ WidgetDemo::WidgetDemo(int, char **):
        window.set_title("GLtk widget demo");
        window.signal_close.connect(sigc::bind(sigc::mem_fun(this, &WidgetDemo::exit), 0));
 
-       root.set_layout(new GLtk::Layout);
+       GLtk::Layout &root_layout = root.get_or_create_layout();
 
        root.add(selector);
-       root.get_layout()->set_expand(selector, false, true);
+       root_layout.set_expand(selector, false, true);
 
        add_demo("Button", new ButtonDemo);
        add_demo("Dropdown", new DropdownDemo);
@@ -46,10 +46,12 @@ void WidgetDemo::tick()
 
 void WidgetDemo::add_demo(const string &title, GLtk::Panel *demo)
 {
+       GLtk::Layout &root_layout = root.get_or_create_layout();
+
        demo->set_visible(false);
        root.add(*demo);
-       root.get_layout()->add_constraint(*demo, GLtk::Layout::RIGHT_OF, selector);
-       root.get_layout()->set_expand(*demo, true, true);
+       root_layout.add_constraint(*demo, GLtk::Layout::RIGHT_OF, selector);
+       root_layout.set_expand(*demo, true, true);
 
        selector.add_demo(title, demo);
 }
index 5c189e0003429b29835efd9a687b97ffe1eb9f06..56aa2dcb5545003fd3feb2975be9fd451bc029ac 100644 (file)
@@ -38,11 +38,15 @@ Panel::~Panel()
        layout = nullptr;
 }
 
-void Panel::set_layout(Layout *l)
+Layout &Panel::get_or_create_layout()
 {
-       l->set_container(*this);
-       delete layout;
-       layout = l;
+       if(!layout)
+       {
+               layout = new Layout;
+               layout->set_container(*this);
+       }
+
+       return *layout;
 }
 
 void Panel::autosize_special(const Part &part, Geometry &ageom) const
@@ -242,14 +246,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)
@@ -261,7 +257,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);
 }
@@ -270,34 +266,34 @@ 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(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);
 }
 
index ab83de078bc17f595f8089112610c3f72fd8cb41..43c0f41b64d91a0241d9211f761e5702f9d5a47e 100644 (file)
@@ -36,7 +36,6 @@ public:
                Loader(Panel &, WidgetMap &);
 
        private:
-               Layout &get_layout();
                Widget &get_last_widget();
                template<typename T>
                void arrangement();
@@ -81,8 +80,7 @@ public:
 
        const char *get_class() const override { return "panel"; }
 
-       void set_layout(Layout *);
-       Layout *get_layout() { return layout; }
+       Layout &get_or_create_layout();
 
 protected:
        void autosize_special(const Part &, Geometry &) const override;