~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to khotkeys/kcm_hotkeys/helper_widgets/window_selector.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************
 
2
 
 
3
 KHotKeys
 
4
 
 
5
 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
 
6
 
 
7
 Distributed under the terms of the GNU General Public License version 2.
 
8
 
 
9
****************************************************************************/
 
10
 
 
11
#define WINDOWSELECTOR_CPP
 
12
 
 
13
#include "helper_widgets/window_selector.h"
 
14
 
 
15
#include <kdebug.h>
 
16
#include <kapplication.h>
 
17
 
 
18
#include <QtGui/QDesktopWidget>
 
19
 
 
20
#include <X11/Xlib.h>
 
21
#include <QX11Info>
 
22
#include <fixx11h.h>
 
23
 
 
24
namespace KHotKeys
 
25
{
 
26
 
 
27
WindowSelector::WindowSelector( QObject* receiver_P, const char* slot_P )
 
28
    {
 
29
    connect( this, SIGNAL( selected_signal( WId )), receiver_P, slot_P );
 
30
    }
 
31
 
 
32
void WindowSelector::select()
 
33
    {
 
34
    kapp->desktop()->grabMouse( QCursor( Qt::CrossCursor ));
 
35
    kapp->installX11EventFilter( this );
 
36
    }
 
37
 
 
38
bool WindowSelector::x11Event( XEvent* e )
 
39
    {
 
40
    if( e->type != ButtonPress )
 
41
        return false;
 
42
    kapp->desktop()->releaseMouse();
 
43
    if( e->xbutton.button == Button1 )
 
44
        {
 
45
        WId window = findRealWindow( e->xbutton.subwindow );
 
46
        if( window )
 
47
            selected_signal( window );
 
48
        }
 
49
    delete this;
 
50
    return true;
 
51
    }
 
52
 
 
53
WId WindowSelector::findRealWindow( WId w, int depth )
 
54
    {
 
55
    if( depth > 5 )
 
56
        return None;
 
57
    static Atom wm_state = XInternAtom( QX11Info::display(), "WM_STATE", False );
 
58
    Atom type;
 
59
    int format;
 
60
    unsigned long nitems, after;
 
61
    unsigned char* prop;
 
62
    if( XGetWindowProperty( QX11Info::display(), w, wm_state, 0, 0, False, AnyPropertyType,
 
63
        &type, &format, &nitems, &after, &prop ) == Success )
 
64
        {
 
65
        if( prop != NULL )
 
66
            XFree( prop );
 
67
        if( type != None )
 
68
            return w;
 
69
        }
 
70
    Window root, parent;
 
71
    Window* children;
 
72
    unsigned int nchildren;
 
73
    Window ret = None;
 
74
    if( XQueryTree( QX11Info::display(), w, &root, &parent, &children, &nchildren ) != 0 )
 
75
        {
 
76
        for( unsigned int i = 0;
 
77
             i < nchildren && ret == None;
 
78
             ++i )
 
79
            ret = findRealWindow( children[ i ], depth + 1 );
 
80
        if( children != NULL )
 
81
            XFree( children );
 
82
        }
 
83
    return ret;
 
84
}
 
85
 
 
86
 
 
87
} // namespace KHotKeys
 
88
 
 
89
#include "window_selector.moc"