libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YMenuButton.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:         YMenuButton.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 "YMenuButton.h"
00031 #include "YMenuItem.h"
00032 
00033 
00034 struct YMenuButtonPrivate
00035 {
00036     YMenuButtonPrivate()
00037         : nextSerialNo( 0 )
00038         {}
00039 
00040     int nextSerialNo;
00041 };
00042 
00043 
00044 
00045 
00046 YMenuButton::YMenuButton( YWidget * parent, const std::string & label )
00047     : YSelectionWidget( parent, label,
00048                         false ) // enforceSingleSelection
00049     , priv( new YMenuButtonPrivate() )
00050 {
00051     YUI_CHECK_NEW( priv );
00052 }
00053 
00054 
00055 YMenuButton::~YMenuButton()
00056 {
00057     // NOP
00058 }
00059 
00060 
00061 void
00062 YMenuButton::addItems( const YItemCollection & itemCollection )
00063 {
00064     YSelectionWidget::addItems( itemCollection );
00065     resolveShortcutConflicts();
00066     rebuildMenuTree();
00067 }
00068 
00069 
00070 void
00071 YMenuButton::addItem( YItem * item )
00072 {
00073     YSelectionWidget::addItem( item );
00074     item->setIndex( ++(priv->nextSerialNo) );
00075 
00076     if ( item->hasChildren() )
00077         assignUniqueIndex( item->childrenBegin(), item->childrenEnd() );
00078 }
00079 
00080 
00081 void
00082 YMenuButton::assignUniqueIndex( YItemIterator begin, YItemIterator end )
00083 {
00084     for ( YItemIterator it = begin; it != end; ++it )
00085     {
00086         YItem * item = *it;
00087 
00088         item->setIndex( ++(priv->nextSerialNo) );
00089 
00090         if ( item->hasChildren() )
00091             assignUniqueIndex( item->childrenBegin(), item->childrenEnd() );
00092     }
00093 }
00094 
00095 
00096 void
00097 YMenuButton::deleteAllItems()
00098 {
00099     YSelectionWidget::deleteAllItems();
00100     priv->nextSerialNo = 0;
00101 }
00102 
00103 
00104 YMenuItem *
00105 YMenuButton::findMenuItem( int index )
00106 {
00107     return findMenuItem( index, itemsBegin(), itemsEnd() );
00108 }
00109 
00110 
00111 YMenuItem *
00112 YMenuButton::findMenuItem( int wantedIndex, YItemConstIterator begin, YItemConstIterator end )
00113 {
00114     for ( YItemConstIterator it = begin; it != end; ++it )
00115     {
00116         YMenuItem * item = dynamic_cast<YMenuItem *> (*it);
00117 
00118         if ( item )
00119         {
00120             if ( item->index() == wantedIndex )
00121                 return item;
00122 
00123             if ( item->hasChildren() )
00124             {
00125                 YMenuItem * result = findMenuItem( wantedIndex, item->childrenBegin(), item->childrenEnd() );
00126 
00127                 if ( result )
00128                     return result;
00129             }
00130         }
00131     }
00132 
00133     return 0;
00134 }
00135 
00136 
00137 void
00138 YMenuButton::resolveShortcutConflicts()
00139 {
00140     // TO DO
00141     // TO DO
00142     // TO DO
00143 
00144     // For every menu level, make sure keyboard shortcuts are unique within that menu level.
00145     // If necessary, change some of them (move the '&' character to some other character).
00146 
00147 
00148     // See YShortcutManager for more hints.
00149 }
00150 
00151 
00152 const YPropertySet &
00153 YMenuButton::propertySet()
00154 {
00155     static YPropertySet propSet;
00156 
00157     if ( propSet.isEmpty() )
00158     {
00159         /*
00160          * @property std::string        Label           Label on the menu button
00161          * @property itemList           Items           All menu items and submenus
00162          * @property std::string        IconPath        Base path for icons (on menu items)
00163          */
00164         propSet.add( YProperty( YUIProperty_Label,              YStringProperty  ) );
00165         propSet.add( YProperty( YUIProperty_Items,              YOtherProperty   ) );
00166         propSet.add( YProperty( YUIProperty_IconPath,           YStringProperty  ) );
00167         propSet.add( YWidget::propertySet() );
00168     }
00169 
00170     return propSet;
00171 }
00172 
00173 
00174 bool
00175 YMenuButton::setProperty( const std::string & propertyName, const YPropertyValue & val )
00176 {
00177     propertySet().check( propertyName, val.type() ); // throws exceptions if not found or type mismatch
00178 
00179     if      ( propertyName == YUIProperty_Label         )       setLabel( val.stringVal() );
00180     else if ( propertyName == YUIProperty_Items         )       return false; // Needs special handling
00181     else if ( propertyName == YUIProperty_IconPath      )       setIconBasePath( val.stringVal() );
00182     else
00183     {
00184         return YWidget::setProperty( propertyName, val );
00185     }
00186 
00187     return true; // success -- no special processing necessary
00188 }
00189 
00190 
00191 YPropertyValue
00192 YMenuButton::getProperty( const std::string & propertyName )
00193 {
00194     propertySet().check( propertyName ); // throws exceptions if not found
00195 
00196     if      ( propertyName == YUIProperty_Label         )       return YPropertyValue( label() );
00197     else if ( propertyName == YUIProperty_Items         )       return YPropertyValue( YOtherProperty );
00198     else if ( propertyName == YUIProperty_IconPath      )       return YPropertyValue( iconBasePath() );
00199     else
00200     {
00201         return YWidget::getProperty( propertyName );
00202     }
00203 }
 All Classes Functions Variables Enumerations Friends