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