kwin Library API Documentation

preview.cpp

00001 /*
00002  *
00003  * Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
00004  *
00005  *  This program is free software; you can redistribute it and/or modify
00006  *  it under the terms of the GNU General Public License as published by
00007  *  the Free Software Foundation; either version 2 of the License, or
00008  *  (at your option) any later version.
00009  *
00010  *  This program 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
00013  *  GNU General Public License for more details.
00014  *
00015  *  You should have received a copy of the GNU General Public License
00016  *  along with this program; if not, write to the Free Software
00017  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
00018  */
00019 
00020 #include "preview.h"
00021 
00022 #include <kapplication.h>
00023 #include <klocale.h>
00024 #include <kconfig.h>
00025 #include <kglobal.h>
00026 #include <qlabel.h>
00027 #include <qstyle.h>
00028 #include <kiconloader.h>
00029 
00030 #include <X11/Xlib.h>
00031 #include <X11/extensions/shape.h>
00032 
00033 #include <kdecoration_plugins_p.h>
00034 
00035 // FRAME the preview doesn't update to reflect the changes done in the kcm
00036 
00037 KDecorationPreview::KDecorationPreview( QWidget* parent, const char* name )
00038     :   QWidget( parent, name )
00039     {
00040     options = new KDecorationPreviewOptions;
00041     
00042     bridge[Active]   = new KDecorationPreviewBridge( this, true );
00043     bridge[Inactive] = new KDecorationPreviewBridge( this, false );
00044 
00045     deco[Active] = deco[Inactive] = NULL;
00046 
00047     no_preview = new QLabel( i18n( "No preview available.\n"
00048                                    "Most probably there\n"
00049                                    "was a problem loading the plugin." ), this );
00050 
00051     no_preview->setAlignment( AlignCenter );
00052 
00053     setMinimumSize( 100, 100 );
00054     no_preview->resize( size());
00055     }
00056 
00057 KDecorationPreview::~KDecorationPreview()
00058     {
00059     for ( int i = 0; i < NumWindows; i++ )
00060         {
00061         delete deco[i];
00062         delete bridge[i];
00063     }
00064     delete options;
00065     }
00066 
00067 bool KDecorationPreview::recreateDecoration( KDecorationPlugins* plugins )
00068     {
00069     for ( int i = 0; i < NumWindows; i++ )
00070         {
00071         delete deco[i];   // deletes also window
00072         deco[i] = plugins->createDecoration( bridge[i] );
00073         deco[i]->init();
00074         }
00075 
00076     if( deco[Active] == NULL || deco[Inactive] == NULL )
00077         {
00078         return false;
00079         }
00080   
00081     positionPreviews();
00082     deco[Inactive]->widget()->show();
00083     deco[Active]->widget()->show();
00084 
00085     return true;
00086     }
00087 
00088 void KDecorationPreview::enablePreview()
00089     {
00090     no_preview->hide();
00091     }
00092     
00093 void KDecorationPreview::disablePreview()
00094     {
00095     delete deco[Active];
00096     delete deco[Inactive];
00097     deco[Active] = deco[Inactive] = NULL;
00098     no_preview->show();
00099     }
00100 
00101 void KDecorationPreview::resizeEvent( QResizeEvent* e )
00102     {
00103     QWidget::resizeEvent( e );
00104     positionPreviews();
00105     }
00106     
00107 void KDecorationPreview::positionPreviews()
00108     {
00109     int titleBarHeight, leftBorder, rightBorder, xoffset, dummy;
00110     QRect geometry;
00111     QSize size;
00112  
00113     no_preview->resize( this->size() );
00114 
00115     if ( !deco[Active] || !deco[Inactive] )
00116         return; 
00117 
00118     deco[Active]->borders( dummy, dummy, titleBarHeight, dummy );
00119     deco[Inactive]->borders( leftBorder, rightBorder, dummy, dummy );
00120 
00121     titleBarHeight = kMin( int( titleBarHeight * .9 ), 30 );
00122     xoffset = kMin( kMax( 10, QApplication::reverseLayout()
00123                 ? leftBorder : rightBorder ), 30 );
00124 
00125     // Resize the active window
00126     size = QSize( width() - xoffset, height() - titleBarHeight )
00127                 .expandedTo( deco[Active]->minimumSize() );
00128     geometry = QRect( QPoint( 0, titleBarHeight ), size );
00129     deco[Active]->widget()->setGeometry( QStyle::visualRect( geometry, this ) );
00130 
00131     // Resize the inactive window
00132     size = QSize( width() - xoffset, height() - titleBarHeight )
00133                 .expandedTo( deco[Inactive]->minimumSize() );
00134     geometry = QRect( QPoint( xoffset, 0 ), size );
00135     deco[Inactive]->widget()->setGeometry( QStyle::visualRect( geometry, this ) );
00136     }
00137 
00138 void KDecorationPreview::setPreviewMask( const QRegion& reg, int mode, bool active )
00139     {
00140     QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
00141 
00142     // FRAME duped from client.cpp
00143     if( mode == Unsorted )
00144         {
00145         XShapeCombineRegion( qt_xdisplay(), widget->winId(), ShapeBounding, 0, 0,
00146             reg.handle(), ShapeSet );
00147         }
00148     else
00149         {
00150         QMemArray< QRect > rects = reg.rects();
00151         XRectangle* xrects = new XRectangle[ rects.count() ];
00152         for( unsigned int i = 0;
00153              i < rects.count();
00154              ++i )
00155             {
00156             xrects[ i ].x = rects[ i ].x();
00157             xrects[ i ].y = rects[ i ].y();
00158             xrects[ i ].width = rects[ i ].width();
00159             xrects[ i ].height = rects[ i ].height();
00160             }
00161         XShapeCombineRectangles( qt_xdisplay(), widget->winId(), ShapeBounding, 0, 0,
00162         xrects, rects.count(), ShapeSet, mode );
00163         delete[] xrects;
00164         }
00165     if( active )
00166         mask = reg; // keep shape of the active window for unobscuredRegion()
00167     }
00168 
00169 QRect KDecorationPreview::windowGeometry( bool active ) const
00170     {
00171     QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
00172     return widget->geometry();
00173     }
00174 
00175 QRegion KDecorationPreview::unobscuredRegion( bool active, const QRegion& r ) const
00176     {
00177     if( active ) // this one is not obscured
00178         return r;
00179     else
00180         {
00181         // copied from KWin core's code
00182         QRegion ret = r;
00183         QRegion r2 = mask;
00184         if( r2.isEmpty())
00185             r2 = QRegion( windowGeometry( true ));
00186         r2.translate( windowGeometry( true ).x() - windowGeometry( false ).x(),
00187             windowGeometry( true ).y() - windowGeometry( false ).y());
00188         ret -= r2;
00189         return ret;
00190         }
00191     }
00192 
00193 KDecorationPreviewBridge::KDecorationPreviewBridge( KDecorationPreview* p, bool a )
00194     :   preview( p ), active( a )
00195     {
00196     }
00197 
00198 QWidget* KDecorationPreviewBridge::initialParentWidget() const
00199     {
00200     return preview;
00201     }
00202     
00203 Qt::WFlags KDecorationPreviewBridge::initialWFlags() const
00204     {
00205     return 0;
00206     }
00207 
00208 bool KDecorationPreviewBridge::isActive() const
00209     {
00210     return active;
00211     }
00212     
00213 bool KDecorationPreviewBridge::isCloseable() const
00214     {
00215     return true;
00216     }
00217     
00218 bool KDecorationPreviewBridge::isMaximizable() const
00219     {
00220     return true;
00221     }
00222     
00223 KDecoration::MaximizeMode KDecorationPreviewBridge::maximizeMode() const
00224     {
00225     return KDecoration::MaximizeRestore;
00226     }
00227     
00228 bool KDecorationPreviewBridge::isMinimizable() const
00229     {
00230     return true;
00231     }
00232     
00233 bool KDecorationPreviewBridge::providesContextHelp() const
00234     {
00235     return true;
00236     }
00237     
00238 int KDecorationPreviewBridge::desktop() const
00239     {
00240     return 1;
00241     }
00242     
00243 bool KDecorationPreviewBridge::isModal() const
00244     {
00245     return false;
00246     }
00247     
00248 bool KDecorationPreviewBridge::isShadeable() const
00249     {
00250     return true;
00251     }
00252     
00253 bool KDecorationPreviewBridge::isShade() const
00254     {
00255     return false;
00256     }
00257     
00258 bool KDecorationPreviewBridge::isSetShade() const
00259     {
00260     return false;
00261     }
00262     
00263 bool KDecorationPreviewBridge::keepAbove() const
00264     {
00265     return false;
00266     }
00267     
00268 bool KDecorationPreviewBridge::keepBelow() const
00269     {
00270     return false;
00271     }
00272     
00273 bool KDecorationPreviewBridge::isMovable() const
00274     {
00275     return true;
00276     }
00277     
00278 bool KDecorationPreviewBridge::isResizable() const
00279     {
00280     return true;
00281     }
00282     
00283 NET::WindowType KDecorationPreviewBridge::windowType( unsigned long ) const
00284     {
00285     return NET::Normal;
00286     }
00287     
00288 QIconSet KDecorationPreviewBridge::icon() const
00289     {
00290     return SmallIconSet( "xapp" );
00291     }
00292 
00293 QString KDecorationPreviewBridge::caption() const
00294     {
00295     return active ? i18n( "Active window" ) : i18n( "Inactive window" );
00296     }
00297     
00298 void KDecorationPreviewBridge::processMousePressEvent( QMouseEvent* )
00299     {
00300     }
00301     
00302 void KDecorationPreviewBridge::showWindowMenu( QPoint )
00303     {
00304     }
00305     
00306 void KDecorationPreviewBridge::performWindowOperation( WindowOperation )
00307     {
00308     }
00309     
00310 void KDecorationPreviewBridge::setMask( const QRegion& reg, int mode )
00311     {
00312     preview->setPreviewMask( reg, mode, active );
00313     }
00314     
00315 bool KDecorationPreviewBridge::isPreview() const
00316     {
00317     return true;
00318     }
00319 
00320 QRect KDecorationPreviewBridge::geometry() const
00321     {
00322     return preview->windowGeometry( active );
00323     }
00324         
00325 QRect KDecorationPreviewBridge::iconGeometry() const
00326     {
00327     return QRect();
00328     }
00329 
00330 QRegion KDecorationPreviewBridge::unobscuredRegion( const QRegion& r ) const
00331     {
00332     return preview->unobscuredRegion( active, r );
00333     }
00334 
00335 QWidget* KDecorationPreviewBridge::workspaceWidget() const
00336     {
00337     return preview;
00338     }
00339         
00340 void KDecorationPreviewBridge::closeWindow()
00341     {
00342     }
00343     
00344 void KDecorationPreviewBridge::maximize( MaximizeMode )
00345     {
00346     }
00347     
00348 void KDecorationPreviewBridge::minimize()
00349     {
00350     }
00351     
00352 void KDecorationPreviewBridge::showContextHelp()
00353     {
00354     }
00355     
00356 void KDecorationPreviewBridge::setDesktop( int )
00357     {
00358     }
00359     
00360 void KDecorationPreviewBridge::titlebarDblClickOperation()
00361     {
00362     }
00363     
00364 void KDecorationPreviewBridge::setShade( bool )
00365     {
00366     }
00367     
00368 void KDecorationPreviewBridge::setKeepAbove( bool )
00369     {
00370     }
00371     
00372 void KDecorationPreviewBridge::setKeepBelow( bool )
00373     {
00374     }
00375     
00376 int KDecorationPreviewBridge::currentDesktop() const
00377     {
00378     return 1;
00379     }
00380 
00381 void KDecorationPreviewBridge::helperShowHide( bool )
00382     {
00383     }
00384 
00385 void KDecorationPreviewBridge::grabXServer( bool )
00386     {
00387     }
00388 
00389 KDecorationPreviewOptions::KDecorationPreviewOptions()
00390     {
00391     d = new KDecorationOptionsPrivate;
00392     d->defaultKWinSettings();
00393     updateSettings();
00394     }
00395 
00396 KDecorationPreviewOptions::~KDecorationPreviewOptions()
00397     {
00398     delete d;
00399     }
00400 
00401 unsigned long KDecorationPreviewOptions::updateSettings()
00402     {
00403     KConfig cfg( "kwinrc", true );
00404     unsigned long changed = 0;
00405     changed |= d->updateKWinSettings( &cfg );
00406     return changed;
00407     }
00408 
00409 bool KDecorationPreviewPlugins::provides( Requirement )
00410     {
00411     return false;
00412     }
00413     
00414 #include "preview.moc"
KDE Logo
This file is part of the documentation for kwin Library Version 3.2.1.
Documentation copyright © 1996-2004 the KDE developers.
Generated on Fri Mar 5 04:41:14 2004 by doxygen 1.3.6-20040222 written by Dimitri van Heesch, © 1997-2003