From 6081ed9020ad22214a8e5b3829092f97b12d7c71 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Tue, 20 Apr 2010 07:57:24 +0000 Subject: [PATCH] Make the Text class interface more obvious by not using a reference to pointer --- source/button.cpp | 4 ++-- source/dropdown.cpp | 2 +- source/entry.cpp | 4 ++-- source/label.cpp | 4 ++-- source/text.cpp | 24 +++++++++++++----------- source/text.h | 10 +++++----- source/toggle.cpp | 4 ++-- 7 files changed, 27 insertions(+), 25 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index 292625d..e754838 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -14,7 +14,7 @@ namespace GLtk { Button::Button(const Resources &r, const std::string &t): Widget(r), - text(style), + text(), icon(0), pressed(false) { @@ -94,7 +94,7 @@ void Button::render_special(const Part &part) const void Button::on_style_change() { - text.update_style(); + text.set_style(style); } diff --git a/source/dropdown.cpp b/source/dropdown.cpp index e0710a8..d7119b7 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -99,7 +99,7 @@ void Dropdown::render_special(const Part &part) const if(part.get_name()=="text") { if(list.get_selected_index()>=0) - Text(style, list.get_selected()).render(part, geom); + Text(*style, list.get_selected()).render(part, geom); } else if(part.get_name()=="list" && dropped) list.render(); diff --git a/source/entry.cpp b/source/entry.cpp index 496a79d..28b454d 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -21,7 +21,7 @@ namespace GLtk { Entry::Entry(const Resources &r, const string &t): Widget(r), - text(style), + text(), edit_pos(0) { update_style(); @@ -86,7 +86,7 @@ void Entry::render_special(const Part &part) const void Entry::on_style_change() { - text.update_style(); + text.set_style(style); } diff --git a/source/label.cpp b/source/label.cpp index 18bef1a..a91f821 100644 --- a/source/label.cpp +++ b/source/label.cpp @@ -16,7 +16,7 @@ namespace GLtk { Label::Label(const Resources &r, const string &t): Widget(r), - text(style) + text() { focusable=false; update_style(); @@ -54,7 +54,7 @@ void Label::render_special(const Part &part) const void Label::on_style_change() { - text.update_style(); + text.set_style(style); } diff --git a/source/text.cpp b/source/text.cpp index d48c33f..31db228 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -8,16 +8,25 @@ using namespace std; namespace Msp { namespace GLtk { -Text::Text(const Style *const &s): - style(s) +Text::Text(): + style(0) { } -Text::Text(const Style *const &s, const string &t): - style(s) +Text::Text(const Style &s, const string &t): + style(&s) { set(t); } +void Text::set_style(const Style *s) +{ + style = s; + + float font_size=style->get_font()->get_default_size(); + for(vector::iterator i=lines.begin(); i!=lines.end(); ++i) + i->width=static_cast(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size); +} + unsigned Text::get_width() const { unsigned width=0; @@ -77,13 +86,6 @@ void Text::insert(unsigned pos, const string &s) } } -void Text::update_style() -{ - float font_size=style->get_font()->get_default_size(); - for(vector::iterator i=lines.begin(); i!=lines.end(); ++i) - i->width=static_cast(style->get_font()->get_string_width(text.substr(i->start, i->length))*font_size); -} - void Text::render(const Part &part, const Geometry &geom) const { if(lines.empty()) diff --git a/source/text.h b/source/text.h index 7be1192..d964aa1 100644 --- a/source/text.h +++ b/source/text.h @@ -24,13 +24,15 @@ private: unsigned width; }; - const Style *const &style; + const Style *style; std::string text; std::vector lines; public: - Text(const Style *const &); - Text(const Style *const &, const std::string &); + Text(); + Text(const Style &, const std::string &); + + void set_style(const Style *); unsigned get_width() const; unsigned get_height() const; @@ -41,8 +43,6 @@ public: const std::string &get() const { return text; } unsigned size() const { return text.size(); } - void update_style(); - void render(const Part &, const Geometry &) const; Text &operator=(const std::string &); diff --git a/source/toggle.cpp b/source/toggle.cpp index 62f46eb..d31ff9f 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -16,7 +16,7 @@ namespace GLtk { Toggle::Toggle(const Resources &r, const string &t): Widget(r), - text(style), + text(), pressed(false), value(false), exclusive(false) @@ -87,7 +87,7 @@ void Toggle::exclude_siblings() void Toggle::on_style_change() { - text.update_style(); + text.set_style(style); } -- 2.43.0