From: Mikko Rasa Date: Thu, 9 Oct 2008 21:06:13 +0000 (+0000) Subject: Support exclusive Toggles for implementing option buttons X-Git-Tag: 1.0~4 X-Git-Url: http://git.tdb.fi/?p=libs%2Fgltk.git;a=commitdiff_plain;h=30780ba31be92c977a68a2a9103eeba87747d530 Support exclusive Toggles for implementing option buttons --- diff --git a/source/panel.h b/source/panel.h index 397d71f..e269ca2 100644 --- a/source/panel.h +++ b/source/panel.h @@ -49,6 +49,7 @@ public: void add(Widget &); void remove(Widget &); + const std::list &get_children() const { return children; } void raise(Widget &); void set_focus(Widget &); diff --git a/source/toggle.cpp b/source/toggle.cpp index dd6ce35..8c727d6 100644 --- a/source/toggle.cpp +++ b/source/toggle.cpp @@ -5,25 +5,45 @@ Copyright © 2007 Mikko Rasa, Mikkosoft Productions Distributed under the LGPL */ +#include "panel.h" #include "part.h" #include "toggle.h" +using namespace std; + namespace Msp { namespace GLtk { Toggle::Toggle(const Resources &r): Widget(r), pressed(false), - value(false) + value(false), + exclusive(false) { update_style(); } +void Toggle::set_text(const string &t) +{ + text=t; +} + +void Toggle::set_exclusive(bool e) +{ + exclusive=e; + if(exclusive && value) + exclude_siblings(); +} + void Toggle::set_value(bool v) { value=v; if(value) + { state|=ACTIVE; + if(exclusive && parent) + exclude_siblings(); + } else state&=~ACTIVE; } @@ -54,12 +74,22 @@ void Toggle::render_special(const Part &part) const render_text(part, text); } +void Toggle::exclude_siblings() +{ + const list &siblings=parent->get_children(); + for(list::const_iterator i=siblings.begin(); i!=siblings.end(); ++i) + if(Toggle *tgl=dynamic_cast(*i)) + if(tgl!=this && tgl->get_exclusive() && tgl->get_value()) + tgl->set_value(false); +} + Toggle::Loader::Loader(Toggle &t): Widget::Loader(t) { - add("text", &Toggle::text); - add("value", &Toggle::value); + add("exclusive", &Toggle::exclusive); + add("text", &Toggle::text); + add("value", &Toggle::value); } Toggle &Toggle::Loader::get_object() const diff --git a/source/toggle.h b/source/toggle.h index f01f84b..bdb1b31 100644 --- a/source/toggle.h +++ b/source/toggle.h @@ -33,12 +33,16 @@ private: std::string text; bool pressed; bool value; + bool exclusive; public: sigc::signal signal_toggled; Toggle(const Resources &); + void set_text(const std::string &); + void set_exclusive(bool); + bool get_exclusive() const { return exclusive; } void set_value(bool); bool get_value() const { return value; } @@ -47,6 +51,8 @@ public: private: virtual const char *get_class() const { return "toggle"; } virtual void render_special(const Part &) const; + + void exclude_siblings(); }; } // namespace GLtk