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 if ( CompareAccelString(current
, wxTRANSLATE("rawctrl")) )
190 accelFlags
|= wxACCEL_RAW_CTRL
;
191 else // not a recognized modifier name
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() )
201 // skip clearing it below
206 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
213 else // not special character
215 current
+= (wxChar
) wxTolower(label
[n
]);
220 const size_t len
= current
.length();
224 wxLogDebug(wxT("No accel key found, accel string ignored."));
228 // it's just a letter
229 keyCode
= current
[0U];
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
);
239 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
243 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
245 const wxKeyName
& kn
= wxKeyNames
[n
];
246 if ( CompareAccelString(current
, kn
.name
) )
255 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
258 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
259 WXK_SPECIAL1
, 1, 20);
263 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
270 wxASSERT_MSG( keyCode
, wxT("logic error: should have key code here") );
273 *flagsOut
= accelFlags
;
281 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
283 const wxString accelStr
= str
.AfterFirst('\t');
284 if ( accelStr
.empty() )
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.
294 if ( !ParseAccel(accelStr
, &flags
, &keyCode
) )
297 return new wxAcceleratorEntry(flags
, keyCode
);
300 bool wxAcceleratorEntry::FromString(const wxString
& str
)
302 return ParseAccel(str
, &m_flags
, &m_keyCode
);
308 wxString
PossiblyLocalize(const wxString
& str
, bool localize
)
310 return localize
? wxGetTranslation(str
) : str
;
315 wxString
wxAcceleratorEntry::AsPossiblyLocalizedString(bool localized
) const
319 int flags
= GetFlags();
320 if ( flags
& wxACCEL_ALT
)
321 text
+= PossiblyLocalize(wxTRANSLATE("Alt+"), localized
);
322 if ( flags
& wxACCEL_CTRL
)
323 text
+= PossiblyLocalize(wxTRANSLATE("Ctrl+"), localized
);
324 if ( flags
& wxACCEL_SHIFT
)
325 text
+= PossiblyLocalize(wxTRANSLATE("Shift+"), localized
);
326 #if defined(__WXMAC__) || defined(__WXCOCOA__)
327 if ( flags
& wxACCEL_RAW_CTRL
)
328 text
+= PossiblyLocalize(wxTRANSLATE("RawCtrl+"), localized
);
331 const int code
= GetKeyCode();
333 if ( code
>= WXK_F1
&& code
<= WXK_F12
)
334 text
<< PossiblyLocalize(wxTRANSLATE("F"), localized
)
335 << code
- WXK_F1
+ 1;
336 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
337 text
<< PossiblyLocalize(wxTRANSLATE("KP_"), localized
)
338 << code
- WXK_NUMPAD0
;
339 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
340 text
<< PossiblyLocalize(wxTRANSLATE("SPECIAL"), localized
)
341 << code
- WXK_SPECIAL1
+ 1;
342 else // check the named keys
345 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
347 const wxKeyName
& kn
= wxKeyNames
[n
];
348 if ( code
== kn
.code
)
350 text
<< PossiblyLocalize(kn
.name
, localized
);
355 if ( n
== WXSIZEOF(wxKeyNames
) )
357 // must be a simple key
360 // we can't call wxIsalnum() for non-ASCII characters in ASCII
361 // build as they're only defined for the ASCII range (or EOF)
366 text
<< (wxChar
)code
;
370 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
378 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
380 return wxAcceleratorEntry::Create(label
);
383 #endif // wxUSE_ACCEL