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