merge in my changes from soc-krdc branch
[kdenetwork.git] / krdc / keycapturedialog.cpp
blob35048511918a8a73210624c65272317729c2cf2b
1 /*
2 Copyright (C) 2002-2003 Tim Jansen <tim@tjansen.de>
3 Copyright (C) 2004 Nadeem Hasan <nhasan@kde.org>
4 */
5 /***************************************************************************
6 * *
7 * This program is free software; you can redistribute it and/or modify *
8 * it under the terms of the GNU General Public License as published by *
9 * the Free Software Foundation; either version 2 of the License, or *
10 * (at your option) any later version. *
11 * *
12 ***************************************************************************/
15 // based on key capture code from kdelibs/kdeui/kshortcutdialog.cpp
18 #include "keycapturedialog.h"
19 #include "keycapturewidget.h"
21 #include <QLabel>
22 #include <QVBoxLayout>
23 #include <QFrame>
25 #include <klocale.h>
27 #define XK_XKB_KEYS
28 #define XK_MISCELLANY
29 #include <X11/Xlib.h> // For x11Event()
30 #include <X11/keysymdef.h> // For XK_...
32 #ifdef KeyPress
33 const int XFocusOut = FocusOut;
34 const int XFocusIn = FocusIn;
35 const int XKeyPress = KeyPress;
36 const int XKeyRelease = KeyRelease;
37 #undef KeyRelease
38 #undef KeyPress
39 #undef FocusOut
40 #undef FocusIn
41 #endif
44 KeyCaptureDialog::KeyCaptureDialog(QWidget *parent)
45 : KDialog( parent ), m_grabbed(false) {
46 setModal( true );
47 setCaption( i18n( "Enter Key Combination" ) );
48 setButtons( Cancel );
49 setDefaultButton( Cancel );
50 showButtonSeparator( true );
52 QFrame *main = new QFrame();
53 setMainWidget( main );
55 QVBoxLayout *layout = new QVBoxLayout( main );
56 layout->setSpacing( KDialog::spacingHint() );
57 layout->setMargin( 0 );
58 m_captureWidget = new KeyCaptureWidget( main, "m_captureWidget" );
59 layout->addWidget( m_captureWidget );
60 layout->addStretch();
63 KeyCaptureDialog::~KeyCaptureDialog() {
64 if (m_grabbed)
65 releaseKeyboard();
68 void KeyCaptureDialog::execute() {
69 m_captureWidget->keyLabel->setText("");
70 exec();
71 if (m_grabbed)
72 releaseKeyboard();
75 bool KeyCaptureDialog::x11Event(XEvent *pEvent)
77 switch( pEvent->type ) {
78 case XKeyPress:
79 case XKeyRelease:
80 x11EventKeyPress( pEvent );
81 return true;
82 case XFocusIn:
83 if (!m_grabbed)
84 grabKeyboard();
85 return true;
86 case XFocusOut:
87 if (m_grabbed)
88 releaseKeyboard();
89 return true;
90 default:
91 break;
93 return QWidget::x11Event( pEvent );
96 void KeyCaptureDialog::x11EventKeyPress( XEvent *pEvent )
98 #ifdef __GNUC__
99 #warning PortMe
100 #endif
101 #if 0
102 // taken from kshortcutdialog.h
103 KKeyNative keyNative( pEvent );
104 uint keyModX = keyNative.mod(), keySymX = keyNative.sym();
105 if ((keySymX == XK_Escape) && !keyModX) {
106 accept();
107 return;
110 switch( keySymX ) {
111 // Don't allow setting a modifier key as an accelerator.
112 // Also, don't release the focus yet. We'll wait until
113 // we get a 'normal' key.
114 case XK_Shift_L: case XK_Shift_R: keyModX = KKeyNative::modXShift(); break;
115 case XK_Control_L: case XK_Control_R: keyModX = KKeyNative::modXCtrl(); break;
116 case XK_Alt_L: case XK_Alt_R: keyModX = KKeyNative::modXAlt(); break;
117 // FIXME: check whether the Meta or Super key are for the Win modifier
118 case XK_Meta_L: case XK_Meta_R:
119 case XK_Super_L: case XK_Super_R: keyModX = KKeyNative::modXWin(); break;
120 case XK_Hyper_L: case XK_Hyper_R:
121 case XK_Mode_switch:
122 case XK_Num_Lock:
123 case XK_Caps_Lock:
124 break;
125 default:
126 if( pEvent->type == XKeyPress && keyNative.sym() ) {
127 emit keyPressed(pEvent);
128 reject();
130 return;
133 // If we are editing the first key in the sequence,
134 // display modifier keys which are held down
135 if( pEvent->type == XKeyPress )
136 keyModX |= pEvent->xkey.state;
137 else
138 keyModX = pEvent->xkey.state & ~keyModX;
140 QString keyModStr;
141 if( keyModX & KKeyNative::modXWin() ) keyModStr += KKey::modFlagLabel(KKey::WIN) + '+';
142 if( keyModX & KKeyNative::modXAlt() ) keyModStr += KKey::modFlagLabel(KKey::ALT) + '+';
143 if( keyModX & KKeyNative::modXCtrl() ) keyModStr += KKey::modFlagLabel(KKey::CTRL) + '+';
144 if( keyModX & KKeyNative::modXShift() ) keyModStr += KKey::modFlagLabel(KKey::SHIFT) + '+';
146 // Display currently selected modifiers, or redisplay old key.
147 m_captureWidget->keyLabel->setText( keyModStr );
148 #endif
151 #include "keycapturedialog.moc"