summaryrefslogtreecommitdiffstats
path: root/src/kwin/iaora.cpp
blob: 887baffeba91dba7d845be6407fe6fff355172c0 (plain)
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
/* Ia Ora KWin window decoration
  Copyright (C) 2006 Laurent Montel <lmontel@mandriva.com>
  Based on plastik code
  Copyright (C) 2003-2005 Sandro Giessl <sandro@giessl.com>

  based on the window decoration "Web":
  Copyright (C) 2001 Rik Hemsley (rikkus) <rik@kde.org>

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public
  License as published by the Free Software Foundation; either
  version 2 of the License, or (at your option) any later version.

  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
  General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; see the file COPYING.  If not, write to
  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  Boston, MA 02110-1301, USA.
 */

#include <qbitmap.h>
#include <qpainter.h>
#include <qimage.h>
#include <qapplication.h>
#include <kconfig.h>
#include <kpixmap.h>
#include <kpixmapeffect.h>

#include "misc.h"
#include "iaora.h"
#include "iaora.moc"
#include "iaoraclient.h"
#include "iaorabutton.h"

namespace KWinIaOra
{

IaOraHandler::IaOraHandler()
{
    memset(m_pixmaps, 0, sizeof(QPixmap*)*NumPixmaps*2*2); // set elements to 0
    memset(m_bitmaps, 0, sizeof(QBitmap*)*NumButtonIcons*2);

    reset(0);
}

IaOraHandler::~IaOraHandler()
{
    for (int t=0; t < 2; ++t)
        for (int a=0; a < 2; ++a)
            for (int i=0; i < NumPixmaps; ++i)
                delete m_pixmaps[t][a][i];
    for (int t=0; t < 2; ++t)
        for (int i=0; i < NumButtonIcons; ++i)
            delete m_bitmaps[t][i];
}

bool IaOraHandler::reset(unsigned long changed)
{
    // we assume the active font to be the same as the inactive font since the control
    // center doesn't offer different settings anyways.
    m_titleFont = KDecoration::options()->font(true, false); // not small
    m_titleFontTool = KDecoration::options()->font(true, true); // small

    // check if we are in reverse layout mode
    m_reverse = QApplication::reverseLayout();

    // read in the configuration
    readConfig();

    // pixmaps probably need to be updated, so delete the cache.
    for (int t=0; t < 2; ++t) {
        for (int a=0; a < 2; ++a) {
            for (int i=0; i < NumPixmaps; i++) {
                if (m_pixmaps[t][a][i]) {
                    delete m_pixmaps[t][a][i];
                    m_pixmaps[t][a][i] = 0;
                }
            }
        }
    }
    for (int t=0; t < 2; ++t) {
        for (int i=0; i < NumButtonIcons; i++) {
            if (m_bitmaps[t][i]) {
                delete m_bitmaps[t][i];
                m_bitmaps[t][i] = 0;
            }
        }
    }

    // Do we need to "hit the wooden hammer" ?
    bool needHardReset = true;
    // TODO: besides the Color and Font settings I can maybe handle more changes
    //       without a hard reset. I will do this later...
    if (changed & SettingColors || changed & SettingFont)
    {
        needHardReset = false;
    } else if (changed & SettingButtons) {
        // handled by KCommonDecoration
        needHardReset = false;
    }

    if (needHardReset) {
        return true;
    } else {
        resetDecorations(changed);
        return false;
    }
}

KDecoration* IaOraHandler::createDecoration( KDecorationBridge* bridge )
{
        return new IaOraClient( bridge, this );
}

bool IaOraHandler::supports( Ability ability )
{
    switch( ability )
    {
        case AbilityAnnounceButtons:
        case AbilityButtonMenu:
        case AbilityButtonOnAllDesktops:
        case AbilityButtonSpacer:
        case AbilityButtonHelp:
        case AbilityButtonMinimize:
        case AbilityButtonMaximize:
        case AbilityButtonClose:
        case AbilityButtonAboveOthers:
        case AbilityButtonBelowOthers:
        case AbilityButtonShade:
            return true;
        default:
            return false;
    };
}

void IaOraHandler::readConfig()
{
    // create a config object
    KConfig config("kwiniaorarc");
    config.setGroup("General");

    // grab settings
    m_titleShadow    = config.readBoolEntry("TitleShadow", true);

    QFontMetrics fm(m_titleFont);  // active font = inactive font
    int titleHeightMin = config.readNumEntry("MinTitleHeight", 22);
    // The title should strech with bigger font sizes!
    m_titleHeight = QMAX(titleHeightMin, fm.height() + 4); // 4 px for the shadow etc.
    // have an even title/button size so the button icons are fully centered...
    if ( m_titleHeight%2 == 0)
        m_titleHeight++;

    fm = QFontMetrics(m_titleFontTool);  // active font = inactive font
    int titleHeightToolMin = config.readNumEntry("MinTitleHeightTool", 22);
    // The title should strech with bigger font sizes!
    m_titleHeightTool = QMAX(titleHeightToolMin, fm.height() ); // don't care about the shadow etc.
    // have an even title/button size so the button icons are fully centered...
    if ( m_titleHeightTool%2 == 0)
        m_titleHeightTool++;

    m_menuClose = config.readBoolEntry("CloseOnMenuDoubleClick", true);
}

QColor IaOraHandler::getBorderColor( KWinIaOra::ColorType type, const bool active)
{
    //Force colors
    switch (type) {
    case Border1:
        return QColor( "#EFF3F7" );
        break;
    case Border2:
        return QColor( "#DFE7EF" );
        break;
    case Border3:
        return QColor( "#C7D3DF" );
        break;
    default:
        return Qt::black;
    }
    return Qt::black;
}

QColor IaOraHandler::getShadowColor()
{
    QColor col = KDecoration::options()->color(ColorTitleBar, true);
    if ( col == QColor("#4964AE") )
    {
        return QColor( "#000000" );
    }
    //default Arctic color
    else if ( col == QColor("#8ec7ff") )
    {
        return QColor( "#666666" );
    }
    else if ( col == QColor("7BAAE7") )
    {
        return QColor( "#666666" );
    }
    //default Orange color
    else if ( col == QColor("#F7B610") )
    {
        return QColor( "#666666" );
    }
    //default Gray color
    else if ( col == QColor("#c7d3df") )
    {
        return QColor( "#333333" );
    }
    return QColor( "#666666" );
}

QColor IaOraHandler::getGradientColor( KWinIaOra::ColorType type, const bool active)
{
    QColor col = KDecoration::options()->color(ColorTitleBar, active);
    //default Blue color
    if ( active )
    {
        if ( col == QColor("#4964AE") )
        {
            switch (type) {
            case TitleGradient1:
                return QColor("#8EA2CF");
                break;
            case TitleGradient2:
                return QColor("#415DA6");
                break;
            case TitleGradient4:
                return QColor("#4964AE");
                break;
            case TitleGradient3:
                return QColor("#21459C");
                break;
            default:
                return Qt::black;
            }
        }
        //default Smooth color
        else if ( col == QColor("#7BAAE7") )
        {
            switch (type) {
            case TitleGradient1:
                return QColor("#ADCFFF");
                break;
            case TitleGradient2:
                return QColor("#5A8AD6");
                break;
            case TitleGradient4:
                return QColor("#7BAAE7");
                break;
            case TitleGradient3:
                return QColor("#427DC6");
                break;
            default:
                return Qt::black;
            }
        }
        //default Orange color
        else if ( col == QColor("#F7B610") )
        {
            switch (type) {
            case TitleGradient1:
                return QColor("#FFCB10");
                break;
            case TitleGradient2:
                return QColor("#ffa208");
                break;
            case TitleGradient4:
                return QColor("#f7b610");
                break;
            case TitleGradient3:
                return QColor("#f79600");
                break;
            default:
                return Qt::black;
            }
        }
        //default Arctic color
        else if ( col == QColor("#8ec7ff") )
        {
            switch (type) {
            case TitleGradient1:
                return QColor("#c7dfff");
                break;
            case TitleGradient2:
                return QColor("#79beff");
                break;
            case TitleGradient4:
                return QColor("#8ec7ff");
                break;
            case TitleGradient3:
                return QColor("#69b6ff");
                break;
            default:
                return Qt::black;
            }
        }
        //default Gray color
        else if ( col == QColor("#c7d3df") )
        {
            switch (type) {
            case TitleGradient1:
                return QColor("#cfd7df");
                break;
            case TitleGradient2:
                return QColor("#a6b2c7");
                break;
            case TitleGradient4:
                return QColor("#c7d3df");
                break;
            case TitleGradient3:
                return QColor("#8692a6");
                break;
            default:
                return Qt::black;
            }
        }
        else
        {
            switch (type) {
            case TitleGradient1:
                return col.light( 150 );
                break;
            case TitleGradient2:
                return col.dark( 112 );
                break;
            case TitleGradient4:
                return col;
                break;
            case TitleGradient3:
                return col.dark( 130 );
                break;
            default:
                return Qt::black;
            }
        }
    }
    else
    {
        if ( col == QColor( "#EFF3F7" ) )
        {
            switch (type) {
            case TitleGradient1:
                return QColor( "#DFE7EF" );
                break;
            case TitleGradient2:
                return QColor( "#C7D3DF" );
                break;
            case TitleGradient4:
                return QColor( "#CFD7DF" );
                break;
            case TitleGradient3:
                return QColor( "#B6C3CF" );
                break;
            default:
                return Qt::black;
            }
        }
        else
        {
            //create algo
            switch (type) {
            case TitleGradient1:
                return col.dark(107);
                break;
            case TitleGradient2:
                return col.dark( 117 );
                break;
            case TitleGradient4:
                return col.dark( 115 );
                break;
            case TitleGradient3:
                return col.dark( 131 );
                break;
            default:
                return Qt::black;
            }
        }

    }
    return Qt::black;
}

QColor IaOraHandler::getColor(KWinIaOra::ColorType type, const bool active)
{
    switch (type) {
	case TitleBorder:
	    return KDecoration::options()->color(ColorTitleBar, active);
        case TitleGradient1:
        case TitleGradient2:
        case TitleGradient4:
        case TitleGradient3:
            return getGradientColor( type, active);
        case Border1:
        case Border2:
        case Border3:
            return getBorderColor(  type, active);
        case ShadeTitleLight:
            return alphaBlendColors(KDecoration::options()->color(ColorTitleBar, active),
                                    Qt::white, active?205:215);
            //todo verify
        case ShadeTitleDark:
            return alphaBlendColors(KDecoration::options()->color(ColorTitleBar, active),
                                    Qt::black, active?205:215);
            break;
        case TitleFont:
            return KDecoration::options()->color(ColorFont, active);
        default:
            return Qt::black;
    }
}

void IaOraHandler::pretile( QPixmap *&pix, int size, Qt::Orientation dir ) const
{
    QPixmap *newpix;
    QPainter p;

    if ( dir == Qt::Horizontal )
        newpix = new QPixmap( size, pix->height() );
    else
        newpix = new QPixmap( pix->width(), size );

    p.begin( newpix );
    p.drawTiledPixmap( newpix->rect(), *pix ) ;
    p.end();

    delete pix;
    pix = newpix;
}

const QPixmap &IaOraHandler::pixmap(Pixmaps type, bool active, bool toolWindow)
{
    if (m_pixmaps[toolWindow][active][type])
        return *m_pixmaps[toolWindow][active][type];

    QPixmap *pm = 0;

    switch (type) {
        case TitleBarTileTop:
        {
            pm = new QPixmap(1, 3);
            QPainter painter(pm);
            painter.setPen(getColor(TitleBorder, active));
            painter.drawPoint(0,0);

            painter.fillRect(0,1,pm->width(),pm->height()-1,getColor(TitleGradient1, active));
            painter.end();
            pretile(pm, 64, Qt::Horizontal);
            break;
        } 
        case TitleBarTile:
        {
            const int titleBarTileHeight = (toolWindow ? m_titleHeightTool : m_titleHeight) + 2;
            // gradient used as well in TitleBarTileTop as TitleBarTile
            const int gradientHeight = 2 + titleBarTileHeight;
            QPixmap gradient(1, gradientHeight);
            QPainter painter(&gradient);
            KPixmap tempPixmap;
            tempPixmap.resize(1, gradientHeight/2);
            KPixmapEffect::gradient(tempPixmap,
                                    getColor(TitleGradient1, active),
                                    getColor(TitleGradient2, active),
                                    KPixmapEffect::VerticalGradient);
            painter.drawPixmap(0,0, tempPixmap);
            tempPixmap.resize(1, gradientHeight - (gradientHeight/2));
            KPixmapEffect::gradient(tempPixmap,
                                    getColor(TitleGradient3, active) ,
                                    getColor(TitleGradient4, active),
                                    KPixmapEffect::VerticalGradient);
            painter.drawPixmap(0,gradientHeight/2, tempPixmap);
            painter.end();

            pm = new QPixmap(1, titleBarTileHeight);
            painter.begin(pm);
            painter.drawPixmap(0, 0, gradient, 0,2);
            painter.setPen(getColor(TitleGradient3, active).dark(110) );
            painter.drawPoint(0,titleBarTileHeight-1);

            painter.end();

            pretile(pm, 64, Qt::Horizontal);

            break;
        }

        case TitleBarLeft:
        {
            const int h = 4 + (toolWindow ? m_titleHeightTool : m_titleHeight) + 2;

            pm = new QPixmap(3, h);
            QPainter painter(pm);

            painter.drawTiledPixmap(0,0, 3, 3, pixmap(TitleBarTileTop, active, toolWindow) );
            painter.drawTiledPixmap(0,3, 3, h-3, pixmap(TitleBarTile, active, toolWindow) );

            painter.setPen(getColor(TitleBorder, active) );
            painter.drawLine(0,0, 0,h);

            break;
        }
        case TitleBarRight:
        {
            const int h = 4 + (toolWindow ? m_titleHeightTool : m_titleHeight) + 2;

            pm = new QPixmap(3, h);
            QPainter painter(pm);

            painter.drawTiledPixmap(0,0, 3, 3, pixmap(TitleBarTileTop, active, toolWindow) );
            painter.drawTiledPixmap(0,3, 3, h-3, pixmap(TitleBarTile, active, toolWindow) );

            painter.setPen(getColor(TitleBorder, active));
            painter.drawLine(2,0, 2,h);

            break;
        }

        case BorderLeftTile:
        {
            pm = new QPixmap(3, 1);
            QPainter painter(pm);
            painter.setPen(getColor(Border3, active) );
            painter.drawPoint(0, 0);
            painter.setPen(getColor(Border2, active) );
            painter.drawPoint(1, 0);

            painter.setPen(getColor(Border1, active) );
            painter.drawPoint(2,0);
            painter.end();
            pretile(pm, 64, Qt::Vertical);
            break;
        }

        case BorderRightTile:
        {

            pm = new QPixmap(3, 1);
            QPainter painter(pm);
            painter.setPen(getColor(Border1, active) );
            painter.drawPoint(0,0);
            painter.setPen(getColor(Border2, active) );
            painter.drawPoint(1, 0);
            painter.setPen(getColor(Border3, active) );
            painter.drawPoint(2, 0);
            painter.end();
            pretile(pm, 64, Qt::Vertical);

            break;
        }

        case BorderBottomLeft:
        {
            pm = new QPixmap(3, 3);
            QPainter painter(pm);
            painter.drawTiledPixmap(0,0,3,3, pixmap(BorderBottomTile, active, toolWindow) );
            painter.setPen(getColor(Border3, active) );
            painter.drawLine(0,0, 0,3);

            painter.setPen(getColor(Border1, active) );
            painter.drawLine(2,0, 2,0);
            painter.end();

            break;
        }

        case BorderBottomRight:
        {

            pm = new QPixmap(3, 3);
            QPainter painter(pm);
            painter.drawTiledPixmap(0,0,3,3, pixmap(BorderBottomTile, active, toolWindow) );
            painter.setPen(getColor(Border3, active) );
            painter.drawLine(2,0, 2,3);
            painter.setPen(getColor(Border2, active) );
            painter.drawLine(1,0, 1,1);

            painter.setPen(getColor(Border1, active) );
            painter.drawLine(0,0, 0,0);

            painter.end();

            break;
        }

        case BorderBottomTile:
        default:
        {
            pm = new QPixmap(1, 3);
            QPainter painter(pm);

            painter.setPen(getColor(Border1, active) );
            painter.drawPoint(0,0);
            painter.setPen(getColor(Border2, active) );
            painter.drawPoint(0,1);
            painter.setPen(getColor(Border3, active) );
            painter.drawPoint(0, 2);
            painter.end();

            pretile(pm, 64, Qt::Horizontal);

            break;
        }
    }

    m_pixmaps[toolWindow][active][type] = pm;
    return *pm;
}

const QBitmap &IaOraHandler::buttonBitmap(ButtonIcon type, const QSize &size, bool toolWindow)
{
    int typeIndex = type;

    // btn icon size...
    int reduceW = 0, reduceH = 0;
    if(size.width()>14) {
        reduceW = static_cast<int>(2*(size.width()/3.5) );
    }
    else
        reduceW = 6;
    if(size.height()>14)
        reduceH = static_cast<int>(2*(size.height()/3.5) );
    else
        reduceH = 6;

    int w = size.width() - reduceW;
    int h = size.height() - reduceH;

    if (m_bitmaps[toolWindow][typeIndex] && m_bitmaps[toolWindow][typeIndex]->size()==QSize(w,h) )
        return *m_bitmaps[toolWindow][typeIndex];

    // no matching pixmap found, create a new one...

    delete m_bitmaps[toolWindow][typeIndex];
    m_bitmaps[toolWindow][typeIndex] = 0;

    QBitmap bmp = IconEngine::icon(type /*icon*/, QMIN(w,h) );
    QBitmap *bitmap = new QBitmap(bmp);
    m_bitmaps[toolWindow][typeIndex] = bitmap;
    return *bitmap;
}


// make the handler accessible to other classes...
static IaOraHandler *handler = 0;
IaOraHandler* Handler()
{
    return handler;
}

} // KWinPlastik

//////////////////////////////////////////////////////////////////////////////
// Plugin Stuff                                                             //
//////////////////////////////////////////////////////////////////////////////

extern "C"
{
    KDE_EXPORT KDecorationFactory *create_factory()
    {
        KWinIaOra::handler = new KWinIaOra::IaOraHandler();
        return KWinIaOra::handler;
    }
}