libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YPushButton.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:         YPushButton.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "ui"
00027 #include "YUILog.h"
00028 
00029 #include "YUI.h"
00030 #include "YApplication.h"
00031 #include "YDialog.h"
00032 #include "YUISymbols.h"
00033 #include "YPushButton.h"
00034 
00035 using std::endl;
00036 
00037 
00038 struct YPushButtonPrivate
00039 {
00040     YPushButtonPrivate( const std::string & label )
00041         : label( label )
00042         , isDefaultButton( false )
00043         , setDefaultButtonRecursive( false )
00044         , isHelpButton( false )
00045         , role( YCustomButton )
00046         {}
00047 
00048     std::string label;
00049     bool        isDefaultButton;
00050     bool        setDefaultButtonRecursive;
00051     bool        isHelpButton;
00052     YButtonRole role;
00053 };
00054 
00055 
00056 YPushButton::YPushButton( YWidget * parent, const std::string & label )
00057     : YWidget( parent )
00058     , priv( new YPushButtonPrivate( label ) )
00059 {
00060     int fkey = YUI::app()->defaultFunctionKey( label );
00061 
00062     if ( fkey > 0 && ! hasFunctionKey() )
00063         setFunctionKey( fkey );
00064 }
00065 
00066 
00067 YPushButton::~YPushButton()
00068 {
00069     YDialog * dialog = findDialog();
00070 
00071     if ( dialog && dialog->defaultButton() == this )
00072     {
00073         dialog->setDefaultButton( 0 );
00074     }
00075 }
00076 
00077 
00078 void YPushButton::setLabel( const std::string & label )
00079 {
00080     priv->label = label;
00081 }
00082 
00083 
00084 std::string YPushButton::label() const
00085 {
00086     return priv->label;
00087 }
00088 
00089 
00090 bool YPushButton::isDefaultButton() const
00091 {
00092     return priv->isDefaultButton;
00093 }
00094 
00095 
00096 void YPushButton::setDefaultButton( bool isDefaultButton )
00097 {
00098     priv->isDefaultButton = isDefaultButton;
00099 
00100     if ( ! priv->setDefaultButtonRecursive )
00101     {
00102         // Prevent endless recursion if dialog->setDefaultButton()
00103         // calls this function again
00104 
00105         priv->setDefaultButtonRecursive = true;
00106 
00107         YDialog * dialog = findDialog();
00108 
00109         if ( dialog )
00110         {
00111             if ( isDefaultButton )
00112                 dialog->setDefaultButton( this );
00113             else
00114             {
00115                 if ( dialog->defaultButton() == this )
00116                     dialog->setDefaultButton( 0 );
00117             }
00118         }
00119 
00120         priv->setDefaultButtonRecursive = false;
00121     }
00122 }
00123 
00124 
00125 bool YPushButton::isHelpButton() const
00126 {
00127     return priv->isHelpButton;
00128 }
00129 
00130 
00131 void YPushButton::setHelpButton( bool helpButton )
00132 {
00133     priv->isHelpButton = helpButton;
00134     priv->role = YHelpButton;
00135 }
00136 
00137 /* setRole can try to guess function key, but only if there isn't a selected
00138    function key already
00139 */
00140 void YPushButton::setRole( YButtonRole role )
00141 {
00142         priv->role = role;
00143         int old_function_key = functionKey();
00144         if (!hasFunctionKey()) // otherwise function key was already determined
00145         {
00146                 switch (priv->role)
00147                 {
00148                         case YOKButton:     YWidget::setFunctionKey( 10 );  break;
00149                         case YCancelButton: YWidget::setFunctionKey( 9 );   break;
00150                         case YApplyButton:  YWidget::setFunctionKey( 10 );  break;
00151                         case YHelpButton:   YWidget::setFunctionKey( 1 );   break;
00152                         default: break;
00153                 }
00154                 if ( functionKey() != old_function_key )
00155                 {
00156                         yuiMilestone() << "Guessing function key F" << functionKey()
00157                                << " for " << this
00158                                << " from button role " << priv->role
00159                                << endl;
00160                 }
00161         }
00162 }
00163 
00164 YButtonRole YPushButton::role() const
00165 {
00166     return priv->role;
00167 }
00168 
00169 /* setFunctionKey (besides setting the function key) should try to guess button
00170    role, but only if button role is not yet determined.
00171 */
00172 void YPushButton::setFunctionKey( int fkey_no )
00173 {
00174     YWidget::setFunctionKey( fkey_no );
00175     YButtonRole oldRole = priv->role;
00176 
00177         if (priv->role == YCustomButton) // otherwise role was already determined
00178         {
00179                 switch ( functionKey() )        // base class method might have changed it
00180                 {
00181                         case 10:    priv->role = YOKButton;     break;
00182                         case 9:     priv->role = YCancelButton; break;
00183                         case 1:     priv->role = YHelpButton;   break;
00184                         default:    break;
00185                 }
00186                 if ( priv->role != oldRole )
00187                 {
00188                         yuiMilestone() << "Guessing button role " << priv->role
00189                                << " for " << this
00190                                << " from function key F" << functionKey()
00191                                << endl;
00192                 }
00193         }
00194 }
00195 
00196 
00197 const YPropertySet &
00198 YPushButton::propertySet()
00199 {
00200     static YPropertySet propSet;
00201 
00202     if ( propSet.isEmpty() )
00203     {
00204         /*
00205          * @property std::string Label  text on the button
00206          */
00207         propSet.add( YProperty( YUIProperty_Label, YStringProperty ) );
00208         propSet.add( YWidget::propertySet() );
00209     }
00210 
00211     return propSet;
00212 }
00213 
00214 
00215 bool
00216 YPushButton::setProperty( const std::string & propertyName, const YPropertyValue & val )
00217 {
00218     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00219 
00220     if ( propertyName == YUIProperty_Label )    setLabel( val.stringVal() );
00221     else
00222     {
00223         YWidget::setProperty( propertyName, val );
00224     }
00225 
00226     return true; // success -- no special handling necessary
00227 }
00228 
00229 
00230 YPropertyValue
00231 YPushButton::getProperty( const std::string & propertyName )
00232 {
00233     propertySet().check( propertyName ); // throws exceptions if not found
00234 
00235     if ( propertyName == YUIProperty_Label )    return YPropertyValue( label() );
00236     else
00237     {
00238         return YWidget::getProperty( propertyName );
00239     }
00240 }
00241 
00242 
00243 std::ostream & operator<<( std::ostream & stream, YButtonRole role )
00244 {
00245     switch ( role )
00246     {
00247         case YCustomButton:     stream << "YCustomButton";      break;
00248         case YOKButton:         stream << "YOKButton";          break;
00249         case YApplyButton:      stream << "YApplyButton";       break;
00250         case YCancelButton:     stream << "YCancelButton";      break;
00251         case YHelpButton:       stream << "YHelpButton";        break;
00252 
00253         default:
00254             stream << "<Undefined button role #" << (int) role << ">";
00255             break;
00256     }
00257 
00258     return stream;
00259 }
 All Classes Functions Variables Enumerations Friends