libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YItem.h
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:         YItem.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 #ifndef YItem_h
00026 #define YItem_h
00027 
00028 #include <string>
00029 #include <vector>
00030 
00031 
00032 class YItem;
00033 
00034 typedef std::vector<YItem *>                    YItemCollection;
00035 typedef YItemCollection::iterator               YItemIterator;
00036 typedef YItemCollection::const_iterator         YItemConstIterator;
00037 
00038 
00039 /**
00040  * Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc. items.
00041  * This class provides stubs for children management.
00042  **/
00043 class YItem
00044 {
00045 public:
00046     /**
00047      * Constructor with just the label and optionally the selected state.
00048      **/
00049     YItem( const std::string & label, bool selected = false )
00050         : _label( label )
00051         , _selected( selected )
00052         , _index( -1 )
00053         , _data( 0 )
00054         {}
00055 
00056     /**
00057      * Constructor with label and icon name and optionally the selected state.
00058      **/
00059     YItem( const std::string & label, const std::string & iconName, bool selected = false )
00060         : _label( label )
00061         , _iconName( iconName )
00062         , _selected( selected )
00063         , _index( -1 )
00064         , _data( 0 )
00065         {}
00066 
00067     /**
00068      * Destructor.
00069      **/
00070     virtual ~YItem() {}
00071 
00072     /**
00073      * Return this item's label. This is what the user sees in a dialog, so
00074      * this will usually be a translated text.
00075      **/
00076     std::string label() const { return _label; }
00077 
00078     /**
00079      * Set this item's label.
00080      **/
00081     void setLabel( const std::string & newLabel ) { _label = newLabel; }
00082 
00083     /**
00084      * Return this item's icon name.
00085      **/
00086     std::string iconName() const { return _iconName; }
00087 
00088     /**
00089      * Return 'true' if this item has an icon name.
00090      **/
00091     bool hasIconName() const { return ! _iconName.empty(); }
00092 
00093     /**
00094      * Set this item's icon name.
00095      **/
00096     void setIconName( const std::string & newIconName ) { _iconName = newIconName; }
00097 
00098     /**
00099      * Return 'true' if this item is currently selected.
00100      **/
00101     bool selected() const { return _selected; }
00102 
00103     /**
00104      * Select or unselect this item. This does not have any effect on any other
00105      * item; if it is desired that only one item is selected at any time, the
00106      * caller has to take care of that.
00107      **/
00108     void setSelected( bool sel = true ) { _selected = sel; }
00109 
00110     /**
00111      * Set this item's index.
00112      **/
00113     void setIndex( int index ) { _index = index; }
00114 
00115     /**
00116      * Return the index of this item (as set with setIndex() ).
00117      **/
00118     int index() const { return _index; }
00119 
00120     /**
00121      * Set the opaque data pointer for application use.
00122      *
00123      * Applications can use this to store the pointer to a counterpart of this
00124      * tree item. It is the application's responsibility to watch for dangling
00125      * pointers and possibliy deleting the data. All this class ever does with
00126      * this pointer is to store it.
00127      **/
00128     void setData( void * newData ) { _data = newData; }
00129 
00130     /**
00131      * Return the opaque data pointer.
00132      **/
00133     void * data() const { return _data; }
00134 
00135     //
00136     // Children management stubs.
00137     //
00138     // Derived classes that can handle child items should reimplement those
00139     // functions.
00140     // The following default implementations don't do anything with children;
00141     // they act as if this item didn't have any children.
00142     //
00143 
00144     /**
00145      * Return 'true' if this item has any child items.
00146      **/
00147     virtual bool hasChildren() const { return false; }
00148 
00149     /**
00150      * Return an iterator that points to the first child item of this item.
00151      *
00152      * This default implementation returns the 'end' iterator of the
00153      * class-static always empty _noChildren YItemCollection.
00154      * It is safe to use this iterator in classic iterator loops:
00155      *
00156      *   for ( YItemIterator it = myItem->childrenBegin();
00157      *         it != myItem->childrenEnd();
00158      *         ++it )
00159      *   {
00160      *       ...
00161      *   }
00162      *
00163      * The loop body will only ever be executed if this item is a derived class
00164      * that actually manages child items.
00165      **/
00166     virtual YItemIterator       childrenBegin()         { return _noChildren.end(); }
00167     virtual YItemConstIterator  childrenBegin() const   { return _noChildren.end(); }
00168 
00169     /**
00170      * Return an iterator that points after the last child item of this item.
00171      *
00172      * This default implementation returns the 'end' iterator of the
00173      * class-static always empty _noChildren YItemCollection.
00174      **/
00175     virtual YItemIterator       childrenEnd()           { return _noChildren.end(); }
00176     virtual YItemConstIterator  childrenEnd() const     { return _noChildren.end(); }
00177 
00178     /**
00179      * Returns this item's parent item or 0 if it is a toplevel item.
00180      * This default implementation always returns 0.
00181      * Derived classes that handle children should reimplement this.
00182      **/
00183     virtual YItem * parent() const { return 0; }
00184 
00185 
00186 private:
00187 
00188     std::string _label;
00189     std::string _iconName;
00190     bool        _selected;
00191     int         _index;
00192     void *      _data;
00193 
00194     /**
00195      * Static children collection that is always empty so the children
00196      * iterators of this base class have something valid to return.
00197      **/
00198     static YItemCollection _noChildren;
00199 };
00200 
00201 
00202 
00203 #endif // YItem_h
 All Classes Functions Variables Enumerations Friends