summaryrefslogtreecommitdiffstats
path: root/client/polyester.cc
blob: 62866fa8d38b08378882983615fb575e6321f051 (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
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
//////////////////////////////////////////////////////////////////////////////
// polyester.cc
// -------------------
// Polyester window decoration for KDE
// Copyright (c) 2006 Marco Martin
// -------------------
// derived from Smooth Blend
// Copyright (c) 2005 Ryan Nickell
// -------------------
// Shadow engine from Plastik decoration
// Copyright (C) 2003 Sandro Giessl <ceebx@users.sourceforge.net>
// -------------------
// Please see the header file for copyright and license information.
//////////////////////////////////////////////////////////////////////////////


#include <kconfig.h>
#include <kdeversion.h>
#include <kglobal.h>
#include <kglobalsettings.h>
#include <klocale.h>
#include <kpixmap.h>
#include <kimageeffect.h>
#include <kpixmapeffect.h>
#include <kpixmap.h>

#include <qbitmap.h>
#include <qlabel.h>
#include <qlayout.h>
#include <qpainter.h>
#include <qtooltip.h>
#include <qtimer.h>
#include <qapplication.h>
#include <qsettings.h>
#include <math.h>
#include <stdlib.h>

#include "polyester.h"
#include "buttons.h"
#include "shadow.h"
#include "../style/misc.h"

using namespace polyester;

//////////////////////////////////////////////////////////////////////////////
// polyesterFactory Class
//////////////////////////////////////////////////////////////////////////////
polyesterFactory* factory=NULL;

bool polyesterFactory::initialized_              = false;
Qt::AlignmentFlags polyesterFactory::titlealign_ = Qt::AlignHCenter;
int polyesterFactory::contrast_                  = 6;
bool polyesterFactory::cornerflags_              = true;
int polyesterFactory::titlesize_                 = 22;
int polyesterFactory::buttonsize_                = 18;
bool polyesterFactory::squareButton_             = false;
int polyesterFactory::framesize_                 = 2;
int polyesterFactory::roundsize_                 = 50;
int polyesterFactory::titleBarStyle_             = 0;
int polyesterFactory::buttonStyle_               = 1;
bool polyesterFactory::titleshadow_              = true;
bool polyesterFactory::nomodalbuttons            = false;
bool polyesterFactory::lightBorder_              = true;
bool polyesterFactory::animatebuttons            = true;
int polyesterFactory::btnComboBox                = 0;
bool polyesterFactory::menuClose                 = false;

// global constants
static const int TOPMARGIN       = 4; // do not change
static const int DECOHEIGHT      = 4; // do not change
static const int SIDETITLEMARGIN = 2;
// Default button layout
const char default_left[]  = "M";
const char default_right[] = "HIAX";

static const uint TIMERINTERVAL = 25; // msec
static const uint ANIMATIONSTEPS = 20;

extern "C" KDecorationFactory* create_factory() {
    return new polyester::polyesterFactory();
}

//////////////////////////////////////////////////////////////////////////////
// polyesterFactory()
// ----------------
// Constructor

polyesterFactory::polyesterFactory() {
    readConfig();
    initialized_ = true;
}

//////////////////////////////////////////////////////////////////////////////
// ~polyesterFactory()
// -----------------
// Destructor

polyesterFactory::~polyesterFactory() {
    initialized_ = false;
}

//////////////////////////////////////////////////////////////////////////////
// createDecoration()
// -----------------
// Create the decoration

KDecoration* polyesterFactory::createDecoration(KDecorationBridge* b) {
    return new polyesterClient(b, this);
}

//////////////////////////////////////////////////////////////////////////////
// reset()
// -------
// Reset the handler. Returns true if decorations need to be remade, false if
// only a repaint is necessary

bool polyesterFactory::reset(unsigned long changed) {
    // read in the configuration
    initialized_ = false;
    bool confchange = readConfig();
    initialized_ = true;

    if (confchange ||
            (changed & (SettingDecoration | SettingButtons | SettingBorder))) {
        return true;
    } else {
        resetDecorations(changed);
        return false;
    }
}

//////////////////////////////////////////////////////////////////////////////
// readConfig()
// ------------
// Read in the configuration file

bool polyesterFactory::readConfig() {
    // create a config object
    KConfig config("kwinpolyesterrc");
    config.setGroup("General");

    // grab settings
    QString value = config.readEntry("TitleAlignment", "AlignHCenter");
    if (value == "AlignLeft")
        titlealign_ = Qt::AlignLeft;
    else if (value == "AlignHCenter")
        titlealign_ = Qt::AlignHCenter;
    else if (value == "AlignRight")
        titlealign_ = Qt::AlignRight;

    QSettings globalSettings;
    contrast_ = globalSettings.readNumEntry("/Qt/KDE/contrast", 6);
    cornerflags_ = config.readBoolEntry("RoundCorners", true);
    titlesize_ = config.readNumEntry("TitleSize",20);
    titleBarStyle_ = config.readNumEntry("TitleBarStyle", 0);
    buttonStyle_ = config.readNumEntry("ButtonStyle", 1);

    buttonsize_ = config.readNumEntry("ButtonSize",18);
    squareButton_ = config.readBoolEntry("SquareButton", false);
    framesize_ = config.readNumEntry("FrameSize",2);
    roundsize_ = config.readNumEntry("RoundPercent",50);


    titleshadow_ = config.readBoolEntry("TitleShadow", true);
    lightBorder_ = config.readBoolEntry("LightBorder", true);
    animatebuttons = config.readBoolEntry("AnimateButtons", true);
    nomodalbuttons = config.readBoolEntry("NoModalButtons", false);
    btnComboBox = config.readNumEntry("ButtonComboBox", 0);
    menuClose = config.readBoolEntry("CloseOnMenuDoubleClick");

    if(buttonsize_ > titlesize_ - framesize_)
    {
        buttonsize_ = titlesize_-framesize_;
    }

    return true;
}

//////////////////////////////////////////////////////////////////////////////
// polyesterButton Class 
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// polyesterButton()
// ---------------
// Constructor
polyesterButton::polyesterButton(polyesterClient *parent, const char *name, const QString& tip, ButtonType type, int button_size, bool squareButton, bool toggle): QButton(parent->widget(), name), 
     client_(parent), 
     type_(type), 
     size_(button_size),
     deco_(0), 
     lastmouse_(NoButton),
     buttonImgActive_created(false),
     buttonImgInactive_created(false),
     hover_(false)
{
    setBackgroundMode(NoBackground);

    int buttonWidth = button_size;
    if( squareButton )
      buttonProportions_ = 1;
    else
    {
      buttonProportions_ = sqrt(2);
      buttonWidth = (int)round(button_size*buttonProportions_);
      //makes always centered icons
      if( buttonWidth % 2 != 0 )
        buttonWidth++;
    }

    setFixedSize( buttonWidth, button_size);
    setCursor(arrowCursor);
    QToolTip::add(this, tip);
    setToggleButton(toggle);
    //button animation setup
    animTmr = new QTimer(this);
    connect(animTmr, SIGNAL(timeout() ), this, SLOT(animate() ) );
    connect(this, SIGNAL(pressed() ), this, SLOT(buttonClicked() ) );
    connect(this, SIGNAL(released() ), this, SLOT(buttonReleased() ) );
    animProgress = 0;
    m_clicked=false;
}

polyesterButton::~polyesterButton() {
    if ( deco_ )
        delete deco_;
}

//////////////////////////////////////////////////////////////////////////////
// sizeHint()
// ----------
// Return size hint

QSize polyesterButton::sizeHint() const {
    return QSize(size_, size_);
}

//////////////////////////////////////////////////////////////////////////////
// buttonClicked()
// ----------
// Button animation progress reset so we don't get any leave event animation
// when the mouse is still pressed
void polyesterButton::buttonClicked() {
    m_clicked=true;
    animProgress=0;
}
void polyesterButton::buttonReleased() {
    //This doesn't work b/c a released() signal is thrown when a leaveEvent occurs
    //m_clicked=false;
}

//////////////////////////////////////////////////////////////////////////////
// animate()
// ----------
// Button animation timing
void polyesterButton::animate() {
    animTmr->stop();
    if (!::factory->animateButtons() )
      return;

    if(hover_) {
        if(animProgress < ANIMATIONSTEPS) {
            animProgress++;
            animTmr->start(TIMERINTERVAL, true); // single-shot
        }
    } else {
        if(animProgress > 0) {
            animProgress--;
            animTmr->start(TIMERINTERVAL, true); // single-shot
        }
    }
    repaint(false);
}
//////////////////////////////////////////////////////////////////////////////
// enterEvent()
// ------------
// Mouse has entered the button

void polyesterButton::enterEvent(QEvent *e) {
    // we wanted to pass on the event
    QButton::enterEvent(e);
    // we want to do mouseovers, so keep track of it here
    hover_=true;
    if(!m_clicked)
    {
        animate();
    }
}

//////////////////////////////////////////////////////////////////////////////
// leaveEvent()
// ------------
// Mouse has left the button

void polyesterButton::leaveEvent(QEvent *e) {
    // we wanted to pass on the event
    QButton::leaveEvent(e);
    // we want to do mouseovers, so keep track of it here
    hover_=false; 
    if(!m_clicked)
    {
        animate();
    }
}

//////////////////////////////////////////////////////////////////////////////
// mousePressEvent()
// -----------------
// Button has been pressed

void polyesterButton::mousePressEvent(QMouseEvent* e) {
    lastmouse_ = e->button();

    // translate and pass on mouse event
    int button = LeftButton;
    if ((type_ != ButtonMax) && (e->button() != LeftButton)) {
        button = NoButton; // middle & right buttons inappropriate
    }
    QMouseEvent me(e->type(), e->pos(), e->globalPos(),
                   button, e->state());
    QButton::mousePressEvent(&me);
}

//////////////////////////////////////////////////////////////////////////////
// mouseReleaseEvent()
// -----------------
// Button has been released

void polyesterButton::mouseReleaseEvent(QMouseEvent* e) {
    lastmouse_ = e->button();

    // translate and pass on mouse event
    int button = LeftButton;
    if ((type_ != ButtonMax) && (e->button() != LeftButton)) {
        button = NoButton; // middle & right buttons inappropriate
    }
    QMouseEvent me(e->type(), e->pos(), e->globalPos(), button, e->state());
    QButton::mouseReleaseEvent(&me);
    if(m_clicked)
    {
        m_clicked=false;
    }
}

void polyesterButton::setOn(bool on)
{
    QButton::setOn(on);
}

void polyesterButton::setDown(bool on)
{
    QButton::setDown(on);
}

//////////////////////////////////////////////////////////
// getButtonImage()
// ----------------
// get the button QImage based on type and window mode
QImage polyesterButton::getButtonImage(ButtonType type)
{
    QImage finalImage;
    switch(type) {
        case ButtonClose:
            finalImage = uic_findImage( "close.png" );
            break;
        case ButtonHelp:
            finalImage = uic_findImage( "help.png" );
            break;
        case ButtonMin:
            finalImage = uic_findImage( "minimize.png" );
            break;
        case ButtonMax:
            if(client_->maximizeMode() == KDecorationDefines::MaximizeFull)
            {
                finalImage = uic_findImage( "restore.png" );
            }
            else
            {
                finalImage = uic_findImage( "maximize.png" );
            }
            break;
        case ButtonSticky:
            if(client_->isOnAllDesktops())
            {
                finalImage = uic_findImage( "circle2.png" );
            }
            else
            {
                finalImage = uic_findImage( "circle.png" );
            }
            break;
        case ButtonShade:
            if(client_->isShade())
            {
                finalImage = uic_findImage( "unsplat.png" );
            }
            else
            {
                finalImage = uic_findImage( "splat.png" );
            }
            break;
        case ButtonAbove:
            if(client_->keepAbove())
            {
                finalImage = uic_findImage( "keep_above_lit.png" );
            }
            else
            {
                finalImage = uic_findImage( "keep_above.png" );
            }
            break;
        case ButtonBelow:
            if(client_->keepBelow())
            {
                finalImage = uic_findImage( "keep_below_lit.png" );
            }
            else
            {
                finalImage = uic_findImage( "keep_below.png" );
            }
            break;
        default:
            finalImage = uic_findImage( "splat.png" );
            break;
    }
    if(qGray(KDecoration::options()->color(KDecoration::ColorButtonBg, client_->isActive()).rgb()) < 150)
        finalImage.invertPixels();

    return finalImage;
}

//////////////////////////////////////////////////////////
// drawButton()
// -------------------------
// draw the pixmap button

void polyesterButton::drawButton( QPainter *painter ) {
    if ( !polyesterFactory::initialized() )
        return ;

    int newWidth = width() - 2;
    int newHeight = height() - 2;
    int dx = (width() - newWidth) / 2;
    int dy = (height() - newHeight) / 2;
    QImage tmpResult;
    QColorGroup group;
    QColor redColor(red);
    bool active = client_->isActive();

    genButtonPix(active);

    QPixmap backgroundTile = client_->getTitleBarTile(active);
    group = KDecoration::options()->colorGroup(KDecoration::ColorButtonBg, active);

    //draw the titlebar behind the buttons and app icons
    if ((client_->maximizeMode()==client_->MaximizeFull) && !KDecoration::options()->moveResizeMaximizedWindows())
    {
        painter->drawTiledPixmap(0, 0, width(), height(), backgroundTile, 0, 1);
    }
    else
    {
        painter->drawTiledPixmap(0, 0, width(), height(), backgroundTile,
                                 0, y()-::factory->frameSize());
    }

    //the menu button doesn't have a button appearance
    if (type_ == ButtonMenu)
    {
      //slight movement to show the menu button is depressed
      if (isDown()) {
          dx++;
          dy++;
      }
      QPixmap menuButtonPixmap(client_->icon().pixmap(QIconSet::Small, QIconSet::Normal));
      QImage menuButtonImage(menuButtonPixmap.convertToImage());
      //draw the menu button the same size as the other buttons
      //using QIconSet::Large gives us a 32x32 icon to resize, resizing larger than
      //that may produce pixilation of the image
      //painter->setPen(group.background());

      painter->drawImage( dx+(int)round(abs(width()-size_)/2), dy, menuButtonImage.smoothScale(size_-2, newHeight) );
      return;
    }

    //build the button image with the icon superimposed to the button
    QImage buttonImage = (active?(buttonImgActive):(buttonImgInactive))->copy();
    KImageEffect::blendOnLower( (int)round(abs(width()-size_)/2), 0,
                                getButtonImage(type_).smoothScale( size_,size_),
                                buttonImage );

    //highlight on a mouse over repaint
    double factor = animProgress * 0.05;//0.13;

    //do the animation thing
    if( !isDown() && factor != 0.0 )
    {
      tmpResult = buttonImage.copy();
      //always colorize with a threatening red color the close button
      if( type_ == ButtonClose)
      {
        //tmpResult = KImageEffect::blend( QColor(180, 50, 50), buttonImage, factor );
        KImageEffect::desaturate( tmpResult, factor );
        KImageEffect::channelIntensity( tmpResult, factor/3, KImageEffect::Red );
        KImageEffect::channelIntensity( tmpResult, -factor/2, KImageEffect::Green );
        KImageEffect::channelIntensity( tmpResult, -factor/2, KImageEffect::Blue );
      }
      else
          switch(::factory->getBtnComboBox())
          {
              //colorize
              case 0:
                  if( type_ == ButtonMax)
                  {
                    KImageEffect::desaturate( tmpResult, factor );
                    KImageEffect::channelIntensity( tmpResult, -factor/3, KImageEffect::Red );
                    KImageEffect::channelIntensity( tmpResult, factor/2, KImageEffect::Green );
                    KImageEffect::channelIntensity( tmpResult, -factor/3, KImageEffect::Blue );
                  }
                  else if( type_ == ButtonMin)
                  {
                    KImageEffect::desaturate( tmpResult, factor );
                    KImageEffect::channelIntensity( tmpResult, factor/4, KImageEffect::Red );
                    KImageEffect::channelIntensity( tmpResult, factor/4, KImageEffect::Green );
                    KImageEffect::channelIntensity( tmpResult, -factor/2, KImageEffect::Blue );
                  }
                  else if( type_ == ButtonSticky)
                  {
                    KImageEffect::desaturate( tmpResult, factor );
                    KImageEffect::channelIntensity( tmpResult, -factor/3, KImageEffect::Red );
                    KImageEffect::channelIntensity( tmpResult, -factor/3, KImageEffect::Green );
                    KImageEffect::channelIntensity( tmpResult, factor/2, KImageEffect::Blue );
                  }
                  else
                  {
                    KImageEffect::desaturate( tmpResult, factor );
                    KImageEffect::channelIntensity( tmpResult, -factor/2, KImageEffect::Red );
                    KImageEffect::channelIntensity( tmpResult, factor/3, KImageEffect::Green );
                    KImageEffect::channelIntensity( tmpResult, factor/3, KImageEffect::Blue );
                  }
                  break;
              //intensify
              case 1:
                  KImageEffect::intensity( tmpResult, factor);
                  break;
              //fade
              case 2:
                  KImageEffect::intensity( tmpResult, -factor/2);

                  break;
          }
    }

    KImageEffect::blendOnLower(tmpResult, QPoint(1,1), buttonImage, QRect(1,1,width()-2, height()-2) );

    QPixmap finalButton = QPixmap(buttonImage);

    painter->drawPixmap( 0, 0, finalButton );
}

void polyesterButton::genButtonPix( bool active )
{
 if(active && buttonImgActive_created) return;
 else if(!active && buttonImgInactive_created) return;

 KPixmap tempPixmap;
 QPixmap *tempButton = new QPixmap(width(), height());
 QPainter painter(tempButton);

 QColorGroup group;
 group = KDecoration::options()->colorGroup(KDecoration::ColorButtonBg, client_->isActive());

 if( ::factory->buttonStyle() == BUTTON_GLASS )
    tempPixmap.resize(width()-2, (height()/2)-1);
 else
    tempPixmap.resize(width()-2, height()-2);

 /*
 if(  ::factory->buttonStyle() != BUTTON_FLAT  )
 {
  tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background().light(100+(::factory->buttonStyle()!=BUTTON_REVGRADIENT?60:-20)),
                                       group.background().light(100+(::factory->buttonStyle()!=BUTTON_GLASS?
                                                                       (::factory->buttonStyle()==BUTTON_REVGRADIENT?
                                                                          60:-20)
                                                                       :0)),
                                       KPixmapEffect::VerticalGradient
                                      );
 }
 else
 {
  tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background(),
                                       group.background(),
                                       KPixmapEffect::VerticalGradient
                                      );
 }
*/

 int contrast = ::factory->contrast();

 switch( ::factory->buttonStyle() )
 {
   case BUTTON_GRADIENT:
       tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background().light(105+contrast*8),
                                       group.background().dark(100+contrast*2),
                                       KPixmapEffect::VerticalGradient
                                      );
     break;
   case BUTTON_GLASS:
       tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background().light(105+contrast*8),
                                       group.background(),
                                       KPixmapEffect::VerticalGradient
                                      );
     break;
   case BUTTON_REVGRADIENT:
       tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background().dark(100+contrast*2),
                                       group.background().light(105+contrast*8),
                                       KPixmapEffect::VerticalGradient
                                      );
     break;

   //case BUTTON_FLAT:
   default:
       tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                       group.background(),
                                       group.background(),
                                       KPixmapEffect::VerticalGradient
                                      );
     break;
 }

 //draw the main area of the buttons
 //painter.begin(tempButton);
 painter.drawPixmap(1, 1, tempPixmap);

 //if the button is glass create the second gradient
 if( ::factory->buttonStyle() == BUTTON_GLASS )
 {
    tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                        group.background().dark(105+contrast),
                                        group.background(),
                                        KPixmapEffect::VerticalGradient
                                       );

    painter.drawPixmap(1, (height()/2), tempPixmap);
  }

 //border
 QColor borderColor = group.background().dark(160);
 painter.setPen( borderColor );
 painter.drawLine(0, 2, 0, height()-3);
 painter.drawLine(width()-1, 2, width()-1, height()-3);
 painter.drawLine(2, 0, width()-3, 0);
 painter.drawLine(2, height()-1, width()-3, height()-1);
 painter.drawPoint(1, 1);
 painter.drawPoint(width()-2, 1);
 painter.drawPoint(1, height()-2);
 painter.drawPoint(width()-2, height()-2);

 //little 3d effect
 painter.setPen( group.background().light(140) );
 painter.drawLine(1, 2, 1, height()-3);
 painter.drawLine(2, 1, width()-3, 1);
 if(!::factory->lightBorder())
   painter.setPen( group.background().dark(110) );
 painter.drawLine(width()-2, 2, width()-2, height()-3);
 painter.drawLine(2, height()-2, width()-3, height()-2);
 painter.end();

  //border antialiasing
 QImage *tempAlphaImg = new QImage( tempButton->convertToImage() );
 //tempAlphaImg = tempPixmap.convertToImage();
 tempAlphaImg->setAlphaBuffer(true);
 int borderRed = borderColor.red();
 int borderGreen = borderColor.green();
 int borderBlue = borderColor.blue();

 tempAlphaImg->setPixel(0,0, qRgba(borderRed, borderGreen , borderBlue, 40));
 tempAlphaImg->setPixel(width()-1, 0, qRgba(borderRed, borderGreen , borderBlue, 40));
 tempAlphaImg->setPixel(0,height()-1, qRgba(borderRed, borderGreen , borderBlue, 40));
 tempAlphaImg->setPixel(width()-1,height()-1, qRgba(borderRed, borderGreen , borderBlue, 40));

 tempAlphaImg->setPixel(0,1, qRgba(borderRed, borderGreen , borderBlue, 127));
 tempAlphaImg->setPixel(1,0, qRgba(borderRed, borderGreen , borderBlue, 127));

 tempAlphaImg->setPixel(width()-2,0, qRgba(borderRed, borderGreen , borderBlue, 127));
 tempAlphaImg->setPixel(width()-1,1, qRgba(borderRed, borderGreen , borderBlue, 127));

 tempAlphaImg->setPixel(0,height()-2, qRgba(borderRed, borderGreen , borderBlue, 127));
 tempAlphaImg->setPixel(1,height()-1, qRgba(borderRed, borderGreen , borderBlue, 127));

 tempAlphaImg->setPixel(width()-1,height()-2, qRgba(borderRed, borderGreen , borderBlue, 127));
 tempAlphaImg->setPixel(width()-2,height()-1, qRgba(borderRed, borderGreen , borderBlue, 127));


 if( client_->isActive() )
 {
   buttonImgActive = tempAlphaImg;
   buttonImgActive_created = true;
 }
 else
 {
   buttonImgInactive = tempAlphaImg;
   buttonImgInactive_created = true;
 }

}

//////////////////////////////////////////////////////////////////////////////
// polyesterClient Class
//////////////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////
// polyesterClient()
// ---------------
// Constructor

polyesterClient::polyesterClient(KDecorationBridge *b, KDecorationFactory *f)
    : KDecoration(b, f),
    mainLayout_(0),
    titleLayout_(0),
    topSpacer_(0),
    titleSpacer_(0),
    leftTitleSpacer_(0), rightTitleSpacer_(0),
    decoSpacer_(0),
    leftSpacer_(0), rightSpacer_(0),
    bottomSpacer_(0), windowSpacer_(0),
    aCaptionBuffer(0), iCaptionBuffer(0),
    aTitleBarTile(0), iTitleBarTile(0), aTitleBarTopTile(0), iTitleBarTopTile(0),
    pixmaps_created(false),
    //captionBufferDirty(true),
    closing(false),
    s_titleHeight(0),
    s_titleFont(QFont()),
    maskDirty(true),
    aDoubleBufferDirty(true),
    iDoubleBufferDirty(true)
    {
        aCaptionBuffer = new QPixmap();
        iCaptionBuffer = new QPixmap();

        //copying the most used configuration options
        frameSize = ::factory->frameSize();

        NET::WindowType type = windowType(NET::NormalMask | NET::DesktopMask |
                                          NET::DockMask | NET::ToolbarMask |
                                          NET::MenuMask | NET::DialogMask|
                                          NET::OverrideMask | NET::TopMenuMask |
                                          NET::UtilityMask | NET::SplashMask);
        if( type == NET::Utility || type == NET::Menu || type == NET::Toolbar )
        {
           titleSize = (int)((double)::factory->titleSize()/1.2);
           buttonSize = titleSize - 2;
           roundedCorners = false;
        }
        else
        {
           titleSize = ::factory->titleSize();
           //don't allow buttonSize > titleSize
           buttonSize = titleSize - 2 < ::factory->buttonSize()?
                                                 titleSize - 2 :
                                                 ::factory->buttonSize();
           roundedCorners = ::factory->roundedCorners();
        }

        //s_titleFont = isTool()?polyesterFactory::titleFontTool():polyesterFactory::titleFont();
        s_titleFont = options()->font();
        s_titleHeight = titleSize;
    }

//////////////////////////////////////////////////////////////////////////////////
// ~polyesterClient()
// --------------------
// Destructor
polyesterClient::~polyesterClient() {
    delete aCaptionBuffer;
    delete iCaptionBuffer;
}

void polyesterClient::create_pixmaps() {
    if(pixmaps_created)
        return;
    KPixmap tempPixmap;
    QPainter painter;


    // active top title bar tile
    tempPixmap.resize(1, s_titleHeight+frameSize);
    tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                         KDecoration::options()->color(ColorTitleBar, true).light(160),
                                         KDecoration::options()->color(ColorTitleBlend, true).light(104),
                                         KPixmapEffect::VerticalGradient
                                        );
    // tempPixmap.fill(KDecoration::options()->color(ColorTitleBar, true).light(150));
    aTitleBarTopTile = new QPixmap(1, s_titleHeight+frameSize);
    painter.begin(aTitleBarTopTile);
    painter.drawPixmap(0, 0, tempPixmap);
    painter.end();
    
    // inactive top title bar tile
    tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                         KDecoration::options()->color(ColorTitleBar, false).light(160),
                                         KDecoration::options()->color(ColorTitleBlend, false),
                                         KPixmapEffect::VerticalGradient
                                        );
    iTitleBarTopTile = new QPixmap(1, s_titleHeight+frameSize);
    painter.begin(iTitleBarTopTile);
    painter.drawPixmap(0, 0, tempPixmap);
    painter.end();

    // active title bar tile
    if( ::factory->titleBarStyle() == TITLEBAR_GLASS)
       tempPixmap.resize(1, ((s_titleHeight+frameSize)/2) - 1 );
    else
       tempPixmap.resize(1, s_titleHeight+frameSize);

    QColor color1;
    QColor color2;
    QColor glassColor;
    if( ::factory->titleBarStyle() == TITLEBAR_GLASS )
    {
      glassColor = alphaBlendColors( KDecoration::options()->color(ColorTitleBar, true),
                                     KDecoration::options()->color(ColorTitleBlend, true),
                                     127 );
      color1 = glassColor.light(105+::factory->contrast()*4);
      color2 = glassColor;
    }
    else
    {
      color1 = KDecoration::options()->color(ColorTitleBar, true);
      color2 = KDecoration::options()->color(ColorTitleBlend, true);
    }

    tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                         color1,
                                         color2,
                                         KPixmapEffect::VerticalGradient
                                        );

    aTitleBarTile = new QPixmap(1, s_titleHeight+frameSize);
    painter.begin(aTitleBarTile);
    painter.drawPixmap(0, 0, tempPixmap);

    if( ::factory->titleBarStyle() == TITLEBAR_GLASS )
    {
       int y = tempPixmap.height();
       tempPixmap.resize(1, (s_titleHeight+frameSize)-y );
       tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                            glassColor.dark(105+::factory->contrast()),
                                            glassColor,
                                            KPixmapEffect::VerticalGradient
                                           );
       painter.drawPixmap(0, y, tempPixmap);
    }
    //  painter.drawPixmap(0, 0, *aTitleBarTopTile);
    painter.end();

    // inactive title bar tile
    //de-glassify inactive titlebar
    if( ::factory->titleBarStyle() == TITLEBAR_GLASS )
       tempPixmap.resize(1, s_titleHeight+frameSize);
    tempPixmap = KPixmapEffect::gradient(tempPixmap,
                                         KDecoration::options()->color(ColorTitleBar, false),
                                         KDecoration::options()->color(ColorTitleBlend, false),
                                         KPixmapEffect::VerticalGradient
                                        );
    iTitleBarTile = new QPixmap(1, s_titleHeight+frameSize);
    painter.begin(iTitleBarTile);
    painter.drawPixmap(0, 0, tempPixmap);
    painter.end();


    pixmaps_created = true;
}

void polyesterClient::delete_pixmaps() {
    delete aTitleBarTopTile;
    aTitleBarTopTile = 0;

    delete iTitleBarTopTile;
    iTitleBarTopTile = 0;

    delete aTitleBarTile;
    aTitleBarTile = 0;

    delete iTitleBarTile;
    iTitleBarTile = 0;

    pixmaps_created = false;
}
//////////////////////////////////////////////////////////////////////////////
// init()
// ------
// Actual initializer for class

void polyesterClient::init() {
    createMainWidget( WResizeNoErase | WRepaintNoErase);
    widget()->installEventFilter(this);
    handlebar = frameSize < 4 ? 4 - frameSize : 0;

    // for flicker-free redraws
    widget()->setBackgroundMode(NoBackground);

    create_pixmaps();
    _resetLayout();

}
void polyesterClient::_resetLayout()
{
    // basic layout:
    //  _______________________________________________________________
    // |                         topSpacer                             |
    // |_______________________________________________________________|
    // | leftTitleSpacer | btns |  titlebar   | bts | rightTitleSpacer |
    // |_________________|______|_____________|_____|__________________|
    // |                         decoSpacer                            |
    // |_______________________________________________________________|
    // | |                                                           | |
    // | |                      windowWrapper                        | |
    // | |                                                           | |
    // |leftSpacer                                          rightSpacer|
    // |_|___________________________________________________________|_|
    // |                           bottomSpacer                        |
    // |_______________________________________________________________|
    //
    if (!::factory->initialized()) return;
    
    delete mainLayout_;
    delete titleLayout_;
    delete topSpacer_;
    delete titleSpacer_;
    delete leftTitleSpacer_;
    delete rightTitleSpacer_;
    delete decoSpacer_;
    delete leftSpacer_;
    delete rightSpacer_;
    delete bottomSpacer_;
    delete windowSpacer_;

    mainLayout_ = new QVBoxLayout(widget());
    // title
    titleLayout_ = new QHBoxLayout();
    QHBoxLayout *windowLayout_ = new QHBoxLayout();


    topSpacer_        = new QSpacerItem(1, frameSize, QSizePolicy::Expanding, QSizePolicy::Fixed);
    titlebar_         = new QSpacerItem(1, s_titleHeight,
                                        QSizePolicy::Expanding, QSizePolicy::Fixed);
    leftTitleSpacer_  = new QSpacerItem(frameSize, s_titleHeight,
                                        QSizePolicy::Fixed, QSizePolicy::Fixed);
    rightTitleSpacer_ = new QSpacerItem(frameSize, s_titleHeight,
                                        QSizePolicy::Fixed, QSizePolicy::Fixed);
    decoSpacer_       = new QSpacerItem(1, frameSize, QSizePolicy::Expanding, QSizePolicy::Fixed);
    leftSpacer_       = new QSpacerItem(frameSize, 1,
                                        QSizePolicy::Fixed, QSizePolicy::Expanding);
    rightSpacer_      = new QSpacerItem(frameSize, 1,
                                        QSizePolicy::Fixed, QSizePolicy::Expanding);
    bottomSpacer_     = new QSpacerItem(1, frameSize,
                                        QSizePolicy::Expanding, QSizePolicy::Fixed);
    
    // sizeof(...) is calculated at compile time
    memset(button, 0, sizeof(polyesterButton *) * ButtonTypeCount);

    // message in preview widget
    if (isPreview()) {
        windowLayout_->addWidget(
            new QLabel( i18n("<b><center>Polyester Preview</center></b>"), widget() ), 1 ); 
    } else {
        windowLayout_->addItem(new QSpacerItem(0, 0));
    }

    // setup titlebar buttons
    for (int n=0; n<ButtonTypeCount; n++)
        button[n] = 0;
    //Deal with the title and buttons
   titleLayout_->addItem(leftTitleSpacer_);

    if( !::factory->noModalButtons() || !isModal() || isResizable())
        addButtons(titleLayout_,
                   options()->customButtonPositions() ? options()->titleButtonsLeft() : QString(default_left),
                   buttonSize);
    titleLayout_->addItem(titlebar_);
    if( !::factory->noModalButtons() ||  !isModal() || isResizable())
        addButtons(titleLayout_,
                   options()->customButtonPositions() ? options()->titleButtonsRight() : QString(default_right),
                   buttonSize);
        titleLayout_->addItem(rightTitleSpacer_);

    //Mid - left side, middle contents and right side
    QHBoxLayout * midLayout_   = new QHBoxLayout();
    midLayout_->addItem(leftSpacer_);
    midLayout_->addLayout(windowLayout_);
    midLayout_->addItem(rightSpacer_);

    //Layout order
    mainLayout_->addItem( topSpacer_ );
    mainLayout_->addLayout( titleLayout_ );
    mainLayout_->addItem( decoSpacer_ );
    mainLayout_->addLayout( midLayout_ );
    mainLayout_->addItem( bottomSpacer_ );
    
    // connections
  //TODO: probably these two connections could be removed
  //  connect(this, SIGNAL(keepAboveChanged(bool)), SLOT(keepAboveChange(bool)));
  //  connect(this, SIGNAL(keepBelowChanged(bool)), SLOT(keepBelowChange(bool)));
}

//////////////////////////////////////////////////////////////////////////////
// addButtons()
// ------------
// Add buttons to title layout

void polyesterClient::addButtons(QBoxLayout *layout, const QString& s, int button_size) {
    QString tip;
    if (s.length() > 0) {
        for (unsigned n=0; n < s.length(); n++) {
            switch (s[n]) {
            case 'M': // Menu button
                if (!button[ButtonMenu]) {
                    button[ButtonMenu] =
                        new polyesterButton(this, "splat.png", i18n("Menu"),ButtonMenu,button_size,::factory->squareButton());
                    connect(button[ButtonMenu], SIGNAL(pressed()), this, SLOT(menuButtonPressed()));
                    connect(button[ButtonMenu], SIGNAL(released()), this, SLOT(menuButtonReleased()));
                    layout->addWidget(button[ButtonMenu]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'S': // Sticky button
                if (!button[ButtonSticky]) {
                    if (isOnAllDesktops()) {
                        tip = i18n("Un-Sticky");
                    } else {
                        tip = i18n("Sticky");
                    }
                    button[ButtonSticky] =
                        new polyesterButton(this, "circle.png", tip, ButtonSticky, button_size,::factory->squareButton(), true);
                    connect(button[ButtonSticky], SIGNAL(clicked()),
                            this, SLOT(toggleOnAllDesktops()));
                    layout->addWidget(button[ButtonSticky]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'H': // Help button
                if ((!button[ButtonHelp]) && providesContextHelp()) {
                    button[ButtonHelp] =
                        new polyesterButton(this, "help.png", i18n("Help"), ButtonHelp, button_size, ::factory->squareButton());
                    connect(button[ButtonHelp], SIGNAL(clicked()),
                            this, SLOT(showContextHelp()));
                    layout->addWidget(button[ButtonHelp]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'I': // Minimize button
                if ((!button[ButtonMin]) && isMinimizable())  {
                    button[ButtonMin] =
                        new polyesterButton(this, "minimize.png", i18n("Minimize"), ButtonMin, button_size,::factory->squareButton());
                    connect(button[ButtonMin], SIGNAL(clicked()),
                            this, SLOT(minimize()));
                    layout->addWidget(button[ButtonMin]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'A': // Maximize button
                if ((!button[ButtonMax]) && isMaximizable()) {
                    if (maximizeMode() == MaximizeFull) {
                        tip = i18n("Restore");
                    } else {
                        tip = i18n("Maximize");
                    }
                    button[ButtonMax]  =
                        new polyesterButton(this, "maximize.png", tip, ButtonMax, button_size,::factory->squareButton(), true);
                    connect(button[ButtonMax], SIGNAL(clicked()),
                            this, SLOT(maxButtonPressed()));
                    layout->addWidget(button[ButtonMax]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'X': // Close button
                if ((!button[ButtonClose]) && isCloseable()) {
                    button[ButtonClose] =
                        new polyesterButton(this, "close.png", i18n("Close"), ButtonClose, button_size, ::factory->squareButton());
                    connect(button[ButtonClose], SIGNAL(clicked()),
                            this, SLOT(closeWindow()));
                    layout->addWidget(button[ButtonClose]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'F': // Above button
                if ((!button[ButtonAbove])) {
                    button[ButtonAbove] =
                        new polyesterButton(this, "keep_above.png",
                                            i18n("Keep Above Others"), ButtonAbove, button_size, ::factory->squareButton(), true);
                    connect(button[ButtonAbove], SIGNAL(clicked()),
                            this, SLOT(aboveButtonPressed()));
                    layout->addWidget(button[ButtonAbove]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'B': // Below button
                if ((!button[ButtonBelow])) {
                    button[ButtonBelow] =
                        new polyesterButton(this, "keep_below.png",
                                            i18n("Keep Below Others"), ButtonBelow, button_size, ::factory->squareButton(), true);
                    connect(button[ButtonBelow], SIGNAL(clicked()),
                            this, SLOT(belowButtonPressed()));
                    layout->addWidget(button[ButtonBelow]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case 'L': // Shade button
                if ((!button[ButtonShade && isShadeable()])) {
                    if ( isSetShade()) {
                        tip = i18n("Unshade");
                    } else {
                        tip = i18n("Shade");
                    }
                    button[ButtonShade] =
                        new polyesterButton(this, "splat.png", tip, ButtonShade, button_size, ::factory->squareButton(), true);
                    connect(button[ButtonShade], SIGNAL(clicked()),
                            this, SLOT(shadeButtonPressed()));
                    layout->addWidget(button[ButtonShade]);
                    if (n < s.length()-1) layout->addSpacing(1);
                }
                break;

            case '_': // Spacer item
                layout->addSpacing(frameSize);
            }
        }
    }
}

//////////////////////////////////////////////////////////////////////////////
// activeChange()
// --------------
// window active state has changed

void polyesterClient::activeChange() {
    for (int n=0; n<ButtonTypeCount; n++)
        if (button[n])
            button[n]->reset();
    maskDirty = true;
    widget()->repaint(false);
}

//////////////////////////////////////////////////////////////////////////////
// captionChange()
// ---------------
// The title has changed

void polyesterClient::captionChange() {
    aDoubleBufferDirty = iDoubleBufferDirty = true;
    widget()->repaint(titlebar_->geometry(), false);
}

//////////////////////////////////////////////////////////////////////////////
// desktopChange()
// ---------------
// Called when desktop/sticky changes

void polyesterClient::desktopChange() {
    bool d = isOnAllDesktops();
    if (button[ButtonSticky]) {
        QToolTip::remove(button[ButtonSticky]);
        QToolTip::add(button[ButtonSticky], d ? i18n("Un-Sticky") : i18n("Sticky"));
        button[ButtonSticky]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// iconChange()
// ------------
// The title has changed

void polyesterClient::iconChange() {
    if (button[ButtonMenu]) {
        button[ButtonMenu]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// maximizeChange()
// ----------------
// Maximized state has changed

void polyesterClient::maximizeChange() {
    maskDirty = aDoubleBufferDirty = iDoubleBufferDirty = true;
    bool m = (maximizeMode() == MaximizeFull);
    if (button[ButtonMax]) {
        QToolTip::remove(button[ButtonMax]);
        QToolTip::add(button[ButtonMax], m ? i18n("Restore") : i18n("Maximize"));
        button[ButtonMax]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// shadeChange()
// -------------
// Called when window shading changes

void polyesterClient::shadeChange() {
    bool s = isSetShade();
    if (button[ButtonShade]) {
        QToolTip::remove(button[ButtonShade]);
        QToolTip::add(button[ButtonShade], s ? i18n("Unshade") : i18n("Shade"));
        button[ButtonShade]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// TODO: probably these two functions can be removed
// keepAboveChange()
// ------------
// The above state has changed

void polyesterClient::keepAboveChange(bool a) {
    if (button[ButtonAbove]) {
        button[ButtonAbove]->setOn(a);
        button[ButtonAbove]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// keepBelowChange()
// ------------
// The below state has changed

void polyesterClient::keepBelowChange(bool b) {
    if (button[ButtonBelow]) {
        button[ButtonBelow]->setOn(b);
        button[ButtonBelow]->repaint(false);
    }
}

//////////////////////////////////////////////////////////////////////////////
// borders()
// ----------
// Get the size of the borders

void polyesterClient::borders(int &left, int &right, int &top, int &bottom) const {

    if ((maximizeMode()==MaximizeFull) && !options()->moveResizeMaximizedWindows()) {
        left = right = bottom = 0;
        top = s_titleHeight;

        // update layout etc.
        topSpacer_->changeSize(1, -1, QSizePolicy::Expanding, QSizePolicy::Fixed);
        decoSpacer_->changeSize(1, 0, QSizePolicy::Expanding, QSizePolicy::Fixed);
        leftSpacer_->changeSize(left, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
        leftTitleSpacer_->changeSize(left, s_titleHeight, QSizePolicy::Fixed, QSizePolicy::Fixed);
        rightSpacer_->changeSize(right, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
        rightTitleSpacer_->changeSize(right, s_titleHeight, QSizePolicy::Fixed, QSizePolicy::Fixed);
        bottomSpacer_->changeSize(1, bottom, QSizePolicy::Expanding, QSizePolicy::Fixed);
    } else {
        /*if the borders are rounded add more left and right borders*/
        if( roundedCorners )
          left = right = bottom = frameSize*2;
        else
          left = right = bottom = frameSize+1;
        top = titleSize + (frameSize*2);

        // update layout etc.
        topSpacer_->changeSize(1, frameSize, QSizePolicy::Expanding, QSizePolicy::Fixed);
        decoSpacer_->changeSize(1, frameSize, QSizePolicy::Expanding, QSizePolicy::Fixed);
        leftSpacer_->changeSize(left, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
        leftTitleSpacer_->changeSize(left, s_titleHeight, QSizePolicy::Fixed, QSizePolicy::Fixed);
        rightSpacer_->changeSize(right, 1, QSizePolicy::Fixed, QSizePolicy::Expanding);
        rightTitleSpacer_->changeSize(right,s_titleHeight,QSizePolicy::Fixed, QSizePolicy::Fixed);
        bottomSpacer_->changeSize(1, bottom, QSizePolicy::Expanding, QSizePolicy::Fixed);
    }
    widget()->layout()->activate();
}

//////////////////////////////////////////////////////////////////////////////
// resize()
// --------
// Called to resize the window

void polyesterClient::resize(const QSize &size) {
    widget()->resize(size);
}

//////////////////////////////////////////////////////////////////////////////
// minimumSize()
// -------------
// Return the minimum allowable size for this window

QSize polyesterClient::minimumSize() const {
    return widget()->minimumSize();
}

//////////////////////////////////////////////////////////////////////////////
// mousePosition()
// ---------------
// Return logical mouse position

KDecoration::Position polyesterClient::mousePosition(const QPoint &point) const {
    const int corner = 24;
    Position pos;
    int fs = frameSize + handlebar;
    //int fs = ::factory->frameSize();

    if (point.y() <= fs) {
        // inside top frame
        if (point.x() <= corner)
            pos = PositionTopLeft;
        else if (point.x() >= (width()-corner))
            pos = PositionTopRight;
        else
            pos = PositionTop;
    } else if (point.y() >= (height()-fs*2)) {
        // inside handle
        if (point.x() <= corner)
            pos = PositionBottomLeft;
        else if (point.x() >= (width()-corner))
            pos = PositionBottomRight;
        else
            pos = PositionBottom;
    } else if (point.x() <= fs ) {
        // on left frame
        if (point.y() <= corner)
            pos = PositionTopLeft;
        else if (point.y() >= (height()-corner))
            pos = PositionBottomLeft;
        else
            pos = PositionLeft;
    } else if (point.x() >= width()-fs) {
        // on right frame
        if (point.y() <= corner)
            pos = PositionTopRight;
        else if (point.y() >= (height()-corner))
            pos = PositionBottomRight;
        else
            pos = PositionRight;
    } else {
        // inside the frame
        pos = PositionCenter;
    }
    return pos;
}

//////////////////////////////////////////////////////////////////////////////
// eventFilter()
// -------------
// Event filter

bool polyesterClient::eventFilter(QObject *obj, QEvent *e) {
    if (obj != widget())
        return false;

    switch (e->type()) {
    case QEvent::MouseButtonDblClick: {
            mouseDoubleClickEvent(static_cast<QMouseEvent *>(e));
            return true;
        }
    case QEvent::MouseButtonPress: {
            processMousePressEvent(static_cast<QMouseEvent *>(e));
            return true;
        }
    case QEvent::Wheel: {
           wheelEvent(static_cast< QWheelEvent* >(e));
           return true;
        }
    case QEvent::Paint: {
            paintEvent(static_cast<QPaintEvent *>(e));
            return true;
        }
    case QEvent::Resize: {
            resizeEvent(static_cast<QResizeEvent *>(e));
            return true;
        }
    case QEvent::Show: {
            showEvent(static_cast<QShowEvent *>(e));
            return true;
        }
    default: {
            return false;
        }
    }

    return false;
}

//////////////////////////////////////////////////////////////////////////////
// mouseDoubleClickEvent()
// -----------------------
// Doubleclick on title

void polyesterClient::mouseDoubleClickEvent(QMouseEvent *e) {
    if (titlebar_->geometry().contains(e->pos()))
        titlebarDblClickOperation();
}

//////////////////////////////////////////////////////////////////////////////
// wheelEvent()
// -----------------------
// Mousewheel over titlebar

void polyesterClient::wheelEvent(QWheelEvent * e) {
#if KDE_VERSION > KDE_MAKE_VERSION(3,4,3)
if (titleLayout_->geometry().contains(e->pos()))
titlebarMouseWheelOperation(e->delta());
#endif
}

//////////////////////////////////////////////////////////////////////////////
// paintEvent()
// ------------
// Repaint the window

void polyesterClient::paintEvent(QPaintEvent* e) {
    if (!::factory->initialized())
    {
        return;
    }

    //int frameSize = ::factory->frameSize();
    const uint maxCaptionLength = 200; // truncate captions longer than this!
                                       // FIXME: truncate related to window size
//titlebar_->geometry().width()/(widget()->font().pointSize());
    QString captionText(caption());
    if (captionText.length() > maxCaptionLength) {
        captionText.truncate(maxCaptionLength);
        captionText.append(" [...]");
    }


    QColor blackColor(black);
    QColor redColor(red);
    QColorGroup group,widgetGroup;

    bool active = isActive();

    //doublebuffering everything to avoid the flicker
    QPainter finalPainter(widget());

    //an ugly debug red border used to thest the speed...
    //finalPainter.fillRect(e->rect(),red);return;

    // mask off which corners we dont want
    // otherwise recycle the old buffer
    /*if( doubleBuff.width()>0 )
    {
       finalPainter.drawPixmap(0,0, doubleBuff);
       finalPainter.end();
       return;
    }*/
    if( maskDirty )
        updateMask();
    if( !(active?aDoubleBufferDirty:iDoubleBufferDirty) && ((active?activeBuff.width():inactiveBuff.width()) == widget()->width() ))
    {
       finalPainter.drawPixmap(0,0, active?activeBuff:inactiveBuff );
       finalPainter.end();
       return;
    }


    if( active )
       activeBuff  =  QPixmap(widget()->width(), widget()->height());
    else
       inactiveBuff  =  QPixmap(widget()->width(), widget()->height());

    QPainter painter(active?&activeBuff:&inactiveBuff);
    //get group information first
    group = options()->colorGroup(KDecoration::ColorTitleBlend, active);
    widgetGroup = widget()->colorGroup();

    QRect topRect( topSpacer_->geometry() );
    QRect titleRect( titleLayout_->geometry() );
    QRect textRect( titlebar_->geometry() );
    QRect Rltitle( leftTitleSpacer_->geometry() );
    QRect Rrtitle( rightTitleSpacer_->geometry() );
    QRect Rdeco( decoSpacer_->geometry() );
    QRect Rleft( leftSpacer_->geometry() );
    QRect Rright( rightSpacer_->geometry() );
    QRect Rbottom( bottomSpacer_->geometry() );
    QRect tempRect;

    
    /*
    if(active)
    {
        qDebug("topRect.y()   = %i\tbottom() = %i",topRect.top(),topRect.bottom());
        qDebug("titleRect.y() = %i\tbottom() = %i",titleRect.top(),titleRect.bottom());
        qDebug("textRect.y()  = %i\tbottom() = %i",textRect.top(),textRect.bottom());
        qDebug("Rltitle.y()   = %i\tbottom() = %i",Rltitle.top(),Rltitle.bottom());
        qDebug("Rrtitle.y()   = %i\tbottom() = %i",Rrtitle.top(),Rrtitle.bottom());
        qDebug("Rdeco.y()     = %i\tbottom() = %i",Rdeco.top(),Rdeco.bottom());
        qDebug("Rleft.y()     = %i\tbottom() = %i",Rleft.top(),Rleft.bottom());
        qDebug("Rright.y()    = %i\tbottom() = %i",Rright.top(),Rright.bottom());
        qDebug("Rbottom.y()   = %i\tbottom() = %i",Rbottom.top(),Rbottom.bottom());
    }
    */

    // top
    painter.drawTiledPixmap(topRect, active ? *aTitleBarTopTile:*iTitleBarTopTile);
    painter.drawTiledPixmap(titleRect.x(),
                            titleRect.y(),
                            titleRect.width(),
                            titleRect.height() + Rdeco.height(),
                            active ? *aTitleBarTile:*iTitleBarTile);

    textRect.setRect(textRect.x()+SIDETITLEMARGIN,
                     textRect.y(),
                     textRect.width()-SIDETITLEMARGIN*2,
                     textRect.height());


    //is the title text too long?
    bool titleTooLong = false;
    Qt::AlignmentFlags titleAlign = ::factory->titleAlign();
    const int gradientWidth = 60;
    if( QFontMetrics(widget()->font()).width(captionText) > (textRect.width() - gradientWidth) )
    {
        titleTooLong = true;
        titleAlign = Qt::AlignLeft;
    }

    //we are shadowing title bar text only if there is some text
    if(::factory->titleShadow() && textRect.width()>0 && textRect.height()>0)
    {
        QPixmap textPixmap;
        QPainter tempPainter;
        QColor shadowColor;

        textPixmap = QPixmap(textRect.width(), textRect.height());
        textPixmap.fill(QColor(0,0,0));
        textPixmap.setMask( textPixmap.createHeuristicMask(TRUE) );
        tempPainter.begin(&textPixmap);


        //shadow text
        tempPainter.setFont(options()->font(isActive(), false));

        tempPainter.setPen(white);

        tempPainter.drawText(1, 1, textRect.width(), textRect.height(),
                         titleAlign | AlignVCenter | Qt::SingleLine,
                         captionText);
        tempPainter.end();

        // make the cute blurred text shadow from Plastik engine
        QImage shadow;
        ShadowEngine se;

        //deciding if the shadow will be black or white
        if(qGray(options()->color(KDecoration::ColorFont, isActive()).rgb()) > 150)
            shadowColor = blackColor;
        else
            shadowColor = white;

        shadow = se.makeShadow(textPixmap, shadowColor);

        painter.drawImage(textRect, shadow);

    }

    // draw title text
    painter.setFont(options()->font(isActive(), false));
    painter.setPen(options()->color(KDecoration::ColorFont, isActive()));
    painter.drawText(textRect,
                     titleAlign | AlignVCenter | Qt::SingleLine,
                     captionText);

    // make title fade out when the caption is too long, veery slow but veery cool :-)
    if( titleTooLong )
    {
        int xGradient = textRect.right()-(gradientWidth-1);

        QPixmap backLayerP = QPixmap(gradientWidth, textRect.height());
        QPainter layerPainter(&backLayerP);
        layerPainter.drawTiledPixmap(backLayerP.rect(), active ? *aTitleBarTile:*iTitleBarTile);
        layerPainter.end();
        QImage backLayer = backLayerP.convertToImage();

        QImage textLayer = ( active ?
                             activeBuff.convertToImage():
                             inactiveBuff.convertToImage()
                            ).copy( xGradient, textRect.y(),
                                    gradientWidth, textRect.height()
                                  );
        KImageEffect::blend(backLayer,  textLayer, KImageEffect::HorizontalGradient);
        painter.drawImage(xGradient, textRect.y(), backLayer);

    }

    //left of buttons and title
    painter.drawTiledPixmap(Rltitle.x(),
                            Rltitle.y(),
                            Rltitle.width(),
                            Rltitle.height()+Rdeco.height(),
                            active ? *aTitleBarTile:*iTitleBarTile);

    //left 3d effect
    painter.drawTiledPixmap(Rltitle.x(),
                            Rltitle.y(),
                            2,
                            Rltitle.height()+Rdeco.height(),
                            active ? *aTitleBarTopTile:*iTitleBarTopTile);

    // left mid layout
    painter.fillRect(Rleft,widgetGroup.background());

    // right of buttons and title
    painter.drawTiledPixmap(Rrtitle.x(),
                            Rrtitle.y(),
                            Rrtitle.width(),
                            Rrtitle.height()+Rdeco.height(),
                            active ? *aTitleBarTile:*iTitleBarTile);
    //right 3d effect
    painter.drawTiledPixmap(width()-2,
                            Rltitle.y(),
                            2,
                            Rltitle.height()+Rdeco.height(),
                            active ? *aTitleBarTopTile:*iTitleBarTopTile);

    // right mid layout
    painter.fillRect(Rright,widgetGroup.background());
    
    // bottom
    /*
    if(isShade())
    {
        frame.setRect(0,::factory->titleSize()+FRAMESIZE, width(), FRAMESIZE);
    }
    else
    {
        frame.setRect(0, height() - (FRAMESIZE*2), width(),  (FRAMESIZE*2));
    }
    */
    painter.fillRect(Rbottom, KDecoration::options()->color(ColorTitleBlend, active));//widgetGroup.background());

    //if the window is maximized the border is invisible
    if( (maximizeMode()==MaximizeFull) && !options()->moveResizeMaximizedWindows() )
      painter.setPen(KDecoration::options()->color(ColorTitleBar, active));
    else
      painter.setPen(group.background().dark(150));

    // outline outside the frame but not the corners if they are rounded
    tempRect = widget()->rect();
    painter.drawRect(tempRect);

    if(roundedCorners && !maximizeMode()) {
        // local temp right and bottom
        int r(width());
        painter.setPen(group.background().dark(150));
        painter.drawPoint(4, 1);
        painter.drawPoint(3, 1);
        painter.drawPoint(2, 2);
        painter.drawPoint(1, 3);
        painter.drawPoint(1, 4);
        painter.drawPoint(r - 5, 1);
        painter.drawPoint(r - 4, 1);
        painter.drawPoint(r - 3, 2);
        painter.drawPoint(r - 2, 3);
        painter.drawPoint(r - 2, 4);

        painter.setPen(KDecoration::options()->color(ColorTitleBar, active).light(160));
        painter.drawPoint(4, 2);
        painter.drawPoint(3, 2);
        painter.drawPoint(2, 3);
        painter.drawPoint(2, 4);

        //painter.setPen(KDecoration::options()->color(ColorTitleBlend, active));
        painter.drawPoint(r - 5, 2);
        painter.drawPoint(r - 4, 2);
        painter.drawPoint(r - 3, 3);
        painter.drawPoint(r - 3, 4);
    } 

    // finally copy the buffer into the widget
    //finalPainter.begin(doubleBuff);
    finalPainter.drawPixmap(0,0, active?activeBuff:inactiveBuff);
    finalPainter.end();

    if( active )
        aDoubleBufferDirty = false;
    else
        iDoubleBufferDirty = false;
}

//////////////////////////////////////////////////////////////////////////////
// updateMask()
// ------------
// update the frame mask
void polyesterClient::updateMask() {

    if ( (!options()->moveResizeMaximizedWindows() && maximizeMode() & MaximizeFull ) ) 
    {
        setMask(QRegion(widget()->rect()));
        return;
    }

    int r(width());
    int b(height());
    QRegion mask;

    mask=QRegion(widget()->rect());
    mask=QRegion( 0, 0, r, b );

   // Remove top-left corner.
    if( roundedCorners &&
        maximizeMode() != KDecorationDefines::MaximizeFull )
    {
        mask -= QRegion(0, 0, 5, 1);
        mask -= QRegion(0, 1, 3, 1);
        mask -= QRegion(0, 2, 2, 1);
        mask -= QRegion(0, 3, 1, 2);
        mask -= QRegion(r - 5, 0, 5, 1);
        mask -= QRegion(r - 3, 1, 3, 1);
        mask -= QRegion(r - 2, 2, 2, 1);
        mask -= QRegion(r - 1, 3, 1, 2);
    }
    //always remove one corner pixel so it simulates a soft corner like plastik
    mask -= QRegion(0,0,1,1);
    mask -= QRegion(r-1,0,1,1);
    mask -= QRegion(0, b-1, 1,1);
    mask -= QRegion(r-1,b-1,1,1);

    setMask(mask, 1);

    maskDirty =  false;
}

//////////////////////////////////////////////////////////////////////////////
// resizeEvent()
// -------------
// Window is being resized

void polyesterClient::resizeEvent(QResizeEvent *) {
    maskDirty = aDoubleBufferDirty = iDoubleBufferDirty = true;
    if (widget()->isShown()) {
        QRegion region = widget()->rect();
        region = region.subtract(titlebar_->geometry());
        widget()->erase(region);
    }
    updateMask();
}

//////////////////////////////////////////////////////////////////////////////
// showEvent()
// -----------
// Window is being shown

void polyesterClient::showEvent(QShowEvent *) {
    widget()->repaint();
    updateMask();
}

//////////////////////////////////////////////////////////////////////////////
// maxButtonPressed()
// -----------------
// Max button was pressed

void polyesterClient::maxButtonPressed() {
    if (button[ButtonMax]) {
#if KDE_IS_VERSION(3, 3, 0)
        maximize(button[ButtonMax]->lastMousePress());
#else

        switch (button[ButtonMax]->lastMousePress()) {
        case MidButton:
            maximize(maximizeMode() ^ MaximizeVertical);
            break;
        case RightButton:
            maximize(maximizeMode() ^ MaximizeHorizontal);
            break;
        default:
            (maximizeMode() == MaximizeFull) ? maximize(MaximizeRestore)
            : maximize(MaximizeFull);
        }
#endif

    }
}

//////////////////////////////////////////////////////////////////////////////
// shadeButtonPressed()
// -----------------
// Shade button was pressed

void polyesterClient::shadeButtonPressed() {
    if (button[ButtonShade]) {
        setShade( !isSetShade());
    }
}

//////////////////////////////////////////////////////////////////////////////
// aboveButtonPressed()
// -----------------
// Above button was pressed

void polyesterClient::aboveButtonPressed() {
    if (button[ButtonAbove]) {
        setKeepAbove( !keepAbove());
    }
}

//////////////////////////////////////////////////////////////////////////////
// belowButtonPressed()
// -----------------
// Below button was pressed

void polyesterClient::belowButtonPressed() {
    if (button[ButtonBelow]) {
        setKeepBelow( !keepBelow());
    }
}

//////////////////////////////////////////////////////////////////////////////
// menuButtonPressed()
// -------------------
// Menu button was pressed (popup the menu)

void polyesterClient::menuButtonPressed() {
    static QTime* t = NULL;
    static polyesterClient* lastClient = NULL;
    if (t == NULL)
        t = new QTime;
    bool dbl = (lastClient==this && t->elapsed() <= QApplication::doubleClickInterval());
    lastClient = this;
    t->start();
    //if (button[ButtonMenu] && !dbl && !::factory->menuClosed()) {
    if ( !dbl || !::factory->menuClosed()) {
        QPoint p(button[ButtonMenu]->rect().bottomLeft().x(),
                 button[ButtonMenu]->rect().bottomLeft().y());
        KDecorationFactory* f = factory();
        showWindowMenu(button[ButtonMenu]->mapToGlobal(p));
        if (!f->exists(this))
            return; // decoration was destroyed
        button[ButtonMenu]->setDown(false);
    }
    else
    {
        closing = true;
    }
}

void polyesterClient::menuButtonReleased()
{
    if(closing)
    {
        closeWindow();
    }
}

#include "polyester.moc"