]> git.tdb.fi Git - libs/gltk.git/blobdiff - source/toggle.cpp
Convert loops and iterators to use C++11 features
[libs/gltk.git] / source / toggle.cpp
index e34b43ae38cc743ef721216e5b3d41b1b662ce26..bc40276baf6f1b35c5e6d769b1730eed93f3d4e0 100644 (file)
@@ -14,31 +14,21 @@ Toggle::Toggle(const string &t):
        value(false),
        exclusive(false)
 {
+       input_type = INPUT_NAVIGATION;
        set_text(t);
 }
 
-void Toggle::autosize()
+void Toggle::autosize_special(const Part &part, Geometry &ageom) const
 {
-       if(!style)
-               return;
-
-       Widget::autosize();
-
-       if(const Part *text_part = style->get_part("text"))
-       {
-               const Sides &margin = text_part->get_margin();
-               geom.w = max(geom.w, text.get_width()+margin.left+margin.right);
-               geom.h = max(geom.h, text.get_height()+margin.top+margin.bottom);
-       }
-
-       rebuild();
+       if(part.get_name()=="text")
+               text.autosize(part, ageom);
 }
 
 void Toggle::set_text(const string &t)
 {
        text = t;
        signal_autosize_changed.emit();
-       rebuild();
+       mark_rebuild();
 }
 
 void Toggle::set_exclusive(bool e)
@@ -50,15 +40,16 @@ void Toggle::set_exclusive(bool e)
 
 void Toggle::exclude_siblings()
 {
-       const list<Widget *> &siblings = parent->get_children();
-       for(list<Widget *>::const_iterator i=siblings.begin(); i!=siblings.end(); ++i)
-               if(Toggle *tgl = dynamic_cast<Toggle *>(*i))
+       for(Widget *w: parent->get_children())
+               if(Toggle *tgl = dynamic_cast<Toggle *>(w))
                        if(tgl!=this && tgl->get_exclusive() && tgl->get_value())
                                tgl->set_value(false);
 }
 
 void Toggle::set_value(bool v)
 {
+       bool old_value = value;
+
        value = v;
        if(value)
        {
@@ -68,12 +59,15 @@ void Toggle::set_value(bool v)
        }
        else
                clear_state(ACTIVE);
+
+       if(value!=old_value)
+               signal_toggled.emit(value);
 }
 
-void Toggle::rebuild_special(const Part &part, CachedPart &cache)
+void Toggle::rebuild_special(const Part &part)
 {
        if(part.get_name()=="text")
-               text.build(part, geom, cache);
+               text.build(part, state, geom, part_cache);
 }
 
 void Toggle::button_press(int, int, unsigned btn)
@@ -87,15 +81,24 @@ void Toggle::button_release(int x, int y, unsigned btn)
        if(pressed && btn==1)
        {
                if(geom.is_inside_relative(x, y) && (!value || !exclusive))
-               {
                        set_value(!value);
-                       signal_toggled.emit(value);
-               }
 
                pressed = false;
        }
 }
 
+bool Toggle::navigate(Navigation nav)
+{
+       if(nav==NAV_ACTIVATE)
+       {
+               if(!value || !exclusive)
+                       set_value(!value);
+               return true;
+       }
+
+       return false;
+}
+
 void Toggle::on_style_change()
 {
        text.set_style(style);