libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YInputField.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:         YInputField.cc
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #define YUILogComponent "ui"
00027 #include "YUILog.h"
00028 
00029 #include "YUISymbols.h"
00030 #include "YMacroRecorder.h"
00031 #include "YInputField.h"
00032 
00033 
00034 
00035 struct YInputFieldPrivate
00036 {
00037     YInputFieldPrivate( std::string label, bool passwordMode )
00038         : label( label )
00039         , passwordMode( passwordMode )
00040         , shrinkable( false )
00041         , inputMaxLength( -1 )
00042         {}
00043 
00044     std::string label;
00045     bool        passwordMode;
00046     bool        shrinkable;
00047     std::string validChars;
00048     int         inputMaxLength;
00049 };
00050 
00051 
00052 
00053 YInputField::YInputField( YWidget * parent, const std::string & label, bool passwordMode )
00054     : YWidget( parent )
00055     , priv( new YInputFieldPrivate( label, passwordMode ) )
00056 {
00057     YUI_CHECK_NEW( priv );
00058 
00059     // setDefaultStretchable( YD_HORIZ, true  );
00060     setDefaultStretchable( YD_VERT,  false );
00061 }
00062 
00063 
00064 YInputField::~YInputField()
00065 {
00066     // NOP
00067 }
00068 
00069 
00070 std::string YInputField::label() const
00071 {
00072     return priv->label;
00073 }
00074 
00075 
00076 void YInputField::setLabel( const std::string & label )
00077 {
00078     priv->label = label;
00079 }
00080 
00081 
00082 bool YInputField::passwordMode() const
00083 {
00084     return priv->passwordMode;
00085 }
00086 
00087 
00088 bool YInputField::shrinkable() const
00089 {
00090     return priv->shrinkable;
00091 }
00092 
00093 
00094 void YInputField::setShrinkable( bool shrinkable )
00095 {
00096     priv->shrinkable = shrinkable;
00097     // setDefaultStretchable( YD_HORIZ, ! shrinkable );
00098 }
00099 
00100 
00101 std::string YInputField::validChars()
00102 {
00103     return priv->validChars;
00104 }
00105 
00106 
00107 void YInputField::setValidChars( const std::string & newValidChars )
00108 {
00109     priv->validChars= newValidChars;
00110 }
00111 
00112 
00113 int YInputField::inputMaxLength() const
00114 {
00115     return priv->inputMaxLength;
00116 }
00117 
00118 
00119 void YInputField::setInputMaxLength( int len )
00120 {
00121     priv->inputMaxLength = len;
00122 }
00123 
00124 
00125 const YPropertySet &
00126 YInputField::propertySet()
00127 {
00128     static YPropertySet propSet;
00129 
00130     if ( propSet.isEmpty() )
00131     {
00132         /*
00133          * @property std::string        Value           the input field's contents (the user input)
00134          * @property std::string        Label           caption above the input field
00135          * @property std::string        ValidChars      set of valid input characters
00136          * @property integer            InputMaxLength  maximum number of input characters
00137          */
00138         propSet.add( YProperty( YUIProperty_Value,              YStringProperty  ) );
00139         propSet.add( YProperty( YUIProperty_Label,              YStringProperty  ) );
00140         propSet.add( YProperty( YUIProperty_ValidChars,         YStringProperty  ) );
00141         propSet.add( YProperty( YUIProperty_InputMaxLength,     YIntegerProperty ) );
00142         propSet.add( YWidget::propertySet() );
00143     }
00144 
00145     return propSet;
00146 }
00147 
00148 
00149 bool
00150 YInputField::setProperty( const std::string & propertyName, const YPropertyValue & val )
00151 {
00152     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00153 
00154     if      ( propertyName == YUIProperty_Value         )       setValue( val.stringVal() );
00155     else if ( propertyName == YUIProperty_Label         )       setLabel( val.stringVal() );
00156     else if ( propertyName == YUIProperty_ValidChars    )       setValidChars( val.stringVal() );
00157     else if ( propertyName == YUIProperty_InputMaxLength )      setInputMaxLength( val.integerVal() );
00158     else
00159     {
00160         return YWidget::setProperty( propertyName, val );
00161     }
00162 
00163     return true; // success -- no special processing necessary
00164 }
00165 
00166 
00167 YPropertyValue
00168 YInputField::getProperty( const std::string & propertyName )
00169 {
00170     propertySet().check( propertyName ); // throws exceptions if not found
00171 
00172     if      ( propertyName == YUIProperty_Value         )       return YPropertyValue( value() );
00173     else if ( propertyName == YUIProperty_Label         )       return YPropertyValue( label() );
00174     else if ( propertyName == YUIProperty_ValidChars    )       return YPropertyValue( validChars() );
00175     else if ( propertyName == YUIProperty_InputMaxLength )      return YPropertyValue( inputMaxLength() );
00176     else
00177     {
00178         return YWidget::getProperty( propertyName );
00179     }
00180 }
00181 
00182 
00183 void
00184 YInputField::saveUserInput( YMacroRecorder *macroRecorder )
00185 {
00186     if ( ! passwordMode() ) // Don't record passwords in the macro file
00187     {
00188         macroRecorder->recordWidgetProperty( this, YUIProperty_Value );
00189     }
00190 }
00191 
00192 
00193 const char *
00194 YInputField::widgetClass() const
00195 {
00196     if ( priv->passwordMode )   return "YPasswordField";
00197     else                        return "YInputField";
00198 }
 All Classes Functions Variables Enumerations Friends