not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kcontrol / randr / krandrpassivepopup.cpp
blobe94d77816f1f78723caebd1d3af117a30f48a3f8
1 /*
2 * Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 #include "krandrpassivepopup.h"
21 #include <kapplication.h>
22 #include <QX11Info>
23 //Added by qt3to4:
24 #include <QPixmap>
25 #include <QEvent>
27 // this class is just like KPassivePopup, but it keeps track of the widget
28 // it's supposed to be positioned next to, and adjust its position if that
29 // widgets moves (needed because after a resolution switch Kicker will
30 // reposition itself, causing normal KPassivePopup to stay at weird places)
32 KRandrPassivePopup::KRandrPassivePopup( QWidget *parent, Qt::WFlags f )
33 : KPassivePopup( parent, f )
35 update_timer.setSingleShot( true );
36 connect( &update_timer, SIGNAL( timeout()), SLOT( slotPositionSelf()));
39 KRandrPassivePopup* KRandrPassivePopup::message( const QString &caption, const QString &text,
40 const QPixmap &icon, QWidget *parent, int timeout )
42 KRandrPassivePopup *pop = new KRandrPassivePopup( parent );
43 pop->setAutoDelete( true );
44 pop->setView( caption, text, icon );
45 pop->setTimeout( timeout );
46 pop->show();
47 pop->startWatchingWidget( parent );
48 return pop;
51 void KRandrPassivePopup::startWatchingWidget( QWidget* widget_P )
53 static Atom wm_state = XInternAtom( QX11Info::display() , "WM_STATE", False );
54 Window win = widget_P->winId();
55 bool x11_events = false;
56 for(;;)
58 Window root, parent;
59 Window* children;
60 unsigned int nchildren;
61 XQueryTree( QX11Info::display(), win, &root, &parent, &children, &nchildren );
62 if( children != NULL )
63 XFree( children );
64 if( win == root ) // huh?
65 break;
66 win = parent;
68 QWidget* widget = QWidget::find( win );
69 if( widget != NULL )
71 widget->installEventFilter( this );
72 watched_widgets.append( widget );
74 else
76 XWindowAttributes attrs;
77 XGetWindowAttributes( QX11Info::display(), win, &attrs );
78 XSelectInput( QX11Info::display(), win, attrs.your_event_mask | StructureNotifyMask );
79 watched_windows.append( win );
80 x11_events = true;
82 Atom type;
83 int format;
84 unsigned long nitems, after;
85 unsigned char* data;
86 if( XGetWindowProperty( QX11Info::display(), win, wm_state, 0, 0, False, AnyPropertyType,
87 &type, &format, &nitems, &after, &data ) == Success )
89 if( data != NULL )
90 XFree( data );
91 if( type != None ) // toplevel window
92 break;
95 if( x11_events )
96 kapp->installX11EventFilter( this );
99 bool KRandrPassivePopup::eventFilter( QObject* o, QEvent* e )
101 if( e->type() == QEvent::Move && o->isWidgetType()
102 && watched_widgets.contains( static_cast< QWidget* >( o )))
103 QTimer::singleShot( 0, this, SLOT( slotPositionSelf()));
104 return false;
107 bool KRandrPassivePopup::x11Event( XEvent* e )
109 if( e->type == ConfigureNotify && watched_windows.contains( e->xconfigure.window ) )
111 if( !update_timer.isActive() )
112 update_timer.start( 10 );
113 return false;
115 return KPassivePopup::x11Event( e );
118 void KRandrPassivePopup::slotPositionSelf()
120 positionSelf();
123 #include "krandrpassivepopup.moc"