File: shadows.cpp

package info (click to toggle)
bespin 0.r1552-1
  • links: PTS, VCS
  • area: main
  • in suites: wheezy
  • size: 9,848 kB
  • sloc: cpp: 24,647; sh: 523; xml: 459; makefile: 79
file content (279 lines) | stat: -rw-r--r-- 8,328 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
 *   Bespin library for Qt style, KWin decoration and everythng else
 *   Copyright 2007-2012 by Thomas Lübking <thomas.luebking@gmail.com>
 *
 *   This library is free software; you can redistribute it and/or modify
 *   it under the terms of the GNU Library General Public License version 2
 *
 *   This program is distributed in the hope that it will be useful,
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *   GNU Library General Public License for more details
 *
 *   You should have received a copy of the GNU Library General Public
 *   License along with this program; if not, write to the
 *   Free Software Foundation, Inc.,
 *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

#include <QEvent>
#include <QImage>
#include <QPainter>
#include <QPixmap>
#include <QX11Info>

#include <QtDebug>

#include "FX.h"
#include "shadows.h"
#include "tileset.h"

using namespace Bespin;

#ifdef Q_WS_X11
#include "xproperty.h"
class ShadowManager : public QObject {
public:
    ShadowManager() : QObject() {}
protected:
    bool eventFilter(QObject *o, QEvent *e)
    {
        if (e->type() == QEvent::Show)
        if (QWidget *w = qobject_cast<QWidget*>(o))
        if (w->isWindow() && w->testAttribute(Qt::WA_WState_Created) && w->internalWinId())
            Shadows::set(w->winId(), Shadows::Small);
        return false;
    }
};

static ShadowManager *shadowManager = 0;
static uint size[2] = { 12, 64 };
static QColor color(0,0,0,0);
static bool halo(false);

static QPixmap (*pixmaps[2])[8] = {0,0};
static unsigned long globalShadowData[2][12];
#endif

static QPixmap nativePixmap(const QPixmap &qtPix)
{
#ifdef Q_WS_X11
    QPainter pd(const_cast<QPixmap*>(&qtPix)); pd.drawTiledPixmap(qtPix.rect(), FX::dither()); pd.end();
    if (FX::usesXRender() || qtPix.isNull())
        return qtPix;

    Pixmap xPix = XCreatePixmap(QX11Info::display(), QX11Info::appRootWindow(), qtPix.width(), qtPix.height(), 32);
    QPixmap qtXPix(QPixmap::fromX11Pixmap( xPix, QPixmap::ExplicitlyShared ));
    QPainter p(&qtXPix);
    p.setCompositionMode(QPainter::CompositionMode_Source);
    p.drawPixmap(0, 0, qtPix);
    p.end();
    return qtXPix;
#else
    return qtPix; // just for GCC - makes no sense at all anyway
#endif
}

static unsigned long*
shadowData(Shadows::Type t, bool storeToRoot)
{
#ifdef Q_WS_X11
    unsigned long _12 = 12;
    unsigned long *data = XProperty::get<unsigned long>(QX11Info::appRootWindow(), XProperty::bespinShadow[t-1], XProperty::LONG, &_12);
    if (!data)
    {
#if VARYING_SHADOWS
#warning *** COMPILING WITH VARYING SHADOW PIXMAPS ***
        const int sz = size[t == Shadows::Large];
#else
        int sz = size[1];
#endif
        if (halo)
        {
            globalShadowData[t-1][8] = globalShadowData[t-1][9] = // yes, next line
            globalShadowData[t-1][10] = globalShadowData[t-1][11] = 3*(sz-4)/4;
        }
        else
        {
            globalShadowData[t-1][8] = (sz-4)/2;
            globalShadowData[t-1][9] = 2*(sz-4)/3;
            globalShadowData[t-1][10] = sz-4;
            globalShadowData[t-1][11] = 2*(sz-4)/3;
        }

        if (!pixmaps[t-1])
        {
            QPixmap *store = new QPixmap[8];
            pixmaps[t-1] = (QPixmap (*)[8])store;

            // radial gradient requires the raster engine anyway and we need *working* ... -> QImage
            QImage shadow(2*sz+1, 2*sz+1, QImage::Format_ARGB32);
            shadow.fill(Qt::transparent);
#if VARYING_SHADOWS
            QRadialGradient rg(QPoint(sz+1,sz+1),sz);
            const QRect shadowRect(shadow.rect());
#else
            QRect shadowRect(shadow.rect());
            QRadialGradient rg;
            if (t == Shadows::Small)
            {
                const int d = (size[1] - (size[0]+2))/2;
                shadowRect.adjust(d+3, d, -(d+3), -(d+6));
                sz = qMin(shadowRect.width(), shadowRect.height()) / 2;
                rg = QRadialGradient(shadowRect.center(), sz);
                sz = size[0];
            }
            else
                rg = QRadialGradient(shadowRect.center(), sz);
#endif
            QPainter p(&shadow);
            p.setPen(Qt::NoPen);

            if (halo)
            {
                rg.setColorAt(0, QColor(255,255,255,192)); rg.setColorAt(0.98, QColor(255,255,255,0));
                p.setBrush(rg);
                p.drawRect(shadowRect);
            }

            QColor transparent = color; transparent.setAlpha(0);
            color.setAlpha(112-sz);
            rg.setColorAt(0, color); rg.setColorAt(0.98, transparent);
            p.setBrush(rg);
            p.drawRect(shadowRect);

            rg.setStops(QGradientStops());

            color.setAlpha(96-sz);
            rg.setColorAt(0, color); rg.setColorAt(0.80, transparent);
            p.setBrush(rg);
            p.drawRect(shadowRect);

            rg.setStops(QGradientStops());

            color.setAlpha(72-sz);
            rg.setColorAt(0, color); rg.setColorAt(0.66, transparent);
            p.setBrush(rg);
            p.drawRect(shadowRect);

            p.setCompositionMode(QPainter::CompositionMode_DestinationIn);
            p.setRenderHint(QPainter::Antialiasing);
            p.setBrush(Qt::transparent);
            p.drawRoundedRect(shadow.rect().adjusted(globalShadowData[t-1][9], globalShadowData[t-1][8],
                                                     -globalShadowData[t-1][11], -globalShadowData[t-1][10]), 8,8);

            p.end();
#if VARYING_SHADOWS
            Tile::Set shadowSet(shadow,sz,sz,1,1);
#else
            Tile::Set shadowSet(shadow,size[1],size[1],1,1);
#endif

            store[0] = nativePixmap(shadowSet.tile(Tile::TopMid));
            store[1] = nativePixmap(shadowSet.tile(Tile::TopRight));
            store[2] = nativePixmap(shadowSet.tile(Tile::MidRight));
            store[3] = nativePixmap(shadowSet.tile(Tile::BtmRight));
            store[4] = nativePixmap(shadowSet.tile(Tile::BtmMid));
            store[5] = nativePixmap(shadowSet.tile(Tile::BtmLeft));
            store[6] = nativePixmap(shadowSet.tile(Tile::MidLeft));
            store[7] = nativePixmap(shadowSet.tile(Tile::TopLeft));
        }
        for (int i = 0; i < 8; ++i)
            globalShadowData[t-1][i] = (*pixmaps[t-1])[i].handle();

        data = &globalShadowData[t-1][0];
        if (storeToRoot)
            XProperty::set(QX11Info::appRootWindow(), XProperty::bespinShadow[t-1], data, XProperty::LONG, 12);
    }
    return data;
#else
    return 0;  // just for GCC - makes no sense at all anyway
#endif
}

bool
Shadows::areSet(WId id)
{
#ifdef Q_WS_X11
    unsigned long _12 = 12;
    return XProperty::get<unsigned long>(id, XProperty::kwinShadow, XProperty::LONG, &_12);
#endif
    return false;
}

void
Shadows::cleanUp()
{
#ifdef Q_WS_X11
    delete shadowManager; shadowManager = 0;
    for (int i = 0; i < 2; ++i)
    {
        if (pixmaps[i])
        {
            if (!FX::usesXRender())
            {
                for (int j = 0; j < 8; ++j)
                    XFreePixmap(QX11Info::display(), (*pixmaps[i])[j].handle());
            }
            delete [] pixmaps[i];
            pixmaps[i] = 0L;
        }
    }
#endif
}

void
Shadows::manage(QWidget *w)
{
#ifdef Q_WS_X11
    if (!shadowManager)
        shadowManager = new ShadowManager;
    w->removeEventFilter(shadowManager);
    w->installEventFilter(shadowManager);
#endif
}

void
Shadows::set(WId id, Shadows::Type t, bool storeToRoot)
{
#ifdef Q_WS_X11
    if (id == QX11Info::appRootWindow()) {
        qWarning("BESPIN WARNING! Setting shadow to ROOT window is NOT supported");
        return;
    }
    switch(t)
    {
    case Shadows::None:
        XProperty::remove(id, XProperty::kwinShadow);
        break;
    case Shadows::Large:
    case Shadows::Small:
        XProperty::set(id, XProperty::kwinShadow, shadowData(t, storeToRoot), XProperty::LONG, 12);
    default:
        break;
    }
#endif
}

void Shadows::setColor(QColor c)
{
#ifdef Q_WS_X11
    color = c;
#endif
}

void Shadows::setHalo(bool h)
{
#ifdef Q_WS_X11
    halo = h;
#endif
}

void
Shadows::setSize(int small, int big)
{
#ifdef Q_WS_X11
    size[0] = qMin(72, qMax(8, small));
    size[1] = qMin(72, qMax(8, big));
#endif
}