]> git.saurik.com Git - wxWidgets.git/blob - src/common/accelcmn.cpp
Rework the GTK WebKit backend history to remove the need for the map between wxWebHis...
[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 // not a recognized modifier name
190 {
191 // we may have "Ctrl-+", for example, but we still want to
192 // catch typos like "Crtl-A" so only give the warning if we
193 // have something before the current '+' or '-', else take
194 // it as a literal symbol
195 if ( current.empty() )
196 {
197 current += label[n];
198
199 // skip clearing it below
200 continue;
201 }
202 else
203 {
204 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
205 current.c_str());
206 }
207 }
208
209 current.clear();
210 }
211 else // not special character
212 {
213 current += (wxChar) wxTolower(label[n]);
214 }
215 }
216
217 int keyCode;
218 const size_t len = current.length();
219 switch ( len )
220 {
221 case 0:
222 wxLogDebug(wxT("No accel key found, accel string ignored."));
223 return false;
224
225 case 1:
226 // it's just a letter
227 keyCode = current[0U];
228
229 // if the key is used with any modifiers, make it an uppercase one
230 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
231 // used alone as 'a' and 'A' are different
232 if ( accelFlags != wxACCEL_NORMAL )
233 keyCode = wxToupper(keyCode);
234 break;
235
236 default:
237 keyCode = IsNumberedAccelKey(current, wxTRANSLATE("F"),
238 WXK_F1, 1, 12);
239 if ( !keyCode )
240 {
241 for ( size_t n = 0; n < WXSIZEOF(wxKeyNames); n++ )
242 {
243 const wxKeyName& kn = wxKeyNames[n];
244 if ( CompareAccelString(current, kn.name) )
245 {
246 keyCode = kn.code;
247 break;
248 }
249 }
250 }
251
252 if ( !keyCode )
253 keyCode = IsNumberedAccelKey(current, wxTRANSLATE("KP_"),
254 WXK_NUMPAD0, 0, 9);
255 if ( !keyCode )
256 keyCode = IsNumberedAccelKey(current, wxTRANSLATE("SPECIAL"),
257 WXK_SPECIAL1, 1, 20);
258
259 if ( !keyCode )
260 {
261 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
262 current.c_str());
263 return false;
264 }
265 }
266
267
268 wxASSERT_MSG( keyCode, wxT("logic error: should have key code here") );
269
270 if ( flagsOut )
271 *flagsOut = accelFlags;
272 if ( keyOut )
273 *keyOut = keyCode;
274
275 return true;
276 }
277
278 /* static */
279 wxAcceleratorEntry *wxAcceleratorEntry::Create(const wxString& str)
280 {
281 const wxString accelStr = str.AfterFirst('\t');
282 if ( accelStr.empty() )
283 {
284 // It's ok to pass strings not containing any accelerators at all to
285 // this function, wxMenuItem code does it and we should just return
286 // NULL in this case.
287 return NULL;
288 }
289
290 int flags,
291 keyCode;
292 if ( !ParseAccel(accelStr, &flags, &keyCode) )
293 return NULL;
294
295 return new wxAcceleratorEntry(flags, keyCode);
296 }
297
298 bool wxAcceleratorEntry::FromString(const wxString& str)
299 {
300 return ParseAccel(str, &m_flags, &m_keyCode);
301 }
302
303 wxString wxAcceleratorEntry::ToString() const
304 {
305 wxString text;
306
307 int flags = GetFlags();
308 if ( flags & wxACCEL_ALT )
309 text += _("Alt+");
310 if ( flags & wxACCEL_CTRL )
311 text += _("Ctrl+");
312 if ( flags & wxACCEL_SHIFT )
313 text += _("Shift+");
314
315 const int code = GetKeyCode();
316
317 if ( code >= WXK_F1 && code <= WXK_F12 )
318 text << _("F") << code - WXK_F1 + 1;
319 else if ( code >= WXK_NUMPAD0 && code <= WXK_NUMPAD9 )
320 text << _("KP_") << code - WXK_NUMPAD0;
321 else if ( code >= WXK_SPECIAL1 && code <= WXK_SPECIAL20 )
322 text << _("SPECIAL") << code - WXK_SPECIAL1 + 1;
323 else // check the named keys
324 {
325 size_t n;
326 for ( n = 0; n < WXSIZEOF(wxKeyNames); n++ )
327 {
328 const wxKeyName& kn = wxKeyNames[n];
329 if ( code == kn.code )
330 {
331 text << wxGetTranslation(kn.name);
332 break;
333 }
334 }
335
336 if ( n == WXSIZEOF(wxKeyNames) )
337 {
338 // must be a simple key
339 if (
340 #if !wxUSE_UNICODE
341 // we can't call wxIsalnum() for non-ASCII characters in ASCII
342 // build as they're only defined for the ASCII range (or EOF)
343 wxIsascii(code) &&
344 #endif // ANSI
345 wxIsalnum(code) )
346 {
347 text << (wxChar)code;
348 }
349 else
350 {
351 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
352 }
353 }
354 }
355
356 return text;
357 }
358
359 wxAcceleratorEntry *wxGetAccelFromString(const wxString& label)
360 {
361 return wxAcceleratorEntry::Create(label);
362 }
363
364 #endif // wxUSE_ACCEL
365
366
367