not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kdm / kfrontend / kfdialog.cpp
blobfb2361c6b6fb1c0e668aa1160d8f42a1eb270dbf
1 /*
3 Dialog class that handles input focus in absence of a wm
5 Copyright (C) 1997, 1998 Steffen Hansen <hansen@kde.org>
6 Copyright (C) 2000-2003 Oswald Buddenhagen <ossi@kde.org>
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
25 #include "kfdialog.h"
26 #include "kdmconfig.h"
27 #include "kdm_greet.h"
29 #include <KGuiItem>
30 #include <KPushButton>
32 #include <QApplication>
33 #include <QDesktopWidget>
34 #include <QFrame>
35 #include <QGridLayout>
36 #include <QLabel>
37 #include <QMouseEvent>
38 #include <QX11Info>
40 #include <X11/Xlib.h>
42 #include <stdio.h>
44 FDialog::FDialog( QWidget *parent, bool framed )
45 : inherited( parent/*, framed ? 0 : WStyle_NoBorder*/ )
47 setModal( true );
48 if (framed) {
49 winFrame = new QFrame( this );
50 winFrame->setFrameStyle( QFrame::WinPanel | QFrame::Raised );
51 winFrame->setLineWidth( 2 );
52 } else
53 winFrame = 0;
56 void
57 FDialog::resizeEvent( QResizeEvent *e )
59 inherited::resizeEvent( e );
60 if (winFrame)
61 winFrame->resize( size() );
64 void
65 FDialog::fitInto( const QRect &scr, QRect &grt )
67 int di;
68 if ((di = scr.right() - grt.right()) < 0)
69 grt.translate( di, 0 );
70 if ((di = scr.left() - grt.left()) > 0)
71 grt.translate( di, 0 );
72 if ((di = scr.bottom() - grt.bottom()) < 0)
73 grt.translate( 0, di );
74 if ((di = scr.top() - grt.top()) > 0)
75 grt.translate( 0, di );
78 void
79 FDialog::adjustGeometry()
81 QDesktopWidget *dsk = qApp->desktop();
83 QRect scr = dsk->screenGeometry( _greeterScreen );
84 if (!winFrame)
85 setFixedSize( scr.size() );
86 else {
87 setMaximumSize( scr.size() * .9 );
88 setMinimumSize( 0, 0 );
89 adjustSize();
92 if (parentWidget())
93 return;
95 QRect grt( rect() );
96 if (winFrame) {
97 unsigned x = 50, y = 50;
98 sscanf( _greeterPos, "%u,%u", &x, &y );
99 grt.moveCenter( QPoint( scr.x() + scr.width() * x / 100,
100 scr.y() + scr.height() * y / 100 ) );
101 fitInto( scr, grt );
102 setGeometry( grt );
105 if (dsk->screenNumber( QCursor::pos() ) != _greeterScreen)
106 QCursor::setPos( grt.center() );
109 static void
110 fakeFocusIn( WId window )
112 // We have keyboard grab, so this application will
113 // get keyboard events even without having focus.
114 // Fake FocusIn to make Qt realize it has the active
115 // window, so that it will correctly show cursor in the dialog.
116 XEvent ev;
117 memset( &ev, 0, sizeof(ev) );
118 ev.xfocus.display = QX11Info::display();
119 ev.xfocus.type = FocusIn;
120 ev.xfocus.window = window;
121 ev.xfocus.mode = NotifyNormal;
122 ev.xfocus.detail = NotifyAncestor;
123 XSendEvent( QX11Info::display(), window, False, NoEventMask, &ev );
127 FDialog::exec()
129 static QWidget *current;
131 adjustGeometry();
132 if (!current)
133 secureInputs( QX11Info::display() );
134 show();
135 qApp->processEvents();
136 fakeFocusIn( winId() );
137 QWidget *previous = current;
138 current = this;
139 inherited::exec();
140 current = previous;
141 if (current)
142 fakeFocusIn( current->winId() );
143 else
144 unsecureInputs( QX11Info::display() );
145 return result();
148 KFMsgBox::KFMsgBox( QWidget *parent, QMessageBox::Icon type, const QString &text )
149 : inherited( parent )
151 QLabel *label1 = new QLabel( this );
152 label1->setPixmap( QMessageBox::standardIcon( type ) );
153 QLabel *label2 = new QLabel( text, this );
154 KPushButton *button = new KPushButton( KStandardGuiItem::ok(), this );
155 button->setDefault( true );
156 button->setSizePolicy( QSizePolicy( QSizePolicy::Preferred, QSizePolicy::Preferred ) );
157 connect( button, SIGNAL(clicked()), SLOT(accept()) );
159 QGridLayout *grid = new QGridLayout( this );
160 grid->addWidget( label1, 0, 0, Qt::AlignCenter );
161 grid->addWidget( label2, 0, 1, Qt::AlignCenter );
162 grid->addWidget( button, 1, 0, 1, 2, Qt::AlignCenter );
165 void
166 KFMsgBox::box( QWidget *parent, QMessageBox::Icon type, const QString &text )
168 KFMsgBox dlg( parent, type, text.trimmed() );
169 dlg.exec();