File: theme.jl

package info (click to toggle)
sawfish-themes 0.11
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k, sarge
  • size: 5,876 kB
  • ctags: 4
  • sloc: lisp: 13,333; sh: 785; makefile: 68; xml: 8; perl: 2
file content (3391 lines) | stat: -rw-r--r-- 89,207 bytes parent folder | download | duplicates (8)
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
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
; mxflat/mxflat.jl
;;; START

#| theme.jl for mxflat (v0.7.3) sawfish theme

   Copyright (C) 2002 mx & ta

   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; if not, write to the Free Software
   Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

   authors: mx (email) & ta (dev.null@gmx.net)
|#

(require 'gradient)
; need hash tables for icon cache
(require 'rep.data.tables)
(require 'rep.io.timers)
(require 'sawfish.wm.util.recolor-image)

; required when gaoling is turned off (for debugging)
;(require 'rep.mmsystem)
;(require 'sawfish.wm.custom)
;(require 'sawfish.wm.colors)
;(require 'sawfish.wm.images)
;(require 'sawfish.wm.fonts)

;   ---------
;;; REDRAWING
;   ---------

;
;; redrawing:internal vars
;

; we need to define theme here
; so we can reach them from our defcustom clauses

; cache for window icons
(defvar icon-cache (make-weak-table eq-hash eq))

; holds the shadow side for the current selected option
(defvar shadow-side ())

; for now empty, see below
(define (recolor-all))

;
;; redrawing:functions
;

(define (get-symbol #!rest strings)
	(intern (mapconcat identity strings ""))
)
(define (check-symbol #!rest strings)
	(let
		(
			(name (intern (mapconcat identity strings "")))
		)
		(if (boundp name)
			(symbol-value name)
			nil
		)
	)
)

(define (refresh-one-frame w)
	(if (eq (window-get w 'current-frame-style) 'mxflat)
		(refresh-window w)
	)
)

(define (refresh-all-frames)
	(map-windows refresh-window)
)

(define (rebuild-all-frames)
		(rebuild-frames-with-style 'mxflat)
)

; same as rebuild but this also deletes the icon cache
(define (recreate-all-frames)
	(reframe-windows-with-style 'mxflat)
)

; after the light source direction is updated we have to destroy the cache
; with the correct shadow sides, it will be recreated on next access
(define (after-set-shadow-light-source)
	(setq shadow-side ())
	(recreate-all-frames)
)

(define update-interval nil)
(define update-timer 
	(make-timer (lambda ()
		(refresh-all-frames)
		(if update-interval
			(set-timer update-timer update-interval)
		)
	))
)

(define (after-set-title-periodic-update)
	(if (check-symbol "mxflat:title-periodic-update")
		(set-timer update-timer update-interval)
		(delete-timer update-timer)
	)
)

(define (after-set-title-update-interval)
	(setq update-interval (check-symbol "mxflat:title-update-interval"))
	(after-set-title-periodic-update)
)

(define (recreate-all-frames-clear-cache)
	; reset icon cache
	(setq icon-cache (make-weak-table eq-hash eq))
	(recreate-all-frames)
)

(define (recreate-all-frames-recolor-all)
	(recolor-all)
	(recreate-all-frames)
)

;   -------------
;;; CONFIGURATION
;   -------------

;
;; configuration:keymaps
;

; we need to define our default keymaps here,
; so we can reach them from our defcustom clauses

(defvar sticky-button-keymap
	(bind-keys (make-keymap)
		"Button1-Off" 'toggle-window-sticky
	)
)
(defvar previous-button-keymap
	(bind-keys (make-keymap)
		"Button6-Click" 'pack-window-left
		"Button3-Off" '(command-sequence `((send-to-workspace ,(1+ (car (workspace-limits))))
											(activate-workspace ,(1+ (car (workspace-limits))))))
		"Button2-Click" 'popup-workspace-list
		"Button1-Off" 'send-to-previous-workspace
	)
)
(defvar next-button-keymap
	(bind-keys (make-keymap)
		"Button6-Click" 'pack-window-right
		"Button3-Off" '(command-sequence `((send-to-workspace ,(1+ (cdr (workspace-limits))))
											(activate-workspace ,(1+ (cdr (workspace-limits))))))
		"Button2-Click" 'popup-workspace-list
		"Button1-Off" 'send-to-next-workspace
	)
)

(defvar configure-button-keymap
	(bind-keys (make-keymap)
		"Button1-Off" 'customize
		"Button3-Off" '(call-command
			(lambda ()
				(if mxflat:custom-title
					(setq mxflat:custom-title nil)
					(setq mxflat:custom-title t)
				)
				;(reframe-windows-with-style 'mxflat)
				(map-windows refresh-window)
			))
	)
)
(defvar always-on-top-button-keymap
	(bind-keys (make-keymap)
		"Button1-Off" '(call-command
			(lambda ()
				(if (> (window-get (current-event-window) 'depth) 0)
					(window-put (current-event-window) 'depth 0)
					(window-put (current-event-window) 'depth mxflat:always-on-top-depth)
				)
				(reframe-window (current-event-window))
			))
	)
)
(defvar lock-button-keymap
	(bind-keys (make-keymap)
		"Button1-Off" '(call-command
			(lambda ()
				(if (window-get (current-event-window) 'fixed-position)
					(window-put (current-event-window) 'fixed-position nil)
					(window-put (current-event-window) 'fixed-position t)
				)
				(reframe-window (current-event-window))
			))
	)
)


;
;; configuration:groups
;

; there are just too many options
; so we put them into several groups/tabs
(defgroup mxflat:group
	"mxflat"
	:group appearance
)
(defgroup mxflat:title-group
	"title"
	:group (appearance mxflat:group)
)
(defgroup mxflat:title-basic-group
	"basic"
	:group (appearance mxflat:group mxflat:title-group)
)
(defgroup mxflat:title-gradient-group
	"gradient"
	:group (appearance mxflat:group mxflat:title-group)
)
(defgroup mxflat:title-colors-group
	"colors"
	:group (appearance mxflat:group mxflat:title-group)
)
(defgroup mxflat:buttons-group
	"buttons"
	:group (appearance mxflat:group)
)
(defgroup mxflat:buttons-basic-group
	"basic"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:buttons-positions1-group
	"positions 1"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:buttons-positions2-group
	"positions 2"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:buttons-keymaps1-group
	"keymaps 1"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:buttons-keymaps2-group
	"keymaps 2"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:button-colors-group
	"colors"
	:group (appearance mxflat:group mxflat:buttons-group)
)
(defgroup mxflat:border-group
	"borders"
	:group (appearance mxflat:group)
)
(defgroup mxflat:border1-group
	"basic"
	:group (appearance mxflat:group mxflat:border-group)
)
(defgroup mxflat:border-extended-group
	"extended"
	:group (appearance mxflat:group mxflat:border-group)
)
(defgroup mxflat:border-colors-group
	"colors"
	:group (appearance mxflat:group mxflat:border-group)
)

;
;; configuration:title-basic
;

(defcustom mxflat:decoration-mode
	'normal
	"decoration mode"
	:tooltip "set the mode 'when to draw a titlebar and window buttons'. the decoration mode 'all' means that all windows, even transients/dialogs, will get a titlebar and buttons. none means no window will get any :-). usually transients only have a contents-border. this option is for reimplementing a sawfish feature that is gone in the latest version."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type symbol
	:options (normal all none)
	:after-set recreate-all-frames
)

(defcustom mxflat:title-height
	16
	"titlebar height (9 - 1000 pixel)"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type (number 9 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:custom-title
	nil
	"customize title string"
	:tooltip "see the tooltip for the 'title string' option below for more information. (NOTE: i hope the redrawing problems with this option are solved. if not, write an email please!)"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type boolean
	:after-set refresh-all-frames
)

(defcustom mxflat:title-string
	"$fullname |$widthx$height-($x-pos,$y-pos)@$current-ws/$last-ws-$(DISPLAY)"
	"title string"
	:tooltip "the string you insert here will be your new title string. variables always start with a dollar-sign $ and are replaced with their actual values. to get the value of an environment variable use $(variable), eg $(DISPLAY). to get the value of an rep/sawfish function use $[fuction], eg $[current-time-string]. the following vars are built-in: $name, $fullname, $width, $width-content, $height, $height-content, $depth, $x-pos, $y-pos, $current-ws and $last-ws. (NOTE: i hope the redrawing problems with this option are solved. if not, write an email please!"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type string
	:depends mxflat:custom-title
	:after-set refresh-all-frames
)

(defcustom mxflat:title-periodic-update
	nil
	"update title string periodically"
	:tooltip "this is useful if your title string changes without user interaction (eg because it displays the current time or something like that)."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type boolean
	:depends mxflat:custom-title
	:after-set after-set-title-periodic-update
)

(defcustom mxflat:title-update-interval
	1000
	"title string update interval (1 - 1000000 s)"
	:tooltip "this is useful if your title string changes without user interaction (eg because it displays the current time or something like that)."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type (number 1 1000000)
	:depends mxflat:custom-title
	:after-set after-set-title-update-interval
)

(defcustom mxflat:title-font
	default-font
	"title string font"
	:tooltip "you should really choose a better font. verdana bold 8 pt looks nice."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type font
	:after-set rebuild-all-frames
)

(defcustom mxflat:customize-title-position
	nil
	"customize title position"
	:tooltip "by default the title string is centered horizontally and vertically."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type boolean
	:after-set rebuild-all-frames
)

(defcustom mxflat:title-x-justify
	'center
	"title string horizontal alignment"
	:tooltip "'real-center' means to take the whole titlebar into account while centering the string. when selecting 'center' only the distance between the buttons is taken into account."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type symbol
	:depends mxflat:customize-title-position
	:options (left center real-center right)
	:after-set rebuild-all-frames
)

(defcustom mxflat:title-x-adjustment
	0
	"title string horizontal align adjustment (-10000 - 10000 pixel)"
	:tooltip "the horizontal position of the title string can only be adjusted if the horizontal alignment is not 'center'. the range also depends on the setting of the option above. it the title string is already aligned left, you cannot adjust it to be even further left because there are button in the way."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type (number -10000 10000)
	:depends mxflat:customize-title-position
	:after-set rebuild-all-frames
)

(defcustom mxflat:title-y-justify
	'center
	"title string vertical alignment"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type symbol
	:depends mxflat:customize-title-position
	:options (top center bottom)
	:after-set rebuild-all-frames
)

(defcustom mxflat:title-y-adjustment
	0
	"title string vertical align adjustment (-10000 - 10000 pixel)"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-basic-group)
	:type (number -10000 10000)
	:depends mxflat:customize-title-position
	:after-set rebuild-all-frames
)

;
;; configuration:title-gradient
;

(defcustom mxflat:title-gradient
	nil
	"gradient titlebar background (experimental)"
	:tooltip "when activating the gradient background the normal titlebar background colors will be ignored. besides the button background colors will be ignored. (NOTE: this can be very cpu intensive if you set the quality to a low value, ie high quality.)"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:gradient-direction
	'horizontal
	"gradient mode"
	:tooltip "with gradient mode vertical or diagonal only the first or last (reversed) 2 colors in the list are used."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:options (none horizontal vertical diagonal)
	:depends mxflat:title-gradient
	:type symbol
	:after-set recreate-all-frames
)

(defcustom mxflat:gradient-scale
	3
	"gradient quality (1 best - 20 worst)"
	:tooltip "the value here is the factor the calculated gradient image will be scaled down. this increases speed but decreases quality."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:depends mxflat:title-gradient
	:type (number 1 20)
	:after-set recreate-all-frames
)

(defcustom mxflat:titlebar-gradient-color-mode
	'rainbow
	"gradient color mode"
	:tooltip "the custom mode will respect the colors set in the list below."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:options (rainbow jamaica rgb custom)
	:depends mxflat:title-gradient
	:type symbol
	:after-set recreate-all-frames
)

(defcustom mxflat:titlebar-gradient-bgcolors
	nil
	nil
	:tooltip "with gradient mode vertical or diagonal only the first or last (reversed) 2 colors in the list are used. besides the gradient color mode must be set to custom to activate this list."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:title-gradient
	:type* `(list color "titlebar background colors")
	:after-set recreate-all-frames
)

(defcustom mxflat:titlebar-gradient-bgcolors-reverse
	nil
	"reverse colors"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:depends mxflat:title-gradient
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:color-hue
	0
	"unfocused/highlighted titlebar colors hue (0 - 360)"
	:tooltip "this value changes the hue of a color, by moving (0 - 360) degress around the color cycle...180 for example inverts the color"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:depends mxflat:title-gradient
	:type (number 0 360)
	:after-set recreate-all-frames
)

(defcustom mxflat:color-saturation
	50
	"unfocused/highlighted titlebar colors saturation (0 - 100% of the focused color)"
	:tooltip "the saturation of a color is a measurement of the color intensity"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:depends mxflat:title-gradient
	:type (number 0 100)
	:after-set recreate-all-frames
)

(defcustom mxflat:color-brightness
	90
	"unfocused/highlighted titlebar colors brightness (0 - 100% of the focused color)"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-gradient-group)
	:depends mxflat:title-gradient
	:type (number 0 100)
	:after-set recreate-all-frames
)

;
;; configuration:title-colors
;

(defcustom mxflat:titlebar-bgcolor-focused
	(get-color "#fffbde")
	"  focused titlebar color"
	:tooltip "this is one of the 4 base colors. you can set these 4 colors and keep the extended colors deactivated. all other color options of the theme will then depend on these 4 colors."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:titlebar-bgcolor-unfocused
	(get-color "#cedfef")
	"unfocused titlebar color"
	:tooltip "this is one of the 4 base colors. you can set these 4 colors and keep the extended colors deactivated. all other color options of the theme will then depend on these 4 colors."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:text-color-focused
	(get-color "#000000")
	"  focused title string color"
	:tooltip "this is one of the 4 base colors. you can set these 4 colors and keep the extended colors deactivated. all other color options of the theme will then depend on these 4 colors."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:text-color-unfocused
	(get-color "#838383")
	"unfocused title string color"
	:tooltip "this is one of the 4 base colors. you can set these 4 colors and keep the extended colors deactivated. all other color options of the theme will then depend on these 4 colors."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:customize-extended-text-colors
	nil
	"customize extended title string colors"
	:tooltip "you can customize additional colors of the title string. if you deactivate this, these colors will be set to the focused title string color."
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:text-color-highlighted
	(get-color "#000000")
	"  focused highlighted title string color"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:depends mxflat:customize-extended-text-colors
	:after-set recreate-all-frames
)

(defcustom mxflat:text-color-inactive-highlighted
	(get-color "#000000")
	"unfocused highlighted title string color"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:depends mxflat:customize-extended-text-colors
	:after-set recreate-all-frames
)

(defcustom mxflat:text-color-clicked
	(get-color "#000000")
	"  focused clicked title string color"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:depends mxflat:customize-extended-text-colors
	:after-set recreate-all-frames
)

(defcustom mxflat:text-color-inactive-clicked
	(get-color "#000000")
	"unfocused clicked title string color"
	:group (appearance mxflat:group mxflat:title-group mxflat:title-colors-group)
	:type color
	:depends mxflat:customize-extended-text-colors
	:after-set recreate-all-frames
)


;; configuration:buttons-basic
;

(defcustom mxflat:button-icon-set
	'default-9
	"button icon set (experimental)"
	:tooltip "the number at the end is the width/height of the button icons in the icon set. (NOTE: YOU NEED TO RESTART SAWFISH AFTER CHANGING THE BUTTON ICON SET!)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:options (default-9 default-12 square-6 square-9 square-12 square-15 square-18 ball-9 ball-12)
	:type symbol
	:after-set recreate-all-frames-clear-cache
)

(defcustom mxflat:customize-button-size
	nil
	"customize button size"
	:tooltip "by default the button size follows the size of the titlebar, but by activating this you can set it to something different."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:type boolean
	:after-set rebuild-all-frames
)

(defcustom mxflat:button-size
	14
	"buttons size (1 - 1000 pixel)"
	:tooltip "a buttons size that is bigger than your current titlebar height is ignored."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:depends mxflat:customize-button-size
	:type (number 1 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:button-y-alignment
	'center
	"button vertical alignment"
	:tooltip "if a button is smaller than the titlebar you might want to set its vertical alignment."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:depends mxflat:customize-button-size
	:options (top center bottom)
	:type symbol
	:after-set rebuild-all-frames
)

(defcustom mxflat:scale-buttons
	nil
	"scale button icons (experimental)"
	:tooltip "in case you don't like the default size of 9 pixel you might activate button-scaling. YOU NEED TO RESTART SAWFISH AFTER CHANGING THE BUTTON SCALE!"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:type boolean
	:after-set recreate-all-frames-clear-cache
)

(defcustom mxflat:button-scale
	9
	"button icon scale; changes here need a restart of sawfish (0 - 1000 pixel)"
	:tooltip "in case you don't like the default size of 9 pixel you might activate button-scaling. (NOTE: YOU NEED TO RESTART SAWFISH AFTER CHANGING THE BUTTON SCALE!)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:depends mxflat:scale-buttons
	:type (number 0 1000)
	:after-set recreate-all-frames-clear-cache
)

(defcustom mxflat:menu-button-icon
	nil
	"display window icon on menu button"
	:tooltip "many people like having the window-icon displayed on the menu-button. this icon is defined by the application itself and is also shown in your pager and task-list."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:menu-button-scale
	12
	"menu icon scale (0 - 1000 pixel)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-basic-group)
	:depends mxflat:menu-button-icon
	:type (number 0 1000)
	:after-set recreate-all-frames-clear-cache
)

;
;; configuration:buttons-position1
;

(defcustom mxflat:button-distance
	3
	"button distance (0 - 1000 pixel)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions1-group)
	:type (number 0  1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:button-left-offset
	1
	"buttons left offset (0 - 1000 pixel)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions1-group)
	:type (number 0  1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:button-right-offset
	1
	"buttons right offset (0 - 1000 pixel)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions1-group)
	:type (number 0  1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:customize-buttons
	nil
	"customize button position (-1 -2 -3 .. title string .. +3 +2 +1)"
	:tooltip "enabling this will allow you to set the positon of each button in the titlebar manually. a negative/positiv value means the button will be drawn on left/right side of the titlebar."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions1-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:button-position-model
	'title-height
	"button position model"
	:tooltip "'titel-height' means calculate position as multiple of title height. when selecting 'pixel' you can adjust the button position pixelwise, but keep in mind that several buttons might slide on top of each other. 'none' means no buttons at all."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions1-group)
	:options (title-height pixel none)
	:depends mxflat:customize-buttons
	:type symbol
	:after-set rebuild-all-frames
)

;
;; configuration:buttons-position2
;

(defcustom mxflat:menu-button-position
	-1
	"menu button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:shade-button-position
	-2
	"shade button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:iconify-button-position
	+3
	"iconify button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:maximize-button-position
	+2
	"maximize button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:close-button-position
	+1
	"close button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:sticky-button-position
	-3
	"sticky button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:previous-button-position
	-5
	"previous workspace button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:next-button-position
	-6
	"next workspace button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:configure-button-position
	0
	"configure button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:info-button-position
	0
	"info button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:always-on-top-button-position
	0
	"always-on-top button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:lock-button-position
	0
	"lock button position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-positions2-group)
	:depends mxflat:customize-buttons
	:type (number -10000 10000)
	:after-set rebuild-all-frames
)

;
;; configuration:buttons-keymaps1
;

(defcustom mxflat:customize-sticky-keymap
	nil
	"customize sticky button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: toggle sticky state of window"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:sticky-button-keymap
	nil
	nil
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:customize-sticky-keymap
    :type keymap
)

(defcustom mxflat:customize-previous-keymap
	nil
	"customize previous button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: send window to previous workspace - button2 workspace-list - button3 send window to first workspace - button6 pack window left"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:previous-button-keymap
	nil
	nil
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:customize-previous-keymap
    :type keymap
)

(defcustom mxflat:customize-next-keymap
	nil
	"customize next button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: send window to next workspace - button2 workspace-list - button3 send window to last workspace - button6 pack window right"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:next-button-keymap
	nil
	nil
	:tooltip "button1: send window to next workspace - button2 workspace-list - button3 send window to last workspace - button6 pack window right"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps1-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:customize-next-keymap
    :type keymap
)

;
;; configuration:buttons-keymaps2
;

(defcustom mxflat:customize-configure-keymap
	nil
	"customize configure button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: start sawfish-ui - button3: toggle custom title string"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:configure-button-keymap
	nil
	nil
	:tooltip "button1: start sawfish-ui - button3: toggle custom title string"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:customize-configure-keymap
    :type keymap
)

(defcustom mxflat:customize-info-keymap
	nil
	"customize info button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: window list - button3 window snooper (not included by default)"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:info-button-keymap
	nil
	nil
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:widget-flags (expand-horizontally)
	:depends mxflat:customize-info-keymap
    :type keymap
)

(defcustom mxflat:always-on-top-depth
	1
	"always on top depth (-1000 - 1000)"
	:tooltip "a window will have this depth after you have pressed the always-on-top button. 0 is the normal window level. every positive value means above other windows, a negative value means below. keept it at a positive value in order to have the always-on-top button do it's job."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:type (number -1000 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:customize-always-on-top-keymap
	nil
	"customize always-on-top button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: sets window depth to the adjusted value"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:always-on-top-button-keymap
	nil
	nil
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:depends mxflat:customize-always-on-top-keymap
	:widget-flags (expand-horizontally)
    :type keymap
)

(defcustom mxflat:customize-lock-keymap
	nil
	"customize look button keymap"
	:tooltip "by default a button will work as it's name suggests. button1: lock window position"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:lock-button-keymap
	nil
	nil
	:group (appearance mxflat:group mxflat:buttons-group mxflat:buttons-keymaps2-group)
	:depends mxflat:customize-lock-keymap
	:widget-flags (expand-horizontally)
    :type keymap
)

;
;; configuration:button-colors
;

(defcustom mxflat:custom-button-colors
	nil
	"customize button-colors"
	:tooltip "when you active this you will be able to set custom button colors. by default the button colors depend on the titlebar colors."
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:type boolean
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:button-bgcolor-focused
	(get-color "#fffbde")
	"  focused button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-bgcolor-unfocused
	(get-color "#cedfef")
	"unfocused button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-bgcolor-highlighted
	(get-color "#def3ff")
	"  focused highlighted button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-bgcolor-inactive-highlighted
	(get-color "#fffbde")
	"unfocused highlighted button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-bgcolor-clicked
	(get-color "#cecece")
	"  focused clicked button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-bgcolor-inactive-clicked
	(get-color "#cecece")
	"unfocused clicked button background color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames
)

(defcustom mxflat:button-fgcolor-focused
	(get-color "#000000")
	"  focused button foreground color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:button-fgcolor-inactive
	(get-color "#838383")
	"unfocused button foreground color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:button-fgcolor-highlighted
	(get-color "#000000")
	"  focused highlighted button foreground color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames-recolor-all
)

(defcustom mxflat:button-fgcolor-clicked
	(get-color "#000000")
	"  focused clicked button foreground color"
	:group (appearance mxflat:group mxflat:buttons-group mxflat:button-colors-group)
	:depends mxflat:custom-button-colors
	:type color
	:after-set recreate-all-frames-recolor-all
)

;
;; configuration:border-basic
;

(defcustom mxflat:border-width-unmaximized
	1
	"border width (0 - 1000 pixel)"
	:tooltip "set the width border around the window."
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type (number 0 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-width-maximized
	1
	"border width for maximized windows (0 - 1000 pixel)"
	:tooltip "when you like having a big border around your windows you might have noticed that sawfish still draws that big border when the window is maximized. if you don't want that, set the border width for maximized windows smaller than the border width for unmaximized windows. (NOTE: this feature is still buggy as sawfish does not resize the windows correctly yet, so the best might be to set it to the normal border width.)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type (number 0 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:custom-border-focused
	nil
	"customize border for focused windows (experimental)"
	:tooltip "(NOTE: this works but it is VERY cpu intensive and might slow down your computer noticably. so turn it on at your own risk.)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type boolean
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-width-focused
	1
	"border width for focused windows (0 - 1000 pixel)"
	:tooltip "(NOTE: this works but it is VERY cpu intensive and might slow down your computer noticably. so turn it on at your own risk.)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type (number 0 1000)
	:depends mxflat:custom-border-focused
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-width-titlebar-contents
	1
	"title-contents border width (0 - 1000 pixel)"
	:tooltip "sets the widths of the line between titlebar and contents window"
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type (number 0 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-offset
	-1
	"border offset (-1000 - 1000 pixel)"
	:tooltip "adjust the point where the border actually starts. many gtk2 apps draw a small black border around their contents window and this is just ugly when sawfish draws another border around the window. with a border offset of -1 the sawfish window border will overlay the gtk2 border exactly."
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type (number -1000 1000)
	:after-set rebuild-all-frames
)

(defcustom mxflat:keep-borders-when-shaded
	nil
	"keep window borders and corners at their place when shading a window"
	:tooltip "when shading a window borders and corners will stay where they were and not merge with the titlebar."
	:group (appearance mxflat:group mxflat:border-group mxflat:border1-group)
	:type boolean
	:after-set recreate-all-frames
)

;
;; configuration:border-extended
;

(defcustom mxflat:show-corners
	t
	"show window corners"
	:tooltip " the corners are used to resize a window in two directions at the same time. usually the width and length of window corners are determined by the border width but you can adjust them below."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:corner-length-adjustment
	20
	"corners length adjustement (-1 - 100 pixel)"
	:tooltip "increasing the corner length will grow the window corners along the window border (i.e. make them longer). with a border width of 1 pixel it is hard to grab the 1 pixel corner to be able to resize the window in two directions; thus, make it bigger."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type (number -1 100)
	:depends mxflat:show-corners
	:after-set rebuild-all-frames
)

(defcustom mxflat:corner-direction
	'outside
	"corner direction"
	:"inside or outside the window border"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type symbol
	:options (outside inside)
	:depends mxflat:show-corners
	:after-set recreate-all-frames
)

(defcustom mxflat:corner-width-adjustment
	1
	"corners width adjustement (0 - 100 pixel)"
	:tooltip "this option makes the window corners broader. with a border width of 1 pixel it is hard to grab this 1 pixel corner to be able to resize the window in two directions; thus, make it bigger."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type (number 0 100)
	:depends mxflat:show-corners
	:after-set rebuild-all-frames
)

(defcustom mxflat:corner-width-adjustment-maximized
	0
	"corners width adjustement for maximized windows (0 - 100 pixel)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type (number 0 100)
	:depends mxflat:show-corners
	:after-set rebuild-all-frames
)

(defcustom mxflat:fake-shadow
	nil
	"fake shadow (experimental)"
	:tooltip "draw a small additional border around your windows emulating a solid shadow effect. use the options below to adjust its width and direction. (NOTE: this is VERY cpu intensive and might slow down your computer noticably. so turn it on at your own risk.)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type boolean
	:after-set recreate-all-frames
)

(defcustom mxflat:fake-shadow-focused-only
	nil
	"fake shadow only on focused windows"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type boolean
	:depends mxflat:fake-shadow
	:after-set recreate-all-frames
)

(defcustom mxflat:shadow-light-source
	'north-west
	"fake shadow light source"
	:tooltip "set the direction of the fake shadow. usually only 2 or 3 borders of your window have a fake shadow. this depends on where the light comes from."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type symbol
	:options (north east south west north-east north-west south-east south-west)
	:depends mxflat:fake-shadow
	:after-set after-set-shadow-light-source
)

(defcustom mxflat:shadow-light-distance
	3
	"fake shadow light distance (0 - 100)"
	:tooltip "this option has only an effect on the north, east, south and west light sources. the further away the light source is, the smaller will the two 'side' shadows be."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type (number 1 100)
	:depends mxflat:fake-shadow
	:after-set rebuild-all-frames
)

(defcustom mxflat:shadow-width
	4
	"fake shadow width (0 - 100 pixel)"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type (number 0 100)
	:depends mxflat:fake-shadow
	:after-set rebuild-all-frames
)

(defcustom mxflat:shadow-color
	(get-color "#838383")
	"shadow color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-extended-group)
	:type color
	:depends mxflat:fake-shadow
	:after-set rebuild-all-frames
)

;
;; configuration:border-colors
;

(defcustom mxflat:customize-border-colors
	nil
	"customize border colors"
	:tooltip "you can customize your border colors here. if you deactivate this, these colors will be set to the focused title string color."
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type boolean
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-focused
	(get-color "#000000")
	"  focused border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-unfocused
	(get-color "#000000")
	"unfocused border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-highlighted
	(get-color "#000000")
	"  focused highlighted border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-inactive-highlighted
	(get-color "#000000")
	"unfocused highlighted border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-clicked
	(get-color "#000000")
	"  focused clicked border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

(defcustom mxflat:border-color-inactive-clicked
	(get-color "#000000")
	"unfocused clicked border color"
	:group (appearance mxflat:group mxflat:border-group mxflat:border-colors-group)
	:type color
	:depends mxflat:customize-border-colors
	:after-set rebuild-all-frames
)

;   ----
;;; MISC
;   ----

(define (get-last-workspace)
	(aref
		(nth 2 (get-x-property 'root '_NET_NUMBER_OF_DESKTOPS))
		0
	)
)

(define (get-first-workspace) 1)

(defmacro window-in-workspace-p (w space)
	`(memq ,space (window-get ,w 'workspaces))
)

(define (window-maximized-p w)
	(window-get w 'unmaximized-geometry)
)

(define (window-maximized-horizontally-p w)
	(window-get w 'maximized-horizontally)
)

(define (window-maximized-vertically-p w)
	(window-get w 'maximized-vertically)
)

; constant accuracy factor since
; we ???are not allowed to  use floats in themes???
; / seems to be gaoled
(defvar afactor 10000000000000)
(defvar afactor360 (* 360 afactor))

;
;; misc:color manipulation
;

; returns a list containing hue saturation and value of color
(define (rgb2hsv color)
	(let*
		(
			; get rgb ints for the color
			(rgbchannels (color-rgb color))

			; get red, green and blue ints from rgbchannels
			(red   (nth 0 rgbchannels))
			(green (nth 1 rgbchannels))
			(blue  (nth 2 rgbchannels))

			; channnel with the biggest int value
			(value (apply max (list red green blue)))
			; delta
			(max-min (- value (apply min (list red green blue))))

			; saturation: 1 - min / max  =  (max - min) / max  =  delta / max
			; saturation * afactor - acutally the saturation
			; is a float between 0 and 1 but as it seems
			; we cannot use them in themes
			(saturation
				(if (= max-min 0)
					0
					(/
						(* max-min afactor)
						value
					)
				)
			)
			; value between 0 and 360 degree * afactor
			; 0 = red, 120 = green, 240 = blue
			(hue
				(if (= max-min 0)
					0
					(/
						(*
							60
							afactor
							(cond
								(
									(= value red)
									(- green blue)
								)
								(
									(= value green)
									(+ (* 2 max-min) (- blue red))
								)
								(
									; (= value blue)
									t
									(+ (* 4 max-min) (- red green))
								)
							)
						)
						max-min
					)
				)
			)
		)
		(list (if (< hue 0) (+ hue afactor360) hue) saturation value)
	)
)

; translates a rgb-color from the ints of the list, which contains hue, saturation and value
(define (hsv2rgb hsv)
	(let*
		(
			(hue (nth 0 hsv))
			(saturation (nth 1 hsv))
			(value (nth 2 hsv))
			
		)

		; value zero -> color black
		(cond
			((= value 0)
				(get-color-rgb 0 0 0)
			)
			((= saturation 0)
		
				; in this case the color is grey and
				; thus its rgb value is:
				(get-color-rgb value value value)
			)
			(t
				; else we need to do some calculations -
				; the hue and saturation are aleady
				; multiplied with afactor
				(let*
					(
						; we need it often, so we define
						; it once here
						(afactor2 (* afactor afactor))
						; kind of an 'compensation delta'
						(f
							(if
								(or
									(= hue 0)
									(= hue afactor360)
								)
								0
								(/ (mod hue (* 60 afactor)) 60)
							)
						)

							; p is the lowest color int
							(p
							(/
								(*  value
									(- afactor saturation)
								)
								afactor
							)
						)
						; tt is a medium color int
						(q
							(/
								(*
									value
									(-
										afactor2
										(* saturation f)
									)
								)
								afactor2
							)
						)
						; tt is a medium color int
						(tt
							(/
								(*
									value
									(-
										afactor2
										(*
											saturation
											(- afactor f)
										)
									)
								)
								afactor2
							)
						)

						; area on the color circle (0 .. 5)
						(circle-part
							(if (>= hue afactor360)
								0
								(/
									(/ hue afactor)
									60
								)
							)
						)
					)
					; arrange the right vars for the current
					; color circle part
					(case circle-part
						((0) (get-color-rgb value  	tt 		p))
						((1) (get-color-rgb q 		value 	p))
						((2) (get-color-rgb p 		value 	tt))
						((3) (get-color-rgb p 		q 		value))
						((4) (get-color-rgb tt 		p 		value))
						((5) (get-color-rgb value 	p 		p))
					)
				)
			)
		)
	)
)

; adjusts the hue, brightness and saturation of color
(define (adjust-color color)
	(let*
		(
			; hue saturation value list
			(hsv (rgb2hsv color))
			; add used configured adjustment to
			; the hue of the color
			(a-hue
				(+
					(nth 0 hsv)
					(* mxflat:color-hue afactor)
				)
			)
			; adjust saturation with
			; user configured factor
			(a-saturation
				(/
					(*
						(nth 1 hsv)
						mxflat:color-saturation
					)
					100
				)
			)
			; adjust value/brightness with
			; user configured factor
			(a-value
				(/
					(*
						(nth 2 hsv)
						mxflat:color-brightness
					)
					100
				)
			)
		)
		(hsv2rgb
			(list
				; adjust the hue if necessary
				; (ie it is >360 degree)
				(if (> a-hue afactor360)
					(- a-hue afactor360)
					a-hue
				)
				a-saturation
				a-value
			)
		)
	)
)

;
;; misc:hook-ins
;

; when the window title or some other wm_hints attribute changes, redraw the window
(call-after-property-changed (list 'WM_NAME)
	(lambda (w)
		(if (eq mxflat:title-x-justify 'real-center)
			(rebuild-frame w)
			(refresh-one-frame w)
		)
	)
)

; when we set a window sticky we have to redraw it,
; the sticky button has changed
(call-after-state-changed '(sticky)	reframe-window)

; when the window is sent to another workspace
; redraw it to update the buttons and titlebar if necessary
(add-hook 'remove-from-workspace-hook
	(lambda (w)
		(reframe-window w)
	)
)

; not necessary anymore?
;(add-hook 'after-resize-hook refresh-one-frame)

(add-hook 'focus-in-hook
	(lambda (w)
		; if 'custom borders for focused windows' -> redraw
		(if (or
				mxflat:custom-border-focused
				(and
					mxflat:fake-shadow
					mxflat:fake-shadow-focused-only
				)
			)
			(reframe-window w)
		)
	)
)

(add-hook 'focus-out-hook
	(lambda (w)
		; if 'custom borders for focused windows' -> redraw
		(if (or
				mxflat:custom-border-focused
				(and
					mxflat:fake-shadow
					mxflat:fake-shadow-focused-only
				)
			)
			(reframe-window w)
		)
	)
)

;
;; misc:functions
;

(define (get-border-offset)
	; we use the negative value to make the defcustom option
	; work more like one would expect, ie a positive value
	; means the border will have a bigger distance to the
	; contents window
	(- mxflat:border-offset)
)

(define (get-button-size)
	(if
		(and
			mxflat:customize-button-size
			(< mxflat:button-size mxflat:title-height)
		)
		mxflat:button-size
		mxflat:title-height
	)
)

;   -----------
;;; FAKE SHADOW
;   -----------

; this list defines which window sides have shadows
; for which direction
(defvar shadow-sides
	`(
		(north . (bottom left right))
		(east . (top bottom left))
		(west . (top bottom right))
		(south . (top left right))
		(north-east . (bottom left))
		(north-west . (bottom right))
		(south-west . (top right))
		(south-east . (top left))
	)
)

; selects the right shadow-side from the shadow-sides list
(define (get-shadow-side)
	(mapc
		(lambda (ss)
			(if (eq (car ss) mxflat:shadow-light-source)
				(setq shadow-side (cdr ss))
			)
		)
		shadow-sides
	)
)

(define (get-shadow-color) mxflat:shadow-color)

(define (get-shadow-width w side)
	(if (member side shadow-side)
		(get-neg-border-width w side)
		mxflat:shadow-width
	)
)

(define (get-shadow-offset w side)
	; the shadow is only not visible on all window edges
	(if (not (window-maximized-p w))
		(if
			(or
				(and
					(eq mxflat:shadow-light-source 'north)
					(not (eq side 'bottom))
				)
				(and
					(eq mxflat:shadow-light-source 'south)
					(not (eq side 'top))
				)
				(and
					(eq mxflat:shadow-light-source 'east)
					(not (eq side 'left))
				)
				(and
					(eq mxflat:shadow-light-source 'west)
					(not (eq side 'right))
				)
			)
			(/ mxflat:shadow-width mxflat:shadow-light-distance)
			(if (member side shadow-side)
				mxflat:shadow-width
				0
			)
		)
		0
	)
)

;   --------
;;; TITLEBAR
;   --------

;
;; titlebar:colors
;

(define (get-text-colors)
	(if mxflat:customize-extended-text-colors
		`(
   			(inactive				. ,mxflat:text-color-unfocused)
			(focused				. ,mxflat:text-color-focused)
			(highlighted			. ,mxflat:text-color-highlighted)
			(inactive-highlighted	. ,mxflat:text-color-inactive-highlighted)
			(clicked				. ,mxflat:text-color-clicked)
			(inactive-clicked		. ,mxflat:text-color-inactive-clicked)
		)
		`(
   			(inactive				. ,mxflat:text-color-unfocused)
			(focused				. ,mxflat:text-color-focused)
			(highlighted			. ,mxflat:text-color-focused)
			(inactive-highlighted	. ,mxflat:text-color-focused)
			(clicked				. ,mxflat:text-color-focused)
			(inactive-clicked		. ,mxflat:text-color-focused)
		)
	)
)

(define (titlebar-gradient-colors-helper)
	(case mxflat:titlebar-gradient-color-mode
		((custom)
			(if (> (length mxflat:titlebar-gradient-bgcolors) 0)
				mxflat:titlebar-gradient-bgcolors
				(list (get-color "#ffffffffffff"))
			)
		)
		((rainbow)
			(list
				(get-color "#ffff0000153b")
				(get-color "#ffff9b700000")
				(get-color "#fffff92d0000")
				(get-color "#3661ffff0000")
				(get-color "#00004c0dffff")
				(get-color "#b7ad0000ffff")
			)
		)
		((jamaica)
			(list
				(get-color "#7899f1340000")
				(get-color "#ffffffff0000")
				(get-color "#ffff37030000")
			)
		)
		((rgb)
			(list
				(get-color "#ffff00000000")
				(get-color "#0000ffff0000")
				(get-color "#00000000ffff")
			)
		)
	)
)
	
(define (titlebar-gradient-colors)
	(if mxflat:titlebar-gradient-bgcolors-reverse
		(reverse (titlebar-gradient-colors-helper))
		(titlebar-gradient-colors-helper)
	)
)

; changes the brightness and saturation for the
; titlebar gradient color with index i
(define (adjust-titlebar-bgcolor i)
	; get rgb ints for the color with index i
	(adjust-color (nth i (titlebar-gradient-colors)))
)

(define (get-titlebar-bgcolors #!optional side)
	(cond
		((and mxflat:title-gradient side)
			(let*
				(
					(index
						(if (eq side 'right)
							(1- (length (titlebar-gradient-colors)))
							0
						)
					)
				)
				`(
					(inactive	. ,(adjust-titlebar-bgcolor index))
					(focused	. ,(nth index (titlebar-gradient-colors)))
				)
			)
		)
		(t
			`(
				(inactive	. ,mxflat:titlebar-bgcolor-unfocused)
				(focused	. ,mxflat:titlebar-bgcolor-focused)
			)
		)
	)
)

;
;; titlebar:bg rendering
;

(define (num-titlebar-parts)
	(if
		(or
			(= (length (titlebar-gradient-colors)) 1)
			(eq mxflat:gradient-direction 'none)
		)
		(length (titlebar-gradient-colors))
		(1- (length (titlebar-gradient-colors)))
   	)
)

; render frame-part background
; this function takes the background img and uses
; many single 2-color gradients to fill it
(define (render-bg img state)
	(let*
		(
			; width and height of the titlebar part we have to fill
			(width (car (image-dimensions img)))
			(height (cdr (image-dimensions img)))

			; 2-color gradient width
			(part-width (/ width (num-titlebar-parts)))
			(rest-width (modulo width (num-titlebar-parts)))

			; the offset in the img that we've already reached
			; with our 2-color gradients
			(offset 0)

			; get the width of the 2-color gradient with index i
			(titlebar-parts-width-helper (lambda (i)
				(if (= i (1- (num-titlebar-parts)))
					(+ part-width rest-width)
					part-width
				)
			))

			; check the 2-color gradient width if it is > 0
			(titlebar-parts-width (lambda (i)
				(let
					(
						(val (titlebar-parts-width-helper i))
					)
					(if (< val 1)
						1
						val
					)
				)
			))

			; 2-color gradient image
			(img2 ())
		)

		(case mxflat:gradient-direction
			((horizontal none)
				(do
					((i 0 (1+ i)))
					((= i (num-titlebar-parts)))

					; create the 2-color gradient image and fill
					; it with a background color
					(setq img2
						(make-sized-image
							(titlebar-parts-width i)
							height
							(if state
								(nth i (titlebar-gradient-colors))
								(adjust-titlebar-bgcolor i)
							)
						)
					)

					; fill the img2 with the 2-color gradient
					; in necessary
					(if (not (eq mxflat:gradient-direction 'none))
						(apply
							draw-horizontal-gradient
							img2
							(if state
								(list
									(nth i (titlebar-gradient-colors))
									(if (or
											(= (length  (titlebar-gradient-colors)) 1)
											(eq mxflat:gradient-direction 'none)
										)
										(nth i (titlebar-gradient-colors))
										(nth (1+ i) (titlebar-gradient-colors))
									)
								)
								(list
									(adjust-titlebar-bgcolor i)
									(if (or
										(= (length (titlebar-gradient-colors)) 1)
											(eq mxflat:gradient-direction 'none)
										)
										(adjust-titlebar-bgcolor i)
										(adjust-titlebar-bgcolor (1+ i))
									)
								)
							)
						)
					)

					; paste the current 2-color gradient into
					; the titlebar background image
					(composite-images img img2 offset 0)
					(setq offset (+ offset (titlebar-parts-width i)))
				)
			)
			((vertical diagonal)
				(apply
					(if (eq mxflat:gradient-direction 'vertical)
						draw-vertical-gradient
						draw-diagonal-gradient
					)
					img
					(if state
						(list
							(nth 0 (titlebar-gradient-colors))
							(if (= (length  (titlebar-gradient-colors)) 1)
								(nth 0 (titlebar-gradient-colors))
								(nth 1 (titlebar-gradient-colors))
							)
						)
						(list
							(adjust-titlebar-bgcolor 0)
							(if (= (length (titlebar-gradient-colors)) 1)
								(adjust-titlebar-bgcolor 0)
								(adjust-titlebar-bgcolor 1)
							)
						)
					)
				)
			)
		)
		img
	)
)

;
;; titlebar:string
;

(define (get-title-font) mxflat:title-font)

; replace vars in the title string with their actual values
; and return the  new title string
(define (get-title-string w)
	(if mxflat:custom-title
		(let*
			(
				(title mxflat:title-string)
				(update-title (lambda (match-str replace-str)
						(while (string-match match-str title)
							(setq title
								(concat
									(expand-last-match "\\1")
									(if (numberp replace-str)
										(number->string replace-str)
										replace-str
									)
									(expand-last-match "\\3")
								)
							)
						)
					))
				)
				; some built-in vars
				(update-title "(.*)(\\$name)(.*)" (window-name w))
				(update-title "(.*)(\\$fullname)(.*)" (window-full-name w))
				(update-title "(.*)(\\$width)(.*)" (car (window-frame-dimensions w)))
				(update-title "(.*)(\\$height)(.*)" (cdr (window-frame-dimensions w)))
				(update-title "(.*)(\\$width-content)(.*)" (car (window-dimensions w)))
				(update-title "(.*)(\\$height-content)(.*)" (cdr (window-dimensions w)))
				(update-title "(.*)(\\$x-pos)(.*)" (car (window-position w)))
				(update-title "(.*)(\\$y-pos)(.*)" (cdr (window-position w)))
				(update-title "(.*)(\\$id)(.*)" (window-id w))
				(update-title "(.*)(\\$depth)(.*)" (window-get w 'depth))
				(update-title "(.*)(\\$gid)(.*)" (window-group-id w))
				(update-title "(.*)(\\$last-ws)(.*)" (get-last-workspace))
				(update-title "(.*)(\\$current-ws)(.*)"
						(let
							(
								(workspaces (window-get w 'workspaces))
							)
							(if workspaces
								(1+ (nth 0 workspaces))
								"*"
							)
						)
				)
				; replace environment vars
				(while (string-match "(.*)\\$\\((.+?)\\)(.*)" title)
					(let
						(
							(part1 (expand-last-match "\\1"))
							(part2 (expand-last-match "\\3"))
							(envvar (getenv (expand-last-match "\\2")))
						)
						(if (not envvar)
							(setq envvar (concat "$?(" (expand-last-match "\\2") ")"))
						)
						(setq title (concat part1 envvar part2))
					)
				)
				; replace rep functions
				; XXX: no arguments to the functions possible yet,
				; does someone need this?
				(while (string-match "(.*)\\$\\[(.+?)\\](.*)" title)
					(let
						(
							(part1 (expand-last-match "\\1"))
							(part2 (expand-last-match "\\3"))
							(symb (intern (expand-last-match "\\2")))
							(func ())
							(value (concat "$?[" (expand-last-match "\\2") "]"))
						)
						(if (boundp symb)
							(setq func (symbol-value symb))
						)
						(if (functionp func)
							(setq value (funcall func))
						)
						(setq title (concat part1 value part2))
					)
				)
				title
			)
			(window-name w)
		)
)

;
;; titlebar:position
;

; calculate the leftmost x position of an object on the titlebar
; this function is also used for button positions on the titlebar
(define (calculate-position pos)
	(if
		(and
			mxflat:customize-buttons
			(eq mxflat:button-position-model 'pixel)
		)
		; if the button-position-model is pixel we dont
		; need to calculate anything but just return the
		; argument value
		pos
		; actually calculate the positon
		(*
			(1- pos)
			(+
				(get-button-size)
				mxflat:button-distance
			)
		)
	)
)

(define (get-title-height)
	(+
		mxflat:title-height
		mxflat:border-width-titlebar-contents
	)
)

; not really the neg value since we add the border offset
; but thats the easiest place for doing so
(define (get-neg-title-height)
	(-
		(get-border-offset)
		(get-title-height)
	)
)

(define (get-titlebar-height w)
	(+
		(get-title-height)
		(get-border-width w 'top)
	)
)

; not really the neg value since we add the border offset
; but thats the easiest place for doing so
(define (get-neg-titlebar-height w)
	(-
		(get-border-offset)
		(get-titlebar-height w)
	)
)

	; get the 1+ position of the leftmost or rightmost button
(define (get-last-button-position left-right)
	(let
		((pos
			(if mxflat:customize-buttons
				(abs (apply
					(if (eq left-right 'left)
						min
						max
					)
					(list mxflat:menu-button-position mxflat:shade-button-position
						mxflat:sticky-button-position mxflat:previous-button-position
						mxflat:next-button-position mxflat:configure-button-position
						mxflat:info-button-position mxflat:iconify-button-position
						mxflat:maximize-button-position mxflat:close-button-position
						mxflat:always-on-top-button-position mxflat:lock-button-position
					)
				))
				(if (eq left-right 'left)
					3
					2
				)
			)
		))
		(if
			(or
				(eq mxflat:button-position-model 'title-height)
				(not mxflat:customize-buttons)
			)
			(calculate-position (1+ pos))
			(if (eq mxflat:button-position-model 'pixel)
				(+
					pos
					mxflat:title-height
				)
				1
			)
		)
	)
)

; by default the title is vertically centered and pixelwise
; adjusted through mxflat:title-y-adjustment
(define (get-title-y-alignment)
	(if mxflat:customize-title-position
		(case mxflat:title-y-justify
			((top)
				; sanity check on the value
				; else the title string would not be visible
				(if (> mxflat:title-y-adjustment 0)
					mxflat:title-y-adjustment
					0
				)
			)
			
			; we have to calculate it manually to be able to take
			; the y align adjustment into account
			((center)
				(+
					mxflat:title-y-adjustment
					(/
						(-
							mxflat:title-height
							(font-height mxflat:title-font)
						)
						2
					)
				)
			)
			; bottom
			(t
				; sanity check on the value
				; else the title string would not be visible
				(if (< mxflat:title-y-adjustment 0)
					mxflat:title-y-adjustment
					-1
				)
			)
		)
		'center
	)
)

(define (get-title-x-alignment w)
	(if mxflat:customize-title-position
		(case mxflat:title-x-justify
			((left)
				; sanity check on the value
				; else the title string would not be visible
				(if (< mxflat:title-x-adjustment 0)
					0
					mxflat:title-x-adjustment
				)
			)
			((center)
				'center
			)
			((right)
				; sanity check on the value
				; else the title string would not be visible
				(if (> mxflat:title-x-adjustment -1)
					-1
					mxflat:title-x-adjustment
				)
			)
			; 'real-center' means to take the whole titlebar into account
			; while centering the title string
			((real-center)
				(let
					(
						(real-title-offset
							(/
								(-
									(car (window-dimensions w))
									(text-width (get-title-string w) (get-title-font))
								)
								2
							)
						)
						(title-offset
							(-
								(get-last-button-position 'left)
								mxflat:title-x-adjustment
							)
						)
					)
					(if (> title-offset real-title-offset)
						0
						(- real-title-offset title-offset)
					)
				)
			)
		)
		'center
	)
)

(define (left-buttons-end-offset)
	(get-last-button-position 'left)
)
(define (right-buttons-end-offset)
	(get-last-button-position 'right)
)

;
;; titlebar:creation
;

(define (titlebar-center backgroun backgroundd)
	`(
		(
			(class			. title)
			(,backgroun		. ,backgroundd)
			(render-scale	. ,mxflat:gradient-scale)
			(foreground		. ,get-text-colors)
			(font			. ,get-title-font)
			(text			. ,get-title-string)
			(x-justify		. ,get-title-x-alignment)
			(y-justify		. ,get-title-y-alignment)
			(left-edge		.
				,(lambda ()
					(+
						(left-buttons-end-offset)
						(get-border-offset)
					)
				)
			)
			(right-edge		.
				,(lambda ()
					(+
						(right-buttons-end-offset)
						(get-border-offset)
					)
				)
			)
			(top-edge		. ,get-neg-title-height)
			(height			. ,(lambda () mxflat:title-height))
		)
	)
)

(define (titlebar-left-right backgroun backgroundd edg1 edgee1 edg2 edgee2)
	`(
		; left part of the titlebar
		(
			(class			. title)
			(,backgroun		. ,backgroundd)
			(render-scale	. ,mxflat:gradient-scale)
			(,edg1			. ,edgee1)
			(,edg2			. ,edgee2)
			(top-edge		. ,get-neg-title-height)
			(height			. ,(lambda () mxflat:title-height))
		)
	)
)

(define (titlebar)
	(append
		(if
			(and
				(or
					(eq mxflat:gradient-direction 'vertical)
					(eq mxflat:gradient-direction 'diagonal)
				)
				mxflat:title-gradient
			)
			(append
				(titlebar-left-right
					'renderer render-bg
					'left-edge get-border-offset
					'width left-buttons-end-offset
				)
				(titlebar-left-right
					'renderer render-bg
					'width right-buttons-end-offset
					'right-edge get-border-offset
				)
			)
			(append
				(titlebar-left-right
					'background	(lambda () (get-titlebar-bgcolors 'left))
					'left-edge get-border-offset
					'width left-buttons-end-offset
				)
				(titlebar-left-right
					'background	(lambda () (get-titlebar-bgcolors 'right))
					'width right-buttons-end-offset
					'right-edge get-border-offset
				)
			)
		)
		(if mxflat:title-gradient
			(titlebar-center 'renderer render-bg)
			(titlebar-center 'background get-titlebar-bgcolors)
		)
	)
)

;   -------
;;; BORDERS
;   -------

;
;; borders:colors
;

(define (get-border-color)
	(if mxflat:customize-border-colors
		`(
			(inactive				. ,mxflat:border-color-unfocused)
			(focused				. ,mxflat:border-color-focused)
			(highlighted			. ,mxflat:border-color-highlighted)
			(inactive-highlighted	. ,mxflat:border-color-inactive-highlighted)
			(clicked				. ,mxflat:border-color-clicked)
			(inactive-clicked		. ,mxflat:border-color-inactive-clicked)
		)
		`(
			(inactive				. ,mxflat:text-color-unfocused)
			(focused				. ,mxflat:text-color-focused)
		)
	)
)

(define (get-border-color-titlebar-contents)
	(if mxflat:customize-border-colors
		`(
			(inactive . ,mxflat:border-color-unfocused)
			(focused . ,mxflat:border-color-focused)
		)
		`(
			(inactive . ,mxflat:text-color-unfocused)
			(focused . ,mxflat:text-color-focused)
		)
	)
)

;
;; borders:width
;

(define (get-border-width w #!optional side)
	(cond
		(
			(or
				(and
					(window-maximized-vertically-p w)
					(or
						(eq side 'top)
						(eq side 'bottom)
					)
				)
				(and
					(window-maximized-horizontally-p w)
					(or
						(eq side 'left)
						(eq side 'right)
					)
				)
			)
			mxflat:border-width-maximized
		)
		(
			(and
				mxflat:custom-border-focused
				(eq w (input-focus))
			)
			mxflat:border-width-focused
		)
		(
			t
			mxflat:border-width-unmaximized
		)
	)
)

(define (get-neg-border-width w #!optional side)
	(-
		(get-border-offset)
		(get-border-width w side)
	)
)

;
;; borders:creation
;

(define (create-border-frame-parts color offset widthh #!optional mode border-list)
	(let*
		(
			(get-pos (lambda (w side)
				(-
					(offset w side)
					(widthh w side)
				)
			))
			; bottom edge of the frame
			(bottom-edgee1 'bottom-edge)
			(bottom-edgee2 'bottom-edge)
			(bottom-edg1 (lambda (w) (offset w 'bottom)))
			(bottom-edg2 (lambda (w) (get-pos w 'bottom)))
			(top-edg1 (lambda (w)
				(-
					(offset w 'top)
					(get-title-height)
				)
			))
			(top-edg2 (lambda (w)
				(-
					(offset w 'top)
					(+
						(get-title-height)
						(widthh w 'top)
					)
				)
			))
			(left-edg (lambda (w) (get-pos w 'left)))
			(right-edg (lambda (w) (get-pos w 'right)))
			(right-borderr (if border-list 'shadow 'right-border))
			(left-borderr (if border-list 'shadow 'left-border))
			(top-borderr (if border-list 'shadow 'top-border))
			(bottom-borderr (if border-list 'shadow 'bottom-border))
		)
		(case mode
			('transient
				(setq top-edg1 (lambda (w) (offset w 'top)))
				(setq top-edg2 (lambda (w) (get-pos w 'top)))
			)
			('shaded
				(setq bottom-edgee2 'top-edge)
				(setq bottom-edg2 (lambda (w) (- (offset w 'bottom) mxflat:border-width-titlebar-contents)))
				; when the window is shaded we rather define the borders by
				; top-edge and height instead of using top- and bottom-edge
				(setq bottom-edgee1 'height)
				(setq bottom-edg1 (lambda (w) (get-titlebar-height w)))
			)
		)
		(append
			; draw the right border
			(if
				(or
					(not border-list)
					(member 'right border-list)
				)
				`((
					(class		. ,right-borderr)
					(background	. ,color)
					(width		. ,(lambda (w) (widthh w 'right)))
					(right-edge	. ,right-edg)
					(top-edge	. ,top-edg1)
					(,bottom-edgee1		. ,bottom-edg1)
				))
			)

			; draw the left border
			(if
				(or
					(not border-list)
					(member 'left border-list)
				)
				`((
					(class		. ,left-borderr)
					(background	. ,color)
					(width		. ,(lambda (w) (widthh w 'left)))
					(left-edge	. ,left-edg)
					(top-edge	. ,top-edg1)
					(,bottom-edgee1		. ,bottom-edg1)
				))
			)

			; draw the top border of the title frame
			(if
				(or
					(not border-list)
					(member 'top border-list)
				)
				`((
					(class		. ,top-borderr)
					(background	. ,color)
					(height		. ,(lambda (w) (widthh w 'top)))
					(left-edge	. ,left-edg)
					(right-edge	. ,right-edg)
					(top-edge	. ,top-edg2)
				))
			)

			; draw the bottom border
			(if
				(or
					(not border-list)
					(member 'bottom border-list)
				)
				`((
					(class		. ,bottom-borderr)
					(background	. ,color)
					(height		. ,(lambda (w) (widthh w 'bottom)))
					(left-edge	. ,left-edg)
					(right-edge	. ,right-edg)
					(,bottom-edgee2		. ,bottom-edg2)
				))
			)
		)
	)
)

; this function appends the window border (frame) and its shadow
(define (create-borders w #!optional mode)
	(if (= (length shadow-side) 0)
		(get-shadow-side)
	)
	(append
		; create normal window border
		(create-border-frame-parts
			get-border-color
			get-border-offset
			get-border-width
			mode
		)
		; create fake shadow if necessary
		(if
			(and
				mxflat:fake-shadow
				(or
					(not mxflat:fake-shadow-focused-only)
					(eq w (input-focus))
				)
			)
			(create-border-frame-parts
				get-shadow-color
				get-shadow-width
				get-shadow-offset
				mode
				shadow-side
			)
		)
	)
)

; create the (small) line between titlebar and contents window
(define (create-titlebar-contents-border)
	`(
		(
			(class			. middle-border)
			(background		. ,get-border-color-titlebar-contents)
			(height			. ,(lambda () mxflat:border-width-titlebar-contents))
			(top-edge		. ,(lambda ()
				(-
					(get-border-offset)
					mxflat:border-width-titlebar-contents
				)
			))
			(left-edge		. ,get-border-offset)
			(right-edge		. ,get-border-offset)
		)
	)
)

; return a top border (for shaded-transient)
(define (create-top-border)
	`(
		(
			(class			. top-border)
			(background		. ,get-border-color)
			(height			. ,(lambda (w) (get-border-width w 'top)))
			(top-edge		. ,(lambda (w) (get-neg-border-width w 'top)))
			(left-edge		. 0)
			(right-edge		. 0)
		)
	)
)

;   -------
;;; BUTTONS
;   -------


; create, scale and cache an image
(define (get-image img)
	(or
		(table-ref icon-cache img)
		(let
			(
				(image
					(if mxflat:scale-buttons
						(scale-image
							(make-image img)
							mxflat:button-scale mxflat:button-scale
						)
						(make-image img)
					)
				)
			)
			(table-set icon-cache img image)
			image
		)
	)
)

;
;; buttons:colors
;

(define (get-button-bgcolor #!optional edge)
	(cond
		(mxflat:title-gradient
			(let*
				(
					(index
						(if (eq edge 'right-edge)
							(1- (length (titlebar-gradient-colors)))
							0
						)
					)
				)
				`(
					(inactive				. ,(adjust-titlebar-bgcolor index))
					(focused				. ,(nth index (titlebar-gradient-colors)))
					(highlighted			. ,(adjust-titlebar-bgcolor index))
					(inactive-highlighted	. ,(nth index (titlebar-gradient-colors)))
					(clicked				. ,(adjust-titlebar-bgcolor index))
					(inactive-clicked		. ,(nth index (titlebar-gradient-colors)))
				)
			)
		)
		(mxflat:custom-button-colors
			`(
				(inactive				. ,mxflat:button-bgcolor-unfocused)
				(focused				. ,mxflat:button-bgcolor-focused)
				(highlighted			. ,mxflat:button-bgcolor-highlighted)
				(inactive-highlighted	. ,mxflat:button-bgcolor-inactive-highlighted)
				(clicked				. ,mxflat:button-bgcolor-clicked)
				(inactive-clicked		. ,mxflat:button-bgcolor-inactive-clicked)
			)
		)
		(t
			`(
				(inactive				. ,mxflat:titlebar-bgcolor-unfocused)
				(focused				. ,mxflat:titlebar-bgcolor-focused)
				(highlighted			. ,mxflat:titlebar-bgcolor-unfocused)
				(inactive-highlighted	. ,mxflat:titlebar-bgcolor-focused)
				(clicked				. ,mxflat:text-color-unfocused)
				(inactive-clicked		. ,mxflat:text-color-unfocused)
			)
		)
	)
)


;
;; buttons:icons
;

; return a list of 4 different image objects
; as needed for the button fg images/icons
(define (get-images img)
	(let
		(
			(image (get-image (concat "button-icons/" (symbol-name mxflat:button-icon-set) "/" img)))
		)
		(list
			image
			(copy-image image)
			(copy-image image)
			(copy-image image)
		)
	)
)

(defvar get-menu-button1 (get-images "button-menu.png"))
(define (get-menu-button w)
	(if mxflat:menu-button-icon
		(or
			(table-ref icon-cache w)
			(or
				(let
					(
						(icon (window-icon-image w))
					)
					(when icon
						(let
							(
								(scaled
									(scale-image icon mxflat:menu-button-scale
										mxflat:menu-button-scale)
								)
							)
							(table-set icon-cache w scaled)
							scaled
						)
					)
				)
				get-menu-button1
			)
		)
		get-menu-button1
	)
)

(defvar get-shade-button1 (get-images "button-shade1.png"))
(defvar get-shade-button2 (get-images "button-shade2.png"))
(define (get-shade-button w)
	(if (window-get w 'shaded)
		get-shade-button2
		get-shade-button1
	)
)

(defvar get-sticky-button1 (get-images "button-sticky1.png"))
(defvar get-sticky-button2 (get-images "button-sticky2.png"))
(define (get-sticky-button w)
	(if (window-get w 'sticky)
		get-sticky-button2
		get-sticky-button1
	)
)

(defvar get-iconify-button (get-images "button-minimize.png"))

(defvar get-maximize-button1 (get-images "button-maximize1.png"))
(defvar get-maximize-button2 (get-images "button-maximize2.png"))
(define (get-maximize-button w)
	(if (window-maximized-p w)
		get-maximize-button2
		get-maximize-button1
	)
)

(defvar get-close-button (get-images "button-close.png"))

(defvar get-previous-button1 (get-images "button-previous1.png"))
(defvar get-previous-button2 (get-images "button-previous2.png"))
(define (get-previous-button w)
	(if
		(or
			(window-in-workspace-p w (- (get-first-workspace) 1))
			(window-get w 'sticky)
		)
		get-previous-button2
		get-previous-button1
	)
)

(defvar get-next-button1 (get-images "button-next1.png"))
(defvar get-next-button2 (get-images "button-next2.png"))
(define (get-next-button w)
	(if
		(or
			(window-in-workspace-p w (- (get-last-workspace) 1))
			(window-get w 'sticky)
		)
		get-next-button2
		get-next-button1
	)
)

(defvar get-configure-button (get-images "button-configure.png"))

(defvar get-info-button (get-images "button-info.png"))

(defvar get-always-on-top-button1 (get-images "button-a1.png"))
(defvar get-always-on-top-button2 (get-images "button-a2.png"))
(define (get-always-on-top-button w)
	(if (> (window-get w 'depth) 0)
		get-always-on-top-button2
		get-always-on-top-button1
	)
)

(defvar get-lock-button1 (get-images "button-lock1.png"))
(defvar get-lock-button2 (get-images "button-lock2.png"))
(define (get-lock-button w)
	(if (window-get w 'fixed-position)
		get-lock-button2
		get-lock-button1
	)
)

;
;; buttons:fg recolor
;

; this function is stolen from the crux sawfish theme
; (the theme is included in the original sawfish package)
(setq recolor-all (lambda ()
	(let*
		(
			(recolorer (lambda (color1 color2)
				(make-image-recolorer
					(if mxflat:custom-button-colors
						color1
						color2
					)
					#:zero-channel blue-channel
					#:index-channel red-channel
				)
			))
			
			; unfocused
			(recolorer1
				(recolorer
					mxflat:button-fgcolor-inactive
					mxflat:text-color-unfocused
				)
			)
			; focused
			(recolorer2
				(recolorer
					mxflat:button-fgcolor-focused
					mxflat:text-color-focused
				)
			)
			; highlighted
			(recolorer3
				(recolorer
					mxflat:button-fgcolor-highlighted
					mxflat:text-color-unfocused
				)
			)
			; clicked
			(recolorer4
				(recolorer
					mxflat:button-fgcolor-clicked
					mxflat:text-color-focused
				)
			)
		)
    	(mapc
			(lambda (x)
				(apply recolorer1 x)
				(apply recolorer2 (cdr x))
				(if (> (length x) 2)
					(apply recolorer3 (nthcdr 2 x))
				)
				(if (> (length x) 3)
					(apply recolorer4 (nthcdr 3 x))
				)
			)
			; list of all button fg icon lists that we have to recolor
			(list get-menu-button1 get-shade-button1 get-shade-button2
				get-sticky-button1 get-sticky-button2 get-iconify-button
				get-maximize-button1 get-maximize-button2 get-close-button
				get-previous-button1 get-previous-button2 get-next-button1
				get-next-button2 get-configure-button get-info-button
				get-always-on-top-button1 get-always-on-top-button2
				get-lock-button1 get-lock-button2
			)
		)
	)
))

;
;; buttons:define classes
;

; missing button frame part classes
; all other classes are defined by sawfish by default -
; it are always 2 classes, one with the default key binding
; and one with the custom (user defined) one
(define-frame-class 'configure-button-custom	'((keymap . mxflat:configure-button-keymap)))
(define-frame-class 'configure-button			'((keymap . configure-button-keymap)))
(define-frame-class 'info-button-custom			'((keymap . mxflat:info-button-keymap)))
(define-frame-class 'info-button				'((keymap . info-button-keymap)))
(define-frame-class 'next-button-custom			'((keymap . mxflat:next-button-keymap)))
(define-frame-class 'next-button				'((keymap . next-button-keymap)))
(define-frame-class 'previous-button-custom		'((keymap . mxflat:previous-button-keymap)))
(define-frame-class 'previous-button			'((keymap . previous-button-keymap)))
(define-frame-class 'sticky-button-custom		'((keymap . mxflat:sticky-button-keymap)))
(define-frame-class 'sticky-button				'((keymap . sticky-button-keymap)))
(define-frame-class 'always-on-top-button-custom '((keymap . mxflat:always-on-top-button-keymap)))
(define-frame-class 'always-on-top-button		'((keymap . always-on-top-button-keymap)))
(define-frame-class 'lock-button-custom			'((keymap . mxflat:lock-button-keymap)))
(define-frame-class 'lock-button				'((keymap . lock-button-keymap)))

;
;; buttons:creation
;

; this function actually creates a button frame part and returns it
(define (create-button-frame-part classs fg edge x-pos y-pos backgroun backgroundd)
	; button frame part definition
	`(
		(class		. ,classs)
		(,backgroun	. ,backgroundd)
		(render-scale . ,mxflat:gradient-scale)
		(foreground	. ,fg)
		(x-justify	. center)
		(y-justify	. center)
		(,edge		. ,x-pos)
		(top-edge	. ,y-pos)
		(height		. ,get-button-size)
		(width		. ,get-button-size)
	)
)

; gather all necessary arguments and then call create-button-frame-part
; to actually create the button
(define (create-button button-type #!optional x-pos-default)
	(let
		(
			; raw position, creatue-button-frame-part does some more
			; calculations on it
			(x-pos-raw
				(if mxflat:customize-buttons
					(check-symbol "mxflat:" button-type "-button-position")
					(if x-pos-default x-pos-default 0)
				)
			)
		)
		
		; position = 0 means the button is not displayed
		; so we can save some time be not doing all these evaluations
		(if (not (= x-pos-raw 0))
			(let*
				(
					; if the position parameter (x-pos-raw) for the current button is
					; positive it starts on the right side
					(edge (if (> x-pos-raw 0) 'right-edge 'left-edge))

					; default button background mode (ie static color)
					(backgroun 'background)
					(backgroundd (get-button-bgcolor edge))
					
					; button x offset from the left/right titlebar edge
					(x-pos (lambda ()
						(+
							(-
								(calculate-position (abs x-pos-raw))
								mxflat:border-offset
							)
							(if (> x-pos-raw 0)
								mxflat:button-right-offset
								mxflat:button-left-offset
							)
						)
					))

					; different top positions for different mxflat:button-y-alignment
					; but only in case mxflat:customize-button-size is true
					; because in the other case the buttons height and width is equal
					; to the titlebar height
					(y-pos (lambda ()
						(if mxflat:customize-button-size
							(case mxflat:button-y-alignment
								((top)
									(get-neg-title-height)
								)
								((bottom)
									(+
										(get-neg-title-height)
										(-
											mxflat:title-height
											(get-button-size)
										)
									)
								)
								; this is the center case
								(t
									(+
										(get-neg-title-height)
										(/
											(-
												mxflat:title-height
												(get-button-size)
											)
											2
										)
									)
								)
							)
							; in case the size is the default we start on top
							; of the titlebar
							(get-neg-title-height)
						)
					))
				)
				
				; in case of a gradient titlebar bg
				; change the button bg to rendering
				(cond
					(
						(and
							mxflat:title-gradient
							(or
								(eq mxflat:gradient-direction 'vertical)
								(eq mxflat:gradient-direction 'diagonal)
							)
						)
						(setq backgroun 'renderer)
						(setq backgroundd render-bg)
					)
				)

				(create-button-frame-part
					; decide which frame part (and indirectly which keymap) to use
					(if (check-symbol "mxflat:customize-" button-type "-keymap")
						(get-symbol button-type "-button-custom")
						(get-symbol button-type "-button")
					)
					; button fg images/icons
					(check-symbol "get-" button-type "-button")
					edge
					x-pos
					y-pos
					backgroun
					backgroundd
				)
			)
		)
	)
)

; create a list of button frame parts and return it
(define (create-buttons)
	; create a list of all custom buttons if the button
	; position model is not none (none means no buttons)
	(if (not (eq mxflat:button-position-model 'none))
		(list
			(create-button "menu" -1)
			(create-button "shade" -2)
			(create-button "iconify" +3)
			(create-button "maximize" +2)
			(create-button "close" +1)
			(create-button "sticky")
			(create-button "next")
			(create-button "previous")
			(create-button "configure")
			(create-button "info")
			(create-button "always-on-top")
			(create-button "lock")
		)
	)
)

;   -------
;;; CORNERS
;   -------

;
;; corners:size
;

(define (get-corner-width-adjustment w #!optional side)
	(if
		(or
			(and
				(window-maximized-vertically-p w)
				(or
					(eq side 'top)
					(eq side 'bottom)
				)
			)
			(and
				(window-maximized-horizontally-p w)
				(or
					(eq side 'left)
					(eq side 'right)
				)
			)
		)
		mxflat:corner-width-adjustment-maximized
		mxflat:corner-width-adjustment
	)
)

(define (get-corner-width w #!optional frame-type side)
	(if
		(and
			(eq frame-type 'shaded)
			(> (get-corner-width-adjustment w side) (/ (get-title-height) 2))
			(eq mxflat:corner-direction 'inside)
		)
		(/ (1+ (get-title-height)) 2)
		(+
			(get-corner-width-adjustment w side)
			(get-border-width w side)
		)
	)
)

(define (get-corner-length w #!optional frame-type)
	(if (= mxflat:corner-length-adjustment -1)
		0
		(+
			(+
				(if (and
						(eq frame-type 'shaded)
						(> mxflat:corner-length-adjustment (/ (get-title-height) 2))
					)
					(/ (1+ (get-title-height)) 2)
					mxflat:corner-length-adjustment
				)
				(get-border-width w 'top)
			)
			(if (eq mxflat:corner-direction 'inside)
				0
				(get-corner-width-adjustment w)
			)
		)
	)
)

;
;; corners:position
;

; get top or bottom position
(define (get-corner-1-pos w side part #!optional frame-type)
	(-
		(if (eq side 'bottom)
			(if (eq frame-type 'shaded)
				(+
					(if (eq part 'b)
						(+
							(- (get-corner-length w frame-type))
							(get-corner-width w frame-type side)
						)
						0
					)
					(-
						(get-border-offset)
						mxflat:border-width-titlebar-contents
					)
				)
				(get-neg-border-width w side)
			)
			(if (eq frame-type 'transient)
				(get-neg-border-width w 'top)
				(get-neg-titlebar-height w)
			)
		)
		(if
			(and
				(eq frame-type 'shaded)
				(eq side 'bottom)
			)
			(if (eq mxflat:corner-direction 'inside)
				(get-corner-width-adjustment w side)
				0
			)
			(if (eq mxflat:corner-direction 'inside)
				0
				(get-corner-width-adjustment w side)
			)
		)
	)
)

; get left or right position
(define (get-corner-2-pos w side)
		(-
			(get-neg-border-width w side)
			(if (eq mxflat:corner-direction 'inside)
				0
				(get-corner-width-adjustment w side)
			)
		)
)

;
;; corners:creation
;

; create the two corner lists a real corner consists of
(define (create-corner classs edgee1 side1 edgee2 side2 frame-type)
	`(
		(
			(class			. ,classs)
			(background		. ,get-border-color)
			(width			. ,get-corner-length)
			(height			. ,(lambda (w) (get-corner-width w frame-type side1)))
			(,edgee1			. ,(lambda (w) (get-corner-1-pos w side1 'a frame-type)))
			(,edgee2			. ,(lambda (w) (get-corner-2-pos w side2)))
		)
		(
			(class			. ,classs)
			(background		. ,get-border-color)
			(width			. ,(lambda (w) (get-corner-width w frame-type side2)))
			(height			. ,(lambda (w) (get-corner-length w frame-type)))
			(,edgee1			. ,(lambda (w) (get-corner-1-pos w side1 'b frame-type)))
			(,edgee2			. ,(lambda (w) (get-corner-2-pos w side2)))
		)
	)
)
; this function actually creates the 4 window corners and returns them
(define (create-corners #!optional frame-type)
	(if mxflat:show-corners
		(let
			(
				(edgee1 'top-edge)
				(edgee2 'bottom-edge)
			)
			(if (eq frame-type 'shaded)
				(setq edgee2 'top-edge)
			)
			(append
				(create-corner 'top-left-corner edgee1 'top 'left-edge 'left frame-type)
				(create-corner 'top-right-corner edgee1 'top 'right-edge 'right frame-type)
				(create-corner 'bottom-left-corner edgee2 'bottom 'left-edge 'left frame-type)
				(create-corner 'bottom-right-corner edgee2 'bottom 'right-edge 'right frame-type)
			)
		)
	)
)

;   --------------
;;; REGISTER THEME
;   --------------

#|

that is from the manual (http://sawmill.sourceforge.net/prog-manual.html)

Frame Types

default
	The normal frame type. Includes all decorations, both borders and the title bar.
	transient
	The frame for a transient window. This usually does not include a title bar,
	but does have all four borders.
shaped
	Shaped windows are normally decorated with only a title-bar, since their boundary
	is not rectangular it makes no sense to surround them with a rectangular border.
shaped-transient
	A combination of the shaped and transient types, normally just a very small title
	border with no text.
shaded
	A shaded window (normally just the title bar).
shaded-transient
	A shaded transient window.
unframed
	No frame at all, just the client window itself. The predefined nil-frame variable
	contains a null frame that may be used for this frame type.

"Normal" 'default
"Title-only" 'shaped
"Border-only" 'transient
"Top-border" 'shaped-transient
"None" 'unframed

|#

; return the window frame and decorations
(define (get-frame w frame-type)
	(let*
		(
			; window with all decorations
			(defaul (lambda ()
				(append (titlebar) (create-titlebar-contents-border)
					(create-borders w) (create-buttons) (create-corners))
			))

			; window with contents frame only
			(transien (lambda ()
				(append (create-borders w 'transient) (create-corners 'transient))
			))

			(titleba (lambda (#!optional mode)
				(if mode
					(titlebar)
					(append (titlebar) (create-buttons))
				)
			))

			; window that is shaded
			(shade (lambda (#!optional titlebar-mode)
				(if mxflat:keep-borders-when-shaded
					(append (titleba titlebar-mode) (create-titlebar-contents-border)
						(create-borders w) (create-corners))
					(append (titleba titlebar-mode)
						(create-borders w 'shaded) (create-corners 'shaded))
				)
			))

			(check (lambda (mode)
				(cond
					((eq mxflat:decoration-mode 'none)
						(transien)
					)
					((eq mxflat:decoration-mode 'all)
						(defaul)
					)
					(t
						(mode)
					)
				)
			))
		)

		(case frame-type
			((default)
				(check defaul)
			)
			((transient)
				(check transien)
			)
			((shaped shaded)
				(if (eq mxflat:decoration-mode 'none)
					(shade 'no-buttons)
					(shade)
				)
			)
			((shaded-transient)
				(if (eq mxflat:decoration-mode 'all)
					(shade)
					(shade 'no-buttons)
				)
			)
			((shaped-transient)
				(create-top-border)
			)
		)
	)
)

; setup initial colors
(recolor-all)
(after-set-title-update-interval)
; register theme with sawfish
(add-frame-style 'mxflat get-frame)

;;; END