From 1afb4f8d6379ad1bf832692fbfedef9714ee3ff5 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 20 Nov 2013 17:47:35 +0200 Subject: [PATCH] Refactor child positioning logic --- source/container.cpp | 31 +++++++++++++++++++++++++ source/container.h | 3 +++ source/entry.cpp | 54 ++++++++------------------------------------ source/entry.h | 1 - source/list.cpp | 24 ++++++++------------ source/list.h | 2 +- 6 files changed, 54 insertions(+), 61 deletions(-) diff --git a/source/container.cpp b/source/container.cpp index a806ccd..7c2e3fb 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -1,4 +1,5 @@ #include "container.h" +#include "part.h" using namespace std; @@ -51,6 +52,36 @@ Container::Child *Container::create_child(Widget *wdg) return new Child(*this, wdg); } +Geometry Container::determine_child_geometry(const Widget &child, const Part &part) const +{ + Geometry pgeom = part.get_geometry(); + if(!pgeom.w || !pgeom.h) + { + Geometry cgeom; + child.autosize(cgeom); + if(!pgeom.w) + pgeom.w = cgeom.w; + if(!pgeom.h) + pgeom.h = cgeom.h; + } + + part.get_alignment().apply(pgeom, geom, part.get_margin()); + return pgeom; +} + +void Container::autosize_child(const Widget &child, const Part &part, Geometry &ageom) const +{ + Geometry cgeom = determine_child_geometry(child, part); + const Sides &margin = part.get_margin(); + ageom.w = max(ageom.w, cgeom.w+margin.left+margin.right); + ageom.h = max(ageom.h, cgeom.h+margin.top+margin.bottom); +} + +void Container::reposition_child(Widget &child, const Part &part) const +{ + child.set_geometry(determine_child_geometry(child, part)); +} + list Container::get_children() const { list result; diff --git a/source/container.h b/source/container.h index 3275f6a..bcb3b4e 100644 --- a/source/container.h +++ b/source/container.h @@ -49,6 +49,9 @@ public: void remove(Widget &); protected: virtual Child *create_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 get_children() const; Widget *get_child_at(int, int); diff --git a/source/entry.cpp b/source/entry.cpp index 44a169a..cee23b0 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -47,22 +47,7 @@ void Entry::autosize_special(const Part &part, Geometry &ageom) const ageom.h = max(ageom.h, line_height+margin.top+margin.bottom); } else if(part.get_name()=="slider" && multiline) - { - Geometry sgeom = part.get_geometry(); - if(!sgeom.w || !sgeom.h) - { - Geometry wgeom; - slider->autosize(wgeom); - if(!sgeom.w) - sgeom.w = wgeom.w; - if(!sgeom.h) - sgeom.h = wgeom.h; - } - - const Sides &margin = part.get_margin(); - ageom.w = max(ageom.w, sgeom.w+margin.left+margin.right); - ageom.h = max(ageom.h, sgeom.h+margin.top+margin.bottom); - } + autosize_child(*slider, part, ageom); } void Entry::set_text(const string &t) @@ -94,7 +79,7 @@ void Entry::set_multiline(bool m) add(*slider); slider->set_step(1); slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed)); - reposition_slider(); + rebuild(); } check_view_range(); } @@ -122,6 +107,14 @@ void Entry::rebuild_special(const Part &part) bld.matrix() *= GL::Matrix::translation(rgeom.x, rgeom.y, 0); graphic->build(part.get_geometry().w, part.get_geometry().h, bld); } + else if(part.get_name()=="slider") + { + if(multiline) + { + reposition_child(*slider, part); + Widget::rebuild_special(part); + } + } else Widget::rebuild_special(part); } @@ -197,8 +190,6 @@ void Entry::focus_out() void Entry::on_geometry_change() { - reposition_slider(); - if(multiline) check_view_range(); } @@ -215,8 +206,6 @@ void Entry::on_style_change() text_part = style->get_part("text"); - reposition_slider(); - if(multiline) check_view_range(); } @@ -228,29 +217,6 @@ void Entry::set_edit_position(unsigned ep) rebuild(); } -void Entry::reposition_slider() -{ - if(!style || !slider) - return; - - if(const Part *slider_part = style->get_part("slider")) - { - Geometry sgeom = slider_part->get_geometry(); - if(!sgeom.w || !sgeom.h) - { - Geometry wgeom; - slider->autosize(wgeom); - if(!sgeom.w) - sgeom.w = wgeom.w; - if(!sgeom.h) - sgeom.h = wgeom.h; - } - - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider->set_geometry(sgeom); - } -} - void Entry::check_view_range() { if(!multiline || !text_part) diff --git a/source/entry.h b/source/entry.h index 541675f..fbd5f92 100644 --- a/source/entry.h +++ b/source/entry.h @@ -77,7 +77,6 @@ private: virtual void on_style_change(); void set_edit_position(unsigned); - void reposition_slider(); void check_view_range(); void slider_value_changed(double); }; diff --git a/source/list.cpp b/source/list.cpp index 2a0aa06..042eb20 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -78,6 +78,8 @@ void List::autosize_special(const Part &part, Geometry &ageom) const ageom.w = max(ageom.w, max_w+margin.left+margin.right); ageom.h = max(ageom.h, total_h+margin.top+margin.bottom); } + else if(part.get_name()=="slider") + autosize_child(slider, part, ageom); } void List::set_data(ListData &d) @@ -156,6 +158,13 @@ void List::set_selected_index(int i) } } +void List::rebuild_special(const Part &part) +{ + if(part.get_name()=="slider") + reposition_child(slider, part); + Widget::rebuild_special(part); +} + void List::render_special(const Part &part, GL::Renderer &renderer) const { if(part.get_name()=="items") @@ -183,7 +192,6 @@ void List::button_press(int x, int y, unsigned btn) void List::on_geometry_change() { - reposition_slider(); reposition_items(); check_view_range(); @@ -194,25 +202,11 @@ void List::on_style_change() if(!style) return; - reposition_slider(); reposition_items(); check_view_range(); } -void List::reposition_slider() -{ - if(!style) - return; - - if(const Part *slider_part = style->get_part("slider")) - { - Geometry sgeom = slider_part->get_geometry(); - slider_part->get_alignment().apply(sgeom, geom, slider_part->get_margin()); - slider.set_geometry(sgeom); - } -} - void List::item_autosize_changed() { signal_autosize_changed.emit(); diff --git a/source/list.h b/source/list.h index 05ba852..9805176 100644 --- a/source/list.h +++ b/source/list.h @@ -183,6 +183,7 @@ public: int get_selected_index() const { return sel_index; } private: + virtual void rebuild_special(const Part &); virtual void render_special(const Part &, GL::Renderer &) const; public: @@ -191,7 +192,6 @@ private: virtual void on_geometry_change(); virtual void on_style_change(); - void reposition_slider(); void item_autosize_changed(); void reposition_items(); void check_view_range(); -- 2.45.2