~ubuntu-branches/ubuntu/gutsy/kdebase-workspace/gutsy-backports

« back to all changes in this revision

Viewing changes to kwin/lib/kcommondecoration.h

  • Committer: Bazaar Package Importer
  • Author(s): Jonathan Riddell
  • Date: 2007-09-05 20:45:14 UTC
  • Revision ID: james.westby@ubuntu.com-20070905204514-632hhspl0nvrc84i
Tags: upstream-3.93.0
ImportĀ upstreamĀ versionĀ 3.93.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  This file is part of the KDE project.
 
3
 
 
4
  Copyright (C) 2005 Sandro Giessl <sandro@giessl.com>
 
5
 
 
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:
 
12
 
 
13
  The above copyright notice and this permission notice shall be included in
 
14
  all copies or substantial portions of the Software.
 
15
 
 
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
 */
 
24
 
 
25
#ifndef KCOMMONDECORATION_H
 
26
#define KCOMMONDECORATION_H
 
27
 
 
28
#include <QtGui/QAbstractButton>
 
29
#include "kdecoration.h"
 
30
 
 
31
class KDecorationBridge;
 
32
class KDecorationFactory;
 
33
 
 
34
enum ButtonType {
 
35
    HelpButton=0,
 
36
    MaxButton,
 
37
    MinButton,
 
38
    CloseButton,
 
39
    MenuButton,
 
40
    OnAllDesktopsButton,
 
41
    AboveButton,
 
42
    BelowButton,
 
43
    ShadeButton,
 
44
    NumButtons
 
45
};
 
46
 
 
47
class KCommonDecorationButton;
 
48
 
 
49
class KCommonDecorationButtonPrivate;
 
50
class KCommonDecorationPrivate;
 
51
 
 
52
/**
 
53
 * This class eases development of decorations by implementing parts of KDecoration
 
54
 * which are error prone and common for most decorations.
 
55
 * It takes care of the window layout, button/action handling, and window mask creation.
 
56
 */
 
57
class KWIN_EXPORT KCommonDecoration : public KDecoration
 
58
{
 
59
    Q_OBJECT
 
60
 
 
61
    public:
 
62
        KCommonDecoration(KDecorationBridge* bridge, KDecorationFactory* factory);
 
63
        virtual ~KCommonDecoration();
 
64
 
 
65
        /**
 
66
         * Used to calculate the decoration layout. The basic layout looks like this:
 
67
         *
 
68
         * Window:
 
69
         *  _______________________________________________________________
 
70
         * |           LM_TitleEdgeTop                                     |
 
71
         * |_______________________________________________________________|
 
72
         * | LM_TitleEdgeLeft |   [title]              | LM_TitleEdgeRight |
 
73
         * |__________________|________________________|___________________|
 
74
         * |           LM_TitleEdgeBottom                                  |
 
75
         * |_______________________________________________________________|
 
76
         * | |                                                           | |
 
77
         * | |                                                           | |
 
78
         * | |                                                           | |
 
79
         * |LM_BorderLeft                                    LM_BorderRight|
 
80
         * |_|___________________________________________________________|_|
 
81
         * |           LM_BorderBottom                                     |
 
82
         * |_______________________________________________________________|
 
83
         *
 
84
         * Title:
 
85
         *  ___________________________________________________________________________________
 
86
         * | LM_ButtonMarginTop             |                |              LM_ButtonMarginTop |
 
87
         * |________________________________|                |_________________________________|
 
88
         * | [Buttons] | LM_TitleBorderLeft | LM_TitleHeight | LM_TitleBorderRight | [Buttons] |
 
89
         * |___________|____________________|________________|_____________________|___________|
 
90
         *
 
91
         * Buttons:
 
92
         *  _____________________________________________________________________________________________
 
93
         * | button | spacing | button | spacing | explicit spacer | spacing | ...    | spacing | button |
 
94
         * |________|_________|________|_________|_________________|_________|________|_________|________|
 
95
         *
 
96
         * @see layoutMetric()
 
97
         */
 
98
        enum LayoutMetric
 
99
        {
 
100
            LM_BorderLeft,
 
101
            LM_BorderRight,
 
102
            LM_BorderBottom,
 
103
            LM_TitleHeight,
 
104
            LM_TitleBorderLeft,
 
105
            LM_TitleBorderRight,
 
106
            LM_TitleEdgeLeft,
 
107
            LM_TitleEdgeRight,
 
108
            LM_TitleEdgeTop,
 
109
            LM_TitleEdgeBottom,
 
110
            LM_ButtonWidth,
 
111
            LM_ButtonHeight,
 
112
            LM_ButtonSpacing,
 
113
            LM_ExplicitButtonSpacer,
 
114
            LM_ButtonMarginTop
 
115
        };
 
116
 
 
117
        enum DecorationBehaviour
 
118
        {
 
119
            DB_MenuClose, ///< Close window on double clicking the menu
 
120
            DB_WindowMask, ///< Set a mask on the window
 
121
            DB_ButtonHide  ///< Hide buttons when there is not enough space in the titlebar
 
122
        };
 
123
 
 
124
        enum WindowCorner
 
125
        {
 
126
            WC_TopLeft,
 
127
            WC_TopRight,
 
128
            WC_BottomLeft,
 
129
            WC_BottomRight
 
130
        };
 
131
 
 
132
        /**
 
133
         * The name of the decoration used in the decoration preview.
 
134
         */
 
135
        virtual QString visibleName() const = 0;
 
136
        /**
 
137
         * The default title button order on the left.
 
138
         * @see KDecoration::titleButtonsLeft()
 
139
         * @see KDecoration::titleButtonsRight()
 
140
         */
 
141
        virtual QString defaultButtonsLeft() const = 0;
 
142
        /**
 
143
         * The default title button order on the left.
 
144
         * @see KDecoration::titleButtonsLeft()
 
145
         * @see KDecoration::titleButtonsRight()
 
146
         */
 
147
        virtual QString defaultButtonsRight() const = 0;
 
148
 
 
149
        /**
 
150
         * This controls whether some specific behaviour should be enabled or not.
 
151
         * @see DecorationBehaviour
 
152
         */
 
153
        virtual bool decorationBehaviour(DecorationBehaviour behaviour) const;
 
154
 
 
155
        /**
 
156
         * This controls the layout of the decoration in various ways. It is
 
157
         * possible to have a different layout for different window states.
 
158
         * @param lm                 The layout element.
 
159
         * @param respectWindowState Whether window states should be taken into account or a "default" state should be assumed.
 
160
         * @param button             For LM_ButtonWidth and LM_ButtonHeight, the button.
 
161
         */
 
162
        virtual int layoutMetric(LayoutMetric lm, bool respectWindowState = true, const KCommonDecorationButton *button = 0) const;
 
163
 
 
164
        /**
 
165
         * Create a new title bar button. KCommonDecoration takes care of memory management.
 
166
         * @return a pointer to the button, or 0 if the button should not be created.
 
167
         */
 
168
        virtual KCommonDecorationButton *createButton(ButtonType type) = 0;
 
169
 
 
170
        /**
 
171
         * @return the mask for the specific window corner.
 
172
         */
 
173
        virtual QRegion cornerShape(WindowCorner corner);
 
174
 
 
175
        /**
 
176
         * This updates the window mask using the information provided by
 
177
         * cornerShape(). Edges which are aligned to screen corners are not
 
178
         * shaped for better usability (remember to paint these areas in paintEvent(), too).
 
179
         * You normally don't want/need to reimplement updateWindowShape().
 
180
         * @see cornerShape()
 
181
         */
 
182
        virtual void updateWindowShape();
 
183
 
 
184
        /**
 
185
         * Draw the window decoration.
 
186
         */
 
187
        virtual void paintEvent(QPaintEvent *e) = 0;
 
188
 
 
189
        /**
 
190
         * This is used to update the painting of the title bar after the caption has been changed.
 
191
         * Reimplement for a more efficient implementation (default calls update() on the whole decoration).
 
192
         */
 
193
        virtual void updateCaption();
 
194
 
 
195
        int buttonsLeftWidth() const;
 
196
        int buttonsRightWidth() const;
 
197
 
 
198
        /**
 
199
         * TODO: remove?
 
200
         */
 
201
        void updateLayout() const;
 
202
        /**
 
203
         * Makes sure all buttons are repainted.
 
204
         */
 
205
        void updateButtons() const;
 
206
        /**
 
207
         * Manually call reset() on each button.
 
208
         */
 
209
        void resetButtons() const;
 
210
 
 
211
        /**
 
212
         * Convenience method.
 
213
         * @returns true if the window type is NET::Toolbar, NET::Utility, or NET::Menu
 
214
         */
 
215
        bool isToolWindow() const;
 
216
        /**
 
217
         * Convenience method.
 
218
         * @returns the title rect.
 
219
         */
 
220
         QRect titleRect() const;
 
221
 
 
222
    public:
 
223
        /**
 
224
         * Handles widget and layout creation, call the base implementation when subclassing this member.
 
225
         */
 
226
        virtual void init();
 
227
        /**
 
228
         * Handles SettingButtons, call the base implementation when subclassing this member.
 
229
         */
 
230
        virtual void reset( unsigned long changed );
 
231
        virtual void borders( int& left, int& right, int& top, int& bottom ) const;
 
232
        virtual void show();
 
233
        virtual void resize(const QSize& s);
 
234
        virtual QSize minimumSize() const;
 
235
        virtual void maximizeChange();
 
236
        virtual void desktopChange();
 
237
        virtual void shadeChange();
 
238
        virtual void iconChange();
 
239
        virtual void activeChange();
 
240
        virtual void captionChange();
 
241
    public Q_SLOTS:
 
242
        void keepAboveChange(bool above);
 
243
        void keepBelowChange(bool below);
 
244
        void slotMaximize();
 
245
        void slotShade();
 
246
        void slotKeepAbove();
 
247
        void slotKeepBelow();
 
248
        void menuButtonPressed();
 
249
        void menuButtonReleased();
 
250
    public:
 
251
        virtual Position mousePosition(const QPoint &point) const;
 
252
 
 
253
        virtual bool eventFilter( QObject* o, QEvent* e );
 
254
        virtual void resizeEvent(QResizeEvent *e);
 
255
        virtual void mouseDoubleClickEvent(QMouseEvent *e);
 
256
        virtual void wheelEvent(QWheelEvent *e);
 
257
 
 
258
    private:
 
259
        void resetLayout();
 
260
 
 
261
        void moveWidget(int x, int y, QWidget *widget) const;
 
262
        void resizeWidget(int w, int h, QWidget *widget) const;
 
263
 
 
264
        typedef QVector <KCommonDecorationButton*> ButtonContainer; ///< If the entry is 0, it's a spacer.
 
265
        int buttonContainerWidth(const ButtonContainer &btnContainer, bool countHidden = false) const;
 
266
        void addButtons(ButtonContainer &btnContainer, const QString& buttons, bool isLeft);
 
267
 
 
268
        KCommonDecorationButton *m_button[NumButtons];
 
269
 
 
270
        ButtonContainer m_buttonsLeft;
 
271
        ButtonContainer m_buttonsRight;
 
272
 
 
273
        QWidget *m_previewWidget;
 
274
 
 
275
        // button hiding for small windows
 
276
        void calcHiddenButtons();
 
277
        int btnHideMinWidth;
 
278
        int btnHideLastWidth;
 
279
 
 
280
        bool closing; // for menu doubleclick closing...
 
281
 
 
282
        KCommonDecorationPrivate *d;
 
283
};
 
284
 
 
285
/**
 
286
 * Title bar buttons of KCommonDecoration need to inherit this class.
 
287
 */
 
288
class KWIN_EXPORT KCommonDecorationButton : public QAbstractButton
 
289
{
 
290
    Q_OBJECT
 
291
 
 
292
    friend class KCommonDecoration;
 
293
 
 
294
    public:
 
295
        KCommonDecorationButton(ButtonType type, KCommonDecoration *parent);
 
296
        virtual ~KCommonDecorationButton();
 
297
 
 
298
        /**
 
299
         * These flags specify what has changed, e.g. the reason for a reset().
 
300
         */
 
301
        enum
 
302
        {
 
303
            ManualReset     = 1 << 0, ///< The button might want to do a full reset for some reason...
 
304
            SizeChange      = 1 << 1, ///< The button size changed @see setSize()
 
305
            ToggleChange    = 1 << 2, ///< The button toggle state has changed @see setToggleButton()
 
306
            StateChange     = 1 << 3, ///< The button has been set pressed or not... @see setOn()
 
307
            IconChange      = 1 << 4, ///< The window icon has been changed
 
308
            DecorationReset = 1 << 5  ///< E.g. when decoration colors have changed
 
309
        };
 
310
        /**
 
311
         * Initialize the button after size change etc.
 
312
         */
 
313
        virtual void reset(unsigned long changed) = 0;
 
314
        /**
 
315
         * @returns the KCommonDecoration the button belongs to.
 
316
         */
 
317
        KCommonDecoration *decoration() const;
 
318
        /**
 
319
         * @returns the button type.
 
320
         * @see ButtonType
 
321
         */
 
322
        ButtonType type() const;
 
323
 
 
324
        /**
 
325
         * Whether the button is left of the titlebar or not.
 
326
         */
 
327
        bool isLeft() const;
 
328
 
 
329
        /**
 
330
         * Set which mouse buttons the button should honor. Used e.g. to prevent accidental right mouse clicks.
 
331
         */
 
332
        void setRealizeButtons(int btns);
 
333
        /**
 
334
         * Set the button size.
 
335
         */
 
336
        void setSize(const QSize &s);
 
337
        /**
 
338
         * Set/update the button's tool tip
 
339
         */
 
340
        void setTipText(const QString &tip);
 
341
        /**
 
342
         * The mouse button that has been clicked last time.
 
343
         */
 
344
        Qt::MouseButtons lastMousePress() const { return m_lastMouse; }
 
345
 
 
346
        QSize sizeHint() const;
 
347
 
 
348
    protected:
 
349
        void setToggleButton(bool toggle);
 
350
        void setOn(bool on);
 
351
        void setLeft(bool left);
 
352
        void mousePressEvent(QMouseEvent *e);
 
353
        void mouseReleaseEvent(QMouseEvent *e);
 
354
 
 
355
    private:
 
356
        KCommonDecoration *m_decoration;
 
357
        ButtonType m_type;
 
358
        int m_realizeButtons;
 
359
        QSize m_size;
 
360
        Qt::MouseButtons m_lastMouse;
 
361
 
 
362
        bool m_isLeft;
 
363
 
 
364
        KCommonDecorationButtonPrivate *d;
 
365
};
 
366
 
 
367
#endif // KCOMMONDECORATION_H
 
368
 
 
369
// kate: space-indent on; indent-width 4; mixedindent off; indent-mode cstyle;