]>
Commit | Line | Data |
---|---|---|
c36d4774 VZ |
1 | /////////////////////////////////////////////////////////////////////////////// |
2 | // Name: src/common/accelcmn.cpp | |
3 | // Purpose: implementation of platform-independent wxAcceleratorEntry parts | |
4 | // Author: Vadim Zeitlin | |
5 | // Created: 2007-05-05 | |
6 | // RCS-ID: $Id$ | |
7 | // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org> | |
8 | // Licence: wxWindows licence | |
9 | /////////////////////////////////////////////////////////////////////////////// | |
10 | ||
11 | // ============================================================================ | |
12 | // declarations | |
13 | // ============================================================================ | |
14 | ||
15 | // ---------------------------------------------------------------------------- | |
16 | // headers | |
17 | // ---------------------------------------------------------------------------- | |
18 | ||
19 | // for compilers that support precompilation, includes "wx.h". | |
20 | #include "wx/wxprec.h" | |
21 | ||
22 | #ifdef __BORLANDC__ | |
23 | #pragma hdrstop | |
24 | #endif | |
25 | ||
26 | #if wxUSE_ACCEL | |
27 | ||
28 | #ifndef WX_PRECOMP | |
29 | #include "wx/string.h" | |
f74a73b3 VZ |
30 | #include "wx/intl.h" |
31 | #include "wx/log.h" | |
c36d4774 | 32 | #include "wx/accel.h" |
0bf751e7 | 33 | #include "wx/crt.h" |
c36d4774 VZ |
34 | #endif //WX_PRECOMP |
35 | ||
36 | // ============================================================================ | |
37 | // wxAcceleratorEntry implementation | |
38 | // ============================================================================ | |
39 | ||
40 | static const struct wxKeyName | |
41 | { | |
42 | wxKeyCode code; | |
e6d4038a | 43 | const char *name; |
c36d4774 VZ |
44 | } wxKeyNames[] = |
45 | { | |
46 | { WXK_DELETE, wxTRANSLATE("DEL") }, | |
47 | { WXK_DELETE, wxTRANSLATE("DELETE") }, | |
48 | { WXK_BACK, wxTRANSLATE("BACK") }, | |
49 | { WXK_INSERT, wxTRANSLATE("INS") }, | |
50 | { WXK_INSERT, wxTRANSLATE("INSERT") }, | |
51 | { WXK_RETURN, wxTRANSLATE("ENTER") }, | |
52 | { WXK_RETURN, wxTRANSLATE("RETURN") }, | |
53 | { WXK_PAGEUP, wxTRANSLATE("PGUP") }, | |
54 | { WXK_PAGEDOWN, wxTRANSLATE("PGDN") }, | |
55 | { WXK_LEFT, wxTRANSLATE("LEFT") }, | |
56 | { WXK_RIGHT, wxTRANSLATE("RIGHT") }, | |
57 | { WXK_UP, wxTRANSLATE("UP") }, | |
58 | { WXK_DOWN, wxTRANSLATE("DOWN") }, | |
59 | { WXK_HOME, wxTRANSLATE("HOME") }, | |
60 | { WXK_END, wxTRANSLATE("END") }, | |
61 | { WXK_SPACE, wxTRANSLATE("SPACE") }, | |
62 | { WXK_TAB, wxTRANSLATE("TAB") }, | |
63 | { WXK_ESCAPE, wxTRANSLATE("ESC") }, | |
64 | { WXK_ESCAPE, wxTRANSLATE("ESCAPE") }, | |
65 | { WXK_CANCEL, wxTRANSLATE("CANCEL") }, | |
66 | { WXK_CLEAR, wxTRANSLATE("CLEAR") }, | |
67 | { WXK_MENU, wxTRANSLATE("MENU") }, | |
68 | { WXK_PAUSE, wxTRANSLATE("PAUSE") }, | |
69 | { WXK_CAPITAL, wxTRANSLATE("CAPITAL") }, | |
70 | { WXK_SELECT, wxTRANSLATE("SELECT") }, | |
71 | { WXK_PRINT, wxTRANSLATE("PRINT") }, | |
72 | { WXK_EXECUTE, wxTRANSLATE("EXECUTE") }, | |
73 | { WXK_SNAPSHOT, wxTRANSLATE("SNAPSHOT") }, | |
74 | { WXK_HELP, wxTRANSLATE("HELP") }, | |
75 | { WXK_ADD, wxTRANSLATE("ADD") }, | |
76 | { WXK_SEPARATOR, wxTRANSLATE("SEPARATOR") }, | |
77 | { WXK_SUBTRACT, wxTRANSLATE("SUBTRACT") }, | |
78 | { WXK_DECIMAL, wxTRANSLATE("DECIMAL") }, | |
79 | { WXK_DIVIDE, wxTRANSLATE("DIVIDE") }, | |
80 | { WXK_NUMLOCK, wxTRANSLATE("NUM_LOCK") }, | |
81 | { WXK_SCROLL, wxTRANSLATE("SCROLL_LOCK") }, | |
82 | { WXK_PAGEUP, wxTRANSLATE("PAGEUP") }, | |
83 | { WXK_PAGEDOWN, wxTRANSLATE("PAGEDOWN") }, | |
84 | { WXK_NUMPAD_SPACE, wxTRANSLATE("KP_SPACE") }, | |
85 | { WXK_NUMPAD_TAB, wxTRANSLATE("KP_TAB") }, | |
86 | { WXK_NUMPAD_ENTER, wxTRANSLATE("KP_ENTER") }, | |
87 | { WXK_NUMPAD_HOME, wxTRANSLATE("KP_HOME") }, | |
88 | { WXK_NUMPAD_LEFT, wxTRANSLATE("KP_LEFT") }, | |
89 | { WXK_NUMPAD_UP, wxTRANSLATE("KP_UP") }, | |
90 | { WXK_NUMPAD_RIGHT, wxTRANSLATE("KP_RIGHT") }, | |
91 | { WXK_NUMPAD_DOWN, wxTRANSLATE("KP_DOWN") }, | |
92 | { WXK_NUMPAD_PAGEUP, wxTRANSLATE("KP_PRIOR") }, | |
93 | { WXK_NUMPAD_PAGEUP, wxTRANSLATE("KP_PAGEUP") }, | |
94 | { WXK_NUMPAD_PAGEDOWN, wxTRANSLATE("KP_NEXT") }, | |
95 | { WXK_NUMPAD_PAGEDOWN, wxTRANSLATE("KP_PAGEDOWN") }, | |
96 | { WXK_NUMPAD_END, wxTRANSLATE("KP_END") }, | |
97 | { WXK_NUMPAD_BEGIN, wxTRANSLATE("KP_BEGIN") }, | |
98 | { WXK_NUMPAD_INSERT, wxTRANSLATE("KP_INSERT") }, | |
99 | { WXK_NUMPAD_DELETE, wxTRANSLATE("KP_DELETE") }, | |
100 | { WXK_NUMPAD_EQUAL, wxTRANSLATE("KP_EQUAL") }, | |
101 | { WXK_NUMPAD_MULTIPLY, wxTRANSLATE("KP_MULTIPLY") }, | |
102 | { WXK_NUMPAD_ADD, wxTRANSLATE("KP_ADD") }, | |
103 | { WXK_NUMPAD_SEPARATOR, wxTRANSLATE("KP_SEPARATOR") }, | |
104 | { WXK_NUMPAD_SUBTRACT, wxTRANSLATE("KP_SUBTRACT") }, | |
105 | { WXK_NUMPAD_DECIMAL, wxTRANSLATE("KP_DECIMAL") }, | |
106 | { WXK_NUMPAD_DIVIDE, wxTRANSLATE("KP_DIVIDE") }, | |
107 | { WXK_WINDOWS_LEFT, wxTRANSLATE("WINDOWS_LEFT") }, | |
108 | { WXK_WINDOWS_RIGHT, wxTRANSLATE("WINDOWS_RIGHT") }, | |
109 | { WXK_WINDOWS_MENU, wxTRANSLATE("WINDOWS_MENU") }, | |
110 | { WXK_COMMAND, wxTRANSLATE("COMMAND") }, | |
111 | }; | |
112 | ||
113 | // return true if the 2 strings refer to the same accel | |
114 | // | |
115 | // as accels can be either translated or not, check for both possibilities and | |
116 | // also compare case-insensitively as the key names case doesn't count | |
e6d4038a | 117 | static inline bool CompareAccelString(const wxString& str, const char *accel) |
c36d4774 VZ |
118 | { |
119 | return str.CmpNoCase(accel) == 0 | |
120 | #if wxUSE_INTL | |
121 | || str.CmpNoCase(wxGetTranslation(accel)) == 0 | |
122 | #endif | |
123 | ; | |
124 | } | |
125 | ||
126 | // return prefixCode+number if the string is of the form "<prefix><number>" and | |
127 | // 0 if it isn't | |
128 | // | |
129 | // first and last parameter specify the valid domain for "number" part | |
130 | static int IsNumberedAccelKey(const wxString& str, | |
e6d4038a | 131 | const char *prefix, |
c36d4774 VZ |
132 | wxKeyCode prefixCode, |
133 | unsigned first, | |
134 | unsigned last) | |
135 | { | |
136 | const size_t lenPrefix = wxStrlen(prefix); | |
137 | if ( !CompareAccelString(str.Left(lenPrefix), prefix) ) | |
138 | return 0; | |
139 | ||
140 | unsigned long num; | |
141 | if ( !str.Mid(lenPrefix).ToULong(&num) ) | |
142 | return 0; | |
143 | ||
144 | if ( num < first || num > last ) | |
145 | { | |
146 | // this must be a mistake, chances that this is a valid name of another | |
147 | // key are vanishingly small | |
9a83f860 | 148 | wxLogDebug(wxT("Invalid key string \"%s\""), str.c_str()); |
c36d4774 VZ |
149 | return 0; |
150 | } | |
151 | ||
152 | return prefixCode + num - first; | |
153 | } | |
154 | ||
155 | /* static */ | |
156 | bool | |
157 | wxAcceleratorEntry::ParseAccel(const wxString& text, int *flagsOut, int *keyOut) | |
158 | { | |
159 | // the parser won't like trailing spaces | |
160 | wxString label = text; | |
161 | label.Trim(true); // the initial \t must be preserved so don't strip leading whitespaces | |
162 | ||
944f641c VZ |
163 | // If we're passed the entire menu item label instead of just the |
164 | // accelerator, skip the label part and only look after the TAB. | |
c36d4774 VZ |
165 | // check for accelerators: they are given after '\t' |
166 | int posTab = label.Find(wxT('\t')); | |
167 | if ( posTab == wxNOT_FOUND ) | |
944f641c VZ |
168 | posTab = 0; |
169 | else | |
170 | posTab++; | |
c36d4774 VZ |
171 | |
172 | // parse the accelerator string | |
173 | int accelFlags = wxACCEL_NORMAL; | |
174 | wxString current; | |
944f641c | 175 | for ( size_t n = (size_t)posTab; n < label.length(); n++ ) |
c36d4774 VZ |
176 | { |
177 | if ( (label[n] == '+') || (label[n] == '-') ) | |
178 | { | |
179 | if ( CompareAccelString(current, wxTRANSLATE("ctrl")) ) | |
180 | accelFlags |= wxACCEL_CTRL; | |
181 | else if ( CompareAccelString(current, wxTRANSLATE("alt")) ) | |
182 | accelFlags |= wxACCEL_ALT; | |
183 | else if ( CompareAccelString(current, wxTRANSLATE("shift")) ) | |
184 | accelFlags |= wxACCEL_SHIFT; | |
185 | else // not a recognized modifier name | |
186 | { | |
187 | // we may have "Ctrl-+", for example, but we still want to | |
188 | // catch typos like "Crtl-A" so only give the warning if we | |
189 | // have something before the current '+' or '-', else take | |
190 | // it as a literal symbol | |
191 | if ( current.empty() ) | |
192 | { | |
193 | current += label[n]; | |
194 | ||
195 | // skip clearing it below | |
196 | continue; | |
197 | } | |
198 | else | |
199 | { | |
200 | wxLogDebug(wxT("Unknown accel modifier: '%s'"), | |
201 | current.c_str()); | |
202 | } | |
203 | } | |
204 | ||
205 | current.clear(); | |
206 | } | |
207 | else // not special character | |
208 | { | |
209 | current += (wxChar) wxTolower(label[n]); | |
210 | } | |
211 | } | |
212 | ||
213 | int keyCode; | |
214 | const size_t len = current.length(); | |
215 | switch ( len ) | |
216 | { | |
217 | case 0: | |
218 | wxLogDebug(wxT("No accel key found, accel string ignored.")); | |
219 | return false; | |
220 | ||
221 | case 1: | |
222 | // it's just a letter | |
223 | keyCode = current[0U]; | |
224 | ||
225 | // if the key is used with any modifiers, make it an uppercase one | |
226 | // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's | |
227 | // used alone as 'a' and 'A' are different | |
228 | if ( accelFlags != wxACCEL_NORMAL ) | |
229 | keyCode = wxToupper(keyCode); | |
230 | break; | |
231 | ||
232 | default: | |
233 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("F"), | |
234 | WXK_F1, 1, 12); | |
235 | if ( !keyCode ) | |
236 | { | |
237 | for ( size_t n = 0; n < WXSIZEOF(wxKeyNames); n++ ) | |
238 | { | |
239 | const wxKeyName& kn = wxKeyNames[n]; | |
240 | if ( CompareAccelString(current, kn.name) ) | |
241 | { | |
242 | keyCode = kn.code; | |
243 | break; | |
244 | } | |
245 | } | |
246 | } | |
247 | ||
248 | if ( !keyCode ) | |
249 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("KP_"), | |
250 | WXK_NUMPAD0, 0, 9); | |
251 | if ( !keyCode ) | |
252 | keyCode = IsNumberedAccelKey(current, wxTRANSLATE("SPECIAL"), | |
253 | WXK_SPECIAL1, 1, 20); | |
254 | ||
255 | if ( !keyCode ) | |
256 | { | |
257 | wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."), | |
258 | current.c_str()); | |
259 | return false; | |
260 | } | |
261 | } | |
262 | ||
263 | ||
9a83f860 | 264 | wxASSERT_MSG( keyCode, wxT("logic error: should have key code here") ); |
c36d4774 VZ |
265 | |
266 | if ( flagsOut ) | |
267 | *flagsOut = accelFlags; | |
268 | if ( keyOut ) | |
269 | *keyOut = keyCode; | |
270 | ||
271 | return true; | |
272 | } | |
273 | ||
274 | /* static */ | |
275 | wxAcceleratorEntry *wxAcceleratorEntry::Create(const wxString& str) | |
276 | { | |
277 | int flags, | |
278 | keyCode; | |
279 | if ( !ParseAccel(str, &flags, &keyCode) ) | |
280 | return NULL; | |
281 | ||
282 | return new wxAcceleratorEntry(flags, keyCode); | |
283 | } | |
284 | ||
285 | bool wxAcceleratorEntry::FromString(const wxString& str) | |
286 | { | |
287 | return ParseAccel(str, &m_flags, &m_keyCode); | |
288 | } | |
289 | ||
290 | wxString wxAcceleratorEntry::ToString() const | |
291 | { | |
292 | wxString text; | |
293 | ||
294 | int flags = GetFlags(); | |
295 | if ( flags & wxACCEL_ALT ) | |
8c3017fe | 296 | text += _("Alt+"); |
c36d4774 | 297 | if ( flags & wxACCEL_CTRL ) |
8c3017fe | 298 | text += _("Ctrl+"); |
c36d4774 | 299 | if ( flags & wxACCEL_SHIFT ) |
8c3017fe | 300 | text += _("Shift+"); |
c36d4774 VZ |
301 | |
302 | const int code = GetKeyCode(); | |
303 | ||
304 | if ( code >= WXK_F1 && code <= WXK_F12 ) | |
305 | text << _("F") << code - WXK_F1 + 1; | |
306 | else if ( code >= WXK_NUMPAD0 && code <= WXK_NUMPAD9 ) | |
307 | text << _("KP_") << code - WXK_NUMPAD0; | |
308 | else if ( code >= WXK_SPECIAL1 && code <= WXK_SPECIAL20 ) | |
309 | text << _("SPECIAL") << code - WXK_SPECIAL1 + 1; | |
310 | else // check the named keys | |
311 | { | |
312 | size_t n; | |
313 | for ( n = 0; n < WXSIZEOF(wxKeyNames); n++ ) | |
314 | { | |
315 | const wxKeyName& kn = wxKeyNames[n]; | |
316 | if ( code == kn.code ) | |
317 | { | |
318 | text << wxGetTranslation(kn.name); | |
319 | break; | |
320 | } | |
321 | } | |
322 | ||
323 | if ( n == WXSIZEOF(wxKeyNames) ) | |
324 | { | |
325 | // must be a simple key | |
326 | if ( | |
327 | #if !wxUSE_UNICODE | |
7a0079d5 VZ |
328 | // we can't call wxIsalnum() for non-ASCII characters in ASCII |
329 | // build as they're only defined for the ASCII range (or EOF) | |
330 | wxIsascii(code) && | |
c36d4774 VZ |
331 | #endif // ANSI |
332 | wxIsalnum(code) ) | |
333 | { | |
334 | text << (wxChar)code; | |
335 | } | |
336 | else | |
337 | { | |
338 | wxFAIL_MSG( wxT("unknown keyboard accelerator code") ); | |
339 | } | |
340 | } | |
341 | } | |
342 | ||
343 | return text; | |
344 | } | |
345 | ||
346 | wxAcceleratorEntry *wxGetAccelFromString(const wxString& label) | |
347 | { | |
348 | return wxAcceleratorEntry::Create(label); | |
349 | } | |
350 | ||
351 | #endif // wxUSE_ACCEL | |
352 | ||
353 | ||
354 |