Source

xwem / lisp / xwem-frame.el

   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
;;; xwem-frame.el -- Frames ops for XWEM.

;; Copyright (C) 2003 by Free Software Foundation, Inc.

;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;;         Steve Youngs  <steve@youngs.au.com>
;; Created: 21 Mar 2003
;; Keywords: xlib, xwem
;; X-CVS: $Id$

;; This file is part of XWEM.

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

;; XWEM is distributed in the hope that it will be useful, but WITHOUT
;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
;; or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public
;; License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: Not in FSF

;;; Commentary

;; This file contain operations on XWEM frames.
;;

;;; Code

(require 'xlib-xlib)
(require 'xlib-xinerama)

(require 'xwem-load)
(require 'xwem-misc)

;;; Variables
(defgroup xwem-frame nil
  "Group to customize xwem frames."
  :prefix "xwem-frame-"
  :prefix "xwem-face-"
  :group 'xwem)

(defcustom xwem-frame-background "gray60"
  "*Frame background color."
  :type 'color
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-shape '(X-XC-top_left_arrow)
  "*Cursors shape which will be used when pointer is over xwem frame."
  :type (xwem-cursor-shape-choice)
  :set (xwem-cus-set-cursor-shape xwem-frame-cursor
         (mapcar 'xwem-frame-xwin (xwem-frames-list)))
  :initialize 'custom-initialize-default
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-foreground-color "#111111"
  "*Cursor's foreground color used when pointer is on xwem's frame."
  :type 'color
  :set (xwem-cus-set-cursor-foreground xwem-frame-cursor)
  :initialize 'custom-initialize-default
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-background-color "#EEEEEE"
  "*Cursor's background color used when pointer is on xwem's frame."
  :type 'color
  :set (xwem-cus-set-cursor-background xwem-frame-cursor)
  :initialize 'custom-initialize-default
  :group 'xwem-frame)

(defconst xwem-frame-builtin-properties
  '(inner-border-width outer-border-width title-height title-thickness)
  "List of valid builtin frame properties.")

;;;###autoload
(defcustom xwem-frame-default-properties
  (list 'inner-border-width 3		; Internal border of xwem's frame
        'inner-border-thickness 1       ; Internal border thickness
	'outer-border-width 0		; X border
        'background "gray60"            ; background color
	'title-height 18)
  "*Default properties list for xwem frames."
  :type '(restricted-sexp :match-alternatives (valid-plist-p))
  :group 'xwem-frame)

;;;###autoload
(defcustom xwem-embedded-frame-default-properties
  (list 'inner-border-width 0
        'inner-border-thickness 0
        'outer-border-width 0)
  "*Default properties for embedded xwem frames.
Values in `xwem-embedded-frame-default-properties' overrides values in
`xwem-frame-default-properties'."
  :type '(restricted-sexp :match-alternatives (valid-plist-p))
  :group 'xwem-frame)

(defcustom xwem-frame-on-delim-resize-mode 'normal
  "*Mode to outline current window when doing `xwem-frame-on-delim-resize'."
  :type '(choice (const :tag "Normal border" normal)
                 (const :tag "Opaque" opaque)
                 )
  :group 'xwem-frame)

(defcustom xwem-frame-iresize-mode 'normal
  "*Default type of drawing outlines when resizing frame interactively.
Opaque mode can do unexpected things, such as eat your food from
refrigerator, so set it to Opaque on your own risk."
  :type '(choice (const :tag "Normal border" normal)
                 (const :tag "Contiguous borders" contiguous)
                 (const :tag "Outline Corners" corners)
                 (const :tag "Grid" grid)
                 (const :tag "Opaque" opaque)
                 )
  :group 'xwem-frame)

(defcustom xwem-frame-imove-mode 'normal
  "*Default type of drawing outlines when moving frame interactively."
  :type '(choice (const :tag "Normal border" normal)
                 (const :tag "Contiguous border" contiguous)
                 (const :tag "Outline Corners" corners)
                 (const :tag "Grid" grid)
                 (const :tag "Opaque" opaque)
                 )
  :group 'xwem-frame)

(defcustom xwem-frame-imoveresize-mode-function
  'xwem-frame-imoveresize-mode-function-default
  "Function to call in way to select move or resize mode.
It is passed with two arguments - FRAME and WHAT.
Where FRAME is frame which is about to move/resize and WHAT is one of 'resize or 'move.
It should return one of:
 'normal     - Normal resize/move mode, just outline frame rectangle.
 'contiguous - Butified 'normal mode.
 'corners    - Outline frame corners.
 'grid       - Outline frame and draw grid inside.
 'opaque     - Opaque move/resize mode."
  :type 'function
  :group 'xwem-frame)

(defcustom xwem-frame-imoveresize-use-minibuffer t
  "*If non-nil, frame's geometry will be show while imove/iresize."
  :type 'boolean
  :group 'xwem-frame)

(defun xwem-frame-imoveresize-mode-function-default (frame what)
  "Default value of `xwem-frame-imoveresize-mode-function'.
Return `xwem-frame-iresize-mode' if WHAT is 'resize.
Return `xwem-frame-imove-mode' if WHAT is 'move."
  (if (eq what 'resize)
      xwem-frame-iresize-mode
    xwem-frame-imove-mode))
  
(defcustom xwem-frame-rolling-switch t
  "*Non-nil mean that \\<xwem-global-map>\\[xwem-frame-next] and
\\<xwem-global-map>\\[xwem-frame-previous] will always switch, even if
there no next or previous frame."
  :type 'boolean
  :group 'xwem-frame)

(defcustom xwem-frame-autoiconify-mode nil
  "Non-nil mean frame possible automatically iconifies when switching.
If switching from OFR to NFR frame values mean next:
 nil       - No frame will be iconified.
 intersect - OFR will be iconified if it intersects with NFR.
 always    - OFR always iconfied."
  :type '(choice (const :tag "Disabled" nil)
                 (conts :tag "Intersect" intersect)
                 (const :tag "Always" always))
  :group 'xwem-frame)

(defcustom xwem-frame-autoselect-embedded t
  "*Non-nil mean if embedded frame selected as client, also select frame."
  :type 'boolean
  :group 'xwem-frame)

;; Hooks
(defcustom xwem-frame-select-hook nil
  "*Hooks to call when new frame just selected."
  :type 'hook
  :group 'xwem-frame)

(defcustom xwem-frame-deselect-hook nil
  "*Hooks to call when selected frame is about to be deselected."
  :type 'hook
  :group 'xwem-frame)

(defcustom xwem-frame-creation-hook nil
  "Hooks called with one argument - frame, when frame just created."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-frame-change-hook nil
  "Hooks called with one argument - frame, when frame changed."
  :type 'hook
  :group 'xwem-hooks)
  
(defcustom xwem-frame-destroy-hook nil
  "Hooks called with one argument - frame, when frame destroyed."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-frame-resize-hook nil
  "Hooks called with one argument - frame, when frame resized."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-frame-move-hook nil
  "Hooks called with one argument - frame, when frame moved."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-frame-redraw-hook nil
  "Hooks called with one argument - frame, when frame redrawed."
  :type 'hook
  :group 'xwem-hooks)
  
;;; Internal variables

(defconst xwem-frame-ev-mask
  (Xmask-or XM-Exposure
            XM-StructureNotify
            XM-SubstructureRedirect
            XM-SubstructureNotify
            XM-KeyPress XM-ButtonPress XM-ButtonRelease
            XM-ResizeRedirect)
  "Events mask for xwem's frame.")

(defvar xwem-frame-cursor nil
  "Cursor used for xwem frame.")

(defvar xwem-frame-types 
  '(desktop embedded embedded-desktop dedicated)
  "List of xwem frame types.")

;;;###xwem-autoload
(defvar xwem-frames-list nil
  "List of all xwem frames.")

(defvar xwem-frame-noredisplay nil
  "If this non-nil than frame it reffers should not be redisplayed.
Internal variable, do not use.")

(defvar xwem-frame-dumped-config nil)


;;;###xwem-autoload
(defun xwem-frames-list (&optional type)
  "Return list of xwem frames of TYPE.
Default TYPE is 'desktop."
  (unless type
    (setq type 'desktop))
  (remove* nil xwem-frames-list
           :test (lambda (f1 f2)
                   (not (eq (xwem-frame-type f2) type)))))

;;; Frame macros
(defmacro xwem-frame-link-insert-after (frame1 frame2)
  "Make FRAME2 to be after FRAME1."
  `(let ((nf (xwem-frame-link-next ,frame1)))
     (when (xwem-frame-p nf)
       (setf (xwem-frame-link-prev nf) ,frame2))
     (setf (xwem-frame-link-next ,frame1) ,frame2)
     (setf (xwem-frame-link-prev ,frame2) ,frame1)
     (setf (xwem-frame-link-next ,frame2) nf)))

(defmacro xwem-frame-link-insert-before (frame1 frame2)
  "Make FRAME2 to be before FRAME1."
  `(let ((pf (xwem-frame-link-prev ,frame1)))
     (when (xwem-frame-p pf)
       (setf (xwem-frame-link-next pf) ,frame2))
     (setf (xwem-frame-link-prev ,frame1) ,frame2)
     (setf (xwem-frame-link-next ,frame2) ,frame1)
     (setf (xwem-frame-link-prev ,frame2) pf)))

(defmacro xwem-frame-link-remove (frame)
  "Remove FRAME from linkage."
  `(let ((nfr (xwem-frame-link-next ,frame))
	 (pfr (xwem-frame-link-prev ,frame)))
     (when (xwem-frame-p pfr)
       (setf (xwem-frame-link-next pfr) nfr))
     (when (xwem-frame-p nfr)
       (setf (xwem-frame-link-prev nfr) pfr))))

(defmacro xwem-frame-link-head (frame)
  "Returns head frame of FRAME's linkage."
  `(let ((fr ,frame))
     (while (xwem-frame-p (xwem-frame-link-prev fr))
       (setq fr (xwem-frame-link-prev fr)))
     fr))

(defmacro xwem-frame-linkage-map (frame fn)
  "Call FN for each frame in FRAME's linkage.
FN called with one argument - frame."
  ;; TODO: avoid infinit recursion
  `(let ((fr (xwem-frame-link-head ,frame)))

     (while (xwem-frame-p fr)
       (funcall ,fn fr)
       (setq fr (xwem-frame-link-next fr)))))

;;; Functions
(define-xwem-deffered xwem-frame-apply-state (frame)
  "Apply FRAME's state to life."
  (cond ((eq (xwem-frame-state frame) 'mapped)
         (XMapWindow (xwem-dpy) (xwem-frame-xwin frame)))
        ((eq (xwem-frame-state frame) 'unmapped)
         (XUnmapWindow (xwem-dpy) (xwem-frame-xwin frame)))))
         
(defun xwem-frame-unmap (frame)
  "Unmap frame FRAME."
  (setf (xwem-frame-state frame) 'unmapped)
  (xwem-frame-apply-state frame))

;;;###xwem-autoload
(defun xwem-frame-map (frame)
  "Map frame FRAME."
  (setf (xwem-frame-state frame) 'mapped)
  (xwem-frame-apply-state frame))

(define-xwem-deffered xwem-frame-apply-raise-lower (frame)
  "Apply FRAME's raise/lower state to life."
  (let ((rl (xwem-frame-get-prop frame 'raise-lower-state)))
    (cond ((eq rl 'raise)
           (XRaiseWindow (xwem-dpy) (xwem-frame-xwin frame)))
          ((eq rl 'lower)
           (XLowerWindow (xwem-dpy) (xwem-frame-xwin frame))))))

;;;###autoload(autoload 'xwem-frame-lower "xwem-frame" "" t)
(define-xwem-command xwem-frame-lower (frame)
  "Lower FRAME's window."
  (xwem-interactive (list (xwem-frame-selected)))

  (xwem-frame-put-prop frame 'raise-lower-state 'lower)
  (xwem-frame-apply-raise-lower frame))
(put 'xwem-frame-lower 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-raise "xwem-frame" "" t)
(define-xwem-command xwem-frame-raise (frame)
  "Raise FRAME's window."
  (xwem-interactive (list (xwem-frame-selected)))

  (xwem-frame-map frame)                ; make sure frame is mapped
  (xwem-frame-put-prop frame 'raise-lower-state 'raise)
  (xwem-frame-apply-raise-lower frame))
(put 'xwem-frame-raise 'xwem-frame-command t)

(defun xwem-frame-embedded-for-frame (frame)
  "Return XWEM frame for which FRAME is embedded."
  (let* ((cl (and (xwem-frame-p frame)
                  (xwem-frame-get-prop frame 'xwem-embedded-cl)))
	 (rv (and (xwem-cl-p cl)
		  (xwem-cl-frame cl))))
    rv))

(defun xwem-frame-unembedd (frame &optional new-type)
  "Unembedd FRAME."
  (when (xwem-frame-embedded-p frame)
    (let* ((cl (xwem-frame-get-prop frame 'xwem-embedded-cl))
           (tpnt (car (XTranslateCoordinates (xwem-dpy) (xwem-frame-xwin frame)
                                             (xwem-rootwin) (xwem-frame-x frame)
                                             (xwem-frame-y frame)))))
      ;; Remove clients stuff
      (xwem-cl-destroy cl)

      ;; Unmark FRAME as embedded
      (xwem-cl-rem-prop cl 'xwem-embedded-frame)
      (xwem-frame-rem-prop frame 'xwem-embedded-cl)

      ;; Set new frame TYPE
      (setf (xwem-frame-type frame) (or new-type 'desktop))

      (XReparentWindow (xwem-dpy) (xwem-frame-xwin frame) (xwem-rootwin)
                       (X-Point-x tpnt) (X-Point-y tpnt))
      (xwem-frame-set-pos frame (X-Point-x tpnt) (X-Point-y tpnt))
      (xwem-frame-apply-state frame)
      )))

;;;###xwem-autoload
(defun xwem-select-frame (frame)
  "Set FRAME to be selected frame.
Actually all the work done in `xwem-select-window'."
  (or (xwem-frame-alive-p frame)
      (error 'xwem-error "Selecting dead frame"))
  (xwem-select-window (xwem-frame-selwin frame)))

(defun xwem-frame-autoiconify-on-deselect ()
  "Maybe iconify FRAME, when deselecting FRAME."
  (when (eq xwem-frame-autoiconify-mode 'always)
    (xwem-frame-unmap (xwem-frame-selected))))

(defun xwem-frame-autoiconify-on-select ()
  "Maybe iconify some frames when selecting FRAME."
  (when (eq xwem-frame-autoiconify-mode 'intersect)
    (let ((nfr-rect (X-Geom-to-X-Rect (xwem-frame-xgeom (xwem-frame-selected))))
          (frames (xwem-frames-list)))
      (while frames
        (when (and (not (eq (xwem-frame-selected) (car frames)))
                   (xwem-frame-mapped-p (car frames))
                   (X-Rect-intersect-p nfr-rect (X-Geom-to-X-Rect (xwem-frame-xgeom (car frames)))))
          (xwem-frame-unmap (car frames)))
        (setq frames (cdr frames))))))

;;;###xwem-autoload(autoload 'xwem-make-frame-1 "xwem-frame")
(defun* xwem-make-frame-1 (type &key params props noselect)
  "Create new frame with optional frame properties PROPS.
If EMBEDDED-p is non-nil than create embedded frame.
If NOSELECT is non-nil then do not select newly created frame to be
current."
  (let* ((fplist (copy-list xwem-frame-default-properties))
	 (frame (apply 'make-xwem-frame params))
         fwin parwin)
    
    (setf (xwem-frame-type frame) type)
    (setf (xwem-frame-state frame) 'unmapped)

    ;;; Initialise FRAME's geometry
    (unless (xwem-frame-xgeom frame)
      (setf (xwem-frame-xgeom frame) (make-X-Geom)))

    (unless (xwem-frame-x frame)
      (setf (xwem-frame-x frame) 0))
    (unless (xwem-frame-y frame)
      (setf (xwem-frame-y frame) 0))
    (unless (xwem-frame-width frame)
      (setf (xwem-frame-width frame) (X-Geom-width (xwem-rootgeom))))
    (unless (xwem-frame-height frame)
      (setf (xwem-frame-height frame)
	    (- (X-Geom-height (xwem-rootgeom))
	       (if (X-Geom-p (xwem-minib-xgeom xwem-minibuffer))
		   (X-Geom-height (xwem-minib-xgeom xwem-minibuffer))
		 0))))

    ;;; Initialize FRAME's X window
    (setq fwin (XCreateWindow
                (xwem-dpy) nil
                (xwem-frame-x frame)
                (xwem-frame-y frame)
                (xwem-frame-width frame)
                (xwem-frame-height frame)
                0                       ; border width
                nil                     ;DefaultDepth
                nil                     ;CopyFromParent
                nil                     ;CopyFromParent
                (make-X-Attr :override-redirect t
                             :backing-store X-Always
                             :cursor xwem-frame-cursor
                             :event-mask xwem-frame-ev-mask)))
    (X-Win-put-prop fwin 'xwem-frame frame)
    (setf (xwem-frame-xwin frame) fwin)

    ;; Install events handlers
    (X-Win-EventHandler-add-new fwin 'xwem-frame-events-handler 150)
    (X-Win-EventHandler-add-new fwin 'xwem-ev-reconfig 40 (list X-ConfigureRequest))

    ;; XXX Setup WM_XXX stuff
    (XSetWMProtocols (xwem-dpy) fwin
                     (list (X-Atom-find-by-name (xwem-dpy) "WM_DELETE_WINDOW")
                           (X-Atom-find-by-name (xwem-dpy) "WM_TAKE_FOCUS")))
    (XSetWMClass (xwem-dpy) fwin
                 (list (symbol-name (xwem-frame-type frame))
                       "xwem-frame"))
    (XSetWMName (xwem-dpy) fwin "xwem-frame")

    ;; Install grabbing
    (xwem-kbd-install-grab 'xwem-frame-prefix fwin)

    ;;; Initialise FRAME properties
    ;; Adjust frame properties in case FRAME is embedded frame.
    (when (xwem-frame-embedded-p frame)
      (setq fplist (xwem-misc-merge-plists fplist xwem-embedded-frame-default-properties)))

    ;; Set FRAME properties
    (xwem-frame-set-properties frame (xwem-misc-merge-plists fplist props))

    ;;; Initialize FRAME's root window
    (setq parwin (xwem-win-new (list :frame frame) nil))
    (setf (xwem-frame-selwin frame) parwin)
    (setf (xwem-frame-rootwin frame) parwin)

    (setf (xwem-win-x parwin) (xwem-frame-inner-border-width frame))
    (setf (xwem-win-y parwin) 
          (+ (xwem-frame-title-height frame)
             (xwem-frame-inner-border-width frame)))
    (setf (xwem-win-width parwin)
          (- (xwem-frame-width frame)
             (* 2 (xwem-frame-inner-border-width frame))))
    (setf (xwem-win-height parwin)
          (- (xwem-frame-height frame)
             (xwem-frame-title-height frame)
             (* 2 (xwem-frame-inner-border-width frame))))

    ;; Add to the end of frames list
    (setq xwem-frames-list
          (append xwem-frames-list (list frame)))

    ;; Handle as client, i.e. make frame to be embedded
    (when (xwem-frame-embedded-p frame)
      (let ((ecl (xwem-xwin-try-to-manage (xwem-frame-xwin frame))))
        (xwem-frame-put-prop frame 'xwem-embedded-cl ecl)
        (xwem-cl-put-sys-prop ecl 'xwem-embedded-frame frame)))

    ;; Finally map and maybe select newly created frame
    (unless (xwem-frame-property frame 'initially-unmapped)
      (xwem-frame-map frame))

    (unless noselect
      (xwem-select-frame frame))

    ;; Now run on-create hooks
    (run-hook-with-args 'xwem-frame-creation-hook frame)
    frame))

(defun xwem-init-frame-at-rect (xrect)
  "Create frame to fit in XRECT rectangle."
  (let ((xmrect (make-X-Rect :x 0 :y 0 :width (X-Geom-width (xwem-rootgeom))
			     :height (if (xwem-minib-xgeom xwem-minibuffer)
                                         (X-Geom-height (xwem-minib-xgeom xwem-minibuffer))
                                       (+ (* 2 xwem-minibuffer-border-width)
                                          (frame-pixel-height (xwem-minib-frame xwem-minibuffer))
                                          xwem-minibuffer-outer-border-width
                                          xwem-minibuffer-outer-border-width)))))

    (when (X-Rect-intersect-p xmrect xrect)
      ;; Take into account this intersection
      (setf (X-Rect-height xrect)
	    (- (X-Rect-height xrect)
	       (X-Rect-height xmrect))))
    
    (xwem-make-frame-1 'desktop
                       :params (list :xgeom (X-Rect-to-X-Geom xrect))
                       :noselect t)))

(defun xwem-frame-adjust-geom (frame new-rect)
  "Adjust FRAME geom according to NEW-RECT and xwem-minibuffer geom."
  (let ((mrect (X-Geom-to-X-Rect (xwem-minib-xgeom xwem-minibuffer)))
        (brd (X-Geom-border-width (xwem-minib-xgeom xwem-minibuffer)))
	ngeom)
    (when brd
      (incf (X-Rect-width mrect) (+ brd brd))
      (incf (X-Rect-height mrect) (+ brd brd)))

    (when (X-Rect-intersect-p new-rect mrect)
      (setf (X-Rect-height new-rect)
	    (- (X-Rect-height new-rect)
	       (X-Rect-height mrect))))

    (setq ngeom (X-Rect-to-X-Geom new-rect))
    (setf (X-Geom-border-width ngeom)
	  (X-Geom-border-width (xwem-frame-xgeom frame)))

    (setf (xwem-frame-xgeom frame) ngeom)))

(defun xwem-frame-default-select-hook ()
  "Do various default things when frame selected.
To be used in `xwem-frame-select-hook'."
  (xwem-frame-autoiconify-on-select)
  (xwem-frame-deffered-redraw-inner-border (xwem-frame-selected)))

(defun xwem-frame-default-deselect-hook ()
  "Do various default things when frame deselected.
To be used in `xwem-frame-deselect-hook'."
  (xwem-frame-autoiconify-on-deselect)
  (xwem-frame-deffered-redraw-inner-border (xwem-frame-selected)))

(defun xwem-frame-create-initial ()
  "Create initial frames."
  (if xwem-frame-dumped-config
      ;; Create frames from saved configuration
      (xwem-frame-config-restore1)

    ;; Xinerama stuff
    (let ((xin (X-XIneramaQueryScreens (xwem-dpy)))
          frame frame-old)
      (if (car xin)
          ;; XInerama enabled, so construct frames linkage
          (while (setq xin (cdr xin))
            (setq frame (xwem-init-frame-at-rect (car xin)))
            (when frame-old
              (xwem-frame-link-insert-after frame-old frame))
            (setq frame-old frame))

        ;; No XInerama, crate just one frame
        (xwem-init-frame-at-rect (X-Geom-to-X-Rect (xwem-rootgeom)))))

    ;; Select very first frame
    (xwem-select-frame (car (xwem-frames-list)))))

;;;###xwem-autoload
(defun xwem-frames-init ()
  "xwem frames initializer."
  (xwem-message 'init "Initializing frames ...")

  (setq xwem-frames-list nil)
  (setq xwem-current-frame nil)

  ;; Create frame cursors
  (setq  xwem-frame-cursor
         (xwem-make-cursor xwem-frame-cursor-shape
                           xwem-frame-cursor-foreground-color
                           xwem-frame-cursor-background-color))

  ;; Add autoiconifier hooks
  (add-hook 'xwem-frame-select-hook 'xwem-frame-default-select-hook)
  (add-hook 'xwem-frame-deselect-hook 'xwem-frame-default-deselect-hook)

  ;; Create initial frames
  (xwem-frame-create-initial)

  (xwem-message 'init "Initializing frames ... done"))

;;;###xwem-autoload
(defun xwem-frames-fini ()
  "Finialize frames."
  (mapc 'xwem-frame-destroy xwem-frames-list))

;;;###autoload(autoload 'xwem-make-frame "xwem-frame" "" t)
(define-xwem-command xwem-make-frame (type &optional arg)
  "Interactively create new XWEM frame.
With prefix ARG create frame of 'desktop type."
  (xwem-interactive (list (or (and xwem-prefix-arg 'desktop)
                              (xwem-completing-read
                               "XWEM Frame type [desktop]: "
                               (mapcar (lambda (cc) (list (symbol-name cc))) xwem-frame-types)))
                          xwem-prefix-arg))
  (when (stringp type)
    (if (string= type "")
        (setq type 'desktop)
      (setq type (intern-soft type))))

  (xwem-make-frame-1 type))

(defun xwem-frame-find (how arg)
  "Find frame by ARG. HOW is search type, one of 'xwin 'win 'cl or 'name."
  (let ((flist xwem-frames-list)
	(rf nil))

    (while flist
      (if (cond ((and (eq how 'xwin)
		      (X-Win-p arg)
		      (= (X-Win-id arg) (X-Win-id (xwem-frame-xwin (car flist)))))
		 t)
	    
		((and (eq how 'win)
		      (eq (xwem-win-frame arg) (car flist)))
		 t)

		((and (eq how 'cl)
		      (eq (xwem-cl-frame arg) (car flist)))
		 t)

                ((and (eq how 'name)
                      (string= arg (xwem-frame-name (car flist))))
                 t)

		(t nil))
	  (progn
	    (setq rf (car flist))
	    (setq flist nil))

	(setq flist (cdr flist))))
    rf))

(defun xwem-frame-other-frame (frame)
  "Return other frame for FRAME.
NOTE: not yet implemented"
  
  ;; Try linkage, and then try closest frame
  (let ((oframe (xwem-frame-link-next frame)))
    (unless (xwem-frame-p oframe)
      (setq oframe (xwem-frame-link-prev frame))
      (unless (xwem-frame-p oframe)
        (setq oframe (cadr (memq frame (xwem-frames-list (xwem-frame-type frame)))))
        (unless (xwem-frame-p oframe)
          (setq oframe (cadr (memq frame (nreverse (xwem-frames-list (xwem-frame-type frame))))))
          (unless (xwem-frame-p oframe)
            (setq oframe (car (xwem-frames-list)))))))
    oframe))

;;;###xwem-autoload
(defun xwem-frame-other (frame &optional type)
  "Same as `xwem-frame-other-frame', but return nil, if no good other frame found.
TYPE is ane of 'any, 'linkage"
  (unless type
    (setq type 'any))

  ;; First try linkaged frames
  (let ((allframes xwem-frames-list)
	(oframe (xwem-frame-link-next frame)))
    (unless (xwem-frame-mapped-p oframe)
      (setq oframe (xwem-frame-link-prev frame)))

    ;; Now try parent in case when FRAME is embedded frame.
    (unless (xwem-frame-mapped-p oframe)
      (when (xwem-frame-embedded-p frame)
	(let ((cl (xwem-frame-get-prop frame 'xwem-embedded-cl)))
	  (when (xwem-cl-p cl)
	    (setq oframe (xwem-cl-frame cl))))))

    (when (and (not (xwem-frame-mapped-p oframe))
	       (eq type 'any))
      ;; Scan frames list only for 'any TYPE
      (while (and allframes (not (xwem-frame-mapped-p oframe)))
	(when (and (xwem-frame-mapped-p (car allframes))
		   (not (eq frame (car allframes))))
	  (setq oframe (car allframes))
	  (setq allframes nil))
	(setq allframes (cdr allframes))))
    oframe))

;;;###xwem-autoload
(defun xwem-frame-num (frame)
  "Return FRAME index in `xwem-frames-list'."
  (unless (xwem-frame-p frame)
    (error 'xwem-error "Invalid frame" frame))

  (let ((fl (xwem-frames-list))
	(num 0))
    (while fl
      (if (eq frame (car fl))
	  (setq fl nil)
	(setq num (+ 1 num))
	(setq fl (cdr fl))))
  num))

(defun xwem-frame-xy-in-p (x y frame)
  "Return non-nil if point at X Y is in FRAME."
  (and (>= x (xwem-frame-x frame))
       (<= x (+ (xwem-frame-x frame) (xwem-frame-width frame)))
       (>= y (xwem-frame-y frame))
       (<= y (+ (xwem-frame-y frame) (xwem-frame-width frame)))))

(defun xwem-frame-at (x y)
  "Return frame which contain point at X Y."
  (let ((fl xwem-frames-list))
    (while (and fl (not (xwem-frame-xy-in-p x y (car fl))))
      (setq fl (cdr fl)))
    (car fl)))

;;;###autoload(autoload 'xwem-frame-apply-xgeom "xwem-frame" nil nil)
;;;###autoload(autoload 'xwem-frame-apply-xgeom-1 "xwem-frame" nil nil)
(define-xwem-deffered xwem-frame-apply-xgeom (frame)
  "Apply FRAME's geometry to life."
  (XConfigureWindow (xwem-dpy) (xwem-frame-xwin frame)
                    (make-X-Conf :x (xwem-frame-x frame)
                                 :y (xwem-frame-y frame)
                                 :width (xwem-frame-width frame)
                                 :height (xwem-frame-height frame)
                                 :border-width (xwem-frame-border-width frame)))

  (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                           (- (xwem-frame-width frame) 
                              (* 2 (xwem-frame-property frame 'inner-border-width))) t nil)
  (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                           (- (xwem-frame-height frame)
                              (xwem-frame-property frame 'title-height)
                              (* 2 (xwem-frame-property frame 'inner-border-width))) t t)

  (run-hook-with-args 'xwem-frame-resize-hook frame))

(define-xwem-deffered xwem-frame-apply-position (frame)
  "Apply FRAME's position to life."
  (when (xwem-frame-p frame)
    (XMoveWindow (xwem-dpy) (xwem-frame-xwin frame) (xwem-frame-x frame) (xwem-frame-y frame))
    (run-hook-with-args 'xwem-frame-move-hook frame)))

;;;###xwem-autoload
(defun xwem-frame-set-pos (frame new-x new-y)
  "Set FRAME position at NEW-X and NEW-Y."
  (setf (xwem-frame-x frame) new-x)
  (setf (xwem-frame-y frame) new-y)

  (xwem-frame-apply-position frame))

(define-xwem-deffered xwem-frame-apply-size (frame)
  "Apply FRAME's size to life."
  (when (xwem-frame-p frame)
    (XResizeWindow (xwem-dpy) (xwem-frame-xwin frame)
                   (xwem-frame-width frame) (xwem-frame-height frame))

    (run-hook-with-args 'xwem-frame-resize-hook frame)))

;;;###xwem-autoload
(defun xwem-frame-set-size (frame new-width new-height)
  "Resize FRAME to NEW-WIDTH and NEW-HEIGHT."
  (setf (xwem-frame-width frame) new-width)
  (setf (xwem-frame-height frame) new-height)

  (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                           (- (xwem-frame-width frame) 
                              (* 2 (xwem-frame-property frame 'inner-border-width))) t nil)
  (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                           (- (xwem-frame-height frame)
                              (xwem-frame-property frame 'title-height)
                              (* 2 (xwem-frame-property frame 'inner-border-width))) t t)

  (xwem-frame-apply-size frame))

;;; Frame Events handling
(defun xwem-frame-hexpose (frame xev)
  "Expose event handler."
  (X-Dpy-log (xwem-dpy) 'xwem-frame "Exposure event count: %S"
	     '(X-Event-xexpose-count xev))

  (when (zerop (X-Event-xexpose-count xev))
    ;; Redraw only when no other exposure events follow
    (and (xwem-frame-p frame)
         (xwem-frame-draw frame nil))))

(defun xwem-frame-remove (frame &optional select-other)
  "Remove FRAME from frames list, switch to other frame if SELECT-OTHER is non-nil."
  (let ((oframe (xwem-frame-other frame 'any))) ; other frame

    ;; Remove FRAME from linkage if any
    (xwem-frame-link-remove frame)

    ;; Now Remove FRAME from frame list
    (setq xwem-frames-list (delq frame xwem-frames-list))

    ;; If frame is not selected it mean that it was embedded
    (if (and select-other (xwem-frame-p oframe) (not (eq oframe frame)))
        (xwem-select-frame oframe)
      (setq xwem-current-frame nil))
    ))

(defun xwem-frame-total-remove (frame)
  "Totally remove FRAME."
  ;; Firstly we need to remove FRAME from frames list.
  (let ((embed-cl (xwem-frame-get-prop frame 'xwem-embedded-cl))
        (oframe (xwem-frame-other frame)))

    ;; If FRAME is selected, select some other frame
    (setq xwem-current-frame nil)
    (when (xwem-frame-p oframe)
      (xwem-select-frame oframe))

    ;; Block events handling
    (XSelectInput (xwem-dpy) (xwem-frame-xwin frame) 0)
    (setf (X-Win-event-handlers (xwem-frame-xwin frame)) nil)
    (X-Win-rem-prop (xwem-frame-xwin frame) 'xwem-frame)

    ;; Remove clients from FRAME
    (xwem-win-map
     (lambda (win)
       (mapc (lambda (cl)
               (when (and (xwem-cl-p cl)
                          (eq (xwem-cl-win cl) win))
                 (XReparentWindow (xwem-dpy) (xwem-cl-xwin cl) (xwem-rootwin)
                                  (X-Geom-width (xwem-rootgeom)) (X-Geom-height (xwem-rootgeom)))
                 (setf (xwem-cl-win cl) nil)
                 (xwem-client-change-state cl 'iconified)))
             xwem-clients))
     (xwem-frame-selwin frame))

    (when (xwem-cl-p embed-cl)
      ;; If we are embedded frame than emulate our destroing
      (xwem-cl-destroy embed-cl))

    ;; Destroy any X wins
    (unless (eq (xwem-frame-state frame) 'destroed)
      (XDestroySubwindows (xwem-dpy) (xwem-frame-xwin frame))
      (XDestroyWindow (xwem-dpy) (xwem-frame-xwin frame)))

    ;; Remove frame and select other
    (xwem-frame-remove frame t)

    ;; Now run on-destroy hooks
    (run-hook-with-args 'xwem-frame-destroy-hook frame)

    ;; Mark FRAME as non valid for referencing.
    (X-invalidate-cl-struct frame)
    ))
    
(defun xwem-frame-hconfigure (frame xev)
  "FRAME just received ConfigureNotify event XEV."
  (let ((owid (xwem-frame-width frame))
	(ohei (xwem-frame-height frame))
	(nwid (X-Event-xconfigure-width xev)) ;new width
	(nhei (X-Event-xconfigure-height xev))) ;new height

    (when (not (= owid nwid))
      (setf (xwem-frame-width frame) nwid)
      (setq nwid (- nwid (* 2 (xwem-frame-property frame 'inner-border-width))))
      (xwem-window-set-pixsize (xwem-frame-rootwin frame) nwid t nil))

    (when (not (= ohei nhei))
      (setf (xwem-frame-height frame) nhei)
      (setq nhei (- nhei (xwem-frame-property frame 'title-height)
                    (* 2 (xwem-frame-property frame 'inner-border-width))))
      (xwem-window-set-pixsize (xwem-frame-rootwin frame) nhei t t))

    (unless (and (= owid nwid) (= ohei nhei))
      (run-hook-with-args 'xwem-frame-resize-hook frame))
    ))

(defun xwem-frame-hclient (frame xev)
  "FRAME just received XClientMessage event XEV."
  (X-Dpy-log (xwem-dpy) 'xwem-frame
             "FRAME[%d] got ClientMessage, Atom=%S(%s).."
             '(xwem-frame-num frame) '(X-Atom-id (X-Event-xclient-atom xev))
             '(X-Atom-name (X-Event-xclient-atom xev)))

  (cond ((string= (X-Atom-name (X-Event-xclient-atom xev)) "WM_PROTOCOLS")
         (let ((wmda (X-Atom-find (xwem-dpy) (caar (X-Event-xclient-msg xev)))))
           (when (X-Atom-p wmda)
             (cond ((string= (X-Atom-name wmda) "WM_DELETE_WINDOW")
                    (X-Dpy-log (xwem-dpy) 'xwem-frame "Killing frame, because of WM_DELETE_WINDOW client message")
                    (xwem-frame-total-remove frame))
                   ((string= (X-Atom-name wmda) "WM_TAKE_FOCUS")
                    (X-Dpy-log (xwem-dpy) 'xwem-frame "Frame(%s): Taking focus .." '(xwem-frame-name frame))
                    (xwem-focus-set (xwem-frame-cl frame)))))
           ))
        ))

(defun xwem-frame-hkeybutton (frame xev)
  "On FRAME handle KeyPress, ButtonPress or ButtonRelease event XEV."
  (xwem-overriding-local-map 'xwem-frame-prefix
    (when (eq (xwem-dispatch-command-xevent xev) 'done)
      (signal 'X-Events-stop nil))))

(defun xwem-frame-events-handler (xdpy win xev)
  "Event handler for frame."
  (X-Dpy-log (xwem-dpy) 'xwem-frame "Got event ev=%S, win=%S"
	     '(X-Event-name xev) '(X-Win-id win))

  (let ((frame (X-Win-get-prop win 'xwem-frame)))
    (when (xwem-frame-p frame)
      (X-Event-CASE xev
	(:X-Expose
	 (xwem-frame-hexpose frame xev))

	((:X-KeyPress :X-ButtonPress :X-ButtonRelease)
	 (xwem-frame-hkeybutton frame xev))

	(:X-DestroyNotify
	 ;; Somebody kill us, or one of clients die
	 (setq frame (X-Win-get-prop (X-Event-xdestroywindow-window xev) 'xwem-frame))
	 (when (xwem-frame-p frame)
           (setf (xwem-frame-state frame) 'destroed)
           (xwem-frame-total-remove frame)))

	(:X-ConfigureNotify
	 ;; Seems that we are embedded frame
	 (setq frame (X-Win-get-prop (X-Event-xconfigure-window xev) 'xwem-frame))
	 (when (xwem-frame-p frame)
	   (xwem-frame-hconfigure frame xev)))

        (:X-ClientMessage
         (xwem-frame-hclient frame xev))
        ))))

;;;; Frame Events handling ends here

(defun xwem-frame-draw (frame full)
  "Draw FRAME. If FULL is t then fully redraw it, i.e. ClearWindow first."
  (unless (eq frame xwem-frame-noredisplay)
    (when full
      (XClearArea (xwem-dpy) (xwem-frame-xwin frame) 0 0
                  (xwem-frame-width frame)
                  (xwem-frame-height frame) nil))

    (xwem-frame-draw-inner-border frame)

    (run-hook-with-args 'xwem-frame-redraw-hook frame)))

;;; Frame configuration section
;;;###xwem-autoload
(defun xwem-frame-configuration-p (frame-config)
  "Return non-nil if FRAME-CONFIG is looks like frame configuration."
  (and (listp frame-config)
       (eq 'xwem-frame-configuration
           (car frame-config))))

(defun xwem-frame-config-find-sframe (frame sframe-list)
  "Find saved frame by FRAME."
  (while (and sframe-list
              (not (eq (xwem-frame-saved-frame (car sframe-list)) frame)))
    (setq sframe-list (cdr sframe-list)))
  (car sframe-list))

;;;###xwem-autoload
(defun xwem-frame-configuration ()
  "Return current xwem frame configuration."
  ;; TODO: save domain faces
  (cons 'xwem-frame-configuration
        (mapcar (lambda (frame)
                  (make-xwem-frame-saved :frame frame
                                         :type (xwem-frame-type frame)
                                         :name (xwem-frame-name frame)
                                         :xgeom (copy-X-Geom (xwem-frame-xgeom frame))
                                         :state (xwem-frame-state frame)
                                         :plist (xwem-frame-properties frame)
                                         :winconfig (xwem-window-configuration frame)))
                xwem-frames-list)))

;;;###xwem-autoload
(defun xwem-set-frame-configuration (frame-config &optional no-delete)
  "Restore the frames to the state described by FRAME-CONFIG."
  (unless (xwem-frame-configuration-p frame-config)
    (signal 'wrong-type-argument
            (list 'xwem-frame-configuration-p frame-config)))

  ;; TODO:
  ;;   - Maybe recreate frames that in config, but already destroyed?
  (let ((conf (cdr frame-config))
        frames-to-delete)
    (mapc (lambda (frame)
            (let ((sframe (xwem-frame-config-find-sframe frame conf)))
              (if (xwem-frame-saved-p sframe)
                  (progn
                    (setf (xwem-frame-name frame) (xwem-frame-saved-name sframe))
                    (setf (xwem-frame-state frame) (xwem-frame-saved-state sframe))
                    (xwem-frame-adjust-geom frame (xwem-frame-saved-xgeom sframe))
                    (xwem-set-window-configuration (xwem-frame-saved-winconfig sframe)))

                (setq frames-to-delete (cons frame frames-to-delete)))))
          xwem-frames-list)

    (if no-delete
        (mapc 'xwem-frame-hide frames-to-delete)
      (mapc 'xwem-frame-destroy frames-to-delete))
    ))

(defun xwem-frame-config-dump1 (config buffer &optional append)
  "Dump frames CONFIG to BUFFER.
If APPEND is non-nil, do not erase FILE, just append to the end."
  ;; TODO: dump also domain faces
  (let ((ccf (copy-sequence config))
        wcf)
    (with-current-buffer (or buffer (current-buffer))
      (if append
          (progn
            (goto-char (point-max))
            (insert "\n"))
        (erase-buffer))

      (insert "(setq xwem-frame-dumped-config (list 'xwem-frame-configuration\n")
      (mapc (lambda (sfr)
              (setf (xwem-frame-saved-frame sfr) nil)

              ;; Adjust win config
              (setq wcf (xwem-frame-saved-winconfig sfr))
              (setf (xwem-win-config-frame wcf) nil)
              (setf (xwem-win-config-current-cl wcf) nil)
              (flet ((clrwin (swin)
                             (setf (xwem-win-saved-clients swin) nil)
                             (setf (xwem-win-saved-cl swin) nil)
                             (when (xwem-win-saved-first-vchild swin)
                               (clrwin (xwem-win-saved-first-vchild swin)))
                             (when (xwem-win-saved-first-hchild swin)
                               (clrwin (xwem-win-saved-first-hchild swin)))
                             (when (xwem-win-saved-next swin)
                               (clrwin (xwem-win-saved-next swin)))
                             (when (xwem-win-saved-prev swin)
                               (clrwin (xwem-win-saved-prev swin)))))
                (clrwin (xwem-win-config-saved-root-window wcf)))
              
              (insert (format "%S\n" sfr)))
            (cdr ccf))
      (insert "))\n"))))

(defun xwem-frame-config-dump (config &optional file)
  "Dump frame configuration CONFIG to FILE.
If FILE ommited, than ~/.xwem/xwem-configs.el will be used."
  (unless (xwem-frame-configuration-p config)
    (error 'xwem-error "Not an xwem frame configuration" config))

  (unless file
    (setq file (expand-file-name "xwem-configs.el" xwem-dir)))

  (let* ((find-file-hooks nil)          ; omit autoinsert and others
         (buf (find-file-noselect file)))
    (xwem-frame-config-dump1 config buf)
    (save-buffer buf)))

(defun xwem-frame-config-restore1 ()
  "Internal frame config restorer."
  (let (frame-to-select)
    (mapc (lambda (sfr)
            (let ((swin (xwem-frame-saved-winconfig sfr))
                  nframe)
              (setq nframe (xwem-make-frame-1 (xwem-frame-saved-type sfr)
                                              :params (list :name (xwem-frame-saved-name sfr)
                                                            :xgeom (xwem-frame-saved-xgeom sfr))
                                              :props (xwem-frame-saved-plist sfr)
                                              :noselect t))
              (when (xwem-frame-p nframe)
                (setf (xwem-win-config-frame swin) nframe)
                (xwem-set-window-configuration swin)

                (unless frame-to-select
                  (setq frame-to-select nframe)))
              ))
          (cdr xwem-frame-dumped-config))
    (when frame-to-select
      (xwem-select-frame frame-to-select)))

  ;; dumped config has been restored
  (setq xwem-frame-dumped-config nil))

(defun xwem-frame-config-restore (&optional file)
  "Restore saved frames configuration from FILE.
Default FILE is ~/.xwem/xwem-configs.el"
  (unless file
    (setq file (expand-file-name "xwem-configs.el" xwem-dir)))

  (load-file file)
  (unless (xwem-frame-configuration-p xwem-frame-dumped-config)
    (error 'xwem-error "no frames configuration to restore"))

  (xwem-frame-config-restore1))

;;; Frame configuration section ends here
;;;###autoload(autoload 'xwem-frame-next "xwem-frame" "" t)
(define-xwem-command xwem-frame-next (arg)
  "Switch to ARG next frame."
  (xwem-interactive "p")

  (let ((frame (nth arg (memq (xwem-frame-selected) xwem-frames-list))))
    (when (and xwem-frame-rolling-switch
               (not (xwem-frame-p frame)))
      ;; Assume first frame if there no next
      (setq frame (car xwem-frames-list)))

    (unless (xwem-frame-p frame)
      (error 'xwem-error "Invalid frame"))

    (xwem-select-frame frame)))
(put 'xwem-frame-next 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-previous "xwem-frame" "" t)
(define-xwem-command xwem-frame-previous (arg)
  "Switch to ARG previous frame."
  (xwem-interactive "p")

  (let ((frame (nth arg (memq (xwem-frame-selected) (reverse xwem-frames-list)))))
    (when (and xwem-frame-rolling-switch
               (not (xwem-frame-p frame)))
      ;; Assume last frame if there no previous
      (setq frame (car (last xwem-frames-list))))

    (unless (xwem-frame-p frame)
      (error 'xwem-error "Invalid frame"))

    (xwem-select-frame frame)))
(put 'xwem-frame-previous 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-switch "xwem-frame" "Switch to frame by name." t)
(define-xwem-command xwem-frame-switch (name)
  "Switch to frame by NAME."
  (xwem-interactive (list (xwem-completing-read "XWEM Frame: "
                                                (mapcar (lambda (fr)
                                                          (list (xwem-frame-name fr)))
                                                        (xwem-frames-list)))))
  ;; Find frame by name
  (let ((frms (xwem-frames-list))
        (frame nil))
    (while (and frms (not (string= (xwem-frame-name (car frms)) name)))
      (setq frms (cdr frms)))
    (setq frame (car frms))

    (unless (xwem-frame-alive-p frame)
      (error 'xwem-error "No such frame"))

    (xwem-select-frame frame)))

;;;###autoload(autoload 'xwem-frame-switch-nth "xwem-frame" "" t)
(define-xwem-command xwem-frame-switch-nth (arg)
  "Switch xwem frame.
If ARG is numeric prefix, then switch to ARG frame.
If ARG ommited, 0 as ARG value will be used.
If ARG is list and selected frame is embedded, than unembedd it."
  (xwem-interactive "P")

  (when (null arg)
    (setq arg 0))
  (if (numberp arg)
      (let ((frame (nth (abs arg) xwem-frames-list)))
	(if (xwem-frame-p frame)
	    (xwem-select-frame frame)
	  (xwem-message 'warning "No such %S frame." (abs arg))))

    ;; UNEMBED selected frame
    (let ((frame (xwem-frame-selected)))
      (when (xwem-frame-embedded-p frame)
        (xwem-frame-unembedd frame)
        (xwem-select-frame frame)))
    ))

;;;###autoload(autoload 'xwem-frame-switch-nth-linkage "xwem-frame" "" t)
(define-xwem-command xwem-frame-switch-nth-linkage (arg)
  "Raise all frames that in linkage of frame with number NUM."
  (xwem-interactive "P")

  (when (null arg) (setq arg 0))
  (if (numberp arg)
      (let ((frame (nth (abs arg) xwem-frames-list)))
	(if (not (xwem-frame-p frame))
	  (xwem-message 'warning "No such %S frame." (abs arg))

	  ;; Select linkage
	  (xwem-frame-linkage-map
	   frame
	   (lambda (fr)
	     (xwem-frame-raise fr)))
	  (xwem-select-frame frame)))
    (xwem-message 'warning "Strange arg value: %S" arg)))

;;;###autoload(autoload 'xwem-frame-destroy "xwem-frame" "" t)
(define-xwem-command xwem-frame-destroy (frame)
  "Destroy FRAME. If FRAME is not given selected frame assumed."
  (xwem-interactive (list (xwem-frame-selected)))

  (when (xwem-frame-p frame)
    (xwem-frame-total-remove frame)))
(put 'xwem-frame-destroy 'xwem-frame-command t)

(defun xwem-frame-goto (n direction &optional frame)
  "Goto window at DIRECTION on FRAME N times.
DIRECTION is one of 'next, 'prev, 'next-vert, ..."
  (let* ((gframe (or frame (xwem-frame-selected)))
	 (cwin (xwem-frame-selwin gframe)))
    (while (> n 0)
      (cond ((eq direction 'next)
	     (setq cwin (xwem-window-next cwin)))
	    ((eq direction 'prev)
	     (setq cwin (xwem-window-prev cwin)))
	    ((eq direction 'next-vert)
	     (setq cwin (xwem-window-next-vertical cwin)))
	    (t (error 'xwem-error "Bad DIRECTION in `xwem-frame-goto', should be one of 'next or 'prev")))
      (setq n (1- n)))

    (xwem-select-window cwin)
  ))

;;;###autoload(autoload 'xwem-frame-goto-next "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-next (arg)
  "Goto ARG next window in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'next))

;;;###autoload(autoload 'xwem-frame-goto-prev "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-prev (arg)
  "Goto ARG previous window in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'prev))

;;;###autoload(autoload 'xwem-frame-goto-next-vert "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-next-vert (arg)
  "Goto ARG next window in vertical direction in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'next-vert))

;;;###autoload(autoload 'xwem-frame-goto-next-hor "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-next-hor (arg)
  "Goto ARG next window in horizontal direction in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'next-hor))

;;;###autoload(autoload 'xwem-frame-goto-prev-vert "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-prev-vert (arg)
  "Goto ARG previous window in vertical direction in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'priv-vert))

;;;###autoload(autoload 'xwem-frame-goto-prev-hor "xwem-frame" "" t)
(define-xwem-command xwem-frame-goto-prev-hor (arg)
  "Goto ARG previous window in horizontal direction in selected frame."
  (xwem-interactive "p")
  (xwem-frame-goto arg 'prev-hor))

;;;###autoload(autoload 'xwem-frame-split-sbs "xwem-frame" "" t)
(define-xwem-command xwem-frame-split-sbs (n &optional frame side)
  "Makes N frames side by size of FRAME.
SIDE is one of 'vertical or 'horizontal, if ommited or not one of
above - 'horizontal will be used.

Example:
SIDE is 'horiz
+--------+                             +----+----+--...--+----+
+--------+                             +----+----+--...--+----+
| 1      | `xwem-frame-split-sbs' |--> | 1  | 2  |       | N  |
|        |                             |    |    |       |    |
+--------+                             +----+----+--...--+----+

Widths sum of all N frames after sbs split is equal to width of frame
before."
  (xwem-interactive "p")

  (let* ((vertp (eq side 'vertical))
	 (frm (or frame (xwem-frame-selected)))
	 (wi (xwem-frame-width frm))
	 (he (xwem-frame-height frm))
	 (nwi wi)
	 (nhe he)
	 (wost 0)
	 (host 0)
	 xoff yoff)
    
    (when (xwem-frame-embedded-p frm)
      (error 'xwem-error "Can't split embedded frame"))

    (if vertp
	(progn
	  (setq nhe (/ he (1+ n)))
	  (setq host (% he (1+ n))))
      (setq nwi (/ wi (1+ n)))
      (setq wost (% wi (1+ n))))

    (setq xoff (+ nwi wost))
    (setq yoff (+ nhe host))

    ;; Resize frame
    (xwem-frame-set-size frm xoff yoff)

    ;; TODO: - inherit same parent in case FRM is embedded
    ;;       - install frames linkage
    (let ((oframe frm)
	  (nframe nil)
	  (samex (xwem-frame-x frm))
	  (samey (xwem-frame-y frm)))
      (while (> n 0)
	(setq nframe
	      (xwem-make-frame-1 (xwem-frame-type frm)
				 :params (list :xgeom
                                               (make-X-Geom :x (if vertp samex (+ samex xoff))
                                                            :y (if vertp (+ samey yoff) samey)
                                                            :width nwi
                                                            :height nhe))
                                 :noselect t))
	;; Now setup linkage
	(xwem-frame-link-insert-after oframe nframe)
	(setq oframe nframe)

	(if vertp
	    (setq yoff (+ yoff nhe))
	  (setq xoff (+ xoff nwi)))
	(setq n (1- n))))
    ))

;;;###autoload(autoload 'xwem-frame-sbs-hor-split "xwem-frame" "" t)
(define-xwem-command xwem-frame-sbs-hor-split (n)
  "Make horizontal sbs split N times for selected frame."
  (xwem-interactive "p")
  (xwem-frame-split-sbs n))
(put 'xwem-frame-sbs-hor-split 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-sbs-vert-split "xwem-frame" "" t)
(define-xwem-command xwem-frame-sbs-vert-split (n)
  "Make vertical sbs split N times for selected frame."
  (xwem-interactive "p")
  (xwem-frame-split-sbs n nil 'vertical))
(put 'xwem-frame-sbs-vert-split 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-fit-screen "xwem-frame" "" t)
(define-xwem-command xwem-frame-fit-screen (frame)
  "Fit FRAME to screen sizes."
  (xwem-interactive (list (xwem-frame-selected)))

  ;; Take into acount XInerama layout
  (let ((frect (X-Geom-to-X-Rect (xwem-frame-xgeom frame)))
	(xin (X-XIneramaQueryScreens (xwem-dpy))))
    (if (car xin)
	(progn
	  ;; XInerama enabled
	  (while (and (setq xin (cdr xin))
		      (not (X-Rect-intersect-p (car xin) frect))))
	  (setq xin (car xin)))

      ;; No XInerama, so use root geometry
      (setq xin (X-Geom-to-X-Rect (xwem-rootgeom))))

    (xwem-frame-adjust-geom frame xin)

    (xwem-frame-set-size frame (xwem-frame-width frame) (xwem-frame-height frame))
    (xwem-frame-set-pos frame (xwem-frame-x frame) (xwem-frame-y frame))

    ;; XXX uninstall linkage if any
    (xwem-frame-link-remove frame)
    ))
(put 'xwem-frame-fit-screen 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-transpose "xwem-frame" "" t)
(define-xwem-command xwem-frame-transpose (arg)
  "Transpose frames ARG times."
  (xwem-interactive "p")
  (error 'xwem-error "`xwem-frame-transpose' not implemented yet."))

;;;###autoload(autoload 'xwem-frame-showroot "xwem-frame" "" t)
(define-xwem-command xwem-frame-showroot ()
  "Show root window, i.e. unmap all xwem frames."
  (xwem-interactive)

  (mapc 'xwem-frame-unmap xwem-frames-list)

  (xwem-select-client nil))

;;;###autoload(autoload 'xwem-frame-hide "xwem-frame" "" t)
(define-xwem-command xwem-frame-hide (frame &optional no-select)
  "Hide FRAME.
When called interactively, FRAME is selected frame.
With prefix ARG, do not select new frame."
  (xwem-interactive (list (xwem-frame-selected)
			  xwem-prefix-arg))

  (let ((oframe (xwem-frame-other frame)))
    (when (xwem-frame-mapped-p frame)
      ;; We are about to hide FRAME, so select other frame
      (when (and (not no-select)
                 (xwem-frame-p oframe))
        (xwem-select-frame oframe))
      (xwem-frame-unmap frame))))
(put 'xwem-frame-hide 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-transpose-frames "xwem-frame" "" t)
(define-xwem-command xwem-transpose-frames (arg)
  "Transpose selected frame with ARG other frame."
  (xwem-interactive "p")

  (let* ((sfr (xwem-frame-selected))
	 (ofr (xwem-frame-other sfr))
	 sg og)
    
    (when (xwem-frame-p ofr)
      (setq sg (copy-sequence (xwem-frame-xgeom sfr))
	    og (copy-sequence (xwem-frame-xgeom ofr)))

      (xwem-frame-set-pos sfr (X-Geom-x og) (X-Geom-y og))
      (xwem-frame-set-pos ofr (X-Geom-x sg) (X-Geom-y sg))
      
      (xwem-frame-set-size sfr (X-Geom-width og) (X-Geom-height og))
      (xwem-frame-set-size ofr (X-Geom-width sg) (X-Geom-height sg))

      ;; Finally exchange positions (aka frame number) in
      ;; `xwem-frames' list
      (xwem-list-exchange-els xwem-frames-list sfr ofr))))
(put 'xwem-transpose-frames 'xwem-frame-command t)

;;;###autoload(autoload 'xwem-frame-set-name "xwem-frame" "" t)
(define-xwem-command xwem-frame-set-name (name &optional frame)
  "Set FRAME's name to NAME."
  (xwem-interactive (list (xwem-read-from-minibuffer
                           (format "New frame name (old: %s): "
                                   (xwem-frame-name (xwem-frame-selected))))
                          (xwem-frame-selected)))

  (setf (xwem-frame-name frame) name)

  ;; Finally run hooks
  (run-hook-with-args 'xwem-frame-change-hook frame))
(put 'xwem-frame-set-name 'xwem-frame-command t)

;; XXX: what this?
(defun xwem-frame-in-delim-p (win x y)
  "Return non-nil if X Y is inside some delimeter."
  (catch 'found
    (let ((hc (xwem-win-hchild win))
          (vc (xwem-win-vchild win))
          rwin)

      (while (xwem-win-p hc)
	;; For horizontal split
        (when (and (> x (+ (xwem-win-x hc) (xwem-win-width hc)))
                   (< x (+ (xwem-win-x hc) (xwem-win-width hc)
                           (xwem-win-delim-width hc)))
                   (> y (xwem-win-y hc))
                   (< y (+ (xwem-win-y hc) (xwem-win-height hc))))
          (throw 'found hc))
        (when (setq rwin (xwem-frame-in-delim-p hc x y))
          (throw 'found rwin))
        (setq hc (xwem-win-next hc)))

      (while (xwem-win-p vc)
        (when (and (> x (xwem-win-x vc))
                   (< x (+ (xwem-win-x vc) (xwem-win-width vc)))
                   (> y (+ (xwem-win-y vc) (xwem-win-height vc)))
                   (< y (+ (xwem-win-y vc) (xwem-win-height vc)
                           (xwem-win-delim-width vc))))
          (throw 'found vc))
        (when (setq rwin (xwem-frame-in-delim-p vc x y))
          (throw 'found rwin))
	(setq vc (xwem-win-next vc)))
      nil)))

;;;###autoload(autoload 'xwem-frame-on-delim-resize "xwem-frame" "" t)
(define-xwem-command xwem-frame-on-delim-resize ()
  "Resize window dragging delimiter."
  (xwem-interactive)
  
  (let* ((x (X-Event-xbutton-event-x xwem-last-xevent))
         (y (X-Event-xbutton-event-y xwem-last-xevent))
         (frame (or (X-Win-get-prop (X-Event-xbutton-event xwem-last-xevent) 'xwem-frame)
                    (xwem-frame-at x y)))
         (in-xy (and (xwem-frame-p frame)
                     (xwem-frame-in-delim-p
                      (xwem-frame-rootwin frame) x y)))
         (win in-xy)
         xrect type cursor done xev)

    (when (xwem-win-p win)
      ;; Findout resize tipe (vertical or horisontal)
      (if (> y (+ (xwem-win-y win)
                  (xwem-win-height win)))
          (setq type 'vert)
        (setq type 'horz))

      ;; Fill xrects
      (setq xrect (make-X-Rect :x (xwem-win-x win)
                               :y (xwem-win-y win)
                               :width (xwem-win-width win)
                               :height (xwem-win-height win)))

      ;; Create cursor
      (setq cursor (xwem-make-cursor
                    (if (eq type 'vert)
                        X-XC-sb_v_double_arrow
                      X-XC-sb_h_double_arrow)))

      (xwem-mouse-grab cursor (xwem-frame-xwin frame)
                       (Xmask-or XM-ButtonRelease XM-ButtonMotion))
      (unless (eq xwem-frame-on-delim-resize-mode 'opaque)
        (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame)))
      (xwem-unwind-protect
          (while (and (not done)        ; still not done?
                      (setq xev (xwem-next-event)) ; deliver event before win check
                      (xwem-win-next win)) ; still has delimiter?
            (X-Event-CASE xev
              (:X-ButtonRelease (setq done t))
        
              (:X-MotionNotify
               (cond ((and (eq type 'vert)
                           (> (X-Event-xmotion-event-y xev) 0)
                           (> (X-Event-xmotion-event-y xev)
                              (+ (X-Rect-y xrect) xwem-win-min-height))
                           (not (= (X-Event-xmotion-event-y xev)
                                   (+ (X-Rect-y xrect) (X-Rect-height xrect)))))
                      (unless (eq xwem-frame-on-delim-resize-mode 'opaque)
                        (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame)))
                      (setf (X-Rect-height xrect)
                            (- (X-Event-xmotion-event-y xev) (X-Rect-y xrect)))
                      (if (eq xwem-frame-on-delim-resize-mode 'opaque)
                          (xwem-window-enlarge-vertically (- (X-Rect-height xrect) (xwem-win-height win)) win)
                        (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame))))

                     ((and (eq type 'horz)
                           (> (X-Event-xmotion-event-x xev) 0)
                           (> (X-Event-xmotion-event-x xev)
                              (+ (X-Rect-x xrect) xwem-win-min-width))
                           (not (= (X-Event-xmotion-event-x xev)
                                   (+ (X-Rect-x xrect) (X-Rect-width xrect)))))
                      (unless (eq xwem-frame-on-delim-resize-mode 'opaque)
                        (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame)))
                      (setf (X-Rect-width xrect)
                            (- (X-Event-xmotion-event-x xev) (X-Rect-x xrect)))
                      (if (eq xwem-frame-on-delim-resize-mode 'opaque)
                          (xwem-window-enlarge-horizontally (- (X-Rect-width xrect) (xwem-win-width win)) win)
                        (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame))))))))

        (xwem-mouse-ungrab)
        (XFreeCursor (xwem-dpy) cursor)

        (unless (eq xwem-frame-on-delim-resize-mode 'opaque)
          (xwem-misc-outline xrect 'normal (xwem-frame-xwin frame))

          (if (eq type 'vert)
              (xwem-window-enlarge-vertically (- (X-Rect-height xrect)
                                                 (xwem-win-height win)) win)
            (xwem-window-enlarge-horizontally (- (X-Rect-width xrect)
                                                 (xwem-win-width win)) win)))
        ))))

;;;###autoload(autoload 'xwem-frame-on-delim-menu "xwem-frame" "" t)
(define-xwem-command xwem-frame-on-delim-menu ()
  "Popup menu when delimiter clicked."
  (xwem-interactive)

  (let* ((x (X-Event-xbutton-event-x xwem-last-xevent))
         (y (X-Event-xbutton-event-y xwem-last-xevent))
         (frame (xwem-frame-at x y))
         (win (and (xwem-frame-p frame)
                   (xwem-frame-in-delim-p
                    (xwem-frame-rootwin frame) x y))))

    (when (xwem-win-p win)
      (xwem-popup-menu (xwem-generate-window-menu "Window" win)))))

;;;###xwem-autoload
(defun xwem-frame-imove-internal (frame sx sy &optional imove-mode)
  "Interactively move FRAME."
  (unless imove-mode
    (setq imove-mode (funcall xwem-frame-imoveresize-mode-function frame 'move)))

  (let ((step 1)
        (done nil)
        last-xrect curr-xrect xev
        pevs)

    (setq last-xrect (X-Geom-to-X-Rect (xwem-frame-xgeom frame))
          curr-xrect (copy-X-Rect last-xrect))
    
    (xwem-mouse-grab xwem-cursor-move nil ;(xwem-frame-xwin frame)
                     (Xmask-or XM-ButtonPress XM-ButtonRelease
                               XM-ButtonMotion XM-PointerMotion))
    (unless (eq imove-mode 'opaque)
      (xwem-misc-outline last-xrect imove-mode))
    ;; Normally we should do this event loop under GrabServer, but
    ;; grabbing server causes XEmacs to freeze in some
    ;; circumstances. --lg
    (xwem-unwind-protect
        (while (not done)
          (setq xev (xwem-next-event nil (list X-ButtonPress X-ButtonRelease
                                               X-MotionNotify X-MapRequest)))
          (X-Event-CASE xev
            (:X-MapRequest
             (setq pevs (append pevs (list xev))))
            ((:X-ButtonPress :X-ButtonRelease) (setq done t))
        
            (:X-MotionNotify
             ;; Update curr-xrect
             (setf (X-Rect-x curr-xrect)
                   (+ (X-Rect-x curr-xrect) (- (X-Event-xmotion-root-x xev) sx)))
             (setq sx (X-Event-xmotion-root-x xev))
             (setf (X-Rect-y curr-xrect)
                   (+ (X-Rect-y curr-xrect) (- (X-Event-xmotion-root-y xev) sy)))
             (setq sy (X-Event-xmotion-root-y xev))

             (when (or (> (abs (- (X-Rect-x curr-xrect) (X-Rect-x last-xrect))) step)
                       (> (abs (- (X-Rect-y curr-xrect) (X-Rect-y last-xrect))) step))
               (unless (eq imove-mode 'opaque)
                 (xwem-misc-outline last-xrect imove-mode))

               (setf (X-Rect-x last-xrect) (X-Rect-x curr-xrect))
               (setf (X-Rect-y last-xrect) (X-Rect-y curr-xrect))
               (if (eq imove-mode 'opaque)
                   (xwem-frame-set-pos frame (X-Rect-x last-xrect) (X-Rect-y last-xrect))
                 (xwem-misc-outline last-xrect imove-mode))))))

      (xwem-mouse-ungrab)
      (unless (eq imove-mode 'opaque)
        (xwem-misc-outline last-xrect imove-mode))

      ;; Apply changes
      (xwem-frame-set-pos frame (X-Rect-x last-xrect) (X-Rect-y last-xrect)))

    ;; Dispatch map requests
    (mapc 'X-Dpy-event-dispatch pevs)))
  
;;;###autoload(autoload 'xwem-frame-imove "xwem-frame" "" t)
(define-xwem-command xwem-frame-imove ()
  "Interactively move FRAME."
  (xwem-interactive)

  (unless (or (interactive-p)
              (= (X-Event-type xwem-last-xevent) X-ButtonPress))
    (error 'xwem-error "`xwem-frame-imove'' must be binded to mouse event"))

  (let* ((srx (X-Event-xbutton-root-x xwem-last-xevent))
         (sry (X-Event-xbutton-root-y xwem-last-xevent))
         (frame (or (xwem-xwin-frame (X-Event-xbutton-event xwem-last-xevent))
                    (xwem-frame-at srx sry))))

    (unless (xwem-frame-p frame)
      (error 'xwem-error "`xwem-frame-imove' on non-frame"))

    (xwem-frame-imove-internal frame srx sry)))

;;;###autoload(autoload 'xwem-frame-iresize "xwem-frame" "" t)
(define-xwem-command xwem-frame-iresize ()
  "Interactively resize frame"
  (xwem-interactive)

  (unless (or (interactive-p)
              (= (X-Event-type xwem-last-xevent) X-ButtonPress))
    (error 'xwem-error "`xwem-frame-iresize' must be binded to mouse event"))

  (let* ((srx (X-Event-xbutton-root-x xwem-last-xevent))
         (sry (X-Event-xbutton-root-y xwem-last-xevent))
         (frame (or (xwem-xwin-frame (X-Event-xbutton-event xwem-last-xevent))
                    (xwem-frame-at srx sry)))
         (iresize-mode (funcall xwem-frame-imoveresize-mode-function frame 'resize))
         (last-xrect (and (xwem-frame-p frame)
                          (make-X-Rect :x (xwem-frame-x frame) :y (xwem-frame-y frame)
                                       :width (- srx (xwem-frame-x frame))
                                       :height (- sry (xwem-frame-y frame)))))
         (curr-xrect (copy-X-Rect last-xrect))
         step-x step-y min-height min-width
         done xev)

    (unless (xwem-frame-p frame)
      (error 'xwem-error "`xwem-frame-iresize' on non-frame"))

    ;; Setup steps
    (when (xwem-frame-dedicated-p frame)
      (let ((sg (xwem-cl-step-geom (xwem-frame-cl frame)))
            (mg (xwem-cl-min-geom (xwem-frame-cl frame))))
        (setq step-x (car sg)
              step-y (cdr sg)
              min-width (car mg)
              min-height (cdr mg))))
    (unless step-x (setq step-x 1))
    (unless step-y (setq step-y 1))

    ;; Calculate minimal width/height of frame.
    ;; Simple sum win's min width/height so many times how many
    ;; windows
    (unless min-width
      (setq min-width
            (+ xwem-win-min-width
               (- (xwem-frame-width frame)
                  (xwem-win-width (xwem-frame-rootwin frame)))))
      (xwem-win-map (lambda (w)
                      (let ((pwin (xwem-win-parent w)))
                        (when (and (and pwin (xwem-win-hchild pwin)))
                          (incf min-width (+ xwem-win-min-width (car xwem-win-horizontal-delim-width))))))
                    (xwem-frame-selwin frame)))
    (unless min-height
      (setq min-height
            (+ xwem-win-min-height
               (- (xwem-frame-height frame)
                  (xwem-win-height (xwem-frame-rootwin frame)))))
      (xwem-win-map (lambda (w)
                      (let ((pwin (xwem-win-parent w)))
                        (when (and (and pwin (xwem-win-vchild pwin)))
                          (incf min-height (+ xwem-win-min-height (car xwem-win-vertical-delim-width))))))
                    (xwem-frame-selwin frame)))

    ;; Adjust last-xrect according to calculated min-width/height
    (when (< (X-Rect-width last-xrect) min-width)
      (setf (X-Rect-width last-xrect) min-width))
    (when (< (X-Rect-height last-xrect) min-height)
      (setf (X-Rect-height last-xrect) min-height))
    
    (xwem-mouse-grab xwem-cursor-resize (xwem-frame-xwin frame)
                     (Xmask-or XM-ButtonRelease XM-ButtonMotion))
    (unless (eq iresize-mode 'opaque)
      (xwem-misc-outline last-xrect iresize-mode))
    (xwem-unwind-protect
        (while (not done)
          (X-Event-CASE (setq xev (xwem-next-event))
            (:X-ButtonRelease (setq done t))
        
            (:X-MotionNotify
             ;; Update curr-xrect
             (setf (X-Rect-width curr-xrect)
                   (- (X-Event-xmotion-root-x xev) (X-Rect-x curr-xrect)))
             (setf (X-Rect-height curr-xrect)
                   (- (X-Event-xmotion-root-y xev) (X-Rect-y curr-xrect)))

             (when (and (or (>= (X-Rect-width curr-xrect) min-width)
                            (>= (X-Rect-height curr-xrect) min-height))
                        (or (> (abs (- (X-Rect-width curr-xrect) (X-Rect-width last-xrect))) step-x)
                            (> (abs (- (X-Rect-height curr-xrect) (X-Rect-height last-xrect))) step-y)))
               (unless (eq iresize-mode 'opaque)
                 (xwem-misc-outline last-xrect iresize-mode))
               (when (>= (X-Rect-width curr-xrect) min-width)
                 (setf (X-Rect-width last-xrect) (X-Rect-width curr-xrect)))
               (when (>= (X-Rect-height curr-xrect) min-height)
                 (setf (X-Rect-height last-xrect) (X-Rect-height curr-xrect)))
               (if (eq iresize-mode 'opaque)
                   (xwem-frame-set-size frame (X-Rect-width last-xrect) (X-Rect-height last-xrect))
                 (xwem-misc-outline last-xrect iresize-mode))
               ))))
      (xwem-mouse-ungrab)
      (unless (eq iresize-mode 'opaque)
        (xwem-misc-outline last-xrect iresize-mode))

      ;; Apply changes
      (xwem-frame-set-pos frame (X-Rect-x last-xrect) (X-Rect-y last-xrect))
      (xwem-frame-set-size frame (X-Rect-width last-xrect) (X-Rect-height last-xrect)))
    ))

;;;###xwem-autoload
(defun xwem-frame-clients (frame)
  "Make list of all clients FRAME holds."
  (xwem-clients-list (lambda (cl)
                       (eq (xwem-cl-frame cl) frame))))

(define-xwem-face xwem-frame-inner-border-face
  `(((light nonselected) (:foreground "gray80" :background "gray80"))
    ((medium nonselected) (:foreground "gray50" :background "gray50"))
    ((dark nonselected) (:foreground "gray20" :background "gray20"))
    ((light selected) (:foreground "cyan2" :background "cyan2"))
    ((medium selected) (:foreground "royalblue" :background "royalblue"))
    ((dark selected) (:foreground "blue4" :background "blue4")))
  "Face to draw frame's inner border."
  :group 'xwem-frame
  :group 'xwem-faces)

;;; Outline inner border
(defun xwem-frame-draw-inner-border (frame)
  "Draw inner border for FRAME."
  (let* ((bw (xwem-frame-property frame 'inner-border-width))
         (th (or (xwem-frame-property frame 'inner-border-thickness) 1))
         (tag1 (if (xwem-frame-selected-p frame) 'selected 'nonselected))
         (wgc (xwem-face-get-gc 'xwem-frame-inner-border-face (list 'light tag1) frame))
         (bgc (xwem-face-get-gc 'xwem-frame-inner-border-face (list 'dark tag1) frame))
         (gc (xwem-face-get-gc 'xwem-frame-inner-border-face (list 'medium tag1) frame))
         (off th))

    (when (> bw 0)
      (XDrawRectangles (xwem-dpy) (xwem-frame-xwin frame)
                       gc (mapcar (lambda (notused)
                                    (prog1
                                        (make-X-Rect :x off :y off
                                                     :width (- (xwem-frame-width frame) (* 2 off) 1)
                                                     :height (- (xwem-frame-height frame) (* 2 off) 1))
                                      (incf off)))
                                  (make-list (- bw (* 2 th)) nil)))

      (xwem-misc-draw-shadow (xwem-dpy) (xwem-frame-xwin frame)
                             wgc bgc 0 0 (xwem-frame-width frame) (xwem-frame-height frame)
                             th)
      (xwem-misc-draw-shadow (xwem-dpy) (xwem-frame-xwin frame)
                             bgc wgc (- bw th) (- bw th) (- (xwem-frame-width frame) (* 2 (- bw th)))
                             (- (xwem-frame-height frame) (* 2 (- bw th)))
                             th)
      )))

(define-xwem-deffered xwem-frame-deffered-redraw-inner-border (frame)
  "Redraw inner border for selected frame."
  (and (xwem-frame-p frame)
       (xwem-frame-draw-inner-border frame)))

;;; Showing mode
(defvar xwem-frame-showing-mode-hook nil
  "Hooks to call when entering/leaving showing mode.
Each hook called with two arguments: FRAME and MODE,
where mode is one of:

  `enter' - FRAME enters showing mode.
  `leave' - FRAME leaving showing mode.")

(defun xwem-frame-enter-showing-mode (frame)
  "Enable showing mode for FRAME."
  (unless (eq (xwem-frame-property frame 'showing-mode) 'on)
    (let ((fcls (xwem-frame-clients frame)))
      (mapc 'xwem-iconify fcls)

      (xwem-frame-put-prop frame 'showing-mode 'on)
      (run-hook-with-args 'xwem-frame-showing-mode-hook frame 'enter))))

(defun xwem-frame-leave-showing-mode (frame)
  "Disable showing mode."
  (unless (eq (xwem-frame-property frame 'showing-mode) 'off)
    (let ((fcls (xwem-frame-clients frame)))
      (mapcar 'xwem-manage fcls)

      (xwem-frame-put-prop frame 'showing-mode 'off)
      (run-hook-with-args 'xwem-frame-showing-mode-hook frame 'leave))))

;;; Frame properties
(defvar xwem-frame-supported-properties nil
  "*List of supported frame properties.")

(defmacro define-xwem-frame-property (prop doc &rest keys-val)
  "Define new frame property PROP.
DOC is doc-string for property and KEYS-VAL is a list of keyword-value
pairs.  Supported keywords are:

  :type   - Type of property (same as in `defcustom')
  :set    - Function to set property.
  :get    - Function to get property value.
  :notifiers - Functions to call when new value for property is set.
"
  `(progn
     (add-to-list 'xwem-frame-supported-properties (quote ,prop))
     (put (quote ,prop) 'xwem-property-definition (list :doc ,doc ,@keys-val))))

(defun xwem-frame-get-prop-keyword (prop keyword &optional default)
  "Return PROP's KEYWORD value.
Return DEFAULT is KEYWORD not found."
  (plist-get (get prop 'xwem-property-definition) keyword default))

;;;###xwem-autoload
(defun xwem-frame-add-property-notifier (prop notifier)
  (let ((notifiers (xwem-frame-get-prop-keyword prop :notifiers)))
    (if notifiers
        (setcdr (last notifiers) (cons notifier nil))
      (put prop 'xwem-property-definition
           (plist-put (get prop 'xwem-property-definition) :notifiers (list notifier))))))

;;;###xwem-autoload
(defun xwem-frame-set-property (frame prop val)
  "Set FRAME's propertie PROP to VAL.
If FRAME is nil - selected frame is used."
  (unless frame
    (setq frame (xwem-frame-selected)))
  (unless (equal (xwem-frame-property frame prop) val)
    (funcall (xwem-frame-get-prop-keyword prop :set 'xwem-frame-put-prop) frame prop val)
    ;; Call notifiers
    (mapc (lambda (notifier) (funcall notifier frame prop val))
          (xwem-frame-get-prop-keyword prop :notifiers))))

;;;###xwem-autoload
(defun xwem-frame-set-properties (frame props-plist)
  "Set FRAME properties PROPS."
  (while props-plist
    (xwem-frame-set-property frame (car props-plist) (cadr props-plist))
    (setq props-plist (cddr props-plist))))

;;;###xwem-autoload
(defun xwem-frame-property (frame prop)
  "Return value for FRAME's property PROP.
If FRAME is nil - selected frame is used."
  (funcall (xwem-frame-get-prop-keyword prop :get 'xwem-frame-get-prop) (or frame (xwem-frame-selected)) prop))

;;;###xwem-autoload
(defun xwem-frame-properties (&optional frame)
  "Return a list of FRAME's properties.
List is copied, you are free to modify it for your needs.
Note that only supported properties are returned.
If FRAME is omitted - selected frame is used."
  (let ((fplist (xwem-frame-plist (or frame (xwem-frame-selected))))
        rplist)
    (while fplist
      (when (memq (car fplist) xwem-frame-supported-properties)
        (setq rplist (plist-put rplist (car fplist) (cadr fplist))))
      (setq fplist (cddr fplist)))
    rplist))

;; Define some basic properties
(defun xwem-frame-set-title-height (frame prop new-height)
  "Set FRAME's title height property PROP to NEW-HEIGHT."
  (let ((old-height (xwem-frame-property frame prop)))
    (xwem-frame-put-prop frame prop new-height)

    ;; Adjust root window position/size
    (when (and new-height old-height (xwem-frame-rootwin frame))
      (incf (xwem-win-y (xwem-frame-rootwin frame))
            (- new-height old-height))
      (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                               (- (xwem-win-height (xwem-frame-rootwin frame))
                                  (- new-height old-height))
                               nil t))))

(define-xwem-frame-property title-height
  "Frame's title height in pixels."
  :type 'number
  :set 'xwem-frame-set-title-height)

(defun xwem-frame-get-outer-border-width (frame prop)
  "Return FRAME's outer border width."
  (X-Geom-border-width (xwem-frame-xgeom  frame)))

(defun xwem-frame-set-outer-border-width (frame prop val)
  "Return FRAME's outer border width."
  (when (numberp val)
    (setf (X-Geom-border-width (xwem-frame-xgeom  frame)) val)
    (XSetWindowBorderWidth (xwem-dpy) (xwem-frame-xwin frame)
                           (X-Geom-border-width (xwem-frame-xgeom  frame)))))

(define-xwem-frame-property outer-border-width
  "Frame's outer border width in pixels."
  :type 'number
  :get 'xwem-frame-get-outer-border-width
  :set 'xwem-frame-set-outer-border-width)

(defun xwem-frame-set-outer-border-color (frame prop val)
  "Set FRAME's outer border color to VAL."
  (xwem-frame-put-prop frame prop val)

  (when val
    (XSetWindowBorder (xwem-dpy) (xwem-frame-xwin frame)
                      (XAllocColor (xwem-dpy) (XDefaultColormap (xwem-dpy))
                                   (xwem-make-color val)))))

(define-xwem-frame-property outer-border-colol
  "Frame's outer border color."
  :type 'color
  :set 'xwem-frame-set-outer-border-color)

(defun xwem-frame-set-inner-border-width (frame prop val)
  "Set FRAME's inner border property PROP to VAL."
  (unless val
    (setq val 0))

  (xwem-frame-put-prop frame prop val)

  ;; Setup windows
  (when (xwem-frame-rootwin frame)
    (setf (xwem-win-x (xwem-frame-rootwin frame))
          (xwem-frame-property frame 'inner-border-width))
    (setf (xwem-win-y (xwem-frame-rootwin frame))
          (+ (xwem-frame-property frame 'title-height)
             (xwem-frame-property frame 'inner-border-width)))

    (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                             (- (xwem-frame-height frame)
                                (xwem-frame-property frame 'title-height)
                                (xwem-frame-property frame 'inner-border-width)
                                (xwem-frame-property frame 'inner-border-width))
                             t t)
    (xwem-window-set-pixsize (xwem-frame-rootwin frame)
                             (- (xwem-frame-width frame)
                                (xwem-frame-property frame 'inner-border-width)
                                (xwem-frame-property frame 'inner-border-width))
                             t nil)

    (xwem-deffered-funcall 'xwem-frame-draw frame t)))
    
(define-xwem-frame-property inner-border-width
  "Frame's inner border width in pixels."
  :type 'number
  :set 'xwem-frame-set-inner-border-width)

(defun xwem-frame-set-inner-border-thickness (frame prop val)
  "Set FRAME's inner border width PROP to VAL."
  (when (and val (> (* val 2) (xwem-frame-property frame 'inner-border-width)))
    (error 'xwem-error "To large thickness to fill in title-height"))

  (xwem-frame-put-prop frame prop val)
  (when val
    (xwem-deffered-funcall 'xwem-frame-draw frame nil)))

(define-xwem-frame-property inner-border-thickness
  "Frame's inner border thickness."
  :type 'number
  :set 'xwem-frame-set-inner-border-thickness)

(defun xwem-frame-set-background (frame prop val)
  "Set FRAME's background color to VAL."
  (xwem-frame-put-prop frame prop val)

  (when val
    (XSetWindowBackground (xwem-dpy) (xwem-frame-xwin frame)
                          (XAllocColor (xwem-dpy) (XDefaultColormap (xwem-dpy))
                                       (xwem-make-color val)))
    (xwem-deffered-funcall 'xwem-frame-draw frame t)))

(define-xwem-frame-property background
  "Frame's background color."
  :type 'color
  :set 'xwem-frame-set-background)

(defun xwem-frame-set-showing-mode (frame prop val)
  "Set FRAME's showing mode PROP to VAL."
  (xwem-frame-put-prop frame prop val)
  (if val
      (xwem-frame-enter-showing-mode frame)
    (xwem-frame-leave-showing-mode frame)))

(define-xwem-frame-property showing-mode
  "Frame's showing mode."
  :type 'boolean
  :set 'xwem-frame-set-showing-mode)


(provide 'xwem-frame)

;;; xwem-frame.el ends here