add more spacing
[personal-kdebase.git] / workspace / kwin / kcmkwin / kwindecoration / preview.cpp
blob938cb2a154f021d64693531fec1a58ace55783ef
1 /*
3 * Copyright (c) 2003 Lubos Lunak <l.lunak@kde.org>
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 #include "preview.h"
22 #include <kapplication.h>
23 #include <klocale.h>
24 #include <kconfig.h>
25 #include <kglobal.h>
26 #include <QLabel>
27 #include <QStyle>
28 //Added by qt3to4:
29 #include <QMouseEvent>
30 #include <QResizeEvent>
31 #include <QVector>
32 #include <kicon.h>
34 #include <X11/Xlib.h>
35 #include <X11/extensions/shape.h>
37 #include <kdecorationfactory.h>
38 #include <kdecoration_plugins_p.h>
39 #include <QX11Info>
41 // FRAME the preview doesn't update to reflect the changes done in the kcm
43 KDecorationPreview::KDecorationPreview( QWidget* parent )
44 : QWidget( parent )
46 options = new KDecorationPreviewOptions;
48 bridge[Active] = new KDecorationPreviewBridge( this, true );
49 bridge[Inactive] = new KDecorationPreviewBridge( this, false );
51 deco[Active] = deco[Inactive] = NULL;
53 no_preview = new QLabel( i18n( "No preview available.\n"
54 "Most probably there\n"
55 "was a problem loading the plugin." ), this );
57 no_preview->setAlignment( Qt::AlignCenter );
59 setMinimumSize( 100, 100 );
60 no_preview->resize( size());
63 KDecorationPreview::~KDecorationPreview()
65 for ( int i = 0; i < NumWindows; i++ )
67 delete deco[i];
68 delete bridge[i];
70 delete options;
73 bool KDecorationPreview::recreateDecoration( KDecorationPlugins* plugins )
75 for ( int i = 0; i < NumWindows; i++ )
77 delete deco[i]; // deletes also window
78 deco[i] = plugins->createDecoration( bridge[i] );
79 deco[i]->init();
82 if( deco[Active] == NULL || deco[Inactive] == NULL )
84 return false;
87 positionPreviews();
88 deco[Inactive]->widget()->show();
89 deco[Active]->widget()->show();
91 return true;
94 void KDecorationPreview::enablePreview()
96 no_preview->hide();
99 void KDecorationPreview::disablePreview()
101 delete deco[Active];
102 delete deco[Inactive];
103 deco[Active] = deco[Inactive] = NULL;
104 no_preview->show();
107 void KDecorationPreview::resizeEvent( QResizeEvent* e )
109 QWidget::resizeEvent( e );
110 positionPreviews();
113 void KDecorationPreview::positionPreviews()
115 int titleBarHeight, leftBorder, rightBorder, xoffset,
116 dummy1, dummy2, dummy3;
117 QRect geometry;
118 QSize size;
120 no_preview->resize( this->size() );
122 if ( !deco[Active] || !deco[Inactive] )
123 return;
125 // don't have more than one reference to the same dummy variable in one borders() call.
126 deco[Active]->borders( dummy1, dummy2, titleBarHeight, dummy3 );
127 deco[Inactive]->borders( leftBorder, rightBorder, dummy1, dummy2 );
129 titleBarHeight = qMin( int( titleBarHeight * .9 ), 30 );
130 xoffset = qMin( qMax( 10, QApplication::isRightToLeft()
131 ? leftBorder : rightBorder ), 30 );
133 // Resize the active window
134 size = QSize( width() - xoffset, height() - titleBarHeight )
135 .expandedTo( deco[Active]->minimumSize() );
136 geometry = QRect( QPoint( 0, titleBarHeight ), size );
137 deco[Active]->widget()->setGeometry( QStyle::visualRect( this->layoutDirection(), this->rect(), geometry ) );
139 // Resize the inactive window
140 size = QSize( width() - xoffset, height() - titleBarHeight )
141 .expandedTo( deco[Inactive]->minimumSize() );
142 geometry = QRect( QPoint( xoffset, 0 ), size );
143 deco[Inactive]->widget()->setGeometry( QStyle::visualRect( this->layoutDirection(), this->rect(), geometry ) );
146 void KDecorationPreview::setPreviewMask( const QRegion& reg, int mode, bool active )
148 QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
150 // FRAME duped from client.cpp
151 if( mode == Unsorted )
153 XShapeCombineRegion( QX11Info::display(), widget->winId(), ShapeBounding, 0, 0,
154 reg.handle(), ShapeSet );
156 else
158 QVector< QRect > rects = reg.rects();
159 XRectangle* xrects = new XRectangle[ rects.count() ];
160 for( int i = 0;
161 i < rects.count();
162 ++i )
164 xrects[ i ].x = rects[ i ].x();
165 xrects[ i ].y = rects[ i ].y();
166 xrects[ i ].width = rects[ i ].width();
167 xrects[ i ].height = rects[ i ].height();
169 XShapeCombineRectangles( QX11Info::display(), widget->winId(), ShapeBounding, 0, 0,
170 xrects, rects.count(), ShapeSet, mode );
171 delete[] xrects;
173 if( active )
174 mask = reg; // keep shape of the active window for unobscuredRegion()
177 QRect KDecorationPreview::windowGeometry( bool active ) const
179 QWidget *widget = active ? deco[Active]->widget() : deco[Inactive]->widget();
180 return widget->geometry();
183 void KDecorationPreview::setTempBorderSize(KDecorationPlugins* plugin, KDecorationDefines::BorderSize size)
185 options->setCustomBorderSize(size);
186 if (plugin->factory()->reset(KDecorationDefines::SettingBorder) )
188 // can't handle the change, recreate decorations then
189 recreateDecoration(plugin);
191 else
193 // handles the update, only update position...
194 positionPreviews();
198 void KDecorationPreview::setTempButtons(KDecorationPlugins* plugin, bool customEnabled, const QString &left, const QString &right)
200 options->setCustomTitleButtonsEnabled(customEnabled);
201 options->setCustomTitleButtons(left, right);
202 if (plugin->factory()->reset(KDecorationDefines::SettingButtons) )
204 // can't handle the change, recreate decorations then
205 recreateDecoration(plugin);
207 else
209 // handles the update, only update position...
210 positionPreviews();
214 QRegion KDecorationPreview::unobscuredRegion( bool active, const QRegion& r ) const
216 if( active ) // this one is not obscured
217 return r;
218 else
220 // copied from KWin core's code
221 QRegion ret = r;
222 QRegion r2 = mask;
223 if( r2.isEmpty())
224 r2 = QRegion( windowGeometry( true ));
225 r2.translate( windowGeometry( true ).x() - windowGeometry( false ).x(),
226 windowGeometry( true ).y() - windowGeometry( false ).y());
227 ret -= r2;
228 return ret;
232 KDecorationPreviewBridge::KDecorationPreviewBridge( KDecorationPreview* p, bool a )
233 : preview( p ), active( a )
237 QWidget* KDecorationPreviewBridge::initialParentWidget() const
239 return preview;
242 Qt::WFlags KDecorationPreviewBridge::initialWFlags() const
244 return 0;
247 bool KDecorationPreviewBridge::isActive() const
249 return active;
252 bool KDecorationPreviewBridge::isCloseable() const
254 return true;
257 bool KDecorationPreviewBridge::isMaximizable() const
259 return true;
262 KDecoration::MaximizeMode KDecorationPreviewBridge::maximizeMode() const
264 return KDecoration::MaximizeRestore;
267 bool KDecorationPreviewBridge::isMinimizable() const
269 return true;
272 bool KDecorationPreviewBridge::providesContextHelp() const
274 return true;
277 int KDecorationPreviewBridge::desktop() const
279 return 1;
282 bool KDecorationPreviewBridge::isModal() const
284 return false;
287 bool KDecorationPreviewBridge::isShadeable() const
289 return true;
292 bool KDecorationPreviewBridge::isShade() const
294 return false;
297 bool KDecorationPreviewBridge::isSetShade() const
299 return false;
302 bool KDecorationPreviewBridge::keepAbove() const
304 return false;
307 bool KDecorationPreviewBridge::keepBelow() const
309 return false;
312 bool KDecorationPreviewBridge::isMovable() const
314 return true;
317 bool KDecorationPreviewBridge::isResizable() const
319 return true;
322 NET::WindowType KDecorationPreviewBridge::windowType( unsigned long ) const
324 return NET::Normal;
327 QIcon KDecorationPreviewBridge::icon() const
329 return KIcon( "xorg" );
332 QString KDecorationPreviewBridge::caption() const
334 return active ? i18n( "Active Window" ) : i18n( "Inactive Window" );
337 void KDecorationPreviewBridge::processMousePressEvent( QMouseEvent* )
341 void KDecorationPreviewBridge::showWindowMenu( const QRect &)
345 void KDecorationPreviewBridge::showWindowMenu( const QPoint & )
349 void KDecorationPreviewBridge::performWindowOperation( WindowOperation )
353 void KDecorationPreviewBridge::setMask( const QRegion& reg, int mode )
355 preview->setPreviewMask( reg, mode, active );
358 bool KDecorationPreviewBridge::isPreview() const
360 return true;
363 QRect KDecorationPreviewBridge::geometry() const
365 return preview->windowGeometry( active );
368 QRect KDecorationPreviewBridge::iconGeometry() const
370 return QRect();
373 QRegion KDecorationPreviewBridge::unobscuredRegion( const QRegion& r ) const
375 return preview->unobscuredRegion( active, r );
378 WId KDecorationPreviewBridge::windowId() const
380 return 0; // no decorated window
383 void KDecorationPreviewBridge::closeWindow()
387 void KDecorationPreviewBridge::maximize( MaximizeMode )
391 void KDecorationPreviewBridge::minimize()
395 void KDecorationPreviewBridge::showContextHelp()
399 void KDecorationPreviewBridge::setDesktop( int )
403 void KDecorationPreviewBridge::titlebarDblClickOperation()
407 void KDecorationPreviewBridge::titlebarMouseWheelOperation( int )
411 void KDecorationPreviewBridge::setShade( bool )
415 void KDecorationPreviewBridge::setKeepAbove( bool )
419 void KDecorationPreviewBridge::setKeepBelow( bool )
423 int KDecorationPreviewBridge::currentDesktop() const
425 return 1;
428 void KDecorationPreviewBridge::grabXServer( bool )
432 void KDecorationPreviewBridge::repaintShadow()
436 bool KDecorationPreviewBridge::compositingActive() const
438 return false;
441 bool KDecorationPreviewBridge::shadowsActive() const
443 return false;
446 double KDecorationPreviewBridge::opacity() const
448 return 1.0;
451 KDecorationPreviewOptions::KDecorationPreviewOptions()
453 customBorderSize = BordersCount; // invalid
454 customButtonsChanged = false; // invalid
455 customButtons = true;
456 customTitleButtonsLeft.clear(); // invalid
457 customTitleButtonsRight.clear(); // invalid
458 updateSettings();
461 KDecorationPreviewOptions::~KDecorationPreviewOptions()
465 unsigned long KDecorationPreviewOptions::updateSettings()
467 KConfig cfg( "kwinrc" );
468 unsigned long changed = 0;
469 changed |= KDecorationOptions::updateSettings( &cfg );
471 // set custom border size/buttons
472 if (customBorderSize != BordersCount)
473 setBorderSize( customBorderSize );
474 if (customButtonsChanged)
475 setCustomButtonPositions( customButtons );
476 if (customButtons) {
477 if (!customTitleButtonsLeft.isNull() )
478 setTitleButtonsLeft( customTitleButtonsLeft );
479 if (!customTitleButtonsRight.isNull() )
480 setTitleButtonsRight( customTitleButtonsRight );
481 } else {
482 setTitleButtonsLeft( KDecorationOptions::defaultTitleButtonsLeft());
483 setTitleButtonsRight( KDecorationOptions::defaultTitleButtonsRight());
486 return changed;
489 void KDecorationPreviewOptions::setCustomBorderSize(BorderSize size)
491 customBorderSize = size;
493 updateSettings();
496 void KDecorationPreviewOptions::setCustomTitleButtonsEnabled(bool enabled)
498 customButtonsChanged = true;
499 customButtons = enabled;
501 updateSettings();
504 void KDecorationPreviewOptions::setCustomTitleButtons(const QString &left, const QString &right)
506 customTitleButtonsLeft = left;
507 customTitleButtonsRight = right;
509 updateSettings();
512 bool KDecorationPreviewPlugins::provides( Requirement )
514 return false;
517 #include "preview.moc"