libyui  3.3.2
YShortcutManager.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: YShortcutManager.h
20 
21  Author: Stefan Hundhammer <sh@suse.de>
22 
23 /-*/
24 
25 
26 #ifndef YShortcutManager_h
27 #define YShortcutManager_h
28 
29 #include "YWidget.h"
30 #include "YShortcut.h"
31 
32 class YDialog;
33 
34 /**
35  * Helper class to manage keyboard shortcuts within one dialog and resolve
36  * keyboard shortcut conflicts.
37  **/
39 {
40 public:
41  /**
42  * Constructor.
43  **/
45 
46  /**
47  * Destructor
48  **/
49  virtual ~YShortcutManager();
50 
51  /**
52  * Check the keyboard shortcuts of all children of this dialog
53  * (not for sub-dialogs!).
54  *
55  * Call resolveAllConflicts() if 'autoResolve' is 'true'.
56  **/
57  void checkShortcuts( bool autoResolve = true );
58 
59  /**
60  * Returns the number of shortcut conflicts.
61  * Valid only after checkShortcuts() or resolveAllConflicts().
62  **/
63  int conflictCount() { return _conflictCount; }
64 
65  /**
66  * Resolve shortcut conflicts. Requires checkShortcuts() to be called first.
67  *
68  * Note: This may or may not work. There is no general solution to that
69  * problem. This method tries its best, but you may end up with widgets
70  * that don't have any ( more ) shortcut.
71  *
72  * Why? Just picture the following ( admittedly pathologic ) situation:
73  *
74  * [& OK]
75  * [& OK]
76  * [& OK]
77  *
78  * This will result in something like this:
79  *
80  * [& OK]
81  * [O& K]
82  * [OK]
83  *
84  * I.e. the first OK button will retain its preferred shortcut ( 'O' ), the
85  * second OK button's shortcut will be reassigned to 'K' and the third
86  * won't get any - there are simply not enough eligible shortcut
87  * characters.
88  *
89  * This may even fail in much less pathological situations. This example is
90  * only supposed to give you a general idea why not to blindly rely on
91  * automatic shortcut resolving.
92  *
93  * It's always best to resolve conflicts manually. This will generally
94  * result in much better shortcuts: Easier to memorize, less chance of
95  * picking characters that cannot really do a good job showing their
96  * shortcut like very narrow characters ( .e.g., 'i' ) or descender
97  * characters ( e.g., 'g', 'p', 'q' - imagine those underlined! ).
98  **/
99  void resolveAllConflicts();
100 
101  /**
102  * Returns the dialog this shortcut manager works on.
103  **/
104  YDialog *dialog() { return _dialog; }
105 
106 protected:
107 
108  /**
109  * Delete all members of the internal shortcut list, then empty the list.
110  **/
111  void clearShortcutList();
112 
113  /**
114  * Recursively search all widgets between iterators 'begin' and 'end' (not
115  * those of any sub-dialogs!) for child widgets that could accept a
116  * keyboard shortcut and add these to _shortcutList.
117  **/
118  void findShortcutWidgets( YWidgetListConstIterator begin,
119  YWidgetListConstIterator end );
120 
121  /**
122  * Pick a new shortcut character for 'shortcut' - one that isn't marked as
123  * used in the '_used' array. Unset the conflict marker if that succeeded.
124  **/
125  void resolveConflict( YShortcut * shortcut );
126 
127  /**
128  * Find the shortest wizard button in 'conflictList', if there is any.
129  * Returns the index of that shortest wizard button or -1 if there is none.
130  **/
131  int findShortestWizardButton( const YShortcutList & conflictList );
132 
133  /**
134  * Find the shortest widget in 'conflictList'. Buttons get priority if they
135  * have the same number of eligible shortcut characters as another widget.
136  *
137  * Returns the index of the shortest widget.
138  **/
139  unsigned findShortestWidget( const YShortcutList & conflictList );
140 
141  /**
142  * The dialog this shortcut manager works on.
143  **/
145 
146  /**
147  * List of all the shortcuts in this dialog.
148  **/
149  YShortcutList _shortcutList;
150 
151  /**
152  * Counters for wanted shortcut characters.
153  **/
154  int _wanted[ sizeof( char ) << 8 ];
155 
156 
157  /**
158  * Flags for used shortcut characters.
159  **/
160  bool _used[ sizeof( char ) << 8 ];
161 
162 
163  /**
164  * Counter for shortcut conflicts
165  **/
167 
168 
169 private:
170 
171  bool _didCheck;
172 };
173 
174 
175 #endif // YShortcutManager_h
bool _used[sizeof(char)<< 8]
Flags for used shortcut characters.
int _conflictCount
Counter for shortcut conflicts.
virtual ~YShortcutManager()
Destructor.
void clearShortcutList()
Delete all members of the internal shortcut list, then empty the list.
Helper class to manage keyboard shortcuts within one dialog and resolve keyboard shortcut conflicts...
YDialog * dialog()
Returns the dialog this shortcut manager works on.
void resolveAllConflicts()
Resolve shortcut conflicts.
Helper class for shortcut management: This class holds data about the shortcut for one single widget...
Definition: YShortcut.h:40
YShortcutList _shortcutList
List of all the shortcuts in this dialog.
int findShortestWizardButton(const YShortcutList &conflictList)
Find the shortest wizard button in &#39;conflictList&#39;, if there is any.
int conflictCount()
Returns the number of shortcut conflicts.
void checkShortcuts(bool autoResolve=true)
Check the keyboard shortcuts of all children of this dialog (not for sub-dialogs!).
unsigned findShortestWidget(const YShortcutList &conflictList)
Find the shortest widget in &#39;conflictList&#39;.
int _wanted[sizeof(char)<< 8]
Counters for wanted shortcut characters.
YDialog * _dialog
The dialog this shortcut manager works on.
A window in the desktop environment.
Definition: YDialog.h:47
void findShortcutWidgets(YWidgetListConstIterator begin, YWidgetListConstIterator end)
Recursively search all widgets between iterators &#39;begin&#39; and &#39;end&#39; (not those of any sub-dialogs!) fo...
YShortcutManager(YDialog *dialog)
Constructor.
void resolveConflict(YShortcut *shortcut)
Pick a new shortcut character for &#39;shortcut&#39; - one that isn&#39;t marked as used in the &#39;_used&#39; array...