~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to kwin/libkdecorations/kdecoration.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

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) 2003 Lubos Lunak <l.lunak@kde.org>
 
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
#include "kdecoration.h"
 
26
#include "kdecoration_p.h"
 
27
 
 
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
 
37
 
 
38
#include "kdecorationfactory.h"
 
39
#include "kdecorationbridge.h"
 
40
 
 
41
/*
 
42
 
 
43
Extending KDecoration:
 
44
======================
 
45
 
 
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.
 
51
 
 
52
KCommonDecoration would have to be extended the same way, adding KCommonDecoration2
 
53
inheriting KCommonDecoration and adding the new API matching KDecoration2.
 
54
 
 
55
*/
 
56
 
 
57
 
 
58
KDecorationOptions* KDecoration::options_;
 
59
 
 
60
KDecoration::KDecoration(KDecorationBridge* bridge, KDecorationFactory* factory)
 
61
    :   bridge_(bridge),
 
62
        w_(NULL),
 
63
        factory_(factory)
 
64
{
 
65
    factory->addDecoration(this);
 
66
}
 
67
 
 
68
KDecoration::~KDecoration()
 
69
{
 
70
    factory()->removeDecoration(this);
 
71
    delete w_;
 
72
}
 
73
 
 
74
const KDecorationOptions* KDecoration::options()
 
75
{
 
76
    return options_;
 
77
}
 
78
 
 
79
void KDecoration::createMainWidget(Qt::WFlags flags)
 
80
{
 
81
    // FRAME check flags?
 
82
    QWidget *w = new QWidget(initialParentWidget(), initialWFlags() | flags);
 
83
    w->setObjectName(QLatin1String("decoration widget"));
 
84
    w->setAttribute(Qt::WA_PaintOnScreen);
 
85
    if (options()->showTooltips())
 
86
        w->setAttribute(Qt::WA_AlwaysShowToolTips);
 
87
    setMainWidget(w);
 
88
}
 
89
 
 
90
void KDecoration::setMainWidget(QWidget* w)
 
91
{
 
92
    assert(w_ == NULL);
 
93
    w_ = w;
 
94
    w->setMouseTracking(true);
 
95
    widget()->resize(geometry().size());
 
96
}
 
97
 
 
98
QWidget* KDecoration::initialParentWidget() const
 
99
{
 
100
    return bridge_->initialParentWidget();
 
101
}
 
102
 
 
103
Qt::WFlags KDecoration::initialWFlags() const
 
104
{
 
105
    return bridge_->initialWFlags();
 
106
}
 
107
 
 
108
bool KDecoration::isActive() const
 
109
{
 
110
    return bridge_->isActive();
 
111
}
 
112
 
 
113
bool KDecoration::isCloseable() const
 
114
{
 
115
    return bridge_->isCloseable();
 
116
}
 
117
 
 
118
bool KDecoration::isMaximizable() const
 
119
{
 
120
    return bridge_->isMaximizable();
 
121
}
 
122
 
 
123
KDecoration::MaximizeMode KDecoration::maximizeMode() const
 
124
{
 
125
    return bridge_->maximizeMode();
 
126
}
 
127
 
 
128
bool KDecoration::isMinimizable() const
 
129
{
 
130
    return bridge_->isMinimizable();
 
131
}
 
132
 
 
133
bool KDecoration::providesContextHelp() const
 
134
{
 
135
    return bridge_->providesContextHelp();
 
136
}
 
137
 
 
138
int KDecoration::desktop() const
 
139
{
 
140
    return bridge_->desktop();
 
141
}
 
142
 
 
143
bool KDecoration::isModal() const
 
144
{
 
145
    return bridge_->isModal();
 
146
}
 
147
 
 
148
bool KDecoration::isShadeable() const
 
149
{
 
150
    return bridge_->isShadeable();
 
151
}
 
152
 
 
153
bool KDecoration::isShade() const
 
154
{
 
155
    return bridge_->isShade();
 
156
}
 
157
 
 
158
bool KDecoration::isSetShade() const
 
159
{
 
160
    return bridge_->isSetShade();
 
161
}
 
162
 
 
163
bool KDecoration::keepAbove() const
 
164
{
 
165
    return bridge_->keepAbove();
 
166
}
 
167
 
 
168
bool KDecoration::keepBelow() const
 
169
{
 
170
    return bridge_->keepBelow();
 
171
}
 
172
 
 
173
bool KDecoration::isMovable() const
 
174
{
 
175
    return bridge_->isMovable();
 
176
}
 
177
 
 
178
bool KDecoration::isResizable() const
 
179
{
 
180
    return bridge_->isResizable();
 
181
}
 
182
 
 
183
NET::WindowType KDecoration::windowType(unsigned long supported_types) const
 
184
{
 
185
    // this one is also duplicated in KDecorationFactory
 
186
    return bridge_->windowType(supported_types);
 
187
}
 
188
 
 
189
QIcon KDecoration::icon() const
 
190
{
 
191
    return bridge_->icon();
 
192
}
 
193
 
 
194
QString KDecoration::caption() const
 
195
{
 
196
    return bridge_->caption();
 
197
}
 
198
 
 
199
void KDecoration::processMousePressEvent(QMouseEvent* e)
 
200
{
 
201
    return bridge_->processMousePressEvent(e);
 
202
}
 
203
 
 
204
void KDecoration::showWindowMenu(const QRect &pos)
 
205
{
 
206
    bridge_->showWindowMenu(pos);
 
207
}
 
208
 
 
209
void KDecoration::showWindowMenu(QPoint pos)
 
210
{
 
211
    bridge_->showWindowMenu(pos);
 
212
}
 
213
 
 
214
void KDecoration::performWindowOperation(WindowOperation op)
 
215
{
 
216
    bridge_->performWindowOperation(op);
 
217
}
 
218
 
 
219
void KDecoration::setMask(const QRegion& reg, int mode)
 
220
{
 
221
    bridge_->setMask(reg, mode);
 
222
}
 
223
 
 
224
void KDecoration::clearMask()
 
225
{
 
226
    bridge_->setMask(QRegion(), 0);
 
227
}
 
228
 
 
229
bool KDecoration::isPreview() const
 
230
{
 
231
    return bridge_->isPreview();
 
232
}
 
233
 
 
234
QRect KDecoration::geometry() const
 
235
{
 
236
    return bridge_->geometry();
 
237
}
 
238
 
 
239
QRect KDecoration::iconGeometry() const
 
240
{
 
241
    return bridge_->iconGeometry();
 
242
}
 
243
 
 
244
QRegion KDecoration::unobscuredRegion(const QRegion& r) const
 
245
{
 
246
    return bridge_->unobscuredRegion(r);
 
247
}
 
248
 
 
249
WId KDecoration::windowId() const
 
250
{
 
251
    return bridge_->windowId();
 
252
}
 
253
 
 
254
void KDecoration::closeWindow()
 
255
{
 
256
    bridge_->closeWindow();
 
257
}
 
258
 
 
259
void KDecoration::maximize(Qt::MouseButtons button)
 
260
{
 
261
    performWindowOperation(options()->operationMaxButtonClick(button));
 
262
}
 
263
 
 
264
void KDecoration::maximize(MaximizeMode mode)
 
265
{
 
266
    bridge_->maximize(mode);
 
267
}
 
268
 
 
269
void KDecoration::minimize()
 
270
{
 
271
    bridge_->minimize();
 
272
}
 
273
 
 
274
void KDecoration::showContextHelp()
 
275
{
 
276
    bridge_->showContextHelp();
 
277
}
 
278
 
 
279
void KDecoration::setDesktop(int desktop)
 
280
{
 
281
    bridge_->setDesktop(desktop);
 
282
}
 
283
 
 
284
void KDecoration::toggleOnAllDesktops()
 
285
{
 
286
    if (isOnAllDesktops())
 
287
        setDesktop(bridge_->currentDesktop());
 
288
    else
 
289
        setDesktop(NET::OnAllDesktops);
 
290
}
 
291
 
 
292
void KDecoration::titlebarDblClickOperation()
 
293
{
 
294
    bridge_->titlebarDblClickOperation();
 
295
}
 
296
 
 
297
void KDecoration::titlebarMouseWheelOperation(int delta)
 
298
{
 
299
    bridge_->titlebarMouseWheelOperation(delta);
 
300
}
 
301
 
 
302
void KDecoration::setShade(bool set)
 
303
{
 
304
    bridge_->setShade(set);
 
305
}
 
306
 
 
307
void KDecoration::setKeepAbove(bool set)
 
308
{
 
309
    bridge_->setKeepAbove(set);
 
310
}
 
311
 
 
312
void KDecoration::setKeepBelow(bool set)
 
313
{
 
314
    bridge_->setKeepBelow(set);
 
315
}
 
316
 
 
317
void KDecoration::emitKeepAboveChanged(bool above)
 
318
{
 
319
    keepAboveChanged(above);
 
320
}
 
321
 
 
322
void KDecoration::emitKeepBelowChanged(bool below)
 
323
{
 
324
    keepBelowChanged(below);
 
325
}
 
326
 
 
327
bool KDecoration::drawbound(const QRect&, bool)
 
328
{
 
329
    return false;
 
330
}
 
331
 
 
332
bool KDecoration::windowDocked(Position)
 
333
{
 
334
    return false;
 
335
}
 
336
 
 
337
void KDecoration::reset(unsigned long)
 
338
{
 
339
}
 
340
 
 
341
void KDecoration::grabXServer()
 
342
{
 
343
    bridge_->grabXServer(true);
 
344
}
 
345
 
 
346
void KDecoration::ungrabXServer()
 
347
{
 
348
    bridge_->grabXServer(false);
 
349
}
 
350
 
 
351
KDecoration::Position KDecoration::mousePosition(const QPoint& p) const
 
352
{
 
353
    const int range = 16;
 
354
    int bleft, bright, btop, bbottom;
 
355
    borders(bleft, bright, btop, bbottom);
 
356
    btop = qMin(btop, 4);   // otherwise whole titlebar would have resize cursor
 
357
 
 
358
    Position m = PositionCenter;
 
359
 
 
360
    if ((p.x() > bleft && p.x() < widget()->width() - bright)
 
361
            && (p.y() > btop && p.y() < widget()->height() - bbottom))
 
362
        return PositionCenter;
 
363
 
 
364
    if (p.y() <= qMax(range, btop) && p.x() <= qMax(range, bleft))
 
365
        m = PositionTopLeft;
 
366
    else if (p.y() >= widget()->height() - qMax(range, bbottom)
 
367
            && p.x() >= widget()->width() - qMax(range, bright))
 
368
        m = PositionBottomRight;
 
369
    else if (p.y() >= widget()->height() - qMax(range, bbottom) && p.x() <= qMax(range, bleft))
 
370
        m = PositionBottomLeft;
 
371
    else if (p.y() <= qMax(range, btop) && p.x() >= widget()->width() - qMax(range, bright))
 
372
        m = PositionTopRight;
 
373
    else if (p.y() <= btop)
 
374
        m = PositionTop;
 
375
    else if (p.y() >= widget()->height() - bbottom)
 
376
        m = PositionBottom;
 
377
    else if (p.x() <= bleft)
 
378
        m = PositionLeft;
 
379
    else if (p.x() >= widget()->width() - bright)
 
380
        m = PositionRight;
 
381
    else
 
382
        m = PositionCenter;
 
383
    return m;
 
384
}
 
385
 
 
386
QRect KDecoration::transparentRect() const
 
387
{
 
388
    if (KDecorationBridgeUnstable *bridge2 = dynamic_cast<KDecorationBridgeUnstable*>(bridge_))
 
389
        return bridge2->transparentRect();
 
390
    else
 
391
        return QRect();
 
392
}
 
393
 
 
394
 
 
395
KDecorationUnstable::KDecorationUnstable(KDecorationBridge* bridge, KDecorationFactory* factory)
 
396
    : KDecoration(bridge, factory)
 
397
{
 
398
    Q_ASSERT(dynamic_cast< KDecorationBridgeUnstable* >(bridge));
 
399
}
 
400
 
 
401
KDecorationUnstable::~KDecorationUnstable()
 
402
{
 
403
}
 
404
 
 
405
bool KDecorationUnstable::compositingActive() const
 
406
{
 
407
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->compositingActive();
 
408
}
 
409
 
 
410
void KDecorationUnstable::padding(int &left, int &right, int &top, int &bottom) const
 
411
{
 
412
    left = right = top = bottom = 0;
 
413
}
 
414
 
 
415
// Window tabbing
 
416
 
 
417
bool KDecorationUnstable::isClientGroupActive()
 
418
{
 
419
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->isClientGroupActive();
 
420
}
 
421
 
 
422
QList< ClientGroupItem > KDecorationUnstable::clientGroupItems() const
 
423
{
 
424
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->clientGroupItems();
 
425
}
 
426
 
 
427
long KDecorationUnstable::itemId(int index)
 
428
{
 
429
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->itemId(index);
 
430
}
 
431
 
 
432
int KDecorationUnstable::visibleClientGroupItem()
 
433
{
 
434
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->visibleClientGroupItem();
 
435
}
 
436
 
 
437
void KDecorationUnstable::setVisibleClientGroupItem(int index)
 
438
{
 
439
    static_cast< KDecorationBridgeUnstable* >(bridge_)->setVisibleClientGroupItem(index);
 
440
}
 
441
 
 
442
void KDecorationUnstable::moveItemInClientGroup(int index, int before)
 
443
{
 
444
    static_cast< KDecorationBridgeUnstable* >(bridge_)->moveItemInClientGroup(index, before);
 
445
}
 
446
 
 
447
void KDecorationUnstable::moveItemToClientGroup(long itemId, int before)
 
448
{
 
449
    static_cast< KDecorationBridgeUnstable* >(bridge_)->moveItemToClientGroup(itemId, before);
 
450
}
 
451
 
 
452
void KDecorationUnstable::removeFromClientGroup(int index, const QRect& newGeom)
 
453
{
 
454
    static_cast< KDecorationBridgeUnstable* >(bridge_)->removeFromClientGroup(index, newGeom);
 
455
}
 
456
 
 
457
void KDecorationUnstable::closeClientGroupItem(int index)
 
458
{
 
459
    static_cast< KDecorationBridgeUnstable* >(bridge_)->closeClientGroupItem(index);
 
460
}
 
461
 
 
462
void KDecorationUnstable::closeAllInClientGroup()
 
463
{
 
464
    static_cast< KDecorationBridgeUnstable* >(bridge_)->closeAllInClientGroup();
 
465
}
 
466
 
 
467
void KDecorationUnstable::displayClientMenu(int index, const QPoint& pos)
 
468
{
 
469
    static_cast< KDecorationBridgeUnstable* >(bridge_)->displayClientMenu(index, pos);
 
470
}
 
471
 
 
472
KDecoration::WindowOperation KDecorationUnstable::buttonToWindowOperation(Qt::MouseButtons button)
 
473
{
 
474
    return static_cast< KDecorationBridgeUnstable* >(bridge_)->buttonToWindowOperation(button);
 
475
}
 
476
 
 
477
QString KDecorationDefines::clientGroupItemDragMimeType()
 
478
{
 
479
    return "text/ClientGroupItem";
 
480
}
 
481
 
 
482
KDecorationOptions::KDecorationOptions()
 
483
    : d(new KDecorationOptionsPrivate)
 
484
{
 
485
    assert(KDecoration::options_ == NULL);
 
486
    KDecoration::options_ = this;
 
487
}
 
488
 
 
489
KDecorationOptions::~KDecorationOptions()
 
490
{
 
491
    assert(KDecoration::options_ == this);
 
492
    KDecoration::options_ = NULL;
 
493
    delete d;
 
494
}
 
495
 
 
496
QColor KDecorationOptions::color(ColorType type, bool active) const
 
497
{
 
498
    return(d->colors[type + (active ? 0 : NUM_COLORS)]);
 
499
}
 
500
 
 
501
QFont KDecorationOptions::font(bool active, bool small) const
 
502
{
 
503
    if (small)
 
504
        return(active ? d->activeFontSmall : d->inactiveFontSmall);
 
505
    else
 
506
        return(active ? d->activeFont : d->inactiveFont);
 
507
}
 
508
 
 
509
QPalette KDecorationOptions::palette(ColorType type, bool active) const
 
510
{
 
511
    int idx = type + (active ? 0 : NUM_COLORS);
 
512
    if (d->pal[idx])
 
513
        return(*d->pal[idx]);
 
514
    // TODO: Why construct the palette this way? Is it worth porting to Qt4?
 
515
    //d->pal[idx] = new QPalette(Qt::black, d->colors[idx], d->colors[idx].light(150),
 
516
    //                           d->colors[idx].dark(), d->colors[idx].dark(120),
 
517
    //                           Qt::black, QApplication::palette().active().
 
518
    //                           base());
 
519
    d->pal[idx] = new QPalette(d->colors[idx]);
 
520
    return(*d->pal[idx]);
 
521
}
 
522
 
 
523
unsigned long KDecorationOptions::updateSettings(KConfig* config)
 
524
{
 
525
    return d->updateSettings(config);
 
526
}
 
527
 
 
528
bool KDecorationOptions::customButtonPositions() const
 
529
{
 
530
    return d->custom_button_positions;
 
531
}
 
532
 
 
533
QString KDecorationOptions::titleButtonsLeft() const
 
534
{
 
535
    return d->title_buttons_left;
 
536
}
 
537
 
 
538
QString KDecorationOptions::defaultTitleButtonsLeft()
 
539
{
 
540
    return "MS";
 
541
}
 
542
 
 
543
QString KDecorationOptions::titleButtonsRight() const
 
544
{
 
545
    return d->title_buttons_right;
 
546
}
 
547
 
 
548
QString KDecorationOptions::defaultTitleButtonsRight()
 
549
{
 
550
    return "HIA__X";
 
551
}
 
552
 
 
553
bool KDecorationOptions::showTooltips() const
 
554
{
 
555
    return d->show_tooltips;
 
556
}
 
557
 
 
558
KDecorationOptions::BorderSize KDecorationOptions::preferredBorderSize(KDecorationFactory* factory) const
 
559
{
 
560
    assert(factory != NULL);
 
561
    if (d->cached_border_size == BordersCount)   // invalid
 
562
        d->cached_border_size = d->findPreferredBorderSize(d->border_size,
 
563
                                factory->borderSizes());
 
564
    return d->cached_border_size;
 
565
}
 
566
 
 
567
bool KDecorationOptions::moveResizeMaximizedWindows() const
 
568
{
 
569
    return d->move_resize_maximized_windows;
 
570
}
 
571
 
 
572
KDecorationDefines::WindowOperation KDecorationOptions::operationMaxButtonClick(Qt::MouseButtons button) const
 
573
{
 
574
    return button == Qt::RightButton ? d->opMaxButtonRightClick :
 
575
           button == Qt::MidButton ?   d->opMaxButtonMiddleClick :
 
576
           d->opMaxButtonLeftClick;
 
577
}
 
578
 
 
579
void KDecorationOptions::setOpMaxButtonLeftClick(WindowOperation op)
 
580
{
 
581
    d->opMaxButtonLeftClick = op;
 
582
}
 
583
 
 
584
void KDecorationOptions::setOpMaxButtonRightClick(WindowOperation op)
 
585
{
 
586
    d->opMaxButtonRightClick = op;
 
587
}
 
588
 
 
589
void KDecorationOptions::setOpMaxButtonMiddleClick(WindowOperation op)
 
590
{
 
591
    d->opMaxButtonMiddleClick = op;
 
592
}
 
593
 
 
594
void KDecorationOptions::setBorderSize(BorderSize bs)
 
595
{
 
596
    d->border_size = bs;
 
597
    d->cached_border_size = BordersCount; // invalid
 
598
}
 
599
 
 
600
void KDecorationOptions::setCustomButtonPositions(bool b)
 
601
{
 
602
    d->custom_button_positions = b;
 
603
}
 
604
 
 
605
void KDecorationOptions::setTitleButtonsLeft(const QString& b)
 
606
{
 
607
    d->title_buttons_left = b;
 
608
}
 
609
 
 
610
void KDecorationOptions::setTitleButtonsRight(const QString& b)
 
611
{
 
612
    d->title_buttons_right = b;
 
613
}
 
614
 
 
615
#include "kdecoration.moc"