Homepage Demos Overview Downloads Tutorials Reference
Credits

ToggleControl.h

Go to the documentation of this file.
00001 //-*-c++-*-
00002 #ifndef INCLUDED_ToggleControl_h_
00003 #define INCLUDED_ToggleControl_h_
00004 
00005 #include "NullControl.h"
00006 #include "Shared/ReferenceCounter.h"
00007 
00008 //! a simple control for turning things on and off
00009 /*! Can also be used for radio buttons - select one of a group */
00010 class ToggleControl : public NullControl {
00011 public:
00012   //! a little class for managing the currently active ToggleControl to allow radio buttons
00013   class RadioGroup : public ReferenceCounter {
00014   public:
00015     //! constructor
00016     explicit RadioGroup(bool must_have_one=true) : ReferenceCounter(), cur(NULL), enforced(must_have_one) {}
00017     //! call this when a different ToggleControl wants to take over
00018     /*! implementation is trickier than you might think! */
00019     virtual void activate(ToggleControl * next) {
00020       bool was_enforced=enforced; //turn off enforcing so current control can turn off...
00021       enforced=false;
00022       if(cur!=NULL) {
00023         ToggleControl * tmp=cur;
00024         cur=NULL; // set this to NULL first to prevent infinite recursion
00025         tmp->setStatus(false);
00026       }
00027       enforced=was_enforced;
00028       cur=next;
00029     }
00030     //! returns the currently active control
00031     virtual ToggleControl * getActive() const { return cur; }
00032     //! can change the "must have one" setting (#enforced)
00033     virtual void setEnforced(bool must_have_one) { enforced=must_have_one; }
00034     //! returns the "must have one" setting (#enforced)
00035     virtual bool getEnforced() const { return enforced; }
00036   protected:
00037     ToggleControl * cur; //!< the currently active control, or NULL
00038     bool enforced; //!< if true, the current control cannot turn off, a new one must be activated
00039   private:
00040     RadioGroup(const RadioGroup& ); //!< don't call
00041     RadioGroup& operator=(const RadioGroup& ); //!< don't call
00042   };
00043 
00044   //!@name Constructors
00045   //!
00046   ToggleControl() : NullControl("[ ] "), rg(NULL) {}
00047   ToggleControl(const std::string& n, RadioGroup * rad=NULL) : NullControl("[ ] "+n), rg(NULL) { setRadioGroup(rad); }
00048   ToggleControl(const std::string& n, const std::string& d, RadioGroup * rad=NULL) : NullControl("[ ] "+n,d), rg(NULL) { setRadioGroup(rad); }
00049   //@}
00050   ~ToggleControl() { setRadioGroup(NULL); } //!< destructor
00051 
00052   virtual ControlBase * activate(MotionManager::MC_ID mcid, Socket * disp) { toggleStatus(); return NullControl::activate(mcid,disp); }
00053   virtual ControlBase * doSelect() { toggleStatus(); return NullControl::doSelect(); }
00054 
00055   virtual ControlBase& setName(const std::string& n) { name=std::string("[")+name.substr(1,1)+std::string("] ")+n; return *this; }
00056 
00057   //! calls setStatus() with the not of getStatus()
00058   virtual void toggleStatus() {
00059     setStatus(!getStatus());
00060   }
00061 
00062   //! if status is a ' ', it'll be replaced with @a c, otherwise a space.
00063   virtual void toggleStatus(char c) {
00064     if(getStatus())
00065       setStatus(false);
00066     else
00067       setStatus(c);
00068   }
00069 
00070   //! status will toggle between the two arguments; if current status is neither, the first is used
00071   virtual void toggleStatus(char c1,char c2) {
00072     if(getStatusChar()==c1)
00073       setStatus(c2);
00074     else
00075       setStatus(c1);
00076   }
00077 
00078   //! a true will put a 'X' for the status; false shows ' '
00079   virtual void setStatus(bool check) {
00080     setStatus(check?'X':' ');
00081   }
00082   
00083   //! pass the character to put as the status
00084   virtual void setStatus(char c) {
00085     if(rg!=NULL) {
00086       if(c==' ') {
00087         if(rg->getEnforced())
00088           return;
00089         rg->activate(NULL);
00090       } else {
00091         rg->activate(this);
00092       }
00093     }
00094     name[1]=c;
00095   }
00096 
00097   //! returns true if there's a non-space as the status
00098   virtual bool getStatus() const {
00099     return getStatusChar()!=' ';
00100   }
00101 
00102   //! returns the current status char
00103   virtual char getStatusChar() const {
00104     return name[1];
00105   }
00106 
00107   //! removes itself from current RadioGroup, and adds itself to @a rad if non-NULL
00108   virtual void setRadioGroup(RadioGroup * rad) {
00109     if(rg!=NULL)
00110       rg->RemoveReference();
00111     if(rad!=NULL)
00112       rad->AddReference();
00113     rg=rad;
00114     if(getStatus() && rg!=NULL)
00115       rg->activate(this);
00116   }
00117 
00118   //! returns the current RadioGroup
00119   virtual RadioGroup * getRadioGroup() const { return rg; }
00120 
00121 protected:
00122   RadioGroup * rg; //!< pointer to an optional radio group to allow one-of-many selections
00123 
00124 private:
00125   ToggleControl(const ToggleControl& ); //!< don't call
00126   ToggleControl& operator=(const ToggleControl& ); //!< don't call
00127 };
00128 
00129 /*! @file
00130  * @brief 
00131  * @author ejt (Creator)
00132  *
00133  * $Author: ejt $
00134  * $Name: tekkotsu-2_1 $
00135  * $Revision: 1.3 $
00136  * $State: Exp $
00137  * $Date: 2004/01/18 10:16:56 $
00138  */
00139 
00140 #endif

Tekkotsu v2.1
Generated Tue Mar 16 23:19:16 2004 by Doxygen 1.3.5