summaryrefslogtreecommitdiffstats
path: root/twin/client.cpp
blob: f32c5064b2fcae28d2536a977e7fa89c617ec2fe (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
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
/*****************************************************************
 KWin - the KDE window manager
 This file is part of the KDE project.

Copyright (C) 1999, 2000 Matthias Ettrich <ettrich@kde.org>
Copyright (C) 2003 Lubos Lunak <l.lunak@kde.org>

You can Freely distribute this program under the GNU General Public
License. See the file "COPYING" for the exact licensing terms.
******************************************************************/

#include "client.h"

#include <math.h>

#include <tqapplication.h>
#include <tqpainter.h>
#include <tqdatetime.h>
#include <tqimage.h>
#include <tqfile.h>
#include <kprocess.h>
#include <unistd.h>
#include <kstandarddirs.h>
#include <tqwhatsthis.h>
#include <twin.h>
#include <kiconloader.h>
#include <tdelocale.h>
#include <stdlib.h>

#include "bridge.h"
#include "group.h"
#include "workspace.h"
#include "atoms.h"
#include "notifications.h"
#include "rules.h"

#include <X11/extensions/shape.h>

// put all externs before the namespace statement to allow the linker
// to resolve them properly

extern Atom tqt_wm_state;
extern Atom tqt_window_role;
extern Atom tqt_sm_client_id;

// wait 200 ms before drawing shadow after move/resize
static const int SHADOW_DELAY = 200;

namespace KWinInternal
{

/* TODO: Remove this once X has real translucency.
 *
 * A list of the regions covered by all shadows and the Clients to which they
 * belong. Used to redraw shadows when a window overlapping or underlying a
 * shadow is moved, resized, or hidden.
 */
struct ShadowRegion
    {
    TQRegion region;
    Client *client;
    };
static TQValueList<ShadowRegion> shadowRegions;

/*

 Creating a client:
     - only by calling Workspace::createClient()
         - it creates a new client and calls manage() for it

 Destroying a client:
     - destroyClient() - only when the window itself has been destroyed
     - releaseWindow() - the window is kept, only the client itself is destroyed

*/


/*!
  \class Client client.h

  \brief The Client class encapsulates a window decoration frame.

*/

/*!
 This ctor is "dumb" - it only initializes data. All the real initialization
 is done in manage().
 */
Client::Client( Workspace *ws )
    :   TQObject( NULL ),
        client( None ),
        wrapper( None ),
        frame( None ),
        decoration( NULL ),
        wspace( ws ),
        bridge( new Bridge( this )),
        move_faked_activity( false ),
        move_resize_grab_window( None ),
        transient_for( NULL ),
        transient_for_id( None ),
        original_transient_for_id( None ),
        in_group( NULL ),
        window_group( None ),
        in_layer( UnknownLayer ),
        ping_timer( NULL ),
        process_killer( NULL ),
        process_resumer( NULL ),
        user_time( CurrentTime ), // not known yet
        allowed_actions( 0 ),
        postpone_geometry_updates( 0 ),
        pending_geometry_update( false ),
        shade_geometry_change( false ),
        border_left( 0 ),
        border_right( 0 ),
        border_top( 0 ),
        border_bottom( 0 ),
        opacity_( 0 ),
        demandAttentionKNotifyTimer( NULL )
// SELI do all as initialization
    {
    autoRaiseTimer = 0;
    shadeHoverTimer = 0;

    shadowDelayTimer = new TQTimer(this);
    opacityCache = &activeOpacityCache;
    shadowAfterClient = NULL;
    shadowWidget = NULL;
    shadowMe = true;
    connect(shadowDelayTimer, TQT_SIGNAL(timeout()), TQT_SLOT(drawShadow()));

    // set the initial mapping state
    mapping_state = WithdrawnState;
    desk = 0; // no desktop yet

    mode = PositionCenter;
    buttonDown = FALSE;
    moveResizeMode = FALSE;

    info = NULL;

    shade_mode = ShadeNone;
    active = FALSE;
    deleting = false;
    keep_above = FALSE;
    keep_below = FALSE;
    is_shape = FALSE;
    motif_noborder = false;
    motif_may_move = TRUE;
    motif_may_resize = TRUE;
    motif_may_close = TRUE;
    fullscreen_mode = FullScreenNone;
    skip_taskbar = FALSE;
    original_skip_taskbar = false;
    minimized = false;
    hidden = false;
    modal = false;
    noborder = false;
    user_noborder = false;
    urgency = false;
    ignore_focus_stealing = false;
    demands_attention = false;
    check_active_modal = false;

    Pdeletewindow = 0;
    Ptakefocus = 0;
    Ptakeactivity = 0;
    Pcontexthelp = 0;
    Pping = 0;
    input = FALSE;
    skip_pager = FALSE;

    max_mode = MaximizeRestore;
    maxmode_restore = MaximizeRestore;
    
    cmap = None;

    frame_geometry = TQRect( 0, 0, 100, 100 ); // so that decorations don't start with size being (0,0)
    client_size = TQSize( 100, 100 );
    custom_opacity = false;
    rule_opacity_active = 0; //translucency rules
    rule_opacity_inactive = 0; //dito.

    // SELI initialize xsizehints??
    }

/*!
  "Dumb" destructor.
 */
Client::~Client()
    {
    assert(!moveResizeMode);
    assert( client == None );
    assert( frame == None && wrapper == None );
    assert( decoration == NULL );
    assert( postpone_geometry_updates == 0 );
    assert( !check_active_modal );
    delete info;
    delete bridge;
    }

// use destroyClient() or releaseWindow(), Client instances cannot be deleted directly
void Client::deleteClient( Client* c, allowed_t )
    {
    delete c;
    }

/*!
  Releases the window. The client has done its job and the window is still existing.
 */
void Client::releaseWindow( bool on_shutdown )
    {
    assert( !deleting );
    deleting = true;
    workspace()->discardUsedWindowRules( this, true ); // remove ForceTemporarily rules
    StackingUpdatesBlocker blocker( workspace());
    if (!custom_opacity) setOpacity(FALSE);
    if (moveResizeMode)
       leaveMoveResize();
    removeShadow();
    drawIntersectingShadows();
    finishWindowRules();
    ++postpone_geometry_updates;
    // grab X during the release to make removing of properties, setting to withdrawn state
    // and repareting to root an atomic operation (http://lists.kde.org/?l=kde-devel&m=116448102901184&w=2)
    grabXServer();
    setMappingState( WithdrawnState );
    setModal( false ); // otherwise its mainwindow wouldn't get focus
    hidden = true; // so that it's not considered visible anymore (can't use hideClient(), it would set flags)
    if( !on_shutdown )
        workspace()->clientHidden( this );
    XUnmapWindow( tqt_xdisplay(), frameId()); // destroying decoration would cause ugly visual effect
    destroyDecoration();
    cleanGrouping();
    if( !on_shutdown )
        {
        workspace()->removeClient( this, Allowed );
        // only when the window is being unmapped, not when closing down KWin
        // (NETWM sections 5.5,5.7)
        info->setDesktop( 0 );
        desk = 0;
        info->setState( 0, info->state()); // reset all state flags
        }
    XDeleteProperty( tqt_xdisplay(), client, atoms->kde_net_wm_user_creation_time);
    XDeleteProperty( tqt_xdisplay(), client, atoms->net_frame_extents );
    XDeleteProperty( tqt_xdisplay(), client, atoms->kde_net_wm_frame_strut );
    XReparentWindow( tqt_xdisplay(), client, workspace()->rootWin(), x(), y());
    XRemoveFromSaveSet( tqt_xdisplay(), client );
    XSelectInput( tqt_xdisplay(), client, NoEventMask );
    if( on_shutdown )
        { // map the window, so it can be found after another WM is started
        XMapWindow( tqt_xdisplay(), client );
	// TODO preserve minimized, shaded etc. state?
        }
    else
        {
        // Make sure it's not mapped if the app unmapped it (#65279). The app
        // may do map+unmap before we initially map the window by calling rawShow() from manage().
        XUnmapWindow( tqt_xdisplay(), client ); 
        }
    client = None;
    XDestroyWindow( tqt_xdisplay(), wrapper );
    wrapper = None;
    XDestroyWindow( tqt_xdisplay(), frame );
    frame = None;
    --postpone_geometry_updates; // don't use GeometryUpdatesBlocker, it would now set the geometry
    checkNonExistentClients();
    deleteClient( this, Allowed );
    ungrabXServer();
    }

// like releaseWindow(), but this one is called when the window has been already destroyed
// (e.g. the application closed it)
void Client::destroyClient()
    {
    assert( !deleting );
    deleting = true;
    workspace()->discardUsedWindowRules( this, true ); // remove ForceTemporarily rules
    StackingUpdatesBlocker blocker( workspace());
    if (moveResizeMode)
       leaveMoveResize();
    removeShadow();
    drawIntersectingShadows();
    finishWindowRules();
    ++postpone_geometry_updates;
    setModal( false );
    hidden = true; // so that it's not considered visible anymore
    workspace()->clientHidden( this );
    destroyDecoration();
    cleanGrouping();
    workspace()->removeClient( this, Allowed );
    client = None; // invalidate
    XDestroyWindow( tqt_xdisplay(), wrapper );
    wrapper = None;
    XDestroyWindow( tqt_xdisplay(), frame );
    frame = None;
    --postpone_geometry_updates; // don't use GeometryUpdatesBlocker, it would now set the geometry
    checkNonExistentClients();
    deleteClient( this, Allowed );
    }

void Client::updateDecoration( bool check_workspace_pos, bool force )
    {
    if( !force && (( decoration == NULL && noBorder())
                    || ( decoration != NULL && !noBorder())))
        return;
    bool do_show = false;
    postponeGeometryUpdates( true );
    if( force )
        destroyDecoration();
    if( !noBorder())
        {
        setMask( TQRegion()); // reset shape mask
        decoration = workspace()->createDecoration( bridge );
        // TODO check decoration's minimum size?
        decoration->init();
        decoration->widget()->installEventFilter( this );
        XReparentWindow( tqt_xdisplay(), decoration->widget()->winId(), frameId(), 0, 0 );
        decoration->widget()->lower();
        decoration->borders( border_left, border_right, border_top, border_bottom );
        options->onlyDecoTranslucent ?
            setDecoHashProperty(border_top, border_right, border_bottom, border_left):
            unsetDecoHashProperty();
        int save_workarea_diff_x = workarea_diff_x;
        int save_workarea_diff_y = workarea_diff_y;
        move( calculateGravitation( false ));
        plainResize( sizeForClientSize( clientSize()), ForceGeometrySet );
        workarea_diff_x = save_workarea_diff_x;
        workarea_diff_y = save_workarea_diff_y;
        do_show = true;
        }
    else
        destroyDecoration();
    if( check_workspace_pos )
        checkWorkspacePosition();
    postponeGeometryUpdates( false );
    if( do_show )
        decoration->widget()->show();
    updateFrameExtents();
    updateOpacityCache();
    }

void Client::destroyDecoration()
    {
    if( decoration != NULL )
        {
        delete decoration;
        decoration = NULL;
        TQPoint grav = calculateGravitation( true );
        border_left = border_right = border_top = border_bottom = 0;
        setMask( TQRegion()); // reset shape mask
        int save_workarea_diff_x = workarea_diff_x;
        int save_workarea_diff_y = workarea_diff_y;
        plainResize( sizeForClientSize( clientSize()), ForceGeometrySet );
        move( grav );
        workarea_diff_x = save_workarea_diff_x;
        workarea_diff_y = save_workarea_diff_y;
        }
    }

void Client::checkBorderSizes()
    {
    if( decoration == NULL )
        return;
    int new_left, new_right, new_top, new_bottom;
    decoration->borders( new_left, new_right, new_top, new_bottom );
    if( new_left == border_left && new_right == border_right
        && new_top == border_top && new_bottom == border_bottom )
        return;
    GeometryUpdatesPostponer blocker( this );
    move( calculateGravitation( true ));
    border_left = new_left;
    border_right = new_right;
    border_top = new_top;
    border_bottom = new_bottom;
    if (border_left != new_left ||
        border_right != new_right ||
        border_top != new_top ||
        border_bottom != new_bottom)
    options->onlyDecoTranslucent ?
       setDecoHashProperty(new_top, new_right, new_bottom, new_left):
       unsetDecoHashProperty();
    move( calculateGravitation( false ));
    plainResize( sizeForClientSize( clientSize()), ForceGeometrySet );
    checkWorkspacePosition();
    }

void Client::detectNoBorder()
    {
    if( Shape::hasShape( window()))
        {
        noborder = true;
        return;
        }
    switch( windowType())
        {
        case NET::Desktop :
        case NET::Dock :
        case NET::TopMenu :
        case NET::Splash :
            noborder = true;
          break;
        case NET::Unknown :
        case NET::Normal :
        case NET::Toolbar :
        case NET::Menu :
        case NET::Dialog :
        case NET::Utility :
            noborder = false;
          break;
        default:
            assert( false );
        }
    // NET::Override is some strange beast without clear definition, usually
    // just meaning "noborder", so let's treat it only as such flag, and ignore it as
    // a window type otherwise (SUPPORTED_WINDOW_TYPES_MASK doesn't include it)
    if( info->windowType( SUPPORTED_WINDOW_TYPES_MASK | NET::OverrideMask ) == NET::Override )
        noborder = true;
    }

void Client::detectShapable()
    {
    if( Shape::hasShape( window()))
        return;
    switch( windowType())
        {
        case NET::Desktop :
        case NET::Dock :
        case NET::TopMenu :
        case NET::Splash :
          break;
        case NET::Unknown :
        case NET::Normal :
        case NET::Toolbar :
        case NET::Menu :
        case NET::Dialog :
        case NET::Utility :
            setShapable(FALSE);
          break;
        default:
            assert( false );
        }
    }

void Client::updateFrameExtents()
    {
    NETStrut strut;
    strut.left = border_left;
    strut.right = border_right;
    strut.top = border_top;
    strut.bottom = border_bottom;
    info->setFrameExtents( strut );
    }

// Resizes the decoration, and makes sure the decoration widget gets resize event
// even if the size hasn't changed. This is needed to make sure the decoration
// re-layouts (e.g. when options()->moveResizeMaximizedWindows() changes,
// the decoration may turn on/off some borders, but the actual size
// of the decoration stays the same).
void Client::resizeDecoration( const TQSize& s )
    {
    if( decoration == NULL )
        return;
    TQSize oldsize = decoration->widget()->size();
    decoration->resize( s );
    if( oldsize == s )
        {
        TQResizeEvent e( s, oldsize );
        TQApplication::sendEvent( decoration->widget(), &e );
        }
    if (!moveResizeMode && options->shadowEnabled(isActive()))
        {
        // If the user is manually resizing, let Client::leaveMoveResize()
        // decide when to redraw the shadow
        updateOpacityCache();
        }
    }

bool Client::noBorder() const
    {
    return noborder || isFullScreen() || user_noborder || motif_noborder;
    }

bool Client::userCanSetNoBorder() const
    {
    return !noborder && !isFullScreen() && !isShade();
    }

bool Client::isUserNoBorder() const
    {
    return user_noborder;
    }

void Client::setUserNoBorder( bool set )
    {
    if( !userCanSetNoBorder())
        return;
    set = rules()->checkNoBorder( set );
    if( user_noborder == set )
        return;
    user_noborder = set;
    updateDecoration( true, false );
    updateWindowRules();
    }

bool Client::isModalSystemNotification() const
    {
    unsigned char *data = 0;
    Atom actual;
    int format, result;
    unsigned long n, left;
    result = XGetWindowProperty(tqt_xdisplay(), window(), atoms->net_wm_system_modal_notification, 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left, /*(unsigned char **)*/ &data);
    if (result == Success && data != None && format == 32 )
        {
        return TRUE;
        }
    return FALSE;
    }

void Client::updateShape()
    {
    // workaround for #19644 - shaped windows shouldn't have decoration
    if( shape() && !noBorder()) 
        {
        noborder = true;
        updateDecoration( true );
        }
    updateOpacityCache();
    if ( shape() )
        {
        XShapeCombineShape(tqt_xdisplay(), frameId(), ShapeBounding,
                           clientPos().x(), clientPos().y(),
                           window(), ShapeBounding, ShapeSet);
        setShapable(TRUE);
        }
    // !shape() mask setting is done in setMask() when the decoration
    // calls it or when the decoration is created/destroyed

    if( Shape::version() >= 0x11 ) // 1.1, has input shape support
        { // There appears to be no way to find out if a window has input
          // shape set or not, so always propagate the input shape
          // (it's the same like the bounding shape by default).
          // Also, build the shape using a helper window, not directly
          // in the frame window, because the sequence set-shape-to-frame,
          // remove-shape-of-client, add-input-shape-of-client has the problem
          // that after the second step there's a hole in the input shape
          // until the real shape of the client is added and that can make
          // the window lose focus (which is a problem with mouse focus policies)
        static Window helper_window = None;
        if( helper_window == None )
            helper_window = XCreateSimpleWindow( tqt_xdisplay(), tqt_xrootwin(),
                0, 0, 1, 1, 0, 0, 0 );
        XResizeWindow( tqt_xdisplay(), helper_window, width(), height());
        XShapeCombineShape( tqt_xdisplay(), helper_window, ShapeInput, 0, 0,
                           frameId(), ShapeBounding, ShapeSet );
        XShapeCombineShape( tqt_xdisplay(), helper_window, ShapeInput,
                           clientPos().x(), clientPos().y(),
                           window(), ShapeBounding, ShapeSubtract );
        XShapeCombineShape( tqt_xdisplay(), helper_window, ShapeInput,
                           clientPos().x(), clientPos().y(),
                           window(), ShapeInput, ShapeUnion );
        XShapeCombineShape( tqt_xdisplay(), frameId(), ShapeInput, 0, 0,
                           helper_window, ShapeInput, ShapeSet );
        }
    }

void Client::setMask( const TQRegion& reg, int mode )
    {
    _mask = reg;
    if( reg.isNull())
        XShapeCombineMask( tqt_xdisplay(), frameId(), ShapeBounding, 0, 0,
            None, ShapeSet );
    else if( mode == X::Unsorted )
        XShapeCombineRegion( tqt_xdisplay(), frameId(), ShapeBounding, 0, 0,
            reg.handle(), ShapeSet );
    else
        {
        TQMemArray< TQRect > rects = reg.rects();
        XRectangle* xrects = new XRectangle[ rects.count() ];
        for( unsigned int i = 0;
             i < rects.count();
             ++i )
            {
            xrects[ i ].x = rects[ i ].x();
            xrects[ i ].y = rects[ i ].y();
            xrects[ i ].width = rects[ i ].width();
            xrects[ i ].height = rects[ i ].height();
            }
        XShapeCombineRectangles( tqt_xdisplay(), frameId(), ShapeBounding, 0, 0,
            xrects, rects.count(), ShapeSet, mode );
        delete[] xrects;
        }
    updateShape();
    }

TQRegion Client::mask() const
    {
    if( _mask.isEmpty())
        return TQRegion( 0, 0, width(), height());
    return _mask;
    }
    
void Client::setShapable(bool b)
    {
    long tmp = b?1:0;
    XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_shapable, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &tmp, 1L);
    }

void Client::hideClient( bool hide )
    {
    if( hidden == hide )
        return;
    hidden = hide;
    updateVisibility();
    }
    
/*!
  Returns whether the window is minimizable or not
 */
bool Client::isMinimizable() const
    {
    if( isSpecialWindow())
        return false;
    if( isModalSystemNotification())
        return false;
    if( isTransient())
        { // #66868 - let other xmms windows be minimized when the mainwindow is minimized
        bool shown_mainwindow = false;
        ClientList mainclients = mainClients();
        for( ClientList::ConstIterator it = mainclients.begin();
             it != mainclients.end();
             ++it )
            {
            if( (*it)->isShown( true ))
                shown_mainwindow = true;
            }
        if( !shown_mainwindow )
            return true;
        }
    // this is here because kicker's taskbar doesn't provide separate entries
    // for windows with an explicitly given parent
    // TODO perhaps this should be redone
    if( transientFor() != NULL )
        return false;
    if( !wantsTabFocus()) // SELI - NET::Utility? why wantsTabFocus() - skiptaskbar? ?
        return false;
    return true;
    }

/*!
  Returns whether the window is kept above or not
 */
bool Client::keepAbove() const
    {
    if( isModalSystemNotification())
        return true;
    return keep_above;
    }

/*!
  Minimizes this client plus its transients
 */
void Client::minimize( bool avoid_animation )
    {
    if ( !isMinimizable() || isMinimized())
        return;

    if (isShade()) // NETWM restriction - KWindowInfo::isMinimized() == Hidden && !Shaded
        info->setState(0, NET::Shaded);

    Notify::raise( Notify::Minimize );

    // SELI mainClients().isEmpty() ??? - and in unminimize() too
    if ( mainClients().isEmpty() && isOnCurrentDesktop() && isShown( true ) && !avoid_animation )
        animateMinimizeOrUnminimize( true ); // was visible or shaded

    minimized = true;

    updateVisibility();
    updateAllowedActions();
    workspace()->updateMinimizedOfTransients( this );
    updateWindowRules();
    workspace()->updateFocusChains( this, Workspace::FocusChainMakeLast );
    }

void Client::unminimize( bool avoid_animation )
    {
    if (!queryUserSuspendedResume())
        return;

    if( !isMinimized())
        return;

    if (isShade()) // NETWM restriction - KWindowInfo::isMinimized() == Hidden && !Shaded
        info->setState(NET::Shaded, NET::Shaded);

    Notify::raise( Notify::UnMinimize );
    minimized = false;
    if( isOnCurrentDesktop() && isShown( true ))
        {
        if( mainClients().isEmpty() && !avoid_animation )
            animateMinimizeOrUnminimize( FALSE );
        }
    updateVisibility();
    updateAllowedActions();
    workspace()->updateMinimizedOfTransients( this );
    updateWindowRules();
    }

extern bool         blockAnimation;

void Client::animateMinimizeOrUnminimize( bool minimize )
    {
    if ( blockAnimation )
        return;
    if ( !options->animateMinimize )
        return;

    if( decoration != NULL && decoration->animateMinimize( minimize ))
        return; // decoration did it

    // the function is a bit tricky since it will ensure that an
    // animation action needs always the same time regardless of the
    // performance of the machine or the X-Server.

    float lf,rf,tf,bf,step;

    int speed = options->animateMinimizeSpeed;
    if ( speed > 10 )
        speed = 10;
    if ( speed < 0 )
        speed = 0;

    step = 40. * (11 - speed );

    NETRect r = info->iconGeometry();
    TQRect icongeom( r.pos.x, r.pos.y, r.size.width, r.size.height );
    if ( !icongeom.isValid() )
        return;

    TQPixmap pm = animationPixmap( minimize ? width() : icongeom.width() );

    TQRect before, after;
    if ( minimize ) 
        {
        before = TQRect( x(), y(), width(), pm.height() );
        after = TQRect( icongeom.x(), icongeom.y(), icongeom.width(), pm.height() );
        }
    else 
        {
        before = TQRect( icongeom.x(), icongeom.y(), icongeom.width(), pm.height() );
        after = TQRect( x(), y(), width(), pm.height() );
        }

    lf = (after.left() - before.left())/step;
    rf = (after.right() - before.right())/step;
    tf = (after.top() - before.top())/step;
    bf = (after.bottom() - before.bottom())/step;

    grabXServer();

    TQRect area = before;
    TQRect area2;
    TQPixmap pm2;

    TQTime t;
    t.start();
    float diff;

    TQPainter p ( workspace()->desktopWidget() );
    bool need_to_clear = FALSE;
    TQPixmap pm3;
    do 
        {
        if (area2 != area)
            {
            pm = animationPixmap( area.width() );
            pm2 = TQPixmap::grabWindow( tqt_xrootwin(), area.x(), area.y(), area.width(), area.height() );
            p.drawPixmap( area.x(), area.y(), pm );
            if ( need_to_clear ) 
                {
                p.drawPixmap( area2.x(), area2.y(), pm3 );
                need_to_clear = FALSE;
                }
            area2 = area;
            }
        XFlush(tqt_xdisplay());
        XSync( tqt_xdisplay(), FALSE );
        diff = t.elapsed();
        if (diff > step)
            diff = step;
        area.setLeft(before.left() + int(diff*lf));
        area.setRight(before.right() + int(diff*rf));
        area.setTop(before.top() + int(diff*tf));
        area.setBottom(before.bottom() + int(diff*bf));
        if (area2 != area ) 
            {
            if ( area2.intersects( area ) )
                p.drawPixmap( area2.x(), area2.y(), pm2 );
            else 
                { // no overlap, we can clear later to avoid flicker
                pm3 = pm2;
                need_to_clear = TRUE;
                }
            }
        } while ( t.elapsed() < step);
    if (area2 == area || need_to_clear )
        p.drawPixmap( area2.x(), area2.y(), pm2 );

    p.end();
    ungrabXServer();
    }


/*!
  The pixmap shown during (un)minimalization animation
 */
TQPixmap Client::animationPixmap( int w )
    {
    TQFont font = options->font(isActive());
    TQFontMetrics fm( font );
    TQPixmap pm( w, fm.lineSpacing() );
    pm.fill( options->color(Options::ColorTitleBar, isActive() || isMinimized() ) );
    TQPainter p( &pm );
    p.setPen(options->color(Options::ColorFont, isActive() || isMinimized() ));
    p.setFont(options->font(isActive()));
    p.drawText( pm.rect(), AlignLeft|AlignVCenter|SingleLine, caption() );
    return pm;
    }


bool Client::isShadeable() const
    {
    return !isSpecialWindow() && !noBorder();
    }

void Client::setShade( ShadeMode mode )
    {
    if( !isShadeable())
        return;
    if( isModalSystemNotification())
        return;
    mode = rules()->checkShade( mode );
    if( shade_mode == mode )
        return;
    bool was_shade = isShade();
    ShadeMode was_shade_mode = shade_mode;
    shade_mode = mode;
    if( was_shade == isShade())
        {
        if( decoration != NULL ) // decoration may want to update after e.g. hover-shade changes
            decoration->shadeChange();
        return; // no real change in shaded state
        }

    if( shade_mode == ShadeNormal )
        {
        if ( isShown( true ) && isOnCurrentDesktop())
                Notify::raise( Notify::ShadeUp );
        }
    else if( shade_mode == ShadeNone )
        {
        if( isShown( true ) && isOnCurrentDesktop())
                Notify::raise( Notify::ShadeDown );
        }

    assert( decoration != NULL ); // noborder windows can't be shaded
    GeometryUpdatesPostponer blocker( this );
    // decorations may turn off some borders when shaded
    decoration->borders( border_left, border_right, border_top, border_bottom );

    int as = options->animateShade? 10 : 1;
// TODO all this unmapping, resizing etc. feels too much duplicated from elsewhere
    if ( isShade()) 
        { // shade_mode == ShadeNormal
        // we're about to shade, texx xcompmgr to prepare
        long _shade = 1;
        XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_shade, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &_shade, 1L);
        // shade
        int h = height();
        shade_geometry_change = true;
        TQSize s( sizeForClientSize( TQSize( clientSize())));
        s.setHeight( border_top + border_bottom );
        XSelectInput( tqt_xdisplay(), wrapper, ClientWinMask ); // avoid getting UnmapNotify
        XUnmapWindow( tqt_xdisplay(), wrapper );
        XUnmapWindow( tqt_xdisplay(), client );
        XSelectInput( tqt_xdisplay(), wrapper, ClientWinMask | SubstructureNotifyMask );
        //as we hid the unmap event, xcompmgr didn't recognize the client wid has vanished, so we'll extra inform it        
        //done xcompmgr workaround
// FRAME       repaint( FALSE );
//        bool wasStaticContents = testWFlags( WStaticContents );
//        setWFlags( WStaticContents );
        int step = TQMAX( 4, QABS( h - s.height() ) / as )+1;
        do 
            {
            h -= step;
            XResizeWindow( tqt_xdisplay(), frameId(), s.width(), h );
            resizeDecoration( TQSize( s.width(), h ));
            TQApplication::syncX();
            } while ( h > s.height() + step );
//        if ( !wasStaticContents )
//            clearWFlags( WStaticContents );
        plainResize( s );
        shade_geometry_change = false;
        if( isActive())
            {
            if( was_shade_mode == ShadeHover )
                workspace()->activateNextClient( this );
            else
                workspace()->focusToNull();
            }
        // tell xcompmgr shade's done
        _shade = 2;
        XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_shade, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &_shade, 1L);    
        }
    else 
        {
        int h = height();
        shade_geometry_change = true;
        TQSize s( sizeForClientSize( clientSize()));
// FRAME       bool wasStaticContents = testWFlags( WStaticContents );
//        setWFlags( WStaticContents );
        int step = TQMAX( 4, QABS( h - s.height() ) / as )+1;
        do 
            {
            h += step;
            XResizeWindow( tqt_xdisplay(), frameId(), s.width(), h );
            resizeDecoration( TQSize( s.width(), h ));
            // assume a border
            // we do not have time to wait for X to send us paint events
// FRAME           repaint( 0, h - step-5, width(), step+5, TRUE);
            TQApplication::syncX();
            } while ( h < s.height() - step );
//        if ( !wasStaticContents )
//            clearWFlags( WStaticContents );
        shade_geometry_change = false;
        plainResize( s );
        if( shade_mode == ShadeHover || shade_mode == ShadeActivated )
            setActive( TRUE );
        XMapWindow( tqt_xdisplay(), wrapperId());
        XMapWindow( tqt_xdisplay(), window());
        XDeleteProperty (tqt_xdisplay(), client, atoms->net_wm_window_shade);
	if (options->shadowEnabled(false))
        {
    	    for (ClientList::ConstIterator it = transients().begin();
        	it != transients().end(); ++it)
        	{
            	    (*it)->removeShadow();
            	    (*it)->drawDelayedShadow();
                }
        }

        if ( isActive() )
            workspace()->requestFocus( this );
        }
    checkMaximizeGeometry();
    info->setState( (isShade() && !isMinimized()) ? NET::Shaded : 0, NET::Shaded );
    info->setState( isShown( false ) ? 0 : NET::Hidden, NET::Hidden );
    updateVisibility();
    updateAllowedActions();
    workspace()->updateMinimizedOfTransients( this );
    decoration->shadeChange();
    updateWindowRules();
    }

void Client::shadeHover()
    {
    setShade( ShadeHover );
    cancelShadeHover();
    }

void Client::cancelShadeHover()
    {
    delete shadeHoverTimer;
    shadeHoverTimer = 0;
    }

void Client::toggleShade()
    {
    // if the mode is ShadeHover or ShadeActive, cancel shade too
    setShade( shade_mode == ShadeNone ? ShadeNormal : ShadeNone );
    }

void Client::updateVisibility()
    {
    if( deleting )
        return;
    bool show = true;
    if( hidden )
        {
        setMappingState( IconicState );
        info->setState( NET::Hidden, NET::Hidden );
        setSkipTaskbar( true, false ); // also hide from taskbar
        rawHide();
        show = false;
        }
    else
        {
        setSkipTaskbar( original_skip_taskbar, false );
        }
    if( minimized )
        {
        setMappingState( IconicState );
        info->setState( NET::Hidden, NET::Hidden );
        rawHide();
        show = false;
        }
    if( show )
        info->setState( 0, NET::Hidden );
    if( !isOnCurrentDesktop())
        {
        setMappingState( IconicState );
        rawHide();
        show = false;
        }
    if( show )
        {
        bool belongs_to_desktop = false;
        for( ClientList::ConstIterator it = group()->members().begin();
             it != group()->members().end();
             ++it )
            if( (*it)->isDesktop())
                {
                belongs_to_desktop = true;
                break;
                }
        if( !belongs_to_desktop && workspace()->showingDesktop())
            workspace()->resetShowingDesktop( true );
        if( isShade())
            setMappingState( IconicState );
        else
            setMappingState( NormalState );
        rawShow();
        }
    }

void Client::setShadowed(bool shadowed)
{
    bool wasShadowed;

    wasShadowed = isShadowed();
    shadowMe = options->shadowEnabled(isActive()) ? shadowed : false;

    if (shadowMe) {
        if (!wasShadowed)
            drawShadow();
    }
    else {
        if (wasShadowed) {
            removeShadow();

            if (!activeOpacityCache.isNull())
                activeOpacityCache.resize(0);
            if (!inactiveOpacityCache.isNull())
                inactiveOpacityCache.resize(0);
        }
    }
}

void Client::updateOpacityCache()
{
    if (!activeOpacityCache.isNull())
        activeOpacityCache.resize(0);
    if (!inactiveOpacityCache.isNull())
        inactiveOpacityCache.resize(0);

    if (!moveResizeMode) {
        // If the user is manually resizing, let Client::finishMoveResize()
        // decide when to redraw the shadow
        removeShadow();
        drawIntersectingShadows();
        if (options->shadowEnabled(isActive()))
            drawDelayedShadow();
    }
}

/*!
   Redraw shadows that were previously occluding or occluded by this window,
   to avoid visual glitches.
 */
void Client::drawIntersectingShadows() {
    //Client *reshadowClient;
    TQRegion region;
    //TQPtrList<Client> reshadowClients;
    TQValueList<Client *> reshadowClients;
    TQValueListIterator<ShadowRegion> it;
    TQValueListIterator<Client *> it2;

    if (!options->shadowEnabled(false))
        // No point in redrawing overlapping/overlapped shadows if only the
        // active window has a shadow.
        return;

    region = shapeBoundingRegion;

    // Generate list of Clients whose shadows need to be redrawn. That is,
    // those that are currently intersecting or intersected by other windows or
    // shadows.
    for (it = shadowRegions.begin(); it != shadowRegions.end(); ++it)
        if ((isOnAllDesktops() || (*it).client->isOnCurrentDesktop()) &&
                !(*it).region.intersect(region).isEmpty())
            reshadowClients.append((*it).client);

    // Redraw shadows for each of the Clients in the list generated above
    for (it2 = reshadowClients.begin(); it2 != reshadowClients.end();
            ++it2) {
        (*it2)->removeShadow();
        (*it2)->drawDelayedShadow();
    }
}

/*!
   Redraw shadows that are above the current window in the stacking order.
   Furthermore, redraw them in the same order as they come in the stacking order
   from bottom to top.
 */
void Client::drawOverlappingShadows(bool waitForMe)
{
    Client *aClient;
    TQRegion region;
    TQValueList<Client *> reshadowClients;
    ClientList stacking_order;
    ClientList::ConstIterator it;
    TQValueListIterator<ShadowRegion> it2;
    TQValueListIterator<Client *> it3;

    if (!options->shadowEnabled(false))
        // No point in redrawing overlapping/overlapped shadows if only the
        // active window has a shadow.
        return;

    region = shapeBoundingRegion;

    stacking_order = workspace()->stackingOrder();
    for (it = stacking_order.fromLast(); it != stacking_order.end(); --it) {
        // Find the position of this window in the stacking order.
        if ((*it) == this)
            break;
    }
    ++it;
    while (it != stacking_order.end()) {
        if ((*it)->windowType() == NET::Dock) {
            // This function is only interested in windows whose shadows don't
            // have weird stacking rules.
            ++it;
            continue;
        }

        // Generate list of Clients whose shadows need to be redrawn. That is,
        // those that are currently overlapping or overlapped by other windows
        // or shadows. The list should be in order from bottom to top in the
        // stacking order.
        for (it2 = shadowRegions.begin(); it2 != shadowRegions.end(); ++it2) {
            if ((*it2).client == (*it)) {
                if ((isOnAllDesktops() || (*it2).client->isOnCurrentDesktop())
                        && !(*it2).region.intersect(region).isEmpty())
                    reshadowClients.append((*it2).client);
            }
        }
        ++it;
    }

    // Redraw shadows for each of the Clients in the list generated above
    for (it3 = reshadowClients.begin(); it3 != reshadowClients.end(); ++it3) {
        (*it3)->removeShadow();
        if (it3 == reshadowClients.begin()) {
            if (waitForMe)
                (*it3)->drawShadowAfter(this);
            else
                (*it3)->drawDelayedShadow();
        }
        else {
            --it3;
            aClient = (*it3);
            ++it3;
            (*it3)->drawShadowAfter(aClient);
        }
    }
}

/*!
   Draw shadow after some time has elapsed, to give recently exposed windows a
   chance to repaint before a shadow gradient is drawn over them.
 */
void Client::drawDelayedShadow()
{
    shadowDelayTimer->stop();
    shadowDelayTimer->start(SHADOW_DELAY, true);
}

/*!
   Draw shadow immediately after the specified Client's shadow finishes drawing.
 */
void Client::drawShadowAfter(Client *after)
{
    shadowAfterClient = after;
    connect(after, TQT_SIGNAL(shadowDrawn()), TQT_SLOT(drawShadow()));
}

/*!
   Draw a shadow under this window and XShape the shadow accordingly.
 */
void Client::drawShadow()
{
    Window shadows[2];
    XRectangle *shapes;
    int i, count, ordering;

    // If we are waiting for another Client's shadow to be drawn, stop waiting now
    if (shadowAfterClient != NULL) {
        disconnect(shadowAfterClient, TQT_SIGNAL(shadowDrawn()), this, TQT_SLOT(drawShadow()));
        shadowAfterClient = NULL;
    }

    if (!isOnCurrentDesktop())
        return;

    /* Store this window's ShapeBoundingRegion even if shadows aren't drawn for
     * this type of window. Otherwise, drawIntersectingShadows() won't update
     * properly when this window is moved/resized/hidden/closed.
     */
    shapes = XShapeGetRectangles(tqt_xdisplay(), frameId(), ShapeBounding,
            &count, &ordering);
    if (!shapes)
        // XShape extension not supported
        shapeBoundingRegion = TQRegion(x(), y(), width(), height());
    else {
        shapeBoundingRegion = TQRegion();
        for (i = 0; i < count; i++) {
            // Translate XShaped window into a TQRegion
            TQRegion shapeRectangle(shapes[i].x, shapes[i].y, shapes[i].width,
                    shapes[i].height);
            shapeBoundingRegion += shapeRectangle;
        }
        if (isShade())
            // Since XResize() doesn't change a window's XShape regions, ensure that
            // shapeBoundingRegion is not taller than the window's shaded height,
            // or the bottom shadow will appear to be missing
            shapeBoundingRegion &= TQRegion(0, 0, width(), height());
        shapeBoundingRegion.translate(x(), y());
    }

    if (!isShadowed() || hidden || isMinimized() ||
            maximizeMode() == MaximizeFull ||
            !options->shadowWindowType(windowType())) {
        XFree(shapes);

        // Tell whatever Clients are listening that this Client's shadow has been drawn.
        // It hasn't, but there's no sense waiting for something that won't happen.
        emit shadowDrawn();

        return;
    }

    removeShadow();

    TQMemArray<QRgb> pixelData;
    TQPixmap shadowPixmap;
    TQRect shadow;
    TQRegion exposedRegion;
    ShadowRegion shadowRegion;
    int thickness, xOffset, yOffset;

    thickness = options->shadowThickness(isActive());
    xOffset = options->shadowXOffset(isActive());
    yOffset = options->shadowYOffset(isActive());
    opacityCache = active? &activeOpacityCache : &inactiveOpacityCache;

    shadow.setRect(x() - thickness + xOffset, y() - thickness + yOffset,
            width() + thickness * 2, height() + thickness * 2);
    shadowPixmap.resize(shadow.size());

    // Create a fake drop-down shadow effect via blended Xwindows
    shadowWidget = new TQWidget(0, 0, (WFlags)(WStyle_Customize | WX11BypassWM));
    shadowWidget->setGeometry(shadow);
    XSelectInput(tqt_xdisplay(), shadowWidget->winId(),
            ButtonPressMask | ButtonReleaseMask | StructureNotifyMask);
    shadowWidget->installEventFilter(this);

    if (!shapes) {
        // XShape extension not supported
        exposedRegion = getExposedRegion(shapeBoundingRegion, shadow.x(),
                shadow.y(), shadow.width(), shadow.height(), thickness,
                xOffset, yOffset);
        shadowRegion.region = exposedRegion;
        shadowRegion.client = this;
        shadowRegions.append(shadowRegion);

        if (opacityCache->isNull())
            imposeRegionShadow(shadowPixmap, shapeBoundingRegion,
                    exposedRegion, thickness,
                    options->shadowOpacity(isActive()));
        else
            imposeCachedShadow(shadowPixmap, exposedRegion);
    }
    else {
        TQMemArray<TQRect> exposedRects;
        TQMemArray<TQRect>::Iterator it, itEnd;
        XRectangle *shadowShapes;

        exposedRegion = getExposedRegion(shapeBoundingRegion, shadow.x(),
                shadow.y(), shadow.width(), shadow.height(), thickness,
                xOffset, yOffset);
        shadowRegion.region = exposedRegion;
        shadowRegion.client = this;
        shadowRegions.append(shadowRegion);

        // XShape the shadow
        exposedRects = exposedRegion.rects();
        i = 0;
        itEnd = exposedRects.end();
        shadowShapes = new XRectangle[exposedRects.count()];
        for (it = exposedRects.begin(); it != itEnd; ++it) {
            shadowShapes[i].x = (*it).x();
            shadowShapes[i].y = (*it).y();
            shadowShapes[i].width = (*it).width();
            shadowShapes[i].height = (*it).height();
            i++;
        }
        XShapeCombineRectangles(tqt_xdisplay(), shadowWidget->winId(),
                ShapeBounding, -x() + thickness - xOffset,
                -y() + thickness - yOffset, shadowShapes, i, ShapeSet,
                Unsorted);
        delete [] shadowShapes;

        if (opacityCache->isNull())
            imposeRegionShadow(shadowPixmap, shapeBoundingRegion,
                    exposedRegion, thickness,
                    options->shadowOpacity(isActive()));
        else
            imposeCachedShadow(shadowPixmap, exposedRegion);
    }

    XFree(shapes);

    // Set the background pixmap
    //shadowPixmap.convertFromImage(shadowImage);
    shadowWidget->setErasePixmap(shadowPixmap);

    // Restack shadows under this window so that shadows drawn for a newly
    // focused (but not raised) window don't overlap any windows above it.
    if (isDock()) {
        ClientList stacking_order = workspace()->stackingOrder();
        for (ClientList::ConstIterator it = stacking_order.begin(); it != stacking_order.end(); ++it)
            if ((*it)->isDesktop())
                {
                ++it;
                shadows[0] = (*it)->frameId();
                shadows[1] = shadowWidget->winId();
                }
    }
    else {
        shadows[0] = frameId();
        if (shadowWidget != NULL)
            shadows[1] = shadowWidget->winId();
    }

    XRestackWindows(tqt_xdisplay(), shadows, 2);

    // Don't use TQWidget::show() so we don't confuse QEffects, thus causing
    // broken focus.
    XMapWindow(tqt_xdisplay(), shadowWidget->winId());

    // Tell whatever Clients are listening that this Client's shadow has been drawn.
    emit shadowDrawn();
}

/*!
   Remove shadow under this window.
 */
void Client::removeShadow()
{
    TQValueList<ShadowRegion>::Iterator it;

    shadowDelayTimer->stop();

    if (shadowWidget != NULL) {
        for (it = shadowRegions.begin(); it != shadowRegions.end(); ++it)
            if ((*it).client == this) {
                shadowRegions.remove(it);
                break;
            }
        delete shadowWidget;
        shadowWidget = NULL;
    }
}

/*!
   Calculate regions in which the shadow will be visible given the window's
   origin, height and width and the shadow's thickness, and X- and Y-offsets.
 */
TQRegion Client::getExposedRegion(TQRegion occludedRegion, int x, int y, int w,
        int h, int thickness, int xOffset, int yOffset)
{
    TQRegion exposedRegion;

    exposedRegion = TQRegion(x, y, w, h);
    exposedRegion -= occludedRegion;

    if (thickness > 0) {
        // Limit exposedRegion to include only where a shadow of the specified
        // thickness will be drawn
        TQMemArray<TQRect> occludedRects;
        TQMemArray<TQRect>::Iterator it, itEnd;
        TQRegion shadowRegion;

        occludedRects = occludedRegion.rects();
        itEnd = occludedRects.end();
        for (it = occludedRects.begin(); it != itEnd; ++it) {
            // Expand each of the occluded region's shape rectangles to contain
            // where a shadow of the specified thickness will be drawn. Create
            // a new TQRegion that contains the expanded occluded region
            it->setTop(it->top() - thickness + yOffset);
            it->setLeft(it->left() - thickness + xOffset);
            it->setRight(it->right() + thickness + xOffset);
            it->setBottom(it->bottom() + thickness + yOffset);
            shadowRegion += TQRegion(*it);
        }
        exposedRegion -= exposedRegion - shadowRegion;
    }

    return exposedRegion;
}

/*!
   Draw shadow gradient around this window using cached opacity values.
 */
void Client::imposeCachedShadow(TQPixmap &pixmap, TQRegion exposed)
{
    QRgb pixel;
    double opacity;
    int red, green, blue, pixelRed, pixelGreen, pixelBlue;
    int subW, subH, w, x, y, zeroX, zeroY;
    TQImage image;
    TQMemArray<TQRect>::Iterator it, itEnd;
    TQMemArray<TQRect> rectangles;
    TQPixmap subPixmap;
    Window rootWindow;
    int thickness, windowX, windowY, xOffset, yOffset;

    rectangles = exposed.rects();
    rootWindow = tqt_xrootwin();
    thickness = options->shadowThickness(isActive());
    windowX = this->x();
    windowY = this->y();
    xOffset = options->shadowXOffset(isActive());
    yOffset = options->shadowYOffset(isActive());
    options->shadowColour(isActive()).rgb(&red, &green, &blue);
    w = pixmap.width();

    itEnd = rectangles.end();
    for (it = rectangles.begin(); it != itEnd; ++it) {
        subW = (*it).width();
        subH = (*it).height();
        subPixmap = TQPixmap::grabWindow(rootWindow, (*it).x(), (*it).y(),
                subW, subH);
        zeroX = (*it).x() - windowX + thickness - xOffset;
        zeroY = (*it).y() - windowY + thickness - yOffset;
        image = subPixmap.convertToImage();

        for (x = 0; x < subW; x++) {
            for (y = 0; y < subH; y++) {
                opacity = (*(opacityCache))[(zeroY + y) * w + zeroX + x];
                pixel = image.pixel(x, y);
                pixelRed = tqRed(pixel);
                pixelGreen = tqGreen(pixel);
                pixelBlue = tqBlue(pixel);
                image.setPixel(x, y,
                        tqRgb((int)(pixelRed + (red - pixelRed) * opacity),
                            (int)(pixelGreen + (green - pixelGreen) * opacity),
                            (int)(pixelBlue + (blue - pixelBlue) * opacity)));
            }
        }

        subPixmap.convertFromImage(image);
        bitBlt(&pixmap, zeroX, zeroY, &subPixmap);
    }
}

/*!
   Draw shadow around this window using calculated opacity values.
 */
void Client::imposeRegionShadow(TQPixmap &pixmap, TQRegion occluded,
        TQRegion exposed, int thickness, double maxOpacity)
{
    register int distance, intersectCount, i, j, x, y;
    QRgb pixel;
    double decay, factor, opacity;
    int red, green, blue, pixelRed, pixelGreen, pixelBlue;
    int lineIntersects, maxIntersects, maxY;
    int irBottom, irLeft, irRight, irTop, yIncrement;
    int subW, subH, w, h, zeroX, zeroY;
    TQImage image;
    TQMemArray<TQRect>::Iterator it, itEnd;
    TQMemArray<TQRect> rectangles;
    TQPixmap subPixmap;
    Window rootWindow;
    int windowX, windowY, xOffset, yOffset;

    rectangles = exposed.rects();
    rootWindow = tqt_xrootwin();
    windowX = this->x();
    windowY = this->y();
    xOffset = options->shadowXOffset(isActive());
    yOffset = options->shadowYOffset(isActive());
    options->shadowColour(isActive()).rgb(&red, &green, &blue);
    maxIntersects = thickness * thickness * 4 + (thickness * 4) + 1;
    lineIntersects = thickness * 2 + 1;
    factor = maxIntersects / maxOpacity;
    decay = (lineIntersects / 0.0125 - factor) / pow((double)maxIntersects, 3.0);
    w = pixmap.width();
    h = pixmap.height();
    xOffset = options->shadowXOffset(isActive());
    yOffset = options->shadowYOffset(isActive());

    opacityCache->resize(0);
    opacityCache->resize(w * h);
    occluded.translate(-windowX + thickness, -windowY + thickness);

    itEnd = rectangles.end();
    for (it = rectangles.begin(); it != itEnd; ++it) {
        subW = (*it).width();
        subH = (*it).height();
        subPixmap = TQPixmap::grabWindow(rootWindow, (*it).x(), (*it).y(),
                subW, subH);
        maxY = subH;
        zeroX = (*it).x() - windowX + thickness - xOffset;
        zeroY = (*it).y() - windowY + thickness - yOffset;
        image = subPixmap.convertToImage();

        intersectCount = 0;
        opacity = -1;
        y = 0;
        yIncrement = 1;
        for (x = 0; x < subW; x++) {
            irLeft = zeroX + x - thickness;
            irRight = zeroX + x + thickness;

            while (y != maxY) {
                // horizontal row about to leave the intersect region, not
                // necessarily the top row
                irTop = zeroY + y - thickness * yIncrement;
                // horizontal row that just came into the intersect region,
                // not necessarily the bottom row
                irBottom = zeroY + y + thickness * yIncrement;

                if (opacity == -1) {
                    // If occluded pixels caused an intersect count to be
                    // skipped, recount it
                    intersectCount = 0;

                    for (j = irTop; j != irBottom; j += yIncrement) {
                        // irTop is not necessarily larger than irBottom and
                        // yIncrement isn't necessarily positive
                        for (i = irLeft; i <= irRight; i++) {
                            if (occluded.contains(TQPoint(i, j)))
                                intersectCount++;
                        }
                    }
                }
                else {
                    if (intersectCount < 0)
                        intersectCount = 0;

                    for (i = irLeft; i <= irRight; i++) {
                        if (occluded.contains(TQPoint(i, irBottom)))
                            intersectCount++;
                    }
                }

                distance = maxIntersects - intersectCount;
                opacity = intersectCount / (factor + pow((double)distance, 3.0) * decay);

                (*(opacityCache))[(zeroY + y) * w + zeroX + x] = opacity;
                pixel = image.pixel(x, y);
                pixelRed = tqRed(pixel);
                pixelGreen = tqGreen(pixel);
                pixelBlue = tqBlue(pixel);
                image.setPixel(x, y,
                        tqRgb((int)(pixelRed + (red - pixelRed) * opacity),
                            (int)(pixelGreen + (green - pixelGreen) * opacity),
                            (int)(pixelBlue + (blue - pixelBlue) * opacity)));

                for (i = irLeft; i <= irRight; i++) {
                    if (occluded.contains(TQPoint(i, irTop)))
                        intersectCount--;
                }

                y += yIncrement;
            }
            y -= yIncrement;

            irTop += yIncrement;
            for (j = irTop; j != irBottom; j += yIncrement) {
                if (occluded.contains(TQPoint(irLeft, j)))
                    intersectCount--;
            }
            irRight++;
            for (j = irTop; j != irBottom; j += yIncrement) {
                if (occluded.contains(TQPoint(irRight, j)))
                    intersectCount++;
            }

            yIncrement *= -1;
            if (yIncrement < 0)
                // Scan Y-axis bottom-up for next X-coordinate iteration
                maxY = -1;
            else
                // Scan Y-axis top-down for next X-coordinate iteration
                maxY = subH;
        }

        subPixmap.convertFromImage(image);
        bitBlt(&pixmap, zeroX, zeroY, &subPixmap);
    }
}

/*!
  Sets the client window's mapping state. Possible values are
  WithdrawnState, IconicState, NormalState.
 */
void Client::setMappingState(int s)
    {
    assert( client != None );
    assert( !deleting || s == WithdrawnState );
    if( mapping_state == s )
        return;
    bool was_unmanaged = ( mapping_state == WithdrawnState );
    mapping_state = s;
    if( mapping_state == WithdrawnState )
        {
        XDeleteProperty( tqt_xdisplay(), window(), tqt_wm_state );
        return;
        }
    assert( s == NormalState || s == IconicState );

    unsigned long data[2];
    data[0] = (unsigned long) s;
    data[1] = (unsigned long) None;
    XChangeProperty(tqt_xdisplay(), window(), tqt_wm_state, tqt_wm_state, 32,
        PropModeReplace, (unsigned char *)data, 2);

    if( was_unmanaged ) // manage() did postpone_geometry_updates = 1, now it's ok to finally set the geometry
        postponeGeometryUpdates( false );
    }

/*!
  Reimplemented to map the managed window in the window wrapper.
  Proper mapping state should be set before showing the client.
 */
void Client::rawShow()
    {
    if( decoration != NULL )
        decoration->widget()->show(); // not really necessary, but let it know the state
    XMapWindow( tqt_xdisplay(), frame );
    if( !isShade())
        {
        XMapWindow( tqt_xdisplay(), wrapper );
        XMapWindow( tqt_xdisplay(), client );
        }
    if (options->shadowEnabled(isActive()))
        drawDelayedShadow();
    }

/*!
  Reimplemented to unmap the managed window in the window wrapper.
  Also informs the workspace.
  Proper mapping state should be set before hiding the client.
*/
void Client::rawHide()
    {
// Here it may look like a race condition, as some other client might try to unmap
// the window between these two XSelectInput() calls. However, they're supposed to
// use XWithdrawWindow(), which also sends a synthetic event to the root window,
// which won't be missed, so this shouldn't be a problem. The chance the real UnmapNotify
// will be missed is also very minimal, so I don't think it's needed to grab the server
// here.
    removeShadow();
    drawIntersectingShadows();
    XSelectInput( tqt_xdisplay(), wrapper, ClientWinMask ); // avoid getting UnmapNotify
    XUnmapWindow( tqt_xdisplay(), frame );
    XUnmapWindow( tqt_xdisplay(), wrapper );
    XUnmapWindow( tqt_xdisplay(), client );
    XSelectInput( tqt_xdisplay(), wrapper, ClientWinMask | SubstructureNotifyMask );
    if( decoration != NULL )
        decoration->widget()->hide(); // not really necessary, but let it know the state
    workspace()->clientHidden( this );
    }

void Client::sendClientMessage(Window w, Atom a, Atom protocol, long data1, long data2, long data3)
    {
    XEvent ev;
    long mask;

    memset(&ev, 0, sizeof(ev));
    ev.xclient.type = ClientMessage;
    ev.xclient.window = w;
    ev.xclient.message_type = a;
    ev.xclient.format = 32;
    ev.xclient.data.l[0] = protocol;
    ev.xclient.data.l[1] = GET_QT_X_TIME();
    ev.xclient.data.l[2] = data1;
    ev.xclient.data.l[3] = data2;
    ev.xclient.data.l[4] = data3;
    mask = 0L;
    if (w == tqt_xrootwin())
      mask = SubstructureRedirectMask;        /* magic! */
    XSendEvent(tqt_xdisplay(), w, False, mask, &ev);
    }

/*
  Returns whether the window may be closed (have a close button)
 */
bool Client::isCloseable() const
    {
    if( isModalSystemNotification())
        return false;
    return rules()->checkCloseable( motif_may_close && !isSpecialWindow());
    }

/*!
  Closes the window by either sending a delete_window message or
  using XKill.
 */
void Client::closeWindow()
    {
    if( !isCloseable())
        return;
    // Update user time, because the window may create a confirming dialog.
    updateUserTime(); 
    if ( Pdeletewindow )
        {
        Notify::raise( Notify::Close );
        sendClientMessage( window(), atoms->wm_protocols, atoms->wm_delete_window);
        pingWindow();
        }
    else 
        {
        // client will not react on wm_delete_window. We have not choice
        // but destroy his connection to the XServer.
        killWindow();
        }
    }


/*!
  Kills the window via XKill
 */
void Client::killWindow()
    {
    kdDebug( 1212 ) << "Client::killWindow():" << caption() << endl;
    // not sure if we need an Notify::Kill or not.. until then, use
    // Notify::Close
    Notify::raise( Notify::Close );

    if( isDialog())
        Notify::raise( Notify::TransDelete );
    if( isNormalWindow())
        Notify::raise( Notify::Delete );
    killProcess( false );
    // always kill this client at the server
    XKillClient(tqt_xdisplay(), window() );
    destroyClient();
    }

// send a ping to the window using _NET_WM_PING if possible
// if it doesn't respond within a reasonable time, it will be
// killed
void Client::pingWindow()
    {
    if( !Pping )
        return; // can't ping :(
    if( options->killPingTimeout == 0 )
        return; // turned off
    if( ping_timer != NULL )
        return; // pinging already
    ping_timer = new TQTimer( this );
    connect( ping_timer, TQT_SIGNAL( timeout()), TQT_SLOT( pingTimeout()));
    ping_timer->start( options->killPingTimeout, true );
    ping_timestamp = GET_QT_X_TIME();
    workspace()->sendPingToWindow( window(), ping_timestamp );
    }

void Client::gotPing( Time timestamp )
    {
    // just plain compare is not good enough because of 64bit and truncating and whatnot
    if( NET::timestampCompare( timestamp, ping_timestamp ) != 0 )
        return;
    delete ping_timer;
    ping_timer = NULL;
    if( process_killer != NULL )
        {
        process_killer->kill();
        delete process_killer;
        process_killer = NULL;
        }
    }

void Client::pingTimeout()
    {
    kdDebug( 1212 ) << "Ping timeout:" << caption() << endl;
    delete ping_timer;
    ping_timer = NULL;
    killProcess( true, ping_timestamp );
    }

void Client::killProcess( bool ask, Time timestamp )
    {
    if( process_killer != NULL )
        return;
    Q_ASSERT( !ask || timestamp != CurrentTime );
    TQCString machine = wmClientMachine( true );
    pid_t pid = info->pid();
    if( pid <= 0 || machine.isEmpty()) // needed properties missing
        return;
    kdDebug( 1212 ) << "Kill process:" << pid << "(" << machine << ")" << endl;
    if( !ask )
        {
        if( machine != "localhost" )
            {
            TDEProcess proc;
            proc << "xon" << machine << "kill" << pid;
            proc.start( TDEProcess::DontCare );
            }
        else
            ::kill( pid, SIGTERM );
        }
    else
        { // SELI TODO handle the window created by handler specially (on top,urgent?)
        process_killer = new TDEProcess( this );
        *process_killer << TDEStandardDirs::findExe( "twin_killer_helper" )
            << "--pid" << TQCString().setNum( pid ) << "--hostname" << machine
            << "--windowname" << caption().utf8()
            << "--applicationname" << resourceClass()
            << "--wid" << TQCString().setNum( window())
            << "--timestamp" << TQCString().setNum( timestamp );
        connect( process_killer, TQT_SIGNAL( processExited( TDEProcess* )),
            TQT_SLOT( processKillerExited()));
        if( !process_killer->start( TDEProcess::NotifyOnExit ))
            {
            delete process_killer;
            process_killer = NULL;
            return;
            }
        }
    }

bool Client::isSuspendable() const
    {
    bool cansuspend = true;
    if( skipTaskbar() || skipPager() )
        return false;
    TQCString machine = wmClientMachine( true );
    pid_t pid = info->pid();
    if( pid <= 0 || machine.isEmpty()) // needed properties missing
        return false;
    kdDebug( 1212 ) << "Check suspendable process:" << pid << "(" << machine << ")" << endl;
    if( machine != "localhost" )
        {
        return false;
        }
    else
        {
        TQFile procStatFile(TQString("/proc/%1/stat").arg(pid));
        if (procStatFile.open(IO_ReadOnly))
            {
            TQByteArray statRaw = procStatFile.readAll();
            procStatFile.close();
            TQString statString(statRaw);
            TQStringList statFields = TQStringList::split(" ", statString, TRUE);
            TQString tcomm = statFields[1];
            TQString state = statFields[2];
            if( state != "T" )
                {
                // Make sure no windows of this process are special
                for ( ClientList::ConstIterator it = workspace()->clients.begin(); it != workspace()->clients.end(); ++it)
                    {
                    Client* nextclient = *it;
                    pid_t nextpid = nextclient->info->pid();
                    TQCString nextmachine = nextclient->wmClientMachine( true );
                    if( nextpid > 0 && (!nextmachine.isEmpty()))
                        {
                        if( ( nextmachine == "localhost" ) && ( pid == nextpid ) )
                            {
                            if( nextclient->skipTaskbar() || nextclient->skipPager() )
                                cansuspend = false;
                            }
                        }
                    }
                // Process exception list
                TQString execname(tcomm);
                execname.truncate(execname.length()-1);
                execname = execname.remove(0,1);
                // FIXME This list should not be hardcoded
                if( (execname == "kdesktop") || (execname == "kicker") )
                    return false;
                else
                    return cansuspend;
                }
            else
                {
                return false;
                }
            }
        else
            {
            return false;
            }
        }
    }

bool Client::isResumeable() const
    {
    TQCString machine = wmClientMachine( true );
    pid_t pid = info->pid();
    if( pid <= 0 || machine.isEmpty()) // needed properties missing
        return false;
    kdDebug( 1212 ) << "Check resumeable process:" << pid << "(" << machine << ")" << endl;
    if( machine != "localhost" )
        {
        return false;
        }
    else
        {
        TQFile procStatFile(TQString("/proc/%1/stat").arg(pid));
        if (procStatFile.open(IO_ReadOnly))
            {
            TQByteArray statRaw = procStatFile.readAll();
            procStatFile.close();
            TQString statString(statRaw);
            TQStringList statFields = TQStringList::split(" ", statString, TRUE);
            TQString tcomm = statFields[1];
            TQString state = statFields[2];
            if( state == "T" )
                {
                return true;
                }
            else
                {
                return false;
                }
            }
        else
            {
            return false;
            }
        }
    }

bool Client::queryUserSuspendedResume()
    {
        if (isResumeable())
            {
            if (process_resumer != NULL)
                {
                return false;
                }
            // FIXME We should display a busy cursor until twin_resumer_helper loads
            process_resumer = new TDEProcess( this );
            *process_resumer << TDEStandardDirs::findExe( "twin_resumer_helper" )
                << "--pid" << TQCString().setNum( info->pid() ) << "--hostname" << wmClientMachine( true )
                << "--windowname" << caption().utf8()
                << "--applicationname" << resourceClass()
                << "--wid" << TQCString().setNum( window());
            connect( process_resumer, TQT_SIGNAL( processExited( TDEProcess* )),
                TQT_SLOT( processResumerExited()));
            if( !process_resumer->start( TDEProcess::NotifyOnExit ))
                {
                delete process_resumer;
                process_resumer = NULL;
                return true;
                }
            return false;
            }
        else
            {
            return true;
            }
    }

void Client::suspendWindow()
    {
    TQCString machine = wmClientMachine( true );
    pid_t pid = info->pid();
    if( pid <= 0 || machine.isEmpty()) // needed properties missing
        return;
    kdDebug( 1212 ) << "Suspend process:" << pid << "(" << machine << ")" << endl;
    if( machine != "localhost" )
        {
        return;
        }
    else
        {
        for ( ClientList::ConstIterator it = workspace()->clients.begin(); it != workspace()->clients.end(); ++it)
            {
            Client* nextclient = *it;
            pid_t nextpid = nextclient->info->pid();
            TQCString nextmachine = nextclient->wmClientMachine( true );
            if( nextpid > 0 && (!nextmachine.isEmpty()))
                {
                if( ( nextmachine == "localhost" ) && ( pid == nextpid ) )
                    {
                    TQString newCaption = TQString(readName()).append(" <").append(i18n("Suspended")).append(">");
                    nextclient->info->setVisibleName(newCaption.utf8());
                    nextclient->info->setVisibleIconName(newCaption.utf8());
                    nextclient->minimized_before_suspend = nextclient->isMinimized();
                    nextclient->minimize(true);
                    }
                }
            }
        ::kill( pid, SIGSTOP );
        }
    }

void Client::resumeWindow()
    {
    TQCString machine = wmClientMachine( true );
    pid_t pid = info->pid();
    if( pid <= 0 || machine.isEmpty()) // needed properties missing
        return;
    kdDebug( 1212 ) << "Resume process:" << pid << "(" << machine << ")" << endl;
    if( machine != "localhost" )
        {
        return;
        }
    else
        {
        ::kill( pid, SIGCONT );
        for ( ClientList::ConstIterator it = workspace()->clients.begin(); it != workspace()->clients.end(); ++it)
            {
            Client* nextclient = *it;
            pid_t nextpid = nextclient->info->pid();
            TQCString nextmachine = nextclient->wmClientMachine( true );
            if( nextpid > 0 && (!nextmachine.isEmpty()))
                {
                if( ( nextmachine == "localhost" ) && ( pid == nextpid ) )
                    {
                        if (!nextclient->minimized_before_suspend)
                        {
                        nextclient->unminimize(true);
                        }
                    nextclient->updateCaption();
                    }
                }
            }
        }
    }

void Client::processKillerExited()
    {
    kdDebug( 1212 ) << "Killer exited" << endl;
    delete process_killer;
    process_killer = NULL;
    }

void Client::processResumerExited()
    {
    kdDebug( 1212 ) << "Resumer exited" << endl;
    // 0 means the user clicked Resume; 2 means that the resumer dialog failed to launch somehow
    if ((process_resumer->exitStatus() == 0) || (process_resumer->exitStatus() == 2))
        {
        resumeWindow();
        takeFocus( Allowed );
        }
    delete process_resumer;
    process_resumer = NULL;
    }

void Client::setSkipTaskbar( bool b, bool from_outside )
    {
    int was_wants_tab_focus = wantsTabFocus();
    if( from_outside )
        {
        b = rules()->checkSkipTaskbar( b );
        original_skip_taskbar = b;
        }
    if ( b == skipTaskbar() )
        return;
    skip_taskbar = b;
    info->setState( b?NET::SkipTaskbar:0, NET::SkipTaskbar );
    updateWindowRules();
    if( was_wants_tab_focus != wantsTabFocus())
        workspace()->updateFocusChains( this,
            isActive() ? Workspace::FocusChainMakeFirst : Workspace::FocusChainUpdate );
    }

void Client::setSkipPager( bool b )
    {
    b = rules()->checkSkipPager( b );
    if ( b == skipPager() )
        return;
    skip_pager = b;
    info->setState( b?NET::SkipPager:0, NET::SkipPager );
    updateWindowRules();
    }

void Client::setModal( bool m )
    { // Qt-3.2 can have even modal normal windows :(
    if( modal == m )
        return;
    modal = m;
    if( !modal )
        return;
    // changing modality for a mapped window is weird (?)
    // _NET_WM_STATE_MODAL should possibly rather be _NET_WM_WINDOW_TYPE_MODAL_DIALOG
    }

void Client::setDesktop( int desktop )
    {
    if( desktop != NET::OnAllDesktops ) // do range check
        desktop = KMAX( 1, KMIN( workspace()->numberOfDesktops(), desktop ));
    desktop = rules()->checkDesktop( desktop );
    if( desk == desktop )
        return;
    int was_desk = desk;
    desk = desktop;
    info->setDesktop( desktop );
    if(( was_desk == NET::OnAllDesktops ) != ( desktop == NET::OnAllDesktops ))
        { // onAllDesktops changed
        if ( isShown( true ))
            Notify::raise( isOnAllDesktops() ? Notify::OnAllDesktops : Notify::NotOnAllDesktops );
        workspace()->updateOnAllDesktopsOfTransients( this );
        }
    if( decoration != NULL )
        decoration->desktopChange();
    workspace()->updateFocusChains( this, Workspace::FocusChainMakeFirst );
    updateVisibility();
    updateWindowRules();
    }

void Client::setOnAllDesktops( bool b )
    {
    if(( b && isOnAllDesktops())
        || ( !b && !isOnAllDesktops()))
        return;
    if( b )
        setDesktop( NET::OnAllDesktops );
    else
        setDesktop( workspace()->currentDesktop());
    }

bool Client::isOnCurrentDesktop() const
    {
    return isOnDesktop( workspace()->currentDesktop());
    }

int Client::screen() const
    {
    if( !options->xineramaEnabled )
        return 0;
    return workspace()->screenNumber( geometry().center());
    }

bool Client::isOnScreen( int screen ) const
    {
    if( !options->xineramaEnabled )
        return screen == 0;
    return workspace()->screenGeometry( screen ).intersects( geometry());
    }

// performs activation and/or raising of the window
void Client::takeActivity( int flags, bool handled, allowed_t )
    {
    if( !handled || !Ptakeactivity )
        {
        if( flags & ActivityFocus )
            takeFocus( Allowed );
        if( flags & ActivityRaise )
            workspace()->raiseClient( this );
        return;
        }

#ifndef NDEBUG
    static Time previous_activity_timestamp;
    static Client* previous_client;
    if( previous_activity_timestamp == GET_QT_X_TIME() && previous_client != this )
        {
        kdDebug( 1212 ) << "Repeated use of the same X timestamp for activity" << endl;
        kdDebug( 1212 ) << kdBacktrace() << endl;
        }
    previous_activity_timestamp = GET_QT_X_TIME();
    previous_client = this;
#endif
    workspace()->sendTakeActivity( this, GET_QT_X_TIME(), flags );
    }

// performs the actual focusing of the window using XSetInputFocus and WM_TAKE_FOCUS
void Client::takeFocus( allowed_t )
    {
#ifndef NDEBUG
    static Time previous_focus_timestamp;
    static Client* previous_client;
    if( previous_focus_timestamp == GET_QT_X_TIME() && previous_client != this )
        {
        kdDebug( 1212 ) << "Repeated use of the same X timestamp for focus" << endl;
        kdDebug( 1212 ) << kdBacktrace() << endl;
        }
    previous_focus_timestamp = GET_QT_X_TIME();
    previous_client = this;
#endif
    if ( rules()->checkAcceptFocus( input ))
        {
        XSetInputFocus( tqt_xdisplay(), window(), RevertToPointerRoot, GET_QT_X_TIME() );
        }
    if ( Ptakefocus )
        sendClientMessage(window(), atoms->wm_protocols, atoms->wm_take_focus);
    workspace()->setShouldGetFocus( this );
    }

/*!
  Returns whether the window provides context help or not. If it does,
  you should show a help menu item or a help button like '?' and call
  contextHelp() if this is invoked.

  \sa contextHelp()
 */
bool Client::providesContextHelp() const
    {
    if (isModalSystemNotification())
        return false;
    return Pcontexthelp;
    }


/*!
  Invokes context help on the window. Only works if the window
  actually provides context help.

  \sa providesContextHelp()
 */
void Client::showContextHelp()
    {
    if ( Pcontexthelp ) 
        {
        sendClientMessage(window(), atoms->wm_protocols, atoms->net_wm_context_help);
        TQWhatsThis::enterWhatsThisMode(); // SELI?
        }
    }


/*!
  Fetches the window's caption (WM_NAME property). It will be
  stored in the client's caption().
 */
void Client::fetchName()
    {
    setCaption( readName());
    }

TQString Client::readName() const
    {
    if ( info->name() && info->name()[ 0 ] != '\0' ) 
        return TQString::fromUtf8( info->name() );
    else 
        return KWin::readNameProperty( window(), XA_WM_NAME );
    }
    
KWIN_COMPARE_PREDICATE( FetchNameInternalPredicate, const Client*, (!cl->isSpecialWindow() || cl->isToolbar()) && cl != value && cl->caption() == value->caption());

void Client::setCaption( const TQString& s, bool force )
    {
    if ( s != cap_normal || force ) 
        {
        bool reset_name = force;
        for( unsigned int i = 0;
             i < s.length();
             ++i )
            if( !s[ i ].isPrint())
                s[ i ] = ' ';
        cap_normal = s;
        bool was_suffix = ( !cap_suffix.isEmpty());
        TQString machine_suffix;
        if( wmClientMachine( false ) != "localhost" && !isLocalMachine( wmClientMachine( false )))
            machine_suffix = " <@" + wmClientMachine( true ) + ">";
        TQString shortcut_suffix = !shortcut().isNull() ? ( " {" + shortcut().toString() + "}" ) : "";
        cap_suffix = machine_suffix + shortcut_suffix;
        if ( ( !isSpecialWindow() || isToolbar()) && workspace()->findClient( FetchNameInternalPredicate( this ))) 
            {
            int i = 2;
            do 
                {
                cap_suffix = machine_suffix + " <" + TQString::number(i) + ">" + shortcut_suffix;
                i++;
                } while ( workspace()->findClient( FetchNameInternalPredicate( this )));
            info->setVisibleName( caption().utf8() );
            reset_name = false;
            }
        if(( (was_suffix && cap_suffix.isEmpty())
            || reset_name )) // if it was new window, it may have old value still set, if the window is reused
            {
            info->setVisibleName( "" ); // remove
            info->setVisibleIconName( "" ); // remove
            }
        else if( !cap_suffix.isEmpty() && !cap_iconic.isEmpty()) // keep the same suffix in iconic name if it's set
            info->setVisibleIconName( ( cap_iconic + cap_suffix ).utf8() );

        if( isManaged() && decoration != NULL )
                decoration->captionChange();
        }
    }

void Client::updateCaption()
    {
    setCaption( cap_normal, true );
    }

void Client::fetchIconicName()
    {
    TQString s;
    if ( info->iconName() && info->iconName()[ 0 ] != '\0' ) 
        s = TQString::fromUtf8( info->iconName() );
    else 
        s = KWin::readNameProperty( window(), XA_WM_ICON_NAME );
    if ( s != cap_iconic ) 
        {
	bool was_set = !cap_iconic.isEmpty();
        cap_iconic = s;
        if( !cap_suffix.isEmpty())
	    {
	    if( !cap_iconic.isEmpty()) // keep the same suffix in iconic name if it's set
        	info->setVisibleIconName( ( s + cap_suffix ).utf8() );
	    else if( was_set )
		info->setVisibleIconName( "" ); //remove
	    }
        }
    }

/*!\reimp
 */
TQString Client::caption( bool full ) const
    {
    return full ? cap_normal + cap_suffix : cap_normal;
    }

void Client::getWMHints()
    {
    XWMHints *hints = XGetWMHints(tqt_xdisplay(), window() );
    input = true;
    window_group = None;
    urgency = false;
    if ( hints )
        {
        if( hints->flags & InputHint )
            input = hints->input;
        if( hints->flags & WindowGroupHint )
            window_group = hints->window_group;
        urgency = ( hints->flags & UrgencyHint ) ? true : false; // true/false needed, it's uint bitfield
        XFree( (char*)hints );
        }
    checkGroup();
    updateUrgency();
    updateAllowedActions(); // group affects isMinimizable()
    }

void Client::getMotifHints()
    {
    bool mnoborder, mresize, mmove, mminimize, mmaximize, mclose;
    Motif::readFlags( client, mnoborder, mresize, mmove, mminimize, mmaximize, mclose );
    motif_noborder = mnoborder;
    if( !hasNETSupport()) // NETWM apps should set type and size constraints
        {
        motif_may_resize = mresize; // this should be set using minsize==maxsize, but oh well
        motif_may_move = mmove;
        }
    else
        motif_may_resize = motif_may_move = true;
    // mminimize; - ignore, bogus - e.g. shading or sending to another desktop is "minimizing" too
    // mmaximize; - ignore, bogus - maximizing is basically just resizing
    motif_may_close = mclose; // motif apps like to crash when they set this hint and WM closes them anyway
    if( isManaged())
        updateDecoration( true ); // check if noborder state has changed
    }

void Client::readIcons( Window win, TQPixmap* icon, TQPixmap* miniicon )
    {    
    // get the icons, allow scaling
    if( icon != NULL )
        *icon = KWin::icon( win, 32, 32, TRUE, KWin::NETWM | KWin::WMHints );
    if( miniicon != NULL )
        {
        if( icon == NULL || !icon->isNull())
            *miniicon = KWin::icon( win, 16, 16, TRUE, KWin::NETWM | KWin::WMHints );
        else
            *miniicon = TQPixmap();
        }
    }

void Client::getIcons()
    {
    // first read icons from the window itself
    readIcons( window(), &icon_pix, &miniicon_pix );
    if( icon_pix.isNull())
        { // then try window group
        icon_pix = group()->icon();
        miniicon_pix = group()->miniIcon();
        }
    if( icon_pix.isNull() && isTransient())
        { // then mainclients
        ClientList mainclients = mainClients();
        for( ClientList::ConstIterator it = mainclients.begin();
             it != mainclients.end() && icon_pix.isNull();
             ++it )
            {
            icon_pix = (*it)->icon();
            miniicon_pix = (*it)->miniIcon();
            }
        }
    if( icon_pix.isNull())
        { // and if nothing else, load icon from classhint or xapp icon
        icon_pix = KWin::icon( window(), 32, 32, TRUE, KWin::ClassHint | KWin::XApp );
        miniicon_pix = KWin::icon( window(), 16, 16, TRUE, KWin::ClassHint | KWin::XApp );
        }
    if( isManaged() && decoration != NULL )
        decoration->iconChange();
    }

void Client::getWindowProtocols()
    {
    Atom *p;
    int i,n;

    Pdeletewindow = 0;
    Ptakefocus = 0;
    Ptakeactivity = 0;
    Pcontexthelp = 0;
    Pping = 0;

    if (XGetWMProtocols(tqt_xdisplay(), window(), &p, &n))
        {
        for (i = 0; i < n; i++)
            if (p[i] == atoms->wm_delete_window)
                Pdeletewindow = 1;
            else if (p[i] == atoms->wm_take_focus)
                Ptakefocus = 1;
            else if (p[i] == atoms->net_wm_take_activity)
                Ptakeactivity = 1;
            else if (p[i] == atoms->net_wm_context_help)
                Pcontexthelp = 1;
            else if (p[i] == atoms->net_wm_ping)
                Pping = 1;
        if (n>0)
            XFree(p);
        }
    }

static int nullErrorHandler(Display *, XErrorEvent *)
    {
    return 0;
    }

/*!
  Returns WM_WINDOW_ROLE property for a given window.
 */
TQCString Client::staticWindowRole(WId w)
    {
    return getStringProperty(w, tqt_window_role).lower();
    }

/*!
  Returns SM_CLIENT_ID property for a given window.
 */
TQCString Client::staticSessionId(WId w)
    {
    return getStringProperty(w, tqt_sm_client_id);
    }

/*!
  Returns WM_COMMAND property for a given window.
 */
TQCString Client::staticWmCommand(WId w)
    {
    return getStringProperty(w, XA_WM_COMMAND, ' ');
    }

/*!
  Returns WM_CLIENT_LEADER property for a given window.
 */
Window Client::staticWmClientLeader(WId w)
    {
    Atom type;
    int format, status;
    unsigned long nitems = 0;
    unsigned long extra = 0;
    unsigned char *data = 0;
    Window result = w;
    XErrorHandler oldHandler = XSetErrorHandler(nullErrorHandler);
    status = XGetWindowProperty( tqt_xdisplay(), w, atoms->wm_client_leader, 0, 10000,
                                 FALSE, XA_WINDOW, &type, &format,
                                 &nitems, &extra, &data );
    XSetErrorHandler(oldHandler);
    if (status  == Success ) 
        {
        if (data && nitems > 0)
            result = *((Window*) data);
        XFree(data);
        }
    return result;
    }


void Client::getWmClientLeader()
    {
    wmClientLeaderWin = staticWmClientLeader(window());
    }

/*!
  Returns sessionId for this client,
  taken either from its window or from the leader window.
 */
TQCString Client::sessionId()
    {
    TQCString result = staticSessionId(window());
    if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
        result = staticSessionId(wmClientLeaderWin);
    return result;
    }

/*!
  Returns command property for this client,
  taken either from its window or from the leader window.
 */
TQCString Client::wmCommand()
    {
    TQCString result = staticWmCommand(window());
    if (result.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
        result = staticWmCommand(wmClientLeaderWin);
    return result;
    }

void Client::getWmClientMachine()
    {
    client_machine = getStringProperty(window(), XA_WM_CLIENT_MACHINE);
    if( client_machine.isEmpty() && wmClientLeaderWin && wmClientLeaderWin!=window())
        client_machine = getStringProperty(wmClientLeaderWin, XA_WM_CLIENT_MACHINE);
    if( client_machine.isEmpty())
        client_machine = "localhost";
    }

/*!
  Returns client machine for this client,
  taken either from its window or from the leader window.
*/
TQCString Client::wmClientMachine( bool use_localhost ) const
    {
    TQCString result = client_machine;
    if( use_localhost )
        { // special name for the local machine (localhost)
        if( result != "localhost" && isLocalMachine( result ))
            result = "localhost";
        }
    return result;
    }

/*!
  Returns client leader window for this client.
  Returns the client window itself if no leader window is defined.
*/
Window Client::wmClientLeader() const
    {
    if (wmClientLeaderWin)
        return wmClientLeaderWin;
    return window();
    }

bool Client::wantsTabFocus() const
    {
    return ( isNormalWindow() || isDialog()) && wantsInput() && !skip_taskbar;
    }


bool Client::wantsInput() const
    {
    return rules()->checkAcceptFocus( input || Ptakefocus );
    }

bool Client::isDesktop() const
    {
    return windowType() == NET::Desktop;
    }

bool Client::isDock() const
    {
    return windowType() == NET::Dock;
    }

bool Client::isTopMenu() const
    {
    return windowType() == NET::TopMenu;
    }


bool Client::isMenu() const
    {
    return windowType() == NET::Menu && !isTopMenu(); // because of backwards comp.
    }

bool Client::isToolbar() const
    {
    return windowType() == NET::Toolbar;
    }

bool Client::isSplash() const
    {
    return windowType() == NET::Splash;
    }

bool Client::isUtility() const
    {
    return windowType() == NET::Utility;
    }

bool Client::isDialog() const
    {
    return windowType() == NET::Dialog;
    }

bool Client::isNormalWindow() const
    {
    return windowType() == NET::Normal;
    }

bool Client::isSpecialWindow() const
    {
    return isDesktop() || isDock() || isSplash() || isTopMenu()
        || isToolbar(); // TODO
    }

NET::WindowType Client::windowType( bool direct, int supported_types ) const
    {
    NET::WindowType wt = info->windowType( supported_types );
    if( direct )
        return wt;
    NET::WindowType wt2 = rules()->checkType( wt );
    if( wt != wt2 )
        {
        wt = wt2;
        info->setWindowType( wt ); // force hint change
        }
    // hacks here
    if( wt == NET::Menu )
        {
        // ugly hack to support the times when NET::Menu meant NET::TopMenu
        // if it's as wide as the screen, not very high and has its upper-left
        // corner a bit above the screen's upper-left cornet, it's a topmenu
        if( x() == 0 && y() < 0 && y() > -10 && height() < 100
            && abs( width() - workspace()->clientArea( FullArea, this ).width()) < 10 )
            wt = NET::TopMenu;
        }
    // TODO change this to rule
    const char* const oo_prefix = "openoffice.org"; // TQCString has no startsWith()
    // oo_prefix is lowercase, because resourceClass() is forced to be lowercase
    if( tqstrncmp( resourceClass(), oo_prefix, strlen( oo_prefix )) == 0 && wt == NET::Dialog )
        wt = NET::Normal; // see bug #66065
    if( wt == NET::Unknown ) // this is more or less suggested in NETWM spec
        wt = isTransient() ? NET::Dialog : NET::Normal;
    return wt;
    }

/*!
  Sets an appropriate cursor shape for the logical mouse position \a m

 */
void Client::setCursor( Position m )
    {
    if( !isResizable() || isShade())
        {
        m = PositionCenter;
        }
    switch ( m ) 
        {
        case PositionTopLeft:
        case PositionBottomRight:
            setCursor( tqsizeFDiagCursor );
            break;
        case PositionBottomLeft:
        case PositionTopRight:
            setCursor( tqsizeBDiagCursor );
            break;
        case PositionTop:
        case PositionBottom:
            setCursor( tqsizeVerCursor );
            break;
        case PositionLeft:
        case PositionRight:
            setCursor( tqsizeHorCursor );
            break;
        default:
            if( buttonDown && isMovable())
                setCursor( tqsizeAllCursor );
            else
                setCursor( tqarrowCursor );
            break;
        }
    }

// TODO mit nejake checkCursor(), ktere se zavola v manage() a pri vecech, kdy by se kurzor mohl zmenit?
// TRANSLATION: TODO: have a checkCursor() function, which is called both in manage() and in cases where the cursor might change
void Client::setCursor( const TQCursor& c )
    {
    if( c.handle() == cursor.handle())
        return;
    cursor = c;
    if( decoration != NULL )
        decoration->widget()->setCursor( cursor );
    XDefineCursor( tqt_xdisplay(), frameId(), cursor.handle());
    }

Client::Position Client::mousePosition( const TQPoint& p ) const
    {
    if( decoration != NULL )
        return decoration->mousePosition( p );
    return PositionCenter;
    }

void Client::updateAllowedActions( bool force )
    {
    if( !isManaged() && !force )
        return;
    unsigned long old_allowed_actions = allowed_actions;
    allowed_actions = 0;
    if( isMovable())
        allowed_actions |= NET::ActionMove;
    if( isResizable())
        allowed_actions |= NET::ActionResize;
    if( isMinimizable())
        allowed_actions |= NET::ActionMinimize;
    if( isShadeable())
        allowed_actions |= NET::ActionShade;
    // sticky state not supported
    if( isMaximizable())
        allowed_actions |= NET::ActionMax;
    if( userCanSetFullScreen())
        allowed_actions |= NET::ActionFullScreen;
    allowed_actions |= NET::ActionChangeDesktop; // always (pagers shouldn't show Docks etc.)
    if( isCloseable())
        allowed_actions |= NET::ActionClose;
    if( old_allowed_actions == allowed_actions )
        return;
    // TODO this could be delayed and compressed - it's only for pagers etc. anyway
    info->setAllowedActions( allowed_actions );
    // TODO this should also tell the decoration, so that it can update the buttons
    }

void Client::autoRaise()
    {
    workspace()->raiseClient( this );
    cancelAutoRaise();
    }
    
void Client::cancelAutoRaise()
    {
    delete autoRaiseTimer;
    autoRaiseTimer = 0;
    }

void Client::setOpacity(bool translucent, uint opacity)
    {
    if (isDesktop())
        return; // xcompmgr does not like non solid desktops and the user could set it accidently by mouse scrolling
//     tqWarning("setting opacity for %d",tqt_xdisplay());
    //rule out activated translulcency with 100% opacity
    if (!translucent || opacity ==  0xFFFFFFFF)
        {
        opacity_ = 0xFFFFFFFF;
        XDeleteProperty (tqt_xdisplay(), frameId(), atoms->net_wm_window_opacity);
        XDeleteProperty (tqt_xdisplay(), window(), atoms->net_wm_window_opacity); // ??? frameId() is necessary for visible changes, window() is the winId() that would be set by apps - we set both to be sure the app knows what's currently displayd
        }
    else{
        if(opacity == opacity_)
            return;
        opacity_ = opacity;
        long data = opacity; // 32bit XChangeProperty needs long
        XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &data, 1L);
        XChangeProperty(tqt_xdisplay(), window(), atoms->net_wm_window_opacity, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &data, 1L);
        }
    }
    
void Client::setShadowSize(uint shadowSize)
    {
    // ignoring all individual settings - if we control a window, we control it's shadow
    // TODO somehow handle individual settings for docks (besides custom sizes)
    long data = shadowSize;
    XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_shadow, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &data, 1L);
    }
        
void Client::updateOpacity()
// extra syncscreen flag allows to avoid double syncs when active state changes (as it will usually change for two windows)
    {
    if (!(isNormalWindow() || isDialog() || isUtility() )|| custom_opacity)
        return;
    if (isActive())
        {
        if( ruleOpacityActive() )
            setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
        else
            setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
        if (isBMP())
        // beep-media-player, only undecorated windows (gtk2 xmms, xmms doesn't work with compmgr at all - s.e.p. :P )
            {
            ClientList tmpGroupMembers = group()->members();
            ClientList activeGroupMembers;
            activeGroupMembers.append(this);
            tmpGroupMembers.remove(this);
            ClientList::Iterator it = tmpGroupMembers.begin();
            while (it != tmpGroupMembers.end())
            // search for next attached and not activated client and repeat if found
                {
                if ((*it) != this && (*it)->isBMP())
                // potential "to activate" client found
                    {
//                     tqWarning("client found");
                    if ((*it)->touches(this)) // first test, if the new client touches the just activated one
                        {
//                         tqWarning("found client touches me");
                        if( ruleOpacityActive() )
                            (*it)->setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
                        else
                            (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
//                         tqWarning("activated, search restarted (1)");
                        (*it)->setShadowSize(options->activeWindowShadowSize);
                        activeGroupMembers.append(*it);
                        tmpGroupMembers.remove(it);
                        it = tmpGroupMembers.begin(); // restart, search next client
                        continue;
                        }
                    else
                        { // pot. client does not touch c, so we have to search if it touches some other activated client
                        bool found = false;
                        for( ClientList::ConstIterator it2 = activeGroupMembers.begin(); it2 != activeGroupMembers.end(); it2++ )
                            {
                            if ((*it2) != this && (*it2) != (*it) && (*it)->touches(*it2))
                                {
//                                 tqWarning("found client touches other active client");
                                if( ruleOpacityActive() )
                                    (*it)->setOpacity(rule_opacity_active < 0xFFFFFFFF, rule_opacity_active);
                                else
                                    (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
                                (*it)->setShadowSize(options->activeWindowShadowSize);
                                activeGroupMembers.append(*it);
                                tmpGroupMembers.remove(it);
                                it = tmpGroupMembers.begin(); // reset potential client search
                                found = true;
//                                 tqWarning("activated, search restarted (2)");
                                break; // skip this loop
                                }
                            }
                        if (found) continue;
                        }
                    }
                    it++;
                }
            }
        else if (isNormalWindow())
        // activate dependend minor windows as well
            {
            for( ClientList::ConstIterator it = group()->members().begin(); it != group()->members().end(); it++ )
                if ((*it)->isDialog() || (*it)->isUtility())
                    {
                    if( (*it)->ruleOpacityActive() )
                        (*it)->setOpacity((*it)->ruleOpacityActive() < 0xFFFFFFFF, (*it)->ruleOpacityActive());
                    else
                        (*it)->setOpacity(options->translucentActiveWindows, options->activeWindowOpacity);
                    }
            }
        }
    else
        {
        if( ruleOpacityInactive() )
            setOpacity(rule_opacity_inactive < 0xFFFFFFFF, rule_opacity_inactive);
        else
            setOpacity(options->translucentInactiveWindows && !(keepAbove() && options->keepAboveAsActive),
                    options->inactiveWindowOpacity);
        // deactivate dependend minor windows as well
        if (isBMP())
        // beep-media-player, only undecorated windows (gtk2 xmms, xmms doesn't work with compmgr at all - s.e.p. :P )
            {
            ClientList tmpGroupMembers = group()->members();
            ClientList inactiveGroupMembers;
            inactiveGroupMembers.append(this);
            tmpGroupMembers.remove(this);
            ClientList::Iterator it = tmpGroupMembers.begin();
            while ( it != tmpGroupMembers.end() )
            // search for next attached and not activated client and repeat if found
                {
                if ((*it) != this && (*it)->isBMP())
                // potential "to activate" client found
                    {
//                     tqWarning("client found");
                    if ((*it)->touches(this)) // first test, if the new client touches the just activated one
                        {
//                         tqWarning("found client touches me");
                        if( (*it)->ruleOpacityInactive() )
                            (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
                        else
                            (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
                        (*it)->setShadowSize(options->inactiveWindowShadowSize);
//                         tqWarning("deactivated, search restarted (1)");
                        inactiveGroupMembers.append(*it);
                        tmpGroupMembers.remove(it);
                        it = tmpGroupMembers.begin(); // restart, search next client
                        continue;
                        }
                    else // pot. client does not touch c, so we have to search if it touches some other activated client
                        {
                        bool found = false;
                        for( ClientList::ConstIterator it2 = inactiveGroupMembers.begin(); it2 != inactiveGroupMembers.end(); it2++ )
                            {
                            if ((*it2) != this && (*it2) != (*it) && (*it)->touches(*it2))
                                {
//                                 tqWarning("found client touches other inactive client");
                                if( (*it)->ruleOpacityInactive() )
                                    (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
                                else
                                    (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
                                (*it)->setShadowSize(options->inactiveWindowShadowSize);
//                                 tqWarning("deactivated, search restarted (2)");
                                inactiveGroupMembers.append(*it);
                                tmpGroupMembers.remove(it);
                                it = tmpGroupMembers.begin(); // reset potential client search
                                found = true;
                                break; // skip this loop
                                }
                            }
                            if (found) continue;
                        }
                    }
                    it++;
                }
            }
        else if (isNormalWindow())
            {
            for( ClientList::ConstIterator it = group()->members().begin(); it != group()->members().end(); it++ )
                if ((*it)->isUtility()) //don't deactivate dialogs...
                    {
                    if( (*it)->ruleOpacityInactive() )
                        (*it)->setOpacity((*it)->ruleOpacityInactive() < 0xFFFFFFFF, (*it)->ruleOpacityInactive());
                    else
                        (*it)->setOpacity(options->translucentInactiveWindows && !((*it)->keepAbove() && options->keepAboveAsActive), options->inactiveWindowOpacity);
                    }
            }
        }
    }
    
void Client::updateShadowSize()
// extra syncscreen flag allows to avoid double syncs when active state changes (as it will usually change for two windows)
    {
    if (!(isNormalWindow() || isDialog() || isUtility() ))
        return;
    if (isActive())
        setShadowSize(options->activeWindowShadowSize);
    else
        setShadowSize(options->inactiveWindowShadowSize);
    }

uint Client::ruleOpacityInactive()
    {
    return rule_opacity_inactive;// != 0 ;
    }

uint Client::ruleOpacityActive()
    {
    return rule_opacity_active;// != 0;
    }
    
bool Client::getWindowOpacity() //query translucency settings from X, returns true if window opacity is set
    {
    unsigned char *data = 0;
    Atom actual;
    int format, result;
    unsigned long n, left;
    result = XGetWindowProperty(tqt_xdisplay(), window(), atoms->net_wm_window_opacity, 0L, 1L, False, XA_CARDINAL, &actual, &format, &n, &left, /*(unsigned char **)*/ &data);
    if (result == Success && data != None && format == 32 )
        {
        opacity_ = *reinterpret_cast< long* >( data );
        custom_opacity = true;
//         setOpacity(opacity_ < 0xFFFFFFFF, opacity_);
        XFree ((char*)data);
        return TRUE;
        }
    return FALSE;
    }
    
void Client::setCustomOpacityFlag(bool custom)
    {
    custom_opacity = custom;
    }
    
uint Client::opacity()
    {
    return opacity_;
    }

int Client::opacityPercentage()
    {
    return int(100*((double)opacity_/0xffffffff));
    }
    
bool Client::touches(const Client* c)
// checks if this client borders c, needed to test beep media player window state
    {
    if (y() == c->y() + c->height()) // this bottom to c
        return TRUE;
    if (y() + height() == c->y()) // this top to c
        return TRUE;
    if (x() == c->x() + c->width()) // this right to c
        return TRUE;
    if (x() + width() == c->x()) // this left to c
        return TRUE;
    return FALSE;
    }
    
void Client::setDecoHashProperty(uint topHeight, uint rightWidth, uint bottomHeight, uint leftWidth)
{
   long data = (topHeight < 255 ? topHeight : 255) << 24 |
               (rightWidth < 255 ? rightWidth : 255) << 16 |
               (bottomHeight < 255 ? bottomHeight : 255) << 8 |
               (leftWidth < 255 ? leftWidth : 255);
    XChangeProperty(tqt_xdisplay(), frameId(), atoms->net_wm_window_decohash, XA_CARDINAL, 32, PropModeReplace, (unsigned char *) &data, 1L);
}

void Client::unsetDecoHashProperty()
{
   XDeleteProperty( tqt_xdisplay(), frameId(), atoms->net_wm_window_decohash);
}
    
#ifndef NDEBUG
kdbgstream& operator<<( kdbgstream& stream, const Client* cl )
    {
    if( cl == NULL )
        return stream << "\'NULL_CLIENT\'";
    return stream << "\'ID:" << cl->window() << ";WMCLASS:" << cl->resourceClass() << ":" << cl->resourceName() << ";Caption:" << cl->caption() << "\'";
    }
kdbgstream& operator<<( kdbgstream& stream, const ClientList& list )
    {
    stream << "LIST:(";
    bool first = true;
    for( ClientList::ConstIterator it = list.begin();
         it != list.end();
         ++it )
        {
        if( !first )
            stream << ":";
        first = false;
        stream << *it;
        }
    stream << ")";
    return stream;
    }
kdbgstream& operator<<( kdbgstream& stream, const ConstClientList& list )
    {
    stream << "LIST:(";
    bool first = true;
    for( ConstClientList::ConstIterator it = list.begin();
         it != list.end();
         ++it )
        {
        if( !first )
            stream << ":";
        first = false;
        stream << *it;
        }
    stream << ")";
    return stream;
    }
#endif

TQPixmap * twin_get_menu_pix_hack()
    {
    static TQPixmap p;
    if ( p.isNull() )
        p = SmallIcon( "bx2" );
    return &p;
    }

} // namespace

#include "client.moc"