libyui  3.0.10
/usr/src/RPM/BUILD/libyui-3.0.10/src/YShortcut.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:         YShortcut.h
00020 
00021   Author:       Stefan Hundhammer <sh@suse.de>
00022 
00023 /-*/
00024 
00025 
00026 #ifndef YShortcut_h
00027 #define YShortcut_h
00028 
00029 #include "YWidget.h"
00030 #include <string>
00031 #include <vector>
00032 
00033 class YItem;
00034 
00035 
00036 /**
00037  * Helper class for shortcut management:
00038  * This class holds data about the shortcut for one single widget.
00039  **/
00040 class YShortcut
00041 {
00042 public:
00043     /**
00044      * Constructor
00045      **/
00046     YShortcut( YWidget *shortcut_widget );
00047 
00048     /**
00049      * Destructor
00050      **/
00051     virtual ~YShortcut();
00052 
00053     /**
00054      * Marker for "no shortcut"
00055      **/
00056     enum { None = 0 };
00057 
00058     /**
00059      * Returns the YWidget this shortcut data belong to.
00060      **/
00061     YWidget * widget() const { return _widget; }
00062 
00063     /**
00064      * Returns the textual representation of the widget class of the widget
00065      * this shortcut data belongs to.
00066      **/
00067     const char * widgetClass() const { return widget()->widgetClass(); }
00068 
00069     /**
00070      * Returns 'true' if the widget that is associated with this shortcut is a
00071      * button (derived from YPushButton).
00072      **/
00073     bool isButton() const { return _isButton; }
00074 
00075     /**
00076      * Returns 'true' if the widget that is associated with this shortcut is a
00077      * wizard button (one of the navigation buttons of a wizard).
00078      **/
00079     bool isWizardButton() const { return _isWizardButton; }
00080 
00081     /**
00082      * Returns the complete shortcut string (which may or may not contain "&"),
00083      * i.e. the value of the widget's shortcut property. For PushButtons, this
00084      * is the label on the button ( e.g., "&Details..." ), for other widgets
00085      * usually the caption above it.
00086      *
00087      * This value is chached, i.e. this isn't a too expensive operation.
00088      **/
00089     std::string shortcutString();
00090 
00091     /**
00092      * Returns the shortcut string ( from the widget's shortcut property )
00093      * without any "&" markers.
00094      **/
00095     std::string cleanShortcutString();
00096 
00097     /**
00098      * Static version of the above for general use:
00099      * Returns the specified string without any "&" markers.
00100      **/
00101     static std::string cleanShortcutString( std::string shortcutString );
00102 
00103     /**
00104      * The preferred shortcut character, i.e. the character that had been
00105      * preceded by "&" before checking / resolving conflicts began.
00106      **/
00107     char preferred();
00108 
00109     /**
00110      * The actual shortcut character.
00111      *
00112      * This may be different from preferred() if it is overridden.
00113      **/
00114     char shortcut();
00115 
00116     /**
00117      * Set (override) the shortcut character.
00118      **/
00119     virtual void setShortcut( char newShortcut );
00120 
00121     /**
00122      * Clear the shortcut: Override the shortcut character with nothing.
00123      * This may happen if a conflict cannot be resolved.
00124      **/
00125     void clearShortcut();
00126 
00127     /**
00128      * Query the internal 'conflict' marker. This class doesn't care about that
00129      * flag, it just stores it for the convenience of higher-level classes.
00130      **/
00131     bool conflict() { return _conflict; }
00132 
00133     /**
00134      * Set or unset the internal 'conflict' marker.
00135      **/
00136     void setConflict( bool newConflictState = true ) { _conflict = newConflictState; }
00137 
00138     /**
00139      * Obtain the number of distinct valid shortcut characters in the shortcut
00140      * string, i.e. how many different shortcuts that widget could get.
00141      **/
00142     int distinctShortcutChars();
00143 
00144     /**
00145      * Return true if this shortcut contains any character that would be valid
00146      * as a shortcut character.
00147      **/
00148     bool hasValidShortcutChar();
00149 
00150     /**
00151      * Static function: Returns the character used for marking keyboard
00152      * shortcuts.
00153      **/
00154     static char shortcutMarker() { return '&'; }
00155 
00156     /**
00157      * Static function: Find the next occurrence of the shortcut marker ('&')
00158      * in a string, beginning at starting position start_pos.
00159      *
00160      * Returns string::npos if not found or the position of the shortcut marker
00161      * (not the shortcut character!) if found.
00162      **/
00163     static std::string::size_type findShortcutPos( const std::string & str, std::string::size_type start_pos = 0 );
00164 
00165     /**
00166      * Static function: Find the next shortcut marker in a string, beginning at
00167      * starting position start_pos.
00168      *
00169      * Returns the shortcut character or 0 if none found.
00170      **/
00171     static char findShortcut( const std::string & str, std::string::size_type start_pos = 0 );
00172 
00173     /**
00174      * Returns 'true' if 'c' is a valid shortcut character, i.e. [a-zA-Z0-9],
00175      * 'false' otherwise.
00176      **/
00177     static bool isValid( char c );
00178 
00179     /**
00180      * Return the normalized version of shortcut character 'c', i.e. a
00181      * lowercase letter or a digit [a-z0-9]. Returns 0 if 'c' is invalid.
00182      **/
00183     static char normalized( char c );
00184 
00185     /**
00186      * Obtain a widget's shortcut property - the string that contains "&" to
00187      * designate a shortcut.
00188      **/
00189     static std::string getShortcutString( const YWidget * widget );
00190 
00191 
00192 protected:
00193 
00194     /**
00195      * Obtain the the shortcut property of this shortcut's widget - the string
00196      * that contains "&" to designate a shortcut.
00197      **/
00198     virtual std::string getShortcutString();
00199 
00200 
00201     // Data members
00202 
00203     YWidget *   _widget;
00204     std::string _shortcutString;
00205     bool        _shortcutStringCached;
00206 
00207     std::string _cleanShortcutString;
00208     bool        _cleanShortcutStringCached;
00209 
00210     int         _preferred;     // int to enable initializing with invalid char (-1)
00211     int         _shortcut;      // int to enable initializing with invalid char (-1)
00212 
00213     bool        _conflict;
00214     bool        _isButton;
00215     bool        _isWizardButton;
00216     int         _distinctShortcutChars;
00217 };
00218 
00219 
00220 
00221 /**
00222  * Special case for widgets that can have multiple shortcuts based on items
00223  * (like YDumbTab)
00224  **/
00225 class YItemShortcut: public YShortcut
00226 {
00227 public:
00228     /**
00229      * Constructor.
00230      **/
00231     YItemShortcut( YWidget * widget, YItem * item )
00232         : YShortcut( widget )
00233         , _item( item )
00234         {}
00235 
00236     /**
00237      * Destructor.
00238      **/
00239     virtual ~YItemShortcut() {}
00240 
00241     /**
00242      * Return the associated item.
00243      **/
00244     YItem * item() const { return _item; }
00245 
00246     /**
00247      * Set (override) the shortcut character.
00248      * In this subclass, it will change the internally stored item.
00249      **/
00250     virtual void setShortcut( char newShortcut );
00251 
00252 protected:
00253 
00254     /**
00255      * Obtain the the shortcut property of this shortcut's widget - the string
00256      * that contains "&" to designate a shortcut.
00257      **/
00258     virtual std::string getShortcutString();
00259 
00260 
00261 private:
00262 
00263     YItem * _item;
00264 };
00265 
00266 
00267 typedef std::vector<YShortcut *>        YShortcutList;
00268 typedef YShortcutList::iterator         YShortcutListIterator;
00269 
00270 
00271 #endif // YShortcut_h
 All Classes Functions Variables Enumerations Friends