libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YRadioButton.cc
00001 /*
00002   Copyright (C) 2000-2012 Novell, Inc
00003   This library is free software; you can redistribute it and/or modify
00004   it under the terms of the GNU Lesser General Public License as
00005   published by the Free Software Foundation; either version 2.1 of the
00006   License, or (at your option) version 3.0 of the License. This library
00007   is distributed in the hope that it will be useful, but WITHOUT ANY
00008   WARRANTY; without even the implied warranty of MERCHANTABILITY or 
00009   FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
00010   License for more details. You should have received a copy of the GNU
00011   Lesser General Public License along with this library; if not, write
00012   to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
00013   Floor, Boston, MA 02110-1301 USA
00014 */
00015 
00016 
00017 /*-/
00018 
00019   File:         YRadioButton.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #include <string>
00027 
00028 #define YUILogComponent "ui"
00029 #include "YUILog.h"
00030 
00031 #include "YUISymbols.h"
00032 #include "YUIException.h"
00033 #include "YMacroRecorder.h"
00034 #include "YRadioButtonGroup.h"
00035 #include "YRadioButton.h"
00036 
00037 
00038 
00039 struct YRadioButtonPrivate
00040 {
00041     /**
00042      * Constructor
00043      **/
00044     YRadioButtonPrivate( const std::string & label )
00045         : label( label )
00046         , radioButtonGroup( 0 )
00047         , useBoldFont( false )
00048         {}
00049 
00050     //
00051     // Data members
00052     //
00053 
00054     std::string         label;
00055     YRadioButtonGroup * radioButtonGroup;
00056     bool                useBoldFont;
00057 };
00058 
00059 
00060 YRadioButton::YRadioButton( YWidget *           parent,
00061                             const std::string & label )
00062     : YWidget( parent )
00063     , priv( new YRadioButtonPrivate( label ) )
00064 {
00065     YUI_CHECK_NEW( priv );
00066 
00067     // Intentionally not calling
00068     //     buttonGroup()->addRadioButton( this );
00069     // here because virtual functions can't be used yet (while the constructor
00070     // isn't finished yet), and the RadioButtonGroup for sure would try to call
00071     // YRadioButton::value() which is (pure) virtual, thus not available yet.
00072     //
00073     // The caller has to take care of this.
00074 }
00075 
00076 
00077 YRadioButton::~YRadioButton()
00078 {
00079     if ( priv->radioButtonGroup )
00080     {
00081         if ( ! priv->radioButtonGroup->beingDestroyed() )
00082             priv->radioButtonGroup->removeRadioButton( this );
00083     }
00084 }
00085 
00086 
00087 void YRadioButton::setLabel( const std::string & newLabel )
00088 {
00089     priv->label = newLabel;
00090 }
00091 
00092 
00093 std::string YRadioButton::label() const
00094 {
00095     return priv->label;
00096 }
00097 
00098 
00099 bool YRadioButton::useBoldFont() const
00100 {
00101     return priv->useBoldFont;
00102 }
00103 
00104 
00105 void YRadioButton::setUseBoldFont( bool bold )
00106 {
00107     priv->useBoldFont = bold;
00108 }
00109 
00110 
00111 const YPropertySet &
00112 YRadioButton::propertySet()
00113 {
00114     static YPropertySet propSet;
00115 
00116     if ( propSet.isEmpty() )
00117     {
00118         /*
00119          * @property boolean            Value   the on/off state of the RadioButton
00120          * @property std::string        Label   the text on the RadioButton
00121          */
00122 
00123         propSet.add( YProperty( YUIProperty_Value,      YBoolProperty   ) );
00124         propSet.add( YProperty( YUIProperty_Label,      YStringProperty ) );
00125         propSet.add( YWidget::propertySet() );
00126     }
00127 
00128     return propSet;
00129 }
00130 
00131 
00132 bool
00133 YRadioButton::setProperty( const std::string & propertyName, const YPropertyValue & val )
00134 {
00135     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00136 
00137     if      ( propertyName == YUIProperty_Value )       setValue( val.boolVal() );
00138     else if ( propertyName == YUIProperty_Label )       setLabel( val.stringVal() );
00139     else
00140     {
00141         return YWidget::setProperty( propertyName, val );
00142     }
00143 
00144     return true; // success -- no special processing necessary
00145 }
00146 
00147 
00148 YPropertyValue
00149 YRadioButton::getProperty( const std::string & propertyName )
00150 {
00151     propertySet().check( propertyName ); // throws exceptions if not found
00152 
00153     if      ( propertyName == YUIProperty_Value )       return YPropertyValue( value() );
00154     else if ( propertyName == YUIProperty_Label )       return YPropertyValue( label() );
00155     else
00156     {
00157         return YWidget::getProperty( propertyName );
00158     }
00159 }
00160 
00161 
00162 YRadioButtonGroup *
00163 YRadioButton::buttonGroup()
00164 {
00165     if ( ! priv->radioButtonGroup )
00166     {
00167         priv->radioButtonGroup = findRadioButtonGroup();
00168     }
00169 
00170     return priv->radioButtonGroup;
00171 }
00172 
00173 
00174 YRadioButtonGroup *
00175 YRadioButton::findRadioButtonGroup() const
00176 {
00177     YWidget * widget = parent();
00178 
00179     while ( widget )
00180     {
00181         YRadioButtonGroup * radioButtonGroup = dynamic_cast<YRadioButtonGroup *> (widget);
00182 
00183         if ( radioButtonGroup )
00184             return radioButtonGroup;
00185         else
00186             widget = widget->parent();
00187     }
00188 
00189     return 0;
00190 }
00191 
00192 
00193 void
00194 YRadioButton::saveUserInput( YMacroRecorder *macroRecorder )
00195 {
00196     if ( value() )
00197     {
00198         // Only record if this radio button is on. By definition one radio
00199         // button of the radio box _must_ be on if the user did anything, so we
00200         // don't record a lot of redundant "ChangeWidget( ..., `Value, false )"
00201         // calls.
00202 
00203         macroRecorder->recordWidgetProperty( this, YUIProperty_Value );
00204     }
00205 }
 All Classes Functions Variables Enumerations Friends