libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YComboBox.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:         YComboBox.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 "YComboBox.h"
00031 #include "YUIException.h"
00032 
00033 
00034 struct YComboBoxPrivate
00035 {
00036     YComboBoxPrivate( bool editable )
00037         : editable( editable )
00038         , inputMaxLength( -1 )
00039         {}
00040 
00041     bool        editable;
00042     std::string validChars;
00043     int         inputMaxLength;
00044 };
00045 
00046 
00047 
00048 
00049 YComboBox::YComboBox( YWidget * parent, const std::string & label, bool editable )
00050     : YSelectionWidget( parent, label,
00051                         true )          // enforceSingleSelection
00052     , priv( new YComboBoxPrivate( editable ) )
00053 {
00054     YUI_CHECK_NEW( priv );
00055 }
00056 
00057 
00058 YComboBox::~YComboBox()
00059 {
00060     // NOP
00061 }
00062 
00063 
00064 bool YComboBox::editable() const
00065 {
00066     return priv->editable;
00067 }
00068 
00069 
00070 std::string YComboBox::validChars()
00071 {
00072     return priv->validChars;
00073 }
00074 
00075 
00076 void YComboBox::setValidChars( const std::string & newValidChars )
00077 {
00078     priv->validChars= newValidChars;
00079 }
00080 
00081 
00082 int YComboBox::inputMaxLength() const
00083 {
00084     return priv->inputMaxLength;
00085 }
00086 
00087 
00088 void YComboBox::setInputMaxLength( int len )
00089 {
00090     priv->inputMaxLength = len;
00091 }
00092 
00093 
00094 std::string YComboBox::value()
00095 {
00096     return text();
00097 }
00098 
00099 
00100 void YComboBox::setValue( const std::string & newText )
00101 {
00102     YItem * item = findItem( newText );
00103 
00104     if ( item )
00105     {
00106         YSelectionWidget::deselectAllItems();
00107         item->setSelected();
00108         setText( item->label() );
00109     }
00110     else
00111     {
00112         if ( editable() )
00113             setText( newText );
00114         else
00115         {
00116             YUI_THROW( YUIException( "Invalid value" ) );
00117         }
00118     }
00119 }
00120 
00121 
00122 void YComboBox::selectItem( YItem * item, bool selected )
00123 {
00124     // Check against null pointer and if the item actually belongs to this
00125     // widget, deselect any previously selected item and select the new one
00126     YSelectionWidget::selectItem( item, selected );
00127 
00128     if ( selected )
00129     {
00130         setText( item->label() );
00131     }
00132 }
00133 
00134 
00135 YItem *
00136 YComboBox::selectedItem()
00137 {
00138     std::string currentText = text();
00139 
00140     // Make sure exactly this item is selected (and no other)
00141     YSelectionWidget::deselectAllItems();
00142 
00143     // Try to find an item with this text
00144     YItem * item = findItem( currentText );
00145 
00146     if ( item )
00147     {
00148         item->setSelected( true );
00149         return item;
00150     }
00151 
00152     return 0;
00153 }
00154 
00155 
00156 YItemCollection
00157 YComboBox::selectedItems()
00158 {
00159     YItemCollection selectedItems;
00160 
00161     // There can be no more than one selected item
00162     YItem * item = selectedItem();
00163 
00164     if ( item )
00165         selectedItems.push_back( item );
00166 
00167     return selectedItems;
00168 }
00169 
00170 
00171 const YPropertySet &
00172 YComboBox::propertySet()
00173 {
00174     static YPropertySet propSet;
00175 
00176     if ( propSet.isEmpty() )
00177     {
00178         /*
00179          * @property itemID | std::string       Value           ID of the selected item or the text the user entered
00180          * @property std::string                Label           caption above the combo box
00181          * @property itemList                   Items           All items
00182          * @property std::string                ValidChars      set of valid input characters
00183          * @property integer                    InputMaxLength  maximum number of input characters
00184          * @property std::string                IconPath        Base path for icons
00185          */
00186         propSet.add( YProperty( YUIProperty_Value,              YOtherProperty   ) );
00187         propSet.add( YProperty( YUIProperty_Items,              YOtherProperty   ) );
00188         propSet.add( YProperty( YUIProperty_Label,              YStringProperty  ) );
00189         propSet.add( YProperty( YUIProperty_ValidChars,         YStringProperty  ) );
00190         propSet.add( YProperty( YUIProperty_InputMaxLength,     YIntegerProperty ) );
00191         propSet.add( YProperty( YUIProperty_IconPath,           YStringProperty  ) );
00192         propSet.add( YWidget::propertySet() );
00193     }
00194 
00195     return propSet;
00196 }
00197 
00198 
00199 bool
00200 YComboBox::setProperty( const std::string & propertyName, const YPropertyValue & val )
00201 {
00202     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00203 
00204     if      ( propertyName == YUIProperty_Value         )       return false; // Need special handling
00205     else if ( propertyName == YUIProperty_Items         )       return false; // Needs special handling
00206     else if ( propertyName == YUIProperty_Label         )       setLabel( val.stringVal() );
00207     else if ( propertyName == YUIProperty_ValidChars    )       setValidChars( val.stringVal() );
00208     else if ( propertyName == YUIProperty_InputMaxLength )      setInputMaxLength( val.integerVal() );
00209     else if ( propertyName == YUIProperty_IconPath      )       setIconBasePath( val.stringVal() );
00210     else
00211     {
00212         return YWidget::setProperty( propertyName, val );
00213     }
00214 
00215     return true; // success -- no special processing necessary
00216 }
00217 
00218 
00219 YPropertyValue
00220 YComboBox::getProperty( const std::string & propertyName )
00221 {
00222     propertySet().check( propertyName ); // throws exceptions if not found
00223 
00224     if      ( propertyName == YUIProperty_Value         )       return YPropertyValue( YOtherProperty );
00225     else if ( propertyName == YUIProperty_Items         )       return YPropertyValue( YOtherProperty );
00226     else if ( propertyName == YUIProperty_Label         )       return YPropertyValue( label() );
00227     else if ( propertyName == YUIProperty_ValidChars    )       return YPropertyValue( validChars() );
00228     else if ( propertyName == YUIProperty_InputMaxLength )      return YPropertyValue( inputMaxLength() );
00229     else if ( propertyName == YUIProperty_IconPath      )       return YPropertyValue( iconBasePath() );
00230     else
00231     {
00232         return YWidget::getProperty( propertyName );
00233     }
00234 }
 All Classes Functions Variables Enumerations Friends