]> git.tdb.fi Git - libs/gltk.git/commitdiff
Add method to get a Part by name
authorMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2010 08:45:37 +0000 (08:45 +0000)
committerMikko Rasa <tdb@tdb.fi>
Fri, 12 Nov 2010 08:45:37 +0000 (08:45 +0000)
Use keyword overloading to rename the special keyword to part

basic.skin
source/dropdown.cpp
source/entry.cpp
source/hslider.cpp
source/label.cpp
source/list.cpp
source/style.cpp
source/style.h
source/vslider.cpp

index 1dd8d41b5dc72f3b08465153e156a378d09a46d7..eb0a9d6802c01a17ca40e46cb266288e736edf27 100644 (file)
@@ -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";
 };
index b911df370de3579e89d3430a8882362346071e14..5901414f3a033a9ad786a537d70e93163840da4e 100644 (file)
@@ -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<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
-       for(std::list<Part>::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));
 }
 
index 3fa4c91bb36423d64e10986bafcd156e995fdf28..1439d426acce4854bf1ecdcb3babd25985f1d20a 100644 (file)
@@ -149,10 +149,7 @@ void Entry::on_geometry_change()
 
 void Entry::on_style_change()
 {
-       text_part = 0;
-       for(list<Part>::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<Part>::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)
index ad447c7b017e627964b3f82e384f90442e16bbe0..8cac45f46f772b9fdee167c9909de584f599ac6e 100644 (file)
@@ -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();
 }
index 60c3f431f9e35ca05016fc25366574d93a48474b..63b0ebdc2a1b24f366d9f4b28e82646455a5c4c3 100644 (file)
@@ -25,15 +25,9 @@ Label::Label(const Resources &r, const string &t):
 
 void Label::autosize()
 {
-       const list<Part> &parts = style->get_parts();
-       const Part *text_part = 0;
-       for(list<Part>::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;
index afa7573ecb837a6fd45d04810105e4b85e651bf5..59d13dcc7236a358b18c19d29ed40602ec9a9b80 100644 (file)
@@ -179,10 +179,7 @@ void List::on_style_change()
 {
        reposition_slider();
 
-       items_part = 0;
-       for(list<Part>::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<unsigned>((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<Part>::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()
index 0bc931188e7e93ee9356cae9e90bf79112d27ecd..8eb5de1e79cb7853a1828dfb12248551204ed787 100644 (file)
@@ -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<void (Loader::*)()>(&Loader::part));
+       add("part",       static_cast<void (Loader::*)(const std::string &)>(&Loader::part));
+       // Deprecated alias
+       add("special",    static_cast<void (Loader::*)(const std::string &)>(&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);
index f17735f05e07c7efb4a3aa88c9e4bb6f4db79724..8aeb61e8ef68f26a61b82acaf97d6e8398a14269 100644 (file)
@@ -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
index ed2e0bc5123d0a6cfad0b43cdf73f6faf15bc13c..0b5dd0ca1efe54cb178a4a4959b04215a0b78496 100644 (file)
@@ -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();
 }