not quite so much needs to be delayed to the init() function
[personal-kdebase.git] / workspace / kwin / lib / kdecoration.cpp
blobb1d349907c562afa514aecad0f6322b1eecd6522
1 /*****************************************************************
2 This file is part of the KDE project.
4 Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>
6 Permission is hereby granted, free of charge, to any person obtaining a
7 copy of this software and associated documentation files (the "Software"),
8 to deal in the Software without restriction, including without limitation
9 the rights to use, copy, modify, merge, publish, distribute, sublicense,
10 and/or sell copies of the Software, and to permit persons to whom the
11 Software is furnished to do so, subject to the following conditions:
13 The above copyright notice and this permission notice shall be included in
14 all copies or substantial portions of the Software.
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
23 ******************************************************************/
25 #include "kdecoration.h"
26 #include "kdecoration_p.h"
28 #include <kdebug.h>
29 #include <QApplication>
30 #include <kglobal.h>
31 #include <assert.h>
32 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
33 #include <X11/Xlib.h>
34 #include <fixx11h.h>
35 #include <QX11Info>
36 #endif
38 #include "kdecorationfactory.h"
39 #include "kdecorationbridge.h"
43 Extending KDecoration:
44 ======================
46 If KDecoration will ever need to be extended in a way that'd break binary compatibility
47 (i.e. adding new virtual methods most probably), new class KDecoration2 should be
48 inherited from KDecoration and those methods added there. Code that would depend
49 on the new functionality could then dynamic_cast<> to KDecoration2 to check whether
50 it is available and use it.
52 KCommonDecoration would have to be extended the same way, adding KCommonDecoration2
53 inheriting KCommonDecoration and adding the new API matching KDecoration2.
58 KDecorationOptions* KDecoration::options_;
60 KDecoration::KDecoration( KDecorationBridge* bridge, KDecorationFactory* factory )
61 : bridge_( bridge ),
62 w_( NULL ),
63 factory_( factory )
65 factory->addDecoration( this );
68 KDecoration::~KDecoration()
70 factory()->removeDecoration( this );
71 delete w_;
74 const KDecorationOptions* KDecoration::options()
76 return options_;
79 void KDecoration::createMainWidget( Qt::WFlags flags )
81 // FRAME check flags?
82 QWidget *w = new QWidget( initialParentWidget(), initialWFlags() | flags );
83 w->setObjectName("decoration widget");
84 w->setAttribute( Qt::WA_PaintOnScreen );
85 if ( options()->showTooltips() )
86 w->setAttribute( Qt::WA_AlwaysShowToolTips );
87 setMainWidget(w);
90 void KDecoration::setMainWidget( QWidget* w )
92 assert( w_ == NULL );
93 w_ = w;
94 w->setMouseTracking( true );
95 widget()->resize( geometry().size());
98 QWidget* KDecoration::initialParentWidget() const
100 return bridge_->initialParentWidget();
103 Qt::WFlags KDecoration::initialWFlags() const
105 return bridge_->initialWFlags();
108 bool KDecoration::isActive() const
110 return bridge_->isActive();
113 bool KDecoration::isCloseable() const
115 return bridge_->isCloseable();
118 bool KDecoration::isMaximizable() const
120 return bridge_->isMaximizable();
123 KDecoration::MaximizeMode KDecoration::maximizeMode() const
125 return bridge_->maximizeMode();
128 bool KDecoration::isMinimizable() const
130 return bridge_->isMinimizable();
133 bool KDecoration::providesContextHelp() const
135 return bridge_->providesContextHelp();
138 int KDecoration::desktop() const
140 return bridge_->desktop();
143 bool KDecoration::isModal() const
145 return bridge_->isModal();
148 bool KDecoration::isShadeable() const
150 return bridge_->isShadeable();
153 bool KDecoration::isShade() const
155 return bridge_->isShade();
158 bool KDecoration::isSetShade() const
160 return bridge_->isSetShade();
163 bool KDecoration::keepAbove() const
165 return bridge_->keepAbove();
168 bool KDecoration::keepBelow() const
170 return bridge_->keepBelow();
173 bool KDecoration::isMovable() const
175 return bridge_->isMovable();
178 bool KDecoration::isResizable() const
180 return bridge_->isResizable();
183 NET::WindowType KDecoration::windowType( unsigned long supported_types ) const
184 { // this one is also duplicated in KDecorationFactory
185 return bridge_->windowType( supported_types );
188 QIcon KDecoration::icon() const
190 return bridge_->icon();
193 QString KDecoration::caption() const
195 return bridge_->caption();
198 void KDecoration::processMousePressEvent( QMouseEvent* e )
200 return bridge_->processMousePressEvent( e );
203 void KDecoration::showWindowMenu( const QRect &pos )
205 bridge_->showWindowMenu( pos );
208 void KDecoration::showWindowMenu( QPoint pos )
210 bridge_->showWindowMenu( pos );
213 void KDecoration::performWindowOperation( WindowOperation op )
215 bridge_->performWindowOperation( op );
218 void KDecoration::setMask( const QRegion& reg, int mode )
220 bridge_->setMask( reg, mode );
223 void KDecoration::clearMask()
225 bridge_->setMask( QRegion(), 0 );
228 bool KDecoration::isPreview() const
230 return bridge_->isPreview();
233 QRect KDecoration::geometry() const
235 return bridge_->geometry();
238 QRect KDecoration::iconGeometry() const
240 return bridge_->iconGeometry();
243 QRegion KDecoration::unobscuredRegion( const QRegion& r ) const
245 return bridge_->unobscuredRegion( r );
248 WId KDecoration::windowId() const
250 return bridge_->windowId();
253 void KDecoration::closeWindow()
255 bridge_->closeWindow();
258 void KDecoration::maximize( Qt::MouseButtons button )
260 performWindowOperation( options()->operationMaxButtonClick( button ));
263 void KDecoration::maximize( MaximizeMode mode )
265 bridge_->maximize( mode );
268 void KDecoration::minimize()
270 bridge_->minimize();
273 void KDecoration::showContextHelp()
275 bridge_->showContextHelp();
278 void KDecoration::setDesktop( int desktop )
280 bridge_->setDesktop( desktop );
283 void KDecoration::toggleOnAllDesktops()
285 if( isOnAllDesktops())
286 setDesktop( bridge_->currentDesktop());
287 else
288 setDesktop( NET::OnAllDesktops );
291 void KDecoration::titlebarDblClickOperation()
293 bridge_->titlebarDblClickOperation();
296 void KDecoration::titlebarMouseWheelOperation( int delta )
298 bridge_->titlebarMouseWheelOperation( delta );
301 void KDecoration::setShade( bool set )
303 bridge_->setShade( set );
306 void KDecoration::setKeepAbove( bool set )
308 bridge_->setKeepAbove( set );
311 void KDecoration::setKeepBelow( bool set )
313 bridge_->setKeepBelow( set );
316 void KDecoration::emitKeepAboveChanged( bool above )
318 keepAboveChanged( above );
321 void KDecoration::emitKeepBelowChanged( bool below )
323 keepBelowChanged( below );
326 bool KDecoration::drawbound( const QRect&, bool )
328 return false;
331 bool KDecoration::windowDocked( Position )
333 return false;
336 void KDecoration::reset( unsigned long )
340 void KDecoration::grabXServer()
342 bridge_->grabXServer( true );
345 void KDecoration::ungrabXServer()
347 bridge_->grabXServer( false );
350 KDecoration::Position KDecoration::mousePosition( const QPoint& p ) const
352 const int range = 16;
353 int bleft, bright, btop, bbottom;
354 borders( bleft, bright, btop, bbottom );
355 btop = qMin( btop, 4 ); // otherwise whole titlebar would have resize cursor
357 Position m = PositionCenter;
359 if ( ( p.x() > bleft && p.x() < widget()->width() - bright )
360 && ( p.y() > btop && p.y() < widget()->height() - bbottom ) )
361 return PositionCenter;
363 if ( p.y() <= qMax( range, btop ) && p.x() <= qMax( range, bleft ))
364 m = PositionTopLeft;
365 else if ( p.y() >= widget()->height()- qMax( range, bbottom )
366 && p.x() >= widget()->width()- qMax( range, bright ))
367 m = PositionBottomRight;
368 else if ( p.y() >= widget()->height()- qMax( range, bbottom ) && p.x() <= qMax( range, bleft ))
369 m = PositionBottomLeft;
370 else if ( p.y() <= qMax( range, btop ) && p.x() >= widget()->width()- qMax( range, bright ))
371 m = PositionTopRight;
372 else if ( p.y() <= btop )
373 m = PositionTop;
374 else if ( p.y() >= widget()->height()-bbottom )
375 m = PositionBottom;
376 else if ( p.x() <= bleft )
377 m = PositionLeft;
378 else if ( p.x() >= widget()->width()-bright )
379 m = PositionRight;
380 else
381 m = PositionCenter;
382 return m;
386 KDecorationUnstable::KDecorationUnstable( KDecorationBridge* bridge, KDecorationFactory* factory )
387 : KDecoration( bridge, factory )
389 Q_ASSERT( dynamic_cast< KDecorationBridgeUnstable* >( bridge ));
392 KDecorationUnstable::~KDecorationUnstable()
396 QList<QRect> KDecorationUnstable::shadowQuads( ShadowType type ) const
398 Q_UNUSED( type );
399 return QList<QRect>();
402 double KDecorationUnstable::shadowOpacity( ShadowType type ) const
404 if( isActive() && type == ShadowBorderedActive )
405 return 1.0;
406 else if( !isActive() && type == ShadowBorderedInactive )
407 return 1.0;
408 return 0.0;
411 double KDecorationUnstable::shadowBrightness( ShadowType type ) const
413 Q_UNUSED( type );
414 return 1.0;
417 double KDecorationUnstable::shadowSaturation( ShadowType type ) const
419 Q_UNUSED( type );
420 return 1.0;
423 void KDecorationUnstable::repaintShadow()
425 static_cast< KDecorationBridgeUnstable* >( bridge_ )->repaintShadow();
428 bool KDecorationUnstable::compositingActive() const
430 return static_cast< KDecorationBridgeUnstable* >( bridge_ )->compositingActive();
433 bool KDecorationUnstable::shadowsActive() const
435 return static_cast< KDecorationBridgeUnstable* >( bridge_ )->shadowsActive();
438 double KDecorationUnstable::opacity() const
440 return static_cast< KDecorationBridgeUnstable* >( bridge_ )->opacity();
444 KDecorationOptions::KDecorationOptions()
445 : d( new KDecorationOptionsPrivate )
447 assert( KDecoration::options_ == NULL );
448 KDecoration::options_ = this;
451 KDecorationOptions::~KDecorationOptions()
453 assert( KDecoration::options_ == this );
454 KDecoration::options_ = NULL;
455 delete d;
458 QColor KDecorationOptions::color(ColorType type, bool active) const
460 return(d->colors[type + (active ? 0 : NUM_COLORS)]);
463 QFont KDecorationOptions::font(bool active, bool small) const
465 if ( small )
466 return(active ? d->activeFontSmall : d->inactiveFontSmall);
467 else
468 return(active ? d->activeFont : d->inactiveFont);
471 QPalette KDecorationOptions::palette(ColorType type, bool active) const
473 int idx = type + (active ? 0 : NUM_COLORS);
474 if(d->pal[idx])
475 return(*d->pal[idx]);
476 #ifdef __GNUC__
477 #warning KDE4 : why construct the palette this way?
478 #endif
479 // TODO: Is this worth 'porting' to Qt4?
480 // d->pal[idx] = new QPalette(Qt::black, d->colors[idx], d->colors[idx].light(150),
481 // d->colors[idx].dark(), d->colors[idx].dark(120),
482 // Qt::black, QApplication::palette().active().
483 // base());
484 d->pal[idx] = new QPalette(d->colors[idx]);
485 return(*d->pal[idx]);
488 unsigned long KDecorationOptions::updateSettings( KConfig* config )
490 return d->updateSettings( config );
493 bool KDecorationOptions::customButtonPositions() const
495 return d->custom_button_positions;
498 QString KDecorationOptions::titleButtonsLeft() const
500 return d->title_buttons_left;
503 QString KDecorationOptions::defaultTitleButtonsLeft()
505 return "MS";
508 QString KDecorationOptions::titleButtonsRight() const
510 return d->title_buttons_right;
513 QString KDecorationOptions::defaultTitleButtonsRight()
515 return "HIA__X";
518 bool KDecorationOptions::showTooltips() const
520 return d->show_tooltips;
523 KDecorationOptions::BorderSize KDecorationOptions::preferredBorderSize( KDecorationFactory* factory ) const
525 assert( factory != NULL );
526 if( d->cached_border_size == BordersCount ) // invalid
527 d->cached_border_size = d->findPreferredBorderSize( d->border_size,
528 factory->borderSizes());
529 return d->cached_border_size;
532 bool KDecorationOptions::moveResizeMaximizedWindows() const
534 return d->move_resize_maximized_windows;
537 KDecorationDefines::WindowOperation KDecorationOptions::operationMaxButtonClick( Qt::MouseButtons button ) const
539 return button == Qt::RightButton? d->opMaxButtonRightClick :
540 button == Qt::MidButton? d->opMaxButtonMiddleClick :
541 d->opMaxButtonLeftClick;
544 void KDecorationOptions::setOpMaxButtonLeftClick( WindowOperation op )
546 d->opMaxButtonLeftClick = op;
549 void KDecorationOptions::setOpMaxButtonRightClick( WindowOperation op )
551 d->opMaxButtonRightClick = op;
554 void KDecorationOptions::setOpMaxButtonMiddleClick( WindowOperation op )
556 d->opMaxButtonMiddleClick = op;
559 void KDecorationOptions::setBorderSize( BorderSize bs )
561 d->border_size = bs;
562 d->cached_border_size = BordersCount; // invalid
565 void KDecorationOptions::setCustomButtonPositions( bool b )
567 d->custom_button_positions = b;
570 void KDecorationOptions::setTitleButtonsLeft( const QString& b )
572 d->title_buttons_left = b;
575 void KDecorationOptions::setTitleButtonsRight( const QString& b )
577 d->title_buttons_right = b;
580 #include "kdecoration.moc"