]> git.tdb.fi Git - libs/gltk.git/commitdiff
Refactor child positioning logic
authorMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 15:47:35 +0000 (17:47 +0200)
committerMikko Rasa <tdb@tdb.fi>
Wed, 20 Nov 2013 16:32:45 +0000 (18:32 +0200)
source/container.cpp
source/container.h
source/entry.cpp
source/entry.h
source/list.cpp
source/list.h

index a806ccd3ce6cd3cecb9cb02f0b8eb9d8051f81dd..7c2e3fb4e8c8afb87b1aa41b49972575a27ffa62 100644 (file)
@@ -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<Widget *> Container::get_children() const
 {
        list<Widget *> result;
index 3275f6a016116ba3f98e4946f5c802e90ccc67eb..bcb3b4ee358537fc6e44ccd40172a4c086d8b89b 100644 (file)
@@ -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<Widget *> get_children() const;
        Widget *get_child_at(int, int);
index 44a169aad7f60283188bc84a3c82c92d2ad56bb8..cee23b02372904317419fc294a17f783fa457589 100644 (file)
@@ -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)
index 541675f7cb60799a3bf7cb3b50ef55154079aa78..fbd5f921054b3a0c17d1ed0a1385475c710831fe 100644 (file)
@@ -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);
 };
index 2a0aa066e98b0a8447df6989a0f7a29ac8a06b8b..042eb20ab7b85dbd052d3274351b03287c09614e 100644 (file)
@@ -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();
index 05ba852949281698a6e4d32fd21706766a42c853..9805176f188da400610f35d17b68546ed6cd27ff 100644 (file)
@@ -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();