]>
git.saurik.com Git - wxWidgets.git/blob - src/common/accelcmn.cpp
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"
29 #include "wx/string.h"
36 // ============================================================================
37 // wxAcceleratorEntry implementation
38 // ============================================================================
40 static const struct wxKeyName
46 { WXK_DELETE
, wxTRANSLATE("DEL") },
47 { WXK_DELETE
, wxTRANSLATE("DELETE") },
48 { WXK_BACK
, wxTRANSLATE("BACK") },
49 { WXK_INSERT
, wxTRANSLATE("INS") },
50 { WXK_INSERT
, wxTRANSLATE("INSERT") },
51 { WXK_RETURN
, wxTRANSLATE("ENTER") },
52 { WXK_RETURN
, wxTRANSLATE("RETURN") },
53 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
54 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
55 { WXK_LEFT
, wxTRANSLATE("LEFT") },
56 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
57 { WXK_UP
, wxTRANSLATE("UP") },
58 { WXK_DOWN
, wxTRANSLATE("DOWN") },
59 { WXK_HOME
, wxTRANSLATE("HOME") },
60 { WXK_END
, wxTRANSLATE("END") },
61 { WXK_SPACE
, wxTRANSLATE("SPACE") },
62 { WXK_TAB
, wxTRANSLATE("TAB") },
63 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
64 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
65 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
66 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
67 { WXK_MENU
, wxTRANSLATE("MENU") },
68 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
69 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
70 { WXK_SELECT
, wxTRANSLATE("SELECT") },
71 { WXK_PRINT
, wxTRANSLATE("PRINT") },
72 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
73 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
74 { WXK_HELP
, wxTRANSLATE("HELP") },
75 { WXK_ADD
, wxTRANSLATE("ADD") },
76 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
77 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
78 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
79 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
80 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
81 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
82 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
83 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
84 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
85 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
86 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
87 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
88 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
89 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
90 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
91 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
92 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
93 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
94 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
95 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
96 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
97 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
98 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
99 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
100 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
101 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
102 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
103 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
104 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
105 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
106 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
107 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
108 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
109 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
110 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
113 // return true if the 2 strings refer to the same accel
115 // as accels can be either translated or not, check for both possibilities and
116 // also compare case-insensitively as the key names case doesn't count
117 static inline bool CompareAccelString(const wxString
& str
, const char *accel
)
119 return str
.CmpNoCase(accel
) == 0
121 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
126 // return prefixCode+number if the string is of the form "<prefix><number>" and
129 // first and last parameter specify the valid domain for "number" part
130 static int IsNumberedAccelKey(const wxString
& str
,
132 wxKeyCode prefixCode
,
136 const size_t lenPrefix
= wxStrlen(prefix
);
137 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
141 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
144 if ( num
< first
|| num
> last
)
146 // this must be a mistake, chances that this is a valid name of another
147 // key are vanishingly small
148 wxLogDebug(wxT("Invalid key string \"%s\""), str
.c_str());
152 return prefixCode
+ num
- first
;
157 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
159 // the parser won't like trailing spaces
160 wxString label
= text
;
163 // For compatibility with the old wx versions which accepted (and actually
164 // even required) a TAB character in the string passed to this function we
165 // ignore anything up to the first TAB. Notice however that the correct
166 // input consists of just the accelerator itself and nothing else, this is
167 // done for compatibility and compatibility only.
168 int posTab
= label
.Find(wxT('\t'));
169 if ( posTab
== wxNOT_FOUND
)
174 // parse the accelerator string
175 int accelFlags
= wxACCEL_NORMAL
;
177 for ( size_t n
= (size_t)posTab
; n
< label
.length(); n
++ )
179 if ( (label
[n
] == '+') || (label
[n
] == '-') )
181 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
182 accelFlags
|= wxACCEL_CTRL
;
183 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
184 accelFlags
|= wxACCEL_ALT
;
185 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
186 accelFlags
|= wxACCEL_SHIFT
;
187 else // not a recognized modifier name
189 // we may have "Ctrl-+", for example, but we still want to
190 // catch typos like "Crtl-A" so only give the warning if we
191 // have something before the current '+' or '-', else take
192 // it as a literal symbol
193 if ( current
.empty() )
197 // skip clearing it below
202 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
209 else // not special character
211 current
+= (wxChar
) wxTolower(label
[n
]);
216 const size_t len
= current
.length();
220 wxLogDebug(wxT("No accel key found, accel string ignored."));
224 // it's just a letter
225 keyCode
= current
[0U];
227 // if the key is used with any modifiers, make it an uppercase one
228 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
229 // used alone as 'a' and 'A' are different
230 if ( accelFlags
!= wxACCEL_NORMAL
)
231 keyCode
= wxToupper(keyCode
);
235 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
239 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
241 const wxKeyName
& kn
= wxKeyNames
[n
];
242 if ( CompareAccelString(current
, kn
.name
) )
251 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
254 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
255 WXK_SPECIAL1
, 1, 20);
259 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
266 wxASSERT_MSG( keyCode
, wxT("logic error: should have key code here") );
269 *flagsOut
= accelFlags
;
277 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
279 const wxString accelStr
= str
.AfterFirst('\t');
280 if ( accelStr
.empty() )
282 // It's ok to pass strings not containing any accelerators at all to
283 // this function, wxMenuItem code does it and we should just return
284 // NULL in this case.
290 if ( !ParseAccel(accelStr
, &flags
, &keyCode
) )
293 return new wxAcceleratorEntry(flags
, keyCode
);
296 bool wxAcceleratorEntry::FromString(const wxString
& str
)
298 return ParseAccel(str
, &m_flags
, &m_keyCode
);
301 wxString
wxAcceleratorEntry::ToString() const
305 int flags
= GetFlags();
306 if ( flags
& wxACCEL_ALT
)
308 if ( flags
& wxACCEL_CTRL
)
310 if ( flags
& wxACCEL_SHIFT
)
313 const int code
= GetKeyCode();
315 if ( code
>= WXK_F1
&& code
<= WXK_F12
)
316 text
<< _("F") << code
- WXK_F1
+ 1;
317 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
318 text
<< _("KP_") << code
- WXK_NUMPAD0
;
319 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
320 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
321 else // check the named keys
324 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
326 const wxKeyName
& kn
= wxKeyNames
[n
];
327 if ( code
== kn
.code
)
329 text
<< wxGetTranslation(kn
.name
);
334 if ( n
== WXSIZEOF(wxKeyNames
) )
336 // must be a simple key
339 // we can't call wxIsalnum() for non-ASCII characters in ASCII
340 // build as they're only defined for the ASCII range (or EOF)
345 text
<< (wxChar
)code
;
349 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
357 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
359 return wxAcceleratorEntry::Create(label
);
362 #endif // wxUSE_ACCEL