1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
|
/*
* Copyright (C) 2007-2010 Openismus GmbH
* Copyright (C) 2013 Red Hat, Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, see <http://www.gnu.org/licenses/>.
*
* Authors:
* Tristan Van Berkom <tristanvb@openismus.com>
* Matthias Clasen <mclasen@redhat.com>
* William Jon McCann <jmccann@redhat.com>
*/
/* Preamble {{{1 */
/**
* SECTION:gtkflowbox
* @Short_Description: A container that allows reflowing its children
* @Title: GtkFlowBox
*
* A GtkFlowBox positions child widgets in sequence according to its
* orientation.
*
* For instance, with the horizontal orientation, the widgets will be
* arranged from left to right, starting a new row under the previous
* row when necessary. Reducing the width in this case will require more
* rows, so a larger height will be requested.
*
* Likewise, with the vertical orientation, the widgets will be arranged
* from top to bottom, starting a new column to the right when necessary.
* Reducing the height will require more columns, so a larger width will
* be requested.
*
* The children of a GtkFlowBox can be dynamically sorted and filtered.
*
* Although a GtkFlowBox must have only #GtkFlowBoxChild children,
* you can add any kind of widget to it via gtk_container_add(), and
* a GtkFlowBoxChild widget will automatically be inserted between
* the box and the widget.
*
* Also see #GtkListBox.
*
* GtkFlowBox was added in GTK+ 3.12.
*/
#include <config.h>
#include "gtkflowbox.h"
#include "gtkmarshalers.h"
#include "gtkprivate.h"
#include "gtkorientableprivate.h"
#include "gtkintl.h"
#include "gtkcssnodeprivate.h"
#include "gtkwidgetprivate.h"
#include "a11y/gtkflowboxaccessibleprivate.h"
#include "a11y/gtkflowboxchildaccessible.h"
/* Forward declarations and utilities {{{1 */
static void gtk_flow_box_update_cursor (GtkFlowBox *box,
GtkFlowBoxChild *child);
static void gtk_flow_box_select_and_activate (GtkFlowBox *box,
GtkFlowBoxChild *child);
static void gtk_flow_box_update_selection (GtkFlowBox *box,
GtkFlowBoxChild *child,
gboolean modify,
gboolean extend);
static void gtk_flow_box_apply_filter (GtkFlowBox *box,
GtkFlowBoxChild *child);
static void gtk_flow_box_apply_sort (GtkFlowBox *box,
GtkFlowBoxChild *child);
static gint gtk_flow_box_sort (GtkFlowBoxChild *a,
GtkFlowBoxChild *b,
GtkFlowBox *box);
static void
get_current_selection_modifiers (GtkWidget *widget,
gboolean *modify,
gboolean *extend)
{
GdkModifierType state = 0;
GdkModifierType mask;
*modify = FALSE;
*extend = FALSE;
if (gtk_get_current_event_state (&state))
{
mask = gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_MODIFY_SELECTION);
if ((state & mask) == mask)
*modify = TRUE;
mask = gtk_widget_get_modifier_mask (widget, GDK_MODIFIER_INTENT_EXTEND_SELECTION);
if ((state & mask) == mask)
*extend = TRUE;
}
}
static void
path_from_horizontal_line_rects (cairo_t *cr,
GdkRectangle *lines,
gint n_lines)
{
gint start_line, end_line;
GdkRectangle *r;
gint i;
/* Join rows vertically by extending to the middle */
for (i = 0; i < n_lines - 1; i++)
{
GdkRectangle *r1 = &lines[i];
GdkRectangle *r2 = &lines[i+1];
gint gap, old;
gap = r2->y - (r1->y + r1->height);
r1->height += gap / 2;
old = r2->y;
r2->y = r1->y + r1->height;
r2->height += old - r2->y;
}
cairo_new_path (cr);
start_line = 0;
do
{
for (i = start_line; i < n_lines; i++)
{
r = &lines[i];
if (i == start_line)
cairo_move_to (cr, r->x + r->width, r->y);
else
cairo_line_to (cr, r->x + r->width, r->y);
cairo_line_to (cr, r->x + r->width, r->y + r->height);
if (i < n_lines - 1 &&
(r->x + r->width < lines[i+1].x ||
r->x > lines[i+1].x + lines[i+1].width))
{
i++;
break;
}
}
end_line = i;
for (i = end_line - 1; i >= start_line; i--)
{
r = &lines[i];
cairo_line_to (cr, r->x, r->y + r->height);
cairo_line_to (cr, r->x, r->y);
}
cairo_close_path (cr);
start_line = end_line;
}
while (end_line < n_lines);
}
static void
path_from_vertical_line_rects (cairo_t *cr,
GdkRectangle *lines,
gint n_lines)
{
gint start_line, end_line;
GdkRectangle *r;
gint i;
/* Join rows horizontally by extending to the middle */
for (i = 0; i < n_lines - 1; i++)
{
GdkRectangle *r1 = &lines[i];
GdkRectangle *r2 = &lines[i+1];
gint gap, old;
gap = r2->x - (r1->x + r1->width);
r1->width += gap / 2;
old = r2->x;
r2->x = r1->x + r1->width;
r2->width += old - r2->x;
}
cairo_new_path (cr);
start_line = 0;
do
{
for (i = start_line; i < n_lines; i++)
{
r = &lines[i];
if (i == start_line)
cairo_move_to (cr, r->x, r->y + r->height);
else
cairo_line_to (cr, r->x, r->y + r->height);
cairo_line_to (cr, r->x + r->width, r->y + r->height);
if (i < n_lines - 1 &&
(r->y + r->height < lines[i+1].y ||
r->y > lines[i+1].y + lines[i+1].height))
{
i++;
break;
}
}
end_line = i;
for (i = end_line - 1; i >= start_line; i--)
{
r = &lines[i];
cairo_line_to (cr, r->x + r->width, r->y);
cairo_line_to (cr, r->x, r->y);
}
cairo_close_path (cr);
start_line = end_line;
}
while (end_line < n_lines);
}
/* GtkFlowBoxChild {{{1 */
/* GObject boilerplate {{{2 */
enum {
CHILD_ACTIVATE,
CHILD_LAST_SIGNAL
};
static guint child_signals[CHILD_LAST_SIGNAL] = { 0 };
typedef struct _GtkFlowBoxChildPrivate GtkFlowBoxChildPrivate;
struct _GtkFlowBoxChildPrivate
{
GSequenceIter *iter;
gboolean selected;
};
#define CHILD_PRIV(child) ((GtkFlowBoxChildPrivate*)gtk_flow_box_child_get_instance_private ((GtkFlowBoxChild*)(child)))
G_DEFINE_TYPE_WITH_PRIVATE (GtkFlowBoxChild, gtk_flow_box_child, GTK_TYPE_BIN)
/* Internal API {{{2 */
static GtkFlowBox *
gtk_flow_box_child_get_box (GtkFlowBoxChild *child)
{
GtkWidget *parent;
parent = gtk_widget_get_parent (GTK_WIDGET (child));
if (parent && GTK_IS_FLOW_BOX (parent))
return GTK_FLOW_BOX (parent);
return NULL;
}
static void
gtk_flow_box_child_set_focus (GtkFlowBoxChild *child)
{
GtkFlowBox *box = gtk_flow_box_child_get_box (child);
gboolean modify;
gboolean extend;
get_current_selection_modifiers (GTK_WIDGET (box), &modify, &extend);
if (modify)
gtk_flow_box_update_cursor (box, child);
else
gtk_flow_box_update_selection (box, child, FALSE, FALSE);
}
/* GtkWidget implementation {{{2 */
static gboolean
gtk_flow_box_child_focus (GtkWidget *widget,
GtkDirectionType direction)
{
gboolean had_focus = FALSE;
GtkWidget *child;
child = gtk_bin_get_child (GTK_BIN (widget));
g_object_get (widget, "has-focus", &had_focus, NULL);
if (had_focus)
{
/* If on row, going right, enter into possible container */
if (child &&
(direction == GTK_DIR_RIGHT || direction == GTK_DIR_TAB_FORWARD))
{
if (gtk_widget_child_focus (GTK_WIDGET (child), direction))
return TRUE;
}
return FALSE;
}
else if (gtk_container_get_focus_child (GTK_CONTAINER (widget)) != NULL)
{
/* Child has focus, always navigate inside it first */
if (gtk_widget_child_focus (child, direction))
return TRUE;
/* If exiting child container to the left, select child */
if (direction == GTK_DIR_LEFT || direction == GTK_DIR_TAB_BACKWARD)
{
gtk_flow_box_child_set_focus (GTK_FLOW_BOX_CHILD (widget));
return TRUE;
}
return FALSE;
}
else
{
/* If coming from the left, enter into possible container */
if (child &&
(direction == GTK_DIR_LEFT || direction == GTK_DIR_TAB_BACKWARD))
{
if (gtk_widget_child_focus (child, direction))
return TRUE;
}
gtk_flow_box_child_set_focus (GTK_FLOW_BOX_CHILD (widget));
return TRUE;
}
}
static void
gtk_flow_box_child_activate (GtkFlowBoxChild *child)
{
GtkFlowBox *box;
box = gtk_flow_box_child_get_box (child);
if (box)
gtk_flow_box_select_and_activate (box, child);
}
static gboolean
gtk_flow_box_child_draw (GtkWidget *widget,
cairo_t *cr)
{
GtkAllocation allocation = {0};
GtkStyleContext* context;
GtkStateFlags state;
GtkBorder border;
gtk_widget_get_allocation (widget, &allocation);
context = gtk_widget_get_style_context (widget);
state = gtk_widget_get_state_flags (widget);
gtk_render_background (context, cr, 0, 0, allocation.width, allocation.height);
gtk_render_frame (context, cr, 0, 0, allocation.width, allocation.height);
if (gtk_widget_has_visible_focus (widget))
{
gtk_style_context_get_border (context, state, &border);
gtk_render_focus (context, cr, border.left, border.top,
allocation.width - border.left - border.right,
allocation.height - border.top - border.bottom);
}
GTK_WIDGET_CLASS (gtk_flow_box_child_parent_class)->draw (widget, cr);
return TRUE;
}
/* Size allocation {{{3 */
static void
gtk_flow_box_child_get_full_border (GtkFlowBoxChild *child,
GtkBorder *full_border)
{
GtkWidget *widget = GTK_WIDGET (child);
GtkStyleContext *context;
GtkStateFlags state;
GtkBorder padding, border;
context = gtk_widget_get_style_context (widget);
state = gtk_style_context_get_state (context);
gtk_style_context_get_padding (context, state, &padding);
gtk_style_context_get_border (context, state, &border);
full_border->left = padding.left + border.left;
full_border->right = padding.right + border.right;
full_border->top = padding.top + border.top;
full_border->bottom = padding.bottom + border.bottom;
}
static GtkSizeRequestMode
gtk_flow_box_child_get_request_mode (GtkWidget *widget)
{
GtkFlowBox *box;
box = gtk_flow_box_child_get_box (GTK_FLOW_BOX_CHILD (widget));
if (box)
return gtk_widget_get_request_mode (GTK_WIDGET (box));
else
return GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH;
}
static void gtk_flow_box_child_get_preferred_width_for_height (GtkWidget *widget,
gint height,
gint *minimum_width,
gint *natural_width);
static void gtk_flow_box_child_get_preferred_height (GtkWidget *widget,
gint *minimum_height,
gint *natural_height);
static void
gtk_flow_box_child_get_preferred_height_for_width (GtkWidget *widget,
gint width,
gint *minimum_height,
gint *natural_height)
{
if (gtk_flow_box_child_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
{
GtkWidget *child;
gint child_min = 0, child_natural = 0;
GtkBorder full_border = { 0, };
gtk_flow_box_child_get_full_border (GTK_FLOW_BOX_CHILD (widget), &full_border);
child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
gtk_widget_get_preferred_height_for_width (child, width - full_border.left - full_border.right,
&child_min, &child_natural);
*minimum_height = full_border.top + child_min + full_border.bottom;
*natural_height = full_border.top + child_natural + full_border.bottom;
}
else
{
gtk_flow_box_child_get_preferred_height (widget, minimum_height, natural_height);
}
}
static void
gtk_flow_box_child_get_preferred_width (GtkWidget *widget,
gint *minimum_width,
gint *natural_width)
{
if (gtk_flow_box_child_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
{
GtkWidget *child;
gint child_min = 0, child_natural = 0;
GtkBorder full_border = { 0, };
gtk_flow_box_child_get_full_border (GTK_FLOW_BOX_CHILD (widget), &full_border);
child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
gtk_widget_get_preferred_width (child, &child_min, &child_natural);
*minimum_width = full_border.left + child_min + full_border.right;
*natural_width = full_border.left + child_natural + full_border.right;
}
else
{
gint natural_height;
gtk_flow_box_child_get_preferred_height (widget, NULL, &natural_height);
gtk_flow_box_child_get_preferred_width_for_height (widget, natural_height,
minimum_width, natural_width);
}
}
static void
gtk_flow_box_child_get_preferred_width_for_height (GtkWidget *widget,
gint height,
gint *minimum_width,
gint *natural_width)
{
if (gtk_flow_box_child_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
{
gtk_flow_box_child_get_preferred_width (widget, minimum_width, natural_width);
}
else
{
GtkWidget *child;
gint child_min = 0, child_natural = 0;
GtkBorder full_border = { 0, };
gtk_flow_box_child_get_full_border (GTK_FLOW_BOX_CHILD (widget), &full_border);
child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
gtk_widget_get_preferred_width_for_height (child, height - full_border.top - full_border.bottom,
&child_min, &child_natural);
*minimum_width = full_border.left + child_min + full_border.right;
*natural_width = full_border.left + child_natural + full_border.right;
}
}
static void
gtk_flow_box_child_get_preferred_height (GtkWidget *widget,
gint *minimum_height,
gint *natural_height)
{
if (gtk_flow_box_child_get_request_mode (widget) == GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH)
{
gint natural_width;
gtk_flow_box_child_get_preferred_width (widget, NULL, &natural_width);
gtk_flow_box_child_get_preferred_height_for_width (widget, natural_width,
minimum_height, natural_height);
}
else
{
GtkWidget *child;
gint child_min = 0, child_natural = 0;
GtkBorder full_border = { 0, };
gtk_flow_box_child_get_full_border (GTK_FLOW_BOX_CHILD (widget), &full_border);
child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
gtk_widget_get_preferred_height (child, &child_min, &child_natural);
*minimum_height = full_border.top + child_min + full_border.bottom;
*natural_height = full_border.top + child_natural + full_border.bottom;
}
}
static void
gtk_flow_box_child_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkWidget *child;
gtk_widget_set_allocation (widget, allocation);
child = gtk_bin_get_child (GTK_BIN (widget));
if (child && gtk_widget_get_visible (child))
{
GtkAllocation child_allocation;
GtkBorder border = { 0, };
gtk_flow_box_child_get_full_border (GTK_FLOW_BOX_CHILD (widget), &border);
child_allocation.x = allocation->x + border.left;
child_allocation.y = allocation->y + border.top;
child_allocation.width = allocation->width - border.left - border.right;
child_allocation.height = allocation->height - border.top - border.bottom;
child_allocation.width = MAX (1, child_allocation.width);
child_allocation.height = MAX (1, child_allocation.height);
gtk_widget_size_allocate (child, &child_allocation);
}
}
/* GObject implementation {{{2 */
static void
gtk_flow_box_child_class_init (GtkFlowBoxChildClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
widget_class->draw = gtk_flow_box_child_draw;
widget_class->get_request_mode = gtk_flow_box_child_get_request_mode;
widget_class->get_preferred_height = gtk_flow_box_child_get_preferred_height;
widget_class->get_preferred_height_for_width = gtk_flow_box_child_get_preferred_height_for_width;
widget_class->get_preferred_width = gtk_flow_box_child_get_preferred_width;
widget_class->get_preferred_width_for_height = gtk_flow_box_child_get_preferred_width_for_height;
widget_class->size_allocate = gtk_flow_box_child_size_allocate;
widget_class->focus = gtk_flow_box_child_focus;
class->activate = gtk_flow_box_child_activate;
/**
* GtkFlowBoxChild::activate:
* @child: The child on which the signal is emitted
*
* The ::activate signal is emitted when the user activates
* a child widget in a #GtkFlowBox, either by clicking or
* double-clicking, or by using the Space or Enter key.
*
* While this signal is used as a
* [keybinding signal][GtkBindingSignal],
* it can be used by applications for their own purposes.
*/
child_signals[CHILD_ACTIVATE] =
g_signal_new (I_("activate"),
G_OBJECT_CLASS_TYPE (object_class),
G_SIGNAL_RUN_FIRST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxChildClass, activate),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
widget_class->activate_signal = child_signals[CHILD_ACTIVATE];
gtk_widget_class_set_accessible_role (widget_class, ATK_ROLE_LIST_ITEM);
}
static void
gtk_flow_box_child_init (GtkFlowBoxChild *child)
{
GtkStyleContext *context;
gtk_widget_set_can_focus (GTK_WIDGET (child), TRUE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (child), TRUE);
context = gtk_widget_get_style_context (GTK_WIDGET (child));
gtk_style_context_add_class (context, "grid-child");
}
/* Public API {{{2 */
/**
* gtk_flow_box_child_new:
*
* Creates a new #GtkFlowBoxChild, to be used as a child
* of a #GtkFlowBox.
*
* Returns: a new #GtkFlowBoxChild
*
* Since: 3.12
*/
GtkWidget *
gtk_flow_box_child_new (void)
{
return g_object_new (GTK_TYPE_FLOW_BOX_CHILD, NULL);
}
/**
* gtk_flow_box_child_get_index:
* @child: a #GtkFlowBoxChild
*
* Gets the current index of the @child in its #GtkFlowBox container.
*
* Returns: the index of the @child, or -1 if the @child is not
* in a flow box.
*
* Since: 3.12
*/
gint
gtk_flow_box_child_get_index (GtkFlowBoxChild *child)
{
GtkFlowBoxChildPrivate *priv;
g_return_val_if_fail (GTK_IS_FLOW_BOX_CHILD (child), -1);
priv = CHILD_PRIV (child);
if (priv->iter != NULL)
return g_sequence_iter_get_position (priv->iter);
return -1;
}
/**
* gtk_flow_box_child_is_selected:
* @child: a #GtkFlowBoxChild
*
* Returns whether the @child is currently selected in its
* #GtkFlowBox container.
*
* Returns: %TRUE if @child is selected
*
* Since: 3.12
*/
gboolean
gtk_flow_box_child_is_selected (GtkFlowBoxChild *child)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX_CHILD (child), FALSE);
return CHILD_PRIV (child)->selected;
}
/**
* gtk_flow_box_child_changed:
* @child: a #GtkFlowBoxChild
*
* Marks @child as changed, causing any state that depends on this
* to be updated. This affects sorting and filtering.
*
* Note that calls to this method must be in sync with the data
* used for the sorting and filtering functions. For instance, if
* the list is mirroring some external data set, and *two* children
* changed in the external data set when you call
* gtk_flow_box_child_changed() on the first child, the sort function
* must only read the new data for the first of the two changed
* children, otherwise the resorting of the children will be wrong.
*
* This generally means that if you don’t fully control the data
* model, you have to duplicate the data that affects the sorting
* and filtering functions into the widgets themselves. Another
* alternative is to call gtk_flow_box_invalidate_sort() on any
* model change, but that is more expensive.
*
* Since: 3.12
*/
void
gtk_flow_box_child_changed (GtkFlowBoxChild *child)
{
GtkFlowBox *box;
g_return_if_fail (GTK_IS_FLOW_BOX_CHILD (child));
box = gtk_flow_box_child_get_box (child);
if (box == NULL)
return;
gtk_flow_box_apply_sort (box, child);
gtk_flow_box_apply_filter (box, child);
}
/* GtkFlowBox {{{1 */
/* Constants {{{2 */
#define DEFAULT_MAX_CHILDREN_PER_LINE 7
#define RUBBERBAND_START_DISTANCE 32
#define AUTOSCROLL_FAST_DISTANCE 32
#define AUTOSCROLL_FACTOR 20
#define AUTOSCROLL_FACTOR_FAST 10
/* GObject boilerplate {{{2 */
enum {
CHILD_ACTIVATED,
SELECTED_CHILDREN_CHANGED,
ACTIVATE_CURSOR_CHILD,
TOGGLE_CURSOR_CHILD,
MOVE_CURSOR,
SELECT_ALL,
UNSELECT_ALL,
LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };
enum {
PROP_0,
PROP_HOMOGENEOUS,
PROP_COLUMN_SPACING,
PROP_ROW_SPACING,
PROP_MIN_CHILDREN_PER_LINE,
PROP_MAX_CHILDREN_PER_LINE,
PROP_SELECTION_MODE,
PROP_ACTIVATE_ON_SINGLE_CLICK,
/* orientable */
PROP_ORIENTATION,
LAST_PROP = PROP_ORIENTATION
};
static GParamSpec *props[LAST_PROP] = { NULL, };
typedef struct _GtkFlowBoxPrivate GtkFlowBoxPrivate;
struct _GtkFlowBoxPrivate {
GtkOrientation orientation;
gboolean homogeneous;
guint row_spacing;
guint column_spacing;
GtkFlowBoxChild *prelight_child;
GtkFlowBoxChild *cursor_child;
GtkFlowBoxChild *selected_child;
gboolean active_child_active;
GtkFlowBoxChild *active_child;
GtkSelectionMode selection_mode;
GtkAdjustment *hadjustment;
GtkAdjustment *vadjustment;
gboolean activate_on_single_click;
guint16 min_children_per_line;
guint16 max_children_per_line;
guint16 cur_children_per_line;
GSequence *children;
GtkFlowBoxFilterFunc filter_func;
gpointer filter_data;
GDestroyNotify filter_destroy;
GtkFlowBoxSortFunc sort_func;
gpointer sort_data;
GDestroyNotify sort_destroy;
GtkGesture *multipress_gesture;
GtkGesture *drag_gesture;
gboolean rubberband_select;
GtkFlowBoxChild *rubberband_first;
GtkFlowBoxChild *rubberband_last;
gboolean rubberband_modify;
gboolean rubberband_extend;
GtkScrollType autoscroll_mode;
guint autoscroll_id;
};
#define BOX_PRIV(box) ((GtkFlowBoxPrivate*)gtk_flow_box_get_instance_private ((GtkFlowBox*)(box)))
G_DEFINE_TYPE_WITH_CODE (GtkFlowBox, gtk_flow_box, GTK_TYPE_CONTAINER,
G_ADD_PRIVATE (GtkFlowBox)
G_IMPLEMENT_INTERFACE (GTK_TYPE_ORIENTABLE, NULL))
/* Internal API, utilities {{{2 */
#define ORIENTATION_ALIGN(box) \
(BOX_PRIV(box)->orientation == GTK_ORIENTATION_HORIZONTAL \
? gtk_widget_get_halign (GTK_WIDGET (box)) \
: gtk_widget_get_valign (GTK_WIDGET (box)))
#define OPPOSING_ORIENTATION_ALIGN(box) \
(BOX_PRIV(box)->orientation == GTK_ORIENTATION_HORIZONTAL \
? gtk_widget_get_valign (GTK_WIDGET (box)) \
: gtk_widget_get_halign (GTK_WIDGET (box)))
/* Children are visible if they are shown by the app (visible)
* and not filtered out (child_visible) by the box
*/
static inline gboolean
child_is_visible (GtkWidget *child)
{
return gtk_widget_get_visible (child) &&
gtk_widget_get_child_visible (child);
}
static gint
get_visible_children (GtkFlowBox *box)
{
GSequenceIter *iter;
gint i = 0;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
child = g_sequence_get (iter);
if (child_is_visible (child))
i++;
}
return i;
}
static GtkFlowBoxChild *
gtk_flow_box_find_child_at_pos (GtkFlowBox *box,
gint x,
gint y)
{
GtkWidget *child;
GSequenceIter *iter;
GtkAllocation allocation;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
gtk_widget_get_allocation (child, &allocation);
if (x >= allocation.x && x < (allocation.x + allocation.width) &&
y >= allocation.y && y < (allocation.y + allocation.height))
return GTK_FLOW_BOX_CHILD (child);
}
return NULL;
}
static void
gtk_flow_box_update_prelight (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (child != priv->prelight_child)
{
priv->prelight_child = child;
gtk_widget_queue_draw (GTK_WIDGET (box));
}
}
static void
gtk_flow_box_update_active (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gboolean val;
val = priv->active_child == child;
if (priv->active_child != NULL &&
val != priv->active_child_active)
{
priv->active_child_active = val;
gtk_widget_queue_draw (GTK_WIDGET (box));
}
}
static void
gtk_flow_box_apply_filter (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gboolean do_show;
do_show = TRUE;
if (priv->filter_func != NULL)
do_show = priv->filter_func (child, priv->filter_data);
gtk_widget_set_child_visible (GTK_WIDGET (child), do_show);
}
static void
gtk_flow_box_apply_filter_all (GtkFlowBox *box)
{
GSequenceIter *iter;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkFlowBoxChild *child;
child = g_sequence_get (iter);
gtk_flow_box_apply_filter (box, child);
}
gtk_widget_queue_resize (GTK_WIDGET (box));
}
static void
gtk_flow_box_apply_sort (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
if (BOX_PRIV (box)->sort_func != NULL)
{
g_sequence_sort_changed (CHILD_PRIV (child)->iter,
(GCompareDataFunc)gtk_flow_box_sort, box);
gtk_widget_queue_resize (GTK_WIDGET (box));
}
}
/* Selection utilities {{{3 */
static gboolean
gtk_flow_box_child_set_selected (GtkFlowBoxChild *child,
gboolean selected)
{
if (CHILD_PRIV (child)->selected != selected)
{
CHILD_PRIV (child)->selected = selected;
if (selected)
gtk_widget_set_state_flags (GTK_WIDGET (child),
GTK_STATE_FLAG_SELECTED, FALSE);
else
gtk_widget_unset_state_flags (GTK_WIDGET (child),
GTK_STATE_FLAG_SELECTED);
gtk_widget_queue_draw (GTK_WIDGET (child));
return TRUE;
}
return FALSE;
}
static gboolean
gtk_flow_box_unselect_all_internal (GtkFlowBox *box)
{
GtkFlowBoxChild *child;
GSequenceIter *iter;
gboolean dirty = FALSE;
if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_NONE)
return FALSE;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
child = g_sequence_get (iter);
dirty |= gtk_flow_box_child_set_selected (child, FALSE);
}
return dirty;
}
static void
gtk_flow_box_unselect_child_internal (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
if (!CHILD_PRIV (child)->selected)
return;
if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_NONE)
return;
else if (BOX_PRIV (box)->selection_mode != GTK_SELECTION_MULTIPLE)
gtk_flow_box_unselect_all_internal (box);
else
gtk_flow_box_child_set_selected (child, FALSE);
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
static void
gtk_flow_box_update_cursor (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
BOX_PRIV (box)->cursor_child = child;
gtk_widget_grab_focus (GTK_WIDGET (child));
gtk_widget_queue_draw (GTK_WIDGET (child));
_gtk_flow_box_accessible_update_cursor (GTK_WIDGET (box), GTK_WIDGET (child));
}
static void
gtk_flow_box_select_child_internal (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
if (CHILD_PRIV (child)->selected)
return;
if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_NONE)
return;
if (BOX_PRIV (box)->selection_mode != GTK_SELECTION_MULTIPLE)
gtk_flow_box_unselect_all_internal (box);
gtk_flow_box_child_set_selected (child, TRUE);
BOX_PRIV (box)->selected_child = child;
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
static void
gtk_flow_box_select_all_between (GtkFlowBox *box,
GtkFlowBoxChild *child1,
GtkFlowBoxChild *child2,
gboolean modify)
{
GSequenceIter *iter, *iter1, *iter2;
if (child1)
iter1 = CHILD_PRIV (child1)->iter;
else
iter1 = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
if (child2)
iter2 = CHILD_PRIV (child2)->iter;
else
iter2 = g_sequence_get_end_iter (BOX_PRIV (box)->children);
if (g_sequence_iter_compare (iter2, iter1) < 0)
{
iter = iter1;
iter1 = iter2;
iter2 = iter;
}
for (iter = iter1;
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
child = g_sequence_get (iter);
if (child_is_visible (child))
{
if (modify)
gtk_flow_box_child_set_selected (GTK_FLOW_BOX_CHILD (child), !CHILD_PRIV (child)->selected);
else
gtk_flow_box_child_set_selected (GTK_FLOW_BOX_CHILD (child), TRUE);
}
if (g_sequence_iter_compare (iter, iter2) == 0)
break;
}
}
static void
gtk_flow_box_update_selection (GtkFlowBox *box,
GtkFlowBoxChild *child,
gboolean modify,
gboolean extend)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gtk_flow_box_update_cursor (box, child);
if (priv->selection_mode == GTK_SELECTION_NONE)
return;
if (priv->selection_mode == GTK_SELECTION_BROWSE)
{
gtk_flow_box_unselect_all_internal (box);
gtk_flow_box_child_set_selected (child, TRUE);
priv->selected_child = child;
}
else if (priv->selection_mode == GTK_SELECTION_SINGLE)
{
gboolean was_selected;
was_selected = CHILD_PRIV (child)->selected;
gtk_flow_box_unselect_all_internal (box);
gtk_flow_box_child_set_selected (child, modify ? !was_selected : TRUE);
priv->selected_child = CHILD_PRIV (child)->selected ? child : NULL;
}
else /* GTK_SELECTION_MULTIPLE */
{
if (extend)
{
gtk_flow_box_unselect_all_internal (box);
if (priv->selected_child == NULL)
{
gtk_flow_box_child_set_selected (child, TRUE);
priv->selected_child = child;
}
else
gtk_flow_box_select_all_between (box, priv->selected_child, child, FALSE);
}
else
{
if (modify)
{
gtk_flow_box_child_set_selected (child, !CHILD_PRIV (child)->selected);
}
else
{
gtk_flow_box_unselect_all_internal (box);
gtk_flow_box_child_set_selected (child, !CHILD_PRIV (child)->selected);
priv->selected_child = child;
}
}
}
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
static void
gtk_flow_box_select_and_activate (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
if (child != NULL)
{
gtk_flow_box_select_child_internal (box, child);
gtk_flow_box_update_cursor (box, child);
g_signal_emit (box, signals[CHILD_ACTIVATED], 0, child);
}
}
/* Focus utilities {{{3 */
static GSequenceIter *
gtk_flow_box_get_previous_focusable (GtkFlowBox *box,
GSequenceIter *iter)
{
GtkFlowBoxChild *child;
while (!g_sequence_iter_is_begin (iter))
{
iter = g_sequence_iter_prev (iter);
child = g_sequence_get (iter);
if (child_is_visible (GTK_WIDGET (child)) &&
gtk_widget_is_sensitive (GTK_WIDGET (child)))
return iter;
}
return NULL;
}
static GSequenceIter *
gtk_flow_box_get_next_focusable (GtkFlowBox *box,
GSequenceIter *iter)
{
GtkFlowBoxChild *child;
while (TRUE)
{
iter = g_sequence_iter_next (iter);
if (g_sequence_iter_is_end (iter))
return NULL;
child = g_sequence_get (iter);
if (child_is_visible (GTK_WIDGET (child)) &&
gtk_widget_is_sensitive (GTK_WIDGET (child)))
return iter;
}
return NULL;
}
static GSequenceIter *
gtk_flow_box_get_first_focusable (GtkFlowBox *box)
{
GSequenceIter *iter;
GtkFlowBoxChild *child;
iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
if (g_sequence_iter_is_end (iter))
return NULL;
child = g_sequence_get (iter);
if (child_is_visible (GTK_WIDGET (child)) &&
gtk_widget_is_sensitive (GTK_WIDGET (child)))
return iter;
return gtk_flow_box_get_next_focusable (box, iter);
}
static GSequenceIter *
gtk_flow_box_get_last_focusable (GtkFlowBox *box)
{
GSequenceIter *iter;
iter = g_sequence_get_end_iter (BOX_PRIV (box)->children);
return gtk_flow_box_get_previous_focusable (box, iter);
}
static GSequenceIter *
gtk_flow_box_get_above_focusable (GtkFlowBox *box,
GSequenceIter *iter)
{
GtkFlowBoxChild *child = NULL;
gint i;
while (TRUE)
{
i = 0;
while (i < BOX_PRIV (box)->cur_children_per_line)
{
if (g_sequence_iter_is_begin (iter))
return NULL;
iter = g_sequence_iter_prev (iter);
child = g_sequence_get (iter);
if (child_is_visible (GTK_WIDGET (child)))
i++;
}
if (child && gtk_widget_get_sensitive (GTK_WIDGET (child)))
return iter;
}
return NULL;
}
static GSequenceIter *
gtk_flow_box_get_below_focusable (GtkFlowBox *box,
GSequenceIter *iter)
{
GtkFlowBoxChild *child = NULL;
gint i;
while (TRUE)
{
i = 0;
while (i < BOX_PRIV (box)->cur_children_per_line)
{
iter = g_sequence_iter_next (iter);
if (g_sequence_iter_is_end (iter))
return NULL;
child = g_sequence_get (iter);
if (child_is_visible (GTK_WIDGET (child)))
i++;
}
if (child && gtk_widget_get_sensitive (GTK_WIDGET (child)))
return iter;
}
return NULL;
}
/* GtkWidget implementation {{{2 */
/* Size allocation {{{3 */
/* Used in columned modes where all items share at least their
* equal widths or heights
*/
static void
get_max_item_size (GtkFlowBox *box,
GtkOrientation orientation,
gint *min_size,
gint *nat_size)
{
GSequenceIter *iter;
gint max_min_size = 0;
gint max_nat_size = 0;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
gint child_min, child_nat;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_get_preferred_width (child, &child_min, &child_nat);
else
gtk_widget_get_preferred_height (child, &child_min, &child_nat);
max_min_size = MAX (max_min_size, child_min);
max_nat_size = MAX (max_nat_size, child_nat);
}
if (min_size)
*min_size = max_min_size;
if (nat_size)
*nat_size = max_nat_size;
}
/* Gets the largest minimum/natural size for a given size (used to get
* the largest item heights for a fixed item width and the opposite)
*/
static void
get_largest_size_for_opposing_orientation (GtkFlowBox *box,
GtkOrientation orientation,
gint item_size,
gint *min_item_size,
gint *nat_item_size)
{
GSequenceIter *iter;
gint max_min_size = 0;
gint max_nat_size = 0;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
gint child_min, child_nat;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_get_preferred_height_for_width (child,
item_size,
&child_min, &child_nat);
else
gtk_widget_get_preferred_width_for_height (child,
item_size,
&child_min, &child_nat);
max_min_size = MAX (max_min_size, child_min);
max_nat_size = MAX (max_nat_size, child_nat);
}
if (min_item_size)
*min_item_size = max_min_size;
if (nat_item_size)
*nat_item_size = max_nat_size;
}
/* Gets the largest minimum/natural size on a single line for a given size
* (used to get the largest line heights for a fixed item width and the opposite
* while iterating over a list of children, note the new index is returned)
*/
static GSequenceIter *
get_largest_size_for_line_in_opposing_orientation (GtkFlowBox *box,
GtkOrientation orientation,
GSequenceIter *cursor,
gint line_length,
GtkRequestedSize *item_sizes,
gint extra_pixels,
gint *min_item_size,
gint *nat_item_size)
{
GSequenceIter *iter;
gint max_min_size = 0;
gint max_nat_size = 0;
gint i;
i = 0;
for (iter = cursor;
!g_sequence_iter_is_end (iter) && i < line_length;
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
gint child_min, child_nat, this_item_size;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
/* Distribute the extra pixels to the first children in the line
* (could be fancier and spread them out more evenly) */
this_item_size = item_sizes[i].minimum_size;
if (extra_pixels > 0 && ORIENTATION_ALIGN (box) == GTK_ALIGN_FILL)
{
this_item_size++;
extra_pixels--;
}
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_get_preferred_height_for_width (child,
this_item_size,
&child_min, &child_nat);
else
gtk_widget_get_preferred_width_for_height (child,
this_item_size,
&child_min, &child_nat);
max_min_size = MAX (max_min_size, child_min);
max_nat_size = MAX (max_nat_size, child_nat);
i++;
}
if (min_item_size)
*min_item_size = max_min_size;
if (nat_item_size)
*nat_item_size = max_nat_size;
/* Return next item in the list */
return iter;
}
/* fit_aligned_item_requests() helper */
static gint
gather_aligned_item_requests (GtkFlowBox *box,
GtkOrientation orientation,
gint line_length,
gint item_spacing,
gint n_children,
GtkRequestedSize *item_sizes)
{
GSequenceIter *iter;
gint i;
gint extra_items, natural_line_size = 0;
extra_items = n_children % line_length;
i = 0;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
GtkAlign item_align;
gint child_min, child_nat;
gint position;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_get_preferred_width (child,
&child_min, &child_nat);
else
gtk_widget_get_preferred_height (child,
&child_min, &child_nat);
/* Get the index and push it over for the last line when spreading to the end */
position = i % line_length;
item_align = ORIENTATION_ALIGN (box);
if (item_align == GTK_ALIGN_END && i >= n_children - extra_items)
position += line_length - extra_items;
/* Round up the size of every column/row */
item_sizes[position].minimum_size = MAX (item_sizes[position].minimum_size, child_min);
item_sizes[position].natural_size = MAX (item_sizes[position].natural_size, child_nat);
i++;
}
for (i = 0; i < line_length; i++)
natural_line_size += item_sizes[i].natural_size;
natural_line_size += (line_length - 1) * item_spacing;
return natural_line_size;
}
static GtkRequestedSize *
fit_aligned_item_requests (GtkFlowBox *box,
GtkOrientation orientation,
gint avail_size,
gint item_spacing,
gint *line_length, /* in-out */
gint items_per_line,
gint n_children)
{
GtkRequestedSize *sizes, *try_sizes;
gint try_line_size, try_length;
sizes = g_new0 (GtkRequestedSize, *line_length);
/* get the sizes for the initial guess */
try_line_size = gather_aligned_item_requests (box,
orientation,
*line_length,
item_spacing,
n_children,
sizes);
/* Try columnizing the whole thing and adding an item to the end of
* the line; try to fit as many columns into the available size as
* possible
*/
for (try_length = *line_length + 1; try_line_size < avail_size; try_length++)
{
try_sizes = g_new0 (GtkRequestedSize, try_length);
try_line_size = gather_aligned_item_requests (box,
orientation,
try_length,
item_spacing,
n_children,
try_sizes);
if (try_line_size <= avail_size &&
items_per_line >= try_length)
{
*line_length = try_length;
g_free (sizes);
sizes = try_sizes;
}
else
{
/* oops, this one failed; stick to the last size that fit and then return */
g_free (try_sizes);
break;
}
}
return sizes;
}
typedef struct {
GArray *requested;
gint extra_pixels;
} AllocatedLine;
static gint
get_offset_pixels (GtkAlign align,
gint pixels)
{
gint offset;
switch (align) {
case GTK_ALIGN_START:
case GTK_ALIGN_FILL:
offset = 0;
break;
case GTK_ALIGN_CENTER:
offset = pixels / 2;
break;
case GTK_ALIGN_END:
offset = pixels;
break;
default:
g_assert_not_reached ();
break;
}
return offset;
}
static void
gtk_flow_box_size_allocate (GtkWidget *widget,
GtkAllocation *allocation)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkAllocation child_allocation;
gint avail_size, avail_other_size, min_items, item_spacing, line_spacing;
GtkAlign item_align;
GtkAlign line_align;
GdkWindow *window;
GtkRequestedSize *line_sizes = NULL;
GtkRequestedSize *item_sizes = NULL;
gint min_item_size, nat_item_size;
gint line_length;
gint item_size = 0;
gint line_size = 0, min_fixed_line_size = 0, nat_fixed_line_size = 0;
gint line_offset, item_offset, n_children, n_lines, line_count;
gint extra_pixels = 0, extra_per_item = 0, extra_extra = 0;
gint extra_line_pixels = 0, extra_per_line = 0, extra_line_extra = 0;
gint i, this_line_size;
GSequenceIter *iter;
child_allocation.x = 0;
child_allocation.y = 0;
child_allocation.width = 0;
child_allocation.height = 0;
gtk_widget_set_allocation (widget, allocation);
window = gtk_widget_get_window (widget);
if (window != NULL)
gdk_window_move_resize (window,
allocation->x, allocation->y,
allocation->width, allocation->height);
child_allocation.x = 0;
child_allocation.y = 0;
child_allocation.width = allocation->width;
min_items = MAX (1, priv->min_children_per_line);
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
avail_size = allocation->width;
avail_other_size = allocation->height;
item_spacing = priv->column_spacing; line_spacing = priv->row_spacing;
}
else /* GTK_ORIENTATION_VERTICAL */
{
avail_size = allocation->height;
avail_other_size = allocation->width;
item_spacing = priv->row_spacing;
line_spacing = priv->column_spacing;
}
item_align = ORIENTATION_ALIGN (box);
line_align = OPPOSING_ORIENTATION_ALIGN (box);
/* Get how many lines we'll be needing to flow */
n_children = get_visible_children (box);
if (n_children <= 0)
return;
/*
* Deal with ALIGNED/HOMOGENEOUS modes first, start with
* initial guesses at item/line sizes
*/
get_max_item_size (box, priv->orientation, &min_item_size, &nat_item_size);
if (nat_item_size <= 0)
return;
/* By default flow at the natural item width */
line_length = avail_size / (nat_item_size + item_spacing);
/* After the above aproximation, check if we cant fit one more on the line */
if (line_length * item_spacing + (line_length + 1) * nat_item_size <= avail_size)
line_length++;
/* Its possible we were allocated just less than the natural width of the
* minimum item flow length */
line_length = MAX (min_items, line_length);
line_length = MIN (line_length, priv->max_children_per_line);
/* Here we just use the largest height-for-width and use that for the height
* of all lines */
if (priv->homogeneous)
{
n_lines = n_children / line_length;
if ((n_children % line_length) > 0)
n_lines++;
n_lines = MAX (n_lines, 1);
/* Now we need the real item allocation size */
item_size = (avail_size - (line_length - 1) * item_spacing) / line_length;
/* Cut out the expand space if we're not distributing any */
if (item_align != GTK_ALIGN_FILL)
item_size = MIN (item_size, nat_item_size);
get_largest_size_for_opposing_orientation (box,
priv->orientation,
item_size,
&min_fixed_line_size,
&nat_fixed_line_size);
/* resolve a fixed 'line_size' */
line_size = (avail_other_size - (n_lines - 1) * line_spacing) / n_lines;
if (line_align != GTK_ALIGN_FILL)
line_size = MIN (line_size, nat_fixed_line_size);
/* Get the real extra pixels incase of GTK_ALIGN_START lines */
extra_pixels = avail_size - (line_length - 1) * item_spacing - item_size * line_length;
extra_line_pixels = avail_other_size - (n_lines - 1) * line_spacing - line_size * n_lines;
}
else
{
gboolean first_line = TRUE;
/* Find the amount of columns that can fit aligned into the available space
* and collect their requests.
*/
item_sizes = fit_aligned_item_requests (box,
priv->orientation,
avail_size,
item_spacing,
&line_length,
priv->max_children_per_line,
n_children);
/* Calculate the number of lines after determining the final line_length */
n_lines = n_children / line_length;
if ((n_children % line_length) > 0)
n_lines++;
n_lines = MAX (n_lines, 1);
line_sizes = g_new0 (GtkRequestedSize, n_lines);
/* Get the available remaining size */
avail_size -= (line_length - 1) * item_spacing;
for (i = 0; i < line_length; i++)
avail_size -= item_sizes[i].minimum_size;
/* Perform a natural allocation on the columnized items and get the remaining pixels */
if (avail_size > 0)
extra_pixels = gtk_distribute_natural_allocation (avail_size, line_length, item_sizes);
/* Now that we have the size of each column of items find the size of each individual
* line based on the aligned item sizes.
*/
for (i = 0, iter = g_sequence_get_begin_iter (priv->children);
!g_sequence_iter_is_end (iter) && i < n_lines;
i++)
{
iter = get_largest_size_for_line_in_opposing_orientation (box,
priv->orientation,
iter,
line_length,
item_sizes,
extra_pixels,
&line_sizes[i].minimum_size,
&line_sizes[i].natural_size);
/* Its possible a line is made of completely invisible children */
if (line_sizes[i].natural_size > 0)
{
if (first_line)
first_line = FALSE;
else
avail_other_size -= line_spacing;
avail_other_size -= line_sizes[i].minimum_size;
line_sizes[i].data = GINT_TO_POINTER (i);
}
}
/* Distribute space among lines naturally */
if (avail_other_size > 0)
extra_line_pixels = gtk_distribute_natural_allocation (avail_other_size, n_lines, line_sizes);
}
/*
* Initial sizes of items/lines guessed at this point,
* go on to distribute expand space if needed.
*/
priv->cur_children_per_line = line_length;
/* FIXME: This portion needs to consider which columns
* and rows asked for expand space and distribute those
* accordingly for the case of ALIGNED allocation.
*
* If at least one child in a column/row asked for expand;
* we should make that row/column expand entirely.
*/
/* Calculate expand space per item */
if (item_align == GTK_ALIGN_FILL)
{
extra_per_item = extra_pixels / line_length;
extra_extra = extra_pixels % line_length;
}
/* Calculate expand space per line */
if (line_align == GTK_ALIGN_FILL)
{
extra_per_line = extra_line_pixels / n_lines;
extra_line_extra = extra_line_pixels % n_lines;
}
/*
* Prepare item/line initial offsets and jump into the
* real allocation loop.
*/
line_offset = item_offset = 0;
/* prepend extra space to item_offset/line_offset for SPREAD_END */
item_offset += get_offset_pixels (item_align, extra_pixels);
line_offset += get_offset_pixels (line_align, extra_line_pixels);
/* Get the allocation size for the first line */
if (priv->homogeneous)
this_line_size = line_size;
else
{
this_line_size = line_sizes[0].minimum_size;
if (line_align == GTK_ALIGN_FILL)
{
this_line_size += extra_per_line;
if (extra_line_extra > 0)
this_line_size++;
}
}
i = 0;
line_count = 0;
for (iter = g_sequence_get_begin_iter (priv->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
gint position;
gint this_item_size;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
/* Get item position */
position = i % line_length;
/* adjust the line_offset/count at the beginning of each new line */
if (i > 0 && position == 0)
{
/* Push the line_offset */
line_offset += this_line_size + line_spacing;
line_count++;
/* Get the new line size */
if (priv->homogeneous)
this_line_size = line_size;
else
{
this_line_size = line_sizes[line_count].minimum_size;
if (line_align == GTK_ALIGN_FILL)
{
this_line_size += extra_per_line;
if (line_count < extra_line_extra)
this_line_size++;
}
}
item_offset = 0;
if (item_align == GTK_ALIGN_CENTER)
{
item_offset += get_offset_pixels (item_align, extra_pixels);
}
else if (item_align == GTK_ALIGN_END)
{
item_offset += get_offset_pixels (item_align, extra_pixels);
/* If we're on the last line, prepend the space for
* any leading items */
if (line_count == n_lines -1)
{
gint extra_items = n_children % line_length;
if (priv->homogeneous)
{
item_offset += item_size * (line_length - extra_items);
item_offset += item_spacing * (line_length - extra_items);
}
else
{
gint j;
for (j = 0; j < (line_length - extra_items); j++)
{
item_offset += item_sizes[j].minimum_size;
item_offset += item_spacing;
}
}
}
}
}
/* Push the index along for the last line when spreading to the end */
if (item_align == GTK_ALIGN_END && line_count == n_lines -1)
{
gint extra_items = n_children % line_length;
position += line_length - extra_items;
}
if (priv->homogeneous)
this_item_size = item_size;
else
this_item_size = item_sizes[position].minimum_size;
if (item_align == GTK_ALIGN_FILL)
{
this_item_size += extra_per_item;
if (position < extra_extra)
this_item_size++;
}
/* Do the actual allocation */
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
child_allocation.x = item_offset;
child_allocation.y = line_offset;
child_allocation.width = this_item_size;
child_allocation.height = this_line_size;
}
else /* GTK_ORIENTATION_VERTICAL */
{
child_allocation.x = line_offset;
child_allocation.y = item_offset;
child_allocation.width = this_line_size;
child_allocation.height = this_item_size;
}
if (gtk_widget_get_direction (widget) == GTK_TEXT_DIR_RTL)
child_allocation.x = allocation->x + allocation->width - (child_allocation.x - allocation->x) - child_allocation.width;
gtk_widget_size_allocate (child, &child_allocation);
item_offset += this_item_size;
item_offset += item_spacing;
i++;
}
g_free (item_sizes);
g_free (line_sizes);
}
static GtkSizeRequestMode
gtk_flow_box_get_request_mode (GtkWidget *widget)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
return (BOX_PRIV (box)->orientation == GTK_ORIENTATION_HORIZONTAL) ?
GTK_SIZE_REQUEST_HEIGHT_FOR_WIDTH : GTK_SIZE_REQUEST_WIDTH_FOR_HEIGHT;
}
/* Gets the largest minimum and natural length of
* 'line_length' consecutive items when aligned into rows/columns */
static void
get_largest_aligned_line_length (GtkFlowBox *box,
GtkOrientation orientation,
gint line_length,
gint *min_size,
gint *nat_size)
{
GSequenceIter *iter;
gint max_min_size = 0;
gint max_nat_size = 0;
gint spacing, i;
GtkRequestedSize *aligned_item_sizes;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
spacing = BOX_PRIV (box)->column_spacing;
else
spacing = BOX_PRIV (box)->row_spacing;
aligned_item_sizes = g_new0 (GtkRequestedSize, line_length);
/* Get the largest sizes of each index in the line.
*/
i = 0;
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
gint child_min, child_nat;
child = g_sequence_get (iter);
if (!child_is_visible (child))
continue;
if (orientation == GTK_ORIENTATION_HORIZONTAL)
gtk_widget_get_preferred_width (child,
&child_min, &child_nat);
else /* GTK_ORIENTATION_VERTICAL */
gtk_widget_get_preferred_height (child,
&child_min, &child_nat);
aligned_item_sizes[i % line_length].minimum_size =
MAX (aligned_item_sizes[i % line_length].minimum_size, child_min);
aligned_item_sizes[i % line_length].natural_size =
MAX (aligned_item_sizes[i % line_length].natural_size, child_nat);
i++;
}
/* Add up the largest indexes */
for (i = 0; i < line_length; i++)
{
max_min_size += aligned_item_sizes[i].minimum_size;
max_nat_size += aligned_item_sizes[i].natural_size;
}
g_free (aligned_item_sizes);
max_min_size += (line_length - 1) * spacing;
max_nat_size += (line_length - 1) * spacing;
if (min_size)
*min_size = max_min_size;
if (nat_size)
*nat_size = max_nat_size;
}
static void
gtk_flow_box_get_preferred_width (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gint min_item_width, nat_item_width;
gint min_items, nat_items;
gint min_width, nat_width;
min_items = MAX (1, priv->min_children_per_line);
nat_items = MAX (min_items, priv->max_children_per_line);
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
min_width = nat_width = 0;
if (!priv->homogeneous)
{
/* When not homogeneous; horizontally oriented boxes
* need enough width for the widest row */
if (min_items == 1)
{
get_max_item_size (box,
GTK_ORIENTATION_HORIZONTAL,
&min_item_width,
&nat_item_width);
min_width += min_item_width;
nat_width += nat_item_width;
}
else
{
gint min_line_length, nat_line_length;
get_largest_aligned_line_length (box,
GTK_ORIENTATION_HORIZONTAL,
min_items,
&min_line_length,
&nat_line_length);
if (nat_items > min_items)
get_largest_aligned_line_length (box,
GTK_ORIENTATION_HORIZONTAL,
nat_items,
NULL,
&nat_line_length);
min_width += min_line_length;
nat_width += nat_line_length;
}
}
else /* In homogeneous mode; horizontally oriented boxs
* give the same width to all children */
{
get_max_item_size (box, GTK_ORIENTATION_HORIZONTAL,
&min_item_width, &nat_item_width);
min_width += min_item_width * min_items;
min_width += (min_items -1) * priv->column_spacing;
nat_width += nat_item_width * nat_items;
nat_width += (nat_items -1) * priv->column_spacing;
}
}
else /* GTK_ORIENTATION_VERTICAL */
{
/* Return the width for the minimum height */
gint min_height, nat_height;
GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_height, &nat_height);
GTK_WIDGET_GET_CLASS (widget)->get_preferred_width_for_height (widget,
min_height,
&min_width,
&nat_width);
}
if (minimum_size)
*minimum_size = min_width;
if (natural_size)
*natural_size = nat_width;
}
static void
gtk_flow_box_get_preferred_height (GtkWidget *widget,
gint *minimum_size,
gint *natural_size)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gint min_item_height, nat_item_height;
gint min_items, nat_items;
gint min_height, nat_height;
min_items = MAX (1, priv->min_children_per_line);
nat_items = MAX (min_items, priv->max_children_per_line);
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
/* Return the height for the minimum width */
gint min_width, nat_width;
GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, &nat_width);
GTK_WIDGET_GET_CLASS (widget)->get_preferred_height_for_width (widget,
min_width,
&min_height,
&nat_height);
}
else /* GTK_ORIENTATION_VERTICAL */
{
min_height = nat_height = 0;
if (! priv->homogeneous)
{
/* When not homogeneous; vertically oriented boxes
* need enough height for the tallest column */
if (min_items == 1)
{
get_max_item_size (box, GTK_ORIENTATION_VERTICAL,
&min_item_height, &nat_item_height);
min_height += min_item_height;
nat_height += nat_item_height;
}
else
{
gint min_line_length, nat_line_length;
get_largest_aligned_line_length (box,
GTK_ORIENTATION_VERTICAL,
min_items,
&min_line_length,
&nat_line_length);
if (nat_items > min_items)
get_largest_aligned_line_length (box,
GTK_ORIENTATION_VERTICAL,
nat_items,
NULL,
&nat_line_length);
min_height += min_line_length;
nat_height += nat_line_length;
}
}
else
{
/* In homogeneous mode; vertically oriented boxes
* give the same height to all children
*/
get_max_item_size (box,
GTK_ORIENTATION_VERTICAL,
&min_item_height,
&nat_item_height);
min_height += min_item_height * min_items;
min_height += (min_items -1) * priv->row_spacing;
nat_height += nat_item_height * nat_items;
nat_height += (nat_items -1) * priv->row_spacing;
}
}
if (minimum_size)
*minimum_size = min_height;
if (natural_size)
*natural_size = nat_height;
}
static void
gtk_flow_box_get_preferred_height_for_width (GtkWidget *widget,
gint width,
gint *minimum_height,
gint *natural_height)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gint min_item_width, nat_item_width;
gint min_items;
gint min_height, nat_height;
gint avail_size, n_children;
min_items = MAX (1, priv->min_children_per_line);
min_height = 0;
nat_height = 0;
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
gint min_width, nat_width;
gint line_length;
gint item_size, extra_pixels;
n_children = get_visible_children (box);
if (n_children <= 0)
goto out;
/* Make sure its no smaller than the minimum */
GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, &nat_width);
avail_size = MAX (width, min_width);
if (avail_size <= 0)
goto out;
get_max_item_size (box, GTK_ORIENTATION_HORIZONTAL, &min_item_width, &nat_item_width);
if (nat_item_width <= 0)
goto out;
/* By default flow at the natural item width */
line_length = avail_size / (nat_item_width + priv->column_spacing);
/* After the above aproximation, check if we cant fit one more on the line */
if (line_length * priv->column_spacing + (line_length + 1) * nat_item_width <= avail_size)
line_length++;
/* Its possible we were allocated just less than the natural width of the
* minimum item flow length
*/
line_length = MAX (min_items, line_length);
line_length = MIN (line_length, priv->max_children_per_line);
/* Now we need the real item allocation size */
item_size = (avail_size - (line_length - 1) * priv->column_spacing) / line_length;
/* Cut out the expand space if we're not distributing any */
if (gtk_widget_get_halign (widget) != GTK_ALIGN_FILL)
{
item_size = MIN (item_size, nat_item_width);
extra_pixels = 0;
}
else
/* Collect the extra pixels for expand children */
extra_pixels = (avail_size - (line_length - 1) * priv->column_spacing) % line_length;
if (priv->homogeneous)
{
gint min_item_height, nat_item_height;
gint lines;
/* Here we just use the largest height-for-width and
* add up the size accordingly
*/
get_largest_size_for_opposing_orientation (box,
GTK_ORIENTATION_HORIZONTAL,
item_size,
&min_item_height,
&nat_item_height);
/* Round up how many lines we need to allocate for */
lines = n_children / line_length;
if ((n_children % line_length) > 0)
lines++;
min_height = min_item_height * lines;
nat_height = nat_item_height * lines;
min_height += (lines - 1) * priv->row_spacing;
nat_height += (lines - 1) * priv->row_spacing;
}
else
{
gint min_line_height, nat_line_height, i;
gboolean first_line = TRUE;
GtkRequestedSize *item_sizes;
GSequenceIter *iter;
/* First get the size each set of items take to span the line
* when aligning the items above and below after flowping.
*/
item_sizes = fit_aligned_item_requests (box,
priv->orientation,
avail_size,
priv->column_spacing,
&line_length,
priv->max_children_per_line,
n_children);
/* Get the available remaining size */
avail_size -= (line_length - 1) * priv->column_spacing;
for (i = 0; i < line_length; i++)
avail_size -= item_sizes[i].minimum_size;
if (avail_size > 0)
extra_pixels = gtk_distribute_natural_allocation (avail_size, line_length, item_sizes);
for (iter = g_sequence_get_begin_iter (priv->children);
!g_sequence_iter_is_end (iter);)
{
iter = get_largest_size_for_line_in_opposing_orientation (box,
GTK_ORIENTATION_HORIZONTAL,
iter,
line_length,
item_sizes,
extra_pixels,
&min_line_height,
&nat_line_height);
/* Its possible the line only had invisible widgets */
if (nat_line_height > 0)
{
if (first_line)
first_line = FALSE;
else
{
min_height += priv->row_spacing;
nat_height += priv->row_spacing;
}
min_height += min_line_height;
nat_height += nat_line_height;
}
}
g_free (item_sizes);
}
}
else /* GTK_ORIENTATION_VERTICAL */
{
/* Return the minimum height */
GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_height, &nat_height);
}
out:
if (minimum_height)
*minimum_height = min_height;
if (natural_height)
*natural_height = nat_height;
}
static void
gtk_flow_box_get_preferred_width_for_height (GtkWidget *widget,
gint height,
gint *minimum_width,
gint *natural_width)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gint min_item_height, nat_item_height;
gint min_items;
gint min_width, nat_width;
gint avail_size, n_children;
min_items = MAX (1, priv->min_children_per_line);
min_width = 0;
nat_width = 0;
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
{
/* Return the minimum width */
GTK_WIDGET_GET_CLASS (widget)->get_preferred_width (widget, &min_width, &nat_width);
}
else /* GTK_ORIENTATION_VERTICAL */
{
gint min_height, nat_height;
gint line_length;
gint item_size, extra_pixels;
n_children = get_visible_children (box);
if (n_children <= 0)
goto out;
/* Make sure its no smaller than the minimum */
GTK_WIDGET_GET_CLASS (widget)->get_preferred_height (widget, &min_height, &nat_height);
avail_size = MAX (height, min_height);
if (avail_size <= 0)
goto out;
get_max_item_size (box, GTK_ORIENTATION_VERTICAL, &min_item_height, &nat_item_height);
/* By default flow at the natural item width */
line_length = avail_size / (nat_item_height + priv->row_spacing);
/* After the above aproximation, check if we cant fit one more on the line */
if (line_length * priv->row_spacing + (line_length + 1) * nat_item_height <= avail_size)
line_length++;
/* Its possible we were allocated just less than the natural width of the
* minimum item flow length
*/
line_length = MAX (min_items, line_length);
line_length = MIN (line_length, priv->max_children_per_line);
/* Now we need the real item allocation size */
item_size = (avail_size - (line_length - 1) * priv->row_spacing) / line_length;
/* Cut out the expand space if we're not distributing any */
if (gtk_widget_get_valign (widget) != GTK_ALIGN_FILL)
{
item_size = MIN (item_size, nat_item_height);
extra_pixels = 0;
}
else
/* Collect the extra pixels for expand children */
extra_pixels = (avail_size - (line_length - 1) * priv->row_spacing) % line_length;
if (priv->homogeneous)
{
gint min_item_width, nat_item_width;
gint lines;
/* Here we just use the largest height-for-width and
* add up the size accordingly
*/
get_largest_size_for_opposing_orientation (box,
GTK_ORIENTATION_VERTICAL,
item_size,
&min_item_width,
&nat_item_width);
/* Round up how many lines we need to allocate for */
n_children = get_visible_children (box);
lines = n_children / line_length;
if ((n_children % line_length) > 0)
lines++;
min_width = min_item_width * lines;
nat_width = nat_item_width * lines;
min_width += (lines - 1) * priv->column_spacing;
nat_width += (lines - 1) * priv->column_spacing;
}
else
{
gint min_line_width, nat_line_width, i;
gboolean first_line = TRUE;
GtkRequestedSize *item_sizes;
GSequenceIter *iter;
/* First get the size each set of items take to span the line
* when aligning the items above and below after flowping.
*/
item_sizes = fit_aligned_item_requests (box,
priv->orientation,
avail_size,
priv->row_spacing,
&line_length,
priv->max_children_per_line,
n_children);
/* Get the available remaining size */
avail_size -= (line_length - 1) * priv->column_spacing;
for (i = 0; i < line_length; i++)
avail_size -= item_sizes[i].minimum_size;
if (avail_size > 0)
extra_pixels = gtk_distribute_natural_allocation (avail_size, line_length, item_sizes);
for (iter = g_sequence_get_begin_iter (priv->children);
!g_sequence_iter_is_end (iter);)
{
iter = get_largest_size_for_line_in_opposing_orientation (box,
GTK_ORIENTATION_VERTICAL,
iter,
line_length,
item_sizes,
extra_pixels,
&min_line_width,
&nat_line_width);
/* Its possible the last line only had invisible widgets */
if (nat_line_width > 0)
{
if (first_line)
first_line = FALSE;
else
{
min_width += priv->column_spacing;
nat_width += priv->column_spacing;
}
min_width += min_line_width;
nat_width += nat_line_width;
}
}
g_free (item_sizes);
}
}
out:
if (minimum_width)
*minimum_width = min_width;
if (natural_width)
*natural_width = nat_width;
}
/* Drawing {{{3 */
static gboolean
gtk_flow_box_draw (GtkWidget *widget,
cairo_t *cr)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkAllocation allocation = { 0, };
GtkStyleContext* context;
gtk_widget_get_allocation (GTK_WIDGET (box), &allocation);
context = gtk_widget_get_style_context (GTK_WIDGET (box));
gtk_render_background (context, cr, 0, 0, allocation.width, allocation.height);
GTK_WIDGET_CLASS (gtk_flow_box_parent_class)->draw (widget, cr);
if (priv->rubberband_first && priv->rubberband_last)
{
GSequenceIter *iter, *iter1, *iter2;
GdkRectangle line_rect, rect;
GArray *lines;
gboolean vertical;
vertical = priv->orientation == GTK_ORIENTATION_VERTICAL;
cairo_save (cr);
context = gtk_widget_get_style_context (widget);
gtk_style_context_save (context);
gtk_style_context_add_class (context, GTK_STYLE_CLASS_RUBBERBAND);
iter1 = CHILD_PRIV (priv->rubberband_first)->iter;
iter2 = CHILD_PRIV (priv->rubberband_last)->iter;
if (g_sequence_iter_compare (iter2, iter1) < 0)
{
iter = iter1;
iter1 = iter2;
iter2 = iter;
}
line_rect.width = 0;
lines = g_array_new (FALSE, FALSE, sizeof (GdkRectangle));
for (iter = iter1;
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
GtkWidget *child;
child = g_sequence_get (iter);
gtk_widget_get_allocation (GTK_WIDGET (child), &rect);
if (line_rect.width == 0)
line_rect = rect;
else
{
if ((vertical && rect.x == line_rect.x) ||
(!vertical && rect.y == line_rect.y))
gdk_rectangle_union (&rect, &line_rect, &line_rect);
else
{
g_array_append_val (lines, line_rect);
line_rect = rect;
}
}
if (g_sequence_iter_compare (iter, iter2) == 0)
break;
}
if (line_rect.width != 0)
g_array_append_val (lines, line_rect);
if (lines->len > 0)
{
GtkStateFlags state;
cairo_path_t *path;
GtkBorder border;
GdkRGBA border_color;
if (vertical)
path_from_vertical_line_rects (cr, (GdkRectangle *)lines->data, lines->len);
else
path_from_horizontal_line_rects (cr, (GdkRectangle *)lines->data, lines->len);
/* For some reason we need to copy and reapply the path,
* or it gets eaten by gtk_render_background()
*/
path = cairo_copy_path (cr);
cairo_save (cr);
cairo_clip (cr);
gtk_widget_get_allocation (widget, &allocation);
gtk_render_background (context, cr,
0, 0,
allocation.width, allocation.height);
cairo_restore (cr);
cairo_append_path (cr, path);
cairo_path_destroy (path);
state = gtk_widget_get_state_flags (widget);
G_GNUC_BEGIN_IGNORE_DEPRECATIONS
gtk_style_context_get_border_color (context, state, &border_color);
G_GNUC_END_IGNORE_DEPRECATIONS
gtk_style_context_get_border (context, state, &border);
cairo_set_line_width (cr, border.left);
gdk_cairo_set_source_rgba (cr, &border_color);
cairo_stroke (cr);
}
g_array_free (lines, TRUE);
gtk_style_context_restore (context);
cairo_restore (cr);
}
return TRUE;
}
/* Autoscrolling {{{3 */
static void
remove_autoscroll (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (priv->autoscroll_id)
{
gtk_widget_remove_tick_callback (GTK_WIDGET (box), priv->autoscroll_id);
priv->autoscroll_id = 0;
}
priv->autoscroll_mode = GTK_SCROLL_NONE;
}
static gboolean
autoscroll_cb (GtkWidget *widget,
GdkFrameClock *frame_clock,
gpointer data)
{
GtkFlowBox *box = GTK_FLOW_BOX (data);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkAdjustment *adjustment;
gdouble factor;
gdouble increment;
gdouble value;
if (priv->orientation == GTK_ORIENTATION_HORIZONTAL)
adjustment = priv->vadjustment;
else
adjustment = priv->hadjustment;
switch (priv->autoscroll_mode)
{
case GTK_SCROLL_STEP_FORWARD:
factor = AUTOSCROLL_FACTOR;
break;
case GTK_SCROLL_STEP_BACKWARD:
factor = - AUTOSCROLL_FACTOR;
break;
case GTK_SCROLL_PAGE_FORWARD:
factor = AUTOSCROLL_FACTOR_FAST;
break;
case GTK_SCROLL_PAGE_BACKWARD:
factor = - AUTOSCROLL_FACTOR_FAST;
break;
default:
g_assert_not_reached ();
}
increment = gtk_adjustment_get_step_increment (adjustment) / factor;
value = gtk_adjustment_get_value (adjustment);
value += increment;
gtk_adjustment_set_value (adjustment, value);
if (priv->rubberband_select)
{
GdkEventSequence *sequence;
gdouble x, y;
GtkFlowBoxChild *child;
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (priv->drag_gesture));
gtk_gesture_get_point (priv->drag_gesture, sequence, &x, &y);
child = gtk_flow_box_find_child_at_pos (box, x, y);
gtk_flow_box_update_prelight (box, child);
gtk_flow_box_update_active (box, child);
if (child != NULL)
priv->rubberband_last = child;
}
return G_SOURCE_CONTINUE;
}
static void
add_autoscroll (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (priv->autoscroll_id != 0 ||
priv->autoscroll_mode == GTK_SCROLL_NONE)
return;
priv->autoscroll_id = gtk_widget_add_tick_callback (GTK_WIDGET (box),
autoscroll_cb,
box,
NULL);
}
static gboolean
get_view_rect (GtkFlowBox *box,
GdkRectangle *rect)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkWidget *parent;
GdkWindow *view;
parent = gtk_widget_get_parent (GTK_WIDGET (box));
if (GTK_IS_VIEWPORT (parent))
{
view = gtk_viewport_get_view_window (GTK_VIEWPORT (parent));
rect->x = rect->y = 0;
rect->x = gtk_adjustment_get_value (priv->hadjustment);
rect->y = gtk_adjustment_get_value (priv->vadjustment);
rect->width = gdk_window_get_width (view);
rect->height = gdk_window_get_height (view);
return TRUE;
}
return FALSE;
}
static void
update_autoscroll_mode (GtkFlowBox *box,
gint x,
gint y)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkScrollType mode = GTK_SCROLL_NONE;
GdkRectangle rect;
gint size, pos;
if (priv->rubberband_select && get_view_rect (box, &rect))
{
if (priv->orientation == GTK_ORIENTATION_VERTICAL)
{
size = rect.width;
pos = x - rect.x;
}
else
{
size = rect.height;
pos = y - rect.y;
}
if (pos < 0 - AUTOSCROLL_FAST_DISTANCE)
mode = GTK_SCROLL_PAGE_BACKWARD;
else if (pos > size + AUTOSCROLL_FAST_DISTANCE)
mode = GTK_SCROLL_PAGE_FORWARD;
else if (pos < 0)
mode = GTK_SCROLL_STEP_BACKWARD;
else if (pos > size)
mode = GTK_SCROLL_STEP_FORWARD;
}
if (mode != priv->autoscroll_mode)
{
remove_autoscroll (box);
priv->autoscroll_mode = mode;
add_autoscroll (box);
}
}
/* Event handling {{{3 */
static gboolean
gtk_flow_box_enter_notify_event (GtkWidget *widget,
GdkEventCrossing *event)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxChild *child;
if (event->window != gtk_widget_get_window (GTK_WIDGET (box)))
return FALSE;
child = gtk_flow_box_find_child_at_pos (box, event->x, event->y);
gtk_flow_box_update_prelight (box, child);
gtk_flow_box_update_active (box, child);
return FALSE;
}
static gboolean
gtk_flow_box_leave_notify_event (GtkWidget *widget,
GdkEventCrossing *event)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxChild *child = NULL;
if (event->window != gtk_widget_get_window (GTK_WIDGET (box)))
return FALSE;
if (event->detail != GDK_NOTIFY_INFERIOR)
child = NULL;
else
child = gtk_flow_box_find_child_at_pos (box, event->x, event->y);
gtk_flow_box_update_prelight (box, child);
gtk_flow_box_update_active (box, child);
return FALSE;
}
static void
gtk_flow_box_drag_gesture_update (GtkGestureDrag *gesture,
gdouble offset_x,
gdouble offset_y,
GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gdouble start_x, start_y;
GtkFlowBoxChild *child;
gtk_gesture_drag_get_start_point (gesture, &start_x, &start_y);
if (!priv->rubberband_select &&
(offset_x * offset_x) + (offset_y * offset_y) > RUBBERBAND_START_DISTANCE * RUBBERBAND_START_DISTANCE)
{
priv->rubberband_select = TRUE;
priv->rubberband_first = gtk_flow_box_find_child_at_pos (box, start_x, start_y);
/* Grab focus here, so Escape-to-stop-rubberband works */
gtk_flow_box_update_cursor (box, priv->rubberband_first);
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
}
if (priv->rubberband_select)
{
child = gtk_flow_box_find_child_at_pos (box, start_x + offset_x,
start_y + offset_y);
if (priv->rubberband_first == NULL)
priv->rubberband_first = child;
if (child != NULL)
priv->rubberband_last = child;
update_autoscroll_mode (box, start_x + offset_x, start_y + offset_y);
gtk_widget_queue_draw (GTK_WIDGET (box));
}
}
static gboolean
gtk_flow_box_motion_notify_event (GtkWidget *widget,
GdkEventMotion *event)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxChild *child;
GdkWindow *window;
GdkWindow *event_window;
gint relative_x;
gint relative_y;
gdouble parent_x;
gdouble parent_y;
window = gtk_widget_get_window (GTK_WIDGET (box));
event_window = event->window;
relative_x = event->x;
relative_y = event->y;
while ((event_window != NULL) && (event_window != window))
{
gdk_window_coords_to_parent (event_window,
relative_x, relative_y,
&parent_x, &parent_y);
relative_x = parent_x;
relative_y = parent_y;
event_window = gdk_window_get_effective_parent (event_window);
}
child = gtk_flow_box_find_child_at_pos (box, relative_x, relative_y);
gtk_flow_box_update_prelight (box, child);
gtk_flow_box_update_active (box, child);
return GTK_WIDGET_CLASS (gtk_flow_box_parent_class)->motion_notify_event (widget, event);
}
static void
gtk_flow_box_multipress_gesture_pressed (GtkGestureMultiPress *gesture,
guint n_press,
gdouble x,
gdouble y,
GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GtkFlowBoxChild *child;
child = gtk_flow_box_find_child_at_pos (box, x, y);
if (child == NULL)
return;
/* The drag gesture is only triggered by first press */
if (n_press != 1)
gtk_gesture_set_state (priv->drag_gesture, GTK_EVENT_SEQUENCE_DENIED);
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_CLAIMED);
priv->active_child = child;
priv->active_child_active = TRUE;
gtk_widget_queue_draw (GTK_WIDGET (box));
if (n_press == 2 && !priv->activate_on_single_click)
g_signal_emit (box, signals[CHILD_ACTIVATED], 0, child);
}
static void
gtk_flow_box_multipress_gesture_released (GtkGestureMultiPress *gesture,
guint n_press,
gdouble x,
gdouble y,
GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (priv->active_child != NULL && priv->active_child_active)
{
if (priv->activate_on_single_click)
gtk_flow_box_select_and_activate (box, priv->active_child);
else
{
GdkEventSequence *sequence;
GdkInputSource source;
const GdkEvent *event;
gboolean modify;
gboolean extend;
get_current_selection_modifiers (GTK_WIDGET (box), &modify, &extend);
/* With touch, we default to modifying the selection.
* You can still clear the selection and start over
* by holding Ctrl.
*/
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
event = gtk_gesture_get_last_event (GTK_GESTURE (gesture), sequence);
source = gdk_device_get_source (gdk_event_get_source_device (event));
if (source == GDK_SOURCE_TOUCHSCREEN)
modify = !modify;
gtk_flow_box_update_selection (box, priv->active_child, modify, extend);
}
}
priv->active_child = NULL;
priv->active_child_active = FALSE;
gtk_widget_queue_draw (GTK_WIDGET (box));
}
static void
gtk_flow_box_drag_gesture_begin (GtkGestureDrag *gesture,
gdouble start_x,
gdouble start_y,
GtkWidget *widget)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (widget);
if (priv->selection_mode != GTK_SELECTION_MULTIPLE)
{
gtk_gesture_set_state (GTK_GESTURE (gesture), GTK_EVENT_SEQUENCE_DENIED);
return;
}
priv->rubberband_select = FALSE;
priv->rubberband_first = NULL;
priv->rubberband_last = NULL;
get_current_selection_modifiers (widget, &priv->rubberband_modify, &priv->rubberband_extend);
}
static void
gtk_flow_box_stop_rubberband (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
priv->rubberband_select = FALSE;
priv->rubberband_first = NULL;
priv->rubberband_last = NULL;
remove_autoscroll (box);
gtk_widget_queue_draw (GTK_WIDGET (box));
}
static void
gtk_flow_box_drag_gesture_end (GtkGestureDrag *gesture,
gdouble offset_x,
gdouble offset_y,
GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
GdkEventSequence *sequence;
if (!priv->rubberband_select)
return;
sequence = gtk_gesture_single_get_current_sequence (GTK_GESTURE_SINGLE (gesture));
if (gtk_gesture_handles_sequence (GTK_GESTURE (gesture), sequence))
{
if (!priv->rubberband_extend && !priv->rubberband_modify)
gtk_flow_box_unselect_all_internal (box);
gtk_flow_box_select_all_between (box, priv->rubberband_first, priv->rubberband_last, priv->rubberband_modify);
gtk_flow_box_stop_rubberband (box);
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
else
gtk_flow_box_stop_rubberband (box);
gtk_widget_queue_draw (GTK_WIDGET (box));
}
static gboolean
gtk_flow_box_key_press_event (GtkWidget *widget,
GdkEventKey *event)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (priv->rubberband_select)
{
if (event->keyval == GDK_KEY_Escape)
{
gtk_flow_box_stop_rubberband (box);
return TRUE;
}
}
return GTK_WIDGET_CLASS (gtk_flow_box_parent_class)->key_press_event (widget, event);
}
/* Realize and map {{{3 */
static void
gtk_flow_box_realize (GtkWidget *widget)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkAllocation allocation;
GdkWindowAttr attributes = {0};
GdkWindow *window;
gtk_widget_get_allocation (GTK_WIDGET (box), &allocation);
gtk_widget_set_realized (GTK_WIDGET (box), TRUE);
attributes.x = allocation.x;
attributes.y = allocation.y;
attributes.width = allocation.width;
attributes.height = allocation.height;
attributes.window_type = GDK_WINDOW_CHILD;
attributes.event_mask = gtk_widget_get_events (GTK_WIDGET (box))
| GDK_ENTER_NOTIFY_MASK
| GDK_LEAVE_NOTIFY_MASK
| GDK_POINTER_MOTION_MASK
| GDK_EXPOSURE_MASK
| GDK_KEY_PRESS_MASK
| GDK_BUTTON_PRESS_MASK
| GDK_BUTTON_RELEASE_MASK;
attributes.wclass = GDK_INPUT_OUTPUT;
window = gdk_window_new (gtk_widget_get_parent_window (GTK_WIDGET (box)),
&attributes, GDK_WA_X | GDK_WA_Y);
gtk_widget_register_window (GTK_WIDGET (box), window);
gtk_widget_set_window (GTK_WIDGET (box), window);
}
static void
gtk_flow_box_unmap (GtkWidget *widget)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
remove_autoscroll (box);
GTK_WIDGET_CLASS (gtk_flow_box_parent_class)->unmap (widget);
}
/* GtkContainer implementation {{{2 */
static void
gtk_flow_box_add (GtkContainer *container,
GtkWidget *child)
{
gtk_flow_box_insert (GTK_FLOW_BOX (container), child, -1);
}
static void
gtk_flow_box_remove (GtkContainer *container,
GtkWidget *widget)
{
GtkFlowBox *box = GTK_FLOW_BOX (container);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gboolean was_visible;
gboolean was_selected;
GtkFlowBoxChild *child;
if (GTK_IS_FLOW_BOX_CHILD (widget))
child = GTK_FLOW_BOX_CHILD (widget);
else
{
child = (GtkFlowBoxChild*)gtk_widget_get_parent (widget);
if (!GTK_IS_FLOW_BOX_CHILD (child))
{
g_warning ("Tried to remove non-child %p\n", widget);
return;
}
}
was_visible = child_is_visible (GTK_WIDGET (child));
was_selected = CHILD_PRIV (child)->selected;
if (child == priv->prelight_child)
priv->prelight_child = NULL;
if (child == priv->active_child)
priv->active_child = NULL;
if (child == priv->selected_child)
priv->selected_child = NULL;
gtk_widget_unparent (GTK_WIDGET (child));
g_sequence_remove (CHILD_PRIV (child)->iter);
if (was_visible && gtk_widget_get_visible (GTK_WIDGET (box)))
gtk_widget_queue_resize (GTK_WIDGET (box));
if (was_selected)
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
static void
gtk_flow_box_forall (GtkContainer *container,
gboolean include_internals,
GtkCallback callback,
gpointer callback_target)
{
GSequenceIter *iter;
GtkWidget *child;
iter = g_sequence_get_begin_iter (BOX_PRIV (container)->children);
while (!g_sequence_iter_is_end (iter))
{
child = g_sequence_get (iter);
iter = g_sequence_iter_next (iter);
callback (child, callback_target);
}
}
static GType
gtk_flow_box_child_type (GtkContainer *container)
{
return GTK_TYPE_FLOW_BOX_CHILD;
}
/* Keynav {{{2 */
static gboolean
gtk_flow_box_focus (GtkWidget *widget,
GtkDirectionType direction)
{
GtkFlowBox *box = GTK_FLOW_BOX (widget);
GtkWidget *focus_child;
GSequenceIter *iter;
GtkFlowBoxChild *next_focus_child;
focus_child = gtk_container_get_focus_child (GTK_CONTAINER (box));
next_focus_child = NULL;
if (focus_child != NULL)
{
if (gtk_widget_child_focus (focus_child, direction))
return TRUE;
iter = CHILD_PRIV (focus_child)->iter;
if (direction == GTK_DIR_LEFT || direction == GTK_DIR_TAB_BACKWARD)
iter = gtk_flow_box_get_previous_focusable (box, iter);
else if (direction == GTK_DIR_RIGHT || direction == GTK_DIR_TAB_FORWARD)
iter = gtk_flow_box_get_next_focusable (box, iter);
else if (direction == GTK_DIR_UP)
iter = gtk_flow_box_get_above_focusable (box, iter);
else if (direction == GTK_DIR_DOWN)
iter = gtk_flow_box_get_below_focusable (box, iter);
if (iter != NULL)
next_focus_child = g_sequence_get (iter);
}
else
{
if (BOX_PRIV (box)->selected_child)
next_focus_child = BOX_PRIV (box)->selected_child;
else
{
if (direction == GTK_DIR_UP || direction == GTK_DIR_TAB_BACKWARD)
iter = gtk_flow_box_get_last_focusable (box);
else
iter = gtk_flow_box_get_first_focusable (box);
if (iter != NULL)
next_focus_child = g_sequence_get (iter);
}
}
if (next_focus_child == NULL)
{
if (direction == GTK_DIR_UP || direction == GTK_DIR_DOWN ||
direction == GTK_DIR_LEFT || direction == GTK_DIR_RIGHT)
{
if (gtk_widget_keynav_failed (GTK_WIDGET (box), direction))
return TRUE;
}
return FALSE;
}
if (gtk_widget_child_focus (GTK_WIDGET (next_focus_child), direction))
return TRUE;
return TRUE;
}
static void
gtk_flow_box_add_move_binding (GtkBindingSet *binding_set,
guint keyval,
GdkModifierType modmask,
GtkMovementStep step,
gint count)
{
GdkDisplay *display;
GdkModifierType extend_mod_mask = GDK_SHIFT_MASK;
GdkModifierType modify_mod_mask = GDK_CONTROL_MASK;
display = gdk_display_get_default ();
if (display)
{
extend_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
GDK_MODIFIER_INTENT_EXTEND_SELECTION);
modify_mod_mask = gdk_keymap_get_modifier_mask (gdk_keymap_get_for_display (display),
GDK_MODIFIER_INTENT_MODIFY_SELECTION);
}
gtk_binding_entry_add_signal (binding_set, keyval, modmask,
"move-cursor", 2,
GTK_TYPE_MOVEMENT_STEP, step,
G_TYPE_INT, count,
NULL);
gtk_binding_entry_add_signal (binding_set, keyval, modmask | extend_mod_mask,
"move-cursor", 2,
GTK_TYPE_MOVEMENT_STEP, step,
G_TYPE_INT, count,
NULL);
gtk_binding_entry_add_signal (binding_set, keyval, modmask | modify_mod_mask,
"move-cursor", 2,
GTK_TYPE_MOVEMENT_STEP, step,
G_TYPE_INT, count,
NULL);
gtk_binding_entry_add_signal (binding_set, keyval, modmask | extend_mod_mask | modify_mod_mask,
"move-cursor", 2,
GTK_TYPE_MOVEMENT_STEP, step,
G_TYPE_INT, count,
NULL);
}
static void
gtk_flow_box_activate_cursor_child (GtkFlowBox *box)
{
gtk_flow_box_select_and_activate (box, BOX_PRIV (box)->cursor_child);
}
static void
gtk_flow_box_toggle_cursor_child (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
if (priv->cursor_child == NULL)
return;
if ((priv->selection_mode == GTK_SELECTION_SINGLE ||
priv->selection_mode == GTK_SELECTION_MULTIPLE) &&
CHILD_PRIV (priv->cursor_child)->selected)
gtk_flow_box_unselect_child_internal (box, priv->cursor_child);
else
gtk_flow_box_select_and_activate (box, priv->cursor_child);
}
static void
gtk_flow_box_move_cursor (GtkFlowBox *box,
GtkMovementStep step,
gint count)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gboolean modify;
gboolean extend;
GtkFlowBoxChild *child;
GtkFlowBoxChild *prev;
GtkFlowBoxChild *next;
GtkAllocation allocation;
gint page_size;
GSequenceIter *iter;
gint start;
GtkAdjustment *adjustment;
gboolean vertical;
vertical = priv->orientation == GTK_ORIENTATION_VERTICAL;
if (vertical)
{
switch (step)
{
case GTK_MOVEMENT_VISUAL_POSITIONS:
step = GTK_MOVEMENT_DISPLAY_LINES;
break;
case GTK_MOVEMENT_DISPLAY_LINES:
step = GTK_MOVEMENT_VISUAL_POSITIONS;
break;
default: ;
}
}
child = NULL;
switch (step)
{
case GTK_MOVEMENT_VISUAL_POSITIONS:
if (priv->cursor_child != NULL)
{
iter = CHILD_PRIV (priv->cursor_child)->iter;
if (gtk_widget_get_direction (GTK_WIDGET (box)) == GTK_TEXT_DIR_RTL)
count = - count;
while (count < 0 && iter != NULL)
{
iter = gtk_flow_box_get_previous_focusable (box, iter);
count = count + 1;
}
while (count > 0 && iter != NULL)
{
iter = gtk_flow_box_get_next_focusable (box, iter);
count = count - 1;
}
if (iter != NULL && !g_sequence_iter_is_end (iter))
child = g_sequence_get (iter);
}
break;
case GTK_MOVEMENT_BUFFER_ENDS:
if (count < 0)
iter = gtk_flow_box_get_first_focusable (box);
else
iter = gtk_flow_box_get_last_focusable (box);
if (iter != NULL)
child = g_sequence_get (iter);
break;
case GTK_MOVEMENT_DISPLAY_LINES:
if (priv->cursor_child != NULL)
{
iter = CHILD_PRIV (priv->cursor_child)->iter;
while (count < 0 && iter != NULL)
{
iter = gtk_flow_box_get_above_focusable (box, iter);
count = count + 1;
}
while (count > 0 && iter != NULL)
{
iter = gtk_flow_box_get_below_focusable (box, iter);
count = count - 1;
}
if (iter != NULL)
child = g_sequence_get (iter);
}
break;
case GTK_MOVEMENT_PAGES:
page_size = 100;
adjustment = vertical ? priv->hadjustment : priv->vadjustment;
if (adjustment)
page_size = gtk_adjustment_get_page_increment (adjustment);
if (priv->cursor_child != NULL)
{
child = priv->cursor_child;
iter = CHILD_PRIV (child)->iter;
gtk_widget_get_allocation (GTK_WIDGET (child), &allocation);
start = vertical ? allocation.x : allocation.y;
if (count < 0)
{
gint i = 0;
/* Up */
while (iter != NULL)
{
iter = gtk_flow_box_get_previous_focusable (box, iter);
if (iter == NULL)
break;
prev = g_sequence_get (iter);
/* go up an even number of rows */
if (i % priv->cur_children_per_line == 0)
{
gtk_widget_get_allocation (GTK_WIDGET (prev), &allocation);
if ((vertical ? allocation.x : allocation.y) < start - page_size)
break;
}
child = prev;
i++;
}
}
else
{
gint i = 0;
/* Down */
while (!g_sequence_iter_is_end (iter))
{
iter = gtk_flow_box_get_next_focusable (box, iter);
if (g_sequence_iter_is_end (iter))
break;
next = g_sequence_get (iter);
if (i % priv->cur_children_per_line == 0)
{
gtk_widget_get_allocation (GTK_WIDGET (next), &allocation);
if ((vertical ? allocation.x : allocation.y) > start + page_size)
break;
}
child = next;
i++;
}
}
gtk_widget_get_allocation (GTK_WIDGET (child), &allocation);
}
break;
default:
g_assert_not_reached ();
}
if (child == NULL || child == priv->cursor_child)
{
GtkDirectionType direction = count < 0 ? GTK_DIR_UP : GTK_DIR_DOWN;
if (!gtk_widget_keynav_failed (GTK_WIDGET (box), direction))
{
GtkWidget *toplevel = gtk_widget_get_toplevel (GTK_WIDGET (box));
if (toplevel)
gtk_widget_child_focus (toplevel,
direction == GTK_DIR_UP ?
GTK_DIR_TAB_BACKWARD :
GTK_DIR_TAB_FORWARD);
}
return;
}
get_current_selection_modifiers (GTK_WIDGET (box), &modify, &extend);
gtk_flow_box_update_cursor (box, child);
if (!modify)
gtk_flow_box_update_selection (box, child, FALSE, extend);
}
/* Selection {{{2 */
static void
gtk_flow_box_selected_children_changed (GtkFlowBox *box)
{
_gtk_flow_box_accessible_selection_changed (GTK_WIDGET (box));
}
/* GObject implementation {{{2 */
static void
gtk_flow_box_get_property (GObject *object,
guint prop_id,
GValue *value,
GParamSpec *pspec)
{
GtkFlowBox *box = GTK_FLOW_BOX (object);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
switch (prop_id)
{
case PROP_ORIENTATION:
g_value_set_enum (value, priv->orientation);
break;
case PROP_HOMOGENEOUS:
g_value_set_boolean (value, priv->homogeneous);
break;
case PROP_COLUMN_SPACING:
g_value_set_uint (value, priv->column_spacing);
break;
case PROP_ROW_SPACING:
g_value_set_uint (value, priv->row_spacing);
break;
case PROP_MIN_CHILDREN_PER_LINE:
g_value_set_uint (value, priv->min_children_per_line);
break;
case PROP_MAX_CHILDREN_PER_LINE:
g_value_set_uint (value, priv->max_children_per_line);
break;
case PROP_SELECTION_MODE:
g_value_set_enum (value, priv->selection_mode);
break;
case PROP_ACTIVATE_ON_SINGLE_CLICK:
g_value_set_boolean (value, priv->activate_on_single_click);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_flow_box_set_property (GObject *object,
guint prop_id,
const GValue *value,
GParamSpec *pspec)
{
GtkFlowBox *box = GTK_FLOW_BOX (object);
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
switch (prop_id)
{
case PROP_ORIENTATION:
if (priv->orientation != g_value_get_enum (value))
{
priv->orientation = g_value_get_enum (value);
_gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
/* Re-box the children in the new orientation */
gtk_widget_queue_resize (GTK_WIDGET (box));
g_object_notify_by_pspec (object, pspec);
}
break;
case PROP_HOMOGENEOUS:
gtk_flow_box_set_homogeneous (box, g_value_get_boolean (value));
break;
case PROP_COLUMN_SPACING:
gtk_flow_box_set_column_spacing (box, g_value_get_uint (value));
break;
case PROP_ROW_SPACING:
gtk_flow_box_set_row_spacing (box, g_value_get_uint (value));
break;
case PROP_MIN_CHILDREN_PER_LINE:
gtk_flow_box_set_min_children_per_line (box, g_value_get_uint (value));
break;
case PROP_MAX_CHILDREN_PER_LINE:
gtk_flow_box_set_max_children_per_line (box, g_value_get_uint (value));
break;
case PROP_SELECTION_MODE:
gtk_flow_box_set_selection_mode (box, g_value_get_enum (value));
break;
case PROP_ACTIVATE_ON_SINGLE_CLICK:
gtk_flow_box_set_activate_on_single_click (box, g_value_get_boolean (value));
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
break;
}
}
static void
gtk_flow_box_finalize (GObject *obj)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (obj);
if (priv->filter_destroy != NULL)
priv->filter_destroy (priv->filter_data);
if (priv->sort_destroy != NULL)
priv->sort_destroy (priv->sort_data);
g_sequence_free (priv->children);
g_clear_object (&priv->hadjustment);
g_clear_object (&priv->vadjustment);
g_object_unref (priv->drag_gesture);
g_object_unref (priv->multipress_gesture);
G_OBJECT_CLASS (gtk_flow_box_parent_class)->finalize (obj);
}
static void
gtk_flow_box_class_init (GtkFlowBoxClass *class)
{
GObjectClass *object_class = G_OBJECT_CLASS (class);
GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (class);
GtkContainerClass *container_class = GTK_CONTAINER_CLASS (class);
GtkBindingSet *binding_set;
object_class->finalize = gtk_flow_box_finalize;
object_class->get_property = gtk_flow_box_get_property;
object_class->set_property = gtk_flow_box_set_property;
widget_class->enter_notify_event = gtk_flow_box_enter_notify_event;
widget_class->leave_notify_event = gtk_flow_box_leave_notify_event;
widget_class->motion_notify_event = gtk_flow_box_motion_notify_event;
widget_class->size_allocate = gtk_flow_box_size_allocate;
widget_class->realize = gtk_flow_box_realize;
widget_class->unmap = gtk_flow_box_unmap;
widget_class->focus = gtk_flow_box_focus;
widget_class->draw = gtk_flow_box_draw;
widget_class->key_press_event = gtk_flow_box_key_press_event;
widget_class->get_request_mode = gtk_flow_box_get_request_mode;
widget_class->get_preferred_width = gtk_flow_box_get_preferred_width;
widget_class->get_preferred_height = gtk_flow_box_get_preferred_height;
widget_class->get_preferred_height_for_width = gtk_flow_box_get_preferred_height_for_width;
widget_class->get_preferred_width_for_height = gtk_flow_box_get_preferred_width_for_height;
container_class->add = gtk_flow_box_add;
container_class->remove = gtk_flow_box_remove;
container_class->forall = gtk_flow_box_forall;
container_class->child_type = gtk_flow_box_child_type;
gtk_container_class_handle_border_width (container_class);
class->activate_cursor_child = gtk_flow_box_activate_cursor_child;
class->toggle_cursor_child = gtk_flow_box_toggle_cursor_child;
class->move_cursor = gtk_flow_box_move_cursor;
class->select_all = gtk_flow_box_select_all;
class->unselect_all = gtk_flow_box_unselect_all;
class->selected_children_changed = gtk_flow_box_selected_children_changed;
g_object_class_override_property (object_class, PROP_ORIENTATION, "orientation");
/**
* GtkFlowBox:selection-mode:
*
* The selection mode used by the flow box.
*/
props[PROP_SELECTION_MODE] =
g_param_spec_enum ("selection-mode",
P_("Selection mode"),
P_("The selection mode"),
GTK_TYPE_SELECTION_MODE,
GTK_SELECTION_SINGLE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:activate-on-single-click:
*
* Determines whether children can be activated with a single
* click, or require a double-click.
*/
props[PROP_ACTIVATE_ON_SINGLE_CLICK] =
g_param_spec_boolean ("activate-on-single-click",
P_("Activate on Single Click"),
P_("Activate row on a single click"),
TRUE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:homogeneous:
*
* Determines whether all children should be allocated the
* same size.
*/
props[PROP_HOMOGENEOUS] =
g_param_spec_boolean ("homogeneous",
P_("Homogeneous"),
P_("Whether the children should all be the same size"),
FALSE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:min-children-per-line:
*
* The minimum number of children to allocate consecutively
* in the given orientation.
*
* Setting the minimum children per line ensures
* that a reasonably small height will be requested
* for the overall minimum width of the box.
*/
props[PROP_MIN_CHILDREN_PER_LINE] =
g_param_spec_uint ("min-children-per-line",
P_("Minimum Children Per Line"),
P_("The minimum number of children to allocate "
"consecutively in the given orientation."),
0, G_MAXUINT, 0,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:max-children-per-line:
*
* The maximum amount of children to request space for consecutively
* in the given orientation.
*/
props[PROP_MAX_CHILDREN_PER_LINE] =
g_param_spec_uint ("max-children-per-line",
P_("Maximum Children Per Line"),
P_("The maximum amount of children to request space for "
"consecutively in the given orientation."),
0, G_MAXUINT, DEFAULT_MAX_CHILDREN_PER_LINE,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:row-spacing:
*
* The amount of vertical space between two children.
*/
props[PROP_ROW_SPACING] =
g_param_spec_uint ("row-spacing",
P_("Vertical spacing"),
P_("The amount of vertical space between two children"),
0, G_MAXUINT, 0,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
/**
* GtkFlowBox:column-spacing:
*
* The amount of horizontal space between two children.
*/
props[PROP_COLUMN_SPACING] =
g_param_spec_uint ("column-spacing",
P_("Horizontal spacing"),
P_("The amount of horizontal space between two children"),
0, G_MAXUINT, 0,
G_PARAM_READWRITE | G_PARAM_EXPLICIT_NOTIFY);
g_object_class_install_properties (object_class, LAST_PROP, props);
/**
* GtkFlowBox::child-activated:
* @box: the #GtkFlowBox on which the signal is emitted
* @child: the child that is activated
*
* The ::child-activated signal is emitted when a child has been
* activated by the user.
*/
signals[CHILD_ACTIVATED] = g_signal_new ("child-activated",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST,
G_STRUCT_OFFSET (GtkFlowBoxClass, child_activated),
NULL, NULL,
g_cclosure_marshal_VOID__OBJECT,
G_TYPE_NONE, 1,
GTK_TYPE_FLOW_BOX_CHILD);
/**
* GtkFlowBox::selected-children-changed:
* @box: the #GtkFlowBox on wich the signal is emitted
*
* The ::selected-children-changed signal is emitted when the
* set of selected children changes.
*
* Use gtk_flow_box_selected_foreach() or
* gtk_flow_box_get_selected_children() to obtain the
* selected children.
*/
signals[SELECTED_CHILDREN_CHANGED] = g_signal_new ("selected-children-changed",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_FIRST,
G_STRUCT_OFFSET (GtkFlowBoxClass, selected_children_changed),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GtkFlowBox::activate-cursor-child:
* @box: the #GtkFlowBox on which the signal is emitted
*
* The ::activate-cursor-child signal is a
* [keybinding signal][GtkBindingSignal]
* which gets emitted when the user activates the @box.
*/
signals[ACTIVATE_CURSOR_CHILD] = g_signal_new ("activate-cursor-child",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxClass, activate_cursor_child),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GtkFlowBox::toggle-cursor-child:
* @box: the #GtkFlowBox on which the signal is emitted
*
* The ::toggle-cursor-child signal is a
* [keybinding signal][GtkBindingSignal]
* which toggles the selection of the child that has the focus.
*
* The default binding for this signal is Ctrl-Space.
*/
signals[TOGGLE_CURSOR_CHILD] = g_signal_new ("toggle-cursor-child",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxClass, toggle_cursor_child),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GtkFlowBox::move-cursor:
* @box: the #GtkFlowBox on which the signal is emitted
* @step: the granularity fo the move, as a #GtkMovementStep
* @count: the number of @step units to move
*
* The ::move-cursor signal is a
* [keybinding signal][GtkBindingSignal]
* which gets emitted when the user initiates a cursor movement.
* If the cursor is not visible in @text_view, this signal causes
* the viewport to be moved instead.
*
* Applications should not connect to it, but may emit it with
* g_signal_emit_by_name() if they need to control the cursor
* programmatically.
*
* The default bindings for this signal come in two variants,
* the variant with the Shift modifier extends the selection,
* the variant without the Shift modifer does not.
* There are too many key combinations to list them all here.
* - Arrow keys move by individual children
* - Home/End keys move to the ends of the box
* - PageUp/PageDown keys move vertically by pages
*/
signals[MOVE_CURSOR] = g_signal_new ("move-cursor",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxClass, move_cursor),
NULL, NULL,
_gtk_marshal_VOID__ENUM_INT,
G_TYPE_NONE, 2,
GTK_TYPE_MOVEMENT_STEP, G_TYPE_INT);
/**
* GtkFlowBox::select-all:
* @box: the #GtkFlowBox on which the signal is emitted
*
* The ::select-all signal is a
* [keybinding signal][GtkBindingSignal]
* which gets emitted to select all children of the box, if
* the selection mode permits it.
*
* The default bindings for this signal is Ctrl-a.
*/
signals[SELECT_ALL] = g_signal_new ("select-all",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxClass, select_all),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
/**
* GtkFlowBox::unselect-all:
* @box: the #GtkFlowBox on which the signal is emitted
*
* The ::unselect-all signal is a
* [keybinding signal][GtkBindingSignal]
* which gets emitted to unselect all children of the box, if
* the selection mode permits it.
*
* The default bindings for this signal is Ctrl-Shift-a.
*/
signals[UNSELECT_ALL] = g_signal_new ("unselect-all",
GTK_TYPE_FLOW_BOX,
G_SIGNAL_RUN_LAST | G_SIGNAL_ACTION,
G_STRUCT_OFFSET (GtkFlowBoxClass, unselect_all),
NULL, NULL,
g_cclosure_marshal_VOID__VOID,
G_TYPE_NONE, 0);
widget_class->activate_signal = signals[ACTIVATE_CURSOR_CHILD];
binding_set = gtk_binding_set_by_class (class);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Home, 0,
GTK_MOVEMENT_BUFFER_ENDS, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Home, 0,
GTK_MOVEMENT_BUFFER_ENDS, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_End, 0,
GTK_MOVEMENT_BUFFER_ENDS, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_End, 0,
GTK_MOVEMENT_BUFFER_ENDS, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Up, 0,
GTK_MOVEMENT_DISPLAY_LINES, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Up, 0,
GTK_MOVEMENT_DISPLAY_LINES, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Down, 0,
GTK_MOVEMENT_DISPLAY_LINES, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Down, 0,
GTK_MOVEMENT_DISPLAY_LINES, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Page_Up, 0,
GTK_MOVEMENT_PAGES, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Page_Up, 0,
GTK_MOVEMENT_PAGES, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Page_Down, 0,
GTK_MOVEMENT_PAGES, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Page_Down, 0,
GTK_MOVEMENT_PAGES, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Right, 0,
GTK_MOVEMENT_VISUAL_POSITIONS, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Right, 0,
GTK_MOVEMENT_VISUAL_POSITIONS, 1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_Left, 0,
GTK_MOVEMENT_VISUAL_POSITIONS, -1);
gtk_flow_box_add_move_binding (binding_set, GDK_KEY_KP_Left, 0,
GTK_MOVEMENT_VISUAL_POSITIONS, -1);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_space, GDK_CONTROL_MASK,
"toggle-cursor-child", 0, NULL);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_KP_Space, GDK_CONTROL_MASK,
"toggle-cursor-child", 0, NULL);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK,
"select-all", 0);
gtk_binding_entry_add_signal (binding_set, GDK_KEY_a, GDK_CONTROL_MASK | GDK_SHIFT_MASK,
"unselect-all", 0);
gtk_widget_class_set_accessible_type (widget_class, GTK_TYPE_FLOW_BOX_ACCESSIBLE);
}
static void
gtk_flow_box_init (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
gtk_widget_set_has_window (GTK_WIDGET (box), TRUE);
gtk_widget_set_redraw_on_allocate (GTK_WIDGET (box), TRUE);
priv->orientation = GTK_ORIENTATION_HORIZONTAL;
priv->selection_mode = GTK_SELECTION_SINGLE;
priv->max_children_per_line = DEFAULT_MAX_CHILDREN_PER_LINE;
priv->column_spacing = 0;
priv->row_spacing = 0;
priv->activate_on_single_click = TRUE;
_gtk_orientable_set_style_classes (GTK_ORIENTABLE (box));
priv->children = g_sequence_new (NULL);
priv->multipress_gesture = gtk_gesture_multi_press_new (GTK_WIDGET (box));
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->multipress_gesture),
FALSE);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->multipress_gesture),
GDK_BUTTON_PRIMARY);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (priv->multipress_gesture),
GTK_PHASE_BUBBLE);
g_signal_connect (priv->multipress_gesture, "pressed",
G_CALLBACK (gtk_flow_box_multipress_gesture_pressed), box);
g_signal_connect (priv->multipress_gesture, "released",
G_CALLBACK (gtk_flow_box_multipress_gesture_released), box);
priv->drag_gesture = gtk_gesture_drag_new (GTK_WIDGET (box));
gtk_gesture_single_set_touch_only (GTK_GESTURE_SINGLE (priv->drag_gesture),
FALSE);
gtk_gesture_single_set_button (GTK_GESTURE_SINGLE (priv->drag_gesture),
GDK_BUTTON_PRIMARY);
gtk_event_controller_set_propagation_phase (GTK_EVENT_CONTROLLER (priv->drag_gesture),
GTK_PHASE_CAPTURE);
g_signal_connect (priv->drag_gesture, "drag-begin",
G_CALLBACK (gtk_flow_box_drag_gesture_begin), box);
g_signal_connect (priv->drag_gesture, "drag-update",
G_CALLBACK (gtk_flow_box_drag_gesture_update), box);
g_signal_connect (priv->drag_gesture, "drag-end",
G_CALLBACK (gtk_flow_box_drag_gesture_end), box);
}
/* Public API {{{2 */
/**
* gtk_flow_box_new:
*
* Creates a GtkFlowBox.
*
* Returns: a new #GtkFlowBox container
*
* Since: 3.12
*/
GtkWidget *
gtk_flow_box_new (void)
{
return (GtkWidget *)g_object_new (GTK_TYPE_FLOW_BOX, NULL);
}
static void
gtk_flow_box_insert_css_node (GtkFlowBox *box,
GtkWidget *child,
GSequenceIter *iter)
{
GSequenceIter *prev_iter;
GtkWidget *sibling;
prev_iter = g_sequence_iter_prev (iter);
if (prev_iter != iter)
{
sibling = g_sequence_get (prev_iter);
gtk_css_node_insert_after (gtk_widget_get_css_node (GTK_WIDGET (box)),
gtk_widget_get_css_node (child),
gtk_widget_get_css_node (sibling));
}
}
/**
* gtk_flow_box_insert:
* @box: a #GtkFlowBox
* @widget: the #GtkWidget to add
* @position: the position to insert @child in
*
* Inserts the @widget into @box at @position.
*
* If a sort function is set, the widget will actually be inserted
* at the calculated position and this function has the same effect
* as gtk_container_add().
*
* If @position is -1, or larger than the total number of children
* in the @box, then the @widget will be appended to the end.
*
* Since: 3.12
*/
void
gtk_flow_box_insert (GtkFlowBox *box,
GtkWidget *widget,
gint position)
{
GtkFlowBoxPrivate *priv;
GtkFlowBoxChild *child;
GSequenceIter *iter;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
g_return_if_fail (GTK_IS_WIDGET (widget));
priv = BOX_PRIV (box);
if (GTK_IS_FLOW_BOX_CHILD (widget))
child = GTK_FLOW_BOX_CHILD (widget);
else
{
child = GTK_FLOW_BOX_CHILD (gtk_flow_box_child_new ());
gtk_widget_show (GTK_WIDGET (child));
gtk_container_add (GTK_CONTAINER (child), widget);
}
if (priv->sort_func != NULL)
iter = g_sequence_insert_sorted (priv->children, child,
(GCompareDataFunc)gtk_flow_box_sort, box);
else if (position == 0)
iter = g_sequence_prepend (priv->children, child);
else if (position == -1)
iter = g_sequence_append (priv->children, child);
else
{
GSequenceIter *pos;
pos = g_sequence_get_iter_at_pos (priv->children, position);
iter = g_sequence_insert_before (pos, child);
}
gtk_flow_box_insert_css_node (box, GTK_WIDGET (child), iter);
CHILD_PRIV (child)->iter = iter;
gtk_widget_set_parent (GTK_WIDGET (child), GTK_WIDGET (box));
gtk_flow_box_apply_filter (box, child);
}
/**
* gtk_flow_box_get_child_at_index:
* @box: a #GtkFlowBox
* @idx: the position of the child
*
* Gets the nth child in the @box.
*
* Returns: (transfer none): the child widget, which will
* always be a #GtkFlowBoxChild
*
* Since: 3.12
*/
GtkFlowBoxChild *
gtk_flow_box_get_child_at_index (GtkFlowBox *box,
gint idx)
{
GSequenceIter *iter;
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), NULL);
iter = g_sequence_get_iter_at_pos (BOX_PRIV (box)->children, idx);
if (iter)
return g_sequence_get (iter);
return NULL;
}
/**
* gtk_flow_box_set_hadjustment:
* @box: a #GtkFlowBox
* @adjustment: an adjustment which should be adjusted
* when the focus is moved among the descendents of @container
*
* Hooks up an adjustment to focus handling in @box.
* The adjustment is also used for autoscrolling during
* rubberband selection. See gtk_scrolled_window_get_hadjustment()
* for a typical way of obtaining the adjustment, and
* gtk_flow_box_set_vadjustment()for setting the vertical
* adjustment.
*
* The adjustments have to be in pixel units and in the same
* coordinate system as the allocation for immediate children
* of the box.
*
* Since: 3.12
*/
void
gtk_flow_box_set_hadjustment (GtkFlowBox *box,
GtkAdjustment *adjustment)
{
GtkFlowBoxPrivate *priv;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
priv = BOX_PRIV (box);
g_object_ref (adjustment);
if (priv->hadjustment)
g_object_unref (priv->hadjustment);
priv->hadjustment = adjustment;
gtk_container_set_focus_hadjustment (GTK_CONTAINER (box), adjustment);
}
/**
* gtk_flow_box_set_vadjustment:
* @box: a #GtkFlowBox
* @adjustment: an adjustment which should be adjusted
* when the focus is moved among the descendents of @container
*
* Hooks up an adjustment to focus handling in @box.
* The adjustment is also used for autoscrolling during
* rubberband selection. See gtk_scrolled_window_get_vadjustment()
* for a typical way of obtaining the adjustment, and
* gtk_flow_box_set_hadjustment()for setting the horizontal
* adjustment.
*
* The adjustments have to be in pixel units and in the same
* coordinate system as the allocation for immediate children
* of the box.
*
* Since: 3.12
*/
void
gtk_flow_box_set_vadjustment (GtkFlowBox *box,
GtkAdjustment *adjustment)
{
GtkFlowBoxPrivate *priv;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
g_return_if_fail (GTK_IS_ADJUSTMENT (adjustment));
priv = BOX_PRIV (box);
g_object_ref (adjustment);
if (priv->vadjustment)
g_object_unref (priv->vadjustment);
priv->vadjustment = adjustment;
gtk_container_set_focus_vadjustment (GTK_CONTAINER (box), adjustment);
}
/* Setters and getters {{{2 */
/**
* gtk_flow_box_get_homogeneous:
* @box: a #GtkFlowBox
*
* Returns whether the box is homogeneous (all children are the
* same size). See gtk_box_set_homogeneous().
*
* Returns: %TRUE if the box is homogeneous.
*
* Since: 3.12
*/
gboolean
gtk_flow_box_get_homogeneous (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->homogeneous;
}
/**
* gtk_flow_box_set_homogeneous:
* @box: a #GtkFlowBox
* @homogeneous: %TRUE to create equal allotments,
* %FALSE for variable allotments
*
* Sets the #GtkFlowBox:homogeneous property of @box, controlling
* whether or not all children of @box are given equal space
* in the box.
*
* Since: 3.12
*/
void
gtk_flow_box_set_homogeneous (GtkFlowBox *box,
gboolean homogeneous)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
homogeneous = homogeneous != FALSE;
if (BOX_PRIV (box)->homogeneous != homogeneous)
{
BOX_PRIV (box)->homogeneous = homogeneous;
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_HOMOGENEOUS]);
gtk_widget_queue_resize (GTK_WIDGET (box));
}
}
/**
* gtk_flow_box_set_row_spacing:
* @box: a #GtkFlowBox
* @spacing: the spacing to use
*
* Sets the vertical space to add between children.
* See the #GtkFlowBox:row-spacing property.
*
* Since: 3.12
*/
void
gtk_flow_box_set_row_spacing (GtkFlowBox *box,
guint spacing)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->row_spacing != spacing)
{
BOX_PRIV (box)->row_spacing = spacing;
gtk_widget_queue_resize (GTK_WIDGET (box));
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_ROW_SPACING]);
}
}
/**
* gtk_flow_box_get_row_spacing:
* @box: a #GtkFlowBox
*
* Gets the vertical spacing.
*
* Returns: the vertical spacing
*
* Since: 3.12
*/
guint
gtk_flow_box_get_row_spacing (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->row_spacing;
}
/**
* gtk_flow_box_set_column_spacing:
* @box: a #GtkFlowBox
* @spacing: the spacing to use
*
* Sets the horizontal space to add between children.
* See the #GtkFlowBox:column-spacing property.
*
* Since: 3.12
*/
void
gtk_flow_box_set_column_spacing (GtkFlowBox *box,
guint spacing)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->column_spacing != spacing)
{
BOX_PRIV (box)->column_spacing = spacing;
gtk_widget_queue_resize (GTK_WIDGET (box));
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_COLUMN_SPACING]);
}
}
/**
* gtk_flow_box_get_column_spacing:
* @box: a #GtkFlowBox
*
* Gets the horizontal spacing.
*
* Returns: the horizontal spacing
*
* Since: 3.12
*/
guint
gtk_flow_box_get_column_spacing (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->column_spacing;
}
/**
* gtk_flow_box_set_min_children_per_line:
* @box: a #GtkFlowBox
* @n_children: the minimum number of children per line
*
* Sets the minimum number of children to line up
* in @box’s orientation before flowing.
*
* Since: 3.12
*/
void
gtk_flow_box_set_min_children_per_line (GtkFlowBox *box,
guint n_children)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->min_children_per_line != n_children)
{
BOX_PRIV (box)->min_children_per_line = n_children;
gtk_widget_queue_resize (GTK_WIDGET (box));
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_MIN_CHILDREN_PER_LINE]);
}
}
/**
* gtk_flow_box_get_min_children_per_line:
* @box: a #GtkFlowBox
*
* Gets the minimum number of children per line.
*
* Returns: the minimum number of children per line
*
* Since: 3.12
*/
guint
gtk_flow_box_get_min_children_per_line (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->min_children_per_line;
}
/**
* gtk_flow_box_set_max_children_per_line:
* @box: a #GtkFlowBox
* @n_children: the maximum number of children per line
*
* Sets the maximum number of children to request and
* allocate space for in @box’s orientation.
*
* Setting the maximum number of children per line
* limits the overall natural size request to be no more
* than @n_children children long in the given orientation.
*
* Since: 3.12
*/
void
gtk_flow_box_set_max_children_per_line (GtkFlowBox *box,
guint n_children)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->max_children_per_line != n_children)
{
BOX_PRIV (box)->max_children_per_line = n_children;
gtk_widget_queue_resize (GTK_WIDGET (box));
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_MAX_CHILDREN_PER_LINE]);
}
}
/**
* gtk_flow_box_get_max_children_per_line:
* @box: a #GtkFlowBox
*
* Gets the maximum number of children per line.
*
* Returns: the maximum number of children per line
*
* Since: 3.12
*/
guint
gtk_flow_box_get_max_children_per_line (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->max_children_per_line;
}
/**
* gtk_flow_box_set_activate_on_single_click:
* @box: a #GtkFlowBox
* @single: %TRUE to emit child-activated on a single click
*
* If @single is %TRUE, children will be activated when you click
* on them, otherwise you need to double-click.
*
* Since: 3.12
*/
void
gtk_flow_box_set_activate_on_single_click (GtkFlowBox *box,
gboolean single)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
single = single != FALSE;
if (BOX_PRIV (box)->activate_on_single_click != single)
{
BOX_PRIV (box)->activate_on_single_click = single;
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_ACTIVATE_ON_SINGLE_CLICK]);
}
}
/**
* gtk_flow_box_get_activate_on_single_click:
* @box: a #GtkFlowBox
*
* Returns whether children activate on single clicks.
*
* Returns: %TRUE if children are activated on single click,
* %FALSE otherwise
*
* Since: 3.12
*/
gboolean
gtk_flow_box_get_activate_on_single_click (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), FALSE);
return BOX_PRIV (box)->activate_on_single_click;
}
/* Selection handling {{{2 */
/**
* gtk_flow_box_get_selected_children:
* @box: a #GtkFlowBox
*
* Creates a list of all selected children.
*
* Returns: (element-type GtkFlowBoxChild) (transfer container):
* A #GList containing the #GtkWidget for each selected child.
* Free with g_list_free() when done.
*
* Since: 3.12
*/
GList *
gtk_flow_box_get_selected_children (GtkFlowBox *box)
{
GtkFlowBoxChild *child;
GSequenceIter *iter;
GList *selected = NULL;
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), NULL);
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
child = g_sequence_get (iter);
if (CHILD_PRIV (child)->selected)
selected = g_list_prepend (selected, child);
}
return g_list_reverse (selected);
}
/**
* gtk_flow_box_select_child:
* @box: a #GtkFlowBox
* @child: a child of @box
*
* Selects a single child of @box, if the selection
* mode allows it.
*
* Since: 3.12
*/
void
gtk_flow_box_select_child (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
g_return_if_fail (GTK_IS_FLOW_BOX_CHILD (child));
gtk_flow_box_select_child_internal (box, child);
}
/**
* gtk_flow_box_unselect_child:
* @box: a #GtkFlowBox
* @child: a child of @box
*
* Unselects a single child of @box, if the selection
* mode allows it.
*
* Since: 3.12
*/
void
gtk_flow_box_unselect_child (GtkFlowBox *box,
GtkFlowBoxChild *child)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
g_return_if_fail (GTK_IS_FLOW_BOX_CHILD (child));
gtk_flow_box_unselect_child_internal (box, child);
}
/**
* gtk_flow_box_select_all:
* @box: a #GtkFlowBox
*
* Select all children of @box, if the selection
* mode allows it.
*
* Since: 3.12
*/
void
gtk_flow_box_select_all (GtkFlowBox *box)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->selection_mode != GTK_SELECTION_MULTIPLE)
return;
if (g_sequence_get_length (BOX_PRIV (box)->children) > 0)
{
gtk_flow_box_select_all_between (box, NULL, NULL, FALSE);
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
}
/**
* gtk_flow_box_unselect_all:
* @box: a #GtkFlowBox
*
* Unselect all children of @box, if the selection
* mode allows it.
*
* Since: 3.12
*/
void
gtk_flow_box_unselect_all (GtkFlowBox *box)
{
gboolean dirty = FALSE;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->selection_mode == GTK_SELECTION_BROWSE)
return;
dirty = gtk_flow_box_unselect_all_internal (box);
if (dirty)
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
/**
* GtkFlowBoxForeachFunc:
* @box: a #GtkFlowBox
* @child: a #GtkFlowBoxChild
* @user_data: (closure): user data
*
* A function used by gtk_flow_box_selected_foreach().
* It will be called on every selected child of the @box.
*
* Since: 3.12
*/
/**
* gtk_flow_box_selected_foreach:
* @box: a #GtkFlowBox
* @func: (scope call): the function to call for each selected child
* @data: user data to pass to the function
*
* Calls a function for each selected child.
*
* Note that the selection cannot be modified from within
* this function.
*
* Since: 3.12
*/
void
gtk_flow_box_selected_foreach (GtkFlowBox *box,
GtkFlowBoxForeachFunc func,
gpointer data)
{
GtkFlowBoxChild *child;
GSequenceIter *iter;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
for (iter = g_sequence_get_begin_iter (BOX_PRIV (box)->children);
!g_sequence_iter_is_end (iter);
iter = g_sequence_iter_next (iter))
{
child = g_sequence_get (iter);
if (CHILD_PRIV (child)->selected)
(*func) (box, child, data);
}
}
/**
* gtk_flow_box_set_selection_mode:
* @box: a #GtkFlowBox
* @mode: the new selection mode
*
* Sets how selection works in @box.
* See #GtkSelectionMode for details.
*
* Since: 3.12
*/
void
gtk_flow_box_set_selection_mode (GtkFlowBox *box,
GtkSelectionMode mode)
{
gboolean dirty = FALSE;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (mode == BOX_PRIV (box)->selection_mode)
return;
if (mode == GTK_SELECTION_NONE ||
BOX_PRIV (box)->selection_mode == GTK_SELECTION_MULTIPLE)
{
dirty = gtk_flow_box_unselect_all_internal (box);
BOX_PRIV (box)->selected_child = NULL;
}
BOX_PRIV (box)->selection_mode = mode;
g_object_notify_by_pspec (G_OBJECT (box), props[PROP_SELECTION_MODE]);
if (dirty)
g_signal_emit (box, signals[SELECTED_CHILDREN_CHANGED], 0);
}
/**
* gtk_flow_box_get_selection_mode:
* @box: a #GtkFlowBox
*
* Gets the selection mode of @box.
*
* Returns: the #GtkSelectionMode
*
* Since: 3.12
*/
GtkSelectionMode
gtk_flow_box_get_selection_mode (GtkFlowBox *box)
{
g_return_val_if_fail (GTK_IS_FLOW_BOX (box), GTK_SELECTION_SINGLE);
return BOX_PRIV (box)->selection_mode;
}
/* Filtering {{{2 */
/**
* GtkFlowBoxFilterFunc:
* @child: a #GtkFlowBoxChild that may be filtered
* @user_data: (closure): user data
*
* A function that will be called whenrever a child changes
* or is added. It lets you control if the child should be
* visible or not.
*
* Returns: %TRUE if the row should be visible, %FALSE otherwise
*
* Since: 3.12
*/
/**
* gtk_flow_box_set_filter_func:
* @box: a #GtkFlowBox
* @filter_func: (closure user_data) (allow-none): callback that
* lets you filter which children to show
* @user_data: user data passed to @filter_func
* @destroy: destroy notifier for @user_data
*
* By setting a filter function on the @box one can decide dynamically
* which of the children to show. For instance, to implement a search
* function that only shows the children matching the search terms.
*
* The @filter_func will be called for each child after the call, and
* it will continue to be called each time a child changes (via
* gtk_flow_box_child_changed()) or when gtk_flow_box_invalidate_filter()
* is called.
*
* Since: 3.12
*/
void
gtk_flow_box_set_filter_func (GtkFlowBox *box,
GtkFlowBoxFilterFunc filter_func,
gpointer user_data,
GDestroyNotify destroy)
{
GtkFlowBoxPrivate *priv;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
priv = BOX_PRIV (box);
if (priv->filter_destroy != NULL)
priv->filter_destroy (priv->filter_data);
priv->filter_func = filter_func;
priv->filter_data = user_data;
priv->filter_destroy = destroy;
gtk_flow_box_apply_filter_all (box);
}
/**
* gtk_flow_box_invalidate_filter:
* @box: a #GtkFlowBox
*
* Updates the filtering for all children.
*
* Call this function when the result of the filter
* function on the @box is changed due ot an external
* factor. For instance, this would be used if the
* filter function just looked for a specific search
* term, and the entry with the string has changed.
*
* Since: 3.12
*/
void
gtk_flow_box_invalidate_filter (GtkFlowBox *box)
{
g_return_if_fail (GTK_IS_FLOW_BOX (box));
if (BOX_PRIV (box)->filter_func != NULL)
gtk_flow_box_apply_filter_all (box);
}
/* Sorting {{{2 */
/**
* GtkFlowBoxSortFunc:
* @child1: the first child
* @child2: the second child
* @user_data: (closure): user data
*
* A function to compare two children to determine which
* should come first.
*
* Returns: < 0 if @child1 should be before @child2, 0 if
* the are equal, and > 0 otherwise
*
* Since: 3.12
*/
/**
* gtk_flow_box_set_sort_func:
* @box: a #GtkFlowBox
* @sort_func: (closure user_data) (allow-none): the sort function
* @user_data: user data passed to @sort_func
* @destroy: destroy notifier for @user_data
*
* By setting a sort function on the @box, one can dynamically
* reorder the children of the box, based on the contents of
* the children.
*
* The @sort_func will be called for each child after the call,
* and will continue to be called each time a child changes (via
* gtk_flow_box_child_changed()) and when gtk_flow_box_invalidate_sort()
* is called.
*
* Since: 3.12
*/
void
gtk_flow_box_set_sort_func (GtkFlowBox *box,
GtkFlowBoxSortFunc sort_func,
gpointer user_data,
GDestroyNotify destroy)
{
GtkFlowBoxPrivate *priv;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
priv = BOX_PRIV (box);
if (priv->sort_destroy != NULL)
priv->sort_destroy (priv->sort_data);
priv->sort_func = sort_func;
priv->sort_data = user_data;
priv->sort_destroy = destroy;
gtk_flow_box_invalidate_sort (box);
}
static gint
gtk_flow_box_sort (GtkFlowBoxChild *a,
GtkFlowBoxChild *b,
GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv = BOX_PRIV (box);
return priv->sort_func (a, b, priv->sort_data);
}
static void
gtk_flow_box_css_node_foreach (gpointer data,
gpointer user_data)
{
GtkWidget **previous = user_data;
GtkWidget *row = data;
GtkCssNode *row_node;
GtkCssNode *prev_node;
if (*previous)
{
prev_node = gtk_widget_get_css_node (*previous);
row_node = gtk_widget_get_css_node (row);
gtk_css_node_insert_after (gtk_css_node_get_parent (row_node),
row_node,
prev_node);
}
*previous = row;
}
/**
* gtk_flow_box_invalidate_sort:
* @box: a #GtkFlowBox
*
* Updates the sorting for all children.
*
* Call this when the result of the sort function on
* @box is changed due to an external factor.
*
* Since: 3.12
*/
void
gtk_flow_box_invalidate_sort (GtkFlowBox *box)
{
GtkFlowBoxPrivate *priv;
GtkWidget *previous = NULL;
g_return_if_fail (GTK_IS_FLOW_BOX (box));
priv = BOX_PRIV (box);
if (priv->sort_func != NULL)
{
g_sequence_sort (priv->children, (GCompareDataFunc)gtk_flow_box_sort, box);
g_sequence_foreach (priv->children, gtk_flow_box_css_node_foreach, &previous);
gtk_widget_queue_resize (GTK_WIDGET (box));
}
}
/* vim:set foldmethod=marker expandtab: */
|