]>
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"
35 // ============================================================================
36 // wxAcceleratorEntry implementation
37 // ============================================================================
39 static const struct wxKeyName
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") },
112 // return true if the 2 strings refer to the same accel
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
116 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
118 return str
.CmpNoCase(accel
) == 0
120 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
125 // return prefixCode+number if the string is of the form "<prefix><number>" and
128 // first and last parameter specify the valid domain for "number" part
129 static int IsNumberedAccelKey(const wxString
& str
,
130 const wxChar
*prefix
,
131 wxKeyCode prefixCode
,
135 const size_t lenPrefix
= wxStrlen(prefix
);
136 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
140 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
143 if ( num
< first
|| num
> last
)
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());
151 return prefixCode
+ num
- first
;
156 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
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
162 // check for accelerators: they are given after '\t'
163 int posTab
= label
.Find(wxT('\t'));
164 if ( posTab
== wxNOT_FOUND
)
169 // parse the accelerator string
170 int accelFlags
= wxACCEL_NORMAL
;
172 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
174 if ( (label
[n
] == '+') || (label
[n
] == '-') )
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
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() )
192 // skip clearing it below
197 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
204 else // not special character
206 current
+= (wxChar
) wxTolower(label
[n
]);
211 const size_t len
= current
.length();
215 wxLogDebug(wxT("No accel key found, accel string ignored."));
219 // it's just a letter
220 keyCode
= current
[0U];
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
);
230 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
234 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
236 const wxKeyName
& kn
= wxKeyNames
[n
];
237 if ( CompareAccelString(current
, kn
.name
) )
246 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
249 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
250 WXK_SPECIAL1
, 1, 20);
254 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
261 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
264 *flagsOut
= accelFlags
;
272 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
276 if ( !ParseAccel(str
, &flags
, &keyCode
) )
279 return new wxAcceleratorEntry(flags
, keyCode
);
282 bool wxAcceleratorEntry::FromString(const wxString
& str
)
284 return ParseAccel(str
, &m_flags
, &m_keyCode
);
287 wxString
wxAcceleratorEntry::ToString() const
291 int flags
= GetFlags();
292 if ( flags
& wxACCEL_ALT
)
294 if ( flags
& wxACCEL_CTRL
)
296 if ( flags
& wxACCEL_SHIFT
)
299 const int code
= GetKeyCode();
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
310 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
312 const wxKeyName
& kn
= wxKeyNames
[n
];
313 if ( code
== kn
.code
)
315 text
<< wxGetTranslation(kn
.name
);
320 if ( n
== WXSIZEOF(wxKeyNames
) )
322 // must be a simple key
329 text
<< (wxChar
)code
;
333 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
341 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
343 return wxAcceleratorEntry::Create(label
);
346 #endif // wxUSE_ACCEL