Welcome to mirror list, hosted at ThFree Co, Russian Federation.

interface_panel.c « interface « editors « blender « source - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 95c804eaccba8e909b46e0d002764d52fdb4bea5 (plain)
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
/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 *
 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
 * All rights reserved.
 */

/** \file
 * \ingroup edinterface
 */

/* a full doc with API notes can be found in
 * bf-blender/trunk/blender/doc/guides/interface_API.txt */

#include <ctype.h>
#include <math.h>
#include <stdlib.h>
#include <string.h>

#include "MEM_guardedalloc.h"

#include "PIL_time.h"

#include "BLI_blenlib.h"
#include "BLI_math.h"
#include "BLI_utildefines.h"

#include "BLT_translation.h"

#include "DNA_screen_types.h"
#include "DNA_userdef_types.h"

#include "BKE_context.h"
#include "BKE_screen.h"

#include "BLF_api.h"

#include "WM_api.h"
#include "WM_types.h"

#include "ED_screen.h"

#include "UI_interface.h"
#include "UI_interface_icons.h"
#include "UI_resources.h"
#include "UI_view2d.h"

#include "GPU_batch_presets.h"
#include "GPU_immediate.h"
#include "GPU_matrix.h"
#include "GPU_state.h"

#include "interface_intern.h"

/*********************** defines and structs ************************/

#define ANIMATION_TIME 0.30
#define ANIMATION_INTERVAL 0.02

#define PNL_LAST_ADDED 1
#define PNL_ACTIVE 2
#define PNL_WAS_ACTIVE 4
#define PNL_ANIM_ALIGN 8
#define PNL_NEW_ADDED 16
#define PNL_FIRST 32

/* only show pin header button for pinned panels */
#define USE_PIN_HIDDEN

/* the state of the mouse position relative to the panel */
typedef enum uiPanelMouseState {
  PANEL_MOUSE_OUTSIDE,        /* mouse is not in the panel */
  PANEL_MOUSE_INSIDE_CONTENT, /* mouse is in the actual panel content */
  PANEL_MOUSE_INSIDE_HEADER,  /* mouse is in the panel header */
  PANEL_MOUSE_INSIDE_SCALE,   /* mouse is inside panel scale widget */
} uiPanelMouseState;

typedef enum uiHandlePanelState {
  PANEL_STATE_DRAG,
  PANEL_STATE_DRAG_SCALE,
  PANEL_STATE_WAIT_UNTAB,
  PANEL_STATE_ANIMATION,
  PANEL_STATE_EXIT,
} uiHandlePanelState;

typedef struct uiHandlePanelData {
  uiHandlePanelState state;

  /* animation */
  wmTimer *animtimer;
  double starttime;

  /* dragging */
  bool is_drag_drop;
  int startx, starty;
  int startofsx, startofsy;
  int startsizex, startsizey;
  float start_cur_xmin, start_cur_ymin;
} uiHandlePanelData;

typedef struct PanelSort {
  Panel *panel, *orig;
} PanelSort;

static int get_panel_real_size_y(const Panel *panel);
static void panel_activate_state(const bContext *C, Panel *panel, uiHandlePanelState state);
static int compare_panel(const void *a1, const void *a2);
static bool panel_type_context_poll(PanelType *panel_type, const char *context);

static void panel_title_color_get(bool show_background, uchar color[4])
{
  if (show_background) {
    UI_GetThemeColor4ubv(TH_TITLE, color);
  }
  else {
    /* Use menu colors for floating panels. */
    bTheme *btheme = UI_GetTheme();
    const uiWidgetColors *wcol = &btheme->tui.wcol_menu_back;
    copy_v4_v4_uchar(color, (const uchar *)wcol->text);
  }
}

/*********************** space specific code ************************/
/* temporary code to remove all sbuts stuff from panel code         */

/* SpaceProperties.align */
typedef enum eSpaceButtons_Align {
  BUT_HORIZONTAL = 0,
  BUT_VERTICAL = 1,
  BUT_AUTO = 2,
} eSpaceButtons_Align;

static int panel_aligned(const ScrArea *area, const ARegion *region)
{
  if (area->spacetype == SPACE_PROPERTIES && region->regiontype == RGN_TYPE_WINDOW) {
    return BUT_VERTICAL;
  }
  if (area->spacetype == SPACE_USERPREF && region->regiontype == RGN_TYPE_WINDOW) {
    return BUT_VERTICAL;
  }
  if (area->spacetype == SPACE_FILE && region->regiontype == RGN_TYPE_CHANNELS) {
    return BUT_VERTICAL;
  }
  if (area->spacetype == SPACE_IMAGE && region->regiontype == RGN_TYPE_PREVIEW) {
    return BUT_VERTICAL;
  }
  if (ELEM(region->regiontype,
           RGN_TYPE_UI,
           RGN_TYPE_TOOLS,
           RGN_TYPE_TOOL_PROPS,
           RGN_TYPE_HUD,
           RGN_TYPE_NAV_BAR,
           RGN_TYPE_EXECUTE)) {
    return BUT_VERTICAL;
  }

  return 0;
}

static bool panel_active_animation_changed(ListBase *lb, Panel **pa_animation, bool *no_animation)
{
  LISTBASE_FOREACH (Panel *, panel, lb) {
    /* Detect panel active flag changes. */
    if (!(panel->type && panel->type->parent)) {
      if ((panel->runtime_flag & PNL_WAS_ACTIVE) && !(panel->runtime_flag & PNL_ACTIVE)) {
        return true;
      }
      if (!(panel->runtime_flag & PNL_WAS_ACTIVE) && (panel->runtime_flag & PNL_ACTIVE)) {
        return true;
      }
    }

    if ((panel->runtime_flag & PNL_ACTIVE) && !(panel->flag & PNL_CLOSED)) {
      if (panel_active_animation_changed(&panel->children, pa_animation, no_animation)) {
        return true;
      }
    }

    /* Detect animation. */
    if (panel->activedata) {
      uiHandlePanelData *data = panel->activedata;
      if (data->state == PANEL_STATE_ANIMATION) {
        *pa_animation = panel;
      }
      else {
        /* Don't animate while handling other interaction. */
        *no_animation = true;
      }
    }
    if ((panel->runtime_flag & PNL_ANIM_ALIGN) && !(*pa_animation)) {
      *pa_animation = panel;
    }
  }

  return false;
}

static bool panels_need_realign(ScrArea *area, ARegion *region, Panel **r_panel_animation)
{
  *r_panel_animation = NULL;

  if (area->spacetype == SPACE_PROPERTIES && region->regiontype == RGN_TYPE_WINDOW) {
    SpaceProperties *sbuts = area->spacedata.first;

    if (sbuts->mainbo != sbuts->mainb) {
      return true;
    }
  }
  else if (area->spacetype == SPACE_IMAGE && region->regiontype == RGN_TYPE_PREVIEW) {
    return true;
  }
  else if (area->spacetype == SPACE_FILE && region->regiontype == RGN_TYPE_CHANNELS) {
    return true;
  }

  /* Detect if a panel was added or removed. */
  Panel *panel_animation = NULL;
  bool no_animation = false;
  if (panel_active_animation_changed(&region->panels, &panel_animation, &no_animation)) {
    return true;
  }

  /* Detect panel marked for animation, if we're not already animating. */
  if (panel_animation) {
    if (!no_animation) {
      *r_panel_animation = panel_animation;
    }
    return true;
  }

  return false;
}

/********* Functions for instanced panels. ***********/

static Panel *UI_panel_add_instanced_ex(ScrArea *area,
                                        ARegion *region,
                                        ListBase *panels,
                                        PanelType *panel_type,
                                        int list_index,
                                        PointerRNA *custom_data)
{
  Panel *panel = MEM_callocN(sizeof(Panel), "instanced panel");
  panel->type = panel_type;
  BLI_strncpy(panel->panelname, panel_type->idname, sizeof(panel->panelname));

  panel->runtime.list_index = list_index;
  panel->runtime.custom_data_ptr = custom_data;

  /* Add the panel's children too. Although they aren't instanced panels, we can still use this
   * function to create them, as UI_panel_begin does other things we don't need to do. */
  LISTBASE_FOREACH (LinkData *, child, &panel_type->children) {
    PanelType *child_type = child->data;
    UI_panel_add_instanced_ex(area, region, &panel->children, child_type, list_index, custom_data);
  }

  /* Make sure the panel is added to the end of the display-order as well. This is needed for
   * loading existing files.
   *
   * Note: We could use special behavior to place it after the panel that starts the list of
   * instanced panels, but that would add complexity that isn't needed for now. */
  int max_sortorder = 0;
  LISTBASE_FOREACH (Panel *, existing_panel, panels) {
    if (existing_panel->sortorder > max_sortorder) {
      max_sortorder = existing_panel->sortorder;
    }
  }
  panel->sortorder = max_sortorder + 1;

  BLI_addtail(panels, panel);

  return panel;
}

/**
 * Called in situations where panels need to be added dynamically rather than having only one panel
 * corresponding to each PanelType.
 */
Panel *UI_panel_add_instanced(ScrArea *area,
                              ARegion *region,
                              ListBase *panels,
                              char *panel_idname,
                              int list_index,
                              PointerRNA *custom_data)
{
  ARegionType *region_type = region->type;

  PanelType *panel_type = BLI_findstring(
      &region_type->paneltypes, panel_idname, offsetof(PanelType, idname));

  if (panel_type == NULL) {
    printf("Panel type '%s' not found.\n", panel_idname);
    return NULL;
  }

  return UI_panel_add_instanced_ex(area, region, panels, panel_type, list_index, custom_data);
}

/**
 * Find a unique key to append to the idname for the lookup to the panel's #uiBlock. Needed for
 * instanced panels, where there can be multiple with the same type and idname.
 */
void UI_list_panel_unique_str(Panel *panel, char *r_name)
{
  snprintf(r_name, LIST_PANEL_UNIQUE_STR_LEN, "%d", panel->runtime.list_index);
}

/**
 * Remove the #uiBlock corresponding to a panel. The lookup is needed because panels don't store
 * a reference to their corresponding #uiBlock.
 */
static void panel_free_block(ARegion *region, Panel *panel)
{
  BLI_assert(panel->type);

  char block_name[BKE_ST_MAXNAME + LIST_PANEL_UNIQUE_STR_LEN];
  strncpy(block_name, panel->type->idname, BKE_ST_MAXNAME);
  char unique_panel_str[LIST_PANEL_UNIQUE_STR_LEN];
  UI_list_panel_unique_str(panel, unique_panel_str);
  strncat(block_name, unique_panel_str, LIST_PANEL_UNIQUE_STR_LEN);

  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    if (STREQ(block->name, block_name)) {
      BLI_remlink(&region->uiblocks, block);
      UI_block_free(NULL, block);
      break; /* Only delete one block for this panel. */
    }
  }
}

/**
 * Free a panel and it's children. Custom data is shared by the panel and its children
 * and is freed by #UI_panels_free_instanced.
 *
 * \note The only panels that should need to be deleted at runtime are panels with the
 * #PNL_INSTANCED flag set.
 */
static void panel_delete(ARegion *region, ListBase *panels, Panel *panel)
{
  /* Recursively delete children. */
  LISTBASE_FOREACH_MUTABLE (Panel *, child, &panel->children) {
    panel_delete(region, &panel->children, child);
  }
  BLI_freelistN(&panel->children);

  panel_free_block(region, panel);

  BLI_remlink(panels, panel);
  if (panel->activedata) {
    MEM_freeN(panel->activedata);
  }
  MEM_freeN(panel);
}

/**
 * Remove instanced panels from the region's panel list.
 *
 * \note Can be called with NULL \a C, but it should be avoided because
 * handlers might not be removed.
 */
void UI_panels_free_instanced(bContext *C, ARegion *region)
{
  /* Delete panels with the instanced flag. */
  LISTBASE_FOREACH_MUTABLE (Panel *, panel, &region->panels) {
    if ((panel->type != NULL) && (panel->type->flag & PNL_INSTANCED)) {
      /* Make sure the panel's handler is removed before deleting it. */
      if (C != NULL && panel->activedata != NULL) {
        panel_activate_state(C, panel, PANEL_STATE_EXIT);
      }

      /* Free panel's custom data. */
      if (panel->runtime.custom_data_ptr != NULL) {
        MEM_freeN(panel->runtime.custom_data_ptr);
      }

      /* Free the panel and its sub-panels. */
      panel_delete(region, &region->panels, panel);
    }
  }
}

/**
 * Check if the instanced panels in the region's panels correspond to the list of data the panels
 * represent. Returns false if the panels have been reordered or if the types from the list data
 * don't match in any way.
 *
 * \param data: The list of data to check against the instanced panels.
 * \param panel_idname_func: Function to find the panel type idname for each item in the data list.
 * For a readability and generality, this lookup happens separately for each type of panel list.
 */
bool UI_panel_list_matches_data(ARegion *region,
                                ListBase *data,
                                uiListPanelIDFromDataFunc panel_idname_func)
{
  /* Check for NULL data. */
  int data_len = 0;
  Link *data_link = NULL;
  if (data == NULL) {
    data_len = 0;
    data_link = NULL;
  }
  else {
    data_len = BLI_listbase_count(data);
    data_link = data->first;
  }

  int i = 0;
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->type != NULL && panel->type->flag & PNL_INSTANCED) {
      /* The panels were reordered by drag and drop. */
      if (panel->flag & PNL_INSTANCED_LIST_ORDER_CHANGED) {
        return false;
      }

      /* We reached the last data item before the last instanced panel. */
      if (data_link == NULL) {
        return false;
      }

      /* Check if the panel type matches the panel type from the data item. */
      char panel_idname[MAX_NAME];
      panel_idname_func(data_link, panel_idname);
      if (!STREQ(panel_idname, panel->type->idname)) {
        return false;
      }

      data_link = data_link->next;
      i++;
    }
  }

  /* If we didn't make it to the last list item, the panel list isn't complete. */
  if (i != data_len) {
    return false;
  }

  return true;
}

static void reorder_instanced_panel_list(bContext *C, ARegion *region, Panel *drag_panel)
{
  /* Without a type we cannot access the reorder callback. */
  if (drag_panel->type == NULL) {
    return;
  }
  /* Don't reorder if this instanced panel doesn't support drag and drop reordering. */
  if (drag_panel->type->reorder == NULL) {
    return;
  }

  char *context = drag_panel->type->context;

  /* Find how many instanced panels with this context string. */
  int list_panels_len = 0;
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->type) {
      if (panel_type_context_poll(panel->type, context)) {
        if (panel->type->flag & PNL_INSTANCED) {
          list_panels_len++;
        }
      }
    }
  }

  /* Sort the matching instanced panels by their display order. */
  PanelSort *panel_sort = MEM_callocN(list_panels_len * sizeof(*panel_sort), "instancedpanelsort");
  PanelSort *sort_index = panel_sort;
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->type) {
      if (panel_type_context_poll(panel->type, context)) {
        if (panel->type->flag & PNL_INSTANCED) {
          sort_index->panel = MEM_dupallocN(panel);
          sort_index->orig = panel;
          sort_index++;
        }
      }
    }
  }
  qsort(panel_sort, list_panels_len, sizeof(*panel_sort), compare_panel);

  /* Find how many of those panels are above this panel. */
  int move_to_index = 0;
  for (; move_to_index < list_panels_len; move_to_index++) {
    if (panel_sort[move_to_index].orig == drag_panel) {
      break;
    }
  }

  /* Free panel sort array. */
  int i = 0;
  for (sort_index = panel_sort; i < list_panels_len; i++, sort_index++) {
    MEM_freeN(sort_index->panel);
  }
  MEM_freeN(panel_sort);

  /* Don't reorder the panel didn't change order after being dropped. */
  if (move_to_index == drag_panel->runtime.list_index) {
    return;
  }

  /* Set the bit to tell the interface to instanced the list. */
  drag_panel->flag |= PNL_INSTANCED_LIST_ORDER_CHANGED;

  /* Finally, move this panel's list item to the new index in its list. */
  drag_panel->type->reorder(C, drag_panel, move_to_index);
}

/**
 * Recursive implementation for #UI_panel_set_expand_from_list_data.
 *
 * \return Whether the closed flag for the panel or any sub-panels changed.
 */
static bool panel_set_expand_from_list_data_recursive(Panel *panel, short flag, short *flag_index)
{
  bool open = (flag & (1 << *flag_index));
  bool changed = (open == (bool)(panel->flag & PNL_CLOSEDY));
  if (open) {
    panel->flag &= ~PNL_CLOSEDY;
  }
  else {
    panel->flag |= PNL_CLOSEDY;
  }
  LISTBASE_FOREACH (Panel *, child, &panel->children) {
    *flag_index = *flag_index + 1;
    changed |= panel_set_expand_from_list_data_recursive(child, flag, flag_index);
  }
  return changed;
}

/**
 * Set the expansion of the panel and its sub-panels from the flag stored by the list data
 * corresponding to this panel. The flag has expansion stored in each bit in depth first
 * order.
 */
void UI_panel_set_expand_from_list_data(const bContext *C, Panel *panel)
{
  BLI_assert(panel->type != NULL);
  BLI_assert(panel->type->flag & PNL_INSTANCED);
  if (panel->type->get_list_data_expand_flag == NULL) {
    /* Instanced panel doesn't support loading expansion. */
    return;
  }

  short expand_flag = panel->type->get_list_data_expand_flag(C, panel);
  short flag_index = 0;

  /* Start panel animation if the open state was changed. */
  if (panel_set_expand_from_list_data_recursive(panel, expand_flag, &flag_index)) {
    panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
  }
}

/**
 * Recursive implementation for #set_panels_list_data_expand_flag.
 */
static void get_panel_expand_flag(Panel *panel, short *flag, short *flag_index)
{
  bool open = !(panel->flag & PNL_CLOSEDY);
  if (open) {
    *flag |= (1 << *flag_index);
  }
  else {
    *flag &= ~(1 << *flag_index);
  }
  LISTBASE_FOREACH (Panel *, child, &panel->children) {
    *flag_index = *flag_index + 1;
    get_panel_expand_flag(child, flag, flag_index);
  }
}

/**
 * Call the callback to store the panel and subpanel expansion settings in the list item that
 * corresponds to this panel.
 *
 * \note This needs to iterate through all of the regions panels because the panel with changed
 * expansion could have been the subpanel of a instanced panel, meaning it might not know
 * which list item it corresponds to.
 */
static void set_panels_list_data_expand_flag(const bContext *C, ARegion *region)
{
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    PanelType *panel_type = panel->type;
    if (panel_type == NULL) {
      continue;
    }

    /* Check for #PNL_ACTIVE so we only set the expand flag for active panels. */
    if (panel_type->flag & PNL_INSTANCED && panel->runtime_flag & PNL_ACTIVE) {
      short expand_flag = 0; /* Initialize to quite complaining compiler, value not used. */
      short flag_index = 0;
      get_panel_expand_flag(panel, &expand_flag, &flag_index);
      if (panel->type->set_list_data_expand_flag) {
        panel->type->set_list_data_expand_flag(C, panel, expand_flag);
      }
    }
  }
}

/****************************** panels ******************************/

/**
 * Set flag state for a panel and its sub-panels.
 *
 * \return True if this function changed any of the flags, false if it didn't.
 */
static bool panel_set_flag_recursive(Panel *panel, int flag, bool value)
{
  short flag_original = panel->flag;

  SET_FLAG_FROM_TEST(panel->flag, value, flag);

  bool changed = (flag_original != panel->flag);

  LISTBASE_FOREACH (Panel *, child, &panel->children) {
    changed |= panel_set_flag_recursive(child, flag, value);
  }

  return changed;
}

static void panels_collapse_all(const bContext *C,
                                ScrArea *area,
                                ARegion *region,
                                const Panel *from_panel)
{
  const bool has_category_tabs = UI_panel_category_is_visible(region);
  const char *category = has_category_tabs ? UI_panel_category_active_get(region, false) : NULL;
  const int flag = ((panel_aligned(area, region) == BUT_HORIZONTAL) ? PNL_CLOSEDX : PNL_CLOSEDY);
  const PanelType *from_pt = from_panel->type;

  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    PanelType *pt = panel->type;

    /* close panels with headers in the same context */
    if (pt && from_pt && !(pt->flag & PNL_NO_HEADER)) {
      if (!pt->context[0] || !from_pt->context[0] || STREQ(pt->context, from_pt->context)) {
        if ((panel->flag & PNL_PIN) || !category || !pt->category[0] ||
            STREQ(pt->category, category)) {
          panel->flag &= ~PNL_CLOSED;
          panel->flag |= flag;
        }
      }
    }
  }
  set_panels_list_data_expand_flag(C, region);
}

static bool panel_type_context_poll(PanelType *panel_type, const char *context)
{
  if (panel_type->context[0] && STREQ(panel_type->context, context)) {
    return true;
  }
  return false;
}

Panel *UI_panel_find_by_type(ListBase *lb, PanelType *pt)
{
  const char *idname = pt->idname;

  LISTBASE_FOREACH (Panel *, panel, lb) {
    if (STREQLEN(panel->panelname, idname, sizeof(panel->panelname))) {
      return panel;
    }
  }
  return NULL;
}

/**
 * \note \a panel should be return value from #UI_panel_find_by_type and can be NULL.
 */
Panel *UI_panel_begin(ScrArea *area,
                      ARegion *region,
                      ListBase *lb,
                      uiBlock *block,
                      PanelType *pt,
                      Panel *panel,
                      bool *r_open)
{
  Panel *panel_last;
  const char *drawname = CTX_IFACE_(pt->translation_context, pt->label);
  const char *idname = pt->idname;
  const bool newpanel = (panel == NULL);
  int align = panel_aligned(area, region);

  if (!newpanel) {
    panel->type = pt;
  }
  else {
    /* new panel */
    panel = MEM_callocN(sizeof(Panel), "new panel");
    panel->type = pt;
    BLI_strncpy(panel->panelname, idname, sizeof(panel->panelname));

    if (pt->flag & PNL_DEFAULT_CLOSED) {
      if (align == BUT_VERTICAL) {
        panel->flag |= PNL_CLOSEDY;
      }
      else {
        panel->flag |= PNL_CLOSEDX;
      }
    }

    panel->ofsx = 0;
    panel->ofsy = 0;
    panel->sizex = 0;
    panel->sizey = 0;
    panel->blocksizex = 0;
    panel->blocksizey = 0;
    panel->runtime_flag |= PNL_NEW_ADDED;

    BLI_addtail(lb, panel);
  }

  /* Do not allow closed panels without headers! Else user could get "disappeared" UI! */
  if ((pt->flag & PNL_NO_HEADER) && (panel->flag & PNL_CLOSED)) {
    panel->flag &= ~PNL_CLOSED;
    /* Force update of panels' positions! */
    panel->sizex = 0;
    panel->sizey = 0;
    panel->blocksizex = 0;
    panel->blocksizey = 0;
  }

  BLI_strncpy(panel->drawname, drawname, sizeof(panel->drawname));

  /* if a new panel is added, we insert it right after the panel
   * that was last added. this way new panels are inserted in the
   * right place between versions */
  for (panel_last = lb->first; panel_last; panel_last = panel_last->next) {
    if (panel_last->runtime_flag & PNL_LAST_ADDED) {
      BLI_remlink(lb, panel);
      BLI_insertlinkafter(lb, panel_last, panel);
      break;
    }
  }

  if (newpanel) {
    panel->sortorder = (panel_last) ? panel_last->sortorder + 1 : 0;

    LISTBASE_FOREACH (Panel *, panel_next, lb) {
      if (panel_next != panel && panel_next->sortorder >= panel->sortorder) {
        panel_next->sortorder++;
      }
    }
  }

  if (panel_last) {
    panel_last->runtime_flag &= ~PNL_LAST_ADDED;
  }

  /* assign to block */
  block->panel = panel;
  panel->runtime_flag |= PNL_ACTIVE | PNL_LAST_ADDED;
  if (region->alignment == RGN_ALIGN_FLOAT) {
    UI_block_theme_style_set(block, UI_BLOCK_THEME_STYLE_POPUP);
  }

  *r_open = false;

  if (panel->flag & PNL_CLOSED) {
    return panel;
  }

  *r_open = true;

  return panel;
}

static float panel_region_offset_x_get(const ARegion *region, int align)
{
  if (UI_panel_category_is_visible(region)) {
    if (align == BUT_VERTICAL &&
        (RGN_ALIGN_ENUM_FROM_MASK(region->alignment) != RGN_ALIGN_RIGHT)) {
      return UI_PANEL_CATEGORY_MARGIN_WIDTH;
    }
  }

  return 0;
}

void UI_panel_end(
    const ScrArea *area, const ARegion *region, uiBlock *block, int width, int height, bool open)
{
  Panel *panel = block->panel;

  /* Set panel size excluding children. */
  panel->blocksizex = width;
  panel->blocksizey = height;

  /* Compute total panel size including children. */
  LISTBASE_FOREACH (Panel *, pachild, &panel->children) {
    if (pachild->runtime_flag & PNL_ACTIVE) {
      width = max_ii(width, pachild->sizex);
      height += get_panel_real_size_y(pachild);
    }
  }

  /* Update total panel size. */
  if (panel->runtime_flag & PNL_NEW_ADDED) {
    panel->runtime_flag &= ~PNL_NEW_ADDED;
    panel->sizex = width;
    panel->sizey = height;
  }
  else {
    int old_sizex = panel->sizex, old_sizey = panel->sizey;
    int old_region_ofsx = panel->runtime.region_ofsx;

    /* update width/height if non-zero */
    if (width != 0) {
      panel->sizex = width;
    }
    if (height != 0 || open) {
      panel->sizey = height;
    }

    /* check if we need to do an animation */
    if (panel->sizex != old_sizex || panel->sizey != old_sizey) {
      panel->runtime_flag |= PNL_ANIM_ALIGN;
      panel->ofsy += old_sizey - panel->sizey;
    }

    int align = panel_aligned(area, region);
    panel->runtime.region_ofsx = panel_region_offset_x_get(region, align);
    if (old_region_ofsx != panel->runtime.region_ofsx) {
      panel->runtime_flag |= PNL_ANIM_ALIGN;
    }
  }
}

static void ui_offset_panel_block(uiBlock *block)
{
  const uiStyle *style = UI_style_get_dpi();

  /* compute bounds and offset */
  ui_block_bounds_calc(block);

  int ofsy = block->panel->sizey - style->panelspace;

  LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
    but->rect.ymin += ofsy;
    but->rect.ymax += ofsy;
  }

  block->rect.xmax = block->panel->sizex;
  block->rect.ymax = block->panel->sizey;
  block->rect.xmin = block->rect.ymin = 0.0;
}

/**************************** drawing *******************************/

/* triangle 'icon' for panel header */
void UI_draw_icon_tri(float x, float y, char dir, const float color[4])
{
  float f3 = 0.05 * U.widget_unit;
  float f5 = 0.15 * U.widget_unit;
  float f7 = 0.25 * U.widget_unit;

  if (dir == 'h') {
    UI_draw_anti_tria(x - f3, y - f5, x - f3, y + f5, x + f7, y, color);
  }
  else if (dir == 't') {
    UI_draw_anti_tria(x - f5, y - f7, x + f5, y - f7, x, y + f3, color);
  }
  else { /* 'v' = vertical, down */
    UI_draw_anti_tria(x - f5, y + f3, x + f5, y + f3, x, y - f7, color);
  }
}

static void ui_draw_anti_x(uint pos, float x1, float y1, float x2, float y2)
{

  /* set antialias line */
  GPU_line_smooth(true);
  GPU_blend(true);

  GPU_line_width(2.0);

  immBegin(GPU_PRIM_LINES, 4);

  immVertex2f(pos, x1, y1);
  immVertex2f(pos, x2, y2);

  immVertex2f(pos, x1, y2);
  immVertex2f(pos, x2, y1);

  immEnd();

  GPU_line_smooth(false);
  GPU_blend(false);
}

/* x 'icon' for panel header */
static void ui_draw_x_icon(uint pos, float x, float y)
{

  ui_draw_anti_x(pos, x, y, x + 9.375f, y + 9.375f);
}

#define PNL_ICON UI_UNIT_X /* could be UI_UNIT_Y too */

static void ui_draw_panel_scalewidget(uint pos, const rcti *rect)
{
  float xmin, xmax, dx;
  float ymin, ymax, dy;

  xmin = rect->xmax - PNL_HEADER + 2;
  xmax = rect->xmax - 3;
  ymin = rect->ymin + 3;
  ymax = rect->ymin + PNL_HEADER - 2;

  dx = 0.5f * (xmax - xmin);
  dy = 0.5f * (ymax - ymin);

  GPU_blend(true);
  immUniformColor4ub(255, 255, 255, 50);

  immBegin(GPU_PRIM_LINES, 4);

  immVertex2f(pos, xmin, ymin);
  immVertex2f(pos, xmax, ymax);

  immVertex2f(pos, xmin + dx, ymin);
  immVertex2f(pos, xmax, ymax - dy);

  immEnd();

  immUniformColor4ub(0, 0, 0, 50);

  immBegin(GPU_PRIM_LINES, 4);

  immVertex2f(pos, xmin, ymin + 1);
  immVertex2f(pos, xmax, ymax + 1);

  immVertex2f(pos, xmin + dx, ymin + 1);
  immVertex2f(pos, xmax, ymax - dy + 1);

  immEnd();

  GPU_blend(false);
}

/* For button layout next to label. */
void UI_panel_label_offset(uiBlock *block, int *r_x, int *r_y)
{
  Panel *panel = block->panel;
  const bool is_subpanel = (panel->type && panel->type->parent);

  *r_x = UI_UNIT_X * 1.0f;
  *r_y = UI_UNIT_Y * 1.5f;

  if (is_subpanel) {
    *r_x += (0.7f * UI_UNIT_X);
  }
}

static void ui_draw_aligned_panel_header(
    uiStyle *style, uiBlock *block, const rcti *rect, char dir, const bool show_background)
{
  Panel *panel = block->panel;
  rcti hrect;
  int pnl_icons;
  const char *activename = panel->drawname;
  const bool is_subpanel = (panel->type && panel->type->parent);
  uiFontStyle *fontstyle = (is_subpanel) ? &style->widgetlabel : &style->paneltitle;
  uchar col_title[4];

  /* + 0.001f to avoid flirting with float inaccuracy */
  if (panel->control & UI_PNL_CLOSE) {
    pnl_icons = (panel->labelofs + (2.0f * PNL_ICON)) / block->aspect + 0.001f;
  }
  else {
    pnl_icons = (panel->labelofs + (1.1f * PNL_ICON)) / block->aspect + 0.001f;
  }

  /* draw text label */
  panel_title_color_get(show_background, col_title);
  col_title[3] = 255;

  hrect = *rect;
  if (dir == 'h') {
    hrect.xmin = rect->xmin + pnl_icons;
    hrect.ymin -= 2.0f / block->aspect;
    UI_fontstyle_draw(fontstyle,
                      &hrect,
                      activename,
                      col_title,
                      &(struct uiFontStyleDraw_Params){
                          .align = UI_STYLE_TEXT_LEFT,
                      });
  }
  else {
    /* ignore 'pnl_icons', otherwise the text gets offset horizontally
     * + 0.001f to avoid flirting with float inaccuracy
     */
    hrect.xmin = rect->xmin + (PNL_ICON + 5) / block->aspect + 0.001f;
    UI_fontstyle_draw_rotated(fontstyle, &hrect, activename, col_title);
  }
}

/* panel integrated in buttonswindow, tool/property lists etc */
void ui_draw_aligned_panel(uiStyle *style,
                           uiBlock *block,
                           const rcti *rect,
                           const bool show_pin,
                           const bool show_background)
{
  Panel *panel = block->panel;
  rctf itemrect;
  float color[4];
  const bool is_closed_x = (panel->flag & PNL_CLOSEDX) ? true : false;
  const bool is_closed_y = (panel->flag & PNL_CLOSEDY) ? true : false;
  const bool is_subpanel = (panel->type && panel->type->parent);
  const bool show_drag = (!is_subpanel &&
                          /* FIXME(campbell): currently no background means floating panel which
                           * can't be dragged. This may be changed in future. */
                          show_background);
  const int panel_col = is_subpanel ? TH_PANEL_SUB_BACK : TH_PANEL_BACK;
  const bool draw_box_style = (panel->type && panel->type->flag & PNL_DRAW_BOX);

  /* Use the theme for box widgets for box-style panels. */
  uiWidgetColors *box_wcol = NULL;
  if (draw_box_style) {
    bTheme *btheme = UI_GetTheme();
    box_wcol = &btheme->tui.wcol_box;
  }

  uint pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);

  if (panel->type && (panel->type->flag & PNL_NO_HEADER)) {
    if (show_background) {
      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
      immUniformThemeColor(panel_col);
      immRectf(pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
      immUnbindProgram();
    }
    return;
  }

  /* Calculate header rect with + 0.001f to prevent flicker due to float inaccuracy */
  rcti headrect = {
      rect->xmin, rect->xmax, rect->ymax, rect->ymax + floor(PNL_HEADER / block->aspect + 0.001f)};

  /* Draw a panel and header backdrops with an opaque box backdrop for box style panels. */
  if (draw_box_style && !is_subpanel) {
    /* Expand the top a tiny bit to give header buttons equal size above and below. */
    rcti box_rect = {rect->xmin,
                     rect->xmax,
                     (is_closed_x || is_closed_y) ? headrect.ymin : rect->ymin,
                     headrect.ymax + U.pixelsize};
    ui_draw_box_opaque(&box_rect, UI_CNR_ALL);

    /* Mimick the border between aligned box widgets for the bottom of the header. */
    if (!(is_closed_x || is_closed_y)) {
      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
      GPU_blend(true);

      immUniformColor4ubv(box_wcol->outline);
      immRectf(pos, rect->xmin, headrect.ymin - U.pixelsize, rect->xmax, headrect.ymin);
      uchar emboss_col[4];
      UI_GetThemeColor4ubv(TH_WIDGET_EMBOSS, emboss_col);
      immUniformColor4ubv(emboss_col);
      immRectf(pos,
               rect->xmin,
               headrect.ymin - U.pixelsize,
               rect->xmax,
               headrect.ymin - U.pixelsize - 1);

      GPU_blend(false);
      immUnbindProgram();
    }
  }

  /* Draw the header backdrop. */
  if (show_background && !is_subpanel && !draw_box_style) {
    float minx = rect->xmin;
    float maxx = is_closed_x ? (minx + PNL_HEADER / block->aspect) : rect->xmax;
    float y = headrect.ymax;

    immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
    GPU_blend(true);

    /* draw with background color */
    immUniformThemeColor(TH_PANEL_HEADER);
    immRectf(pos, minx, headrect.ymin, maxx, y);

    immBegin(GPU_PRIM_LINES, 4);

    immVertex2f(pos, minx, y);
    immVertex2f(pos, maxx, y);

    immVertex2f(pos, minx, y);
    immVertex2f(pos, maxx, y);

    immEnd();

    GPU_blend(false);
    immUnbindProgram();
  }

/* draw optional pin icon */
#ifdef USE_PIN_HIDDEN
  if (show_pin && (block->panel->flag & PNL_PIN))
#else
  if (show_pin)
#endif
  {
    uchar col_title[4];
    panel_title_color_get(show_background, col_title);

    GPU_blend(true);
    UI_icon_draw_ex(headrect.xmax - ((PNL_ICON * 2.2f) / block->aspect),
                    headrect.ymin + (5.0f / block->aspect),
                    (panel->flag & PNL_PIN) ? ICON_PINNED : ICON_UNPINNED,
                    (block->aspect * U.inv_dpi_fac),
                    1.0f,
                    0.0f,
                    col_title,
                    false);
    GPU_blend(false);
  }

  /* horizontal title */
  rcti titlerect = headrect;
  if (is_subpanel) {
    titlerect.xmin += (0.7f * UI_UNIT_X) / block->aspect + 0.001f;
  }
  if (is_closed_x == false) {
    ui_draw_aligned_panel_header(style, block, &titlerect, 'h', show_background);

    if (show_drag) {
      /* itemrect smaller */
      const float scale = 0.7;
      itemrect.xmax = headrect.xmax - (0.2f * UI_UNIT_X);
      itemrect.xmin = itemrect.xmax - BLI_rcti_size_y(&headrect);
      itemrect.ymin = headrect.ymin;
      itemrect.ymax = headrect.ymax;
      BLI_rctf_scale(&itemrect, scale);

      GPU_matrix_push();
      GPU_matrix_translate_2f(itemrect.xmin, itemrect.ymin);

      const int col_tint = 84;
      float col_high[4], col_dark[4];
      UI_GetThemeColorShade4fv(TH_PANEL_HEADER, col_tint, col_high);
      UI_GetThemeColorShade4fv(TH_PANEL_BACK, -col_tint, col_dark);

      GPUBatch *batch = GPU_batch_preset_panel_drag_widget(
          U.pixelsize, col_high, col_dark, BLI_rcti_size_y(&headrect) * scale);
      GPU_batch_program_set_builtin(batch, GPU_SHADER_2D_FLAT_COLOR);
      GPU_batch_draw(batch);
      GPU_matrix_pop();
    }
  }

  /* Draw panel backdrop. */
  if (is_closed_y) {
    /* skip */
  }
  else if (is_closed_x) {
    /* draw vertical title */
    ui_draw_aligned_panel_header(style, block, &headrect, 'v', show_background);
    pos = GPU_vertformat_attr_add(immVertexFormat(), "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
  }
  /* an open panel */
  else {
    /* in some occasions, draw a border */
    if (panel->flag & PNL_SELECT && !is_subpanel) {
      float radius;
      if (panel->control & UI_PNL_SOLID) {
        UI_draw_roundbox_corner_set(UI_CNR_ALL);
        radius = 8.0f;
      }
      else if (draw_box_style) {
        UI_draw_roundbox_corner_set(UI_CNR_ALL);
        radius = box_wcol->roundness * U.widget_unit;
      }
      else {
        UI_draw_roundbox_corner_set(UI_CNR_NONE);
        radius = 0.0f;
      }

      UI_GetThemeColorShade4fv(TH_BACK, -120, color);
      UI_draw_roundbox_aa(false,
                          0.5f + rect->xmin,
                          0.5f + rect->ymin,
                          0.5f + rect->xmax,
                          0.5f + headrect.ymax + 1,
                          radius,
                          color);
    }

    immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
    GPU_blend(true);

    /* Draw panel backdrop if it wasn't already been drawn by the single opaque round box earlier.
     * Note: Sub-panels blend with panels, so they can't be opaque. */
    if (show_background && !(draw_box_style && !is_subpanel)) {
      /* Draw the bottom sub-panels. */
      if (draw_box_style) {
        if (panel->next) {
          immUniformThemeColor(panel_col);
          immRectf(
              pos, rect->xmin + U.pixelsize, rect->ymin, rect->xmax - U.pixelsize, rect->ymax);
        }
        else {
          /* Change the width a little bit to line up with sides. */
          UI_draw_roundbox_corner_set(UI_CNR_BOTTOM_RIGHT | UI_CNR_BOTTOM_LEFT);
          UI_GetThemeColor4fv(panel_col, color);
          UI_draw_roundbox_aa(true,
                              rect->xmin + U.pixelsize,
                              rect->ymin + U.pixelsize,
                              rect->xmax - U.pixelsize,
                              rect->ymax,
                              box_wcol->roundness * U.widget_unit,
                              color);
        }
      }
      else {
        immUniformThemeColor(panel_col);
        immRectf(pos, rect->xmin, rect->ymin, rect->xmax, rect->ymax);
      }
    }

    if (panel->control & UI_PNL_SCALE) {
      ui_draw_panel_scalewidget(pos, rect);
    }

    immUnbindProgram();
  }

  uchar col_title[4];
  panel_title_color_get(show_background, col_title);

  /* draw optional close icon */

  if (panel->control & UI_PNL_CLOSE) {
    const int ofsx = 6;
    immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
    immUniformColor3ubv(col_title);
    ui_draw_x_icon(pos, rect->xmin + 2 + ofsx, rect->ymax + 2);
    immUnbindProgram();
  }

  /* draw collapse icon */

  /* itemrect smaller */
  itemrect.xmin = titlerect.xmin;
  itemrect.xmax = itemrect.xmin + BLI_rcti_size_y(&titlerect);
  itemrect.ymin = titlerect.ymin;
  itemrect.ymax = titlerect.ymax;

  BLI_rctf_scale(&itemrect, 0.25f);

  {
    float tria_color[4];
    rgb_uchar_to_float(tria_color, col_title);
    tria_color[3] = 1.0f;

    if (is_closed_y) {
      ui_draw_anti_tria_rect(&itemrect, 'h', tria_color);
    }
    else if (is_closed_x) {
      ui_draw_anti_tria_rect(&itemrect, 'h', tria_color);
    }
    else {
      ui_draw_anti_tria_rect(&itemrect, 'v', tria_color);
    }
  }
}

/************************** panel alignment *************************/

static int get_panel_header(const Panel *panel)
{
  if (panel->type && (panel->type->flag & PNL_NO_HEADER)) {
    return 0;
  }

  return PNL_HEADER;
}

static int get_panel_size_y(const Panel *panel)
{
  if (panel->type && (panel->type->flag & PNL_NO_HEADER)) {
    return panel->sizey;
  }

  return PNL_HEADER + panel->sizey;
}

static int get_panel_real_size_y(const Panel *panel)
{
  int sizey = (panel->flag & PNL_CLOSED) ? 0 : panel->sizey;

  if (panel->type && (panel->type->flag & PNL_NO_HEADER)) {
    return sizey;
  }

  return PNL_HEADER + sizey;
}

int UI_panel_size_y(const Panel *panel)
{
  return get_panel_real_size_y(panel);
}

/* this function is needed because uiBlock and Panel itself don't
 * change sizey or location when closed */
static int get_panel_real_ofsy(Panel *panel)
{
  if (panel->flag & PNL_CLOSEDY) {
    return panel->ofsy + panel->sizey;
  }
  return panel->ofsy;
}

static int get_panel_real_ofsx(Panel *panel)
{
  if (panel->flag & PNL_CLOSEDX) {
    return panel->ofsx + get_panel_header(panel);
  }
  return panel->ofsx + panel->sizex;
}

bool UI_panel_is_dragging(const struct Panel *panel)
{
  uiHandlePanelData *data = panel->activedata;
  if (!data) {
    return false;
  }

  return data->is_drag_drop;
}

/**
 * \note about sorting;
 * the sortorder has a lower value for new panels being added.
 * however, that only works to insert a single panel, when more new panels get
 * added the coordinates of existing panels and the previously stored to-be-inserted
 * panels do not match for sorting
 */

static int find_leftmost_panel(const void *a1, const void *a2)
{
  const PanelSort *ps1 = a1, *ps2 = a2;

  if (ps1->panel->ofsx > ps2->panel->ofsx) {
    return 1;
  }
  if (ps1->panel->ofsx < ps2->panel->ofsx) {
    return -1;
  }
  if (ps1->panel->sortorder > ps2->panel->sortorder) {
    return 1;
  }
  if (ps1->panel->sortorder < ps2->panel->sortorder) {
    return -1;
  }

  return 0;
}

static int find_highest_panel(const void *a1, const void *a2)
{
  const PanelSort *ps1 = a1, *ps2 = a2;

  /* stick uppermost header-less panels to the top of the region -
   * prevent them from being sorted (multiple header-less panels have to be sorted though) */
  if (ps1->panel->type->flag & PNL_NO_HEADER && ps2->panel->type->flag & PNL_NO_HEADER) {
    /* skip and check for ofs and sortorder below */
  }
  if (ps1->panel->type->flag & PNL_NO_HEADER) {
    return -1;
  }
  if (ps2->panel->type->flag & PNL_NO_HEADER) {
    return 1;
  }

  if (ps1->panel->ofsy + ps1->panel->sizey < ps2->panel->ofsy + ps2->panel->sizey) {
    return 1;
  }
  if (ps1->panel->ofsy + ps1->panel->sizey > ps2->panel->ofsy + ps2->panel->sizey) {
    return -1;
  }
  if (ps1->panel->sortorder > ps2->panel->sortorder) {
    return 1;
  }
  if (ps1->panel->sortorder < ps2->panel->sortorder) {
    return -1;
  }

  return 0;
}

static int compare_panel(const void *a1, const void *a2)
{
  const PanelSort *ps1 = a1, *ps2 = a2;

  if (ps1->panel->sortorder > ps2->panel->sortorder) {
    return 1;
  }
  if (ps1->panel->sortorder < ps2->panel->sortorder) {
    return -1;
  }

  return 0;
}

static void align_sub_panels(Panel *panel)
{
  /* Position sub panels. */
  int ofsy = panel->ofsy + panel->sizey - panel->blocksizey;

  LISTBASE_FOREACH (Panel *, pachild, &panel->children) {
    if (pachild->runtime_flag & PNL_ACTIVE) {
      pachild->ofsx = panel->ofsx;
      pachild->ofsy = ofsy - get_panel_size_y(pachild);
      ofsy -= get_panel_real_size_y(pachild);

      if (pachild->children.first) {
        align_sub_panels(pachild);
      }
    }
  }
}

/* this doesn't draw */
/* returns 1 when it did something */
static bool uiAlignPanelStep(ScrArea *area, ARegion *region, const float fac, const bool drag)
{
  PanelSort *ps, *panelsort, *psnext;
  int a, tot = 0;
  bool done;
  int align = panel_aligned(area, region);

  /* count active, not tabbed panels */
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      tot++;
    }
  }

  if (tot == 0) {
    return 0;
  }

  /* extra; change close direction? */
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      if ((panel->flag & PNL_CLOSEDX) && (align == BUT_VERTICAL)) {
        panel->flag ^= PNL_CLOSED;
      }
      else if ((panel->flag & PNL_CLOSEDY) && (align == BUT_HORIZONTAL)) {
        panel->flag ^= PNL_CLOSED;
      }
    }
  }

  /* sort panels */
  panelsort = MEM_callocN(tot * sizeof(PanelSort), "panelsort");

  ps = panelsort;
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      ps->panel = MEM_dupallocN(panel);
      ps->orig = panel;
      ps++;
    }
  }

  if (drag) {
    /* while we are dragging, we sort on location and update sortorder */
    if (align == BUT_VERTICAL) {
      qsort(panelsort, tot, sizeof(PanelSort), find_highest_panel);
    }
    else {
      qsort(panelsort, tot, sizeof(PanelSort), find_leftmost_panel);
    }

    for (ps = panelsort, a = 0; a < tot; a++, ps++) {
      ps->orig->sortorder = a;
    }
  }
  else {
    /* otherwise use sortorder */
    qsort(panelsort, tot, sizeof(PanelSort), compare_panel);
  }

  /* no smart other default start loc! this keeps switching f5/f6/etc compatible */
  ps = panelsort;
  ps->panel->runtime.region_ofsx = panel_region_offset_x_get(region, align);
  ps->panel->ofsx = 0;
  ps->panel->ofsy = -get_panel_size_y(ps->panel);
  ps->panel->ofsx += ps->panel->runtime.region_ofsx;

  for (a = 0; a < tot - 1; a++, ps++) {
    psnext = ps + 1;

    if (align == BUT_VERTICAL) {
      bool use_box = ps->panel->type && ps->panel->type->flag & PNL_DRAW_BOX;
      bool use_box_next = psnext->panel->type && psnext->panel->type->flag & PNL_DRAW_BOX;
      psnext->panel->ofsx = ps->panel->ofsx;
      psnext->panel->ofsy = get_panel_real_ofsy(ps->panel) - get_panel_size_y(psnext->panel);

      /* Extra margin for box style panels. */
      ps->panel->ofsx += (use_box) ? UI_PANEL_BOX_STYLE_MARGIN : 0.0f;
      if (use_box || use_box_next) {
        psnext->panel->ofsy -= UI_PANEL_BOX_STYLE_MARGIN;
      }
    }
    else {
      psnext->panel->ofsx = get_panel_real_ofsx(ps->panel);
      psnext->panel->ofsy = ps->panel->ofsy + get_panel_size_y(ps->panel) -
                            get_panel_size_y(psnext->panel);
    }
  }
  /* Extra margin for the last panel if it's a box-style panel. */
  if (panelsort[tot - 1].panel->type && panelsort[tot - 1].panel->type->flag & PNL_DRAW_BOX) {
    panelsort[tot - 1].panel->ofsx += UI_PANEL_BOX_STYLE_MARGIN;
  }

  /* we interpolate */
  done = false;
  ps = panelsort;
  for (a = 0; a < tot; a++, ps++) {
    if ((ps->panel->flag & PNL_SELECT) == 0) {
      if ((ps->orig->ofsx != ps->panel->ofsx) || (ps->orig->ofsy != ps->panel->ofsy)) {
        ps->orig->ofsx = round_fl_to_int(fac * (float)ps->panel->ofsx +
                                         (1.0f - fac) * (float)ps->orig->ofsx);
        ps->orig->ofsy = round_fl_to_int(fac * (float)ps->panel->ofsy +
                                         (1.0f - fac) * (float)ps->orig->ofsy);
        done = true;
      }
    }
  }

  /* set locations for tabbed and sub panels */
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      if (panel->children.first) {
        align_sub_panels(panel);
      }
    }
  }

  /* free panelsort array */
  for (ps = panelsort, a = 0; a < tot; a++, ps++) {
    MEM_freeN(ps->panel);
  }
  MEM_freeN(panelsort);

  return done;
}

static void ui_panels_size(ScrArea *area, ARegion *region, int *r_x, int *r_y)
{
  int align = panel_aligned(area, region);
  int sizex = 0;
  int sizey = 0;

  /* compute size taken up by panels, for setting in view2d */
  LISTBASE_FOREACH (Panel *, panel, &region->panels) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      int pa_sizex, pa_sizey;

      if (align == BUT_VERTICAL) {
        pa_sizex = panel->ofsx + panel->sizex;
        pa_sizey = get_panel_real_ofsy(panel);
      }
      else {
        pa_sizex = get_panel_real_ofsx(panel) + panel->sizex;
        pa_sizey = panel->ofsy + get_panel_size_y(panel);
      }

      sizex = max_ii(sizex, pa_sizex);
      sizey = min_ii(sizey, pa_sizey);
    }
  }

  if (sizex == 0) {
    sizex = UI_PANEL_WIDTH;
  }
  if (sizey == 0) {
    sizey = -UI_PANEL_WIDTH;
  }

  *r_x = sizex;
  *r_y = sizey;
}

static void ui_do_animate(bContext *C, Panel *panel)
{
  uiHandlePanelData *data = panel->activedata;
  ScrArea *area = CTX_wm_area(C);
  ARegion *region = CTX_wm_region(C);
  float fac;

  fac = (PIL_check_seconds_timer() - data->starttime) / ANIMATION_TIME;
  fac = min_ff(sqrtf(fac), 1.0f);

  /* for max 1 second, interpolate positions */
  if (uiAlignPanelStep(area, region, fac, false)) {
    ED_region_tag_redraw(region);
  }
  else {
    fac = 1.0f;
  }

  if (fac >= 1.0f) {
    /* Store before data is freed. */
    const bool is_drag_drop = data->is_drag_drop;

    panel_activate_state(C, panel, PANEL_STATE_EXIT);
    if (is_drag_drop) {
      /* Note: doing this in #panel_activate_state would require removing const for context in many
       * other places. */
      reorder_instanced_panel_list(C, region, panel);
    }
    return;
  }
}

static void panel_list_clear_active(ListBase *lb)
{
  /* set all panels as inactive, so that at the end we know
   * which ones were used */
  LISTBASE_FOREACH (Panel *, panel, lb) {
    if (panel->runtime_flag & PNL_ACTIVE) {
      panel->runtime_flag = PNL_WAS_ACTIVE;
    }
    else {
      panel->runtime_flag = 0;
    }

    panel_list_clear_active(&panel->children);
  }
}

void UI_panels_begin(const bContext *UNUSED(C), ARegion *region)
{
  panel_list_clear_active(&region->panels);
}

/* only draws blocks with panels */
void UI_panels_end(const bContext *C, ARegion *region, int *r_x, int *r_y)
{
  ScrArea *area = CTX_wm_area(C);
  Panel *panel, *panel_first;

  /* offset contents */
  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    if (block->active && block->panel) {
      ui_offset_panel_block(block);
    }
  }

  /* re-align, possibly with animation */
  if (panels_need_realign(area, region, &panel)) {
    if (panel) {
      panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
    }
    else {
      uiAlignPanelStep(area, region, 1.0, false);
    }
  }

  /* tag first panel */
  panel_first = NULL;
  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    if (block->active && block->panel) {
      if (!panel_first || block->panel->sortorder < panel_first->sortorder) {
        panel_first = block->panel;
      }
    }
  }

  if (panel_first) {
    panel_first->runtime_flag |= PNL_FIRST;
  }

  /* compute size taken up by panel */
  ui_panels_size(area, region, r_x, r_y);
}

void UI_panels_draw(const bContext *C, ARegion *region)
{
  if (region->alignment != RGN_ALIGN_FLOAT) {
    UI_ThemeClearColor(TH_BACK);
  }

  /* Draw panels, selected on top. Also in reverse order, because
   * UI blocks are added in reverse order and we need child panels
   * to draw on top. */
  LISTBASE_FOREACH_BACKWARD (uiBlock *, block, &region->uiblocks) {
    if (block->active && block->panel && !(block->panel->flag & PNL_SELECT)) {
      UI_block_draw(C, block);
    }
  }

  LISTBASE_FOREACH_BACKWARD (uiBlock *, block, &region->uiblocks) {
    if (block->active && block->panel && (block->panel->flag & PNL_SELECT)) {
      UI_block_draw(C, block);
    }
  }
}

void UI_panels_scale(ARegion *region, float new_width)
{
  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    if (block->panel) {
      float fac = new_width / (float)block->panel->sizex;
      block->panel->sizex = new_width;

      LISTBASE_FOREACH (uiBut *, but, &block->buttons) {
        but->rect.xmin *= fac;
        but->rect.xmax *= fac;
      }
    }
  }
}

/* ------------ panel merging ---------------- */

static void check_panel_overlap(ARegion *region, Panel *panel)
{
  Panel *panel_list;

  /* also called with (panel == NULL) for clear */

  for (panel_list = region->panels.first; panel_list; panel_list = panel_list->next) {
    panel_list->flag &= ~PNL_OVERLAP;
    if (panel && (panel_list != panel)) {
      if (panel_list->runtime_flag & PNL_ACTIVE) {
        float safex = 0.2, safey = 0.2;

        if (panel_list->flag & PNL_CLOSEDX) {
          safex = 0.05;
        }
        else if (panel_list->flag & PNL_CLOSEDY) {
          safey = 0.05;
        }
        else if (panel->flag & PNL_CLOSEDX) {
          safex = 0.05;
        }
        else if (panel->flag & PNL_CLOSEDY) {
          safey = 0.05;
        }

        if (panel_list->ofsx > panel->ofsx - safex * panel->sizex) {
          if (panel_list->ofsx + panel_list->sizex < panel->ofsx + (1.0f + safex) * panel->sizex) {
            if (panel_list->ofsy > panel->ofsy - safey * panel->sizey) {
              if (panel_list->ofsy + panel_list->sizey <
                  panel->ofsy + (1.0f + safey) * panel->sizey) {
                panel_list->flag |= PNL_OVERLAP;
              }
            }
          }
        }
      }
    }
  }
}

/************************ panel dragging ****************************/

#define DRAG_REGION_PAD (PNL_HEADER * 0.5)
static void ui_do_drag(const bContext *C, const wmEvent *event, Panel *panel)
{
  uiHandlePanelData *data = panel->activedata;
  ScrArea *area = CTX_wm_area(C);
  ARegion *region = CTX_wm_region(C);
  short align = panel_aligned(area, region);

  /* Keep the drag position in the region with a small pad to keep the panel visible. */
  int x = clamp_i(event->x, region->winrct.xmin, region->winrct.xmax + DRAG_REGION_PAD);
  int y = clamp_i(event->y, region->winrct.ymin, region->winrct.ymax + DRAG_REGION_PAD);

  float dx = (float)(x - data->startx);
  float dy = (float)(y - data->starty);

  /* Adjust for region zoom. */
  dx *= (float)BLI_rctf_size_x(&region->v2d.cur) / (float)BLI_rcti_size_x(&region->winrct);
  dy *= (float)BLI_rctf_size_y(&region->v2d.cur) / (float)BLI_rcti_size_y(&region->winrct);

  if (data->state == PANEL_STATE_DRAG_SCALE) {
    panel->sizex = MAX2(data->startsizex + dx, UI_PANEL_MINX);

    if (data->startsizey - dy < UI_PANEL_MINY) {
      dy = -UI_PANEL_MINY + data->startsizey;
    }

    panel->sizey = data->startsizey - dy;
    panel->ofsy = data->startofsy + dy;
  }
  else {
    /* reset the panel snapping, to allow dragging away from snapped edges */
    panel->snap = PNL_SNAP_NONE;

    /* Add the movement of the view due to edge scrolling while dragging. */
    dx += ((float)region->v2d.cur.xmin - data->start_cur_xmin);
    dy += ((float)region->v2d.cur.ymin - data->start_cur_ymin);
    panel->ofsx = data->startofsx + round_fl_to_int(dx);
    panel->ofsy = data->startofsy + round_fl_to_int(dy);
    check_panel_overlap(region, panel);

    if (align) {
      uiAlignPanelStep(area, region, 0.2f, true);
    }
  }

  ED_region_tag_redraw(region);
}
#undef DRAG_REGION_PAD

/******************* region level panel interaction *****************/

static uiPanelMouseState ui_panel_mouse_state_get(const uiBlock *block,
                                                  const Panel *panel,
                                                  const int mx,
                                                  const int my)
{
  /* open panel */
  if (panel->flag & PNL_CLOSEDX) {
    if ((block->rect.xmin <= mx) && (block->rect.xmin + PNL_HEADER >= mx)) {
      return PANEL_MOUSE_INSIDE_HEADER;
    }
  }
  /* outside left/right side */
  else if ((block->rect.xmin > mx) || (block->rect.xmax < mx)) {
    /* pass */
  }
  else if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
    return PANEL_MOUSE_INSIDE_HEADER;
  }
  /* open panel */
  else if (!(panel->flag & PNL_CLOSEDY)) {
    if (panel->control & UI_PNL_SCALE) {
      if (block->rect.xmax - PNL_HEADER <= mx) {
        if (block->rect.ymin + PNL_HEADER >= my) {
          return PANEL_MOUSE_INSIDE_SCALE;
        }
      }
    }
    if ((block->rect.xmin <= mx) && (block->rect.xmax >= mx)) {
      if ((block->rect.ymin <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
        return PANEL_MOUSE_INSIDE_CONTENT;
      }
    }
  }
  return PANEL_MOUSE_OUTSIDE;
}

typedef struct uiPanelDragCollapseHandle {
  bool was_first_open;
  int xy_init[2];
} uiPanelDragCollapseHandle;

static void ui_panel_drag_collapse_handler_remove(bContext *UNUSED(C), void *userdata)
{
  uiPanelDragCollapseHandle *dragcol_data = userdata;
  MEM_freeN(dragcol_data);
}

static void ui_panel_drag_collapse(bContext *C,
                                   uiPanelDragCollapseHandle *dragcol_data,
                                   const int xy_dst[2])
{
  ScrArea *area = CTX_wm_area(C);
  ARegion *region = CTX_wm_region(C);
  Panel *panel;

  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    float xy_a_block[2] = {UNPACK2(dragcol_data->xy_init)};
    float xy_b_block[2] = {UNPACK2(xy_dst)};
    rctf rect = block->rect;
    int oldflag;
    const bool is_horizontal = (panel_aligned(area, region) == BUT_HORIZONTAL);

    if ((panel = block->panel) == 0 || (panel->type && (panel->type->flag & PNL_NO_HEADER))) {
      continue;
    }
    oldflag = panel->flag;

    /* lock one axis */
    if (is_horizontal) {
      xy_b_block[1] = dragcol_data->xy_init[1];
    }
    else {
      xy_b_block[0] = dragcol_data->xy_init[0];
    }

    /* use cursor coords in block space */
    ui_window_to_block_fl(region, block, &xy_a_block[0], &xy_a_block[1]);
    ui_window_to_block_fl(region, block, &xy_b_block[0], &xy_b_block[1]);

    /* set up rect to match header size */
    rect.ymin = rect.ymax;
    rect.ymax = rect.ymin + PNL_HEADER;
    if (panel->flag & PNL_CLOSEDX) {
      rect.xmax = rect.xmin + PNL_HEADER;
    }

    /* touch all panels between last mouse coord and the current one */
    if (BLI_rctf_isect_segment(&rect, xy_a_block, xy_b_block)) {
      /* force panel to close */
      if (dragcol_data->was_first_open == true) {
        panel->flag |= (is_horizontal ? PNL_CLOSEDX : PNL_CLOSEDY);
      }
      /* force panel to open */
      else {
        panel->flag &= ~PNL_CLOSED;
      }

      /* if panel->flag has changed this means a panel was opened/closed here */
      if (panel->flag != oldflag) {
        panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
      }
    }
  }
  /* Update the instanced panel data expand flags with the changes made here. */
  set_panels_list_data_expand_flag(C, region);
}

/**
 * Panel drag-collapse (modal handler)
 * Clicking and dragging over panels toggles their collapse state based on the panel that was first
 * dragged over. If it was open all affected panels incl the initial one are closed and vice versa.
 */
static int ui_panel_drag_collapse_handler(bContext *C, const wmEvent *event, void *userdata)
{
  wmWindow *win = CTX_wm_window(C);
  uiPanelDragCollapseHandle *dragcol_data = userdata;
  short retval = WM_UI_HANDLER_CONTINUE;

  switch (event->type) {
    case MOUSEMOVE:
      ui_panel_drag_collapse(C, dragcol_data, &event->x);

      retval = WM_UI_HANDLER_BREAK;
      break;
    case LEFTMOUSE:
      if (event->val == KM_RELEASE) {
        /* done! */
        WM_event_remove_ui_handler(&win->modalhandlers,
                                   ui_panel_drag_collapse_handler,
                                   ui_panel_drag_collapse_handler_remove,
                                   dragcol_data,
                                   true);
        ui_panel_drag_collapse_handler_remove(C, dragcol_data);
      }
      /* don't let any left-mouse event fall through! */
      retval = WM_UI_HANDLER_BREAK;
      break;
  }

  return retval;
}

static void ui_panel_drag_collapse_handler_add(const bContext *C, const bool was_open)
{
  wmWindow *win = CTX_wm_window(C);
  const wmEvent *event = win->eventstate;
  uiPanelDragCollapseHandle *dragcol_data = MEM_mallocN(sizeof(*dragcol_data), __func__);

  dragcol_data->was_first_open = was_open;
  copy_v2_v2_int(dragcol_data->xy_init, &event->x);

  WM_event_add_ui_handler(C,
                          &win->modalhandlers,
                          ui_panel_drag_collapse_handler,
                          ui_panel_drag_collapse_handler_remove,
                          dragcol_data,
                          0);
}

/* this function is supposed to call general window drawing too */
/* also it supposes a block has panel, and isn't a menu */
static void ui_handle_panel_header(
    const bContext *C, uiBlock *block, int mx, int my, int event, short ctrl, short shift)
{
  ScrArea *area = CTX_wm_area(C);
  ARegion *region = CTX_wm_region(C);
#ifdef USE_PIN_HIDDEN
  const bool show_pin = UI_panel_category_is_visible(region) &&
                        (block->panel->type->parent == NULL) && (block->panel->flag & PNL_PIN);
#else
  const bool show_pin = UI_panel_category_is_visible(region) &&
                        (block->panel->type->parent == NULL);
#endif
  const bool is_subpanel = (block->panel->type && block->panel->type->parent);
  const bool show_drag = !is_subpanel;

  int align = panel_aligned(area, region), button = 0;

  rctf rect_drag, rect_pin;
  float rect_leftmost;

  /* drag and pin rect's */
  rect_drag = block->rect;
  rect_drag.xmin = block->rect.xmax - (PNL_ICON * 1.5f);
  rect_pin = rect_drag;
  if (show_pin) {
    BLI_rctf_translate(&rect_pin, -PNL_ICON, 0.0f);
  }
  rect_leftmost = rect_pin.xmin;

  /* mouse coordinates in panel space! */

  /* XXX weak code, currently it assumes layout style for location of widgets */

  /* check open/collapsed button */
  if (event == EVT_RETKEY) {
    button = 1;
  }
  else if (event == EVT_AKEY) {
    button = 1;
  }
  else if (ELEM(event, 0, EVT_RETKEY, LEFTMOUSE) && shift) {
    if (block->panel->type->parent == NULL) {
      block->panel->flag ^= PNL_PIN;
      button = 2;
    }
  }
  else if (block->panel->flag & PNL_CLOSEDX) {
    if (my >= block->rect.ymax) {
      button = 1;
    }
  }
  else if (block->panel->control & UI_PNL_CLOSE) {
    /* whole of header can be used to collapse panel (except top-right corner) */
    if (mx <= block->rect.xmax - 8 - PNL_ICON) {
      button = 2;
    }
    // else if (mx <= block->rect.xmin + 10 + 2 * PNL_ICON + 2) {
    //  button = 1;
    //}
  }
  else if (mx < rect_leftmost) {
    button = 1;
  }

  if (button) {
    if (button == 2) { /* close */
      ED_region_tag_redraw(region);
    }
    else {
      /* Collapse and expand panels. */

      if (ctrl) {
        /* For parent panels, collapse all other panels or toggle children. */
        if (block->panel->type != NULL && block->panel->type->parent == NULL) {
          if (block->panel->flag & PNL_CLOSED || BLI_listbase_is_empty(&block->panel->children)) {
            panels_collapse_all(C, area, region, block->panel);

            /* Reset the view - we don't want to display a view without content. */
            UI_view2d_offset(&region->v2d, 0.0f, 1.0f);
          }
          else {
            const int closed_flag = (align == BUT_HORIZONTAL) ? PNL_CLOSEDX : PNL_CLOSEDY;
            /* If a panel has sub-panels and it's open, toggle the expansion
             * of the sub-panels (based on the expansion of the first subpanel). */
            Panel *first_child = block->panel->children.first;
            BLI_assert(first_child != NULL);
            panel_set_flag_recursive(
                block->panel, closed_flag, (first_child->flag & PNL_CLOSED) == 0);
            block->panel->flag |= closed_flag;
          }
        }
      }

      if (block->panel->flag & PNL_CLOSED) {
        block->panel->flag &= ~PNL_CLOSED;
        /* snap back up so full panel aligns with screen edge */
        if (block->panel->snap & PNL_SNAP_BOTTOM) {
          block->panel->ofsy = 0;
        }

        if (event == LEFTMOUSE) {
          ui_panel_drag_collapse_handler_add(C, false);
        }
      }
      else if (align == BUT_HORIZONTAL) {
        block->panel->flag |= PNL_CLOSEDX;

        if (event == LEFTMOUSE) {
          ui_panel_drag_collapse_handler_add(C, true);
        }
      }
      else {
        /* snap down to bottom screen edge */
        block->panel->flag |= PNL_CLOSEDY;
        if (block->panel->snap & PNL_SNAP_BOTTOM) {
          block->panel->ofsy = -block->panel->sizey;
        }

        if (event == LEFTMOUSE) {
          ui_panel_drag_collapse_handler_add(C, true);
        }
      }

      set_panels_list_data_expand_flag(C, region);
    }

    if (align) {
      panel_activate_state(C, block->panel, PANEL_STATE_ANIMATION);
    }
    else {
      /* FIXME: this doesn't update the panel drawing, assert to avoid debugging why this is.
       * We could fix this in the future if it's ever needed. */
      BLI_assert(0);
      ED_region_tag_redraw(region);
    }
  }
  else if (show_drag && BLI_rctf_isect_x(&rect_drag, mx)) {
    /* XXX, for now don't allow dragging in floating windows yet. */
    if (region->alignment == RGN_ALIGN_FLOAT) {
      return;
    }
    panel_activate_state(C, block->panel, PANEL_STATE_DRAG);
  }
  else if (show_pin && BLI_rctf_isect_x(&rect_pin, mx)) {
    block->panel->flag ^= PNL_PIN;
    ED_region_tag_redraw(region);
  }
}

bool UI_panel_category_is_visible(const ARegion *region)
{
  /* more than one */
  return region->panels_category.first &&
         region->panels_category.first != region->panels_category.last;
}

PanelCategoryDyn *UI_panel_category_find(ARegion *region, const char *idname)
{
  return BLI_findstring(&region->panels_category, idname, offsetof(PanelCategoryDyn, idname));
}

PanelCategoryStack *UI_panel_category_active_find(ARegion *region, const char *idname)
{
  return BLI_findstring(
      &region->panels_category_active, idname, offsetof(PanelCategoryStack, idname));
}

static void ui_panel_category_active_set(ARegion *region, const char *idname, bool fallback)
{
  ListBase *lb = &region->panels_category_active;
  PanelCategoryStack *pc_act = UI_panel_category_active_find(region, idname);

  if (pc_act) {
    BLI_remlink(lb, pc_act);
  }
  else {
    pc_act = MEM_callocN(sizeof(PanelCategoryStack), __func__);
    BLI_strncpy(pc_act->idname, idname, sizeof(pc_act->idname));
  }

  if (fallback) {
    /* For fallbacks, add at the end so explicitly chosen categories have priority. */
    BLI_addtail(lb, pc_act);
  }
  else {
    BLI_addhead(lb, pc_act);
  }

  /* validate all active panels, we could do this on load,
   * they are harmless - but we should remove somewhere.
   * (addons could define own and gather cruft over time) */
  {
    PanelCategoryStack *pc_act_next;
    /* intentionally skip first */
    pc_act_next = pc_act->next;
    while ((pc_act = pc_act_next)) {
      pc_act_next = pc_act->next;
      if (!BLI_findstring(
              &region->type->paneltypes, pc_act->idname, offsetof(PanelType, category))) {
        BLI_remlink(lb, pc_act);
        MEM_freeN(pc_act);
      }
    }
  }
}

void UI_panel_category_active_set(ARegion *region, const char *idname)
{
  ui_panel_category_active_set(region, idname, false);
}

void UI_panel_category_active_set_default(ARegion *region, const char *idname)
{
  if (!UI_panel_category_active_find(region, idname)) {
    ui_panel_category_active_set(region, idname, true);
  }
}

const char *UI_panel_category_active_get(ARegion *region, bool set_fallback)
{
  LISTBASE_FOREACH (PanelCategoryStack *, pc_act, &region->panels_category_active) {
    if (UI_panel_category_find(region, pc_act->idname)) {
      return pc_act->idname;
    }
  }

  if (set_fallback) {
    PanelCategoryDyn *pc_dyn = region->panels_category.first;
    if (pc_dyn) {
      ui_panel_category_active_set(region, pc_dyn->idname, true);
      return pc_dyn->idname;
    }
  }

  return NULL;
}

PanelCategoryDyn *UI_panel_category_find_mouse_over_ex(ARegion *region, const int x, const int y)
{
  LISTBASE_FOREACH (PanelCategoryDyn *, ptd, &region->panels_category) {
    if (BLI_rcti_isect_pt(&ptd->rect, x, y)) {
      return ptd;
    }
  }

  return NULL;
}

PanelCategoryDyn *UI_panel_category_find_mouse_over(ARegion *region, const wmEvent *event)
{
  return UI_panel_category_find_mouse_over_ex(region, event->mval[0], event->mval[1]);
}

void UI_panel_category_add(ARegion *region, const char *name)
{
  PanelCategoryDyn *pc_dyn = MEM_callocN(sizeof(*pc_dyn), __func__);
  BLI_addtail(&region->panels_category, pc_dyn);

  BLI_strncpy(pc_dyn->idname, name, sizeof(pc_dyn->idname));

  /* 'pc_dyn->rect' must be set on draw */
}

void UI_panel_category_clear_all(ARegion *region)
{
  BLI_freelistN(&region->panels_category);
}

static void imm_buf_append(
    float vbuf[][2], uchar cbuf[][3], float x, float y, const uchar col[3], int *index)
{
  ARRAY_SET_ITEMS(vbuf[*index], x, y);
  ARRAY_SET_ITEMS(cbuf[*index], UNPACK3(col));
  (*index)++;
}

/* based on UI_draw_roundbox, check on making a version which allows us to skip some sides */
static void ui_panel_category_draw_tab(bool filled,
                                       float minx,
                                       float miny,
                                       float maxx,
                                       float maxy,
                                       float rad,
                                       const int roundboxtype,
                                       const bool use_highlight,
                                       const bool use_shadow,
                                       const bool use_flip_x,
                                       const uchar highlight_fade[3],
                                       const uchar col[3])
{
  float vec[4][2] = {{0.195, 0.02}, {0.55, 0.169}, {0.831, 0.45}, {0.98, 0.805}};
  int a;

  /* mult */
  for (a = 0; a < 4; a++) {
    mul_v2_fl(vec[a], rad);
  }

  uint vert_len = 0;
  if (use_highlight) {
    vert_len += (roundboxtype & UI_CNR_TOP_RIGHT) ? 6 : 1;
    vert_len += (roundboxtype & UI_CNR_TOP_LEFT) ? 6 : 1;
  }
  if (use_highlight && !use_shadow) {
    vert_len++;
  }
  else {
    vert_len += (roundboxtype & UI_CNR_BOTTOM_RIGHT) ? 6 : 1;
    vert_len += (roundboxtype & UI_CNR_BOTTOM_LEFT) ? 6 : 1;
  }
  /* Maximum size. */
  float vbuf[24][2];
  uchar cbuf[24][3];
  int buf_index = 0;

  /* start with corner right-top */
  if (use_highlight) {
    if (roundboxtype & UI_CNR_TOP_RIGHT) {
      imm_buf_append(vbuf, cbuf, maxx, maxy - rad, col, &buf_index);
      for (a = 0; a < 4; a++) {
        imm_buf_append(vbuf, cbuf, maxx - vec[a][1], maxy - rad + vec[a][0], col, &buf_index);
      }
      imm_buf_append(vbuf, cbuf, maxx - rad, maxy, col, &buf_index);
    }
    else {
      imm_buf_append(vbuf, cbuf, maxx, maxy, col, &buf_index);
    }

    /* corner left-top */
    if (roundboxtype & UI_CNR_TOP_LEFT) {
      imm_buf_append(vbuf, cbuf, minx + rad, maxy, col, &buf_index);
      for (a = 0; a < 4; a++) {
        imm_buf_append(vbuf, cbuf, minx + rad - vec[a][0], maxy - vec[a][1], col, &buf_index);
      }
      imm_buf_append(vbuf, cbuf, minx, maxy - rad, col, &buf_index);
    }
    else {
      imm_buf_append(vbuf, cbuf, minx, maxy, col, &buf_index);
    }
  }

  if (use_highlight && !use_shadow) {
    imm_buf_append(
        vbuf, cbuf, minx, miny + rad, highlight_fade ? col : highlight_fade, &buf_index);
  }
  else {
    /* corner left-bottom */
    if (roundboxtype & UI_CNR_BOTTOM_LEFT) {
      imm_buf_append(vbuf, cbuf, minx, miny + rad, col, &buf_index);
      for (a = 0; a < 4; a++) {
        imm_buf_append(vbuf, cbuf, minx + vec[a][1], miny + rad - vec[a][0], col, &buf_index);
      }
      imm_buf_append(vbuf, cbuf, minx + rad, miny, col, &buf_index);
    }
    else {
      imm_buf_append(vbuf, cbuf, minx, miny, col, &buf_index);
    }

    /* corner right-bottom */
    if (roundboxtype & UI_CNR_BOTTOM_RIGHT) {
      imm_buf_append(vbuf, cbuf, maxx - rad, miny, col, &buf_index);
      for (a = 0; a < 4; a++) {
        imm_buf_append(vbuf, cbuf, maxx - rad + vec[a][0], miny + vec[a][1], col, &buf_index);
      }
      imm_buf_append(vbuf, cbuf, maxx, miny + rad, col, &buf_index);
    }
    else {
      imm_buf_append(vbuf, cbuf, maxx, miny, col, &buf_index);
    }
  }

  if (use_flip_x) {
    float midx = (minx + maxx) / 2.0f;
    for (int i = 0; i < buf_index; i++) {
      vbuf[i][0] = midx - (vbuf[i][0] - midx);
    }
  }

  GPUVertFormat *format = immVertexFormat();
  uint pos = GPU_vertformat_attr_add(format, "pos", GPU_COMP_F32, 2, GPU_FETCH_FLOAT);
  uint color = GPU_vertformat_attr_add(
      format, "color", GPU_COMP_U8, 3, GPU_FETCH_INT_TO_FLOAT_UNIT);

  immBindBuiltinProgram(GPU_SHADER_2D_SMOOTH_COLOR);
  immBegin(filled ? GPU_PRIM_TRI_FAN : GPU_PRIM_LINE_STRIP, vert_len);
  for (int i = 0; i < buf_index; i++) {
    immAttr3ubv(color, cbuf[i]);
    immVertex2fv(pos, vbuf[i]);
  }
  immEnd();
  immUnbindProgram();
}

/**
 * Draw vertical tabs on the left side of the region,
 * one tab per category.
 */
void UI_panel_category_draw_all(ARegion *region, const char *category_id_active)
{
  /* no tab outlines for */
  // #define USE_FLAT_INACTIVE
  const bool is_left = RGN_ALIGN_ENUM_FROM_MASK(region->alignment != RGN_ALIGN_RIGHT);
  View2D *v2d = &region->v2d;
  const uiStyle *style = UI_style_get();
  const uiFontStyle *fstyle = &style->widget;
  const int fontid = fstyle->uifont_id;
  short fstyle_points = fstyle->points;
  const float aspect = ((uiBlock *)region->uiblocks.first)->aspect;
  const float zoom = 1.0f / aspect;
  const int px = max_ii(1, round_fl_to_int(U.pixelsize));
  const int px_x_sign = is_left ? px : -px;
  const int category_tabs_width = round_fl_to_int(UI_PANEL_CATEGORY_MARGIN_WIDTH * zoom);
  const float dpi_fac = UI_DPI_FAC;
  /* padding of tabs around text */
  const int tab_v_pad_text = round_fl_to_int((2 + ((px * 3) * dpi_fac)) * zoom);
  /* padding between tabs */
  const int tab_v_pad = round_fl_to_int((4 + (2 * px * dpi_fac)) * zoom);
  const float tab_curve_radius = ((px * 3) * dpi_fac) * zoom;
  /* We flip the tab drawing, so always use these flags. */
  const int roundboxtype = UI_CNR_TOP_LEFT | UI_CNR_BOTTOM_LEFT;
  bool is_alpha;
  bool do_scaletabs = false;
#ifdef USE_FLAT_INACTIVE
  bool is_active_prev = false;
#endif
  float scaletabs = 1.0f;
  /* same for all tabs */
  /* intentionally dont scale by 'px' */
  const int rct_xmin = is_left ? v2d->mask.xmin + 3 : (v2d->mask.xmax - category_tabs_width);
  const int rct_xmax = is_left ? v2d->mask.xmin + category_tabs_width : (v2d->mask.xmax - 3);
  const int text_v_ofs = (rct_xmax - rct_xmin) * 0.3f;

  int y_ofs = tab_v_pad;

  /* Primary theme colors */
  uchar theme_col_back[4];
  uchar theme_col_text[3];
  uchar theme_col_text_hi[3];

  /* Tab colors */
  uchar theme_col_tab_bg[4];
  uchar theme_col_tab_active[3];
  uchar theme_col_tab_inactive[3];

  /* Secondary theme colors */
  uchar theme_col_tab_outline[3];
  uchar theme_col_tab_divider[3]; /* line that divides tabs from the main region */
  uchar theme_col_tab_highlight[3];
  uchar theme_col_tab_highlight_inactive[3];

  UI_GetThemeColor4ubv(TH_BACK, theme_col_back);
  UI_GetThemeColor3ubv(TH_TEXT, theme_col_text);
  UI_GetThemeColor3ubv(TH_TEXT_HI, theme_col_text_hi);

  UI_GetThemeColor4ubv(TH_TAB_BACK, theme_col_tab_bg);
  UI_GetThemeColor3ubv(TH_TAB_ACTIVE, theme_col_tab_active);
  UI_GetThemeColor3ubv(TH_TAB_INACTIVE, theme_col_tab_inactive);
  UI_GetThemeColor3ubv(TH_TAB_OUTLINE, theme_col_tab_outline);

  interp_v3_v3v3_uchar(theme_col_tab_divider, theme_col_back, theme_col_tab_outline, 0.3f);
  interp_v3_v3v3_uchar(theme_col_tab_highlight, theme_col_back, theme_col_text_hi, 0.2f);
  interp_v3_v3v3_uchar(
      theme_col_tab_highlight_inactive, theme_col_tab_inactive, theme_col_text_hi, 0.12f);

  is_alpha = (region->overlap && (theme_col_back[3] != 255));

  if (fstyle->kerning == 1) {
    BLF_enable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
  }

  BLF_enable(fontid, BLF_ROTATION);
  BLF_rotation(fontid, M_PI_2);
  // UI_fontstyle_set(&style->widget);
  ui_fontscale(&fstyle_points, aspect / (U.pixelsize * 1.1f));
  BLF_size(fontid, fstyle_points, U.dpi);

  /* Check the region type supports categories to avoid an assert
   * for showing 3D view panels in the properties space. */
  if ((1 << region->regiontype) & RGN_TYPE_HAS_CATEGORY_MASK) {
    BLI_assert(UI_panel_category_is_visible(region));
  }

  /* calculate tab rect's and check if we need to scale down */
  LISTBASE_FOREACH (PanelCategoryDyn *, pc_dyn, &region->panels_category) {

    rcti *rct = &pc_dyn->rect;
    const char *category_id = pc_dyn->idname;
    const char *category_id_draw = IFACE_(category_id);
    const int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);

    rct->xmin = rct_xmin;
    rct->xmax = rct_xmax;

    rct->ymin = v2d->mask.ymax - (y_ofs + category_width + (tab_v_pad_text * 2));
    rct->ymax = v2d->mask.ymax - (y_ofs);

    y_ofs += category_width + tab_v_pad + (tab_v_pad_text * 2);
  }

  if (y_ofs > BLI_rcti_size_y(&v2d->mask)) {
    scaletabs = (float)BLI_rcti_size_y(&v2d->mask) / (float)y_ofs;

    LISTBASE_FOREACH (PanelCategoryDyn *, pc_dyn, &region->panels_category) {
      rcti *rct = &pc_dyn->rect;
      rct->ymin = ((rct->ymin - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
      rct->ymax = ((rct->ymax - v2d->mask.ymax) * scaletabs) + v2d->mask.ymax;
    }

    do_scaletabs = true;
  }

  /* begin drawing */
  GPU_line_smooth(true);

  uint pos = GPU_vertformat_attr_add(
      immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
  immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);

  /* draw the background */
  if (is_alpha) {
    GPU_blend(true);
    immUniformColor4ubv(theme_col_tab_bg);
  }
  else {
    immUniformColor3ubv(theme_col_tab_bg);
  }

  if (is_left) {
    immRecti(
        pos, v2d->mask.xmin, v2d->mask.ymin, v2d->mask.xmin + category_tabs_width, v2d->mask.ymax);
  }
  else {
    immRecti(
        pos, v2d->mask.xmax - category_tabs_width, v2d->mask.ymin, v2d->mask.xmax, v2d->mask.ymax);
  }

  if (is_alpha) {
    GPU_blend(false);
  }

  immUnbindProgram();

  const int divider_xmin = is_left ? (v2d->mask.xmin + (category_tabs_width - px)) :
                                     (v2d->mask.xmax - category_tabs_width) + px;
  const int divider_xmax = is_left ? (v2d->mask.xmin + category_tabs_width) :
                                     (v2d->mask.xmax - (category_tabs_width + px)) + px;

  LISTBASE_FOREACH (PanelCategoryDyn *, pc_dyn, &region->panels_category) {
    const rcti *rct = &pc_dyn->rect;
    const char *category_id = pc_dyn->idname;
    const char *category_id_draw = IFACE_(category_id);
    int category_width = BLI_rcti_size_y(rct) - (tab_v_pad_text * 2);
    size_t category_draw_len = BLF_DRAW_STR_DUMMY_MAX;
    // int category_width = BLF_width(fontid, category_id_draw, BLF_DRAW_STR_DUMMY_MAX);

    const bool is_active = STREQ(category_id, category_id_active);

    GPU_blend(true);

#ifdef USE_FLAT_INACTIVE
    if (is_active)
#endif
    {
      const bool use_flip_x = !is_left;
      ui_panel_category_draw_tab(true,
                                 rct->xmin,
                                 rct->ymin,
                                 rct->xmax,
                                 rct->ymax,
                                 tab_curve_radius - px,
                                 roundboxtype,
                                 true,
                                 true,
                                 use_flip_x,
                                 NULL,
                                 is_active ? theme_col_tab_active : theme_col_tab_inactive);

      /* tab outline */
      ui_panel_category_draw_tab(false,
                                 rct->xmin - px_x_sign,
                                 rct->ymin - px,
                                 rct->xmax - px_x_sign,
                                 rct->ymax + px,
                                 tab_curve_radius,
                                 roundboxtype,
                                 true,
                                 true,
                                 use_flip_x,
                                 NULL,
                                 theme_col_tab_outline);

      /* tab highlight (3d look) */
      ui_panel_category_draw_tab(false,
                                 rct->xmin,
                                 rct->ymin,
                                 rct->xmax,
                                 rct->ymax,
                                 tab_curve_radius,
                                 roundboxtype,
                                 true,
                                 false,
                                 use_flip_x,
                                 is_active ? theme_col_back : theme_col_tab_inactive,
                                 is_active ? theme_col_tab_highlight :
                                             theme_col_tab_highlight_inactive);
    }

    /* tab blackline */
    if (!is_active) {
      pos = GPU_vertformat_attr_add(
          immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
      immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);

      immUniformColor3ubv(theme_col_tab_divider);
      immRecti(pos, divider_xmin, rct->ymin - tab_v_pad, divider_xmax, rct->ymax + tab_v_pad);
      immUnbindProgram();
    }

    if (do_scaletabs) {
      category_draw_len = BLF_width_to_strlen(
          fontid, category_id_draw, category_draw_len, category_width, NULL);
    }

    BLF_position(fontid, rct->xmax - text_v_ofs, rct->ymin + tab_v_pad_text, 0.0f);

    /* tab titles */

    /* draw white shadow to give text more depth */
    BLF_color3ubv(fontid, theme_col_text);

    /* main tab title */
    BLF_draw(fontid, category_id_draw, category_draw_len);

    GPU_blend(false);

    /* tab blackline remaining (last tab) */
    pos = GPU_vertformat_attr_add(
        immVertexFormat(), "pos", GPU_COMP_I32, 2, GPU_FETCH_INT_TO_FLOAT);
    immBindBuiltinProgram(GPU_SHADER_2D_UNIFORM_COLOR);
    if (pc_dyn->prev == NULL) {
      immUniformColor3ubv(theme_col_tab_divider);
      immRecti(pos, divider_xmin, rct->ymax + px, divider_xmax, v2d->mask.ymax);
    }
    if (pc_dyn->next == NULL) {
      immUniformColor3ubv(theme_col_tab_divider);
      immRecti(pos, divider_xmin, 0, divider_xmax, rct->ymin);
    }

#ifdef USE_FLAT_INACTIVE
    /* draw line between inactive tabs */
    if (is_active == false && is_active_prev == false && pc_dyn->prev) {
      immUniformColor3ubv(theme_col_tab_divider);
      immRecti(pos,
               v2d->mask.xmin + (category_tabs_width / 5),
               rct->ymax + px,
               (v2d->mask.xmin + category_tabs_width) - (category_tabs_width / 5),
               rct->ymax + (px * 3));
    }

    is_active_prev = is_active;
#endif
    immUnbindProgram();

    /* not essential, but allows events to be handled right up until the region edge [#38171] */
    if (is_left) {
      pc_dyn->rect.xmin = v2d->mask.xmin;
    }
    else {
      pc_dyn->rect.xmax = v2d->mask.xmax;
    }
  }

  GPU_line_smooth(false);

  BLF_disable(fontid, BLF_ROTATION);

  if (fstyle->kerning == 1) {
    BLF_disable(fstyle->uifont_id, BLF_KERNING_DEFAULT);
  }

#undef USE_FLAT_INACTIVE
}

static int ui_handle_panel_category_cycling(const wmEvent *event,
                                            ARegion *region,
                                            const uiBut *active_but)
{
  const bool is_mousewheel = ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE);
  const bool inside_tabregion =
      ((RGN_ALIGN_ENUM_FROM_MASK(region->alignment) != RGN_ALIGN_RIGHT) ?
           (event->mval[0] < ((PanelCategoryDyn *)region->panels_category.first)->rect.xmax) :
           (event->mval[0] > ((PanelCategoryDyn *)region->panels_category.first)->rect.xmin));

  /* if mouse is inside non-tab region, ctrl key is required */
  if (is_mousewheel && !event->ctrl && !inside_tabregion) {
    return WM_UI_HANDLER_CONTINUE;
  }

  if (active_but && ui_but_supports_cycling(active_but)) {
    /* skip - exception to make cycling buttons
     * using ctrl+mousewheel work in tabbed regions */
  }
  else {
    const char *category = UI_panel_category_active_get(region, false);
    if (LIKELY(category)) {
      PanelCategoryDyn *pc_dyn = UI_panel_category_find(region, category);
      if (LIKELY(pc_dyn)) {
        if (is_mousewheel) {
          /* we can probably get rid of this and only allow ctrl+tabbing */
          pc_dyn = (event->type == WHEELDOWNMOUSE) ? pc_dyn->next : pc_dyn->prev;
        }
        else {
          const bool backwards = event->shift;
          pc_dyn = backwards ? pc_dyn->prev : pc_dyn->next;
          if (!pc_dyn) {
            /* proper cyclic behavior,
             * back to first/last category (only used for ctrl+tab) */
            pc_dyn = backwards ? region->panels_category.last : region->panels_category.first;
          }
        }

        if (pc_dyn) {
          /* intentionally don't reset scroll in this case,
           * this allows for quick browsing between tabs */
          UI_panel_category_active_set(region, pc_dyn->idname);
          ED_region_tag_redraw(region);
        }
      }
    }
    return WM_UI_HANDLER_BREAK;
  }

  return WM_UI_HANDLER_CONTINUE;
}

/* XXX should become modal keymap */
/* AKey is opening/closing panels, independent of button state now */

int ui_handler_panel_region(bContext *C,
                            const wmEvent *event,
                            ARegion *region,
                            const uiBut *active_but)
{
  Panel *panel;
  int retval, mx, my;
  bool has_category_tabs = UI_panel_category_is_visible(region);

  retval = WM_UI_HANDLER_CONTINUE;

  /* Scrollbars can overlap panels now, they have handling priority. */
  if (UI_view2d_mouse_in_scrollers(region, &region->v2d, event->x, event->y)) {
    return retval;
  }

  /* handle category tabs */
  if (has_category_tabs) {
    if (event->val == KM_PRESS) {
      if (event->type == LEFTMOUSE) {
        PanelCategoryDyn *pc_dyn = UI_panel_category_find_mouse_over(region, event);
        if (pc_dyn) {
          UI_panel_category_active_set(region, pc_dyn->idname);
          ED_region_tag_redraw(region);

          /* reset scroll to the top [#38348] */
          UI_view2d_offset(&region->v2d, -1.0f, 1.0f);

          retval = WM_UI_HANDLER_BREAK;
        }
      }
      else if ((event->type == EVT_TABKEY && event->ctrl) ||
               ELEM(event->type, WHEELUPMOUSE, WHEELDOWNMOUSE)) {
        /* cycle tabs */
        retval = ui_handle_panel_category_cycling(event, region, active_but);
      }
    }
  }

  if (retval == WM_UI_HANDLER_BREAK) {
    return retval;
  }

  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    uiPanelMouseState mouse_state;

    mx = event->x;
    my = event->y;
    ui_window_to_block(region, block, &mx, &my);

    /* checks for mouse position inside */
    panel = block->panel;

    if (!panel) {
      continue;
    }
    /* XXX - accessed freed panels when scripts reload, need to fix. */
    if (panel->type && panel->type->flag & PNL_NO_HEADER) {
      continue;
    }

    mouse_state = ui_panel_mouse_state_get(block, panel, mx, my);

    /* XXX hardcoded key warning */
    if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER) &&
        event->val == KM_PRESS) {
      if (event->type == EVT_AKEY &&
          ((event->ctrl + event->oskey + event->shift + event->alt) == 0)) {

        if (panel->flag & PNL_CLOSEDY) {
          if ((block->rect.ymax <= my) && (block->rect.ymax + PNL_HEADER >= my)) {
            ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
          }
        }
        else {
          ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
        }

        retval = WM_UI_HANDLER_BREAK;
        continue;
      }
    }

    /* on active button, do not handle panels */
    if (ui_region_find_active_but(region) != NULL) {
      continue;
    }

    if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER)) {

      if (event->val == KM_PRESS) {

        /* open close on header */
        if (ELEM(event->type, EVT_RETKEY, EVT_PADENTER)) {
          if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
            ui_handle_panel_header(C, block, mx, my, EVT_RETKEY, event->ctrl, event->shift);
            retval = WM_UI_HANDLER_BREAK;
            break;
          }
        }
        else if (event->type == LEFTMOUSE) {
          /* all inside clicks should return in break - overlapping/float panels */
          retval = WM_UI_HANDLER_BREAK;

          if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
            ui_handle_panel_header(C, block, mx, my, event->type, event->ctrl, event->shift);
            retval = WM_UI_HANDLER_BREAK;
            break;
          }
          if ((mouse_state == PANEL_MOUSE_INSIDE_SCALE) && !(panel->flag & PNL_CLOSED)) {
            panel_activate_state(C, panel, PANEL_STATE_DRAG_SCALE);
            retval = WM_UI_HANDLER_BREAK;
            break;
          }
        }
        else if (event->type == RIGHTMOUSE) {
          if (mouse_state == PANEL_MOUSE_INSIDE_HEADER) {
            ui_popup_context_menu_for_panel(C, region, block->panel);
            retval = WM_UI_HANDLER_BREAK;
            break;
          }
        }
        else if (event->type == EVT_ESCKEY) {
          /*XXX 2.50*/
#if 0
          if (block->handler) {
            rem_blockhandler(area, block->handler);
            ED_region_tag_redraw(region);
            retval = WM_UI_HANDLER_BREAK;
          }
#endif
        }
        else if (event->type == EVT_PADPLUSKEY || event->type == EVT_PADMINUS) {
#if 0 /* XXX make float panel exception? */
          int zoom = 0;

          /* if panel is closed, only zoom if mouse is over the header */
          if (panel->flag & (PNL_CLOSEDX | PNL_CLOSEDY)) {
            if (inside_header) {
              zoom = 1;
            }
          }
          else {
            zoom = 1;
          }

          if (zoom) {
            ScrArea *area = CTX_wm_area(C);
            SpaceLink *sl = area->spacedata.first;

            if (area->spacetype != SPACE_PROPERTIES) {
              if (!(panel->control & UI_PNL_SCALE)) {
                if (event->type == PADPLUSKEY) {
                  sl->blockscale += 0.1;
                }
                else {
                  sl->blockscale -= 0.1;
                }
                CLAMP(sl->blockscale, 0.6, 1.0);

                ED_region_tag_redraw(region);
                retval = WM_UI_HANDLER_BREAK;
              }
            }
          }
#endif
        }
      }
    }
  }

  return retval;
}

static void ui_panel_custom_data_set_recursive(Panel *panel, PointerRNA *custom_data)
{
  panel->runtime.custom_data_ptr = custom_data;

  LISTBASE_FOREACH (Panel *, child_panel, &panel->children) {
    ui_panel_custom_data_set_recursive(child_panel, custom_data);
  }
}

void UI_panel_custom_data_set(Panel *panel, PointerRNA *custom_data)
{
  BLI_assert(panel->type != NULL);

  /* Free the old custom data, which should be shared among all of the panel's sub-panels. */
  if (panel->runtime.custom_data_ptr != NULL) {
    MEM_freeN(panel->runtime.custom_data_ptr);
  }

  ui_panel_custom_data_set_recursive(panel, custom_data);
}

PointerRNA *UI_region_panel_custom_data_under_cursor(const bContext *C, const wmEvent *event)
{
  ARegion *region = CTX_wm_region(C);

  Panel *panel = NULL;
  LISTBASE_FOREACH (uiBlock *, block, &region->uiblocks) {
    panel = block->panel;
    if (panel == NULL) {
      continue;
    }

    int mx = event->x;
    int my = event->y;
    ui_window_to_block(region, block, &mx, &my);
    int mouse_state = ui_panel_mouse_state_get(block, panel, mx, my);
    if (ELEM(mouse_state, PANEL_MOUSE_INSIDE_CONTENT, PANEL_MOUSE_INSIDE_HEADER)) {
      break;
    }
  }

  if (panel == NULL) {
    return NULL;
  }

  PointerRNA *customdata = panel->runtime.custom_data_ptr;

  return customdata;
}

/**************** window level modal panel interaction **************/

/* note, this is modal handler and should not swallow events for animation */
static int ui_handler_panel(bContext *C, const wmEvent *event, void *userdata)
{
  Panel *panel = userdata;
  uiHandlePanelData *data = panel->activedata;

  /* verify if we can stop */
  if (event->type == LEFTMOUSE && event->val == KM_RELEASE) {
    ScrArea *area = CTX_wm_area(C);
    ARegion *region = CTX_wm_region(C);
    int align = panel_aligned(area, region);

    if (align) {
      panel_activate_state(C, panel, PANEL_STATE_ANIMATION);
    }
    else {
      panel_activate_state(C, panel, PANEL_STATE_EXIT);
    }
  }
  else if (event->type == MOUSEMOVE) {
    if (data->state == PANEL_STATE_DRAG) {
      ui_do_drag(C, event, panel);
    }
  }
  else if (event->type == TIMER && event->customdata == data->animtimer) {
    if (data->state == PANEL_STATE_ANIMATION) {
      ui_do_animate(C, panel);
    }
    else if (data->state == PANEL_STATE_DRAG) {
      ui_do_drag(C, event, panel);
    }
  }

  data = panel->activedata;

  if (data && data->state == PANEL_STATE_ANIMATION) {
    return WM_UI_HANDLER_CONTINUE;
  }
  return WM_UI_HANDLER_BREAK;
}

static void ui_handler_remove_panel(bContext *C, void *userdata)
{
  Panel *panel = userdata;

  panel_activate_state(C, panel, PANEL_STATE_EXIT);
}

static void panel_activate_state(const bContext *C, Panel *panel, uiHandlePanelState state)
{
  uiHandlePanelData *data = panel->activedata;
  wmWindow *win = CTX_wm_window(C);
  ARegion *region = CTX_wm_region(C);

  if (data && data->state == state) {
    return;
  }

  bool was_drag_drop = (data && data->state == PANEL_STATE_DRAG);

  /* Set selection state for the panel and its sub-panels, which need to know they are selected
   * too so they can be drawn above their parent when it's dragged. */
  if (state == PANEL_STATE_EXIT || state == PANEL_STATE_ANIMATION) {
    if (data && data->state != PANEL_STATE_ANIMATION) {
      /* XXX:
       * - the panel tabbing function call below (test_add_new_tabs()) has been commented out
       *   "It is too easy to do by accident when reordering panels,
       *   is very hard to control and use, and has no real benefit." - BillRey
       * Aligorith, 2009Sep
       */
      // test_add_new_tabs(region);   // also copies locations of tabs in dragged panel
      check_panel_overlap(region, NULL); /* clears */
    }

    panel_set_flag_recursive(panel, PNL_SELECT, false);
  }
  else {
    panel_set_flag_recursive(panel, PNL_SELECT, true);
  }

  if (data && data->animtimer) {
    WM_event_remove_timer(CTX_wm_manager(C), win, data->animtimer);
    data->animtimer = NULL;
  }

  if (state == PANEL_STATE_EXIT) {
    MEM_freeN(data);
    panel->activedata = NULL;

    WM_event_remove_ui_handler(
        &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, panel, false);
  }
  else {
    if (!data) {
      data = MEM_callocN(sizeof(uiHandlePanelData), "uiHandlePanelData");
      panel->activedata = data;

      WM_event_add_ui_handler(
          C, &win->modalhandlers, ui_handler_panel, ui_handler_remove_panel, panel, 0);
    }

    if (ELEM(state, PANEL_STATE_ANIMATION, PANEL_STATE_DRAG)) {
      data->animtimer = WM_event_add_timer(CTX_wm_manager(C), win, TIMER, ANIMATION_INTERVAL);
    }

    /* Initiate edge panning during drags so we can move beyond the initial region view. */
    if (state == PANEL_STATE_DRAG) {
      wmOperatorType *ot = WM_operatortype_find("VIEW2D_OT_edge_pan", true);
      ui_handle_afterfunc_add_operator(ot, WM_OP_INVOKE_DEFAULT, true);
    }

    data->state = state;
    data->startx = win->eventstate->x;
    data->starty = win->eventstate->y;
    data->startofsx = panel->ofsx;
    data->startofsy = panel->ofsy;
    data->startsizex = panel->sizex;
    data->startsizey = panel->sizey;
    data->start_cur_xmin = region->v2d.cur.xmin;
    data->start_cur_ymin = region->v2d.cur.ymin;
    data->starttime = PIL_check_seconds_timer();

    /* Remember drag drop state even when animating to the aligned position after dragging. */
    data->is_drag_drop = was_drag_drop;
    if (state == PANEL_STATE_DRAG) {
      data->is_drag_drop = true;
    }
  }

  ED_region_tag_redraw(region);
}

PanelType *UI_paneltype_find(int space_id, int region_id, const char *idname)
{
  SpaceType *st = BKE_spacetype_from_id(space_id);
  if (st) {
    ARegionType *art = BKE_regiontype_from_id(st, region_id);
    if (art) {
      return BLI_findstring(&art->paneltypes, idname, offsetof(PanelType, idname));
    }
  }
  return NULL;
}