2 This file is part of the KDE project.
4 Copyright (C) 2005 Sandro Giessl <sandro@giessl.com>
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.
25 #ifndef KCOMMONDECORATION_H
26 #define KCOMMONDECORATION_H
28 #include <QtGui/QAbstractButton>
29 #include "kdecoration.h"
31 /** @addtogroup kdecoration */
34 class KDecorationBridge
;
35 class KDecorationFactory
;
50 class KCommonDecorationButton
;
52 class KCommonDecorationButtonPrivate
;
53 class KCommonDecorationPrivate
;
54 class KCommonDecorationWrapper
;
57 * This class eases development of decorations by implementing parts of KDecoration
58 * which are error prone and common for most decorations.
59 * It takes care of the window layout, button/action handling, and window mask creation.
60 * Note that for technical reasons KCommonDecoration does not inherit KDecoration but
61 * only provides the same API. If in rare cases you need to convert to KDecoration,
62 * use the decoration() function.
63 * See KDecoration documentation for all the wrapped functions.
65 class KWIN_EXPORT KCommonDecoration
: public QObject
, public KDecorationDefines
70 KCommonDecoration(KDecorationBridge
* bridge
, KDecorationFactory
* factory
);
71 virtual ~KCommonDecoration();
74 * Used to calculate the decoration layout. The basic layout looks like this:
77 * _______________________________________________________________
79 * |_______________________________________________________________|
80 * | LM_TitleEdgeLeft | [title] | LM_TitleEdgeRight |
81 * |__________________|________________________|___________________|
82 * | LM_TitleEdgeBottom |
83 * |_______________________________________________________________|
87 * |LM_BorderLeft LM_BorderRight|
88 * |_|___________________________________________________________|_|
90 * |_______________________________________________________________|
93 * ___________________________________________________________________________________
94 * | LM_ButtonMarginTop | | LM_ButtonMarginTop |
95 * |________________________________| |_________________________________|
96 * | [Buttons] | LM_TitleBorderLeft | LM_TitleHeight | LM_TitleBorderRight | [Buttons] |
97 * |___________|____________________|________________|_____________________|___________|
100 * _____________________________________________________________________________________________
101 * | button | spacing | button | spacing | explicit spacer | spacing | ... | spacing | button |
102 * |________|_________|________|_________|_________________|_________|________|_________|________|
104 * @see layoutMetric()
121 LM_ExplicitButtonSpacer
,
125 enum DecorationBehaviour
127 DB_MenuClose
, ///< Close window on double clicking the menu
128 DB_WindowMask
, ///< Set a mask on the window
129 DB_ButtonHide
///< Hide buttons when there is not enough space in the titlebar
141 * The name of the decoration used in the decoration preview.
143 virtual QString
visibleName() const = 0;
145 * The default title button order on the left.
146 * @see KDecoration::titleButtonsLeft()
147 * @see KDecoration::titleButtonsRight()
149 virtual QString
defaultButtonsLeft() const;
151 * The default title button order on the left.
152 * @see KDecoration::titleButtonsLeft()
153 * @see KDecoration::titleButtonsRight()
155 virtual QString
defaultButtonsRight() const;
158 * This controls whether some specific behaviour should be enabled or not.
159 * @see DecorationBehaviour
161 virtual bool decorationBehaviour(DecorationBehaviour behaviour
) const;
164 * This controls the layout of the decoration in various ways. It is
165 * possible to have a different layout for different window states.
166 * @param lm The layout element.
167 * @param respectWindowState Whether window states should be taken into account or a "default" state should be assumed.
168 * @param button For LM_ButtonWidth and LM_ButtonHeight, the button.
170 virtual int layoutMetric(LayoutMetric lm
, bool respectWindowState
= true, const KCommonDecorationButton
*button
= 0) const;
173 * Create a new title bar button. KCommonDecoration takes care of memory management.
174 * @return a pointer to the button, or 0 if the button should not be created.
176 virtual KCommonDecorationButton
*createButton(ButtonType type
) = 0;
179 * @return the mask for the specific window corner.
181 virtual QRegion
cornerShape(WindowCorner corner
);
184 * This updates the window mask using the information provided by
185 * cornerShape(). Edges which are aligned to screen corners are not
186 * shaped for better usability (remember to paint these areas in paintEvent(), too).
187 * You normally don't want/need to reimplement updateWindowShape().
190 virtual void updateWindowShape();
193 * Draw the window decoration.
195 virtual void paintEvent(QPaintEvent
*e
) = 0;
198 * This is used to update the painting of the title bar after the caption has been changed.
199 * Reimplement for a more efficient implementation (default calls update() on the whole decoration).
201 virtual void updateCaption();
203 int buttonsLeftWidth() const;
204 int buttonsRightWidth() const;
209 void updateLayout() const;
211 * Makes sure all buttons are repainted.
213 void updateButtons() const;
215 * Manually call reset() on each button.
217 void resetButtons() const;
220 * Convenience method.
221 * @returns true if the window type is NET::Toolbar, NET::Utility, or NET::Menu
223 bool isToolWindow() const;
225 * Convenience method.
226 * @returns the title rect.
228 QRect
titleRect() const;
232 * Handles widget and layout creation, call the base implementation when subclassing this member.
236 * Handles SettingButtons, call the base implementation when subclassing this member.
238 virtual void reset( unsigned long changed
);
239 virtual void borders( int& left
, int& right
, int& top
, int& bottom
) const;
241 virtual void resize(const QSize
& s
);
242 virtual QSize
minimumSize() const;
243 virtual void maximizeChange();
244 virtual void desktopChange();
245 virtual void shadeChange();
246 virtual void iconChange();
247 virtual void activeChange();
248 virtual void captionChange();
250 void keepAboveChange(bool above
);
251 void keepBelowChange(bool below
);
254 void slotKeepAbove();
255 void slotKeepBelow();
256 void menuButtonPressed();
257 void menuButtonReleased();
259 virtual Position
mousePosition(const QPoint
&point
) const;
261 virtual bool eventFilter( QObject
* o
, QEvent
* e
);
262 virtual void resizeEvent(QResizeEvent
*e
);
263 virtual void mouseDoubleClickEvent(QMouseEvent
*e
);
264 virtual void wheelEvent(QWheelEvent
*e
);
266 // *** wrap everything from KDecoration *** //
267 // reimplementing from KDecoration (wrapped)
268 virtual bool drawbound( const QRect
& geom
, bool clear
);
269 virtual bool windowDocked( Position side
);
270 // wrap everything KDecoration provides
271 static const KDecorationOptions
* options();
272 bool isActive() const;
273 bool isCloseable() const;
274 bool isMaximizable() const;
275 MaximizeMode
maximizeMode() const;
276 bool isMinimizable() const;
277 bool providesContextHelp() const;
279 bool isOnAllDesktops() const; // convenience
280 bool isModal() const;
281 bool isShadeable() const;
282 bool isShade() const;
283 bool isSetShade() const;
284 bool keepAbove() const;
285 bool keepBelow() const;
286 bool isMovable() const;
287 bool isResizable() const;
288 NET::WindowType
windowType( unsigned long supported_types
) const;
290 QString
caption() const;
291 void showWindowMenu( const QRect
&pos
);
292 void showWindowMenu( QPoint pos
);
293 void performWindowOperation( WindowOperation op
);
294 void setMask( const QRegion
& reg
, int mode
= 0 );
295 void clearMask(); // convenience
296 bool isPreview() const;
297 QRect
geometry() const;
298 QRect
iconGeometry() const;
299 QRegion
unobscuredRegion( const QRegion
& r
) const;
300 WId
windowId() const;
301 int width() const; // convenience
302 int height() const; // convenience
303 void processMousePressEvent( QMouseEvent
* e
);
305 void keepAboveChanged( bool );
306 void keepBelowChanged( bool );
308 void setMainWidget( QWidget
* );
309 void createMainWidget( Qt::WFlags flags
= 0 );
310 QWidget
* initialParentWidget() const;
311 Qt::WFlags
initialWFlags() const;
313 const QWidget
* widget() const;
314 KDecorationFactory
* factory() const;
316 void ungrabXServer();
319 void maximize( Qt::MouseButtons button
);
320 void maximize( MaximizeMode mode
);
322 void showContextHelp();
323 void setDesktop( int desktop
);
324 void toggleOnAllDesktops(); // convenience
325 void titlebarDblClickOperation();
326 void titlebarMouseWheelOperation( int delta
);
327 void setShade( bool set
);
328 void setKeepAbove( bool set
);
329 void setKeepBelow( bool set
);
330 // *** end of wrapping of everything from KDecoration *** //
332 // access the KDecoration wrapper
333 const KDecoration
* decoration() const;
334 KDecoration
* decoration();
339 void moveWidget(int x
, int y
, QWidget
*widget
) const;
340 void resizeWidget(int w
, int h
, QWidget
*widget
) const;
342 typedef QVector
<KCommonDecorationButton
*> ButtonContainer
; ///< If the entry is 0, it's a spacer.
343 int buttonContainerWidth(const ButtonContainer
&btnContainer
, bool countHidden
= false) const;
344 void addButtons(ButtonContainer
&btnContainer
, const QString
& buttons
, bool isLeft
);
346 KCommonDecorationButton
*m_button
[NumButtons
];
348 ButtonContainer m_buttonsLeft
;
349 ButtonContainer m_buttonsRight
;
351 QWidget
*m_previewWidget
;
353 // button hiding for small windows
354 void calcHiddenButtons();
356 int btnHideLastWidth
;
358 bool closing
; // for menu doubleclick closing...
360 KCommonDecorationWrapper
* wrapper
;
362 KCommonDecorationPrivate
*d
;
365 class KWIN_EXPORT KCommonDecorationUnstable
366 : public KCommonDecoration
370 KCommonDecorationUnstable(KDecorationBridge
* bridge
, KDecorationFactory
* factory
);
371 virtual ~KCommonDecorationUnstable();
372 virtual QList
<QRect
> shadowQuads( ShadowType type
) const;
373 virtual double shadowOpacity( ShadowType type
) const;
374 virtual double shadowBrightness( ShadowType type
) const;
375 virtual double shadowSaturation( ShadowType type
) const;
376 void repaintShadow();
377 bool compositingActive() const;
378 bool shadowsActive() const;
379 double opacity() const;
383 * Title bar buttons of KCommonDecoration need to inherit this class.
385 class KWIN_EXPORT KCommonDecorationButton
: public QAbstractButton
389 friend class KCommonDecoration
;
392 KCommonDecorationButton(ButtonType type
, KCommonDecoration
*parent
);
393 virtual ~KCommonDecorationButton();
396 * These flags specify what has changed, e.g. the reason for a reset().
400 ManualReset
= 1 << 0, ///< The button might want to do a full reset for some reason...
401 SizeChange
= 1 << 1, ///< The button size changed @see setSize()
402 ToggleChange
= 1 << 2, ///< The button toggle state has changed @see setToggleButton()
403 StateChange
= 1 << 3, ///< The button has been set pressed or not... @see setOn()
404 IconChange
= 1 << 4, ///< The window icon has been changed
405 DecorationReset
= 1 << 5 ///< E.g. when decoration colors have changed
408 * Initialize the button after size change etc.
410 virtual void reset(unsigned long changed
) = 0;
412 * @returns the KCommonDecoration the button belongs to.
414 KCommonDecoration
*decoration() const;
416 * @returns the button type.
419 ButtonType
type() const;
422 * Whether the button is left of the titlebar or not.
427 * Set which mouse buttons the button should honor. Used e.g. to prevent accidental right mouse clicks.
429 void setRealizeButtons(int btns
);
431 * Set the button size.
433 void setSize(const QSize
&s
);
435 * Set/update the button's tool tip
437 void setTipText(const QString
&tip
);
439 * The mouse button that has been clicked last time.
441 Qt::MouseButtons
lastMousePress() const { return m_lastMouse
; }
443 QSize
sizeHint() const;
446 void setToggleButton(bool toggle
);
448 void setLeft(bool left
);
449 void mousePressEvent(QMouseEvent
*e
);
450 void mouseReleaseEvent(QMouseEvent
*e
);
453 KCommonDecoration
*m_decoration
;
455 int m_realizeButtons
;
457 Qt::MouseButtons m_lastMouse
;
461 KCommonDecorationButtonPrivate
*d
;
466 #endif // KCOMMONDECORATION_H
468 // kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;