This file is indexed.

/usr/lib/perl5/Prima/MDI.pm is in libprima-perl 1.28-1.1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

   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
#  Copyright (c) 1997-2002 The Protein Laboratory, University of Copenhagen
#  All rights reserved.
#
#  Redistribution and use in source and binary forms, with or without
#  modification, are permitted provided that the following conditions
#  are met:
#  1. Redistributions of source code must retain the above copyright
#     notice, this list of conditions and the following disclaimer.
#  2. Redistributions in binary form must reproduce the above copyright
#     notice, this list of conditions and the following disclaimer in the
#     documentation and/or other materials provided with the distribution.
#
#  THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
#  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
#  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
#  ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
#  FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
#  DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
#  OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
#  HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
#  LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
#  OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
#  SUCH DAMAGE.
#
#  Created by Dmitry Karasik <dk@plab.ku.dk>
#
#  $Id: MDI.pm,v 1.28 2007/09/13 15:12:24 dk Exp $
package Prima::MDI;

# contains:
#   MDI
#   MDIOwner
#   MDIWindowOwner

use strict;
use Prima::Classes;

package Prima::MDIMethods;

sub mdi_activate
{
	my $self = $_[0];
	my @m    = $self-> mdis;
	for ( @m) {
		next unless $_-> visible;
		$_-> repaint_title;
	}
}

sub mdis
{
	return grep {$_-> isa('Prima::MDI')?$_:0} $_[0]-> widgets
}

sub arrange_icons
{
	my @m = $_[0]-> mdis;
	$m[0]-> arrange_icons if $m[0];
}

sub cascade
{
	my @m = $_[0]-> mdis;
	$m[0]-> cascade if $m[0];
}

sub tile
{
	my @m = $_[0]-> mdis;
	$m[0]-> tile if $m[0];
}

package Prima::MDIWindowOwner;
use vars qw(@ISA);
@ISA = qw( Prima::Window Prima::MDIMethods);

package Prima::MDIOwner;
use vars qw(@ISA);
@ISA = qw( Prima::Widget Prima::MDIMethods);

sub profile_default
{
	my $def = $_[ 0]-> SUPER::profile_default;
	my %prf = (
		growMode    => gm::Client,
	);
	@$def{keys %prf} = values %prf;
	return $def;
}

package mbi;

use constant SystemMenu => bi::SystemMenu;
use constant Minimize   => bi::Minimize;
use constant Maximize   => bi::Maximize;
use constant TitleBar   => bi::TitleBar;
use constant Close      => bi::TitleBar << 1;
use constant All        => bi::All | Close;

package Prima::MDI;
use vars qw(@ISA);
@ISA = qw(Prima::Widget);

use Prima::StdBitmap;

{
my %RNT = (
	%{Prima::Widget-> notification_types()},
	Activate      => nt::Default,
	Deactivate    => nt::Default,
	WindowState   => nt::Default,
);

sub notification_types { return \%RNT; }
}

use constant cMIN        => 0;
use constant cMAX        => 1;
use constant cCLOSE      => 2;
use constant cRESTORE    => 3;
use constant pMIN        => 4;
use constant pMAX        => 5;
use constant pCLOSE      => 6;
use constant pRESTORE    => 7;


sub profile_default
{
	my $def = $_[ 0]-> SUPER::profile_default;
	my %prf = (
		width                 => 150,
		heigth                => 150,
		selectable            => 1,
		borderIcons           => mbi::All,
		borderStyle           => bs::Sizeable,
		font                  => Prima::Application-> get_caption_font,
		selectingButtons      => 0,
		icon                  => 0,
		iconMin               => Prima::StdBitmap::image( sbmp::Min),
		iconMax               => Prima::StdBitmap::image( sbmp::Max),
		iconClose             => Prima::StdBitmap::image( sbmp::Close),
		iconRestore           => Prima::StdBitmap::image( sbmp::Restore),
		iconMinPressed        => Prima::StdBitmap::image( sbmp::MinPressed),
		iconMaxPressed        => Prima::StdBitmap::image( sbmp::MaxPressed),
		iconClosePressed      => Prima::StdBitmap::image( sbmp::ClosePressed),
		iconRestorePressed    => Prima::StdBitmap::image( sbmp::RestorePressed),
		ownerFont             => 0,
		dragMode              => undef,
		tabStop               => 0,
		tileable              => 1,
		titleHeight           => 0, # use system-default
		transparent           => 0,
		widgetClass           => wc::Window,
		windowState           => ws::Normal,
		clientClass           => 'Prima::Widget',
		clientProfile         => {},
		firstClick            => 1,
		popupItems            => [
			[ restore => '~Restore'  => 'Ctrl+F5' => '^F5' => q(restore)],
			[ min     => 'Mi~nimize' => q(minimize)],
			[ max     => 'Ma~ximize' => 'Ctrl+F10' => '^F10' => q(maximize)],
			[ move    => '~Move' => q(keyMove)],
			[ size    => '~Size' => q(keySize)],
			[],
			[ close   => '~Close' => 'Ctrl+F4' => '^F4' => sub { $_[0]-> close; } ],
		],
	);
	@$def{keys %prf} = values %prf;
	return $def;
}

sub init
{
	my $self = shift;
	for ( qw( borderStyle borderIcons icon windowState))
		{ $self-> {$_} = -1; }
	for ( qw( pressState iconsAtRight border tileable titleHeight titleY))
		{ $self-> {$_} = 0; }
	my %profile = $self-> SUPER::init(@_);
	$self-> {zoomGrowMode} = $profile{growMode};
	for ( qw( titleHeight borderStyle borderIcons icon tileable windowState dragMode
		iconMin iconMax iconClose iconRestore iconMinPressed iconMaxPressed
		iconClosePressed iconRestorePressed
		))
		{ $self-> $_( $profile{ $_}); }
	$self-> {zoomRect} = [ $self-> rect];
	$self-> {miniRect} = [ 0, 0, $self-> sizeMin];
	$self-> popup-> autoPopup(0) if $self-> popup;
	$self-> {client} = $self-> insert( $profile{clientClass},
		rect         => [ $self-> get_client_rect],
		growMode     => gm::Client,
		pointerType  => cr::Arrow,
		name         => 'MDIClient',
		delegations  => ['Destroy'],
		%{$profile{clientProfile}},
	);
	$self-> update_popup_commands;
	return %profile;
}

sub on_paint
{
	my ( $self, $canvas) = @_;
	my @cl  = ( $self-> color, $self-> backColor);
	my @c3d   = ( $self-> light3DColor, $self-> dark3DColor);
	my @size  = $canvas-> size;
	my @csize = @size;
	my $dy    = $self-> {titleY};
	my ( $bs, $bi, $bb, $bp) =
		( $self-> {borderStyle}, $self-> {borderIcons},$self-> {border},$self-> {pressState});
	my @ct = ($self-> hiliteColor, $self-> hiliteBackColor);

	$bi = 0 unless $bi & mbi::TitleBar;

	$canvas-> rect3d( 0, 0, $size[0]-1, $size[1]-1, 1, $c3d[1], cl::Black) if $bb > 0;
	$canvas-> rect3d( 1, 1, $size[0]-2, $size[1]-2, 1, $c3d[0], $c3d[1]) if $bb > 1;
	$canvas-> rect3d( 2, 2, $size[0]-3, $size[1]-3, $bb - 2, 
		cl::Back|wc::Window, cl::Back|wc::Window) if $bb > 2;

	$csize[0] -= $bb * 2;
	$csize[1] -= $bb * 2;
	$csize[1] -= $dy if $bi & mbi::TitleBar;

	my $ico = $self-> {icon} ? 
		$self-> {icon} : 
		Prima::StdBitmap::image( sbmp::SysMenu);

	my $dicon = 4;
	my $icos = $self-> { iconsAtRight};

	my $tyStart = $size[1] - $bb - $dy;

	if ( $bi & mbi::TitleBar) {
		my $xAt  = $bb + 4;
		$xAt += $dy if $bi & mbi::SystemMenu;
		my $tx = $self-> text;
		my $w  = $size[0] - $bb - $icos * $dy - 1 - $xAt;
		if ( $canvas-> get_text_width( $tx) > $w) {
			$w -= $canvas-> get_text_width( '...');
			$tx = $canvas-> text_wrap( $tx, $w, 0)-> [0].'...';
		}

		my $dbmw =  $size[0] - $bb * 2 - $icos * $dy;
		if ( $dbmw > 0 && $dy > 0) {
			my $dbm = Prima::DeviceBitmap-> create(
				width  => $dbmw,
				height => $dy,
				font   => $canvas-> font,
			);
			my $x = $canvas-> font;
			$dbm-> color( $ct[1]);
			$dbm-> bar( 0, 0, $dbm-> size);
			$dbm-> color( $ct[0]);
			$dbm-> text_out( $tx, $xAt, ( $dy - $canvas-> font-> height) / 2);
			$dbm-> stretch_image( 0, 0, $dy, $dy, $ico) if $bi & mbi::SystemMenu;
			$canvas-> put_image( $bb, $tyStart, $dbm);
			$dbm-> destroy;
		}
	}

	my $tx = $size[0] - $bb - $icos * $dy;
	my $bbi = $bi;
	while ( $icos) {
		my $di;
		if ( $bbi & mbi::Minimize) {
			my $wmin = $self-> {windowState} == ws::Minimized;
			$di  = ( $bp & mbi::Minimize) ?
				$self-> {icons}-> [ $wmin ? pRESTORE : pMIN] :
				$self-> {icons}-> [ $wmin ? cRESTORE : cMIN];
			$bbi &= ~mbi::Minimize;
		} elsif ( $bbi & mbi::Maximize) {
			my $wmax = $self-> {windowState} == ws::Maximized;
			$di = ( $bp & mbi::Maximize) ?
				$self-> {icons}-> [ $wmax ? pRESTORE : pMAX] :
				$self-> {icons}-> [ $wmax ? cRESTORE : cMAX];
			$bbi &= ~mbi::Maximize;
		} elsif ( $bbi & mbi::Close) {
			$di =  $self-> {icons}-> [( $bp & mbi::Close) ? pCLOSE : cCLOSE];
			$bbi &= ~mbi::Close;
		}
		$canvas-> stretch_image( $tx, $tyStart, $dy, $dy, $di);
		$tx += $dy;
		$icos--;
	}

	$canvas-> color( $cl[1]);
	$canvas-> bar( $bb, $bb, $bb + $csize[0] - 1, $bb + $csize[1] - 1);
}

sub get_client_rect
{
	my ( $self, $x, $y) = @_;
	( $x, $y) = $self-> size unless defined $x;
	my $bw = $self-> { border};
	my @r = ( $bw, $bw, $x - $bw, $y - $bw);
	$r[3] -= $self-> {titleY} + 1 if $self-> {borderIcons} & mbi::TitleBar;
	$r[3] = $r[1] if $r[3] < $r[1];
	return @r;
}

sub client2frame
{
	my ( $self, $x1, $y1, $x2, $y2) = @_;
	my $bw = $self-> { border};
	my @r = ( $x1 - $bw, $y1 - $bw, $x2 + $bw, $y2 + $bw);
	$r[3] += $self-> {titleY} + 1 if $self-> {borderIcons} & mbi::TitleBar;
	return @r;
}

sub frame2client
{
	my ( $self, $x1, $y1, $x2, $y2) = @_;
	my $bw = $self-> { border};
	my @r = ( $x1 + $bw, $y1 + $bw, $x2 - $bw, $y2 - $bw);
	$r[3] -= $self-> {titleY} + 1 if $self-> {borderIcons} & mbi::TitleBar;
	$r[3] = $r[1] if $r[3] < $r[1];
	return @r;
}

sub sync_client
{
	return unless $_[0]-> {client};
	$_[0]-> {client}-> rect( $_[0]-> get_client_rect);
}

sub client
{
	return $_[0]-> {client} unless $#_;
	my ( $self, $c) = @_;
	$c-> owner( $self);
	$c-> clipOwner(1);
	$c-> rect( $self-> get_client_rect);
	$self-> {client} = $c;
}   

sub mdis
{
	return grep {$_-> isa('Prima::MDI') ? $_ : 0} $_[0]-> owner-> widgets
}

sub arrange_icons
{
	my $self = $_[0]-> owner;
	my @mdis = grep { ($_-> windowState == ws::Minimized && $_-> clipOwner) ? $_ : 0} $_[0]-> mdis;
	return unless scalar @mdis;
	my @szMin = $mdis[0]-> sizeMin;
	my $szx  = $self-> width / $szMin[0];
	my $szy  = int( $self-> height / $szMin[1]);
	$szx ++ if $szx > int($szx) and scalar @mdis > $szx * $szy;
	$szx = int( $szx);
	my $i = 0;
	$_-> lock for @mdis;
	for ( @mdis) {
		$_-> origin( $i % $szx * $szMin[0], int($i / $szx) * $szMin[1]);
		$i++;
	}
	$_-> unlock for @mdis;
}

sub cascade
{
	my $self = $_[0]-> owner;
	my @mdis = grep { 
		(($_-> windowState != ws::Minimized) and $_-> tileable && $_-> clipOwner) ? 
			$_ : 
			0
	} $_[0]-> mdis;
	return unless scalar @mdis;

	my $i = 0;
	for ( @mdis) {
		last if $_-> selected;
		$i++;
	}
	
	if ( $i < scalar @mdis) {
		$i = splice( @mdis, $i, 1);
		push( @mdis, $i);
	}
	
	$_-> lock for @mdis;
	
	my @r = (0,0,$self-> size);
	for ( @mdis) {
		$_-> restore if $_-> windowState != ws::Normal;
		$_-> rect( @r);
		my @radd = $_-> get_client_rect;
		$r[2]  = $r[0] + $radd[2];
		$r[3]  = $r[1] + $radd[3];
		$r[0] += $radd[0];
		$r[1] += $radd[1];
		$_-> bring_to_front;
	}

	$_-> unlock for @mdis;
}

sub tile
{
	my $self = $_[0]-> owner;
	my @mdis = grep { (($_-> windowState != ws::Minimized) and $_-> tileable && $_-> clipOwner) ? $_ : 0} $_[0]-> mdis;
	return unless scalar @mdis;
	
	my $n = scalar @mdis;
	my $i = int( sqrt( $n));
	$i++ if $n % $i and (( $n % ( $i + 1)) == 0);
	$i = int($n / $i) if $i < int( $n / $i);
	my ( $col, $row) = ( int($n / $i), $i);
	my @sz = $self-> size;
	return if $sz[0] < $col or $sz[1] < $row;
	
	( $i, $n) = ( scalar @mdis % $col, $#mdis);
	$_-> lock for @mdis;
	
	for ( @mdis) {
		my $a = ( $col - $i) * $row;
		my $x = ( $n < $a) ? int( $n / $row) : int(( $n - $a) / ( $row + 1)) + ( $col - $i);
		my $y = ( $n < $a) ? $n % $row : int(( $n - $a) % ( $row + 1));
		$_-> rect(
			$sz[0] * $x / $col,
			$sz[1] * $y / ( $row + (( $n >= $a) ? 1 : 0)),
			$sz[0] * ( $x + 1) / $col,
			$sz[1] * ( $y + 1) / ( $row + (( $n >= $a) ? 1 : 0))
		);
		$n--;
	}

	$_-> unlock for @mdis;
}

sub xy2part
{
	my ( $self, $x, $y) = @_;
	my @size = $self-> size;
	my $bw   = $self-> { border};
	my $bwx  = $bw + 16;
	my $bi   = $self-> { borderIcons};
	my $bs   = $self-> { borderStyle};
	my $icos = $self-> { iconsAtRight};
	my $move = $self-> {windowState} != ws::Maximized;
	my $size = ( $bs == bs::Sizeable) && ( $self-> {windowState} == ws::Normal);
	my $dy   = ( $bi & mbi::TitleBar) ? $self-> {titleY} : 0;

	if ( $x < $bw) {
		return q(border) unless $size;
		return q(SizeSW) if $y < $bwx;
		return q(SizeNW) if $y >= $size[1] - $bwx;
		return q(SizeW);
	} elsif ( $x >= $size[0] - $bw) {
		return q(border) unless $size;
		return q(SizeSE) if $y < $bwx;
		return q(SizeNE) if $y >= $size[1] - $bwx;
		return $size ? q(SizeE) : q(border);
	} elsif (( $y < $bw) or ( $y >= $size[1] - $bw)) {
		return q(border) unless $size;
		return ( $y < $bw) ? q(SizeSW) : q(SizeNW) if $x < $bwx;
		return ( $y < $bw) ? q(SizeSE) : q(SizeNE) if $x >= $size[0] - $bwx;
		return ($y < $bw) ? q(SizeS) : q(SizeN);
	} elsif ( $y < $size[1] - $bw - $dy) {
		return q(client);
	} elsif ( $x < $dy + $bw) {
		return ( $bi & mbi::SystemMenu) ? q(menu) : ( $move ? q(caption) : q(title));
	} elsif ( $x <= $size[0] - $bw - $icos * $dy) {
		return $move ? q(caption) : q(title);
	} elsif ( $x >= $size[0] - $bw - $dy) {
		return ( $bi & mbi::Close) ? q(close) :
			(( $bi & mbi::Maximize)  ? (
				($self-> {windowState} == ws::Maximized) ? q(restore) : q(max)
			) : (
				($self-> {windowState} == ws::Minimized) ? q(restore) : q(min)
			));
	} elsif ( $x >= $size[0] - $bw - $dy * 2) {
		return (( $bi & ( mbi::Close | mbi::Maximize)) == (mbi::Close | mbi::Maximize))
			? (
				($self-> {windowState} == ws::Maximized) ? q(restore) : q(max)
			) : (
				($self-> {windowState} == ws::Minimized) ? q(restore) : q(min)
			);
	} else {
		return ($self-> {windowState} == ws::Minimized) ? q(restore) : q(min);
	}
	return q(desktop);
}

sub post_action
{
	my ( $self, $action) = @_;
	$self-> post_message(q(cmMDI), $action);
}

sub repaint_title
{
	my ( $self, $area) = @_;
	my @size = $self-> size;
	my $bw = $self-> {border};
	my $dy = $self-> {titleY};
	$area ||= '';
	return unless $self-> { borderIcons} & mbi::TitleBar;

	if ( $area eq 'right') {
		$self-> invalidate_rect(
			$size[0] - $bw - $self-> {iconsAtRight} * $dy, $size[1] - $bw,
			$size[0] - $bw, $size[1] - $bw - $dy
		);
	} elsif ( $area eq 'left') {
		$self-> invalidate_rect(
			$bw, $size[1] - $bw - $dy,
			$bw + $dy, $size[1] - $bw,
		);
	} else {
		my $dx = (( $self-> {borderIcons} & mbi::SystemMenu) and ( !defined $self-> {icon})) ? $dy : 0;
		$self-> invalidate_rect(
			$bw + $dx, $size[1] - $bw - $dy,
			$size[0] - $bw - $self-> {iconsAtRight} * $dy, $size[1] - $bw,
		);
	}
}

sub sizemove_cancel
{
	my $self = $_[0];
	my $ok;
	return unless $self-> {mouseTransaction};
	if ( $self-> {mouseTransaction} eq q(caption)) {
		if ( $self-> {fullDrag}) {
			$self-> origin( @{$self-> {trackSaveData}});
		} else {
			$self-> xorrect( @{$self-> {prevRect}});
		}
		$ok = 1;
	} elsif ( $self-> {mouseTransactionArea} eq q(size)) {
		if ( $self-> {fullDrag}) {
			$self-> rect( @{$self-> {trackSaveData}});
		} else {
			$self-> xorrect( @{$self-> {prevRect}});
		}
		$ok = 1;
	} elsif ( $self-> {mouseTransaction} eq q(keyMove)) {
		if ( $self-> {fullDrag}) {
			$self-> origin( @{$self-> {trackSaveData}});
		} else {
			$self-> xorrect( @{$self-> {prevRect}});
		}
		$self-> select;
		$ok = 1;
	} elsif ( $self-> {mouseTransaction} eq q(keySize)) {
		if ( $self-> {fullDrag}) {
			$self-> rect( @{$self-> {trackSaveData}});
		} else {
			$self-> xorrect( @{$self-> {prevRect}});
		}
		$self-> select;
		$ok = 1;
	}

	if ( $ok) {
		$self-> {mouseTransaction} =
		$self-> {mouseTransactionArea} =
		$self-> {dirData} =
		$self-> {spotX} = $self-> {spotY} = undef;
		$self-> pointer( cr::Default);
		$self-> capture(0);
	}
	return $ok;
}

sub check_drag
{
	my $self = $_[0];
	if ( defined $self-> {dragMode}) {
		$self-> {fullDrag} = $self-> {dragMode};
	} else {
		$self-> {fullDrag} = $::application-> get_system_value( sv::FullDrag);
	}
}

sub keyMove
{
	my $self = $_[0];
	$self-> { mouseTransaction} = $self-> { mouseTransactionArea} = q(keyMove);
	($self-> {spotX}, $self-> {spotY}) = $self-> pointerPos;
	$self-> {trackSaveData} = [$self-> origin];
	$self-> pointer( cr::Move);
	$self-> focus;
	$self-> capture(1, $self-> clipOwner ? $self-> owner : ());
	$self-> check_drag;

	unless ($self-> {fullDrag}) {
		$self-> {prevRect} = [$self-> client_to_screen(0,0,$self-> size)];
		$self-> xorrect( @{$self-> {prevRect}});
	};
}

sub keySize
{
	my $self = $_[0];
	$self-> { mouseTransaction} = $self-> { mouseTransactionArea} = q(keySize);
	$self-> {trackSaveData} = [$self-> rect];
	$self-> pointer( cr::Size);
	$self-> focus;
	$self-> capture(1, $self-> clipOwner ? $self-> owner : ());
	$self-> check_drag;

	unless ($self-> {fullDrag}) {
		$self-> {prevRect} = [$self-> client_to_screen(0,0,$self-> size)];
		$self-> xorrect( @{$self-> {prevRect}});
	};
}


sub on_postmessage
{
	my ( $self, $cm, $cm2) = @_;
	return if $cm ne q(cmMDI);

	if ( $cm2 eq q(min)) {
		$self-> windowState( ws::Minimized);
	} elsif ( $cm2 eq q(max)) {
		$self-> windowState( ws::Maximized);
	} elsif ( $cm2 eq q(restore)) {
		$self-> windowState( ws::Normal);
	} elsif ( $cm2 eq q(close)) {
		$self-> close;
	}
}

sub xorrect
{
	my ( $self, @r) = @_;
	$r[2]--;
	$r[3]--;
	my $o = $::application;
	$o-> begin_paint;
	my $oo = $self-> clipOwner ? $self-> owner : $::application;
	$o-> clipRect( $oo-> client_to_screen( 0,0,$oo-> size));
	$o-> rect_focus( @r, $self-> {border});
	$o-> end_paint;
}

sub on_mousedown
{
	my ( $self, $btn, $mod, $x, $y) = @_;
	$self-> clear_event;
	return if $self-> {mouseTransaction};

	my $part = $self-> xy2part( $x, $y);
	$self-> bring_to_front;
	$self-> select;
	return if
		$part eq q(client) or
		$part eq q(border) or
		$btn != mb::Left;

	if ( $part eq q(menu)) {
		# Win32 test results - we won't get on_click( double=1) after 'cancelling'
		# on_mousedown with popup - the system says it's no more a double click,
		# just a single click to popup. Moreover, in general this is correct. We could
		# catch second on_mousedown in system-defined timeout interval, but
		# this popup-canceling on_mousedown won't come to us either.
		my $delay = Prima::Application-> get_system_value( sv::DblClickDelay);
		$delay = 250 if $delay > 250;
		$self-> {exTimer} = Prima::Timer-> create(
			owner   => $self,
			timeout => $delay,
			onTick  => sub {
				my $self = $_[0]-> owner;
				$_[0]-> destroy;
				delete $self-> {exTimer};
				return unless $self-> alive || ! defined ( $self-> popup);
				
				my ( $w, $y, $bw)  = ( $self-> height, $self-> {titleY}, $self-> {border});
				$self-> popup-> popup( 
					$bw, $w - $y - $bw, 
					$bw, $w - $y - $bw, $bw + $y, $w - $bw
				);
			},
		);
		$self-> {exTimer}-> start;
		return;
	}

	if ( $part eq q(caption)) {
		$self-> {mouseTransaction} = $part;
		$self-> {spotX} = $x;
		$self-> {spotY} = $y;
		$self-> {trackSaveData} = [$self-> origin];
		$self-> capture(1, $self-> clipOwner ? $self-> owner : ());
		$self-> check_drag;
		unless ($self-> {fullDrag}) {
			$self-> {prevRect} = [$self-> client_to_screen(0,0,$self-> size)];
			$self-> xorrect( @{$self-> {prevRect}});
			my @r = @{$self-> {prevRect}};
		};
		return;
	}

	if ( $part =~ /Size/) {
		$self-> {mouseTransaction} = $part;
		$self-> {mouseTransactionArea} = 'size';
		$self-> {spotX} = $x;
		$self-> {spotY} = $y;
		$self-> {trackSaveData} = [$self-> rect];
		$part =~ s/Size//;
		my ( $xa, $ya);

		if    ( $part eq q(S))   { ( $xa, $ya) = ( 0,-1); }
		elsif ( $part eq q(N))   { ( $xa, $ya) = ( 0, 1); }
		elsif ( $part eq q(W))   { ( $xa, $ya) = (-1, 0); }
		elsif ( $part eq q(E))   { ( $xa, $ya) = ( 1, 0); }
		elsif ( $part eq q(SW)) { ( $xa, $ya) = (-1,-1); }
		elsif ( $part eq q(NE)) { ( $xa, $ya) = ( 1, 1); }
		elsif ( $part eq q(NW)) { ( $xa, $ya) = (-1, 1); }
		elsif ( $part eq q(SE)) { ( $xa, $ya) = ( 1,-1); }
		
		$self-> {dirData} = [$xa, $ya];
		$self-> capture(1, $self-> clipOwner ? $self-> owner : ());
		$self-> check_drag;
		
		unless ($self-> {fullDrag}) {
			$self-> {prevRect} = [$self-> client_to_screen(0,0,$self-> size)];
			$self-> xorrect( @{$self-> {prevRect}});
		};
		return;
	}

	$self-> {mouseTransaction} = $part;
	$self-> {mouseTransactionArea} = 'right';
	$self-> {pressState} = 0 |
		( $part eq q(min)     ? mbi::Minimize : 0)  |
		( $part eq q(max)     ? mbi::Maximize : 0)  |
		( $part eq q(close)   ? mbi::Close    : 0)|
		( $part eq q(restore) ? (
			($self-> {windowState} == ws::Minimized) ? mbi::Minimize : mbi::Maximize
		) : 0)
	;
	$self-> {lastMouseOver} = 1;
	$self-> repaint_title(q(right));
	$self-> capture(1, $self-> clipOwner ? $self-> owner : ());
}

sub on_mouseclick
{
	my ( $self, $btn, $mod, $x, $y, $dbl) = @_;
	$self-> clear_event;
	return if $self-> {mouseTransaction} or !$dbl;
	my $part = $self-> xy2part( $x, $y);

	if ( $part eq q(caption)) {
		$self-> post_action(( $self-> {windowState} == ws::Normal) ? q(max) : q(restore));
		return;
	}

	if ( $part eq q(title)) {
		$self-> post_action( q(restore));
		return;
	}

	if ( $part eq q(menu)) {
		if ( $self-> {exTimer}) {
			$self-> {exTimer}-> destroy;
			delete $self-> {exTimer};
		}
		$self-> post_action(q(close));
		return;
	}
}

sub on_translateaccel
{
	my ( $self, $code, $key, $mod) = @_;
	if ( !$self-> {mouseTransaction}) {
		if (( $key == kb::Tab || $key == kb::BackTab) && ( $mod & km::Ctrl)) {
			my $back = ($key == kb::BackTab);
			my $x = $self-> first;
			my @mdis = ( $x );
			push @mdis, $x while $x = $x-> next;
			@mdis = grep { $_-> isa('Prima::MDI') } @mdis;
			$x = $mdis[ $back ? 1 : -1];
			if ( $x) {
				$back ? $mdis[0]-> send_to_back : $x-> bring_to_front;
				$x-> select;
			}
			$self-> clear_event;
		} if (
			( $key == kb::Space) && 
			( $mod & km::Ctrl) && 
			( $self-> { borderIcons} & mbi::SystemMenu)
		) {
			my ( $w, $y, $bw)  = ( $self-> height, $self-> {titleY}, $self-> {border});
			$self-> popup-> popup( 
				$bw, $w - $y - $bw, 
				$bw, $w - $y - $bw, $bw + $y, $w - $bw
			);
			$self-> clear_event;
		}
	}
}

sub on_keydown
{
	my ( $self, $code, $key, $mod) = @_;

	if ( $self-> {mouseTransaction} and ( $key == kb::Esc)) {
		$self-> sizemove_cancel;
		$self-> clear_event;
		return;
	}

	return unless $self-> {mouseTransaction};

	if ( $self-> {mouseTransaction} eq q(keyMove)) {
		my ( $dx, $dy) = (0,0);

		if 
		( $key == kb::Down)  { $dy -= 5; } elsif
		( $key == kb::Up)    { $dy += 5; } elsif
		( $key == kb::Left)  { $dx -= 5; } elsif
		( $key == kb::Right) { $dx += 5; } elsif
		( $key == kb::Enter) {
			$self-> pointer( cr::Default);
			$self-> capture(0);
			$self-> clear_event;
			unless ( $self-> {fullDrag}) {
				$self-> xorrect( @{$self-> {prevRect}});
				my $oo = $self-> clipOwner ? $self-> owner : $::application;
				$self-> origin( 
					$oo-> screen_to_client(@{$self-> {prevRect}}[0,1])
				);
			}
			$self-> {mouseTransaction} = 
				$self-> {mouseTransactionArea} = 
				$self-> {dirData} =
				$self-> {spotX} = 
				$self-> {spotY} = undef;
			return;
		}

		if ( $dx or $dy) {
			my @p = $self-> pointerPos;
			my @o = $self-> origin;
			$self-> pointerPos( $p[0] + $dx, $p[1] + $dy);
			$self-> clear_event;
			if ( $self-> {fullDrag}) {
				$self-> origin( $o[0] + $dx, $o[1] + $dy);
			} else {
				$self-> xorrect( @{$self-> {prevRect}});
				${$self-> {prevRect}}[0] += $dx;
				${$self-> {prevRect}}[1] += $dy;
				${$self-> {prevRect}}[2] += $dx;
				${$self-> {prevRect}}[3] += $dy;
				$self-> xorrect( @{$self-> {prevRect}});
			}
			return;
		}
	}

	if ( $self-> {mouseTransaction} eq q(keySize)) {
		my ( $dx, $dy) = (0,0);
		if 
			( $key == kb::Down)  { $dy += 5; } elsif
			( $key == kb::Up)    { $dy -= 5; } elsif
			( $key == kb::Left)  { $dx -= 5; } elsif
			( $key == kb::Right) { $dx += 5; } elsif
			( $key == kb::Enter) {
				$self-> {mouseTransaction} = $self-> {mouseTransactionArea} = undef;
				$self-> pointer( cr::Default);
				$self-> capture(0);
				$self-> clear_event;
				unless ( $self-> {fullDrag}) {
					$self-> xorrect( @{$self-> {prevRect}});
					my $oo = $self-> clipOwner ? 
						$self-> owner : 
						$::application;
					$self-> rect( $oo-> screen_to_client(@{$self-> {prevRect}}));
				}
				return;
			}

		if ( $dx or $dy) {
			if ( $self-> {fullDrag}) {
				my @o = $self-> size;
				$self-> width( $o[0] + $dx) if $dx;
				if ( $dy) {
					my $b = $self-> bottom;
					$self-> height( $o[1] + $dy);
					$self-> bottom( $b - $dy) unless $self-> height != $o[1] + $dy;
				}
			} else {
				$self-> xorrect( @{$self-> {prevRect}});
				
				my @r = @{$self-> {prevRect}};
				$r[1] -= $dy;
				$r[2] += $dx;
				
				my @min = $self-> sizeMin;
				$r[1] = $r[3] - $min[1] if $r[3] - $r[1] < $min[1];
				$r[2] = $r[0] + $min[0] if $r[2] < $r[0] + $min[0];

				my @max = $self-> sizeMax;
				$r[1] = $r[3] - $max[1] if $r[3] - $r[1] > $max[1];
				$r[2] = $r[0] + $max[0] if $r[2] > $r[0] + $max[0];

				$self-> {prevRect} = \@r;
				$self-> xorrect( @r);
			}
			$self-> clear_event;
			return;
		}
	}
}

sub on_mouseup
{
	my ( $self, $btn, $mod, $x, $y) = @_;
	$self-> clear_event;
	return unless $self-> {mouseTransaction};

	my $tr = $self-> {mouseTransaction};
	$self-> {mouseTransaction} = undef;

	$self-> {lastMouseOver} = undef;
	$self-> capture(0);

	if ( $tr eq q(caption)) {
		unless ( $self-> {fullDrag}) {
			$self-> xorrect( @{$self-> {prevRect}});

			my @org = $_[0]-> origin;
			my @new = ( $org[0] + $x - $self-> {spotX}, $org[1] + $y - $self-> {spotY});
			$self-> origin( $new[0], $new[1]) if $org[1] != $new[1] || $org[0] != $new[0];
		}
		$self-> {spotX} = $self-> {spotY} = undef;
		return;
	}

	if ( $self-> {mouseTransactionArea} eq q(right)) {
		$self-> {pressState} = 0;
		$self-> {mouseTransactionArea} = undef;
		my $part = $self-> xy2part( $x, $y);
		if ( $part eq $tr) {
			$self-> repaint_title(q(right));
			$self-> post_action( $tr);
		}
		return;
	}

	if ( $self-> {mouseTransactionArea} eq q(size)) {
		unless ($self-> {fullDrag}) {
			my @r = @{$self-> {prevRect}};
			$self-> xorrect( @r);
			my $oo = $self-> clipOwner ? $self-> owner : $::application;
			$self-> rect( $oo-> screen_to_client(@r));
		};
		$self-> {mouseTransactionArea} = $self-> {dirData} = undef;
		return;
	}
}

sub on_mousemove
{
	my ( $self, $mod, $x, $y) = @_;
	$self-> clear_event;

	if ( $self-> {mouseTransaction}) {
		if ( $self-> {mouseTransaction} eq q(caption)) {
			if ( $self-> {fullDrag}) {
				my @org = $_[0]-> origin;
				my @new = ( 
					$org[0] + $x - $self-> {spotX}, 
					$org[1] + $y - $self-> {spotY}
				);
				$self-> origin( $new[0], $new[1]) 
					if $org[1] != $new[1] || $org[0] != $new[0];
			} else {
				$self-> xorrect( @{$self-> {prevRect}});
				my @xorg = $self-> client_to_screen( 
					$x - $self-> {spotX}, 
					$y - $self-> {spotY}
				);
				my @sz   = $self-> size;
				$self-> {prevRect} = [ 
					@xorg, 
					$sz[0] + $xorg[0], $sz[1] + $xorg[1]
				];
				$self-> xorrect( @{$self-> {prevRect}});
			}
			return;
		}

		if ( $self-> {mouseTransaction} eq q(keyMove)) {
			if ( $self-> {fullDrag}) {
				my @org = $_[0]-> origin;
				my @new = ( 
					$org[0] + $x - $self-> {spotX}, 
					$org[1] + $y - $self-> {spotY}
				);
				$self-> origin( $new[0], $new[1]) 
					if $org[1] != $new[1] || $org[0] != $new[0];
			} else {
			#  works also, but is confusing slightly
			#  $self-> xorrect( @{$self-> {prevRect}});
			#  my @xorg = $self-> client_to_screen( $x - $self->{spotX}, $y - $self->{spotY});
			#  my @sz   = $self-> size;
			#  $self-> {prevRect} = [ @xorg, $sz[0] + $xorg[0], $sz[1] + $xorg[1]];
			#  $self-> xorrect( @{$self-> {prevRect}});
			}
			return;
		}

		if ( $self-> {mouseTransactionArea} eq q(right)) {
			my $part = $self-> xy2part( $x, $y);
			my $mouseOver = $part eq $self-> {mouseTransaction};
			if ( $self-> { lastMouseOver} != $mouseOver) {
				$self-> {pressState} = $mouseOver ? ( 0 |
					( $part eq q(min)     ? mbi::Minimize : 0)  |
					( $part eq q(max)     ? mbi::Maximize : 0)  |
					( $part eq q(close)   ? mbi::Close    : 0)|
					( $part eq q(restore) ? (
						( $self-> {windowState} == ws::Minimized) ? 
							mbi::Minimize : 
							mbi::Maximize
					) : 0)) : 0;
				$self-> { lastMouseOver} = $mouseOver;
				$self-> repaint_title(q(right));
			}
			return;
		}

		if ( $self-> {mouseTransactionArea} eq q(size)) {
			my @org = $_[0]-> rect;
			my @new = @org;
			my @min = $self-> sizeMin;
			my ( $xa, $ya) = @{$self-> {dirData}};

			if ( $xa < 0) {
				$new[0] = $org[0] + $x - $self-> {spotX};
				$new[0] = $org[2] - $min[0] if $new[0] > $org[2] - $min[0];
			} elsif ( $xa > 0) {
				$new[2] = $org[2] + $x - $self-> {spotX};
				if ( $new[2] < $org[0] + $min[0]) {
					$new[2] = $org[0] + $min[0];
					$self-> {spotX} = $min[0] if $self-> {fullDrag};
				} else {
					$self-> {spotX} = $x if $self-> {fullDrag};
				}
			}

			if ( $ya < 0) {
				$new[1] = $org[1] + $y - $self-> {spotY};
				$new[1] = $org[3] - $min[1] if $new[1] > $org[3] - $min[1];
			} elsif ( $ya > 0) {
				$new[3] = $org[3] + $y - $self-> {spotY};
				if ( $new[3] < $org[1] + $min[1]) {
					$new[3] = $org[1] + $min[1];
					$self-> {spotY} = $min[1] if $self-> {fullDrag};
				} else {
					$self-> {spotY} = $y if $self-> {fullDrag};
				}
			}
			if ( 
				$org[0] != $new[0] || 
				$org[1] != $new[1] || 
				$org[2] != $new[2] || 
				$org[3] != $new[3]
			) {
				if ( $self-> {fullDrag}) {
					$self-> rect( @new)
				} else {
					$self-> xorrect( @{$self-> {prevRect}});
					my $oo = $self-> clipOwner ? $self-> owner : $::application;
					$self-> {prevRect} = [$oo-> client_to_screen( @new)];
					$self-> xorrect( @{$self-> {prevRect}});
				}
			}
			return;
		}
	} else {
		if (
			$self-> {borderStyle} == bs::Sizeable and
			$self-> {windowState} == ws::Normal
		) {
			return if !$self-> enabled;
			my $part = $self-> xy2part( $x, $y);
			$self-> pointer( $part =~ /^Size/ ? &{$cr::{$part}} : cr::Arrow);
		} else {
			$self-> pointer( cr::Arrow);
		}
	}
}

sub on_enable { $_[0]-> repaint; }
sub on_disable { $_[0]-> repaint; }

sub text
{
	return $_[0]-> SUPER::text unless $#_;
	$_[0]-> SUPER::text( $_[1]);
	$_[0]-> repaint_title(q(title));
}

sub update_size_min
{
	my $self = $_[0];
	my @a = Prima::Application-> get_default_window_borders( $self-> {borderStyle});
	my $ic = $self-> { iconsAtRight} + 
		1 + 
		(($self-> {borderIcons} & mbi::SystemMenu) ? 1 : 0);
	$self-> sizeMin( 
		$self-> {titleY} * $ic + $a[0] * 2, 
		$self-> {titleY} + $a[1] * 2
	); 
}   

sub set_border_icons
{
	my ( $self, $bi) = @_;
	$bi &= mbi::All;
	return if $bi == $self-> {borderIcons};
	$self-> {borderIcons} = $bi;

	my $icos = 0;
	$icos++ if $bi & mbi::Minimize;
	$icos++ if $bi & mbi::Maximize;
	$icos++ if $bi & mbi::Close;

	$self-> { iconsAtRight} = $icos;

	$self-> update_popup_commands;
	$self-> update_size_min;
	$self-> sync_client;
	$self-> repaint;
}

sub set_border_style
{
	my ( $self, $bs) = @_;
	return if $bs == $self-> {borderStyle} or $bs < bs::None or $bs > bs::Dialog;

	$self-> {borderStyle} = $bs;
	my ( $bbx, $bby) = Prima::Application-> get_default_window_borders( $bs);
	$bbx = $bbx > 1 ? $bbx : (( $bbx > 0) ? 1 : 0);
	$bby = $bby > 1 ? $bby : (( $bby > 0) ? 1 : 0);
	$self-> {border} = $bbx > $bby ? $bby : $bbx;

	$self-> update_popup_commands;
	$self-> update_size_min;
	$self-> sync_client;
	$self-> repaint;
}

sub update_popup_commands
{
	my $self = $_[0];
	my $popup = $self-> popup;

	my ( $bi, $bs, $ws) = ( $self-> {borderIcons}, $self-> {borderStyle}, $self-> {windowState});
	
	$popup-> max-> enabled(( $bi & mbi::Maximize) && ( $ws != ws::Maximized));
	$popup-> min-> enabled(( $bi & mbi::Minimize) && ( $ws != ws::Minimized));
	$popup-> restore-> enabled(( $bi & (mbi::Minimize|mbi::Maximize)) && ( $ws != ws::Normal));
	$popup-> move-> enabled( $ws == ws::Normal);
	$popup-> size-> enabled(( $ws == ws::Normal) && ( $bs == bs::Sizeable));
}

sub set_window_state
{
	my ( $self, $ws) = @_;
	return if $ws == $self-> {windowState} or $ws < ws::Normal or $ws > ws::Maximized;
	my $ows = $self-> {windowState};

	my $popup = $self-> popup;
	if ( $ws == ws::Minimized) {
		return unless $self-> {borderIcons} & mbi::Minimize;
		$self-> {zoomRect} = [ $self-> rect] if $ows == ws::Normal;
		$self-> growMode( $self-> {zoomGrowMode}) if $ows == ws::Maximized;
		my @szMin = $self-> sizeMin;
		my ($x, $y) = ( $self-> {miniRect}-> [0], $self-> {miniRect}-> [1]);
		$self-> clipOwner(1) unless ($self-> {saveClipOwner} = $self-> clipOwner);
		
		# start calculating position for min-widnow
		my @sz = $self-> clipOwner ? $self-> owner-> size : $::application-> size;
		$x = $sz[0] - $szMin[0] if $x > $sz[0] - $szMin[0];
		$y = $sz[1] - $szMin[1] if $y > $sz[1] - $szMin[1];

		my @mdis = grep {
			(( $_ ne $self) and ( $_-> windowState == ws::Minimized)) ? 
				$_ : 
				0
		} $self-> mdis;

		my @maps;
		my @szmaxids = ( int($sz[0] / $szMin[0]), int($sz[1] / $szMin[1]));
		$szmaxids[0] = 1 unless $szmaxids[0];
		$szmaxids[1] = 1 unless $szmaxids[1];

		for ( @mdis) {
			my ( $ax, $ay) = ( $_-> left / $szMin[0], $_-> bottom / $szMin[1]);
			my $id = int( $ay) * $szmaxids[0] + int( $ax);
			push ( @maps, $id);
			push ( @maps, $id + 1) if $ax > int( $ax);
			push ( @maps, $id + $szmaxids[0]) if $ay > int( $ay);
			push ( @maps, $id + $szmaxids[0] + 1) if $ay > int( $ay) and $ax > int( $ax);
		}
		
		my $id = int( $y / $szMin[1]) * $szmaxids[0] + int( $x / $szMin[0]);
		if ( scalar grep { $_ == $id ? 1 : 0} @maps) {
			my $i;
			my %hh = map {$_=>1} @maps;
			@maps = sort { $a <=> $b} keys %hh;
			my $newID = scalar @maps;
			for ( $i = 0; $i < scalar @maps; $i++) {
				$newID = $i, last if $i != $maps[ $i];
			}
			$x = ( $newID % $szmaxids[0]) * $szMin[0];
			$y = int ( $newID / $szmaxids[0]) * $szMin[1];
		}
		# end calculating position for min-widnow

		($self-> {miniRect}-> [0], $self-> {miniRect}-> [1]) = ( $x, $y);
		$self-> {miniRect}-> [2] = $self-> {miniRect}-> [0] + $szMin[0];
		$self-> {miniRect}-> [3] = $self-> {miniRect}-> [1] + $szMin[1];
		$self-> rect( @{$self-> {miniRect}});
		
		$popup-> enable( $_)  for ( qw( max restore move));
		$popup-> disable( $_) for ( qw( min size));
	} elsif ( $ws == ws::Maximized) {
		return unless $self-> {borderIcons} & mbi::Maximize;
		
		$self-> clipOwner(0) 
			if $ows == ws::Minimized && !$self-> {saveClipOwner};
		delete $self-> {saveClipOwner};
		
		$self-> {miniRect} = [ $self-> rect] if $ows == ws::Minimized;
		$self-> {zoomRect} = [ $self-> rect] if $ows == ws::Normal;
		$self-> rect( 0, 0, $self-> clipOwner ? $self-> owner-> size : $::application-> size);
		$self-> {zoomGrowMode} = $self-> growMode;
		$self-> growMode( gm::Client);
		
		$popup-> enable( $_)  for ( qw( min restore));
		$popup-> disable( $_) for ( qw( max move size));
	} else {
		$self-> {windowState} = ws::Normal;
		return unless $self-> {borderIcons} & (mbi::Maximize|mbi::Minimize);
		
		$self-> clipOwner(0) 
			if $ows == ws::Minimized && !$self-> {saveClipOwner};
			
		delete $self-> {saveClipOwner};
		$self-> {miniRect} = [ $self-> rect] if $ows == ws::Minimized;
		$self-> growMode( $self-> {zoomGrowMode}) if $ows == ws::Maximized;
		$self-> rect( @{$self-> {zoomRect}});
		$popup-> enable( $_)  for ( qw( max min move size));
		$popup-> disable( $_) for ( qw( restore));
	}
	$self-> {windowState} = $ws;
	$self-> update_popup_commands;
	$self-> notify(q(WindowState));
}


sub set_icon
{
	my ( $self, $icon) = @_;
	$self-> {icon} = $icon;
	$self-> repaint_title(q('left'));
}

sub on_windowstate
{
#	my $self = $_[0];
}

sub on_activate
{
#	my $self = $_[0];
}

sub on_deactivate
{
#	my $self = $_[0];
}

sub MDIClient_Destroy
{
	$_[0]-> destroy;
}

sub titleHeight
{
	return $_[0]-> {titleHeight} unless $#_;
	my ( $self, $th) = @_;
	$self-> {titleHeight} = $th;
	$th = Prima::Application-> get_system_value( sv::YTitleBar) unless $th;
	return if $self-> {titleY} == $th;
	$self-> {titleY} = $th;
	$self-> sync_client;
	$self-> repaint;
}   

sub __icon
{
	my ( $self, $id) = ( shift, shift);
	return $self-> {icons}-> [$id] unless @_;
	$self-> {icons}-> [$id] = shift;
	$self-> repaint_title;
}   

sub iconMin            { return shift-> __icon( cMIN,     @_)};
sub iconMax            { return shift-> __icon( cMAX,     @_)};
sub iconClose          { return shift-> __icon( cCLOSE,   @_)};
sub iconRestore        { return shift-> __icon( cRESTORE, @_)};
sub iconMinPressed     { return shift-> __icon( pMIN,     @_)};
sub iconMaxPressed     { return shift-> __icon( pMAX,     @_)};
sub iconClosePressed   { return shift-> __icon( pCLOSE,   @_)};
sub iconRestorePressed { return shift-> __icon( pRESTORE, @_)};

sub maximize    { $_[0]-> windowState( ws::Maximized)}
sub minimize    { $_[0]-> windowState( ws::Minimized)}
sub restore     { $_[0]-> windowState( ws::Normal)}

sub borderIcons {($#_)?$_[0]-> set_border_icons($_[1])   :return $_[0]-> {borderIcons}}
sub borderStyle {($#_)?$_[0]-> set_border_style($_[1])   :return $_[0]-> {borderStyle}}
sub dragMode    {($#_)?($_[0]-> {dragMode} = $_[1])      :return $_[0]-> {dragMode}   }
sub frameOrigin {return shift-> origin( @_);}
sub frameSize   {return shift-> size( @_);}
sub frameWidth  {return shift-> width( @_);}
sub frameHeight {return shift-> height( @_);}
sub tileable    {($#_)?$_[0]-> {tileable}=$_[1]          :return $_[0]-> {tileable}   }
sub windowState {($#_)?$_[0]-> set_window_state($_[1])   :return $_[0]-> {windowState}}
sub icon        {($#_)?$_[0]-> set_icon        ($_[1])   :return $_[0]-> {icon}       }

1;

__DATA__

=pod

=head1 NAME

Prima::MDI - top-level windows emulation classes

=head1 DESCRIPTION

MDI stands for Multiple Document Interface, and is a Microsoft Windows user
interface, that consists of multiple non-toplevel windows belonging to an
application window. The module contains classes that provide similar
functionality; sub-window widgets realize a set of operations, close to those
of the real top-level windows, - iconize, maximize, cascade etc.

The basic classes required to use the MDI are C<Prima::MDIOwner> and
C<Prima::MDI>, which are, correspondingly, sub-window owner class and
sub-window class. C<Prima::MDIWindowOwner> is exactly the same as
C<Prima::MDIOwner> but is a C<Prima::Window> descendant: the both owner classes
are different only in their first ascendants. Their second ascendant is
C<Prima::MDIMethods> package, that contains all the owner class functionality.

Usage of C<Prima::MDI> class extends beyond the multi-document paradigm.
C<Prima::DockManager> module uses the class as a base of a dockable toolbar
window class ( see L<Prima::DockManager>.

=head1 SYNOPSIS

	use Prima::MDI;

	my $owner = Prima::MDIWindowOwner-> create();
	my $mdi   = $owner-> insert( 'Prima::MDI');
	$mdi-> client-> insert( 'Prima::Button' => centered => 1 );

=head1 Prima::MDI

Implements MDI window functionality. A subwindow widget consists of a titlebar,
titlebar buttons, and a client widget. The latter must be used as an insertion
target for all children widgets.

A subwindow can be moved and resized, both by mouse and keyboard.  These
functions, along with maximize, minimize, and restore commands are accessible
via the toolbar-anchored popup menu. The default set of commands is as follows:

	Close window              - Ctrl+F4
	Restore window            - Ctrl+F5 or a double click on the titlebar
	Maximize window           - Ctrl+F10 or a double click on the titlebar 
	Go to next MDI window     - Ctrl+Tab
	Go to previous MDI window - Ctrl+Shift+Tab
	Invoke popup menu         - Ctrl+Space

The class mimics API of C<Prima::Window> class, and in some extent
L<Prima::Window> and this page share the same information.

=head2 Properties

=over

=item borderIcons INTEGER

Selects window decorations, which are buttons on titlebar and the titlebar itself.
Can be 0 or a combination of the following C<mbi::XXX> constants, which are supreset
of C<bi::XXX> constants ( see L<Prima::Window/borderIcons> ) and are interchangeable.

	mbi::SystemMenu  - system menu button with icon is shown
	mbi::Minimize    - minimize button 
	mbi::Maximize    - maximize ( and eventual restore )
	mbi::TitleBar    - window title 
	mbi::Close       - close button
	mbi::All         - all of the above

Default value: C<mbi::All>

=item borderStyle INTEGER

One of C<bs::XXX> constants, selecting the window border style.
The constants are:

	bs::None      - no border
	bs::Single    - thin border
	bs::Dialog    - thick border
	bs::Sizeable  - thick border with interactive resize capabilities

C<bs::Sizeable> is an unique mode. If selected, the user
can resize the window interactively. The other border styles
disallow resizing and affect the border width and design only.

Default value: C<bs::Sizeable>

=item client OBJECT

Selects the client widget at runtime. When changing the client, the old client 
children are not reparented to the new client. The property cannot be used to
set the client during the MDI window creation; use
C<clientClass> and C<clientProfile> properties instead. 

When setting new client object, note that is has to be named C<MDIClient>
and the window is automatically destroyed after the client is destroyed.

=item clientClass STRING

Assigns client widget class.

Create-only property.

Default value: C<Prima::Widget>

=item clientProfile HASH

Assigns hash of properties, passed to the client during the creation.

Create-only property.

=item dragMode SCALAR

A three-state variable, which governs the visual feedback style when the user
moves or resizes a window. If 1, the window is moved or resized simultaneously
with the user mouse or keyboard actions.  If 0, a marquee rectangle is drawn,
which is moved or resized as the user sends the commands; the window is
actually positioned and / or resized after the dragging session is successfully
finished. If C<undef>, the system-dependant dragging style is used. ( See
L<Prima::Application/get_system_value> ).

The dragging session can be aborted by 
hitting Esc key or calling C<sizemove_cancel> method. 

Default value: C<undef>.

=item icon HANDLE

Selects a custom image to be drawn in the left corner of the toolbar.  If 0,
the default image ( menu button icon ) is drawn. 

Default value: 0

=item iconMin HANDLE

Selects minimized button image in normal state.

=item iconMax HANDLE

Selects maximized button image in normal state.

=item iconClose HANDLE

Selects close button image in normal state.

=item iconRestore HANDLE

Selects restore button image in normal state.

=item iconMinPressed HANDLE

Selects minimize button image in pressed state.

=item iconMaxPressed HANDLE

Selects maximize button image in pressed state.

=item iconClosePressed HANDLE

Selects close button image in pressed state.

=item iconRestorePressed HANDLE

Selects restore button image in pressed state.

=item tileable BOOLEAN

Selects whether the window is allowed to participate in cascading and tiling
auto-arrangements, performed correspondingly by C<cascade> and C<tile> methods.
If 0, the window is never positioned automatically.

Default value: 1

=item titleHeight INTEGER

Selects height of the title bar in pixels. If 0, the default system
value is used.

Default value: 0

=item windowState STATE

A three-state property, that governs the state of a window.
STATE can be one of three C<ws::XXX> constants:

	ws::Normal
	ws::Minimized
	ws::Maximized

The property can be changed
either by explicit set-mode call or by the user. In either case,
a C<WindowState> notification is triggered.

The property has three convenience wrappers: C<maximize()>, C<minimize()> and
C<restore()>.

Default value: C<ws::Normal>

See also: C<WindowState>

=back

=head2 Methods

=over

=item arrange_icons

Arranges geometrically the minimized sibling MDI windows. 

=item cascade

Arranges sibling MDI windows so they form a cascade-like structure: the lowest
window is expanded to the full owner window inferior rectangle, window next to
the lowest occupies the inferior rectangle of the lowest window, etc. 

Only windows with C<tileable> property set to 1 are processed.

=item client2frame X1, Y1, X2, Y2

Returns a rectangle that the window would occupy if
its client rectangle is assigned to X1, Y1, X2, Y2 
rectangle.

=item frame2client X1, Y1, X2, Y2

Returns a rectangle that the window client would occupy if
the window rectangle is assigned to X1, Y1, X2, Y2 
rectangle.

=item get_client_rect [ WIDTH, HEIGHT ]

Returns a rectangle in the window coordinate system that the client would
occupy if the window extensions are WIDTH and HEIGHT.  If WIDTH and HEIGHT are
undefined, the current window size is used.

=item keyMove

Initiates window moving session, navigated by the keyboard.

=item keySize

Initiates window resizing session, navigated by the keyboard.

=item mdis

Returns array of sibling MDI windows.

=item maximize

Maximizes window. A shortcut for C<windowState(ws::Maximized)>.

=item minimize

Minimizes window. A shortcut for C<windowState(ws::Minimized)>.

=item post_action STRING

Posts an action to the windows; the action is deferred and executed in the next
message loop. This is used to avoid unnecessary state checks when the
action-executing code returns. The current implementation accepts following
string commands: C<min>, C<max>, C<restore>, C<close>.

=item repaint_title [ STRING = C<title> ]

Invalidates rectangle on the title bar, corresponding to STRING, which can be
one of the following:

	left    - redraw the menu button
	right   - redraw minimize, maximize, and close buttons
	title   - redraw the title

=item restore

Restores window to normal state from minimized or maximized state. A shortcut
for C<windowState(ws::Normal)>.

=item sizemove_cancel

Cancels active window moving or resizing session and returns the window to the
state before the session.

=item tile

Arranges sibling MDI windows so they form a grid-like structure, where all
windows occupy equal space, if possible.

Only windows with C<tileable> property set to 1 are processed.

=item xy2part X, Y

Maps a point in (X,Y) coordinates into a string, corresponding to a part of the
window: titlebar, button, or part of the border. The latter can be returned
only if C<borderStyle> is set to C<bs::Sizeable>.  The possible return values
are:

	border   - window border; the window is not sizeable
	client   - client widget
	caption  - titlebar; the window is not moveable
	title    - titlebar; the window is movable
	close    - close button
	min      - minimize button
	max      - maximize button
	restore  - restore button
	menu     - menu button
	desktop  - the point does not belong to the window

In addition, if the window is sizeable, the following constants can be
returned, indicating part of the border:

	SizeN    - upper side
	SizeS    - lower side
	SizeW    - left side
	SizeE    - right side
	SizeSW   - lower left corner
	SizeNW   - upper left corner
	SizeSE   - lower right corner
	SizeNE   - upper right corner

=back

=head2 Events

=over

=item Activate

Triggered when the user activates a window.  Activation mark is usually resides
on a window that contains keyboard focus.

The module does not provide the activation function; C<select()> call is used
instead.

=item Deactivate

Triggered when the user deactivates a window.  Window is usually marked
inactive, when it contains no keyboard focus.

The module does not provide the de-activation function; C<deselect()> call is
used instead.

=item WindowState STATE

Triggered when window state is changed, either by an explicit C<windowState()>
call, or by the user.  STATE is the new window state, one of three C<ws::XXX>
constants.

=back

=head1 Prima::MDIMethods

=head2 Methods

The package contains several methods for a class that is to be used as a MDI
windows owner. It is enough to add class inheritance to C<Prima::MDIMethods> to
use the functionality. This step, however, is not required for a widget to
become a MDI windows owner; the package contains helper functions only, which
mostly mirror the arrangement functions of C<Prima::MDI> class.

=over

=item mdi_activate

Repaints window title in all children MDI windows.

=item mdis

Returns array of children MDI windows.

=item arrange_icons

Same as C<Prima::MDI::arrange_icons>.

=item cascade

Same as C<Prima::MDI::cascade>.

=item tile

Same as C<Prima::MDI::tile>.

=back

=head1 Prima::MDIOwner

A predeclared descendant class of C<Prima::Widget> and C<Prima::MDIMethods>.

=head1 Prima::MDIWindowOwner

A pre-declared descendant class of C<Prima::Window> and C<Prima::MDIMethods>.

=head1 AUTHOR

Dmitry Karasik, E<lt>dmitry@karasik.eu.orgE<gt>.

=head1 SEE ALSO

L<Prima>, L<Prima::Widget>, L<Prima::Window>, L<Prima::DockManager>, F<examples/mdi.pl>

=cut