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