Source

xwem / lisp / xwem-frame.el

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

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

;; Author: Zajcev Evgeny <zevlg@yandex.ru>
;; 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


(eval-when-compile
  (require 'xlib-xlib)
  (require 'xwem-misc))
(require 'xlib-xinerama)

;;; 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 '(restricted-sexp :match-alternatives (xwem-misc-colorspec-valid-p))
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-shape 'X-XC-left_ptr
  "*Cursors shape which will be used when pointer is over xwem frame."
  :type (xwem-cursor-shape-choice)
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-foreground-color "#002800"
  "*Cursor's foreground color used when pointer is on xwem's frame."
  :type '(restricted-sexp :match-alternatives ('nil xwem-misc-colorspec-valid-p))
  :group 'xwem-frame)

(defcustom xwem-frame-cursor-background-color "#000000"
  "*Cursor's background color used when pointer is on xwem's frame."
  :type '(restricted-sexp :match-alternatives ('nil xwem-misc-colorspec-valid-p))
  :group 'xwem-frame)

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

(defcustom xwem-frame-defprops
  (list 'inner-border-width 0		;internal border of xwem's frame
	'otter-border-width 0		;
	'title-height 17		; TODO: maybe create 'auto to fit font height?
	'title-thickness 2		;
	)
  "*Default PLIST for frame"
  :type '(restricted-sexp :match-alternatives (valid-plist-p))
  :group 'xwem-frame)

(defface xwem-face-frame-selected-win-selected
  `((t (:foreground "green" :background "green4" :line-width 3)))
  "Face to outline selected window in selected frame."
  :group 'xwem-frame
  :group 'xwem-faces)

(defface xwem-face-frame-selected-win-nonselected
  `((t (:foreground "blue4" :background "blue4" :line-style X-LineDoubleDash)))
  "Face to outline nonselected window in selected frame."
  :group 'xwem-frame
  :group 'xwem-faces)

(defface xwem-face-frame-nonselected-win-selected
  `((t (:foreground "green4" :background "green4" :line-width 2)))
  "Face to outline selected window in nonselected frame."
  :group 'xwem-frame
  :group 'xwem-faces)

(defface xwem-face-frame-nonselected-win-nonselected
  `((t (:foreground "blue4" :background "blue" :line-style X-LineDoubleDash)))
  "Face to outline nonselected window in nonselected frame."
  :group 'xwem-frame
  :group 'xwem-faces)

(defface xwem-face-win-delimeter
  `((t (:foreground "gray80" :background "black")))
  "Face to draw window delimeter."
  :group 'xwem-frame
  :group 'xwem-faces)

(defface xwem-face-win-delimeter-shadow
  `((t (:foreground "gray50" :background "black")))
  "Face to draw shadow of window delimeter."
  :group 'xwem-frame
  :group 'xwem-faces)

(defcustom xwem-frame-iresize-mode 'normal
  "Default type of drawing outlines when resizing frame interactively.
It is not recommeded to use 'opaque resize mode."
  :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)

(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-omit-init-frame nil
  "Non-nil mean no frames will be created on startup."
  :type 'boolean
  :group 'xwem-frame)

(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)

;; Hooks
(defcustom xwem-frame-switch-hook nil
  "Hooks to be called when frame switching occurs (i.e. `xwem-current-frame' changes).
 Function will receive two arguments OLD-FRAME and NEW-FRAME."
  :type 'hook
  :group 'xwem-hooks)

(defcustom xwem-frame-creation-hook nil
  "Hooks called with one argument - frame, when frame just created."
  :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)
  
;; 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-keymap
  (let ((map (make-sparse-keymap)))
    (define-key map [button1] 'xwem-frame-on-delim-resize)
    (define-key map [button3] 'xwem-frame-on-delim-menu)
    
    (set-keymap-parent map xwem-global-map)
    map)
  "Keymap used in frames.")

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

;;;###autoload
(defvar xwem-frames-list nil "All frames xwem owns.")

(defvar xwem-current-frame nil "Current frame.")

;;;###autoload
(defstruct (xwem-frame (:predicate xwem-isframe-p))
  (name "default")                      ; frame name
  xgeom					; frame geometry
  xwin					; X window 
  rootwin				; XWEM's root window for frame
  selwin				; XWEM's selected window
  link-next				; Link to next xwem's frame in linkage
  link-prev				; Link to prev xwem's frame in linkage
  embedded-p				; Non-nil when frame is embedded
  state					; 'mapped, 'unmapped, 'destroyed

  props)				; Properties list

(defstruct xwem-frame-saved
  frame                                 ; nil or `xwem-frame'
  name
  xgeom
  state
  props

  winconfig)
  

;;; Functions
;;;###autoload
(defun xwem-frame-p (frame &optional sig)
  "Return t if FRAME is XWEM frame."
  (let ((isfr (xwem-isframe-p frame)))
    (if (and sig (not isfr) )
	(signal 'wrong-type-argument (list sig 'xwem-frame-p frame))
      isfr)))

;;;###autoload
(defmacro xwem-frame-x (frame)
  `(X-Geom-x (xwem-frame-xgeom ,frame)))
;;;###autoload
(defmacro xwem-frame-y (frame)
  `(X-Geom-y (xwem-frame-xgeom ,frame)))
;;;###autoload
(defmacro xwem-frame-width (frame)
  `(X-Geom-width (xwem-frame-xgeom ,frame)))
;;;###autoload
(defmacro xwem-frame-height (frame)
  `(X-Geom-height (xwem-frame-xgeom ,frame)))

(defsetf xwem-frame-x (frame) (x)
  `(setf (X-Geom-x (xwem-frame-xgeom ,frame)) ,x))
(defsetf xwem-frame-y (frame) (y)
  `(setf (X-Geom-y (xwem-frame-xgeom ,frame)) ,y))
(defsetf xwem-frame-width (frame) (width)
  `(setf (X-Geom-width (xwem-frame-xgeom ,frame)) ,width))
(defsetf xwem-frame-height (frame) (height)
  `(setf (X-Geom-height (xwem-frame-xgeom ,frame)) ,height))

(defsubst xwem-frame-alive-p (frame)
  "Return non-nil if FRAME is alive XWEM frame, i.e. which is in `xwem-frames-list'."
  (member frame xwem-frames-list))

(defsubst xwem-frame-mapped-p (frame)
  "Return non-nil if xwem FRAME is mapped."
  (and (xwem-frame-p frame)
       (eq (xwem-frame-state frame) 'mapped)))

(defsubst xwem-frame-dedicated-p (frame)
  "Return non-nil if FRAME is dedicated to client."
  (xwem-frame-get-prop frame 'dedicated-p))

(defun xwem-frame-unmap (frame)
  "Unmap frame FRAME."
  (when (xwem-frame-mapped-p frame)
    (XSelectInput (xwem-dpy) (xwem-frame-xwin frame) 0)
    (XUnmapWindow (xwem-dpy) (xwem-frame-xwin frame))
    (setf (xwem-frame-state frame) 'unmapped)))

(defun xwem-frame-map (frame)
  "Map frame FRAME."
  (unless (xwem-frame-mapped-p frame)
    (XSelectInput (xwem-dpy) (xwem-frame-xwin frame) xwem-frame-ev-mask)

    (XMapWindow (xwem-dpy) (xwem-frame-xwin frame))
    (setf (xwem-frame-state frame) 'mapped)))

;;;###autoload
(defun xwem-frame-selected ()
  "Return selected frame."
  xwem-current-frame)

;;;###autoload
(defun xwem-frame-selected-p (frame)
  "Return non-nil if FRAME is selected."
  (eq frame (xwem-frame-selected)))

(defun xwem-frame-embedded-for-frame (frame)
  "Return XWEM frame for which FRAME is embedded."
  (let* ((cl (and (xwem-frame-p frame)
		  (xwem-find-client (xwem-frame-xwin frame))))
	 (rv (and (xwem-cl-p cl)
		  (xwem-cl-frame cl))))
    rv))

(defun xwem-frame-embedded-pop (frame)
  "Pop embedded FRAME, see also `xwem-cl-pop-to-client'."
  (let* ((cl (xwem-find-client (xwem-frame-xwin frame)))
	 (clfr (and (xwem-cl-p cl) (xwem-cl-frame cl)))
	 (clw (and (xwem-cl-p cl) (xwem-cl-win cl))))

    (when (null clfr)
      (setq clfr (xwem-frame-selected)))
    (when (null clw)
      (setq clw (xwem-win-selected)))

    ;; Raise frame in which we are embedded.
    (when (not (xwem-frame-selected-p clfr))
      (if (xwem-frame-embedded-p clfr)
	  (xwem-frame-embedded-pop clfr)
	(xwem-frame-raise clfr)))

    (when (not (xwem-win-selected-p clw))
      (xwem-window-select clw))

    (xwem-manda-manage cl clw)))

(defun xwem-frame-unembedd (frame)
  "Unembedd FRAME."
  (when (xwem-frame-embedded-p frame)
    (let* ((cl (xwem-find-client (xwem-frame-xwin frame)))
           (ngeom (nthcdr 4 (XTranslateCoordinates (xwem-dpy) (xwem-frame-xwin frame)
                                                   (xwem-rootwin) (xwem-frame-x frame)
                                                   (xwem-frame-y frame)))))
      ;; Remove clients stuff
      (X-Win-EventHandler-rem (xwem-cl-xwin cl) 'xwem-cl-events-handler)
      (xwem-cl-hdestroy-notify cl nil)

      (XReparentWindow (xwem-dpy) (xwem-frame-xwin frame) (xwem-rootwin)
                       (car ngeom) (cadr ngeom))
      (setf (xwem-frame-embedded-p frame) nil)
      (xwem-frame-set-pos frame (car ngeom) (cadr ngeom))
      )))

;;;###autoload
(defun xwem-frame-select (frame &optional dnr-hooks)
  "Set FRAME as selected frame.
If DNR-HOOKS is non-nil, than do not run hooks."

  (let ((ofr (xwem-frame-selected))
	(nfr frame))
    ;; If FRAME is embedded frame we should raise frame in whose
    ;; window FRAME is embedded.
    (when (xwem-frame-embedded-p frame)
      (xwem-frame-embedded-pop frame))

    (setq xwem-current-frame frame)

    ;; Now raise and set input focus to FRAME
    (xwem-frame-raise nfr)
    (xwem-focus-set frame)

    (when (not dnr-hooks)
      (run-hook-with-args 'xwem-frame-switch-hook ofr nfr))))

(defun xwem-frame-maybe-autoiconify (ofr nfr)
  "This function aimed to be used in `xwem-frame-switch-hook'.
According to `xwem-frame-autoiconify-mode' it possible iconfies OFR
frame."
  (when (and xwem-frame-autoiconify-mode (not (eq ofr nfr))
             (or (eq xwem-frame-autoiconify-mode 'always)
                 (and (eq xwem-frame-autoiconify-mode 'intersect) (xwem-frame-p nfr)
                      (X-Rect-intersect-p (X-Geom-to-X-Rect (xwem-frame-xgeom nfr))
                                          (X-Geom-to-X-Rect (xwem-frame-xgeom ofr))))))
    (xwem-frame-unmap ofr)))

(defun xwem-frame-select-defhook (ofr nfr)
  "Default hook for `xwem-frame-switch-hook'.
NOT USED."
  ;; Here is OFR may be invisible, but we redraw it in case it is
  ;; visible.
  (X-Dpy-send-excursion (xwem-dpy)
    (when (and (xwem-frame-p ofr)
	       (xwem-frame-alive-p ofr))
        (xwem-frame-redraw ofr))

    (when (and (xwem-frame-p nfr)
	       (xwem-frame-alive-p nfr))
      (xwem-frame-redraw nfr))
    ))

;;;###autoload
(defun xwem-make-frame-1 (&optional embedded-p params props non-select)
  "Create new frame with optional frame properties PROPS.
If EMBEDDED-p is non-nil than create embedded frame.
If NON-SELECT is non-nil then do not select newly created frame to be
current."
  (let* ((fplist (copy-sequence xwem-frame-defprops))
	 (frame (apply 'make-xwem-frame params)))

    (setf (xwem-frame-embedded-p frame) embedded-p)

    ;; setup properties
    (while props
      (setq fplist (plist-put fplist (car props) (cadr props)))
      (setq props (cddr props)))
    (setf (xwem-frame-props frame) fplist)

    (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))))

    ;; We should not process events before frame's window events
    ;; handler installed
    (setf (xwem-frame-xwin frame)
	  (XCreateWindow
	   (xwem-dpy) nil
	   (xwem-frame-x frame)
	   (xwem-frame-y frame)
	   (xwem-frame-width frame)
	   (xwem-frame-height frame)
	   (xwem-frame-get-prop frame 'inner-border-width)
	   nil				;DefaultDepth
	   nil				;CopyFromParent
	   nil				;CopyFromParent
	   (make-X-Attr :override-redirect 0
			:background-pixel (XAllocNamedColor
					   (xwem-dpy) (XDefaultColormap (xwem-dpy))
					   xwem-frame-background)
			:border-pixel (XBlackPixel (xwem-dpy))
                        :backing-store X-WhenMapped
			:cursor xwem-frame-cursor
			:event-mask xwem-frame-ev-mask)))

    (let ((fwin (xwem-frame-xwin frame))
	  parwin)
      (X-Win-put-prop fwin 'xwem-frame frame)

      ;; Setup events handlers
      (X-Win-EventHandler-add-new fwin 'xwem-frame-events-handler 200)
      (X-Win-EventHandler-add-new fwin 'xwem-ev-reconfig 90 (list X-ConfigureRequest))

      ;; XXX Setup WM_XXX stuff
      (XSetWMProtocols (xwem-dpy) fwin
                       (list (X-Atom-find-by-name (xwem-dpy) "WM_DELETE_WINDOW")))

      (XSetWMClass (xwem-dpy) fwin
		   '("xwem-frame" "xwem-frame"))
      (XSetWMName (xwem-dpy) fwin
		  (format "xwem-frame[%d]" (length xwem-frames-list)))

      ;; Initialize root window
      (setq parwin (xwem-win-new (list :frame frame) nil))

      (setf (xwem-win-x parwin)
	    (xwem-frame-get-prop frame 'otter-border-width))
      (setf (xwem-win-y parwin)
	    (+ (xwem-frame-get-prop frame 'title-height)
	       (xwem-frame-get-prop frame 'otter-border-width)))
      (setf (xwem-win-width parwin)
	    (- (xwem-frame-width frame)
	       (* 2 (xwem-frame-get-prop frame 'otter-border-width))))
      (setf (xwem-win-height parwin)
	    (- (xwem-frame-height frame)
	       (xwem-frame-get-prop frame 'title-height)
	       (* 2 (xwem-frame-get-prop frame 'otter-border-width))))

      (setf (xwem-frame-selwin frame) parwin)
      (setf (xwem-frame-rootwin frame) parwin)

      ;; Add to the end of frames list
      (if xwem-frames-list
	  (setcdr (last xwem-frames-list) (list frame))
	(setq xwem-frames-list (list frame)))

      ;; Handle as client, i.e. make frame to be embedded
      (when embedded-p
	(xwem-make-client (xwem-frame-xwin frame)))

      ;; Finally map and maybe select newly created frame
      (xwem-frame-map frame)

      ;; TODO:
      ;;   Actually here we should wait for an Expose event, according to
      ;;   some standard.
;      (unless embedded-p
;	;; Not for embedded frames
;	(XIfEvent (xwem-dpy)
;		  (lambda (xev)
;		    (and (= (X-Event-type xev) X-Expose)
;			 (= (X-Win-id (X-Event-win xev)) (X-Win-id fwin))))))

      ;; Install grabbing
      (unless embedded-p
	(xwem-kbd-install-grab xwem-global-map fwin))
      
      (unless non-select
	(xwem-frame-select frame))

      ;; Now draw frames contents.
;      (unless embedded-p
;	(xwem-frame-draw frame nil))
      )

    ;; 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-frame xwem-minibuffer)
				 (+ (* 2 (frame-property (xwem-minib-frame xwem-minibuffer) 'border-width))
                                    (frame-pixel-height (xwem-minib-frame xwem-minibuffer)))
			       0))))

    (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 nil (list :xgeom (X-Rect-to-X-Geom xrect)) nil 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)))
	ngeom)

    (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)))

;;;###autoload
(defun xwem-init-frames ()
  "xwem frames initializer."
  (xwem-message 'msg "Initializing frames ... wait")
  (setq xwem-frames-list nil)
  (setq xwem-current-frame nil)

  ;; Create cursor used while pointer is over xwem frame
  (setq  xwem-frame-cursor (xwem-make-cursor (eval xwem-frame-cursor-shape)
					     xwem-frame-cursor-foreground-color
					     xwem-frame-cursor-background-color))

  ;; Default swich hook
  (add-hook 'xwem-frame-switch-hook 'xwem-frame-select-defhook)
  ;; Add autoiconifier hook
  (add-hook 'xwem-frame-switch-hook 'xwem-frame-maybe-autoiconify)

  (unless xwem-frame-omit-init-frame
    ;; Xinerama stuff
    (let ((xin (X-XIneramaQueryScreens (xwem-dpy))))
      (if (car xin)
          ;; XInerama enabled
          (while (setq xin (cdr xin))
            (xwem-init-frame-at-rect (car xin)))

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

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

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

;;;###autoload(autoload 'xwem-make-frame "xwem-frame" "" t)
(define-xwem-command xwem-make-frame (arg)
  "Create new XWEM frame.
If ARG is non-nil then create embedded frame."
  (xwem-interactive "P")

  (xwem-make-frame-1 arg))

(defun xwem-frame-cl (frame)
  "Return currently active xwem client in FRAME."
  (xwem-win-cl (xwem-frame-selwin frame)))

;;;###autoload
(defun xwem-find-frame (win)
  ;;XXX rewrite me
  (let ((flist xwem-frames-list)
	(rf nil))
    (while flist
      (if (= (X-Win-id win)
	     (X-Win-id (xwem-frame-xwin (car flist))))
	  (progn
	    (setq rf (car flist))
	    (setq flist nil))
	(setq flist (cdr flist))))
    rf))

;;;###autoload
(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 (nth 0 xwem-frames-list)))
    oframe))

;;;###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-find-client (xwem-frame-xwin frame))))
	  (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))

;;;###autoload
(defun xwem-frame-num (frame)
  "Return FRAME index in `xwem-frames-list'."
  (xwem-frame-p frame 'xwem-frame-num)
  (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
(defun xwem-frame-set-pos (frame new-x new-y)
  "Set FRAME position at NEW-X and NEW-Y."

  (when (xwem-frame-embedded-p frame)
    (error "Can't change position of embedded frame"))

  (setf (xwem-frame-x frame) new-x)
  (setf (xwem-frame-y frame) new-y)

  (XMoveWindow (xwem-dpy) (xwem-frame-xwin frame) new-x new-y)

  ;; Now run on-move hooks
  (run-hook-with-args 'xwem-frame-move-hook frame))
  
;;;###autoload
(defun xwem-frame-set-size (frame new-width new-height)
  "Resize FRAME to NEW-WIDTH and NEW-HEIGHT."

  (when (xwem-frame-embedded-p frame)
    (error "Can't change size of embedded frame"))

  (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) t nil)
  (xwem-window-set-pixsize (xwem-frame-rootwin frame)
			   (- (xwem-frame-height frame)
			      (xwem-frame-get-prop frame 'title-height)) t t)
  (XResizeWindow (xwem-dpy) (xwem-frame-xwin frame) new-width new-height)

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

;;; Frame Events handling
(defun xwem-frame-hexpose (frame xev)
  "Expose event handler."
  (X-Dpy-log (xwem-dpy) "xwem-frame-expose: Exposure event count: %S\n"
	     '(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 (delete 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-frame-select 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-find-client (xwem-frame-xwin frame))))

    ;; Block events handling
    (setf (X-Win-event-handlers (xwem-frame-xwin frame)) nil)

    ;; 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-hdestroy-notify embed-cl nil))

    ;; 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
	rwwid rwhei)

    (when (not (= owid nwid))
      (setf (xwem-frame-width frame) nwid)
      (setq rwwid nwid)
      (xwem-window-set-pixsize (xwem-frame-rootwin frame) rwwid t nil))

    (when (not (= ohei nhei))
      (setf (xwem-frame-height frame) nhei)
      (setq rwhei (- nhei (xwem-frame-get-prop frame 'title-height)))
      (xwem-window-set-pixsize (xwem-frame-rootwin frame) rwhei t t))

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

(defun xwem-frame-hclient (frame xev)
  "FRAME just received XClientMessage event XEV."
  (xwem-message 'info "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 (and (X-Atom-p wmda) (string= (X-Atom-name wmda) "WM_DELETE_WINDOW"))
             (xwem-frame-total-remove frame))))
        ))
  
(defun xwem-frame-hkeybutton (frame xev)
  "On FRAME handle KeyPress, ButtonPress or ButtonRelease event XEV."
  (let ((xwem-override-global-map xwem-frame-keymap))
    (xwem-kbd-handle-keybutton xev)))

(defun xwem-frame-events-handler (xdpy win xev)
  "Event handler for frame."
  (X-Dpy-log (xwem-dpy) "XWEM-FRAME-EVENTS-HANDLER: ev=%S, win=%S\n"
	     '(X-Event-name xev) '(X-Win-id win))

  (let ((frame (xwem-frame-find 'xwin win)))

    (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 (xwem-frame-find 'xwin (X-Event-xdestroywindow-window xev)))
	 (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 (xwem-frame-find 'xwin (X-Event-xconfigure-window xev)))
	 (when (xwem-frame-p frame)
	   (xwem-frame-hconfigure frame xev))
         )

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

;;;; Frame Events handling ends here

;;;###autoload
(defun xwem-frame-redraw (frame)
  "Redraw FRAMEs windows outlines."
  (let* ((sw-gc (xwem-face-get-gc
                 (if (xwem-frame-selected-p frame)
                     'xwem-face-frame-selected-win-nonselected
                   'xwem-face-frame-nonselected-win-nonselected)))
         (cw-gc (xwem-face-get-gc
                 (if (xwem-frame-selected-p frame)
                     'xwem-face-frame-selected-win-selected
                   'xwem-face-frame-nonselected-win-selected))))

    (xwem-win-map
     (lambda (win)
       (let* ((cgc (if (xwem-win-selected-p win) cw-gc sw-gc))
              (hthi (X-Gc-line-width cgc)))

         (unless (and (numberp hthi) (not (zerop hthi)))
	   (setq hthi 1))
	 
	 ;; XXX is Clearing window area is really needed?
	 (XClearArea (xwem-dpy) (xwem-frame-xwin (xwem-win-frame win))
		     (+ (xwem-win-x win) hthi)
		     (+ (xwem-win-y win) hthi)
		     (- (xwem-win-width win) hthi)
		     (- (xwem-win-height win) hthi)
		     nil)

	 (XDrawRectangle (xwem-dpy)
			 (xwem-frame-xwin frame)
			 cgc
			 (+ (xwem-win-x win) (/ hthi 2))
			 (+ (xwem-win-y win) (/ hthi 2))
			 (- (xwem-win-width win) hthi)
			 (- (xwem-win-height win) hthi))))

     (xwem-frame-selwin frame)))

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

;;;###autoload
(defun xwem-frame-draw (frame full)
  "Draw FRAME. If FULL is t then fully redraw it, i.e. ClearWindow first."
  (let ((win (xwem-frame-xwin frame)))
    (X-Dpy-send-excursion (xwem-dpy)
      (when full
	(XClearArea (xwem-dpy) win 0 0
		    (xwem-frame-width frame)
		    (xwem-frame-height frame) nil))

      ;; Outile windows
      (xwem-frame-redraw frame)

      ;; Delims
      (xwem-win-draw-delims (xwem-frame-rootwin frame))
      )))

;;; Frame configuration section
;;;###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))

;;;###autoload
(defun xwem-frame-configuration ()
  "Return current xwem frame configuration."
  (cons 'xwem-frame-configuration
        (mapcar (lambda (frame)
                  (make-xwem-frame-saved :frame frame
                                         :name (purecopy (xwem-frame-name frame))
                                         :xgeom (purecopy (xwem-frame-xgeom frame))
                                         :state (purecopy (xwem-frame-state frame))
                                         :props (purecopy (xwem-frame-props frame))
                                         :winconfig (xwem-window-configuration frame)))
                xwem-frames-list)))

;;;###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))
                    (setf (xwem-frame-props frame) (xwem-frame-saved-props 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))
    ))

;;;###autoload
(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 "Not an xwem frame configuration" config))

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

  (let* ((ccf (copy-sequence config))
         (find-file-hooks nil)          ; omit autoinsert and others
         (buf (find-file-noselect file))
         wcf nprops)
    (with-current-buffer buf
      (erase-buffer buf)
      (insert "(setq xwem-frame-dumped-config (list 'xwem-frame-configuration\n")
      (mapc (lambda (sfr)
              (setf (xwem-frame-saved-frame sfr) nil)

              ;; Adjust properties
              (setq nprops (xwem-frame-saved-props sfr))
              (setf (xwem-frame-saved-props sfr) nil)
              (mapc (lambda (prop)
                      (plist-put (xwem-frame-saved-props sfr) prop
                                 (plist-get nprops prop)))
                    '(inner-border-width otter-border-width title-height title-thickness))

              ;; 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")
      (save-buffer)
      )))

;;;###autoload
(defun xwem-frame-config-restore (&optional file)
  "Restore saved frames configuration from FILE.
Default FILE is ~/.xwem/xwem-configs.el"
  (declare (special xwem-frame-dumped-config))

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

  (setq xwem-frame-dumped-config nil)
  (load-file file)
  (unless (xwem-frame-configuration-p xwem-frame-dumped-config)
    (error "no configs stored"))

  (let (frame-to-select)
    (mapc (lambda (sfr)
            (let ((swin (xwem-frame-saved-winconfig sfr))
                  nframe)
              (setq nframe (xwem-make-frame-1 nil (list :name (xwem-frame-saved-name sfr)
                                                        :xgeom (xwem-frame-saved-xgeom sfr)
                                                        :props (xwem-frame-saved-props sfr))
                                              nil 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-frame-select frame-to-select))
    ))

;;; Frame configuration section ends here

;;;###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)))

  (XLowerWindow (xwem-dpy) (xwem-frame-xwin frame)))

;;;###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)
  (XRaiseWindow (xwem-dpy) (xwem-frame-xwin frame))
  )

;;;###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 (member (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)))

    (if (xwem-frame-p frame)
       (xwem-frame-select frame)

      (xwem-message 'warn "Can't switch to `%S' frame" frame))))

;;;###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 (member (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))))

    (if (xwem-frame-p frame)
       (xwem-frame-select frame)

      (xwem-message 'warn "Can't switch to `%S' 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-frame-select frame)
	  (xwem-message 'warn "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-frame-select frame t)))
    ))

;;;###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 'warn "No such %S frame." (abs arg))

	  ;; Select linkage
	  (xwem-frame-linkage-map
	   frame
	   (lambda (fr)
	     (xwem-frame-raise fr)))
	  (xwem-frame-select frame)))
    (xwem-message 'warn "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))
  )

;;;###autoload(autoload 'xwem-frame-del-win "xwem-frame" "" t)
(define-xwem-command xwem-frame-del-win (&optional window)
  "Removes WINDOW from FRAME.
If FRAME ommited then selected frame assumed.
If WINDOW ommitted selected window will be used."
  (xwem-interactive)

  (let* ((del-frame (or (and window (xwem-win-frame window)) (xwem-frame-selected)))
	 (del-win (or window (xwem-frame-selwin del-frame))))

    (if (xwem-win-only-one-p del-win)
	(xwem-message 'warn "Can't delete window, because it is only one.")

      (xwem-window-delete del-win)
      (xwem-frame-redraw del-frame))
    ))

;;;###autoload(autoload 'xwem-frame-del-others "xwem-frame" "" t)
(define-xwem-command xwem-frame-del-others (&optional window)
  "Remove all xwem windows other then WINDOW."
  (xwem-interactive)

  (let* ((del-frame (or (and window (xwem-win-frame window)) (xwem-frame-selected)))
	 (del-win (or window (xwem-frame-selwin del-frame)))
	 (cl (xwem-win-cl del-win)))

    (xwem-window-delete-others del-win)

    (xwem-frame-redraw del-frame)

    ;; If there was client I think it need refitting
    (when (xwem-cl-p cl)
      (xwem-manda-refit cl))
    ))

;;;###autoload(autoload 'xwem-frame-split-horiz "xwem-frame" "" t)
(define-xwem-command xwem-frame-split-horiz (arg &optional frame)
  "Split FRAME horizontally.
Prefix argument specifies how many pixels splitted window will be after split."
  (xwem-interactive "p")

  (let* ((sp-frame (or frame (xwem-frame-selected))))
    (when (xwem-frame-dedicated-p sp-frame)
      (error "Can't split dedicated frame."))

    (xwem-win-split (xwem-frame-selwin sp-frame) 'horizontal arg)
    (xwem-frame-redraw sp-frame)
    ))

;;;###autoload(autoload 'xwem-frame-split-vert "xwem-frame" "" t)
(define-xwem-command xwem-frame-split-vert (arg &optional frame)
  "Split FRAME vertically.
Prefix argument specifies how many pixels splitted window will be after split."
  (xwem-interactive "p")

  (let* ((sp-frame (or frame (xwem-frame-selected))))
    (when (xwem-frame-dedicated-p sp-frame)
      (error "Can't split dedicated frame."))
    (xwem-win-split (xwem-frame-selwin sp-frame) 'vertical arg)
    (xwem-frame-redraw sp-frame)
    ))

;;;###autoload(autoload 'xwem-frame-win-enlarge-hor "xwem-frame" "" t)
(define-xwem-command xwem-frame-win-enlarge-hor (n &optional win)
  "Enlarge WIN or selected window N pixels horizontaly."
  (xwem-interactive "p")

  (xwem-window-enlarge n nil (or win (xwem-win-selected)))
  (xwem-frame-draw (xwem-win-frame (or win (xwem-win-selected))) nil))

;;;###autoload(autoload 'xwem-frame-win-enlarge-ver "xwem-frame" "" t)
(define-xwem-command xwem-frame-win-enlarge-ver (n &optional win)
  "Enlarge WIN or selected window N pixels horizontaly."
  (xwem-interactive "p")

  (xwem-window-enlarge n t (or win (xwem-win-selected)))
  (xwem-frame-draw (xwem-win-frame (or win (xwem-win-selected))) nil))

(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 "Bad DIRECTION in xwem-frame-goto, should be one of 'next or 'prev")))
      (setq n (1- n)))

    (xwem-window-select cwin)
    (xwem-frame-redraw gframe))
  )

;;;###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 "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 nil
				 (list :xgeom
				       (make-X-Geom :x (if vertp samex (+ samex xoff))
						    :y (if vertp (+ samey yoff) samey)
						    :width nwi
						    :height nhe))
				 nil
				 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))

;;;###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))

;;;###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 account 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-pos frame (xwem-frame-x frame) (xwem-frame-y frame))
    (xwem-frame-set-size frame (xwem-frame-width frame) (xwem-frame-height frame))

    ;; XXX Raise?
;    (XRaiseWindow (xwem-dpy) (xwem-frame-xwin frame))

    ;; XXX uninstall linkage if any
    (xwem-frame-link-remove frame)
    ))

;;;###autoload(autoload 'xwem-frame-transpose "xwem-frame" "" t)
(define-xwem-command xwem-frame-transpose (arg)
  "Transpose frames ARG times."
  (xwem-interactive "p")
  (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))

;;;###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-frame-select oframe))

      (xwem-frame-unmap frame)
      )))

;;;###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)
      )))

;;;###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))

;; 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)))

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

      (while (xwem-win-p vc)
	;; For vertical split
	(when (xwem-win-p (xwem-win-next 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)))
            (throw 'found t))
          (when (xwem-frame-in-delim-p vc x y)
            (throw 'found t)))
	(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-root-x xwem-last-xevent))
         (y (X-Event-xbutton-root-y xwem-last-xevent))
         (frame (xwem-frame-at x y))
         (in-xy (xwem-frame-in-delim-p (xwem-frame-rootwin frame) x y)))

    (when in-xy
      ;; TODO: Drag delimiter to resize window
      )
    ))

;;;###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-root-x xwem-last-xevent))
         (y (X-Event-xbutton-root-y xwem-last-xevent))
         (frame (xwem-frame-at x y))
         (in-xy (xwem-frame-in-delim-p (xwem-frame-rootwin frame) x y)))

    (when in-xy
      (xwem-popup-function-menu nil)
      )
    ))

;;;###autoload(autoload 'xwem-frame-imove "xwem-frame" "" t)
(define-xwem-command xwem-frame-imove (&optional step)
  "Interactively move FRAME."
  (xwem-interactive)

  (unless (or (interactive-p)
              (= (X-Event-type xwem-last-xevent) X-ButtonPress))
    (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-frame-find 'xwin (X-Event-win xwem-last-xevent))
                    (xwem-frame-at srx sry)))
         (imove-mode (funcall xwem-frame-imoveresize-mode-function frame 'move))
         (last-xrect (X-Geom-to-X-Rect (xwem-frame-xgeom frame)))
         (curr-xrect (copy-sequence last-xrect))
         moving done xev)

    (if (not (xwem-frame-p frame))
        (error "Click on non-XWEM frame")

      (unless step
        (setq step 1))                  ; XXX

      (xwem-mouse-grab xwem-cursor-move (xwem-frame-xwin frame)
                       (Xmask-or XM-ButtonRelease XM-ButtonMotion))
      (unwind-protect
          (while (not done)
            (setq xev (xwem-next-event))
            (X-Event-CASE xev
              (: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) srx)))
               (setq srx (X-Event-xmotion-root-x xev))
               (setf (X-Rect-y curr-xrect)
                     (+ (X-Rect-y curr-xrect) (- (X-Event-xmotion-root-y xev) sry)))
               (setq sry (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))
                 (if moving
                     (unless (eq imove-mode 'opaque)
                       (XGrabServer (xwem-dpy))
                       (xwem-misc-outline last-xrect imove-mode))
                   (setq moving t))

                 (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))
                 ))
              ))

        (when moving
          (unless (eq imove-mode 'opaque)
            (xwem-misc-outline last-xrect imove-mode)
            (XUngrabServer (xwem-dpy)))

          ;; 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-mouse-ungrab))
      )))

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

  (unless (or (interactive-p)
              (= (X-Event-type xwem-last-xevent) X-ButtonPress))
    (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-frame-find 'xwin (X-Event-win 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 (- srx (xwem-frame-y frame)))))
         (curr-xrect (copy-sequence last-xrect))
         moving done xev)

    (if (not (xwem-frame-p frame))
        (error "Click on non-XWEM frame")

      (unless step
        (setq step 1))                  ; XXX

      (xwem-mouse-grab xwem-cursor-move (xwem-frame-xwin frame)
                       (Xmask-or XM-ButtonRelease XM-ButtonMotion))
      (unwind-protect
          (while (not done)
            (setq xev (xwem-next-event))
            (X-Event-CASE xev
              (: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 (or (> (abs (- (X-Rect-width curr-xrect) (X-Rect-width last-xrect))) step)
                         (> (abs (- (X-Rect-height curr-xrect) (X-Rect-height last-xrect))) step))
                 (if moving
                     (unless (eq iresize-mode 'opaque)
                       (XGrabServer (xwem-dpy))
                       (xwem-misc-outline last-xrect iresize-mode))
                   (setq moving t))

                 (setf (X-Rect-width last-xrect) (X-Rect-width curr-xrect))
                 (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 curr-xrect iresize-mode))
                 ))
              ))

        (when moving
          (unless (eq iresize-mode 'opaque)
            (xwem-misc-outline last-xrect iresize-mode)
            (XUngrabServer (xwem-dpy)))

          ;; 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-mouse-ungrab))
      )))

;;;###autoload
(defun xwem-frame-make-cl-list (frame)
  "Make list of all clients FRAME holds."
  (let ((rcls nil))
    (xwem-win-map (lambda (win)
		    (setq rcls (nconc rcls (xwem-win-make-cl-list win))))
		  (xwem-window-next (xwem-frame-selwin frame)))
    rcls))


(provide 'xwem-frame)

;;; xwem-frame.el ends here