1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
|
/*
* ***** BEGIN GPL LICENSE BLOCK *****
*
* 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) 2008 Blender Foundation.
* All rights reserved.
*
* Contributor(s): Blender Foundation
*
* ***** END GPL LICENSE BLOCK *****
*/
/** \file blender/editors/interface/interface_regions.c
* \ingroup edinterface
*/
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include "MEM_guardedalloc.h"
#include "DNA_userdef_types.h"
#include "BLI_math.h"
#include "BLI_blenlib.h"
#include "BLI_utildefines.h"
#include "BLI_ghash.h"
#include "PIL_time.h"
#include "BKE_context.h"
#include "BKE_screen.h"
#include "BKE_report.h"
#include "BKE_global.h"
#include "WM_api.h"
#include "WM_types.h"
#include "wm_draw.h"
#include "wm_subwindow.h"
#include "RNA_access.h"
#include "BIF_gl.h"
#include "UI_interface.h"
#include "UI_interface_icons.h"
#include "UI_view2d.h"
#include "BLF_api.h"
#include "BLF_translation.h"
#include "ED_screen.h"
#include "IMB_colormanagement.h"
#include "interface_intern.h"
#define MENU_TOP (int)(8 * UI_DPI_FAC)
#define MENU_PADDING (int)(0.2f * UI_UNIT_Y)
#define MENU_BORDER (int)(0.3f * U.widget_unit)
static int rna_property_enum_step(const bContext *C, PointerRNA *ptr, PropertyRNA *prop, int direction)
{
EnumPropertyItem *item_array;
int totitem;
bool free;
int value;
int i, i_init;
int step = (direction < 0) ? -1 : 1;
int step_tot = 0;
RNA_property_enum_items((bContext *)C, ptr, prop, &item_array, &totitem, &free);
value = RNA_property_enum_get(ptr, prop);
i = RNA_enum_from_value(item_array, value);
i_init = i;
do {
i = mod_i(i + step, totitem);
if (item_array[i].identifier[0]) {
step_tot += step;
}
} while ((i != i_init) && (step_tot != direction));
if (i != i_init) {
value = item_array[i].value;
}
if (free) {
MEM_freeN(item_array);
}
return value;
}
int ui_but_menu_step(uiBut *but, int direction)
{
/* currenly only RNA buttons */
if ((but->rnaprop == NULL) || (RNA_property_type(but->rnaprop) != PROP_ENUM)) {
printf("%s: cannot cycle button '%s'\n", __func__, but->str);
return 0;
}
return rna_property_enum_step(but->block->evil_C, &but->rnapoin, but->rnaprop, direction);
}
/******************** Creating Temporary regions ******************/
static ARegion *ui_region_temp_add(bScreen *sc)
{
ARegion *ar;
ar = MEM_callocN(sizeof(ARegion), "area region");
BLI_addtail(&sc->regionbase, ar);
ar->regiontype = RGN_TYPE_TEMPORARY;
ar->alignment = RGN_ALIGN_FLOAT;
return ar;
}
static void ui_region_temp_remove(bContext *C, bScreen *sc, ARegion *ar)
{
wmWindow *win = CTX_wm_window(C);
if (win)
wm_draw_region_clear(win, ar);
ED_region_exit(C, ar);
BKE_area_region_free(NULL, ar); /* NULL: no spacetype */
BLI_freelinkN(&sc->regionbase, ar);
}
/************************* Creating Tooltips **********************/
#define UI_TIP_PAD_FAC 1.3f
#define UI_TIP_PADDING (int)(UI_TIP_PAD_FAC * UI_UNIT_Y)
#define MAX_TOOLTIP_LINES 8
typedef struct uiTooltipData {
rcti bbox;
uiFontStyle fstyle;
char lines[MAX_TOOLTIP_LINES][512];
char header[512], active_info[512];
struct {
enum {
UI_TIP_STYLE_NORMAL = 0,
UI_TIP_STYLE_HEADER,
UI_TIP_STYLE_MONO,
} style : 3;
enum {
UI_TIP_LC_MAIN = 0, /* primary text */
UI_TIP_LC_VALUE, /* the value of buttons (also shortcuts) */
UI_TIP_LC_ACTIVE, /* titles of active enum values */
UI_TIP_LC_NORMAL, /* regular text */
UI_TIP_LC_PYTHON, /* Python snippet */
UI_TIP_LC_ALERT, /* description of why operator can't run */
} color_id : 4;
int is_pad : 1;
} format[MAX_TOOLTIP_LINES];
int totline;
int toth, lineh;
} uiTooltipData;
#define UI_TIP_LC_MAX 6
BLI_STATIC_ASSERT(UI_TIP_LC_MAX == UI_TIP_LC_ALERT + 1, "invalid lc-max");
BLI_STATIC_ASSERT(sizeof(((uiTooltipData *)NULL)->format[0]) <= sizeof(int), "oversize");
static void rgb_tint(
float col[3],
float h, float h_strength,
float v, float v_strength)
{
float col_hsv_from[3];
float col_hsv_to[3];
rgb_to_hsv_v(col, col_hsv_from);
col_hsv_to[0] = h;
col_hsv_to[1] = h_strength;
col_hsv_to[2] = (col_hsv_from[2] * (1.0f - v_strength)) + (v * v_strength);
hsv_to_rgb_v(col_hsv_to, col);
}
static void ui_tooltip_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
{
const float pad_px = UI_TIP_PADDING;
uiTooltipData *data = ar->regiondata;
uiWidgetColors *theme = ui_tooltip_get_theme();
rcti bbox = data->bbox;
float tip_colors[UI_TIP_LC_MAX][3];
float *main_color = tip_colors[UI_TIP_LC_MAIN]; /* the color from the theme */
float *value_color = tip_colors[UI_TIP_LC_VALUE];
float *active_color = tip_colors[UI_TIP_LC_ACTIVE];
float *normal_color = tip_colors[UI_TIP_LC_NORMAL];
float *python_color = tip_colors[UI_TIP_LC_PYTHON];
float *alert_color = tip_colors[UI_TIP_LC_ALERT];
float background_color[3];
float tone_bg;
int i, multisample_enabled;
/* disable AA, makes widgets too blurry */
multisample_enabled = glIsEnabled(GL_MULTISAMPLE_ARB);
if (multisample_enabled)
glDisable(GL_MULTISAMPLE_ARB);
wmOrtho2_region_ui(ar);
/* draw background */
ui_draw_tooltip_background(UI_style_get(), NULL, &bbox);
/* set background_color */
rgb_uchar_to_float(background_color, (const unsigned char *)theme->inner);
/* calculate normal_color */
rgb_uchar_to_float(main_color, (const unsigned char *)theme->text);
copy_v3_v3(active_color, main_color);
copy_v3_v3(normal_color, main_color);
copy_v3_v3(python_color, main_color);
copy_v3_v3(alert_color, main_color);
copy_v3_v3(value_color, main_color);
/* find the brightness difference between background and text colors */
tone_bg = rgb_to_grayscale(background_color);
/* tone_fg = rgb_to_grayscale(main_color); */
/* mix the colors */
rgb_tint(value_color, 0.0f, 0.0f, tone_bg, 0.2f); /* light gray */
rgb_tint(active_color, 0.6f, 0.2f, tone_bg, 0.2f); /* light blue */
rgb_tint(normal_color, 0.0f, 0.0f, tone_bg, 0.4f); /* gray */
rgb_tint(python_color, 0.0f, 0.0f, tone_bg, 0.5f); /* dark gray */
rgb_tint(alert_color, 0.0f, 0.8f, tone_bg, 0.1f); /* red */
/* draw text */
bbox.xmin += 0.5f * pad_px; /* add padding to the text */
bbox.ymax -= 0.5f * (BLI_rcti_size_y(&bbox) - data->toth);
bbox.ymin = bbox.ymax - data->lineh;
for (i = 0; i < data->totline; i++) {
if (data->format[i].style == UI_TIP_STYLE_HEADER) {
/* draw header and active data (is done here to be able to change color) */
uiFontStyle fstyle_header = data->fstyle;
float xofs;
/* override text-style */
fstyle_header.shadow = 1;
fstyle_header.shadowcolor = rgb_to_grayscale(tip_colors[UI_TIP_LC_MAIN]);
fstyle_header.shadx = fstyle_header.shady = 0;
fstyle_header.shadowalpha = 1.0f;
UI_fontstyle_set(&fstyle_header);
glColor3fv(tip_colors[UI_TIP_LC_MAIN]);
UI_fontstyle_draw(&fstyle_header, &bbox, data->header);
xofs = BLF_width(fstyle_header.uifont_id, data->header, sizeof(data->header));
bbox.xmin += xofs;
glColor3fv(tip_colors[UI_TIP_LC_ACTIVE]);
UI_fontstyle_draw(&data->fstyle, &bbox, data->active_info);
bbox.xmin -= xofs;
}
else if (data->format[i].style == UI_TIP_STYLE_MONO) {
uiFontStyle fstyle_mono = data->fstyle;
fstyle_mono.uifont_id = blf_mono_font;
UI_fontstyle_set(&fstyle_mono);
/* XXX, needed because we dont have mono in 'U.uifonts' */
BLF_size(fstyle_mono.uifont_id, fstyle_mono.points * U.pixelsize, U.dpi);
glColor3fv(tip_colors[data->format[i].color_id]);
UI_fontstyle_draw(&fstyle_mono, &bbox, data->lines[i]);
}
else {
BLI_assert(data->format[i].style == UI_TIP_STYLE_NORMAL);
/* draw remaining data */
UI_fontstyle_set(&data->fstyle);
glColor3fv(tip_colors[data->format[i].color_id]);
UI_fontstyle_draw(&data->fstyle, &bbox, data->lines[i]);
}
if ((i + 1 != data->totline) && data->format[i + 1].is_pad) {
bbox.ymax -= data->lineh * UI_TIP_PAD_FAC;
bbox.ymin -= data->lineh * UI_TIP_PAD_FAC;
}
else {
bbox.ymax -= data->lineh;
bbox.ymin -= data->lineh;
}
}
if (multisample_enabled)
glEnable(GL_MULTISAMPLE_ARB);
}
static void ui_tooltip_region_free_cb(ARegion *ar)
{
uiTooltipData *data;
data = ar->regiondata;
MEM_freeN(data);
ar->regiondata = NULL;
}
ARegion *ui_tooltip_create(bContext *C, ARegion *butregion, uiBut *but)
{
const float pad_px = UI_TIP_PADDING;
wmWindow *win = CTX_wm_window(C);
uiStyle *style = UI_style_get();
static ARegionType type;
ARegion *ar;
uiTooltipData *data;
/* IDProperty *prop;*/
char buf[512];
/* aspect values that shrink text are likely unreadable */
const float aspect = min_ff(1.0f, but->block->aspect);
float fonth, fontw;
int winx, ofsx, ofsy, w = 0, h, i;
rctf rect_fl;
rcti rect_i;
uiStringInfo but_tip = {BUT_GET_TIP, NULL};
uiStringInfo enum_label = {BUT_GET_RNAENUM_LABEL, NULL};
uiStringInfo enum_tip = {BUT_GET_RNAENUM_TIP, NULL};
uiStringInfo op_keymap = {BUT_GET_OP_KEYMAP, NULL};
uiStringInfo prop_keymap = {BUT_GET_PROP_KEYMAP, NULL};
uiStringInfo rna_struct = {BUT_GET_RNASTRUCT_IDENTIFIER, NULL};
uiStringInfo rna_prop = {BUT_GET_RNAPROP_IDENTIFIER, NULL};
if (but->drawflag & UI_BUT_NO_TOOLTIP)
return NULL;
/* create tooltip data */
data = MEM_callocN(sizeof(uiTooltipData), "uiTooltipData");
UI_but_string_info_get(C, but, &but_tip, &enum_label, &enum_tip, &op_keymap, &prop_keymap, &rna_struct, &rna_prop, NULL);
/* Tip */
if (but_tip.strinfo) {
BLI_strncpy(data->header, but_tip.strinfo, sizeof(data->lines[0]));
if (enum_label.strinfo) {
BLI_snprintf(data->header, sizeof(data->header), "%s: ", but_tip.strinfo);
BLI_strncpy(data->active_info, enum_label.strinfo, sizeof(data->lines[0]));
}
data->format[data->totline].style = UI_TIP_STYLE_HEADER;
data->totline++;
/* special case enum rna buttons */
if ((but->type & UI_BTYPE_ROW) && but->rnaprop && RNA_property_flag(but->rnaprop) & PROP_ENUM_FLAG) {
BLI_strncpy(data->lines[data->totline], IFACE_("(Shift-Click/Drag to select multiple)"),
sizeof(data->lines[0]));
data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
data->totline++;
}
}
/* Enum item label & tip */
if (enum_tip.strinfo) {
BLI_strncpy(data->lines[data->totline], enum_tip.strinfo, sizeof(data->lines[0]));
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_VALUE;
data->totline++;
}
/* Op shortcut */
if (op_keymap.strinfo) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), op_keymap.strinfo);
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_VALUE;
data->totline++;
}
/* Property context-toggle shortcut */
if (prop_keymap.strinfo) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Shortcut: %s"), prop_keymap.strinfo);
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_VALUE;
data->totline++;
}
if (ELEM(but->type, UI_BTYPE_TEXT, UI_BTYPE_SEARCH_MENU)) {
/* better not show the value of a password */
if ((but->rnaprop && (RNA_property_subtype(but->rnaprop) == PROP_PASSWORD)) == 0) {
/* full string */
ui_but_string_get(but, buf, sizeof(buf));
if (buf[0]) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Value: %s"), buf);
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_VALUE;
data->totline++;
}
}
}
if (but->rnaprop) {
int unit_type = UI_but_unit_type_get(but);
if (unit_type == PROP_UNIT_ROTATION) {
if (RNA_property_type(but->rnaprop) == PROP_FLOAT) {
float value = RNA_property_array_check(but->rnaprop) ?
RNA_property_float_get_index(&but->rnapoin, but->rnaprop, but->rnaindex) :
RNA_property_float_get(&but->rnapoin, but->rnaprop);
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Radians: %f"), value);
data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
data->totline++;
}
}
if (but->flag & UI_BUT_DRIVEN) {
if (ui_but_anim_expression_get(but, buf, sizeof(buf))) {
/* expression */
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Expression: %s"), buf);
data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
data->totline++;
}
}
if (but->rnapoin.id.data) {
ID *id = but->rnapoin.id.data;
if (id->lib) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Library: %s"), id->lib->name);
data->format[data->totline].color_id = UI_TIP_LC_NORMAL;
data->totline++;
}
}
}
else if (but->optype) {
PointerRNA *opptr;
char *str;
opptr = UI_but_operator_ptr_get(but); /* allocated when needed, the button owns it */
/* so the context is passed to itemf functions (some py itemf functions use it) */
WM_operator_properties_sanitize(opptr, false);
str = WM_operator_pystring_ex(C, NULL, false, false, but->optype, opptr);
/* avoid overly verbose tips (eg, arrays of 20 layers), exact limit is arbitrary */
WM_operator_pystring_abbreviate(str, 32);
/* operator info */
if ((U.flag & USER_TOOLTIPS_PYTHON) == 0) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Python: %s"), str);
data->format[data->totline].style = UI_TIP_STYLE_MONO;
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
data->totline++;
}
MEM_freeN(str);
/* second check if we are disabled - why */
if (but->flag & UI_BUT_DISABLED) {
const char *poll_msg;
CTX_wm_operator_poll_msg_set(C, NULL);
WM_operator_poll_context(C, but->optype, but->opcontext);
poll_msg = CTX_wm_operator_poll_msg_get(C);
if (poll_msg) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]), TIP_("Disabled: %s"), poll_msg);
data->format[data->totline].color_id = UI_TIP_LC_ALERT;
data->totline++;
}
}
}
if ((U.flag & USER_TOOLTIPS_PYTHON) == 0 && !but->optype && rna_struct.strinfo) {
if (rna_prop.strinfo) {
/* Struct and prop */
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
TIP_("Python: %s.%s"),
rna_struct.strinfo, rna_prop.strinfo);
}
else {
/* Only struct (e.g. menus) */
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
TIP_("Python: %s"), rna_struct.strinfo);
}
data->format[data->totline].style = UI_TIP_STYLE_MONO;
data->format[data->totline].is_pad = true;
data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
data->totline++;
if (but->rnapoin.id.data) {
/* this could get its own 'BUT_GET_...' type */
PointerRNA *ptr = &but->rnapoin;
PropertyRNA *prop = but->rnaprop;
ID *id = ptr->id.data;
char *id_path;
char *data_path = NULL;
/* never fails */
id_path = RNA_path_full_ID_py(id);
if (ptr->data && prop) {
data_path = RNA_path_from_ID_to_property(ptr, prop);
}
if (data_path) {
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
"%s.%s", /* no need to translate */
id_path, data_path);
MEM_freeN(data_path);
}
else if (prop) {
/* can't find the path. be explicit in our ignorance "..." */
BLI_snprintf(data->lines[data->totline], sizeof(data->lines[0]),
"%s ... %s", /* no need to translate */
id_path, rna_prop.strinfo ? rna_prop.strinfo : RNA_property_identifier(prop));
}
MEM_freeN(id_path);
data->format[data->totline].style = UI_TIP_STYLE_MONO;
data->format[data->totline].color_id = UI_TIP_LC_PYTHON;
data->totline++;
}
}
/* Free strinfo's... */
if (but_tip.strinfo)
MEM_freeN(but_tip.strinfo);
if (enum_label.strinfo)
MEM_freeN(enum_label.strinfo);
if (enum_tip.strinfo)
MEM_freeN(enum_tip.strinfo);
if (op_keymap.strinfo)
MEM_freeN(op_keymap.strinfo);
if (prop_keymap.strinfo)
MEM_freeN(prop_keymap.strinfo);
if (rna_struct.strinfo)
MEM_freeN(rna_struct.strinfo);
if (rna_prop.strinfo)
MEM_freeN(rna_prop.strinfo);
BLI_assert(data->totline < MAX_TOOLTIP_LINES);
if (data->totline == 0) {
MEM_freeN(data);
return NULL;
}
/* create area region */
ar = ui_region_temp_add(CTX_wm_screen(C));
memset(&type, 0, sizeof(ARegionType));
type.draw = ui_tooltip_region_draw_cb;
type.free = ui_tooltip_region_free_cb;
type.regionid = RGN_TYPE_TEMPORARY;
ar->type = &type;
/* set font, get bb */
data->fstyle = style->widget; /* copy struct */
ui_fontscale(&data->fstyle.points, aspect);
UI_fontstyle_set(&data->fstyle);
/* these defines tweaked depending on font */
#define TIP_BORDER_X (16.0f / aspect)
#define TIP_BORDER_Y (6.0f / aspect)
h = BLF_height_max(data->fstyle.uifont_id);
for (i = 0, fontw = 0, fonth = 0; i < data->totline; i++) {
if (data->format[i].style == UI_TIP_STYLE_HEADER) {
w = BLF_width(data->fstyle.uifont_id, data->header, sizeof(data->header));
if (enum_label.strinfo)
w += BLF_width(data->fstyle.uifont_id, data->active_info, sizeof(data->active_info));
}
else if (data->format[i].style == UI_TIP_STYLE_MONO) {
BLF_size(blf_mono_font, data->fstyle.points * U.pixelsize, U.dpi);
w = BLF_width(blf_mono_font, data->lines[i], sizeof(data->lines[i]));
}
else {
BLI_assert(data->format[i].style == UI_TIP_STYLE_NORMAL);
w = BLF_width(data->fstyle.uifont_id, data->lines[i], sizeof(data->lines[i]));
}
fontw = max_ff(fontw, (float)w);
if ((i + 1 != data->totline) && data->format[i + 1].is_pad) {
fonth += h * UI_TIP_PAD_FAC;
}
else {
fonth += h;
}
}
//fontw *= aspect;
ar->regiondata = data;
data->toth = fonth;
data->lineh = h;
/* compute position */
ofsx = 0; //(but->block->panel) ? but->block->panel->ofsx : 0;
ofsy = 0; //(but->block->panel) ? but->block->panel->ofsy : 0;
rect_fl.xmin = BLI_rctf_cent_x(&but->rect) + ofsx - TIP_BORDER_X;
rect_fl.xmax = rect_fl.xmin + fontw + pad_px;
rect_fl.ymax = but->rect.ymin + ofsy - TIP_BORDER_Y;
rect_fl.ymin = rect_fl.ymax - fonth - TIP_BORDER_Y;
/* since the text has beens caled already, the size of tooltips is defined now */
/* here we try to figure out the right location */
if (butregion) {
float mx, my;
float ofsx_fl = rect_fl.xmin, ofsy_fl = rect_fl.ymax;
ui_block_to_window_fl(butregion, but->block, &ofsx_fl, &ofsy_fl);
#if 1
/* use X mouse location */
mx = (win->eventstate->x + (TIP_BORDER_X * 2)) - BLI_rctf_cent_x(&but->rect);
#else
mx = ofsx_fl - rect_fl.xmin;
#endif
my = ofsy_fl - rect_fl.ymax;
BLI_rctf_translate(&rect_fl, mx, my);
}
BLI_rcti_rctf_copy(&rect_i, &rect_fl);
#undef TIP_BORDER_X
#undef TIP_BORDER_Y
/* clip with window boundaries */
winx = WM_window_pixels_x(win);
if (rect_i.xmax > winx) {
/* super size */
if (rect_i.xmax > winx + rect_i.xmin) {
rect_i.xmax = winx;
rect_i.xmin = 0;
}
else {
rect_i.xmin -= rect_i.xmax - winx;
rect_i.xmax = winx;
}
}
/* ensure at least 5 px above screen bounds
* 25 is just a guess to be above the menu item */
if (rect_i.ymin < 5) {
rect_i.ymax += (-rect_i.ymin) + 30;
rect_i.ymin = 30;
}
/* add padding */
BLI_rcti_resize(&rect_i,
BLI_rcti_size_x(&rect_i) + pad_px,
BLI_rcti_size_y(&rect_i) + pad_px);
/* widget rect, in region coords */
{
const int margin = UI_POPUP_MARGIN;
data->bbox.xmin = margin;
data->bbox.xmax = BLI_rcti_size_x(&rect_i) - margin;
data->bbox.ymin = margin;
data->bbox.ymax = BLI_rcti_size_y(&rect_i);
/* region bigger for shadow */
ar->winrct.xmin = rect_i.xmin - margin;
ar->winrct.xmax = rect_i.xmax + margin;
ar->winrct.ymin = rect_i.ymin - margin;
ar->winrct.ymax = rect_i.ymax + margin;
}
/* adds subwindow */
ED_region_init(C, ar);
/* notify change and redraw */
ED_region_tag_redraw(ar);
return ar;
}
void ui_tooltip_free(bContext *C, ARegion *ar)
{
ui_region_temp_remove(C, CTX_wm_screen(C), ar);
}
/************************* Creating Search Box **********************/
struct uiSearchItems {
int maxitem, totitem, maxstrlen;
int offset, offset_i; /* offset for inserting in array */
int more; /* flag indicating there are more items */
char **names;
void **pointers;
int *icons;
AutoComplete *autocpl;
void *active;
};
typedef struct uiSearchboxData {
rcti bbox;
uiFontStyle fstyle;
uiSearchItems items;
int active; /* index in items array */
bool noback; /* when menu opened with enough space for this */
bool preview; /* draw thumbnail previews, rather than list */
bool use_sep; /* use the UI_SEP_CHAR char for splitting shortcuts (good for operators, bad for data) */
int prv_rows, prv_cols;
} uiSearchboxData;
#define SEARCH_ITEMS 10
/* exported for use by search callbacks */
/* returns zero if nothing to add */
bool UI_search_item_add(uiSearchItems *items, const char *name, void *poin, int iconid)
{
/* hijack for autocomplete */
if (items->autocpl) {
UI_autocomplete_update_name(items->autocpl, name);
return true;
}
/* hijack for finding active item */
if (items->active) {
if (poin == items->active)
items->offset_i = items->totitem;
items->totitem++;
return true;
}
if (items->totitem >= items->maxitem) {
items->more = 1;
return false;
}
/* skip first items in list */
if (items->offset_i > 0) {
items->offset_i--;
return true;
}
if (items->names)
BLI_strncpy(items->names[items->totitem], name, items->maxstrlen);
if (items->pointers)
items->pointers[items->totitem] = poin;
if (items->icons)
items->icons[items->totitem] = iconid;
items->totitem++;
return true;
}
int UI_searchbox_size_y(void)
{
return SEARCH_ITEMS * UI_UNIT_Y + 2 * MENU_TOP;
}
int UI_searchbox_size_x(void)
{
return 12 * UI_UNIT_X;
}
int UI_search_items_find_index(uiSearchItems *items, const char *name)
{
int i;
for (i = 0; i < items->totitem; i++) {
if (STREQ(name, items->names[i])) {
return i;
}
}
return -1;
}
/* ar is the search box itself */
static void ui_searchbox_select(bContext *C, ARegion *ar, uiBut *but, int step)
{
uiSearchboxData *data = ar->regiondata;
/* apply step */
data->active += step;
if (data->items.totitem == 0) {
data->active = -1;
}
else if (data->active >= data->items.totitem) {
if (data->items.more) {
data->items.offset++;
data->active = data->items.totitem - 1;
ui_searchbox_update(C, ar, but, false);
}
else {
data->active = data->items.totitem - 1;
}
}
else if (data->active < 0) {
if (data->items.offset) {
data->items.offset--;
data->active = 0;
ui_searchbox_update(C, ar, but, false);
}
else {
/* only let users step into an 'unset' state for unlink buttons */
data->active = (but->flag & UI_BUT_SEARCH_UNLINK) ? -1 : 0;
}
}
ED_region_tag_redraw(ar);
}
static void ui_searchbox_butrect(rcti *r_rect, uiSearchboxData *data, int itemnr)
{
/* thumbnail preview */
if (data->preview) {
int butw = (BLI_rcti_size_x(&data->bbox) - 2 * MENU_BORDER) / data->prv_cols;
int buth = (BLI_rcti_size_y(&data->bbox) - 2 * MENU_BORDER) / data->prv_rows;
int row, col;
*r_rect = data->bbox;
col = itemnr % data->prv_cols;
row = itemnr / data->prv_cols;
r_rect->xmin += MENU_BORDER + (col * butw);
r_rect->xmax = r_rect->xmin + butw;
r_rect->ymax -= MENU_BORDER + (row * buth);
r_rect->ymin = r_rect->ymax - buth;
}
/* list view */
else {
int buth = (BLI_rcti_size_y(&data->bbox) - 2 * MENU_TOP) / SEARCH_ITEMS;
*r_rect = data->bbox;
r_rect->xmin = data->bbox.xmin + 3.0f;
r_rect->xmax = data->bbox.xmax - 3.0f;
r_rect->ymax = data->bbox.ymax - MENU_TOP - itemnr * buth;
r_rect->ymin = r_rect->ymax - buth;
}
}
int ui_searchbox_find_index(ARegion *ar, const char *name)
{
uiSearchboxData *data = ar->regiondata;
return UI_search_items_find_index(&data->items, name);
}
/* x and y in screencoords */
bool ui_searchbox_inside(ARegion *ar, int x, int y)
{
uiSearchboxData *data = ar->regiondata;
return BLI_rcti_isect_pt(&data->bbox, x - ar->winrct.xmin, y - ar->winrct.ymin);
}
/* string validated to be of correct length (but->hardmax) */
bool ui_searchbox_apply(uiBut *but, ARegion *ar)
{
uiSearchboxData *data = ar->regiondata;
but->func_arg2 = NULL;
if (data->active != -1) {
const char *name = data->items.names[data->active];
const char *name_sep = data->use_sep ? strrchr(name, UI_SEP_CHAR) : NULL;
BLI_strncpy(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen);
but->func_arg2 = data->items.pointers[data->active];
return true;
}
else if (but->flag & UI_BUT_SEARCH_UNLINK) {
/* It is valid for _UNLINK flavor to have no active element (it's a valid way to unlink). */
but->editstr[0] = '\0';
return true;
}
else {
return false;
}
}
void ui_searchbox_event(bContext *C, ARegion *ar, uiBut *but, const wmEvent *event)
{
uiSearchboxData *data = ar->regiondata;
int type = event->type, val = event->val;
if (type == MOUSEPAN)
ui_pan_to_scroll(event, &type, &val);
switch (type) {
case WHEELUPMOUSE:
case UPARROWKEY:
ui_searchbox_select(C, ar, but, -1);
break;
case WHEELDOWNMOUSE:
case DOWNARROWKEY:
ui_searchbox_select(C, ar, but, 1);
break;
case MOUSEMOVE:
if (BLI_rcti_isect_pt(&ar->winrct, event->x, event->y)) {
rcti rect;
int a;
for (a = 0; a < data->items.totitem; a++) {
ui_searchbox_butrect(&rect, data, a);
if (BLI_rcti_isect_pt(&rect, event->x - ar->winrct.xmin, event->y - ar->winrct.ymin)) {
if (data->active != a) {
data->active = a;
ui_searchbox_select(C, ar, but, 0);
break;
}
}
}
}
break;
}
}
/* ar is the search box itself */
void ui_searchbox_update(bContext *C, ARegion *ar, uiBut *but, const bool reset)
{
uiSearchboxData *data = ar->regiondata;
/* reset vars */
data->items.totitem = 0;
data->items.more = 0;
if (reset == false) {
data->items.offset_i = data->items.offset;
}
else {
data->items.offset_i = data->items.offset = 0;
data->active = -1;
/* handle active */
if (but->search_func && but->func_arg2) {
data->items.active = but->func_arg2;
but->search_func(C, but->search_arg, but->editstr, &data->items);
data->items.active = NULL;
/* found active item, calculate real offset by centering it */
if (data->items.totitem) {
/* first case, begin of list */
if (data->items.offset_i < data->items.maxitem) {
data->active = data->items.offset_i;
data->items.offset_i = 0;
}
else {
/* second case, end of list */
if (data->items.totitem - data->items.offset_i <= data->items.maxitem) {
data->active = data->items.offset_i - data->items.totitem + data->items.maxitem;
data->items.offset_i = data->items.totitem - data->items.maxitem;
}
else {
/* center active item */
data->items.offset_i -= data->items.maxitem / 2;
data->active = data->items.maxitem / 2;
}
}
}
data->items.offset = data->items.offset_i;
data->items.totitem = 0;
}
}
/* callback */
if (but->search_func)
but->search_func(C, but->search_arg, but->editstr, &data->items);
/* handle case where editstr is equal to one of items */
if (reset && data->active == -1) {
int a;
for (a = 0; a < data->items.totitem; a++) {
const char *name = data->items.names[a];
const char *name_sep = data->use_sep ? strrchr(name, UI_SEP_CHAR) : NULL;
if (STREQLEN(but->editstr, name, name_sep ? (name_sep - name) : data->items.maxstrlen)) {
data->active = a;
break;
}
}
if (data->items.totitem == 1 && but->editstr[0])
data->active = 0;
}
/* validate selected item */
ui_searchbox_select(C, ar, but, 0);
ED_region_tag_redraw(ar);
}
int ui_searchbox_autocomplete(bContext *C, ARegion *ar, uiBut *but, char *str)
{
uiSearchboxData *data = ar->regiondata;
int match = AUTOCOMPLETE_NO_MATCH;
if (str[0]) {
data->items.autocpl = UI_autocomplete_begin(str, ui_but_string_get_max_length(but));
but->search_func(C, but->search_arg, but->editstr, &data->items);
match = UI_autocomplete_end(data->items.autocpl, str);
data->items.autocpl = NULL;
}
return match;
}
static void ui_searchbox_region_draw_cb(const bContext *UNUSED(C), ARegion *ar)
{
uiSearchboxData *data = ar->regiondata;
/* pixel space */
wmOrtho2_region_ui(ar);
if (data->noback == false)
ui_draw_search_back(NULL, NULL, &data->bbox); /* style not used yet */
/* draw text */
if (data->items.totitem) {
rcti rect;
int a;
if (data->preview) {
/* draw items */
for (a = 0; a < data->items.totitem; a++) {
ui_searchbox_butrect(&rect, data, a);
/* widget itself */
ui_draw_preview_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
(a == data->active) ? UI_ACTIVE : 0);
}
/* indicate more */
if (data->items.more) {
ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
glEnable(GL_BLEND);
UI_icon_draw(rect.xmax - 18, rect.ymin - 7, ICON_TRIA_DOWN);
glDisable(GL_BLEND);
}
if (data->items.offset) {
ui_searchbox_butrect(&rect, data, 0);
glEnable(GL_BLEND);
UI_icon_draw(rect.xmin, rect.ymax - 9, ICON_TRIA_UP);
glDisable(GL_BLEND);
}
}
else {
/* draw items */
for (a = 0; a < data->items.totitem; a++) {
ui_searchbox_butrect(&rect, data, a);
/* widget itself */
ui_draw_menu_item(&data->fstyle, &rect, data->items.names[a], data->items.icons[a],
(a == data->active) ? UI_ACTIVE : 0, data->use_sep);
}
/* indicate more */
if (data->items.more) {
ui_searchbox_butrect(&rect, data, data->items.maxitem - 1);
glEnable(GL_BLEND);
UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymin - 9, ICON_TRIA_DOWN);
glDisable(GL_BLEND);
}
if (data->items.offset) {
ui_searchbox_butrect(&rect, data, 0);
glEnable(GL_BLEND);
UI_icon_draw((BLI_rcti_size_x(&rect)) / 2, rect.ymax - 7, ICON_TRIA_UP);
glDisable(GL_BLEND);
}
}
}
}
static void ui_searchbox_region_free_cb(ARegion *ar)
{
uiSearchboxData *data = ar->regiondata;
int a;
/* free search data */
for (a = 0; a < data->items.maxitem; a++) {
MEM_freeN(data->items.names[a]);
}
MEM_freeN(data->items.names);
MEM_freeN(data->items.pointers);
MEM_freeN(data->items.icons);
MEM_freeN(data);
ar->regiondata = NULL;
}
ARegion *ui_searchbox_create(bContext *C, ARegion *butregion, uiBut *but)
{
wmWindow *win = CTX_wm_window(C);
uiStyle *style = UI_style_get();
static ARegionType type;
ARegion *ar;
uiSearchboxData *data;
float aspect = but->block->aspect;
rctf rect_fl;
rcti rect_i;
int winx /*, winy */, ofsx, ofsy;
int i;
/* create area region */
ar = ui_region_temp_add(CTX_wm_screen(C));
memset(&type, 0, sizeof(ARegionType));
type.draw = ui_searchbox_region_draw_cb;
type.free = ui_searchbox_region_free_cb;
type.regionid = RGN_TYPE_TEMPORARY;
ar->type = &type;
/* create searchbox data */
data = MEM_callocN(sizeof(uiSearchboxData), "uiSearchboxData");
/* set font, get bb */
data->fstyle = style->widget; /* copy struct */
data->fstyle.align = UI_STYLE_TEXT_CENTER;
ui_fontscale(&data->fstyle.points, aspect);
UI_fontstyle_set(&data->fstyle);
ar->regiondata = data;
/* special case, hardcoded feature, not draw backdrop when called from menus,
* assume for design that popup already added it */
if (but->block->flag & UI_BLOCK_SEARCH_MENU)
data->noback = true;
if (but->a1 > 0 && but->a2 > 0) {
data->preview = true;
data->prv_rows = but->a1;
data->prv_cols = but->a2;
}
/* only show key shortcuts when needed (not rna buttons) [#36699] */
if (but->rnaprop == NULL) {
data->use_sep = true;
}
/* compute position */
if (but->block->flag & UI_BLOCK_SEARCH_MENU) {
const int margin_x = UI_POPUP_MARGIN;
const int margin_y = MENU_TOP;
const int search_but_h = BLI_rctf_size_y(&but->rect) + 10;
/* this case is search menu inside other menu */
/* we copy region size */
ar->winrct = butregion->winrct;
/* widget rect, in region coords */
data->bbox.xmin = margin_x;
data->bbox.xmax = BLI_rcti_size_x(&ar->winrct) - margin_x;
/* Do not use shadow width for height, gives insane margin with big shadows, and issue T41548 with small ones */
data->bbox.ymin = margin_y;
data->bbox.ymax = BLI_rcti_size_y(&ar->winrct) - margin_y;
/* check if button is lower half */
if (but->rect.ymax < BLI_rctf_cent_y(&but->block->rect)) {
data->bbox.ymin += search_but_h;
}
else {
data->bbox.ymax -= search_but_h;
}
}
else {
const int searchbox_width = UI_searchbox_size_x();
const int margin = UI_POPUP_MARGIN;
rect_fl.xmin = but->rect.xmin - 5; /* align text with button */
rect_fl.xmax = but->rect.xmax + 5; /* symmetrical */
rect_fl.ymax = but->rect.ymin;
rect_fl.ymin = rect_fl.ymax - UI_searchbox_size_y();
ofsx = (but->block->panel) ? but->block->panel->ofsx : 0;
ofsy = (but->block->panel) ? but->block->panel->ofsy : 0;
BLI_rctf_translate(&rect_fl, ofsx, ofsy);
/* minimal width */
if (BLI_rctf_size_x(&rect_fl) < searchbox_width) {
rect_fl.xmax = rect_fl.xmin + searchbox_width;
}
/* copy to int, gets projected if possible too */
BLI_rcti_rctf_copy(&rect_i, &rect_fl);
if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax) {
UI_view2d_view_to_region_rcti(&butregion->v2d, &rect_fl, &rect_i);
}
BLI_rcti_translate(&rect_i, butregion->winrct.xmin, butregion->winrct.ymin);
winx = WM_window_pixels_x(win);
// winy = WM_window_pixels_y(win); /* UNUSED */
//wm_window_get_size(win, &winx, &winy);
if (rect_i.xmax > winx) {
/* super size */
if (rect_i.xmax > winx + rect_i.xmin) {
rect_i.xmax = winx;
rect_i.xmin = 0;
}
else {
rect_i.xmin -= rect_i.xmax - winx;
rect_i.xmax = winx;
}
}
if (rect_i.ymin < 0) {
int newy1 = but->rect.ymax + ofsy;
if (butregion->v2d.cur.xmin != butregion->v2d.cur.xmax)
newy1 = UI_view2d_view_to_region_y(&butregion->v2d, newy1);
newy1 += butregion->winrct.ymin;
rect_i.ymax = BLI_rcti_size_y(&rect_i) + newy1;
rect_i.ymin = newy1;
}
/* widget rect, in region coords */
data->bbox.xmin = margin;
data->bbox.xmax = BLI_rcti_size_x(&rect_i) + margin;
data->bbox.ymin = margin;
data->bbox.ymax = BLI_rcti_size_y(&rect_i) + margin;
/* region bigger for shadow */
ar->winrct.xmin = rect_i.xmin - margin;
ar->winrct.xmax = rect_i.xmax + margin;
ar->winrct.ymin = rect_i.ymin - margin;
ar->winrct.ymax = rect_i.ymax;
}
/* adds subwindow */
ED_region_init(C, ar);
/* notify change and redraw */
ED_region_tag_redraw(ar);
/* prepare search data */
if (data->preview) {
data->items.maxitem = data->prv_rows * data->prv_cols;
}
else {
data->items.maxitem = SEARCH_ITEMS;
}
data->items.maxstrlen = but->hardmax;
data->items.totitem = 0;
data->items.names = MEM_callocN(data->items.maxitem * sizeof(void *), "search names");
data->items.pointers = MEM_callocN(data->items.maxitem * sizeof(void *), "search pointers");
data->items.icons = MEM_callocN(data->items.maxitem * sizeof(int), "search icons");
for (i = 0; i < data->items.maxitem; i++)
data->items.names[i] = MEM_callocN(but->hardmax + 1, "search pointers");
return ar;
}
void ui_searchbox_free(bContext *C, ARegion *ar)
{
ui_region_temp_remove(C, CTX_wm_screen(C), ar);
}
/* sets red alert if button holds a string it can't find */
/* XXX weak: search_func adds all partial matches... */
void ui_but_search_refresh(uiBut *but)
{
uiSearchItems *items;
int x1;
/* possibly very large lists (such as ID datablocks) only
* only validate string RNA buts (not pointers) */
if (but->rnaprop && RNA_property_type(but->rnaprop) != PROP_STRING) {
return;
}
items = MEM_callocN(sizeof(uiSearchItems), "search items");
/* setup search struct */
items->maxitem = 10;
items->maxstrlen = 256;
items->names = MEM_callocN(items->maxitem * sizeof(void *), "search names");
for (x1 = 0; x1 < items->maxitem; x1++)
items->names[x1] = MEM_callocN(but->hardmax + 1, "search names");
but->search_func(but->block->evil_C, but->search_arg, but->drawstr, items);
/* only redalert when we are sure of it, this can miss cases when >10 matches */
if (items->totitem == 0) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
else if (items->more == 0) {
if (UI_search_items_find_index(items, but->drawstr) == -1) {
UI_but_flag_enable(but, UI_BUT_REDALERT);
}
}
for (x1 = 0; x1 < items->maxitem; x1++) {
MEM_freeN(items->names[x1]);
}
MEM_freeN(items->names);
MEM_freeN(items);
}
/************************* Creating Menu Blocks **********************/
/* position block relative to but, result is in window space */
static void ui_block_position(wmWindow *window, ARegion *butregion, uiBut *but, uiBlock *block)
{
uiBut *bt;
uiSafetyRct *saferct;
rctf butrct;
/*float aspect;*/ /*UNUSED*/
int xsize, ysize, xof = 0, yof = 0, center;
short dir1 = 0, dir2 = 0;
/* transform to window coordinates, using the source button region/block */
ui_block_to_window_rctf(butregion, but->block, &butrct, &but->rect);
/* widget_roundbox_set has this correction too, keep in sync */
if (but->type != UI_BTYPE_PULLDOWN) {
if (but->drawflag & UI_BUT_ALIGN_TOP)
butrct.ymax += U.pixelsize;
if (but->drawflag & UI_BUT_ALIGN_LEFT)
butrct.xmin -= U.pixelsize;
}
/* calc block rect */
if (block->rect.xmin == 0.0f && block->rect.xmax == 0.0f) {
if (block->buttons.first) {
BLI_rctf_init_minmax(&block->rect);
for (bt = block->buttons.first; bt; bt = bt->next) {
BLI_rctf_union(&block->rect, &bt->rect);
if (UNLIKELY(block->flag & UI_BLOCK_HAS_SUBMENU)) {
bt->rect.xmax += UI_MENU_SUBMENU_PADDING;
}
}
}
else {
/* we're nice and allow empty blocks too */
block->rect.xmin = block->rect.ymin = 0;
block->rect.xmax = block->rect.ymax = 20;
}
if (block->flag & UI_BLOCK_HAS_SUBMENU) {
block->rect.xmax += UI_MENU_SUBMENU_PADDING;
}
}
/* aspect = (float)(BLI_rcti_size_x(&block->rect) + 4);*/ /*UNUSED*/
ui_block_to_window_rctf(butregion, but->block, &block->rect, &block->rect);
//block->rect.xmin -= 2.0; block->rect.ymin -= 2.0;
//block->rect.xmax += 2.0; block->rect.ymax += 2.0;
xsize = BLI_rctf_size_x(&block->rect) + 0.2f * UI_UNIT_X; /* 4 for shadow */
ysize = BLI_rctf_size_y(&block->rect) + 0.2f * UI_UNIT_Y;
/* aspect /= (float)xsize;*/ /*UNUSED*/
{
bool left = 0, right = 0, top = 0, down = 0;
int winx, winy;
// int offscreen;
winx = WM_window_pixels_x(window);
winy = WM_window_pixels_y(window);
// wm_window_get_size(window, &winx, &winy);
if (block->direction & UI_DIR_CENTER_Y) {
center = ysize / 2;
}
else {
center = 0;
}
/* check if there's space at all */
if (butrct.xmin - xsize > 0.0f) left = 1;
if (butrct.xmax + xsize < winx) right = 1;
if (butrct.ymin - ysize + center > 0.0f) down = 1;
if (butrct.ymax + ysize - center < winy) top = 1;
if (top == 0 && down == 0) {
if (butrct.ymin - ysize < winy - butrct.ymax - ysize)
top = 1;
else
down = 1;
}
dir1 = (block->direction & UI_DIR_ALL);
/* secundary directions */
if (dir1 & (UI_DIR_UP | UI_DIR_DOWN)) {
if (dir1 & UI_DIR_LEFT) dir2 = UI_DIR_LEFT;
else if (dir1 & UI_DIR_RIGHT) dir2 = UI_DIR_RIGHT;
dir1 &= (UI_DIR_UP | UI_DIR_DOWN);
}
if ((dir2 == 0) && (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT)) dir2 = UI_DIR_DOWN;
if ((dir2 == 0) && (dir1 == UI_DIR_UP || dir1 == UI_DIR_DOWN)) dir2 = UI_DIR_LEFT;
/* no space at all? don't change */
if (left || right) {
if (dir1 == UI_DIR_LEFT && left == 0) dir1 = UI_DIR_RIGHT;
if (dir1 == UI_DIR_RIGHT && right == 0) dir1 = UI_DIR_LEFT;
/* this is aligning, not append! */
if (dir2 == UI_DIR_LEFT && right == 0) dir2 = UI_DIR_RIGHT;
if (dir2 == UI_DIR_RIGHT && left == 0) dir2 = UI_DIR_LEFT;
}
if (down || top) {
if (dir1 == UI_DIR_UP && top == 0) dir1 = UI_DIR_DOWN;
if (dir1 == UI_DIR_DOWN && down == 0) dir1 = UI_DIR_UP;
BLI_assert(dir2 != UI_DIR_UP);
// if (dir2 == UI_DIR_UP && top == 0) dir2 = UI_DIR_DOWN;
if (dir2 == UI_DIR_DOWN && down == 0) dir2 = UI_DIR_UP;
}
#define RECTS_BUT_BLOCK_DIFF(member) (butrct.member - block->rect.member)
switch (dir1) {
case UI_DIR_LEFT:
xof = butrct.xmin - block->rect.xmax;
if (dir2 == UI_DIR_UP) yof = RECTS_BUT_BLOCK_DIFF(ymin) - center - MENU_PADDING;
else yof = RECTS_BUT_BLOCK_DIFF(ymax) + center + MENU_PADDING;
break;
case UI_DIR_RIGHT:
xof = butrct.xmax - block->rect.xmin;
if (dir2 == UI_DIR_UP) yof = RECTS_BUT_BLOCK_DIFF(ymin) - center - MENU_PADDING;
else yof = RECTS_BUT_BLOCK_DIFF(ymax) + center + MENU_PADDING;
break;
case UI_DIR_UP:
yof = iroundf(butrct.ymax - block->rect.ymin - (2.0f * U.pixelsize));
if (dir2 == UI_DIR_RIGHT) xof = RECTS_BUT_BLOCK_DIFF(xmax);
else xof = RECTS_BUT_BLOCK_DIFF(xmin);
/* changed direction? */
if ((dir1 & block->direction) == 0) {
UI_block_order_flip(block);
}
break;
case UI_DIR_DOWN:
yof = iroundf(butrct.ymin - block->rect.ymax + (2.0f * U.pixelsize));
if (dir2 == UI_DIR_RIGHT) xof = RECTS_BUT_BLOCK_DIFF(xmax);
else xof = RECTS_BUT_BLOCK_DIFF(xmin);
/* changed direction? */
if ((dir1 & block->direction) == 0) {
UI_block_order_flip(block);
}
break;
}
#undef RECTS_BUT_BLOCK_DIFF
/* and now we handle the exception; no space below or to top */
if (top == 0 && down == 0) {
if (dir1 == UI_DIR_LEFT || dir1 == UI_DIR_RIGHT) {
/* align with bottom of screen */
// yof = ysize; (not with menu scrolls)
}
}
/* or no space left or right */
if (left == 0 && right == 0) {
if (dir1 == UI_DIR_UP || dir1 == UI_DIR_DOWN) {
/* align with left size of screen */
xof = -block->rect.xmin + 5;
}
}
#if 0
/* clamp to window bounds, could be made into an option if its ever annoying */
if ( (offscreen = (block->rect.ymin + yof)) < 0) yof -= offscreen; /* bottom */
else if ((offscreen = (block->rect.ymax + yof) - winy) > 0) yof -= offscreen; /* top */
if ( (offscreen = (block->rect.xmin + xof)) < 0) xof -= offscreen; /* left */
else if ((offscreen = (block->rect.xmax + xof) - winx) > 0) xof -= offscreen; /* right */
#endif
}
/* apply offset, buttons in window coords */
for (bt = block->buttons.first; bt; bt = bt->next) {
ui_block_to_window_rctf(butregion, but->block, &bt->rect, &bt->rect);
BLI_rctf_translate(&bt->rect, xof, yof);
/* ui_but_update recalculates drawstring size in pixels */
ui_but_update(bt);
}
BLI_rctf_translate(&block->rect, xof, yof);
/* safety calculus */
{
const float midx = BLI_rctf_cent_x(&butrct);
const float midy = BLI_rctf_cent_y(&butrct);
/* when you are outside parent button, safety there should be smaller */
/* parent button to left */
if (midx < block->rect.xmin) block->safety.xmin = block->rect.xmin - 3;
else block->safety.xmin = block->rect.xmin - 40;
/* parent button to right */
if (midx > block->rect.xmax) block->safety.xmax = block->rect.xmax + 3;
else block->safety.xmax = block->rect.xmax + 40;
/* parent button on bottom */
if (midy < block->rect.ymin) block->safety.ymin = block->rect.ymin - 3;
else block->safety.ymin = block->rect.ymin - 40;
/* parent button on top */
if (midy > block->rect.ymax) block->safety.ymax = block->rect.ymax + 3;
else block->safety.ymax = block->rect.ymax + 40;
/* exception for switched pulldowns... */
if (dir1 && (dir1 & block->direction) == 0) {
if (dir2 == UI_DIR_RIGHT) block->safety.xmax = block->rect.xmax + 3;
if (dir2 == UI_DIR_LEFT) block->safety.xmin = block->rect.xmin - 3;
}
block->direction = dir1;
}
/* keep a list of these, needed for pulldown menus */
saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
saferct->parent = butrct;
saferct->safety = block->safety;
BLI_freelistN(&block->saferct);
BLI_duplicatelist(&block->saferct, &but->block->saferct);
BLI_addhead(&block->saferct, saferct);
}
static void ui_block_region_draw(const bContext *C, ARegion *ar)
{
uiBlock *block;
if (ar->do_draw & RGN_DRAW_REFRESH_UI) {
uiBlock *block_next;
ar->do_draw &= ~RGN_DRAW_REFRESH_UI;
for (block = ar->uiblocks.first; block; block = block_next) {
block_next = block->next;
ui_popup_block_refresh((bContext *)C, block->handle, NULL, NULL);
}
}
for (block = ar->uiblocks.first; block; block = block->next)
UI_block_draw(C, block);
}
static void ui_popup_block_clip(wmWindow *window, uiBlock *block)
{
uiBut *bt;
float xofs = 0.0f;
int width = UI_SCREEN_MARGIN;
int winx, winy;
if (block->flag & UI_BLOCK_NO_WIN_CLIP) {
return;
}
winx = WM_window_pixels_x(window);
winy = WM_window_pixels_y(window);
/* shift menus to right if outside of view */
if (block->rect.xmin < width) {
xofs = (width - block->rect.xmin);
block->rect.xmin += xofs;
block->rect.xmax += xofs;
}
/* or shift to left if outside of view */
if (block->rect.xmax > winx - width) {
xofs = winx - width - block->rect.xmax;
block->rect.xmin += xofs;
block->rect.xmax += xofs;
}
if (block->rect.ymin < width)
block->rect.ymin = width;
if (block->rect.ymax > winy - MENU_TOP)
block->rect.ymax = winy - MENU_TOP;
/* ensure menu items draw inside left/right boundary */
for (bt = block->buttons.first; bt; bt = bt->next) {
bt->rect.xmin += xofs;
bt->rect.xmax += xofs;
}
}
void ui_popup_block_scrolltest(uiBlock *block)
{
uiBut *bt;
block->flag &= ~(UI_BLOCK_CLIPBOTTOM | UI_BLOCK_CLIPTOP);
for (bt = block->buttons.first; bt; bt = bt->next)
bt->flag &= ~UI_SCROLLED;
if (block->buttons.first == block->buttons.last)
return;
/* mark buttons that are outside boundary */
for (bt = block->buttons.first; bt; bt = bt->next) {
if (bt->rect.ymin < block->rect.ymin) {
bt->flag |= UI_SCROLLED;
block->flag |= UI_BLOCK_CLIPBOTTOM;
}
if (bt->rect.ymax > block->rect.ymax) {
bt->flag |= UI_SCROLLED;
block->flag |= UI_BLOCK_CLIPTOP;
}
}
/* mark buttons overlapping arrows, if we have them */
for (bt = block->buttons.first; bt; bt = bt->next) {
if (block->flag & UI_BLOCK_CLIPBOTTOM) {
if (bt->rect.ymin < block->rect.ymin + UI_MENU_SCROLL_ARROW)
bt->flag |= UI_SCROLLED;
}
if (block->flag & UI_BLOCK_CLIPTOP) {
if (bt->rect.ymax > block->rect.ymax - UI_MENU_SCROLL_ARROW)
bt->flag |= UI_SCROLLED;
}
}
}
static void ui_popup_block_remove(bContext *C, uiPopupBlockHandle *handle)
{
ui_region_temp_remove(C, CTX_wm_screen(C), handle->region);
if (handle->scrolltimer)
WM_event_remove_timer(CTX_wm_manager(C), CTX_wm_window(C), handle->scrolltimer);
}
/**
* Called for creatign new popups and refreshing existing ones.
*/
uiBlock *ui_popup_block_refresh(
bContext *C, uiPopupBlockHandle *handle,
ARegion *butregion, uiBut *but)
{
const int margin = UI_POPUP_MARGIN;
wmWindow *window = CTX_wm_window(C);
ARegion *ar = handle->region;
uiBlockCreateFunc create_func = handle->popup_create_vars.create_func;
uiBlockHandleCreateFunc handle_create_func = handle->popup_create_vars.handle_create_func;
void *arg = handle->popup_create_vars.arg;
uiBlock *block_old = ar->uiblocks.first;
uiBlock *block;
#ifdef DEBUG
wmEvent *event_back = window->eventstate;
#endif
/* create ui block */
if (create_func)
block = create_func(C, ar, arg);
else
block = handle_create_func(C, handle, arg);
/* callbacks _must_ leave this for us, otherwise we can't call UI_block_update_from_old */
BLI_assert(!block->endblock);
/* ensure we don't use mouse coords here! */
#ifdef DEBUG
window->eventstate = NULL;
#endif
if (block->handle) {
memcpy(block->handle, handle, sizeof(uiPopupBlockHandle));
MEM_freeN(handle);
handle = block->handle;
}
else
block->handle = handle;
ar->regiondata = handle;
/* set UI_BLOCK_NUMSELECT before UI_block_end() so we get alphanumeric keys assigned */
if (but == NULL) {
block->flag |= UI_BLOCK_POPUP;
}
block->flag |= UI_BLOCK_LOOP;
/* defer this until blocks are translated (below) */
block->oldblock = NULL;
if (!block->endblock)
UI_block_end_ex(C, block, handle->popup_create_vars.event_xy);
/* if this is being created from a button */
if (but) {
block->aspect = but->block->aspect;
ui_block_position(window, butregion, but, block);
handle->direction = block->direction;
}
else {
uiSafetyRct *saferct;
/* keep a list of these, needed for pulldown menus */
saferct = MEM_callocN(sizeof(uiSafetyRct), "uiSafetyRct");
saferct->safety = block->safety;
BLI_addhead(&block->saferct, saferct);
}
if (block->flag & UI_BLOCK_RADIAL) {
uiBut *but;
int win_width = UI_SCREEN_MARGIN;
int winx, winy;
int x_offset = 0, y_offset = 0;
winx = WM_window_pixels_x(window);
winy = WM_window_pixels_y(window);
copy_v2_v2(block->pie_data.pie_center_init, block->pie_data.pie_center_spawned);
/* only try translation if area is large enough */
if (BLI_rctf_size_x(&block->rect) < winx - (2.0f * win_width)) {
if (block->rect.xmin < win_width ) x_offset += win_width - block->rect.xmin;
if (block->rect.xmax > winx - win_width) x_offset += winx - win_width - block->rect.xmax;
}
if (BLI_rctf_size_y(&block->rect) < winy - (2.0f * win_width)) {
if (block->rect.ymin < win_width ) y_offset += win_width - block->rect.ymin;
if (block->rect.ymax > winy - win_width) y_offset += winy - win_width - block->rect.ymax;
}
/* if we are offsetting set up initial data for timeout functionality */
if ((x_offset != 0) || (y_offset != 0)) {
block->pie_data.pie_center_spawned[0] += x_offset;
block->pie_data.pie_center_spawned[1] += y_offset;
ui_block_translate(block, x_offset, y_offset);
if (U.pie_initial_timeout > 0)
block->pie_data.flags |= UI_PIE_INITIAL_DIRECTION;
}
ar->winrct.xmin = 0;
ar->winrct.xmax = winx;
ar->winrct.ymin = 0;
ar->winrct.ymax = winy;
ui_block_calc_pie_segment(block, block->pie_data.pie_center_init);
/* lastly set the buttons at the center of the pie menu, ready for animation */
if (U.pie_animation_timeout > 0) {
for (but = block->buttons.first; but; but = but->next) {
if (but->pie_dir != UI_RADIAL_NONE) {
BLI_rctf_recenter(&but->rect, UNPACK2(block->pie_data.pie_center_spawned));
}
}
}
}
else {
/* clip block with window boundary */
ui_popup_block_clip(window, block);
/* the block and buttons were positioned in window space as in 2.4x, now
* these menu blocks are regions so we bring it back to region space.
* additionally we add some padding for the menu shadow or rounded menus */
ar->winrct.xmin = block->rect.xmin - margin;
ar->winrct.xmax = block->rect.xmax + margin;
ar->winrct.ymin = block->rect.ymin - margin;
ar->winrct.ymax = block->rect.ymax + MENU_TOP;
ui_block_translate(block, -ar->winrct.xmin, -ar->winrct.ymin);
}
if (block_old) {
block->oldblock = block_old;
UI_block_update_from_old(C, block);
UI_blocklist_free_inactive(C, &ar->uiblocks);
}
/* checks which buttons are visible, sets flags to prevent draw (do after region init) */
ui_popup_block_scrolltest(block);
/* adds subwindow */
ED_region_init(C, ar);
/* get winmat now that we actually have the subwindow */
wmSubWindowSet(window, ar->swinid);
wm_subwindow_matrix_get(window, ar->swinid, block->winmat);
/* notify change and redraw */
ED_region_tag_redraw(ar);
ED_region_update_rect(C, ar);
#ifdef DEBUG
window->eventstate = event_back;
#endif
return block;
}
uiPopupBlockHandle *ui_popup_block_create(
bContext *C, ARegion *butregion, uiBut *but,
uiBlockCreateFunc create_func, uiBlockHandleCreateFunc handle_create_func,
void *arg)
{
wmWindow *window = CTX_wm_window(C);
static ARegionType type;
ARegion *ar;
uiBlock *block;
uiPopupBlockHandle *handle;
/* create handle */
handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
/* store context for operator */
handle->ctx_area = CTX_wm_area(C);
handle->ctx_region = CTX_wm_region(C);
/* store vars to refresh popup (RGN_DRAW_REFRESH_UI) */
handle->popup_create_vars.create_func = create_func;
handle->popup_create_vars.handle_create_func = handle_create_func;
handle->popup_create_vars.arg = arg;
handle->popup_create_vars.butregion = but ? butregion : NULL;
copy_v2_v2_int(handle->popup_create_vars.event_xy, &window->eventstate->x);
/* create area region */
ar = ui_region_temp_add(CTX_wm_screen(C));
handle->region = ar;
memset(&type, 0, sizeof(ARegionType));
type.draw = ui_block_region_draw;
type.regionid = RGN_TYPE_TEMPORARY;
ar->type = &type;
UI_region_handlers_add(&ar->handlers);
block = ui_popup_block_refresh(C, handle, butregion, but);
handle = block->handle;
return handle;
}
void ui_popup_block_free(bContext *C, uiPopupBlockHandle *handle)
{
ui_popup_block_remove(C, handle);
MEM_freeN(handle);
}
/***************************** Menu Button ***************************/
#if 0
static void ui_warp_pointer(int x, int y)
{
/* XXX 2.50 which function to use for this? */
/* OSX has very poor mouse-warp support, it sends events;
* this causes a menu being pressed immediately ... */
# ifndef __APPLE__
warp_pointer(x, y);
# endif
}
#endif
/********************* Color Button ****************/
/* for picker, while editing hsv */
void ui_but_hsv_set(uiBut *but)
{
float col[3];
ColorPicker *cpicker = but->custom_data;
float *hsv = cpicker->color_data;
ui_color_picker_to_rgb_v(hsv, col);
ui_but_v3_set(but, col);
}
/* Updates all buttons who share the same color picker as the one passed
* also used by small picker, be careful with name checks below... */
static void ui_update_color_picker_buts_rgb(uiBlock *block, ColorPicker *cpicker, const float rgb[3], bool is_display_space)
{
uiBut *bt;
float *hsv = cpicker->color_data;
struct ColorManagedDisplay *display = NULL;
/* this is to keep the H and S value when V is equal to zero
* and we are working in HSV mode, of course!
*/
if (is_display_space) {
ui_rgb_to_color_picker_compat_v(rgb, hsv);
}
else {
/* we need to convert to display space to use hsv, because hsv is stored in display space */
float rgb_display[3];
copy_v3_v3(rgb_display, rgb);
ui_block_cm_to_display_space_v3(block, rgb_display);
ui_rgb_to_color_picker_compat_v(rgb_display, hsv);
}
if (block->color_profile)
display = ui_block_cm_display_get(block);
/* this updates button strings, is hackish... but button pointers are on stack of caller function */
for (bt = block->buttons.first; bt; bt = bt->next) {
if (bt->custom_data != cpicker)
continue;
if (bt->rnaprop) {
ui_but_v3_set(bt, rgb);
}
else if (STREQ(bt->str, "Hex: ")) {
float rgb_gamma[3];
unsigned char rgb_gamma_uchar[3];
double intpart;
char col[16];
/* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
copy_v3_v3(rgb_gamma, rgb);
if (display) {
/* make a display version, for Hex code */
IMB_colormanagement_scene_linear_to_display_v3(rgb_gamma, display);
}
if (rgb_gamma[0] > 1.0f) rgb_gamma[0] = modf(rgb_gamma[0], &intpart);
if (rgb_gamma[1] > 1.0f) rgb_gamma[1] = modf(rgb_gamma[1], &intpart);
if (rgb_gamma[2] > 1.0f) rgb_gamma[2] = modf(rgb_gamma[2], &intpart);
rgb_float_to_uchar(rgb_gamma_uchar, rgb_gamma);
BLI_snprintf(col, sizeof(col), "%02X%02X%02X", UNPACK3_EX((unsigned int), rgb_gamma_uchar, ));
strcpy(bt->poin, col);
}
else if (bt->str[1] == ' ') {
if (bt->str[0] == 'R') {
ui_but_value_set(bt, rgb[0]);
}
else if (bt->str[0] == 'G') {
ui_but_value_set(bt, rgb[1]);
}
else if (bt->str[0] == 'B') {
ui_but_value_set(bt, rgb[2]);
}
else if (bt->str[0] == 'H') {
ui_but_value_set(bt, hsv[0]);
}
else if (bt->str[0] == 'S') {
ui_but_value_set(bt, hsv[1]);
}
else if (bt->str[0] == 'V') {
ui_but_value_set(bt, hsv[2]);
}
else if (bt->str[0] == 'L') {
ui_but_value_set(bt, hsv[2]);
}
}
ui_but_update(bt);
}
}
static void ui_colorpicker_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
{
uiBut *but = (uiBut *)bt1;
uiPopupBlockHandle *popup = but->block->handle;
PropertyRNA *prop = but->rnaprop;
PointerRNA ptr = but->rnapoin;
float rgb[4];
if (prop) {
RNA_property_float_get_array(&ptr, prop, rgb);
ui_update_color_picker_buts_rgb(but->block, but->custom_data, rgb, (RNA_property_subtype(prop) == PROP_COLOR_GAMMA));
}
if (popup)
popup->menuretval = UI_RETURN_UPDATE;
}
static void ui_color_wheel_rna_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
{
uiBut *but = (uiBut *)bt1;
uiPopupBlockHandle *popup = but->block->handle;
float rgb[3];
ColorPicker *cpicker = but->custom_data;
float *hsv = cpicker->color_data;
bool use_display_colorspace = ui_but_is_colorpicker_display_space(but);
ui_color_picker_to_rgb_v(hsv, rgb);
/* hsv is saved in display space so convert back */
if (use_display_colorspace) {
ui_block_cm_to_scene_linear_v3(but->block, rgb);
}
ui_update_color_picker_buts_rgb(but->block, cpicker, rgb, !use_display_colorspace);
if (popup)
popup->menuretval = UI_RETURN_UPDATE;
}
static void ui_colorpicker_hex_rna_cb(bContext *UNUSED(C), void *bt1, void *hexcl)
{
uiBut *but = (uiBut *)bt1;
uiPopupBlockHandle *popup = but->block->handle;
ColorPicker *cpicker = but->custom_data;
char *hexcol = (char *)hexcl;
float rgb[3];
hex_to_rgb(hexcol, rgb, rgb + 1, rgb + 2);
/* Hex code is assumed to be in sRGB space (coming from other applications, web, etc) */
if (but->block->color_profile) {
/* so we need to linearise it for Blender */
ui_block_cm_to_scene_linear_v3(but->block, rgb);
}
ui_update_color_picker_buts_rgb(but->block, cpicker, rgb, false);
if (popup)
popup->menuretval = UI_RETURN_UPDATE;
}
static void ui_popup_close_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
{
uiBut *but = (uiBut *)bt1;
uiPopupBlockHandle *popup = but->block->handle;
if (popup)
popup->menuretval = UI_RETURN_OK;
}
static void ui_colorpicker_hide_reveal(uiBlock *block, short colormode)
{
uiBut *bt;
/* tag buttons */
for (bt = block->buttons.first; bt; bt = bt->next) {
if ((bt->func == ui_colorpicker_rna_cb) && bt->type == UI_BTYPE_NUM_SLIDER && bt->rnaindex != 3) {
/* RGB sliders (color circle and alpha are always shown) */
if (colormode == 0) bt->flag &= ~UI_HIDDEN;
else bt->flag |= UI_HIDDEN;
}
else if (bt->func == ui_color_wheel_rna_cb) {
/* HSV sliders */
if (colormode == 1) bt->flag &= ~UI_HIDDEN;
else bt->flag |= UI_HIDDEN;
}
else if (bt->func == ui_colorpicker_hex_rna_cb || bt->type == UI_BTYPE_LABEL) {
/* hex input or gamma correction status label */
if (colormode == 2) bt->flag &= ~UI_HIDDEN;
else bt->flag |= UI_HIDDEN;
}
}
}
static void ui_colorpicker_create_mode_cb(bContext *UNUSED(C), void *bt1, void *UNUSED(arg))
{
uiBut *bt = bt1;
short colormode = ui_but_value_get(bt);
ui_colorpicker_hide_reveal(bt->block, colormode);
}
#define PICKER_H (7.5f * U.widget_unit)
#define PICKER_W (7.5f * U.widget_unit)
#define PICKER_SPACE (0.3f * U.widget_unit)
#define PICKER_BAR (0.7f * U.widget_unit)
#define PICKER_TOTAL_W (PICKER_W + PICKER_SPACE + PICKER_BAR)
static void ui_colorpicker_circle(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, ColorPicker *cpicker)
{
uiBut *bt;
/* HS circle */
bt = uiDefButR_prop(block, UI_BTYPE_HSVCIRCLE, 0, "", 0, 0, PICKER_H, PICKER_W, ptr, prop, -1, 0.0, 0.0, 0.0, 0, TIP_("Color"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
/* value */
if (U.color_picker_type == USER_CP_CIRCLE_HSL) {
bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", PICKER_W + PICKER_SPACE, 0, PICKER_BAR, PICKER_H, ptr, prop, -1, 0.0, 0.0, UI_GRAD_L_ALT, 0, "Lightness");
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
}
else {
bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", PICKER_W + PICKER_SPACE, 0, PICKER_BAR, PICKER_H, ptr, prop, -1, 0.0, 0.0, UI_GRAD_V_ALT, 0, TIP_("Value"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
}
bt->custom_data = cpicker;
}
static void ui_colorpicker_square(uiBlock *block, PointerRNA *ptr, PropertyRNA *prop, int type, ColorPicker *cpicker)
{
uiBut *bt;
int bartype = type + 3;
/* HS square */
bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", 0, PICKER_BAR + PICKER_SPACE, PICKER_TOTAL_W, PICKER_H, ptr, prop, -1, 0.0, 0.0, type, 0, TIP_("Color"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
/* value */
bt = uiDefButR_prop(block, UI_BTYPE_HSVCUBE, 0, "", 0, 0, PICKER_TOTAL_W, PICKER_BAR, ptr, prop, -1, 0.0, 0.0, bartype, 0, TIP_("Value"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
}
/* a HS circle, V slider, rgb/hsv/hex sliders */
static void ui_block_colorpicker(uiBlock *block, float rgba[4], PointerRNA *ptr, PropertyRNA *prop, bool show_picker)
{
static short colormode = 0; /* temp? 0=rgb, 1=hsv, 2=hex */
uiBut *bt;
int width, butwidth;
static char tip[50];
static char hexcol[128];
float rgb_gamma[3];
unsigned char rgb_gamma_uchar[3];
float softmin, softmax, hardmin, hardmax, step, precision;
int yco;
ColorPicker *cpicker = ui_block_colorpicker_create(block);
float *hsv = cpicker->color_data;
width = PICKER_TOTAL_W;
butwidth = width - 1.5f * UI_UNIT_X;
/* existence of profile means storage is in linear color space, with display correction */
/* XXX That tip message is not use anywhere! */
if (!block->color_profile) {
BLI_strncpy(tip, N_("Value in Display Color Space"), sizeof(tip));
copy_v3_v3(rgb_gamma, rgba);
}
else {
BLI_strncpy(tip, N_("Value in Linear RGB Color Space"), sizeof(tip));
/* make a display version, for Hex code */
copy_v3_v3(rgb_gamma, rgba);
ui_block_cm_to_display_space_v3(block, rgb_gamma);
}
/* sneaky way to check for alpha */
rgba[3] = FLT_MAX;
RNA_property_float_ui_range(ptr, prop, &softmin, &softmax, &step, &precision);
RNA_property_float_range(ptr, prop, &hardmin, &hardmax);
RNA_property_float_get_array(ptr, prop, rgba);
switch (U.color_picker_type) {
case USER_CP_SQUARE_SV:
ui_colorpicker_square(block, ptr, prop, UI_GRAD_SV, cpicker);
break;
case USER_CP_SQUARE_HS:
ui_colorpicker_square(block, ptr, prop, UI_GRAD_HS, cpicker);
break;
case USER_CP_SQUARE_HV:
ui_colorpicker_square(block, ptr, prop, UI_GRAD_HV, cpicker);
break;
/* user default */
case USER_CP_CIRCLE_HSV:
case USER_CP_CIRCLE_HSL:
default:
ui_colorpicker_circle(block, ptr, prop, cpicker);
break;
}
/* mode */
yco = -1.5f * UI_UNIT_Y;
UI_block_align_begin(block);
bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("RGB"), 0, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 0.0, 0, 0, "");
UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
bt->custom_data = cpicker;
if (U.color_picker_type == USER_CP_CIRCLE_HSL)
bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("HSL"), width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
else
bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("HSV"), width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 1.0, 0, 0, "");
UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
bt->custom_data = cpicker;
bt = uiDefButS(block, UI_BTYPE_ROW, 0, IFACE_("Hex"), 2 * width / 3, yco, width / 3, UI_UNIT_Y, &colormode, 0.0, 2.0, 0, 0, "");
UI_but_func_set(bt, ui_colorpicker_create_mode_cb, bt, NULL);
bt->custom_data = cpicker;
UI_block_align_end(block);
yco = -3.0f * UI_UNIT_Y;
if (show_picker) {
bt = uiDefIconButO(block, UI_BTYPE_BUT, "UI_OT_eyedropper_color", WM_OP_INVOKE_DEFAULT, ICON_EYEDROPPER, butwidth + 10, yco, UI_UNIT_X, UI_UNIT_Y, NULL);
UI_but_func_set(bt, ui_popup_close_cb, bt, NULL);
bt->custom_data = cpicker;
}
/* RGB values */
UI_block_align_begin(block);
bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("R:"), 0, yco, butwidth, UI_UNIT_Y, ptr, prop, 0, 0.0, 0.0, 0, 3, TIP_("Red"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("G:"), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 1, 0.0, 0.0, 0, 3, TIP_("Green"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("B:"), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 2, 0.0, 0.0, 0, 3, TIP_("Blue"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
/* could use uiItemFullR(col, ptr, prop, -1, 0, UI_ITEM_R_EXPAND|UI_ITEM_R_SLIDER, "", ICON_NONE);
* but need to use UI_but_func_set for updating other fake buttons */
/* HSV values */
yco = -3.0f * UI_UNIT_Y;
UI_block_align_begin(block);
bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("H:"), 0, yco, butwidth, UI_UNIT_Y, hsv, 0.0, 1.0, 10, 3, TIP_("Hue"));
UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
bt->custom_data = cpicker;
bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("S:"), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 1, 0.0, 1.0, 10, 3, TIP_("Saturation"));
UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
bt->custom_data = cpicker;
if (U.color_picker_type == USER_CP_CIRCLE_HSL)
bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("L:"), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 2, 0.0, 1.0, 10, 3, TIP_("Lightness"));
else
bt = uiDefButF(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("V:"), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, hsv + 2, 0.0, softmax, 10, 3, TIP_("Value"));
bt->hardmax = hardmax; /* not common but rgb may be over 1.0 */
UI_but_func_set(bt, ui_color_wheel_rna_cb, bt, hsv);
bt->custom_data = cpicker;
UI_block_align_end(block);
if (rgba[3] != FLT_MAX) {
bt = uiDefButR_prop(block, UI_BTYPE_NUM_SLIDER, 0, IFACE_("A: "), 0, yco -= UI_UNIT_Y, butwidth, UI_UNIT_Y, ptr, prop, 3, 0.0, 0.0, 0, 3, TIP_("Alpha"));
UI_but_func_set(bt, ui_colorpicker_rna_cb, bt, NULL);
bt->custom_data = cpicker;
}
else {
rgba[3] = 1.0f;
}
rgb_float_to_uchar(rgb_gamma_uchar, rgb_gamma);
BLI_snprintf(hexcol, sizeof(hexcol), "%02X%02X%02X", UNPACK3_EX((unsigned int), rgb_gamma_uchar, ));
yco = -3.0f * UI_UNIT_Y;
bt = uiDefBut(block, UI_BTYPE_TEXT, 0, IFACE_("Hex: "), 0, yco, butwidth, UI_UNIT_Y, hexcol, 0, 8, 0, 0, TIP_("Hex triplet for color (#RRGGBB)"));
UI_but_func_set(bt, ui_colorpicker_hex_rna_cb, bt, hexcol);
bt->custom_data = cpicker;
uiDefBut(block, UI_BTYPE_LABEL, 0, IFACE_("(Gamma Corrected)"), 0, yco - UI_UNIT_Y, butwidth, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
ui_rgb_to_color_picker_v(rgb_gamma, hsv);
ui_colorpicker_hide_reveal(block, colormode);
}
static int ui_colorpicker_small_wheel_cb(const bContext *UNUSED(C), uiBlock *block, const wmEvent *event)
{
float add = 0.0f;
if (event->type == WHEELUPMOUSE)
add = 0.05f;
else if (event->type == WHEELDOWNMOUSE)
add = -0.05f;
if (add != 0.0f) {
uiBut *but;
for (but = block->buttons.first; but; but = but->next) {
if (but->type == UI_BTYPE_HSVCUBE && but->active == NULL) {
uiPopupBlockHandle *popup = block->handle;
float rgb[3];
ColorPicker *cpicker = but->custom_data;
float *hsv = cpicker->color_data;
bool use_display_colorspace = ui_but_is_colorpicker_display_space(but);
ui_but_v3_get(but, rgb);
if (use_display_colorspace)
ui_block_cm_to_display_space_v3(block, rgb);
ui_rgb_to_color_picker_compat_v(rgb, hsv);
hsv[2] = CLAMPIS(hsv[2] + add, 0.0f, 1.0f);
ui_color_picker_to_rgb_v(hsv, rgb);
if (use_display_colorspace)
ui_block_cm_to_scene_linear_v3(block, rgb);
ui_but_v3_set(but, rgb);
ui_update_color_picker_buts_rgb(block, cpicker, rgb, !use_display_colorspace);
if (popup)
popup->menuretval = UI_RETURN_UPDATE;
return 1;
}
}
}
return 0;
}
uiBlock *ui_block_func_COLOR(bContext *C, uiPopupBlockHandle *handle, void *arg_but)
{
uiBut *but = arg_but;
uiBlock *block;
bool show_picker = true;
block = UI_block_begin(C, handle->region, __func__, UI_EMBOSS);
if (RNA_property_subtype(but->rnaprop) == PROP_COLOR_GAMMA) {
block->color_profile = false;
}
if (but->block) {
/* if color block is invoked from a popup we wouldn't be able to set color properly
* this is because color picker will close popups first and then will try to figure
* out active button RNA, and of course it'll fail
*/
show_picker = (but->block->flag & UI_BLOCK_POPUP) == 0;
}
copy_v3_v3(handle->retvec, but->editvec);
ui_block_colorpicker(block, handle->retvec, &but->rnapoin, but->rnaprop, show_picker);
block->flag = UI_BLOCK_LOOP | UI_BLOCK_KEEP_OPEN | UI_BLOCK_OUT_1 | UI_BLOCK_MOVEMOUSE_QUIT;
UI_block_bounds_set_normal(block, 0.5 * UI_UNIT_X);
block->block_event_func = ui_colorpicker_small_wheel_cb;
/* and lets go */
block->direction = UI_DIR_UP;
return block;
}
/************************ Popup Menu Memory ****************************/
static unsigned int ui_popup_string_hash(const char *str)
{
/* sometimes button contains hotkey, sometimes not, strip for proper compare */
int hash;
const char *delimit = strrchr(str, UI_SEP_CHAR);
if (delimit) {
hash = BLI_ghashutil_strhash_n(str, delimit - str);
}
else {
hash = BLI_ghashutil_strhash(str);
}
return hash;
}
static unsigned int ui_popup_menu_hash(const char *str)
{
return BLI_ghashutil_strhash(str);
}
/* but == NULL read, otherwise set */
static uiBut *ui_popup_menu_memory__internal(uiBlock *block, uiBut *but)
{
static unsigned int mem[256];
static bool first = true;
const unsigned int hash = block->puphash;
const unsigned int hash_mod = hash & 255;
if (first) {
/* init */
memset(mem, -1, sizeof(mem));
first = 0;
}
if (but) {
/* set */
mem[hash_mod] = ui_popup_string_hash(but->str);
return NULL;
}
else {
/* get */
for (but = block->buttons.first; but; but = but->next)
if (ui_popup_string_hash(but->str) == mem[hash_mod])
return but;
return NULL;
}
}
uiBut *ui_popup_menu_memory_get(uiBlock *block)
{
return ui_popup_menu_memory__internal(block, NULL);
}
void ui_popup_menu_memory_set(uiBlock *block, uiBut *but)
{
ui_popup_menu_memory__internal(block, but);
}
/**
* Translate any popup regions (so we can drag them).
*/
void ui_popup_translate(bContext *C, ARegion *ar, const int mdiff[2])
{
uiBlock *block;
BLI_rcti_translate(&ar->winrct, UNPACK2(mdiff));
ED_region_update_rect(C, ar);
ED_region_tag_redraw(ar);
/* update blocks */
for (block = ar->uiblocks.first; block; block = block->next) {
uiSafetyRct *saferct;
for (saferct = block->saferct.first; saferct; saferct = saferct->next) {
BLI_rctf_translate(&saferct->parent, UNPACK2(mdiff));
BLI_rctf_translate(&saferct->safety, UNPACK2(mdiff));
}
}
}
/******************** Popup Menu with callback or string **********************/
struct uiPopupMenu {
uiBlock *block;
uiLayout *layout;
uiBut *but;
int mx, my;
bool popup, slideout;
uiMenuCreateFunc menu_func;
void *menu_arg;
};
struct uiPieMenu {
uiBlock *block_radial; /* radial block of the pie menu (more could be added later) */
uiLayout *layout;
int mx, my;
};
static uiBlock *ui_block_func_POPUP(bContext *C, uiPopupBlockHandle *handle, void *arg_pup)
{
uiBlock *block;
uiBut *bt;
uiPopupMenu *pup = arg_pup;
int offset[2], minwidth, width, height;
char direction;
bool flip;
if (pup->menu_func) {
pup->block->handle = handle;
pup->menu_func(C, pup->layout, pup->menu_arg);
pup->block->handle = NULL;
}
if (pup->but) {
/* minimum width to enforece */
minwidth = BLI_rctf_size_x(&pup->but->rect);
/* settings (typically rna-enum-popups) show above the button,
* menu's like file-menu, show below */
if (pup->block->direction != 0) {
/* allow overriding the direction from menu_func */
direction = pup->block->direction;
}
else if ((pup->but->type == UI_BTYPE_PULLDOWN) ||
(UI_but_menutype_get(pup->but) != NULL))
{
direction = UI_DIR_DOWN;
}
else {
direction = UI_DIR_UP;
}
}
else {
minwidth = 50;
direction = UI_DIR_DOWN;
}
flip = (direction == UI_DIR_DOWN);
block = pup->block;
/* in some cases we create the block before the region,
* so we set it delayed here if necessary */
if (BLI_findindex(&handle->region->uiblocks, block) == -1)
UI_block_region_set(block, handle->region);
block->direction = direction;
UI_block_layout_resolve(block, &width, &height);
UI_block_flag_enable(block, UI_BLOCK_MOVEMOUSE_QUIT);
if (pup->popup) {
uiBut *but_activate = NULL;
UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_NUMSELECT);
UI_block_direction_set(block, direction);
/* offset the mouse position, possibly based on earlier selection */
if ((block->flag & UI_BLOCK_POPUP_MEMORY) &&
(bt = ui_popup_menu_memory_get(block)))
{
/* position mouse on last clicked item, at 0.8*width of the
* button, so it doesn't overlap the text too much, also note
* the offset is negative because we are inverse moving the
* block to be under the mouse */
offset[0] = -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect));
offset[1] = -(bt->rect.ymin + 0.5f * UI_UNIT_Y);
if (ui_but_is_editable(bt)) {
but_activate = bt;
}
}
else {
/* position mouse at 0.8*width of the button and below the tile
* on the first item */
offset[0] = 0;
for (bt = block->buttons.first; bt; bt = bt->next)
offset[0] = min_ii(offset[0], -(bt->rect.xmin + 0.8f * BLI_rctf_size_x(&bt->rect)));
offset[1] = 2.1 * UI_UNIT_Y;
for (bt = block->buttons.first; bt; bt = bt->next) {
if (ui_but_is_editable(bt)) {
but_activate = bt;
break;
}
}
}
/* in rare cases this is needed since moving the popup
* to be within the window bounds may move it away from the mouse,
* This ensures we set an item to be active. */
if (but_activate) {
ui_but_activate_over(C, handle->region, but_activate);
}
block->minbounds = minwidth;
UI_block_bounds_set_menu(block, 1, offset[0], offset[1]);
}
else {
/* for a header menu we set the direction automatic */
if (!pup->slideout && flip) {
ScrArea *sa = CTX_wm_area(C);
if (sa && sa->headertype == HEADERDOWN) {
ARegion *ar = CTX_wm_region(C);
if (ar && ar->regiontype == RGN_TYPE_HEADER) {
UI_block_direction_set(block, UI_DIR_UP);
UI_block_order_flip(block);
}
}
}
block->minbounds = minwidth;
UI_block_bounds_set_text(block, 3.0f * UI_UNIT_X);
}
/* if menu slides out of other menu, override direction */
if (pup->slideout)
UI_block_direction_set(block, UI_DIR_RIGHT);
return pup->block;
}
uiPopupBlockHandle *ui_popup_menu_create(
bContext *C, ARegion *butregion, uiBut *but,
uiMenuCreateFunc menu_func, void *arg)
{
wmWindow *window = CTX_wm_window(C);
uiStyle *style = UI_style_get_dpi();
uiPopupBlockHandle *handle;
uiPopupMenu *pup;
pup = MEM_callocN(sizeof(uiPopupMenu), __func__);
pup->block = UI_block_begin(C, NULL, __func__, UI_EMBOSS_PULLDOWN);
pup->block->flag |= UI_BLOCK_NUMSELECT; /* default menus to numselect */
pup->layout = UI_block_layout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
pup->slideout = but ? ui_block_is_menu(but->block) : false;
pup->but = but;
uiLayoutSetOperatorContext(pup->layout, WM_OP_INVOKE_REGION_WIN);
if (!but) {
/* no button to start from, means we are a popup */
pup->mx = window->eventstate->x;
pup->my = window->eventstate->y;
pup->popup = true;
pup->block->flag |= UI_BLOCK_NO_FLIP;
}
/* some enums reversing is strange, currently we have no good way to
* reverse some enum's but not others, so reverse all so the first menu
* items are always close to the mouse cursor */
else {
#if 0
/* if this is an rna button then we can assume its an enum
* flipping enums is generally not good since the order can be
* important [#28786] */
if (but->rnaprop && RNA_property_type(but->rnaprop) == PROP_ENUM) {
pup->block->flag |= UI_BLOCK_NO_FLIP;
}
#endif
if (but->context)
uiLayoutContextCopy(pup->layout, but->context);
}
/* menu is created from a callback */
pup->menu_func = menu_func;
pup->menu_arg = arg;
handle = ui_popup_block_create(C, butregion, but, NULL, ui_block_func_POPUP, pup);
if (!but) {
handle->popup = true;
UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
WM_event_add_mousemove(C);
}
MEM_freeN(pup);
return handle;
}
/******************** Popup Menu API with begin and end ***********************/
/* only return handler, and set optional title */
uiPopupMenu *UI_popup_menu_begin(bContext *C, const char *title, int icon)
{
uiStyle *style = UI_style_get_dpi();
uiPopupMenu *pup = MEM_callocN(sizeof(uiPopupMenu), "popup menu");
uiBut *but;
pup->block = UI_block_begin(C, NULL, __func__, UI_EMBOSS_PULLDOWN);
pup->block->flag |= UI_BLOCK_POPUP_MEMORY | UI_BLOCK_IS_FLIP;
pup->block->puphash = ui_popup_menu_hash(title);
pup->layout = UI_block_layout(pup->block, UI_LAYOUT_VERTICAL, UI_LAYOUT_MENU, 0, 0, 200, 0, MENU_PADDING, style);
/* note, this intentionally differs from the menu & submenu default because many operators
* use popups like this to select one of their options - where having invoke doesn't make sense */
uiLayoutSetOperatorContext(pup->layout, WM_OP_EXEC_REGION_WIN);
/* create in advance so we can let buttons point to retval already */
pup->block->handle = MEM_callocN(sizeof(uiPopupBlockHandle), "uiPopupBlockHandle");
/* create title button */
if (title[0]) {
char titlestr[256];
if (icon) {
BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
but = uiDefIconTextBut(pup->block, UI_BTYPE_LABEL, 0, icon, titlestr, 0, 0,
UI_MENU_TITLE_WIDTH, UI_MENU_TITLE_HEIGHT, NULL,
0.0, 0.0, 0, 0, "");
but->flag |= UI_BUT_MENU_TITLE;
}
else {
but = uiDefMenuTitleBut(pup->block, title);
but->drawflag = UI_BUT_TEXT_LEFT;
}
}
return pup;
}
/* set the whole structure to work */
void UI_popup_menu_end(bContext *C, uiPopupMenu *pup)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *menu;
pup->popup = true;
pup->mx = window->eventstate->x;
pup->my = window->eventstate->y;
menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_POPUP, pup);
menu->popup = true;
UI_popup_handlers_add(C, &window->modalhandlers, menu, 0);
WM_event_add_mousemove(C);
MEM_freeN(pup);
}
uiLayout *UI_popup_menu_layout(uiPopupMenu *pup)
{
return pup->layout;
}
/*************************** Pie Menus ***************************************/
static uiBlock *ui_block_func_PIE(bContext *UNUSED(C), uiPopupBlockHandle *handle, void *arg_pie)
{
uiBlock *block;
uiPieMenu *pie = arg_pie;
int minwidth, width, height;
minwidth = 50;
block = pie->block_radial;
/* in some cases we create the block before the region,
* so we set it delayed here if necessary */
if (BLI_findindex(&handle->region->uiblocks, block) == -1)
UI_block_region_set(block, handle->region);
UI_block_layout_resolve(block, &width, &height);
UI_block_flag_enable(block, UI_BLOCK_LOOP | UI_BLOCK_NUMSELECT);
block->minbounds = minwidth;
block->bounds = 1;
block->mx = 0;
block->my = 0;
block->bounds_type = UI_BLOCK_BOUNDS_PIE_CENTER;
block->pie_data.pie_center_spawned[0] = pie->mx;
block->pie_data.pie_center_spawned[1] = pie->my;
return pie->block_radial;
}
static float ui_pie_menu_title_width(const char *name, int icon)
{
const uiFontStyle *fstyle = UI_FSTYLE_WIDGET;
return (UI_fontstyle_string_width(fstyle, name) +
(UI_UNIT_X * (1.50f + (icon ? 0.25f : 0.0f))));
}
uiPieMenu *UI_pie_menu_begin(struct bContext *C, const char *title, int icon, const wmEvent *event)
{
uiStyle *style;
uiPieMenu *pie;
short event_type;
wmWindow *win = CTX_wm_window(C);
style = UI_style_get_dpi();
pie = MEM_callocN(sizeof(uiPopupMenu), "pie menu");
pie->block_radial = UI_block_begin(C, NULL, __func__, UI_EMBOSS);
/* may be useful later to allow spawning pies
* from old positions */
/* pie->block_radial->flag |= UI_BLOCK_POPUP_MEMORY; */
pie->block_radial->puphash = ui_popup_menu_hash(title);
pie->block_radial->flag |= UI_BLOCK_RADIAL;
/* if pie is spawned by a left click, it is always assumed to be click style */
if (event->type == LEFTMOUSE) {
pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
pie->block_radial->pie_data.event = EVENT_NONE;
win->lock_pie_event = EVENT_NONE;
}
else {
if (win->last_pie_event != EVENT_NONE) {
/* original pie key has been released, so don't propagate the event */
if (win->lock_pie_event == EVENT_NONE) {
event_type = EVENT_NONE;
pie->block_radial->pie_data.flags |= UI_PIE_CLICK_STYLE;
}
else
event_type = win->last_pie_event;
}
else {
event_type = event->type;
}
pie->block_radial->pie_data.event = event_type;
win->lock_pie_event = event_type;
}
pie->layout = UI_block_layout(pie->block_radial, UI_LAYOUT_VERTICAL, UI_LAYOUT_PIEMENU, 0, 0, 200, 0, 0, style);
pie->mx = event->x;
pie->my = event->y;
/* create title button */
if (title[0]) {
uiBut *but;
char titlestr[256];
int w;
if (icon) {
BLI_snprintf(titlestr, sizeof(titlestr), " %s", title);
w = ui_pie_menu_title_width(titlestr, icon);
but = uiDefIconTextBut(pie->block_radial, UI_BTYPE_LABEL, 0, icon, titlestr, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
}
else {
w = ui_pie_menu_title_width(title, 0);
but = uiDefBut(pie->block_radial, UI_BTYPE_LABEL, 0, title, 0, 0, w, UI_UNIT_Y, NULL, 0.0, 0.0, 0, 0, "");
}
/* do not align left */
but->drawflag &= ~UI_BUT_TEXT_LEFT;
}
return pie;
}
void UI_pie_menu_end(bContext *C, uiPieMenu *pie)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *menu;
menu = ui_popup_block_create(C, NULL, NULL, NULL, ui_block_func_PIE, pie);
menu->popup = true;
menu->towardstime = PIL_check_seconds_timer();
UI_popup_handlers_add(
C, &window->modalhandlers,
menu, WM_HANDLER_ACCEPT_DBL_CLICK);
WM_event_add_mousemove(C);
MEM_freeN(pie);
}
uiLayout *UI_pie_menu_layout(uiPieMenu *pie)
{
return pie->layout;
}
int UI_pie_menu_invoke(struct bContext *C, const char *idname, const wmEvent *event)
{
uiPieMenu *pie;
uiLayout *layout;
Menu menu;
MenuType *mt = WM_menutype_find(idname, true);
if (mt == NULL) {
printf("%s: named menu \"%s\" not found\n", __func__, idname);
return OPERATOR_CANCELLED;
}
if (mt->poll && mt->poll(C, mt) == 0)
return OPERATOR_CANCELLED;
pie = UI_pie_menu_begin(C, IFACE_(mt->label), ICON_NONE, event);
layout = UI_pie_menu_layout(pie);
menu.layout = layout;
menu.type = mt;
if (G.debug & G_DEBUG_WM) {
printf("%s: opening menu \"%s\"\n", __func__, idname);
}
mt->draw(C, &menu);
UI_pie_menu_end(C, pie);
return OPERATOR_INTERFACE;
}
int UI_pie_menu_invoke_from_operator_enum(
struct bContext *C, const char *title, const char *opname,
const char *propname, const wmEvent *event)
{
uiPieMenu *pie;
uiLayout *layout;
pie = UI_pie_menu_begin(C, IFACE_(title), ICON_NONE, event);
layout = UI_pie_menu_layout(pie);
layout = uiLayoutRadial(layout);
uiItemsEnumO(layout, opname, propname);
UI_pie_menu_end(C, pie);
return OPERATOR_INTERFACE;
}
int UI_pie_menu_invoke_from_rna_enum(
struct bContext *C, const char *title, const char *path,
const wmEvent *event)
{
PointerRNA ctx_ptr;
PointerRNA r_ptr;
PropertyRNA *r_prop;
uiPieMenu *pie;
uiLayout *layout;
RNA_pointer_create(NULL, &RNA_Context, C, &ctx_ptr);
if (!RNA_path_resolve(&ctx_ptr, path, &r_ptr, &r_prop)) {
return OPERATOR_CANCELLED;
}
/* invalid property, only accept enums */
if (RNA_property_type(r_prop) != PROP_ENUM) {
BLI_assert(0);
return OPERATOR_CANCELLED;
}
pie = UI_pie_menu_begin(C, IFACE_(title), ICON_NONE, event);
layout = UI_pie_menu_layout(pie);
layout = uiLayoutRadial(layout);
uiItemFullR(layout, &r_ptr, r_prop, RNA_NO_INDEX, 0, UI_ITEM_R_EXPAND, NULL, 0);
UI_pie_menu_end(C, pie);
return OPERATOR_INTERFACE;
}
/*************************** Standard Popup Menus ****************************/
void UI_popup_menu_reports(bContext *C, ReportList *reports)
{
Report *report;
uiPopupMenu *pup = NULL;
uiLayout *layout;
if (!CTX_wm_window(C))
return;
for (report = reports->list.first; report; report = report->next) {
int icon;
const char *msg, *msg_next;
if (report->type < reports->printlevel) {
continue;
}
if (pup == NULL) {
char title[UI_MAX_DRAW_STR];
BLI_snprintf(title, sizeof(title), "%s: %s", IFACE_("Report"), report->typestr);
pup = UI_popup_menu_begin(C, title, ICON_NONE);
layout = UI_popup_menu_layout(pup);
}
else {
uiItemS(layout);
}
/* split each newline into a label */
msg = report->message;
icon = UI_icon_from_report_type(report->type);
do {
char buf[UI_MAX_DRAW_STR];
msg_next = strchr(msg, '\n');
if (msg_next) {
msg_next++;
BLI_strncpy(buf, msg, MIN2(sizeof(buf), msg_next - msg));
msg = buf;
}
uiItemL(layout, msg, icon);
icon = ICON_NONE;
} while ((msg = msg_next) && *msg);
}
if (pup) {
UI_popup_menu_end(C, pup);
}
}
int UI_popup_menu_invoke(bContext *C, const char *idname, ReportList *reports)
{
uiPopupMenu *pup;
uiLayout *layout;
Menu menu;
MenuType *mt = WM_menutype_find(idname, true);
if (mt == NULL) {
BKE_reportf(reports, RPT_ERROR, "Menu \"%s\" not found", idname);
return OPERATOR_CANCELLED;
}
if (mt->poll && mt->poll(C, mt) == 0)
return OPERATOR_CANCELLED;
pup = UI_popup_menu_begin(C, IFACE_(mt->label), ICON_NONE);
layout = UI_popup_menu_layout(pup);
menu.layout = layout;
menu.type = mt;
if (G.debug & G_DEBUG_WM) {
printf("%s: opening menu \"%s\"\n", __func__, idname);
}
mt->draw(C, &menu);
UI_popup_menu_end(C, pup);
return OPERATOR_INTERFACE;
}
/*************************** Popup Block API **************************/
void UI_popup_block_invoke_ex(bContext *C, uiBlockCreateFunc func, void *arg, const char *opname, int opcontext)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *handle;
handle = ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
handle->popup = true;
handle->optype = (opname) ? WM_operatortype_find(opname, 0) : NULL;
handle->opcontext = opcontext;
UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
WM_event_add_mousemove(C);
}
void UI_popup_block_invoke(bContext *C, uiBlockCreateFunc func, void *arg)
{
UI_popup_block_invoke_ex(C, func, arg, NULL, WM_OP_INVOKE_DEFAULT);
}
void UI_popup_block_ex(bContext *C, uiBlockCreateFunc func, uiBlockHandleFunc popup_func, uiBlockCancelFunc cancel_func, void *arg)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *handle;
handle = ui_popup_block_create(C, NULL, NULL, func, NULL, arg);
handle->popup = true;
handle->retvalue = 1;
handle->popup_arg = arg;
handle->popup_func = popup_func;
handle->cancel_func = cancel_func;
// handle->opcontext = opcontext;
UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
WM_event_add_mousemove(C);
}
#if 0 /* UNUSED */
void uiPupBlockOperator(bContext *C, uiBlockCreateFunc func, wmOperator *op, int opcontext)
{
wmWindow *window = CTX_wm_window(C);
uiPopupBlockHandle *handle;
handle = ui_popup_block_create(C, NULL, NULL, func, NULL, op);
handle->popup = 1;
handle->retvalue = 1;
handle->popup_arg = op;
handle->popup_func = operator_cb;
handle->cancel_func = confirm_cancel_operator;
handle->opcontext = opcontext;
UI_popup_handlers_add(C, &window->modalhandlers, handle, 0);
WM_event_add_mousemove(C);
}
#endif
void UI_popup_block_close(bContext *C, wmWindow *win, uiBlock *block)
{
/* if loading new .blend while popup is open, window will be NULL */
if (block->handle) {
if (win) {
UI_popup_handlers_remove(&win->modalhandlers, block->handle);
ui_popup_block_free(C, block->handle);
}
}
}
ColorPicker *ui_block_colorpicker_create(struct uiBlock *block)
{
ColorPicker *cpicker = MEM_callocN(sizeof(ColorPicker), "color_picker");
BLI_addhead(&block->color_pickers.list, cpicker);
return cpicker;
}
void ui_rgb_to_color_picker_compat_v(const float rgb[3], float r_cp[3])
{
switch (U.color_picker_type) {
case USER_CP_CIRCLE_HSL:
rgb_to_hsl_compat_v(rgb, r_cp);
break;
default:
rgb_to_hsv_compat_v(rgb, r_cp);
break;
}
}
void ui_rgb_to_color_picker_v(const float rgb[3], float r_cp[3])
{
switch (U.color_picker_type) {
case USER_CP_CIRCLE_HSL:
rgb_to_hsl_v(rgb, r_cp);
break;
default:
rgb_to_hsv_v(rgb, r_cp);
break;
}
}
void ui_color_picker_to_rgb_v(const float r_cp[3], float rgb[3])
{
switch (U.color_picker_type) {
case USER_CP_CIRCLE_HSL:
hsl_to_rgb_v(r_cp, rgb);
break;
default:
hsv_to_rgb_v(r_cp, rgb);
break;
}
}
void ui_color_picker_to_rgb(float r_cp0, float r_cp1, float r_cp2, float *r, float *g, float *b)
{
switch (U.color_picker_type) {
case USER_CP_CIRCLE_HSL:
hsl_to_rgb(r_cp0, r_cp1, r_cp2, r, g, b);
break;
default:
hsv_to_rgb(r_cp0, r_cp1, r_cp2, r, g, b);
break;
}
}
|