package bogue

  1. Overview
  2. Docs

Source file b_layout.ml

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
(* This file is part of BOGUE, by San Vu Ngoc *)

(* Layout is the main object type. *)

(* a layout is a 'box' which can contain 'sub-boxes'. We use the
   terminology of houses: a house contains several rooms. Each room
   can be viewed as a house which contains other rooms etc. Thus, this
   is a simple graph with variable degree. A leaf (a room which does
   not contain subrooms) is called a resident; it contains a
   Widget. In the whole (connected) tree, the summit is the main
   layout: the only one which does not belong to any house; it is
   called the top_house, and corresponds to a "physical" SDL window.
   The size of the SDL window should always match the size of the
   top_house. *)

(* Warning: a widget should *not* appear twice (or more) inside a
   Layout. Otherwise, the results are not going to be satisfactory: a
   widget is associated to a geometry in a layout. Instead one should
   use two differents widgets with a connection between them to
   synchronize the data. *)


open Tsdl
open B_utils
module Avar = B_avar
module Box = B_box
module Chain = B_chain
module Draw = B_draw
module Label = B_label
module Mouse = B_mouse
module Selection = B_selection
module Slider = B_slider
module Style = B_style
module Sync = B_sync
module Theme = B_theme
module Time = B_time
module Trigger = B_trigger
module Tvar = B_tvar
module Var = B_var
module Widget = B_widget

type background =
  (* TODO instead we should keep track of how the box was created... in case we
     want to recreate (eg. when the window is scaled, or use it for another
     window...) *)
  | Style of Style.t
  | Box of Box.t

let color_bg color =
  Style (Style.(of_bg (color_bg color)))

let opaque_bg color = color_bg Draw.(opaque color)

let theme_bg = opaque_bg Draw.bg_color

let style_bg s =
  Style s

let box_bg b =
  Box b

type adjust =
  | Fit
  | Width
  | Height
  | Nothing

type transform = {
    angle : float Avar.t;
    center : (Sdl.point option) Avar.t;
    flip : Sdl.flip Avar.t;
    alpha : float Avar.t
  }

type geometry = {
    x : int Avar.t;
    y : int Avar.t;
    (* The (x,y) coords define the position of the layout wrt its
       container (the house). Origin is top-left. *)
    w : int Avar.t;
    h : int Avar.t;
    voffset : (int Avar.t) Var.t;
    (* The [voffset] is the vertical offset = the y value of the new origin from
       where the content of the layout will be drawn. It is typically used for
       scrolling, and then y <= 0. It is similar to the 'y' variable', except
       that:

       1. the clipping rect (if defined) is *not* translated in case of voffset
       2. the background is not translated either *)
    transform: transform;
  }

type current_geom = {
    x : int;
    y : int;
    w : int;
    h : int;
    voffset : int
  }

(* convert between same type in Draw... *)
let to_draw_geom (g : current_geom) =
  { Draw.x = g.x; Draw.y = g.y; Draw.w = g.w; Draw.h = g.h;
    Draw.voffset = g.voffset }

type room_content =
  | Rooms of room list
  (* In principle, rooms in a house with the same layer should have
     non-intersecting geometries, otherwise it is not clear which one gets the
     mouse focus (this can be violated, eg. with Layout.superpose). Popups are
     drawn on a different layer. *)
  | Resident of Widget.t

and room = {
  id : int; (* unique identifier. *)
  name : string option;
  (* If needed for debugging, one can give a name to the room. *)
  lock : Mutex.t;
  (* Lock for concurrent access by several threads. *)
  mutable thread_id : int;
  (* Id of the thread holding the lock. TODO use Var.t instead. *)
  adjust : adjust;
  (* should we adjust the size of this room to fit its content? *)
  (* not implemented yet *)
  mutable resize : ((int * int) -> unit);
  (* The [resize] function is called when the *house* of the current room
     changed size. (int * int) is the new house size (w,h). This function is
     responsible for effectively changing the size and relative position of the
     current room (except for the top house = window where this field has no
     effect). The field is usually set by the house of the room, (when the room
     is inserted in a house), not at the creation of the room itself. *)
  mutable show : bool;
  (* Should we show this room? Warning: when modifying this, it is highly
     advisable to modify also all inhabitants of the room (recursively); use
     [rec_set_show]. *)
  mutable hidden : bool;
  (* The [hidden] field is only useful when [t.show = true]. Then [t.hidden =
     true] if the layout is currently not displayed onscreen. (Upon creation,
     all layouts are hidden.)  Only used to correctly detect if animations are
     running. This field is only set by the Layout.display function, it should
     not be modified by user.  Note that t.show has precedence for being
     hidden: it [t.show = false], then t is hidden no matter what [t.hidden]
     says. *)
  mutable geometry : geometry;
  (* [geometry] contains the relative geometry of the room wrt the house. All
     components are dynamic variables, that need to be recomputed at each
     iteration. Note: rooms inside a house must be physically inside the
     geometry of the house. If not, they will not be detected by the mouse,
     for instance. *)
  mutable current_geom : current_geom;
  (* [current_geom] is the current *absolute* geometry (the y value does not
     include the voffset, the latter is part of current_geom). Is updated at
     each display. But because of clip, the actual rendered size can be smaller
     than indicated size. Before the start of the main loop, it is equal to the
     initial values of the geometry field. *)
  (* A special case of current_geom.(x,y) is to specify window position for
     the top layouts. See set_window_pos. *)
  mutable clip : bool;
  (* If [clip]=true, the room (and its children) will be clipped inside its
     geometry. This should be set whenever one want to scroll the content of
     the layout inside the layout. This is also used (and set) by hide/show
     animations. TODO replace this by a more flexible 'overflow'
     specification.  *)
  mutable background : background option;
  mutable shadow : Style.shadow option;
  mask : Sdl.surface option;
  (* If there is a mask, a position (x,y) will be declared inside the layout
     if it corresponds to a mask pixel with alpha value <> 0. A mask will act
     as a clip if it is uniformly white, and the shape is given by nonzero
     alpha values. (TODO) *)
  mutable content : room_content;
  mutable layer : Draw.layer;
  (* [layer] is the particular layer = chain element of this layout. It should
     never be an empty layer (Chain.None), except for the special layout that
     contains all windows. If a room contains other Rooms, its layer should be
     at least as deep as the layers of the Rooms, otherwise the "background"
     might end-up not being at the background... *)
  (* In principle a chain of layers is attached to a window. When creating a
     new window, one has to select a new layer chain (use_new_layer). *)
  mutable canvas : Draw.canvas option;
  (* The canvas contains the "hardware" information to render the room *)
  (* The canvas is not really an intrinsic property of the layout, it is used
     only when rendering is required. It may change "without notice" when a
     layout is copied into another window. It is first initialized by
     [make_window]. *)
  mutable house: room option;
  (* [house] = parent: this is the "room" that contains this room in his
     "Rooms". This field is mutable because of cyclic definition: one cannot
     set the house before defining it... It is our responsibility to make sure
     that the house really corresponds to the parent element, in order to
     avoid cycles etc. *)
  (* cache : Sdlvideo.surface; *) (* ou texture? mettre un cache pour
                                     accélerer l'affichage, plutôt que
                                     d'effacer tout à chaque itération ? *)
  mutable mouse_focus : bool; (* set interactively when has mouse focus *)
  mutable keyboard_focus : bool option;
  (* None = cannot have focus; Some b = has focus or not *)
  (* TODO: should we move the keyboard_focus to the Widget? A layout which
     contains a Rooms list cannot really have keyboard_focus...and in fact it
     will not be detected by 'next_keyboard' *)
  (* TODO : mutable draggable : int option; *) (* None = not draggable; Some
                                                  delay = drag after delay (in ms) *)
  mutable draggable : bool;
  (* TODO keep_focus_on_pressed: bool (default = true) CF. menu2. BUT It's not
     so easy because many layouts can cover a widget. Ideally, this property
     should belong to the widget. *)
  mutable removed : bool;
  (* [removed] is an experimental field: hint that the layout should not be
     used anymore by the board, at least temporarily. Maybe show/hidden could
     be used instead. Currently redundant with [disabled].  *)
  mutable disabled : bool;
  (* a disabled layout is still displayed but should not accept any focus *)
}

type t = room
(* The whole connected component of a layout is a tree, whose vertices (nodes)
   are rooms and leaves are widgets (=Resident). The number of branches (=Rooms
   list) from a vertex is arbitrary. The house field gives the parent of a
   vertex.

   There are several interesting ways of going through a tree:
   - through every vertex
   - only through leaves
   - only leaves at a common level (=same generation number)
   - nearest neighbour (left, right, up, or down) in the planar embedding
*)

(* We use words "room", "layout", and "house" for the same type of object.

   - "layout" will in general refer to the main house, ie containing everything
     that is displayed on the window.
   - "house" in general refers to a parent of some room, ie an object contaning
     sub-rooms.
   - "room" is the generic term for sub objects contained in the general layout.
 *)

exception Fatal_error of (t * string)
exception Found of t

(* [not_specified] is a special value used to indicate that the window position
   should be guessed by the program. *)
let not_specified = Sdl.Window.pos_undefined

let no_clip = ref false
(* The normal behaviour when a non-zero voffset is specified is to clip the
   layout to the original rectangle. This permits the show/hide
   animation. Setting [no_clip = true] can be a good idea for debugging
   graphics. *)

let draw_boxes = Widget.draw_boxes
(* this is only used for debugging. This can slow down rendering quite a bit *)

let equal r1 r2 = r1.id = r2.id
let (==) = equal

let sprint_id r =
  Printf.sprintf "#%u%s" r.id (match r.name with
      | None -> ""
      | Some s -> Printf.sprintf " (%s)" s)

module Hash = struct
  type t = room
  let equal = equal
  let hash room = room.id
end

module WHash = Weak.Make(Hash)

(* [rooms_wtable] this is a weak set of all created rooms, searchable by their
   unique id. It is weak in the sense that rooms can be reclaimed by the GC when
   not anymore in use, and automatically disappear from the set. *)
let rooms_wtable = WHash.create 50


(* [cemetery] is only for debugging: we insert here the room ids we think are
   not used anymore. Then we can check if the GC did remove them from the
   [rooms_wtable]. *)
let cemetery = ref []
let send_to_cemetery room =
  cemetery := room.id :: !cemetery

let rec remove_wtable room =
  if WHash.mem rooms_wtable room
  then begin
      printd debug_memory "Removing room %s from Wtable" (sprint_id room);
      WHash.remove rooms_wtable room;
      if WHash.mem rooms_wtable room
      then begin
          printd debug_error
            "Several instances of room %s are registered in the weak hash table."
            (sprint_id room);
          remove_wtable room;
        (* The hash can host several instances of the room. However this signals
           a bug somewhere. *)
        end;
      send_to_cemetery room;
    end

let clear_wtable () = WHash.clear rooms_wtable

(* let rooms_table : (int, room) Hashtbl.t = Hashtbl.create 50;;*)
(* this is where we store the reverse lookup: room.id ==> room *)
(* of course the problem is to free this when rooms are not used anymore... to
   prevent a memory leak. *)
(* TODO use weak tables (or Ephemerons???) *)
(* https://caml.inria.fr/pub/docs/manual-ocaml/libref/Weak.html *)

(* let of_id id = *)
(*   try Hashtbl.find rooms_table id with *)
(*   | Not_found -> failwith (Printf.sprintf "Cannot find room with id=%d" id);; *)


(* if !debug is true, we replace the background by solid red *)
let delete_background room =
  printd debug_memory "Delete background for room %s" (sprint_id room);
  do_option room.background
    (fun b ->
      let () =
        room.background <-
          if !debug then Some (opaque_bg Draw.red) else None in
      match b with
      | Style s -> Style.unload s
      | Box b -> Box.unload b)

(* this can be used to force recreating the background, for instance after
   changing the size of the room *)
let unload_background room =
  do_option room.background (function
      | Box b -> Box.unload b
      | Style s -> Style.unload s) (* maybe not necessary *)

(* WARNING: in "iter" and in all other search functions below, recall that
   itering though a room is tricky because of mutability and threading. The
   structure of the tree can be changed by another thread while we iter. Most
   dangerous: it can also be changed by the itering itself, hehe. If necessary,
   doing "iter lock room" should minimize the risk (but not 100%: the tree can
   still be modified while we are locking..) *)

(* Iter through all the layouts (rooms & residents) contained in the [room],
   including the initial [room] itself. *)
(* top to bottom *)
let rec iter f room =
  f room;
  match room.content with
  | Resident _ -> ()
  | Rooms list -> List.iter (iter f) list

(* iter through widgets *)
let rec iter_widgets f room =
  match room.content with
  | Resident w -> f w
  | Rooms list -> List.iter (iter_widgets f) list

let map_widgets f room =
  let list = ref [] in
  iter_widgets (fun w -> list := (f w) :: !list) room;
  !list

(* iter the direct children *)
let iter_rooms f house =
  match house.content with
  | Resident _ -> printd (debug_error + debug_board)
                    "Layout %s has no rooms: cannot iter." (sprint_id house)
  | Rooms list -> List.iter f list

(* returns the list of rooms of the layout, or Not_found if there is a
   resident *)
let get_rooms layout =
  match layout.content with
  | Resident _ ->
    printd debug_error
      "[Layout.get_rooms] This layout %s is a leaf, not a node: it does not \
       contain a list of rooms" (sprint_id layout);
    raise Not_found
  | Rooms list -> list

(* all rooms in the same house as room (including itself) *)
let neighbours room =
  match room.house with
  | None ->
     printd debug_error
       "Cannot get siblings of room %s because it does not belong to any \
        house." (sprint_id room);
     []
  | Some house -> get_rooms house

let siblings = neighbours

let rec belongs_to ~parent room =
  match room.house with
  | None -> false
  | Some h -> equal h parent || belongs_to ~parent h

(* return the resident widget, or Not_found *)
let widget layout =
  match layout.content with
  | Rooms _ ->
     printd debug_error
       "This room %s is a node, not a leaf: \
        it does not contain a resident widget" (sprint_id layout);
     raise Not_found
  (* or, return the first available widget with next_widget? *)
  | Resident w -> w

let get_resident = widget

(* return the first resident widget with show=true inside the layout, or
   Not_found *)
let rec first_show_widget layout =
  if layout.show
  then match layout.content with
       | Resident w ->
          (printd debug_board "first_show_widget selects %u" (Widget.id w); w)
       | Rooms rooms ->
          let rec loop = function
            | [] -> raise Not_found
            | r::rest -> try first_show_widget r with Not_found -> loop rest in
          loop rooms
  else raise Not_found

(* Use this to reset all widget textures (room + all children) for reducing
   memory. The layout can still be used without any impact, the textures will be
   recreated on the fly. If you want to really remove all created textures, you
   have to use delete_backgrounds too; but then the backgrounds will *not* be
   recreated. *)
let unload_widget_textures room =
  unload_background room;
  iter_widgets Widget.unload_texture room


let unload_texture room =
  match room.content with
  | Resident w -> Widget.unload_texture w
  | _ -> ()

(* Same as [unload_widget_textures], but for all rooms + widgets. *)
let iter_unload_textures room =
  let f r =
    unload_texture r;
    unload_background r in
  iter f room

let delete_backgrounds room =
  iter delete_background room

let delete_textures room =
  iter_unload_textures room;
  delete_backgrounds room

(* As finalize is called on *each* dangling room, we should not remove texture
   recursively from the contained Rooms. *)
let finalize room =
  printd debug_memory "Finalize room %s" (sprint_id room);
  delete_background room;
  unload_texture room

(* Return the list of all texts contained in the widgets *)
let get_texts room =
  map_widgets Widget.get_text room
  |> List.filter (fun s -> s <> "")

let get_text room =
  get_resident room
  |> Widget.get_text

let set_text room text =
  Widget.set_text (get_resident room) text

(* Pressing the TAB key in the main loop will switch the keyboard focus to
   another room. Here we save the room that had keyboard focus just before
   pressing TAB. This global variable should be thread safe because it is
   modified only by the main loop. Another option could be to store the room_id
   in an event. (?) *)
let keyboard_focus_before_tab : t option ref = ref None

let fresh_id = fresh_int ()

(** make geometry *)
let geometry ?(x=0) ?(y=0) ?(w=0) ?(h=0) ?(voffset=0) ?transform () : geometry =
  { x = Avar.var x;
    y = Avar.var y;
    w = Avar.var w;
    h = Avar.var h;
    voffset = Var.create (Avar.var voffset);
    transform = default transform
        { angle = Avar.var 0.;
          center = Avar.var None;
          flip = Avar.var Sdl.Flip.none;
          alpha = Avar.var 1.}
  }

(** list of all integer dynamical variables *)
let get_int_avars room =
let g = room.geometry in [g.x; g.y; g.w; g.h; Var.get g.voffset]

let current_geom ?(x=0) ?(y=0) ?(w=0) ?(h=0) ?(voffset=0) () : current_geom =
  { x; y; w; h; voffset}

(* Transform geometry into current_geom *)
let to_current_geom (g : geometry) : current_geom =
  { x = Avar.get g.x;
    y = Avar.get g.y;
    w = Avar.get g.w;
    h = Avar.get g.h;
    voffset = Avar.get (Var.get g.voffset) }

(* Get current layer of layout *)
let get_layer l =
  l.layer

(* [base_layer rooms] returns the deepest layer of the list of rooms, or the
   current layer if the list is empty. *)
let base_layer = function
  | [] -> Draw.get_current_layer ()
  | room::rooms ->
     List.fold_left Chain.min (get_layer room) (List.map get_layer rooms)

(* Create a new room. Rather use the [create] function below. *)
let create_unsafe
    ?name
    ?(set_house = true) ?(adjust = Fit)
    ?(resize = fun _ -> ())
    ?layer
    ?mask ?background ?shadow ?house ?keyboard_focus ?(mouse_focus=false)
    ?(show = true) ?(clip = false) ?(draggable = false) ?canvas
    geometry content =
  let id = fresh_id () in
  let layer = match layer with
    | Some layer -> layer
    | None -> match content with
      | Rooms rooms -> base_layer rooms
      | Resident _ -> Draw.get_current_layer () in
  let room =
    {
      id;
      name;
      lock = Mutex.create ();
      thread_id = Thread.(id (self ()));
      show;
      hidden = true;
      adjust;
      resize;
      geometry;
      current_geom = to_current_geom geometry;
      clip;
      mask;
      background; (* = (Some (Solid Draw.(opaque blue))); (* DEBUG *) *)
      shadow;
      content;
      layer;
      house;
      keyboard_focus;
      mouse_focus;
      canvas;
      draggable;
      removed = false;
      disabled = false
    } in
  (* We update the lookup table: *)
  (* remove is in principle not necessary *)
  if !debug
  then if WHash.mem rooms_wtable room
    then (printd debug_error "A room with same id was already in the table !";
          remove_wtable room);
  WHash.add rooms_wtable room;
  (* we update the resident room_id field *)
  (* we update the content's house field *)
  let () = match content with
    | Resident w -> w.Widget.room_id <- Some id
    | Rooms list -> if set_house
      then List.iter (fun r -> r.house <- Some room) list in
  Gc.finalise finalize room;
  (* Should we really do this [finalize]? Because who knows when the Gc will
     destroy the background texture.... maybe too late (after renderer was
     destroyed, and hence the texture pointer could point to a completely
     different texture). In order to prevent this, we call Gc.full_major when
     destroying the renderer. *)
  printd debug_board "Layout %s created." (sprint_id room);
  room

(* The public [create] version. *)
let create = create_unsafe ~set_house:true

(* the dummy room is only used to search the Weak table *)
let dummy_room = create ~name:"dummy" (geometry ()) (Rooms [])

let of_id_unsafe id =
  match WHash.find_opt rooms_wtable {dummy_room with id} with
  | None ->
    printd debug_warning "Cannot find room with id=%d" id;
    None
  | o -> o

(* A detached room is a layout that does not belong to the current layout tree,
   and is not associated to any SDL window (so no canvas field).  *)
let is_detached room =
  room.house = None && room.canvas = None

(* Currently [is_removed] is different from [is_detached]. *)
let is_removed room =
  room.removed

(* Notify the board that the layout cannot have focus (but it can still belong
   to the layout tree). *)
let remove_one room =
  printd debug_board "Removing layout %s from focus" (sprint_id room);
  room.removed <- true

let remove ?(children = false) room =
  if children
  then iter remove_one room
  else remove_one room;
  Trigger.push_remove_focus (room.id)

(* This one is more secure: we check if the layout is not detached. *)
let of_id_opt ?not_found id : room option =
  match WHash.find_opt rooms_wtable {dummy_room with id} with
  | None ->
    printd debug_error "Cannot find room with id=%d" id;
    do_option not_found run;
    None
  | Some r as o ->
    if is_detached r
    then (printd debug_error "Trying to access the detached room #%d" id; None)
    else o

(* Find the room containing a widget (or None if the widget does not belong to a
   room or if the room has disappeared in the air)*)
let containing_widget w =
  check_option w.Widget.room_id of_id_opt

let of_wid wid =
  let w = Widget.of_id wid in
  containing_widget w

(* only for debugging: *)
(* check if rooms sent to cemetery have effectively been removed by GC *)
let check_cemetery () =
  let check id = match of_id_unsafe id with
    | Some r ->
        printd debug_memory
        "Dead room %s seems to be living. Beware of zombies." (sprint_id r);
      false
    | None ->
      printd debug_memory
        "Dead room #%u was correctly burried by the GC. RIP." id;
      true
  in
  let rec loop list newlist empty = (* easier to use a Queue *)
    match list with
    | [] -> empty, newlist
    | id::rest ->
      if check id
      then loop rest newlist empty
      else loop rest (id :: newlist) false in
  let empty, newlist = loop !cemetery [] true in
  cemetery := newlist;
  empty

(* Kind of recursive Mutex. Bad style? TODO remove this necessity...
   Here we lock the layout to make it available only by the locking
   thread. Hence two consecutive locks by the same thread will not
   block. TODO mutualize with Var?  Probably better to use protect_fn
   anyways. *)
let lock l =
  if Mutex.try_lock l.lock
  then begin
      let id = Thread.(id (self ())) in
      printd debug_thread "Locking room %s for thread #%i." (sprint_id l) id;
      l.thread_id <- id
    end
  else (* then it was already locked *)
    if Thread.(id (self ())) <> l.thread_id (* not same thread, we must wait *)
    then begin
        let id = Thread.(id (self ())) in
        printd debug_thread "Waiting for thread #%i to remove lock for room %s"
          id (sprint_id l);
        Mutex.lock l.lock;
        l.thread_id <- id
      end
    else begin
        printd (debug_thread + debug_error + debug_user)
          "!! Layout %s was locked, but by the same thread: we \
           continue, but this should be corrected."
          (sprint_id l)
      end

let unlock l =
  printd debug_thread "Unlocking layout %s" (sprint_id l);
  if Mutex.try_lock l.lock
  then printd debug_thread " (but layout %s was already unlocked)." (sprint_id l);
  Mutex.unlock l.lock

(* get the renderer of the layout *)
let renderer t = match t.canvas with
  | Some c -> c.Draw.renderer
  | _ -> failwith "Cannot get renderer because no canvas was defined"

(* get the Sdl window of the layout *)
let window_opt t =
  map_option t.canvas (fun c -> c.Draw.window)

let window t =
  match window_opt t with
  | Some w -> w
  | _ -> begin
      printd debug_error
        "Cannot get window for layout %s because no canvas was defined"
        (sprint_id t);
      raise Not_found
    end

(* return the top-level layout *)
(* This is relevent only once the main loop has started. Before this, the
   top_house is not even created, so it will not return what you expect. *)
let rec top_house layout =
  match layout.house with
  | None -> layout
  | Some r -> top_house r

(* see [top_house] *)
let guess_top () =
  try WHash.iter (fun r ->
      if not (is_detached r) then raise (Found r)) rooms_wtable;
  None with
  | Found r -> Some (top_house r)

(* Check for the (currently in use) top layout *)
let is_top layout =
  layout.house = None && layout.canvas <> None

(* Shoud this be a public function? The house it not always the one we
   imagine. For instance [make_clip] will change the room. Maybe we should
   enforce in [make_clip] and others that the original room should not belong to
   a house to start with. *)
let get_house layout =
  layout.house

let get_content layout =
  layout.content

let get_canvas l =
  match l.canvas with
  | Some c -> c
  | None ->
    raise (Fatal_error
             (l, Printf.sprintf "The room #%d is not associated with any canvas"
                l.id))

(* test if layouts share the same layer (= same depth) *)
let same_layer l1 l2 =
  Chain.(get_layer l1 == get_layer l2)

let same_stack l1 l2 =
  Chain.(same_stack (get_layer l1) (get_layer l2))

(* get the layout background *)
let get_background l =
  l.background

(* force compute background at current size. Canvas must be created *)
let compute_background room =
  do_option room.background (
    fun bg ->
      let g = room.current_geom in
      Sdl.log "COMPUTE BG w=%u h=%u" g.w g.h;
      let box = match bg with
        | Style style ->
          let b = Box.(create ~width:g.w ~height:g.h ~style ()) in
          room.background <- (Some (Box b));
          b
        | Box b -> Box.unload b; b in
      ignore (Box.display (get_canvas room) (get_layer room) box
                (Draw.scale_geom (to_draw_geom g))))

(* Change background. *)
(* can be called by a thread *)
(* Remark: one should not set a "Box" background (for safety, because one cannot
   use a background of type Box in case the box already belongs to another
   room...) *)
let set_background l b =
  unload_background l;
  l.background <- b

let set_shadow l s =
  l.shadow <- s

(** get size of layout *)
let get_size l =
  l.current_geom.w, l.current_geom.h

let get_physical_size l =
  get_size l |> Draw.scale_size

(** get width of layout *)
let width l =
  l.current_geom.w

(** get height *)
let height l =
  l.current_geom.h

let resize room =
  do_option (get_house room) (fun house ->
      room.resize (get_size house))

let disable_resize room =
  printd debug_board "Disabling resize fn of %s." (sprint_id room);
  room.resize <- (fun _ -> ())

let on_resize room f =
  let r = room.resize in
  room.resize <- (fun house_size -> r house_size; f ())

let fix_content house =
  iter_rooms disable_resize house

(* Call the content resize functions with a fixed house size (even if the resize
   function mutate the house -- which is not recommended...) *)
let resize_content house =
  let s = get_size house in
  match house.content with
  | Rooms list -> List.iter (fun room -> room.resize s) list
  | Resident w -> Widget.resize w s

(* [l] must be the top house *)
let adjust_window_size l =
  if not (is_top l)
  then printd debug_error
      "[adjust_window_size] should only be called on a top house, what %s is not."
      (sprint_id l)
  else if l.canvas <> None
  then let w,h = get_physical_size l in
    let win = window l in
    if (w,h) <> Draw.get_window_size win
    then Draw.set_window_size win ~w ~h
    else printd debug_graphics
        "Window for layout %s already has the required size."
        (sprint_id l)

(* get voffset *)
let get_voffset l =
  (* l.current_geom.voffset;; *)
  Avar.get (Var.get l.geometry.voffset)

(* Get current absolute x position (relative to the top-left corner of the
   window). Not necessarily up-to-date. *)
let xpos l =
  l.current_geom.x

(* Get current absolute y position *)
let ypos l =
  l.current_geom.y

(* Left absolute coordinate of the layout's house *)
let x_origin l = match l.house with
  | None -> 0
  | Some h -> xpos h

(* Top absolute coordinate of the layout's house *)
let y_origin l = match l.house with
  | None -> 0
  | Some h -> ypos h

(* Position of room relative to house; in principle same as [getx, gety] but
   here we don't force recomputing. *)
let pos_from house room =
  xpos room - xpos house, ypos room - ypos house

(** get current x value.  *)
(* WARNING don't use this inside an animation for x ! It will loop
forever. Instead use Avar.old l.geometry.x *)
let getx l =
  Avar.get l.geometry.x

let get_oldx l =
  Avar.old l.geometry.x

(** get current y value *)
let gety l =
  Avar.get l.geometry.y

let get_oldy l =
  Avar.old l.geometry.y

(* Update [current_geom] of the [layout] and all contained rooms,
   recursively. This assumes that the layout's house already has a correct
   [current_geom]. This is rarely necessary, as all visible rooms'
   [current_geom] is updated automatically at each frame. Use this if waiting
   for next frame is not acceptable (see Long_list). However, after functions
   that mutate room content like [replace room ~by], it could be a good idea to
   call [update_current_geom by] systematically.  *)
let update_current_geom l =
  match l.house with
  | None ->
    printd (debug_error + debug_board)
      "Cannot [update_current_geom] because layout %s has no house." (sprint_id l)
  | Some house ->
    printd debug_custom "[update_current_geom] for room %s" (sprint_id l);
    let rec loop x0 y0 v0 r =
      (* printd debug_custom " => updating current_geom for %s (y0=%i)"
         (sprint_id r) y0; *)
      let x, y, voffset = x0 + getx r, y0 + gety r + v0, get_voffset r in
      r.current_geom <- { r.current_geom with x; y; voffset};
      match r.content with
      | Resident _ -> ()
      | Rooms list -> List.iter (loop x y voffset) list in
    let x0, y0, v0 = xpos house, ypos house, get_voffset house in
    loop x0 y0 v0 l

(* Change x of layout, without adjusting parent house. Warning, by
   default this disables the resize function. *)
(* This is the x coordinate wrt the containing house *)
(* This won't work if there is an animation running (see Avar.set) *)
let setx ?(keep_resize = false) l x =
  let x0 = getx l in
  l.current_geom <- { l.current_geom with x = l.current_geom.x + x - x0 };
  Avar.set l.geometry.x x;
  if not keep_resize then disable_resize l
  (* :TODO à vérifier, cf dans "flat" et "tower" *)

(* Change y of layout, without adjusting parent house. *)
(* see above *)
let sety ?(keep_resize = false) l y =
  let y0 = get_oldy l in
  l.current_geom <- { l.current_geom with y = l.current_geom.y + y - y0 };
  Avar.set l.geometry.y y;
  if not keep_resize then disable_resize l

(* see above *)
(* warning, it the animation is not finished, using Avar.set has almost no
   effect *)
let set_voffset l vo =
  Avar.set (Var.get l.geometry.voffset) vo;
  l.current_geom <-  { l.current_geom with voffset = vo }

(* use this to shift the voffset by a constant amount without stopping an
   animation *)
let shift_voffset l dv =
  Var.update l.geometry.voffset (fun av ->
  if Avar.finished av
  then begin
      let vo = Avar.get av + dv in
      Avar.set av vo;
      l.current_geom <- { l.current_geom with voffset = vo };
      av
    end
  else Avar.apply (fun y -> y + dv) av)

(* Change the size of the room and call the resize functions of the room
   content. By default this will cancel the resize function of this room. If
   [set_size] or its derivatives [set_width] and [set_height] are used as part
   of a layout resize function of the same room, this default behaviour should
   be disabled to prevent the resize function to cancel itself: use
   [keep_resize:true]. *)
(* TODO? check if the new size is really different from the old one? *)
let set_size ?(keep_resize = false) ?(check_window = true)
    ?(update_bg = false) ?w ?h l  =
  if w = None && h = None then ()
  else let () = match w,h with
    | Some w, Some h ->
       l.current_geom <- { l.current_geom with w; h };
       Avar.set l.geometry.h h;
       Avar.set l.geometry.w w
    | Some w, None ->
       l.current_geom <- { l.current_geom with w };
       Avar.set l.geometry.w w
    | None, Some h ->
       l.current_geom <- { l.current_geom with h };
       Avar.set l.geometry.h h
    | None, None -> failwith "set_size: case already treated" in

  if update_bg && l.canvas <> None then compute_background l;
  (* = ou plutot unload_background ?? *)
  if not keep_resize then disable_resize l;
  if check_window && is_top l then adjust_window_size l;
  resize_content l

module Resize = struct
  (* Convenience module for setting keep_resize=true.  Warning, when using these
     functions to modify a room's resize field, remember that they will trigger
     the room's inhabitants' resize functions. Hence one should not directly
     resize the inhabitants, otherwise this would triggers two series of
     parallel resizing... If really necessary, then make sure the resize
     functions of inhabitants is first disabled.  *)
  (* For the same reason (recursive calls to room content) one should try to
     call only one of the [set_width/set_height/set_size] function
     per resize. *)

  let keep_resize = true
  let check_window = false

  type strategy =
    | Disable
    | Linear
    | Default

  let set_height ?update_bg l h =
    set_size ~keep_resize ~check_window ?update_bg ~h l

  let set_width ?update_bg l w =
    set_size ~keep_resize ~check_window ?update_bg ~w l

  let set_size = set_size ~keep_resize ~check_window

  let setx l x = setx ~keep_resize l x

  let sety l y = sety ~keep_resize l y

  type position =
  | Pos of (int * int)
  | Left_of of room
  | Right_of of room
  | Below of room
  | Above of room
  | Top_margin
  | Bottom_margin
  | Left_margin
  | Right_margin

  (* Many of this is redundant with Space.** *)

  let set_pos_fn ?(sep = Theme.room_margin) room pos =
    let top_m = if pos = Top_margin then gety room else 0 in
    let bottom_m = if pos = Bottom_margin
      then match get_house room with
        | Some r -> height r - gety room - height room
        | None -> Theme.room_margin
      else 0 in
    let left_m = if pos = Left_margin then getx room else 0 in
    let right_m = if pos = Right_margin
      then match get_house room with
        | Some r -> width r - getx room - width room
        | None -> Theme.room_margin
      else 0 in
    (fun (w, h) ->
       match pos with
       | Pos (x, y) -> setx room x; sety room y
       | Left_of r -> setx room (getx r - sep - width room)
       | Right_of r -> setx room (getx r + sep + width r)
       | Below r -> sety room (gety r + height r + sep)
       | Above r -> sety room (gety r - height room - sep)
       | Top_margin -> sety room top_m
       | Bottom_margin -> sety room (h - bottom_m - height room)
       | Left_margin -> setx room left_m
       | Right_margin -> setx room (w - right_m - width room)
    )

  let ( >> ) f1 f2 = fun p -> (f1 p; f2 p)

  (* Only set position, without resizing *)
  let only_set_pos ?(sep = Theme.room_margin) room pos =
    let rsz = set_pos_fn ~sep room pos in
    room.resize <- rsz

  (* Add the set_pos function after the currently registered resize function. *)
  let and_set_pos ?(sep = Theme.room_margin) room pos =
    let rsz = set_pos_fn ~sep room pos in
    room.resize <- (room.resize >> rsz)

end

let set_height ?keep_resize ?check_window ?update_bg l h =
  set_size ?keep_resize ?check_window ?update_bg ~h l

let set_width ?keep_resize ?check_window ?update_bg l w =
  set_size ?keep_resize ?check_window ?update_bg ~w l

(* The public version of [set_size] *)
(* let set_size ?keep_resize ?check_window ?update_bg l (w,h) = *)
(*   set_size ?keep_resize ?check_window ?update_bg ~w ~h l *)

(* not used... *)
let reset_pos l =
  let w,h = get_size l in
  let g = geometry ~w ~h () in (* or modify l.geometry fields in-place? *)
  l.geometry <- g;
  l.current_geom <- to_current_geom g

(* [get_window_pos] is meaningful only when the window is created, that is after
   calling Bogue.make. The corresponding SDL window is created only after
   Bogue.run. In the meantime, a special use of current_geom is to indicate the
   desired window position within the desktop at startup. *)
let get_window_pos layout =
  let f x = if x = not_specified then None else Some x in
  let x,y = match layout.canvas with
    | None -> xpos layout, ypos layout
    | Some _ -> Draw.get_window_position (window layout) in
  f x, f y

(* see [get_window_pos]. It should be set *after* Bogue.make. Otherwise it has
   possibly no effect, or perhaps causes some glitches. TODO test this more
   thoroughly. *)
let set_window_pos layout (x,y) =
  match layout.canvas with
  | None -> let g = layout.current_geom in
    layout.current_geom <- { g with x; y }
  | Some _ -> Draw.set_window_position (window layout) x y

let get_transform l =
  let t = l.geometry.transform in
  let angle = Avar.get t.angle in
  let center = Avar.get t.center in
  let flip = Avar.get t.flip in
  let alpha = Avar.get t.alpha in
  Draw.make_transform ~angle ?center ~flip ~alpha ()

let get_alpha l =
  Avar.get l.geometry.transform.alpha

let draggable l =
  l.draggable

let set_draggable l =
  l.draggable <- true

let set_clip l =
  l.clip <- true

let unset_clip l =
  l.clip <- false

let set_show l b =
  l.show <- b

let rec_set_show b l =
  let rec loop b l =
    l.show <- b;
    match l.content with
    | Resident _ -> ()
    | Rooms list -> List.iter (loop b) list in
  loop b l

let is_shown l = l.show

let show_window t =
  set_show (top_house t) true;
  do_option (window_opt t) Sdl.show_window

let hide_window t =
  set_show (top_house t) false;
  do_option (window_opt t) Sdl.hide_window

(** return absolute (x,y) position *)
(* TODO optimize: test if x is up_to_date, then one can use current_geom
   instead? *)
(* of course this test will fail for hidden rooms *)
let compute_pos room =
  let rec loop x0 y0 r =
    let x,y = x0 + (Avar.get r.geometry.x),
              y0 + (Avar.get r.geometry.y) + (Avar.get (Var.get r.geometry.voffset)) in
    match r.house with
      | None -> x,y
      | Some h -> loop x y h in
  loop 0 0 room

(* Get absolute position of the parent house *)
let house_pos room =
  match room.house with
    | None -> 0,0
    | Some h -> compute_pos h;;

(* Not used, just to fix the vocabulary "leaf" *)
let is_leaf room =
  match room.content with
  | Resident _
  | Rooms [] -> true
  | _ -> false

(* Return the first resident *below (or including) room* for which test w =
    true, or None *)
let rec find_resident test room =
  match room.content with
  | Resident w -> if test w then Some room else None
  | Rooms list -> let rec loop = function
    | [] -> None
    | r :: rest -> let f = find_resident test r in
      if f = None then loop rest else f in
    loop list

(* Search through the whole component of the layout (children and parents)
   starting from top house containing room *)
let search room scan =
  if scan room then Some room
  (* =just in case it might speed-up things: room is the "initial guess" *)
  else let f r = if scan r then raise (Found r) in
    try
      iter f (top_house room);
      raise Not_found
    with
    | Found r -> printd debug_warning "Search OK"; Some r
    | Not_found -> printd debug_error "Search produced no result!"; None
    | e -> raise e

(* Find room by id in the connected component of house *)
(* cf Layout.of_id *)
let find_room_old house id =
  printd debug_warning "Search room #%d in %d..." id (house.id);
  let scan r = r.id = id in
  search house scan

(* Find the next room among neighbours (that is, in the same level of the
   house). In circular mode, after the last one comes the first. In non circular
   mode, if room is the last one, we return None. If [only_visible] is true, we
   skip all rooms that have [.show=false]. This should never raise Not_found. *)
let next ?(circular = false) ?(only_visible = true) room =
  match room.house with
  | None -> None (* we must be in top_house *)
  (* | Some h when only_visible && not h.show ->  (\* h is hidden *\) *)
  (*    None *) (* we don't do this anymore because then why not check house of
                   house of house...*)
  | Some h ->
    let rooms = get_rooms h in (* It should not be empty since room is inside. *)
    let equal r1 r2 = (r1.show || not only_visible) && r1 == r2 in
    (* TODO CORRECT THIS see [prev] below *)
    match try list_next equal room rooms with Not_found -> None
    (* : no visible room, or the only visible is the initial room *)
    with
    | None -> if circular
      then if only_visible then List.find_opt is_shown rooms else list_hd_opt rooms
      else None
    | n -> n

let prev ?(circular = false) ?(only_visible = true) room =
  match room.house with
  | None -> None (* we must be in top_house *)
  | Some h ->
    let rooms = get_rooms h in (* It should not be empty since room is inside. *)
    match try list_prev_check (equal room) ~check:(fun r -> r.show || not only_visible)
                rooms with Not_found -> None
    with
    | None ->
      if circular
      then if only_visible
        then List.find_opt is_shown (List.rev rooms)
        else list_last_opt rooms
      else None
    | n -> n

(* Find the "first" (and deepest) room (leaf) contained in the layout by going
   deep and choosing always the first room of a house *)
(* WARNING a room with empty content is considered a leaf too *)
let rec first_room r =
  printd debug_board "Descending to room %s" (sprint_id r);
  match r.content with
  | Resident _ -> r
  | Rooms [] -> r
  | Rooms (a::_) -> first_room a

(* Find a 'uncle': a room next to some parent room, going up in generation. *)
let rec next_up r =
  check_option r.house (fun h ->
      match next h with
      | None -> next_up h
      | o -> o)

(* Find the next leaf (=room containing a widget, or empty) in the whole layout
   (which should be the connected component of [top]. If [room] does not belong
   to this compoment -- which can happen after mutation -- we return the first
   leaf of [top]). *)
(* we first look at the same level, then below, then upstairs. *)
(* repeated calls to this function will visit the whole connected component --
   although this is not the optimal way to visit everything, of course -- and
   start over indefinitely. Thus you should check when the returned room is the
   one you started with... (which means you should start with a leaf !) *)
let next_leaf ~top room =
  if not (top_house room == top)
  then begin
    printd (debug_board + debug_warning + debug_custom)
      "Room %s does not belong the the top house %s. We select the first room \
       of the top house." (sprint_id room) (sprint_id top);
    first_room top
  end
  else match room.content with
    | Rooms []
    | Resident _ -> begin
        match next room with
        | None -> (* last one at this level; we go upstairs *)
          let h = match next_up room with
            | Some r -> r
            | None -> printd debug_board
                        "No next widget was found; we start again from top";
              top in
          first_room h
        | Some n ->
          (match n.content with
           | Resident _ -> n
           | Rooms _ -> first_room n)
      end
    | Rooms _ -> first_room room

(* Find the next visible room with a widget that can have keyboard_focus *)
(* TODO check example25 *)
let next_keyboard ~top room =
  let rec loop r visited =
    if List.mem r.id visited (* this happens sometimes (in case of mutation) *)
    then (printd debug_custom "Room %s already visited" (sprint_id room);
         None)
    else
      let n = next_leaf ~top r in
      if equal room n
      then (printd (debug_board + debug_custom) "No keyboard_focus found"; None)
      else if n.keyboard_focus <> None && n.show && not n.disabled
      then (printd (debug_board + debug_custom) "Found %s" (sprint_id n); Some n)
      else loop n (r.id :: visited) in
  loop room []

(********************)



(* Textures should be freed before *)
let remove_canvas room =
  iter (fun r -> r.canvas <- None) room

(* What to do when a layout is not used anymore ? *)
(* First check that it is detached from its house. *)
(* If it has children, they will become orphans  *)
(* (because a room should NOT belong to several different houses). *)
(* The resulting orphans are not freed. They are still available to use in a new
   room (see eg. longlist.ml). Use kill_all if you want to recursively free all
   subrooms *)
(* mask and background are not freed because nothing prevents them from being
   shared with another object (which is maybe not a good idea...) We leave this
   to the GC... (?) *)
(* WARNING: be careful it's quite easy to forget that something else points to
   the layout... or its children. This is easily the case for instance with
   Bogue.board fields like windows_house, mouse_focus, keyboard_focus,
   button_down... Not to mention widgets, which refer indirectly to layouts via
   their id... So it's preferable never to use this... *)
(* not used yet *)


(* When to call this ? *)
(* in particular, when this function is called, the layout l in principle has
   already been removed from rooms_wtable *)
let free l =
  printd debug_memory "Freeing Layout %s" (sprint_id l);
  unload_background l;
  begin match l.content with
  | Resident _ -> ()
  | Rooms list ->
     list_iter list (fun r ->
         do_option r.house (fun h ->
             if equal h l
             then printd debug_warning "Room %s is now orphan" (sprint_id r)))
  end

(* kill functions below are quite dangerous, beware *)

(* use this when the layout + all children is not used anymore *)
(* In fact don't use this, use kill_rooms instead. Because very often a layout
   is created with subrooms that don't all necessarily have a name. Thus, if you
   want to kill a layout, you may forget that its direct house has no name, so
   it will likely stay in the table for ever. It's difficult for the user to
   keep track of this. One could use ocaml's Ephemeron instead ?*)
(* Note that rooms can be reclaimed though their id, for instance via of_wid, or
   even more devily stored in an event... At this point it DOES cause some fatal
   errors that I don't know how to locate... *)
let kill_all_NO room =
  match room.house with
  | Some h -> printd debug_error "Cannot kill layout #%u because it still \
                                  belongs to a house #%u" room.id h.id;
  | None -> (* we defer it to the main loop *)
     Sync.push (fun () ->
         delete_backgrounds room;
         let rec loop r =
           remove_wtable r;
           match r.content with
           | Resident w -> Widget.free w
           | Rooms list -> List.iter loop list
         in
         loop room)

(* kill all rooms (and theirs subrooms) of this house *)
(* defered to the main loop *)
(* don't use this. See WARNING in "kill" above *)
let kill_rooms_NO house =
  match house.content with
  | Resident _ -> printd debug_error "House #%u does not have rooms to kill..." house.id
  | Rooms list ->
     Sync.push (fun () ->
         let rec loop r =
           delete_background r;
           remove_wtable r;
           match r.content with
           | Resident w -> Widget.free w
           | Rooms list -> List.iter loop list
         in
         List.iter loop list)



(**********)

(* Use this to shift all current_geometries before inserting a room inside a
   house. This can be needed because inserting will trigger fit_content which
   uses current_geom *)
let global_translate room dx dy =
  do_option room.house (fun h ->
      printd debug_warning
        "You are translating the current_geom of room #%u which already has a \
         house #%u. This is likely to have no effect, as the current_geom is \
         automatically updated at each display" room.id h.id);
  iter (fun r ->
      r.current_geom <- { r.current_geom with x = r.current_geom.x + dx;
                                              y = r.current_geom.y + dy }) room

(* Adjust layout size to the inner content in the same layer (but not to the
   larger layouts, neither to the window) *)
(* TODO: treat margins *)
(* Not widely used yet... but see for instance the "modif_parent" tutorial. *)
let rec fit_content ?(sep = Theme.room_margin/2) l =
  if l.adjust = Nothing || l.clip then ()
  else let w,h = match l.content with
      | Resident widget -> Widget.default_size widget
      (* | Rooms [r] -> r.geometry.w, r.geometry.h *)
      | Rooms list ->
        let x0 = l.current_geom.x in
        let y0 = l.current_geom.y in
        ( List.fold_left (fun m r ->
              if same_layer r l
              then imax m (r.current_geom.x - x0 + r.current_geom.w)
              else m)
              0 list,
          List.fold_left (fun m r ->
              if same_layer r l
              then imax m (r.current_geom.y - y0 + r.current_geom.h)
              else m)
            0 list )
    in
    let g' = match l.adjust with
      | Fit -> { l.current_geom with w = w+sep; h = h+sep };
      | Width -> { l.current_geom with w = w+sep };
      | Height -> { l.current_geom with h = h+sep };
      | Nothing -> failwith "already treated case !" in
    let oldg = l.current_geom in
    if g' <> oldg then begin
      printd debug_graphics "ADJUST %s to New SIZE %d,%d" (sprint_id l) w h;
      set_size l ~w:g'.w ~h:g'.h;
      do_option l.house fit_content  (* we adjust the parent (???) *)
    end

(* Return the list of widgets used inside the layout. *)
let rec get_widgets layout =
  match layout.content with
    | Rooms h -> List.flatten (List.map get_widgets h)
    | Resident w -> [w]

let has_resident layout =
  match layout.content with
  | Resident _ -> true
  | Rooms _ -> false

let accept_focus r =
  r.show && not r.disabled && not r.removed && not r.hidden

let has_keyboard_focus r =
  r.keyboard_focus = Some true && not r.disabled

(* Set keyboard_focus to the room and the resident widget, if possible. In debug
   mode, this will draw some shadow around the layout when focused... Warning,
   currently, even if the room doesn't have the keyboard_focus flag, this does
   not prevent the board to register it as keyboard focus... TODO: what to
   do? *)
let set_keyboard_focus r =
  if r.disabled
  then printd debug_error "Trying to set keyboard_focus to a disabled room %s" (sprint_id r)
  else match r.keyboard_focus with
    | Some b ->
      if not b then begin
        printd debug_board "Setting keyboard_focus to room %s" (sprint_id r);
        r.keyboard_focus <- Some true;
        match r.content with
        | Rooms _ -> ()
        | Resident w -> Widget.set_keyboard_focus w
      end
    | None -> printd debug_board
                "Cannot set keyboard_focus to room %s because if was not created \
                 with keyboard_focus capability." (sprint_id r)

let rec remove_keyboard_focus r =
  do_option r.keyboard_focus (fun b -> if b then r.keyboard_focus <- Some false);
  Trigger.push_remove_focus r.id;
  match r.content with
  | Rooms list -> List.iter remove_keyboard_focus list
  | Resident w -> Widget.remove_keyboard_focus w

let claim_focus r =
  if has_resident r then Trigger.push_mouse_focus r.id
  else printd (debug_error + debug_board)
         "Cannot claim focus on room %s without resident." (sprint_id r)

let claim_keyboard_focus r =
  if has_resident r then Trigger.push_keyboard_focus r.id
  else printd (debug_error + debug_board)
         "Cannot claim keyboard_focus on room %s without resident." (sprint_id r)

let disable t =
  iter (fun r -> r.disabled <- true) t;
  remove_keyboard_focus t;
  Trigger.push_remove_focus t.id

let enable t =
  iter (fun r -> r.disabled <- false) t

let set_enabled t = function
  | true -> enable t
  | false -> disable t

(* Center vertically the rooms of the layout (first generation only) *)
let v_center layout y0 h =
  match layout.content with
  | Resident _ -> ()
  | Rooms rs ->
     list_iter rs
       (fun r -> let h0 = height r in
                 let y = Draw.center y0 h h0 in
                 sety r y)

(** vertical align *)
(* v_center is the same as v_align ~align:Draw.Center. This resets the [resize]
   field *)
let v_align ~align layout y0 h =
  match layout.content with
  | Resident _ -> ()
  | Rooms rs ->
    list_iter rs
      (fun r -> let h0 = height r in
        let y = Draw.align align y0 h h0 in
        sety r y)


(** create a room (=layout) with a unique resident (=widget), in the current
   layer (unless specified). No margin possible. *)
(* x and y should be 0 if the room is the main layout *)
(* warning, the widget is always centered *)
(* x,y specification will be overwritten if the room is then included in a flat
   or tower, which is essentially always the case... *)
let resident_with_layer ?layer ?name ?(x = 0) ?(y = 0) ?w ?h ?background
    ?draggable ?canvas ?keyboard_focus widget =
  let (w',h') = Widget.default_size widget in
  let w = default w w' in
  let h = default h h' in
  let keyboard_focus = match keyboard_focus with
    | Some true -> Some false
    | Some false -> None
    | None -> Widget.guess_unset_keyboard_focus widget in
  let geometry = geometry ~x ~y ~w ~h () in
  create ?name ?background ?keyboard_focus ?draggable ?canvas ?layer
    geometry (Resident widget)

let resident ?name ?(x = 0) ?(y = 0) ?w ?h ?background ?draggable
    ?canvas ?keyboard_focus widget =
  resident_with_layer ?name ~x ~y ?w ?h ?background ?draggable
    ?canvas ?keyboard_focus widget

let of_widget = resident

(* Set the given widget as the new resident of the given room. If w,h are not
   specified, the size of the room will be updated by the size of the widget. *)
let change_resident ?w ?h room widget =
  match room.content with
  | Resident resid ->
     printd debug_board "Replacing room %s's widget by widget #w%d"
       (sprint_id room) (Widget.id widget);
     let (w',h') = Widget.default_size widget in
     let w = default w w' in
     let h = default h h' in
     room.content <- Resident widget;
     widget.Widget.room_id <- Some room.id;
     room.keyboard_focus <- Widget.guess_unset_keyboard_focus widget;
     resid.Widget.room_id <- None;
     set_size room ~w ~h
  | _ -> printd debug_event "[change_resident]: but target room has no resident!"

(* An empty layout can reserve some space without stealing focus (and has no
   keyboard_focus) *)
(* WARNING TODO in the search functions, we have assumed rooms where never
   empty... *)
let empty ?name ?background ~w ~h () =
  let geometry = geometry ~w ~h () in
  create ?name ?background geometry (Rooms [])

(* Simple resize function that scales the room with respect to the given
   original house size (w,h) *)
let scale_resize ?(scale_width=true) ?(scale_height=true)
    ?(scale_x=true) ?(scale_y=true) (w,h) r =
  let x = xpos r in
  let y = ypos r in
  let rw,rh = get_size r in
  let resize (hw,hh) = let open Resize in
    let scalex z = z * hw / w in
    let scaley z = z * hh / h in
    if scale_x then setx r (scalex x);
    if scale_y then sety r (scaley y);
    if scale_height then set_voffset r (scaley (get_voffset r));
    if scale_height && scale_width then set_size r ~w:(scalex rw) ~h:(scaley rh)
    else if scale_height then set_height r (scaley rh)
    else if scale_width then set_width r (scalex rw) in
  r.resize <- resize

(* Convenience function for scaling all rooms (including margins) by the same
   factor -- to be use for rooms in the same house *)
let scale_resize_list ?scale_width ?scale_height ?scale_x ?scale_y (w,h) rooms =
  List.iter (scale_resize ?scale_width ?scale_height ?scale_x ?scale_y (w,h))
    rooms

(* Overrides scale_resize to retrieve the house size automatically. Can only be
   applied if the room is already in a house. *)
let scale_resize ?scale_width ?scale_height ?scale_x ?scale_y room =
  match room.house with
  | None -> printd debug_error
              "Cannot compute the resize function of room %s since it does not \
               belong to a house" (sprint_id room)
  | Some h ->
     let s = get_size h in
     scale_resize ?scale_width ?scale_height ?scale_x ?scale_y s room

let auto_scale house =
  let (w,h) = get_size house in
  match house.content with
  | Rooms rooms -> scale_resize_list (w,h) rooms
  | Resident _ -> printd debug_warning "TODO: auto_scale resident"


let clip_interval ?xmin ?xmax x =
  match xmin, xmax with
  | None, None -> x
  | Some xmin, None -> imax x xmin
  | None, Some xmax -> imin x xmax
  | Some xmin, Some xmax -> imin xmax (imax xmin x)

let clip_size ?min_width ?min_height ?max_width ?max_height (w, h) =
  let ww = clip_interval ?xmin:min_width ?xmax:max_width w in
  let hh = clip_interval ?xmin:min_height ?xmax:max_height h in
  (ww, hh)

let resize_follow_house ?min_width ?min_height ?max_width ?max_height room =
  let rsz = match min_width, min_height, max_width, max_height with
    | None, None, None, None ->
      fun (w, h) -> Resize.set_size room ~w ~h
    | _ -> let open Resize in fun (w, h) ->
        let w, h = clip_size ?min_width ?min_height ?max_width ?max_height (w, h) in
        set_size room ~w ~h
  in
  room.resize <- rsz

let resize_follow_width ?min_width ?max_width room =
  let rsz = match min_width, max_width with
    | None, None -> fun (w, _) -> Resize.set_width room w
    | _ -> let open Resize in fun (w, _) ->
        set_width room (clip_interval ?xmin:min_width ?xmax:max_width w) in
  room.resize <- rsz

(* Resize by keeping the initial 4 margins with respect to the house *)
let resize_keep_margins ?margin ?(min_width=Theme.room_margin)
    ?(min_height = Theme.room_margin) ?max_width ?max_height room =
  let top_margin, bottom_margin, left_margin, right_margin =
    match margin with
    | Some x -> x, x, x, x
    | None -> let bm, rm = match get_house room with
        | Some house -> height house - height room - gety room,
                        width house - width room - getx room
        | None -> gety room, getx room in
      gety room, bm, getx room, rm in
  room.resize <- let open Resize in
    (fun (w, h) ->
       let w, h = clip_size ~min_width ~min_height ?max_width ?max_height
           (w - left_margin - right_margin, h - top_margin - bottom_margin) in
       set_size room ~w ~h;
       setx room left_margin;
       sety room top_margin)

(* Locate x,y of rooms in a flat arrangement and return global size. This resets
   the [resize] field. *)
let flat_settle_rooms ~sep ~hmargin ~vmargin rooms =
  let rec loop list x y =
    match list with
    | [] -> if x = hmargin then (2*hmargin, 2*vmargin) (* empty list*)
      else (x - sep + hmargin, y)
    | r::rest ->
      setx r x;
      sety r vmargin;
      let wr = width r in
      loop rest (if wr = 0 then x else x + sep + wr)
        (max y ((height r) + 2*vmargin))
  in loop rooms hmargin vmargin

module Detect = struct
  (* EXPERIMENTAL. Not used. Detect flats and towers. Of course we could also add
     a [shape] entry to the layout type... *)

  let constant_list = function
    | [] -> true
    | x::rest -> List.for_all (fun (i : int) -> i = x) rest

  let almost_equal x y = abs (x-y) <= 1

  let almost_constant = function
    | [] -> true
    | x::rest ->
      let rec loop m mm = function
        | [] -> almost_equal m mm
        | i::rest -> almost_equal m mm && almost_equal m i && almost_equal m i &&
                     loop (imin m i) (imax mm i) rest in
      loop x x rest

  let test_almost_constant () =
    assert (almost_constant [5;5;5;5;5]);
    assert (almost_constant [4;3;4;3;4;4;3;3;4]);
    assert (not (almost_constant [4;3;4;3;4;4;3;3;5]))

  let detect_h_alignment gs =
    let tops = List.map (fun g -> g.y) gs in
    let bottoms = List.map (fun g -> g.y + g.h) gs in
    let mids = List.map (fun g -> g.y + g.h/2) gs in
    if constant_list tops then Some Draw.Min
    else if constant_list bottoms then Some Draw.Max
    else if constant_list mids then Some Draw.Center
    else if almost_constant tops then Some Draw.Min
    else if almost_constant bottoms then Some Draw.Max
    else if almost_constant mids then Some Draw.Center
    else None

  let detect_h_margin w = function
    | [] -> 0
    | g1::_ as l ->
      let g2 = list_last l in
      let m1 = g1.x in
      let m2 = w - g2.x - g2.w in
      if almost_equal m1 m2 then imin m1 m2
      else (printd debug_warning "left and right margins are not equal, taking min.";
            imin m1 m2)

  let detect_sep = function
    | [] -> 0
    | g::rest -> let rec loop seps g = function
        | [] -> seps
        | g2::rest -> loop ((g2.x - g.x - g.w) :: seps) g2 rest in
      let seps = loop [] g rest in
      if not (almost_constant seps)
      then printd debug_error
          "Separation spaces are not constant in this layout; using the min.";
      default (list_min Int.compare seps) 0

  (* Return Some(align, sep, hmargin, vmargin) if layout seems to be a flat. *)
  let detect_flat_margins room =
    match room.content with
    | Resident _ ->
      printd (debug_error + debug_board)
        "[detect_flat_margins] on %s expects rooms, not a resident"
        (sprint_id room);
      None
    | Rooms [] -> Some (Draw.Min, 0, 0, 0)
    | Rooms rooms ->
      let gs = List.map (fun g -> g.current_geom) rooms in
      match detect_h_alignment gs with
      | None ->
        printd (debug_error + debug_board)
          "[detect_flat_margins] argument %s does not seem to be a flat (not aligned)"
          (sprint_id room); None
      | Some align ->
        let w = width room in
        let hmargin = detect_h_margin w gs in
        let vmargin = default (list_min Int.compare (List.map (fun g -> g.y) gs)) 0 in
        let sep = detect_sep gs in
        Some (align, sep, hmargin, vmargin)
end

let compute_linear_scale n scale initial_len ~len ~margin ~sep =
  if n <> 0
  then let havail_old = imax 1 (initial_len - 2*margin - (n-1)*sep) in
    let havail_new = imax 1 (len - 2*margin - (n-1)*sep) in
    let s = float havail_new /. float havail_old in
    scale := Some s;
    (* printd debug_custom "[compute_linear_scale] new=%i, old=%i, scale=%f"
       havail_new havail_old s; *)
    s
  else 1.

let resize_flat_room ~resize_height initial_flat_size scale
    ~hmargin ~vmargin ~sep ~align room =
  let init_w, init_h = get_size room in
  let scalex, scaley = scale in
  room.resize <- (fun (w, h) ->
      let open Resize in
      if room.show then begin
        (* compute width *)
        let scale, margin, len = scalex, hmargin, w in
        let n = List.length (neighbours room) in
        let s = match !scale with
          | Some x -> x
          | None -> compute_linear_scale n scale ~len (fst initial_flat_size)
                      ~margin ~sep in
        let ww = (imax 1 (round (s *. float init_w))) in
        let hh = if resize_height
          then begin (* compute height *)
            let scale, margin, len = scaley, vmargin, h in
            let s = match !scale with
              | Some x -> x
              | None -> compute_linear_scale 1 scale ~len (snd initial_flat_size)
                          ~margin ~sep in
            Some (imax 1 (round (s *. float init_h)))
          end else None in
        set_size room ~w:ww ?h:hh;
        if next room = None (* last room *) then (scalex := None; scaley:=None);
        (* set x position *)
        let () = match prev room with
          | None -> setx room hmargin
          (* = first room. In principle, this is not necessary. *)
          | Some p -> let wp = width p in
            setx room (if wp = 0 then getx p else getx p + wp + sep) in
        (* vertical align *)
        let y = Draw.align align vmargin (h - 2*vmargin) (height room) in
        sety room y
      end)

(* almost copy-paste from above; any change to one, please update the other *)
let resize_tower_room ~resize_width initial_tower_size scale
    ~hmargin ~vmargin ~sep ~align room =
  let init_w, init_h = get_size room in
  let scalex, scaley = scale in
  printd debug_custom "[resize_tower_room] %s init_w=%i" (sprint_id room) init_w;
  room.resize <- (fun (w,h) ->
      let open Resize in
      if room.show then begin
        printd debug_custom "resize function for %s w=%i" (sprint_id room) w;
        (* compute height *)
        let scale, margin, len = scaley, vmargin, h in
        let n = List.length (neighbours room) in
        let s = match !scale with
          | Some x -> x
          | None -> compute_linear_scale n scale ~len (snd initial_tower_size)
                      ~margin ~sep in
        let hh = (imax 1 (round (s *. float init_h))) in
        let ww = if resize_width then begin (* compute optional width *)
          let scale, margin, len = scalex, hmargin, w in
          let s = match !scale with
            | Some x -> x
            | None -> compute_linear_scale 1 scale ~len (fst initial_tower_size)
                        ~margin ~sep in
          printd debug_custom "set_width %i" (imax 1 (round (s *. float init_w)));
          Some (imax 1 (round (s *. float init_w)))
        end else None in
        set_size room ?w:ww ~h:hh;
        if next room = None (* last room *) then (scalex := None; scaley := None);
        (* set y position *)
        let () = match prev room with
          | None -> sety room vmargin
          (* = first room. In principle, this is not necessary. *)
          | Some p -> printd debug_custom "previous room %s, show=%b, y=%i"
                        (sprint_id p) (p.show) (gety p);
            let hp = height p in
            sety room (if hp = 0 then gety p else gety p + height p + sep) in
        (* horizontal align *)
        let x = Draw.align align hmargin (w - 2*hmargin) (width room) in
        setx room x
      end)

(* Equip the flat rooms by a resize function that scales proportionally to the
   initial size, but do not scale margins. *)
(* TODO allow some elements to have min width and max width *)
let resize_flat ?(resize_height = true) ~hmargin ~vmargin ~sep ~align house =
  let size = get_size house in
  let scale = ref None, ref None in
  (* This [scale] variable is shared by all rooms. Currently, this systems does
     not allow to correctly resize a room dynamically added to the flat after
     creation, because we cannot retrieve this variable. WARNING [W1] The scale
     variable is reset by the last room of the flat. Hence, if someone changes
     later the resize function of the last room, the whole flat will stop
     resizing. (See for instance Space's [reset_scaling].) *)
  iter_rooms (resize_flat_room ~resize_height size scale ~hmargin ~vmargin ~sep ~align)
    house

let resize_tower ?(resize_width = true) ~hmargin ~vmargin ~sep ~align house =
  let size = get_size house in
  let scale = ref None, ref None in
  iter_rooms (resize_tower_room ~resize_width size scale ~hmargin ~vmargin ~sep ~align)
    house

(* Not very smart, but [resize_fix_x/y] is currently used by Space. Another
   possibility would be to have two resize functions: horizontally and
   vertically. Should not be called 1000 times on the same room: resize function
   will pile up...  *)
let resize_fix_x room =
  let f = room.resize in
  room.resize <- (fun size ->
      let open Resize in
      let x,w = getx room, width room in
      f size;
      setx room x;
      set_width room w
    )

let resize_fix_y room =
  let f = room.resize in
  room.resize <- (fun size ->
      let open Resize in
      let y,h = gety room, height room in
      f size;
      sety room y;
      set_height room h
    )

(* sets l with the size of the top_house. In principle the (x,y) of the
   top_house should be (0,0), we don't check this here. The (x,y) of l is set to
   (0,0). Should be called dynamically after main loop starts. *)
let maximize l =
  setx l 0;
  sety l 0;
  let w,h = get_size (top_house l) in
  l.current_geom <- { l.current_geom with h; w };
  Avar.set l.geometry.h h;
  Avar.set l.geometry.w w;
  scale_resize_list (w,h) [l];
  resize_content l

(* check if a sublayer is deeper (= below = Chain.<) than the main layer, which
    (in principle) should not happen *)
let check_layers room =
  let rec loop house r =
    match r.content with
    | Resident _ ->
       if Chain.(get_layer house >. get_layer r)
       then printd debug_error
              "The house #%d contains a room #%d with deeper layer! (%d>%d)"
              house.id r.id (Chain.depth (get_layer house))
              (Chain.depth (get_layer r));
    | Rooms h -> List.iter (loop r) h
  in
  loop room room

(** Set the canvas of the layout. Warning! we assume that if a room has a
    canvas, all smaller rooms already have the same canvas... *)
(* warning: this is also used when we change the layer, but the window stays the
   same *)
(* let rec set_canvas canvas room = *)
(*   if Draw.canvas_equal room.canvas canvas *)
(*   then () *)
(*   else begin *)
(*     printd debug_warning "Changing room canvas"; *)
(*     room.canvas <- canvas; *)
(*     match room.content with *)
(*       | Rooms list -> List.iter (set_canvas canvas) list *)
(*       | Resident _ -> () *)
(*   end;; *)
let set_canvas canvas room =
  room.canvas <- Some canvas;
  if !debug then check_layers room

(** Set the canvas for layout and all children *)
let global_set_canvas room canvas =
  iter (fun r -> r.canvas <- Some canvas) room

let check_layer_error room house =
  if not (Chain.same_stack room.layer house.layer)
  then printd debug_error
         "The replacement room %s belongs to a separate set of layers disjoint \
          from the house %s (or one of them has empty layer). Beware that it \
          will probably never be displayed"
         (sprint_id room) (sprint_id house)

(* Move the room layer into the stack of the dst layer (this actually makes a
   copy of the layer into a dst stack, without destroying the initial layer,
   which may be shared by other rooms). Warning: this does not check the blit
   contents of the layers; it's supposed to be done before the blits are
   computed (when all layers should contain empty queues). The new copy get an
   empty queue anyways.*)
let move_to_stack ~dst room =
  if not (same_stack room dst)
  then begin
    let dst_layer = get_layer dst in
      printd debug_board "Moving layer of %s into that of %s (stack:%u)"
        (sprint_id room) (sprint_id dst) (Chain.get_stack_id dst_layer);
      room.layer <- Chain.copy_into ~dst:dst_layer (get_layer room);
      Chain.replace room.layer (Draw.new_layer ())
    end

(* Move the room layer and the layers of all inhabitants to the dst layer *)
let move_all_to_stack ~dst room =
  iter (move_to_stack ~dst) room

(* move all layers contained in [room] into the same stack as the [room]
   layer. *)
let unify_layer_stack room =
  move_all_to_stack ~dst:room room

let set_new_stack ?(force = false) win =
  if force || is_top win then begin
    printd debug_board "Creating a new stack for window layout %s."
      (sprint_id win);
    win.layer <- Chain.copy_into ~dst:None (get_layer win);
    Chain.replace win.layer (Draw.new_layer ())
  end else printd (debug_board + debug_error)
      "Creating a new stack is only allowed for top layouts (windows), not for %s"
      (sprint_id win)

let move_to_new_stack room =
  set_new_stack room;
  unify_layer_stack room

(* specialized [create] version for creating the list of all windows (= top
   layouts) *)
let create_win_house windows =
  (* We make sure each window's layer belongs to a different stack. (If not, we
     create new stacks.) *)
  let rec loop layer_ids = function
    | [] -> ()
    | win::rest ->
      let id = Chain.get_stack_id (get_layer win) in
      let id =
        if List.mem id layer_ids then
          begin
            set_new_stack ~force:true win;
            Chain.get_stack_id (get_layer win)
          end else id in
      unify_layer_stack win;
      loop (id::layer_ids) rest in
  loop [] windows;
  let layer = None in
  create_unsafe ~set_house:false ~name:"windows_house" ~layer
    (geometry ()) (Rooms windows)

(* use this only if you know what you are doing... *)
(* remember that a room with no house will be considered a "top layout" *)
(* see WARNING of the "kill" fn. If the detached room is still pointed to by the
   board (eg. mouse_focus...=> the mouse will not find what you expect) *)
(* not used *)
let detach_rooms layout =
  match layout.content with
  | Resident _ ->
     printd debug_error "No rooms to detach from layout %s" (sprint_id layout)
  | Rooms rooms ->
     list_iter rooms (fun r ->
         if r.house <> None then
           (r.house <- None;
            r.canvas <- None;
            printd debug_warning "Room %s was detached from House %s..."
              (sprint_id r) (sprint_id layout)))

(* Detach a room from its house. See detach_rooms. Warning, the textures are not
   freed. *)
let detach room =
  match room.house with
  | None -> printd debug_error "Cannot detach because room %s has no house"
              (sprint_id room)
  | Some h ->
    room.house <- None;
    room.canvas <- None;
    remove ~children:true room;
    let rooms = List.filter (fun r -> not (r == room)) (get_rooms h) in
    h.content <- Rooms rooms;
    printd debug_warning "Room %s was detached from House %s."
      (sprint_id room) (sprint_id h)

(* Sets the required fields for [room] to be a room of [dst], but does not
   install it within the rooms list. This has to be done separately. See for
   instance [add_room] or [replace_room]. *)
let attach ~dst room =
  room.house <- Some dst;
  move_to_stack ~dst room;
  (* if there is a canvas in layout, we copy it to all rooms *)
  do_option dst.canvas (global_set_canvas room)

(* Check if [room] can be added to the content of [dst]. If [already = true] we
   accept to add a room in a house that already contains it... (this is used
   when we want to re-order rooms). If [loop_error = false] it doesn't raise an
   error when room = dst, we just return false. *)
let ok_to_add_room ?(already = false) ?(loop_error = true) ~dst room =
  if equal room dst
  then begin
    printd debug_error "Cannot add room %s to itself!" (sprint_id room);
    if loop_error then invalid_arg "[Layout.ok_to_add_room] room %s"
        (sprint_id room)
    else false
    (* equivalent to (not ((not loop_error) || raise ...)) *)
  end else match room.house with
    | Some h when equal h dst ->
      printd ((if already then debug_warning else debug_error) + debug_board)
        "Room %s already belongs to %s." (sprint_id room) (sprint_id dst);
      already
    | Some h ->
      printd (debug_error + debug_board)
        "Room %s should not be added to %s because it belongs to another house \
         (%s). We do it anyway." (sprint_id room) (sprint_id dst) (sprint_id h);
      (* This is actually quite bad error because then the other house will be
         garbage collected (finalized), its rooms will be GCed too... *)
      true
    | None -> true

(* Modify the layout content by setting new rooms *)
(* Old ones are *not* freed, but they are *detached* from house, and their
   textures are "unloaded". *)
(* Note that setting rooms that are already there is legal (can be used to
   change the order). Then they are not detached, of course. *)
(* With sync=false, this is highly non thread safe. Locking layout is not enough
   (or, we should lock all layouts in the main loop too... Therefore, it is
   better to set sync=true, which delays the execution to Sync (the main loop
   Queue) *)
(* mutualize with [add_room, replace_room]?*)
let set_rooms layout rooms =
  match layout.content with
  | Resident _ ->
    printd debug_error
      "Cannot transform a leaf (Resident #%u) to a node (Rooms) because the \
       resident widget would be lost" layout.id
  | Rooms old_rooms ->
    list_iter rooms (fun r ->
        if mem equal r old_rooms
        then printd (debug_board + debug_warning)
            "Trying to insert a room (%s) that is already there (%s). We leave \
             it there, no problem."
            (sprint_id r) (sprint_id layout)
        else if ok_to_add_room ~dst:layout r
        then attach ~dst:layout r);

    (* Now we rescan the list to detach unused rooms. *)
    list_iter old_rooms (fun r ->
        if not (mem equal r rooms) then begin
          detach r;
          iter_unload_textures r
        end);
    (* detach_rooms layout; *) (* we don't detach because some orphans may
                                  want to survive longer than you
                                  think... see WARNING in 'kill' *)
    layout.content <- Rooms rooms
(*fit_content layout*)

let set_rooms layout ?(sync=true) rooms =
  (if sync then Sync.push else run) (fun () -> set_rooms layout rooms)

(* like set_rooms but in addition the old ones are killed *)
let replace_rooms_NO layout rooms =
  kill_rooms_NO layout;
  set_rooms layout rooms

(* copy the 'relocatable content' of src into dst.  Of course, this should be
   avoided when writing in functional style, but can be handy sometimes *)
(* Warning: size will change, and this is not transmitted to the parent house *)
(* Warning: the old content is not freed from memory *)
(* TODO: move everything to Sync (not only set_rooms) ? *)
let copy ~src ~dst =
  let dx = dst.current_geom.x - src.current_geom.x in
  let dy = dst.current_geom.y - src.current_geom.y in
  global_translate src dx dy;
  dst.geometry <- { dst.geometry with w = src.geometry.w; h = src.geometry.h };
  let w,h = get_size src in
  dst.current_geom <- { dst.current_geom with w; h };
  dst.clip <- src.clip;
  dst.background <- src.background;
  begin match src.content with
    | Resident r as c -> r.Widget.room_id <- Some dst.id; dst.content <- c
    | Rooms rooms -> set_rooms dst rooms
  end;
  dst.keyboard_focus <- src.keyboard_focus;
  dst.draggable <- src.draggable

(* Add a room to the dst layout content (END of the list). This does *not*
   enlarge the containing house. The resize function of the room is cancelled. *)
(* This is used to add a pop-up *)
(* warning: the room should NOT already belong to some house. *)
(* TODO: write a "remove_room" function *)
let add_room ?valign ?halign ~dst room =
  if ok_to_add_room ~dst room then begin
    check_layer_error room dst;
    let rooms = get_rooms dst in
    (* we cannot add room to layout which already contains a Resident *)
    (* We now check oversize. But this should not happen. The user should not
       rely on this. If the added room is too large, beware that nothing outside
       of the geometry of the destination room will never have mouse focus
       (mouse focus is detected per house, and THEN into the children rooms. *)
    let wmax = (getx room) + (width room) in
    if wmax > width dst
    then (printd debug_error "The attached Room #%u is too wide" room.id;
          (*set_width dst wmax*));
    let hmax = (gety room) + (height room) in
    if hmax > height dst
    then (printd debug_error "The attached Room #%u is too tall" room.id;
          (*set_height dst hmax*));

    let x = default_lazy (map_option halign (fun a ->
        Draw.align a 0 (width dst) (width room)))
        (lazy (getx room)) in
    let y = default_lazy (map_option valign (fun a ->
        Draw.align a 0 (height dst) (height room)))
        (lazy (gety room)) in
    setx room x;
    sety room y;
    attach ~dst room;

    dst.content <- Rooms (List.rev (room :: (List.rev rooms)))
    (*  fit_content dst;; *)
  end

let set_layer ?(debug = !debug) room layer =
  room.layer <- layer;
  if debug then check_layers room

(* TODO: do some "move layer" or translate layer instead *)
let rec_set_layer room layer =
  iter (fun r -> set_layer ~debug:false r layer) room

(* Construct a horizontal house from a list of rooms *)
(* sep = horizontal space between two rooms *)
(* hmargin = horizontal margin (left and right). *)
(* vmargin = vertical margin (top and bottom). *)
(* if margins is set, then sep, hmargin and vmargin are all set to this value *)
(* WARNING: resulting layout has position (0,0). *)
(* There are 3 resizing strategies, when the flat layout is resized:

   1. Do nothing: the rooms maintain their sizes and positions: use
   [resize:Resize.Disable], and previous room resize function is removed. If [clip]
   is true and the layout size is smaller than the initial size, some of the
   content will be hidden ("clipped").

   2. Globally scale rooms proportionnally to the layout size. (Default
   behaviour.)

   3. Scale rooms horizontally proportionnally to the layout width, and keep
   rooms height (while adjusting their vertical position according to the
   [align] parameter): use [resize:Resize.Linear].

   In all cases, resize functions already present in [rooms] will be cancelled.
*)
let flat ?name ?sep ?(adjust=Fit) ?hmargin ?vmargin ?margins ?align
    ?background ?shadow ?canvas ?resize ?clip rooms =
  (* List.iter (set_canvas canvas) rooms; *)
  let sep, hmargin, vmargin = match margins with
    | Some m ->
      if sep <> None || hmargin <> None || vmargin <> None
      then printd debug_user "Call to [flat]: the [margins] parameter is set and \
                              overrides [sep,hmargin,vmargin].";
      m,m,m
    | None -> default sep (Theme.room_margin / 2),
              default hmargin (Theme.room_margin),
              default vmargin (Theme.room_margin) in
  let w,h = flat_settle_rooms ~sep ~hmargin ~vmargin rooms in
  let layout = create ?name ?background ?shadow ?clip
      (geometry ~w ~h ()) ~adjust (Rooms rooms) ?canvas in
  do_option align (fun align -> v_align ~align layout vmargin (h-2*vmargin));
  (* Now that the geometry is finalized, we may compute the resize function for
     each room: *)
  let resize = default resize
      (if clip = Some true then Resize.Disable else Resize.Default) in
  let () = if resize = Resize.Disable then List.iter disable_resize rooms
    else let resize_height = not (resize = Resize.Linear) in
      resize_flat ~resize_height ~hmargin ~vmargin ~sep ~align:(default align Draw.Min)
        layout in
  (* if !debug then begin *)
  (*   match Detect.detect_flat_margins layout with *)
  (*   | None -> failwith "flat not detected as flat! Fix me." *)
  (*   | Some (align', sep', hmargin', vmargin') -> *)
  (*     printd debug_custom "Flat detected: align=%s (detected %s), sep=%i (detected \ *)
  (*                           %i) hmargin=%i (detected %i) vmargin=%i (detected %i)" *)
  (*       (string_of_option Draw.pr_align align) (Draw.pr_align align') *)
  (*       sep sep' hmargin hmargin' vmargin vmargin' *)
  (* end; *)
  layout

let hbox = flat

(* Construct a flat directly from a list of widgets that we convert to
   Residents. By default it uses smaller margins than [flat]. *)
let flat_of_w ?name ?(sep = Theme.room_margin) ?h ?align ?background ?widget_bg
    ?canvas ?resize ?clip widgets =
  let rooms =
    List.map (fun wg ->
        let name = map_option name (fun s -> "Resident of [" ^ s ^ "]") in
        resident ?name ?h ~x:0 ~y:sep ?background:widget_bg ?canvas wg) widgets
  in
  flat ?name ~margins:sep ?align ?background ?canvas ?resize ?clip rooms

let h_center layout x0 w =
  match layout.content with
  | Resident _ -> ()
  | Rooms rs ->
     list_iter rs
       (fun r -> let w0 = width r in
                 let x = Draw.center x0 w w0 in
                 setx r x)

let h_align ~align layout x0 w =
  match layout.content with
  | Resident _ -> ()
  | Rooms rs ->
     list_iter rs
       (fun r -> let w0 = width r in
                 let x = Draw.align align x0 w w0 in
                 setx r x)

(* Create a tower from a list of rooms (this modifies the x/y pos of the
   rooms)*)
(* sep = vertical space between two rooms; NOT used below a room of height 0 *)
(* hmargin = horizontal margin (left and right). *)
(* vmargin = vertical margin (top and bottom). *)
let tower ?name ?sep ?margins ?hmargin ?vmargin ?align
    ?(adjust = Fit) ?background ?shadow ?canvas ?resize
    ?clip rooms =
  (* List.iter (set_canvas canvas) rooms; TODO *)
  let sep, hmargin, vmargin = match margins with
    | Some m ->
      if sep <> None || hmargin <> None || vmargin <> None
      then printd debug_user "Call to [tower]: the [margins] parameter is set \
                              and overrides [sep,hmargin,vmargin].";
      m,m,m
    | None -> default sep (Theme.room_margin / 2),
              default hmargin (Theme.room_margin),
              default vmargin (Theme.room_margin) in
  let rec loop list x y =
    match list with
    | [] -> if y = vmargin then (2*hmargin, 2*vmargin) (* empty list *)
      else (x, y - sep + vmargin)
    | r::rest ->
      setx r hmargin;
      sety r y;
      let hr = height r in
      loop rest (max x ((width r) + 2*hmargin))
        (if hr = 0 then y else y + sep + hr) in
  let w,h = loop rooms hmargin vmargin in
  let layout = create ~adjust ?name ?background ?shadow ?clip
      (geometry ~w ~h ()) (Rooms rooms) ?canvas in
  do_option align (fun align -> h_align ~align layout hmargin (w-2*hmargin));
  let resize = default resize
      (if clip = Some true then Resize.Disable else Resize.Default) in
  let () = if resize = Resize.Disable then List.iter disable_resize rooms
    else let resize_width = not (resize = Resize.Linear) in
      resize_tower ~resize_width ~hmargin ~vmargin ~sep ~align:(default align Draw.Min)
        layout in
  printd debug_custom "end of tower for %s" (sprint_id layout);
  layout

(* Construct a tower directly from a list of widgets that we convert to
   Residents. *)
let tower_of_w ?name ?(sep = Theme.room_margin) ?w ?align ?background ?widget_bg
      ?canvas ?resize ?clip widgets =
  let rooms =
    List.map (fun wg ->
        let name = map_option name (fun s -> "Resident of [" ^ s ^ "]") in
        resident ?name ?w ~x:sep ~y:0 ?background:widget_bg ?canvas wg) widgets
  in
  tower ?name ~margins:sep ?align ?background ?canvas ?resize ?clip rooms

(* compute the x,y,w,h that contains all rooms in the list *)
let bounding_geometry = function
  | [] -> printd debug_warning "Trying to find bounding_geometry of empty list";
    0,0,0,0
  | rooms ->
    let rec loop xmin ymin xmax ymax = function
      | [] -> (xmin, ymin, xmax - xmin + 1, ymax - ymin + 1)
      | room :: rest ->
        let x,y = getx room, gety room in
        loop
          (imin xmin x)
          (imin ymin y)
          (imax xmax (width room + x - 1))
          (imax ymax (height room + y - 1))
          rest in
    loop max_int max_int 0 0 rooms

module Grid = struct

  (* return a Selection.t corresponding to the vertical projections of the
     bounding boxes of the rooms in the house. *)
  let detect_rows ?(overlap = 1) house =
    let vranges =
      match house.content with
      | Resident _ -> [(0, height house)]
      | Rooms rooms -> List.map (fun r ->
          let y = gety r in
          (y, y + height r - overlap)) rooms in
    Selection.of_list vranges

  (* same for horizontal projections *)
  let detect_columns ?(overlap = 1) house =
    let hranges =
      match house.content with
      | Resident _ -> [(0, width house)]
      | Rooms rooms -> List.map (fun r ->
          let x = getx r in
          (x, x + width r - overlap)) rooms in
    Selection.of_list hranges

end

(* Make a house out of a unique room; usueful for clipping content; see also
   [make_clip] *)
let cover ?name room =
  create ?name ~clip:true room.geometry (Rooms [room])

(* Superpose a list of rooms without changing their relative (x,y) positions.
   Unless specified by ~w ~h, the resulting layout has the *size* of the total
   bounding box of all rooms. Its (x,y) *position* is such that, when displayed
   at this position, all rooms should be located at the positions they
   claimed. *)
(* TODO it seems that only the first one gets focus... *)
let superpose ?w ?h ?name ?background ?canvas ?(center=false)
    ?(scale_content=true) rooms =
  let x,y,bw,bh = bounding_geometry rooms in
  (* We translate the rooms: *)
  List.iter (fun r ->
      setx r (getx r - x);
      sety r (gety r - y)) rooms;
  let w = default w bw in
  let h = default h bh in
  if center then List.iter (fun r ->
      setx r (Draw.center (getx r) w (width r));
      sety r (Draw.center (gety r) h (height r))) rooms;
  let geometry = geometry ~x ~y ~w ~h () in
  if scale_content then scale_resize_list (w,h) rooms;
  (* else List.iter disable_resize rooms; *) (* already done by [setx] above *)
  create ?name ?background ?canvas geometry (Rooms rooms)

(** save the layout_id in the user event *)
(* not used anymore *)
let save_to_event_OLD event room =
  Sdl.Event.(set event Trigger.room_id room.id)
(* TODO: since we only use one global event for mouse_enter or mouse_leave, it
   would be more efficient to store directly the room in a global variable,
   rather that storing the id, and then painfully search the room corresponding
   to id... *)

(** ask all the subwidgets to update themselves. *)
(* in fact, this just send the redraw_event, which ask for redrawing the whole
   window. Thus, it would be enough to ask this to only one widget of the layout
   (since all widgets must be in the same window) *)
let rec ask_update room =
  match room.content with
  | Resident w -> Widget.update w
  | Rooms list -> List.iter ask_update list

(** animations: *)
(* animations with Anim are deprecated, use Avar instead *)

let animate_x room x =
  let g = room.geometry in
  Avar.stop g.x;
  room.geometry <- { g with x }

let animate_y room y =
  let g = room.geometry in
  Avar.stop g.y;
  room.geometry <- { g with y }

(* Warning: Animations do NOT trigger the resize function *)
let animate_w room w =
  let g = room.geometry in
  Avar.stop g.w;
  room.geometry <- { g with w }

let animate_h room h =
  let g = room.geometry in
  Avar.stop g.h;
  room.geometry <- { g with h }

let animate_voffset room voffset =
  let g = room.geometry in
  let avar = Var.get g.voffset in
  let is_current = Avar.started avar && not (Avar.finished avar) in
  Avar.stop avar;
  Var.set g.voffset voffset;
  (* if the animation was already running we need to start immediately,
     otherwise the value that we set here will be valid only for the next
     iteration, which may cause non-immediate transitions: useful ???*)
  if is_current then ignore (get_voffset room)

let animate_alpha room alpha =
  let g = room.geometry in
  Avar.stop g.transform.alpha;
  room.geometry <- { g with transform = { g.transform with alpha }};
  ask_update room

let animate_angle room angle =
  let g = room.geometry in
  Avar.stop g.transform.angle;
  room.geometry <- { g with transform = { g.transform with angle }}

let stop_pos room =
  printd debug_graphics "Stop position animation for layout %s." (sprint_id room);
  let g = room.geometry in
  Avar.stop g.x;
  Avar.stop g.y

(** get desired room (relative) geometry after applying animation *)
let geom r =
  let g = r.geometry in
  to_current_geom g (* the calculation is there *)


(** some predefined animations: *)
(* warning, these animations can be set on-the-fly, so be careful with other
   existing animations *)
let default_duration = 300

(* add a show animation (vertical sliding) to the room; however: *)
(* 1. if the room is already animated, we replace the old animation by the show,
   and the duration is reduced proportionally to the current voffset of the old
   animation *)
(* 2. if the room is shown and without animation, we do nothing *)
let show ?(duration=default_duration) ?from room =
  if room.show && Avar.finished (Var.get room.geometry.voffset)
  then if !debug then begin assert (not room.removed);
      printd (debug_board + debug_warning)
        "Room %s is already shown, we don't run the show animation"
        (sprint_id room)
    end else ()
  (* it is ok to show a room that currently is performing a hide animation. *)
  else begin
    let clip = ref false in
    let init () =
      clip := room.clip;
      set_clip room
    in
    (* it is important to do this AFTER the ending() of the previous
       animation. *)
    let h = height room in
    if not room.show && (get_voffset room <> -h)
    then (printd debug_warning
            "Using a 'show' animation on a room that was not previously in a \
             'hidden' state. Forcing voffset to %d." (-h);
          set_voffset room (-h));
    let h, duration = match from with
      | None ->
        let current_vo = get_voffset room in
        let d' = if h = 0 then 0 else abs ((current_vo * duration) / h) in
        current_vo, d'
      | Some Avar.Bottom -> h, duration
      | Some Avar.Top -> -h, duration
      | Some _ -> printd debug_board "Layout.show direction not implemented";
        h, duration in
    let ending () =
      printd debug_board "End of show for %s" (sprint_id room);
      room.clip <- !clip in
    let voffset = Avar.show ~init ~ending ~duration h 0 in
    animate_voffset room voffset;
    rec_set_show true room;
    room.removed <- false
  end

(* add a hide animation to the room *)
let hide ?(duration=default_duration) ?(towards = Avar.Bottom) room =
  if (not room.show) (*&& Avar.finished (Var.get room.geometry.voffset)*) then ()
  else begin
      let clip = ref false in
      let init () =
        clip := room.clip;
        set_clip room
      in
      let h = height room in
      let current_vo = get_voffset room in
      let d' = abs ((h + current_vo)*duration) / (abs h+1) in (* DEBUG *)
      let vo = match towards with
        | Avar.Bottom -> h
        | Avar.Top -> -h
        | _ -> printd debug_board "Layout.show direction not implemented"; h in
      let ending _ =
        printd debug_board "End of hide";
        room.clip <- !clip;
        rec_set_show false room;
        remove room in
      (* WARNING: if the room contains subrooms with animations, they will remain
       forever because a layout with show=false is not displayed and hence not
       updated: the anim is not removed. Even more so with Avar. Thus compute
       has_anim during display ? *)
      let voffset = Avar.show ~init ~ending ~duration:d' current_vo vo in
      animate_voffset room voffset
    end

(** scrolling to a particular vertical position y, for a prescribed duration. *)
(* y should be between 0 and the total height *)
(* Warning: in principle, room.house.clip should be set to true for this *)
let scroll_to ?(duration=1000) (*?(weight=0.5)*) y room =
  match room.house with
  | None -> printd debug_error "Cannot scroll the top layout (need a house)"
  | Some house ->
    let current_vo = get_voffset house in
    let y' = max 0 (min y (height room - height house)) in
    let duration = duration * (abs(current_vo + y') + 1) / (abs (current_vo + y) + 1) in
    printd debug_graphics "Scroll room#%u in house #%u, from %d to %d, duration=%d"
      room.id house.id current_vo (-y') duration;
    let voffset = Avar.fromto ~duration current_vo (-y') in
    (* TODO: make a special animation when reaching bounds *)
    animate_voffset house voffset

(** relative scrolling *)
(* the specifications are: the scrolling must be continuous (no jump), and n
   calls to (scroll dy) should end up in the same position as (scroll (n*dy)),
   even if they are triggered before the previous animation is not finished. In
   case of rapid mouse wheel events, it is not obvious to have the scrolling
   look smooth. Thus we add the following spec: the scroll curve should be
   epsilon-close to the h-translated starways curve that would be obtained with
   immediate jumps, where epsilon is the jump height (50 for scroll wheel) and h
   is a small time amout, that we choose to be 2*dt here (it should be less than
   duration, otherwise the second part of the animation g2 is never executed) *)
let scroll_delay = ref 0.5
let scroll_old2 ?(duration=default_duration) dy room =
  do_option room.house (fun house ->
      let current_vo = get_voffset house in
      let avar = Var.get house.geometry.voffset in
      let jump_vo = Avar.final_value avar in
      let final_vo = - (max 0 (min (dy - jump_vo) (height room - height house))) in
      let duration = duration * (abs(final_vo - current_vo) + 1) / (abs (jump_vo - dy - current_vo) + 1) in
      let dt = Avar.progress avar in
      printd debug_graphics "Scroll room #%u: dy=%d, current_vo=%d, jump_vo=%d, final_vo=%d, duration=%d, progress=%f"
        room.id dy current_vo jump_vo final_vo duration dt;
      let voffset = if current_vo = jump_vo || duration = 0
        then (scroll_delay := 0.5;
              Avar.fromto ~duration current_vo final_vo)
        else (* the previous animation was not finished, we need to catch up *)
          let dv = final_vo - jump_vo in (* warning, opposite sign to dy *)
          let h = if dt < !scroll_delay
            then scroll_delay := max 0.3 !scroll_delay /. 1.5
            else if dt > 1.5 *. !scroll_delay
            then scroll_delay := min 0.5 (1.5 *. !scroll_delay);
            !scroll_delay in
          let slope = (float (jump_vo - current_vo)) *. (1. -. h) /.
                      (h *. (float (final_vo - jump_vo))) in
          (* rem we may have (slope < 0.) in case of changing direction *)
          let g1 = Avar.affine (float current_vo) (float jump_vo) in
          let g2 u = Avar.initial_slope ~slope:(max 1. (abs_float slope)) u
                     |> Avar.affine (float jump_vo) (float final_vo) in

          let update _ u = Avar.concat ~weight:h g1 g2 u
                           |> round in
          printd debug_graphics "Scroll: dv=%d, h=%f slope=%f" dv h slope;
          Avar.create ~duration ~update current_vo in
      animate_voffset house voffset)

let last_time = ref (Time.now ());; (* TODO replace this by the time of the Avar *)
let scroll ?(duration=default_duration) dy room =
  do_option room.house (fun house ->
      let current_vo = get_voffset house in
      let avar = Var.get house.geometry.voffset in
      let jump_vo = Avar.final_value avar in (* à vérifier *)
      let final_vo = - (max 0 (min (dy - jump_vo) (height room - height house))) in
      let elapsed = Time.(now () - !last_time) in
      let duration =
        if (elapsed = 0)
        || (elapsed > 300)
        || current_vo = final_vo
        then duration
        else let speed = (float (-dy)) /. (float elapsed) in
          (* = the speed that user expects to have, because she's rolling the
             mouse wheel *this* fast. In pixels per ms. Now we need to adjust
             the duration so that the expected final value is indeed reached at
             that speed. *)
          let final = current_vo + (round (speed *. (float duration))) in
          if final = current_vo then duration
          else abs (duration * (final_vo - current_vo) / (final - current_vo)) in
      printd debug_graphics
        "Scroll room #%u: current:%d, final_vo=%d, duration=%d"
        room.id current_vo final_vo duration;
      let voffset = Avar.fromto ~duration current_vo final_vo in
      last_time := Time.now ();
      animate_voffset house voffset)

let scroll_old ?(duration=300) dy room =
  do_option room.house (fun house ->
      Avar.finish (Var.get house.geometry.voffset); (* TODO: do something smoother *)
      let previous_vo = get_voffset house in
      printd debug_graphics "Scroll room #%u, dy=%d, previous_vo=%d" room.id dy previous_vo;
      scroll_to ~duration (dy - previous_vo) room)

(* find a parent whose house has the 'clip' property *)
let rec find_clip_house room =
  match room.house with
  | None -> None
  | Some h ->
    if h.clip then Some room
    else find_clip_house h

(** add fade_in transform to the existing animation of the room *)
let fade_in ?duration ?(from_alpha = 0.) ?(to_alpha = 1.) room =
  let alpha = Avar.fade_in ?duration ~from_alpha ~to_alpha () in
  animate_alpha room alpha

(** add fade_out transform to the existing animation of the room *)
(* WARNING: fading out to alpha=0 results in a completely transparent room, but
   the room is *still there*. (it's not "hidden"). Which means it can still get
   mouse focus. If you want to hide it, then use hide=true *)
let fade_out ?duration ?from_alpha ?(to_alpha = 0.) ?(hide=false) room =
  let from_alpha = default_lazy from_alpha (lazy (get_alpha room)) in
  let ending _ =
    printd debug_board "End of complete fade_out => hiding room";
    remove room;
    (* One could also use: Trigger.push_from_id Trigger.E.mouse_motion room.id; *)
    (* This forces the board to recompute the new mouse focus. *)
    rec_set_show false room in
  let ending = if hide then Some ending else None in
  let alpha = Avar.fade_out ?duration ?ending ~from_alpha ~to_alpha () in
  animate_alpha room alpha

(* angle in degree *)
(* WARNING: it's not a global rotation. All widgets in the room will rotate
   separately about their own center *)
(* If you want to rotate a complete layout, use a Snapshot *)
let rotate ?duration ?(from_angle = 0.) ~angle room =
  let angle = Avar.fromto_float ?duration from_angle (from_angle +. angle) in
  animate_angle room angle

(* Zoom works for Resident, but for a general Rooms it will not work *)
(* moreover, only few resident widgets will be ok. (image, box...) *)
(* In order to zoom a general layout, use a Snapshot *)
(* TODO: add zoom center *)
let zoom_x ?duration ~from_factor ~to_factor room =
  let w0 = round (float (width room) *. from_factor) in
  let w1 = round (float (width room) *. to_factor) in
  let w = Avar.fromto ?duration w0 w1 in
  printd debug_graphics "ZOOM width from %d to %d" w0 w1; (* DEBUG *)
  animate_w room w

let zoom_y ?duration ~from_factor ~to_factor room =
  let h0 = round (float (height room) *. from_factor) in
  let h1 = round (float (height room) *. to_factor) in
  let h = Avar.fromto ?duration h0 h1 in
  printd debug_graphics "ZOOM height from %d to %d" h0 h1; (* DEBUG *)
  animate_h room h

let zoom ?duration ~from_factor ~to_factor room =
  zoom_x ?duration ~from_factor ~to_factor room;
  zoom_y ?duration ~from_factor ~to_factor room

(** oscillate (for fun) *)
let oscillate ?(duration = 10000) ?(frequency=5.) amplitude room =
  let x = Avar.oscillate ~duration ~frequency amplitude (getx room) in
  animate_x room x

(** add a slide_in animation to the room *)
let slide_in ?duration ?from ?dst room =
  let dst = default dst room in
  let x,y = Avar.slide_in ?from ?duration ~size:(get_size dst)
              ~pos:(getx room, gety room) () in
  animate_x room x;
  animate_y room y

(** translation animation *)
let slide_to ?(duration=default_duration) room (x0,y0) =
  let x1 = getx room in
  let y1 = gety room in
  let x = Avar.fromto ~duration x1 x0 in
  let y = Avar.fromto ~duration y1 y0 in
  animate_x room x;
  animate_y room y

(** follow mouse animation. *)
(* Note that the window is not available before running the layout... *)
let mouse_motion_x ?dx ?modifier room =
  let x0 = ref 0 in (* we store here the dist between mouse and room *)
  let init () =
    x0 := default_lazy dx
        (lazy (fst (Mouse.window_pos (window room)) - xpos room)) in
  let update _ _ =
    let x = fst (Mouse.window_pos (window room)) - (x_origin room) - !x0 in
    match modifier with
    | None -> x
    | Some f -> x + f x in
  Avar.create ~duration:(-1) ~update ~init 0

let mouse_motion_y ?dy ?modifier room =
  let y0 = ref 0 in
  let init () =
    y0 := default_lazy dy
        (lazy (snd (Mouse.window_pos (window room)) - ypos room)) in
  let update _ _ =
    let y = snd (Mouse.window_pos (window room)) - (y_origin room) - !y0 in
    match modifier with
    | None -> y
    | Some f -> y + f y in
  Avar.create ~duration:(-1) ~update ~init 0

(* set the origin of the room at the mouse position - (dx,dy) *)
(* if dx or dy is not specified, the default is the current distance between
   room and mouse *)
(* modifierx and modifiery are executed continuously during the animation, and
   return an integer offset, which can typically be used to obtain a "magnetic"
   effect. See examples/displays *)
(* TODO merge into unique function "action" *)
let follow_mouse ?dx ?dy ?modifierx ?modifiery room =
  let x = mouse_motion_x ?dx ?modifier:modifierx room in
  let y = mouse_motion_y ?dy ?modifier:modifiery room in
  animate_x room x;
  animate_y room y

(* TODO let insulate room =*)

(* Clip a room inside a smaller container and make it scrollable, and optionally
   add a scrollbar widget (which should appear only when necessary). Currently
   only vertical scrolling is implemented. The [on_scroll] function is called
   anytime the scroll is modified or accessed, with the voffset as parameter. *)
let make_clip
    ?w ?(scrollbar = true) ?(scrollbar_inside = false)
    ?(scrollbar_width = 10) ?on_scroll ~h room =
  (* iter_rooms disable_resize room; *)
  let name = (default room.name "") ^ ":clip" in
  if w <> None
  then printd debug_error "Horizontal scrolling is not implemented yet";
  let w = default w (width room) in
  let x0 = getx room in
  let y0 = gety room in
  sety room 0;
  let active_bg = Widget.empty ~w:(width room) ~h:(height room) () in
  (* We add an invisible box (over the room) to make the whole area selectable
     by the mouse focus. Otherwise, only the parts of the room that contain a
     widget will react to the *mouse wheel* event. Of course, if the room is
     full of widgets, this is superfluous... *)
  let layer = get_layer room in
  let hopt = match room.house with
    | Some h ->
      printd (debug_user + debug_warning + debug_board)
        "Applying [make_clip] to a room %s that already belongs to a house \
         %s. We try to relocate the container in that house and hope for the \
         best." (sprint_id room) (sprint_id h);
      detach room; Some h
    | None -> None in
  let container = tower ~margins:0 ~clip:true
      [superpose [resident_with_layer ~layer active_bg; room]] in
  (* The container should be a room with a unique subroom (and the active
     background); the subroom can then be scrolled with respect to the container
  *)
  set_size container ~w ~h;
  do_option hopt (fun dst -> attach ~dst container);

  let result =
    if scrollbar
    then begin
      (* We first initialize the bar layout with a dummy widget, so that the
         var is able to use it. This is only useful if the height of the
         container is modified after creation, for instance when the user
         resizes the window. *)
      let bar = resident_with_layer ~layer
          ~background:(color_bg Draw.(lighter scrollbar_color))
          (Widget.empty ~w:10 ~h:10 ()) in
      (* The scrollbar is a slider. Its Tvar takes the voffset value into the
         slider value, between 0 and (height room - height container). 0
         corresponds to the bottom position of the slider, so this means the
         *largest* scroll (voffset is the most negative). *)
      let old_vo = ref (get_voffset container) in
      let var = Tvar.create container.geometry.voffset
          ~t_from:(fun vo ->
              let vo = Avar.get vo in
              if vo <> !old_vo then do_option on_scroll (apply vo);
              old_vo := vo;
              let dh = height room - height bar in
              if dh <= 0 then 0 (* then the bar should be hidden *)
              else dh + vo)
          ~t_to:(fun v ->
              let dh = height room - height bar in
              let v = imin v dh |> imax 0 in
              let vo = height bar - height room + v in
              if vo <> !old_vo then do_option on_scroll (apply vo);
              old_vo := vo;
              Avar.var vo) in
      let wsli = Widget.slider ~kind:Slider.Vertical ~length:h
          ~thickness:scrollbar_width ~tick_size:(h * h / (height room))
          ~var (imax 0 (height room - h)) in
      change_resident bar wsli;
      if h >= (height room) then hide ~duration:0 bar;
      let r = if scrollbar_inside
        then (setx bar (w - width bar);
              set_layer bar (Chain.insert_after
                               (Chain.last (get_layer container))
                               (Draw.new_layer ()));
              (* TODO: is this a bit too much ?? We just want to make sure the
                 scrollbar gets mouse focus. *)
              superpose ~name [container; bar])
        else flat ~name ~margins:0 [container; bar] in
      disable_resize bar;
      (* We register a resize function that simultaneously sets the container
         and the bar sizes. It will hide the bar when the container is large
         enough to display the whole content. *)
      container.resize <- (fun (w,h) ->
          let open Resize in
          set_height bar h;
          if scrollbar_inside then set_size container ~w ~h
          else begin
            set_size container ~w:(w - width bar) ~h;
            setx bar (w - width bar)
          end;
          let dh = height room - height bar in
          let sli = Widget.get_slider wsli in
          if dh >= 1 then Slider.set_max sli dh
          else set_voffset container 0;
          (* Warning: we set the voffset directly, because when the bar is
             hidden, the Tvar will never be activated -- except if the user
             scrolls with the mouse. *)
          let h = height bar in
          if height room <> 0
          then Slider.set_tick_size sli
              (imax (Slider.min_tick_size sli) (h * h / (height room)));
          let v = Slider.update_value sli; Slider.value sli in
          if v < 0 then Slider.set sli 0;
          if dh <= 0
          then (if is_shown bar then hide ~duration:0 bar)
          else (if not (is_shown bar) then show ~duration:0 bar));
      r
    end
    else container in

  sety result y0;
  setx result x0;
  (* We copy the shadow. TODO: this has no effect at the moment, because of the
     'clip' flag, the layout is sharply clipped to its bounding box when
     rendering, so the shadow is hidden. *)
  let shadow = room.shadow in
  result.shadow <- shadow;
  room.shadow <- None;
  result

let relayout createfn ?(duration=200) layout =
  let rooms = get_rooms layout in
  let old_pos = List.map (fun r -> getx r, gety r) rooms in
  (* [createfn] will change the rooms positions, but warning, this may also set
     a new house, we have to set it back. *)
  let tmp = createfn rooms in
  tmp.content <- Rooms [];
  List.iter (fun r -> r.house <- Some layout) rooms;
  if duration <> 0 then
    List.iter2 (fun (oldx,oldy) room ->
        let newx = getx room in
        if oldx <> newx
        then animate_x room (Avar.fromto ~duration oldx newx);
        let newy = gety room in
        if oldy <> newy
        then animate_y room (Avar.fromto ~duration oldy newy))
      old_pos rooms

(* adjust an existing layout to arrange its rooms in a "flat" fashion, as if
   they were created by Layout.flat. Will be animated if duration <> 0 *)
let reflat  (*?(sep = Theme.room_margin / 2)*) ?align
    ?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin) ?margins =
  relayout (fun rooms -> flat ~name:"reflat_tmp" ~hmargin ~vmargin ?margins ?align rooms)

(* same as reflat but with Layout.tower *)
let retower (*?(sep = Theme.room_margin / 2)*) ?align
    ?(hmargin = Theme.room_margin) ?(vmargin = Theme.room_margin) ?margins =
  relayout (fun rooms -> tower ~name:"retower_tmp" ~hmargin ~vmargin ?margins ?align rooms)

(* Typically in a tower, enlarge all rooms to have the width of the house.  This
   is not recursive: only rooms of depth 1. This disables the rooms [resize]
   fields. *)
let expand_width house =
  let w = width house in
  iter_rooms (fun room ->
      let x = getx room in
      if w-x < 1 then printd debug_warning
          "Cannot expand_width because house x position is larger than width.";
      set_width room (w-x)) house

(* Replace "room" by "by" in lieu and place of the initial room. No size
   adjustments are made (WARNING the [current_geom] of the new room will be
   computed only after the first actual display required by the Redraw event
   TODO: do it?). Of course this is dangerous, because it modifies both the
   house and "by". See also [add_room]. Returns [true] if successful. *)
(* TODO copy old (x,y) position *)
(* If [keep_layer] is not specified (or false), and if the new room and the old
   room belong to different layers (of the same stack), then the new room [by]
   and all of its content will be moved to the layer where the original [room]
   belongs. *)
let replace_room_with_layer ?(keep_layer=false) ~by room =
  match room.house with
  | None ->
    printd (debug_error + debug_user)
      "Cannot use \"replace_room\" because room %s does not belong to a house."
      (sprint_id room);
    false
  | Some h when ok_to_add_room ~dst:h by ->
    printd (debug_warning + debug_board) "Replacing room %s by room %s inside %s."
      (sprint_id room) (sprint_id by) (sprint_id h);
    check_layer_error by h;
    if same_stack room by && not (same_layer room by) then begin
      printd debug_warning "The replacement room does not belong to the same \
                            layer as the original room.";
      if not keep_layer then begin
        printd debug_warning "[replace_room]: we fix the layer. Use the \
                              [keep_layer] option to override.";
        rec_set_layer by (room.layer)
      end
    end;
    let rooms = get_rooms h in
    detach room;
    attach ~dst:h by;
    h.content <- Rooms (list_replace (equal room) rooms by);
    let x = getx by + xpos h in
    let y = gety by + ypos h in
    by.current_geom <- { by.current_geom with x; y};
    true
  | _ -> printd (debug_board + debug_error) "Cannot replace room %s by %s"
           (sprint_id room) (sprint_id by);
    false

let replace_room ~by room =
  replace_room_with_layer ~keep_layer:false ~by room

(* move a room to a new house, trying to keep the same visual
   position. Optionnally adding a scrollbar (in which case the returned layout
   is not the same as the original one). *)
(* WARNING this doesn't take voffset into account, so it won't work if a 'hide'
   animation was used to the room. *)
let relocate ~dst ?(scroll=true) ?(auto_scale=false) room =
  (* TODO check they have the same top_house? *)
  let x0,y0 = compute_pos dst in
  let x1,y1 = compute_pos room in
  (* 'pos_from' won't work here because this is called (by Select) before rooms
     positions are computed... *)

  if room.house <> None then detach room;
  let room2 = if not scroll then room
    else begin
      let y2 = y1-y0 + height room in
      printd debug_board "Relocate room : y2=%i y1=%i y0=%i room=%i \
                          dst=%i" y2 y1 y0 (height room) (height dst);
      (* if y2 <= height dst then room *)
      (* else *)  (* If scroll=true it's good to use make clip anyway, just in
                     case the size of the house shrinks. *)
      make_clip ~h:(height dst - y1 + y0) ~scrollbar_inside:true
        ~scrollbar_width:4 room
    end in

  (* We add it to the dst *)
  add_room ~dst room2;
  setx room2 (x1-x0);
  sety room2 (y1-y0);
  if auto_scale then scale_resize room2;
  room2

(********************)
(** display section *)
(********************)

let debug_box ~color room x y =
  let w,h = Draw.scale_size (get_size room) in
  let x,y = Draw.scale_pos (x,y) in
  let bg = if room.mouse_focus then Some (Draw.lighter color) else None in
  Draw.rect_to_layer ?bg ~color (get_canvas room) (get_layer room)
      (x,y) w h

let scale_rect rect =
  Sdl.Rect.(create
              ~x:(Theme.scale_int (x rect))
              ~y:(Theme.scale_int (y rect))
              ~h:(Theme.scale_int (h rect))
              ~w:(Theme.scale_int (w rect)))

let scale_clip clip =
  map_option clip scale_rect

let show_keyboard_focus room _transform rect =
  (* TODO use transform *)
  let layer = get_layer room in
  let canvas = get_canvas room in
  let blits = Draw.box_shadow ~offset:(0,0) ~size:(Theme.scale_int 3) ~fill:false
      canvas layer (scale_rect rect) in
  List.iter Draw.blit_to_layer blits

(* Display a room: *)
(* this function sends all the blits to be displayed to the layers, *)
(* it does not directly interact with the renderer. *)
(* pos0 is the position of the house containing the room *)
let display ?pos0 room =
  let x0,y0 = match pos0 with
    | None -> house_pos room
    | Some p -> p in
  let rec display_loop x0 y0 h0 clip0 tr0 r =
    (* clip contains the rect that should contain the current room r. But of
       course, clip can be much bigger than r. *)
    if not r.show then ()
    else begin
      let g = geom r in
      let x = x0 + g.x
      and y = y0 + g.y + h0
      and voffset = g.voffset in
      (* update current position, independent of clip *)
      r.current_geom <- { g with x; y };
      (* print  "ALPHA=%f" (Avar.old room.geometry.transform.alpha);*)
      let rect = Sdl.Rect.create ~x ~y ~w:g.w ~h:g.h in

      (* if there is a nonzero offset, we perform a new clip : this is used for
         "show/hide" animation *)
      (* TODO clip should be enlarged in case of shadow *)
      let clip = if (*voffset = 0*) not r.clip || !no_clip then clip0
        else Draw.intersect_rect clip0 (Some rect) in
      let sclip = scale_clip clip in
      match clip with
      | Some clip_rect when not (Sdl.has_intersection clip_rect rect) ->
        (r.hidden <- true;
         printd debug_warning "Room #%u is hidden (y=%d)" r.id y)
      (* Because of clip, the rendered size can be smaller than what the geom
         says. *)
      (* If the clip is empty, there is nothing to display. Warning: this means
         that all children will be hidden, even if they happen to pop out of
         this rect. *)
      | _ -> begin
          r.hidden <- false;
          let transform =
            let tr = get_transform r in
            (* printd debug_board "TRANSFORM alpha=%f" tr.Draw.alpha; *)
            let open Draw in
            (* printd debug_board "COMPOSED TRANSFORM alpha=%f" (tr.alpha *. tr0.alpha); *)
            (*{ tr0 with alpha = tr0.alpha *. tr.alpha } in*)
            (* TODO: compose also rotations with centres, flips !! *)
            compose_transform tr0 tr in

          (* background (cf compute_background)*)
          let bg = map_option r.background (fun bg ->
              let box = match bg with
                | Style style ->
                  (* let c = Draw.random_color () in *)  (* DEBUG *)
                  (* let style = Style.create ~background:(Style.Solid c)
                   *     ?shadow:r.shadow () in *)
                  let b = Box.(create ~width:g.w ~height:g.h ~style ()) in
                  r.background <- (Some (Box b));
                  b
                | Box b -> b in
              let blits = Box.display (get_canvas r) (get_layer r) box
                  Draw.(scale_geom {x; y; w = g.w; h = g.h;
                                    voffset = - voffset}) in
              blits) in
          (* !!! in case of shadow, the blits contains several elements!! *)

          if !debug && r.keyboard_focus = Some true
          then show_keyboard_focus r transform rect;

          begin match r.content with
            | Rooms h ->
              (* We only draw the background. Make sure that the layer of the
                 room r is at least as deep as the layers of the Rooms h *)
              do_option bg
                (List.iter
                   (fun blit ->
                      let open Draw in
                      let t = compose_transform transform blit.transform in
                      blit_to_layer { blit with clip = sclip; transform = t }));
              if !draw_boxes then begin
                let rect = debug_box ~color:(0,0,255,200) r x y in
                let open Draw in
                let t = compose_transform transform rect.transform in
                blit_to_layer { rect with clip = sclip; transform = t }
              end;
              List.iter (display_loop x y voffset clip transform) h
            | Resident w ->
              let blits = Widget.display (get_canvas r) (get_layer r) w
                  Draw.({x; y; w = g.w; h = g.h; voffset}) in
              let blits = match bg with
                | None -> blits
                | Some b -> List.rev_append b blits in

              (* debug boxes *)
              let blits = if !draw_boxes
                then
                  let color = (255,0,0,200) in
                  let rect = debug_box ~color r x y in
                  rect :: blits
                else blits in

              List.iter
                (fun blit ->
                   let open Draw in
                   let t = compose_transform transform blit.transform in
                   let clip = sclip in
                   blit_to_layer { blit with clip; transform = t }) blits
          end;
          if !draw_boxes (* we print the room number at the end to make sure
                            it's visible *)
          then let label = Label.create ~size:7 ~fg:(Draw.(transp blue))
                   (sprint_id r) in
            let geom = Draw.scale_geom {Draw.x; y; w=g.w+1; h=g.h+1; voffset} in
            let blits = Label.display (get_canvas r) (get_layer r) label geom in
            List.iter Draw.blit_to_layer blits;
            List.iter Draw.unload_blit blits
        end
    end in
  display_loop x0 y0 0 None (Draw.make_transform ()) room

(* the display function we export *)
(* NO: we need pos for snapshot.ml *)
(* let display r : unit = display r *)

let get_focus room =
  room.mouse_focus

let set_focus room =
  room.mouse_focus <- true

let unset_focus room =
  room.mouse_focus <- false

let set_cursor roomo =
  let cursor = match roomo with
    | None -> go (Draw.create_system_cursor Sdl.System_cursor.arrow)
    | Some room -> match room.content with
      | Rooms _ -> go (Draw.create_system_cursor Sdl.System_cursor.arrow)
      | Resident w -> Widget.get_cursor w in
  Sdl.set_cursor (Some cursor)

(* comme display sauf qu'on ne trace que si nécessaire *)
(* not used anymore *)
let update_old room =
  let rec update_loop x0 y0 h0 clip0 room =
    if not room.show then ()
    else begin
      let g = geom room in (* attention, ça met anim <- None si anim = finished... *)
      let x = x0 + g.x
      and y = y0 + g.y + h0 in
      let clip = if g.voffset = 0 || !no_clip then clip0
        else Draw.intersect_rect clip0 (Some (Sdl.Rect.create ~x ~y ~w:g.w ~h:g.h)) in
      room.current_geom <- current_geom ~x ~y ~w:g.w ~h:g.h ();
      match room.content with
      | Rooms h -> List.iter (update_loop x y g.voffset clip) h
      | Resident w -> if not (Widget.is_fresh w) then begin
          (* if !draw_boxes then Draw.box (renderer room) ~bg:(200,10,20,50) x y g.w g.h; *)
          (* TODO background , transform *)
          let blits = Widget.display (get_canvas room) (get_layer room) w
              {Draw.x = x; y; w = g.w; h = g.h; voffset = g.voffset} in
          List.iter (fun blit -> Draw.(blit_to_layer { blit with clip })) blits
        end
    end in
  let x0,y0 = house_pos room in
  update_loop x0 y0 0 None room

(** check is the room has some non-fresh components. *)
(* optimize (Bogue)? *)
let rec is_fresh room =
  match room.content with
  | Rooms list -> let rec loop = function
      | [] -> true
      | r::h -> if not (is_fresh r) then false
        else loop h in
    loop list
  | Resident w -> Widget.is_fresh w

let room_has_anim room =
  Avar.has_anim room.geometry.transform.alpha ||
  Avar.has_anim room.geometry.transform.center ||
  Avar.has_anim room.geometry.transform.flip ||
  Avar.has_anim room.geometry.transform.angle ||
  List.fold_left (fun b v -> b || (Avar.has_anim v))
    false (get_int_avars room)

(* optimize (Bogue)? *)
(* TODO one could transfer it into Layout.display, which would return the anim
   status of what was displayed (and not what was hidden...) *)
let rec has_anim room =
  if !debug && (not room.show) && (room.hidden) && (room_has_anim room)
  then printd debug_error "Room %s has unfinished animation but it is not shown."
      (sprint_id room);
  room.show && (not room.hidden) &&
  (room_has_anim room ||
   match room.content with
   | Rooms list -> List.fold_left (fun b r -> b || (has_anim r)) false list
   | Resident _ -> false)

(* Flip buffers. Here the layout SHOULD be the main layout (house) of the
   window. Only one canvas/renderer is used, the one specified by the layout. *)
let flip ?(clear=false) ?(present=true) layout =
  printd debug_graphics "flip layout %s" (sprint_id layout);
  (* go (Sdl.set_render_target (renderer layout) None); *)
  if clear then Draw.clear_canvas (get_canvas layout);
  printd debug_graphics "Render layers";
  Var.protect_do Draw.current_layer (fun () ->
      (* : we assume that the layout layer is in the same component as the
         current_layer... TODO do better *)
      Draw.render_all_layers (get_layer layout));
  if present then begin
    printd debug_graphics "Present";
    Draw.(sdl_flip (renderer layout))
  end

(* prerender the layout to the layers *)
let render layout =
  (* let renderer = renderer layout in *)
  (* go (Sdl.render_set_clip_rect renderer None); *)
  (* Draw.(set_color renderer (opaque black)); *)
  (* go (Sdl.render_clear renderer); *)
  (* Draw.clear_canvas (get_canvas layout); *)
  (* We should not clear the canvas here, since all rendering is done at the end
     of the main loop, with flip *)
  if Draw.window_is_shown (window layout) then display layout
  else printd debug_board "Window (layout #%u) is hidden" layout.id

(* The function to call when the window has been resized *)
let resize_from_window ?(flip=true) layout =
  let top = top_house layout in
  if not (equal layout top)
  then printd debug_error
      "The layout for resizing window should be the top layout";
  let w,h = Draw.get_window_size (window top)
            |> Draw.unscale_pos in
  let w', h' = get_size top in
  if (w',h') <> (w,h)
  then begin
    (* TODO in rare occasions, it might happen that this test is different
       from get_physical_size top <> Draw.get_window_size win*)
    printd debug_graphics "Resize window layout %s: (%d,%d) --> (%d,%d)"
      (sprint_id layout) w' h' w h;
    set_size ~keep_resize:true(* TODO:REALLY? *) ~check_window:false top ~w ~h;
    Draw.update_background (get_canvas top);
    (* layout.resize (w,h); *) (* redundant ? A REOIR*)
    if flip then Draw.sdl_flip (renderer top)
  end
(* : somehow we need this intermediate flip so that the renderer takes into
    account the new size. Otherwise texture are still clipped to the old
    size... On the other hand it might flicker if triggered too quickly *)
(* fit_content layout;;*) (* not useful *)

(** initialize SDL if necessary and create a window of the size of the layout *)
let make_window ?window layout =
  printd debug_graphics "Make window";
  let top = top_house layout in
  if not (equal layout top)
  then printd debug_error
      "  The layout for creating a window should be the top layout";
  Draw.video_init (); (* this will compute the scale. If we don't do this here,
                         the size below will be (0,0). *)
  let w,h = get_physical_size top in
  let wmax, hmax = 4096, 4096 in
  (* = TODO ? instead clip to ri_max_texture_width,ri_max_texture_height ? *)
  if wmax < w || hmax < h
  then printd debug_error
      "  The layout has size (%u,%u), which exceeds the max size (%u,%u)."
      w h wmax hmax;
  let w = min w wmax in
  let h = min h hmax in
  let x,y = get_window_pos layout in
  let canvas = Draw.init ?window ?name:layout.name ?x ?y ~w ~h () in
  global_set_canvas top canvas

(* adjust the window size to the top layout *)
(* This should be enforced all the time *)
(* this is not executed immediately, but sent to Sync *)
(* TODO move this directly to the render loop, since it has to be done anyway,
   and should not be done more than once per step. *)
(* TODO combine with [adjust_window_size] *)
let adjust_window ?(display=false) layout =
  Sync.push (fun () ->
      let top = top_house layout in
      if not (equal layout top)
      then printd debug_error
          "The layout for resizing window should be the top layout";
      let w,h = get_physical_size top in
      let win = window top in
      printd debug_graphics "SDL set window size %d x %d" w h;
      Draw.set_window_size win ~w ~h;


      (* resize ~flip:display top; *)
      (* : of course, top didn't really change size, but somehow the texture was
         clipped to the old window size, and if we don't update it, the previous
         clipped texture is stretched to the new window size. *)
      (* render top; *)
      (* flip top; *)
      (* Draw.(flip top.canvas.renderer); *)

      (* Now we render and flip. This is not strictly necessary, as it will surely
         be done by the main loop anyway. But it doesn't hurt to do it twice... *)
      (* it should not be done if the window is hidden, because render targets
         don't work well *)
      if display && Draw.window_is_shown (window top) then begin
        render top;
        flip top
      end)

(* Emit the close-window event to the window containing the layout *)
let push_close r =
  let id = Sdl.get_window_id (window r) in
  Trigger.push_close id

let destroy_window r =
  match window_opt r with
  | Some w ->
    let window_id = Sdl.get_window_id w in
    Trigger.push_destroy_window (r.id) ~window_id
  | None ->
    printd (debug_board + debug_error + debug_user)
      "Cannot destroy window for layout %s because it is not associated with any \
       SDL window." (sprint_id r)

(* Ask the board to create a new window with the given room *)
let add_window r =
  match window_opt r with
  | Some w ->
    printd (debug_error + debug_user + debug_board)
      "Cannot create a new window with Room %s because it already belongs to an \
       SDL window (id=%u)" (sprint_id r) (Sdl.get_window_id w)
  | None -> Trigger.push_add_window (r.id)

let inside_geom geometry (x,y) =
  x <= geometry.x + geometry.w && x >= geometry.x &&
  y <= geometry.y + geometry.h && y >= geometry.y

let inside room (x,y) =
  match room.mask with
  | None -> inside_geom room.current_geom (x,y)
  | Some mask -> (* TODO vérifier aussi qu'on est dans la dimension du mask *)
    let x0,y0 = room.current_geom.x, room.current_geom.y in
    let _,_,_,a = Draw.get_pixel_color mask ~x:(x-x0) ~y:(y-y0) in
    a <> 0

(** get the smallest room (with Resident) containing point (x,y), or None *)
(* not used anymore *)
let rec over_focus x y t =
  let g = to_current_geom t.geometry
  (* one should also take into account transforms, clipping... and also if a
     SUBroom is animated, the geometry can extend beyond the initial (fixed)
     house geometry,... TODO *) in
  if t.show && (inside_geom g (x,y))
  then match t.content with
    | Resident _ -> Some t
    | Rooms h ->
      list_check (fun r -> over_focus (x - g.x) (y - g.y) r) h
      (* we translate because geometry is relative *)
  else None

(* instead of the first one, get the complete list *)
(* cf remarks above *)
let rec focus_list_old x y t =
  let g = to_current_geom t.geometry in
  if t.show && (inside_geom g (x,y)) then match t.content with
    | Resident _ -> [ t ]
    | Rooms h -> List.flatten (List.map (fun r -> focus_list_old (x - g.x) (y - g.y) r) h)
  else []

(* instead of the first one, get the complete list *)
(* in each layer, the first element of the list has priority (TODO this is not
   consistent with the fact that it is the first displayed) *)
(* cf remarks above *)
let rec focus_list x y t =
  (* printd debug_custom "Searching for focus widget in room %s" (sprint_id t); *)
  if t.show && not (t.disabled) && (inside t (x,y)) then match t.content with
    | Resident _ -> (* printd debug_custom "Widget found"; *) [ t ]
    | Rooms list -> List.flatten (List.rev_map (focus_list x y) list)
  else []

(* get the focus element in the top layer *)
let top_focus x y t =
  let flist = focus_list x y t in
  printd debug_graphics "Number of layers detected under mouse: %u (%s)"
    (List.length flist)
    (String.concat " " (List.map (fun r -> "#" ^ (string_of_int r.id)) flist));
  let compare r1 r2 = Chain.compare (get_layer r1) (get_layer r2) in
  list_max compare flist

(** get the smallest room (with Rooms or Resident) containing (x,y), or None *)
(* only used for testing *)
(* TODO à fusionner avec le précédent pour retourner une paire ? *)
(* TODO vérifier qu'on est dans le même calque (layer) *)
let rec hover x y t =
  let g = to_current_geom t.geometry in
  if t.show && (inside_geom g (x,y)) then match t.content with
    | Resident _ -> Some t
    | Rooms h -> (match
                    list_check (fun r -> hover (x - g.x) (y - g.y) r) h
                  with
                  | None -> Some t
                  | o -> o)
  else None