Changeset 35580 in vbox
- Timestamp:
- Jan 17, 2011 12:09:32 PM (14 years ago)
- Location:
- trunk/src/VBox/Frontends/VirtualBox/src/extensions
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIHotKeyEdit.cpp
r33540 r35580 7 7 8 8 /* 9 * Copyright (C) 2006-20 07Oracle Corporation9 * Copyright (C) 2006-2011 Oracle Corporation 10 10 * 11 11 * This file is part of VirtualBox Open Source Edition (OSE), as … … 18 18 */ 19 19 20 /* Local includes */ 20 21 #include "QIHotKeyEdit.h" 21 22 #include "VBoxDefs.h" 22 23 #include "VBoxGlobal.h" 23 24 24 /* Qtincludes */25 /* Global includes */ 25 26 #include <QApplication> 26 27 #include <QStyleOption> … … 29 30 #ifdef Q_WS_WIN 30 31 /* VBox/cdefs.h defines these: */ 31 #undef LOWORD 32 #undef HIWORD 33 #undef LOBYTE 34 #undef HIBYTE 35 #include <windows.h> 36 #endif 37 38 #if defined (Q_WS_PM) 39 QMap<int, QString> QIHotKeyEdit::sKeyNames; 40 #endif 32 # undef LOWORD 33 # undef HIWORD 34 # undef LOBYTE 35 # undef HIBYTE 36 # include <windows.h> 37 #endif /* Q_WS_WIN */ 41 38 42 39 #ifdef Q_WS_X11 … … 46 43 * conflicts with Qt. Therefore we use the following hack 47 44 * to redefine those conflicting identifiers. */ 48 # define XK_XKB_KEYS49 # define XK_MISCELLANY50 # include <X11/Xlib.h>51 # include <X11/Xutil.h>52 # include <X11/keysymdef.h>53 # ifdef KeyPress45 # define XK_XKB_KEYS 46 # define XK_MISCELLANY 47 # include <X11/Xlib.h> 48 # include <X11/Xutil.h> 49 # include <X11/keysymdef.h> 50 # ifdef KeyPress 54 51 const int XFocusOut = FocusOut; 55 52 const int XFocusIn = FocusIn; 56 53 const int XKeyPress = KeyPress; 57 54 const int XKeyRelease = KeyRelease; 58 # undef KeyRelease59 # undef KeyPress60 # undef FocusOut61 # undef FocusIn62 # endif63 # include "XKeyboard.h"64 QMap<QString, QString> QIHotKeyEdit::s KeyNames;65 # include <QX11Info>66 #endif 55 # undef KeyRelease 56 # undef KeyPress 57 # undef FocusOut 58 # undef FocusIn 59 # endif /* KeyPress */ 60 # include "XKeyboard.h" 61 QMap<QString, QString> QIHotKeyEdit::s_keyNames; 62 # include <QX11Info> 63 #endif /* Q_WS_X11 */ 67 64 68 65 #ifdef Q_WS_MAC … … 74 71 75 72 76 #if defined (Q_WS_WIN32) 77 /** 78 * Returns the correct modifier vkey for the *last* keyboard message, 79 * distinguishing between left and right keys. If both are pressed 80 * the left key wins. If the pressed key not a modifier, wParam is returned 81 * unchanged. 82 */ 83 int qi_distinguish_modifier_vkey (WPARAM wParam) 73 #ifdef Q_WS_WIN 74 /* Returns the correct modifier vkey for the *last* keyboard message, 75 * distinguishing between left and right keys. If both are pressed 76 * the left key wins. If the pressed key not a modifier, wParam is returned 77 * unchanged. */ 78 int qi_distinguish_modifier_vkey(WPARAM wParam) 84 79 { 85 80 int keyval = wParam; … … 87 82 { 88 83 case VK_SHIFT: 89 if (::GetKeyState 90 else if (::GetKeyState 84 if (::GetKeyState(VK_LSHIFT) & 0x8000) keyval = VK_LSHIFT; 85 else if (::GetKeyState(VK_RSHIFT) & 0x8000) keyval = VK_RSHIFT; 91 86 break; 92 87 case VK_CONTROL: 93 if (::GetKeyState 94 else if (::GetKeyState 88 if (::GetKeyState(VK_LCONTROL) & 0x8000) keyval = VK_LCONTROL; 89 else if (::GetKeyState(VK_RCONTROL) & 0x8000) keyval = VK_RCONTROL; 95 90 break; 96 91 case VK_MENU: 97 if (::GetKeyState 98 else if (::GetKeyState 92 if (::GetKeyState(VK_LMENU) & 0x8000) keyval = VK_LMENU; 93 else if (::GetKeyState(VK_RMENU) & 0x8000) keyval = VK_RMENU; 99 94 break; 100 95 } 101 96 return keyval; 102 97 } 103 #endif 104 105 /** @class QIHotKeyEdit 106 * 107 * The QIHotKeyEdit widget is a hot key editor. 108 */ 109 110 const char *QIHotKeyEdit::kNoneSymbName = "None"; 111 112 QIHotKeyEdit::QIHotKeyEdit (QWidget *aParent) : 113 QLabel (aParent) 98 #endif /* Q_WS_WIN */ 99 100 101 const char *QIHotKeyEdit::m_spNoneSymbName = "None"; 102 103 QIHotKeyEdit::QIHotKeyEdit(QWidget *pParent) 104 : QLabel(pParent) 114 105 { 115 106 #ifdef Q_WS_X11 116 /* Initialize the X keyboard subsystem */ 117 initMappedX11Keyboard(QX11Info::display(), 118 vboxGlobal().settings().publicProperty ("GUI/RemapScancodes")); 119 #endif 107 /* Initialize the X keyboard subsystem: */ 108 initMappedX11Keyboard(QX11Info::display(), vboxGlobal().settings().publicProperty("GUI/RemapScancodes")); 109 #endif /* Q_WS_X11 */ 120 110 121 111 clear(); 122 112 123 #if defined (Q_WS_WIN32) 124 /* Qt documentation hasn't mentioned this is 125 * windows-only flag, but looks like that is so, 126 * anyway it is required for winEvent() handler only */ 127 setAttribute (Qt::WA_NativeWindow); 128 #endif 129 setFrameStyle (QFrame::StyledPanel | Sunken); 130 setAlignment (Qt::AlignCenter); 131 setFocusPolicy (Qt::StrongFocus); 132 setAutoFillBackground (true); 113 #ifdef Q_WS_WIN 114 /* Qt documentation hasn't mentioned this is windows-only flag, 115 * but looks like that is so, anyway it is required for winEvent() handler only: */ 116 setAttribute(Qt::WA_NativeWindow); 117 #endif /* Q_WS_WIN */ 118 setFrameStyle(QFrame::StyledPanel | Sunken); 119 setAlignment(Qt::AlignCenter); 120 setFocusPolicy(Qt::StrongFocus); 121 setAutoFillBackground(true); 133 122 134 123 QPalette p = palette(); 135 p.setColor (QPalette::Active, QPalette::Foreground, 136 p.color (QPalette::Active, QPalette::Text)); 137 p.setColor (QPalette::Active, QPalette::Background, 138 p.color (QPalette::Active, QPalette::Base)); 139 setPalette (p); 124 p.setColor(QPalette::Active, QPalette::Foreground, p.color(QPalette::Active, QPalette::Text)); 125 p.setColor(QPalette::Active, QPalette::Background, p.color(QPalette::Active, QPalette::Base)); 126 setPalette(p); 140 127 141 128 #ifdef Q_WS_MAC 142 m DarwinKeyModifiers = GetCurrentEventKeyModifiers();129 m_uDarwinKeyModifiers = GetCurrentEventKeyModifiers(); 143 130 UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown | NSKeyUp | | NSFlagsChanged */, QIHotKeyEdit::darwinEventHandlerProc, this); 144 ::DarwinGrabKeyboard 145 #endif 131 ::DarwinGrabKeyboard(false /* just modifiers */); 132 #endif /* Q_WS_MAC */ 146 133 } 147 134 … … 153 140 #endif 154 141 } 155 156 // Public members157 /////////////////////////////////////////////////////////////////////////////158 142 159 143 /** … … 165 149 * to the keycode. 166 150 */ 167 void QIHotKeyEdit::setKey (int aKeyVal)168 { 169 m KeyVal = aKeyVal;170 m SymbName = QIHotKeyEdit::keyName (aKeyVal);151 void QIHotKeyEdit::setKey(int iKeyVal) 152 { 153 m_iKeyVal = iKeyVal; 154 m_strSymbName = QIHotKeyEdit::keyName(iKeyVal); 171 155 updateText(); 172 156 } … … 183 167 */ 184 168 185 /**186 * Stolen from QLineEdit.187 */188 169 QSize QIHotKeyEdit::sizeHint() const 189 170 { 190 171 ensurePolished(); 191 QFontMetrics fm 192 int h = qMax 193 int w = fm.width ('x') * 17; // "some"172 QFontMetrics fm(font()); 173 int h = qMax(fm.lineSpacing(), 14) + 2; 174 int w = fm.width('x') * 17; 194 175 int m = frameWidth() * 2; 195 176 QStyleOption option; 196 option.initFrom (this); 197 return (style()->sizeFromContents (QStyle::CT_LineEdit, &option, 198 QSize (w + m, h + m) 199 .expandedTo (QApplication::globalStrut()), 200 this)); 201 } 202 203 /** 204 * Stolen from QLineEdit. 205 */ 177 option.initFrom(this); 178 return (style()->sizeFromContents(QStyle::CT_LineEdit, &option, 179 QSize(w + m, h + m) 180 .expandedTo(QApplication::globalStrut()), 181 this)); 182 } 183 206 184 QSize QIHotKeyEdit::minimumSizeHint() const 207 185 { 208 186 ensurePolished(); 209 187 QFontMetrics fm = fontMetrics(); 210 int h = fm.height() + qMax 188 int h = fm.height() + qMax(2, fm.leading()); 211 189 int w = fm.maxWidth(); 212 190 int m = frameWidth() * 2; 213 return QSize (w + m, h + m); 214 } 215 216 #if defined (Q_WS_PM) 217 /** 218 * Returns the virtual key extracted from the QMSG structure. 219 * 220 * This function tries to detect some extra virtual keys definitions missing 221 * in PM (like Left Shift, Left Ctrl, Win keys). In all other cases it simply 222 * returns SHORT2FROMMP (aMsg->mp2). 223 * 224 * @param aMsg Pointer to the QMSG structure to extract the virtual key from. 225 * @return The extracted virtual key code or zero if there is no virtual key. 226 */ 227 /* static */ 228 int QIHotKeyEdit::virtualKey (QMSG *aMsg) 229 { 230 USHORT f = SHORT1FROMMP (aMsg->mp1); 231 CHAR scan = CHAR4FROMMP (aMsg->mp1); 232 USHORT ch = SHORT1FROMMP (aMsg->mp2); 233 int vkey = (unsigned int) SHORT2FROMMP (aMsg->mp2); 234 235 if (f & KC_VIRTUALKEY) 236 { 237 /* distinguish Left Shift from Right Shift) */ 238 if (vkey == VK_SHIFT && scan == 0x2A) 239 vkey = VK_LSHIFT; 240 /* distinguish Left Ctrl from Right Ctrl */ 241 else if (vkey == VK_CTRL && scan == 0x1D) 242 vkey = VK_LCTRL; 243 /* distinguish Ctrl+ScrLock from Ctrl+Break */ 244 else if (vkey == VK_BREAK && scan == 0x46 && f & KC_CTRL) 245 vkey = VK_SCRLLOCK; 246 } 247 else if (!(f & KC_CHAR)) 248 { 249 /* detect some special keys that have a pseudo char code in the high 250 * byte of ch (probably this is less device-dependent than 251 * scancode) */ 252 switch (ch) 253 { 254 case 0xEC00: vkey = VK_LWIN; break; 255 case 0xED00: vkey = VK_RWIN; break; 256 case 0xEE00: vkey = VK_WINMENU; break; 257 case 0xF900: vkey = VK_FORWARD; break; 258 case 0xFA00: vkey = VK_BACKWARD; break; 259 default: vkey = 0; 260 } 261 } 262 263 return vkey; 264 } 265 #endif 266 267 #if defined (Q_WS_PM) 268 /** 269 * Updates the associative array containing the translations of PM virtual 270 * keys to human readable key names. 271 */ 272 void QIHotKeyEdit::retranslateUi() 273 { 274 /* Note: strings for the same key must match strings in retranslateUi() 275 * versions for all platforms, to keep translators happy. */ 276 277 sKeyNames [VK_LSHIFT] = tr ("Left Shift"); 278 sKeyNames [VK_SHIFT] = tr ("Right Shift"); 279 sKeyNames [VK_LCTRL] = tr ("Left Ctrl"); 280 sKeyNames [VK_CTRL] = tr ("Right Ctrl"); 281 sKeyNames [VK_ALT] = tr ("Left Alt"); 282 sKeyNames [VK_ALTGRAF] = tr ("Right Alt"); 283 sKeyNames [VK_LWIN] = tr ("Left WinKey"); 284 sKeyNames [VK_RWIN] = tr ("Right WinKey"); 285 sKeyNames [VK_WINMENU] = tr ("Menu key"); 286 sKeyNames [VK_CAPSLOCK] = tr ("Caps Lock"); 287 sKeyNames [VK_SCRLLOCK] = tr ("Scroll Lock"); 288 289 sKeyNames [VK_PAUSE] = tr ("Pause"); 290 sKeyNames [VK_PRINTSCRN] = tr ("Print Screen"); 291 292 sKeyNames [VK_F1] = tr ("F1"); 293 sKeyNames [VK_F2] = tr ("F2"); 294 sKeyNames [VK_F3] = tr ("F3"); 295 sKeyNames [VK_F4] = tr ("F4"); 296 sKeyNames [VK_F5] = tr ("F5"); 297 sKeyNames [VK_F6] = tr ("F6"); 298 sKeyNames [VK_F7] = tr ("F7"); 299 sKeyNames [VK_F8] = tr ("F8"); 300 sKeyNames [VK_F9] = tr ("F9"); 301 sKeyNames [VK_F10] = tr ("F10"); 302 sKeyNames [VK_F11] = tr ("F11"); 303 sKeyNames [VK_F12] = tr ("F12"); 304 sKeyNames [VK_F13] = tr ("F13"); 305 sKeyNames [VK_F14] = tr ("F14"); 306 sKeyNames [VK_F15] = tr ("F15"); 307 sKeyNames [VK_F16] = tr ("F16"); 308 sKeyNames [VK_F17] = tr ("F17"); 309 sKeyNames [VK_F18] = tr ("F18"); 310 sKeyNames [VK_F19] = tr ("F19"); 311 sKeyNames [VK_F20] = tr ("F20"); 312 sKeyNames [VK_F21] = tr ("F21"); 313 sKeyNames [VK_F22] = tr ("F22"); 314 sKeyNames [VK_F23] = tr ("F23"); 315 sKeyNames [VK_F24] = tr ("F24"); 316 317 sKeyNames [VK_NUMLOCK] = tr ("Num Lock"); 318 sKeyNames [VK_FORWARD] = tr ("Forward"); 319 sKeyNames [VK_BACKWARD] = tr ("Back"); 320 } 321 #elif defined (Q_WS_X11) 322 /** 323 * Updates the associative array containing the translations of X11 key strings to human 324 * readable key names. 325 */ 326 void QIHotKeyEdit::retranslateUi() 327 { 328 /* Note: strings for the same key must match strings in retranslateUi() 329 * versions for all platforms, to keep translators happy. */ 330 331 sKeyNames ["Shift_L"] = tr ("Left Shift"); 332 sKeyNames ["Shift_R"] = tr ("Right Shift"); 333 sKeyNames ["Control_L"] = tr ("Left Ctrl"); 334 sKeyNames ["Control_R"] = tr ("Right Ctrl"); 335 sKeyNames ["Alt_L"] = tr ("Left Alt"); 336 sKeyNames ["Alt_R"] = tr ("Right Alt"); 337 sKeyNames ["Super_L"] = tr ("Left WinKey"); 338 sKeyNames ["Super_R"] = tr ("Right WinKey"); 339 sKeyNames ["Menu"] = tr ("Menu key"); 340 sKeyNames ["ISO_Level3_Shift"] = tr ("Alt Gr"); 341 sKeyNames ["Caps_Lock"] = tr ("Caps Lock"); 342 sKeyNames ["Scroll_Lock"] = tr ("Scroll Lock"); 343 } 344 #endif 191 return QSize(w + m, h + m); 192 } 345 193 346 194 /** … … 353 201 */ 354 202 /* static */ 355 QString QIHotKeyEdit::keyName (int aKeyVal)356 { 357 QString name;358 359 if (! aKeyVal)360 { 361 name = tr (kNoneSymbName);203 QString QIHotKeyEdit::keyName(int iKeyVal) 204 { 205 QString strName; 206 207 if (!iKeyVal) 208 { 209 strName = tr(m_spNoneSymbName); 362 210 } 363 211 else 364 212 { 365 #if defined (Q_WS_WIN 32)213 #if defined (Q_WS_WIN) 366 214 /* Stupid MapVirtualKey doesn't distinguish between right and left 367 215 * vkeys, even under XP, despite that it stated in msdn. Do it by … … 369 217 * VK_DIVIDE & VK_PAUSE, this is also known bug. */ 370 218 int scan; 371 switch ( aKeyVal)219 switch (iKeyVal) 372 220 { 373 221 /* Processing special keys... */ … … 380 228 case VK_LWIN: 381 229 case VK_RWIN: 382 case VK_NUMLOCK: scan = (::MapVirtualKey (aKeyVal, 0) | 256) << 16; break;383 default: scan = ::MapVirtualKey (aKeyVal, 0) << 16;384 } 385 TCHAR *str = new TCHAR 386 if (::GetKeyNameText 387 { 388 name = QString::fromUtf16(str);230 case VK_NUMLOCK: scan = (::MapVirtualKey(iKeyVal, 0) | 256) << 16; break; 231 default: scan = ::MapVirtualKey(iKeyVal, 0) << 16; 232 } 233 TCHAR *str = new TCHAR[256]; 234 if (::GetKeyNameText(scan, str, 256)) 235 { 236 strName = QString::fromUtf16(str); 389 237 } 390 238 else 391 239 { 392 240 AssertFailed(); 393 name = QString (tr ("<key_%1>")).arg (aKeyVal);241 strName = QString(tr("<key_%1>")).arg(iKeyVal); 394 242 } 395 243 delete[] str; 396 #elif defined (Q_WS_PM) 397 name = sKeyNames [aKeyVal]; 398 if (name.isNull()) 244 #elif defined (Q_WS_X11) 245 char *sn = ::XKeysymToString((KeySym)iKeyVal); 246 if (sn) 247 { 248 strName = s_keyNames[sn]; 249 if (strName.isEmpty()) 250 strName = sn; 251 } 252 else 399 253 { 400 254 AssertFailed(); 401 name = QString (tr ("<key_%1>")).arg (aKeyVal); 402 } 403 #elif defined (Q_WS_X11) 404 char *sn = ::XKeysymToString ((KeySym) aKeyVal); 405 if (sn) 406 { 407 name = sKeyNames [sn]; 408 if (name.isEmpty()) 409 name = sn; 410 } 411 else 412 { 413 AssertFailed(); 414 name = QString (tr ("<key_%1>")).arg (aKeyVal); 255 strName = QString(tr("<key_%1>")).arg(iKeyVal); 415 256 } 416 257 #elif defined(Q_WS_MAC) 417 UInt32 modMask = DarwinKeyCodeToDarwinModifierMask (aKeyVal);258 UInt32 modMask = DarwinKeyCodeToDarwinModifierMask(iKeyVal); 418 259 switch (modMask) 419 260 { … … 422 263 case controlKey: 423 264 case cmdKey: 424 name = tr("Left ");265 strName = tr("Left "); 425 266 break; 426 267 case rightShiftKey: … … 428 269 case rightControlKey: 429 270 case kEventKeyModifierRightCmdKeyMask: 430 name = tr("Right ");271 strName = tr("Right "); 431 272 break; 432 273 default: 433 AssertMsgFailedReturn 274 AssertMsgFailedReturn(("modMask=%#x\n", modMask), QString()); 434 275 } 435 276 switch (modMask) … … 437 278 case shiftKey: 438 279 case rightShiftKey: 439 name += QChar(kShiftUnicode);280 strName += QChar(kShiftUnicode); 440 281 break; 441 282 case optionKey: 442 283 case rightOptionKey: 443 name += QChar(kOptionUnicode);284 strName += QChar(kOptionUnicode); 444 285 break; 445 286 case controlKey: 446 287 case rightControlKey: 447 name += QChar(kControlUnicode);288 strName += QChar(kControlUnicode); 448 289 break; 449 290 case cmdKey: 450 291 case kEventKeyModifierRightCmdKeyMask: 451 name += QChar(kCommandUnicode);292 strName += QChar(kCommandUnicode); 452 293 break; 453 294 } 454 295 #else 455 296 AssertFailed(); 456 name = QString (tr ("<key_%1>")).arg (aKeyVal);457 #endif 458 } 459 460 return name;297 strName = QString(tr("<key_%1>")).arg(iKeyVal); 298 #endif 299 } 300 301 return strName; 461 302 } 462 303 463 304 /* static */ 464 bool QIHotKeyEdit::isValidKey (int aKeyVal)305 bool QIHotKeyEdit::isValidKey(int iKeyVal) 465 306 { 466 307 /* Empty value is correct: */ 467 if ( aKeyVal == 0)308 if (iKeyVal == 0) 468 309 return true; 469 #if defined(Q_WS_WIN32) 470 return ( 471 (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPITAL) || 472 aKeyVal == VK_PRINT || 473 aKeyVal == VK_LWIN || aKeyVal == VK_RWIN || 474 aKeyVal == VK_APPS || 475 (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) || 476 aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCROLL || 477 (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_RMENU)); 478 #elif defined(Q_WS_PM) 479 return ( 480 (aKeyVal >= VK_SHIFT && aKeyVal <= VK_CAPSLOCK) || 481 aKeyVal == VK_PRINTSCRN || 482 (aKeyVal >= VK_F1 && aKeyVal <= VK_F24) || 483 aKeyVal == VK_NUMLOCK || aKeyVal == VK_SCRLLOCK || 484 (aKeyVal >= VK_LSHIFT && aKeyVal <= VK_BACKWARD)); 310 #if defined(Q_WS_WIN) 311 return ((iKeyVal >= VK_SHIFT && iKeyVal <= VK_CAPITAL) || 312 iKeyVal == VK_PRINT || 313 iKeyVal == VK_LWIN || iKeyVal == VK_RWIN || 314 iKeyVal == VK_APPS || 315 (iKeyVal >= VK_F1 && iKeyVal <= VK_F24) || 316 iKeyVal == VK_NUMLOCK || iKeyVal == VK_SCROLL || 317 (iKeyVal >= VK_LSHIFT && iKeyVal <= VK_RMENU)); 485 318 #elif defined(Q_WS_X11) 486 KeySym ks = (KeySym) aKeyVal; 487 return 488 ( 489 ks != NoSymbol && 490 ks != XK_Insert 491 ) && ( 492 ks == XK_Scroll_Lock || 493 IsModifierKey (ks) || 494 IsFunctionKey (ks) || 495 IsMiscFunctionKey (ks) 496 ); 319 KeySym ks = (KeySym)iKeyVal; 320 return (ks != NoSymbol && ks != XK_Insert) && 321 (ks == XK_Scroll_Lock || IsModifierKey(ks) || IsFunctionKey(ks) || IsMiscFunctionKey(ks)); 497 322 #elif defined(Q_WS_MAC) 498 UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask (aKeyVal);323 UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask(iKeyVal); 499 324 switch (modMask) 500 325 { … … 512 337 } 513 338 #else 514 Q_UNUSED (aKeyVal);339 Q_UNUSED(iKeyVal); 515 340 return true; 516 341 #endif 517 342 } 518 343 519 // Public slots 520 ///////////////////////////////////////////////////////////////////////////// 344 #ifdef Q_WS_X11 345 /* Updates the associative array containing the translations 346 * of X11 key strings to human readable key names. */ 347 void QIHotKeyEdit::retranslateUi() 348 { 349 /* Note: strings for the same key must match strings in retranslateUi() 350 * versions for all platforms, to keep translators happy. */ 351 352 s_keyNames["Shift_L"] = tr("Left Shift"); 353 s_keyNames["Shift_R"] = tr("Right Shift"); 354 s_keyNames["Control_L"] = tr("Left Ctrl"); 355 s_keyNames["Control_R"] = tr("Right Ctrl"); 356 s_keyNames["Alt_L"] = tr("Left Alt"); 357 s_keyNames["Alt_R"] = tr("Right Alt"); 358 s_keyNames["Super_L"] = tr("Left WinKey"); 359 s_keyNames["Super_R"] = tr("Right WinKey"); 360 s_keyNames["Menu"] = tr("Menu key"); 361 s_keyNames["ISO_Level3_Shift"] = tr("Alt Gr"); 362 s_keyNames["Caps_Lock"] = tr("Caps Lock"); 363 s_keyNames["Scroll_Lock"] = tr("Scroll Lock"); 364 } 365 #endif /* Q_WS_X11 */ 521 366 522 367 void QIHotKeyEdit::clear() 523 368 { 524 m KeyVal = 0;525 m SymbName = tr (kNoneSymbName);369 m_iKeyVal = 0; 370 m_strSymbName = tr(m_spNoneSymbName); 526 371 updateText(); 527 372 } 528 373 529 // Protected members 530 ///////////////////////////////////////////////////////////////////////////// 531 532 // Protected events 533 ///////////////////////////////////////////////////////////////////////////// 534 535 #if defined (Q_WS_WIN32) 536 537 bool QIHotKeyEdit::winEvent (MSG *aMsg, long* /* aResult */) 538 { 539 if (!(aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN || 540 aMsg->message == WM_KEYUP || aMsg->message == WM_SYSKEYUP || 541 aMsg->message == WM_CHAR || aMsg->message == WM_SYSCHAR || 542 aMsg->message == WM_DEADCHAR || aMsg->message == WM_SYSDEADCHAR || 543 aMsg->message == WM_CONTEXTMENU)) 374 #if defined (Q_WS_WIN) 375 376 bool QIHotKeyEdit::winEvent(MSG *pMsg, long* /* pResult */) 377 { 378 if (!(pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN || 379 pMsg->message == WM_KEYUP || pMsg->message == WM_SYSKEYUP || 380 pMsg->message == WM_CHAR || pMsg->message == WM_SYSCHAR || 381 pMsg->message == WM_DEADCHAR || pMsg->message == WM_SYSDEADCHAR || 382 pMsg->message == WM_CONTEXTMENU)) 544 383 return false; 545 384 546 /* ignore if not a valid hot key*/547 if (!isValidKey (aMsg->wParam))385 /* Ignore if not a valid hot key: */ 386 if (!isValidKey(pMsg->wParam)) 548 387 return false; 549 388 550 389 #if 0 551 LogFlow 552 553 aMsg->message, aMsg->wParam,554 (aMsg->lParam & 0xFFFF),555 ((aMsg->lParam >> 16) & 0xFF),556 ((aMsg->lParam >> 24) & 0x1),557 ((aMsg->lParam >> 25) & 0xF),558 ((aMsg->lParam >> 29) & 0x1),559 ((aMsg->lParam >> 30) & 0x1),560 ((aMsg->lParam >> 31) & 0x1)));561 #endif 562 563 if ( aMsg->message == WM_KEYDOWN || aMsg->message == WM_SYSKEYDOWN)564 { 565 /* determine platform-dependent key*/566 m KeyVal = qi_distinguish_modifier_vkey (aMsg->wParam);567 /* determine symbolic name*/568 TCHAR * str = new TCHAR[256];569 if (::GetKeyNameText (aMsg->lParam, str, 256))570 { 571 m SymbName = QString::fromUtf16 (str);390 LogFlow(("%WM_%04X: vk=%04X rep=%05d scan=%02X ext=%01d" 391 "rzv=%01X ctx=%01d prev=%01d tran=%01d\n", 392 pMsg->message, pMsg->wParam, 393 (pMsg->lParam & 0xFFFF), 394 ((pMsg->lParam >> 16) & 0xFF), 395 ((pMsg->lParam >> 24) & 0x1), 396 ((pMsg->lParam >> 25) & 0xF), 397 ((pMsg->lParam >> 29) & 0x1), 398 ((pMsg->lParam >> 30) & 0x1), 399 ((pMsg->lParam >> 31) & 0x1))); 400 #endif 401 402 if (pMsg->message == WM_KEYDOWN || pMsg->message == WM_SYSKEYDOWN) 403 { 404 /* Determine platform-dependent key: */ 405 m_iKeyVal = qi_distinguish_modifier_vkey(pMsg->wParam); 406 /* Determine symbolic name: */ 407 TCHAR *pStr = new TCHAR[256]; 408 if (::GetKeyNameText(pMsg->lParam, pStr, 256)) 409 { 410 m_strSymbName = QString::fromUtf16(pStr); 572 411 } 573 412 else 574 413 { 575 414 AssertFailed(); 576 m SymbName = QString (tr ("<key_%1>")).arg (mKeyVal);577 } 578 delete[] str;579 /* update the display*/415 m_strSymbName = QString(tr("<key_%1>")).arg(m_iKeyVal); 416 } 417 delete[] pStr; 418 /* Update the display: */ 580 419 updateText(); 581 420 } … … 584 423 } 585 424 586 #elif defined (Q_WS_PM)587 588 bool QIHotKeyEdit::pmEvent (QMSG *aMsg)589 {590 if (aMsg->msg != WM_CHAR)591 return false;592 593 USHORT f = SHORT1FROMMP (aMsg->mp1);594 595 int vkey = QIHotKeyEdit::virtualKey (aMsg);596 597 /* ignore if not a valid hot key */598 if (!isValidKey (vkey))599 return false;600 601 if (!(f & KC_KEYUP))602 {603 /* determine platform-dependent key */604 mKeyVal = vkey;605 /* determine symbolic name */606 mSymbName = QIHotKeyEdit::keyName (mKeyVal);607 /* update the display */608 updateText();609 }610 611 return true;612 }613 614 425 #elif defined (Q_WS_X11) 615 426 616 bool QIHotKeyEdit::x11Event (XEvent *event)617 { 618 switch ( event->type)427 bool QIHotKeyEdit::x11Event(XEvent *pEvent) 428 { 429 switch (pEvent->type) 619 430 { 620 431 case XKeyPress: 621 432 case XKeyRelease: 622 433 { 623 XKeyEvent * ke = (XKeyEvent *) event;624 KeySym ks = ::XKeycodeToKeysym (ke->display, ke->keycode, 0);625 /* ignore if not a valid hot key*/626 if (!isValidKey ((int)ks))434 XKeyEvent *pKeyEvent = (XKeyEvent*)pEvent; 435 KeySym ks = ::XKeycodeToKeysym(pKeyEvent->display, pKeyEvent->keycode, 0); 436 /* Ignore if not a valid hot key: */ 437 if (!isValidKey((int)ks)) 627 438 return false; 628 439 629 /* skip key releases*/630 if ( event->type == XKeyRelease)440 /* Skip key releases: */ 441 if (pEvent->type == XKeyRelease) 631 442 return true; 632 443 633 /* determine platform-dependent key*/634 m KeyVal = (int)ks;635 /* determine symbolic name*/636 m SymbName = QIHotKeyEdit::keyName (mKeyVal);637 /* update the display*/444 /* Determine platform-dependent key: */ 445 m_iKeyVal = (int)ks; 446 /* Determine symbolic name: */ 447 m_strSymbName = QIHotKeyEdit::keyName(m_iKeyVal); 448 /* Update the display: */ 638 449 updateText(); 639 450 #if 0 640 LogFlow 641 event->type == XKeyPress ? "XKeyPress" : "XKeyRelease",642 ke->state, ke->keycode, ks,643 451 LogFlow(("%s: state=%08X keycode=%08X keysym=%08X symb=%s\n", 452 pEvent->type == XKeyPress ? "XKeyPress" : "XKeyRelease", 453 pKeyEvent->state, pKeyEvent->keycode, ks, 454 symbname.latin1())); 644 455 #endif 645 456 return true; … … 651 462 652 463 #elif defined (Q_WS_MAC) 464 653 465 /* static */ 654 bool QIHotKeyEdit::darwinEventHandlerProc 655 { 656 QIHotKeyEdit *edit = (QIHotKeyEdit *)pvUser;466 bool QIHotKeyEdit::darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser) 467 { 468 QIHotKeyEdit *edit = (QIHotKeyEdit*)pvUser; 657 469 EventRef inEvent = (EventRef)pvCarbonEvent; 658 UInt32 EventClass = ::GetEventClass 470 UInt32 EventClass = ::GetEventClass(inEvent); 659 471 if (EventClass == kEventClassKeyboard) 660 return edit->darwinKeyboardEvent 472 return edit->darwinKeyboardEvent(pvCocoaEvent, inEvent); 661 473 return false; 662 474 } 663 475 664 665 bool QIHotKeyEdit::darwinKeyboardEvent (const void *pvCocoaEvent, EventRef inEvent) 476 bool QIHotKeyEdit::darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent) 666 477 { 667 478 #if 0 /* for debugging */ … … 669 480 #endif 670 481 671 /* ignore key changes unless we're the focus widget*/482 /* Ignore key changes unless we're the focus widget: */ 672 483 if (!hasFocus()) 673 484 return false; 674 485 675 UInt32 eventKind = ::GetEventKind 486 UInt32 eventKind = ::GetEventKind(inEvent); 676 487 switch (eventKind) 677 488 { … … 682 493 { 683 494 UInt32 modifierMask = 0; 684 ::GetEventParameter 685 sizeof(modifierMask), NULL, &modifierMask);686 687 modifierMask = ::DarwinAdjustModifierMask 688 UInt32 changed = m DarwinKeyModifiers ^ modifierMask;689 m DarwinKeyModifiers = modifierMask;690 691 /* skip key releases*/495 ::GetEventParameter(inEvent, kEventParamKeyModifiers, typeUInt32, NULL, 496 sizeof(modifierMask), NULL, &modifierMask); 497 498 modifierMask = ::DarwinAdjustModifierMask(modifierMask, pvCocoaEvent); 499 UInt32 changed = m_uDarwinKeyModifiers ^ modifierMask; 500 m_uDarwinKeyModifiers = modifierMask; 501 502 /* Skip key releases: */ 692 503 if (changed && (changed & modifierMask)) 693 504 break; 694 505 695 /* convert to keycode and skip keycodes we don't care about. */696 unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode 697 if (!keyCode || keyCode == ~0U || !isValidKey 698 break; 699 700 /* update key current key.*/701 m KeyVal = keyCode;702 m SymbName = QIHotKeyEdit::keyName(keyCode);506 /* Convert to keycode and skip keycodes we don't care about. */ 507 unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode(changed); 508 if (!keyCode || keyCode == ~0U || !isValidKey(keyCode)) 509 break; 510 511 /* Update key current key: */ 512 m_iKeyVal = keyCode; 513 m_strSymbName = QIHotKeyEdit::keyName(keyCode); 703 514 updateText(); 704 515 break; //return true; … … 713 524 #endif 714 525 715 void QIHotKeyEdit::focusInEvent (QFocusEvent *aEvent)716 { 717 QLabel::focusInEvent (aEvent);526 void QIHotKeyEdit::focusInEvent(QFocusEvent *pEvent) 527 { 528 QLabel::focusInEvent(pEvent); 718 529 719 530 QPalette p = palette(); 720 p.setColor (QPalette::Active, QPalette::Foreground, 721 p.color (QPalette::Active, QPalette::HighlightedText)); 722 p.setColor (QPalette::Active, QPalette::Background, 723 p.color (QPalette::Active, QPalette::Highlight)); 724 setPalette (p); 725 } 726 727 void QIHotKeyEdit::focusOutEvent (QFocusEvent *aEvent) 728 { 729 QLabel::focusOutEvent (aEvent); 531 p.setColor(QPalette::Active, QPalette::Foreground, p.color(QPalette::Active, QPalette::HighlightedText)); 532 p.setColor(QPalette::Active, QPalette::Background, p.color(QPalette::Active, QPalette::Highlight)); 533 setPalette(p); 534 } 535 536 void QIHotKeyEdit::focusOutEvent(QFocusEvent *pEvent) 537 { 538 QLabel::focusOutEvent(pEvent); 730 539 731 540 QPalette p = palette(); 732 p.setColor (QPalette::Active, QPalette::Foreground, 733 p.color (QPalette::Active, QPalette::Text)); 734 p.setColor (QPalette::Active, QPalette::Background, 735 p.color (QPalette::Active, QPalette::Base)); 736 setPalette (p); 737 } 738 739 void QIHotKeyEdit::paintEvent (QPaintEvent *aEvent) 541 p.setColor(QPalette::Active, QPalette::Foreground, p.color(QPalette::Active, QPalette::Text)); 542 p.setColor(QPalette::Active, QPalette::Background, p.color(QPalette::Active, QPalette::Base)); 543 setPalette(p); 544 } 545 546 void QIHotKeyEdit::paintEvent(QPaintEvent *pEvent) 740 547 { 741 548 if (hasFocus()) 742 549 { 743 QStylePainter painter 550 QStylePainter painter(this); 744 551 QStyleOptionFocusRect option; 745 option.initFrom 746 option.backgroundColor = palette().color 552 option.initFrom(this); 553 option.backgroundColor = palette().color(QPalette::Background); 747 554 option.rect = contentsRect(); 748 painter.drawPrimitive (QStyle::PE_FrameFocusRect, option); 749 } 750 QLabel::paintEvent (aEvent); 751 } 752 753 // Private members 754 ///////////////////////////////////////////////////////////////////////////// 555 painter.drawPrimitive(QStyle::PE_FrameFocusRect, option); 556 } 557 QLabel::paintEvent(pEvent); 558 } 755 559 756 560 void QIHotKeyEdit::updateText() 757 561 { 758 setText (QString (" %1 ").arg (mSymbName));759 } 760 562 setText(QString(" %1 ").arg(m_strSymbName)); 563 } 564 -
trunk/src/VBox/Frontends/VirtualBox/src/extensions/QIHotKeyEdit.h
r29816 r35580 6 6 7 7 /* 8 * Copyright (C) 2006-20 07Oracle Corporation8 * Copyright (C) 2006-2011 Oracle Corporation 9 9 * 10 10 * This file is part of VirtualBox Open Source Edition (OSE), as … … 20 20 #define ___QIHotKeyEdit_h___ 21 21 22 /* Global includes */ 22 23 #include <QLabel> 23 24 24 25 #ifdef Q_WS_X11 25 26 # include <QMap> 26 #endif 27 28 #ifdef Q_WS_PM 29 /* Extra virtual keys returned by QIHotKeyEdit::virtualKey() */ 30 # define VK_LSHIFT VK_USERFIRST + 0 31 # define VK_LCTRL VK_USERFIRST + 1 32 # define VK_LWIN VK_USERFIRST + 2 33 # define VK_RWIN VK_USERFIRST + 3 34 # define VK_WINMENU VK_USERFIRST + 4 35 # define VK_FORWARD VK_USERFIRST + 5 36 # define VK_BACKWARD VK_USERFIRST + 6 37 #endif 27 #endif /* Q_WS_X11 */ 38 28 39 29 class QIHotKeyEdit : public QLabel … … 43 33 public: 44 34 45 QIHotKeyEdit (QWidget *aParent);35 QIHotKeyEdit(QWidget *pParent); 46 36 virtual ~QIHotKeyEdit(); 47 37 48 void setKey (int aKeyVal);49 int key() const { return m KeyVal; }38 void setKey(int iKeyVal); 39 int key() const { return m_iKeyVal; } 50 40 51 QString symbolicName() const { return m SymbName; }41 QString symbolicName() const { return m_strSymbName; } 52 42 53 43 QSize sizeHint() const; 54 44 QSize minimumSizeHint() const; 55 45 56 #ifdef Q_WS_PM 57 static int virtualKey (QMSG *aMsg); 58 #endif 59 60 #if defined (Q_WS_PM) || defined (Q_WS_X11) 46 static QString keyName(int iKeyVal); 47 static bool isValidKey(int iKeyVal); 48 #ifdef Q_WS_X11 61 49 static void retranslateUi(); 62 #endif 63 static QString keyName (int aKeyVal); 64 static bool isValidKey (int aKeyVal); 50 #endif /* Q_WS_X11 */ 65 51 66 52 public slots: … … 71 57 72 58 #if defined (Q_WS_WIN32) 73 bool winEvent (MSG *aMsg, long *aResult); 74 #elif defined (Q_WS_PM) 75 bool pmEvent (QMSG *aMsg); 59 bool winEvent(MSG *pMsg, long *pResult); 76 60 #elif defined (Q_WS_X11) 77 bool x11Event (XEvent *event);61 bool x11Event(XEvent *pEvent); 78 62 #elif defined (Q_WS_MAC) 79 static bool darwinEventHandlerProc 80 bool darwinKeyboardEvent 63 static bool darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser); 64 bool darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent); 81 65 #endif 82 66 83 void focusInEvent (QFocusEvent *); 84 void focusOutEvent (QFocusEvent *); 85 86 void paintEvent (QPaintEvent *); 67 void focusInEvent(QFocusEvent *pEvent); 68 void focusOutEvent(QFocusEvent *pEvent); 69 void paintEvent(QPaintEvent *pEvent); 87 70 88 71 private: … … 90 73 void updateText(); 91 74 92 int m KeyVal;93 QString m SymbName;75 int m_iKeyVal; 76 QString m_strSymbName; 94 77 95 #if defined (Q_WS_PM) 96 static QMap <int, QString> sKeyNames; 97 #elif defined (Q_WS_X11) 98 static QMap <QString, QString> sKeyNames; 99 #endif 78 #ifdef Q_WS_X11 79 static QMap<QString, QString> s_keyNames; 80 #endif /* Q_WS_X11 */ 100 81 101 82 #ifdef Q_WS_MAC 102 /* *The current modifier key mask. Used to figure out which modifier103 * 104 uint32_t m DarwinKeyModifiers;105 #endif 83 /* The current modifier key mask. Used to figure out which modifier 84 * key was pressed when we get a kEventRawKeyModifiersChanged event. */ 85 uint32_t m_uDarwinKeyModifiers; 86 #endif /* Q_WS_MAC */ 106 87 107 static const char * kNoneSymbName;88 static const char *m_spNoneSymbName; 108 89 }; 109 90
Note:
See TracChangeset
for help on using the changeset viewer.