VirtualBox

Changeset 35580 in vbox


Ignore:
Timestamp:
Jan 17, 2011 12:09:32 PM (14 years ago)
Author:
vboxsync
Message:

FE/Qt: Hot-key editor refactoring to latest coding-style, some cleanup.

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  
    77
    88/*
    9  * Copyright (C) 2006-2007 Oracle Corporation
     9 * Copyright (C) 2006-2011 Oracle Corporation
    1010 *
    1111 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    1818 */
    1919
     20/* Local includes */
    2021#include "QIHotKeyEdit.h"
    2122#include "VBoxDefs.h"
    2223#include "VBoxGlobal.h"
    2324
    24 /* Qt includes */
     25/* Global includes */
    2526#include <QApplication>
    2627#include <QStyleOption>
     
    2930#ifdef Q_WS_WIN
    3031/* 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 */
    4138
    4239#ifdef Q_WS_X11
     
    4643 * conflicts with Qt. Therefore we use the following hack
    4744 * to redefine those conflicting identifiers. */
    48 #define XK_XKB_KEYS
    49 #define XK_MISCELLANY
    50 #include <X11/Xlib.h>
    51 #include <X11/Xutil.h>
    52 #include <X11/keysymdef.h>
    53 #ifdef KeyPress
     45# 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
    5451const int XFocusOut = FocusOut;
    5552const int XFocusIn = FocusIn;
    5653const int XKeyPress = KeyPress;
    5754const int XKeyRelease = KeyRelease;
    58 #undef KeyRelease
    59 #undef KeyPress
    60 #undef FocusOut
    61 #undef FocusIn
    62 #endif
    63 #include "XKeyboard.h"
    64 QMap<QString, QString> QIHotKeyEdit::sKeyNames;
    65 #include <QX11Info>
    66 #endif
     55#  undef KeyRelease
     56#  undef KeyPress
     57#  undef FocusOut
     58#  undef FocusIn
     59# endif /* KeyPress */
     60# include "XKeyboard.h"
     61QMap<QString, QString> QIHotKeyEdit::s_keyNames;
     62# include <QX11Info>
     63#endif /* Q_WS_X11 */
    6764
    6865#ifdef Q_WS_MAC
     
    7471
    7572
    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. */
     78int qi_distinguish_modifier_vkey(WPARAM wParam)
    8479{
    8580    int keyval = wParam;
     
    8782    {
    8883        case VK_SHIFT:
    89             if (::GetKeyState (VK_LSHIFT) & 0x8000) keyval = VK_LSHIFT;
    90             else if (::GetKeyState (VK_RSHIFT) & 0x8000) keyval = VK_RSHIFT;
     84            if (::GetKeyState(VK_LSHIFT) & 0x8000) keyval = VK_LSHIFT;
     85            else if (::GetKeyState(VK_RSHIFT) & 0x8000) keyval = VK_RSHIFT;
    9186            break;
    9287        case VK_CONTROL:
    93             if (::GetKeyState (VK_LCONTROL) & 0x8000) keyval = VK_LCONTROL;
    94             else if (::GetKeyState (VK_RCONTROL) & 0x8000) keyval = VK_RCONTROL;
     88            if (::GetKeyState(VK_LCONTROL) & 0x8000) keyval = VK_LCONTROL;
     89            else if (::GetKeyState(VK_RCONTROL) & 0x8000) keyval = VK_RCONTROL;
    9590            break;
    9691        case VK_MENU:
    97             if (::GetKeyState (VK_LMENU) & 0x8000) keyval = VK_LMENU;
    98             else if (::GetKeyState (VK_RMENU) & 0x8000) keyval = VK_RMENU;
     92            if (::GetKeyState(VK_LMENU) & 0x8000) keyval = VK_LMENU;
     93            else if (::GetKeyState(VK_RMENU) & 0x8000) keyval = VK_RMENU;
    9994            break;
    10095    }
    10196    return keyval;
    10297}
    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
     101const char *QIHotKeyEdit::m_spNoneSymbName = "None";
     102
     103QIHotKeyEdit::QIHotKeyEdit(QWidget *pParent)
     104    : QLabel(pParent)
    114105{
    115106#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 */
    120110
    121111    clear();
    122112
    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);
    133122
    134123    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);
    140127
    141128#ifdef Q_WS_MAC
    142     mDarwinKeyModifiers = GetCurrentEventKeyModifiers();
     129    m_uDarwinKeyModifiers = GetCurrentEventKeyModifiers();
    143130    UICocoaApplication::instance()->registerForNativeEvents(RT_BIT_32(10) | RT_BIT_32(11) | RT_BIT_32(12) /* NSKeyDown  | NSKeyUp | | NSFlagsChanged */, QIHotKeyEdit::darwinEventHandlerProc, this);
    144     ::DarwinGrabKeyboard (false /* just modifiers */);
    145 #endif
     131    ::DarwinGrabKeyboard(false /* just modifiers */);
     132#endif /* Q_WS_MAC */
    146133}
    147134
     
    153140#endif
    154141}
    155 
    156 // Public members
    157 /////////////////////////////////////////////////////////////////////////////
    158142
    159143/**
     
    165149 *      to the keycode.
    166150 */
    167 void QIHotKeyEdit::setKey (int aKeyVal)
    168 {
    169     mKeyVal = aKeyVal;
    170     mSymbName = QIHotKeyEdit::keyName (aKeyVal);
     151void QIHotKeyEdit::setKey(int iKeyVal)
     152{
     153    m_iKeyVal = iKeyVal;
     154    m_strSymbName = QIHotKeyEdit::keyName(iKeyVal);
    171155    updateText();
    172156}
     
    183167 */
    184168
    185 /**
    186  *  Stolen from QLineEdit.
    187  */
    188169QSize QIHotKeyEdit::sizeHint() const
    189170{
    190171    ensurePolished();
    191     QFontMetrics fm (font());
    192     int h = qMax (fm.lineSpacing(), 14) + 2;
    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;
    194175    int m = frameWidth() * 2;
    195176    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
    206184QSize QIHotKeyEdit::minimumSizeHint() const
    207185{
    208186    ensurePolished();
    209187    QFontMetrics fm = fontMetrics();
    210     int h = fm.height() + qMax (2, fm.leading());
     188    int h = fm.height() + qMax(2, fm.leading());
    211189    int w = fm.maxWidth();
    212190    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}
    345193
    346194/**
     
    353201 */
    354202/* static */
    355 QString QIHotKeyEdit::keyName (int aKeyVal)
    356 {
    357     QString name;
    358 
    359     if (!aKeyVal)
    360     {
    361         name = tr (kNoneSymbName);
     203QString QIHotKeyEdit::keyName(int iKeyVal)
     204{
     205    QString strName;
     206
     207    if (!iKeyVal)
     208    {
     209        strName = tr(m_spNoneSymbName);
    362210    }
    363211    else
    364212    {
    365 #if defined (Q_WS_WIN32)
     213#if defined (Q_WS_WIN)
    366214        /* Stupid MapVirtualKey doesn't distinguish between right and left
    367215         * vkeys, even under XP, despite that it stated in msdn. Do it by
     
    369217         * VK_DIVIDE & VK_PAUSE, this is also known bug. */
    370218        int scan;
    371         switch (aKeyVal)
     219        switch (iKeyVal)
    372220        {
    373221            /* Processing special keys... */
     
    380228            case VK_LWIN:
    381229            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 [256];
    386         if (::GetKeyNameText (scan, str, 256))
    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);
    389237        }
    390238        else
    391239        {
    392240            AssertFailed();
    393             name = QString (tr ("<key_%1>")).arg (aKeyVal);
     241            strName = QString(tr("<key_%1>")).arg(iKeyVal);
    394242        }
    395243        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
    399253        {
    400254            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);
    415256        }
    416257#elif defined(Q_WS_MAC)
    417         UInt32 modMask = DarwinKeyCodeToDarwinModifierMask (aKeyVal);
     258        UInt32 modMask = DarwinKeyCodeToDarwinModifierMask(iKeyVal);
    418259        switch (modMask)
    419260        {
     
    422263            case controlKey:
    423264            case cmdKey:
    424                 name = tr ("Left ");
     265                strName = tr("Left ");
    425266                break;
    426267            case rightShiftKey:
     
    428269            case rightControlKey:
    429270            case kEventKeyModifierRightCmdKeyMask:
    430                 name = tr ("Right ");
     271                strName = tr("Right ");
    431272                break;
    432273            default:
    433                 AssertMsgFailedReturn (("modMask=%#x\n", modMask), QString());
     274                AssertMsgFailedReturn(("modMask=%#x\n", modMask), QString());
    434275        }
    435276        switch (modMask)
     
    437278            case shiftKey:
    438279            case rightShiftKey:
    439                 name += QChar (kShiftUnicode);
     280                strName += QChar(kShiftUnicode);
    440281                break;
    441282            case optionKey:
    442283            case rightOptionKey:
    443                 name += QChar (kOptionUnicode);
     284                strName += QChar(kOptionUnicode);
    444285                break;
    445286            case controlKey:
    446287            case rightControlKey:
    447                 name += QChar (kControlUnicode);
     288                strName += QChar(kControlUnicode);
    448289                break;
    449290            case cmdKey:
    450291            case kEventKeyModifierRightCmdKeyMask:
    451                 name += QChar (kCommandUnicode);
     292                strName += QChar(kCommandUnicode);
    452293                break;
    453294        }
    454295#else
    455296        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;
    461302}
    462303
    463304/* static */
    464 bool QIHotKeyEdit::isValidKey (int aKeyVal)
     305bool QIHotKeyEdit::isValidKey(int iKeyVal)
    465306{
    466307    /* Empty value is correct: */
    467     if (aKeyVal == 0)
     308    if (iKeyVal == 0)
    468309        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));
    485318#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));
    497322#elif defined(Q_WS_MAC)
    498     UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask (aKeyVal);
     323    UInt32 modMask = ::DarwinKeyCodeToDarwinModifierMask(iKeyVal);
    499324    switch (modMask)
    500325    {
     
    512337    }
    513338#else
    514     Q_UNUSED (aKeyVal);
     339    Q_UNUSED(iKeyVal);
    515340    return true;
    516341#endif
    517342}
    518343
    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. */
     347void 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 */
    521366
    522367void QIHotKeyEdit::clear()
    523368{
    524     mKeyVal = 0;
    525     mSymbName = tr (kNoneSymbName);
     369    m_iKeyVal = 0;
     370    m_strSymbName = tr(m_spNoneSymbName);
    526371    updateText();
    527372}
    528373
    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
     376bool 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))
    544383        return false;
    545384
    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))
    548387        return false;
    549388
    550389#if 0
    551     LogFlow (("%WM_%04X: vk=%04X rep=%05d scan=%02X ext=%01d"
    552               "rzv=%01X ctx=%01d prev=%01d tran=%01d\n",
    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         mKeyVal = 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             mSymbName = 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);
    572411        }
    573412        else
    574413        {
    575414            AssertFailed();
    576             mSymbName = 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: */
    580419        updateText();
    581420    }
     
    584423}
    585424
    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 
    614425#elif defined (Q_WS_X11)
    615426
    616 bool QIHotKeyEdit::x11Event (XEvent *event)
    617 {
    618     switch (event->type)
     427bool QIHotKeyEdit::x11Event(XEvent *pEvent)
     428{
     429    switch (pEvent->type)
    619430    {
    620431        case XKeyPress:
    621432        case XKeyRelease:
    622433        {
    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))
    627438                return false;
    628439
    629             /* skip key releases */
    630             if (event->type == XKeyRelease)
     440            /* Skip key releases: */
     441            if (pEvent->type == XKeyRelease)
    631442                return true;
    632443
    633             /* determine platform-dependent key */
    634             mKeyVal = (int) ks;
    635             /* determine symbolic name */
    636             mSymbName = 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: */
    638449            updateText();
    639450#if 0
    640             LogFlow (("%s: state=%08X keycode=%08X keysym=%08X symb=%s\n",
    641                       event->type == XKeyPress ? "XKeyPress" : "XKeyRelease",
    642                       ke->state, ke->keycode, ks,
    643                       symbname.latin1()));
     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()));
    644455#endif
    645456            return true;
     
    651462
    652463#elif defined (Q_WS_MAC)
     464
    653465/* static */
    654 bool QIHotKeyEdit::darwinEventHandlerProc (const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser)
    655 {
    656     QIHotKeyEdit *edit = (QIHotKeyEdit *) pvUser;
     466bool QIHotKeyEdit::darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser)
     467{
     468    QIHotKeyEdit *edit = (QIHotKeyEdit*)pvUser;
    657469    EventRef inEvent = (EventRef)pvCarbonEvent;
    658     UInt32 EventClass = ::GetEventClass (inEvent);
     470    UInt32 EventClass = ::GetEventClass(inEvent);
    659471    if (EventClass == kEventClassKeyboard)
    660         return edit->darwinKeyboardEvent (pvCocoaEvent, inEvent);
     472        return edit->darwinKeyboardEvent(pvCocoaEvent, inEvent);
    661473    return false;
    662474}
    663475
    664 
    665 bool QIHotKeyEdit::darwinKeyboardEvent (const void *pvCocoaEvent, EventRef inEvent)
     476bool QIHotKeyEdit::darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent)
    666477{
    667478#if 0 /* for debugging */
     
    669480#endif
    670481
    671     /* ignore key changes unless we're the focus widget */
     482    /* Ignore key changes unless we're the focus widget: */
    672483    if (!hasFocus())
    673484        return false;
    674485
    675     UInt32 eventKind = ::GetEventKind (inEvent);
     486    UInt32 eventKind = ::GetEventKind(inEvent);
    676487    switch (eventKind)
    677488    {
     
    682493        {
    683494            UInt32 modifierMask = 0;
    684             ::GetEventParameter (inEvent, kEventParamKeyModifiers, typeUInt32, NULL,
    685                                  sizeof (modifierMask), NULL, &modifierMask);
    686 
    687             modifierMask = ::DarwinAdjustModifierMask (modifierMask, pvCocoaEvent);
    688             UInt32 changed = mDarwinKeyModifiers ^ modifierMask;
    689             mDarwinKeyModifiers = 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: */
    692503            if (changed && (changed & modifierMask))
    693504                break;
    694505
    695             /* convert to keycode and skip keycodes we don't care about. */
    696             unsigned keyCode = ::DarwinModifierMaskToDarwinKeycode (changed);
    697             if (!keyCode || keyCode == ~0U || !isValidKey (keyCode))
    698                 break;
    699 
    700             /* update key current key. */
    701             mKeyVal = keyCode;
    702             mSymbName = 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);
    703514            updateText();
    704515            break; //return true;
     
    713524#endif
    714525
    715 void QIHotKeyEdit::focusInEvent (QFocusEvent *aEvent)
    716 {
    717     QLabel::focusInEvent (aEvent);
     526void QIHotKeyEdit::focusInEvent(QFocusEvent *pEvent)
     527{
     528    QLabel::focusInEvent(pEvent);
    718529
    719530    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
     536void QIHotKeyEdit::focusOutEvent(QFocusEvent *pEvent)
     537{
     538    QLabel::focusOutEvent(pEvent);
    730539
    731540    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
     546void QIHotKeyEdit::paintEvent(QPaintEvent *pEvent)
    740547{
    741548    if (hasFocus())
    742549    {
    743         QStylePainter painter (this);
     550        QStylePainter painter(this);
    744551        QStyleOptionFocusRect option;
    745         option.initFrom (this);
    746         option.backgroundColor = palette().color (QPalette::Background);
     552        option.initFrom(this);
     553        option.backgroundColor = palette().color(QPalette::Background);
    747554        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}
    755559
    756560void QIHotKeyEdit::updateText()
    757561{
    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  
    66
    77/*
    8  * Copyright (C) 2006-2007 Oracle Corporation
     8 * Copyright (C) 2006-2011 Oracle Corporation
    99 *
    1010 * This file is part of VirtualBox Open Source Edition (OSE), as
     
    2020#define ___QIHotKeyEdit_h___
    2121
     22/* Global includes */
    2223#include <QLabel>
    2324
    2425#ifdef Q_WS_X11
    2526# 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 */
    3828
    3929class QIHotKeyEdit : public QLabel
     
    4333public:
    4434
    45     QIHotKeyEdit (QWidget *aParent);
     35    QIHotKeyEdit(QWidget *pParent);
    4636    virtual ~QIHotKeyEdit();
    4737
    48     void setKey (int aKeyVal);
    49     int key() const { return mKeyVal; }
     38    void setKey(int iKeyVal);
     39    int key() const { return m_iKeyVal; }
    5040
    51     QString symbolicName() const { return mSymbName; }
     41    QString symbolicName() const { return m_strSymbName; }
    5242
    5343    QSize sizeHint() const;
    5444    QSize minimumSizeHint() const;
    5545
    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
    6149    static void retranslateUi();
    62 #endif
    63     static QString keyName (int aKeyVal);
    64     static bool isValidKey (int aKeyVal);
     50#endif /* Q_WS_X11 */
    6551
    6652public slots:
     
    7157
    7258#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);
    7660#elif defined (Q_WS_X11)
    77     bool x11Event (XEvent *event);
     61    bool x11Event(XEvent *pEvent);
    7862#elif defined (Q_WS_MAC)
    79     static bool darwinEventHandlerProc (const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
    80     bool darwinKeyboardEvent (const void *pvCocoaEvent, EventRef inEvent);
     63    static bool darwinEventHandlerProc(const void *pvCocoaEvent, const void *pvCarbonEvent, void *pvUser);
     64    bool darwinKeyboardEvent(const void *pvCocoaEvent, EventRef inEvent);
    8165#endif
    8266
    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);
    8770
    8871private:
     
    9073    void updateText();
    9174
    92     int mKeyVal;
    93     QString mSymbName;
     75    int m_iKeyVal;
     76    QString m_strSymbName;
    9477
    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 */
    10081
    10182#ifdef Q_WS_MAC
    102     /** The current modifier key mask. Used to figure out which modifier
    103      *  key was pressed when we get a kEventRawKeyModifiersChanged event. */
    104     uint32_t mDarwinKeyModifiers;
    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 */
    10687
    107     static const char *kNoneSymbName;
     88    static const char *m_spNoneSymbName;
    10889};
    10990
Note: See TracChangeset for help on using the changeset viewer.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette