From: Mikko Rasa Date: Fri, 12 Nov 2010 08:45:37 +0000 (+0000) Subject: Add method to get a Part by name X-Git-Url: http://git.tdb.fi/?a=commitdiff_plain;h=787dd5572e904a87686e7309401b86e0c528d823;p=libs%2Fgltk.git Add method to get a Part by name Use keyword overloading to rename the special keyword to part --- diff --git a/basic.skin b/basic.skin index 1dd8d41..eb0a9d6 100644 --- a/basic.skin +++ b/basic.skin @@ -34,7 +34,7 @@ style "button" graphic ACTIVE "dark_grey_sunken"; }; - special "text" + part "text" { align 0.5 0.5; fill 0.0 0.0; @@ -85,7 +85,7 @@ style "toggle" margin { left 2; }; }; - special "text" + part "text" { align 0.0 0.5; fill 0.0 0.0; @@ -129,7 +129,7 @@ style "toggle-option" margin { left 3; }; }; - special "text" + part "text" { align 0.0 0.5; fill 0.0 0.0; @@ -141,7 +141,7 @@ style "label" { font_color 0.0 0.0 0.0; - special "text" + part "text" { align 0.5 0.5; fill 0.0 0.0; @@ -163,14 +163,14 @@ style "entry" graphic NORMAL "white_sunken"; }; - special "text" + part "text" { align 0.0 0.5; fill 0.0 0.0; margin { left 3; right 3; top 3; bottom 3; }; }; - special "cursor" + part "cursor" { graphic FOCUS "entry_cursor"; align 0.0 0.0; @@ -207,7 +207,7 @@ style "hslider" graphic NORMAL "dark_grey_sunken_shallow"; }; - special "slider" + part "slider" { graphic NORMAL "hthumb"; graphic HOVER "hthumb_light"; @@ -237,7 +237,7 @@ style "vslider" graphic NORMAL "dark_grey_sunken_shallow"; }; - special "slider" + part "slider" { graphic NORMAL "vthumb"; graphic HOVER "vthumb_light"; @@ -262,7 +262,7 @@ style "list" margin { right 12; }; }; - special "slider" + part "slider" { size 12 12; fill 0.0 1.0; @@ -270,13 +270,13 @@ style "list" //margin { right 1; top 1; bottom 1; }; }; - special "selection" + part "selection" { graphic NORMAL "blue_flat"; margin { left 3; right 14; top 3; bottom 3; }; }; - special "items" + part "items" { margin { left 3; right 14; top 3; bottom 3; }; }; @@ -327,14 +327,14 @@ style "dropdown" margin { right 3; }; }; - special "text" + part "text" { fill 0.0 0.0; align 0.0 0.5; margin { left 4; }; }; - special "list"; + part "list"; }; graphic "grey_beveled" @@ -351,10 +351,10 @@ style "panel" graphic NORMAL "grey_beveled"; }; - special "children"; + part "children"; }; style "root" { - special "children"; + part "children"; }; diff --git a/source/dropdown.cpp b/source/dropdown.cpp index b911df3..5901414 100644 --- a/source/dropdown.cpp +++ b/source/dropdown.cpp @@ -117,12 +117,11 @@ void Dropdown::resize_list() const Style &stl = list.get_style(); const GL::Font &font = *stl.get_font(); unsigned h = min(max(n_items, 1U), 10U)*static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); - for(std::list::const_iterator i=stl.get_parts().begin(); i!=stl.get_parts().end(); ++i) - if(i->get_name()=="items") - { - const Sides &margin = i->get_margin(); - h += margin.top+margin.bottom; - } + if(const Part *items_part = stl.get_part("items")) + { + const Sides &margin = items_part->get_margin(); + h += margin.top+margin.bottom; + } list.set_geometry(Geometry(0, -h, geom.w, h)); } diff --git a/source/entry.cpp b/source/entry.cpp index 3fa4c91..1439d42 100644 --- a/source/entry.cpp +++ b/source/entry.cpp @@ -149,10 +149,7 @@ void Entry::on_geometry_change() void Entry::on_style_change() { - text_part = 0; - for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="text") - text_part = &*i; + text_part = style->get_part("text"); text.set_style(style); reposition_slider(); @@ -163,13 +160,12 @@ void Entry::reposition_slider() if(!slider) return; - for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="slider") - { - Geometry sgeom = i->get_geometry(); - i->get_alignment().apply(sgeom, geom, i->get_margin()); - slider->set_geometry(sgeom); - } + 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 Entry::slider_value_changed(double value) diff --git a/source/hslider.cpp b/source/hslider.cpp index ad447c7..8cac45f 100644 --- a/source/hslider.cpp +++ b/source/hslider.cpp @@ -72,9 +72,8 @@ void HSlider::on_geometry_change() void HSlider::on_style_change() { - for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="slider") - slider_size = i->get_geometry().w; + if(const Part *slider_part = style->get_part("slider")) + slider_size = slider_part->get_geometry().w; on_geometry_change(); } diff --git a/source/label.cpp b/source/label.cpp index 60c3f43..63b0ebd 100644 --- a/source/label.cpp +++ b/source/label.cpp @@ -25,15 +25,9 @@ Label::Label(const Resources &r, const string &t): void Label::autosize() { - const list &parts = style->get_parts(); - const Part *text_part = 0; - for(list::const_iterator i=parts.begin(); (!text_part && i!=parts.end()); ++i) - if(i->get_name()=="text") - text_part = &*i; - geom.h = text.get_height(); geom.w = text.get_width(); - if(text_part) + if(const Part *text_part = style->get_part("text")) { const Sides &margin = text_part->get_margin(); geom.w += margin.left+margin.right; diff --git a/source/list.cpp b/source/list.cpp index afa7573..59d13dc 100644 --- a/source/list.cpp +++ b/source/list.cpp @@ -179,10 +179,7 @@ void List::on_style_change() { reposition_slider(); - items_part = 0; - for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="items") - items_part = &*i; + items_part = style->get_part("items"); const GL::Font &font = *style->get_font(); row_height = static_cast((font.get_ascent()-font.get_descent())*font.get_default_size()); @@ -192,13 +189,12 @@ void List::on_style_change() void List::reposition_slider() { - for(list::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="slider") - { - Geometry sgeom = i->get_geometry(); - i->get_alignment().apply(sgeom, geom, i->get_margin()); - slider.set_geometry(sgeom); - } + 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::recalculate_parameters() diff --git a/source/style.cpp b/source/style.cpp index 0bc9311..8eb5de1 100644 --- a/source/style.cpp +++ b/source/style.cpp @@ -17,6 +17,15 @@ Style::Style(Resources &r): font(&r.get_default_font()) { } +const Part *Style::get_part(const string &name) const +{ + for(PartSeq::const_iterator i=parts.begin(); i!=parts.end(); ++i) + if(i->get_name()==name) + return &*i; + + return 0; +} + Style::Loader::Loader(Style &s, Resources &r): style(s), @@ -24,8 +33,10 @@ Style::Loader::Loader(Style &s, Resources &r): { add("font", &Style::font); add("font_color", &Loader::font_color); - add("part", &Loader::part); - add("special", &Loader::special); + add("part", static_cast(&Loader::part)); + add("part", static_cast(&Loader::part)); + // Deprecated alias + add("special", static_cast(&Loader::part)); } void Style::Loader::font_color(float r, float g, float b) @@ -35,12 +46,10 @@ void Style::Loader::font_color(float r, float g, float b) void Style::Loader::part() { - Part p((string())); - load_sub(p, res); - style.parts.push_back(p); + part(string()); } -void Style::Loader::special(const string &n) +void Style::Loader::part(const string &n) { Part p(n); load_sub(p, res); diff --git a/source/style.h b/source/style.h index f17735f..8aeb61e 100644 --- a/source/style.h +++ b/source/style.h @@ -41,7 +41,7 @@ public: void font(const std::string &); void font_color(float, float, float); void part(); - void special(const std::string &); + void part(const std::string &); }; private: @@ -54,6 +54,7 @@ public: const GL::Font *get_font() const { return font; } const GL::Color &get_font_color() const { return font_color; } const PartSeq &get_parts() const { return parts; } + const Part *get_part(const std::string &) const; }; } // namespace GLtk diff --git a/source/vslider.cpp b/source/vslider.cpp index ed2e0bc..0b5dd0c 100644 --- a/source/vslider.cpp +++ b/source/vslider.cpp @@ -72,9 +72,8 @@ void VSlider::on_geometry_change() void VSlider::on_style_change() { - for(PartSeq::const_iterator i=style->get_parts().begin(); i!=style->get_parts().end(); ++i) - if(i->get_name()=="slider") - slider_size = i->get_geometry().h; + if(const Part *slider_part = style->get_part("slider")) + slider_size = slider_part->get_geometry().h; on_geometry_change(); }