tdescreensaver.cpp
00001 /* This file is part of the KDE libraries 00002 00003 Copyright (c) 2001 Martin R. Jones <mjones@kde.org> 00004 00005 This library is free software; you can redistribute it and/or 00006 modify it under the terms of the GNU Library General Public 00007 License as published by the Free Software Foundation; either 00008 version 2 of the License, or (at your option) any later version. 00009 00010 This library is distributed in the hope that it will be useful, 00011 but WITHOUT ANY WARRANTY; without even the implied warranty of 00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00013 Library General Public License for more details. 00014 00015 You should have received a copy of the GNU Library General Public License 00016 along with this library; see the file COPYING.LIB. If not, write to 00017 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 00018 Boston, MA 02110-1301, USA. 00019 */ 00020 00021 #include <tqpainter.h> 00022 #include <tqtimer.h> 00023 #include <tdeapplication.h> 00024 #include "tdescreensaver.h" 00025 #ifdef Q_WS_X11 00026 #include <X11/Xlib.h> 00027 #else 00028 typedef WId Window; 00029 #endif 00030 00031 #undef KeyPress 00032 #undef KeyRelease 00033 00034 //----------------------------------------------------------------------------- 00035 00036 class KScreenSaverPrivate 00037 { 00038 public: 00039 TQWidget *owner; 00040 }; 00041 00042 KScreenSaver::KScreenSaver( WId id ) : TQWidget() 00043 { 00044 Window root; 00045 int ai; 00046 unsigned int au; 00047 unsigned int w = 0; 00048 unsigned int h = 0; 00049 00050 XSync(tqt_xdisplay(), false); 00051 00052 d = new KScreenSaverPrivate; 00053 d->owner = TQT_TQWIDGET(find( id )); 00054 if ( d->owner ) 00055 installEventFilter( this ); 00056 00057 if ( id ) 00058 { 00059 #ifdef Q_WS_X11 //FIXME 00060 XGetGeometry(tqt_xdisplay(), (Drawable)id, &root, &ai, &ai, 00061 &w, &h, &au, &au); 00062 #endif 00063 00064 create( id, false, true ); 00065 } 00066 00067 if ( w == 0 ) w = 600; 00068 if ( h == 0 ) h = 420; 00069 resize( w, h ); 00070 TDEApplication::sendPostedEvents(); 00071 show(); 00072 } 00073 00074 KScreenSaver::~KScreenSaver() 00075 { 00076 destroy( false, false ); 00077 delete d; 00078 } 00079 00080 void KScreenSaver::embed( TQWidget *w ) 00081 { 00082 TDEApplication::sendPostedEvents(); 00083 #ifdef Q_WS_X11 //FIXME 00084 XReparentWindow(tqt_xdisplay(), w->winId(), winId(), 0, 0); 00085 #endif 00086 w->setGeometry( 0, 0, width(), height() ); 00087 TDEApplication::sendPostedEvents(); 00088 } 00089 00090 bool KScreenSaver::eventFilter( TQObject *o, TQEvent *e ) 00091 { 00092 // make sure events get to the original window owner 00093 if ( d->owner && TQT_BASE_OBJECT(o) == TQT_BASE_OBJECT(this) ) { 00094 TQApplication::sendEvent( d->owner, e ); 00095 return false; 00096 } 00097 00098 return TQWidget::eventFilter( o, e ); 00099 } 00100 00101 //============================================================================ 00102 00103 class KBlankEffectPrivate 00104 { 00105 public: 00106 KBlankEffect::BlankEffect currentEffect; 00107 int effectProgress; 00108 TQTimer *timer; 00109 TQWidget *widget; 00110 }; 00111 00112 KBlankEffect::BlankEffect KBlankEffect::effects[] = { 00113 &KBlankEffect::blankNormal, 00114 &KBlankEffect::blankSweepRight, 00115 &KBlankEffect::blankSweepDown, 00116 &KBlankEffect::blankBlocks 00117 }; 00118 00119 KBlankEffect::KBlankEffect( TQObject *parent ) : TQObject( parent ) 00120 { 00121 d = new KBlankEffectPrivate; 00122 d->currentEffect = &KBlankEffect::blankNormal; 00123 d->effectProgress = 0; 00124 d->timer = new TQTimer( this ); 00125 connect( d->timer, TQT_SIGNAL(timeout()), this, TQT_SLOT(timeout()) ); 00126 } 00127 00128 00129 KBlankEffect::~KBlankEffect() 00130 { 00131 delete d; 00132 } 00133 00134 void KBlankEffect::finished() 00135 { 00136 d->timer->stop(); 00137 d->effectProgress = 0; 00138 emit doneBlank(); 00139 } 00140 00141 00142 void KBlankEffect::blank( TQWidget *w, Effect effect ) 00143 { 00144 if ( !w ) { 00145 emit doneBlank(); 00146 return; 00147 } 00148 00149 if ( effect == Random ) 00150 effect = (Effect)(kapp->random() % MaximumEffects); 00151 00152 d->effectProgress = 0; 00153 d->widget = w; 00154 d->currentEffect = effects[ (int)effect ]; 00155 d->timer->start( 10 ); 00156 } 00157 00158 void KBlankEffect::timeout() 00159 { 00160 (this->*d->currentEffect)(); 00161 } 00162 00163 void KBlankEffect::blankNormal() 00164 { 00165 TQPainter p( d->widget ); 00166 p.fillRect( 0, 0, d->widget->width(), d->widget->height(), black ); 00167 finished(); 00168 } 00169 00170 00171 void KBlankEffect::blankSweepRight() 00172 { 00173 TQPainter p( d->widget ); 00174 p.fillRect( d->effectProgress, 0, 50, d->widget->height(), black ); 00175 kapp->flushX(); 00176 d->effectProgress += 50; 00177 if ( d->effectProgress >= d->widget->width() ) 00178 finished(); 00179 } 00180 00181 00182 void KBlankEffect::blankSweepDown() 00183 { 00184 TQPainter p( d->widget ); 00185 p.fillRect( 0, d->effectProgress, d->widget->width(), 50, black ); 00186 kapp->flushX(); 00187 d->effectProgress += 50; 00188 if ( d->effectProgress >= d->widget->height() ) 00189 finished(); 00190 } 00191 00192 00193 void KBlankEffect::blankBlocks() 00194 { 00195 static int *block = 0; 00196 00197 int bx = (d->widget->width()+63)/64; 00198 int by = (d->widget->height()+63)/64; 00199 00200 if ( !d->effectProgress ) { 00201 block = new int [ bx*by ]; 00202 00203 for ( int i = 0; i < bx*by; i++ ) 00204 block[i] = i; 00205 for ( int i = 0; i < bx*by; i++ ) { 00206 int swap = kapp->random()%(bx*by); 00207 int tmp = block[i]; 00208 block[i] = block[swap]; 00209 block[swap] = tmp; 00210 } 00211 } 00212 00213 TQPainter p( d->widget ); 00214 00215 // erase a couple of blocks at a time, otherwise it looks too slow 00216 for ( int i = 0; i < 2 && d->effectProgress < bx*by; i++ ) { 00217 int x = block[d->effectProgress]%bx; 00218 int y = block[d->effectProgress]/bx; 00219 p.fillRect( x*64, y*64, 64, 64, black ); 00220 d->effectProgress++; 00221 } 00222 00223 kapp->flushX(); 00224 00225 if ( d->effectProgress >= bx*by ) { 00226 delete[] block; 00227 finished(); 00228 } 00229 } 00230 00231 #include "tdescreensaver.moc"