Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kwin / lib / kdecoration.cpp
blob45d173681e4c9893f27c58e9df80eda3cc27e52c
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"
27 #include <kdebug.h>
28 #include <QApplication>
29 #include <kglobal.h>
30 #include <assert.h>
31 #if defined Q_WS_X11 && ! defined K_WS_QTONLY
32 #include <X11/Xlib.h>
33 #include <fixx11h.h>
34 #include <QX11Info>
35 #endif
37 #include "kdecoration_p.h"
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 setMainWidget(w);
88 void KDecoration::setMainWidget( QWidget* w )
90 assert( w_ == NULL );
91 w_ = w;
92 w->setMouseTracking( true );
93 widget()->resize( geometry().size());
96 QWidget* KDecoration::initialParentWidget() const
98 return bridge_->initialParentWidget();
101 Qt::WFlags KDecoration::initialWFlags() const
103 return bridge_->initialWFlags();
106 bool KDecoration::isActive() const
108 return bridge_->isActive();
111 bool KDecoration::isCloseable() const
113 return bridge_->isCloseable();
116 bool KDecoration::isMaximizable() const
118 return bridge_->isMaximizable();
121 KDecoration::MaximizeMode KDecoration::maximizeMode() const
123 return bridge_->maximizeMode();
126 bool KDecoration::isMinimizable() const
128 return bridge_->isMinimizable();
131 bool KDecoration::providesContextHelp() const
133 return bridge_->providesContextHelp();
136 int KDecoration::desktop() const
138 return bridge_->desktop();
141 bool KDecoration::isModal() const
143 return bridge_->isModal();
146 bool KDecoration::isShadeable() const
148 return bridge_->isShadeable();
151 bool KDecoration::isShade() const
153 return bridge_->isShade();
156 bool KDecoration::isSetShade() const
158 return bridge_->isSetShade();
161 bool KDecoration::keepAbove() const
163 return bridge_->keepAbove();
166 bool KDecoration::keepBelow() const
168 return bridge_->keepBelow();
171 bool KDecoration::isMovable() const
173 return bridge_->isMovable();
176 bool KDecoration::isResizable() const
178 return bridge_->isResizable();
181 NET::WindowType KDecoration::windowType( unsigned long supported_types ) const
182 { // this one is also duplicated in KDecorationFactory
183 return bridge_->windowType( supported_types );
186 QIcon KDecoration::icon() const
188 return bridge_->icon();
191 QString KDecoration::caption() const
193 return bridge_->caption();
196 void KDecoration::processMousePressEvent( QMouseEvent* e )
198 return bridge_->processMousePressEvent( e );
201 void KDecoration::showWindowMenu( const QRect &pos )
203 bridge_->showWindowMenu( pos );
206 void KDecoration::showWindowMenu( QPoint pos )
208 bridge_->showWindowMenu( pos );
211 void KDecoration::performWindowOperation( WindowOperation op )
213 bridge_->performWindowOperation( op );
216 void KDecoration::setMask( const QRegion& reg, int mode )
218 bridge_->setMask( reg, mode );
221 void KDecoration::clearMask()
223 bridge_->setMask( QRegion(), 0 );
226 bool KDecoration::isPreview() const
228 return bridge_->isPreview();
231 QRect KDecoration::geometry() const
233 return bridge_->geometry();
236 QRect KDecoration::iconGeometry() const
238 return bridge_->iconGeometry();
241 QRegion KDecoration::unobscuredRegion( const QRegion& r ) const
243 return bridge_->unobscuredRegion( r );
246 WId KDecoration::windowId() const
248 return bridge_->windowId();
251 void KDecoration::closeWindow()
253 bridge_->closeWindow();
256 void KDecoration::maximize( Qt::MouseButtons button )
258 performWindowOperation( options()->operationMaxButtonClick( button ));
261 void KDecoration::maximize( MaximizeMode mode )
263 bridge_->maximize( mode );
266 void KDecoration::minimize()
268 bridge_->minimize();
271 void KDecoration::showContextHelp()
273 bridge_->showContextHelp();
276 void KDecoration::setDesktop( int desktop )
278 bridge_->setDesktop( desktop );
281 void KDecoration::toggleOnAllDesktops()
283 if( isOnAllDesktops())
284 setDesktop( bridge_->currentDesktop());
285 else
286 setDesktop( NET::OnAllDesktops );
289 void KDecoration::titlebarDblClickOperation()
291 bridge_->titlebarDblClickOperation();
294 void KDecoration::titlebarMouseWheelOperation( int delta )
296 bridge_->titlebarMouseWheelOperation( delta );
299 void KDecoration::setShade( bool set )
301 bridge_->setShade( set );
304 void KDecoration::setKeepAbove( bool set )
306 bridge_->setKeepAbove( set );
309 void KDecoration::setKeepBelow( bool set )
311 bridge_->setKeepBelow( set );
314 void KDecoration::emitKeepAboveChanged( bool above )
316 keepAboveChanged( above );
319 void KDecoration::emitKeepBelowChanged( bool below )
321 keepBelowChanged( below );
324 bool KDecoration::drawbound( const QRect&, bool )
326 return false;
329 bool KDecoration::windowDocked( Position )
331 return false;
334 void KDecoration::reset( unsigned long )
338 void KDecoration::grabXServer()
340 bridge_->grabXServer( true );
343 void KDecoration::ungrabXServer()
345 bridge_->grabXServer( false );
348 KDecoration::Position KDecoration::mousePosition( const QPoint& p ) const
350 const int range = 16;
351 int bleft, bright, btop, bbottom;
352 borders( bleft, bright, btop, bbottom );
353 btop = qMin( btop, 4 ); // otherwise whole titlebar would have resize cursor
355 Position m = PositionCenter;
357 if ( ( p.x() > bleft && p.x() < widget()->width() - bright )
358 && ( p.y() > btop && p.y() < widget()->height() - bbottom ) )
359 return PositionCenter;
361 if ( p.y() <= qMax( range, btop ) && p.x() <= qMax( range, bleft ))
362 m = PositionTopLeft;
363 else if ( p.y() >= widget()->height()- qMax( range, bbottom )
364 && p.x() >= widget()->width()- qMax( range, bright ))
365 m = PositionBottomRight;
366 else if ( p.y() >= widget()->height()- qMax( range, bbottom ) && p.x() <= qMax( range, bleft ))
367 m = PositionBottomLeft;
368 else if ( p.y() <= qMax( range, btop ) && p.x() >= widget()->width()- qMax( range, bright ))
369 m = PositionTopRight;
370 else if ( p.y() <= btop )
371 m = PositionTop;
372 else if ( p.y() >= widget()->height()-bbottom )
373 m = PositionBottom;
374 else if ( p.x() <= bleft )
375 m = PositionLeft;
376 else if ( p.x() >= widget()->width()-bright )
377 m = PositionRight;
378 else
379 m = PositionCenter;
380 return m;
383 KDecorationOptions::KDecorationOptions()
384 : d( new KDecorationOptionsPrivate )
386 assert( KDecoration::options_ == NULL );
387 KDecoration::options_ = this;
390 KDecorationOptions::~KDecorationOptions()
392 assert( KDecoration::options_ == this );
393 KDecoration::options_ = NULL;
394 delete d;
397 QColor KDecorationOptions::color(ColorType type, bool active) const
399 return(d->colors[type + (active ? 0 : NUM_COLORS)]);
402 QFont KDecorationOptions::font(bool active, bool small) const
404 if ( small )
405 return(active ? d->activeFontSmall : d->inactiveFontSmall);
406 else
407 return(active ? d->activeFont : d->inactiveFont);
410 QPalette KDecorationOptions::palette(ColorType type, bool active) const
412 int idx = type + (active ? 0 : NUM_COLORS);
413 if(d->pal[idx])
414 return(*d->pal[idx]);
415 #ifdef __GNUC__
416 #warning KDE4 : why construct the palette this way?
417 #endif
418 // TODO: Is this worth 'porting' to Qt4?
419 // d->pal[idx] = new QPalette(Qt::black, d->colors[idx], d->colors[idx].light(150),
420 // d->colors[idx].dark(), d->colors[idx].dark(120),
421 // Qt::black, QApplication::palette().active().
422 // base());
423 d->pal[idx] = new QPalette(d->colors[idx]);
424 return(*d->pal[idx]);
427 unsigned long KDecorationOptions::updateSettings( KConfig* config )
429 return d->updateSettings( config );
432 bool KDecorationOptions::customButtonPositions() const
434 return d->custom_button_positions;
437 QString KDecorationOptions::titleButtonsLeft() const
439 return d->title_buttons_left;
442 QString KDecorationOptions::defaultTitleButtonsLeft()
444 return "MS";
447 QString KDecorationOptions::titleButtonsRight() const
449 return d->title_buttons_right;
452 QString KDecorationOptions::defaultTitleButtonsRight()
454 return "HIA__X";
457 bool KDecorationOptions::showTooltips() const
459 return d->show_tooltips;
462 KDecorationOptions::BorderSize KDecorationOptions::preferredBorderSize( KDecorationFactory* factory ) const
464 assert( factory != NULL );
465 if( d->cached_border_size == BordersCount ) // invalid
466 d->cached_border_size = d->findPreferredBorderSize( d->border_size,
467 factory->borderSizes());
468 return d->cached_border_size;
471 bool KDecorationOptions::moveResizeMaximizedWindows() const
473 return d->move_resize_maximized_windows;
476 KDecorationDefines::WindowOperation KDecorationOptions::operationMaxButtonClick( Qt::MouseButtons button ) const
478 return button == Qt::RightButton? d->opMaxButtonRightClick :
479 button == Qt::MidButton? d->opMaxButtonMiddleClick :
480 d->opMaxButtonLeftClick;
483 void KDecorationOptions::setOpMaxButtonLeftClick( WindowOperation op )
485 d->opMaxButtonLeftClick = op;
488 void KDecorationOptions::setOpMaxButtonRightClick( WindowOperation op )
490 d->opMaxButtonRightClick = op;
493 void KDecorationOptions::setOpMaxButtonMiddleClick( WindowOperation op )
495 d->opMaxButtonMiddleClick = op;
498 void KDecorationOptions::setBorderSize( BorderSize bs )
500 d->border_size = bs;
501 d->cached_border_size = BordersCount; // invalid
504 void KDecorationOptions::setCustomButtonPositions( bool b )
506 d->custom_button_positions = b;
509 void KDecorationOptions::setTitleButtonsLeft( const QString& b )
511 d->title_buttons_left = b;
514 void KDecorationOptions::setTitleButtonsRight( const QString& b )
516 d->title_buttons_right = b;
519 #include "kdecoration.moc"