From 707b59d45ae50b69c94918f8f74313283b304597 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Wed, 6 Nov 2013 13:12:24 +0200 Subject: [PATCH] Make autosize_special const and add a const autosize overload This should reduce useless back-and-forth resizing of widgets in several common patterns. --- source/button.cpp | 2 +- source/button.h | 2 +- source/dropdown.cpp | 7 +++---- source/dropdown.h | 2 +- source/entry.cpp | 2 +- source/entry.h | 2 +- source/hslider.cpp | 2 +- source/hslider.h | 2 +- source/image.cpp | 2 +- source/image.h | 2 +- source/label.cpp | 2 +- source/label.h | 2 +- source/layout.cpp | 9 +++------ source/list.cpp | 4 ++-- source/list.h | 4 ++-- source/panel.cpp | 2 +- source/panel.h | 2 +- source/toggle.cpp | 2 +- source/toggle.h | 2 +- source/vslider.cpp | 2 +- source/vslider.h | 2 +- source/widget.cpp | 13 ++++++++----- source/widget.h | 3 ++- 23 files changed, 37 insertions(+), 37 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index 8fd2de6..57e5fa4 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -16,7 +16,7 @@ Button::Button(const std::string &t): set_text(t); } -void Button::autosize_special(const Part &part, Geometry &ageom) +void Button::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="text") text.autosize(part, ageom); diff --git a/source/button.h b/source/button.h index edf0b62..7904515 100644 --- a/source/button.h +++ b/source/button.h @@ -37,7 +37,7 @@ public: virtual const char *get_class() const { return "button"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_text(const std::string &); diff --git a/source/dropdown.cpp b/source/dropdown.cpp index 50aced9..013e1d9 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -33,11 +33,12 @@ void Dropdown::init() list.signal_autosize_changed.connect(sigc::mem_fun(this, &Dropdown::list_autosize_changed)); } -void Dropdown::autosize_special(const Part &part, Geometry &ageom) +void Dropdown::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="list") { - list.autosize(); + Geometry lgeom; + list.autosize(lgeom); ageom.w = max(ageom.w, list.get_geometry().w); } else if(part.get_name()=="text") @@ -58,8 +59,6 @@ void Dropdown::autosize_special(const Part &part, Geometry &ageom) unsigned line_height = static_cast((font.get_ascent()-font.get_descent())*font_size); ageom.h = max(ageom.h, line_height+margin.top+margin.bottom); } - - rebuild(); } void Dropdown::set_selected_index(int index) diff --git a/source/dropdown.h b/source/dropdown.h index 06aefa5..24c62cf 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -39,7 +39,7 @@ public: virtual const char *get_class() const { return "dropdown"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_data(ListData &d) { list.set_data(d); } diff --git a/source/entry.cpp b/source/entry.cpp index f21aacb..1198bde 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -28,7 +28,7 @@ Entry::Entry(const string &t): set_text(t); } -void Entry::autosize_special(const Part &part, Geometry &ageom) +void Entry::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="text") { diff --git a/source/entry.h b/source/entry.h index 4854340..541675f 100644 --- a/source/entry.h +++ b/source/entry.h @@ -51,7 +51,7 @@ public: virtual const char *get_class() const { return "entry"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_text(const std::string &); diff --git a/source/hslider.cpp b/source/hslider.cpp index 2c6e968..dcb7634 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -12,7 +12,7 @@ HSlider::HSlider(): slider_size(1) { } -void HSlider::autosize_special(const Part &part, Geometry &ageom) +void HSlider::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="slider") { diff --git a/source/hslider.h b/source/hslider.h index cadae59..774c736 100644 --- a/source/hslider.h +++ b/source/hslider.h @@ -21,7 +21,7 @@ public: virtual const char *get_class() const { return "hslider"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; virtual void rebuild_special(const Part &); public: diff --git a/source/image.cpp b/source/image.cpp index 8e14328..5dcb2e3 100644 --- a/source/image.cpp +++ b/source/image.cpp @@ -15,7 +15,7 @@ Image::Image(const GL::Texture2D *i): focusable = false; } -void Image::autosize_special(const Part &part, Geometry &ageom) +void Image::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="image") { diff --git a/source/image.h b/source/image.h index 8c5a9d5..3194216 100644 --- a/source/image.h +++ b/source/image.h @@ -22,7 +22,7 @@ public: virtual const char *get_class() const { return "image"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_image(const GL::Texture2D *); diff --git a/source/label.cpp b/source/label.cpp index 2de02f6..c59e718 100644 --- a/source/label.cpp +++ b/source/label.cpp @@ -20,7 +20,7 @@ void Label::set_text(const string &t) rebuild(); } -void Label::autosize_special(const Part &part, Geometry &ageom) +void Label::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="text") text.autosize(part, ageom); diff --git a/source/label.h b/source/label.h index f227934..e3cff69 100644 --- a/source/label.h +++ b/source/label.h @@ -32,7 +32,7 @@ public: void set_text(const std::string &); private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; virtual void rebuild_special(const Part &); virtual void on_style_change(); diff --git a/source/layout.cpp b/source/layout.cpp index 5ebb55c..bee972d 100644 --- a/source/layout.cpp +++ b/source/layout.cpp @@ -470,16 +470,13 @@ Layout::Slot::Slot(Layout &l, Widget &w): void Layout::Slot::autosize_changed() { - widget.autosize(); - autosize_geom = widget.get_geometry(); + widget.autosize(autosize_geom); if(!widget.is_visible() && !ghost) return; - // If the widget fits in the area it had, just leave it there. - if(autosize_geom.w<=geom.w && autosize_geom.h<=geom.h) - widget.set_geometry(geom); - else + // Only trigger an update if the widget won't fit in its current area. + if(autosize_geom.w>geom.w || autosize_geom.h>geom.h) { layout.container->signal_autosize_changed.emit(); layout.update(); diff --git a/source/list.cpp b/source/list.cpp index 653a40c..461d5e4 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -55,7 +55,7 @@ List::~List() delete data; } -void List::autosize_special(const Part &part, Geometry &ageom) +void List::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="items") { @@ -344,7 +344,7 @@ void List::DataObserver::refresh_item(unsigned i) } -void List::Item::autosize_special(const Part &part, Geometry &ageom) +void List::Item::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="children") { diff --git a/source/list.h b/source/list.h index dd3ddaa..6f6aec1 100644 --- a/source/list.h +++ b/source/list.h @@ -61,7 +61,7 @@ public: virtual const char *get_class() const { return "listitem"; } protected: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_active(bool); @@ -145,7 +145,7 @@ public: virtual const char *get_class() const { return "list"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_data(ListData &); diff --git a/source/panel.cpp b/source/panel.cpp index 23657de..f1eef11 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -41,7 +41,7 @@ void Panel::set_layout(Layout *l) layout = l; } -void Panel::autosize_special(const Part &part, Geometry &ageom) +void Panel::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="children" && layout) layout->autosize(ageom); diff --git a/source/panel.h b/source/panel.h index d224596..165cc6a 100644 --- a/source/panel.h +++ b/source/panel.h @@ -68,7 +68,7 @@ public: Layout *get_layout() { return layout; } protected: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; virtual void render_special(const Part &, GL::Renderer &) const; virtual void on_geometry_change(); diff --git a/source/toggle.cpp b/source/toggle.cpp index 988b0a5..b597eb9 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -17,7 +17,7 @@ Toggle::Toggle(const string &t): set_text(t); } -void Toggle::autosize_special(const Part &part, Geometry &ageom) +void Toggle::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="text") text.autosize(part, ageom); diff --git a/source/toggle.h b/source/toggle.h index 9bba268..54becb9 100644 --- a/source/toggle.h +++ b/source/toggle.h @@ -37,7 +37,7 @@ public: virtual const char *get_class() const { return "toggle"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; public: void set_text(const std::string &); diff --git a/source/vslider.cpp b/source/vslider.cpp index e4c40d8..d32c57d 100644 --- a/source/vslider.cpp +++ b/source/vslider.cpp @@ -12,7 +12,7 @@ VSlider::VSlider(): slider_size(1) { } -void VSlider::autosize_special(const Part &part, Geometry &ageom) +void VSlider::autosize_special(const Part &part, Geometry &ageom) const { if(part.get_name()=="slider") { diff --git a/source/vslider.h b/source/vslider.h index a994aac..ee12689 100644 --- a/source/vslider.h +++ b/source/vslider.h @@ -17,7 +17,7 @@ public: virtual const char *get_class() const { return "vslider"; } private: - virtual void autosize_special(const Part &, Geometry &); + virtual void autosize_special(const Part &, Geometry &) const; virtual void rebuild_special(const Part &); public: diff --git a/source/widget.cpp b/source/widget.cpp index 59105b9..e5c58a2 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -39,13 +39,18 @@ void Widget::set_size(unsigned w, unsigned h) } void Widget::autosize() +{ + Geometry ageom; + autosize(ageom); + set_geometry(ageom); +} + +void Widget::autosize(Geometry &ageom) const { if(!style) return; - Geometry ageom; - ageom.x = geom.x; - ageom.y = geom.y; + ageom = Geometry(geom.x, geom.y, 0, 0); const Style::PartSeq &parts = style->get_parts(); for(Style::PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) { @@ -59,8 +64,6 @@ void Widget::autosize() else autosize_special(*i, ageom); } - - set_geometry(ageom); } void Widget::set_geometry(const Geometry &g) diff --git a/source/widget.h b/source/widget.h index 461cafe..9caae9f 100644 --- a/source/widget.h +++ b/source/widget.h @@ -65,8 +65,9 @@ public: void set_position(int, int); void set_size(unsigned, unsigned); void autosize(); + void autosize(Geometry &) const; protected: - virtual void autosize_special(const Part &, Geometry &) { } + virtual void autosize_special(const Part &, Geometry &) const { }; public: void set_geometry(const Geometry &); const Geometry &get_geometry() const { return geom; } -- 2.45.2