From 90d5f5f2ebaeb8aaa4aa47a0c2207f96758cba8c Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Mon, 21 Aug 2023 15:42:10 +0300 Subject: [PATCH] Refactor Panel to create Layout internally on request 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 | 2 +- examples/widgetdemo/demoselector.cpp | 2 +- examples/widgetdemo/dropdowndemo.cpp | 2 +- examples/widgetdemo/entrydemo.cpp | 2 +- examples/widgetdemo/toggledemo.cpp | 10 ++++---- examples/widgetdemo/widgetdemo.cpp | 10 ++++---- source/panel.cpp | 34 ++++++++++++---------------- source/panel.h | 4 +--- 8 files changed, 31 insertions(+), 35 deletions(-) diff --git a/examples/widgetdemo/buttondemo.cpp b/examples/widgetdemo/buttondemo.cpp index f189056..170ce85 100644 --- a/examples/widgetdemo/buttondemo.cpp +++ b/examples/widgetdemo/buttondemo.cpp @@ -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); diff --git a/examples/widgetdemo/demoselector.cpp b/examples/widgetdemo/demoselector.cpp index de8d027..52ba458 100644 --- a/examples/widgetdemo/demoselector.cpp +++ b/examples/widgetdemo/demoselector.cpp @@ -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); diff --git a/examples/widgetdemo/dropdowndemo.cpp b/examples/widgetdemo/dropdowndemo.cpp index 6ee33e0..5ab8782 100644 --- a/examples/widgetdemo/dropdowndemo.cpp +++ b/examples/widgetdemo/dropdowndemo.cpp @@ -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)); diff --git a/examples/widgetdemo/entrydemo.cpp b/examples/widgetdemo/entrydemo.cpp index 64a7bcb..062ce8b 100644 --- a/examples/widgetdemo/entrydemo.cpp +++ b/examples/widgetdemo/entrydemo.cpp @@ -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); diff --git a/examples/widgetdemo/toggledemo.cpp b/examples/widgetdemo/toggledemo.cpp index cc928a0..91849e6 100644 --- a/examples/widgetdemo/toggledemo.cpp +++ b/examples/widgetdemo/toggledemo.cpp @@ -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; } diff --git a/examples/widgetdemo/widgetdemo.cpp b/examples/widgetdemo/widgetdemo.cpp index bfbcfc2..52545a2 100644 --- a/examples/widgetdemo/widgetdemo.cpp +++ b/examples/widgetdemo/widgetdemo.cpp @@ -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); } diff --git a/source/panel.cpp b/source/panel.cpp index 5c189e0..56aa2dc 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -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 &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 void Panel::Loader::arrangement() { - T arr(get_layout()); + T arr(obj.get_or_create_layout()); ArrangedLoader 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 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); } diff --git a/source/panel.h b/source/panel.h index ab83de0..43c0f41 100644 --- a/source/panel.h +++ b/source/panel.h @@ -36,7 +36,6 @@ public: Loader(Panel &, WidgetMap &); private: - Layout &get_layout(); Widget &get_last_widget(); template 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; -- 2.45.2