From 91997dd3189b93a67179822ec2fed5f2a7bddb74 Mon Sep 17 00:00:00 2001 From: Mikko Rasa Date: Fri, 12 Nov 2010 21:43:29 +0000 Subject: [PATCH] Store the Resources reference only in Root widget Make other widgets get it from the Root SOURCE COMPATIBILITY BREAK --- source/button.cpp | 4 +--- source/button.h | 3 ++- source/container.cpp | 13 +++++++++++-- source/container.h | 3 ++- source/dialog.cpp | 4 +--- source/dialog.h | 2 +- source/dropdown.cpp | 12 ++++++------ source/dropdown.h | 3 ++- source/entry.cpp | 18 +++++++++++------- source/entry.h | 2 +- source/hslider.cpp | 11 ++++++----- source/hslider.h | 2 +- source/image.cpp | 4 +--- source/image.h | 2 +- source/indicator.cpp | 4 +--- source/indicator.h | 2 +- source/label.cpp | 8 ++++---- source/label.h | 2 +- source/list.cpp | 21 ++++++++++++++------- source/list.h | 2 +- source/panel.cpp | 12 ++++-------- source/panel.h | 2 +- source/root.cpp | 5 ++--- source/root.h | 2 ++ source/slider.cpp | 3 +-- source/slider.h | 2 +- source/table.cpp | 4 +--- source/table.h | 2 +- source/text.cpp | 28 +++++++++++++++++++++------- source/toggle.cpp | 4 +--- source/toggle.h | 2 +- source/vslider.cpp | 8 +++++--- source/vslider.h | 2 +- source/widget.cpp | 34 +++++++++++++++++++++++++--------- source/widget.h | 7 ++++--- 35 files changed, 140 insertions(+), 99 deletions(-) diff --git a/source/button.cpp b/source/button.cpp index 5871a5f..8e6e713 100644 --- a/source/button.cpp +++ b/source/button.cpp @@ -12,13 +12,11 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -Button::Button(const Resources &r, const std::string &t): - Widget(r), +Button::Button(const std::string &t): text(), icon(0), pressed(false) { - update_style(); set_text(t); } diff --git a/source/button.h b/source/button.h index 687f5dd..b84511e 100644 --- a/source/button.h +++ b/source/button.h @@ -39,7 +39,8 @@ private: public: sigc::signal signal_clicked; - Button(const Resources &, const std::string & =std::string()); + Button(const std::string & = std::string()); + void set_text(const std::string &); void set_icon(const GL::Texture2D *); virtual void button_press(int, int, unsigned); diff --git a/source/container.cpp b/source/container.cpp index 2ce2c5e..b2b8c56 100644 --- a/source/container.cpp +++ b/source/container.cpp @@ -12,8 +12,7 @@ using namespace std; namespace Msp { namespace GLtk { -Container::Container(const Resources &r): - Widget(r), +Container::Container(): click_focus(0), click_button(0) { } @@ -145,6 +144,16 @@ Container::Child *Container::create_child(Widget *wdg) return new Child(*this, wdg); } +void Container::on_reparent() +{ + for(list::iterator i=children.begin(); i!=children.end(); ++i) + { + if(Container *c = dynamic_cast((*i)->widget)) + c->on_reparent(); + update_style(*(*i)->widget); + } +} + Container::Child::Child(Container &c, Widget *w): container(c), diff --git a/source/container.h b/source/container.h index 32e8157..0384521 100644 --- a/source/container.h +++ b/source/container.h @@ -33,7 +33,7 @@ protected: Widget *click_focus; unsigned click_button; - Container(const Resources &); + Container(); public: virtual ~Container(); @@ -49,6 +49,7 @@ public: virtual void pointer_leave(); protected: virtual Child *create_child(Widget *); + virtual void on_reparent(); }; } // namespace GLtk diff --git a/source/dialog.cpp b/source/dialog.cpp index 3e09783..5a03102 100644 --- a/source/dialog.cpp +++ b/source/dialog.cpp @@ -11,9 +11,7 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -Dialog::Dialog(const Resources &r): - Widget(r), - Panel(r), +Dialog::Dialog(): stale(false) { } diff --git a/source/dialog.h b/source/dialog.h index 0a9b0d8..89065dc 100644 --- a/source/dialog.h +++ b/source/dialog.h @@ -27,7 +27,7 @@ private: public: sigc::signal signal_response; - Dialog(const Resources &); + Dialog(); /** Adds an action button to the dialog. Pressing the button will invoke response handlers and delete the dialog. */ diff --git a/source/dropdown.cpp b/source/dropdown.cpp index d120c85..bcd5019 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -18,16 +18,11 @@ using namespace std; namespace Msp { namespace GLtk { -Dropdown::Dropdown(const Resources &r): - Widget(r), - Container(r), - list(r), +Dropdown::Dropdown(): dropped(false) { add(list); list.signal_item_selected.connect(sigc::mem_fun(this, &Dropdown::list_item_selected)); - - update_style(); } void Dropdown::append(const string &item) @@ -110,6 +105,11 @@ void Dropdown::on_geometry_change() resize_list(); } +void Dropdown::on_style_change() +{ + resize_list(); +} + void Dropdown::resize_list() { list.autosize(); diff --git a/source/dropdown.h b/source/dropdown.h index 7428ebf..2fe82d0 100644 --- a/source/dropdown.h +++ b/source/dropdown.h @@ -35,7 +35,7 @@ private: public: sigc::signal signal_item_selected; - Dropdown(const Resources &); + Dropdown(); void append(const std::string &); void insert(unsigned, const std::string &); @@ -54,6 +54,7 @@ private: virtual void render_special(const Part &) const; virtual void on_geometry_change(); + virtual void on_style_change(); void resize_list(); void list_item_selected(unsigned, const std::string &); diff --git a/source/entry.cpp b/source/entry.cpp index 8853ad7..2c9cb35 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -20,9 +20,7 @@ using namespace std; namespace Msp { namespace GLtk { -Entry::Entry(const Resources &r, const string &t): - Widget(r), - Container(r), +Entry::Entry(const string &t): text(), multiline(false), edit_pos(0), @@ -31,7 +29,6 @@ Entry::Entry(const Resources &r, const string &t): text_part(0), slider(0) { - update_style(); set_text(t); } @@ -51,7 +48,7 @@ void Entry::set_multiline(bool m) { if(!slider) { - slider = new VSlider(res); + slider = new VSlider; add(*slider); slider->set_step(1); slider->signal_value_changed.connect(sigc::mem_fun(this, &Entry::slider_value_changed)); @@ -159,9 +156,16 @@ void Entry::on_geometry_change() void Entry::on_style_change() { + text.set_style(style); + + if(!style) + { + text_part = 0; + return; + } + text_part = style->get_part("text"); - text.set_style(style); reposition_slider(); if(multiline) @@ -170,7 +174,7 @@ void Entry::on_style_change() void Entry::reposition_slider() { - if(!slider) + if(!style || !slider) return; if(const Part *slider_part = style->get_part("slider")) diff --git a/source/entry.h b/source/entry.h index a8bbcd5..5327064 100644 --- a/source/entry.h +++ b/source/entry.h @@ -47,7 +47,7 @@ private: public: sigc::signal signal_enter; - Entry(const Resources &, const std::string & =std::string()); + Entry(const std::string & = std::string()); void set_text(const std::string &); const std::string &get_text() const { return text.get(); } diff --git a/source/hslider.cpp b/source/hslider.cpp index 8cac45f..a4378f0 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -15,11 +15,9 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -HSlider::HSlider(const Resources &r): - Slider(r) -{ - update_style(); -} +HSlider::HSlider(): + slider_size(1) +{ } void HSlider::button_press(int x, int y, unsigned btn) { @@ -72,6 +70,9 @@ void HSlider::on_geometry_change() void HSlider::on_style_change() { + if(!style) + return; + if(const Part *slider_part = style->get_part("slider")) slider_size = slider_part->get_geometry().w; diff --git a/source/hslider.h b/source/hslider.h index 5da211d..4706699 100644 --- a/source/hslider.h +++ b/source/hslider.h @@ -23,7 +23,7 @@ private: unsigned slider_size; public: - HSlider(const Resources &); + HSlider(); virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); diff --git a/source/image.cpp b/source/image.cpp index f7f61bb..11003f4 100644 --- a/source/image.cpp +++ b/source/image.cpp @@ -16,13 +16,11 @@ using namespace std; namespace Msp { namespace GLtk { -Image::Image(const Resources &r, const GL::Texture2D *i): - Widget(r), +Image::Image(const GL::Texture2D *i): image(i), keep_aspect(true) { focusable = false; - update_style(); } void Image::set_image(const GL::Texture2D *i) diff --git a/source/image.h b/source/image.h index 5d113cc..8256a8d 100644 --- a/source/image.h +++ b/source/image.h @@ -24,7 +24,7 @@ private: bool keep_aspect; public: - Image(const Resources &, const GL::Texture2D * = 0); + Image(const GL::Texture2D * = 0); void set_image(const GL::Texture2D *); void set_keep_aspect(bool); diff --git a/source/indicator.cpp b/source/indicator.cpp index d0e7558..8aeca21 100644 --- a/source/indicator.cpp +++ b/source/indicator.cpp @@ -10,11 +10,9 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -Indicator::Indicator(const Resources &r): - Widget(r) +Indicator::Indicator() { focusable = false; - update_style(); } void Indicator::set_active(bool a) diff --git a/source/indicator.h b/source/indicator.h index 69e488f..ce95151 100644 --- a/source/indicator.h +++ b/source/indicator.h @@ -19,7 +19,7 @@ An Indicator visualizes a boolean state. It can be either active or inactive. class Indicator: public Widget { public: - Indicator(const Resources &); + Indicator(); void set_active(bool); private: virtual const char *get_class() const { return "indicator"; } diff --git a/source/label.cpp b/source/label.cpp index 63b0ebd..6db6aa3 100644 --- a/source/label.cpp +++ b/source/label.cpp @@ -14,17 +14,17 @@ using namespace std; namespace Msp { namespace GLtk { -Label::Label(const Resources &r, const string &t): - Widget(r), - text() +Label::Label(const string &t) { focusable = false; - update_style(); set_text(t); } void Label::autosize() { + if(!style) + return; + geom.h = text.get_height(); geom.w = text.get_width(); if(const Part *text_part = style->get_part("text")) diff --git a/source/label.h b/source/label.h index f157616..87835da 100644 --- a/source/label.h +++ b/source/label.h @@ -32,7 +32,7 @@ private: Text text; public: - Label(const Resources &, const std::string & =std::string()); + Label(const std::string & = std::string()); virtual void autosize(); void set_text(const std::string &); diff --git a/source/list.cpp b/source/list.cpp index 113871e..672cf49 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -20,25 +20,23 @@ using namespace std; namespace Msp { namespace GLtk { -List::List(const Resources &r): - Widget(r), - Container(r), +List::List(): sel_index(-1), first(0), n_visible(1), row_height(1), - items_part(0), - slider(res) + items_part(0) { add(slider); slider.set_step(1); slider.signal_value_changed.connect(sigc::mem_fun(this, &List::slider_value_changed)); - - update_style(); } void List::autosize() { + if(!style) + return; + float font_size = style->get_font()->get_default_size(); geom.w = 0; @@ -186,6 +184,12 @@ void List::on_geometry_change() void List::on_style_change() { + if(!style) + { + items_part = 0; + return; + } + reposition_slider(); items_part = style->get_part("items"); @@ -198,6 +202,9 @@ void List::on_style_change() void List::reposition_slider() { + if(!style) + return; + if(const Part *slider_part = style->get_part("slider")) { Geometry sgeom = slider_part->get_geometry(); diff --git a/source/list.h b/source/list.h index 426d309..f1e9ac3 100644 --- a/source/list.h +++ b/source/list.h @@ -44,7 +44,7 @@ private: public: sigc::signal signal_item_selected; - List(const Resources &); + List(); virtual void autosize(); diff --git a/source/panel.cpp b/source/panel.cpp index 59553b4..f0c1c7c 100644 --- a/source/panel.cpp +++ b/source/panel.cpp @@ -25,15 +25,11 @@ using namespace std; namespace Msp { namespace GLtk { -Panel::Panel(const Resources &r): - Widget(r), - Container(r), +Panel::Panel(): pointer_focus(0), pointer_grabbed(false), input_focus(0) -{ - update_style(); -} +{ } void Panel::raise(Widget &wdg) { @@ -193,7 +189,7 @@ Panel::Loader::Loader(Panel &p, map &m): template void Panel::Loader::child(const string &n) { - RefPtr chl = new T(pnl.res); + RefPtr chl = new T(); load_sub(*chl); pnl.add(*chl.get()); wdg_map[n] = chl.release(); @@ -201,7 +197,7 @@ void Panel::Loader::child(const string &n) void Panel::Loader::panel(const string &n) { - RefPtr p = new Panel(pnl.res); + RefPtr p = new Panel(); load_sub(*p, wdg_map); pnl.add(*p.get()); wdg_map[n] = p.release(); diff --git a/source/panel.h b/source/panel.h index 1082447..dc98416 100644 --- a/source/panel.h +++ b/source/panel.h @@ -55,7 +55,7 @@ protected: Panel(const Panel &); Panel &operator=(const Panel &); public: - Panel(const Resources &); + Panel(); void raise(Widget &); Widget *get_input_focus() const { return input_focus; } diff --git a/source/root.cpp b/source/root.cpp index 9023b7c..fbb4cc6 100644 --- a/source/root.cpp +++ b/source/root.cpp @@ -16,8 +16,7 @@ namespace Msp { namespace GLtk { Root::Root(const Resources &r, Graphics::Window &w): - Widget(r), - Panel(r), + resources(r), window(w), lbl_tooltip(0), tooltip_target(0) @@ -53,7 +52,7 @@ void Root::tick() { if(!lbl_tooltip) { - lbl_tooltip = new Label(res); + lbl_tooltip = new Label; add(*lbl_tooltip); lbl_tooltip->set_style("tooltip"); } diff --git a/source/root.h b/source/root.h index 16e01ae..cfe2af3 100644 --- a/source/root.h +++ b/source/root.h @@ -30,6 +30,7 @@ public: sigc::signal signal_tooltip; private: + const Resources &resources; Graphics::Window &window; Label *lbl_tooltip; int pointer_x; @@ -41,6 +42,7 @@ public: Root(const Resources &, Graphics::Window &); void tick(); + const Resources &get_resources() const { return resources; } virtual unsigned get_width() const { return geom.w; } virtual unsigned get_height() const { return geom.h; } diff --git a/source/slider.cpp b/source/slider.cpp index 425f721..3392ac4 100644 --- a/source/slider.cpp +++ b/source/slider.cpp @@ -10,8 +10,7 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -Slider::Slider(const Resources &r): - Widget(r), +Slider::Slider(): min(0), max(1), value(0), diff --git a/source/slider.h b/source/slider.h index 6c95526..c4326e3 100644 --- a/source/slider.h +++ b/source/slider.h @@ -43,7 +43,7 @@ public: sigc::signal signal_value_changed; protected: - Slider(const Resources &); + Slider(); public: void set_value(double); void set_range(double, double); diff --git a/source/table.cpp b/source/table.cpp index 085a442..c0c8238 100644 --- a/source/table.cpp +++ b/source/table.cpp @@ -16,15 +16,13 @@ using namespace std; namespace Msp { namespace GLtk { -Table::Table(const Resources &r): - Widget(r), +Table::Table(): rows(1), columns(1), data(1), col_w(1) { focusable = false; - update_style(); } void Table::set_rows(unsigned r) diff --git a/source/table.h b/source/table.h index 190973e..ab1654c 100644 --- a/source/table.h +++ b/source/table.h @@ -36,7 +36,7 @@ private: std::vector col_w; public: - Table(const Resources &); + Table(); void set_rows(unsigned); void set_columns(unsigned); diff --git a/source/text.cpp b/source/text.cpp index c173a8a..c8c6f55 100644 --- a/source/text.cpp +++ b/source/text.cpp @@ -42,9 +42,12 @@ 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); + if(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); + } } unsigned Text::get_width() const @@ -57,6 +60,9 @@ unsigned Text::get_width() const unsigned Text::get_height() const { + if(!style) + return lines.size(); + const GL::Font *font = style->get_font(); float font_size = font->get_default_size(); unsigned line_height = static_cast((font->get_ascent()-font->get_descent())*font_size); @@ -158,7 +164,7 @@ Geometry Text::coords_to_geometry(const Part &part, const Geometry &parent, unsi void Text::render(const Part &part, const Geometry &parent, unsigned first_row) const { - if(lines.empty()) + if(!style || lines.empty()) return; const GL::Color &color = style->get_font_color(); @@ -180,7 +186,7 @@ Text &Text::operator=(const string &t) void Text::find_lines() { lines.clear(); - float font_size = style->get_font()->get_default_size(); + float font_size = (style ? style->get_font()->get_default_size() : 1); string::size_type start = 0; while(1) { @@ -189,7 +195,12 @@ void Text::find_lines() Line line; line.start = start; line.length = (newline==string::npos ? text.size() : newline)-start; - line.width = static_cast(style->get_font()->get_string_width(text.substr(line.start, line.length))*font_size); + line.width = line.length; + if(style) + { + string str = text.substr(line.start, line.length); + line.width = static_cast(style->get_font()->get_string_width(str)*font_size); + } lines.push_back(line); if(newline==string::npos) @@ -201,6 +212,9 @@ void Text::find_lines() template void Text::process_lines(const Part &part, const Geometry &parent, unsigned first_row, T &data) const { + if(!style) + return; + const GL::Font *font = style->get_font(); float font_size = font->get_default_size(); unsigned line_height = static_cast((font->get_ascent()-font->get_descent())*font_size); @@ -209,7 +223,7 @@ void Text::process_lines(const Part &part, const Geometry &parent, unsigned firs int y_offset = static_cast(-font->get_descent()*font_size); const Sides &margin = part.get_margin(); - unsigned n_lines = max(min(lines.size(), (parent.h-margin.top-margin.bottom)/line_spacing), 1U); + unsigned n_lines = min(lines.size(), max((parent.h-margin.top-margin.bottom)/line_spacing, 1U)); first_row = min(first_row, lines.size()-n_lines); for(unsigned i=0; i signal_toggled; - Toggle(const Resources &, const std::string & =std::string()); + Toggle(const std::string & = std::string()); void set_text(const std::string &); void set_exclusive(bool); diff --git a/source/vslider.cpp b/source/vslider.cpp index 0b5dd0c..cdd2675 100644 --- a/source/vslider.cpp +++ b/source/vslider.cpp @@ -15,10 +15,9 @@ Distributed under the LGPL namespace Msp { namespace GLtk { -VSlider::VSlider(const Resources &r): - Slider(r) +VSlider::VSlider(): + slider_size(1) { - update_style(); } void VSlider::button_press(int x, int y, unsigned btn) @@ -72,6 +71,9 @@ void VSlider::on_geometry_change() void VSlider::on_style_change() { + if(!style) + return; + if(const Part *slider_part = style->get_part("slider")) slider_size = slider_part->get_geometry().h; diff --git a/source/vslider.h b/source/vslider.h index 3823879..2d2ac58 100644 --- a/source/vslider.h +++ b/source/vslider.h @@ -19,7 +19,7 @@ private: unsigned slider_size; public: - VSlider(const Resources &); + VSlider(); virtual void button_press(int, int, unsigned); virtual void button_release(int, int, unsigned); virtual void pointer_motion(int, int); diff --git a/source/widget.cpp b/source/widget.cpp index 2af3b60..434ec45 100644 --- a/source/widget.cpp +++ b/source/widget.cpp @@ -9,8 +9,9 @@ Distributed under the LGPL #include #include #include -#include "panel.h" +#include "container.h" #include "resources.h" +#include "root.h" #include "widget.h" using namespace std; @@ -18,8 +19,7 @@ using namespace std; namespace Msp { namespace GLtk { -Widget::Widget(const Resources &r): - res(r), +Widget::Widget(): style(0), state(NORMAL), visible(true), @@ -92,7 +92,7 @@ void Widget::set_focus() void Widget::render() const { if(!style) - throw InvalidState(format("Attempt to render a widget without a style (class=\"%s\")", get_class())); + throw InvalidState(format("Attempt to render a widget with null style (class=\"%s\", style_name=\"%s\")", get_class(), style_name)); GL::push_matrix(); GL::translate(geom.x, geom.y, 0); @@ -131,16 +131,31 @@ void Widget::focus_out() void Widget::update_style() { - string sname = get_class(); - if(!style_name.empty()) + Widget *top; + for(top=this; top->parent; top=top->parent) ; + Root *root = dynamic_cast(top); + if(!root) + style = 0; + else { - sname += '-'; - sname += style_name; + string sname = get_class(); + if(!style_name.empty()) + { + sname += '-'; + sname += style_name; + } + + style = root->get_resources().get