]>
git.saurik.com Git - wxWidgets.git/blob - src/common/accelcmn.cpp
a038c58d206b94e5be2cd2bb610d2bd63fddb86c
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"
33 // ============================================================================
34 // wxAcceleratorEntry implementation
35 // ============================================================================
37 static const struct wxKeyName
43 { WXK_DELETE
, wxTRANSLATE("DEL") },
44 { WXK_DELETE
, wxTRANSLATE("DELETE") },
45 { WXK_BACK
, wxTRANSLATE("BACK") },
46 { WXK_INSERT
, wxTRANSLATE("INS") },
47 { WXK_INSERT
, wxTRANSLATE("INSERT") },
48 { WXK_RETURN
, wxTRANSLATE("ENTER") },
49 { WXK_RETURN
, wxTRANSLATE("RETURN") },
50 { WXK_PAGEUP
, wxTRANSLATE("PGUP") },
51 { WXK_PAGEDOWN
, wxTRANSLATE("PGDN") },
52 { WXK_LEFT
, wxTRANSLATE("LEFT") },
53 { WXK_RIGHT
, wxTRANSLATE("RIGHT") },
54 { WXK_UP
, wxTRANSLATE("UP") },
55 { WXK_DOWN
, wxTRANSLATE("DOWN") },
56 { WXK_HOME
, wxTRANSLATE("HOME") },
57 { WXK_END
, wxTRANSLATE("END") },
58 { WXK_SPACE
, wxTRANSLATE("SPACE") },
59 { WXK_TAB
, wxTRANSLATE("TAB") },
60 { WXK_ESCAPE
, wxTRANSLATE("ESC") },
61 { WXK_ESCAPE
, wxTRANSLATE("ESCAPE") },
62 { WXK_CANCEL
, wxTRANSLATE("CANCEL") },
63 { WXK_CLEAR
, wxTRANSLATE("CLEAR") },
64 { WXK_MENU
, wxTRANSLATE("MENU") },
65 { WXK_PAUSE
, wxTRANSLATE("PAUSE") },
66 { WXK_CAPITAL
, wxTRANSLATE("CAPITAL") },
67 { WXK_SELECT
, wxTRANSLATE("SELECT") },
68 { WXK_PRINT
, wxTRANSLATE("PRINT") },
69 { WXK_EXECUTE
, wxTRANSLATE("EXECUTE") },
70 { WXK_SNAPSHOT
, wxTRANSLATE("SNAPSHOT") },
71 { WXK_HELP
, wxTRANSLATE("HELP") },
72 { WXK_ADD
, wxTRANSLATE("ADD") },
73 { WXK_SEPARATOR
, wxTRANSLATE("SEPARATOR") },
74 { WXK_SUBTRACT
, wxTRANSLATE("SUBTRACT") },
75 { WXK_DECIMAL
, wxTRANSLATE("DECIMAL") },
76 { WXK_DIVIDE
, wxTRANSLATE("DIVIDE") },
77 { WXK_NUMLOCK
, wxTRANSLATE("NUM_LOCK") },
78 { WXK_SCROLL
, wxTRANSLATE("SCROLL_LOCK") },
79 { WXK_PAGEUP
, wxTRANSLATE("PAGEUP") },
80 { WXK_PAGEDOWN
, wxTRANSLATE("PAGEDOWN") },
81 { WXK_NUMPAD_SPACE
, wxTRANSLATE("KP_SPACE") },
82 { WXK_NUMPAD_TAB
, wxTRANSLATE("KP_TAB") },
83 { WXK_NUMPAD_ENTER
, wxTRANSLATE("KP_ENTER") },
84 { WXK_NUMPAD_HOME
, wxTRANSLATE("KP_HOME") },
85 { WXK_NUMPAD_LEFT
, wxTRANSLATE("KP_LEFT") },
86 { WXK_NUMPAD_UP
, wxTRANSLATE("KP_UP") },
87 { WXK_NUMPAD_RIGHT
, wxTRANSLATE("KP_RIGHT") },
88 { WXK_NUMPAD_DOWN
, wxTRANSLATE("KP_DOWN") },
89 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PRIOR") },
90 { WXK_NUMPAD_PAGEUP
, wxTRANSLATE("KP_PAGEUP") },
91 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_NEXT") },
92 { WXK_NUMPAD_PAGEDOWN
, wxTRANSLATE("KP_PAGEDOWN") },
93 { WXK_NUMPAD_END
, wxTRANSLATE("KP_END") },
94 { WXK_NUMPAD_BEGIN
, wxTRANSLATE("KP_BEGIN") },
95 { WXK_NUMPAD_INSERT
, wxTRANSLATE("KP_INSERT") },
96 { WXK_NUMPAD_DELETE
, wxTRANSLATE("KP_DELETE") },
97 { WXK_NUMPAD_EQUAL
, wxTRANSLATE("KP_EQUAL") },
98 { WXK_NUMPAD_MULTIPLY
, wxTRANSLATE("KP_MULTIPLY") },
99 { WXK_NUMPAD_ADD
, wxTRANSLATE("KP_ADD") },
100 { WXK_NUMPAD_SEPARATOR
, wxTRANSLATE("KP_SEPARATOR") },
101 { WXK_NUMPAD_SUBTRACT
, wxTRANSLATE("KP_SUBTRACT") },
102 { WXK_NUMPAD_DECIMAL
, wxTRANSLATE("KP_DECIMAL") },
103 { WXK_NUMPAD_DIVIDE
, wxTRANSLATE("KP_DIVIDE") },
104 { WXK_WINDOWS_LEFT
, wxTRANSLATE("WINDOWS_LEFT") },
105 { WXK_WINDOWS_RIGHT
, wxTRANSLATE("WINDOWS_RIGHT") },
106 { WXK_WINDOWS_MENU
, wxTRANSLATE("WINDOWS_MENU") },
107 { WXK_COMMAND
, wxTRANSLATE("COMMAND") },
110 // return true if the 2 strings refer to the same accel
112 // as accels can be either translated or not, check for both possibilities and
113 // also compare case-insensitively as the key names case doesn't count
114 static inline bool CompareAccelString(const wxString
& str
, const wxChar
*accel
)
116 return str
.CmpNoCase(accel
) == 0
118 || str
.CmpNoCase(wxGetTranslation(accel
)) == 0
123 // return prefixCode+number if the string is of the form "<prefix><number>" and
126 // first and last parameter specify the valid domain for "number" part
127 static int IsNumberedAccelKey(const wxString
& str
,
128 const wxChar
*prefix
,
129 wxKeyCode prefixCode
,
133 const size_t lenPrefix
= wxStrlen(prefix
);
134 if ( !CompareAccelString(str
.Left(lenPrefix
), prefix
) )
138 if ( !str
.Mid(lenPrefix
).ToULong(&num
) )
141 if ( num
< first
|| num
> last
)
143 // this must be a mistake, chances that this is a valid name of another
144 // key are vanishingly small
145 wxLogDebug(_T("Invalid key string \"%s\""), str
.c_str());
149 return prefixCode
+ num
- first
;
154 wxAcceleratorEntry::ParseAccel(const wxString
& text
, int *flagsOut
, int *keyOut
)
156 // the parser won't like trailing spaces
157 wxString label
= text
;
158 label
.Trim(true); // the initial \t must be preserved so don't strip leading whitespaces
160 // check for accelerators: they are given after '\t'
161 int posTab
= label
.Find(wxT('\t'));
162 if ( posTab
== wxNOT_FOUND
)
167 // parse the accelerator string
168 int accelFlags
= wxACCEL_NORMAL
;
170 for ( size_t n
= (size_t)posTab
+ 1; n
< label
.length(); n
++ )
172 if ( (label
[n
] == '+') || (label
[n
] == '-') )
174 if ( CompareAccelString(current
, wxTRANSLATE("ctrl")) )
175 accelFlags
|= wxACCEL_CTRL
;
176 else if ( CompareAccelString(current
, wxTRANSLATE("alt")) )
177 accelFlags
|= wxACCEL_ALT
;
178 else if ( CompareAccelString(current
, wxTRANSLATE("shift")) )
179 accelFlags
|= wxACCEL_SHIFT
;
180 else // not a recognized modifier name
182 // we may have "Ctrl-+", for example, but we still want to
183 // catch typos like "Crtl-A" so only give the warning if we
184 // have something before the current '+' or '-', else take
185 // it as a literal symbol
186 if ( current
.empty() )
190 // skip clearing it below
195 wxLogDebug(wxT("Unknown accel modifier: '%s'"),
202 else // not special character
204 current
+= (wxChar
) wxTolower(label
[n
]);
209 const size_t len
= current
.length();
213 wxLogDebug(wxT("No accel key found, accel string ignored."));
217 // it's just a letter
218 keyCode
= current
[0U];
220 // if the key is used with any modifiers, make it an uppercase one
221 // because Ctrl-A and Ctrl-a are the same; but keep it as is if it's
222 // used alone as 'a' and 'A' are different
223 if ( accelFlags
!= wxACCEL_NORMAL
)
224 keyCode
= wxToupper(keyCode
);
228 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("F"),
232 for ( size_t n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
234 const wxKeyName
& kn
= wxKeyNames
[n
];
235 if ( CompareAccelString(current
, kn
.name
) )
244 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("KP_"),
247 keyCode
= IsNumberedAccelKey(current
, wxTRANSLATE("SPECIAL"),
248 WXK_SPECIAL1
, 1, 20);
252 wxLogDebug(wxT("Unrecognized accel key '%s', accel string ignored."),
259 wxASSERT_MSG( keyCode
, _T("logic error: should have key code here") );
262 *flagsOut
= accelFlags
;
270 wxAcceleratorEntry
*wxAcceleratorEntry::Create(const wxString
& str
)
274 if ( !ParseAccel(str
, &flags
, &keyCode
) )
277 return new wxAcceleratorEntry(flags
, keyCode
);
280 bool wxAcceleratorEntry::FromString(const wxString
& str
)
282 return ParseAccel(str
, &m_flags
, &m_keyCode
);
285 wxString
wxAcceleratorEntry::ToString() const
289 int flags
= GetFlags();
290 if ( flags
& wxACCEL_ALT
)
292 if ( flags
& wxACCEL_CTRL
)
294 if ( flags
& wxACCEL_SHIFT
)
297 const int code
= GetKeyCode();
299 if ( code
>= WXK_F1
&& code
<= WXK_F12
)
300 text
<< _("F") << code
- WXK_F1
+ 1;
301 else if ( code
>= WXK_NUMPAD0
&& code
<= WXK_NUMPAD9
)
302 text
<< _("KP_") << code
- WXK_NUMPAD0
;
303 else if ( code
>= WXK_SPECIAL1
&& code
<= WXK_SPECIAL20
)
304 text
<< _("SPECIAL") << code
- WXK_SPECIAL1
+ 1;
305 else // check the named keys
308 for ( n
= 0; n
< WXSIZEOF(wxKeyNames
); n
++ )
310 const wxKeyName
& kn
= wxKeyNames
[n
];
311 if ( code
== kn
.code
)
313 text
<< wxGetTranslation(kn
.name
);
318 if ( n
== WXSIZEOF(wxKeyNames
) )
320 // must be a simple key
327 text
<< (wxChar
)code
;
331 wxFAIL_MSG( wxT("unknown keyboard accelerator code") );
339 wxAcceleratorEntry
*wxGetAccelFromString(const wxString
& label
)
341 return wxAcceleratorEntry::Create(label
);
344 #endif // wxUSE_ACCEL