gtkada_21.0.0_3c1373c0/src/gtkada-mdi.adb

   1
   2
   3
   4
   5
   6
   7
   8
   9
  10
  11
  12
  13
  14
  15
  16
  17
  18
  19
  20
  21
  22
  23
  24
  25
  26
  27
  28
  29
  30
  31
  32
  33
  34
  35
  36
  37
  38
  39
  40
  41
  42
  43
  44
  45
  46
  47
  48
  49
  50
  51
  52
  53
  54
  55
  56
  57
  58
  59
  60
  61
  62
  63
  64
  65
  66
  67
  68
  69
  70
  71
  72
  73
  74
  75
  76
  77
  78
  79
  80
  81
  82
  83
  84
  85
  86
  87
  88
  89
  90
  91
  92
  93
  94
  95
  96
  97
  98
  99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 124
 125
 126
 127
 128
 129
 130
 131
 132
 133
 134
 135
 136
 137
 138
 139
 140
 141
 142
 143
 144
 145
 146
 147
 148
 149
 150
 151
 152
 153
 154
 155
 156
 157
 158
 159
 160
 161
 162
 163
 164
 165
 166
 167
 168
 169
 170
 171
 172
 173
 174
 175
 176
 177
 178
 179
 180
 181
 182
 183
 184
 185
 186
 187
 188
 189
 190
 191
 192
 193
 194
 195
 196
 197
 198
 199
 200
 201
 202
 203
 204
 205
 206
 207
 208
 209
 210
 211
 212
 213
 214
 215
 216
 217
 218
 219
 220
 221
 222
 223
 224
 225
 226
 227
 228
 229
 230
 231
 232
 233
 234
 235
 236
 237
 238
 239
 240
 241
 242
 243
 244
 245
 246
 247
 248
 249
 250
 251
 252
 253
 254
 255
 256
 257
 258
 259
 260
 261
 262
 263
 264
 265
 266
 267
 268
 269
 270
 271
 272
 273
 274
 275
 276
 277
 278
 279
 280
 281
 282
 283
 284
 285
 286
 287
 288
 289
 290
 291
 292
 293
 294
 295
 296
 297
 298
 299
 300
 301
 302
 303
 304
 305
 306
 307
 308
 309
 310
 311
 312
 313
 314
 315
 316
 317
 318
 319
 320
 321
 322
 323
 324
 325
 326
 327
 328
 329
 330
 331
 332
 333
 334
 335
 336
 337
 338
 339
 340
 341
 342
 343
 344
 345
 346
 347
 348
 349
 350
 351
 352
 353
 354
 355
 356
 357
 358
 359
 360
 361
 362
 363
 364
 365
 366
 367
 368
 369
 370
 371
 372
 373
 374
 375
 376
 377
 378
 379
 380
 381
 382
 383
 384
 385
 386
 387
 388
 389
 390
 391
 392
 393
 394
 395
 396
 397
 398
 399
 400
 401
 402
 403
 404
 405
 406
 407
 408
 409
 410
 411
 412
 413
 414
 415
 416
 417
 418
 419
 420
 421
 422
 423
 424
 425
 426
 427
 428
 429
 430
 431
 432
 433
 434
 435
 436
 437
 438
 439
 440
 441
 442
 443
 444
 445
 446
 447
 448
 449
 450
 451
 452
 453
 454
 455
 456
 457
 458
 459
 460
 461
 462
 463
 464
 465
 466
 467
 468
 469
 470
 471
 472
 473
 474
 475
 476
 477
 478
 479
 480
 481
 482
 483
 484
 485
 486
 487
 488
 489
 490
 491
 492
 493
 494
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
------------------------------------------------------------------------------
--                  GtkAda - Ada95 binding for Gtk+/Gnome                   --
--                                                                          --
--                     Copyright (C) 2001-2020, AdaCore                     --
--                                                                          --
-- This library is free software;  you can redistribute it and/or modify it --
-- under terms of the  GNU General Public License  as published by the Free --
-- Software  Foundation;  either version 3,  or (at your  option) any later --
-- version. This library is distributed in the hope that it will be useful, --
-- but WITHOUT ANY WARRANTY;  without even the implied warranty of MERCHAN- --
-- TABILITY or FITNESS FOR A PARTICULAR PURPOSE.                            --
--                                                                          --
-- As a special exception under Section 7 of GPL version 3, you are granted --
-- additional permissions described in the GCC Runtime Library Exception,   --
-- version 3.1, as published by the Free Software Foundation.               --
--                                                                          --
-- You should have received a copy of the GNU General Public License and    --
-- a copy of the GCC Runtime Library Exception along with this program;     --
-- see the files COPYING3 and COPYING.RUNTIME respectively.  If not, see    --
-- <http://www.gnu.org/licenses/>.                                          --
--                                                                          --
------------------------------------------------------------------------------

--  TODO:
--  - handles multiple views of the MDI (through several top-level windows)
--  - Add support for groups (children are associated with groups, and groups
--    can have special colors, can be minimized,...). Groups could be
--    implemented as special MDI_Children ?
--  - Manipulation of the title bar for children (adding buttons, adding
--    pixmaps,...)
--  - Automatically add a new menu bar when a child is floated (settable
--    on a per-child basis).
--  - contextual menu in the title bar of children to dock them, float them,...

with Interfaces.C.Strings;    use Interfaces.C.Strings;

with Ada.Characters.Handling; use Ada.Characters.Handling;
with Ada.Containers.Doubly_Linked_Lists;
with Ada.Containers.Vectors;
with Ada.Exceptions;          use Ada.Exceptions;
with Ada.Strings.Fixed;       use Ada.Strings.Fixed;
with Ada.Strings.Unbounded;   use Ada.Strings.Unbounded;
with Ada.Tags;                use Ada.Tags;
with Ada.Unchecked_Conversion;
with Ada.Unchecked_Deallocation;
with System;                  use System;
with System.Address_Image;

with GNAT.IO;                 use GNAT.IO;
with GNAT.Strings;            use GNAT.Strings;

with Glib.Convert;            use Glib.Convert;
with Glib.Error;              use Glib.Error;
with Glib.G_Icon;             use Glib.G_Icon;
with Glib.Menu;               use Glib.Menu;
with Glib.Object;             use Glib.Object;
with Glib.Properties;         use Glib.Properties;
with Glib.Simple_Action;      use Glib.Simple_Action;
with Glib.Unicode;            use Glib.Unicode;
with Glib.Values;             use Glib.Values;
with Glib.Variant;            use Glib.Variant;

with Cairo;                   use Cairo;

with Pango;                   use Pango;
with Pango.Enums;             use Pango.Enums;
with Pango.Font;              use Pango.Font;
with Pango.Layout;            use Pango.Layout;

with Gdk;                     use Gdk;
with Gdk.Cairo;               use Gdk.Cairo;
with Gdk.Cursor;              use Gdk.Cursor;
with Gdk.Display;             use Gdk.Display;
with Gdk.Event;               use Gdk.Event;
with Gdk.Main;                use Gdk.Main;
with Gdk.Pixbuf;              use Gdk.Pixbuf;
with Gdk.RGBA;                use Gdk.RGBA;
with Gdk.Rectangle;           use Gdk.Rectangle;
with Gdk.Types;               use Gdk.Types;
with Gdk.Types.Keysyms;
with Gdk.Window;              use Gdk.Window;

with Gtk;                     use Gtk;
with Gtk.Accel_Group;         use Gtk.Accel_Group;
with Gtk.Accel_Label;         use Gtk.Accel_Label;
with Gtk.Application;         use Gtk.Application;
with Gtk.Arguments;           use Gtk.Arguments;
with Gtk.Box;                 use Gtk.Box;
with Gtk.Button;              use Gtk.Button;
with Gtk.Check_Menu_Item;     use Gtk.Check_Menu_Item;
with Gtk.Css_Provider;        use Gtk.Css_Provider;
with Gtk.Container;           use Gtk.Container;
with Gtk.Dialog;              use Gtk.Dialog;
with Gtk.Enums;               use Gtk.Enums;
with Gtk.Event_Box;           use Gtk.Event_Box;
with Gtk.Frame;               use Gtk.Frame;
with Gtk.GEntry;              use Gtk.GEntry;
with Gtk.Handlers;
with Gtk.Image;               use Gtk.Image;
with Gtk.Image_Menu_Item;     use Gtk.Image_Menu_Item;
with Gtk.Label;               use Gtk.Label;
with Gtk.Main;                use Gtk.Main;
with Gtk.Menu;                use Gtk.Menu;
with Gtk.Menu_Item;           use Gtk.Menu_Item;
with Gtk.Notebook;            use Gtk.Notebook;
with Gtk.Radio_Menu_Item;     use Gtk.Radio_Menu_Item;
with Gtk.Separator_Menu_Item; use Gtk.Separator_Menu_Item;
with Gtk.Stock;               use Gtk.Stock;
with Gtk.Style_Context;       use Gtk.Style_Context;
with Gtk.Style_Provider;      use Gtk.Style_Provider;
with Gtk.Window;              use Gtk.Window;

with Gtkada.Bindings;         use Gtkada.Bindings;
with Gtkada.Handlers;         use Gtkada.Handlers;
with Gtkada.Multi_Paned;      use Gtkada.Multi_Paned;
with Gtkada.Style;

package body Gtkada.MDI is

   use Glib.Xml_Int;

   Traces : constant Boolean := False;
   --  True if traces should be activated

   Default_Title_Bar_Focus_Color : constant String := "#000088";
   --  Default color to use for the title bar of the child that has
   --  the focus.

   Default_Title_Bar_Color : constant String := "#AAAAAA";
   --  Default color to use for the title bar of children that do not
   --  have the focus.

   Default_Title_Font : constant String := "Sans 8";
   --  Default title font for the children

   Max_Drag_Border_Width : constant Gint := 30;
   --  Width or height of the drag-and-drop borders for each notebook. On the
   --  sides of the MDI, half of it is dedicated to moving the window so that
   --  it occupies that whole side of the MDI

   Drag_Threshold : constant Gint := 20;
   --  Our own threshold (instead of Gtk.Dnd.Check_Threshold), since on
   --  Windows the later seems to be set to 0, and thus we can't change a
   --  notebook page by clicking on its tab without splitting the notebook

   Icon_Size_In_Title : constant := 8;
   Icon_Size_In_Tabs  : constant := 8;

   MDI_Class_Record        : Glib.Object.Ada_GObject_Class :=
     Glib.Object.Uninitialized_Class;
   Child_Class_Record      : aliased Glib.Object.Ada_GObject_Class :=
     Glib.Object.Uninitialized_Class;

   MDI_Signals : constant Interfaces.C.Strings.chars_ptr_array :=
     (1 => New_String (String (Signal_Child_Selected)),
      2 => New_String (String (Signal_Float_Child)),
      3 => New_String (String (Signal_Child_Title_Changed)),
      4 => New_String (String (Signal_Child_Added)),
      5 => New_String (String (Signal_Child_Removed)),
      6 => New_String (String (Signal_Child_Icon_Changed)),
      7 => New_String (String (Signal_Children_Reorganized)),
      8 => New_String (String (Signal_Perspective_Changed)),
      9 => New_String (String (Signal_Unfloat_Child)),
      10 => New_String (String (Signal_Before_Unfloat_Child)),
      11 => New_String (String (Signal_Before_Destroy_Child)),
      12 => New_String (String (Signal_Perspectives_Added)));

   Child_Signals : constant Interfaces.C.Strings.chars_ptr_array :=
     (1 => New_String (String (Signal_Float_Child)),
      2 => New_String (String (Signal_Unfloat_Child)),
      3 => New_String (String (Signal_Before_Unfloat_Child)),
      4 => New_String (String (Signal_Selected)),
      5 => New_String (String (Signal_Child_State_Changed)),
      6 => New_String (String (Signal_Before_Destroy_Child)),
      7 => New_String (String (Signal_Before_Remove_Child)),
      8 => New_String (String (Signal_Maximize_Child)),
      9 => New_String (String (Signal_Unmaximize)));

   function Cb_To_Address is new Ada.Unchecked_Conversion
     (Cb_Gtkada_MDI_Window_MDI_Child_Void, System.Address);
   function Address_To_Cb is new Ada.Unchecked_Conversion
     (System.Address, Cb_Gtkada_MDI_Window_MDI_Child_Void);

   function Cb_To_Address is new Ada.Unchecked_Conversion
     (Cb_GObject_MDI_Child_Void, System.Address);
   function Address_To_Cb is new Ada.Unchecked_Conversion
     (System.Address, Cb_GObject_MDI_Child_Void);

   procedure Connect
     (Object  : access MDI_Window_Record'Class;
      C_Name  : Glib.Signal_Name;
      Handler : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After   : Boolean);

   procedure Connect_Slot
     (Object  : access MDI_Window_Record'Class;
      C_Name  : Glib.Signal_Name;
      Handler : Cb_GObject_MDI_Child_Void;
      After   : Boolean;
      Slot    : access Glib.Object.GObject_Record'Class := null);

   procedure Marsh_GObject_MDI_Child_Void
     (Closure         : GClosure;
      Return_Value    : Glib.Values.GValue;
      N_Params        : Glib.Guint;
      Params          : Glib.Values.C_GValues;
      Invocation_Hint : System.Address;
      User_Data       : System.Address);
   pragma Convention (C, Marsh_GObject_MDI_Child_Void);

   procedure Marsh_MDI_Window_MDI_Child_Void
     (Closure         : GClosure;
      Return_Value    : Glib.Values.GValue;
      N_Params        : Glib.Guint;
      Params          : Glib.Values.C_GValues;
      Invocation_Hint : System.Address;
      User_Data       : System.Address);
   pragma Convention (C, Marsh_MDI_Window_MDI_Child_Void);

   use Widget_List;

   type Selection_Dialog_Record is new Gtk_Window_Record with record
      Current_Child : Widget_List.Glist;
      Label         : Gtk_Label;
      Ent           : Gtk_Entry;
      Length        : Natural := 0;
      Modifier      : Gdk_Modifier_Type;
      Icon          : Gtk.Image.Gtk_Image;
   end record;
   type Selection_Dialog_Access is access all Selection_Dialog_Record'Class;

   type MDI_Check_Menu_Item_Record is new Gtk_Check_Menu_Item_Record with
      record
         MDI : MDI_Window;
      end record;
   type MDI_Check_Menu_Item is access all MDI_Check_Menu_Item_Record'Class;

   type Child_Menu_Item_Record is new Gtk_Image_Menu_Item_Record with record
      Child : MDI_Child;
   end record;
   type Child_Menu_Item is access all Child_Menu_Item_Record'Class;
   --  A menu item used in a contextual menu, to switch to an existing notebook
   --  page.

   type Menu_Item_For_Child_Record is new Gtk_Radio_Menu_Item_Record with
      record
         Child : MDI_Child;
      end record;
   type Menu_Item_For_Child is access all Menu_Item_For_Child_Record'Class;
   --  The special type of item added to the /Window menu. We use a special
   --  widget so that we know which items to remove when we refresh the menu.

   type MDI_Menu_Record is new Gtk_Menu_Record with record
      MDI : MDI_Window;
   end record;
   type MDI_Menu is access all MDI_Menu_Record'Class;

   type MDI_Tab_Record is new Gtk_Event_Box_Record with record
      Timestamp : Natural := 0;
   end record;
   type MDI_Tab is access all MDI_Tab_Record'Class;
   --  A container that requests a specific size for a Gtk_Label that is
   --  displayed in a Gtk_Notebook tab. This is a workaround against the naive
   --  algorithm used in gtk+ to compute the size of tabs. What we want is the
   --  following:
   --  * if the sum of natural sizes of all tab labels is less than the
   --    notebook's size, use the natural size (we need to pass this as the
   --    minimal size as well, since gtk+ notebook always uses the latter).
   --  * Otherwise, we want all tabs to have the same size (down to a minimum
   --    after which we'll let the notebook scroll), and use ellipsization in
   --    the labels.
   --  This label will automatically collaborate with all other such labels in
   --  the same notebook for size negociation.

   MDI_Tab_Class_Record : aliased Glib.Object.Ada_GObject_Class :=
     Glib.Object.Uninitialized_Class;

   procedure Tab_Class_Init (Self : GObject_Class);
   pragma Convention (C, Tab_Class_Init);
   --  Initialize the gtk+ class fields

   function Short_Title_Less_Than (C1, C2 : MDI_Child) return Boolean;
   function Short_Title_Less_Than (C1, C2 : MDI_Child) return Boolean is
   begin
      return C1.Get_Short_Title < C2.Get_Short_Title;
   end Short_Title_Less_Than;

   package Child_Vectors is new Ada.Containers.Vectors (Natural, MDI_Child);
   package Vector_Sort is new Child_Vectors.Generic_Sorting
      (Short_Title_Less_Than);

   procedure Tab_Get_Preferred_Width
     (Label : System.Address; Minimum_Size, Natural_Size : out Gint);
   pragma Convention (C, Tab_Get_Preferred_Width);
   procedure Tab_Get_Preferred_Height
     (Label : System.Address; Minimum_Size, Natural_Size : out Gint);
   pragma Convention (C, Tab_Get_Preferred_Height);

   package Tab_Orientation_Callback is new Gtk.Handlers.User_Callback
     (Gtk_Notebook_Record, Tab_Orientation_Type);

   procedure On_Tab_Orientation
     (Notebook    : access Gtk_Notebook_Record'Class;
      Orientation : Tab_Orientation_Type);
   --  Called when the user selects a new orientation for the tabs.

   type MDI_Notebook_Record is new Gtk.Notebook.Gtk_Notebook_Record with record
      Timestamp : Natural := 0;
      Tab_Size  : Gint;
      --  Used to handle the sizing of tabs (through MDI_Tab_Record). Tab_Size
      --  is set to -1 if the tabs should use their natural size.

      Tab_Orientation : Tab_Orientation_Type := Automatic;
      --  Orientation for the tabs, as requested by the user

      Actual_Tab_Orientation : Tab_Orientation_Type := Horizontal;
      --  The orientation after "Automatic" has been resolved.
   end record;
   type MDI_Notebook is access all MDI_Notebook_Record'Class;
   --  The type of notebooks used in the MDI.

   procedure Menu_Switch_Page (Item : access Gtk_Widget_Record'Class);
   --  Called when a Child_Menu_Item is activated

   package Child_User_Data is new Glib.Object.User_Data (MDI_Child);

   type Children_Array is array (Natural range <>) of Widget_List.Glist;

   procedure Free is new
     Ada.Unchecked_Deallocation (UTF8_String, String_Access);

   package Tab_Pos_Callback is new Gtk.Handlers.User_Callback
     (Gtk_Notebook_Record, Gtk.Enums.Gtk_Position_Type);
   procedure On_Tab_Pos
     (Note : access Gtk_Notebook_Record'Class;
      Pos  : Gtk.Enums.Gtk_Position_Type);
   --  Called when the user changes the position of tabs for a specific
   --  notebook.

   function Button_Pressed
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   function Button_Release
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   function Button_Motion
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean;
   --  Handle the various mouse events on a child (or the tab of a child)

   function Button_Pressed_Forced
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   function Button_Motion_Forced
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean;
   function Button_Release_Forced
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button;
      Allow_Move : Boolean := True) return Boolean;
   --  Same as Button_Pressed, Button_Motion and Button_Release, but perform
   --  an action even if the event is not related to the child's window.

   function Button_Motion_Notebook
     (Note : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean;
   function Button_Release_Notebook
     (Note : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   --  Same as Button_Motion and Button_Release, but monitor the events on the
   --  notebook itself (which is where the event is sent during a reorder
   --  operation).

   function Button_Pressed_On_Title_Icon
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   --  The title icon of a child window was clicked (and if it was a
   --  double click, we want to close that child).

   function On_Notebook_Button_Press
     (Child    : access Gtk_Widget_Record'Class;
      Event    : Gdk.Event.Gdk_Event) return Boolean;
   --  Manage the contextual menu on tabs

   function Hover_Tabs
     (N : MDI_Notebook; X_Root, Y_Root : Gdouble) return Boolean;
   --  Returns True if the given root coordinates are on top of the tabs area
   --  of the specified notebook.

   procedure Child_Widget_Shown
     (Widget : access Gtk_Widget_Record'Class);
   procedure Child_Widget_Hidden
     (Widget : access Gtk_Widget_Record'Class);
   --  Called when the child widget is shown or hidden by the user, to reflect
   --  that fact at the MDI_Child level, no matter whether the child is
   --  currently floating or not.

   procedure On_Select_Child_Update_Close_Menu
      (Item   : access Gtk_Widget_Record'Class;
       Params : GValues);
   --  Called to update the "Close" menu item, when a new child is selected

   procedure On_Float_Child_Update_Menu
      (Check : access Gtk_Widget_Record'Class);
   --  Updates the "/Window/Floating" menu item when a child is floated or
   --  unfloated. The argument is a MDI_Check_Menu_Item

   function Insert_Child_If_Needed
     (MDI   : access MDI_Window_Record'Class;
      Child : MDI_Child) return MDI_Child;
   --  If the child is currently invisible in the perspective, insert it back
   --  in the MDI. In both case, return the child itself

   function Create_Notebook
     (MDI : access MDI_Window_Record'Class) return MDI_Notebook;
   --  Create a notebook, and set it up for drag-and-drop

   procedure Configure_Notebook_Tabs
     (MDI           : access MDI_Window_Record'Class;
      Notebook      : access Gtk_Notebook_Record'Class;
      Hide_If_Empty : Boolean := False);
   --  Configure the visibility and position of notebook tabs.
   --  If there are no visible pages and Hide_If_Empty is true, then the
   --  notebook itself is hidden

   procedure Update_Tab_Color
     (Note    : access MDI_Notebook_Record'Class;
      Focused : Boolean);
   --  Change the background color of the notebook tab containing child,
   --  depending on whether the child is selected or not.

   procedure Set_State
     (Child : access MDI_Child_Record'Class; New_State : State_Type);
   --  Change the state of Child

   function Delete_Child
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean;
   --  Forward a delete_event from the toplevel window to the child

   procedure Destroy_Child (Child : access Gtk_Widget_Record'Class);
   procedure Destroy_Initial_Child
      (Child : access Gtk_Widget_Record'Class);
   --  Called when either the child itself, or the widget we initially put
   --  in it, are destroyed. Remove the child from the MDI properly.

   procedure Destroy_MDI (MDI : access Gtk_Widget_Record'Class);
   --  Called when the MDI is destroyed

   procedure Realize_MDI (MDI : access Gtk_Widget_Record'Class);
   --  Called when the child is realized

   procedure Map_MDI (MDI : access Gtk_Widget_Record'Class);
   --  Called when the child is mapped

   procedure Set_Dnd_Source
     (Widget : access Gtk_Widget_Record'Class;
      Child  : access Gtk_Widget_Record'Class);
   --  Setup a widget as either a source or a target for drag-and-drop ops.

   procedure Get_Dnd_Target
     (MDI              : not null access MDI_Window_Record'Class;
      Parent           : out Gtk_Widget;
      Position         : out Child_Position;
      Parent_Rectangle : out Gdk_Rectangle;
      Rectangle        : out Gdk_Rectangle;
      In_Central       : out Boolean;
      Allowed          : out Boolean);
   --  Return the widget that is the current target for dnd
   --  Position indicated where in the parent the child would be dropped:
   --    Position_Bottom .. Position_Right: To one of the sides
   --    Position_Automatic:                In the center
   --  Rectangle are the coordinates of the target rectangle, relative to the
   --  top-left corner of the MDI. Parent_Rectangle is the area of the
   --  notebook on top of which the pointer is (so Rectangle is a subrectangle
   --  of it).
   --  Allowed is set to False if the current target is invalid for the child.
   --  In_Central is set to true if the Parent is inside the central area.

   procedure Draw_Dnd_Rectangle
     (Child : access MDI_Child_Record'Class;
      Hide_Only : Boolean := False);
   --  Draw the DND rectangle

   procedure Update_Float_Menu (Child : access MDI_Child_Record'Class);
   --  Update the state of the "Float" menu item associated with child

   procedure Put_In_Notebook
     (MDI                      : access MDI_Window_Record'Class;
      Child                    : access MDI_Child_Record'Class;
      Notebook                 : MDI_Notebook := null;
      Initial_Position         : Child_Position := Position_Automatic;
      Force_Parent_Destruction : Boolean := True);
   --  Remove Child from MDI, and put it under control of a notebook.
   --  Notebook can be used to specify a specific notebook to which the child
   --  should be added. If null, this function will compute what notebook
   --  should be used or created depending on the Child's position attribute.
   --  If Force_Parent_Destruction is True, then the notebook containing the
   --  Child will always be destroyed if Child was its last child. Otherwise,
   --  it is possible that the notebook will be kept, albeit empty.

   function Get_Notebook
     (Child : access MDI_Child_Record'Class) return MDI_Notebook;
   --  Return the notebook that directly contains Child

   procedure Get_Sorted_List_Of_Visible_Children
      (MDI  : not null access MDI_Window_Record'Class;
       Vec  : out Child_Vectors.Vector);
   --  Return the list of visible children in Vec.

   procedure Update_Menu_Model_List_Of_Children
     (MDI : not null access MDI_Window_Record'Class);
   procedure Add_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class);
   procedure Remove_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class);
   procedure Update_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class);
   procedure Recompute_Menu
     (Menu : access Gtk_Widget_Record'Class);
   procedure On_Child_Selected_Update_Menu
     (Item : access Gtk_Widget_Record'Class);
   --  Update the contents of the menu with the list of children.

   function Find_Child_Menu
     (Menu  : access MDI_Menu_Record'Class;
      Child : not null access MDI_Child_Record'Class)
      return Menu_Item_For_Child;
   --  Return the Menu of Child in MDI

   procedure Internal_Add_Child_Menu
     (Menu  : access MDI_Menu_Record'Class;
      Child : not null access MDI_Child_Record'Class;
      Group : in out Widget_SList.GSlist);
   --  Add a menu for Child in Group of Menu

   procedure Internal_Update_Menu_Content
     (Child : access MDI_Child_Record'Class;
      It    : not null access Gtk_Radio_Menu_Item_Record'Class);
   --  Destroy and recreate the content of a Menu

   procedure Split_H_Cb    (MDI  : access Gtk_Widget_Record'Class);
   procedure Split_V_Cb    (MDI  : access Gtk_Widget_Record'Class);
   procedure Maximize_Cb   (MDI  : access Gtk_Widget_Record'Class);
   procedure Unmaximize_Cb (MDI  : access Gtk_Widget_Record'Class);
   procedure Float_Cb      (MDI  : access GObject_Record'Class);
   procedure Close_Cb      (MDI  : access Gtk_Widget_Record'Class);
   procedure Focus_Cb      (Item : access Gtk_Check_Menu_Item_Record'Class);
   --  Callbacks for the menu

   procedure On_Action_Floating
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant);
   procedure On_Action_Close
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant);
   procedure On_Action_Split_H
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant);
   procedure On_Action_Split_V
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant);
   procedure On_Action_Select
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant);
   --  Callbacks for the Gaction associated with menus.

   procedure Set_Focus_Child_MDI
     (MDI : access Gtk_Widget_Record'Class; Args : Gtk_Args);
   procedure Set_Focus_Child_Notebook
     (Note : access Gtk_Widget_Record'Class; Args : Gtk_Args);
   --  Called when the widget that has the keyboard focus has changed. This is
   --  used to automatically select its parent MDI_Child.

   function Set_Focus_Child_MDI_Floating
     (Child : access Gtk_Widget_Record'Class) return Boolean;
   --  Same as Set_Focus_Child_MDI, but for floating windows

   function Set_Focus_Child_MDI_From_Tab
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean;
   --  Gives the focus to Child when the notebook tab associated with it is
   --  pressed.

   procedure Set_Focus_Child_Switch_Notebook_Page
     (Note : access Gtk_Widget_Record'Class; Args : Gtk_Args);
   --  Called when a new page from a notebook has been selected, in particular
   --  when using the scroll arrows when there are too many pages to be
   --  displayed

   function Toplevel_Focus_In
     (MDI   : access GObject_Record'Class;
      Event : Gdk_Event_Focus) return Boolean;
   --  Called when the toplevel window that contains a the MDI gains the focus
   --  from the window manager

   procedure Give_Focus_To_Child (Child : MDI_Child);
   --  Give the focus to a specific MDI child
   --  You should never call Grab_Focus directly

   function Matching_Children
     (MDI : access MDI_Window_Record'Class; Str : String)
      return Children_Array;
   --  Return the list of children of the MDI that match Str

   procedure Reset_Title_Bars_And_Colors
     (MDI  : access MDI_Window_Record'Class);
   --  Reset the color and title bar of the MDI Child.

   procedure Update_Selection_Dialog
     (MDI : access MDI_Window_Record'Class; Increment : Integer);
   --  Update the currently selected child in the selection dialog, so that it
   --  matches the filter.

   function Key_Event_In_Floating
     (Win   : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean;
   --  Forward the key press event to the Win

   function Key_Event_Selection_Dialog
     (MDI   : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean;
   --  Handle key events in the selection dialog

   procedure Update_Tab_Label
     (Child : access MDI_Child_Record'Class);
   --  Return the tab to use in the notebooks containing Child

   function Find_Current_In_Central
     (Pane             : access Gtkada_Multi_Paned_Record'Class;
      MDI              : access MDI_Window_Record'Class;
      Group            : Child_Group := Group_Any;
      Initial_Position : Child_Position := Position_Automatic)
      return MDI_Notebook;
   --  Return the first notebook that contains at least one child within the
   --  given Group. The search starts in the notebook that currently has the
   --  focus.
   --  A new notebook is created if needed (ie if no notebook has a child with
   --  the same attribute).

   procedure Removed_From_Notebook
     (Note : access Gtk_Widget_Record'Class; Args : Gtk_Args);
   --  Called when a child is removed from one of the notebooks

   procedure Emit_By_Name_Child
     (Object : System.Address; Name : String; Child : System.Address);
   pragma Import (C, Emit_By_Name_Child, "ada_g_signal_emit_by_name_ptr");

   procedure Emit_By_Name (Object : System.Address; Name : String);
   pragma Import (C, Emit_By_Name, "ada_g_signal_emit_by_name");

   procedure Emit_By_Name_Str (Object : System.Address; Name, Param : String);
   pragma Import (C, Emit_By_Name_Str, "ada_g_signal_emit_by_name_ptr");
   --  Both Name and Param must end with ASCII.NUL

   procedure Internal_Float_Child
     (Child             : access MDI_Child_Record'Class;
      Float             : Boolean;
      Position_At_Mouse : Boolean;
      X, Y              : Gint;
      Width, Height     : Gint := -1);
   --  internal version of Float_Child, where the user can choose whether the
   --  new floating window should be located where the mouse is, or at
   --  coordinates specified by (X, Y).
   --  (Width, Height) can be use to force a specific size.

   procedure Set_Child_Title_Bar (Child : access MDI_Child_Record'Class);
   --  Hide or display the title bar of the child, depending on its status.

   procedure Note_Notify (Data : System.Address; Where : System.Address);
   pragma Convention (C, Note_Notify);
   --  Notified if the old notebook that contained Child is destroyed

   function In_Central_Area
     (MDI   : access MDI_Window_Record'Class;
      Child : access Gtk_Widget_Record'Class) return Boolean;
   --  Whether Child is in the central area

   procedure Move_To_Next_Notebook (Iterator : in out Child_Iterator);
   --  Move to the next notebook for this iterator (does nothing if Iterator
   --  already points to a notebook).

   procedure Freeze_Focus (MDI : access MDI_Window_Record'Class);
   --  Freeze emission of focus changes. Each of these calls should correspond
   --  to a call to Thaw_Focus below.

   procedure Thaw_Focus (MDI : access MDI_Window_Record'Class);
   --  Unfreeze the emission of the focus, and do the actual focus change on
   --  the current child.

   procedure For_Each_Notebook
      (Self         : not null access MDI_Window_Record'Class;
       Central_Only : Boolean;
       Callback     : not null access procedure (Note : MDI_Notebook));
   --  Traverse all notebooks contained in Self.

   package Close_Button is

      --  We use an event box as a basis so that we have a gdk_window
      --  available for handling mouse events. We'll set this event box as
      --  transparent to be able to draw transparent buttons as we wish.
      type Gtkada_MDI_Close_Button_Record is new Gtk_Event_Box_Record
      with record
         Child        : MDI_Child;
         --  The child this button is attached to

         Tab_Over     : Boolean;
         --  Whether the mouse is over the button's container

         Over         : Boolean;
         --  Whether the mouse is over the button

         Pressed      : Boolean;
         --  Whether the button is pressed

         In_Titlebar  : Boolean;
         --  Whether the button is in the title bar or in the tab

         Position     : Gtk_Position_Type;
         --  The tab's position

         Default_Size : Glib.Gint;
         --  The button's default size. The actual drawing depends on the final
         --  allocated space.
      end record;
      type Gtkada_MDI_Close_Button is
        access all Gtkada_MDI_Close_Button_Record'Class;

      procedure Gtk_New
        (Button      : out Gtkada_MDI_Close_Button;
         Tab         : access Gtk_Widget_Record'Class;
         Child       : access MDI_Child_Record'Class;
         Position    : Gtk.Enums.Gtk_Position_Type;
         In_Titlebar : Boolean);
      --  Tab: the button's container. This container shall have a Gdk_Window
      --   to allow mouse motion event retrieval.
      --  Child: the MDI child that button is attached to. This child is closed
      --   upon button click.
      --  In_Titlebar: set to True if the button is in the title bar, to false
      --   if it's in the notebook tab.

   end Close_Button;
   package body Close_Button is separate;

   package Maximize_Action is
      function Is_Maximized_Mode
        (MDI : access MDI_Window_Record'Class) return Boolean;
      --  Return True if a child is currently maximized in MDI

      procedure On_Toggle_Maximize
        (Child : access Gtk_Widget_Record'Class);
      --  Callback when the signal "double_click_child_tab" is sent

      procedure On_Remove_Child
        (Self : access Gtk.Widget.Gtk_Widget_Record'Class);
      --  Called before destroying a child

      procedure Hide_When_Maximized (MDI : access MDI_Window_Record'Class);
      --  Hide the non-maximized MDI children

      procedure Free_Saved_Data (MDI : access MDI_Window_Record'Class);
      --  Free the data used by the maximize feature

   end Maximize_Action;
   package body Maximize_Action is separate;

   function Get_Icon
     (Child : not null access MDI_Child_Record'Class)
      return Gtk_Image;
   --  Retrieve icon. Can return null.

   ---------------------
   -- In_Central_Area --
   ---------------------

   function In_Central_Area
     (MDI   : access MDI_Window_Record'Class;
      Child : access Gtk_Widget_Record'Class) return Boolean
   is
      P : Gtk_Widget := Get_Parent (Child);
   begin
      while P /= null and then P /= Gtk_Widget (MDI) loop
         if P = Gtk_Widget (MDI.Central) then
            return True;
         end if;

         P := Get_Parent (P);
      end loop;

      return False;
   end In_Central_Area;

   ---------------
   -- Set_State --
   ---------------

   procedure Set_State
     (Child : access MDI_Child_Record'Class; New_State : State_Type)
   is
      Old_State : constant State_Type := Child.State;
   begin
      if New_State /= Old_State then
         Child.State := New_State;
         Widget_Callback.Emit_By_Name (Child, Signal_Child_State_Changed);
      end if;
   end Set_State;

   ------------------------
   -- Get_Child_Notebook --
   ------------------------

   function Get_Child_Notebook
     (Child : access MDI_Child_Record'Class) return Gtk.Notebook.Gtk_Notebook
   is
      Notebook : constant MDI_Notebook := Get_Notebook (Child);
   begin
      return Gtk_Notebook (Notebook);
   end Get_Child_Notebook;

   ------------------
   -- Get_Notebook --
   ------------------

   function Get_Notebook
     (Child : access MDI_Child_Record'Class) return MDI_Notebook is
   begin
      case Child.State is
         when Floating  => return null;
         when Invisible => return null;
         when Normal    =>
            if Get_Parent (Child) /= null
              and then Get_Parent (Child).all in Gtk_Notebook_Record'Class
            then
               return MDI_Notebook (Get_Parent (Child));
            end if;

            return null;
      end case;
   end Get_Notebook;

   -------------------------
   -- Set_Focus_Child_MDI --
   -------------------------

   procedure Set_Focus_Child_MDI
     (MDI : access Gtk_Widget_Record'Class; Args : Gtk_Args)
   is
      Widget : Gtk_Widget := Gtk_Widget (To_Object (Args, 1));
   begin
      --  This code must always be executed (we cannot take into account
      --  whether the MDI has the focus or not). Otherwise, clicking
      --  inside an open editor in GPS, for instance, will not properly give
      --  the focus to the MDI child
      if Widget /= null then
         --  The widget is currently either a notebook or the Gtk_Fixed. Get
         --  its focus widget, which is the one we are really interested in.

         Widget := Get_Focus_Child (Gtk_Container (Widget));

         if Widget /= null then
            Print_Debug ("Set_Focus_Child_MDI");
            Set_Focus_Child (MDI_Window (MDI), Containing => Widget);
         end if;
      end if;

      --  No need to call the parent's set_focus_child, this is called
      --  automatically when the signal is propagated.
   end Set_Focus_Child_MDI;

   ------------------------------------------
   -- Set_Focus_Child_Switch_Notebook_Page --
   ------------------------------------------

   procedure Set_Focus_Child_Switch_Notebook_Page
     (Note : access Gtk_Widget_Record'Class; Args : Gtk_Args)
   is
      N     : constant Gtk_Notebook := Gtk_Notebook (Note);
      Page  : constant Guint := To_Guint (Args, 2);
      Child : MDI_Child;
   begin
      Child := MDI_Child (Get_Nth_Page (N, Gint (Page)));

      --  Avoid setting the focus child at this point when we are performing
      --  a Drag'n'Drop operation: the focus will be set properly once it
      --  has fully finished.

      if Child /= null and then Child.MDI.In_Drag = No_Drag then
         Print_Debug ("Set_Focus_Child_Switch_Notebook_Page "
                      & Get_Title (Child));
         Set_Focus_Child (Child);
      end if;
   end Set_Focus_Child_Switch_Notebook_Page;

   ------------------------------
   -- Set_Focus_Child_Notebook --
   ------------------------------

   procedure Set_Focus_Child_Notebook
     (Note : access Gtk_Widget_Record'Class; Args : Gtk_Args)
   is
      pragma Unreferenced (Note);
      Widget : constant Gtk_Widget := Gtk_Widget (To_Object (Args, 1));
   begin
      --  This code must always be executed (we cannot take into account
      --  whether the notebook has the focus or not). Otherwise, clicking
      --  inside an open editor in GPS, for instance, will not properly give
      --  the focus to the MDI child
      if Widget /= null
        and then Widget.all in MDI_Child_Record'Class
      then
         Print_Debug ("Set_Focus_Child_Notebook "
                      & Get_Title (MDI_Child (Widget)));
         Set_Focus_Child (MDI_Child (Widget));
      end if;
   end Set_Focus_Child_Notebook;

   ----------------------------------
   -- Set_Focus_Child_MDI_Floating --
   ----------------------------------

   function Set_Focus_Child_MDI_Floating
     (Child : access Gtk_Widget_Record'Class) return Boolean
   is
      C  : constant MDI_Child := MDI_Child (Child);
   begin
      Print_Debug ("Set_Focus_Child_MDI_Floating");
      Set_Focus_Child (C);
      return False;
   end Set_Focus_Child_MDI_Floating;

   ----------------------------------
   -- Set_Focus_Child_MDI_From_Tab --
   ----------------------------------

   function Set_Focus_Child_MDI_From_Tab
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean
   is
      Tmp : Boolean;
      pragma Unreferenced (Tmp);
   begin
      if Event.The_Type = Button_Release then
         Tmp := Button_Release (Child => Gtk_Widget (Child), Event => Event);
         return False;

      elsif Event.Button = 1 then
         --  Let the event through if the child already has the focus. This way
         --  the notebook tab of the focus child can still be used for
         --  drag-and-drop
         if MDI_Child (Child).MDI.Focus_Child = MDI_Child (Child) then
            return False;

         else
            --  Process the button press event to select the child and start a
            --  drag-and-drop operation

            Tmp := Button_Pressed_Forced (Child => Child, Event => Event);

            --  is called as a result of a button_press event in the
            --  notebook's. tabs The call to Set_Focus_Child above raises the
            --  child and gives it the focus appropriately. However, if we let
            --  the signal go through it will be handled by the notebook, which
            --  will not see change in the current page, and will give the
            --  focus to the tab itself, not to the page's contents.

            return True;
         end if;
      end if;
      return False;
   end Set_Focus_Child_MDI_From_Tab;

   -----------------------
   -- Toplevel_Focus_In --
   -----------------------

   function Toplevel_Focus_In
     (MDI   : access GObject_Record'Class;
      Event : Gdk_Event_Focus) return Boolean
   is
      pragma Unreferenced (Event);
      M   : constant MDI_Window := MDI_Window (MDI);
      Win : constant Gtk_Window := Gtk_Window (Get_Toplevel (M));
   begin
      Print_Debug ("Toplevel_Focus_In");

      --  When the main window gains the focus, make sure to give the focus
      --  to the MDI child that had the focus before leaving the main window.

      if (M.Focus_Child /= null
          and then M.Focus_Child.State = Floating)
        or else M.Focus_Child = null
      then
         declare
            Focus_Widget : constant Gtk_Widget := Win.Get_Focus;
            Child        : MDI_Child := null;
         begin
            if Focus_Widget /= null then
               Child := Find_MDI_Child_From_Widget (Focus_Widget);
            end if;

            --  If the focus widget of the main window is contained in a MDI
            --  child, make sure to give it the focus too.
            --
            --  Otherwise, give the focus to the last focused MDI child of the
            --  main window.

            if Child /= null then
               Give_Focus_To_Child (Child);
               M.Set_Focus_Child (Child);
            end if;
         end;
      end if;

      return False;
   end Toplevel_Focus_In;

   ---------------------------
   -- Setup_Toplevel_Window --
   ---------------------------

   procedure Setup_Toplevel_Window
     (MDI    : access MDI_Window_Record;
      Parent : access Gtk.Window.Gtk_Window_Record'Class) is
   begin
      Parent.On_Focus_In_Event (Toplevel_Focus_In'Access, MDI);
   end Setup_Toplevel_Window;

   -------------
   -- Gtk_New --
   -------------

   procedure Gtk_New
     (MDI   : out MDI_Window;
      Group : access Gtk.Accel_Group.Gtk_Accel_Group_Record'Class;
      Independent_Perspectives  : Boolean := False) is
   begin
      MDI := new MDI_Window_Record;
      Gtkada.MDI.Initialize (MDI, Group, Independent_Perspectives);
   end Gtk_New;

   --------------
   -- Get_Type --
   --------------

   function Get_Type return Glib.GType is
      Signal_Parameters : constant Glib.Object.Signal_Parameter_Types
         (Integer (MDI_Signals'First) .. Integer (MDI_Signals'Last), 1 .. 1) :=
        (1 => (1 => GType_Pointer),
         2 => (1 => GType_Pointer),
         3 => (1 => GType_Pointer),
         4 => (1 => GType_Pointer),
         5 => (1 => GType_Pointer),
         6 => (1 => GType_Pointer),
         7 => (1 => GType_None),
         8 => (1 => GType_None),
         9 => (1 => GType_Pointer),
         10 => (1 => GType_Pointer),
         11 => (1 => GType_Pointer),
         12 => (1 => GType_String));
   begin
      Glib.Object.Initialize_Class_Record
        (Ancestor     => Gtkada.Multi_Paned.Get_Type,
         Signals      => MDI_Signals,
         Class_Record => MDI_Class_Record,
         Type_Name    => "GtkAdaMDI",
         Parameters   => Signal_Parameters);
      return MDI_Class_Record.The_Type;
   end Get_Type;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (MDI   : access MDI_Window_Record'Class;
      Group : access Gtk.Accel_Group.Gtk_Accel_Group_Record'Class;
      Independent_Perspectives  : Boolean := False)
   is
      Ctx : Gtk_Style_Context;
      Success : Boolean;
      Title_Color : Gdk.RGBA.Gdk_RGBA;
      Focus_Color : Gdk.RGBA.Gdk_RGBA;
   begin
      G_New (MDI, Gtkada.MDI.Get_Type);     --  void if already initialized
      Gtkada.Multi_Paned.Initialize (MDI);  --  Initialize parent Ada fields

      Get_Style_Context (MDI).Add_Class ("mdi");

      --  Request a null size, so that the window can be resized at will, even
      --  though we have played with Set_Size_Request on the children.

      Set_Size_Request (MDI, 0, 0);

      --  The MDI must have a window, so that we can change the background
      --  color. No other notebook or paned inside has a window

      Set_Has_Window (MDI, True);

      MDI.Group := Gtk_Accel_Group (Group);
      MDI.Independent_Perspectives := Independent_Perspectives;

      Set_Dnd_Message (MDI, "");

      Parse (Title_Color, Default_Title_Bar_Color, Success);
      Parse (Focus_Color, Default_Title_Bar_Focus_Color, Success);

      Gtk_New (MDI.Css_Provider);
      Gtk.Style_Context.Add_Provider_For_Screen
        (Get_Style_Context (MDI).Get_Screen, +MDI.Css_Provider,
         Priority => Gtk.Style_Provider.Priority_Settings);

      Ctx := Get_Style_Context (MDI);
      Ctx.Get_Color (Gtk_State_Flag_Normal, MDI.Default_Title_Color);

      Configure
        (MDI,
         Title_Bar_Color   => Title_Color,
         Focus_Title_Color => Focus_Color);

      --  Create a default empty central area. That will be overridden if the
      --  user loads a perspective later on

      Gtk_New (MDI.Central);

      Add_Child (MDI, MDI.Central);

      --  Put an empty notebook in the MDI, which will act as a recipient for
      --  the Position_Default widgets

      Add_Child
        (MDI.Central, New_Child => Create_Notebook (MDI),
         Width       => -1,
         Height      => -1,
         Orientation => Orientation_Vertical);

      Widget_Callback.Connect
        (MDI, Gtk.Widget.Signal_Realize,
         Widget_Callback.To_Marshaller (Realize_MDI'Access));
      Widget_Callback.Connect
        (MDI, Gtk.Widget.Signal_Map,
         Widget_Callback.To_Marshaller (Map_MDI'Access));
      Widget_Callback.Connect
        (MDI, Signal_Destroy,
         Widget_Callback.To_Marshaller (Destroy_MDI'Access));
      Widget_Callback.Connect
        (MDI, Signal_Set_Focus_Child, Set_Focus_Child_MDI'Access);
   end Initialize;

   -----------------------
   -- Matching_Children --
   -----------------------

   function Matching_Children
     (MDI : access MDI_Window_Record'Class; Str : String) return Children_Array
   is
      Count    : constant Natural := Natural (Length (MDI.Items));
      Children : Children_Array (1 .. Count);
      L        : Widget_List.Glist := MDI.Items;
      Ind      : Natural := Children'First;
      C        : MDI_Child;
   begin
      while L /= Null_List loop
         C := MDI_Child (Get_Data (L));
         if Str = ""
           or else Index (To_Lower (Get_Short_Title (C)), Str) /= 0
         then
            Children (Ind) := L;
            Ind := Ind + 1;
         end if;
         L := Next (L);
      end loop;
      return Children (Children'First .. Ind - 1);
   end Matching_Children;

   -----------------------------
   -- Update_Selection_Dialog --
   -----------------------------

   procedure Update_Selection_Dialog
     (MDI : access MDI_Window_Record'Class; Increment : Integer)
   is
      D        : constant Selection_Dialog_Access :=
                   Selection_Dialog_Access (MDI.Selection_Dialog);
      Str      : constant UTF8_String := Get_Text (D.Ent);
      Children : constant Children_Array :=
                   Matching_Children (MDI, To_Lower (Str));
      Index    : Integer := Children'First;
      Tmp      : Integer;
      Pos      : Gint := -1;

   begin
      --  Update graphically the list of children matching the filter

      D.Length := Str'Length;
      Insert_Text (D.Ent, " {", Pos);
      Set_Position (D.Ent, Gint (D.Length));

      --  Find the index of the current child

      if Children'Length /= 0 then
         while Index <= Children'Last loop
            exit when Children (Index) = D.Current_Child;
            Index := Index + 1;
         end loop;

         Index := Index + Increment;

         if Index > Children'Last then
            Index := Children'First;
         elsif Index < Children'First then
            Index := Children'Last;
         end if;

         Tmp := Index;
         loop
            if Tmp /= Index then
               Insert_Text (D.Ent, ",", Pos);
            end if;

            Insert_Text
              (D.Ent,
               Get_Short_Title (MDI_Child (Get_Data (Children (Tmp)))),
               Pos);

            Tmp := (Tmp + 1 - Children'First) mod Children'Length
              + Children'First;
            exit when Tmp = Index;
         end loop;

         D.Current_Child := Children (Index);

      else
         D.Current_Child := Null_List;
      end if;

      Insert_Text (D.Ent, "}", Pos);

      if D.Current_Child = Null_List then
         Set_Text (D.Label, "");
         Set_Child_Visible (D.Icon, False);
      else
         declare
            C : MDI_Child;
            Scaled : Gdk_Pixbuf;
         begin
            C := MDI_Child (Get_Data (D.Current_Child));
            Set_Text (D.Label, Get_Short_Title (C));

            if C.Icon_Name /= null then
               D.Icon.Set_From_Icon_Name
                  (C.Icon_Name.all, Size => Icon_Size_Menu);
            elsif C.Title_Icon /= null then
               Scaled := Get_Icon (C);
               Set_Child_Visible (D.Icon, Scaled /= null);
               if Scaled /= null then
                  Scaled := Scale_Simple (Scaled, 32, 32);
                  Gtk.Image.Set (D.Icon, Scaled);
                  Unref (Scaled);
               end if;
            end if;
         end;
      end if;
   end Update_Selection_Dialog;

   --------------------------------
   -- Key_Event_Selection_Dialog --
   --------------------------------

   function Key_Event_Selection_Dialog
     (MDI   : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean
   is
      M     : constant MDI_Window := MDI_Window (MDI);
      D     : constant Selection_Dialog_Access :=
                Selection_Dialog_Access (M.Selection_Dialog);
      Close : Boolean := False;
      Tmp   : Boolean;
      Key   : Gdk_Key_Type;
      pragma Unreferenced (Tmp);
   begin
      --  This isn't a key press for the next_child or previous_child
      --  functions, since those are handled by the outside application.

      if Get_Event_Type (Event) = Key_Press then
         Key := Event.Key.Keyval;

         if Key = Gdk.Types.Keysyms.GDK_BackSpace
           or else Key = Gdk.Types.Keysyms.GDK_Delete
         then
            Delete_Text (D.Ent, Gint (D.Length) - 1, -1);
         else
            Delete_Text (D.Ent, Gint (D.Length), -1);

            Event.Key.State := 0;
            Tmp := Return_Callback.Emit_By_Name
              (D.Ent, "key_press_event", Event);
         end if;

         Update_Selection_Dialog (M, 0);
         return True;

      elsif Get_Event_Type (Event) = Key_Release then
         Key := Event.Key.Keyval;

         --  As soon as one of the modifiers of the initial key is released,
         --  we close the dialog
         if (D.Modifier and Control_Mask) /= 0
           and then
             (Key = Gdk.Types.Keysyms.GDK_Control_L
              or else Key = Gdk.Types.Keysyms.GDK_Control_R
              or else Key = Gdk.Types.Keysyms.GDK_ISO_Next_Group)
         then
            Close := True;

         elsif (D.Modifier and Mod1_Mask) /= 0
            and then (Key = Gdk.Types.Keysyms.GDK_Meta_L
                      or else Key = Gdk.Types.Keysyms.GDK_Meta_R
                      or else Key = Gdk.Types.Keysyms.GDK_Alt_L
                      or else Key = Gdk.Types.Keysyms.GDK_Alt_R)
         then
            Close := True;

         elsif (D.Modifier and Shift_Mask) /= 0
           and then (Key = Gdk.Types.Keysyms.GDK_Shift_L
                     or else Key = Gdk.Types.Keysyms.GDK_Shift_R)
         then
            Close := True;

         elsif Key = Gdk.Types.Keysyms.GDK_Escape then
            Close := True;
         end if;

         if Close then
            if D.Current_Child /= Null_List then
               Set_Focus_Child
                 (MDI_Child (Widget_List.Get_Data (D.Current_Child)));
            end if;

            Keyboard_Ungrab (Time => 0);
            M.Selection_Dialog.Grab_Remove;
            Destroy (M.Selection_Dialog);
            M.Selection_Dialog := null;
         end if;

         return True;
      end if;

      return False;
   end Key_Event_Selection_Dialog;

   -----------------------
   -- For_Each_Notebook --
   -----------------------

   procedure For_Each_Notebook
      (Self         : not null access MDI_Window_Record'Class;
       Central_Only : Boolean;
       Callback     : not null access procedure (Note : MDI_Notebook))
   is
      procedure For_Multi_Paned
         (Pane : not null access Gtkada_Multi_Paned_Record'Class);
      --  Process a specific multi-pane recursively

      procedure For_Multi_Paned
         (Pane : not null access Gtkada_Multi_Paned_Record'Class)
      is
         Iter : Gtkada.Multi_Paned.Child_Iterator := Pane.Start;
         W    : Gtk_Widget;
      begin
         while not At_End (Iter) loop
            W := Get_Widget (Iter);
            if W = null then
               null;   --  separator
            elsif W.all in Gtkada_Multi_Paned_Record'Class then
               For_Multi_Paned (Gtkada_Multi_Paned (W));
            else
               Callback (MDI_Notebook (W));
            end if;

            Next (Iter);
         end loop;
      end For_Multi_Paned;

   begin
      if Central_Only then
         For_Multi_Paned (Self.Central);
      else
         For_Multi_Paned (Self);
      end if;
   end For_Each_Notebook;

   ----------------------------------------
   -- Check_Interactive_Selection_Dialog --
   ----------------------------------------

   procedure Check_Interactive_Selection_Dialog
     (MDI          : access MDI_Window_Record;
      Event        : Gdk.Event.Gdk_Event;
      Move_To_Next : Boolean;
      Only_Group   : Child_Group := Group_Any)
   is
      D         : Selection_Dialog_Access;
      Box, HBox : Gtk_Box;
      Frame     : Gtk_Frame;
      Tmp       : Gdk_Grab_Status;
      pragma Unreferenced (Tmp);

   begin
      if MDI.Items = Null_List then
         return;
      end if;

      if Event = null
        or else (Get_Event_Type (Event) /= Key_Press
                 and then Get_Event_Type (Event) /= Key_Release)
      then
         if MDI.Focus_Child = null then
            --  Nothing to do
            return;
         end if;

         --  We can't simply look at MDI.Items, since otherwise we
         --  will only be traversing the same two items in the
         --  following scenario: split main window in 3; select window 1;
         --  move to next window (window 2 is selected and moved to front
         --  of MDI.Items); move to next window (window 1 is selected
         --  again).

         declare
            Current : constant MDI_Notebook := Get_Notebook (MDI.Focus_Child);
            Child   : MDI_Child;

            procedure On_Notebook (Note : MDI_Notebook);
            --  Called for each notebook in the MDI

            First_Notebook : MDI_Notebook;
            Prev_Notebook  : MDI_Notebook;
            Next_Notebook  : MDI_Notebook;
            Last_Notebook  : MDI_Notebook;
            --  The notebook just before or just after the current notebook,
            --  for which the current child belongs to the right group.

            Found : Boolean := False;

            procedure On_Notebook (Note : MDI_Notebook) is
               C : MDI_Child;
            begin
               if Note = Current then
                  Found := True;
               else
                  C := MDI_Child (Note.Get_Nth_Page (Note.Get_Current_Page));
                  if Only_Group /= Group_Any
                     and then C.Group /= Only_Group
                  then
                     --  Not a notebook we are interested in
                     return;
                  end if;

                  --  Store first and last notebook, to wrap around
                  Last_Notebook := Note;
                  if First_Notebook = null then
                     First_Notebook := Note;
                  end if;

                  if not Found then
                     Prev_Notebook := Note;
                  elsif Next_Notebook = null then
                     Next_Notebook := Note;
                  end if;
               end if;
            end On_Notebook;

         begin
            For_Each_Notebook
               (MDI, Central_Only => False, Callback => On_Notebook'Access);

            if Move_To_Next then
               if Next_Notebook = null then
                  Next_Notebook := First_Notebook;
               end if;

               if Next_Notebook /= null then
                  Child := MDI_Child (Next_Notebook.Get_Nth_Page
                     (Next_Notebook.Get_Current_Page));
                  Set_Focus_Child (Child);
               end if;

            else
               if Prev_Notebook = null then
                  Prev_Notebook := Last_Notebook;
               end if;

               if Prev_Notebook /= null then
                  Child := MDI_Child (Prev_Notebook.Get_Nth_Page
                     (Prev_Notebook.Get_Current_Page));
                  Set_Focus_Child (Child);
               end if;
            end if;
         end;
         return;
      end if;

      if MDI.Selection_Dialog = null then
         D := new Selection_Dialog_Record;
         Initialize (D, Window_Popup);

         if MDI.All_Floating_Mode then
            Set_Position (D, Win_Pos_Mouse);
         else
            Set_Transient_For (D, Gtk_Window (Get_Toplevel (MDI)));
            Set_Position (D, Win_Pos_Center_On_Parent);
         end if;
         Set_Default_Size (D, 300, 70);

         Gtk_New (Frame);
         Add (D, Frame);

         --  By default, switch between the last two selected items
         D.Current_Child := First (MDI.Items);

         Gtk_New_Vbox (Box, Homogeneous => False);
         Add (Frame, Box);

         Gtk_New_Hbox (HBox, Homogeneous => False);
         Pack_Start (Box, HBox, Expand => False);

         Gtk_New (D.Icon);
         Pack_Start (HBox, D.Icon, Expand => False);

         Gtk_New (D.Label);
         Pack_Start (HBox, D.Label, Expand => True, Fill => True);

         Gtk_New (D.Ent);
         Pack_Start (Box, D.Ent, Expand => True);

         Show_All (D);
         D.Modifier := Get_State (Event);

         MDI.Selection_Dialog := Gtk_Widget (D);

         --  Make sure all the key events are forwarded to us, as otherwise
         --  if the mouse was moving out of the window we wouldn't the
         --  events
         Tmp := Keyboard_Grab (Get_Window (D), True, Time => 0);
         D.Grab_Add;

         Grab_Focus (D.Ent);

         Return_Callback.Object_Connect
           (D, Signal_Key_Release_Event,
            Return_Callback.To_Marshaller
              (Key_Event_Selection_Dialog'Access), MDI);
         Return_Callback.Object_Connect
           (D, Signal_Key_Press_Event,
            Return_Callback.To_Marshaller
              (Key_Event_Selection_Dialog'Access), MDI);
      else
         D := Selection_Dialog_Access (MDI.Selection_Dialog);
         Delete_Text (D.Ent, Gint (D.Length), -1);
      end if;

      if Move_To_Next then
         Update_Selection_Dialog (MDI, +1);
      else
         Update_Selection_Dialog (MDI, -1);
      end if;
   end Check_Interactive_Selection_Dialog;

   ---------------
   -- Configure --
   ---------------

   procedure Configure
     (MDI                       : access MDI_Window_Record;
      Opaque_Resize             : Boolean             := False;
      Close_Floating_Is_Unfloat : Boolean             := True;
      Title_Font                : Pango_Font_Description := null;
      Title_Bar_Color           : Gdk.RGBA.Gdk_RGBA := Gdk.RGBA.Null_RGBA;
      Focus_Title_Color         : Gdk.RGBA.Gdk_RGBA := Gdk.RGBA.Null_RGBA;
      Draw_Title_Bars           : Title_Bars_Policy   := Always;
      Tabs_Position             : Gtk.Enums.Gtk_Position_Type :=
        Gtk.Enums.Pos_Top;
      Show_Tabs_Policy          : Show_Tabs_Policy_Enum := Automatic;
      Homogeneous_Tabs          : Boolean := True;
      Tabs_Orientation          : Tab_Orientation_Type := Automatic)
   is
      C            : MDI_Child;
      Need_Redraw  : Boolean := MDI.Draw_Title_Bars /= Draw_Title_Bars;
      Iter         : Child_Iterator;
      Old_Tabs_Pos : constant Gtk_Position_Type := MDI.Tabs_Position;
      Pos_Changed  : constant Boolean := Old_Tabs_Pos /= Tabs_Position;
      Old_Tabs_Rot : constant Tab_Orientation_Type := MDI.Tabs_Orientation;
      Rot_Changed  : constant Boolean := Old_Tabs_Rot /= Tabs_Orientation;
      Note         : Gtk_Notebook;
      Success      : Boolean;
      pragma Unreferenced (Success);
      Default_Bg   : Gdk.RGBA.Gdk_RGBA;
      Homogeneous_Changed : constant Boolean :=
         MDI.Homogeneous_Tabs /= Homogeneous_Tabs;

   begin
      MDI.Close_Floating_Is_Unfloat := Close_Floating_Is_Unfloat;
      MDI.Draw_Title_Bars  := Draw_Title_Bars;
      MDI.Tabs_Position    := Tabs_Position;
      MDI.Tabs_Orientation := Tabs_Orientation;
      MDI.Show_Tabs_Policy := Show_Tabs_Policy;
      MDI.Homogeneous_Tabs := Homogeneous_Tabs;

      Set_Opaque_Resizing (MDI, Opaque_Resize);

      if MDI.Central /= null then
         MDI.Central.Set_Opaque_Resizing (Opaque_Resize);
      end if;

      if MDI.Title_Font /= null then
         Free (MDI.Title_Font);
      end if;

      if Title_Font = null then
         MDI.Title_Font := From_String (Default_Title_Font);
      else
         MDI.Title_Font := Copy (Title_Font);
      end if;

      MDI.Title_Bar_Height :=
        2 + Get_Size (MDI.Title_Font) / Pango.Enums.Pango_Scale;

      if MDI.Focus_Title_Color /= Focus_Title_Color
        or MDI.Title_Bar_Color /= Title_Bar_Color
      then
         MDI.Focus_Title_Color := Focus_Title_Color;
         MDI.Title_Bar_Color := Title_Bar_Color;

         Get_Style_Context (MDI).Get_Background_Color
           (Gtk.Enums.Gtk_State_Flag_Normal,
            Default_Bg);

         declare
            Err  : aliased GError;
            C    : constant String :=
                     Gdk.RGBA.To_String (MDI.Focus_Title_Color);
            Bg   : constant String :=
                     Gdk.RGBA.To_String (MDI.Title_Bar_Color);
            LF   : Character renames ASCII.LF;

            --  This will highlight the whole notebook in blue when not
            --  using a proper theme. Otherwise, the theme will override
            --  some of the settings, and only the current focused tab
            --  will be highlighted with the title color.
            --  The theme should define
            --       .mdifocused {background-image: -gtk-gradient{...}}
            --  or some such, to give a background to the tabs. We can't
            --  do it here, since the following is loaded with a higher
            --  priority than the theme, and thus it would override the
            --  theme.

            Css  : constant String :=
                     ".mdititle {" & LF & --  Normal title
                     "  border-width: 2px;" & LF &
                     "  background-image: -gtk-gradient(" & LF &
                     "            linear," & LF &
                     "            left top, left bottom," & LF &
                     "            from(" & Bg & ")," & LF &
                     "            to(shade(" & Bg & ",1.1)));" & LF &
                     "}" & LF &
                     ".mdifocused .mdititle {" & LF & --  Focused title
                     "  background-image: -gtk-gradient(" & LF &
                     "            linear," & LF &
                     "            left top, left bottom," & LF &
                     "            from(" & C & ")," & LF &
                     "            to(shade(" & C & ",1.1)));" & LF &
                     "}";
         begin
            Success := MDI.Css_Provider.Load_From_Data (Css, Err'Access);
         end;
      end if;

      --  Ignore changes in colors, unless the MDI is realized

      Iter := First_Child
        (MDI, Group_By_Notebook => True, Visible_Only => True);

      loop
         C := Get (Iter);
         exit when C = null;

         if Get_Notebook (Iter) /= Note then
            Note := Get_Notebook (Iter);

            if Note /= null then
               --  Unless we had a specific position for tabs in this notebook

               if Pos_Changed
                 and then Get_Tab_Pos (Note) = Old_Tabs_Pos
               then
                  On_Tab_Pos (Note, MDI.Tabs_Position);
               elsif Rot_Changed
                 and then MDI_Notebook (Note).Tab_Orientation = Old_Tabs_Rot
               then
                  On_Tab_Orientation (Note, MDI.Tabs_Orientation);
               end if;

               Configure_Notebook_Tabs (MDI, Note);
            end if;
         end if;

         if Homogeneous_Changed then
            Update_Tab_Label (C);
         end if;

         Next (Iter);
      end loop;

      if MDI.Get_Realized then
         if Title_Bar_Color /= Null_RGBA then
            Need_Redraw := True;
         end if;

         if Focus_Title_Color /= Null_RGBA then
            Need_Redraw := True;
         end if;
      end if;

      Reset_Title_Bars_And_Colors (MDI);

      if Need_Redraw then
         Queue_Draw (MDI);
      end if;
   end Configure;

   ------------------------------
   -- Independent_Perspectives --
   ------------------------------

   function Independent_Perspectives
     (MDI : access MDI_Window_Record) return Boolean is
   begin
      return MDI.Independent_Perspectives;
   end Independent_Perspectives;

   ---------------------------------
   -- Reset_Title_Bars_And_Colors --
   ---------------------------------

   procedure Reset_Title_Bars_And_Colors
     (MDI  : access MDI_Window_Record'Class)
   is
      List : Widget_List.Glist;
      C    : MDI_Child;
   begin
      List := First (MDI.Items);
      while List /= Null_List loop
         C := MDI_Child (Get_Data (List));
         C.Title_Label.Override_Font (MDI.Title_Font);
         Set_Child_Title_Bar (C);
         List := Widget_List.Next (List);
      end loop;
   end Reset_Title_Bars_And_Colors;

   -----------------
   -- Realize_MDI --
   -----------------

   procedure Realize_MDI (MDI : access Gtk_Widget_Record'Class) is
      M : constant MDI_Window := MDI_Window (MDI);

   begin
      if M.Cursor_Cross = null then
         Gdk_New (M.Cursor_Cross, Cross);
      end if;

      Queue_Resize (MDI);
   end Realize_MDI;

   -------------
   -- Map_MDI --
   -------------

   procedure Map_MDI (MDI : access Gtk_Widget_Record'Class) is
      M : constant MDI_Window := MDI_Window (MDI);

   begin
      if M.Focus_Child /= null then
         Update_Tab_Color (Get_Notebook (M.Focus_Child), True);
      end if;
   end Map_MDI;

   -----------------
   -- Destroy_MDI --
   -----------------

   procedure Destroy_MDI (MDI : access Gtk_Widget_Record'Class) is
      M   : constant MDI_Window := MDI_Window (MDI);
      Tmp : Widget_List.Glist := First (M.Items);
      N   : Widget_List.Glist;
      C   : MDI_Child;
   begin
      Print_Debug ("Destroy_MDI");
      --  Note: we only destroy the floating children. Other children will be
      --  destroyed when their parent container is destroyed, so we have
      --  nothing to do for them.

      while Tmp /= Null_List loop
         --  Get the next field first, since Destroy will actually destroy Tmp

         C := MDI_Child (Get_Data (Tmp));

         N := Next (Tmp);
         if C.State = Floating then
            Print_Debug
              ("Destroy_MDI => Destroying floating " & Get_Title (C));
            Destroy (C);

         elsif C.State = Invisible then
            Print_Debug
              ("Destroy_MDI => Unref invisible " & Get_Title (C));
            Set_State (C, Normal);
            Unref (C);
         else
            Print_Debug
              ("Destroy_MDI => Do nothing to " & Get_Title (C));
            --  Pretend the child is not docked or floating. Otherwise,
            --  Destroy_Child would try to undock the child. Standard gtk+
            --  containers handle this by having this destroy callback called
            --  last, but it isn't doable from GtkAda since it means modifying
            --  the pointer-to-subprogram in the Class struct.
            Set_State (C, Normal);
         end if;
         Tmp := N;
      end loop;

      Free (M.Items);

      if M.Cursor_Cross /= null then
         Unref (M.Cursor_Cross);
      end if;

      Free (M.Dnd_Message);
      Free (M.Perspectives);
      Free (M.View_Contents);
      Free (M.Perspective_Names);

      Free (M.Accel_Path_Prefix);
   end Destroy_MDI;

   -----------
   -- Close --
   -----------

   procedure Close
     (MDI   : access MDI_Window_Record;
      Child : access Gtk.Widget.Gtk_Widget_Record'Class;
      Force : Boolean := False)
   is
      C : constant MDI_Child := Find_MDI_Child (MDI, Child);
   begin
      if C /= null then
         Close_Child (C, Force);
      end if;
   end Close;

   -----------------
   -- Close_Child --
   -----------------

   procedure Close_Child
     (Child : not null access MDI_Child_Record'Class;
      Force : Boolean := False)
   is
      MDI   : constant MDI_Window := Child.MDI;
      Event : Gdk_Event;
      Prevent_Delete : Boolean;
      Focus_Child : MDI_Child;
   begin
      --  Don't do anything for now if the MDI isn't realized, since we
      --  can't send create the event anyway.

      Print_Debug ("Close_Child, " & Get_Title (Child) & " force="
                   & Boolean'Image (Force));

      Widget_Callback.Emit_By_Name (Child, Signal_Before_Remove_Child);

      if MDI.Get_Realized then
         --  For a top-level window, we must rebuild the initial widget
         --  temporarily, so that the application can do all the test it wants.
         --  However, we need to restore the initial state before calling
         --  Dock_Child and Float_Child below.
         --  We should not test this when the MDI is being destroyed, though,
         --  to avoid memory leaks

         if Force
           or else MDI.In_Destruction
         then
            Prevent_Delete := False;
         else
            Print_Debug ("Close_Child, emitting delete_event", Debug_Increase);

            if (Child.Flags and Destroy_Button) = 0 then
               Prevent_Delete := True;
            else
               Gdk_New (Event, Delete);
               Event.Any.Window := Get_Window (Child.Initial);

               Prevent_Delete := Return_Callback.Emit_By_Name
                 (Child.Initial, "delete_event", Event);

               --  Unref'ing the event causes an unref of Event.Any.Window.
               --  Avoid this by setting this field to null before freeing the
               --  event.
               Event.Any.Window := null;

               Free (Event);
            end if;

            Print_Debug ("Close_Child, done delete_event, prevent_delete ?"
                         & Boolean'Image (Prevent_Delete),
                         Debug_Decrease);
         end if;

         if not Prevent_Delete then
            --  Transfer the focus before unfloating, so that the parent in
            --  which the child is unfloated (which might be random from the
            --  user's point of view) doesn't influence who gets the focus.
            if MDI_Child (Child) = MDI.Focus_Child then
               Give_Focus_To_Previous_Child (Child);
            end if;

            --  The call to Float_Child below will change the focus_child
            --  to the child we are about to destroy! Save the focus_child
            --  here...
            Focus_Child := MDI.Focus_Child;

            --  ... unfloat...
            Float_Child (Child, False);

            --- ... and restore the focus child.
            MDI.Set_Focus_Child (Focus_Child);

            Print_Debug ("Close_Child: about to destroy " & Get_Title (Child));
            Destroy (Child);
         end if;
      end if;

   exception
      when E : others =>
         --  Silently ignore the exceptions for now, to avoid crashes.
         --  The application using the MDI can not do it, since this callback
         --  is called directly from the button in Initialize

         if Traces then
            Print_Debug ("Unexpected exception "
                         & Exception_Information (E));
         end if;
   end Close_Child;

   -------------------
   -- Destroy_Child --
   -------------------

   procedure Destroy_Child (Child : access Gtk_Widget_Record'Class) is
      C                   : constant MDI_Child := MDI_Child (Child);
      MDI                 : constant MDI_Window := C.MDI;
      In_Selection_Dialog : Boolean := False;

   begin
      --  We know at that stage that Child has already been unparent-ed

      pragma Assert (Get_Parent (Child) = null);

      Print_Debug ("Destroy_Child " & Get_Title (C));

      Ref (C);

      --  Do not transfer the focus elsewhere: for an interactive close, this
      --  is done in Close_Child, otherwise we do not want to change the focus.
      --  No need to send a signal to signal that a new child has been selected
      --  since Give_Focus_To_Previous_Child has been called already
      --
      --  P111-059: Focus child should be reset before any manipulation with
      --  widget tree, otherwise GPS can use it to recompute selection context.
      if C = MDI.Focus_Child then
         MDI.Focus_Child := null;
      end if;

      --  Emit the signal now, giving clients a chance to react to the
      --  imminent destruction of the child

      Emit_By_Name_Child
        (Get_Object (MDI),
         String (Signal_Before_Destroy_Child) & ASCII.NUL,
         Get_Object (C));
      Widget_Callback.Emit_By_Name (C, Signal_Before_Destroy_Child);

      C.Tab_Label := null;

      --  The child of the MDI_Child has now been taken care of, thus we need
      --  to take care of the MDI_Child itself now.

      Update_Menu_Model_List_Of_Children (MDI);

      if not C.MDI.In_Destruction then
         --  Do not unfloat the child, since the toplevel is no longer a
         --  Gtk_Window, and we would get a CE in Float_Child.

         if Get_Parent (C) /= null then
            Remove (Gtk_Container (Get_Parent (C)), C);
         end if;
      end if;

      if Get_Parent (C.Initial) /= null then
         Print_Debug ("Destroy_Child removing initial child from parent");
         Remove (Gtk_Container (Get_Parent (C.Initial)), C.Initial);
      end if;

      C.Initial := null;

      In_Selection_Dialog := MDI.Selection_Dialog /= null
        and then C = MDI_Child (Get_Data (Selection_Dialog_Access
          (MDI.Selection_Dialog).Current_Child));

      --  Only remove it from the list of children at the end, since some of
      --  calls above might result in calls to Raise_Child_Idle, which tries
      --  to manipulate that list.
      Widget_List.Remove (MDI.Items, Gtk_Widget (C));

      --  Report that the child has been removed only after it has indeed be
      --  fully removed, but before we actually free it
      Emit_By_Name_Child
        (Get_Object (MDI),
         String (Signal_Child_Removed) & ASCII.NUL, Get_Object (C));

      --  If we are currently displaying the window selection dialog, update it
      --  so that the widget that has been destroyed does not show up in the
      --  selection window.
      if In_Selection_Dialog then
         Update_Selection_Dialog (MDI, +1);
      end if;

      Unref (C.Title_Icon);
      Free (C.Icon_Name);
      Free (C.Title);
      Free (C.Short_Title);
      Free (C.XML_Node_Name);

      if C.State = Invisible then
         --  We owned an extra reference in this case
         Unref (C);
      end if;

      --  Destroy the child, unless the user has explicitely kept a Ref on it
      --  (therefore, do not use Destroy, only Unref). In all cases, it should
      --  be hidden on the screen
      Unref (C);
   end Destroy_Child;

   ---------------------------
   -- Destroy_Initial_Child --
   ---------------------------

   procedure Destroy_Initial_Child
      (Child : access Gtk_Widget_Record'Class) is
   begin
      if not Child.In_Destruction then
         Destroy (Child);
      end if;
   end Destroy_Initial_Child;

   -------------------------
   -- Set_Child_Title_Bar --
   -------------------------

   procedure Set_Child_Title_Bar (Child : access MDI_Child_Record'Class) is
      Event : constant Gtk_Event_Box :=
        Gtk_Event_Box (Get_Parent (Child.Title_Box));
   begin
      if not Has_Title_Bar (Child) then
         Hide (Event);
         Set_Child_Visible (Event, False);
         Set_Size_Request (Event, -1, 0);

      else
         Show (Event);
         Set_Child_Visible (Event, True);
         Set_Size_Request (Event, -1, Child.MDI.Title_Bar_Height);
      end if;
   end Set_Child_Title_Bar;

   ---------------------
   -- Set_Dnd_Message --
   ---------------------

   procedure Set_Dnd_Message
     (MDI     : access MDI_Window_Record;
      Message : String) is
   begin
      Free (MDI.Dnd_Message);

      if Message /= "" then
         MDI.Dnd_Message := new String'(Message);
      else
         MDI.Dnd_Message := new String'
           ("<i><b>control</b> moves whole notebook"
            & ASCII.LF
            & "<b>shift</b> creates new view</i>");
      end if;
   end Set_Dnd_Message;

   -----------------------
   -- Get_Allowed_Areas --
   -----------------------

   function Get_Allowed_Areas
     (Child : not null access MDI_Child_Record'Class) return Allowed_Areas is
   begin
      return Child.Areas;
   end Get_Allowed_Areas;

   -------------------------
   -- Get_Tab_Orientation --
   -------------------------

   function Get_Tab_Orientation
     (Child : not null access MDI_Child_Record'Class)
      return Tab_Orientation_Type
   is
      Angle : constant Gdouble := Child.Tab_Label.Get_Angle;
   begin
      if Angle = 0.0 then
         return Horizontal;
      elsif Angle = 90.0 then
         return Bottom_To_Top;
      else
         return Top_To_Bottom;
      end if;
   end Get_Tab_Orientation;

   ----------------------------------
   -- Button_Pressed_On_Title_Icon --
   ----------------------------------

   function Button_Pressed_On_Title_Icon
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean
   is
   begin
      if Event.The_Type = Gdk_2button_Press
        and then Event.Button = 1
      then
         Close_Child (MDI_Child (Child));
         return False;
      end if;

      return Button_Pressed_Forced (Child, Event);
   end Button_Pressed_On_Title_Icon;

   --------------------
   -- Button_Pressed --
   --------------------

   function Button_Pressed
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean is
   begin
      --  It sometimes happens that widgets let events pass through (for
      --  instance scrollbars do that), and thus wouldn't be useable anymore
      --  if we do a grab.
      --  ??? The comment above was kept for reference. We used to check that
      --  Event.Window was Child.Get_Window, but this prevents dragging from
      --  the title bar.

      if Event.Window /= Child.Get_Window then
         return False;
      end if;

      return Button_Pressed_Forced (Child, Event);
   end Button_Pressed;

   ---------------------------
   -- Button_Pressed_Forced --
   ---------------------------

   function Button_Pressed_Forced
     (Child : access GObject_Record'Class;
      Event : Gdk_Event_Button) return Boolean
   is
      C    : constant MDI_Child := MDI_Child (Child);
   begin
      C.MDI.In_Drag := No_Drag;

      if Event.The_Type /= Button_Press or else Event.Button /= 1 then
         return False;
      end if;

      --  Start a drag-and-drop operation. This won't be effective unless
      --  the user actually drags the mouse a while

      Print_Debug ("Button_Pressed_Forced");
      Child_Drag_Begin (C, Event, C.Areas);

      --  Let the event through, the drag hasn't started yet
      return False;
   end Button_Pressed_Forced;

   --------------------
   -- Button_Release --
   --------------------

   function Button_Release
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean
   is
      C : constant MDI_Child := MDI_Child (Child);
   begin
      if Get_Window (Child) /= Event.Window then
         C.MDI.In_Drag := No_Drag;
         Pointer_Ungrab (Time => 0);
         return False;
      end if;

      return Button_Release_Forced (Child, Event);
   end Button_Release;

   ---------------------------
   -- Button_Release_Forced --
   ---------------------------

   function Button_Release_Forced
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button;
      Allow_Move : Boolean := True) return Boolean
   is
      C                    : constant MDI_Child := MDI_Child (Child);
      MDI                  : constant MDI_Window := C.MDI;
      Move_Whole_Notebook  : constant Boolean :=
                               (Event.State and Control_Mask) /= 0;
      Copy_Instead_Of_Move : constant Boolean :=
                               (Event.State and Shift_Mask) /= 0;
      C2                   : MDI_Child;
      Current              : Gtk_Widget;
      Note                 : MDI_Notebook;
      Position             : Child_Position;
      Pane                 : Gtkada_Multi_Paned;
      Parent_Rect          : Gdk_Rectangle;
      Allowed              : Boolean;
      In_Central           : Boolean;
   begin
      Print_Debug
        ("Button release, drag=" & Drag_Status'Image (C.MDI.In_Drag));

      Pointer_Ungrab (Time => 0);

      case C.MDI.In_Drag is
         when In_Pre_Drag =>
            Gtkada.Style.Delete_Overlay (C.MDI, C.MDI.Dnd_Overlay);
            Child_Drag_Finished (C);

         when In_Drag =>
            if C.MDI.Central /= null then
               Set_Border_Width (C.MDI.Central, 0);
            end if;

            Gtkada.Style.Delete_Overlay (C.MDI, C.MDI.Dnd_Overlay);

            if Allow_Move then
               Get_Dnd_Target
                 (C.MDI, Current, Position,
                  Parent_Rect, C.MDI.Dnd_Rectangle,
                  In_Central => In_Central,
                  Allowed    => Allowed);
            else
               Allowed := False;
            end if;

            if not Allowed then
               Child_Drag_Finished (C);
               C.MDI.In_Drag := No_Drag;
               return True;
            end if;

            if Current = null then --  outside of the main window ?
               Pane := null;
            elsif Current = Gtk_Widget (C.MDI) then
               Pane := Gtkada_Multi_Paned (C.MDI);
            elsif Get_Parent (Current) = Gtk_Widget (C.MDI.Central) then
               Pane := C.MDI.Central;
            else
               Pane := Gtkada_Multi_Paned (C.MDI);
            end if;

            C2 := Dnd_Data (C, Copy => Copy_Instead_Of_Move);
            if C2 = null then
               C2 := C;
            end if;

            if Current = null then
               --  Floating child ?
               Float_Child (C2, True);

            --  If the child is dropped at the same location, nothing to do

            elsif C2.State = Normal  --  A floating child is always moved
              and then Current = Get_Parent (C2)  --  same notebook ?
              and then
                (Position = Position_Automatic  --  inside the nook
                 or else Move_Whole_Notebook  --  to one side but moving all
                 or else Get_Nth_Page (Gtk_Notebook (Current), 1) = null)
            then
               null;

            --  Do the actual moving

            else
               --  In the notebook that contains the window we are moving, we
               --  now raise the last window that had the focus

               declare
                  Item : Widget_List.Glist := MDI.Items;
                  It   : MDI_Child;
               begin
                  if C /= C2 then
                     Print_Debug ("Button_Release raising last1 "
                                  & Get_Title (C));
                     Raise_Child (C, False);
                  else
                     while Item /= Widget_List.Null_List loop
                        It := MDI_Child (Get_Data (Item));
                        if It /= C2
                          and then Get_Parent (C2) = Get_Parent (It)
                        then
                           Print_Debug
                             ("Button_Release raising last2 "
                              & Get_Title (It));
                           Raise_Child (It, False);
                           exit;
                        end if;

                        Item := Widget_List.Next (Item);
                     end loop;
                  end if;
               end;

               --  Find in which notebook the widget should be moved.

               if Current = Gtk_Widget (C.MDI.Central)
                 or else Current = Gtk_Widget (C.MDI)
               then
                  --  The central area is empty if Current has this value, we
                  --  always create a new notebook
                  Note := Create_Notebook (MDI);

                  if Current = Gtk_Widget (C.MDI) then
                     Current := null;
                  end if;

               else
                  --  We dropped in a notebook, should we reuse or create one ?
                  if Position = Position_Automatic then
                     Note := MDI_Notebook (Current);
                  else
                     Note := Create_Notebook (MDI);
                  end if;
               end if;

               --  Add to the contents of this notebook

               if Move_Whole_Notebook then
                  declare
                     Children : Widget_List.Glist :=
                       Get_Children (Get_Notebook (C2));
                     L : Widget_List.Glist := Children;
                  begin
                     while L /= Null_List loop
                        Put_In_Notebook
                          (C.MDI, MDI_Child (Get_Data (L)), Note,
                           Force_Parent_Destruction => False);
                        L := Next (L);
                     end loop;
                     Free (Children);
                  end;
               else
                  Put_In_Notebook
                    (C.MDI, C2, Note, Force_Parent_Destruction => False);
               end if;

               case Position is
                  when Position_Bottom =>
                     if Current = null then
                        Split
                          (Pane,
                           Root_Pane, Note, Orientation_Vertical,
                           Height => -1);
                     else
                        Split (Pane, Current, Note, Orientation_Vertical);
                     end if;

                  when Position_Top =>
                     if Current = null then
                        Split
                          (Pane,
                           Root_Pane, Note, Orientation_Vertical,
                           Height => -1, After  => False);
                     else
                        Split
                          (Pane,
                           Current, Note, Orientation_Vertical,
                           After  => False);
                     end if;
                  when Position_Left =>
                     if Current = null then
                        Split
                          (Pane,
                           Root_Pane, Note, Orientation_Horizontal,
                           Width => -1, After  => False);
                     else
                        Split
                          (Pane,
                           Current, Note, Orientation_Horizontal,
                           After  => False);
                     end if;

                  when Position_Right =>
                     if Current = null then
                        Split
                          (Pane,
                           Root_Pane, Note, Orientation_Horizontal,
                           Width => -1);
                     else
                        Split (Pane, Current, Note, Orientation_Horizontal);
                     end if;

                  when Position_Automatic | Position_Float =>
                     if C.MDI.Central /= null
                        and then Current = Gtk_Widget (C.MDI.Central)
                     then
                        Add_Child
                          (Win         => C.MDI.Central,
                           New_Child   => Note,
                           Orientation => Orientation_Horizontal,
                           Width       => 0,
                           Height      => 0);
                     end if;
               end case;

               Emit_By_Name
                 (Get_Object (MDI),
                  String (Signal_Children_Reorganized)
                  & ASCII.NUL);
            end if;

            Child_Drag_Finished (C);

            Print_Debug ("Button_Release raising " & Get_Title (C2));
            Raise_Child (C2, False);
            Print_Debug ("Button_Release, set_focus " & Get_Title (C2));
            Set_Focus_Child (C2);

            --  For a focus manually, because Set_Focus_Child above might not
            --  have had any effect (the selected window is still the same,
            --  although in some cases gtk+ has transfered the keyboard focus
            --  elsewhere).
            Give_Focus_To_Child (C2);

            MDI.Dnd_Target := null;

         when No_Drag =>
            --  Let the even through, we have nothing to do here
            return False;
      end case;

      C.MDI.In_Drag := No_Drag;
      return True;
   end Button_Release_Forced;

   -----------------------------
   -- Button_Release_Notebook --
   -----------------------------

   function Button_Release_Notebook
     (Note : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Button) return Boolean
   is
      N : constant MDI_Notebook := MDI_Notebook (Note);
      Child : constant MDI_Child :=
        MDI_Child (Get_Nth_Page (N, Get_Current_Page (N)));
      Tmp : Boolean;
      pragma Unreferenced (Tmp);
   begin
      if Child.MDI.In_Drag = In_Drag
        or else Child.MDI.In_Drag = In_Pre_Drag
      then
         Tmp := Button_Release_Forced
           (Child, Event,
            Allow_Move => not Hover_Tabs (N, Event.X_Root, Event.Y_Root));

         --  Fallback to standard gtk+ handler, so that it terminates its
         --  reorder operation if any
         --  Unfortunately, the notebook might no longer exist at this point,
         --  so it is dangerous to forward.

         if Child.Get_State = Floating then
            return True;  --  stop propagating
         end if;
      end if;

      return False;
   end Button_Release_Notebook;

   -------------------
   -- Button_Motion --
   -------------------

   function Button_Motion
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean
   is
   begin
      if Get_Window (Child) /= Event.Window then
         return False;
      end if;

      return Button_Motion_Forced (Child, Event);
   end Button_Motion;

   --------------------------
   -- Button_Motion_Forced --
   --------------------------

   function Button_Motion_Forced
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean
   is
      C        : constant MDI_Child := MDI_Child (Child);
      Note     : MDI_Notebook;
      Tmp      : Gdk_Grab_Status;
      Delta_X, Delta_Y : Gint;
      pragma Unreferenced (Tmp);

   begin
      --  This is no longer called once the GtkNotebook has started reordering
      --  the tab, because it creates a transparent overlay window that grabs
      --  all the events (so the motion is sent to the GtkNotebook, no longer
      --  to the child).

      case C.MDI.In_Drag is
         when In_Drag =>
            Draw_Dnd_Rectangle (C);
            return True;

         when In_Pre_Drag =>
            --  If we are still in the tabs area, do nothing so that tabs can
            --  be reordered graphically

            Delta_X := abs (Gint (Event.X_Root) - C.MDI.Drag_Start_X);
            Delta_Y := abs (Gint (Event.Y_Root) - C.MDI.Drag_Start_Y);

            Note := Get_Notebook (C);
            if Note /= null
              and then Get_Show_Tabs (Note)
            then
               case Get_Tab_Pos (Note) is
                  when Pos_Top | Pos_Bottom =>
                     if Delta_Y < Drag_Threshold / 2
                        and then Delta_Y < Delta_X
                     then
                        return False;
                     end if;

                  when Pos_Left | Pos_Right =>
                     if Delta_X < Drag_Threshold / 2
                        and then Delta_X < Delta_Y
                     then
                        return False;
                     end if;
               end case;
            end if;

            --  Else start a drag operation if appropriate

            if Delta_X > Drag_Threshold
              or else Delta_Y > Drag_Threshold
            then
               --  If we had a tab reorder operation, but the tab was left at
               --  the same position, the signal "page_reordered" has not been
               --  emitted. Still, the pointer has been ungrabbed, so we do
               --  the following test below, so that we do not start our own
               --  dnd operation

               if not Pointer_Is_Grabbed then
                  return False;
               end if;

               C.MDI.In_Drag := In_Drag;

               Pointer_Ungrab (Time => 0);

               if C.MDI.Cursor_Fleur = null then
                  Gdk_New (C.MDI.Cursor_Fleur, Fleur);
               end if;

               Tmp := Pointer_Grab
                 (Get_Window (C),
                  False, Button_Motion_Mask or Button_Release_Mask,
                  Cursor => C.MDI.Cursor_Fleur,
                  Time   => 0);

               return True;
            end if;

         when others =>
            null;
      end case;
      return True;
   end Button_Motion_Forced;

   ----------------
   -- Hover_Tabs --
   ----------------

   function Hover_Tabs
     (N : MDI_Notebook;
      X_Root, Y_Root : Gdouble) return Boolean
   is
      In_Tabs_Area          : Boolean;
      X_Win, Y_Win          : Gint;
      W, H                  : Gint;
      Mouse_X, Mouse_Y      : Gint;
      Alloc                 : Gtk_Allocation;
      Tab_Label             : Gtk_Widget;
      Tab_Width, Tab_Height : Gint;
   begin
      Get_Origin (N.Get_Window, X_Win, Y_Win);
      N.Get_Allocation (Alloc);
      Mouse_X := Gint (X_Root) - X_Win - Alloc.X;
      Mouse_Y := Gint (Y_Root) - Y_Win - Alloc.Y;

      W := N.Get_Allocated_Width;
      H := N.Get_Allocated_Height;
      Tab_Label := N.Get_Tab_Label (N.Get_Nth_Page (0));

      case N.Actual_Tab_Orientation is
         when Horizontal | Automatic =>   --  'Automatic' is never set
            Tab_Width  := Tab_Label.Get_Allocated_Width;
            Tab_Height := 30;   --   ??? hard-coded
         when Bottom_To_Top | Top_To_Bottom =>
            Tab_Width := 30;    --    ??? hard-coded
            Tab_Height := Tab_Label.Get_Allocated_Height;
      end case;

      case N.Get_Tab_Pos is
         when Pos_Top =>
            In_Tabs_Area :=
              (Mouse_X >= 0 and then Mouse_X <= W
               and then Mouse_Y >= 0 and then Mouse_Y <= Tab_Height);
         when Pos_Bottom =>
            In_Tabs_Area :=
              (Mouse_X >= 0 and then Mouse_X <= W
               and then Mouse_Y >= H - Tab_Height
               and then Mouse_Y <= H);
         when Pos_Left =>
            In_Tabs_Area :=
              (Mouse_X >= 0 and then Mouse_X <= Tab_Width
               and then Mouse_Y >= 0 and then Mouse_Y <= H);
         when Pos_Right =>
            In_Tabs_Area :=
              (Mouse_X >= W - Tab_Width and then Mouse_X <= W
               and then Mouse_Y >= 0 and then Mouse_Y <= H);
      end case;

      return In_Tabs_Area;
   end Hover_Tabs;

   ----------------------------
   -- Button_Motion_Notebook --
   ----------------------------

   function Button_Motion_Notebook
     (Note : access Gtk_Widget_Record'Class;
      Event : Gdk_Event_Motion) return Boolean
   is
      N : constant MDI_Notebook := MDI_Notebook (Note);
      Child : constant MDI_Child :=
        MDI_Child (Get_Nth_Page (N, Get_Current_Page (N)));
      In_Tabs_Area          : Boolean;
   begin
      if Child /= null
        and then (Child.MDI.In_Drag = In_Drag
                  or else Child.MDI.In_Drag = In_Pre_Drag)
      then
         In_Tabs_Area := Hover_Tabs (N, Event.X_Root, Event.Y_Root);

         if not In_Tabs_Area then
            Child.MDI.In_Drag := In_Drag;
            return Button_Motion_Forced (Child, Event);
         end if;

         if Child.MDI.In_Drag = In_Drag then
            --  Remove the previous highlighting, since we are back to
            --  reordering tabs
            Draw_Dnd_Rectangle (Child, Hide_Only => True);
         end if;
      end if;

      return False;
   end Button_Motion_Notebook;

   --------------
   -- Dnd_Data --
   --------------

   function Dnd_Data
     (Child : not null access MDI_Child_Record; Copy : Boolean)
      return MDI_Child
   is
      pragma Unreferenced (Copy);
   begin
      return MDI_Child (Child);
   end Dnd_Data;

   -------------------------
   -- Child_Widget_Hidden --
   -------------------------

   procedure Child_Widget_Hidden
     (Widget : access Gtk_Widget_Record'Class)
   is
      Child : constant MDI_Child := MDI_Child (Widget);
      Note  : MDI_Notebook;
   begin
      if Child.State = Floating then
         Hide (Get_Toplevel (Get_Widget (Child)));
      else
         Hide (Child);

         --  At startup, the notebook might be null
         Note := Get_Notebook (Child);
         if Note /= null then
            Configure_Notebook_Tabs (Child.MDI, Note, Hide_If_Empty => True);
         end if;
      end if;
   end Child_Widget_Hidden;

   ------------------------
   -- Child_Widget_Shown --
   ------------------------

   procedure Child_Widget_Shown
     (Widget : access Gtk_Widget_Record'Class)
   is
      Child : constant MDI_Child := MDI_Child (Widget);
      Note  : MDI_Notebook;
   begin
      if Child.State = Floating then
         Show (Get_Toplevel (Get_Widget (Child)));
      else
         Show (Child);

         --  At startup, the notebook might be null
         Note := Get_Notebook (Child);
         if Note /= null then
            Configure_Notebook_Tabs (Child.MDI, Note, Hide_If_Empty => True);
         end if;
      end if;
   end Child_Widget_Shown;

   --------------------
   -- Child_Get_Type --
   --------------------

   function Child_Get_Type return Glib.GType is
      Signal_Parameters : constant Glib.Object.Signal_Parameter_Types
        (Integer (Child_Signals'First) .. Integer (Child_Signals'Last),
         1 .. 1) := (others => (1 => GType_None));
   begin
      Glib.Object.Initialize_Class_Record
        (Ancestor     => Gtk.Event_Box.Get_Type,
         Signals      => Child_Signals,
         Class_Record => Child_Class_Record,
         Type_Name    => "GtkAdaMDIChild",
         Parameters   => Signal_Parameters);
      return Child_Class_Record.The_Type;
   end Child_Get_Type;

   -------------
   -- Gtk_New --
   -------------

   procedure Gtk_New
     (Child        : out MDI_Child;
      Widget       : access Gtk.Widget.Gtk_Widget_Record'Class;
      Flags        : Child_Flags := All_Buttons;
      Group        : Child_Group := Group_Default;
      Focus_Widget : Gtk.Widget.Gtk_Widget := null;
      Areas        : Allowed_Areas := Both) is
   begin
      Child := new MDI_Child_Record;
      Initialize (Child, Widget, Flags, Group, Focus_Widget, Areas);
   end Gtk_New;

   ----------------
   -- Initialize --
   ----------------

   procedure Initialize
     (Child        : access MDI_Child_Record'Class;
      Widget       : access Gtk.Widget.Gtk_Widget_Record'Class;
      Flags        : Child_Flags := All_Buttons;
      Group        : Child_Group := Group_Default;
      Focus_Widget : Gtk.Widget.Gtk_Widget := null;
      Areas        : Allowed_Areas := Both)
   is
      Event             : Gtk_Event_Box;
      Button            : Close_Button.Gtkada_MDI_Close_Button;

   begin
      if Widget.all in Gtk_Window_Record'Class then
         raise Program_Error;
      end if;

      G_New (Child, Child_Get_Type);

      Child.Set_Border_Width (0);
      Get_Style_Context (Child).Add_Class ("mdichild");
      Child.Initial      := Gtk_Widget (Widget);
      Child.State        := Normal;
      Child.Flags        := Flags;
      Child.Group        := Group;
      Child.Focus_Widget := Focus_Widget;
      Child.MDI          := null;
      Child.Areas        := Areas;

      Add_Events
        (Child, Button_Press_Mask
           or Button_Motion_Mask
           or Button_Release_Mask
           or Pointer_Motion_Mask);

      Child.On_Button_Press_Event (Button_Pressed'Access);
      Child.On_Button_Release_Event (Button_Release'Access);
      Child.On_Motion_Notify_Event (Button_Motion'Access);
      Widget_Callback.Connect
        (Child, Signal_Destroy,
         Widget_Callback.To_Marshaller (Destroy_Child'Access));

      Gtk_New_Vbox (Child.Main_Box, Homogeneous => False, Spacing => 0);
      Add (Child, Child.Main_Box);

      --  Buttons in the title bar

      Gtk_New (Event);
      Get_Style_Context (Event).Add_Class ("mdititle");
      Pack_Start
        (Child.Main_Box, Event, Expand => False, Fill => False);
      Gtk_New_Hbox (Child.Title_Box, Homogeneous => False);
      Add (Event, Child.Title_Box);
      Event.On_Button_Press_Event (Button_Pressed_Forced'Access, Child);

      Gtk_New (Event);
      Gtk_New (Child.Title_Icon);
      Ref (Child.Title_Icon);  --  floating a child should not destroy icon
      Event.Add (Child.Title_Icon);
      Child.Title_Box.Pack_Start (Event, Expand => False);
      Event.On_Button_Press_Event
        (Button_Pressed_On_Title_Icon'Access, Child);

      Gtk_New (Child.Title_Label);
      Child.Title_Box.Pack_Start
        (Child.Title_Label, Expand => True, Fill => True);
      Child.Title_Label.Set_Ellipsize (Pango.Layout.Ellipsize_Start);
      Child.Title_Label.Set_Alignment (0.0, 0.5);
      Child.Title_Label.Set_Padding (5, 0);

      --  Do not set the label as selectable, otherwise it has its own window,
      --  and we can no longer drag from the title
      Child.Title_Label.Set_Selectable (False);

      if (Flags and Destroy_Button) /= 0 then
         Close_Button.Gtk_New
           (Button, Child, Child,
            Position    => Pos_Top,
            In_Titlebar => True);
         Pack_End
           (Child.Title_Box,
            Button, Expand => False, Fill => False, Padding => 2);
      end if;

      --  This internal Event box is needed when the child is floated
      Gtk_New (Event);
      Add (Event, Widget);
      Pack_Start
        (Child.Main_Box, Event, Expand => True, Fill => True, Padding => 0);

      Child.Set_Title (" ");

      Widget_Callback.Object_Connect
        (Child.Initial, Signal_Destroy,
         Widget_Callback.To_Marshaller (Destroy_Initial_Child'Access),
         Child);
      Widget_Callback.Connect
        (Child, Signal_Hide, Child_Widget_Hidden'Access);
      Widget_Callback.Connect
        (Child, Signal_Show, Child_Widget_Shown'Access);
      Widget_Callback.Object_Connect
        (Child.Initial, Signal_Hide, Child_Widget_Hidden'Access, Child);
      Widget_Callback.Object_Connect
        (Child.Initial, Signal_Show, Child_Widget_Shown'Access, Child);
      Widget_Callback.Connect
        (Child, Signal_Maximize_Child,
         Maximize_Action.On_Toggle_Maximize'Access);
      Widget_Callback.Connect
        (Child, Signal_Unmaximize,
         Maximize_Action.On_Toggle_Maximize'Access);
      Widget_Callback.Connect
        (Child, Signal_Before_Remove_Child,
         Maximize_Action.On_Remove_Child'Access);
   end Initialize;

   -------------------------
   -- Give_Focus_To_Child --
   -------------------------

   procedure Give_Focus_To_Child (Child : MDI_Child) is
      F : Gtk_Widget := Gtk_Widget (Child);
   begin
      if Child /= null then
         if Child.Focus_Widget /= null then
            F := Child.Focus_Widget;
         end if;

         --  If we can't give the focus to the focus widget, give it to
         --  child itself. This is better than keeping it on the previous
         --  child.

         Grab_Focus (F);
      end if;
   end Give_Focus_To_Child;

   ----------------------------------
   -- Give_Focus_To_Previous_Child --
   ----------------------------------

   procedure Give_Focus_To_Previous_Child
     (Child          : access MDI_Child_Record'Class;
      From_Same_Area : Boolean := True)
   is
      Item : Widget_List.Glist;
      It   : MDI_Child;
      Last : MDI_Child;
   begin
      --  Set the focus on the child that had the focus just before,
      --  and in the same notebook, and still visible

      Item := Child.MDI.Items;
      while Item /= Widget_List.Null_List loop
         It := MDI_Child (Get_Data (Item));

         if It.Get_Visible and then It /= MDI_Child (Child) then
            if Last = null then
               Last := It;
            end if;

            if not From_Same_Area
              or else (It.State = Child.State
                       and then  Get_Parent (It) = Get_Parent (Child))
            then
               Print_Debug ("Give_Focus_To_Previous_Child "
                            & Get_Title (It));
               Set_Focus_Child (It);
               return;
            end if;
         end if;

         Item := Widget_List.Next (Item);
      end loop;

      --  No such child, give it to the last child that had the focus
      if Last = null then
         Print_Debug ("Give_Focus_To_Previous_Child: no one");
         Child.MDI.Focus_Child := null;
         Child_Selected (Child.MDI, null);
      else
         Set_Focus_Child (Last);
      end if;
   end Give_Focus_To_Previous_Child;

   --------------------
   -- Child_Selected --
   --------------------

   procedure Child_Selected
      (Self  : not null access MDI_Window_Record'Class;
       Child : access MDI_Child_Record'Class := null) is
   begin
      Emit_By_Name_Child
        (Get_Object (Self),
         String (Signal_Child_Selected) & ASCII.NUL,
         Get_Object_Or_Null (GObject (Child)));
   end Child_Selected;

   ------------------
   -- Save_Desktop --
   ------------------

   function Save_Desktop
     (Self : not null access MDI_Child_Record) return Glib.Xml_Int.Node_Ptr
   is
      pragma Unreferenced (Self);
   begin
      return null;
   end Save_Desktop;

   ---------
   -- Put --
   ---------

   procedure Put
     (MDI              : access MDI_Window_Record;
      Child            : access MDI_Child_Record'Class;
      Initial_Position : Child_Position := Position_Automatic;
      Position_At_Mouse : Boolean := True;
      X, Y              : Gint := 0)
   is
      Float : constant Boolean :=
         MDI.All_Floating_Mode
         or else (not MDI.Loading_Desktop
                  and then Initial_Position = Position_Float);
   begin
      Child.MDI := MDI_Window (MDI);

      --  We need to show the widget before inserting it in a notebook,
      --  otherwise the notebook page will not be made visible.

      Ref (Child);

      Show_All (Child);

      if Child.State = Invisible then
         Unref (Child);  --  Set in Remove_All_Items
         return;
      end if;

      Set_State (Child, Normal);

      Float_Child (Child, Float, Position_At_Mouse, X, Y);

      if not Float then
         Put_In_Notebook (MDI, Child, Initial_Position => Initial_Position);
      end if;

      Set_Child_Title_Bar (Child);

      --  Add the child to the list of widgets. It could in fact already be in
      --  the list if we are reusing a Invisible child from a previous
      --  perspective. We however want to move it to the front of the list

      Remove (MDI.Items, Gtk_Widget (Child));
      Prepend (MDI.Items, Gtk_Widget (Child));
      Unref (Child);

      Update_Menu_Model_List_Of_Children (MDI);

      --  Restore the keyboard focus, which might have been stolen if the new
      --  child was added to a notebook.

      Give_Focus_To_Child (MDI.Focus_Child);

      Emit_By_Name_Child
        (Get_Object (MDI),
         String (Signal_Child_Added) & ASCII.NUL, Get_Object (Child));
   end Put;

   --------------
   -- Set_Size --
   --------------

   procedure Set_Size
     (MDI        : access MDI_Window_Record;
      Child      : access MDI_Child_Record'Class;
      Width      : Glib.Gint;
      Height     : Glib.Gint;
      Fixed_Size : Boolean := False)
   is
      Notebook : constant MDI_Notebook := Get_Notebook (Child);
   begin
      --  Ignore specific size requests while loading the desktop, since the
      --  latter should force the size
      if not MDI.Loading_Desktop and then Notebook /= null then
         --  Only take this into account if we have a single page
         if Get_Nth_Page (Notebook, 1) = null
           and then MDI.Show_Tabs_Policy /= Always
         then
            Set_Size (MDI,
                      Widget     => Notebook,
                      Width      => Width, Height => Height,
                      Fixed_Size => Fixed_Size);
         else
            Set_Size (MDI,
                      Widget     => Notebook,
                      Width      => Width,
                      Height     => Height
                      + Get_Allocated_Height (Notebook)
                      - Get_Allocated_Height (Child),
                      Fixed_Size => Fixed_Size);
         end if;
      end if;
   end Set_Size;

   ---------------
   -- Get_Title --
   ---------------

   function Get_Title
     (Child : not null access MDI_Child_Record) return UTF8_String is
   begin
      if Child.Title = null then
         return "";
      else
         return Child.Title.all;
      end if;
   end Get_Title;

   ---------------------
   -- Get_Short_Title --
   ---------------------

   function Get_Short_Title
     (Child : not null access MDI_Child_Record) return UTF8_String is
   begin
      return Child.Short_Title.all;
   end Get_Short_Title;

   --------------
   -- Set_Icon --
   --------------

   procedure Set_Icon
     (Child : not null access MDI_Child_Record;
      Icon  : Gdk.Pixbuf.Gdk_Pixbuf) is
   begin
      Free (Child.Icon_Name);
      Child.Title_Icon.Set (Icon);
      if Icon /= null then
         Child.Title_Icon.Show;
      else
         Child.Title_Icon.Hide;
      end if;

      if Child.Tab_Icon /= null then
         Child.Tab_Icon.Set (Icon);
         if Icon /= null then
            Child.Tab_Icon.Show;
         else
            Child.Tab_Icon.Hide;
         end if;
      end if;

      Update_Menu_Model_List_Of_Children (Child.MDI);
      Emit_By_Name_Child
        (Get_Object (Child.MDI),
         String (Signal_Child_Icon_Changed) & ASCII.NUL,
         Get_Object (Child));
   end Set_Icon;

   -------------------
   -- Set_Icon_Name --
   -------------------

   procedure Set_Icon_Name
     (Child     : not null access MDI_Child_Record;
      Icon_Name : String)
   is
      Changed : constant Boolean :=
        (Child.Icon_Name = null or else
         Child.Icon_Name.all /= Icon_Name);
   begin
      Free (Child.Icon_Name);
      Child.Icon_Name := new String'(Icon_Name);

      if Icon_Name /= "" then
         Child.Title_Icon.Show;
         Child.Title_Icon.Set_From_Icon_Name (Icon_Name, Icon_Size_In_Title);
      else
         Child.Title_Icon.Hide;
      end if;

      if Child.Tab_Icon /= null then
         if Icon_Name /= "" then
            Child.Tab_Icon.Show;
            Child.Tab_Icon.Set_From_Icon_Name (Icon_Name, Icon_Size_In_Tabs);
         else
            Child.Tab_Icon.Hide;
         end if;
      end if;

      Update_Menu_Model_List_Of_Children (Child.MDI);

      if Changed then
         Emit_By_Name_Child
           (Get_Object (Child.MDI),
            String (Signal_Child_Icon_Changed) & ASCII.NUL,
            Get_Object (Child));
      end if;
   end Set_Icon_Name;

   --------------
   -- Get_Icon --
   --------------

   function Get_Icon
     (Child : not null access MDI_Child_Record) return Gdk.Pixbuf.Gdk_Pixbuf
   is
   begin
      return Child.Title_Icon.Get;
   end Get_Icon;

   --------------
   -- Get_Icon --
   --------------

   function Get_Icon
     (Child : not null access MDI_Child_Record'Class)
      return Gtk_Image
   is
      Result : Gtk_Image;

      Pixbuf : Gdk_Pixbuf;
      G_Icon : Glib.G_Icon.G_Icon;
      Size   : Gtk.Enums.Gtk_Icon_Size;

   begin
      Pixbuf := Child.Title_Icon.Get;   --  still owned by the image
      if Pixbuf /= null then
         Gtk_New (Result, Pixbuf);

      else
         Child.Title_Icon.Get (G_Icon, Size);
         if G_Icon /= Null_G_Icon then
            Result := Gtk_Image_New_From_Gicon (G_Icon, Size);
         end if;
      end if;

      return Result;
   end Get_Icon;

   -------------------
   -- Get_Icon_Name --
   -------------------

   function Get_Icon_Name
     (Child : not null access MDI_Child_Record) return String
   is
   begin
      if Child.Icon_Name = null then
         return "";
      else
         return Child.Icon_Name.all;
      end if;
   end Get_Icon_Name;

   ---------------
   -- Set_Title --
   ---------------

   procedure Set_Title
     (Child       : not null access MDI_Child_Record;
      Title       : String;
      Short_Title : String := "")
   is
      function To_UTF8 (Str : String) return String;
      --  Ensure the output string is valid UTF8

      function To_UTF8 (Str : String) return String is
         Valid : Boolean;
         Invalid_Pos : Natural;
      begin
         UTF8_Validate (Str, Valid, Invalid_Pos);

         if Valid then
            return Str;
         else
            declare
               S : constant String := Glib.Convert.Filename_To_UTF8 (Str);
            begin
               if S /= "" then
                  return S;
               end if;
            end;

            declare
               S : constant String := Glib.Convert.Locale_To_UTF8 (Str);
            begin
               if S /= "" then
                  return S;
               end if;
            end;

            return Str (Str'First .. Str'First + Invalid_Pos);
         end if;
      end To_UTF8;

      T : constant String := To_UTF8 (Title);
      S : constant String := To_UTF8 (Short_Title);

      Title_Changed       : constant Boolean := Child.Title = null
                              or else Child.Title.all /= T;
      Short_Title_Changed : constant Boolean := Child.Short_Title = null
                              or else Child.Short_Title.all /= S;

   begin
      if Title_Changed then
         Free (Child.Title);
         Child.Title := new UTF8_String'(T);
      end if;

      if Short_Title_Changed then
         Free (Child.Short_Title);
         if S /= "" then
            Child.Short_Title := new UTF8_String'(S);
         else
            Child.Short_Title := new UTF8_String'(T);
         end if;
      end if;

      if Child.MDI /= null
        and then Child.MDI.Use_Short_Titles_For_Floats
      then
         Child.Title_Label.Set_Text (Child.Short_Title.all);
      else
         Child.Title_Label.Set_Text (Child.Title.all);
      end if;

      if Title_Changed and then Child.State = Floating then
         if Child.MDI.Use_Short_Titles_For_Floats then
            Set_Title
              (Gtk_Window (Get_Toplevel (Child.Initial)),
               Locale_From_UTF8 (Child.Short_Title.all));
         else
            Set_Title
              (Gtk_Window (Get_Toplevel (Child.Initial)),
               Locale_From_UTF8 (Child.Title.all));
         end if;
      end if;

      if Short_Title_Changed then
         --  Update the menu, if it exists. We need to recreate the menu item
         --  to keep it sorted

         if Child.MDI /= null then
            Update_Menu_Model_List_Of_Children (Child.MDI);
         end if;
      end if;

      if Title_Changed or else Short_Title_Changed then
         Update_Tab_Label (Child);   --  this also updated the tooltip
         Child.Queue_Draw;
         if Child.MDI /= null then
            Emit_By_Name_Child
              (Get_Object (Child.MDI),
               String (Signal_Child_Title_Changed) & ASCII.NUL,
               Get_Object (Child));
         end if;
      end if;
   end Set_Title;

   ----------------------------
   -- Insert_Child_If_Needed --
   ----------------------------

   function Insert_Child_If_Needed
     (MDI   : access MDI_Window_Record'Class;
      Child : MDI_Child) return MDI_Child is
   begin
      if Child /= null and then Child.State = Invisible then
         Set_State (Child, Normal);
         Put (MDI, Child);

         --  When a child is made invisible, it is Ref'ed - we need to Unref to
         --  remove this reference, and we need to do this after the Put so
         --  that the child doesn't get destroyed.
         Unref (Child);
      end if;

      return Child;
   end Insert_Child_If_Needed;

   --------------------
   -- Find_MDI_Child --
   --------------------

   function Find_MDI_Child
     (MDI    : access MDI_Window_Record;
      Widget : access Gtk.Widget.Gtk_Widget_Record'Class) return MDI_Child
   is
      Tmp : Widget_List.Glist;
      Child_Widget : constant Gtk_Widget :=
        (if Widget /= null then
           Gtk_Widget_Record (Widget.all)'Unchecked_Access
         else
           null);
   begin
      Tmp := First (MDI.Items);

      while Tmp /= Null_List loop
         if MDI_Child (Get_Data (Tmp)).Initial = Child_Widget then
            return Insert_Child_If_Needed (MDI, MDI_Child (Get_Data (Tmp)));
         end if;

         Tmp := Next (Tmp);
      end loop;

      return null;
   end Find_MDI_Child;

   --------------------------------
   -- Find_MDI_Child_From_Widget --
   --------------------------------

   function Find_MDI_Child_From_Widget
     (Widget : access Gtk.Widget.Gtk_Widget_Record'Class) return MDI_Child
   is
      W   : Gtk_Widget := Gtk_Widget (Widget);
      Win : Gtk_Widget;
      C   : MDI_Child;
   begin
      --  As a special case, if the widget's parent is a notebook, we check
      --  whether the associated page is a MDI child, and behave as if that
      --  child had the focus (EC19-008)

      while W /= null loop
         if W.all in MDI_Child_Record'Class then
            return Insert_Child_If_Needed (MDI_Child (W).MDI, MDI_Child (W));

         elsif W.all in Gtk_Notebook_Record'Class
           and then Get_Nth_Page
             (Gtk_Notebook (W), Get_Current_Page (Gtk_Notebook (W))).all
              in MDI_Child_Record'Class
         then
            C := MDI_Child
              (Get_Nth_Page
                 (Gtk_Notebook (W), Get_Current_Page (Gtk_Notebook (W))));
            return Insert_Child_If_Needed (C.MDI, C);
         end if;

         W := Get_Parent (W);
      end loop;

      --  Not found ? We might have a floating window. Unfortunately, these
      --  windows do not keep track of the MDI child they belong to...

      Win := Get_Toplevel (Widget);
      if Win /= null
         and then Child_User_Data.Is_Set (Win, "parent_mdi_child")
      then
         --  The call to Get will never raise a Data_Error exception
         C := Child_User_Data.Get (Win, "parent_mdi_child");
         return Insert_Child_If_Needed (C.MDI, C);
      end if;

      return null;
   end Find_MDI_Child_From_Widget;

   ---------------------------
   -- Find_MDI_Child_By_Tag --
   ---------------------------

   function Find_MDI_Child_By_Tag
     (MDI : access MDI_Window_Record;
      Tag : Ada.Tags.Tag;
      Visible_Only : Boolean := False) return MDI_Child
   is
      Child : MDI_Child;
      Iter  : Child_Iterator :=
        First_Child (MDI, Visible_Only => Visible_Only);
   begin
      loop
         Child := Get (Iter);
         exit when Child = null or else Child.Initial'Tag = Tag;
         Next (Iter);
      end loop;

      if Child /= null then
         return Insert_Child_If_Needed (MDI, Child);
      else
         return null;
      end if;
   end Find_MDI_Child_By_Tag;

   ----------------------------
   -- Find_MDI_Child_By_Name --
   ----------------------------

   function Find_MDI_Child_By_Name
     (MDI  : access MDI_Window_Record;
      Name : String) return MDI_Child
   is
      Child : MDI_Child;
      Iter  : Child_Iterator := First_Child (MDI, Visible_Only => False);
   begin
      loop
         Child := Get (Iter);
         exit when Child = null
           or else Child.Title.all = Name
           or else Child.Short_Title.all = Name;
         Next (Iter);
      end loop;

      return Insert_Child_If_Needed (MDI, Get (Iter));
   end Find_MDI_Child_By_Name;

   -----------------
   -- Lower_Child --
   -----------------

   procedure Lower_Child (Child : not null access MDI_Child_Record'Class) is
      Note : MDI_Notebook;
   begin
      Ref (Child);
      Remove (Child.MDI.Items, Gtk_Widget (Child));
      Append (Child.MDI.Items, Gtk_Widget (Child));
      Unref (Child);

      if Child.State = Normal then
         Note := Get_Notebook (Child);
         Set_Current_Page (Note, Page_Num (Note, Child));

      elsif Child.Get_Realized then
         Gdk.Window.Lower (Get_Window (Child));

         if Child.State = Floating then
            Gdk.Window.Lower
              (Get_Window (Gtk_Window (Get_Toplevel (Child.Initial))));
         end if;
      end if;
   end Lower_Child;

   ---------------
   -- Is_Raised --
   ---------------

   function Is_Raised
     (Child : not null access MDI_Child_Record'Class) return Boolean
   is
      Note : MDI_Notebook;
   begin
      case Child.State is
         when Floating =>
            return True;
         when Invisible =>
            return False;
         when Normal =>
            Note := Get_Notebook (Child);
            return Get_Nth_Page (Note, Get_Current_Page (Note)) =
              Gtk_Widget (Child);
      end case;
   end Is_Raised;

   -----------------
   -- Raise_Child --
   -----------------

   procedure Raise_Child
     (Child      : not null access MDI_Child_Record'Class;
      Give_Focus : Boolean := True)
   is
      Old_Focus     : constant MDI_Child := Child.MDI.Focus_Child;
      Note          : MDI_Notebook;
      Give          : Boolean := Give_Focus;
   begin
      Show (Child);  --  Make sure the child is visible

      if Child.State = Invisible then
         --  child was in another desktop, not visible in the current one

         Put_In_Notebook (Child.MDI, Child);
      end if;

      --  For a docked item, we in fact want to raise its parent dock,
      --  and make sure the current page in that dock is the correct one.

      if Child.State = Normal then
         Note := Get_Notebook (Child);

         --  We'll have to transfer the focus if the current focus window is in
         --  the same dock, since otherwise that means an invisible window
         --  would have the focus.

         if Old_Focus /= null
           and then Old_Focus.State = Normal
           and then Get_Notebook (Old_Focus) = Note
         then
            Give := True;
         end if;

         --  Temporary fool the system, so that the child doesn't necessarily
         --  gain the focus. Otherwise, switching a notebook page gives the
         --  child the focus.
         Child.MDI.Focus_Child :=
           MDI_Child_Record (Child.all)'Unchecked_Access;

         --  There could be no parent if we are in all-floating mode
         if Note /= null then
            Set_Current_Page (Note, Page_Num (Note, Child));
         end if;

         Child.MDI.Focus_Child := Old_Focus;

         if Give_Focus and then Child.Initial.Get_Realized then
            Present (Gtk_Window (Get_Toplevel (Child.Initial)));
         end if;

      elsif Child.State = Floating
        and then Give_Focus
        and then Child.Initial.Get_Realized
      then
         --  When raising a floating child that has the Float_As_Transient
         --  flag set to True, set it as transient for the current window
         --  (i.e: the currently focused child's window if any or the MDI's
         --  main window).
         --  This ensures that the floating child will always be on top of
         --  the currently focused window, which is what we want in the
         --  Float_As_Transient mode.

         if Old_Focus /= Child
           and then (Child.Flags and Float_As_Transient) /= 0
         then
            declare
               Child_Window : constant Gtk_Window :=
                                Gtk_Window
                                  (Child.Initial.Get_Toplevel);
               New_Parent   : Gtk_Window := null;
            begin
               if Old_Focus = null then
                  New_Parent := Gtk_Window (Child.MDI.Get_Toplevel);
               else
                  New_Parent := Gtk_Window (Old_Focus.Initial.Get_Toplevel);
               end if;

               if New_Parent /= null then
                  Child_Window.Set_Transient_For (New_Parent);
               end if;
            end;
         end if;

         Present (Gtk_Window (Get_Toplevel (Child.Initial)));

      elsif Child.Get_Realized then
         Gdk.Window.Gdk_Raise (Get_Window (Child));

      else
         --  We still need to raise the window, but don't give it the focus
         --  on systems where it is possible.
         Gdk.Window.Gdk_Raise
            (Get_Window (Gtk_Window (Get_Toplevel (Child.Initial))));
      end if;

      --  Give the focus to the Focus_Child, since the notebook page switch
      --  might have changed that.

      if not Child.MDI.Loading_Desktop then
         if not Give then
            --  This must be done even if Old_Focus = MDI.Focus_Child.
            --  Otherwise, clicking inside an editor in GPS for instance will
            --  not properly refresh the outline view
            Give_Focus_To_Child (Old_Focus);
         else
            Print_Debug ("Raise_Child, give focus to "
                         & Get_Title (Child));
            Set_Focus_Child (Child);
         end if;
      end if;
   end Raise_Child;

   -----------------------
   -- Update_Float_Menu --
   -----------------------

   procedure Update_Float_Menu (Child : access MDI_Child_Record'Class) is
   begin
      if Child.MDI.Action_Float /= null then
         Child.MDI.Action_Float.Set_State
            (Gvariant_New_Boolean (Child.State = Floating));
      end if;
   end Update_Float_Menu;

   -------------------
   -- Has_Title_Bar --
   -------------------

   function Has_Title_Bar
     (Child : not null access MDI_Child_Record) return Boolean is
   begin
      case Child.MDI.Draw_Title_Bars is
         when Always       => return True;
         when Never        => return False;
         when Central_Only => return In_Central_Area (Child.MDI, Child);
      end case;
   end Has_Title_Bar;

   ----------------------
   -- Update_Tab_Color --
   ----------------------

   procedure Update_Tab_Color
     (Note    : access MDI_Notebook_Record'Class;
      Focused : Boolean)
   is
   begin
      if Note = null then
         return;
      end if;

      if not Note.Get_Mapped then
         return;
      end if;

      if not Focused then
         Get_Style_Context (Note).Remove_Class ("mdifocused");
      else
         Get_Style_Context (Note).Add_Class ("mdifocused");
      end if;

      Note.Queue_Draw;
   end Update_Tab_Color;

   ---------------------
   -- Set_Focus_Child --
   ---------------------

   procedure Set_Focus_Child (Child : not null access MDI_Child_Record) is
      Old : constant MDI_Child := Child.MDI.Focus_Child;
      C   : constant MDI_Child := Child.all'Unchecked_Access;
      Tmp : Boolean;
      pragma Unreferenced (Tmp);

      Previous_Focus_Child : constant MDI_Child := Child.MDI.Focus_Child;
      Widget : constant Gtk_Widget :=
        Gtk_Widget_Record (Child.all)'Unchecked_Access;
   begin
      if Child.MDI.Loading_Desktop then
         return;
      end if;

      if not Child.Is_Visible
        and then Maximize_Action.Is_Maximized_Mode (Child.MDI)
      then
         --  The child is hidden of another maximized child, thus unmaximize
         Widget_Callback.Emit_By_Name (Child, Signal_Unmaximize);
      end if;

      --  Be lazy. And avoid infinite loop when updating the MDI menu...

      if C = Old or else C.MDI.In_Destruction then
         return;
      end if;

      --  It is possible that this function is called before the child is
      --  even in the list of items. In this case, we do nothing at this
      --  point (might be called because we insert the child in a notebook
      --  first for instance)

      if Widget_List.Find (C.MDI.Items, Widget) = Null_List then
         return;
      end if;

      Child.MDI.Focus_Child := C;

      if Child.MDI.Focus_Freeze > 0 then
         return;
      end if;

      Show (C);  --  Make sure the child is visible

      if Traces then
         Print_Debug ("Set_Focus_Child on " & Get_Title (C));
      end if;

      if Previous_Focus_Child /= null then
         Update_Tab_Color (Get_Notebook (Previous_Focus_Child), False);
      end if;

      Update_Tab_Color (Get_Notebook (C), True);

      Ref (C);
      Remove (C.MDI.Items, Widget);
      Prepend (C.MDI.Items, Widget);
      Unref (C);

      --  Make sure the page containing Child in a notebook is put on top.
      --  Do not raise floating children, since this is the role of the window
      --  manager.

      if C.State /= Floating then
         Print_Debug ("Set_Focus_Child, raise child " & Get_Title (C));
         Raise_Child (C, False);
      end if;

      --  Give the actual keyboard focus to the appropriate subwindow of
      --  the focus child.

      Give_Focus_To_Child (Child.MDI.Focus_Child);

      if Old /= null
        and then Old.Get_Realized
      then
         Old.Title_Box.Queue_Draw;
      end if;

      if C.Initial.Get_Realized then
         C.Title_Box.Queue_Draw;

         --  Give the focus to the window containing the child.
         --  Giving the focus to a window has the side effect of moving the
         --  window to the current desktop. Therefore, we only do this when the
         --  input focus was already on a window of the MDI.

         if not Child.MDI.Loading_Desktop
           and then Previous_Focus_Child /= null
           and then Get_Toplevel (Previous_Focus_Child.Initial).Get_Realized
           and then Get_Property
             (Get_Toplevel (Previous_Focus_Child.Initial),
              Has_Toplevel_Focus_Property)
         then
            Raise_Child (C);
         end if;
      end if;

      Update_Float_Menu (C);

      if C.MDI.Action_Close /= null then
         C.MDI.Action_Close.Set_Enabled ((C.Flags and Destroy_Button) /= 0);
      end if;

      if C.MDI.Action_Select /= null then
         C.MDI.Action_Select.Set_State (Gvariant_New_String (C.Get_Title));
      end if;

      --  It would be nice to find the first child of C.Initial that
      --  accepts the keyboard focus. However, in the meantime, we at least
      --  want to make sure that no other widget has the focus. As a result,
      --  focus_in events will always be sent the next time the user selects a
      --  widget.

      Highlight_Child (C, False);

      Widget_Callback.Emit_By_Name (C, "selected");
      Child_Selected (C.MDI, C);
   end Set_Focus_Child;

   ------------------
   -- Delete_Child --
   ------------------

   function Delete_Child
     (Child : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean is
   begin
      if MDI_Child (Child).MDI.In_Destruction then
         --  We can always close a child when the MDI is being destroyed
         return False;

      elsif MDI_Child (Child).MDI.Close_Floating_Is_Unfloat
        and then (MDI_Child (Child).Flags and Always_Destroy_Float) = 0
        and then not MDI_Child (Child).MDI.All_Floating_Mode
      then
         Float_Child (MDI_Child (Child), False);

         Print_Debug
           ("Delete_Child, raising " & Get_Title (MDI_Child (Child)));
         Raise_Child (MDI_Child (Child), False);
         return True;

      else
         return Return_Callback.Emit_By_Name
           (MDI_Child (Child).Initial, "delete_event", Event);
      end if;
   end Delete_Child;

   ---------------------------
   -- Key_Event_In_Floating --
   ---------------------------

   function Key_Event_In_Floating
     (Win   : access Gtk_Widget_Record'Class;
      Event : Gdk_Event) return Boolean is
   begin
      --  Note: the following works because we are connected after the standard
      --  keypress event. Otherwise, standard keys in the child (space in
      --  editors most notably) will not work as expected.

      --  We do not want to forward to the main window any event.
      --  For instance, a simple character key press sent to a floating
      --  tree view should not be sent to a docked editor.

      --  We are interested in forwarding only a very specific set of
      --  key events, for instance the alt-<character> combinations that
      --  would open a menu.
      --  The test below is a coarse-grain for this.
      --  ??? We should probably do the opposite: rather than passing all
      --  events, only pass those that are known to be of interest.
      if Get_State (Event) = 0 then
         return False;
      end if;

      if Get_Event_Type (Event) = Key_Press then
         return Return_Callback.Emit_By_Name
           (Win, Signal_Key_Press_Event, Event);
      else
         return Return_Callback.Emit_By_Name
           (Win, Signal_Key_Release_Event, Event);
      end if;
   end Key_Event_In_Floating;

   ------------------------------------------
   -- Set_Default_Size_For_Floating_Window --
   ------------------------------------------

   procedure Set_Default_Size_For_Floating_Window
     (Child : not null access MDI_Child_Record;
      Win   : not null access Gtk_Window_Record'Class;
      Width, Height : Glib.Gint)
   is
      pragma Unreferenced (Child);
   begin
      Win.Set_Default_Size (Width, Height);
   end Set_Default_Size_For_Floating_Window;

   -----------------
   -- Float_Child --
   -----------------

   procedure Float_Child
     (Child             : not null access MDI_Child_Record'Class;
      Float             : Boolean;
      Position_At_Mouse : Boolean := True;
      X, Y              : Gint := 0) is
   begin
      Internal_Float_Child
        (Child, Float, Position_At_Mouse => Position_At_Mouse, X => X, Y => Y);
   end Float_Child;

   -----------------------------------
   -- Create_Float_Window_For_Child --
   -----------------------------------

   procedure Create_Float_Window_For_Child
      (Child     : not null access MDI_Child_Record;
       Win       : out Gtk_Window;
       Container : out Gtk_Container)
   is
      function Get_Current_Floating_Child return MDI_Child;
      --  If the current MDI child (i.e: the one that has the focus) is
      --  floating and realized, return it. Return null otherwise.

      --------------------------------
      -- Get_Current_Floating_Child --
      --------------------------------

      function Get_Current_Floating_Child return MDI_Child
      is
         Children : Widget_List.Glist;
         It       : MDI_Child;
      begin
         Children := Child.MDI.Items;
         It := MDI_Child (Get_Data (Children));

         if It /= MDI_Child (Child)
           and then It.State = Floating
              and then It.Initial.Get_Realized
         then
            return It;
         else
            return null;
         end if;
      end Get_Current_Floating_Child;

      Parent                 : Gtk_Window := null;
      Current_Floating_Child : constant MDI_Child :=
                                 Get_Current_Floating_Child;
   begin
      Gtk_New (Win);
      Container := Gtk_Container (Win);

      --  If the mode is set to Float_To_Main, we want to float the dialog as
      --  transient for the MDI's main window.
      --
      --  If the mode is set to Float_As_Transient , we want to float the
      --  dialog as transient for the current child when floating or the main
      --  window otherwise.
      --
      --  In any other case, we don't set any transient window.

      if (Child.Flags and Float_To_Main) /= 0 then
         Parent := Gtk_Window (Child.MDI.Get_Toplevel);
      elsif (Child.Flags and Float_As_Transient) /= 0 then
         if Current_Floating_Child /= null then
            Parent := Gtk_Window (Current_Floating_Child.Initial.Get_Toplevel);
         else
            Parent := Gtk_Window (Child.MDI.Get_Toplevel);
         end if;
      end if;

      if Parent /= null then
         Win.Set_Transient_For (Parent);

         --  Set the window's type hint to dialog to avoid displaying the
         --  minimize/maximize buttons: these buttons can lead to some strange
         --  behavior when a window is set as transient to a parent one (e.g:
         --  minimizing a window also minimizes the parent window on Windows).
         Win.Set_Type_Hint (Window_Type_Hint_Dialog);
      end if;
   end Create_Float_Window_For_Child;

   --------------------------
   -- Internal_Float_Child --
   --------------------------

   procedure Internal_Float_Child
     (Child             : access MDI_Child_Record'Class;
      Float             : Boolean;
      Position_At_Mouse : Boolean;
      X, Y              : Gint;
      Width, Height     : Gint := -1)
   is
      use Object_List;
      Win         : Gtk_Window;
      Cont        : Gtk_Container;
      Widget      : Gtk_Widget;
      Min, Requisition : Gtk_Requisition;
      Groups      : Object_List.GSlist;
      Box         : Gtk_Box;
      W, H        : Gint;
   begin
      if Traces then
         Print_Debug
           ("Float_Child " & Get_Title (Child)
            & " State=" & State_Type'Image (Child.State)
            & " Float=" & Boolean'Image (Float),
            Debug_Increase);
      end if;

      if Child.State /= Floating and then Float then
         --  If the Child already has a window, the resulting floating window
         --  should have the same size.
         --  Otherwise, ask the Child for its requisiton.

         if Width /= -1 and then Height /= -1 then
            W := Width;
            H := Height;
            if Traces then
               Print_Debug ("Forced size:" & Gint'Image (W) & "x"
                            & Gint'Image (H));
            end if;

         elsif Child.Get_Mapped then
            W := Child.Get_Parent.Get_Allocated_Width;
            H := Child.Get_Parent.Get_Allocated_Height;
            if Traces then
               Print_Debug ("Size from allocated:" & Gint'Image (W) & "x"
                            & Gint'Image (H));
            end if;

         else
            Child.Get_Preferred_Size (Min, Requisition);
            W := Requisition.Width;
            H := Requisition.Height;
            if Traces then
               Print_Debug ("Use preferred size:" & Gint'Image (W) & "x"
                            & Gint'Image (H));
            end if;
         end if;

         --  Ref is removed when the child is unfloated

         Ref (Child);

         --  This could be called before the child even has a parent if
         --  All_Floating_Mode is set.

         if Get_Parent (Child) /= null then
            Remove (Gtk_Container (Get_Parent (Child)), Child);
         end if;

         Child.Create_Float_Window_For_Child (Win, Cont);
         Child.Set_Default_Size_For_Floating_Window (Win, W, H);

         if Child.MDI.Use_Short_Titles_For_Floats then
            Set_Title (Win, Locale_From_UTF8 (Child.Short_Title.all));
         else
            Set_Title (Win, Locale_From_UTF8 (Child.Title.all));
         end if;

         --  Memorize the MDI_Child associated with the window, for faster
         --  lookup for instance in Find_MDI_Child_From_Widget.

         Child_User_Data.Set (Win, MDI_Child (Child), "parent_mdi_child");

         --  Add the 'mdichild' CSS class to the newly created floating window
         Get_Style_Context (Win).Add_Class ("mdichild");

         --  Set the accelerators for this window, so that menu key shortcuts
         --  behave the same as in the main window.
         --  ??? Should we do the same for mnemonics, even though the menu
         --  bar isn't available on this floating window.

         Groups := From_Object (Get_Toplevel (Child.MDI));
         while Groups /= Object_List.Null_List loop
            Add_Accel_Group (Win, Gtk_Accel_Group (Get_Data (Groups)));
            Groups := Next (Groups);
         end loop;

         if Position_At_Mouse then
            Set_Position (Win, Win_Pos_Mouse);
         else
            Move (Win, X, Y);
            Set_Position (Win, Win_Pos_None);
         end if;

         --  Delete_Event should be forwarded to the child, not to the
         --  toplevel window

         Return_Callback.Object_Connect
           (Win, Signal_Delete_Event,
            Return_Callback.To_Marshaller (Delete_Child'Access), Child);

         Add_Events (Win, Enter_Notify_Mask);
         Return_Callback.Object_Connect
           (Win, Signal_Focus_In_Event,
            Return_Callback.To_Marshaller
               (Set_Focus_Child_MDI_Floating'Access),
            Child);

         --  Forward all key events to the toplevel of the MDI. This provides
         --  proper handling of menu key shortcuts.

         Return_Callback.Object_Connect
           (Win, Signal_Key_Press_Event,
            Return_Callback.To_Marshaller (Key_Event_In_Floating'Access),
            Gtk_Window (Get_Toplevel (Child.MDI)), After => True);
         Return_Callback.Object_Connect
           (Win, Signal_Key_Release_Event,
            Return_Callback.To_Marshaller (Key_Event_In_Floating'Access),
            Gtk_Window (Get_Toplevel (Child.MDI)), After => True);

         Widget := Child.Initial.Get_Parent;
         Ref (Widget);
         Gtk_Container (Widget.Get_Parent).Remove (Widget);
         Cont.Add (Widget);
         Unref (Widget);
         if Cont.all in Gtk_Box_Record'Class then
            Gtk_Box (Cont).Set_Child_Packing
               (Widget, Expand => True, Fill => True, Padding => 0,
                Pack_Type => Pack_Start);
         end if;

         Set_State (Child, Floating);
         Update_Float_Menu (Child);
         Show_All (Win);

         Emit_By_Name_Child
           (Get_Object (Child.MDI), String (Signal_Float_Child) & ASCII.NUL,
            Get_Object (Child));
         Widget_Callback.Emit_By_Name (Child, Signal_Float_Child);

      elsif Child.State = Floating and then not Float then
         --  We are about to unfloat the child: emit the "before" signal
         --  before the actual unfloat, so that clients have the possibility
         --  to grab the coordinates of the floating window, for instance.

         Emit_By_Name_Child
           (Get_Object (Child.MDI),
            String (Signal_Before_Unfloat_Child) & ASCII.NUL,
            Get_Object (Child));
         Widget_Callback.Emit_By_Name (Child, Signal_Before_Unfloat_Child);

         --  Reassign the widget to Child instead of the notebook

         Win := Gtk_Window (Get_Toplevel (Child.Initial));
         Box := Gtk_Box (Get_Child (Child));
         Widget := Child.Initial.Get_Parent;

         Ref (Widget);
         Gtk_Container (Widget.Get_Parent).Remove (Widget);
         Box.Add (Widget);
         Unref (Widget);
         Box.Set_Child_Packing
            (Widget, Expand => True, Fill => True, Padding => 0,
             Pack_Type => Pack_Start);

         Set_State (Child, Normal);
         Destroy (Win);

         Put_In_Notebook (Child.MDI, Child);

         Update_Float_Menu (Child);

         Emit_By_Name_Child
           (Get_Object (Child.MDI), String (Signal_Unfloat_Child) & ASCII.NUL,
            Get_Object (Child));
         Widget_Callback.Emit_By_Name (Child, Signal_Unfloat_Child);

         Unref (Child);
      end if;

      Print_Debug ("", Debug_Decrease);
   end Internal_Float_Child;

   -----------------
   -- Is_Floating --
   -----------------

   function Is_Floating
     (Child : not null access MDI_Child_Record'Class) return Boolean is
   begin
      return Child.State = Floating;
   end Is_Floating;

   ------------------------
   -- On_Tab_Orientation --
   ------------------------

   procedure On_Tab_Orientation
     (Notebook    : access Gtk_Notebook_Record'Class;
      Orientation : Tab_Orientation_Type)
   is
      Child : MDI_Child;
      Length : constant Gint := Notebook.Get_N_Pages;
   begin
      MDI_Notebook (Notebook).Tab_Orientation := Orientation;

      if Length > 0 then
         for Page_Index in 0 .. Length - 1 loop
            Child := MDI_Child (Notebook.Get_Nth_Page (Page_Index));
            Update_Tab_Label (Child);
         end loop;
      end if;
   end On_Tab_Orientation;

   ----------------
   -- On_Tab_Pos --
   ----------------

   procedure On_Tab_Pos
     (Note : access Gtk_Notebook_Record'Class;
      Pos  : Gtk.Enums.Gtk_Position_Type)
   is
   begin
      Note.Set_Tab_Pos (Pos);

      Get_Style_Context (Note).Remove_Class ("rightTabs");
      Get_Style_Context (Note).Remove_Class ("leftTabs");
      Get_Style_Context (Note).Remove_Class ("bottomTabs");

      case Pos is
         when Pos_Top =>
            null;
         when Pos_Left =>
            Get_Style_Context (Note).Add_Class ("leftTabs");
         when Pos_Right =>
            Get_Style_Context (Note).Add_Class ("rightTabs");
         when Pos_Bottom =>
            Get_Style_Context (Note).Add_Class ("bottomTabs");
      end case;

      On_Tab_Orientation (Note, MDI_Notebook (Note).Tab_Orientation);
   end On_Tab_Pos;

   -------------
   -- Get_MDI --
   -------------

   function Get_MDI
     (Child : not null access MDI_Child_Record) return MDI_Window is
   begin
      return Child.MDI;
   end Get_MDI;

   -------------------------------------
   -- Set_Tab_Contextual_Menu_Factory --
   -------------------------------------

   procedure Set_Tab_Contextual_Menu_Factory
     (MDI     : access MDI_Window_Record;
      Factory : Tab_Contextual_Menu_Factory)
   is
   begin
      MDI.Tab_Factory := Factory;
   end Set_Tab_Contextual_Menu_Factory;

   ------------------------------
   -- On_Notebook_Button_Press --
   ------------------------------

   function On_Notebook_Button_Press
     (Child    : access Gtk_Widget_Record'Class;
      Event    : Gdk.Event.Gdk_Event) return Boolean
   is
      C    : constant MDI_Child := MDI_Child (Child);
      Note : constant MDI_Notebook := Get_Notebook (C);
      Menu : Gtk_Menu;
      Submenu : Gtk_Menu;
      Sep  : Gtk_Separator_Menu_Item;
      Item : Gtk_Menu_Item;
      MItem : Child_Menu_Item;
      Widget : MDI_Child;
      Image  : Gtk_Image;
      Current : Gint;
   begin
      if Get_Button (Event) = 3 then
         Gtk_New (Menu);

         Current := Note.Get_Current_Page;

         for P in 1 .. Note.Get_N_Pages loop
            Widget := MDI_Child (Note.Get_Nth_Page (P - 1));
            MItem := new Child_Menu_Item_Record;
            MItem.Child := Widget;
            Gtk.Image_Menu_Item.Initialize
              (MItem, Label => Widget.Short_Title.all);

            if P - 1 = Current then
               Gtk_Label (MItem.Get_Child).Set_Markup
                 ("<b>" & Widget.Short_Title.all & "</b>");
            end if;

            if Widget.Tab_Icon /= null then
               Gtk_New (Image, Gdk_Pixbuf'(Widget.Tab_Icon.Get));
               MItem.Set_Image (Image);
               MItem.Set_Always_Show_Image (True);
            end if;

            Menu.Append (MItem);
            Widget_Callback.Connect
              (MItem, Gtk.Menu_Item.Signal_Activate, Menu_Switch_Page'Access);
         end loop;

         Gtk_New (Sep);
         Menu.Append (Sep);

         Gtk_New (Item, "Tabs location");
         Append (Menu, Item);

         Gtk_New (Submenu);
         Set_Submenu (Item, Submenu);

         Gtk_New (Item, "Top");
         Tab_Pos_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Pos'Access, Note, Pos_Top);
         Append (Submenu, Item);

         Gtk_New (Item, "Bottom");
         Tab_Pos_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Pos'Access, Note, Pos_Bottom);
         Append (Submenu, Item);

         Gtk_New (Item, "Left");
         Tab_Pos_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Pos'Access, Note, Pos_Left);
         Append (Submenu, Item);

         Gtk_New (Item, "Right");
         Tab_Pos_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Pos'Access, Note, Pos_Right);
         Append (Submenu, Item);

         Gtk_New (Item, "Tabs rotation");
         Menu.Append (Item);

         Gtk_New (Submenu);
         Item.Set_Submenu (Submenu);

         Gtk_New (Item, "Automatic");
         Tab_Orientation_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Orientation'Access, Note, Automatic);
         Submenu.Append (Item);

         Gtk_New (Item, "Horizontal");
         Tab_Orientation_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Orientation'Access, Note, Horizontal);
         Submenu.Append (Item);

         Gtk_New (Item, "Bottom to top");
         Tab_Orientation_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Orientation'Access, Note, Bottom_To_Top);
         Submenu.Append (Item);

         Gtk_New (Item, "Top to bottom");
         Tab_Orientation_Callback.Object_Connect
           (Item, Gtk.Menu_Item.Signal_Activate,
            On_Tab_Orientation'Access, Note, Top_To_Bottom);
         Submenu.Append (Item);

         Gtk_New (Sep);
         Menu.Append (Sep);

         if (C.Flags and Destroy_Button) /= 0 then
            Gtk_New (Item, "Close");
            Widget_Callback.Object_Connect
              (Item, Gtk.Menu_Item.Signal_Activate, Close_Cb'Access, Child);
            Prepend (Menu, Item);
         end if;

         if C.MDI.Tab_Factory /= null then
            C.MDI.Tab_Factory (C, Menu);
         end if;

         Show_All (Menu);
         Popup (Menu,
                Button        => 3,
                Activate_Time => Gdk.Event.Get_Time (Event));
         return True;

      elsif Get_Event_Type (Event) = Gdk_2button_Press
        and then Get_Button (Event) = 1
      then
         if not Maximize_Action.Is_Maximized_Mode (C.MDI) then
            Widget_Callback.Emit_By_Name (C, Signal_Maximize_Child);
         else
            Widget_Callback.Emit_By_Name (C, Signal_Unmaximize);
         end if;
         return True;

      elsif Get_Button (Event) = 2 then
         C.Close_Child;
         return True;

      end if;
      return False;
   end On_Notebook_Button_Press;

   ----------------------
   -- Menu_Switch_Page --
   ----------------------

   procedure Menu_Switch_Page (Item : access Gtk_Widget_Record'Class) is
      It : constant Child_Menu_Item := Child_Menu_Item (Item);
   begin
      It.Child.Set_Focus_Child;
   end Menu_Switch_Page;

   ---------------------
   -- Create_Notebook --
   ---------------------

   function Create_Notebook
     (MDI : access MDI_Window_Record'Class) return MDI_Notebook
   is
      Notebook : MDI_Notebook;
   begin
      Notebook := new MDI_Notebook_Record;
      Notebook.Tab_Orientation := MDI.Tabs_Orientation;
      Gtk.Notebook.Initialize (Notebook);
      Configure_Notebook_Tabs (MDI, Notebook);
      Set_Border_Width (Notebook, 0);
      Set_Scrollable (Notebook);
      Notebook.Set_Group_Name ("MDI");
      Get_Style_Context (Notebook).Add_Class ("mdi");

      Widget_Callback.Connect
        (Notebook, Signal_Remove, Removed_From_Notebook'Access);
      Widget_Callback.Connect
        (Notebook, Signal_Set_Focus_Child, Set_Focus_Child_Notebook'Access);
      Widget_Callback.Connect
        (Notebook, Signal_Switch_Page,
         Set_Focus_Child_Switch_Notebook_Page'Access);

      Notebook.On_Motion_Notify_Event (Button_Motion_Notebook'Access);
      Notebook.On_Button_Release_Event (Button_Release_Notebook'Access);

      On_Tab_Pos (Notebook, MDI.Tabs_Position);
      return Notebook;
   end Create_Notebook;

   -----------------------------
   -- Configure_Notebook_Tabs --
   -----------------------------

   procedure Configure_Notebook_Tabs
     (MDI      : access MDI_Window_Record'Class;
      Notebook : access Gtk_Notebook_Record'Class;
      Hide_If_Empty : Boolean := False)
   is
      Child : MDI_Child;
      Page_Count : constant Gint := Get_N_Pages (Notebook);
      Visible_Page_Count : Natural := 0;
   begin
      --  Some pages might be hidden, in which case they should not be counted
      --  when we compute whether the tabs should be made visible

      for P in 0 .. Page_Count - 1 loop
         if Get_Nth_Page (Notebook, P).Get_Visible then
            Visible_Page_Count := Visible_Page_Count + 1;
         end if;
      end loop;

      if Visible_Page_Count >= 2 then
         Set_Show_Tabs (Notebook, MDI.Show_Tabs_Policy /= Never);
      else
         Set_Show_Tabs (Notebook, MDI.Show_Tabs_Policy = Always);
      end if;

      if Notebook.Get_Show_Tabs then
         Get_Style_Context (Notebook).Remove_Class ("noTabs");
      else
         Get_Style_Context (Notebook).Add_Class ("noTabs");
      end if;

      Child := MDI_Child (Get_Nth_Page (Notebook, 0));
      if Child /= null then
         Set_Property
            (Notebook, Show_Border_Property,
             Get_Nth_Page (Notebook, 1) = null
             and then MDI.Show_Tabs_Policy /= Always);
      end if;

      if Hide_If_Empty then
         if Visible_Page_Count = 0 then
            Hide (Notebook);
         else
            Show (Notebook);
         end if;
      end if;
   end Configure_Notebook_Tabs;

   -----------------------------
   -- Tab_Get_Preferred_Width --
   -----------------------------

   procedure Tab_Get_Preferred_Width
     (Label : System.Address; Minimum_Size, Natural_Size : out Gint)
   is
      Tab   : constant MDI_Tab := MDI_Tab (Glib.Object.Convert (Label));
      Note  : constant MDI_Notebook := MDI_Notebook (Tab.Get_Parent);
      Pages : Gint;
      Lab   : MDI_Tab;
      Min, Nat : Gint;
      Total    : Gint;
      Val : GValue;
      Focus_Width : Gint;
      Focus_Pad   : Gint;

   begin
      case Note.Get_Tab_Pos is
         when Pos_Left | Pos_Right =>
            --  fallback to gtk behavior
            Tab.Get_Child.Get_Preferred_Width (Minimum_Size, Natural_Size);

         when Pos_Top | Pos_Bottom =>
            --  Do we need to recompute, or has another tab already done the
            --  computation ?

            Tab.Timestamp := Tab.Timestamp + 1;
            if Note.Timestamp < Tab.Timestamp then
               Note.Timestamp := Tab.Timestamp;

               Pages := Note.Get_N_Pages;
               Minimum_Size := 0;
               Natural_Size := 0;

               --  Find all other MDI_Tab_Labels.

               for N in 0 .. Pages - 1 loop
                  Lab := MDI_Tab (Note.Get_Tab_Label (Note.Get_Nth_Page (N)));
                  Lab.Get_Child.Get_Preferred_Width (Min, Nat);
                  Natural_Size := Natural_Size + Nat;
                  Minimum_Size := Minimum_Size + Min;
               end loop;

               --  Compute borders from the theme. This is copied from
               --  gtknotebook.c. Apparently we do not need to get
               --     Get_Style_Context (Note).Get_Padding
               Init (Val, GType_Int);
               Note.Style_Get_Property ("focus-line-width", Val);
               Focus_Width := Get_Int (Val);
               Note.Style_Get_Property ("focus-padding", Val);
               Focus_Pad := Get_Int (Val);
               Unset (Val);

               Total := Note.Get_Allocated_Width
                 - Pages * 2 * (Focus_Width + Focus_Pad);

               if Traces then
                  Tab.Get_Child.Get_Preferred_Width (Min, Nat);
                  Put_Line ("Tabsize: total {Nat=" & Gint'Image (Natural_Size)
                            & " Min=" & Gint'Image (Minimum_Size)
                            & "} Child={"
                            & Gint'Image (Min) & Gint'Image (Nat)
                            & "} Notebook="
                            & Gint'Image (Total) & " Pages="
                            & Gint'Image (Pages));
               end if;

               if Natural_Size <= Total then
                  --  All tabs can use their natural size, but we must set the
                  --  tab's minimum size to its natural size since gtk+ always
                  --  uses the minimum size.

                  Note.Tab_Size := -1;
               else
                  --  All tabs should use the same size (and have a min. size)
                  --  "100" is random here, seems to be good enough to display
                  --  enough chars to distinguish editors.
                  Nat := Gint'Max (100, Total / Pages);
                  Note.Tab_Size := Nat;
               end if;

               --  Will need to resize all other tabs (nothing will happen for
               --  those that have already been refreshed, because of timestamp
               --  comparison)

               for N in 0 .. Pages - 1 loop
                  Lab := MDI_Tab (Note.Get_Tab_Label (Note.Get_Nth_Page (N)));
                  if Lab /= Tab then
                     Lab.Queue_Resize;
                  end if;
               end loop;

               Minimum_Size := Nat;
               Natural_Size := Nat;

               if Traces then
                  Put_Line ("tab size => note.timestamp="
                            & Integer'Image (Note.Timestamp)
                            & " tab.timestamp="
                            & Integer'Image (Tab.Timestamp)
                            & " size=" & Gint'Image (Nat));
               end if;
            else
               Tab.Timestamp := Note.Timestamp;
               if Note.Tab_Size = -1 then
                  Tab.Get_Child.Get_Preferred_Width (Min, Natural_Size);
                  Minimum_Size := Natural_Size;
               else
                  Minimum_Size := Note.Tab_Size;
                  Natural_Size := Note.Tab_Size;
               end if;
            end if;
      end case;
   end Tab_Get_Preferred_Width;

   ------------------------------
   -- Tab_Get_Preferred_Height --
   ------------------------------

   procedure Tab_Get_Preferred_Height
     (Label : System.Address; Minimum_Size, Natural_Size : out Gint)
   is
      Tab  : constant MDI_Tab := MDI_Tab (Glib.Object.Convert (Label));
      Note : constant MDI_Notebook := MDI_Notebook (Tab.Get_Parent);
      Pages       : Gint;
      Lab         : MDI_Tab;
      Max_Of_Min, Min, Nat    : Gint;
      Total       : Gint;
   begin
      case Note.Get_Tab_Pos is
         when Pos_Left | Pos_Right =>
            --  Do we need to recompute, or has another tab already done the
            --  computation ?

            Tab.Timestamp := Tab.Timestamp + 1;
            if Note.Timestamp < Tab.Timestamp then
               Note.Timestamp := Tab.Timestamp;

               Pages := Note.Get_N_Pages;
               Minimum_Size := 0;
               Natural_Size := 0;
               Max_Of_Min := 0;

               --  Find all other MDI_Tab_Labels.

               for N in 0 .. Pages - 1 loop
                  Lab := MDI_Tab (Note.Get_Tab_Label (Note.Get_Nth_Page (N)));
                  Lab.Get_Child.Get_Preferred_Height (Min, Nat);
                  Natural_Size := Natural_Size + Nat;
                  Minimum_Size := Minimum_Size + Min;
                  Max_Of_Min := Gint'Max (Max_Of_Min, Min);
               end loop;

               Total := Note.Get_Allocated_Height;

               if Traces then
                  Put_Line ("Tabsize: total {Nat=" & Gint'Image (Natural_Size)
                            & " Min=" & Gint'Image (Minimum_Size)
                            & "} Child={"
                            & Gint'Image (Min) & Gint'Image (Nat)
                            & "} Notebook="
                            & Gint'Image (Total) & " Pages="
                            & Gint'Image (Pages));
               end if;

               if Natural_Size <= Total then
                  --  All tabs can use their natural size, but we must set the
                  --  tab's minimum size to its natural size since gtk+ always
                  --  uses the minimum size.

                  Note.Tab_Size := -1;
               else
                  --  All tabs should use the same size (and have a min. size)
                  --  "100" is random here, seems to be good enough to display
                  --  enough chars to distinguish editors.
                  Nat := Gint'Max (Max_Of_Min, Total / Pages);
                  Note.Tab_Size := Nat;
               end if;

               --  Will need to resize all other tabs (nothing will happen for
               --  those that have already been refreshed, because of timestamp
               --  comparison)

               for N in 0 .. Pages - 1 loop
                  Lab := MDI_Tab (Note.Get_Tab_Label (Note.Get_Nth_Page (N)));
                  if Lab /= Tab then
                     Lab.Queue_Resize;
                  end if;
               end loop;

               Minimum_Size := Nat;
               Natural_Size := Nat;

               if Traces then
                  Put_Line ("tab size => note.timestamp="
                            & Integer'Image (Note.Timestamp)
                            & " tab.timestamp="
                            & Integer'Image (Tab.Timestamp)
                            & " size=" & Gint'Image (Nat));
               end if;
            else
               Tab.Timestamp := Note.Timestamp;
               if Note.Tab_Size = -1 then
                  Tab.Get_Child.Get_Preferred_Height (Min, Natural_Size);
                  Minimum_Size := Natural_Size;
               else
                  Minimum_Size := Note.Tab_Size;
                  Natural_Size := Note.Tab_Size;
               end if;
            end if;

         when Pos_Top | Pos_Bottom =>
            --  fallback to gtk behavior
            Tab.Get_Child.Get_Preferred_Height (Minimum_Size, Natural_Size);
      end case;
   end Tab_Get_Preferred_Height;

   --------------------
   -- Tab_Class_Init --
   --------------------

   procedure Tab_Class_Init (Self : GObject_Class) is
   begin
      Set_Default_Get_Preferred_Width_Handler
        (Self, Tab_Get_Preferred_Width'Access);
      Set_Default_Get_Preferred_Height_Handler
        (Self, Tab_Get_Preferred_Height'Access);
   end Tab_Class_Init;

   -----------------
   -- Get_Tooltip --
   -----------------

   function Get_Tooltip
     (Child : not null access MDI_Child_Record) return String
   is
      pragma Unreferenced (Child);
   begin
      return "";
   end Get_Tooltip;

   ---------------------------
   -- Get_Tooltip_Is_Markup --
   ---------------------------

   function Get_Tooltip_Is_Markup
     (Child : not null access MDI_Child_Record) return Boolean
   is
      pragma Unreferenced (Child);
   begin
      return False;
   end Get_Tooltip_Is_Markup;

   ----------------------
   -- Update_Tab_Label --
   ----------------------

   procedure Update_Tab_Label (Child : access MDI_Child_Record'Class) is
      Note   : constant MDI_Notebook := Get_Notebook (Child);
      Event  : Gtk_Event_Box;
      Box    : Gtk_Box;
      Close  : Close_Button.Gtkada_MDI_Close_Button;
      Orientation : Tab_Orientation_Type;

      procedure Add_Icon;
      procedure Add_Close_Button;
      procedure Add_Label;

      procedure Add_Icon is
      begin
         if Child.Icon_Name = null then
            Gtk_New (Child.Tab_Icon);
            Child.Tab_Icon.Hide;
         else
            Gtk_New_From_Icon_Name
              (Child.Tab_Icon, Child.Icon_Name.all, Icon_Size_In_Tabs);
            Child.Tab_Icon.Show_All;
         end if;

         --  The visibility of the Tab_Icon should be controlled by
         --  the actual presence of the icon. Set it to No_Show_All
         --  so that it does not take up space if the icon is not set.
         Child.Tab_Icon.Set_No_Show_All (No_Show_All => True);

         Box.Pack_Start (Child.Tab_Icon, Expand => False, Padding => 1);
      end Add_Icon;

      procedure Add_Close_Button is
      begin
         if (Child.Flags and Destroy_Button) /= 0 then
            Close_Button.Gtk_New
              (Close, Event, Child,
               Position    => Note.Get_Tab_Pos,
               In_Titlebar => False);
            Box.Pack_Start (Close, Expand => False, Padding => 2);
         end if;
      end Add_Close_Button;

      procedure Add_Label is
      begin
         Gtk_New (Child.Tab_Label, Child.Short_Title.all);
         Box.Pack_Start (Child.Tab_Label, Expand => True, Fill => True);

         declare
            Tooltip : constant String := Child.Get_Tooltip;
         begin
            if Tooltip = "" then
               Child.Tab_Label.Set_Tooltip_Text (Child.Title.all);
            elsif Child.Get_Tooltip_Is_Markup then
               Child.Tab_Label.Set_Tooltip_Markup (Tooltip);
            else
               Child.Tab_Label.Set_Tooltip_Text (Tooltip);
            end if;
         end;

         if Child.MDI.Homogeneous_Tabs then
            Child.Tab_Label.Set_Ellipsize (Pango.Layout.Ellipsize_Middle);
         end if;

         case Note.Actual_Tab_Orientation is
            when Horizontal | Automatic =>
               Child.Tab_Label.Set_Angle (0.0);
            when Bottom_To_Top =>
               Child.Tab_Label.Set_Angle (90.0);
            when Top_To_Bottom =>
               Child.Tab_Label.Set_Angle (270.0);
         end case;
      end Add_Label;

   begin
      if Note /= null and then Child.State = Normal then
         if Child.MDI.Homogeneous_Tabs then
            Event := new MDI_Tab_Record;
            MDI_Tab (Event).Timestamp := Note.Timestamp;

            Glib.Object.Initialize_Class_Record
              (Ancestor     => Gtk.Event_Box.Get_Type,
               Signals      => (1 .. 0 => Interfaces.C.Strings.Null_Ptr),
               Parameters   => (1 .. 0 => (1 => GType_None)),
               Class_Record => MDI_Tab_Class_Record,
               Type_Name    => "MDITab",
               Class_Init   => Tab_Class_Init'Access);

            G_New (Event, MDI_Tab_Class_Record.The_Type);
         else
            Gtk_New (Event);
         end if;

         Event.Set_App_Paintable (True);  --  prevent gtk_event_box_draw
         Event.Set_Visible_Window (False);

         Orientation := Note.Tab_Orientation;
         if Orientation = Automatic then
            case Note.Get_Tab_Pos is
               when Pos_Top | Pos_Bottom =>
                  Orientation := Horizontal;
               when Pos_Left =>
                  Orientation := Bottom_To_Top;
               when Pos_Right =>
                  Orientation := Top_To_Bottom;
            end case;
         end if;

         Note.Actual_Tab_Orientation := Orientation;

         if Note.Actual_Tab_Orientation = Horizontal then
            Gtk_New_Hbox (Box, Homogeneous => False);
         else
            Gtk_New_Vbox (Box, Homogeneous => False);
         end if;

         if Note.Actual_Tab_Orientation = Bottom_To_Top then
            Add_Close_Button;
            Add_Label;
            Add_Icon;
         else
            Add_Icon;
            Add_Label;
            Add_Close_Button;
         end if;

         Event.Add (Box);

         Note.Set_Tab_Label (Child, Event);
         Note.Set_Tab_Detachable (Child, False);

         --  Disable reordering. Otherwise, we hit a bug in gtk 3.8 where the
         --  display of the tabs is broken when the reorder operation ends up
         --  being a drag of the view to another notebook (N408-003)
         Note.Set_Tab_Reorderable (Child, False);

         Show_All (Event);

         Event.On_Button_Press_Event
           (Set_Focus_Child_MDI_From_Tab'Access, Slot => Child);
         Return_Callback.Object_Connect
           (Event, Signal_Button_Press_Event,
            Return_Callback.To_Marshaller (On_Notebook_Button_Press'Access),
            Child);
         Event.On_Button_Release_Event
           (Set_Focus_Child_MDI_From_Tab'Access, Slot => Child);

         --  Setup drag-and-drop, so that items can be moved from one location
         --  to another.

         Set_Dnd_Source (Event, Child);

         Event.Queue_Resize;
      end if;
   end Update_Tab_Label;

   -----------------
   -- Note_Notify --
   -----------------

   procedure Note_Notify (Data : System.Address; Where : System.Address) is
      pragma Unreferenced (Where);
      Old_Note_Was_Destroyed : aliased Boolean;
      for Old_Note_Was_Destroyed'Address use Data;
   begin
      Old_Note_Was_Destroyed := True;
   end Note_Notify;

   ---------------------
   -- Put_In_Notebook --
   ---------------------

   procedure Put_In_Notebook
     (MDI                      : access MDI_Window_Record'Class;
      Child                    : access MDI_Child_Record'Class;
      Notebook                 : MDI_Notebook := null;
      Initial_Position         : Child_Position := Position_Automatic;
      Force_Parent_Destruction : Boolean := True)
   is
      Note                   : MDI_Notebook;
      Old_Parent             : Gtk_Container;
      Destroy_Old            : Boolean := False;
      Old_Note_Was_Destroyed : aliased Boolean := False;

   begin
      --  Embed the contents of the child into the notebook

      if Notebook /= null then
         Note   := Notebook;

      elsif Child.Areas = Central_Only
        or else (Child.Group = Group_Default
                 and then not MDI.Independent_Perspectives
                 and then MDI.Central /= null
                 and then Child.Areas /= Sides_Only)
      then
         Note := Find_Current_In_Central
           (MDI.Central, MDI, Child.Group, Initial_Position);

      else
         Note := Find_Current_In_Central
           (MDI, MDI, Child.Group, Initial_Position);
      end if;

      if Get_Parent (Child) = Gtk_Widget (Note) then
         return;
      end if;

      Ref (Child);

      if Get_Parent (Child) /= null then
         Old_Parent := Gtk_Container (Get_Parent (Child));

         --  Always destroy the notebook we were in, since we are
         --  putting the item elsewhere anyway, there will still be
         --  a notebook for items in the same position.

         Destroy_Old := Force_Parent_Destruction
           and then Old_Parent.all in Gtk_Notebook_Record'Class
           and then Get_Nth_Page (Gtk_Notebook (Old_Parent), 1) = null;

         Weak_Ref
           (Old_Parent, Note_Notify'Access, Old_Note_Was_Destroyed'Address);
         Remove (Old_Parent, Child);

         if not Old_Note_Was_Destroyed then
            Weak_Unref
              (Old_Parent, Note_Notify'Access, Old_Note_Was_Destroyed'Address);
         end if;

         --  Problem: Old_Note might no longer exist not, since
         --  Removed_From_Notebook might have destroyed it.

         if Destroy_Old and then not Old_Note_Was_Destroyed then
            Destroy (Old_Parent);
         end if;
      end if;

      Set_State (Child, Normal);

      Append_Page (Note, Child);
      Note.Set_Menu_Label_Text (Child, Child.Short_Title.all);

      Configure_Notebook_Tabs (MDI, Note);

      if Child = Child.MDI.Focus_Child then
         Update_Tab_Color (Note, True);
      end if;

      Update_Tab_Label (Child);

      --  In case the user displays title bars only in the central area, we
      --  might need to change its visibility when moving in or out of the
      --  central area
      Set_Child_Title_Bar (Child);

      Set_Child_Visible (Note, True);
      Show (Note);

      Unref (Child);
   end Put_In_Notebook;

   -----------------------------
   -- Find_Current_In_Central --
   -----------------------------

   function Find_Current_In_Central
     (Pane             : access Gtkada_Multi_Paned_Record'Class;
      MDI              : access MDI_Window_Record'Class;
      Group            : Child_Group := Group_Any;
      Initial_Position : Child_Position := Position_Automatic)
      return MDI_Notebook
   is
      List                  : Widget_List.Glist := MDI.Items;
      C                     : MDI_Child;
      Note                  : MDI_Notebook;
      Current               : MDI_Notebook;
      Default_Current_Found : Boolean := False;
   begin
      if Gtkada_Multi_Paned (Pane) = Gtkada_Multi_Paned (MDI) then
         --  Do we already have a child within the same group ?

         while List /= Widget_List.Null_List loop
            C := MDI_Child (Get_Data (List));

            if C.State = Normal then
               Note := Get_Notebook (C);
               if Current = null then
                  Current := Note;
               end if;

               if not Default_Current_Found
                 and then C.Group = Group_Default
               then
                  Default_Current_Found := True;
                  Current := Note;
               end if;

               exit when Note /= null
                 and then (Group = Group_Any or else C.Group = Group);
               Note := null;
            end if;

            List := Next (List);
         end loop;

      else
         --  In the central area, look for the last child used, and put the new
         --  window on top of it

         if not MDI.Independent_Perspectives then
            while List /= Widget_List.Null_List loop
               C := MDI_Child (Get_Data (List));

               if In_Central_Area (MDI, C) then
                  Note := Get_Notebook (C);
                  Current := Note;
                  exit;
               end if;

               List := Next (List);
            end loop;
         end if;

         --  No last child ? It means the central area is empty (or contains
         --  an empty notebook, in case we could not reload the desktop, for
         --  instance because a file previously edited no longer exists).

         if Current = null then
            if Traces then
               Print_Debug
                 ("Find_Current_In_Central: no last child in <central>,"
                  & " checking whether we have an empty notebook");
            end if;

            if not MDI.Independent_Perspectives then
               declare
                  Iter : Gtkada.Multi_Paned.Child_Iterator :=
                    Start (MDI.Central);
               begin
                  while not At_End (Iter)
                    and then Get_Widget (Iter) = null
                  loop
                     Next (Iter);
                  end loop;

                  if not At_End (Iter) then
                     Print_Debug ("Found empty notebook, using it");
                     Note := MDI_Notebook (Get_Widget (Iter));
                     Current := Note;
                  end if;
               end;
            end if;

            --  Current might still be null if the central area really is empty
         end if;
      end if;

      if Note = null then
         if Traces then
            Print_Debug ("no notebook yet, Position="
                         & Child_Position'Image (Initial_Position));
         end if;

         case Initial_Position is
            when Position_Bottom =>
               Note := Create_Notebook (MDI);
               Split (Pane,
                      New_Child   => Note,
                      Ref_Pane    => Root_Pane,
                      Orientation => Orientation_Vertical,
                      Width       => -1,
                      Height      => -1,
                      After       => True);
            when Position_Top =>
               Note := Create_Notebook (MDI);
               Split (Pane,
                      New_Child   => Note,
                      Ref_Pane    => Root_Pane,
                      Orientation => Orientation_Vertical,
                      Width       => -1,
                      Height      => -1,
                      After       => False);
            when Position_Left =>
               Note := Create_Notebook (MDI);
               Split (Pane,
                      New_Child   => Note,
                      Ref_Pane    => Root_Pane,
                      Orientation => Orientation_Horizontal,
                      Width       => -1,
                      Height      => -1,
                      After       => False);
            when Position_Right =>
               Note := Create_Notebook (MDI);
               Split (Pane,
                      New_Child   => Note,
                      Ref_Pane    => Root_Pane,
                      Orientation => Orientation_Horizontal,
                      Width       => -1,
                      Height      => -1,
                      After       => True);
            when Position_Automatic | Position_Float =>
               if Current /= null then
                  Note := Current;
               else
                  Note := Create_Notebook (MDI);
                  Add_Child
                    (Pane, New_Child => Note, Width => -1, Height => -1);
               end if;
         end case;
      end if;

      return Note;
   end Find_Current_In_Central;

   ---------------------------
   -- Set_All_Floating_Mode --
   ---------------------------

   procedure Set_All_Floating_Mode
     (MDI : access MDI_Window_Record; All_Floating : Boolean)
   is
      List : Widget_List.Glist := First (MDI.Items);
      C    : MDI_Child;
   begin
      if All_Floating /= MDI.All_Floating_Mode then
         MDI.All_Floating_Mode := All_Floating;

         --  We cannot do a simple loop here. When a child is floated, it
         --  can happen that the mouse enters the window, and the focus changes
         --  immediately, resulting in a change in the order of children in the
         --  list, even though not all windows have been floated yet.
         --  To fix this, we do two loops: one to list all the widgets, and
         --  one to float them.

         MDI.Freeze_Focus;
         declare
            package Child_List is new Ada.Containers.Doubly_Linked_Lists
              (MDI_Child);
            --  ??? We should use this to implement MDI.Items, rather than
            --  a Widget_List.Glist.
            use Child_List;
            To_Process : Child_List.List;
            Cursor     : Child_List.Cursor;
         begin
            List := First (MDI.Items);

            while List /= Null_List loop
               C := MDI_Child (Get_Data (List));
               if (C.State /= Floating and then All_Floating)
                 or else (C.State = Floating and then not All_Floating)
               then
                  To_Process.Append (C);
               end if;
               List := Next (List);
            end loop;

            Cursor := To_Process.First;
            while Has_Element (Cursor) loop
               Float_Child (Element (Cursor), All_Floating);
               Next (Cursor);
            end loop;

            To_Process.Clear;

            MDI.Thaw_Focus;
         exception
            when others =>
               MDI.Thaw_Focus;
               raise;
         end;

         if MDI.Action_Float /= null then
            MDI.Action_Float.Set_Enabled (not All_Floating);
         end if;

         Set_Child_Visible (MDI, not All_Floating);

         --  Force a recomputation of the size
         Resize (Gtk_Window (Get_Toplevel (MDI)), -1, -1);
      end if;
   end Set_All_Floating_Mode;

   ---------------------------------
   -- Use_Short_Titles_For_Floats --
   ---------------------------------

   procedure Use_Short_Titles_For_Floats
     (MDI : access MDI_Window_Record; Short_Titles : Boolean)
   is
      List  : Widget_List.Glist := First (MDI.Items);
      Child : MDI_Child;
   begin
      if MDI.Use_Short_Titles_For_Floats = Short_Titles then
         --  Nothing to be changed
         return;
      end if;

      MDI.Use_Short_Titles_For_Floats := Short_Titles;

      --  The property has been changed. We need to walk though all children
      --  and enforce the title to the short one for floating children.

      loop
         List := First (MDI.Items);

         while List /= Null_List loop
            Child := MDI_Child (Get_Data (List));

            declare
               T : constant String := Child.Title.all;
               S : constant String := Child.Short_Title.all;
            begin
               Free (Child.Title);  --  Force a refresh
               Free (Child.Short_Title);
               Child.Set_Title (T, S);
            end;

            List := Next (List);
         end loop;

         exit when List = Null_List;
      end loop;
   end Use_Short_Titles_For_Floats;

   ----------------
   -- Get_Widget --
   ----------------

   function Get_Widget (Child : access MDI_Child_Record) return Gtk_Widget is
   begin
      if Child = null then
         return null;
      else
         return Child.Initial;
      end if;
   end Get_Widget;

   ---------------------
   -- Get_Focus_Child --
   ---------------------

   function Get_Focus_Child
     (MDI : access MDI_Window_Record) return MDI_Child is
   begin
      return MDI.Focus_Child;
   end Get_Focus_Child;

   ---------------------------
   -- Removed_From_Notebook --
   ---------------------------

   procedure Removed_From_Notebook
     (Note : access Gtk_Widget_Record'Class; Args  : Gtk_Args)
   is
      C     : constant Gtk_Widget := Gtk_Widget (To_Object (Args, 1));
      Child : MDI_Child;
   begin
      if C.all not in MDI_Child_Record'Class then
         return;
      end if;

      Child := MDI_Child (C);
      Child.Tab_Label := null;
      Child.Tab_Icon := null;
      Set_State (Child, Normal);

      if Child.MDI.Focus_Child = Child then
         Update_Tab_Color (Get_Notebook (Child), False);
      end if;

      if not Note.In_Destruction then
         Print_Debug ("Removed_From_Notebook: " & Get_Title (Child));

         --  No more pages in the notebook ? => Destroy it

         if Get_Nth_Page (Gtk_Notebook (Note), 0) = null then
            Destroy (Note);
         else
            Configure_Notebook_Tabs
              (Child.MDI, Gtk_Notebook (Note), Hide_If_Empty => True);

            --  force computation of tab size
            MDI_Notebook (Note).Timestamp := 0;
            Note.Queue_Resize;
         end if;

         if Traces then
            Print_Debug ("Removed_From_Notebook: desktop is now");
            Dump (Child.MDI);
         end if;
      end if;

   exception
      when E : others =>
         --  Silently ignore the exceptions for now, to avoid crashes.
         --  The application using the MDI can not do it, since this callback
         --  is called directly from the menu in Create_Menu.
         Print_Debug
           ("Unexpected exception: " & Exception_Information (E));
   end Removed_From_Notebook;

   -----------
   -- Split --
   -----------

   procedure Split
     (MDI           : access MDI_Window_Record;
      Orientation   : Gtk.Enums.Gtk_Orientation;
      Child         : MDI_Child := null;
      Mode          : Split_Mode := Before;
      Width, Height : Glib.Gint := 0)
   is
      Note, Note2 : MDI_Notebook;
      Target      : MDI_Child;
      Pane        : Gtkada_Multi_Paned;
      W           : Gtk_Widget;
      After       : Boolean := True;
   begin
      if Child /= null then
         Target := Child;
      elsif MDI.Focus_Child /= null then
         Target := MDI.Focus_Child;
      elsif MDI.Items = Widget_List.Null_List then
         return;
      else
         Target := MDI_Child (Get_Data (MDI.Items));
      end if;

      Note := Get_Notebook (Target);

      --  Only split if there are at least two children
      if Note /= null and then Get_Nth_Page (Note, 1) /= null then

         if In_Central_Area (MDI, Target) then
            Pane := MDI.Central;
         else
            Pane := Gtkada_Multi_Paned (MDI);
         end if;

         case Mode is
            when Before =>
               Note2 := null;
               After := False;

            when Gtkada.MDI.After =>
               Note2 := null;
               After := True;

            when Before_Reuse =>
               W := Splitted_Area (Pane, Note, Orientation, After => False);
               After := False;

            when After_Reuse =>
               W := Splitted_Area (Pane, Note, Orientation, After => True);
               After := True;

            when Any_Side_Reuse =>
               W := Splitted_Area (Pane, Note, Orientation, After => True);
               if W = null then
                  W := Splitted_Area (Pane, Note, Orientation, After => False);
               end if;
               After := True;
         end case;

         if W /= null and then W.all in Gtk_Notebook_Record'Class then
            Note2 := MDI_Notebook (W);
         end if;

         if Note2 = null then
            Note2 := Create_Notebook (MDI);
            Show_All (Note2);
            Split (Pane,
                   Ref_Widget  => Note,
                   New_Child   => Note2,
                   Width       => Width,
                   Height      => Height,
                   Orientation => Orientation,
                   After       => After);
         end if;

         Show (Note2);

         Ref (Target);
         Give_Focus_To_Previous_Child (Target);
         Remove (Note, Target);
         Put_In_Notebook (MDI, Target, Note2);
         Unref (Target);
         Set_Focus_Child (Target);

         Emit_By_Name
           (Get_Object (MDI),
            String (Signal_Children_Reorganized) & ASCII.NUL);
      end if;

      if Traces then
         Print_Debug ("After split " & Gtk_Orientation'Image (Orientation));
         Dump (MDI);
      end if;
   end Split;

   ----------------
   -- Split_H_Cb --
   ----------------

   procedure Split_H_Cb (MDI : access Gtk_Widget_Record'Class) is
      M : constant MDI_Window := MDI_Window (MDI);
   begin
      --  Do nothing unless the current child is in the central area, since
      --  otherwise this is disturbing for the user

      if M.Focus_Child /= null
        and then M.Focus_Child.State = Normal
      then
         Split (M, Orientation => Orientation_Horizontal);
      end if;

   exception
      when E : others =>
         Print_Debug
           ("Unexpected exception: " & Exception_Information (E));
   end Split_H_Cb;

   ----------------
   -- Split_V_Cb --
   ----------------

   procedure Split_V_Cb (MDI : access Gtk_Widget_Record'Class) is
   begin
      --  Do nothing unless the current child is in the central area, since
      --  otherwise this is disturbing for the user

      if MDI_Window (MDI).Focus_Child /= null
        and then MDI_Window (MDI).Focus_Child.State = Normal
      then
         Split (MDI_Window (MDI), Orientation => Orientation_Vertical);
      end if;
   exception
      when E : others =>
         Print_Debug
           ("Unexpected exception: " & Exception_Information (E));
   end Split_V_Cb;

   -----------------
   -- Maximize_Cb --
   -----------------

   procedure Maximize_Cb (MDI : access Gtk_Widget_Record'Class)
   is
      Child : constant MDI_Child := MDI_Window (MDI).Focus_Child;
   begin
      if Child /= null
        and then not Maximize_Action.Is_Maximized_Mode (MDI_Window (MDI))
      then
         Widget_Callback.Emit_By_Name (Child, Signal_Maximize_Child);
      end if;
   exception
      when E : others =>
         Print_Debug
           ("Unexpected exception: " & Exception_Information (E));
   end Maximize_Cb;

   -------------------
   -- Unmaximize_Cb --
   -------------------

   procedure Unmaximize_Cb (MDI : access Gtk_Widget_Record'Class)
   is
      Child : constant MDI_Child := MDI_Window (MDI).Focus_Child;
   begin
      if Child /= null
        and then Maximize_Action.Is_Maximized_Mode (MDI_Window (MDI))
      then
         Widget_Callback.Emit_By_Name (Child, Signal_Unmaximize);
      end if;
   exception
      when E : others =>
         Print_Debug
           ("Unexpected exception: " & Exception_Information (E));
   end Unmaximize_Cb;

   ---------------------------------------
   -- On_Select_Child_Update_Close_Menu --
   ---------------------------------------

   procedure On_Select_Child_Update_Close_Menu
      (Item   : access Gtk_Widget_Record'Class;
       Params : GValues)
   is
      C : constant MDI_Child :=
         MDI_Child (Get_User_Data_Or_Null (To_Address (Params, 1)));
   begin
      if C = null then
         Item.Set_Sensitive (False);
      else
         Item.Set_Sensitive ((C.Flags and Destroy_Button) /= 0);
      end if;
   end On_Select_Child_Update_Close_Menu;

   --------------------------------
   -- On_Float_Child_Update_Menu --
   --------------------------------

   procedure On_Float_Child_Update_Menu
      (Check : access Gtk_Widget_Record'Class)
   is
      C : constant MDI_Check_Menu_Item := MDI_Check_Menu_Item (Check);
      Focus : constant MDI_Child := C.MDI.Get_Focus_Child;
   begin
      C.MDI.Freeze_Float_Menu := True;
      C.Set_Active (Focus /= null and then Focus.State = Floating);
      C.MDI.Freeze_Float_Menu := False;
   end On_Float_Child_Update_Menu;

   --------------
   -- Float_Cb --
   --------------

   procedure Float_Cb (MDI : access GObject_Record'Class) is
      C : constant MDI_Child := Get_Focus_Child (MDI_Window (MDI));
   begin
      if not MDI_Window (MDI).Freeze_Float_Menu and then C /= null then
         Float_Child (C, C.State /= Floating);
         Set_Focus_Child (C);
         Raise_Child (C, False);
      end if;
   end Float_Cb;

   --------------
   -- Close_Cb --
   --------------

   procedure Close_Cb (MDI : access Gtk_Widget_Record'Class) is
      C : MDI_Child;
   begin
      if MDI.all in MDI_Window_Record'Class then
         C := MDI_Window (MDI).Focus_Child;

         --  Close automatically gets the contents of docks, instead of the
         --  dock itself

      else
         C := MDI_Child (MDI);
      end if;

      if C /= null then
         Close_Child (C);
      end if;

   exception
      when E : others =>
         --  Silently ignore the exceptions for now, to avoid crashes.
         --  The application using the MDI can not do it, since this callback
         --  is called directly from the menu in Create_Menu.
         Print_Debug ("Unexpected exception: " & Exception_Information (E));
   end Close_Cb;

   --------------
   -- Focus_Cb --
   --------------

   procedure Focus_Cb (Item  : access Gtk_Check_Menu_Item_Record'Class) is
      It : constant Menu_Item_For_Child := Menu_Item_For_Child (Item);
   begin
      if It.Child.MDI.Internal_Updating_Menu then
         return;
      end if;

      if It.Get_Active then
         --  If C is floating, raise the window.
         if It.Child.State = Floating then
            Raise_Child (It.Child, True);
         end if;
         Set_Focus_Child (It.Child);
      end if;
   end Focus_Cb;

   -----------------------------------------
   -- Get_Sorted_List_Of_Visible_Children --
   -----------------------------------------

   procedure Get_Sorted_List_Of_Visible_Children
      (MDI  : not null access MDI_Window_Record'Class;
       Vec  : out Child_Vectors.Vector)
   is
      use Child_Vectors;
      Tmp   : Widget_List.Glist;
      Child : MDI_Child;
   begin
      Vec.Clear;
      Tmp := First (MDI.Items);
      while Tmp /= Null_List loop
         Child := MDI_Child (Get_Data (Tmp));
         if Child.State /= Invisible then
            Vec.Append (Child);
         end if;
         Tmp := Next (Tmp);
      end loop;

      Vector_Sort.Sort (Vec);
   end Get_Sorted_List_Of_Visible_Children;

   -----------------------------------
   -- On_Child_Selected_Update_Menu --
   -----------------------------------

   procedure On_Child_Selected_Update_Menu
      (Item : access Gtk_Widget_Record'Class)
   is
      It : constant Menu_Item_For_Child := Menu_Item_For_Child (Item);
   begin
      It.Child.MDI.Internal_Updating_Menu := True;
      It.Set_Active (It.Child = It.Child.MDI.Get_Focus_Child);
      It.Child.MDI.Internal_Updating_Menu := False;
   end On_Child_Selected_Update_Menu;

   -----------------------------
   -- Internal_Add_Child_Menu --
   -----------------------------

   procedure Internal_Add_Child_Menu
     (Menu  : access MDI_Menu_Record'Class;
      Child : not null access MDI_Child_Record'Class;
      Group : in out Widget_SList.GSlist)
   is
      It : Gtk_Radio_Menu_Item;
   begin
      It := new Menu_Item_For_Child_Record'
        (Gtk_Radio_Menu_Item_Record with Child => MDI_Child (Child));
      Gtk.Radio_Menu_Item.Initialize (It, Group, "");
      Group := It.Get_Group;
      Menu.Add (It);

      Internal_Update_Menu_Content (Child, It);

      It.On_Toggled (Focus_Cb'Access, After => True);
      Widget_Callback.Object_Connect
        (Menu.MDI, Signal_Child_Selected,
         On_Child_Selected_Update_Menu'Access, It,
         After => True);

      It.Set_Accel_Path
        (Child.MDI.Accel_Path_Prefix.all
         & "/window/child/" & Child.Short_Title.all,
         Child.MDI.Group);
   end Internal_Add_Child_Menu;

   ----------------------------------
   -- Internal_Update_Menu_Content --
   ----------------------------------

   procedure Internal_Update_Menu_Content
     (Child : access MDI_Child_Record'Class;
      It    : not null access Gtk_Radio_Menu_Item_Record'Class)
   is
      Box   : Gtk_Box;
      Label : Gtk_Accel_Label;
      Icon  : Gtk_Image;
   begin
      It.Remove (It.Get_Child);

      Gtk_New_Hbox (Box, Homogeneous => False, Spacing => 5);
      It.Add (Box);

      Icon := Get_Icon (Child);
      if Icon /= null then
         Box.Pack_Start (Icon, Expand => False);
      end if;

      Gtk_New (Label, Child.Short_Title.all);
      Label.Set_Alignment (0.0, 0.5);
      Label.Set_Accel_Widget (It);
      Box.Pack_Start (Label, Expand => True, Fill => True);
      It.Set_Active (Child = Child.MDI.Focus_Child);

      --  Refresh graphically the menu
      It.Show_All;
   end Internal_Update_Menu_Content;

   ---------------------
   -- Find_Child_Menu --
   ---------------------

   function Find_Child_Menu
     (Menu  : access MDI_Menu_Record'Class;
      Child : not null access MDI_Child_Record'Class)
      return Menu_Item_For_Child
   is
      Children, L : Widget_List.Glist;
      W           : Gtk_Widget;
   begin
      Children := Menu.Get_Children;
      L := Children;
      while L /= Null_List loop
         W := Get_Data (L);
         L := Next (L);
         if W.all in Menu_Item_For_Child_Record'Class then
            declare
               It : constant Menu_Item_For_Child := Menu_Item_For_Child (W);
            begin
               if It.Child = Child then
                  return It;
               end if;
            end;
         end if;
      end loop;

      return null;
   end Find_Child_Menu;

   --------------------
   -- Add_Child_Menu --
   --------------------

   procedure Add_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class)
   is
      Menu        : constant MDI_Menu := MDI_Menu (Self);
      G           : Widget_SList.GSlist := Widget_SList.Null_List;
      Children, L : Widget_List.Glist;
      W           : Gtk_Widget;
   begin
      Children := Menu.Get_Children;
      L := Children;
      while L /= Null_List loop
         W := Get_Data (L);
         L := Next (L);
         if W.all in Menu_Item_For_Child_Record'Class then
            G := Menu_Item_For_Child (W).Get_Group;
            exit;
         end if;
      end loop;
      Free (Children);

      Internal_Add_Child_Menu (Menu, Child, G);

   end Add_Child_Menu;

   -----------------------
   -- Remove_Child_Menu --
   -----------------------

   procedure Remove_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class)
   is
      Menu : constant MDI_Menu := MDI_Menu (Self);
   begin
      if Self = null then
         return;
      end if;

      declare
         It : constant Menu_Item_For_Child := Find_Child_Menu (Menu, Child);
      begin
         if It /= null then
            It.Destroy;
         end if;
      end;
   end Remove_Child_Menu;

   -----------------------
   -- Update_Child_Menu --
   -----------------------

   procedure Update_Child_Menu
     (Self  : access Glib.Object.GObject_Record'Class;
      Child : not null access MDI_Child_Record'Class)
   is
      Menu : constant MDI_Menu := MDI_Menu (Self);
   begin
      if Self = null then
         return;
      end if;

      declare
         It : constant Menu_Item_For_Child := Find_Child_Menu (Menu, Child);
      begin
         if It /= null then
            Internal_Update_Menu_Content (Child, It);
         end if;
      end;
   end Update_Child_Menu;

   --------------------
   -- Recompute_Menu --
   --------------------

   procedure Recompute_Menu
      (Menu : access Gtk_Widget_Record'Class)
   is
      M : constant MDI_Menu := MDI_Menu (Menu);

      use Child_Vectors;
      G           : Widget_SList.GSlist := Widget_SList.Null_List;
      Vec         : Child_Vectors.Vector;
      Curs        : Child_Vectors.Cursor;
      Child       : MDI_Child;
      Children, L : Widget_List.Glist;
      W           : Gtk_Widget;
   begin
      --  Remove all items

      Children := M.Get_Children;
      L := Children;
      while L /= Null_List loop
         W := Get_Data (L);
         L := Next (L);
         if W.all in Menu_Item_For_Child_Record'Class then
            W.Destroy;
         end if;
      end loop;
      Free (Children);

      --  Add the list of children

      Get_Sorted_List_Of_Visible_Children (M.MDI, Vec);

      Curs := First (Vec);
      while Has_Element (Curs) loop
         Child := Element (Curs);
         Internal_Add_Child_Menu (M, Child, G);
         Next (Curs);
      end loop;

      M.Show_All;
   end Recompute_Menu;

   ---------------------
   -- Set_Focus_Child --
   ---------------------

   procedure Set_Focus_Child
     (MDI        : access MDI_Window_Record;
      Containing : access Gtk.Widget.Gtk_Widget_Record'Class)
   is
      Parent : Gtk_Widget := Gtk_Widget (Containing);
   begin
      if Containing = null then
         MDI.Focus_Child := null;
         Child_Selected (MDI, null);
      else
         while Parent /= null
           and then not (Parent.all in MDI_Child_Record'Class)
         loop
            Parent := Get_Parent (Parent);
         end loop;

         if Parent /= null then
            Set_Focus_Child (MDI_Child (Parent));
         end if;
      end if;
   end Set_Focus_Child;

   ------------------------
   -- On_Action_Floating --
   ------------------------

   procedure On_Action_Floating
      (MDI       : access GObject_Record'Class;
       Parameter : Glib.Variant.Gvariant)
   is
      pragma Unreferenced (Parameter);
   begin
      Float_Cb (MDI_Window (MDI));
   end On_Action_Floating;

   ---------------------
   -- On_Action_Close --
   ---------------------

   procedure On_Action_Close
      (MDI       : access GObject_Record'Class;
       Parameter : Glib.Variant.Gvariant)
   is
      pragma Unreferenced (Parameter);
   begin
      Close_Cb (MDI_Window (MDI));
   end On_Action_Close;

   -----------------------
   -- On_Action_Split_H --
   -----------------------

   procedure On_Action_Split_H
      (MDI       : access GObject_Record'Class;
       Parameter : Glib.Variant.Gvariant)
   is
      pragma Unreferenced (Parameter);
   begin
      Split_H_Cb (MDI_Window (MDI));
   end On_Action_Split_H;

   -----------------------
   -- On_Action_Split_V --
   -----------------------

   procedure On_Action_Split_V
      (MDI       : access GObject_Record'Class;
       Parameter : Glib.Variant.Gvariant)
   is
      pragma Unreferenced (Parameter);
   begin
      Split_V_Cb (MDI_Window (MDI));
   end On_Action_Split_V;

   ----------------------
   -- On_Action_Select --
   ----------------------

   procedure On_Action_Select
     (MDI       : access GObject_Record'Class;
      Parameter : Glib.Variant.Gvariant)
   is
      M      : constant MDI_Window := MDI_Window (MDI);
      Length : aliased Gsize;
      Name   : constant String := Get_String (Parameter, Length'Access);
      C      : constant MDI_Child := M.Find_MDI_Child_By_Name (Name);
   begin
      if C /= null then
         if C.State = Floating then
            Raise_Child (C, True);
         end if;
         C.Set_Focus_Child;
      end if;
   end On_Action_Select;

   -------------
   -- Desktop --
   -------------

   package body Desktop is

      Invalid_Desktop : exception;

      procedure Get_XML_For_Widget
        (Child            : MDI_Child;
         User             : User_Data;
         Widget_Is_Unique : out Boolean;
         Data             : out Node_Ptr);
      --  Get the XML node for a given widget. This automatically sets
      --  Child.XML_Node_Name as well.
      --  Widget_Is_Unique is set to True if there can be only one of the
      --  corresponding widget.

      procedure Parse_Child_Node
        (MDI         : access MDI_Window_Record'Class;
         Child_Node  : Node_Ptr;
         User        : User_Data;
         Focus_Child : in out MDI_Child;
         X           : out Gint;
         Y           : out Gint;
         W, H        : out Gint;
         Raised      : out Boolean;
         State       : out State_Type;
         Child       : out MDI_Child;
         To_Hide     : in out Gtk.Widget.Widget_List.Glist);
      --  Parse a <child> node and return the corresponding Child. The latter
      --  has not been inserted in the MDI.

      procedure Parse_Notebook_Node
        (MDI                   : access MDI_Window_Record'Class;
         Child_Node            : Node_Ptr;
         User                  : User_Data;
         Parent_Width, Parent_Height : Gint;
         Children_Count        : Integer;
         Parent_Orientation          : Gtk_Orientation;
         Focus_Child           : in out MDI_Child;
         Width, Height         : out Gint;
         Notebook              : out MDI_Notebook;
         To_Raise              : in out Gtk.Widget.Widget_List.Glist;
         To_Hide               : in out Gtk.Widget.Widget_List.Glist;
         Empty_Notebook_Filler : in out MDI_Child);
      --  Parse a <notebook> node.
      --  A new notebook is created and returned.
      --  If Reuse_Empty_If_Needed and we need to insert an empty notebook,
      --  we'll try and reuse an existing empty notebook. In this case, the
      --  variable is set to False.
      --  To_Raise is the children that are visible in the notebooks. It cannot
      --  be changed within this procedure, since when other items are loaded
      --  into the desktop, they might be put in the same notebook temporarily,
      --  before being moved to their actual location, and that would change
      --  the current page.
      --  Paned_Width and Paned_Height are the size of the multi_paned widget
      --  to which the "width" and "height" attributes are relative.

      procedure Parse_Pane_Node
        (Paned                 : access Gtkada_Multi_Paned_Record'Class;
         MDI                   : access MDI_Window_Record'Class;
         Node                  : Node_Ptr;
         Focus_Child           : in out MDI_Child;
         Parent_Width, Parent_Height : Gint;
         Parent_Children_Count : Integer;
         Parent_Orientation    : Gtk_Orientation;
         User                  : User_Data;
         Initial_Ref_Child     : MDI_Notebook := null;
         To_Raise              : in out Gtk.Widget.Widget_List.Glist;
         To_Hide               : in out Gtk.Widget.Widget_List.Glist;
         Empty_Notebook_Filler : in out MDI_Child);
      --  Parse a <Pane> node
      --  First_Child is the first notebook insert in pane (possibly inserted
      --  From_Tree points to the project-specific part of the desktop, where
      --  the contents of the children are saved.

      procedure Restore_Multi_Pane
        (Pane                    : access Gtkada_Multi_Paned_Record'Class;
         MDI                     : access MDI_Window_Record'Class;
         Focus_Child             : in out MDI_Child;
         To_Raise                : in out Gtk.Widget.Widget_List.Glist;
         To_Hide                 : in out Gtk.Widget.Widget_List.Glist;
         Node                    : Node_Ptr;
         User                    : User_Data;
         Full_Width, Full_Height : Gint);
      --  Restore a multi paned widget (either the perspective or the contents
      --  of the editor area)
      --  From_Tree points to the project-specific part of the desktop, where
      --  the contents of the children are saved.

      procedure Internal_Load_Perspective
        (MDI              : access MDI_Window_Record'Class;
         Name             : String;
         User             : User_Data;
         Focus_Child      : in out MDI_Child;
         To_Raise         : in out Gtk.Widget.Widget_List.Glist;
         To_Hide          : in out Gtk.Widget.Widget_List.Glist;
         MDI_Width, MDI_Height : Gint;
         Do_Size_Allocate : Boolean);
      --  Internal version of Load_Perspective.
      --  If Name is "", the first perspective is loaded.

      procedure Compute_Size_From_Attributes
        (MDI                         : access MDI_Window_Record'Class;
         Node                        : Node_Ptr;
         Parent_Width, Parent_Height : Gint;
         Parent_Orientation          : Gtk_Orientation;
         Width, Height               : out Gint;
         Children_Count              : Integer := 1);
      --  Compute the actual size of the widget represented by node, from the
      --  attributes of the node ("width" and "height", which use percent of
      --  the total pane size), and the attributes of the parent container.
      --  Children_Count is the number of children for the widget represented
      --  by Node, since the size returned is the one really available for
      --  sharing between the children (thus omitting the resize handles)

      type MDI_Menu_Item_Record is new Gtk_Menu_Item_Record with record
         MDI  : MDI_Window;
         User : User_Data;
      end record;
      type MDI_Menu_Item is access all MDI_Menu_Item_Record'Class;
      --  A menu item that stores a reference to the MDI and user data

      procedure Update_Perspective_Menu_Model
         (MDI : not null access MDI_Window_Record'Class);
      --  Update the menu model for /Window/Perspectives
      --
      procedure Create_Perspective_Query_Name
        (MDI  : not null access MDI_Window_Record'Class;
         User : User_Data);
      --  Ask the user for the name of a new perspective, and create it

      procedure Recompute_Perspective_Names
        (MDI : access MDI_Window_Record'Class);
      --  Recompute the name of all perspectives, and cache them

      package MDI_User_Data_Cb is new Gtk.Handlers.User_Callback
         (MDI_Window_Record, User_Data);

      ------------------------
      -- Change_Perspective --
      ------------------------

      procedure Change_Perspective
        (Item : access Gtk_Widget_Record'Class)
      is
         Persp : constant Perspective_Menu_Item :=
           Perspective_Menu_Item (Item);

         Name : constant String :=
           Persp.MDI.Perspective_Names (Persp.Name).all;
         --  Make a copy of the name, since Load_Perspective changes
         --  Persp.MDI.Perspective_Names
      begin
         if Get_Active (Persp) then
            Print_Debug ("++++ Change_Perspective to " & Name
                         & Integer'Image (Persp.Name));
            if not Persp.MDI.Loading_Desktop then
               Load_Perspective (Persp.MDI, Name, Persp.User);
            end if;
         end if;
      end Change_Perspective;

      -----------------------------------
      -- Create_Perspective_Query_Name --
      -----------------------------------

      procedure Create_Perspective_Query_Name
        (MDI  : not null access MDI_Window_Record'Class;
         User : User_Data)
      is
         Dialog : Gtk_Dialog;
         Label  : Gtk_Label;
         Ent    : Gtk_Entry;
         Button : Gtk_Widget;
         pragma Warnings (Off, Button);
      begin
         Gtk_New (Dialog, Title => "Enter perspective name",
                  Parent => Gtk_Window (Get_Toplevel (MDI)),
                  Flags  => Modal and Destroy_With_Parent);
         Button := Add_Button (Dialog, Stock_Ok, Gtk_Response_OK);
         Button := Add_Button (Dialog, Stock_Cancel, Gtk_Response_Cancel);
         Set_Default_Response (Dialog, Gtk_Response_OK);

         Gtk_New (Label, "Enter name of new perspective:");
         Pack_Start (Get_Content_Area (Dialog), Label, Expand => False);

         Gtk_New (Ent);
         Set_Activates_Default (Ent, True);
         Pack_Start (Get_Content_Area (Dialog), Ent, Expand => False);

         Show_All (Dialog);

         if Run (Dialog) = Gtk_Response_OK then
            Create_Perspective (MDI, Get_Text (Ent), User);
         end if;

         Destroy (Dialog);
      end Create_Perspective_Query_Name;

      ---------------------------
      -- Create_Perspective_CB --
      ---------------------------

      procedure Create_Perspective_CB
        (Item : access Gtk_Widget_Record'Class)
      is
         Persp : constant Perspective_Menu_Item :=
           Perspective_Menu_Item (Item);
      begin
         Create_Perspective_Query_Name (Persp.MDI, Persp.User);
      end Create_Perspective_CB;

      ------------------------
      -- Create_Perspective --
      ------------------------

      procedure Create_Perspective
        (MDI          : access MDI_Window_Record'Class;
         Name         : String;
         User         : User_Data)
      is
         Perspectives, Central : Node_Ptr;
      begin
         MDI.Current_Perspective := null;
         Save_Desktop (MDI, User, Perspectives, Central);
         Set_Attribute (MDI.Current_Perspective, "name", Name);
         Free (Perspectives);
         Free (Central);

         Recompute_Perspective_Names (MDI);
         Update_Perspective_Menu_Model (MDI);
         Emit_By_Name_Str
           (Get_Object (MDI),
            String (Signal_Perspectives_Added) & ASCII.NUL,
            Name & ASCII.NUL);
      end Create_Perspective;

      ------------------------
      -- Define_Perspective --
      ------------------------

      procedure Define_Perspective
        (MDI          : access MDI_Window_Record'Class;
         XML          : Glib.Xml_Int.Node_Ptr;
         User         : User_Data)
      is
         pragma Unreferenced (User);
         Name : constant String := Get_Attribute (XML, "name");
         Tmp : Node_Ptr;
      begin
         if Name = "" or else MDI.Perspectives = null then
            return;
         end if;

         Tmp := MDI.Perspectives.Child;

         while Tmp /= null loop
            if Get_Attribute (Tmp, "name") = Name then
               --  Perspective already exists
               return;
            end if;

            Tmp := Tmp.Next;
         end loop;

         Add_Child (MDI.Perspectives, Deep_Copy (XML), Append => True);
         Update_Perspective_Menu_Model (MDI);
         Emit_By_Name_Str
           (Get_Object (MDI),
            String (Signal_Perspectives_Added) & ASCII.NUL,
            Name & ASCII.NUL);
      end Define_Perspective;

      ----------------------------------------
      -- On_Perspective_Changed_Update_Menu --
      ----------------------------------------

      procedure On_Perspective_Changed_Update_Menu
         (Menu : access Gtk_Widget_Record'Class)
      is
         M : constant MDI_Menu_Item := MDI_Menu_Item (Menu);
         Submenu : Gtk_Menu;
         Persp   : Perspective_Menu_Item;
         Group   : Widget_SList.GSlist := Widget_SList.Null_List;
      begin
         Print_Debug ("Create_Perspective_Menu", Debug_Increase);

         --  Prevent changing perspective when setting "Active" on the buttons
         M.MDI.Loading_Desktop := True;

         Gtk_New (Submenu);
         Set_Submenu (M, Submenu);

         if M.MDI.Perspective_Names /= null then
            for N in M.MDI.Perspective_Names'Range loop
               Persp := new Perspective_Menu_Item_Record;
               Persp.MDI  := M.MDI;
               Persp.Name := N;
               Persp.User := M.User;

               Initialize (Persp, Group, M.MDI.Perspective_Names (N).all);
               Set_Active
                  (Persp,
                   M.MDI.Current_Perspective /= null
                   and then M.MDI.Perspective_Names (N).all =
                     Get_Attribute (M.MDI.Current_Perspective, "name"));
               Group := Get_Group (Persp);
               Append (Submenu, Persp);
               Widget_Callback.Connect
                 (Persp, Gtk.Menu_Item.Signal_Activate, CP_Access);
            end loop;
         end if;

         Persp := new Perspective_Menu_Item_Record;
         Persp.MDI  := M.MDI;
         Persp.User := M.User;
         Gtk.Menu_Item.Initialize (Persp, "<create new>");

         Widget_Callback.Connect
           (Persp, Gtk.Menu_Item.Signal_Activate, CreateP_Access);
         Append (Submenu, Persp);

         Show_All (Submenu);
         Show (Menu);

         M.MDI.Loading_Desktop := False;
         Print_Debug ("", Debug_Decrease);
      end On_Perspective_Changed_Update_Menu;

      -----------------------------------
      -- Update_Perspective_Menu_Model --
      -----------------------------------

      procedure Update_Perspective_Menu_Model
         (MDI : not null access MDI_Window_Record'Class)
      is
      begin
         if MDI.Perspectives_Menu /= null then
            MDI.Perspectives_Menu.Remove_All;

            if MDI.Perspective_Names /= null then
               for N in MDI.Perspective_Names'Range loop
                  MDI.Perspectives_Menu.Append
                      (MDI.Perspective_Names (N).all,
                       "app.mdi_set_perspective("""
                       & MDI.Perspective_Names (N).all
                       & """)");
               end loop;
            end if;

            MDI.Perspectives_Menu.Append
               ("<create new>", "app.mdi_create_perspective");

            MDI.Action_Select_Perspective.Set_State
               (Gvariant_New_String
                   (Get_Attribute (MDI.Current_Perspective, "name")));
         end if;

         Emit_By_Name
           (Get_Object (MDI), String (Signal_Perspective_Changed) & ASCII.NUL);
      end Update_Perspective_Menu_Model;

      -----------------
      -- Create_Menu --
      -----------------

      function Create_Menu
        (MDI               : access MDI_Window_Record'Class;
         Accel_Path_Prefix : String := "<gtkada>";
         User              : User_Data;
         Registration      : Menu_Registration_Procedure := null)
         return Gtk.Menu.Gtk_Menu
      is
         Menu  : MDI_Menu;
         Item  : Gtk_Menu_Item;
         Check : Gtk_Check_Menu_Item;
         Sep   : Gtk_Separator_Menu_Item;

         procedure Connect_Menu
           (Item       : Gtk_Menu_Item;
            Callback   : Widget_Callback.Marshallers.Void_Marshaller.Handler;
            Accel_Path : String);
         --  Utility function, factorizes code

         ------------------
         -- Connect_Menu --
         ------------------

         procedure Connect_Menu
           (Item       : Gtk_Menu_Item;
            Callback   : Widget_Callback.Marshallers.Void_Marshaller.Handler;
            Accel_Path : String)
         is
            Full_Accel_Path : constant String :=
                                Accel_Path_Prefix & "/window/" & Accel_Path;
         begin
            Append (Menu, Item);
            Widget_Callback.Object_Connect
              (Item, Gtk.Menu_Item.Signal_Activate,
               Widget_Callback.To_Marshaller (Callback), MDI);
            Set_Accel_Path (Item, Full_Accel_Path, MDI.Group);
            if Registration /= null then
               Registration (User, Get_Label (Item), Full_Accel_Path);
            end if;
         end Connect_Menu;

      begin
         if MDI.Accel_Path_Prefix = null then
            MDI.Accel_Path_Prefix := new String'(Accel_Path_Prefix);
         end if;

         Menu := new MDI_Menu_Record;
         Menu.MDI := MDI_Window (MDI);
         Gtk.Menu.Initialize (Menu);

         Item := new MDI_Menu_Item_Record'
            (Gtk_Menu_Item_Record with
             MDI => MDI_Window (MDI), User => User);
         Gtk.Menu_Item.Initialize (Item, "Perspectives");
         Append (Menu, Item);
         Widget_Callback.Object_Connect
            (MDI, Signal_Perspective_Changed,
             On_Perspective_Changed_Update_Menu_Access, Item);
         On_Perspective_Changed_Update_Menu (Item);

         Gtk_New (Item, "Split Side-by-Side");
         Connect_Menu (Item, Split_H_Cb'Access, "split_horizontal");

         Gtk_New (Item, "Split Up-Down");
         Connect_Menu (Item, Split_V_Cb'Access, "split_vertical");

         Gtk_New (Sep);
         Append (Menu, Sep);

         Gtk_New (Item, "Maximize");
         Connect_Menu (Item, Maximize_Cb'Access, "maximize_current_child");

         Gtk_New (Item, "Unmaximize");
         Connect_Menu (Item, Unmaximize_Cb'Access, "unmaximize_current_child");

         Check := new MDI_Check_Menu_Item_Record'
            (Gtk_Check_Menu_Item_Record with MDI => MDI_Window (MDI));
         Gtk.Check_Menu_Item.Initialize (Check, "Floating");
         Append (Menu, Check);
         Check.On_Toggled (Float_Cb'Access, MDI);
         Check.Set_Accel_Path
            (Accel_Path_Prefix & "/window/floating", MDI.Group);
         Widget_Callback.Object_Connect
            (MDI, Signal_Float_Child,
             Widget_Callback.To_Marshaller
                (On_Float_Child_Update_Menu'Access), Check);
         Widget_Callback.Object_Connect
            (MDI, Signal_Unfloat_Child,
             Widget_Callback.To_Marshaller
                (On_Float_Child_Update_Menu'Access), Check);

         Gtk_New (Sep);
         Append (Menu, Sep);

         Gtk_New (Item, "Close");
         Connect_Menu (Item, Close_Cb'Access, "close");
         Widget_Callback.Object_Connect
            (MDI, Signal_Child_Selected,
             On_Select_Child_Update_Close_Menu'Access, Item);

         Gtk_New (Sep);
         Append (Menu, Sep);

         Recompute_Menu (Menu);
         MDI.On_Child_Removed (Remove_Child_Menu'Access, Slot => Menu);
         MDI.On_Child_Added (Add_Child_Menu'Access, Slot => Menu);
         MDI.On_Child_Icon_Changed (Update_Child_Menu'Access, Slot => Menu);
         MDI.On_Child_Title_Changed (Update_Child_Menu'Access, Slot => Menu);
         Widget_Callback.Object_Connect
            (MDI, Signal_Perspective_Changed, Recompute_Menu'Access, Menu);

         Show_All (Menu);
         return Gtk_Menu (Menu);
      end Create_Menu;

      --------------------------------
      -- Register_Desktop_Functions --
      --------------------------------

      procedure Register_Desktop_Functions
        (Save : Save_Desktop_Function;
         Load : Load_Desktop_Function) is
      begin
         Registers := new Register_Node_Record'
           (Save => Save,
            Load => Load,
            Next => Registers);
      end Register_Desktop_Functions;

      ----------------------------------
      -- Compute_Size_From_Attributes --
      ----------------------------------

      procedure Compute_Size_From_Attributes
        (MDI                         : access MDI_Window_Record'Class;
         Node                        : Node_Ptr;
         Parent_Width, Parent_Height : Gint;
         Parent_Orientation          : Gtk_Orientation;
         Width, Height               : out Gint;
         Children_Count              : Integer := 1)
      is
         WAttr : constant String := Get_Attribute (Node, "width", "100%");
         HAttr : constant String := Get_Attribute (Node, "height", "100%");
         Tmp   : Gint;
         Handle_Size : constant Gint := MDI.Handle_Size;
      begin
         if WAttr (WAttr'Last) /= '%'
           or else HAttr (HAttr'Last) /= '%'
         then
            raise Invalid_Desktop with
              "Old-format perspectives are no longer supported";
         end if;

         --  For backward compatibility, we accept absolute sizes in the XML
         --  nodes, but that might lead to inconsistencies (and incorrect
         --  reload of desktop) if the user modifies this by hand
         --
         --  Depending on the orientation of the parent, one of the dimensions
         --  is in fact fixed (the full height or width of the parent).
         --
         --  If there are multiple children (case of panes for instance), the
         --  size we return is the one really available for children, not the
         --  physical size of the pane itself.

         case Parent_Orientation is
            when Orientation_Horizontal =>
               Height := Parent_Height;
               Tmp :=
                 Parent_Width - Gint (Children_Count - 1) * Handle_Size;
               Width  := Gint
                 (Float'Value (WAttr (WAttr'First .. WAttr'Last - 1))
                  * Float (Tmp) / 100.0);

            when Orientation_Vertical =>
               Width := Parent_Width;
               Tmp :=
                 Parent_Height - Gint (Children_Count - 1) * Handle_Size;
               Height := Gint
                 (Float'Value (HAttr (HAttr'First .. HAttr'Last - 1))
                  * Float (Tmp) / 100.0);
         end case;

         if Traces then
            Print_Debug
              ("Compute_Size_From_Attributes WAttr=" & WAttr
               & " HAttr=" & HAttr
               & " children=" & Integer'Image (Children_Count)
               & " => size " & Gint'Image (Width) & Gint'Image (Height));
         end if;
      end Compute_Size_From_Attributes;

      -------------------------
      -- Parse_Notebook_Node --
      -------------------------

      procedure Parse_Notebook_Node
        (MDI                         : access MDI_Window_Record'Class;
         Child_Node                  : Node_Ptr;
         User                        : User_Data;
         Parent_Width, Parent_Height : Gint;
         Children_Count              : Integer;
         Parent_Orientation          : Gtk_Orientation;
         Focus_Child                 : in out MDI_Child;
         Width, Height               : out Gint;
         Notebook                    : out MDI_Notebook;
         To_Raise                    : in out Gtk.Widget.Widget_List.Glist;
         To_Hide                     : in out Gtk.Widget.Widget_List.Glist;
         Empty_Notebook_Filler       : in out MDI_Child)
      is
         N            : Node_Ptr := Child_Node.Child;
         State        : State_Type;
         Raised       : Boolean;
         Raised_Child : MDI_Child;
         Child        : MDI_Child;
         X, Y, W, H   : Gint;
         Dummy        : Gtk_Label;
         Pos          : Gtk_Position_Type;

      begin
         if Traces then
            Print_Debug ("Parse_Notebook_Node Parent_Width="
                         & Gint'Image (Parent_Width) & " Parent_Height="
                         & Gint'Image (Parent_Height) & " Parent_Orientation="
                         & Gtk_Orientation'Image (Parent_Orientation),
                         Debug_Increase);
         end if;

         Compute_Size_From_Attributes
           (MDI, Child_Node, Parent_Width, Parent_Height, Parent_Orientation,
            Width, Height, Children_Count => Children_Count);

         Pos    := Gtk_Position_Type'Value
           (Get_Attribute (Child_Node, "Tabs",
            Gtk_Position_Type'Image (MDI.Tabs_Position)));

         Notebook := Create_Notebook (MDI);
         Print_Debug
           ("Parse_Notebook_Node: created new notebook "
            & System.Address_Image (Notebook.all'Address));

         Notebook.Tab_Orientation :=
           Tab_Orientation_Type'Value
             (Get_Attribute (Child_Node, "orientation",
              Tab_Orientation_Type'Image (Automatic)));

         --  Make sure Width and Height are not too small: that could happen
         --  if the main window has not been resized yet (thus has a size 1x1)
         --  and we load a perspective (since keeping place for the children
         --  windows might end up with negative sizes.

         Width := Gint'Max (Width, -1);
         Height := Gint'Max (Height, -1);

         On_Tab_Pos (Notebook, Pos);
         Set_Child_Visible (Notebook, True);
         Show_All (Notebook);

         while N /= null loop
            if N.Tag.all = "Child" then
               Parse_Child_Node
                 (MDI, N, User, Focus_Child, X, Y, W, H,
                  Raised, State, Child, To_Hide => To_Hide);

               --  Child cannot be floating while in a notebook
               if Child /= null then
                  if Raised
                    or else Raised_Child = null
                    or else Focus_Child = Child
                  then
                     Raised_Child := Child;
                  end if;

                  Print_Debug
                    ("Parse_Notebook_Node, moving child into the"
                     & " the notebook");
                  Float_Child (Child, False);
                  Put_In_Notebook (MDI, Child, Notebook);
                  Print_Debug
                    ("Parse_Notebook_Node, done moving child");
               else
                  Print_Debug ("Parse_Notebook_Node: no child created");
               end if;

            else
               --  Invalid node
               null;
            end if;

            N := N.Next;
         end loop;

         Print_Debug ("Parse_Notebook_Node: done adding all children");

         --  Create a dummy node if necessary, since otherwise the calls to
         --  Split afterward will simply discard that notebook. This dummy
         --  widget is destroyed at the end of restoring the desktop

         if Child_Node.Child = null and then Empty_Notebook_Filler = null then
            Gtk_New (Dummy, "");
            Gtk_New (Empty_Notebook_Filler, Dummy);
            Set_Title (Empty_Notebook_Filler, "<Dummy, notebook filler>");
            Put (MDI, Empty_Notebook_Filler);
            Put_In_Notebook (MDI, Empty_Notebook_Filler, Notebook);
         end if;

         if Raised_Child /= null then
            Prepend (To_Raise, Gtk_Widget (Raised_Child));

            --  Make sure the child appears first in the list for this
            --  notebook. That way, if the current focus child is closed by the
            --  user, we know the focus won't fallback to a child currently not
            --  visible in the notebook, which would result in a raise.
            Ref (Raised_Child);
            Remove (MDI.Items, Gtk_Widget (Raised_Child));
            Prepend (MDI.Items, Gtk_Widget (Raised_Child));
            Unref (Raised_Child);
         end if;

         Print_Debug ("", Debug_Decrease);
      end Parse_Notebook_Node;

      ----------------------
      -- Parse_Child_Node --
      ----------------------

      procedure Parse_Child_Node
        (MDI         : access MDI_Window_Record'Class;
         Child_Node  : Node_Ptr;
         User        : User_Data;
         Focus_Child : in out MDI_Child;
         X           : out Gint;
         Y           : out Gint;
         W, H        : out Gint;
         Raised      : out Boolean;
         State       : out State_Type;
         Child       : out MDI_Child;
         To_Hide     : in out Gtk.Widget.Widget_List.Glist)
      is
         N        : Node_Ptr;
         Register : Register_Node;
         Visible  : constant Boolean := Boolean'Value
           (Get_Attribute (Child_Node, "visible", "true"));
         Iter     : Child_Iterator;
         Tmp      : MDI_Child;
      begin
         Print_Debug ("Parse_Child_Node", Debug_Increase);

         W        := -1;
         H        := -1;
         Child    := null;
         Raised   := False;
         State    := Normal;
         X        := 0;
         Y        := 0;

         --  Check whether this child was already in a previous perspective.
         --  If that's the case, reuse it

         Iter := First_Child (MDI, Visible_Only => False);
         loop
            Tmp := Get (Iter);
            exit when Tmp = null;

            --  If not already used in the perspective

            if Tmp.State = Invisible
              and then Tmp.XML_Node_Name /= null
              and then Tmp.XML_Node_Name.all = Child_Node.Child.Tag.all
            then
               Print_Debug ("Reusing existing hidden view for "
                            & Child_Node.Child.Tag.all);
               Child := Tmp;
               Put (MDI, Child);  --  put it back in the MDI
               exit;
            end if;

            Next (Iter);
         end loop;

         --  Is there data associated with the node (in particular for widgets
         --  in the central area)

         if Child = null
           and then (Child_Node.Child.Child /= null
                     or else (Child_Node.Child.Attributes /= null
                              and then Child_Node.Child.Attributes.all /= ""))
         then
            Register := Registers;
            while Child = null and then Register /= null loop
               Child := Register.Load
                 (MDI_Window (MDI), Child_Node.Child, User);
               Register := Register.Next;
            end loop;
         end if;

         --  Check whether we have a project-specific contents for this child.
         --  This always takes priority other any project-independent contents.
         --  When we have multiple children with the same XML node name, we
         --  should use the first project-dependent part, then the second,...,
         --  and not reuse multiple times the first one. To do this, we simply
         --  remove the nodes from the project-dependent part as we use them,
         --  which also saves memory.

         N := MDI.View_Contents;
         if Child = null and then N /= null then
            N := N.Child;
            while N /= null loop
               if N.Tag.all = Child_Node.Child.Tag.all then
                  Register := Registers;
                  while Child = null and then Register /= null loop
                     Child := Register.Load (MDI_Window (MDI), N, User);
                     Register := Register.Next;
                  end loop;

                  if Child /= null then
                     Print_Debug ("Found project-specific contents for "
                                  & Child_Node.Child.Tag.all);

                     Free (N);
                     exit;
                  end if;
               end if;

               N := N.Next;
            end loop;
         end if;

         --  Else search for project-specific contents

         Register := Registers;

         while Child = null and then Register /= null loop
            Child := Register.Load (MDI_Window (MDI), Child_Node.Child, User);
            if Child /= null then
               Print_Debug ("Found project-independent contents for "
                            & Child_Node.Child.Tag.all);
            end if;
            Register := Register.Next;
         end loop;

         if Child = null then
            Print_Debug ("Parse_Child_Node: Could not create the child");
            return;
         end if;

         Print_Debug ("Parse_Child_Node: created " & Get_Title (Child));

         Child.Group := Child_Group'Value
           (Get_Attribute (Child_Node, "Group",
                           Child_Group'Image (Child.Group)));
         State := State_Type'Value
           (Get_Attribute (Child_Node, "State", "NORMAL"));
         Raised := Boolean'Value
           (Get_Attribute (Child_Node, "Raised", "False"));
         if Boolean'Value (Get_Attribute (Child_Node, "Focus", "False")) then
            Focus_Child := Child;
         end if;

         N := Child_Node.Child.Next;

         while N /= null loop
            --  We ignore the <title> and <short_title> fields. After all,
            --  the callback that created the child has or should have set
            --  a proper title already, and there is no reason to override
            --  this.

            if N.Tag.all = "X" then
               X := Gint'Value (N.Value.all);

            elsif N.Tag.all = "Y" then
               Y := Gint'Value (N.Value.all);

            elsif N.Tag.all = "width" then
               W := Gint'Value (N.Value.all);

            elsif N.Tag.all = "height" then
               H := Gint'Value (N.Value.all);

            else
               --  ??? Unknown node, just ignore for now
               null;
            end if;

            N := N.Next;
         end loop;

         Set_Size (MDI, Widget => Child, Width => W, Height => H);

         if not Visible then
            Print_Debug ("Parse_Child_Node: child will be hidden");
            Prepend (To_Hide, Gtk_Widget (Child));
         end if;

         Print_Debug ("", Debug_Decrease);

         exception
            when E : others =>
               pragma Debug
                 (Put_Line
                    ("Unexpected exception: " & Exception_Information (E)));

               if Traces then
                  Print_Debug ("Unexpected exception "
                               & Exception_Information (E));
               end if;
      end Parse_Child_Node;

      ---------------------
      -- Get_XML_Content --
      ---------------------

      function Get_XML_Content
        (MDI : access MDI_Window_Record'Class;
         Tag : String) return Glib.Xml_Int.Node_Ptr
      is
         function Internal_Get_XML_Content
           (N : Glib.Xml_Int.Node_Ptr) return Glib.Xml_Int.Node_Ptr;

         ------------------------------
         -- Internal_Get_XML_Content --
         ------------------------------

         function Internal_Get_XML_Content
           (N : Glib.Xml_Int.Node_Ptr) return Glib.Xml_Int.Node_Ptr
         is
            Node  : Glib.Xml_Int.Node_Ptr := N;
            Child : Glib.Xml_Int.Node_Ptr;

         begin
            while Node /= null loop
               if Node.Tag.all = Tag then
                  return Node;
               end if;

               Child := Internal_Get_XML_Content (Node.Child);

               if Child /= null then
                  return Child;
               end if;

               Node := Node.Next;
            end loop;

            return null;
         end Internal_Get_XML_Content;

      begin
         return Internal_Get_XML_Content (MDI.View_Contents);
      end Get_XML_Content;

      ---------------------
      -- Parse_Pane_Node --
      ---------------------

      procedure Parse_Pane_Node
        (Paned                       : access Gtkada_Multi_Paned_Record'Class;
         MDI                         : access MDI_Window_Record'Class;
         Node                        : Node_Ptr;
         Focus_Child                 : in out MDI_Child;
         Parent_Width, Parent_Height : Gint;
         Parent_Children_Count       : Integer;
         Parent_Orientation          : Gtk_Orientation;
         User                        : User_Data;
         Initial_Ref_Child           : MDI_Notebook := null;
         To_Raise                    : in out Gtk.Widget.Widget_List.Glist;
         To_Hide                     : in out Gtk.Widget.Widget_List.Glist;
         Empty_Notebook_Filler       : in out MDI_Child)
      is
         Orientation : constant Gtk_Orientation := Gtk_Orientation'Value
           (Get_Attribute (Node, "Orientation"));
         N             : Node_Ptr;
         Ref_Item      : Gtk_Widget := Gtk_Widget (Initial_Ref_Child);
         Count         : constant Natural := Children_Count (Node);
         Notebook_Node : Node_Ptr;
         Width, Height : Gint;

         Width_For_Children  : Gint := Parent_Width;
         Height_For_Children : Gint := Parent_Height;

      begin
         Compute_Size_From_Attributes
           (MDI, Node, Parent_Width, Parent_Height, Parent_Orientation,
            Width_For_Children, Height_For_Children, Parent_Children_Count);

         if Traces then
            New_Line;
            Print_Debug
              ("Parse_Pane_Node " & Gtk_Orientation'Image (Orientation)
               & " children=" & Integer'Image (Count)
               & " child_size=" & Gint'Image (Width_For_Children)
               & "x" & Gint'Image (Height_For_Children),
               Debug_Increase);
         end if;

         declare
            Notebooks : array (1 .. Count) of MDI_Notebook;
            W         : Gtk_Widget;
            Tmp_Width, Tmp_Height : Gint;
            Tmp_Orientation : Gtk_Orientation;
            Index     : Natural := Notebooks'First;
            Child_Count : Integer := Count;
         begin
            --  First insert all direct children of the pane, splitting as
            --  needed. Only then process the Pane children. Otherwise, the
            --  children of Pane will have been split and reorganized so that
            --  we won't be able to get a reference item for further splitting.

            N := Node.Child;

            while N /= null loop
               Tmp_Width       := Width_For_Children;
               Tmp_Height      := Height_For_Children;
               Tmp_Orientation := Orientation;

               --  Find the first notebook node of N
               Notebook_Node := N;
               while Notebook_Node.Tag /= null
                 and then Notebook_Node.Tag.all = "Pane"
               loop
                  Child_Count := Children_Count (Notebook_Node);
                  Compute_Size_From_Attributes
                    (MDI,
                     Notebook_Node,
                     Parent_Width       => Tmp_Width,
                     Parent_Height      => Tmp_Height,
                     Parent_Orientation => Tmp_Orientation,
                     Width              => Tmp_Width,
                     Height             => Tmp_Height,
                     Children_Count     => Child_Count);
                  Tmp_Orientation := Gtk_Orientation'Value
                    (Get_Attribute (Notebook_Node, "Orientation"));
                  Print_Debug
                    ("Descending into pane while looking for first notebook w="
                     & Gint'Image (Tmp_Width) & "x" & Gint'Image (Tmp_Height));

                  Notebook_Node := Notebook_Node.Child;
               end loop;

               if Index = Notebooks'First
                 and then Initial_Ref_Child /= null
               then
                  Notebooks (Index) := Initial_Ref_Child;
                  W := Gtk_Widget (Initial_Ref_Child);

               else
                  if Notebook_Node.Tag.all = "Notebook" then
                     Parse_Notebook_Node
                       (MDI                => MDI,
                        Child_Node         => Notebook_Node,
                        Parent_Width       => Tmp_Width,
                        Parent_Height      => Tmp_Height,
                        Parent_Orientation => Tmp_Orientation,
                        Children_Count     => Child_Count,
                        User         => User,
                        Focus_Child  => Focus_Child,
                        Width        => Width,
                        Height       => Height,
                        Notebook     => Notebooks (Index),
                        To_Raise     => To_Raise,
                        To_Hide      => To_Hide,
                        Empty_Notebook_Filler => Empty_Notebook_Filler);

                     W := Gtk_Widget (Notebooks (Index));

                  elsif Notebook_Node.Tag.all = "central" then
                     Print_Debug ("Parse_Pane_Node: seen <central>");
                     W      := Gtk_Widget (MDI.Central);
                     Compute_Size_From_Attributes
                       (MDI,
                        Notebook_Node,
                        Parent_Width       => Tmp_Width,
                        Parent_Height      => Tmp_Height,
                        Parent_Orientation => Tmp_Orientation,
                        Width              => Width,
                        Height             => Height,
                        Children_Count     => Child_Count);
                  end if;

                  if Get_Parent (W) = null then
                     if Ref_Item = null then
                        Print_Debug
                          ("Parse_Pane_Node, add notebook in MDI "
                           & System.Address_Image (W.all'Address));
                        Add_Child (Win         => Paned,
                                   New_Child   => W,
                                   Orientation => Orientation,
                                   Width       => Width,
                                   Height      => Height);
                     else
                        Print_Debug
                          ("Parse_Pane_Node Split notebook into MDI "
                           & System.Address_Image (W.all'Address)
                           & " ref="
                           & System.Address_Image (Ref_Item.all'Address)
                           & " Orient="
                           & Gtk_Orientation'Image (Orientation));
                        Split (Paned,
                               Ref_Widget  => Ref_Item,
                               New_Child   => W,
                               Width       => Width,
                               Height      => Height,
                               Orientation => Orientation);
                     end if;
                  else
                     Print_Debug
                       ("Parse_Pane_Node: notebook already in MDI");
                     Set_Size (Paned,
                               W,
                               Width  => Width,
                               Height => Height);
                  end if;
               end if;

               Ref_Item := W;
               Index := Index + 1;
               N := N.Next;
            end loop;

            --  Now process the Pane children recursively, splitting as needed

            Print_Debug ("Parse_Pane_Node: now process pane children");

            N := Node.Child;
            Index := Notebooks'First;
            while N /= null loop
               if N.Tag.all = "Pane" then
                  Parse_Pane_Node
                    (Paned                 => Paned,
                     MDI                   => MDI,
                     Node                  => N,
                     Focus_Child           => Focus_Child,
                     User                  => User,
                     Parent_Width          => Width_For_Children,
                     Parent_Height         => Height_For_Children,
                     Parent_Children_Count => Count,
                     Parent_Orientation    => Orientation,
                     Initial_Ref_Child     => Notebooks (Index),
                     To_Raise              => To_Raise,
                     To_Hide               => To_Hide,
                     Empty_Notebook_Filler => Empty_Notebook_Filler);
               end if;
               Index := Index + 1;
               N := N.Next;
            end loop;
         end;

         Print_Debug ("", Debug_Decrease);
      end Parse_Pane_Node;

      ------------------------
      -- Restore_Multi_Pane --
      ------------------------

      procedure Restore_Multi_Pane
        (Pane                    : access Gtkada_Multi_Paned_Record'Class;
         MDI                     : access MDI_Window_Record'Class;
         Focus_Child             : in out MDI_Child;
         To_Raise                : in out Gtk.Widget.Widget_List.Glist;
         To_Hide                 : in out Gtk.Widget.Widget_List.Glist;
         Node                    : Node_Ptr;
         User                    : User_Data;
         Full_Width, Full_Height : Gint)
      is
         Child_Node : Node_Ptr := Node.Child;
         Raised     : Boolean;
         X, Y, W, H : Gint;
         Child      : MDI_Child;
         State      : State_Type;

         Empty_Notebook_Filler : MDI_Child;
         --  Used to fill the empty notebook, and prevent it from being
         --  destroyed during a desktop load.

      begin
         Print_Debug ("Restore_Multi_Pane full_size="
                      & Gint'Image (Full_Width) & "x"
                      & Gint'Image (Full_Height), Debug_Increase);

         MDI.Freeze_Focus;
         declare
         begin
            while Child_Node /= null loop
               if Traces then
                  Print_Debug
                    ("Restore_Multi_Pane, got child """
                     & Child_Node.Tag.all & """");
               end if;

               if Child_Node.Tag.all = "Pane" then
                  Parse_Pane_Node
                    (Pane,
                     MDI                   => MDI,
                     Node                  => Child_Node,
                     Focus_Child           => Focus_Child,
                     Parent_Width          => Full_Width,
                     Parent_Height         => Full_Height,
                     Parent_Children_Count => 1,
                     Parent_Orientation    => Orientation_Horizontal,
                     User                  => User,
                     Initial_Ref_Child     => null,
                     To_Raise              => To_Raise,
                     To_Hide               => To_Hide,
                     Empty_Notebook_Filler => Empty_Notebook_Filler);

               elsif Child_Node.Tag.all = "Child" then
                  --  Used for floating children, and children in the default
                  --  desktop (see Add_To_Tree)

                  Parse_Child_Node
                    (MDI, Child_Node, User,
                     Focus_Child, X, Y, W, H, Raised, State, Child,
                     To_Hide => To_Hide);

                  if Child /= null then
                     case State is
                     when Floating =>
                        Internal_Float_Child
                          (Child, True, Position_At_Mouse => False,
                           X => X, Y => Y, Width => W, Height => H);

                     when Invisible =>
                        null;

                     when Normal =>
                        Float_Child (Child, False);
                     end case;
                  end if;

               elsif Child_Node.Tag.all = "central" then
                  Add_Child (MDI, MDI.Central,
                             Width => Full_Width, Height => Full_Height);
               end if;

               Child_Node := Child_Node.Next;
            end loop;
            MDI.Thaw_Focus;
         exception
            when others =>
               MDI.Thaw_Focus;
               raise;
         end;

         if Empty_Notebook_Filler /= null then
            --  The empty notebook has been created during the desktop load
            declare
               Note : constant Gtk_Notebook :=
                        Gtk_Notebook (Get_Parent (Empty_Notebook_Filler));
            begin
               Print_Debug
                 ("Restore desktop, removing empty_notebook_filler");

               Remove_Page (Note, 0);
            end;
         end if;

         Print_Debug ("", Debug_Decrease);
      end Restore_Multi_Pane;

      ---------------------------------
      -- Recompute_Perspective_Names --
      ---------------------------------

      procedure Recompute_Perspective_Names
        (MDI : access MDI_Window_Record'Class)
      is
         N     : Node_Ptr;
         Count : Natural := 0;
      begin
         Free (MDI.Perspective_Names);

         if MDI.Perspectives /= null then
            N := MDI.Perspectives.Child;

            while N /= null loop
               Count := Count + 1;
               N := N.Next;
            end loop;

            MDI.Perspective_Names := new GNAT.Strings.String_List (1 .. Count);

            Count := MDI.Perspective_Names'First;
            N := MDI.Perspectives.Child;
            while N /= null loop
               MDI.Perspective_Names (Count) :=
                 new String'(Get_Attribute (N, "name"));
               Count := Count + 1;
               N := N.Next;
            end loop;
         end if;
      end Recompute_Perspective_Names;

      ---------------------
      -- Restore_Desktop --
      ---------------------

      function Restore_Desktop
        (MDI          : access MDI_Window_Record'Class;
         Perspectives : Glib.Xml_Int.Node_Ptr;
         From_Tree    : Glib.Xml_Int.Node_Ptr;
         User         : User_Data) return Boolean
      is
         To_Raise                  : Gtk.Widget.Widget_List.Glist;
         To_Hide                   : Gtk.Widget.Widget_List.Glist;
         Focus_Child               : MDI_Child;
         Initial_All_Floating_Mode : constant Boolean := MDI.All_Floating_Mode;
         Do_Size_Allocate          : Boolean := True;
         MDI_Width, MDI_Height               : Gint;

      begin
         Maximize_Action.Free_Saved_Data (MDI);

         if Perspectives = null
           or else Perspectives.Child = null  --   <perspective> node
         then
            --  No desktop to load, but we still have to setup a minimal
            --  environment to avoid critical errors later on.
            if MDI.Central /= null then
               Destroy (MDI.Central);
            end if;

            Gtk_New (MDI.Central);
            Add_Child (MDI, MDI.Central);
            Print_Debug ("No perspective to restore");
            return False;
         end if;

         Free (MDI.Perspectives);
         MDI.Perspectives := Deep_Copy (Perspectives);
         MDI.Current_Perspective := null;
         Recompute_Perspective_Names (MDI);

         Free (MDI.View_Contents);
         MDI.View_Contents := Deep_Copy (From_Tree);
         --  ??? We could save some memory by freeing the <pane> nodes, but is
         --  there any point ?

         --  Temporarily disable the use of all floating mode, so that we can
         --  properly restore the desktop even if notebooks are referenced.
         MDI.All_Floating_Mode := False;

         if From_Tree /= null and then From_Tree.Tag.all /= "desktop" then
            return False;
         end if;

         Print_Debug ("Restore_Desktop", Debug_Increase);

         --  We must restore the size of the main window first, so that the
         --  rest of the desktop makes sense.

         declare
            Maximized : Boolean;
            Toplevel_Width, Toplevel_Height : Gint := -1;
         begin
            Maximized := Integer'Value
               (Get_Attribute (Perspectives, "state", "0")) = 4;
            if Maximized then

               --  Compute the width the window will have when maximized.
               --  We cannot simply do a Maximize and then read the allocation
               --  size, since that is asynchronous.
               --  On many systems, the following calls seem to fail, so we
               --  just simulate a size (this is irrelevant anyway, since the
               --  call to Maximize will find the correct size, but it helps
               --  debugging when we use the real sizes).

               --  A value big enough that when the window is finally
               --  resized the MDI is able to keep the proportions of its
               --  children (which would not be the case if we had created
               --  a 10x10 window for instance).
               MDI_Width := 1000;
               MDI_Height := 1000;

               Maximize (Gtk_Window (Get_Toplevel (MDI)));
               Do_Size_Allocate := False;

            else
               Toplevel_Width  := Gint'Value
                 (Get_Attribute (Perspectives, "width",  "-1"));
               Toplevel_Height := Gint'Value
                 (Get_Attribute (Perspectives, "height", "-1"));

               --  More recent versions of the desktop also explicitly store
               --  the size of the MDI
               MDI_Width  := Gint'Value
                 (Get_Attribute (Perspectives, "mdi_width",
                  Gint'Image (Toplevel_Width)));
               MDI_Height := Gint'Value
                 (Get_Attribute (Perspectives, "mdi_height",
                  Gint'Image (Toplevel_Height)));

               if Traces then
                  Print_Debug
                    ("Toplevel size from perspective:"
                     & Gint'Image (Toplevel_Width) & "x"
                     & Gint'Image (Toplevel_Height));
                  Print_Debug
                    ("MDI size from perspective:"
                     & Gint'Image (MDI_Width) & "x"
                     & Gint'Image (MDI_Height));
               end if;

               if Toplevel_Width /= -1 then
                  Set_Default_Size
                    (Gtk_Window (MDI.Get_Toplevel),
                     Toplevel_Width, Toplevel_Height);
               end if;

               --  ??? Should not call MDI.Set_Size_Request, since that
               --  sets a minimal size for the MDI, not a default size.
               --     MDI.Set_Size_Request (MDI_Width, MDI_Height);
            end if;
         exception
            when others =>
               --  An invalid attribute in XML ?
               null;
         end;

         --  Close all existing windows (internal_load_perspective would try to
         --  preserve them, but they do not apply to the current desktop)

         MDI.Freeze_Focus;
         declare
            Tmp              : Widget_List.Glist := MDI.Items;
            Tmp2             : Widget_List.Glist;
            C                : MDI_Child;
            Widget_Node      : Node_Ptr;
            Widget_Is_Unique : Boolean;
         begin
            while Tmp /= Null_List loop
               Tmp2 := Next (Tmp);

               --  Do not force closure, we want to keep desktop-independent
               --  views
               Close (MDI, MDI_Child (Get_Data (Tmp)), Force => False);
               Tmp := Tmp2;
            end loop;

            Tmp := MDI.Items;
            while Tmp /= Null_List loop
               C := MDI_Child (Get_Data (Tmp));

               --  For those items still in the list, we must ensure we know
               --  their XML node name, otherwise they will never be reused and
               --  just waste memory (and result in memory leaks)

               if C.XML_Node_Name = null then
                  Get_XML_For_Widget
                    (Child => C, User => User, Data => Widget_Node,
                     Widget_Is_Unique => Widget_Is_Unique);
                  Free (Widget_Node);
               end if;

               Tmp := Next (Tmp);
            end loop;
            MDI.Thaw_Focus;
         exception
            when others =>
               MDI.Thaw_Focus;
               raise;
         end;

         --  Prepare the contents of the central area. This will automatically
         --  replace the central area's contents in the perspective

         if MDI.Central /= null then
            Print_Debug ("Destroying central area", Debug_Increase);
            --  It could come from a previous desktop
            Destroy (MDI.Central);
            Print_Debug ("", Debug_Decrease);
         end if;

         Gtk_New (MDI.Central);

         --  The central area describes the floating children, so they are not
         --  part of MDI.Central.

         To_Raise := Widget_List.Null_List;
         To_Hide  := Widget_List.Null_List;

         MDI.Loading_Desktop := True;

         if not MDI.Independent_Perspectives and then From_Tree /= null then
            --  ??? Incorrect: the size of the central area is only known once
            --  we have loaded the perspectives, it isn't MDI_Width. But we
            --  need the central area to load the rest of the desktop.

            if Traces then
               Print_Debug ("Loading central area", Debug_Increase);
            end if;

            Restore_Multi_Pane
              (Pane        => MDI.Central,
               MDI         => MDI,
               Focus_Child => Focus_Child,
               To_Raise    => To_Raise,
               To_Hide     => To_Hide,
               User        => User,
               Node        => From_Tree,
               Full_Width  => MDI_Width,
               Full_Height => MDI_Height);

            Print_Debug ("Done loading central area", Debug_Decrease);

            Set_Child_Visible (MDI.Central, True);
         end if;

         --  Now restore the appropriate perspective, which gives the global
         --  organization of the desktop apart from the default area (which is
         --  restored later on).

         Internal_Load_Perspective
           (MDI,
            Get_Attribute (From_Tree, "perspective", ""),
            User, Focus_Child => Focus_Child,
            To_Raise          => To_Raise,
            To_Hide           => To_Hide,
            MDI_Width         => MDI_Width,
            MDI_Height        => MDI_Height,
            Do_Size_Allocate  => Do_Size_Allocate);

         Set_All_Floating_Mode (MDI, Initial_All_Floating_Mode);

         if Focus_Child /= null then
            Print_Debug
              ("Desktop set focus on " & Get_Title (Focus_Child));
            Set_Focus_Child (Focus_Child);
         end if;

         Emit_By_Name
           (Get_Object (MDI), String (Signal_Perspective_Changed) & ASCII.NUL);
         Emit_By_Name
           (Get_Object (MDI),
            String (Signal_Children_Reorganized) & ASCII.NUL);

         Print_Debug ("Done Restore_Desktop", Debug_Decrease);

         return True;

      exception
         when E : Invalid_Desktop =>
            Put_Line ("Invalid_Desktop: " & Exception_Message (E));
            return False;
      end Restore_Desktop;

      ------------------------
      -- Get_XML_For_Widget --
      ------------------------

      procedure Get_XML_For_Widget
        (Child            : MDI_Child;
         User             : User_Data;
         Widget_Is_Unique : out Boolean;
         Data             : out Node_Ptr)
      is
         Register    : Register_Node := Registers;
         Widget_Node : Node_Ptr;
      begin

         Widget_Node := Child.Save_Desktop;  --  first, the primitif op.

         while Widget_Node = null and then Register /= null loop
            if Register.Save /= null then
               Widget_Node := Register.Save (Child.Initial, User);
            end if;
            Register := Register.Next;
         end loop;

         if Widget_Node /= null then
            --  Save the XML node name, which might be useful when switching
            --  perspectives

            Free (Child.XML_Node_Name);
            Child.XML_Node_Name := new String'(Widget_Node.Tag.all);
         end if;

         Data := Widget_Node;

         --  ??? Hard-coded for now. See comments in Save_Widget
         Widget_Is_Unique := True;
      end Get_XML_For_Widget;

      ------------------
      -- Save_Desktop --
      ------------------

      procedure Save_Desktop
        (MDI          : access MDI_Window_Record'Class;
         User         : User_Data;
         Perspectives : out Glib.Xml_Int.Node_Ptr;
         Central      : out Glib.Xml_Int.Node_Ptr)
      is
         Item             : Widget_List.Glist;
         Child_Node       : Node_Ptr;
         Child            : MDI_Child;

         Has_Negative_Values : Boolean := False;

         procedure Add (Parent : Node_Ptr; Name, Value : String);
         --  Add a new child to Child_Node

         procedure Save_Widget
           (Parent     : Node_Ptr;
            Child      : MDI_Child;
            Raised     : Boolean;
            In_Central : Boolean);
         --  Save the Child. Raised is True if Child is the current page
         --  in a notebook. In_Central is True if we are saving a child part of
         --  the central area of the desktop

         procedure Save_Size
           (Iter   : Gtkada.Multi_Paned.Child_Iterator;
            Node   : Node_Ptr);
         --  Set the size of Widget (relative to the total size of the window)
         --  as attributes of Node

         procedure Save_Paned
           (Paned      : access Gtkada_Multi_Paned_Record'Class;
            Parent     : Node_Ptr;
            In_Central : Boolean);
         --  Look through all the notebooks, and save the widgets in the
         --  notebook order.

         function Save_Notebook
           (Current    : Node_Ptr;
            Note       : MDI_Notebook;
            In_Central : Boolean) return Node_Ptr;
         --  save all pages of the notebook

         procedure Prune_Empty (N : in out Node_Ptr);
         --  Prunes empty panes below N

         ---------
         -- Add --
         ---------

         procedure Add (Parent : Node_Ptr; Name, Value : String) is
            N : Node_Ptr;
         begin
            N := new Node;
            N.Tag := new String'(Name);
            N.Value := new String'(Value);
            Add_Child (Parent, N, Append => True);
         end Add;

         -----------------
         -- Save_Widget --
         -----------------

         procedure Save_Widget
           (Parent     : Node_Ptr;
            Child      : MDI_Child;
            Raised     : Boolean;
            In_Central : Boolean)
         is
            Widget_Node      : Node_Ptr;
            Tmp_Node         : Node_Ptr;
            Widget_Is_Unique : Boolean;
         begin
            if Child.State = Invisible then
               return;
            end if;

            Get_XML_For_Widget (Child, User, Widget_Is_Unique, Widget_Node);

            if Widget_Node /= null then
               --  When a window is in the perspective (and not in the central
               --  area), we used to save its location in the <perspective>
               --  node, and its contents in <central>, since it is project
               --  specific..
               --  This works fine for the location window, for instance.
               --  But this doesn't work well for editors (for which there
               --  appears an empty area in the newly loaded desktop). Getting
               --  rid of the emplty areas requires restarting the MDI.
               --  Since the latter is blocking, the safest route for now is to
               --  always save the contents in the perspective, independently
               --  of the project. Most users always load the same project
               --  anyway.
               --  For windows that are in multiple perspectives, their
               --  contents is only saved in the current perspective. That
               --  works as expected because this is also the perspective that
               --  is reloaded.
               --  ??? One solution might be to let the widget itself decide
               --  where to save the contents when the window is not in the
               --  central area. Note that saving in <central> works best if
               --  there is a single window of a kind (so not for editors).

               Tmp_Node := Widget_Node;

               if Widget_Is_Unique
                 and then not MDI.Independent_Perspectives
                 and then not In_Central
                 and then
                   (Widget_Node.Child /= null
                    or else Widget_Node.Attributes /= null)
               then
                  Tmp_Node := new Node;
                  Tmp_Node.Tag := new String'(Widget_Node.Tag.all);
                  Add_Child (Central, Widget_Node, Append => True);
               end if;

               --  Note: We need to insert the children in the opposite order
               --  from Restore_Desktop, since the children are added at the
               --  beginning of the list.

               Child_Node := new Node;
               Child_Node.Tag := new String'("Child");
               Add_Child (Child_Node, Tmp_Node, Append => True);

               Set_Attribute (Child_Node, "State",
                              State_Type'Image (Child.State));
               Set_Attribute (Child_Node, "Group",
                              Child_Group'Image (Child.Group));

               if Child.State = Floating then
                  declare
                     Win  : constant Gtk_Widget :=
                              Get_Toplevel (Child.Initial);
                     X, Y : Gint;
                  begin

                     --  Save the floating window's size
                     --  Note: This size doesn't include the size of the window
                     --  decorations, doesn't seem to be a way to do this.

                     Add (Child_Node, "height",
                          Gint'Image (Win.Get_Allocated_Height));
                     Add (Child_Node, "width",
                          Gint'Image (Win.Get_Allocated_Width));

                     --  Save the floating window's coordinates

                     Get_Root_Origin (Get_Window (Win), X, Y);

                     Add (Child_Node, "X", Gint'Image (X));
                     Add (Child_Node, "Y", Gint'Image (Y));
                  end;
               end if;

               if Child = MDI.Focus_Child then
                  Set_Attribute (Child_Node, "Focus", "True");
               end if;

               if Raised then
                  Set_Attribute (Child_Node, "Raised", "True");
               end if;

               if not Child.Get_Visible then
                  Set_Attribute (Child_Node, "visible", "False");
               end if;

               Add_Child (Parent, Child_Node, Append => True);
            end if;
         end Save_Widget;

         ---------------
         -- Save_Size --
         ---------------

         procedure Save_Size
           (Iter   : Gtkada.Multi_Paned.Child_Iterator;
            Node   : Node_Ptr)
         is
            Parent_Width, Parent_Height, Width, Height : Gint;
            Orientation : Gtk_Orientation;
         begin
            Get_Size (Iter, Width, Height, Parent_Width, Parent_Height,
                      Orientation);

            if Width < 0
              or else Height < 0
              or else Parent_Width < 0
              or else Parent_Height < 0
            then
               Has_Negative_Values := True;
            end if;

            case Orientation is
               when Orientation_Horizontal =>
                  Set_Attribute
                    (Node, "width",
                     Float'Image
                       (Float (Width) * 100.0 / Float (Parent_Width)) & "%");
               when Orientation_Vertical =>
                  Set_Attribute
                    (Node, "height",
                     Float'Image
                       (Float (Height) * 100.0 / Float (Parent_Height)) & "%");
            end case;
         end Save_Size;

         -------------------
         -- Save_Notebook --
         -------------------

         function Save_Notebook
           (Current    : Node_Ptr;
            Note       : MDI_Notebook;
            In_Central : Boolean) return Node_Ptr
         is
            Length                  : constant Gint := Get_N_Pages (Note);
            Current_Page            : constant Gint := Get_Current_Page (Note);
            Parent                  : Node_Ptr;
            Has_Default_Group_Child : Boolean := False;
            Child                   : MDI_Child;
         begin
            Parent := new Node;
            Parent.Tag := new String'("Notebook");
            Set_Attribute
              (Parent, "Tabs",
               Gtk_Position_Type'Image (Get_Tab_Pos (Note)));

            if Note.Tab_Orientation /= Automatic then
               Set_Attribute
                 (Parent, "orientation",
                  Tab_Orientation_Type'Image (Note.Tab_Orientation));
            end if;

            if Length > 0 then
               for Page_Index in 0 .. Length - 1 loop
                  Child := MDI_Child
                    (Get_Nth_Page (Note, Page_Index));
                  Has_Default_Group_Child := Has_Default_Group_Child
                    or else Child.Group = Group_Default;
                  Save_Widget
                    (Parent,
                     Child,
                     Raised     => Current_Page = Page_Index,
                     In_Central => In_Central);
               end loop;
            end if;

            --  Do not append the Notebook node to the parent if no child in
            --  the notebook was found, unless the number of pages is 0, in
            --  which case this is a real empty space which should be saved
            --  in the desktop. Also add the default notebook always, since
            --  it plays a special role

            Print_Debug
              ("Saving notebook, Length="
               & Gint'Image (Length)
               & " parent.child is null="
               & Boolean'Image (Parent.Child = null));

            if Length = 0
              or else Parent.Child /= null
            then
               Add_Child (Current, Parent, Append => True);
               return Parent;
            else
               Free (Parent);
               return null;
            end if;
         end Save_Notebook;

         -----------------
         -- Prune_Empty --
         -----------------

         procedure Prune_Empty (N : in out Node_Ptr) is
            C, Tmp : Node_Ptr;
         begin
            if N.Tag.all = "Pane" then
               C := N.Child;

               while C /= null loop
                  Tmp := C.Next;
                  Prune_Empty (C);
                  C := Tmp;
               end loop;

               if N.Child = null then
                  Free (N);
               end if;
            end if;
         end Prune_Empty;

         ----------------
         -- Save_Paned --
         ----------------

         procedure Save_Paned
           (Paned      : access Gtkada_Multi_Paned_Record'Class;
            Parent     : Node_Ptr;
            In_Central : Boolean)
         is
            Current    : Node_Ptr := Parent;
            N          : Node_Ptr;
            Depth      : Natural := 0;
            Iter       : Gtkada.Multi_Paned.Child_Iterator := Start (Paned);
            Orientation   : Gtk_Orientation;

         begin
            while not At_End (Iter) loop
               for N in Get_Depth (Iter) + 1 .. Depth loop
                  Current := Current.Parent;
               end loop;

               Orientation := Get_Orientation (Iter);

               if Get_Widget (Iter) = Gtk_Widget (MDI.Central) then
                  if MDI.Independent_Perspectives then
                     Save_Paned (MDI.Central, Current, In_Central => True);
                  else
                     N := new Node;
                     N.Tag := new String'("central");
                     Save_Size (Iter, N);
                     Add_Child (Current, N, Append => True);
                  end if;

               elsif Get_Widget (Iter) /= null then
                  N := Save_Notebook
                    (Current, MDI_Notebook (Get_Widget (Iter)),
                     In_Central => In_Central);
                  if N /= null then
                     Save_Size (Iter, N);
                  end if;

               else
                  N := new Node;
                  N.Tag := new String'("Pane");
                  Set_Attribute
                    (N, "Orientation", Gtk_Orientation'Image (Orientation));
                  Save_Size (Iter, N);
                  Add_Child (Current, N, Append => True);
                  Current := N;
               end if;

               Depth := Get_Depth (Iter);
               Next (Iter);
            end loop;

            if Parent.Child /= null then
               Prune_Empty (Parent.Child);
            end if;
         end Save_Paned;

         Old_Perspectives : Glib.Xml_Int.Node_Ptr;

      begin
         if MDI.Perspectives = null then
            MDI.Perspectives := new Node;
            MDI.Perspectives.Tag := new String'("perspectives");
         end if;

         if MDI.Current_Perspective /= null then
            if To_Lower (Get_Attribute
               (MDI.Current_Perspective, "save_on_exit", "True")) = "false"
            then
               return;
            end if;

            Old_Perspectives := Deep_Copy (MDI.Perspectives);

            --  Replace (in place) the perspective. This is so that the
            --  order in the /Window/Perspectives menu is preserved as much
            --  as possible

            declare
               N : Node_Ptr := MDI.Current_Perspective.Child;
               N2 : Node_Ptr;
            begin
               while N /= null loop
                  N2 := N.Next;
                  Free (N);
                  N := N2;
               end loop;
            end;

         else
            MDI.Current_Perspective := new Node;
            MDI.Current_Perspective.Tag := new String'("perspective");
            Set_Attribute (MDI.Current_Perspective, "name", "default");
            Add_Child
              (MDI.Perspectives, MDI.Current_Perspective, Append => False);
            Old_Perspectives := Deep_Copy (MDI.Perspectives);
         end if;

         Central := new Node;
         Central.Tag := new String'("desktop");

         --  Save the general configuration of the MDI

         declare
            Win   : constant Gtk_Window := Gtk_Window (Get_Toplevel (MDI));
         begin
            if Win /= null then
               if not Win.Is_Maximized and then Get_Window (Win) /= null then
                  Set_Attribute
                    (MDI.Perspectives, "width",
                     Gint'Image (MDI.Get_Toplevel.Get_Allocated_Width));
                  Set_Attribute
                    (MDI.Perspectives, "height",
                     Gint'Image (MDI.Get_Toplevel.Get_Allocated_Height));
               end if;

               Set_Attribute
                 (MDI.Perspectives, "mdi_width",
                  Gint'Image (MDI.Get_Allocated_Width));
               Set_Attribute
                 (MDI.Perspectives, "mdi_height",
                  Gint'Image (MDI.Get_Allocated_Height));

               --  We are only interested in whether the window is maximized
               if Win.Is_Maximized then
                  Set_Attribute (MDI.Perspectives, "state", " 4");
               else
                  Set_Attribute (MDI.Perspectives, "state", " 0");
               end if;

               Set_Attribute
                 (Central, "perspective", Current_Perspective (MDI));
            end if;
         end;

         Print_Debug ("Save_Desktop: window size reported as"
                      & Gint'Image (MDI.Get_Toplevel.Get_Allocated_Width) & "x"
                      & Gint'Image (MDI.Get_Toplevel.Get_Allocated_Height));

         Print_Debug ("Save_Desktop: saving the perspective");
         Save_Paned (MDI, MDI.Current_Perspective, In_Central => False);

         if not MDI.Independent_Perspectives then
            Print_Debug ("Save_Desktop: saving central area");
            Save_Paned (MDI.Central, Central, In_Central => True);
         end if;

         --  Save the floating widgets (these are part of the perspective)

         Print_Debug ("Save_Desktop: saving floating widgets");
         Item := MDI.Items;
         while Item /= Widget_List.Null_List loop
            Child := MDI_Child (Widget_List.Get_Data (Item));

            case Child.State is
               when Normal | Invisible  => null;
               when Floating =>
                  Save_Widget (Central, Child, False, In_Central => True);
            end case;

            Item := Widget_List.Next (Item);
         end loop;

         if Has_Negative_Values then
            --  In case of negative values, retrieve the old perspectives
            --  But keep the current perspective as the active one

            declare
               N    : Node_Ptr := Old_Perspectives.Child;
               Name : constant String := Get_Attribute
                 (MDI.Current_Perspective, "name");
            begin
               while N /= null loop
                  if Get_Attribute (N, "name") = Name then
                     MDI.Current_Perspective := N;
                     exit;
                  end if;
                  N := N.Next;
               end loop;
            end;

            Free (MDI.Perspectives);
            MDI.Perspectives := Old_Perspectives;

         else
            Free (Old_Perspectives);
         end if;

         Perspectives := Deep_Copy (MDI.Perspectives);

         if Traces then
            Print_Debug ("After saving the desktop (current perspective is "
                         & Current_Perspective (MDI) & "), desktop is");
            Print (MDI.Perspectives);

            if not MDI.Independent_Perspectives then
               Print_Debug ("And the central area is");
               Print (Central);
            end if;
         end if;
      end Save_Desktop;

      ---------------------------------------
      -- Free_Registered_Desktop_Functions --
      ---------------------------------------

      procedure Free_Registered_Desktop_Functions is
         procedure Unchecked_Free is new Ada.Unchecked_Deallocation
           (Register_Node_Record, Register_Node);
         Next : Register_Node;
      begin
         while Registers /= null loop
            Next := Registers.Next;
            Unchecked_Free (Registers);
            Registers := Next;
         end loop;
      end Free_Registered_Desktop_Functions;

      -------------------------------
      -- Internal_Load_Perspective --
      -------------------------------

      procedure Internal_Load_Perspective
        (MDI              : access MDI_Window_Record'Class;
         Name             : String;
         User             : User_Data;
         Focus_Child      : in out MDI_Child;
         To_Raise         : in out Gtk.Widget.Widget_List.Glist;
         To_Hide          : in out Gtk.Widget.Widget_List.Glist;
         MDI_Width, MDI_Height : Gint;
         Do_Size_Allocate : Boolean)
      is
         Alloc    : Gtk_Allocation;
         Child    : MDI_Child;

         procedure Remove_All_Items (Remove_All_Empty : Boolean);
         --  Remove all items from the MDI

         ----------------------
         -- Remove_All_Items --
         ----------------------

         procedure Remove_All_Items (Remove_All_Empty : Boolean) is
            Children    : Widget_List.Glist;
            L           : Widget_List.Glist;
            Note        : Gtk_Notebook;
            C           : MDI_Child;
            Parent      : Gtk_Widget;
         begin
            Print_Debug ("Remove_All_Items: remove_empty="
                         & Boolean'Image (Remove_All_Empty),
                         Debug_Increase);

            --  Remove all children from the MDI. However, we do not close them
            --  in case we switch back to the perspective (or the user opens
            --  them while in the perspective). They will just be marked as
            --  Invisible for now.

            L := MDI.Items;
            while L /= Null_List loop
               C := MDI_Child (Get_Data (L));

               if C.State = Normal
                 and then (MDI.Independent_Perspectives
                           or else not In_Central_Area (MDI, C))
               then
                  Print_Debug ("Remove_All_Items, marking "
                               & Get_Title (C) & " as invisible");
                  Ref (C);   --  Unref called in Destroy_Child and Put

                  Parent := Get_Parent (C);

                  if Parent /= null then
                     Remove (Gtk_Container (Get_Parent (C)), C);
                  end if;

                  Set_State (C, Invisible);
               end if;

               L := Next (L);
            end loop;

            --  We now force the closing of all empty notebooks
            --  The central area should no longer be part of the MDI at this
            --  stage, so all we get are notebooks

            Children := Get_Children (MDI);
            L := Children;
            while L /= Null_List loop
               Note := Gtk_Notebook (Get_Data (L));
               if Get_Nth_Page (Note, 0) = null then
                  Remove (MDI, Note);
               end if;
               L := Next (L);
            end loop;
            Free (Children);

            --  We used to close empty notebooks, but in fact such notebooks
            --  can now only be in the central area, whose contents has not
            --  changed anyway.

            Print_Debug ("", Debug_Decrease);
         end Remove_All_Items;

         Tmp_Persp : Node_Ptr;

      begin
         MDI.Freeze_Focus;

         --  Find the right perspective node

         Tmp_Persp := MDI.Perspectives.Child;
         while Tmp_Persp /= null
           and then Get_Attribute (Tmp_Persp, "name") /= Name
         loop
            Tmp_Persp := Tmp_Persp.Next;
         end loop;

         if Tmp_Persp = null then
            --  If not found, and we already have a perspective => do nothing

            if MDI.Current_Perspective /= null then
               return;
            end if;

            --  Else load the first one
            Print_Debug ("Perspective not found, loading default one");
            Tmp_Persp := MDI.Perspectives.Child;
         end if;

         MDI.Current_Perspective := Tmp_Persp;

         if Traces then
            Print_Debug ("Loading perspective " & Name,
                         Debug_Increase);
         end if;

         --  Remove central from the MDI, and it will be put in the new
         --  perspective

         Ref (MDI.Central);
         if Get_Parent (MDI.Central) /= null then
            Remove (Gtk_Container (Get_Parent (MDI.Central)), MDI.Central);
         end if;

         MDI.Loading_Desktop := True;
         Freeze (MDI);

         --  Clean up MDI if necessary

         Remove_All_Items (Remove_All_Empty => True);

         Restore_Multi_Pane
           (Pane                  => MDI,
            MDI                   => MDI,
            Focus_Child           => Focus_Child,
            To_Raise              => To_Raise,
            To_Hide               => To_Hide,
            User                  => User,
            Node                  => MDI.Current_Perspective,
            Full_Width            => MDI_Width,
            Full_Height           => MDI_Height);

         --  If the central area was not in the desktop, that's an error and
         --  the application will not be usable anyway, so better break the
         --  desktop but show the central area

         if not MDI.Independent_Perspectives
           and then Get_Parent (MDI.Central) = null
         then
            Print_Debug ("central not in desktop, force adding");
            Add_Child (MDI, MDI.Central);
         end if;

         MDI.Desktop_Was_Loaded := True;

         --  Raise all appropriate items at the end, so that even if some items
         --  are added temporarily to notebooks, they have no long-lasting
         --  impact on the notebook itself.
         declare
            Item : Widget_List.Glist := To_Raise;
         begin
            while Item /= Widget_List.Null_List loop
               Child := MDI_Child (Widget_List.Get_Data (Item));

               if Focus_Child = null then
                  Focus_Child := Child;
               end if;

               Print_Debug
                 ("Restore_Desktop, raising child with no focus "
                  & Get_Title (Child));
               Raise_Child (Child, Give_Focus => False);
               Item := Widget_List.Next (Item);
            end loop;
            Free (To_Raise);

            Print_Debug ("Restore_Desktop, hidding children");
            Item := To_Hide;
            while Item /= Widget_List.Null_List loop
               Child := MDI_Child (Widget_List.Get_Data (Item));
               Print_Debug
                 ("Restore_Desktop, hidding " & Get_Title (Child));
               Hide (Child);
               Item := Widget_List.Next (Item);
            end loop;
            Free (To_Hide);

            if Traces then
               Dump (MDI);
            end if;
         end;

         if not MDI.Independent_Perspectives then
            Realize (MDI.Central);
            Show_All (MDI.Central);
            Unref (MDI.Central);
         end if;

         Show_All (MDI);
         Reset_Title_Bars_And_Colors (MDI);

         MDI.Loading_Desktop := False;
         Thaw (MDI);
         Queue_Resize (MDI);

         --  Update to show which menu is active
         Update_Menu_Model_List_Of_Children (MDI);

         Emit_By_Name
           (Get_Object (MDI), String (Signal_Perspective_Changed) & ASCII.NUL);

         --  Realize the window while frozen, so that windows that insist on
         --  setting their own size when realized (eg. the search window in
         --  GPS) will not break the desktop.
         --  However, don't do this when attempting to maximize the desktop,
         --  since otherwise we get a first Size_Allocate for whatever current
         --  size we have, and then a second one for the maximized size. The
         --  first one breaks the desktop partially.

         if Do_Size_Allocate then
            Print_Debug ("Internal_Load_Perspective, forcing a Size_Allocate");
            Realize (MDI);
            MDI.Get_Allocation (Alloc);
            Size_Allocate (MDI, Alloc);
         end if;

         if Traces then
            Print_Debug ("Done loading perspective", Debug_Decrease);
         end if;

         MDI.Thaw_Focus;
      exception
         when others =>
            MDI.Thaw_Focus;
            raise;
      end Internal_Load_Perspective;

      ----------------------
      -- Load_Perspective --
      ----------------------

      procedure Load_Perspective
        (MDI          : access MDI_Window_Record'Class;
         Name         : String;
         User         : User_Data)
      is
         To_Raise     : Gtk.Widget.Widget_List.Glist;
         To_Hide      : Gtk.Widget.Widget_List.Glist;
         Focus_Child  : MDI_Child;
         Perspectives : Node_Ptr;
      begin
         --  Save modifications to current perspective, so that we can restore
         --  them when the user switches back to that perspective. A signal
         --  "perspective_changed" will be sent to the user, so that he has a
         --  chance to save the changes in an external file for persistency

         Print_Debug ("++++++ Load_Perspective " & Name);
         if MDI.Current_Perspective /= null then
            if Get_Attribute (MDI.Current_Perspective, "name") = Name then
               return;
            end if;

            --  We only rely on the side effect of changing MDI.Perspectives,
            --  since the central area does not change in any case
            Free (MDI.View_Contents);
            Save_Desktop (MDI, User, Perspectives, MDI.View_Contents);
            Free (Perspectives);
         end if;

         Print_Debug ("++++ Load_Perspective, desktop was saved, now loading");
         Internal_Load_Perspective
           (MDI, Name, User,
            Focus_Child      => Focus_Child,
            To_Raise         => To_Raise,
            To_Hide          => To_Hide,
            MDI_Width        => MDI.Get_Allocated_Width,
            MDI_Height       => MDI.Get_Allocated_Height,
            Do_Size_Allocate => False);

         Maximize_Action.Hide_When_Maximized (MDI);
      end Load_Perspective;

      ----------------------------------
      -- On_Action_Select_Perspective --
      ----------------------------------

      procedure On_Action_Select_Perspective
         (MDI       : access MDI_Window_Record'Class;
          Params    : GValues;
          User      : User_Data)
      is
         Length : aliased Gsize;
         Val    : constant Gvariant := From_Object (To_Address (Params, 1));
         Name   : constant String := Get_String (Val, Length'Access);
      begin
         if not MDI.Loading_Desktop then
            Print_Debug ("++++ Change_Perspective to " & Name);
            Load_Perspective (MDI, Name, User);
         end if;
      end On_Action_Select_Perspective;

      ----------------------------------
      -- On_Action_Create_Perspective --
      ----------------------------------

      procedure On_Action_Create_Perspective
         (MDI       : access MDI_Window_Record'Class;
          Params    : GValues;
          User      : User_Data)
      is
         pragma Unreferenced (Params);
      begin
         Create_Perspective_Query_Name (MDI, User);
      end On_Action_Create_Perspective;

      --------------------
      -- Set_Menu_Model --
      --------------------

      procedure Set_Menu_Model
        (MDI   : not null access MDI_Window_Record'Class;
         App   : not null access Gtk.Application.Gtk_Application_Record'Class;
         Model : access Glib.Menu.Gmenu_Record'Class;
         User  : User_Data)
      is
         Section : Gmenu;
         Act     : Gsimple_Action;
      begin
         if Gmenu (Model) /= MDI.Menu_Model
            and then MDI.Menu_Model /= null
         then
            Unref (MDI.Menu_Model);
            Unref (MDI.Menu_Items_Section);
            MDI.Menu_Model := null;
            MDI.Menu_Items_Section := null;
         end if;

         MDI.Menu_Model := Gmenu (Model);
         MDI.Application := Gtk_Application (App);

         Ref (MDI.Menu_Model);

         --  Create the actions if necessary (the first time)

         if MDI.Action_Close = null then
            G_New (Act, "mdi_split_h", null);
            Act.On_Activate (On_Action_Split_H'Access, MDI);
            App.Add_Action (+Act);

            G_New (Act, "mdi_split_v", null);
            Act.On_Activate (On_Action_Split_V'Access, MDI);
            App.Add_Action (+Act);

            G_New_Stateful
               (MDI.Action_Float, "mdi_floating",
                Parameter_Type => null,
                State          => Gvariant_New_Boolean (False));
            MDI.Action_Float.On_Activate (On_Action_Floating'Access, MDI);
            App.Add_Action (+MDI.Action_Float);

            G_New (MDI.Action_Close, "mdi_close", null);
            MDI.Action_Close.On_Activate (On_Action_Close'Access, MDI);
            App.Add_Action (+MDI.Action_Close);

            G_New_Stateful
               (MDI.Action_Select_Perspective, "mdi_set_perspective",
                Parameter_Type => Gvariant_Type_String,
                State          => Gvariant_New_String (""));
            MDI_User_Data_Cb.Object_Connect
               (MDI.Action_Select_Perspective,
                Glib.Simple_Action.Signal_Activate,
                On_Action_Select_Perspective'Access, MDI, User);
            App.Add_Action (+MDI.Action_Select_Perspective);

            G_New (Act, "mdi_create_perspective", null);
            MDI_User_Data_Cb.Object_Connect
               (Act, Glib.Simple_Action.Signal_Activate,
                On_Action_Create_Perspective'Access, MDI, User);
            App.Add_Action (+Act);

            G_New_Stateful
               (MDI.Action_Select, "mdi_select",
                Parameter_Type => Gvariant_Type_String,
                State          => Gvariant_New_String (""));
            MDI.Action_Select.On_Activate (On_Action_Select'Access, MDI);
            App.Add_Action (+MDI.Action_Select);
         end if;

         --  Create the menus

         if Model /= null then
            MDI.Menu_Model.Remove_All;

            Section := Gmenu_New;
            MDI.Menu_Model.Append_Section ("", Section);

            MDI.Perspectives_Menu := Gmenu_New;
            Section.Append_Submenu ("Perspectives", MDI.Perspectives_Menu);

            Section.Append ("Split Side-by-Side", "app.mdi_split_h");
            Section.Append ("Split Up-Down", "app.mdi_split_v");

            Section := Gmenu_New;
            MDI.Menu_Model.Append_Section ("", Section);

            Section.Append ("Floating", "app.mdi_floating");
            Section.Append ("Close", "app.mdi_close");

            MDI.Menu_Items_Section := Gmenu_New;
            MDI.Menu_Model.Append_Section ("", MDI.Menu_Items_Section);

            Update_Menu_Model_List_Of_Children (MDI);
         end if;
      end Set_Menu_Model;

   end Desktop;

   ----------------------------------------
   -- Update_Menu_Model_List_Of_Children --
   ----------------------------------------

   procedure Update_Menu_Model_List_Of_Children
      (MDI : not null access MDI_Window_Record'Class)
   is
      use Child_Vectors;
      Child   : MDI_Child;
      Vec     : Child_Vectors.Vector;
      Curs    : Child_Vectors.Cursor;
   begin
      if MDI.Menu_Items_Section /= null then
         MDI.Menu_Items_Section.Remove_All;

         Get_Sorted_List_Of_Visible_Children (MDI, Vec);

         Curs := First (Vec);
         while Has_Element (Curs) loop
            Child := Element (Curs);
            MDI.Menu_Items_Section.Append
               (Child.Get_Short_Title,
                "app.mdi_select(""" & Child.Get_Title & """)");
            Next (Curs);
         end loop;

         if MDI.Focus_Child = null then
            MDI.Action_Select.Set_State (Gvariant_New_String (""));
         else
            MDI.Action_Select.Set_State
               (Gvariant_New_String (MDI.Focus_Child.Get_Title));
         end if;
      end if;
   end Update_Menu_Model_List_Of_Children;

   -----------------
   -- First_Child --
   -----------------

   function First_Child
     (MDI               : access MDI_Window_Record;
      Group_By_Notebook : Boolean := False;
      Visible_Only      : Boolean := True) return Child_Iterator
   is
      Children : Widget_List.Glist;
      C        : MDI_Child;
   begin
      if Group_By_Notebook then
         declare
            Iter : Child_Iterator :=
              (Group_By_Notebook => True,
               Visible_Only      => Visible_Only,
               Paned_Iter        => Start (MDI),
               In_Central        => False,
               Notebook          => null,
               Notebook_Page     => 0,
               Floating_Iter     => MDI.Items,
               MDI               => MDI_Window (MDI));
         begin
            if MDI.Central /= null then
               Iter.Paned_Iter := Start (MDI.Central);
               Iter.In_Central := True;
            end if;

            Move_To_Next_Notebook (Iter);

            while Iter.Floating_Iter /= Null_List
              and then MDI_Child
                (Widget_List.Get_Data (Iter.Floating_Iter)).State /= Floating
            loop
               Iter.Floating_Iter := Widget_List.Next (Iter.Floating_Iter);
            end loop;

            return Iter;
         end;
      else
         Children := MDI.Items;

         if Children /= Widget_List.Null_List and then Visible_Only then
            C := MDI_Child (Widget_List.Get_Data (MDI.Items));
            if  C.State = Invisible then
               --  There are no visible child, since the first one should be
               --  the one with the focus
               Children := Widget_List.Null_List;
            end if;
         end if;

         return (Group_By_Notebook => False,
                 Visible_Only      => Visible_Only,
                 Iter              => Children);
      end if;
   end First_Child;

   ------------------
   -- Get_Notebook --
   ------------------

   function Get_Notebook
     (Iterator : Child_Iterator) return Gtk.Notebook.Gtk_Notebook
   is
      C : constant MDI_Child := Get (Iterator);
   begin
      if C = null then
         return null;
      else
         return Gtk_Notebook (Get_Notebook (C));
      end if;
   end Get_Notebook;

   ---------------------------
   -- Move_To_Next_Notebook --
   ---------------------------

   procedure Move_To_Next_Notebook (Iterator : in out Child_Iterator) is
   begin
      Iterator.Notebook := null;
      Iterator.Notebook_Page := 0;

      loop
         if At_End (Iterator.Paned_Iter) then
            if Iterator.In_Central then
               Iterator.In_Central := False;
               Iterator.Paned_Iter := Start (Iterator.MDI);
               exit when At_End (Iterator.Paned_Iter);
            else
               exit;
            end if;
         end if;

         --  Assert (not At_End (Iterator.Paned_Iter))

         if Get_Widget (Iterator.Paned_Iter) /= null
           and then
             (not Iterator.Visible_Only
              or else Get_Widget (Iterator.Paned_Iter).Get_Visible)
           and then Get_Widget (Iterator.Paned_Iter).all
             in Gtk_Notebook_Record'Class
         then
            Iterator.Notebook :=
              Gtk_Notebook (Get_Widget (Iterator.Paned_Iter));
            exit;
         end if;

         Next (Iterator.Paned_Iter);
      end loop;
   end Move_To_Next_Notebook;

   ----------
   -- Next --
   ----------

   procedure Next (Iterator : in out Child_Iterator) is
      C  : MDI_Child;
   begin
      if Iterator.Group_By_Notebook then
         if Iterator.Notebook = null then
            --  Find the next floating child
            loop
               Iterator.Floating_Iter :=
                 Widget_List.Next (Iterator.Floating_Iter);

               exit when Iterator.Floating_Iter = Null_List
                 or else MDI_Child
                   (Widget_List.Get_Data (Iterator.Floating_Iter)).State =
                 Floating;
            end loop;

         else
            Iterator.Notebook_Page := Iterator.Notebook_Page + 1;
            if Get_Nth_Page
              (Iterator.Notebook, Iterator.Notebook_Page) = null
            then
               Next (Iterator.Paned_Iter);
               Move_To_Next_Notebook (Iterator);
            end if;
         end if;

      else
         loop
            Iterator.Iter := Widget_List.Next (Iterator.Iter);
            if Iterator.Visible_Only then
               C := Get (Iterator);
               exit when C = null or else C.State /= Invisible;
            else
               exit;
            end if;
         end loop;
      end if;
   end Next;

   ---------
   -- Get --
   ---------

   function Get (Iterator : Child_Iterator) return MDI_Child is
   begin
      if Iterator.Group_By_Notebook then
         if Iterator.Notebook = null then
            if Iterator.Floating_Iter = Widget_List.Null_List then
               return null;
            else
               return MDI_Child
                 (Widget_List.Get_Data (Iterator.Floating_Iter));
            end if;

         else
            return MDI_Child
              (Get_Nth_Page (Iterator.Notebook, Iterator.Notebook_Page));
         end if;

      elsif Iterator.Iter /= Widget_List.Null_List then
         return MDI_Child (Widget_List.Get_Data (Iterator.Iter));
      else
         return null;
      end if;
   end Get;

   ---------------------
   -- Highlight_Child --
   ---------------------

   procedure Highlight_Child
     (Child : not null access MDI_Child_Record; Highlight : Boolean := True)
   is
      Note      : constant MDI_Notebook := Get_Notebook (Child);
   begin
      --  Do nothing if Child is not in a notebook, if it has no tab label, or
      --  if it already has the focus.
      if Note = null
        or else Child.Tab_Label = null
        or else (Highlight
                   and then Note.Get_Current_Page = Note.Page_Num (Child))
      then
         return;
      end if;

      if Highlight then
         Get_Style_Context (Child.Tab_Label).Add_Class
           ("mdi-highlighted-tab");
      else
         Get_Style_Context (Child.Tab_Label).Remove_Class
           ("mdi-highlighted-tab");
      end if;
   end Highlight_Child;

   ------------------------
   -- Desktop_Was_Loaded --
   ------------------------

   function Desktop_Was_Loaded (MDI : access MDI_Window_Record)
      return Boolean is
   begin
      return MDI.Desktop_Was_Loaded;
   end Desktop_Was_Loaded;

   ---------------
   -- Get_State --
   ---------------

   function Get_State
     (Child : not null access MDI_Child_Record) return State_Type is
   begin
      return Child.State;
   end Get_State;

   --------------------
   -- Set_Dnd_Source --
   --------------------

   procedure Set_Dnd_Source
     (Widget : access Gtk_Widget_Record'Class;
      Child  : access Gtk_Widget_Record'Class) is
   begin
      Add_Events (Widget, Button_Press_Mask);
      Widget.On_Button_Press_Event (Button_Pressed_Forced'Access, Child);
   end Set_Dnd_Source;

   ------------------------
   -- Draw_Dnd_Rectangle --
   ------------------------

   procedure Draw_Dnd_Rectangle
     (Child : access MDI_Child_Record'Class;
      Hide_Only : Boolean := False)
   is
      MDI : constant MDI_Window := Child.MDI;

      Current    : Gtk_Widget;
      Position   : Child_Position;
      New_Pos    : Gdk_Rectangle;
      Parent_Rect : Gdk_Rectangle;
      Message    : Unbounded_String;
      In_Central : Boolean;
      C3         : MDI_Child;
      Note       : Gtk_Notebook;
      Allowed    : Boolean;

      procedure Do_Draw (Cr : Cairo_Context; Draw : Boolean);
      procedure Do_Draw (Cr : Cairo_Context; Draw : Boolean) is
         Base_Color : Gdk_RGBA;
         Color : Gdk_RGBA;
         Layout : Pango_Layout;
         Ink_Rect, Logical_Rect : Pango_Rectangle;
         X, Y : Gdouble;
         W, H : Gdouble;
      begin
         if Draw then
            if not Allowed
              or else Current = null
            then
               --  A future floating child ? Nothing to draw for now, and there
               --  will be nothing to hide either.
               MDI.Dnd_Rectangle_Real.Width := 0;
               return;
            end if;

            if In_Central then
               Base_Color := MDI.Focus_Title_Color;
            else
               Base_Color := (Red   => 1.0 - MDI.Focus_Title_Color.Red,
                              Green => 1.0 - MDI.Focus_Title_Color.Green,
                              Blue  => 1.0 - MDI.Focus_Title_Color.Blue,
                              Alpha => 1.0);
            end if;

            Color := Base_Color;

            Color.Alpha := 0.2;
            Set_Source_RGBA (Cr, Color);
            Cairo.Rectangle
              (Cr,
               Gdouble (Parent_Rect.X),
               Gdouble (Parent_Rect.Y),
               Gdouble (Parent_Rect.Width),
               Gdouble (Parent_Rect.Height));
            Cairo.Fill (Cr);

            Color.Alpha := 0.6;
            Set_Source_RGBA (Cr, Color);

            MDI.Dnd_Rectangle := New_Pos;

            --  Offset here is to compensate the line width
            Gtkada.Style.Rounded_Rectangle
              (Cr,
               Gdouble (New_Pos.X) + 3.0,
               Gdouble (New_Pos.Y) + 3.0,
               Gdouble (New_Pos.Width) - 6.0,
               Gdouble (New_Pos.Height) - 6.0,
               Radius => 8.0);
            Cairo.Fill_Preserve (Cr);

            Color.Alpha := 0.9;
            Set_Source_RGBA (Cr, Color);
            Set_Line_Width (Cr, 3.0);
            Stroke (Cr);

            Layout := MDI.Create_Pango_Layout;
            Layout.Set_Markup
              (ASCII.HT & To_String (Message)
               & ASCII.LF & MDI.Dnd_Message.all);

            Layout.Get_Extents (Ink_Rect, Logical_Rect);
            W := Gdouble (Logical_Rect.Width / Pango_Scale);
            H := Gdouble (Logical_Rect.Height / Pango_Scale);

            X := Gdouble (MDI.Dnd_Rectangle.X) +
              (Gdouble (New_Pos.Width) - W) / 2.0;
            Y := Gdouble (MDI.Dnd_Rectangle.Y) +
              (Gdouble (New_Pos.Height) - H) / 2.0;

            --  Keep the text window within the MDI

            X := Gdouble'Max (X, 0.0);
            Y := Gdouble'Max (Y, 0.0);

            if X + W > Gdouble (MDI.Get_Allocated_Width) then
               X := Gdouble (MDI.Get_Allocated_Width) - W;
            end if;

            if Y + H > Gdouble (MDI.Get_Allocated_Height) then
               Y := Gdouble (MDI.Get_Allocated_Height) - H;
            end if;

            --  Slightly lighter (and non-transparent) background for the msg.
            Set_Line_Width (Cr, 1.0);
            Set_Source_RGBA
              (Cr, Gtkada.Style.Lighten (Base_Color, 0.3));
            Gtkada.Style.Rounded_Rectangle
              (Cr, X - 2.0, Y - 2.0, W + 4.0, H + 4.0, 4.0);
            Cairo.Fill (Cr);

            Get_Style_Context (MDI).Render_Layout (Cr, X, Y, Layout);
            Unref (Layout);

            MDI.Dnd_Rectangle_Real.X := Gint'Min (Parent_Rect.X, Gint (X) - 3);
            MDI.Dnd_Rectangle_Real.Y := Gint'Min (Parent_Rect.Y, Gint (Y) - 3);
            MDI.Dnd_Rectangle_Real.Width := Gint'Max
              (Parent_Rect.Width + Parent_Rect.X - MDI.Dnd_Rectangle_Real.X,
               Gint (W + X) + 4 - MDI.Dnd_Rectangle_Real.X);
            MDI.Dnd_Rectangle_Real.Height := Gint'Max
              (Parent_Rect.Height + Parent_Rect.Y - MDI.Dnd_Rectangle_Real.Y,
               Gint (H + Y) + 4 - MDI.Dnd_Rectangle_Real.Y);

         elsif MDI.Dnd_Rectangle_Real.Width /= 0 then
            Cairo.Rectangle
              (Cr,
               Gdouble (MDI.Dnd_Rectangle_Real.X),
               Gdouble (MDI.Dnd_Rectangle_Real.Y),
               Gdouble (MDI.Dnd_Rectangle_Real.Width),
               Gdouble (MDI.Dnd_Rectangle_Real.Height));
            Cairo.Fill (Cr);
         end if;
      end Do_Draw;

      Old_Dnd_Target : constant Gdk.Gdk_Window := MDI.Dnd_Target;

   begin
      Get_Dnd_Target (MDI              => Child.MDI,
                      Parent           => Current,
                      Position         => Position,
                      Parent_Rectangle => Parent_Rect,
                      Rectangle        => New_Pos,
                      Allowed          => Allowed,
                      In_Central       => In_Central);

      if Hide_Only then
         Allowed := False;
      end if;

      if not Allowed then
         Current := null;
         MDI.Dnd_Target := null;

      elsif Current = null then
         MDI.Dnd_Target := null;

      elsif Current = Gtk_Widget (MDI) then
         MDI.Dnd_Target := Get_Window (MDI);
         case Position is
            when Position_Bottom =>
               Message := To_Unbounded_String ("Below all other windows");
            when Position_Top =>
               Message := To_Unbounded_String ("Above all other windows");
            when Position_Left =>
               Message := To_Unbounded_String ("Left of all other windows");
            when Position_Right =>
               Message := To_Unbounded_String ("Right of all other windows");
            when others =>
               null;   --  Cannot occur
         end case;

      elsif Current = Gtk_Widget (MDI.Central) then
         MDI.Dnd_Target := Get_Window (MDI.Central);
         case Position is
            when Position_Bottom =>
               Message := To_Unbounded_String ("Below central area");
            when Position_Top =>
               Message := To_Unbounded_String ("Above central area");
            when Position_Left =>
               Message := To_Unbounded_String ("Left of central area");
            when Position_Right =>
               Message := To_Unbounded_String ("Right of central area");
            when others =>
               Message := To_Unbounded_String ("In central area");
               In_Central := True;
         end case;

      elsif Current = Get_Parent (Child)
        and then Position = Position_Automatic
      then
         MDI.Dnd_Target := Get_Window (Child);
         Message := To_Unbounded_String ("Leave at current position");

      else
         Note := Gtk_Notebook (Current);
         C3  := MDI_Child (Get_Nth_Page (Note, Get_Current_Page (Note)));

         if C3 = null then
            Message := To_Unbounded_String ("In central area");
            MDI.Dnd_Target := Get_Window (MDI.Central);

         else
            MDI.Dnd_Target := Get_Window (C3);

            case Position is
               when Position_Bottom =>
                  Message := To_Unbounded_String
                    ("Below <b>"
                     & Glib.Convert.Escape_Text (Get_Short_Title (C3))
                     & "</b>");
               when Position_Top =>
                  Message := To_Unbounded_String
                    ("Above <b>"
                     & Glib.Convert.Escape_Text (Get_Short_Title (C3))
                     & "</b>");
               when Position_Left =>
                  Message := To_Unbounded_String
                    ("Left of <b>"
                     & Glib.Convert.Escape_Text (Get_Short_Title (C3))
                     & "</b>");
               when Position_Right =>
                  Message := To_Unbounded_String
                    ("Right of <b>"
                     & Glib.Convert.Escape_Text (Get_Short_Title (C3))
                     & "</b>");
               when others =>
                  Message := To_Unbounded_String
                    ("On top of <b>"
                     & Glib.Convert.Escape_Text (Get_Short_Title (C3))
                     & "</b>");
            end case;
         end if;
      end if;

      --  Call this if the state has changed to hide the current highlight. We
      --  might, or not, highlight the target depending whether it is allowed
      --  for Child.

      if not (Position = MDI.Old_Dnd_Position
              and then MDI.Dnd_Target = Old_Dnd_Target)
      then
         Gtkada.Style.Draw_Overlay (MDI, MDI.Dnd_Overlay, Do_Draw'Access);
      end if;

      --  Update the drag state.

      MDI.Old_Dnd_Position := Position;

   end Draw_Dnd_Rectangle;

   ----------------------
   -- Child_Drag_Begin --
   ----------------------

   procedure Child_Drag_Begin
     (Child : not null access MDI_Child_Record'Class;
      Event : Gdk_Event_Button;
      Areas : Allowed_Areas)
   is
      Tmp  : Gdk_Grab_Status;
      Win  : Gdk.Gdk_Window;
      pragma Unreferenced (Tmp);
   begin
      --  Focus and raise the child. Raise_Child must be called explicitly
      --  since Set_Focus_Child won't do it if the child already has the focus.
      --  We have to raise the child, since otherwise the Pointer_Grab below
      --  will fail

      Print_Debug ("Child_Drag_Begin, focus and raise "
                   & Get_Title (Child));

      Set_Focus_Child (Child);
      Raise_Child (Child, False);

      Win := Get_Window (Child);

      --  If Child is floating, Win may be null at this point. In this case,
      --  do nothing.

      if Win /= null then
         --  Grab the pointer, so that we can detect whether the mouse moved
         --  far enough from its initial position to start a drag. This also
         --  ensures we get all the button_motion events

         Tmp := Pointer_Grab
           (Win,
            False,
            Button_Press_Mask or Button_Motion_Mask or Button_Release_Mask,
            Cursor => null,
            Time   => 0);

         Child.MDI.Drag_Start_X := Gint (Event.X_Root);
         Child.MDI.Drag_Start_Y := Gint (Event.Y_Root);
         Child.MDI.In_Drag := In_Pre_Drag;
         Child.MDI.Dnd_Rectangle := (0, 0, 0, 0);
         Child.MDI.Drag_Areas := Areas;

      else
         Print_Debug ("Child is floating, did not initiate DnD");
      end if;
   end Child_Drag_Begin;

   -----------------------
   -- Cancel_Child_Drag --
   -----------------------

   procedure Cancel_Child_Drag
     (Child : not null access MDI_Child_Record'Class) is
   begin
      Print_Debug ("Cancel_Child_Drag");

      Pointer_Ungrab;
      Child.MDI.In_Drag := No_Drag;
   end Cancel_Child_Drag;

   -------------------------
   -- Child_Drag_Finished --
   -------------------------

   procedure Child_Drag_Finished (Child : not null access MDI_Child_Record) is
      pragma Unreferenced (Child);
   begin
      null;
   end Child_Drag_Finished;

   --------------------
   -- Get_Dnd_Target --
   --------------------

   procedure Get_Dnd_Target
     (MDI              : not null access MDI_Window_Record'Class;
      Parent           : out Gtk_Widget;
      Position         : out Child_Position;
      Parent_Rectangle : out Gdk_Rectangle;
      Rectangle        : out Gdk_Rectangle;
      In_Central       : out Boolean;
      Allowed          : out Boolean)
   is
      Border_Width, Border_Height : Gint;
      Win                         : Gdk.Gdk_Window;
      Mask                        : Gdk_Modifier_Type;
      Current                     : Gtk_Widget;
      X, Y                        : Gint;
      Alloc                       : Gtk_Allocation;
   begin
      Gdk.Display.Get_Window_At_Pointer
         (Get_Display (Get_Window (MDI)), X, Y, Win);

      if Win = null then
         Position := Position_Automatic;
         Parent := null;
         Allowed := True;   --  floating

      else
         Current := Gtk_Widget (Get_User_Data (Win));

         while Current /= null
           and then Current /= Gtk_Widget (MDI)
           and then Current /= Gtk_Widget (MDI.Central)
           and then Get_Parent (Current) /= null
           and then
             (Current.all not in Gtk_Notebook_Record'Class
              or else
                 (Get_Parent (Current) /= Gtk_Widget (MDI)
                  and then Get_Parent (Current) /= Gtk_Widget (MDI.Central)))
         loop
            Current := Get_Parent (Current);
         end loop;

         --  If the cursor was put in a floating window, we should make the
         --  new child floating as well.
         if Current = null or else Get_Parent (Current) = null then
            Parent   := null;
            Position := Position_Automatic;
            Allowed  := True;
            return;
         end if;

         if Current = Gtk_Widget (MDI)
           and then MDI.Central /= null
         then
            Current := Gtk_Widget (MDI.Central);

            --  Central area not empty ? We have therefore passed the mouse on
            --  one of the handles, and should not allow a drop there

            if not At_End (Start (MDI.Central)) then
               Position := Position_Automatic;
               Parent   := null;
               Allowed  := True;
               return;
            end if;
         end if;

         Parent := Current;

         --  Are we on the sides of the MDI itself ?

         Rectangle :=
           (X      => 0,
            Y      => 0,
            Width  => Get_Allocated_Width (MDI),
            Height => Get_Allocated_Height (MDI));
         Parent_Rectangle := Rectangle;

         Gdk.Window.Get_Device_Position
            (Self   => Get_Window (MDI),
             Device => Gtk.Main.Get_Current_Event_Device,
             X      => X,
             Y      => Y,
             Mask   => Mask,
             Window => Win);

         if Y < Max_Drag_Border_Width / 2 then
            Position := Position_Top;
            Parent := Gtk_Widget (MDI);
            In_Central := False;
            Rectangle :=
              (X      => 0,
               Y      => 0,
               Width  => Rectangle.Width,
               Height => Max_Drag_Border_Width / 2);

         elsif Y > Rectangle.Height - Max_Drag_Border_Width / 2 then
            Position := Position_Bottom;
            Parent := Gtk_Widget (MDI);
            In_Central := False;
            Rectangle :=
              (X      => 0,
               Y      => Rectangle.Height - Max_Drag_Border_Width / 2,
               Width  => Rectangle.Width,
               Height => Max_Drag_Border_Width / 2);

         elsif X < Max_Drag_Border_Width / 2 then
            Position := Position_Left;
            Parent := Gtk_Widget (MDI);
            In_Central := False;
            Rectangle :=
              (X      => 0,
               Y      => 0,
               Width  => Max_Drag_Border_Width / 2,
               Height => Rectangle.Height);

         elsif X > Rectangle.Width - Max_Drag_Border_Width / 2 then
            Position := Position_Right;
            Parent := Gtk_Widget (MDI);
            In_Central := False;
            Rectangle :=
              (X      => Rectangle.Width - Max_Drag_Border_Width / 2,
               Y      => 0,
               Width  => Max_Drag_Border_Width / 2,
               Height => Rectangle.Height);

         else
            --  Are we on the sides of the current MDI child ?
            --  Parent is likey a Gtk_Notebook

            Gtkada.Style.Get_Offset
              (Parent, MDI, Rectangle.X, Rectangle.Y);
            Parent.Get_Allocation (Alloc);

            --  Compute device position relative to the MDI
            Gdk.Window.Get_Device_Position
               (Self   => Get_Window (MDI),
                Device => Gtk.Main.Get_Current_Event_Device,
                X      => X,
                Y      => Y,
                Mask   => Mask,
                Window => Win);

            --  size of the parent, and position relative to MDI
            Rectangle := (X      => Rectangle.X + Alloc.X,
                          Y      => Rectangle.Y + Alloc.Y,
                          Width  => Alloc.Width,
                          Height => Alloc.Height);
            Parent_Rectangle := Rectangle;

            In_Central := In_Central_Area (MDI, Parent);

            Border_Height := Gint'Min
              (Max_Drag_Border_Width, Rectangle.Height / 3);
            Border_Width :=
              Gint'Min (Max_Drag_Border_Width, Rectangle.Width / 3);

            if Y < Rectangle.Y + Border_Height then
               Position := Position_Top;
               Rectangle.Height := Border_Height;

            elsif Y > Rectangle.Y + Rectangle.Height - Border_Height then
               Position := Position_Bottom;
               Rectangle.Y := Rectangle.Y + Rectangle.Height - Border_Height;
               Rectangle.Height := Border_Height;

            elsif X < Rectangle.X + Border_Width then
               Position := Position_Left;
               Rectangle.Width := Border_Width;

            elsif X > Rectangle.X + Rectangle.Width - Border_Width then
               Position := Position_Right;
               Rectangle.X := Rectangle.X + Rectangle.Width - Border_Width;
               Rectangle.Width := Border_Width;

            else
               Position := Position_Automatic;
               Rectangle :=
                 (X      => Rectangle.X + Border_Width,
                  Y      => Rectangle.Y + Border_Height,
                  Width  => Rectangle.Width - 2 * Border_Width,
                  Height => Rectangle.Height - 2 * Border_Height);
            end if;
         end if;

         Allowed :=
           MDI.Drag_Areas = Both
           or else (MDI.Drag_Areas = Central_Only and then In_Central)
           or else (MDI.Drag_Areas = Sides_Only and then not In_Central);
      end if;
   end Get_Dnd_Target;

   --------------------------
   -- List_Of_Perspectives --
   --------------------------

   function List_Of_Perspectives
     (MDI : access MDI_Window_Record)
      return GNAT.Strings.String_List_Access
   is
   begin
      return MDI.Perspective_Names;
   end List_Of_Perspectives;

   -------------------------
   -- Current_Perspective --
   -------------------------

   function Current_Perspective
     (MDI : access MDI_Window_Record'Class) return String is
   begin
      if MDI.Current_Perspective = null then
         return "";
      else
         return Get_Attribute (MDI.Current_Perspective, "name", "");
      end if;
   end Current_Perspective;

   ------------------
   -- Freeze_Focus --
   ------------------

   procedure Freeze_Focus (MDI : access MDI_Window_Record'Class) is
   begin
      MDI.Focus_Freeze := MDI.Focus_Freeze + 1;
   end Freeze_Focus;

   ----------------
   -- Thaw_Focus --
   ----------------

   procedure Thaw_Focus (MDI : access MDI_Window_Record'Class) is
   begin
      if MDI.Focus_Freeze = 0 then
         if Traces then
            Print_Debug ("Calls to (Freeze|Thaw)_Focus do not match");
         end if;
         return;
      end if;

      MDI.Focus_Freeze := MDI.Focus_Freeze - 1;
      if MDI.Focus_Freeze = 0
        and then MDI.Focus_Child /= null
      then
         MDI.Set_Focus_Child (MDI.Focus_Child);
      end if;
   end Thaw_Focus;

   -------------
   -- Connect --
   -------------

   procedure Connect
     (Object  : access MDI_Window_Record'Class;
      C_Name  : Glib.Signal_Name;
      Handler : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After   : Boolean) is
   begin
      Unchecked_Do_Signal_Connect
        (Object      => Object,
         C_Name      => C_Name,
         Marshaller  => Marsh_MDI_Window_MDI_Child_Void'Access,
         Handler     => Cb_To_Address (Handler), --  Set in the closure
         After       => After);
   end Connect;

   ------------------
   -- Connect_Slot --
   ------------------

   procedure Connect_Slot
     (Object  : access MDI_Window_Record'Class;
      C_Name  : Glib.Signal_Name;
      Handler : Cb_GObject_MDI_Child_Void;
      After   : Boolean;
      Slot    : access Glib.Object.GObject_Record'Class := null) is
   begin
      Unchecked_Do_Signal_Connect
        (Object      => Object,
         C_Name      => C_Name,
         Marshaller  => Marsh_GObject_MDI_Child_Void'Access,
         Handler     => Cb_To_Address (Handler), --  Set in the closure
         Slot_Object => Slot,
         After       => After);
   end Connect_Slot;

   ----------------------------------
   -- Marsh_GObject_MDI_Child_Void --
   ----------------------------------

   procedure Marsh_GObject_MDI_Child_Void
     (Closure         : GClosure;
      Return_Value    : Glib.Values.GValue;
      N_Params        : Glib.Guint;
      Params          : Glib.Values.C_GValues;
      Invocation_Hint : System.Address;
      User_Data       : System.Address)
   is
      pragma Unreferenced (Return_Value, N_Params, Invocation_Hint, User_Data);
      H   : constant Cb_GObject_MDI_Child_Void :=
        Address_To_Cb (Get_Callback (Closure));
      Obj : constant Glib.Object.GObject :=
        Glib.Object.Convert (Get_Data (Closure));
   begin
      H (Obj, MDI_Child (Unchecked_To_Object (Params, 1)));
   exception when E : others => Process_Exception (E);
   end Marsh_GObject_MDI_Child_Void;

   -------------------------------------
   -- Marsh_MDI_Window_MDI_Child_Void --
   -------------------------------------

   procedure Marsh_MDI_Window_MDI_Child_Void
     (Closure         : GClosure;
      Return_Value    : Glib.Values.GValue;
      N_Params        : Glib.Guint;
      Params          : Glib.Values.C_GValues;
      Invocation_Hint : System.Address;
      User_Data       : System.Address)
   is
      pragma Unreferenced (Return_Value, N_Params, Invocation_Hint, User_Data);
      H   : constant Cb_Gtkada_MDI_Window_MDI_Child_Void :=
        Address_To_Cb (Get_Callback (Closure));
      Obj : constant MDI_Window :=
        MDI_Window (Unchecked_To_Object (Params, 0));
   begin
      H (Obj, MDI_Child (Unchecked_To_Object (Params, 1)));
   exception when E : others => Process_Exception (E);
   end Marsh_MDI_Window_MDI_Child_Void;

   -----------------------
   -- On_Child_Selected --
   -----------------------

   procedure On_Child_Selected
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Child_Selected & ASCII.NUL, Call, After);
   end On_Child_Selected;

   -----------------------
   -- On_Child_Selected --
   -----------------------

   procedure On_Child_Selected
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Child_Selected & ASCII.NUL, Call, After, Slot);
   end On_Child_Selected;

   --------------------
   -- On_Float_Child --
   --------------------

   procedure On_Float_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Float_Child & ASCII.NUL, Call, After);
   end On_Float_Child;

   --------------------
   -- On_Float_Child --
   --------------------

   procedure On_Float_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Float_Child & ASCII.NUL, Call, After, Slot);
   end On_Float_Child;

   ----------------------------
   -- On_Child_Title_Changed --
   ----------------------------

   procedure On_Child_Title_Changed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Child_Title_Changed & ASCII.NUL, Call, After);
   end On_Child_Title_Changed;

   ----------------------------
   -- On_Child_Title_Changed --
   ----------------------------

   procedure On_Child_Title_Changed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Child_Title_Changed & ASCII.NUL, Call, After, Slot);
   end On_Child_Title_Changed;

   --------------------
   -- On_Child_Added --
   --------------------

   procedure On_Child_Added
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Child_Added & ASCII.NUL, Call, After);
   end On_Child_Added;

   --------------------
   -- On_Child_Added --
   --------------------

   procedure On_Child_Added
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Child_Added & ASCII.NUL, Call, After, Slot);
   end On_Child_Added;

   ----------------------
   -- On_Child_Removed --
   ----------------------

   procedure On_Child_Removed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Child_Removed & ASCII.NUL, Call, After);
   end On_Child_Removed;

   ----------------------
   -- On_Child_Removed --
   ----------------------

   procedure On_Child_Removed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Child_Removed & ASCII.NUL, Call, After, Slot);
   end On_Child_Removed;

   ---------------------------
   -- On_Child_Icon_Changed --
   ---------------------------

   procedure On_Child_Icon_Changed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Child_Icon_Changed & ASCII.NUL, Call, After);
   end On_Child_Icon_Changed;

   ---------------------------
   -- On_Child_Icon_Changed --
   ---------------------------

   procedure On_Child_Icon_Changed
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Child_Icon_Changed & ASCII.NUL, Call, After, Slot);
   end On_Child_Icon_Changed;

   ---------------------
   -- On_Delete_Event --
   ---------------------

   procedure On_Delete_Event
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Delete_Event & ASCII.NUL, Call, After);
   end On_Delete_Event;

   ---------------------
   -- On_Delete_Event --
   ---------------------

   procedure On_Delete_Event
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Delete_Event & ASCII.NUL, Call, After, Slot);
   end On_Delete_Event;

   ----------------------
   -- On_Unfloat_Child --
   ----------------------

   procedure On_Unfloat_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Unfloat_Child & ASCII.NUL, Call, After);
   end On_Unfloat_Child;

   ----------------------
   -- On_Unfloat_Child --
   ----------------------

   procedure On_Unfloat_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Unfloat_Child & ASCII.NUL, Call, After, Slot);
   end On_Unfloat_Child;

   -----------------------------
   -- On_Before_Unfloat_Child --
   -----------------------------

   procedure On_Before_Unfloat_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Before_Unfloat_Child & ASCII.NUL, Call, After);
   end On_Before_Unfloat_Child;

   -----------------------------
   -- On_Before_Unfloat_Child --
   -----------------------------

   procedure On_Before_Unfloat_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Before_Unfloat_Child & ASCII.NUL, Call, After, Slot);
   end On_Before_Unfloat_Child;

   -----------------------------
   -- On_Before_Destroy_Child --
   -----------------------------

   procedure On_Before_Destroy_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Before_Destroy_Child & ASCII.NUL, Call, After);
   end On_Before_Destroy_Child;

   -----------------------------
   -- On_Before_Unfloat_Child --
   -----------------------------

   procedure On_Before_Destroy_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Before_Destroy_Child & ASCII.NUL, Call, After, Slot);
   end On_Before_Destroy_Child;

   ----------------------------
   -- On_Before_Remove_Child --
   ----------------------------

   procedure On_Before_Remove_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_Gtkada_MDI_Window_MDI_Child_Void;
      After : Boolean := False) is
   begin
      Connect (Self, Signal_Before_Remove_Child & ASCII.NUL, Call, After);
   end On_Before_Remove_Child;

   -----------------------------
   -- On_Before_Unfloat_Child --
   -----------------------------

   procedure On_Before_Remove_Child
     (Self  : not null access MDI_Window_Record'Class;
      Call  : Cb_GObject_MDI_Child_Void;
      Slot  : not null access Glib.Object.GObject_Record'Class;
      After : Boolean := False) is
   begin
      Connect_Slot
        (Self, Signal_Before_Remove_Child & ASCII.NUL, Call, After, Slot);
   end On_Before_Remove_Child;
end Gtkada.MDI;