1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
|
/* GTK - The GIMP Toolkit
* Copyright (C) 1995-1997 Peter Mattis, Spencer Kimball and Josh MacDonald
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Modified by the GTK+ Team and others 1997-2000. See the AUTHORS
* file for a list of people on the GTK+ Team. See the ChangeLog
* files for a list of changes. These files are distributed with
* GTK+ at ftp://ftp.gtk.org/pub/gtk/.
*/
/**
* SECTION:gtkmenu
* @Short_description: A menu widget
* @Title: GtkMenu
*
* A #GtkMenu is a #GtkMenuShell that implements a drop down menu
* consisting of a list of #GtkMenuItem objects which can be navigated
* and activated by the user to perform application functions.
*
* A #GtkMenu is most commonly dropped down by activating a
* #GtkMenuItem in a #GtkMenuBar or popped up by activating a
* #GtkMenuItem in another #GtkMenu.
*
* A #GtkMenu can also be popped up by activating a #GtkComboBox.
* Other composite widgets such as the #GtkNotebook can pop up a
* #GtkMenu as well.
*
* Applications can display a #GtkMenu as a popup menu by calling the
* gtk_menu_popup() function. The example below shows how an application
* can pop up a menu when the 3rd mouse button is pressed.
*
* ## Connecting the popup signal handler.
*
* |[<!-- language="C" -->
* // connect our handler which will popup the menu
* gesture = gtk_gesture_multi_press_new (window);
* gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture),
* GDK_BUTTON_SECONDARY);
* g_signal_connect (gesture, "event", G_CALLBACK (my_popup_handler), menu);
* ]|
*
* ## Signal handler which displays a popup menu.
*
* |[<!-- language="C" -->
* static void
* my_popup_handler (GtkGesture *gesture,
* guint n_press,
* double x,
* double y,
* gpointer data)
* {
* GtkMenu *menu = data;
*
* gtk_menu_popup (menu, NULL, NULL, NULL, NULL, button, GDK_CURRENT_TIME);
* }
* ]|
*
* # CSS nodes
*
* |[<!-- language="plain" -->
* menu
* ├── arrow.top
* ├── <child>
* ┊
* ├── <child>
* ╰── arrow.bottom
* ]|
*
* The main CSS node of GtkMenu has name menu, and there are two subnodes
* with name arrow, for scrolling menu arrows. These subnodes get the
* .top and .bottom style classes.
*/
#include "config.h"
#include "gtkmenuprivate.h"
#include "gtkaccellabel.h"
#include "gtkaccelmap.h"
#include "gtkadjustment.h"
#include "gtkbindings.h"
#include "gtkbox.h"
#include "gtkcheckmenuitemprivate.h"
#include "gtkcssnodeprivate.h"
#include "gtkcssstylepropertyprivate.h"
#include "gtkdnd.h"
#include "gtkeventcontrollerscroll.h"
#include "gtkiconprivate.h"
#include "gtkintl.h"
#include "gtkmain.h"
#include "gtkmarshalers.h"
#include "gtkmenuitemprivate.h"
#include "gtkmenushellprivate.h"
#include "gtkprivate.h"
#include "gtkscrollbar.h"
#include "gtksettings.h"
#include "gtksnapshot.h"
#include "gtkstylecontextprivate.h"
#include "gtktypebuiltins.h"
#include "gtkwidgetpath.h"
#include "gtkwidgetprivate.h"
#include "gtkwindowgroup.h"
#include "gtkwindowprivate.h"
#include "gtkeventcontrollerkey.h"
#include "a11y/gtkmenuaccessible.h"
#include "gdk/gdk-private.h"
#include <gobject/gvaluecollector.h>
#include <string.h>
#define NAVIGATION_REGION_OVERSHOOT 50 /* How much the navigation region
* extends below the submenu
*/
#define MENU_SCROLL_STEP1 8
#define MENU_SCROLL_STEP2 15
#define MENU_SCROLL_FAST_ZONE 8
#define MENU_SCROLL_TIMEOUT1 50
#define MENU_SCROLL_TIMEOUT2 20
#define MENU_POPUP_DELAY 225
#define ATTACH_INFO_KEY "gtk-menu-child-attach-info-key"
#define ATTACHED_MENUS "gtk-attached-menus"
typedef struct _GtkMenuAttachData GtkMenuAttachData;
typedef struct _GtkMenuPopdownData GtkMenuPopdownData;
struct _GtkMenuAttachData
{
GtkWidget *attach_widget;
GtkMenuDetachFunc detacher;
};
struct _GtkMenuPopdownData
{
GtkMenu *menu;
GdkDevice *device;
};
typedef struct
{
gint left_attach;
gint right_attach;
gint top_attach;
gint bottom_attach;
gint effective_left_attach;
gint effective_right_attach;
gint effective_top_attach;
gint effective_bottom_attach;
} AttachInfo;
enum {
MOVE_SCROLL,
POPPED_UP,
LAST_SIGNAL
};
enum {
PROP_0,
PROP_ACTIVE,
PROP_ACCEL_GROUP,
PROP_ACCEL_PATH,
PROP_ATTACH_WIDGET,
PROP_TEAROFF_STATE,
PROP_TEAROFF_TITLE,
PROP_MONITOR,
PROP_RESERVE_TOGGLE_SIZE,
PROP_ANCHOR_HINTS,
PROP_RECT_ANCHOR_DX,
PROP_RECT_ANCHOR_DY,
PROP_MENU_TYPE_HINT
};
enum {
CHILD_PROP_0,
CHILD_PROP_LEFT_ATTACH,
CHILD_PROP_RIGHT_ATTACH,
CHILD_PROP_TOP_ATTACH,
CHILD_PROP_BOTTOM_ATTACH
};
static void gtk_menu_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_menu_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec);
static void gtk_menu_finalize (GObject *object);
static void gtk_menu_set_child_property(GtkContainer *container,
GtkWidget *child,
guint property_id,
const GValue *value,
GParamSpec *pspec);
static void gtk_menu_get_child_property(GtkContainer *container,
GtkWidget *child,
guint property_id,
GValue *value,
GParamSpec *pspec);
static void gtk_menu_destroy (GtkWidget *widget);
static void gtk_menu_realize (GtkWidget *widget);
static void gtk_menu_unrealize (GtkWidget *widget);
static void gtk_menu_size_allocate (GtkWidget *widget,
const GtkAllocation *allocation,
int baseline);
static void gtk_menu_show (GtkWidget *widget);
static void gtk_menu_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot);
static void gtk_menu_motion (GtkEventController *controller,
double x,
double y,
gpointer user_data);
static void gtk_menu_enter (GtkEventController *controller,
double x,
double y,
gpointer user_data);
static void gtk_menu_leave (GtkEventController *controller,
gpointer user_data);
static gboolean gtk_menu_key_pressed (GtkEventControllerKey *controller,
guint keyval,
guint keycode,
GdkModifierType state,
GtkMenu *menu);
static void gtk_menu_scroll_to (GtkMenu *menu,
gint offset);
static void gtk_menu_grab_notify (GtkWidget *widget,
gboolean was_grabbed);
static gboolean gtk_menu_captured_event (GtkWidget *widget,
GdkEvent *event);
static void gtk_menu_scroll_controller_scroll (GtkEventControllerScroll *scroll,
gdouble dx,
gdouble dy,
GtkMenu *menu);
static void gtk_menu_stop_scrolling (GtkMenu *menu);
static void gtk_menu_remove_scroll_timeout (GtkMenu *menu);
static gboolean gtk_menu_scroll_timeout (gpointer data);
static void gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
GtkWidget *menu_item);
static void gtk_menu_select_item (GtkMenuShell *menu_shell,
GtkWidget *menu_item);
static void gtk_menu_real_insert (GtkMenuShell *menu_shell,
GtkWidget *child,
gint position);
static void gtk_menu_handle_scrolling (GtkMenu *menu,
gint event_x,
gint event_y,
gboolean enter,
gboolean motion);
static gboolean gtk_menu_focus (GtkWidget *widget,
GtkDirectionType direction);
static gint gtk_menu_get_popup_delay (GtkMenuShell *menu_shell);
static void gtk_menu_move_current (GtkMenuShell *menu_shell,
GtkMenuDirectionType direction);
static void gtk_menu_real_move_scroll (GtkMenu *menu,
GtkScrollType type);
static void gtk_menu_stop_navigating_submenu (GtkMenu *menu);
static gboolean gtk_menu_navigating_submenu (GtkMenu *menu,
gint event_x,
gint event_y);
static void gtk_menu_deactivate (GtkMenuShell *menu_shell);
static void gtk_menu_position (GtkMenu *menu,
gboolean set_scroll_offset);
static void gtk_menu_remove (GtkContainer *menu,
GtkWidget *widget);
static void menu_grab_transfer_surface_destroy (GtkMenu *menu);
static GdkSurface *menu_grab_transfer_surface_get (GtkMenu *menu);
static gboolean gtk_menu_real_can_activate_accel (GtkWidget *widget,
guint signal_id);
static void _gtk_menu_refresh_accel_paths (GtkMenu *menu,
gboolean group_changed);
static void gtk_menu_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline);
static void gtk_menu_pressed_cb (GtkGestureMultiPress *gesture,
int n_press,
double x,
double y,
gpointer user_data);
static void gtk_menu_released_cb (GtkGestureMultiPress *gesture,
int n_press,
double x,
double y,
gpointer user_data);
static const gchar attach_data_key[] = "gtk-menu-attach-data";
static guint menu_signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GtkMenu, gtk_menu, GTK_TYPE_MENU_SHELL)
static void
menu_queue_resize (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
priv->have_layout = FALSE;
gtk_widget_queue_resize (GTK_WIDGET (menu));
}
static void
attach_info_free (AttachInfo *info)
{
g_slice_free (AttachInfo, info);
}
static AttachInfo *
get_attach_info (GtkWidget *child)
{
GObject *object = G_OBJECT (child);
AttachInfo *ai = g_object_get_data (object, ATTACH_INFO_KEY);
if (!ai)
{
ai = g_slice_new0 (AttachInfo);
g_object_set_data_full (object, I_(ATTACH_INFO_KEY), ai,
(GDestroyNotify) attach_info_free);
}
return ai;
}
static gboolean
is_grid_attached (AttachInfo *ai)
{
return (ai->left_attach >= 0 &&
ai->right_attach >= 0 &&
ai->top_attach >= 0 &&
ai->bottom_attach >= 0);
}
static void
menu_ensure_layout (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
if (!priv->have_layout)
{
GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
GList *l;
gchar *row_occupied;
gint current_row;
gint max_right_attach;
gint max_bottom_attach;
/* Find extents of gridded portion
*/
max_right_attach = 1;
max_bottom_attach = 0;
for (l = menu_shell->priv->children; l; l = l->next)
{
GtkWidget *child = l->data;
AttachInfo *ai = get_attach_info (child);
if (is_grid_attached (ai))
{
max_bottom_attach = MAX (max_bottom_attach, ai->bottom_attach);
max_right_attach = MAX (max_right_attach, ai->right_attach);
}
}
/* Find empty rows */
row_occupied = g_malloc0 (max_bottom_attach);
for (l = menu_shell->priv->children; l; l = l->next)
{
GtkWidget *child = l->data;
AttachInfo *ai = get_attach_info (child);
if (is_grid_attached (ai))
{
gint i;
for (i = ai->top_attach; i < ai->bottom_attach; i++)
row_occupied[i] = TRUE;
}
}
/* Lay non-grid-items out in those rows
*/
current_row = 0;
for (l = menu_shell->priv->children; l; l = l->next)
{
GtkWidget *child = l->data;
AttachInfo *ai = get_attach_info (child);
if (!is_grid_attached (ai))
{
while (current_row < max_bottom_attach && row_occupied[current_row])
current_row++;
ai->effective_left_attach = 0;
ai->effective_right_attach = max_right_attach;
ai->effective_top_attach = current_row;
ai->effective_bottom_attach = current_row + 1;
current_row++;
}
else
{
ai->effective_left_attach = ai->left_attach;
ai->effective_right_attach = ai->right_attach;
ai->effective_top_attach = ai->top_attach;
ai->effective_bottom_attach = ai->bottom_attach;
}
}
g_free (row_occupied);
priv->n_rows = MAX (current_row, max_bottom_attach);
priv->n_columns = max_right_attach;
priv->have_layout = TRUE;
}
}
static gint
gtk_menu_get_n_columns (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
menu_ensure_layout (menu);
return priv->n_columns;
}
static gint
gtk_menu_get_n_rows (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
menu_ensure_layout (menu);
return priv->n_rows;
}
static void
get_effective_child_attach (GtkWidget *child,
int *l,
int *r,
int *t,
int *b)
{
GtkMenu *menu = GTK_MENU (gtk_widget_get_parent (child));
AttachInfo *ai;
menu_ensure_layout (menu);
ai = get_attach_info (child);
if (l)
*l = ai->effective_left_attach;
if (r)
*r = ai->effective_right_attach;
if (t)
*t = ai->effective_top_attach;
if (b)
*b = ai->effective_bottom_attach;
}
static void
gtk_menu_class_init (GtkMenuClass *class)
{
GObjectClass *gobject_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
GtkMenuShellClass *menu_shell_class = GTK_MENU_SHELL_CLASS (class);
GtkBindingSet *binding_set;
gobject_class->set_property = gtk_menu_set_property;
gobject_class->get_property = gtk_menu_get_property;
gobject_class->finalize = gtk_menu_finalize;
widget_class->destroy = gtk_menu_destroy;
widget_class->realize = gtk_menu_realize;
widget_class->unrealize = gtk_menu_unrealize;
widget_class->size_allocate = gtk_menu_size_allocate;
widget_class->show = gtk_menu_show;
widget_class->snapshot = gtk_menu_snapshot;
widget_class->focus = gtk_menu_focus;
widget_class->can_activate_accel = gtk_menu_real_can_activate_accel;
widget_class->grab_notify = gtk_menu_grab_notify;
widget_class->measure = gtk_menu_measure;
container_class->remove = gtk_menu_remove;
container_class->get_child_property = gtk_menu_get_child_property;
container_class->set_child_property = gtk_menu_set_child_property;
menu_shell_class->submenu_placement = GTK_LEFT_RIGHT;
menu_shell_class->deactivate = gtk_menu_deactivate;
menu_shell_class->select_item = gtk_menu_select_item;
menu_shell_class->insert = gtk_menu_real_insert;
menu_shell_class->get_popup_delay = gtk_menu_get_popup_delay;
menu_shell_class->move_current = gtk_menu_move_current;
/**
* GtkMenu::move-scroll:
* @menu: a #GtkMenu
* @scroll_type: a #GtkScrollType
*/
menu_signals[MOVE_SCROLL] =
g_signal_new_class_handler (I_("move-scroll"),
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_CALLBACK (gtk_menu_real_move_scroll),
NULL, NULL,
NULL,
G_TYPE_NONE, 1,
GTK_TYPE_SCROLL_TYPE);
/**
* GtkMenu::popped-up:
* @menu: the #GtkMenu that popped up
* @flipped_rect: (nullable): the position of @menu after any possible
* flipping or %NULL if the backend can't obtain it
* @final_rect: (nullable): the final position of @menu or %NULL if the
* backend can't obtain it
* @flipped_x: %TRUE if the anchors were flipped horizontally
* @flipped_y: %TRUE if the anchors were flipped vertically
*
* Emitted when the position of @menu is finalized after being popped up
* using gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (), or
* gtk_menu_popup_at_pointer ().
*
* @menu might be flipped over the anchor rectangle in order to keep it
* on-screen, in which case @flipped_x and @flipped_y will be set to %TRUE
* accordingly.
*
* @flipped_rect is the ideal position of @menu after any possible flipping,
* but before any possible sliding. @final_rect is @flipped_rect, but possibly
* translated in the case that flipping is still ineffective in keeping @menu
* on-screen.
*
* ![](popup-slide.png)
*
* The blue menu is @menu's ideal position, the green menu is @flipped_rect,
* and the red menu is @final_rect.
*
* See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (),
* gtk_menu_popup_at_pointer (), #GtkMenu:anchor-hints,
* #GtkMenu:rect-anchor-dx, #GtkMenu:rect-anchor-dy, and
* #GtkMenu:menu-type-hint.
*/
menu_signals[POPPED_UP] =
g_signal_new_class_handler (I_("popped-up"),
G_OBJECT_CLASS_TYPE (gobject_class),
G_SIGNAL_RUN_FIRST,
NULL,
NULL,
NULL,
_gtk_marshal_VOID__POINTER_POINTER_BOOLEAN_BOOLEAN,
G_TYPE_NONE,
4,
G_TYPE_POINTER,
G_TYPE_POINTER,
G_TYPE_BOOLEAN,
G_TYPE_BOOLEAN);
/**
* GtkMenu:active:
*
* The index of the currently selected menu item, or -1 if no
* menu item is selected.
**/
g_object_class_install_property (gobject_class,
PROP_ACTIVE,
g_param_spec_int ("active",
P_("Active"),
P_("The currently selected menu item"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE));
/**
* GtkMenu:accel-group:
*
* The accel group holding accelerators for the menu.
**/
g_object_class_install_property (gobject_class,
PROP_ACCEL_GROUP,
g_param_spec_object ("accel-group",
P_("Accel Group"),
P_("The accel group holding accelerators for the menu"),
GTK_TYPE_ACCEL_GROUP,
GTK_PARAM_READWRITE));
/**
* GtkMenu:accel-path:
*
* An accel path used to conveniently construct accel paths of child items.
**/
g_object_class_install_property (gobject_class,
PROP_ACCEL_PATH,
g_param_spec_string ("accel-path",
P_("Accel Path"),
P_("An accel path used to conveniently construct accel paths of child items"),
NULL,
GTK_PARAM_READWRITE));
/**
* GtkMenu:attach-widget:
*
* The widget the menu is attached to. Setting this property attaches
* the menu without a #GtkMenuDetachFunc. If you need to use a detacher,
* use gtk_menu_attach_to_widget() directly.
**/
g_object_class_install_property (gobject_class,
PROP_ATTACH_WIDGET,
g_param_spec_object ("attach-widget",
P_("Attach Widget"),
P_("The widget the menu is attached to"),
GTK_TYPE_WIDGET,
GTK_PARAM_READWRITE));
/**
* GtkMenu:monitor:
*
* The monitor the menu will be popped up on.
**/
g_object_class_install_property (gobject_class,
PROP_MONITOR,
g_param_spec_int ("monitor",
P_("Monitor"),
P_("The monitor the menu will be popped up on"),
-1, G_MAXINT, -1,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkMenu:reserve-toggle-size:
*
* A boolean that indicates whether the menu reserves space for
* toggles and icons, regardless of their actual presence.
*
* This property should only be changed from its default value
* for special-purposes such as tabular menus. Regular menus that
* are connected to a menu bar or context menus should reserve
* toggle space for consistency.
*/
g_object_class_install_property (gobject_class,
PROP_RESERVE_TOGGLE_SIZE,
g_param_spec_boolean ("reserve-toggle-size",
P_("Reserve Toggle Size"),
P_("A boolean that indicates whether the menu reserves space for toggles and icons"),
TRUE,
GTK_PARAM_READWRITE|G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkMenu:anchor-hints:
*
* Positioning hints for aligning the menu relative to a rectangle.
*
* These hints determine how the menu should be positioned in the case that
* the menu would fall off-screen if placed in its ideal position.
*
* ![](popup-flip.png)
*
* For example, %GDK_ANCHOR_FLIP_Y will replace %GDK_GRAVITY_NORTH_WEST with
* %GDK_GRAVITY_SOUTH_WEST and vice versa if the menu extends beyond the
* bottom edge of the monitor.
*
* See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (),
* gtk_menu_popup_at_pointer (), #GtkMenu:rect-anchor-dx,
* #GtkMenu:rect-anchor-dy, #GtkMenu:menu-type-hint, and #GtkMenu::popped-up.
*/
g_object_class_install_property (gobject_class,
PROP_ANCHOR_HINTS,
g_param_spec_flags ("anchor-hints",
P_("Anchor hints"),
P_("Positioning hints for when the menu might fall off-screen"),
GDK_TYPE_ANCHOR_HINTS,
GDK_ANCHOR_FLIP |
GDK_ANCHOR_SLIDE |
GDK_ANCHOR_RESIZE,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB |
G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkMenu:rect-anchor-dx:
*
* Horizontal offset to apply to the menu, i.e. the rectangle or widget
* anchor.
*
* See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (),
* gtk_menu_popup_at_pointer (), #GtkMenu:anchor-hints,
* #GtkMenu:rect-anchor-dy, #GtkMenu:menu-type-hint, and #GtkMenu::popped-up.
*/
g_object_class_install_property (gobject_class,
PROP_RECT_ANCHOR_DX,
g_param_spec_int ("rect-anchor-dx",
P_("Rect anchor dx"),
P_("Rect anchor horizontal offset"),
G_MININT,
G_MAXINT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB |
G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkMenu:rect-anchor-dy:
*
* Vertical offset to apply to the menu, i.e. the rectangle or widget anchor.
*
* See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (),
* gtk_menu_popup_at_pointer (), #GtkMenu:anchor-hints,
* #GtkMenu:rect-anchor-dx, #GtkMenu:menu-type-hint, and #GtkMenu::popped-up.
*/
g_object_class_install_property (gobject_class,
PROP_RECT_ANCHOR_DY,
g_param_spec_int ("rect-anchor-dy",
P_("Rect anchor dy"),
P_("Rect anchor vertical offset"),
G_MININT,
G_MAXINT,
0,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB |
G_PARAM_EXPLICIT_NOTIFY));
/**
* GtkMenu:menu-type-hint:
*
* The #GdkSurfaceTypeHint to use for the menu's #GdkSurface.
*
* See gtk_menu_popup_at_rect (), gtk_menu_popup_at_widget (),
* gtk_menu_popup_at_pointer (), #GtkMenu:anchor-hints,
* #GtkMenu:rect-anchor-dx, #GtkMenu:rect-anchor-dy, and #GtkMenu::popped-up.
*/
g_object_class_install_property (gobject_class,
PROP_MENU_TYPE_HINT,
g_param_spec_enum ("menu-type-hint",
P_("Menu type hint"),
P_("Menu window type hint"),
GDK_TYPE_SURFACE_TYPE_HINT,
GDK_SURFACE_TYPE_HINT_POPUP_MENU,
G_PARAM_READWRITE |
G_PARAM_CONSTRUCT |
G_PARAM_STATIC_NAME |
G_PARAM_STATIC_NICK |
G_PARAM_STATIC_BLURB |
G_PARAM_EXPLICIT_NOTIFY));
gtk_container_class_install_child_property (container_class,
CHILD_PROP_LEFT_ATTACH,
g_param_spec_int ("left-attach",
P_("Left Attach"),
P_("The column number to attach the left side of the child to"),
-1, INT_MAX, -1,
GTK_PARAM_READWRITE));
gtk_container_class_install_child_property (container_class,
CHILD_PROP_RIGHT_ATTACH,
g_param_spec_int ("right-attach",
P_("Right Attach"),
P_("The column number to attach the right side of the child to"),
-1, INT_MAX, -1,
GTK_PARAM_READWRITE));
gtk_container_class_install_child_property (container_class,
CHILD_PROP_TOP_ATTACH,
g_param_spec_int ("top-attach",
P_("Top Attach"),
P_("The row number to attach the top of the child to"),
-1, INT_MAX, -1,
GTK_PARAM_READWRITE));
gtk_container_class_install_child_property (container_class,
CHILD_PROP_BOTTOM_ATTACH,
g_param_spec_int ("bottom-attach",
P_("Bottom Attach"),
P_("The row number to attach the bottom of the child to"),
-1, INT_MAX, -1,
GTK_PARAM_READWRITE));
binding_set = gtk_binding_set_by_class (class);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Up, 0,
I_("move-current"), 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_PREV);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Up, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_PREV);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Down, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_NEXT);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Down, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_NEXT);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Left, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_PARENT);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Left, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_PARENT);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Right, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_CHILD);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Right, 0,
"move-current", 1,
GTK_TYPE_MENU_DIRECTION_TYPE,
GTK_MENU_DIR_CHILD);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Home, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_START);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Home, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_START);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_End, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_END);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_End, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_END);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Page_Up, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_PAGE_UP);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Page_Up, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_PAGE_UP);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_Page_Down, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_PAGE_DOWN);
gtk_binding_entry_add_signal (binding_set,
GDK_KEY_KP_Page_Down, 0,
"move-scroll", 1,
GTK_TYPE_SCROLL_TYPE,
GTK_SCROLL_PAGE_DOWN);
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_MENU_ACCESSIBLE);
gtk_widget_class_set_css_name (widget_class, I_("menu"));
}
static void
gtk_menu_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkMenu *menu = GTK_MENU (object);
switch (prop_id)
{
case PROP_ACTIVE:
gtk_menu_set_active (menu, g_value_get_int (value));
break;
case PROP_ACCEL_GROUP:
gtk_menu_set_accel_group (menu, g_value_get_object (value));
break;
case PROP_ACCEL_PATH:
gtk_menu_set_accel_path (menu, g_value_get_string (value));
break;
case PROP_ATTACH_WIDGET:
{
GtkWidget *widget;
widget = gtk_menu_get_attach_widget (menu);
if (widget)
gtk_menu_detach (menu);
widget = (GtkWidget*) g_value_get_object (value);
if (widget)
gtk_menu_attach_to_widget (menu, widget, NULL);
}
break;
case PROP_MONITOR:
gtk_menu_set_monitor (menu, g_value_get_int (value));
break;
case PROP_RESERVE_TOGGLE_SIZE:
gtk_menu_set_reserve_toggle_size (menu, g_value_get_boolean (value));
break;
case PROP_ANCHOR_HINTS:
if (menu->priv->anchor_hints != g_value_get_flags (value))
{
menu->priv->anchor_hints = g_value_get_flags (value);
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_RECT_ANCHOR_DX:
if (menu->priv->rect_anchor_dx != g_value_get_int (value))
{
menu->priv->rect_anchor_dx = g_value_get_int (value);
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_RECT_ANCHOR_DY:
if (menu->priv->rect_anchor_dy != g_value_get_int (value))
{
menu->priv->rect_anchor_dy = g_value_get_int (value);
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_MENU_TYPE_HINT:
if (menu->priv->menu_type_hint != g_value_get_enum (value))
{
menu->priv->menu_type_hint = g_value_get_enum (value);
g_object_notify_by_pspec (object, pspec);
}
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_menu_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkMenu *menu = GTK_MENU (object);
switch (prop_id)
{
case PROP_ACTIVE:
g_value_set_int (value, g_list_index (GTK_MENU_SHELL (menu)->priv->children, gtk_menu_get_active (menu)));
break;
case PROP_ACCEL_GROUP:
g_value_set_object (value, gtk_menu_get_accel_group (menu));
break;
case PROP_ACCEL_PATH:
g_value_set_string (value, gtk_menu_get_accel_path (menu));
break;
case PROP_ATTACH_WIDGET:
g_value_set_object (value, gtk_menu_get_attach_widget (menu));
break;
case PROP_MONITOR:
g_value_set_int (value, gtk_menu_get_monitor (menu));
break;
case PROP_RESERVE_TOGGLE_SIZE:
g_value_set_boolean (value, gtk_menu_get_reserve_toggle_size (menu));
break;
case PROP_ANCHOR_HINTS:
g_value_set_flags (value, menu->priv->anchor_hints);
break;
case PROP_RECT_ANCHOR_DX:
g_value_set_int (value, menu->priv->rect_anchor_dx);
break;
case PROP_RECT_ANCHOR_DY:
g_value_set_int (value, menu->priv->rect_anchor_dy);
break;
case PROP_MENU_TYPE_HINT:
g_value_set_enum (value, menu->priv->menu_type_hint);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_menu_set_child_property (GtkContainer *container,
GtkWidget *child,
guint property_id,
const GValue *value,
GParamSpec *pspec)
{
GtkMenu *menu = GTK_MENU (container);
AttachInfo *ai = get_attach_info (child);
switch (property_id)
{
case CHILD_PROP_LEFT_ATTACH:
ai->left_attach = g_value_get_int (value);
break;
case CHILD_PROP_RIGHT_ATTACH:
ai->right_attach = g_value_get_int (value);
break;
case CHILD_PROP_TOP_ATTACH:
ai->top_attach = g_value_get_int (value);
break;
case CHILD_PROP_BOTTOM_ATTACH:
ai->bottom_attach = g_value_get_int (value);
break;
default:
GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
return;
}
menu_queue_resize (menu);
}
static void
gtk_menu_get_child_property (GtkContainer *container,
GtkWidget *child,
guint property_id,
GValue *value,
GParamSpec *pspec)
{
AttachInfo *ai = get_attach_info (child);
switch (property_id)
{
case CHILD_PROP_LEFT_ATTACH:
g_value_set_int (value, ai->left_attach);
break;
case CHILD_PROP_RIGHT_ATTACH:
g_value_set_int (value, ai->right_attach);
break;
case CHILD_PROP_TOP_ATTACH:
g_value_set_int (value, ai->top_attach);
break;
case CHILD_PROP_BOTTOM_ATTACH:
g_value_set_int (value, ai->bottom_attach);
break;
default:
GTK_CONTAINER_WARN_INVALID_CHILD_PROPERTY_ID (container, property_id, pspec);
return;
}
}
static void
gtk_menu_init (GtkMenu *menu)
{
GtkMenuPrivate *priv;
GtkGesture *gesture;
priv = gtk_menu_get_instance_private (menu);
menu->priv = priv;
priv->toplevel = gtk_window_new (GTK_WINDOW_POPUP);
gtk_container_add (GTK_CONTAINER (priv->toplevel), GTK_WIDGET (menu));
g_signal_connect (priv->toplevel, "destroy", G_CALLBACK (gtk_widget_destroyed), &priv->toplevel);
gtk_window_set_resizable (GTK_WINDOW (priv->toplevel), FALSE);
gtk_window_set_mnemonic_modifier (GTK_WINDOW (priv->toplevel), 0);
_gtk_window_request_csd (GTK_WINDOW (priv->toplevel));
gtk_style_context_add_class (gtk_widget_get_style_context (priv->toplevel),
GTK_STYLE_CLASS_POPUP);
/* Refloat the menu, so that reference counting for the menu isn't
* affected by it being a child of the toplevel
*/
g_object_force_floating (G_OBJECT (menu));
priv->needs_destruction_ref = TRUE;
priv->monitor_num = -1;
priv->drag_start_y = -1;
priv->anchor_hints = GDK_ANCHOR_FLIP | GDK_ANCHOR_SLIDE | GDK_ANCHOR_RESIZE;
priv->rect_anchor_dx = 0;
priv->rect_anchor_dy = 0;
priv->menu_type_hint = GDK_SURFACE_TYPE_HINT_POPUP_MENU;
_gtk_widget_set_captured_event_handler (GTK_WIDGET (menu), gtk_menu_captured_event);
priv->top_arrow_widget = gtk_icon_new ("arrow");
gtk_style_context_add_class (gtk_widget_get_style_context (priv->top_arrow_widget),
GTK_STYLE_CLASS_TOP);
gtk_widget_set_parent (priv->top_arrow_widget, GTK_WIDGET (menu));
gtk_widget_hide (priv->top_arrow_widget);
priv->bottom_arrow_widget = gtk_icon_new ("arrow");
gtk_style_context_add_class (gtk_widget_get_style_context (priv->bottom_arrow_widget),
GTK_STYLE_CLASS_BOTTOM);
gtk_widget_set_parent (priv->bottom_arrow_widget, GTK_WIDGET (menu));
gtk_widget_hide (priv->bottom_arrow_widget);
gesture = gtk_gesture_multi_press_new ();
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (gesture), FALSE);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (gesture), 0);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (gesture), GTK_PHASE_BUBBLE);
g_signal_connect (gesture, "pressed", G_CALLBACK (gtk_menu_pressed_cb), menu);
g_signal_connect (gesture, "released", G_CALLBACK (gtk_menu_released_cb), menu);
gtk_widget_add_controller (GTK_WIDGET (menu), GTK_EVENT_CONTROLLER (gesture));
priv->scroll_controller =
gtk_event_controller_scroll_new (GTK_WIDGET (menu),
GTK_EVENT_CONTROLLER_SCROLL_VERTICAL);
g_signal_connect (priv->scroll_controller, "scroll",
G_CALLBACK (gtk_menu_scroll_controller_scroll), menu);
priv->motion_controller =
gtk_event_controller_motion_new (GTK_WIDGET (menu));
g_signal_connect (priv->motion_controller, "enter", G_CALLBACK (gtk_menu_enter), menu);
g_signal_connect (priv->motion_controller, "motion", G_CALLBACK (gtk_menu_motion), menu);
g_signal_connect (priv->motion_controller, "leave", G_CALLBACK (gtk_menu_leave), menu);
priv->key_controller =
gtk_event_controller_key_new (GTK_WIDGET (menu));
g_signal_connect (priv->key_controller, "key-pressed",
G_CALLBACK (gtk_menu_key_pressed), menu);
}
static void
moved_to_rect_cb (GdkSurface *surface,
const GdkRectangle *flipped_rect,
const GdkRectangle *final_rect,
gboolean flipped_x,
gboolean flipped_y,
GtkMenu *menu)
{
g_signal_emit (menu,
menu_signals[POPPED_UP],
0,
flipped_rect,
final_rect,
flipped_x,
flipped_y);
}
static void
gtk_menu_destroy (GtkWidget *widget)
{
GtkMenu *menu = GTK_MENU (widget);
GtkMenuPrivate *priv = menu->priv;
GtkMenuAttachData *data;
gtk_menu_remove_scroll_timeout (menu);
data = g_object_get_data (G_OBJECT (widget), attach_data_key);
if (data)
gtk_menu_detach (menu);
gtk_menu_stop_navigating_submenu (menu);
g_clear_object (&priv->old_active_menu_item);
/* Add back the reference count for being a child */
if (priv->needs_destruction_ref)
{
priv->needs_destruction_ref = FALSE;
g_object_ref (widget);
}
g_clear_object (&priv->accel_group);
if (priv->toplevel)
{
g_signal_handlers_disconnect_by_func (priv->toplevel, moved_to_rect_cb, menu);
gtk_widget_destroy (priv->toplevel);
}
g_clear_pointer (&priv->heights, g_free);
if (priv->position_func_data_destroy)
{
priv->position_func_data_destroy (priv->position_func_data);
priv->position_func_data = NULL;
priv->position_func_data_destroy = NULL;
}
GTK_WIDGET_CLASS (gtk_menu_parent_class)->destroy (widget);
}
static void
gtk_menu_finalize (GObject *object)
{
GtkMenu *menu = GTK_MENU (object);
GtkMenuPrivate *priv = menu->priv;
gtk_widget_unparent (priv->top_arrow_widget);
gtk_widget_unparent (priv->bottom_arrow_widget);
g_clear_object (&priv->scroll_controller);
g_clear_object (&priv->motion_controller);
g_clear_object (&priv->key_controller);
G_OBJECT_CLASS (gtk_menu_parent_class)->finalize (object);
}
static void
menu_change_display (GtkMenu *menu,
GdkDisplay *new_display)
{
GtkMenuPrivate *priv = menu->priv;
if (new_display == gtk_widget_get_display (GTK_WIDGET (menu)))
return;
gtk_window_set_display (GTK_WINDOW (priv->toplevel), new_display);
priv->monitor_num = -1;
}
static void
attach_widget_display_changed (GtkWidget *attach_widget,
GdkDisplay *previous_display,
GtkMenu *menu)
{
if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-display"))
menu_change_display (menu, gtk_widget_get_display (attach_widget));
}
static void
menu_toplevel_attached_to (GtkWindow *toplevel, GParamSpec *pspec, GtkMenu *menu)
{
GtkMenuAttachData *data;
data = g_object_get_data (G_OBJECT (menu), attach_data_key);
g_return_if_fail (data);
gtk_menu_detach (menu);
}
/**
* gtk_menu_attach_to_widget:
* @menu: a #GtkMenu
* @attach_widget: the #GtkWidget that the menu will be attached to
* @detacher: (scope async)(allow-none): the user supplied callback function
* that will be called when the menu calls gtk_menu_detach()
*
* Attaches the menu to the widget and provides a callback function
* that will be invoked when the menu calls gtk_menu_detach() during
* its destruction.
*
* If the menu is attached to the widget then it will be destroyed
* when the widget is destroyed, as if it was a child widget.
* An attached menu will also move between screens correctly if the
* widgets moves between screens.
*/
void
gtk_menu_attach_to_widget (GtkMenu *menu,
GtkWidget *attach_widget,
GtkMenuDetachFunc detacher)
{
GtkMenuAttachData *data;
GList *list;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GTK_IS_WIDGET (attach_widget));
/* keep this function in sync with gtk_widget_set_parent() */
data = g_object_get_data (G_OBJECT (menu), attach_data_key);
if (data)
{
g_warning ("gtk_menu_attach_to_widget(): menu already attached to %s",
g_type_name (G_TYPE_FROM_INSTANCE (data->attach_widget)));
return;
}
g_object_ref_sink (menu);
data = g_slice_new (GtkMenuAttachData);
data->attach_widget = attach_widget;
g_signal_connect (attach_widget, "display-changed",
G_CALLBACK (attach_widget_display_changed), menu);
attach_widget_display_changed (attach_widget, NULL, menu);
data->detacher = detacher;
g_object_set_data (G_OBJECT (menu), I_(attach_data_key), data);
list = g_object_steal_data (G_OBJECT (attach_widget), ATTACHED_MENUS);
if (!g_list_find (list, menu))
list = g_list_prepend (list, menu);
g_object_set_data_full (G_OBJECT (attach_widget), I_(ATTACHED_MENUS), list,
(GDestroyNotify) g_list_free);
/* Attach the widget to the toplevel window. */
gtk_window_set_attached_to (GTK_WINDOW (menu->priv->toplevel), attach_widget);
g_signal_connect (GTK_WINDOW (menu->priv->toplevel), "notify::attached-to",
G_CALLBACK (menu_toplevel_attached_to), menu);
_gtk_widget_update_parent_muxer (GTK_WIDGET (menu));
g_object_notify (G_OBJECT (menu), "attach-widget");
}
/**
* gtk_menu_get_attach_widget:
* @menu: a #GtkMenu
*
* Returns the #GtkWidget that the menu is attached to.
*
* Returns: (transfer none): the #GtkWidget that the menu is attached to
*/
GtkWidget*
gtk_menu_get_attach_widget (GtkMenu *menu)
{
GtkMenuAttachData *data;
g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
data = g_object_get_data (G_OBJECT (menu), attach_data_key);
if (data)
return data->attach_widget;
return NULL;
}
/**
* gtk_menu_detach:
* @menu: a #GtkMenu
*
* Detaches the menu from the widget to which it had been attached.
* This function will call the callback function, @detacher, provided
* when the gtk_menu_attach_to_widget() function was called.
*/
void
gtk_menu_detach (GtkMenu *menu)
{
GtkWindow *toplevel;
GtkMenuAttachData *data;
GList *list;
g_return_if_fail (GTK_IS_MENU (menu));
toplevel = GTK_WINDOW (menu->priv->toplevel);
/* keep this function in sync with gtk_widget_unparent() */
data = g_object_get_data (G_OBJECT (menu), attach_data_key);
if (!data)
{
g_warning ("gtk_menu_detach(): menu is not attached");
return;
}
g_object_set_data (G_OBJECT (menu), I_(attach_data_key), NULL);
/* Detach the toplevel window. */
if (toplevel)
{
g_signal_handlers_disconnect_by_func (toplevel,
(gpointer) menu_toplevel_attached_to,
menu);
if (gtk_window_get_attached_to (toplevel) == data->attach_widget)
gtk_window_set_attached_to (toplevel, NULL);
}
g_signal_handlers_disconnect_by_func (data->attach_widget,
(gpointer) attach_widget_display_changed,
menu);
if (data->detacher)
data->detacher (data->attach_widget, menu);
list = g_object_steal_data (G_OBJECT (data->attach_widget), ATTACHED_MENUS);
list = g_list_remove (list, menu);
if (list)
g_object_set_data_full (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), list,
(GDestroyNotify) g_list_free);
else
g_object_set_data (G_OBJECT (data->attach_widget), I_(ATTACHED_MENUS), NULL);
if (gtk_widget_get_realized (GTK_WIDGET (menu)))
gtk_widget_unrealize (GTK_WIDGET (menu));
g_slice_free (GtkMenuAttachData, data);
_gtk_widget_update_parent_muxer (GTK_WIDGET (menu));
g_object_notify (G_OBJECT (menu), "attach-widget");
g_object_unref (menu);
}
static void
gtk_menu_remove (GtkContainer *container,
GtkWidget *widget)
{
GtkMenu *menu = GTK_MENU (container);
GtkMenuPrivate *priv = menu->priv;
/* Clear out old_active_menu_item if it matches the item we are removing */
if (priv->old_active_menu_item == widget)
g_clear_object (&priv->old_active_menu_item);
GTK_CONTAINER_CLASS (gtk_menu_parent_class)->remove (container, widget);
g_object_set_data (G_OBJECT (widget), I_(ATTACH_INFO_KEY), NULL);
menu_queue_resize (menu);
}
/**
* gtk_menu_new:
*
* Creates a new #GtkMenu
*
* Returns: a new #GtkMenu
*/
GtkWidget*
gtk_menu_new (void)
{
return g_object_new (GTK_TYPE_MENU, NULL);
}
static void
gtk_menu_real_insert (GtkMenuShell *menu_shell,
GtkWidget *child,
gint position)
{
GtkMenu *menu = GTK_MENU (menu_shell);
GtkMenuPrivate *priv = menu->priv;
AttachInfo *ai = get_attach_info (child);
ai->left_attach = -1;
ai->right_attach = -1;
ai->top_attach = -1;
ai->bottom_attach = -1;
gtk_widget_insert_before (child, GTK_WIDGET (menu), priv->bottom_arrow_widget);
GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->insert (menu_shell, child, position);
menu_queue_resize (menu);
}
static gboolean
popup_grab_on_surface (GdkSurface *surface,
GdkDevice *pointer)
{
GdkGrabStatus status;
GdkSeat *seat;
seat = gdk_device_get_seat (pointer);
status = gdk_seat_grab (seat, surface,
GDK_SEAT_CAPABILITY_ALL, TRUE,
NULL, NULL, NULL, NULL);
return status == GDK_GRAB_SUCCESS;
}
static void
associate_menu_grab_transfer_surface (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
GdkSurface *toplevel_surface;
GdkSurface *transfer_surface;
toplevel_surface = gtk_widget_get_surface (priv->toplevel);
transfer_surface = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-surface");
if (toplevel_surface == NULL || transfer_surface == NULL)
return;
g_object_set_data (G_OBJECT (toplevel_surface), I_("gdk-attached-grab-surface"), transfer_surface);
}
static void
gtk_menu_popup_internal (GtkMenu *menu,
GdkDevice *device,
GtkWidget *parent_menu_shell,
GtkWidget *parent_menu_item,
GtkMenuPositionFunc func,
gpointer data,
GDestroyNotify destroy,
guint button,
guint32 activate_time)
{
GtkMenuPrivate *priv = menu->priv;
GtkWidget *widget;
GtkWidget *xgrab_shell;
GtkWidget *parent;
GdkEvent *current_event;
GtkMenuShell *menu_shell;
gboolean grab_keyboard;
GtkWidget *parent_toplevel;
GdkDevice *pointer, *source_device = NULL;
GdkDisplay *display;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (device == NULL || GDK_IS_DEVICE (device));
display = gtk_widget_get_display (GTK_WIDGET (menu));
if (device == NULL)
device = gtk_get_current_event_device ();
if (device && gdk_device_get_display (device) != display)
device = NULL;
if (device == NULL)
device = gdk_seat_get_pointer (gdk_display_get_default_seat (display));
widget = GTK_WIDGET (menu);
menu_shell = GTK_MENU_SHELL (menu);
if (gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
pointer = gdk_device_get_associated_device (device);
else
pointer = device;
menu_shell->priv->parent_menu_shell = parent_menu_shell;
priv->seen_item_enter = FALSE;
/* Find the last viewable ancestor, and make an X grab on it
*/
parent = GTK_WIDGET (menu);
xgrab_shell = NULL;
while (parent)
{
gboolean viewable = TRUE;
GtkWidget *tmp = parent;
while (tmp)
{
if (!gtk_widget_get_mapped (tmp))
{
viewable = FALSE;
break;
}
tmp = gtk_widget_get_parent (tmp);
}
if (viewable)
xgrab_shell = parent;
parent = GTK_MENU_SHELL (parent)->priv->parent_menu_shell;
}
/* We want to receive events generated when we map the menu;
* unfortunately, since there is probably already an implicit
* grab in place from the button that the user used to pop up
* the menu, we won't receive then -- in particular, the EnterNotify
* when the menu pops up under the pointer.
*
* If we are grabbing on a parent menu shell, no problem; just grab
* on that menu shell first before popping up the window with
* owner_events = TRUE.
*
* When grabbing on the menu itself, things get more convoluted --
* we do an explicit grab on a specially created window with
* owner_events = TRUE, which we override further down with a
* grab on the menu. (We can't grab on the menu until it is mapped;
* we probably could just leave the grab on the other window,
* with a little reorganization of the code in gtkmenu*).
*/
grab_keyboard = gtk_menu_shell_get_take_focus (menu_shell);
gtk_window_set_accept_focus (GTK_WINDOW (priv->toplevel), grab_keyboard);
if (xgrab_shell && xgrab_shell != widget)
{
if (popup_grab_on_surface (gtk_widget_get_surface (xgrab_shell), pointer))
{
_gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
}
}
else
{
GdkSurface *transfer_surface;
xgrab_shell = widget;
transfer_surface = menu_grab_transfer_surface_get (menu);
if (popup_grab_on_surface (transfer_surface, pointer))
{
_gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (xgrab_shell), pointer);
GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab = TRUE;
}
}
if (!GTK_MENU_SHELL (xgrab_shell)->priv->have_xgrab)
{
/* We failed to make our pointer/keyboard grab.
* Rather than leaving the user with a stuck up window,
* we just abort here. Presumably the user will try again.
*/
menu_shell->priv->parent_menu_shell = NULL;
menu_grab_transfer_surface_destroy (menu);
return;
}
_gtk_menu_shell_set_grab_device (GTK_MENU_SHELL (menu), pointer);
menu_shell->priv->active = TRUE;
menu_shell->priv->button = button;
/* If we are popping up the menu from something other than, a button
* press then, as a heuristic, we ignore enter events for the menu
* until we get a MOTION_NOTIFY.
*/
current_event = gtk_get_current_event ();
if (current_event)
{
GdkEventType event_type = gdk_event_get_event_type (current_event);
if ((event_type != GDK_BUTTON_PRESS) &&
(event_type != GDK_ENTER_NOTIFY))
menu_shell->priv->ignore_enter = TRUE;
source_device = gdk_event_get_source_device (current_event);
g_object_unref (current_event);
}
else
menu_shell->priv->ignore_enter = TRUE;
parent_toplevel = NULL;
if (parent_menu_shell)
parent_toplevel = gtk_widget_get_toplevel (parent_menu_shell);
else if (!g_object_get_data (G_OBJECT (menu), "gtk-menu-explicit-display"))
{
GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
if (attach_widget)
parent_toplevel = gtk_widget_get_toplevel (attach_widget);
}
/* Set transient for to get the right window group and parent */
if (GTK_IS_WINDOW (parent_toplevel))
gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel),
GTK_WINDOW (parent_toplevel));
priv->parent_menu_item = parent_menu_item;
priv->position_func = func;
priv->position_func_data = data;
priv->position_func_data_destroy = destroy;
menu_shell->priv->activate_time = activate_time;
/* We need to show the menu here rather in the init function
* because code expects to be able to tell if the menu is onscreen
* by looking at gtk_widget_get_visible (menu)
*/
gtk_widget_show (GTK_WIDGET (menu));
/* Position the menu, possibly changing the size request
*/
gtk_menu_position (menu, TRUE);
associate_menu_grab_transfer_surface (menu);
gtk_menu_scroll_to (menu, priv->scroll_offset);
/* if no item is selected, select the first one */
if (!menu_shell->priv->active_menu_item &&
source_device && gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
gtk_menu_shell_select_first (menu_shell, TRUE);
/* Once everything is set up correctly, map the toplevel */
gtk_widget_show (priv->toplevel);
if (xgrab_shell == widget)
popup_grab_on_surface (gtk_widget_get_surface (widget), pointer); /* Should always succeed */
gtk_grab_add (GTK_WIDGET (menu));
if (parent_menu_shell)
{
gboolean keyboard_mode;
keyboard_mode = _gtk_menu_shell_get_keyboard_mode (GTK_MENU_SHELL (parent_menu_shell));
_gtk_menu_shell_set_keyboard_mode (menu_shell, keyboard_mode);
}
else if (menu_shell->priv->button == 0) /* a keynav-activated context menu */
_gtk_menu_shell_set_keyboard_mode (menu_shell, TRUE);
_gtk_menu_shell_update_mnemonics (menu_shell);
}
/**
* gtk_menu_popup_for_device:
* @menu: a #GtkMenu
* @device: (allow-none): a #GdkDevice
* @parent_menu_shell: (allow-none): the menu shell containing the triggering
* menu item, or %NULL
* @parent_menu_item: (allow-none): the menu item whose activation triggered
* the popup, or %NULL
* @func: (allow-none): a user supplied function used to position the menu,
* or %NULL
* @data: (allow-none): user supplied data to be passed to @func
* @destroy: (allow-none): destroy notify for @data
* @button: the mouse button which was pressed to initiate the event
* @activate_time: the time at which the activation event occurred
*
* Displays a menu and makes it available for selection.
*
* Applications can use this function to display context-sensitive menus,
* and will typically supply %NULL for the @parent_menu_shell,
* @parent_menu_item, @func, @data and @destroy parameters. The default
* menu positioning function will position the menu at the current position
* of @device (or its corresponding pointer).
*
* The @button parameter should be the mouse button pressed to initiate
* the menu popup. If the menu popup was initiated by something other than
* a mouse button press, such as a mouse button release or a keypress,
* @button should be 0.
*
* The @activate_time parameter is used to conflict-resolve initiation of
* concurrent requests for mouse/keyboard grab requests. To function
* properly, this needs to be the time stamp of the user event (such as
* a mouse click or key press) that caused the initiation of the popup.
* Only if no such event is available, gtk_get_current_event_time() can
* be used instead.
*/
void
gtk_menu_popup_for_device (GtkMenu *menu,
GdkDevice *device,
GtkWidget *parent_menu_shell,
GtkWidget *parent_menu_item,
GtkMenuPositionFunc func,
gpointer data,
GDestroyNotify destroy,
guint button,
guint32 activate_time)
{
GtkMenuPrivate *priv;
g_return_if_fail (GTK_IS_MENU (menu));
priv = menu->priv;
priv->rect_surface = NULL;
priv->widget = NULL;
gtk_menu_popup_internal (menu,
device,
parent_menu_shell,
parent_menu_item,
func,
data,
destroy,
button,
activate_time);
}
/**
* gtk_menu_popup:
* @menu: a #GtkMenu
* @parent_menu_shell: (allow-none): the menu shell containing the
* triggering menu item, or %NULL
* @parent_menu_item: (allow-none): the menu item whose activation
* triggered the popup, or %NULL
* @func: (scope async) (allow-none): a user supplied function used to position
* the menu, or %NULL
* @data: user supplied data to be passed to @func.
* @button: the mouse button which was pressed to initiate the event.
* @activate_time: the time at which the activation event occurred.
*
* Displays a menu and makes it available for selection.
*
* Applications can use this function to display context-sensitive
* menus, and will typically supply %NULL for the @parent_menu_shell,
* @parent_menu_item, @func and @data parameters. The default menu
* positioning function will position the menu at the current mouse
* cursor position.
*
* The @button parameter should be the mouse button pressed to initiate
* the menu popup. If the menu popup was initiated by something other
* than a mouse button press, such as a mouse button release or a keypress,
* @button should be 0.
*
* The @activate_time parameter is used to conflict-resolve initiation
* of concurrent requests for mouse/keyboard grab requests. To function
* properly, this needs to be the timestamp of the user event (such as
* a mouse click or key press) that caused the initiation of the popup.
* Only if no such event is available, gtk_get_current_event_time() can
* be used instead.
*/
void
gtk_menu_popup (GtkMenu *menu,
GtkWidget *parent_menu_shell,
GtkWidget *parent_menu_item,
GtkMenuPositionFunc func,
gpointer data,
guint button,
guint32 activate_time)
{
g_return_if_fail (GTK_IS_MENU (menu));
gtk_menu_popup_for_device (menu,
NULL,
parent_menu_shell,
parent_menu_item,
func, data, NULL,
button, activate_time);
}
static GdkDevice *
get_device_for_event (const GdkEvent *event)
{
GdkDevice *device = NULL;
GdkSeat *seat = NULL;
GdkDisplay *display = NULL;
device = gdk_event_get_device (event);
if (device)
return device;
seat = gdk_event_get_seat (event);
if (!seat)
{
display = gdk_event_get_display (event);
if (!display)
{
g_warning ("no display for event, using default");
display = gdk_display_get_default ();
}
if (display)
seat = gdk_display_get_default_seat (display);
}
return seat ? gdk_seat_get_pointer (seat) : NULL;
}
/**
* gtk_menu_popup_at_rect:
* @menu: the #GtkMenu to pop up
* @rect_surface: (not nullable): the #GdkSurface @rect is relative to
* @rect: (not nullable): the #GdkRectangle to align @menu with
* @rect_anchor: the point on @rect to align with @menu's anchor point
* @menu_anchor: the point on @menu to align with @rect's anchor point
* @trigger_event: (nullable): the #GdkEvent that initiated this request or
* %NULL if it's the current event
*
* Displays @menu and makes it available for selection.
*
* See gtk_menu_popup_at_widget () and gtk_menu_popup_at_pointer (), which
* handle more common cases for popping up menus.
*
* @menu will be positioned at @rect, aligning their anchor points. @rect is
* relative to the top-left corner of @rect_surface. @rect_anchor and
* @menu_anchor determine anchor points on @rect and @menu to pin together.
* @menu can optionally be offset by #GtkMenu:rect-anchor-dx and
* #GtkMenu:rect-anchor-dy.
*
* Anchors should be specified under the assumption that the text direction is
* left-to-right; they will be flipped horizontally automatically if the text
* direction is right-to-left.
*
* Other properties that influence the behaviour of this function are
* #GtkMenu:anchor-hints and #GtkMenu:menu-type-hint. Connect to the
* #GtkMenu::popped-up signal to find out how it was actually positioned.
*/
void
gtk_menu_popup_at_rect (GtkMenu *menu,
GdkSurface *rect_surface,
const GdkRectangle *rect,
GdkGravity rect_anchor,
GdkGravity menu_anchor,
const GdkEvent *trigger_event)
{
GtkMenuPrivate *priv;
GdkEvent *current_event = NULL;
GdkDevice *device = NULL;
guint button = 0;
guint32 activate_time = GDK_CURRENT_TIME;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GDK_IS_SURFACE (rect_surface));
g_return_if_fail (rect);
priv = menu->priv;
priv->rect_surface = rect_surface;
priv->rect = *rect;
priv->widget = NULL;
priv->rect_anchor = rect_anchor;
priv->menu_anchor = menu_anchor;
if (!trigger_event)
{
current_event = gtk_get_current_event ();
trigger_event = current_event;
}
if (trigger_event)
{
device = get_device_for_event (trigger_event);
gdk_event_get_button (trigger_event, &button);
activate_time = gdk_event_get_time (trigger_event);
}
else
g_warning ("no trigger event for menu popup");
gtk_menu_popup_internal (menu,
device,
NULL,
NULL,
NULL,
NULL,
NULL,
button,
activate_time);
g_clear_object (¤t_event);
}
/**
* gtk_menu_popup_at_widget:
* @menu: the #GtkMenu to pop up
* @widget: (not nullable): the #GtkWidget to align @menu with
* @widget_anchor: the point on @widget to align with @menu's anchor point
* @menu_anchor: the point on @menu to align with @widget's anchor point
* @trigger_event: (nullable): the #GdkEvent that initiated this request or
* %NULL if it's the current event
*
* Displays @menu and makes it available for selection.
*
* See gtk_menu_popup_at_pointer () to pop up a menu at the master pointer.
* gtk_menu_popup_at_rect () also allows you to position a menu at an arbitrary
* rectangle.
*
* ![](popup-anchors.png)
*
* @menu will be positioned at @widget, aligning their anchor points.
* @widget_anchor and @menu_anchor determine anchor points on @widget and @menu
* to pin together. @menu can optionally be offset by #GtkMenu:rect-anchor-dx
* and #GtkMenu:rect-anchor-dy.
*
* Anchors should be specified under the assumption that the text direction is
* left-to-right; they will be flipped horizontally automatically if the text
* direction is right-to-left.
*
* Other properties that influence the behaviour of this function are
* #GtkMenu:anchor-hints and #GtkMenu:menu-type-hint. Connect to the
* #GtkMenu::popped-up signal to find out how it was actually positioned.
*/
void
gtk_menu_popup_at_widget (GtkMenu *menu,
GtkWidget *widget,
GdkGravity widget_anchor,
GdkGravity menu_anchor,
const GdkEvent *trigger_event)
{
GtkMenuPrivate *priv;
GdkEvent *current_event = NULL;
GdkDevice *device = NULL;
guint button = 0;
guint32 activate_time = GDK_CURRENT_TIME;
GtkWidget *parent_menu_shell = NULL;
GtkWidget *parent_menu_item = NULL;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GTK_IS_WIDGET (widget));
priv = menu->priv;
priv->rect_surface = NULL;
priv->widget = widget;
priv->rect_anchor = widget_anchor;
priv->menu_anchor = menu_anchor;
if (!trigger_event)
{
current_event = gtk_get_current_event ();
trigger_event = current_event;
}
if (trigger_event)
{
device = get_device_for_event (trigger_event);
gdk_event_get_button (trigger_event, &button);
activate_time = gdk_event_get_time (trigger_event);
}
else
g_warning ("no trigger event for menu popup");
if (GTK_IS_MENU_ITEM (priv->widget))
{
parent_menu_item = priv->widget;
if (GTK_IS_MENU_SHELL (gtk_widget_get_parent (parent_menu_item)))
parent_menu_shell = gtk_widget_get_parent (parent_menu_item);
}
gtk_menu_popup_internal (menu,
device,
parent_menu_shell,
parent_menu_item,
NULL,
NULL,
NULL,
button,
activate_time);
g_clear_object (¤t_event);
}
/**
* gtk_menu_popup_at_pointer:
* @menu: the #GtkMenu to pop up
* @trigger_event: (nullable): the #GdkEvent that initiated this request or
* %NULL if it's the current event
*
* Displays @menu and makes it available for selection.
*
* See gtk_menu_popup_at_widget () to pop up a menu at a widget.
* gtk_menu_popup_at_rect () also allows you to position a menu at an arbitrary
* rectangle.
*
* @menu will be positioned at the pointer associated with @trigger_event.
*
* Properties that influence the behaviour of this function are
* #GtkMenu:anchor-hints, #GtkMenu:rect-anchor-dx, #GtkMenu:rect-anchor-dy, and
* #GtkMenu:menu-type-hint. Connect to the #GtkMenu::popped-up signal to find
* out how it was actually positioned.
*/
void
gtk_menu_popup_at_pointer (GtkMenu *menu,
const GdkEvent *trigger_event)
{
GdkEvent *current_event = NULL;
GdkSurface *rect_surface = NULL;
GdkDevice *device = NULL;
GdkRectangle rect = { 0, 0, 1, 1 };
g_return_if_fail (GTK_IS_MENU (menu));
if (!trigger_event)
{
current_event = gtk_get_current_event ();
trigger_event = current_event;
}
if (trigger_event)
{
rect_surface = gdk_event_get_surface (trigger_event);
if (rect_surface)
{
device = get_device_for_event (trigger_event);
if (device && gdk_device_get_source (device) == GDK_SOURCE_KEYBOARD)
device = gdk_device_get_associated_device (device);
if (device)
gdk_surface_get_device_position (rect_surface, device, &rect.x, &rect.y, NULL);
}
}
else
g_warning ("no trigger event for menu popup");
gtk_menu_popup_at_rect (menu,
rect_surface,
&rect,
GDK_GRAVITY_SOUTH_EAST,
GDK_GRAVITY_NORTH_WEST,
trigger_event);
g_clear_object (¤t_event);
}
static void
get_arrows_border (GtkMenu *menu,
GtkBorder *border)
{
GtkMenuPrivate *priv = menu->priv;
gint top_arrow_height, bottom_arrow_height;
gtk_widget_measure (priv->top_arrow_widget,
GTK_ORIENTATION_VERTICAL,
-1,
&top_arrow_height, NULL,
NULL, NULL);
gtk_widget_measure (priv->bottom_arrow_widget,
GTK_ORIENTATION_VERTICAL,
-1,
&bottom_arrow_height, NULL,
NULL, NULL);
border->top = priv->upper_arrow_visible ? top_arrow_height : 0;
border->bottom = priv->lower_arrow_visible ? bottom_arrow_height : 0;
border->left = border->right = 0;
}
/**
* gtk_menu_update_scroll_offset:
* @menu: the #GtkMenu that popped up
* @flipped_rect: (nullable): the position of @menu after any possible flipping
* or %NULL if unknown
* @final_rect: (nullable): the final position of @menu or %NULL if unknown
* @flipped_x: %TRUE if the anchors were flipped horizontally
* @flipped_y: %TRUE if the anchors were flipped vertically
* @user_data: user data
*
* Updates the scroll offset of @menu based on the amount of sliding done while
* positioning @menu. Connect this to the #GtkMenu::popped-up signal to keep the
* contents of the menu vertically aligned with their ideal position, for combo
* boxes for example.
*/
void
gtk_menu_update_scroll_offset (GtkMenu *menu,
const GdkRectangle *flipped_rect,
const GdkRectangle *final_rect,
gboolean flipped_x,
gboolean flipped_y,
gpointer user_data)
{
GtkBorder arrows_border;
g_return_if_fail (GTK_IS_MENU (menu));
if (!flipped_rect || !final_rect)
return;
get_arrows_border (menu, &arrows_border);
menu->priv->scroll_offset = arrows_border.top + (final_rect->y - flipped_rect->y);
gtk_menu_scroll_to (menu, menu->priv->scroll_offset);
}
/**
* gtk_menu_popdown:
* @menu: a #GtkMenu
*
* Removes the menu from the screen.
*/
void
gtk_menu_popdown (GtkMenu *menu)
{
GtkMenuPrivate *priv;
GtkMenuShell *menu_shell;
g_return_if_fail (GTK_IS_MENU (menu));
menu_shell = GTK_MENU_SHELL (menu);
priv = menu->priv;
menu_shell->priv->parent_menu_shell = NULL;
menu_shell->priv->active = FALSE;
menu_shell->priv->ignore_enter = FALSE;
priv->have_position = FALSE;
gtk_menu_stop_scrolling (menu);
gtk_menu_stop_navigating_submenu (menu);
if (menu_shell->priv->active_menu_item)
{
if (priv->old_active_menu_item)
g_object_unref (priv->old_active_menu_item);
priv->old_active_menu_item = menu_shell->priv->active_menu_item;
g_object_ref (priv->old_active_menu_item);
}
gtk_menu_shell_deselect (menu_shell);
/* The X Grab, if present, will automatically be removed
* when we hide the window
*/
if (priv->toplevel)
{
gtk_widget_hide (priv->toplevel);
gtk_window_set_transient_for (GTK_WINDOW (priv->toplevel), NULL);
}
gtk_widget_hide (GTK_WIDGET (menu));
menu_shell->priv->have_xgrab = FALSE;
gtk_grab_remove (GTK_WIDGET (menu));
_gtk_menu_shell_set_grab_device (menu_shell, NULL);
menu_grab_transfer_surface_destroy (menu);
}
/**
* gtk_menu_get_active:
* @menu: a #GtkMenu
*
* Returns the selected menu item from the menu. This is used by the
* #GtkComboBox.
*
* Returns: (transfer none): the #GtkMenuItem that was last selected
* in the menu. If a selection has not yet been made, the
* first menu item is selected.
*/
GtkWidget*
gtk_menu_get_active (GtkMenu *menu)
{
GtkMenuPrivate *priv;
GtkWidget *child;
GList *children;
g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
priv = menu->priv;
if (!priv->old_active_menu_item)
{
child = NULL;
children = GTK_MENU_SHELL (menu)->priv->children;
while (children)
{
child = children->data;
children = children->next;
if (gtk_bin_get_child (GTK_BIN (child)))
break;
child = NULL;
}
priv->old_active_menu_item = child;
if (priv->old_active_menu_item)
g_object_ref (priv->old_active_menu_item);
}
return priv->old_active_menu_item;
}
/**
* gtk_menu_set_active:
* @menu: a #GtkMenu
* @index: the index of the menu item to select. Index values are
* from 0 to n-1
*
* Selects the specified menu item within the menu. This is used by
* the #GtkComboBox and should not be used by anyone else.
*/
void
gtk_menu_set_active (GtkMenu *menu,
guint index)
{
GtkMenuPrivate *priv;
GtkWidget *child;
GList *tmp_list;
g_return_if_fail (GTK_IS_MENU (menu));
priv = menu->priv;
tmp_list = g_list_nth (GTK_MENU_SHELL (menu)->priv->children, index);
if (tmp_list)
{
child = tmp_list->data;
if (gtk_bin_get_child (GTK_BIN (child)))
{
if (priv->old_active_menu_item)
g_object_unref (priv->old_active_menu_item);
priv->old_active_menu_item = child;
g_object_ref (priv->old_active_menu_item);
}
}
g_object_notify (G_OBJECT (menu), "active");
}
/**
* gtk_menu_set_accel_group:
* @menu: a #GtkMenu
* @accel_group: (allow-none): the #GtkAccelGroup to be associated
* with the menu.
*
* Set the #GtkAccelGroup which holds global accelerators for the
* menu. This accelerator group needs to also be added to all windows
* that this menu is being used in with gtk_window_add_accel_group(),
* in order for those windows to support all the accelerators
* contained in this group.
*/
void
gtk_menu_set_accel_group (GtkMenu *menu,
GtkAccelGroup *accel_group)
{
GtkMenuPrivate *priv;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GTK_IS_ACCEL_GROUP (accel_group));
priv = menu->priv;
if (priv->accel_group != accel_group)
{
if (priv->accel_group)
g_object_unref (priv->accel_group);
priv->accel_group = accel_group;
if (priv->accel_group)
g_object_ref (priv->accel_group);
_gtk_menu_refresh_accel_paths (menu, TRUE);
}
}
/**
* gtk_menu_get_accel_group:
* @menu: a #GtkMenu
*
* Gets the #GtkAccelGroup which holds global accelerators for the
* menu. See gtk_menu_set_accel_group().
*
* Returns: (transfer none): the #GtkAccelGroup associated with the menu
*/
GtkAccelGroup*
gtk_menu_get_accel_group (GtkMenu *menu)
{
g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
return menu->priv->accel_group;
}
static gboolean
gtk_menu_real_can_activate_accel (GtkWidget *widget,
guint signal_id)
{
/* Menu items chain here to figure whether they can activate their
* accelerators. Unlike ordinary widgets, menus allow accel
* activation even if invisible since that's the usual case for
* submenus/popup-menus. however, the state of the attach widget
* affects the "activeness" of the menu.
*/
GtkWidget *awidget = gtk_menu_get_attach_widget (GTK_MENU (widget));
if (awidget)
return gtk_widget_can_activate_accel (awidget, signal_id);
else
return gtk_widget_is_sensitive (widget);
}
/**
* gtk_menu_set_accel_path:
* @menu: a valid #GtkMenu
* @accel_path: (nullable): a valid accelerator path, or %NULL to unset the path
*
* Sets an accelerator path for this menu from which accelerator paths
* for its immediate children, its menu items, can be constructed.
* The main purpose of this function is to spare the programmer the
* inconvenience of having to call gtk_menu_item_set_accel_path() on
* each menu item that should support runtime user changable accelerators.
* Instead, by just calling gtk_menu_set_accel_path() on their parent,
* each menu item of this menu, that contains a label describing its
* purpose, automatically gets an accel path assigned.
*
* For example, a menu containing menu items “New” and “Exit”, will, after
* `gtk_menu_set_accel_path (menu, "<Gnumeric-Sheet>/File");` has been
* called, assign its items the accel paths: `"<Gnumeric-Sheet>/File/New"`
* and `"<Gnumeric-Sheet>/File/Exit"`.
*
* Assigning accel paths to menu items then enables the user to change
* their accelerators at runtime. More details about accelerator paths
* and their default setups can be found at gtk_accel_map_add_entry().
*
* Note that @accel_path string will be stored in a #GQuark. Therefore,
* if you pass a static string, you can save some memory by interning
* it first with g_intern_static_string().
*/
void
gtk_menu_set_accel_path (GtkMenu *menu,
const gchar *accel_path)
{
GtkMenuPrivate *priv;
g_return_if_fail (GTK_IS_MENU (menu));
priv = menu->priv;
if (accel_path)
g_return_if_fail (accel_path[0] == '<' && strchr (accel_path, '/')); /* simplistic check */
priv->accel_path = g_intern_string (accel_path);
if (priv->accel_path)
_gtk_menu_refresh_accel_paths (menu, FALSE);
}
/**
* gtk_menu_get_accel_path:
* @menu: a valid #GtkMenu
*
* Retrieves the accelerator path set on the menu.
*
* Returns: the accelerator path set on the menu.
*/
const gchar*
gtk_menu_get_accel_path (GtkMenu *menu)
{
g_return_val_if_fail (GTK_IS_MENU (menu), NULL);
return menu->priv->accel_path;
}
typedef struct {
GtkMenu *menu;
gboolean group_changed;
} AccelPropagation;
static void
refresh_accel_paths_foreach (GtkWidget *widget,
gpointer data)
{
GtkMenuPrivate *priv;
AccelPropagation *prop = data;
if (GTK_IS_MENU_ITEM (widget)) /* should always be true */
{
priv = prop->menu->priv;
_gtk_menu_item_refresh_accel_path (GTK_MENU_ITEM (widget),
priv->accel_path,
priv->accel_group,
prop->group_changed);
}
}
static void
_gtk_menu_refresh_accel_paths (GtkMenu *menu,
gboolean group_changed)
{
GtkMenuPrivate *priv = menu->priv;
if (priv->accel_path && priv->accel_group)
{
AccelPropagation prop;
prop.menu = menu;
prop.group_changed = group_changed;
gtk_container_foreach (GTK_CONTAINER (menu),
refresh_accel_paths_foreach,
&prop);
}
}
/**
* gtk_menu_reposition:
* @menu: a #GtkMenu
*
* Repositions the menu according to its position function.
*/
void
gtk_menu_reposition (GtkMenu *menu)
{
g_return_if_fail (GTK_IS_MENU (menu));
if (gtk_widget_is_drawable (GTK_WIDGET (menu)))
gtk_menu_position (menu, FALSE);
}
/**
* gtk_menu_reorder_child:
* @menu: a #GtkMenu
* @child: the #GtkMenuItem to move
* @position: the new position to place @child.
* Positions are numbered from 0 to n - 1
*
* Moves @child to a new @position in the list of @menu
* children.
*/
void
gtk_menu_reorder_child (GtkMenu *menu,
GtkWidget *child,
gint position)
{
GtkMenuShell *menu_shell;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GTK_IS_MENU_ITEM (child));
menu_shell = GTK_MENU_SHELL (menu);
if (g_list_find (menu_shell->priv->children, child))
{
menu_shell->priv->children = g_list_remove (menu_shell->priv->children, child);
menu_shell->priv->children = g_list_insert (menu_shell->priv->children, child, position);
menu_queue_resize (menu);
}
}
static void
gtk_menu_realize (GtkWidget *widget)
{
GTK_WIDGET_CLASS (gtk_menu_parent_class)->realize (widget);
if (GTK_MENU_SHELL (widget)->priv->active_menu_item)
gtk_menu_scroll_item_visible (GTK_MENU_SHELL (widget),
GTK_MENU_SHELL (widget)->priv->active_menu_item);
}
static gboolean
gtk_menu_focus (GtkWidget *widget,
GtkDirectionType direction)
{
/* A menu or its menu items cannot have focus */
return FALSE;
}
/* See notes in gtk_menu_popup() for information
* about the “grab transfer window”
*/
static GdkSurface *
menu_grab_transfer_surface_get (GtkMenu *menu)
{
GdkSurface *surface = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-surface");
if (!surface)
{
surface = gdk_surface_new_temp (gtk_widget_get_display (GTK_WIDGET (menu)));
gtk_widget_register_surface (GTK_WIDGET (menu), surface);
gdk_surface_show (surface);
g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-surface"), surface);
}
return surface;
}
static void
menu_grab_transfer_surface_destroy (GtkMenu *menu)
{
GdkSurface *surface = g_object_get_data (G_OBJECT (menu), "gtk-menu-transfer-surface");
if (surface)
{
GdkSurface *widget_surface;
gtk_widget_unregister_surface (GTK_WIDGET (menu), surface);
gdk_surface_destroy (surface);
g_object_set_data (G_OBJECT (menu), I_("gtk-menu-transfer-surface"), NULL);
widget_surface = gtk_widget_get_surface (GTK_WIDGET (menu));
g_object_set_data (G_OBJECT (widget_surface), I_("gdk-attached-grab-surface"), surface);
}
}
static void
gtk_menu_unrealize (GtkWidget *widget)
{
GtkMenu *menu = GTK_MENU (widget);
menu_grab_transfer_surface_destroy (menu);
GTK_WIDGET_CLASS (gtk_menu_parent_class)->unrealize (widget);
}
static gint
calculate_line_heights (GtkMenu *menu,
gint for_width,
guint **ret_min_heights,
guint **ret_nat_heights)
{
GtkMenuPrivate *priv;
GtkMenuShell *menu_shell;
GtkWidget *child, *widget;
GList *children;
guint n_columns;
gint n_heights;
guint *min_heights;
guint *nat_heights;
gint avail_width;
priv = menu->priv;
widget = GTK_WIDGET (menu);
menu_shell = GTK_MENU_SHELL (widget);
min_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
nat_heights = g_new0 (guint, gtk_menu_get_n_rows (menu));
n_heights = gtk_menu_get_n_rows (menu);
n_columns = gtk_menu_get_n_columns (menu);
avail_width = for_width - (2 * priv->toggle_size + priv->accel_size) * n_columns;
for (children = menu_shell->priv->children; children; children = children->next)
{
gint part;
gint toggle_size;
gint l, r, t, b;
gint child_min, child_nat;
child = children->data;
if (!gtk_widget_get_visible (child))
continue;
get_effective_child_attach (child, &l, &r, &t, &b);
part = avail_width / (r - l);
gtk_widget_measure (child, GTK_ORIENTATION_VERTICAL,
part,
&child_min, &child_nat,
NULL, NULL);
gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
part = MAX (child_min, toggle_size) / (b - t);
min_heights[t] = MAX (min_heights[t], part);
part = MAX (child_nat, toggle_size) / (b - t);
nat_heights[t] = MAX (nat_heights[t], part);
}
if (ret_min_heights)
*ret_min_heights = min_heights;
else
g_free (min_heights);
if (ret_nat_heights)
*ret_nat_heights = nat_heights;
else
g_free (nat_heights);
return n_heights;
}
static void
gtk_menu_size_allocate (GtkWidget *widget,
const GtkAllocation *allocation,
int baseline)
{
GtkMenu *menu;
GtkMenuPrivate *priv;
GtkMenuShell *menu_shell;
GtkWidget *child;
GtkAllocation arrow_allocation, child_allocation;
GList *children;
gint x, y, i;
gint width, height;
GtkBorder arrow_border;
g_return_if_fail (GTK_IS_MENU (widget));
g_return_if_fail (allocation != NULL);
menu = GTK_MENU (widget);
menu_shell = GTK_MENU_SHELL (widget);
priv = menu->priv;
g_free (priv->heights);
priv->heights_length = calculate_line_heights (menu,
allocation->width,
&priv->heights,
NULL);
/* refresh our cached height request */
priv->requested_height = 0;
for (i = 0; i < priv->heights_length; i++)
priv->requested_height += priv->heights[i];
x = allocation->x;
y = allocation->y;
width = allocation->width;
height = allocation->height;
if (menu_shell->priv->active)
gtk_menu_scroll_to (menu, priv->scroll_offset);
get_arrows_border (menu, &arrow_border);
arrow_allocation.x = x;
arrow_allocation.y = y;
arrow_allocation.width = width;
arrow_allocation.height = arrow_border.top;
if (priv->upper_arrow_visible)
gtk_widget_size_allocate (priv->top_arrow_widget, &arrow_allocation, -1);
arrow_allocation.y = height - y - arrow_border.bottom;
arrow_allocation.height = arrow_border.bottom;
if (priv->lower_arrow_visible)
gtk_widget_size_allocate (priv->bottom_arrow_widget, &arrow_allocation, -1);
width = MAX (1, width);
height = MAX (1, height);
if (menu_shell->priv->children)
{
gint base_width = width / gtk_menu_get_n_columns (menu);
children = menu_shell->priv->children;
while (children)
{
child = children->data;
children = children->next;
if (gtk_widget_get_visible (child))
{
gint l, r, t, b;
get_effective_child_attach (child, &l, &r, &t, &b);
if (gtk_widget_get_direction (GTK_WIDGET (menu)) == GTK_TEXT_DIR_RTL)
{
guint tmp;
tmp = gtk_menu_get_n_columns (menu) - l;
l = gtk_menu_get_n_columns (menu) - r;
r = tmp;
}
child_allocation.width = (r - l) * base_width;
child_allocation.height = 0;
child_allocation.x = l * base_width;
child_allocation.y = 0;
for (i = 0; i < b; i++)
{
if (i < t)
child_allocation.y += priv->heights[i];
else
child_allocation.height += priv->heights[i];
}
gtk_menu_item_toggle_size_allocate (GTK_MENU_ITEM (child),
priv->toggle_size);
gtk_widget_size_allocate (child, &child_allocation, -1);
}
}
}
}
static void
gtk_menu_snapshot (GtkWidget *widget,
GtkSnapshot *snapshot)
{
gtk_snapshot_push_clip (snapshot,
&GRAPHENE_RECT_INIT(
0, 0,
gtk_widget_get_width (widget),
gtk_widget_get_height (widget)));
GTK_WIDGET_CLASS (gtk_menu_parent_class)->snapshot (widget, snapshot);
gtk_snapshot_pop (snapshot);
}
static void
gtk_menu_show (GtkWidget *widget)
{
GtkMenu *menu = GTK_MENU (widget);
_gtk_menu_refresh_accel_paths (menu, FALSE);
GTK_WIDGET_CLASS (gtk_menu_parent_class)->show (widget);
}
static void gtk_menu_measure (GtkWidget *widget,
GtkOrientation orientation,
int for_size,
int *minimum,
int *natural,
int *minimum_baseline,
int *natural_baseline)
{
GtkMenu *menu = GTK_MENU (widget);
GtkMenuShell *menu_shell = GTK_MENU_SHELL (widget);
GtkMenuPrivate *priv = gtk_menu_get_instance_private (menu);
GtkWidget *child;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
{
GList *children;
guint max_toggle_size;
guint max_accel_width;
gint child_min, child_nat;
gint min_width, nat_width;
min_width = nat_width = 0;
max_toggle_size = 0;
max_accel_width = 0;
children = menu_shell->priv->children;
while (children)
{
gint part;
gint toggle_size;
gint l, r, t, b;
child = children->data;
children = children->next;
if (! gtk_widget_get_visible (child))
continue;
get_effective_child_attach (child, &l, &r, &t, &b);
/* It's important to size_request the child
* before doing the toggle size request, in
* case the toggle size request depends on the size
* request of a child of the child (e.g. for ImageMenuItem)
*/
gtk_widget_measure (child, GTK_ORIENTATION_HORIZONTAL,
-1, &child_min, &child_nat, NULL, NULL);
gtk_menu_item_toggle_size_request (GTK_MENU_ITEM (child), &toggle_size);
max_toggle_size = MAX (max_toggle_size, toggle_size);
max_accel_width = MAX (max_accel_width,
GTK_MENU_ITEM (child)->priv->accelerator_width);
part = child_min / (r - l);
min_width = MAX (min_width, part);
part = child_nat / (r - l);
nat_width = MAX (nat_width, part);
}
/* If the menu doesn't include any images or check items
* reserve the space so that all menus are consistent.
* We only do this for 'ordinary' menus, not for combobox
* menus or multi-column menus
*/
if (max_toggle_size == 0 &&
gtk_menu_get_n_columns (menu) == 1 &&
!priv->no_toggle_size)
{
GtkWidget *menu_item;
GtkWidget *indicator_widget;
gint indicator_width;
/* Create a GtkCheckMenuItem, to query indicator size */
menu_item = gtk_check_menu_item_new ();
indicator_widget = _gtk_check_menu_item_get_indicator_widget (GTK_CHECK_MENU_ITEM (menu_item));
gtk_widget_measure (indicator_widget,
GTK_ORIENTATION_HORIZONTAL,
-1,
&indicator_width, NULL,
NULL, NULL);
max_toggle_size = indicator_width;
gtk_widget_destroy (menu_item);
g_object_ref_sink (menu_item);
g_object_unref (menu_item);
}
min_width += 2 * max_toggle_size + max_accel_width;
min_width *= gtk_menu_get_n_columns (menu);
nat_width += 2 * max_toggle_size + max_accel_width;
nat_width *= gtk_menu_get_n_columns (menu);
priv->toggle_size = max_toggle_size;
priv->accel_size = max_accel_width;
*minimum = min_width;
*natural = nat_width;
}
else /* VERTICAL */
{
if (for_size < 0)
{
gint min_width, nat_width;
/* Menus are height-for-width only, just return the height
* for the minimum width
*/
GTK_WIDGET_GET_CLASS (widget)->measure (widget, GTK_ORIENTATION_HORIZONTAL, -1,
&min_width, &nat_width, NULL, NULL);
GTK_WIDGET_GET_CLASS (widget)->measure (widget, GTK_ORIENTATION_VERTICAL, min_width,
minimum, natural,
minimum_baseline, natural_baseline);
}
else
{
GtkBorder arrow_border;
guint *min_heights, *nat_heights;
gint n_heights, i;
gint min_height, single_height, nat_height;
min_height = nat_height = 0;
single_height = 0;
n_heights =
calculate_line_heights (menu, for_size, &min_heights, &nat_heights);
for (i = 0; i < n_heights; i++)
{
min_height += min_heights[i];
single_height = MAX (single_height, min_heights[i]);
nat_height += nat_heights[i];
}
get_arrows_border (menu, &arrow_border);
single_height += arrow_border.top + arrow_border.bottom;
min_height = MIN (min_height, single_height);
if (priv->have_position)
{
GdkDisplay *display;
GdkMonitor *monitor;
GdkRectangle workarea;
GtkBorder border;
display = gtk_widget_get_display (priv->toplevel);
monitor = gdk_display_get_monitor (display, priv->monitor_num);
gdk_monitor_get_workarea (monitor, &workarea);
if (priv->position_y + min_height > workarea.y + workarea.height)
min_height = workarea.y + workarea.height - priv->position_y;
if (priv->position_y + nat_height > workarea.y + workarea.height)
nat_height = workarea.y + workarea.height - priv->position_y;
_gtk_window_get_shadow_width (GTK_WINDOW (priv->toplevel), &border);
if (priv->position_y + border.top < workarea.y)
{
min_height -= workarea.y - (priv->position_y + border.top);
nat_height -= workarea.y - (priv->position_y + border.top);
}
}
*minimum = min_height;
*natural = nat_height;
g_free (min_heights);
g_free (nat_heights);
}
}
}
static gboolean
pointer_in_menu_surface (GtkWidget *widget,
gdouble x_root,
gdouble y_root)
{
GtkMenu *menu = GTK_MENU (widget);
GtkMenuPrivate *priv = menu->priv;
GtkAllocation allocation;
if (gtk_widget_get_mapped (priv->toplevel))
{
GtkMenuShell *menu_shell;
gint surface_x, surface_y;
gdk_surface_get_position (gtk_widget_get_surface (priv->toplevel),
&surface_x, &surface_y);
gtk_widget_get_allocation (widget, &allocation);
if (x_root >= surface_x && x_root < surface_x + allocation.width &&
y_root >= surface_y && y_root < surface_y + allocation.height)
return TRUE;
menu_shell = GTK_MENU_SHELL (widget);
if (GTK_IS_MENU (menu_shell->priv->parent_menu_shell))
return pointer_in_menu_surface (menu_shell->priv->parent_menu_shell,
x_root, y_root);
}
return FALSE;
}
static void
gtk_menu_pressed_cb (GtkGestureMultiPress *gesture,
int n_press,
double x,
double y,
gpointer user_data)
{
GtkMenu *menu = user_data;
GdkEventSequence *sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
const GdkEvent *event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
const GdkEventButton *button_event = (GdkEventButton *)event;
GdkDevice *source_device;
GtkWidget *event_widget;
source_device = gdk_event_get_source_device (event);
event_widget = gtk_get_event_widget ((GdkEvent *)event);
/* Don't pass down to menu shell if a non-menuitem part of the menu
* was clicked. The check for the event_widget being a GtkMenuShell
* works because we have the pointer grabbed on menu_shell->window
* with owner_events=TRUE, so all events that are either outside
* the menu or on its border are delivered relative to
* menu_shell->window.
*/
if (GTK_IS_MENU_SHELL (event_widget) &&
pointer_in_menu_surface (GTK_WIDGET (menu), button_event->x_root, button_event->y_root))
{
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
return;
}
if (GTK_IS_MENU_ITEM (event_widget) &&
gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
GTK_MENU_ITEM (event_widget)->priv->submenu != NULL &&
!gtk_widget_is_drawable (GTK_MENU_ITEM (event_widget)->priv->submenu))
menu->priv->ignore_button_release = TRUE;
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
}
static void
gtk_menu_released_cb (GtkGestureMultiPress *gesture,
int n_press,
double x,
double y,
gpointer user_data)
{
GtkMenu *menu = user_data;
GtkMenuPrivate *priv = menu->priv;
GdkEventSequence *sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
const GdkEvent *event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
const GdkEventButton *button_event = (GdkEventButton *)event;
if (priv->ignore_button_release)
{
priv->ignore_button_release = FALSE;
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
return;
}
/* Don't pass down to menu shell if a non-menuitem part of the menu
* was clicked (see comment in gtk_menu_pressed_cb()).
*/
if (GTK_IS_MENU_SHELL (gtk_get_event_widget ((GdkEvent *) event)) &&
pointer_in_menu_surface (GTK_WIDGET (menu), button_event->x_root, button_event->y_root))
{
/* Ugly: make sure menu_shell->button gets reset to 0 when we
* bail out early here so it is in a consistent state for the
* next button press or release in GtkMenuShell.
* See bug #449371.
*/
if (GTK_MENU_SHELL (menu)->priv->active)
GTK_MENU_SHELL (menu)->priv->button = 0;
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}
}
static gboolean
check_threshold (GtkWidget *widget,
gint start_x,
gint start_y,
gint x,
gint y)
{
#define THRESHOLD 8
return
ABS (start_x - x) > THRESHOLD ||
ABS (start_y - y) > THRESHOLD;
}
static gboolean
definitely_within_item (GtkMenu *menu,
GtkWidget *widget,
gint x,
gint y)
{
int w, h;
graphene_rect_t bounds;
gtk_widget_compute_bounds (widget, GTK_WIDGET (menu), &bounds);
w = bounds.size.width;
h = bounds.size.height;
return
check_threshold (widget, 0, 0, x, y) &&
check_threshold (widget, w - 1, 0, x, y) &&
check_threshold (widget, w - 1, h - 1, x, y) &&
check_threshold (widget, 0, h - 1, x, y);
}
static gboolean
gtk_menu_has_navigation_triangle (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
return priv->navigation_height && priv->navigation_width;
}
static void
gtk_menu_motion (GtkEventController *controller,
double x,
double y,
gpointer user_data)
{
GtkWidget *menu_item;
GtkMenu *menu;
GtkMenuShell *menu_shell;
GtkWidget *parent;
GdkDevice *source_device;
GdkEventMotion *event;
menu_item = GTK_WIDGET (user_data);
event = (GdkEventMotion *)gtk_get_current_event (); /* FIXME: controller event */
source_device = gdk_event_get_source_device ((GdkEvent *)event);
if (GTK_IS_MENU (menu_item) &&
gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
{
GtkMenuPrivate *priv = GTK_MENU(menu_item)->priv;
if (priv->ignore_button_release)
priv->ignore_button_release = FALSE;
gtk_menu_handle_scrolling (GTK_MENU (menu_item), event->x_root, event->y_root, TRUE, TRUE);
}
/*We received the event for one of two reasons:
*
* a) We are the active menu, and did gtk_grab_add()
* b) The widget is a child of ours, and the event was propagated
*
* Since for computation of navigation regions, we want the menu which
* is the parent of the menu item, for a), we need to find that menu,
* which may be different from 'widget'.
*/
parent = gtk_widget_get_parent (menu_item);
if (!GTK_IS_MENU_ITEM (menu_item) ||
!GTK_IS_MENU (parent))
return;
menu_shell = GTK_MENU_SHELL (parent);
menu = GTK_MENU (menu_shell);
if (definitely_within_item (menu, menu_item, event->x, event->y))
menu_shell->priv->activate_time = 0;
/* Check to see if we are within an active submenu's navigation region
*/
if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
return;
/* Make sure we pop down if we enter a non-selectable menu item, so we
* don't show a submenu when the cursor is outside the stay-up triangle.
*/
if (!_gtk_menu_item_is_selectable (menu_item))
{
/* We really want to deselect, but this gives the menushell code
* a chance to do some bookkeeping about the menuitem.
*/
gtk_menu_shell_select_item (menu_shell, menu_item);
}
}
static void
gtk_menu_scroll_by (GtkMenu *menu,
gint step)
{
GtkMenuPrivate *priv = menu->priv;
GtkBorder arrow_border;
GtkWidget *widget;
gint offset;
gint view_height;
widget = GTK_WIDGET (menu);
offset = priv->scroll_offset + step;
get_arrows_border (menu, &arrow_border);
/* Don't scroll over the top if we weren't before: */
if ((priv->scroll_offset >= 0) && (offset < 0))
offset = 0;
view_height = gdk_surface_get_height (gtk_widget_get_surface (widget));
if (priv->scroll_offset == 0 &&
view_height >= priv->requested_height)
return;
/* Don't scroll past the bottom if we weren't before: */
if (priv->scroll_offset > 0)
view_height -= arrow_border.top;
/* Since arrows are shown, reduce view height even more */
view_height -= arrow_border.bottom;
if ((priv->scroll_offset + view_height <= priv->requested_height) &&
(offset + view_height > priv->requested_height))
offset = priv->requested_height - view_height;
if (offset != priv->scroll_offset)
gtk_menu_scroll_to (menu, offset);
}
static gboolean
gtk_menu_scroll_timeout (gpointer data)
{
GtkMenu *menu;
menu = GTK_MENU (data);
gtk_menu_scroll_by (menu, menu->priv->scroll_step);
return TRUE;
}
static void
gtk_menu_scroll_controller_scroll (GtkEventControllerScroll *scroll,
gdouble dx,
gdouble dy,
GtkMenu *menu)
{
gtk_menu_scroll_by (menu, dy * MENU_SCROLL_STEP2);
}
static void
get_arrows_sensitive_area (GtkMenu *menu,
GdkRectangle *upper,
GdkRectangle *lower)
{
GtkMenuPrivate *priv = menu->priv;
GtkWidget *widget = GTK_WIDGET (menu);
GdkSurface *surface;
gint width, height;
gint win_x, win_y;
gint top_arrow_height, bottom_arrow_height;
gtk_widget_measure (priv->top_arrow_widget,
GTK_ORIENTATION_VERTICAL,
-1,
&top_arrow_height, NULL,
NULL, NULL);
gtk_widget_measure (priv->bottom_arrow_widget,
GTK_ORIENTATION_VERTICAL,
-1,
&bottom_arrow_height, NULL,
NULL, NULL);
surface = gtk_widget_get_surface (widget);
width = gdk_surface_get_width (surface);
height = gdk_surface_get_height (surface);
gdk_surface_get_position (surface, &win_x, &win_y);
if (upper)
{
upper->x = win_x;
upper->y = win_y;
upper->width = width;
upper->height = top_arrow_height;
}
if (lower)
{
lower->x = win_x;
lower->y = win_y + height - bottom_arrow_height;
lower->width = width;
lower->height = bottom_arrow_height;
}
}
static void
gtk_menu_handle_scrolling (GtkMenu *menu,
gint x,
gint y,
gboolean enter,
gboolean motion)
{
GtkMenuPrivate *priv = menu->priv;
GtkMenuShell *menu_shell;
GdkRectangle rect;
gboolean in_arrow;
gboolean scroll_fast = FALSE;
gint top_x, top_y;
menu_shell = GTK_MENU_SHELL (menu);
gdk_surface_get_position (gtk_widget_get_surface (priv->toplevel),
&top_x, &top_y);
x -= top_x;
y -= top_y;
/* upper arrow handling */
get_arrows_sensitive_area (menu, &rect, NULL);
in_arrow = FALSE;
if (priv->upper_arrow_visible &&
(x >= rect.x) && (x < rect.x + rect.width) &&
(y >= rect.y) && (y < rect.y + rect.height))
{
in_arrow = TRUE;
}
if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
{
gboolean arrow_pressed = FALSE;
if (priv->upper_arrow_visible)
{
scroll_fast = (y < rect.y + MENU_SCROLL_FAST_ZONE);
if (enter && in_arrow &&
(!priv->upper_arrow_prelight ||
priv->scroll_fast != scroll_fast))
{
priv->upper_arrow_prelight = TRUE;
priv->scroll_fast = scroll_fast;
/* Deselect the active item so that
* any submenus are popped down
*/
gtk_menu_shell_deselect (menu_shell);
gtk_menu_remove_scroll_timeout (menu);
priv->scroll_step = scroll_fast
? -MENU_SCROLL_STEP2
: -MENU_SCROLL_STEP1;
priv->scroll_timeout = g_timeout_add (scroll_fast
? MENU_SCROLL_TIMEOUT2
: MENU_SCROLL_TIMEOUT1,
gtk_menu_scroll_timeout,
menu);
g_source_set_name_by_id (priv->scroll_timeout, "[gtk+] gtk_menu_scroll_timeout");
}
else if (!enter && !in_arrow && priv->upper_arrow_prelight)
{
gtk_menu_stop_scrolling (menu);
}
}
/* check if the button isn't insensitive before
* changing it to something else.
*/
if ((priv->upper_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
{
GtkStateFlags arrow_state = 0;
if (arrow_pressed)
arrow_state |= GTK_STATE_FLAG_ACTIVE;
if (priv->upper_arrow_prelight)
arrow_state |= GTK_STATE_FLAG_PRELIGHT;
if (arrow_state != priv->upper_arrow_state)
{
priv->upper_arrow_state = arrow_state;
gtk_widget_set_state_flags (priv->top_arrow_widget, arrow_state, TRUE);
}
}
}
/* lower arrow handling */
get_arrows_sensitive_area (menu, NULL, &rect);
in_arrow = FALSE;
if (priv->lower_arrow_visible &&
(x >= rect.x) && (x < rect.x + rect.width) &&
(y >= rect.y) && (y < rect.y + rect.height))
{
in_arrow = TRUE;
}
if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
{
gboolean arrow_pressed = FALSE;
if (priv->lower_arrow_visible)
{
scroll_fast = (y > rect.y + rect.height - MENU_SCROLL_FAST_ZONE);
if (enter && in_arrow &&
(!priv->lower_arrow_prelight ||
priv->scroll_fast != scroll_fast))
{
priv->lower_arrow_prelight = TRUE;
priv->scroll_fast = scroll_fast;
/* Deselect the active item so that
* any submenus are popped down
*/
gtk_menu_shell_deselect (menu_shell);
gtk_menu_remove_scroll_timeout (menu);
priv->scroll_step = scroll_fast
? MENU_SCROLL_STEP2
: MENU_SCROLL_STEP1;
priv->scroll_timeout = g_timeout_add (scroll_fast
? MENU_SCROLL_TIMEOUT2
: MENU_SCROLL_TIMEOUT1,
gtk_menu_scroll_timeout,
menu);
g_source_set_name_by_id (priv->scroll_timeout, "[gtk+] gtk_menu_scroll_timeout");
}
else if (!enter && !in_arrow && priv->lower_arrow_prelight)
{
gtk_menu_stop_scrolling (menu);
}
}
/* check if the button isn't insensitive before
* changing it to something else.
*/
if ((priv->lower_arrow_state & GTK_STATE_FLAG_INSENSITIVE) == 0)
{
GtkStateFlags arrow_state = 0;
if (arrow_pressed)
arrow_state |= GTK_STATE_FLAG_ACTIVE;
if (priv->lower_arrow_prelight)
arrow_state |= GTK_STATE_FLAG_PRELIGHT;
if (arrow_state != priv->lower_arrow_state)
{
priv->lower_arrow_state = arrow_state;
gtk_widget_set_state_flags (priv->bottom_arrow_widget, arrow_state, TRUE);
}
}
}
}
static void
gtk_menu_enter (GtkEventController *controller,
double x,
double y,
gpointer user_data)
{
GdkDevice *source_device;
GdkEventCrossing *event;
event = (GdkEventCrossing *)gtk_get_current_event ();
if (event->mode == GDK_CROSSING_GTK_GRAB ||
event->mode == GDK_CROSSING_GTK_UNGRAB ||
event->mode == GDK_CROSSING_STATE_CHANGED)
return;
source_device = gdk_event_get_source_device ((GdkEvent *) event);
if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
{
GtkMenuShell *menu_shell = GTK_MENU_SHELL (user_data);
if (!menu_shell->priv->ignore_enter)
gtk_menu_handle_scrolling (GTK_MENU (user_data),
event->x_root, event->y_root, TRUE, TRUE);
}
}
static void
gtk_menu_leave (GtkEventController *controller,
gpointer user_data)
{
GtkMenu *menu;
GdkDevice *source_device;
GdkEventCrossing *event;
event = (GdkEventCrossing *)gtk_get_current_event ();
if (event->mode == GDK_CROSSING_GTK_GRAB ||
event->mode == GDK_CROSSING_GTK_UNGRAB ||
event->mode == GDK_CROSSING_STATE_CHANGED)
return;
menu = GTK_MENU (user_data);
if (gtk_menu_navigating_submenu (menu, event->x_root, event->y_root))
return;
source_device = gdk_event_get_source_device ((GdkEvent *) event);
if (gdk_device_get_source (source_device) != GDK_SOURCE_TOUCHSCREEN)
gtk_menu_handle_scrolling (menu, event->x_root, event->y_root, FALSE, TRUE);
}
static gboolean
gtk_menu_key_pressed (GtkEventControllerKey *controller,
guint keyval,
guint keycode,
GdkModifierType state,
GtkMenu *menu)
{
gtk_menu_stop_navigating_submenu (menu);
return FALSE;
}
static gboolean
pointer_on_menu_widget (GtkMenu *menu,
gdouble x_root,
gdouble y_root)
{
GtkMenuPrivate *priv = menu->priv;
GtkAllocation allocation;
gint surface_x, surface_y;
gtk_widget_get_allocation (GTK_WIDGET (menu), &allocation);
gdk_surface_get_position (gtk_widget_get_surface (priv->toplevel),
&surface_x, &surface_y);
if (x_root >= surface_x && x_root < surface_x + allocation.width &&
y_root >= surface_y && y_root < surface_y + allocation.height)
return TRUE;
return FALSE;
}
static gboolean
gtk_menu_captured_event (GtkWidget *widget,
GdkEvent *event)
{
GdkDevice *source_device;
gboolean retval = FALSE;
GtkMenuPrivate *priv;
GtkMenu *menu;
gdouble x_root, y_root;
guint button;
GdkModifierType state;
menu = GTK_MENU (widget);
priv = menu->priv;
if (!priv->upper_arrow_visible && !priv->lower_arrow_visible && priv->drag_start_y < 0)
return retval;
source_device = gdk_event_get_source_device (event);
gdk_event_get_root_coords (event, &x_root, &y_root);
switch ((guint) gdk_event_get_event_type (event))
{
case GDK_TOUCH_BEGIN:
case GDK_BUTTON_PRESS:
if ((!gdk_event_get_button (event, &button) || button == 1) &&
gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN &&
pointer_on_menu_widget (menu, x_root, y_root))
{
priv->drag_start_y = event->button.y_root;
priv->initial_drag_offset = priv->scroll_offset;
priv->drag_scroll_started = FALSE;
}
else
priv->drag_start_y = -1;
priv->drag_already_pressed = TRUE;
break;
case GDK_TOUCH_END:
case GDK_BUTTON_RELEASE:
if (priv->drag_scroll_started)
{
priv->drag_scroll_started = FALSE;
priv->drag_start_y = -1;
priv->drag_already_pressed = FALSE;
retval = TRUE;
}
break;
case GDK_TOUCH_UPDATE:
case GDK_MOTION_NOTIFY:
if ((!gdk_event_get_state (event, &state) || (state & GDK_BUTTON1_MASK) != 0) &&
gdk_device_get_source (source_device) == GDK_SOURCE_TOUCHSCREEN)
{
if (!priv->drag_already_pressed)
{
if (pointer_on_menu_widget (menu, x_root, y_root))
{
priv->drag_start_y = y_root;
priv->initial_drag_offset = priv->scroll_offset;
priv->drag_scroll_started = FALSE;
}
else
priv->drag_start_y = -1;
priv->drag_already_pressed = TRUE;
}
if (priv->drag_start_y < 0 && !priv->drag_scroll_started)
break;
if (priv->drag_scroll_started)
{
gint offset, view_height;
GtkBorder arrow_border;
gdouble y_diff;
y_diff = y_root - priv->drag_start_y;
offset = priv->initial_drag_offset - y_diff;
view_height = gdk_surface_get_height (gtk_widget_get_surface (widget));
get_arrows_border (menu, &arrow_border);
if (priv->upper_arrow_visible)
view_height -= arrow_border.top;
if (priv->lower_arrow_visible)
view_height -= arrow_border.bottom;
offset = CLAMP (offset,
MIN (priv->scroll_offset, 0),
MAX (priv->scroll_offset, priv->requested_height - view_height));
gtk_menu_scroll_to (menu, offset);
retval = TRUE;
}
else if (gtk_drag_check_threshold (widget,
0, priv->drag_start_y,
0, y_root))
{
priv->drag_scroll_started = TRUE;
gtk_menu_shell_deselect (GTK_MENU_SHELL (menu));
retval = TRUE;
}
}
break;
case GDK_ENTER_NOTIFY:
case GDK_LEAVE_NOTIFY:
if (priv->drag_scroll_started)
retval = TRUE;
break;
default:
break;
}
return retval;
}
static void
gtk_menu_stop_navigating_submenu (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
priv->navigation_x = 0;
priv->navigation_y = 0;
priv->navigation_width = 0;
priv->navigation_height = 0;
if (priv->navigation_timeout)
{
g_source_remove (priv->navigation_timeout);
priv->navigation_timeout = 0;
}
}
static gboolean
gtk_menu_navigating_submenu (GtkMenu *menu,
gint event_x,
gint event_y)
{
GtkMenuPrivate *priv = menu->priv;
gint width, height;
if (!gtk_menu_has_navigation_triangle (menu))
return FALSE;
width = priv->navigation_width;
height = priv->navigation_height;
/* Check if x/y are in the triangle spanned by the navigation parameters */
/* 1) Move the coordinates so the triangle starts at 0,0 */
event_x -= priv->navigation_x;
event_y -= priv->navigation_y;
/* 2) Ensure both legs move along the positive axis */
if (width < 0)
{
event_x = -event_x;
width = -width;
}
if (height < 0)
{
event_y = -event_y;
height = -height;
}
/* 3) Check that the given coordinate is inside the triangle. The formula
* is a transformed form of this formula: x/w + y/h <= 1
*/
if (event_x >= 0 && event_y >= 0 &&
event_x * height + event_y * width <= width * height)
{
return TRUE;
}
else
{
gtk_menu_stop_navigating_submenu (menu);
return FALSE;
}
}
static void
gtk_menu_deactivate (GtkMenuShell *menu_shell)
{
GtkWidget *parent;
g_return_if_fail (GTK_IS_MENU (menu_shell));
parent = menu_shell->priv->parent_menu_shell;
menu_shell->priv->activate_time = 0;
gtk_menu_popdown (GTK_MENU (menu_shell));
if (parent)
gtk_menu_shell_deactivate (GTK_MENU_SHELL (parent));
}
static void
gtk_menu_position_legacy (GtkMenu *menu,
gboolean set_scroll_offset)
{
GtkMenuPrivate *priv = menu->priv;
GtkWidget *widget;
GtkRequisition requisition;
gint x, y;
gint scroll_offset;
GdkDisplay *display;
GdkMonitor *monitor;
GdkRectangle workarea;
gint monitor_num;
GdkDevice *pointer;
GtkBorder border;
gint i;
widget = GTK_WIDGET (menu);
display = gtk_widget_get_display (widget);
pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
gdk_device_get_position (pointer, &x, &y);
/* Realize so we have the proper width and height to figure out
* the right place to popup the menu.
*/
gtk_widget_realize (priv->toplevel);
_gtk_window_get_shadow_width (GTK_WINDOW (priv->toplevel), &border);
requisition.width = gtk_widget_get_allocated_width (widget);
requisition.height = gtk_widget_get_allocated_height (widget);
monitor = gdk_display_get_monitor_at_point (display, x, y);
monitor_num = 0;
for (i = 0; ; i++)
{
GdkMonitor *m = gdk_display_get_monitor (display, i);
if (m == monitor)
{
monitor_num = i;
break;
}
if (m == NULL)
break;
}
priv->monitor_num = monitor_num;
priv->initially_pushed_in = FALSE;
/* Set the type hint here to allow custom position functions
* to set a different hint
*/
if (!gtk_widget_get_visible (priv->toplevel))
gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), GDK_SURFACE_TYPE_HINT_POPUP_MENU);
if (priv->position_func)
{
(* priv->position_func) (menu, &x, &y, &priv->initially_pushed_in,
priv->position_func_data);
if (priv->monitor_num < 0)
priv->monitor_num = monitor_num;
monitor = gdk_display_get_monitor (display, priv->monitor_num);
gdk_monitor_get_workarea (monitor, &workarea);
}
else
{
gint space_left, space_right, space_above, space_below;
gint needed_width;
gint needed_height;
gboolean rtl = (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL);
/* The placement of popup menus horizontally works like this (with
* RTL in parentheses)
*
* - If there is enough room to the right (left) of the mouse cursor,
* position the menu there.
*
* - Otherwise, if if there is enough room to the left (right) of the
* mouse cursor, position the menu there.
*
* - Otherwise if the menu is smaller than the monitor, position it
* on the side of the mouse cursor that has the most space available
*
* - Otherwise (if there is simply not enough room for the menu on the
* monitor), position it as far left (right) as possible.
*
* Positioning in the vertical direction is similar: first try below
* mouse cursor, then above.
*/
monitor = gdk_display_get_monitor (display, priv->monitor_num);
gdk_monitor_get_workarea (monitor, &workarea);
space_left = x - workarea.x;
space_right = workarea.x + workarea.width - x - 1;
space_above = y - workarea.y;
space_below = workarea.y + workarea.height - y - 1;
/* Position horizontally. */
/* the amount of space we need to position the menu.
* Note the menu is offset "thickness" pixels
*/
needed_width = requisition.width;
if (needed_width <= space_left ||
needed_width <= space_right)
{
if ((rtl && needed_width <= space_left) ||
(!rtl && needed_width > space_right))
{
/* position left */
x = x - requisition.width + 1;
}
else
{
/* position right */
}
/* x is clamped on-screen further down */
}
else if (requisition.width <= workarea.width)
{
/* the menu is too big to fit on either side of the mouse
* cursor, but smaller than the monitor. Position it on
* the side that has the most space
*/
if (space_left > space_right)
{
/* left justify */
x = workarea.x;
}
else
{
/* right justify */
x = workarea.x + workarea.width - requisition.width;
}
}
else /* menu is simply too big for the monitor */
{
if (rtl)
{
/* right justify */
x = workarea.x + workarea.width - requisition.width;
}
else
{
/* left justify */
x = workarea.x;
}
}
/* Position vertically.
* The algorithm is the same as above, but simpler
* because we don't have to take RTL into account.
*/
needed_height = requisition.height;
if (needed_height <= space_above ||
needed_height <= space_below)
{
if (needed_height > space_below)
y = y - requisition.height + 1;
y = CLAMP (y, workarea.y,
workarea.y + workarea.height - requisition.height);
}
else if (needed_height > space_below && needed_height > space_above)
{
if (space_below >= space_above)
y = workarea.y + workarea.height - requisition.height;
else
y = workarea.y;
}
else
{
y = workarea.y;
}
}
scroll_offset = 0;
if (y + requisition.height > workarea.y + workarea.height)
{
if (priv->initially_pushed_in)
scroll_offset += (workarea.y + workarea.height) - requisition.height - y;
y = (workarea.y + workarea.height) - requisition.height;
}
if (y < workarea.y)
{
if (priv->initially_pushed_in)
scroll_offset += workarea.y - y;
y = workarea.y;
}
x = CLAMP (x, workarea.x, MAX (workarea.x, workarea.x + workarea.width - requisition.width));
x -= border.left;
y -= border.top;
if (GTK_MENU_SHELL (menu)->priv->active)
{
priv->have_position = TRUE;
priv->position_x = x;
priv->position_y = y;
}
if (scroll_offset != 0)
{
GtkBorder arrow_border;
get_arrows_border (menu, &arrow_border);
scroll_offset += arrow_border.top;
}
gtk_window_move (GTK_WINDOW (priv->toplevel), x, y);
if (set_scroll_offset)
priv->scroll_offset = scroll_offset;
}
static GdkGravity
get_horizontally_flipped_anchor (GdkGravity anchor)
{
switch (anchor)
{
case GDK_GRAVITY_STATIC:
case GDK_GRAVITY_NORTH_WEST:
return GDK_GRAVITY_NORTH_EAST;
case GDK_GRAVITY_NORTH:
return GDK_GRAVITY_NORTH;
case GDK_GRAVITY_NORTH_EAST:
return GDK_GRAVITY_NORTH_WEST;
case GDK_GRAVITY_WEST:
return GDK_GRAVITY_EAST;
case GDK_GRAVITY_CENTER:
return GDK_GRAVITY_CENTER;
case GDK_GRAVITY_EAST:
return GDK_GRAVITY_WEST;
case GDK_GRAVITY_SOUTH_WEST:
return GDK_GRAVITY_SOUTH_EAST;
case GDK_GRAVITY_SOUTH:
return GDK_GRAVITY_SOUTH;
case GDK_GRAVITY_SOUTH_EAST:
return GDK_GRAVITY_SOUTH_WEST;
default:
g_warning ("unknown GdkGravity: %d", anchor);
return anchor;
}
}
static void
gtk_menu_position (GtkMenu *menu,
gboolean set_scroll_offset)
{
GtkMenuPrivate *priv = menu->priv;
GdkSurface *rect_surface = NULL;
GdkRectangle rect;
GtkTextDirection text_direction = GTK_TEXT_DIR_NONE;
GdkGravity rect_anchor;
GdkGravity menu_anchor;
GdkAnchorHints anchor_hints;
gint rect_anchor_dx, rect_anchor_dy;
GdkSurface *toplevel;
gboolean emulated_move_to_rect = FALSE;
rect_anchor = priv->rect_anchor;
menu_anchor = priv->menu_anchor;
anchor_hints = priv->anchor_hints;
rect_anchor_dx = priv->rect_anchor_dx;
rect_anchor_dy = priv->rect_anchor_dy;
if (priv->rect_surface)
{
rect_surface = priv->rect_surface;
rect = priv->rect;
}
else if (priv->widget)
{
rect_surface = gtk_widget_get_surface (priv->widget);
gtk_widget_get_surface_allocation (priv->widget, &rect);
text_direction = gtk_widget_get_direction (priv->widget);
}
else if (!priv->position_func)
{
GtkWidget *attach_widget;
GdkDevice *grab_device;
/*
* One of the legacy gtk_menu_popup*() functions were used to popup but
* without a custom positioning function, so make an attempt to let the
* backend do the position constraining when required conditions are met.
*/
grab_device = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (menu));
attach_widget = gtk_menu_get_attach_widget (menu);
if (grab_device && attach_widget)
{
rect = (GdkRectangle) { 0, 0, 1, 1 };
rect_surface = gtk_widget_get_surface (attach_widget);
gdk_surface_get_device_position (rect_surface, grab_device,
&rect.x, &rect.y, NULL);
text_direction = gtk_widget_get_direction (attach_widget);
rect_anchor = GDK_GRAVITY_SOUTH_EAST;
menu_anchor = GDK_GRAVITY_NORTH_WEST;
anchor_hints = GDK_ANCHOR_FLIP | GDK_ANCHOR_SLIDE | GDK_ANCHOR_RESIZE;
rect_anchor_dx = 0;
rect_anchor_dy = 0;
emulated_move_to_rect = TRUE;
}
}
if (!rect_surface)
{
gtk_menu_position_legacy (menu, set_scroll_offset);
return;
}
/* Realize so we have the proper width and height to figure out
* the right place to popup the menu.
*/
gtk_widget_realize (priv->toplevel);
if (!gtk_widget_get_visible (priv->toplevel))
gtk_window_set_type_hint (GTK_WINDOW (priv->toplevel), priv->menu_type_hint);
if (text_direction == GTK_TEXT_DIR_NONE)
text_direction = gtk_widget_get_direction (GTK_WIDGET (menu));
if (text_direction == GTK_TEXT_DIR_RTL)
{
rect_anchor = get_horizontally_flipped_anchor (rect_anchor);
menu_anchor = get_horizontally_flipped_anchor (menu_anchor);
}
toplevel = gtk_widget_get_surface (priv->toplevel);
gdk_surface_set_transient_for (toplevel, rect_surface);
g_signal_handlers_disconnect_by_func (toplevel, moved_to_rect_cb, menu);
if (!emulated_move_to_rect)
g_signal_connect (toplevel, "moved-to-rect", G_CALLBACK (moved_to_rect_cb),
menu);
gdk_surface_move_to_rect (toplevel,
&rect,
rect_anchor,
menu_anchor,
anchor_hints,
rect_anchor_dx,
rect_anchor_dy);
}
static void
gtk_menu_remove_scroll_timeout (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
if (priv->scroll_timeout)
{
g_source_remove (priv->scroll_timeout);
priv->scroll_timeout = 0;
}
}
static void
gtk_menu_stop_scrolling (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
GtkCssNode *top_arrow_node, *bottom_arrow_node;
GtkStateFlags state;
gtk_menu_remove_scroll_timeout (menu);
priv->upper_arrow_prelight = FALSE;
priv->lower_arrow_prelight = FALSE;
top_arrow_node = gtk_widget_get_css_node (priv->top_arrow_widget);
state = gtk_css_node_get_state (top_arrow_node);
gtk_css_node_set_state (top_arrow_node, state & ~GTK_STATE_FLAG_PRELIGHT);
bottom_arrow_node = gtk_widget_get_css_node (priv->bottom_arrow_widget);
state = gtk_css_node_get_state (bottom_arrow_node);
gtk_css_node_set_state (bottom_arrow_node, state & ~GTK_STATE_FLAG_PRELIGHT);
}
static void
gtk_menu_scroll_to (GtkMenu *menu,
gint offset)
{
GtkMenuPrivate *priv = menu->priv;
GtkCssNode *top_arrow_node, *bottom_arrow_node;
top_arrow_node = gtk_widget_get_css_node (priv->top_arrow_widget);
gtk_css_node_set_visible (top_arrow_node, priv->upper_arrow_visible);
gtk_css_node_set_state (top_arrow_node, priv->upper_arrow_state);
bottom_arrow_node = gtk_widget_get_css_node (priv->bottom_arrow_widget);
gtk_css_node_set_visible (bottom_arrow_node, priv->lower_arrow_visible);
gtk_css_node_set_state (bottom_arrow_node, priv->lower_arrow_state);
priv->scroll_offset = offset;
}
static gboolean
compute_child_offset (GtkMenu *menu,
GtkWidget *menu_item,
gint *offset,
gint *height,
gboolean *is_last_child)
{
GtkMenuPrivate *priv = menu->priv;
gint item_top_attach;
gint item_bottom_attach;
gint child_offset = 0;
gint i;
get_effective_child_attach (menu_item, NULL, NULL,
&item_top_attach, &item_bottom_attach);
/* there is a possibility that we get called before _size_request,
* so check the height table for safety.
*/
if (!priv->heights || priv->heights_length < gtk_menu_get_n_rows (menu))
return FALSE;
/* when we have a row with only invisible children, its height will
* be zero, so there's no need to check WIDGET_VISIBLE here
*/
for (i = 0; i < item_top_attach; i++)
child_offset += priv->heights[i];
if (is_last_child)
*is_last_child = (item_bottom_attach == gtk_menu_get_n_rows (menu));
if (offset)
*offset = child_offset;
if (height)
*height = priv->heights[item_top_attach];
return TRUE;
}
static void
gtk_menu_scroll_item_visible (GtkMenuShell *menu_shell,
GtkWidget *menu_item)
{
GtkMenu *menu = GTK_MENU (menu_shell);
GtkMenuPrivate *priv = menu->priv;
GtkWidget *widget = GTK_WIDGET (menu_shell);
gint child_offset, child_height;
gint height;
gint y;
gint arrow_height;
gboolean last_child = 0;
/* We need to check if the selected item fully visible.
* If not we need to scroll the menu so that it becomes fully
* visible.
*/
if (compute_child_offset (menu, menu_item,
&child_offset, &child_height, &last_child))
{
y = priv->scroll_offset;
height = gdk_surface_get_height (gtk_widget_get_surface (widget));
if (child_offset < y)
{
/* Ignore the enter event we might get if the pointer
* is on the menu
*/
menu_shell->priv->ignore_enter = TRUE;
gtk_menu_scroll_to (menu, child_offset);
}
else
{
GtkBorder arrow_border;
arrow_height = 0;
get_arrows_border (menu, &arrow_border);
arrow_height = arrow_border.top + arrow_border.bottom;
if (child_offset + child_height > y + height - arrow_height)
{
arrow_height = arrow_border.bottom + arrow_border.top;
y = child_offset + child_height - height + arrow_height;
/* Ignore the enter event we might get if the pointer
* is on the menu
*/
menu_shell->priv->ignore_enter = TRUE;
gtk_menu_scroll_to (menu, y);
}
}
}
}
static void
gtk_menu_select_item (GtkMenuShell *menu_shell,
GtkWidget *menu_item)
{
GtkMenu *menu = GTK_MENU (menu_shell);
if (gtk_widget_get_realized (GTK_WIDGET (menu)))
gtk_menu_scroll_item_visible (menu_shell, menu_item);
GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->select_item (menu_shell, menu_item);
}
/**
* gtk_menu_set_display:
* @menu: a #GtkMenu
* @display: (allow-none): a #GdkDisplay, or %NULL if the display should be
* determined by the widget the menu is attached to
*
* Sets the #GdkDisplay on which the menu will be displayed.
*/
void
gtk_menu_set_display (GtkMenu *menu,
GdkDisplay *display)
{
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (display == NULL || GDK_IS_DISPLAY (display));
g_object_set_data (G_OBJECT (menu), I_("gtk-menu-explicit-display"), display);
if (display)
{
menu_change_display (menu, display);
}
else
{
GtkWidget *attach_widget = gtk_menu_get_attach_widget (menu);
if (attach_widget)
attach_widget_display_changed (attach_widget, NULL, menu);
}
}
/**
* gtk_menu_attach:
* @menu: a #GtkMenu
* @child: a #GtkMenuItem
* @left_attach: The column number to attach the left side of the item to
* @right_attach: The column number to attach the right side of the item to
* @top_attach: The row number to attach the top of the item to
* @bottom_attach: The row number to attach the bottom of the item to
*
* Adds a new #GtkMenuItem to a (table) menu. The number of “cells” that
* an item will occupy is specified by @left_attach, @right_attach,
* @top_attach and @bottom_attach. These each represent the leftmost,
* rightmost, uppermost and lower column and row numbers of the table.
* (Columns and rows are indexed from zero).
*
* Note that this function is not related to gtk_menu_detach().
*/
void
gtk_menu_attach (GtkMenu *menu,
GtkWidget *child,
guint left_attach,
guint right_attach,
guint top_attach,
guint bottom_attach)
{
GtkMenuShell *menu_shell;
GtkWidget *parent;
g_return_if_fail (GTK_IS_MENU (menu));
g_return_if_fail (GTK_IS_MENU_ITEM (child));
parent = gtk_widget_get_parent (child);
g_return_if_fail (parent == NULL || parent == GTK_WIDGET (menu));
g_return_if_fail (left_attach < right_attach);
g_return_if_fail (top_attach < bottom_attach);
menu_shell = GTK_MENU_SHELL (menu);
if (!parent)
{
AttachInfo *ai = get_attach_info (child);
ai->left_attach = left_attach;
ai->right_attach = right_attach;
ai->top_attach = top_attach;
ai->bottom_attach = bottom_attach;
menu_shell->priv->children = g_list_append (menu_shell->priv->children, child);
gtk_widget_insert_before (child, GTK_WIDGET (menu), menu->priv->bottom_arrow_widget);
menu_queue_resize (menu);
}
else
{
gtk_container_child_set (GTK_CONTAINER (parent), child,
"left-attach", left_attach,
"right-attach", right_attach,
"top-attach", top_attach,
"bottom-attach", bottom_attach,
NULL);
}
}
static gint
gtk_menu_get_popup_delay (GtkMenuShell *menu_shell)
{
return MENU_POPUP_DELAY;
}
static GtkWidget *
find_child_containing (GtkMenuShell *menu_shell,
int left,
int right,
int top,
int bottom)
{
GList *list;
/* find a child which includes the area given by
* left, right, top, bottom.
*/
for (list = menu_shell->priv->children; list; list = list->next)
{
gint l, r, t, b;
if (!_gtk_menu_item_is_selectable (list->data))
continue;
get_effective_child_attach (list->data, &l, &r, &t, &b);
if (l <= left && right <= r && t <= top && bottom <= b)
return GTK_WIDGET (list->data);
}
return NULL;
}
static void
gtk_menu_move_current (GtkMenuShell *menu_shell,
GtkMenuDirectionType direction)
{
GtkMenu *menu = GTK_MENU (menu_shell);
gint i;
gint l, r, t, b;
GtkWidget *match = NULL;
if (gtk_widget_get_direction (GTK_WIDGET (menu_shell)) == GTK_TEXT_DIR_RTL)
{
switch (direction)
{
case GTK_MENU_DIR_CHILD:
direction = GTK_MENU_DIR_PARENT;
break;
case GTK_MENU_DIR_PARENT:
direction = GTK_MENU_DIR_CHILD;
break;
case GTK_MENU_DIR_NEXT:
case GTK_MENU_DIR_PREV:
default:
break;
}
}
/* use special table menu key bindings */
if (menu_shell->priv->active_menu_item && gtk_menu_get_n_columns (menu) > 1)
{
get_effective_child_attach (menu_shell->priv->active_menu_item, &l, &r, &t, &b);
if (direction == GTK_MENU_DIR_NEXT)
{
for (i = b; i < gtk_menu_get_n_rows (menu); i++)
{
match = find_child_containing (menu_shell, l, l + 1, i, i + 1);
if (match)
break;
}
if (!match)
{
/* wrap around */
for (i = 0; i < t; i++)
{
match = find_child_containing (menu_shell,
l, l + 1, i, i + 1);
if (match)
break;
}
}
}
else if (direction == GTK_MENU_DIR_PREV)
{
for (i = t; i > 0; i--)
{
match = find_child_containing (menu_shell,
l, l + 1, i - 1, i);
if (match)
break;
}
if (!match)
{
/* wrap around */
for (i = gtk_menu_get_n_rows (menu); i > b; i--)
{
match = find_child_containing (menu_shell,
l, l + 1, i - 1, i);
if (match)
break;
}
}
}
else if (direction == GTK_MENU_DIR_PARENT)
{
/* we go one left if possible */
if (l > 0)
match = find_child_containing (menu_shell,
l - 1, l, t, t + 1);
if (!match)
{
GtkWidget *parent = menu_shell->priv->parent_menu_shell;
if (!parent
|| g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1)
match = menu_shell->priv->active_menu_item;
}
}
else if (direction == GTK_MENU_DIR_CHILD)
{
/* we go one right if possible */
if (r < gtk_menu_get_n_columns (menu))
match = find_child_containing (menu_shell, r, r + 1, t, t + 1);
if (!match)
{
GtkWidget *parent = menu_shell->priv->parent_menu_shell;
if (! GTK_MENU_ITEM (menu_shell->priv->active_menu_item)->priv->submenu &&
(!parent ||
g_list_length (GTK_MENU_SHELL (parent)->priv->children) <= 1))
match = menu_shell->priv->active_menu_item;
}
}
if (match)
{
gtk_menu_shell_select_item (menu_shell, match);
return;
}
}
GTK_MENU_SHELL_CLASS (gtk_menu_parent_class)->move_current (menu_shell, direction);
}
static gint
get_visible_size (GtkMenu *menu)
{
GtkAllocation allocation;
GtkWidget *widget = GTK_WIDGET (menu);
GtkBorder arrow_border;
gint menu_height;
gtk_widget_get_allocation (widget, &allocation);
menu_height = allocation.height;
get_arrows_border (menu, &arrow_border);
menu_height -= arrow_border.top;
menu_height -= arrow_border.bottom;
return menu_height;
}
/* Find the sensitive on-screen child containing @y, or if none,
* the nearest selectable onscreen child. (%NULL if none)
*/
static GtkWidget *
child_at (GtkMenu *menu,
gint y)
{
GtkMenuPrivate *priv = menu->priv;
GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
GtkWidget *child = NULL;
gint child_offset = 0;
GList *children;
gint menu_height;
gint lower, upper; /* Onscreen bounds */
menu_height = get_visible_size (menu);
lower = priv->scroll_offset;
upper = priv->scroll_offset + menu_height;
for (children = menu_shell->priv->children; children; children = children->next)
{
if (gtk_widget_get_visible (children->data))
{
GtkRequisition child_requisition;
gtk_widget_get_preferred_size (children->data,
&child_requisition, NULL);
if (_gtk_menu_item_is_selectable (children->data) &&
child_offset >= lower &&
child_offset + child_requisition.height <= upper)
{
child = children->data;
if (child_offset + child_requisition.height > y)
return child;
}
child_offset += child_requisition.height;
}
}
return child;
}
static gint
get_menu_height (GtkMenu *menu)
{
GtkMenuPrivate *priv = menu->priv;
GtkBorder arrow_border;
gint height;
height = priv->requested_height;
get_arrows_border (menu, &arrow_border);
height -= arrow_border.top;
height -= arrow_border.bottom;
return height;
}
static void
gtk_menu_real_move_scroll (GtkMenu *menu,
GtkScrollType type)
{
GtkMenuPrivate *priv = menu->priv;
gint page_size = get_visible_size (menu);
gint end_position = get_menu_height (menu);
GtkMenuShell *menu_shell = GTK_MENU_SHELL (menu);
switch ((guint) type)
{
case GTK_SCROLL_PAGE_UP:
case GTK_SCROLL_PAGE_DOWN:
{
gint old_offset;
gint new_offset;
gint child_offset = 0;
gboolean old_upper_arrow_visible;
gint step;
if (type == GTK_SCROLL_PAGE_UP)
step = - page_size;
else
step = page_size;
if (menu_shell->priv->active_menu_item)
{
gint child_height;
if (compute_child_offset (menu, menu_shell->priv->active_menu_item,
&child_offset, &child_height, NULL))
child_offset += child_height / 2;
}
menu_shell->priv->ignore_enter = TRUE;
old_upper_arrow_visible = priv->upper_arrow_visible;
old_offset = priv->scroll_offset;
new_offset = priv->scroll_offset + step;
new_offset = CLAMP (new_offset, 0, end_position - page_size);
gtk_menu_scroll_to (menu, new_offset);
if (menu_shell->priv->active_menu_item)
{
GtkWidget *new_child;
gboolean new_upper_arrow_visible = priv->upper_arrow_visible;
GtkBorder arrow_border;
get_arrows_border (menu, &arrow_border);
if (priv->scroll_offset != old_offset)
step = priv->scroll_offset - old_offset;
step -= (new_upper_arrow_visible - old_upper_arrow_visible) * arrow_border.top;
new_child = child_at (menu, child_offset + step);
if (new_child)
gtk_menu_shell_select_item (menu_shell, new_child);
}
}
break;
case GTK_SCROLL_START:
/* Ignore the enter event we might get if the pointer is on the menu */
menu_shell->priv->ignore_enter = TRUE;
gtk_menu_shell_select_first (menu_shell, TRUE);
break;
case GTK_SCROLL_END:
/* Ignore the enter event we might get if the pointer is on the menu */
menu_shell->priv->ignore_enter = TRUE;
_gtk_menu_shell_select_last (menu_shell, TRUE);
break;
default:
break;
}
}
/**
* gtk_menu_set_monitor:
* @menu: a #GtkMenu
* @monitor_num: the number of the monitor on which the menu should
* be popped up
*
* Informs GTK+ on which monitor a menu should be popped up.
* See gdk_monitor_get_geometry().
*
* This function should be called from a #GtkMenuPositionFunc
* if the menu should not appear on the same monitor as the pointer.
* This information can’t be reliably inferred from the coordinates
* returned by a #GtkMenuPositionFunc, since, for very long menus,
* these coordinates may extend beyond the monitor boundaries or even
* the screen boundaries.
*/
void
gtk_menu_set_monitor (GtkMenu *menu,
gint monitor_num)
{
GtkMenuPrivate *priv;
g_return_if_fail (GTK_IS_MENU (menu));
priv = menu->priv;
if (priv->monitor_num != monitor_num)
{
priv->monitor_num = monitor_num;
g_object_notify (G_OBJECT (menu), "monitor");
}
}
/**
* gtk_menu_get_monitor:
* @menu: a #GtkMenu
*
* Retrieves the number of the monitor on which to show the menu.
*
* Returns: the number of the monitor on which the menu should
* be popped up or -1, if no monitor has been set
*/
gint
gtk_menu_get_monitor (GtkMenu *menu)
{
g_return_val_if_fail (GTK_IS_MENU (menu), -1);
return menu->priv->monitor_num;
}
/**
* gtk_menu_place_on_monitor:
* @menu: a #GtkMenu
* @monitor: the monitor to place the menu on
*
* Places @menu on the given monitor.
*/
void
gtk_menu_place_on_monitor (GtkMenu *menu,
GdkMonitor *monitor)
{
GdkDisplay *display;
gint i, monitor_num;
g_return_if_fail (GTK_IS_MENU (menu));
display = gtk_widget_get_display (GTK_WIDGET (menu));
monitor_num = 0;
for (i = 0; ; i++)
{
GdkMonitor *m = gdk_display_get_monitor (display, i);
if (m == monitor)
{
monitor_num = i;
break;
}
if (m == NULL)
break;
}
gtk_menu_set_monitor (menu, monitor_num);
}
/**
* gtk_menu_get_for_attach_widget:
* @widget: a #GtkWidget
*
* Returns a list of the menus which are attached to this widget.
* This list is owned by GTK+ and must not be modified.
*
* Returns: (element-type GtkWidget) (transfer none): the list
* of menus attached to his widget.
*/
GList*
gtk_menu_get_for_attach_widget (GtkWidget *widget)
{
GList *list;
g_return_val_if_fail (GTK_IS_WIDGET (widget), NULL);
list = g_object_get_data (G_OBJECT (widget), ATTACHED_MENUS);
return list;
}
static void
gtk_menu_grab_notify (GtkWidget *widget,
gboolean was_grabbed)
{
GtkMenu *menu;
GtkWidget *toplevel;
GtkWindowGroup *group;
GtkWidget *grab;
GdkDevice *pointer;
menu = GTK_MENU (widget);
pointer = _gtk_menu_shell_get_grab_device (GTK_MENU_SHELL (widget));
if (!pointer ||
!gtk_widget_device_is_shadowed (widget, pointer))
return;
toplevel = gtk_widget_get_toplevel (widget);
if (!GTK_IS_WINDOW (toplevel))
return;
group = gtk_window_get_group (GTK_WINDOW (toplevel));
grab = gtk_window_group_get_current_grab (group);
if (GTK_MENU_SHELL (widget)->priv->active && !GTK_IS_MENU_SHELL (grab))
gtk_menu_shell_cancel (GTK_MENU_SHELL (widget));
menu->priv->drag_scroll_started = FALSE;
}
/**
* gtk_menu_set_reserve_toggle_size:
* @menu: a #GtkMenu
* @reserve_toggle_size: whether to reserve size for toggles
*
* Sets whether the menu should reserve space for drawing toggles
* or icons, regardless of their actual presence.
*/
void
gtk_menu_set_reserve_toggle_size (GtkMenu *menu,
gboolean reserve_toggle_size)
{
GtkMenuPrivate *priv;
gboolean no_toggle_size;
g_return_if_fail (GTK_IS_MENU (menu));
priv = menu->priv;
no_toggle_size = !reserve_toggle_size;
if (priv->no_toggle_size != no_toggle_size)
{
priv->no_toggle_size = no_toggle_size;
g_object_notify (G_OBJECT (menu), "reserve-toggle-size");
}
}
/**
* gtk_menu_get_reserve_toggle_size:
* @menu: a #GtkMenu
*
* Returns whether the menu reserves space for toggles and
* icons, regardless of their actual presence.
*
* Returns: Whether the menu reserves toggle space
*/
gboolean
gtk_menu_get_reserve_toggle_size (GtkMenu *menu)
{
g_return_val_if_fail (GTK_IS_MENU (menu), FALSE);
return !menu->priv->no_toggle_size;
}
/**
* gtk_menu_new_from_model:
* @model: a #GMenuModel
*
* Creates a #GtkMenu and populates it with menu items and
* submenus according to @model.
*
* The created menu items are connected to actions found in the
* #GtkApplicationWindow to which the menu belongs - typically
* by means of being attached to a widget (see gtk_menu_attach_to_widget())
* that is contained within the #GtkApplicationWindows widget hierarchy.
*
* Actions can also be added using gtk_widget_insert_action_group() on the menu's
* attach widget or on any of its parent widgets.
*
* Returns: a new #GtkMenu
*/
GtkWidget *
gtk_menu_new_from_model (GMenuModel *model)
{
GtkWidget *menu;
g_return_val_if_fail (G_IS_MENU_MODEL (model), NULL);
menu = gtk_menu_new ();
gtk_menu_shell_bind_model (GTK_MENU_SHELL (menu), model, NULL, TRUE);
return menu;
}
|