00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
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 <kdecorationfactory.h>
00034 #include <kdecoration_plugins_p.h>
00035
00036
00037
00038 KDecorationPreview::KDecorationPreview( QWidget* parent, const char* name )
00039 : QWidget( parent, name )
00040 {
00041 options = new KDecorationPreviewOptions;
00042
00043 bridge[Active] = new KDecorationPreviewBridge( this, true );
00044 bridge[Inactive] = new KDecorationPreviewBridge( this, false );
00045
00046 deco[Active] = deco[Inactive] = NULL;
00047
00048 no_preview = new QLabel( i18n( "No preview available.\n"
00049 "Most probably there\n"
00050 "was a problem loading the plugin." ), this );
00051
00052 no_preview->setAlignment( AlignCenter );
00053
00054 setMinimumSize( 100, 100 );
00055 no_preview->resize( size());
00056 }
00057
00058 KDecorationPreview::~KDecorationPreview()
00059 {
00060 for ( int i = 0; i < NumWindows; i++ )
00061 {
00062 delete deco[i];
00063 delete bridge[i];
00064 }
00065 delete options;
00066 }
00067
00068 bool KDecorationPreview::recreateDecoration( KDecorationPlugins* plugins )
00069 {
00070 for ( int i = 0; i < NumWindows; i++ )
00071 {
00072 delete deco[i];
00073 deco[i] = plugins->createDecoration( bridge[i] );
00074 deco[i]->init();
00075 }
00076
00077 if( deco[Active] == NULL || deco[Inactive] == NULL )
00078 {
00079 return false;
00080 }
00081
00082 positionPreviews();
00083 deco[Inactive]->widget()->show();
00084 deco[Active]->widget()->show();
00085
00086 return true;
00087 }
00088
00089 void KDecorationPreview::enablePreview()
00090 {
00091 no_preview->hide();
00092 }
00093
00094 void KDecorationPreview::disablePreview()
00095 {
00096 delete deco[Active];
00097 delete deco[Inactive];
00098 deco[Active] = deco[Inactive] = NULL;
00099 no_preview->show();
00100 }
00101
00102 void KDecorationPreview::resizeEvent( QResizeEvent* e )
00103 {
00104 QWidget::resizeEvent( e );
00105 positionPreviews();
00106 }
00107
00108 void KDecorationPreview::positionPreviews()
00109 {
00110 int titleBarHeight, leftBorder, rightBorder, xoffset,
00111 dummy1, dummy2, dummy3;
00112 QRect geometry;
00113 QSize size;
00114
00115 no_preview->resize( this->size() );
00116
00117 if ( !deco[Active] || !deco[Inactive] )
00118 return;
00119
00120
00121 deco[Active]->borders( dummy1, dummy2, titleBarHeight, dummy3 );
00122 deco[Inactive]->borders( leftBorder, rightBorder, dummy1, dummy2 );
00123
00124 titleBarHeight = kMin( int( titleBarHeight * .9 ), 30 );
00125 xoffset = kMin( kMax( 10, QApplication::reverseLayout()
00126 ? leftBorder : rightBorder ), 30 );
00127
00128
00129 size = QSize( width() - xoffset, height() - titleBarHeight )
00130 .expandedTo( deco[Active]->minimumSize() );
00131 geometry = QRect( QPoint( 0, titleBarHeight ), size );
00132 deco[Active]->widget()->setGeometry( QStyle::visualRect( geometry, this ) );
00133
00134
00135 size = QSize( width() - xoffset, height() - titleBarHeight )
00136 .expandedTo( deco[Inactive]->minimumSize() );
00137 geometry = QRect( QPoint( xoffset, 0 ), size );
00138 deco[Inactive]->widget()->setGeometry( QStyle::visualRect( geometry, this ) );
00139 }
00140
00141 void KDecorationPreview::setPreviewMask( const QRegion& reg, int mode, bool active )
00142 {
00143 QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
00144
00145
00146 if( mode == Unsorted )
00147 {
00148 XShapeCombineRegion( qt_xdisplay(), widget->winId(), ShapeBounding, 0, 0,
00149 reg.handle(), ShapeSet );
00150 }
00151 else
00152 {
00153 QMemArray< QRect > rects = reg.rects();
00154 XRectangle* xrects = new XRectangle[ rects.count() ];
00155 for( unsigned int i = 0;
00156 i < rects.count();
00157 ++i )
00158 {
00159 xrects[ i ].x = rects[ i ].x();
00160 xrects[ i ].y = rects[ i ].y();
00161 xrects[ i ].width = rects[ i ].width();
00162 xrects[ i ].height = rects[ i ].height();
00163 }
00164 XShapeCombineRectangles( qt_xdisplay(), widget->winId(), ShapeBounding, 0, 0,
00165 xrects, rects.count(), ShapeSet, mode );
00166 delete[] xrects;
00167 }
00168 if( active )
00169 mask = reg;
00170 }
00171
00172 QRect KDecorationPreview::windowGeometry( bool active ) const
00173 {
00174 QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
00175 return widget->geometry();
00176 }
00177
00178 void KDecorationPreview::setTempBorderSize(KDecorationPlugins* plugin, KDecorationDefines::BorderSize size)
00179 {
00180 options->setCustomBorderSize(size);
00181 if (plugin->factory()->reset(KDecorationDefines::SettingBorder) )
00182 {
00183
00184 recreateDecoration(plugin);
00185 }
00186 else
00187 {
00188
00189 positionPreviews();
00190 }
00191 }
00192
00193 void KDecorationPreview::setTempButtons(KDecorationPlugins* plugin, bool customEnabled, const QString &left, const QString &right)
00194 {
00195 options->setCustomTitleButtonsEnabled(customEnabled);
00196 options->setCustomTitleButtons(left, right);
00197 if (plugin->factory()->reset(KDecorationDefines::SettingButtons) )
00198 {
00199
00200 recreateDecoration(plugin);
00201 }
00202 else
00203 {
00204
00205 positionPreviews();
00206 }
00207 }
00208
00209 QRegion KDecorationPreview::unobscuredRegion( bool active, const QRegion& r ) const
00210 {
00211 if( active )
00212 return r;
00213 else
00214 {
00215
00216 QRegion ret = r;
00217 QRegion r2 = mask;
00218 if( r2.isEmpty())
00219 r2 = QRegion( windowGeometry( true ));
00220 r2.translate( windowGeometry( true ).x() - windowGeometry( false ).x(),
00221 windowGeometry( true ).y() - windowGeometry( false ).y());
00222 ret -= r2;
00223 return ret;
00224 }
00225 }
00226
00227 KDecorationPreviewBridge::KDecorationPreviewBridge( KDecorationPreview* p, bool a )
00228 : preview( p ), active( a )
00229 {
00230 }
00231
00232 QWidget* KDecorationPreviewBridge::initialParentWidget() const
00233 {
00234 return preview;
00235 }
00236
00237 Qt::WFlags KDecorationPreviewBridge::initialWFlags() const
00238 {
00239 return 0;
00240 }
00241
00242 bool KDecorationPreviewBridge::isActive() const
00243 {
00244 return active;
00245 }
00246
00247 bool KDecorationPreviewBridge::isCloseable() const
00248 {
00249 return true;
00250 }
00251
00252 bool KDecorationPreviewBridge::isMaximizable() const
00253 {
00254 return true;
00255 }
00256
00257 KDecoration::MaximizeMode KDecorationPreviewBridge::maximizeMode() const
00258 {
00259 return KDecoration::MaximizeRestore;
00260 }
00261
00262 bool KDecorationPreviewBridge::isMinimizable() const
00263 {
00264 return true;
00265 }
00266
00267 bool KDecorationPreviewBridge::providesContextHelp() const
00268 {
00269 return true;
00270 }
00271
00272 int KDecorationPreviewBridge::desktop() const
00273 {
00274 return 1;
00275 }
00276
00277 bool KDecorationPreviewBridge::isModal() const
00278 {
00279 return false;
00280 }
00281
00282 bool KDecorationPreviewBridge::isShadeable() const
00283 {
00284 return true;
00285 }
00286
00287 bool KDecorationPreviewBridge::isShade() const
00288 {
00289 return false;
00290 }
00291
00292 bool KDecorationPreviewBridge::isSetShade() const
00293 {
00294 return false;
00295 }
00296
00297 bool KDecorationPreviewBridge::keepAbove() const
00298 {
00299 return false;
00300 }
00301
00302 bool KDecorationPreviewBridge::keepBelow() const
00303 {
00304 return false;
00305 }
00306
00307 bool KDecorationPreviewBridge::isMovable() const
00308 {
00309 return true;
00310 }
00311
00312 bool KDecorationPreviewBridge::isResizable() const
00313 {
00314 return true;
00315 }
00316
00317 NET::WindowType KDecorationPreviewBridge::windowType( unsigned long ) const
00318 {
00319 return NET::Normal;
00320 }
00321
00322 QIconSet KDecorationPreviewBridge::icon() const
00323 {
00324 return SmallIconSet( "xapp" );
00325 }
00326
00327 QString KDecorationPreviewBridge::caption() const
00328 {
00329 return active ? i18n( "Active Window" ) : i18n( "Inactive Window" );
00330 }
00331
00332 void KDecorationPreviewBridge::processMousePressEvent( QMouseEvent* )
00333 {
00334 }
00335
00336 void KDecorationPreviewBridge::showWindowMenu( const QRect &)
00337 {
00338 }
00339
00340 void KDecorationPreviewBridge::showWindowMenu( QPoint )
00341 {
00342 }
00343
00344 void KDecorationPreviewBridge::performWindowOperation( WindowOperation )
00345 {
00346 }
00347
00348 void KDecorationPreviewBridge::setMask( const QRegion& reg, int mode )
00349 {
00350 preview->setPreviewMask( reg, mode, active );
00351 }
00352
00353 bool KDecorationPreviewBridge::isPreview() const
00354 {
00355 return true;
00356 }
00357
00358 QRect KDecorationPreviewBridge::geometry() const
00359 {
00360 return preview->windowGeometry( active );
00361 }
00362
00363 QRect KDecorationPreviewBridge::iconGeometry() const
00364 {
00365 return QRect();
00366 }
00367
00368 QRegion KDecorationPreviewBridge::unobscuredRegion( const QRegion& r ) const
00369 {
00370 return preview->unobscuredRegion( active, r );
00371 }
00372
00373 QWidget* KDecorationPreviewBridge::workspaceWidget() const
00374 {
00375 return preview;
00376 }
00377
00378 WId KDecorationPreviewBridge::windowId() const
00379 {
00380 return 0;
00381 }
00382
00383 void KDecorationPreviewBridge::closeWindow()
00384 {
00385 }
00386
00387 void KDecorationPreviewBridge::maximize( MaximizeMode )
00388 {
00389 }
00390
00391 void KDecorationPreviewBridge::minimize()
00392 {
00393 }
00394
00395 void KDecorationPreviewBridge::showContextHelp()
00396 {
00397 }
00398
00399 void KDecorationPreviewBridge::setDesktop( int )
00400 {
00401 }
00402
00403 void KDecorationPreviewBridge::titlebarDblClickOperation()
00404 {
00405 }
00406
00407 void KDecorationPreviewBridge::setShade( bool )
00408 {
00409 }
00410
00411 void KDecorationPreviewBridge::setKeepAbove( bool )
00412 {
00413 }
00414
00415 void KDecorationPreviewBridge::setKeepBelow( bool )
00416 {
00417 }
00418
00419 int KDecorationPreviewBridge::currentDesktop() const
00420 {
00421 return 1;
00422 }
00423
00424 void KDecorationPreviewBridge::helperShowHide( bool )
00425 {
00426 }
00427
00428 void KDecorationPreviewBridge::grabXServer( bool )
00429 {
00430 }
00431
00432 KDecorationPreviewOptions::KDecorationPreviewOptions()
00433 {
00434 customBorderSize = BordersCount;
00435 customButtonsChanged = false;
00436 customButtons = true;
00437 customTitleButtonsLeft = QString::null;
00438 customTitleButtonsRight = QString::null;
00439
00440 d = new KDecorationOptionsPrivate;
00441 d->defaultKWinSettings();
00442 updateSettings();
00443 }
00444
00445 KDecorationPreviewOptions::~KDecorationPreviewOptions()
00446 {
00447 delete d;
00448 }
00449
00450 unsigned long KDecorationPreviewOptions::updateSettings()
00451 {
00452 KConfig cfg( "kwinrc", true );
00453 unsigned long changed = 0;
00454 changed |= d->updateKWinSettings( &cfg );
00455
00456
00457 if (customBorderSize != BordersCount)
00458 d->border_size = customBorderSize;
00459 if (customButtonsChanged)
00460 d->custom_button_positions = customButtons;
00461 if (customButtons) {
00462 if (!customTitleButtonsLeft.isNull() )
00463 d->title_buttons_left = customTitleButtonsLeft;
00464 if (!customTitleButtonsRight.isNull() )
00465 d->title_buttons_right = customTitleButtonsRight;
00466 } else {
00467 d->title_buttons_left = "MS";
00468 d->title_buttons_right = "HIAX";
00469 }
00470
00471 return changed;
00472 }
00473
00474 void KDecorationPreviewOptions::setCustomBorderSize(BorderSize size)
00475 {
00476 customBorderSize = size;
00477
00478 updateSettings();
00479 }
00480
00481 void KDecorationPreviewOptions::setCustomTitleButtonsEnabled(bool enabled)
00482 {
00483 customButtonsChanged = true;
00484 customButtons = enabled;
00485
00486 updateSettings();
00487 }
00488
00489 void KDecorationPreviewOptions::setCustomTitleButtons(const QString &left, const QString &right)
00490 {
00491 customTitleButtonsLeft = left;
00492 customTitleButtonsRight = right;
00493
00494 updateSettings();
00495 }
00496
00497 bool KDecorationPreviewPlugins::provides( Requirement )
00498 {
00499 return false;
00500 }
00501
00502 #include "preview.moc"