Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

CEGUIRadioButton.cpp

Go to the documentation of this file.
00001 /************************************************************************
00002         filename:       CEGUIRadioButton.cpp
00003         created:        13/4/2004
00004         author:         Paul D Turner
00005         
00006         purpose:        Implementation of RadioButton widget base class
00007 *************************************************************************/
00008 /*************************************************************************
00009     Crazy Eddie's GUI System (http://www.cegui.org.uk)
00010     Copyright (C)2004 - 2005 Paul D Turner (paul@cegui.org.uk)
00011 
00012     This library is free software; you can redistribute it and/or
00013     modify it under the terms of the GNU Lesser General Public
00014     License as published by the Free Software Foundation; either
00015     version 2.1 of the License, or (at your option) any later version.
00016 
00017     This library is distributed in the hope that it will be useful,
00018     but WITHOUT ANY WARRANTY; without even the implied warranty of
00019     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00020     Lesser General Public License for more details.
00021 
00022     You should have received a copy of the GNU Lesser General Public
00023     License along with this library; if not, write to the Free Software
00024     Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00025 *************************************************************************/
00026 #include "elements/CEGUIRadioButton.h"
00027 
00028 // Start of CEGUI namespace section
00029 namespace CEGUI
00030 {
00031 const String RadioButton::EventNamespace("RadioButton");
00032 
00033 /*************************************************************************
00034         Definitions of Properties for this class
00035 *************************************************************************/
00036 RadioButtonProperties::Selected RadioButton::d_selectedProperty;
00037 RadioButtonProperties::GroupID  RadioButton::d_groupIDProperty;
00038 
00039 
00040 /*************************************************************************
00041         Event name constants
00042 *************************************************************************/
00043 // generated internally by Window
00044 const String RadioButton::EventSelectStateChanged( (utf8*)"SelectStateChanged" );
00045 
00046 
00047 /*************************************************************************
00048         Constructor
00049 *************************************************************************/
00050 RadioButton::RadioButton(const String& type, const String& name) :
00051         ButtonBase(type, name),
00052         d_selected(false),
00053         d_groupID(0)
00054 {
00055         // add radio button specific events.
00056         addRadioButtonEvents();
00057 
00058         addRadioButtonProperties();
00059 }
00060 
00061 
00062 /*************************************************************************
00063         Destructor
00064 *************************************************************************/
00065 RadioButton::~RadioButton(void)
00066 {
00067 }
00068 
00069 
00070 /*************************************************************************
00071         set whether the radio button is selected or not 
00072 *************************************************************************/
00073 void RadioButton::setSelected(bool select)
00074 {
00075         if (select != d_selected)
00076         {
00077                 d_selected = select;
00078                 requestRedraw();
00079 
00080                 // if new state is 'selected', we must de-select any selected radio buttons within our group.
00081                 if (d_selected)
00082                 {
00083                         deselectOtherButtonsInGroup();
00084                 }
00085 
00086                 WindowEventArgs args(this);
00087                 onSelectStateChanged(args);
00088         }
00089 
00090 }
00091 
00092 
00093 /*************************************************************************
00094         set the groupID for this radio button   
00095 *************************************************************************/
00096 void RadioButton::setGroupID(ulong group)
00097 {
00098         d_groupID = group;
00099 
00100         if (d_selected)
00101         {
00102                 deselectOtherButtonsInGroup();
00103         }
00104 
00105 }
00106 
00107 
00108 /*************************************************************************
00109         Add radio button specific events        
00110 *************************************************************************/
00111 void RadioButton::addRadioButtonEvents(void)
00112 {
00113         addEvent(EventSelectStateChanged);
00114 }
00115 
00116 
00117 /*************************************************************************
00118         Deselect any selected radio buttons attached to the same parent
00119         within the same group (but not do not deselect 'this').
00120 *************************************************************************/
00121 void RadioButton::deselectOtherButtonsInGroup(void) const
00122 {
00123         // nothing to do unless we are attached to another window.
00124         if (d_parent != NULL)
00125         {
00126                 int child_count = d_parent->getChildCount();
00127 
00128                 // scan all children
00129                 for (int child = 0; child < child_count; ++child)
00130                 {
00131                         // is this child same type as we are?
00132                         if (d_parent->getChildAtIdx(child)->getType() == getType())
00133                         {
00134                                 RadioButton* rb = (RadioButton*)d_parent->getChildAtIdx(child);
00135 
00136                                 // is child same group, selected, but not 'this'?
00137                                 if (rb->isSelected() && (rb != this) && (rb->getGroupID() == d_groupID))
00138                                 {
00139                                         // deselect the radio button.
00140                                         rb->setSelected(false);
00141                                 }
00142 
00143                         }
00144 
00145                 }
00146 
00147         }
00148 
00149 }
00150 
00151 
00152 /*************************************************************************
00153         event triggered internally when the select state of the button changes.
00154 *************************************************************************/
00155 void RadioButton::onSelectStateChanged(WindowEventArgs& e)
00156 {
00157         fireEvent(EventSelectStateChanged, e, EventNamespace);
00158 }
00159 
00160 
00161 /*************************************************************************
00162         Handler called when mouse button gets released
00163 *************************************************************************/
00164 void RadioButton::onMouseButtonUp(MouseEventArgs& e)
00165 {
00166         if ((e.button == LeftButton) && isPushed())
00167         {
00168                 Window* sheet = System::getSingleton().getGUISheet();
00169 
00170                 if (sheet != NULL)
00171                 {
00172                         // if mouse was released over this widget
00173                         if (this == sheet->getChildAtPosition(e.position))
00174                         {
00175                                 // select this button & deselect all others in the same group.
00176                                 setSelected(true);
00177                         }
00178 
00179                 }
00180 
00181                 e.handled = true;
00182         }
00183 
00184         // default handling
00185         ButtonBase::onMouseButtonUp(e);
00186 }
00187 
00188 
00189 /*************************************************************************
00190         Return a pointer to the RadioButton object within the same group as
00191         this RadioButton, that is currently selected.
00192 *************************************************************************/
00193 RadioButton* RadioButton::getSelectedButtonInGroup(void) const
00194 {
00195         // Only search we we are a child window
00196         if (d_parent != NULL)
00197         {
00198                 int child_count = d_parent->getChildCount();
00199 
00200                 // scan all children
00201                 for (int child = 0; child < child_count; ++child)
00202                 {
00203                         // is this child same type as we are?
00204                         if (d_parent->getChildAtIdx(child)->getType() == getType())
00205                         {
00206                                 RadioButton* rb = (RadioButton*)d_parent->getChildAtIdx(child);
00207 
00208                                 // is child same group and selected?
00209                                 if (rb->isSelected() && (rb->getGroupID() == d_groupID))
00210                                 {
00211                                         // return the matching RadioButton pointer (may even be 'this').
00212                                         return rb;
00213                                 }
00214 
00215                         }
00216 
00217                 }
00218 
00219         }
00220 
00221         // no selected button attached to this window is in same group
00222         return NULL;
00223 }
00224 
00225 /*************************************************************************
00226         Add properties for radio button
00227 *************************************************************************/
00228 void RadioButton::addRadioButtonProperties(void)
00229 {
00230         addProperty(&d_selectedProperty);
00231         addProperty(&d_groupIDProperty);
00232 }
00233 
00234 
00235 } // End of  CEGUI namespace section

Generated on Wed Sep 7 09:56:33 2005 for Crazy Eddies GUI System by  doxygen 1.4.3