1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: src/common/accelcmn.cpp
3 // Purpose: implementation of platform-independent wxAcceleratorEntry parts
4 // Author: Vadim Zeitlin
7 // Copyright: (c) 2007 Vadim Zeitlin <vadim@wxwindows.org>
8 // Licence: wxWindows licence
9 ///////////////////////////////////////////////////////////////////////////////
11 // ============================================================================
13 // ============================================================================
15 // ----------------------------------------------------------------------------
17 // ----------------------------------------------------------------------------
19 // for compilers that support precompilation, includes "wx.h".
20 #include "wx/wxprec.h"
30 #include "wx/string.h"
36 wxAcceleratorTable wxNullAcceleratorTable
;
38 // ============================================================================
39 // wxAcceleratorEntry implementation
40 // ============================================================================
42 static const struct wxKeyName
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") },
115 // return true if the 2 strings refer to the same accel
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
)
121 return str
.CmpNoCase(accel
) == 0
123 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
128 // return prefixCode+number if the string is of the form "<prefix><number>" and
131 // first and last parameter specify the valid domain for "number" part
132 static int IsNumberedAccelKey(const wxString
& str
,
134 wxKeyCode prefixCode
,
138 const size_t lenPrefix
= wxStrlen(prefix
);
139 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
143 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
146 if ( num
< first
|| num
> last
)
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());
154 return prefixCode
+ num
- first
;
159 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
161 // the parser won't like trailing spaces
162 wxString label
= text
;
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
)
176 // parse the accelerator string
177 int accelFlags
= wxACCEL_NORMAL
;
179 for ( size_t n
= (size_t)posTab
; n
< label
.length(); n
++ )
181 if ( (label
[n
] == '+') || (label
[n
] == '-') )
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
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() )
199 // skip clearing it below
204 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
211 else // not special character
213 current
+= (wxChar
) wxTolower(label
[n
]);
218 const size_t len
= current
.length();
222 wxLogDebug(wxT("No accel key found, accel string ignored."));
226 // it's just a letter
227 keyCode
= current
[0U];
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
);
237 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
241 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
243 const wxKeyName
& kn
= wxKeyNames
[n
];
244 if ( CompareAccelString(current
, kn
.name
) )
253 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
256 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
257 WXK_SPECIAL1
, 1, 20);
261 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
268 wxASSERT_MSG( keyCode
, wxT("logic error: should have key code here") );
271 *flagsOut
= accelFlags
;
279 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
281 const wxString accelStr
= str
.AfterFirst('\t');
282 if ( accelStr
.empty() )
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.
292 if ( !ParseAccel(accelStr
, &flags
, &keyCode
) )
295 return new wxAcceleratorEntry(flags
, keyCode
);
298 bool wxAcceleratorEntry::FromString(const wxString
& str
)
300 return ParseAccel(str
, &m_flags
, &m_keyCode
);
303 wxString
wxAcceleratorEntry::ToString() const
307 int flags
= GetFlags();
308 if ( flags
& wxACCEL_ALT
)
310 if ( flags
& (wxACCEL_CTRL
| wxACCEL_CMD
) )
312 if ( flags
& wxACCEL_SHIFT
)
315 const int code
= GetKeyCode();
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
326 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
328 const wxKeyName
& kn
= wxKeyNames
[n
];
329 if ( code
== kn
.code
)
331 text
<< wxGetTranslation(kn
.name
);
336 if ( n
== WXSIZEOF(wxKeyNames
) )
338 // must be a simple key
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)
347 text
<< (wxChar
)code
;
351 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
359 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
361 return wxAcceleratorEntry::Create(label
);
364 #endif // wxUSE_ACCEL