]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/list.cpp
Add method to get a Part by name
[libs/gltk.git] / source / list.cpp
index cc9f707efa1d9dadf008e585101f0a6365c15782..59d13dcc7236a358b18c19d29ed40602ec9a9b80 100644 (file)
@@ -60,7 +60,7 @@ void List::remove(unsigned i)
        if(sel_index>static_cast<int>(i))
                --sel_index;
        else if(sel_index==static_cast<int>(i))
-               sel_index=-1;
+               sel_index = -1;
 
        recalculate_parameters();
 }
@@ -68,7 +68,7 @@ void List::remove(unsigned i)
 void List::clear()
 {
        items.clear();
-       sel_index=-1;
+       sel_index = -1;
 
        recalculate_parameters();
 }
@@ -76,10 +76,10 @@ void List::clear()
 void List::set_selected_index(int i)
 {
        if(i<0)
-               sel_index=-1;
+               sel_index = -1;
        else if(i<static_cast<int>(items.size()))
        {
-               sel_index=i;
+               sel_index = i;
                signal_item_selected.emit(sel_index, items[sel_index]);
        }
        else
@@ -100,12 +100,12 @@ void List::button_press(int x, int y, unsigned btn)
        if(!click_focus && btn==1)
        {
                if(items_part)
-                       y+=items_part->get_margin().top;
+                       y += items_part->get_margin().top;
 
-               unsigned i=(geom.h-1-y)/row_height;
+               unsigned i = (geom.h-1-y)/row_height;
                if(i<n_visible && first+i<items.size())
                {
-                       sel_index=first+i;
+                       sel_index = first+i;
 
                        signal_item_selected.emit(sel_index, items[sel_index]);
                }
@@ -116,22 +116,22 @@ void List::render_special(const Part &part) const
 {
        if(part.get_name()=="items")
        {
-               const GL::Font &font=*style->get_font();
-               const float font_size=font.get_default_size();
-               const GL::Color &color=style->get_font_color();
-               const Sides &margin=part.get_margin();
+               const GL::Font &font = *style->get_font();
+               const float font_size = font.get_default_size();
+               const GL::Color &color = style->get_font_color();
+               const Sides &margin = part.get_margin();
 
-               Geometry pgeom=geom;
-               pgeom.h=row_height;
-               pgeom.w-=margin.left+margin.right;
+               Geometry pgeom = geom;
+               pgeom.h = row_height;
+               pgeom.w -= margin.left+margin.right;
 
                for(unsigned i=0; (i<n_visible && first+i<items.size()); ++i)
                {
                        Geometry rgeom;
-                       rgeom.w=static_cast<unsigned>(font.get_string_width(items[first+i])*font_size);
-                       rgeom.h=row_height;
-                       rgeom.x=margin.left;
-                       rgeom.y=geom.h-margin.top-(i+1)*row_height-static_cast<int>(font.get_descent()*font_size);
+                       rgeom.w = static_cast<unsigned>(font.get_string_width(items[first+i])*font_size);
+                       rgeom.h = row_height;
+                       rgeom.x = margin.left;
+                       rgeom.y = geom.h-margin.top-(i+1)*row_height-static_cast<int>(font.get_descent()*font_size);
                        part.get_alignment().apply(rgeom, pgeom);
 
                        GL::push_matrix();
@@ -147,15 +147,15 @@ void List::render_special(const Part &part) const
        {
                if(sel_index>=static_cast<int>(first) && sel_index<static_cast<int>(first+n_visible))
                {
-                       const Sides &margin=part.get_margin();
+                       const Sides &margin = part.get_margin();
 
-                       Geometry pgeom=geom;
-                       pgeom.h=row_height;
-                       pgeom.w-=margin.left+margin.right;
+                       Geometry pgeom = geom;
+                       pgeom.h = row_height;
+                       pgeom.w -= margin.left+margin.right;
 
-                       Geometry rgeom=part.get_geometry();
-                       rgeom.y+=geom.h-margin.top-row_height*(sel_index-first+1);
-                       rgeom.x+=margin.left;
+                       Geometry rgeom = part.get_geometry();
+                       rgeom.y += geom.h-margin.top-row_height*(sel_index-first+1);
+                       rgeom.x += margin.left;
                        part.get_alignment().apply(rgeom, pgeom);
 
                        GL::push_matrix();
@@ -179,45 +179,41 @@ 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());
+       const GL::Font &font = *style->get_font();
+       row_height = static_cast<unsigned>((font.get_ascent()-font.get_descent())*font.get_default_size());
 
        recalculate_parameters();
 }
 
 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()
 {
-       unsigned h=geom.h;
+       unsigned h = geom.h;
        if(items_part)
        {
-               const Sides &margin=items_part->get_margin();
-               h-=margin.top+margin.bottom;
+               const Sides &margin = items_part->get_margin();
+               h -= margin.top+margin.bottom;
        }
 
-       n_visible=h/row_height;
+       n_visible = h/row_height;
 
        if(first+n_visible>items.size())
        {
                if(items.size()>n_visible)
-                       first=items.size()-n_visible;
+                       first = items.size()-n_visible;
                else
-                       first=0;
+                       first = 0;
        }
 
        if(items.size()>n_visible)
@@ -235,7 +231,7 @@ void List::recalculate_parameters()
 void List::slider_value_changed(double value)
 {
        if(items.size()>n_visible)
-               first=items.size()-n_visible-static_cast<unsigned>(value);
+               first = items.size()-n_visible-static_cast<unsigned>(value);
 }