1 | /** @file
|
---|
2 | *
|
---|
3 | * VBox frontends: Qt GUI ("VirtualBox"):
|
---|
4 | * VirtualBox Qt extensions: QIHotKeyEdit class implementation
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2006-2007 Sun Microsystems, Inc.
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | *
|
---|
18 | * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
|
---|
19 | * Clara, CA 95054 USA or visit http://www.sun.com if you need
|
---|
20 | * additional information or have any questions.
|
---|
21 | */
|
---|
22 |
|
---|
23 | #include "QIHotKeyEdit.h"
|
---|
24 |
|
---|
25 | #include "VBoxDefs.h"
|
---|
26 |
|
---|
27 | #include <qapplication.h>
|
---|
28 | #include <qstyle.h>
|
---|
29 | #include <qlineedit.h>
|
---|
30 |
|
---|
31 | #ifdef Q_WS_WIN
|
---|
32 | /* VBox/cdefs.h defines these: */
|
---|
33 | #undef LOWORD
|
---|
34 | #undef HIWORD
|
---|
35 | #undef LOBYTE
|
---|
36 | #undef HIBYTE
|
---|
37 | #include <windows.h>
|
---|
38 | #endif
|
---|
39 |
|
---|
40 | #if defined (Q_WS_PM)
|
---|
41 | QMap <int, QString> QIHotKeyEdit::sKeyNames;
|
---|
42 | #endif
|
---|
43 |
|
---|
44 | #ifdef Q_WS_X11
|
---|
45 | /* We need to capture some X11 events directly which
|
---|
46 | * requires the XEvent structure to be defined. However,
|
---|
47 | * including the Xlib header file will cause some nasty
|
---|
48 | * conflicts with Qt. Therefore we use the following hack
|
---|
49 | * to redefine those conflicting identifiers. */
|
---|
50 | #define XK_XKB_KEYS
|
---|
51 | #define XK_MISCELLANY
|
---|
52 | #include <X11/Xlib.h>
|
---|
53 | #include <X11/Xutil.h>
|
---|
54 | #include <X11/keysymdef.h>
|
---|
55 | #ifdef KeyPress
|
---|
56 | const int XFocusOut = FocusOut;
|
---|
57 | const int XFocusIn = FocusIn;
|
---|
58 | const int XKeyPress = KeyPress;
|
---|
59 | const int XKeyRelease = KeyRelease;
|
---|
60 | #undef KeyRelease
|
---|
61 | #undef KeyPress
|
---|
62 | #undef FocusOut
|
---|
63 | #undef FocusIn
|
---|
64 | #endif
|
---|
65 | #include "XKeyboard.h"
|
---|
66 | QMap <QString, QString> QIHotKeyEdit::sKeyNames;
|
---|
67 | #endif
|
---|
68 |
|
---|
69 | #ifdef Q_WS_MAC
|
---|
70 | #include "DarwinKeyboard.h"
|
---|
71 | #endif
|
---|
72 |
|
---|
73 |
|
---|
74 | #if defined (Q_WS_WIN32)
|
---|
75 | /**
|
---|
76 | * Returns the correct modifier vkey for the *last* keyboard message,
|
---|
77 | * distinguishing between left and right keys. If both are pressed
|
---|
78 | * the left key wins. If the pressed key not a modifier, wParam is returned
|
---|
79 | * unchanged.
|
---|
80 | */
|
---|
81 | int qi_distinguish_modifier_vkey (WPARAM wParam)
|
---|
82 | {
|
---|
83 | int keyval = wParam;
|
---|
84 | switch (wParam)
|
---|
85 | {
|
---|
86 | case VK_SHIFT:
|
---|
87 | if (::GetKeyState (VK_LSHIFT) & 0x8000) keyval = VK_LSHIFT;
|
---|
88 | else if (::GetKeyState (VK_RSHIFT) & 0x8000) keyval = VK_RSHIFT;
|
---|
89 | break;
|
---|
90 | case VK_CONTROL:
|
---|
91 | if (::GetKeyState (VK_LCONTROL) & 0x8000) keyval = VK_LCONTROL;
|
---|
92 | else if (::GetKeyState (VK_RCONTROL) & 0x8000) keyval = VK_RCONTROL;
|
---|
93 | break;
|
---|
94 | case VK_MENU:
|
---|
95 | if (::GetKeyState (VK_LMENU) & 0x8000) keyval = VK_LMENU;
|
---|
96 | else if (::GetKeyState (VK_RMENU) & 0x8000) keyval = VK_RMENU;
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | return keyval;
|
---|
100 | }
|
---|
101 | #endif
|
---|
102 |
|
---|
103 | /** @class QIHotKeyEdit
|
---|
104 | *
|
---|
105 | * The QIHotKeyEdit widget is a hot key editor.
|
---|
106 | */
|
---|
107 |
|
---|
108 | const char *QIHotKeyEdit::kNoneSymbName = "<none>";
|
---|
109 |
|
---|
110 | QIHotKeyEdit::QIHotKeyEdit (QWidget *aParent, const char *aName) :
|
---|
111 | QLabel (aParent, aName)
|
---|
112 | {
|
---|
113 | #ifdef Q_WS_X11
|
---|
114 | // initialize the X keyboard subsystem
|
---|
115 | initXKeyboard (this->x11Display());
|
---|
116 | #endif
|
---|
117 |
|
---|
118 | clear();
|
---|
119 |
|
---|
120 | setFrameStyle (LineEditPanel | Sunken);
|
---|
121 | setAlignment (AlignHCenter | AlignBottom);
|
---|
122 | setFocusPolicy (StrongFocus);
|
---|
123 |
|
---|
124 | QPalette p = palette();
|
---|
125 | p.setColor (QPalette::Active, QColorGroup::Foreground,
|
---|
126 | p.color (QPalette::Active, QColorGroup::HighlightedText));
|
---|
127 | p.setColor (QPalette::Active, QColorGroup::Background,
|
---|
128 | p.color (QPalette::Active, QColorGroup::Highlight));
|
---|
129 | p.setColor (QPalette::Inactive, QColorGroup::Foreground,
|
---|
130 | p.color (QPalette::Active, QColorGroup::Text));
|
---|
131 | p.setColor (QPalette::Inactive, QColorGroup::Background,
|
---|
132 | p.color (QPalette::Active, QColorGroup::Base));
|
---|
133 |
|
---|
134 | mTrueACG = p.active();
|
---|
135 | p.setActive (p.inactive());
|
---|
136 | setPalette (p);
|
---|
137 |
|
---|
138 | #ifdef Q_WS_MAC
|
---|
139 | mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
|
---|
140 |
|
---|
141 | EventTypeSpec eventTypes[4];
|
---|
142 | eventTypes[0].eventClass = kEventClassKeyboard;
|
---|
143 | eventTypes[0].eventKind = kEventRawKeyDown;
|
---|
144 | eventTypes[1].eventClass = kEventClassKeyboard;
|
---|
145 | eventTypes[1].eventKind = kEventRawKeyUp;
|
---|
146 | eventTypes[2].eventClass = kEventClassKeyboard;
|
---|
147 | eventTypes[2].eventKind = kEventRawKeyRepeat;
|
---|
148 | eventTypes[3].eventClass = kEventClassKeyboard;
|
---|
149 | eventTypes[3].eventKind = kEventRawKeyModifiersChanged;
|
---|
150 |
|
---|
151 | EventHandlerUPP eventHandler = ::NewEventHandlerUPP (QIHotKeyEdit::darwinEventHandlerProc);
|
---|
152 |
|
---|
153 | mDarwinEventHandlerRef = NULL;
|
---|
154 | ::InstallApplicationEventHandler (eventHandler, RT_ELEMENTS( eventTypes ), &eventTypes[0],
|
---|
155 | this, &mDarwinEventHandlerRef);
|
---|
156 | ::DisposeEventHandlerUPP (eventHandler);
|
---|
157 | ::DarwinGrabKeyboard (false /* just modifiers */);
|
---|
158 | #endif
|
---|
159 | }
|
---|
160 |
|
---|
161 | QIHotKeyEdit::~QIHotKeyEdit()
|
---|
162 | {
|
---|
163 | #ifdef Q_WS_MAC
|
---|
164 | ::DarwinReleaseKeyboard();
|
---|
165 | ::RemoveEventHandler (mDarwinEventHandlerRef);
|
---|
166 | mDarwinEventHandlerRef = NULL;
|
---|
167 | #endif
|
---|
168 | }
|
---|
169 |
|
---|
170 | // Public members
|
---|
171 | /////////////////////////////////////////////////////////////////////////////
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Set the hot key value. O means there is no hot key.
|
---|
175 | *
|
---|
176 | * @note
|
---|
177 | * The key value is platform-dependent. On Win32 it is the
|
---|
178 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
179 | * to the keycode.
|
---|
180 | */
|
---|
181 | void QIHotKeyEdit::setKey (int aKeyVal)
|
---|
182 | {
|
---|
183 | mKeyVal = aKeyVal;
|
---|
184 | mSymbName = QIHotKeyEdit::keyName (aKeyVal);
|
---|
185 | updateText();
|
---|
186 | }
|
---|
187 |
|
---|
188 | /**@@ QIHotKeyEdit::key() const
|
---|
189 | *
|
---|
190 | * Returns the value of the last recodred hot key.
|
---|
191 | * O means there is no hot key.
|
---|
192 | *
|
---|
193 | * @note
|
---|
194 | * The key value is platform-dependent. On Win32 it is the
|
---|
195 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
196 | * to the keycode.
|
---|
197 | */
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Stolen from QLineEdit.
|
---|
201 | */
|
---|
202 | QSize QIHotKeyEdit::sizeHint() const
|
---|
203 | {
|
---|
204 | constPolish();
|
---|
205 | QFontMetrics fm (font());
|
---|
206 | int h = QMAX(fm.lineSpacing(), 14) + 2;
|
---|
207 | int w = fm.width( 'x' ) * 17; // "some"
|
---|
208 | int m = frameWidth() * 2;
|
---|
209 | return (style().sizeFromContents (QStyle::CT_LineEdit, this,
|
---|
210 | QSize (w + m, h + m)
|
---|
211 | .expandedTo(QApplication::globalStrut())));
|
---|
212 | }
|
---|
213 |
|
---|
214 | /**
|
---|
215 | * Stolen from QLineEdit.
|
---|
216 | */
|
---|
217 | QSize QIHotKeyEdit::minimumSizeHint() const
|
---|
218 | {
|
---|
219 | constPolish();
|
---|
220 | QFontMetrics fm = fontMetrics();
|
---|
221 | int h = fm.height() + QMAX (2, fm.leading());
|
---|
222 | int w = fm.maxWidth();
|
---|
223 | int m = frameWidth() * 2;
|
---|
224 | return QSize (w + m, h + m);
|
---|
225 | }
|
---|
226 |
|
---|
227 | #if defined (Q_WS_PM)
|
---|
228 | /**
|
---|
229 | * Returns the virtual key extracted from the QMSG structure.
|
---|
230 | *
|
---|
231 | * This function tries to detect some extra virtual keys definitions missing
|
---|
232 | * in PM (like Left Shift, Left Ctrl, Win keys). In all other cases it simply
|
---|
233 | * returns SHORT2FROMMP (aMsg->mp2).
|
---|
234 | *
|
---|
235 | * @param aMsg Pointer to the QMSG structure to extract the virtual key from.
|
---|
236 | * @return The extracted virtual key code or zero if there is no virtual key.
|
---|
237 | */
|
---|
238 | /* static */
|
---|
239 | int QIHotKeyEdit::virtualKey (QMSG *aMsg)
|
---|
240 | {
|
---|
241 | USHORT f = SHORT1FROMMP (aMsg->mp1);
|
---|
242 | CHAR scan = CHAR4FROMMP (aMsg->mp1);
|
---|
243 | USHORT ch = SHORT1FROMMP (aMsg->mp2);
|
---|
244 | int vkey = (unsigned int) SHORT2FROMMP (aMsg->mp2);
|
---|
245 |
|
---|
246 | if (f & KC_VIRTUALKEY)
|
---|
247 | {
|
---|
248 | /* distinguish Left Shift from Right Shift) */
|
---|
249 | if (vkey == VK_SHIFT && scan == 0x2A)
|
---|
250 | vkey = VK_LSHIFT;
|
---|
251 | /* distinguish Left Ctrl from Right Ctrl */
|
---|
252 | else if (vkey == VK_CTRL && scan == 0x1D)
|
---|
253 | vkey = VK_LCTRL;
|
---|
254 | /* distinguish Ctrl+ScrLock from Ctrl+Break */
|
---|
255 | else if (vkey == VK_BREAK && scan == 0x46 && f & KC_CTRL)
|
---|
256 | vkey = VK_SCRLLOCK;
|
---|
257 | }
|
---|
258 | else if (!(f & KC_CHAR))
|
---|
259 | {
|
---|
260 | /* detect some special keys that have a pseudo char code in the high
|
---|
261 | * byte of ch (probably this is less device-dependent than
|
---|
262 | * scancode) */
|
---|
263 | switch (ch)
|
---|
264 | {
|
---|
265 | case 0xEC00: vkey = VK_LWIN; break;
|
---|
266 | case 0xED00: vkey = VK_RWIN; break;
|
---|
267 | case 0xEE00: vkey = VK_WINMENU; break;
|
---|
268 | case 0xF900: vkey = VK_FORWARD; break;
|
---|
269 | case 0xFA00: vkey = VK_BACKWARD; break;
|
---|
270 | default: vkey = 0;
|
---|
271 | }
|
---|
272 | }
|
---|
273 |
|
---|
274 | return vkey;
|
---|
275 | }
|
---|
276 | #endif
|
---|
277 |
|
---|
278 | #if defined (Q_WS_PM)
|
---|
279 | /**
|
---|
280 | * Updates the associative array containing the translations of PM virtual
|
---|
281 | * keys to human readable key names.
|
---|
282 | */
|
---|
283 | /* static */
|
---|
284 | void QIHotKeyEdit::languageChange()
|
---|
285 | {
|
---|
286 | /* Note: strings for the same key must match strings in languageChange()
|
---|
287 | * versions for all platforms, to keep translators happy. */
|
---|
288 |
|
---|
289 | sKeyNames [VK_LSHIFT] = tr ("Left Shift");
|
---|
290 | sKeyNames [VK_SHIFT] = tr ("Right Shift");
|
---|
291 | sKeyNames [VK_LCTRL] = tr ("Left Ctrl");
|
---|
292 | sKeyNames [VK_CTRL] = tr ("Right Ctrl");
|
---|
293 | sKeyNames [VK_ALT] = tr ("Left Alt");
|
---|
294 | sKeyNames [VK_ALTGRAF] = tr ("Right Alt");
|
---|
295 | sKeyNames [VK_LWIN] = tr ("Left WinKey");
|
---|
296 | sKeyNames [VK_RWIN] = tr ("Right WinKey");
|
---|
297 | sKeyNames [VK_WINMENU] = tr ("Menu key");
|
---|
298 | sKeyNames [VK_CAPSLOCK] = tr ("Caps Lock");
|
---|
299 | sKeyNames [VK_SCRLLOCK] = tr ("Scroll Lock");
|
---|
300 |
|
---|
301 | sKeyNames [VK_PAUSE] = tr ("Pause");
|
---|
302 | sKeyNames [VK_PRINTSCRN] = tr ("Print Screen");
|
---|
303 |
|
---|
304 | sKeyNames [VK_F1] = tr ("F1");
|
---|
305 | sKeyNames [VK_F2] = tr ("F2");
|
---|
306 | sKeyNames [VK_F3] = tr ("F3");
|
---|
307 | sKeyNames [VK_F4] = tr ("F4");
|
---|
308 | sKeyNames [VK_F5] = tr ("F5");
|
---|
309 | sKeyNames [VK_F6] = tr ("F6");
|
---|
310 | sKeyNames [VK_F7] = tr ("F7");
|
---|
311 | sKeyNames [VK_F8] = tr ("F8");
|
---|
312 | sKeyNames [VK_F9] = tr ("F9");
|
---|
313 | sKeyNames [VK_F10] = tr ("F10");
|
---|
314 | sKeyNames [VK_F11] = tr ("F11");
|
---|
315 | sKeyNames [VK_F12] = tr ("F12");
|
---|
316 | sKeyNames [VK_F13] = tr ("F13");
|
---|
317 | sKeyNames [VK_F14] = tr ("F14");
|
---|
318 | sKeyNames [VK_F15] = tr ("F15");
|
---|
319 | sKeyNames [VK_F16] = tr ("F16");
|
---|
320 | sKeyNames [VK_F17] = tr ("F17");
|
---|
321 | sKeyNames [VK_F18] = tr ("F18");
|
---|
322 | sKeyNames [VK_F19] = tr ("F19");
|
---|
323 | sKeyNames [VK_F20] = tr ("F20");
|
---|
324 | sKeyNames [VK_F21] = tr ("F21");
|
---|
325 | sKeyNames [VK_F22] = tr ("F22");
|
---|
326 | sKeyNames [VK_F23] = tr ("F23");
|
---|
327 | sKeyNames [VK_F24] = tr ("F24");
|
---|
328 |
|
---|
329 | sKeyNames [VK_NUMLOCK] = tr ("Num Lock");
|
---|
330 | sKeyNames [VK_FORWARD] = tr ("Forward");
|
---|
331 | sKeyNames [VK_BACKWARD] = tr ("Back");
|
---|
332 | }
|
---|
333 | #elif defined (Q_WS_X11)
|
---|
334 | /**
|
---|
335 | * Updates the associative array containing the translations of X11 key strings to human
|
---|
336 | * readable key names.
|
---|
337 | */
|
---|
338 | /* static */
|
---|
339 | void QIHotKeyEdit::languageChange()
|
---|
340 | {
|
---|
341 | /* Note: strings for the same key must match strings in languageChange()
|
---|
342 | * versions for all platforms, to keep translators happy. */
|
---|
343 |
|
---|
344 | sKeyNames ["Shift_L"] = tr ("Left Shift");
|
---|
345 | sKeyNames ["Shift_R"] = tr ("Right Shift");
|
---|
346 | sKeyNames ["Control_L"] = tr ("Left Ctrl");
|
---|
347 | sKeyNames ["Control_R"] = tr ("Right Ctrl");
|
---|
348 | sKeyNames ["Alt_L"] = tr ("Left Alt");
|
---|
349 | sKeyNames ["Alt_R"] = tr ("Right Alt");
|
---|
350 | sKeyNames ["Super_L"] = tr ("Left WinKey");
|
---|
351 | sKeyNames ["Super_R"] = tr ("Right WinKey");
|
---|
352 | sKeyNames ["Menu"] = tr ("Menu key");
|
---|
353 | sKeyNames ["ISO_Level3_Shift"] = tr ("Alt Gr");
|
---|
354 | sKeyNames ["Caps_Lock"] = tr ("Caps Lock");
|
---|
355 | sKeyNames ["Scroll_Lock"] = tr ("Scroll Lock");
|
---|
356 | }
|
---|
357 | #endif
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Returns the string representation of a given key.
|
---|
361 | *
|
---|
362 | * @note
|
---|
363 | * The key value is platform-dependent. On Win32 it is the
|
---|
364 | * virtial key, on Linux it is the first (0) keysym corresponding
|
---|
365 | * to the keycode.
|
---|
366 | */
|
---|
367 | /* static */
|
---|
368 | QString QIHotKeyEdit::keyName (int aKeyVal)
|
---|
369 | {
|
---|
370 | QString name;
|
---|
371 |
|
---|
372 | if (!aKeyVal)
|
---|
373 | {
|
---|
374 | name = tr (kNoneSymbName);
|
---|
375 | }
|
---|
376 | else
|
---|
377 | {
|
---|
378 | #if defined (Q_WS_WIN32)
|
---|
379 | /* stupid MapVirtualKey doesn't distinguish between right and left
|
---|
380 | * vkeys, even under XP, despite that it stated in msdn. do it by
|
---|
381 | * hand. */
|
---|
382 | int scan;
|
---|
383 | switch (aKeyVal)
|
---|
384 | {
|
---|
385 | case VK_RSHIFT: scan = 0x36 << 16; break;
|
---|
386 | case VK_RCONTROL: scan = (0x1D << 16) | (1 << 24); break;
|
---|
387 | case VK_RMENU: scan = (0x38 << 16) | (1 << 24); break;
|
---|
388 | default: scan = ::MapVirtualKey (aKeyVal, 0) << 16;
|
---|
389 | }
|
---|
390 | TCHAR *str = new TCHAR[256];
|
---|
391 | if (::GetKeyNameText (scan, str, 256))
|
---|
392 | {
|
---|
393 | name = QString::fromUcs2 (str);
|
---|
394 | }
|
---|
395 | else
|
---|
396 | {
|
---|
397 | AssertFailed();
|
---|
398 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
399 | }
|
---|
400 | delete[] str;
|
---|
401 | #elif defined (Q_WS_PM)
|
---|
402 | name = sKeyNames [aKeyVal];
|
---|
403 | if (name.isNull())
|
---|
404 | {
|
---|
405 | AssertFailed();
|
---|
406 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
407 | }
|
---|
408 | #elif defined (Q_WS_X11)
|
---|
409 | char *sn = ::XKeysymToString ((KeySym) aKeyVal);
|
---|
410 | if (sn)
|
---|
411 | {
|
---|
412 | name = sKeyNames [sn];
|
---|
413 | if (name.isEmpty())
|
---|
414 | name = sn;
|
---|
415 | }
|
---|
416 | else
|
---|
417 | {
|
---|
418 | AssertFailed();
|
---|
419 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
420 | }
|
---|
421 | #elif defined(Q_WS_MAC)
|
---|
422 | UInt32 modMask = DarwinKeyCodeToDarwinModifierMask (aKeyVal);
|
---|
423 | switch (modMask)
|
---|
424 | {
|
---|
425 | case shiftKey:
|
---|
426 | case optionKey:
|
---|
427 | case controlKey:
|
---|
428 | case cmdKey:
|
---|
429 | name = tr ("Left ");
|
---|
430 | break;
|
---|
431 | case rightShiftKey:
|
---|
432 | case rightOptionKey:
|
---|
433 | case rightControlKey:
|
---|
434 | case kEventKeyModifierRightCmdKeyMask:
|
---|
435 | name = tr ("Right ");
|
---|
436 | break;
|
---|
437 | default:
|
---|
438 | AssertMsgFailedReturn (("modMask=%#x\n", modMask), QString());
|
---|
439 | }
|
---|
440 | switch (modMask)
|
---|
441 | {
|
---|
442 | case shiftKey:
|
---|
443 | case rightShiftKey:
|
---|
444 | name += QChar (kShiftUnicode);
|
---|
445 | break;
|
---|
446 | case optionKey:
|
---|
447 | case rightOptionKey:
|
---|
448 | name += QChar (kOptionUnicode);
|
---|
449 | break;
|
---|
450 | case controlKey:
|
---|
451 | case rightControlKey:
|
---|
452 | name += QChar (kControlUnicode);
|
---|
453 | break;
|
---|
454 | case cmdKey:
|
---|
455 | case kEventKeyModifierRightCmdKeyMask:
|
---|
456 | name += QChar (kCommandUnicode);
|
---|
457 | break;
|
---|
458 | }
|
---|
459 | #else
|
---|
460 | AssertFailed();
|
---|
461 | name = QString (tr ("<key_%1>")).arg (aKeyVal);
|
---|
462 | #endif
|
---|
463 | }
|
---|
464 |
|
---|
465 | return name;
|
---|
466 | }
|
---|
467 |
|
---|
468 | /* static */
|
---|
469 | bool QIHotKeyEdit::isValidKey (int aKeyVal)
|
---|
470 | {
|
---|
471 | #if defined(Q_WS_WIN32)
|
---|
472 | return (
|
---|
473 | (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPITAL) ||
|
---|
474 | aKeyVal == VK_PRINT ||
|
---|
475 | aKeyVal == VK_LWIN || aKeyVal == VK_RWIN ||
|
---|
476 | aKeyVal == VK_APPS ||
|
---|
477 | (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
|
---|
478 | aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCROLL ||
|
---|
479 | (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_RMENU));
|
---|
480 | #elif defined(Q_WS_PM)
|
---|
481 | return (
|
---|
482 | (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPSLOCK) ||
|
---|
483 | aKeyVal == VK_PRINTSCRN ||
|
---|
484 | (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) ||
|
---|
485 | aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCRLLOCK ||
|
---|
486 | (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_BACKWARD));
|
---|
487 | #elif defined(Q_WS_X11)
|
---|
488 | KeySym ks = (KeySym) aKeyVal;
|
---|
489 | return
|
---|
490 | (
|
---|
491 | ks != NoSymbol &&
|
---|
492 | ks != XK_Insert
|
---|
493 | ) && (
|
---|
494 | ks == XK_Scroll_Lock ||
|
---|
495 | IsModifierKey (ks) ||
|
---|
496 | IsFunctionKey (ks) ||
|
---|
497 | IsMiscFunctionKey (ks)
|
---|
498 | );
|
---|
499 | #elif defined(Q_WS_MAC)
|
---|
500 | UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask (aKeyVal);
|
---|
501 | switch (modMask)
|
---|
502 | {
|
---|
503 | case shiftKey:
|
---|
504 | case optionKey:
|
---|
505 | case controlKey:
|
---|
506 | case rightShiftKey:
|
---|
507 | case rightOptionKey:
|
---|
508 | case rightControlKey:
|
---|
509 | case cmdKey:
|
---|
510 | case kEventKeyModifierRightCmdKeyMask:
|
---|
511 | return true;
|
---|
512 | default:
|
---|
513 | return false;
|
---|
514 | }
|
---|
515 | #else
|
---|
516 | Q_UNUSED (aKeyVal);
|
---|
517 | return true;
|
---|
518 | #endif
|
---|
519 | }
|
---|
520 |
|
---|
521 | // Public slots
|
---|
522 | /////////////////////////////////////////////////////////////////////////////
|
---|
523 |
|
---|
524 | void QIHotKeyEdit::clear()
|
---|
525 | {
|
---|
526 | mKeyVal = 0;
|
---|
527 | mSymbName = tr (kNoneSymbName);
|
---|
528 | updateText();
|
---|
529 | }
|
---|
530 |
|
---|
531 | // Protected members
|
---|
532 | /////////////////////////////////////////////////////////////////////////////
|
---|
533 |
|
---|
534 | // Protected events
|
---|
535 | /////////////////////////////////////////////////////////////////////////////
|
---|
536 |
|
---|
537 | #if defined (Q_WS_WIN32)
|
---|
538 |
|
---|
539 | bool QIHotKeyEdit::winEvent (MSG *msg)
|
---|
540 | {
|
---|
541 | if (!(msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN ||
|
---|
542 | msg->message == WM_KEYUP || msg->message == WM_SYSKEYUP ||
|
---|
543 | msg->message == WM_CHAR || msg->message == WM_SYSCHAR ||
|
---|
544 | msg->message == WM_DEADCHAR || msg->message == WM_SYSDEADCHAR ||
|
---|
545 | msg->message == WM_CONTEXTMENU))
|
---|
546 | return false;
|
---|
547 |
|
---|
548 | /* ignore if not a valid hot key */
|
---|
549 | if (!isValidKey (msg->wParam))
|
---|
550 | return false;
|
---|
551 |
|
---|
552 | #if 0
|
---|
553 | LogFlow (("%WM_%04X: vk=%04X rep=%05d scan=%02X ext=%01d"
|
---|
554 | "rzv=%01X ctx=%01d prev=%01d tran=%01d\n",
|
---|
555 | msg->message, msg->wParam,
|
---|
556 | (msg->lParam & 0xFFFF),
|
---|
557 | ((msg->lParam >> 16) & 0xFF),
|
---|
558 | ((msg->lParam >> 24) & 0x1),
|
---|
559 | ((msg->lParam >> 25) & 0xF),
|
---|
560 | ((msg->lParam >> 29) & 0x1),
|
---|
561 | ((msg->lParam >> 30) & 0x1),
|
---|
562 | ((msg->lParam >> 31) & 0x1)));
|
---|
563 | #endif
|
---|
564 |
|
---|
565 | if (msg->message == WM_KEYDOWN || msg->message == WM_SYSKEYDOWN)
|
---|
566 | {
|
---|
567 | /* determine platform-dependent key */
|
---|
568 | mKeyVal = qi_distinguish_modifier_vkey (msg->wParam);
|
---|
569 | /* determine symbolic name */
|
---|
570 | TCHAR *str = new TCHAR [256];
|
---|
571 | if (::GetKeyNameText (msg->lParam, str, 256))
|
---|
572 | {
|
---|
573 | mSymbName = QString::fromUcs2 (str);
|
---|
574 | }
|
---|
575 | else
|
---|
576 | {
|
---|
577 | AssertFailed();
|
---|
578 | mSymbName = QString (tr ("<key_%1>")).arg (mKeyVal);
|
---|
579 | }
|
---|
580 | delete[] str;
|
---|
581 | /* update the display */
|
---|
582 | updateText();
|
---|
583 | }
|
---|
584 |
|
---|
585 | return true;
|
---|
586 | }
|
---|
587 |
|
---|
588 | #elif defined (Q_WS_PM)
|
---|
589 |
|
---|
590 | bool QIHotKeyEdit::pmEvent (QMSG *aMsg)
|
---|
591 | {
|
---|
592 | if (aMsg->msg != WM_CHAR)
|
---|
593 | return false;
|
---|
594 |
|
---|
595 | USHORT f = SHORT1FROMMP (aMsg->mp1);
|
---|
596 |
|
---|
597 | int vkey = QIHotKeyEdit::virtualKey (aMsg);
|
---|
598 |
|
---|
599 | /* ignore if not a valid hot key */
|
---|
600 | if (!isValidKey (vkey))
|
---|
601 | return false;
|
---|
602 |
|
---|
603 | if (!(f & KC_KEYUP))
|
---|
604 | {
|
---|
605 | /* determine platform-dependent key */
|
---|
606 | mKeyVal = vkey;
|
---|
607 | /* determine symbolic name */
|
---|
608 | mSymbName = QIHotKeyEdit::keyName (mKeyVal);
|
---|
609 | /* update the display */
|
---|
610 | updateText();
|
---|
611 | }
|
---|
612 |
|
---|
613 | return true;
|
---|
614 | }
|
---|
615 |
|
---|
616 | #elif defined (Q_WS_X11)
|
---|
617 |
|
---|
618 | bool QIHotKeyEdit::x11Event (XEvent *event)
|
---|
619 | {
|
---|
620 | switch (event->type)
|
---|
621 | {
|
---|
622 | case XKeyPress:
|
---|
623 | case XKeyRelease:
|
---|
624 | {
|
---|
625 | XKeyEvent *ke = (XKeyEvent *) event;
|
---|
626 | KeySym ks = ::XKeycodeToKeysym (ke->display, ke->keycode, 0);
|
---|
627 | /* ignore if not a valid hot key */
|
---|
628 | if (!isValidKey ((int) ks))
|
---|
629 | return false;
|
---|
630 |
|
---|
631 | /* skip key releases */
|
---|
632 | if (event->type == XKeyRelease)
|
---|
633 | return true;
|
---|
634 |
|
---|
635 | /* determine platform-dependent key */
|
---|
636 | mKeyVal = (int) ks;
|
---|
637 | /* determine symbolic name */
|
---|
638 | mSymbName = QIHotKeyEdit::keyName (mKeyVal);
|
---|
639 | /* update the display */
|
---|
640 | updateText();
|
---|
641 | #if 0
|
---|
642 | LogFlow (("%s: state=%08X keycode=%08X keysym=%08X symb=%s\n",
|
---|
643 | event->type == XKeyPress ? "XKeyPress" : "XKeyRelease",
|
---|
644 | ke->state, ke->keycode, ks,
|
---|
645 | symbname.latin1()));
|
---|
646 | #endif
|
---|
647 | return true;
|
---|
648 | }
|
---|
649 | }
|
---|
650 |
|
---|
651 | return false;
|
---|
652 | }
|
---|
653 |
|
---|
654 | #elif defined (Q_WS_MAC)
|
---|
655 |
|
---|
656 | /* static */
|
---|
657 | pascal OSStatus QIHotKeyEdit::darwinEventHandlerProc (EventHandlerCallRef inHandlerCallRef,
|
---|
658 | EventRef inEvent, void *inUserData)
|
---|
659 | {
|
---|
660 | QIHotKeyEdit *edit = (QIHotKeyEdit *) inUserData;
|
---|
661 | UInt32 EventClass = ::GetEventClass (inEvent);
|
---|
662 | if (EventClass == kEventClassKeyboard)
|
---|
663 | {
|
---|
664 | if (edit->darwinKeyboardEvent (inEvent))
|
---|
665 | return 0;
|
---|
666 | }
|
---|
667 | return CallNextEventHandler (inHandlerCallRef, inEvent);
|
---|
668 | }
|
---|
669 |
|
---|
670 | bool QIHotKeyEdit::darwinKeyboardEvent (EventRef inEvent)
|
---|
671 | {
|
---|
672 | /* ignore key changes unless we're the focus widget */
|
---|
673 | if (!hasFocus())
|
---|
674 | return false;
|
---|
675 |
|
---|
676 | UInt32 eventKind = ::GetEventKind (inEvent);
|
---|
677 | switch (eventKind)
|
---|
678 | {
|
---|
679 | /*case kEventRawKeyDown:
|
---|
680 | case kEventRawKeyUp:
|
---|
681 | case kEventRawKeyRepeat:*/
|
---|
682 | case kEventRawKeyModifiersChanged:
|
---|
683 | {
|
---|
684 | UInt32 modifierMask = 0;
|
---|
685 | ::GetEventParameter (inEvent, kEventParamKeyModifiers, typeUInt32, NULL,
|
---|
686 | sizeof (modifierMask), NULL, &modifierMask);
|
---|
687 |
|
---|
688 | modifierMask = ::DarwinAdjustModifierMask (modifierMask);
|
---|
689 | UInt32 changed = mDarwinKeyModifiers ^ modifierMask;
|
---|
690 | mDarwinKeyModifiers = modifierMask;
|
---|
691 |
|
---|
692 | /* skip key releases */
|
---|
693 | if (changed && (changed & modifierMask))
|
---|
694 | break;
|
---|
695 |
|
---|
696 | /* convert to keycode and skip keycodes we don't care about. */
|
---|
697 | unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode (changed);
|
---|
698 | if (!keyCode || keyCode == ~0U || !isValidKey (keyCode))
|
---|
699 | break;
|
---|
700 |
|
---|
701 | /* update key current key. */
|
---|
702 | mKeyVal = keyCode;
|
---|
703 | mSymbName = QIHotKeyEdit::keyName (keyCode);
|
---|
704 | updateText();
|
---|
705 | break; //return true;
|
---|
706 | }
|
---|
707 | break;
|
---|
708 | }
|
---|
709 | return false;
|
---|
710 | }
|
---|
711 |
|
---|
712 | #else
|
---|
713 | # warning "Port me!"
|
---|
714 | #endif
|
---|
715 |
|
---|
716 | void QIHotKeyEdit::focusInEvent (QFocusEvent *)
|
---|
717 | {
|
---|
718 | QPalette p = palette();
|
---|
719 | p.setActive (mTrueACG);
|
---|
720 | setPalette (p);
|
---|
721 | }
|
---|
722 |
|
---|
723 | void QIHotKeyEdit::focusOutEvent (QFocusEvent *)
|
---|
724 | {
|
---|
725 | QPalette p = palette();
|
---|
726 | p.setActive (p.inactive());
|
---|
727 | setPalette (p);
|
---|
728 | }
|
---|
729 |
|
---|
730 | void QIHotKeyEdit::drawContents (QPainter * p)
|
---|
731 | {
|
---|
732 | QLabel::drawContents (p);
|
---|
733 | if (hasFocus())
|
---|
734 | {
|
---|
735 | style().drawPrimitive (
|
---|
736 | QStyle::PE_FocusRect, p, contentsRect(), colorGroup(),
|
---|
737 | QStyle::Style_Default,
|
---|
738 | QStyleOption( colorGroup().background()));
|
---|
739 | }
|
---|
740 | }
|
---|
741 |
|
---|
742 | // Private members
|
---|
743 | /////////////////////////////////////////////////////////////////////////////
|
---|
744 |
|
---|
745 | void QIHotKeyEdit::updateText()
|
---|
746 | {
|
---|
747 | setText (QString (" %1 ").arg (mSymbName));
|
---|
748 | }
|
---|