libyui  3.12.1
YShortcut.h
1 /*
2  Copyright (C) 2000-2012 Novell, Inc
3  This library is free software; you can redistribute it and/or modify
4  it under the terms of the GNU Lesser General Public License as
5  published by the Free Software Foundation; either version 2.1 of the
6  License, or (at your option) version 3.0 of the License. This library
7  is distributed in the hope that it will be useful, but WITHOUT ANY
8  WARRANTY; without even the implied warranty of MERCHANTABILITY or
9  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
10  License for more details. You should have received a copy of the GNU
11  Lesser General Public License along with this library; if not, write
12  to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
13  Floor, Boston, MA 02110-1301 USA
14 */
15 
16 
17 /*-/
18 
19  File: YShortcut.h
20 
21  Author: Stefan Hundhammer <shundhammer@suse.de>
22 
23 /-*/
24 
25 
26 #ifndef YShortcut_h
27 #define YShortcut_h
28 
29 #include "YWidget.h"
30 #include <string>
31 #include <vector>
32 
33 class YItem;
34 
35 
36 /**
37  * Helper class for shortcut management:
38  * This class holds data about the shortcut for one single widget.
39  **/
40 class YShortcut
41 {
42 public:
43  /**
44  * Constructor
45  * @param shortcut_widget (not owned, not nullptr)
46  **/
47  YShortcut( YWidget *shortcut_widget );
48 
49  /**
50  * Destructor
51  **/
52  virtual ~YShortcut();
53 
54  /**
55  * Marker for "no shortcut"
56  **/
57  enum { None = 0 };
58 
59  /**
60  * Returns the YWidget this shortcut data belong to.
61  **/
62  YWidget * widget() const { return _widget; }
63 
64  /**
65  * Returns the textual representation of the widget class of the widget
66  * this shortcut data belongs to.
67  **/
68  const char * widgetClass() const { return widget()->widgetClass(); }
69 
70  /**
71  * Returns 'true' if the widget that is associated with this shortcut is a
72  * button (derived from YPushButton).
73  **/
74  bool isButton() const { return _isButton; }
75 
76  /**
77  * Returns 'true' if the widget that is associated with this shortcut is a
78  * wizard button (one of the navigation buttons of a wizard).
79  **/
80  bool isWizardButton() const { return _isWizardButton; }
81 
82  /**
83  * Returns the complete shortcut string (which may or may not contain "&"),
84  * i.e. the value of the widget's shortcut property. For PushButtons, this
85  * is the label on the button ( e.g., "&Details..." ), for other widgets
86  * usually the caption above it.
87  *
88  * This value is chached, i.e. this isn't a too expensive operation.
89  **/
90  std::string shortcutString();
91 
92  /**
93  * Returns the shortcut string ( from the widget's shortcut property )
94  * without any "&" markers.
95  *
96  * But an escaped "&&" is preserved.
97  **/
98  std::string cleanShortcutString();
99 
100  /**
101  * Static version of the above for general use:
102  * Returns the specified string without any "&" markers.
103  * But an escaped "&&" is preserved.
104  **/
105  static std::string cleanShortcutString( std::string shortcutString );
106 
107  /**
108  * The preferred shortcut character, i.e. the character that had been
109  * preceded by "&" before checking / resolving conflicts began. 0 if none.
110  **/
111  char preferred();
112 
113  /**
114  * The actual shortcut character. 0 if none.
115  *
116  * This may be different from preferred() if it is overridden.
117  **/
118  char shortcut();
119 
120  /**
121  * Set (override) the shortcut character.
122  **/
123  virtual void setShortcut( char newShortcut );
124 
125  /**
126  * Clear the shortcut: Override the shortcut character with nothing.
127  * This may happen if a conflict cannot be resolved.
128  **/
129  void clearShortcut();
130 
131  /**
132  * Query the internal 'conflict' marker. This class doesn't care about that
133  * flag, it just stores it for the convenience of higher-level classes.
134  **/
135  bool conflict() { return _conflict; }
136 
137  /**
138  * Set or unset the internal 'conflict' marker.
139  **/
140  void setConflict( bool newConflictState = true ) { _conflict = newConflictState; }
141 
142  /**
143  * Obtain the number of distinct valid shortcut characters in the shortcut
144  * string, i.e. how many different shortcuts that widget could get.
145  **/
146  int distinctShortcutChars();
147 
148  /**
149  * Return true if this shortcut contains any character that would be valid
150  * as a shortcut character.
151  **/
152  bool hasValidShortcutChar();
153 
154  /**
155  * Static function: Returns the character used for marking keyboard
156  * shortcuts.
157  **/
158  static char shortcutMarker() { return '&'; }
159 
160  /**
161  * Static function: Find the next occurrence of the shortcut marker ('&')
162  * in a string, beginning at starting position start_pos.
163  *
164  * An escaped "&&" does not count.
165  *
166  * Returns string::npos if not found or the position of the shortcut marker
167  * (not the shortcut character!) if found.
168  **/
169  static std::string::size_type findShortcutPos( const std::string & str, std::string::size_type start_pos = 0 );
170 
171  /**
172  * Static function: Find the next shortcut marker in a string, beginning at
173  * starting position start_pos.
174  *
175  * Returns the shortcut character or 0 if none found.
176  **/
177  static char findShortcut( const std::string & str, std::string::size_type start_pos = 0 );
178 
179  /**
180  * Returns 'true' if 'c' is a valid shortcut character, i.e. [a-zA-Z0-9],
181  * 'false' otherwise.
182  **/
183  static bool isValid( char c );
184 
185  /**
186  * Return the normalized version of shortcut character 'c', i.e. a
187  * lowercase letter or a digit [a-z0-9]. Returns 0 if 'c' is invalid.
188  **/
189  static char normalized( char c );
190 
191  /**
192  * Obtain a widget's shortcut property - the string that contains "&" to
193  * designate a shortcut.
194  **/
195  static std::string getShortcutString( const YWidget * widget );
196 
197 
198 protected:
199 
200  /**
201  * Obtain the the shortcut property of this shortcut's widget - the string
202  * that contains "&" to designate a shortcut.
203  **/
204  virtual std::string getShortcutString();
205 
206 
207  // Data members
208 
209  YWidget * _widget; ///< (not owned)
210  std::string _shortcutString; ///< @see shortcutString
211  bool _shortcutStringCached; ///< is _shortcutString initialized
212 
213  std::string _cleanShortcutString;
214  bool _cleanShortcutStringCached; ///< always false :facepalm:
215 
216  /// char or 0 (none found) or -1 (not initialized yet)
217  /// @see preferred
219  /// char or 0 (none found) or -1 (not initialized yet)
220  /// @see shortcut
222 
223  bool _conflict; ///< @see conflict
224  bool _isButton; ///< @see isButton
225  bool _isWizardButton; ///< @see isWizardButton
226  /// -1 means uninitialized
227  /// @see distinctShortcutChars
229 };
230 
231 
232 
233 /**
234  * Special case for widgets that can have multiple shortcuts based on items
235  * (like YDumbTab)
236  **/
238 {
239 public:
240  /**
241  * Constructor.
242  **/
244  : YShortcut( widget )
245  , _item( item )
246  {}
247 
248  /**
249  * Destructor.
250  **/
251  virtual ~YItemShortcut() {}
252 
253  /**
254  * Return the associated item.
255  **/
256  YItem * item() const { return _item; }
257 
258  /**
259  * Set (override) the shortcut character.
260  * In this subclass, it will change the internally stored item.
261  **/
262  virtual void setShortcut( char newShortcut );
263 
264 protected:
265 
266  /**
267  * Obtain the the shortcut property of this shortcut's widget - the string
268  * that contains "&" to designate a shortcut.
269  **/
270  virtual std::string getShortcutString();
271 
272 
273 private:
274 
275  YItem * _item;
276 };
277 
278 
279 typedef std::vector<YShortcut *> YShortcutList;
280 typedef YShortcutList::iterator YShortcutListIterator;
281 
282 
283 #endif // YShortcut_h
YWidget
Abstract base class of all UI widgets.
Definition: YWidget.h:55
YShortcut::_isWizardButton
bool _isWizardButton
Definition: YShortcut.h:225
YShortcut::shortcutMarker
static char shortcutMarker()
Static function: Returns the character used for marking keyboard shortcuts.
Definition: YShortcut.h:158
YShortcut::isButton
bool isButton() const
Returns 'true' if the widget that is associated with this shortcut is a button (derived from YPushBut...
Definition: YShortcut.h:74
YShortcut::isWizardButton
bool isWizardButton() const
Returns 'true' if the widget that is associated with this shortcut is a wizard button (one of the nav...
Definition: YShortcut.h:80
YShortcut::YShortcut
YShortcut(YWidget *shortcut_widget)
Constructor.
Definition: YShortcut.cc:43
YShortcut::conflict
bool conflict()
Query the internal 'conflict' marker.
Definition: YShortcut.h:135
YItemShortcut::~YItemShortcut
virtual ~YItemShortcut()
Destructor.
Definition: YShortcut.h:251
YShortcut::~YShortcut
virtual ~YShortcut()
Destructor.
Definition: YShortcut.cc:71
YItemShortcut::setShortcut
virtual void setShortcut(char newShortcut)
Set (override) the shortcut character.
Definition: YShortcut.cc:322
YShortcut::widgetClass
const char * widgetClass() const
Returns the textual representation of the widget class of the widget this shortcut data belongs to.
Definition: YShortcut.h:68
YShortcut::_cleanShortcutStringCached
bool _cleanShortcutStringCached
always false :facepalm:
Definition: YShortcut.h:214
YShortcut::findShortcut
static char findShortcut(const std::string &str, std::string::size_type start_pos=0)
Static function: Find the next shortcut marker in a string, beginning at starting position start_pos.
Definition: YShortcut.cc:282
YShortcut::_preferred
int _preferred
char or 0 (none found) or -1 (not initialized yet)
Definition: YShortcut.h:218
YShortcut::_shortcutString
std::string _shortcutString
Definition: YShortcut.h:210
YItemShortcut
Special case for widgets that can have multiple shortcuts based on items (like YDumbTab)
Definition: YShortcut.h:238
YShortcut::widget
YWidget * widget() const
Returns the YWidget this shortcut data belong to.
Definition: YShortcut.h:62
YShortcut::_isButton
bool _isButton
Definition: YShortcut.h:224
YShortcut::setShortcut
virtual void setShortcut(char newShortcut)
Set (override) the shortcut character.
Definition: YShortcut.cc:143
YShortcut::normalized
static char normalized(char c)
Return the normalized version of shortcut character 'c', i.e.
Definition: YShortcut.cc:301
YShortcut::shortcut
char shortcut()
The actual shortcut character.
Definition: YShortcut.cc:131
YShortcut
Helper class for shortcut management: This class holds data about the shortcut for one single widget.
Definition: YShortcut.h:41
YShortcut::_distinctShortcutChars
int _distinctShortcutChars
-1 means uninitialized
Definition: YShortcut.h:228
YShortcut::_shortcutStringCached
bool _shortcutStringCached
is _shortcutString initialized
Definition: YShortcut.h:211
YWidget::widgetClass
virtual const char * widgetClass() const
Returns a descriptive name of this widget class for logging, debugging etc.
Definition: YWidget.h:72
YShortcut::clearShortcut
void clearShortcut()
Clear the shortcut: Override the shortcut character with nothing.
Definition: YShortcut.cc:175
YShortcut::isValid
static bool isValid(char c)
Returns 'true' if 'c' is a valid shortcut character, i.e.
Definition: YShortcut.cc:291
YShortcut::setConflict
void setConflict(bool newConflictState=true)
Set or unset the internal 'conflict' marker.
Definition: YShortcut.h:140
YShortcut::shortcutString
std::string shortcutString()
Returns the complete shortcut string (which may or may not contain "&"), i.e.
Definition: YShortcut.cc:77
YShortcut::findShortcutPos
static std::string::size_type findShortcutPos(const std::string &str, std::string::size_type start_pos=0)
Static function: Find the next occurrence of the shortcut marker ('&') in a string,...
Definition: YShortcut.cc:256
YShortcut::getShortcutString
virtual std::string getShortcutString()
Obtain the the shortcut property of this shortcut's widget - the string that contains "&" to designat...
Definition: YShortcut.cc:239
YShortcut::hasValidShortcutChar
bool hasValidShortcutChar()
Return true if this shortcut contains any character that would be valid as a shortcut character.
Definition: YShortcut.cc:224
YItemShortcut::getShortcutString
virtual std::string getShortcutString()
Obtain the the shortcut property of this shortcut's widget - the string that contains "&" to designat...
Definition: YShortcut.cc:312
YShortcut::_conflict
bool _conflict
Definition: YShortcut.h:223
YItemShortcut::YItemShortcut
YItemShortcut(YWidget *widget, YItem *item)
Constructor.
Definition: YShortcut.h:243
YShortcut::distinctShortcutChars
int distinctShortcutChars()
Obtain the number of distinct valid shortcut characters in the shortcut string, i....
Definition: YShortcut.cc:182
YShortcut::preferred
char preferred()
The preferred shortcut character, i.e.
Definition: YShortcut.cc:119
YShortcut::cleanShortcutString
std::string cleanShortcutString()
Returns the shortcut string ( from the widget's shortcut property ) without any "&" markers.
Definition: YShortcut.cc:93
YItemShortcut::item
YItem * item() const
Return the associated item.
Definition: YShortcut.h:256
YShortcut::_shortcut
int _shortcut
char or 0 (none found) or -1 (not initialized yet)
Definition: YShortcut.h:221
YItem
Simple item class for SelectionBox, ComboBox, MultiSelectionBox etc.
Definition: YItem.h:56
YShortcut::_widget
YWidget * _widget
(not owned)
Definition: YShortcut.h:209